blob: 5361298be4d09e8d9a1646cd223964b97a271981 [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 Iwai84eb01b2010-09-07 12:27:25 +020038#include "hda_codec.h"
39#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020040#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020041
Takashi Iwai0ebaa242011-01-11 18:11:04 +010042static bool static_hdmi_pcm;
43module_param(static_hdmi_pcm, bool, 0644);
44MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
45
Takashi Iwai84eb01b2010-09-07 12:27:25 +020046/*
47 * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
Stephen Warren384a48d2011-06-01 11:14:21 -060048 * could support N independent pipes, each of them can be connected to one or
Takashi Iwai84eb01b2010-09-07 12:27:25 +020049 * more ports (DVI, HDMI or DisplayPort).
50 *
51 * The HDA correspondence of pipes/ports are converter/pin nodes.
52 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +010053#define MAX_HDMI_CVTS 8
54#define MAX_HDMI_PINS 8
Wu Fengguang079d88c2010-03-08 10:44:23 +080055
Stephen Warren384a48d2011-06-01 11:14:21 -060056struct hdmi_spec_per_cvt {
57 hda_nid_t cvt_nid;
58 int assigned;
59 unsigned int channels_min;
60 unsigned int channels_max;
61 u32 rates;
62 u64 formats;
63 unsigned int maxbps;
Wang Xingchao433968d2012-09-06 10:02:37 +080064 bool non_pcm;
Stephen Warren384a48d2011-06-01 11:14:21 -060065};
66
67struct hdmi_spec_per_pin {
68 hda_nid_t pin_nid;
69 int num_mux_nids;
70 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Wu Fengguang744626d2011-11-16 16:29:47 +080071
72 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060073 struct hdmi_eld sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +080074 struct delayed_work work;
Wu Fengguangc6e84532011-11-18 16:59:32 -060075 int repoll_count;
Stephen Warren384a48d2011-06-01 11:14:21 -060076};
77
Wu Fengguang079d88c2010-03-08 10:44:23 +080078struct hdmi_spec {
79 int num_cvts;
Stephen Warren384a48d2011-06-01 11:14:21 -060080 struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
81
Wu Fengguang079d88c2010-03-08 10:44:23 +080082 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060083 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
84 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Wu Fengguang079d88c2010-03-08 10:44:23 +080085
86 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060087 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080088 */
89 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020090 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080091};
92
93
94struct hdmi_audio_infoframe {
95 u8 type; /* 0x84 */
96 u8 ver; /* 0x01 */
97 u8 len; /* 0x0a */
98
Wu Fengguang53d7d692010-09-21 14:25:49 +080099 u8 checksum;
100
Wu Fengguang079d88c2010-03-08 10:44:23 +0800101 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
102 u8 SS01_SF24;
103 u8 CXT04;
104 u8 CA;
105 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800106};
107
108struct dp_audio_infoframe {
109 u8 type; /* 0x84 */
110 u8 len; /* 0x1b */
111 u8 ver; /* 0x11 << 2 */
112
113 u8 CC02_CT47; /* match with HDMI infoframe from this on */
114 u8 SS01_SF24;
115 u8 CXT04;
116 u8 CA;
117 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800118};
119
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100120union audio_infoframe {
121 struct hdmi_audio_infoframe hdmi;
122 struct dp_audio_infoframe dp;
123 u8 bytes[0];
124};
125
Wu Fengguang079d88c2010-03-08 10:44:23 +0800126/*
127 * CEA speaker placement:
128 *
129 * FLH FCH FRH
130 * FLW FL FLC FC FRC FR FRW
131 *
132 * LFE
133 * TC
134 *
135 * RL RLC RC RRC RR
136 *
137 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
138 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
139 */
140enum cea_speaker_placement {
141 FL = (1 << 0), /* Front Left */
142 FC = (1 << 1), /* Front Center */
143 FR = (1 << 2), /* Front Right */
144 FLC = (1 << 3), /* Front Left Center */
145 FRC = (1 << 4), /* Front Right Center */
146 RL = (1 << 5), /* Rear Left */
147 RC = (1 << 6), /* Rear Center */
148 RR = (1 << 7), /* Rear Right */
149 RLC = (1 << 8), /* Rear Left Center */
150 RRC = (1 << 9), /* Rear Right Center */
151 LFE = (1 << 10), /* Low Frequency Effect */
152 FLW = (1 << 11), /* Front Left Wide */
153 FRW = (1 << 12), /* Front Right Wide */
154 FLH = (1 << 13), /* Front Left High */
155 FCH = (1 << 14), /* Front Center High */
156 FRH = (1 << 15), /* Front Right High */
157 TC = (1 << 16), /* Top Center */
158};
159
160/*
161 * ELD SA bits in the CEA Speaker Allocation data block
162 */
163static int eld_speaker_allocation_bits[] = {
164 [0] = FL | FR,
165 [1] = LFE,
166 [2] = FC,
167 [3] = RL | RR,
168 [4] = RC,
169 [5] = FLC | FRC,
170 [6] = RLC | RRC,
171 /* the following are not defined in ELD yet */
172 [7] = FLW | FRW,
173 [8] = FLH | FRH,
174 [9] = TC,
175 [10] = FCH,
176};
177
178struct cea_channel_speaker_allocation {
179 int ca_index;
180 int speakers[8];
181
182 /* derived values, just for convenience */
183 int channels;
184 int spk_mask;
185};
186
187/*
188 * ALSA sequence is:
189 *
190 * surround40 surround41 surround50 surround51 surround71
191 * ch0 front left = = = =
192 * ch1 front right = = = =
193 * ch2 rear left = = = =
194 * ch3 rear right = = = =
195 * ch4 LFE center center center
196 * ch5 LFE LFE
197 * ch6 side left
198 * ch7 side right
199 *
200 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
201 */
202static int hdmi_channel_mapping[0x32][8] = {
203 /* stereo */
204 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
205 /* 2.1 */
206 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
207 /* Dolby Surround */
208 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
209 /* surround40 */
210 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
211 /* 4ch */
212 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
213 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800214 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800215 /* surround50 */
216 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
217 /* surround51 */
218 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
219 /* 7.1 */
220 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
221};
222
223/*
224 * This is an ordered list!
225 *
226 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800227 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800228 */
229static struct cea_channel_speaker_allocation channel_allocations[] = {
230/* channel: 7 6 5 4 3 2 1 0 */
231{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
232 /* 2.1 */
233{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
234 /* Dolby Surround */
235{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
236 /* surround40 */
237{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
238 /* surround41 */
239{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
240 /* surround50 */
241{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
242 /* surround51 */
243{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
244 /* 6.1 */
245{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
246 /* surround71 */
247{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
248
249{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
250{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
251{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
252{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
253{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
254{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
255{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
256{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
257{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
258{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
259{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
260{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
261{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
262{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
263{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
264{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
265{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
266{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
267{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
268{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
269{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
270{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
271{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
272{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
273{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
274{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
275{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
276{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
277{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
278{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
279{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
280{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
281{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
282{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
283{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
284{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
285{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
286{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
287{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
288{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
290};
291
292
293/*
294 * HDMI routines
295 */
296
Stephen Warren384a48d2011-06-01 11:14:21 -0600297static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800298{
Stephen Warren384a48d2011-06-01 11:14:21 -0600299 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800300
Stephen Warren384a48d2011-06-01 11:14:21 -0600301 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
302 if (spec->pins[pin_idx].pin_nid == pin_nid)
303 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
306 return -EINVAL;
307}
308
309static int hinfo_to_pin_index(struct hdmi_spec *spec,
310 struct hda_pcm_stream *hinfo)
311{
312 int pin_idx;
313
314 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
315 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
316 return pin_idx;
317
318 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
319 return -EINVAL;
320}
321
322static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
323{
324 int cvt_idx;
325
326 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
327 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
328 return cvt_idx;
329
330 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800331 return -EINVAL;
332}
333
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500334static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
335 struct snd_ctl_elem_info *uinfo)
336{
337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
338 struct hdmi_spec *spec;
339 int pin_idx;
340
341 spec = codec->spec;
342 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
343
344 pin_idx = kcontrol->private_value;
345 uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
346
347 return 0;
348}
349
350static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_value *ucontrol)
352{
353 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
354 struct hdmi_spec *spec;
355 int pin_idx;
356
357 spec = codec->spec;
358 pin_idx = kcontrol->private_value;
359
360 memcpy(ucontrol->value.bytes.data,
361 spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
362
363 return 0;
364}
365
366static struct snd_kcontrol_new eld_bytes_ctl = {
367 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
368 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
369 .name = "ELD",
370 .info = hdmi_eld_ctl_info,
371 .get = hdmi_eld_ctl_get,
372};
373
374static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
375 int device)
376{
377 struct snd_kcontrol *kctl;
378 struct hdmi_spec *spec = codec->spec;
379 int err;
380
381 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
382 if (!kctl)
383 return -ENOMEM;
384 kctl->private_value = pin_idx;
385 kctl->id.device = device;
386
387 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
388 if (err < 0)
389 return err;
390
391 return 0;
392}
393
Wu Fengguang079d88c2010-03-08 10:44:23 +0800394#ifdef BE_PARANOID
395static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
396 int *packet_index, int *byte_index)
397{
398 int val;
399
400 val = snd_hda_codec_read(codec, pin_nid, 0,
401 AC_VERB_GET_HDMI_DIP_INDEX, 0);
402
403 *packet_index = val >> 5;
404 *byte_index = val & 0x1f;
405}
406#endif
407
408static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
409 int packet_index, int byte_index)
410{
411 int val;
412
413 val = (packet_index << 5) | (byte_index & 0x1f);
414
415 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
416}
417
418static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
419 unsigned char val)
420{
421 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
422}
423
Stephen Warren384a48d2011-06-01 11:14:21 -0600424static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800425{
426 /* Unmute */
427 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
428 snd_hda_codec_write(codec, pin_nid, 0,
429 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren384a48d2011-06-01 11:14:21 -0600430 /* Disable pin out until stream is active*/
Wu Fengguang079d88c2010-03-08 10:44:23 +0800431 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -0600432 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800433}
434
Stephen Warren384a48d2011-06-01 11:14:21 -0600435static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800436{
Stephen Warren384a48d2011-06-01 11:14:21 -0600437 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800438 AC_VERB_GET_CVT_CHAN_COUNT, 0);
439}
440
441static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600442 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800443{
Stephen Warren384a48d2011-06-01 11:14:21 -0600444 if (chs != hdmi_get_channel_count(codec, cvt_nid))
445 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800446 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
447}
448
449
450/*
451 * Channel mapping routines
452 */
453
454/*
455 * Compute derived values in channel_allocations[].
456 */
457static void init_channel_allocations(void)
458{
459 int i, j;
460 struct cea_channel_speaker_allocation *p;
461
462 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
463 p = channel_allocations + i;
464 p->channels = 0;
465 p->spk_mask = 0;
466 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
467 if (p->speakers[j]) {
468 p->channels++;
469 p->spk_mask |= p->speakers[j];
470 }
471 }
472}
473
Wang Xingchao72357c72012-09-06 10:02:36 +0800474static int get_channel_allocation_order(int ca)
475{
476 int i;
477
478 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
479 if (channel_allocations[i].ca_index == ca)
480 break;
481 }
482 return i;
483}
484
Wu Fengguang079d88c2010-03-08 10:44:23 +0800485/*
486 * The transformation takes two steps:
487 *
488 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
489 * spk_mask => (channel_allocations[]) => ai->CA
490 *
491 * TODO: it could select the wrong CA from multiple candidates.
492*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600493static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800494{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800495 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800496 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800497 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800498 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
499
500 /*
501 * CA defaults to 0 for basic stereo audio
502 */
503 if (channels <= 2)
504 return 0;
505
Wu Fengguang079d88c2010-03-08 10:44:23 +0800506 /*
507 * expand ELD's speaker allocation mask
508 *
509 * ELD tells the speaker mask in a compact(paired) form,
510 * expand ELD's notions to match the ones used by Audio InfoFrame.
511 */
512 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
513 if (eld->spk_alloc & (1 << i))
514 spk_mask |= eld_speaker_allocation_bits[i];
515 }
516
517 /* search for the first working match in the CA table */
518 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
519 if (channels == channel_allocations[i].channels &&
520 (spk_mask & channel_allocations[i].spk_mask) ==
521 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800522 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800523 break;
524 }
525 }
526
527 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800528 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800529 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800530
Wu Fengguang53d7d692010-09-21 14:25:49 +0800531 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800532}
533
534static void hdmi_debug_channel_mapping(struct hda_codec *codec,
535 hda_nid_t pin_nid)
536{
537#ifdef CONFIG_SND_DEBUG_VERBOSE
538 int i;
539 int slot;
540
541 for (i = 0; i < 8; i++) {
542 slot = snd_hda_codec_read(codec, pin_nid, 0,
543 AC_VERB_GET_HDMI_CHAN_SLOT, i);
544 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
545 slot >> 4, slot & 0xf);
546 }
547#endif
548}
549
550
551static void hdmi_setup_channel_mapping(struct hda_codec *codec,
552 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800553 hda_nid_t cvt_nid,
554 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800555 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800556{
557 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800558 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800559 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800560 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800561
Wang Xingchao72357c72012-09-06 10:02:36 +0800562 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800563
Wu Fengguang079d88c2010-03-08 10:44:23 +0800564 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800565 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800566 hdmi_channel_mapping[ca][i] = i | (i << 4);
567 for (; i < 8; i++)
568 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
569 }
570
Wang Xingchao433968d2012-09-06 10:02:37 +0800571 if (non_pcm) {
572 for (i = 0; i < channel_allocations[order].channels; i++)
573 non_pcm_mapping[i] = i | (i << 4);
574 for (; i < 8; i++)
575 non_pcm_mapping[i] = 0xf | (i << 4);
576 }
577
Wu Fengguang079d88c2010-03-08 10:44:23 +0800578 for (i = 0; i < 8; i++) {
579 err = snd_hda_codec_write(codec, pin_nid, 0,
580 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800581 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800582 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800583 snd_printdd(KERN_NOTICE
584 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585 break;
586 }
587 }
588
589 hdmi_debug_channel_mapping(codec, pin_nid);
590}
591
592
593/*
594 * Audio InfoFrame routines
595 */
596
597/*
598 * Enable Audio InfoFrame Transmission
599 */
600static void hdmi_start_infoframe_trans(struct hda_codec *codec,
601 hda_nid_t pin_nid)
602{
603 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
604 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
605 AC_DIPXMIT_BEST);
606}
607
608/*
609 * Disable Audio InfoFrame Transmission
610 */
611static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
612 hda_nid_t pin_nid)
613{
614 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
615 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
616 AC_DIPXMIT_DISABLE);
617}
618
619static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
620{
621#ifdef CONFIG_SND_DEBUG_VERBOSE
622 int i;
623 int size;
624
625 size = snd_hdmi_get_eld_size(codec, pin_nid);
626 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
627
628 for (i = 0; i < 8; i++) {
629 size = snd_hda_codec_read(codec, pin_nid, 0,
630 AC_VERB_GET_HDMI_DIP_SIZE, i);
631 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
632 }
633#endif
634}
635
636static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
637{
638#ifdef BE_PARANOID
639 int i, j;
640 int size;
641 int pi, bi;
642 for (i = 0; i < 8; i++) {
643 size = snd_hda_codec_read(codec, pin_nid, 0,
644 AC_VERB_GET_HDMI_DIP_SIZE, i);
645 if (size == 0)
646 continue;
647
648 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
649 for (j = 1; j < 1000; j++) {
650 hdmi_write_dip_byte(codec, pin_nid, 0x0);
651 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
652 if (pi != i)
653 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
654 bi, pi, i);
655 if (bi == 0) /* byte index wrapped around */
656 break;
657 }
658 snd_printd(KERN_INFO
659 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
660 i, size, j);
661 }
662#endif
663}
664
Wu Fengguang53d7d692010-09-21 14:25:49 +0800665static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800666{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800667 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800668 u8 sum = 0;
669 int i;
670
Wu Fengguang53d7d692010-09-21 14:25:49 +0800671 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800672
Wu Fengguang53d7d692010-09-21 14:25:49 +0800673 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800674 sum += bytes[i];
675
Wu Fengguang53d7d692010-09-21 14:25:49 +0800676 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800677}
678
679static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
680 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800681 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800682{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800683 int i;
684
685 hdmi_debug_dip_size(codec, pin_nid);
686 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
687
Wu Fengguang079d88c2010-03-08 10:44:23 +0800688 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800689 for (i = 0; i < size; i++)
690 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800691}
692
693static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800694 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800695{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800696 u8 val;
697 int i;
698
699 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
700 != AC_DIPXMIT_BEST)
701 return false;
702
703 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800704 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800705 val = snd_hda_codec_read(codec, pin_nid, 0,
706 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800707 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800708 return false;
709 }
710
711 return true;
712}
713
Stephen Warren384a48d2011-06-01 11:14:21 -0600714static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Wang Xingchao433968d2012-09-06 10:02:37 +0800715 hda_nid_t cvt_nid, struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800716{
717 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600718 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
Wang Xingchao433968d2012-09-06 10:02:37 +0800719 struct hdmi_spec_per_cvt *per_cvt;
720 struct hda_spdif_out *spdif;
Stephen Warren384a48d2011-06-01 11:14:21 -0600721 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800722 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600723 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800724 int ca;
Wang Xingchao433968d2012-09-06 10:02:37 +0800725 int cvt_idx;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100726 union audio_infoframe ai;
Wang Xingchao433968d2012-09-06 10:02:37 +0800727 bool non_pcm = false;
728
729 cvt_idx = cvt_nid_to_cvt_index(spec, cvt_nid);
730 per_cvt = &spec->cvts[cvt_idx];
731
732 mutex_lock(&codec->spdif_mutex);
733 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
734 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
735 mutex_unlock(&codec->spdif_mutex);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800736
Stephen Warren384a48d2011-06-01 11:14:21 -0600737 eld = &spec->pins[pin_idx].sink_eld;
738 if (!eld->monitor_present)
739 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800740
Stephen Warren384a48d2011-06-01 11:14:21 -0600741 ca = hdmi_channel_allocation(eld, channels);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800742
Stephen Warren384a48d2011-06-01 11:14:21 -0600743 memset(&ai, 0, sizeof(ai));
744 if (eld->conn_type == 0) { /* HDMI */
745 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800746
Stephen Warren384a48d2011-06-01 11:14:21 -0600747 hdmi_ai->type = 0x84;
748 hdmi_ai->ver = 0x01;
749 hdmi_ai->len = 0x0a;
750 hdmi_ai->CC02_CT47 = channels - 1;
751 hdmi_ai->CA = ca;
752 hdmi_checksum_audio_infoframe(hdmi_ai);
753 } else if (eld->conn_type == 1) { /* DisplayPort */
754 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800755
Stephen Warren384a48d2011-06-01 11:14:21 -0600756 dp_ai->type = 0x84;
757 dp_ai->len = 0x1b;
758 dp_ai->ver = 0x11 << 2;
759 dp_ai->CC02_CT47 = channels - 1;
760 dp_ai->CA = ca;
761 } else {
762 snd_printd("HDMI: unknown connection type at pin %d\n",
763 pin_nid);
764 return;
765 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800766
Stephen Warren384a48d2011-06-01 11:14:21 -0600767 /*
768 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
769 * sizeof(*dp_ai) to avoid partial match/update problems when
770 * the user switches between HDMI/DP monitors.
771 */
772 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
773 sizeof(ai))) {
774 snd_printdd("hdmi_setup_audio_infoframe: "
775 "pin=%d channels=%d\n",
776 pin_nid,
777 channels);
Wang Xingchao433968d2012-09-06 10:02:37 +0800778 hdmi_setup_channel_mapping(codec, pin_nid, cvt_nid, non_pcm, ca);
Stephen Warren384a48d2011-06-01 11:14:21 -0600779 hdmi_stop_infoframe_trans(codec, pin_nid);
780 hdmi_fill_audio_infoframe(codec, pin_nid,
781 ai.bytes, sizeof(ai));
782 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800783 } else {
784 /* For non-pcm audio switch, setup new channel mapping
785 * accordingly */
786 if (per_cvt->non_pcm != non_pcm)
787 hdmi_setup_channel_mapping(codec, pin_nid, cvt_nid, non_pcm, ca);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800788 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800789
790 per_cvt->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800791}
792
793
794/*
795 * Unsolicited events
796 */
797
Wu Fengguangc6e84532011-11-18 16:59:32 -0600798static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200799
Wu Fengguang079d88c2010-03-08 10:44:23 +0800800static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
801{
802 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200803 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
804 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600805 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200806 struct hda_jack_tbl *jack;
807
808 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
809 if (!jack)
810 return;
811 pin_nid = jack->nid;
812 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800813
Fengguang Wufae3d882012-04-10 17:00:35 +0800814 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600815 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800816 codec->addr, pin_nid,
817 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800818
Stephen Warren384a48d2011-06-01 11:14:21 -0600819 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
820 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800821 return;
822
Wu Fengguangc6e84532011-11-18 16:59:32 -0600823 hdmi_present_sense(&spec->pins[pin_idx], 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200824 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800825}
826
827static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
828{
829 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
830 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
831 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
832 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
833
834 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200835 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600836 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800837 tag,
838 subtag,
839 cp_state,
840 cp_ready);
841
842 /* TODO */
843 if (cp_state)
844 ;
845 if (cp_ready)
846 ;
847}
848
849
850static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
851{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
853 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
854
Takashi Iwai3a938972011-10-28 01:16:55 +0200855 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800856 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
857 return;
858 }
859
860 if (subtag == 0)
861 hdmi_intrinsic_event(codec, res);
862 else
863 hdmi_non_intrinsic_event(codec, res);
864}
865
866/*
867 * Callbacks
868 */
869
Takashi Iwai92f10b32010-08-03 14:21:00 +0200870/* HBR should be Non-PCM, 8 channels */
871#define is_hbr_format(format) \
872 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
873
Stephen Warren384a48d2011-06-01 11:14:21 -0600874static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
875 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800876{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300877 int pinctl;
878 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300879
Stephen Warren384a48d2011-06-01 11:14:21 -0600880 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
881 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300882 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
883
884 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +0200885 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300886 new_pinctl |= AC_PINCTL_EPT_HBR;
887 else
888 new_pinctl |= AC_PINCTL_EPT_NATIVE;
889
890 snd_printdd("hdmi_setup_stream: "
891 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600892 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300893 pinctl == new_pinctl ? "" : "new-",
894 new_pinctl);
895
896 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -0600897 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300898 AC_VERB_SET_PIN_WIDGET_CONTROL,
899 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300900
Stephen Warren384a48d2011-06-01 11:14:21 -0600901 }
Takashi Iwai92f10b32010-08-03 14:21:00 +0200902 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300903 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
904 return -EINVAL;
905 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800906
Stephen Warren384a48d2011-06-01 11:14:21 -0600907 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +0300908 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800909}
910
911/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200912 * HDA PCM callbacks
913 */
914static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
915 struct hda_codec *codec,
916 struct snd_pcm_substream *substream)
917{
918 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +0100919 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -0600920 int pin_idx, cvt_idx, mux_idx = 0;
921 struct hdmi_spec_per_pin *per_pin;
922 struct hdmi_eld *eld;
923 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200924
Stephen Warren384a48d2011-06-01 11:14:21 -0600925 /* Validate hinfo */
926 pin_idx = hinfo_to_pin_index(spec, hinfo);
927 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200928 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -0600929 per_pin = &spec->pins[pin_idx];
930 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200931
Stephen Warren384a48d2011-06-01 11:14:21 -0600932 /* Dynamically assign converter to stream */
933 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
934 per_cvt = &spec->cvts[cvt_idx];
935
936 /* Must not already be assigned */
937 if (per_cvt->assigned)
938 continue;
939 /* Must be in pin's mux's list of converters */
940 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
941 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
942 break;
943 /* Not in mux list */
944 if (mux_idx == per_pin->num_mux_nids)
945 continue;
946 break;
947 }
948 /* No free converters */
949 if (cvt_idx == spec->num_cvts)
950 return -ENODEV;
951
952 /* Claim converter */
953 per_cvt->assigned = 1;
954 hinfo->nid = per_cvt->cvt_nid;
955
956 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
957 AC_VERB_SET_CONNECT_SEL,
958 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -0600959 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200960
Stephen Warren2def8172011-06-01 11:14:20 -0600961 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -0600962 hinfo->channels_min = per_cvt->channels_min;
963 hinfo->channels_max = per_cvt->channels_max;
964 hinfo->rates = per_cvt->rates;
965 hinfo->formats = per_cvt->formats;
966 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -0600967
Stephen Warren384a48d2011-06-01 11:14:21 -0600968 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -0600969 if (!static_hdmi_pcm && eld->eld_valid) {
Stephen Warren2def8172011-06-01 11:14:20 -0600970 snd_hdmi_eld_update_pcm_info(eld, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200971 if (hinfo->channels_min > hinfo->channels_max ||
972 !hinfo->rates || !hinfo->formats)
973 return -ENODEV;
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200974 }
Stephen Warren2def8172011-06-01 11:14:20 -0600975
976 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +0100977 runtime->hw.channels_min = hinfo->channels_min;
978 runtime->hw.channels_max = hinfo->channels_max;
979 runtime->hw.formats = hinfo->formats;
980 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +0100981
982 snd_pcm_hw_constraint_step(substream->runtime, 0,
983 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +0200984 return 0;
985}
986
987/*
Wu Fengguang079d88c2010-03-08 10:44:23 +0800988 * HDA/HDMI auto parsing
989 */
Stephen Warren384a48d2011-06-01 11:14:21 -0600990static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800991{
992 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600993 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
994 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800995
996 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
997 snd_printk(KERN_WARNING
998 "HDMI: pin %d wcaps %#x "
999 "does not support connection list\n",
1000 pin_nid, get_wcaps(codec, pin_nid));
1001 return -EINVAL;
1002 }
1003
Stephen Warren384a48d2011-06-01 11:14:21 -06001004 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1005 per_pin->mux_nids,
1006 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001007
1008 return 0;
1009}
1010
Wu Fengguangc6e84532011-11-18 16:59:32 -06001011static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001012{
Wu Fengguang744626d2011-11-16 16:29:47 +08001013 struct hda_codec *codec = per_pin->codec;
1014 struct hdmi_eld *eld = &per_pin->sink_eld;
1015 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001016 /*
1017 * Always execute a GetPinSense verb here, even when called from
1018 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1019 * response's PD bit is not the real PD value, but indicates that
1020 * the real PD value changed. An older version of the HD-audio
1021 * specification worked this way. Hence, we just ignore the data in
1022 * the unsolicited response to avoid custom WARs.
1023 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001024 int present = snd_hda_pin_sense(codec, pin_nid);
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001025 bool eld_valid = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001026
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001027 memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001028
Stephen Warren5d44f922011-05-24 17:11:17 -06001029 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1030 if (eld->monitor_present)
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001031 eld_valid = !!(present & AC_PINSENSE_ELDV);
Stephen Warren5d44f922011-05-24 17:11:17 -06001032
Fengguang Wufae3d882012-04-10 17:00:35 +08001033 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001034 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001035 codec->addr, pin_nid, eld->monitor_present, eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001036
Wu Fengguang744626d2011-11-16 16:29:47 +08001037 if (eld_valid) {
Stephen Warren5d44f922011-05-24 17:11:17 -06001038 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
1039 snd_hdmi_show_eld(eld);
Wu Fengguangc6e84532011-11-18 16:59:32 -06001040 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001041 queue_delayed_work(codec->bus->workq,
1042 &per_pin->work,
1043 msecs_to_jiffies(300));
1044 }
1045 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001046}
1047
Wu Fengguang744626d2011-11-16 16:29:47 +08001048static void hdmi_repoll_eld(struct work_struct *work)
1049{
1050 struct hdmi_spec_per_pin *per_pin =
1051 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1052
Wu Fengguangc6e84532011-11-18 16:59:32 -06001053 if (per_pin->repoll_count++ > 6)
1054 per_pin->repoll_count = 0;
1055
1056 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001057}
1058
Wu Fengguang079d88c2010-03-08 10:44:23 +08001059static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1060{
1061 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001062 unsigned int caps, config;
1063 int pin_idx;
1064 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001065 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001066
Stephen Warren384a48d2011-06-01 11:14:21 -06001067 caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
1068 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1069 return 0;
1070
1071 config = snd_hda_codec_read(codec, pin_nid, 0,
1072 AC_VERB_GET_CONFIG_DEFAULT, 0);
1073 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1074 return 0;
1075
1076 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001077 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001078
1079 pin_idx = spec->num_pins;
1080 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001081
1082 per_pin->pin_nid = pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001083
Stephen Warren384a48d2011-06-01 11:14:21 -06001084 err = hdmi_read_pin_conn(codec, pin_idx);
1085 if (err < 0)
1086 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001087
Wu Fengguang079d88c2010-03-08 10:44:23 +08001088 spec->num_pins++;
1089
Stephen Warren384a48d2011-06-01 11:14:21 -06001090 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001091}
1092
Stephen Warren384a48d2011-06-01 11:14:21 -06001093static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001094{
1095 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001096 int cvt_idx;
1097 struct hdmi_spec_per_cvt *per_cvt;
1098 unsigned int chans;
1099 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001100
David Henningsson116dcde2010-11-23 10:23:40 +01001101 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1102 return -E2BIG;
1103
Stephen Warren384a48d2011-06-01 11:14:21 -06001104 chans = get_wcaps(codec, cvt_nid);
1105 chans = get_wcaps_channels(chans);
1106
1107 cvt_idx = spec->num_cvts;
1108 per_cvt = &spec->cvts[cvt_idx];
1109
1110 per_cvt->cvt_nid = cvt_nid;
1111 per_cvt->channels_min = 2;
Wang Xingchao433968d2012-09-06 10:02:37 +08001112 per_cvt->non_pcm = false;
Stephen Warren384a48d2011-06-01 11:14:21 -06001113 if (chans <= 16)
1114 per_cvt->channels_max = chans;
1115
1116 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1117 &per_cvt->rates,
1118 &per_cvt->formats,
1119 &per_cvt->maxbps);
1120 if (err < 0)
1121 return err;
1122
Wu Fengguang079d88c2010-03-08 10:44:23 +08001123 spec->num_cvts++;
1124
1125 return 0;
1126}
1127
1128static int hdmi_parse_codec(struct hda_codec *codec)
1129{
1130 hda_nid_t nid;
1131 int i, nodes;
1132
1133 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1134 if (!nid || nodes < 0) {
1135 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1136 return -EINVAL;
1137 }
1138
1139 for (i = 0; i < nodes; i++, nid++) {
1140 unsigned int caps;
1141 unsigned int type;
1142
1143 caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1144 type = get_wcaps_type(caps);
1145
1146 if (!(caps & AC_WCAP_DIGITAL))
1147 continue;
1148
1149 switch (type) {
1150 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001151 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001152 break;
1153 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001154 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001155 break;
1156 }
1157 }
1158
1159 /*
1160 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1161 * can be lost and presence sense verb will become inaccurate if the
1162 * HDA link is powered off at hot plug or hw initialization time.
1163 */
Takashi Iwai83012a72012-08-24 18:38:08 +02001164#ifdef CONFIG_PM
Wu Fengguang079d88c2010-03-08 10:44:23 +08001165 if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
1166 AC_PWRST_EPSS))
1167 codec->bus->power_keep_link_on = 1;
1168#endif
1169
1170 return 0;
1171}
1172
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001173/*
1174 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001175static char *get_hdmi_pcm_name(int idx)
1176{
1177 static char names[MAX_HDMI_PINS][8];
1178 sprintf(&names[idx][0], "HDMI %d", idx);
1179 return &names[idx][0];
1180}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001181
1182/*
1183 * HDMI callbacks
1184 */
1185
1186static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1187 struct hda_codec *codec,
1188 unsigned int stream_tag,
1189 unsigned int format,
1190 struct snd_pcm_substream *substream)
1191{
Stephen Warren384a48d2011-06-01 11:14:21 -06001192 hda_nid_t cvt_nid = hinfo->nid;
1193 struct hdmi_spec *spec = codec->spec;
1194 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1195 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Dylan Reid9e76e6d2012-07-19 17:52:58 -07001196 int pinctl;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001197
Stephen Warren384a48d2011-06-01 11:14:21 -06001198 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001199
Wang Xingchao433968d2012-09-06 10:02:37 +08001200 hdmi_setup_audio_infoframe(codec, pin_idx, cvt_nid, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001201
Dylan Reid9e76e6d2012-07-19 17:52:58 -07001202 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1203 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1204 snd_hda_codec_write(codec, pin_nid, 0,
1205 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl | PIN_OUT);
1206
Stephen Warren384a48d2011-06-01 11:14:21 -06001207 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001208}
1209
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001210static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1211 struct hda_codec *codec,
1212 struct snd_pcm_substream *substream)
1213{
1214 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1215 return 0;
1216}
1217
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001218static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1219 struct hda_codec *codec,
1220 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001221{
1222 struct hdmi_spec *spec = codec->spec;
1223 int cvt_idx, pin_idx;
1224 struct hdmi_spec_per_cvt *per_cvt;
1225 struct hdmi_spec_per_pin *per_pin;
1226 int pinctl;
1227
Stephen Warren384a48d2011-06-01 11:14:21 -06001228 if (hinfo->nid) {
1229 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1230 if (snd_BUG_ON(cvt_idx < 0))
1231 return -EINVAL;
1232 per_cvt = &spec->cvts[cvt_idx];
1233
1234 snd_BUG_ON(!per_cvt->assigned);
1235 per_cvt->assigned = 0;
1236 hinfo->nid = 0;
1237
1238 pin_idx = hinfo_to_pin_index(spec, hinfo);
1239 if (snd_BUG_ON(pin_idx < 0))
1240 return -EINVAL;
1241 per_pin = &spec->pins[pin_idx];
1242
1243 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1244 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1245 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1246 AC_VERB_SET_PIN_WIDGET_CONTROL,
1247 pinctl & ~PIN_OUT);
1248 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1249 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001250 return 0;
1251}
1252
1253static const struct hda_pcm_ops generic_ops = {
1254 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001255 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001256 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001257 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001258};
1259
1260static int generic_hdmi_build_pcms(struct hda_codec *codec)
1261{
1262 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001263 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001264
Stephen Warren384a48d2011-06-01 11:14:21 -06001265 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1266 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001267 struct hda_pcm_stream *pstr;
1268
Stephen Warren384a48d2011-06-01 11:14:21 -06001269 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001270 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001271 info->pcm_type = HDA_PCM_TYPE_HDMI;
Stephen Warren384a48d2011-06-01 11:14:21 -06001272
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001273 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001274 pstr->substreams = 1;
1275 pstr->ops = generic_ops;
1276 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001277 }
1278
Stephen Warren384a48d2011-06-01 11:14:21 -06001279 codec->num_pcms = spec->num_pins;
1280 codec->pcm_info = spec->pcm_rec;
1281
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001282 return 0;
1283}
1284
David Henningsson0b6c49b2011-08-23 16:56:03 +02001285static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1286{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001287 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001288 struct hdmi_spec *spec = codec->spec;
1289 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1290 int pcmdev = spec->pcm_rec[pin_idx].device;
1291
Takashi Iwai31ef2252011-12-01 17:41:36 +01001292 if (pcmdev > 0)
1293 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001294
Takashi Iwai31ef2252011-12-01 17:41:36 +01001295 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001296}
1297
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001298static int generic_hdmi_build_controls(struct hda_codec *codec)
1299{
1300 struct hdmi_spec *spec = codec->spec;
1301 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001302 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001303
Stephen Warren384a48d2011-06-01 11:14:21 -06001304 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1305 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001306
1307 err = generic_hdmi_build_jack(codec, pin_idx);
1308 if (err < 0)
1309 return err;
1310
Stephen Warren384a48d2011-06-01 11:14:21 -06001311 err = snd_hda_create_spdif_out_ctls(codec,
1312 per_pin->pin_nid,
1313 per_pin->mux_nids[0]);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001314 if (err < 0)
1315 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001316 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001317
1318 /* add control for ELD Bytes */
1319 err = hdmi_create_eld_ctl(codec,
1320 pin_idx,
1321 spec->pcm_rec[pin_idx].device);
1322
1323 if (err < 0)
1324 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001325
Takashi Iwai82b1d732011-12-20 15:53:07 +01001326 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001327 }
1328
1329 return 0;
1330}
1331
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001332static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1333{
1334 struct hdmi_spec *spec = codec->spec;
1335 int pin_idx;
1336
1337 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1338 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1339 struct hdmi_eld *eld = &per_pin->sink_eld;
1340
1341 per_pin->codec = codec;
1342 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1343 snd_hda_eld_proc_new(codec, eld, pin_idx);
1344 }
1345 return 0;
1346}
1347
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001348static int generic_hdmi_init(struct hda_codec *codec)
1349{
1350 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001351 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001352
Stephen Warren384a48d2011-06-01 11:14:21 -06001353 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1354 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1355 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001356
1357 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001358 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001359 }
1360 return 0;
1361}
1362
1363static void generic_hdmi_free(struct hda_codec *codec)
1364{
1365 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001366 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001367
Stephen Warren384a48d2011-06-01 11:14:21 -06001368 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1369 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1370 struct hdmi_eld *eld = &per_pin->sink_eld;
1371
Wu Fengguang744626d2011-11-16 16:29:47 +08001372 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001373 snd_hda_eld_proc_free(codec, eld);
1374 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001375
Wu Fengguang744626d2011-11-16 16:29:47 +08001376 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001377 kfree(spec);
1378}
1379
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001380static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001381 .init = generic_hdmi_init,
1382 .free = generic_hdmi_free,
1383 .build_pcms = generic_hdmi_build_pcms,
1384 .build_controls = generic_hdmi_build_controls,
1385 .unsol_event = hdmi_unsol_event,
1386};
1387
1388static int patch_generic_hdmi(struct hda_codec *codec)
1389{
1390 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001391
1392 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1393 if (spec == NULL)
1394 return -ENOMEM;
1395
1396 codec->spec = spec;
1397 if (hdmi_parse_codec(codec) < 0) {
1398 codec->spec = NULL;
1399 kfree(spec);
1400 return -EINVAL;
1401 }
1402 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001403 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001404
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001405 init_channel_allocations();
1406
1407 return 0;
1408}
1409
1410/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001411 * Shared non-generic implementations
1412 */
1413
1414static int simple_playback_build_pcms(struct hda_codec *codec)
1415{
1416 struct hdmi_spec *spec = codec->spec;
1417 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001418 unsigned int chans;
1419 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001420
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001421 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001422 codec->pcm_info = info;
1423
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001424 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1425 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001426
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001427 info->name = get_hdmi_pcm_name(0);
1428 info->pcm_type = HDA_PCM_TYPE_HDMI;
1429 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1430 *pstr = spec->pcm_playback;
1431 pstr->nid = spec->cvts[0].cvt_nid;
1432 if (pstr->channels_max <= 2 && chans && chans <= 16)
1433 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001434
1435 return 0;
1436}
1437
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001438/* unsolicited event for jack sensing */
1439static void simple_hdmi_unsol_event(struct hda_codec *codec,
1440 unsigned int res)
1441{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001442 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001443 snd_hda_jack_report_sync(codec);
1444}
1445
1446/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1447 * as long as spec->pins[] is set correctly
1448 */
1449#define simple_hdmi_build_jack generic_hdmi_build_jack
1450
Stephen Warren3aaf8982011-06-01 11:14:19 -06001451static int simple_playback_build_controls(struct hda_codec *codec)
1452{
1453 struct hdmi_spec *spec = codec->spec;
1454 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001455
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001456 err = snd_hda_create_spdif_out_ctls(codec,
1457 spec->cvts[0].cvt_nid,
1458 spec->cvts[0].cvt_nid);
1459 if (err < 0)
1460 return err;
1461 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001462}
1463
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001464static int simple_playback_init(struct hda_codec *codec)
1465{
1466 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001467 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001468
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001469 snd_hda_codec_write(codec, pin, 0,
1470 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1471 /* some codecs require to unmute the pin */
1472 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1473 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1474 AMP_OUT_UNMUTE);
1475 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001476 return 0;
1477}
1478
Stephen Warren3aaf8982011-06-01 11:14:19 -06001479static void simple_playback_free(struct hda_codec *codec)
1480{
1481 struct hdmi_spec *spec = codec->spec;
1482
1483 kfree(spec);
1484}
1485
1486/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001487 * Nvidia specific implementations
1488 */
1489
1490#define Nv_VERB_SET_Channel_Allocation 0xF79
1491#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1492#define Nv_VERB_SET_Audio_Protection_On 0xF98
1493#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1494
1495#define nvhdmi_master_con_nid_7x 0x04
1496#define nvhdmi_master_pin_nid_7x 0x05
1497
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001498static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001499 /*front, rear, clfe, rear_surr */
1500 0x6, 0x8, 0xa, 0xc,
1501};
1502
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001503static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1504 /* set audio protect on */
1505 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1506 /* enable digital output on pin widget */
1507 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1508 {} /* terminator */
1509};
1510
1511static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001512 /* set audio protect on */
1513 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1514 /* enable digital output on pin widget */
1515 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1516 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1517 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1518 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1519 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1520 {} /* terminator */
1521};
1522
1523#ifdef LIMITED_RATE_FMT_SUPPORT
1524/* support only the safe format and rate */
1525#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1526#define SUPPORTED_MAXBPS 16
1527#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1528#else
1529/* support all rates and formats */
1530#define SUPPORTED_RATES \
1531 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1532 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1533 SNDRV_PCM_RATE_192000)
1534#define SUPPORTED_MAXBPS 24
1535#define SUPPORTED_FORMATS \
1536 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1537#endif
1538
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001539static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001540{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001541 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1542 return 0;
1543}
1544
1545static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
1546{
1547 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001548 return 0;
1549}
1550
Nitin Daga393004b2011-01-10 21:49:31 +05301551static unsigned int channels_2_6_8[] = {
1552 2, 6, 8
1553};
1554
1555static unsigned int channels_2_8[] = {
1556 2, 8
1557};
1558
1559static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
1560 .count = ARRAY_SIZE(channels_2_6_8),
1561 .list = channels_2_6_8,
1562 .mask = 0,
1563};
1564
1565static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
1566 .count = ARRAY_SIZE(channels_2_8),
1567 .list = channels_2_8,
1568 .mask = 0,
1569};
1570
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001571static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
1572 struct hda_codec *codec,
1573 struct snd_pcm_substream *substream)
1574{
1575 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05301576 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
1577
1578 switch (codec->preset->id) {
1579 case 0x10de0002:
1580 case 0x10de0003:
1581 case 0x10de0005:
1582 case 0x10de0006:
1583 hw_constraints_channels = &hw_constraints_2_8_channels;
1584 break;
1585 case 0x10de0007:
1586 hw_constraints_channels = &hw_constraints_2_6_8_channels;
1587 break;
1588 default:
1589 break;
1590 }
1591
1592 if (hw_constraints_channels != NULL) {
1593 snd_pcm_hw_constraint_list(substream->runtime, 0,
1594 SNDRV_PCM_HW_PARAM_CHANNELS,
1595 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01001596 } else {
1597 snd_pcm_hw_constraint_step(substream->runtime, 0,
1598 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05301599 }
1600
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001601 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1602}
1603
1604static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
1605 struct hda_codec *codec,
1606 struct snd_pcm_substream *substream)
1607{
1608 struct hdmi_spec *spec = codec->spec;
1609 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1610}
1611
1612static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1613 struct hda_codec *codec,
1614 unsigned int stream_tag,
1615 unsigned int format,
1616 struct snd_pcm_substream *substream)
1617{
1618 struct hdmi_spec *spec = codec->spec;
1619 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1620 stream_tag, format, substream);
1621}
1622
Takashi Iwaid0b12522012-06-15 14:34:42 +02001623static const struct hda_pcm_stream simple_pcm_playback = {
1624 .substreams = 1,
1625 .channels_min = 2,
1626 .channels_max = 2,
1627 .ops = {
1628 .open = simple_playback_pcm_open,
1629 .close = simple_playback_pcm_close,
1630 .prepare = simple_playback_pcm_prepare
1631 },
1632};
1633
1634static const struct hda_codec_ops simple_hdmi_patch_ops = {
1635 .build_controls = simple_playback_build_controls,
1636 .build_pcms = simple_playback_build_pcms,
1637 .init = simple_playback_init,
1638 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02001639 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02001640};
1641
1642static int patch_simple_hdmi(struct hda_codec *codec,
1643 hda_nid_t cvt_nid, hda_nid_t pin_nid)
1644{
1645 struct hdmi_spec *spec;
1646
1647 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1648 if (!spec)
1649 return -ENOMEM;
1650
1651 codec->spec = spec;
1652
1653 spec->multiout.num_dacs = 0; /* no analog */
1654 spec->multiout.max_channels = 2;
1655 spec->multiout.dig_out_nid = cvt_nid;
1656 spec->num_cvts = 1;
1657 spec->num_pins = 1;
1658 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02001659 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001660 spec->pcm_playback = simple_pcm_playback;
1661
1662 codec->patch_ops = simple_hdmi_patch_ops;
1663
1664 return 0;
1665}
1666
Aaron Plattner1f348522011-04-06 17:19:04 -07001667static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
1668 int channels)
1669{
1670 unsigned int chanmask;
1671 int chan = channels ? (channels - 1) : 1;
1672
1673 switch (channels) {
1674 default:
1675 case 0:
1676 case 2:
1677 chanmask = 0x00;
1678 break;
1679 case 4:
1680 chanmask = 0x08;
1681 break;
1682 case 6:
1683 chanmask = 0x0b;
1684 break;
1685 case 8:
1686 chanmask = 0x13;
1687 break;
1688 }
1689
1690 /* Set the audio infoframe channel allocation and checksum fields. The
1691 * channel count is computed implicitly by the hardware. */
1692 snd_hda_codec_write(codec, 0x1, 0,
1693 Nv_VERB_SET_Channel_Allocation, chanmask);
1694
1695 snd_hda_codec_write(codec, 0x1, 0,
1696 Nv_VERB_SET_Info_Frame_Checksum,
1697 (0x71 - chan - chanmask));
1698}
1699
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001700static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
1701 struct hda_codec *codec,
1702 struct snd_pcm_substream *substream)
1703{
1704 struct hdmi_spec *spec = codec->spec;
1705 int i;
1706
1707 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
1708 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1709 for (i = 0; i < 4; i++) {
1710 /* set the stream id */
1711 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
1712 AC_VERB_SET_CHANNEL_STREAMID, 0);
1713 /* set the stream format */
1714 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
1715 AC_VERB_SET_STREAM_FORMAT, 0);
1716 }
1717
Aaron Plattner1f348522011-04-06 17:19:04 -07001718 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
1719 * streams are disabled. */
1720 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
1721
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001722 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1723}
1724
1725static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
1726 struct hda_codec *codec,
1727 unsigned int stream_tag,
1728 unsigned int format,
1729 struct snd_pcm_substream *substream)
1730{
1731 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01001732 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001733 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06001734 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02001735 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001736
1737 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02001738 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001739
1740 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001741
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001742 dataDCC2 = 0x2;
1743
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001744 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06001745 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001746 snd_hda_codec_write(codec,
1747 nvhdmi_master_con_nid_7x,
1748 0,
1749 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06001750 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001751
1752 /* set the stream id */
1753 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
1754 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
1755
1756 /* set the stream format */
1757 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
1758 AC_VERB_SET_STREAM_FORMAT, format);
1759
1760 /* turn on again (if needed) */
1761 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06001762 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001763 snd_hda_codec_write(codec,
1764 nvhdmi_master_con_nid_7x,
1765 0,
1766 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06001767 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001768 snd_hda_codec_write(codec,
1769 nvhdmi_master_con_nid_7x,
1770 0,
1771 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
1772 }
1773
1774 for (i = 0; i < 4; i++) {
1775 if (chs == 2)
1776 channel_id = 0;
1777 else
1778 channel_id = i * 2;
1779
1780 /* turn off SPDIF once;
1781 *otherwise the IEC958 bits won't be updated
1782 */
1783 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06001784 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001785 snd_hda_codec_write(codec,
1786 nvhdmi_con_nids_7x[i],
1787 0,
1788 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06001789 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001790 /* set the stream id */
1791 snd_hda_codec_write(codec,
1792 nvhdmi_con_nids_7x[i],
1793 0,
1794 AC_VERB_SET_CHANNEL_STREAMID,
1795 (stream_tag << 4) | channel_id);
1796 /* set the stream format */
1797 snd_hda_codec_write(codec,
1798 nvhdmi_con_nids_7x[i],
1799 0,
1800 AC_VERB_SET_STREAM_FORMAT,
1801 format);
1802 /* turn on again (if needed) */
1803 /* enable and set the channel status audio/data flag */
1804 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06001805 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001806 snd_hda_codec_write(codec,
1807 nvhdmi_con_nids_7x[i],
1808 0,
1809 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06001810 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001811 snd_hda_codec_write(codec,
1812 nvhdmi_con_nids_7x[i],
1813 0,
1814 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
1815 }
1816 }
1817
Aaron Plattner1f348522011-04-06 17:19:04 -07001818 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001819
1820 mutex_unlock(&codec->spdif_mutex);
1821 return 0;
1822}
1823
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001824static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001825 .substreams = 1,
1826 .channels_min = 2,
1827 .channels_max = 8,
1828 .nid = nvhdmi_master_con_nid_7x,
1829 .rates = SUPPORTED_RATES,
1830 .maxbps = SUPPORTED_MAXBPS,
1831 .formats = SUPPORTED_FORMATS,
1832 .ops = {
1833 .open = simple_playback_pcm_open,
1834 .close = nvhdmi_8ch_7x_pcm_close,
1835 .prepare = nvhdmi_8ch_7x_pcm_prepare
1836 },
1837};
1838
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001839static int patch_nvhdmi_2ch(struct hda_codec *codec)
1840{
1841 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001842 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
1843 nvhdmi_master_pin_nid_7x);
1844 if (err < 0)
1845 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001846
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001847 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001848 /* override the PCM rates, etc, as the codec doesn't give full list */
1849 spec = codec->spec;
1850 spec->pcm_playback.rates = SUPPORTED_RATES;
1851 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
1852 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001853 return 0;
1854}
1855
1856static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
1857{
1858 struct hdmi_spec *spec;
1859 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001860 if (err < 0)
1861 return err;
1862 spec = codec->spec;
1863 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001864 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001865 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Aaron Plattner1f348522011-04-06 17:19:04 -07001866
1867 /* Initialize the audio infoframe channel mask and checksum to something
1868 * valid */
1869 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
1870
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001871 return 0;
1872}
1873
1874/*
1875 * ATI-specific implementations
1876 *
1877 * FIXME: we may omit the whole this and use the generic code once after
1878 * it's confirmed to work.
1879 */
1880
1881#define ATIHDMI_CVT_NID 0x02 /* audio converter */
1882#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
1883
1884static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1885 struct hda_codec *codec,
1886 unsigned int stream_tag,
1887 unsigned int format,
1888 struct snd_pcm_substream *substream)
1889{
1890 struct hdmi_spec *spec = codec->spec;
1891 int chans = substream->runtime->channels;
1892 int i, err;
1893
1894 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
1895 substream);
1896 if (err < 0)
1897 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001898 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
1899 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001900 /* FIXME: XXX */
1901 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001902 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001903 AC_VERB_SET_HDMI_CHAN_SLOT,
1904 (i << 4) | i);
1905 }
1906 return 0;
1907}
1908
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001909static int patch_atihdmi(struct hda_codec *codec)
1910{
1911 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001912 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
1913 if (err < 0)
1914 return err;
1915 spec = codec->spec;
1916 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001917 return 0;
1918}
1919
Annie Liu3de5ff82012-06-08 19:18:42 +08001920/* VIA HDMI Implementation */
1921#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
1922#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
1923
Annie Liu3de5ff82012-06-08 19:18:42 +08001924static int patch_via_hdmi(struct hda_codec *codec)
1925{
Takashi Iwai250e41a2012-06-15 14:40:21 +02001926 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08001927}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001928
1929/*
1930 * patch entries
1931 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001932static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001933{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
1934{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
1935{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02001936{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001937{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
1938{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
1939{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
1940{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
1941{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
1942{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
1943{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
1944{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06001945{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
1946{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
1947{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
1948{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
1949{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
1950{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
1951{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
1952{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
1953{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
1954{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
1955{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01001956/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06001957{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
1958{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
1959{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
1960{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
1961{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
1962{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
1963{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
1964{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
1965{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
1966{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07001967{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001968{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
1969{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08001970{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
1971{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
1972{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
1973{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001974{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
1975{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
1976{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
1977{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
1978{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
1979{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08001980{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08001981{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08001982{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001983{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
1984{} /* terminator */
1985};
1986
1987MODULE_ALIAS("snd-hda-codec-id:1002793c");
1988MODULE_ALIAS("snd-hda-codec-id:10027919");
1989MODULE_ALIAS("snd-hda-codec-id:1002791a");
1990MODULE_ALIAS("snd-hda-codec-id:1002aa01");
1991MODULE_ALIAS("snd-hda-codec-id:10951390");
1992MODULE_ALIAS("snd-hda-codec-id:10951392");
1993MODULE_ALIAS("snd-hda-codec-id:10de0002");
1994MODULE_ALIAS("snd-hda-codec-id:10de0003");
1995MODULE_ALIAS("snd-hda-codec-id:10de0005");
1996MODULE_ALIAS("snd-hda-codec-id:10de0006");
1997MODULE_ALIAS("snd-hda-codec-id:10de0007");
1998MODULE_ALIAS("snd-hda-codec-id:10de000a");
1999MODULE_ALIAS("snd-hda-codec-id:10de000b");
2000MODULE_ALIAS("snd-hda-codec-id:10de000c");
2001MODULE_ALIAS("snd-hda-codec-id:10de000d");
2002MODULE_ALIAS("snd-hda-codec-id:10de0010");
2003MODULE_ALIAS("snd-hda-codec-id:10de0011");
2004MODULE_ALIAS("snd-hda-codec-id:10de0012");
2005MODULE_ALIAS("snd-hda-codec-id:10de0013");
2006MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002007MODULE_ALIAS("snd-hda-codec-id:10de0015");
2008MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002009MODULE_ALIAS("snd-hda-codec-id:10de0018");
2010MODULE_ALIAS("snd-hda-codec-id:10de0019");
2011MODULE_ALIAS("snd-hda-codec-id:10de001a");
2012MODULE_ALIAS("snd-hda-codec-id:10de001b");
2013MODULE_ALIAS("snd-hda-codec-id:10de001c");
2014MODULE_ALIAS("snd-hda-codec-id:10de0040");
2015MODULE_ALIAS("snd-hda-codec-id:10de0041");
2016MODULE_ALIAS("snd-hda-codec-id:10de0042");
2017MODULE_ALIAS("snd-hda-codec-id:10de0043");
2018MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002019MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002020MODULE_ALIAS("snd-hda-codec-id:10de0067");
2021MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002022MODULE_ALIAS("snd-hda-codec-id:11069f80");
2023MODULE_ALIAS("snd-hda-codec-id:11069f81");
2024MODULE_ALIAS("snd-hda-codec-id:11069f84");
2025MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002026MODULE_ALIAS("snd-hda-codec-id:17e80047");
2027MODULE_ALIAS("snd-hda-codec-id:80860054");
2028MODULE_ALIAS("snd-hda-codec-id:80862801");
2029MODULE_ALIAS("snd-hda-codec-id:80862802");
2030MODULE_ALIAS("snd-hda-codec-id:80862803");
2031MODULE_ALIAS("snd-hda-codec-id:80862804");
2032MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002033MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002034MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002035MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002036MODULE_ALIAS("snd-hda-codec-id:808629fb");
2037
2038MODULE_LICENSE("GPL");
2039MODULE_DESCRIPTION("HDMI HD-audio codec");
2040MODULE_ALIAS("snd-hda-codec-intelhdmi");
2041MODULE_ALIAS("snd-hda-codec-nvhdmi");
2042MODULE_ALIAS("snd-hda-codec-atihdmi");
2043
2044static struct hda_codec_preset_list intel_list = {
2045 .preset = snd_hda_preset_hdmi,
2046 .owner = THIS_MODULE,
2047};
2048
2049static int __init patch_hdmi_init(void)
2050{
2051 return snd_hda_add_codec_preset(&intel_list);
2052}
2053
2054static void __exit patch_hdmi_exit(void)
2055{
2056 snd_hda_delete_codec_preset(&intel_list);
2057}
2058
2059module_init(patch_hdmi_init)
2060module_exit(patch_hdmi_exit)