xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 754695f9960b58a9c9d6b9ab7fae68f7c4b47d9c)
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
45*754695f9SJeeja 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;
85*754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
86*754695f9SJeeja KP 	int num_ports;
87*754695f9SJeeja KP 	struct hdac_ext_device *edev;
88*754695f9SJeeja KP };
89*754695f9SJeeja KP 
90*754695f9SJeeja KP struct hdac_hdmi_port {
91*754695f9SJeeja KP 	int id;
92*754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9318382eadSSubhransu S. Prusty 	int num_mux_nids;
9418382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
95b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
9618382eadSSubhransu S. Prusty };
9718382eadSSubhransu S. Prusty 
984a3478deSJeeja KP struct hdac_hdmi_pcm {
994a3478deSJeeja KP 	struct list_head head;
1004a3478deSJeeja KP 	int pcm_id;
101*754695f9SJeeja KP 	struct hdac_hdmi_port *port;
1024a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
1034a3478deSJeeja KP 	struct snd_jack *jack;
104c9bfb5d7SJeeja KP 	int stream_tag;
105c9bfb5d7SJeeja KP 	int channels;
106c9bfb5d7SJeeja KP 	int format;
107ab1eea19SJeeja KP 	bool chmap_set;
108ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
109ab1eea19SJeeja KP 	struct mutex lock;
1104a3478deSJeeja KP };
1114a3478deSJeeja KP 
112*754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11318382eadSSubhransu S. Prusty 	int dai_id;
114*754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11618382eadSSubhransu S. Prusty };
11718382eadSSubhransu S. Prusty 
11818382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
119*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12015b91447SSubhransu S. Prusty 	struct list_head pin_list;
12115b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1224a3478deSJeeja KP 	struct list_head pcm_list;
12315b91447SSubhransu S. Prusty 	int num_pin;
12415b91447SSubhransu S. Prusty 	int num_cvt;
125*754695f9SJeeja KP 	int num_ports;
1264a3478deSJeeja KP 	struct mutex pin_mutex;
127bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
12818382eadSSubhransu S. Prusty };
12918382eadSSubhransu S. Prusty 
130c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
131c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
132c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
133c9bfb5d7SJeeja KP {
134c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1351de777feSJeeja KP 
136c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
137c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
138c9bfb5d7SJeeja KP 			break;
139c9bfb5d7SJeeja KP 	}
140c9bfb5d7SJeeja KP 
141c9bfb5d7SJeeja KP 	return pcm;
142c9bfb5d7SJeeja KP }
1431de777feSJeeja KP 
1442889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
1452889099eSSubhransu S. Prusty 						int pcm_idx)
1462889099eSSubhransu S. Prusty {
1472889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
1482889099eSSubhransu S. Prusty 
1492889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
1502889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
1512889099eSSubhransu S. Prusty 			return pcm;
1522889099eSSubhransu S. Prusty 	}
1532889099eSSubhransu S. Prusty 
1542889099eSSubhransu S. Prusty 	return NULL;
1552889099eSSubhransu S. Prusty }
1562889099eSSubhransu S. Prusty 
157e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
158e342ac08SSubhransu S. Prusty {
15951b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
160e342ac08SSubhransu S. Prusty 
16151b2c425SGeliang Tang 	return to_ehdac_device(hdac);
162e342ac08SSubhransu S. Prusty }
163e342ac08SSubhransu S. Prusty 
1642428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1652428bca3SSubhransu S. Prusty {
1662428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1672428bca3SSubhransu S. Prusty }
1682428bca3SSubhransu S. Prusty 
1692428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1702428bca3SSubhransu S. Prusty {
1712428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1722428bca3SSubhransu S. Prusty }
1732428bca3SSubhransu S. Prusty 
1742428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1752428bca3SSubhransu S. Prusty 						void *eld)
1762428bca3SSubhransu S. Prusty {
1772428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1782428bca3SSubhransu S. Prusty 	int i;
1792428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1802428bca3SSubhransu S. Prusty 
1812428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1822428bca3SSubhransu S. Prusty 	if (!sad)
1832428bca3SSubhransu S. Prusty 		goto format_constraint;
1842428bca3SSubhransu S. Prusty 
1852428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1862428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1872428bca3SSubhransu S. Prusty 
1882428bca3SSubhransu S. Prusty 			/*
1892428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1902428bca3SSubhransu S. Prusty 			 * container so we set S32
1912428bca3SSubhransu S. Prusty 			 */
1922428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1932428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1942428bca3SSubhransu S. Prusty 		}
1952428bca3SSubhransu S. Prusty 	}
1962428bca3SSubhransu S. Prusty 
1972428bca3SSubhransu S. Prusty format_constraint:
1982428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1992428bca3SSubhransu S. Prusty 				formats);
2002428bca3SSubhransu S. Prusty 
2012428bca3SSubhransu S. Prusty }
2022428bca3SSubhransu S. Prusty 
203a657f1d0SSubhransu S. Prusty static void
204a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
205a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
206a657f1d0SSubhransu S. Prusty {
207a657f1d0SSubhransu S. Prusty 	int val;
208a657f1d0SSubhransu S. Prusty 
209a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
210a657f1d0SSubhransu S. Prusty 
211a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
212a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
213a657f1d0SSubhransu S. Prusty }
214a657f1d0SSubhransu S. Prusty 
215478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
216478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
217478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
218478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
219478f544eSSubhransu S. Prusty 
220478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
221478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
222478f544eSSubhransu S. Prusty 	u8 CXT04;
223478f544eSSubhransu S. Prusty 	u8 CA;
224478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
225478f544eSSubhransu S. Prusty };
226478f544eSSubhransu S. Prusty 
227a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
228*754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
229a657f1d0SSubhransu S. Prusty {
230a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
231a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
232*754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
233478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
234478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
235ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
236478f544eSSubhransu S. Prusty 	u8 *dip;
237a657f1d0SSubhransu S. Prusty 	int ret;
238a657f1d0SSubhransu S. Prusty 	int i;
239478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
240478f544eSSubhransu S. Prusty 	u8 conn_type;
241bcced704SSubhransu S. Prusty 	int channels, ca;
242a657f1d0SSubhransu S. Prusty 
243*754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
244ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
245bcced704SSubhransu S. Prusty 
246bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
247ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
248bcced704SSubhransu S. Prusty 
249bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
250ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
251bcced704SSubhransu S. Prusty 
252*754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
253478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
254a657f1d0SSubhransu S. Prusty 
255478f544eSSubhransu S. Prusty 	switch (conn_type) {
256478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
257478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
258478f544eSSubhransu S. Prusty 
259478f544eSSubhransu S. Prusty 		frame.channels = channels;
260bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
261a657f1d0SSubhransu S. Prusty 
262a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
263a657f1d0SSubhransu S. Prusty 		if (ret < 0)
264a657f1d0SSubhransu S. Prusty 			return ret;
265a657f1d0SSubhransu S. Prusty 
266478f544eSSubhransu S. Prusty 		break;
267478f544eSSubhransu S. Prusty 
268478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
269478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
270478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
271478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
272478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
273478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
274bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
275478f544eSSubhransu S. Prusty 
276478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
277478f544eSSubhransu S. Prusty 		break;
278478f544eSSubhransu S. Prusty 
279478f544eSSubhransu S. Prusty 	default:
280478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
281478f544eSSubhransu S. Prusty 						conn_type);
282478f544eSSubhransu S. Prusty 		return -EIO;
283478f544eSSubhransu S. Prusty 	}
284478f544eSSubhransu S. Prusty 
285a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
286ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
287ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
288a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
289a657f1d0SSubhransu S. Prusty 
290a657f1d0SSubhransu S. Prusty 
291a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
292ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
293478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
294391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
295ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
296391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
297478f544eSSubhransu S. Prusty 	} else {
298478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
299ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
300478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
301478f544eSSubhransu S. Prusty 	}
302a657f1d0SSubhransu S. Prusty 
303a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
304ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
305ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
306a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
307a657f1d0SSubhransu S. Prusty 
308a657f1d0SSubhransu S. Prusty 	return 0;
309a657f1d0SSubhransu S. Prusty }
310a657f1d0SSubhransu S. Prusty 
311c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
312c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
313c9bfb5d7SJeeja KP 		int slots, int slot_width)
314b0362adbSSubhransu S. Prusty {
315c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
316c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
317*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
318c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
319c9bfb5d7SJeeja KP 
320c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
321b0362adbSSubhransu S. Prusty 
322b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
323b0362adbSSubhransu S. Prusty 
324c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
325b0362adbSSubhransu S. Prusty 
326c9bfb5d7SJeeja KP 	if (pcm)
327c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
328bcced704SSubhransu S. Prusty 
329c9bfb5d7SJeeja KP 	return 0;
330b0362adbSSubhransu S. Prusty }
331b0362adbSSubhransu S. Prusty 
332b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
333b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
334b0362adbSSubhransu S. Prusty {
335b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
33654dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
337*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
338*754695f9SJeeja KP 	struct hdac_hdmi_port *port;
339c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
340c9bfb5d7SJeeja KP 	int format;
341b0362adbSSubhransu S. Prusty 
34254dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
343*754695f9SJeeja KP 	port = dai_map->port;
34454dfa1eaSSubhransu S. Prusty 
345*754695f9SJeeja KP 	if (!port)
34654dfa1eaSSubhransu S. Prusty 		return -ENODEV;
34754dfa1eaSSubhransu S. Prusty 
348*754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
349*754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
350*754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
351*754695f9SJeeja KP 					port->pin->nid, port->id);
352b0362adbSSubhransu S. Prusty 		return -ENODEV;
353b0362adbSSubhransu S. Prusty 	}
354b0362adbSSubhransu S. Prusty 
355c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
356b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
357b0362adbSSubhransu S. Prusty 			24, 0);
358b0362adbSSubhransu S. Prusty 
359c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
360c9bfb5d7SJeeja KP 	if (!pcm)
361148569fdSSubhransu S. Prusty 		return -EIO;
362148569fdSSubhransu S. Prusty 
363c9bfb5d7SJeeja KP 	pcm->format = format;
364c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
365148569fdSSubhransu S. Prusty 
366148569fdSSubhransu S. Prusty 	return 0;
367148569fdSSubhransu S. Prusty }
368148569fdSSubhransu S. Prusty 
369*754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
370*754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
371*754695f9SJeeja KP 					struct hdac_hdmi_port *port)
372148569fdSSubhransu S. Prusty {
373148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
374148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
375148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
376148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
377148569fdSSubhransu S. Prusty 		return -EINVAL;
378148569fdSSubhransu S. Prusty 	}
379148569fdSSubhransu S. Prusty 
380*754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
381*754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
382*754695f9SJeeja KP 	if (port->num_mux_nids == 0)
383*754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
384*754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
385*754695f9SJeeja KP 						pin->nid, port->id);
386148569fdSSubhransu S. Prusty 
387*754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
388*754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
389148569fdSSubhransu S. Prusty 
390*754695f9SJeeja KP 	return port->num_mux_nids;
391148569fdSSubhransu S. Prusty }
392148569fdSSubhransu S. Prusty 
393148569fdSSubhransu S. Prusty /*
394*754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
395148569fdSSubhransu S. Prusty  *
396*754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
397148569fdSSubhransu S. Prusty  *
398*754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
399*754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
400148569fdSSubhransu S. Prusty  * connected.
401148569fdSSubhransu S. Prusty  */
402*754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
403148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
404148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
405148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
406148569fdSSubhransu S. Prusty {
407148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
408*754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
409148569fdSSubhransu S. Prusty 	int ret, i;
410148569fdSSubhransu S. Prusty 
411148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
412148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
413*754695f9SJeeja KP 			port = pcm->port;
414148569fdSSubhransu S. Prusty 			break;
415148569fdSSubhransu S. Prusty 		}
416148569fdSSubhransu S. Prusty 	}
417148569fdSSubhransu S. Prusty 
418*754695f9SJeeja KP 	if (port) {
419*754695f9SJeeja KP 		ret = hdac_hdmi_query_port_connlist(edev, port->pin, port);
420148569fdSSubhransu S. Prusty 		if (ret < 0)
421148569fdSSubhransu S. Prusty 			return NULL;
422148569fdSSubhransu S. Prusty 
423*754695f9SJeeja KP 		for (i = 0; i < port->num_mux_nids; i++) {
424*754695f9SJeeja KP 			if (port->mux_nids[i] == cvt->nid)
425*754695f9SJeeja KP 				return port;
426148569fdSSubhransu S. Prusty 		}
427148569fdSSubhransu S. Prusty 	}
428148569fdSSubhransu S. Prusty 
429148569fdSSubhransu S. Prusty 	return NULL;
430148569fdSSubhransu S. Prusty }
431148569fdSSubhransu S. Prusty 
43254dfa1eaSSubhransu S. Prusty /*
43354dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
43454dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
43554dfa1eaSSubhransu S. Prusty  * doesn't fail.
43654dfa1eaSSubhransu S. Prusty  */
437b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
438b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
439b0362adbSSubhransu S. Prusty {
440b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
441b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
442*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
443148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
444*754695f9SJeeja KP 	struct hdac_hdmi_port *port;
4452428bca3SSubhransu S. Prusty 	int ret;
446b0362adbSSubhransu S. Prusty 
447b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
448b0362adbSSubhransu S. Prusty 
449148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
450*754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
45154dfa1eaSSubhransu S. Prusty 
45254dfa1eaSSubhransu S. Prusty 	/*
45354dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
45454dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
45554dfa1eaSSubhransu S. Prusty 	 */
456*754695f9SJeeja KP 	if (!port)
45754dfa1eaSSubhransu S. Prusty 		return 0;
458148569fdSSubhransu S. Prusty 
459*754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
460*754695f9SJeeja KP 			(!port->eld.eld_valid)) {
461b0362adbSSubhransu S. Prusty 
46254dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
463*754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
464*754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
465*754695f9SJeeja KP 			port->pin->nid, port->id);
466b8a54545SSubhransu S. Prusty 
46754dfa1eaSSubhransu S. Prusty 		return 0;
468b0362adbSSubhransu S. Prusty 	}
469b0362adbSSubhransu S. Prusty 
470*754695f9SJeeja KP 	dai_map->port = port;
471b0362adbSSubhransu S. Prusty 
4722428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
473*754695f9SJeeja KP 				port->eld.eld_buffer);
4742428bca3SSubhransu S. Prusty 	if (ret < 0)
4752428bca3SSubhransu S. Prusty 		return ret;
476b0362adbSSubhransu S. Prusty 
4772428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
478*754695f9SJeeja KP 				port->eld.eld_buffer);
479b0362adbSSubhransu S. Prusty }
480b0362adbSSubhransu S. Prusty 
481b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
482b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
483b0362adbSSubhransu S. Prusty {
484b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
485b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
486*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
487ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
488b0362adbSSubhransu S. Prusty 
489b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
490b0362adbSSubhransu S. Prusty 
491ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
492bcced704SSubhransu S. Prusty 
493ab1eea19SJeeja KP 	if (pcm) {
494ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
495ab1eea19SJeeja KP 		pcm->chmap_set = false;
496ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
497ab1eea19SJeeja KP 		pcm->channels = 0;
498ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
499b0362adbSSubhransu S. Prusty 	}
500ab1eea19SJeeja KP 
501*754695f9SJeeja KP 	if (dai_map->port)
502*754695f9SJeeja KP 		dai_map->port = NULL;
50354dfa1eaSSubhransu S. Prusty }
504b0362adbSSubhransu S. Prusty 
50518382eadSSubhransu S. Prusty static int
50618382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
50718382eadSSubhransu S. Prusty {
508bcced704SSubhransu S. Prusty 	unsigned int chans;
509bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
510bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
51118382eadSSubhransu S. Prusty 	int err;
51218382eadSSubhransu S. Prusty 
513bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
514bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
515bcced704SSubhransu S. Prusty 
516bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
517bcced704SSubhransu S. Prusty 
518bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
519bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
520bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
52118382eadSSubhransu S. Prusty 
52218382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
52318382eadSSubhransu S. Prusty 			&cvt->params.rates,
52418382eadSSubhransu S. Prusty 			&cvt->params.formats,
52518382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
52618382eadSSubhransu S. Prusty 	if (err < 0)
52718382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
52818382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
52918382eadSSubhransu S. Prusty 			cvt->nid, err);
53018382eadSSubhransu S. Prusty 
53118382eadSSubhransu S. Prusty 	return err;
53218382eadSSubhransu S. Prusty }
53318382eadSSubhransu S. Prusty 
53479f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
535c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
536c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
537c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
538c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
539c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
54018382eadSSubhransu S. Prusty {
54118382eadSSubhransu S. Prusty 	w->id = id;
54279f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
54379f4e922SSubhransu S. Prusty 	if (!w->name)
54479f4e922SSubhransu S. Prusty 		return -ENOMEM;
54579f4e922SSubhransu S. Prusty 
54618382eadSSubhransu S. Prusty 	w->sname = stream;
54718382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
54818382eadSSubhransu S. Prusty 	w->shift = 0;
54979f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
55079f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
55179f4e922SSubhransu S. Prusty 	w->priv = priv;
552c9bfb5d7SJeeja KP 	w->event = event;
553c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
55479f4e922SSubhransu S. Prusty 
55579f4e922SSubhransu S. Prusty 	return 0;
55618382eadSSubhransu S. Prusty }
55718382eadSSubhransu S. Prusty 
55818382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
55979f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
56079f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
56179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
56218382eadSSubhransu S. Prusty {
56318382eadSSubhransu S. Prusty 	route->sink = sink;
56418382eadSSubhransu S. Prusty 	route->source = src;
56518382eadSSubhransu S. Prusty 	route->control = control;
56679f4e922SSubhransu S. Prusty 	route->connected = handler;
56718382eadSSubhransu S. Prusty }
56818382eadSSubhransu S. Prusty 
5694a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
570*754695f9SJeeja KP 					struct hdac_hdmi_port *port)
5714a3478deSJeeja KP {
5724a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
5734a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
5744a3478deSJeeja KP 
5754a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
576*754695f9SJeeja KP 		if (!pcm->port)
577*754695f9SJeeja KP 			continue;
578*754695f9SJeeja KP 
579*754695f9SJeeja KP 		if (pcm->port == port)
5804a3478deSJeeja KP 			return pcm;
5814a3478deSJeeja KP 	}
5824a3478deSJeeja KP 
5834a3478deSJeeja KP 	return NULL;
5844a3478deSJeeja KP }
5854a3478deSJeeja KP 
586c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
587c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
588c9bfb5d7SJeeja KP {
589c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
590c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
591c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
592c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
593c9bfb5d7SJeeja KP 	}
594c9bfb5d7SJeeja KP }
595c9bfb5d7SJeeja KP 
596c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
597c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
598c9bfb5d7SJeeja KP {
599c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
600c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
601c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
602c9bfb5d7SJeeja KP }
603c9bfb5d7SJeeja KP 
604c9bfb5d7SJeeja KP 
605c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
606c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
607c9bfb5d7SJeeja KP {
608*754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
609c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
610c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
611c9bfb5d7SJeeja KP 
612c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
613c9bfb5d7SJeeja KP 			__func__, w->name, event);
614c9bfb5d7SJeeja KP 
615*754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
616c9bfb5d7SJeeja KP 	if (!pcm)
617c9bfb5d7SJeeja KP 		return -EIO;
618c9bfb5d7SJeeja KP 
619c9bfb5d7SJeeja KP 	switch (event) {
620c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
621*754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
622c9bfb5d7SJeeja KP 
623c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
624*754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
625c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
626c9bfb5d7SJeeja KP 
627*754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
628c9bfb5d7SJeeja KP 
629*754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
630c9bfb5d7SJeeja KP 
631c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
632*754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
633c9bfb5d7SJeeja KP 
634c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
635*754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
636c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
637c9bfb5d7SJeeja KP 
638*754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
639c9bfb5d7SJeeja KP 		break;
640c9bfb5d7SJeeja KP 
641c9bfb5d7SJeeja KP 	}
642c9bfb5d7SJeeja KP 
643c9bfb5d7SJeeja KP 	return 0;
644c9bfb5d7SJeeja KP }
645c9bfb5d7SJeeja KP 
646c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
647c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
648c9bfb5d7SJeeja KP {
649c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
650c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
651c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
652c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
653c9bfb5d7SJeeja KP 
654c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
655c9bfb5d7SJeeja KP 			__func__, w->name, event);
656c9bfb5d7SJeeja KP 
657c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
658c9bfb5d7SJeeja KP 	if (!pcm)
659c9bfb5d7SJeeja KP 		return -EIO;
660c9bfb5d7SJeeja KP 
661c9bfb5d7SJeeja KP 	switch (event) {
662c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
663c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
664c9bfb5d7SJeeja KP 
665c9bfb5d7SJeeja KP 		/* Enable transmission */
666c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
667c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
668c9bfb5d7SJeeja KP 
669c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
670c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
671c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
672c9bfb5d7SJeeja KP 
673c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
674c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
675c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
676c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
677c9bfb5d7SJeeja KP 		break;
678c9bfb5d7SJeeja KP 
679c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
680c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
681c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
682c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
683c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
684c9bfb5d7SJeeja KP 
685c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
686c9bfb5d7SJeeja KP 		break;
687c9bfb5d7SJeeja KP 
688c9bfb5d7SJeeja KP 	}
689c9bfb5d7SJeeja KP 
690c9bfb5d7SJeeja KP 	return 0;
691c9bfb5d7SJeeja KP }
692c9bfb5d7SJeeja KP 
693c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
694c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
695c9bfb5d7SJeeja KP {
696*754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
697c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
698c9bfb5d7SJeeja KP 	int mux_idx;
699c9bfb5d7SJeeja KP 
700c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
701c9bfb5d7SJeeja KP 			__func__, w->name, event);
702c9bfb5d7SJeeja KP 
703c9bfb5d7SJeeja KP 	if (!kc)
704c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
705c9bfb5d7SJeeja KP 
706c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
707c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
708*754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
709c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
710c9bfb5d7SJeeja KP 	}
711c9bfb5d7SJeeja KP 
712c9bfb5d7SJeeja KP 	return 0;
713c9bfb5d7SJeeja KP }
714c9bfb5d7SJeeja KP 
7154a3478deSJeeja KP /*
7164a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7174a3478deSJeeja KP  */
718*754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
7194a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7204a3478deSJeeja KP {
7214a3478deSJeeja KP 	int ret;
7224a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7234a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7244a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
725*754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7264a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7274a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7284a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7294a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7304a3478deSJeeja KP 
7314a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7324a3478deSJeeja KP 	if (ret < 0)
7334a3478deSJeeja KP 		return ret;
7344a3478deSJeeja KP 
735*754695f9SJeeja KP 	if (port == NULL)
736*754695f9SJeeja KP 		return -EINVAL;
737*754695f9SJeeja KP 
7384a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7394a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
740*754695f9SJeeja KP 		if (!pcm->port && pcm->port == port &&
741*754695f9SJeeja KP 			pcm->port->id == port->id)
742*754695f9SJeeja KP 			pcm->port = NULL;
7434a3478deSJeeja KP 
7444a3478deSJeeja KP 		/*
7454a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7464a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7474a3478deSJeeja KP 		 */
748*754695f9SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->port) {
749*754695f9SJeeja KP 			pcm->port = port;
750*754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
7514a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7524a3478deSJeeja KP 					"jack report for pcm=%d\n",
7534a3478deSJeeja KP 					pcm->pcm_id);
7544a3478deSJeeja KP 
7554a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
7564a3478deSJeeja KP 			}
7574a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
7584a3478deSJeeja KP 			return ret;
7594a3478deSJeeja KP 		}
7604a3478deSJeeja KP 	}
7614a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
7624a3478deSJeeja KP 
7634a3478deSJeeja KP 	return ret;
7644a3478deSJeeja KP }
7654a3478deSJeeja KP 
76679f4e922SSubhransu S. Prusty /*
76779f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
76879f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
76979f4e922SSubhransu S. Prusty  * widget runtime.
77079f4e922SSubhransu S. Prusty  *
77179f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
77279f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
77379f4e922SSubhransu S. Prusty  * "NONE"
77479f4e922SSubhransu S. Prusty  */
775*754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
776*754695f9SJeeja KP 				struct hdac_hdmi_port *port,
77779f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
77879f4e922SSubhransu S. Prusty 				const char *widget_name)
77918382eadSSubhransu S. Prusty {
78079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
781*754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
78279f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
78379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
78479f4e922SSubhransu S. Prusty 	struct soc_enum *se;
78579f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
78679f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
78779f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
78879f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
78979f4e922SSubhransu S. Prusty 	int i = 0;
79079f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
79118382eadSSubhransu S. Prusty 
79279f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
79379f4e922SSubhransu S. Prusty 	if (!kc)
79479f4e922SSubhransu S. Prusty 		return -ENOMEM;
79518382eadSSubhransu S. Prusty 
79679f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
79779f4e922SSubhransu S. Prusty 	if (!se)
79879f4e922SSubhransu S. Prusty 		return -ENOMEM;
79918382eadSSubhransu S. Prusty 
800*754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
80179f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
80279f4e922SSubhransu S. Prusty 	if (!kc->name)
80379f4e922SSubhransu S. Prusty 		return -ENOMEM;
80418382eadSSubhransu S. Prusty 
80579f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
80679f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
80779f4e922SSubhransu S. Prusty 	kc->access = 0;
80879f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
809*754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
81079f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
81179f4e922SSubhransu S. Prusty 
81279f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
81379f4e922SSubhransu S. Prusty 
81479f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
81579f4e922SSubhransu S. Prusty 	se->items = num_items;
81679f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
81779f4e922SSubhransu S. Prusty 
81879f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
81979f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
82079f4e922SSubhransu S. Prusty 	if (!items[i])
82179f4e922SSubhransu S. Prusty 		return -ENOMEM;
82279f4e922SSubhransu S. Prusty 
82379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
82479f4e922SSubhransu S. Prusty 		i++;
82579f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
82679f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
82779f4e922SSubhransu S. Prusty 		if (!items[i])
82879f4e922SSubhransu S. Prusty 			return -ENOMEM;
82979f4e922SSubhransu S. Prusty 	}
83079f4e922SSubhransu S. Prusty 
83179f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
83279f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
83379f4e922SSubhransu S. Prusty 	if (!se->texts)
83479f4e922SSubhransu S. Prusty 		return -ENOMEM;
83579f4e922SSubhransu S. Prusty 
83679f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
837*754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
838c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
839c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
84079f4e922SSubhransu S. Prusty }
84179f4e922SSubhransu S. Prusty 
84279f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
84379f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
84479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
84579f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
84679f4e922SSubhransu S. Prusty {
84779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
84879f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
84979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
850*754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
85179f4e922SSubhransu S. Prusty 	int i, j;
85279f4e922SSubhransu S. Prusty 
853*754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
85479f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
85579f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
85679f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
85779f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
85879f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
85979f4e922SSubhransu S. Prusty 					se->texts[j + 1],
86079f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
86179f4e922SSubhransu S. Prusty 
86279f4e922SSubhransu S. Prusty 			rindex++;
86379f4e922SSubhransu S. Prusty 		}
86479f4e922SSubhransu S. Prusty 
86579f4e922SSubhransu S. Prusty 		mux_index++;
86679f4e922SSubhransu S. Prusty 	}
86779f4e922SSubhransu S. Prusty }
86879f4e922SSubhransu S. Prusty 
86979f4e922SSubhransu S. Prusty /*
87079f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
87179f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
872*754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
873*754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
87479f4e922SSubhransu S. Prusty  *
875*754695f9SJeeja KP  * For each port, one Mux and One output widget is added
876*754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
87779f4e922SSubhransu S. Prusty  *
87879f4e922SSubhransu S. Prusty  * Routes are added as below:
879*754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
880*754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
88179f4e922SSubhransu S. Prusty  *
88279f4e922SSubhransu S. Prusty  * Total route elements:
883*754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
88479f4e922SSubhransu S. Prusty  */
88579f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
88679f4e922SSubhransu S. Prusty {
88779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
88879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
88979f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
89079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
89179f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
89279f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
89379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
89479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
895*754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
89679f4e922SSubhransu S. Prusty 
89779f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
89879f4e922SSubhransu S. Prusty 		return -EINVAL;
89979f4e922SSubhransu S. Prusty 
900*754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
901*754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
90279f4e922SSubhransu S. Prusty 				GFP_KERNEL);
90379f4e922SSubhransu S. Prusty 
90479f4e922SSubhransu S. Prusty 	if (!widgets)
90579f4e922SSubhransu S. Prusty 		return -ENOMEM;
90679f4e922SSubhransu S. Prusty 
90779f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
90879f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
90979f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
91079f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
911c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
912c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
913c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
914c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
91579f4e922SSubhransu S. Prusty 		if (ret < 0)
91679f4e922SSubhransu S. Prusty 			return ret;
91779f4e922SSubhransu S. Prusty 		i++;
91879f4e922SSubhransu S. Prusty 	}
91979f4e922SSubhransu S. Prusty 
92079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
921*754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
922*754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
923*754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
92479f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
925*754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
926c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
927c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
928*754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
929*754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
93079f4e922SSubhransu S. Prusty 			if (ret < 0)
93179f4e922SSubhransu S. Prusty 				return ret;
93279f4e922SSubhransu S. Prusty 			i++;
93379f4e922SSubhransu S. Prusty 		}
934*754695f9SJeeja KP 	}
93579f4e922SSubhransu S. Prusty 
93679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
93779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
938*754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
939*754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
940*754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
941*754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
942*754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
94379f4e922SSubhransu S. Prusty 						widget_name);
94479f4e922SSubhransu S. Prusty 			if (ret < 0)
94579f4e922SSubhransu S. Prusty 				return ret;
94679f4e922SSubhransu S. Prusty 			i++;
94779f4e922SSubhransu S. Prusty 
94879f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
94979f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
95079f4e922SSubhransu S. Prusty 
95179f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
95279f4e922SSubhransu S. Prusty 			num_routes++;
95379f4e922SSubhransu S. Prusty 		}
954*754695f9SJeeja KP 	}
95579f4e922SSubhransu S. Prusty 
95679f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
95779f4e922SSubhransu S. Prusty 							GFP_KERNEL);
95879f4e922SSubhransu S. Prusty 	if (!route)
95979f4e922SSubhransu S. Prusty 		return -ENOMEM;
96079f4e922SSubhransu S. Prusty 
96179f4e922SSubhransu S. Prusty 	i = 0;
96279f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
96379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
964*754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
96579f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
966*754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
967*754695f9SJeeja KP 						hdmi->num_pin;
96879f4e922SSubhransu S. Prusty 
96979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
97079f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
97179f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
97279f4e922SSubhransu S. Prusty 			i++;
973*754695f9SJeeja KP 		}
97479f4e922SSubhransu S. Prusty 	}
97579f4e922SSubhransu S. Prusty 
97679f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
97779f4e922SSubhransu S. Prusty 
97879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
979*754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
98079f4e922SSubhransu S. Prusty 
98179f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
98279f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
98379f4e922SSubhransu S. Prusty 
98479f4e922SSubhransu S. Prusty 	return 0;
98579f4e922SSubhransu S. Prusty 
98618382eadSSubhransu S. Prusty }
98718382eadSSubhransu S. Prusty 
98815b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
98918382eadSSubhransu S. Prusty {
99015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
991*754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
99215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
993148569fdSSubhransu S. Prusty 	int dai_id = 0;
99418382eadSSubhransu S. Prusty 
995148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
99615b91447SSubhransu S. Prusty 		return -EINVAL;
99718382eadSSubhransu S. Prusty 
998148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
999148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1000148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
100115b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
100218382eadSSubhransu S. Prusty 
1003148569fdSSubhransu S. Prusty 		dai_id++;
1004148569fdSSubhransu S. Prusty 
1005148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1006148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1007148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1008148569fdSSubhransu S. Prusty 			break;
1009148569fdSSubhransu S. Prusty 		}
1010148569fdSSubhransu S. Prusty 	}
101118382eadSSubhransu S. Prusty 
101215b91447SSubhransu S. Prusty 	return 0;
101315b91447SSubhransu S. Prusty }
101415b91447SSubhransu S. Prusty 
101515b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
101615b91447SSubhransu S. Prusty {
101715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
101815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10194a3478deSJeeja KP 	char name[NAME_SIZE];
102015b91447SSubhransu S. Prusty 
102115b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
102215b91447SSubhransu S. Prusty 	if (!cvt)
102315b91447SSubhransu S. Prusty 		return -ENOMEM;
102415b91447SSubhransu S. Prusty 
102515b91447SSubhransu S. Prusty 	cvt->nid = nid;
10264a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10274a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
102815b91447SSubhransu S. Prusty 
102915b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
103015b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
103115b91447SSubhransu S. Prusty 
103215b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
103315b91447SSubhransu S. Prusty }
103415b91447SSubhransu S. Prusty 
1035f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1036*754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1037b7756edeSSubhransu S. Prusty {
1038f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1039f6fa11a3SSandeep Tayal 
1040*754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1041f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1042f6fa11a3SSandeep Tayal 
1043f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1044f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1045f6fa11a3SSandeep Tayal 		return -EINVAL;
1046b7756edeSSubhransu S. Prusty 	}
1047b7756edeSSubhransu S. Prusty 
1048*754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1049f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1050f6fa11a3SSandeep Tayal 
1051f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1052f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1053f6fa11a3SSandeep Tayal 		return -EINVAL;
1054f6fa11a3SSandeep Tayal 	}
1055f6fa11a3SSandeep Tayal 
1056*754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1057f6fa11a3SSandeep Tayal 
1058f6fa11a3SSandeep Tayal 	return 0;
1059f6fa11a3SSandeep Tayal }
1060f6fa11a3SSandeep Tayal 
1061*754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1062*754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1063b8a54545SSubhransu S. Prusty {
1064b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10654a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10664a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1067*754695f9SJeeja KP 	int size = 0;
1068*754695f9SJeeja KP 
1069*754695f9SJeeja KP 	if (!hdmi)
1070*754695f9SJeeja KP 		return;
10714a3478deSJeeja KP 
10724a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1073*754695f9SJeeja KP 	port->eld.monitor_present = false;
1074f6fa11a3SSandeep Tayal 
1075f6fa11a3SSandeep Tayal 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, -1,
1076*754695f9SJeeja KP 				&port->eld.monitor_present,
1077*754695f9SJeeja KP 				port->eld.eld_buffer,
1078f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1079f6fa11a3SSandeep Tayal 
1080f6fa11a3SSandeep Tayal 	if (size > 0) {
1081f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1082*754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1083f6fa11a3SSandeep Tayal 			size = -EINVAL;
1084f6fa11a3SSandeep Tayal 	}
1085f6fa11a3SSandeep Tayal 
1086f6fa11a3SSandeep Tayal 	if (size > 0) {
1087*754695f9SJeeja KP 		port->eld.eld_valid = true;
1088*754695f9SJeeja KP 		port->eld.eld_size = size;
1089f6fa11a3SSandeep Tayal 	} else {
1090*754695f9SJeeja KP 		port->eld.eld_valid = false;
1091*754695f9SJeeja KP 		port->eld.eld_size = 0;
1092f6fa11a3SSandeep Tayal 	}
1093b8a54545SSubhransu S. Prusty 
1094*754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
10954a3478deSJeeja KP 
1096*754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1097b8a54545SSubhransu S. Prusty 
1098*754695f9SJeeja KP 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1099*754695f9SJeeja KP 						__func__, pin->nid, port->id);
11004a3478deSJeeja KP 
11014a3478deSJeeja KP 		/*
11024a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
11034a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
11044a3478deSJeeja KP 		 * control select.
11054a3478deSJeeja KP 		 */
11064a3478deSJeeja KP 		if (pcm) {
11074a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11084a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
11094a3478deSJeeja KP 
11104a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
11114a3478deSJeeja KP 		}
11124a3478deSJeeja KP 
11134a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1114f6fa11a3SSandeep Tayal 		return;
1115b8a54545SSubhransu S. Prusty 	}
1116b8a54545SSubhransu S. Prusty 
1117*754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
11184a3478deSJeeja KP 		if (pcm) {
11194a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11204a3478deSJeeja KP 				"jack report for pcm=%d\n",
11214a3478deSJeeja KP 				pcm->pcm_id);
11224a3478deSJeeja KP 
11234a3478deSJeeja KP 			snd_jack_report(pcm->jack, SND_JACK_AVOUT);
11244a3478deSJeeja KP 		}
11254a3478deSJeeja KP 
1126f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1127*754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1128*754695f9SJeeja KP 
1129*754695f9SJeeja KP 	}
1130*754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
11314a3478deSJeeja KP }
11324a3478deSJeeja KP 
1133*754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1134*754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1135*754695f9SJeeja KP {
1136*754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1137*754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1138*754695f9SJeeja KP 	int i;
1139*754695f9SJeeja KP 
1140*754695f9SJeeja KP 	/*
1141*754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1142*754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1143*754695f9SJeeja KP 	 * implemented.
1144*754695f9SJeeja KP 	 */
1145*754695f9SJeeja KP 
1146*754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1147*754695f9SJeeja KP 	if (!ports)
1148*754695f9SJeeja KP 		return -ENOMEM;
1149*754695f9SJeeja KP 
1150*754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1151*754695f9SJeeja KP 		ports[i].id = i;
1152*754695f9SJeeja KP 		ports[i].pin = pin;
1153*754695f9SJeeja KP 	}
1154*754695f9SJeeja KP 	pin->ports = ports;
1155*754695f9SJeeja KP 	pin->num_ports = max_ports;
1156*754695f9SJeeja KP 	return 0;
1157b8a54545SSubhransu S. Prusty }
1158b8a54545SSubhransu S. Prusty 
115915b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
116015b91447SSubhransu S. Prusty {
116115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
116215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1163*754695f9SJeeja KP 	int ret;
116415b91447SSubhransu S. Prusty 
116515b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
116615b91447SSubhransu S. Prusty 	if (!pin)
116715b91447SSubhransu S. Prusty 		return -ENOMEM;
116815b91447SSubhransu S. Prusty 
116915b91447SSubhransu S. Prusty 	pin->nid = nid;
1170*754695f9SJeeja KP 	pin->edev = edev;
1171*754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1172*754695f9SJeeja KP 	if (ret < 0)
1173*754695f9SJeeja KP 		return ret;
117415b91447SSubhransu S. Prusty 
117515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
117615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1177*754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1178b8a54545SSubhransu S. Prusty 
117915b91447SSubhransu S. Prusty 	return 0;
118018382eadSSubhransu S. Prusty }
118118382eadSSubhransu S. Prusty 
1182211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1183211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1184211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1185211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1186211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1187211caab7SSubhransu S. Prusty 
1188211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1189211caab7SSubhransu S. Prusty {
1190211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1191211caab7SSubhransu S. Prusty 
1192211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1193211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1194211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1195211caab7SSubhransu S. Prusty 		return;
1196211caab7SSubhransu S. Prusty 
1197211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1198211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1199211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1200211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1201211caab7SSubhransu S. Prusty 		return;
1202211caab7SSubhransu S. Prusty }
1203211caab7SSubhransu S. Prusty 
1204211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1205211caab7SSubhransu S. Prusty {
1206211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1207211caab7SSubhransu S. Prusty 
1208211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1209211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1210211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1211211caab7SSubhransu S. Prusty 		return;
1212211caab7SSubhransu S. Prusty 
1213211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1214211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1215211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1216211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1217211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1218211caab7SSubhransu S. Prusty 		return;
1219211caab7SSubhransu S. Prusty 
1220211caab7SSubhransu S. Prusty }
1221211caab7SSubhransu S. Prusty 
122217a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
122317a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
122417a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
122517a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1226c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
122717a42c45SSubhransu S. Prusty };
122817a42c45SSubhransu S. Prusty 
122917a42c45SSubhransu S. Prusty /*
123017a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
123117a42c45SSubhransu S. Prusty  * based on the number of converter queried.
123217a42c45SSubhransu S. Prusty  */
123317a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
123417a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
123517a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
123617a42c45SSubhransu S. Prusty {
123717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
123817a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
123917a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
124017a42c45SSubhransu S. Prusty 	int i = 0;
124117a42c45SSubhransu S. Prusty 	u32 rates, bps;
124217a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
124317a42c45SSubhransu S. Prusty 	u64 formats;
124417a42c45SSubhransu S. Prusty 	int ret;
124517a42c45SSubhransu S. Prusty 
124617a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
124717a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
124817a42c45SSubhransu S. Prusty 			GFP_KERNEL);
124917a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
125017a42c45SSubhransu S. Prusty 		return -ENOMEM;
125117a42c45SSubhransu S. Prusty 
125217a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
125317a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
125417a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
125517a42c45SSubhransu S. Prusty 		if (ret)
125617a42c45SSubhransu S. Prusty 			return ret;
125717a42c45SSubhransu S. Prusty 
125817a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
125917a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
126017a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
126117a42c45SSubhransu S. Prusty 
126217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
126317a42c45SSubhransu S. Prusty 			return -ENOMEM;
126417a42c45SSubhransu S. Prusty 
126517a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
126617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
126717a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
126817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
126917a42c45SSubhransu S. Prusty 			return -ENOMEM;
127017a42c45SSubhransu S. Prusty 
127117a42c45SSubhransu S. Prusty 		/*
127217a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
127317a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
127417a42c45SSubhransu S. Prusty 		 */
127517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
127617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
127717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
127817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
127917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
128017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
128117a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
128217a42c45SSubhransu S. Prusty 
128317a42c45SSubhransu S. Prusty 		i++;
128417a42c45SSubhransu S. Prusty 	}
128517a42c45SSubhransu S. Prusty 
128617a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
128717a42c45SSubhransu S. Prusty 
128817a42c45SSubhransu S. Prusty 	return 0;
128917a42c45SSubhransu S. Prusty }
129017a42c45SSubhransu S. Prusty 
129118382eadSSubhransu S. Prusty /*
129218382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
129318382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
129418382eadSSubhransu S. Prusty  */
129517a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
129617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
129718382eadSSubhransu S. Prusty {
129818382eadSSubhransu S. Prusty 	hda_nid_t nid;
12993c83ac23SSudip Mukherjee 	int i, num_nodes;
130018382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
130118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
130215b91447SSubhransu S. Prusty 	int ret;
130318382eadSSubhransu S. Prusty 
1304211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1305211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1306211caab7SSubhransu S. Prusty 
13073c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1308541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
130918382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
131018382eadSSubhransu S. Prusty 		return -EINVAL;
131118382eadSSubhransu S. Prusty 	}
131218382eadSSubhransu S. Prusty 
13133c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
131418382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
131518382eadSSubhransu S. Prusty 
131618382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
131718382eadSSubhransu S. Prusty 		unsigned int caps;
131818382eadSSubhransu S. Prusty 		unsigned int type;
131918382eadSSubhransu S. Prusty 
132018382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
132118382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
132218382eadSSubhransu S. Prusty 
132318382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
132418382eadSSubhransu S. Prusty 			continue;
132518382eadSSubhransu S. Prusty 
132618382eadSSubhransu S. Prusty 		switch (type) {
132718382eadSSubhransu S. Prusty 
132818382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
132915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
133015b91447SSubhransu S. Prusty 			if (ret < 0)
133115b91447SSubhransu S. Prusty 				return ret;
133218382eadSSubhransu S. Prusty 			break;
133318382eadSSubhransu S. Prusty 
133418382eadSSubhransu S. Prusty 		case AC_WID_PIN:
133515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
133615b91447SSubhransu S. Prusty 			if (ret < 0)
133715b91447SSubhransu S. Prusty 				return ret;
133818382eadSSubhransu S. Prusty 			break;
133918382eadSSubhransu S. Prusty 		}
134018382eadSSubhransu S. Prusty 	}
134118382eadSSubhransu S. Prusty 
134218382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
134318382eadSSubhransu S. Prusty 
134415b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
134518382eadSSubhransu S. Prusty 		return -EIO;
134618382eadSSubhransu S. Prusty 
134717a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
134817a42c45SSubhransu S. Prusty 	if (ret) {
134917a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
135017a42c45SSubhransu S. Prusty 							ret);
135117a42c45SSubhransu S. Prusty 		return ret;
135217a42c45SSubhransu S. Prusty 	}
135317a42c45SSubhransu S. Prusty 
135417a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
135517a42c45SSubhransu S. Prusty 
135615b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
135718382eadSSubhransu S. Prusty }
135818382eadSSubhransu S. Prusty 
1359f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1360b8a54545SSubhransu S. Prusty {
1361b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1362b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1363*754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1364*754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1365b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1366b8a54545SSubhransu S. Prusty 
1367b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1368b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1369b8a54545SSubhransu S. Prusty 
1370*754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1371*754695f9SJeeja KP 							pin_nid, pipe);
1372b8a54545SSubhransu S. Prusty 
1373b8a54545SSubhransu S. Prusty 	/*
1374b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1375b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1376b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1377b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1378b8a54545SSubhransu S. Prusty 	 */
1379b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1380b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1381b8a54545SSubhransu S. Prusty 		return;
1382b8a54545SSubhransu S. Prusty 
1383b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1384b8a54545SSubhransu S. Prusty 		return;
1385b8a54545SSubhransu S. Prusty 
1386b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1387*754695f9SJeeja KP 		if (pin->nid != pin_nid)
1388*754695f9SJeeja KP 			continue;
1389*754695f9SJeeja KP 
1390*754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1391*754695f9SJeeja KP 		if (pipe == -1) {
1392*754695f9SJeeja KP 			/* if not MST, default is port[0] */
1393*754695f9SJeeja KP 			hport = &pin->ports[0];
1394*754695f9SJeeja KP 			break;
1395b8a54545SSubhransu S. Prusty 		}
1396b8a54545SSubhransu S. Prusty 	}
1397b8a54545SSubhransu S. Prusty 
1398*754695f9SJeeja KP 	if (hport)
1399*754695f9SJeeja KP 		hdac_hdmi_present_sense(pin, hport);
1400*754695f9SJeeja KP }
1401*754695f9SJeeja KP 
1402b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1403b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1404b8a54545SSubhransu S. Prusty };
1405b8a54545SSubhransu S. Prusty 
14062889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
14072889099eSSubhransu S. Prusty 						int device)
14082889099eSSubhransu S. Prusty {
14092889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
14102889099eSSubhransu S. Prusty 
14112889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
14122889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
14132889099eSSubhransu S. Prusty 			return rtd->pcm;
14142889099eSSubhransu S. Prusty 	}
14152889099eSSubhransu S. Prusty 
14162889099eSSubhransu S. Prusty 	return NULL;
14172889099eSSubhransu S. Prusty }
14182889099eSSubhransu S. Prusty 
14194a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
14204a3478deSJeeja KP {
14214a3478deSJeeja KP 	char jack_name[NAME_SIZE];
14224a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
14234a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
14244a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
14254a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
14264a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
14274a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
14282889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
14292889099eSSubhransu S. Prusty 	int err;
14304a3478deSJeeja KP 
14314a3478deSJeeja KP 	/*
14324a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
14334a3478deSJeeja KP 	 * add to the pcm list
14344a3478deSJeeja KP 	 */
14354a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
14364a3478deSJeeja KP 	if (!pcm)
14374a3478deSJeeja KP 		return -ENOMEM;
14384a3478deSJeeja KP 	pcm->pcm_id = device;
14394a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1440ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
14414a3478deSJeeja KP 
14422889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
14432889099eSSubhransu S. Prusty 	if (snd_pcm) {
14442889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
14452889099eSSubhransu S. Prusty 		if (err < 0) {
14462889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
14472889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
14482889099eSSubhransu S. Prusty 				err, device);
14492889099eSSubhransu S. Prusty 			kfree(pcm);
14502889099eSSubhransu S. Prusty 			return err;
14512889099eSSubhransu S. Prusty 		}
14522889099eSSubhransu S. Prusty 	}
14532889099eSSubhransu S. Prusty 
14544a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
14554a3478deSJeeja KP 
14564a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
14574a3478deSJeeja KP 
14584a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
14594a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
14604a3478deSJeeja KP }
14614a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
14624a3478deSJeeja KP 
146318382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
146418382eadSSubhransu S. Prusty {
146518382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
146618382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
146718382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
146818382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1469b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1470b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1471*754695f9SJeeja KP 	int ret, i;
147218382eadSSubhransu S. Prusty 
147318382eadSSubhransu S. Prusty 	edev->scodec = codec;
147418382eadSSubhransu S. Prusty 
1475b2047e99SVinod Koul 	/*
1476b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1477b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1478b2047e99SVinod Koul 	 */
1479b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1480500e06b9SVinod Koul 	if (!hlink) {
1481500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1482500e06b9SVinod Koul 		return -EIO;
1483500e06b9SVinod Koul 	}
1484500e06b9SVinod Koul 
1485b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1486b2047e99SVinod Koul 
148779f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
148879f4e922SSubhransu S. Prusty 	if (ret < 0)
148979f4e922SSubhransu S. Prusty 		return ret;
149018382eadSSubhransu S. Prusty 
1491b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1492b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1493b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1494b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1495b8a54545SSubhransu S. Prusty 				ret);
1496b8a54545SSubhransu S. Prusty 		return ret;
1497b8a54545SSubhransu S. Prusty 	}
1498b8a54545SSubhransu S. Prusty 
1499b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1500*754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1501*754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1502b8a54545SSubhransu S. Prusty 
150318382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
150418382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
150518382eadSSubhransu S. Prusty 
1506e342ac08SSubhransu S. Prusty 	/*
1507e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1508e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1509e342ac08SSubhransu S. Prusty 	 */
1510e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1511e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1512e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1513e342ac08SSubhransu S. Prusty 
1514e342ac08SSubhransu S. Prusty 	return 0;
1515e342ac08SSubhransu S. Prusty }
1516e342ac08SSubhransu S. Prusty 
1517e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1518e342ac08SSubhransu S. Prusty {
1519e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1520e342ac08SSubhransu S. Prusty 
1521e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
152218382eadSSubhransu S. Prusty 	return 0;
152318382eadSSubhransu S. Prusty }
152418382eadSSubhransu S. Prusty 
1525571d5078SJeeja KP #ifdef CONFIG_PM
15261b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
15271b377ccdSSubhransu S. Prusty {
15281b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
15291b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
15301b377ccdSSubhransu S. Prusty 
15311b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
15321b377ccdSSubhransu S. Prusty 
15331b377ccdSSubhransu S. Prusty 	/*
15341b377ccdSSubhransu S. Prusty 	 * Power down afg.
15351b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
15361b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
15371b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
15381b377ccdSSubhransu S. Prusty 	 * to read the state.
15391b377ccdSSubhransu S. Prusty 	 */
15401b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15411b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
15421b377ccdSSubhransu S. Prusty 
15431b377ccdSSubhransu S. Prusty 	return 0;
15441b377ccdSSubhransu S. Prusty }
15451b377ccdSSubhransu S. Prusty 
15460fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1547571d5078SJeeja KP {
15480fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1549571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1550571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1551571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
1552*754695f9SJeeja KP 	int i;
15531b377ccdSSubhransu S. Prusty 
15541b377ccdSSubhransu S. Prusty 	/* Power up afg */
15551b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15561b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1557571d5078SJeeja KP 
1558571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1559571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1560571d5078SJeeja KP 
1561571d5078SJeeja KP 	/*
1562571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1563571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1564571d5078SJeeja KP 	 * all pins here.
1565571d5078SJeeja KP 	 */
1566571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1567*754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1568*754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1569571d5078SJeeja KP 
15701b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1571571d5078SJeeja KP }
1572571d5078SJeeja KP #else
15731b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
15740fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1575571d5078SJeeja KP #endif
1576571d5078SJeeja KP 
157718382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
157818382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1579e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
158018382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
158118382eadSSubhransu S. Prusty };
158218382eadSSubhransu S. Prusty 
15832889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
15842889099eSSubhransu S. Prusty 					unsigned char *chmap)
15852889099eSSubhransu S. Prusty {
15862889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15872889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15882889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
15892889099eSSubhransu S. Prusty 
1590ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
15912889099eSSubhransu S. Prusty }
15922889099eSSubhransu S. Prusty 
15932889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
15942889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
15952889099eSSubhransu S. Prusty {
15962889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15972889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15982889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1599*754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16002889099eSSubhransu S. Prusty 
1601ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1602ab1eea19SJeeja KP 	pcm->chmap_set = true;
1603ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
16042889099eSSubhransu S. Prusty 	if (prepared)
1605*754695f9SJeeja KP 		hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1606ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
16072889099eSSubhransu S. Prusty }
16082889099eSSubhransu S. Prusty 
16092889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
16102889099eSSubhransu S. Prusty {
16112889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16122889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16132889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1614*754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16152889099eSSubhransu S. Prusty 
1616*754695f9SJeeja KP 	return port ? true:false;
16172889099eSSubhransu S. Prusty }
16182889099eSSubhransu S. Prusty 
16192889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
16202889099eSSubhransu S. Prusty {
16212889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16222889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16232889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1624*754695f9SJeeja KP 	struct hdac_hdmi_port *port = pcm->port;
16252889099eSSubhransu S. Prusty 
1626*754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
16272889099eSSubhransu S. Prusty 		return 0;
16282889099eSSubhransu S. Prusty 
1629*754695f9SJeeja KP 	return port->eld.info.spk_alloc;
16302889099eSSubhransu S. Prusty }
16312889099eSSubhransu S. Prusty 
163218382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
163318382eadSSubhransu S. Prusty {
163418382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
163518382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
163617a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1637b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
163817a42c45SSubhransu S. Prusty 	int num_dais = 0;
163918382eadSSubhransu S. Prusty 	int ret = 0;
164018382eadSSubhransu S. Prusty 
1641b2047e99SVinod Koul 	/* hold the ref while we probe */
1642b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1643500e06b9SVinod Koul 	if (!hlink) {
1644500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1645500e06b9SVinod Koul 		return -EIO;
1646500e06b9SVinod Koul 	}
1647500e06b9SVinod Koul 
1648b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1649b2047e99SVinod Koul 
165018382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
165118382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
165218382eadSSubhransu S. Prusty 		return -ENOMEM;
165318382eadSSubhransu S. Prusty 
165418382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1655bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
16562889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
16572889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
16582889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
16592889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
166018382eadSSubhransu S. Prusty 
166118382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
166218382eadSSubhransu S. Prusty 
166315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
166415b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
16654a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
16664a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
166715b91447SSubhransu S. Prusty 
1668aeaccef0SRamesh Babu 	/*
1669aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1670aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1671aeaccef0SRamesh Babu 	 */
1672aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1673aeaccef0SRamesh Babu 	if (ret < 0) {
1674aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1675aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1676aeaccef0SRamesh Babu 			ret);
1677aeaccef0SRamesh Babu 		return ret;
1678aeaccef0SRamesh Babu 	}
1679aeaccef0SRamesh Babu 
168017a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
168117a42c45SSubhransu S. Prusty 	if (ret < 0) {
168217a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
168317a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
168418382eadSSubhransu S. Prusty 		return ret;
168517a42c45SSubhransu S. Prusty 	}
168618382eadSSubhransu S. Prusty 
168718382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1688b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
168917a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1690b2047e99SVinod Koul 
1691b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1692b2047e99SVinod Koul 
1693b2047e99SVinod Koul 	return ret;
169418382eadSSubhransu S. Prusty }
169518382eadSSubhransu S. Prusty 
169618382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
169718382eadSSubhransu S. Prusty {
169815b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
169915b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
170015b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
17014a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
1702*754695f9SJeeja KP 	int i;
170315b91447SSubhransu S. Prusty 
170418382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
170518382eadSSubhransu S. Prusty 
17064a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
17074a3478deSJeeja KP 		pcm->cvt = NULL;
1708*754695f9SJeeja KP 		pcm->port = NULL;
17094a3478deSJeeja KP 		list_del(&pcm->head);
17104a3478deSJeeja KP 		kfree(pcm);
17114a3478deSJeeja KP 	}
17124a3478deSJeeja KP 
171315b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
171415b91447SSubhransu S. Prusty 		list_del(&cvt->head);
17154a3478deSJeeja KP 		kfree(cvt->name);
171615b91447SSubhransu S. Prusty 		kfree(cvt);
171715b91447SSubhransu S. Prusty 	}
171815b91447SSubhransu S. Prusty 
171915b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1720*754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1721*754695f9SJeeja KP 			pin->ports[i].pin = NULL;
1722*754695f9SJeeja KP 		kfree(pin->ports);
172315b91447SSubhransu S. Prusty 		list_del(&pin->head);
172415b91447SSubhransu S. Prusty 		kfree(pin);
172515b91447SSubhransu S. Prusty 	}
172615b91447SSubhransu S. Prusty 
172718382eadSSubhransu S. Prusty 	return 0;
172818382eadSSubhransu S. Prusty }
172918382eadSSubhransu S. Prusty 
1730e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1731e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1732e342ac08SSubhransu S. Prusty {
1733e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1734e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
173507f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1736b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1737b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
173807f083abSSubhransu S. Prusty 	int err;
1739e342ac08SSubhransu S. Prusty 
1740e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1741e342ac08SSubhransu S. Prusty 
174207f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
174307f083abSSubhransu S. Prusty 	if (!bus)
174407f083abSSubhransu S. Prusty 		return 0;
174507f083abSSubhransu S. Prusty 
17461b377ccdSSubhransu S. Prusty 	/*
17471b377ccdSSubhransu S. Prusty 	 * Power down afg.
17481b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
17491b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
17501b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
17511b377ccdSSubhransu S. Prusty 	 * to read the state.
17521b377ccdSSubhransu S. Prusty 	 */
17531b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17541b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
175507f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
175607f083abSSubhransu S. Prusty 	if (err < 0) {
175707f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
175807f083abSSubhransu S. Prusty 		return err;
175907f083abSSubhransu S. Prusty 	}
176007f083abSSubhransu S. Prusty 
1761b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1762500e06b9SVinod Koul 	if (!hlink) {
1763500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1764500e06b9SVinod Koul 		return -EIO;
1765500e06b9SVinod Koul 	}
1766500e06b9SVinod Koul 
1767b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1768b2047e99SVinod Koul 
1769e342ac08SSubhransu S. Prusty 	return 0;
1770e342ac08SSubhransu S. Prusty }
1771e342ac08SSubhransu S. Prusty 
1772e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1773e342ac08SSubhransu S. Prusty {
1774e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1775e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
177607f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1777b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1778b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
177907f083abSSubhransu S. Prusty 	int err;
1780e342ac08SSubhransu S. Prusty 
1781e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1782e342ac08SSubhransu S. Prusty 
178307f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
178407f083abSSubhransu S. Prusty 	if (!bus)
178507f083abSSubhransu S. Prusty 		return 0;
178607f083abSSubhransu S. Prusty 
1787b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1788500e06b9SVinod Koul 	if (!hlink) {
1789500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1790500e06b9SVinod Koul 		return -EIO;
1791500e06b9SVinod Koul 	}
1792500e06b9SVinod Koul 
1793b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1794b2047e99SVinod Koul 
179507f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
179607f083abSSubhransu S. Prusty 	if (err < 0) {
179707f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
179807f083abSSubhransu S. Prusty 		return err;
179907f083abSSubhransu S. Prusty 	}
180007f083abSSubhransu S. Prusty 
1801ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1802ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1803ab85f5b3SSubhransu S. Prusty 
1804e342ac08SSubhransu S. Prusty 	/* Power up afg */
18051b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18061b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1807e342ac08SSubhransu S. Prusty 
1808e342ac08SSubhransu S. Prusty 	return 0;
1809e342ac08SSubhransu S. Prusty }
1810e342ac08SSubhransu S. Prusty #else
1811e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1812e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1813e342ac08SSubhransu S. Prusty #endif
1814e342ac08SSubhransu S. Prusty 
1815e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1816e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
18171b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
18180fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1819e342ac08SSubhransu S. Prusty };
1820e342ac08SSubhransu S. Prusty 
182118382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
182218382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1823e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1824cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
182518382eadSSubhransu S. Prusty 	{}
182618382eadSSubhransu S. Prusty };
182718382eadSSubhransu S. Prusty 
182818382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
182918382eadSSubhransu S. Prusty 
183018382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
183118382eadSSubhransu S. Prusty 	. hdac = {
183218382eadSSubhransu S. Prusty 		.driver = {
183318382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1834e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
183518382eadSSubhransu S. Prusty 		},
183618382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
183718382eadSSubhransu S. Prusty 	},
183818382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
183918382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
184018382eadSSubhransu S. Prusty };
184118382eadSSubhransu S. Prusty 
184218382eadSSubhransu S. Prusty static int __init hdmi_init(void)
184318382eadSSubhransu S. Prusty {
184418382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
184518382eadSSubhransu S. Prusty }
184618382eadSSubhransu S. Prusty 
184718382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
184818382eadSSubhransu S. Prusty {
184918382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
185018382eadSSubhransu S. Prusty }
185118382eadSSubhransu S. Prusty 
185218382eadSSubhransu S. Prusty module_init(hdmi_init);
185318382eadSSubhransu S. Prusty module_exit(hdmi_exit);
185418382eadSSubhransu S. Prusty 
185518382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
185618382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
185718382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
185818382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1859