blob: 6cf25eedf48263053e41598290e2536238ee2295 [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>
Anssi Hannula5a6135842013-10-24 21:10:35 +03009 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
Wu Fengguang079d88c2010-03-08 10:44:23 +080010 *
11 * Authors:
12 * Wu Fengguang <wfg@linux.intel.com>
13 *
14 * Maintained by:
15 * Wu Fengguang <wfg@linux.intel.com>
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
20 * any later version.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
Takashi Iwai84eb01b2010-09-07 12:27:25 +020032#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040035#include <linux/module.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020036#include <sound/core.h>
David Henningsson07acecc2011-05-19 11:46:03 +020037#include <sound/jack.h>
Wang Xingchao433968d2012-09-06 10:02:37 +080038#include <sound/asoundef.h>
Takashi Iwaid45e6882012-07-31 11:36:00 +020039#include <sound/tlv.h>
David Henningsson25adc132015-08-19 10:48:58 +020040#include <sound/hdaudio.h>
41#include <sound/hda_i915.h>
Takashi Iwai84eb01b2010-09-07 12:27:25 +020042#include "hda_codec.h"
43#include "hda_local.h"
Takashi Iwai1835a0f2011-10-27 22:12:46 +020044#include "hda_jack.h"
Takashi Iwai84eb01b2010-09-07 12:27:25 +020045
Takashi Iwai0ebaa242011-01-11 18:11:04 +010046static bool static_hdmi_pcm;
47module_param(static_hdmi_pcm, bool, 0644);
48MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
49
Takashi Iwai7639a062015-03-03 10:07:24 +010050#define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
51#define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
52#define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
Lu, Hane2656412015-11-11 16:54:27 +080053#define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
Libin Yang91815d82016-01-14 14:09:00 +080054#define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
Libin Yang432ac1a2014-12-16 13:17:34 +080055#define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
Libin Yang91815d82016-01-14 14:09:00 +080056 || is_skylake(codec) || is_broxton(codec) \
57 || is_kabylake(codec))
Mengdong Lin75dcbe42014-01-08 15:55:32 -050058
Takashi Iwai7639a062015-03-03 10:07:24 +010059#define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
60#define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
Libin Yangca2e7222014-08-19 16:20:12 +080061#define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
Mengdong Linfb87fa32013-09-04 16:36:57 -040062
Stephen Warren384a48d2011-06-01 11:14:21 -060063struct hdmi_spec_per_cvt {
64 hda_nid_t cvt_nid;
65 int assigned;
66 unsigned int channels_min;
67 unsigned int channels_max;
68 u32 rates;
69 u64 formats;
70 unsigned int maxbps;
71};
72
Takashi Iwai4eea3092013-02-07 18:18:19 +010073/* max. connections to a widget */
74#define HDA_MAX_CONNECTIONS 32
75
Stephen Warren384a48d2011-06-01 11:14:21 -060076struct hdmi_spec_per_pin {
77 hda_nid_t pin_nid;
Libin Yanga76056f2015-12-16 16:48:15 +080078 /* pin idx, different device entries on the same pin use the same idx */
79 int pin_nid_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -060080 int num_mux_nids;
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Mengdong Lin2df67422014-03-20 13:01:06 +080082 int mux_idx;
Anssi Hannula1df5a062013-10-05 02:25:40 +030083 hda_nid_t cvt_nid;
Wu Fengguang744626d2011-11-16 16:29:47 +080084
85 struct hda_codec *codec;
Stephen Warren384a48d2011-06-01 11:14:21 -060086 struct hdmi_eld sink_eld;
Takashi Iwaia4e9a382013-10-17 18:21:12 +020087 struct mutex lock;
Wu Fengguang744626d2011-11-16 16:29:47 +080088 struct delayed_work work;
David Henningsson92c69e72013-02-19 16:11:26 +010089 struct snd_kcontrol *eld_ctl;
Takashi Iwai788d4412015-11-12 15:36:13 +010090 struct snd_jack *acomp_jack; /* jack via audio component */
Libin Yang6590faa2015-12-16 13:42:41 +080091 struct hda_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
Libin Yanga76056f2015-12-16 16:48:15 +080092 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
Wu Fengguangc6e84532011-11-18 16:59:32 -060093 int repoll_count;
Takashi Iwaib0540872013-09-02 12:33:02 +020094 bool setup; /* the stream has been set up by prepare callback */
95 int channels; /* current number of channels */
Takashi Iwai1a6003b2012-09-06 17:42:08 +020096 bool non_pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +020097 bool chmap_set; /* channel-map override by ALSA API? */
98 unsigned char chmap[8]; /* ALSA API channel-map */
Jie Yangcd6a6502015-05-27 19:45:45 +080099#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200100 struct snd_info_entry *proc_entry;
101#endif
Stephen Warren384a48d2011-06-01 11:14:21 -0600102};
103
Anssi Hannula307229d2013-10-24 21:10:34 +0300104struct cea_channel_speaker_allocation;
105
106/* operations used by generic code that can be overridden by patches */
107struct hdmi_ops {
108 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
109 unsigned char *buf, int *eld_size);
110
111 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
112 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
113 int asp_slot);
114 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int asp_slot, int channel);
116
117 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
118 int ca, int active_channels, int conn_type);
119
120 /* enable/disable HBR (HD passthrough) */
121 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
122
123 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
124 hda_nid_t pin_nid, u32 stream_tag, int format);
125
126 /* Helpers for producing the channel map TLVs. These can be overridden
127 * for devices that have non-standard mapping requirements. */
128 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
129 int channels);
130 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
131 unsigned int *chmap, int channels);
132
133 /* check that the user-given chmap is supported */
134 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
135};
136
Wu Fengguang079d88c2010-03-08 10:44:23 +0800137struct hdmi_spec {
138 int num_cvts;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100139 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
140 hda_nid_t cvt_nids[4]; /* only for haswell fix */
Stephen Warren384a48d2011-06-01 11:14:21 -0600141
Wu Fengguang079d88c2010-03-08 10:44:23 +0800142 int num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100143 struct snd_array pins; /* struct hdmi_spec_per_pin */
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100144 struct hda_pcm *pcm_rec[16];
Libin Yang42b29872015-12-16 13:42:42 +0800145 struct mutex pcm_lock;
Libin Yanga76056f2015-12-16 16:48:15 +0800146 /* pcm_bitmap means which pcms have been assigned to pins*/
147 unsigned long pcm_bitmap;
Libin Yang2bf3c852015-12-16 13:42:43 +0800148 int pcm_used; /* counter of pcm_rec[] */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200149 unsigned int channels_max; /* max over all cvts */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800150
David Henningsson4bd038f2013-02-19 16:11:25 +0100151 struct hdmi_eld temp_eld;
Anssi Hannula307229d2013-10-24 21:10:34 +0300152 struct hdmi_ops ops;
Stephen Warren75fae112014-01-30 11:52:16 -0700153
154 bool dyn_pin_out;
Libin Yang6590faa2015-12-16 13:42:41 +0800155 bool dyn_pcm_assign;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800156 /*
Anssi Hannula5a6135842013-10-24 21:10:35 +0300157 * Non-generic VIA/NVIDIA specific
Wu Fengguang079d88c2010-03-08 10:44:23 +0800158 */
159 struct hda_multi_out multiout;
Takashi Iwaid0b12522012-06-15 14:34:42 +0200160 struct hda_pcm_stream pcm_playback;
David Henningsson25adc132015-08-19 10:48:58 +0200161
162 /* i915/powerwell (Haswell+/Valleyview+) specific */
163 struct i915_audio_component_audio_ops i915_audio_ops;
Takashi Iwai55913112015-12-10 13:03:29 +0100164 bool i915_bound; /* was i915 bound in this driver? */
Wu Fengguang079d88c2010-03-08 10:44:23 +0800165};
166
Takashi Iwaif4e30402015-12-10 13:01:28 +0100167#ifdef CONFIG_SND_HDA_I915
Takashi Iwai66032492015-12-01 16:49:35 +0100168#define codec_has_acomp(codec) \
169 ((codec)->bus->core.audio_component != NULL)
Takashi Iwaif4e30402015-12-10 13:01:28 +0100170#else
171#define codec_has_acomp(codec) false
172#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800173
174struct hdmi_audio_infoframe {
175 u8 type; /* 0x84 */
176 u8 ver; /* 0x01 */
177 u8 len; /* 0x0a */
178
Wu Fengguang53d7d692010-09-21 14:25:49 +0800179 u8 checksum;
180
Wu Fengguang079d88c2010-03-08 10:44:23 +0800181 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
182 u8 SS01_SF24;
183 u8 CXT04;
184 u8 CA;
185 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800186};
187
188struct dp_audio_infoframe {
189 u8 type; /* 0x84 */
190 u8 len; /* 0x1b */
191 u8 ver; /* 0x11 << 2 */
192
193 u8 CC02_CT47; /* match with HDMI infoframe from this on */
194 u8 SS01_SF24;
195 u8 CXT04;
196 u8 CA;
197 u8 LFEPBL01_LSV36_DM_INH7;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800198};
199
Takashi Iwai2b203db2011-02-11 12:17:30 +0100200union audio_infoframe {
201 struct hdmi_audio_infoframe hdmi;
202 struct dp_audio_infoframe dp;
203 u8 bytes[0];
204};
205
Wu Fengguang079d88c2010-03-08 10:44:23 +0800206/*
207 * CEA speaker placement:
208 *
209 * FLH FCH FRH
210 * FLW FL FLC FC FRC FR FRW
211 *
212 * LFE
213 * TC
214 *
215 * RL RLC RC RRC RR
216 *
217 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
218 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
219 */
220enum cea_speaker_placement {
221 FL = (1 << 0), /* Front Left */
222 FC = (1 << 1), /* Front Center */
223 FR = (1 << 2), /* Front Right */
224 FLC = (1 << 3), /* Front Left Center */
225 FRC = (1 << 4), /* Front Right Center */
226 RL = (1 << 5), /* Rear Left */
227 RC = (1 << 6), /* Rear Center */
228 RR = (1 << 7), /* Rear Right */
229 RLC = (1 << 8), /* Rear Left Center */
230 RRC = (1 << 9), /* Rear Right Center */
231 LFE = (1 << 10), /* Low Frequency Effect */
232 FLW = (1 << 11), /* Front Left Wide */
233 FRW = (1 << 12), /* Front Right Wide */
234 FLH = (1 << 13), /* Front Left High */
235 FCH = (1 << 14), /* Front Center High */
236 FRH = (1 << 15), /* Front Right High */
237 TC = (1 << 16), /* Top Center */
238};
239
240/*
241 * ELD SA bits in the CEA Speaker Allocation data block
242 */
243static int eld_speaker_allocation_bits[] = {
244 [0] = FL | FR,
245 [1] = LFE,
246 [2] = FC,
247 [3] = RL | RR,
248 [4] = RC,
249 [5] = FLC | FRC,
250 [6] = RLC | RRC,
251 /* the following are not defined in ELD yet */
252 [7] = FLW | FRW,
253 [8] = FLH | FRH,
254 [9] = TC,
255 [10] = FCH,
256};
257
258struct cea_channel_speaker_allocation {
259 int ca_index;
260 int speakers[8];
261
262 /* derived values, just for convenience */
263 int channels;
264 int spk_mask;
265};
266
267/*
268 * ALSA sequence is:
269 *
270 * surround40 surround41 surround50 surround51 surround71
271 * ch0 front left = = = =
272 * ch1 front right = = = =
273 * ch2 rear left = = = =
274 * ch3 rear right = = = =
275 * ch4 LFE center center center
276 * ch5 LFE LFE
277 * ch6 side left
278 * ch7 side right
279 *
280 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
281 */
282static int hdmi_channel_mapping[0x32][8] = {
283 /* stereo */
284 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
285 /* 2.1 */
286 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
287 /* Dolby Surround */
288 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
289 /* surround40 */
290 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
291 /* 4ch */
292 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
293 /* surround41 */
Jerry Zhou9396d312010-09-21 14:44:51 +0800294 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
Wu Fengguang079d88c2010-03-08 10:44:23 +0800295 /* surround50 */
296 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
297 /* surround51 */
298 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
299 /* 7.1 */
300 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
301};
302
303/*
304 * This is an ordered list!
305 *
306 * The preceding ones have better chances to be selected by
Wu Fengguang53d7d692010-09-21 14:25:49 +0800307 * hdmi_channel_allocation().
Wu Fengguang079d88c2010-03-08 10:44:23 +0800308 */
309static struct cea_channel_speaker_allocation channel_allocations[] = {
310/* channel: 7 6 5 4 3 2 1 0 */
311{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
312 /* 2.1 */
313{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
314 /* Dolby Surround */
315{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
316 /* surround40 */
317{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
318 /* surround41 */
319{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
320 /* surround50 */
321{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
322 /* surround51 */
323{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
324 /* 6.1 */
325{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
326 /* surround71 */
327{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
328
329{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
330{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
331{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
332{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
333{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
334{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
335{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
336{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
337{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
338{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
339{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
340{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
341{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
342{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
343{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
344{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
345{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
346{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
347{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
348{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
349{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
350{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
351{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
352{ .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
353{ .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
354{ .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
355{ .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
356{ .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
357{ .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
358{ .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
359{ .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
360{ .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
361{ .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
362{ .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
363{ .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
364{ .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
365{ .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
366{ .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
367{ .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
368{ .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
369{ .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
370};
371
372
373/*
374 * HDMI routines
375 */
376
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100377#define get_pin(spec, idx) \
378 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
379#define get_cvt(spec, idx) \
380 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
Takashi Iwaibbbc7e82015-02-27 17:43:19 +0100381#define get_pcm_rec(spec, idx) ((spec)->pcm_rec[idx])
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100382
Takashi Iwai4e76a882014-02-25 12:21:03 +0100383static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800384{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100385 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600386 int pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800387
Stephen Warren384a48d2011-06-01 11:14:21 -0600388 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100389 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600390 return pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800391
Takashi Iwai4e76a882014-02-25 12:21:03 +0100392 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -0600393 return -EINVAL;
394}
395
Libin Yang2bf3c852015-12-16 13:42:43 +0800396static int hinfo_to_pcm_index(struct hda_codec *codec,
397 struct hda_pcm_stream *hinfo)
398{
399 struct hdmi_spec *spec = codec->spec;
400 int pcm_idx;
401
402 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
403 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
404 return pcm_idx;
405
406 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
407 return -EINVAL;
408}
409
Takashi Iwai4e76a882014-02-25 12:21:03 +0100410static int hinfo_to_pin_index(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600411 struct hda_pcm_stream *hinfo)
412{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100413 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +0800414 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -0600415 int pin_idx;
416
Libin Yang6590faa2015-12-16 13:42:41 +0800417 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
418 per_pin = get_pin(spec, pin_idx);
419 if (per_pin->pcm && per_pin->pcm->stream == hinfo)
Stephen Warren384a48d2011-06-01 11:14:21 -0600420 return pin_idx;
Libin Yang6590faa2015-12-16 13:42:41 +0800421 }
Stephen Warren384a48d2011-06-01 11:14:21 -0600422
Libin Yang6590faa2015-12-16 13:42:41 +0800423 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
Stephen Warren384a48d2011-06-01 11:14:21 -0600424 return -EINVAL;
425}
426
Takashi Iwai4e76a882014-02-25 12:21:03 +0100427static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600428{
Takashi Iwai4e76a882014-02-25 12:21:03 +0100429 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -0600430 int cvt_idx;
431
432 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100433 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
Stephen Warren384a48d2011-06-01 11:14:21 -0600434 return cvt_idx;
435
Takashi Iwai4e76a882014-02-25 12:21:03 +0100436 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800437 return -EINVAL;
438}
439
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500440static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
441 struct snd_ctl_elem_info *uinfo)
442{
443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100444 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200445 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100446 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500447 int pin_idx;
448
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500449 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
450
451 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200452 per_pin = get_pin(spec, pin_idx);
453 eld = &per_pin->sink_eld;
David Henningsson68e03de2013-02-19 16:11:23 +0100454
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200455 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100456 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200457 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500458
459 return 0;
460}
461
462static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
463 struct snd_ctl_elem_value *ucontrol)
464{
465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
David Henningsson68e03de2013-02-19 16:11:23 +0100466 struct hdmi_spec *spec = codec->spec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200467 struct hdmi_spec_per_pin *per_pin;
David Henningsson68e03de2013-02-19 16:11:23 +0100468 struct hdmi_eld *eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500469 int pin_idx;
470
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500471 pin_idx = kcontrol->private_value;
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200472 per_pin = get_pin(spec, pin_idx);
473 eld = &per_pin->sink_eld;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500474
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200475 mutex_lock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100476 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200477 mutex_unlock(&per_pin->lock);
David Henningsson68e03de2013-02-19 16:11:23 +0100478 snd_BUG();
479 return -EINVAL;
480 }
481
482 memset(ucontrol->value.bytes.data, 0,
483 ARRAY_SIZE(ucontrol->value.bytes.data));
484 if (eld->eld_valid)
485 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
486 eld->eld_size);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200487 mutex_unlock(&per_pin->lock);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500488
489 return 0;
490}
491
492static struct snd_kcontrol_new eld_bytes_ctl = {
493 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
494 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
495 .name = "ELD",
496 .info = hdmi_eld_ctl_info,
497 .get = hdmi_eld_ctl_get,
498};
499
500static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
501 int device)
502{
503 struct snd_kcontrol *kctl;
504 struct hdmi_spec *spec = codec->spec;
505 int err;
506
507 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
508 if (!kctl)
509 return -ENOMEM;
510 kctl->private_value = pin_idx;
511 kctl->id.device = device;
512
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100513 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500514 if (err < 0)
515 return err;
516
Takashi Iwaibce0d2a2013-03-13 14:40:31 +0100517 get_pin(spec, pin_idx)->eld_ctl = kctl;
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -0500518 return 0;
519}
520
Wu Fengguang079d88c2010-03-08 10:44:23 +0800521#ifdef BE_PARANOID
522static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
523 int *packet_index, int *byte_index)
524{
525 int val;
526
527 val = snd_hda_codec_read(codec, pin_nid, 0,
528 AC_VERB_GET_HDMI_DIP_INDEX, 0);
529
530 *packet_index = val >> 5;
531 *byte_index = val & 0x1f;
532}
533#endif
534
535static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
536 int packet_index, int byte_index)
537{
538 int val;
539
540 val = (packet_index << 5) | (byte_index & 0x1f);
541
542 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
543}
544
545static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
546 unsigned char val)
547{
548 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
549}
550
Stephen Warren384a48d2011-06-01 11:14:21 -0600551static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800552{
Stephen Warren75fae112014-01-30 11:52:16 -0700553 struct hdmi_spec *spec = codec->spec;
554 int pin_out;
555
Wu Fengguang079d88c2010-03-08 10:44:23 +0800556 /* Unmute */
557 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
558 snd_hda_codec_write(codec, pin_nid, 0,
559 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
Stephen Warren75fae112014-01-30 11:52:16 -0700560
561 if (spec->dyn_pin_out)
562 /* Disable pin out until stream is active */
563 pin_out = 0;
564 else
565 /* Enable pin out: some machines with GM965 gets broken output
566 * when the pin is disabled or changed while using with HDMI
567 */
568 pin_out = PIN_OUT;
569
Wu Fengguang079d88c2010-03-08 10:44:23 +0800570 snd_hda_codec_write(codec, pin_nid, 0,
Stephen Warren75fae112014-01-30 11:52:16 -0700571 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800572}
573
Stephen Warren384a48d2011-06-01 11:14:21 -0600574static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800575{
Stephen Warren384a48d2011-06-01 11:14:21 -0600576 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800577 AC_VERB_GET_CVT_CHAN_COUNT, 0);
578}
579
580static void hdmi_set_channel_count(struct hda_codec *codec,
Stephen Warren384a48d2011-06-01 11:14:21 -0600581 hda_nid_t cvt_nid, int chs)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800582{
Stephen Warren384a48d2011-06-01 11:14:21 -0600583 if (chs != hdmi_get_channel_count(codec, cvt_nid))
584 snd_hda_codec_write(codec, cvt_nid, 0,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800585 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
586}
587
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200588/*
589 * ELD proc files
590 */
591
Jie Yangcd6a6502015-05-27 19:45:45 +0800592#ifdef CONFIG_SND_PROC_FS
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200593static void print_eld_info(struct snd_info_entry *entry,
594 struct snd_info_buffer *buffer)
595{
596 struct hdmi_spec_per_pin *per_pin = entry->private_data;
597
598 mutex_lock(&per_pin->lock);
599 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
600 mutex_unlock(&per_pin->lock);
601}
602
603static void write_eld_info(struct snd_info_entry *entry,
604 struct snd_info_buffer *buffer)
605{
606 struct hdmi_spec_per_pin *per_pin = entry->private_data;
607
608 mutex_lock(&per_pin->lock);
609 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
610 mutex_unlock(&per_pin->lock);
611}
612
613static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
614{
615 char name[32];
616 struct hda_codec *codec = per_pin->codec;
617 struct snd_info_entry *entry;
618 int err;
619
620 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
Takashi Iwai6efdd852015-02-27 16:09:22 +0100621 err = snd_card_proc_new(codec->card, name, &entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200622 if (err < 0)
623 return err;
624
625 snd_info_set_text_ops(entry, per_pin, print_eld_info);
626 entry->c.text.write = write_eld_info;
627 entry->mode |= S_IWUSR;
628 per_pin->proc_entry = entry;
629
630 return 0;
631}
632
633static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
634{
Markus Elfring1947a112015-06-28 11:15:28 +0200635 if (!per_pin->codec->bus->shutdown) {
Takashi Iwaic560a672015-04-22 18:26:38 +0200636 snd_info_free_entry(per_pin->proc_entry);
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200637 per_pin->proc_entry = NULL;
638 }
639}
640#else
Takashi Iwaib55447a2013-10-21 16:31:45 +0200641static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
642 int index)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200643{
644 return 0;
645}
Takashi Iwaib55447a2013-10-21 16:31:45 +0200646static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
Takashi Iwaia4e9a382013-10-17 18:21:12 +0200647{
648}
649#endif
Wu Fengguang079d88c2010-03-08 10:44:23 +0800650
651/*
652 * Channel mapping routines
653 */
654
655/*
656 * Compute derived values in channel_allocations[].
657 */
658static void init_channel_allocations(void)
659{
660 int i, j;
661 struct cea_channel_speaker_allocation *p;
662
663 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
664 p = channel_allocations + i;
665 p->channels = 0;
666 p->spk_mask = 0;
667 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
668 if (p->speakers[j]) {
669 p->channels++;
670 p->spk_mask |= p->speakers[j];
671 }
672 }
673}
674
Wang Xingchao72357c72012-09-06 10:02:36 +0800675static int get_channel_allocation_order(int ca)
676{
677 int i;
678
679 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
680 if (channel_allocations[i].ca_index == ca)
681 break;
682 }
683 return i;
684}
685
Wu Fengguang079d88c2010-03-08 10:44:23 +0800686/*
687 * The transformation takes two steps:
688 *
689 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
690 * spk_mask => (channel_allocations[]) => ai->CA
691 *
692 * TODO: it could select the wrong CA from multiple candidates.
693*/
Takashi Iwai79514d42014-06-06 18:04:34 +0200694static int hdmi_channel_allocation(struct hda_codec *codec,
695 struct hdmi_eld *eld, int channels)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800696{
Wu Fengguang079d88c2010-03-08 10:44:23 +0800697 int i;
Wu Fengguang53d7d692010-09-21 14:25:49 +0800698 int ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800699 int spk_mask = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800700 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
701
702 /*
703 * CA defaults to 0 for basic stereo audio
704 */
705 if (channels <= 2)
706 return 0;
707
Wu Fengguang079d88c2010-03-08 10:44:23 +0800708 /*
709 * expand ELD's speaker allocation mask
710 *
711 * ELD tells the speaker mask in a compact(paired) form,
712 * expand ELD's notions to match the ones used by Audio InfoFrame.
713 */
714 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
David Henningsson1613d6b2013-02-19 16:11:24 +0100715 if (eld->info.spk_alloc & (1 << i))
Wu Fengguang079d88c2010-03-08 10:44:23 +0800716 spk_mask |= eld_speaker_allocation_bits[i];
717 }
718
719 /* search for the first working match in the CA table */
720 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
721 if (channels == channel_allocations[i].channels &&
722 (spk_mask & channel_allocations[i].spk_mask) ==
723 channel_allocations[i].spk_mask) {
Wu Fengguang53d7d692010-09-21 14:25:49 +0800724 ca = channel_allocations[i].ca_index;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800725 break;
726 }
727 }
728
Anssi Hannula18e39182013-09-01 14:36:47 +0300729 if (!ca) {
730 /* if there was no match, select the regular ALSA channel
731 * allocation with the matching number of channels */
732 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
733 if (channels == channel_allocations[i].channels) {
734 ca = channel_allocations[i].ca_index;
735 break;
736 }
737 }
738 }
739
David Henningsson1613d6b2013-02-19 16:11:24 +0100740 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
Takashi Iwai79514d42014-06-06 18:04:34 +0200741 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
Wu Fengguang53d7d692010-09-21 14:25:49 +0800742 ca, channels, buf);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800743
Wu Fengguang53d7d692010-09-21 14:25:49 +0800744 return ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800745}
746
747static void hdmi_debug_channel_mapping(struct hda_codec *codec,
748 hda_nid_t pin_nid)
749{
750#ifdef CONFIG_SND_DEBUG_VERBOSE
Anssi Hannula307229d2013-10-24 21:10:34 +0300751 struct hdmi_spec *spec = codec->spec;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800752 int i;
Anssi Hannula307229d2013-10-24 21:10:34 +0300753 int channel;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800754
755 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300756 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100757 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +0300758 channel, i);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800759 }
760#endif
761}
762
Takashi Iwaid45e6882012-07-31 11:36:00 +0200763static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +0800764 hda_nid_t pin_nid,
Wang Xingchao433968d2012-09-06 10:02:37 +0800765 bool non_pcm,
Wu Fengguang53d7d692010-09-21 14:25:49 +0800766 int ca)
Wu Fengguang079d88c2010-03-08 10:44:23 +0800767{
Anssi Hannula307229d2013-10-24 21:10:34 +0300768 struct hdmi_spec *spec = codec->spec;
Anssi Hannula90f28002013-10-05 02:25:39 +0300769 struct cea_channel_speaker_allocation *ch_alloc;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800770 int i;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800771 int err;
Wang Xingchao72357c72012-09-06 10:02:36 +0800772 int order;
Wang Xingchao433968d2012-09-06 10:02:37 +0800773 int non_pcm_mapping[8];
Wu Fengguang079d88c2010-03-08 10:44:23 +0800774
Wang Xingchao72357c72012-09-06 10:02:36 +0800775 order = get_channel_allocation_order(ca);
Anssi Hannula90f28002013-10-05 02:25:39 +0300776 ch_alloc = &channel_allocations[order];
Wang Xingchao433968d2012-09-06 10:02:37 +0800777
Wu Fengguang079d88c2010-03-08 10:44:23 +0800778 if (hdmi_channel_mapping[ca][1] == 0) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300779 int hdmi_slot = 0;
780 /* fill actual channel mappings in ALSA channel (i) order */
781 for (i = 0; i < ch_alloc->channels; i++) {
782 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
783 hdmi_slot++; /* skip zero slots */
784
785 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
786 }
787 /* fill the rest of the slots with ALSA channel 0xf */
788 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
789 if (!ch_alloc->speakers[7 - hdmi_slot])
790 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
Wu Fengguang079d88c2010-03-08 10:44:23 +0800791 }
792
Wang Xingchao433968d2012-09-06 10:02:37 +0800793 if (non_pcm) {
Anssi Hannula90f28002013-10-05 02:25:39 +0300794 for (i = 0; i < ch_alloc->channels; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300795 non_pcm_mapping[i] = (i << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800796 for (; i < 8; i++)
Anssi Hannula11f7c522013-10-05 02:25:41 +0300797 non_pcm_mapping[i] = (0xf << 4) | i;
Wang Xingchao433968d2012-09-06 10:02:37 +0800798 }
799
Wu Fengguang079d88c2010-03-08 10:44:23 +0800800 for (i = 0; i < 8; i++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300801 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
802 int hdmi_slot = slotsetup & 0x0f;
803 int channel = (slotsetup & 0xf0) >> 4;
804 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
Wu Fengguang079d88c2010-03-08 10:44:23 +0800805 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100806 codec_dbg(codec, "HDMI: channel mapping failed\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +0800807 break;
808 }
809 }
Wu Fengguang079d88c2010-03-08 10:44:23 +0800810}
811
Takashi Iwaid45e6882012-07-31 11:36:00 +0200812struct channel_map_table {
813 unsigned char map; /* ALSA API channel map position */
Takashi Iwaid45e6882012-07-31 11:36:00 +0200814 int spk_mask; /* speaker position bit mask */
815};
816
817static struct channel_map_table map_tables[] = {
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300818 { SNDRV_CHMAP_FL, FL },
819 { SNDRV_CHMAP_FR, FR },
820 { SNDRV_CHMAP_RL, RL },
821 { SNDRV_CHMAP_RR, RR },
822 { SNDRV_CHMAP_LFE, LFE },
823 { SNDRV_CHMAP_FC, FC },
824 { SNDRV_CHMAP_RLC, RLC },
825 { SNDRV_CHMAP_RRC, RRC },
826 { SNDRV_CHMAP_RC, RC },
827 { SNDRV_CHMAP_FLC, FLC },
828 { SNDRV_CHMAP_FRC, FRC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200829 { SNDRV_CHMAP_TFL, FLH },
830 { SNDRV_CHMAP_TFR, FRH },
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300831 { SNDRV_CHMAP_FLW, FLW },
832 { SNDRV_CHMAP_FRW, FRW },
833 { SNDRV_CHMAP_TC, TC },
Anssi Hannula94908a32013-11-10 21:24:04 +0200834 { SNDRV_CHMAP_TFC, FCH },
Takashi Iwaid45e6882012-07-31 11:36:00 +0200835 {} /* terminator */
836};
837
838/* from ALSA API channel position to speaker bit mask */
839static int to_spk_mask(unsigned char c)
840{
841 struct channel_map_table *t = map_tables;
842 for (; t->map; t++) {
843 if (t->map == c)
844 return t->spk_mask;
845 }
846 return 0;
847}
848
849/* from ALSA API channel position to CEA slot */
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300850static int to_cea_slot(int ordered_ca, unsigned char pos)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200851{
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300852 int mask = to_spk_mask(pos);
853 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200854
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300855 if (mask) {
856 for (i = 0; i < 8; i++) {
857 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
858 return i;
859 }
Takashi Iwaid45e6882012-07-31 11:36:00 +0200860 }
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300861
862 return -1;
Takashi Iwaid45e6882012-07-31 11:36:00 +0200863}
864
865/* from speaker bit mask to ALSA API channel position */
866static int spk_to_chmap(int spk)
867{
868 struct channel_map_table *t = map_tables;
869 for (; t->map; t++) {
870 if (t->spk_mask == spk)
871 return t->map;
872 }
873 return 0;
874}
875
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300876/* from CEA slot to ALSA API channel position */
877static int from_cea_slot(int ordered_ca, unsigned char slot)
878{
879 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
880
881 return spk_to_chmap(mask);
882}
883
Takashi Iwaid45e6882012-07-31 11:36:00 +0200884/* get the CA index corresponding to the given ALSA API channel map */
885static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
886{
887 int i, spks = 0, spk_mask = 0;
888
889 for (i = 0; i < chs; i++) {
890 int mask = to_spk_mask(map[i]);
891 if (mask) {
892 spk_mask |= mask;
893 spks++;
894 }
895 }
896
897 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
898 if ((chs == channel_allocations[i].channels ||
899 spks == channel_allocations[i].channels) &&
900 (spk_mask & channel_allocations[i].spk_mask) ==
901 channel_allocations[i].spk_mask)
902 return channel_allocations[i].ca_index;
903 }
904 return -1;
905}
906
907/* set up the channel slots for the given ALSA API channel map */
908static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
909 hda_nid_t pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300910 int chs, unsigned char *map,
911 int ca)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200912{
Anssi Hannula307229d2013-10-24 21:10:34 +0300913 struct hdmi_spec *spec = codec->spec;
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300914 int ordered_ca = get_channel_allocation_order(ca);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300915 int alsa_pos, hdmi_slot;
916 int assignments[8] = {[0 ... 7] = 0xf};
917
918 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
919
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300920 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
Anssi Hannula11f7c522013-10-05 02:25:41 +0300921
922 if (hdmi_slot < 0)
923 continue; /* unassigned channel */
924
925 assignments[hdmi_slot] = alsa_pos;
926 }
927
928 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
Anssi Hannula307229d2013-10-24 21:10:34 +0300929 int err;
Anssi Hannula11f7c522013-10-05 02:25:41 +0300930
Anssi Hannula307229d2013-10-24 21:10:34 +0300931 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
932 assignments[hdmi_slot]);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200933 if (err)
934 return -EINVAL;
935 }
936 return 0;
937}
938
939/* store ALSA API channel map from the current default map */
940static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
941{
942 int i;
Anssi Hannula56cac412013-10-05 02:25:38 +0300943 int ordered_ca = get_channel_allocation_order(ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200944 for (i = 0; i < 8; i++) {
Anssi Hannula56cac412013-10-05 02:25:38 +0300945 if (i < channel_allocations[ordered_ca].channels)
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300946 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200947 else
948 map[i] = 0;
949 }
950}
951
952static void hdmi_setup_channel_mapping(struct hda_codec *codec,
953 hda_nid_t pin_nid, bool non_pcm, int ca,
Anssi Hannula20608732013-02-03 17:55:45 +0200954 int channels, unsigned char *map,
955 bool chmap_set)
Takashi Iwaid45e6882012-07-31 11:36:00 +0200956{
Anssi Hannula20608732013-02-03 17:55:45 +0200957 if (!non_pcm && chmap_set) {
Takashi Iwaid45e6882012-07-31 11:36:00 +0200958 hdmi_manual_setup_channel_mapping(codec, pin_nid,
Anssi Hannulaa5b7d512013-10-05 02:25:42 +0300959 channels, map, ca);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200960 } else {
961 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
962 hdmi_setup_fake_chmap(map, ca);
963 }
Anssi Hannula980b2492013-10-05 02:25:44 +0300964
965 hdmi_debug_channel_mapping(codec, pin_nid);
Takashi Iwaid45e6882012-07-31 11:36:00 +0200966}
Wu Fengguang079d88c2010-03-08 10:44:23 +0800967
Anssi Hannula307229d2013-10-24 21:10:34 +0300968static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
969 int asp_slot, int channel)
970{
971 return snd_hda_codec_write(codec, pin_nid, 0,
972 AC_VERB_SET_HDMI_CHAN_SLOT,
973 (channel << 4) | asp_slot);
974}
975
976static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
977 int asp_slot)
978{
979 return (snd_hda_codec_read(codec, pin_nid, 0,
980 AC_VERB_GET_HDMI_CHAN_SLOT,
981 asp_slot) & 0xf0) >> 4;
982}
983
Wu Fengguang079d88c2010-03-08 10:44:23 +0800984/*
985 * Audio InfoFrame routines
986 */
987
988/*
989 * Enable Audio InfoFrame Transmission
990 */
991static void hdmi_start_infoframe_trans(struct hda_codec *codec,
992 hda_nid_t pin_nid)
993{
994 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
995 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
996 AC_DIPXMIT_BEST);
997}
998
999/*
1000 * Disable Audio InfoFrame Transmission
1001 */
1002static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1003 hda_nid_t pin_nid)
1004{
1005 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1006 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1007 AC_DIPXMIT_DISABLE);
1008}
1009
1010static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1011{
1012#ifdef CONFIG_SND_DEBUG_VERBOSE
1013 int i;
1014 int size;
1015
1016 size = snd_hdmi_get_eld_size(codec, pin_nid);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001017 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001018
1019 for (i = 0; i < 8; i++) {
1020 size = snd_hda_codec_read(codec, pin_nid, 0,
1021 AC_VERB_GET_HDMI_DIP_SIZE, i);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001022 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001023 }
1024#endif
1025}
1026
1027static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1028{
1029#ifdef BE_PARANOID
1030 int i, j;
1031 int size;
1032 int pi, bi;
1033 for (i = 0; i < 8; i++) {
1034 size = snd_hda_codec_read(codec, pin_nid, 0,
1035 AC_VERB_GET_HDMI_DIP_SIZE, i);
1036 if (size == 0)
1037 continue;
1038
1039 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1040 for (j = 1; j < 1000; j++) {
1041 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1042 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1043 if (pi != i)
Takashi Iwai4e76a882014-02-25 12:21:03 +01001044 codec_dbg(codec, "dip index %d: %d != %d\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001045 bi, pi, i);
1046 if (bi == 0) /* byte index wrapped around */
1047 break;
1048 }
Takashi Iwai4e76a882014-02-25 12:21:03 +01001049 codec_dbg(codec,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001050 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1051 i, size, j);
1052 }
1053#endif
1054}
1055
Wu Fengguang53d7d692010-09-21 14:25:49 +08001056static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001057{
Wu Fengguang53d7d692010-09-21 14:25:49 +08001058 u8 *bytes = (u8 *)hdmi_ai;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001059 u8 sum = 0;
1060 int i;
1061
Wu Fengguang53d7d692010-09-21 14:25:49 +08001062 hdmi_ai->checksum = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001063
Wu Fengguang53d7d692010-09-21 14:25:49 +08001064 for (i = 0; i < sizeof(*hdmi_ai); i++)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001065 sum += bytes[i];
1066
Wu Fengguang53d7d692010-09-21 14:25:49 +08001067 hdmi_ai->checksum = -sum;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001068}
1069
1070static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1071 hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001072 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001073{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001074 int i;
1075
1076 hdmi_debug_dip_size(codec, pin_nid);
1077 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1078
Wu Fengguang079d88c2010-03-08 10:44:23 +08001079 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001080 for (i = 0; i < size; i++)
1081 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001082}
1083
1084static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
Wu Fengguang53d7d692010-09-21 14:25:49 +08001085 u8 *dip, int size)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001086{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001087 u8 val;
1088 int i;
1089
1090 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1091 != AC_DIPXMIT_BEST)
1092 return false;
1093
1094 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001095 for (i = 0; i < size; i++) {
Wu Fengguang079d88c2010-03-08 10:44:23 +08001096 val = snd_hda_codec_read(codec, pin_nid, 0,
1097 AC_VERB_GET_HDMI_DIP_DATA, 0);
Wu Fengguang53d7d692010-09-21 14:25:49 +08001098 if (val != dip[i])
Wu Fengguang079d88c2010-03-08 10:44:23 +08001099 return false;
1100 }
1101
1102 return true;
1103}
1104
Anssi Hannula307229d2013-10-24 21:10:34 +03001105static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1106 hda_nid_t pin_nid,
1107 int ca, int active_channels,
1108 int conn_type)
1109{
1110 union audio_infoframe ai;
1111
Mengdong Lincaaf5ef2014-03-11 17:12:52 -04001112 memset(&ai, 0, sizeof(ai));
Anssi Hannula307229d2013-10-24 21:10:34 +03001113 if (conn_type == 0) { /* HDMI */
1114 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1115
1116 hdmi_ai->type = 0x84;
1117 hdmi_ai->ver = 0x01;
1118 hdmi_ai->len = 0x0a;
1119 hdmi_ai->CC02_CT47 = active_channels - 1;
1120 hdmi_ai->CA = ca;
1121 hdmi_checksum_audio_infoframe(hdmi_ai);
1122 } else if (conn_type == 1) { /* DisplayPort */
1123 struct dp_audio_infoframe *dp_ai = &ai.dp;
1124
1125 dp_ai->type = 0x84;
1126 dp_ai->len = 0x1b;
1127 dp_ai->ver = 0x11 << 2;
1128 dp_ai->CC02_CT47 = active_channels - 1;
1129 dp_ai->CA = ca;
1130 } else {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001131 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001132 pin_nid);
1133 return;
1134 }
1135
1136 /*
1137 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1138 * sizeof(*dp_ai) to avoid partial match/update problems when
1139 * the user switches between HDMI/DP monitors.
1140 */
1141 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1142 sizeof(ai))) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001143 codec_dbg(codec,
1144 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
Anssi Hannula307229d2013-10-24 21:10:34 +03001145 pin_nid,
1146 active_channels, ca);
1147 hdmi_stop_infoframe_trans(codec, pin_nid);
1148 hdmi_fill_audio_infoframe(codec, pin_nid,
1149 ai.bytes, sizeof(ai));
1150 hdmi_start_infoframe_trans(codec, pin_nid);
1151 }
1152}
1153
Takashi Iwaib0540872013-09-02 12:33:02 +02001154static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1155 struct hdmi_spec_per_pin *per_pin,
1156 bool non_pcm)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001157{
Anssi Hannula307229d2013-10-24 21:10:34 +03001158 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001159 hda_nid_t pin_nid = per_pin->pin_nid;
Takashi Iwaib0540872013-09-02 12:33:02 +02001160 int channels = per_pin->channels;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001161 int active_channels;
Stephen Warren384a48d2011-06-01 11:14:21 -06001162 struct hdmi_eld *eld;
Anssi Hannula1df5a062013-10-05 02:25:40 +03001163 int ca, ordered_ca;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001164
Takashi Iwaib0540872013-09-02 12:33:02 +02001165 if (!channels)
1166 return;
1167
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001168 if (is_haswell_plus(codec))
Mengdong Lin58f7d282013-09-04 16:37:12 -04001169 snd_hda_codec_write(codec, pin_nid, 0,
1170 AC_VERB_SET_AMP_GAIN_MUTE,
1171 AMP_OUT_UNMUTE);
1172
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001173 eld = &per_pin->sink_eld;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001174
Takashi Iwaid45e6882012-07-31 11:36:00 +02001175 if (!non_pcm && per_pin->chmap_set)
1176 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1177 else
Takashi Iwai79514d42014-06-06 18:04:34 +02001178 ca = hdmi_channel_allocation(codec, eld, channels);
Takashi Iwaid45e6882012-07-31 11:36:00 +02001179 if (ca < 0)
1180 ca = 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001181
Anssi Hannula1df5a062013-10-05 02:25:40 +03001182 ordered_ca = get_channel_allocation_order(ca);
1183 active_channels = channel_allocations[ordered_ca].channels;
1184
1185 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1186
Stephen Warren384a48d2011-06-01 11:14:21 -06001187 /*
Anssi Hannula39edac72013-10-07 19:24:52 +03001188 * always configure channel mapping, it may have been changed by the
1189 * user in the meantime
1190 */
1191 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1192 channels, per_pin->chmap,
1193 per_pin->chmap_set);
1194
Anssi Hannula307229d2013-10-24 21:10:34 +03001195 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1196 eld->info.conn_type);
Wang Xingchao433968d2012-09-06 10:02:37 +08001197
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001198 per_pin->non_pcm = non_pcm;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001199}
1200
Wu Fengguang079d88c2010-03-08 10:44:23 +08001201/*
1202 * Unsolicited events
1203 */
1204
Takashi Iwaiefe47102013-11-07 13:38:23 +01001205static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
Takashi Iwai38faddb2010-07-28 14:21:55 +02001206
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001207static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001208{
1209 struct hdmi_spec *spec = codec->spec;
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001210 int pin_idx = pin_nid_to_pin_index(codec, nid);
1211
David Henningsson20ce9022013-12-04 10:19:41 +08001212 if (pin_idx < 0)
1213 return;
David Henningsson20ce9022013-12-04 10:19:41 +08001214 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1215 snd_hda_jack_report_sync(codec);
1216}
1217
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001218static void jack_callback(struct hda_codec *codec,
1219 struct hda_jack_callback *jack)
1220{
1221 check_presence_and_report(codec, jack->tbl->nid);
1222}
1223
David Henningsson20ce9022013-12-04 10:19:41 +08001224static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1225{
Takashi Iwai3a938972011-10-28 01:16:55 +02001226 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001227 struct hda_jack_tbl *jack;
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001228 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
Takashi Iwai3a938972011-10-28 01:16:55 +02001229
1230 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1231 if (!jack)
1232 return;
Takashi Iwai3a938972011-10-28 01:16:55 +02001233 jack->jack_dirty = 1;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001234
Takashi Iwai4e76a882014-02-25 12:21:03 +01001235 codec_dbg(codec,
Mengdong Lin2e59e5a2013-08-26 21:35:49 -04001236 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
David Henningsson20ce9022013-12-04 10:19:41 +08001237 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
Fengguang Wufae3d882012-04-10 17:00:35 +08001238 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
Wu Fengguang079d88c2010-03-08 10:44:23 +08001239
Takashi Iwai1a4f69d2014-09-11 15:22:46 +02001240 check_presence_and_report(codec, jack->nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001241}
1242
1243static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1244{
1245 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1246 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1247 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1248 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1249
Takashi Iwai4e76a882014-02-25 12:21:03 +01001250 codec_info(codec,
Takashi Iwaie9ea8e82012-06-21 11:41:05 +02001251 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001252 codec->addr,
Wu Fengguang079d88c2010-03-08 10:44:23 +08001253 tag,
1254 subtag,
1255 cp_state,
1256 cp_ready);
1257
1258 /* TODO */
1259 if (cp_state)
1260 ;
1261 if (cp_ready)
1262 ;
1263}
1264
1265
1266static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1267{
Wu Fengguang079d88c2010-03-08 10:44:23 +08001268 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1269 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1270
Takashi Iwai3a938972011-10-28 01:16:55 +02001271 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001272 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001273 return;
1274 }
1275
1276 if (subtag == 0)
1277 hdmi_intrinsic_event(codec, res);
1278 else
1279 hdmi_non_intrinsic_event(codec, res);
1280}
1281
Mengdong Lin58f7d282013-09-04 16:37:12 -04001282static void haswell_verify_D0(struct hda_codec *codec,
Wang Xingchao53b434f2013-06-18 10:41:53 +08001283 hda_nid_t cvt_nid, hda_nid_t nid)
David Henningsson83f26ad2013-04-10 12:26:07 +02001284{
Mengdong Lin58f7d282013-09-04 16:37:12 -04001285 int pwr;
David Henningsson83f26ad2013-04-10 12:26:07 +02001286
Wang Xingchao53b434f2013-06-18 10:41:53 +08001287 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1288 * thus pins could only choose converter 0 for use. Make sure the
1289 * converters are in correct power state */
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001290 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
Wang Xingchao53b434f2013-06-18 10:41:53 +08001291 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1292
Takashi Iwaifd678ca2013-06-18 16:28:36 +02001293 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
David Henningsson83f26ad2013-04-10 12:26:07 +02001294 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1295 AC_PWRST_D0);
1296 msleep(40);
1297 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1298 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
Takashi Iwai4e76a882014-02-25 12:21:03 +01001299 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
David Henningsson83f26ad2013-04-10 12:26:07 +02001300 }
David Henningsson83f26ad2013-04-10 12:26:07 +02001301}
1302
Wu Fengguang079d88c2010-03-08 10:44:23 +08001303/*
1304 * Callbacks
1305 */
1306
Takashi Iwai92f10b32010-08-03 14:21:00 +02001307/* HBR should be Non-PCM, 8 channels */
1308#define is_hbr_format(format) \
1309 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1310
Anssi Hannula307229d2013-10-24 21:10:34 +03001311static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1312 bool hbr)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001313{
Anssi Hannula307229d2013-10-24 21:10:34 +03001314 int pinctl, new_pinctl;
David Henningsson83f26ad2013-04-10 12:26:07 +02001315
Stephen Warren384a48d2011-06-01 11:14:21 -06001316 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1317 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001318 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1319
Anssi Hannula13122e62013-11-10 20:56:10 +02001320 if (pinctl < 0)
1321 return hbr ? -EINVAL : 0;
1322
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001323 new_pinctl = pinctl & ~AC_PINCTL_EPT;
Anssi Hannula307229d2013-10-24 21:10:34 +03001324 if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001325 new_pinctl |= AC_PINCTL_EPT_HBR;
1326 else
1327 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1328
Takashi Iwai4e76a882014-02-25 12:21:03 +01001329 codec_dbg(codec,
1330 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
Stephen Warren384a48d2011-06-01 11:14:21 -06001331 pin_nid,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001332 pinctl == new_pinctl ? "" : "new-",
1333 new_pinctl);
1334
1335 if (pinctl != new_pinctl)
Stephen Warren384a48d2011-06-01 11:14:21 -06001336 snd_hda_codec_write(codec, pin_nid, 0,
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001337 AC_VERB_SET_PIN_WIDGET_CONTROL,
1338 new_pinctl);
Anssi Hannula307229d2013-10-24 21:10:34 +03001339 } else if (hbr)
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001340 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03001341
1342 return 0;
1343}
1344
1345static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1346 hda_nid_t pin_nid, u32 stream_tag, int format)
1347{
1348 struct hdmi_spec *spec = codec->spec;
1349 int err;
1350
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001351 if (is_haswell_plus(codec))
Anssi Hannula307229d2013-10-24 21:10:34 +03001352 haswell_verify_D0(codec, cvt_nid, pin_nid);
1353
1354 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1355
1356 if (err) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001357 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
Anssi Hannula307229d2013-10-24 21:10:34 +03001358 return err;
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001359 }
Wu Fengguang079d88c2010-03-08 10:44:23 +08001360
Stephen Warren384a48d2011-06-01 11:14:21 -06001361 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
Anssi Hannulaea87d1c2010-08-03 13:28:58 +03001362 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001363}
1364
Libin Yang42b29872015-12-16 13:42:42 +08001365/* Try to find an available converter
1366 * If pin_idx is less then zero, just try to find an available converter.
1367 * Otherwise, try to find an available converter and get the cvt mux index
1368 * of the pin.
1369 */
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001370static int hdmi_choose_cvt(struct hda_codec *codec,
1371 int pin_idx, int *cvt_id, int *mux_id)
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001372{
1373 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001374 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06001375 struct hdmi_spec_per_cvt *per_cvt = NULL;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001376 int cvt_idx, mux_idx = 0;
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001377
Libin Yang42b29872015-12-16 13:42:42 +08001378 /* pin_idx < 0 means no pin will be bound to the converter */
1379 if (pin_idx < 0)
1380 per_pin = NULL;
1381 else
1382 per_pin = get_pin(spec, pin_idx);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001383
Stephen Warren384a48d2011-06-01 11:14:21 -06001384 /* Dynamically assign converter to stream */
1385 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001386 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001387
1388 /* Must not already be assigned */
1389 if (per_cvt->assigned)
1390 continue;
Libin Yang42b29872015-12-16 13:42:42 +08001391 if (per_pin == NULL)
1392 break;
Stephen Warren384a48d2011-06-01 11:14:21 -06001393 /* Must be in pin's mux's list of converters */
1394 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1395 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1396 break;
1397 /* Not in mux list */
1398 if (mux_idx == per_pin->num_mux_nids)
1399 continue;
1400 break;
1401 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001402
Stephen Warren384a48d2011-06-01 11:14:21 -06001403 /* No free converters */
1404 if (cvt_idx == spec->num_cvts)
Libin Yang42b29872015-12-16 13:42:42 +08001405 return -EBUSY;
Stephen Warren384a48d2011-06-01 11:14:21 -06001406
Libin Yang42b29872015-12-16 13:42:42 +08001407 if (per_pin != NULL)
1408 per_pin->mux_idx = mux_idx;
Mengdong Lin2df67422014-03-20 13:01:06 +08001409
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001410 if (cvt_id)
1411 *cvt_id = cvt_idx;
1412 if (mux_id)
1413 *mux_id = mux_idx;
1414
1415 return 0;
1416}
1417
Mengdong Lin2df67422014-03-20 13:01:06 +08001418/* Assure the pin select the right convetor */
1419static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1420 struct hdmi_spec_per_pin *per_pin)
1421{
1422 hda_nid_t pin_nid = per_pin->pin_nid;
1423 int mux_idx, curr;
1424
1425 mux_idx = per_pin->mux_idx;
1426 curr = snd_hda_codec_read(codec, pin_nid, 0,
1427 AC_VERB_GET_CONNECT_SEL, 0);
1428 if (curr != mux_idx)
1429 snd_hda_codec_write_cache(codec, pin_nid, 0,
1430 AC_VERB_SET_CONNECT_SEL,
1431 mux_idx);
1432}
1433
Libin Yang42b29872015-12-16 13:42:42 +08001434/* get the mux index for the converter of the pins
1435 * converter's mux index is the same for all pins on Intel platform
1436 */
1437static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1438 hda_nid_t cvt_nid)
1439{
1440 int i;
1441
1442 for (i = 0; i < spec->num_cvts; i++)
1443 if (spec->cvt_nids[i] == cvt_nid)
1444 return i;
1445 return -EINVAL;
1446}
1447
Mengdong Lin300016b2013-11-04 01:13:13 -05001448/* Intel HDMI workaround to fix audio routing issue:
1449 * For some Intel display codecs, pins share the same connection list.
1450 * So a conveter can be selected by multiple pins and playback on any of these
1451 * pins will generate sound on the external display, because audio flows from
1452 * the same converter to the display pipeline. Also muting one pin may make
1453 * other pins have no sound output.
1454 * So this function assures that an assigned converter for a pin is not selected
1455 * by any other pins.
1456 */
1457static void intel_not_share_assigned_cvt(struct hda_codec *codec,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001458 hda_nid_t pin_nid, int mux_idx)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001459{
1460 struct hdmi_spec *spec = codec->spec;
Takashi Iwai7639a062015-03-03 10:07:24 +01001461 hda_nid_t nid;
Mengdong Linf82d7d12013-09-21 20:34:45 -04001462 int cvt_idx, curr;
1463 struct hdmi_spec_per_cvt *per_cvt;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001464
Mengdong Linf82d7d12013-09-21 20:34:45 -04001465 /* configure all pins, including "no physical connection" ones */
Takashi Iwai7639a062015-03-03 10:07:24 +01001466 for_each_hda_codec_node(nid, codec) {
Mengdong Linf82d7d12013-09-21 20:34:45 -04001467 unsigned int wid_caps = get_wcaps(codec, nid);
1468 unsigned int wid_type = get_wcaps_type(wid_caps);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001469
Mengdong Linf82d7d12013-09-21 20:34:45 -04001470 if (wid_type != AC_WID_PIN)
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001471 continue;
1472
Mengdong Linf82d7d12013-09-21 20:34:45 -04001473 if (nid == pin_nid)
1474 continue;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001475
Mengdong Linf82d7d12013-09-21 20:34:45 -04001476 curr = snd_hda_codec_read(codec, nid, 0,
1477 AC_VERB_GET_CONNECT_SEL, 0);
1478 if (curr != mux_idx)
1479 continue;
1480
1481 /* choose an unassigned converter. The conveters in the
1482 * connection list are in the same order as in the codec.
1483 */
1484 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1485 per_cvt = get_cvt(spec, cvt_idx);
1486 if (!per_cvt->assigned) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001487 codec_dbg(codec,
1488 "choose cvt %d for pin nid %d\n",
Mengdong Linf82d7d12013-09-21 20:34:45 -04001489 cvt_idx, nid);
1490 snd_hda_codec_write_cache(codec, nid, 0,
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001491 AC_VERB_SET_CONNECT_SEL,
Mengdong Linf82d7d12013-09-21 20:34:45 -04001492 cvt_idx);
1493 break;
1494 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001495 }
1496 }
1497}
1498
Libin Yang42b29872015-12-16 13:42:42 +08001499/* A wrapper of intel_not_share_asigned_cvt() */
1500static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1501 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1502{
1503 int mux_idx;
1504 struct hdmi_spec *spec = codec->spec;
1505
1506 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1507 return;
1508
1509 /* On Intel platform, the mapping of converter nid to
1510 * mux index of the pins are always the same.
1511 * The pin nid may be 0, this means all pins will not
1512 * share the converter.
1513 */
1514 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1515 if (mux_idx >= 0)
1516 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1517}
1518
1519/* called in hdmi_pcm_open when no pin is assigned to the PCM
1520 * in dyn_pcm_assign mode.
1521 */
1522static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1523 struct hda_codec *codec,
1524 struct snd_pcm_substream *substream)
1525{
1526 struct hdmi_spec *spec = codec->spec;
1527 struct snd_pcm_runtime *runtime = substream->runtime;
1528 int cvt_idx;
1529 struct hdmi_spec_per_cvt *per_cvt = NULL;
1530 int err;
1531
1532 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1533 if (err)
1534 return err;
1535
1536 per_cvt = get_cvt(spec, cvt_idx);
1537 per_cvt->assigned = 1;
1538 hinfo->nid = per_cvt->cvt_nid;
1539
1540 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1541
1542 /* todo: setup spdif ctls assign */
1543
1544 /* Initially set the converter's capabilities */
1545 hinfo->channels_min = per_cvt->channels_min;
1546 hinfo->channels_max = per_cvt->channels_max;
1547 hinfo->rates = per_cvt->rates;
1548 hinfo->formats = per_cvt->formats;
1549 hinfo->maxbps = per_cvt->maxbps;
1550
1551 /* Store the updated parameters */
1552 runtime->hw.channels_min = hinfo->channels_min;
1553 runtime->hw.channels_max = hinfo->channels_max;
1554 runtime->hw.formats = hinfo->formats;
1555 runtime->hw.rates = hinfo->rates;
1556
1557 snd_pcm_hw_constraint_step(substream->runtime, 0,
1558 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1559 return 0;
1560}
1561
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001562/*
1563 * HDA PCM callbacks
1564 */
1565static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1566 struct hda_codec *codec,
1567 struct snd_pcm_substream *substream)
1568{
1569 struct hdmi_spec *spec = codec->spec;
1570 struct snd_pcm_runtime *runtime = substream->runtime;
Libin Yang2bf3c852015-12-16 13:42:43 +08001571 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001572 struct hdmi_spec_per_pin *per_pin;
1573 struct hdmi_eld *eld;
1574 struct hdmi_spec_per_cvt *per_cvt = NULL;
1575 int err;
1576
1577 /* Validate hinfo */
Libin Yang2bf3c852015-12-16 13:42:43 +08001578 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1579 if (pcm_idx < 0)
1580 return -EINVAL;
1581
Libin Yang42b29872015-12-16 13:42:42 +08001582 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01001583 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08001584 if (!spec->dyn_pcm_assign) {
1585 if (snd_BUG_ON(pin_idx < 0)) {
1586 mutex_unlock(&spec->pcm_lock);
1587 return -EINVAL;
1588 }
1589 } else {
1590 /* no pin is assigned to the PCM
1591 * PA need pcm open successfully when probe
1592 */
1593 if (pin_idx < 0) {
1594 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1595 mutex_unlock(&spec->pcm_lock);
1596 return err;
1597 }
1598 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001599
1600 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001601 if (err < 0) {
1602 mutex_unlock(&spec->pcm_lock);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001603 return err;
Libin Yang42b29872015-12-16 13:42:42 +08001604 }
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001605
1606 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001607 /* Claim converter */
1608 per_cvt->assigned = 1;
Libin Yang42b29872015-12-16 13:42:42 +08001609
1610
1611 per_pin = get_pin(spec, pin_idx);
Anssi Hannula1df5a062013-10-05 02:25:40 +03001612 per_pin->cvt_nid = per_cvt->cvt_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06001613 hinfo->nid = per_cvt->cvt_nid;
1614
Takashi Iwaibddee962013-06-18 16:14:22 +02001615 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
Stephen Warren384a48d2011-06-01 11:14:21 -06001616 AC_VERB_SET_CONNECT_SEL,
1617 mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001618
1619 /* configure unused pins to choose other converters */
Libin Yangca2e7222014-08-19 16:20:12 +08001620 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
Mengdong Lin300016b2013-11-04 01:13:13 -05001621 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
Wang Xingchao7ef166b2013-06-18 21:42:14 +08001622
Libin Yang2bf3c852015-12-16 13:42:43 +08001623 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001624
Stephen Warren2def8172011-06-01 11:14:20 -06001625 /* Initially set the converter's capabilities */
Stephen Warren384a48d2011-06-01 11:14:21 -06001626 hinfo->channels_min = per_cvt->channels_min;
1627 hinfo->channels_max = per_cvt->channels_max;
1628 hinfo->rates = per_cvt->rates;
1629 hinfo->formats = per_cvt->formats;
1630 hinfo->maxbps = per_cvt->maxbps;
Stephen Warren2def8172011-06-01 11:14:20 -06001631
Libin Yang42b29872015-12-16 13:42:42 +08001632 eld = &per_pin->sink_eld;
Stephen Warren384a48d2011-06-01 11:14:21 -06001633 /* Restrict capabilities by ELD if this isn't disabled */
Stephen Warrenc3d52102011-06-01 11:14:16 -06001634 if (!static_hdmi_pcm && eld->eld_valid) {
David Henningsson1613d6b2013-02-19 16:11:24 +01001635 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001636 if (hinfo->channels_min > hinfo->channels_max ||
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001637 !hinfo->rates || !hinfo->formats) {
1638 per_cvt->assigned = 0;
1639 hinfo->nid = 0;
Libin Yang2bf3c852015-12-16 13:42:43 +08001640 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Libin Yang42b29872015-12-16 13:42:42 +08001641 mutex_unlock(&spec->pcm_lock);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001642 return -ENODEV;
Takashi Iwai2ad779b2013-02-01 14:01:27 +01001643 }
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001644 }
Stephen Warren2def8172011-06-01 11:14:20 -06001645
Libin Yang42b29872015-12-16 13:42:42 +08001646 mutex_unlock(&spec->pcm_lock);
Stephen Warren2def8172011-06-01 11:14:20 -06001647 /* Store the updated parameters */
Takashi Iwai639cef02011-01-14 10:30:46 +01001648 runtime->hw.channels_min = hinfo->channels_min;
1649 runtime->hw.channels_max = hinfo->channels_max;
1650 runtime->hw.formats = hinfo->formats;
1651 runtime->hw.rates = hinfo->rates;
Takashi Iwai4fe2ca12011-01-14 10:33:26 +01001652
1653 snd_pcm_hw_constraint_step(substream->runtime, 0,
1654 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Takashi Iwaibbbe3392010-08-13 08:45:23 +02001655 return 0;
1656}
1657
1658/*
Wu Fengguang079d88c2010-03-08 10:44:23 +08001659 * HDA/HDMI auto parsing
1660 */
Stephen Warren384a48d2011-06-01 11:14:21 -06001661static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001662{
1663 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001664 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06001665 hda_nid_t pin_nid = per_pin->pin_nid;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001666
1667 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01001668 codec_warn(codec,
1669 "HDMI: pin %d wcaps %#x does not support connection list\n",
Wu Fengguang079d88c2010-03-08 10:44:23 +08001670 pin_nid, get_wcaps(codec, pin_nid));
1671 return -EINVAL;
1672 }
1673
Stephen Warren384a48d2011-06-01 11:14:21 -06001674 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1675 per_pin->mux_nids,
1676 HDA_MAX_CONNECTIONS);
Wu Fengguang079d88c2010-03-08 10:44:23 +08001677
1678 return 0;
1679}
1680
Libin Yanga76056f2015-12-16 16:48:15 +08001681static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1682 struct hdmi_spec_per_pin *per_pin)
1683{
1684 int i;
1685
1686 /* try the prefer PCM */
1687 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1688 return per_pin->pin_nid_idx;
1689
1690 /* have a second try; check the "reserved area" over num_pins */
1691 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1692 if (!test_bit(i, &spec->pcm_bitmap))
1693 return i;
1694 }
1695
1696 /* the last try; check the empty slots in pins */
1697 for (i = 0; i < spec->num_pins; i++) {
1698 if (!test_bit(i, &spec->pcm_bitmap))
1699 return i;
1700 }
1701 return -EBUSY;
1702}
1703
1704static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1705 struct hdmi_spec_per_pin *per_pin)
1706{
1707 int idx;
1708
1709 /* pcm already be attached to the pin */
1710 if (per_pin->pcm)
1711 return;
1712 idx = hdmi_find_pcm_slot(spec, per_pin);
1713 if (idx == -ENODEV)
1714 return;
1715 per_pin->pcm_idx = idx;
1716 per_pin->pcm = spec->pcm_rec[idx];
1717 set_bit(idx, &spec->pcm_bitmap);
1718}
1719
1720static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1721 struct hdmi_spec_per_pin *per_pin)
1722{
1723 int idx;
1724
1725 /* pcm already be detached from the pin */
1726 if (!per_pin->pcm)
1727 return;
1728 idx = per_pin->pcm_idx;
1729 per_pin->pcm_idx = -1;
1730 per_pin->pcm = NULL;
1731 if (idx >= 0 && idx < spec->pcm_used)
1732 clear_bit(idx, &spec->pcm_bitmap);
1733}
1734
Takashi Iwaie90247f2015-11-13 09:12:12 +01001735/* update per_pin ELD from the given new ELD;
1736 * setup info frame and notification accordingly
1737 */
1738static void update_eld(struct hda_codec *codec,
1739 struct hdmi_spec_per_pin *per_pin,
1740 struct hdmi_eld *eld)
1741{
1742 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Libin Yanga76056f2015-12-16 16:48:15 +08001743 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie90247f2015-11-13 09:12:12 +01001744 bool old_eld_valid = pin_eld->eld_valid;
1745 bool eld_changed;
1746
Libin Yanga76056f2015-12-16 16:48:15 +08001747 if (spec->dyn_pcm_assign) {
1748 if (eld->eld_valid)
1749 hdmi_attach_hda_pcm(spec, per_pin);
1750 else
1751 hdmi_detach_hda_pcm(spec, per_pin);
1752 }
1753
Takashi Iwaie90247f2015-11-13 09:12:12 +01001754 if (eld->eld_valid)
1755 snd_hdmi_show_eld(codec, &eld->info);
1756
1757 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1758 if (eld->eld_valid && pin_eld->eld_valid)
1759 if (pin_eld->eld_size != eld->eld_size ||
1760 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1761 eld->eld_size) != 0)
1762 eld_changed = true;
1763
1764 pin_eld->eld_valid = eld->eld_valid;
1765 pin_eld->eld_size = eld->eld_size;
1766 if (eld->eld_valid)
1767 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1768 pin_eld->info = eld->info;
1769
1770 /*
1771 * Re-setup pin and infoframe. This is needed e.g. when
1772 * - sink is first plugged-in
1773 * - transcoder can change during stream playback on Haswell
1774 * and this can make HW reset converter selection on a pin.
1775 */
1776 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1777 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1778 intel_verify_pin_cvt_connect(codec, per_pin);
1779 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1780 per_pin->mux_idx);
1781 }
1782
1783 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1784 }
1785
1786 if (eld_changed)
1787 snd_ctl_notify(codec->card,
1788 SNDRV_CTL_EVENT_MASK_VALUE |
1789 SNDRV_CTL_EVENT_MASK_INFO,
1790 &per_pin->eld_ctl->id);
1791}
1792
Takashi Iwai788d4412015-11-12 15:36:13 +01001793/* update ELD and jack state via HD-audio verbs */
1794static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1795 int repoll)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001796{
David Henningsson464837a2013-11-07 13:38:25 +01001797 struct hda_jack_tbl *jack;
Wu Fengguang744626d2011-11-16 16:29:47 +08001798 struct hda_codec *codec = per_pin->codec;
David Henningsson4bd038f2013-02-19 16:11:25 +01001799 struct hdmi_spec *spec = codec->spec;
1800 struct hdmi_eld *eld = &spec->temp_eld;
1801 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
Wu Fengguang744626d2011-11-16 16:29:47 +08001802 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren5d44f922011-05-24 17:11:17 -06001803 /*
1804 * Always execute a GetPinSense verb here, even when called from
1805 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1806 * response's PD bit is not the real PD value, but indicates that
1807 * the real PD value changed. An older version of the HD-audio
1808 * specification worked this way. Hence, we just ignore the data in
1809 * the unsolicited response to avoid custom WARs.
1810 */
David Henningssonda4a7a32013-12-18 10:46:04 +01001811 int present;
Takashi Iwaiefe47102013-11-07 13:38:23 +01001812 bool ret;
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001813 bool do_repoll = false;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001814
Takashi Iwai664c7152015-04-08 11:43:14 +02001815 snd_hda_power_up_pm(codec);
David Henningssonda4a7a32013-12-18 10:46:04 +01001816 present = snd_hda_pin_sense(codec, pin_nid);
1817
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001818 mutex_lock(&per_pin->lock);
David Henningsson4bd038f2013-02-19 16:11:25 +01001819 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1820 if (pin_eld->monitor_present)
1821 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1822 else
1823 eld->eld_valid = false;
Stephen Warren5d44f922011-05-24 17:11:17 -06001824
Takashi Iwai4e76a882014-02-25 12:21:03 +01001825 codec_dbg(codec,
Stephen Warren384a48d2011-06-01 11:14:21 -06001826 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
Mengdong Lin10250912013-03-28 05:21:28 -04001827 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
Stephen Warren5d44f922011-05-24 17:11:17 -06001828
David Henningsson4bd038f2013-02-19 16:11:25 +01001829 if (eld->eld_valid) {
Anssi Hannula307229d2013-10-24 21:10:34 +03001830 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001831 &eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001832 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001833 else {
Takashi Iwai79514d42014-06-06 18:04:34 +02001834 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
David Henningsson1613d6b2013-02-19 16:11:24 +01001835 eld->eld_size) < 0)
David Henningsson4bd038f2013-02-19 16:11:25 +01001836 eld->eld_valid = false;
David Henningsson1613d6b2013-02-19 16:11:24 +01001837 }
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001838 if (!eld->eld_valid && repoll)
1839 do_repoll = true;
Wu Fengguang744626d2011-11-16 16:29:47 +08001840 }
David Henningsson4bd038f2013-02-19 16:11:25 +01001841
Takashi Iwai9a5e5232015-12-10 14:35:09 +01001842 if (do_repoll)
Takashi Iwaie90247f2015-11-13 09:12:12 +01001843 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1844 else
1845 update_eld(codec, per_pin, eld);
Anssi Hannula6acce402014-10-19 19:25:19 +03001846
Takashi Iwaiaff747eb2013-11-07 16:39:37 +01001847 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
David Henningsson464837a2013-11-07 13:38:25 +01001848
1849 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1850 if (jack)
1851 jack->block_report = !ret;
1852
Takashi Iwaia4e9a382013-10-17 18:21:12 +02001853 mutex_unlock(&per_pin->lock);
Takashi Iwai664c7152015-04-08 11:43:14 +02001854 snd_hda_power_down_pm(codec);
Takashi Iwaiefe47102013-11-07 13:38:23 +01001855 return ret;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001856}
1857
Takashi Iwai788d4412015-11-12 15:36:13 +01001858/* update ELD and jack state via audio component */
1859static void sync_eld_via_acomp(struct hda_codec *codec,
1860 struct hdmi_spec_per_pin *per_pin)
1861{
Takashi Iwai788d4412015-11-12 15:36:13 +01001862 struct hdmi_spec *spec = codec->spec;
1863 struct hdmi_eld *eld = &spec->temp_eld;
1864 int size;
1865
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001866 mutex_lock(&per_pin->lock);
1867 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1868 &eld->monitor_present, eld->eld_buffer,
1869 ELD_MAX_SIZE);
1870 if (size < 0)
1871 goto unlock;
1872 if (size > 0) {
1873 size = min(size, ELD_MAX_SIZE);
1874 if (snd_hdmi_parse_eld(codec, &eld->info,
1875 eld->eld_buffer, size) < 0)
1876 size = -EINVAL;
Takashi Iwai788d4412015-11-12 15:36:13 +01001877 }
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01001878
1879 if (size > 0) {
1880 eld->eld_valid = true;
1881 eld->eld_size = size;
1882 } else {
1883 eld->eld_valid = false;
1884 eld->eld_size = 0;
1885 }
1886
1887 update_eld(codec, per_pin, eld);
1888 snd_jack_report(per_pin->acomp_jack,
1889 eld->monitor_present ? SND_JACK_AVOUT : 0);
1890 unlock:
1891 mutex_unlock(&per_pin->lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001892}
1893
1894static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1895{
1896 struct hda_codec *codec = per_pin->codec;
Libin Yanga76056f2015-12-16 16:48:15 +08001897 struct hdmi_spec *spec = codec->spec;
1898 int ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001899
Libin Yanga76056f2015-12-16 16:48:15 +08001900 mutex_lock(&spec->pcm_lock);
Takashi Iwai788d4412015-11-12 15:36:13 +01001901 if (codec_has_acomp(codec)) {
1902 sync_eld_via_acomp(codec, per_pin);
Libin Yanga76056f2015-12-16 16:48:15 +08001903 ret = false; /* don't call snd_hda_jack_report_sync() */
Takashi Iwai788d4412015-11-12 15:36:13 +01001904 } else {
Libin Yanga76056f2015-12-16 16:48:15 +08001905 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
Takashi Iwai788d4412015-11-12 15:36:13 +01001906 }
Libin Yanga76056f2015-12-16 16:48:15 +08001907 mutex_unlock(&spec->pcm_lock);
1908
1909 return ret;
Takashi Iwai788d4412015-11-12 15:36:13 +01001910}
1911
Wu Fengguang744626d2011-11-16 16:29:47 +08001912static void hdmi_repoll_eld(struct work_struct *work)
1913{
1914 struct hdmi_spec_per_pin *per_pin =
1915 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1916
Wu Fengguangc6e84532011-11-18 16:59:32 -06001917 if (per_pin->repoll_count++ > 6)
1918 per_pin->repoll_count = 0;
1919
Takashi Iwaiefe47102013-11-07 13:38:23 +01001920 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
1921 snd_hda_jack_report_sync(per_pin->codec);
Wu Fengguang744626d2011-11-16 16:29:47 +08001922}
1923
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001924static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1925 hda_nid_t nid);
1926
Wu Fengguang079d88c2010-03-08 10:44:23 +08001927static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1928{
1929 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001930 unsigned int caps, config;
1931 int pin_idx;
1932 struct hdmi_spec_per_pin *per_pin;
David Henningsson07acecc2011-05-19 11:46:03 +02001933 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001934
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001935 caps = snd_hda_query_pin_caps(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001936 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1937 return 0;
1938
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01001939 config = snd_hda_codec_get_pincfg(codec, pin_nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06001940 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1941 return 0;
1942
Mengdong Lin75dcbe42014-01-08 15:55:32 -05001943 if (is_haswell_plus(codec))
Takashi Iwaic88d4e82013-02-08 17:10:04 -05001944 intel_haswell_fixup_connect_list(codec, pin_nid);
1945
Stephen Warren384a48d2011-06-01 11:14:21 -06001946 pin_idx = spec->num_pins;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001947 per_pin = snd_array_new(&spec->pins);
1948 if (!per_pin)
1949 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001950
1951 per_pin->pin_nid = pin_nid;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02001952 per_pin->non_pcm = false;
Libin Yanga76056f2015-12-16 16:48:15 +08001953 if (spec->dyn_pcm_assign)
1954 per_pin->pcm_idx = -1;
1955 else
1956 per_pin->pcm_idx = pin_idx;
1957 per_pin->pin_nid_idx = pin_idx;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001958
Stephen Warren384a48d2011-06-01 11:14:21 -06001959 err = hdmi_read_pin_conn(codec, pin_idx);
1960 if (err < 0)
1961 return err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001962
Wu Fengguang079d88c2010-03-08 10:44:23 +08001963 spec->num_pins++;
1964
Stephen Warren384a48d2011-06-01 11:14:21 -06001965 return 0;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001966}
1967
Stephen Warren384a48d2011-06-01 11:14:21 -06001968static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
Wu Fengguang079d88c2010-03-08 10:44:23 +08001969{
1970 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06001971 struct hdmi_spec_per_cvt *per_cvt;
1972 unsigned int chans;
1973 int err;
Wu Fengguang079d88c2010-03-08 10:44:23 +08001974
Stephen Warren384a48d2011-06-01 11:14:21 -06001975 chans = get_wcaps(codec, cvt_nid);
1976 chans = get_wcaps_channels(chans);
1977
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001978 per_cvt = snd_array_new(&spec->cvts);
1979 if (!per_cvt)
1980 return -ENOMEM;
Stephen Warren384a48d2011-06-01 11:14:21 -06001981
1982 per_cvt->cvt_nid = cvt_nid;
1983 per_cvt->channels_min = 2;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001984 if (chans <= 16) {
Stephen Warren384a48d2011-06-01 11:14:21 -06001985 per_cvt->channels_max = chans;
Takashi Iwaid45e6882012-07-31 11:36:00 +02001986 if (chans > spec->channels_max)
1987 spec->channels_max = chans;
1988 }
Stephen Warren384a48d2011-06-01 11:14:21 -06001989
1990 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1991 &per_cvt->rates,
1992 &per_cvt->formats,
1993 &per_cvt->maxbps);
1994 if (err < 0)
1995 return err;
1996
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01001997 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1998 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1999 spec->num_cvts++;
Wu Fengguang079d88c2010-03-08 10:44:23 +08002000
2001 return 0;
2002}
2003
2004static int hdmi_parse_codec(struct hda_codec *codec)
2005{
2006 hda_nid_t nid;
2007 int i, nodes;
2008
Takashi Iwai7639a062015-03-03 10:07:24 +01002009 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002010 if (!nid || nodes < 0) {
Takashi Iwai4e76a882014-02-25 12:21:03 +01002011 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
Wu Fengguang079d88c2010-03-08 10:44:23 +08002012 return -EINVAL;
2013 }
2014
2015 for (i = 0; i < nodes; i++, nid++) {
2016 unsigned int caps;
2017 unsigned int type;
2018
Takashi Iwaiefc2f8de2012-11-21 14:27:37 +01002019 caps = get_wcaps(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002020 type = get_wcaps_type(caps);
2021
2022 if (!(caps & AC_WCAP_DIGITAL))
2023 continue;
2024
2025 switch (type) {
2026 case AC_WID_AUD_OUT:
Stephen Warren384a48d2011-06-01 11:14:21 -06002027 hdmi_add_cvt(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002028 break;
2029 case AC_WID_PIN:
Wu Fengguang3eaead52010-05-14 16:36:15 +08002030 hdmi_add_pin(codec, nid);
Wu Fengguang079d88c2010-03-08 10:44:23 +08002031 break;
2032 }
2033 }
2034
Wu Fengguang079d88c2010-03-08 10:44:23 +08002035 return 0;
2036}
2037
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002038/*
2039 */
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002040static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2041{
2042 struct hda_spdif_out *spdif;
2043 bool non_pcm;
2044
2045 mutex_lock(&codec->spdif_mutex);
2046 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2047 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2048 mutex_unlock(&codec->spdif_mutex);
2049 return non_pcm;
2050}
2051
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002052/*
2053 * HDMI callbacks
2054 */
2055
2056static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2057 struct hda_codec *codec,
2058 unsigned int stream_tag,
2059 unsigned int format,
2060 struct snd_pcm_substream *substream)
2061{
Stephen Warren384a48d2011-06-01 11:14:21 -06002062 hda_nid_t cvt_nid = hinfo->nid;
2063 struct hdmi_spec *spec = codec->spec;
Libin Yang42b29872015-12-16 13:42:42 +08002064 int pin_idx;
2065 struct hdmi_spec_per_pin *per_pin;
2066 hda_nid_t pin_nid;
Libin Yangddd621f2015-09-02 14:11:40 +08002067 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002068 bool non_pcm;
Stephen Warren75fae112014-01-30 11:52:16 -07002069 int pinctl;
Libin Yang42b29872015-12-16 13:42:42 +08002070 int err;
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002071
Libin Yang42b29872015-12-16 13:42:42 +08002072 mutex_lock(&spec->pcm_lock);
2073 pin_idx = hinfo_to_pin_index(codec, hinfo);
2074 if (spec->dyn_pcm_assign && pin_idx < 0) {
2075 /* when dyn_pcm_assign and pcm is not bound to a pin
2076 * skip pin setup and return 0 to make audio playback
2077 * be ongoing
2078 */
2079 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2080 snd_hda_codec_setup_stream(codec, cvt_nid,
2081 stream_tag, 0, format);
2082 mutex_unlock(&spec->pcm_lock);
2083 return 0;
2084 }
2085
2086 if (snd_BUG_ON(pin_idx < 0)) {
2087 mutex_unlock(&spec->pcm_lock);
2088 return -EINVAL;
2089 }
2090 per_pin = get_pin(spec, pin_idx);
2091 pin_nid = per_pin->pin_nid;
Libin Yangca2e7222014-08-19 16:20:12 +08002092 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
Mengdong Lin2df67422014-03-20 13:01:06 +08002093 /* Verify pin:cvt selections to avoid silent audio after S3.
2094 * After S3, the audio driver restores pin:cvt selections
2095 * but this can happen before gfx is ready and such selection
2096 * is overlooked by HW. Thus multiple pins can share a same
2097 * default convertor and mute control will affect each other,
2098 * which can cause a resumed audio playback become silent
2099 * after S3.
2100 */
2101 intel_verify_pin_cvt_connect(codec, per_pin);
2102 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2103 }
2104
Libin Yangddd621f2015-09-02 14:11:40 +08002105 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2106 /* Todo: add DP1.2 MST audio support later */
Takashi Iwaie2dc7d72015-12-01 12:39:38 +01002107 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
Libin Yangddd621f2015-09-02 14:11:40 +08002108
Takashi Iwai1a6003b2012-09-06 17:42:08 +02002109 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002110 mutex_lock(&per_pin->lock);
Takashi Iwaib0540872013-09-02 12:33:02 +02002111 per_pin->channels = substream->runtime->channels;
2112 per_pin->setup = true;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002113
Takashi Iwaib0540872013-09-02 12:33:02 +02002114 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002115 mutex_unlock(&per_pin->lock);
Stephen Warren75fae112014-01-30 11:52:16 -07002116 if (spec->dyn_pin_out) {
2117 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2118 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2119 snd_hda_codec_write(codec, pin_nid, 0,
2120 AC_VERB_SET_PIN_WIDGET_CONTROL,
2121 pinctl | PIN_OUT);
2122 }
2123
Libin Yang42b29872015-12-16 13:42:42 +08002124 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2125 stream_tag, format);
2126 mutex_unlock(&spec->pcm_lock);
2127 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002128}
2129
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002130static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2131 struct hda_codec *codec,
2132 struct snd_pcm_substream *substream)
2133{
2134 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2135 return 0;
2136}
2137
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002138static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2139 struct hda_codec *codec,
2140 struct snd_pcm_substream *substream)
Stephen Warren384a48d2011-06-01 11:14:21 -06002141{
2142 struct hdmi_spec *spec = codec->spec;
Libin Yang2bf3c852015-12-16 13:42:43 +08002143 int cvt_idx, pin_idx, pcm_idx;
Stephen Warren384a48d2011-06-01 11:14:21 -06002144 struct hdmi_spec_per_cvt *per_cvt;
2145 struct hdmi_spec_per_pin *per_pin;
Stephen Warren75fae112014-01-30 11:52:16 -07002146 int pinctl;
Stephen Warren384a48d2011-06-01 11:14:21 -06002147
Stephen Warren384a48d2011-06-01 11:14:21 -06002148 if (hinfo->nid) {
Libin Yang2bf3c852015-12-16 13:42:43 +08002149 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2150 if (snd_BUG_ON(pcm_idx < 0))
2151 return -EINVAL;
Takashi Iwai4e76a882014-02-25 12:21:03 +01002152 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
Stephen Warren384a48d2011-06-01 11:14:21 -06002153 if (snd_BUG_ON(cvt_idx < 0))
2154 return -EINVAL;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002155 per_cvt = get_cvt(spec, cvt_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002156
2157 snd_BUG_ON(!per_cvt->assigned);
2158 per_cvt->assigned = 0;
2159 hinfo->nid = 0;
2160
Libin Yang42b29872015-12-16 13:42:42 +08002161 mutex_lock(&spec->pcm_lock);
Takashi Iwai4e76a882014-02-25 12:21:03 +01002162 pin_idx = hinfo_to_pin_index(codec, hinfo);
Libin Yang42b29872015-12-16 13:42:42 +08002163 if (spec->dyn_pcm_assign && pin_idx < 0) {
2164 mutex_unlock(&spec->pcm_lock);
2165 return 0;
2166 }
2167
2168 if (snd_BUG_ON(pin_idx < 0)) {
2169 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002170 return -EINVAL;
Libin Yang42b29872015-12-16 13:42:42 +08002171 }
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002172 per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002173
Stephen Warren75fae112014-01-30 11:52:16 -07002174 if (spec->dyn_pin_out) {
2175 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2176 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2177 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2178 AC_VERB_SET_PIN_WIDGET_CONTROL,
2179 pinctl & ~PIN_OUT);
2180 }
2181
Libin Yang2bf3c852015-12-16 13:42:43 +08002182 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
Takashi Iwaicbbaa602013-10-17 18:03:24 +02002183
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002184 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002185 per_pin->chmap_set = false;
2186 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
Takashi Iwaib0540872013-09-02 12:33:02 +02002187
2188 per_pin->setup = false;
2189 per_pin->channels = 0;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002190 mutex_unlock(&per_pin->lock);
Libin Yang42b29872015-12-16 13:42:42 +08002191 mutex_unlock(&spec->pcm_lock);
Stephen Warren384a48d2011-06-01 11:14:21 -06002192 }
Takashi Iwaid45e6882012-07-31 11:36:00 +02002193
Stephen Warren384a48d2011-06-01 11:14:21 -06002194 return 0;
2195}
2196
2197static const struct hda_pcm_ops generic_ops = {
2198 .open = hdmi_pcm_open,
Takashi Iwaif2ad24f2012-07-26 18:08:14 +02002199 .close = hdmi_pcm_close,
Stephen Warren384a48d2011-06-01 11:14:21 -06002200 .prepare = generic_hdmi_playback_pcm_prepare,
Takashi Iwai8dfaa572012-08-06 14:49:36 +02002201 .cleanup = generic_hdmi_playback_pcm_cleanup,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002202};
2203
Takashi Iwaid45e6882012-07-31 11:36:00 +02002204/*
2205 * ALSA API channel-map control callbacks
2206 */
2207static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2208 struct snd_ctl_elem_info *uinfo)
2209{
2210 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2211 struct hda_codec *codec = info->private_data;
2212 struct hdmi_spec *spec = codec->spec;
2213 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2214 uinfo->count = spec->channels_max;
2215 uinfo->value.integer.min = 0;
2216 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2217 return 0;
2218}
2219
Anssi Hannula307229d2013-10-24 21:10:34 +03002220static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2221 int channels)
2222{
2223 /* If the speaker allocation matches the channel count, it is OK.*/
2224 if (cap->channels != channels)
2225 return -1;
2226
2227 /* all channels are remappable freely */
2228 return SNDRV_CTL_TLVT_CHMAP_VAR;
2229}
2230
2231static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2232 unsigned int *chmap, int channels)
2233{
2234 int count = 0;
2235 int c;
2236
2237 for (c = 7; c >= 0; c--) {
2238 int spk = cap->speakers[c];
2239 if (!spk)
2240 continue;
2241
2242 chmap[count++] = spk_to_chmap(spk);
2243 }
2244
2245 WARN_ON(count != channels);
2246}
2247
Takashi Iwaid45e6882012-07-31 11:36:00 +02002248static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2249 unsigned int size, unsigned int __user *tlv)
2250{
2251 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2252 struct hda_codec *codec = info->private_data;
2253 struct hdmi_spec *spec = codec->spec;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002254 unsigned int __user *dst;
2255 int chs, count = 0;
2256
2257 if (size < 8)
2258 return -ENOMEM;
2259 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2260 return -EFAULT;
2261 size -= 8;
2262 dst = tlv + 2;
Takashi Iwai498dab32012-09-10 16:08:40 +02002263 for (chs = 2; chs <= spec->channels_max; chs++) {
Anssi Hannula307229d2013-10-24 21:10:34 +03002264 int i;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002265 struct cea_channel_speaker_allocation *cap;
2266 cap = channel_allocations;
2267 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2268 int chs_bytes = chs * 4;
Anssi Hannula307229d2013-10-24 21:10:34 +03002269 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2270 unsigned int tlv_chmap[8];
2271
2272 if (type < 0)
Takashi Iwaid45e6882012-07-31 11:36:00 +02002273 continue;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002274 if (size < 8)
2275 return -ENOMEM;
Anssi Hannula307229d2013-10-24 21:10:34 +03002276 if (put_user(type, dst) ||
Takashi Iwaid45e6882012-07-31 11:36:00 +02002277 put_user(chs_bytes, dst + 1))
2278 return -EFAULT;
2279 dst += 2;
2280 size -= 8;
2281 count += 8;
2282 if (size < chs_bytes)
2283 return -ENOMEM;
2284 size -= chs_bytes;
2285 count += chs_bytes;
Anssi Hannula307229d2013-10-24 21:10:34 +03002286 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2287 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2288 return -EFAULT;
2289 dst += chs;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002290 }
2291 }
2292 if (put_user(count, tlv + 1))
2293 return -EFAULT;
2294 return 0;
2295}
2296
2297static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2298 struct snd_ctl_elem_value *ucontrol)
2299{
2300 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2301 struct hda_codec *codec = info->private_data;
2302 struct hdmi_spec *spec = codec->spec;
2303 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002304 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002305 int i;
2306
2307 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2308 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2309 return 0;
2310}
2311
2312static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
2314{
2315 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2316 struct hda_codec *codec = info->private_data;
2317 struct hdmi_spec *spec = codec->spec;
2318 int pin_idx = kcontrol->private_value;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002319 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002320 unsigned int ctl_idx;
2321 struct snd_pcm_substream *substream;
2322 unsigned char chmap[8];
Anssi Hannula307229d2013-10-24 21:10:34 +03002323 int i, err, ca, prepared = 0;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002324
2325 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2326 substream = snd_pcm_chmap_substream(info, ctl_idx);
2327 if (!substream || !substream->runtime)
Takashi Iwai6f54c362013-01-15 14:44:41 +01002328 return 0; /* just for avoiding error from alsactl restore */
Takashi Iwaid45e6882012-07-31 11:36:00 +02002329 switch (substream->runtime->status->state) {
2330 case SNDRV_PCM_STATE_OPEN:
2331 case SNDRV_PCM_STATE_SETUP:
2332 break;
2333 case SNDRV_PCM_STATE_PREPARED:
2334 prepared = 1;
2335 break;
2336 default:
2337 return -EBUSY;
2338 }
2339 memset(chmap, 0, sizeof(chmap));
2340 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2341 chmap[i] = ucontrol->value.integer.value[i];
2342 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2343 return 0;
2344 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2345 if (ca < 0)
2346 return -EINVAL;
Anssi Hannula307229d2013-10-24 21:10:34 +03002347 if (spec->ops.chmap_validate) {
2348 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2349 if (err)
2350 return err;
2351 }
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002352 mutex_lock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002353 per_pin->chmap_set = true;
2354 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2355 if (prepared)
Takashi Iwaib0540872013-09-02 12:33:02 +02002356 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002357 mutex_unlock(&per_pin->lock);
Takashi Iwaid45e6882012-07-31 11:36:00 +02002358
2359 return 0;
2360}
2361
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002362static int generic_hdmi_build_pcms(struct hda_codec *codec)
2363{
2364 struct hdmi_spec *spec = codec->spec;
Libin Yang6590faa2015-12-16 13:42:41 +08002365 struct hdmi_spec_per_pin *per_pin;
Stephen Warren384a48d2011-06-01 11:14:21 -06002366 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002367
Stephen Warren384a48d2011-06-01 11:14:21 -06002368 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2369 struct hda_pcm *info;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002370 struct hda_pcm_stream *pstr;
2371
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002372 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002373 if (!info)
2374 return -ENOMEM;
Libin Yang6590faa2015-12-16 13:42:41 +08002375 if (!spec->dyn_pcm_assign) {
2376 per_pin = get_pin(spec, pin_idx);
2377 per_pin->pcm = info;
2378 }
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002379 spec->pcm_rec[pin_idx] = info;
Libin Yang2bf3c852015-12-16 13:42:43 +08002380 spec->pcm_used++;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002381 info->pcm_type = HDA_PCM_TYPE_HDMI;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002382 info->own_chmap = true;
Stephen Warren384a48d2011-06-01 11:14:21 -06002383
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002384 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
Stephen Warren384a48d2011-06-01 11:14:21 -06002385 pstr->substreams = 1;
2386 pstr->ops = generic_ops;
2387 /* other pstr fields are set in open */
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002388 }
2389
2390 return 0;
2391}
2392
Takashi Iwai788d4412015-11-12 15:36:13 +01002393static void free_acomp_jack_priv(struct snd_jack *jack)
2394{
2395 struct hdmi_spec_per_pin *per_pin = jack->private_data;
2396
2397 per_pin->acomp_jack = NULL;
2398}
2399
2400static int add_acomp_jack_kctl(struct hda_codec *codec,
2401 struct hdmi_spec_per_pin *per_pin,
2402 const char *name)
2403{
2404 struct snd_jack *jack;
2405 int err;
2406
2407 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2408 true, false);
2409 if (err < 0)
2410 return err;
2411 per_pin->acomp_jack = jack;
2412 jack->private_data = per_pin;
2413 jack->private_free = free_acomp_jack_priv;
2414 return 0;
2415}
2416
David Henningsson0b6c49b2011-08-23 16:56:03 +02002417static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
2418{
Takashi Iwai31ef2252011-12-01 17:41:36 +01002419 char hdmi_str[32] = "HDMI/DP";
David Henningsson0b6c49b2011-08-23 16:56:03 +02002420 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002421 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2422 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
Takashi Iwai909cadc2015-11-12 11:52:13 +01002423 bool phantom_jack;
David Henningsson0b6c49b2011-08-23 16:56:03 +02002424
Takashi Iwai31ef2252011-12-01 17:41:36 +01002425 if (pcmdev > 0)
2426 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
Takashi Iwai788d4412015-11-12 15:36:13 +01002427 if (codec_has_acomp(codec))
2428 return add_acomp_jack_kctl(codec, per_pin, hdmi_str);
Takashi Iwai909cadc2015-11-12 11:52:13 +01002429 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2430 if (phantom_jack)
David Henningsson30efd8d2013-02-22 10:16:28 +01002431 strncat(hdmi_str, " Phantom",
2432 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002433
Takashi Iwai909cadc2015-11-12 11:52:13 +01002434 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2435 phantom_jack);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002436}
2437
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002438static int generic_hdmi_build_controls(struct hda_codec *codec)
2439{
2440 struct hdmi_spec *spec = codec->spec;
2441 int err;
Stephen Warren384a48d2011-06-01 11:14:21 -06002442 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002443
Stephen Warren384a48d2011-06-01 11:14:21 -06002444 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002445 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
David Henningsson0b6c49b2011-08-23 16:56:03 +02002446
2447 err = generic_hdmi_build_jack(codec, pin_idx);
2448 if (err < 0)
2449 return err;
2450
Takashi Iwaidcda5802012-10-12 17:24:51 +02002451 err = snd_hda_create_dig_out_ctls(codec,
2452 per_pin->pin_nid,
2453 per_pin->mux_nids[0],
2454 HDA_PCM_TYPE_HDMI);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002455 if (err < 0)
2456 return err;
Libin Yang2bf3c852015-12-16 13:42:43 +08002457 /* pin number is the same with pcm number so far */
Stephen Warren384a48d2011-06-01 11:14:21 -06002458 snd_hda_spdif_ctls_unassign(codec, pin_idx);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002459
2460 /* add control for ELD Bytes */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002461 err = hdmi_create_eld_ctl(codec, pin_idx,
2462 get_pcm_rec(spec, pin_idx)->device);
Pierre-Louis Bossart14bc52b2011-09-30 16:35:41 -05002463
2464 if (err < 0)
2465 return err;
Takashi Iwai31ef2252011-12-01 17:41:36 +01002466
Takashi Iwai82b1d732011-12-20 15:53:07 +01002467 hdmi_present_sense(per_pin, 0);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002468 }
2469
Takashi Iwaid45e6882012-07-31 11:36:00 +02002470 /* add channel maps */
2471 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002472 struct hda_pcm *pcm;
Takashi Iwaid45e6882012-07-31 11:36:00 +02002473 struct snd_pcm_chmap *chmap;
2474 struct snd_kcontrol *kctl;
2475 int i;
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002476
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002477 pcm = spec->pcm_rec[pin_idx];
2478 if (!pcm || !pcm->pcm)
Takashi Iwai2ca320e2013-08-22 09:55:36 +02002479 break;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002480 err = snd_pcm_add_chmap_ctls(pcm->pcm,
Takashi Iwaid45e6882012-07-31 11:36:00 +02002481 SNDRV_PCM_STREAM_PLAYBACK,
2482 NULL, 0, pin_idx, &chmap);
2483 if (err < 0)
2484 return err;
2485 /* override handlers */
2486 chmap->private_data = codec;
2487 kctl = chmap->kctl;
2488 for (i = 0; i < kctl->count; i++)
2489 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2490 kctl->info = hdmi_chmap_ctl_info;
2491 kctl->get = hdmi_chmap_ctl_get;
2492 kctl->put = hdmi_chmap_ctl_put;
2493 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2494 }
2495
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002496 return 0;
2497}
2498
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002499static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2500{
2501 struct hdmi_spec *spec = codec->spec;
2502 int pin_idx;
2503
2504 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002505 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002506
2507 per_pin->codec = codec;
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002508 mutex_init(&per_pin->lock);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002509 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002510 eld_proc_new(per_pin, pin_idx);
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002511 }
2512 return 0;
2513}
2514
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002515static int generic_hdmi_init(struct hda_codec *codec)
2516{
2517 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002518 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002519
Stephen Warren384a48d2011-06-01 11:14:21 -06002520 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002521 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002522 hda_nid_t pin_nid = per_pin->pin_nid;
Stephen Warren384a48d2011-06-01 11:14:21 -06002523
2524 hdmi_init_pin(codec, pin_nid);
Takashi Iwai788d4412015-11-12 15:36:13 +01002525 if (!codec_has_acomp(codec))
2526 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2527 codec->jackpoll_interval > 0 ?
2528 jack_callback : NULL);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002529 }
2530 return 0;
2531}
2532
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002533static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2534{
2535 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2536 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002537}
2538
2539static void hdmi_array_free(struct hdmi_spec *spec)
2540{
2541 snd_array_free(&spec->pins);
2542 snd_array_free(&spec->cvts);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002543}
2544
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002545static void generic_hdmi_free(struct hda_codec *codec)
2546{
2547 struct hdmi_spec *spec = codec->spec;
Stephen Warren384a48d2011-06-01 11:14:21 -06002548 int pin_idx;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002549
Takashi Iwai66032492015-12-01 16:49:35 +01002550 if (codec_has_acomp(codec))
David Henningsson25adc132015-08-19 10:48:58 +02002551 snd_hdac_i915_register_notifier(NULL);
2552
Stephen Warren384a48d2011-06-01 11:14:21 -06002553 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002554 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
Stephen Warren384a48d2011-06-01 11:14:21 -06002555
Takashi Iwai2f35c632015-02-27 22:43:26 +01002556 cancel_delayed_work_sync(&per_pin->work);
Takashi Iwaia4e9a382013-10-17 18:21:12 +02002557 eld_proc_free(per_pin);
Takashi Iwai788d4412015-11-12 15:36:13 +01002558 if (per_pin->acomp_jack)
2559 snd_device_free(codec->card, per_pin->acomp_jack);
Stephen Warren384a48d2011-06-01 11:14:21 -06002560 }
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002561
Takashi Iwai55913112015-12-10 13:03:29 +01002562 if (spec->i915_bound)
2563 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002564 hdmi_array_free(spec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002565 kfree(spec);
2566}
2567
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002568#ifdef CONFIG_PM
2569static int generic_hdmi_resume(struct hda_codec *codec)
2570{
2571 struct hdmi_spec *spec = codec->spec;
2572 int pin_idx;
2573
Pierre Ossmana2833682014-06-18 21:48:09 +02002574 codec->patch_ops.init(codec);
Takashi Iwaieeecd9d2015-02-25 15:18:50 +01002575 regcache_sync(codec->core.regmap);
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002576
2577 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2578 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2579 hdmi_present_sense(per_pin, 1);
2580 }
2581 return 0;
2582}
2583#endif
2584
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002585static const struct hda_codec_ops generic_hdmi_patch_ops = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002586 .init = generic_hdmi_init,
2587 .free = generic_hdmi_free,
2588 .build_pcms = generic_hdmi_build_pcms,
2589 .build_controls = generic_hdmi_build_controls,
2590 .unsol_event = hdmi_unsol_event,
Wang Xingchao28cb72e2013-06-24 07:45:23 -04002591#ifdef CONFIG_PM
2592 .resume = generic_hdmi_resume,
2593#endif
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002594};
2595
Anssi Hannula307229d2013-10-24 21:10:34 +03002596static const struct hdmi_ops generic_standard_hdmi_ops = {
2597 .pin_get_eld = snd_hdmi_get_eld,
2598 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2599 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2600 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2601 .pin_hbr_setup = hdmi_pin_hbr_setup,
2602 .setup_stream = hdmi_setup_stream,
2603 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2604 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2605};
2606
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002607
2608static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2609 hda_nid_t nid)
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002610{
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002611 struct hdmi_spec *spec = codec->spec;
2612 hda_nid_t conns[4];
2613 int nconns;
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002614
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002615 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2616 if (nconns == spec->num_cvts &&
2617 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002618 return;
2619
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002620 /* override pins connection list */
Takashi Iwai4e76a882014-02-25 12:21:03 +01002621 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002622 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002623}
2624
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002625#define INTEL_VENDOR_NID 0x08
2626#define INTEL_GET_VENDOR_VERB 0xf81
2627#define INTEL_SET_VENDOR_VERB 0x781
2628#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2629#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2630
2631static void intel_haswell_enable_all_pins(struct hda_codec *codec,
Takashi Iwai17df3f52013-05-08 08:09:34 +02002632 bool update_tree)
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002633{
2634 unsigned int vendor_param;
2635
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002636 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2637 INTEL_GET_VENDOR_VERB, 0);
2638 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2639 return;
2640
2641 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2642 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2643 INTEL_SET_VENDOR_VERB, vendor_param);
2644 if (vendor_param == -1)
2645 return;
2646
Takashi Iwai17df3f52013-05-08 08:09:34 +02002647 if (update_tree)
2648 snd_hda_codec_update_widgets(codec);
Mengdong Lin1611a9c2013-02-08 17:09:52 -05002649}
2650
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002651static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2652{
2653 unsigned int vendor_param;
2654
2655 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2656 INTEL_GET_VENDOR_VERB, 0);
2657 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2658 return;
2659
2660 /* enable DP1.2 mode */
2661 vendor_param |= INTEL_EN_DP12;
Takashi Iwaia551d912015-02-26 12:34:49 +01002662 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002663 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2664 INTEL_SET_VENDOR_VERB, vendor_param);
2665}
2666
Takashi Iwai17df3f52013-05-08 08:09:34 +02002667/* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2668 * Otherwise you may get severe h/w communication errors.
2669 */
2670static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2671 unsigned int power_state)
2672{
2673 if (power_state == AC_PWRST_D0) {
2674 intel_haswell_enable_all_pins(codec, false);
2675 intel_haswell_fixup_enable_dp12(codec);
2676 }
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002677
Takashi Iwai17df3f52013-05-08 08:09:34 +02002678 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2679 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2680}
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002681
David Henningssonf0675d42015-09-03 11:51:34 +02002682static void intel_pin_eld_notify(void *audio_ptr, int port)
David Henningsson25adc132015-08-19 10:48:58 +02002683{
2684 struct hda_codec *codec = audio_ptr;
2685 int pin_nid = port + 0x04;
2686
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002687 /* skip notification during system suspend (but not in runtime PM);
2688 * the state will be updated at resume
2689 */
2690 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2691 return;
Takashi Iwaieb399d32015-11-27 14:53:35 +01002692 /* ditto during suspend/resume process itself */
2693 if (atomic_read(&(codec)->core.in_pm))
2694 return;
Takashi Iwai8ae743e2015-11-27 14:23:00 +01002695
David Henningsson25adc132015-08-19 10:48:58 +02002696 check_presence_and_report(codec, pin_nid);
2697}
2698
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002699static int patch_generic_hdmi(struct hda_codec *codec)
2700{
2701 struct hdmi_spec *spec;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002702
2703 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2704 if (spec == NULL)
2705 return -ENOMEM;
2706
Anssi Hannula307229d2013-10-24 21:10:34 +03002707 spec->ops = generic_standard_hdmi_ops;
Libin Yang42b29872015-12-16 13:42:42 +08002708 mutex_init(&spec->pcm_lock);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002709 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002710 hdmi_array_init(spec, 4);
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002711
Takashi Iwai55913112015-12-10 13:03:29 +01002712 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2713 if (!codec_has_acomp(codec) &&
2714 (codec->core.vendor_id >> 16) == 0x8086)
2715 if (!snd_hdac_i915_init(&codec->bus->core))
2716 spec->i915_bound = true;
2717
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002718 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002719 intel_haswell_enable_all_pins(codec, true);
Takashi Iwaic88d4e82013-02-08 17:10:04 -05002720 intel_haswell_fixup_enable_dp12(codec);
Takashi Iwai17df3f52013-05-08 08:09:34 +02002721 }
Mengdong Lin6ffe1682012-12-18 16:59:15 -05002722
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002723 /* For Valleyview/Cherryview, only the display codec is in the display
2724 * power well and can use link_power ops to request/release the power.
2725 * For Haswell/Broadwell, the controller is also in the power well and
2726 * can cover the codec power request, and so need not set this flag.
2727 * For previous platforms, there is no such power well feature.
2728 */
Lu, Hanff9d8852015-11-19 23:25:13 +08002729 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2730 is_broxton(codec))
Mengdong Lin2bd1f73f2015-04-29 17:43:43 +08002731 codec->core.link_power_control = 1;
2732
Takashi Iwai66032492015-12-01 16:49:35 +01002733 if (codec_has_acomp(codec)) {
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002734 codec->depop_delay = 0;
David Henningsson25adc132015-08-19 10:48:58 +02002735 spec->i915_audio_ops.audio_ptr = codec;
2736 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2737 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2738 }
Mengdong Lin5b8620b2013-12-05 18:35:48 -05002739
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002740 if (hdmi_parse_codec(codec) < 0) {
Takashi Iwai55913112015-12-10 13:03:29 +01002741 if (spec->i915_bound)
2742 snd_hdac_i915_exit(&codec->bus->core);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002743 codec->spec = NULL;
2744 kfree(spec);
2745 return -EINVAL;
2746 }
2747 codec->patch_ops = generic_hdmi_patch_ops;
Mengdong Lin75dcbe42014-01-08 15:55:32 -05002748 if (is_haswell_plus(codec)) {
Takashi Iwai17df3f52013-05-08 08:09:34 +02002749 codec->patch_ops.set_power_state = haswell_set_power_state;
Mengdong Lin5dc989b2013-08-26 21:35:41 -04002750 codec->dp_mst = true;
2751 }
Takashi Iwai17df3f52013-05-08 08:09:34 +02002752
Lu, Han2377c3c2015-06-09 16:50:38 +08002753 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2754 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2755 codec->auto_runtime_pm = 1;
2756
Takashi Iwai8b8d6542012-06-20 16:32:22 +02002757 generic_hdmi_init_per_pins(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002758
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002759 init_channel_allocations();
2760
2761 return 0;
2762}
2763
2764/*
Stephen Warren3aaf8982011-06-01 11:14:19 -06002765 * Shared non-generic implementations
2766 */
2767
2768static int simple_playback_build_pcms(struct hda_codec *codec)
2769{
2770 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002771 struct hda_pcm *info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002772 unsigned int chans;
2773 struct hda_pcm_stream *pstr;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002774 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002775
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002776 per_cvt = get_cvt(spec, 0);
2777 chans = get_wcaps(codec, per_cvt->cvt_nid);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002778 chans = get_wcaps_channels(chans);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002779
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002780 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002781 if (!info)
2782 return -ENOMEM;
Takashi Iwaibbbc7e82015-02-27 17:43:19 +01002783 spec->pcm_rec[0] = info;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002784 info->pcm_type = HDA_PCM_TYPE_HDMI;
2785 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2786 *pstr = spec->pcm_playback;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002787 pstr->nid = per_cvt->cvt_nid;
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002788 if (pstr->channels_max <= 2 && chans && chans <= 16)
2789 pstr->channels_max = chans;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002790
2791 return 0;
2792}
2793
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002794/* unsolicited event for jack sensing */
2795static void simple_hdmi_unsol_event(struct hda_codec *codec,
2796 unsigned int res)
2797{
Takashi Iwai9dd8cf12012-06-21 10:43:15 +02002798 snd_hda_jack_set_dirty_all(codec);
Takashi Iwai4b6ace92012-06-15 11:53:32 +02002799 snd_hda_jack_report_sync(codec);
2800}
2801
2802/* generic_hdmi_build_jack can be used for simple_hdmi, too,
2803 * as long as spec->pins[] is set correctly
2804 */
2805#define simple_hdmi_build_jack generic_hdmi_build_jack
2806
Stephen Warren3aaf8982011-06-01 11:14:19 -06002807static int simple_playback_build_controls(struct hda_codec *codec)
2808{
2809 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002810 struct hdmi_spec_per_cvt *per_cvt;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002811 int err;
Stephen Warren3aaf8982011-06-01 11:14:19 -06002812
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002813 per_cvt = get_cvt(spec, 0);
Anssi Hannulac9a63382013-12-10 22:46:34 +02002814 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2815 per_cvt->cvt_nid,
2816 HDA_PCM_TYPE_HDMI);
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002817 if (err < 0)
2818 return err;
2819 return simple_hdmi_build_jack(codec, 0);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002820}
2821
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002822static int simple_playback_init(struct hda_codec *codec)
2823{
2824 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002825 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2826 hda_nid_t pin = per_pin->pin_nid;
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002827
Takashi Iwai8ceb3322012-06-21 08:23:27 +02002828 snd_hda_codec_write(codec, pin, 0,
2829 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2830 /* some codecs require to unmute the pin */
2831 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2832 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2833 AMP_OUT_UNMUTE);
Takashi Iwai62f949b2014-09-11 14:06:53 +02002834 snd_hda_jack_detect_enable(codec, pin);
Takashi Iwai4f0110c2012-06-15 12:45:43 +02002835 return 0;
2836}
2837
Stephen Warren3aaf8982011-06-01 11:14:19 -06002838static void simple_playback_free(struct hda_codec *codec)
2839{
2840 struct hdmi_spec *spec = codec->spec;
2841
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01002842 hdmi_array_free(spec);
Stephen Warren3aaf8982011-06-01 11:14:19 -06002843 kfree(spec);
2844}
2845
2846/*
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002847 * Nvidia specific implementations
2848 */
2849
2850#define Nv_VERB_SET_Channel_Allocation 0xF79
2851#define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2852#define Nv_VERB_SET_Audio_Protection_On 0xF98
2853#define Nv_VERB_SET_Audio_Protection_Off 0xF99
2854
2855#define nvhdmi_master_con_nid_7x 0x04
2856#define nvhdmi_master_pin_nid_7x 0x05
2857
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02002858static const hda_nid_t nvhdmi_con_nids_7x[4] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002859 /*front, rear, clfe, rear_surr */
2860 0x6, 0x8, 0xa, 0xc,
2861};
2862
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002863static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2864 /* set audio protect on */
2865 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2866 /* enable digital output on pin widget */
2867 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2868 {} /* terminator */
2869};
2870
2871static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002872 /* set audio protect on */
2873 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2874 /* enable digital output on pin widget */
2875 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2876 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2877 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2878 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2879 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2880 {} /* terminator */
2881};
2882
2883#ifdef LIMITED_RATE_FMT_SUPPORT
2884/* support only the safe format and rate */
2885#define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2886#define SUPPORTED_MAXBPS 16
2887#define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2888#else
2889/* support all rates and formats */
2890#define SUPPORTED_RATES \
2891 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2892 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2893 SNDRV_PCM_RATE_192000)
2894#define SUPPORTED_MAXBPS 24
2895#define SUPPORTED_FORMATS \
2896 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2897#endif
2898
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002899static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002900{
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02002901 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2902 return 0;
2903}
2904
2905static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2906{
2907 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002908 return 0;
2909}
2910
Nitin Daga393004b2011-01-10 21:49:31 +05302911static unsigned int channels_2_6_8[] = {
2912 2, 6, 8
2913};
2914
2915static unsigned int channels_2_8[] = {
2916 2, 8
2917};
2918
2919static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2920 .count = ARRAY_SIZE(channels_2_6_8),
2921 .list = channels_2_6_8,
2922 .mask = 0,
2923};
2924
2925static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2926 .count = ARRAY_SIZE(channels_2_8),
2927 .list = channels_2_8,
2928 .mask = 0,
2929};
2930
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002931static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2932 struct hda_codec *codec,
2933 struct snd_pcm_substream *substream)
2934{
2935 struct hdmi_spec *spec = codec->spec;
Nitin Daga393004b2011-01-10 21:49:31 +05302936 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2937
Takashi Iwaib9a94a92015-10-01 16:20:04 +02002938 switch (codec->preset->vendor_id) {
Nitin Daga393004b2011-01-10 21:49:31 +05302939 case 0x10de0002:
2940 case 0x10de0003:
2941 case 0x10de0005:
2942 case 0x10de0006:
2943 hw_constraints_channels = &hw_constraints_2_8_channels;
2944 break;
2945 case 0x10de0007:
2946 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2947 break;
2948 default:
2949 break;
2950 }
2951
2952 if (hw_constraints_channels != NULL) {
2953 snd_pcm_hw_constraint_list(substream->runtime, 0,
2954 SNDRV_PCM_HW_PARAM_CHANNELS,
2955 hw_constraints_channels);
Takashi Iwaiad09fc92011-01-14 09:42:27 +01002956 } else {
2957 snd_pcm_hw_constraint_step(substream->runtime, 0,
2958 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Nitin Daga393004b2011-01-10 21:49:31 +05302959 }
2960
Takashi Iwai84eb01b2010-09-07 12:27:25 +02002961 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2962}
2963
2964static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2965 struct hda_codec *codec,
2966 struct snd_pcm_substream *substream)
2967{
2968 struct hdmi_spec *spec = codec->spec;
2969 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2970}
2971
2972static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2973 struct hda_codec *codec,
2974 unsigned int stream_tag,
2975 unsigned int format,
2976 struct snd_pcm_substream *substream)
2977{
2978 struct hdmi_spec *spec = codec->spec;
2979 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2980 stream_tag, format, substream);
2981}
2982
Takashi Iwaid0b12522012-06-15 14:34:42 +02002983static const struct hda_pcm_stream simple_pcm_playback = {
2984 .substreams = 1,
2985 .channels_min = 2,
2986 .channels_max = 2,
2987 .ops = {
2988 .open = simple_playback_pcm_open,
2989 .close = simple_playback_pcm_close,
2990 .prepare = simple_playback_pcm_prepare
2991 },
2992};
2993
2994static const struct hda_codec_ops simple_hdmi_patch_ops = {
2995 .build_controls = simple_playback_build_controls,
2996 .build_pcms = simple_playback_build_pcms,
2997 .init = simple_playback_init,
2998 .free = simple_playback_free,
Takashi Iwai250e41a2012-06-15 14:40:21 +02002999 .unsol_event = simple_hdmi_unsol_event,
Takashi Iwaid0b12522012-06-15 14:34:42 +02003000};
3001
3002static int patch_simple_hdmi(struct hda_codec *codec,
3003 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3004{
3005 struct hdmi_spec *spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003006 struct hdmi_spec_per_cvt *per_cvt;
3007 struct hdmi_spec_per_pin *per_pin;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003008
3009 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3010 if (!spec)
3011 return -ENOMEM;
3012
3013 codec->spec = spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003014 hdmi_array_init(spec, 1);
Takashi Iwaid0b12522012-06-15 14:34:42 +02003015
3016 spec->multiout.num_dacs = 0; /* no analog */
3017 spec->multiout.max_channels = 2;
3018 spec->multiout.dig_out_nid = cvt_nid;
3019 spec->num_cvts = 1;
3020 spec->num_pins = 1;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003021 per_pin = snd_array_new(&spec->pins);
3022 per_cvt = snd_array_new(&spec->cvts);
3023 if (!per_pin || !per_cvt) {
3024 simple_playback_free(codec);
3025 return -ENOMEM;
3026 }
3027 per_cvt->cvt_nid = cvt_nid;
3028 per_pin->pin_nid = pin_nid;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003029 spec->pcm_playback = simple_pcm_playback;
3030
3031 codec->patch_ops = simple_hdmi_patch_ops;
3032
3033 return 0;
3034}
3035
Aaron Plattner1f348522011-04-06 17:19:04 -07003036static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3037 int channels)
3038{
3039 unsigned int chanmask;
3040 int chan = channels ? (channels - 1) : 1;
3041
3042 switch (channels) {
3043 default:
3044 case 0:
3045 case 2:
3046 chanmask = 0x00;
3047 break;
3048 case 4:
3049 chanmask = 0x08;
3050 break;
3051 case 6:
3052 chanmask = 0x0b;
3053 break;
3054 case 8:
3055 chanmask = 0x13;
3056 break;
3057 }
3058
3059 /* Set the audio infoframe channel allocation and checksum fields. The
3060 * channel count is computed implicitly by the hardware. */
3061 snd_hda_codec_write(codec, 0x1, 0,
3062 Nv_VERB_SET_Channel_Allocation, chanmask);
3063
3064 snd_hda_codec_write(codec, 0x1, 0,
3065 Nv_VERB_SET_Info_Frame_Checksum,
3066 (0x71 - chan - chanmask));
3067}
3068
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003069static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3070 struct hda_codec *codec,
3071 struct snd_pcm_substream *substream)
3072{
3073 struct hdmi_spec *spec = codec->spec;
3074 int i;
3075
3076 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3077 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3078 for (i = 0; i < 4; i++) {
3079 /* set the stream id */
3080 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3081 AC_VERB_SET_CHANNEL_STREAMID, 0);
3082 /* set the stream format */
3083 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3084 AC_VERB_SET_STREAM_FORMAT, 0);
3085 }
3086
Aaron Plattner1f348522011-04-06 17:19:04 -07003087 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3088 * streams are disabled. */
3089 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3090
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003091 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3092}
3093
3094static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3095 struct hda_codec *codec,
3096 unsigned int stream_tag,
3097 unsigned int format,
3098 struct snd_pcm_substream *substream)
3099{
3100 int chs;
Takashi Iwai112daa72011-11-02 21:40:06 +01003101 unsigned int dataDCC2, channel_id;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003102 int i;
Stephen Warren7c935972011-06-01 11:14:17 -06003103 struct hdmi_spec *spec = codec->spec;
Takashi Iwaie3245cd2012-05-10 10:21:29 +02003104 struct hda_spdif_out *spdif;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003105 struct hdmi_spec_per_cvt *per_cvt;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003106
3107 mutex_lock(&codec->spdif_mutex);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003108 per_cvt = get_cvt(spec, 0);
3109 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003110
3111 chs = substream->runtime->channels;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003112
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003113 dataDCC2 = 0x2;
3114
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003115 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
Stephen Warren7c935972011-06-01 11:14:17 -06003116 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003117 snd_hda_codec_write(codec,
3118 nvhdmi_master_con_nid_7x,
3119 0,
3120 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003121 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003122
3123 /* set the stream id */
3124 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3125 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3126
3127 /* set the stream format */
3128 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3129 AC_VERB_SET_STREAM_FORMAT, format);
3130
3131 /* turn on again (if needed) */
3132 /* enable and set the channel status audio/data flag */
Stephen Warren7c935972011-06-01 11:14:17 -06003133 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003134 snd_hda_codec_write(codec,
3135 nvhdmi_master_con_nid_7x,
3136 0,
3137 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003138 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003139 snd_hda_codec_write(codec,
3140 nvhdmi_master_con_nid_7x,
3141 0,
3142 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3143 }
3144
3145 for (i = 0; i < 4; i++) {
3146 if (chs == 2)
3147 channel_id = 0;
3148 else
3149 channel_id = i * 2;
3150
3151 /* turn off SPDIF once;
3152 *otherwise the IEC958 bits won't be updated
3153 */
3154 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003155 (spdif->ctls & AC_DIG1_ENABLE))
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003156 snd_hda_codec_write(codec,
3157 nvhdmi_con_nids_7x[i],
3158 0,
3159 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003160 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003161 /* set the stream id */
3162 snd_hda_codec_write(codec,
3163 nvhdmi_con_nids_7x[i],
3164 0,
3165 AC_VERB_SET_CHANNEL_STREAMID,
3166 (stream_tag << 4) | channel_id);
3167 /* set the stream format */
3168 snd_hda_codec_write(codec,
3169 nvhdmi_con_nids_7x[i],
3170 0,
3171 AC_VERB_SET_STREAM_FORMAT,
3172 format);
3173 /* turn on again (if needed) */
3174 /* enable and set the channel status audio/data flag */
3175 if (codec->spdif_status_reset &&
Stephen Warren7c935972011-06-01 11:14:17 -06003176 (spdif->ctls & AC_DIG1_ENABLE)) {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003177 snd_hda_codec_write(codec,
3178 nvhdmi_con_nids_7x[i],
3179 0,
3180 AC_VERB_SET_DIGI_CONVERT_1,
Stephen Warren7c935972011-06-01 11:14:17 -06003181 spdif->ctls & 0xff);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003182 snd_hda_codec_write(codec,
3183 nvhdmi_con_nids_7x[i],
3184 0,
3185 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3186 }
3187 }
3188
Aaron Plattner1f348522011-04-06 17:19:04 -07003189 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003190
3191 mutex_unlock(&codec->spdif_mutex);
3192 return 0;
3193}
3194
Takashi Iwaifb79e1e2011-05-02 12:17:41 +02003195static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003196 .substreams = 1,
3197 .channels_min = 2,
3198 .channels_max = 8,
3199 .nid = nvhdmi_master_con_nid_7x,
3200 .rates = SUPPORTED_RATES,
3201 .maxbps = SUPPORTED_MAXBPS,
3202 .formats = SUPPORTED_FORMATS,
3203 .ops = {
3204 .open = simple_playback_pcm_open,
3205 .close = nvhdmi_8ch_7x_pcm_close,
3206 .prepare = nvhdmi_8ch_7x_pcm_prepare
3207 },
3208};
3209
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003210static int patch_nvhdmi_2ch(struct hda_codec *codec)
3211{
3212 struct hdmi_spec *spec;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003213 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3214 nvhdmi_master_pin_nid_7x);
3215 if (err < 0)
3216 return err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003217
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003218 codec->patch_ops.init = nvhdmi_7x_init_2ch;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003219 /* override the PCM rates, etc, as the codec doesn't give full list */
3220 spec = codec->spec;
3221 spec->pcm_playback.rates = SUPPORTED_RATES;
3222 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3223 spec->pcm_playback.formats = SUPPORTED_FORMATS;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003224 return 0;
3225}
3226
Takashi Iwai53775b02012-08-01 12:17:41 +02003227static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3228{
3229 struct hdmi_spec *spec = codec->spec;
3230 int err = simple_playback_build_pcms(codec);
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003231 if (!err) {
3232 struct hda_pcm *info = get_pcm_rec(spec, 0);
3233 info->own_chmap = true;
3234 }
Takashi Iwai53775b02012-08-01 12:17:41 +02003235 return err;
3236}
3237
3238static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3239{
3240 struct hdmi_spec *spec = codec->spec;
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003241 struct hda_pcm *info;
Takashi Iwai53775b02012-08-01 12:17:41 +02003242 struct snd_pcm_chmap *chmap;
3243 int err;
3244
3245 err = simple_playback_build_controls(codec);
3246 if (err < 0)
3247 return err;
3248
3249 /* add channel maps */
Takashi Iwaibce0d2a2013-03-13 14:40:31 +01003250 info = get_pcm_rec(spec, 0);
3251 err = snd_pcm_add_chmap_ctls(info->pcm,
Takashi Iwai53775b02012-08-01 12:17:41 +02003252 SNDRV_PCM_STREAM_PLAYBACK,
3253 snd_pcm_alt_chmaps, 8, 0, &chmap);
3254 if (err < 0)
3255 return err;
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003256 switch (codec->preset->vendor_id) {
Takashi Iwai53775b02012-08-01 12:17:41 +02003257 case 0x10de0002:
3258 case 0x10de0003:
3259 case 0x10de0005:
3260 case 0x10de0006:
3261 chmap->channel_mask = (1U << 2) | (1U << 8);
3262 break;
3263 case 0x10de0007:
3264 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3265 }
3266 return 0;
3267}
3268
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003269static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3270{
3271 struct hdmi_spec *spec;
3272 int err = patch_nvhdmi_2ch(codec);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003273 if (err < 0)
3274 return err;
3275 spec = codec->spec;
3276 spec->multiout.max_channels = 8;
Takashi Iwaid0b12522012-06-15 14:34:42 +02003277 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
Takashi Iwaiceaa86b2012-06-15 14:38:31 +02003278 codec->patch_ops.init = nvhdmi_7x_init_8ch;
Takashi Iwai53775b02012-08-01 12:17:41 +02003279 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3280 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
Aaron Plattner1f348522011-04-06 17:19:04 -07003281
3282 /* Initialize the audio infoframe channel mask and checksum to something
3283 * valid */
3284 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3285
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003286 return 0;
3287}
3288
3289/*
Anssi Hannula611885b2013-11-03 17:15:00 +02003290 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3291 * - 0x10de0015
3292 * - 0x10de0040
3293 */
3294static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3295 int channels)
3296{
3297 if (cap->ca_index == 0x00 && channels == 2)
3298 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3299
3300 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3301}
3302
3303static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3304{
3305 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3306 return -EINVAL;
3307
3308 return 0;
3309}
3310
3311static int patch_nvhdmi(struct hda_codec *codec)
3312{
3313 struct hdmi_spec *spec;
3314 int err;
3315
3316 err = patch_generic_hdmi(codec);
3317 if (err)
3318 return err;
3319
3320 spec = codec->spec;
Stephen Warren75fae112014-01-30 11:52:16 -07003321 spec->dyn_pin_out = true;
Anssi Hannula611885b2013-11-03 17:15:00 +02003322
3323 spec->ops.chmap_cea_alloc_validate_get_type =
3324 nvhdmi_chmap_cea_alloc_validate_get_type;
3325 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3326
3327 return 0;
3328}
3329
3330/*
Thierry Reding26e9a962015-05-05 14:56:20 +02003331 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3332 * accessed using vendor-defined verbs. These registers can be used for
3333 * interoperability between the HDA and HDMI drivers.
3334 */
3335
3336/* Audio Function Group node */
3337#define NVIDIA_AFG_NID 0x01
3338
3339/*
3340 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3341 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3342 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3343 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3344 * additional bit (at position 30) to signal the validity of the format.
3345 *
3346 * | 31 | 30 | 29 16 | 15 0 |
3347 * +---------+-------+--------+--------+
3348 * | TRIGGER | VALID | UNUSED | FORMAT |
3349 * +-----------------------------------|
3350 *
3351 * Note that for the trigger bit to take effect it needs to change value
3352 * (i.e. it needs to be toggled).
3353 */
3354#define NVIDIA_GET_SCRATCH0 0xfa6
3355#define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3356#define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3357#define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3358#define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3359#define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3360#define NVIDIA_SCRATCH_VALID (1 << 6)
3361
3362#define NVIDIA_GET_SCRATCH1 0xfab
3363#define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3364#define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3365#define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3366#define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3367
3368/*
3369 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3370 * the format is invalidated so that the HDMI codec can be disabled.
3371 */
3372static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3373{
3374 unsigned int value;
3375
3376 /* bits [31:30] contain the trigger and valid bits */
3377 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3378 NVIDIA_GET_SCRATCH0, 0);
3379 value = (value >> 24) & 0xff;
3380
3381 /* bits [15:0] are used to store the HDA format */
3382 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3383 NVIDIA_SET_SCRATCH0_BYTE0,
3384 (format >> 0) & 0xff);
3385 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3386 NVIDIA_SET_SCRATCH0_BYTE1,
3387 (format >> 8) & 0xff);
3388
3389 /* bits [16:24] are unused */
3390 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3391 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3392
3393 /*
3394 * Bit 30 signals that the data is valid and hence that HDMI audio can
3395 * be enabled.
3396 */
3397 if (format == 0)
3398 value &= ~NVIDIA_SCRATCH_VALID;
3399 else
3400 value |= NVIDIA_SCRATCH_VALID;
3401
3402 /*
3403 * Whenever the trigger bit is toggled, an interrupt is raised in the
3404 * HDMI codec. The HDMI driver will use that as trigger to update its
3405 * configuration.
3406 */
3407 value ^= NVIDIA_SCRATCH_TRIGGER;
3408
3409 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3410 NVIDIA_SET_SCRATCH0_BYTE3, value);
3411}
3412
3413static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3414 struct hda_codec *codec,
3415 unsigned int stream_tag,
3416 unsigned int format,
3417 struct snd_pcm_substream *substream)
3418{
3419 int err;
3420
3421 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3422 format, substream);
3423 if (err < 0)
3424 return err;
3425
3426 /* notify the HDMI codec of the format change */
3427 tegra_hdmi_set_format(codec, format);
3428
3429 return 0;
3430}
3431
3432static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3433 struct hda_codec *codec,
3434 struct snd_pcm_substream *substream)
3435{
3436 /* invalidate the format in the HDMI codec */
3437 tegra_hdmi_set_format(codec, 0);
3438
3439 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3440}
3441
3442static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3443{
3444 struct hdmi_spec *spec = codec->spec;
3445 unsigned int i;
3446
3447 for (i = 0; i < spec->num_pins; i++) {
3448 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3449
3450 if (pcm->pcm_type == type)
3451 return pcm;
3452 }
3453
3454 return NULL;
3455}
3456
3457static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3458{
3459 struct hda_pcm_stream *stream;
3460 struct hda_pcm *pcm;
3461 int err;
3462
3463 err = generic_hdmi_build_pcms(codec);
3464 if (err < 0)
3465 return err;
3466
3467 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3468 if (!pcm)
3469 return -ENODEV;
3470
3471 /*
3472 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3473 * codec about format changes.
3474 */
3475 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3476 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3477 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3478
3479 return 0;
3480}
3481
3482static int patch_tegra_hdmi(struct hda_codec *codec)
3483{
3484 int err;
3485
3486 err = patch_generic_hdmi(codec);
3487 if (err)
3488 return err;
3489
3490 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3491
3492 return 0;
3493}
3494
3495/*
Anssi Hannula5a6135842013-10-24 21:10:35 +03003496 * ATI/AMD-specific implementations
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003497 */
3498
Anssi Hannula5a6135842013-10-24 21:10:35 +03003499#define is_amdhdmi_rev3_or_later(codec) \
Takashi Iwai7639a062015-03-03 10:07:24 +01003500 ((codec)->core.vendor_id == 0x1002aa01 && \
3501 ((codec)->core.revision_id & 0xff00) >= 0x0300)
Anssi Hannula5a6135842013-10-24 21:10:35 +03003502#define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003503
Anssi Hannula5a6135842013-10-24 21:10:35 +03003504/* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3505#define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3506#define ATI_VERB_SET_DOWNMIX_INFO 0x772
3507#define ATI_VERB_SET_MULTICHANNEL_01 0x777
3508#define ATI_VERB_SET_MULTICHANNEL_23 0x778
3509#define ATI_VERB_SET_MULTICHANNEL_45 0x779
3510#define ATI_VERB_SET_MULTICHANNEL_67 0x77a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003511#define ATI_VERB_SET_HBR_CONTROL 0x77c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003512#define ATI_VERB_SET_MULTICHANNEL_1 0x785
3513#define ATI_VERB_SET_MULTICHANNEL_3 0x786
3514#define ATI_VERB_SET_MULTICHANNEL_5 0x787
3515#define ATI_VERB_SET_MULTICHANNEL_7 0x788
3516#define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3517#define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3518#define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3519#define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3520#define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3521#define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3522#define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003523#define ATI_VERB_GET_HBR_CONTROL 0xf7c
Anssi Hannula5a6135842013-10-24 21:10:35 +03003524#define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3525#define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3526#define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3527#define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3528#define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3529
Anssi Hannula84d69e72013-10-24 21:10:38 +03003530/* AMD specific HDA cvt verbs */
3531#define ATI_VERB_SET_RAMP_RATE 0x770
3532#define ATI_VERB_GET_RAMP_RATE 0xf70
3533
Anssi Hannula5a6135842013-10-24 21:10:35 +03003534#define ATI_OUT_ENABLE 0x1
3535
3536#define ATI_MULTICHANNEL_MODE_PAIRED 0
3537#define ATI_MULTICHANNEL_MODE_SINGLE 1
3538
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003539#define ATI_HBR_CAPABLE 0x01
3540#define ATI_HBR_ENABLE 0x10
3541
Anssi Hannula89250f82013-10-24 21:10:36 +03003542static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3543 unsigned char *buf, int *eld_size)
3544{
3545 /* call hda_eld.c ATI/AMD-specific function */
3546 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3547 is_amdhdmi_rev3_or_later(codec));
3548}
3549
Anssi Hannula5a6135842013-10-24 21:10:35 +03003550static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3551 int active_channels, int conn_type)
3552{
3553 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3554}
3555
3556static int atihdmi_paired_swap_fc_lfe(int pos)
3557{
3558 /*
3559 * ATI/AMD have automatic FC/LFE swap built-in
3560 * when in pairwise mapping mode.
3561 */
3562
3563 switch (pos) {
3564 /* see channel_allocations[].speakers[] */
3565 case 2: return 3;
3566 case 3: return 2;
3567 default: break;
3568 }
3569
3570 return pos;
3571}
3572
3573static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3574{
3575 struct cea_channel_speaker_allocation *cap;
3576 int i, j;
3577
3578 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3579
3580 cap = &channel_allocations[get_channel_allocation_order(ca)];
3581 for (i = 0; i < chs; ++i) {
3582 int mask = to_spk_mask(map[i]);
3583 bool ok = false;
3584 bool companion_ok = false;
3585
3586 if (!mask)
3587 continue;
3588
3589 for (j = 0 + i % 2; j < 8; j += 2) {
3590 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3591 if (cap->speakers[chan_idx] == mask) {
3592 /* channel is in a supported position */
3593 ok = true;
3594
3595 if (i % 2 == 0 && i + 1 < chs) {
3596 /* even channel, check the odd companion */
3597 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3598 int comp_mask_req = to_spk_mask(map[i+1]);
3599 int comp_mask_act = cap->speakers[comp_chan_idx];
3600
3601 if (comp_mask_req == comp_mask_act)
3602 companion_ok = true;
3603 else
3604 return -EINVAL;
3605 }
3606 break;
3607 }
3608 }
3609
3610 if (!ok)
3611 return -EINVAL;
3612
3613 if (companion_ok)
3614 i++; /* companion channel already checked */
3615 }
3616
3617 return 0;
3618}
3619
3620static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3621 int hdmi_slot, int stream_channel)
3622{
3623 int verb;
3624 int ati_channel_setup = 0;
3625
3626 if (hdmi_slot > 7)
3627 return -EINVAL;
3628
3629 if (!has_amd_full_remap_support(codec)) {
3630 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3631
3632 /* In case this is an odd slot but without stream channel, do not
3633 * disable the slot since the corresponding even slot could have a
3634 * channel. In case neither have a channel, the slot pair will be
3635 * disabled when this function is called for the even slot. */
3636 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3637 return 0;
3638
3639 hdmi_slot -= hdmi_slot % 2;
3640
3641 if (stream_channel != 0xf)
3642 stream_channel -= stream_channel % 2;
3643 }
3644
3645 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3646
3647 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3648
3649 if (stream_channel != 0xf)
3650 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3651
3652 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3653}
3654
3655static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3656 int asp_slot)
3657{
3658 bool was_odd = false;
3659 int ati_asp_slot = asp_slot;
3660 int verb;
3661 int ati_channel_setup;
3662
3663 if (asp_slot > 7)
3664 return -EINVAL;
3665
3666 if (!has_amd_full_remap_support(codec)) {
3667 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3668 if (ati_asp_slot % 2 != 0) {
3669 ati_asp_slot -= 1;
3670 was_odd = true;
3671 }
3672 }
3673
3674 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3675
3676 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3677
3678 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3679 return 0xf;
3680
3681 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3682}
3683
3684static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3685 int channels)
3686{
3687 int c;
3688
3689 /*
3690 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3691 * we need to take that into account (a single channel may take 2
3692 * channel slots if we need to carry a silent channel next to it).
3693 * On Rev3+ AMD codecs this function is not used.
3694 */
3695 int chanpairs = 0;
3696
3697 /* We only produce even-numbered channel count TLVs */
3698 if ((channels % 2) != 0)
3699 return -1;
3700
3701 for (c = 0; c < 7; c += 2) {
3702 if (cap->speakers[c] || cap->speakers[c+1])
3703 chanpairs++;
3704 }
3705
3706 if (chanpairs * 2 != channels)
3707 return -1;
3708
3709 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3710}
3711
3712static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3713 unsigned int *chmap, int channels)
3714{
3715 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3716 int count = 0;
3717 int c;
3718
3719 for (c = 7; c >= 0; c--) {
3720 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3721 int spk = cap->speakers[chan];
3722 if (!spk) {
3723 /* add N/A channel if the companion channel is occupied */
3724 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3725 chmap[count++] = SNDRV_CHMAP_NA;
3726
3727 continue;
3728 }
3729
3730 chmap[count++] = spk_to_chmap(spk);
3731 }
3732
3733 WARN_ON(count != channels);
3734}
3735
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003736static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3737 bool hbr)
3738{
3739 int hbr_ctl, hbr_ctl_new;
3740
3741 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
Anssi Hannula13122e62013-11-10 20:56:10 +02003742 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003743 if (hbr)
3744 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3745 else
3746 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3747
Takashi Iwai4e76a882014-02-25 12:21:03 +01003748 codec_dbg(codec,
3749 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003750 pin_nid,
3751 hbr_ctl == hbr_ctl_new ? "" : "new-",
3752 hbr_ctl_new);
3753
3754 if (hbr_ctl != hbr_ctl_new)
3755 snd_hda_codec_write(codec, pin_nid, 0,
3756 ATI_VERB_SET_HBR_CONTROL,
3757 hbr_ctl_new);
3758
3759 } else if (hbr)
3760 return -EINVAL;
3761
3762 return 0;
3763}
3764
Anssi Hannula84d69e72013-10-24 21:10:38 +03003765static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3766 hda_nid_t pin_nid, u32 stream_tag, int format)
3767{
3768
3769 if (is_amdhdmi_rev3_or_later(codec)) {
3770 int ramp_rate = 180; /* default as per AMD spec */
3771 /* disable ramp-up/down for non-pcm as per AMD spec */
3772 if (format & AC_FMT_TYPE_NON_PCM)
3773 ramp_rate = 0;
3774
3775 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3776 }
3777
3778 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3779}
3780
3781
Anssi Hannula5a6135842013-10-24 21:10:35 +03003782static int atihdmi_init(struct hda_codec *codec)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003783{
3784 struct hdmi_spec *spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003785 int pin_idx, err;
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003786
Anssi Hannula5a6135842013-10-24 21:10:35 +03003787 err = generic_hdmi_init(codec);
3788
3789 if (err)
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003790 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003791
3792 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3793 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3794
3795 /* make sure downmix information in infoframe is zero */
3796 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3797
3798 /* enable channel-wise remap mode if supported */
3799 if (has_amd_full_remap_support(codec))
3800 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3801 ATI_VERB_SET_MULTICHANNEL_MODE,
3802 ATI_MULTICHANNEL_MODE_SINGLE);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003803 }
Anssi Hannula5a6135842013-10-24 21:10:35 +03003804
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003805 return 0;
3806}
3807
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003808static int patch_atihdmi(struct hda_codec *codec)
3809{
3810 struct hdmi_spec *spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003811 struct hdmi_spec_per_cvt *per_cvt;
3812 int err, cvt_idx;
3813
3814 err = patch_generic_hdmi(codec);
3815
3816 if (err)
Takashi Iwaid0b12522012-06-15 14:34:42 +02003817 return err;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003818
3819 codec->patch_ops.init = atihdmi_init;
3820
Takashi Iwaid0b12522012-06-15 14:34:42 +02003821 spec = codec->spec;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003822
Anssi Hannula89250f82013-10-24 21:10:36 +03003823 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003824 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3825 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3826 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
Anssi Hannula461cf6b2013-10-24 21:10:37 +03003827 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
Anssi Hannula84d69e72013-10-24 21:10:38 +03003828 spec->ops.setup_stream = atihdmi_setup_stream;
Anssi Hannula5a6135842013-10-24 21:10:35 +03003829
3830 if (!has_amd_full_remap_support(codec)) {
3831 /* override to ATI/AMD-specific versions with pairwise mapping */
3832 spec->ops.chmap_cea_alloc_validate_get_type =
3833 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3834 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3835 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3836 }
3837
3838 /* ATI/AMD converters do not advertise all of their capabilities */
3839 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3840 per_cvt = get_cvt(spec, cvt_idx);
3841 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3842 per_cvt->rates |= SUPPORTED_RATES;
3843 per_cvt->formats |= SUPPORTED_FORMATS;
3844 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3845 }
3846
3847 spec->channels_max = max(spec->channels_max, 8u);
3848
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003849 return 0;
3850}
3851
Annie Liu3de5ff82012-06-08 19:18:42 +08003852/* VIA HDMI Implementation */
3853#define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3854#define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3855
Annie Liu3de5ff82012-06-08 19:18:42 +08003856static int patch_via_hdmi(struct hda_codec *codec)
3857{
Takashi Iwai250e41a2012-06-15 14:40:21 +02003858 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
Annie Liu3de5ff82012-06-08 19:18:42 +08003859}
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003860
3861/*
3862 * patch entries
3863 */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003864static const struct hda_device_id snd_hda_id_hdmi[] = {
3865HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3866HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3867HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3868HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3869HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3870HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3871HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3872HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3873HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3874HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3875HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3876HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3877HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3878HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3879HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3880HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3881HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3882HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3883HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3884HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3885HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3886HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3887HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
Richard Samsonc8900a02011-03-03 12:46:13 +01003888/* 17 is known to be absent */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003889HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3890HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3891HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3892HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3893HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3894HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3895HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3896HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3897HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3898HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3899HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3900HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3901HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3902HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3903HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3904HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3905HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3906HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3907HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3908HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3909HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
3910HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3911HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3912HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3913HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
3914HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
3915HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
3916HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
3917HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
3918HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
3919HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
3920HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
3921HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
3922HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
3923HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
3924HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
3925HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
Libin Yang91815d82016-01-14 14:09:00 +08003926HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003927HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
3928HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
3929HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
3930HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003931/* special ID for generic HDMI */
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003932HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003933{} /* terminator */
3934};
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003935MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003936
3937MODULE_LICENSE("GPL");
3938MODULE_DESCRIPTION("HDMI HD-audio codec");
3939MODULE_ALIAS("snd-hda-codec-intelhdmi");
3940MODULE_ALIAS("snd-hda-codec-nvhdmi");
3941MODULE_ALIAS("snd-hda-codec-atihdmi");
3942
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003943static struct hda_codec_driver hdmi_driver = {
Takashi Iwaib9a94a92015-10-01 16:20:04 +02003944 .id = snd_hda_id_hdmi,
Takashi Iwai84eb01b2010-09-07 12:27:25 +02003945};
3946
Takashi Iwaid8a766a2015-02-17 15:25:37 +01003947module_hda_codec_driver(hdmi_driver);