blob: e77735d804a395c0c361f421a21b1dc0dc561d79 [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
Takashi Iwai84eb01b2010-09-07 12:27:25 +020047/*
48 * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
Stephen Warren384a48d2011-06-01 11:14:21 -060049 * could support N independent pipes, each of them can be connected to one or
Takashi Iwai84eb01b2010-09-07 12:27:25 +020050 * more ports (DVI, HDMI or DisplayPort).
51 *
52 * The HDA correspondence of pipes/ports are converter/pin nodes.
53 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +010054#define MAX_HDMI_CVTS 8
55#define MAX_HDMI_PINS 8
Wu Fengguang079d88c2010-03-08 10:44:23 +080056
Stephen Warren384a48d2011-06-01 11:14:21 -060057struct hdmi_spec_per_cvt {
58 hda_nid_t cvt_nid;
59 int assigned;
60 unsigned int channels_min;
61 unsigned int channels_max;
62 u32 rates;
63 u64 formats;
64 unsigned int maxbps;
65};
66
Takashi Iwai4eea3092013-02-07 18:18:19 +010067/* max. connections to a widget */
68#define HDA_MAX_CONNECTIONS 32
69
Stephen Warren384a48d2011-06-01 11:14:21 -060070struct hdmi_spec_per_pin {
71 hda_nid_t pin_nid;
72 int num_mux_nids;
73 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080074
75 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060076 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080077 struct delayed_work work;
Wu Fengguangc6e84532011-11-18 16:59:32 -060078 int repoll_count;
Takashi Iwai1a6003b2012-09-06 17:42:08 +020079 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020080 bool chmap_set; /* channel-map override by ALSA API? */
81 unsigned char chmap[8]; /* ALSA API channel-map */
Stephen Warren384a48d2011-06-01 11:14:21 -060082};
83
Wu Fengguang079d88c2010-03-08 10:44:23 +080084struct hdmi_spec {
85 int num_cvts;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
Takashi Iwaic88d4e82013-02-08 17:10:04 -050087 hda_nid_t cvt_nids[MAX_HDMI_CVTS];
Stephen Warren384a48d2011-06-01 11:14:21 -060088
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060090 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
91 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Takashi Iwaid45e6882012-07-31 11:36:00 +020092 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080093
David Henningsson4bd038f2013-02-19 16:11:25 +010094 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080095 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060096 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080097 */
98 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020099 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800100};
101
102
103struct hdmi_audio_infoframe {
104 u8 type; /* 0x84 */
105 u8 ver; /* 0x01 */
106 u8 len; /* 0x0a */
107
Wu Fengguang53d7d692010-09-21 14:25:49 +0800108 u8 checksum;
109
Wu Fengguang079d88c2010-03-08 10:44:23 +0800110 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
111 u8 SS01_SF24;
112 u8 CXT04;
113 u8 CA;
114 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800115};
116
117struct dp_audio_infoframe {
118 u8 type; /* 0x84 */
119 u8 len; /* 0x1b */
120 u8 ver; /* 0x11 << 2 */
121
122 u8 CC02_CT47; /* match with HDMI infoframe from this on */
123 u8 SS01_SF24;
124 u8 CXT04;
125 u8 CA;
126 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800127};
128
Takashi Iwai2b203db2011-02-11 12:17:30 +0100129union audio_infoframe {
130 struct hdmi_audio_infoframe hdmi;
131 struct dp_audio_infoframe dp;
132 u8 bytes[0];
133};
134
Wu Fengguang079d88c2010-03-08 10:44:23 +0800135/*
136 * CEA speaker placement:
137 *
138 * FLH FCH FRH
139 * FLW FL FLC FC FRC FR FRW
140 *
141 * LFE
142 * TC
143 *
144 * RL RLC RC RRC RR
145 *
146 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
147 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
148 */
149enum cea_speaker_placement {
150 FL = (1 << 0), /* Front Left */
151 FC = (1 << 1), /* Front Center */
152 FR = (1 << 2), /* Front Right */
153 FLC = (1 << 3), /* Front Left Center */
154 FRC = (1 << 4), /* Front Right Center */
155 RL = (1 << 5), /* Rear Left */
156 RC = (1 << 6), /* Rear Center */
157 RR = (1 << 7), /* Rear Right */
158 RLC = (1 << 8), /* Rear Left Center */
159 RRC = (1 << 9), /* Rear Right Center */
160 LFE = (1 << 10), /* Low Frequency Effect */
161 FLW = (1 << 11), /* Front Left Wide */
162 FRW = (1 << 12), /* Front Right Wide */
163 FLH = (1 << 13), /* Front Left High */
164 FCH = (1 << 14), /* Front Center High */
165 FRH = (1 << 15), /* Front Right High */
166 TC = (1 << 16), /* Top Center */
167};
168
169/*
170 * ELD SA bits in the CEA Speaker Allocation data block
171 */
172static int eld_speaker_allocation_bits[] = {
173 [0] = FL | FR,
174 [1] = LFE,
175 [2] = FC,
176 [3] = RL | RR,
177 [4] = RC,
178 [5] = FLC | FRC,
179 [6] = RLC | RRC,
180 /* the following are not defined in ELD yet */
181 [7] = FLW | FRW,
182 [8] = FLH | FRH,
183 [9] = TC,
184 [10] = FCH,
185};
186
187struct cea_channel_speaker_allocation {
188 int ca_index;
189 int speakers[8];
190
191 /* derived values, just for convenience */
192 int channels;
193 int spk_mask;
194};
195
196/*
197 * ALSA sequence is:
198 *
199 * surround40 surround41 surround50 surround51 surround71
200 * ch0 front left = = = =
201 * ch1 front right = = = =
202 * ch2 rear left = = = =
203 * ch3 rear right = = = =
204 * ch4 LFE center center center
205 * ch5 LFE LFE
206 * ch6 side left
207 * ch7 side right
208 *
209 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
210 */
211static int hdmi_channel_mapping[0x32][8] = {
212 /* stereo */
213 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
214 /* 2.1 */
215 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
216 /* Dolby Surround */
217 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
218 /* surround40 */
219 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
220 /* 4ch */
221 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
222 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800223 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800224 /* surround50 */
225 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
226 /* surround51 */
227 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
228 /* 7.1 */
229 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
230};
231
232/*
233 * This is an ordered list!
234 *
235 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800236 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800237 */
238static struct cea_channel_speaker_allocation channel_allocations[] = {
239/* channel: 7 6 5 4 3 2 1 0 */
240{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
241 /* 2.1 */
242{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
243 /* Dolby Surround */
244{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
245 /* surround40 */
246{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
247 /* surround41 */
248{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
249 /* surround50 */
250{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
251 /* surround51 */
252{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
253 /* 6.1 */
254{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
255 /* surround71 */
256{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
257
258{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
259{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
260{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
261{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
262{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
263{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
264{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
265{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
266{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
267{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
268{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
269{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
270{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
271{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
272{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
273{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
274{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
275{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
276{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
277{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
278{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
279{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
280{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
281{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
282{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
283{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
284{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
285{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
286{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
287{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
288{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
289{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
291{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
292{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
293{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
294{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
295{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
296{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
297{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
298{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
299};
300
301
302/*
303 * HDMI routines
304 */
305
Stephen Warren384a48d2011-06-01 11:14:21 -0600306static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800307{
Stephen Warren384a48d2011-06-01 11:14:21 -0600308 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800309
Stephen Warren384a48d2011-06-01 11:14:21 -0600310 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
311 if (spec->pins[pin_idx].pin_nid == pin_nid)
312 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800313
Stephen Warren384a48d2011-06-01 11:14:21 -0600314 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
315 return -EINVAL;
316}
317
318static int hinfo_to_pin_index(struct hdmi_spec *spec,
319 struct hda_pcm_stream *hinfo)
320{
321 int pin_idx;
322
323 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
324 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
325 return pin_idx;
326
327 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
328 return -EINVAL;
329}
330
331static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
332{
333 int cvt_idx;
334
335 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
336 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
337 return cvt_idx;
338
339 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800340 return -EINVAL;
341}
342
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500343static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
344 struct snd_ctl_elem_info *uinfo)
345{
346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100347 struct hdmi_spec *spec = codec->spec;
348 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500349 int pin_idx;
350
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500351 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
352
353 pin_idx = kcontrol->private_value;
David Henningsson68e03de2013-02-19 16:11:23 +0100354 eld = &spec->pins[pin_idx].sink_eld;
355
David Henningsson4bd038f2013-02-19 16:11:25 +0100356 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100357 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100358 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500359
360 return 0;
361}
362
363static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
364 struct snd_ctl_elem_value *ucontrol)
365{
366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100367 struct hdmi_spec *spec = codec->spec;
368 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500369 int pin_idx;
370
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500371 pin_idx = kcontrol->private_value;
David Henningsson68e03de2013-02-19 16:11:23 +0100372 eld = &spec->pins[pin_idx].sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500373
David Henningsson4bd038f2013-02-19 16:11:25 +0100374 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100375 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100376 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100377 snd_BUG();
378 return -EINVAL;
379 }
380
381 memset(ucontrol->value.bytes.data, 0,
382 ARRAY_SIZE(ucontrol->value.bytes.data));
383 if (eld->eld_valid)
384 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
385 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100386 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500387
388 return 0;
389}
390
391static struct snd_kcontrol_new eld_bytes_ctl = {
392 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
393 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
394 .name = "ELD",
395 .info = hdmi_eld_ctl_info,
396 .get = hdmi_eld_ctl_get,
397};
398
399static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
400 int device)
401{
402 struct snd_kcontrol *kctl;
403 struct hdmi_spec *spec = codec->spec;
404 int err;
405
406 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
407 if (!kctl)
408 return -ENOMEM;
409 kctl->private_value = pin_idx;
410 kctl->id.device = device;
411
412 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
413 if (err < 0)
414 return err;
415
416 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;
Stephen Warren384a48d2011-06-01 11:14:21 -0600876 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
877 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 Iwai2b203db2011-02-11 12:17:30 +0100881 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800882
Stephen Warren384a48d2011-06-01 11:14:21 -0600883 eld = &spec->pins[pin_idx].sink_eld;
884 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
Wu Fengguangc6e84532011-11-18 16:59:32 -0600978 hdmi_present_sense(&spec->pins[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
1021/*
1022 * Callbacks
1023 */
1024
Takashi Iwai92f10b32010-08-03 14:21:00 +02001025/* HBR should be Non-PCM, 8 channels */
1026#define is_hbr_format(format) \
1027 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1028
Stephen Warren384a48d2011-06-01 11:14:21 -06001029static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1030 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001031{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001032 int pinctl;
1033 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001034
Stephen Warren384a48d2011-06-01 11:14:21 -06001035 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1036 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001037 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1038
1039 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001040 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001041 new_pinctl |= AC_PINCTL_EPT_HBR;
1042 else
1043 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1044
1045 snd_printdd("hdmi_setup_stream: "
1046 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001047 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001048 pinctl == new_pinctl ? "" : "new-",
1049 new_pinctl);
1050
1051 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001052 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001053 AC_VERB_SET_PIN_WIDGET_CONTROL,
1054 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001055
Stephen Warren384a48d2011-06-01 11:14:21 -06001056 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001057 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001058 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1059 return -EINVAL;
1060 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001061
Stephen Warren384a48d2011-06-01 11:14:21 -06001062 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001063 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001064}
1065
1066/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001067 * HDA PCM callbacks
1068 */
1069static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1070 struct hda_codec *codec,
1071 struct snd_pcm_substream *substream)
1072{
1073 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001074 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001075 int pin_idx, cvt_idx, mux_idx = 0;
1076 struct hdmi_spec_per_pin *per_pin;
1077 struct hdmi_eld *eld;
1078 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001079
Stephen Warren384a48d2011-06-01 11:14:21 -06001080 /* Validate hinfo */
1081 pin_idx = hinfo_to_pin_index(spec, hinfo);
1082 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001083 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -06001084 per_pin = &spec->pins[pin_idx];
1085 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001086
Stephen Warren384a48d2011-06-01 11:14:21 -06001087 /* Dynamically assign converter to stream */
1088 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1089 per_cvt = &spec->cvts[cvt_idx];
1090
1091 /* Must not already be assigned */
1092 if (per_cvt->assigned)
1093 continue;
1094 /* Must be in pin's mux's list of converters */
1095 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1096 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1097 break;
1098 /* Not in mux list */
1099 if (mux_idx == per_pin->num_mux_nids)
1100 continue;
1101 break;
1102 }
1103 /* No free converters */
1104 if (cvt_idx == spec->num_cvts)
1105 return -ENODEV;
1106
1107 /* Claim converter */
1108 per_cvt->assigned = 1;
1109 hinfo->nid = per_cvt->cvt_nid;
1110
1111 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1112 AC_VERB_SET_CONNECT_SEL,
1113 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001114 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001115
Stephen Warren2def8172011-06-01 11:14:20 -06001116 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001117 hinfo->channels_min = per_cvt->channels_min;
1118 hinfo->channels_max = per_cvt->channels_max;
1119 hinfo->rates = per_cvt->rates;
1120 hinfo->formats = per_cvt->formats;
1121 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001122
Stephen Warren384a48d2011-06-01 11:14:21 -06001123 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001124 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001125 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001126 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001127 !hinfo->rates || !hinfo->formats) {
1128 per_cvt->assigned = 0;
1129 hinfo->nid = 0;
1130 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001131 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001132 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001133 }
Stephen Warren2def8172011-06-01 11:14:20 -06001134
1135 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001136 runtime->hw.channels_min = hinfo->channels_min;
1137 runtime->hw.channels_max = hinfo->channels_max;
1138 runtime->hw.formats = hinfo->formats;
1139 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001140
1141 snd_pcm_hw_constraint_step(substream->runtime, 0,
1142 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001143 return 0;
1144}
1145
1146/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001147 * HDA/HDMI auto parsing
1148 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001149static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001150{
1151 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001152 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1153 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001154
1155 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1156 snd_printk(KERN_WARNING
1157 "HDMI: pin %d wcaps %#x "
1158 "does not support connection list\n",
1159 pin_nid, get_wcaps(codec, pin_nid));
1160 return -EINVAL;
1161 }
1162
Stephen Warren384a48d2011-06-01 11:14:21 -06001163 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1164 per_pin->mux_nids,
1165 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001166
1167 return 0;
1168}
1169
Wu Fengguangc6e84532011-11-18 16:59:32 -06001170static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001171{
Wu Fengguang744626d2011-11-16 16:29:47 +08001172 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001173 struct hdmi_spec *spec = codec->spec;
1174 struct hdmi_eld *eld = &spec->temp_eld;
1175 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001176 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001177 /*
1178 * Always execute a GetPinSense verb here, even when called from
1179 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1180 * response's PD bit is not the real PD value, but indicates that
1181 * the real PD value changed. An older version of the HD-audio
1182 * specification worked this way. Hence, we just ignore the data in
1183 * the unsolicited response to avoid custom WARs.
1184 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001185 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001186 bool update_eld = false;
1187 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001188
David Henningsson4bd038f2013-02-19 16:11:25 +01001189 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1190 if (pin_eld->monitor_present)
1191 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1192 else
1193 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001194
Fengguang Wufae3d882012-04-10 17:00:35 +08001195 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001196 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
David Henningsson4bd038f2013-02-19 16:11:25 +01001197 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001198
David Henningsson4bd038f2013-02-19 16:11:25 +01001199 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001200 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1201 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001202 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001203 else {
1204 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1205 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1206 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001207 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001208 }
1209
David Henningsson4bd038f2013-02-19 16:11:25 +01001210 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001211 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001212 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001213 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001214 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001215 queue_delayed_work(codec->bus->workq,
1216 &per_pin->work,
1217 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001218 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001219 }
1220 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001221
1222 mutex_lock(&pin_eld->lock);
1223 if (pin_eld->eld_valid && !eld->eld_valid)
1224 update_eld = true;
1225 if (update_eld) {
1226 pin_eld->eld_valid = eld->eld_valid;
1227 eld_changed = memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1228 eld->eld_size) != 0;
1229 if (eld_changed)
1230 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1231 eld->eld_size);
1232 pin_eld->eld_size = eld->eld_size;
1233 pin_eld->info = eld->info;
1234 }
1235 mutex_unlock(&pin_eld->lock);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001236}
1237
Wu Fengguang744626d2011-11-16 16:29:47 +08001238static void hdmi_repoll_eld(struct work_struct *work)
1239{
1240 struct hdmi_spec_per_pin *per_pin =
1241 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1242
Wu Fengguangc6e84532011-11-18 16:59:32 -06001243 if (per_pin->repoll_count++ > 6)
1244 per_pin->repoll_count = 0;
1245
1246 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001247}
1248
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001249static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1250 hda_nid_t nid);
1251
Wu Fengguang079d88c2010-03-08 10:44:23 +08001252static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1253{
1254 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001255 unsigned int caps, config;
1256 int pin_idx;
1257 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001258 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001259
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001260 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001261 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1262 return 0;
1263
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001264 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001265 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1266 return 0;
1267
1268 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001269 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001270
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001271 if (codec->vendor_id == 0x80862807)
1272 intel_haswell_fixup_connect_list(codec, pin_nid);
1273
Stephen Warren384a48d2011-06-01 11:14:21 -06001274 pin_idx = spec->num_pins;
1275 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001276
1277 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001278 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001279
Stephen Warren384a48d2011-06-01 11:14:21 -06001280 err = hdmi_read_pin_conn(codec, pin_idx);
1281 if (err < 0)
1282 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001283
Wu Fengguang079d88c2010-03-08 10:44:23 +08001284 spec->num_pins++;
1285
Stephen Warren384a48d2011-06-01 11:14:21 -06001286 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001287}
1288
Stephen Warren384a48d2011-06-01 11:14:21 -06001289static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001290{
1291 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001292 int cvt_idx;
1293 struct hdmi_spec_per_cvt *per_cvt;
1294 unsigned int chans;
1295 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001296
David Henningsson116dcde2010-11-23 10:23:40 +01001297 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1298 return -E2BIG;
1299
Stephen Warren384a48d2011-06-01 11:14:21 -06001300 chans = get_wcaps(codec, cvt_nid);
1301 chans = get_wcaps_channels(chans);
1302
1303 cvt_idx = spec->num_cvts;
1304 per_cvt = &spec->cvts[cvt_idx];
1305
1306 per_cvt->cvt_nid = cvt_nid;
1307 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001308 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001309 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001310 if (chans > spec->channels_max)
1311 spec->channels_max = chans;
1312 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001313
1314 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1315 &per_cvt->rates,
1316 &per_cvt->formats,
1317 &per_cvt->maxbps);
1318 if (err < 0)
1319 return err;
1320
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001321 spec->cvt_nids[spec->num_cvts++] = cvt_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001322
1323 return 0;
1324}
1325
1326static int hdmi_parse_codec(struct hda_codec *codec)
1327{
1328 hda_nid_t nid;
1329 int i, nodes;
1330
1331 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1332 if (!nid || nodes < 0) {
1333 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1334 return -EINVAL;
1335 }
1336
1337 for (i = 0; i < nodes; i++, nid++) {
1338 unsigned int caps;
1339 unsigned int type;
1340
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001341 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001342 type = get_wcaps_type(caps);
1343
1344 if (!(caps & AC_WCAP_DIGITAL))
1345 continue;
1346
1347 switch (type) {
1348 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001349 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001350 break;
1351 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001352 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001353 break;
1354 }
1355 }
1356
David Henningssonc9adeef2012-11-07 09:22:33 +01001357#ifdef CONFIG_PM
1358 /* We're seeing some problems with unsolicited hot plug events on
1359 * PantherPoint after S3, if this is not enabled */
1360 if (codec->vendor_id == 0x80862806)
1361 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001362 /*
1363 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1364 * can be lost and presence sense verb will become inaccurate if the
1365 * HDA link is powered off at hot plug or hw initialization time.
1366 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001367 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001368 AC_PWRST_EPSS))
1369 codec->bus->power_keep_link_on = 1;
1370#endif
1371
1372 return 0;
1373}
1374
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001375/*
1376 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001377static char *get_hdmi_pcm_name(int idx)
1378{
1379 static char names[MAX_HDMI_PINS][8];
1380 sprintf(&names[idx][0], "HDMI %d", idx);
1381 return &names[idx][0];
1382}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001383
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001384static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1385{
1386 struct hda_spdif_out *spdif;
1387 bool non_pcm;
1388
1389 mutex_lock(&codec->spdif_mutex);
1390 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1391 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1392 mutex_unlock(&codec->spdif_mutex);
1393 return non_pcm;
1394}
1395
1396
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001397/*
1398 * HDMI callbacks
1399 */
1400
1401static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1402 struct hda_codec *codec,
1403 unsigned int stream_tag,
1404 unsigned int format,
1405 struct snd_pcm_substream *substream)
1406{
Stephen Warren384a48d2011-06-01 11:14:21 -06001407 hda_nid_t cvt_nid = hinfo->nid;
1408 struct hdmi_spec *spec = codec->spec;
1409 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1410 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001411 bool non_pcm;
1412
1413 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001414
Stephen Warren384a48d2011-06-01 11:14:21 -06001415 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001416
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001417 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001418
1419 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001420}
1421
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001422static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1423 struct hda_codec *codec,
1424 struct snd_pcm_substream *substream)
1425{
1426 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1427 return 0;
1428}
1429
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001430static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1431 struct hda_codec *codec,
1432 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001433{
1434 struct hdmi_spec *spec = codec->spec;
1435 int cvt_idx, pin_idx;
1436 struct hdmi_spec_per_cvt *per_cvt;
1437 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001438
Stephen Warren384a48d2011-06-01 11:14:21 -06001439 if (hinfo->nid) {
1440 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1441 if (snd_BUG_ON(cvt_idx < 0))
1442 return -EINVAL;
1443 per_cvt = &spec->cvts[cvt_idx];
1444
1445 snd_BUG_ON(!per_cvt->assigned);
1446 per_cvt->assigned = 0;
1447 hinfo->nid = 0;
1448
1449 pin_idx = hinfo_to_pin_index(spec, hinfo);
1450 if (snd_BUG_ON(pin_idx < 0))
1451 return -EINVAL;
1452 per_pin = &spec->pins[pin_idx];
1453
Stephen Warren384a48d2011-06-01 11:14:21 -06001454 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001455 per_pin->chmap_set = false;
1456 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001457 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001458
Stephen Warren384a48d2011-06-01 11:14:21 -06001459 return 0;
1460}
1461
1462static const struct hda_pcm_ops generic_ops = {
1463 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001464 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001465 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001466 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001467};
1468
Takashi Iwaid45e6882012-07-31 11:36:00 +02001469/*
1470 * ALSA API channel-map control callbacks
1471 */
1472static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1473 struct snd_ctl_elem_info *uinfo)
1474{
1475 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1476 struct hda_codec *codec = info->private_data;
1477 struct hdmi_spec *spec = codec->spec;
1478 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1479 uinfo->count = spec->channels_max;
1480 uinfo->value.integer.min = 0;
1481 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1482 return 0;
1483}
1484
1485static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1486 unsigned int size, unsigned int __user *tlv)
1487{
1488 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1489 struct hda_codec *codec = info->private_data;
1490 struct hdmi_spec *spec = codec->spec;
1491 const unsigned int valid_mask =
1492 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1493 unsigned int __user *dst;
1494 int chs, count = 0;
1495
1496 if (size < 8)
1497 return -ENOMEM;
1498 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1499 return -EFAULT;
1500 size -= 8;
1501 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001502 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001503 int i, c;
1504 struct cea_channel_speaker_allocation *cap;
1505 cap = channel_allocations;
1506 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1507 int chs_bytes = chs * 4;
1508 if (cap->channels != chs)
1509 continue;
1510 if (cap->spk_mask & ~valid_mask)
1511 continue;
1512 if (size < 8)
1513 return -ENOMEM;
1514 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1515 put_user(chs_bytes, dst + 1))
1516 return -EFAULT;
1517 dst += 2;
1518 size -= 8;
1519 count += 8;
1520 if (size < chs_bytes)
1521 return -ENOMEM;
1522 size -= chs_bytes;
1523 count += chs_bytes;
1524 for (c = 7; c >= 0; c--) {
1525 int spk = cap->speakers[c];
1526 if (!spk)
1527 continue;
1528 if (put_user(spk_to_chmap(spk), dst))
1529 return -EFAULT;
1530 dst++;
1531 }
1532 }
1533 }
1534 if (put_user(count, tlv + 1))
1535 return -EFAULT;
1536 return 0;
1537}
1538
1539static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_value *ucontrol)
1541{
1542 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1543 struct hda_codec *codec = info->private_data;
1544 struct hdmi_spec *spec = codec->spec;
1545 int pin_idx = kcontrol->private_value;
1546 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1547 int i;
1548
1549 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1550 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1551 return 0;
1552}
1553
1554static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1555 struct snd_ctl_elem_value *ucontrol)
1556{
1557 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1558 struct hda_codec *codec = info->private_data;
1559 struct hdmi_spec *spec = codec->spec;
1560 int pin_idx = kcontrol->private_value;
1561 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1562 unsigned int ctl_idx;
1563 struct snd_pcm_substream *substream;
1564 unsigned char chmap[8];
1565 int i, ca, prepared = 0;
1566
1567 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1568 substream = snd_pcm_chmap_substream(info, ctl_idx);
1569 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001570 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001571 switch (substream->runtime->status->state) {
1572 case SNDRV_PCM_STATE_OPEN:
1573 case SNDRV_PCM_STATE_SETUP:
1574 break;
1575 case SNDRV_PCM_STATE_PREPARED:
1576 prepared = 1;
1577 break;
1578 default:
1579 return -EBUSY;
1580 }
1581 memset(chmap, 0, sizeof(chmap));
1582 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1583 chmap[i] = ucontrol->value.integer.value[i];
1584 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1585 return 0;
1586 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1587 if (ca < 0)
1588 return -EINVAL;
1589 per_pin->chmap_set = true;
1590 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1591 if (prepared)
1592 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1593 substream);
1594
1595 return 0;
1596}
1597
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001598static int generic_hdmi_build_pcms(struct hda_codec *codec)
1599{
1600 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001601 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001602
Stephen Warren384a48d2011-06-01 11:14:21 -06001603 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1604 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001605 struct hda_pcm_stream *pstr;
1606
Stephen Warren384a48d2011-06-01 11:14:21 -06001607 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001608 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001609 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001610 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001611
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001612 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001613 pstr->substreams = 1;
1614 pstr->ops = generic_ops;
1615 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001616 }
1617
Stephen Warren384a48d2011-06-01 11:14:21 -06001618 codec->num_pcms = spec->num_pins;
1619 codec->pcm_info = spec->pcm_rec;
1620
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001621 return 0;
1622}
1623
David Henningsson0b6c49b2011-08-23 16:56:03 +02001624static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1625{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001626 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001627 struct hdmi_spec *spec = codec->spec;
1628 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1629 int pcmdev = spec->pcm_rec[pin_idx].device;
1630
Takashi Iwai31ef2252011-12-01 17:41:36 +01001631 if (pcmdev > 0)
1632 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001633
Takashi Iwai31ef2252011-12-01 17:41:36 +01001634 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001635}
1636
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001637static int generic_hdmi_build_controls(struct hda_codec *codec)
1638{
1639 struct hdmi_spec *spec = codec->spec;
1640 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001641 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001642
Stephen Warren384a48d2011-06-01 11:14:21 -06001643 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1644 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001645
1646 err = generic_hdmi_build_jack(codec, pin_idx);
1647 if (err < 0)
1648 return err;
1649
Takashi Iwaidcda5802012-10-12 17:24:51 +02001650 err = snd_hda_create_dig_out_ctls(codec,
1651 per_pin->pin_nid,
1652 per_pin->mux_nids[0],
1653 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001654 if (err < 0)
1655 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001656 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001657
1658 /* add control for ELD Bytes */
1659 err = hdmi_create_eld_ctl(codec,
1660 pin_idx,
1661 spec->pcm_rec[pin_idx].device);
1662
1663 if (err < 0)
1664 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001665
Takashi Iwai82b1d732011-12-20 15:53:07 +01001666 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001667 }
1668
Takashi Iwaid45e6882012-07-31 11:36:00 +02001669 /* add channel maps */
1670 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1671 struct snd_pcm_chmap *chmap;
1672 struct snd_kcontrol *kctl;
1673 int i;
1674 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1675 SNDRV_PCM_STREAM_PLAYBACK,
1676 NULL, 0, pin_idx, &chmap);
1677 if (err < 0)
1678 return err;
1679 /* override handlers */
1680 chmap->private_data = codec;
1681 kctl = chmap->kctl;
1682 for (i = 0; i < kctl->count; i++)
1683 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1684 kctl->info = hdmi_chmap_ctl_info;
1685 kctl->get = hdmi_chmap_ctl_get;
1686 kctl->put = hdmi_chmap_ctl_put;
1687 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1688 }
1689
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001690 return 0;
1691}
1692
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001693static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1694{
1695 struct hdmi_spec *spec = codec->spec;
1696 int pin_idx;
1697
1698 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1699 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1700 struct hdmi_eld *eld = &per_pin->sink_eld;
1701
1702 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001703 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001704 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1705 snd_hda_eld_proc_new(codec, eld, pin_idx);
1706 }
1707 return 0;
1708}
1709
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001710static int generic_hdmi_init(struct hda_codec *codec)
1711{
1712 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001713 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001714
Stephen Warren384a48d2011-06-01 11:14:21 -06001715 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1716 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1717 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001718
1719 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001720 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001721 }
1722 return 0;
1723}
1724
1725static void generic_hdmi_free(struct hda_codec *codec)
1726{
1727 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001728 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001729
Stephen Warren384a48d2011-06-01 11:14:21 -06001730 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1731 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1732 struct hdmi_eld *eld = &per_pin->sink_eld;
1733
Wu Fengguang744626d2011-11-16 16:29:47 +08001734 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001735 snd_hda_eld_proc_free(codec, eld);
1736 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001737
Wu Fengguang744626d2011-11-16 16:29:47 +08001738 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001739 kfree(spec);
1740}
1741
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001742static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001743 .init = generic_hdmi_init,
1744 .free = generic_hdmi_free,
1745 .build_pcms = generic_hdmi_build_pcms,
1746 .build_controls = generic_hdmi_build_controls,
1747 .unsol_event = hdmi_unsol_event,
1748};
1749
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001750
1751static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1752 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001753{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001754 struct hdmi_spec *spec = codec->spec;
1755 hda_nid_t conns[4];
1756 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001757
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001758 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1759 if (nconns == spec->num_cvts &&
1760 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001761 return;
1762
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001763 /* override pins connection list */
1764 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1765 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001766}
1767
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001768#define INTEL_VENDOR_NID 0x08
1769#define INTEL_GET_VENDOR_VERB 0xf81
1770#define INTEL_SET_VENDOR_VERB 0x781
1771#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1772#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1773
1774static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1775 const struct hda_fixup *fix, int action)
1776{
1777 unsigned int vendor_param;
1778
1779 if (action != HDA_FIXUP_ACT_PRE_PROBE)
1780 return;
1781 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1782 INTEL_GET_VENDOR_VERB, 0);
1783 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1784 return;
1785
1786 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1787 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1788 INTEL_SET_VENDOR_VERB, vendor_param);
1789 if (vendor_param == -1)
1790 return;
1791
1792 snd_hda_codec_update_widgets(codec);
1793 return;
1794}
1795
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001796static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1797{
1798 unsigned int vendor_param;
1799
1800 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1801 INTEL_GET_VENDOR_VERB, 0);
1802 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1803 return;
1804
1805 /* enable DP1.2 mode */
1806 vendor_param |= INTEL_EN_DP12;
1807 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1808 INTEL_SET_VENDOR_VERB, vendor_param);
1809}
1810
1811
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001812
1813/* available models for fixup */
1814enum {
1815 INTEL_HASWELL,
1816};
1817
1818static const struct hda_model_fixup hdmi_models[] = {
1819 {.id = INTEL_HASWELL, .name = "Haswell"},
1820 {}
1821};
1822
1823static const struct snd_pci_quirk hdmi_fixup_tbl[] = {
1824 SND_PCI_QUIRK(0x8086, 0x2010, "Haswell", INTEL_HASWELL),
1825 {} /* terminator */
1826};
1827
1828static const struct hda_fixup hdmi_fixups[] = {
1829 [INTEL_HASWELL] = {
1830 .type = HDA_FIXUP_FUNC,
1831 .v.func = intel_haswell_enable_all_pins,
1832 },
1833};
1834
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001835
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001836static int patch_generic_hdmi(struct hda_codec *codec)
1837{
1838 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001839
1840 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1841 if (spec == NULL)
1842 return -ENOMEM;
1843
1844 codec->spec = spec;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001845
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001846 snd_hda_pick_fixup(codec, hdmi_models, hdmi_fixup_tbl, hdmi_fixups);
1847 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1848
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001849 if (codec->vendor_id == 0x80862807)
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001850 intel_haswell_fixup_enable_dp12(codec);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001851
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001852 if (hdmi_parse_codec(codec) < 0) {
1853 codec->spec = NULL;
1854 kfree(spec);
1855 return -EINVAL;
1856 }
1857 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001858 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001859
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001860 init_channel_allocations();
1861
1862 return 0;
1863}
1864
1865/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001866 * Shared non-generic implementations
1867 */
1868
1869static int simple_playback_build_pcms(struct hda_codec *codec)
1870{
1871 struct hdmi_spec *spec = codec->spec;
1872 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001873 unsigned int chans;
1874 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001875
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001876 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001877 codec->pcm_info = info;
1878
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001879 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1880 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001881
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001882 info->name = get_hdmi_pcm_name(0);
1883 info->pcm_type = HDA_PCM_TYPE_HDMI;
1884 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1885 *pstr = spec->pcm_playback;
1886 pstr->nid = spec->cvts[0].cvt_nid;
1887 if (pstr->channels_max <= 2 && chans && chans <= 16)
1888 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001889
1890 return 0;
1891}
1892
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001893/* unsolicited event for jack sensing */
1894static void simple_hdmi_unsol_event(struct hda_codec *codec,
1895 unsigned int res)
1896{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001897 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001898 snd_hda_jack_report_sync(codec);
1899}
1900
1901/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1902 * as long as spec->pins[] is set correctly
1903 */
1904#define simple_hdmi_build_jack generic_hdmi_build_jack
1905
Stephen Warren3aaf8982011-06-01 11:14:19 -06001906static int simple_playback_build_controls(struct hda_codec *codec)
1907{
1908 struct hdmi_spec *spec = codec->spec;
1909 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001910
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001911 err = snd_hda_create_spdif_out_ctls(codec,
1912 spec->cvts[0].cvt_nid,
1913 spec->cvts[0].cvt_nid);
1914 if (err < 0)
1915 return err;
1916 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001917}
1918
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001919static int simple_playback_init(struct hda_codec *codec)
1920{
1921 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001922 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001923
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001924 snd_hda_codec_write(codec, pin, 0,
1925 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1926 /* some codecs require to unmute the pin */
1927 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1928 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1929 AMP_OUT_UNMUTE);
1930 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001931 return 0;
1932}
1933
Stephen Warren3aaf8982011-06-01 11:14:19 -06001934static void simple_playback_free(struct hda_codec *codec)
1935{
1936 struct hdmi_spec *spec = codec->spec;
1937
1938 kfree(spec);
1939}
1940
1941/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001942 * Nvidia specific implementations
1943 */
1944
1945#define Nv_VERB_SET_Channel_Allocation 0xF79
1946#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1947#define Nv_VERB_SET_Audio_Protection_On 0xF98
1948#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1949
1950#define nvhdmi_master_con_nid_7x 0x04
1951#define nvhdmi_master_pin_nid_7x 0x05
1952
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001953static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001954 /*front, rear, clfe, rear_surr */
1955 0x6, 0x8, 0xa, 0xc,
1956};
1957
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001958static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1959 /* set audio protect on */
1960 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1961 /* enable digital output on pin widget */
1962 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1963 {} /* terminator */
1964};
1965
1966static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001967 /* set audio protect on */
1968 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1969 /* enable digital output on pin widget */
1970 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1971 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1972 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1973 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1974 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1975 {} /* terminator */
1976};
1977
1978#ifdef LIMITED_RATE_FMT_SUPPORT
1979/* support only the safe format and rate */
1980#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1981#define SUPPORTED_MAXBPS 16
1982#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1983#else
1984/* support all rates and formats */
1985#define SUPPORTED_RATES \
1986 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1987 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1988 SNDRV_PCM_RATE_192000)
1989#define SUPPORTED_MAXBPS 24
1990#define SUPPORTED_FORMATS \
1991 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1992#endif
1993
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001994static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001995{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001996 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1997 return 0;
1998}
1999
2000static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2001{
2002 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002003 return 0;
2004}
2005
Nitin Daga393004b2011-01-10 21:49:31 +05302006static unsigned int channels_2_6_8[] = {
2007 2, 6, 8
2008};
2009
2010static unsigned int channels_2_8[] = {
2011 2, 8
2012};
2013
2014static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2015 .count = ARRAY_SIZE(channels_2_6_8),
2016 .list = channels_2_6_8,
2017 .mask = 0,
2018};
2019
2020static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2021 .count = ARRAY_SIZE(channels_2_8),
2022 .list = channels_2_8,
2023 .mask = 0,
2024};
2025
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002026static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2027 struct hda_codec *codec,
2028 struct snd_pcm_substream *substream)
2029{
2030 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302031 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2032
2033 switch (codec->preset->id) {
2034 case 0x10de0002:
2035 case 0x10de0003:
2036 case 0x10de0005:
2037 case 0x10de0006:
2038 hw_constraints_channels = &hw_constraints_2_8_channels;
2039 break;
2040 case 0x10de0007:
2041 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2042 break;
2043 default:
2044 break;
2045 }
2046
2047 if (hw_constraints_channels != NULL) {
2048 snd_pcm_hw_constraint_list(substream->runtime, 0,
2049 SNDRV_PCM_HW_PARAM_CHANNELS,
2050 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002051 } else {
2052 snd_pcm_hw_constraint_step(substream->runtime, 0,
2053 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302054 }
2055
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002056 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2057}
2058
2059static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2060 struct hda_codec *codec,
2061 struct snd_pcm_substream *substream)
2062{
2063 struct hdmi_spec *spec = codec->spec;
2064 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2065}
2066
2067static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2068 struct hda_codec *codec,
2069 unsigned int stream_tag,
2070 unsigned int format,
2071 struct snd_pcm_substream *substream)
2072{
2073 struct hdmi_spec *spec = codec->spec;
2074 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2075 stream_tag, format, substream);
2076}
2077
Takashi Iwaid0b12522012-06-15 14:34:42 +02002078static const struct hda_pcm_stream simple_pcm_playback = {
2079 .substreams = 1,
2080 .channels_min = 2,
2081 .channels_max = 2,
2082 .ops = {
2083 .open = simple_playback_pcm_open,
2084 .close = simple_playback_pcm_close,
2085 .prepare = simple_playback_pcm_prepare
2086 },
2087};
2088
2089static const struct hda_codec_ops simple_hdmi_patch_ops = {
2090 .build_controls = simple_playback_build_controls,
2091 .build_pcms = simple_playback_build_pcms,
2092 .init = simple_playback_init,
2093 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002094 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002095};
2096
2097static int patch_simple_hdmi(struct hda_codec *codec,
2098 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2099{
2100 struct hdmi_spec *spec;
2101
2102 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2103 if (!spec)
2104 return -ENOMEM;
2105
2106 codec->spec = spec;
2107
2108 spec->multiout.num_dacs = 0; /* no analog */
2109 spec->multiout.max_channels = 2;
2110 spec->multiout.dig_out_nid = cvt_nid;
2111 spec->num_cvts = 1;
2112 spec->num_pins = 1;
2113 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02002114 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002115 spec->pcm_playback = simple_pcm_playback;
2116
2117 codec->patch_ops = simple_hdmi_patch_ops;
2118
2119 return 0;
2120}
2121
Aaron Plattner1f348522011-04-06 17:19:04 -07002122static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2123 int channels)
2124{
2125 unsigned int chanmask;
2126 int chan = channels ? (channels - 1) : 1;
2127
2128 switch (channels) {
2129 default:
2130 case 0:
2131 case 2:
2132 chanmask = 0x00;
2133 break;
2134 case 4:
2135 chanmask = 0x08;
2136 break;
2137 case 6:
2138 chanmask = 0x0b;
2139 break;
2140 case 8:
2141 chanmask = 0x13;
2142 break;
2143 }
2144
2145 /* Set the audio infoframe channel allocation and checksum fields. The
2146 * channel count is computed implicitly by the hardware. */
2147 snd_hda_codec_write(codec, 0x1, 0,
2148 Nv_VERB_SET_Channel_Allocation, chanmask);
2149
2150 snd_hda_codec_write(codec, 0x1, 0,
2151 Nv_VERB_SET_Info_Frame_Checksum,
2152 (0x71 - chan - chanmask));
2153}
2154
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002155static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2156 struct hda_codec *codec,
2157 struct snd_pcm_substream *substream)
2158{
2159 struct hdmi_spec *spec = codec->spec;
2160 int i;
2161
2162 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2163 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2164 for (i = 0; i < 4; i++) {
2165 /* set the stream id */
2166 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2167 AC_VERB_SET_CHANNEL_STREAMID, 0);
2168 /* set the stream format */
2169 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2170 AC_VERB_SET_STREAM_FORMAT, 0);
2171 }
2172
Aaron Plattner1f348522011-04-06 17:19:04 -07002173 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2174 * streams are disabled. */
2175 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2176
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002177 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2178}
2179
2180static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2181 struct hda_codec *codec,
2182 unsigned int stream_tag,
2183 unsigned int format,
2184 struct snd_pcm_substream *substream)
2185{
2186 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002187 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002188 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002189 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002190 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002191
2192 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002193 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002194
2195 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002196
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002197 dataDCC2 = 0x2;
2198
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002199 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002200 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002201 snd_hda_codec_write(codec,
2202 nvhdmi_master_con_nid_7x,
2203 0,
2204 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002205 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002206
2207 /* set the stream id */
2208 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2209 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2210
2211 /* set the stream format */
2212 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2213 AC_VERB_SET_STREAM_FORMAT, format);
2214
2215 /* turn on again (if needed) */
2216 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002217 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002218 snd_hda_codec_write(codec,
2219 nvhdmi_master_con_nid_7x,
2220 0,
2221 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002222 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002223 snd_hda_codec_write(codec,
2224 nvhdmi_master_con_nid_7x,
2225 0,
2226 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2227 }
2228
2229 for (i = 0; i < 4; i++) {
2230 if (chs == 2)
2231 channel_id = 0;
2232 else
2233 channel_id = i * 2;
2234
2235 /* turn off SPDIF once;
2236 *otherwise the IEC958 bits won't be updated
2237 */
2238 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002239 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002240 snd_hda_codec_write(codec,
2241 nvhdmi_con_nids_7x[i],
2242 0,
2243 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002244 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002245 /* set the stream id */
2246 snd_hda_codec_write(codec,
2247 nvhdmi_con_nids_7x[i],
2248 0,
2249 AC_VERB_SET_CHANNEL_STREAMID,
2250 (stream_tag << 4) | channel_id);
2251 /* set the stream format */
2252 snd_hda_codec_write(codec,
2253 nvhdmi_con_nids_7x[i],
2254 0,
2255 AC_VERB_SET_STREAM_FORMAT,
2256 format);
2257 /* turn on again (if needed) */
2258 /* enable and set the channel status audio/data flag */
2259 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002260 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002261 snd_hda_codec_write(codec,
2262 nvhdmi_con_nids_7x[i],
2263 0,
2264 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002265 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002266 snd_hda_codec_write(codec,
2267 nvhdmi_con_nids_7x[i],
2268 0,
2269 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2270 }
2271 }
2272
Aaron Plattner1f348522011-04-06 17:19:04 -07002273 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002274
2275 mutex_unlock(&codec->spdif_mutex);
2276 return 0;
2277}
2278
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002279static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002280 .substreams = 1,
2281 .channels_min = 2,
2282 .channels_max = 8,
2283 .nid = nvhdmi_master_con_nid_7x,
2284 .rates = SUPPORTED_RATES,
2285 .maxbps = SUPPORTED_MAXBPS,
2286 .formats = SUPPORTED_FORMATS,
2287 .ops = {
2288 .open = simple_playback_pcm_open,
2289 .close = nvhdmi_8ch_7x_pcm_close,
2290 .prepare = nvhdmi_8ch_7x_pcm_prepare
2291 },
2292};
2293
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002294static int patch_nvhdmi_2ch(struct hda_codec *codec)
2295{
2296 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002297 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2298 nvhdmi_master_pin_nid_7x);
2299 if (err < 0)
2300 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002301
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002302 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002303 /* override the PCM rates, etc, as the codec doesn't give full list */
2304 spec = codec->spec;
2305 spec->pcm_playback.rates = SUPPORTED_RATES;
2306 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2307 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002308 return 0;
2309}
2310
Takashi Iwai53775b02012-08-01 12:17:41 +02002311static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2312{
2313 struct hdmi_spec *spec = codec->spec;
2314 int err = simple_playback_build_pcms(codec);
2315 spec->pcm_rec[0].own_chmap = true;
2316 return err;
2317}
2318
2319static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2320{
2321 struct hdmi_spec *spec = codec->spec;
2322 struct snd_pcm_chmap *chmap;
2323 int err;
2324
2325 err = simple_playback_build_controls(codec);
2326 if (err < 0)
2327 return err;
2328
2329 /* add channel maps */
2330 err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
2331 SNDRV_PCM_STREAM_PLAYBACK,
2332 snd_pcm_alt_chmaps, 8, 0, &chmap);
2333 if (err < 0)
2334 return err;
2335 switch (codec->preset->id) {
2336 case 0x10de0002:
2337 case 0x10de0003:
2338 case 0x10de0005:
2339 case 0x10de0006:
2340 chmap->channel_mask = (1U << 2) | (1U << 8);
2341 break;
2342 case 0x10de0007:
2343 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2344 }
2345 return 0;
2346}
2347
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002348static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2349{
2350 struct hdmi_spec *spec;
2351 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002352 if (err < 0)
2353 return err;
2354 spec = codec->spec;
2355 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002356 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002357 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002358 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2359 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002360
2361 /* Initialize the audio infoframe channel mask and checksum to something
2362 * valid */
2363 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2364
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002365 return 0;
2366}
2367
2368/*
2369 * ATI-specific implementations
2370 *
2371 * FIXME: we may omit the whole this and use the generic code once after
2372 * it's confirmed to work.
2373 */
2374
2375#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2376#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2377
2378static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2379 struct hda_codec *codec,
2380 unsigned int stream_tag,
2381 unsigned int format,
2382 struct snd_pcm_substream *substream)
2383{
2384 struct hdmi_spec *spec = codec->spec;
2385 int chans = substream->runtime->channels;
2386 int i, err;
2387
2388 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2389 substream);
2390 if (err < 0)
2391 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002392 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
2393 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002394 /* FIXME: XXX */
2395 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002396 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002397 AC_VERB_SET_HDMI_CHAN_SLOT,
2398 (i << 4) | i);
2399 }
2400 return 0;
2401}
2402
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002403static int patch_atihdmi(struct hda_codec *codec)
2404{
2405 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002406 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2407 if (err < 0)
2408 return err;
2409 spec = codec->spec;
2410 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002411 return 0;
2412}
2413
Annie Liu3de5ff82012-06-08 19:18:42 +08002414/* VIA HDMI Implementation */
2415#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2416#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2417
Annie Liu3de5ff82012-06-08 19:18:42 +08002418static int patch_via_hdmi(struct hda_codec *codec)
2419{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002420 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002421}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002422
2423/*
2424 * patch entries
2425 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002426static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002427{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2428{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2429{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002430{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002431{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2432{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2433{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2434{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2435{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2436{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2437{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2438{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002439{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2440{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2441{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2442{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2443{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2444{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2445{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2446{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2447{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2448{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2449{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002450/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002451{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2452{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2453{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2454{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2455{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2456{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2457{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2458{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2459{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2460{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002461{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002462{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2463{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002464{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2465{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2466{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2467{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002468{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2469{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2470{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2471{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2472{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2473{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002474{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002475{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002476{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002477{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2478{} /* terminator */
2479};
2480
2481MODULE_ALIAS("snd-hda-codec-id:1002793c");
2482MODULE_ALIAS("snd-hda-codec-id:10027919");
2483MODULE_ALIAS("snd-hda-codec-id:1002791a");
2484MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2485MODULE_ALIAS("snd-hda-codec-id:10951390");
2486MODULE_ALIAS("snd-hda-codec-id:10951392");
2487MODULE_ALIAS("snd-hda-codec-id:10de0002");
2488MODULE_ALIAS("snd-hda-codec-id:10de0003");
2489MODULE_ALIAS("snd-hda-codec-id:10de0005");
2490MODULE_ALIAS("snd-hda-codec-id:10de0006");
2491MODULE_ALIAS("snd-hda-codec-id:10de0007");
2492MODULE_ALIAS("snd-hda-codec-id:10de000a");
2493MODULE_ALIAS("snd-hda-codec-id:10de000b");
2494MODULE_ALIAS("snd-hda-codec-id:10de000c");
2495MODULE_ALIAS("snd-hda-codec-id:10de000d");
2496MODULE_ALIAS("snd-hda-codec-id:10de0010");
2497MODULE_ALIAS("snd-hda-codec-id:10de0011");
2498MODULE_ALIAS("snd-hda-codec-id:10de0012");
2499MODULE_ALIAS("snd-hda-codec-id:10de0013");
2500MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002501MODULE_ALIAS("snd-hda-codec-id:10de0015");
2502MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002503MODULE_ALIAS("snd-hda-codec-id:10de0018");
2504MODULE_ALIAS("snd-hda-codec-id:10de0019");
2505MODULE_ALIAS("snd-hda-codec-id:10de001a");
2506MODULE_ALIAS("snd-hda-codec-id:10de001b");
2507MODULE_ALIAS("snd-hda-codec-id:10de001c");
2508MODULE_ALIAS("snd-hda-codec-id:10de0040");
2509MODULE_ALIAS("snd-hda-codec-id:10de0041");
2510MODULE_ALIAS("snd-hda-codec-id:10de0042");
2511MODULE_ALIAS("snd-hda-codec-id:10de0043");
2512MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002513MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002514MODULE_ALIAS("snd-hda-codec-id:10de0067");
2515MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002516MODULE_ALIAS("snd-hda-codec-id:11069f80");
2517MODULE_ALIAS("snd-hda-codec-id:11069f81");
2518MODULE_ALIAS("snd-hda-codec-id:11069f84");
2519MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002520MODULE_ALIAS("snd-hda-codec-id:17e80047");
2521MODULE_ALIAS("snd-hda-codec-id:80860054");
2522MODULE_ALIAS("snd-hda-codec-id:80862801");
2523MODULE_ALIAS("snd-hda-codec-id:80862802");
2524MODULE_ALIAS("snd-hda-codec-id:80862803");
2525MODULE_ALIAS("snd-hda-codec-id:80862804");
2526MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002527MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002528MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002529MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002530MODULE_ALIAS("snd-hda-codec-id:808629fb");
2531
2532MODULE_LICENSE("GPL");
2533MODULE_DESCRIPTION("HDMI HD-audio codec");
2534MODULE_ALIAS("snd-hda-codec-intelhdmi");
2535MODULE_ALIAS("snd-hda-codec-nvhdmi");
2536MODULE_ALIAS("snd-hda-codec-atihdmi");
2537
2538static struct hda_codec_preset_list intel_list = {
2539 .preset = snd_hda_preset_hdmi,
2540 .owner = THIS_MODULE,
2541};
2542
2543static int __init patch_hdmi_init(void)
2544{
2545 return snd_hda_add_codec_preset(&intel_list);
2546}
2547
2548static void __exit patch_hdmi_exit(void)
2549{
2550 snd_hda_delete_codec_preset(&intel_list);
2551}
2552
2553module_init(patch_hdmi_init)
2554module_exit(patch_hdmi_exit)