blob: 8bf526ea8cf27919f0c3fd9fe35ffe3aa2247eee [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 Iwai2b203db2011-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 }
640
641 hdmi_debug_channel_mapping(codec, pin_nid);
642}
643
Takashi Iwaid45e6882012-07-31 11:36:00 +0200644struct channel_map_table {
645 unsigned char map; /* ALSA API channel map position */
646 unsigned char cea_slot; /* CEA slot value */
647 int spk_mask; /* speaker position bit mask */
648};
649
650static struct channel_map_table map_tables[] = {
651 { SNDRV_CHMAP_FL, 0x00, FL },
652 { SNDRV_CHMAP_FR, 0x01, FR },
653 { SNDRV_CHMAP_RL, 0x04, RL },
654 { SNDRV_CHMAP_RR, 0x05, RR },
655 { SNDRV_CHMAP_LFE, 0x02, LFE },
656 { SNDRV_CHMAP_FC, 0x03, FC },
657 { SNDRV_CHMAP_RLC, 0x06, RLC },
658 { SNDRV_CHMAP_RRC, 0x07, RRC },
659 {} /* terminator */
660};
661
662/* from ALSA API channel position to speaker bit mask */
663static int to_spk_mask(unsigned char c)
664{
665 struct channel_map_table *t = map_tables;
666 for (; t->map; t++) {
667 if (t->map == c)
668 return t->spk_mask;
669 }
670 return 0;
671}
672
673/* from ALSA API channel position to CEA slot */
674static int to_cea_slot(unsigned char c)
675{
676 struct channel_map_table *t = map_tables;
677 for (; t->map; t++) {
678 if (t->map == c)
679 return t->cea_slot;
680 }
Anssi Hannula11f7c522013-10-05 02:25:41 +0300681 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200682}
683
684/* from CEA slot to ALSA API channel position */
685static int from_cea_slot(unsigned char c)
686{
687 struct channel_map_table *t = map_tables;
688 for (; t->map; t++) {
689 if (t->cea_slot == c)
690 return t->map;
691 }
692 return 0;
693}
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
706/* get the CA index corresponding to the given ALSA API channel map */
707static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
708{
709 int i, spks = 0, spk_mask = 0;
710
711 for (i = 0; i < chs; i++) {
712 int mask = to_spk_mask(map[i]);
713 if (mask) {
714 spk_mask |= mask;
715 spks++;
716 }
717 }
718
719 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
720 if ((chs == channel_allocations[i].channels ||
721 spks == channel_allocations[i].channels) &&
722 (spk_mask & channel_allocations[i].spk_mask) ==
723 channel_allocations[i].spk_mask)
724 return channel_allocations[i].ca_index;
725 }
726 return -1;
727}
728
729/* set up the channel slots for the given ALSA API channel map */
730static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
731 hda_nid_t pin_nid,
732 int chs, unsigned char *map)
733{
Anssi Hannula11f7c522013-10-05 02:25:41 +0300734 int alsa_pos, hdmi_slot;
735 int assignments[8] = {[0 ... 7] = 0xf};
736
737 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
738
739 hdmi_slot = to_cea_slot(map[alsa_pos]);
740
741 if (hdmi_slot < 0)
742 continue; /* unassigned channel */
743
744 assignments[hdmi_slot] = alsa_pos;
745 }
746
747 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200748 int val, err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300749
750 val = (assignments[hdmi_slot] << 4) | hdmi_slot;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200751 err = snd_hda_codec_write(codec, pin_nid, 0,
752 AC_VERB_SET_HDMI_CHAN_SLOT, val);
753 if (err)
754 return -EINVAL;
755 }
756 return 0;
757}
758
759/* store ALSA API channel map from the current default map */
760static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
761{
762 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300763 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200764 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300765 if (i < channel_allocations[ordered_ca].channels)
766 map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200767 else
768 map[i] = 0;
769 }
770}
771
772static void hdmi_setup_channel_mapping(struct hda_codec *codec,
773 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200774 int channels, unsigned char *map,
775 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200776{
Anssi Hannula20608732013-02-03 17:55:45 +0200777 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200778 hdmi_manual_setup_channel_mapping(codec, pin_nid,
779 channels, map);
780 } else {
781 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
782 hdmi_setup_fake_chmap(map, ca);
783 }
784}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800785
786/*
787 * Audio InfoFrame routines
788 */
789
790/*
791 * Enable Audio InfoFrame Transmission
792 */
793static void hdmi_start_infoframe_trans(struct hda_codec *codec,
794 hda_nid_t pin_nid)
795{
796 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
797 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
798 AC_DIPXMIT_BEST);
799}
800
801/*
802 * Disable Audio InfoFrame Transmission
803 */
804static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
805 hda_nid_t pin_nid)
806{
807 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
808 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
809 AC_DIPXMIT_DISABLE);
810}
811
812static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
813{
814#ifdef CONFIG_SND_DEBUG_VERBOSE
815 int i;
816 int size;
817
818 size = snd_hdmi_get_eld_size(codec, pin_nid);
819 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
820
821 for (i = 0; i < 8; i++) {
822 size = snd_hda_codec_read(codec, pin_nid, 0,
823 AC_VERB_GET_HDMI_DIP_SIZE, i);
824 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
825 }
826#endif
827}
828
829static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
830{
831#ifdef BE_PARANOID
832 int i, j;
833 int size;
834 int pi, bi;
835 for (i = 0; i < 8; i++) {
836 size = snd_hda_codec_read(codec, pin_nid, 0,
837 AC_VERB_GET_HDMI_DIP_SIZE, i);
838 if (size == 0)
839 continue;
840
841 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
842 for (j = 1; j < 1000; j++) {
843 hdmi_write_dip_byte(codec, pin_nid, 0x0);
844 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
845 if (pi != i)
846 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
847 bi, pi, i);
848 if (bi == 0) /* byte index wrapped around */
849 break;
850 }
851 snd_printd(KERN_INFO
852 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
853 i, size, j);
854 }
855#endif
856}
857
Wu Fengguang53d7d692010-09-21 14:25:49 +0800858static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800859{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800860 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800861 u8 sum = 0;
862 int i;
863
Wu Fengguang53d7d692010-09-21 14:25:49 +0800864 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800865
Wu Fengguang53d7d692010-09-21 14:25:49 +0800866 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800867 sum += bytes[i];
868
Wu Fengguang53d7d692010-09-21 14:25:49 +0800869 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800870}
871
872static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
873 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800874 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876 int i;
877
878 hdmi_debug_dip_size(codec, pin_nid);
879 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
880
Wu Fengguang079d88c2010-03-08 10:44:23 +0800881 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800882 for (i = 0; i < size; i++)
883 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800884}
885
886static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800887 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800888{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800889 u8 val;
890 int i;
891
892 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
893 != AC_DIPXMIT_BEST)
894 return false;
895
896 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800897 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800898 val = snd_hda_codec_read(codec, pin_nid, 0,
899 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800900 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800901 return false;
902 }
903
904 return true;
905}
906
Takashi Iwaib0540872013-09-02 12:33:02 +0200907static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
908 struct hdmi_spec_per_pin *per_pin,
909 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800910{
Stephen Warren384a48d2011-06-01 11:14:21 -0600911 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200912 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300913 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600914 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300915 int ca, ordered_ca;
Takashi Iwai2b203db2011-02-11 12:17:30 +0100916 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800917
Takashi Iwaib0540872013-09-02 12:33:02 +0200918 if (!channels)
919 return;
920
Mengdong Lin58f7d282013-09-04 16:37:12 -0400921 if (is_haswell(codec))
922 snd_hda_codec_write(codec, pin_nid, 0,
923 AC_VERB_SET_AMP_GAIN_MUTE,
924 AMP_OUT_UNMUTE);
925
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100926 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600927 if (!eld->monitor_present)
928 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800929
Takashi Iwaid45e6882012-07-31 11:36:00 +0200930 if (!non_pcm && per_pin->chmap_set)
931 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
932 else
933 ca = hdmi_channel_allocation(eld, channels);
934 if (ca < 0)
935 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800936
Anssi Hannula1df5a062013-10-05 02:25:40 +0300937 ordered_ca = get_channel_allocation_order(ca);
938 active_channels = channel_allocations[ordered_ca].channels;
939
940 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
941
Stephen Warren384a48d2011-06-01 11:14:21 -0600942 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100943 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600944 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800945
Stephen Warren384a48d2011-06-01 11:14:21 -0600946 hdmi_ai->type = 0x84;
947 hdmi_ai->ver = 0x01;
948 hdmi_ai->len = 0x0a;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300949 hdmi_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600950 hdmi_ai->CA = ca;
951 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100952 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600953 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800954
Stephen Warren384a48d2011-06-01 11:14:21 -0600955 dp_ai->type = 0x84;
956 dp_ai->len = 0x1b;
957 dp_ai->ver = 0x11 << 2;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300958 dp_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600959 dp_ai->CA = ca;
960 } else {
961 snd_printd("HDMI: unknown connection type at pin %d\n",
962 pin_nid);
963 return;
964 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800965
Stephen Warren384a48d2011-06-01 11:14:21 -0600966 /*
967 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
968 * sizeof(*dp_ai) to avoid partial match/update problems when
969 * the user switches between HDMI/DP monitors.
970 */
971 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
972 sizeof(ai))) {
973 snd_printdd("hdmi_setup_audio_infoframe: "
974 "pin=%d channels=%d\n",
975 pin_nid,
Anssi Hannula1df5a062013-10-05 02:25:40 +0300976 active_channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200977 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200978 channels, per_pin->chmap,
979 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600980 hdmi_stop_infoframe_trans(codec, pin_nid);
981 hdmi_fill_audio_infoframe(codec, pin_nid,
982 ai.bytes, sizeof(ai));
983 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800984 } else {
985 /* For non-pcm audio switch, setup new channel mapping
986 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200987 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200988 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200989 channels, per_pin->chmap,
990 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800991 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800992
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200993 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800994}
995
996
997/*
998 * Unsolicited events
999 */
1000
Wu Fengguangc6e84532011-11-18 16:59:32 -06001001static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001002
Wu Fengguang079d88c2010-03-08 10:44:23 +08001003static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1004{
1005 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +02001006 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1007 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001008 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +02001009 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001010 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001011
1012 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1013 if (!jack)
1014 return;
1015 pin_nid = jack->nid;
1016 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001017
Fengguang Wufae3d882012-04-10 17:00:35 +08001018 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001019 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1020 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001021 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001022
Stephen Warren384a48d2011-06-01 11:14:21 -06001023 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1024 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001025 return;
1026
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001027 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001028 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001029}
1030
1031static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1032{
1033 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1034 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1035 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1036 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1037
1038 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001039 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001040 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001041 tag,
1042 subtag,
1043 cp_state,
1044 cp_ready);
1045
1046 /* TODO */
1047 if (cp_state)
1048 ;
1049 if (cp_ready)
1050 ;
1051}
1052
1053
1054static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1055{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001056 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1057 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1058
Takashi Iwai3a938972011-10-28 01:16:55 +02001059 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001060 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1061 return;
1062 }
1063
1064 if (subtag == 0)
1065 hdmi_intrinsic_event(codec, res);
1066 else
1067 hdmi_non_intrinsic_event(codec, res);
1068}
1069
Mengdong Lin58f7d282013-09-04 16:37:12 -04001070static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001071 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001072{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001073 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001074
Wang Xingchao53b434f2013-06-18 10:41:53 +08001075 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1076 * thus pins could only choose converter 0 for use. Make sure the
1077 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001078 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001079 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1080
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001081 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001082 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1083 AC_PWRST_D0);
1084 msleep(40);
1085 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1086 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1087 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1088 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001089}
1090
Wu Fengguang079d88c2010-03-08 10:44:23 +08001091/*
1092 * Callbacks
1093 */
1094
Takashi Iwai92f10b32010-08-03 14:21:00 +02001095/* HBR should be Non-PCM, 8 channels */
1096#define is_hbr_format(format) \
1097 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1098
Stephen Warren384a48d2011-06-01 11:14:21 -06001099static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1100 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001101{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001102 int pinctl;
1103 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001104
Mengdong Linfb87fa32013-09-04 16:36:57 -04001105 if (is_haswell(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001106 haswell_verify_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001107
Stephen Warren384a48d2011-06-01 11:14:21 -06001108 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1109 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001110 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1111
1112 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001113 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001114 new_pinctl |= AC_PINCTL_EPT_HBR;
1115 else
1116 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1117
1118 snd_printdd("hdmi_setup_stream: "
1119 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001120 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001121 pinctl == new_pinctl ? "" : "new-",
1122 new_pinctl);
1123
1124 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001125 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001126 AC_VERB_SET_PIN_WIDGET_CONTROL,
1127 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001128
Stephen Warren384a48d2011-06-01 11:14:21 -06001129 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001130 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001131 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1132 return -EINVAL;
1133 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001134
Stephen Warren384a48d2011-06-01 11:14:21 -06001135 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001136 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001137}
1138
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001139static int hdmi_choose_cvt(struct hda_codec *codec,
1140 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001141{
1142 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001143 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001144 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001145 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001146
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001147 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001148
Stephen Warren384a48d2011-06-01 11:14:21 -06001149 /* Dynamically assign converter to stream */
1150 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001151 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001152
1153 /* Must not already be assigned */
1154 if (per_cvt->assigned)
1155 continue;
1156 /* Must be in pin's mux's list of converters */
1157 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1158 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1159 break;
1160 /* Not in mux list */
1161 if (mux_idx == per_pin->num_mux_nids)
1162 continue;
1163 break;
1164 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001165
Stephen Warren384a48d2011-06-01 11:14:21 -06001166 /* No free converters */
1167 if (cvt_idx == spec->num_cvts)
1168 return -ENODEV;
1169
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001170 if (cvt_id)
1171 *cvt_id = cvt_idx;
1172 if (mux_id)
1173 *mux_id = mux_idx;
1174
1175 return 0;
1176}
1177
1178static void haswell_config_cvts(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001179 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001180{
1181 struct hdmi_spec *spec = codec->spec;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001182 hda_nid_t nid, end_nid;
1183 int cvt_idx, curr;
1184 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001185
Mengdong Linf82d7d12013-09-21 20:34:45 -04001186 /* configure all pins, including "no physical connection" ones */
1187 end_nid = codec->start_nid + codec->num_nodes;
1188 for (nid = codec->start_nid; nid < end_nid; nid++) {
1189 unsigned int wid_caps = get_wcaps(codec, nid);
1190 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001191
Mengdong Linf82d7d12013-09-21 20:34:45 -04001192 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001193 continue;
1194
Mengdong Linf82d7d12013-09-21 20:34:45 -04001195 if (nid == pin_nid)
1196 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001197
Mengdong Linf82d7d12013-09-21 20:34:45 -04001198 curr = snd_hda_codec_read(codec, nid, 0,
1199 AC_VERB_GET_CONNECT_SEL, 0);
1200 if (curr != mux_idx)
1201 continue;
1202
1203 /* choose an unassigned converter. The conveters in the
1204 * connection list are in the same order as in the codec.
1205 */
1206 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1207 per_cvt = get_cvt(spec, cvt_idx);
1208 if (!per_cvt->assigned) {
1209 snd_printdd("choose cvt %d for pin nid %d\n",
1210 cvt_idx, nid);
1211 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001212 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001213 cvt_idx);
1214 break;
1215 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001216 }
1217 }
1218}
1219
1220/*
1221 * HDA PCM callbacks
1222 */
1223static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1224 struct hda_codec *codec,
1225 struct snd_pcm_substream *substream)
1226{
1227 struct hdmi_spec *spec = codec->spec;
1228 struct snd_pcm_runtime *runtime = substream->runtime;
1229 int pin_idx, cvt_idx, mux_idx = 0;
1230 struct hdmi_spec_per_pin *per_pin;
1231 struct hdmi_eld *eld;
1232 struct hdmi_spec_per_cvt *per_cvt = NULL;
1233 int err;
1234
1235 /* Validate hinfo */
1236 pin_idx = hinfo_to_pin_index(spec, hinfo);
1237 if (snd_BUG_ON(pin_idx < 0))
1238 return -EINVAL;
1239 per_pin = get_pin(spec, pin_idx);
1240 eld = &per_pin->sink_eld;
1241
1242 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1243 if (err < 0)
1244 return err;
1245
1246 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001247 /* Claim converter */
1248 per_cvt->assigned = 1;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001249 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001250 hinfo->nid = per_cvt->cvt_nid;
1251
Takashi Iwaibddee962013-06-18 16:14:22 +02001252 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001253 AC_VERB_SET_CONNECT_SEL,
1254 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001255
1256 /* configure unused pins to choose other converters */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001257 if (is_haswell(codec))
Mengdong Linf82d7d12013-09-21 20:34:45 -04001258 haswell_config_cvts(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001259
Stephen Warren384a48d2011-06-01 11:14:21 -06001260 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001261
Stephen Warren2def8172011-06-01 11:14:20 -06001262 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001263 hinfo->channels_min = per_cvt->channels_min;
1264 hinfo->channels_max = per_cvt->channels_max;
1265 hinfo->rates = per_cvt->rates;
1266 hinfo->formats = per_cvt->formats;
1267 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001268
Stephen Warren384a48d2011-06-01 11:14:21 -06001269 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001270 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001271 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001272 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001273 !hinfo->rates || !hinfo->formats) {
1274 per_cvt->assigned = 0;
1275 hinfo->nid = 0;
1276 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001277 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001278 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001279 }
Stephen Warren2def8172011-06-01 11:14:20 -06001280
1281 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001282 runtime->hw.channels_min = hinfo->channels_min;
1283 runtime->hw.channels_max = hinfo->channels_max;
1284 runtime->hw.formats = hinfo->formats;
1285 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001286
1287 snd_pcm_hw_constraint_step(substream->runtime, 0,
1288 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001289 return 0;
1290}
1291
1292/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001293 * HDA/HDMI auto parsing
1294 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001295static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001296{
1297 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001298 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001299 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001300
1301 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1302 snd_printk(KERN_WARNING
1303 "HDMI: pin %d wcaps %#x "
1304 "does not support connection list\n",
1305 pin_nid, get_wcaps(codec, pin_nid));
1306 return -EINVAL;
1307 }
1308
Stephen Warren384a48d2011-06-01 11:14:21 -06001309 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1310 per_pin->mux_nids,
1311 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001312
1313 return 0;
1314}
1315
Wu Fengguangc6e84532011-11-18 16:59:32 -06001316static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001317{
Wu Fengguang744626d2011-11-16 16:29:47 +08001318 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001319 struct hdmi_spec *spec = codec->spec;
1320 struct hdmi_eld *eld = &spec->temp_eld;
1321 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001322 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001323 /*
1324 * Always execute a GetPinSense verb here, even when called from
1325 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1326 * response's PD bit is not the real PD value, but indicates that
1327 * the real PD value changed. An older version of the HD-audio
1328 * specification worked this way. Hence, we just ignore the data in
1329 * the unsolicited response to avoid custom WARs.
1330 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001331 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001332 bool update_eld = false;
1333 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001334
David Henningsson4bd038f2013-02-19 16:11:25 +01001335 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1336 if (pin_eld->monitor_present)
1337 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1338 else
1339 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001340
Fengguang Wufae3d882012-04-10 17:00:35 +08001341 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001342 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001343 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001344
David Henningsson4bd038f2013-02-19 16:11:25 +01001345 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001346 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1347 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001348 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001349 else {
1350 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1351 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1352 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001353 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001354 }
1355
David Henningsson4bd038f2013-02-19 16:11:25 +01001356 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001357 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001358 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001359 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001360 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001361 queue_delayed_work(codec->bus->workq,
1362 &per_pin->work,
1363 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001364 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001365 }
1366 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001367
1368 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001369 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001370 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001371 eld_changed = true;
1372 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001373 if (update_eld) {
Takashi Iwaib0540872013-09-02 12:33:02 +02001374 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001375 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001376 eld_changed = pin_eld->eld_size != eld->eld_size ||
1377 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001378 eld->eld_size) != 0;
1379 if (eld_changed)
1380 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1381 eld->eld_size);
1382 pin_eld->eld_size = eld->eld_size;
1383 pin_eld->info = eld->info;
Takashi Iwaib0540872013-09-02 12:33:02 +02001384
1385 /* Haswell-specific workaround: re-setup when the transcoder is
1386 * changed during the stream playback
1387 */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001388 if (is_haswell(codec) &&
Mengdong Lin58f7d282013-09-04 16:37:12 -04001389 eld->eld_valid && !old_eld_valid && per_pin->setup)
Takashi Iwaib0540872013-09-02 12:33:02 +02001390 hdmi_setup_audio_infoframe(codec, per_pin,
1391 per_pin->non_pcm);
David Henningsson4bd038f2013-02-19 16:11:25 +01001392 }
1393 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001394
1395 if (eld_changed)
1396 snd_ctl_notify(codec->bus->card,
1397 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1398 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001399}
1400
Wu Fengguang744626d2011-11-16 16:29:47 +08001401static void hdmi_repoll_eld(struct work_struct *work)
1402{
1403 struct hdmi_spec_per_pin *per_pin =
1404 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1405
Wu Fengguangc6e84532011-11-18 16:59:32 -06001406 if (per_pin->repoll_count++ > 6)
1407 per_pin->repoll_count = 0;
1408
1409 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001410}
1411
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001412static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1413 hda_nid_t nid);
1414
Wu Fengguang079d88c2010-03-08 10:44:23 +08001415static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1416{
1417 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001418 unsigned int caps, config;
1419 int pin_idx;
1420 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001421 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001422
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001423 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001424 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1425 return 0;
1426
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001427 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001428 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1429 return 0;
1430
Mengdong Linfb87fa32013-09-04 16:36:57 -04001431 if (is_haswell(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001432 intel_haswell_fixup_connect_list(codec, pin_nid);
1433
Stephen Warren384a48d2011-06-01 11:14:21 -06001434 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001435 per_pin = snd_array_new(&spec->pins);
1436 if (!per_pin)
1437 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001438
1439 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001440 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001441
Stephen Warren384a48d2011-06-01 11:14:21 -06001442 err = hdmi_read_pin_conn(codec, pin_idx);
1443 if (err < 0)
1444 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001445
Wu Fengguang079d88c2010-03-08 10:44:23 +08001446 spec->num_pins++;
1447
Stephen Warren384a48d2011-06-01 11:14:21 -06001448 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001449}
1450
Stephen Warren384a48d2011-06-01 11:14:21 -06001451static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001452{
1453 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001454 struct hdmi_spec_per_cvt *per_cvt;
1455 unsigned int chans;
1456 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001457
Stephen Warren384a48d2011-06-01 11:14:21 -06001458 chans = get_wcaps(codec, cvt_nid);
1459 chans = get_wcaps_channels(chans);
1460
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001461 per_cvt = snd_array_new(&spec->cvts);
1462 if (!per_cvt)
1463 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001464
1465 per_cvt->cvt_nid = cvt_nid;
1466 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001467 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001468 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001469 if (chans > spec->channels_max)
1470 spec->channels_max = chans;
1471 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001472
1473 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1474 &per_cvt->rates,
1475 &per_cvt->formats,
1476 &per_cvt->maxbps);
1477 if (err < 0)
1478 return err;
1479
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001480 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1481 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1482 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001483
1484 return 0;
1485}
1486
1487static int hdmi_parse_codec(struct hda_codec *codec)
1488{
1489 hda_nid_t nid;
1490 int i, nodes;
1491
1492 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1493 if (!nid || nodes < 0) {
1494 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1495 return -EINVAL;
1496 }
1497
1498 for (i = 0; i < nodes; i++, nid++) {
1499 unsigned int caps;
1500 unsigned int type;
1501
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001502 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001503 type = get_wcaps_type(caps);
1504
1505 if (!(caps & AC_WCAP_DIGITAL))
1506 continue;
1507
1508 switch (type) {
1509 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001510 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001511 break;
1512 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001513 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001514 break;
1515 }
1516 }
1517
David Henningssonc9adeef2012-11-07 09:22:33 +01001518#ifdef CONFIG_PM
1519 /* We're seeing some problems with unsolicited hot plug events on
1520 * PantherPoint after S3, if this is not enabled */
1521 if (codec->vendor_id == 0x80862806)
1522 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001523 /*
1524 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1525 * can be lost and presence sense verb will become inaccurate if the
1526 * HDA link is powered off at hot plug or hw initialization time.
1527 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001528 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001529 AC_PWRST_EPSS))
1530 codec->bus->power_keep_link_on = 1;
1531#endif
1532
1533 return 0;
1534}
1535
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001536/*
1537 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001538static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1539{
1540 struct hda_spdif_out *spdif;
1541 bool non_pcm;
1542
1543 mutex_lock(&codec->spdif_mutex);
1544 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1545 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1546 mutex_unlock(&codec->spdif_mutex);
1547 return non_pcm;
1548}
1549
1550
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001551/*
1552 * HDMI callbacks
1553 */
1554
1555static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1556 struct hda_codec *codec,
1557 unsigned int stream_tag,
1558 unsigned int format,
1559 struct snd_pcm_substream *substream)
1560{
Stephen Warren384a48d2011-06-01 11:14:21 -06001561 hda_nid_t cvt_nid = hinfo->nid;
1562 struct hdmi_spec *spec = codec->spec;
1563 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaib0540872013-09-02 12:33:02 +02001564 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1565 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001566 bool non_pcm;
1567
1568 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaib0540872013-09-02 12:33:02 +02001569 per_pin->channels = substream->runtime->channels;
1570 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001571
Takashi Iwaib0540872013-09-02 12:33:02 +02001572 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001573
1574 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001575}
1576
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001577static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1578 struct hda_codec *codec,
1579 struct snd_pcm_substream *substream)
1580{
1581 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1582 return 0;
1583}
1584
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001585static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1586 struct hda_codec *codec,
1587 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001588{
1589 struct hdmi_spec *spec = codec->spec;
1590 int cvt_idx, pin_idx;
1591 struct hdmi_spec_per_cvt *per_cvt;
1592 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001593
Stephen Warren384a48d2011-06-01 11:14:21 -06001594 if (hinfo->nid) {
1595 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1596 if (snd_BUG_ON(cvt_idx < 0))
1597 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001598 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001599
1600 snd_BUG_ON(!per_cvt->assigned);
1601 per_cvt->assigned = 0;
1602 hinfo->nid = 0;
1603
1604 pin_idx = hinfo_to_pin_index(spec, hinfo);
1605 if (snd_BUG_ON(pin_idx < 0))
1606 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001607 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001608
Stephen Warren384a48d2011-06-01 11:14:21 -06001609 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001610 per_pin->chmap_set = false;
1611 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001612
1613 per_pin->setup = false;
1614 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001615 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001616
Stephen Warren384a48d2011-06-01 11:14:21 -06001617 return 0;
1618}
1619
1620static const struct hda_pcm_ops generic_ops = {
1621 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001622 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001623 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001624 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001625};
1626
Takashi Iwaid45e6882012-07-31 11:36:00 +02001627/*
1628 * ALSA API channel-map control callbacks
1629 */
1630static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_info *uinfo)
1632{
1633 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1634 struct hda_codec *codec = info->private_data;
1635 struct hdmi_spec *spec = codec->spec;
1636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1637 uinfo->count = spec->channels_max;
1638 uinfo->value.integer.min = 0;
1639 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1640 return 0;
1641}
1642
1643static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1644 unsigned int size, unsigned int __user *tlv)
1645{
1646 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1647 struct hda_codec *codec = info->private_data;
1648 struct hdmi_spec *spec = codec->spec;
1649 const unsigned int valid_mask =
1650 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1651 unsigned int __user *dst;
1652 int chs, count = 0;
1653
1654 if (size < 8)
1655 return -ENOMEM;
1656 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1657 return -EFAULT;
1658 size -= 8;
1659 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001660 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001661 int i, c;
1662 struct cea_channel_speaker_allocation *cap;
1663 cap = channel_allocations;
1664 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1665 int chs_bytes = chs * 4;
1666 if (cap->channels != chs)
1667 continue;
1668 if (cap->spk_mask & ~valid_mask)
1669 continue;
1670 if (size < 8)
1671 return -ENOMEM;
1672 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1673 put_user(chs_bytes, dst + 1))
1674 return -EFAULT;
1675 dst += 2;
1676 size -= 8;
1677 count += 8;
1678 if (size < chs_bytes)
1679 return -ENOMEM;
1680 size -= chs_bytes;
1681 count += chs_bytes;
1682 for (c = 7; c >= 0; c--) {
1683 int spk = cap->speakers[c];
1684 if (!spk)
1685 continue;
1686 if (put_user(spk_to_chmap(spk), dst))
1687 return -EFAULT;
1688 dst++;
1689 }
1690 }
1691 }
1692 if (put_user(count, tlv + 1))
1693 return -EFAULT;
1694 return 0;
1695}
1696
1697static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol)
1699{
1700 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1701 struct hda_codec *codec = info->private_data;
1702 struct hdmi_spec *spec = codec->spec;
1703 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001704 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001705 int i;
1706
1707 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1708 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1709 return 0;
1710}
1711
1712static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_value *ucontrol)
1714{
1715 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1716 struct hda_codec *codec = info->private_data;
1717 struct hdmi_spec *spec = codec->spec;
1718 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001719 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001720 unsigned int ctl_idx;
1721 struct snd_pcm_substream *substream;
1722 unsigned char chmap[8];
1723 int i, ca, prepared = 0;
1724
1725 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1726 substream = snd_pcm_chmap_substream(info, ctl_idx);
1727 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001728 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001729 switch (substream->runtime->status->state) {
1730 case SNDRV_PCM_STATE_OPEN:
1731 case SNDRV_PCM_STATE_SETUP:
1732 break;
1733 case SNDRV_PCM_STATE_PREPARED:
1734 prepared = 1;
1735 break;
1736 default:
1737 return -EBUSY;
1738 }
1739 memset(chmap, 0, sizeof(chmap));
1740 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1741 chmap[i] = ucontrol->value.integer.value[i];
1742 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1743 return 0;
1744 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1745 if (ca < 0)
1746 return -EINVAL;
1747 per_pin->chmap_set = true;
1748 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1749 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02001750 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001751
1752 return 0;
1753}
1754
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001755static int generic_hdmi_build_pcms(struct hda_codec *codec)
1756{
1757 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001758 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001759
Stephen Warren384a48d2011-06-01 11:14:21 -06001760 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1761 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001762 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001763 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001764
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001765 per_pin = get_pin(spec, pin_idx);
1766 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1767 info = snd_array_new(&spec->pcm_rec);
1768 if (!info)
1769 return -ENOMEM;
1770 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001771 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001772 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001773
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001774 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001775 pstr->substreams = 1;
1776 pstr->ops = generic_ops;
1777 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001778 }
1779
Stephen Warren384a48d2011-06-01 11:14:21 -06001780 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001781 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001782
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001783 return 0;
1784}
1785
David Henningsson0b6c49b2011-08-23 16:56:03 +02001786static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1787{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001788 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001789 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001790 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1791 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001792
Takashi Iwai31ef2252011-12-01 17:41:36 +01001793 if (pcmdev > 0)
1794 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001795 if (!is_jack_detectable(codec, per_pin->pin_nid))
1796 strncat(hdmi_str, " Phantom",
1797 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001798
Takashi Iwai31ef2252011-12-01 17:41:36 +01001799 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001800}
1801
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001802static int generic_hdmi_build_controls(struct hda_codec *codec)
1803{
1804 struct hdmi_spec *spec = codec->spec;
1805 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001806 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001807
Stephen Warren384a48d2011-06-01 11:14:21 -06001808 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001809 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001810
1811 err = generic_hdmi_build_jack(codec, pin_idx);
1812 if (err < 0)
1813 return err;
1814
Takashi Iwaidcda5802012-10-12 17:24:51 +02001815 err = snd_hda_create_dig_out_ctls(codec,
1816 per_pin->pin_nid,
1817 per_pin->mux_nids[0],
1818 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001819 if (err < 0)
1820 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001821 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001822
1823 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001824 err = hdmi_create_eld_ctl(codec, pin_idx,
1825 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001826
1827 if (err < 0)
1828 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001829
Takashi Iwai82b1d732011-12-20 15:53:07 +01001830 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001831 }
1832
Takashi Iwaid45e6882012-07-31 11:36:00 +02001833 /* add channel maps */
1834 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1835 struct snd_pcm_chmap *chmap;
1836 struct snd_kcontrol *kctl;
1837 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02001838
1839 if (!codec->pcm_info[pin_idx].pcm)
1840 break;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001841 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1842 SNDRV_PCM_STREAM_PLAYBACK,
1843 NULL, 0, pin_idx, &chmap);
1844 if (err < 0)
1845 return err;
1846 /* override handlers */
1847 chmap->private_data = codec;
1848 kctl = chmap->kctl;
1849 for (i = 0; i < kctl->count; i++)
1850 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1851 kctl->info = hdmi_chmap_ctl_info;
1852 kctl->get = hdmi_chmap_ctl_get;
1853 kctl->put = hdmi_chmap_ctl_put;
1854 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1855 }
1856
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001857 return 0;
1858}
1859
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001860static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1861{
1862 struct hdmi_spec *spec = codec->spec;
1863 int pin_idx;
1864
1865 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001866 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001867 struct hdmi_eld *eld = &per_pin->sink_eld;
1868
1869 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001870 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001871 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1872 snd_hda_eld_proc_new(codec, eld, pin_idx);
1873 }
1874 return 0;
1875}
1876
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001877static int generic_hdmi_init(struct hda_codec *codec)
1878{
1879 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001880 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001881
Stephen Warren384a48d2011-06-01 11:14:21 -06001882 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001883 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001884 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001885
1886 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001887 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001888 }
1889 return 0;
1890}
1891
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001892static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1893{
1894 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1895 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1896 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1897}
1898
1899static void hdmi_array_free(struct hdmi_spec *spec)
1900{
1901 snd_array_free(&spec->pins);
1902 snd_array_free(&spec->cvts);
1903 snd_array_free(&spec->pcm_rec);
1904}
1905
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001906static void generic_hdmi_free(struct hda_codec *codec)
1907{
1908 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001909 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001910
Stephen Warren384a48d2011-06-01 11:14:21 -06001911 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001912 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001913 struct hdmi_eld *eld = &per_pin->sink_eld;
1914
Wu Fengguang744626d2011-11-16 16:29:47 +08001915 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001916 snd_hda_eld_proc_free(codec, eld);
1917 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001918
Wu Fengguang744626d2011-11-16 16:29:47 +08001919 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001920 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001921 kfree(spec);
1922}
1923
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001924#ifdef CONFIG_PM
1925static int generic_hdmi_resume(struct hda_codec *codec)
1926{
1927 struct hdmi_spec *spec = codec->spec;
1928 int pin_idx;
1929
1930 generic_hdmi_init(codec);
1931 snd_hda_codec_resume_amp(codec);
1932 snd_hda_codec_resume_cache(codec);
1933
1934 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1935 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1936 hdmi_present_sense(per_pin, 1);
1937 }
1938 return 0;
1939}
1940#endif
1941
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001942static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001943 .init = generic_hdmi_init,
1944 .free = generic_hdmi_free,
1945 .build_pcms = generic_hdmi_build_pcms,
1946 .build_controls = generic_hdmi_build_controls,
1947 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001948#ifdef CONFIG_PM
1949 .resume = generic_hdmi_resume,
1950#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001951};
1952
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001953
1954static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1955 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001956{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001957 struct hdmi_spec *spec = codec->spec;
1958 hda_nid_t conns[4];
1959 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001960
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001961 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1962 if (nconns == spec->num_cvts &&
1963 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001964 return;
1965
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001966 /* override pins connection list */
1967 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1968 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001969}
1970
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001971#define INTEL_VENDOR_NID 0x08
1972#define INTEL_GET_VENDOR_VERB 0xf81
1973#define INTEL_SET_VENDOR_VERB 0x781
1974#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1975#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1976
1977static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001978 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001979{
1980 unsigned int vendor_param;
1981
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001982 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1983 INTEL_GET_VENDOR_VERB, 0);
1984 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1985 return;
1986
1987 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1988 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1989 INTEL_SET_VENDOR_VERB, vendor_param);
1990 if (vendor_param == -1)
1991 return;
1992
Takashi Iwai17df3f52013-05-08 08:09:34 +02001993 if (update_tree)
1994 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001995}
1996
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001997static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1998{
1999 unsigned int vendor_param;
2000
2001 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2002 INTEL_GET_VENDOR_VERB, 0);
2003 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2004 return;
2005
2006 /* enable DP1.2 mode */
2007 vendor_param |= INTEL_EN_DP12;
2008 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2009 INTEL_SET_VENDOR_VERB, vendor_param);
2010}
2011
Takashi Iwai17df3f52013-05-08 08:09:34 +02002012/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2013 * Otherwise you may get severe h/w communication errors.
2014 */
2015static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2016 unsigned int power_state)
2017{
2018 if (power_state == AC_PWRST_D0) {
2019 intel_haswell_enable_all_pins(codec, false);
2020 intel_haswell_fixup_enable_dp12(codec);
2021 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002022
Takashi Iwai17df3f52013-05-08 08:09:34 +02002023 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2024 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2025}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002026
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002027static int patch_generic_hdmi(struct hda_codec *codec)
2028{
2029 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002030
2031 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2032 if (spec == NULL)
2033 return -ENOMEM;
2034
2035 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002036 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002037
Mengdong Linfb87fa32013-09-04 16:36:57 -04002038 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002039 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002040 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002041 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002042
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002043 if (hdmi_parse_codec(codec) < 0) {
2044 codec->spec = NULL;
2045 kfree(spec);
2046 return -EINVAL;
2047 }
2048 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Linfb87fa32013-09-04 16:36:57 -04002049 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002050 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002051 codec->dp_mst = true;
2052 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002053
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002054 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002055
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002056 init_channel_allocations();
2057
2058 return 0;
2059}
2060
2061/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002062 * Shared non-generic implementations
2063 */
2064
2065static int simple_playback_build_pcms(struct hda_codec *codec)
2066{
2067 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002068 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002069 unsigned int chans;
2070 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002071 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002072
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002073 per_cvt = get_cvt(spec, 0);
2074 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002075 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002076
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002077 info = snd_array_new(&spec->pcm_rec);
2078 if (!info)
2079 return -ENOMEM;
2080 info->name = get_pin(spec, 0)->pcm_name;
2081 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002082 info->pcm_type = HDA_PCM_TYPE_HDMI;
2083 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2084 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002085 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002086 if (pstr->channels_max <= 2 && chans && chans <= 16)
2087 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002088
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002089 codec->num_pcms = 1;
2090 codec->pcm_info = info;
2091
Stephen Warren3aaf8982011-06-01 11:14:19 -06002092 return 0;
2093}
2094
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002095/* unsolicited event for jack sensing */
2096static void simple_hdmi_unsol_event(struct hda_codec *codec,
2097 unsigned int res)
2098{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002099 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002100 snd_hda_jack_report_sync(codec);
2101}
2102
2103/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2104 * as long as spec->pins[] is set correctly
2105 */
2106#define simple_hdmi_build_jack generic_hdmi_build_jack
2107
Stephen Warren3aaf8982011-06-01 11:14:19 -06002108static int simple_playback_build_controls(struct hda_codec *codec)
2109{
2110 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002111 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002112 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002113
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002114 per_cvt = get_cvt(spec, 0);
2115 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2116 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002117 if (err < 0)
2118 return err;
2119 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002120}
2121
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002122static int simple_playback_init(struct hda_codec *codec)
2123{
2124 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002125 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2126 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002127
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002128 snd_hda_codec_write(codec, pin, 0,
2129 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2130 /* some codecs require to unmute the pin */
2131 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2132 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2133 AMP_OUT_UNMUTE);
2134 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002135 return 0;
2136}
2137
Stephen Warren3aaf8982011-06-01 11:14:19 -06002138static void simple_playback_free(struct hda_codec *codec)
2139{
2140 struct hdmi_spec *spec = codec->spec;
2141
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002142 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002143 kfree(spec);
2144}
2145
2146/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002147 * Nvidia specific implementations
2148 */
2149
2150#define Nv_VERB_SET_Channel_Allocation 0xF79
2151#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2152#define Nv_VERB_SET_Audio_Protection_On 0xF98
2153#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2154
2155#define nvhdmi_master_con_nid_7x 0x04
2156#define nvhdmi_master_pin_nid_7x 0x05
2157
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002158static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002159 /*front, rear, clfe, rear_surr */
2160 0x6, 0x8, 0xa, 0xc,
2161};
2162
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002163static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2164 /* set audio protect on */
2165 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2166 /* enable digital output on pin widget */
2167 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2168 {} /* terminator */
2169};
2170
2171static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002172 /* 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 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2177 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2178 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2179 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2180 {} /* terminator */
2181};
2182
2183#ifdef LIMITED_RATE_FMT_SUPPORT
2184/* support only the safe format and rate */
2185#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2186#define SUPPORTED_MAXBPS 16
2187#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2188#else
2189/* support all rates and formats */
2190#define SUPPORTED_RATES \
2191 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2192 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2193 SNDRV_PCM_RATE_192000)
2194#define SUPPORTED_MAXBPS 24
2195#define SUPPORTED_FORMATS \
2196 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2197#endif
2198
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002199static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002200{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002201 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2202 return 0;
2203}
2204
2205static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2206{
2207 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002208 return 0;
2209}
2210
Nitin Daga393004b2011-01-10 21:49:31 +05302211static unsigned int channels_2_6_8[] = {
2212 2, 6, 8
2213};
2214
2215static unsigned int channels_2_8[] = {
2216 2, 8
2217};
2218
2219static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2220 .count = ARRAY_SIZE(channels_2_6_8),
2221 .list = channels_2_6_8,
2222 .mask = 0,
2223};
2224
2225static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2226 .count = ARRAY_SIZE(channels_2_8),
2227 .list = channels_2_8,
2228 .mask = 0,
2229};
2230
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002231static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2232 struct hda_codec *codec,
2233 struct snd_pcm_substream *substream)
2234{
2235 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302236 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2237
2238 switch (codec->preset->id) {
2239 case 0x10de0002:
2240 case 0x10de0003:
2241 case 0x10de0005:
2242 case 0x10de0006:
2243 hw_constraints_channels = &hw_constraints_2_8_channels;
2244 break;
2245 case 0x10de0007:
2246 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2247 break;
2248 default:
2249 break;
2250 }
2251
2252 if (hw_constraints_channels != NULL) {
2253 snd_pcm_hw_constraint_list(substream->runtime, 0,
2254 SNDRV_PCM_HW_PARAM_CHANNELS,
2255 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002256 } else {
2257 snd_pcm_hw_constraint_step(substream->runtime, 0,
2258 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302259 }
2260
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002261 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2262}
2263
2264static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2265 struct hda_codec *codec,
2266 struct snd_pcm_substream *substream)
2267{
2268 struct hdmi_spec *spec = codec->spec;
2269 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2270}
2271
2272static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2273 struct hda_codec *codec,
2274 unsigned int stream_tag,
2275 unsigned int format,
2276 struct snd_pcm_substream *substream)
2277{
2278 struct hdmi_spec *spec = codec->spec;
2279 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2280 stream_tag, format, substream);
2281}
2282
Takashi Iwaid0b12522012-06-15 14:34:42 +02002283static const struct hda_pcm_stream simple_pcm_playback = {
2284 .substreams = 1,
2285 .channels_min = 2,
2286 .channels_max = 2,
2287 .ops = {
2288 .open = simple_playback_pcm_open,
2289 .close = simple_playback_pcm_close,
2290 .prepare = simple_playback_pcm_prepare
2291 },
2292};
2293
2294static const struct hda_codec_ops simple_hdmi_patch_ops = {
2295 .build_controls = simple_playback_build_controls,
2296 .build_pcms = simple_playback_build_pcms,
2297 .init = simple_playback_init,
2298 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002299 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002300};
2301
2302static int patch_simple_hdmi(struct hda_codec *codec,
2303 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2304{
2305 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002306 struct hdmi_spec_per_cvt *per_cvt;
2307 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002308
2309 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2310 if (!spec)
2311 return -ENOMEM;
2312
2313 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002314 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002315
2316 spec->multiout.num_dacs = 0; /* no analog */
2317 spec->multiout.max_channels = 2;
2318 spec->multiout.dig_out_nid = cvt_nid;
2319 spec->num_cvts = 1;
2320 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002321 per_pin = snd_array_new(&spec->pins);
2322 per_cvt = snd_array_new(&spec->cvts);
2323 if (!per_pin || !per_cvt) {
2324 simple_playback_free(codec);
2325 return -ENOMEM;
2326 }
2327 per_cvt->cvt_nid = cvt_nid;
2328 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002329 spec->pcm_playback = simple_pcm_playback;
2330
2331 codec->patch_ops = simple_hdmi_patch_ops;
2332
2333 return 0;
2334}
2335
Aaron Plattner1f348522011-04-06 17:19:04 -07002336static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2337 int channels)
2338{
2339 unsigned int chanmask;
2340 int chan = channels ? (channels - 1) : 1;
2341
2342 switch (channels) {
2343 default:
2344 case 0:
2345 case 2:
2346 chanmask = 0x00;
2347 break;
2348 case 4:
2349 chanmask = 0x08;
2350 break;
2351 case 6:
2352 chanmask = 0x0b;
2353 break;
2354 case 8:
2355 chanmask = 0x13;
2356 break;
2357 }
2358
2359 /* Set the audio infoframe channel allocation and checksum fields. The
2360 * channel count is computed implicitly by the hardware. */
2361 snd_hda_codec_write(codec, 0x1, 0,
2362 Nv_VERB_SET_Channel_Allocation, chanmask);
2363
2364 snd_hda_codec_write(codec, 0x1, 0,
2365 Nv_VERB_SET_Info_Frame_Checksum,
2366 (0x71 - chan - chanmask));
2367}
2368
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002369static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2370 struct hda_codec *codec,
2371 struct snd_pcm_substream *substream)
2372{
2373 struct hdmi_spec *spec = codec->spec;
2374 int i;
2375
2376 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2377 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2378 for (i = 0; i < 4; i++) {
2379 /* set the stream id */
2380 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2381 AC_VERB_SET_CHANNEL_STREAMID, 0);
2382 /* set the stream format */
2383 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2384 AC_VERB_SET_STREAM_FORMAT, 0);
2385 }
2386
Aaron Plattner1f348522011-04-06 17:19:04 -07002387 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2388 * streams are disabled. */
2389 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2390
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002391 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2392}
2393
2394static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2395 struct hda_codec *codec,
2396 unsigned int stream_tag,
2397 unsigned int format,
2398 struct snd_pcm_substream *substream)
2399{
2400 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002401 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002402 int i;
Stephen Warren7c9359762011-06-01 11:14:17 -06002403 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002404 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002405 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002406
2407 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002408 per_cvt = get_cvt(spec, 0);
2409 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002410
2411 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002412
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002413 dataDCC2 = 0x2;
2414
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002415 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c9359762011-06-01 11:14:17 -06002416 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002417 snd_hda_codec_write(codec,
2418 nvhdmi_master_con_nid_7x,
2419 0,
2420 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002421 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002422
2423 /* set the stream id */
2424 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2425 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2426
2427 /* set the stream format */
2428 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2429 AC_VERB_SET_STREAM_FORMAT, format);
2430
2431 /* turn on again (if needed) */
2432 /* enable and set the channel status audio/data flag */
Stephen Warren7c9359762011-06-01 11:14:17 -06002433 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002434 snd_hda_codec_write(codec,
2435 nvhdmi_master_con_nid_7x,
2436 0,
2437 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002438 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002439 snd_hda_codec_write(codec,
2440 nvhdmi_master_con_nid_7x,
2441 0,
2442 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2443 }
2444
2445 for (i = 0; i < 4; i++) {
2446 if (chs == 2)
2447 channel_id = 0;
2448 else
2449 channel_id = i * 2;
2450
2451 /* turn off SPDIF once;
2452 *otherwise the IEC958 bits won't be updated
2453 */
2454 if (codec->spdif_status_reset &&
Stephen Warren7c9359762011-06-01 11:14:17 -06002455 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002456 snd_hda_codec_write(codec,
2457 nvhdmi_con_nids_7x[i],
2458 0,
2459 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002460 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002461 /* set the stream id */
2462 snd_hda_codec_write(codec,
2463 nvhdmi_con_nids_7x[i],
2464 0,
2465 AC_VERB_SET_CHANNEL_STREAMID,
2466 (stream_tag << 4) | channel_id);
2467 /* set the stream format */
2468 snd_hda_codec_write(codec,
2469 nvhdmi_con_nids_7x[i],
2470 0,
2471 AC_VERB_SET_STREAM_FORMAT,
2472 format);
2473 /* turn on again (if needed) */
2474 /* enable and set the channel status audio/data flag */
2475 if (codec->spdif_status_reset &&
Stephen Warren7c9359762011-06-01 11:14:17 -06002476 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002477 snd_hda_codec_write(codec,
2478 nvhdmi_con_nids_7x[i],
2479 0,
2480 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c9359762011-06-01 11:14:17 -06002481 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002482 snd_hda_codec_write(codec,
2483 nvhdmi_con_nids_7x[i],
2484 0,
2485 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2486 }
2487 }
2488
Aaron Plattner1f348522011-04-06 17:19:04 -07002489 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002490
2491 mutex_unlock(&codec->spdif_mutex);
2492 return 0;
2493}
2494
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002495static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002496 .substreams = 1,
2497 .channels_min = 2,
2498 .channels_max = 8,
2499 .nid = nvhdmi_master_con_nid_7x,
2500 .rates = SUPPORTED_RATES,
2501 .maxbps = SUPPORTED_MAXBPS,
2502 .formats = SUPPORTED_FORMATS,
2503 .ops = {
2504 .open = simple_playback_pcm_open,
2505 .close = nvhdmi_8ch_7x_pcm_close,
2506 .prepare = nvhdmi_8ch_7x_pcm_prepare
2507 },
2508};
2509
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002510static int patch_nvhdmi_2ch(struct hda_codec *codec)
2511{
2512 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002513 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2514 nvhdmi_master_pin_nid_7x);
2515 if (err < 0)
2516 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002517
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002518 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002519 /* override the PCM rates, etc, as the codec doesn't give full list */
2520 spec = codec->spec;
2521 spec->pcm_playback.rates = SUPPORTED_RATES;
2522 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2523 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002524 return 0;
2525}
2526
Takashi Iwai53775b02012-08-01 12:17:41 +02002527static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2528{
2529 struct hdmi_spec *spec = codec->spec;
2530 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002531 if (!err) {
2532 struct hda_pcm *info = get_pcm_rec(spec, 0);
2533 info->own_chmap = true;
2534 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002535 return err;
2536}
2537
2538static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2539{
2540 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002541 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002542 struct snd_pcm_chmap *chmap;
2543 int err;
2544
2545 err = simple_playback_build_controls(codec);
2546 if (err < 0)
2547 return err;
2548
2549 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002550 info = get_pcm_rec(spec, 0);
2551 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002552 SNDRV_PCM_STREAM_PLAYBACK,
2553 snd_pcm_alt_chmaps, 8, 0, &chmap);
2554 if (err < 0)
2555 return err;
2556 switch (codec->preset->id) {
2557 case 0x10de0002:
2558 case 0x10de0003:
2559 case 0x10de0005:
2560 case 0x10de0006:
2561 chmap->channel_mask = (1U << 2) | (1U << 8);
2562 break;
2563 case 0x10de0007:
2564 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2565 }
2566 return 0;
2567}
2568
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002569static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2570{
2571 struct hdmi_spec *spec;
2572 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002573 if (err < 0)
2574 return err;
2575 spec = codec->spec;
2576 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002577 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002578 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002579 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2580 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002581
2582 /* Initialize the audio infoframe channel mask and checksum to something
2583 * valid */
2584 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2585
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002586 return 0;
2587}
2588
2589/*
2590 * ATI-specific implementations
2591 *
2592 * FIXME: we may omit the whole this and use the generic code once after
2593 * it's confirmed to work.
2594 */
2595
2596#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2597#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2598
2599static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2600 struct hda_codec *codec,
2601 unsigned int stream_tag,
2602 unsigned int format,
2603 struct snd_pcm_substream *substream)
2604{
2605 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002606 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002607 int chans = substream->runtime->channels;
2608 int i, err;
2609
2610 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2611 substream);
2612 if (err < 0)
2613 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002614 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002615 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002616 /* FIXME: XXX */
2617 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002618 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002619 AC_VERB_SET_HDMI_CHAN_SLOT,
2620 (i << 4) | i);
2621 }
2622 return 0;
2623}
2624
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002625static int patch_atihdmi(struct hda_codec *codec)
2626{
2627 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002628 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2629 if (err < 0)
2630 return err;
2631 spec = codec->spec;
2632 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002633 return 0;
2634}
2635
Annie Liu3de5ff82012-06-08 19:18:42 +08002636/* VIA HDMI Implementation */
2637#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2638#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2639
Annie Liu3de5ff82012-06-08 19:18:42 +08002640static int patch_via_hdmi(struct hda_codec *codec)
2641{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002642 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002643}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002644
2645/*
2646 * patch entries
2647 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002648static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002649{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2650{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2651{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002652{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002653{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2654{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2655{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2656{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2657{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2658{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2659{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2660{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002661{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2662{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2663{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2664{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2665{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2666{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2667{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2668{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2669{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2670{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2671{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002672/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002673{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2674{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2675{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2676{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2677{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2678{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2679{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2680{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2681{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2682{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002683{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002684{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002685{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2686{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002687{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2688{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2689{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2690{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002691{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2692{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2693{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2694{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2695{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2696{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002697{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002698{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002699{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002700{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2701{} /* terminator */
2702};
2703
2704MODULE_ALIAS("snd-hda-codec-id:1002793c");
2705MODULE_ALIAS("snd-hda-codec-id:10027919");
2706MODULE_ALIAS("snd-hda-codec-id:1002791a");
2707MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2708MODULE_ALIAS("snd-hda-codec-id:10951390");
2709MODULE_ALIAS("snd-hda-codec-id:10951392");
2710MODULE_ALIAS("snd-hda-codec-id:10de0002");
2711MODULE_ALIAS("snd-hda-codec-id:10de0003");
2712MODULE_ALIAS("snd-hda-codec-id:10de0005");
2713MODULE_ALIAS("snd-hda-codec-id:10de0006");
2714MODULE_ALIAS("snd-hda-codec-id:10de0007");
2715MODULE_ALIAS("snd-hda-codec-id:10de000a");
2716MODULE_ALIAS("snd-hda-codec-id:10de000b");
2717MODULE_ALIAS("snd-hda-codec-id:10de000c");
2718MODULE_ALIAS("snd-hda-codec-id:10de000d");
2719MODULE_ALIAS("snd-hda-codec-id:10de0010");
2720MODULE_ALIAS("snd-hda-codec-id:10de0011");
2721MODULE_ALIAS("snd-hda-codec-id:10de0012");
2722MODULE_ALIAS("snd-hda-codec-id:10de0013");
2723MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002724MODULE_ALIAS("snd-hda-codec-id:10de0015");
2725MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002726MODULE_ALIAS("snd-hda-codec-id:10de0018");
2727MODULE_ALIAS("snd-hda-codec-id:10de0019");
2728MODULE_ALIAS("snd-hda-codec-id:10de001a");
2729MODULE_ALIAS("snd-hda-codec-id:10de001b");
2730MODULE_ALIAS("snd-hda-codec-id:10de001c");
2731MODULE_ALIAS("snd-hda-codec-id:10de0040");
2732MODULE_ALIAS("snd-hda-codec-id:10de0041");
2733MODULE_ALIAS("snd-hda-codec-id:10de0042");
2734MODULE_ALIAS("snd-hda-codec-id:10de0043");
2735MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002736MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002737MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002738MODULE_ALIAS("snd-hda-codec-id:10de0067");
2739MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002740MODULE_ALIAS("snd-hda-codec-id:11069f80");
2741MODULE_ALIAS("snd-hda-codec-id:11069f81");
2742MODULE_ALIAS("snd-hda-codec-id:11069f84");
2743MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002744MODULE_ALIAS("snd-hda-codec-id:17e80047");
2745MODULE_ALIAS("snd-hda-codec-id:80860054");
2746MODULE_ALIAS("snd-hda-codec-id:80862801");
2747MODULE_ALIAS("snd-hda-codec-id:80862802");
2748MODULE_ALIAS("snd-hda-codec-id:80862803");
2749MODULE_ALIAS("snd-hda-codec-id:80862804");
2750MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002751MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002752MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002753MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002754MODULE_ALIAS("snd-hda-codec-id:808629fb");
2755
2756MODULE_LICENSE("GPL");
2757MODULE_DESCRIPTION("HDMI HD-audio codec");
2758MODULE_ALIAS("snd-hda-codec-intelhdmi");
2759MODULE_ALIAS("snd-hda-codec-nvhdmi");
2760MODULE_ALIAS("snd-hda-codec-atihdmi");
2761
2762static struct hda_codec_preset_list intel_list = {
2763 .preset = snd_hda_preset_hdmi,
2764 .owner = THIS_MODULE,
2765};
2766
2767static int __init patch_hdmi_init(void)
2768{
2769 return snd_hda_add_codec_preset(&intel_list);
2770}
2771
2772static void __exit patch_hdmi_exit(void)
2773{
2774 snd_hda_delete_codec_preset(&intel_list);
2775}
2776
2777module_init(patch_hdmi_init)
2778module_exit(patch_hdmi_exit)