blob: 00e0413339040c896951d0ffcd96cb0459124921 [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Mengdong Linfb87fa32013-09-04 16:36:57 -040047#define is_haswell(codec) ((codec)->vendor_id == 0x80862807)
48
Stephen Warren384a48d2011-06-01 11:14:21 -060049struct hdmi_spec_per_cvt {
50 hda_nid_t cvt_nid;
51 int assigned;
52 unsigned int channels_min;
53 unsigned int channels_max;
54 u32 rates;
55 u64 formats;
56 unsigned int maxbps;
57};
58
Takashi Iwai4eea3092013-02-07 18:18:19 +010059/* max. connections to a widget */
60#define HDA_MAX_CONNECTIONS 32
61
Stephen Warren384a48d2011-06-01 11:14:21 -060062struct hdmi_spec_per_pin {
63 hda_nid_t pin_nid;
64 int num_mux_nids;
65 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Anssi Hannula1df5a062013-10-05 02:25:40 +030066 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080067
68 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060069 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080070 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010071 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060072 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020073 bool setup; /* the stream has been set up by prepare callback */
74 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020075 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020076 bool chmap_set; /* channel-map override by ALSA API? */
77 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010078 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060079};
80
Wu Fengguang079d88c2010-03-08 10:44:23 +080081struct hdmi_spec {
82 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010083 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
84 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060085
Wu Fengguang079d88c2010-03-08 10:44:23 +080086 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010087 struct snd_array pins; /* struct hdmi_spec_per_pin */
88 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020089 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080090
David Henningsson4bd038f2013-02-19 16:11:25 +010091 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080092 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060093 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080094 */
95 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020096 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080097};
98
99
100struct hdmi_audio_infoframe {
101 u8 type; /* 0x84 */
102 u8 ver; /* 0x01 */
103 u8 len; /* 0x0a */
104
Wu Fengguang53d7d692010-09-21 14:25:49 +0800105 u8 checksum;
106
Wu Fengguang079d88c2010-03-08 10:44:23 +0800107 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
108 u8 SS01_SF24;
109 u8 CXT04;
110 u8 CA;
111 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800112};
113
114struct dp_audio_infoframe {
115 u8 type; /* 0x84 */
116 u8 len; /* 0x1b */
117 u8 ver; /* 0x11 << 2 */
118
119 u8 CC02_CT47; /* match with HDMI infoframe from this on */
120 u8 SS01_SF24;
121 u8 CXT04;
122 u8 CA;
123 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800124};
125
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100126union audio_infoframe {
127 struct hdmi_audio_infoframe hdmi;
128 struct dp_audio_infoframe dp;
129 u8 bytes[0];
130};
131
Wu Fengguang079d88c2010-03-08 10:44:23 +0800132/*
133 * CEA speaker placement:
134 *
135 * FLH FCH FRH
136 * FLW FL FLC FC FRC FR FRW
137 *
138 * LFE
139 * TC
140 *
141 * RL RLC RC RRC RR
142 *
143 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
144 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
145 */
146enum cea_speaker_placement {
147 FL = (1 << 0), /* Front Left */
148 FC = (1 << 1), /* Front Center */
149 FR = (1 << 2), /* Front Right */
150 FLC = (1 << 3), /* Front Left Center */
151 FRC = (1 << 4), /* Front Right Center */
152 RL = (1 << 5), /* Rear Left */
153 RC = (1 << 6), /* Rear Center */
154 RR = (1 << 7), /* Rear Right */
155 RLC = (1 << 8), /* Rear Left Center */
156 RRC = (1 << 9), /* Rear Right Center */
157 LFE = (1 << 10), /* Low Frequency Effect */
158 FLW = (1 << 11), /* Front Left Wide */
159 FRW = (1 << 12), /* Front Right Wide */
160 FLH = (1 << 13), /* Front Left High */
161 FCH = (1 << 14), /* Front Center High */
162 FRH = (1 << 15), /* Front Right High */
163 TC = (1 << 16), /* Top Center */
164};
165
166/*
167 * ELD SA bits in the CEA Speaker Allocation data block
168 */
169static int eld_speaker_allocation_bits[] = {
170 [0] = FL | FR,
171 [1] = LFE,
172 [2] = FC,
173 [3] = RL | RR,
174 [4] = RC,
175 [5] = FLC | FRC,
176 [6] = RLC | RRC,
177 /* the following are not defined in ELD yet */
178 [7] = FLW | FRW,
179 [8] = FLH | FRH,
180 [9] = TC,
181 [10] = FCH,
182};
183
184struct cea_channel_speaker_allocation {
185 int ca_index;
186 int speakers[8];
187
188 /* derived values, just for convenience */
189 int channels;
190 int spk_mask;
191};
192
193/*
194 * ALSA sequence is:
195 *
196 * surround40 surround41 surround50 surround51 surround71
197 * ch0 front left = = = =
198 * ch1 front right = = = =
199 * ch2 rear left = = = =
200 * ch3 rear right = = = =
201 * ch4 LFE center center center
202 * ch5 LFE LFE
203 * ch6 side left
204 * ch7 side right
205 *
206 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
207 */
208static int hdmi_channel_mapping[0x32][8] = {
209 /* stereo */
210 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211 /* 2.1 */
212 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* Dolby Surround */
214 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
215 /* surround40 */
216 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
217 /* 4ch */
218 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
219 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800220 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800221 /* surround50 */
222 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
223 /* surround51 */
224 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
225 /* 7.1 */
226 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
227};
228
229/*
230 * This is an ordered list!
231 *
232 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800233 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800234 */
235static struct cea_channel_speaker_allocation channel_allocations[] = {
236/* channel: 7 6 5 4 3 2 1 0 */
237{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
238 /* 2.1 */
239{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
240 /* Dolby Surround */
241{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
242 /* surround40 */
243{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
244 /* surround41 */
245{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
246 /* surround50 */
247{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
248 /* surround51 */
249{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
250 /* 6.1 */
251{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
252 /* surround71 */
253{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
254
255{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
256{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
257{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
258{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
259{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
260{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
261{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
262{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
263{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
264{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
265{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
266{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
267{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
268{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
269{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
270{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
271{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
272{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
273{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
274{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
275{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
276{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
277{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
278{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
283{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
284{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
285{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
286{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
287{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
288{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
294{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
295{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
296};
297
298
299/*
300 * HDMI routines
301 */
302
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100303#define get_pin(spec, idx) \
304 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
305#define get_cvt(spec, idx) \
306 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
307#define get_pcm_rec(spec, idx) \
308 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
309
Stephen Warren384a48d2011-06-01 11:14:21 -0600310static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800311{
Stephen Warren384a48d2011-06-01 11:14:21 -0600312 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800313
Stephen Warren384a48d2011-06-01 11:14:21 -0600314 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100315 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600316 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800317
Stephen Warren384a48d2011-06-01 11:14:21 -0600318 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
319 return -EINVAL;
320}
321
322static int hinfo_to_pin_index(struct hdmi_spec *spec,
323 struct hda_pcm_stream *hinfo)
324{
325 int pin_idx;
326
327 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100328 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600329 return pin_idx;
330
331 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
332 return -EINVAL;
333}
334
335static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
336{
337 int cvt_idx;
338
339 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100340 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600341 return cvt_idx;
342
343 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800344 return -EINVAL;
345}
346
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500347static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_info *uinfo)
349{
350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100351 struct hdmi_spec *spec = codec->spec;
352 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500353 int pin_idx;
354
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500355 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
356
357 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100358 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100359
David Henningsson4bd038f2013-02-19 16:11:25 +0100360 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100361 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100362 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500363
364 return 0;
365}
366
367static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
369{
370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100371 struct hdmi_spec *spec = codec->spec;
372 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500373 int pin_idx;
374
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500375 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100376 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500377
David Henningsson4bd038f2013-02-19 16:11:25 +0100378 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100379 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100380 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100381 snd_BUG();
382 return -EINVAL;
383 }
384
385 memset(ucontrol->value.bytes.data, 0,
386 ARRAY_SIZE(ucontrol->value.bytes.data));
387 if (eld->eld_valid)
388 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
389 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100390 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500391
392 return 0;
393}
394
395static struct snd_kcontrol_new eld_bytes_ctl = {
396 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = "ELD",
399 .info = hdmi_eld_ctl_info,
400 .get = hdmi_eld_ctl_get,
401};
402
403static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
404 int device)
405{
406 struct snd_kcontrol *kctl;
407 struct hdmi_spec *spec = codec->spec;
408 int err;
409
410 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
411 if (!kctl)
412 return -ENOMEM;
413 kctl->private_value = pin_idx;
414 kctl->id.device = device;
415
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100416 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500417 if (err < 0)
418 return err;
419
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100420 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500421 return 0;
422}
423
Wu Fengguang079d88c2010-03-08 10:44:23 +0800424#ifdef BE_PARANOID
425static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426 int *packet_index, int *byte_index)
427{
428 int val;
429
430 val = snd_hda_codec_read(codec, pin_nid, 0,
431 AC_VERB_GET_HDMI_DIP_INDEX, 0);
432
433 *packet_index = val >> 5;
434 *byte_index = val & 0x1f;
435}
436#endif
437
438static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439 int packet_index, int byte_index)
440{
441 int val;
442
443 val = (packet_index << 5) | (byte_index & 0x1f);
444
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
446}
447
448static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
449 unsigned char val)
450{
451 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
452}
453
Stephen Warren384a48d2011-06-01 11:14:21 -0600454static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800455{
456 /* Unmute */
457 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
458 snd_hda_codec_write(codec, pin_nid, 0,
459 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100460 /* Enable pin out: some machines with GM965 gets broken output when
461 * the pin is disabled or changed while using with HDMI
462 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800463 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100464 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800465}
466
Stephen Warren384a48d2011-06-01 11:14:21 -0600467static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800468{
Stephen Warren384a48d2011-06-01 11:14:21 -0600469 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800470 AC_VERB_GET_CVT_CHAN_COUNT, 0);
471}
472
473static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600474 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800475{
Stephen Warren384a48d2011-06-01 11:14:21 -0600476 if (chs != hdmi_get_channel_count(codec, cvt_nid))
477 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800478 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
479}
480
481
482/*
483 * Channel mapping routines
484 */
485
486/*
487 * Compute derived values in channel_allocations[].
488 */
489static void init_channel_allocations(void)
490{
491 int i, j;
492 struct cea_channel_speaker_allocation *p;
493
494 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
495 p = channel_allocations + i;
496 p->channels = 0;
497 p->spk_mask = 0;
498 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
499 if (p->speakers[j]) {
500 p->channels++;
501 p->spk_mask |= p->speakers[j];
502 }
503 }
504}
505
Wang Xingchao72357c72012-09-06 10:02:36 +0800506static int get_channel_allocation_order(int ca)
507{
508 int i;
509
510 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
511 if (channel_allocations[i].ca_index == ca)
512 break;
513 }
514 return i;
515}
516
Wu Fengguang079d88c2010-03-08 10:44:23 +0800517/*
518 * The transformation takes two steps:
519 *
520 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
521 * spk_mask => (channel_allocations[]) => ai->CA
522 *
523 * TODO: it could select the wrong CA from multiple candidates.
524*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600525static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800526{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800528 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800529 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800530 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
531
532 /*
533 * CA defaults to 0 for basic stereo audio
534 */
535 if (channels <= 2)
536 return 0;
537
Wu Fengguang079d88c2010-03-08 10:44:23 +0800538 /*
539 * expand ELD's speaker allocation mask
540 *
541 * ELD tells the speaker mask in a compact(paired) form,
542 * expand ELD's notions to match the ones used by Audio InfoFrame.
543 */
544 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100545 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800546 spk_mask |= eld_speaker_allocation_bits[i];
547 }
548
549 /* search for the first working match in the CA table */
550 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
551 if (channels == channel_allocations[i].channels &&
552 (spk_mask & channel_allocations[i].spk_mask) ==
553 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800554 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800555 break;
556 }
557 }
558
Anssi Hannula18e39182013-09-01 14:36:47 +0300559 if (!ca) {
560 /* if there was no match, select the regular ALSA channel
561 * allocation with the matching number of channels */
562 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
563 if (channels == channel_allocations[i].channels) {
564 ca = channel_allocations[i].ca_index;
565 break;
566 }
567 }
568 }
569
David Henningsson1613d6b2013-02-19 16:11:24 +0100570 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800571 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800572 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800573
Wu Fengguang53d7d692010-09-21 14:25:49 +0800574 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575}
576
577static void hdmi_debug_channel_mapping(struct hda_codec *codec,
578 hda_nid_t pin_nid)
579{
580#ifdef CONFIG_SND_DEBUG_VERBOSE
581 int i;
582 int slot;
583
584 for (i = 0; i < 8; i++) {
585 slot = snd_hda_codec_read(codec, pin_nid, 0,
586 AC_VERB_GET_HDMI_CHAN_SLOT, i);
587 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
588 slot >> 4, slot & 0xf);
589 }
590#endif
591}
592
593
Takashi Iwaid45e6882012-07-31 11:36:00 +0200594static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800596 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800597 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800598{
Anssi Hannula90f28002013-10-05 02:25:39 +0300599 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800601 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800602 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800603 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800604
Wang Xingchao72357c72012-09-06 10:02:36 +0800605 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300606 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800607
Wu Fengguang079d88c2010-03-08 10:44:23 +0800608 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300609 int hdmi_slot = 0;
610 /* fill actual channel mappings in ALSA channel (i) order */
611 for (i = 0; i < ch_alloc->channels; i++) {
612 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
613 hdmi_slot++; /* skip zero slots */
614
615 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
616 }
617 /* fill the rest of the slots with ALSA channel 0xf */
618 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
619 if (!ch_alloc->speakers[7 - hdmi_slot])
620 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800621 }
622
Wang Xingchao433968d2012-09-06 10:02:37 +0800623 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300624 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300625 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800626 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300627 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800628 }
629
Wu Fengguang079d88c2010-03-08 10:44:23 +0800630 for (i = 0; i < 8; i++) {
631 err = snd_hda_codec_write(codec, pin_nid, 0,
632 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800633 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800634 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800635 snd_printdd(KERN_NOTICE
636 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800637 break;
638 }
639 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800640}
641
Takashi Iwaid45e6882012-07-31 11:36:00 +0200642struct channel_map_table {
643 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200644 int spk_mask; /* speaker position bit mask */
645};
646
647static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300648 { SNDRV_CHMAP_FL, FL },
649 { SNDRV_CHMAP_FR, FR },
650 { SNDRV_CHMAP_RL, RL },
651 { SNDRV_CHMAP_RR, RR },
652 { SNDRV_CHMAP_LFE, LFE },
653 { SNDRV_CHMAP_FC, FC },
654 { SNDRV_CHMAP_RLC, RLC },
655 { SNDRV_CHMAP_RRC, RRC },
656 { SNDRV_CHMAP_RC, RC },
657 { SNDRV_CHMAP_FLC, FLC },
658 { SNDRV_CHMAP_FRC, FRC },
659 { SNDRV_CHMAP_FLH, FLH },
660 { SNDRV_CHMAP_FRH, FRH },
661 { SNDRV_CHMAP_FLW, FLW },
662 { SNDRV_CHMAP_FRW, FRW },
663 { SNDRV_CHMAP_TC, TC },
664 { SNDRV_CHMAP_FCH, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200665 {} /* terminator */
666};
667
668/* from ALSA API channel position to speaker bit mask */
669static int to_spk_mask(unsigned char c)
670{
671 struct channel_map_table *t = map_tables;
672 for (; t->map; t++) {
673 if (t->map == c)
674 return t->spk_mask;
675 }
676 return 0;
677}
678
679/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300680static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200681{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300682 int mask = to_spk_mask(pos);
683 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200684
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300685 if (mask) {
686 for (i = 0; i < 8; i++) {
687 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
688 return i;
689 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200690 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300691
692 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200693}
694
695/* from speaker bit mask to ALSA API channel position */
696static int spk_to_chmap(int spk)
697{
698 struct channel_map_table *t = map_tables;
699 for (; t->map; t++) {
700 if (t->spk_mask == spk)
701 return t->map;
702 }
703 return 0;
704}
705
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300706/* from CEA slot to ALSA API channel position */
707static int from_cea_slot(int ordered_ca, unsigned char slot)
708{
709 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
710
711 return spk_to_chmap(mask);
712}
713
Takashi Iwaid45e6882012-07-31 11:36:00 +0200714/* get the CA index corresponding to the given ALSA API channel map */
715static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
716{
717 int i, spks = 0, spk_mask = 0;
718
719 for (i = 0; i < chs; i++) {
720 int mask = to_spk_mask(map[i]);
721 if (mask) {
722 spk_mask |= mask;
723 spks++;
724 }
725 }
726
727 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
728 if ((chs == channel_allocations[i].channels ||
729 spks == channel_allocations[i].channels) &&
730 (spk_mask & channel_allocations[i].spk_mask) ==
731 channel_allocations[i].spk_mask)
732 return channel_allocations[i].ca_index;
733 }
734 return -1;
735}
736
737/* set up the channel slots for the given ALSA API channel map */
738static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
739 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300740 int chs, unsigned char *map,
741 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200742{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300743 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300744 int alsa_pos, hdmi_slot;
745 int assignments[8] = {[0 ... 7] = 0xf};
746
747 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
748
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300749 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300750
751 if (hdmi_slot < 0)
752 continue; /* unassigned channel */
753
754 assignments[hdmi_slot] = alsa_pos;
755 }
756
757 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200758 int val, err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300759
760 val = (assignments[hdmi_slot] << 4) | hdmi_slot;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200761 err = snd_hda_codec_write(codec, pin_nid, 0,
762 AC_VERB_SET_HDMI_CHAN_SLOT, val);
763 if (err)
764 return -EINVAL;
765 }
766 return 0;
767}
768
769/* store ALSA API channel map from the current default map */
770static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
771{
772 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300773 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200774 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300775 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300776 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200777 else
778 map[i] = 0;
779 }
780}
781
782static void hdmi_setup_channel_mapping(struct hda_codec *codec,
783 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200784 int channels, unsigned char *map,
785 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200786{
Anssi Hannula20608732013-02-03 17:55:45 +0200787 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200788 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300789 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200790 } else {
791 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
792 hdmi_setup_fake_chmap(map, ca);
793 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300794
795 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200796}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800797
798/*
799 * Audio InfoFrame routines
800 */
801
802/*
803 * Enable Audio InfoFrame Transmission
804 */
805static void hdmi_start_infoframe_trans(struct hda_codec *codec,
806 hda_nid_t pin_nid)
807{
808 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
809 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
810 AC_DIPXMIT_BEST);
811}
812
813/*
814 * Disable Audio InfoFrame Transmission
815 */
816static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
817 hda_nid_t pin_nid)
818{
819 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
820 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
821 AC_DIPXMIT_DISABLE);
822}
823
824static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
825{
826#ifdef CONFIG_SND_DEBUG_VERBOSE
827 int i;
828 int size;
829
830 size = snd_hdmi_get_eld_size(codec, pin_nid);
831 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
832
833 for (i = 0; i < 8; i++) {
834 size = snd_hda_codec_read(codec, pin_nid, 0,
835 AC_VERB_GET_HDMI_DIP_SIZE, i);
836 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
837 }
838#endif
839}
840
841static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
842{
843#ifdef BE_PARANOID
844 int i, j;
845 int size;
846 int pi, bi;
847 for (i = 0; i < 8; i++) {
848 size = snd_hda_codec_read(codec, pin_nid, 0,
849 AC_VERB_GET_HDMI_DIP_SIZE, i);
850 if (size == 0)
851 continue;
852
853 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
854 for (j = 1; j < 1000; j++) {
855 hdmi_write_dip_byte(codec, pin_nid, 0x0);
856 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
857 if (pi != i)
858 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
859 bi, pi, i);
860 if (bi == 0) /* byte index wrapped around */
861 break;
862 }
863 snd_printd(KERN_INFO
864 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
865 i, size, j);
866 }
867#endif
868}
869
Wu Fengguang53d7d692010-09-21 14:25:49 +0800870static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800871{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800872 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800873 u8 sum = 0;
874 int i;
875
Wu Fengguang53d7d692010-09-21 14:25:49 +0800876 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800877
Wu Fengguang53d7d692010-09-21 14:25:49 +0800878 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800879 sum += bytes[i];
880
Wu Fengguang53d7d692010-09-21 14:25:49 +0800881 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800882}
883
884static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
885 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800886 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800887{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800888 int i;
889
890 hdmi_debug_dip_size(codec, pin_nid);
891 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
892
Wu Fengguang079d88c2010-03-08 10:44:23 +0800893 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800894 for (i = 0; i < size; i++)
895 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800896}
897
898static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800899 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800900{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800901 u8 val;
902 int i;
903
904 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
905 != AC_DIPXMIT_BEST)
906 return false;
907
908 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800909 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800910 val = snd_hda_codec_read(codec, pin_nid, 0,
911 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800912 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800913 return false;
914 }
915
916 return true;
917}
918
Takashi Iwaib0540872013-09-02 12:33:02 +0200919static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
920 struct hdmi_spec_per_pin *per_pin,
921 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800922{
Stephen Warren384a48d2011-06-01 11:14:21 -0600923 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200924 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300925 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600926 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300927 int ca, ordered_ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100928 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800929
Takashi Iwaib0540872013-09-02 12:33:02 +0200930 if (!channels)
931 return;
932
Mengdong Lin58f7d282013-09-04 16:37:12 -0400933 if (is_haswell(codec))
934 snd_hda_codec_write(codec, pin_nid, 0,
935 AC_VERB_SET_AMP_GAIN_MUTE,
936 AMP_OUT_UNMUTE);
937
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100938 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600939 if (!eld->monitor_present)
940 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800941
Takashi Iwaid45e6882012-07-31 11:36:00 +0200942 if (!non_pcm && per_pin->chmap_set)
943 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
944 else
945 ca = hdmi_channel_allocation(eld, channels);
946 if (ca < 0)
947 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800948
Anssi Hannula1df5a062013-10-05 02:25:40 +0300949 ordered_ca = get_channel_allocation_order(ca);
950 active_channels = channel_allocations[ordered_ca].channels;
951
952 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
953
Stephen Warren384a48d2011-06-01 11:14:21 -0600954 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100955 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600956 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800957
Stephen Warren384a48d2011-06-01 11:14:21 -0600958 hdmi_ai->type = 0x84;
959 hdmi_ai->ver = 0x01;
960 hdmi_ai->len = 0x0a;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300961 hdmi_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600962 hdmi_ai->CA = ca;
963 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100964 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600965 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800966
Stephen Warren384a48d2011-06-01 11:14:21 -0600967 dp_ai->type = 0x84;
968 dp_ai->len = 0x1b;
969 dp_ai->ver = 0x11 << 2;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300970 dp_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600971 dp_ai->CA = ca;
972 } else {
973 snd_printd("HDMI: unknown connection type at pin %d\n",
974 pin_nid);
975 return;
976 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800977
Stephen Warren384a48d2011-06-01 11:14:21 -0600978 /*
979 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
980 * sizeof(*dp_ai) to avoid partial match/update problems when
981 * the user switches between HDMI/DP monitors.
982 */
983 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
984 sizeof(ai))) {
985 snd_printdd("hdmi_setup_audio_infoframe: "
Anssi Hannula980b2492013-10-05 02:25:44 +0300986 "pin=%d channels=%d ca=0x%02x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600987 pin_nid,
Anssi Hannula980b2492013-10-05 02:25:44 +0300988 active_channels, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200989 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200990 channels, per_pin->chmap,
991 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600992 hdmi_stop_infoframe_trans(codec, pin_nid);
993 hdmi_fill_audio_infoframe(codec, pin_nid,
994 ai.bytes, sizeof(ai));
995 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800996 } else {
997 /* For non-pcm audio switch, setup new channel mapping
998 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200999 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +02001000 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +02001001 channels, per_pin->chmap,
1002 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001003 }
Wang Xingchao433968d2012-09-06 10:02:37 +08001004
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001005 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001006}
1007
1008
1009/*
1010 * Unsolicited events
1011 */
1012
Wu Fengguangc6e84532011-11-18 16:59:32 -06001013static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001014
Wu Fengguang079d88c2010-03-08 10:44:23 +08001015static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1016{
1017 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +02001018 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1019 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001020 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +02001021 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001022 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001023
1024 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1025 if (!jack)
1026 return;
1027 pin_nid = jack->nid;
1028 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001029
Fengguang Wufae3d882012-04-10 17:00:35 +08001030 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001031 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1032 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001033 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001034
Stephen Warren384a48d2011-06-01 11:14:21 -06001035 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1036 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001037 return;
1038
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001039 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001040 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001041}
1042
1043static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1044{
1045 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1046 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1047 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1048 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1049
1050 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001051 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001052 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001053 tag,
1054 subtag,
1055 cp_state,
1056 cp_ready);
1057
1058 /* TODO */
1059 if (cp_state)
1060 ;
1061 if (cp_ready)
1062 ;
1063}
1064
1065
1066static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1067{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001068 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1069 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1070
Takashi Iwai3a938972011-10-28 01:16:55 +02001071 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001072 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1073 return;
1074 }
1075
1076 if (subtag == 0)
1077 hdmi_intrinsic_event(codec, res);
1078 else
1079 hdmi_non_intrinsic_event(codec, res);
1080}
1081
Mengdong Lin58f7d282013-09-04 16:37:12 -04001082static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001083 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001084{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001085 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001086
Wang Xingchao53b434f2013-06-18 10:41:53 +08001087 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1088 * thus pins could only choose converter 0 for use. Make sure the
1089 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001090 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001091 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1092
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001093 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001094 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1095 AC_PWRST_D0);
1096 msleep(40);
1097 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1098 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1099 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1100 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001101}
1102
Wu Fengguang079d88c2010-03-08 10:44:23 +08001103/*
1104 * Callbacks
1105 */
1106
Takashi Iwai92f10b32010-08-03 14:21:00 +02001107/* HBR should be Non-PCM, 8 channels */
1108#define is_hbr_format(format) \
1109 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1110
Stephen Warren384a48d2011-06-01 11:14:21 -06001111static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1112 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001113{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001114 int pinctl;
1115 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001116
Mengdong Linfb87fa32013-09-04 16:36:57 -04001117 if (is_haswell(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001118 haswell_verify_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001119
Stephen Warren384a48d2011-06-01 11:14:21 -06001120 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1121 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001122 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1123
1124 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001125 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001126 new_pinctl |= AC_PINCTL_EPT_HBR;
1127 else
1128 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1129
1130 snd_printdd("hdmi_setup_stream: "
1131 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001132 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001133 pinctl == new_pinctl ? "" : "new-",
1134 new_pinctl);
1135
1136 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001137 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001138 AC_VERB_SET_PIN_WIDGET_CONTROL,
1139 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001140
Stephen Warren384a48d2011-06-01 11:14:21 -06001141 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001142 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001143 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1144 return -EINVAL;
1145 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001146
Stephen Warren384a48d2011-06-01 11:14:21 -06001147 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001148 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001149}
1150
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001151static int hdmi_choose_cvt(struct hda_codec *codec,
1152 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001153{
1154 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001155 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001156 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001157 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001158
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001159 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001160
Stephen Warren384a48d2011-06-01 11:14:21 -06001161 /* Dynamically assign converter to stream */
1162 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001163 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001164
1165 /* Must not already be assigned */
1166 if (per_cvt->assigned)
1167 continue;
1168 /* Must be in pin's mux's list of converters */
1169 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1170 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1171 break;
1172 /* Not in mux list */
1173 if (mux_idx == per_pin->num_mux_nids)
1174 continue;
1175 break;
1176 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001177
Stephen Warren384a48d2011-06-01 11:14:21 -06001178 /* No free converters */
1179 if (cvt_idx == spec->num_cvts)
1180 return -ENODEV;
1181
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001182 if (cvt_id)
1183 *cvt_id = cvt_idx;
1184 if (mux_id)
1185 *mux_id = mux_idx;
1186
1187 return 0;
1188}
1189
1190static void haswell_config_cvts(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001191 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001192{
1193 struct hdmi_spec *spec = codec->spec;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001194 hda_nid_t nid, end_nid;
1195 int cvt_idx, curr;
1196 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001197
Mengdong Linf82d7d12013-09-21 20:34:45 -04001198 /* configure all pins, including "no physical connection" ones */
1199 end_nid = codec->start_nid + codec->num_nodes;
1200 for (nid = codec->start_nid; nid < end_nid; nid++) {
1201 unsigned int wid_caps = get_wcaps(codec, nid);
1202 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001203
Mengdong Linf82d7d12013-09-21 20:34:45 -04001204 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001205 continue;
1206
Mengdong Linf82d7d12013-09-21 20:34:45 -04001207 if (nid == pin_nid)
1208 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001209
Mengdong Linf82d7d12013-09-21 20:34:45 -04001210 curr = snd_hda_codec_read(codec, nid, 0,
1211 AC_VERB_GET_CONNECT_SEL, 0);
1212 if (curr != mux_idx)
1213 continue;
1214
1215 /* choose an unassigned converter. The conveters in the
1216 * connection list are in the same order as in the codec.
1217 */
1218 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1219 per_cvt = get_cvt(spec, cvt_idx);
1220 if (!per_cvt->assigned) {
1221 snd_printdd("choose cvt %d for pin nid %d\n",
1222 cvt_idx, nid);
1223 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001224 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001225 cvt_idx);
1226 break;
1227 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001228 }
1229 }
1230}
1231
1232/*
1233 * HDA PCM callbacks
1234 */
1235static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1236 struct hda_codec *codec,
1237 struct snd_pcm_substream *substream)
1238{
1239 struct hdmi_spec *spec = codec->spec;
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1241 int pin_idx, cvt_idx, mux_idx = 0;
1242 struct hdmi_spec_per_pin *per_pin;
1243 struct hdmi_eld *eld;
1244 struct hdmi_spec_per_cvt *per_cvt = NULL;
1245 int err;
1246
1247 /* Validate hinfo */
1248 pin_idx = hinfo_to_pin_index(spec, hinfo);
1249 if (snd_BUG_ON(pin_idx < 0))
1250 return -EINVAL;
1251 per_pin = get_pin(spec, pin_idx);
1252 eld = &per_pin->sink_eld;
1253
1254 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1255 if (err < 0)
1256 return err;
1257
1258 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001259 /* Claim converter */
1260 per_cvt->assigned = 1;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001261 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001262 hinfo->nid = per_cvt->cvt_nid;
1263
Takashi Iwaibddee962013-06-18 16:14:22 +02001264 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001265 AC_VERB_SET_CONNECT_SEL,
1266 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001267
1268 /* configure unused pins to choose other converters */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001269 if (is_haswell(codec))
Mengdong Linf82d7d12013-09-21 20:34:45 -04001270 haswell_config_cvts(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001271
Stephen Warren384a48d2011-06-01 11:14:21 -06001272 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001273
Stephen Warren2def8172011-06-01 11:14:20 -06001274 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001275 hinfo->channels_min = per_cvt->channels_min;
1276 hinfo->channels_max = per_cvt->channels_max;
1277 hinfo->rates = per_cvt->rates;
1278 hinfo->formats = per_cvt->formats;
1279 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001280
Stephen Warren384a48d2011-06-01 11:14:21 -06001281 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001282 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001283 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001284 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001285 !hinfo->rates || !hinfo->formats) {
1286 per_cvt->assigned = 0;
1287 hinfo->nid = 0;
1288 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001289 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001290 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001291 }
Stephen Warren2def8172011-06-01 11:14:20 -06001292
1293 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001294 runtime->hw.channels_min = hinfo->channels_min;
1295 runtime->hw.channels_max = hinfo->channels_max;
1296 runtime->hw.formats = hinfo->formats;
1297 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001298
1299 snd_pcm_hw_constraint_step(substream->runtime, 0,
1300 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001301 return 0;
1302}
1303
1304/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001305 * HDA/HDMI auto parsing
1306 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001307static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001308{
1309 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001310 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001311 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001312
1313 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1314 snd_printk(KERN_WARNING
1315 "HDMI: pin %d wcaps %#x "
1316 "does not support connection list\n",
1317 pin_nid, get_wcaps(codec, pin_nid));
1318 return -EINVAL;
1319 }
1320
Stephen Warren384a48d2011-06-01 11:14:21 -06001321 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1322 per_pin->mux_nids,
1323 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001324
1325 return 0;
1326}
1327
Wu Fengguangc6e84532011-11-18 16:59:32 -06001328static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001329{
Wu Fengguang744626d2011-11-16 16:29:47 +08001330 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001331 struct hdmi_spec *spec = codec->spec;
1332 struct hdmi_eld *eld = &spec->temp_eld;
1333 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001334 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001335 /*
1336 * Always execute a GetPinSense verb here, even when called from
1337 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1338 * response's PD bit is not the real PD value, but indicates that
1339 * the real PD value changed. An older version of the HD-audio
1340 * specification worked this way. Hence, we just ignore the data in
1341 * the unsolicited response to avoid custom WARs.
1342 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001343 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001344 bool update_eld = false;
1345 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001346
David Henningsson4bd038f2013-02-19 16:11:25 +01001347 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1348 if (pin_eld->monitor_present)
1349 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1350 else
1351 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001352
Fengguang Wufae3d882012-04-10 17:00:35 +08001353 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001354 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001355 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001356
David Henningsson4bd038f2013-02-19 16:11:25 +01001357 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001358 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1359 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001360 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001361 else {
1362 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1363 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1364 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001365 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001366 }
1367
David Henningsson4bd038f2013-02-19 16:11:25 +01001368 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001369 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001370 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001371 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001372 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001373 queue_delayed_work(codec->bus->workq,
1374 &per_pin->work,
1375 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001376 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001377 }
1378 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001379
1380 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001381 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001382 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001383 eld_changed = true;
1384 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001385 if (update_eld) {
Takashi Iwaib0540872013-09-02 12:33:02 +02001386 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001387 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001388 eld_changed = pin_eld->eld_size != eld->eld_size ||
1389 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001390 eld->eld_size) != 0;
1391 if (eld_changed)
1392 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1393 eld->eld_size);
1394 pin_eld->eld_size = eld->eld_size;
1395 pin_eld->info = eld->info;
Takashi Iwaib0540872013-09-02 12:33:02 +02001396
1397 /* Haswell-specific workaround: re-setup when the transcoder is
1398 * changed during the stream playback
1399 */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001400 if (is_haswell(codec) &&
Mengdong Lin58f7d282013-09-04 16:37:12 -04001401 eld->eld_valid && !old_eld_valid && per_pin->setup)
Takashi Iwaib0540872013-09-02 12:33:02 +02001402 hdmi_setup_audio_infoframe(codec, per_pin,
1403 per_pin->non_pcm);
David Henningsson4bd038f2013-02-19 16:11:25 +01001404 }
1405 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001406
1407 if (eld_changed)
1408 snd_ctl_notify(codec->bus->card,
1409 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1410 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001411}
1412
Wu Fengguang744626d2011-11-16 16:29:47 +08001413static void hdmi_repoll_eld(struct work_struct *work)
1414{
1415 struct hdmi_spec_per_pin *per_pin =
1416 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1417
Wu Fengguangc6e84532011-11-18 16:59:32 -06001418 if (per_pin->repoll_count++ > 6)
1419 per_pin->repoll_count = 0;
1420
1421 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001422}
1423
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001424static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1425 hda_nid_t nid);
1426
Wu Fengguang079d88c2010-03-08 10:44:23 +08001427static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1428{
1429 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001430 unsigned int caps, config;
1431 int pin_idx;
1432 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001433 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001434
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001435 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001436 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1437 return 0;
1438
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001439 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001440 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1441 return 0;
1442
Mengdong Linfb87fa32013-09-04 16:36:57 -04001443 if (is_haswell(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001444 intel_haswell_fixup_connect_list(codec, pin_nid);
1445
Stephen Warren384a48d2011-06-01 11:14:21 -06001446 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001447 per_pin = snd_array_new(&spec->pins);
1448 if (!per_pin)
1449 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001450
1451 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001452 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001453
Stephen Warren384a48d2011-06-01 11:14:21 -06001454 err = hdmi_read_pin_conn(codec, pin_idx);
1455 if (err < 0)
1456 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001457
Wu Fengguang079d88c2010-03-08 10:44:23 +08001458 spec->num_pins++;
1459
Stephen Warren384a48d2011-06-01 11:14:21 -06001460 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001461}
1462
Stephen Warren384a48d2011-06-01 11:14:21 -06001463static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001464{
1465 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001466 struct hdmi_spec_per_cvt *per_cvt;
1467 unsigned int chans;
1468 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001469
Stephen Warren384a48d2011-06-01 11:14:21 -06001470 chans = get_wcaps(codec, cvt_nid);
1471 chans = get_wcaps_channels(chans);
1472
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001473 per_cvt = snd_array_new(&spec->cvts);
1474 if (!per_cvt)
1475 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001476
1477 per_cvt->cvt_nid = cvt_nid;
1478 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001479 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001480 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001481 if (chans > spec->channels_max)
1482 spec->channels_max = chans;
1483 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001484
1485 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1486 &per_cvt->rates,
1487 &per_cvt->formats,
1488 &per_cvt->maxbps);
1489 if (err < 0)
1490 return err;
1491
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001492 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1493 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1494 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001495
1496 return 0;
1497}
1498
1499static int hdmi_parse_codec(struct hda_codec *codec)
1500{
1501 hda_nid_t nid;
1502 int i, nodes;
1503
1504 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1505 if (!nid || nodes < 0) {
1506 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1507 return -EINVAL;
1508 }
1509
1510 for (i = 0; i < nodes; i++, nid++) {
1511 unsigned int caps;
1512 unsigned int type;
1513
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001514 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001515 type = get_wcaps_type(caps);
1516
1517 if (!(caps & AC_WCAP_DIGITAL))
1518 continue;
1519
1520 switch (type) {
1521 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001522 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001523 break;
1524 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001525 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001526 break;
1527 }
1528 }
1529
David Henningssonc9adeef2012-11-07 09:22:33 +01001530#ifdef CONFIG_PM
1531 /* We're seeing some problems with unsolicited hot plug events on
1532 * PantherPoint after S3, if this is not enabled */
1533 if (codec->vendor_id == 0x80862806)
1534 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001535 /*
1536 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1537 * can be lost and presence sense verb will become inaccurate if the
1538 * HDA link is powered off at hot plug or hw initialization time.
1539 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001540 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001541 AC_PWRST_EPSS))
1542 codec->bus->power_keep_link_on = 1;
1543#endif
1544
1545 return 0;
1546}
1547
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001548/*
1549 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001550static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1551{
1552 struct hda_spdif_out *spdif;
1553 bool non_pcm;
1554
1555 mutex_lock(&codec->spdif_mutex);
1556 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1557 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1558 mutex_unlock(&codec->spdif_mutex);
1559 return non_pcm;
1560}
1561
1562
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001563/*
1564 * HDMI callbacks
1565 */
1566
1567static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1568 struct hda_codec *codec,
1569 unsigned int stream_tag,
1570 unsigned int format,
1571 struct snd_pcm_substream *substream)
1572{
Stephen Warren384a48d2011-06-01 11:14:21 -06001573 hda_nid_t cvt_nid = hinfo->nid;
1574 struct hdmi_spec *spec = codec->spec;
1575 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaib0540872013-09-02 12:33:02 +02001576 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1577 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001578 bool non_pcm;
1579
1580 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaib0540872013-09-02 12:33:02 +02001581 per_pin->channels = substream->runtime->channels;
1582 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001583
Takashi Iwaib0540872013-09-02 12:33:02 +02001584 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001585
1586 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001587}
1588
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001589static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1590 struct hda_codec *codec,
1591 struct snd_pcm_substream *substream)
1592{
1593 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1594 return 0;
1595}
1596
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001597static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1598 struct hda_codec *codec,
1599 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001600{
1601 struct hdmi_spec *spec = codec->spec;
1602 int cvt_idx, pin_idx;
1603 struct hdmi_spec_per_cvt *per_cvt;
1604 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001605
Stephen Warren384a48d2011-06-01 11:14:21 -06001606 if (hinfo->nid) {
1607 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1608 if (snd_BUG_ON(cvt_idx < 0))
1609 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001610 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001611
1612 snd_BUG_ON(!per_cvt->assigned);
1613 per_cvt->assigned = 0;
1614 hinfo->nid = 0;
1615
1616 pin_idx = hinfo_to_pin_index(spec, hinfo);
1617 if (snd_BUG_ON(pin_idx < 0))
1618 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001619 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001620
Stephen Warren384a48d2011-06-01 11:14:21 -06001621 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001622 per_pin->chmap_set = false;
1623 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001624
1625 per_pin->setup = false;
1626 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001627 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001628
Stephen Warren384a48d2011-06-01 11:14:21 -06001629 return 0;
1630}
1631
1632static const struct hda_pcm_ops generic_ops = {
1633 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001634 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001635 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001636 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001637};
1638
Takashi Iwaid45e6882012-07-31 11:36:00 +02001639/*
1640 * ALSA API channel-map control callbacks
1641 */
1642static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_info *uinfo)
1644{
1645 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1646 struct hda_codec *codec = info->private_data;
1647 struct hdmi_spec *spec = codec->spec;
1648 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1649 uinfo->count = spec->channels_max;
1650 uinfo->value.integer.min = 0;
1651 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1652 return 0;
1653}
1654
1655static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1656 unsigned int size, unsigned int __user *tlv)
1657{
1658 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1659 struct hda_codec *codec = info->private_data;
1660 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001661 unsigned int __user *dst;
1662 int chs, count = 0;
1663
1664 if (size < 8)
1665 return -ENOMEM;
1666 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1667 return -EFAULT;
1668 size -= 8;
1669 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001670 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001671 int i, c;
1672 struct cea_channel_speaker_allocation *cap;
1673 cap = channel_allocations;
1674 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1675 int chs_bytes = chs * 4;
1676 if (cap->channels != chs)
1677 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001678 if (size < 8)
1679 return -ENOMEM;
1680 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1681 put_user(chs_bytes, dst + 1))
1682 return -EFAULT;
1683 dst += 2;
1684 size -= 8;
1685 count += 8;
1686 if (size < chs_bytes)
1687 return -ENOMEM;
1688 size -= chs_bytes;
1689 count += chs_bytes;
1690 for (c = 7; c >= 0; c--) {
1691 int spk = cap->speakers[c];
1692 if (!spk)
1693 continue;
1694 if (put_user(spk_to_chmap(spk), dst))
1695 return -EFAULT;
1696 dst++;
1697 }
1698 }
1699 }
1700 if (put_user(count, tlv + 1))
1701 return -EFAULT;
1702 return 0;
1703}
1704
1705static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
1707{
1708 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1709 struct hda_codec *codec = info->private_data;
1710 struct hdmi_spec *spec = codec->spec;
1711 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001712 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001713 int i;
1714
1715 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1716 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1717 return 0;
1718}
1719
1720static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1722{
1723 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1724 struct hda_codec *codec = info->private_data;
1725 struct hdmi_spec *spec = codec->spec;
1726 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001727 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001728 unsigned int ctl_idx;
1729 struct snd_pcm_substream *substream;
1730 unsigned char chmap[8];
1731 int i, ca, prepared = 0;
1732
1733 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1734 substream = snd_pcm_chmap_substream(info, ctl_idx);
1735 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001736 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001737 switch (substream->runtime->status->state) {
1738 case SNDRV_PCM_STATE_OPEN:
1739 case SNDRV_PCM_STATE_SETUP:
1740 break;
1741 case SNDRV_PCM_STATE_PREPARED:
1742 prepared = 1;
1743 break;
1744 default:
1745 return -EBUSY;
1746 }
1747 memset(chmap, 0, sizeof(chmap));
1748 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1749 chmap[i] = ucontrol->value.integer.value[i];
1750 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1751 return 0;
1752 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1753 if (ca < 0)
1754 return -EINVAL;
1755 per_pin->chmap_set = true;
1756 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1757 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02001758 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001759
1760 return 0;
1761}
1762
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001763static int generic_hdmi_build_pcms(struct hda_codec *codec)
1764{
1765 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001766 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001767
Stephen Warren384a48d2011-06-01 11:14:21 -06001768 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1769 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001770 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001771 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001772
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001773 per_pin = get_pin(spec, pin_idx);
1774 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1775 info = snd_array_new(&spec->pcm_rec);
1776 if (!info)
1777 return -ENOMEM;
1778 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001779 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001780 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001781
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001782 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001783 pstr->substreams = 1;
1784 pstr->ops = generic_ops;
1785 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001786 }
1787
Stephen Warren384a48d2011-06-01 11:14:21 -06001788 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001789 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001790
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001791 return 0;
1792}
1793
David Henningsson0b6c49b2011-08-23 16:56:03 +02001794static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1795{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001796 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001797 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001798 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1799 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001800
Takashi Iwai31ef2252011-12-01 17:41:36 +01001801 if (pcmdev > 0)
1802 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001803 if (!is_jack_detectable(codec, per_pin->pin_nid))
1804 strncat(hdmi_str, " Phantom",
1805 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001806
Takashi Iwai31ef2252011-12-01 17:41:36 +01001807 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001808}
1809
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001810static int generic_hdmi_build_controls(struct hda_codec *codec)
1811{
1812 struct hdmi_spec *spec = codec->spec;
1813 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001814 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001815
Stephen Warren384a48d2011-06-01 11:14:21 -06001816 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001817 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001818
1819 err = generic_hdmi_build_jack(codec, pin_idx);
1820 if (err < 0)
1821 return err;
1822
Takashi Iwaidcda5802012-10-12 17:24:51 +02001823 err = snd_hda_create_dig_out_ctls(codec,
1824 per_pin->pin_nid,
1825 per_pin->mux_nids[0],
1826 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001827 if (err < 0)
1828 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001829 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001830
1831 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001832 err = hdmi_create_eld_ctl(codec, pin_idx,
1833 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001834
1835 if (err < 0)
1836 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001837
Takashi Iwai82b1d732011-12-20 15:53:07 +01001838 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001839 }
1840
Takashi Iwaid45e6882012-07-31 11:36:00 +02001841 /* add channel maps */
1842 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1843 struct snd_pcm_chmap *chmap;
1844 struct snd_kcontrol *kctl;
1845 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02001846
1847 if (!codec->pcm_info[pin_idx].pcm)
1848 break;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001849 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1850 SNDRV_PCM_STREAM_PLAYBACK,
1851 NULL, 0, pin_idx, &chmap);
1852 if (err < 0)
1853 return err;
1854 /* override handlers */
1855 chmap->private_data = codec;
1856 kctl = chmap->kctl;
1857 for (i = 0; i < kctl->count; i++)
1858 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1859 kctl->info = hdmi_chmap_ctl_info;
1860 kctl->get = hdmi_chmap_ctl_get;
1861 kctl->put = hdmi_chmap_ctl_put;
1862 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1863 }
1864
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001865 return 0;
1866}
1867
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001868static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1869{
1870 struct hdmi_spec *spec = codec->spec;
1871 int pin_idx;
1872
1873 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001874 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001875 struct hdmi_eld *eld = &per_pin->sink_eld;
1876
1877 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001878 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001879 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1880 snd_hda_eld_proc_new(codec, eld, pin_idx);
1881 }
1882 return 0;
1883}
1884
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001885static int generic_hdmi_init(struct hda_codec *codec)
1886{
1887 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001888 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001889
Stephen Warren384a48d2011-06-01 11:14:21 -06001890 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001891 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001892 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001893
1894 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001895 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001896 }
1897 return 0;
1898}
1899
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001900static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1901{
1902 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1903 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1904 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1905}
1906
1907static void hdmi_array_free(struct hdmi_spec *spec)
1908{
1909 snd_array_free(&spec->pins);
1910 snd_array_free(&spec->cvts);
1911 snd_array_free(&spec->pcm_rec);
1912}
1913
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001914static void generic_hdmi_free(struct hda_codec *codec)
1915{
1916 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001917 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001918
Stephen Warren384a48d2011-06-01 11:14:21 -06001919 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001920 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001921 struct hdmi_eld *eld = &per_pin->sink_eld;
1922
Wu Fengguang744626d2011-11-16 16:29:47 +08001923 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001924 snd_hda_eld_proc_free(codec, eld);
1925 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001926
Wu Fengguang744626d2011-11-16 16:29:47 +08001927 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001928 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001929 kfree(spec);
1930}
1931
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001932#ifdef CONFIG_PM
1933static int generic_hdmi_resume(struct hda_codec *codec)
1934{
1935 struct hdmi_spec *spec = codec->spec;
1936 int pin_idx;
1937
1938 generic_hdmi_init(codec);
1939 snd_hda_codec_resume_amp(codec);
1940 snd_hda_codec_resume_cache(codec);
1941
1942 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1943 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1944 hdmi_present_sense(per_pin, 1);
1945 }
1946 return 0;
1947}
1948#endif
1949
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001950static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001951 .init = generic_hdmi_init,
1952 .free = generic_hdmi_free,
1953 .build_pcms = generic_hdmi_build_pcms,
1954 .build_controls = generic_hdmi_build_controls,
1955 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001956#ifdef CONFIG_PM
1957 .resume = generic_hdmi_resume,
1958#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001959};
1960
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001961
1962static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1963 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001964{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001965 struct hdmi_spec *spec = codec->spec;
1966 hda_nid_t conns[4];
1967 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001968
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001969 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1970 if (nconns == spec->num_cvts &&
1971 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001972 return;
1973
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001974 /* override pins connection list */
1975 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1976 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001977}
1978
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001979#define INTEL_VENDOR_NID 0x08
1980#define INTEL_GET_VENDOR_VERB 0xf81
1981#define INTEL_SET_VENDOR_VERB 0x781
1982#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1983#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1984
1985static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001986 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001987{
1988 unsigned int vendor_param;
1989
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001990 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1991 INTEL_GET_VENDOR_VERB, 0);
1992 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1993 return;
1994
1995 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1996 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1997 INTEL_SET_VENDOR_VERB, vendor_param);
1998 if (vendor_param == -1)
1999 return;
2000
Takashi Iwai17df3f52013-05-08 08:09:34 +02002001 if (update_tree)
2002 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002003}
2004
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002005static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2006{
2007 unsigned int vendor_param;
2008
2009 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2010 INTEL_GET_VENDOR_VERB, 0);
2011 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2012 return;
2013
2014 /* enable DP1.2 mode */
2015 vendor_param |= INTEL_EN_DP12;
2016 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2017 INTEL_SET_VENDOR_VERB, vendor_param);
2018}
2019
Takashi Iwai17df3f52013-05-08 08:09:34 +02002020/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2021 * Otherwise you may get severe h/w communication errors.
2022 */
2023static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2024 unsigned int power_state)
2025{
2026 if (power_state == AC_PWRST_D0) {
2027 intel_haswell_enable_all_pins(codec, false);
2028 intel_haswell_fixup_enable_dp12(codec);
2029 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002030
Takashi Iwai17df3f52013-05-08 08:09:34 +02002031 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2032 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2033}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002034
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002035static int patch_generic_hdmi(struct hda_codec *codec)
2036{
2037 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002038
2039 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2040 if (spec == NULL)
2041 return -ENOMEM;
2042
2043 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002044 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002045
Mengdong Linfb87fa32013-09-04 16:36:57 -04002046 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002047 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002048 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002049 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002050
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002051 if (hdmi_parse_codec(codec) < 0) {
2052 codec->spec = NULL;
2053 kfree(spec);
2054 return -EINVAL;
2055 }
2056 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Linfb87fa32013-09-04 16:36:57 -04002057 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002058 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002059 codec->dp_mst = true;
2060 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002061
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002062 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002063
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002064 init_channel_allocations();
2065
2066 return 0;
2067}
2068
2069/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002070 * Shared non-generic implementations
2071 */
2072
2073static int simple_playback_build_pcms(struct hda_codec *codec)
2074{
2075 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002076 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002077 unsigned int chans;
2078 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002079 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002080
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002081 per_cvt = get_cvt(spec, 0);
2082 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002083 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002084
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002085 info = snd_array_new(&spec->pcm_rec);
2086 if (!info)
2087 return -ENOMEM;
2088 info->name = get_pin(spec, 0)->pcm_name;
2089 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002090 info->pcm_type = HDA_PCM_TYPE_HDMI;
2091 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2092 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002093 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002094 if (pstr->channels_max <= 2 && chans && chans <= 16)
2095 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002096
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002097 codec->num_pcms = 1;
2098 codec->pcm_info = info;
2099
Stephen Warren3aaf8982011-06-01 11:14:19 -06002100 return 0;
2101}
2102
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002103/* unsolicited event for jack sensing */
2104static void simple_hdmi_unsol_event(struct hda_codec *codec,
2105 unsigned int res)
2106{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002107 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002108 snd_hda_jack_report_sync(codec);
2109}
2110
2111/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2112 * as long as spec->pins[] is set correctly
2113 */
2114#define simple_hdmi_build_jack generic_hdmi_build_jack
2115
Stephen Warren3aaf8982011-06-01 11:14:19 -06002116static int simple_playback_build_controls(struct hda_codec *codec)
2117{
2118 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002119 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002120 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002121
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002122 per_cvt = get_cvt(spec, 0);
2123 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2124 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002125 if (err < 0)
2126 return err;
2127 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002128}
2129
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002130static int simple_playback_init(struct hda_codec *codec)
2131{
2132 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002133 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2134 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002135
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002136 snd_hda_codec_write(codec, pin, 0,
2137 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2138 /* some codecs require to unmute the pin */
2139 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2140 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2141 AMP_OUT_UNMUTE);
2142 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002143 return 0;
2144}
2145
Stephen Warren3aaf8982011-06-01 11:14:19 -06002146static void simple_playback_free(struct hda_codec *codec)
2147{
2148 struct hdmi_spec *spec = codec->spec;
2149
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002150 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002151 kfree(spec);
2152}
2153
2154/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002155 * Nvidia specific implementations
2156 */
2157
2158#define Nv_VERB_SET_Channel_Allocation 0xF79
2159#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2160#define Nv_VERB_SET_Audio_Protection_On 0xF98
2161#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2162
2163#define nvhdmi_master_con_nid_7x 0x04
2164#define nvhdmi_master_pin_nid_7x 0x05
2165
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002166static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002167 /*front, rear, clfe, rear_surr */
2168 0x6, 0x8, 0xa, 0xc,
2169};
2170
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002171static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2172 /* set audio protect on */
2173 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2174 /* enable digital output on pin widget */
2175 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2176 {} /* terminator */
2177};
2178
2179static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002180 /* set audio protect on */
2181 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2182 /* enable digital output on pin widget */
2183 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2184 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2185 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2186 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2187 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2188 {} /* terminator */
2189};
2190
2191#ifdef LIMITED_RATE_FMT_SUPPORT
2192/* support only the safe format and rate */
2193#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2194#define SUPPORTED_MAXBPS 16
2195#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2196#else
2197/* support all rates and formats */
2198#define SUPPORTED_RATES \
2199 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2200 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2201 SNDRV_PCM_RATE_192000)
2202#define SUPPORTED_MAXBPS 24
2203#define SUPPORTED_FORMATS \
2204 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2205#endif
2206
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002207static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002208{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002209 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2210 return 0;
2211}
2212
2213static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2214{
2215 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002216 return 0;
2217}
2218
Nitin Daga393004b2011-01-10 21:49:31 +05302219static unsigned int channels_2_6_8[] = {
2220 2, 6, 8
2221};
2222
2223static unsigned int channels_2_8[] = {
2224 2, 8
2225};
2226
2227static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2228 .count = ARRAY_SIZE(channels_2_6_8),
2229 .list = channels_2_6_8,
2230 .mask = 0,
2231};
2232
2233static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2234 .count = ARRAY_SIZE(channels_2_8),
2235 .list = channels_2_8,
2236 .mask = 0,
2237};
2238
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002239static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2240 struct hda_codec *codec,
2241 struct snd_pcm_substream *substream)
2242{
2243 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302244 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2245
2246 switch (codec->preset->id) {
2247 case 0x10de0002:
2248 case 0x10de0003:
2249 case 0x10de0005:
2250 case 0x10de0006:
2251 hw_constraints_channels = &hw_constraints_2_8_channels;
2252 break;
2253 case 0x10de0007:
2254 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2255 break;
2256 default:
2257 break;
2258 }
2259
2260 if (hw_constraints_channels != NULL) {
2261 snd_pcm_hw_constraint_list(substream->runtime, 0,
2262 SNDRV_PCM_HW_PARAM_CHANNELS,
2263 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002264 } else {
2265 snd_pcm_hw_constraint_step(substream->runtime, 0,
2266 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302267 }
2268
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002269 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2270}
2271
2272static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2273 struct hda_codec *codec,
2274 struct snd_pcm_substream *substream)
2275{
2276 struct hdmi_spec *spec = codec->spec;
2277 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2278}
2279
2280static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2281 struct hda_codec *codec,
2282 unsigned int stream_tag,
2283 unsigned int format,
2284 struct snd_pcm_substream *substream)
2285{
2286 struct hdmi_spec *spec = codec->spec;
2287 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2288 stream_tag, format, substream);
2289}
2290
Takashi Iwaid0b12522012-06-15 14:34:42 +02002291static const struct hda_pcm_stream simple_pcm_playback = {
2292 .substreams = 1,
2293 .channels_min = 2,
2294 .channels_max = 2,
2295 .ops = {
2296 .open = simple_playback_pcm_open,
2297 .close = simple_playback_pcm_close,
2298 .prepare = simple_playback_pcm_prepare
2299 },
2300};
2301
2302static const struct hda_codec_ops simple_hdmi_patch_ops = {
2303 .build_controls = simple_playback_build_controls,
2304 .build_pcms = simple_playback_build_pcms,
2305 .init = simple_playback_init,
2306 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002307 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002308};
2309
2310static int patch_simple_hdmi(struct hda_codec *codec,
2311 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2312{
2313 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002314 struct hdmi_spec_per_cvt *per_cvt;
2315 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002316
2317 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2318 if (!spec)
2319 return -ENOMEM;
2320
2321 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002322 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002323
2324 spec->multiout.num_dacs = 0; /* no analog */
2325 spec->multiout.max_channels = 2;
2326 spec->multiout.dig_out_nid = cvt_nid;
2327 spec->num_cvts = 1;
2328 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002329 per_pin = snd_array_new(&spec->pins);
2330 per_cvt = snd_array_new(&spec->cvts);
2331 if (!per_pin || !per_cvt) {
2332 simple_playback_free(codec);
2333 return -ENOMEM;
2334 }
2335 per_cvt->cvt_nid = cvt_nid;
2336 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002337 spec->pcm_playback = simple_pcm_playback;
2338
2339 codec->patch_ops = simple_hdmi_patch_ops;
2340
2341 return 0;
2342}
2343
Aaron Plattner1f348522011-04-06 17:19:04 -07002344static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2345 int channels)
2346{
2347 unsigned int chanmask;
2348 int chan = channels ? (channels - 1) : 1;
2349
2350 switch (channels) {
2351 default:
2352 case 0:
2353 case 2:
2354 chanmask = 0x00;
2355 break;
2356 case 4:
2357 chanmask = 0x08;
2358 break;
2359 case 6:
2360 chanmask = 0x0b;
2361 break;
2362 case 8:
2363 chanmask = 0x13;
2364 break;
2365 }
2366
2367 /* Set the audio infoframe channel allocation and checksum fields. The
2368 * channel count is computed implicitly by the hardware. */
2369 snd_hda_codec_write(codec, 0x1, 0,
2370 Nv_VERB_SET_Channel_Allocation, chanmask);
2371
2372 snd_hda_codec_write(codec, 0x1, 0,
2373 Nv_VERB_SET_Info_Frame_Checksum,
2374 (0x71 - chan - chanmask));
2375}
2376
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002377static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2378 struct hda_codec *codec,
2379 struct snd_pcm_substream *substream)
2380{
2381 struct hdmi_spec *spec = codec->spec;
2382 int i;
2383
2384 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2385 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2386 for (i = 0; i < 4; i++) {
2387 /* set the stream id */
2388 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2389 AC_VERB_SET_CHANNEL_STREAMID, 0);
2390 /* set the stream format */
2391 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2392 AC_VERB_SET_STREAM_FORMAT, 0);
2393 }
2394
Aaron Plattner1f348522011-04-06 17:19:04 -07002395 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2396 * streams are disabled. */
2397 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2398
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002399 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2400}
2401
2402static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2403 struct hda_codec *codec,
2404 unsigned int stream_tag,
2405 unsigned int format,
2406 struct snd_pcm_substream *substream)
2407{
2408 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002409 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002410 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002411 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002412 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002413 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002414
2415 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002416 per_cvt = get_cvt(spec, 0);
2417 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002418
2419 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002420
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002421 dataDCC2 = 0x2;
2422
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002423 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002424 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002425 snd_hda_codec_write(codec,
2426 nvhdmi_master_con_nid_7x,
2427 0,
2428 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002429 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002430
2431 /* set the stream id */
2432 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2433 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2434
2435 /* set the stream format */
2436 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2437 AC_VERB_SET_STREAM_FORMAT, format);
2438
2439 /* turn on again (if needed) */
2440 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002441 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002442 snd_hda_codec_write(codec,
2443 nvhdmi_master_con_nid_7x,
2444 0,
2445 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002446 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002447 snd_hda_codec_write(codec,
2448 nvhdmi_master_con_nid_7x,
2449 0,
2450 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2451 }
2452
2453 for (i = 0; i < 4; i++) {
2454 if (chs == 2)
2455 channel_id = 0;
2456 else
2457 channel_id = i * 2;
2458
2459 /* turn off SPDIF once;
2460 *otherwise the IEC958 bits won't be updated
2461 */
2462 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002463 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002464 snd_hda_codec_write(codec,
2465 nvhdmi_con_nids_7x[i],
2466 0,
2467 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002468 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002469 /* set the stream id */
2470 snd_hda_codec_write(codec,
2471 nvhdmi_con_nids_7x[i],
2472 0,
2473 AC_VERB_SET_CHANNEL_STREAMID,
2474 (stream_tag << 4) | channel_id);
2475 /* set the stream format */
2476 snd_hda_codec_write(codec,
2477 nvhdmi_con_nids_7x[i],
2478 0,
2479 AC_VERB_SET_STREAM_FORMAT,
2480 format);
2481 /* turn on again (if needed) */
2482 /* enable and set the channel status audio/data flag */
2483 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002484 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002485 snd_hda_codec_write(codec,
2486 nvhdmi_con_nids_7x[i],
2487 0,
2488 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002489 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002490 snd_hda_codec_write(codec,
2491 nvhdmi_con_nids_7x[i],
2492 0,
2493 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2494 }
2495 }
2496
Aaron Plattner1f348522011-04-06 17:19:04 -07002497 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002498
2499 mutex_unlock(&codec->spdif_mutex);
2500 return 0;
2501}
2502
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002503static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002504 .substreams = 1,
2505 .channels_min = 2,
2506 .channels_max = 8,
2507 .nid = nvhdmi_master_con_nid_7x,
2508 .rates = SUPPORTED_RATES,
2509 .maxbps = SUPPORTED_MAXBPS,
2510 .formats = SUPPORTED_FORMATS,
2511 .ops = {
2512 .open = simple_playback_pcm_open,
2513 .close = nvhdmi_8ch_7x_pcm_close,
2514 .prepare = nvhdmi_8ch_7x_pcm_prepare
2515 },
2516};
2517
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002518static int patch_nvhdmi_2ch(struct hda_codec *codec)
2519{
2520 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002521 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2522 nvhdmi_master_pin_nid_7x);
2523 if (err < 0)
2524 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002525
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002526 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002527 /* override the PCM rates, etc, as the codec doesn't give full list */
2528 spec = codec->spec;
2529 spec->pcm_playback.rates = SUPPORTED_RATES;
2530 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2531 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002532 return 0;
2533}
2534
Takashi Iwai53775b02012-08-01 12:17:41 +02002535static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2536{
2537 struct hdmi_spec *spec = codec->spec;
2538 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002539 if (!err) {
2540 struct hda_pcm *info = get_pcm_rec(spec, 0);
2541 info->own_chmap = true;
2542 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002543 return err;
2544}
2545
2546static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2547{
2548 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002549 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002550 struct snd_pcm_chmap *chmap;
2551 int err;
2552
2553 err = simple_playback_build_controls(codec);
2554 if (err < 0)
2555 return err;
2556
2557 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002558 info = get_pcm_rec(spec, 0);
2559 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002560 SNDRV_PCM_STREAM_PLAYBACK,
2561 snd_pcm_alt_chmaps, 8, 0, &chmap);
2562 if (err < 0)
2563 return err;
2564 switch (codec->preset->id) {
2565 case 0x10de0002:
2566 case 0x10de0003:
2567 case 0x10de0005:
2568 case 0x10de0006:
2569 chmap->channel_mask = (1U << 2) | (1U << 8);
2570 break;
2571 case 0x10de0007:
2572 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2573 }
2574 return 0;
2575}
2576
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002577static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2578{
2579 struct hdmi_spec *spec;
2580 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002581 if (err < 0)
2582 return err;
2583 spec = codec->spec;
2584 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002585 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002586 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002587 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2588 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002589
2590 /* Initialize the audio infoframe channel mask and checksum to something
2591 * valid */
2592 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2593
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002594 return 0;
2595}
2596
2597/*
2598 * ATI-specific implementations
2599 *
2600 * FIXME: we may omit the whole this and use the generic code once after
2601 * it's confirmed to work.
2602 */
2603
2604#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2605#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2606
2607static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2608 struct hda_codec *codec,
2609 unsigned int stream_tag,
2610 unsigned int format,
2611 struct snd_pcm_substream *substream)
2612{
2613 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002614 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002615 int chans = substream->runtime->channels;
2616 int i, err;
2617
2618 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2619 substream);
2620 if (err < 0)
2621 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002622 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002623 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624 /* FIXME: XXX */
2625 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002626 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002627 AC_VERB_SET_HDMI_CHAN_SLOT,
2628 (i << 4) | i);
2629 }
2630 return 0;
2631}
2632
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002633static int patch_atihdmi(struct hda_codec *codec)
2634{
2635 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002636 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2637 if (err < 0)
2638 return err;
2639 spec = codec->spec;
2640 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002641 return 0;
2642}
2643
Annie Liu3de5ff82012-06-08 19:18:42 +08002644/* VIA HDMI Implementation */
2645#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2646#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2647
Annie Liu3de5ff82012-06-08 19:18:42 +08002648static int patch_via_hdmi(struct hda_codec *codec)
2649{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002650 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002651}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002652
2653/*
2654 * patch entries
2655 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002656static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002657{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2658{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2659{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002660{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002661{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2662{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2663{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2664{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2665{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2666{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2667{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2668{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002669{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2670{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2671{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2672{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2673{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2674{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2675{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2676{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2677{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2678{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2679{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002680/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002681{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2682{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2683{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2684{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2685{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2686{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2687{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2688{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2689{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2690{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002691{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002692{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002693{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2694{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002695{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2696{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2697{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2698{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002699{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2700{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2701{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2702{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2703{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2704{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002705{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002706{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002707{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002708{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2709{} /* terminator */
2710};
2711
2712MODULE_ALIAS("snd-hda-codec-id:1002793c");
2713MODULE_ALIAS("snd-hda-codec-id:10027919");
2714MODULE_ALIAS("snd-hda-codec-id:1002791a");
2715MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2716MODULE_ALIAS("snd-hda-codec-id:10951390");
2717MODULE_ALIAS("snd-hda-codec-id:10951392");
2718MODULE_ALIAS("snd-hda-codec-id:10de0002");
2719MODULE_ALIAS("snd-hda-codec-id:10de0003");
2720MODULE_ALIAS("snd-hda-codec-id:10de0005");
2721MODULE_ALIAS("snd-hda-codec-id:10de0006");
2722MODULE_ALIAS("snd-hda-codec-id:10de0007");
2723MODULE_ALIAS("snd-hda-codec-id:10de000a");
2724MODULE_ALIAS("snd-hda-codec-id:10de000b");
2725MODULE_ALIAS("snd-hda-codec-id:10de000c");
2726MODULE_ALIAS("snd-hda-codec-id:10de000d");
2727MODULE_ALIAS("snd-hda-codec-id:10de0010");
2728MODULE_ALIAS("snd-hda-codec-id:10de0011");
2729MODULE_ALIAS("snd-hda-codec-id:10de0012");
2730MODULE_ALIAS("snd-hda-codec-id:10de0013");
2731MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002732MODULE_ALIAS("snd-hda-codec-id:10de0015");
2733MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002734MODULE_ALIAS("snd-hda-codec-id:10de0018");
2735MODULE_ALIAS("snd-hda-codec-id:10de0019");
2736MODULE_ALIAS("snd-hda-codec-id:10de001a");
2737MODULE_ALIAS("snd-hda-codec-id:10de001b");
2738MODULE_ALIAS("snd-hda-codec-id:10de001c");
2739MODULE_ALIAS("snd-hda-codec-id:10de0040");
2740MODULE_ALIAS("snd-hda-codec-id:10de0041");
2741MODULE_ALIAS("snd-hda-codec-id:10de0042");
2742MODULE_ALIAS("snd-hda-codec-id:10de0043");
2743MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002744MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002745MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002746MODULE_ALIAS("snd-hda-codec-id:10de0067");
2747MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002748MODULE_ALIAS("snd-hda-codec-id:11069f80");
2749MODULE_ALIAS("snd-hda-codec-id:11069f81");
2750MODULE_ALIAS("snd-hda-codec-id:11069f84");
2751MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002752MODULE_ALIAS("snd-hda-codec-id:17e80047");
2753MODULE_ALIAS("snd-hda-codec-id:80860054");
2754MODULE_ALIAS("snd-hda-codec-id:80862801");
2755MODULE_ALIAS("snd-hda-codec-id:80862802");
2756MODULE_ALIAS("snd-hda-codec-id:80862803");
2757MODULE_ALIAS("snd-hda-codec-id:80862804");
2758MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002759MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002760MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002761MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002762MODULE_ALIAS("snd-hda-codec-id:808629fb");
2763
2764MODULE_LICENSE("GPL");
2765MODULE_DESCRIPTION("HDMI HD-audio codec");
2766MODULE_ALIAS("snd-hda-codec-intelhdmi");
2767MODULE_ALIAS("snd-hda-codec-nvhdmi");
2768MODULE_ALIAS("snd-hda-codec-atihdmi");
2769
2770static struct hda_codec_preset_list intel_list = {
2771 .preset = snd_hda_preset_hdmi,
2772 .owner = THIS_MODULE,
2773};
2774
2775static int __init patch_hdmi_init(void)
2776{
2777 return snd_hda_add_codec_preset(&intel_list);
2778}
2779
2780static void __exit patch_hdmi_exit(void)
2781{
2782 snd_hda_delete_codec_preset(&intel_list);
2783}
2784
2785module_init(patch_hdmi_init)
2786module_exit(patch_hdmi_exit)