xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 2acd8309)
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 {
92754695f9SJeeja KP 	int id;
93754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9418382eadSSubhransu S. Prusty 	int num_mux_nids;
9518382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
96b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
9718382eadSSubhransu S. Prusty };
9818382eadSSubhransu S. Prusty 
994a3478deSJeeja KP struct hdac_hdmi_pcm {
1004a3478deSJeeja KP 	struct list_head head;
1014a3478deSJeeja KP 	int pcm_id;
102754695f9SJeeja KP 	struct hdac_hdmi_port *port;
1034a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
1044a3478deSJeeja KP 	struct snd_jack *jack;
105c9bfb5d7SJeeja KP 	int stream_tag;
106c9bfb5d7SJeeja KP 	int channels;
107c9bfb5d7SJeeja KP 	int format;
108ab1eea19SJeeja KP 	bool chmap_set;
109ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
110ab1eea19SJeeja KP 	struct mutex lock;
1114a3478deSJeeja KP };
1124a3478deSJeeja KP 
113754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11418382eadSSubhransu S. Prusty 	int dai_id;
115754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11615b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11718382eadSSubhransu S. Prusty };
11818382eadSSubhransu S. Prusty 
11918382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
120754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12115b91447SSubhransu S. Prusty 	struct list_head pin_list;
12215b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1234a3478deSJeeja KP 	struct list_head pcm_list;
12415b91447SSubhransu S. Prusty 	int num_pin;
12515b91447SSubhransu S. Prusty 	int num_cvt;
126754695f9SJeeja KP 	int num_ports;
1274a3478deSJeeja KP 	struct mutex pin_mutex;
128bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
12918382eadSSubhransu S. Prusty };
13018382eadSSubhransu S. Prusty 
131c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
132c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
133c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
134c9bfb5d7SJeeja KP {
135c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1361de777feSJeeja KP 
137c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
138c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
139c9bfb5d7SJeeja KP 			break;
140c9bfb5d7SJeeja KP 	}
141c9bfb5d7SJeeja KP 
142c9bfb5d7SJeeja KP 	return pcm;
143c9bfb5d7SJeeja KP }
1441de777feSJeeja KP 
1452889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
1462889099eSSubhransu S. Prusty 						int pcm_idx)
1472889099eSSubhransu S. Prusty {
1482889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
1492889099eSSubhransu S. Prusty 
1502889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
1512889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
1522889099eSSubhransu S. Prusty 			return pcm;
1532889099eSSubhransu S. Prusty 	}
1542889099eSSubhransu S. Prusty 
1552889099eSSubhransu S. Prusty 	return NULL;
1562889099eSSubhransu S. Prusty }
1572889099eSSubhransu S. Prusty 
158e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
159e342ac08SSubhransu S. Prusty {
16051b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
161e342ac08SSubhransu S. Prusty 
16251b2c425SGeliang Tang 	return to_ehdac_device(hdac);
163e342ac08SSubhransu S. Prusty }
164e342ac08SSubhransu S. Prusty 
1652428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1662428bca3SSubhransu S. Prusty {
1672428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1682428bca3SSubhransu S. Prusty }
1692428bca3SSubhransu S. Prusty 
1702428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1712428bca3SSubhransu S. Prusty {
1722428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1732428bca3SSubhransu S. Prusty }
1742428bca3SSubhransu S. Prusty 
1752428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1762428bca3SSubhransu S. Prusty 						void *eld)
1772428bca3SSubhransu S. Prusty {
1782428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1792428bca3SSubhransu S. Prusty 	int i;
1802428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1812428bca3SSubhransu S. Prusty 
1822428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1832428bca3SSubhransu S. Prusty 	if (!sad)
1842428bca3SSubhransu S. Prusty 		goto format_constraint;
1852428bca3SSubhransu S. Prusty 
1862428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1872428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1882428bca3SSubhransu S. Prusty 
1892428bca3SSubhransu S. Prusty 			/*
1902428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1912428bca3SSubhransu S. Prusty 			 * container so we set S32
1922428bca3SSubhransu S. Prusty 			 */
1932428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1942428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1952428bca3SSubhransu S. Prusty 		}
1962428bca3SSubhransu S. Prusty 	}
1972428bca3SSubhransu S. Prusty 
1982428bca3SSubhransu S. Prusty format_constraint:
1992428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
2002428bca3SSubhransu S. Prusty 				formats);
2012428bca3SSubhransu S. Prusty 
2022428bca3SSubhransu S. Prusty }
2032428bca3SSubhransu S. Prusty 
204a657f1d0SSubhransu S. Prusty static void
205a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
206a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
207a657f1d0SSubhransu S. Prusty {
208a657f1d0SSubhransu S. Prusty 	int val;
209a657f1d0SSubhransu S. Prusty 
210a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
211a657f1d0SSubhransu S. Prusty 
212a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
213a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
214a657f1d0SSubhransu S. Prusty }
215a657f1d0SSubhransu S. Prusty 
216478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
217478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
218478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
219478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
220478f544eSSubhransu S. Prusty 
221478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
222478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
223478f544eSSubhransu S. Prusty 	u8 CXT04;
224478f544eSSubhransu S. Prusty 	u8 CA;
225478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
226478f544eSSubhransu S. Prusty };
227478f544eSSubhransu S. Prusty 
228a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
229754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
230a657f1d0SSubhransu S. Prusty {
231a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
232a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
233754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
234478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
235478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
236ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
237478f544eSSubhransu S. Prusty 	u8 *dip;
238a657f1d0SSubhransu S. Prusty 	int ret;
239a657f1d0SSubhransu S. Prusty 	int i;
240478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
241478f544eSSubhransu S. Prusty 	u8 conn_type;
242bcced704SSubhransu S. Prusty 	int channels, ca;
243a657f1d0SSubhransu S. Prusty 
244754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
245ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
246bcced704SSubhransu S. Prusty 
247bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
248ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
249bcced704SSubhransu S. Prusty 
250bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
251ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
252bcced704SSubhransu S. Prusty 
253754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
254478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
255a657f1d0SSubhransu S. Prusty 
256478f544eSSubhransu S. Prusty 	switch (conn_type) {
257478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
258478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
259478f544eSSubhransu S. Prusty 
260478f544eSSubhransu S. Prusty 		frame.channels = channels;
261bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
262a657f1d0SSubhransu S. Prusty 
263a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
264a657f1d0SSubhransu S. Prusty 		if (ret < 0)
265a657f1d0SSubhransu S. Prusty 			return ret;
266a657f1d0SSubhransu S. Prusty 
267478f544eSSubhransu S. Prusty 		break;
268478f544eSSubhransu S. Prusty 
269478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
270478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
271478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
272478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
273478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
274478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
275bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
276478f544eSSubhransu S. Prusty 
277478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
278478f544eSSubhransu S. Prusty 		break;
279478f544eSSubhransu S. Prusty 
280478f544eSSubhransu S. Prusty 	default:
281478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
282478f544eSSubhransu S. Prusty 						conn_type);
283478f544eSSubhransu S. Prusty 		return -EIO;
284478f544eSSubhransu S. Prusty 	}
285478f544eSSubhransu S. Prusty 
286a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
287ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
288ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
289a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
290a657f1d0SSubhransu S. Prusty 
291a657f1d0SSubhransu S. Prusty 
292a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
293ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
294478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
295391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
296ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
297391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
298478f544eSSubhransu S. Prusty 	} else {
299478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
300ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
301478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
302478f544eSSubhransu S. Prusty 	}
303a657f1d0SSubhransu S. Prusty 
304a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
305ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
306ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
307a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
308a657f1d0SSubhransu S. Prusty 
309a657f1d0SSubhransu S. Prusty 	return 0;
310a657f1d0SSubhransu S. Prusty }
311a657f1d0SSubhransu S. Prusty 
312c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
313c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
314c9bfb5d7SJeeja KP 		int slots, int slot_width)
315b0362adbSSubhransu S. Prusty {
316c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
317c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
318754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
319c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
320c9bfb5d7SJeeja KP 
321c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
322b0362adbSSubhransu S. Prusty 
323b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
324b0362adbSSubhransu S. Prusty 
325c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
326b0362adbSSubhransu S. Prusty 
327c9bfb5d7SJeeja KP 	if (pcm)
328c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
329bcced704SSubhransu S. Prusty 
330c9bfb5d7SJeeja KP 	return 0;
331b0362adbSSubhransu S. Prusty }
332b0362adbSSubhransu S. Prusty 
333b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
334b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
335b0362adbSSubhransu S. Prusty {
336b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
33754dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
338754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
339754695f9SJeeja KP 	struct hdac_hdmi_port *port;
340c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
341c9bfb5d7SJeeja KP 	int format;
342b0362adbSSubhransu S. Prusty 
34354dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
344754695f9SJeeja KP 	port = dai_map->port;
34554dfa1eaSSubhransu S. Prusty 
346754695f9SJeeja KP 	if (!port)
34754dfa1eaSSubhransu S. Prusty 		return -ENODEV;
34854dfa1eaSSubhransu S. Prusty 
349754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
350754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
351754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
352754695f9SJeeja KP 					port->pin->nid, port->id);
353b0362adbSSubhransu S. Prusty 		return -ENODEV;
354b0362adbSSubhransu S. Prusty 	}
355b0362adbSSubhransu S. Prusty 
356c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
357b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
358b0362adbSSubhransu S. Prusty 			24, 0);
359b0362adbSSubhransu S. Prusty 
360c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
361c9bfb5d7SJeeja KP 	if (!pcm)
362148569fdSSubhransu S. Prusty 		return -EIO;
363148569fdSSubhransu S. Prusty 
364c9bfb5d7SJeeja KP 	pcm->format = format;
365c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
366148569fdSSubhransu S. Prusty 
367148569fdSSubhransu S. Prusty 	return 0;
368148569fdSSubhransu S. Prusty }
369148569fdSSubhransu S. Prusty 
370754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
371754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
372754695f9SJeeja KP 					struct hdac_hdmi_port *port)
373148569fdSSubhransu S. Prusty {
374148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
375148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
376148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
377148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
378148569fdSSubhransu S. Prusty 		return -EINVAL;
379148569fdSSubhransu S. Prusty 	}
380148569fdSSubhransu S. Prusty 
381754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
382754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
383754695f9SJeeja KP 	if (port->num_mux_nids == 0)
384754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
385754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
386754695f9SJeeja KP 						pin->nid, port->id);
387148569fdSSubhransu S. Prusty 
388754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
389754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
390148569fdSSubhransu S. Prusty 
391754695f9SJeeja KP 	return port->num_mux_nids;
392148569fdSSubhransu S. Prusty }
393148569fdSSubhransu S. Prusty 
394148569fdSSubhransu S. Prusty /*
395754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
396148569fdSSubhransu S. Prusty  *
397754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
398148569fdSSubhransu S. Prusty  *
399754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
400754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
401148569fdSSubhransu S. Prusty  * connected.
402148569fdSSubhransu S. Prusty  */
403754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
404148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
405148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
406148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
407148569fdSSubhransu S. Prusty {
408148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
409754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
410148569fdSSubhransu S. Prusty 	int ret, i;
411148569fdSSubhransu S. Prusty 
412148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
413148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
414754695f9SJeeja KP 			port = pcm->port;
415148569fdSSubhransu S. Prusty 			break;
416148569fdSSubhransu S. Prusty 		}
417148569fdSSubhransu S. Prusty 	}
418148569fdSSubhransu S. Prusty 
419754695f9SJeeja KP 	if (port) {
420754695f9SJeeja KP 		ret = hdac_hdmi_query_port_connlist(edev, port->pin, port);
421148569fdSSubhransu S. Prusty 		if (ret < 0)
422148569fdSSubhransu S. Prusty 			return NULL;
423148569fdSSubhransu S. Prusty 
424754695f9SJeeja KP 		for (i = 0; i < port->num_mux_nids; i++) {
425754695f9SJeeja KP 			if (port->mux_nids[i] == cvt->nid)
426754695f9SJeeja KP 				return port;
427148569fdSSubhransu S. Prusty 		}
428148569fdSSubhransu S. Prusty 	}
429148569fdSSubhransu S. Prusty 
430148569fdSSubhransu S. Prusty 	return NULL;
431148569fdSSubhransu S. Prusty }
432148569fdSSubhransu S. Prusty 
43354dfa1eaSSubhransu S. Prusty /*
43454dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
43554dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
43654dfa1eaSSubhransu S. Prusty  * doesn't fail.
43754dfa1eaSSubhransu S. Prusty  */
438b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
439b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
440b0362adbSSubhransu S. Prusty {
441b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
442b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
443754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
444148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
445754695f9SJeeja KP 	struct hdac_hdmi_port *port;
4462428bca3SSubhransu S. Prusty 	int ret;
447b0362adbSSubhransu S. Prusty 
448b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
449b0362adbSSubhransu S. Prusty 
450148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
451754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
45254dfa1eaSSubhransu S. Prusty 
45354dfa1eaSSubhransu S. Prusty 	/*
45454dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
45554dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
45654dfa1eaSSubhransu S. Prusty 	 */
457754695f9SJeeja KP 	if (!port)
45854dfa1eaSSubhransu S. Prusty 		return 0;
459148569fdSSubhransu S. Prusty 
460754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
461754695f9SJeeja KP 			(!port->eld.eld_valid)) {
462b0362adbSSubhransu S. Prusty 
46354dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
464754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
465754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
466754695f9SJeeja KP 			port->pin->nid, port->id);
467b8a54545SSubhransu S. Prusty 
46854dfa1eaSSubhransu S. Prusty 		return 0;
469b0362adbSSubhransu S. Prusty 	}
470b0362adbSSubhransu S. Prusty 
471754695f9SJeeja KP 	dai_map->port = port;
472b0362adbSSubhransu S. Prusty 
4732428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
474754695f9SJeeja KP 				port->eld.eld_buffer);
4752428bca3SSubhransu S. Prusty 	if (ret < 0)
4762428bca3SSubhransu S. Prusty 		return ret;
477b0362adbSSubhransu S. Prusty 
4782428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
479754695f9SJeeja KP 				port->eld.eld_buffer);
480b0362adbSSubhransu S. Prusty }
481b0362adbSSubhransu S. Prusty 
482b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
483b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
484b0362adbSSubhransu S. Prusty {
485b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
486b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
487754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
488ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
489b0362adbSSubhransu S. Prusty 
490b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
491b0362adbSSubhransu S. Prusty 
492ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
493bcced704SSubhransu S. Prusty 
494ab1eea19SJeeja KP 	if (pcm) {
495ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
496ab1eea19SJeeja KP 		pcm->chmap_set = false;
497ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
498ab1eea19SJeeja KP 		pcm->channels = 0;
499ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
500b0362adbSSubhransu S. Prusty 	}
501ab1eea19SJeeja KP 
502754695f9SJeeja KP 	if (dai_map->port)
503754695f9SJeeja KP 		dai_map->port = NULL;
50454dfa1eaSSubhransu S. Prusty }
505b0362adbSSubhransu S. Prusty 
50618382eadSSubhransu S. Prusty static int
50718382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
50818382eadSSubhransu S. Prusty {
509bcced704SSubhransu S. Prusty 	unsigned int chans;
510bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
511bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
51218382eadSSubhransu S. Prusty 	int err;
51318382eadSSubhransu S. Prusty 
514bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
515bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
516bcced704SSubhransu S. Prusty 
517bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
518bcced704SSubhransu S. Prusty 
519bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
520bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
521bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
52218382eadSSubhransu S. Prusty 
52318382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
52418382eadSSubhransu S. Prusty 			&cvt->params.rates,
52518382eadSSubhransu S. Prusty 			&cvt->params.formats,
52618382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
52718382eadSSubhransu S. Prusty 	if (err < 0)
52818382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
52918382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
53018382eadSSubhransu S. Prusty 			cvt->nid, err);
53118382eadSSubhransu S. Prusty 
53218382eadSSubhransu S. Prusty 	return err;
53318382eadSSubhransu S. Prusty }
53418382eadSSubhransu S. Prusty 
53579f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
536c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
537c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
538c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
539c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
540c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
54118382eadSSubhransu S. Prusty {
54218382eadSSubhransu S. Prusty 	w->id = id;
54379f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
54479f4e922SSubhransu S. Prusty 	if (!w->name)
54579f4e922SSubhransu S. Prusty 		return -ENOMEM;
54679f4e922SSubhransu S. Prusty 
54718382eadSSubhransu S. Prusty 	w->sname = stream;
54818382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
54918382eadSSubhransu S. Prusty 	w->shift = 0;
55079f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
55179f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
55279f4e922SSubhransu S. Prusty 	w->priv = priv;
553c9bfb5d7SJeeja KP 	w->event = event;
554c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
55579f4e922SSubhransu S. Prusty 
55679f4e922SSubhransu S. Prusty 	return 0;
55718382eadSSubhransu S. Prusty }
55818382eadSSubhransu S. Prusty 
55918382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
56079f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
56179f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
56279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
56318382eadSSubhransu S. Prusty {
56418382eadSSubhransu S. Prusty 	route->sink = sink;
56518382eadSSubhransu S. Prusty 	route->source = src;
56618382eadSSubhransu S. Prusty 	route->control = control;
56779f4e922SSubhransu S. Prusty 	route->connected = handler;
56818382eadSSubhransu S. Prusty }
56918382eadSSubhransu S. Prusty 
5704a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
571754695f9SJeeja KP 					struct hdac_hdmi_port *port)
5724a3478deSJeeja KP {
5734a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
5744a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
5754a3478deSJeeja KP 
5764a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
577754695f9SJeeja KP 		if (!pcm->port)
578754695f9SJeeja KP 			continue;
579754695f9SJeeja KP 
580754695f9SJeeja KP 		if (pcm->port == port)
5814a3478deSJeeja KP 			return pcm;
5824a3478deSJeeja KP 	}
5834a3478deSJeeja KP 
5844a3478deSJeeja KP 	return NULL;
5854a3478deSJeeja KP }
5864a3478deSJeeja KP 
587c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
588c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
589c9bfb5d7SJeeja KP {
590c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
591c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
592c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
593c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
594c9bfb5d7SJeeja KP 	}
595c9bfb5d7SJeeja KP }
596c9bfb5d7SJeeja KP 
597c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
598c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
599c9bfb5d7SJeeja KP {
600c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
601c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
602c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
603c9bfb5d7SJeeja KP }
604c9bfb5d7SJeeja KP 
605c9bfb5d7SJeeja KP 
606c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
607c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
608c9bfb5d7SJeeja KP {
609754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
610c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
611c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
612c9bfb5d7SJeeja KP 
613c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
614c9bfb5d7SJeeja KP 			__func__, w->name, event);
615c9bfb5d7SJeeja KP 
616754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
617c9bfb5d7SJeeja KP 	if (!pcm)
618c9bfb5d7SJeeja KP 		return -EIO;
619c9bfb5d7SJeeja KP 
620c9bfb5d7SJeeja KP 	switch (event) {
621c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
622754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
623c9bfb5d7SJeeja KP 
624c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
625754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
626c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
627c9bfb5d7SJeeja KP 
628754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
629c9bfb5d7SJeeja KP 
630754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
631c9bfb5d7SJeeja KP 
632c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
633754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
634c9bfb5d7SJeeja KP 
635c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
636754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
637c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
638c9bfb5d7SJeeja KP 
639754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
640c9bfb5d7SJeeja KP 		break;
641c9bfb5d7SJeeja KP 
642c9bfb5d7SJeeja KP 	}
643c9bfb5d7SJeeja KP 
644c9bfb5d7SJeeja KP 	return 0;
645c9bfb5d7SJeeja KP }
646c9bfb5d7SJeeja KP 
647c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
648c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
649c9bfb5d7SJeeja KP {
650c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
651c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
652c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
653c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
654c9bfb5d7SJeeja KP 
655c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
656c9bfb5d7SJeeja KP 			__func__, w->name, event);
657c9bfb5d7SJeeja KP 
658c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
659c9bfb5d7SJeeja KP 	if (!pcm)
660c9bfb5d7SJeeja KP 		return -EIO;
661c9bfb5d7SJeeja KP 
662c9bfb5d7SJeeja KP 	switch (event) {
663c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
664c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
665c9bfb5d7SJeeja KP 
666c9bfb5d7SJeeja KP 		/* Enable transmission */
667c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
668c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
669c9bfb5d7SJeeja KP 
670c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
671c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
672c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
673c9bfb5d7SJeeja KP 
674c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
675c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
676c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
677c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
678c9bfb5d7SJeeja KP 		break;
679c9bfb5d7SJeeja KP 
680c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
681c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
682c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
683c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
684c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
685c9bfb5d7SJeeja KP 
686c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
687c9bfb5d7SJeeja KP 		break;
688c9bfb5d7SJeeja KP 
689c9bfb5d7SJeeja KP 	}
690c9bfb5d7SJeeja KP 
691c9bfb5d7SJeeja KP 	return 0;
692c9bfb5d7SJeeja KP }
693c9bfb5d7SJeeja KP 
694c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
695c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
696c9bfb5d7SJeeja KP {
697754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
698c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
699c9bfb5d7SJeeja KP 	int mux_idx;
700c9bfb5d7SJeeja KP 
701c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
702c9bfb5d7SJeeja KP 			__func__, w->name, event);
703c9bfb5d7SJeeja KP 
704c9bfb5d7SJeeja KP 	if (!kc)
705c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
706c9bfb5d7SJeeja KP 
707c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
708c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
709754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
710c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
711c9bfb5d7SJeeja KP 	}
712c9bfb5d7SJeeja KP 
713c9bfb5d7SJeeja KP 	return 0;
714c9bfb5d7SJeeja KP }
715c9bfb5d7SJeeja KP 
7164a3478deSJeeja KP /*
7174a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7184a3478deSJeeja KP  */
719754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
7204a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7214a3478deSJeeja KP {
7224a3478deSJeeja KP 	int ret;
7234a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7244a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7254a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
726754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7274a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7284a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7294a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7304a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7314a3478deSJeeja KP 
7324a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7334a3478deSJeeja KP 	if (ret < 0)
7344a3478deSJeeja KP 		return ret;
7354a3478deSJeeja KP 
736754695f9SJeeja KP 	if (port == NULL)
737754695f9SJeeja KP 		return -EINVAL;
738754695f9SJeeja KP 
7394a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7404a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
741754695f9SJeeja KP 		if (!pcm->port && pcm->port == port &&
742754695f9SJeeja KP 			pcm->port->id == port->id)
743754695f9SJeeja KP 			pcm->port = NULL;
7444a3478deSJeeja KP 
7454a3478deSJeeja KP 		/*
7464a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7474a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7484a3478deSJeeja KP 		 */
749754695f9SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->port) {
750754695f9SJeeja KP 			pcm->port = port;
751754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
7524a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7534a3478deSJeeja KP 					"jack report for pcm=%d\n",
7544a3478deSJeeja KP 					pcm->pcm_id);
7554a3478deSJeeja KP 
7564a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
7574a3478deSJeeja KP 			}
7584a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
7594a3478deSJeeja KP 			return ret;
7604a3478deSJeeja KP 		}
7614a3478deSJeeja KP 	}
7624a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
7634a3478deSJeeja KP 
7644a3478deSJeeja KP 	return ret;
7654a3478deSJeeja KP }
7664a3478deSJeeja KP 
76779f4e922SSubhransu S. Prusty /*
76879f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
76979f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
77079f4e922SSubhransu S. Prusty  * widget runtime.
77179f4e922SSubhransu S. Prusty  *
77279f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
77379f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
77479f4e922SSubhransu S. Prusty  * "NONE"
77579f4e922SSubhransu S. Prusty  */
776754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
777754695f9SJeeja KP 				struct hdac_hdmi_port *port,
77879f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
77979f4e922SSubhransu S. Prusty 				const char *widget_name)
78018382eadSSubhransu S. Prusty {
78179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
782754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
78379f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
78479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
78579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
78679f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
78779f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
78879f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
78979f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
79079f4e922SSubhransu S. Prusty 	int i = 0;
79179f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
79218382eadSSubhransu S. Prusty 
79379f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
79479f4e922SSubhransu S. Prusty 	if (!kc)
79579f4e922SSubhransu S. Prusty 		return -ENOMEM;
79618382eadSSubhransu S. Prusty 
79779f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
79879f4e922SSubhransu S. Prusty 	if (!se)
79979f4e922SSubhransu S. Prusty 		return -ENOMEM;
80018382eadSSubhransu S. Prusty 
801754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
80279f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
80379f4e922SSubhransu S. Prusty 	if (!kc->name)
80479f4e922SSubhransu S. Prusty 		return -ENOMEM;
80518382eadSSubhransu S. Prusty 
80679f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
80779f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
80879f4e922SSubhransu S. Prusty 	kc->access = 0;
80979f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
810754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
81179f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
81279f4e922SSubhransu S. Prusty 
81379f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
81479f4e922SSubhransu S. Prusty 
81579f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
81679f4e922SSubhransu S. Prusty 	se->items = num_items;
81779f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
81879f4e922SSubhransu S. Prusty 
81979f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
82079f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
82179f4e922SSubhransu S. Prusty 	if (!items[i])
82279f4e922SSubhransu S. Prusty 		return -ENOMEM;
82379f4e922SSubhransu S. Prusty 
82479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
82579f4e922SSubhransu S. Prusty 		i++;
82679f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
82779f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
82879f4e922SSubhransu S. Prusty 		if (!items[i])
82979f4e922SSubhransu S. Prusty 			return -ENOMEM;
83079f4e922SSubhransu S. Prusty 	}
83179f4e922SSubhransu S. Prusty 
83279f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
83379f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
83479f4e922SSubhransu S. Prusty 	if (!se->texts)
83579f4e922SSubhransu S. Prusty 		return -ENOMEM;
83679f4e922SSubhransu S. Prusty 
83779f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
838754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
839c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
840c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
84179f4e922SSubhransu S. Prusty }
84279f4e922SSubhransu S. Prusty 
84379f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
84479f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
84579f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
84679f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
84779f4e922SSubhransu S. Prusty {
84879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
84979f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
85079f4e922SSubhransu S. Prusty 	struct soc_enum *se;
851754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
85279f4e922SSubhransu S. Prusty 	int i, j;
85379f4e922SSubhransu S. Prusty 
854754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
85579f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
85679f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
85779f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
85879f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
85979f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
86079f4e922SSubhransu S. Prusty 					se->texts[j + 1],
86179f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
86279f4e922SSubhransu S. Prusty 
86379f4e922SSubhransu S. Prusty 			rindex++;
86479f4e922SSubhransu S. Prusty 		}
86579f4e922SSubhransu S. Prusty 
86679f4e922SSubhransu S. Prusty 		mux_index++;
86779f4e922SSubhransu S. Prusty 	}
86879f4e922SSubhransu S. Prusty }
86979f4e922SSubhransu S. Prusty 
87079f4e922SSubhransu S. Prusty /*
87179f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
87279f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
873754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
874754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
87579f4e922SSubhransu S. Prusty  *
876754695f9SJeeja KP  * For each port, one Mux and One output widget is added
877754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
87879f4e922SSubhransu S. Prusty  *
87979f4e922SSubhransu S. Prusty  * Routes are added as below:
880754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
881754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
88279f4e922SSubhransu S. Prusty  *
88379f4e922SSubhransu S. Prusty  * Total route elements:
884754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
88579f4e922SSubhransu S. Prusty  */
88679f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
88779f4e922SSubhransu S. Prusty {
88879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
88979f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
89079f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
89179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
89279f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
89379f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
89479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
89579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
896754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
89779f4e922SSubhransu S. Prusty 
89879f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
89979f4e922SSubhransu S. Prusty 		return -EINVAL;
90079f4e922SSubhransu S. Prusty 
901754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
902754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
90379f4e922SSubhransu S. Prusty 				GFP_KERNEL);
90479f4e922SSubhransu S. Prusty 
90579f4e922SSubhransu S. Prusty 	if (!widgets)
90679f4e922SSubhransu S. Prusty 		return -ENOMEM;
90779f4e922SSubhransu S. Prusty 
90879f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
90979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
91079f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
91179f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
912c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
913c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
914c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
915c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
91679f4e922SSubhransu S. Prusty 		if (ret < 0)
91779f4e922SSubhransu S. Prusty 			return ret;
91879f4e922SSubhransu S. Prusty 		i++;
91979f4e922SSubhransu S. Prusty 	}
92079f4e922SSubhransu S. Prusty 
92179f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
922754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
923754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
924754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
92579f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
926754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
927c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
928c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
929754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
930754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
93179f4e922SSubhransu S. Prusty 			if (ret < 0)
93279f4e922SSubhransu S. Prusty 				return ret;
93379f4e922SSubhransu S. Prusty 			i++;
93479f4e922SSubhransu S. Prusty 		}
935754695f9SJeeja KP 	}
93679f4e922SSubhransu S. Prusty 
93779f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
93879f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
939754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
940754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
941754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
942754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
943754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
94479f4e922SSubhransu S. Prusty 						widget_name);
94579f4e922SSubhransu S. Prusty 			if (ret < 0)
94679f4e922SSubhransu S. Prusty 				return ret;
94779f4e922SSubhransu S. Prusty 			i++;
94879f4e922SSubhransu S. Prusty 
94979f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
95079f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
95179f4e922SSubhransu S. Prusty 
95279f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
95379f4e922SSubhransu S. Prusty 			num_routes++;
95479f4e922SSubhransu S. Prusty 		}
955754695f9SJeeja KP 	}
95679f4e922SSubhransu S. Prusty 
95779f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
95879f4e922SSubhransu S. Prusty 							GFP_KERNEL);
95979f4e922SSubhransu S. Prusty 	if (!route)
96079f4e922SSubhransu S. Prusty 		return -ENOMEM;
96179f4e922SSubhransu S. Prusty 
96279f4e922SSubhransu S. Prusty 	i = 0;
96379f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
96479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
965754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
96679f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
967754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
968754695f9SJeeja KP 						hdmi->num_pin;
96979f4e922SSubhransu S. Prusty 
97079f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
97179f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
97279f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
97379f4e922SSubhransu S. Prusty 			i++;
974754695f9SJeeja KP 		}
97579f4e922SSubhransu S. Prusty 	}
97679f4e922SSubhransu S. Prusty 
97779f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
97879f4e922SSubhransu S. Prusty 
97979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
980754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
98179f4e922SSubhransu S. Prusty 
98279f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
98379f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
98479f4e922SSubhransu S. Prusty 
98579f4e922SSubhransu S. Prusty 	return 0;
98679f4e922SSubhransu S. Prusty 
98718382eadSSubhransu S. Prusty }
98818382eadSSubhransu S. Prusty 
98915b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
99018382eadSSubhransu S. Prusty {
99115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
992754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
99315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
994148569fdSSubhransu S. Prusty 	int dai_id = 0;
99518382eadSSubhransu S. Prusty 
996148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
99715b91447SSubhransu S. Prusty 		return -EINVAL;
99818382eadSSubhransu S. Prusty 
999148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1000148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1001148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
100215b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
100318382eadSSubhransu S. Prusty 
1004148569fdSSubhransu S. Prusty 		dai_id++;
1005148569fdSSubhransu S. Prusty 
1006148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1007148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1008148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1009148569fdSSubhransu S. Prusty 			break;
1010148569fdSSubhransu S. Prusty 		}
1011148569fdSSubhransu S. Prusty 	}
101218382eadSSubhransu S. Prusty 
101315b91447SSubhransu S. Prusty 	return 0;
101415b91447SSubhransu S. Prusty }
101515b91447SSubhransu S. Prusty 
101615b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
101715b91447SSubhransu S. Prusty {
101815b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
101915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10204a3478deSJeeja KP 	char name[NAME_SIZE];
102115b91447SSubhransu S. Prusty 
102215b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
102315b91447SSubhransu S. Prusty 	if (!cvt)
102415b91447SSubhransu S. Prusty 		return -ENOMEM;
102515b91447SSubhransu S. Prusty 
102615b91447SSubhransu S. Prusty 	cvt->nid = nid;
10274a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10284a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
102915b91447SSubhransu S. Prusty 
103015b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
103115b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
103215b91447SSubhransu S. Prusty 
103315b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
103415b91447SSubhransu S. Prusty }
103515b91447SSubhransu S. Prusty 
1036f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1037754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1038b7756edeSSubhransu S. Prusty {
1039f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1040f6fa11a3SSandeep Tayal 
1041754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1042f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1043f6fa11a3SSandeep Tayal 
1044f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1045f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1046f6fa11a3SSandeep Tayal 		return -EINVAL;
1047b7756edeSSubhransu S. Prusty 	}
1048b7756edeSSubhransu S. Prusty 
1049754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1050f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1051f6fa11a3SSandeep Tayal 
1052f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1053f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1054f6fa11a3SSandeep Tayal 		return -EINVAL;
1055f6fa11a3SSandeep Tayal 	}
1056f6fa11a3SSandeep Tayal 
1057754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1058f6fa11a3SSandeep Tayal 
1059f6fa11a3SSandeep Tayal 	return 0;
1060f6fa11a3SSandeep Tayal }
1061f6fa11a3SSandeep Tayal 
1062754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1063754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1064b8a54545SSubhransu S. Prusty {
1065b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10664a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10674a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1068754695f9SJeeja KP 	int size = 0;
10692acd8309SJeeja KP 	int port_id = -1;
1070754695f9SJeeja KP 
1071754695f9SJeeja KP 	if (!hdmi)
1072754695f9SJeeja KP 		return;
10734a3478deSJeeja KP 
10742acd8309SJeeja KP 	/*
10752acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
10762acd8309SJeeja KP 	 * to be -1.
10772acd8309SJeeja KP 	 */
10784a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1079754695f9SJeeja KP 	port->eld.monitor_present = false;
1080f6fa11a3SSandeep Tayal 
10812acd8309SJeeja KP 	if (pin->mst_capable)
10822acd8309SJeeja KP 		port_id = port->id;
10832acd8309SJeeja KP 
10842acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1085754695f9SJeeja KP 				&port->eld.monitor_present,
1086754695f9SJeeja KP 				port->eld.eld_buffer,
1087f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1088f6fa11a3SSandeep Tayal 
1089f6fa11a3SSandeep Tayal 	if (size > 0) {
1090f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1091754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1092f6fa11a3SSandeep Tayal 			size = -EINVAL;
1093f6fa11a3SSandeep Tayal 	}
1094f6fa11a3SSandeep Tayal 
1095f6fa11a3SSandeep Tayal 	if (size > 0) {
1096754695f9SJeeja KP 		port->eld.eld_valid = true;
1097754695f9SJeeja KP 		port->eld.eld_size = size;
1098f6fa11a3SSandeep Tayal 	} else {
1099754695f9SJeeja KP 		port->eld.eld_valid = false;
1100754695f9SJeeja KP 		port->eld.eld_size = 0;
1101f6fa11a3SSandeep Tayal 	}
1102b8a54545SSubhransu S. Prusty 
1103754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
11044a3478deSJeeja KP 
1105754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1106b8a54545SSubhransu S. Prusty 
1107754695f9SJeeja KP 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1108754695f9SJeeja KP 						__func__, pin->nid, port->id);
11094a3478deSJeeja KP 
11104a3478deSJeeja KP 		/*
11114a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
11124a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
11134a3478deSJeeja KP 		 * control select.
11144a3478deSJeeja KP 		 */
11154a3478deSJeeja KP 		if (pcm) {
11164a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11174a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
11184a3478deSJeeja KP 
11194a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
11204a3478deSJeeja KP 		}
11214a3478deSJeeja KP 
11224a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1123f6fa11a3SSandeep Tayal 		return;
1124b8a54545SSubhransu S. Prusty 	}
1125b8a54545SSubhransu S. Prusty 
1126754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
11274a3478deSJeeja KP 		if (pcm) {
11284a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11294a3478deSJeeja KP 				"jack report for pcm=%d\n",
11304a3478deSJeeja KP 				pcm->pcm_id);
11314a3478deSJeeja KP 
11324a3478deSJeeja KP 			snd_jack_report(pcm->jack, SND_JACK_AVOUT);
11334a3478deSJeeja KP 		}
11344a3478deSJeeja KP 
1135f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1136754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1137754695f9SJeeja KP 
1138754695f9SJeeja KP 	}
1139754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
11404a3478deSJeeja KP }
11414a3478deSJeeja KP 
1142754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1143754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1144754695f9SJeeja KP {
1145754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1146754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1147754695f9SJeeja KP 	int i;
1148754695f9SJeeja KP 
1149754695f9SJeeja KP 	/*
1150754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1151754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1152754695f9SJeeja KP 	 * implemented.
1153754695f9SJeeja KP 	 */
1154754695f9SJeeja KP 
1155754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1156754695f9SJeeja KP 	if (!ports)
1157754695f9SJeeja KP 		return -ENOMEM;
1158754695f9SJeeja KP 
1159754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1160754695f9SJeeja KP 		ports[i].id = i;
1161754695f9SJeeja KP 		ports[i].pin = pin;
1162754695f9SJeeja KP 	}
1163754695f9SJeeja KP 	pin->ports = ports;
1164754695f9SJeeja KP 	pin->num_ports = max_ports;
1165754695f9SJeeja KP 	return 0;
1166b8a54545SSubhransu S. Prusty }
1167b8a54545SSubhransu S. Prusty 
116815b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
116915b91447SSubhransu S. Prusty {
117015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
117115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1172754695f9SJeeja KP 	int ret;
117315b91447SSubhransu S. Prusty 
117415b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
117515b91447SSubhransu S. Prusty 	if (!pin)
117615b91447SSubhransu S. Prusty 		return -ENOMEM;
117715b91447SSubhransu S. Prusty 
117815b91447SSubhransu S. Prusty 	pin->nid = nid;
11792acd8309SJeeja KP 	pin->mst_capable = false;
1180754695f9SJeeja KP 	pin->edev = edev;
1181754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1182754695f9SJeeja KP 	if (ret < 0)
1183754695f9SJeeja KP 		return ret;
118415b91447SSubhransu S. Prusty 
118515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
118615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1187754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1188b8a54545SSubhransu S. Prusty 
118915b91447SSubhransu S. Prusty 	return 0;
119018382eadSSubhransu S. Prusty }
119118382eadSSubhransu S. Prusty 
1192211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1193211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1194211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1195211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1196211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1197211caab7SSubhransu S. Prusty 
1198211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1199211caab7SSubhransu S. Prusty {
1200211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1201211caab7SSubhransu S. Prusty 
1202211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1203211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1204211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1205211caab7SSubhransu S. Prusty 		return;
1206211caab7SSubhransu S. Prusty 
1207211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1208211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1209211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1210211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1211211caab7SSubhransu S. Prusty 		return;
1212211caab7SSubhransu S. Prusty }
1213211caab7SSubhransu S. Prusty 
1214211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1215211caab7SSubhransu S. Prusty {
1216211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1217211caab7SSubhransu S. Prusty 
1218211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1219211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1220211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1221211caab7SSubhransu S. Prusty 		return;
1222211caab7SSubhransu S. Prusty 
1223211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1224211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1225211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1226211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1227211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1228211caab7SSubhransu S. Prusty 		return;
1229211caab7SSubhransu S. Prusty 
1230211caab7SSubhransu S. Prusty }
1231211caab7SSubhransu S. Prusty 
123217a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
123317a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
123417a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
123517a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1236c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
123717a42c45SSubhransu S. Prusty };
123817a42c45SSubhransu S. Prusty 
123917a42c45SSubhransu S. Prusty /*
124017a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
124117a42c45SSubhransu S. Prusty  * based on the number of converter queried.
124217a42c45SSubhransu S. Prusty  */
124317a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
124417a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
124517a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
124617a42c45SSubhransu S. Prusty {
124717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
124817a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
124917a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
125017a42c45SSubhransu S. Prusty 	int i = 0;
125117a42c45SSubhransu S. Prusty 	u32 rates, bps;
125217a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
125317a42c45SSubhransu S. Prusty 	u64 formats;
125417a42c45SSubhransu S. Prusty 	int ret;
125517a42c45SSubhransu S. Prusty 
125617a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
125717a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
125817a42c45SSubhransu S. Prusty 			GFP_KERNEL);
125917a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
126017a42c45SSubhransu S. Prusty 		return -ENOMEM;
126117a42c45SSubhransu S. Prusty 
126217a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
126317a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
126417a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
126517a42c45SSubhransu S. Prusty 		if (ret)
126617a42c45SSubhransu S. Prusty 			return ret;
126717a42c45SSubhransu S. Prusty 
126817a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
126917a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
127017a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
127117a42c45SSubhransu S. Prusty 
127217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
127317a42c45SSubhransu S. Prusty 			return -ENOMEM;
127417a42c45SSubhransu S. Prusty 
127517a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
127617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
127717a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
127817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
127917a42c45SSubhransu S. Prusty 			return -ENOMEM;
128017a42c45SSubhransu S. Prusty 
128117a42c45SSubhransu S. Prusty 		/*
128217a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
128317a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
128417a42c45SSubhransu S. Prusty 		 */
128517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
128617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
128717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
128817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
128917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
129017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
129117a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
129217a42c45SSubhransu S. Prusty 
129317a42c45SSubhransu S. Prusty 		i++;
129417a42c45SSubhransu S. Prusty 	}
129517a42c45SSubhransu S. Prusty 
129617a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
129717a42c45SSubhransu S. Prusty 
129817a42c45SSubhransu S. Prusty 	return 0;
129917a42c45SSubhransu S. Prusty }
130017a42c45SSubhransu S. Prusty 
130118382eadSSubhransu S. Prusty /*
130218382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
130318382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
130418382eadSSubhransu S. Prusty  */
130517a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
130617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
130718382eadSSubhransu S. Prusty {
130818382eadSSubhransu S. Prusty 	hda_nid_t nid;
13093c83ac23SSudip Mukherjee 	int i, num_nodes;
131018382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
131118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
131215b91447SSubhransu S. Prusty 	int ret;
131318382eadSSubhransu S. Prusty 
1314211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1315211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1316211caab7SSubhransu S. Prusty 
13173c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1318541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
131918382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
132018382eadSSubhransu S. Prusty 		return -EINVAL;
132118382eadSSubhransu S. Prusty 	}
132218382eadSSubhransu S. Prusty 
13233c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
132418382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
132518382eadSSubhransu S. Prusty 
132618382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
132718382eadSSubhransu S. Prusty 		unsigned int caps;
132818382eadSSubhransu S. Prusty 		unsigned int type;
132918382eadSSubhransu S. Prusty 
133018382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
133118382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
133218382eadSSubhransu S. Prusty 
133318382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
133418382eadSSubhransu S. Prusty 			continue;
133518382eadSSubhransu S. Prusty 
133618382eadSSubhransu S. Prusty 		switch (type) {
133718382eadSSubhransu S. Prusty 
133818382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
133915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
134015b91447SSubhransu S. Prusty 			if (ret < 0)
134115b91447SSubhransu S. Prusty 				return ret;
134218382eadSSubhransu S. Prusty 			break;
134318382eadSSubhransu S. Prusty 
134418382eadSSubhransu S. Prusty 		case AC_WID_PIN:
134515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
134615b91447SSubhransu S. Prusty 			if (ret < 0)
134715b91447SSubhransu S. Prusty 				return ret;
134818382eadSSubhransu S. Prusty 			break;
134918382eadSSubhransu S. Prusty 		}
135018382eadSSubhransu S. Prusty 	}
135118382eadSSubhransu S. Prusty 
135218382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
135318382eadSSubhransu S. Prusty 
135415b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
135518382eadSSubhransu S. Prusty 		return -EIO;
135618382eadSSubhransu S. Prusty 
135717a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
135817a42c45SSubhransu S. Prusty 	if (ret) {
135917a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
136017a42c45SSubhransu S. Prusty 							ret);
136117a42c45SSubhransu S. Prusty 		return ret;
136217a42c45SSubhransu S. Prusty 	}
136317a42c45SSubhransu S. Prusty 
136417a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
136517a42c45SSubhransu S. Prusty 
136615b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
136718382eadSSubhransu S. Prusty }
136818382eadSSubhransu S. Prusty 
1369f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1370b8a54545SSubhransu S. Prusty {
1371b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1372b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1373754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1374754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1375b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
13762acd8309SJeeja KP 	int i;
1377b8a54545SSubhransu S. Prusty 
1378b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1379b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1380b8a54545SSubhransu S. Prusty 
1381754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1382754695f9SJeeja KP 							pin_nid, pipe);
1383b8a54545SSubhransu S. Prusty 
1384b8a54545SSubhransu S. Prusty 	/*
1385b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1386b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1387b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1388b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1389b8a54545SSubhransu S. Prusty 	 */
1390b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1391b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1392b8a54545SSubhransu S. Prusty 		return;
1393b8a54545SSubhransu S. Prusty 
1394b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1395b8a54545SSubhransu S. Prusty 		return;
1396b8a54545SSubhransu S. Prusty 
1397b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1398754695f9SJeeja KP 		if (pin->nid != pin_nid)
1399754695f9SJeeja KP 			continue;
1400754695f9SJeeja KP 
1401754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1402754695f9SJeeja KP 		if (pipe == -1) {
14032acd8309SJeeja KP 			pin->mst_capable = false;
1404754695f9SJeeja KP 			/* if not MST, default is port[0] */
1405754695f9SJeeja KP 			hport = &pin->ports[0];
14062acd8309SJeeja KP 			goto out;
14072acd8309SJeeja KP 		} else {
14082acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
14092acd8309SJeeja KP 				pin->mst_capable = true;
14102acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
14112acd8309SJeeja KP 					hport = &pin->ports[i];
14122acd8309SJeeja KP 					goto out;
14132acd8309SJeeja KP 				}
14142acd8309SJeeja KP 			}
1415b8a54545SSubhransu S. Prusty 		}
1416b8a54545SSubhransu S. Prusty 	}
1417b8a54545SSubhransu S. Prusty 
14182acd8309SJeeja KP out:
14192acd8309SJeeja KP 	if (pin && hport)
1420754695f9SJeeja KP 		hdac_hdmi_present_sense(pin, hport);
1421754695f9SJeeja KP }
1422754695f9SJeeja KP 
1423b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1424b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1425b8a54545SSubhransu S. Prusty };
1426b8a54545SSubhransu S. Prusty 
14272889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
14282889099eSSubhransu S. Prusty 						int device)
14292889099eSSubhransu S. Prusty {
14302889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
14312889099eSSubhransu S. Prusty 
14322889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
14332889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
14342889099eSSubhransu S. Prusty 			return rtd->pcm;
14352889099eSSubhransu S. Prusty 	}
14362889099eSSubhransu S. Prusty 
14372889099eSSubhransu S. Prusty 	return NULL;
14382889099eSSubhransu S. Prusty }
14392889099eSSubhransu S. Prusty 
14404a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
14414a3478deSJeeja KP {
14424a3478deSJeeja KP 	char jack_name[NAME_SIZE];
14434a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
14444a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
14454a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
14464a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
14474a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
14484a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
14492889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
14502889099eSSubhransu S. Prusty 	int err;
14514a3478deSJeeja KP 
14524a3478deSJeeja KP 	/*
14534a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
14544a3478deSJeeja KP 	 * add to the pcm list
14554a3478deSJeeja KP 	 */
14564a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
14574a3478deSJeeja KP 	if (!pcm)
14584a3478deSJeeja KP 		return -ENOMEM;
14594a3478deSJeeja KP 	pcm->pcm_id = device;
14604a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1461ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
14624a3478deSJeeja KP 
14632889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
14642889099eSSubhransu S. Prusty 	if (snd_pcm) {
14652889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
14662889099eSSubhransu S. Prusty 		if (err < 0) {
14672889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
14682889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
14692889099eSSubhransu S. Prusty 				err, device);
14702889099eSSubhransu S. Prusty 			kfree(pcm);
14712889099eSSubhransu S. Prusty 			return err;
14722889099eSSubhransu S. Prusty 		}
14732889099eSSubhransu S. Prusty 	}
14742889099eSSubhransu S. Prusty 
14754a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
14764a3478deSJeeja KP 
14774a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
14784a3478deSJeeja KP 
14794a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
14804a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
14814a3478deSJeeja KP }
14824a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
14834a3478deSJeeja KP 
148418382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
148518382eadSSubhransu S. Prusty {
148618382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
148718382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
148818382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
148918382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1490b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1491b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1492754695f9SJeeja KP 	int ret, i;
149318382eadSSubhransu S. Prusty 
149418382eadSSubhransu S. Prusty 	edev->scodec = codec;
149518382eadSSubhransu S. Prusty 
1496b2047e99SVinod Koul 	/*
1497b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1498b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1499b2047e99SVinod Koul 	 */
1500b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1501500e06b9SVinod Koul 	if (!hlink) {
1502500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1503500e06b9SVinod Koul 		return -EIO;
1504500e06b9SVinod Koul 	}
1505500e06b9SVinod Koul 
1506b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1507b2047e99SVinod Koul 
150879f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
150979f4e922SSubhransu S. Prusty 	if (ret < 0)
151079f4e922SSubhransu S. Prusty 		return ret;
151118382eadSSubhransu S. Prusty 
1512b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1513b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1514b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1515b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1516b8a54545SSubhransu S. Prusty 				ret);
1517b8a54545SSubhransu S. Prusty 		return ret;
1518b8a54545SSubhransu S. Prusty 	}
1519b8a54545SSubhransu S. Prusty 
1520b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1521754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1522754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1523b8a54545SSubhransu S. Prusty 
152418382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
152518382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
152618382eadSSubhransu S. Prusty 
1527e342ac08SSubhransu S. Prusty 	/*
1528e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1529e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1530e342ac08SSubhransu S. Prusty 	 */
1531e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1532e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1533e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1534e342ac08SSubhransu S. Prusty 
1535e342ac08SSubhransu S. Prusty 	return 0;
1536e342ac08SSubhransu S. Prusty }
1537e342ac08SSubhransu S. Prusty 
1538e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1539e342ac08SSubhransu S. Prusty {
1540e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1541e342ac08SSubhransu S. Prusty 
1542e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
154318382eadSSubhransu S. Prusty 	return 0;
154418382eadSSubhransu S. Prusty }
154518382eadSSubhransu S. Prusty 
1546571d5078SJeeja KP #ifdef CONFIG_PM
15471b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
15481b377ccdSSubhransu S. Prusty {
15491b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
15501b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
15511b377ccdSSubhransu S. Prusty 
15521b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
15531b377ccdSSubhransu S. Prusty 
15541b377ccdSSubhransu S. Prusty 	/*
15551b377ccdSSubhransu S. Prusty 	 * Power down afg.
15561b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
15571b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
15581b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
15591b377ccdSSubhransu S. Prusty 	 * to read the state.
15601b377ccdSSubhransu S. Prusty 	 */
15611b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15621b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
15631b377ccdSSubhransu S. Prusty 
15641b377ccdSSubhransu S. Prusty 	return 0;
15651b377ccdSSubhransu S. Prusty }
15661b377ccdSSubhransu S. Prusty 
15670fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1568571d5078SJeeja KP {
15690fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1570571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1571571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1572571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
1573754695f9SJeeja KP 	int i;
15741b377ccdSSubhransu S. Prusty 
15751b377ccdSSubhransu S. Prusty 	/* Power up afg */
15761b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15771b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1578571d5078SJeeja KP 
1579571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1580571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1581571d5078SJeeja KP 
1582571d5078SJeeja KP 	/*
1583571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1584571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1585571d5078SJeeja KP 	 * all pins here.
1586571d5078SJeeja KP 	 */
1587571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1588754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1589754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1590571d5078SJeeja KP 
15911b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1592571d5078SJeeja KP }
1593571d5078SJeeja KP #else
15941b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
15950fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1596571d5078SJeeja KP #endif
1597571d5078SJeeja KP 
159818382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
159918382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1600e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
160118382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
160218382eadSSubhransu S. Prusty };
160318382eadSSubhransu S. Prusty 
16042889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
16052889099eSSubhransu S. Prusty 					unsigned char *chmap)
16062889099eSSubhransu S. Prusty {
16072889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16082889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16092889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
16102889099eSSubhransu S. Prusty 
1611ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
16122889099eSSubhransu S. Prusty }
16132889099eSSubhransu S. Prusty 
16142889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
16152889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
16162889099eSSubhransu S. Prusty {
16172889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16182889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16192889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1620754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16212889099eSSubhransu S. Prusty 
1622ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1623ab1eea19SJeeja KP 	pcm->chmap_set = true;
1624ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
16252889099eSSubhransu S. Prusty 	if (prepared)
1626754695f9SJeeja KP 		hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1627ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
16282889099eSSubhransu S. Prusty }
16292889099eSSubhransu S. Prusty 
16302889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
16312889099eSSubhransu S. Prusty {
16322889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16332889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16342889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1635754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16362889099eSSubhransu S. Prusty 
1637754695f9SJeeja KP 	return port ? true:false;
16382889099eSSubhransu S. Prusty }
16392889099eSSubhransu S. Prusty 
16402889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
16412889099eSSubhransu S. Prusty {
16422889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16432889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16442889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1645754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16462889099eSSubhransu S. Prusty 
1647754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
16482889099eSSubhransu S. Prusty 		return 0;
16492889099eSSubhransu S. Prusty 
1650754695f9SJeeja KP 	return port->eld.info.spk_alloc;
16512889099eSSubhransu S. Prusty }
16522889099eSSubhransu S. Prusty 
165318382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
165418382eadSSubhransu S. Prusty {
165518382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
165618382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
165717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1658b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
165917a42c45SSubhransu S. Prusty 	int num_dais = 0;
166018382eadSSubhransu S. Prusty 	int ret = 0;
166118382eadSSubhransu S. Prusty 
1662b2047e99SVinod Koul 	/* hold the ref while we probe */
1663b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1664500e06b9SVinod Koul 	if (!hlink) {
1665500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1666500e06b9SVinod Koul 		return -EIO;
1667500e06b9SVinod Koul 	}
1668500e06b9SVinod Koul 
1669b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1670b2047e99SVinod Koul 
167118382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
167218382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
167318382eadSSubhransu S. Prusty 		return -ENOMEM;
167418382eadSSubhransu S. Prusty 
167518382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1676bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
16772889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
16782889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
16792889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
16802889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
168118382eadSSubhransu S. Prusty 
168218382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
168318382eadSSubhransu S. Prusty 
168415b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
168515b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
16864a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
16874a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
168815b91447SSubhransu S. Prusty 
1689aeaccef0SRamesh Babu 	/*
1690aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1691aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1692aeaccef0SRamesh Babu 	 */
1693aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1694aeaccef0SRamesh Babu 	if (ret < 0) {
1695aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1696aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1697aeaccef0SRamesh Babu 			ret);
1698aeaccef0SRamesh Babu 		return ret;
1699aeaccef0SRamesh Babu 	}
1700aeaccef0SRamesh Babu 
170117a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
170217a42c45SSubhransu S. Prusty 	if (ret < 0) {
170317a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
170417a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
170518382eadSSubhransu S. Prusty 		return ret;
170617a42c45SSubhransu S. Prusty 	}
170718382eadSSubhransu S. Prusty 
170818382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1709b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
171017a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1711b2047e99SVinod Koul 
1712b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1713b2047e99SVinod Koul 
1714b2047e99SVinod Koul 	return ret;
171518382eadSSubhransu S. Prusty }
171618382eadSSubhransu S. Prusty 
171718382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
171818382eadSSubhransu S. Prusty {
171915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
172015b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
172115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
17224a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
1723754695f9SJeeja KP 	int i;
172415b91447SSubhransu S. Prusty 
172518382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
172618382eadSSubhransu S. Prusty 
17274a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
17284a3478deSJeeja KP 		pcm->cvt = NULL;
1729754695f9SJeeja KP 		pcm->port = NULL;
17304a3478deSJeeja KP 		list_del(&pcm->head);
17314a3478deSJeeja KP 		kfree(pcm);
17324a3478deSJeeja KP 	}
17334a3478deSJeeja KP 
173415b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
173515b91447SSubhransu S. Prusty 		list_del(&cvt->head);
17364a3478deSJeeja KP 		kfree(cvt->name);
173715b91447SSubhransu S. Prusty 		kfree(cvt);
173815b91447SSubhransu S. Prusty 	}
173915b91447SSubhransu S. Prusty 
174015b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1741754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1742754695f9SJeeja KP 			pin->ports[i].pin = NULL;
1743754695f9SJeeja KP 		kfree(pin->ports);
174415b91447SSubhransu S. Prusty 		list_del(&pin->head);
174515b91447SSubhransu S. Prusty 		kfree(pin);
174615b91447SSubhransu S. Prusty 	}
174715b91447SSubhransu S. Prusty 
174818382eadSSubhransu S. Prusty 	return 0;
174918382eadSSubhransu S. Prusty }
175018382eadSSubhransu S. Prusty 
1751e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1752e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1753e342ac08SSubhransu S. Prusty {
1754e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1755e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
175607f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1757b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1758b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
175907f083abSSubhransu S. Prusty 	int err;
1760e342ac08SSubhransu S. Prusty 
1761e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1762e342ac08SSubhransu S. Prusty 
176307f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
176407f083abSSubhransu S. Prusty 	if (!bus)
176507f083abSSubhransu S. Prusty 		return 0;
176607f083abSSubhransu S. Prusty 
17671b377ccdSSubhransu S. Prusty 	/*
17681b377ccdSSubhransu S. Prusty 	 * Power down afg.
17691b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
17701b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
17711b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
17721b377ccdSSubhransu S. Prusty 	 * to read the state.
17731b377ccdSSubhransu S. Prusty 	 */
17741b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17751b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
177607f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
177707f083abSSubhransu S. Prusty 	if (err < 0) {
177807f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
177907f083abSSubhransu S. Prusty 		return err;
178007f083abSSubhransu S. Prusty 	}
178107f083abSSubhransu S. Prusty 
1782b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1783500e06b9SVinod Koul 	if (!hlink) {
1784500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1785500e06b9SVinod Koul 		return -EIO;
1786500e06b9SVinod Koul 	}
1787500e06b9SVinod Koul 
1788b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1789b2047e99SVinod Koul 
1790e342ac08SSubhransu S. Prusty 	return 0;
1791e342ac08SSubhransu S. Prusty }
1792e342ac08SSubhransu S. Prusty 
1793e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1794e342ac08SSubhransu S. Prusty {
1795e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1796e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
179707f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1798b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1799b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
180007f083abSSubhransu S. Prusty 	int err;
1801e342ac08SSubhransu S. Prusty 
1802e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1803e342ac08SSubhransu S. Prusty 
180407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
180507f083abSSubhransu S. Prusty 	if (!bus)
180607f083abSSubhransu S. Prusty 		return 0;
180707f083abSSubhransu S. Prusty 
1808b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1809500e06b9SVinod Koul 	if (!hlink) {
1810500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1811500e06b9SVinod Koul 		return -EIO;
1812500e06b9SVinod Koul 	}
1813500e06b9SVinod Koul 
1814b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1815b2047e99SVinod Koul 
181607f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
181707f083abSSubhransu S. Prusty 	if (err < 0) {
181807f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
181907f083abSSubhransu S. Prusty 		return err;
182007f083abSSubhransu S. Prusty 	}
182107f083abSSubhransu S. Prusty 
1822ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1823ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1824ab85f5b3SSubhransu S. Prusty 
1825e342ac08SSubhransu S. Prusty 	/* Power up afg */
18261b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18271b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1828e342ac08SSubhransu S. Prusty 
1829e342ac08SSubhransu S. Prusty 	return 0;
1830e342ac08SSubhransu S. Prusty }
1831e342ac08SSubhransu S. Prusty #else
1832e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1833e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1834e342ac08SSubhransu S. Prusty #endif
1835e342ac08SSubhransu S. Prusty 
1836e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1837e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
18381b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
18390fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1840e342ac08SSubhransu S. Prusty };
1841e342ac08SSubhransu S. Prusty 
184218382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
184318382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1844e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1845cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
184618382eadSSubhransu S. Prusty 	{}
184718382eadSSubhransu S. Prusty };
184818382eadSSubhransu S. Prusty 
184918382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
185018382eadSSubhransu S. Prusty 
185118382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
185218382eadSSubhransu S. Prusty 	. hdac = {
185318382eadSSubhransu S. Prusty 		.driver = {
185418382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1855e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
185618382eadSSubhransu S. Prusty 		},
185718382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
185818382eadSSubhransu S. Prusty 	},
185918382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
186018382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
186118382eadSSubhransu S. Prusty };
186218382eadSSubhransu S. Prusty 
186318382eadSSubhransu S. Prusty static int __init hdmi_init(void)
186418382eadSSubhransu S. Prusty {
186518382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
186618382eadSSubhransu S. Prusty }
186718382eadSSubhransu S. Prusty 
186818382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
186918382eadSSubhransu S. Prusty {
187018382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
187118382eadSSubhransu S. Prusty }
187218382eadSSubhransu S. Prusty 
187318382eadSSubhransu S. Prusty module_init(hdmi_init);
187418382eadSSubhransu S. Prusty module_exit(hdmi_exit);
187518382eadSSubhransu S. Prusty 
187618382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
187718382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
187818382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
187918382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1880