blob: 22b50899b15107b2aea6c6b1e0776b25267db89b [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Stephen Warren384a48d2011-06-01 11:14:21 -060047struct hdmi_spec_per_cvt {
48 hda_nid_t cvt_nid;
49 int assigned;
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
56
Takashi Iwai4eea3092013-02-07 18:18:19 +010057/* max. connections to a widget */
58#define HDA_MAX_CONNECTIONS 32
59
Stephen Warren384a48d2011-06-01 11:14:21 -060060struct hdmi_spec_per_pin {
61 hda_nid_t pin_nid;
62 int num_mux_nids;
63 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080064
65 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060066 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080067 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010068 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060069 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020070 bool setup; /* the stream has been set up by prepare callback */
71 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020072 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020073 bool chmap_set; /* channel-map override by ALSA API? */
74 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010075 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060076};
77
Wu Fengguang079d88c2010-03-08 10:44:23 +080078struct hdmi_spec {
79 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010080 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
81 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060082
Wu Fengguang079d88c2010-03-08 10:44:23 +080083 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010084 struct snd_array pins; /* struct hdmi_spec_per_pin */
85 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020086 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080087
David Henningsson4bd038f2013-02-19 16:11:25 +010088 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060090 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080091 */
92 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020093 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080094};
95
96
97struct hdmi_audio_infoframe {
98 u8 type; /* 0x84 */
99 u8 ver; /* 0x01 */
100 u8 len; /* 0x0a */
101
Wu Fengguang53d7d692010-09-21 14:25:49 +0800102 u8 checksum;
103
Wu Fengguang079d88c2010-03-08 10:44:23 +0800104 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
105 u8 SS01_SF24;
106 u8 CXT04;
107 u8 CA;
108 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800109};
110
111struct dp_audio_infoframe {
112 u8 type; /* 0x84 */
113 u8 len; /* 0x1b */
114 u8 ver; /* 0x11 << 2 */
115
116 u8 CC02_CT47; /* match with HDMI infoframe from this on */
117 u8 SS01_SF24;
118 u8 CXT04;
119 u8 CA;
120 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800121};
122
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100123union audio_infoframe {
124 struct hdmi_audio_infoframe hdmi;
125 struct dp_audio_infoframe dp;
126 u8 bytes[0];
127};
128
Wu Fengguang079d88c2010-03-08 10:44:23 +0800129/*
130 * CEA speaker placement:
131 *
132 * FLH FCH FRH
133 * FLW FL FLC FC FRC FR FRW
134 *
135 * LFE
136 * TC
137 *
138 * RL RLC RC RRC RR
139 *
140 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
141 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
142 */
143enum cea_speaker_placement {
144 FL = (1 << 0), /* Front Left */
145 FC = (1 << 1), /* Front Center */
146 FR = (1 << 2), /* Front Right */
147 FLC = (1 << 3), /* Front Left Center */
148 FRC = (1 << 4), /* Front Right Center */
149 RL = (1 << 5), /* Rear Left */
150 RC = (1 << 6), /* Rear Center */
151 RR = (1 << 7), /* Rear Right */
152 RLC = (1 << 8), /* Rear Left Center */
153 RRC = (1 << 9), /* Rear Right Center */
154 LFE = (1 << 10), /* Low Frequency Effect */
155 FLW = (1 << 11), /* Front Left Wide */
156 FRW = (1 << 12), /* Front Right Wide */
157 FLH = (1 << 13), /* Front Left High */
158 FCH = (1 << 14), /* Front Center High */
159 FRH = (1 << 15), /* Front Right High */
160 TC = (1 << 16), /* Top Center */
161};
162
163/*
164 * ELD SA bits in the CEA Speaker Allocation data block
165 */
166static int eld_speaker_allocation_bits[] = {
167 [0] = FL | FR,
168 [1] = LFE,
169 [2] = FC,
170 [3] = RL | RR,
171 [4] = RC,
172 [5] = FLC | FRC,
173 [6] = RLC | RRC,
174 /* the following are not defined in ELD yet */
175 [7] = FLW | FRW,
176 [8] = FLH | FRH,
177 [9] = TC,
178 [10] = FCH,
179};
180
181struct cea_channel_speaker_allocation {
182 int ca_index;
183 int speakers[8];
184
185 /* derived values, just for convenience */
186 int channels;
187 int spk_mask;
188};
189
190/*
191 * ALSA sequence is:
192 *
193 * surround40 surround41 surround50 surround51 surround71
194 * ch0 front left = = = =
195 * ch1 front right = = = =
196 * ch2 rear left = = = =
197 * ch3 rear right = = = =
198 * ch4 LFE center center center
199 * ch5 LFE LFE
200 * ch6 side left
201 * ch7 side right
202 *
203 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
204 */
205static int hdmi_channel_mapping[0x32][8] = {
206 /* stereo */
207 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
208 /* 2.1 */
209 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
210 /* Dolby Surround */
211 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
212 /* surround40 */
213 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
214 /* 4ch */
215 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
216 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800217 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800218 /* surround50 */
219 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
220 /* surround51 */
221 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
222 /* 7.1 */
223 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
224};
225
226/*
227 * This is an ordered list!
228 *
229 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800230 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800231 */
232static struct cea_channel_speaker_allocation channel_allocations[] = {
233/* channel: 7 6 5 4 3 2 1 0 */
234{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
235 /* 2.1 */
236{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
237 /* Dolby Surround */
238{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
239 /* surround40 */
240{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
241 /* surround41 */
242{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
243 /* surround50 */
244{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
245 /* surround51 */
246{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
247 /* 6.1 */
248{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
249 /* surround71 */
250{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
251
252{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
253{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
254{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
255{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
256{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
257{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
258{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
259{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
260{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
261{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
262{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
263{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
264{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
265{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
266{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
267{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
268{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
269{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
270{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
271{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
272{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
273{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
274{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
275{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
276{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
277{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
278{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
279{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
280{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
281{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
282{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
283{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
284{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
285{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
286{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
287{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
291{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
292{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
293};
294
295
296/*
297 * HDMI routines
298 */
299
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100300#define get_pin(spec, idx) \
301 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
302#define get_cvt(spec, idx) \
303 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
304#define get_pcm_rec(spec, idx) \
305 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
306
Stephen Warren384a48d2011-06-01 11:14:21 -0600307static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308{
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800310
Stephen Warren384a48d2011-06-01 11:14:21 -0600311 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100312 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800314
Stephen Warren384a48d2011-06-01 11:14:21 -0600315 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
316 return -EINVAL;
317}
318
319static int hinfo_to_pin_index(struct hdmi_spec *spec,
320 struct hda_pcm_stream *hinfo)
321{
322 int pin_idx;
323
324 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100325 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600326 return pin_idx;
327
328 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
329 return -EINVAL;
330}
331
332static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
333{
334 int cvt_idx;
335
336 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100337 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600338 return cvt_idx;
339
340 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800341 return -EINVAL;
342}
343
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500344static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
345 struct snd_ctl_elem_info *uinfo)
346{
347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100348 struct hdmi_spec *spec = codec->spec;
349 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500350 int pin_idx;
351
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500352 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
353
354 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100355 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100356
David Henningsson4bd038f2013-02-19 16:11:25 +0100357 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100358 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100359 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500360
361 return 0;
362}
363
364static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
365 struct snd_ctl_elem_value *ucontrol)
366{
367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100368 struct hdmi_spec *spec = codec->spec;
369 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500370 int pin_idx;
371
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500372 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100373 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500374
David Henningsson4bd038f2013-02-19 16:11:25 +0100375 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100376 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100377 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100378 snd_BUG();
379 return -EINVAL;
380 }
381
382 memset(ucontrol->value.bytes.data, 0,
383 ARRAY_SIZE(ucontrol->value.bytes.data));
384 if (eld->eld_valid)
385 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
386 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100387 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500388
389 return 0;
390}
391
392static struct snd_kcontrol_new eld_bytes_ctl = {
393 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
394 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
395 .name = "ELD",
396 .info = hdmi_eld_ctl_info,
397 .get = hdmi_eld_ctl_get,
398};
399
400static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
401 int device)
402{
403 struct snd_kcontrol *kctl;
404 struct hdmi_spec *spec = codec->spec;
405 int err;
406
407 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
408 if (!kctl)
409 return -ENOMEM;
410 kctl->private_value = pin_idx;
411 kctl->id.device = device;
412
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100413 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500414 if (err < 0)
415 return err;
416
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100417 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500418 return 0;
419}
420
Wu Fengguang079d88c2010-03-08 10:44:23 +0800421#ifdef BE_PARANOID
422static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
423 int *packet_index, int *byte_index)
424{
425 int val;
426
427 val = snd_hda_codec_read(codec, pin_nid, 0,
428 AC_VERB_GET_HDMI_DIP_INDEX, 0);
429
430 *packet_index = val >> 5;
431 *byte_index = val & 0x1f;
432}
433#endif
434
435static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
436 int packet_index, int byte_index)
437{
438 int val;
439
440 val = (packet_index << 5) | (byte_index & 0x1f);
441
442 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
443}
444
445static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
446 unsigned char val)
447{
448 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
449}
450
Stephen Warren384a48d2011-06-01 11:14:21 -0600451static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800452{
453 /* Unmute */
454 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
455 snd_hda_codec_write(codec, pin_nid, 0,
456 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100457 /* Enable pin out: some machines with GM965 gets broken output when
458 * the pin is disabled or changed while using with HDMI
459 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800460 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100461 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800462}
463
Stephen Warren384a48d2011-06-01 11:14:21 -0600464static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800465{
Stephen Warren384a48d2011-06-01 11:14:21 -0600466 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800467 AC_VERB_GET_CVT_CHAN_COUNT, 0);
468}
469
470static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600471 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800472{
Stephen Warren384a48d2011-06-01 11:14:21 -0600473 if (chs != hdmi_get_channel_count(codec, cvt_nid))
474 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800475 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
476}
477
478
479/*
480 * Channel mapping routines
481 */
482
483/*
484 * Compute derived values in channel_allocations[].
485 */
486static void init_channel_allocations(void)
487{
488 int i, j;
489 struct cea_channel_speaker_allocation *p;
490
491 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
492 p = channel_allocations + i;
493 p->channels = 0;
494 p->spk_mask = 0;
495 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
496 if (p->speakers[j]) {
497 p->channels++;
498 p->spk_mask |= p->speakers[j];
499 }
500 }
501}
502
Wang Xingchao72357c72012-09-06 10:02:36 +0800503static int get_channel_allocation_order(int ca)
504{
505 int i;
506
507 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
508 if (channel_allocations[i].ca_index == ca)
509 break;
510 }
511 return i;
512}
513
Wu Fengguang079d88c2010-03-08 10:44:23 +0800514/*
515 * The transformation takes two steps:
516 *
517 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
518 * spk_mask => (channel_allocations[]) => ai->CA
519 *
520 * TODO: it could select the wrong CA from multiple candidates.
521*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600522static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800523{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800524 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800525 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800526 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
528
529 /*
530 * CA defaults to 0 for basic stereo audio
531 */
532 if (channels <= 2)
533 return 0;
534
Wu Fengguang079d88c2010-03-08 10:44:23 +0800535 /*
536 * expand ELD's speaker allocation mask
537 *
538 * ELD tells the speaker mask in a compact(paired) form,
539 * expand ELD's notions to match the ones used by Audio InfoFrame.
540 */
541 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100542 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800543 spk_mask |= eld_speaker_allocation_bits[i];
544 }
545
546 /* search for the first working match in the CA table */
547 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
548 if (channels == channel_allocations[i].channels &&
549 (spk_mask & channel_allocations[i].spk_mask) ==
550 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800551 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800552 break;
553 }
554 }
555
Anssi Hannula18e39182013-09-01 14:36:47 +0300556 if (!ca) {
557 /* if there was no match, select the regular ALSA channel
558 * allocation with the matching number of channels */
559 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
560 if (channels == channel_allocations[i].channels) {
561 ca = channel_allocations[i].ca_index;
562 break;
563 }
564 }
565 }
566
David Henningsson1613d6b2013-02-19 16:11:24 +0100567 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800568 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800569 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570
Wu Fengguang53d7d692010-09-21 14:25:49 +0800571 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800572}
573
574static void hdmi_debug_channel_mapping(struct hda_codec *codec,
575 hda_nid_t pin_nid)
576{
577#ifdef CONFIG_SND_DEBUG_VERBOSE
578 int i;
579 int slot;
580
581 for (i = 0; i < 8; i++) {
582 slot = snd_hda_codec_read(codec, pin_nid, 0,
583 AC_VERB_GET_HDMI_CHAN_SLOT, i);
584 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
585 slot >> 4, slot & 0xf);
586 }
587#endif
588}
589
590
Takashi Iwaid45e6882012-07-31 11:36:00 +0200591static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800592 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800593 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800594 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595{
596 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800597 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800598 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800599 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600
Wang Xingchao72357c72012-09-06 10:02:36 +0800601 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800602
Wu Fengguang079d88c2010-03-08 10:44:23 +0800603 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800604 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800605 hdmi_channel_mapping[ca][i] = i | (i << 4);
606 for (; i < 8; i++)
607 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
608 }
609
Wang Xingchao433968d2012-09-06 10:02:37 +0800610 if (non_pcm) {
611 for (i = 0; i < channel_allocations[order].channels; i++)
612 non_pcm_mapping[i] = i | (i << 4);
613 for (; i < 8; i++)
614 non_pcm_mapping[i] = 0xf | (i << 4);
615 }
616
Wu Fengguang079d88c2010-03-08 10:44:23 +0800617 for (i = 0; i < 8; i++) {
618 err = snd_hda_codec_write(codec, pin_nid, 0,
619 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800620 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800621 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800622 snd_printdd(KERN_NOTICE
623 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800624 break;
625 }
626 }
627
628 hdmi_debug_channel_mapping(codec, pin_nid);
629}
630
Takashi Iwaid45e6882012-07-31 11:36:00 +0200631struct channel_map_table {
632 unsigned char map; /* ALSA API channel map position */
633 unsigned char cea_slot; /* CEA slot value */
634 int spk_mask; /* speaker position bit mask */
635};
636
637static struct channel_map_table map_tables[] = {
638 { SNDRV_CHMAP_FL, 0x00, FL },
639 { SNDRV_CHMAP_FR, 0x01, FR },
640 { SNDRV_CHMAP_RL, 0x04, RL },
641 { SNDRV_CHMAP_RR, 0x05, RR },
642 { SNDRV_CHMAP_LFE, 0x02, LFE },
643 { SNDRV_CHMAP_FC, 0x03, FC },
644 { SNDRV_CHMAP_RLC, 0x06, RLC },
645 { SNDRV_CHMAP_RRC, 0x07, RRC },
646 {} /* terminator */
647};
648
649/* from ALSA API channel position to speaker bit mask */
650static int to_spk_mask(unsigned char c)
651{
652 struct channel_map_table *t = map_tables;
653 for (; t->map; t++) {
654 if (t->map == c)
655 return t->spk_mask;
656 }
657 return 0;
658}
659
660/* from ALSA API channel position to CEA slot */
661static int to_cea_slot(unsigned char c)
662{
663 struct channel_map_table *t = map_tables;
664 for (; t->map; t++) {
665 if (t->map == c)
666 return t->cea_slot;
667 }
668 return 0x0f;
669}
670
671/* from CEA slot to ALSA API channel position */
672static int from_cea_slot(unsigned char c)
673{
674 struct channel_map_table *t = map_tables;
675 for (; t->map; t++) {
676 if (t->cea_slot == c)
677 return t->map;
678 }
679 return 0;
680}
681
682/* from speaker bit mask to ALSA API channel position */
683static int spk_to_chmap(int spk)
684{
685 struct channel_map_table *t = map_tables;
686 for (; t->map; t++) {
687 if (t->spk_mask == spk)
688 return t->map;
689 }
690 return 0;
691}
692
693/* get the CA index corresponding to the given ALSA API channel map */
694static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
695{
696 int i, spks = 0, spk_mask = 0;
697
698 for (i = 0; i < chs; i++) {
699 int mask = to_spk_mask(map[i]);
700 if (mask) {
701 spk_mask |= mask;
702 spks++;
703 }
704 }
705
706 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
707 if ((chs == channel_allocations[i].channels ||
708 spks == channel_allocations[i].channels) &&
709 (spk_mask & channel_allocations[i].spk_mask) ==
710 channel_allocations[i].spk_mask)
711 return channel_allocations[i].ca_index;
712 }
713 return -1;
714}
715
716/* set up the channel slots for the given ALSA API channel map */
717static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
718 hda_nid_t pin_nid,
719 int chs, unsigned char *map)
720{
721 int i;
722 for (i = 0; i < 8; i++) {
723 int val, err;
724 if (i < chs)
725 val = to_cea_slot(map[i]);
726 else
727 val = 0xf;
728 val |= (i << 4);
729 err = snd_hda_codec_write(codec, pin_nid, 0,
730 AC_VERB_SET_HDMI_CHAN_SLOT, val);
731 if (err)
732 return -EINVAL;
733 }
734 return 0;
735}
736
737/* store ALSA API channel map from the current default map */
738static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
739{
740 int i;
741 for (i = 0; i < 8; i++) {
742 if (i < channel_allocations[ca].channels)
743 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
744 else
745 map[i] = 0;
746 }
747}
748
749static void hdmi_setup_channel_mapping(struct hda_codec *codec,
750 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200751 int channels, unsigned char *map,
752 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200753{
Anssi Hannula20608732013-02-03 17:55:45 +0200754 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200755 hdmi_manual_setup_channel_mapping(codec, pin_nid,
756 channels, map);
757 } else {
758 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
759 hdmi_setup_fake_chmap(map, ca);
760 }
761}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800762
763/*
764 * Audio InfoFrame routines
765 */
766
767/*
768 * Enable Audio InfoFrame Transmission
769 */
770static void hdmi_start_infoframe_trans(struct hda_codec *codec,
771 hda_nid_t pin_nid)
772{
773 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
774 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
775 AC_DIPXMIT_BEST);
776}
777
778/*
779 * Disable Audio InfoFrame Transmission
780 */
781static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
782 hda_nid_t pin_nid)
783{
784 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
785 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
786 AC_DIPXMIT_DISABLE);
787}
788
789static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
790{
791#ifdef CONFIG_SND_DEBUG_VERBOSE
792 int i;
793 int size;
794
795 size = snd_hdmi_get_eld_size(codec, pin_nid);
796 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
797
798 for (i = 0; i < 8; i++) {
799 size = snd_hda_codec_read(codec, pin_nid, 0,
800 AC_VERB_GET_HDMI_DIP_SIZE, i);
801 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
802 }
803#endif
804}
805
806static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
807{
808#ifdef BE_PARANOID
809 int i, j;
810 int size;
811 int pi, bi;
812 for (i = 0; i < 8; i++) {
813 size = snd_hda_codec_read(codec, pin_nid, 0,
814 AC_VERB_GET_HDMI_DIP_SIZE, i);
815 if (size == 0)
816 continue;
817
818 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
819 for (j = 1; j < 1000; j++) {
820 hdmi_write_dip_byte(codec, pin_nid, 0x0);
821 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
822 if (pi != i)
823 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
824 bi, pi, i);
825 if (bi == 0) /* byte index wrapped around */
826 break;
827 }
828 snd_printd(KERN_INFO
829 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
830 i, size, j);
831 }
832#endif
833}
834
Wu Fengguang53d7d692010-09-21 14:25:49 +0800835static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800836{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800837 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800838 u8 sum = 0;
839 int i;
840
Wu Fengguang53d7d692010-09-21 14:25:49 +0800841 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800842
Wu Fengguang53d7d692010-09-21 14:25:49 +0800843 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800844 sum += bytes[i];
845
Wu Fengguang53d7d692010-09-21 14:25:49 +0800846 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800847}
848
849static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
850 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800851 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800853 int i;
854
855 hdmi_debug_dip_size(codec, pin_nid);
856 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
857
Wu Fengguang079d88c2010-03-08 10:44:23 +0800858 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800859 for (i = 0; i < size; i++)
860 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800861}
862
863static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800864 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800865{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800866 u8 val;
867 int i;
868
869 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
870 != AC_DIPXMIT_BEST)
871 return false;
872
873 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800874 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875 val = snd_hda_codec_read(codec, pin_nid, 0,
876 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800877 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800878 return false;
879 }
880
881 return true;
882}
883
Takashi Iwaib0540872013-09-02 12:33:02 +0200884static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
885 struct hdmi_spec_per_pin *per_pin,
886 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800887{
Stephen Warren384a48d2011-06-01 11:14:21 -0600888 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200889 int channels = per_pin->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800891 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100892 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800893
Takashi Iwaib0540872013-09-02 12:33:02 +0200894 if (!channels)
895 return;
896
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100897 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600898 if (!eld->monitor_present)
899 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800900
Takashi Iwaid45e6882012-07-31 11:36:00 +0200901 if (!non_pcm && per_pin->chmap_set)
902 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
903 else
904 ca = hdmi_channel_allocation(eld, channels);
905 if (ca < 0)
906 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800907
Stephen Warren384a48d2011-06-01 11:14:21 -0600908 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100909 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600910 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800911
Stephen Warren384a48d2011-06-01 11:14:21 -0600912 hdmi_ai->type = 0x84;
913 hdmi_ai->ver = 0x01;
914 hdmi_ai->len = 0x0a;
915 hdmi_ai->CC02_CT47 = channels - 1;
916 hdmi_ai->CA = ca;
917 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100918 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600919 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800920
Stephen Warren384a48d2011-06-01 11:14:21 -0600921 dp_ai->type = 0x84;
922 dp_ai->len = 0x1b;
923 dp_ai->ver = 0x11 << 2;
924 dp_ai->CC02_CT47 = channels - 1;
925 dp_ai->CA = ca;
926 } else {
927 snd_printd("HDMI: unknown connection type at pin %d\n",
928 pin_nid);
929 return;
930 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800931
Stephen Warren384a48d2011-06-01 11:14:21 -0600932 /*
933 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
934 * sizeof(*dp_ai) to avoid partial match/update problems when
935 * the user switches between HDMI/DP monitors.
936 */
937 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
938 sizeof(ai))) {
939 snd_printdd("hdmi_setup_audio_infoframe: "
940 "pin=%d channels=%d\n",
941 pin_nid,
942 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200943 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200944 channels, per_pin->chmap,
945 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600946 hdmi_stop_infoframe_trans(codec, pin_nid);
947 hdmi_fill_audio_infoframe(codec, pin_nid,
948 ai.bytes, sizeof(ai));
949 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800950 } else {
951 /* For non-pcm audio switch, setup new channel mapping
952 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200953 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200954 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200955 channels, per_pin->chmap,
956 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800957 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800958
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200959 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800960}
961
962
963/*
964 * Unsolicited events
965 */
966
Wu Fengguangc6e84532011-11-18 16:59:32 -0600967static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200968
Wu Fengguang079d88c2010-03-08 10:44:23 +0800969static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
970{
971 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200972 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
973 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600974 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200975 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400976 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +0200977
978 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
979 if (!jack)
980 return;
981 pin_nid = jack->nid;
982 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800983
Fengguang Wufae3d882012-04-10 17:00:35 +0800984 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -0400985 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
986 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +0800987 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800988
Stephen Warren384a48d2011-06-01 11:14:21 -0600989 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
990 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800991 return;
992
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100993 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200994 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800995}
996
997static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
998{
999 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1000 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1001 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1002 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1003
1004 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001005 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001006 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001007 tag,
1008 subtag,
1009 cp_state,
1010 cp_ready);
1011
1012 /* TODO */
1013 if (cp_state)
1014 ;
1015 if (cp_ready)
1016 ;
1017}
1018
1019
1020static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1021{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001022 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1023 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1024
Takashi Iwai3a938972011-10-28 01:16:55 +02001025 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001026 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1027 return;
1028 }
1029
1030 if (subtag == 0)
1031 hdmi_intrinsic_event(codec, res);
1032 else
1033 hdmi_non_intrinsic_event(codec, res);
1034}
1035
Wang Xingchao53b434f2013-06-18 10:41:53 +08001036static void haswell_verify_pin_D0(struct hda_codec *codec,
1037 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001038{
1039 int pwr, lamp, ramp;
1040
Wang Xingchao53b434f2013-06-18 10:41:53 +08001041 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1042 * thus pins could only choose converter 0 for use. Make sure the
1043 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001044 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001045 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1046
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001047 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001048 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1049 AC_PWRST_D0);
1050 msleep(40);
1051 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1052 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1053 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1054 }
1055
1056 lamp = snd_hda_codec_read(codec, nid, 0,
1057 AC_VERB_GET_AMP_GAIN_MUTE,
1058 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1059 ramp = snd_hda_codec_read(codec, nid, 0,
1060 AC_VERB_GET_AMP_GAIN_MUTE,
1061 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1062 if (lamp != ramp) {
1063 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1064 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1065
1066 lamp = snd_hda_codec_read(codec, nid, 0,
1067 AC_VERB_GET_AMP_GAIN_MUTE,
1068 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1069 ramp = snd_hda_codec_read(codec, nid, 0,
1070 AC_VERB_GET_AMP_GAIN_MUTE,
1071 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1072 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1073 }
1074}
1075
Wu Fengguang079d88c2010-03-08 10:44:23 +08001076/*
1077 * Callbacks
1078 */
1079
Takashi Iwai92f10b32010-08-03 14:21:00 +02001080/* HBR should be Non-PCM, 8 channels */
1081#define is_hbr_format(format) \
1082 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1083
Stephen Warren384a48d2011-06-01 11:14:21 -06001084static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1085 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001086{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001087 int pinctl;
1088 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001089
David Henningsson83f26ad2013-04-10 12:26:07 +02001090 if (codec->vendor_id == 0x80862807)
Wang Xingchao53b434f2013-06-18 10:41:53 +08001091 haswell_verify_pin_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001092
Stephen Warren384a48d2011-06-01 11:14:21 -06001093 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1094 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001095 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1096
1097 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001098 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001099 new_pinctl |= AC_PINCTL_EPT_HBR;
1100 else
1101 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1102
1103 snd_printdd("hdmi_setup_stream: "
1104 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001105 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001106 pinctl == new_pinctl ? "" : "new-",
1107 new_pinctl);
1108
1109 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001110 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001111 AC_VERB_SET_PIN_WIDGET_CONTROL,
1112 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001113
Stephen Warren384a48d2011-06-01 11:14:21 -06001114 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001115 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001116 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1117 return -EINVAL;
1118 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001119
Stephen Warren384a48d2011-06-01 11:14:21 -06001120 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001121 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001122}
1123
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001124static int hdmi_choose_cvt(struct hda_codec *codec,
1125 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001126{
1127 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001128 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001129 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001130 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001131
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001132 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001133
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 /* Dynamically assign converter to stream */
1135 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001136 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001137
1138 /* Must not already be assigned */
1139 if (per_cvt->assigned)
1140 continue;
1141 /* Must be in pin's mux's list of converters */
1142 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1143 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1144 break;
1145 /* Not in mux list */
1146 if (mux_idx == per_pin->num_mux_nids)
1147 continue;
1148 break;
1149 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001150
Stephen Warren384a48d2011-06-01 11:14:21 -06001151 /* No free converters */
1152 if (cvt_idx == spec->num_cvts)
1153 return -ENODEV;
1154
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001155 if (cvt_id)
1156 *cvt_id = cvt_idx;
1157 if (mux_id)
1158 *mux_id = mux_idx;
1159
1160 return 0;
1161}
1162
1163static void haswell_config_cvts(struct hda_codec *codec,
1164 int pin_id, int mux_id)
1165{
1166 struct hdmi_spec *spec = codec->spec;
1167 struct hdmi_spec_per_pin *per_pin;
1168 int pin_idx, mux_idx;
1169 int curr;
1170 int err;
1171
1172 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1173 per_pin = get_pin(spec, pin_idx);
1174
1175 if (pin_idx == pin_id)
1176 continue;
1177
1178 curr = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1179 AC_VERB_GET_CONNECT_SEL, 0);
1180
1181 /* Choose another unused converter */
1182 if (curr == mux_id) {
1183 err = hdmi_choose_cvt(codec, pin_idx, NULL, &mux_idx);
1184 if (err < 0)
1185 return;
1186 snd_printdd("HDMI: choose converter %d for pin %d\n", mux_idx, pin_idx);
1187 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1188 AC_VERB_SET_CONNECT_SEL,
1189 mux_idx);
1190 }
1191 }
1192}
1193
1194/*
1195 * HDA PCM callbacks
1196 */
1197static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1198 struct hda_codec *codec,
1199 struct snd_pcm_substream *substream)
1200{
1201 struct hdmi_spec *spec = codec->spec;
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 int pin_idx, cvt_idx, mux_idx = 0;
1204 struct hdmi_spec_per_pin *per_pin;
1205 struct hdmi_eld *eld;
1206 struct hdmi_spec_per_cvt *per_cvt = NULL;
1207 int err;
1208
1209 /* Validate hinfo */
1210 pin_idx = hinfo_to_pin_index(spec, hinfo);
1211 if (snd_BUG_ON(pin_idx < 0))
1212 return -EINVAL;
1213 per_pin = get_pin(spec, pin_idx);
1214 eld = &per_pin->sink_eld;
1215
1216 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1217 if (err < 0)
1218 return err;
1219
1220 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001221 /* Claim converter */
1222 per_cvt->assigned = 1;
1223 hinfo->nid = per_cvt->cvt_nid;
1224
Takashi Iwaibddee962013-06-18 16:14:22 +02001225 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001226 AC_VERB_SET_CONNECT_SEL,
1227 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001228
1229 /* configure unused pins to choose other converters */
1230 if (codec->vendor_id == 0x80862807)
1231 haswell_config_cvts(codec, pin_idx, mux_idx);
1232
Stephen Warren384a48d2011-06-01 11:14:21 -06001233 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001234
Stephen Warren2def8172011-06-01 11:14:20 -06001235 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001236 hinfo->channels_min = per_cvt->channels_min;
1237 hinfo->channels_max = per_cvt->channels_max;
1238 hinfo->rates = per_cvt->rates;
1239 hinfo->formats = per_cvt->formats;
1240 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001241
Stephen Warren384a48d2011-06-01 11:14:21 -06001242 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001243 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001244 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001245 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001246 !hinfo->rates || !hinfo->formats) {
1247 per_cvt->assigned = 0;
1248 hinfo->nid = 0;
1249 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001250 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001251 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001252 }
Stephen Warren2def8172011-06-01 11:14:20 -06001253
1254 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001255 runtime->hw.channels_min = hinfo->channels_min;
1256 runtime->hw.channels_max = hinfo->channels_max;
1257 runtime->hw.formats = hinfo->formats;
1258 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001259
1260 snd_pcm_hw_constraint_step(substream->runtime, 0,
1261 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001262 return 0;
1263}
1264
1265/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001266 * HDA/HDMI auto parsing
1267 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001268static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001269{
1270 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001271 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001272 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001273
1274 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1275 snd_printk(KERN_WARNING
1276 "HDMI: pin %d wcaps %#x "
1277 "does not support connection list\n",
1278 pin_nid, get_wcaps(codec, pin_nid));
1279 return -EINVAL;
1280 }
1281
Stephen Warren384a48d2011-06-01 11:14:21 -06001282 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1283 per_pin->mux_nids,
1284 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001285
1286 return 0;
1287}
1288
Wu Fengguangc6e84532011-11-18 16:59:32 -06001289static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001290{
Wu Fengguang744626d2011-11-16 16:29:47 +08001291 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001292 struct hdmi_spec *spec = codec->spec;
1293 struct hdmi_eld *eld = &spec->temp_eld;
1294 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001295 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001296 /*
1297 * Always execute a GetPinSense verb here, even when called from
1298 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1299 * response's PD bit is not the real PD value, but indicates that
1300 * the real PD value changed. An older version of the HD-audio
1301 * specification worked this way. Hence, we just ignore the data in
1302 * the unsolicited response to avoid custom WARs.
1303 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001304 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001305 bool update_eld = false;
1306 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001307
David Henningsson4bd038f2013-02-19 16:11:25 +01001308 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1309 if (pin_eld->monitor_present)
1310 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1311 else
1312 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001313
Fengguang Wufae3d882012-04-10 17:00:35 +08001314 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001315 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001316 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001317
David Henningsson4bd038f2013-02-19 16:11:25 +01001318 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001319 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1320 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001321 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001322 else {
1323 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1324 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1325 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001326 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001327 }
1328
David Henningsson4bd038f2013-02-19 16:11:25 +01001329 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001330 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001331 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001332 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001333 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001334 queue_delayed_work(codec->bus->workq,
1335 &per_pin->work,
1336 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001337 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001338 }
1339 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001340
1341 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001342 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001343 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001344 eld_changed = true;
1345 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001346 if (update_eld) {
Takashi Iwaib0540872013-09-02 12:33:02 +02001347 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001348 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001349 eld_changed = pin_eld->eld_size != eld->eld_size ||
1350 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001351 eld->eld_size) != 0;
1352 if (eld_changed)
1353 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1354 eld->eld_size);
1355 pin_eld->eld_size = eld->eld_size;
1356 pin_eld->info = eld->info;
Takashi Iwaib0540872013-09-02 12:33:02 +02001357
1358 /* Haswell-specific workaround: re-setup when the transcoder is
1359 * changed during the stream playback
1360 */
1361 if (codec->vendor_id == 0x80862807 &&
1362 eld->eld_valid && !old_eld_valid && per_pin->setup) {
1363 snd_hda_codec_write(codec, pin_nid, 0,
1364 AC_VERB_SET_AMP_GAIN_MUTE,
1365 AMP_OUT_UNMUTE);
1366 hdmi_setup_audio_infoframe(codec, per_pin,
1367 per_pin->non_pcm);
1368 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001369 }
1370 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001371
1372 if (eld_changed)
1373 snd_ctl_notify(codec->bus->card,
1374 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1375 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001376}
1377
Wu Fengguang744626d2011-11-16 16:29:47 +08001378static void hdmi_repoll_eld(struct work_struct *work)
1379{
1380 struct hdmi_spec_per_pin *per_pin =
1381 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1382
Wu Fengguangc6e84532011-11-18 16:59:32 -06001383 if (per_pin->repoll_count++ > 6)
1384 per_pin->repoll_count = 0;
1385
1386 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001387}
1388
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001389static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1390 hda_nid_t nid);
1391
Wu Fengguang079d88c2010-03-08 10:44:23 +08001392static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1393{
1394 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001395 unsigned int caps, config;
1396 int pin_idx;
1397 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001398 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001399
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001400 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001401 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1402 return 0;
1403
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001404 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001405 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1406 return 0;
1407
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001408 if (codec->vendor_id == 0x80862807)
1409 intel_haswell_fixup_connect_list(codec, pin_nid);
1410
Stephen Warren384a48d2011-06-01 11:14:21 -06001411 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001412 per_pin = snd_array_new(&spec->pins);
1413 if (!per_pin)
1414 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001415
1416 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001417 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001418
Stephen Warren384a48d2011-06-01 11:14:21 -06001419 err = hdmi_read_pin_conn(codec, pin_idx);
1420 if (err < 0)
1421 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001422
Wu Fengguang079d88c2010-03-08 10:44:23 +08001423 spec->num_pins++;
1424
Stephen Warren384a48d2011-06-01 11:14:21 -06001425 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001426}
1427
Stephen Warren384a48d2011-06-01 11:14:21 -06001428static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001429{
1430 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001431 struct hdmi_spec_per_cvt *per_cvt;
1432 unsigned int chans;
1433 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001434
Stephen Warren384a48d2011-06-01 11:14:21 -06001435 chans = get_wcaps(codec, cvt_nid);
1436 chans = get_wcaps_channels(chans);
1437
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001438 per_cvt = snd_array_new(&spec->cvts);
1439 if (!per_cvt)
1440 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001441
1442 per_cvt->cvt_nid = cvt_nid;
1443 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001444 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001445 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001446 if (chans > spec->channels_max)
1447 spec->channels_max = chans;
1448 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001449
1450 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1451 &per_cvt->rates,
1452 &per_cvt->formats,
1453 &per_cvt->maxbps);
1454 if (err < 0)
1455 return err;
1456
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001457 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1458 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1459 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001460
1461 return 0;
1462}
1463
1464static int hdmi_parse_codec(struct hda_codec *codec)
1465{
1466 hda_nid_t nid;
1467 int i, nodes;
1468
1469 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1470 if (!nid || nodes < 0) {
1471 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1472 return -EINVAL;
1473 }
1474
1475 for (i = 0; i < nodes; i++, nid++) {
1476 unsigned int caps;
1477 unsigned int type;
1478
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001479 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001480 type = get_wcaps_type(caps);
1481
1482 if (!(caps & AC_WCAP_DIGITAL))
1483 continue;
1484
1485 switch (type) {
1486 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001487 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001488 break;
1489 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001490 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001491 break;
1492 }
1493 }
1494
David Henningssonc9adeef2012-11-07 09:22:33 +01001495#ifdef CONFIG_PM
1496 /* We're seeing some problems with unsolicited hot plug events on
1497 * PantherPoint after S3, if this is not enabled */
1498 if (codec->vendor_id == 0x80862806)
1499 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001500 /*
1501 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1502 * can be lost and presence sense verb will become inaccurate if the
1503 * HDA link is powered off at hot plug or hw initialization time.
1504 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001505 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001506 AC_PWRST_EPSS))
1507 codec->bus->power_keep_link_on = 1;
1508#endif
1509
1510 return 0;
1511}
1512
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001513/*
1514 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001515static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1516{
1517 struct hda_spdif_out *spdif;
1518 bool non_pcm;
1519
1520 mutex_lock(&codec->spdif_mutex);
1521 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1522 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1523 mutex_unlock(&codec->spdif_mutex);
1524 return non_pcm;
1525}
1526
1527
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001528/*
1529 * HDMI callbacks
1530 */
1531
1532static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1533 struct hda_codec *codec,
1534 unsigned int stream_tag,
1535 unsigned int format,
1536 struct snd_pcm_substream *substream)
1537{
Stephen Warren384a48d2011-06-01 11:14:21 -06001538 hda_nid_t cvt_nid = hinfo->nid;
1539 struct hdmi_spec *spec = codec->spec;
1540 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaib0540872013-09-02 12:33:02 +02001541 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1542 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001543 bool non_pcm;
1544
1545 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaib0540872013-09-02 12:33:02 +02001546 per_pin->channels = substream->runtime->channels;
1547 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001548
Stephen Warren384a48d2011-06-01 11:14:21 -06001549 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001550
Takashi Iwaib0540872013-09-02 12:33:02 +02001551 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001552
1553 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001554}
1555
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001556static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1557 struct hda_codec *codec,
1558 struct snd_pcm_substream *substream)
1559{
1560 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1561 return 0;
1562}
1563
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001564static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1565 struct hda_codec *codec,
1566 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001567{
1568 struct hdmi_spec *spec = codec->spec;
1569 int cvt_idx, pin_idx;
1570 struct hdmi_spec_per_cvt *per_cvt;
1571 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001572
Stephen Warren384a48d2011-06-01 11:14:21 -06001573 if (hinfo->nid) {
1574 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1575 if (snd_BUG_ON(cvt_idx < 0))
1576 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001577 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001578
1579 snd_BUG_ON(!per_cvt->assigned);
1580 per_cvt->assigned = 0;
1581 hinfo->nid = 0;
1582
1583 pin_idx = hinfo_to_pin_index(spec, hinfo);
1584 if (snd_BUG_ON(pin_idx < 0))
1585 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001586 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001587
Stephen Warren384a48d2011-06-01 11:14:21 -06001588 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001589 per_pin->chmap_set = false;
1590 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001591
1592 per_pin->setup = false;
1593 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001594 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001595
Stephen Warren384a48d2011-06-01 11:14:21 -06001596 return 0;
1597}
1598
1599static const struct hda_pcm_ops generic_ops = {
1600 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001601 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001602 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001603 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001604};
1605
Takashi Iwaid45e6882012-07-31 11:36:00 +02001606/*
1607 * ALSA API channel-map control callbacks
1608 */
1609static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_info *uinfo)
1611{
1612 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1613 struct hda_codec *codec = info->private_data;
1614 struct hdmi_spec *spec = codec->spec;
1615 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1616 uinfo->count = spec->channels_max;
1617 uinfo->value.integer.min = 0;
1618 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1619 return 0;
1620}
1621
1622static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1623 unsigned int size, unsigned int __user *tlv)
1624{
1625 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1626 struct hda_codec *codec = info->private_data;
1627 struct hdmi_spec *spec = codec->spec;
1628 const unsigned int valid_mask =
1629 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1630 unsigned int __user *dst;
1631 int chs, count = 0;
1632
1633 if (size < 8)
1634 return -ENOMEM;
1635 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1636 return -EFAULT;
1637 size -= 8;
1638 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001639 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001640 int i, c;
1641 struct cea_channel_speaker_allocation *cap;
1642 cap = channel_allocations;
1643 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1644 int chs_bytes = chs * 4;
1645 if (cap->channels != chs)
1646 continue;
1647 if (cap->spk_mask & ~valid_mask)
1648 continue;
1649 if (size < 8)
1650 return -ENOMEM;
1651 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1652 put_user(chs_bytes, dst + 1))
1653 return -EFAULT;
1654 dst += 2;
1655 size -= 8;
1656 count += 8;
1657 if (size < chs_bytes)
1658 return -ENOMEM;
1659 size -= chs_bytes;
1660 count += chs_bytes;
1661 for (c = 7; c >= 0; c--) {
1662 int spk = cap->speakers[c];
1663 if (!spk)
1664 continue;
1665 if (put_user(spk_to_chmap(spk), dst))
1666 return -EFAULT;
1667 dst++;
1668 }
1669 }
1670 }
1671 if (put_user(count, tlv + 1))
1672 return -EFAULT;
1673 return 0;
1674}
1675
1676static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol)
1678{
1679 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1680 struct hda_codec *codec = info->private_data;
1681 struct hdmi_spec *spec = codec->spec;
1682 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001683 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001684 int i;
1685
1686 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1687 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1688 return 0;
1689}
1690
1691static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
1693{
1694 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1695 struct hda_codec *codec = info->private_data;
1696 struct hdmi_spec *spec = codec->spec;
1697 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001698 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001699 unsigned int ctl_idx;
1700 struct snd_pcm_substream *substream;
1701 unsigned char chmap[8];
1702 int i, ca, prepared = 0;
1703
1704 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1705 substream = snd_pcm_chmap_substream(info, ctl_idx);
1706 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001707 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001708 switch (substream->runtime->status->state) {
1709 case SNDRV_PCM_STATE_OPEN:
1710 case SNDRV_PCM_STATE_SETUP:
1711 break;
1712 case SNDRV_PCM_STATE_PREPARED:
1713 prepared = 1;
1714 break;
1715 default:
1716 return -EBUSY;
1717 }
1718 memset(chmap, 0, sizeof(chmap));
1719 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1720 chmap[i] = ucontrol->value.integer.value[i];
1721 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1722 return 0;
1723 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1724 if (ca < 0)
1725 return -EINVAL;
1726 per_pin->chmap_set = true;
1727 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1728 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02001729 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001730
1731 return 0;
1732}
1733
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001734static int generic_hdmi_build_pcms(struct hda_codec *codec)
1735{
1736 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001737 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001738
Stephen Warren384a48d2011-06-01 11:14:21 -06001739 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1740 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001741 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001742 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001743
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001744 per_pin = get_pin(spec, pin_idx);
1745 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1746 info = snd_array_new(&spec->pcm_rec);
1747 if (!info)
1748 return -ENOMEM;
1749 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001750 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001751 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001752
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001753 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001754 pstr->substreams = 1;
1755 pstr->ops = generic_ops;
1756 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001757 }
1758
Stephen Warren384a48d2011-06-01 11:14:21 -06001759 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001760 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001761
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001762 return 0;
1763}
1764
David Henningsson0b6c49b2011-08-23 16:56:03 +02001765static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1766{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001767 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001768 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001769 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1770 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001771
Takashi Iwai31ef2252011-12-01 17:41:36 +01001772 if (pcmdev > 0)
1773 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001774 if (!is_jack_detectable(codec, per_pin->pin_nid))
1775 strncat(hdmi_str, " Phantom",
1776 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001777
Takashi Iwai31ef2252011-12-01 17:41:36 +01001778 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001779}
1780
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001781static int generic_hdmi_build_controls(struct hda_codec *codec)
1782{
1783 struct hdmi_spec *spec = codec->spec;
1784 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001785 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001786
Stephen Warren384a48d2011-06-01 11:14:21 -06001787 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001788 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001789
1790 err = generic_hdmi_build_jack(codec, pin_idx);
1791 if (err < 0)
1792 return err;
1793
Takashi Iwaidcda5802012-10-12 17:24:51 +02001794 err = snd_hda_create_dig_out_ctls(codec,
1795 per_pin->pin_nid,
1796 per_pin->mux_nids[0],
1797 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001798 if (err < 0)
1799 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001800 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001801
1802 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001803 err = hdmi_create_eld_ctl(codec, pin_idx,
1804 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001805
1806 if (err < 0)
1807 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001808
Takashi Iwai82b1d732011-12-20 15:53:07 +01001809 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001810 }
1811
Takashi Iwaid45e6882012-07-31 11:36:00 +02001812 /* add channel maps */
1813 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1814 struct snd_pcm_chmap *chmap;
1815 struct snd_kcontrol *kctl;
1816 int i;
1817 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1818 SNDRV_PCM_STREAM_PLAYBACK,
1819 NULL, 0, pin_idx, &chmap);
1820 if (err < 0)
1821 return err;
1822 /* override handlers */
1823 chmap->private_data = codec;
1824 kctl = chmap->kctl;
1825 for (i = 0; i < kctl->count; i++)
1826 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1827 kctl->info = hdmi_chmap_ctl_info;
1828 kctl->get = hdmi_chmap_ctl_get;
1829 kctl->put = hdmi_chmap_ctl_put;
1830 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1831 }
1832
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001833 return 0;
1834}
1835
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001836static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1837{
1838 struct hdmi_spec *spec = codec->spec;
1839 int pin_idx;
1840
1841 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001842 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001843 struct hdmi_eld *eld = &per_pin->sink_eld;
1844
1845 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001846 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001847 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1848 snd_hda_eld_proc_new(codec, eld, pin_idx);
1849 }
1850 return 0;
1851}
1852
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001853static int generic_hdmi_init(struct hda_codec *codec)
1854{
1855 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001856 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001857
Stephen Warren384a48d2011-06-01 11:14:21 -06001858 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001859 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001860 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001861
1862 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001863 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001864 }
1865 return 0;
1866}
1867
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001868static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1869{
1870 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1871 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1872 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1873}
1874
1875static void hdmi_array_free(struct hdmi_spec *spec)
1876{
1877 snd_array_free(&spec->pins);
1878 snd_array_free(&spec->cvts);
1879 snd_array_free(&spec->pcm_rec);
1880}
1881
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001882static void generic_hdmi_free(struct hda_codec *codec)
1883{
1884 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001885 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001886
Stephen Warren384a48d2011-06-01 11:14:21 -06001887 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001888 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001889 struct hdmi_eld *eld = &per_pin->sink_eld;
1890
Wu Fengguang744626d2011-11-16 16:29:47 +08001891 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001892 snd_hda_eld_proc_free(codec, eld);
1893 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001894
Wu Fengguang744626d2011-11-16 16:29:47 +08001895 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001896 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001897 kfree(spec);
1898}
1899
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001900#ifdef CONFIG_PM
1901static int generic_hdmi_resume(struct hda_codec *codec)
1902{
1903 struct hdmi_spec *spec = codec->spec;
1904 int pin_idx;
1905
1906 generic_hdmi_init(codec);
1907 snd_hda_codec_resume_amp(codec);
1908 snd_hda_codec_resume_cache(codec);
1909
1910 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1911 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1912 hdmi_present_sense(per_pin, 1);
1913 }
1914 return 0;
1915}
1916#endif
1917
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001918static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001919 .init = generic_hdmi_init,
1920 .free = generic_hdmi_free,
1921 .build_pcms = generic_hdmi_build_pcms,
1922 .build_controls = generic_hdmi_build_controls,
1923 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001924#ifdef CONFIG_PM
1925 .resume = generic_hdmi_resume,
1926#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001927};
1928
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001929
1930static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1931 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001932{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001933 struct hdmi_spec *spec = codec->spec;
1934 hda_nid_t conns[4];
1935 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001936
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001937 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1938 if (nconns == spec->num_cvts &&
1939 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001940 return;
1941
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001942 /* override pins connection list */
1943 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1944 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001945}
1946
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001947#define INTEL_VENDOR_NID 0x08
1948#define INTEL_GET_VENDOR_VERB 0xf81
1949#define INTEL_SET_VENDOR_VERB 0x781
1950#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1951#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1952
1953static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001954 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001955{
1956 unsigned int vendor_param;
1957
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001958 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1959 INTEL_GET_VENDOR_VERB, 0);
1960 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1961 return;
1962
1963 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1964 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1965 INTEL_SET_VENDOR_VERB, vendor_param);
1966 if (vendor_param == -1)
1967 return;
1968
Takashi Iwai17df3f52013-05-08 08:09:34 +02001969 if (update_tree)
1970 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001971}
1972
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001973static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1974{
1975 unsigned int vendor_param;
1976
1977 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1978 INTEL_GET_VENDOR_VERB, 0);
1979 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1980 return;
1981
1982 /* enable DP1.2 mode */
1983 vendor_param |= INTEL_EN_DP12;
1984 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1985 INTEL_SET_VENDOR_VERB, vendor_param);
1986}
1987
Takashi Iwai17df3f52013-05-08 08:09:34 +02001988/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1989 * Otherwise you may get severe h/w communication errors.
1990 */
1991static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1992 unsigned int power_state)
1993{
1994 if (power_state == AC_PWRST_D0) {
1995 intel_haswell_enable_all_pins(codec, false);
1996 intel_haswell_fixup_enable_dp12(codec);
1997 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001998
Takashi Iwai17df3f52013-05-08 08:09:34 +02001999 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2000 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2001}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002002
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002003static int patch_generic_hdmi(struct hda_codec *codec)
2004{
2005 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002006
2007 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2008 if (spec == NULL)
2009 return -ENOMEM;
2010
2011 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002012 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002013
Takashi Iwai17df3f52013-05-08 08:09:34 +02002014 if (codec->vendor_id == 0x80862807) {
2015 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002016 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002017 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002018
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002019 if (hdmi_parse_codec(codec) < 0) {
2020 codec->spec = NULL;
2021 kfree(spec);
2022 return -EINVAL;
2023 }
2024 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002025 if (codec->vendor_id == 0x80862807) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002026 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002027 codec->dp_mst = true;
2028 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002029
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002030 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002031
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002032 init_channel_allocations();
2033
2034 return 0;
2035}
2036
2037/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002038 * Shared non-generic implementations
2039 */
2040
2041static int simple_playback_build_pcms(struct hda_codec *codec)
2042{
2043 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002044 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002045 unsigned int chans;
2046 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002047 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002048
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002049 per_cvt = get_cvt(spec, 0);
2050 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002051 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002052
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002053 info = snd_array_new(&spec->pcm_rec);
2054 if (!info)
2055 return -ENOMEM;
2056 info->name = get_pin(spec, 0)->pcm_name;
2057 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002058 info->pcm_type = HDA_PCM_TYPE_HDMI;
2059 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2060 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002061 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002062 if (pstr->channels_max <= 2 && chans && chans <= 16)
2063 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002064
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002065 codec->num_pcms = 1;
2066 codec->pcm_info = info;
2067
Stephen Warren3aaf8982011-06-01 11:14:19 -06002068 return 0;
2069}
2070
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002071/* unsolicited event for jack sensing */
2072static void simple_hdmi_unsol_event(struct hda_codec *codec,
2073 unsigned int res)
2074{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002075 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002076 snd_hda_jack_report_sync(codec);
2077}
2078
2079/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2080 * as long as spec->pins[] is set correctly
2081 */
2082#define simple_hdmi_build_jack generic_hdmi_build_jack
2083
Stephen Warren3aaf8982011-06-01 11:14:19 -06002084static int simple_playback_build_controls(struct hda_codec *codec)
2085{
2086 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002087 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002088 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002089
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002090 per_cvt = get_cvt(spec, 0);
2091 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2092 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002093 if (err < 0)
2094 return err;
2095 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002096}
2097
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002098static int simple_playback_init(struct hda_codec *codec)
2099{
2100 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002101 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2102 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002103
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002104 snd_hda_codec_write(codec, pin, 0,
2105 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2106 /* some codecs require to unmute the pin */
2107 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2108 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2109 AMP_OUT_UNMUTE);
2110 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002111 return 0;
2112}
2113
Stephen Warren3aaf8982011-06-01 11:14:19 -06002114static void simple_playback_free(struct hda_codec *codec)
2115{
2116 struct hdmi_spec *spec = codec->spec;
2117
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002118 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002119 kfree(spec);
2120}
2121
2122/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002123 * Nvidia specific implementations
2124 */
2125
2126#define Nv_VERB_SET_Channel_Allocation 0xF79
2127#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2128#define Nv_VERB_SET_Audio_Protection_On 0xF98
2129#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2130
2131#define nvhdmi_master_con_nid_7x 0x04
2132#define nvhdmi_master_pin_nid_7x 0x05
2133
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002134static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002135 /*front, rear, clfe, rear_surr */
2136 0x6, 0x8, 0xa, 0xc,
2137};
2138
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002139static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2140 /* set audio protect on */
2141 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2142 /* enable digital output on pin widget */
2143 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2144 {} /* terminator */
2145};
2146
2147static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002148 /* set audio protect on */
2149 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2150 /* enable digital output on pin widget */
2151 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2152 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2153 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2154 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2155 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2156 {} /* terminator */
2157};
2158
2159#ifdef LIMITED_RATE_FMT_SUPPORT
2160/* support only the safe format and rate */
2161#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2162#define SUPPORTED_MAXBPS 16
2163#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2164#else
2165/* support all rates and formats */
2166#define SUPPORTED_RATES \
2167 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2168 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2169 SNDRV_PCM_RATE_192000)
2170#define SUPPORTED_MAXBPS 24
2171#define SUPPORTED_FORMATS \
2172 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2173#endif
2174
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002175static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002176{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002177 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2178 return 0;
2179}
2180
2181static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2182{
2183 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002184 return 0;
2185}
2186
Nitin Daga393004b2011-01-10 21:49:31 +05302187static unsigned int channels_2_6_8[] = {
2188 2, 6, 8
2189};
2190
2191static unsigned int channels_2_8[] = {
2192 2, 8
2193};
2194
2195static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2196 .count = ARRAY_SIZE(channels_2_6_8),
2197 .list = channels_2_6_8,
2198 .mask = 0,
2199};
2200
2201static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2202 .count = ARRAY_SIZE(channels_2_8),
2203 .list = channels_2_8,
2204 .mask = 0,
2205};
2206
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002207static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2208 struct hda_codec *codec,
2209 struct snd_pcm_substream *substream)
2210{
2211 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302212 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2213
2214 switch (codec->preset->id) {
2215 case 0x10de0002:
2216 case 0x10de0003:
2217 case 0x10de0005:
2218 case 0x10de0006:
2219 hw_constraints_channels = &hw_constraints_2_8_channels;
2220 break;
2221 case 0x10de0007:
2222 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2223 break;
2224 default:
2225 break;
2226 }
2227
2228 if (hw_constraints_channels != NULL) {
2229 snd_pcm_hw_constraint_list(substream->runtime, 0,
2230 SNDRV_PCM_HW_PARAM_CHANNELS,
2231 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002232 } else {
2233 snd_pcm_hw_constraint_step(substream->runtime, 0,
2234 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302235 }
2236
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002237 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2238}
2239
2240static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2241 struct hda_codec *codec,
2242 struct snd_pcm_substream *substream)
2243{
2244 struct hdmi_spec *spec = codec->spec;
2245 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2246}
2247
2248static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2249 struct hda_codec *codec,
2250 unsigned int stream_tag,
2251 unsigned int format,
2252 struct snd_pcm_substream *substream)
2253{
2254 struct hdmi_spec *spec = codec->spec;
2255 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2256 stream_tag, format, substream);
2257}
2258
Takashi Iwaid0b12522012-06-15 14:34:42 +02002259static const struct hda_pcm_stream simple_pcm_playback = {
2260 .substreams = 1,
2261 .channels_min = 2,
2262 .channels_max = 2,
2263 .ops = {
2264 .open = simple_playback_pcm_open,
2265 .close = simple_playback_pcm_close,
2266 .prepare = simple_playback_pcm_prepare
2267 },
2268};
2269
2270static const struct hda_codec_ops simple_hdmi_patch_ops = {
2271 .build_controls = simple_playback_build_controls,
2272 .build_pcms = simple_playback_build_pcms,
2273 .init = simple_playback_init,
2274 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002275 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002276};
2277
2278static int patch_simple_hdmi(struct hda_codec *codec,
2279 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2280{
2281 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002282 struct hdmi_spec_per_cvt *per_cvt;
2283 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002284
2285 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2286 if (!spec)
2287 return -ENOMEM;
2288
2289 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002290 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002291
2292 spec->multiout.num_dacs = 0; /* no analog */
2293 spec->multiout.max_channels = 2;
2294 spec->multiout.dig_out_nid = cvt_nid;
2295 spec->num_cvts = 1;
2296 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002297 per_pin = snd_array_new(&spec->pins);
2298 per_cvt = snd_array_new(&spec->cvts);
2299 if (!per_pin || !per_cvt) {
2300 simple_playback_free(codec);
2301 return -ENOMEM;
2302 }
2303 per_cvt->cvt_nid = cvt_nid;
2304 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002305 spec->pcm_playback = simple_pcm_playback;
2306
2307 codec->patch_ops = simple_hdmi_patch_ops;
2308
2309 return 0;
2310}
2311
Aaron Plattner1f348522011-04-06 17:19:04 -07002312static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2313 int channels)
2314{
2315 unsigned int chanmask;
2316 int chan = channels ? (channels - 1) : 1;
2317
2318 switch (channels) {
2319 default:
2320 case 0:
2321 case 2:
2322 chanmask = 0x00;
2323 break;
2324 case 4:
2325 chanmask = 0x08;
2326 break;
2327 case 6:
2328 chanmask = 0x0b;
2329 break;
2330 case 8:
2331 chanmask = 0x13;
2332 break;
2333 }
2334
2335 /* Set the audio infoframe channel allocation and checksum fields. The
2336 * channel count is computed implicitly by the hardware. */
2337 snd_hda_codec_write(codec, 0x1, 0,
2338 Nv_VERB_SET_Channel_Allocation, chanmask);
2339
2340 snd_hda_codec_write(codec, 0x1, 0,
2341 Nv_VERB_SET_Info_Frame_Checksum,
2342 (0x71 - chan - chanmask));
2343}
2344
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002345static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2346 struct hda_codec *codec,
2347 struct snd_pcm_substream *substream)
2348{
2349 struct hdmi_spec *spec = codec->spec;
2350 int i;
2351
2352 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2353 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2354 for (i = 0; i < 4; i++) {
2355 /* set the stream id */
2356 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2357 AC_VERB_SET_CHANNEL_STREAMID, 0);
2358 /* set the stream format */
2359 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2360 AC_VERB_SET_STREAM_FORMAT, 0);
2361 }
2362
Aaron Plattner1f348522011-04-06 17:19:04 -07002363 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2364 * streams are disabled. */
2365 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2366
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002367 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2368}
2369
2370static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2371 struct hda_codec *codec,
2372 unsigned int stream_tag,
2373 unsigned int format,
2374 struct snd_pcm_substream *substream)
2375{
2376 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002377 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002378 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002379 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002380 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002381 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002382
2383 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002384 per_cvt = get_cvt(spec, 0);
2385 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002386
2387 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002388
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002389 dataDCC2 = 0x2;
2390
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002391 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002392 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002393 snd_hda_codec_write(codec,
2394 nvhdmi_master_con_nid_7x,
2395 0,
2396 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002397 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002398
2399 /* set the stream id */
2400 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2401 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2402
2403 /* set the stream format */
2404 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2405 AC_VERB_SET_STREAM_FORMAT, format);
2406
2407 /* turn on again (if needed) */
2408 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002409 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002410 snd_hda_codec_write(codec,
2411 nvhdmi_master_con_nid_7x,
2412 0,
2413 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002414 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002415 snd_hda_codec_write(codec,
2416 nvhdmi_master_con_nid_7x,
2417 0,
2418 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2419 }
2420
2421 for (i = 0; i < 4; i++) {
2422 if (chs == 2)
2423 channel_id = 0;
2424 else
2425 channel_id = i * 2;
2426
2427 /* turn off SPDIF once;
2428 *otherwise the IEC958 bits won't be updated
2429 */
2430 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002431 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002432 snd_hda_codec_write(codec,
2433 nvhdmi_con_nids_7x[i],
2434 0,
2435 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002436 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002437 /* set the stream id */
2438 snd_hda_codec_write(codec,
2439 nvhdmi_con_nids_7x[i],
2440 0,
2441 AC_VERB_SET_CHANNEL_STREAMID,
2442 (stream_tag << 4) | channel_id);
2443 /* set the stream format */
2444 snd_hda_codec_write(codec,
2445 nvhdmi_con_nids_7x[i],
2446 0,
2447 AC_VERB_SET_STREAM_FORMAT,
2448 format);
2449 /* turn on again (if needed) */
2450 /* enable and set the channel status audio/data flag */
2451 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002452 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002453 snd_hda_codec_write(codec,
2454 nvhdmi_con_nids_7x[i],
2455 0,
2456 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002457 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002458 snd_hda_codec_write(codec,
2459 nvhdmi_con_nids_7x[i],
2460 0,
2461 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2462 }
2463 }
2464
Aaron Plattner1f348522011-04-06 17:19:04 -07002465 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002466
2467 mutex_unlock(&codec->spdif_mutex);
2468 return 0;
2469}
2470
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002471static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002472 .substreams = 1,
2473 .channels_min = 2,
2474 .channels_max = 8,
2475 .nid = nvhdmi_master_con_nid_7x,
2476 .rates = SUPPORTED_RATES,
2477 .maxbps = SUPPORTED_MAXBPS,
2478 .formats = SUPPORTED_FORMATS,
2479 .ops = {
2480 .open = simple_playback_pcm_open,
2481 .close = nvhdmi_8ch_7x_pcm_close,
2482 .prepare = nvhdmi_8ch_7x_pcm_prepare
2483 },
2484};
2485
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002486static int patch_nvhdmi_2ch(struct hda_codec *codec)
2487{
2488 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002489 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2490 nvhdmi_master_pin_nid_7x);
2491 if (err < 0)
2492 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002493
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002494 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002495 /* override the PCM rates, etc, as the codec doesn't give full list */
2496 spec = codec->spec;
2497 spec->pcm_playback.rates = SUPPORTED_RATES;
2498 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2499 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002500 return 0;
2501}
2502
Takashi Iwai53775b02012-08-01 12:17:41 +02002503static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2504{
2505 struct hdmi_spec *spec = codec->spec;
2506 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002507 if (!err) {
2508 struct hda_pcm *info = get_pcm_rec(spec, 0);
2509 info->own_chmap = true;
2510 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002511 return err;
2512}
2513
2514static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2515{
2516 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002517 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002518 struct snd_pcm_chmap *chmap;
2519 int err;
2520
2521 err = simple_playback_build_controls(codec);
2522 if (err < 0)
2523 return err;
2524
2525 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002526 info = get_pcm_rec(spec, 0);
2527 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002528 SNDRV_PCM_STREAM_PLAYBACK,
2529 snd_pcm_alt_chmaps, 8, 0, &chmap);
2530 if (err < 0)
2531 return err;
2532 switch (codec->preset->id) {
2533 case 0x10de0002:
2534 case 0x10de0003:
2535 case 0x10de0005:
2536 case 0x10de0006:
2537 chmap->channel_mask = (1U << 2) | (1U << 8);
2538 break;
2539 case 0x10de0007:
2540 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2541 }
2542 return 0;
2543}
2544
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002545static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2546{
2547 struct hdmi_spec *spec;
2548 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002549 if (err < 0)
2550 return err;
2551 spec = codec->spec;
2552 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002553 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002554 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002555 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2556 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002557
2558 /* Initialize the audio infoframe channel mask and checksum to something
2559 * valid */
2560 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2561
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002562 return 0;
2563}
2564
2565/*
2566 * ATI-specific implementations
2567 *
2568 * FIXME: we may omit the whole this and use the generic code once after
2569 * it's confirmed to work.
2570 */
2571
2572#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2573#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2574
2575static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2576 struct hda_codec *codec,
2577 unsigned int stream_tag,
2578 unsigned int format,
2579 struct snd_pcm_substream *substream)
2580{
2581 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002582 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002583 int chans = substream->runtime->channels;
2584 int i, err;
2585
2586 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2587 substream);
2588 if (err < 0)
2589 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002590 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002591 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002592 /* FIXME: XXX */
2593 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002594 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002595 AC_VERB_SET_HDMI_CHAN_SLOT,
2596 (i << 4) | i);
2597 }
2598 return 0;
2599}
2600
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002601static int patch_atihdmi(struct hda_codec *codec)
2602{
2603 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002604 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2605 if (err < 0)
2606 return err;
2607 spec = codec->spec;
2608 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002609 return 0;
2610}
2611
Annie Liu3de5ff82012-06-08 19:18:42 +08002612/* VIA HDMI Implementation */
2613#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2614#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2615
Annie Liu3de5ff82012-06-08 19:18:42 +08002616static int patch_via_hdmi(struct hda_codec *codec)
2617{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002618 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002619}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002620
2621/*
2622 * patch entries
2623 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002624static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002625{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2626{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2627{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002628{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002629{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2630{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2631{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2632{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2633{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2634{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2635{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2636{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002637{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2638{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2639{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2640{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2641{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2642{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2643{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2644{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2645{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2646{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2647{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002648/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002649{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2650{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2651{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2652{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2653{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2654{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2655{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2656{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2657{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2658{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002659{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002660{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002661{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2662{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002663{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2664{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2665{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2666{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002667{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2668{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2669{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2670{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2671{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2672{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002673{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002674{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002675{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002676{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2677{} /* terminator */
2678};
2679
2680MODULE_ALIAS("snd-hda-codec-id:1002793c");
2681MODULE_ALIAS("snd-hda-codec-id:10027919");
2682MODULE_ALIAS("snd-hda-codec-id:1002791a");
2683MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2684MODULE_ALIAS("snd-hda-codec-id:10951390");
2685MODULE_ALIAS("snd-hda-codec-id:10951392");
2686MODULE_ALIAS("snd-hda-codec-id:10de0002");
2687MODULE_ALIAS("snd-hda-codec-id:10de0003");
2688MODULE_ALIAS("snd-hda-codec-id:10de0005");
2689MODULE_ALIAS("snd-hda-codec-id:10de0006");
2690MODULE_ALIAS("snd-hda-codec-id:10de0007");
2691MODULE_ALIAS("snd-hda-codec-id:10de000a");
2692MODULE_ALIAS("snd-hda-codec-id:10de000b");
2693MODULE_ALIAS("snd-hda-codec-id:10de000c");
2694MODULE_ALIAS("snd-hda-codec-id:10de000d");
2695MODULE_ALIAS("snd-hda-codec-id:10de0010");
2696MODULE_ALIAS("snd-hda-codec-id:10de0011");
2697MODULE_ALIAS("snd-hda-codec-id:10de0012");
2698MODULE_ALIAS("snd-hda-codec-id:10de0013");
2699MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002700MODULE_ALIAS("snd-hda-codec-id:10de0015");
2701MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002702MODULE_ALIAS("snd-hda-codec-id:10de0018");
2703MODULE_ALIAS("snd-hda-codec-id:10de0019");
2704MODULE_ALIAS("snd-hda-codec-id:10de001a");
2705MODULE_ALIAS("snd-hda-codec-id:10de001b");
2706MODULE_ALIAS("snd-hda-codec-id:10de001c");
2707MODULE_ALIAS("snd-hda-codec-id:10de0040");
2708MODULE_ALIAS("snd-hda-codec-id:10de0041");
2709MODULE_ALIAS("snd-hda-codec-id:10de0042");
2710MODULE_ALIAS("snd-hda-codec-id:10de0043");
2711MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002712MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002713MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002714MODULE_ALIAS("snd-hda-codec-id:10de0067");
2715MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002716MODULE_ALIAS("snd-hda-codec-id:11069f80");
2717MODULE_ALIAS("snd-hda-codec-id:11069f81");
2718MODULE_ALIAS("snd-hda-codec-id:11069f84");
2719MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002720MODULE_ALIAS("snd-hda-codec-id:17e80047");
2721MODULE_ALIAS("snd-hda-codec-id:80860054");
2722MODULE_ALIAS("snd-hda-codec-id:80862801");
2723MODULE_ALIAS("snd-hda-codec-id:80862802");
2724MODULE_ALIAS("snd-hda-codec-id:80862803");
2725MODULE_ALIAS("snd-hda-codec-id:80862804");
2726MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002727MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002728MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002729MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002730MODULE_ALIAS("snd-hda-codec-id:808629fb");
2731
2732MODULE_LICENSE("GPL");
2733MODULE_DESCRIPTION("HDMI HD-audio codec");
2734MODULE_ALIAS("snd-hda-codec-intelhdmi");
2735MODULE_ALIAS("snd-hda-codec-nvhdmi");
2736MODULE_ALIAS("snd-hda-codec-atihdmi");
2737
2738static struct hda_codec_preset_list intel_list = {
2739 .preset = snd_hda_preset_hdmi,
2740 .owner = THIS_MODULE,
2741};
2742
2743static int __init patch_hdmi_init(void)
2744{
2745 return snd_hda_add_codec_preset(&intel_list);
2746}
2747
2748static void __exit patch_hdmi_exit(void)
2749{
2750 snd_hda_delete_codec_preset(&intel_list);
2751}
2752
2753module_init(patch_hdmi_init)
2754module_exit(patch_hdmi_exit)