blob: 009bcccbee7a418f0d00e096a7b3f331337b808e [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
Mengdong Linfb87fa32013-09-04 16:36:57 -040047#define is_haswell(codec) ((codec)->vendor_id == 0x80862807)
48
Stephen Warren384a48d2011-06-01 11:14:21 -060049struct hdmi_spec_per_cvt {
50 hda_nid_t cvt_nid;
51 int assigned;
52 unsigned int channels_min;
53 unsigned int channels_max;
54 u32 rates;
55 u64 formats;
56 unsigned int maxbps;
57};
58
Takashi Iwai4eea3092013-02-07 18:18:19 +010059/* max. connections to a widget */
60#define HDA_MAX_CONNECTIONS 32
61
Stephen Warren384a48d2011-06-01 11:14:21 -060062struct hdmi_spec_per_pin {
63 hda_nid_t pin_nid;
64 int num_mux_nids;
65 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080066
67 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060068 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080069 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010070 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060071 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020072 bool setup; /* the stream has been set up by prepare callback */
73 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020074 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020075 bool chmap_set; /* channel-map override by ALSA API? */
76 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010077 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060078};
79
Wu Fengguang079d88c2010-03-08 10:44:23 +080080struct hdmi_spec {
81 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010082 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
83 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060084
Wu Fengguang079d88c2010-03-08 10:44:23 +080085 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010086 struct snd_array pins; /* struct hdmi_spec_per_pin */
87 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020088 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080089
David Henningsson4bd038f2013-02-19 16:11:25 +010090 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080091 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060092 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080093 */
94 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020095 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080096};
97
98
99struct hdmi_audio_infoframe {
100 u8 type; /* 0x84 */
101 u8 ver; /* 0x01 */
102 u8 len; /* 0x0a */
103
Wu Fengguang53d7d692010-09-21 14:25:49 +0800104 u8 checksum;
105
Wu Fengguang079d88c2010-03-08 10:44:23 +0800106 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
107 u8 SS01_SF24;
108 u8 CXT04;
109 u8 CA;
110 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800111};
112
113struct dp_audio_infoframe {
114 u8 type; /* 0x84 */
115 u8 len; /* 0x1b */
116 u8 ver; /* 0x11 << 2 */
117
118 u8 CC02_CT47; /* match with HDMI infoframe from this on */
119 u8 SS01_SF24;
120 u8 CXT04;
121 u8 CA;
122 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800123};
124
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100125union audio_infoframe {
126 struct hdmi_audio_infoframe hdmi;
127 struct dp_audio_infoframe dp;
128 u8 bytes[0];
129};
130
Wu Fengguang079d88c2010-03-08 10:44:23 +0800131/*
132 * CEA speaker placement:
133 *
134 * FLH FCH FRH
135 * FLW FL FLC FC FRC FR FRW
136 *
137 * LFE
138 * TC
139 *
140 * RL RLC RC RRC RR
141 *
142 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
143 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
144 */
145enum cea_speaker_placement {
146 FL = (1 << 0), /* Front Left */
147 FC = (1 << 1), /* Front Center */
148 FR = (1 << 2), /* Front Right */
149 FLC = (1 << 3), /* Front Left Center */
150 FRC = (1 << 4), /* Front Right Center */
151 RL = (1 << 5), /* Rear Left */
152 RC = (1 << 6), /* Rear Center */
153 RR = (1 << 7), /* Rear Right */
154 RLC = (1 << 8), /* Rear Left Center */
155 RRC = (1 << 9), /* Rear Right Center */
156 LFE = (1 << 10), /* Low Frequency Effect */
157 FLW = (1 << 11), /* Front Left Wide */
158 FRW = (1 << 12), /* Front Right Wide */
159 FLH = (1 << 13), /* Front Left High */
160 FCH = (1 << 14), /* Front Center High */
161 FRH = (1 << 15), /* Front Right High */
162 TC = (1 << 16), /* Top Center */
163};
164
165/*
166 * ELD SA bits in the CEA Speaker Allocation data block
167 */
168static int eld_speaker_allocation_bits[] = {
169 [0] = FL | FR,
170 [1] = LFE,
171 [2] = FC,
172 [3] = RL | RR,
173 [4] = RC,
174 [5] = FLC | FRC,
175 [6] = RLC | RRC,
176 /* the following are not defined in ELD yet */
177 [7] = FLW | FRW,
178 [8] = FLH | FRH,
179 [9] = TC,
180 [10] = FCH,
181};
182
183struct cea_channel_speaker_allocation {
184 int ca_index;
185 int speakers[8];
186
187 /* derived values, just for convenience */
188 int channels;
189 int spk_mask;
190};
191
192/*
193 * ALSA sequence is:
194 *
195 * surround40 surround41 surround50 surround51 surround71
196 * ch0 front left = = = =
197 * ch1 front right = = = =
198 * ch2 rear left = = = =
199 * ch3 rear right = = = =
200 * ch4 LFE center center center
201 * ch5 LFE LFE
202 * ch6 side left
203 * ch7 side right
204 *
205 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
206 */
207static int hdmi_channel_mapping[0x32][8] = {
208 /* stereo */
209 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
210 /* 2.1 */
211 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
212 /* Dolby Surround */
213 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
214 /* surround40 */
215 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
216 /* 4ch */
217 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
218 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800219 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800220 /* surround50 */
221 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
222 /* surround51 */
223 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
224 /* 7.1 */
225 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
226};
227
228/*
229 * This is an ordered list!
230 *
231 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800232 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800233 */
234static struct cea_channel_speaker_allocation channel_allocations[] = {
235/* channel: 7 6 5 4 3 2 1 0 */
236{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
237 /* 2.1 */
238{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
239 /* Dolby Surround */
240{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
241 /* surround40 */
242{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
243 /* surround41 */
244{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
245 /* surround50 */
246{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
247 /* surround51 */
248{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
249 /* 6.1 */
250{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
251 /* surround71 */
252{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
253
254{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
255{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
256{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
257{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
258{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
259{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
260{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
261{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
262{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
263{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
264{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
265{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
266{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
267{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
268{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
269{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
270{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
271{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
272{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
273{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
274{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
275{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
276{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
277{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
278{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
279{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
280{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
281{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
282{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
283{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
284{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
285{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
286{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
287{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
291{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
292{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
293{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
294{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
295};
296
297
298/*
299 * HDMI routines
300 */
301
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100302#define get_pin(spec, idx) \
303 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
304#define get_cvt(spec, idx) \
305 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
306#define get_pcm_rec(spec, idx) \
307 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800310{
Stephen Warren384a48d2011-06-01 11:14:21 -0600311 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800312
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100314 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600315 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800316
Stephen Warren384a48d2011-06-01 11:14:21 -0600317 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
318 return -EINVAL;
319}
320
321static int hinfo_to_pin_index(struct hdmi_spec *spec,
322 struct hda_pcm_stream *hinfo)
323{
324 int pin_idx;
325
326 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100327 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600328 return pin_idx;
329
330 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
331 return -EINVAL;
332}
333
334static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
335{
336 int cvt_idx;
337
338 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100339 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600340 return cvt_idx;
341
342 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800343 return -EINVAL;
344}
345
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500346static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
347 struct snd_ctl_elem_info *uinfo)
348{
349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100350 struct hdmi_spec *spec = codec->spec;
351 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500352 int pin_idx;
353
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500354 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
355
356 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100357 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100358
David Henningsson4bd038f2013-02-19 16:11:25 +0100359 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100360 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100361 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500362
363 return 0;
364}
365
366static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
367 struct snd_ctl_elem_value *ucontrol)
368{
369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100370 struct hdmi_spec *spec = codec->spec;
371 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500372 int pin_idx;
373
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500374 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100375 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500376
David Henningsson4bd038f2013-02-19 16:11:25 +0100377 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100378 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100379 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100380 snd_BUG();
381 return -EINVAL;
382 }
383
384 memset(ucontrol->value.bytes.data, 0,
385 ARRAY_SIZE(ucontrol->value.bytes.data));
386 if (eld->eld_valid)
387 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
388 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100389 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500390
391 return 0;
392}
393
394static struct snd_kcontrol_new eld_bytes_ctl = {
395 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
396 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
397 .name = "ELD",
398 .info = hdmi_eld_ctl_info,
399 .get = hdmi_eld_ctl_get,
400};
401
402static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
403 int device)
404{
405 struct snd_kcontrol *kctl;
406 struct hdmi_spec *spec = codec->spec;
407 int err;
408
409 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
410 if (!kctl)
411 return -ENOMEM;
412 kctl->private_value = pin_idx;
413 kctl->id.device = device;
414
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100415 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500416 if (err < 0)
417 return err;
418
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100419 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500420 return 0;
421}
422
Wu Fengguang079d88c2010-03-08 10:44:23 +0800423#ifdef BE_PARANOID
424static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
425 int *packet_index, int *byte_index)
426{
427 int val;
428
429 val = snd_hda_codec_read(codec, pin_nid, 0,
430 AC_VERB_GET_HDMI_DIP_INDEX, 0);
431
432 *packet_index = val >> 5;
433 *byte_index = val & 0x1f;
434}
435#endif
436
437static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
438 int packet_index, int byte_index)
439{
440 int val;
441
442 val = (packet_index << 5) | (byte_index & 0x1f);
443
444 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
445}
446
447static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
448 unsigned char val)
449{
450 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
451}
452
Stephen Warren384a48d2011-06-01 11:14:21 -0600453static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800454{
455 /* Unmute */
456 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
457 snd_hda_codec_write(codec, pin_nid, 0,
458 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100459 /* Enable pin out: some machines with GM965 gets broken output when
460 * the pin is disabled or changed while using with HDMI
461 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800462 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100463 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800464}
465
Stephen Warren384a48d2011-06-01 11:14:21 -0600466static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800467{
Stephen Warren384a48d2011-06-01 11:14:21 -0600468 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800469 AC_VERB_GET_CVT_CHAN_COUNT, 0);
470}
471
472static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600473 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800474{
Stephen Warren384a48d2011-06-01 11:14:21 -0600475 if (chs != hdmi_get_channel_count(codec, cvt_nid))
476 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800477 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
478}
479
480
481/*
482 * Channel mapping routines
483 */
484
485/*
486 * Compute derived values in channel_allocations[].
487 */
488static void init_channel_allocations(void)
489{
490 int i, j;
491 struct cea_channel_speaker_allocation *p;
492
493 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
494 p = channel_allocations + i;
495 p->channels = 0;
496 p->spk_mask = 0;
497 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
498 if (p->speakers[j]) {
499 p->channels++;
500 p->spk_mask |= p->speakers[j];
501 }
502 }
503}
504
Wang Xingchao72357c72012-09-06 10:02:36 +0800505static int get_channel_allocation_order(int ca)
506{
507 int i;
508
509 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
510 if (channel_allocations[i].ca_index == ca)
511 break;
512 }
513 return i;
514}
515
Wu Fengguang079d88c2010-03-08 10:44:23 +0800516/*
517 * The transformation takes two steps:
518 *
519 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
520 * spk_mask => (channel_allocations[]) => ai->CA
521 *
522 * TODO: it could select the wrong CA from multiple candidates.
523*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600524static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800525{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800526 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800527 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800528 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800529 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
530
531 /*
532 * CA defaults to 0 for basic stereo audio
533 */
534 if (channels <= 2)
535 return 0;
536
Wu Fengguang079d88c2010-03-08 10:44:23 +0800537 /*
538 * expand ELD's speaker allocation mask
539 *
540 * ELD tells the speaker mask in a compact(paired) form,
541 * expand ELD's notions to match the ones used by Audio InfoFrame.
542 */
543 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100544 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800545 spk_mask |= eld_speaker_allocation_bits[i];
546 }
547
548 /* search for the first working match in the CA table */
549 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
550 if (channels == channel_allocations[i].channels &&
551 (spk_mask & channel_allocations[i].spk_mask) ==
552 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800553 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800554 break;
555 }
556 }
557
Anssi Hannula18e39182013-09-01 14:36:47 +0300558 if (!ca) {
559 /* if there was no match, select the regular ALSA channel
560 * allocation with the matching number of channels */
561 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
562 if (channels == channel_allocations[i].channels) {
563 ca = channel_allocations[i].ca_index;
564 break;
565 }
566 }
567 }
568
David Henningsson1613d6b2013-02-19 16:11:24 +0100569 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800570 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800571 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800572
Wu Fengguang53d7d692010-09-21 14:25:49 +0800573 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800574}
575
576static void hdmi_debug_channel_mapping(struct hda_codec *codec,
577 hda_nid_t pin_nid)
578{
579#ifdef CONFIG_SND_DEBUG_VERBOSE
580 int i;
581 int slot;
582
583 for (i = 0; i < 8; i++) {
584 slot = snd_hda_codec_read(codec, pin_nid, 0,
585 AC_VERB_GET_HDMI_CHAN_SLOT, i);
586 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
587 slot >> 4, slot & 0xf);
588 }
589#endif
590}
591
592
Takashi Iwaid45e6882012-07-31 11:36:00 +0200593static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800594 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800595 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800596 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800597{
598 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800599 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800600 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800601 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800602
Wang Xingchao72357c72012-09-06 10:02:36 +0800603 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800604
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800606 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800607 hdmi_channel_mapping[ca][i] = i | (i << 4);
608 for (; i < 8; i++)
609 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
610 }
611
Wang Xingchao433968d2012-09-06 10:02:37 +0800612 if (non_pcm) {
613 for (i = 0; i < channel_allocations[order].channels; i++)
614 non_pcm_mapping[i] = i | (i << 4);
615 for (; i < 8; i++)
616 non_pcm_mapping[i] = 0xf | (i << 4);
617 }
618
Wu Fengguang079d88c2010-03-08 10:44:23 +0800619 for (i = 0; i < 8; i++) {
620 err = snd_hda_codec_write(codec, pin_nid, 0,
621 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800622 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800623 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800624 snd_printdd(KERN_NOTICE
625 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800626 break;
627 }
628 }
629
630 hdmi_debug_channel_mapping(codec, pin_nid);
631}
632
Takashi Iwaid45e6882012-07-31 11:36:00 +0200633struct channel_map_table {
634 unsigned char map; /* ALSA API channel map position */
635 unsigned char cea_slot; /* CEA slot value */
636 int spk_mask; /* speaker position bit mask */
637};
638
639static struct channel_map_table map_tables[] = {
640 { SNDRV_CHMAP_FL, 0x00, FL },
641 { SNDRV_CHMAP_FR, 0x01, FR },
642 { SNDRV_CHMAP_RL, 0x04, RL },
643 { SNDRV_CHMAP_RR, 0x05, RR },
644 { SNDRV_CHMAP_LFE, 0x02, LFE },
645 { SNDRV_CHMAP_FC, 0x03, FC },
646 { SNDRV_CHMAP_RLC, 0x06, RLC },
647 { SNDRV_CHMAP_RRC, 0x07, RRC },
648 {} /* terminator */
649};
650
651/* from ALSA API channel position to speaker bit mask */
652static int to_spk_mask(unsigned char c)
653{
654 struct channel_map_table *t = map_tables;
655 for (; t->map; t++) {
656 if (t->map == c)
657 return t->spk_mask;
658 }
659 return 0;
660}
661
662/* from ALSA API channel position to CEA slot */
663static int to_cea_slot(unsigned char c)
664{
665 struct channel_map_table *t = map_tables;
666 for (; t->map; t++) {
667 if (t->map == c)
668 return t->cea_slot;
669 }
670 return 0x0f;
671}
672
673/* from CEA slot to ALSA API channel position */
674static int from_cea_slot(unsigned char c)
675{
676 struct channel_map_table *t = map_tables;
677 for (; t->map; t++) {
678 if (t->cea_slot == c)
679 return t->map;
680 }
681 return 0;
682}
683
684/* from speaker bit mask to ALSA API channel position */
685static int spk_to_chmap(int spk)
686{
687 struct channel_map_table *t = map_tables;
688 for (; t->map; t++) {
689 if (t->spk_mask == spk)
690 return t->map;
691 }
692 return 0;
693}
694
695/* get the CA index corresponding to the given ALSA API channel map */
696static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
697{
698 int i, spks = 0, spk_mask = 0;
699
700 for (i = 0; i < chs; i++) {
701 int mask = to_spk_mask(map[i]);
702 if (mask) {
703 spk_mask |= mask;
704 spks++;
705 }
706 }
707
708 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
709 if ((chs == channel_allocations[i].channels ||
710 spks == channel_allocations[i].channels) &&
711 (spk_mask & channel_allocations[i].spk_mask) ==
712 channel_allocations[i].spk_mask)
713 return channel_allocations[i].ca_index;
714 }
715 return -1;
716}
717
718/* set up the channel slots for the given ALSA API channel map */
719static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
720 hda_nid_t pin_nid,
721 int chs, unsigned char *map)
722{
723 int i;
724 for (i = 0; i < 8; i++) {
725 int val, err;
726 if (i < chs)
727 val = to_cea_slot(map[i]);
728 else
729 val = 0xf;
730 val |= (i << 4);
731 err = snd_hda_codec_write(codec, pin_nid, 0,
732 AC_VERB_SET_HDMI_CHAN_SLOT, val);
733 if (err)
734 return -EINVAL;
735 }
736 return 0;
737}
738
739/* store ALSA API channel map from the current default map */
740static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
741{
742 int i;
743 for (i = 0; i < 8; i++) {
744 if (i < channel_allocations[ca].channels)
745 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
746 else
747 map[i] = 0;
748 }
749}
750
751static void hdmi_setup_channel_mapping(struct hda_codec *codec,
752 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200753 int channels, unsigned char *map,
754 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200755{
Anssi Hannula20608732013-02-03 17:55:45 +0200756 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200757 hdmi_manual_setup_channel_mapping(codec, pin_nid,
758 channels, map);
759 } else {
760 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
761 hdmi_setup_fake_chmap(map, ca);
762 }
763}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800764
765/*
766 * Audio InfoFrame routines
767 */
768
769/*
770 * Enable Audio InfoFrame Transmission
771 */
772static void hdmi_start_infoframe_trans(struct hda_codec *codec,
773 hda_nid_t pin_nid)
774{
775 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
776 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
777 AC_DIPXMIT_BEST);
778}
779
780/*
781 * Disable Audio InfoFrame Transmission
782 */
783static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
784 hda_nid_t pin_nid)
785{
786 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
787 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
788 AC_DIPXMIT_DISABLE);
789}
790
791static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
792{
793#ifdef CONFIG_SND_DEBUG_VERBOSE
794 int i;
795 int size;
796
797 size = snd_hdmi_get_eld_size(codec, pin_nid);
798 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
799
800 for (i = 0; i < 8; i++) {
801 size = snd_hda_codec_read(codec, pin_nid, 0,
802 AC_VERB_GET_HDMI_DIP_SIZE, i);
803 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
804 }
805#endif
806}
807
808static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
809{
810#ifdef BE_PARANOID
811 int i, j;
812 int size;
813 int pi, bi;
814 for (i = 0; i < 8; i++) {
815 size = snd_hda_codec_read(codec, pin_nid, 0,
816 AC_VERB_GET_HDMI_DIP_SIZE, i);
817 if (size == 0)
818 continue;
819
820 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
821 for (j = 1; j < 1000; j++) {
822 hdmi_write_dip_byte(codec, pin_nid, 0x0);
823 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
824 if (pi != i)
825 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
826 bi, pi, i);
827 if (bi == 0) /* byte index wrapped around */
828 break;
829 }
830 snd_printd(KERN_INFO
831 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
832 i, size, j);
833 }
834#endif
835}
836
Wu Fengguang53d7d692010-09-21 14:25:49 +0800837static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800838{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800839 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800840 u8 sum = 0;
841 int i;
842
Wu Fengguang53d7d692010-09-21 14:25:49 +0800843 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800844
Wu Fengguang53d7d692010-09-21 14:25:49 +0800845 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800846 sum += bytes[i];
847
Wu Fengguang53d7d692010-09-21 14:25:49 +0800848 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800849}
850
851static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
852 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800853 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800854{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800855 int i;
856
857 hdmi_debug_dip_size(codec, pin_nid);
858 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
859
Wu Fengguang079d88c2010-03-08 10:44:23 +0800860 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800861 for (i = 0; i < size; i++)
862 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800863}
864
865static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800866 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800867{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800868 u8 val;
869 int i;
870
871 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
872 != AC_DIPXMIT_BEST)
873 return false;
874
875 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800876 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800877 val = snd_hda_codec_read(codec, pin_nid, 0,
878 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800879 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800880 return false;
881 }
882
883 return true;
884}
885
Takashi Iwaib0540872013-09-02 12:33:02 +0200886static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
887 struct hdmi_spec_per_pin *per_pin,
888 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800889{
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200891 int channels = per_pin->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600892 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800893 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100894 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800895
Takashi Iwaib0540872013-09-02 12:33:02 +0200896 if (!channels)
897 return;
898
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100899 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600900 if (!eld->monitor_present)
901 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800902
Takashi Iwaid45e6882012-07-31 11:36:00 +0200903 if (!non_pcm && per_pin->chmap_set)
904 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
905 else
906 ca = hdmi_channel_allocation(eld, channels);
907 if (ca < 0)
908 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800909
Stephen Warren384a48d2011-06-01 11:14:21 -0600910 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100911 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600912 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800913
Stephen Warren384a48d2011-06-01 11:14:21 -0600914 hdmi_ai->type = 0x84;
915 hdmi_ai->ver = 0x01;
916 hdmi_ai->len = 0x0a;
917 hdmi_ai->CC02_CT47 = channels - 1;
918 hdmi_ai->CA = ca;
919 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100920 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600921 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800922
Stephen Warren384a48d2011-06-01 11:14:21 -0600923 dp_ai->type = 0x84;
924 dp_ai->len = 0x1b;
925 dp_ai->ver = 0x11 << 2;
926 dp_ai->CC02_CT47 = channels - 1;
927 dp_ai->CA = ca;
928 } else {
929 snd_printd("HDMI: unknown connection type at pin %d\n",
930 pin_nid);
931 return;
932 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800933
Stephen Warren384a48d2011-06-01 11:14:21 -0600934 /*
935 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
936 * sizeof(*dp_ai) to avoid partial match/update problems when
937 * the user switches between HDMI/DP monitors.
938 */
939 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
940 sizeof(ai))) {
941 snd_printdd("hdmi_setup_audio_infoframe: "
942 "pin=%d channels=%d\n",
943 pin_nid,
944 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200945 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200946 channels, per_pin->chmap,
947 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600948 hdmi_stop_infoframe_trans(codec, pin_nid);
949 hdmi_fill_audio_infoframe(codec, pin_nid,
950 ai.bytes, sizeof(ai));
951 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800952 } else {
953 /* For non-pcm audio switch, setup new channel mapping
954 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200955 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200956 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200957 channels, per_pin->chmap,
958 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800959 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800960
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200961 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800962}
963
964
965/*
966 * Unsolicited events
967 */
968
Wu Fengguangc6e84532011-11-18 16:59:32 -0600969static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200970
Wu Fengguang079d88c2010-03-08 10:44:23 +0800971static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
972{
973 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200974 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
975 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600976 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200977 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400978 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200979
980 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
981 if (!jack)
982 return;
983 pin_nid = jack->nid;
984 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800985
Fengguang Wufae3d882012-04-10 17:00:35 +0800986 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400987 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
988 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800989 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800990
Stephen Warren384a48d2011-06-01 11:14:21 -0600991 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
992 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800993 return;
994
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100995 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200996 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800997}
998
999static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1000{
1001 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1002 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1003 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1004 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1005
1006 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001007 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001008 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001009 tag,
1010 subtag,
1011 cp_state,
1012 cp_ready);
1013
1014 /* TODO */
1015 if (cp_state)
1016 ;
1017 if (cp_ready)
1018 ;
1019}
1020
1021
1022static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1023{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001024 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1025 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1026
Takashi Iwai3a938972011-10-28 01:16:55 +02001027 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001028 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1029 return;
1030 }
1031
1032 if (subtag == 0)
1033 hdmi_intrinsic_event(codec, res);
1034 else
1035 hdmi_non_intrinsic_event(codec, res);
1036}
1037
Wang Xingchao53b434f2013-06-18 10:41:53 +08001038static void haswell_verify_pin_D0(struct hda_codec *codec,
1039 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001040{
1041 int pwr, lamp, ramp;
1042
Wang Xingchao53b434f2013-06-18 10:41:53 +08001043 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1044 * thus pins could only choose converter 0 for use. Make sure the
1045 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001046 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001047 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1048
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001049 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001050 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1051 AC_PWRST_D0);
1052 msleep(40);
1053 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1054 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1055 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1056 }
1057
1058 lamp = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_AMP_GAIN_MUTE,
1060 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1061 ramp = snd_hda_codec_read(codec, nid, 0,
1062 AC_VERB_GET_AMP_GAIN_MUTE,
1063 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1064 if (lamp != ramp) {
1065 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1066 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1067
1068 lamp = snd_hda_codec_read(codec, nid, 0,
1069 AC_VERB_GET_AMP_GAIN_MUTE,
1070 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1071 ramp = snd_hda_codec_read(codec, nid, 0,
1072 AC_VERB_GET_AMP_GAIN_MUTE,
1073 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1074 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1075 }
1076}
1077
Wu Fengguang079d88c2010-03-08 10:44:23 +08001078/*
1079 * Callbacks
1080 */
1081
Takashi Iwai92f10b32010-08-03 14:21:00 +02001082/* HBR should be Non-PCM, 8 channels */
1083#define is_hbr_format(format) \
1084 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1085
Stephen Warren384a48d2011-06-01 11:14:21 -06001086static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1087 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001088{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001089 int pinctl;
1090 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001091
Mengdong Linfb87fa32013-09-04 16:36:57 -04001092 if (is_haswell(codec))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001093 haswell_verify_pin_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001094
Stephen Warren384a48d2011-06-01 11:14:21 -06001095 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1096 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001097 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1098
1099 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001100 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001101 new_pinctl |= AC_PINCTL_EPT_HBR;
1102 else
1103 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1104
1105 snd_printdd("hdmi_setup_stream: "
1106 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001107 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001108 pinctl == new_pinctl ? "" : "new-",
1109 new_pinctl);
1110
1111 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001112 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001113 AC_VERB_SET_PIN_WIDGET_CONTROL,
1114 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001115
Stephen Warren384a48d2011-06-01 11:14:21 -06001116 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001117 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001118 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1119 return -EINVAL;
1120 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001121
Stephen Warren384a48d2011-06-01 11:14:21 -06001122 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001123 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001124}
1125
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001126static int hdmi_choose_cvt(struct hda_codec *codec,
1127 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001128{
1129 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001130 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001131 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001132 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001133
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001134 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001135
Stephen Warren384a48d2011-06-01 11:14:21 -06001136 /* Dynamically assign converter to stream */
1137 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001138 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001139
1140 /* Must not already be assigned */
1141 if (per_cvt->assigned)
1142 continue;
1143 /* Must be in pin's mux's list of converters */
1144 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1145 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1146 break;
1147 /* Not in mux list */
1148 if (mux_idx == per_pin->num_mux_nids)
1149 continue;
1150 break;
1151 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001152
Stephen Warren384a48d2011-06-01 11:14:21 -06001153 /* No free converters */
1154 if (cvt_idx == spec->num_cvts)
1155 return -ENODEV;
1156
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001157 if (cvt_id)
1158 *cvt_id = cvt_idx;
1159 if (mux_id)
1160 *mux_id = mux_idx;
1161
1162 return 0;
1163}
1164
1165static void haswell_config_cvts(struct hda_codec *codec,
1166 int pin_id, int mux_id)
1167{
1168 struct hdmi_spec *spec = codec->spec;
1169 struct hdmi_spec_per_pin *per_pin;
1170 int pin_idx, mux_idx;
1171 int curr;
1172 int err;
1173
1174 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1175 per_pin = get_pin(spec, pin_idx);
1176
1177 if (pin_idx == pin_id)
1178 continue;
1179
1180 curr = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1181 AC_VERB_GET_CONNECT_SEL, 0);
1182
1183 /* Choose another unused converter */
1184 if (curr == mux_id) {
1185 err = hdmi_choose_cvt(codec, pin_idx, NULL, &mux_idx);
1186 if (err < 0)
1187 return;
1188 snd_printdd("HDMI: choose converter %d for pin %d\n", mux_idx, pin_idx);
1189 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1190 AC_VERB_SET_CONNECT_SEL,
1191 mux_idx);
1192 }
1193 }
1194}
1195
1196/*
1197 * HDA PCM callbacks
1198 */
1199static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1200 struct hda_codec *codec,
1201 struct snd_pcm_substream *substream)
1202{
1203 struct hdmi_spec *spec = codec->spec;
1204 struct snd_pcm_runtime *runtime = substream->runtime;
1205 int pin_idx, cvt_idx, mux_idx = 0;
1206 struct hdmi_spec_per_pin *per_pin;
1207 struct hdmi_eld *eld;
1208 struct hdmi_spec_per_cvt *per_cvt = NULL;
1209 int err;
1210
1211 /* Validate hinfo */
1212 pin_idx = hinfo_to_pin_index(spec, hinfo);
1213 if (snd_BUG_ON(pin_idx < 0))
1214 return -EINVAL;
1215 per_pin = get_pin(spec, pin_idx);
1216 eld = &per_pin->sink_eld;
1217
1218 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1219 if (err < 0)
1220 return err;
1221
1222 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001223 /* Claim converter */
1224 per_cvt->assigned = 1;
1225 hinfo->nid = per_cvt->cvt_nid;
1226
Takashi Iwaibddee962013-06-18 16:14:22 +02001227 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001228 AC_VERB_SET_CONNECT_SEL,
1229 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001230
1231 /* configure unused pins to choose other converters */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001232 if (is_haswell(codec))
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001233 haswell_config_cvts(codec, pin_idx, mux_idx);
1234
Stephen Warren384a48d2011-06-01 11:14:21 -06001235 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001236
Stephen Warren2def8172011-06-01 11:14:20 -06001237 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001238 hinfo->channels_min = per_cvt->channels_min;
1239 hinfo->channels_max = per_cvt->channels_max;
1240 hinfo->rates = per_cvt->rates;
1241 hinfo->formats = per_cvt->formats;
1242 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001243
Stephen Warren384a48d2011-06-01 11:14:21 -06001244 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001245 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001246 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001247 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001248 !hinfo->rates || !hinfo->formats) {
1249 per_cvt->assigned = 0;
1250 hinfo->nid = 0;
1251 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001252 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001253 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001254 }
Stephen Warren2def8172011-06-01 11:14:20 -06001255
1256 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001257 runtime->hw.channels_min = hinfo->channels_min;
1258 runtime->hw.channels_max = hinfo->channels_max;
1259 runtime->hw.formats = hinfo->formats;
1260 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001261
1262 snd_pcm_hw_constraint_step(substream->runtime, 0,
1263 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001264 return 0;
1265}
1266
1267/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001268 * HDA/HDMI auto parsing
1269 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001270static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001271{
1272 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001273 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001274 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001275
1276 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1277 snd_printk(KERN_WARNING
1278 "HDMI: pin %d wcaps %#x "
1279 "does not support connection list\n",
1280 pin_nid, get_wcaps(codec, pin_nid));
1281 return -EINVAL;
1282 }
1283
Stephen Warren384a48d2011-06-01 11:14:21 -06001284 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1285 per_pin->mux_nids,
1286 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001287
1288 return 0;
1289}
1290
Wu Fengguangc6e84532011-11-18 16:59:32 -06001291static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001292{
Wu Fengguang744626d2011-11-16 16:29:47 +08001293 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001294 struct hdmi_spec *spec = codec->spec;
1295 struct hdmi_eld *eld = &spec->temp_eld;
1296 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001297 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001298 /*
1299 * Always execute a GetPinSense verb here, even when called from
1300 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1301 * response's PD bit is not the real PD value, but indicates that
1302 * the real PD value changed. An older version of the HD-audio
1303 * specification worked this way. Hence, we just ignore the data in
1304 * the unsolicited response to avoid custom WARs.
1305 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001306 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001307 bool update_eld = false;
1308 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001309
David Henningsson4bd038f2013-02-19 16:11:25 +01001310 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1311 if (pin_eld->monitor_present)
1312 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1313 else
1314 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001315
Fengguang Wufae3d882012-04-10 17:00:35 +08001316 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001317 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001318 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001319
David Henningsson4bd038f2013-02-19 16:11:25 +01001320 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001321 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1322 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001323 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001324 else {
1325 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1326 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1327 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001328 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001329 }
1330
David Henningsson4bd038f2013-02-19 16:11:25 +01001331 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001332 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001333 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001334 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001335 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001336 queue_delayed_work(codec->bus->workq,
1337 &per_pin->work,
1338 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001339 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001340 }
1341 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001342
1343 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001344 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001345 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001346 eld_changed = true;
1347 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001348 if (update_eld) {
Takashi Iwaib0540872013-09-02 12:33:02 +02001349 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001350 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001351 eld_changed = pin_eld->eld_size != eld->eld_size ||
1352 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001353 eld->eld_size) != 0;
1354 if (eld_changed)
1355 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1356 eld->eld_size);
1357 pin_eld->eld_size = eld->eld_size;
1358 pin_eld->info = eld->info;
Takashi Iwaib0540872013-09-02 12:33:02 +02001359
1360 /* Haswell-specific workaround: re-setup when the transcoder is
1361 * changed during the stream playback
1362 */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001363 if (is_haswell(codec) &&
Takashi Iwaib0540872013-09-02 12:33:02 +02001364 eld->eld_valid && !old_eld_valid && per_pin->setup) {
1365 snd_hda_codec_write(codec, pin_nid, 0,
1366 AC_VERB_SET_AMP_GAIN_MUTE,
1367 AMP_OUT_UNMUTE);
1368 hdmi_setup_audio_infoframe(codec, per_pin,
1369 per_pin->non_pcm);
1370 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001371 }
1372 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001373
1374 if (eld_changed)
1375 snd_ctl_notify(codec->bus->card,
1376 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1377 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001378}
1379
Wu Fengguang744626d2011-11-16 16:29:47 +08001380static void hdmi_repoll_eld(struct work_struct *work)
1381{
1382 struct hdmi_spec_per_pin *per_pin =
1383 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1384
Wu Fengguangc6e84532011-11-18 16:59:32 -06001385 if (per_pin->repoll_count++ > 6)
1386 per_pin->repoll_count = 0;
1387
1388 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001389}
1390
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001391static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1392 hda_nid_t nid);
1393
Wu Fengguang079d88c2010-03-08 10:44:23 +08001394static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1395{
1396 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001397 unsigned int caps, config;
1398 int pin_idx;
1399 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001400 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001401
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001402 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001403 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1404 return 0;
1405
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001406 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001407 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1408 return 0;
1409
Mengdong Linfb87fa32013-09-04 16:36:57 -04001410 if (is_haswell(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001411 intel_haswell_fixup_connect_list(codec, pin_nid);
1412
Stephen Warren384a48d2011-06-01 11:14:21 -06001413 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001414 per_pin = snd_array_new(&spec->pins);
1415 if (!per_pin)
1416 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001417
1418 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001419 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001420
Stephen Warren384a48d2011-06-01 11:14:21 -06001421 err = hdmi_read_pin_conn(codec, pin_idx);
1422 if (err < 0)
1423 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001424
Wu Fengguang079d88c2010-03-08 10:44:23 +08001425 spec->num_pins++;
1426
Stephen Warren384a48d2011-06-01 11:14:21 -06001427 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001428}
1429
Stephen Warren384a48d2011-06-01 11:14:21 -06001430static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001431{
1432 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001433 struct hdmi_spec_per_cvt *per_cvt;
1434 unsigned int chans;
1435 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001436
Stephen Warren384a48d2011-06-01 11:14:21 -06001437 chans = get_wcaps(codec, cvt_nid);
1438 chans = get_wcaps_channels(chans);
1439
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001440 per_cvt = snd_array_new(&spec->cvts);
1441 if (!per_cvt)
1442 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001443
1444 per_cvt->cvt_nid = cvt_nid;
1445 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001446 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001447 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001448 if (chans > spec->channels_max)
1449 spec->channels_max = chans;
1450 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001451
1452 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1453 &per_cvt->rates,
1454 &per_cvt->formats,
1455 &per_cvt->maxbps);
1456 if (err < 0)
1457 return err;
1458
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001459 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1460 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1461 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001462
1463 return 0;
1464}
1465
1466static int hdmi_parse_codec(struct hda_codec *codec)
1467{
1468 hda_nid_t nid;
1469 int i, nodes;
1470
1471 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1472 if (!nid || nodes < 0) {
1473 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1474 return -EINVAL;
1475 }
1476
1477 for (i = 0; i < nodes; i++, nid++) {
1478 unsigned int caps;
1479 unsigned int type;
1480
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001481 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001482 type = get_wcaps_type(caps);
1483
1484 if (!(caps & AC_WCAP_DIGITAL))
1485 continue;
1486
1487 switch (type) {
1488 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001489 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001490 break;
1491 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001492 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001493 break;
1494 }
1495 }
1496
David Henningssonc9adeef2012-11-07 09:22:33 +01001497#ifdef CONFIG_PM
1498 /* We're seeing some problems with unsolicited hot plug events on
1499 * PantherPoint after S3, if this is not enabled */
1500 if (codec->vendor_id == 0x80862806)
1501 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001502 /*
1503 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1504 * can be lost and presence sense verb will become inaccurate if the
1505 * HDA link is powered off at hot plug or hw initialization time.
1506 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001507 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001508 AC_PWRST_EPSS))
1509 codec->bus->power_keep_link_on = 1;
1510#endif
1511
1512 return 0;
1513}
1514
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001515/*
1516 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001517static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1518{
1519 struct hda_spdif_out *spdif;
1520 bool non_pcm;
1521
1522 mutex_lock(&codec->spdif_mutex);
1523 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1524 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1525 mutex_unlock(&codec->spdif_mutex);
1526 return non_pcm;
1527}
1528
1529
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001530/*
1531 * HDMI callbacks
1532 */
1533
1534static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1535 struct hda_codec *codec,
1536 unsigned int stream_tag,
1537 unsigned int format,
1538 struct snd_pcm_substream *substream)
1539{
Stephen Warren384a48d2011-06-01 11:14:21 -06001540 hda_nid_t cvt_nid = hinfo->nid;
1541 struct hdmi_spec *spec = codec->spec;
1542 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaib0540872013-09-02 12:33:02 +02001543 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1544 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001545 bool non_pcm;
1546
1547 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaib0540872013-09-02 12:33:02 +02001548 per_pin->channels = substream->runtime->channels;
1549 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001550
Stephen Warren384a48d2011-06-01 11:14:21 -06001551 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001552
Takashi Iwaib0540872013-09-02 12:33:02 +02001553 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001554
1555 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001556}
1557
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001558static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1559 struct hda_codec *codec,
1560 struct snd_pcm_substream *substream)
1561{
1562 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1563 return 0;
1564}
1565
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001566static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1567 struct hda_codec *codec,
1568 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001569{
1570 struct hdmi_spec *spec = codec->spec;
1571 int cvt_idx, pin_idx;
1572 struct hdmi_spec_per_cvt *per_cvt;
1573 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001574
Stephen Warren384a48d2011-06-01 11:14:21 -06001575 if (hinfo->nid) {
1576 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1577 if (snd_BUG_ON(cvt_idx < 0))
1578 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001579 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001580
1581 snd_BUG_ON(!per_cvt->assigned);
1582 per_cvt->assigned = 0;
1583 hinfo->nid = 0;
1584
1585 pin_idx = hinfo_to_pin_index(spec, hinfo);
1586 if (snd_BUG_ON(pin_idx < 0))
1587 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001588 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001589
Stephen Warren384a48d2011-06-01 11:14:21 -06001590 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001591 per_pin->chmap_set = false;
1592 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001593
1594 per_pin->setup = false;
1595 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001596 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001597
Stephen Warren384a48d2011-06-01 11:14:21 -06001598 return 0;
1599}
1600
1601static const struct hda_pcm_ops generic_ops = {
1602 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001603 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001604 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001605 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001606};
1607
Takashi Iwaid45e6882012-07-31 11:36:00 +02001608/*
1609 * ALSA API channel-map control callbacks
1610 */
1611static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_info *uinfo)
1613{
1614 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1615 struct hda_codec *codec = info->private_data;
1616 struct hdmi_spec *spec = codec->spec;
1617 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1618 uinfo->count = spec->channels_max;
1619 uinfo->value.integer.min = 0;
1620 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1621 return 0;
1622}
1623
1624static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1625 unsigned int size, unsigned int __user *tlv)
1626{
1627 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1628 struct hda_codec *codec = info->private_data;
1629 struct hdmi_spec *spec = codec->spec;
1630 const unsigned int valid_mask =
1631 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1632 unsigned int __user *dst;
1633 int chs, count = 0;
1634
1635 if (size < 8)
1636 return -ENOMEM;
1637 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1638 return -EFAULT;
1639 size -= 8;
1640 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001641 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001642 int i, c;
1643 struct cea_channel_speaker_allocation *cap;
1644 cap = channel_allocations;
1645 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1646 int chs_bytes = chs * 4;
1647 if (cap->channels != chs)
1648 continue;
1649 if (cap->spk_mask & ~valid_mask)
1650 continue;
1651 if (size < 8)
1652 return -ENOMEM;
1653 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1654 put_user(chs_bytes, dst + 1))
1655 return -EFAULT;
1656 dst += 2;
1657 size -= 8;
1658 count += 8;
1659 if (size < chs_bytes)
1660 return -ENOMEM;
1661 size -= chs_bytes;
1662 count += chs_bytes;
1663 for (c = 7; c >= 0; c--) {
1664 int spk = cap->speakers[c];
1665 if (!spk)
1666 continue;
1667 if (put_user(spk_to_chmap(spk), dst))
1668 return -EFAULT;
1669 dst++;
1670 }
1671 }
1672 }
1673 if (put_user(count, tlv + 1))
1674 return -EFAULT;
1675 return 0;
1676}
1677
1678static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_value *ucontrol)
1680{
1681 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1682 struct hda_codec *codec = info->private_data;
1683 struct hdmi_spec *spec = codec->spec;
1684 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001685 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001686 int i;
1687
1688 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1689 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1690 return 0;
1691}
1692
1693static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1694 struct snd_ctl_elem_value *ucontrol)
1695{
1696 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1697 struct hda_codec *codec = info->private_data;
1698 struct hdmi_spec *spec = codec->spec;
1699 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001700 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001701 unsigned int ctl_idx;
1702 struct snd_pcm_substream *substream;
1703 unsigned char chmap[8];
1704 int i, ca, prepared = 0;
1705
1706 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1707 substream = snd_pcm_chmap_substream(info, ctl_idx);
1708 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001709 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001710 switch (substream->runtime->status->state) {
1711 case SNDRV_PCM_STATE_OPEN:
1712 case SNDRV_PCM_STATE_SETUP:
1713 break;
1714 case SNDRV_PCM_STATE_PREPARED:
1715 prepared = 1;
1716 break;
1717 default:
1718 return -EBUSY;
1719 }
1720 memset(chmap, 0, sizeof(chmap));
1721 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1722 chmap[i] = ucontrol->value.integer.value[i];
1723 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1724 return 0;
1725 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1726 if (ca < 0)
1727 return -EINVAL;
1728 per_pin->chmap_set = true;
1729 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1730 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02001731 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001732
1733 return 0;
1734}
1735
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001736static int generic_hdmi_build_pcms(struct hda_codec *codec)
1737{
1738 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001739 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001740
Stephen Warren384a48d2011-06-01 11:14:21 -06001741 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1742 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001743 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001744 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001745
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001746 per_pin = get_pin(spec, pin_idx);
1747 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1748 info = snd_array_new(&spec->pcm_rec);
1749 if (!info)
1750 return -ENOMEM;
1751 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001752 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001753 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001754
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001755 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001756 pstr->substreams = 1;
1757 pstr->ops = generic_ops;
1758 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001759 }
1760
Stephen Warren384a48d2011-06-01 11:14:21 -06001761 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001762 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001763
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001764 return 0;
1765}
1766
David Henningsson0b6c49b2011-08-23 16:56:03 +02001767static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1768{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001769 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001770 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001771 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1772 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001773
Takashi Iwai31ef2252011-12-01 17:41:36 +01001774 if (pcmdev > 0)
1775 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001776 if (!is_jack_detectable(codec, per_pin->pin_nid))
1777 strncat(hdmi_str, " Phantom",
1778 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001779
Takashi Iwai31ef2252011-12-01 17:41:36 +01001780 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001781}
1782
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001783static int generic_hdmi_build_controls(struct hda_codec *codec)
1784{
1785 struct hdmi_spec *spec = codec->spec;
1786 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001787 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001788
Stephen Warren384a48d2011-06-01 11:14:21 -06001789 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001790 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001791
1792 err = generic_hdmi_build_jack(codec, pin_idx);
1793 if (err < 0)
1794 return err;
1795
Takashi Iwaidcda5802012-10-12 17:24:51 +02001796 err = snd_hda_create_dig_out_ctls(codec,
1797 per_pin->pin_nid,
1798 per_pin->mux_nids[0],
1799 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001800 if (err < 0)
1801 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001802 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001803
1804 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001805 err = hdmi_create_eld_ctl(codec, pin_idx,
1806 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001807
1808 if (err < 0)
1809 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001810
Takashi Iwai82b1d732011-12-20 15:53:07 +01001811 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001812 }
1813
Takashi Iwaid45e6882012-07-31 11:36:00 +02001814 /* add channel maps */
1815 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1816 struct snd_pcm_chmap *chmap;
1817 struct snd_kcontrol *kctl;
1818 int i;
1819 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1820 SNDRV_PCM_STREAM_PLAYBACK,
1821 NULL, 0, pin_idx, &chmap);
1822 if (err < 0)
1823 return err;
1824 /* override handlers */
1825 chmap->private_data = codec;
1826 kctl = chmap->kctl;
1827 for (i = 0; i < kctl->count; i++)
1828 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1829 kctl->info = hdmi_chmap_ctl_info;
1830 kctl->get = hdmi_chmap_ctl_get;
1831 kctl->put = hdmi_chmap_ctl_put;
1832 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1833 }
1834
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001835 return 0;
1836}
1837
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001838static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1839{
1840 struct hdmi_spec *spec = codec->spec;
1841 int pin_idx;
1842
1843 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001844 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001845 struct hdmi_eld *eld = &per_pin->sink_eld;
1846
1847 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001848 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001849 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1850 snd_hda_eld_proc_new(codec, eld, pin_idx);
1851 }
1852 return 0;
1853}
1854
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001855static int generic_hdmi_init(struct hda_codec *codec)
1856{
1857 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001858 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001859
Stephen Warren384a48d2011-06-01 11:14:21 -06001860 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001861 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001862 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001863
1864 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001865 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001866 }
1867 return 0;
1868}
1869
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001870static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1871{
1872 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1873 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1874 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1875}
1876
1877static void hdmi_array_free(struct hdmi_spec *spec)
1878{
1879 snd_array_free(&spec->pins);
1880 snd_array_free(&spec->cvts);
1881 snd_array_free(&spec->pcm_rec);
1882}
1883
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001884static void generic_hdmi_free(struct hda_codec *codec)
1885{
1886 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001887 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001888
Stephen Warren384a48d2011-06-01 11:14:21 -06001889 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001890 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001891 struct hdmi_eld *eld = &per_pin->sink_eld;
1892
Wu Fengguang744626d2011-11-16 16:29:47 +08001893 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001894 snd_hda_eld_proc_free(codec, eld);
1895 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001896
Wu Fengguang744626d2011-11-16 16:29:47 +08001897 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001898 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001899 kfree(spec);
1900}
1901
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001902#ifdef CONFIG_PM
1903static int generic_hdmi_resume(struct hda_codec *codec)
1904{
1905 struct hdmi_spec *spec = codec->spec;
1906 int pin_idx;
1907
1908 generic_hdmi_init(codec);
1909 snd_hda_codec_resume_amp(codec);
1910 snd_hda_codec_resume_cache(codec);
1911
1912 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1913 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1914 hdmi_present_sense(per_pin, 1);
1915 }
1916 return 0;
1917}
1918#endif
1919
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001920static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001921 .init = generic_hdmi_init,
1922 .free = generic_hdmi_free,
1923 .build_pcms = generic_hdmi_build_pcms,
1924 .build_controls = generic_hdmi_build_controls,
1925 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001926#ifdef CONFIG_PM
1927 .resume = generic_hdmi_resume,
1928#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001929};
1930
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001931
1932static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1933 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001934{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001935 struct hdmi_spec *spec = codec->spec;
1936 hda_nid_t conns[4];
1937 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001938
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001939 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1940 if (nconns == spec->num_cvts &&
1941 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001942 return;
1943
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001944 /* override pins connection list */
1945 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1946 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001947}
1948
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001949#define INTEL_VENDOR_NID 0x08
1950#define INTEL_GET_VENDOR_VERB 0xf81
1951#define INTEL_SET_VENDOR_VERB 0x781
1952#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1953#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1954
1955static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001956 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001957{
1958 unsigned int vendor_param;
1959
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001960 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1961 INTEL_GET_VENDOR_VERB, 0);
1962 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1963 return;
1964
1965 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1966 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1967 INTEL_SET_VENDOR_VERB, vendor_param);
1968 if (vendor_param == -1)
1969 return;
1970
Takashi Iwai17df3f52013-05-08 08:09:34 +02001971 if (update_tree)
1972 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001973}
1974
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001975static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1976{
1977 unsigned int vendor_param;
1978
1979 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1980 INTEL_GET_VENDOR_VERB, 0);
1981 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1982 return;
1983
1984 /* enable DP1.2 mode */
1985 vendor_param |= INTEL_EN_DP12;
1986 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1987 INTEL_SET_VENDOR_VERB, vendor_param);
1988}
1989
Takashi Iwai17df3f52013-05-08 08:09:34 +02001990/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1991 * Otherwise you may get severe h/w communication errors.
1992 */
1993static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1994 unsigned int power_state)
1995{
1996 if (power_state == AC_PWRST_D0) {
1997 intel_haswell_enable_all_pins(codec, false);
1998 intel_haswell_fixup_enable_dp12(codec);
1999 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002000
Takashi Iwai17df3f52013-05-08 08:09:34 +02002001 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2002 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2003}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002004
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002005static int patch_generic_hdmi(struct hda_codec *codec)
2006{
2007 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002008
2009 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2010 if (spec == NULL)
2011 return -ENOMEM;
2012
2013 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002014 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002015
Mengdong Linfb87fa32013-09-04 16:36:57 -04002016 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002017 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002018 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002019 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002020
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002021 if (hdmi_parse_codec(codec) < 0) {
2022 codec->spec = NULL;
2023 kfree(spec);
2024 return -EINVAL;
2025 }
2026 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Linfb87fa32013-09-04 16:36:57 -04002027 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002028 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002029 codec->dp_mst = true;
2030 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002031
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002032 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002033
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002034 init_channel_allocations();
2035
2036 return 0;
2037}
2038
2039/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002040 * Shared non-generic implementations
2041 */
2042
2043static int simple_playback_build_pcms(struct hda_codec *codec)
2044{
2045 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002046 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002047 unsigned int chans;
2048 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002049 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002050
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002051 per_cvt = get_cvt(spec, 0);
2052 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002053 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002054
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002055 info = snd_array_new(&spec->pcm_rec);
2056 if (!info)
2057 return -ENOMEM;
2058 info->name = get_pin(spec, 0)->pcm_name;
2059 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002060 info->pcm_type = HDA_PCM_TYPE_HDMI;
2061 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2062 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002063 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002064 if (pstr->channels_max <= 2 && chans && chans <= 16)
2065 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002066
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002067 codec->num_pcms = 1;
2068 codec->pcm_info = info;
2069
Stephen Warren3aaf8982011-06-01 11:14:19 -06002070 return 0;
2071}
2072
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002073/* unsolicited event for jack sensing */
2074static void simple_hdmi_unsol_event(struct hda_codec *codec,
2075 unsigned int res)
2076{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002077 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002078 snd_hda_jack_report_sync(codec);
2079}
2080
2081/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2082 * as long as spec->pins[] is set correctly
2083 */
2084#define simple_hdmi_build_jack generic_hdmi_build_jack
2085
Stephen Warren3aaf8982011-06-01 11:14:19 -06002086static int simple_playback_build_controls(struct hda_codec *codec)
2087{
2088 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002089 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002090 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002091
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002092 per_cvt = get_cvt(spec, 0);
2093 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2094 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002095 if (err < 0)
2096 return err;
2097 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002098}
2099
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002100static int simple_playback_init(struct hda_codec *codec)
2101{
2102 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002103 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2104 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002105
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002106 snd_hda_codec_write(codec, pin, 0,
2107 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2108 /* some codecs require to unmute the pin */
2109 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2110 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2111 AMP_OUT_UNMUTE);
2112 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002113 return 0;
2114}
2115
Stephen Warren3aaf8982011-06-01 11:14:19 -06002116static void simple_playback_free(struct hda_codec *codec)
2117{
2118 struct hdmi_spec *spec = codec->spec;
2119
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002120 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002121 kfree(spec);
2122}
2123
2124/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002125 * Nvidia specific implementations
2126 */
2127
2128#define Nv_VERB_SET_Channel_Allocation 0xF79
2129#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2130#define Nv_VERB_SET_Audio_Protection_On 0xF98
2131#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2132
2133#define nvhdmi_master_con_nid_7x 0x04
2134#define nvhdmi_master_pin_nid_7x 0x05
2135
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002136static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002137 /*front, rear, clfe, rear_surr */
2138 0x6, 0x8, 0xa, 0xc,
2139};
2140
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002141static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2142 /* set audio protect on */
2143 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2144 /* enable digital output on pin widget */
2145 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2146 {} /* terminator */
2147};
2148
2149static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002150 /* set audio protect on */
2151 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2152 /* enable digital output on pin widget */
2153 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2154 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2155 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2156 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2157 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2158 {} /* terminator */
2159};
2160
2161#ifdef LIMITED_RATE_FMT_SUPPORT
2162/* support only the safe format and rate */
2163#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2164#define SUPPORTED_MAXBPS 16
2165#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2166#else
2167/* support all rates and formats */
2168#define SUPPORTED_RATES \
2169 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2170 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2171 SNDRV_PCM_RATE_192000)
2172#define SUPPORTED_MAXBPS 24
2173#define SUPPORTED_FORMATS \
2174 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2175#endif
2176
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002177static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002178{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002179 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2180 return 0;
2181}
2182
2183static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2184{
2185 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002186 return 0;
2187}
2188
Nitin Daga393004b2011-01-10 21:49:31 +05302189static unsigned int channels_2_6_8[] = {
2190 2, 6, 8
2191};
2192
2193static unsigned int channels_2_8[] = {
2194 2, 8
2195};
2196
2197static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2198 .count = ARRAY_SIZE(channels_2_6_8),
2199 .list = channels_2_6_8,
2200 .mask = 0,
2201};
2202
2203static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2204 .count = ARRAY_SIZE(channels_2_8),
2205 .list = channels_2_8,
2206 .mask = 0,
2207};
2208
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002209static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2210 struct hda_codec *codec,
2211 struct snd_pcm_substream *substream)
2212{
2213 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302214 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2215
2216 switch (codec->preset->id) {
2217 case 0x10de0002:
2218 case 0x10de0003:
2219 case 0x10de0005:
2220 case 0x10de0006:
2221 hw_constraints_channels = &hw_constraints_2_8_channels;
2222 break;
2223 case 0x10de0007:
2224 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2225 break;
2226 default:
2227 break;
2228 }
2229
2230 if (hw_constraints_channels != NULL) {
2231 snd_pcm_hw_constraint_list(substream->runtime, 0,
2232 SNDRV_PCM_HW_PARAM_CHANNELS,
2233 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002234 } else {
2235 snd_pcm_hw_constraint_step(substream->runtime, 0,
2236 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302237 }
2238
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002239 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2240}
2241
2242static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2243 struct hda_codec *codec,
2244 struct snd_pcm_substream *substream)
2245{
2246 struct hdmi_spec *spec = codec->spec;
2247 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2248}
2249
2250static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2251 struct hda_codec *codec,
2252 unsigned int stream_tag,
2253 unsigned int format,
2254 struct snd_pcm_substream *substream)
2255{
2256 struct hdmi_spec *spec = codec->spec;
2257 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2258 stream_tag, format, substream);
2259}
2260
Takashi Iwaid0b12522012-06-15 14:34:42 +02002261static const struct hda_pcm_stream simple_pcm_playback = {
2262 .substreams = 1,
2263 .channels_min = 2,
2264 .channels_max = 2,
2265 .ops = {
2266 .open = simple_playback_pcm_open,
2267 .close = simple_playback_pcm_close,
2268 .prepare = simple_playback_pcm_prepare
2269 },
2270};
2271
2272static const struct hda_codec_ops simple_hdmi_patch_ops = {
2273 .build_controls = simple_playback_build_controls,
2274 .build_pcms = simple_playback_build_pcms,
2275 .init = simple_playback_init,
2276 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002277 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002278};
2279
2280static int patch_simple_hdmi(struct hda_codec *codec,
2281 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2282{
2283 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002284 struct hdmi_spec_per_cvt *per_cvt;
2285 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002286
2287 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2288 if (!spec)
2289 return -ENOMEM;
2290
2291 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002292 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002293
2294 spec->multiout.num_dacs = 0; /* no analog */
2295 spec->multiout.max_channels = 2;
2296 spec->multiout.dig_out_nid = cvt_nid;
2297 spec->num_cvts = 1;
2298 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002299 per_pin = snd_array_new(&spec->pins);
2300 per_cvt = snd_array_new(&spec->cvts);
2301 if (!per_pin || !per_cvt) {
2302 simple_playback_free(codec);
2303 return -ENOMEM;
2304 }
2305 per_cvt->cvt_nid = cvt_nid;
2306 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002307 spec->pcm_playback = simple_pcm_playback;
2308
2309 codec->patch_ops = simple_hdmi_patch_ops;
2310
2311 return 0;
2312}
2313
Aaron Plattner1f348522011-04-06 17:19:04 -07002314static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2315 int channels)
2316{
2317 unsigned int chanmask;
2318 int chan = channels ? (channels - 1) : 1;
2319
2320 switch (channels) {
2321 default:
2322 case 0:
2323 case 2:
2324 chanmask = 0x00;
2325 break;
2326 case 4:
2327 chanmask = 0x08;
2328 break;
2329 case 6:
2330 chanmask = 0x0b;
2331 break;
2332 case 8:
2333 chanmask = 0x13;
2334 break;
2335 }
2336
2337 /* Set the audio infoframe channel allocation and checksum fields. The
2338 * channel count is computed implicitly by the hardware. */
2339 snd_hda_codec_write(codec, 0x1, 0,
2340 Nv_VERB_SET_Channel_Allocation, chanmask);
2341
2342 snd_hda_codec_write(codec, 0x1, 0,
2343 Nv_VERB_SET_Info_Frame_Checksum,
2344 (0x71 - chan - chanmask));
2345}
2346
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002347static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2348 struct hda_codec *codec,
2349 struct snd_pcm_substream *substream)
2350{
2351 struct hdmi_spec *spec = codec->spec;
2352 int i;
2353
2354 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2355 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2356 for (i = 0; i < 4; i++) {
2357 /* set the stream id */
2358 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2359 AC_VERB_SET_CHANNEL_STREAMID, 0);
2360 /* set the stream format */
2361 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2362 AC_VERB_SET_STREAM_FORMAT, 0);
2363 }
2364
Aaron Plattner1f348522011-04-06 17:19:04 -07002365 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2366 * streams are disabled. */
2367 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2368
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002369 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2370}
2371
2372static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2373 struct hda_codec *codec,
2374 unsigned int stream_tag,
2375 unsigned int format,
2376 struct snd_pcm_substream *substream)
2377{
2378 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002379 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002380 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002381 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002382 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002383 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002384
2385 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002386 per_cvt = get_cvt(spec, 0);
2387 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002388
2389 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002390
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002391 dataDCC2 = 0x2;
2392
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002393 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002394 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002395 snd_hda_codec_write(codec,
2396 nvhdmi_master_con_nid_7x,
2397 0,
2398 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002399 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002400
2401 /* set the stream id */
2402 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2403 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2404
2405 /* set the stream format */
2406 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2407 AC_VERB_SET_STREAM_FORMAT, format);
2408
2409 /* turn on again (if needed) */
2410 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002411 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002412 snd_hda_codec_write(codec,
2413 nvhdmi_master_con_nid_7x,
2414 0,
2415 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002416 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002417 snd_hda_codec_write(codec,
2418 nvhdmi_master_con_nid_7x,
2419 0,
2420 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2421 }
2422
2423 for (i = 0; i < 4; i++) {
2424 if (chs == 2)
2425 channel_id = 0;
2426 else
2427 channel_id = i * 2;
2428
2429 /* turn off SPDIF once;
2430 *otherwise the IEC958 bits won't be updated
2431 */
2432 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002433 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002434 snd_hda_codec_write(codec,
2435 nvhdmi_con_nids_7x[i],
2436 0,
2437 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002438 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002439 /* set the stream id */
2440 snd_hda_codec_write(codec,
2441 nvhdmi_con_nids_7x[i],
2442 0,
2443 AC_VERB_SET_CHANNEL_STREAMID,
2444 (stream_tag << 4) | channel_id);
2445 /* set the stream format */
2446 snd_hda_codec_write(codec,
2447 nvhdmi_con_nids_7x[i],
2448 0,
2449 AC_VERB_SET_STREAM_FORMAT,
2450 format);
2451 /* turn on again (if needed) */
2452 /* enable and set the channel status audio/data flag */
2453 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002454 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002455 snd_hda_codec_write(codec,
2456 nvhdmi_con_nids_7x[i],
2457 0,
2458 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002459 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002460 snd_hda_codec_write(codec,
2461 nvhdmi_con_nids_7x[i],
2462 0,
2463 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2464 }
2465 }
2466
Aaron Plattner1f348522011-04-06 17:19:04 -07002467 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002468
2469 mutex_unlock(&codec->spdif_mutex);
2470 return 0;
2471}
2472
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002473static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002474 .substreams = 1,
2475 .channels_min = 2,
2476 .channels_max = 8,
2477 .nid = nvhdmi_master_con_nid_7x,
2478 .rates = SUPPORTED_RATES,
2479 .maxbps = SUPPORTED_MAXBPS,
2480 .formats = SUPPORTED_FORMATS,
2481 .ops = {
2482 .open = simple_playback_pcm_open,
2483 .close = nvhdmi_8ch_7x_pcm_close,
2484 .prepare = nvhdmi_8ch_7x_pcm_prepare
2485 },
2486};
2487
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002488static int patch_nvhdmi_2ch(struct hda_codec *codec)
2489{
2490 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002491 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2492 nvhdmi_master_pin_nid_7x);
2493 if (err < 0)
2494 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002495
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002496 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002497 /* override the PCM rates, etc, as the codec doesn't give full list */
2498 spec = codec->spec;
2499 spec->pcm_playback.rates = SUPPORTED_RATES;
2500 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2501 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002502 return 0;
2503}
2504
Takashi Iwai53775b02012-08-01 12:17:41 +02002505static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2506{
2507 struct hdmi_spec *spec = codec->spec;
2508 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002509 if (!err) {
2510 struct hda_pcm *info = get_pcm_rec(spec, 0);
2511 info->own_chmap = true;
2512 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002513 return err;
2514}
2515
2516static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2517{
2518 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002519 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002520 struct snd_pcm_chmap *chmap;
2521 int err;
2522
2523 err = simple_playback_build_controls(codec);
2524 if (err < 0)
2525 return err;
2526
2527 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002528 info = get_pcm_rec(spec, 0);
2529 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002530 SNDRV_PCM_STREAM_PLAYBACK,
2531 snd_pcm_alt_chmaps, 8, 0, &chmap);
2532 if (err < 0)
2533 return err;
2534 switch (codec->preset->id) {
2535 case 0x10de0002:
2536 case 0x10de0003:
2537 case 0x10de0005:
2538 case 0x10de0006:
2539 chmap->channel_mask = (1U << 2) | (1U << 8);
2540 break;
2541 case 0x10de0007:
2542 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2543 }
2544 return 0;
2545}
2546
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002547static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2548{
2549 struct hdmi_spec *spec;
2550 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002551 if (err < 0)
2552 return err;
2553 spec = codec->spec;
2554 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002555 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002556 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002557 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2558 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002559
2560 /* Initialize the audio infoframe channel mask and checksum to something
2561 * valid */
2562 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2563
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002564 return 0;
2565}
2566
2567/*
2568 * ATI-specific implementations
2569 *
2570 * FIXME: we may omit the whole this and use the generic code once after
2571 * it's confirmed to work.
2572 */
2573
2574#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2575#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2576
2577static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2578 struct hda_codec *codec,
2579 unsigned int stream_tag,
2580 unsigned int format,
2581 struct snd_pcm_substream *substream)
2582{
2583 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002584 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002585 int chans = substream->runtime->channels;
2586 int i, err;
2587
2588 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2589 substream);
2590 if (err < 0)
2591 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002592 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002593 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002594 /* FIXME: XXX */
2595 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002596 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002597 AC_VERB_SET_HDMI_CHAN_SLOT,
2598 (i << 4) | i);
2599 }
2600 return 0;
2601}
2602
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002603static int patch_atihdmi(struct hda_codec *codec)
2604{
2605 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002606 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2607 if (err < 0)
2608 return err;
2609 spec = codec->spec;
2610 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002611 return 0;
2612}
2613
Annie Liu3de5ff82012-06-08 19:18:42 +08002614/* VIA HDMI Implementation */
2615#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2616#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2617
Annie Liu3de5ff82012-06-08 19:18:42 +08002618static int patch_via_hdmi(struct hda_codec *codec)
2619{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002620 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002621}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002622
2623/*
2624 * patch entries
2625 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002626static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002627{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2628{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2629{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002630{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002631{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2632{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2633{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2634{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2635{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2636{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2637{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2638{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002639{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2640{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2641{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2642{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2643{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2644{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2645{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2646{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2647{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2648{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2649{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002650/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002651{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2652{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2653{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2654{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2655{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2656{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2657{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2658{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2659{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2660{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002661{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002662{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002663{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2664{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002665{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2666{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2667{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2668{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002669{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2670{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2671{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2672{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2673{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2674{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002675{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002676{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002677{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002678{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2679{} /* terminator */
2680};
2681
2682MODULE_ALIAS("snd-hda-codec-id:1002793c");
2683MODULE_ALIAS("snd-hda-codec-id:10027919");
2684MODULE_ALIAS("snd-hda-codec-id:1002791a");
2685MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2686MODULE_ALIAS("snd-hda-codec-id:10951390");
2687MODULE_ALIAS("snd-hda-codec-id:10951392");
2688MODULE_ALIAS("snd-hda-codec-id:10de0002");
2689MODULE_ALIAS("snd-hda-codec-id:10de0003");
2690MODULE_ALIAS("snd-hda-codec-id:10de0005");
2691MODULE_ALIAS("snd-hda-codec-id:10de0006");
2692MODULE_ALIAS("snd-hda-codec-id:10de0007");
2693MODULE_ALIAS("snd-hda-codec-id:10de000a");
2694MODULE_ALIAS("snd-hda-codec-id:10de000b");
2695MODULE_ALIAS("snd-hda-codec-id:10de000c");
2696MODULE_ALIAS("snd-hda-codec-id:10de000d");
2697MODULE_ALIAS("snd-hda-codec-id:10de0010");
2698MODULE_ALIAS("snd-hda-codec-id:10de0011");
2699MODULE_ALIAS("snd-hda-codec-id:10de0012");
2700MODULE_ALIAS("snd-hda-codec-id:10de0013");
2701MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002702MODULE_ALIAS("snd-hda-codec-id:10de0015");
2703MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002704MODULE_ALIAS("snd-hda-codec-id:10de0018");
2705MODULE_ALIAS("snd-hda-codec-id:10de0019");
2706MODULE_ALIAS("snd-hda-codec-id:10de001a");
2707MODULE_ALIAS("snd-hda-codec-id:10de001b");
2708MODULE_ALIAS("snd-hda-codec-id:10de001c");
2709MODULE_ALIAS("snd-hda-codec-id:10de0040");
2710MODULE_ALIAS("snd-hda-codec-id:10de0041");
2711MODULE_ALIAS("snd-hda-codec-id:10de0042");
2712MODULE_ALIAS("snd-hda-codec-id:10de0043");
2713MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002714MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002715MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002716MODULE_ALIAS("snd-hda-codec-id:10de0067");
2717MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002718MODULE_ALIAS("snd-hda-codec-id:11069f80");
2719MODULE_ALIAS("snd-hda-codec-id:11069f81");
2720MODULE_ALIAS("snd-hda-codec-id:11069f84");
2721MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002722MODULE_ALIAS("snd-hda-codec-id:17e80047");
2723MODULE_ALIAS("snd-hda-codec-id:80860054");
2724MODULE_ALIAS("snd-hda-codec-id:80862801");
2725MODULE_ALIAS("snd-hda-codec-id:80862802");
2726MODULE_ALIAS("snd-hda-codec-id:80862803");
2727MODULE_ALIAS("snd-hda-codec-id:80862804");
2728MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002729MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002730MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002731MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002732MODULE_ALIAS("snd-hda-codec-id:808629fb");
2733
2734MODULE_LICENSE("GPL");
2735MODULE_DESCRIPTION("HDMI HD-audio codec");
2736MODULE_ALIAS("snd-hda-codec-intelhdmi");
2737MODULE_ALIAS("snd-hda-codec-nvhdmi");
2738MODULE_ALIAS("snd-hda-codec-atihdmi");
2739
2740static struct hda_codec_preset_list intel_list = {
2741 .preset = snd_hda_preset_hdmi,
2742 .owner = THIS_MODULE,
2743};
2744
2745static int __init patch_hdmi_init(void)
2746{
2747 return snd_hda_add_codec_preset(&intel_list);
2748}
2749
2750static void __exit patch_hdmi_exit(void)
2751{
2752 snd_hda_delete_codec_preset(&intel_list);
2753}
2754
2755module_init(patch_hdmi_init)
2756module_exit(patch_hdmi_exit)