xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 478f544e5b5538ccbcfdb18b4e9860a2f61ab921)
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 
63b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
64b8a54545SSubhransu S. Prusty 	bool	monitor_present;
65b8a54545SSubhransu S. Prusty 	bool	eld_valid;
66b8a54545SSubhransu S. Prusty 	int	eld_size;
67b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
68b8a54545SSubhransu S. Prusty };
69b8a54545SSubhransu S. Prusty 
7018382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7115b91447SSubhransu S. Prusty 	struct list_head head;
7218382eadSSubhransu S. Prusty 	hda_nid_t nid;
7318382eadSSubhransu S. Prusty 	int num_mux_nids;
7418382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
76b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev;
77b8a54545SSubhransu S. Prusty 	int repoll_count;
78b8a54545SSubhransu S. Prusty 	struct delayed_work work;
7918382eadSSubhransu S. Prusty };
8018382eadSSubhransu S. Prusty 
814a3478deSJeeja KP struct hdac_hdmi_pcm {
824a3478deSJeeja KP 	struct list_head head;
834a3478deSJeeja KP 	int pcm_id;
844a3478deSJeeja KP 	struct hdac_hdmi_pin *pin;
854a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
864a3478deSJeeja KP 	struct snd_jack *jack;
874a3478deSJeeja KP };
884a3478deSJeeja KP 
8918382eadSSubhransu S. Prusty struct hdac_hdmi_dai_pin_map {
9018382eadSSubhransu S. Prusty 	int dai_id;
9115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
9215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
9318382eadSSubhransu S. Prusty };
9418382eadSSubhransu S. Prusty 
9518382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
96148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
9715b91447SSubhransu S. Prusty 	struct list_head pin_list;
9815b91447SSubhransu S. Prusty 	struct list_head cvt_list;
994a3478deSJeeja KP 	struct list_head pcm_list;
10015b91447SSubhransu S. Prusty 	int num_pin;
10115b91447SSubhransu S. Prusty 	int num_cvt;
1024a3478deSJeeja KP 	struct mutex pin_mutex;
10318382eadSSubhransu S. Prusty };
10418382eadSSubhransu S. Prusty 
105e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106e342ac08SSubhransu S. Prusty {
10751b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
108e342ac08SSubhransu S. Prusty 
10951b2c425SGeliang Tang 	return to_ehdac_device(hdac);
110e342ac08SSubhransu S. Prusty }
111e342ac08SSubhransu S. Prusty 
1122428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1132428bca3SSubhransu S. Prusty {
1142428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1152428bca3SSubhransu S. Prusty }
1162428bca3SSubhransu S. Prusty 
1172428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1182428bca3SSubhransu S. Prusty {
1192428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1202428bca3SSubhransu S. Prusty }
1212428bca3SSubhransu S. Prusty 
1222428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1232428bca3SSubhransu S. Prusty 						void *eld)
1242428bca3SSubhransu S. Prusty {
1252428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1262428bca3SSubhransu S. Prusty 	int i;
1272428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1282428bca3SSubhransu S. Prusty 
1292428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1302428bca3SSubhransu S. Prusty 	if (!sad)
1312428bca3SSubhransu S. Prusty 		goto format_constraint;
1322428bca3SSubhransu S. Prusty 
1332428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1342428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1352428bca3SSubhransu S. Prusty 
1362428bca3SSubhransu S. Prusty 			/*
1372428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1382428bca3SSubhransu S. Prusty 			 * container so we set S32
1392428bca3SSubhransu S. Prusty 			 */
1402428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1412428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1422428bca3SSubhransu S. Prusty 		}
1432428bca3SSubhransu S. Prusty 	}
1442428bca3SSubhransu S. Prusty 
1452428bca3SSubhransu S. Prusty format_constraint:
1462428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1472428bca3SSubhransu S. Prusty 				formats);
1482428bca3SSubhransu S. Prusty 
1492428bca3SSubhransu S. Prusty }
1502428bca3SSubhransu S. Prusty 
151b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
152b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
154b8a54545SSubhransu S. Prusty {
155b8a54545SSubhransu S. Prusty 	unsigned int val;
156b8a54545SSubhransu S. Prusty 
157b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158b8a54545SSubhransu S. Prusty 							byte_index);
159b8a54545SSubhransu S. Prusty 
160b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161b8a54545SSubhransu S. Prusty 					byte_index, val);
162b8a54545SSubhransu S. Prusty 
163b8a54545SSubhransu S. Prusty 	return val;
164b8a54545SSubhransu S. Prusty }
165b8a54545SSubhransu S. Prusty 
166b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
167b8a54545SSubhransu S. Prusty {
168b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
169b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
170b8a54545SSubhransu S. Prusty }
171b8a54545SSubhransu S. Prusty 
172b8a54545SSubhransu S. Prusty /*
173b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
174b8a54545SSubhransu S. Prusty  * passed by user
175b8a54545SSubhransu S. Prusty  */
176b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
178b8a54545SSubhransu S. Prusty {
179b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
180b8a54545SSubhransu S. Prusty 
181b8a54545SSubhransu S. Prusty 	/*
182b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
183b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
184b8a54545SSubhransu S. Prusty 	 */
185b8a54545SSubhransu S. Prusty 
186b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
187b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
188b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
189b8a54545SSubhransu S. Prusty 		return -ERANGE;
190b8a54545SSubhransu S. Prusty 	}
191b8a54545SSubhransu S. Prusty 
192b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
193b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
194b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
195b8a54545SSubhransu S. Prusty 		/*
196b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
197b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
198b8a54545SSubhransu S. Prusty 		 */
199b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
200b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
201b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
202b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
203b8a54545SSubhransu S. Prusty 			goto error;
204b8a54545SSubhransu S. Prusty 		}
205b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
206b8a54545SSubhransu S. Prusty 		/*
207b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
208b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
209b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
210b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
211b8a54545SSubhransu S. Prusty 		 */
212b8a54545SSubhransu S. Prusty 		if (!val && !i) {
213b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
215b8a54545SSubhransu S. Prusty 			goto error;
216b8a54545SSubhransu S. Prusty 		}
217b8a54545SSubhransu S. Prusty 		buf[i] = val;
218b8a54545SSubhransu S. Prusty 	}
219b8a54545SSubhransu S. Prusty 
220b8a54545SSubhransu S. Prusty 	*eld_size = size;
221b8a54545SSubhransu S. Prusty error:
222b8a54545SSubhransu S. Prusty 	return ret;
223b8a54545SSubhransu S. Prusty }
224b8a54545SSubhransu S. Prusty 
225b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
226b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
227b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
228b0362adbSSubhransu S. Prusty {
229b0362adbSSubhransu S. Prusty 	unsigned int val;
230b0362adbSSubhransu S. Prusty 
231b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
232b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
233b0362adbSSubhransu S. Prusty 
234b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
235b0362adbSSubhransu S. Prusty 
236b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
237b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
238b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
239b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
240b0362adbSSubhransu S. Prusty 
241b0362adbSSubhransu S. Prusty 	return 0;
242b0362adbSSubhransu S. Prusty }
243b0362adbSSubhransu S. Prusty 
244a657f1d0SSubhransu S. Prusty static void
245a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
247a657f1d0SSubhransu S. Prusty {
248a657f1d0SSubhransu S. Prusty 	int val;
249a657f1d0SSubhransu S. Prusty 
250a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
251a657f1d0SSubhransu S. Prusty 
252a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
253a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
254a657f1d0SSubhransu S. Prusty }
255a657f1d0SSubhransu S. Prusty 
256*478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
257*478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
258*478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
259*478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
260*478f544eSSubhransu S. Prusty 
261*478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
262*478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
263*478f544eSSubhransu S. Prusty 	u8 CXT04;
264*478f544eSSubhransu S. Prusty 	u8 CA;
265*478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
266*478f544eSSubhransu S. Prusty };
267*478f544eSSubhransu S. Prusty 
268a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
269a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
270a657f1d0SSubhransu S. Prusty {
271a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
272a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
273*478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
274*478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
275*478f544eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
276*478f544eSSubhransu S. Prusty 	u8 *dip;
277a657f1d0SSubhransu S. Prusty 	int ret;
278a657f1d0SSubhransu S. Prusty 	int i;
279*478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
280*478f544eSSubhransu S. Prusty 	u8 conn_type;
281*478f544eSSubhransu S. Prusty 	int channels = 2;
282a657f1d0SSubhransu S. Prusty 
283*478f544eSSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
284*478f544eSSubhransu S. Prusty 		if (pin->nid == pin_nid)
285*478f544eSSubhransu S. Prusty 			break;
286*478f544eSSubhransu S. Prusty 	}
287a657f1d0SSubhransu S. Prusty 
288*478f544eSSubhransu S. Prusty 	eld_buf = pin->eld.eld_buffer;
289*478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
290a657f1d0SSubhransu S. Prusty 
291a657f1d0SSubhransu S. Prusty 	/* setup channel count */
292a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
293*478f544eSSubhransu S. Prusty 			    AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
294*478f544eSSubhransu S. Prusty 
295*478f544eSSubhransu S. Prusty 	switch (conn_type) {
296*478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
297*478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
298*478f544eSSubhransu S. Prusty 
299*478f544eSSubhransu S. Prusty 		/* Default stereo for now */
300*478f544eSSubhransu S. Prusty 		frame.channels = channels;
301a657f1d0SSubhransu S. Prusty 
302a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
303a657f1d0SSubhransu S. Prusty 		if (ret < 0)
304a657f1d0SSubhransu S. Prusty 			return ret;
305a657f1d0SSubhransu S. Prusty 
306*478f544eSSubhransu S. Prusty 		dip = (u8 *)&frame;
307*478f544eSSubhransu S. Prusty 		break;
308*478f544eSSubhransu S. Prusty 
309*478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
310*478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
311*478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
312*478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
313*478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
314*478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
315*478f544eSSubhransu S. Prusty 		dp_ai.CA	= 0;
316*478f544eSSubhransu S. Prusty 
317*478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
318*478f544eSSubhransu S. Prusty 		break;
319*478f544eSSubhransu S. Prusty 
320*478f544eSSubhransu S. Prusty 	default:
321*478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
322*478f544eSSubhransu S. Prusty 						conn_type);
323*478f544eSSubhransu S. Prusty 		return -EIO;
324*478f544eSSubhransu S. Prusty 	}
325*478f544eSSubhransu S. Prusty 
326a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
327a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
328a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
329a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
330a657f1d0SSubhransu S. Prusty 
331a657f1d0SSubhransu S. Prusty 
332a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
333a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
334*478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
335a657f1d0SSubhransu S. Prusty 		for (i = 0; i < sizeof(frame); i++)
336a657f1d0SSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
337a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
338*478f544eSSubhransu S. Prusty 	} else {
339*478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
340*478f544eSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
341*478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
342*478f544eSSubhransu S. Prusty 	}
343a657f1d0SSubhransu S. Prusty 
344a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
345a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
346a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
347a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
348a657f1d0SSubhransu S. Prusty 
349a657f1d0SSubhransu S. Prusty 	return 0;
350a657f1d0SSubhransu S. Prusty }
351a657f1d0SSubhransu S. Prusty 
352b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
353b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
354b0362adbSSubhransu S. Prusty {
355b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
35615b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
35715b91447SSubhransu S. Prusty 						pwr_state))
35815b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
359b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
360b0362adbSSubhransu S. Prusty 
361b0362adbSSubhransu S. Prusty 	/* Power up converter */
36215b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
36315b91447SSubhransu S. Prusty 						pwr_state))
36415b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
365b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
366b0362adbSSubhransu S. Prusty }
367b0362adbSSubhransu S. Prusty 
368b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
369b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
370b0362adbSSubhransu S. Prusty {
371b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
372b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
373b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
374b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
375a657f1d0SSubhransu S. Prusty 	int ret;
376b0362adbSSubhransu S. Prusty 
377b0362adbSSubhransu S. Prusty 	if (dai->id > 0) {
378b0362adbSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
379b0362adbSSubhransu S. Prusty 		return -ENODEV;
380b0362adbSSubhransu S. Prusty 	}
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);
401b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
402b0362adbSSubhransu S. Prusty 
403b0362adbSSubhransu S. Prusty 	if (dai->id > 0) {
404b0362adbSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
405b0362adbSSubhransu S. Prusty 		return -ENODEV;
406b0362adbSSubhransu S. Prusty 	}
407b0362adbSSubhransu S. Prusty 
408b0362adbSSubhransu S. Prusty 	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
4098d33ab24SSudip Mukherjee 	if (!dd)
4108d33ab24SSudip Mukherjee 		return -ENOMEM;
411b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
412b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
413b0362adbSSubhransu S. Prusty 			24, 0);
414b0362adbSSubhransu S. Prusty 
415b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
416b0362adbSSubhransu S. Prusty 
417b0362adbSSubhransu S. Prusty 	return 0;
418b0362adbSSubhransu S. Prusty }
419b0362adbSSubhransu S. Prusty 
420b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
421b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
422b0362adbSSubhransu S. Prusty {
423b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
424b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
425b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
426b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
427b0362adbSSubhransu S. Prusty 
428b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
429b0362adbSSubhransu S. Prusty 
43015b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
431b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
43215b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
433b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
434b0362adbSSubhransu S. Prusty 
435b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
436b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, NULL);
437b0362adbSSubhransu S. Prusty 
438b0362adbSSubhransu S. Prusty 	kfree(dd);
439b0362adbSSubhransu S. Prusty 
440b0362adbSSubhransu S. Prusty 	return 0;
441b0362adbSSubhransu S. Prusty }
442b0362adbSSubhransu S. Prusty 
443148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
444148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
445148569fdSSubhransu S. Prusty {
446148569fdSSubhransu S. Prusty 	int mux_idx;
447148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
448148569fdSSubhransu S. Prusty 
449148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
450148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
451148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
452148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
453148569fdSSubhransu S. Prusty 			break;
454148569fdSSubhransu S. Prusty 		}
455148569fdSSubhransu S. Prusty 	}
456148569fdSSubhransu S. Prusty 
457148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
458148569fdSSubhransu S. Prusty 		return -EIO;
459148569fdSSubhransu S. Prusty 
460148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
461148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
462148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
463148569fdSSubhransu S. Prusty 
464148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
465148569fdSSubhransu S. Prusty 
466148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
467148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
468148569fdSSubhransu S. Prusty 
469148569fdSSubhransu S. Prusty 	return 0;
470148569fdSSubhransu S. Prusty }
471148569fdSSubhransu S. Prusty 
472148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
473148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
474148569fdSSubhransu S. Prusty {
475148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
476148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
477148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
478148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
479148569fdSSubhransu S. Prusty 		return -EINVAL;
480148569fdSSubhransu S. Prusty 	}
481148569fdSSubhransu S. Prusty 
482148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
483148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
484148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
485148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
486148569fdSSubhransu S. Prusty 								pin->nid);
487148569fdSSubhransu S. Prusty 
488148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
489148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
490148569fdSSubhransu S. Prusty 
491148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
492148569fdSSubhransu S. Prusty }
493148569fdSSubhransu S. Prusty 
494148569fdSSubhransu S. Prusty /*
495148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
496148569fdSSubhransu S. Prusty  *
497148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
498148569fdSSubhransu S. Prusty  *
499148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
500148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
501148569fdSSubhransu S. Prusty  * connected.
502148569fdSSubhransu S. Prusty  */
503148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
504148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
505148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
506148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
507148569fdSSubhransu S. Prusty {
508148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
509148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
510148569fdSSubhransu S. Prusty 	int ret, i;
511148569fdSSubhransu S. Prusty 
512148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
513148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
514148569fdSSubhransu S. Prusty 			pin = pcm->pin;
515148569fdSSubhransu S. Prusty 			break;
516148569fdSSubhransu S. Prusty 		}
517148569fdSSubhransu S. Prusty 	}
518148569fdSSubhransu S. Prusty 
519148569fdSSubhransu S. Prusty 	if (pin) {
520148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
521148569fdSSubhransu S. Prusty 		if (ret < 0)
522148569fdSSubhransu S. Prusty 			return NULL;
523148569fdSSubhransu S. Prusty 
524148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
525148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
526148569fdSSubhransu S. Prusty 				return pin;
527148569fdSSubhransu S. Prusty 		}
528148569fdSSubhransu S. Prusty 	}
529148569fdSSubhransu S. Prusty 
530148569fdSSubhransu S. Prusty 	return NULL;
531148569fdSSubhransu S. Prusty }
532148569fdSSubhransu S. Prusty 
533b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
534b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
535b0362adbSSubhransu S. Prusty {
536b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
537b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
538b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
539148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
540148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
5412428bca3SSubhransu S. Prusty 	int ret;
542b0362adbSSubhransu S. Prusty 
543b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
544b0362adbSSubhransu S. Prusty 
545148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
546148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
547148569fdSSubhransu S. Prusty 	if (!pin)
548148569fdSSubhransu S. Prusty 		return -EIO;
549148569fdSSubhransu S. Prusty 
550148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
551148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
552b0362adbSSubhransu S. Prusty 
553b8a54545SSubhransu S. Prusty 		dev_err(&hdac->hdac.dev,
554148569fdSSubhransu S. Prusty 			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
555148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
556b8a54545SSubhransu S. Prusty 
557b0362adbSSubhransu S. Prusty 		return -ENODEV;
558b0362adbSSubhransu S. Prusty 	}
559b0362adbSSubhransu S. Prusty 
560148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
561b0362adbSSubhransu S. Prusty 
562148569fdSSubhransu S. Prusty 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
563148569fdSSubhransu S. Prusty 	if (ret < 0)
564148569fdSSubhransu S. Prusty 		return ret;
565b0362adbSSubhransu S. Prusty 
5662428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
567148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
5682428bca3SSubhransu S. Prusty 	if (ret < 0)
5692428bca3SSubhransu S. Prusty 		return ret;
570b0362adbSSubhransu S. Prusty 
5712428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
572148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
573b0362adbSSubhransu S. Prusty }
574b0362adbSSubhransu S. Prusty 
575b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
576b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
577b0362adbSSubhransu S. Prusty {
578b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
579b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
580b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
581b0362adbSSubhransu S. Prusty 
582b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
583b0362adbSSubhransu S. Prusty 
584b0362adbSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
585b0362adbSSubhransu S. Prusty 
58615b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
587b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
588148569fdSSubhransu S. Prusty 
589148569fdSSubhransu S. Prusty 	dai_map->pin = NULL;
590b0362adbSSubhransu S. Prusty }
591b0362adbSSubhransu S. Prusty 
59218382eadSSubhransu S. Prusty static int
59318382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
59418382eadSSubhransu S. Prusty {
59518382eadSSubhransu S. Prusty 	int err;
59618382eadSSubhransu S. Prusty 
59718382eadSSubhransu S. Prusty 	/* Only stereo supported as of now */
59818382eadSSubhransu S. Prusty 	cvt->params.channels_min = cvt->params.channels_max = 2;
59918382eadSSubhransu S. Prusty 
60018382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
60118382eadSSubhransu S. Prusty 			&cvt->params.rates,
60218382eadSSubhransu S. Prusty 			&cvt->params.formats,
60318382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
60418382eadSSubhransu S. Prusty 	if (err < 0)
60518382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
60618382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
60718382eadSSubhransu S. Prusty 			cvt->nid, err);
60818382eadSSubhransu S. Prusty 
60918382eadSSubhransu S. Prusty 	return err;
61018382eadSSubhransu S. Prusty }
61118382eadSSubhransu S. Prusty 
61279f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
61379f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
61479f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
61579f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
61679f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
61718382eadSSubhransu S. Prusty {
61818382eadSSubhransu S. Prusty 	w->id = id;
61979f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
62079f4e922SSubhransu S. Prusty 	if (!w->name)
62179f4e922SSubhransu S. Prusty 		return -ENOMEM;
62279f4e922SSubhransu S. Prusty 
62318382eadSSubhransu S. Prusty 	w->sname = stream;
62418382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
62518382eadSSubhransu S. Prusty 	w->shift = 0;
62679f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
62779f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
62879f4e922SSubhransu S. Prusty 	w->priv = priv;
62979f4e922SSubhransu S. Prusty 
63079f4e922SSubhransu S. Prusty 	return 0;
63118382eadSSubhransu S. Prusty }
63218382eadSSubhransu S. Prusty 
63318382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
63479f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
63579f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
63679f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
63718382eadSSubhransu S. Prusty {
63818382eadSSubhransu S. Prusty 	route->sink = sink;
63918382eadSSubhransu S. Prusty 	route->source = src;
64018382eadSSubhransu S. Prusty 	route->control = control;
64179f4e922SSubhransu S. Prusty 	route->connected = handler;
64218382eadSSubhransu S. Prusty }
64318382eadSSubhransu S. Prusty 
6444a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
6454a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
6464a3478deSJeeja KP {
6474a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6484a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
6494a3478deSJeeja KP 
6504a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
6514a3478deSJeeja KP 		if (pcm->pin == pin)
6524a3478deSJeeja KP 			return pcm;
6534a3478deSJeeja KP 	}
6544a3478deSJeeja KP 
6554a3478deSJeeja KP 	return NULL;
6564a3478deSJeeja KP }
6574a3478deSJeeja KP 
6584a3478deSJeeja KP /*
6594a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
6604a3478deSJeeja KP  */
6614a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
6624a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
6634a3478deSJeeja KP {
6644a3478deSJeeja KP 	int ret;
6654a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
6664a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
6674a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
6684a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
6694a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
6704a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6714a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
6724a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
6734a3478deSJeeja KP 
6744a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
6754a3478deSJeeja KP 	if (ret < 0)
6764a3478deSJeeja KP 		return ret;
6774a3478deSJeeja KP 
6784a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
6794a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
6804a3478deSJeeja KP 		if (pcm->pin == pin)
6814a3478deSJeeja KP 			pcm->pin = NULL;
6824a3478deSJeeja KP 
6834a3478deSJeeja KP 		/*
6844a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
6854a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
6864a3478deSJeeja KP 		 */
6874a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
6884a3478deSJeeja KP 			pcm->pin = pin;
6894a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
6904a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
6914a3478deSJeeja KP 					"jack report for pcm=%d\n",
6924a3478deSJeeja KP 					pcm->pcm_id);
6934a3478deSJeeja KP 
6944a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
6954a3478deSJeeja KP 			}
6964a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
6974a3478deSJeeja KP 			return ret;
6984a3478deSJeeja KP 		}
6994a3478deSJeeja KP 	}
7004a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
7014a3478deSJeeja KP 
7024a3478deSJeeja KP 	return ret;
7034a3478deSJeeja KP }
7044a3478deSJeeja KP 
70579f4e922SSubhransu S. Prusty /*
70679f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
70779f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
70879f4e922SSubhransu S. Prusty  * widget runtime.
70979f4e922SSubhransu S. Prusty  *
71079f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
71179f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
71279f4e922SSubhransu S. Prusty  * "NONE"
71379f4e922SSubhransu S. Prusty  */
71479f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
71579f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
71679f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
71779f4e922SSubhransu S. Prusty 				const char *widget_name)
71818382eadSSubhransu S. Prusty {
71979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
72079f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
72179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
72279f4e922SSubhransu S. Prusty 	struct soc_enum *se;
72379f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
72479f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
72579f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
72679f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
72779f4e922SSubhransu S. Prusty 	int i = 0;
72879f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
72918382eadSSubhransu S. Prusty 
73079f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
73179f4e922SSubhransu S. Prusty 	if (!kc)
73279f4e922SSubhransu S. Prusty 		return -ENOMEM;
73318382eadSSubhransu S. Prusty 
73479f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
73579f4e922SSubhransu S. Prusty 	if (!se)
73679f4e922SSubhransu S. Prusty 		return -ENOMEM;
73718382eadSSubhransu S. Prusty 
73879f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
73979f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
74079f4e922SSubhransu S. Prusty 	if (!kc->name)
74179f4e922SSubhransu S. Prusty 		return -ENOMEM;
74218382eadSSubhransu S. Prusty 
74379f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
74479f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
74579f4e922SSubhransu S. Prusty 	kc->access = 0;
74679f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
7474a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
74879f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
74979f4e922SSubhransu S. Prusty 
75079f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
75179f4e922SSubhransu S. Prusty 
75279f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
75379f4e922SSubhransu S. Prusty 	se->items = num_items;
75479f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
75579f4e922SSubhransu S. Prusty 
75679f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
75779f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
75879f4e922SSubhransu S. Prusty 	if (!items[i])
75979f4e922SSubhransu S. Prusty 		return -ENOMEM;
76079f4e922SSubhransu S. Prusty 
76179f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
76279f4e922SSubhransu S. Prusty 		i++;
76379f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
76479f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
76579f4e922SSubhransu S. Prusty 		if (!items[i])
76679f4e922SSubhransu S. Prusty 			return -ENOMEM;
76779f4e922SSubhransu S. Prusty 	}
76879f4e922SSubhransu S. Prusty 
76979f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
77079f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
77179f4e922SSubhransu S. Prusty 	if (!se->texts)
77279f4e922SSubhransu S. Prusty 		return -ENOMEM;
77379f4e922SSubhransu S. Prusty 
77479f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
7754a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
77679f4e922SSubhransu S. Prusty }
77779f4e922SSubhransu S. Prusty 
77879f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
77979f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
78079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
78179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
78279f4e922SSubhransu S. Prusty {
78379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
78479f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
78579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
78679f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
78779f4e922SSubhransu S. Prusty 	int i, j;
78879f4e922SSubhransu S. Prusty 
78979f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
79079f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
79179f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
79279f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
79379f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
79479f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
79579f4e922SSubhransu S. Prusty 					se->texts[j + 1],
79679f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
79779f4e922SSubhransu S. Prusty 
79879f4e922SSubhransu S. Prusty 			rindex++;
79979f4e922SSubhransu S. Prusty 		}
80079f4e922SSubhransu S. Prusty 
80179f4e922SSubhransu S. Prusty 		mux_index++;
80279f4e922SSubhransu S. Prusty 	}
80379f4e922SSubhransu S. Prusty }
80479f4e922SSubhransu S. Prusty 
80579f4e922SSubhransu S. Prusty /*
80679f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
80779f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
80879f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
80979f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
81079f4e922SSubhransu S. Prusty  *
81179f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
81279f4e922SSubhransu S. Prusty  *
81379f4e922SSubhransu S. Prusty  * Routes are added as below:
81479f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
81579f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
81679f4e922SSubhransu S. Prusty  *
81779f4e922SSubhransu S. Prusty  * Total route elements:
81879f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
81979f4e922SSubhransu S. Prusty  */
82079f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
82179f4e922SSubhransu S. Prusty {
82279f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
82379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
82479f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
82579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
82679f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
82779f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
82879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
82979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
83079f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
83179f4e922SSubhransu S. Prusty 
83279f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
83379f4e922SSubhransu S. Prusty 		return -EINVAL;
83479f4e922SSubhransu S. Prusty 
83579f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
83679f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
83779f4e922SSubhransu S. Prusty 		GFP_KERNEL);
83879f4e922SSubhransu S. Prusty 
83979f4e922SSubhransu S. Prusty 	if (!widgets)
84079f4e922SSubhransu S. Prusty 		return -ENOMEM;
84179f4e922SSubhransu S. Prusty 
84279f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
84379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
84479f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
84579f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
84679f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
84779f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
84879f4e922SSubhransu S. Prusty 		if (ret < 0)
84979f4e922SSubhransu S. Prusty 			return ret;
85079f4e922SSubhransu S. Prusty 		i++;
85179f4e922SSubhransu S. Prusty 	}
85279f4e922SSubhransu S. Prusty 
85379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
85479f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
85579f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
85679f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
85779f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
85879f4e922SSubhransu S. Prusty 		if (ret < 0)
85979f4e922SSubhransu S. Prusty 			return ret;
86079f4e922SSubhransu S. Prusty 		i++;
86179f4e922SSubhransu S. Prusty 	}
86279f4e922SSubhransu S. Prusty 
86379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
86479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
86579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
86679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
86779f4e922SSubhransu S. Prusty 							widget_name);
86879f4e922SSubhransu S. Prusty 		if (ret < 0)
86979f4e922SSubhransu S. Prusty 			return ret;
87079f4e922SSubhransu S. Prusty 		i++;
87179f4e922SSubhransu S. Prusty 
87279f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
87379f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
87479f4e922SSubhransu S. Prusty 
87579f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
87679f4e922SSubhransu S. Prusty 		num_routes++;
87779f4e922SSubhransu S. Prusty 	}
87879f4e922SSubhransu S. Prusty 
87979f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
88079f4e922SSubhransu S. Prusty 							GFP_KERNEL);
88179f4e922SSubhransu S. Prusty 	if (!route)
88279f4e922SSubhransu S. Prusty 		return -ENOMEM;
88379f4e922SSubhransu S. Prusty 
88479f4e922SSubhransu S. Prusty 	i = 0;
88579f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
88679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
88779f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
88879f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
88979f4e922SSubhransu S. Prusty 
89079f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
89179f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
89279f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
89379f4e922SSubhransu S. Prusty 		i++;
89479f4e922SSubhransu S. Prusty 
89579f4e922SSubhransu S. Prusty 	}
89679f4e922SSubhransu S. Prusty 
89779f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
89879f4e922SSubhransu S. Prusty 
89979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
90079f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
90179f4e922SSubhransu S. Prusty 
90279f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
90379f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
90479f4e922SSubhransu S. Prusty 
90579f4e922SSubhransu S. Prusty 	return 0;
90679f4e922SSubhransu S. Prusty 
90718382eadSSubhransu S. Prusty }
90818382eadSSubhransu S. Prusty 
90915b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
91018382eadSSubhransu S. Prusty {
91115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
912148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
91315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
914148569fdSSubhransu S. Prusty 	int dai_id = 0;
91518382eadSSubhransu S. Prusty 
916148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
91715b91447SSubhransu S. Prusty 		return -EINVAL;
91818382eadSSubhransu S. Prusty 
919148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
920148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
921148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
92215b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
92318382eadSSubhransu S. Prusty 
92418382eadSSubhransu S. Prusty 		/* Enable transmission */
92515b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
92618382eadSSubhransu S. Prusty 				AC_VERB_SET_DIGI_CONVERT_1, 1);
92718382eadSSubhransu S. Prusty 
92818382eadSSubhransu S. Prusty 		/* Category Code (CC) to zero */
92915b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
93018382eadSSubhransu S. Prusty 				AC_VERB_SET_DIGI_CONVERT_2, 0);
93118382eadSSubhransu S. Prusty 
932148569fdSSubhransu S. Prusty 		dai_id++;
933148569fdSSubhransu S. Prusty 
934148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
935148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
936148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
937148569fdSSubhransu S. Prusty 			break;
938148569fdSSubhransu S. Prusty 		}
939148569fdSSubhransu S. Prusty 	}
94018382eadSSubhransu S. Prusty 
94115b91447SSubhransu S. Prusty 	return 0;
94215b91447SSubhransu S. Prusty }
94315b91447SSubhransu S. Prusty 
94415b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
94515b91447SSubhransu S. Prusty {
94615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
94715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
9484a3478deSJeeja KP 	char name[NAME_SIZE];
94915b91447SSubhransu S. Prusty 
95015b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
95115b91447SSubhransu S. Prusty 	if (!cvt)
95215b91447SSubhransu S. Prusty 		return -ENOMEM;
95315b91447SSubhransu S. Prusty 
95415b91447SSubhransu S. Prusty 	cvt->nid = nid;
9554a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
9564a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
95715b91447SSubhransu S. Prusty 
95815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
95915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
96015b91447SSubhransu S. Prusty 
96115b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
96215b91447SSubhransu S. Prusty }
96315b91447SSubhransu S. Prusty 
964b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
965b8a54545SSubhransu S. Prusty {
966b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
9674a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
9684a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
969b8a54545SSubhransu S. Prusty 	int val;
970b8a54545SSubhransu S. Prusty 
971b8a54545SSubhransu S. Prusty 	if (!edev)
972b8a54545SSubhransu S. Prusty 		return;
973b8a54545SSubhransu S. Prusty 
974b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
975b8a54545SSubhransu S. Prusty 
976b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
977b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
978b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
979b8a54545SSubhransu S. Prusty 
980b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
981b8a54545SSubhransu S. Prusty 						val, pin->nid);
982b8a54545SSubhransu S. Prusty 
9834a3478deSJeeja KP 
9844a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
985b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
986b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
987b8a54545SSubhransu S. Prusty 
9884a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
9894a3478deSJeeja KP 
990b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
991b8a54545SSubhransu S. Prusty 
992b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
993b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
9944a3478deSJeeja KP 
9954a3478deSJeeja KP 		/*
9964a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
9974a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
9984a3478deSJeeja KP 		 * control select.
9994a3478deSJeeja KP 		 */
10004a3478deSJeeja KP 		if (pcm) {
10014a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
10024a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
10034a3478deSJeeja KP 
10044a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
10054a3478deSJeeja KP 		}
10064a3478deSJeeja KP 
10074a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1008b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
1009b8a54545SSubhransu S. Prusty 	}
1010b8a54545SSubhransu S. Prusty 
1011b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
1012b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
1013b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1014b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
1015b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
1016b8a54545SSubhransu S. Prusty 
10174a3478deSJeeja KP 			if (pcm) {
10184a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
10194a3478deSJeeja KP 					"jack report for pcm=%d\n",
10204a3478deSJeeja KP 					pcm->pcm_id);
10214a3478deSJeeja KP 
10224a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
10234a3478deSJeeja KP 			}
10244a3478deSJeeja KP 
1025b8a54545SSubhransu S. Prusty 			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1026b8a54545SSubhransu S. Prusty 					pin->eld.eld_buffer, pin->eld.eld_size);
1027b8a54545SSubhransu S. Prusty 		} else {
1028b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
1029b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
10304a3478deSJeeja KP 
10314a3478deSJeeja KP 			if (pcm) {
10324a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
10334a3478deSJeeja KP 					"jack report for pcm=%d\n",
10344a3478deSJeeja KP 					pcm->pcm_id);
10354a3478deSJeeja KP 
10364a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
1037b8a54545SSubhransu S. Prusty 			}
1038b8a54545SSubhransu S. Prusty 		}
10394a3478deSJeeja KP 	}
10404a3478deSJeeja KP 
10414a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
1042b8a54545SSubhransu S. Prusty 
1043b8a54545SSubhransu S. Prusty 	/*
1044b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
1045b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
1046b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
1047b8a54545SSubhransu S. Prusty 	 */
1048b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1049b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1050b8a54545SSubhransu S. Prusty 
1051b8a54545SSubhransu S. Prusty put_hdac_device:
1052b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1053b8a54545SSubhransu S. Prusty }
1054b8a54545SSubhransu S. Prusty 
1055b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1056b8a54545SSubhransu S. Prusty {
1057b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1058b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1059b8a54545SSubhransu S. Prusty 
1060b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1061b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1062b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1063b8a54545SSubhransu S. Prusty 
1064b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1065b8a54545SSubhransu S. Prusty }
1066b8a54545SSubhransu S. Prusty 
106715b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
106815b91447SSubhransu S. Prusty {
106915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
107015b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
107115b91447SSubhransu S. Prusty 
107215b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
107315b91447SSubhransu S. Prusty 	if (!pin)
107415b91447SSubhransu S. Prusty 		return -ENOMEM;
107515b91447SSubhransu S. Prusty 
107615b91447SSubhransu S. Prusty 	pin->nid = nid;
107715b91447SSubhransu S. Prusty 
107815b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
107915b91447SSubhransu S. Prusty 	hdmi->num_pin++;
108015b91447SSubhransu S. Prusty 
1081b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1082b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1083b8a54545SSubhransu S. Prusty 
108415b91447SSubhransu S. Prusty 	return 0;
108518382eadSSubhransu S. Prusty }
108618382eadSSubhransu S. Prusty 
1087211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1088211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1089211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1090211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1091211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1092211caab7SSubhransu S. Prusty 
1093211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1094211caab7SSubhransu S. Prusty {
1095211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1096211caab7SSubhransu S. Prusty 
1097211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1098211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1099211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1100211caab7SSubhransu S. Prusty 		return;
1101211caab7SSubhransu S. Prusty 
1102211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1103211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1104211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1105211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1106211caab7SSubhransu S. Prusty 		return;
1107211caab7SSubhransu S. Prusty }
1108211caab7SSubhransu S. Prusty 
1109211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1110211caab7SSubhransu S. Prusty {
1111211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1112211caab7SSubhransu S. Prusty 
1113211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1114211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1115211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1116211caab7SSubhransu S. Prusty 		return;
1117211caab7SSubhransu S. Prusty 
1118211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1119211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1120211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1121211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1122211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1123211caab7SSubhransu S. Prusty 		return;
1124211caab7SSubhransu S. Prusty 
1125211caab7SSubhransu S. Prusty }
1126211caab7SSubhransu S. Prusty 
112717a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
112817a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
112917a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
113017a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
113117a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
113217a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
113317a42c45SSubhransu S. Prusty };
113417a42c45SSubhransu S. Prusty 
113517a42c45SSubhransu S. Prusty /*
113617a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
113717a42c45SSubhransu S. Prusty  * based on the number of converter queried.
113817a42c45SSubhransu S. Prusty  */
113917a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
114017a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
114117a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
114217a42c45SSubhransu S. Prusty {
114317a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
114417a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
114517a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
114617a42c45SSubhransu S. Prusty 	int i = 0;
114717a42c45SSubhransu S. Prusty 	u32 rates, bps;
114817a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
114917a42c45SSubhransu S. Prusty 	u64 formats;
115017a42c45SSubhransu S. Prusty 	int ret;
115117a42c45SSubhransu S. Prusty 
115217a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
115317a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
115417a42c45SSubhransu S. Prusty 			GFP_KERNEL);
115517a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
115617a42c45SSubhransu S. Prusty 		return -ENOMEM;
115717a42c45SSubhransu S. Prusty 
115817a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
115917a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
116017a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
116117a42c45SSubhransu S. Prusty 		if (ret)
116217a42c45SSubhransu S. Prusty 			return ret;
116317a42c45SSubhransu S. Prusty 
116417a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
116517a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
116617a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
116717a42c45SSubhransu S. Prusty 
116817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
116917a42c45SSubhransu S. Prusty 			return -ENOMEM;
117017a42c45SSubhransu S. Prusty 
117117a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
117217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
117317a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
117417a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
117517a42c45SSubhransu S. Prusty 			return -ENOMEM;
117617a42c45SSubhransu S. Prusty 
117717a42c45SSubhransu S. Prusty 		/*
117817a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
117917a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
118017a42c45SSubhransu S. Prusty 		 */
118117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
118217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
118317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
118417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
118517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
118617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
118717a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
118817a42c45SSubhransu S. Prusty 
118917a42c45SSubhransu S. Prusty 		i++;
119017a42c45SSubhransu S. Prusty 	}
119117a42c45SSubhransu S. Prusty 
119217a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
119317a42c45SSubhransu S. Prusty 
119417a42c45SSubhransu S. Prusty 	return 0;
119517a42c45SSubhransu S. Prusty }
119617a42c45SSubhransu S. Prusty 
119718382eadSSubhransu S. Prusty /*
119818382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
119918382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
120018382eadSSubhransu S. Prusty  */
120117a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
120217a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
120318382eadSSubhransu S. Prusty {
120418382eadSSubhransu S. Prusty 	hda_nid_t nid;
12053c83ac23SSudip Mukherjee 	int i, num_nodes;
120618382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
120718382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
120815b91447SSubhransu S. Prusty 	int ret;
120918382eadSSubhransu S. Prusty 
1210211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1211211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1212211caab7SSubhransu S. Prusty 
12133c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1214541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
121518382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
121618382eadSSubhransu S. Prusty 		return -EINVAL;
121718382eadSSubhransu S. Prusty 	}
121818382eadSSubhransu S. Prusty 
12193c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
122018382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
122118382eadSSubhransu S. Prusty 
122218382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
122318382eadSSubhransu S. Prusty 		unsigned int caps;
122418382eadSSubhransu S. Prusty 		unsigned int type;
122518382eadSSubhransu S. Prusty 
122618382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
122718382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
122818382eadSSubhransu S. Prusty 
122918382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
123018382eadSSubhransu S. Prusty 			continue;
123118382eadSSubhransu S. Prusty 
123218382eadSSubhransu S. Prusty 		switch (type) {
123318382eadSSubhransu S. Prusty 
123418382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
123515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
123615b91447SSubhransu S. Prusty 			if (ret < 0)
123715b91447SSubhransu S. Prusty 				return ret;
123818382eadSSubhransu S. Prusty 			break;
123918382eadSSubhransu S. Prusty 
124018382eadSSubhransu S. Prusty 		case AC_WID_PIN:
124115b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
124215b91447SSubhransu S. Prusty 			if (ret < 0)
124315b91447SSubhransu S. Prusty 				return ret;
124418382eadSSubhransu S. Prusty 			break;
124518382eadSSubhransu S. Prusty 		}
124618382eadSSubhransu S. Prusty 	}
124718382eadSSubhransu S. Prusty 
124818382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
124918382eadSSubhransu S. Prusty 
125015b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
125118382eadSSubhransu S. Prusty 		return -EIO;
125218382eadSSubhransu S. Prusty 
125317a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
125417a42c45SSubhransu S. Prusty 	if (ret) {
125517a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
125617a42c45SSubhransu S. Prusty 							ret);
125717a42c45SSubhransu S. Prusty 		return ret;
125817a42c45SSubhransu S. Prusty 	}
125917a42c45SSubhransu S. Prusty 
126017a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
126117a42c45SSubhransu S. Prusty 
126215b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
126318382eadSSubhransu S. Prusty }
126418382eadSSubhransu S. Prusty 
1265b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1266b8a54545SSubhransu S. Prusty {
1267b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1268b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1269b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1270b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1271b8a54545SSubhransu S. Prusty 
1272b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1273b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1274b8a54545SSubhransu S. Prusty 
1275b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1276b8a54545SSubhransu S. Prusty 
1277b8a54545SSubhransu S. Prusty 	/*
1278b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1279b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1280b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1281b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1282b8a54545SSubhransu S. Prusty 	 */
1283b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1284b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1285b8a54545SSubhransu S. Prusty 		return;
1286b8a54545SSubhransu S. Prusty 
1287b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1288b8a54545SSubhransu S. Prusty 		return;
1289b8a54545SSubhransu S. Prusty 
1290b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1291b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1292b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1293b8a54545SSubhransu S. Prusty 	}
1294b8a54545SSubhransu S. Prusty }
1295b8a54545SSubhransu S. Prusty 
1296b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1297b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1298b8a54545SSubhransu S. Prusty };
1299b8a54545SSubhransu S. Prusty 
13004a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
13014a3478deSJeeja KP {
13024a3478deSJeeja KP 	char jack_name[NAME_SIZE];
13034a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
13044a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
13054a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
13064a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
13074a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
13084a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
13094a3478deSJeeja KP 
13104a3478deSJeeja KP 	/*
13114a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
13124a3478deSJeeja KP 	 * add to the pcm list
13134a3478deSJeeja KP 	 */
13144a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
13154a3478deSJeeja KP 	if (!pcm)
13164a3478deSJeeja KP 		return -ENOMEM;
13174a3478deSJeeja KP 	pcm->pcm_id = device;
13184a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
13194a3478deSJeeja KP 
13204a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
13214a3478deSJeeja KP 
13224a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
13234a3478deSJeeja KP 
13244a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
13254a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
13264a3478deSJeeja KP }
13274a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
13284a3478deSJeeja KP 
132918382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
133018382eadSSubhransu S. Prusty {
133118382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
133218382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
133318382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
133418382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1335b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1336b8a54545SSubhransu S. Prusty 	int ret;
133718382eadSSubhransu S. Prusty 
133818382eadSSubhransu S. Prusty 	edev->scodec = codec;
133918382eadSSubhransu S. Prusty 
134079f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
134179f4e922SSubhransu S. Prusty 	if (ret < 0)
134279f4e922SSubhransu S. Prusty 		return ret;
134318382eadSSubhransu S. Prusty 
1344b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1345b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1346b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1347b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1348b8a54545SSubhransu S. Prusty 				ret);
1349b8a54545SSubhransu S. Prusty 		return ret;
1350b8a54545SSubhransu S. Prusty 	}
1351b8a54545SSubhransu S. Prusty 
1352b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1353b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1354b8a54545SSubhransu S. Prusty 
135518382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
135618382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
135718382eadSSubhransu S. Prusty 
1358e342ac08SSubhransu S. Prusty 	/*
1359e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1360e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1361e342ac08SSubhransu S. Prusty 	 */
1362e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1363e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1364e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1365e342ac08SSubhransu S. Prusty 
1366e342ac08SSubhransu S. Prusty 	return 0;
1367e342ac08SSubhransu S. Prusty }
1368e342ac08SSubhransu S. Prusty 
1369e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1370e342ac08SSubhransu S. Prusty {
1371e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1372e342ac08SSubhransu S. Prusty 
1373e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
137418382eadSSubhransu S. Prusty 	return 0;
137518382eadSSubhransu S. Prusty }
137618382eadSSubhransu S. Prusty 
137718382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
137818382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1379e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
138018382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
138118382eadSSubhransu S. Prusty };
138218382eadSSubhransu S. Prusty 
138318382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
138418382eadSSubhransu S. Prusty {
138518382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
138618382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
138717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
138817a42c45SSubhransu S. Prusty 	int num_dais = 0;
138918382eadSSubhransu S. Prusty 	int ret = 0;
139018382eadSSubhransu S. Prusty 
139118382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
139218382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
139318382eadSSubhransu S. Prusty 		return -ENOMEM;
139418382eadSSubhransu S. Prusty 
139518382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
139618382eadSSubhransu S. Prusty 
139718382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
139818382eadSSubhransu S. Prusty 
139915b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
140015b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
14014a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
14024a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
140315b91447SSubhransu S. Prusty 
140417a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
140517a42c45SSubhransu S. Prusty 	if (ret < 0) {
140617a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
140717a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
140818382eadSSubhransu S. Prusty 		return ret;
140917a42c45SSubhransu S. Prusty 	}
141018382eadSSubhransu S. Prusty 
141118382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
141218382eadSSubhransu S. Prusty 	return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
141317a42c45SSubhransu S. Prusty 			hdmi_dais, num_dais);
141418382eadSSubhransu S. Prusty }
141518382eadSSubhransu S. Prusty 
141618382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
141718382eadSSubhransu S. Prusty {
141815b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
141915b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
142015b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
14214a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
142215b91447SSubhransu S. Prusty 
142318382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
142418382eadSSubhransu S. Prusty 
14254a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
14264a3478deSJeeja KP 		pcm->cvt = NULL;
14274a3478deSJeeja KP 		pcm->pin = NULL;
14284a3478deSJeeja KP 		list_del(&pcm->head);
14294a3478deSJeeja KP 		kfree(pcm);
14304a3478deSJeeja KP 	}
14314a3478deSJeeja KP 
143215b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
143315b91447SSubhransu S. Prusty 		list_del(&cvt->head);
14344a3478deSJeeja KP 		kfree(cvt->name);
143515b91447SSubhransu S. Prusty 		kfree(cvt);
143615b91447SSubhransu S. Prusty 	}
143715b91447SSubhransu S. Prusty 
143815b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
143915b91447SSubhransu S. Prusty 		list_del(&pin->head);
144015b91447SSubhransu S. Prusty 		kfree(pin);
144115b91447SSubhransu S. Prusty 	}
144215b91447SSubhransu S. Prusty 
144318382eadSSubhransu S. Prusty 	return 0;
144418382eadSSubhransu S. Prusty }
144518382eadSSubhransu S. Prusty 
1446e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1447e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1448e342ac08SSubhransu S. Prusty {
1449e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1450e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
145107f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
145207f083abSSubhransu S. Prusty 	int err;
1453e342ac08SSubhransu S. Prusty 
1454e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1455e342ac08SSubhransu S. Prusty 
145607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
145707f083abSSubhransu S. Prusty 	if (!bus)
145807f083abSSubhransu S. Prusty 		return 0;
145907f083abSSubhransu S. Prusty 
1460e342ac08SSubhransu S. Prusty 	/* Power down afg */
1461e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1462e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1463e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1464e342ac08SSubhransu S. Prusty 
146507f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
146607f083abSSubhransu S. Prusty 	if (err < 0) {
146707f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
146807f083abSSubhransu S. Prusty 		return err;
146907f083abSSubhransu S. Prusty 	}
147007f083abSSubhransu S. Prusty 
1471e342ac08SSubhransu S. Prusty 	return 0;
1472e342ac08SSubhransu S. Prusty }
1473e342ac08SSubhransu S. Prusty 
1474e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1475e342ac08SSubhransu S. Prusty {
1476e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1477e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
147807f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
147907f083abSSubhransu S. Prusty 	int err;
1480e342ac08SSubhransu S. Prusty 
1481e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1482e342ac08SSubhransu S. Prusty 
148307f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
148407f083abSSubhransu S. Prusty 	if (!bus)
148507f083abSSubhransu S. Prusty 		return 0;
148607f083abSSubhransu S. Prusty 
148707f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
148807f083abSSubhransu S. Prusty 	if (err < 0) {
148907f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
149007f083abSSubhransu S. Prusty 		return err;
149107f083abSSubhransu S. Prusty 	}
149207f083abSSubhransu S. Prusty 
1493e342ac08SSubhransu S. Prusty 	/* Power up afg */
1494e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1495e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1496e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1497e342ac08SSubhransu S. Prusty 
1498e342ac08SSubhransu S. Prusty 	return 0;
1499e342ac08SSubhransu S. Prusty }
1500e342ac08SSubhransu S. Prusty #else
1501e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1502e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1503e342ac08SSubhransu S. Prusty #endif
1504e342ac08SSubhransu S. Prusty 
1505e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1506e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1507e342ac08SSubhransu S. Prusty };
1508e342ac08SSubhransu S. Prusty 
150918382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
151018382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
151118382eadSSubhransu S. Prusty 	{}
151218382eadSSubhransu S. Prusty };
151318382eadSSubhransu S. Prusty 
151418382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
151518382eadSSubhransu S. Prusty 
151618382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
151718382eadSSubhransu S. Prusty 	. hdac = {
151818382eadSSubhransu S. Prusty 		.driver = {
151918382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1520e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
152118382eadSSubhransu S. Prusty 		},
152218382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
152318382eadSSubhransu S. Prusty 	},
152418382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
152518382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
152618382eadSSubhransu S. Prusty };
152718382eadSSubhransu S. Prusty 
152818382eadSSubhransu S. Prusty static int __init hdmi_init(void)
152918382eadSSubhransu S. Prusty {
153018382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
153118382eadSSubhransu S. Prusty }
153218382eadSSubhransu S. Prusty 
153318382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
153418382eadSSubhransu S. Prusty {
153518382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
153618382eadSSubhransu S. Prusty }
153718382eadSSubhransu S. Prusty 
153818382eadSSubhransu S. Prusty module_init(hdmi_init);
153918382eadSSubhransu S. Prusty module_exit(hdmi_exit);
154018382eadSSubhransu S. Prusty 
154118382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
154218382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
154318382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
154418382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1545