blob: b83b14fa1d29a84f02d670f7c654676e349e4301 [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Stephen Warren384a48d2011-06-01 11:14:21 -060047struct hdmi_spec_per_cvt {
48 hda_nid_t cvt_nid;
49 int assigned;
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
56
Takashi Iwai4eea3092013-02-07 18:18:19 +010057/* max. connections to a widget */
58#define HDA_MAX_CONNECTIONS 32
59
Stephen Warren384a48d2011-06-01 11:14:21 -060060struct hdmi_spec_per_pin {
61 hda_nid_t pin_nid;
62 int num_mux_nids;
63 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080064
65 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060066 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080067 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010068 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060069 int repoll_count;
Takashi Iwai1a6003b2012-09-06 17:42:08 +020070 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020071 bool chmap_set; /* channel-map override by ALSA API? */
72 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010073 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060074};
75
Wu Fengguang079d88c2010-03-08 10:44:23 +080076struct hdmi_spec {
77 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010078 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
79 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060080
Wu Fengguang079d88c2010-03-08 10:44:23 +080081 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010082 struct snd_array pins; /* struct hdmi_spec_per_pin */
83 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020084 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080085
David Henningsson4bd038f2013-02-19 16:11:25 +010086 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080087 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060088 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 */
90 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020091 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080092};
93
94
95struct hdmi_audio_infoframe {
96 u8 type; /* 0x84 */
97 u8 ver; /* 0x01 */
98 u8 len; /* 0x0a */
99
Wu Fengguang53d7d692010-09-21 14:25:49 +0800100 u8 checksum;
101
Wu Fengguang079d88c2010-03-08 10:44:23 +0800102 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
103 u8 SS01_SF24;
104 u8 CXT04;
105 u8 CA;
106 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800107};
108
109struct dp_audio_infoframe {
110 u8 type; /* 0x84 */
111 u8 len; /* 0x1b */
112 u8 ver; /* 0x11 << 2 */
113
114 u8 CC02_CT47; /* match with HDMI infoframe from this on */
115 u8 SS01_SF24;
116 u8 CXT04;
117 u8 CA;
118 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800119};
120
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100121union audio_infoframe {
122 struct hdmi_audio_infoframe hdmi;
123 struct dp_audio_infoframe dp;
124 u8 bytes[0];
125};
126
Wu Fengguang079d88c2010-03-08 10:44:23 +0800127/*
128 * CEA speaker placement:
129 *
130 * FLH FCH FRH
131 * FLW FL FLC FC FRC FR FRW
132 *
133 * LFE
134 * TC
135 *
136 * RL RLC RC RRC RR
137 *
138 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
139 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
140 */
141enum cea_speaker_placement {
142 FL = (1 << 0), /* Front Left */
143 FC = (1 << 1), /* Front Center */
144 FR = (1 << 2), /* Front Right */
145 FLC = (1 << 3), /* Front Left Center */
146 FRC = (1 << 4), /* Front Right Center */
147 RL = (1 << 5), /* Rear Left */
148 RC = (1 << 6), /* Rear Center */
149 RR = (1 << 7), /* Rear Right */
150 RLC = (1 << 8), /* Rear Left Center */
151 RRC = (1 << 9), /* Rear Right Center */
152 LFE = (1 << 10), /* Low Frequency Effect */
153 FLW = (1 << 11), /* Front Left Wide */
154 FRW = (1 << 12), /* Front Right Wide */
155 FLH = (1 << 13), /* Front Left High */
156 FCH = (1 << 14), /* Front Center High */
157 FRH = (1 << 15), /* Front Right High */
158 TC = (1 << 16), /* Top Center */
159};
160
161/*
162 * ELD SA bits in the CEA Speaker Allocation data block
163 */
164static int eld_speaker_allocation_bits[] = {
165 [0] = FL | FR,
166 [1] = LFE,
167 [2] = FC,
168 [3] = RL | RR,
169 [4] = RC,
170 [5] = FLC | FRC,
171 [6] = RLC | RRC,
172 /* the following are not defined in ELD yet */
173 [7] = FLW | FRW,
174 [8] = FLH | FRH,
175 [9] = TC,
176 [10] = FCH,
177};
178
179struct cea_channel_speaker_allocation {
180 int ca_index;
181 int speakers[8];
182
183 /* derived values, just for convenience */
184 int channels;
185 int spk_mask;
186};
187
188/*
189 * ALSA sequence is:
190 *
191 * surround40 surround41 surround50 surround51 surround71
192 * ch0 front left = = = =
193 * ch1 front right = = = =
194 * ch2 rear left = = = =
195 * ch3 rear right = = = =
196 * ch4 LFE center center center
197 * ch5 LFE LFE
198 * ch6 side left
199 * ch7 side right
200 *
201 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
202 */
203static int hdmi_channel_mapping[0x32][8] = {
204 /* stereo */
205 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
206 /* 2.1 */
207 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
208 /* Dolby Surround */
209 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
210 /* surround40 */
211 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
212 /* 4ch */
213 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
214 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800215 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800216 /* surround50 */
217 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
218 /* surround51 */
219 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
220 /* 7.1 */
221 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
222};
223
224/*
225 * This is an ordered list!
226 *
227 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800228 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800229 */
230static struct cea_channel_speaker_allocation channel_allocations[] = {
231/* channel: 7 6 5 4 3 2 1 0 */
232{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
233 /* 2.1 */
234{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
235 /* Dolby Surround */
236{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
237 /* surround40 */
238{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
239 /* surround41 */
240{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
241 /* surround50 */
242{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
243 /* surround51 */
244{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
245 /* 6.1 */
246{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
247 /* surround71 */
248{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
249
250{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
251{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
252{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
253{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
254{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
255{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
256{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
257{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
258{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
259{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
260{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
261{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
262{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
263{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
264{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
265{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
266{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
267{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
268{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
269{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
270{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
271{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
272{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
273{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
274{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
275{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
276{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
277{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
278{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
279{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
280{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
281{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
282{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
283{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
284{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
285{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
286{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
287{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
291};
292
293
294/*
295 * HDMI routines
296 */
297
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100298#define get_pin(spec, idx) \
299 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
300#define get_cvt(spec, idx) \
301 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
302#define get_pcm_rec(spec, idx) \
303 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800306{
Stephen Warren384a48d2011-06-01 11:14:21 -0600307 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100310 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600311 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800312
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
314 return -EINVAL;
315}
316
317static int hinfo_to_pin_index(struct hdmi_spec *spec,
318 struct hda_pcm_stream *hinfo)
319{
320 int pin_idx;
321
322 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100323 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600324 return pin_idx;
325
326 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
327 return -EINVAL;
328}
329
330static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
331{
332 int cvt_idx;
333
334 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100335 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600336 return cvt_idx;
337
338 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800339 return -EINVAL;
340}
341
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500342static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_info *uinfo)
344{
345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100346 struct hdmi_spec *spec = codec->spec;
347 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500348 int pin_idx;
349
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500350 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
351
352 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100353 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100354
David Henningsson4bd038f2013-02-19 16:11:25 +0100355 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100356 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100357 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500358
359 return 0;
360}
361
362static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
364{
365 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100366 struct hdmi_spec *spec = codec->spec;
367 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500368 int pin_idx;
369
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500370 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100371 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500372
David Henningsson4bd038f2013-02-19 16:11:25 +0100373 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100374 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100375 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100376 snd_BUG();
377 return -EINVAL;
378 }
379
380 memset(ucontrol->value.bytes.data, 0,
381 ARRAY_SIZE(ucontrol->value.bytes.data));
382 if (eld->eld_valid)
383 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
384 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100385 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500386
387 return 0;
388}
389
390static struct snd_kcontrol_new eld_bytes_ctl = {
391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
392 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
393 .name = "ELD",
394 .info = hdmi_eld_ctl_info,
395 .get = hdmi_eld_ctl_get,
396};
397
398static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
399 int device)
400{
401 struct snd_kcontrol *kctl;
402 struct hdmi_spec *spec = codec->spec;
403 int err;
404
405 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
406 if (!kctl)
407 return -ENOMEM;
408 kctl->private_value = pin_idx;
409 kctl->id.device = device;
410
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100411 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500412 if (err < 0)
413 return err;
414
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100415 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500416 return 0;
417}
418
Wu Fengguang079d88c2010-03-08 10:44:23 +0800419#ifdef BE_PARANOID
420static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
421 int *packet_index, int *byte_index)
422{
423 int val;
424
425 val = snd_hda_codec_read(codec, pin_nid, 0,
426 AC_VERB_GET_HDMI_DIP_INDEX, 0);
427
428 *packet_index = val >> 5;
429 *byte_index = val & 0x1f;
430}
431#endif
432
433static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
434 int packet_index, int byte_index)
435{
436 int val;
437
438 val = (packet_index << 5) | (byte_index & 0x1f);
439
440 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
441}
442
443static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
444 unsigned char val)
445{
446 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
447}
448
Stephen Warren384a48d2011-06-01 11:14:21 -0600449static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800450{
451 /* Unmute */
452 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
453 snd_hda_codec_write(codec, pin_nid, 0,
454 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100455 /* Enable pin out: some machines with GM965 gets broken output when
456 * the pin is disabled or changed while using with HDMI
457 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800458 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100459 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800460}
461
Stephen Warren384a48d2011-06-01 11:14:21 -0600462static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800463{
Stephen Warren384a48d2011-06-01 11:14:21 -0600464 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800465 AC_VERB_GET_CVT_CHAN_COUNT, 0);
466}
467
468static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600469 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800470{
Stephen Warren384a48d2011-06-01 11:14:21 -0600471 if (chs != hdmi_get_channel_count(codec, cvt_nid))
472 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800473 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
474}
475
476
477/*
478 * Channel mapping routines
479 */
480
481/*
482 * Compute derived values in channel_allocations[].
483 */
484static void init_channel_allocations(void)
485{
486 int i, j;
487 struct cea_channel_speaker_allocation *p;
488
489 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
490 p = channel_allocations + i;
491 p->channels = 0;
492 p->spk_mask = 0;
493 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
494 if (p->speakers[j]) {
495 p->channels++;
496 p->spk_mask |= p->speakers[j];
497 }
498 }
499}
500
Wang Xingchao72357c72012-09-06 10:02:36 +0800501static int get_channel_allocation_order(int ca)
502{
503 int i;
504
505 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
506 if (channel_allocations[i].ca_index == ca)
507 break;
508 }
509 return i;
510}
511
Wu Fengguang079d88c2010-03-08 10:44:23 +0800512/*
513 * The transformation takes two steps:
514 *
515 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
516 * spk_mask => (channel_allocations[]) => ai->CA
517 *
518 * TODO: it could select the wrong CA from multiple candidates.
519*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600520static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800521{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800522 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800523 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800524 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800525 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
526
527 /*
528 * CA defaults to 0 for basic stereo audio
529 */
530 if (channels <= 2)
531 return 0;
532
Wu Fengguang079d88c2010-03-08 10:44:23 +0800533 /*
534 * expand ELD's speaker allocation mask
535 *
536 * ELD tells the speaker mask in a compact(paired) form,
537 * expand ELD's notions to match the ones used by Audio InfoFrame.
538 */
539 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100540 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800541 spk_mask |= eld_speaker_allocation_bits[i];
542 }
543
544 /* search for the first working match in the CA table */
545 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
546 if (channels == channel_allocations[i].channels &&
547 (spk_mask & channel_allocations[i].spk_mask) ==
548 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800549 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800550 break;
551 }
552 }
553
Anssi Hannula18e39182013-09-01 14:36:47 +0300554 if (!ca) {
555 /* if there was no match, select the regular ALSA channel
556 * allocation with the matching number of channels */
557 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
558 if (channels == channel_allocations[i].channels) {
559 ca = channel_allocations[i].ca_index;
560 break;
561 }
562 }
563 }
564
David Henningsson1613d6b2013-02-19 16:11:24 +0100565 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800566 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800567 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800568
Wu Fengguang53d7d692010-09-21 14:25:49 +0800569 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570}
571
572static void hdmi_debug_channel_mapping(struct hda_codec *codec,
573 hda_nid_t pin_nid)
574{
575#ifdef CONFIG_SND_DEBUG_VERBOSE
576 int i;
577 int slot;
578
579 for (i = 0; i < 8; i++) {
580 slot = snd_hda_codec_read(codec, pin_nid, 0,
581 AC_VERB_GET_HDMI_CHAN_SLOT, i);
582 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
583 slot >> 4, slot & 0xf);
584 }
585#endif
586}
587
588
Takashi Iwaid45e6882012-07-31 11:36:00 +0200589static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800591 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800592 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800593{
594 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800596 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800597 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800598
Wang Xingchao72357c72012-09-06 10:02:36 +0800599 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800600
Wu Fengguang079d88c2010-03-08 10:44:23 +0800601 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800602 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800603 hdmi_channel_mapping[ca][i] = i | (i << 4);
604 for (; i < 8; i++)
605 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
606 }
607
Wang Xingchao433968d2012-09-06 10:02:37 +0800608 if (non_pcm) {
609 for (i = 0; i < channel_allocations[order].channels; i++)
610 non_pcm_mapping[i] = i | (i << 4);
611 for (; i < 8; i++)
612 non_pcm_mapping[i] = 0xf | (i << 4);
613 }
614
Wu Fengguang079d88c2010-03-08 10:44:23 +0800615 for (i = 0; i < 8; i++) {
616 err = snd_hda_codec_write(codec, pin_nid, 0,
617 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800618 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800619 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800620 snd_printdd(KERN_NOTICE
621 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800622 break;
623 }
624 }
625
626 hdmi_debug_channel_mapping(codec, pin_nid);
627}
628
Takashi Iwaid45e6882012-07-31 11:36:00 +0200629struct channel_map_table {
630 unsigned char map; /* ALSA API channel map position */
631 unsigned char cea_slot; /* CEA slot value */
632 int spk_mask; /* speaker position bit mask */
633};
634
635static struct channel_map_table map_tables[] = {
636 { SNDRV_CHMAP_FL, 0x00, FL },
637 { SNDRV_CHMAP_FR, 0x01, FR },
638 { SNDRV_CHMAP_RL, 0x04, RL },
639 { SNDRV_CHMAP_RR, 0x05, RR },
640 { SNDRV_CHMAP_LFE, 0x02, LFE },
641 { SNDRV_CHMAP_FC, 0x03, FC },
642 { SNDRV_CHMAP_RLC, 0x06, RLC },
643 { SNDRV_CHMAP_RRC, 0x07, RRC },
644 {} /* terminator */
645};
646
647/* from ALSA API channel position to speaker bit mask */
648static int to_spk_mask(unsigned char c)
649{
650 struct channel_map_table *t = map_tables;
651 for (; t->map; t++) {
652 if (t->map == c)
653 return t->spk_mask;
654 }
655 return 0;
656}
657
658/* from ALSA API channel position to CEA slot */
659static int to_cea_slot(unsigned char c)
660{
661 struct channel_map_table *t = map_tables;
662 for (; t->map; t++) {
663 if (t->map == c)
664 return t->cea_slot;
665 }
666 return 0x0f;
667}
668
669/* from CEA slot to ALSA API channel position */
670static int from_cea_slot(unsigned char c)
671{
672 struct channel_map_table *t = map_tables;
673 for (; t->map; t++) {
674 if (t->cea_slot == c)
675 return t->map;
676 }
677 return 0;
678}
679
680/* from speaker bit mask to ALSA API channel position */
681static int spk_to_chmap(int spk)
682{
683 struct channel_map_table *t = map_tables;
684 for (; t->map; t++) {
685 if (t->spk_mask == spk)
686 return t->map;
687 }
688 return 0;
689}
690
691/* get the CA index corresponding to the given ALSA API channel map */
692static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
693{
694 int i, spks = 0, spk_mask = 0;
695
696 for (i = 0; i < chs; i++) {
697 int mask = to_spk_mask(map[i]);
698 if (mask) {
699 spk_mask |= mask;
700 spks++;
701 }
702 }
703
704 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
705 if ((chs == channel_allocations[i].channels ||
706 spks == channel_allocations[i].channels) &&
707 (spk_mask & channel_allocations[i].spk_mask) ==
708 channel_allocations[i].spk_mask)
709 return channel_allocations[i].ca_index;
710 }
711 return -1;
712}
713
714/* set up the channel slots for the given ALSA API channel map */
715static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
716 hda_nid_t pin_nid,
717 int chs, unsigned char *map)
718{
719 int i;
720 for (i = 0; i < 8; i++) {
721 int val, err;
722 if (i < chs)
723 val = to_cea_slot(map[i]);
724 else
725 val = 0xf;
726 val |= (i << 4);
727 err = snd_hda_codec_write(codec, pin_nid, 0,
728 AC_VERB_SET_HDMI_CHAN_SLOT, val);
729 if (err)
730 return -EINVAL;
731 }
732 return 0;
733}
734
735/* store ALSA API channel map from the current default map */
736static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
737{
738 int i;
739 for (i = 0; i < 8; i++) {
740 if (i < channel_allocations[ca].channels)
741 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
742 else
743 map[i] = 0;
744 }
745}
746
747static void hdmi_setup_channel_mapping(struct hda_codec *codec,
748 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200749 int channels, unsigned char *map,
750 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200751{
Anssi Hannula20608732013-02-03 17:55:45 +0200752 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200753 hdmi_manual_setup_channel_mapping(codec, pin_nid,
754 channels, map);
755 } else {
756 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
757 hdmi_setup_fake_chmap(map, ca);
758 }
759}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800760
761/*
762 * Audio InfoFrame routines
763 */
764
765/*
766 * Enable Audio InfoFrame Transmission
767 */
768static void hdmi_start_infoframe_trans(struct hda_codec *codec,
769 hda_nid_t pin_nid)
770{
771 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
772 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
773 AC_DIPXMIT_BEST);
774}
775
776/*
777 * Disable Audio InfoFrame Transmission
778 */
779static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
780 hda_nid_t pin_nid)
781{
782 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
783 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
784 AC_DIPXMIT_DISABLE);
785}
786
787static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
788{
789#ifdef CONFIG_SND_DEBUG_VERBOSE
790 int i;
791 int size;
792
793 size = snd_hdmi_get_eld_size(codec, pin_nid);
794 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
795
796 for (i = 0; i < 8; i++) {
797 size = snd_hda_codec_read(codec, pin_nid, 0,
798 AC_VERB_GET_HDMI_DIP_SIZE, i);
799 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
800 }
801#endif
802}
803
804static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
805{
806#ifdef BE_PARANOID
807 int i, j;
808 int size;
809 int pi, bi;
810 for (i = 0; i < 8; i++) {
811 size = snd_hda_codec_read(codec, pin_nid, 0,
812 AC_VERB_GET_HDMI_DIP_SIZE, i);
813 if (size == 0)
814 continue;
815
816 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
817 for (j = 1; j < 1000; j++) {
818 hdmi_write_dip_byte(codec, pin_nid, 0x0);
819 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
820 if (pi != i)
821 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
822 bi, pi, i);
823 if (bi == 0) /* byte index wrapped around */
824 break;
825 }
826 snd_printd(KERN_INFO
827 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
828 i, size, j);
829 }
830#endif
831}
832
Wu Fengguang53d7d692010-09-21 14:25:49 +0800833static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800834{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800835 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800836 u8 sum = 0;
837 int i;
838
Wu Fengguang53d7d692010-09-21 14:25:49 +0800839 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800840
Wu Fengguang53d7d692010-09-21 14:25:49 +0800841 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800842 sum += bytes[i];
843
Wu Fengguang53d7d692010-09-21 14:25:49 +0800844 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800845}
846
847static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
848 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800849 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800850{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800851 int i;
852
853 hdmi_debug_dip_size(codec, pin_nid);
854 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
855
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800857 for (i = 0; i < size; i++)
858 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800859}
860
861static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800862 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800863{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800864 u8 val;
865 int i;
866
867 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
868 != AC_DIPXMIT_BEST)
869 return false;
870
871 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800872 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800873 val = snd_hda_codec_read(codec, pin_nid, 0,
874 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800875 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876 return false;
877 }
878
879 return true;
880}
881
Stephen Warren384a48d2011-06-01 11:14:21 -0600882static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200883 bool non_pcm,
884 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800885{
886 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100887 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600888 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800889 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800891 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100892 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800893
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100894 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600895 if (!eld->monitor_present)
896 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800897
Takashi Iwaid45e6882012-07-31 11:36:00 +0200898 if (!non_pcm && per_pin->chmap_set)
899 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
900 else
901 ca = hdmi_channel_allocation(eld, channels);
902 if (ca < 0)
903 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800904
Stephen Warren384a48d2011-06-01 11:14:21 -0600905 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100906 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600907 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800908
Stephen Warren384a48d2011-06-01 11:14:21 -0600909 hdmi_ai->type = 0x84;
910 hdmi_ai->ver = 0x01;
911 hdmi_ai->len = 0x0a;
912 hdmi_ai->CC02_CT47 = channels - 1;
913 hdmi_ai->CA = ca;
914 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100915 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600916 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800917
Stephen Warren384a48d2011-06-01 11:14:21 -0600918 dp_ai->type = 0x84;
919 dp_ai->len = 0x1b;
920 dp_ai->ver = 0x11 << 2;
921 dp_ai->CC02_CT47 = channels - 1;
922 dp_ai->CA = ca;
923 } else {
924 snd_printd("HDMI: unknown connection type at pin %d\n",
925 pin_nid);
926 return;
927 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800928
Stephen Warren384a48d2011-06-01 11:14:21 -0600929 /*
930 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
931 * sizeof(*dp_ai) to avoid partial match/update problems when
932 * the user switches between HDMI/DP monitors.
933 */
934 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
935 sizeof(ai))) {
936 snd_printdd("hdmi_setup_audio_infoframe: "
937 "pin=%d channels=%d\n",
938 pin_nid,
939 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200940 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200941 channels, per_pin->chmap,
942 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600943 hdmi_stop_infoframe_trans(codec, pin_nid);
944 hdmi_fill_audio_infoframe(codec, pin_nid,
945 ai.bytes, sizeof(ai));
946 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800947 } else {
948 /* For non-pcm audio switch, setup new channel mapping
949 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200950 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200951 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200952 channels, per_pin->chmap,
953 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800954 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800955
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200956 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800957}
958
959
960/*
961 * Unsolicited events
962 */
963
Wu Fengguangc6e84532011-11-18 16:59:32 -0600964static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200965
Wu Fengguang079d88c2010-03-08 10:44:23 +0800966static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
967{
968 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200969 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
970 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600971 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200972 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400973 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200974
975 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
976 if (!jack)
977 return;
978 pin_nid = jack->nid;
979 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800980
Fengguang Wufae3d882012-04-10 17:00:35 +0800981 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400982 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
983 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800984 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800985
Stephen Warren384a48d2011-06-01 11:14:21 -0600986 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
987 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800988 return;
989
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100990 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200991 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800992}
993
994static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
995{
996 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
997 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
998 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
999 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1000
1001 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001002 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001003 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001004 tag,
1005 subtag,
1006 cp_state,
1007 cp_ready);
1008
1009 /* TODO */
1010 if (cp_state)
1011 ;
1012 if (cp_ready)
1013 ;
1014}
1015
1016
1017static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1018{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001019 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1020 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1021
Takashi Iwai3a938972011-10-28 01:16:55 +02001022 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001023 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1024 return;
1025 }
1026
1027 if (subtag == 0)
1028 hdmi_intrinsic_event(codec, res);
1029 else
1030 hdmi_non_intrinsic_event(codec, res);
1031}
1032
Wang Xingchao53b434f2013-06-18 10:41:53 +08001033static void haswell_verify_pin_D0(struct hda_codec *codec,
1034 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001035{
1036 int pwr, lamp, ramp;
1037
Wang Xingchao53b434f2013-06-18 10:41:53 +08001038 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1039 * thus pins could only choose converter 0 for use. Make sure the
1040 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001041 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001042 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1043
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001044 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001045 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1046 AC_PWRST_D0);
1047 msleep(40);
1048 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1049 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1050 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1051 }
1052
1053 lamp = snd_hda_codec_read(codec, nid, 0,
1054 AC_VERB_GET_AMP_GAIN_MUTE,
1055 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1056 ramp = snd_hda_codec_read(codec, nid, 0,
1057 AC_VERB_GET_AMP_GAIN_MUTE,
1058 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1059 if (lamp != ramp) {
1060 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1061 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1062
1063 lamp = snd_hda_codec_read(codec, nid, 0,
1064 AC_VERB_GET_AMP_GAIN_MUTE,
1065 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1066 ramp = snd_hda_codec_read(codec, nid, 0,
1067 AC_VERB_GET_AMP_GAIN_MUTE,
1068 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1069 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1070 }
1071}
1072
Wu Fengguang079d88c2010-03-08 10:44:23 +08001073/*
1074 * Callbacks
1075 */
1076
Takashi Iwai92f10b32010-08-03 14:21:00 +02001077/* HBR should be Non-PCM, 8 channels */
1078#define is_hbr_format(format) \
1079 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1080
Stephen Warren384a48d2011-06-01 11:14:21 -06001081static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1082 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001083{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001084 int pinctl;
1085 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001086
David Henningsson83f26ad2013-04-10 12:26:07 +02001087 if (codec->vendor_id == 0x80862807)
Wang Xingchao53b434f2013-06-18 10:41:53 +08001088 haswell_verify_pin_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001089
Stephen Warren384a48d2011-06-01 11:14:21 -06001090 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1091 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001092 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1093
1094 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001095 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001096 new_pinctl |= AC_PINCTL_EPT_HBR;
1097 else
1098 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1099
1100 snd_printdd("hdmi_setup_stream: "
1101 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001102 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001103 pinctl == new_pinctl ? "" : "new-",
1104 new_pinctl);
1105
1106 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001107 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001108 AC_VERB_SET_PIN_WIDGET_CONTROL,
1109 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001110
Stephen Warren384a48d2011-06-01 11:14:21 -06001111 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001112 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001113 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1114 return -EINVAL;
1115 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001116
Stephen Warren384a48d2011-06-01 11:14:21 -06001117 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001118 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001119}
1120
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001121static int hdmi_choose_cvt(struct hda_codec *codec,
1122 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001123{
1124 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001125 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001126 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001127 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001128
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001129 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001130
Stephen Warren384a48d2011-06-01 11:14:21 -06001131 /* Dynamically assign converter to stream */
1132 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001133 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001134
1135 /* Must not already be assigned */
1136 if (per_cvt->assigned)
1137 continue;
1138 /* Must be in pin's mux's list of converters */
1139 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1140 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1141 break;
1142 /* Not in mux list */
1143 if (mux_idx == per_pin->num_mux_nids)
1144 continue;
1145 break;
1146 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001147
Stephen Warren384a48d2011-06-01 11:14:21 -06001148 /* No free converters */
1149 if (cvt_idx == spec->num_cvts)
1150 return -ENODEV;
1151
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001152 if (cvt_id)
1153 *cvt_id = cvt_idx;
1154 if (mux_id)
1155 *mux_id = mux_idx;
1156
1157 return 0;
1158}
1159
1160static void haswell_config_cvts(struct hda_codec *codec,
1161 int pin_id, int mux_id)
1162{
1163 struct hdmi_spec *spec = codec->spec;
1164 struct hdmi_spec_per_pin *per_pin;
1165 int pin_idx, mux_idx;
1166 int curr;
1167 int err;
1168
1169 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1170 per_pin = get_pin(spec, pin_idx);
1171
1172 if (pin_idx == pin_id)
1173 continue;
1174
1175 curr = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1176 AC_VERB_GET_CONNECT_SEL, 0);
1177
1178 /* Choose another unused converter */
1179 if (curr == mux_id) {
1180 err = hdmi_choose_cvt(codec, pin_idx, NULL, &mux_idx);
1181 if (err < 0)
1182 return;
1183 snd_printdd("HDMI: choose converter %d for pin %d\n", mux_idx, pin_idx);
1184 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1185 AC_VERB_SET_CONNECT_SEL,
1186 mux_idx);
1187 }
1188 }
1189}
1190
1191/*
1192 * HDA PCM callbacks
1193 */
1194static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1195 struct hda_codec *codec,
1196 struct snd_pcm_substream *substream)
1197{
1198 struct hdmi_spec *spec = codec->spec;
1199 struct snd_pcm_runtime *runtime = substream->runtime;
1200 int pin_idx, cvt_idx, mux_idx = 0;
1201 struct hdmi_spec_per_pin *per_pin;
1202 struct hdmi_eld *eld;
1203 struct hdmi_spec_per_cvt *per_cvt = NULL;
1204 int err;
1205
1206 /* Validate hinfo */
1207 pin_idx = hinfo_to_pin_index(spec, hinfo);
1208 if (snd_BUG_ON(pin_idx < 0))
1209 return -EINVAL;
1210 per_pin = get_pin(spec, pin_idx);
1211 eld = &per_pin->sink_eld;
1212
1213 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1214 if (err < 0)
1215 return err;
1216
1217 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001218 /* Claim converter */
1219 per_cvt->assigned = 1;
1220 hinfo->nid = per_cvt->cvt_nid;
1221
Takashi Iwaibddee962013-06-18 16:14:22 +02001222 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001223 AC_VERB_SET_CONNECT_SEL,
1224 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001225
1226 /* configure unused pins to choose other converters */
1227 if (codec->vendor_id == 0x80862807)
1228 haswell_config_cvts(codec, pin_idx, mux_idx);
1229
Stephen Warren384a48d2011-06-01 11:14:21 -06001230 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001231
Stephen Warren2def8172011-06-01 11:14:20 -06001232 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001233 hinfo->channels_min = per_cvt->channels_min;
1234 hinfo->channels_max = per_cvt->channels_max;
1235 hinfo->rates = per_cvt->rates;
1236 hinfo->formats = per_cvt->formats;
1237 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001238
Stephen Warren384a48d2011-06-01 11:14:21 -06001239 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001240 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001241 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001242 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001243 !hinfo->rates || !hinfo->formats) {
1244 per_cvt->assigned = 0;
1245 hinfo->nid = 0;
1246 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001247 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001248 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001249 }
Stephen Warren2def8172011-06-01 11:14:20 -06001250
1251 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001252 runtime->hw.channels_min = hinfo->channels_min;
1253 runtime->hw.channels_max = hinfo->channels_max;
1254 runtime->hw.formats = hinfo->formats;
1255 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001256
1257 snd_pcm_hw_constraint_step(substream->runtime, 0,
1258 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001259 return 0;
1260}
1261
1262/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001263 * HDA/HDMI auto parsing
1264 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001265static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001266{
1267 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001268 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001269 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001270
1271 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1272 snd_printk(KERN_WARNING
1273 "HDMI: pin %d wcaps %#x "
1274 "does not support connection list\n",
1275 pin_nid, get_wcaps(codec, pin_nid));
1276 return -EINVAL;
1277 }
1278
Stephen Warren384a48d2011-06-01 11:14:21 -06001279 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1280 per_pin->mux_nids,
1281 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001282
1283 return 0;
1284}
1285
Wu Fengguangc6e84532011-11-18 16:59:32 -06001286static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001287{
Wu Fengguang744626d2011-11-16 16:29:47 +08001288 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001289 struct hdmi_spec *spec = codec->spec;
1290 struct hdmi_eld *eld = &spec->temp_eld;
1291 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001292 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001293 /*
1294 * Always execute a GetPinSense verb here, even when called from
1295 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1296 * response's PD bit is not the real PD value, but indicates that
1297 * the real PD value changed. An older version of the HD-audio
1298 * specification worked this way. Hence, we just ignore the data in
1299 * the unsolicited response to avoid custom WARs.
1300 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001301 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001302 bool update_eld = false;
1303 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001304
David Henningsson4bd038f2013-02-19 16:11:25 +01001305 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1306 if (pin_eld->monitor_present)
1307 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1308 else
1309 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001310
Fengguang Wufae3d882012-04-10 17:00:35 +08001311 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001312 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001313 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001314
David Henningsson4bd038f2013-02-19 16:11:25 +01001315 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001316 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1317 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001318 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001319 else {
1320 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1321 if (snd_hdmi_parse_eld(&eld->info, 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 }
1325
David Henningsson4bd038f2013-02-19 16:11:25 +01001326 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001327 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001328 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001329 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001330 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001331 queue_delayed_work(codec->bus->workq,
1332 &per_pin->work,
1333 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001334 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001335 }
1336 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001337
1338 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001339 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001340 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001341 eld_changed = true;
1342 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001343 if (update_eld) {
1344 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001345 eld_changed = pin_eld->eld_size != eld->eld_size ||
1346 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001347 eld->eld_size) != 0;
1348 if (eld_changed)
1349 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1350 eld->eld_size);
1351 pin_eld->eld_size = eld->eld_size;
1352 pin_eld->info = eld->info;
1353 }
1354 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001355
1356 if (eld_changed)
1357 snd_ctl_notify(codec->bus->card,
1358 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1359 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001360}
1361
Wu Fengguang744626d2011-11-16 16:29:47 +08001362static void hdmi_repoll_eld(struct work_struct *work)
1363{
1364 struct hdmi_spec_per_pin *per_pin =
1365 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1366
Wu Fengguangc6e84532011-11-18 16:59:32 -06001367 if (per_pin->repoll_count++ > 6)
1368 per_pin->repoll_count = 0;
1369
1370 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001371}
1372
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001373static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1374 hda_nid_t nid);
1375
Wu Fengguang079d88c2010-03-08 10:44:23 +08001376static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1377{
1378 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001379 unsigned int caps, config;
1380 int pin_idx;
1381 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001382 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001383
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001384 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001385 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1386 return 0;
1387
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001388 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001389 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1390 return 0;
1391
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001392 if (codec->vendor_id == 0x80862807)
1393 intel_haswell_fixup_connect_list(codec, pin_nid);
1394
Stephen Warren384a48d2011-06-01 11:14:21 -06001395 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001396 per_pin = snd_array_new(&spec->pins);
1397 if (!per_pin)
1398 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001399
1400 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001401 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001402
Stephen Warren384a48d2011-06-01 11:14:21 -06001403 err = hdmi_read_pin_conn(codec, pin_idx);
1404 if (err < 0)
1405 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001406
Wu Fengguang079d88c2010-03-08 10:44:23 +08001407 spec->num_pins++;
1408
Stephen Warren384a48d2011-06-01 11:14:21 -06001409 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001410}
1411
Stephen Warren384a48d2011-06-01 11:14:21 -06001412static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001413{
1414 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001415 struct hdmi_spec_per_cvt *per_cvt;
1416 unsigned int chans;
1417 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001418
Stephen Warren384a48d2011-06-01 11:14:21 -06001419 chans = get_wcaps(codec, cvt_nid);
1420 chans = get_wcaps_channels(chans);
1421
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001422 per_cvt = snd_array_new(&spec->cvts);
1423 if (!per_cvt)
1424 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001425
1426 per_cvt->cvt_nid = cvt_nid;
1427 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001428 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001429 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001430 if (chans > spec->channels_max)
1431 spec->channels_max = chans;
1432 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001433
1434 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1435 &per_cvt->rates,
1436 &per_cvt->formats,
1437 &per_cvt->maxbps);
1438 if (err < 0)
1439 return err;
1440
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001441 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1442 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1443 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001444
1445 return 0;
1446}
1447
1448static int hdmi_parse_codec(struct hda_codec *codec)
1449{
1450 hda_nid_t nid;
1451 int i, nodes;
1452
1453 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1454 if (!nid || nodes < 0) {
1455 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1456 return -EINVAL;
1457 }
1458
1459 for (i = 0; i < nodes; i++, nid++) {
1460 unsigned int caps;
1461 unsigned int type;
1462
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001463 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001464 type = get_wcaps_type(caps);
1465
1466 if (!(caps & AC_WCAP_DIGITAL))
1467 continue;
1468
1469 switch (type) {
1470 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001471 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001472 break;
1473 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001474 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001475 break;
1476 }
1477 }
1478
David Henningssonc9adeef2012-11-07 09:22:33 +01001479#ifdef CONFIG_PM
1480 /* We're seeing some problems with unsolicited hot plug events on
1481 * PantherPoint after S3, if this is not enabled */
1482 if (codec->vendor_id == 0x80862806)
1483 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001484 /*
1485 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1486 * can be lost and presence sense verb will become inaccurate if the
1487 * HDA link is powered off at hot plug or hw initialization time.
1488 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001489 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001490 AC_PWRST_EPSS))
1491 codec->bus->power_keep_link_on = 1;
1492#endif
1493
1494 return 0;
1495}
1496
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001497/*
1498 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001499static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1500{
1501 struct hda_spdif_out *spdif;
1502 bool non_pcm;
1503
1504 mutex_lock(&codec->spdif_mutex);
1505 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1506 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1507 mutex_unlock(&codec->spdif_mutex);
1508 return non_pcm;
1509}
1510
1511
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001512/*
1513 * HDMI callbacks
1514 */
1515
1516static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1517 struct hda_codec *codec,
1518 unsigned int stream_tag,
1519 unsigned int format,
1520 struct snd_pcm_substream *substream)
1521{
Stephen Warren384a48d2011-06-01 11:14:21 -06001522 hda_nid_t cvt_nid = hinfo->nid;
1523 struct hdmi_spec *spec = codec->spec;
1524 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001525 hda_nid_t pin_nid = get_pin(spec, pin_idx)->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001526 bool non_pcm;
1527
1528 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001529
Stephen Warren384a48d2011-06-01 11:14:21 -06001530 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001531
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001532 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001533
1534 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001535}
1536
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001537static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1538 struct hda_codec *codec,
1539 struct snd_pcm_substream *substream)
1540{
1541 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1542 return 0;
1543}
1544
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001545static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1546 struct hda_codec *codec,
1547 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001548{
1549 struct hdmi_spec *spec = codec->spec;
1550 int cvt_idx, pin_idx;
1551 struct hdmi_spec_per_cvt *per_cvt;
1552 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001553
Stephen Warren384a48d2011-06-01 11:14:21 -06001554 if (hinfo->nid) {
1555 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1556 if (snd_BUG_ON(cvt_idx < 0))
1557 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001558 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001559
1560 snd_BUG_ON(!per_cvt->assigned);
1561 per_cvt->assigned = 0;
1562 hinfo->nid = 0;
1563
1564 pin_idx = hinfo_to_pin_index(spec, hinfo);
1565 if (snd_BUG_ON(pin_idx < 0))
1566 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001567 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001568
Stephen Warren384a48d2011-06-01 11:14:21 -06001569 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001570 per_pin->chmap_set = false;
1571 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001572 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001573
Stephen Warren384a48d2011-06-01 11:14:21 -06001574 return 0;
1575}
1576
1577static const struct hda_pcm_ops generic_ops = {
1578 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001579 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001580 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001581 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001582};
1583
Takashi Iwaid45e6882012-07-31 11:36:00 +02001584/*
1585 * ALSA API channel-map control callbacks
1586 */
1587static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1588 struct snd_ctl_elem_info *uinfo)
1589{
1590 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1591 struct hda_codec *codec = info->private_data;
1592 struct hdmi_spec *spec = codec->spec;
1593 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1594 uinfo->count = spec->channels_max;
1595 uinfo->value.integer.min = 0;
1596 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1597 return 0;
1598}
1599
1600static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1601 unsigned int size, unsigned int __user *tlv)
1602{
1603 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1604 struct hda_codec *codec = info->private_data;
1605 struct hdmi_spec *spec = codec->spec;
1606 const unsigned int valid_mask =
1607 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1608 unsigned int __user *dst;
1609 int chs, count = 0;
1610
1611 if (size < 8)
1612 return -ENOMEM;
1613 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1614 return -EFAULT;
1615 size -= 8;
1616 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001617 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001618 int i, c;
1619 struct cea_channel_speaker_allocation *cap;
1620 cap = channel_allocations;
1621 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1622 int chs_bytes = chs * 4;
1623 if (cap->channels != chs)
1624 continue;
1625 if (cap->spk_mask & ~valid_mask)
1626 continue;
1627 if (size < 8)
1628 return -ENOMEM;
1629 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1630 put_user(chs_bytes, dst + 1))
1631 return -EFAULT;
1632 dst += 2;
1633 size -= 8;
1634 count += 8;
1635 if (size < chs_bytes)
1636 return -ENOMEM;
1637 size -= chs_bytes;
1638 count += chs_bytes;
1639 for (c = 7; c >= 0; c--) {
1640 int spk = cap->speakers[c];
1641 if (!spk)
1642 continue;
1643 if (put_user(spk_to_chmap(spk), dst))
1644 return -EFAULT;
1645 dst++;
1646 }
1647 }
1648 }
1649 if (put_user(count, tlv + 1))
1650 return -EFAULT;
1651 return 0;
1652}
1653
1654static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1656{
1657 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1658 struct hda_codec *codec = info->private_data;
1659 struct hdmi_spec *spec = codec->spec;
1660 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001661 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001662 int i;
1663
1664 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1665 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1666 return 0;
1667}
1668
1669static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol)
1671{
1672 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1673 struct hda_codec *codec = info->private_data;
1674 struct hdmi_spec *spec = codec->spec;
1675 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001676 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001677 unsigned int ctl_idx;
1678 struct snd_pcm_substream *substream;
1679 unsigned char chmap[8];
1680 int i, ca, prepared = 0;
1681
1682 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1683 substream = snd_pcm_chmap_substream(info, ctl_idx);
1684 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001685 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001686 switch (substream->runtime->status->state) {
1687 case SNDRV_PCM_STATE_OPEN:
1688 case SNDRV_PCM_STATE_SETUP:
1689 break;
1690 case SNDRV_PCM_STATE_PREPARED:
1691 prepared = 1;
1692 break;
1693 default:
1694 return -EBUSY;
1695 }
1696 memset(chmap, 0, sizeof(chmap));
1697 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1698 chmap[i] = ucontrol->value.integer.value[i];
1699 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1700 return 0;
1701 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1702 if (ca < 0)
1703 return -EINVAL;
1704 per_pin->chmap_set = true;
1705 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1706 if (prepared)
1707 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1708 substream);
1709
1710 return 0;
1711}
1712
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001713static int generic_hdmi_build_pcms(struct hda_codec *codec)
1714{
1715 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001716 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001717
Stephen Warren384a48d2011-06-01 11:14:21 -06001718 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1719 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001720 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001721 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001722
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001723 per_pin = get_pin(spec, pin_idx);
1724 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1725 info = snd_array_new(&spec->pcm_rec);
1726 if (!info)
1727 return -ENOMEM;
1728 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001729 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001730 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001731
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001732 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001733 pstr->substreams = 1;
1734 pstr->ops = generic_ops;
1735 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001736 }
1737
Stephen Warren384a48d2011-06-01 11:14:21 -06001738 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001739 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001740
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001741 return 0;
1742}
1743
David Henningsson0b6c49b2011-08-23 16:56:03 +02001744static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1745{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001746 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001747 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001748 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1749 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001750
Takashi Iwai31ef2252011-12-01 17:41:36 +01001751 if (pcmdev > 0)
1752 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001753 if (!is_jack_detectable(codec, per_pin->pin_nid))
1754 strncat(hdmi_str, " Phantom",
1755 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001756
Takashi Iwai31ef2252011-12-01 17:41:36 +01001757 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001758}
1759
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001760static int generic_hdmi_build_controls(struct hda_codec *codec)
1761{
1762 struct hdmi_spec *spec = codec->spec;
1763 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001764 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001765
Stephen Warren384a48d2011-06-01 11:14:21 -06001766 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001767 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001768
1769 err = generic_hdmi_build_jack(codec, pin_idx);
1770 if (err < 0)
1771 return err;
1772
Takashi Iwaidcda5802012-10-12 17:24:51 +02001773 err = snd_hda_create_dig_out_ctls(codec,
1774 per_pin->pin_nid,
1775 per_pin->mux_nids[0],
1776 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001777 if (err < 0)
1778 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001779 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001780
1781 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001782 err = hdmi_create_eld_ctl(codec, pin_idx,
1783 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001784
1785 if (err < 0)
1786 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001787
Takashi Iwai82b1d732011-12-20 15:53:07 +01001788 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001789 }
1790
Takashi Iwaid45e6882012-07-31 11:36:00 +02001791 /* add channel maps */
1792 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1793 struct snd_pcm_chmap *chmap;
1794 struct snd_kcontrol *kctl;
1795 int i;
1796 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1797 SNDRV_PCM_STREAM_PLAYBACK,
1798 NULL, 0, pin_idx, &chmap);
1799 if (err < 0)
1800 return err;
1801 /* override handlers */
1802 chmap->private_data = codec;
1803 kctl = chmap->kctl;
1804 for (i = 0; i < kctl->count; i++)
1805 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1806 kctl->info = hdmi_chmap_ctl_info;
1807 kctl->get = hdmi_chmap_ctl_get;
1808 kctl->put = hdmi_chmap_ctl_put;
1809 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1810 }
1811
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001812 return 0;
1813}
1814
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02001815static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1816{
1817 struct hdmi_spec *spec = codec->spec;
1818 int pin_idx;
1819
1820 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001821 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02001822 struct hdmi_eld *eld = &per_pin->sink_eld;
1823
1824 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001825 mutex_init(&eld->lock);
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02001826 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1827 snd_hda_eld_proc_new(codec, eld, pin_idx);
1828 }
1829 return 0;
1830}
1831
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001832static int generic_hdmi_init(struct hda_codec *codec)
1833{
1834 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001835 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001836
Stephen Warren384a48d2011-06-01 11:14:21 -06001837 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001838 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001839 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001840
1841 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001842 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001843 }
1844 return 0;
1845}
1846
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001847static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1848{
1849 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1850 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1851 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1852}
1853
1854static void hdmi_array_free(struct hdmi_spec *spec)
1855{
1856 snd_array_free(&spec->pins);
1857 snd_array_free(&spec->cvts);
1858 snd_array_free(&spec->pcm_rec);
1859}
1860
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001861static void generic_hdmi_free(struct hda_codec *codec)
1862{
1863 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001864 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001865
Stephen Warren384a48d2011-06-01 11:14:21 -06001866 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001867 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001868 struct hdmi_eld *eld = &per_pin->sink_eld;
1869
Wu Fengguang744626d2011-11-16 16:29:47 +08001870 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001871 snd_hda_eld_proc_free(codec, eld);
1872 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001873
Wu Fengguang744626d2011-11-16 16:29:47 +08001874 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001875 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001876 kfree(spec);
1877}
1878
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001879#ifdef CONFIG_PM
1880static int generic_hdmi_resume(struct hda_codec *codec)
1881{
1882 struct hdmi_spec *spec = codec->spec;
1883 int pin_idx;
1884
1885 generic_hdmi_init(codec);
1886 snd_hda_codec_resume_amp(codec);
1887 snd_hda_codec_resume_cache(codec);
1888
1889 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1890 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1891 hdmi_present_sense(per_pin, 1);
1892 }
1893 return 0;
1894}
1895#endif
1896
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001897static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001898 .init = generic_hdmi_init,
1899 .free = generic_hdmi_free,
1900 .build_pcms = generic_hdmi_build_pcms,
1901 .build_controls = generic_hdmi_build_controls,
1902 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001903#ifdef CONFIG_PM
1904 .resume = generic_hdmi_resume,
1905#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001906};
1907
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001908
1909static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1910 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001911{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001912 struct hdmi_spec *spec = codec->spec;
1913 hda_nid_t conns[4];
1914 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001915
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001916 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1917 if (nconns == spec->num_cvts &&
1918 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001919 return;
1920
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001921 /* override pins connection list */
1922 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1923 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001924}
1925
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001926#define INTEL_VENDOR_NID 0x08
1927#define INTEL_GET_VENDOR_VERB 0xf81
1928#define INTEL_SET_VENDOR_VERB 0x781
1929#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1930#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1931
1932static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001933 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001934{
1935 unsigned int vendor_param;
1936
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001937 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1938 INTEL_GET_VENDOR_VERB, 0);
1939 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1940 return;
1941
1942 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1943 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1944 INTEL_SET_VENDOR_VERB, vendor_param);
1945 if (vendor_param == -1)
1946 return;
1947
Takashi Iwai17df3f52013-05-08 08:09:34 +02001948 if (update_tree)
1949 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001950}
1951
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001952static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1953{
1954 unsigned int vendor_param;
1955
1956 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1957 INTEL_GET_VENDOR_VERB, 0);
1958 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1959 return;
1960
1961 /* enable DP1.2 mode */
1962 vendor_param |= INTEL_EN_DP12;
1963 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1964 INTEL_SET_VENDOR_VERB, vendor_param);
1965}
1966
Takashi Iwai17df3f52013-05-08 08:09:34 +02001967/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1968 * Otherwise you may get severe h/w communication errors.
1969 */
1970static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1971 unsigned int power_state)
1972{
1973 if (power_state == AC_PWRST_D0) {
1974 intel_haswell_enable_all_pins(codec, false);
1975 intel_haswell_fixup_enable_dp12(codec);
1976 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001977
Takashi Iwai17df3f52013-05-08 08:09:34 +02001978 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1979 snd_hda_codec_set_power_to_all(codec, fg, power_state);
1980}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001981
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001982static int patch_generic_hdmi(struct hda_codec *codec)
1983{
1984 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001985
1986 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1987 if (spec == NULL)
1988 return -ENOMEM;
1989
1990 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001991 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001992
Takashi Iwai17df3f52013-05-08 08:09:34 +02001993 if (codec->vendor_id == 0x80862807) {
1994 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001995 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02001996 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001997
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001998 if (hdmi_parse_codec(codec) < 0) {
1999 codec->spec = NULL;
2000 kfree(spec);
2001 return -EINVAL;
2002 }
2003 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002004 if (codec->vendor_id == 0x80862807) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002005 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002006 codec->dp_mst = true;
2007 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002008
Takashi Iwai8b8d654b2012-06-20 16:32:22 +02002009 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002010
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002011 init_channel_allocations();
2012
2013 return 0;
2014}
2015
2016/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002017 * Shared non-generic implementations
2018 */
2019
2020static int simple_playback_build_pcms(struct hda_codec *codec)
2021{
2022 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002023 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002024 unsigned int chans;
2025 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002026 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002027
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002028 per_cvt = get_cvt(spec, 0);
2029 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002030 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002031
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002032 info = snd_array_new(&spec->pcm_rec);
2033 if (!info)
2034 return -ENOMEM;
2035 info->name = get_pin(spec, 0)->pcm_name;
2036 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002037 info->pcm_type = HDA_PCM_TYPE_HDMI;
2038 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2039 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002040 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002041 if (pstr->channels_max <= 2 && chans && chans <= 16)
2042 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002043
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002044 codec->num_pcms = 1;
2045 codec->pcm_info = info;
2046
Stephen Warren3aaf8982011-06-01 11:14:19 -06002047 return 0;
2048}
2049
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002050/* unsolicited event for jack sensing */
2051static void simple_hdmi_unsol_event(struct hda_codec *codec,
2052 unsigned int res)
2053{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002054 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002055 snd_hda_jack_report_sync(codec);
2056}
2057
2058/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2059 * as long as spec->pins[] is set correctly
2060 */
2061#define simple_hdmi_build_jack generic_hdmi_build_jack
2062
Stephen Warren3aaf8982011-06-01 11:14:19 -06002063static int simple_playback_build_controls(struct hda_codec *codec)
2064{
2065 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002066 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002067 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002068
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002069 per_cvt = get_cvt(spec, 0);
2070 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2071 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002072 if (err < 0)
2073 return err;
2074 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002075}
2076
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002077static int simple_playback_init(struct hda_codec *codec)
2078{
2079 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002080 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2081 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002082
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002083 snd_hda_codec_write(codec, pin, 0,
2084 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2085 /* some codecs require to unmute the pin */
2086 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2087 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2088 AMP_OUT_UNMUTE);
2089 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002090 return 0;
2091}
2092
Stephen Warren3aaf8982011-06-01 11:14:19 -06002093static void simple_playback_free(struct hda_codec *codec)
2094{
2095 struct hdmi_spec *spec = codec->spec;
2096
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002097 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002098 kfree(spec);
2099}
2100
2101/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002102 * Nvidia specific implementations
2103 */
2104
2105#define Nv_VERB_SET_Channel_Allocation 0xF79
2106#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2107#define Nv_VERB_SET_Audio_Protection_On 0xF98
2108#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2109
2110#define nvhdmi_master_con_nid_7x 0x04
2111#define nvhdmi_master_pin_nid_7x 0x05
2112
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002113static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002114 /*front, rear, clfe, rear_surr */
2115 0x6, 0x8, 0xa, 0xc,
2116};
2117
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002118static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2119 /* set audio protect on */
2120 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2121 /* enable digital output on pin widget */
2122 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2123 {} /* terminator */
2124};
2125
2126static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002127 /* set audio protect on */
2128 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2129 /* enable digital output on pin widget */
2130 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2131 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2132 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2133 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2134 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2135 {} /* terminator */
2136};
2137
2138#ifdef LIMITED_RATE_FMT_SUPPORT
2139/* support only the safe format and rate */
2140#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2141#define SUPPORTED_MAXBPS 16
2142#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2143#else
2144/* support all rates and formats */
2145#define SUPPORTED_RATES \
2146 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2147 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2148 SNDRV_PCM_RATE_192000)
2149#define SUPPORTED_MAXBPS 24
2150#define SUPPORTED_FORMATS \
2151 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2152#endif
2153
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002154static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002155{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002156 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2157 return 0;
2158}
2159
2160static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2161{
2162 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002163 return 0;
2164}
2165
Nitin Daga393004b2011-01-10 21:49:31 +05302166static unsigned int channels_2_6_8[] = {
2167 2, 6, 8
2168};
2169
2170static unsigned int channels_2_8[] = {
2171 2, 8
2172};
2173
2174static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2175 .count = ARRAY_SIZE(channels_2_6_8),
2176 .list = channels_2_6_8,
2177 .mask = 0,
2178};
2179
2180static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2181 .count = ARRAY_SIZE(channels_2_8),
2182 .list = channels_2_8,
2183 .mask = 0,
2184};
2185
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002186static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2187 struct hda_codec *codec,
2188 struct snd_pcm_substream *substream)
2189{
2190 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302191 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2192
2193 switch (codec->preset->id) {
2194 case 0x10de0002:
2195 case 0x10de0003:
2196 case 0x10de0005:
2197 case 0x10de0006:
2198 hw_constraints_channels = &hw_constraints_2_8_channels;
2199 break;
2200 case 0x10de0007:
2201 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2202 break;
2203 default:
2204 break;
2205 }
2206
2207 if (hw_constraints_channels != NULL) {
2208 snd_pcm_hw_constraint_list(substream->runtime, 0,
2209 SNDRV_PCM_HW_PARAM_CHANNELS,
2210 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002211 } else {
2212 snd_pcm_hw_constraint_step(substream->runtime, 0,
2213 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302214 }
2215
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002216 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2217}
2218
2219static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2220 struct hda_codec *codec,
2221 struct snd_pcm_substream *substream)
2222{
2223 struct hdmi_spec *spec = codec->spec;
2224 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2225}
2226
2227static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2228 struct hda_codec *codec,
2229 unsigned int stream_tag,
2230 unsigned int format,
2231 struct snd_pcm_substream *substream)
2232{
2233 struct hdmi_spec *spec = codec->spec;
2234 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2235 stream_tag, format, substream);
2236}
2237
Takashi Iwaid0b12522012-06-15 14:34:42 +02002238static const struct hda_pcm_stream simple_pcm_playback = {
2239 .substreams = 1,
2240 .channels_min = 2,
2241 .channels_max = 2,
2242 .ops = {
2243 .open = simple_playback_pcm_open,
2244 .close = simple_playback_pcm_close,
2245 .prepare = simple_playback_pcm_prepare
2246 },
2247};
2248
2249static const struct hda_codec_ops simple_hdmi_patch_ops = {
2250 .build_controls = simple_playback_build_controls,
2251 .build_pcms = simple_playback_build_pcms,
2252 .init = simple_playback_init,
2253 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002254 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002255};
2256
2257static int patch_simple_hdmi(struct hda_codec *codec,
2258 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2259{
2260 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002261 struct hdmi_spec_per_cvt *per_cvt;
2262 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002263
2264 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2265 if (!spec)
2266 return -ENOMEM;
2267
2268 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002269 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002270
2271 spec->multiout.num_dacs = 0; /* no analog */
2272 spec->multiout.max_channels = 2;
2273 spec->multiout.dig_out_nid = cvt_nid;
2274 spec->num_cvts = 1;
2275 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002276 per_pin = snd_array_new(&spec->pins);
2277 per_cvt = snd_array_new(&spec->cvts);
2278 if (!per_pin || !per_cvt) {
2279 simple_playback_free(codec);
2280 return -ENOMEM;
2281 }
2282 per_cvt->cvt_nid = cvt_nid;
2283 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002284 spec->pcm_playback = simple_pcm_playback;
2285
2286 codec->patch_ops = simple_hdmi_patch_ops;
2287
2288 return 0;
2289}
2290
Aaron Plattner1f348522011-04-06 17:19:04 -07002291static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2292 int channels)
2293{
2294 unsigned int chanmask;
2295 int chan = channels ? (channels - 1) : 1;
2296
2297 switch (channels) {
2298 default:
2299 case 0:
2300 case 2:
2301 chanmask = 0x00;
2302 break;
2303 case 4:
2304 chanmask = 0x08;
2305 break;
2306 case 6:
2307 chanmask = 0x0b;
2308 break;
2309 case 8:
2310 chanmask = 0x13;
2311 break;
2312 }
2313
2314 /* Set the audio infoframe channel allocation and checksum fields. The
2315 * channel count is computed implicitly by the hardware. */
2316 snd_hda_codec_write(codec, 0x1, 0,
2317 Nv_VERB_SET_Channel_Allocation, chanmask);
2318
2319 snd_hda_codec_write(codec, 0x1, 0,
2320 Nv_VERB_SET_Info_Frame_Checksum,
2321 (0x71 - chan - chanmask));
2322}
2323
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002324static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2325 struct hda_codec *codec,
2326 struct snd_pcm_substream *substream)
2327{
2328 struct hdmi_spec *spec = codec->spec;
2329 int i;
2330
2331 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2332 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2333 for (i = 0; i < 4; i++) {
2334 /* set the stream id */
2335 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2336 AC_VERB_SET_CHANNEL_STREAMID, 0);
2337 /* set the stream format */
2338 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2339 AC_VERB_SET_STREAM_FORMAT, 0);
2340 }
2341
Aaron Plattner1f348522011-04-06 17:19:04 -07002342 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2343 * streams are disabled. */
2344 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2345
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002346 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2347}
2348
2349static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2350 struct hda_codec *codec,
2351 unsigned int stream_tag,
2352 unsigned int format,
2353 struct snd_pcm_substream *substream)
2354{
2355 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002356 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002357 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002358 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002359 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002360 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002361
2362 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002363 per_cvt = get_cvt(spec, 0);
2364 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002365
2366 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002367
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002368 dataDCC2 = 0x2;
2369
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002370 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002371 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002372 snd_hda_codec_write(codec,
2373 nvhdmi_master_con_nid_7x,
2374 0,
2375 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002376 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002377
2378 /* set the stream id */
2379 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2380 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2381
2382 /* set the stream format */
2383 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2384 AC_VERB_SET_STREAM_FORMAT, format);
2385
2386 /* turn on again (if needed) */
2387 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002388 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002389 snd_hda_codec_write(codec,
2390 nvhdmi_master_con_nid_7x,
2391 0,
2392 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002393 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002394 snd_hda_codec_write(codec,
2395 nvhdmi_master_con_nid_7x,
2396 0,
2397 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2398 }
2399
2400 for (i = 0; i < 4; i++) {
2401 if (chs == 2)
2402 channel_id = 0;
2403 else
2404 channel_id = i * 2;
2405
2406 /* turn off SPDIF once;
2407 *otherwise the IEC958 bits won't be updated
2408 */
2409 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002410 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002411 snd_hda_codec_write(codec,
2412 nvhdmi_con_nids_7x[i],
2413 0,
2414 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002415 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002416 /* set the stream id */
2417 snd_hda_codec_write(codec,
2418 nvhdmi_con_nids_7x[i],
2419 0,
2420 AC_VERB_SET_CHANNEL_STREAMID,
2421 (stream_tag << 4) | channel_id);
2422 /* set the stream format */
2423 snd_hda_codec_write(codec,
2424 nvhdmi_con_nids_7x[i],
2425 0,
2426 AC_VERB_SET_STREAM_FORMAT,
2427 format);
2428 /* turn on again (if needed) */
2429 /* enable and set the channel status audio/data flag */
2430 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002431 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002432 snd_hda_codec_write(codec,
2433 nvhdmi_con_nids_7x[i],
2434 0,
2435 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002436 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002437 snd_hda_codec_write(codec,
2438 nvhdmi_con_nids_7x[i],
2439 0,
2440 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2441 }
2442 }
2443
Aaron Plattner1f348522011-04-06 17:19:04 -07002444 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002445
2446 mutex_unlock(&codec->spdif_mutex);
2447 return 0;
2448}
2449
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002450static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002451 .substreams = 1,
2452 .channels_min = 2,
2453 .channels_max = 8,
2454 .nid = nvhdmi_master_con_nid_7x,
2455 .rates = SUPPORTED_RATES,
2456 .maxbps = SUPPORTED_MAXBPS,
2457 .formats = SUPPORTED_FORMATS,
2458 .ops = {
2459 .open = simple_playback_pcm_open,
2460 .close = nvhdmi_8ch_7x_pcm_close,
2461 .prepare = nvhdmi_8ch_7x_pcm_prepare
2462 },
2463};
2464
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002465static int patch_nvhdmi_2ch(struct hda_codec *codec)
2466{
2467 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002468 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2469 nvhdmi_master_pin_nid_7x);
2470 if (err < 0)
2471 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002472
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002473 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002474 /* override the PCM rates, etc, as the codec doesn't give full list */
2475 spec = codec->spec;
2476 spec->pcm_playback.rates = SUPPORTED_RATES;
2477 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2478 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002479 return 0;
2480}
2481
Takashi Iwai53775b02012-08-01 12:17:41 +02002482static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2483{
2484 struct hdmi_spec *spec = codec->spec;
2485 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002486 if (!err) {
2487 struct hda_pcm *info = get_pcm_rec(spec, 0);
2488 info->own_chmap = true;
2489 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002490 return err;
2491}
2492
2493static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2494{
2495 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002496 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002497 struct snd_pcm_chmap *chmap;
2498 int err;
2499
2500 err = simple_playback_build_controls(codec);
2501 if (err < 0)
2502 return err;
2503
2504 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002505 info = get_pcm_rec(spec, 0);
2506 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002507 SNDRV_PCM_STREAM_PLAYBACK,
2508 snd_pcm_alt_chmaps, 8, 0, &chmap);
2509 if (err < 0)
2510 return err;
2511 switch (codec->preset->id) {
2512 case 0x10de0002:
2513 case 0x10de0003:
2514 case 0x10de0005:
2515 case 0x10de0006:
2516 chmap->channel_mask = (1U << 2) | (1U << 8);
2517 break;
2518 case 0x10de0007:
2519 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2520 }
2521 return 0;
2522}
2523
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002524static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2525{
2526 struct hdmi_spec *spec;
2527 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002528 if (err < 0)
2529 return err;
2530 spec = codec->spec;
2531 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002532 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002533 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002534 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2535 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002536
2537 /* Initialize the audio infoframe channel mask and checksum to something
2538 * valid */
2539 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2540
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002541 return 0;
2542}
2543
2544/*
2545 * ATI-specific implementations
2546 *
2547 * FIXME: we may omit the whole this and use the generic code once after
2548 * it's confirmed to work.
2549 */
2550
2551#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2552#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2553
2554static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2555 struct hda_codec *codec,
2556 unsigned int stream_tag,
2557 unsigned int format,
2558 struct snd_pcm_substream *substream)
2559{
2560 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002561 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002562 int chans = substream->runtime->channels;
2563 int i, err;
2564
2565 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2566 substream);
2567 if (err < 0)
2568 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002569 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002570 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002571 /* FIXME: XXX */
2572 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002573 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002574 AC_VERB_SET_HDMI_CHAN_SLOT,
2575 (i << 4) | i);
2576 }
2577 return 0;
2578}
2579
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002580static int patch_atihdmi(struct hda_codec *codec)
2581{
2582 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002583 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2584 if (err < 0)
2585 return err;
2586 spec = codec->spec;
2587 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002588 return 0;
2589}
2590
Annie Liu3de5ff82012-06-08 19:18:42 +08002591/* VIA HDMI Implementation */
2592#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2593#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2594
Annie Liu3de5ff82012-06-08 19:18:42 +08002595static int patch_via_hdmi(struct hda_codec *codec)
2596{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002597 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002598}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002599
2600/*
2601 * patch entries
2602 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002603static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002604{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2605{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2606{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002607{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002608{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2609{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2610{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2611{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2612{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2613{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2614{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2615{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002616{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2617{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2618{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2619{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2620{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2621{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2622{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2623{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2624{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2625{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2626{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002627/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002628{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2629{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2630{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2631{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2632{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2633{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2634{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2635{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2636{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2637{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002638{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002639{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002640{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2641{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002642{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2643{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2644{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2645{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002646{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2647{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2648{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2649{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2650{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2651{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e610d2011-05-20 15:35:43 +08002652{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002653{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002654{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002655{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2656{} /* terminator */
2657};
2658
2659MODULE_ALIAS("snd-hda-codec-id:1002793c");
2660MODULE_ALIAS("snd-hda-codec-id:10027919");
2661MODULE_ALIAS("snd-hda-codec-id:1002791a");
2662MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2663MODULE_ALIAS("snd-hda-codec-id:10951390");
2664MODULE_ALIAS("snd-hda-codec-id:10951392");
2665MODULE_ALIAS("snd-hda-codec-id:10de0002");
2666MODULE_ALIAS("snd-hda-codec-id:10de0003");
2667MODULE_ALIAS("snd-hda-codec-id:10de0005");
2668MODULE_ALIAS("snd-hda-codec-id:10de0006");
2669MODULE_ALIAS("snd-hda-codec-id:10de0007");
2670MODULE_ALIAS("snd-hda-codec-id:10de000a");
2671MODULE_ALIAS("snd-hda-codec-id:10de000b");
2672MODULE_ALIAS("snd-hda-codec-id:10de000c");
2673MODULE_ALIAS("snd-hda-codec-id:10de000d");
2674MODULE_ALIAS("snd-hda-codec-id:10de0010");
2675MODULE_ALIAS("snd-hda-codec-id:10de0011");
2676MODULE_ALIAS("snd-hda-codec-id:10de0012");
2677MODULE_ALIAS("snd-hda-codec-id:10de0013");
2678MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002679MODULE_ALIAS("snd-hda-codec-id:10de0015");
2680MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002681MODULE_ALIAS("snd-hda-codec-id:10de0018");
2682MODULE_ALIAS("snd-hda-codec-id:10de0019");
2683MODULE_ALIAS("snd-hda-codec-id:10de001a");
2684MODULE_ALIAS("snd-hda-codec-id:10de001b");
2685MODULE_ALIAS("snd-hda-codec-id:10de001c");
2686MODULE_ALIAS("snd-hda-codec-id:10de0040");
2687MODULE_ALIAS("snd-hda-codec-id:10de0041");
2688MODULE_ALIAS("snd-hda-codec-id:10de0042");
2689MODULE_ALIAS("snd-hda-codec-id:10de0043");
2690MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002691MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002692MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002693MODULE_ALIAS("snd-hda-codec-id:10de0067");
2694MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002695MODULE_ALIAS("snd-hda-codec-id:11069f80");
2696MODULE_ALIAS("snd-hda-codec-id:11069f81");
2697MODULE_ALIAS("snd-hda-codec-id:11069f84");
2698MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002699MODULE_ALIAS("snd-hda-codec-id:17e80047");
2700MODULE_ALIAS("snd-hda-codec-id:80860054");
2701MODULE_ALIAS("snd-hda-codec-id:80862801");
2702MODULE_ALIAS("snd-hda-codec-id:80862802");
2703MODULE_ALIAS("snd-hda-codec-id:80862803");
2704MODULE_ALIAS("snd-hda-codec-id:80862804");
2705MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e610d2011-05-20 15:35:43 +08002706MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002707MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002708MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002709MODULE_ALIAS("snd-hda-codec-id:808629fb");
2710
2711MODULE_LICENSE("GPL");
2712MODULE_DESCRIPTION("HDMI HD-audio codec");
2713MODULE_ALIAS("snd-hda-codec-intelhdmi");
2714MODULE_ALIAS("snd-hda-codec-nvhdmi");
2715MODULE_ALIAS("snd-hda-codec-atihdmi");
2716
2717static struct hda_codec_preset_list intel_list = {
2718 .preset = snd_hda_preset_hdmi,
2719 .owner = THIS_MODULE,
2720};
2721
2722static int __init patch_hdmi_init(void)
2723{
2724 return snd_hda_add_codec_preset(&intel_list);
2725}
2726
2727static void __exit patch_hdmi_exit(void)
2728{
2729 snd_hda_delete_codec_preset(&intel_list);
2730}
2731
2732module_init(patch_hdmi_init)
2733module_exit(patch_hdmi_exit)