blob: 32adaa6c5627d397c5c77b3d9bd60afc0ef108f6 [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
Takashi Iwai84eb01b2010-09-07 12:27:25 +020047/*
48 * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
Stephen Warren384a48d2011-06-01 11:14:21 -060049 * could support N independent pipes, each of them can be connected to one or
Takashi Iwai84eb01b2010-09-07 12:27:25 +020050 * more ports (DVI, HDMI or DisplayPort).
51 *
52 * The HDA correspondence of pipes/ports are converter/pin nodes.
53 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +010054#define MAX_HDMI_CVTS 8
55#define MAX_HDMI_PINS 8
Wu Fengguang079d88c2010-03-08 10:44:23 +080056
Stephen Warren384a48d2011-06-01 11:14:21 -060057struct hdmi_spec_per_cvt {
58 hda_nid_t cvt_nid;
59 int assigned;
60 unsigned int channels_min;
61 unsigned int channels_max;
62 u32 rates;
63 u64 formats;
64 unsigned int maxbps;
65};
66
Takashi Iwai4eea3092013-02-07 18:18:19 +010067/* max. connections to a widget */
68#define HDA_MAX_CONNECTIONS 32
69
Stephen Warren384a48d2011-06-01 11:14:21 -060070struct hdmi_spec_per_pin {
71 hda_nid_t pin_nid;
72 int num_mux_nids;
73 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080074
75 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060076 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080077 struct delayed_work work;
Wu Fengguangc6e84532011-11-18 16:59:32 -060078 int repoll_count;
Takashi Iwai1a6003b2012-09-06 17:42:08 +020079 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020080 bool chmap_set; /* channel-map override by ALSA API? */
81 unsigned char chmap[8]; /* ALSA API channel-map */
Stephen Warren384a48d2011-06-01 11:14:21 -060082};
83
Wu Fengguang079d88c2010-03-08 10:44:23 +080084struct hdmi_spec {
85 int num_cvts;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
Takashi Iwaic88d4e82013-02-08 17:10:04 -050087 hda_nid_t cvt_nids[MAX_HDMI_CVTS];
Stephen Warren384a48d2011-06-01 11:14:21 -060088
Wu Fengguang079d88c2010-03-08 10:44:23 +080089 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060090 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
91 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Takashi Iwaid45e6882012-07-31 11:36:00 +020092 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080093
94 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060095 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080096 */
97 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020098 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080099};
100
101
102struct hdmi_audio_infoframe {
103 u8 type; /* 0x84 */
104 u8 ver; /* 0x01 */
105 u8 len; /* 0x0a */
106
Wu Fengguang53d7d692010-09-21 14:25:49 +0800107 u8 checksum;
108
Wu Fengguang079d88c2010-03-08 10:44:23 +0800109 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
110 u8 SS01_SF24;
111 u8 CXT04;
112 u8 CA;
113 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800114};
115
116struct dp_audio_infoframe {
117 u8 type; /* 0x84 */
118 u8 len; /* 0x1b */
119 u8 ver; /* 0x11 << 2 */
120
121 u8 CC02_CT47; /* match with HDMI infoframe from this on */
122 u8 SS01_SF24;
123 u8 CXT04;
124 u8 CA;
125 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800126};
127
Takashi Iwai2b203db2011-02-11 12:17:30 +0100128union audio_infoframe {
129 struct hdmi_audio_infoframe hdmi;
130 struct dp_audio_infoframe dp;
131 u8 bytes[0];
132};
133
Wu Fengguang079d88c2010-03-08 10:44:23 +0800134/*
135 * CEA speaker placement:
136 *
137 * FLH FCH FRH
138 * FLW FL FLC FC FRC FR FRW
139 *
140 * LFE
141 * TC
142 *
143 * RL RLC RC RRC RR
144 *
145 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
146 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
147 */
148enum cea_speaker_placement {
149 FL = (1 << 0), /* Front Left */
150 FC = (1 << 1), /* Front Center */
151 FR = (1 << 2), /* Front Right */
152 FLC = (1 << 3), /* Front Left Center */
153 FRC = (1 << 4), /* Front Right Center */
154 RL = (1 << 5), /* Rear Left */
155 RC = (1 << 6), /* Rear Center */
156 RR = (1 << 7), /* Rear Right */
157 RLC = (1 << 8), /* Rear Left Center */
158 RRC = (1 << 9), /* Rear Right Center */
159 LFE = (1 << 10), /* Low Frequency Effect */
160 FLW = (1 << 11), /* Front Left Wide */
161 FRW = (1 << 12), /* Front Right Wide */
162 FLH = (1 << 13), /* Front Left High */
163 FCH = (1 << 14), /* Front Center High */
164 FRH = (1 << 15), /* Front Right High */
165 TC = (1 << 16), /* Top Center */
166};
167
168/*
169 * ELD SA bits in the CEA Speaker Allocation data block
170 */
171static int eld_speaker_allocation_bits[] = {
172 [0] = FL | FR,
173 [1] = LFE,
174 [2] = FC,
175 [3] = RL | RR,
176 [4] = RC,
177 [5] = FLC | FRC,
178 [6] = RLC | RRC,
179 /* the following are not defined in ELD yet */
180 [7] = FLW | FRW,
181 [8] = FLH | FRH,
182 [9] = TC,
183 [10] = FCH,
184};
185
186struct cea_channel_speaker_allocation {
187 int ca_index;
188 int speakers[8];
189
190 /* derived values, just for convenience */
191 int channels;
192 int spk_mask;
193};
194
195/*
196 * ALSA sequence is:
197 *
198 * surround40 surround41 surround50 surround51 surround71
199 * ch0 front left = = = =
200 * ch1 front right = = = =
201 * ch2 rear left = = = =
202 * ch3 rear right = = = =
203 * ch4 LFE center center center
204 * ch5 LFE LFE
205 * ch6 side left
206 * ch7 side right
207 *
208 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
209 */
210static int hdmi_channel_mapping[0x32][8] = {
211 /* stereo */
212 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* 2.1 */
214 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
215 /* Dolby Surround */
216 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
217 /* surround40 */
218 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
219 /* 4ch */
220 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
221 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800222 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800223 /* surround50 */
224 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
225 /* surround51 */
226 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
227 /* 7.1 */
228 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
229};
230
231/*
232 * This is an ordered list!
233 *
234 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800235 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800236 */
237static struct cea_channel_speaker_allocation channel_allocations[] = {
238/* channel: 7 6 5 4 3 2 1 0 */
239{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
240 /* 2.1 */
241{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
242 /* Dolby Surround */
243{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
244 /* surround40 */
245{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
246 /* surround41 */
247{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
248 /* surround50 */
249{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
250 /* surround51 */
251{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
252 /* 6.1 */
253{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
254 /* surround71 */
255{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
256
257{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
258{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
259{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
260{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
261{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
262{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
263{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
264{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
265{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
266{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
267{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
268{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
269{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
270{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
271{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
272{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
273{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
274{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
275{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
276{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
277{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
278{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
283{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
284{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
285{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
286{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
287{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
288{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
294{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
295{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
296{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
297{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
298};
299
300
301/*
302 * HDMI routines
303 */
304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800306{
Stephen Warren384a48d2011-06-01 11:14:21 -0600307 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
310 if (spec->pins[pin_idx].pin_nid == pin_nid)
311 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800312
Stephen Warren384a48d2011-06-01 11:14:21 -0600313 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
314 return -EINVAL;
315}
316
317static int hinfo_to_pin_index(struct hdmi_spec *spec,
318 struct hda_pcm_stream *hinfo)
319{
320 int pin_idx;
321
322 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
323 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
324 return pin_idx;
325
326 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
327 return -EINVAL;
328}
329
330static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
331{
332 int cvt_idx;
333
334 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
335 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
336 return cvt_idx;
337
338 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800339 return -EINVAL;
340}
341
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500342static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_info *uinfo)
344{
345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
346 struct hdmi_spec *spec;
347 int pin_idx;
348
349 spec = codec->spec;
350 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
351
352 pin_idx = kcontrol->private_value;
353 uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
354
355 return 0;
356}
357
358static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
359 struct snd_ctl_elem_value *ucontrol)
360{
361 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
362 struct hdmi_spec *spec;
363 int pin_idx;
364
365 spec = codec->spec;
366 pin_idx = kcontrol->private_value;
367
368 memcpy(ucontrol->value.bytes.data,
369 spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
370
371 return 0;
372}
373
374static struct snd_kcontrol_new eld_bytes_ctl = {
375 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
376 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
377 .name = "ELD",
378 .info = hdmi_eld_ctl_info,
379 .get = hdmi_eld_ctl_get,
380};
381
382static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
383 int device)
384{
385 struct snd_kcontrol *kctl;
386 struct hdmi_spec *spec = codec->spec;
387 int err;
388
389 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
390 if (!kctl)
391 return -ENOMEM;
392 kctl->private_value = pin_idx;
393 kctl->id.device = device;
394
395 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
396 if (err < 0)
397 return err;
398
399 return 0;
400}
401
Wu Fengguang079d88c2010-03-08 10:44:23 +0800402#ifdef BE_PARANOID
403static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
404 int *packet_index, int *byte_index)
405{
406 int val;
407
408 val = snd_hda_codec_read(codec, pin_nid, 0,
409 AC_VERB_GET_HDMI_DIP_INDEX, 0);
410
411 *packet_index = val >> 5;
412 *byte_index = val & 0x1f;
413}
414#endif
415
416static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
417 int packet_index, int byte_index)
418{
419 int val;
420
421 val = (packet_index << 5) | (byte_index & 0x1f);
422
423 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
424}
425
426static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
427 unsigned char val)
428{
429 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
430}
431
Stephen Warren384a48d2011-06-01 11:14:21 -0600432static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800433{
434 /* Unmute */
435 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
436 snd_hda_codec_write(codec, pin_nid, 0,
437 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100438 /* Enable pin out: some machines with GM965 gets broken output when
439 * the pin is disabled or changed while using with HDMI
440 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800441 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100442 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800443}
444
Stephen Warren384a48d2011-06-01 11:14:21 -0600445static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800446{
Stephen Warren384a48d2011-06-01 11:14:21 -0600447 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800448 AC_VERB_GET_CVT_CHAN_COUNT, 0);
449}
450
451static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600452 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800453{
Stephen Warren384a48d2011-06-01 11:14:21 -0600454 if (chs != hdmi_get_channel_count(codec, cvt_nid))
455 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800456 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
457}
458
459
460/*
461 * Channel mapping routines
462 */
463
464/*
465 * Compute derived values in channel_allocations[].
466 */
467static void init_channel_allocations(void)
468{
469 int i, j;
470 struct cea_channel_speaker_allocation *p;
471
472 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
473 p = channel_allocations + i;
474 p->channels = 0;
475 p->spk_mask = 0;
476 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
477 if (p->speakers[j]) {
478 p->channels++;
479 p->spk_mask |= p->speakers[j];
480 }
481 }
482}
483
Wang Xingchao72357c72012-09-06 10:02:36 +0800484static int get_channel_allocation_order(int ca)
485{
486 int i;
487
488 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
489 if (channel_allocations[i].ca_index == ca)
490 break;
491 }
492 return i;
493}
494
Wu Fengguang079d88c2010-03-08 10:44:23 +0800495/*
496 * The transformation takes two steps:
497 *
498 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
499 * spk_mask => (channel_allocations[]) => ai->CA
500 *
501 * TODO: it could select the wrong CA from multiple candidates.
502*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600503static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800504{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800505 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800506 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800507 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800508 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
509
510 /*
511 * CA defaults to 0 for basic stereo audio
512 */
513 if (channels <= 2)
514 return 0;
515
Wu Fengguang079d88c2010-03-08 10:44:23 +0800516 /*
517 * expand ELD's speaker allocation mask
518 *
519 * ELD tells the speaker mask in a compact(paired) form,
520 * expand ELD's notions to match the ones used by Audio InfoFrame.
521 */
522 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
523 if (eld->spk_alloc & (1 << i))
524 spk_mask |= eld_speaker_allocation_bits[i];
525 }
526
527 /* search for the first working match in the CA table */
528 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
529 if (channels == channel_allocations[i].channels &&
530 (spk_mask & channel_allocations[i].spk_mask) ==
531 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800532 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800533 break;
534 }
535 }
536
537 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800538 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800539 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800540
Wu Fengguang53d7d692010-09-21 14:25:49 +0800541 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800542}
543
544static void hdmi_debug_channel_mapping(struct hda_codec *codec,
545 hda_nid_t pin_nid)
546{
547#ifdef CONFIG_SND_DEBUG_VERBOSE
548 int i;
549 int slot;
550
551 for (i = 0; i < 8; i++) {
552 slot = snd_hda_codec_read(codec, pin_nid, 0,
553 AC_VERB_GET_HDMI_CHAN_SLOT, i);
554 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
555 slot >> 4, slot & 0xf);
556 }
557#endif
558}
559
560
Takashi Iwaid45e6882012-07-31 11:36:00 +0200561static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800562 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800563 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800564 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800565{
566 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800567 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800568 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800569 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570
Wang Xingchao72357c72012-09-06 10:02:36 +0800571 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800572
Wu Fengguang079d88c2010-03-08 10:44:23 +0800573 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800574 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575 hdmi_channel_mapping[ca][i] = i | (i << 4);
576 for (; i < 8; i++)
577 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
578 }
579
Wang Xingchao433968d2012-09-06 10:02:37 +0800580 if (non_pcm) {
581 for (i = 0; i < channel_allocations[order].channels; i++)
582 non_pcm_mapping[i] = i | (i << 4);
583 for (; i < 8; i++)
584 non_pcm_mapping[i] = 0xf | (i << 4);
585 }
586
Wu Fengguang079d88c2010-03-08 10:44:23 +0800587 for (i = 0; i < 8; i++) {
588 err = snd_hda_codec_write(codec, pin_nid, 0,
589 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800590 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800591 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800592 snd_printdd(KERN_NOTICE
593 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800594 break;
595 }
596 }
597
598 hdmi_debug_channel_mapping(codec, pin_nid);
599}
600
Takashi Iwaid45e6882012-07-31 11:36:00 +0200601struct channel_map_table {
602 unsigned char map; /* ALSA API channel map position */
603 unsigned char cea_slot; /* CEA slot value */
604 int spk_mask; /* speaker position bit mask */
605};
606
607static struct channel_map_table map_tables[] = {
608 { SNDRV_CHMAP_FL, 0x00, FL },
609 { SNDRV_CHMAP_FR, 0x01, FR },
610 { SNDRV_CHMAP_RL, 0x04, RL },
611 { SNDRV_CHMAP_RR, 0x05, RR },
612 { SNDRV_CHMAP_LFE, 0x02, LFE },
613 { SNDRV_CHMAP_FC, 0x03, FC },
614 { SNDRV_CHMAP_RLC, 0x06, RLC },
615 { SNDRV_CHMAP_RRC, 0x07, RRC },
616 {} /* terminator */
617};
618
619/* from ALSA API channel position to speaker bit mask */
620static int to_spk_mask(unsigned char c)
621{
622 struct channel_map_table *t = map_tables;
623 for (; t->map; t++) {
624 if (t->map == c)
625 return t->spk_mask;
626 }
627 return 0;
628}
629
630/* from ALSA API channel position to CEA slot */
631static int to_cea_slot(unsigned char c)
632{
633 struct channel_map_table *t = map_tables;
634 for (; t->map; t++) {
635 if (t->map == c)
636 return t->cea_slot;
637 }
638 return 0x0f;
639}
640
641/* from CEA slot to ALSA API channel position */
642static int from_cea_slot(unsigned char c)
643{
644 struct channel_map_table *t = map_tables;
645 for (; t->map; t++) {
646 if (t->cea_slot == c)
647 return t->map;
648 }
649 return 0;
650}
651
652/* from speaker bit mask to ALSA API channel position */
653static int spk_to_chmap(int spk)
654{
655 struct channel_map_table *t = map_tables;
656 for (; t->map; t++) {
657 if (t->spk_mask == spk)
658 return t->map;
659 }
660 return 0;
661}
662
663/* get the CA index corresponding to the given ALSA API channel map */
664static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
665{
666 int i, spks = 0, spk_mask = 0;
667
668 for (i = 0; i < chs; i++) {
669 int mask = to_spk_mask(map[i]);
670 if (mask) {
671 spk_mask |= mask;
672 spks++;
673 }
674 }
675
676 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
677 if ((chs == channel_allocations[i].channels ||
678 spks == channel_allocations[i].channels) &&
679 (spk_mask & channel_allocations[i].spk_mask) ==
680 channel_allocations[i].spk_mask)
681 return channel_allocations[i].ca_index;
682 }
683 return -1;
684}
685
686/* set up the channel slots for the given ALSA API channel map */
687static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
688 hda_nid_t pin_nid,
689 int chs, unsigned char *map)
690{
691 int i;
692 for (i = 0; i < 8; i++) {
693 int val, err;
694 if (i < chs)
695 val = to_cea_slot(map[i]);
696 else
697 val = 0xf;
698 val |= (i << 4);
699 err = snd_hda_codec_write(codec, pin_nid, 0,
700 AC_VERB_SET_HDMI_CHAN_SLOT, val);
701 if (err)
702 return -EINVAL;
703 }
704 return 0;
705}
706
707/* store ALSA API channel map from the current default map */
708static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
709{
710 int i;
711 for (i = 0; i < 8; i++) {
712 if (i < channel_allocations[ca].channels)
713 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
714 else
715 map[i] = 0;
716 }
717}
718
719static void hdmi_setup_channel_mapping(struct hda_codec *codec,
720 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200721 int channels, unsigned char *map,
722 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200723{
Anssi Hannula20608732013-02-03 17:55:45 +0200724 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200725 hdmi_manual_setup_channel_mapping(codec, pin_nid,
726 channels, map);
727 } else {
728 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
729 hdmi_setup_fake_chmap(map, ca);
730 }
731}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800732
733/*
734 * Audio InfoFrame routines
735 */
736
737/*
738 * Enable Audio InfoFrame Transmission
739 */
740static void hdmi_start_infoframe_trans(struct hda_codec *codec,
741 hda_nid_t pin_nid)
742{
743 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
744 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
745 AC_DIPXMIT_BEST);
746}
747
748/*
749 * Disable Audio InfoFrame Transmission
750 */
751static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
752 hda_nid_t pin_nid)
753{
754 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
755 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
756 AC_DIPXMIT_DISABLE);
757}
758
759static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
760{
761#ifdef CONFIG_SND_DEBUG_VERBOSE
762 int i;
763 int size;
764
765 size = snd_hdmi_get_eld_size(codec, pin_nid);
766 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
767
768 for (i = 0; i < 8; i++) {
769 size = snd_hda_codec_read(codec, pin_nid, 0,
770 AC_VERB_GET_HDMI_DIP_SIZE, i);
771 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
772 }
773#endif
774}
775
776static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
777{
778#ifdef BE_PARANOID
779 int i, j;
780 int size;
781 int pi, bi;
782 for (i = 0; i < 8; i++) {
783 size = snd_hda_codec_read(codec, pin_nid, 0,
784 AC_VERB_GET_HDMI_DIP_SIZE, i);
785 if (size == 0)
786 continue;
787
788 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
789 for (j = 1; j < 1000; j++) {
790 hdmi_write_dip_byte(codec, pin_nid, 0x0);
791 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
792 if (pi != i)
793 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
794 bi, pi, i);
795 if (bi == 0) /* byte index wrapped around */
796 break;
797 }
798 snd_printd(KERN_INFO
799 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
800 i, size, j);
801 }
802#endif
803}
804
Wu Fengguang53d7d692010-09-21 14:25:49 +0800805static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800806{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800807 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800808 u8 sum = 0;
809 int i;
810
Wu Fengguang53d7d692010-09-21 14:25:49 +0800811 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800812
Wu Fengguang53d7d692010-09-21 14:25:49 +0800813 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800814 sum += bytes[i];
815
Wu Fengguang53d7d692010-09-21 14:25:49 +0800816 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800817}
818
819static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
820 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800821 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800822{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800823 int i;
824
825 hdmi_debug_dip_size(codec, pin_nid);
826 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
827
Wu Fengguang079d88c2010-03-08 10:44:23 +0800828 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800829 for (i = 0; i < size; i++)
830 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800831}
832
833static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800834 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800835{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800836 u8 val;
837 int i;
838
839 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
840 != AC_DIPXMIT_BEST)
841 return false;
842
843 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800844 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800845 val = snd_hda_codec_read(codec, pin_nid, 0,
846 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800847 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800848 return false;
849 }
850
851 return true;
852}
853
Stephen Warren384a48d2011-06-01 11:14:21 -0600854static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200855 bool non_pcm,
856 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800857{
858 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600859 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
860 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800861 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600862 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800863 int ca;
Takashi Iwai2b203db2011-02-11 12:17:30 +0100864 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800865
Stephen Warren384a48d2011-06-01 11:14:21 -0600866 eld = &spec->pins[pin_idx].sink_eld;
867 if (!eld->monitor_present)
868 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800869
Takashi Iwaid45e6882012-07-31 11:36:00 +0200870 if (!non_pcm && per_pin->chmap_set)
871 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
872 else
873 ca = hdmi_channel_allocation(eld, channels);
874 if (ca < 0)
875 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876
Stephen Warren384a48d2011-06-01 11:14:21 -0600877 memset(&ai, 0, sizeof(ai));
878 if (eld->conn_type == 0) { /* HDMI */
879 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800880
Stephen Warren384a48d2011-06-01 11:14:21 -0600881 hdmi_ai->type = 0x84;
882 hdmi_ai->ver = 0x01;
883 hdmi_ai->len = 0x0a;
884 hdmi_ai->CC02_CT47 = channels - 1;
885 hdmi_ai->CA = ca;
886 hdmi_checksum_audio_infoframe(hdmi_ai);
887 } else if (eld->conn_type == 1) { /* DisplayPort */
888 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800889
Stephen Warren384a48d2011-06-01 11:14:21 -0600890 dp_ai->type = 0x84;
891 dp_ai->len = 0x1b;
892 dp_ai->ver = 0x11 << 2;
893 dp_ai->CC02_CT47 = channels - 1;
894 dp_ai->CA = ca;
895 } else {
896 snd_printd("HDMI: unknown connection type at pin %d\n",
897 pin_nid);
898 return;
899 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800900
Stephen Warren384a48d2011-06-01 11:14:21 -0600901 /*
902 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
903 * sizeof(*dp_ai) to avoid partial match/update problems when
904 * the user switches between HDMI/DP monitors.
905 */
906 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
907 sizeof(ai))) {
908 snd_printdd("hdmi_setup_audio_infoframe: "
909 "pin=%d channels=%d\n",
910 pin_nid,
911 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200912 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200913 channels, per_pin->chmap,
914 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600915 hdmi_stop_infoframe_trans(codec, pin_nid);
916 hdmi_fill_audio_infoframe(codec, pin_nid,
917 ai.bytes, sizeof(ai));
918 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800919 } else {
920 /* For non-pcm audio switch, setup new channel mapping
921 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200922 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200923 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200924 channels, per_pin->chmap,
925 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800926 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800927
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200928 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800929}
930
931
932/*
933 * Unsolicited events
934 */
935
Wu Fengguangc6e84532011-11-18 16:59:32 -0600936static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200937
Wu Fengguang079d88c2010-03-08 10:44:23 +0800938static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
939{
940 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200941 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
942 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600943 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200944 struct hda_jack_tbl *jack;
945
946 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
947 if (!jack)
948 return;
949 pin_nid = jack->nid;
950 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800951
Fengguang Wufae3d882012-04-10 17:00:35 +0800952 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600953 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800954 codec->addr, pin_nid,
955 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800956
Stephen Warren384a48d2011-06-01 11:14:21 -0600957 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
958 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800959 return;
960
Wu Fengguangc6e84532011-11-18 16:59:32 -0600961 hdmi_present_sense(&spec->pins[pin_idx], 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200962 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800963}
964
965static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
966{
967 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
968 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
969 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
970 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
971
972 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200973 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600974 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800975 tag,
976 subtag,
977 cp_state,
978 cp_ready);
979
980 /* TODO */
981 if (cp_state)
982 ;
983 if (cp_ready)
984 ;
985}
986
987
988static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
989{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800990 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
991 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
992
Takashi Iwai3a938972011-10-28 01:16:55 +0200993 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800994 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
995 return;
996 }
997
998 if (subtag == 0)
999 hdmi_intrinsic_event(codec, res);
1000 else
1001 hdmi_non_intrinsic_event(codec, res);
1002}
1003
1004/*
1005 * Callbacks
1006 */
1007
Takashi Iwai92f10b32010-08-03 14:21:00 +02001008/* HBR should be Non-PCM, 8 channels */
1009#define is_hbr_format(format) \
1010 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1011
Stephen Warren384a48d2011-06-01 11:14:21 -06001012static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1013 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001014{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001015 int pinctl;
1016 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001017
Stephen Warren384a48d2011-06-01 11:14:21 -06001018 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1019 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001020 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1021
1022 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001023 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001024 new_pinctl |= AC_PINCTL_EPT_HBR;
1025 else
1026 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1027
1028 snd_printdd("hdmi_setup_stream: "
1029 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001030 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001031 pinctl == new_pinctl ? "" : "new-",
1032 new_pinctl);
1033
1034 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001035 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001036 AC_VERB_SET_PIN_WIDGET_CONTROL,
1037 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001038
Stephen Warren384a48d2011-06-01 11:14:21 -06001039 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001040 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001041 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1042 return -EINVAL;
1043 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001044
Stephen Warren384a48d2011-06-01 11:14:21 -06001045 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001046 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001047}
1048
1049/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001050 * HDA PCM callbacks
1051 */
1052static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1053 struct hda_codec *codec,
1054 struct snd_pcm_substream *substream)
1055{
1056 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001057 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001058 int pin_idx, cvt_idx, mux_idx = 0;
1059 struct hdmi_spec_per_pin *per_pin;
1060 struct hdmi_eld *eld;
1061 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001062
Stephen Warren384a48d2011-06-01 11:14:21 -06001063 /* Validate hinfo */
1064 pin_idx = hinfo_to_pin_index(spec, hinfo);
1065 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001066 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -06001067 per_pin = &spec->pins[pin_idx];
1068 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001069
Stephen Warren384a48d2011-06-01 11:14:21 -06001070 /* Dynamically assign converter to stream */
1071 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1072 per_cvt = &spec->cvts[cvt_idx];
1073
1074 /* Must not already be assigned */
1075 if (per_cvt->assigned)
1076 continue;
1077 /* Must be in pin's mux's list of converters */
1078 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1079 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1080 break;
1081 /* Not in mux list */
1082 if (mux_idx == per_pin->num_mux_nids)
1083 continue;
1084 break;
1085 }
1086 /* No free converters */
1087 if (cvt_idx == spec->num_cvts)
1088 return -ENODEV;
1089
1090 /* Claim converter */
1091 per_cvt->assigned = 1;
1092 hinfo->nid = per_cvt->cvt_nid;
1093
1094 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1095 AC_VERB_SET_CONNECT_SEL,
1096 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001097 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001098
Stephen Warren2def8172011-06-01 11:14:20 -06001099 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001100 hinfo->channels_min = per_cvt->channels_min;
1101 hinfo->channels_max = per_cvt->channels_max;
1102 hinfo->rates = per_cvt->rates;
1103 hinfo->formats = per_cvt->formats;
1104 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001105
Stephen Warren384a48d2011-06-01 11:14:21 -06001106 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001107 if (!static_hdmi_pcm && eld->eld_valid) {
Stephen Warren2def8172011-06-01 11:14:20 -06001108 snd_hdmi_eld_update_pcm_info(eld, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001109 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001110 !hinfo->rates || !hinfo->formats) {
1111 per_cvt->assigned = 0;
1112 hinfo->nid = 0;
1113 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001114 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001115 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001116 }
Stephen Warren2def8172011-06-01 11:14:20 -06001117
1118 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001119 runtime->hw.channels_min = hinfo->channels_min;
1120 runtime->hw.channels_max = hinfo->channels_max;
1121 runtime->hw.formats = hinfo->formats;
1122 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001123
1124 snd_pcm_hw_constraint_step(substream->runtime, 0,
1125 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001126 return 0;
1127}
1128
1129/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001130 * HDA/HDMI auto parsing
1131 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001132static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001133{
1134 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001135 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1136 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001137
1138 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1139 snd_printk(KERN_WARNING
1140 "HDMI: pin %d wcaps %#x "
1141 "does not support connection list\n",
1142 pin_nid, get_wcaps(codec, pin_nid));
1143 return -EINVAL;
1144 }
1145
Stephen Warren384a48d2011-06-01 11:14:21 -06001146 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1147 per_pin->mux_nids,
1148 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001149
1150 return 0;
1151}
1152
Wu Fengguangc6e84532011-11-18 16:59:32 -06001153static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001154{
Wu Fengguang744626d2011-11-16 16:29:47 +08001155 struct hda_codec *codec = per_pin->codec;
1156 struct hdmi_eld *eld = &per_pin->sink_eld;
1157 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001158 /*
1159 * Always execute a GetPinSense verb here, even when called from
1160 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1161 * response's PD bit is not the real PD value, but indicates that
1162 * the real PD value changed. An older version of the HD-audio
1163 * specification worked this way. Hence, we just ignore the data in
1164 * the unsolicited response to avoid custom WARs.
1165 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001166 int present = snd_hda_pin_sense(codec, pin_nid);
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001167 bool eld_valid = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001168
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001169 memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001170
Stephen Warren5d44f922011-05-24 17:11:17 -06001171 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1172 if (eld->monitor_present)
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001173 eld_valid = !!(present & AC_PINSENSE_ELDV);
Stephen Warren5d44f922011-05-24 17:11:17 -06001174
Fengguang Wufae3d882012-04-10 17:00:35 +08001175 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001176 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001177 codec->addr, pin_nid, eld->monitor_present, eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001178
David Henningssonbbfd8a12013-02-19 16:11:22 +01001179 eld->eld_valid = false;
Wu Fengguang744626d2011-11-16 16:29:47 +08001180 if (eld_valid) {
Stephen Warren5d44f922011-05-24 17:11:17 -06001181 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
1182 snd_hdmi_show_eld(eld);
Wu Fengguangc6e84532011-11-18 16:59:32 -06001183 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001184 queue_delayed_work(codec->bus->workq,
1185 &per_pin->work,
1186 msecs_to_jiffies(300));
1187 }
1188 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001189}
1190
Wu Fengguang744626d2011-11-16 16:29:47 +08001191static void hdmi_repoll_eld(struct work_struct *work)
1192{
1193 struct hdmi_spec_per_pin *per_pin =
1194 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1195
Wu Fengguangc6e84532011-11-18 16:59:32 -06001196 if (per_pin->repoll_count++ > 6)
1197 per_pin->repoll_count = 0;
1198
1199 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001200}
1201
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001202static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1203 hda_nid_t nid);
1204
Wu Fengguang079d88c2010-03-08 10:44:23 +08001205static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1206{
1207 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001208 unsigned int caps, config;
1209 int pin_idx;
1210 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001211 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001212
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001213 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001214 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1215 return 0;
1216
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001217 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001218 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1219 return 0;
1220
1221 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001222 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001223
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001224 if (codec->vendor_id == 0x80862807)
1225 intel_haswell_fixup_connect_list(codec, pin_nid);
1226
Stephen Warren384a48d2011-06-01 11:14:21 -06001227 pin_idx = spec->num_pins;
1228 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001229
1230 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001231 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001232
Stephen Warren384a48d2011-06-01 11:14:21 -06001233 err = hdmi_read_pin_conn(codec, pin_idx);
1234 if (err < 0)
1235 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001236
Wu Fengguang079d88c2010-03-08 10:44:23 +08001237 spec->num_pins++;
1238
Stephen Warren384a48d2011-06-01 11:14:21 -06001239 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001240}
1241
Stephen Warren384a48d2011-06-01 11:14:21 -06001242static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001243{
1244 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001245 int cvt_idx;
1246 struct hdmi_spec_per_cvt *per_cvt;
1247 unsigned int chans;
1248 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001249
David Henningsson116dcde2010-11-23 10:23:40 +01001250 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1251 return -E2BIG;
1252
Stephen Warren384a48d2011-06-01 11:14:21 -06001253 chans = get_wcaps(codec, cvt_nid);
1254 chans = get_wcaps_channels(chans);
1255
1256 cvt_idx = spec->num_cvts;
1257 per_cvt = &spec->cvts[cvt_idx];
1258
1259 per_cvt->cvt_nid = cvt_nid;
1260 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001261 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001262 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001263 if (chans > spec->channels_max)
1264 spec->channels_max = chans;
1265 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001266
1267 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1268 &per_cvt->rates,
1269 &per_cvt->formats,
1270 &per_cvt->maxbps);
1271 if (err < 0)
1272 return err;
1273
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001274 spec->cvt_nids[spec->num_cvts++] = cvt_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001275
1276 return 0;
1277}
1278
1279static int hdmi_parse_codec(struct hda_codec *codec)
1280{
1281 hda_nid_t nid;
1282 int i, nodes;
1283
1284 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1285 if (!nid || nodes < 0) {
1286 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1287 return -EINVAL;
1288 }
1289
1290 for (i = 0; i < nodes; i++, nid++) {
1291 unsigned int caps;
1292 unsigned int type;
1293
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001294 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001295 type = get_wcaps_type(caps);
1296
1297 if (!(caps & AC_WCAP_DIGITAL))
1298 continue;
1299
1300 switch (type) {
1301 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001302 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001303 break;
1304 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001305 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001306 break;
1307 }
1308 }
1309
David Henningssonc9adeef2012-11-07 09:22:33 +01001310#ifdef CONFIG_PM
1311 /* We're seeing some problems with unsolicited hot plug events on
1312 * PantherPoint after S3, if this is not enabled */
1313 if (codec->vendor_id == 0x80862806)
1314 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001315 /*
1316 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1317 * can be lost and presence sense verb will become inaccurate if the
1318 * HDA link is powered off at hot plug or hw initialization time.
1319 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001320 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001321 AC_PWRST_EPSS))
1322 codec->bus->power_keep_link_on = 1;
1323#endif
1324
1325 return 0;
1326}
1327
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001328/*
1329 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001330static char *get_hdmi_pcm_name(int idx)
1331{
1332 static char names[MAX_HDMI_PINS][8];
1333 sprintf(&names[idx][0], "HDMI %d", idx);
1334 return &names[idx][0];
1335}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001336
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001337static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1338{
1339 struct hda_spdif_out *spdif;
1340 bool non_pcm;
1341
1342 mutex_lock(&codec->spdif_mutex);
1343 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1344 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1345 mutex_unlock(&codec->spdif_mutex);
1346 return non_pcm;
1347}
1348
1349
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001350/*
1351 * HDMI callbacks
1352 */
1353
1354static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1355 struct hda_codec *codec,
1356 unsigned int stream_tag,
1357 unsigned int format,
1358 struct snd_pcm_substream *substream)
1359{
Stephen Warren384a48d2011-06-01 11:14:21 -06001360 hda_nid_t cvt_nid = hinfo->nid;
1361 struct hdmi_spec *spec = codec->spec;
1362 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1363 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001364 bool non_pcm;
1365
1366 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001367
Stephen Warren384a48d2011-06-01 11:14:21 -06001368 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001369
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001370 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001371
1372 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001373}
1374
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001375static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1376 struct hda_codec *codec,
1377 struct snd_pcm_substream *substream)
1378{
1379 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1380 return 0;
1381}
1382
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001383static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1384 struct hda_codec *codec,
1385 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001386{
1387 struct hdmi_spec *spec = codec->spec;
1388 int cvt_idx, pin_idx;
1389 struct hdmi_spec_per_cvt *per_cvt;
1390 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001391
Stephen Warren384a48d2011-06-01 11:14:21 -06001392 if (hinfo->nid) {
1393 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1394 if (snd_BUG_ON(cvt_idx < 0))
1395 return -EINVAL;
1396 per_cvt = &spec->cvts[cvt_idx];
1397
1398 snd_BUG_ON(!per_cvt->assigned);
1399 per_cvt->assigned = 0;
1400 hinfo->nid = 0;
1401
1402 pin_idx = hinfo_to_pin_index(spec, hinfo);
1403 if (snd_BUG_ON(pin_idx < 0))
1404 return -EINVAL;
1405 per_pin = &spec->pins[pin_idx];
1406
Stephen Warren384a48d2011-06-01 11:14:21 -06001407 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001408 per_pin->chmap_set = false;
1409 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001410 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001411
Stephen Warren384a48d2011-06-01 11:14:21 -06001412 return 0;
1413}
1414
1415static const struct hda_pcm_ops generic_ops = {
1416 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001417 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001418 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001419 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001420};
1421
Takashi Iwaid45e6882012-07-31 11:36:00 +02001422/*
1423 * ALSA API channel-map control callbacks
1424 */
1425static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1426 struct snd_ctl_elem_info *uinfo)
1427{
1428 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1429 struct hda_codec *codec = info->private_data;
1430 struct hdmi_spec *spec = codec->spec;
1431 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1432 uinfo->count = spec->channels_max;
1433 uinfo->value.integer.min = 0;
1434 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1435 return 0;
1436}
1437
1438static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1439 unsigned int size, unsigned int __user *tlv)
1440{
1441 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1442 struct hda_codec *codec = info->private_data;
1443 struct hdmi_spec *spec = codec->spec;
1444 const unsigned int valid_mask =
1445 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1446 unsigned int __user *dst;
1447 int chs, count = 0;
1448
1449 if (size < 8)
1450 return -ENOMEM;
1451 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1452 return -EFAULT;
1453 size -= 8;
1454 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001455 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001456 int i, c;
1457 struct cea_channel_speaker_allocation *cap;
1458 cap = channel_allocations;
1459 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1460 int chs_bytes = chs * 4;
1461 if (cap->channels != chs)
1462 continue;
1463 if (cap->spk_mask & ~valid_mask)
1464 continue;
1465 if (size < 8)
1466 return -ENOMEM;
1467 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1468 put_user(chs_bytes, dst + 1))
1469 return -EFAULT;
1470 dst += 2;
1471 size -= 8;
1472 count += 8;
1473 if (size < chs_bytes)
1474 return -ENOMEM;
1475 size -= chs_bytes;
1476 count += chs_bytes;
1477 for (c = 7; c >= 0; c--) {
1478 int spk = cap->speakers[c];
1479 if (!spk)
1480 continue;
1481 if (put_user(spk_to_chmap(spk), dst))
1482 return -EFAULT;
1483 dst++;
1484 }
1485 }
1486 }
1487 if (put_user(count, tlv + 1))
1488 return -EFAULT;
1489 return 0;
1490}
1491
1492static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_value *ucontrol)
1494{
1495 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1496 struct hda_codec *codec = info->private_data;
1497 struct hdmi_spec *spec = codec->spec;
1498 int pin_idx = kcontrol->private_value;
1499 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1500 int i;
1501
1502 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1503 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1504 return 0;
1505}
1506
1507static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1509{
1510 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1511 struct hda_codec *codec = info->private_data;
1512 struct hdmi_spec *spec = codec->spec;
1513 int pin_idx = kcontrol->private_value;
1514 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1515 unsigned int ctl_idx;
1516 struct snd_pcm_substream *substream;
1517 unsigned char chmap[8];
1518 int i, ca, prepared = 0;
1519
1520 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1521 substream = snd_pcm_chmap_substream(info, ctl_idx);
1522 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001523 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001524 switch (substream->runtime->status->state) {
1525 case SNDRV_PCM_STATE_OPEN:
1526 case SNDRV_PCM_STATE_SETUP:
1527 break;
1528 case SNDRV_PCM_STATE_PREPARED:
1529 prepared = 1;
1530 break;
1531 default:
1532 return -EBUSY;
1533 }
1534 memset(chmap, 0, sizeof(chmap));
1535 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1536 chmap[i] = ucontrol->value.integer.value[i];
1537 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1538 return 0;
1539 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1540 if (ca < 0)
1541 return -EINVAL;
1542 per_pin->chmap_set = true;
1543 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1544 if (prepared)
1545 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1546 substream);
1547
1548 return 0;
1549}
1550
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001551static int generic_hdmi_build_pcms(struct hda_codec *codec)
1552{
1553 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001554 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001555
Stephen Warren384a48d2011-06-01 11:14:21 -06001556 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1557 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001558 struct hda_pcm_stream *pstr;
1559
Stephen Warren384a48d2011-06-01 11:14:21 -06001560 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001561 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001562 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001563 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001564
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001565 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001566 pstr->substreams = 1;
1567 pstr->ops = generic_ops;
1568 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001569 }
1570
Stephen Warren384a48d2011-06-01 11:14:21 -06001571 codec->num_pcms = spec->num_pins;
1572 codec->pcm_info = spec->pcm_rec;
1573
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001574 return 0;
1575}
1576
David Henningsson0b6c49b2011-08-23 16:56:03 +02001577static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1578{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001579 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001580 struct hdmi_spec *spec = codec->spec;
1581 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1582 int pcmdev = spec->pcm_rec[pin_idx].device;
1583
Takashi Iwai31ef2252011-12-01 17:41:36 +01001584 if (pcmdev > 0)
1585 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001586
Takashi Iwai31ef2252011-12-01 17:41:36 +01001587 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001588}
1589
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001590static int generic_hdmi_build_controls(struct hda_codec *codec)
1591{
1592 struct hdmi_spec *spec = codec->spec;
1593 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001594 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001595
Stephen Warren384a48d2011-06-01 11:14:21 -06001596 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1597 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001598
1599 err = generic_hdmi_build_jack(codec, pin_idx);
1600 if (err < 0)
1601 return err;
1602
Takashi Iwaidcda5802012-10-12 17:24:51 +02001603 err = snd_hda_create_dig_out_ctls(codec,
1604 per_pin->pin_nid,
1605 per_pin->mux_nids[0],
1606 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001607 if (err < 0)
1608 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001609 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001610
1611 /* add control for ELD Bytes */
1612 err = hdmi_create_eld_ctl(codec,
1613 pin_idx,
1614 spec->pcm_rec[pin_idx].device);
1615
1616 if (err < 0)
1617 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001618
Takashi Iwai82b1d732011-12-20 15:53:07 +01001619 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001620 }
1621
Takashi Iwaid45e6882012-07-31 11:36:00 +02001622 /* add channel maps */
1623 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1624 struct snd_pcm_chmap *chmap;
1625 struct snd_kcontrol *kctl;
1626 int i;
1627 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1628 SNDRV_PCM_STREAM_PLAYBACK,
1629 NULL, 0, pin_idx, &chmap);
1630 if (err < 0)
1631 return err;
1632 /* override handlers */
1633 chmap->private_data = codec;
1634 kctl = chmap->kctl;
1635 for (i = 0; i < kctl->count; i++)
1636 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1637 kctl->info = hdmi_chmap_ctl_info;
1638 kctl->get = hdmi_chmap_ctl_get;
1639 kctl->put = hdmi_chmap_ctl_put;
1640 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1641 }
1642
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001643 return 0;
1644}
1645
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001646static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1647{
1648 struct hdmi_spec *spec = codec->spec;
1649 int pin_idx;
1650
1651 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1652 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1653 struct hdmi_eld *eld = &per_pin->sink_eld;
1654
1655 per_pin->codec = codec;
1656 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1657 snd_hda_eld_proc_new(codec, eld, pin_idx);
1658 }
1659 return 0;
1660}
1661
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001662static int generic_hdmi_init(struct hda_codec *codec)
1663{
1664 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001665 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001666
Stephen Warren384a48d2011-06-01 11:14:21 -06001667 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1668 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1669 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001670
1671 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001672 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001673 }
1674 return 0;
1675}
1676
1677static void generic_hdmi_free(struct hda_codec *codec)
1678{
1679 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001680 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001681
Stephen Warren384a48d2011-06-01 11:14:21 -06001682 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1683 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1684 struct hdmi_eld *eld = &per_pin->sink_eld;
1685
Wu Fengguang744626d2011-11-16 16:29:47 +08001686 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001687 snd_hda_eld_proc_free(codec, eld);
1688 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001689
Wu Fengguang744626d2011-11-16 16:29:47 +08001690 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001691 kfree(spec);
1692}
1693
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001694static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001695 .init = generic_hdmi_init,
1696 .free = generic_hdmi_free,
1697 .build_pcms = generic_hdmi_build_pcms,
1698 .build_controls = generic_hdmi_build_controls,
1699 .unsol_event = hdmi_unsol_event,
1700};
1701
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001702
1703static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1704 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001705{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001706 struct hdmi_spec *spec = codec->spec;
1707 hda_nid_t conns[4];
1708 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001709
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001710 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1711 if (nconns == spec->num_cvts &&
1712 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001713 return;
1714
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001715 /* override pins connection list */
1716 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1717 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001718}
1719
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001720#define INTEL_VENDOR_NID 0x08
1721#define INTEL_GET_VENDOR_VERB 0xf81
1722#define INTEL_SET_VENDOR_VERB 0x781
1723#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1724#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1725
1726static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1727 const struct hda_fixup *fix, int action)
1728{
1729 unsigned int vendor_param;
1730
1731 if (action != HDA_FIXUP_ACT_PRE_PROBE)
1732 return;
1733 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1734 INTEL_GET_VENDOR_VERB, 0);
1735 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1736 return;
1737
1738 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1739 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1740 INTEL_SET_VENDOR_VERB, vendor_param);
1741 if (vendor_param == -1)
1742 return;
1743
1744 snd_hda_codec_update_widgets(codec);
1745 return;
1746}
1747
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001748static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1749{
1750 unsigned int vendor_param;
1751
1752 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1753 INTEL_GET_VENDOR_VERB, 0);
1754 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1755 return;
1756
1757 /* enable DP1.2 mode */
1758 vendor_param |= INTEL_EN_DP12;
1759 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1760 INTEL_SET_VENDOR_VERB, vendor_param);
1761}
1762
1763
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001764
1765/* available models for fixup */
1766enum {
1767 INTEL_HASWELL,
1768};
1769
1770static const struct hda_model_fixup hdmi_models[] = {
1771 {.id = INTEL_HASWELL, .name = "Haswell"},
1772 {}
1773};
1774
1775static const struct snd_pci_quirk hdmi_fixup_tbl[] = {
1776 SND_PCI_QUIRK(0x8086, 0x2010, "Haswell", INTEL_HASWELL),
1777 {} /* terminator */
1778};
1779
1780static const struct hda_fixup hdmi_fixups[] = {
1781 [INTEL_HASWELL] = {
1782 .type = HDA_FIXUP_FUNC,
1783 .v.func = intel_haswell_enable_all_pins,
1784 },
1785};
1786
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001787
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001788static int patch_generic_hdmi(struct hda_codec *codec)
1789{
1790 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001791
1792 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1793 if (spec == NULL)
1794 return -ENOMEM;
1795
1796 codec->spec = spec;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001797
Mengdong Lin1611a9c2013-02-08 17:09:52 -05001798 snd_hda_pick_fixup(codec, hdmi_models, hdmi_fixup_tbl, hdmi_fixups);
1799 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1800
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001801 if (codec->vendor_id == 0x80862807)
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001802 intel_haswell_fixup_enable_dp12(codec);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001803
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001804 if (hdmi_parse_codec(codec) < 0) {
1805 codec->spec = NULL;
1806 kfree(spec);
1807 return -EINVAL;
1808 }
1809 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001810 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001811
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001812 init_channel_allocations();
1813
1814 return 0;
1815}
1816
1817/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001818 * Shared non-generic implementations
1819 */
1820
1821static int simple_playback_build_pcms(struct hda_codec *codec)
1822{
1823 struct hdmi_spec *spec = codec->spec;
1824 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001825 unsigned int chans;
1826 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001827
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001828 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001829 codec->pcm_info = info;
1830
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001831 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1832 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001833
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001834 info->name = get_hdmi_pcm_name(0);
1835 info->pcm_type = HDA_PCM_TYPE_HDMI;
1836 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1837 *pstr = spec->pcm_playback;
1838 pstr->nid = spec->cvts[0].cvt_nid;
1839 if (pstr->channels_max <= 2 && chans && chans <= 16)
1840 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001841
1842 return 0;
1843}
1844
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001845/* unsolicited event for jack sensing */
1846static void simple_hdmi_unsol_event(struct hda_codec *codec,
1847 unsigned int res)
1848{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001849 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001850 snd_hda_jack_report_sync(codec);
1851}
1852
1853/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1854 * as long as spec->pins[] is set correctly
1855 */
1856#define simple_hdmi_build_jack generic_hdmi_build_jack
1857
Stephen Warren3aaf8982011-06-01 11:14:19 -06001858static int simple_playback_build_controls(struct hda_codec *codec)
1859{
1860 struct hdmi_spec *spec = codec->spec;
1861 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001862
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001863 err = snd_hda_create_spdif_out_ctls(codec,
1864 spec->cvts[0].cvt_nid,
1865 spec->cvts[0].cvt_nid);
1866 if (err < 0)
1867 return err;
1868 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001869}
1870
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001871static int simple_playback_init(struct hda_codec *codec)
1872{
1873 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001874 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001875
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001876 snd_hda_codec_write(codec, pin, 0,
1877 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1878 /* some codecs require to unmute the pin */
1879 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1880 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1881 AMP_OUT_UNMUTE);
1882 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001883 return 0;
1884}
1885
Stephen Warren3aaf8982011-06-01 11:14:19 -06001886static void simple_playback_free(struct hda_codec *codec)
1887{
1888 struct hdmi_spec *spec = codec->spec;
1889
1890 kfree(spec);
1891}
1892
1893/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001894 * Nvidia specific implementations
1895 */
1896
1897#define Nv_VERB_SET_Channel_Allocation 0xF79
1898#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1899#define Nv_VERB_SET_Audio_Protection_On 0xF98
1900#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1901
1902#define nvhdmi_master_con_nid_7x 0x04
1903#define nvhdmi_master_pin_nid_7x 0x05
1904
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001905static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001906 /*front, rear, clfe, rear_surr */
1907 0x6, 0x8, 0xa, 0xc,
1908};
1909
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001910static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1911 /* set audio protect on */
1912 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1913 /* enable digital output on pin widget */
1914 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1915 {} /* terminator */
1916};
1917
1918static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001919 /* set audio protect on */
1920 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1921 /* enable digital output on pin widget */
1922 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1923 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1924 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1925 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1926 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1927 {} /* terminator */
1928};
1929
1930#ifdef LIMITED_RATE_FMT_SUPPORT
1931/* support only the safe format and rate */
1932#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1933#define SUPPORTED_MAXBPS 16
1934#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1935#else
1936/* support all rates and formats */
1937#define SUPPORTED_RATES \
1938 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1939 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1940 SNDRV_PCM_RATE_192000)
1941#define SUPPORTED_MAXBPS 24
1942#define SUPPORTED_FORMATS \
1943 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1944#endif
1945
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001946static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001947{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001948 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1949 return 0;
1950}
1951
1952static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
1953{
1954 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001955 return 0;
1956}
1957
Nitin Daga393004b2011-01-10 21:49:31 +05301958static unsigned int channels_2_6_8[] = {
1959 2, 6, 8
1960};
1961
1962static unsigned int channels_2_8[] = {
1963 2, 8
1964};
1965
1966static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
1967 .count = ARRAY_SIZE(channels_2_6_8),
1968 .list = channels_2_6_8,
1969 .mask = 0,
1970};
1971
1972static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
1973 .count = ARRAY_SIZE(channels_2_8),
1974 .list = channels_2_8,
1975 .mask = 0,
1976};
1977
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001978static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
1979 struct hda_codec *codec,
1980 struct snd_pcm_substream *substream)
1981{
1982 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05301983 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
1984
1985 switch (codec->preset->id) {
1986 case 0x10de0002:
1987 case 0x10de0003:
1988 case 0x10de0005:
1989 case 0x10de0006:
1990 hw_constraints_channels = &hw_constraints_2_8_channels;
1991 break;
1992 case 0x10de0007:
1993 hw_constraints_channels = &hw_constraints_2_6_8_channels;
1994 break;
1995 default:
1996 break;
1997 }
1998
1999 if (hw_constraints_channels != NULL) {
2000 snd_pcm_hw_constraint_list(substream->runtime, 0,
2001 SNDRV_PCM_HW_PARAM_CHANNELS,
2002 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002003 } else {
2004 snd_pcm_hw_constraint_step(substream->runtime, 0,
2005 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302006 }
2007
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002008 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2009}
2010
2011static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2012 struct hda_codec *codec,
2013 struct snd_pcm_substream *substream)
2014{
2015 struct hdmi_spec *spec = codec->spec;
2016 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2017}
2018
2019static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2020 struct hda_codec *codec,
2021 unsigned int stream_tag,
2022 unsigned int format,
2023 struct snd_pcm_substream *substream)
2024{
2025 struct hdmi_spec *spec = codec->spec;
2026 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2027 stream_tag, format, substream);
2028}
2029
Takashi Iwaid0b12522012-06-15 14:34:42 +02002030static const struct hda_pcm_stream simple_pcm_playback = {
2031 .substreams = 1,
2032 .channels_min = 2,
2033 .channels_max = 2,
2034 .ops = {
2035 .open = simple_playback_pcm_open,
2036 .close = simple_playback_pcm_close,
2037 .prepare = simple_playback_pcm_prepare
2038 },
2039};
2040
2041static const struct hda_codec_ops simple_hdmi_patch_ops = {
2042 .build_controls = simple_playback_build_controls,
2043 .build_pcms = simple_playback_build_pcms,
2044 .init = simple_playback_init,
2045 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002046 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02002047};
2048
2049static int patch_simple_hdmi(struct hda_codec *codec,
2050 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2051{
2052 struct hdmi_spec *spec;
2053
2054 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2055 if (!spec)
2056 return -ENOMEM;
2057
2058 codec->spec = spec;
2059
2060 spec->multiout.num_dacs = 0; /* no analog */
2061 spec->multiout.max_channels = 2;
2062 spec->multiout.dig_out_nid = cvt_nid;
2063 spec->num_cvts = 1;
2064 spec->num_pins = 1;
2065 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02002066 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002067 spec->pcm_playback = simple_pcm_playback;
2068
2069 codec->patch_ops = simple_hdmi_patch_ops;
2070
2071 return 0;
2072}
2073
Aaron Plattner1f348522011-04-06 17:19:04 -07002074static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2075 int channels)
2076{
2077 unsigned int chanmask;
2078 int chan = channels ? (channels - 1) : 1;
2079
2080 switch (channels) {
2081 default:
2082 case 0:
2083 case 2:
2084 chanmask = 0x00;
2085 break;
2086 case 4:
2087 chanmask = 0x08;
2088 break;
2089 case 6:
2090 chanmask = 0x0b;
2091 break;
2092 case 8:
2093 chanmask = 0x13;
2094 break;
2095 }
2096
2097 /* Set the audio infoframe channel allocation and checksum fields. The
2098 * channel count is computed implicitly by the hardware. */
2099 snd_hda_codec_write(codec, 0x1, 0,
2100 Nv_VERB_SET_Channel_Allocation, chanmask);
2101
2102 snd_hda_codec_write(codec, 0x1, 0,
2103 Nv_VERB_SET_Info_Frame_Checksum,
2104 (0x71 - chan - chanmask));
2105}
2106
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002107static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2108 struct hda_codec *codec,
2109 struct snd_pcm_substream *substream)
2110{
2111 struct hdmi_spec *spec = codec->spec;
2112 int i;
2113
2114 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2115 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2116 for (i = 0; i < 4; i++) {
2117 /* set the stream id */
2118 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2119 AC_VERB_SET_CHANNEL_STREAMID, 0);
2120 /* set the stream format */
2121 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2122 AC_VERB_SET_STREAM_FORMAT, 0);
2123 }
2124
Aaron Plattner1f348522011-04-06 17:19:04 -07002125 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2126 * streams are disabled. */
2127 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2128
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002129 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2130}
2131
2132static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2133 struct hda_codec *codec,
2134 unsigned int stream_tag,
2135 unsigned int format,
2136 struct snd_pcm_substream *substream)
2137{
2138 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002139 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002140 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002141 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002142 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002143
2144 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002145 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002146
2147 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002148
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002149 dataDCC2 = 0x2;
2150
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002151 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002152 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002153 snd_hda_codec_write(codec,
2154 nvhdmi_master_con_nid_7x,
2155 0,
2156 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002157 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002158
2159 /* set the stream id */
2160 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2161 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2162
2163 /* set the stream format */
2164 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2165 AC_VERB_SET_STREAM_FORMAT, format);
2166
2167 /* turn on again (if needed) */
2168 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002169 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002170 snd_hda_codec_write(codec,
2171 nvhdmi_master_con_nid_7x,
2172 0,
2173 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002174 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002175 snd_hda_codec_write(codec,
2176 nvhdmi_master_con_nid_7x,
2177 0,
2178 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2179 }
2180
2181 for (i = 0; i < 4; i++) {
2182 if (chs == 2)
2183 channel_id = 0;
2184 else
2185 channel_id = i * 2;
2186
2187 /* turn off SPDIF once;
2188 *otherwise the IEC958 bits won't be updated
2189 */
2190 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002191 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002192 snd_hda_codec_write(codec,
2193 nvhdmi_con_nids_7x[i],
2194 0,
2195 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002196 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002197 /* set the stream id */
2198 snd_hda_codec_write(codec,
2199 nvhdmi_con_nids_7x[i],
2200 0,
2201 AC_VERB_SET_CHANNEL_STREAMID,
2202 (stream_tag << 4) | channel_id);
2203 /* set the stream format */
2204 snd_hda_codec_write(codec,
2205 nvhdmi_con_nids_7x[i],
2206 0,
2207 AC_VERB_SET_STREAM_FORMAT,
2208 format);
2209 /* turn on again (if needed) */
2210 /* enable and set the channel status audio/data flag */
2211 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002212 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002213 snd_hda_codec_write(codec,
2214 nvhdmi_con_nids_7x[i],
2215 0,
2216 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002217 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002218 snd_hda_codec_write(codec,
2219 nvhdmi_con_nids_7x[i],
2220 0,
2221 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2222 }
2223 }
2224
Aaron Plattner1f348522011-04-06 17:19:04 -07002225 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002226
2227 mutex_unlock(&codec->spdif_mutex);
2228 return 0;
2229}
2230
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002231static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002232 .substreams = 1,
2233 .channels_min = 2,
2234 .channels_max = 8,
2235 .nid = nvhdmi_master_con_nid_7x,
2236 .rates = SUPPORTED_RATES,
2237 .maxbps = SUPPORTED_MAXBPS,
2238 .formats = SUPPORTED_FORMATS,
2239 .ops = {
2240 .open = simple_playback_pcm_open,
2241 .close = nvhdmi_8ch_7x_pcm_close,
2242 .prepare = nvhdmi_8ch_7x_pcm_prepare
2243 },
2244};
2245
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002246static int patch_nvhdmi_2ch(struct hda_codec *codec)
2247{
2248 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002249 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2250 nvhdmi_master_pin_nid_7x);
2251 if (err < 0)
2252 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002253
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002254 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002255 /* override the PCM rates, etc, as the codec doesn't give full list */
2256 spec = codec->spec;
2257 spec->pcm_playback.rates = SUPPORTED_RATES;
2258 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2259 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002260 return 0;
2261}
2262
Takashi Iwai53775b02012-08-01 12:17:41 +02002263static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2264{
2265 struct hdmi_spec *spec = codec->spec;
2266 int err = simple_playback_build_pcms(codec);
2267 spec->pcm_rec[0].own_chmap = true;
2268 return err;
2269}
2270
2271static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2272{
2273 struct hdmi_spec *spec = codec->spec;
2274 struct snd_pcm_chmap *chmap;
2275 int err;
2276
2277 err = simple_playback_build_controls(codec);
2278 if (err < 0)
2279 return err;
2280
2281 /* add channel maps */
2282 err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
2283 SNDRV_PCM_STREAM_PLAYBACK,
2284 snd_pcm_alt_chmaps, 8, 0, &chmap);
2285 if (err < 0)
2286 return err;
2287 switch (codec->preset->id) {
2288 case 0x10de0002:
2289 case 0x10de0003:
2290 case 0x10de0005:
2291 case 0x10de0006:
2292 chmap->channel_mask = (1U << 2) | (1U << 8);
2293 break;
2294 case 0x10de0007:
2295 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2296 }
2297 return 0;
2298}
2299
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002300static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2301{
2302 struct hdmi_spec *spec;
2303 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002304 if (err < 0)
2305 return err;
2306 spec = codec->spec;
2307 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002308 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002309 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002310 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2311 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002312
2313 /* Initialize the audio infoframe channel mask and checksum to something
2314 * valid */
2315 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2316
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002317 return 0;
2318}
2319
2320/*
2321 * ATI-specific implementations
2322 *
2323 * FIXME: we may omit the whole this and use the generic code once after
2324 * it's confirmed to work.
2325 */
2326
2327#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2328#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2329
2330static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2331 struct hda_codec *codec,
2332 unsigned int stream_tag,
2333 unsigned int format,
2334 struct snd_pcm_substream *substream)
2335{
2336 struct hdmi_spec *spec = codec->spec;
2337 int chans = substream->runtime->channels;
2338 int i, err;
2339
2340 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2341 substream);
2342 if (err < 0)
2343 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002344 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
2345 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002346 /* FIXME: XXX */
2347 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002348 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002349 AC_VERB_SET_HDMI_CHAN_SLOT,
2350 (i << 4) | i);
2351 }
2352 return 0;
2353}
2354
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002355static int patch_atihdmi(struct hda_codec *codec)
2356{
2357 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002358 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2359 if (err < 0)
2360 return err;
2361 spec = codec->spec;
2362 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002363 return 0;
2364}
2365
Annie Liu3de5ff82012-06-08 19:18:42 +08002366/* VIA HDMI Implementation */
2367#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2368#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2369
Annie Liu3de5ff82012-06-08 19:18:42 +08002370static int patch_via_hdmi(struct hda_codec *codec)
2371{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002372 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002373}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002374
2375/*
2376 * patch entries
2377 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002378static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002379{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2380{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2381{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002382{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002383{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2384{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2385{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2386{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2387{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2388{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2389{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2390{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002391{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2392{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2393{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2394{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2395{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2396{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2397{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2398{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2399{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2400{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2401{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002402/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002403{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2404{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2405{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2406{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2407{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2408{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2409{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2410{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2411{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2412{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002413{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002414{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2415{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002416{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2417{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2418{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2419{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002420{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2421{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2422{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2423{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2424{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2425{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002426{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002427{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002428{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002429{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2430{} /* terminator */
2431};
2432
2433MODULE_ALIAS("snd-hda-codec-id:1002793c");
2434MODULE_ALIAS("snd-hda-codec-id:10027919");
2435MODULE_ALIAS("snd-hda-codec-id:1002791a");
2436MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2437MODULE_ALIAS("snd-hda-codec-id:10951390");
2438MODULE_ALIAS("snd-hda-codec-id:10951392");
2439MODULE_ALIAS("snd-hda-codec-id:10de0002");
2440MODULE_ALIAS("snd-hda-codec-id:10de0003");
2441MODULE_ALIAS("snd-hda-codec-id:10de0005");
2442MODULE_ALIAS("snd-hda-codec-id:10de0006");
2443MODULE_ALIAS("snd-hda-codec-id:10de0007");
2444MODULE_ALIAS("snd-hda-codec-id:10de000a");
2445MODULE_ALIAS("snd-hda-codec-id:10de000b");
2446MODULE_ALIAS("snd-hda-codec-id:10de000c");
2447MODULE_ALIAS("snd-hda-codec-id:10de000d");
2448MODULE_ALIAS("snd-hda-codec-id:10de0010");
2449MODULE_ALIAS("snd-hda-codec-id:10de0011");
2450MODULE_ALIAS("snd-hda-codec-id:10de0012");
2451MODULE_ALIAS("snd-hda-codec-id:10de0013");
2452MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002453MODULE_ALIAS("snd-hda-codec-id:10de0015");
2454MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002455MODULE_ALIAS("snd-hda-codec-id:10de0018");
2456MODULE_ALIAS("snd-hda-codec-id:10de0019");
2457MODULE_ALIAS("snd-hda-codec-id:10de001a");
2458MODULE_ALIAS("snd-hda-codec-id:10de001b");
2459MODULE_ALIAS("snd-hda-codec-id:10de001c");
2460MODULE_ALIAS("snd-hda-codec-id:10de0040");
2461MODULE_ALIAS("snd-hda-codec-id:10de0041");
2462MODULE_ALIAS("snd-hda-codec-id:10de0042");
2463MODULE_ALIAS("snd-hda-codec-id:10de0043");
2464MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002465MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002466MODULE_ALIAS("snd-hda-codec-id:10de0067");
2467MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002468MODULE_ALIAS("snd-hda-codec-id:11069f80");
2469MODULE_ALIAS("snd-hda-codec-id:11069f81");
2470MODULE_ALIAS("snd-hda-codec-id:11069f84");
2471MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002472MODULE_ALIAS("snd-hda-codec-id:17e80047");
2473MODULE_ALIAS("snd-hda-codec-id:80860054");
2474MODULE_ALIAS("snd-hda-codec-id:80862801");
2475MODULE_ALIAS("snd-hda-codec-id:80862802");
2476MODULE_ALIAS("snd-hda-codec-id:80862803");
2477MODULE_ALIAS("snd-hda-codec-id:80862804");
2478MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002479MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002480MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002481MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002482MODULE_ALIAS("snd-hda-codec-id:808629fb");
2483
2484MODULE_LICENSE("GPL");
2485MODULE_DESCRIPTION("HDMI HD-audio codec");
2486MODULE_ALIAS("snd-hda-codec-intelhdmi");
2487MODULE_ALIAS("snd-hda-codec-nvhdmi");
2488MODULE_ALIAS("snd-hda-codec-atihdmi");
2489
2490static struct hda_codec_preset_list intel_list = {
2491 .preset = snd_hda_preset_hdmi,
2492 .owner = THIS_MODULE,
2493};
2494
2495static int __init patch_hdmi_init(void)
2496{
2497 return snd_hda_add_codec_preset(&intel_list);
2498}
2499
2500static void __exit patch_hdmi_exit(void)
2501{
2502 snd_hda_delete_codec_preset(&intel_list);
2503}
2504
2505module_init(patch_hdmi_init)
2506module_exit(patch_hdmi_exit)