xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 624900163d060f15d71ff383104a909737de770c)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
3318382eadSSubhransu S. Prusty #include "../../hda/local.h"
344a3478deSJeeja KP #include "hdac_hdmi.h"
3518382eadSSubhransu S. Prusty 
3617a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3717a42c45SSubhransu S. Prusty 
38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
4018382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
41b0362adbSSubhransu S. Prusty 
4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4318382eadSSubhransu S. Prusty 
44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
45754695f9SJeeja KP #define HDA_MAX_PORTS		3
46148569fdSSubhransu S. Prusty 
47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
49b8a54545SSubhransu S. Prusty 
50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
53f6fa11a3SSandeep Tayal 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5518382eadSSubhransu S. Prusty 	unsigned int channels_min;
5618382eadSSubhransu S. Prusty 	unsigned int channels_max;
5718382eadSSubhransu S. Prusty 	u32 rates;
5818382eadSSubhransu S. Prusty 	u64 formats;
5918382eadSSubhransu S. Prusty 	unsigned int maxbps;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
6315b91447SSubhransu S. Prusty 	struct list_head head;
6418382eadSSubhransu S. Prusty 	hda_nid_t nid;
654a3478deSJeeja KP 	const char *name;
6618382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6718382eadSSubhransu S. Prusty };
6818382eadSSubhransu S. Prusty 
69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
71b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
72b7756edeSSubhransu S. Prusty };
73b7756edeSSubhransu S. Prusty 
74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
75b8a54545SSubhransu S. Prusty 	bool	monitor_present;
76b8a54545SSubhransu S. Prusty 	bool	eld_valid;
77b8a54545SSubhransu S. Prusty 	int	eld_size;
78b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
79b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
80b8a54545SSubhransu S. Prusty };
81b8a54545SSubhransu S. Prusty 
8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
8315b91447SSubhransu S. Prusty 	struct list_head head;
8418382eadSSubhransu S. Prusty 	hda_nid_t nid;
852acd8309SJeeja KP 	bool mst_capable;
86754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
87754695f9SJeeja KP 	int num_ports;
88754695f9SJeeja KP 	struct hdac_ext_device *edev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
9818382eadSSubhransu S. Prusty };
9918382eadSSubhransu S. Prusty 
1004a3478deSJeeja KP struct hdac_hdmi_pcm {
1014a3478deSJeeja KP 	struct list_head head;
1024a3478deSJeeja KP 	int pcm_id;
103e0e5d3e5SJeeja KP 	struct list_head port_list;
1044a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
105*62490016SJeeja KP 	struct snd_soc_jack *jack;
106c9bfb5d7SJeeja KP 	int stream_tag;
107c9bfb5d7SJeeja KP 	int channels;
108c9bfb5d7SJeeja KP 	int format;
109ab1eea19SJeeja KP 	bool chmap_set;
110ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
111ab1eea19SJeeja KP 	struct mutex lock;
112e0e5d3e5SJeeja KP 	int jack_event;
1134a3478deSJeeja KP };
1144a3478deSJeeja KP 
115754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11618382eadSSubhransu S. Prusty 	int dai_id;
117754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11918382eadSSubhransu S. Prusty };
12018382eadSSubhransu S. Prusty 
12118382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
122754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12315b91447SSubhransu S. Prusty 	struct list_head pin_list;
12415b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1254a3478deSJeeja KP 	struct list_head pcm_list;
12615b91447SSubhransu S. Prusty 	int num_pin;
12715b91447SSubhransu S. Prusty 	int num_cvt;
128754695f9SJeeja KP 	int num_ports;
1294a3478deSJeeja KP 	struct mutex pin_mutex;
130bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
13118382eadSSubhransu S. Prusty };
13218382eadSSubhransu S. Prusty 
133c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
134c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
135c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
136c9bfb5d7SJeeja KP {
137c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1381de777feSJeeja KP 
139c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
140c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
141c9bfb5d7SJeeja KP 			break;
142c9bfb5d7SJeeja KP 	}
143c9bfb5d7SJeeja KP 
144c9bfb5d7SJeeja KP 	return pcm;
145c9bfb5d7SJeeja KP }
1461de777feSJeeja KP 
147e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
148e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
149e0e5d3e5SJeeja KP {
150e0e5d3e5SJeeja KP 	struct hdac_ext_device *edev = port->pin->edev;
151e0e5d3e5SJeeja KP 
152e0e5d3e5SJeeja KP 	if (is_connect) {
153e0e5d3e5SJeeja KP 		/*
154e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
155e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
156e0e5d3e5SJeeja KP 		 * ports.
157e0e5d3e5SJeeja KP 		 */
158e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
159e0e5d3e5SJeeja KP 			dev_dbg(&edev->hdac.dev,
160e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
161e0e5d3e5SJeeja KP 					pcm->pcm_id);
162*62490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
163*62490016SJeeja KP 						SND_JACK_AVOUT);
164e0e5d3e5SJeeja KP 		}
165e0e5d3e5SJeeja KP 		pcm->jack_event++;
166e0e5d3e5SJeeja KP 	} else {
167e0e5d3e5SJeeja KP 		/*
168e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
169e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
170e0e5d3e5SJeeja KP 		 * to multiple ports.
171e0e5d3e5SJeeja KP 		 */
172e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
173*62490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
174e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
175e0e5d3e5SJeeja KP 			pcm->jack_event--;
176e0e5d3e5SJeeja KP 	}
177e0e5d3e5SJeeja KP }
178e0e5d3e5SJeeja KP 
179fc181b04SJeeja KP /* MST supported verbs */
180fc181b04SJeeja KP /*
181fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
182fc181b04SJeeja KP  */
183fc181b04SJeeja KP static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
184fc181b04SJeeja KP {
185fc181b04SJeeja KP 	unsigned int caps;
186fc181b04SJeeja KP 	unsigned int type, param;
187fc181b04SJeeja KP 
188fc181b04SJeeja KP 	caps = get_wcaps(&hdac->hdac, nid);
189fc181b04SJeeja KP 	type = get_wcaps_type(caps);
190fc181b04SJeeja KP 
191fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
192fc181b04SJeeja KP 		return 0;
193fc181b04SJeeja KP 
194fc181b04SJeeja KP 	param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
195fc181b04SJeeja KP 					AC_PAR_DEVLIST_LEN);
196fc181b04SJeeja KP 	if (param == -1)
197fc181b04SJeeja KP 		return param;
198fc181b04SJeeja KP 
199fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
200fc181b04SJeeja KP }
201fc181b04SJeeja KP 
202fc181b04SJeeja KP /*
203fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
204fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
205fc181b04SJeeja KP  * is selected or MST is not supported.
206fc181b04SJeeja KP  */
207fc181b04SJeeja KP static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
208fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
209fc181b04SJeeja KP {
210fc181b04SJeeja KP 	return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
211fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
212fc181b04SJeeja KP }
213fc181b04SJeeja KP 
214fc181b04SJeeja KP /*
215fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
216fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
217fc181b04SJeeja KP  */
218fc181b04SJeeja KP static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
219fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
220fc181b04SJeeja KP {
221fc181b04SJeeja KP 	int num_ports;
222fc181b04SJeeja KP 
223fc181b04SJeeja KP 	if (!port->pin->mst_capable)
224fc181b04SJeeja KP 		return 0;
225fc181b04SJeeja KP 
226fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
227fc181b04SJeeja KP 	num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
228fc181b04SJeeja KP 
229fc181b04SJeeja KP 	if (num_ports < 0)
230fc181b04SJeeja KP 		return -EIO;
231fc181b04SJeeja KP 	/*
232fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
233fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
234fc181b04SJeeja KP 	 */
235fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
236fc181b04SJeeja KP 		return 0;
237fc181b04SJeeja KP 
238fc181b04SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
239fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
240fc181b04SJeeja KP 
241fc181b04SJeeja KP 	if (port->id != hdac_hdmi_port_select_get(hdac, port))
242fc181b04SJeeja KP 		return -EIO;
243fc181b04SJeeja KP 
244fc181b04SJeeja KP 	dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
245fc181b04SJeeja KP 
246fc181b04SJeeja KP 	return 0;
247fc181b04SJeeja KP }
248fc181b04SJeeja KP 
2492889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2502889099eSSubhransu S. Prusty 						int pcm_idx)
2512889099eSSubhransu S. Prusty {
2522889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2532889099eSSubhransu S. Prusty 
2542889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2552889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2562889099eSSubhransu S. Prusty 			return pcm;
2572889099eSSubhransu S. Prusty 	}
2582889099eSSubhransu S. Prusty 
2592889099eSSubhransu S. Prusty 	return NULL;
2602889099eSSubhransu S. Prusty }
2612889099eSSubhransu S. Prusty 
262e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
263e342ac08SSubhransu S. Prusty {
26451b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
265e342ac08SSubhransu S. Prusty 
26651b2c425SGeliang Tang 	return to_ehdac_device(hdac);
267e342ac08SSubhransu S. Prusty }
268e342ac08SSubhransu S. Prusty 
2692428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2702428bca3SSubhransu S. Prusty {
2712428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2722428bca3SSubhransu S. Prusty }
2732428bca3SSubhransu S. Prusty 
2742428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2752428bca3SSubhransu S. Prusty {
2762428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2772428bca3SSubhransu S. Prusty }
2782428bca3SSubhransu S. Prusty 
2792428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2802428bca3SSubhransu S. Prusty 						void *eld)
2812428bca3SSubhransu S. Prusty {
2822428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
2832428bca3SSubhransu S. Prusty 	int i;
2842428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
2852428bca3SSubhransu S. Prusty 
2862428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
2872428bca3SSubhransu S. Prusty 	if (!sad)
2882428bca3SSubhransu S. Prusty 		goto format_constraint;
2892428bca3SSubhransu S. Prusty 
2902428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
2912428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
2922428bca3SSubhransu S. Prusty 
2932428bca3SSubhransu S. Prusty 			/*
2942428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
2952428bca3SSubhransu S. Prusty 			 * container so we set S32
2962428bca3SSubhransu S. Prusty 			 */
2972428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
2982428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
2992428bca3SSubhransu S. Prusty 		}
3002428bca3SSubhransu S. Prusty 	}
3012428bca3SSubhransu S. Prusty 
3022428bca3SSubhransu S. Prusty format_constraint:
3032428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3042428bca3SSubhransu S. Prusty 				formats);
3052428bca3SSubhransu S. Prusty 
3062428bca3SSubhransu S. Prusty }
3072428bca3SSubhransu S. Prusty 
308a657f1d0SSubhransu S. Prusty static void
309a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
310a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
311a657f1d0SSubhransu S. Prusty {
312a657f1d0SSubhransu S. Prusty 	int val;
313a657f1d0SSubhransu S. Prusty 
314a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
315a657f1d0SSubhransu S. Prusty 
316a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
317a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
318a657f1d0SSubhransu S. Prusty }
319a657f1d0SSubhransu S. Prusty 
320478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
321478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
322478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
323478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
324478f544eSSubhransu S. Prusty 
325478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
326478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
327478f544eSSubhransu S. Prusty 	u8 CXT04;
328478f544eSSubhransu S. Prusty 	u8 CA;
329478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
330478f544eSSubhransu S. Prusty };
331478f544eSSubhransu S. Prusty 
332a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
333754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
334a657f1d0SSubhransu S. Prusty {
335a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
336a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
337754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
338478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
339478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
340ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
341478f544eSSubhransu S. Prusty 	u8 *dip;
342a657f1d0SSubhransu S. Prusty 	int ret;
343a657f1d0SSubhransu S. Prusty 	int i;
344478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
345478f544eSSubhransu S. Prusty 	u8 conn_type;
346bcced704SSubhransu S. Prusty 	int channels, ca;
347a657f1d0SSubhransu S. Prusty 
348754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
349ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
350bcced704SSubhransu S. Prusty 
351bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
352ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
353bcced704SSubhransu S. Prusty 
354bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
355ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
356bcced704SSubhransu S. Prusty 
357754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
358478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
359a657f1d0SSubhransu S. Prusty 
360478f544eSSubhransu S. Prusty 	switch (conn_type) {
361478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
362478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
363478f544eSSubhransu S. Prusty 
364478f544eSSubhransu S. Prusty 		frame.channels = channels;
365bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
366a657f1d0SSubhransu S. Prusty 
367a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
368a657f1d0SSubhransu S. Prusty 		if (ret < 0)
369a657f1d0SSubhransu S. Prusty 			return ret;
370a657f1d0SSubhransu S. Prusty 
371478f544eSSubhransu S. Prusty 		break;
372478f544eSSubhransu S. Prusty 
373478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
374478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
375478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
376478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
377478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
378478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
379bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
380478f544eSSubhransu S. Prusty 
381478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
382478f544eSSubhransu S. Prusty 		break;
383478f544eSSubhransu S. Prusty 
384478f544eSSubhransu S. Prusty 	default:
385478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
386478f544eSSubhransu S. Prusty 						conn_type);
387478f544eSSubhransu S. Prusty 		return -EIO;
388478f544eSSubhransu S. Prusty 	}
389478f544eSSubhransu S. Prusty 
390a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
391ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
392ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
393a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
394a657f1d0SSubhransu S. Prusty 
395a657f1d0SSubhransu S. Prusty 
396a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
397ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
398478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
399391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
400ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
401391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
402478f544eSSubhransu S. Prusty 	} else {
403478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
404ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
405478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
406478f544eSSubhransu S. Prusty 	}
407a657f1d0SSubhransu S. Prusty 
408a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
409ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
410ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
411a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
412a657f1d0SSubhransu S. Prusty 
413a657f1d0SSubhransu S. Prusty 	return 0;
414a657f1d0SSubhransu S. Prusty }
415a657f1d0SSubhransu S. Prusty 
416c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
417c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
418c9bfb5d7SJeeja KP 		int slots, int slot_width)
419b0362adbSSubhransu S. Prusty {
420c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
421c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
422754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
423c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
424c9bfb5d7SJeeja KP 
425c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
426b0362adbSSubhransu S. Prusty 
427b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
428b0362adbSSubhransu S. Prusty 
429c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
430b0362adbSSubhransu S. Prusty 
431c9bfb5d7SJeeja KP 	if (pcm)
432c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
433bcced704SSubhransu S. Prusty 
434c9bfb5d7SJeeja KP 	return 0;
435b0362adbSSubhransu S. Prusty }
436b0362adbSSubhransu S. Prusty 
437b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
438b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
439b0362adbSSubhransu S. Prusty {
440b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
44154dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
442754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
443754695f9SJeeja KP 	struct hdac_hdmi_port *port;
444c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
445c9bfb5d7SJeeja KP 	int format;
446b0362adbSSubhransu S. Prusty 
44754dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
448754695f9SJeeja KP 	port = dai_map->port;
44954dfa1eaSSubhransu S. Prusty 
450754695f9SJeeja KP 	if (!port)
45154dfa1eaSSubhransu S. Prusty 		return -ENODEV;
45254dfa1eaSSubhransu S. Prusty 
453754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
454754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
455754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
456754695f9SJeeja KP 					port->pin->nid, port->id);
457b0362adbSSubhransu S. Prusty 		return -ENODEV;
458b0362adbSSubhransu S. Prusty 	}
459b0362adbSSubhransu S. Prusty 
460c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
461b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
462b0362adbSSubhransu S. Prusty 			24, 0);
463b0362adbSSubhransu S. Prusty 
464c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
465c9bfb5d7SJeeja KP 	if (!pcm)
466148569fdSSubhransu S. Prusty 		return -EIO;
467148569fdSSubhransu S. Prusty 
468c9bfb5d7SJeeja KP 	pcm->format = format;
469c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
470148569fdSSubhransu S. Prusty 
471148569fdSSubhransu S. Prusty 	return 0;
472148569fdSSubhransu S. Prusty }
473148569fdSSubhransu S. Prusty 
474754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
475754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
476754695f9SJeeja KP 					struct hdac_hdmi_port *port)
477148569fdSSubhransu S. Prusty {
478148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
479148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
480148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
481148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
482148569fdSSubhransu S. Prusty 		return -EINVAL;
483148569fdSSubhransu S. Prusty 	}
484148569fdSSubhransu S. Prusty 
4851b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(hdac, port) < 0)
4861b46ebd1SJeeja KP 		return -EIO;
4871b46ebd1SJeeja KP 
488754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
489754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
490754695f9SJeeja KP 	if (port->num_mux_nids == 0)
491754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
492754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
493754695f9SJeeja KP 						pin->nid, port->id);
494148569fdSSubhransu S. Prusty 
495754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
496754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
497148569fdSSubhransu S. Prusty 
498754695f9SJeeja KP 	return port->num_mux_nids;
499148569fdSSubhransu S. Prusty }
500148569fdSSubhransu S. Prusty 
501148569fdSSubhransu S. Prusty /*
502754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
503148569fdSSubhransu S. Prusty  *
504754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
505148569fdSSubhransu S. Prusty  *
506754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
507754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
508148569fdSSubhransu S. Prusty  * connected.
509148569fdSSubhransu S. Prusty  */
510754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
511148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
512148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
513148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
514148569fdSSubhransu S. Prusty {
515148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
516754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
517148569fdSSubhransu S. Prusty 	int ret, i;
518148569fdSSubhransu S. Prusty 
519148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
520148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
521e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
522e0e5d3e5SJeeja KP 				continue;
523148569fdSSubhransu S. Prusty 
524e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
525e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
526e0e5d3e5SJeeja KP 				ret = hdac_hdmi_query_port_connlist(edev,
527e0e5d3e5SJeeja KP 							port->pin, port);
528e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
529148569fdSSubhransu S. Prusty 				if (ret < 0)
530e0e5d3e5SJeeja KP 					continue;
531148569fdSSubhransu S. Prusty 
532754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
533e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
534e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
535e0e5d3e5SJeeja KP 						port->eld.eld_valid)
536754695f9SJeeja KP 						return port;
537148569fdSSubhransu S. Prusty 				}
538148569fdSSubhransu S. Prusty 			}
539e0e5d3e5SJeeja KP 		}
540e0e5d3e5SJeeja KP 	}
541148569fdSSubhransu S. Prusty 
542148569fdSSubhransu S. Prusty 	return NULL;
543148569fdSSubhransu S. Prusty }
544148569fdSSubhransu S. Prusty 
54554dfa1eaSSubhransu S. Prusty /*
54654dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
54754dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
54854dfa1eaSSubhransu S. Prusty  * doesn't fail.
54954dfa1eaSSubhransu S. Prusty  */
550b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
551b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
552b0362adbSSubhransu S. Prusty {
553b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
554b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
555754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
556148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
557754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5582428bca3SSubhransu S. Prusty 	int ret;
559b0362adbSSubhransu S. Prusty 
560b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
561b0362adbSSubhransu S. Prusty 
562148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
563754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
56454dfa1eaSSubhransu S. Prusty 
56554dfa1eaSSubhransu S. Prusty 	/*
56654dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
56754dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
56854dfa1eaSSubhransu S. Prusty 	 */
569754695f9SJeeja KP 	if (!port)
57054dfa1eaSSubhransu S. Prusty 		return 0;
571754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
572754695f9SJeeja KP 			(!port->eld.eld_valid)) {
573b0362adbSSubhransu S. Prusty 
57454dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
575754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
576754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
577754695f9SJeeja KP 			port->pin->nid, port->id);
578b8a54545SSubhransu S. Prusty 
57954dfa1eaSSubhransu S. Prusty 		return 0;
580b0362adbSSubhransu S. Prusty 	}
581b0362adbSSubhransu S. Prusty 
582754695f9SJeeja KP 	dai_map->port = port;
583b0362adbSSubhransu S. Prusty 
5842428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
585754695f9SJeeja KP 				port->eld.eld_buffer);
5862428bca3SSubhransu S. Prusty 	if (ret < 0)
5872428bca3SSubhransu S. Prusty 		return ret;
588b0362adbSSubhransu S. Prusty 
5892428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
590754695f9SJeeja KP 				port->eld.eld_buffer);
591b0362adbSSubhransu S. Prusty }
592b0362adbSSubhransu S. Prusty 
593b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
594b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
595b0362adbSSubhransu S. Prusty {
596b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
597b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
598754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
599ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
600b0362adbSSubhransu S. Prusty 
601b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
602b0362adbSSubhransu S. Prusty 
603ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
604bcced704SSubhransu S. Prusty 
605ab1eea19SJeeja KP 	if (pcm) {
606ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
607ab1eea19SJeeja KP 		pcm->chmap_set = false;
608ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
609ab1eea19SJeeja KP 		pcm->channels = 0;
610ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
611b0362adbSSubhransu S. Prusty 	}
612ab1eea19SJeeja KP 
613754695f9SJeeja KP 	if (dai_map->port)
614754695f9SJeeja KP 		dai_map->port = NULL;
61554dfa1eaSSubhransu S. Prusty }
616b0362adbSSubhransu S. Prusty 
61718382eadSSubhransu S. Prusty static int
61818382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
61918382eadSSubhransu S. Prusty {
620bcced704SSubhransu S. Prusty 	unsigned int chans;
621bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
622bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
62318382eadSSubhransu S. Prusty 	int err;
62418382eadSSubhransu S. Prusty 
625bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
626bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
627bcced704SSubhransu S. Prusty 
628bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
629bcced704SSubhransu S. Prusty 
630bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
631bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
632bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
63318382eadSSubhransu S. Prusty 
63418382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
63518382eadSSubhransu S. Prusty 			&cvt->params.rates,
63618382eadSSubhransu S. Prusty 			&cvt->params.formats,
63718382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
63818382eadSSubhransu S. Prusty 	if (err < 0)
63918382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
64018382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
64118382eadSSubhransu S. Prusty 			cvt->nid, err);
64218382eadSSubhransu S. Prusty 
64318382eadSSubhransu S. Prusty 	return err;
64418382eadSSubhransu S. Prusty }
64518382eadSSubhransu S. Prusty 
64679f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
647c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
648c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
649c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
650c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
651c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
65218382eadSSubhransu S. Prusty {
65318382eadSSubhransu S. Prusty 	w->id = id;
65479f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
65579f4e922SSubhransu S. Prusty 	if (!w->name)
65679f4e922SSubhransu S. Prusty 		return -ENOMEM;
65779f4e922SSubhransu S. Prusty 
65818382eadSSubhransu S. Prusty 	w->sname = stream;
65918382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
66018382eadSSubhransu S. Prusty 	w->shift = 0;
66179f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
66279f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
66379f4e922SSubhransu S. Prusty 	w->priv = priv;
664c9bfb5d7SJeeja KP 	w->event = event;
665c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
66679f4e922SSubhransu S. Prusty 
66779f4e922SSubhransu S. Prusty 	return 0;
66818382eadSSubhransu S. Prusty }
66918382eadSSubhransu S. Prusty 
67018382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
67179f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
67279f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
67379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
67418382eadSSubhransu S. Prusty {
67518382eadSSubhransu S. Prusty 	route->sink = sink;
67618382eadSSubhransu S. Prusty 	route->source = src;
67718382eadSSubhransu S. Prusty 	route->control = control;
67879f4e922SSubhransu S. Prusty 	route->connected = handler;
67918382eadSSubhransu S. Prusty }
68018382eadSSubhransu S. Prusty 
6814a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
682754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6834a3478deSJeeja KP {
6844a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6854a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
686e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6874a3478deSJeeja KP 
6884a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
689e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
690754695f9SJeeja KP 			continue;
691754695f9SJeeja KP 
692e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
693e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
6944a3478deSJeeja KP 				return pcm;
6954a3478deSJeeja KP 		}
696e0e5d3e5SJeeja KP 	}
6974a3478deSJeeja KP 
6984a3478deSJeeja KP 	return NULL;
6994a3478deSJeeja KP }
7004a3478deSJeeja KP 
701c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
702c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
703c9bfb5d7SJeeja KP {
704c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
705c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
706c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
707c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
708c9bfb5d7SJeeja KP 	}
709c9bfb5d7SJeeja KP }
710c9bfb5d7SJeeja KP 
711c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
712c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
713c9bfb5d7SJeeja KP {
714c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
715c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
716c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
717c9bfb5d7SJeeja KP }
718c9bfb5d7SJeeja KP 
719c9bfb5d7SJeeja KP 
720c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
721c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
722c9bfb5d7SJeeja KP {
723754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
724c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
725c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
726c9bfb5d7SJeeja KP 
727c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
728c9bfb5d7SJeeja KP 			__func__, w->name, event);
729c9bfb5d7SJeeja KP 
730754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
731c9bfb5d7SJeeja KP 	if (!pcm)
732c9bfb5d7SJeeja KP 		return -EIO;
733c9bfb5d7SJeeja KP 
7341b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7351b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
7361b46ebd1SJeeja KP 		return -EIO;
7371b46ebd1SJeeja KP 
738c9bfb5d7SJeeja KP 	switch (event) {
739c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
740754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
741c9bfb5d7SJeeja KP 
742c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
743754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
744c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
745c9bfb5d7SJeeja KP 
746754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
747c9bfb5d7SJeeja KP 
748754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
749c9bfb5d7SJeeja KP 
750c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
751754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
752c9bfb5d7SJeeja KP 
753c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
754754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
755c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
756c9bfb5d7SJeeja KP 
757754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
758c9bfb5d7SJeeja KP 		break;
759c9bfb5d7SJeeja KP 
760c9bfb5d7SJeeja KP 	}
761c9bfb5d7SJeeja KP 
762c9bfb5d7SJeeja KP 	return 0;
763c9bfb5d7SJeeja KP }
764c9bfb5d7SJeeja KP 
765c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
766c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
767c9bfb5d7SJeeja KP {
768c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
769c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
770c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
771c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
772c9bfb5d7SJeeja KP 
773c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
774c9bfb5d7SJeeja KP 			__func__, w->name, event);
775c9bfb5d7SJeeja KP 
776c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
777c9bfb5d7SJeeja KP 	if (!pcm)
778c9bfb5d7SJeeja KP 		return -EIO;
779c9bfb5d7SJeeja KP 
780c9bfb5d7SJeeja KP 	switch (event) {
781c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
782c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
783c9bfb5d7SJeeja KP 
784c9bfb5d7SJeeja KP 		/* Enable transmission */
785c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
786c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
787c9bfb5d7SJeeja KP 
788c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
789c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
790c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
791c9bfb5d7SJeeja KP 
792c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
793c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
794c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
795c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
796c9bfb5d7SJeeja KP 		break;
797c9bfb5d7SJeeja KP 
798c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
799c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
800c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
801c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
802c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
803c9bfb5d7SJeeja KP 
804c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
805c9bfb5d7SJeeja KP 		break;
806c9bfb5d7SJeeja KP 
807c9bfb5d7SJeeja KP 	}
808c9bfb5d7SJeeja KP 
809c9bfb5d7SJeeja KP 	return 0;
810c9bfb5d7SJeeja KP }
811c9bfb5d7SJeeja KP 
812c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
813c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
814c9bfb5d7SJeeja KP {
815754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
816c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
817c9bfb5d7SJeeja KP 	int mux_idx;
818c9bfb5d7SJeeja KP 
819c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
820c9bfb5d7SJeeja KP 			__func__, w->name, event);
821c9bfb5d7SJeeja KP 
822c9bfb5d7SJeeja KP 	if (!kc)
823c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
824c9bfb5d7SJeeja KP 
825c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8261b46ebd1SJeeja KP 
8271b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8281b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
8291b46ebd1SJeeja KP 		return -EIO;
8301b46ebd1SJeeja KP 
831c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
832754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
833c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
834c9bfb5d7SJeeja KP 	}
835c9bfb5d7SJeeja KP 
836c9bfb5d7SJeeja KP 	return 0;
837c9bfb5d7SJeeja KP }
838c9bfb5d7SJeeja KP 
8394a3478deSJeeja KP /*
8404a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8414a3478deSJeeja KP  */
842754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8434a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8444a3478deSJeeja KP {
8454a3478deSJeeja KP 	int ret;
846e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8474a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8484a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8494a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
850754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8514a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
8524a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
8534a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8544a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8554a3478deSJeeja KP 
8564a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8574a3478deSJeeja KP 	if (ret < 0)
8584a3478deSJeeja KP 		return ret;
8594a3478deSJeeja KP 
860754695f9SJeeja KP 	if (port == NULL)
861754695f9SJeeja KP 		return -EINVAL;
862754695f9SJeeja KP 
8634a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8644a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
865e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
866e0e5d3e5SJeeja KP 			continue;
867e0e5d3e5SJeeja KP 
868e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
869e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
870e0e5d3e5SJeeja KP 					p->pin == port->pin) {
871e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
872e0e5d3e5SJeeja KP 				list_del(&p->head);
873e0e5d3e5SJeeja KP 			}
874e0e5d3e5SJeeja KP 		}
875e0e5d3e5SJeeja KP 	}
8764a3478deSJeeja KP 
8774a3478deSJeeja KP 	/*
8784a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8794a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8804a3478deSJeeja KP 	 */
881e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
882e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
883e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
884754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
885e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
8864a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
8874a3478deSJeeja KP 				return ret;
8884a3478deSJeeja KP 			}
8894a3478deSJeeja KP 		}
890e0e5d3e5SJeeja KP 	}
8914a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
8924a3478deSJeeja KP 
8934a3478deSJeeja KP 	return ret;
8944a3478deSJeeja KP }
8954a3478deSJeeja KP 
89679f4e922SSubhransu S. Prusty /*
89779f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
89879f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
89979f4e922SSubhransu S. Prusty  * widget runtime.
90079f4e922SSubhransu S. Prusty  *
90179f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
90279f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
90379f4e922SSubhransu S. Prusty  * "NONE"
90479f4e922SSubhransu S. Prusty  */
905754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
906754695f9SJeeja KP 				struct hdac_hdmi_port *port,
90779f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
90879f4e922SSubhransu S. Prusty 				const char *widget_name)
90918382eadSSubhransu S. Prusty {
91079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
911754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
91279f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
91379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
91479f4e922SSubhransu S. Prusty 	struct soc_enum *se;
91579f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
91679f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
91779f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
91879f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
91979f4e922SSubhransu S. Prusty 	int i = 0;
92079f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
92118382eadSSubhransu S. Prusty 
92279f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
92379f4e922SSubhransu S. Prusty 	if (!kc)
92479f4e922SSubhransu S. Prusty 		return -ENOMEM;
92518382eadSSubhransu S. Prusty 
92679f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
92779f4e922SSubhransu S. Prusty 	if (!se)
92879f4e922SSubhransu S. Prusty 		return -ENOMEM;
92918382eadSSubhransu S. Prusty 
930754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
93179f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
93279f4e922SSubhransu S. Prusty 	if (!kc->name)
93379f4e922SSubhransu S. Prusty 		return -ENOMEM;
93418382eadSSubhransu S. Prusty 
93579f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
93679f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
93779f4e922SSubhransu S. Prusty 	kc->access = 0;
93879f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
939754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
94079f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
94179f4e922SSubhransu S. Prusty 
94279f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
94379f4e922SSubhransu S. Prusty 
94479f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
94579f4e922SSubhransu S. Prusty 	se->items = num_items;
94679f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
94779f4e922SSubhransu S. Prusty 
94879f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
94979f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
95079f4e922SSubhransu S. Prusty 	if (!items[i])
95179f4e922SSubhransu S. Prusty 		return -ENOMEM;
95279f4e922SSubhransu S. Prusty 
95379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
95479f4e922SSubhransu S. Prusty 		i++;
95579f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
95679f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
95779f4e922SSubhransu S. Prusty 		if (!items[i])
95879f4e922SSubhransu S. Prusty 			return -ENOMEM;
95979f4e922SSubhransu S. Prusty 	}
96079f4e922SSubhransu S. Prusty 
96179f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
96279f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
96379f4e922SSubhransu S. Prusty 	if (!se->texts)
96479f4e922SSubhransu S. Prusty 		return -ENOMEM;
96579f4e922SSubhransu S. Prusty 
96679f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
967754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
968c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
969c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
97079f4e922SSubhransu S. Prusty }
97179f4e922SSubhransu S. Prusty 
97279f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
97379f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
97479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
97579f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
97679f4e922SSubhransu S. Prusty {
97779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
97879f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
97979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
980754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
98179f4e922SSubhransu S. Prusty 	int i, j;
98279f4e922SSubhransu S. Prusty 
983754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
98479f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
98579f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
98679f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
98779f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
98879f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
98979f4e922SSubhransu S. Prusty 					se->texts[j + 1],
99079f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
99179f4e922SSubhransu S. Prusty 
99279f4e922SSubhransu S. Prusty 			rindex++;
99379f4e922SSubhransu S. Prusty 		}
99479f4e922SSubhransu S. Prusty 
99579f4e922SSubhransu S. Prusty 		mux_index++;
99679f4e922SSubhransu S. Prusty 	}
99779f4e922SSubhransu S. Prusty }
99879f4e922SSubhransu S. Prusty 
99979f4e922SSubhransu S. Prusty /*
100079f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
100179f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1002754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1003754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
100479f4e922SSubhransu S. Prusty  *
1005754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1006754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
100779f4e922SSubhransu S. Prusty  *
100879f4e922SSubhransu S. Prusty  * Routes are added as below:
1009754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1010754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
101179f4e922SSubhransu S. Prusty  *
101279f4e922SSubhransu S. Prusty  * Total route elements:
1013754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
101479f4e922SSubhransu S. Prusty  */
101579f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
101679f4e922SSubhransu S. Prusty {
101779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
101879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
101979f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
102079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
102179f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
102279f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
102379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
102479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1025754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
102679f4e922SSubhransu S. Prusty 
102779f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
102879f4e922SSubhransu S. Prusty 		return -EINVAL;
102979f4e922SSubhransu S. Prusty 
1030754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1031754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
103279f4e922SSubhransu S. Prusty 				GFP_KERNEL);
103379f4e922SSubhransu S. Prusty 
103479f4e922SSubhransu S. Prusty 	if (!widgets)
103579f4e922SSubhransu S. Prusty 		return -ENOMEM;
103679f4e922SSubhransu S. Prusty 
103779f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
103879f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
103979f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
104079f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1041c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1042c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1043c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1044c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
104579f4e922SSubhransu S. Prusty 		if (ret < 0)
104679f4e922SSubhransu S. Prusty 			return ret;
104779f4e922SSubhransu S. Prusty 		i++;
104879f4e922SSubhransu S. Prusty 	}
104979f4e922SSubhransu S. Prusty 
105079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1051754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1052754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1053754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
105479f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1055754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1056c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1057c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1058754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1059754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
106079f4e922SSubhransu S. Prusty 			if (ret < 0)
106179f4e922SSubhransu S. Prusty 				return ret;
106279f4e922SSubhransu S. Prusty 			i++;
106379f4e922SSubhransu S. Prusty 		}
1064754695f9SJeeja KP 	}
106579f4e922SSubhransu S. Prusty 
106679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
106779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1068754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1069754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1070754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1071754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1072754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
107379f4e922SSubhransu S. Prusty 						widget_name);
107479f4e922SSubhransu S. Prusty 			if (ret < 0)
107579f4e922SSubhransu S. Prusty 				return ret;
107679f4e922SSubhransu S. Prusty 			i++;
107779f4e922SSubhransu S. Prusty 
107879f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
107979f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
108079f4e922SSubhransu S. Prusty 
108179f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
108279f4e922SSubhransu S. Prusty 			num_routes++;
108379f4e922SSubhransu S. Prusty 		}
1084754695f9SJeeja KP 	}
108579f4e922SSubhransu S. Prusty 
108679f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
108779f4e922SSubhransu S. Prusty 							GFP_KERNEL);
108879f4e922SSubhransu S. Prusty 	if (!route)
108979f4e922SSubhransu S. Prusty 		return -ENOMEM;
109079f4e922SSubhransu S. Prusty 
109179f4e922SSubhransu S. Prusty 	i = 0;
109279f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
109379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1094754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
109579f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1096754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1097754695f9SJeeja KP 						hdmi->num_pin;
109879f4e922SSubhransu S. Prusty 
109979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
110079f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
110179f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
110279f4e922SSubhransu S. Prusty 			i++;
1103754695f9SJeeja KP 		}
110479f4e922SSubhransu S. Prusty 	}
110579f4e922SSubhransu S. Prusty 
110679f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
110779f4e922SSubhransu S. Prusty 
110879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1109754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
111079f4e922SSubhransu S. Prusty 
111179f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
111279f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
111379f4e922SSubhransu S. Prusty 
111479f4e922SSubhransu S. Prusty 	return 0;
111579f4e922SSubhransu S. Prusty 
111618382eadSSubhransu S. Prusty }
111718382eadSSubhransu S. Prusty 
111815b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
111918382eadSSubhransu S. Prusty {
112015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1121754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
112215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1123148569fdSSubhransu S. Prusty 	int dai_id = 0;
112418382eadSSubhransu S. Prusty 
1125148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
112615b91447SSubhransu S. Prusty 		return -EINVAL;
112718382eadSSubhransu S. Prusty 
1128148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1129148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1130148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
113115b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
113218382eadSSubhransu S. Prusty 
1133148569fdSSubhransu S. Prusty 		dai_id++;
1134148569fdSSubhransu S. Prusty 
1135148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1136148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1137148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1138148569fdSSubhransu S. Prusty 			break;
1139148569fdSSubhransu S. Prusty 		}
1140148569fdSSubhransu S. Prusty 	}
114118382eadSSubhransu S. Prusty 
114215b91447SSubhransu S. Prusty 	return 0;
114315b91447SSubhransu S. Prusty }
114415b91447SSubhransu S. Prusty 
114515b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
114615b91447SSubhransu S. Prusty {
114715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
114815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11494a3478deSJeeja KP 	char name[NAME_SIZE];
115015b91447SSubhransu S. Prusty 
115115b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
115215b91447SSubhransu S. Prusty 	if (!cvt)
115315b91447SSubhransu S. Prusty 		return -ENOMEM;
115415b91447SSubhransu S. Prusty 
115515b91447SSubhransu S. Prusty 	cvt->nid = nid;
11564a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11574a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
115815b91447SSubhransu S. Prusty 
115915b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
116015b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
116115b91447SSubhransu S. Prusty 
116215b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
116315b91447SSubhransu S. Prusty }
116415b91447SSubhransu S. Prusty 
1165f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1166754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1167b7756edeSSubhransu S. Prusty {
1168f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1169f6fa11a3SSandeep Tayal 
1170754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1171f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1172f6fa11a3SSandeep Tayal 
1173f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1174f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1175f6fa11a3SSandeep Tayal 		return -EINVAL;
1176b7756edeSSubhransu S. Prusty 	}
1177b7756edeSSubhransu S. Prusty 
1178754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1179f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1180f6fa11a3SSandeep Tayal 
1181f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1182f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1183f6fa11a3SSandeep Tayal 		return -EINVAL;
1184f6fa11a3SSandeep Tayal 	}
1185f6fa11a3SSandeep Tayal 
1186754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1187f6fa11a3SSandeep Tayal 
1188f6fa11a3SSandeep Tayal 	return 0;
1189f6fa11a3SSandeep Tayal }
1190f6fa11a3SSandeep Tayal 
1191754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1192754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1193b8a54545SSubhransu S. Prusty {
1194b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
11954a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
11964a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1197754695f9SJeeja KP 	int size = 0;
11982acd8309SJeeja KP 	int port_id = -1;
1199754695f9SJeeja KP 
1200754695f9SJeeja KP 	if (!hdmi)
1201754695f9SJeeja KP 		return;
12024a3478deSJeeja KP 
12032acd8309SJeeja KP 	/*
12042acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12052acd8309SJeeja KP 	 * to be -1.
12062acd8309SJeeja KP 	 */
12074a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1208754695f9SJeeja KP 	port->eld.monitor_present = false;
1209f6fa11a3SSandeep Tayal 
12102acd8309SJeeja KP 	if (pin->mst_capable)
12112acd8309SJeeja KP 		port_id = port->id;
12122acd8309SJeeja KP 
12132acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1214754695f9SJeeja KP 				&port->eld.monitor_present,
1215754695f9SJeeja KP 				port->eld.eld_buffer,
1216f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1217f6fa11a3SSandeep Tayal 
1218f6fa11a3SSandeep Tayal 	if (size > 0) {
1219f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1220754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1221f6fa11a3SSandeep Tayal 			size = -EINVAL;
1222f6fa11a3SSandeep Tayal 	}
1223f6fa11a3SSandeep Tayal 
1224f6fa11a3SSandeep Tayal 	if (size > 0) {
1225754695f9SJeeja KP 		port->eld.eld_valid = true;
1226754695f9SJeeja KP 		port->eld.eld_size = size;
1227f6fa11a3SSandeep Tayal 	} else {
1228754695f9SJeeja KP 		port->eld.eld_valid = false;
1229754695f9SJeeja KP 		port->eld.eld_size = 0;
1230f6fa11a3SSandeep Tayal 	}
1231b8a54545SSubhransu S. Prusty 
1232754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12334a3478deSJeeja KP 
1234754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1235b8a54545SSubhransu S. Prusty 
1236e0e5d3e5SJeeja KP 		dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1237754695f9SJeeja KP 						__func__, pin->nid, port->id);
12384a3478deSJeeja KP 
12394a3478deSJeeja KP 		/*
12404a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12414a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12424a3478deSJeeja KP 		 * control select.
12434a3478deSJeeja KP 		 */
1244e0e5d3e5SJeeja KP 		if (pcm)
1245e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12464a3478deSJeeja KP 
12474a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1248f6fa11a3SSandeep Tayal 		return;
1249b8a54545SSubhransu S. Prusty 	}
1250b8a54545SSubhransu S. Prusty 
1251754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1252e0e5d3e5SJeeja KP 		if (pcm)
1253e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12544a3478deSJeeja KP 
1255f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1256754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1257754695f9SJeeja KP 
1258754695f9SJeeja KP 	}
1259754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12604a3478deSJeeja KP }
12614a3478deSJeeja KP 
1262754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1263754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1264754695f9SJeeja KP {
1265754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1266754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1267754695f9SJeeja KP 	int i;
1268754695f9SJeeja KP 
1269754695f9SJeeja KP 	/*
1270754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1271754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1272754695f9SJeeja KP 	 * implemented.
1273754695f9SJeeja KP 	 */
1274754695f9SJeeja KP 
1275754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1276754695f9SJeeja KP 	if (!ports)
1277754695f9SJeeja KP 		return -ENOMEM;
1278754695f9SJeeja KP 
1279754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1280754695f9SJeeja KP 		ports[i].id = i;
1281754695f9SJeeja KP 		ports[i].pin = pin;
1282754695f9SJeeja KP 	}
1283754695f9SJeeja KP 	pin->ports = ports;
1284754695f9SJeeja KP 	pin->num_ports = max_ports;
1285754695f9SJeeja KP 	return 0;
1286b8a54545SSubhransu S. Prusty }
1287b8a54545SSubhransu S. Prusty 
128815b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
128915b91447SSubhransu S. Prusty {
129015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
129115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1292754695f9SJeeja KP 	int ret;
129315b91447SSubhransu S. Prusty 
129415b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
129515b91447SSubhransu S. Prusty 	if (!pin)
129615b91447SSubhransu S. Prusty 		return -ENOMEM;
129715b91447SSubhransu S. Prusty 
129815b91447SSubhransu S. Prusty 	pin->nid = nid;
12992acd8309SJeeja KP 	pin->mst_capable = false;
1300754695f9SJeeja KP 	pin->edev = edev;
1301754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1302754695f9SJeeja KP 	if (ret < 0)
1303754695f9SJeeja KP 		return ret;
130415b91447SSubhransu S. Prusty 
130515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
130615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1307754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1308b8a54545SSubhransu S. Prusty 
130915b91447SSubhransu S. Prusty 	return 0;
131018382eadSSubhransu S. Prusty }
131118382eadSSubhransu S. Prusty 
1312211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1313211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1314211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1315211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1316211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1317211caab7SSubhransu S. Prusty 
1318211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1319211caab7SSubhransu S. Prusty {
1320211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1321211caab7SSubhransu S. Prusty 
1322211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1323211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1324211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1325211caab7SSubhransu S. Prusty 		return;
1326211caab7SSubhransu S. Prusty 
1327211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1328211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1329211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1330211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1331211caab7SSubhransu S. Prusty 		return;
1332211caab7SSubhransu S. Prusty }
1333211caab7SSubhransu S. Prusty 
1334211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1335211caab7SSubhransu S. Prusty {
1336211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1337211caab7SSubhransu S. Prusty 
1338211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1339211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1340211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1341211caab7SSubhransu S. Prusty 		return;
1342211caab7SSubhransu S. Prusty 
1343211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1344211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1345211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1346211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1347211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1348211caab7SSubhransu S. Prusty 		return;
1349211caab7SSubhransu S. Prusty 
1350211caab7SSubhransu S. Prusty }
1351211caab7SSubhransu S. Prusty 
135217a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
135317a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
135417a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
135517a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1356c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
135717a42c45SSubhransu S. Prusty };
135817a42c45SSubhransu S. Prusty 
135917a42c45SSubhransu S. Prusty /*
136017a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
136117a42c45SSubhransu S. Prusty  * based on the number of converter queried.
136217a42c45SSubhransu S. Prusty  */
136317a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
136417a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
136517a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
136617a42c45SSubhransu S. Prusty {
136717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
136817a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
136917a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
137017a42c45SSubhransu S. Prusty 	int i = 0;
137117a42c45SSubhransu S. Prusty 	u32 rates, bps;
137217a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
137317a42c45SSubhransu S. Prusty 	u64 formats;
137417a42c45SSubhransu S. Prusty 	int ret;
137517a42c45SSubhransu S. Prusty 
137617a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
137717a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
137817a42c45SSubhransu S. Prusty 			GFP_KERNEL);
137917a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
138017a42c45SSubhransu S. Prusty 		return -ENOMEM;
138117a42c45SSubhransu S. Prusty 
138217a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
138317a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
138417a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
138517a42c45SSubhransu S. Prusty 		if (ret)
138617a42c45SSubhransu S. Prusty 			return ret;
138717a42c45SSubhransu S. Prusty 
138817a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
138917a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
139017a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
139117a42c45SSubhransu S. Prusty 
139217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
139317a42c45SSubhransu S. Prusty 			return -ENOMEM;
139417a42c45SSubhransu S. Prusty 
139517a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
139617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
139717a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
139817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
139917a42c45SSubhransu S. Prusty 			return -ENOMEM;
140017a42c45SSubhransu S. Prusty 
140117a42c45SSubhransu S. Prusty 		/*
140217a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
140317a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
140417a42c45SSubhransu S. Prusty 		 */
140517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
140617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
140717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
140817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
140917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
141017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
141117a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
141217a42c45SSubhransu S. Prusty 
141317a42c45SSubhransu S. Prusty 		i++;
141417a42c45SSubhransu S. Prusty 	}
141517a42c45SSubhransu S. Prusty 
141617a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
141717a42c45SSubhransu S. Prusty 
141817a42c45SSubhransu S. Prusty 	return 0;
141917a42c45SSubhransu S. Prusty }
142017a42c45SSubhransu S. Prusty 
142118382eadSSubhransu S. Prusty /*
142218382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
142318382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
142418382eadSSubhransu S. Prusty  */
142517a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
142617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
142718382eadSSubhransu S. Prusty {
142818382eadSSubhransu S. Prusty 	hda_nid_t nid;
14293c83ac23SSudip Mukherjee 	int i, num_nodes;
143018382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
143118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
143215b91447SSubhransu S. Prusty 	int ret;
143318382eadSSubhransu S. Prusty 
1434211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1435211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1436211caab7SSubhransu S. Prusty 
14373c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1438541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
143918382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
144018382eadSSubhransu S. Prusty 		return -EINVAL;
144118382eadSSubhransu S. Prusty 	}
144218382eadSSubhransu S. Prusty 
14433c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
144418382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
144518382eadSSubhransu S. Prusty 
144618382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
144718382eadSSubhransu S. Prusty 		unsigned int caps;
144818382eadSSubhransu S. Prusty 		unsigned int type;
144918382eadSSubhransu S. Prusty 
145018382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
145118382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
145218382eadSSubhransu S. Prusty 
145318382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
145418382eadSSubhransu S. Prusty 			continue;
145518382eadSSubhransu S. Prusty 
145618382eadSSubhransu S. Prusty 		switch (type) {
145718382eadSSubhransu S. Prusty 
145818382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
145915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
146015b91447SSubhransu S. Prusty 			if (ret < 0)
146115b91447SSubhransu S. Prusty 				return ret;
146218382eadSSubhransu S. Prusty 			break;
146318382eadSSubhransu S. Prusty 
146418382eadSSubhransu S. Prusty 		case AC_WID_PIN:
146515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
146615b91447SSubhransu S. Prusty 			if (ret < 0)
146715b91447SSubhransu S. Prusty 				return ret;
146818382eadSSubhransu S. Prusty 			break;
146918382eadSSubhransu S. Prusty 		}
147018382eadSSubhransu S. Prusty 	}
147118382eadSSubhransu S. Prusty 
147218382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
147318382eadSSubhransu S. Prusty 
147415b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
147518382eadSSubhransu S. Prusty 		return -EIO;
147618382eadSSubhransu S. Prusty 
147717a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
147817a42c45SSubhransu S. Prusty 	if (ret) {
147917a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
148017a42c45SSubhransu S. Prusty 							ret);
148117a42c45SSubhransu S. Prusty 		return ret;
148217a42c45SSubhransu S. Prusty 	}
148317a42c45SSubhransu S. Prusty 
148417a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
148517a42c45SSubhransu S. Prusty 
148615b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
148718382eadSSubhransu S. Prusty }
148818382eadSSubhransu S. Prusty 
1489f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1490b8a54545SSubhransu S. Prusty {
1491b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1492b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1493754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1494754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1495b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
14962acd8309SJeeja KP 	int i;
1497b8a54545SSubhransu S. Prusty 
1498b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1499b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1500b8a54545SSubhransu S. Prusty 
1501754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1502754695f9SJeeja KP 							pin_nid, pipe);
1503b8a54545SSubhransu S. Prusty 
1504b8a54545SSubhransu S. Prusty 	/*
1505b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1506b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1507b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1508b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1509b8a54545SSubhransu S. Prusty 	 */
1510b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1511b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1512b8a54545SSubhransu S. Prusty 		return;
1513b8a54545SSubhransu S. Prusty 
1514b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1515b8a54545SSubhransu S. Prusty 		return;
1516b8a54545SSubhransu S. Prusty 
1517b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1518754695f9SJeeja KP 		if (pin->nid != pin_nid)
1519754695f9SJeeja KP 			continue;
1520754695f9SJeeja KP 
1521754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1522754695f9SJeeja KP 		if (pipe == -1) {
15232acd8309SJeeja KP 			pin->mst_capable = false;
1524754695f9SJeeja KP 			/* if not MST, default is port[0] */
1525754695f9SJeeja KP 			hport = &pin->ports[0];
15262acd8309SJeeja KP 			goto out;
15272acd8309SJeeja KP 		} else {
15282acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15292acd8309SJeeja KP 				pin->mst_capable = true;
15302acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15312acd8309SJeeja KP 					hport = &pin->ports[i];
15322acd8309SJeeja KP 					goto out;
15332acd8309SJeeja KP 				}
15342acd8309SJeeja KP 			}
1535b8a54545SSubhransu S. Prusty 		}
1536b8a54545SSubhransu S. Prusty 	}
1537b8a54545SSubhransu S. Prusty 
15382acd8309SJeeja KP out:
15392acd8309SJeeja KP 	if (pin && hport)
1540754695f9SJeeja KP 		hdac_hdmi_present_sense(pin, hport);
1541754695f9SJeeja KP }
1542754695f9SJeeja KP 
1543b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1544b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1545b8a54545SSubhransu S. Prusty };
1546b8a54545SSubhransu S. Prusty 
15472889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15482889099eSSubhransu S. Prusty 						int device)
15492889099eSSubhransu S. Prusty {
15502889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15512889099eSSubhransu S. Prusty 
15522889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15532889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15542889099eSSubhransu S. Prusty 			return rtd->pcm;
15552889099eSSubhransu S. Prusty 	}
15562889099eSSubhransu S. Prusty 
15572889099eSSubhransu S. Prusty 	return NULL;
15582889099eSSubhransu S. Prusty }
15592889099eSSubhransu S. Prusty 
1560*62490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1561*62490016SJeeja KP 				struct snd_soc_jack *jack)
15624a3478deSJeeja KP {
15634a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
15644a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
15654a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15664a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
15672889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
15682889099eSSubhransu S. Prusty 	int err;
15694a3478deSJeeja KP 
15704a3478deSJeeja KP 	/*
15714a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
15724a3478deSJeeja KP 	 * add to the pcm list
15734a3478deSJeeja KP 	 */
15744a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
15754a3478deSJeeja KP 	if (!pcm)
15764a3478deSJeeja KP 		return -ENOMEM;
15774a3478deSJeeja KP 	pcm->pcm_id = device;
15784a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1579e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
1580*62490016SJeeja KP 	pcm->jack = jack;
1581ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1582e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
15832889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
15842889099eSSubhransu S. Prusty 	if (snd_pcm) {
15852889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
15862889099eSSubhransu S. Prusty 		if (err < 0) {
15872889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
15882889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
15892889099eSSubhransu S. Prusty 				err, device);
15902889099eSSubhransu S. Prusty 			kfree(pcm);
15912889099eSSubhransu S. Prusty 			return err;
15922889099eSSubhransu S. Prusty 		}
15932889099eSSubhransu S. Prusty 	}
15942889099eSSubhransu S. Prusty 
15954a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
15964a3478deSJeeja KP 
1597*62490016SJeeja KP 	return 0;
15984a3478deSJeeja KP }
15994a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
16004a3478deSJeeja KP 
1601a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1602a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1603a9ce96bcSJeeja KP {
1604a9ce96bcSJeeja KP 	int i;
1605a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1606a9ce96bcSJeeja KP 
1607a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1608a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1609a9ce96bcSJeeja KP 
1610a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1611a9ce96bcSJeeja KP 				pin->mst_capable = false;
1612a9ce96bcSJeeja KP 			else
1613a9ce96bcSJeeja KP 				pin->mst_capable = true;
1614a9ce96bcSJeeja KP 		}
1615a9ce96bcSJeeja KP 
1616a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1617a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1618a9ce96bcSJeeja KP 				continue;
1619a9ce96bcSJeeja KP 
1620a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1621a9ce96bcSJeeja KP 		}
1622a9ce96bcSJeeja KP 	}
1623a9ce96bcSJeeja KP }
1624a9ce96bcSJeeja KP 
162518382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
162618382eadSSubhransu S. Prusty {
162718382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
162818382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
162918382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
163018382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1631b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1632a9ce96bcSJeeja KP 	int ret;
163318382eadSSubhransu S. Prusty 
163418382eadSSubhransu S. Prusty 	edev->scodec = codec;
163518382eadSSubhransu S. Prusty 
1636b2047e99SVinod Koul 	/*
1637b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1638b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1639b2047e99SVinod Koul 	 */
1640b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1641500e06b9SVinod Koul 	if (!hlink) {
1642500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1643500e06b9SVinod Koul 		return -EIO;
1644500e06b9SVinod Koul 	}
1645500e06b9SVinod Koul 
1646b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1647b2047e99SVinod Koul 
164879f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
164979f4e922SSubhransu S. Prusty 	if (ret < 0)
165079f4e922SSubhransu S. Prusty 		return ret;
165118382eadSSubhransu S. Prusty 
1652b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1653b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1654b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1655b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1656b8a54545SSubhransu S. Prusty 				ret);
1657b8a54545SSubhransu S. Prusty 		return ret;
1658b8a54545SSubhransu S. Prusty 	}
1659b8a54545SSubhransu S. Prusty 
1660a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
166118382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
166218382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
166318382eadSSubhransu S. Prusty 
1664e342ac08SSubhransu S. Prusty 	/*
1665e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1666e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1667e342ac08SSubhransu S. Prusty 	 */
1668e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1669e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1670e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1671e342ac08SSubhransu S. Prusty 
1672e342ac08SSubhransu S. Prusty 	return 0;
1673e342ac08SSubhransu S. Prusty }
1674e342ac08SSubhransu S. Prusty 
1675e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1676e342ac08SSubhransu S. Prusty {
1677e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1678e342ac08SSubhransu S. Prusty 
1679e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
168018382eadSSubhransu S. Prusty 	return 0;
168118382eadSSubhransu S. Prusty }
168218382eadSSubhransu S. Prusty 
1683571d5078SJeeja KP #ifdef CONFIG_PM
16841b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
16851b377ccdSSubhransu S. Prusty {
16861b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
16871b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
16881b377ccdSSubhransu S. Prusty 
16891b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
16901b377ccdSSubhransu S. Prusty 
16911b377ccdSSubhransu S. Prusty 	/*
16921b377ccdSSubhransu S. Prusty 	 * Power down afg.
16931b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
16941b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
16951b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
16961b377ccdSSubhransu S. Prusty 	 * to read the state.
16971b377ccdSSubhransu S. Prusty 	 */
16981b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
16991b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
17001b377ccdSSubhransu S. Prusty 
17011b377ccdSSubhransu S. Prusty 	return 0;
17021b377ccdSSubhransu S. Prusty }
17031b377ccdSSubhransu S. Prusty 
17040fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1705571d5078SJeeja KP {
17060fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1707571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1708571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
17091b377ccdSSubhransu S. Prusty 
17101b377ccdSSubhransu S. Prusty 	/* Power up afg */
17111b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17121b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1713571d5078SJeeja KP 
1714571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1715571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1716571d5078SJeeja KP 
1717571d5078SJeeja KP 	/*
1718571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1719571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1720a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1721a9ce96bcSJeeja KP 	 * already set pin caps.
1722571d5078SJeeja KP 	 */
1723a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1724571d5078SJeeja KP 
17251b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1726571d5078SJeeja KP }
1727571d5078SJeeja KP #else
17281b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
17290fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1730571d5078SJeeja KP #endif
1731571d5078SJeeja KP 
173218382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
173318382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1734e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
173518382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
173618382eadSSubhransu S. Prusty };
173718382eadSSubhransu S. Prusty 
17382889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
17392889099eSSubhransu S. Prusty 					unsigned char *chmap)
17402889099eSSubhransu S. Prusty {
17412889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17422889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17432889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
17442889099eSSubhransu S. Prusty 
1745ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
17462889099eSSubhransu S. Prusty }
17472889099eSSubhransu S. Prusty 
17482889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
17492889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
17502889099eSSubhransu S. Prusty {
17512889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17522889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17532889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1754e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1755e0e5d3e5SJeeja KP 
1756e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1757e0e5d3e5SJeeja KP 		return;
17582889099eSSubhransu S. Prusty 
1759ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1760ab1eea19SJeeja KP 	pcm->chmap_set = true;
1761ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1762e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
17632889099eSSubhransu S. Prusty 		if (prepared)
1764754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1765ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
17662889099eSSubhransu S. Prusty }
17672889099eSSubhransu S. Prusty 
17682889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
17692889099eSSubhransu S. Prusty {
17702889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17712889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17722889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
17732889099eSSubhransu S. Prusty 
1774e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1775e0e5d3e5SJeeja KP 		return false;
1776e0e5d3e5SJeeja KP 
1777e0e5d3e5SJeeja KP 	return true;
17782889099eSSubhransu S. Prusty }
17792889099eSSubhransu S. Prusty 
17802889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
17812889099eSSubhransu S. Prusty {
17822889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17832889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17842889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1785e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1786e0e5d3e5SJeeja KP 
1787e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1788e0e5d3e5SJeeja KP 		return 0;
1789e0e5d3e5SJeeja KP 
1790e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1791e0e5d3e5SJeeja KP 
1792e0e5d3e5SJeeja KP 	if (!port)
1793e0e5d3e5SJeeja KP 		return 0;
17942889099eSSubhransu S. Prusty 
1795754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
17962889099eSSubhransu S. Prusty 		return 0;
17972889099eSSubhransu S. Prusty 
1798754695f9SJeeja KP 	return port->eld.info.spk_alloc;
17992889099eSSubhransu S. Prusty }
18002889099eSSubhransu S. Prusty 
180118382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
180218382eadSSubhransu S. Prusty {
180318382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
180418382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
180517a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1806b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
180717a42c45SSubhransu S. Prusty 	int num_dais = 0;
180818382eadSSubhransu S. Prusty 	int ret = 0;
180918382eadSSubhransu S. Prusty 
1810b2047e99SVinod Koul 	/* hold the ref while we probe */
1811b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1812500e06b9SVinod Koul 	if (!hlink) {
1813500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1814500e06b9SVinod Koul 		return -EIO;
1815500e06b9SVinod Koul 	}
1816500e06b9SVinod Koul 
1817b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1818b2047e99SVinod Koul 
181918382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
182018382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
182118382eadSSubhransu S. Prusty 		return -ENOMEM;
182218382eadSSubhransu S. Prusty 
182318382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1824bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
18252889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
18262889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
18272889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
18282889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
182918382eadSSubhransu S. Prusty 
183018382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
183118382eadSSubhransu S. Prusty 
183215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
183315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
18344a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
18354a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
183615b91447SSubhransu S. Prusty 
1837aeaccef0SRamesh Babu 	/*
1838aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1839aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1840aeaccef0SRamesh Babu 	 */
1841aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1842aeaccef0SRamesh Babu 	if (ret < 0) {
1843aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1844aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1845aeaccef0SRamesh Babu 			ret);
1846aeaccef0SRamesh Babu 		return ret;
1847aeaccef0SRamesh Babu 	}
1848aeaccef0SRamesh Babu 
184917a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
185017a42c45SSubhransu S. Prusty 	if (ret < 0) {
185117a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
185217a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
185318382eadSSubhransu S. Prusty 		return ret;
185417a42c45SSubhransu S. Prusty 	}
185518382eadSSubhransu S. Prusty 
185618382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1857b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
185817a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1859b2047e99SVinod Koul 
1860b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1861b2047e99SVinod Koul 
1862b2047e99SVinod Koul 	return ret;
186318382eadSSubhransu S. Prusty }
186418382eadSSubhransu S. Prusty 
186518382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
186618382eadSSubhransu S. Prusty {
186715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
186815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
186915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
18704a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
1871e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1872754695f9SJeeja KP 	int i;
187315b91447SSubhransu S. Prusty 
187418382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
187518382eadSSubhransu S. Prusty 
18764a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
18774a3478deSJeeja KP 		pcm->cvt = NULL;
1878e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
1879e0e5d3e5SJeeja KP 			continue;
1880e0e5d3e5SJeeja KP 
1881e0e5d3e5SJeeja KP 		list_for_each_entry(port, &pcm->port_list, head)
1882e0e5d3e5SJeeja KP 			port = NULL;
1883e0e5d3e5SJeeja KP 
18844a3478deSJeeja KP 		list_del(&pcm->head);
18854a3478deSJeeja KP 		kfree(pcm);
18864a3478deSJeeja KP 	}
18874a3478deSJeeja KP 
188815b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
188915b91447SSubhransu S. Prusty 		list_del(&cvt->head);
18904a3478deSJeeja KP 		kfree(cvt->name);
189115b91447SSubhransu S. Prusty 		kfree(cvt);
189215b91447SSubhransu S. Prusty 	}
189315b91447SSubhransu S. Prusty 
189415b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1895754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1896754695f9SJeeja KP 			pin->ports[i].pin = NULL;
1897754695f9SJeeja KP 		kfree(pin->ports);
189815b91447SSubhransu S. Prusty 		list_del(&pin->head);
189915b91447SSubhransu S. Prusty 		kfree(pin);
190015b91447SSubhransu S. Prusty 	}
190115b91447SSubhransu S. Prusty 
190218382eadSSubhransu S. Prusty 	return 0;
190318382eadSSubhransu S. Prusty }
190418382eadSSubhransu S. Prusty 
1905e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1906e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1907e342ac08SSubhransu S. Prusty {
1908e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1909e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
191007f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1911b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1912b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
191307f083abSSubhransu S. Prusty 	int err;
1914e342ac08SSubhransu S. Prusty 
1915e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1916e342ac08SSubhransu S. Prusty 
191707f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
191807f083abSSubhransu S. Prusty 	if (!bus)
191907f083abSSubhransu S. Prusty 		return 0;
192007f083abSSubhransu S. Prusty 
19211b377ccdSSubhransu S. Prusty 	/*
19221b377ccdSSubhransu S. Prusty 	 * Power down afg.
19231b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
19241b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
19251b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
19261b377ccdSSubhransu S. Prusty 	 * to read the state.
19271b377ccdSSubhransu S. Prusty 	 */
19281b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
19291b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
193007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
193107f083abSSubhransu S. Prusty 	if (err < 0) {
193207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
193307f083abSSubhransu S. Prusty 		return err;
193407f083abSSubhransu S. Prusty 	}
193507f083abSSubhransu S. Prusty 
1936b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1937500e06b9SVinod Koul 	if (!hlink) {
1938500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1939500e06b9SVinod Koul 		return -EIO;
1940500e06b9SVinod Koul 	}
1941500e06b9SVinod Koul 
1942b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1943b2047e99SVinod Koul 
1944e342ac08SSubhransu S. Prusty 	return 0;
1945e342ac08SSubhransu S. Prusty }
1946e342ac08SSubhransu S. Prusty 
1947e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1948e342ac08SSubhransu S. Prusty {
1949e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1950e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
195107f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1952b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1953b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
195407f083abSSubhransu S. Prusty 	int err;
1955e342ac08SSubhransu S. Prusty 
1956e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1957e342ac08SSubhransu S. Prusty 
195807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
195907f083abSSubhransu S. Prusty 	if (!bus)
196007f083abSSubhransu S. Prusty 		return 0;
196107f083abSSubhransu S. Prusty 
1962b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1963500e06b9SVinod Koul 	if (!hlink) {
1964500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1965500e06b9SVinod Koul 		return -EIO;
1966500e06b9SVinod Koul 	}
1967500e06b9SVinod Koul 
1968b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1969b2047e99SVinod Koul 
197007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
197107f083abSSubhransu S. Prusty 	if (err < 0) {
197207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
197307f083abSSubhransu S. Prusty 		return err;
197407f083abSSubhransu S. Prusty 	}
197507f083abSSubhransu S. Prusty 
1976ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1977ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1978ab85f5b3SSubhransu S. Prusty 
1979e342ac08SSubhransu S. Prusty 	/* Power up afg */
19801b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
19811b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1982e342ac08SSubhransu S. Prusty 
1983e342ac08SSubhransu S. Prusty 	return 0;
1984e342ac08SSubhransu S. Prusty }
1985e342ac08SSubhransu S. Prusty #else
1986e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1987e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1988e342ac08SSubhransu S. Prusty #endif
1989e342ac08SSubhransu S. Prusty 
1990e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1991e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
19921b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
19930fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1994e342ac08SSubhransu S. Prusty };
1995e342ac08SSubhransu S. Prusty 
199618382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
199718382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1998e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1999cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
200018382eadSSubhransu S. Prusty 	{}
200118382eadSSubhransu S. Prusty };
200218382eadSSubhransu S. Prusty 
200318382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
200418382eadSSubhransu S. Prusty 
200518382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
200618382eadSSubhransu S. Prusty 	. hdac = {
200718382eadSSubhransu S. Prusty 		.driver = {
200818382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2009e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
201018382eadSSubhransu S. Prusty 		},
201118382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
201218382eadSSubhransu S. Prusty 	},
201318382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
201418382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
201518382eadSSubhransu S. Prusty };
201618382eadSSubhransu S. Prusty 
201718382eadSSubhransu S. Prusty static int __init hdmi_init(void)
201818382eadSSubhransu S. Prusty {
201918382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
202018382eadSSubhransu S. Prusty }
202118382eadSSubhransu S. Prusty 
202218382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
202318382eadSSubhransu S. Prusty {
202418382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
202518382eadSSubhransu S. Prusty }
202618382eadSSubhransu S. Prusty 
202718382eadSSubhransu S. Prusty module_init(hdmi_init);
202818382eadSSubhransu S. Prusty module_exit(hdmi_exit);
202918382eadSSubhransu S. Prusty 
203018382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
203118382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
203218382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
203318382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2034