xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 1de777fed54dfa93e166a3c934c5846920b86f0c)
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
45148569fdSSubhransu S. Prusty 
46b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
47b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
48b8a54545SSubhransu S. Prusty 
4918382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5018382eadSSubhransu S. Prusty 	unsigned int channels_min;
5118382eadSSubhransu S. Prusty 	unsigned int channels_max;
5218382eadSSubhransu S. Prusty 	u32 rates;
5318382eadSSubhransu S. Prusty 	u64 formats;
5418382eadSSubhransu S. Prusty 	unsigned int maxbps;
5518382eadSSubhransu S. Prusty };
5618382eadSSubhransu S. Prusty 
5718382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5815b91447SSubhransu S. Prusty 	struct list_head head;
5918382eadSSubhransu S. Prusty 	hda_nid_t nid;
604a3478deSJeeja KP 	const char *name;
6118382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6218382eadSSubhransu S. Prusty };
6318382eadSSubhransu S. Prusty 
64b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
65b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
66b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
67b7756edeSSubhransu S. Prusty };
68b7756edeSSubhransu S. Prusty 
69b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
70b8a54545SSubhransu S. Prusty 	bool	monitor_present;
71b8a54545SSubhransu S. Prusty 	bool	eld_valid;
72b8a54545SSubhransu S. Prusty 	int	eld_size;
73b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
74b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
75b8a54545SSubhransu S. Prusty };
76b8a54545SSubhransu S. Prusty 
7718382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7815b91447SSubhransu S. Prusty 	struct list_head head;
7918382eadSSubhransu S. Prusty 	hda_nid_t nid;
8018382eadSSubhransu S. Prusty 	int num_mux_nids;
8118382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
82b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
83b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev;
84b8a54545SSubhransu S. Prusty 	int repoll_count;
85b8a54545SSubhransu S. Prusty 	struct delayed_work work;
86bcced704SSubhransu S. Prusty 	struct mutex lock;
87bcced704SSubhransu S. Prusty 	bool chmap_set;
88bcced704SSubhransu S. Prusty 	unsigned char chmap[8]; /* ALSA API channel-map */
89bcced704SSubhransu S. Prusty 	int channels; /* current number of channels */
9018382eadSSubhransu S. Prusty };
9118382eadSSubhransu S. Prusty 
924a3478deSJeeja KP struct hdac_hdmi_pcm {
934a3478deSJeeja KP 	struct list_head head;
944a3478deSJeeja KP 	int pcm_id;
954a3478deSJeeja KP 	struct hdac_hdmi_pin *pin;
964a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
974a3478deSJeeja KP 	struct snd_jack *jack;
984a3478deSJeeja KP };
994a3478deSJeeja KP 
10018382eadSSubhransu S. Prusty struct hdac_hdmi_dai_pin_map {
10118382eadSSubhransu S. Prusty 	int dai_id;
10215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
10315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10418382eadSSubhransu S. Prusty };
10518382eadSSubhransu S. Prusty 
10618382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
107148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
10815b91447SSubhransu S. Prusty 	struct list_head pin_list;
10915b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1104a3478deSJeeja KP 	struct list_head pcm_list;
11115b91447SSubhransu S. Prusty 	int num_pin;
11215b91447SSubhransu S. Prusty 	int num_cvt;
1134a3478deSJeeja KP 	struct mutex pin_mutex;
114bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
11518382eadSSubhransu S. Prusty };
11618382eadSSubhransu S. Prusty 
117*1de777feSJeeja KP static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
118*1de777feSJeeja KP 			struct hdac_hdmi_dai_pin_map *dai_map);
119*1de777feSJeeja KP 
120*1de777feSJeeja KP static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
121*1de777feSJeeja KP 			struct hdac_hdmi_dai_pin_map *dai_map);
122*1de777feSJeeja KP 
1232889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
1242889099eSSubhransu S. Prusty 						int pcm_idx)
1252889099eSSubhransu S. Prusty {
1262889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
1272889099eSSubhransu S. Prusty 
1282889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
1292889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
1302889099eSSubhransu S. Prusty 			return pcm;
1312889099eSSubhransu S. Prusty 	}
1322889099eSSubhransu S. Prusty 
1332889099eSSubhransu S. Prusty 	return NULL;
1342889099eSSubhransu S. Prusty }
1352889099eSSubhransu S. Prusty 
136e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
137e342ac08SSubhransu S. Prusty {
13851b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
139e342ac08SSubhransu S. Prusty 
14051b2c425SGeliang Tang 	return to_ehdac_device(hdac);
141e342ac08SSubhransu S. Prusty }
142e342ac08SSubhransu S. Prusty 
1432428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1442428bca3SSubhransu S. Prusty {
1452428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1462428bca3SSubhransu S. Prusty }
1472428bca3SSubhransu S. Prusty 
1482428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1492428bca3SSubhransu S. Prusty {
1502428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1512428bca3SSubhransu S. Prusty }
1522428bca3SSubhransu S. Prusty 
1532428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1542428bca3SSubhransu S. Prusty 						void *eld)
1552428bca3SSubhransu S. Prusty {
1562428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1572428bca3SSubhransu S. Prusty 	int i;
1582428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1592428bca3SSubhransu S. Prusty 
1602428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1612428bca3SSubhransu S. Prusty 	if (!sad)
1622428bca3SSubhransu S. Prusty 		goto format_constraint;
1632428bca3SSubhransu S. Prusty 
1642428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1652428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1662428bca3SSubhransu S. Prusty 
1672428bca3SSubhransu S. Prusty 			/*
1682428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1692428bca3SSubhransu S. Prusty 			 * container so we set S32
1702428bca3SSubhransu S. Prusty 			 */
1712428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1722428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1732428bca3SSubhransu S. Prusty 		}
1742428bca3SSubhransu S. Prusty 	}
1752428bca3SSubhransu S. Prusty 
1762428bca3SSubhransu S. Prusty format_constraint:
1772428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1782428bca3SSubhransu S. Prusty 				formats);
1792428bca3SSubhransu S. Prusty 
1802428bca3SSubhransu S. Prusty }
1812428bca3SSubhransu S. Prusty 
182b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
183b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
184b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
185b8a54545SSubhransu S. Prusty {
186b8a54545SSubhransu S. Prusty 	unsigned int val;
187b8a54545SSubhransu S. Prusty 
188b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
189b8a54545SSubhransu S. Prusty 							byte_index);
190b8a54545SSubhransu S. Prusty 
191b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
192b8a54545SSubhransu S. Prusty 					byte_index, val);
193b8a54545SSubhransu S. Prusty 
194b8a54545SSubhransu S. Prusty 	return val;
195b8a54545SSubhransu S. Prusty }
196b8a54545SSubhransu S. Prusty 
197b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
198b8a54545SSubhransu S. Prusty {
199b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
200b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
201b8a54545SSubhransu S. Prusty }
202b8a54545SSubhransu S. Prusty 
203b8a54545SSubhransu S. Prusty /*
204b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
205b8a54545SSubhransu S. Prusty  * passed by user
206b8a54545SSubhransu S. Prusty  */
207b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
208b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
209b8a54545SSubhransu S. Prusty {
210b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
211b8a54545SSubhransu S. Prusty 
212b8a54545SSubhransu S. Prusty 	/*
213b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
214b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
215b8a54545SSubhransu S. Prusty 	 */
216b8a54545SSubhransu S. Prusty 
217b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
218b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
219b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
220b8a54545SSubhransu S. Prusty 		return -ERANGE;
221b8a54545SSubhransu S. Prusty 	}
222b8a54545SSubhransu S. Prusty 
223b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
224b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
225b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
226b8a54545SSubhransu S. Prusty 		/*
227b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
228b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
229b8a54545SSubhransu S. Prusty 		 */
230b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
231b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
232b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
233b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
234b8a54545SSubhransu S. Prusty 			goto error;
235b8a54545SSubhransu S. Prusty 		}
236b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
237b8a54545SSubhransu S. Prusty 		/*
238b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
239b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
240b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
241b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
242b8a54545SSubhransu S. Prusty 		 */
243b8a54545SSubhransu S. Prusty 		if (!val && !i) {
244b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
245b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
246b8a54545SSubhransu S. Prusty 			goto error;
247b8a54545SSubhransu S. Prusty 		}
248b8a54545SSubhransu S. Prusty 		buf[i] = val;
249b8a54545SSubhransu S. Prusty 	}
250b8a54545SSubhransu S. Prusty 
251b8a54545SSubhransu S. Prusty 	*eld_size = size;
252b8a54545SSubhransu S. Prusty error:
253b8a54545SSubhransu S. Prusty 	return ret;
254b8a54545SSubhransu S. Prusty }
255b8a54545SSubhransu S. Prusty 
256b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
257b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
258b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
259b0362adbSSubhransu S. Prusty {
260b0362adbSSubhransu S. Prusty 	unsigned int val;
261b0362adbSSubhransu S. Prusty 
262b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
263b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
264b0362adbSSubhransu S. Prusty 
265b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
266b0362adbSSubhransu S. Prusty 
267b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
268b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
269b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
270b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
271b0362adbSSubhransu S. Prusty 
272b0362adbSSubhransu S. Prusty 	return 0;
273b0362adbSSubhransu S. Prusty }
274b0362adbSSubhransu S. Prusty 
275a657f1d0SSubhransu S. Prusty static void
276a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
277a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
278a657f1d0SSubhransu S. Prusty {
279a657f1d0SSubhransu S. Prusty 	int val;
280a657f1d0SSubhransu S. Prusty 
281a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
282a657f1d0SSubhransu S. Prusty 
283a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
284a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
285a657f1d0SSubhransu S. Prusty }
286a657f1d0SSubhransu S. Prusty 
287478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
288478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
289478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
290478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
291478f544eSSubhransu S. Prusty 
292478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
293478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
294478f544eSSubhransu S. Prusty 	u8 CXT04;
295478f544eSSubhransu S. Prusty 	u8 CA;
296478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
297478f544eSSubhransu S. Prusty };
298478f544eSSubhransu S. Prusty 
299a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
300a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
301a657f1d0SSubhransu S. Prusty {
302a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
303a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
304478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
305478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
306478f544eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
307478f544eSSubhransu S. Prusty 	u8 *dip;
308a657f1d0SSubhransu S. Prusty 	int ret;
309a657f1d0SSubhransu S. Prusty 	int i;
310478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
311478f544eSSubhransu S. Prusty 	u8 conn_type;
312bcced704SSubhransu S. Prusty 	int channels, ca;
313a657f1d0SSubhransu S. Prusty 
314478f544eSSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
315478f544eSSubhransu S. Prusty 		if (pin->nid == pin_nid)
316478f544eSSubhransu S. Prusty 			break;
317478f544eSSubhransu S. Prusty 	}
318a657f1d0SSubhransu S. Prusty 
319bcced704SSubhransu S. Prusty 	ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
320bcced704SSubhransu S. Prusty 			pin->channels, pin->chmap_set, true, pin->chmap);
321bcced704SSubhransu S. Prusty 
322bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
323bcced704SSubhransu S. Prusty 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
324bcced704SSubhransu S. Prusty 
325bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
326bcced704SSubhransu S. Prusty 				pin->channels, pin->chmap, pin->chmap_set);
327bcced704SSubhransu S. Prusty 
328478f544eSSubhransu S. Prusty 	eld_buf = pin->eld.eld_buffer;
329478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
330a657f1d0SSubhransu S. Prusty 
331478f544eSSubhransu S. Prusty 	switch (conn_type) {
332478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
333478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
334478f544eSSubhransu S. Prusty 
335478f544eSSubhransu S. Prusty 		frame.channels = channels;
336bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
337a657f1d0SSubhransu S. Prusty 
338a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
339a657f1d0SSubhransu S. Prusty 		if (ret < 0)
340a657f1d0SSubhransu S. Prusty 			return ret;
341a657f1d0SSubhransu S. Prusty 
342478f544eSSubhransu S. Prusty 		break;
343478f544eSSubhransu S. Prusty 
344478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
345478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
346478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
347478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
348478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
349478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
350bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
351478f544eSSubhransu S. Prusty 
352478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
353478f544eSSubhransu S. Prusty 		break;
354478f544eSSubhransu S. Prusty 
355478f544eSSubhransu S. Prusty 	default:
356478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
357478f544eSSubhransu S. Prusty 						conn_type);
358478f544eSSubhransu S. Prusty 		return -EIO;
359478f544eSSubhransu S. Prusty 	}
360478f544eSSubhransu S. Prusty 
361a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
362a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
363a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
364a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
365a657f1d0SSubhransu S. Prusty 
366a657f1d0SSubhransu S. Prusty 
367a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
368a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
369478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
370391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
371a657f1d0SSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
372391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
373478f544eSSubhransu S. Prusty 	} else {
374478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
375478f544eSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
376478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
377478f544eSSubhransu S. Prusty 	}
378a657f1d0SSubhransu S. Prusty 
379a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
380a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
381a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
382a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
383a657f1d0SSubhransu S. Prusty 
384a657f1d0SSubhransu S. Prusty 	return 0;
385a657f1d0SSubhransu S. Prusty }
386a657f1d0SSubhransu S. Prusty 
387b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
388b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
389b0362adbSSubhransu S. Prusty {
390b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
39115b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
39215b91447SSubhransu S. Prusty 						pwr_state))
39315b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
394b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
395b0362adbSSubhransu S. Prusty 
396b0362adbSSubhransu S. Prusty 	/* Power up converter */
39715b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
39815b91447SSubhransu S. Prusty 						pwr_state))
39915b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
400b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
401b0362adbSSubhransu S. Prusty }
402b0362adbSSubhransu S. Prusty 
403b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
404b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
405b0362adbSSubhransu S. Prusty {
406b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
407b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
408b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
409bcced704SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
410b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
411a657f1d0SSubhransu S. Prusty 	int ret;
412b0362adbSSubhransu S. Prusty 
413b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
414bcced704SSubhransu S. Prusty 	pin = dai_map->pin;
415b0362adbSSubhransu S. Prusty 
416b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
417b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
418b0362adbSSubhransu S. Prusty 			dd->stream_tag,	dd->format);
419b0362adbSSubhransu S. Prusty 
420*1de777feSJeeja KP 	hdac_hdmi_enable_cvt(hdac, dai_map);
421*1de777feSJeeja KP 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
422*1de777feSJeeja KP 	if (ret < 0)
423*1de777feSJeeja KP 		return ret;
424bcced704SSubhransu S. Prusty 	mutex_lock(&pin->lock);
425bcced704SSubhransu S. Prusty 	pin->channels = substream->runtime->channels;
426bcced704SSubhransu S. Prusty 
42715b91447SSubhransu S. Prusty 	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
42815b91447SSubhransu S. Prusty 						dai_map->pin->nid);
429bcced704SSubhransu S. Prusty 	mutex_unlock(&pin->lock);
430a657f1d0SSubhransu S. Prusty 	if (ret < 0)
431a657f1d0SSubhransu S. Prusty 		return ret;
432a657f1d0SSubhransu S. Prusty 
43315b91447SSubhransu S. Prusty 	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
43415b91447SSubhransu S. Prusty 			dai_map->pin->nid, dd->stream_tag, dd->format);
435b0362adbSSubhransu S. Prusty }
436b0362adbSSubhransu S. Prusty 
437b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
438b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
439b0362adbSSubhransu S. Prusty {
440b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
44154dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
44254dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
44354dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
444b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
445b0362adbSSubhransu S. Prusty 
44654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
44754dfa1eaSSubhransu S. Prusty 	pin = dai_map->pin;
44854dfa1eaSSubhransu S. Prusty 
44954dfa1eaSSubhransu S. Prusty 	if (!pin)
45054dfa1eaSSubhransu S. Prusty 		return -ENODEV;
45154dfa1eaSSubhransu S. Prusty 
45254dfa1eaSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
45354dfa1eaSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
45454dfa1eaSSubhransu S. Prusty 								pin->nid);
455b0362adbSSubhransu S. Prusty 		return -ENODEV;
456b0362adbSSubhransu S. Prusty 	}
457b0362adbSSubhransu S. Prusty 
4586793a3d7SSubhransu S. Prusty 	dd = snd_soc_dai_get_dma_data(dai, substream);
4596793a3d7SSubhransu S. Prusty 	if (!dd) {
460b0362adbSSubhransu S. Prusty 		dd = kzalloc(sizeof(*dd), GFP_KERNEL);
4618d33ab24SSudip Mukherjee 		if (!dd)
4628d33ab24SSudip Mukherjee 			return -ENOMEM;
4636793a3d7SSubhransu S. Prusty 	}
4646793a3d7SSubhransu S. Prusty 
465b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
466b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
467b0362adbSSubhransu S. Prusty 			24, 0);
468b0362adbSSubhransu S. Prusty 
469b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
470b0362adbSSubhransu S. Prusty 
471b0362adbSSubhransu S. Prusty 	return 0;
472b0362adbSSubhransu S. Prusty }
473b0362adbSSubhransu S. Prusty 
474b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
475b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
476b0362adbSSubhransu S. Prusty {
477b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
478b0362adbSSubhransu S. Prusty 
479b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
480b0362adbSSubhransu S. Prusty 
4816793a3d7SSubhransu S. Prusty 	if (dd) {
4826793a3d7SSubhransu S. Prusty 		snd_soc_dai_set_dma_data(dai, substream, NULL);
483b0362adbSSubhransu S. Prusty 		kfree(dd);
4846793a3d7SSubhransu S. Prusty 	}
485b0362adbSSubhransu S. Prusty 
486b0362adbSSubhransu S. Prusty 	return 0;
487b0362adbSSubhransu S. Prusty }
488b0362adbSSubhransu S. Prusty 
489ab85f5b3SSubhransu S. Prusty static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
490ab85f5b3SSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
491ab85f5b3SSubhransu S. Prusty {
492ab85f5b3SSubhransu S. Prusty 	/* Enable transmission */
493ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
494ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_1, 1);
495ab85f5b3SSubhransu S. Prusty 
496ab85f5b3SSubhransu S. Prusty 	/* Category Code (CC) to zero */
497ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
498ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_2, 0);
499ab85f5b3SSubhransu S. Prusty }
500ab85f5b3SSubhransu S. Prusty 
501148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
502148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
503148569fdSSubhransu S. Prusty {
504148569fdSSubhransu S. Prusty 	int mux_idx;
505148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
506148569fdSSubhransu S. Prusty 
507148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
508148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
509148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
510148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
511148569fdSSubhransu S. Prusty 			break;
512148569fdSSubhransu S. Prusty 		}
513148569fdSSubhransu S. Prusty 	}
514148569fdSSubhransu S. Prusty 
515148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
516148569fdSSubhransu S. Prusty 		return -EIO;
517148569fdSSubhransu S. Prusty 
518148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
519148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
520148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
521148569fdSSubhransu S. Prusty 
522148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
523148569fdSSubhransu S. Prusty 
524148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
525148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
526148569fdSSubhransu S. Prusty 
527148569fdSSubhransu S. Prusty 	return 0;
528148569fdSSubhransu S. Prusty }
529148569fdSSubhransu S. Prusty 
530148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
531148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
532148569fdSSubhransu S. Prusty {
533148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
534148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
535148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
536148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
537148569fdSSubhransu S. Prusty 		return -EINVAL;
538148569fdSSubhransu S. Prusty 	}
539148569fdSSubhransu S. Prusty 
540148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
541148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
542148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
543148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
544148569fdSSubhransu S. Prusty 								pin->nid);
545148569fdSSubhransu S. Prusty 
546148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
547148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
548148569fdSSubhransu S. Prusty 
549148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
550148569fdSSubhransu S. Prusty }
551148569fdSSubhransu S. Prusty 
552148569fdSSubhransu S. Prusty /*
553148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
554148569fdSSubhransu S. Prusty  *
555148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
556148569fdSSubhransu S. Prusty  *
557148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
558148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
559148569fdSSubhransu S. Prusty  * connected.
560148569fdSSubhransu S. Prusty  */
561148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
562148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
563148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
564148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
565148569fdSSubhransu S. Prusty {
566148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
567148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
568148569fdSSubhransu S. Prusty 	int ret, i;
569148569fdSSubhransu S. Prusty 
570148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
571148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
572148569fdSSubhransu S. Prusty 			pin = pcm->pin;
573148569fdSSubhransu S. Prusty 			break;
574148569fdSSubhransu S. Prusty 		}
575148569fdSSubhransu S. Prusty 	}
576148569fdSSubhransu S. Prusty 
577148569fdSSubhransu S. Prusty 	if (pin) {
578148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
579148569fdSSubhransu S. Prusty 		if (ret < 0)
580148569fdSSubhransu S. Prusty 			return NULL;
581148569fdSSubhransu S. Prusty 
582148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
583148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
584148569fdSSubhransu S. Prusty 				return pin;
585148569fdSSubhransu S. Prusty 		}
586148569fdSSubhransu S. Prusty 	}
587148569fdSSubhransu S. Prusty 
588148569fdSSubhransu S. Prusty 	return NULL;
589148569fdSSubhransu S. Prusty }
590148569fdSSubhransu S. Prusty 
59154dfa1eaSSubhransu S. Prusty /*
59254dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
59354dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
59454dfa1eaSSubhransu S. Prusty  * doesn't fail.
59554dfa1eaSSubhransu S. Prusty  */
596b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
597b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
598b0362adbSSubhransu S. Prusty {
599b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
600b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
601b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
602148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
603148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
6042428bca3SSubhransu S. Prusty 	int ret;
605b0362adbSSubhransu S. Prusty 
606b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
607b0362adbSSubhransu S. Prusty 
608148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
609148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
61054dfa1eaSSubhransu S. Prusty 
61154dfa1eaSSubhransu S. Prusty 	/*
61254dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
61354dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
61454dfa1eaSSubhransu S. Prusty 	 */
615148569fdSSubhransu S. Prusty 	if (!pin)
61654dfa1eaSSubhransu S. Prusty 		return 0;
617148569fdSSubhransu S. Prusty 
618148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
619148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
620b0362adbSSubhransu S. Prusty 
62154dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
6223fb7b4e4SColin Ian King 			"Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
623148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
624b8a54545SSubhransu S. Prusty 
62554dfa1eaSSubhransu S. Prusty 		return 0;
626b0362adbSSubhransu S. Prusty 	}
627b0362adbSSubhransu S. Prusty 
628148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
629b0362adbSSubhransu S. Prusty 
6302428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
631148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
6322428bca3SSubhransu S. Prusty 	if (ret < 0)
6332428bca3SSubhransu S. Prusty 		return ret;
634b0362adbSSubhransu S. Prusty 
6352428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
636148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
637b0362adbSSubhransu S. Prusty }
638b0362adbSSubhransu S. Prusty 
639571d5078SJeeja KP static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
640571d5078SJeeja KP 		struct snd_soc_dai *dai)
641571d5078SJeeja KP {
642571d5078SJeeja KP 
643*1de777feSJeeja KP 	switch (cmd) {
644*1de777feSJeeja KP 	case SNDRV_PCM_TRIGGER_RESUME:
645*1de777feSJeeja KP 	case SNDRV_PCM_TRIGGER_START:
646*1de777feSJeeja KP 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
647571d5078SJeeja KP 		return hdac_hdmi_playback_prepare(substream, dai);
648*1de777feSJeeja KP 
649*1de777feSJeeja KP 	default:
650*1de777feSJeeja KP 		return 0;
651571d5078SJeeja KP 	}
652571d5078SJeeja KP 
653571d5078SJeeja KP 	return 0;
654571d5078SJeeja KP }
655571d5078SJeeja KP 
656b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
657b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
658b0362adbSSubhransu S. Prusty {
659b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
660b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
661b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
662b0362adbSSubhransu S. Prusty 
663b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
664b0362adbSSubhransu S. Prusty 
66554dfa1eaSSubhransu S. Prusty 	if (dai_map->pin) {
66654dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
66754dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
66854dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
66954dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
67054dfa1eaSSubhransu S. Prusty 
671b0362adbSSubhransu S. Prusty 		hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
672b0362adbSSubhransu S. Prusty 
67315b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
674b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
675148569fdSSubhransu S. Prusty 
676bcced704SSubhransu S. Prusty 		mutex_lock(&dai_map->pin->lock);
6772889099eSSubhransu S. Prusty 		dai_map->pin->chmap_set = false;
6782889099eSSubhransu S. Prusty 		memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
679bcced704SSubhransu S. Prusty 		dai_map->pin->channels = 0;
680bcced704SSubhransu S. Prusty 		mutex_unlock(&dai_map->pin->lock);
681bcced704SSubhransu S. Prusty 
682148569fdSSubhransu S. Prusty 		dai_map->pin = NULL;
683b0362adbSSubhransu S. Prusty 	}
68454dfa1eaSSubhransu S. Prusty }
685b0362adbSSubhransu S. Prusty 
68618382eadSSubhransu S. Prusty static int
68718382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
68818382eadSSubhransu S. Prusty {
689bcced704SSubhransu S. Prusty 	unsigned int chans;
690bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
691bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
69218382eadSSubhransu S. Prusty 	int err;
69318382eadSSubhransu S. Prusty 
694bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
695bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
696bcced704SSubhransu S. Prusty 
697bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
698bcced704SSubhransu S. Prusty 
699bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
700bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
701bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
70218382eadSSubhransu S. Prusty 
70318382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
70418382eadSSubhransu S. Prusty 			&cvt->params.rates,
70518382eadSSubhransu S. Prusty 			&cvt->params.formats,
70618382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
70718382eadSSubhransu S. Prusty 	if (err < 0)
70818382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
70918382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
71018382eadSSubhransu S. Prusty 			cvt->nid, err);
71118382eadSSubhransu S. Prusty 
71218382eadSSubhransu S. Prusty 	return err;
71318382eadSSubhransu S. Prusty }
71418382eadSSubhransu S. Prusty 
71579f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
71679f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
71779f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
71879f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
71979f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
72018382eadSSubhransu S. Prusty {
72118382eadSSubhransu S. Prusty 	w->id = id;
72279f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
72379f4e922SSubhransu S. Prusty 	if (!w->name)
72479f4e922SSubhransu S. Prusty 		return -ENOMEM;
72579f4e922SSubhransu S. Prusty 
72618382eadSSubhransu S. Prusty 	w->sname = stream;
72718382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
72818382eadSSubhransu S. Prusty 	w->shift = 0;
72979f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
73079f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
73179f4e922SSubhransu S. Prusty 	w->priv = priv;
73279f4e922SSubhransu S. Prusty 
73379f4e922SSubhransu S. Prusty 	return 0;
73418382eadSSubhransu S. Prusty }
73518382eadSSubhransu S. Prusty 
73618382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
73779f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
73879f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
73979f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
74018382eadSSubhransu S. Prusty {
74118382eadSSubhransu S. Prusty 	route->sink = sink;
74218382eadSSubhransu S. Prusty 	route->source = src;
74318382eadSSubhransu S. Prusty 	route->control = control;
74479f4e922SSubhransu S. Prusty 	route->connected = handler;
74518382eadSSubhransu S. Prusty }
74618382eadSSubhransu S. Prusty 
7474a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
7484a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
7494a3478deSJeeja KP {
7504a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7514a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7524a3478deSJeeja KP 
7534a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7544a3478deSJeeja KP 		if (pcm->pin == pin)
7554a3478deSJeeja KP 			return pcm;
7564a3478deSJeeja KP 	}
7574a3478deSJeeja KP 
7584a3478deSJeeja KP 	return NULL;
7594a3478deSJeeja KP }
7604a3478deSJeeja KP 
7614a3478deSJeeja KP /*
7624a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7634a3478deSJeeja KP  */
7644a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
7654a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7664a3478deSJeeja KP {
7674a3478deSJeeja KP 	int ret;
7684a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7694a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7704a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
7714a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
7724a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7734a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7744a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7754a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7764a3478deSJeeja KP 
7774a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7784a3478deSJeeja KP 	if (ret < 0)
7794a3478deSJeeja KP 		return ret;
7804a3478deSJeeja KP 
7814a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7824a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7834a3478deSJeeja KP 		if (pcm->pin == pin)
7844a3478deSJeeja KP 			pcm->pin = NULL;
7854a3478deSJeeja KP 
7864a3478deSJeeja KP 		/*
7874a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7884a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7894a3478deSJeeja KP 		 */
7904a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
7914a3478deSJeeja KP 			pcm->pin = pin;
7924a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
7934a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7944a3478deSJeeja KP 					"jack report for pcm=%d\n",
7954a3478deSJeeja KP 					pcm->pcm_id);
7964a3478deSJeeja KP 
7974a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
7984a3478deSJeeja KP 			}
7994a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
8004a3478deSJeeja KP 			return ret;
8014a3478deSJeeja KP 		}
8024a3478deSJeeja KP 	}
8034a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
8044a3478deSJeeja KP 
8054a3478deSJeeja KP 	return ret;
8064a3478deSJeeja KP }
8074a3478deSJeeja KP 
80879f4e922SSubhransu S. Prusty /*
80979f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
81079f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
81179f4e922SSubhransu S. Prusty  * widget runtime.
81279f4e922SSubhransu S. Prusty  *
81379f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
81479f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
81579f4e922SSubhransu S. Prusty  * "NONE"
81679f4e922SSubhransu S. Prusty  */
81779f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
81879f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
81979f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
82079f4e922SSubhransu S. Prusty 				const char *widget_name)
82118382eadSSubhransu S. Prusty {
82279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
82379f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
82479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
82579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
82679f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
82779f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
82879f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
82979f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
83079f4e922SSubhransu S. Prusty 	int i = 0;
83179f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
83218382eadSSubhransu S. Prusty 
83379f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
83479f4e922SSubhransu S. Prusty 	if (!kc)
83579f4e922SSubhransu S. Prusty 		return -ENOMEM;
83618382eadSSubhransu S. Prusty 
83779f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
83879f4e922SSubhransu S. Prusty 	if (!se)
83979f4e922SSubhransu S. Prusty 		return -ENOMEM;
84018382eadSSubhransu S. Prusty 
84179f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
84279f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
84379f4e922SSubhransu S. Prusty 	if (!kc->name)
84479f4e922SSubhransu S. Prusty 		return -ENOMEM;
84518382eadSSubhransu S. Prusty 
84679f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
84779f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
84879f4e922SSubhransu S. Prusty 	kc->access = 0;
84979f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
8504a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
85179f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
85279f4e922SSubhransu S. Prusty 
85379f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
85479f4e922SSubhransu S. Prusty 
85579f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
85679f4e922SSubhransu S. Prusty 	se->items = num_items;
85779f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
85879f4e922SSubhransu S. Prusty 
85979f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
86079f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
86179f4e922SSubhransu S. Prusty 	if (!items[i])
86279f4e922SSubhransu S. Prusty 		return -ENOMEM;
86379f4e922SSubhransu S. Prusty 
86479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
86579f4e922SSubhransu S. Prusty 		i++;
86679f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
86779f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
86879f4e922SSubhransu S. Prusty 		if (!items[i])
86979f4e922SSubhransu S. Prusty 			return -ENOMEM;
87079f4e922SSubhransu S. Prusty 	}
87179f4e922SSubhransu S. Prusty 
87279f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
87379f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
87479f4e922SSubhransu S. Prusty 	if (!se->texts)
87579f4e922SSubhransu S. Prusty 		return -ENOMEM;
87679f4e922SSubhransu S. Prusty 
87779f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
8784a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
87979f4e922SSubhransu S. Prusty }
88079f4e922SSubhransu S. Prusty 
88179f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
88279f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
88379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
88479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
88579f4e922SSubhransu S. Prusty {
88679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
88779f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
88879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
88979f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
89079f4e922SSubhransu S. Prusty 	int i, j;
89179f4e922SSubhransu S. Prusty 
89279f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
89379f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
89479f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
89579f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
89679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
89779f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
89879f4e922SSubhransu S. Prusty 					se->texts[j + 1],
89979f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
90079f4e922SSubhransu S. Prusty 
90179f4e922SSubhransu S. Prusty 			rindex++;
90279f4e922SSubhransu S. Prusty 		}
90379f4e922SSubhransu S. Prusty 
90479f4e922SSubhransu S. Prusty 		mux_index++;
90579f4e922SSubhransu S. Prusty 	}
90679f4e922SSubhransu S. Prusty }
90779f4e922SSubhransu S. Prusty 
90879f4e922SSubhransu S. Prusty /*
90979f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
91079f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
91179f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
91279f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
91379f4e922SSubhransu S. Prusty  *
91479f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
91579f4e922SSubhransu S. Prusty  *
91679f4e922SSubhransu S. Prusty  * Routes are added as below:
91779f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
91879f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
91979f4e922SSubhransu S. Prusty  *
92079f4e922SSubhransu S. Prusty  * Total route elements:
92179f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
92279f4e922SSubhransu S. Prusty  */
92379f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
92479f4e922SSubhransu S. Prusty {
92579f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
92679f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
92779f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
92879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
92979f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
93079f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
93179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
93279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
93379f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
93479f4e922SSubhransu S. Prusty 
93579f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
93679f4e922SSubhransu S. Prusty 		return -EINVAL;
93779f4e922SSubhransu S. Prusty 
93879f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
93979f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
94079f4e922SSubhransu S. Prusty 		GFP_KERNEL);
94179f4e922SSubhransu S. Prusty 
94279f4e922SSubhransu S. Prusty 	if (!widgets)
94379f4e922SSubhransu S. Prusty 		return -ENOMEM;
94479f4e922SSubhransu S. Prusty 
94579f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
94679f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
94779f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
94879f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
94979f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
95079f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
95179f4e922SSubhransu S. Prusty 		if (ret < 0)
95279f4e922SSubhransu S. Prusty 			return ret;
95379f4e922SSubhransu S. Prusty 		i++;
95479f4e922SSubhransu S. Prusty 	}
95579f4e922SSubhransu S. Prusty 
95679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
95779f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
95879f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
95979f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
96079f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
96179f4e922SSubhransu S. Prusty 		if (ret < 0)
96279f4e922SSubhransu S. Prusty 			return ret;
96379f4e922SSubhransu S. Prusty 		i++;
96479f4e922SSubhransu S. Prusty 	}
96579f4e922SSubhransu S. Prusty 
96679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
96779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
96879f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
96979f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
97079f4e922SSubhransu S. Prusty 							widget_name);
97179f4e922SSubhransu S. Prusty 		if (ret < 0)
97279f4e922SSubhransu S. Prusty 			return ret;
97379f4e922SSubhransu S. Prusty 		i++;
97479f4e922SSubhransu S. Prusty 
97579f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
97679f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
97779f4e922SSubhransu S. Prusty 
97879f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
97979f4e922SSubhransu S. Prusty 		num_routes++;
98079f4e922SSubhransu S. Prusty 	}
98179f4e922SSubhransu S. Prusty 
98279f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
98379f4e922SSubhransu S. Prusty 							GFP_KERNEL);
98479f4e922SSubhransu S. Prusty 	if (!route)
98579f4e922SSubhransu S. Prusty 		return -ENOMEM;
98679f4e922SSubhransu S. Prusty 
98779f4e922SSubhransu S. Prusty 	i = 0;
98879f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
98979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
99079f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
99179f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
99279f4e922SSubhransu S. Prusty 
99379f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
99479f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
99579f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
99679f4e922SSubhransu S. Prusty 		i++;
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty 	}
99979f4e922SSubhransu S. Prusty 
100079f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
100179f4e922SSubhransu S. Prusty 
100279f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
100379f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
100479f4e922SSubhransu S. Prusty 
100579f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
100679f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
100779f4e922SSubhransu S. Prusty 
100879f4e922SSubhransu S. Prusty 	return 0;
100979f4e922SSubhransu S. Prusty 
101018382eadSSubhransu S. Prusty }
101118382eadSSubhransu S. Prusty 
101215b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
101318382eadSSubhransu S. Prusty {
101415b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1015148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
101615b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1017148569fdSSubhransu S. Prusty 	int dai_id = 0;
101818382eadSSubhransu S. Prusty 
1019148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
102015b91447SSubhransu S. Prusty 		return -EINVAL;
102118382eadSSubhransu S. Prusty 
1022148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1023148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1024148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
102515b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
102618382eadSSubhransu S. Prusty 
1027148569fdSSubhransu S. Prusty 		dai_id++;
1028148569fdSSubhransu S. Prusty 
1029148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1030148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1031148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1032148569fdSSubhransu S. Prusty 			break;
1033148569fdSSubhransu S. Prusty 		}
1034148569fdSSubhransu S. Prusty 	}
103518382eadSSubhransu S. Prusty 
103615b91447SSubhransu S. Prusty 	return 0;
103715b91447SSubhransu S. Prusty }
103815b91447SSubhransu S. Prusty 
103915b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
104015b91447SSubhransu S. Prusty {
104115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
104215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10434a3478deSJeeja KP 	char name[NAME_SIZE];
104415b91447SSubhransu S. Prusty 
104515b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
104615b91447SSubhransu S. Prusty 	if (!cvt)
104715b91447SSubhransu S. Prusty 		return -ENOMEM;
104815b91447SSubhransu S. Prusty 
104915b91447SSubhransu S. Prusty 	cvt->nid = nid;
10504a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10514a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
105215b91447SSubhransu S. Prusty 
105315b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
105415b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
105515b91447SSubhransu S. Prusty 
105615b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
105715b91447SSubhransu S. Prusty }
105815b91447SSubhransu S. Prusty 
1059b7756edeSSubhransu S. Prusty static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1060b7756edeSSubhransu S. Prusty 			struct hdac_hdmi_pin *pin)
1061b7756edeSSubhransu S. Prusty {
1062b7756edeSSubhransu S. Prusty 	pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1063b7756edeSSubhransu S. Prusty }
1064b7756edeSSubhransu S. Prusty 
1065b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1066b8a54545SSubhransu S. Prusty {
1067b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10684a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10694a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1070b8a54545SSubhransu S. Prusty 	int val;
1071b8a54545SSubhransu S. Prusty 
1072b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
1073b8a54545SSubhransu S. Prusty 
1074b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
1075b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1076b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
1077b8a54545SSubhransu S. Prusty 
1078b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1079b8a54545SSubhransu S. Prusty 						val, pin->nid);
1080b8a54545SSubhransu S. Prusty 
10814a3478deSJeeja KP 
10824a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1083b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1084b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1085b8a54545SSubhransu S. Prusty 
10864a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
10874a3478deSJeeja KP 
1088b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1089b8a54545SSubhransu S. Prusty 
1090b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1091b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
10924a3478deSJeeja KP 
10934a3478deSJeeja KP 		/*
10944a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
10954a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
10964a3478deSJeeja KP 		 * control select.
10974a3478deSJeeja KP 		 */
10984a3478deSJeeja KP 		if (pcm) {
10994a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11004a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
11014a3478deSJeeja KP 
11024a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
11034a3478deSJeeja KP 		}
11044a3478deSJeeja KP 
11054a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1106b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
1107b8a54545SSubhransu S. Prusty 	}
1108b8a54545SSubhransu S. Prusty 
1109b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
1110b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
1111b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1112b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
1113b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
1114b8a54545SSubhransu S. Prusty 
11154a3478deSJeeja KP 			if (pcm) {
11164a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11174a3478deSJeeja KP 					"jack report for pcm=%d\n",
11184a3478deSJeeja KP 					pcm->pcm_id);
11194a3478deSJeeja KP 
11204a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
11214a3478deSJeeja KP 			}
1122b7756edeSSubhransu S. Prusty 			hdac_hdmi_parse_eld(edev, pin);
11234a3478deSJeeja KP 
11243cb7cec1SVedang Patel 			print_hex_dump_debug("ELD: ",
11253cb7cec1SVedang Patel 					DUMP_PREFIX_OFFSET, 16, 1,
11263cb7cec1SVedang Patel 					pin->eld.eld_buffer, pin->eld.eld_size,
11273cb7cec1SVedang Patel 					true);
1128b8a54545SSubhransu S. Prusty 		} else {
1129b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
1130b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
11314a3478deSJeeja KP 
11324a3478deSJeeja KP 			if (pcm) {
11334a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11344a3478deSJeeja KP 					"jack report for pcm=%d\n",
11354a3478deSJeeja KP 					pcm->pcm_id);
11364a3478deSJeeja KP 
11374a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
1138b8a54545SSubhransu S. Prusty 			}
1139b8a54545SSubhransu S. Prusty 		}
11404a3478deSJeeja KP 	}
11414a3478deSJeeja KP 
11424a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
1143b8a54545SSubhransu S. Prusty 
1144b8a54545SSubhransu S. Prusty 	/*
1145b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
1146b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
1147b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
1148b8a54545SSubhransu S. Prusty 	 */
1149b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1150b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1151b8a54545SSubhransu S. Prusty 
1152b8a54545SSubhransu S. Prusty put_hdac_device:
1153b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1154b8a54545SSubhransu S. Prusty }
1155b8a54545SSubhransu S. Prusty 
1156b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1157b8a54545SSubhransu S. Prusty {
1158b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1159b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1160b8a54545SSubhransu S. Prusty 
1161b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1162b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1163b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1164b8a54545SSubhransu S. Prusty 
1165b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1166b8a54545SSubhransu S. Prusty }
1167b8a54545SSubhransu S. Prusty 
116815b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
116915b91447SSubhransu S. Prusty {
117015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
117115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
117215b91447SSubhransu S. Prusty 
117315b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
117415b91447SSubhransu S. Prusty 	if (!pin)
117515b91447SSubhransu S. Prusty 		return -ENOMEM;
117615b91447SSubhransu S. Prusty 
117715b91447SSubhransu S. Prusty 	pin->nid = nid;
117815b91447SSubhransu S. Prusty 
117915b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
118015b91447SSubhransu S. Prusty 	hdmi->num_pin++;
118115b91447SSubhransu S. Prusty 
1182b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1183bcced704SSubhransu S. Prusty 	mutex_init(&pin->lock);
1184b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1185b8a54545SSubhransu S. Prusty 
118615b91447SSubhransu S. Prusty 	return 0;
118718382eadSSubhransu S. Prusty }
118818382eadSSubhransu S. Prusty 
1189211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1190211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1191211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1192211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1193211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1194211caab7SSubhransu S. Prusty 
1195211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1196211caab7SSubhransu S. Prusty {
1197211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1198211caab7SSubhransu S. Prusty 
1199211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1200211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1201211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1202211caab7SSubhransu S. Prusty 		return;
1203211caab7SSubhransu S. Prusty 
1204211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1205211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1206211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1207211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1208211caab7SSubhransu S. Prusty 		return;
1209211caab7SSubhransu S. Prusty }
1210211caab7SSubhransu S. Prusty 
1211211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1212211caab7SSubhransu S. Prusty {
1213211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1214211caab7SSubhransu S. Prusty 
1215211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1216211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1217211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1218211caab7SSubhransu S. Prusty 		return;
1219211caab7SSubhransu S. Prusty 
1220211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1221211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1222211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1223211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1224211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1225211caab7SSubhransu S. Prusty 		return;
1226211caab7SSubhransu S. Prusty 
1227211caab7SSubhransu S. Prusty }
1228211caab7SSubhransu S. Prusty 
122917a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
123017a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
123117a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
123217a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
123317a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
1234571d5078SJeeja KP 	.trigger = hdac_hdmi_trigger,
123517a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
123617a42c45SSubhransu S. Prusty };
123717a42c45SSubhransu S. Prusty 
123817a42c45SSubhransu S. Prusty /*
123917a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
124017a42c45SSubhransu S. Prusty  * based on the number of converter queried.
124117a42c45SSubhransu S. Prusty  */
124217a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
124317a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
124417a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
124517a42c45SSubhransu S. Prusty {
124617a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
124717a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
124817a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
124917a42c45SSubhransu S. Prusty 	int i = 0;
125017a42c45SSubhransu S. Prusty 	u32 rates, bps;
125117a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
125217a42c45SSubhransu S. Prusty 	u64 formats;
125317a42c45SSubhransu S. Prusty 	int ret;
125417a42c45SSubhransu S. Prusty 
125517a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
125617a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
125717a42c45SSubhransu S. Prusty 			GFP_KERNEL);
125817a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
125917a42c45SSubhransu S. Prusty 		return -ENOMEM;
126017a42c45SSubhransu S. Prusty 
126117a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
126217a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
126317a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
126417a42c45SSubhransu S. Prusty 		if (ret)
126517a42c45SSubhransu S. Prusty 			return ret;
126617a42c45SSubhransu S. Prusty 
126717a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
126817a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
126917a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
127017a42c45SSubhransu S. Prusty 
127117a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
127217a42c45SSubhransu S. Prusty 			return -ENOMEM;
127317a42c45SSubhransu S. Prusty 
127417a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
127517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
127617a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
127717a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
127817a42c45SSubhransu S. Prusty 			return -ENOMEM;
127917a42c45SSubhransu S. Prusty 
128017a42c45SSubhransu S. Prusty 		/*
128117a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
128217a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
128317a42c45SSubhransu S. Prusty 		 */
128417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
128517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
128617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
128717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
128817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
128917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
129017a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
129117a42c45SSubhransu S. Prusty 
129217a42c45SSubhransu S. Prusty 		i++;
129317a42c45SSubhransu S. Prusty 	}
129417a42c45SSubhransu S. Prusty 
129517a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
129617a42c45SSubhransu S. Prusty 
129717a42c45SSubhransu S. Prusty 	return 0;
129817a42c45SSubhransu S. Prusty }
129917a42c45SSubhransu S. Prusty 
130018382eadSSubhransu S. Prusty /*
130118382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
130218382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
130318382eadSSubhransu S. Prusty  */
130417a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
130517a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
130618382eadSSubhransu S. Prusty {
130718382eadSSubhransu S. Prusty 	hda_nid_t nid;
13083c83ac23SSudip Mukherjee 	int i, num_nodes;
130918382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
131018382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
131115b91447SSubhransu S. Prusty 	int ret;
131218382eadSSubhransu S. Prusty 
1313211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1314211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1315211caab7SSubhransu S. Prusty 
13163c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1317541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
131818382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
131918382eadSSubhransu S. Prusty 		return -EINVAL;
132018382eadSSubhransu S. Prusty 	}
132118382eadSSubhransu S. Prusty 
13223c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
132318382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
132418382eadSSubhransu S. Prusty 
132518382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
132618382eadSSubhransu S. Prusty 		unsigned int caps;
132718382eadSSubhransu S. Prusty 		unsigned int type;
132818382eadSSubhransu S. Prusty 
132918382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
133018382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
133118382eadSSubhransu S. Prusty 
133218382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
133318382eadSSubhransu S. Prusty 			continue;
133418382eadSSubhransu S. Prusty 
133518382eadSSubhransu S. Prusty 		switch (type) {
133618382eadSSubhransu S. Prusty 
133718382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
133815b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
133915b91447SSubhransu S. Prusty 			if (ret < 0)
134015b91447SSubhransu S. Prusty 				return ret;
134118382eadSSubhransu S. Prusty 			break;
134218382eadSSubhransu S. Prusty 
134318382eadSSubhransu S. Prusty 		case AC_WID_PIN:
134415b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
134515b91447SSubhransu S. Prusty 			if (ret < 0)
134615b91447SSubhransu S. Prusty 				return ret;
134718382eadSSubhransu S. Prusty 			break;
134818382eadSSubhransu S. Prusty 		}
134918382eadSSubhransu S. Prusty 	}
135018382eadSSubhransu S. Prusty 
135118382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
135218382eadSSubhransu S. Prusty 
135315b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
135418382eadSSubhransu S. Prusty 		return -EIO;
135518382eadSSubhransu S. Prusty 
135617a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
135717a42c45SSubhransu S. Prusty 	if (ret) {
135817a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
135917a42c45SSubhransu S. Prusty 							ret);
136017a42c45SSubhransu S. Prusty 		return ret;
136117a42c45SSubhransu S. Prusty 	}
136217a42c45SSubhransu S. Prusty 
136317a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
136417a42c45SSubhransu S. Prusty 
136515b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
136618382eadSSubhransu S. Prusty }
136718382eadSSubhransu S. Prusty 
1368b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1369b8a54545SSubhransu S. Prusty {
1370b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1371b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1372b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1373b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1374b8a54545SSubhransu S. Prusty 
1375b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1376b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1377b8a54545SSubhransu S. Prusty 
1378b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1379b8a54545SSubhransu S. Prusty 
1380b8a54545SSubhransu S. Prusty 	/*
1381b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1382b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1383b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1384b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1385b8a54545SSubhransu S. Prusty 	 */
1386b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1387b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1388b8a54545SSubhransu S. Prusty 		return;
1389b8a54545SSubhransu S. Prusty 
1390b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1391b8a54545SSubhransu S. Prusty 		return;
1392b8a54545SSubhransu S. Prusty 
1393b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1394b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1395b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1396b8a54545SSubhransu S. Prusty 	}
1397b8a54545SSubhransu S. Prusty }
1398b8a54545SSubhransu S. Prusty 
1399b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1400b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1401b8a54545SSubhransu S. Prusty };
1402b8a54545SSubhransu S. Prusty 
14032889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
14042889099eSSubhransu S. Prusty 						int device)
14052889099eSSubhransu S. Prusty {
14062889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
14072889099eSSubhransu S. Prusty 
14082889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
14092889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
14102889099eSSubhransu S. Prusty 			return rtd->pcm;
14112889099eSSubhransu S. Prusty 	}
14122889099eSSubhransu S. Prusty 
14132889099eSSubhransu S. Prusty 	return NULL;
14142889099eSSubhransu S. Prusty }
14152889099eSSubhransu S. Prusty 
14164a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
14174a3478deSJeeja KP {
14184a3478deSJeeja KP 	char jack_name[NAME_SIZE];
14194a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
14204a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
14214a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
14224a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
14234a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
14244a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
14252889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
14262889099eSSubhransu S. Prusty 	int err;
14274a3478deSJeeja KP 
14284a3478deSJeeja KP 	/*
14294a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
14304a3478deSJeeja KP 	 * add to the pcm list
14314a3478deSJeeja KP 	 */
14324a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
14334a3478deSJeeja KP 	if (!pcm)
14344a3478deSJeeja KP 		return -ENOMEM;
14354a3478deSJeeja KP 	pcm->pcm_id = device;
14364a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
14374a3478deSJeeja KP 
14382889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
14392889099eSSubhransu S. Prusty 	if (snd_pcm) {
14402889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
14412889099eSSubhransu S. Prusty 		if (err < 0) {
14422889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
14432889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
14442889099eSSubhransu S. Prusty 				err, device);
14452889099eSSubhransu S. Prusty 			kfree(pcm);
14462889099eSSubhransu S. Prusty 			return err;
14472889099eSSubhransu S. Prusty 		}
14482889099eSSubhransu S. Prusty 	}
14492889099eSSubhransu S. Prusty 
14504a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
14514a3478deSJeeja KP 
14524a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
14534a3478deSJeeja KP 
14544a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
14554a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
14564a3478deSJeeja KP }
14574a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
14584a3478deSJeeja KP 
145918382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
146018382eadSSubhransu S. Prusty {
146118382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
146218382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
146318382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
146418382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1465b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1466b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1467b8a54545SSubhransu S. Prusty 	int ret;
146818382eadSSubhransu S. Prusty 
146918382eadSSubhransu S. Prusty 	edev->scodec = codec;
147018382eadSSubhransu S. Prusty 
1471b2047e99SVinod Koul 	/*
1472b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1473b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1474b2047e99SVinod Koul 	 */
1475b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1476500e06b9SVinod Koul 	if (!hlink) {
1477500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1478500e06b9SVinod Koul 		return -EIO;
1479500e06b9SVinod Koul 	}
1480500e06b9SVinod Koul 
1481b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1482b2047e99SVinod Koul 
148379f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
148479f4e922SSubhransu S. Prusty 	if (ret < 0)
148579f4e922SSubhransu S. Prusty 		return ret;
148618382eadSSubhransu S. Prusty 
1487b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1488b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1489b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1490b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1491b8a54545SSubhransu S. Prusty 				ret);
1492b8a54545SSubhransu S. Prusty 		return ret;
1493b8a54545SSubhransu S. Prusty 	}
1494b8a54545SSubhransu S. Prusty 
1495b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1496b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1497b8a54545SSubhransu S. Prusty 
149818382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
149918382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
150018382eadSSubhransu S. Prusty 
1501e342ac08SSubhransu S. Prusty 	/*
1502e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1503e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1504e342ac08SSubhransu S. Prusty 	 */
1505e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1506e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1507e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1508e342ac08SSubhransu S. Prusty 
1509e342ac08SSubhransu S. Prusty 	return 0;
1510e342ac08SSubhransu S. Prusty }
1511e342ac08SSubhransu S. Prusty 
1512e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1513e342ac08SSubhransu S. Prusty {
1514e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1515e342ac08SSubhransu S. Prusty 
1516e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
151718382eadSSubhransu S. Prusty 	return 0;
151818382eadSSubhransu S. Prusty }
151918382eadSSubhransu S. Prusty 
1520571d5078SJeeja KP #ifdef CONFIG_PM
15211b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
15221b377ccdSSubhransu S. Prusty {
15231b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
15241b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
15251b377ccdSSubhransu S. Prusty 
15261b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
15271b377ccdSSubhransu S. Prusty 
15281b377ccdSSubhransu S. Prusty 	/*
15291b377ccdSSubhransu S. Prusty 	 * Power down afg.
15301b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
15311b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
15321b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
15331b377ccdSSubhransu S. Prusty 	 * to read the state.
15341b377ccdSSubhransu S. Prusty 	 */
15351b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15361b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
15371b377ccdSSubhransu S. Prusty 
15381b377ccdSSubhransu S. Prusty 	return 0;
15391b377ccdSSubhransu S. Prusty }
15401b377ccdSSubhransu S. Prusty 
15410fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1542571d5078SJeeja KP {
15430fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1544571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1545571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1546571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
15471b377ccdSSubhransu S. Prusty 
15481b377ccdSSubhransu S. Prusty 	/* Power up afg */
15491b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15501b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1551571d5078SJeeja KP 
1552571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1553571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1554571d5078SJeeja KP 
1555571d5078SJeeja KP 	/*
1556571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1557571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1558571d5078SJeeja KP 	 * all pins here.
1559571d5078SJeeja KP 	 */
1560571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1561571d5078SJeeja KP 		hdac_hdmi_present_sense(pin, 1);
1562571d5078SJeeja KP 
15631b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1564571d5078SJeeja KP }
1565571d5078SJeeja KP #else
15661b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
15670fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1568571d5078SJeeja KP #endif
1569571d5078SJeeja KP 
157018382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
157118382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1572e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
157318382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
157418382eadSSubhransu S. Prusty };
157518382eadSSubhransu S. Prusty 
15762889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
15772889099eSSubhransu S. Prusty 					unsigned char *chmap)
15782889099eSSubhransu S. Prusty {
15792889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15802889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15812889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
15822889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
15832889099eSSubhransu S. Prusty 
15842889099eSSubhransu S. Prusty 	/* chmap is already set to 0 in caller */
15852889099eSSubhransu S. Prusty 	if (!pin)
15862889099eSSubhransu S. Prusty 		return;
15872889099eSSubhransu S. Prusty 
15882889099eSSubhransu S. Prusty 	memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
15892889099eSSubhransu S. Prusty }
15902889099eSSubhransu S. Prusty 
15912889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
15922889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
15932889099eSSubhransu S. Prusty {
15942889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15952889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15962889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
15972889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
15982889099eSSubhransu S. Prusty 
15992889099eSSubhransu S. Prusty 	mutex_lock(&pin->lock);
16002889099eSSubhransu S. Prusty 	pin->chmap_set = true;
16012889099eSSubhransu S. Prusty 	memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
16022889099eSSubhransu S. Prusty 	if (prepared)
16032889099eSSubhransu S. Prusty 		hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
16042889099eSSubhransu S. Prusty 	mutex_unlock(&pin->lock);
16052889099eSSubhransu S. Prusty }
16062889099eSSubhransu S. Prusty 
16072889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
16082889099eSSubhransu S. Prusty {
16092889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16102889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16112889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
16122889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
16132889099eSSubhransu S. Prusty 
16142889099eSSubhransu S. Prusty 	return pin ? true:false;
16152889099eSSubhransu S. Prusty }
16162889099eSSubhransu S. Prusty 
16172889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
16182889099eSSubhransu S. Prusty {
16192889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16202889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16212889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
16222889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
16232889099eSSubhransu S. Prusty 
16248f658815SDan Carpenter 	if (!pin || !pin->eld.eld_valid)
16252889099eSSubhransu S. Prusty 		return 0;
16262889099eSSubhransu S. Prusty 
16272889099eSSubhransu S. Prusty 	return pin->eld.info.spk_alloc;
16282889099eSSubhransu S. Prusty }
16292889099eSSubhransu S. Prusty 
163018382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
163118382eadSSubhransu S. Prusty {
163218382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
163318382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
163417a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1635b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
163617a42c45SSubhransu S. Prusty 	int num_dais = 0;
163718382eadSSubhransu S. Prusty 	int ret = 0;
163818382eadSSubhransu S. Prusty 
1639b2047e99SVinod Koul 	/* hold the ref while we probe */
1640b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1641500e06b9SVinod Koul 	if (!hlink) {
1642500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1643500e06b9SVinod Koul 		return -EIO;
1644500e06b9SVinod Koul 	}
1645500e06b9SVinod Koul 
1646b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1647b2047e99SVinod Koul 
164818382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
164918382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
165018382eadSSubhransu S. Prusty 		return -ENOMEM;
165118382eadSSubhransu S. Prusty 
165218382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1653bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
16542889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
16552889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
16562889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
16572889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
165818382eadSSubhransu S. Prusty 
165918382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
166018382eadSSubhransu S. Prusty 
166115b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
166215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
16634a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
16644a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
166515b91447SSubhransu S. Prusty 
1666aeaccef0SRamesh Babu 	/*
1667aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1668aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1669aeaccef0SRamesh Babu 	 */
1670aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1671aeaccef0SRamesh Babu 	if (ret < 0) {
1672aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1673aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1674aeaccef0SRamesh Babu 			ret);
1675aeaccef0SRamesh Babu 		return ret;
1676aeaccef0SRamesh Babu 	}
1677aeaccef0SRamesh Babu 
167817a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
167917a42c45SSubhransu S. Prusty 	if (ret < 0) {
168017a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
168117a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
168218382eadSSubhransu S. Prusty 		return ret;
168317a42c45SSubhransu S. Prusty 	}
168418382eadSSubhransu S. Prusty 
168518382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1686b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
168717a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1688b2047e99SVinod Koul 
1689b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1690b2047e99SVinod Koul 
1691b2047e99SVinod Koul 	return ret;
169218382eadSSubhransu S. Prusty }
169318382eadSSubhransu S. Prusty 
169418382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
169518382eadSSubhransu S. Prusty {
169615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
169715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
169815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
16994a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
170015b91447SSubhransu S. Prusty 
170118382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
170218382eadSSubhransu S. Prusty 
17034a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
17044a3478deSJeeja KP 		pcm->cvt = NULL;
17054a3478deSJeeja KP 		pcm->pin = NULL;
17064a3478deSJeeja KP 		list_del(&pcm->head);
17074a3478deSJeeja KP 		kfree(pcm);
17084a3478deSJeeja KP 	}
17094a3478deSJeeja KP 
171015b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
171115b91447SSubhransu S. Prusty 		list_del(&cvt->head);
17124a3478deSJeeja KP 		kfree(cvt->name);
171315b91447SSubhransu S. Prusty 		kfree(cvt);
171415b91447SSubhransu S. Prusty 	}
171515b91447SSubhransu S. Prusty 
171615b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
171715b91447SSubhransu S. Prusty 		list_del(&pin->head);
171815b91447SSubhransu S. Prusty 		kfree(pin);
171915b91447SSubhransu S. Prusty 	}
172015b91447SSubhransu S. Prusty 
172118382eadSSubhransu S. Prusty 	return 0;
172218382eadSSubhransu S. Prusty }
172318382eadSSubhransu S. Prusty 
1724e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1725e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1726e342ac08SSubhransu S. Prusty {
1727e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1728e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
172907f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1730b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1731b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
173207f083abSSubhransu S. Prusty 	int err;
1733e342ac08SSubhransu S. Prusty 
1734e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1735e342ac08SSubhransu S. Prusty 
173607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
173707f083abSSubhransu S. Prusty 	if (!bus)
173807f083abSSubhransu S. Prusty 		return 0;
173907f083abSSubhransu S. Prusty 
17401b377ccdSSubhransu S. Prusty 	/*
17411b377ccdSSubhransu S. Prusty 	 * Power down afg.
17421b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
17431b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
17441b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
17451b377ccdSSubhransu S. Prusty 	 * to read the state.
17461b377ccdSSubhransu S. Prusty 	 */
17471b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17481b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
174907f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
175007f083abSSubhransu S. Prusty 	if (err < 0) {
175107f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
175207f083abSSubhransu S. Prusty 		return err;
175307f083abSSubhransu S. Prusty 	}
175407f083abSSubhransu S. Prusty 
1755b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1756500e06b9SVinod Koul 	if (!hlink) {
1757500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1758500e06b9SVinod Koul 		return -EIO;
1759500e06b9SVinod Koul 	}
1760500e06b9SVinod Koul 
1761b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1762b2047e99SVinod Koul 
1763e342ac08SSubhransu S. Prusty 	return 0;
1764e342ac08SSubhransu S. Prusty }
1765e342ac08SSubhransu S. Prusty 
1766e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1767e342ac08SSubhransu S. Prusty {
1768e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1769e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
177007f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1771b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1772b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
177307f083abSSubhransu S. Prusty 	int err;
1774e342ac08SSubhransu S. Prusty 
1775e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1776e342ac08SSubhransu S. Prusty 
177707f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
177807f083abSSubhransu S. Prusty 	if (!bus)
177907f083abSSubhransu S. Prusty 		return 0;
178007f083abSSubhransu S. Prusty 
1781b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1782500e06b9SVinod Koul 	if (!hlink) {
1783500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1784500e06b9SVinod Koul 		return -EIO;
1785500e06b9SVinod Koul 	}
1786500e06b9SVinod Koul 
1787b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1788b2047e99SVinod Koul 
178907f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
179007f083abSSubhransu S. Prusty 	if (err < 0) {
179107f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
179207f083abSSubhransu S. Prusty 		return err;
179307f083abSSubhransu S. Prusty 	}
179407f083abSSubhransu S. Prusty 
1795ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1796ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1797ab85f5b3SSubhransu S. Prusty 
1798e342ac08SSubhransu S. Prusty 	/* Power up afg */
17991b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18001b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1801e342ac08SSubhransu S. Prusty 
1802e342ac08SSubhransu S. Prusty 	return 0;
1803e342ac08SSubhransu S. Prusty }
1804e342ac08SSubhransu S. Prusty #else
1805e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1806e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1807e342ac08SSubhransu S. Prusty #endif
1808e342ac08SSubhransu S. Prusty 
1809e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1810e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
18111b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
18120fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1813e342ac08SSubhransu S. Prusty };
1814e342ac08SSubhransu S. Prusty 
181518382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
181618382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1817e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1818cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
181918382eadSSubhransu S. Prusty 	{}
182018382eadSSubhransu S. Prusty };
182118382eadSSubhransu S. Prusty 
182218382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
182318382eadSSubhransu S. Prusty 
182418382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
182518382eadSSubhransu S. Prusty 	. hdac = {
182618382eadSSubhransu S. Prusty 		.driver = {
182718382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1828e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
182918382eadSSubhransu S. Prusty 		},
183018382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
183118382eadSSubhransu S. Prusty 	},
183218382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
183318382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
183418382eadSSubhransu S. Prusty };
183518382eadSSubhransu S. Prusty 
183618382eadSSubhransu S. Prusty static int __init hdmi_init(void)
183718382eadSSubhransu S. Prusty {
183818382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
183918382eadSSubhransu S. Prusty }
184018382eadSSubhransu S. Prusty 
184118382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
184218382eadSSubhransu S. Prusty {
184318382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
184418382eadSSubhransu S. Prusty }
184518382eadSSubhransu S. Prusty 
184618382eadSSubhransu S. Prusty module_init(hdmi_init);
184718382eadSSubhransu S. Prusty module_exit(hdmi_exit);
184818382eadSSubhransu S. Prusty 
184918382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
185018382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
185118382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
185218382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1853