blob: 1b3b4ee3e8a7b2a35e21692110340cbf3dc71e42 [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
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;
Takashi Iwai1a6003b2012-09-06 17:42:08 +020076 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020077 bool chmap_set; /* channel-map override by ALSA API? */
78 unsigned char chmap[8]; /* ALSA API channel-map */
Stephen Warren384a48d2011-06-01 11:14:21 -060079};
80
Wu Fengguang079d88c2010-03-08 10:44:23 +080081struct hdmi_spec {
82 int num_cvts;
Stephen Warren384a48d2011-06-01 11:14:21 -060083 struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
84
Wu Fengguang079d88c2010-03-08 10:44:23 +080085 int num_pins;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
87 struct hda_pcm pcm_rec[MAX_HDMI_PINS];
Takashi Iwaid45e6882012-07-31 11:36:00 +020088 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +080089
90 /*
Stephen Warren384a48d2011-06-01 11:14:21 -060091 * Non-generic ATI/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +080092 */
93 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +020094 struct hda_pcm_stream pcm_playback;
Wu Fengguang079d88c2010-03-08 10:44:23 +080095};
96
97
98struct hdmi_audio_infoframe {
99 u8 type; /* 0x84 */
100 u8 ver; /* 0x01 */
101 u8 len; /* 0x0a */
102
Wu Fengguang53d7d692010-09-21 14:25:49 +0800103 u8 checksum;
104
Wu Fengguang079d88c2010-03-08 10:44:23 +0800105 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
106 u8 SS01_SF24;
107 u8 CXT04;
108 u8 CA;
109 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800110};
111
112struct dp_audio_infoframe {
113 u8 type; /* 0x84 */
114 u8 len; /* 0x1b */
115 u8 ver; /* 0x11 << 2 */
116
117 u8 CC02_CT47; /* match with HDMI infoframe from this on */
118 u8 SS01_SF24;
119 u8 CXT04;
120 u8 CA;
121 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800122};
123
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100124union audio_infoframe {
125 struct hdmi_audio_infoframe hdmi;
126 struct dp_audio_infoframe dp;
127 u8 bytes[0];
128};
129
Wu Fengguang079d88c2010-03-08 10:44:23 +0800130/*
131 * CEA speaker placement:
132 *
133 * FLH FCH FRH
134 * FLW FL FLC FC FRC FR FRW
135 *
136 * LFE
137 * TC
138 *
139 * RL RLC RC RRC RR
140 *
141 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
142 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
143 */
144enum cea_speaker_placement {
145 FL = (1 << 0), /* Front Left */
146 FC = (1 << 1), /* Front Center */
147 FR = (1 << 2), /* Front Right */
148 FLC = (1 << 3), /* Front Left Center */
149 FRC = (1 << 4), /* Front Right Center */
150 RL = (1 << 5), /* Rear Left */
151 RC = (1 << 6), /* Rear Center */
152 RR = (1 << 7), /* Rear Right */
153 RLC = (1 << 8), /* Rear Left Center */
154 RRC = (1 << 9), /* Rear Right Center */
155 LFE = (1 << 10), /* Low Frequency Effect */
156 FLW = (1 << 11), /* Front Left Wide */
157 FRW = (1 << 12), /* Front Right Wide */
158 FLH = (1 << 13), /* Front Left High */
159 FCH = (1 << 14), /* Front Center High */
160 FRH = (1 << 15), /* Front Right High */
161 TC = (1 << 16), /* Top Center */
162};
163
164/*
165 * ELD SA bits in the CEA Speaker Allocation data block
166 */
167static int eld_speaker_allocation_bits[] = {
168 [0] = FL | FR,
169 [1] = LFE,
170 [2] = FC,
171 [3] = RL | RR,
172 [4] = RC,
173 [5] = FLC | FRC,
174 [6] = RLC | RRC,
175 /* the following are not defined in ELD yet */
176 [7] = FLW | FRW,
177 [8] = FLH | FRH,
178 [9] = TC,
179 [10] = FCH,
180};
181
182struct cea_channel_speaker_allocation {
183 int ca_index;
184 int speakers[8];
185
186 /* derived values, just for convenience */
187 int channels;
188 int spk_mask;
189};
190
191/*
192 * ALSA sequence is:
193 *
194 * surround40 surround41 surround50 surround51 surround71
195 * ch0 front left = = = =
196 * ch1 front right = = = =
197 * ch2 rear left = = = =
198 * ch3 rear right = = = =
199 * ch4 LFE center center center
200 * ch5 LFE LFE
201 * ch6 side left
202 * ch7 side right
203 *
204 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
205 */
206static int hdmi_channel_mapping[0x32][8] = {
207 /* stereo */
208 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
209 /* 2.1 */
210 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211 /* Dolby Surround */
212 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
213 /* surround40 */
214 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
215 /* 4ch */
216 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
217 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800218 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800219 /* surround50 */
220 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
221 /* surround51 */
222 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
223 /* 7.1 */
224 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
225};
226
227/*
228 * This is an ordered list!
229 *
230 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800231 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800232 */
233static struct cea_channel_speaker_allocation channel_allocations[] = {
234/* channel: 7 6 5 4 3 2 1 0 */
235{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
236 /* 2.1 */
237{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
238 /* Dolby Surround */
239{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
240 /* surround40 */
241{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
242 /* surround41 */
243{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
244 /* surround50 */
245{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
246 /* surround51 */
247{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
248 /* 6.1 */
249{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
250 /* surround71 */
251{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
252
253{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
254{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
255{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
256{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
257{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
258{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
259{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
260{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
261{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
262{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
263{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
264{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
265{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
266{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
267{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
268{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
269{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
270{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
271{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
272{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
273{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
274{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
275{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
276{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
277{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
278{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
279{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
280{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
281{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
282{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
283{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
284{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
285{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
286{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
287{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
288{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
289{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
290{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
291{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
292{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
293{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
294};
295
296
297/*
298 * HDMI routines
299 */
300
Stephen Warren384a48d2011-06-01 11:14:21 -0600301static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800302{
Stephen Warren384a48d2011-06-01 11:14:21 -0600303 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800304
Stephen Warren384a48d2011-06-01 11:14:21 -0600305 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
306 if (spec->pins[pin_idx].pin_nid == pin_nid)
307 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308
Stephen Warren384a48d2011-06-01 11:14:21 -0600309 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
310 return -EINVAL;
311}
312
313static int hinfo_to_pin_index(struct hdmi_spec *spec,
314 struct hda_pcm_stream *hinfo)
315{
316 int pin_idx;
317
318 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
319 if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
320 return pin_idx;
321
322 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
323 return -EINVAL;
324}
325
326static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
327{
328 int cvt_idx;
329
330 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
331 if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
332 return cvt_idx;
333
334 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800335 return -EINVAL;
336}
337
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500338static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_info *uinfo)
340{
341 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
342 struct hdmi_spec *spec;
343 int pin_idx;
344
345 spec = codec->spec;
346 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
347
348 pin_idx = kcontrol->private_value;
349 uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
350
351 return 0;
352}
353
354static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
355 struct snd_ctl_elem_value *ucontrol)
356{
357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
358 struct hdmi_spec *spec;
359 int pin_idx;
360
361 spec = codec->spec;
362 pin_idx = kcontrol->private_value;
363
364 memcpy(ucontrol->value.bytes.data,
365 spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
366
367 return 0;
368}
369
370static struct snd_kcontrol_new eld_bytes_ctl = {
371 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
372 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
373 .name = "ELD",
374 .info = hdmi_eld_ctl_info,
375 .get = hdmi_eld_ctl_get,
376};
377
378static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
379 int device)
380{
381 struct snd_kcontrol *kctl;
382 struct hdmi_spec *spec = codec->spec;
383 int err;
384
385 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
386 if (!kctl)
387 return -ENOMEM;
388 kctl->private_value = pin_idx;
389 kctl->id.device = device;
390
391 err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
392 if (err < 0)
393 return err;
394
395 return 0;
396}
397
Wu Fengguang079d88c2010-03-08 10:44:23 +0800398#ifdef BE_PARANOID
399static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
400 int *packet_index, int *byte_index)
401{
402 int val;
403
404 val = snd_hda_codec_read(codec, pin_nid, 0,
405 AC_VERB_GET_HDMI_DIP_INDEX, 0);
406
407 *packet_index = val >> 5;
408 *byte_index = val & 0x1f;
409}
410#endif
411
412static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
413 int packet_index, int byte_index)
414{
415 int val;
416
417 val = (packet_index << 5) | (byte_index & 0x1f);
418
419 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
420}
421
422static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
423 unsigned char val)
424{
425 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
426}
427
Stephen Warren384a48d2011-06-01 11:14:21 -0600428static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800429{
430 /* Unmute */
431 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
432 snd_hda_codec_write(codec, pin_nid, 0,
433 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Takashi Iwai6169b672012-12-14 10:22:35 +0100434 /* Enable pin out: some machines with GM965 gets broken output when
435 * the pin is disabled or changed while using with HDMI
436 */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800437 snd_hda_codec_write(codec, pin_nid, 0,
Takashi Iwai6169b672012-12-14 10:22:35 +0100438 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800439}
440
Stephen Warren384a48d2011-06-01 11:14:21 -0600441static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800442{
Stephen Warren384a48d2011-06-01 11:14:21 -0600443 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800444 AC_VERB_GET_CVT_CHAN_COUNT, 0);
445}
446
447static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600448 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800449{
Stephen Warren384a48d2011-06-01 11:14:21 -0600450 if (chs != hdmi_get_channel_count(codec, cvt_nid))
451 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800452 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
453}
454
455
456/*
457 * Channel mapping routines
458 */
459
460/*
461 * Compute derived values in channel_allocations[].
462 */
463static void init_channel_allocations(void)
464{
465 int i, j;
466 struct cea_channel_speaker_allocation *p;
467
468 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
469 p = channel_allocations + i;
470 p->channels = 0;
471 p->spk_mask = 0;
472 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
473 if (p->speakers[j]) {
474 p->channels++;
475 p->spk_mask |= p->speakers[j];
476 }
477 }
478}
479
Wang Xingchao72357c72012-09-06 10:02:36 +0800480static int get_channel_allocation_order(int ca)
481{
482 int i;
483
484 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
485 if (channel_allocations[i].ca_index == ca)
486 break;
487 }
488 return i;
489}
490
Wu Fengguang079d88c2010-03-08 10:44:23 +0800491/*
492 * The transformation takes two steps:
493 *
494 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
495 * spk_mask => (channel_allocations[]) => ai->CA
496 *
497 * TODO: it could select the wrong CA from multiple candidates.
498*/
Stephen Warren384a48d2011-06-01 11:14:21 -0600499static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800500{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800501 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800502 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800503 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800504 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
505
506 /*
507 * CA defaults to 0 for basic stereo audio
508 */
509 if (channels <= 2)
510 return 0;
511
Wu Fengguang079d88c2010-03-08 10:44:23 +0800512 /*
513 * expand ELD's speaker allocation mask
514 *
515 * ELD tells the speaker mask in a compact(paired) form,
516 * expand ELD's notions to match the ones used by Audio InfoFrame.
517 */
518 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
519 if (eld->spk_alloc & (1 << i))
520 spk_mask |= eld_speaker_allocation_bits[i];
521 }
522
523 /* search for the first working match in the CA table */
524 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
525 if (channels == channel_allocations[i].channels &&
526 (spk_mask & channel_allocations[i].spk_mask) ==
527 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800528 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800529 break;
530 }
531 }
532
533 snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
Wu Fengguang2abbf432010-03-08 10:45:38 +0800534 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800535 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800536
Wu Fengguang53d7d692010-09-21 14:25:49 +0800537 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800538}
539
540static void hdmi_debug_channel_mapping(struct hda_codec *codec,
541 hda_nid_t pin_nid)
542{
543#ifdef CONFIG_SND_DEBUG_VERBOSE
544 int i;
545 int slot;
546
547 for (i = 0; i < 8; i++) {
548 slot = snd_hda_codec_read(codec, pin_nid, 0,
549 AC_VERB_GET_HDMI_CHAN_SLOT, i);
550 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
551 slot >> 4, slot & 0xf);
552 }
553#endif
554}
555
556
Takashi Iwaid45e6882012-07-31 11:36:00 +0200557static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800558 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800559 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800560 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800561{
562 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800563 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800564 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800565 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800566
Wang Xingchao72357c72012-09-06 10:02:36 +0800567 order = get_channel_allocation_order(ca);
Wang Xingchao433968d2012-09-06 10:02:37 +0800568
Wu Fengguang079d88c2010-03-08 10:44:23 +0800569 if (hdmi_channel_mapping[ca][1] == 0) {
Wang Xingchao72357c72012-09-06 10:02:36 +0800570 for (i = 0; i < channel_allocations[order].channels; i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800571 hdmi_channel_mapping[ca][i] = i | (i << 4);
572 for (; i < 8; i++)
573 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
574 }
575
Wang Xingchao433968d2012-09-06 10:02:37 +0800576 if (non_pcm) {
577 for (i = 0; i < channel_allocations[order].channels; i++)
578 non_pcm_mapping[i] = i | (i << 4);
579 for (; i < 8; i++)
580 non_pcm_mapping[i] = 0xf | (i << 4);
581 }
582
Wu Fengguang079d88c2010-03-08 10:44:23 +0800583 for (i = 0; i < 8; i++) {
584 err = snd_hda_codec_write(codec, pin_nid, 0,
585 AC_VERB_SET_HDMI_CHAN_SLOT,
Wang Xingchao433968d2012-09-06 10:02:37 +0800586 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800587 if (err) {
Wu Fengguang2abbf432010-03-08 10:45:38 +0800588 snd_printdd(KERN_NOTICE
589 "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800590 break;
591 }
592 }
593
594 hdmi_debug_channel_mapping(codec, pin_nid);
595}
596
Takashi Iwaid45e6882012-07-31 11:36:00 +0200597struct channel_map_table {
598 unsigned char map; /* ALSA API channel map position */
599 unsigned char cea_slot; /* CEA slot value */
600 int spk_mask; /* speaker position bit mask */
601};
602
603static struct channel_map_table map_tables[] = {
604 { SNDRV_CHMAP_FL, 0x00, FL },
605 { SNDRV_CHMAP_FR, 0x01, FR },
606 { SNDRV_CHMAP_RL, 0x04, RL },
607 { SNDRV_CHMAP_RR, 0x05, RR },
608 { SNDRV_CHMAP_LFE, 0x02, LFE },
609 { SNDRV_CHMAP_FC, 0x03, FC },
610 { SNDRV_CHMAP_RLC, 0x06, RLC },
611 { SNDRV_CHMAP_RRC, 0x07, RRC },
612 {} /* terminator */
613};
614
615/* from ALSA API channel position to speaker bit mask */
616static int to_spk_mask(unsigned char c)
617{
618 struct channel_map_table *t = map_tables;
619 for (; t->map; t++) {
620 if (t->map == c)
621 return t->spk_mask;
622 }
623 return 0;
624}
625
626/* from ALSA API channel position to CEA slot */
627static int to_cea_slot(unsigned char c)
628{
629 struct channel_map_table *t = map_tables;
630 for (; t->map; t++) {
631 if (t->map == c)
632 return t->cea_slot;
633 }
634 return 0x0f;
635}
636
637/* from CEA slot to ALSA API channel position */
638static int from_cea_slot(unsigned char c)
639{
640 struct channel_map_table *t = map_tables;
641 for (; t->map; t++) {
642 if (t->cea_slot == c)
643 return t->map;
644 }
645 return 0;
646}
647
648/* from speaker bit mask to ALSA API channel position */
649static int spk_to_chmap(int spk)
650{
651 struct channel_map_table *t = map_tables;
652 for (; t->map; t++) {
653 if (t->spk_mask == spk)
654 return t->map;
655 }
656 return 0;
657}
658
659/* get the CA index corresponding to the given ALSA API channel map */
660static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
661{
662 int i, spks = 0, spk_mask = 0;
663
664 for (i = 0; i < chs; i++) {
665 int mask = to_spk_mask(map[i]);
666 if (mask) {
667 spk_mask |= mask;
668 spks++;
669 }
670 }
671
672 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
673 if ((chs == channel_allocations[i].channels ||
674 spks == channel_allocations[i].channels) &&
675 (spk_mask & channel_allocations[i].spk_mask) ==
676 channel_allocations[i].spk_mask)
677 return channel_allocations[i].ca_index;
678 }
679 return -1;
680}
681
682/* set up the channel slots for the given ALSA API channel map */
683static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
684 hda_nid_t pin_nid,
685 int chs, unsigned char *map)
686{
687 int i;
688 for (i = 0; i < 8; i++) {
689 int val, err;
690 if (i < chs)
691 val = to_cea_slot(map[i]);
692 else
693 val = 0xf;
694 val |= (i << 4);
695 err = snd_hda_codec_write(codec, pin_nid, 0,
696 AC_VERB_SET_HDMI_CHAN_SLOT, val);
697 if (err)
698 return -EINVAL;
699 }
700 return 0;
701}
702
703/* store ALSA API channel map from the current default map */
704static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
705{
706 int i;
707 for (i = 0; i < 8; i++) {
708 if (i < channel_allocations[ca].channels)
709 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
710 else
711 map[i] = 0;
712 }
713}
714
715static void hdmi_setup_channel_mapping(struct hda_codec *codec,
716 hda_nid_t pin_nid, bool non_pcm, int ca,
717 int channels, unsigned char *map)
718{
719 if (!non_pcm && map) {
720 hdmi_manual_setup_channel_mapping(codec, pin_nid,
721 channels, map);
722 } else {
723 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
724 hdmi_setup_fake_chmap(map, ca);
725 }
726}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800727
728/*
729 * Audio InfoFrame routines
730 */
731
732/*
733 * Enable Audio InfoFrame Transmission
734 */
735static void hdmi_start_infoframe_trans(struct hda_codec *codec,
736 hda_nid_t pin_nid)
737{
738 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
739 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
740 AC_DIPXMIT_BEST);
741}
742
743/*
744 * Disable Audio InfoFrame Transmission
745 */
746static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
747 hda_nid_t pin_nid)
748{
749 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
750 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
751 AC_DIPXMIT_DISABLE);
752}
753
754static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
755{
756#ifdef CONFIG_SND_DEBUG_VERBOSE
757 int i;
758 int size;
759
760 size = snd_hdmi_get_eld_size(codec, pin_nid);
761 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
762
763 for (i = 0; i < 8; i++) {
764 size = snd_hda_codec_read(codec, pin_nid, 0,
765 AC_VERB_GET_HDMI_DIP_SIZE, i);
766 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
767 }
768#endif
769}
770
771static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
772{
773#ifdef BE_PARANOID
774 int i, j;
775 int size;
776 int pi, bi;
777 for (i = 0; i < 8; i++) {
778 size = snd_hda_codec_read(codec, pin_nid, 0,
779 AC_VERB_GET_HDMI_DIP_SIZE, i);
780 if (size == 0)
781 continue;
782
783 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
784 for (j = 1; j < 1000; j++) {
785 hdmi_write_dip_byte(codec, pin_nid, 0x0);
786 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
787 if (pi != i)
788 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
789 bi, pi, i);
790 if (bi == 0) /* byte index wrapped around */
791 break;
792 }
793 snd_printd(KERN_INFO
794 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
795 i, size, j);
796 }
797#endif
798}
799
Wu Fengguang53d7d692010-09-21 14:25:49 +0800800static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800801{
Wu Fengguang53d7d692010-09-21 14:25:49 +0800802 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800803 u8 sum = 0;
804 int i;
805
Wu Fengguang53d7d692010-09-21 14:25:49 +0800806 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800807
Wu Fengguang53d7d692010-09-21 14:25:49 +0800808 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800809 sum += bytes[i];
810
Wu Fengguang53d7d692010-09-21 14:25:49 +0800811 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800812}
813
814static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
815 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800816 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800817{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800818 int i;
819
820 hdmi_debug_dip_size(codec, pin_nid);
821 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
822
Wu Fengguang079d88c2010-03-08 10:44:23 +0800823 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800824 for (i = 0; i < size; i++)
825 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800826}
827
828static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800829 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800830{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800831 u8 val;
832 int i;
833
834 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
835 != AC_DIPXMIT_BEST)
836 return false;
837
838 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800839 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800840 val = snd_hda_codec_read(codec, pin_nid, 0,
841 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +0800842 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +0800843 return false;
844 }
845
846 return true;
847}
848
Stephen Warren384a48d2011-06-01 11:14:21 -0600849static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200850 bool non_pcm,
851 struct snd_pcm_substream *substream)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800852{
853 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600854 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
855 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800856 int channels = substream->runtime->channels;
Stephen Warren384a48d2011-06-01 11:14:21 -0600857 struct hdmi_eld *eld;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800858 int ca;
Takashi Iwai2b203dbb2011-02-11 12:17:30 +0100859 union audio_infoframe ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800860
Stephen Warren384a48d2011-06-01 11:14:21 -0600861 eld = &spec->pins[pin_idx].sink_eld;
862 if (!eld->monitor_present)
863 return;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800864
Takashi Iwaid45e6882012-07-31 11:36:00 +0200865 if (!non_pcm && per_pin->chmap_set)
866 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
867 else
868 ca = hdmi_channel_allocation(eld, channels);
869 if (ca < 0)
870 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800871
Stephen Warren384a48d2011-06-01 11:14:21 -0600872 memset(&ai, 0, sizeof(ai));
873 if (eld->conn_type == 0) { /* HDMI */
874 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800875
Stephen Warren384a48d2011-06-01 11:14:21 -0600876 hdmi_ai->type = 0x84;
877 hdmi_ai->ver = 0x01;
878 hdmi_ai->len = 0x0a;
879 hdmi_ai->CC02_CT47 = channels - 1;
880 hdmi_ai->CA = ca;
881 hdmi_checksum_audio_infoframe(hdmi_ai);
882 } else if (eld->conn_type == 1) { /* DisplayPort */
883 struct dp_audio_infoframe *dp_ai = &ai.dp;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800884
Stephen Warren384a48d2011-06-01 11:14:21 -0600885 dp_ai->type = 0x84;
886 dp_ai->len = 0x1b;
887 dp_ai->ver = 0x11 << 2;
888 dp_ai->CC02_CT47 = channels - 1;
889 dp_ai->CA = ca;
890 } else {
891 snd_printd("HDMI: unknown connection type at pin %d\n",
892 pin_nid);
893 return;
894 }
Wu Fengguang53d7d692010-09-21 14:25:49 +0800895
Stephen Warren384a48d2011-06-01 11:14:21 -0600896 /*
897 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
898 * sizeof(*dp_ai) to avoid partial match/update problems when
899 * the user switches between HDMI/DP monitors.
900 */
901 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
902 sizeof(ai))) {
903 snd_printdd("hdmi_setup_audio_infoframe: "
904 "pin=%d channels=%d\n",
905 pin_nid,
906 channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200907 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
908 channels, per_pin->chmap);
Stephen Warren384a48d2011-06-01 11:14:21 -0600909 hdmi_stop_infoframe_trans(codec, pin_nid);
910 hdmi_fill_audio_infoframe(codec, pin_nid,
911 ai.bytes, sizeof(ai));
912 hdmi_start_infoframe_trans(codec, pin_nid);
Wang Xingchao2d7e8872012-09-06 10:02:38 +0800913 } else {
914 /* For non-pcm audio switch, setup new channel mapping
915 * accordingly */
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200916 if (per_pin->non_pcm != non_pcm)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200917 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
918 channels, per_pin->chmap);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800919 }
Wang Xingchao433968d2012-09-06 10:02:37 +0800920
Takashi Iwai1a6003b2012-09-06 17:42:08 +0200921 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800922}
923
924
925/*
926 * Unsolicited events
927 */
928
Wu Fengguangc6e84532011-11-18 16:59:32 -0600929static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +0200930
Wu Fengguang079d88c2010-03-08 10:44:23 +0800931static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
932{
933 struct hdmi_spec *spec = codec->spec;
Takashi Iwai3a938972011-10-28 01:16:55 +0200934 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
935 int pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -0600936 int pin_idx;
Takashi Iwai3a938972011-10-28 01:16:55 +0200937 struct hda_jack_tbl *jack;
938
939 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
940 if (!jack)
941 return;
942 pin_nid = jack->nid;
943 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800944
Fengguang Wufae3d882012-04-10 17:00:35 +0800945 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -0600946 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Fengguang Wufae3d882012-04-10 17:00:35 +0800947 codec->addr, pin_nid,
948 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +0800949
Stephen Warren384a48d2011-06-01 11:14:21 -0600950 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
951 if (pin_idx < 0)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800952 return;
953
Wu Fengguangc6e84532011-11-18 16:59:32 -0600954 hdmi_present_sense(&spec->pins[pin_idx], 1);
Takashi Iwai01a61e12011-10-28 00:03:22 +0200955 snd_hda_jack_report_sync(codec);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800956}
957
958static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
959{
960 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
961 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
962 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
963 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
964
965 printk(KERN_INFO
Takashi Iwaie9ea8e82012-06-21 11:41:05 +0200966 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -0600967 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800968 tag,
969 subtag,
970 cp_state,
971 cp_ready);
972
973 /* TODO */
974 if (cp_state)
975 ;
976 if (cp_ready)
977 ;
978}
979
980
981static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
982{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800983 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
984 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
985
Takashi Iwai3a938972011-10-28 01:16:55 +0200986 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Wu Fengguang079d88c2010-03-08 10:44:23 +0800987 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
988 return;
989 }
990
991 if (subtag == 0)
992 hdmi_intrinsic_event(codec, res);
993 else
994 hdmi_non_intrinsic_event(codec, res);
995}
996
997/*
998 * Callbacks
999 */
1000
Takashi Iwai92f10b32010-08-03 14:21:00 +02001001/* HBR should be Non-PCM, 8 channels */
1002#define is_hbr_format(format) \
1003 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1004
Stephen Warren384a48d2011-06-01 11:14:21 -06001005static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1006 hda_nid_t pin_nid, u32 stream_tag, int format)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001007{
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001008 int pinctl;
1009 int new_pinctl = 0;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001010
Stephen Warren384a48d2011-06-01 11:14:21 -06001011 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1012 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001013 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1014
1015 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Takashi Iwai92f10b32010-08-03 14:21:00 +02001016 if (is_hbr_format(format))
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001017 new_pinctl |= AC_PINCTL_EPT_HBR;
1018 else
1019 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1020
1021 snd_printdd("hdmi_setup_stream: "
1022 "NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001023 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001024 pinctl == new_pinctl ? "" : "new-",
1025 new_pinctl);
1026
1027 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001028 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001029 AC_VERB_SET_PIN_WIDGET_CONTROL,
1030 new_pinctl);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001031
Stephen Warren384a48d2011-06-01 11:14:21 -06001032 }
Takashi Iwai92f10b32010-08-03 14:21:00 +02001033 if (is_hbr_format(format) && !new_pinctl) {
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001034 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1035 return -EINVAL;
1036 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001037
Stephen Warren384a48d2011-06-01 11:14:21 -06001038 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001039 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001040}
1041
1042/*
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001043 * HDA PCM callbacks
1044 */
1045static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1046 struct hda_codec *codec,
1047 struct snd_pcm_substream *substream)
1048{
1049 struct hdmi_spec *spec = codec->spec;
Takashi Iwai639cef02011-01-14 10:30:46 +01001050 struct snd_pcm_runtime *runtime = substream->runtime;
Stephen Warren384a48d2011-06-01 11:14:21 -06001051 int pin_idx, cvt_idx, mux_idx = 0;
1052 struct hdmi_spec_per_pin *per_pin;
1053 struct hdmi_eld *eld;
1054 struct hdmi_spec_per_cvt *per_cvt = NULL;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001055
Stephen Warren384a48d2011-06-01 11:14:21 -06001056 /* Validate hinfo */
1057 pin_idx = hinfo_to_pin_index(spec, hinfo);
1058 if (snd_BUG_ON(pin_idx < 0))
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001059 return -EINVAL;
Stephen Warren384a48d2011-06-01 11:14:21 -06001060 per_pin = &spec->pins[pin_idx];
1061 eld = &per_pin->sink_eld;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001062
Stephen Warren384a48d2011-06-01 11:14:21 -06001063 /* Dynamically assign converter to stream */
1064 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1065 per_cvt = &spec->cvts[cvt_idx];
1066
1067 /* Must not already be assigned */
1068 if (per_cvt->assigned)
1069 continue;
1070 /* Must be in pin's mux's list of converters */
1071 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1072 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1073 break;
1074 /* Not in mux list */
1075 if (mux_idx == per_pin->num_mux_nids)
1076 continue;
1077 break;
1078 }
1079 /* No free converters */
1080 if (cvt_idx == spec->num_cvts)
1081 return -ENODEV;
1082
1083 /* Claim converter */
1084 per_cvt->assigned = 1;
1085 hinfo->nid = per_cvt->cvt_nid;
1086
1087 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1088 AC_VERB_SET_CONNECT_SEL,
1089 mux_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001090 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001091
Stephen Warren2def8172011-06-01 11:14:20 -06001092 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001093 hinfo->channels_min = per_cvt->channels_min;
1094 hinfo->channels_max = per_cvt->channels_max;
1095 hinfo->rates = per_cvt->rates;
1096 hinfo->formats = per_cvt->formats;
1097 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001098
Stephen Warren384a48d2011-06-01 11:14:21 -06001099 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001100 if (!static_hdmi_pcm && eld->eld_valid) {
Stephen Warren2def8172011-06-01 11:14:20 -06001101 snd_hdmi_eld_update_pcm_info(eld, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001102 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001103 !hinfo->rates || !hinfo->formats) {
1104 per_cvt->assigned = 0;
1105 hinfo->nid = 0;
1106 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001107 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001108 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001109 }
Stephen Warren2def8172011-06-01 11:14:20 -06001110
1111 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001112 runtime->hw.channels_min = hinfo->channels_min;
1113 runtime->hw.channels_max = hinfo->channels_max;
1114 runtime->hw.formats = hinfo->formats;
1115 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001116
1117 snd_pcm_hw_constraint_step(substream->runtime, 0,
1118 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001119 return 0;
1120}
1121
1122/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001123 * HDA/HDMI auto parsing
1124 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001125static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001126{
1127 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001128 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1129 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001130
1131 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1132 snd_printk(KERN_WARNING
1133 "HDMI: pin %d wcaps %#x "
1134 "does not support connection list\n",
1135 pin_nid, get_wcaps(codec, pin_nid));
1136 return -EINVAL;
1137 }
1138
Stephen Warren384a48d2011-06-01 11:14:21 -06001139 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1140 per_pin->mux_nids,
1141 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001142
1143 return 0;
1144}
1145
Wu Fengguangc6e84532011-11-18 16:59:32 -06001146static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001147{
Wu Fengguang744626d2011-11-16 16:29:47 +08001148 struct hda_codec *codec = per_pin->codec;
1149 struct hdmi_eld *eld = &per_pin->sink_eld;
1150 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001151 /*
1152 * Always execute a GetPinSense verb here, even when called from
1153 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1154 * response's PD bit is not the real PD value, but indicates that
1155 * the real PD value changed. An older version of the HD-audio
1156 * specification worked this way. Hence, we just ignore the data in
1157 * the unsolicited response to avoid custom WARs.
1158 */
Wu Fengguang079d88c2010-03-08 10:44:23 +08001159 int present = snd_hda_pin_sense(codec, pin_nid);
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001160 bool eld_valid = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001161
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001162 memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001163
Stephen Warren5d44f922011-05-24 17:11:17 -06001164 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1165 if (eld->monitor_present)
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001166 eld_valid = !!(present & AC_PINSENSE_ELDV);
Stephen Warren5d44f922011-05-24 17:11:17 -06001167
Fengguang Wufae3d882012-04-10 17:00:35 +08001168 _snd_printd(SND_PR_VERBOSE,
Stephen Warren384a48d2011-06-01 11:14:21 -06001169 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Wu Fengguangb95d68b2011-11-16 16:29:46 +08001170 codec->addr, pin_nid, eld->monitor_present, eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001171
Wu Fengguang744626d2011-11-16 16:29:47 +08001172 if (eld_valid) {
Stephen Warren5d44f922011-05-24 17:11:17 -06001173 if (!snd_hdmi_get_eld(eld, codec, pin_nid))
1174 snd_hdmi_show_eld(eld);
Wu Fengguangc6e84532011-11-18 16:59:32 -06001175 else if (repoll) {
Wu Fengguang744626d2011-11-16 16:29:47 +08001176 queue_delayed_work(codec->bus->workq,
1177 &per_pin->work,
1178 msecs_to_jiffies(300));
1179 }
1180 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001181}
1182
Wu Fengguang744626d2011-11-16 16:29:47 +08001183static void hdmi_repoll_eld(struct work_struct *work)
1184{
1185 struct hdmi_spec_per_pin *per_pin =
1186 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1187
Wu Fengguangc6e84532011-11-18 16:59:32 -06001188 if (per_pin->repoll_count++ > 6)
1189 per_pin->repoll_count = 0;
1190
1191 hdmi_present_sense(per_pin, per_pin->repoll_count);
Wu Fengguang744626d2011-11-16 16:29:47 +08001192}
1193
Wu Fengguang079d88c2010-03-08 10:44:23 +08001194static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1195{
1196 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001197 unsigned int caps, config;
1198 int pin_idx;
1199 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001200 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001201
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001202 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001203 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1204 return 0;
1205
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001206 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001207 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1208 return 0;
1209
1210 if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
Wu Fengguang3eaead52010-05-14 16:36:15 +08001211 return -E2BIG;
Stephen Warren384a48d2011-06-01 11:14:21 -06001212
1213 pin_idx = spec->num_pins;
1214 per_pin = &spec->pins[pin_idx];
Stephen Warren384a48d2011-06-01 11:14:21 -06001215
1216 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001217 per_pin->non_pcm = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001218
Stephen Warren384a48d2011-06-01 11:14:21 -06001219 err = hdmi_read_pin_conn(codec, pin_idx);
1220 if (err < 0)
1221 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001222
Wu Fengguang079d88c2010-03-08 10:44:23 +08001223 spec->num_pins++;
1224
Stephen Warren384a48d2011-06-01 11:14:21 -06001225 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001226}
1227
Stephen Warren384a48d2011-06-01 11:14:21 -06001228static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001229{
1230 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001231 int cvt_idx;
1232 struct hdmi_spec_per_cvt *per_cvt;
1233 unsigned int chans;
1234 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001235
David Henningsson116dcde2010-11-23 10:23:40 +01001236 if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
1237 return -E2BIG;
1238
Stephen Warren384a48d2011-06-01 11:14:21 -06001239 chans = get_wcaps(codec, cvt_nid);
1240 chans = get_wcaps_channels(chans);
1241
1242 cvt_idx = spec->num_cvts;
1243 per_cvt = &spec->cvts[cvt_idx];
1244
1245 per_cvt->cvt_nid = cvt_nid;
1246 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001247 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001248 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001249 if (chans > spec->channels_max)
1250 spec->channels_max = chans;
1251 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001252
1253 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1254 &per_cvt->rates,
1255 &per_cvt->formats,
1256 &per_cvt->maxbps);
1257 if (err < 0)
1258 return err;
1259
Wu Fengguang079d88c2010-03-08 10:44:23 +08001260 spec->num_cvts++;
1261
1262 return 0;
1263}
1264
1265static int hdmi_parse_codec(struct hda_codec *codec)
1266{
1267 hda_nid_t nid;
1268 int i, nodes;
1269
1270 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1271 if (!nid || nodes < 0) {
1272 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1273 return -EINVAL;
1274 }
1275
1276 for (i = 0; i < nodes; i++, nid++) {
1277 unsigned int caps;
1278 unsigned int type;
1279
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001280 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001281 type = get_wcaps_type(caps);
1282
1283 if (!(caps & AC_WCAP_DIGITAL))
1284 continue;
1285
1286 switch (type) {
1287 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06001288 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001289 break;
1290 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08001291 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001292 break;
1293 }
1294 }
1295
David Henningssonc9adeef2012-11-07 09:22:33 +01001296#ifdef CONFIG_PM
1297 /* We're seeing some problems with unsolicited hot plug events on
1298 * PantherPoint after S3, if this is not enabled */
1299 if (codec->vendor_id == 0x80862806)
1300 codec->bus->power_keep_link_on = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001301 /*
1302 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1303 * can be lost and presence sense verb will become inaccurate if the
1304 * HDA link is powered off at hot plug or hw initialization time.
1305 */
David Henningssonc9adeef2012-11-07 09:22:33 +01001306 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
Wu Fengguang079d88c2010-03-08 10:44:23 +08001307 AC_PWRST_EPSS))
1308 codec->bus->power_keep_link_on = 1;
1309#endif
1310
1311 return 0;
1312}
1313
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001314/*
1315 */
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001316static char *get_hdmi_pcm_name(int idx)
1317{
1318 static char names[MAX_HDMI_PINS][8];
1319 sprintf(&names[idx][0], "HDMI %d", idx);
1320 return &names[idx][0];
1321}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001322
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001323static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1324{
1325 struct hda_spdif_out *spdif;
1326 bool non_pcm;
1327
1328 mutex_lock(&codec->spdif_mutex);
1329 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1330 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1331 mutex_unlock(&codec->spdif_mutex);
1332 return non_pcm;
1333}
1334
1335
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001336/*
1337 * HDMI callbacks
1338 */
1339
1340static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1341 struct hda_codec *codec,
1342 unsigned int stream_tag,
1343 unsigned int format,
1344 struct snd_pcm_substream *substream)
1345{
Stephen Warren384a48d2011-06-01 11:14:21 -06001346 hda_nid_t cvt_nid = hinfo->nid;
1347 struct hdmi_spec *spec = codec->spec;
1348 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1349 hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001350 bool non_pcm;
1351
1352 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001353
Stephen Warren384a48d2011-06-01 11:14:21 -06001354 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001355
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001356 hdmi_setup_audio_infoframe(codec, pin_idx, non_pcm, substream);
Stephen Warren384a48d2011-06-01 11:14:21 -06001357
1358 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001359}
1360
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001361static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1362 struct hda_codec *codec,
1363 struct snd_pcm_substream *substream)
1364{
1365 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1366 return 0;
1367}
1368
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001369static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1370 struct hda_codec *codec,
1371 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06001372{
1373 struct hdmi_spec *spec = codec->spec;
1374 int cvt_idx, pin_idx;
1375 struct hdmi_spec_per_cvt *per_cvt;
1376 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001377
Stephen Warren384a48d2011-06-01 11:14:21 -06001378 if (hinfo->nid) {
1379 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1380 if (snd_BUG_ON(cvt_idx < 0))
1381 return -EINVAL;
1382 per_cvt = &spec->cvts[cvt_idx];
1383
1384 snd_BUG_ON(!per_cvt->assigned);
1385 per_cvt->assigned = 0;
1386 hinfo->nid = 0;
1387
1388 pin_idx = hinfo_to_pin_index(spec, hinfo);
1389 if (snd_BUG_ON(pin_idx < 0))
1390 return -EINVAL;
1391 per_pin = &spec->pins[pin_idx];
1392
Stephen Warren384a48d2011-06-01 11:14:21 -06001393 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001394 per_pin->chmap_set = false;
1395 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Stephen Warren384a48d2011-06-01 11:14:21 -06001396 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02001397
Stephen Warren384a48d2011-06-01 11:14:21 -06001398 return 0;
1399}
1400
1401static const struct hda_pcm_ops generic_ops = {
1402 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02001403 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06001404 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02001405 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001406};
1407
Takashi Iwaid45e6882012-07-31 11:36:00 +02001408/*
1409 * ALSA API channel-map control callbacks
1410 */
1411static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1412 struct snd_ctl_elem_info *uinfo)
1413{
1414 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1415 struct hda_codec *codec = info->private_data;
1416 struct hdmi_spec *spec = codec->spec;
1417 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1418 uinfo->count = spec->channels_max;
1419 uinfo->value.integer.min = 0;
1420 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1421 return 0;
1422}
1423
1424static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1425 unsigned int size, unsigned int __user *tlv)
1426{
1427 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1428 struct hda_codec *codec = info->private_data;
1429 struct hdmi_spec *spec = codec->spec;
1430 const unsigned int valid_mask =
1431 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1432 unsigned int __user *dst;
1433 int chs, count = 0;
1434
1435 if (size < 8)
1436 return -ENOMEM;
1437 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1438 return -EFAULT;
1439 size -= 8;
1440 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02001441 for (chs = 2; chs <= spec->channels_max; chs++) {
Takashi Iwaid45e6882012-07-31 11:36:00 +02001442 int i, c;
1443 struct cea_channel_speaker_allocation *cap;
1444 cap = channel_allocations;
1445 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1446 int chs_bytes = chs * 4;
1447 if (cap->channels != chs)
1448 continue;
1449 if (cap->spk_mask & ~valid_mask)
1450 continue;
1451 if (size < 8)
1452 return -ENOMEM;
1453 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1454 put_user(chs_bytes, dst + 1))
1455 return -EFAULT;
1456 dst += 2;
1457 size -= 8;
1458 count += 8;
1459 if (size < chs_bytes)
1460 return -ENOMEM;
1461 size -= chs_bytes;
1462 count += chs_bytes;
1463 for (c = 7; c >= 0; c--) {
1464 int spk = cap->speakers[c];
1465 if (!spk)
1466 continue;
1467 if (put_user(spk_to_chmap(spk), dst))
1468 return -EFAULT;
1469 dst++;
1470 }
1471 }
1472 }
1473 if (put_user(count, tlv + 1))
1474 return -EFAULT;
1475 return 0;
1476}
1477
1478static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
1480{
1481 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1482 struct hda_codec *codec = info->private_data;
1483 struct hdmi_spec *spec = codec->spec;
1484 int pin_idx = kcontrol->private_value;
1485 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1486 int i;
1487
1488 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1489 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1490 return 0;
1491}
1492
1493static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_value *ucontrol)
1495{
1496 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1497 struct hda_codec *codec = info->private_data;
1498 struct hdmi_spec *spec = codec->spec;
1499 int pin_idx = kcontrol->private_value;
1500 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1501 unsigned int ctl_idx;
1502 struct snd_pcm_substream *substream;
1503 unsigned char chmap[8];
1504 int i, ca, prepared = 0;
1505
1506 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1507 substream = snd_pcm_chmap_substream(info, ctl_idx);
1508 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01001509 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02001510 switch (substream->runtime->status->state) {
1511 case SNDRV_PCM_STATE_OPEN:
1512 case SNDRV_PCM_STATE_SETUP:
1513 break;
1514 case SNDRV_PCM_STATE_PREPARED:
1515 prepared = 1;
1516 break;
1517 default:
1518 return -EBUSY;
1519 }
1520 memset(chmap, 0, sizeof(chmap));
1521 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1522 chmap[i] = ucontrol->value.integer.value[i];
1523 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1524 return 0;
1525 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1526 if (ca < 0)
1527 return -EINVAL;
1528 per_pin->chmap_set = true;
1529 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1530 if (prepared)
1531 hdmi_setup_audio_infoframe(codec, pin_idx, per_pin->non_pcm,
1532 substream);
1533
1534 return 0;
1535}
1536
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001537static int generic_hdmi_build_pcms(struct hda_codec *codec)
1538{
1539 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001540 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001541
Stephen Warren384a48d2011-06-01 11:14:21 -06001542 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1543 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001544 struct hda_pcm_stream *pstr;
1545
Stephen Warren384a48d2011-06-01 11:14:21 -06001546 info = &spec->pcm_rec[pin_idx];
Takashi Iwaia4567cb2011-11-24 14:44:19 +01001547 info->name = get_hdmi_pcm_name(pin_idx);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001548 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001549 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06001550
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001551 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06001552 pstr->substreams = 1;
1553 pstr->ops = generic_ops;
1554 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001555 }
1556
Stephen Warren384a48d2011-06-01 11:14:21 -06001557 codec->num_pcms = spec->num_pins;
1558 codec->pcm_info = spec->pcm_rec;
1559
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001560 return 0;
1561}
1562
David Henningsson0b6c49b2011-08-23 16:56:03 +02001563static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1564{
Takashi Iwai31ef2252011-12-01 17:41:36 +01001565 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02001566 struct hdmi_spec *spec = codec->spec;
1567 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1568 int pcmdev = spec->pcm_rec[pin_idx].device;
1569
Takashi Iwai31ef2252011-12-01 17:41:36 +01001570 if (pcmdev > 0)
1571 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001572
Takashi Iwai31ef2252011-12-01 17:41:36 +01001573 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
David Henningsson0b6c49b2011-08-23 16:56:03 +02001574}
1575
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001576static int generic_hdmi_build_controls(struct hda_codec *codec)
1577{
1578 struct hdmi_spec *spec = codec->spec;
1579 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001580 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001581
Stephen Warren384a48d2011-06-01 11:14:21 -06001582 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1583 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
David Henningsson0b6c49b2011-08-23 16:56:03 +02001584
1585 err = generic_hdmi_build_jack(codec, pin_idx);
1586 if (err < 0)
1587 return err;
1588
Takashi Iwaidcda5802012-10-12 17:24:51 +02001589 err = snd_hda_create_dig_out_ctls(codec,
1590 per_pin->pin_nid,
1591 per_pin->mux_nids[0],
1592 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001593 if (err < 0)
1594 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06001595 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05001596
1597 /* add control for ELD Bytes */
1598 err = hdmi_create_eld_ctl(codec,
1599 pin_idx,
1600 spec->pcm_rec[pin_idx].device);
1601
1602 if (err < 0)
1603 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01001604
Takashi Iwai82b1d732011-12-20 15:53:07 +01001605 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001606 }
1607
Takashi Iwaid45e6882012-07-31 11:36:00 +02001608 /* add channel maps */
1609 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1610 struct snd_pcm_chmap *chmap;
1611 struct snd_kcontrol *kctl;
1612 int i;
1613 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1614 SNDRV_PCM_STREAM_PLAYBACK,
1615 NULL, 0, pin_idx, &chmap);
1616 if (err < 0)
1617 return err;
1618 /* override handlers */
1619 chmap->private_data = codec;
1620 kctl = chmap->kctl;
1621 for (i = 0; i < kctl->count; i++)
1622 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1623 kctl->info = hdmi_chmap_ctl_info;
1624 kctl->get = hdmi_chmap_ctl_get;
1625 kctl->put = hdmi_chmap_ctl_put;
1626 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1627 }
1628
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001629 return 0;
1630}
1631
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001632static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1633{
1634 struct hdmi_spec *spec = codec->spec;
1635 int pin_idx;
1636
1637 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1638 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1639 struct hdmi_eld *eld = &per_pin->sink_eld;
1640
1641 per_pin->codec = codec;
1642 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1643 snd_hda_eld_proc_new(codec, eld, pin_idx);
1644 }
1645 return 0;
1646}
1647
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001648static int generic_hdmi_init(struct hda_codec *codec)
1649{
1650 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001651 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001652
Stephen Warren384a48d2011-06-01 11:14:21 -06001653 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1654 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1655 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001656
1657 hdmi_init_pin(codec, pin_nid);
Takashi Iwai1835a0f2011-10-27 22:12:46 +02001658 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001659 }
1660 return 0;
1661}
1662
1663static void generic_hdmi_free(struct hda_codec *codec)
1664{
1665 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001666 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001667
Stephen Warren384a48d2011-06-01 11:14:21 -06001668 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1669 struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
1670 struct hdmi_eld *eld = &per_pin->sink_eld;
1671
Wu Fengguang744626d2011-11-16 16:29:47 +08001672 cancel_delayed_work(&per_pin->work);
Stephen Warren384a48d2011-06-01 11:14:21 -06001673 snd_hda_eld_proc_free(codec, eld);
1674 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001675
Wu Fengguang744626d2011-11-16 16:29:47 +08001676 flush_workqueue(codec->bus->workq);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001677 kfree(spec);
1678}
1679
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001680static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001681 .init = generic_hdmi_init,
1682 .free = generic_hdmi_free,
1683 .build_pcms = generic_hdmi_build_pcms,
1684 .build_controls = generic_hdmi_build_controls,
1685 .unsol_event = hdmi_unsol_event,
1686};
1687
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001688static void intel_haswell_fixup_connect_list(struct hda_codec *codec)
1689{
1690 unsigned int vendor_param;
1691 hda_nid_t list[3] = {0x2, 0x3, 0x4};
1692
1693 vendor_param = snd_hda_codec_read(codec, 0x08, 0, 0xf81, 0);
1694 if (vendor_param == -1 || vendor_param & 0x02)
1695 return;
1696
1697 /* enable DP1.2 mode */
1698 vendor_param |= 0x02;
1699 snd_hda_codec_read(codec, 0x08, 0, 0x781, vendor_param);
1700
1701 vendor_param = snd_hda_codec_read(codec, 0x08, 0, 0xf81, 0);
1702 if (vendor_param == -1 || !(vendor_param & 0x02))
1703 return;
1704
1705 /* override 3 pins connection list */
1706 snd_hda_override_conn_list(codec, 0x05, 3, list);
1707 snd_hda_override_conn_list(codec, 0x06, 3, list);
1708 snd_hda_override_conn_list(codec, 0x07, 3, list);
1709}
1710
1711
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001712static int patch_generic_hdmi(struct hda_codec *codec)
1713{
1714 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001715
1716 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1717 if (spec == NULL)
1718 return -ENOMEM;
1719
1720 codec->spec = spec;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05001721
1722 if (codec->vendor_id == 0x80862807)
1723 intel_haswell_fixup_connect_list(codec);
1724
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001725 if (hdmi_parse_codec(codec) < 0) {
1726 codec->spec = NULL;
1727 kfree(spec);
1728 return -EINVAL;
1729 }
1730 codec->patch_ops = generic_hdmi_patch_ops;
Takashi Iwai8b8d6542012-06-20 16:32:22 +02001731 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001732
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001733 init_channel_allocations();
1734
1735 return 0;
1736}
1737
1738/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06001739 * Shared non-generic implementations
1740 */
1741
1742static int simple_playback_build_pcms(struct hda_codec *codec)
1743{
1744 struct hdmi_spec *spec = codec->spec;
1745 struct hda_pcm *info = spec->pcm_rec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001746 unsigned int chans;
1747 struct hda_pcm_stream *pstr;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001748
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001749 codec->num_pcms = 1;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001750 codec->pcm_info = info;
1751
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001752 chans = get_wcaps(codec, spec->cvts[0].cvt_nid);
1753 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001754
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001755 info->name = get_hdmi_pcm_name(0);
1756 info->pcm_type = HDA_PCM_TYPE_HDMI;
1757 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1758 *pstr = spec->pcm_playback;
1759 pstr->nid = spec->cvts[0].cvt_nid;
1760 if (pstr->channels_max <= 2 && chans && chans <= 16)
1761 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001762
1763 return 0;
1764}
1765
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001766/* unsolicited event for jack sensing */
1767static void simple_hdmi_unsol_event(struct hda_codec *codec,
1768 unsigned int res)
1769{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02001770 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02001771 snd_hda_jack_report_sync(codec);
1772}
1773
1774/* generic_hdmi_build_jack can be used for simple_hdmi, too,
1775 * as long as spec->pins[] is set correctly
1776 */
1777#define simple_hdmi_build_jack generic_hdmi_build_jack
1778
Stephen Warren3aaf8982011-06-01 11:14:19 -06001779static int simple_playback_build_controls(struct hda_codec *codec)
1780{
1781 struct hdmi_spec *spec = codec->spec;
1782 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06001783
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001784 err = snd_hda_create_spdif_out_ctls(codec,
1785 spec->cvts[0].cvt_nid,
1786 spec->cvts[0].cvt_nid);
1787 if (err < 0)
1788 return err;
1789 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06001790}
1791
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001792static int simple_playback_init(struct hda_codec *codec)
1793{
1794 struct hdmi_spec *spec = codec->spec;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001795 hda_nid_t pin = spec->pins[0].pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001796
Takashi Iwai8ceb3322012-06-21 08:23:27 +02001797 snd_hda_codec_write(codec, pin, 0,
1798 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1799 /* some codecs require to unmute the pin */
1800 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
1801 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1802 AMP_OUT_UNMUTE);
1803 snd_hda_jack_detect_enable(codec, pin, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02001804 return 0;
1805}
1806
Stephen Warren3aaf8982011-06-01 11:14:19 -06001807static void simple_playback_free(struct hda_codec *codec)
1808{
1809 struct hdmi_spec *spec = codec->spec;
1810
1811 kfree(spec);
1812}
1813
1814/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001815 * Nvidia specific implementations
1816 */
1817
1818#define Nv_VERB_SET_Channel_Allocation 0xF79
1819#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
1820#define Nv_VERB_SET_Audio_Protection_On 0xF98
1821#define Nv_VERB_SET_Audio_Protection_Off 0xF99
1822
1823#define nvhdmi_master_con_nid_7x 0x04
1824#define nvhdmi_master_pin_nid_7x 0x05
1825
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02001826static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001827 /*front, rear, clfe, rear_surr */
1828 0x6, 0x8, 0xa, 0xc,
1829};
1830
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001831static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
1832 /* set audio protect on */
1833 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1834 /* enable digital output on pin widget */
1835 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1836 {} /* terminator */
1837};
1838
1839static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001840 /* set audio protect on */
1841 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
1842 /* enable digital output on pin widget */
1843 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1844 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1845 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1846 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1847 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
1848 {} /* terminator */
1849};
1850
1851#ifdef LIMITED_RATE_FMT_SUPPORT
1852/* support only the safe format and rate */
1853#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
1854#define SUPPORTED_MAXBPS 16
1855#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
1856#else
1857/* support all rates and formats */
1858#define SUPPORTED_RATES \
1859 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
1860 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
1861 SNDRV_PCM_RATE_192000)
1862#define SUPPORTED_MAXBPS 24
1863#define SUPPORTED_FORMATS \
1864 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1865#endif
1866
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001867static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001868{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02001869 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
1870 return 0;
1871}
1872
1873static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
1874{
1875 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001876 return 0;
1877}
1878
Nitin Daga393004b2011-01-10 21:49:31 +05301879static unsigned int channels_2_6_8[] = {
1880 2, 6, 8
1881};
1882
1883static unsigned int channels_2_8[] = {
1884 2, 8
1885};
1886
1887static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
1888 .count = ARRAY_SIZE(channels_2_6_8),
1889 .list = channels_2_6_8,
1890 .mask = 0,
1891};
1892
1893static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
1894 .count = ARRAY_SIZE(channels_2_8),
1895 .list = channels_2_8,
1896 .mask = 0,
1897};
1898
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001899static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
1900 struct hda_codec *codec,
1901 struct snd_pcm_substream *substream)
1902{
1903 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05301904 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
1905
1906 switch (codec->preset->id) {
1907 case 0x10de0002:
1908 case 0x10de0003:
1909 case 0x10de0005:
1910 case 0x10de0006:
1911 hw_constraints_channels = &hw_constraints_2_8_channels;
1912 break;
1913 case 0x10de0007:
1914 hw_constraints_channels = &hw_constraints_2_6_8_channels;
1915 break;
1916 default:
1917 break;
1918 }
1919
1920 if (hw_constraints_channels != NULL) {
1921 snd_pcm_hw_constraint_list(substream->runtime, 0,
1922 SNDRV_PCM_HW_PARAM_CHANNELS,
1923 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01001924 } else {
1925 snd_pcm_hw_constraint_step(substream->runtime, 0,
1926 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05301927 }
1928
Takashi Iwai84eb01b2010-09-07 12:27:25 +02001929 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1930}
1931
1932static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
1933 struct hda_codec *codec,
1934 struct snd_pcm_substream *substream)
1935{
1936 struct hdmi_spec *spec = codec->spec;
1937 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1938}
1939
1940static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1941 struct hda_codec *codec,
1942 unsigned int stream_tag,
1943 unsigned int format,
1944 struct snd_pcm_substream *substream)
1945{
1946 struct hdmi_spec *spec = codec->spec;
1947 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1948 stream_tag, format, substream);
1949}
1950
Takashi Iwaid0b12522012-06-15 14:34:42 +02001951static const struct hda_pcm_stream simple_pcm_playback = {
1952 .substreams = 1,
1953 .channels_min = 2,
1954 .channels_max = 2,
1955 .ops = {
1956 .open = simple_playback_pcm_open,
1957 .close = simple_playback_pcm_close,
1958 .prepare = simple_playback_pcm_prepare
1959 },
1960};
1961
1962static const struct hda_codec_ops simple_hdmi_patch_ops = {
1963 .build_controls = simple_playback_build_controls,
1964 .build_pcms = simple_playback_build_pcms,
1965 .init = simple_playback_init,
1966 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02001967 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02001968};
1969
1970static int patch_simple_hdmi(struct hda_codec *codec,
1971 hda_nid_t cvt_nid, hda_nid_t pin_nid)
1972{
1973 struct hdmi_spec *spec;
1974
1975 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1976 if (!spec)
1977 return -ENOMEM;
1978
1979 codec->spec = spec;
1980
1981 spec->multiout.num_dacs = 0; /* no analog */
1982 spec->multiout.max_channels = 2;
1983 spec->multiout.dig_out_nid = cvt_nid;
1984 spec->num_cvts = 1;
1985 spec->num_pins = 1;
1986 spec->cvts[0].cvt_nid = cvt_nid;
Takashi Iwai21cd6832012-06-20 09:19:32 +02001987 spec->pins[0].pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02001988 spec->pcm_playback = simple_pcm_playback;
1989
1990 codec->patch_ops = simple_hdmi_patch_ops;
1991
1992 return 0;
1993}
1994
Aaron Plattner1f348522011-04-06 17:19:04 -07001995static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
1996 int channels)
1997{
1998 unsigned int chanmask;
1999 int chan = channels ? (channels - 1) : 1;
2000
2001 switch (channels) {
2002 default:
2003 case 0:
2004 case 2:
2005 chanmask = 0x00;
2006 break;
2007 case 4:
2008 chanmask = 0x08;
2009 break;
2010 case 6:
2011 chanmask = 0x0b;
2012 break;
2013 case 8:
2014 chanmask = 0x13;
2015 break;
2016 }
2017
2018 /* Set the audio infoframe channel allocation and checksum fields. The
2019 * channel count is computed implicitly by the hardware. */
2020 snd_hda_codec_write(codec, 0x1, 0,
2021 Nv_VERB_SET_Channel_Allocation, chanmask);
2022
2023 snd_hda_codec_write(codec, 0x1, 0,
2024 Nv_VERB_SET_Info_Frame_Checksum,
2025 (0x71 - chan - chanmask));
2026}
2027
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002028static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2029 struct hda_codec *codec,
2030 struct snd_pcm_substream *substream)
2031{
2032 struct hdmi_spec *spec = codec->spec;
2033 int i;
2034
2035 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2036 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2037 for (i = 0; i < 4; i++) {
2038 /* set the stream id */
2039 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2040 AC_VERB_SET_CHANNEL_STREAMID, 0);
2041 /* set the stream format */
2042 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2043 AC_VERB_SET_STREAM_FORMAT, 0);
2044 }
2045
Aaron Plattner1f348522011-04-06 17:19:04 -07002046 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2047 * streams are disabled. */
2048 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2049
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002050 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2051}
2052
2053static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2054 struct hda_codec *codec,
2055 unsigned int stream_tag,
2056 unsigned int format,
2057 struct snd_pcm_substream *substream)
2058{
2059 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01002060 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002061 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06002062 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002063 struct hda_spdif_out *spdif;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002064
2065 mutex_lock(&codec->spdif_mutex);
Takashi Iwaie3245cd2012-05-10 10:21:29 +02002066 spdif = snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002067
2068 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002069
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002070 dataDCC2 = 0x2;
2071
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002072 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06002073 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002074 snd_hda_codec_write(codec,
2075 nvhdmi_master_con_nid_7x,
2076 0,
2077 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002078 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002079
2080 /* set the stream id */
2081 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2082 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2083
2084 /* set the stream format */
2085 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2086 AC_VERB_SET_STREAM_FORMAT, format);
2087
2088 /* turn on again (if needed) */
2089 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06002090 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002091 snd_hda_codec_write(codec,
2092 nvhdmi_master_con_nid_7x,
2093 0,
2094 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002095 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002096 snd_hda_codec_write(codec,
2097 nvhdmi_master_con_nid_7x,
2098 0,
2099 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2100 }
2101
2102 for (i = 0; i < 4; i++) {
2103 if (chs == 2)
2104 channel_id = 0;
2105 else
2106 channel_id = i * 2;
2107
2108 /* turn off SPDIF once;
2109 *otherwise the IEC958 bits won't be updated
2110 */
2111 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002112 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002113 snd_hda_codec_write(codec,
2114 nvhdmi_con_nids_7x[i],
2115 0,
2116 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002117 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002118 /* set the stream id */
2119 snd_hda_codec_write(codec,
2120 nvhdmi_con_nids_7x[i],
2121 0,
2122 AC_VERB_SET_CHANNEL_STREAMID,
2123 (stream_tag << 4) | channel_id);
2124 /* set the stream format */
2125 snd_hda_codec_write(codec,
2126 nvhdmi_con_nids_7x[i],
2127 0,
2128 AC_VERB_SET_STREAM_FORMAT,
2129 format);
2130 /* turn on again (if needed) */
2131 /* enable and set the channel status audio/data flag */
2132 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06002133 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002134 snd_hda_codec_write(codec,
2135 nvhdmi_con_nids_7x[i],
2136 0,
2137 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06002138 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002139 snd_hda_codec_write(codec,
2140 nvhdmi_con_nids_7x[i],
2141 0,
2142 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2143 }
2144 }
2145
Aaron Plattner1f348522011-04-06 17:19:04 -07002146 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002147
2148 mutex_unlock(&codec->spdif_mutex);
2149 return 0;
2150}
2151
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002152static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002153 .substreams = 1,
2154 .channels_min = 2,
2155 .channels_max = 8,
2156 .nid = nvhdmi_master_con_nid_7x,
2157 .rates = SUPPORTED_RATES,
2158 .maxbps = SUPPORTED_MAXBPS,
2159 .formats = SUPPORTED_FORMATS,
2160 .ops = {
2161 .open = simple_playback_pcm_open,
2162 .close = nvhdmi_8ch_7x_pcm_close,
2163 .prepare = nvhdmi_8ch_7x_pcm_prepare
2164 },
2165};
2166
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002167static int patch_nvhdmi_2ch(struct hda_codec *codec)
2168{
2169 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002170 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2171 nvhdmi_master_pin_nid_7x);
2172 if (err < 0)
2173 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002174
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002175 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002176 /* override the PCM rates, etc, as the codec doesn't give full list */
2177 spec = codec->spec;
2178 spec->pcm_playback.rates = SUPPORTED_RATES;
2179 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2180 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002181 return 0;
2182}
2183
Takashi Iwai53775b02012-08-01 12:17:41 +02002184static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2185{
2186 struct hdmi_spec *spec = codec->spec;
2187 int err = simple_playback_build_pcms(codec);
2188 spec->pcm_rec[0].own_chmap = true;
2189 return err;
2190}
2191
2192static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2193{
2194 struct hdmi_spec *spec = codec->spec;
2195 struct snd_pcm_chmap *chmap;
2196 int err;
2197
2198 err = simple_playback_build_controls(codec);
2199 if (err < 0)
2200 return err;
2201
2202 /* add channel maps */
2203 err = snd_pcm_add_chmap_ctls(spec->pcm_rec[0].pcm,
2204 SNDRV_PCM_STREAM_PLAYBACK,
2205 snd_pcm_alt_chmaps, 8, 0, &chmap);
2206 if (err < 0)
2207 return err;
2208 switch (codec->preset->id) {
2209 case 0x10de0002:
2210 case 0x10de0003:
2211 case 0x10de0005:
2212 case 0x10de0006:
2213 chmap->channel_mask = (1U << 2) | (1U << 8);
2214 break;
2215 case 0x10de0007:
2216 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2217 }
2218 return 0;
2219}
2220
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002221static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2222{
2223 struct hdmi_spec *spec;
2224 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002225 if (err < 0)
2226 return err;
2227 spec = codec->spec;
2228 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002229 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002230 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02002231 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2232 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07002233
2234 /* Initialize the audio infoframe channel mask and checksum to something
2235 * valid */
2236 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2237
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002238 return 0;
2239}
2240
2241/*
2242 * ATI-specific implementations
2243 *
2244 * FIXME: we may omit the whole this and use the generic code once after
2245 * it's confirmed to work.
2246 */
2247
2248#define ATIHDMI_CVT_NID 0x02 /* audio converter */
2249#define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2250
2251static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2252 struct hda_codec *codec,
2253 unsigned int stream_tag,
2254 unsigned int format,
2255 struct snd_pcm_substream *substream)
2256{
2257 struct hdmi_spec *spec = codec->spec;
2258 int chans = substream->runtime->channels;
2259 int i, err;
2260
2261 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2262 substream);
2263 if (err < 0)
2264 return err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002265 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
2266 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002267 /* FIXME: XXX */
2268 for (i = 0; i < chans; i++) {
Stephen Warren384a48d2011-06-01 11:14:21 -06002269 snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002270 AC_VERB_SET_HDMI_CHAN_SLOT,
2271 (i << 4) | i);
2272 }
2273 return 0;
2274}
2275
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002276static int patch_atihdmi(struct hda_codec *codec)
2277{
2278 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02002279 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2280 if (err < 0)
2281 return err;
2282 spec = codec->spec;
2283 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002284 return 0;
2285}
2286
Annie Liu3de5ff82012-06-08 19:18:42 +08002287/* VIA HDMI Implementation */
2288#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2289#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2290
Annie Liu3de5ff82012-06-08 19:18:42 +08002291static int patch_via_hdmi(struct hda_codec *codec)
2292{
Takashi Iwai250e41a2012-06-15 14:40:21 +02002293 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08002294}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002295
2296/*
2297 * patch entries
2298 */
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002299static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002300{ .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2301{ .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2302{ .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
Anssi Hannula36e9c132010-12-05 02:34:15 +02002303{ .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002304{ .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2305{ .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2306{ .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2307{ .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2308{ .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2309{ .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2310{ .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2311{ .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
Stephen Warren5d44f922011-05-24 17:11:17 -06002312{ .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2313{ .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2314{ .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2315{ .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2316{ .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2317{ .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2318{ .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2319{ .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2320{ .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2321{ .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2322{ .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
Richard Samsonc8900a02011-03-03 12:46:13 +01002323/* 17 is known to be absent */
Stephen Warren5d44f922011-05-24 17:11:17 -06002324{ .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2325{ .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2326{ .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2327{ .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2328{ .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2329{ .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2330{ .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2331{ .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2332{ .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2333{ .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002334{ .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002335{ .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2336{ .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
Annie Liu3de5ff82012-06-08 19:18:42 +08002337{ .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2338{ .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2339{ .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2340{ .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002341{ .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2342{ .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2343{ .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2344{ .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2345{ .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2346{ .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
Wu Fengguang591e6102011-05-20 15:35:43 +08002347{ .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
Wang Xingchao1c766842012-06-13 10:23:52 +08002348{ .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002349{ .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002350{ .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2351{} /* terminator */
2352};
2353
2354MODULE_ALIAS("snd-hda-codec-id:1002793c");
2355MODULE_ALIAS("snd-hda-codec-id:10027919");
2356MODULE_ALIAS("snd-hda-codec-id:1002791a");
2357MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2358MODULE_ALIAS("snd-hda-codec-id:10951390");
2359MODULE_ALIAS("snd-hda-codec-id:10951392");
2360MODULE_ALIAS("snd-hda-codec-id:10de0002");
2361MODULE_ALIAS("snd-hda-codec-id:10de0003");
2362MODULE_ALIAS("snd-hda-codec-id:10de0005");
2363MODULE_ALIAS("snd-hda-codec-id:10de0006");
2364MODULE_ALIAS("snd-hda-codec-id:10de0007");
2365MODULE_ALIAS("snd-hda-codec-id:10de000a");
2366MODULE_ALIAS("snd-hda-codec-id:10de000b");
2367MODULE_ALIAS("snd-hda-codec-id:10de000c");
2368MODULE_ALIAS("snd-hda-codec-id:10de000d");
2369MODULE_ALIAS("snd-hda-codec-id:10de0010");
2370MODULE_ALIAS("snd-hda-codec-id:10de0011");
2371MODULE_ALIAS("snd-hda-codec-id:10de0012");
2372MODULE_ALIAS("snd-hda-codec-id:10de0013");
2373MODULE_ALIAS("snd-hda-codec-id:10de0014");
Richard Samsonc8900a02011-03-03 12:46:13 +01002374MODULE_ALIAS("snd-hda-codec-id:10de0015");
2375MODULE_ALIAS("snd-hda-codec-id:10de0016");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002376MODULE_ALIAS("snd-hda-codec-id:10de0018");
2377MODULE_ALIAS("snd-hda-codec-id:10de0019");
2378MODULE_ALIAS("snd-hda-codec-id:10de001a");
2379MODULE_ALIAS("snd-hda-codec-id:10de001b");
2380MODULE_ALIAS("snd-hda-codec-id:10de001c");
2381MODULE_ALIAS("snd-hda-codec-id:10de0040");
2382MODULE_ALIAS("snd-hda-codec-id:10de0041");
2383MODULE_ALIAS("snd-hda-codec-id:10de0042");
2384MODULE_ALIAS("snd-hda-codec-id:10de0043");
2385MODULE_ALIAS("snd-hda-codec-id:10de0044");
Aaron Plattner7ae48b52012-07-16 17:10:04 -07002386MODULE_ALIAS("snd-hda-codec-id:10de0051");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002387MODULE_ALIAS("snd-hda-codec-id:10de0067");
2388MODULE_ALIAS("snd-hda-codec-id:10de8001");
Annie Liu3de5ff82012-06-08 19:18:42 +08002389MODULE_ALIAS("snd-hda-codec-id:11069f80");
2390MODULE_ALIAS("snd-hda-codec-id:11069f81");
2391MODULE_ALIAS("snd-hda-codec-id:11069f84");
2392MODULE_ALIAS("snd-hda-codec-id:11069f85");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002393MODULE_ALIAS("snd-hda-codec-id:17e80047");
2394MODULE_ALIAS("snd-hda-codec-id:80860054");
2395MODULE_ALIAS("snd-hda-codec-id:80862801");
2396MODULE_ALIAS("snd-hda-codec-id:80862802");
2397MODULE_ALIAS("snd-hda-codec-id:80862803");
2398MODULE_ALIAS("snd-hda-codec-id:80862804");
2399MODULE_ALIAS("snd-hda-codec-id:80862805");
Wu Fengguang591e6102011-05-20 15:35:43 +08002400MODULE_ALIAS("snd-hda-codec-id:80862806");
Wang Xingchao1c766842012-06-13 10:23:52 +08002401MODULE_ALIAS("snd-hda-codec-id:80862807");
Wu Fengguang6edc59e2012-02-23 15:07:44 +08002402MODULE_ALIAS("snd-hda-codec-id:80862880");
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002403MODULE_ALIAS("snd-hda-codec-id:808629fb");
2404
2405MODULE_LICENSE("GPL");
2406MODULE_DESCRIPTION("HDMI HD-audio codec");
2407MODULE_ALIAS("snd-hda-codec-intelhdmi");
2408MODULE_ALIAS("snd-hda-codec-nvhdmi");
2409MODULE_ALIAS("snd-hda-codec-atihdmi");
2410
2411static struct hda_codec_preset_list intel_list = {
2412 .preset = snd_hda_preset_hdmi,
2413 .owner = THIS_MODULE,
2414};
2415
2416static int __init patch_hdmi_init(void)
2417{
2418 return snd_hda_add_codec_preset(&intel_list);
2419}
2420
2421static void __exit patch_hdmi_exit(void)
2422{
2423 snd_hda_delete_codec_preset(&intel_list);
2424}
2425
2426module_init(patch_hdmi_init)
2427module_exit(patch_hdmi_exit)