blob: 899c4fbbfd83fe08ca79b50d98829164f397778c [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];
87
Wu Fengguang079d88c2010-03-08 10:44:23 +080088 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060089 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
90 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Takashi Iwaid45e6882012-07-31 11:36:00 +020091 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080092
93 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060094 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080095 */
96 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020097 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080098};
99
100
101struct hdmi_audio_infoframe {
102 u8 type; /* 0x84 */
103 u8 ver; /* 0x01 */
104 u8 len; /* 0x0a */
105
Wu Fengguang53d7d692010-09-21 14:25:49 +0800106 u8 checksum;
107
Wu Fengguang079d88c2010-03-08 10:44:23 +0800108 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
109 u8 SS01_SF24;
110 u8 CXT04;
111 u8 CA;
112 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800113};
114
115struct dp_audio_infoframe {
116 u8 type; /* 0x84 */
117 u8 len; /* 0x1b */
118 u8 ver; /* 0x11 << 2 */
119
120 u8 CC02_CT47; /* match with HDMI infoframe from this on */
121 u8 SS01_SF24;
122 u8 CXT04;
123 u8 CA;
124 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800125};
126
Takashi Iwai2b203db2011-02-11 12:17:30 +0100127union audio_infoframe {
128 struct hdmi_audio_infoframe hdmi;
129 struct dp_audio_infoframe dp;
130 u8 bytes[0];
131};
132
Wu Fengguang079d88c2010-03-08 10:44:23 +0800133/*
134 * CEA speaker placement:
135 *
136 * FLH FCH FRH
137 * FLW FL FLC FC FRC FR FRW
138 *
139 * LFE
140 * TC
141 *
142 * RL RLC RC RRC RR
143 *
144 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
145 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
146 */
147enum cea_speaker_placement {
148 FL = (1 << 0), /* Front Left */
149 FC = (1 << 1), /* Front Center */
150 FR = (1 << 2), /* Front Right */
151 FLC = (1 << 3), /* Front Left Center */
152 FRC = (1 << 4), /* Front Right Center */
153 RL = (1 << 5), /* Rear Left */
154 RC = (1 << 6), /* Rear Center */
155 RR = (1 << 7), /* Rear Right */
156 RLC = (1 << 8), /* Rear Left Center */
157 RRC = (1 << 9), /* Rear Right Center */
158 LFE = (1 << 10), /* Low Frequency Effect */
159 FLW = (1 << 11), /* Front Left Wide */
160 FRW = (1 << 12), /* Front Right Wide */
161 FLH = (1 << 13), /* Front Left High */
162 FCH = (1 << 14), /* Front Center High */
163 FRH = (1 << 15), /* Front Right High */
164 TC = (1 << 16), /* Top Center */
165};
166
167/*
168 * ELD SA bits in the CEA Speaker Allocation data block
169 */
170static int eld_speaker_allocation_bits[] = {
171 [0] = FL | FR,
172 [1] = LFE,
173 [2] = FC,
174 [3] = RL | RR,
175 [4] = RC,
176 [5] = FLC | FRC,
177 [6] = RLC | RRC,
178 /* the following are not defined in ELD yet */
179 [7] = FLW | FRW,
180 [8] = FLH | FRH,
181 [9] = TC,
182 [10] = FCH,
183};
184
185struct cea_channel_speaker_allocation {
186 int ca_index;
187 int speakers[8];
188
189 /* derived values, just for convenience */
190 int channels;
191 int spk_mask;
192};
193
194/*
195 * ALSA sequence is:
196 *
197 * surround40 surround41 surround50 surround51 surround71
198 * ch0 front left = = = =
199 * ch1 front right = = = =
200 * ch2 rear left = = = =
201 * ch3 rear right = = = =
202 * ch4 LFE center center center
203 * ch5 LFE LFE
204 * ch6 side left
205 * ch7 side right
206 *
207 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
208 */
209static int hdmi_channel_mapping[0x32][8] = {
210 /* stereo */
211 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
212 /* 2.1 */
213 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
214 /* Dolby Surround */
215 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
216 /* surround40 */
217 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
218 /* 4ch */
219 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
220 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800221 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800222 /* surround50 */
223 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
224 /* surround51 */
225 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
226 /* 7.1 */
227 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
228};
229
230/*
231 * This is an ordered list!
232 *
233 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800234 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800235 */
236static struct cea_channel_speaker_allocation channel_allocations[] = {
237/* channel: 7 6 5 4 3 2 1 0 */
238{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
239 /* 2.1 */
240{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
241 /* Dolby Surround */
242{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
243 /* surround40 */
244{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
245 /* surround41 */
246{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
247 /* surround50 */
248{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
249 /* surround51 */
250{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
251 /* 6.1 */
252{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
253 /* surround71 */
254{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
255
256{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
257{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
258{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
259{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
260{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
261{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
262{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
263{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
264{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
265{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
266{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
267{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
268{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
269{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
270{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
271{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
272{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
273{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
274{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
275{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
276{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
277{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
278{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
279{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
280{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
281{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
282{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
283{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
284{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
285{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
286{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
287{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
288{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
289{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
290{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
291{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
292{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
293{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
294{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
295{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
296{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
297};
298
299
300/*
301 * HDMI routines
302 */
303
Stephen Warren384a48d2011-06-01 11:14:21 -0600304static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800305{
Stephen Warren384a48d2011-06-01 11:14:21 -0600306 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800307
Stephen Warren384a48d2011-06-01 11:14:21 -0600308 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
309 if (spec->pins[pin_idx].pin_nid == pin_nid)
310 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800311
Stephen Warren384a48d2011-06-01 11:14:21 -0600312 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
313 return -EINVAL;
314}
315
316static int hinfo_to_pin_index(struct hdmi_spec *spec,
317 struct hda_pcm_stream *hinfo)
318{
319 int pin_idx;
320
321 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
322 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
323 return pin_idx;
324
325 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
326 return -EINVAL;
327}
328
329static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
330{
331 int cvt_idx;
332
333 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
334 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
335 return cvt_idx;
336
337 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800338 return -EINVAL;
339}
340
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500341static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
342 struct snd_ctl_elem_info *uinfo)
343{
344 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
345 struct hdmi_spec *spec;
346 int pin_idx;
347
348 spec = codec->spec;
349 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
350
351 pin_idx = kcontrol->private_value;
352 uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
353
354 return 0;
355}
356
357static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
358 struct snd_ctl_elem_value *ucontrol)
359{
360 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
361 struct hdmi_spec *spec;
362 int pin_idx;
363
364 spec = codec->spec;
365 pin_idx = kcontrol->private_value;
366
367 memcpy(ucontrol->value.bytes.data,
368 spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
369
370 return 0;
371}
372
373static struct snd_kcontrol_new eld_bytes_ctl = {
374 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
375 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
376 .name = "ELD",
377 .info = hdmi_eld_ctl_info,
378 .get = hdmi_eld_ctl_get,
379};
380
381static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
382 int device)
383{
384 struct snd_kcontrol *kctl;
385 struct hdmi_spec *spec = codec->spec;
386 int err;
387
388 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
389 if (!kctl)
390 return -ENOMEM;
391 kctl->private_value = pin_idx;
392 kctl->id.device = device;
393
394 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
395 if (err < 0)
396 return err;
397
398 return 0;
399}
400
Wu Fengguang079d88c2010-03-08 10:44:23 +0800401#ifdef BE_PARANOID
402static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
403 int *packet_index, int *byte_index)
404{
405 int val;
406
407 val = snd_hda_codec_read(codec, pin_nid, 0,
408 AC_VERB_GET_HDMI_DIP_INDEX, 0);
409
410 *packet_index = val >> 5;
411 *byte_index = val & 0x1f;
412}
413#endif
414
415static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
416 int packet_index, int byte_index)
417{
418 int val;
419
420 val = (packet_index << 5) | (byte_index & 0x1f);
421
422 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
423}
424
425static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
426 unsigned char val)
427{
428 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
429}
430
Stephen Warren384a48d2011-06-01 11:14:21 -0600431static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800432{
433 /* Unmute */
434 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
435 snd_hda_codec_write(codec, pin_nid, 0,
436 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100437 /* Enable pin out: some machines with GM965 gets broken output when
438 * the pin is disabled or changed while using with HDMI
439 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800440 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100441 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800442}
443
Stephen Warren384a48d2011-06-01 11:14:21 -0600444static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800445{
Stephen Warren384a48d2011-06-01 11:14:21 -0600446 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800447 AC_VERB_GET_CVT_CHAN_COUNT, 0);
448}
449
450static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600451 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800452{
Stephen Warren384a48d2011-06-01 11:14:21 -0600453 if (chs != hdmi_get_channel_count(codec, cvt_nid))
454 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800455 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
456}
457
458
459/*
460 * Channel mapping routines
461 */
462
463/*
464 * Compute derived values in channel_allocations[].
465 */
466static void init_channel_allocations(void)
467{
468 int i, j;
469 struct cea_channel_speaker_allocation *p;
470
471 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
472 p = channel_allocations + i;
473 p->channels = 0;
474 p->spk_mask = 0;
475 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
476 if (p->speakers[j]) {
477 p->channels++;
478 p->spk_mask |= p->speakers[j];
479 }
480 }
481}
482
Wang Xingchao72357c72012-09-06 10:02:36 +0800483static int get_channel_allocation_order(int ca)
484{
485 int i;
486
487 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
488 if (channel_allocations[i].ca_index == ca)
489 break;
490 }
491 return i;
492}
493
Wu Fengguang079d88c2010-03-08 10:44:23 +0800494/*
495 * The transformation takes two steps:
496 *
497 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
498 * spk_mask => (channel_allocations[]) => ai->CA
499 *
500 * TODO: it could select the wrong CA from multiple candidates.
501*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600502static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800503{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800504 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800505 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800506 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800507 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
508
509 /*
510 * CA defaults to 0 for basic stereo audio
511 */
512 if (channels <= 2)
513 return 0;
514
Wu Fengguang079d88c2010-03-08 10:44:23 +0800515 /*
516 * expand ELD's speaker allocation mask
517 *
518 * ELD tells the speaker mask in a compact(paired) form,
519 * expand ELD's notions to match the ones used by Audio InfoFrame.
520 */
521 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
522 if (eld->spk_alloc & (1 << i))
523 spk_mask |= eld_speaker_allocation_bits[i];
524 }
525
526 /* search for the first working match in the CA table */
527 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
528 if (channels == channel_allocations[i].channels &&
529 (spk_mask & channel_allocations[i].spk_mask) ==
530 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800531 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800532 break;
533 }
534 }
535
536 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800537 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800538 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800539
Wu Fengguang53d7d692010-09-21 14:25:49 +0800540 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800541}
542
543static void hdmi_debug_channel_mapping(struct hda_codec *codec,
544 hda_nid_t pin_nid)
545{
546#ifdef CONFIG_SND_DEBUG_VERBOSE
547 int i;
548 int slot;
549
550 for (i = 0; i < 8; i++) {
551 slot = snd_hda_codec_read(codec, pin_nid, 0,
552 AC_VERB_GET_HDMI_CHAN_SLOT, i);
553 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
554 slot >> 4, slot & 0xf);
555 }
556#endif
557}
558
559
Takashi Iwaid45e6882012-07-31 11:36:00 +0200560static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800561 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800562 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800563 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800564{
565 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800566 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800567 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800568 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800569
Wang Xingchao72357c72012-09-06 10:02:36 +0800570 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800571
Wu Fengguang079d88c2010-03-08 10:44:23 +0800572 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800573 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800574 hdmi_channel_mapping[ca][i] = i | (i << 4);
575 for (; i < 8; i++)
576 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
577 }
578
Wang Xingchao433968d2012-09-06 10:02:37 +0800579 if (non_pcm) {
580 for (i = 0; i < channel_allocations[order].channels; i++)
581 non_pcm_mapping[i] = i | (i << 4);
582 for (; i < 8; i++)
583 non_pcm_mapping[i] = 0xf | (i << 4);
584 }
585
Wu Fengguang079d88c2010-03-08 10:44:23 +0800586 for (i = 0; i < 8; i++) {
587 err = snd_hda_codec_write(codec, pin_nid, 0,
588 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800589 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800591 snd_printdd(KERN_NOTICE
592 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800593 break;
594 }
595 }
596
597 hdmi_debug_channel_mapping(codec, pin_nid);
598}
599
Takashi Iwaid45e6882012-07-31 11:36:00 +0200600struct channel_map_table {
601 unsigned char map; /* ALSA API channel map position */
602 unsigned char cea_slot; /* CEA slot value */
603 int spk_mask; /* speaker position bit mask */
604};
605
606static struct channel_map_table map_tables[] = {
607 { SNDRV_CHMAP_FL, 0x00, FL },
608 { SNDRV_CHMAP_FR, 0x01, FR },
609 { SNDRV_CHMAP_RL, 0x04, RL },
610 { SNDRV_CHMAP_RR, 0x05, RR },
611 { SNDRV_CHMAP_LFE, 0x02, LFE },
612 { SNDRV_CHMAP_FC, 0x03, FC },
613 { SNDRV_CHMAP_RLC, 0x06, RLC },
614 { SNDRV_CHMAP_RRC, 0x07, RRC },
615 {} /* terminator */
616};
617
618/* from ALSA API channel position to speaker bit mask */
619static int to_spk_mask(unsigned char c)
620{
621 struct channel_map_table *t = map_tables;
622 for (; t->map; t++) {
623 if (t->map == c)
624 return t->spk_mask;
625 }
626 return 0;
627}
628
629/* from ALSA API channel position to CEA slot */
630static int to_cea_slot(unsigned char c)
631{
632 struct channel_map_table *t = map_tables;
633 for (; t->map; t++) {
634 if (t->map == c)
635 return t->cea_slot;
636 }
637 return 0x0f;
638}
639
640/* from CEA slot to ALSA API channel position */
641static int from_cea_slot(unsigned char c)
642{
643 struct channel_map_table *t = map_tables;
644 for (; t->map; t++) {
645 if (t->cea_slot == c)
646 return t->map;
647 }
648 return 0;
649}
650
651/* from speaker bit mask to ALSA API channel position */
652static int spk_to_chmap(int spk)
653{
654 struct channel_map_table *t = map_tables;
655 for (; t->map; t++) {
656 if (t->spk_mask == spk)
657 return t->map;
658 }
659 return 0;
660}
661
662/* get the CA index corresponding to the given ALSA API channel map */
663static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
664{
665 int i, spks = 0, spk_mask = 0;
666
667 for (i = 0; i < chs; i++) {
668 int mask = to_spk_mask(map[i]);
669 if (mask) {
670 spk_mask |= mask;
671 spks++;
672 }
673 }
674
675 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
676 if ((chs == channel_allocations[i].channels ||
677 spks == channel_allocations[i].channels) &&
678 (spk_mask & channel_allocations[i].spk_mask) ==
679 channel_allocations[i].spk_mask)
680 return channel_allocations[i].ca_index;
681 }
682 return -1;
683}
684
685/* set up the channel slots for the given ALSA API channel map */
686static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
687 hda_nid_t pin_nid,
688 int chs, unsigned char *map)
689{
690 int i;
691 for (i = 0; i < 8; i++) {
692 int val, err;
693 if (i < chs)
694 val = to_cea_slot(map[i]);
695 else
696 val = 0xf;
697 val |= (i << 4);
698 err = snd_hda_codec_write(codec, pin_nid, 0,
699 AC_VERB_SET_HDMI_CHAN_SLOT, val);
700 if (err)
701 return -EINVAL;
702 }
703 return 0;
704}
705
706/* store ALSA API channel map from the current default map */
707static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
708{
709 int i;
710 for (i = 0; i < 8; i++) {
711 if (i < channel_allocations[ca].channels)
712 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
713 else
714 map[i] = 0;
715 }
716}
717
718static void hdmi_setup_channel_mapping(struct hda_codec *codec,
719 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200720 int channels, unsigned char *map,
721 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200722{
Anssi Hannula20608732013-02-03 17:55:45 +0200723 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200724 hdmi_manual_setup_channel_mapping(codec, pin_nid,
725 channels, map);
726 } else {
727 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
728 hdmi_setup_fake_chmap(map, ca);
729 }
730}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800731
732/*
733 * Audio InfoFrame routines
734 */
735
736/*
737 * Enable Audio InfoFrame Transmission
738 */
739static void hdmi_start_infoframe_trans(struct hda_codec *codec,
740 hda_nid_t pin_nid)
741{
742 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
743 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
744 AC_DIPXMIT_BEST);
745}
746
747/*
748 * Disable Audio InfoFrame Transmission
749 */
750static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
751 hda_nid_t pin_nid)
752{
753 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
754 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
755 AC_DIPXMIT_DISABLE);
756}
757
758static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
759{
760#ifdef CONFIG_SND_DEBUG_VERBOSE
761 int i;
762 int size;
763
764 size = snd_hdmi_get_eld_size(codec, pin_nid);
765 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
766
767 for (i = 0; i < 8; i++) {
768 size = snd_hda_codec_read(codec, pin_nid, 0,
769 AC_VERB_GET_HDMI_DIP_SIZE, i);
770 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
771 }
772#endif
773}
774
775static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
776{
777#ifdef BE_PARANOID
778 int i, j;
779 int size;
780 int pi, bi;
781 for (i = 0; i < 8; i++) {
782 size = snd_hda_codec_read(codec, pin_nid, 0,
783 AC_VERB_GET_HDMI_DIP_SIZE, i);
784 if (size == 0)
785 continue;
786
787 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
788 for (j = 1; j < 1000; j++) {
789 hdmi_write_dip_byte(codec, pin_nid, 0x0);
790 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
791 if (pi != i)
792 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
793 bi, pi, i);
794 if (bi == 0) /* byte index wrapped around */
795 break;
796 }
797 snd_printd(KERN_INFO
798 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
799 i, size, j);
800 }
801#endif
802}
803
Wu Fengguang53d7d692010-09-21 14:25:49 +0800804static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800805{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800806 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800807 u8 sum = 0;
808 int i;
809
Wu Fengguang53d7d692010-09-21 14:25:49 +0800810 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800811
Wu Fengguang53d7d692010-09-21 14:25:49 +0800812 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800813 sum += bytes[i];
814
Wu Fengguang53d7d692010-09-21 14:25:49 +0800815 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800816}
817
818static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
819 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800820 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800821{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800822 int i;
823
824 hdmi_debug_dip_size(codec, pin_nid);
825 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
826
Wu Fengguang079d88c2010-03-08 10:44:23 +0800827 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800828 for (i = 0; i < size; i++)
829 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800830}
831
832static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800833 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800834{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800835 u8 val;
836 int i;
837
838 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
839 != AC_DIPXMIT_BEST)
840 return false;
841
842 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800843 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800844 val = snd_hda_codec_read(codec, pin_nid, 0,
845 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800846 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800847 return false;
848 }
849
850 return true;
851}
852
Stephen Warren384a48d2011-06-01 11:14:21 -0600853static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200854 bool non_pcm,
855 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856{
857 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600858 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
859 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800860 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600861 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800862 int ca;
Takashi Iwai2b203db2011-02-11 12:17:30 +0100863 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800864
Stephen Warren384a48d2011-06-01 11:14:21 -0600865 eld = &spec->pins[pin_idx].sink_eld;
866 if (!eld->monitor_present)
867 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800868
Takashi Iwaid45e6882012-07-31 11:36:00 +0200869 if (!non_pcm && per_pin->chmap_set)
870 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
871 else
872 ca = hdmi_channel_allocation(eld, channels);
873 if (ca < 0)
874 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800875
Stephen Warren384a48d2011-06-01 11:14:21 -0600876 memset(&ai, 0, sizeof(ai));
877 if (eld->conn_type == 0) { /* HDMI */
878 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800879
Stephen Warren384a48d2011-06-01 11:14:21 -0600880 hdmi_ai->type = 0x84;
881 hdmi_ai->ver = 0x01;
882 hdmi_ai->len = 0x0a;
883 hdmi_ai->CC02_CT47 = channels - 1;
884 hdmi_ai->CA = ca;
885 hdmi_checksum_audio_infoframe(hdmi_ai);
886 } else if (eld->conn_type == 1) { /* DisplayPort */
887 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800888
Stephen Warren384a48d2011-06-01 11:14:21 -0600889 dp_ai->type = 0x84;
890 dp_ai->len = 0x1b;
891 dp_ai->ver = 0x11 << 2;
892 dp_ai->CC02_CT47 = channels - 1;
893 dp_ai->CA = ca;
894 } else {
895 snd_printd("HDMI: unknown connection type at pin %d\n",
896 pin_nid);
897 return;
898 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800899
Stephen Warren384a48d2011-06-01 11:14:21 -0600900 /*
901 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
902 * sizeof(*dp_ai) to avoid partial match/update problems when
903 * the user switches between HDMI/DP monitors.
904 */
905 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
906 sizeof(ai))) {
907 snd_printdd("hdmi_setup_audio_infoframe: "
908 "pin=%d channels=%d\n",
909 pin_nid,
910 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200911 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200912 channels, per_pin->chmap,
913 per_pin->chmap_set);
Stephen Warren384a48d2011-06-01 11:14:21 -0600914 hdmi_stop_infoframe_trans(codec, pin_nid);
915 hdmi_fill_audio_infoframe(codec, pin_nid,
916 ai.bytes, sizeof(ai));
917 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800918 } else {
919 /* For non-pcm audio switch, setup new channel mapping
920 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200921 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200922 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200923 channels, per_pin->chmap,
924 per_pin->chmap_set);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800925 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800926
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200927 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800928}
929
930
931/*
932 * Unsolicited events
933 */
934
Wu Fengguangc6e84532011-11-18 16:59:32 -0600935static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200936
Wu Fengguang079d88c2010-03-08 10:44:23 +0800937static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
938{
939 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200940 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
941 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600942 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200943 struct hda_jack_tbl *jack;
944
945 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
946 if (!jack)
947 return;
948 pin_nid = jack->nid;
949 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800950
Fengguang Wufae3d882012-04-10 17:00:35 +0800951 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600952 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800953 codec->addr, pin_nid,
954 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800955
Stephen Warren384a48d2011-06-01 11:14:21 -0600956 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
957 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800958 return;
959
Wu Fengguangc6e84532011-11-18 16:59:32 -0600960 hdmi_present_sense(&spec->pins[pin_idx], 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200961 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800962}
963
964static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
965{
966 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
967 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
968 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
969 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
970
971 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200972 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600973 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800974 tag,
975 subtag,
976 cp_state,
977 cp_ready);
978
979 /* TODO */
980 if (cp_state)
981 ;
982 if (cp_ready)
983 ;
984}
985
986
987static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
988{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800989 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
990 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
991
Takashi Iwai3a938972011-10-28 01:16:55 +0200992 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800993 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
994 return;
995 }
996
997 if (subtag == 0)
998 hdmi_intrinsic_event(codec, res);
999 else
1000 hdmi_non_intrinsic_event(codec, res);
1001}
1002
1003/*
1004 * Callbacks
1005 */
1006
Takashi Iwai92f10b32010-08-03 14:21:00 +02001007/* HBR should be Non-PCM, 8 channels */
1008#define is_hbr_format(format) \
1009 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1010
Stephen Warren384a48d2011-06-01 11:14:21 -06001011static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1012 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001013{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001014 int pinctl;
1015 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001016
Stephen Warren384a48d2011-06-01 11:14:21 -06001017 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1018 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001019 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1020
1021 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001022 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001023 new_pinctl |= AC_PINCTL_EPT_HBR;
1024 else
1025 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1026
1027 snd_printdd("hdmi_setup_stream: "
1028 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001029 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001030 pinctl == new_pinctl ? "" : "new-",
1031 new_pinctl);
1032
1033 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001034 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001035 AC_VERB_SET_PIN_WIDGET_CONTROL,
1036 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001037
Stephen Warren384a48d2011-06-01 11:14:21 -06001038 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001039 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001040 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1041 return -EINVAL;
1042 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001043
Stephen Warren384a48d2011-06-01 11:14:21 -06001044 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001045 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001046}
1047
1048/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001049 * HDA PCM callbacks
1050 */
1051static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1052 struct hda_codec *codec,
1053 struct snd_pcm_substream *substream)
1054{
1055 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001056 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001057 int pin_idx, cvt_idx, mux_idx = 0;
1058 struct hdmi_spec_per_pin *per_pin;
1059 struct hdmi_eld *eld;
1060 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001061
Stephen Warren384a48d2011-06-01 11:14:21 -06001062 /* Validate hinfo */
1063 pin_idx = hinfo_to_pin_index(spec, hinfo);
1064 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001065 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -06001066 per_pin = &spec->pins[pin_idx];
1067 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001068
Stephen Warren384a48d2011-06-01 11:14:21 -06001069 /* Dynamically assign converter to stream */
1070 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1071 per_cvt = &spec->cvts[cvt_idx];
1072
1073 /* Must not already be assigned */
1074 if (per_cvt->assigned)
1075 continue;
1076 /* Must be in pin's mux's list of converters */
1077 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1078 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1079 break;
1080 /* Not in mux list */
1081 if (mux_idx == per_pin->num_mux_nids)
1082 continue;
1083 break;
1084 }
1085 /* No free converters */
1086 if (cvt_idx == spec->num_cvts)
1087 return -ENODEV;
1088
1089 /* Claim converter */
1090 per_cvt->assigned = 1;
1091 hinfo->nid = per_cvt->cvt_nid;
1092
1093 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1094 AC_VERB_SET_CONNECT_SEL,
1095 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001096 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001097
Stephen Warren2def8172011-06-01 11:14:20 -06001098 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001099 hinfo->channels_min = per_cvt->channels_min;
1100 hinfo->channels_max = per_cvt->channels_max;
1101 hinfo->rates = per_cvt->rates;
1102 hinfo->formats = per_cvt->formats;
1103 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001104
Stephen Warren384a48d2011-06-01 11:14:21 -06001105 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001106 if (!static_hdmi_pcm && eld->eld_valid) {
Stephen Warren2def8172011-06-01 11:14:20 -06001107 snd_hdmi_eld_update_pcm_info(eld, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001108 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001109 !hinfo->rates || !hinfo->formats) {
1110 per_cvt->assigned = 0;
1111 hinfo->nid = 0;
1112 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001113 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001114 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001115 }
Stephen Warren2def8172011-06-01 11:14:20 -06001116
1117 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001118 runtime->hw.channels_min = hinfo->channels_min;
1119 runtime->hw.channels_max = hinfo->channels_max;
1120 runtime->hw.formats = hinfo->formats;
1121 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001122
1123 snd_pcm_hw_constraint_step(substream->runtime, 0,
1124 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001125 return 0;
1126}
1127
1128/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001129 * HDA/HDMI auto parsing
1130 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001131static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001132{
1133 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001134 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1135 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001136
1137 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1138 snd_printk(KERN_WARNING
1139 "HDMI: pin %d wcaps %#x "
1140 "does not support connection list\n",
1141 pin_nid, get_wcaps(codec, pin_nid));
1142 return -EINVAL;
1143 }
1144
Stephen Warren384a48d2011-06-01 11:14:21 -06001145 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1146 per_pin->mux_nids,
1147 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001148
1149 return 0;
1150}
1151
Wu Fengguangc6e84532011-11-18 16:59:32 -06001152static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001153{
Wu Fengguang744626d2011-11-16 16:29:47 +08001154 struct hda_codec *codec = per_pin->codec;
1155 struct hdmi_eld *eld = &per_pin->sink_eld;
1156 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001157 /*
1158 * Always execute a GetPinSense verb here, even when called from
1159 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1160 * response's PD bit is not the real PD value, but indicates that
1161 * the real PD value changed. An older version of the HD-audio
1162 * specification worked this way. Hence, we just ignore the data in
1163 * the unsolicited response to avoid custom WARs.
1164 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001165 int present = snd_hda_pin_sense(codec, pin_nid);
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001166 bool eld_valid = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001167
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001168 memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001169
Stephen Warren5d44f922011-05-24 17:11:17 -06001170 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1171 if (eld->monitor_present)
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001172 eld_valid = !!(present & AC_PINSENSE_ELDV);
Stephen Warren5d44f922011-05-24 17:11:17 -06001173
Fengguang Wufae3d882012-04-10 17:00:35 +08001174 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001175 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001176 codec->addr, pin_nid, eld->monitor_present, eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001177
Wu Fengguang744626d2011-11-16 16:29:47 +08001178 if (eld_valid) {
Stephen Warren5d44f922011-05-24 17:11:17 -06001179 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
1180 snd_hdmi_show_eld(eld);
Wu Fengguangc6e84532011-11-18 16:59:32 -06001181 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001182 queue_delayed_work(codec->bus->workq,
1183 &per_pin->work,
1184 msecs_to_jiffies(300));
1185 }
1186 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001187}
1188
Wu Fengguang744626d2011-11-16 16:29:47 +08001189static void hdmi_repoll_eld(struct work_struct *work)
1190{
1191 struct hdmi_spec_per_pin *per_pin =
1192 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1193
Wu Fengguangc6e84532011-11-18 16:59:32 -06001194 if (per_pin->repoll_count++ > 6)
1195 per_pin->repoll_count = 0;
1196
1197 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001198}
1199
Wu Fengguang079d88c2010-03-08 10:44:23 +08001200static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1201{
1202 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001203 unsigned int caps, config;
1204 int pin_idx;
1205 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001206 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001207
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001208 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001209 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1210 return 0;
1211
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001212 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001213 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1214 return 0;
1215
1216 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001217 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001218
1219 pin_idx = spec->num_pins;
1220 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001221
1222 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001223 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001224
Stephen Warren384a48d2011-06-01 11:14:21 -06001225 err = hdmi_read_pin_conn(codec, pin_idx);
1226 if (err < 0)
1227 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001228
Wu Fengguang079d88c2010-03-08 10:44:23 +08001229 spec->num_pins++;
1230
Stephen Warren384a48d2011-06-01 11:14:21 -06001231 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001232}
1233
Stephen Warren384a48d2011-06-01 11:14:21 -06001234static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001235{
1236 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001237 int cvt_idx;
1238 struct hdmi_spec_per_cvt *per_cvt;
1239 unsigned int chans;
1240 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001241
David Henningsson116dcde2010-11-23 10:23:40 +01001242 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1243 return -E2BIG;
1244
Stephen Warren384a48d2011-06-01 11:14:21 -06001245 chans = get_wcaps(codec, cvt_nid);
1246 chans = get_wcaps_channels(chans);
1247
1248 cvt_idx = spec->num_cvts;
1249 per_cvt = &spec->cvts[cvt_idx];
1250
1251 per_cvt->cvt_nid = cvt_nid;
1252 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001253 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001254 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001255 if (chans > spec->channels_max)
1256 spec->channels_max = chans;
1257 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001258
1259 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1260 &per_cvt->rates,
1261 &per_cvt->formats,
1262 &per_cvt->maxbps);
1263 if (err < 0)
1264 return err;
1265
Wu Fengguang079d88c2010-03-08 10:44:23 +08001266 spec->num_cvts++;
1267
1268 return 0;
1269}
1270
1271static int hdmi_parse_codec(struct hda_codec *codec)
1272{
1273 hda_nid_t nid;
1274 int i, nodes;
1275
1276 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1277 if (!nid || nodes < 0) {
1278 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1279 return -EINVAL;
1280 }
1281
1282 for (i = 0; i < nodes; i++, nid++) {
1283 unsigned int caps;
1284 unsigned int type;
1285
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001286 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001287 type = get_wcaps_type(caps);
1288
1289 if (!(caps & AC_WCAP_DIGITAL))
1290 continue;
1291
1292 switch (type) {
1293 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001294 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001295 break;
1296 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001297 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001298 break;
1299 }
1300 }
1301
David Henningssonc9adeef2012-11-07 09:22:33 +01001302#ifdef CONFIG_PM
1303 /* We're seeing some problems with unsolicited hot plug events on
1304 * PantherPoint after S3, if this is not enabled */
1305 if (codec->vendor_id == 0x80862806)
1306 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001307 /*
1308 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1309 * can be lost and presence sense verb will become inaccurate if the
1310 * HDA link is powered off at hot plug or hw initialization time.
1311 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001312 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001313 AC_PWRST_EPSS))
1314 codec->bus->power_keep_link_on = 1;
1315#endif
1316
1317 return 0;
1318}
1319
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001320/*
1321 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001322static char *get_hdmi_pcm_name(int idx)
1323{
1324 static char names[MAX_HDMI_PINS][8];
1325 sprintf(&names[idx][0], "HDMI %d", idx);
1326 return &names[idx][0];
1327}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001328
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001329static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1330{
1331 struct hda_spdif_out *spdif;
1332 bool non_pcm;
1333
1334 mutex_lock(&codec->spdif_mutex);
1335 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1336 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1337 mutex_unlock(&codec->spdif_mutex);
1338 return non_pcm;
1339}
1340
1341
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001342/*
1343 * HDMI callbacks
1344 */
1345
1346static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1347 struct hda_codec *codec,
1348 unsigned int stream_tag,
1349 unsigned int format,
1350 struct snd_pcm_substream *substream)
1351{
Stephen Warren384a48d2011-06-01 11:14:21 -06001352 hda_nid_t cvt_nid = hinfo->nid;
1353 struct hdmi_spec *spec = codec->spec;
1354 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1355 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001356 bool non_pcm;
1357
1358 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001359
Stephen Warren384a48d2011-06-01 11:14:21 -06001360 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001361
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001362 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001363
1364 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001365}
1366
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001367static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1368 struct hda_codec *codec,
1369 struct snd_pcm_substream *substream)
1370{
1371 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1372 return 0;
1373}
1374
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001375static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1376 struct hda_codec *codec,
1377 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001378{
1379 struct hdmi_spec *spec = codec->spec;
1380 int cvt_idx, pin_idx;
1381 struct hdmi_spec_per_cvt *per_cvt;
1382 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001383
Stephen Warren384a48d2011-06-01 11:14:21 -06001384 if (hinfo->nid) {
1385 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1386 if (snd_BUG_ON(cvt_idx < 0))
1387 return -EINVAL;
1388 per_cvt = &spec->cvts[cvt_idx];
1389
1390 snd_BUG_ON(!per_cvt->assigned);
1391 per_cvt->assigned = 0;
1392 hinfo->nid = 0;
1393
1394 pin_idx = hinfo_to_pin_index(spec, hinfo);
1395 if (snd_BUG_ON(pin_idx < 0))
1396 return -EINVAL;
1397 per_pin = &spec->pins[pin_idx];
1398
Stephen Warren384a48d2011-06-01 11:14:21 -06001399 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001400 per_pin->chmap_set = false;
1401 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001402 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001403
Stephen Warren384a48d2011-06-01 11:14:21 -06001404 return 0;
1405}
1406
1407static const struct hda_pcm_ops generic_ops = {
1408 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001409 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001410 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001411 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001412};
1413
Takashi Iwaid45e6882012-07-31 11:36:00 +02001414/*
1415 * ALSA API channel-map control callbacks
1416 */
1417static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1418 struct snd_ctl_elem_info *uinfo)
1419{
1420 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1421 struct hda_codec *codec = info->private_data;
1422 struct hdmi_spec *spec = codec->spec;
1423 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1424 uinfo->count = spec->channels_max;
1425 uinfo->value.integer.min = 0;
1426 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1427 return 0;
1428}
1429
1430static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1431 unsigned int size, unsigned int __user *tlv)
1432{
1433 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1434 struct hda_codec *codec = info->private_data;
1435 struct hdmi_spec *spec = codec->spec;
1436 const unsigned int valid_mask =
1437 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1438 unsigned int __user *dst;
1439 int chs, count = 0;
1440
1441 if (size < 8)
1442 return -ENOMEM;
1443 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1444 return -EFAULT;
1445 size -= 8;
1446 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001447 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001448 int i, c;
1449 struct cea_channel_speaker_allocation *cap;
1450 cap = channel_allocations;
1451 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1452 int chs_bytes = chs * 4;
1453 if (cap->channels != chs)
1454 continue;
1455 if (cap->spk_mask & ~valid_mask)
1456 continue;
1457 if (size < 8)
1458 return -ENOMEM;
1459 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1460 put_user(chs_bytes, dst + 1))
1461 return -EFAULT;
1462 dst += 2;
1463 size -= 8;
1464 count += 8;
1465 if (size < chs_bytes)
1466 return -ENOMEM;
1467 size -= chs_bytes;
1468 count += chs_bytes;
1469 for (c = 7; c >= 0; c--) {
1470 int spk = cap->speakers[c];
1471 if (!spk)
1472 continue;
1473 if (put_user(spk_to_chmap(spk), dst))
1474 return -EFAULT;
1475 dst++;
1476 }
1477 }
1478 }
1479 if (put_user(count, tlv + 1))
1480 return -EFAULT;
1481 return 0;
1482}
1483
1484static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_value *ucontrol)
1486{
1487 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1488 struct hda_codec *codec = info->private_data;
1489 struct hdmi_spec *spec = codec->spec;
1490 int pin_idx = kcontrol->private_value;
1491 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1492 int i;
1493
1494 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1495 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1496 return 0;
1497}
1498
1499static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_value *ucontrol)
1501{
1502 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1503 struct hda_codec *codec = info->private_data;
1504 struct hdmi_spec *spec = codec->spec;
1505 int pin_idx = kcontrol->private_value;
1506 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1507 unsigned int ctl_idx;
1508 struct snd_pcm_substream *substream;
1509 unsigned char chmap[8];
1510 int i, ca, prepared = 0;
1511
1512 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1513 substream = snd_pcm_chmap_substream(info, ctl_idx);
1514 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001515 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001516 switch (substream->runtime->status->state) {
1517 case SNDRV_PCM_STATE_OPEN:
1518 case SNDRV_PCM_STATE_SETUP:
1519 break;
1520 case SNDRV_PCM_STATE_PREPARED:
1521 prepared = 1;
1522 break;
1523 default:
1524 return -EBUSY;
1525 }
1526 memset(chmap, 0, sizeof(chmap));
1527 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1528 chmap[i] = ucontrol->value.integer.value[i];
1529 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1530 return 0;
1531 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1532 if (ca < 0)
1533 return -EINVAL;
1534 per_pin->chmap_set = true;
1535 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1536 if (prepared)
1537 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1538 substream);
1539
1540 return 0;
1541}
1542
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001543static int generic_hdmi_build_pcms(struct hda_codec *codec)
1544{
1545 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001546 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001547
Stephen Warren384a48d2011-06-01 11:14:21 -06001548 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1549 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001550 struct hda_pcm_stream *pstr;
1551
Stephen Warren384a48d2011-06-01 11:14:21 -06001552 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001553 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001554 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001555 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001556
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001557 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001558 pstr->substreams = 1;
1559 pstr->ops = generic_ops;
1560 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001561 }
1562
Stephen Warren384a48d2011-06-01 11:14:21 -06001563 codec->num_pcms = spec->num_pins;
1564 codec->pcm_info = spec->pcm_rec;
1565
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001566 return 0;
1567}
1568
David Henningsson0b6c49b2011-08-23 16:56:03 +02001569static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1570{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001571 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001572 struct hdmi_spec *spec = codec->spec;
1573 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1574 int pcmdev = spec->pcm_rec[pin_idx].device;
1575
Takashi Iwai31ef2252011-12-01 17:41:36 +01001576 if (pcmdev > 0)
1577 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001578
Takashi Iwai31ef2252011-12-01 17:41:36 +01001579 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001580}
1581
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001582static int generic_hdmi_build_controls(struct hda_codec *codec)
1583{
1584 struct hdmi_spec *spec = codec->spec;
1585 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001586 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001587
Stephen Warren384a48d2011-06-01 11:14:21 -06001588 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1589 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001590
1591 err = generic_hdmi_build_jack(codec, pin_idx);
1592 if (err < 0)
1593 return err;
1594
Takashi Iwaidcda5802012-10-12 17:24:51 +02001595 err = snd_hda_create_dig_out_ctls(codec,
1596 per_pin->pin_nid,
1597 per_pin->mux_nids[0],
1598 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001599 if (err < 0)
1600 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001601 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001602
1603 /* add control for ELD Bytes */
1604 err = hdmi_create_eld_ctl(codec,
1605 pin_idx,
1606 spec->pcm_rec[pin_idx].device);
1607
1608 if (err < 0)
1609 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001610
Takashi Iwai82b1d732011-12-20 15:53:07 +01001611 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001612 }
1613
Takashi Iwaid45e6882012-07-31 11:36:00 +02001614 /* add channel maps */
1615 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1616 struct snd_pcm_chmap *chmap;
1617 struct snd_kcontrol *kctl;
1618 int i;
1619 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1620 SNDRV_PCM_STREAM_PLAYBACK,
1621 NULL, 0, pin_idx, &chmap);
1622 if (err < 0)
1623 return err;
1624 /* override handlers */
1625 chmap->private_data = codec;
1626 kctl = chmap->kctl;
1627 for (i = 0; i < kctl->count; i++)
1628 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1629 kctl->info = hdmi_chmap_ctl_info;
1630 kctl->get = hdmi_chmap_ctl_get;
1631 kctl->put = hdmi_chmap_ctl_put;
1632 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1633 }
1634
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001635 return 0;
1636}
1637
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001638static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1639{
1640 struct hdmi_spec *spec = codec->spec;
1641 int pin_idx;
1642
1643 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1644 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1645 struct hdmi_eld *eld = &per_pin->sink_eld;
1646
1647 per_pin->codec = codec;
1648 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1649 snd_hda_eld_proc_new(codec, eld, pin_idx);
1650 }
1651 return 0;
1652}
1653
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001654static int generic_hdmi_init(struct hda_codec *codec)
1655{
1656 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001657 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001658
Stephen Warren384a48d2011-06-01 11:14:21 -06001659 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1660 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1661 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001662
1663 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001664 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001665 }
1666 return 0;
1667}
1668
1669static void generic_hdmi_free(struct hda_codec *codec)
1670{
1671 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001672 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001673
Stephen Warren384a48d2011-06-01 11:14:21 -06001674 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1675 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1676 struct hdmi_eld *eld = &per_pin->sink_eld;
1677
Wu Fengguang744626d2011-11-16 16:29:47 +08001678 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001679 snd_hda_eld_proc_free(codec, eld);
1680 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001681
Wu Fengguang744626d2011-11-16 16:29:47 +08001682 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001683 kfree(spec);
1684}
1685
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001686static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001687 .init = generic_hdmi_init,
1688 .free = generic_hdmi_free,
1689 .build_pcms = generic_hdmi_build_pcms,
1690 .build_controls = generic_hdmi_build_controls,
1691 .unsol_event = hdmi_unsol_event,
1692};
1693
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001694static void intel_haswell_fixup_connect_list(struct hda_codec *codec)
1695{
1696 unsigned int vendor_param;
1697 hda_nid_t list[3] = {0x2, 0x3, 0x4};
1698
1699 vendor_param = snd_hda_codec_read(codec, 0x08, 0, 0xf81, 0);
1700 if (vendor_param == -1 || vendor_param & 0x02)
1701 return;
1702
1703 /* enable DP1.2 mode */
1704 vendor_param |= 0x02;
1705 snd_hda_codec_read(codec, 0x08, 0, 0x781, vendor_param);
1706
1707 vendor_param = snd_hda_codec_read(codec, 0x08, 0, 0xf81, 0);
1708 if (vendor_param == -1 || !(vendor_param & 0x02))
1709 return;
1710
1711 /* override 3 pins connection list */
1712 snd_hda_override_conn_list(codec, 0x05, 3, list);
1713 snd_hda_override_conn_list(codec, 0x06, 3, list);
1714 snd_hda_override_conn_list(codec, 0x07, 3, list);
1715}
1716
1717
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001718static int patch_generic_hdmi(struct hda_codec *codec)
1719{
1720 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001721
1722 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1723 if (spec == NULL)
1724 return -ENOMEM;
1725
1726 codec->spec = spec;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001727
1728 if (codec->vendor_id == 0x80862807)
1729 intel_haswell_fixup_connect_list(codec);
1730
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001731 if (hdmi_parse_codec(codec) < 0) {
1732 codec->spec = NULL;
1733 kfree(spec);
1734 return -EINVAL;
1735 }
1736 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001737 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001738
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001739 init_channel_allocations();
1740
1741 return 0;
1742}
1743
1744/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001745 * Shared non-generic implementations
1746 */
1747
1748static int simple_playback_build_pcms(struct hda_codec *codec)
1749{
1750 struct hdmi_spec *spec = codec->spec;
1751 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001752 unsigned int chans;
1753 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001754
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001755 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001756 codec->pcm_info = info;
1757
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001758 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1759 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001760
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001761 info->name = get_hdmi_pcm_name(0);
1762 info->pcm_type = HDA_PCM_TYPE_HDMI;
1763 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1764 *pstr = spec->pcm_playback;
1765 pstr->nid = spec->cvts[0].cvt_nid;
1766 if (pstr->channels_max <= 2 && chans && chans <= 16)
1767 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001768
1769 return 0;
1770}
1771
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001772/* unsolicited event for jack sensing */
1773static void simple_hdmi_unsol_event(struct hda_codec *codec,
1774 unsigned int res)
1775{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001776 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001777 snd_hda_jack_report_sync(codec);
1778}
1779
1780/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1781 * as long as spec->pins[] is set correctly
1782 */
1783#define simple_hdmi_build_jack generic_hdmi_build_jack
1784
Stephen Warren3aaf8982011-06-01 11:14:19 -06001785static int simple_playback_build_controls(struct hda_codec *codec)
1786{
1787 struct hdmi_spec *spec = codec->spec;
1788 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001789
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001790 err = snd_hda_create_spdif_out_ctls(codec,
1791 spec->cvts[0].cvt_nid,
1792 spec->cvts[0].cvt_nid);
1793 if (err < 0)
1794 return err;
1795 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001796}
1797
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001798static int simple_playback_init(struct hda_codec *codec)
1799{
1800 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001801 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001802
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001803 snd_hda_codec_write(codec, pin, 0,
1804 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1805 /* some codecs require to unmute the pin */
1806 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1807 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1808 AMP_OUT_UNMUTE);
1809 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001810 return 0;
1811}
1812
Stephen Warren3aaf8982011-06-01 11:14:19 -06001813static void simple_playback_free(struct hda_codec *codec)
1814{
1815 struct hdmi_spec *spec = codec->spec;
1816
1817 kfree(spec);
1818}
1819
1820/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001821 * Nvidia specific implementations
1822 */
1823
1824#define Nv_VERB_SET_Channel_Allocation 0xF79
1825#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1826#define Nv_VERB_SET_Audio_Protection_On 0xF98
1827#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1828
1829#define nvhdmi_master_con_nid_7x 0x04
1830#define nvhdmi_master_pin_nid_7x 0x05
1831
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001832static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001833 /*front, rear, clfe, rear_surr */
1834 0x6, 0x8, 0xa, 0xc,
1835};
1836
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001837static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1838 /* set audio protect on */
1839 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1840 /* enable digital output on pin widget */
1841 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1842 {} /* terminator */
1843};
1844
1845static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001846 /* set audio protect on */
1847 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1848 /* enable digital output on pin widget */
1849 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1850 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1851 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1852 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1853 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1854 {} /* terminator */
1855};
1856
1857#ifdef LIMITED_RATE_FMT_SUPPORT
1858/* support only the safe format and rate */
1859#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1860#define SUPPORTED_MAXBPS 16
1861#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1862#else
1863/* support all rates and formats */
1864#define SUPPORTED_RATES \
1865 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1866 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1867 SNDRV_PCM_RATE_192000)
1868#define SUPPORTED_MAXBPS 24
1869#define SUPPORTED_FORMATS \
1870 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1871#endif
1872
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001873static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001874{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001875 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1876 return 0;
1877}
1878
1879static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
1880{
1881 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001882 return 0;
1883}
1884
Nitin Daga393004b2011-01-10 21:49:31 +05301885static unsigned int channels_2_6_8[] = {
1886 2, 6, 8
1887};
1888
1889static unsigned int channels_2_8[] = {
1890 2, 8
1891};
1892
1893static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
1894 .count = ARRAY_SIZE(channels_2_6_8),
1895 .list = channels_2_6_8,
1896 .mask = 0,
1897};
1898
1899static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
1900 .count = ARRAY_SIZE(channels_2_8),
1901 .list = channels_2_8,
1902 .mask = 0,
1903};
1904
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001905static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
1906 struct hda_codec *codec,
1907 struct snd_pcm_substream *substream)
1908{
1909 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05301910 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
1911
1912 switch (codec->preset->id) {
1913 case 0x10de0002:
1914 case 0x10de0003:
1915 case 0x10de0005:
1916 case 0x10de0006:
1917 hw_constraints_channels = &hw_constraints_2_8_channels;
1918 break;
1919 case 0x10de0007:
1920 hw_constraints_channels = &hw_constraints_2_6_8_channels;
1921 break;
1922 default:
1923 break;
1924 }
1925
1926 if (hw_constraints_channels != NULL) {
1927 snd_pcm_hw_constraint_list(substream->runtime, 0,
1928 SNDRV_PCM_HW_PARAM_CHANNELS,
1929 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01001930 } else {
1931 snd_pcm_hw_constraint_step(substream->runtime, 0,
1932 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05301933 }
1934
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001935 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1936}
1937
1938static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
1939 struct hda_codec *codec,
1940 struct snd_pcm_substream *substream)
1941{
1942 struct hdmi_spec *spec = codec->spec;
1943 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1944}
1945
1946static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1947 struct hda_codec *codec,
1948 unsigned int stream_tag,
1949 unsigned int format,
1950 struct snd_pcm_substream *substream)
1951{
1952 struct hdmi_spec *spec = codec->spec;
1953 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1954 stream_tag, format, substream);
1955}
1956
Takashi Iwaid0b12522012-06-15 14:34:42 +02001957static const struct hda_pcm_stream simple_pcm_playback = {
1958 .substreams = 1,
1959 .channels_min = 2,
1960 .channels_max = 2,
1961 .ops = {
1962 .open = simple_playback_pcm_open,
1963 .close = simple_playback_pcm_close,
1964 .prepare = simple_playback_pcm_prepare
1965 },
1966};
1967
1968static const struct hda_codec_ops simple_hdmi_patch_ops = {
1969 .build_controls = simple_playback_build_controls,
1970 .build_pcms = simple_playback_build_pcms,
1971 .init = simple_playback_init,
1972 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02001973 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02001974};
1975
1976static int patch_simple_hdmi(struct hda_codec *codec,
1977 hda_nid_t cvt_nid, hda_nid_t pin_nid)
1978{
1979 struct hdmi_spec *spec;
1980
1981 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1982 if (!spec)
1983 return -ENOMEM;
1984
1985 codec->spec = spec;
1986
1987 spec->multiout.num_dacs = 0; /* no analog */
1988 spec->multiout.max_channels = 2;
1989 spec->multiout.dig_out_nid = cvt_nid;
1990 spec->num_cvts = 1;
1991 spec->num_pins = 1;
1992 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02001993 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001994 spec->pcm_playback = simple_pcm_playback;
1995
1996 codec->patch_ops = simple_hdmi_patch_ops;
1997
1998 return 0;
1999}
2000
Aaron Plattner1f348522011-04-06 17:19:04 -07002001static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2002 int channels)
2003{
2004 unsigned int chanmask;
2005 int chan = channels ? (channels - 1) : 1;
2006
2007 switch (channels) {
2008 default:
2009 case 0:
2010 case 2:
2011 chanmask = 0x00;
2012 break;
2013 case 4:
2014 chanmask = 0x08;
2015 break;
2016 case 6:
2017 chanmask = 0x0b;
2018 break;
2019 case 8:
2020 chanmask = 0x13;
2021 break;
2022 }
2023
2024 /* Set the audio infoframe channel allocation and checksum fields. The
2025 * channel count is computed implicitly by the hardware. */
2026 snd_hda_codec_write(codec, 0x1, 0,
2027 Nv_VERB_SET_Channel_Allocation, chanmask);
2028
2029 snd_hda_codec_write(codec, 0x1, 0,
2030 Nv_VERB_SET_Info_Frame_Checksum,
2031 (0x71 - chan - chanmask));
2032}
2033
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002034static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2035 struct hda_codec *codec,
2036 struct snd_pcm_substream *substream)
2037{
2038 struct hdmi_spec *spec = codec->spec;
2039 int i;
2040
2041 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2042 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2043 for (i = 0; i < 4; i++) {
2044 /* set the stream id */
2045 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2046 AC_VERB_SET_CHANNEL_STREAMID, 0);
2047 /* set the stream format */
2048 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2049 AC_VERB_SET_STREAM_FORMAT, 0);
2050 }
2051
Aaron Plattner1f348522011-04-06 17:19:04 -07002052 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2053 * streams are disabled. */
2054 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2055
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002056 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2057}
2058
2059static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2060 struct hda_codec *codec,
2061 unsigned int stream_tag,
2062 unsigned int format,
2063 struct snd_pcm_substream *substream)
2064{
2065 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002066 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002067 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002068 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002069 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002070
2071 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002072 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002073
2074 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002075
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002076 dataDCC2 = 0x2;
2077
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002078 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002079 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002080 snd_hda_codec_write(codec,
2081 nvhdmi_master_con_nid_7x,
2082 0,
2083 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002084 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002085
2086 /* set the stream id */
2087 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2088 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2089
2090 /* set the stream format */
2091 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2092 AC_VERB_SET_STREAM_FORMAT, format);
2093
2094 /* turn on again (if needed) */
2095 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002096 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002097 snd_hda_codec_write(codec,
2098 nvhdmi_master_con_nid_7x,
2099 0,
2100 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002101 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002102 snd_hda_codec_write(codec,
2103 nvhdmi_master_con_nid_7x,
2104 0,
2105 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2106 }
2107
2108 for (i = 0; i < 4; i++) {
2109 if (chs == 2)
2110 channel_id = 0;
2111 else
2112 channel_id = i * 2;
2113
2114 /* turn off SPDIF once;
2115 *otherwise the IEC958 bits won't be updated
2116 */
2117 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002118 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002119 snd_hda_codec_write(codec,
2120 nvhdmi_con_nids_7x[i],
2121 0,
2122 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002123 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002124 /* set the stream id */
2125 snd_hda_codec_write(codec,
2126 nvhdmi_con_nids_7x[i],
2127 0,
2128 AC_VERB_SET_CHANNEL_STREAMID,
2129 (stream_tag << 4) | channel_id);
2130 /* set the stream format */
2131 snd_hda_codec_write(codec,
2132 nvhdmi_con_nids_7x[i],
2133 0,
2134 AC_VERB_SET_STREAM_FORMAT,
2135 format);
2136 /* turn on again (if needed) */
2137 /* enable and set the channel status audio/data flag */
2138 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002139 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002140 snd_hda_codec_write(codec,
2141 nvhdmi_con_nids_7x[i],
2142 0,
2143 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002144 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002145 snd_hda_codec_write(codec,
2146 nvhdmi_con_nids_7x[i],
2147 0,
2148 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2149 }
2150 }
2151
Aaron Plattner1f348522011-04-06 17:19:04 -07002152 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002153
2154 mutex_unlock(&codec->spdif_mutex);
2155 return 0;
2156}
2157
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002158static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002159 .substreams = 1,
2160 .channels_min = 2,
2161 .channels_max = 8,
2162 .nid = nvhdmi_master_con_nid_7x,
2163 .rates = SUPPORTED_RATES,
2164 .maxbps = SUPPORTED_MAXBPS,
2165 .formats = SUPPORTED_FORMATS,
2166 .ops = {
2167 .open = simple_playback_pcm_open,
2168 .close = nvhdmi_8ch_7x_pcm_close,
2169 .prepare = nvhdmi_8ch_7x_pcm_prepare
2170 },
2171};
2172
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002173static int patch_nvhdmi_2ch(struct hda_codec *codec)
2174{
2175 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002176 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2177 nvhdmi_master_pin_nid_7x);
2178 if (err < 0)
2179 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002180
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002181 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002182 /* override the PCM rates, etc, as the codec doesn't give full list */
2183 spec = codec->spec;
2184 spec->pcm_playback.rates = SUPPORTED_RATES;
2185 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2186 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002187 return 0;
2188}
2189
Takashi Iwai53775b02012-08-01 12:17:41 +02002190static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2191{
2192 struct hdmi_spec *spec = codec->spec;
2193 int err = simple_playback_build_pcms(codec);
2194 spec->pcm_rec[0].own_chmap = true;
2195 return err;
2196}
2197
2198static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2199{
2200 struct hdmi_spec *spec = codec->spec;
2201 struct snd_pcm_chmap *chmap;
2202 int err;
2203
2204 err = simple_playback_build_controls(codec);
2205 if (err < 0)
2206 return err;
2207
2208 /* add channel maps */
2209 err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
2210 SNDRV_PCM_STREAM_PLAYBACK,
2211 snd_pcm_alt_chmaps, 8, 0, &chmap);
2212 if (err < 0)
2213 return err;
2214 switch (codec->preset->id) {
2215 case 0x10de0002:
2216 case 0x10de0003:
2217 case 0x10de0005:
2218 case 0x10de0006:
2219 chmap->channel_mask = (1U << 2) | (1U << 8);
2220 break;
2221 case 0x10de0007:
2222 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2223 }
2224 return 0;
2225}
2226
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002227static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2228{
2229 struct hdmi_spec *spec;
2230 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002231 if (err < 0)
2232 return err;
2233 spec = codec->spec;
2234 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002235 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002236 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002237 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2238 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002239
2240 /* Initialize the audio infoframe channel mask and checksum to something
2241 * valid */
2242 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2243
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002244 return 0;
2245}
2246
2247/*
2248 * ATI-specific implementations
2249 *
2250 * FIXME: we may omit the whole this and use the generic code once after
2251 * it's confirmed to work.
2252 */
2253
2254#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2255#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2256
2257static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2258 struct hda_codec *codec,
2259 unsigned int stream_tag,
2260 unsigned int format,
2261 struct snd_pcm_substream *substream)
2262{
2263 struct hdmi_spec *spec = codec->spec;
2264 int chans = substream->runtime->channels;
2265 int i, err;
2266
2267 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2268 substream);
2269 if (err < 0)
2270 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002271 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
2272 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002273 /* FIXME: XXX */
2274 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002275 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002276 AC_VERB_SET_HDMI_CHAN_SLOT,
2277 (i << 4) | i);
2278 }
2279 return 0;
2280}
2281
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002282static int patch_atihdmi(struct hda_codec *codec)
2283{
2284 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002285 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2286 if (err < 0)
2287 return err;
2288 spec = codec->spec;
2289 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002290 return 0;
2291}
2292
Annie Liu3de5ff82012-06-08 19:18:42 +08002293/* VIA HDMI Implementation */
2294#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2295#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2296
Annie Liu3de5ff82012-06-08 19:18:42 +08002297static int patch_via_hdmi(struct hda_codec *codec)
2298{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002299 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002300}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002301
2302/*
2303 * patch entries
2304 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002305static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002306{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2307{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2308{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002309{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002310{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2311{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2312{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2313{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2314{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2315{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2316{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2317{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002318{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2319{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2320{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2321{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2322{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2323{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2324{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2325{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2326{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2327{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2328{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002329/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002330{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2331{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2332{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2333{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2334{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2335{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2336{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2337{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2338{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2339{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002340{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002341{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2342{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002343{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2344{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2345{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2346{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002347{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2348{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2349{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2350{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2351{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2352{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002353{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002354{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002355{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002356{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2357{} /* terminator */
2358};
2359
2360MODULE_ALIAS("snd-hda-codec-id:1002793c");
2361MODULE_ALIAS("snd-hda-codec-id:10027919");
2362MODULE_ALIAS("snd-hda-codec-id:1002791a");
2363MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2364MODULE_ALIAS("snd-hda-codec-id:10951390");
2365MODULE_ALIAS("snd-hda-codec-id:10951392");
2366MODULE_ALIAS("snd-hda-codec-id:10de0002");
2367MODULE_ALIAS("snd-hda-codec-id:10de0003");
2368MODULE_ALIAS("snd-hda-codec-id:10de0005");
2369MODULE_ALIAS("snd-hda-codec-id:10de0006");
2370MODULE_ALIAS("snd-hda-codec-id:10de0007");
2371MODULE_ALIAS("snd-hda-codec-id:10de000a");
2372MODULE_ALIAS("snd-hda-codec-id:10de000b");
2373MODULE_ALIAS("snd-hda-codec-id:10de000c");
2374MODULE_ALIAS("snd-hda-codec-id:10de000d");
2375MODULE_ALIAS("snd-hda-codec-id:10de0010");
2376MODULE_ALIAS("snd-hda-codec-id:10de0011");
2377MODULE_ALIAS("snd-hda-codec-id:10de0012");
2378MODULE_ALIAS("snd-hda-codec-id:10de0013");
2379MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002380MODULE_ALIAS("snd-hda-codec-id:10de0015");
2381MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002382MODULE_ALIAS("snd-hda-codec-id:10de0018");
2383MODULE_ALIAS("snd-hda-codec-id:10de0019");
2384MODULE_ALIAS("snd-hda-codec-id:10de001a");
2385MODULE_ALIAS("snd-hda-codec-id:10de001b");
2386MODULE_ALIAS("snd-hda-codec-id:10de001c");
2387MODULE_ALIAS("snd-hda-codec-id:10de0040");
2388MODULE_ALIAS("snd-hda-codec-id:10de0041");
2389MODULE_ALIAS("snd-hda-codec-id:10de0042");
2390MODULE_ALIAS("snd-hda-codec-id:10de0043");
2391MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002392MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002393MODULE_ALIAS("snd-hda-codec-id:10de0067");
2394MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002395MODULE_ALIAS("snd-hda-codec-id:11069f80");
2396MODULE_ALIAS("snd-hda-codec-id:11069f81");
2397MODULE_ALIAS("snd-hda-codec-id:11069f84");
2398MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002399MODULE_ALIAS("snd-hda-codec-id:17e80047");
2400MODULE_ALIAS("snd-hda-codec-id:80860054");
2401MODULE_ALIAS("snd-hda-codec-id:80862801");
2402MODULE_ALIAS("snd-hda-codec-id:80862802");
2403MODULE_ALIAS("snd-hda-codec-id:80862803");
2404MODULE_ALIAS("snd-hda-codec-id:80862804");
2405MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002406MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002407MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002408MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002409MODULE_ALIAS("snd-hda-codec-id:808629fb");
2410
2411MODULE_LICENSE("GPL");
2412MODULE_DESCRIPTION("HDMI HD-audio codec");
2413MODULE_ALIAS("snd-hda-codec-intelhdmi");
2414MODULE_ALIAS("snd-hda-codec-nvhdmi");
2415MODULE_ALIAS("snd-hda-codec-atihdmi");
2416
2417static struct hda_codec_preset_list intel_list = {
2418 .preset = snd_hda_preset_hdmi,
2419 .owner = THIS_MODULE,
2420};
2421
2422static int __init patch_hdmi_init(void)
2423{
2424 return snd_hda_add_codec_preset(&intel_list);
2425}
2426
2427static void __exit patch_hdmi_exit(void)
2428{
2429 snd_hda_delete_codec_preset(&intel_list);
2430}
2431
2432module_init(patch_hdmi_init)
2433module_exit(patch_hdmi_exit)