blob: 4efaca89d805b39082e446911fbeff9142279be6 [file] [log] [blame]
Wu Fengguang079d88c2010-03-08 10:44:23 +08001/*
2 *
3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
4 *
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
Takashi Iwai84eb01b2010-09-07 12:27:25 +02006 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
Wu Fengguang079d88c2010-03-08 10:44:23 +08009 *
10 * Authors:
11 * Wu Fengguang <wfg@linux.intel.com>
12 *
13 * Maintained by:
14 * Wu Fengguang <wfg@linux.intel.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30
Takashi Iwai84eb01b2010-09-07 12:27:25 +020031#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040034#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020035#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020036#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080037#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020038#include <sound/tlv.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020039#include "hda_codec.h"
40#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020041#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042
Takashi Iwai0ebaa242011-01-11 18:11:04 +010043static bool static_hdmi_pcm;
44module_param(static_hdmi_pcm, bool, 0644);
45MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
46
Mengdong Linfb87fa32013-09-04 16:36:57 -040047#define is_haswell(codec) ((codec)->vendor_id == 0x80862807)
48
Stephen Warren384a48d2011-06-01 11:14:21 -060049struct hdmi_spec_per_cvt {
50 hda_nid_t cvt_nid;
51 int assigned;
52 unsigned int channels_min;
53 unsigned int channels_max;
54 u32 rates;
55 u64 formats;
56 unsigned int maxbps;
57};
58
Takashi Iwai4eea3092013-02-07 18:18:19 +010059/* max. connections to a widget */
60#define HDA_MAX_CONNECTIONS 32
61
Stephen Warren384a48d2011-06-01 11:14:21 -060062struct hdmi_spec_per_pin {
63 hda_nid_t pin_nid;
64 int num_mux_nids;
65 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Anssi Hannula1df5a062013-10-05 02:25:40 +030066 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080067
68 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060069 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080070 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010071 struct snd_kcontrol *eld_ctl;
Wu Fengguangc6e84532011-11-18 16:59:32 -060072 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020073 bool setup; /* the stream has been set up by prepare callback */
74 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020075 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020076 bool chmap_set; /* channel-map override by ALSA API? */
77 unsigned char chmap[8]; /* ALSA API channel-map */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010078 char pcm_name[8]; /* filled in build_pcm callbacks */
Stephen Warren384a48d2011-06-01 11:14:21 -060079};
80
Wu Fengguang079d88c2010-03-08 10:44:23 +080081struct hdmi_spec {
82 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010083 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
84 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -060085
Wu Fengguang079d88c2010-03-08 10:44:23 +080086 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +010087 struct snd_array pins; /* struct hdmi_spec_per_pin */
88 struct snd_array pcm_rec; /* struct hda_pcm */
Takashi Iwaid45e6882012-07-31 11:36:00 +020089 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080090
David Henningsson4bd038f2013-02-19 16:11:25 +010091 struct hdmi_eld temp_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +080092 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060093 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080094 */
95 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020096 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080097};
98
99
100struct hdmi_audio_infoframe {
101 u8 type; /* 0x84 */
102 u8 ver; /* 0x01 */
103 u8 len; /* 0x0a */
104
Wu Fengguang53d7d692010-09-21 14:25:49 +0800105 u8 checksum;
106
Wu Fengguang079d88c2010-03-08 10:44:23 +0800107 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
108 u8 SS01_SF24;
109 u8 CXT04;
110 u8 CA;
111 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800112};
113
114struct dp_audio_infoframe {
115 u8 type; /* 0x84 */
116 u8 len; /* 0x1b */
117 u8 ver; /* 0x11 << 2 */
118
119 u8 CC02_CT47; /* match with HDMI infoframe from this on */
120 u8 SS01_SF24;
121 u8 CXT04;
122 u8 CA;
123 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800124};
125
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100126union audio_infoframe {
127 struct hdmi_audio_infoframe hdmi;
128 struct dp_audio_infoframe dp;
129 u8 bytes[0];
130};
131
Wu Fengguang079d88c2010-03-08 10:44:23 +0800132/*
133 * CEA speaker placement:
134 *
135 * FLH FCH FRH
136 * FLW FL FLC FC FRC FR FRW
137 *
138 * LFE
139 * TC
140 *
141 * RL RLC RC RRC RR
142 *
143 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
144 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
145 */
146enum cea_speaker_placement {
147 FL = (1 << 0), /* Front Left */
148 FC = (1 << 1), /* Front Center */
149 FR = (1 << 2), /* Front Right */
150 FLC = (1 << 3), /* Front Left Center */
151 FRC = (1 << 4), /* Front Right Center */
152 RL = (1 << 5), /* Rear Left */
153 RC = (1 << 6), /* Rear Center */
154 RR = (1 << 7), /* Rear Right */
155 RLC = (1 << 8), /* Rear Left Center */
156 RRC = (1 << 9), /* Rear Right Center */
157 LFE = (1 << 10), /* Low Frequency Effect */
158 FLW = (1 << 11), /* Front Left Wide */
159 FRW = (1 << 12), /* Front Right Wide */
160 FLH = (1 << 13), /* Front Left High */
161 FCH = (1 << 14), /* Front Center High */
162 FRH = (1 << 15), /* Front Right High */
163 TC = (1 << 16), /* Top Center */
164};
165
166/*
167 * ELD SA bits in the CEA Speaker Allocation data block
168 */
169static int eld_speaker_allocation_bits[] = {
170 [0] = FL | FR,
171 [1] = LFE,
172 [2] = FC,
173 [3] = RL | RR,
174 [4] = RC,
175 [5] = FLC | FRC,
176 [6] = RLC | RRC,
177 /* the following are not defined in ELD yet */
178 [7] = FLW | FRW,
179 [8] = FLH | FRH,
180 [9] = TC,
181 [10] = FCH,
182};
183
184struct cea_channel_speaker_allocation {
185 int ca_index;
186 int speakers[8];
187
188 /* derived values, just for convenience */
189 int channels;
190 int spk_mask;
191};
192
193/*
194 * ALSA sequence is:
195 *
196 * surround40 surround41 surround50 surround51 surround71
197 * ch0 front left = = = =
198 * ch1 front right = = = =
199 * ch2 rear left = = = =
200 * ch3 rear right = = = =
201 * ch4 LFE center center center
202 * ch5 LFE LFE
203 * ch6 side left
204 * ch7 side right
205 *
206 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
207 */
208static int hdmi_channel_mapping[0x32][8] = {
209 /* stereo */
210 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211 /* 2.1 */
212 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* Dolby Surround */
214 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
215 /* surround40 */
216 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
217 /* 4ch */
218 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
219 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800220 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800221 /* surround50 */
222 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
223 /* surround51 */
224 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
225 /* 7.1 */
226 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
227};
228
229/*
230 * This is an ordered list!
231 *
232 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800233 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800234 */
235static struct cea_channel_speaker_allocation channel_allocations[] = {
236/* channel: 7 6 5 4 3 2 1 0 */
237{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
238 /* 2.1 */
239{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
240 /* Dolby Surround */
241{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
242 /* surround40 */
243{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
244 /* surround41 */
245{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
246 /* surround50 */
247{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
248 /* surround51 */
249{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
250 /* 6.1 */
251{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
252 /* surround71 */
253{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
254
255{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
256{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
257{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
258{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
259{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
260{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
261{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
262{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
263{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
264{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
265{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
266{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
267{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
268{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
269{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
270{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
271{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
272{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
273{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
274{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
275{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
276{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
277{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
278{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
283{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
284{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
285{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
286{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
287{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
288{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
294{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
295{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
296};
297
298
299/*
300 * HDMI routines
301 */
302
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100303#define get_pin(spec, idx) \
304 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
305#define get_cvt(spec, idx) \
306 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
307#define get_pcm_rec(spec, idx) \
308 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
309
Stephen Warren384a48d2011-06-01 11:14:21 -0600310static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800311{
Stephen Warren384a48d2011-06-01 11:14:21 -0600312 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800313
Stephen Warren384a48d2011-06-01 11:14:21 -0600314 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100315 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600316 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800317
Stephen Warren384a48d2011-06-01 11:14:21 -0600318 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
319 return -EINVAL;
320}
321
322static int hinfo_to_pin_index(struct hdmi_spec *spec,
323 struct hda_pcm_stream *hinfo)
324{
325 int pin_idx;
326
327 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100328 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600329 return pin_idx;
330
331 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
332 return -EINVAL;
333}
334
335static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
336{
337 int cvt_idx;
338
339 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100340 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600341 return cvt_idx;
342
343 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800344 return -EINVAL;
345}
346
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500347static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_info *uinfo)
349{
350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100351 struct hdmi_spec *spec = codec->spec;
352 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500353 int pin_idx;
354
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500355 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
356
357 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100358 eld = &get_pin(spec, pin_idx)->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100359
David Henningsson4bd038f2013-02-19 16:11:25 +0100360 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100361 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
David Henningsson4bd038f2013-02-19 16:11:25 +0100362 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500363
364 return 0;
365}
366
367static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
369{
370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100371 struct hdmi_spec *spec = codec->spec;
372 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500373 int pin_idx;
374
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500375 pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100376 eld = &get_pin(spec, pin_idx)->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500377
David Henningsson4bd038f2013-02-19 16:11:25 +0100378 mutex_lock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100379 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
David Henningsson4bd038f2013-02-19 16:11:25 +0100380 mutex_unlock(&eld->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100381 snd_BUG();
382 return -EINVAL;
383 }
384
385 memset(ucontrol->value.bytes.data, 0,
386 ARRAY_SIZE(ucontrol->value.bytes.data));
387 if (eld->eld_valid)
388 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
389 eld->eld_size);
David Henningsson4bd038f2013-02-19 16:11:25 +0100390 mutex_unlock(&eld->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500391
392 return 0;
393}
394
395static struct snd_kcontrol_new eld_bytes_ctl = {
396 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = "ELD",
399 .info = hdmi_eld_ctl_info,
400 .get = hdmi_eld_ctl_get,
401};
402
403static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
404 int device)
405{
406 struct snd_kcontrol *kctl;
407 struct hdmi_spec *spec = codec->spec;
408 int err;
409
410 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
411 if (!kctl)
412 return -ENOMEM;
413 kctl->private_value = pin_idx;
414 kctl->id.device = device;
415
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100416 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500417 if (err < 0)
418 return err;
419
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100420 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500421 return 0;
422}
423
Wu Fengguang079d88c2010-03-08 10:44:23 +0800424#ifdef BE_PARANOID
425static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426 int *packet_index, int *byte_index)
427{
428 int val;
429
430 val = snd_hda_codec_read(codec, pin_nid, 0,
431 AC_VERB_GET_HDMI_DIP_INDEX, 0);
432
433 *packet_index = val >> 5;
434 *byte_index = val & 0x1f;
435}
436#endif
437
438static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439 int packet_index, int byte_index)
440{
441 int val;
442
443 val = (packet_index << 5) | (byte_index & 0x1f);
444
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
446}
447
448static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
449 unsigned char val)
450{
451 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
452}
453
Stephen Warren384a48d2011-06-01 11:14:21 -0600454static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800455{
456 /* Unmute */
457 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
458 snd_hda_codec_write(codec, pin_nid, 0,
459 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100460 /* Enable pin out: some machines with GM965 gets broken output when
461 * the pin is disabled or changed while using with HDMI
462 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800463 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100464 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800465}
466
Stephen Warren384a48d2011-06-01 11:14:21 -0600467static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800468{
Stephen Warren384a48d2011-06-01 11:14:21 -0600469 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800470 AC_VERB_GET_CVT_CHAN_COUNT, 0);
471}
472
473static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600474 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800475{
Stephen Warren384a48d2011-06-01 11:14:21 -0600476 if (chs != hdmi_get_channel_count(codec, cvt_nid))
477 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800478 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
479}
480
481
482/*
483 * Channel mapping routines
484 */
485
486/*
487 * Compute derived values in channel_allocations[].
488 */
489static void init_channel_allocations(void)
490{
491 int i, j;
492 struct cea_channel_speaker_allocation *p;
493
494 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
495 p = channel_allocations + i;
496 p->channels = 0;
497 p->spk_mask = 0;
498 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
499 if (p->speakers[j]) {
500 p->channels++;
501 p->spk_mask |= p->speakers[j];
502 }
503 }
504}
505
Wang Xingchao72357c72012-09-06 10:02:36 +0800506static int get_channel_allocation_order(int ca)
507{
508 int i;
509
510 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
511 if (channel_allocations[i].ca_index == ca)
512 break;
513 }
514 return i;
515}
516
Wu Fengguang079d88c2010-03-08 10:44:23 +0800517/*
518 * The transformation takes two steps:
519 *
520 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
521 * spk_mask => (channel_allocations[]) => ai->CA
522 *
523 * TODO: it could select the wrong CA from multiple candidates.
524*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600525static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800526{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800527 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800528 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800529 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800530 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
531
532 /*
533 * CA defaults to 0 for basic stereo audio
534 */
535 if (channels <= 2)
536 return 0;
537
Wu Fengguang079d88c2010-03-08 10:44:23 +0800538 /*
539 * expand ELD's speaker allocation mask
540 *
541 * ELD tells the speaker mask in a compact(paired) form,
542 * expand ELD's notions to match the ones used by Audio InfoFrame.
543 */
544 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100545 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800546 spk_mask |= eld_speaker_allocation_bits[i];
547 }
548
549 /* search for the first working match in the CA table */
550 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
551 if (channels == channel_allocations[i].channels &&
552 (spk_mask & channel_allocations[i].spk_mask) ==
553 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800554 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800555 break;
556 }
557 }
558
Anssi Hannula18e39182013-09-01 14:36:47 +0300559 if (!ca) {
560 /* if there was no match, select the regular ALSA channel
561 * allocation with the matching number of channels */
562 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
563 if (channels == channel_allocations[i].channels) {
564 ca = channel_allocations[i].ca_index;
565 break;
566 }
567 }
568 }
569
David Henningsson1613d6b2013-02-19 16:11:24 +0100570 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800571 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800572 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800573
Wu Fengguang53d7d692010-09-21 14:25:49 +0800574 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575}
576
577static void hdmi_debug_channel_mapping(struct hda_codec *codec,
578 hda_nid_t pin_nid)
579{
580#ifdef CONFIG_SND_DEBUG_VERBOSE
581 int i;
582 int slot;
583
584 for (i = 0; i < 8; i++) {
585 slot = snd_hda_codec_read(codec, pin_nid, 0,
586 AC_VERB_GET_HDMI_CHAN_SLOT, i);
587 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
588 slot >> 4, slot & 0xf);
589 }
590#endif
591}
592
593
Takashi Iwaid45e6882012-07-31 11:36:00 +0200594static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800595 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800596 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800597 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800598{
Anssi Hannula90f28002013-10-05 02:25:39 +0300599 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800600 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800601 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800602 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800603 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800604
Wang Xingchao72357c72012-09-06 10:02:36 +0800605 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300606 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800607
Wu Fengguang079d88c2010-03-08 10:44:23 +0800608 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300609 int hdmi_slot = 0;
610 /* fill actual channel mappings in ALSA channel (i) order */
611 for (i = 0; i < ch_alloc->channels; i++) {
612 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
613 hdmi_slot++; /* skip zero slots */
614
615 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
616 }
617 /* fill the rest of the slots with ALSA channel 0xf */
618 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
619 if (!ch_alloc->speakers[7 - hdmi_slot])
620 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800621 }
622
Wang Xingchao433968d2012-09-06 10:02:37 +0800623 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300624 for (i = 0; i < ch_alloc->channels; i++)
Wang Xingchao433968d2012-09-06 10:02:37 +0800625 non_pcm_mapping[i] = i | (i << 4);
626 for (; i < 8; i++)
627 non_pcm_mapping[i] = 0xf | (i << 4);
628 }
629
Wu Fengguang079d88c2010-03-08 10:44:23 +0800630 for (i = 0; i < 8; i++) {
631 err = snd_hda_codec_write(codec, pin_nid, 0,
632 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800633 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800634 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800635 snd_printdd(KERN_NOTICE
636 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800637 break;
638 }
639 }
640
641 hdmi_debug_channel_mapping(codec, pin_nid);
642}
643
Takashi Iwaid45e6882012-07-31 11:36:00 +0200644struct channel_map_table {
645 unsigned char map; /* ALSA API channel map position */
646 unsigned char cea_slot; /* CEA slot value */
647 int spk_mask; /* speaker position bit mask */
648};
649
650static struct channel_map_table map_tables[] = {
651 { SNDRV_CHMAP_FL, 0x00, FL },
652 { SNDRV_CHMAP_FR, 0x01, FR },
653 { SNDRV_CHMAP_RL, 0x04, RL },
654 { SNDRV_CHMAP_RR, 0x05, RR },
655 { SNDRV_CHMAP_LFE, 0x02, LFE },
656 { SNDRV_CHMAP_FC, 0x03, FC },
657 { SNDRV_CHMAP_RLC, 0x06, RLC },
658 { SNDRV_CHMAP_RRC, 0x07, RRC },
659 {} /* terminator */
660};
661
662/* from ALSA API channel position to speaker bit mask */
663static int to_spk_mask(unsigned char c)
664{
665 struct channel_map_table *t = map_tables;
666 for (; t->map; t++) {
667 if (t->map == c)
668 return t->spk_mask;
669 }
670 return 0;
671}
672
673/* from ALSA API channel position to CEA slot */
674static int to_cea_slot(unsigned char c)
675{
676 struct channel_map_table *t = map_tables;
677 for (; t->map; t++) {
678 if (t->map == c)
679 return t->cea_slot;
680 }
681 return 0x0f;
682}
683
684/* from CEA slot to ALSA API channel position */
685static int from_cea_slot(unsigned char c)
686{
687 struct channel_map_table *t = map_tables;
688 for (; t->map; t++) {
689 if (t->cea_slot == c)
690 return t->map;
691 }
692 return 0;
693}
694
695/* from speaker bit mask to ALSA API channel position */
696static int spk_to_chmap(int spk)
697{
698 struct channel_map_table *t = map_tables;
699 for (; t->map; t++) {
700 if (t->spk_mask == spk)
701 return t->map;
702 }
703 return 0;
704}
705
706/* get the CA index corresponding to the given ALSA API channel map */
707static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
708{
709 int i, spks = 0, spk_mask = 0;
710
711 for (i = 0; i < chs; i++) {
712 int mask = to_spk_mask(map[i]);
713 if (mask) {
714 spk_mask |= mask;
715 spks++;
716 }
717 }
718
719 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
720 if ((chs == channel_allocations[i].channels ||
721 spks == channel_allocations[i].channels) &&
722 (spk_mask & channel_allocations[i].spk_mask) ==
723 channel_allocations[i].spk_mask)
724 return channel_allocations[i].ca_index;
725 }
726 return -1;
727}
728
729/* set up the channel slots for the given ALSA API channel map */
730static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
731 hda_nid_t pin_nid,
732 int chs, unsigned char *map)
733{
734 int i;
735 for (i = 0; i < 8; i++) {
736 int val, err;
737 if (i < chs)
738 val = to_cea_slot(map[i]);
739 else
740 val = 0xf;
741 val |= (i << 4);
742 err = snd_hda_codec_write(codec, pin_nid, 0,
743 AC_VERB_SET_HDMI_CHAN_SLOT, val);
744 if (err)
745 return -EINVAL;
746 }
747 return 0;
748}
749
750/* store ALSA API channel map from the current default map */
751static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
752{
753 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300754 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200755 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300756 if (i < channel_allocations[ordered_ca].channels)
757 map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200758 else
759 map[i] = 0;
760 }
761}
762
763static void hdmi_setup_channel_mapping(struct hda_codec *codec,
764 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200765 int channels, unsigned char *map,
766 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200767{
Anssi Hannula20608732013-02-03 17:55:45 +0200768 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200769 hdmi_manual_setup_channel_mapping(codec, pin_nid,
770 channels, map);
771 } else {
772 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
773 hdmi_setup_fake_chmap(map, ca);
774 }
775}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800776
777/*
778 * Audio InfoFrame routines
779 */
780
781/*
782 * Enable Audio InfoFrame Transmission
783 */
784static void hdmi_start_infoframe_trans(struct hda_codec *codec,
785 hda_nid_t pin_nid)
786{
787 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
788 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
789 AC_DIPXMIT_BEST);
790}
791
792/*
793 * Disable Audio InfoFrame Transmission
794 */
795static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
796 hda_nid_t pin_nid)
797{
798 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
799 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
800 AC_DIPXMIT_DISABLE);
801}
802
803static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
804{
805#ifdef CONFIG_SND_DEBUG_VERBOSE
806 int i;
807 int size;
808
809 size = snd_hdmi_get_eld_size(codec, pin_nid);
810 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
811
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 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
816 }
817#endif
818}
819
820static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
821{
822#ifdef BE_PARANOID
823 int i, j;
824 int size;
825 int pi, bi;
826 for (i = 0; i < 8; i++) {
827 size = snd_hda_codec_read(codec, pin_nid, 0,
828 AC_VERB_GET_HDMI_DIP_SIZE, i);
829 if (size == 0)
830 continue;
831
832 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
833 for (j = 1; j < 1000; j++) {
834 hdmi_write_dip_byte(codec, pin_nid, 0x0);
835 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
836 if (pi != i)
837 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
838 bi, pi, i);
839 if (bi == 0) /* byte index wrapped around */
840 break;
841 }
842 snd_printd(KERN_INFO
843 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
844 i, size, j);
845 }
846#endif
847}
848
Wu Fengguang53d7d692010-09-21 14:25:49 +0800849static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800850{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800851 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852 u8 sum = 0;
853 int i;
854
Wu Fengguang53d7d692010-09-21 14:25:49 +0800855 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856
Wu Fengguang53d7d692010-09-21 14:25:49 +0800857 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800858 sum += bytes[i];
859
Wu Fengguang53d7d692010-09-21 14:25:49 +0800860 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800861}
862
863static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
864 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800865 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800866{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800867 int i;
868
869 hdmi_debug_dip_size(codec, pin_nid);
870 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
871
Wu Fengguang079d88c2010-03-08 10:44:23 +0800872 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800873 for (i = 0; i < size; i++)
874 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875}
876
877static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800878 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800879{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800880 u8 val;
881 int i;
882
883 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
884 != AC_DIPXMIT_BEST)
885 return false;
886
887 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800888 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800889 val = snd_hda_codec_read(codec, pin_nid, 0,
890 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800891 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800892 return false;
893 }
894
895 return true;
896}
897
Takashi Iwaib0540872013-09-02 12:33:02 +0200898static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
899 struct hdmi_spec_per_pin *per_pin,
900 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800901{
Stephen Warren384a48d2011-06-01 11:14:21 -0600902 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +0200903 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300904 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600905 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300906 int ca, ordered_ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100907 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800908
Takashi Iwaib0540872013-09-02 12:33:02 +0200909 if (!channels)
910 return;
911
Mengdong Lin58f7d282013-09-04 16:37:12 -0400912 if (is_haswell(codec))
913 snd_hda_codec_write(codec, pin_nid, 0,
914 AC_VERB_SET_AMP_GAIN_MUTE,
915 AMP_OUT_UNMUTE);
916
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100917 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -0600918 if (!eld->monitor_present)
919 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800920
Takashi Iwaid45e6882012-07-31 11:36:00 +0200921 if (!non_pcm && per_pin->chmap_set)
922 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
923 else
924 ca = hdmi_channel_allocation(eld, channels);
925 if (ca < 0)
926 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800927
Anssi Hannula1df5a062013-10-05 02:25:40 +0300928 ordered_ca = get_channel_allocation_order(ca);
929 active_channels = channel_allocations[ordered_ca].channels;
930
931 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
932
Stephen Warren384a48d2011-06-01 11:14:21 -0600933 memset(&ai, 0, sizeof(ai));
David Henningsson1613d6b2013-02-19 16:11:24 +0100934 if (eld->info.conn_type == 0) { /* HDMI */
Stephen Warren384a48d2011-06-01 11:14:21 -0600935 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800936
Stephen Warren384a48d2011-06-01 11:14:21 -0600937 hdmi_ai->type = 0x84;
938 hdmi_ai->ver = 0x01;
939 hdmi_ai->len = 0x0a;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300940 hdmi_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600941 hdmi_ai->CA = ca;
942 hdmi_checksum_audio_infoframe(hdmi_ai);
David Henningsson1613d6b2013-02-19 16:11:24 +0100943 } else if (eld->info.conn_type == 1) { /* DisplayPort */
Stephen Warren384a48d2011-06-01 11:14:21 -0600944 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800945
Stephen Warren384a48d2011-06-01 11:14:21 -0600946 dp_ai->type = 0x84;
947 dp_ai->len = 0x1b;
948 dp_ai->ver = 0x11 << 2;
Anssi Hannula1df5a062013-10-05 02:25:40 +0300949 dp_ai->CC02_CT47 = active_channels - 1;
Stephen Warren384a48d2011-06-01 11:14:21 -0600950 dp_ai->CA = ca;
951 } else {
952 snd_printd("HDMI: unknown connection type at pin %d\n",
953 pin_nid);
954 return;
955 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800956
Stephen Warren384a48d2011-06-01 11:14:21 -0600957 /*
958 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
959 * sizeof(*dp_ai) to avoid partial match/update problems when
960 * the user switches between HDMI/DP monitors.
961 */
962 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
963 sizeof(ai))) {
964 snd_printdd("hdmi_setup_audio_infoframe: "
965 "pin=%d channels=%d\n",
966 pin_nid,
Anssi Hannula1df5a062013-10-05 02:25:40 +0300967 active_channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200968 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200969 channels, per_pin->chmap,
970 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600971 hdmi_stop_infoframe_trans(codec, pin_nid);
972 hdmi_fill_audio_infoframe(codec, pin_nid,
973 ai.bytes, sizeof(ai));
974 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800975 } else {
976 /* For non-pcm audio switch, setup new channel mapping
977 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200978 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200979 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200980 channels, per_pin->chmap,
981 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800982 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800983
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200984 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800985}
986
987
988/*
989 * Unsolicited events
990 */
991
Wu Fengguangc6e84532011-11-18 16:59:32 -0600992static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200993
Wu Fengguang079d88c2010-03-08 10:44:23 +0800994static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
995{
996 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200997 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
998 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600999 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +02001000 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001001 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001002
1003 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1004 if (!jack)
1005 return;
1006 pin_nid = jack->nid;
1007 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001008
Fengguang Wufae3d882012-04-10 17:00:35 +08001009 _snd_printd(SND_PR_VERBOSE,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001010 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1011 codec->addr, pin_nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001012 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001013
Stephen Warren384a48d2011-06-01 11:14:21 -06001014 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1015 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001016 return;
1017
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001018 hdmi_present_sense(get_pin(spec, pin_idx), 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +02001019 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001020}
1021
1022static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1023{
1024 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1025 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1026 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1027 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1028
1029 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001030 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001031 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001032 tag,
1033 subtag,
1034 cp_state,
1035 cp_ready);
1036
1037 /* TODO */
1038 if (cp_state)
1039 ;
1040 if (cp_ready)
1041 ;
1042}
1043
1044
1045static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1046{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001047 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1048 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1049
Takashi Iwai3a938972011-10-28 01:16:55 +02001050 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001051 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1052 return;
1053 }
1054
1055 if (subtag == 0)
1056 hdmi_intrinsic_event(codec, res);
1057 else
1058 hdmi_non_intrinsic_event(codec, res);
1059}
1060
Mengdong Lin58f7d282013-09-04 16:37:12 -04001061static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001062 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001063{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001064 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001065
Wang Xingchao53b434f2013-06-18 10:41:53 +08001066 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1067 * thus pins could only choose converter 0 for use. Make sure the
1068 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001069 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001070 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1071
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001072 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001073 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1074 AC_PWRST_D0);
1075 msleep(40);
1076 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1077 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1078 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1079 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001080}
1081
Wu Fengguang079d88c2010-03-08 10:44:23 +08001082/*
1083 * Callbacks
1084 */
1085
Takashi Iwai92f10b32010-08-03 14:21:00 +02001086/* HBR should be Non-PCM, 8 channels */
1087#define is_hbr_format(format) \
1088 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1089
Stephen Warren384a48d2011-06-01 11:14:21 -06001090static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1091 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001092{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001093 int pinctl;
1094 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001095
Mengdong Linfb87fa32013-09-04 16:36:57 -04001096 if (is_haswell(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001097 haswell_verify_D0(codec, cvt_nid, pin_nid);
David Henningsson83f26ad2013-04-10 12:26:07 +02001098
Stephen Warren384a48d2011-06-01 11:14:21 -06001099 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1100 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001101 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1102
1103 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001104 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001105 new_pinctl |= AC_PINCTL_EPT_HBR;
1106 else
1107 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1108
1109 snd_printdd("hdmi_setup_stream: "
1110 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001111 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001112 pinctl == new_pinctl ? "" : "new-",
1113 new_pinctl);
1114
1115 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001116 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001117 AC_VERB_SET_PIN_WIDGET_CONTROL,
1118 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001119
Stephen Warren384a48d2011-06-01 11:14:21 -06001120 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001121 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001122 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1123 return -EINVAL;
1124 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001125
Stephen Warren384a48d2011-06-01 11:14:21 -06001126 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001127 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001128}
1129
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001130static int hdmi_choose_cvt(struct hda_codec *codec,
1131 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001132{
1133 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001135 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001136 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001137
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001138 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001139
Stephen Warren384a48d2011-06-01 11:14:21 -06001140 /* Dynamically assign converter to stream */
1141 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001142 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001143
1144 /* Must not already be assigned */
1145 if (per_cvt->assigned)
1146 continue;
1147 /* Must be in pin's mux's list of converters */
1148 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1149 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1150 break;
1151 /* Not in mux list */
1152 if (mux_idx == per_pin->num_mux_nids)
1153 continue;
1154 break;
1155 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001156
Stephen Warren384a48d2011-06-01 11:14:21 -06001157 /* No free converters */
1158 if (cvt_idx == spec->num_cvts)
1159 return -ENODEV;
1160
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001161 if (cvt_id)
1162 *cvt_id = cvt_idx;
1163 if (mux_id)
1164 *mux_id = mux_idx;
1165
1166 return 0;
1167}
1168
1169static void haswell_config_cvts(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001170 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001171{
1172 struct hdmi_spec *spec = codec->spec;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001173 hda_nid_t nid, end_nid;
1174 int cvt_idx, curr;
1175 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001176
Mengdong Linf82d7d12013-09-21 20:34:45 -04001177 /* configure all pins, including "no physical connection" ones */
1178 end_nid = codec->start_nid + codec->num_nodes;
1179 for (nid = codec->start_nid; nid < end_nid; nid++) {
1180 unsigned int wid_caps = get_wcaps(codec, nid);
1181 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001182
Mengdong Linf82d7d12013-09-21 20:34:45 -04001183 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001184 continue;
1185
Mengdong Linf82d7d12013-09-21 20:34:45 -04001186 if (nid == pin_nid)
1187 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001188
Mengdong Linf82d7d12013-09-21 20:34:45 -04001189 curr = snd_hda_codec_read(codec, nid, 0,
1190 AC_VERB_GET_CONNECT_SEL, 0);
1191 if (curr != mux_idx)
1192 continue;
1193
1194 /* choose an unassigned converter. The conveters in the
1195 * connection list are in the same order as in the codec.
1196 */
1197 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1198 per_cvt = get_cvt(spec, cvt_idx);
1199 if (!per_cvt->assigned) {
1200 snd_printdd("choose cvt %d for pin nid %d\n",
1201 cvt_idx, nid);
1202 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001203 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001204 cvt_idx);
1205 break;
1206 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001207 }
1208 }
1209}
1210
1211/*
1212 * HDA PCM callbacks
1213 */
1214static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1215 struct hda_codec *codec,
1216 struct snd_pcm_substream *substream)
1217{
1218 struct hdmi_spec *spec = codec->spec;
1219 struct snd_pcm_runtime *runtime = substream->runtime;
1220 int pin_idx, cvt_idx, mux_idx = 0;
1221 struct hdmi_spec_per_pin *per_pin;
1222 struct hdmi_eld *eld;
1223 struct hdmi_spec_per_cvt *per_cvt = NULL;
1224 int err;
1225
1226 /* Validate hinfo */
1227 pin_idx = hinfo_to_pin_index(spec, hinfo);
1228 if (snd_BUG_ON(pin_idx < 0))
1229 return -EINVAL;
1230 per_pin = get_pin(spec, pin_idx);
1231 eld = &per_pin->sink_eld;
1232
1233 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1234 if (err < 0)
1235 return err;
1236
1237 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001238 /* Claim converter */
1239 per_cvt->assigned = 1;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001240 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001241 hinfo->nid = per_cvt->cvt_nid;
1242
Takashi Iwaibddee962013-06-18 16:14:22 +02001243 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001244 AC_VERB_SET_CONNECT_SEL,
1245 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001246
1247 /* configure unused pins to choose other converters */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001248 if (is_haswell(codec))
Mengdong Linf82d7d12013-09-21 20:34:45 -04001249 haswell_config_cvts(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001250
Stephen Warren384a48d2011-06-01 11:14:21 -06001251 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001252
Stephen Warren2def8172011-06-01 11:14:20 -06001253 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001254 hinfo->channels_min = per_cvt->channels_min;
1255 hinfo->channels_max = per_cvt->channels_max;
1256 hinfo->rates = per_cvt->rates;
1257 hinfo->formats = per_cvt->formats;
1258 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001259
Stephen Warren384a48d2011-06-01 11:14:21 -06001260 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001261 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001262 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001263 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001264 !hinfo->rates || !hinfo->formats) {
1265 per_cvt->assigned = 0;
1266 hinfo->nid = 0;
1267 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001268 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001269 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001270 }
Stephen Warren2def8172011-06-01 11:14:20 -06001271
1272 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001273 runtime->hw.channels_min = hinfo->channels_min;
1274 runtime->hw.channels_max = hinfo->channels_max;
1275 runtime->hw.formats = hinfo->formats;
1276 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001277
1278 snd_pcm_hw_constraint_step(substream->runtime, 0,
1279 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001280 return 0;
1281}
1282
1283/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001284 * HDA/HDMI auto parsing
1285 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001286static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001287{
1288 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001289 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001290 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001291
1292 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1293 snd_printk(KERN_WARNING
1294 "HDMI: pin %d wcaps %#x "
1295 "does not support connection list\n",
1296 pin_nid, get_wcaps(codec, pin_nid));
1297 return -EINVAL;
1298 }
1299
Stephen Warren384a48d2011-06-01 11:14:21 -06001300 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1301 per_pin->mux_nids,
1302 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001303
1304 return 0;
1305}
1306
Wu Fengguangc6e84532011-11-18 16:59:32 -06001307static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001308{
Wu Fengguang744626d2011-11-16 16:29:47 +08001309 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001310 struct hdmi_spec *spec = codec->spec;
1311 struct hdmi_eld *eld = &spec->temp_eld;
1312 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001313 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001314 /*
1315 * Always execute a GetPinSense verb here, even when called from
1316 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1317 * response's PD bit is not the real PD value, but indicates that
1318 * the real PD value changed. An older version of the HD-audio
1319 * specification worked this way. Hence, we just ignore the data in
1320 * the unsolicited response to avoid custom WARs.
1321 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001322 int present = snd_hda_pin_sense(codec, pin_nid);
David Henningsson4bd038f2013-02-19 16:11:25 +01001323 bool update_eld = false;
1324 bool eld_changed = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001325
David Henningsson4bd038f2013-02-19 16:11:25 +01001326 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1327 if (pin_eld->monitor_present)
1328 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1329 else
1330 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001331
Fengguang Wufae3d882012-04-10 17:00:35 +08001332 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001333 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001334 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001335
David Henningsson4bd038f2013-02-19 16:11:25 +01001336 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001337 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1338 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001339 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001340 else {
1341 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1342 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1343 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001344 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001345 }
1346
David Henningsson4bd038f2013-02-19 16:11:25 +01001347 if (eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001348 snd_hdmi_show_eld(&eld->info);
David Henningsson4bd038f2013-02-19 16:11:25 +01001349 update_eld = true;
David Henningsson1613d6b2013-02-19 16:11:24 +01001350 }
Wu Fengguangc6e84532011-11-18 16:59:32 -06001351 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001352 queue_delayed_work(codec->bus->workq,
1353 &per_pin->work,
1354 msecs_to_jiffies(300));
David Henningsson4bd038f2013-02-19 16:11:25 +01001355 return;
Wu Fengguang744626d2011-11-16 16:29:47 +08001356 }
1357 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001358
1359 mutex_lock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001360 if (pin_eld->eld_valid && !eld->eld_valid) {
David Henningsson4bd038f2013-02-19 16:11:25 +01001361 update_eld = true;
David Henningsson92c69e72013-02-19 16:11:26 +01001362 eld_changed = true;
1363 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001364 if (update_eld) {
Takashi Iwaib0540872013-09-02 12:33:02 +02001365 bool old_eld_valid = pin_eld->eld_valid;
David Henningsson4bd038f2013-02-19 16:11:25 +01001366 pin_eld->eld_valid = eld->eld_valid;
David Henningsson92c69e72013-02-19 16:11:26 +01001367 eld_changed = pin_eld->eld_size != eld->eld_size ||
1368 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
David Henningsson4bd038f2013-02-19 16:11:25 +01001369 eld->eld_size) != 0;
1370 if (eld_changed)
1371 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1372 eld->eld_size);
1373 pin_eld->eld_size = eld->eld_size;
1374 pin_eld->info = eld->info;
Takashi Iwaib0540872013-09-02 12:33:02 +02001375
1376 /* Haswell-specific workaround: re-setup when the transcoder is
1377 * changed during the stream playback
1378 */
Mengdong Linfb87fa32013-09-04 16:36:57 -04001379 if (is_haswell(codec) &&
Mengdong Lin58f7d282013-09-04 16:37:12 -04001380 eld->eld_valid && !old_eld_valid && per_pin->setup)
Takashi Iwaib0540872013-09-02 12:33:02 +02001381 hdmi_setup_audio_infoframe(codec, per_pin,
1382 per_pin->non_pcm);
David Henningsson4bd038f2013-02-19 16:11:25 +01001383 }
1384 mutex_unlock(&pin_eld->lock);
David Henningsson92c69e72013-02-19 16:11:26 +01001385
1386 if (eld_changed)
1387 snd_ctl_notify(codec->bus->card,
1388 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1389 &per_pin->eld_ctl->id);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001390}
1391
Wu Fengguang744626d2011-11-16 16:29:47 +08001392static void hdmi_repoll_eld(struct work_struct *work)
1393{
1394 struct hdmi_spec_per_pin *per_pin =
1395 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1396
Wu Fengguangc6e84532011-11-18 16:59:32 -06001397 if (per_pin->repoll_count++ > 6)
1398 per_pin->repoll_count = 0;
1399
1400 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001401}
1402
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001403static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1404 hda_nid_t nid);
1405
Wu Fengguang079d88c2010-03-08 10:44:23 +08001406static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1407{
1408 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001409 unsigned int caps, config;
1410 int pin_idx;
1411 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001412 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001413
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001414 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001415 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1416 return 0;
1417
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001418 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001419 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1420 return 0;
1421
Mengdong Linfb87fa32013-09-04 16:36:57 -04001422 if (is_haswell(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001423 intel_haswell_fixup_connect_list(codec, pin_nid);
1424
Stephen Warren384a48d2011-06-01 11:14:21 -06001425 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001426 per_pin = snd_array_new(&spec->pins);
1427 if (!per_pin)
1428 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001429
1430 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001431 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001432
Stephen Warren384a48d2011-06-01 11:14:21 -06001433 err = hdmi_read_pin_conn(codec, pin_idx);
1434 if (err < 0)
1435 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001436
Wu Fengguang079d88c2010-03-08 10:44:23 +08001437 spec->num_pins++;
1438
Stephen Warren384a48d2011-06-01 11:14:21 -06001439 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001440}
1441
Stephen Warren384a48d2011-06-01 11:14:21 -06001442static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001443{
1444 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001445 struct hdmi_spec_per_cvt *per_cvt;
1446 unsigned int chans;
1447 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001448
Stephen Warren384a48d2011-06-01 11:14:21 -06001449 chans = get_wcaps(codec, cvt_nid);
1450 chans = get_wcaps_channels(chans);
1451
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001452 per_cvt = snd_array_new(&spec->cvts);
1453 if (!per_cvt)
1454 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001455
1456 per_cvt->cvt_nid = cvt_nid;
1457 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001458 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001459 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001460 if (chans > spec->channels_max)
1461 spec->channels_max = chans;
1462 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001463
1464 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1465 &per_cvt->rates,
1466 &per_cvt->formats,
1467 &per_cvt->maxbps);
1468 if (err < 0)
1469 return err;
1470
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001471 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1472 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1473 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001474
1475 return 0;
1476}
1477
1478static int hdmi_parse_codec(struct hda_codec *codec)
1479{
1480 hda_nid_t nid;
1481 int i, nodes;
1482
1483 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1484 if (!nid || nodes < 0) {
1485 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1486 return -EINVAL;
1487 }
1488
1489 for (i = 0; i < nodes; i++, nid++) {
1490 unsigned int caps;
1491 unsigned int type;
1492
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001493 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001494 type = get_wcaps_type(caps);
1495
1496 if (!(caps & AC_WCAP_DIGITAL))
1497 continue;
1498
1499 switch (type) {
1500 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001501 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001502 break;
1503 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001504 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001505 break;
1506 }
1507 }
1508
David Henningssonc9adeef2012-11-07 09:22:33 +01001509#ifdef CONFIG_PM
1510 /* We're seeing some problems with unsolicited hot plug events on
1511 * PantherPoint after S3, if this is not enabled */
1512 if (codec->vendor_id == 0x80862806)
1513 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001514 /*
1515 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1516 * can be lost and presence sense verb will become inaccurate if the
1517 * HDA link is powered off at hot plug or hw initialization time.
1518 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001519 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001520 AC_PWRST_EPSS))
1521 codec->bus->power_keep_link_on = 1;
1522#endif
1523
1524 return 0;
1525}
1526
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001527/*
1528 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001529static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1530{
1531 struct hda_spdif_out *spdif;
1532 bool non_pcm;
1533
1534 mutex_lock(&codec->spdif_mutex);
1535 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1536 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1537 mutex_unlock(&codec->spdif_mutex);
1538 return non_pcm;
1539}
1540
1541
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001542/*
1543 * HDMI callbacks
1544 */
1545
1546static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1547 struct hda_codec *codec,
1548 unsigned int stream_tag,
1549 unsigned int format,
1550 struct snd_pcm_substream *substream)
1551{
Stephen Warren384a48d2011-06-01 11:14:21 -06001552 hda_nid_t cvt_nid = hinfo->nid;
1553 struct hdmi_spec *spec = codec->spec;
1554 int pin_idx = hinfo_to_pin_index(spec, hinfo);
Takashi Iwaib0540872013-09-02 12:33:02 +02001555 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1556 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001557 bool non_pcm;
1558
1559 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaib0540872013-09-02 12:33:02 +02001560 per_pin->channels = substream->runtime->channels;
1561 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001562
Takashi Iwaib0540872013-09-02 12:33:02 +02001563 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Stephen Warren384a48d2011-06-01 11:14:21 -06001564
1565 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001566}
1567
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001568static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1569 struct hda_codec *codec,
1570 struct snd_pcm_substream *substream)
1571{
1572 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1573 return 0;
1574}
1575
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001576static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1577 struct hda_codec *codec,
1578 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001579{
1580 struct hdmi_spec *spec = codec->spec;
1581 int cvt_idx, pin_idx;
1582 struct hdmi_spec_per_cvt *per_cvt;
1583 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001584
Stephen Warren384a48d2011-06-01 11:14:21 -06001585 if (hinfo->nid) {
1586 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1587 if (snd_BUG_ON(cvt_idx < 0))
1588 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001589 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001590
1591 snd_BUG_ON(!per_cvt->assigned);
1592 per_cvt->assigned = 0;
1593 hinfo->nid = 0;
1594
1595 pin_idx = hinfo_to_pin_index(spec, hinfo);
1596 if (snd_BUG_ON(pin_idx < 0))
1597 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001598 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001599
Stephen Warren384a48d2011-06-01 11:14:21 -06001600 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001601 per_pin->chmap_set = false;
1602 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02001603
1604 per_pin->setup = false;
1605 per_pin->channels = 0;
Stephen Warren384a48d2011-06-01 11:14:21 -06001606 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001607
Stephen Warren384a48d2011-06-01 11:14:21 -06001608 return 0;
1609}
1610
1611static const struct hda_pcm_ops generic_ops = {
1612 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001613 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001614 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001615 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001616};
1617
Takashi Iwaid45e6882012-07-31 11:36:00 +02001618/*
1619 * ALSA API channel-map control callbacks
1620 */
1621static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_info *uinfo)
1623{
1624 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1625 struct hda_codec *codec = info->private_data;
1626 struct hdmi_spec *spec = codec->spec;
1627 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1628 uinfo->count = spec->channels_max;
1629 uinfo->value.integer.min = 0;
1630 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1631 return 0;
1632}
1633
1634static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1635 unsigned int size, unsigned int __user *tlv)
1636{
1637 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1638 struct hda_codec *codec = info->private_data;
1639 struct hdmi_spec *spec = codec->spec;
1640 const unsigned int valid_mask =
1641 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1642 unsigned int __user *dst;
1643 int chs, count = 0;
1644
1645 if (size < 8)
1646 return -ENOMEM;
1647 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1648 return -EFAULT;
1649 size -= 8;
1650 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001651 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001652 int i, c;
1653 struct cea_channel_speaker_allocation *cap;
1654 cap = channel_allocations;
1655 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1656 int chs_bytes = chs * 4;
1657 if (cap->channels != chs)
1658 continue;
1659 if (cap->spk_mask & ~valid_mask)
1660 continue;
1661 if (size < 8)
1662 return -ENOMEM;
1663 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1664 put_user(chs_bytes, dst + 1))
1665 return -EFAULT;
1666 dst += 2;
1667 size -= 8;
1668 count += 8;
1669 if (size < chs_bytes)
1670 return -ENOMEM;
1671 size -= chs_bytes;
1672 count += chs_bytes;
1673 for (c = 7; c >= 0; c--) {
1674 int spk = cap->speakers[c];
1675 if (!spk)
1676 continue;
1677 if (put_user(spk_to_chmap(spk), dst))
1678 return -EFAULT;
1679 dst++;
1680 }
1681 }
1682 }
1683 if (put_user(count, tlv + 1))
1684 return -EFAULT;
1685 return 0;
1686}
1687
1688static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_value *ucontrol)
1690{
1691 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1692 struct hda_codec *codec = info->private_data;
1693 struct hdmi_spec *spec = codec->spec;
1694 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001695 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001696 int i;
1697
1698 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1699 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1700 return 0;
1701}
1702
1703static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol)
1705{
1706 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1707 struct hda_codec *codec = info->private_data;
1708 struct hdmi_spec *spec = codec->spec;
1709 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001710 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001711 unsigned int ctl_idx;
1712 struct snd_pcm_substream *substream;
1713 unsigned char chmap[8];
1714 int i, ca, prepared = 0;
1715
1716 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1717 substream = snd_pcm_chmap_substream(info, ctl_idx);
1718 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001719 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001720 switch (substream->runtime->status->state) {
1721 case SNDRV_PCM_STATE_OPEN:
1722 case SNDRV_PCM_STATE_SETUP:
1723 break;
1724 case SNDRV_PCM_STATE_PREPARED:
1725 prepared = 1;
1726 break;
1727 default:
1728 return -EBUSY;
1729 }
1730 memset(chmap, 0, sizeof(chmap));
1731 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1732 chmap[i] = ucontrol->value.integer.value[i];
1733 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1734 return 0;
1735 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1736 if (ca < 0)
1737 return -EINVAL;
1738 per_pin->chmap_set = true;
1739 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1740 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02001741 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001742
1743 return 0;
1744}
1745
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001746static int generic_hdmi_build_pcms(struct hda_codec *codec)
1747{
1748 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001749 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001750
Stephen Warren384a48d2011-06-01 11:14:21 -06001751 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1752 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001753 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001754 struct hdmi_spec_per_pin *per_pin;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001755
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001756 per_pin = get_pin(spec, pin_idx);
1757 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1758 info = snd_array_new(&spec->pcm_rec);
1759 if (!info)
1760 return -ENOMEM;
1761 info->name = per_pin->pcm_name;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001762 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001763 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001764
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001765 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001766 pstr->substreams = 1;
1767 pstr->ops = generic_ops;
1768 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001769 }
1770
Stephen Warren384a48d2011-06-01 11:14:21 -06001771 codec->num_pcms = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001772 codec->pcm_info = spec->pcm_rec.list;
Stephen Warren384a48d2011-06-01 11:14:21 -06001773
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001774 return 0;
1775}
1776
David Henningsson0b6c49b2011-08-23 16:56:03 +02001777static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1778{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001779 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001780 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001781 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1782 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
David Henningsson0b6c49b2011-08-23 16:56:03 +02001783
Takashi Iwai31ef2252011-12-01 17:41:36 +01001784 if (pcmdev > 0)
1785 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson30efd8d2013-02-22 10:16:28 +01001786 if (!is_jack_detectable(codec, per_pin->pin_nid))
1787 strncat(hdmi_str, " Phantom",
1788 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001789
Takashi Iwai31ef2252011-12-01 17:41:36 +01001790 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001791}
1792
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001793static int generic_hdmi_build_controls(struct hda_codec *codec)
1794{
1795 struct hdmi_spec *spec = codec->spec;
1796 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001797 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001798
Stephen Warren384a48d2011-06-01 11:14:21 -06001799 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001800 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001801
1802 err = generic_hdmi_build_jack(codec, pin_idx);
1803 if (err < 0)
1804 return err;
1805
Takashi Iwaidcda5802012-10-12 17:24:51 +02001806 err = snd_hda_create_dig_out_ctls(codec,
1807 per_pin->pin_nid,
1808 per_pin->mux_nids[0],
1809 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001810 if (err < 0)
1811 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001812 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001813
1814 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001815 err = hdmi_create_eld_ctl(codec, pin_idx,
1816 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001817
1818 if (err < 0)
1819 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001820
Takashi Iwai82b1d732011-12-20 15:53:07 +01001821 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001822 }
1823
Takashi Iwaid45e6882012-07-31 11:36:00 +02001824 /* add channel maps */
1825 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1826 struct snd_pcm_chmap *chmap;
1827 struct snd_kcontrol *kctl;
1828 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02001829
1830 if (!codec->pcm_info[pin_idx].pcm)
1831 break;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001832 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1833 SNDRV_PCM_STREAM_PLAYBACK,
1834 NULL, 0, pin_idx, &chmap);
1835 if (err < 0)
1836 return err;
1837 /* override handlers */
1838 chmap->private_data = codec;
1839 kctl = chmap->kctl;
1840 for (i = 0; i < kctl->count; i++)
1841 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1842 kctl->info = hdmi_chmap_ctl_info;
1843 kctl->get = hdmi_chmap_ctl_get;
1844 kctl->put = hdmi_chmap_ctl_put;
1845 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1846 }
1847
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001848 return 0;
1849}
1850
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001851static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1852{
1853 struct hdmi_spec *spec = codec->spec;
1854 int pin_idx;
1855
1856 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001857 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001858 struct hdmi_eld *eld = &per_pin->sink_eld;
1859
1860 per_pin->codec = codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001861 mutex_init(&eld->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001862 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1863 snd_hda_eld_proc_new(codec, eld, pin_idx);
1864 }
1865 return 0;
1866}
1867
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001868static int generic_hdmi_init(struct hda_codec *codec)
1869{
1870 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001871 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001872
Stephen Warren384a48d2011-06-01 11:14:21 -06001873 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001874 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001875 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001876
1877 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001878 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001879 }
1880 return 0;
1881}
1882
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001883static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1884{
1885 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1886 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1887 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1888}
1889
1890static void hdmi_array_free(struct hdmi_spec *spec)
1891{
1892 snd_array_free(&spec->pins);
1893 snd_array_free(&spec->cvts);
1894 snd_array_free(&spec->pcm_rec);
1895}
1896
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001897static void generic_hdmi_free(struct hda_codec *codec)
1898{
1899 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001900 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001901
Stephen Warren384a48d2011-06-01 11:14:21 -06001902 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001903 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001904 struct hdmi_eld *eld = &per_pin->sink_eld;
1905
Wu Fengguang744626d2011-11-16 16:29:47 +08001906 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001907 snd_hda_eld_proc_free(codec, eld);
1908 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001909
Wu Fengguang744626d2011-11-16 16:29:47 +08001910 flush_workqueue(codec->bus->workq);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001911 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001912 kfree(spec);
1913}
1914
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001915#ifdef CONFIG_PM
1916static int generic_hdmi_resume(struct hda_codec *codec)
1917{
1918 struct hdmi_spec *spec = codec->spec;
1919 int pin_idx;
1920
1921 generic_hdmi_init(codec);
1922 snd_hda_codec_resume_amp(codec);
1923 snd_hda_codec_resume_cache(codec);
1924
1925 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1926 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1927 hdmi_present_sense(per_pin, 1);
1928 }
1929 return 0;
1930}
1931#endif
1932
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001933static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001934 .init = generic_hdmi_init,
1935 .free = generic_hdmi_free,
1936 .build_pcms = generic_hdmi_build_pcms,
1937 .build_controls = generic_hdmi_build_controls,
1938 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04001939#ifdef CONFIG_PM
1940 .resume = generic_hdmi_resume,
1941#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001942};
1943
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001944
1945static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1946 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001947{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001948 struct hdmi_spec *spec = codec->spec;
1949 hda_nid_t conns[4];
1950 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001951
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001952 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1953 if (nconns == spec->num_cvts &&
1954 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001955 return;
1956
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001957 /* override pins connection list */
1958 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1959 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001960}
1961
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001962#define INTEL_VENDOR_NID 0x08
1963#define INTEL_GET_VENDOR_VERB 0xf81
1964#define INTEL_SET_VENDOR_VERB 0x781
1965#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1966#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1967
1968static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02001969 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001970{
1971 unsigned int vendor_param;
1972
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001973 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1974 INTEL_GET_VENDOR_VERB, 0);
1975 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1976 return;
1977
1978 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1979 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1980 INTEL_SET_VENDOR_VERB, vendor_param);
1981 if (vendor_param == -1)
1982 return;
1983
Takashi Iwai17df3f52013-05-08 08:09:34 +02001984 if (update_tree)
1985 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001986}
1987
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001988static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1989{
1990 unsigned int vendor_param;
1991
1992 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1993 INTEL_GET_VENDOR_VERB, 0);
1994 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1995 return;
1996
1997 /* enable DP1.2 mode */
1998 vendor_param |= INTEL_EN_DP12;
1999 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2000 INTEL_SET_VENDOR_VERB, vendor_param);
2001}
2002
Takashi Iwai17df3f52013-05-08 08:09:34 +02002003/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2004 * Otherwise you may get severe h/w communication errors.
2005 */
2006static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2007 unsigned int power_state)
2008{
2009 if (power_state == AC_PWRST_D0) {
2010 intel_haswell_enable_all_pins(codec, false);
2011 intel_haswell_fixup_enable_dp12(codec);
2012 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002013
Takashi Iwai17df3f52013-05-08 08:09:34 +02002014 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2015 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2016}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002017
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002018static int patch_generic_hdmi(struct hda_codec *codec)
2019{
2020 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002021
2022 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2023 if (spec == NULL)
2024 return -ENOMEM;
2025
2026 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002027 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002028
Mengdong Linfb87fa32013-09-04 16:36:57 -04002029 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002030 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002031 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002032 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002033
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002034 if (hdmi_parse_codec(codec) < 0) {
2035 codec->spec = NULL;
2036 kfree(spec);
2037 return -EINVAL;
2038 }
2039 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Linfb87fa32013-09-04 16:36:57 -04002040 if (is_haswell(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002041 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002042 codec->dp_mst = true;
2043 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002044
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002045 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002046
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002047 init_channel_allocations();
2048
2049 return 0;
2050}
2051
2052/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002053 * Shared non-generic implementations
2054 */
2055
2056static int simple_playback_build_pcms(struct hda_codec *codec)
2057{
2058 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002059 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002060 unsigned int chans;
2061 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002062 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002063
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002064 per_cvt = get_cvt(spec, 0);
2065 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002066 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002067
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002068 info = snd_array_new(&spec->pcm_rec);
2069 if (!info)
2070 return -ENOMEM;
2071 info->name = get_pin(spec, 0)->pcm_name;
2072 sprintf(info->name, "HDMI 0");
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002073 info->pcm_type = HDA_PCM_TYPE_HDMI;
2074 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2075 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002076 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002077 if (pstr->channels_max <= 2 && chans && chans <= 16)
2078 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002079
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002080 codec->num_pcms = 1;
2081 codec->pcm_info = info;
2082
Stephen Warren3aaf8982011-06-01 11:14:19 -06002083 return 0;
2084}
2085
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002086/* unsolicited event for jack sensing */
2087static void simple_hdmi_unsol_event(struct hda_codec *codec,
2088 unsigned int res)
2089{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002090 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002091 snd_hda_jack_report_sync(codec);
2092}
2093
2094/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2095 * as long as spec->pins[] is set correctly
2096 */
2097#define simple_hdmi_build_jack generic_hdmi_build_jack
2098
Stephen Warren3aaf8982011-06-01 11:14:19 -06002099static int simple_playback_build_controls(struct hda_codec *codec)
2100{
2101 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002102 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002103 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002104
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002105 per_cvt = get_cvt(spec, 0);
2106 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2107 per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002108 if (err < 0)
2109 return err;
2110 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002111}
2112
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002113static int simple_playback_init(struct hda_codec *codec)
2114{
2115 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002116 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2117 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002118
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002119 snd_hda_codec_write(codec, pin, 0,
2120 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2121 /* some codecs require to unmute the pin */
2122 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2123 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2124 AMP_OUT_UNMUTE);
2125 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002126 return 0;
2127}
2128
Stephen Warren3aaf8982011-06-01 11:14:19 -06002129static void simple_playback_free(struct hda_codec *codec)
2130{
2131 struct hdmi_spec *spec = codec->spec;
2132
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002133 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002134 kfree(spec);
2135}
2136
2137/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002138 * Nvidia specific implementations
2139 */
2140
2141#define Nv_VERB_SET_Channel_Allocation 0xF79
2142#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2143#define Nv_VERB_SET_Audio_Protection_On 0xF98
2144#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2145
2146#define nvhdmi_master_con_nid_7x 0x04
2147#define nvhdmi_master_pin_nid_7x 0x05
2148
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002149static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002150 /*front, rear, clfe, rear_surr */
2151 0x6, 0x8, 0xa, 0xc,
2152};
2153
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002154static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2155 /* set audio protect on */
2156 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2157 /* enable digital output on pin widget */
2158 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2159 {} /* terminator */
2160};
2161
2162static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002163 /* set audio protect on */
2164 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2165 /* enable digital output on pin widget */
2166 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2167 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2168 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2169 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2170 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2171 {} /* terminator */
2172};
2173
2174#ifdef LIMITED_RATE_FMT_SUPPORT
2175/* support only the safe format and rate */
2176#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2177#define SUPPORTED_MAXBPS 16
2178#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2179#else
2180/* support all rates and formats */
2181#define SUPPORTED_RATES \
2182 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2183 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2184 SNDRV_PCM_RATE_192000)
2185#define SUPPORTED_MAXBPS 24
2186#define SUPPORTED_FORMATS \
2187 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2188#endif
2189
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002190static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002191{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002192 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2193 return 0;
2194}
2195
2196static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2197{
2198 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002199 return 0;
2200}
2201
Nitin Daga393004b2011-01-10 21:49:31 +05302202static unsigned int channels_2_6_8[] = {
2203 2, 6, 8
2204};
2205
2206static unsigned int channels_2_8[] = {
2207 2, 8
2208};
2209
2210static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2211 .count = ARRAY_SIZE(channels_2_6_8),
2212 .list = channels_2_6_8,
2213 .mask = 0,
2214};
2215
2216static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2217 .count = ARRAY_SIZE(channels_2_8),
2218 .list = channels_2_8,
2219 .mask = 0,
2220};
2221
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002222static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2223 struct hda_codec *codec,
2224 struct snd_pcm_substream *substream)
2225{
2226 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302227 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2228
2229 switch (codec->preset->id) {
2230 case 0x10de0002:
2231 case 0x10de0003:
2232 case 0x10de0005:
2233 case 0x10de0006:
2234 hw_constraints_channels = &hw_constraints_2_8_channels;
2235 break;
2236 case 0x10de0007:
2237 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2238 break;
2239 default:
2240 break;
2241 }
2242
2243 if (hw_constraints_channels != NULL) {
2244 snd_pcm_hw_constraint_list(substream->runtime, 0,
2245 SNDRV_PCM_HW_PARAM_CHANNELS,
2246 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002247 } else {
2248 snd_pcm_hw_constraint_step(substream->runtime, 0,
2249 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302250 }
2251
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002252 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2253}
2254
2255static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2256 struct hda_codec *codec,
2257 struct snd_pcm_substream *substream)
2258{
2259 struct hdmi_spec *spec = codec->spec;
2260 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2261}
2262
2263static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2264 struct hda_codec *codec,
2265 unsigned int stream_tag,
2266 unsigned int format,
2267 struct snd_pcm_substream *substream)
2268{
2269 struct hdmi_spec *spec = codec->spec;
2270 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2271 stream_tag, format, substream);
2272}
2273
Takashi Iwaid0b12522012-06-15 14:34:42 +02002274static const struct hda_pcm_stream simple_pcm_playback = {
2275 .substreams = 1,
2276 .channels_min = 2,
2277 .channels_max = 2,
2278 .ops = {
2279 .open = simple_playback_pcm_open,
2280 .close = simple_playback_pcm_close,
2281 .prepare = simple_playback_pcm_prepare
2282 },
2283};
2284
2285static const struct hda_codec_ops simple_hdmi_patch_ops = {
2286 .build_controls = simple_playback_build_controls,
2287 .build_pcms = simple_playback_build_pcms,
2288 .init = simple_playback_init,
2289 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002290 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002291};
2292
2293static int patch_simple_hdmi(struct hda_codec *codec,
2294 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2295{
2296 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002297 struct hdmi_spec_per_cvt *per_cvt;
2298 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002299
2300 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2301 if (!spec)
2302 return -ENOMEM;
2303
2304 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002305 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02002306
2307 spec->multiout.num_dacs = 0; /* no analog */
2308 spec->multiout.max_channels = 2;
2309 spec->multiout.dig_out_nid = cvt_nid;
2310 spec->num_cvts = 1;
2311 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002312 per_pin = snd_array_new(&spec->pins);
2313 per_cvt = snd_array_new(&spec->cvts);
2314 if (!per_pin || !per_cvt) {
2315 simple_playback_free(codec);
2316 return -ENOMEM;
2317 }
2318 per_cvt->cvt_nid = cvt_nid;
2319 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002320 spec->pcm_playback = simple_pcm_playback;
2321
2322 codec->patch_ops = simple_hdmi_patch_ops;
2323
2324 return 0;
2325}
2326
Aaron Plattner1f348522011-04-06 17:19:04 -07002327static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2328 int channels)
2329{
2330 unsigned int chanmask;
2331 int chan = channels ? (channels - 1) : 1;
2332
2333 switch (channels) {
2334 default:
2335 case 0:
2336 case 2:
2337 chanmask = 0x00;
2338 break;
2339 case 4:
2340 chanmask = 0x08;
2341 break;
2342 case 6:
2343 chanmask = 0x0b;
2344 break;
2345 case 8:
2346 chanmask = 0x13;
2347 break;
2348 }
2349
2350 /* Set the audio infoframe channel allocation and checksum fields. The
2351 * channel count is computed implicitly by the hardware. */
2352 snd_hda_codec_write(codec, 0x1, 0,
2353 Nv_VERB_SET_Channel_Allocation, chanmask);
2354
2355 snd_hda_codec_write(codec, 0x1, 0,
2356 Nv_VERB_SET_Info_Frame_Checksum,
2357 (0x71 - chan - chanmask));
2358}
2359
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002360static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2361 struct hda_codec *codec,
2362 struct snd_pcm_substream *substream)
2363{
2364 struct hdmi_spec *spec = codec->spec;
2365 int i;
2366
2367 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2368 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2369 for (i = 0; i < 4; i++) {
2370 /* set the stream id */
2371 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2372 AC_VERB_SET_CHANNEL_STREAMID, 0);
2373 /* set the stream format */
2374 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2375 AC_VERB_SET_STREAM_FORMAT, 0);
2376 }
2377
Aaron Plattner1f348522011-04-06 17:19:04 -07002378 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2379 * streams are disabled. */
2380 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2381
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002382 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2383}
2384
2385static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2386 struct hda_codec *codec,
2387 unsigned int stream_tag,
2388 unsigned int format,
2389 struct snd_pcm_substream *substream)
2390{
2391 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002392 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002393 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002394 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002395 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002396 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002397
2398 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002399 per_cvt = get_cvt(spec, 0);
2400 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002401
2402 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002403
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002404 dataDCC2 = 0x2;
2405
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002406 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002407 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002408 snd_hda_codec_write(codec,
2409 nvhdmi_master_con_nid_7x,
2410 0,
2411 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002412 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002413
2414 /* set the stream id */
2415 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2416 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2417
2418 /* set the stream format */
2419 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2420 AC_VERB_SET_STREAM_FORMAT, format);
2421
2422 /* turn on again (if needed) */
2423 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002424 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002425 snd_hda_codec_write(codec,
2426 nvhdmi_master_con_nid_7x,
2427 0,
2428 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002429 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002430 snd_hda_codec_write(codec,
2431 nvhdmi_master_con_nid_7x,
2432 0,
2433 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2434 }
2435
2436 for (i = 0; i < 4; i++) {
2437 if (chs == 2)
2438 channel_id = 0;
2439 else
2440 channel_id = i * 2;
2441
2442 /* turn off SPDIF once;
2443 *otherwise the IEC958 bits won't be updated
2444 */
2445 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002446 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002447 snd_hda_codec_write(codec,
2448 nvhdmi_con_nids_7x[i],
2449 0,
2450 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002451 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002452 /* set the stream id */
2453 snd_hda_codec_write(codec,
2454 nvhdmi_con_nids_7x[i],
2455 0,
2456 AC_VERB_SET_CHANNEL_STREAMID,
2457 (stream_tag << 4) | channel_id);
2458 /* set the stream format */
2459 snd_hda_codec_write(codec,
2460 nvhdmi_con_nids_7x[i],
2461 0,
2462 AC_VERB_SET_STREAM_FORMAT,
2463 format);
2464 /* turn on again (if needed) */
2465 /* enable and set the channel status audio/data flag */
2466 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002467 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002468 snd_hda_codec_write(codec,
2469 nvhdmi_con_nids_7x[i],
2470 0,
2471 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002472 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002473 snd_hda_codec_write(codec,
2474 nvhdmi_con_nids_7x[i],
2475 0,
2476 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2477 }
2478 }
2479
Aaron Plattner1f348522011-04-06 17:19:04 -07002480 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002481
2482 mutex_unlock(&codec->spdif_mutex);
2483 return 0;
2484}
2485
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002486static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002487 .substreams = 1,
2488 .channels_min = 2,
2489 .channels_max = 8,
2490 .nid = nvhdmi_master_con_nid_7x,
2491 .rates = SUPPORTED_RATES,
2492 .maxbps = SUPPORTED_MAXBPS,
2493 .formats = SUPPORTED_FORMATS,
2494 .ops = {
2495 .open = simple_playback_pcm_open,
2496 .close = nvhdmi_8ch_7x_pcm_close,
2497 .prepare = nvhdmi_8ch_7x_pcm_prepare
2498 },
2499};
2500
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002501static int patch_nvhdmi_2ch(struct hda_codec *codec)
2502{
2503 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002504 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2505 nvhdmi_master_pin_nid_7x);
2506 if (err < 0)
2507 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002508
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002509 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002510 /* override the PCM rates, etc, as the codec doesn't give full list */
2511 spec = codec->spec;
2512 spec->pcm_playback.rates = SUPPORTED_RATES;
2513 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2514 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002515 return 0;
2516}
2517
Takashi Iwai53775b02012-08-01 12:17:41 +02002518static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2519{
2520 struct hdmi_spec *spec = codec->spec;
2521 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002522 if (!err) {
2523 struct hda_pcm *info = get_pcm_rec(spec, 0);
2524 info->own_chmap = true;
2525 }
Takashi Iwai53775b02012-08-01 12:17:41 +02002526 return err;
2527}
2528
2529static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2530{
2531 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002532 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02002533 struct snd_pcm_chmap *chmap;
2534 int err;
2535
2536 err = simple_playback_build_controls(codec);
2537 if (err < 0)
2538 return err;
2539
2540 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002541 info = get_pcm_rec(spec, 0);
2542 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02002543 SNDRV_PCM_STREAM_PLAYBACK,
2544 snd_pcm_alt_chmaps, 8, 0, &chmap);
2545 if (err < 0)
2546 return err;
2547 switch (codec->preset->id) {
2548 case 0x10de0002:
2549 case 0x10de0003:
2550 case 0x10de0005:
2551 case 0x10de0006:
2552 chmap->channel_mask = (1U << 2) | (1U << 8);
2553 break;
2554 case 0x10de0007:
2555 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2556 }
2557 return 0;
2558}
2559
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002560static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2561{
2562 struct hdmi_spec *spec;
2563 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002564 if (err < 0)
2565 return err;
2566 spec = codec->spec;
2567 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002568 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002569 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002570 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2571 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002572
2573 /* Initialize the audio infoframe channel mask and checksum to something
2574 * valid */
2575 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2576
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002577 return 0;
2578}
2579
2580/*
2581 * ATI-specific implementations
2582 *
2583 * FIXME: we may omit the whole this and use the generic code once after
2584 * it's confirmed to work.
2585 */
2586
2587#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2588#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2589
2590static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2591 struct hda_codec *codec,
2592 unsigned int stream_tag,
2593 unsigned int format,
2594 struct snd_pcm_substream *substream)
2595{
2596 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002597 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002598 int chans = substream->runtime->channels;
2599 int i, err;
2600
2601 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2602 substream);
2603 if (err < 0)
2604 return err;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002605 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06002606 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002607 /* FIXME: XXX */
2608 for (i = 0; i < chans; i++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002609 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002610 AC_VERB_SET_HDMI_CHAN_SLOT,
2611 (i << 4) | i);
2612 }
2613 return 0;
2614}
2615
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002616static int patch_atihdmi(struct hda_codec *codec)
2617{
2618 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002619 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2620 if (err < 0)
2621 return err;
2622 spec = codec->spec;
2623 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002624 return 0;
2625}
2626
Annie Liu3de5ff82012-06-08 19:18:42 +08002627/* VIA HDMI Implementation */
2628#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2629#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2630
Annie Liu3de5ff82012-06-08 19:18:42 +08002631static int patch_via_hdmi(struct hda_codec *codec)
2632{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002633 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002634}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002635
2636/*
2637 * patch entries
2638 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002639static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002640{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2641{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2642{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002643{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002644{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2645{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2646{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2647{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2648{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2649{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2650{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2651{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002652{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2653{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2654{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2655{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2656{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2657{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2658{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2659{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2660{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2661{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2662{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002663/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002664{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2665{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2666{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2667{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2668{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2669{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2670{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2671{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2672{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2673{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002674{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002675{ .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002676{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2677{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002678{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2679{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2680{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2681{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002682{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2683{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2684{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2685{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2686{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2687{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002688{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002689{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002690{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002691{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2692{} /* terminator */
2693};
2694
2695MODULE_ALIAS("snd-hda-codec-id:1002793c");
2696MODULE_ALIAS("snd-hda-codec-id:10027919");
2697MODULE_ALIAS("snd-hda-codec-id:1002791a");
2698MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2699MODULE_ALIAS("snd-hda-codec-id:10951390");
2700MODULE_ALIAS("snd-hda-codec-id:10951392");
2701MODULE_ALIAS("snd-hda-codec-id:10de0002");
2702MODULE_ALIAS("snd-hda-codec-id:10de0003");
2703MODULE_ALIAS("snd-hda-codec-id:10de0005");
2704MODULE_ALIAS("snd-hda-codec-id:10de0006");
2705MODULE_ALIAS("snd-hda-codec-id:10de0007");
2706MODULE_ALIAS("snd-hda-codec-id:10de000a");
2707MODULE_ALIAS("snd-hda-codec-id:10de000b");
2708MODULE_ALIAS("snd-hda-codec-id:10de000c");
2709MODULE_ALIAS("snd-hda-codec-id:10de000d");
2710MODULE_ALIAS("snd-hda-codec-id:10de0010");
2711MODULE_ALIAS("snd-hda-codec-id:10de0011");
2712MODULE_ALIAS("snd-hda-codec-id:10de0012");
2713MODULE_ALIAS("snd-hda-codec-id:10de0013");
2714MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002715MODULE_ALIAS("snd-hda-codec-id:10de0015");
2716MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002717MODULE_ALIAS("snd-hda-codec-id:10de0018");
2718MODULE_ALIAS("snd-hda-codec-id:10de0019");
2719MODULE_ALIAS("snd-hda-codec-id:10de001a");
2720MODULE_ALIAS("snd-hda-codec-id:10de001b");
2721MODULE_ALIAS("snd-hda-codec-id:10de001c");
2722MODULE_ALIAS("snd-hda-codec-id:10de0040");
2723MODULE_ALIAS("snd-hda-codec-id:10de0041");
2724MODULE_ALIAS("snd-hda-codec-id:10de0042");
2725MODULE_ALIAS("snd-hda-codec-id:10de0043");
2726MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002727MODULE_ALIAS("snd-hda-codec-id:10de0051");
Aaron Plattnerd52392b2013-07-12 11:01:37 -07002728MODULE_ALIAS("snd-hda-codec-id:10de0060");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002729MODULE_ALIAS("snd-hda-codec-id:10de0067");
2730MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002731MODULE_ALIAS("snd-hda-codec-id:11069f80");
2732MODULE_ALIAS("snd-hda-codec-id:11069f81");
2733MODULE_ALIAS("snd-hda-codec-id:11069f84");
2734MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002735MODULE_ALIAS("snd-hda-codec-id:17e80047");
2736MODULE_ALIAS("snd-hda-codec-id:80860054");
2737MODULE_ALIAS("snd-hda-codec-id:80862801");
2738MODULE_ALIAS("snd-hda-codec-id:80862802");
2739MODULE_ALIAS("snd-hda-codec-id:80862803");
2740MODULE_ALIAS("snd-hda-codec-id:80862804");
2741MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002742MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002743MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002744MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002745MODULE_ALIAS("snd-hda-codec-id:808629fb");
2746
2747MODULE_LICENSE("GPL");
2748MODULE_DESCRIPTION("HDMI HD-audio codec");
2749MODULE_ALIAS("snd-hda-codec-intelhdmi");
2750MODULE_ALIAS("snd-hda-codec-nvhdmi");
2751MODULE_ALIAS("snd-hda-codec-atihdmi");
2752
2753static struct hda_codec_preset_list intel_list = {
2754 .preset = snd_hda_preset_hdmi,
2755 .owner = THIS_MODULE,
2756};
2757
2758static int __init patch_hdmi_init(void)
2759{
2760 return snd_hda_add_codec_preset(&intel_list);
2761}
2762
2763static void __exit patch_hdmi_exit(void)
2764{
2765 snd_hda_delete_codec_preset(&intel_list);
2766}
2767
2768module_init(patch_hdmi_init)
2769module_exit(patch_hdmi_exit)