xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision b7756edeb7d03b675e10b4862dccc8deb4b0ca17)
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>
3218382eadSSubhransu S. Prusty #include "../../hda/local.h"
334a3478deSJeeja KP #include "hdac_hdmi.h"
3418382eadSSubhransu S. Prusty 
3517a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3617a42c45SSubhransu S. Prusty 
37b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
38b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
3918382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
40b0362adbSSubhransu S. Prusty 
4118382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4218382eadSSubhransu S. Prusty 
43148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
44148569fdSSubhransu S. Prusty 
45b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
46b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
47b8a54545SSubhransu S. Prusty 
4818382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
4918382eadSSubhransu S. Prusty 	unsigned int channels_min;
5018382eadSSubhransu S. Prusty 	unsigned int channels_max;
5118382eadSSubhransu S. Prusty 	u32 rates;
5218382eadSSubhransu S. Prusty 	u64 formats;
5318382eadSSubhransu S. Prusty 	unsigned int maxbps;
5418382eadSSubhransu S. Prusty };
5518382eadSSubhransu S. Prusty 
5618382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5715b91447SSubhransu S. Prusty 	struct list_head head;
5818382eadSSubhransu S. Prusty 	hda_nid_t nid;
594a3478deSJeeja KP 	const char *name;
6018382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6118382eadSSubhransu S. Prusty };
6218382eadSSubhransu S. Prusty 
63*b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
64*b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
65*b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
66*b7756edeSSubhransu S. Prusty };
67*b7756edeSSubhransu S. Prusty 
68b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
69b8a54545SSubhransu S. Prusty 	bool	monitor_present;
70b8a54545SSubhransu S. Prusty 	bool	eld_valid;
71b8a54545SSubhransu S. Prusty 	int	eld_size;
72b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
73*b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
74b8a54545SSubhransu S. Prusty };
75b8a54545SSubhransu S. Prusty 
7618382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7715b91447SSubhransu S. Prusty 	struct list_head head;
7818382eadSSubhransu S. Prusty 	hda_nid_t nid;
7918382eadSSubhransu S. Prusty 	int num_mux_nids;
8018382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
81b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
82b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev;
83b8a54545SSubhransu S. Prusty 	int repoll_count;
84b8a54545SSubhransu S. Prusty 	struct delayed_work work;
8518382eadSSubhransu S. Prusty };
8618382eadSSubhransu S. Prusty 
874a3478deSJeeja KP struct hdac_hdmi_pcm {
884a3478deSJeeja KP 	struct list_head head;
894a3478deSJeeja KP 	int pcm_id;
904a3478deSJeeja KP 	struct hdac_hdmi_pin *pin;
914a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
924a3478deSJeeja KP 	struct snd_jack *jack;
934a3478deSJeeja KP };
944a3478deSJeeja KP 
9518382eadSSubhransu S. Prusty struct hdac_hdmi_dai_pin_map {
9618382eadSSubhransu S. Prusty 	int dai_id;
9715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
9815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
9918382eadSSubhransu S. Prusty };
10018382eadSSubhransu S. Prusty 
10118382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
102148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
10315b91447SSubhransu S. Prusty 	struct list_head pin_list;
10415b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1054a3478deSJeeja KP 	struct list_head pcm_list;
10615b91447SSubhransu S. Prusty 	int num_pin;
10715b91447SSubhransu S. Prusty 	int num_cvt;
1084a3478deSJeeja KP 	struct mutex pin_mutex;
10918382eadSSubhransu S. Prusty };
11018382eadSSubhransu S. Prusty 
111e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
112e342ac08SSubhransu S. Prusty {
11351b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
114e342ac08SSubhransu S. Prusty 
11551b2c425SGeliang Tang 	return to_ehdac_device(hdac);
116e342ac08SSubhransu S. Prusty }
117e342ac08SSubhransu S. Prusty 
1182428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1192428bca3SSubhransu S. Prusty {
1202428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1212428bca3SSubhransu S. Prusty }
1222428bca3SSubhransu S. Prusty 
1232428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1242428bca3SSubhransu S. Prusty {
1252428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1262428bca3SSubhransu S. Prusty }
1272428bca3SSubhransu S. Prusty 
1282428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1292428bca3SSubhransu S. Prusty 						void *eld)
1302428bca3SSubhransu S. Prusty {
1312428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1322428bca3SSubhransu S. Prusty 	int i;
1332428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1342428bca3SSubhransu S. Prusty 
1352428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1362428bca3SSubhransu S. Prusty 	if (!sad)
1372428bca3SSubhransu S. Prusty 		goto format_constraint;
1382428bca3SSubhransu S. Prusty 
1392428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1402428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1412428bca3SSubhransu S. Prusty 
1422428bca3SSubhransu S. Prusty 			/*
1432428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1442428bca3SSubhransu S. Prusty 			 * container so we set S32
1452428bca3SSubhransu S. Prusty 			 */
1462428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1472428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1482428bca3SSubhransu S. Prusty 		}
1492428bca3SSubhransu S. Prusty 	}
1502428bca3SSubhransu S. Prusty 
1512428bca3SSubhransu S. Prusty format_constraint:
1522428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1532428bca3SSubhransu S. Prusty 				formats);
1542428bca3SSubhransu S. Prusty 
1552428bca3SSubhransu S. Prusty }
1562428bca3SSubhransu S. Prusty 
157b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
158b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
159b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
160b8a54545SSubhransu S. Prusty {
161b8a54545SSubhransu S. Prusty 	unsigned int val;
162b8a54545SSubhransu S. Prusty 
163b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
164b8a54545SSubhransu S. Prusty 							byte_index);
165b8a54545SSubhransu S. Prusty 
166b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
167b8a54545SSubhransu S. Prusty 					byte_index, val);
168b8a54545SSubhransu S. Prusty 
169b8a54545SSubhransu S. Prusty 	return val;
170b8a54545SSubhransu S. Prusty }
171b8a54545SSubhransu S. Prusty 
172b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
173b8a54545SSubhransu S. Prusty {
174b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
175b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
176b8a54545SSubhransu S. Prusty }
177b8a54545SSubhransu S. Prusty 
178b8a54545SSubhransu S. Prusty /*
179b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
180b8a54545SSubhransu S. Prusty  * passed by user
181b8a54545SSubhransu S. Prusty  */
182b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
183b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
184b8a54545SSubhransu S. Prusty {
185b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
186b8a54545SSubhransu S. Prusty 
187b8a54545SSubhransu S. Prusty 	/*
188b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
189b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
190b8a54545SSubhransu S. Prusty 	 */
191b8a54545SSubhransu S. Prusty 
192b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
193b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
194b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
195b8a54545SSubhransu S. Prusty 		return -ERANGE;
196b8a54545SSubhransu S. Prusty 	}
197b8a54545SSubhransu S. Prusty 
198b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
199b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
200b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
201b8a54545SSubhransu S. Prusty 		/*
202b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
203b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
204b8a54545SSubhransu S. Prusty 		 */
205b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
206b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
207b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
208b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
209b8a54545SSubhransu S. Prusty 			goto error;
210b8a54545SSubhransu S. Prusty 		}
211b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
212b8a54545SSubhransu S. Prusty 		/*
213b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
214b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
215b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
216b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
217b8a54545SSubhransu S. Prusty 		 */
218b8a54545SSubhransu S. Prusty 		if (!val && !i) {
219b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
220b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
221b8a54545SSubhransu S. Prusty 			goto error;
222b8a54545SSubhransu S. Prusty 		}
223b8a54545SSubhransu S. Prusty 		buf[i] = val;
224b8a54545SSubhransu S. Prusty 	}
225b8a54545SSubhransu S. Prusty 
226b8a54545SSubhransu S. Prusty 	*eld_size = size;
227b8a54545SSubhransu S. Prusty error:
228b8a54545SSubhransu S. Prusty 	return ret;
229b8a54545SSubhransu S. Prusty }
230b8a54545SSubhransu S. Prusty 
231b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
232b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
233b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
234b0362adbSSubhransu S. Prusty {
235b0362adbSSubhransu S. Prusty 	unsigned int val;
236b0362adbSSubhransu S. Prusty 
237b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
238b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
239b0362adbSSubhransu S. Prusty 
240b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
241b0362adbSSubhransu S. Prusty 
242b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
243b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
244b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
245b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
246b0362adbSSubhransu S. Prusty 
247b0362adbSSubhransu S. Prusty 	return 0;
248b0362adbSSubhransu S. Prusty }
249b0362adbSSubhransu S. Prusty 
250a657f1d0SSubhransu S. Prusty static void
251a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
252a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
253a657f1d0SSubhransu S. Prusty {
254a657f1d0SSubhransu S. Prusty 	int val;
255a657f1d0SSubhransu S. Prusty 
256a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
257a657f1d0SSubhransu S. Prusty 
258a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
259a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
260a657f1d0SSubhransu S. Prusty }
261a657f1d0SSubhransu S. Prusty 
262478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
263478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
264478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
265478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
266478f544eSSubhransu S. Prusty 
267478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
268478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
269478f544eSSubhransu S. Prusty 	u8 CXT04;
270478f544eSSubhransu S. Prusty 	u8 CA;
271478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
272478f544eSSubhransu S. Prusty };
273478f544eSSubhransu S. Prusty 
274a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
275a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
276a657f1d0SSubhransu S. Prusty {
277a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
278a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
279478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
280478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
281478f544eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
282478f544eSSubhransu S. Prusty 	u8 *dip;
283a657f1d0SSubhransu S. Prusty 	int ret;
284a657f1d0SSubhransu S. Prusty 	int i;
285478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
286478f544eSSubhransu S. Prusty 	u8 conn_type;
287478f544eSSubhransu S. Prusty 	int channels = 2;
288a657f1d0SSubhransu S. Prusty 
289478f544eSSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
290478f544eSSubhransu S. Prusty 		if (pin->nid == pin_nid)
291478f544eSSubhransu S. Prusty 			break;
292478f544eSSubhransu S. Prusty 	}
293a657f1d0SSubhransu S. Prusty 
294478f544eSSubhransu S. Prusty 	eld_buf = pin->eld.eld_buffer;
295478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
296a657f1d0SSubhransu S. Prusty 
297a657f1d0SSubhransu S. Prusty 	/* setup channel count */
298a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
299478f544eSSubhransu S. Prusty 			    AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
300478f544eSSubhransu S. Prusty 
301478f544eSSubhransu S. Prusty 	switch (conn_type) {
302478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
303478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
304478f544eSSubhransu S. Prusty 
305478f544eSSubhransu S. Prusty 		/* Default stereo for now */
306478f544eSSubhransu S. Prusty 		frame.channels = channels;
307a657f1d0SSubhransu S. Prusty 
308a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
309a657f1d0SSubhransu S. Prusty 		if (ret < 0)
310a657f1d0SSubhransu S. Prusty 			return ret;
311a657f1d0SSubhransu S. Prusty 
312478f544eSSubhransu S. Prusty 		break;
313478f544eSSubhransu S. Prusty 
314478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
315478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
316478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
317478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
318478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
319478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
320478f544eSSubhransu S. Prusty 		dp_ai.CA	= 0;
321478f544eSSubhransu S. Prusty 
322478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
323478f544eSSubhransu S. Prusty 		break;
324478f544eSSubhransu S. Prusty 
325478f544eSSubhransu S. Prusty 	default:
326478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
327478f544eSSubhransu S. Prusty 						conn_type);
328478f544eSSubhransu S. Prusty 		return -EIO;
329478f544eSSubhransu S. Prusty 	}
330478f544eSSubhransu S. Prusty 
331a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
332a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
333a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
334a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
335a657f1d0SSubhransu S. Prusty 
336a657f1d0SSubhransu S. Prusty 
337a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
338a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
339478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
340391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
341a657f1d0SSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
342391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
343478f544eSSubhransu S. Prusty 	} else {
344478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
345478f544eSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
346478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
347478f544eSSubhransu S. Prusty 	}
348a657f1d0SSubhransu S. Prusty 
349a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
350a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
351a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
352a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
353a657f1d0SSubhransu S. Prusty 
354a657f1d0SSubhransu S. Prusty 	return 0;
355a657f1d0SSubhransu S. Prusty }
356a657f1d0SSubhransu S. Prusty 
357b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
358b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
359b0362adbSSubhransu S. Prusty {
360b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
36115b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
36215b91447SSubhransu S. Prusty 						pwr_state))
36315b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
364b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
365b0362adbSSubhransu S. Prusty 
366b0362adbSSubhransu S. Prusty 	/* Power up converter */
36715b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
36815b91447SSubhransu S. Prusty 						pwr_state))
36915b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
370b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
371b0362adbSSubhransu S. Prusty }
372b0362adbSSubhransu S. Prusty 
373b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
374b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
375b0362adbSSubhransu S. Prusty {
376b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
377b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
378b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
379b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
380a657f1d0SSubhransu S. Prusty 	int ret;
381b0362adbSSubhransu S. Prusty 
382b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
383b0362adbSSubhransu S. Prusty 
384b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
385b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
386b0362adbSSubhransu S. Prusty 			dd->stream_tag,	dd->format);
387b0362adbSSubhransu S. Prusty 
38815b91447SSubhransu S. Prusty 	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
38915b91447SSubhransu S. Prusty 						dai_map->pin->nid);
390a657f1d0SSubhransu S. Prusty 	if (ret < 0)
391a657f1d0SSubhransu S. Prusty 		return ret;
392a657f1d0SSubhransu S. Prusty 
39315b91447SSubhransu S. Prusty 	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
39415b91447SSubhransu S. Prusty 			dai_map->pin->nid, dd->stream_tag, dd->format);
395b0362adbSSubhransu S. Prusty }
396b0362adbSSubhransu S. Prusty 
397b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
398b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
399b0362adbSSubhransu S. Prusty {
400b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
40154dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
40254dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
40354dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
404b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
405b0362adbSSubhransu S. Prusty 
40654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
40754dfa1eaSSubhransu S. Prusty 	pin = dai_map->pin;
40854dfa1eaSSubhransu S. Prusty 
40954dfa1eaSSubhransu S. Prusty 	if (!pin)
41054dfa1eaSSubhransu S. Prusty 		return -ENODEV;
41154dfa1eaSSubhransu S. Prusty 
41254dfa1eaSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
41354dfa1eaSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
41454dfa1eaSSubhransu S. Prusty 								pin->nid);
415b0362adbSSubhransu S. Prusty 		return -ENODEV;
416b0362adbSSubhransu S. Prusty 	}
417b0362adbSSubhransu S. Prusty 
4186793a3d7SSubhransu S. Prusty 	dd = snd_soc_dai_get_dma_data(dai, substream);
4196793a3d7SSubhransu S. Prusty 	if (!dd) {
420b0362adbSSubhransu S. Prusty 		dd = kzalloc(sizeof(*dd), GFP_KERNEL);
4218d33ab24SSudip Mukherjee 		if (!dd)
4228d33ab24SSudip Mukherjee 			return -ENOMEM;
4236793a3d7SSubhransu S. Prusty 	}
4246793a3d7SSubhransu S. Prusty 
425b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
426b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
427b0362adbSSubhransu S. Prusty 			24, 0);
428b0362adbSSubhransu S. Prusty 
429b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
430b0362adbSSubhransu S. Prusty 
431b0362adbSSubhransu S. Prusty 	return 0;
432b0362adbSSubhransu S. Prusty }
433b0362adbSSubhransu S. Prusty 
434b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
435b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
436b0362adbSSubhransu S. Prusty {
437b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
438b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
439b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
440b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
441b0362adbSSubhransu S. Prusty 
442b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
443b0362adbSSubhransu S. Prusty 
444b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
445b0362adbSSubhransu S. Prusty 
4466793a3d7SSubhransu S. Prusty 	if (dd) {
4476793a3d7SSubhransu S. Prusty 		snd_soc_dai_set_dma_data(dai, substream, NULL);
448b0362adbSSubhransu S. Prusty 		kfree(dd);
4496793a3d7SSubhransu S. Prusty 	}
450b0362adbSSubhransu S. Prusty 
451b0362adbSSubhransu S. Prusty 	return 0;
452b0362adbSSubhransu S. Prusty }
453b0362adbSSubhransu S. Prusty 
454ab85f5b3SSubhransu S. Prusty static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
455ab85f5b3SSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
456ab85f5b3SSubhransu S. Prusty {
457ab85f5b3SSubhransu S. Prusty 	/* Enable transmission */
458ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
459ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_1, 1);
460ab85f5b3SSubhransu S. Prusty 
461ab85f5b3SSubhransu S. Prusty 	/* Category Code (CC) to zero */
462ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
463ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_2, 0);
464ab85f5b3SSubhransu S. Prusty }
465ab85f5b3SSubhransu S. Prusty 
466148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
467148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
468148569fdSSubhransu S. Prusty {
469148569fdSSubhransu S. Prusty 	int mux_idx;
470148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
471148569fdSSubhransu S. Prusty 
472148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
473148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
474148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
475148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
476148569fdSSubhransu S. Prusty 			break;
477148569fdSSubhransu S. Prusty 		}
478148569fdSSubhransu S. Prusty 	}
479148569fdSSubhransu S. Prusty 
480148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
481148569fdSSubhransu S. Prusty 		return -EIO;
482148569fdSSubhransu S. Prusty 
483148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
484148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
485148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
486148569fdSSubhransu S. Prusty 
487148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
488148569fdSSubhransu S. Prusty 
489148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
490148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
491148569fdSSubhransu S. Prusty 
492148569fdSSubhransu S. Prusty 	return 0;
493148569fdSSubhransu S. Prusty }
494148569fdSSubhransu S. Prusty 
495148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
496148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
497148569fdSSubhransu S. Prusty {
498148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
499148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
500148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
501148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
502148569fdSSubhransu S. Prusty 		return -EINVAL;
503148569fdSSubhransu S. Prusty 	}
504148569fdSSubhransu S. Prusty 
505148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
506148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
507148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
508148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
509148569fdSSubhransu S. Prusty 								pin->nid);
510148569fdSSubhransu S. Prusty 
511148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
512148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
513148569fdSSubhransu S. Prusty 
514148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
515148569fdSSubhransu S. Prusty }
516148569fdSSubhransu S. Prusty 
517148569fdSSubhransu S. Prusty /*
518148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
519148569fdSSubhransu S. Prusty  *
520148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
521148569fdSSubhransu S. Prusty  *
522148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
523148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
524148569fdSSubhransu S. Prusty  * connected.
525148569fdSSubhransu S. Prusty  */
526148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
527148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
528148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
529148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
530148569fdSSubhransu S. Prusty {
531148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
532148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
533148569fdSSubhransu S. Prusty 	int ret, i;
534148569fdSSubhransu S. Prusty 
535148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
536148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
537148569fdSSubhransu S. Prusty 			pin = pcm->pin;
538148569fdSSubhransu S. Prusty 			break;
539148569fdSSubhransu S. Prusty 		}
540148569fdSSubhransu S. Prusty 	}
541148569fdSSubhransu S. Prusty 
542148569fdSSubhransu S. Prusty 	if (pin) {
543148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
544148569fdSSubhransu S. Prusty 		if (ret < 0)
545148569fdSSubhransu S. Prusty 			return NULL;
546148569fdSSubhransu S. Prusty 
547148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
548148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
549148569fdSSubhransu S. Prusty 				return pin;
550148569fdSSubhransu S. Prusty 		}
551148569fdSSubhransu S. Prusty 	}
552148569fdSSubhransu S. Prusty 
553148569fdSSubhransu S. Prusty 	return NULL;
554148569fdSSubhransu S. Prusty }
555148569fdSSubhransu S. Prusty 
55654dfa1eaSSubhransu S. Prusty /*
55754dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
55854dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
55954dfa1eaSSubhransu S. Prusty  * doesn't fail.
56054dfa1eaSSubhransu S. Prusty  */
561b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
562b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
563b0362adbSSubhransu S. Prusty {
564b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
565b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
566b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
567148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
568148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
5692428bca3SSubhransu S. Prusty 	int ret;
570b0362adbSSubhransu S. Prusty 
571b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
572b0362adbSSubhransu S. Prusty 
573148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
574148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
57554dfa1eaSSubhransu S. Prusty 
57654dfa1eaSSubhransu S. Prusty 	/*
57754dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
57854dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
57954dfa1eaSSubhransu S. Prusty 	 */
580148569fdSSubhransu S. Prusty 	if (!pin)
58154dfa1eaSSubhransu S. Prusty 		return 0;
582148569fdSSubhransu S. Prusty 
583148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
584148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
585b0362adbSSubhransu S. Prusty 
58654dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
587148569fdSSubhransu S. Prusty 			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
588148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
589b8a54545SSubhransu S. Prusty 
59054dfa1eaSSubhransu S. Prusty 		return 0;
591b0362adbSSubhransu S. Prusty 	}
592b0362adbSSubhransu S. Prusty 
593148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
594b0362adbSSubhransu S. Prusty 
595ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_enable_cvt(hdac, dai_map);
596148569fdSSubhransu S. Prusty 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
597148569fdSSubhransu S. Prusty 	if (ret < 0)
598148569fdSSubhransu S. Prusty 		return ret;
599b0362adbSSubhransu S. Prusty 
6002428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
601148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
6022428bca3SSubhransu S. Prusty 	if (ret < 0)
6032428bca3SSubhransu S. Prusty 		return ret;
604b0362adbSSubhransu S. Prusty 
6052428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
606148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
607b0362adbSSubhransu S. Prusty }
608b0362adbSSubhransu S. Prusty 
609571d5078SJeeja KP static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
610571d5078SJeeja KP 		struct snd_soc_dai *dai)
611571d5078SJeeja KP {
612571d5078SJeeja KP 	struct hdac_hdmi_dai_pin_map *dai_map;
613571d5078SJeeja KP 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
614571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
615571d5078SJeeja KP 	int ret;
616571d5078SJeeja KP 
617571d5078SJeeja KP 	dai_map = &hdmi->dai_map[dai->id];
618571d5078SJeeja KP 	if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
619571d5078SJeeja KP 		ret = hdac_hdmi_enable_pin(hdac, dai_map);
620571d5078SJeeja KP 		if (ret < 0)
621571d5078SJeeja KP 			return ret;
622571d5078SJeeja KP 
623571d5078SJeeja KP 		return hdac_hdmi_playback_prepare(substream, dai);
624571d5078SJeeja KP 	}
625571d5078SJeeja KP 
626571d5078SJeeja KP 	return 0;
627571d5078SJeeja KP }
628571d5078SJeeja KP 
629b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
630b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
631b0362adbSSubhransu S. Prusty {
632b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
633b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
634b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
635b0362adbSSubhransu S. Prusty 
636b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
637b0362adbSSubhransu S. Prusty 
63854dfa1eaSSubhransu S. Prusty 	if (dai_map->pin) {
63954dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
64054dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
64154dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
64254dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
64354dfa1eaSSubhransu S. Prusty 
644b0362adbSSubhransu S. Prusty 		hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
645b0362adbSSubhransu S. Prusty 
64615b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
647b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
648148569fdSSubhransu S. Prusty 
649148569fdSSubhransu S. Prusty 		dai_map->pin = NULL;
650b0362adbSSubhransu S. Prusty 	}
65154dfa1eaSSubhransu S. Prusty }
652b0362adbSSubhransu S. Prusty 
65318382eadSSubhransu S. Prusty static int
65418382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
65518382eadSSubhransu S. Prusty {
65618382eadSSubhransu S. Prusty 	int err;
65718382eadSSubhransu S. Prusty 
65818382eadSSubhransu S. Prusty 	/* Only stereo supported as of now */
65918382eadSSubhransu S. Prusty 	cvt->params.channels_min = cvt->params.channels_max = 2;
66018382eadSSubhransu S. Prusty 
66118382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
66218382eadSSubhransu S. Prusty 			&cvt->params.rates,
66318382eadSSubhransu S. Prusty 			&cvt->params.formats,
66418382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
66518382eadSSubhransu S. Prusty 	if (err < 0)
66618382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
66718382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
66818382eadSSubhransu S. Prusty 			cvt->nid, err);
66918382eadSSubhransu S. Prusty 
67018382eadSSubhransu S. Prusty 	return err;
67118382eadSSubhransu S. Prusty }
67218382eadSSubhransu S. Prusty 
67379f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
67479f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
67579f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
67679f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
67779f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
67818382eadSSubhransu S. Prusty {
67918382eadSSubhransu S. Prusty 	w->id = id;
68079f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
68179f4e922SSubhransu S. Prusty 	if (!w->name)
68279f4e922SSubhransu S. Prusty 		return -ENOMEM;
68379f4e922SSubhransu S. Prusty 
68418382eadSSubhransu S. Prusty 	w->sname = stream;
68518382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
68618382eadSSubhransu S. Prusty 	w->shift = 0;
68779f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
68879f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
68979f4e922SSubhransu S. Prusty 	w->priv = priv;
69079f4e922SSubhransu S. Prusty 
69179f4e922SSubhransu S. Prusty 	return 0;
69218382eadSSubhransu S. Prusty }
69318382eadSSubhransu S. Prusty 
69418382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
69579f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
69679f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
69779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
69818382eadSSubhransu S. Prusty {
69918382eadSSubhransu S. Prusty 	route->sink = sink;
70018382eadSSubhransu S. Prusty 	route->source = src;
70118382eadSSubhransu S. Prusty 	route->control = control;
70279f4e922SSubhransu S. Prusty 	route->connected = handler;
70318382eadSSubhransu S. Prusty }
70418382eadSSubhransu S. Prusty 
7054a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
7064a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
7074a3478deSJeeja KP {
7084a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7094a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7104a3478deSJeeja KP 
7114a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7124a3478deSJeeja KP 		if (pcm->pin == pin)
7134a3478deSJeeja KP 			return pcm;
7144a3478deSJeeja KP 	}
7154a3478deSJeeja KP 
7164a3478deSJeeja KP 	return NULL;
7174a3478deSJeeja KP }
7184a3478deSJeeja KP 
7194a3478deSJeeja KP /*
7204a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7214a3478deSJeeja KP  */
7224a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
7234a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7244a3478deSJeeja KP {
7254a3478deSJeeja KP 	int ret;
7264a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7274a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7284a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
7294a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
7304a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7314a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7324a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7334a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7344a3478deSJeeja KP 
7354a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7364a3478deSJeeja KP 	if (ret < 0)
7374a3478deSJeeja KP 		return ret;
7384a3478deSJeeja KP 
7394a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7404a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7414a3478deSJeeja KP 		if (pcm->pin == pin)
7424a3478deSJeeja KP 			pcm->pin = NULL;
7434a3478deSJeeja KP 
7444a3478deSJeeja KP 		/*
7454a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7464a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7474a3478deSJeeja KP 		 */
7484a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
7494a3478deSJeeja KP 			pcm->pin = pin;
7504a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
7514a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7524a3478deSJeeja KP 					"jack report for pcm=%d\n",
7534a3478deSJeeja KP 					pcm->pcm_id);
7544a3478deSJeeja KP 
7554a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
7564a3478deSJeeja KP 			}
7574a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
7584a3478deSJeeja KP 			return ret;
7594a3478deSJeeja KP 		}
7604a3478deSJeeja KP 	}
7614a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
7624a3478deSJeeja KP 
7634a3478deSJeeja KP 	return ret;
7644a3478deSJeeja KP }
7654a3478deSJeeja KP 
76679f4e922SSubhransu S. Prusty /*
76779f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
76879f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
76979f4e922SSubhransu S. Prusty  * widget runtime.
77079f4e922SSubhransu S. Prusty  *
77179f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
77279f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
77379f4e922SSubhransu S. Prusty  * "NONE"
77479f4e922SSubhransu S. Prusty  */
77579f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
77679f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
77779f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
77879f4e922SSubhransu S. Prusty 				const char *widget_name)
77918382eadSSubhransu S. Prusty {
78079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
78179f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
78279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
78379f4e922SSubhransu S. Prusty 	struct soc_enum *se;
78479f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
78579f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
78679f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
78779f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
78879f4e922SSubhransu S. Prusty 	int i = 0;
78979f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
79018382eadSSubhransu S. Prusty 
79179f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
79279f4e922SSubhransu S. Prusty 	if (!kc)
79379f4e922SSubhransu S. Prusty 		return -ENOMEM;
79418382eadSSubhransu S. Prusty 
79579f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
79679f4e922SSubhransu S. Prusty 	if (!se)
79779f4e922SSubhransu S. Prusty 		return -ENOMEM;
79818382eadSSubhransu S. Prusty 
79979f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
80079f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
80179f4e922SSubhransu S. Prusty 	if (!kc->name)
80279f4e922SSubhransu S. Prusty 		return -ENOMEM;
80318382eadSSubhransu S. Prusty 
80479f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
80579f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
80679f4e922SSubhransu S. Prusty 	kc->access = 0;
80779f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
8084a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
80979f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
81079f4e922SSubhransu S. Prusty 
81179f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
81279f4e922SSubhransu S. Prusty 
81379f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
81479f4e922SSubhransu S. Prusty 	se->items = num_items;
81579f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
81679f4e922SSubhransu S. Prusty 
81779f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
81879f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
81979f4e922SSubhransu S. Prusty 	if (!items[i])
82079f4e922SSubhransu S. Prusty 		return -ENOMEM;
82179f4e922SSubhransu S. Prusty 
82279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
82379f4e922SSubhransu S. Prusty 		i++;
82479f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
82579f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
82679f4e922SSubhransu S. Prusty 		if (!items[i])
82779f4e922SSubhransu S. Prusty 			return -ENOMEM;
82879f4e922SSubhransu S. Prusty 	}
82979f4e922SSubhransu S. Prusty 
83079f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
83179f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
83279f4e922SSubhransu S. Prusty 	if (!se->texts)
83379f4e922SSubhransu S. Prusty 		return -ENOMEM;
83479f4e922SSubhransu S. Prusty 
83579f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
8364a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
83779f4e922SSubhransu S. Prusty }
83879f4e922SSubhransu S. Prusty 
83979f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
84079f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
84179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
84279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
84379f4e922SSubhransu S. Prusty {
84479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
84579f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
84679f4e922SSubhransu S. Prusty 	struct soc_enum *se;
84779f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
84879f4e922SSubhransu S. Prusty 	int i, j;
84979f4e922SSubhransu S. Prusty 
85079f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
85179f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
85279f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
85379f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
85479f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
85579f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
85679f4e922SSubhransu S. Prusty 					se->texts[j + 1],
85779f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
85879f4e922SSubhransu S. Prusty 
85979f4e922SSubhransu S. Prusty 			rindex++;
86079f4e922SSubhransu S. Prusty 		}
86179f4e922SSubhransu S. Prusty 
86279f4e922SSubhransu S. Prusty 		mux_index++;
86379f4e922SSubhransu S. Prusty 	}
86479f4e922SSubhransu S. Prusty }
86579f4e922SSubhransu S. Prusty 
86679f4e922SSubhransu S. Prusty /*
86779f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
86879f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
86979f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
87079f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
87179f4e922SSubhransu S. Prusty  *
87279f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
87379f4e922SSubhransu S. Prusty  *
87479f4e922SSubhransu S. Prusty  * Routes are added as below:
87579f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
87679f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
87779f4e922SSubhransu S. Prusty  *
87879f4e922SSubhransu S. Prusty  * Total route elements:
87979f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
88079f4e922SSubhransu S. Prusty  */
88179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
88279f4e922SSubhransu S. Prusty {
88379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
88479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
88579f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
88679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
88779f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
88879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
88979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
89079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
89179f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
89279f4e922SSubhransu S. Prusty 
89379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
89479f4e922SSubhransu S. Prusty 		return -EINVAL;
89579f4e922SSubhransu S. Prusty 
89679f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
89779f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
89879f4e922SSubhransu S. Prusty 		GFP_KERNEL);
89979f4e922SSubhransu S. Prusty 
90079f4e922SSubhransu S. Prusty 	if (!widgets)
90179f4e922SSubhransu S. Prusty 		return -ENOMEM;
90279f4e922SSubhransu S. Prusty 
90379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
90479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
90579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
90679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
90779f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
90879f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
90979f4e922SSubhransu S. Prusty 		if (ret < 0)
91079f4e922SSubhransu S. Prusty 			return ret;
91179f4e922SSubhransu S. Prusty 		i++;
91279f4e922SSubhransu S. Prusty 	}
91379f4e922SSubhransu S. Prusty 
91479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
91579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
91679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
91779f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
91879f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
91979f4e922SSubhransu S. Prusty 		if (ret < 0)
92079f4e922SSubhransu S. Prusty 			return ret;
92179f4e922SSubhransu S. Prusty 		i++;
92279f4e922SSubhransu S. Prusty 	}
92379f4e922SSubhransu S. Prusty 
92479f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
92579f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
92679f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
92779f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
92879f4e922SSubhransu S. Prusty 							widget_name);
92979f4e922SSubhransu S. Prusty 		if (ret < 0)
93079f4e922SSubhransu S. Prusty 			return ret;
93179f4e922SSubhransu S. Prusty 		i++;
93279f4e922SSubhransu S. Prusty 
93379f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
93479f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
93579f4e922SSubhransu S. Prusty 
93679f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
93779f4e922SSubhransu S. Prusty 		num_routes++;
93879f4e922SSubhransu S. Prusty 	}
93979f4e922SSubhransu S. Prusty 
94079f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
94179f4e922SSubhransu S. Prusty 							GFP_KERNEL);
94279f4e922SSubhransu S. Prusty 	if (!route)
94379f4e922SSubhransu S. Prusty 		return -ENOMEM;
94479f4e922SSubhransu S. Prusty 
94579f4e922SSubhransu S. Prusty 	i = 0;
94679f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
94779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
94879f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
94979f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
95079f4e922SSubhransu S. Prusty 
95179f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
95279f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
95379f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
95479f4e922SSubhransu S. Prusty 		i++;
95579f4e922SSubhransu S. Prusty 
95679f4e922SSubhransu S. Prusty 	}
95779f4e922SSubhransu S. Prusty 
95879f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
95979f4e922SSubhransu S. Prusty 
96079f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
96179f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
96279f4e922SSubhransu S. Prusty 
96379f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
96479f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
96579f4e922SSubhransu S. Prusty 
96679f4e922SSubhransu S. Prusty 	return 0;
96779f4e922SSubhransu S. Prusty 
96818382eadSSubhransu S. Prusty }
96918382eadSSubhransu S. Prusty 
97015b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
97118382eadSSubhransu S. Prusty {
97215b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
973148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
97415b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
975148569fdSSubhransu S. Prusty 	int dai_id = 0;
97618382eadSSubhransu S. Prusty 
977148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
97815b91447SSubhransu S. Prusty 		return -EINVAL;
97918382eadSSubhransu S. Prusty 
980148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
981148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
982148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
98315b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
98418382eadSSubhransu S. Prusty 
985148569fdSSubhransu S. Prusty 		dai_id++;
986148569fdSSubhransu S. Prusty 
987148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
988148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
989148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
990148569fdSSubhransu S. Prusty 			break;
991148569fdSSubhransu S. Prusty 		}
992148569fdSSubhransu S. Prusty 	}
99318382eadSSubhransu S. Prusty 
99415b91447SSubhransu S. Prusty 	return 0;
99515b91447SSubhransu S. Prusty }
99615b91447SSubhransu S. Prusty 
99715b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
99815b91447SSubhransu S. Prusty {
99915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
100015b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10014a3478deSJeeja KP 	char name[NAME_SIZE];
100215b91447SSubhransu S. Prusty 
100315b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
100415b91447SSubhransu S. Prusty 	if (!cvt)
100515b91447SSubhransu S. Prusty 		return -ENOMEM;
100615b91447SSubhransu S. Prusty 
100715b91447SSubhransu S. Prusty 	cvt->nid = nid;
10084a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10094a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
101015b91447SSubhransu S. Prusty 
101115b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
101215b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
101315b91447SSubhransu S. Prusty 
101415b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
101515b91447SSubhransu S. Prusty }
101615b91447SSubhransu S. Prusty 
1017*b7756edeSSubhransu S. Prusty static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1018*b7756edeSSubhransu S. Prusty 			struct hdac_hdmi_pin *pin)
1019*b7756edeSSubhransu S. Prusty {
1020*b7756edeSSubhransu S. Prusty 	pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1021*b7756edeSSubhransu S. Prusty }
1022*b7756edeSSubhransu S. Prusty 
1023b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1024b8a54545SSubhransu S. Prusty {
1025b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10264a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10274a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1028b8a54545SSubhransu S. Prusty 	int val;
1029b8a54545SSubhransu S. Prusty 
1030b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
1031b8a54545SSubhransu S. Prusty 
1032b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
1033b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1034b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
1035b8a54545SSubhransu S. Prusty 
1036b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1037b8a54545SSubhransu S. Prusty 						val, pin->nid);
1038b8a54545SSubhransu S. Prusty 
10394a3478deSJeeja KP 
10404a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1041b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1042b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1043b8a54545SSubhransu S. Prusty 
10444a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
10454a3478deSJeeja KP 
1046b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1047b8a54545SSubhransu S. Prusty 
1048b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1049b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
10504a3478deSJeeja KP 
10514a3478deSJeeja KP 		/*
10524a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
10534a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
10544a3478deSJeeja KP 		 * control select.
10554a3478deSJeeja KP 		 */
10564a3478deSJeeja KP 		if (pcm) {
10574a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
10584a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
10594a3478deSJeeja KP 
10604a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
10614a3478deSJeeja KP 		}
10624a3478deSJeeja KP 
10634a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1064b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
1065b8a54545SSubhransu S. Prusty 	}
1066b8a54545SSubhransu S. Prusty 
1067b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
1068b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
1069b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1070b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
1071b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
1072b8a54545SSubhransu S. Prusty 
10734a3478deSJeeja KP 			if (pcm) {
10744a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
10754a3478deSJeeja KP 					"jack report for pcm=%d\n",
10764a3478deSJeeja KP 					pcm->pcm_id);
10774a3478deSJeeja KP 
10784a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
10794a3478deSJeeja KP 			}
1080*b7756edeSSubhransu S. Prusty 			hdac_hdmi_parse_eld(edev, pin);
10814a3478deSJeeja KP 
1082b8a54545SSubhransu S. Prusty 			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1083b8a54545SSubhransu S. Prusty 					pin->eld.eld_buffer, pin->eld.eld_size);
1084b8a54545SSubhransu S. Prusty 		} else {
1085b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
1086b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
10874a3478deSJeeja KP 
10884a3478deSJeeja KP 			if (pcm) {
10894a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
10904a3478deSJeeja KP 					"jack report for pcm=%d\n",
10914a3478deSJeeja KP 					pcm->pcm_id);
10924a3478deSJeeja KP 
10934a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
1094b8a54545SSubhransu S. Prusty 			}
1095b8a54545SSubhransu S. Prusty 		}
10964a3478deSJeeja KP 	}
10974a3478deSJeeja KP 
10984a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
1099b8a54545SSubhransu S. Prusty 
1100b8a54545SSubhransu S. Prusty 	/*
1101b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
1102b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
1103b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
1104b8a54545SSubhransu S. Prusty 	 */
1105b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1106b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1107b8a54545SSubhransu S. Prusty 
1108b8a54545SSubhransu S. Prusty put_hdac_device:
1109b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1110b8a54545SSubhransu S. Prusty }
1111b8a54545SSubhransu S. Prusty 
1112b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1113b8a54545SSubhransu S. Prusty {
1114b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1115b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1116b8a54545SSubhransu S. Prusty 
1117b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1118b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1119b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1120b8a54545SSubhransu S. Prusty 
1121b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1122b8a54545SSubhransu S. Prusty }
1123b8a54545SSubhransu S. Prusty 
112415b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
112515b91447SSubhransu S. Prusty {
112615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
112715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
112815b91447SSubhransu S. Prusty 
112915b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
113015b91447SSubhransu S. Prusty 	if (!pin)
113115b91447SSubhransu S. Prusty 		return -ENOMEM;
113215b91447SSubhransu S. Prusty 
113315b91447SSubhransu S. Prusty 	pin->nid = nid;
113415b91447SSubhransu S. Prusty 
113515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
113615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
113715b91447SSubhransu S. Prusty 
1138b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1139b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1140b8a54545SSubhransu S. Prusty 
114115b91447SSubhransu S. Prusty 	return 0;
114218382eadSSubhransu S. Prusty }
114318382eadSSubhransu S. Prusty 
1144211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1145211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1146211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1147211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1148211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1149211caab7SSubhransu S. Prusty 
1150211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1151211caab7SSubhransu S. Prusty {
1152211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1153211caab7SSubhransu S. Prusty 
1154211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1155211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1156211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1157211caab7SSubhransu S. Prusty 		return;
1158211caab7SSubhransu S. Prusty 
1159211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1160211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1161211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1162211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1163211caab7SSubhransu S. Prusty 		return;
1164211caab7SSubhransu S. Prusty }
1165211caab7SSubhransu S. Prusty 
1166211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1167211caab7SSubhransu S. Prusty {
1168211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1169211caab7SSubhransu S. Prusty 
1170211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1171211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1172211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1173211caab7SSubhransu S. Prusty 		return;
1174211caab7SSubhransu S. Prusty 
1175211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1176211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1177211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1178211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1179211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1180211caab7SSubhransu S. Prusty 		return;
1181211caab7SSubhransu S. Prusty 
1182211caab7SSubhransu S. Prusty }
1183211caab7SSubhransu S. Prusty 
118417a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
118517a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
118617a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
118717a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
118817a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
1189571d5078SJeeja KP 	.trigger = hdac_hdmi_trigger,
119017a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
119117a42c45SSubhransu S. Prusty };
119217a42c45SSubhransu S. Prusty 
119317a42c45SSubhransu S. Prusty /*
119417a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
119517a42c45SSubhransu S. Prusty  * based on the number of converter queried.
119617a42c45SSubhransu S. Prusty  */
119717a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
119817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
119917a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
120017a42c45SSubhransu S. Prusty {
120117a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
120217a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
120317a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
120417a42c45SSubhransu S. Prusty 	int i = 0;
120517a42c45SSubhransu S. Prusty 	u32 rates, bps;
120617a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
120717a42c45SSubhransu S. Prusty 	u64 formats;
120817a42c45SSubhransu S. Prusty 	int ret;
120917a42c45SSubhransu S. Prusty 
121017a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
121117a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
121217a42c45SSubhransu S. Prusty 			GFP_KERNEL);
121317a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
121417a42c45SSubhransu S. Prusty 		return -ENOMEM;
121517a42c45SSubhransu S. Prusty 
121617a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
121717a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
121817a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
121917a42c45SSubhransu S. Prusty 		if (ret)
122017a42c45SSubhransu S. Prusty 			return ret;
122117a42c45SSubhransu S. Prusty 
122217a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
122317a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
122417a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
122517a42c45SSubhransu S. Prusty 
122617a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
122717a42c45SSubhransu S. Prusty 			return -ENOMEM;
122817a42c45SSubhransu S. Prusty 
122917a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
123017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
123117a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
123217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
123317a42c45SSubhransu S. Prusty 			return -ENOMEM;
123417a42c45SSubhransu S. Prusty 
123517a42c45SSubhransu S. Prusty 		/*
123617a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
123717a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
123817a42c45SSubhransu S. Prusty 		 */
123917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
124017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
124117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
124217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
124317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
124417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
124517a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
124617a42c45SSubhransu S. Prusty 
124717a42c45SSubhransu S. Prusty 		i++;
124817a42c45SSubhransu S. Prusty 	}
124917a42c45SSubhransu S. Prusty 
125017a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
125117a42c45SSubhransu S. Prusty 
125217a42c45SSubhransu S. Prusty 	return 0;
125317a42c45SSubhransu S. Prusty }
125417a42c45SSubhransu S. Prusty 
125518382eadSSubhransu S. Prusty /*
125618382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
125718382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
125818382eadSSubhransu S. Prusty  */
125917a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
126017a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
126118382eadSSubhransu S. Prusty {
126218382eadSSubhransu S. Prusty 	hda_nid_t nid;
12633c83ac23SSudip Mukherjee 	int i, num_nodes;
126418382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
126518382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
126615b91447SSubhransu S. Prusty 	int ret;
126718382eadSSubhransu S. Prusty 
1268211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1269211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1270211caab7SSubhransu S. Prusty 
12713c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1272541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
127318382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
127418382eadSSubhransu S. Prusty 		return -EINVAL;
127518382eadSSubhransu S. Prusty 	}
127618382eadSSubhransu S. Prusty 
12773c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
127818382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
127918382eadSSubhransu S. Prusty 
128018382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
128118382eadSSubhransu S. Prusty 		unsigned int caps;
128218382eadSSubhransu S. Prusty 		unsigned int type;
128318382eadSSubhransu S. Prusty 
128418382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
128518382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
128618382eadSSubhransu S. Prusty 
128718382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
128818382eadSSubhransu S. Prusty 			continue;
128918382eadSSubhransu S. Prusty 
129018382eadSSubhransu S. Prusty 		switch (type) {
129118382eadSSubhransu S. Prusty 
129218382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
129315b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
129415b91447SSubhransu S. Prusty 			if (ret < 0)
129515b91447SSubhransu S. Prusty 				return ret;
129618382eadSSubhransu S. Prusty 			break;
129718382eadSSubhransu S. Prusty 
129818382eadSSubhransu S. Prusty 		case AC_WID_PIN:
129915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
130015b91447SSubhransu S. Prusty 			if (ret < 0)
130115b91447SSubhransu S. Prusty 				return ret;
130218382eadSSubhransu S. Prusty 			break;
130318382eadSSubhransu S. Prusty 		}
130418382eadSSubhransu S. Prusty 	}
130518382eadSSubhransu S. Prusty 
130618382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
130718382eadSSubhransu S. Prusty 
130815b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
130918382eadSSubhransu S. Prusty 		return -EIO;
131018382eadSSubhransu S. Prusty 
131117a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
131217a42c45SSubhransu S. Prusty 	if (ret) {
131317a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
131417a42c45SSubhransu S. Prusty 							ret);
131517a42c45SSubhransu S. Prusty 		return ret;
131617a42c45SSubhransu S. Prusty 	}
131717a42c45SSubhransu S. Prusty 
131817a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
131917a42c45SSubhransu S. Prusty 
132015b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
132118382eadSSubhransu S. Prusty }
132218382eadSSubhransu S. Prusty 
1323b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1324b8a54545SSubhransu S. Prusty {
1325b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1326b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1327b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1328b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1329b8a54545SSubhransu S. Prusty 
1330b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1331b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1332b8a54545SSubhransu S. Prusty 
1333b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1334b8a54545SSubhransu S. Prusty 
1335b8a54545SSubhransu S. Prusty 	/*
1336b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1337b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1338b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1339b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1340b8a54545SSubhransu S. Prusty 	 */
1341b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1342b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1343b8a54545SSubhransu S. Prusty 		return;
1344b8a54545SSubhransu S. Prusty 
1345b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1346b8a54545SSubhransu S. Prusty 		return;
1347b8a54545SSubhransu S. Prusty 
1348b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1349b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1350b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1351b8a54545SSubhransu S. Prusty 	}
1352b8a54545SSubhransu S. Prusty }
1353b8a54545SSubhransu S. Prusty 
1354b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1355b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1356b8a54545SSubhransu S. Prusty };
1357b8a54545SSubhransu S. Prusty 
13584a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
13594a3478deSJeeja KP {
13604a3478deSJeeja KP 	char jack_name[NAME_SIZE];
13614a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
13624a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
13634a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
13644a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
13654a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
13664a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
13674a3478deSJeeja KP 
13684a3478deSJeeja KP 	/*
13694a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
13704a3478deSJeeja KP 	 * add to the pcm list
13714a3478deSJeeja KP 	 */
13724a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
13734a3478deSJeeja KP 	if (!pcm)
13744a3478deSJeeja KP 		return -ENOMEM;
13754a3478deSJeeja KP 	pcm->pcm_id = device;
13764a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
13774a3478deSJeeja KP 
13784a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
13794a3478deSJeeja KP 
13804a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
13814a3478deSJeeja KP 
13824a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
13834a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
13844a3478deSJeeja KP }
13854a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
13864a3478deSJeeja KP 
138718382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
138818382eadSSubhransu S. Prusty {
138918382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
139018382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
139118382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
139218382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1393b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1394b8a54545SSubhransu S. Prusty 	int ret;
139518382eadSSubhransu S. Prusty 
139618382eadSSubhransu S. Prusty 	edev->scodec = codec;
139718382eadSSubhransu S. Prusty 
139879f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
139979f4e922SSubhransu S. Prusty 	if (ret < 0)
140079f4e922SSubhransu S. Prusty 		return ret;
140118382eadSSubhransu S. Prusty 
1402b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1403b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1404b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1405b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1406b8a54545SSubhransu S. Prusty 				ret);
1407b8a54545SSubhransu S. Prusty 		return ret;
1408b8a54545SSubhransu S. Prusty 	}
1409b8a54545SSubhransu S. Prusty 
1410b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1411b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1412b8a54545SSubhransu S. Prusty 
141318382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
141418382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
141518382eadSSubhransu S. Prusty 
1416e342ac08SSubhransu S. Prusty 	/*
1417e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1418e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1419e342ac08SSubhransu S. Prusty 	 */
1420e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1421e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1422e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1423e342ac08SSubhransu S. Prusty 
1424e342ac08SSubhransu S. Prusty 	return 0;
1425e342ac08SSubhransu S. Prusty }
1426e342ac08SSubhransu S. Prusty 
1427e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1428e342ac08SSubhransu S. Prusty {
1429e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1430e342ac08SSubhransu S. Prusty 
1431e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
143218382eadSSubhransu S. Prusty 	return 0;
143318382eadSSubhransu S. Prusty }
143418382eadSSubhransu S. Prusty 
1435571d5078SJeeja KP #ifdef CONFIG_PM
1436571d5078SJeeja KP static int hdmi_codec_resume(struct snd_soc_codec *codec)
1437571d5078SJeeja KP {
1438571d5078SJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1439571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1440571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1441571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
1442571d5078SJeeja KP 	struct hdac_bus *bus = hdac->bus;
1443571d5078SJeeja KP 	int err;
1444571d5078SJeeja KP 	unsigned long timeout;
1445571d5078SJeeja KP 
1446571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1447571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1448571d5078SJeeja KP 
1449571d5078SJeeja KP 	/* Power up afg */
1450571d5078SJeeja KP 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1451571d5078SJeeja KP 
1452571d5078SJeeja KP 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1453571d5078SJeeja KP 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1454571d5078SJeeja KP 
1455571d5078SJeeja KP 		/* Wait till power state is set to D0 */
1456571d5078SJeeja KP 		timeout = jiffies + msecs_to_jiffies(1000);
1457571d5078SJeeja KP 		while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1458571d5078SJeeja KP 				&& time_before(jiffies, timeout)) {
1459571d5078SJeeja KP 			msleep(50);
1460571d5078SJeeja KP 		}
1461571d5078SJeeja KP 	}
1462571d5078SJeeja KP 
1463571d5078SJeeja KP 	/*
1464571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1465571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1466571d5078SJeeja KP 	 * all pins here.
1467571d5078SJeeja KP 	 */
1468571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1469571d5078SJeeja KP 		hdac_hdmi_present_sense(pin, 1);
1470571d5078SJeeja KP 
1471571d5078SJeeja KP 	/*
1472571d5078SJeeja KP 	 * Codec power is turned ON during controller resume.
1473571d5078SJeeja KP 	 * Turn it OFF here
1474571d5078SJeeja KP 	 */
1475571d5078SJeeja KP 	err = snd_hdac_display_power(bus, false);
1476571d5078SJeeja KP 	if (err < 0) {
1477571d5078SJeeja KP 		dev_err(bus->dev,
1478571d5078SJeeja KP 			"Cannot turn OFF display power on i915, err: %d\n",
1479571d5078SJeeja KP 			err);
1480571d5078SJeeja KP 		return err;
1481571d5078SJeeja KP 	}
1482571d5078SJeeja KP 
1483571d5078SJeeja KP 	return 0;
1484571d5078SJeeja KP }
1485571d5078SJeeja KP #else
1486571d5078SJeeja KP #define hdmi_codec_resume NULL
1487571d5078SJeeja KP #endif
1488571d5078SJeeja KP 
148918382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
149018382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1491e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
1492571d5078SJeeja KP 	.resume		= hdmi_codec_resume,
149318382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
149418382eadSSubhransu S. Prusty };
149518382eadSSubhransu S. Prusty 
149618382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
149718382eadSSubhransu S. Prusty {
149818382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
149918382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
150017a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
150117a42c45SSubhransu S. Prusty 	int num_dais = 0;
150218382eadSSubhransu S. Prusty 	int ret = 0;
150318382eadSSubhransu S. Prusty 
150418382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
150518382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
150618382eadSSubhransu S. Prusty 		return -ENOMEM;
150718382eadSSubhransu S. Prusty 
150818382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
150918382eadSSubhransu S. Prusty 
151018382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
151118382eadSSubhransu S. Prusty 
151215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
151315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
15144a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
15154a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
151615b91447SSubhransu S. Prusty 
1517aeaccef0SRamesh Babu 	/*
1518aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1519aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1520aeaccef0SRamesh Babu 	 */
1521aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1522aeaccef0SRamesh Babu 	if (ret < 0) {
1523aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1524aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1525aeaccef0SRamesh Babu 			ret);
1526aeaccef0SRamesh Babu 		return ret;
1527aeaccef0SRamesh Babu 	}
1528aeaccef0SRamesh Babu 
152917a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
153017a42c45SSubhransu S. Prusty 	if (ret < 0) {
153117a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
153217a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
153318382eadSSubhransu S. Prusty 		return ret;
153417a42c45SSubhransu S. Prusty 	}
153518382eadSSubhransu S. Prusty 
153618382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
153718382eadSSubhransu S. Prusty 	return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
153817a42c45SSubhransu S. Prusty 			hdmi_dais, num_dais);
153918382eadSSubhransu S. Prusty }
154018382eadSSubhransu S. Prusty 
154118382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
154218382eadSSubhransu S. Prusty {
154315b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
154415b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
154515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
15464a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
154715b91447SSubhransu S. Prusty 
154818382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
154918382eadSSubhransu S. Prusty 
15504a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
15514a3478deSJeeja KP 		pcm->cvt = NULL;
15524a3478deSJeeja KP 		pcm->pin = NULL;
15534a3478deSJeeja KP 		list_del(&pcm->head);
15544a3478deSJeeja KP 		kfree(pcm);
15554a3478deSJeeja KP 	}
15564a3478deSJeeja KP 
155715b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
155815b91447SSubhransu S. Prusty 		list_del(&cvt->head);
15594a3478deSJeeja KP 		kfree(cvt->name);
156015b91447SSubhransu S. Prusty 		kfree(cvt);
156115b91447SSubhransu S. Prusty 	}
156215b91447SSubhransu S. Prusty 
156315b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
156415b91447SSubhransu S. Prusty 		list_del(&pin->head);
156515b91447SSubhransu S. Prusty 		kfree(pin);
156615b91447SSubhransu S. Prusty 	}
156715b91447SSubhransu S. Prusty 
156818382eadSSubhransu S. Prusty 	return 0;
156918382eadSSubhransu S. Prusty }
157018382eadSSubhransu S. Prusty 
1571e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1572e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1573e342ac08SSubhransu S. Prusty {
1574e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1575e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
157607f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
15777ed49700SSubhransu S. Prusty 	unsigned long timeout;
157807f083abSSubhransu S. Prusty 	int err;
1579e342ac08SSubhransu S. Prusty 
1580e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1581e342ac08SSubhransu S. Prusty 
158207f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
158307f083abSSubhransu S. Prusty 	if (!bus)
158407f083abSSubhransu S. Prusty 		return 0;
158507f083abSSubhransu S. Prusty 
1586e342ac08SSubhransu S. Prusty 	/* Power down afg */
15877ed49700SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
1588e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1589e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1590e342ac08SSubhransu S. Prusty 
15917ed49700SSubhransu S. Prusty 		/* Wait till power state is set to D3 */
15927ed49700SSubhransu S. Prusty 		timeout = jiffies + msecs_to_jiffies(1000);
15937ed49700SSubhransu S. Prusty 		while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
15947ed49700SSubhransu S. Prusty 				&& time_before(jiffies, timeout)) {
15957ed49700SSubhransu S. Prusty 
15967ed49700SSubhransu S. Prusty 			msleep(50);
15977ed49700SSubhransu S. Prusty 		}
15987ed49700SSubhransu S. Prusty 	}
15997ed49700SSubhransu S. Prusty 
160007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
160107f083abSSubhransu S. Prusty 	if (err < 0) {
160207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
160307f083abSSubhransu S. Prusty 		return err;
160407f083abSSubhransu S. Prusty 	}
160507f083abSSubhransu S. Prusty 
1606e342ac08SSubhransu S. Prusty 	return 0;
1607e342ac08SSubhransu S. Prusty }
1608e342ac08SSubhransu S. Prusty 
1609e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1610e342ac08SSubhransu S. Prusty {
1611e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1612e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
161307f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
161407f083abSSubhransu S. Prusty 	int err;
1615e342ac08SSubhransu S. Prusty 
1616e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1617e342ac08SSubhransu S. Prusty 
161807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
161907f083abSSubhransu S. Prusty 	if (!bus)
162007f083abSSubhransu S. Prusty 		return 0;
162107f083abSSubhransu S. Prusty 
162207f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
162307f083abSSubhransu S. Prusty 	if (err < 0) {
162407f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
162507f083abSSubhransu S. Prusty 		return err;
162607f083abSSubhransu S. Prusty 	}
162707f083abSSubhransu S. Prusty 
1628ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1629ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1630ab85f5b3SSubhransu S. Prusty 
1631e342ac08SSubhransu S. Prusty 	/* Power up afg */
1632e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1633e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1634e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1635e342ac08SSubhransu S. Prusty 
1636e342ac08SSubhransu S. Prusty 	return 0;
1637e342ac08SSubhransu S. Prusty }
1638e342ac08SSubhransu S. Prusty #else
1639e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1640e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1641e342ac08SSubhransu S. Prusty #endif
1642e342ac08SSubhransu S. Prusty 
1643e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1644e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1645e342ac08SSubhransu S. Prusty };
1646e342ac08SSubhransu S. Prusty 
164718382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
164818382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1649e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
165018382eadSSubhransu S. Prusty 	{}
165118382eadSSubhransu S. Prusty };
165218382eadSSubhransu S. Prusty 
165318382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
165418382eadSSubhransu S. Prusty 
165518382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
165618382eadSSubhransu S. Prusty 	. hdac = {
165718382eadSSubhransu S. Prusty 		.driver = {
165818382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1659e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
166018382eadSSubhransu S. Prusty 		},
166118382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
166218382eadSSubhransu S. Prusty 	},
166318382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
166418382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
166518382eadSSubhransu S. Prusty };
166618382eadSSubhransu S. Prusty 
166718382eadSSubhransu S. Prusty static int __init hdmi_init(void)
166818382eadSSubhransu S. Prusty {
166918382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
167018382eadSSubhransu S. Prusty }
167118382eadSSubhransu S. Prusty 
167218382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
167318382eadSSubhransu S. Prusty {
167418382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
167518382eadSSubhransu S. Prusty }
167618382eadSSubhransu S. Prusty 
167718382eadSSubhransu S. Prusty module_init(hdmi_init);
167818382eadSSubhransu S. Prusty module_exit(hdmi_exit);
167918382eadSSubhransu S. Prusty 
168018382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
168118382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
168218382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
168318382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1684