xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision bcced704)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
3318382eadSSubhransu S. Prusty #include "../../hda/local.h"
344a3478deSJeeja KP #include "hdac_hdmi.h"
3518382eadSSubhransu S. Prusty 
3617a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3717a42c45SSubhransu S. Prusty 
38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
4018382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
41b0362adbSSubhransu S. Prusty 
4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4318382eadSSubhransu S. Prusty 
44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
45148569fdSSubhransu S. Prusty 
46b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
47b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
48b8a54545SSubhransu S. Prusty 
4918382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5018382eadSSubhransu S. Prusty 	unsigned int channels_min;
5118382eadSSubhransu S. Prusty 	unsigned int channels_max;
5218382eadSSubhransu S. Prusty 	u32 rates;
5318382eadSSubhransu S. Prusty 	u64 formats;
5418382eadSSubhransu S. Prusty 	unsigned int maxbps;
5518382eadSSubhransu S. Prusty };
5618382eadSSubhransu S. Prusty 
5718382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5815b91447SSubhransu S. Prusty 	struct list_head head;
5918382eadSSubhransu S. Prusty 	hda_nid_t nid;
604a3478deSJeeja KP 	const char *name;
6118382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6218382eadSSubhransu S. Prusty };
6318382eadSSubhransu S. Prusty 
64b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
65b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
66b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
67b7756edeSSubhransu S. Prusty };
68b7756edeSSubhransu S. Prusty 
69b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
70b8a54545SSubhransu S. Prusty 	bool	monitor_present;
71b8a54545SSubhransu S. Prusty 	bool	eld_valid;
72b8a54545SSubhransu S. Prusty 	int	eld_size;
73b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
74b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
75b8a54545SSubhransu S. Prusty };
76b8a54545SSubhransu S. Prusty 
7718382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7815b91447SSubhransu S. Prusty 	struct list_head head;
7918382eadSSubhransu S. Prusty 	hda_nid_t nid;
8018382eadSSubhransu S. Prusty 	int num_mux_nids;
8118382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
82b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
83b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev;
84b8a54545SSubhransu S. Prusty 	int repoll_count;
85b8a54545SSubhransu S. Prusty 	struct delayed_work work;
86bcced704SSubhransu S. Prusty 	struct mutex lock;
87bcced704SSubhransu S. Prusty 	bool chmap_set;
88bcced704SSubhransu S. Prusty 	unsigned char chmap[8]; /* ALSA API channel-map */
89bcced704SSubhransu S. Prusty 	int channels; /* current number of channels */
9018382eadSSubhransu S. Prusty };
9118382eadSSubhransu S. Prusty 
924a3478deSJeeja KP struct hdac_hdmi_pcm {
934a3478deSJeeja KP 	struct list_head head;
944a3478deSJeeja KP 	int pcm_id;
954a3478deSJeeja KP 	struct hdac_hdmi_pin *pin;
964a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
974a3478deSJeeja KP 	struct snd_jack *jack;
984a3478deSJeeja KP };
994a3478deSJeeja KP 
10018382eadSSubhransu S. Prusty struct hdac_hdmi_dai_pin_map {
10118382eadSSubhransu S. Prusty 	int dai_id;
10215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
10315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10418382eadSSubhransu S. Prusty };
10518382eadSSubhransu S. Prusty 
10618382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
107148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
10815b91447SSubhransu S. Prusty 	struct list_head pin_list;
10915b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1104a3478deSJeeja KP 	struct list_head pcm_list;
11115b91447SSubhransu S. Prusty 	int num_pin;
11215b91447SSubhransu S. Prusty 	int num_cvt;
1134a3478deSJeeja KP 	struct mutex pin_mutex;
114bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
11518382eadSSubhransu S. Prusty };
11618382eadSSubhransu S. Prusty 
117e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
118e342ac08SSubhransu S. Prusty {
11951b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
120e342ac08SSubhransu S. Prusty 
12151b2c425SGeliang Tang 	return to_ehdac_device(hdac);
122e342ac08SSubhransu S. Prusty }
123e342ac08SSubhransu S. Prusty 
1242428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1252428bca3SSubhransu S. Prusty {
1262428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1272428bca3SSubhransu S. Prusty }
1282428bca3SSubhransu S. Prusty 
1292428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1302428bca3SSubhransu S. Prusty {
1312428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1322428bca3SSubhransu S. Prusty }
1332428bca3SSubhransu S. Prusty 
1342428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1352428bca3SSubhransu S. Prusty 						void *eld)
1362428bca3SSubhransu S. Prusty {
1372428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1382428bca3SSubhransu S. Prusty 	int i;
1392428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1402428bca3SSubhransu S. Prusty 
1412428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1422428bca3SSubhransu S. Prusty 	if (!sad)
1432428bca3SSubhransu S. Prusty 		goto format_constraint;
1442428bca3SSubhransu S. Prusty 
1452428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1462428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1472428bca3SSubhransu S. Prusty 
1482428bca3SSubhransu S. Prusty 			/*
1492428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1502428bca3SSubhransu S. Prusty 			 * container so we set S32
1512428bca3SSubhransu S. Prusty 			 */
1522428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1532428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1542428bca3SSubhransu S. Prusty 		}
1552428bca3SSubhransu S. Prusty 	}
1562428bca3SSubhransu S. Prusty 
1572428bca3SSubhransu S. Prusty format_constraint:
1582428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1592428bca3SSubhransu S. Prusty 				formats);
1602428bca3SSubhransu S. Prusty 
1612428bca3SSubhransu S. Prusty }
1622428bca3SSubhransu S. Prusty 
163b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
164b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
165b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
166b8a54545SSubhransu S. Prusty {
167b8a54545SSubhransu S. Prusty 	unsigned int val;
168b8a54545SSubhransu S. Prusty 
169b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
170b8a54545SSubhransu S. Prusty 							byte_index);
171b8a54545SSubhransu S. Prusty 
172b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
173b8a54545SSubhransu S. Prusty 					byte_index, val);
174b8a54545SSubhransu S. Prusty 
175b8a54545SSubhransu S. Prusty 	return val;
176b8a54545SSubhransu S. Prusty }
177b8a54545SSubhransu S. Prusty 
178b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
179b8a54545SSubhransu S. Prusty {
180b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
181b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
182b8a54545SSubhransu S. Prusty }
183b8a54545SSubhransu S. Prusty 
184b8a54545SSubhransu S. Prusty /*
185b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
186b8a54545SSubhransu S. Prusty  * passed by user
187b8a54545SSubhransu S. Prusty  */
188b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
189b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
190b8a54545SSubhransu S. Prusty {
191b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
192b8a54545SSubhransu S. Prusty 
193b8a54545SSubhransu S. Prusty 	/*
194b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
195b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
196b8a54545SSubhransu S. Prusty 	 */
197b8a54545SSubhransu S. Prusty 
198b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
199b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
200b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
201b8a54545SSubhransu S. Prusty 		return -ERANGE;
202b8a54545SSubhransu S. Prusty 	}
203b8a54545SSubhransu S. Prusty 
204b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
205b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
206b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
207b8a54545SSubhransu S. Prusty 		/*
208b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
209b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
210b8a54545SSubhransu S. Prusty 		 */
211b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
212b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
213b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
214b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
215b8a54545SSubhransu S. Prusty 			goto error;
216b8a54545SSubhransu S. Prusty 		}
217b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
218b8a54545SSubhransu S. Prusty 		/*
219b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
220b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
221b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
222b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
223b8a54545SSubhransu S. Prusty 		 */
224b8a54545SSubhransu S. Prusty 		if (!val && !i) {
225b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
226b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
227b8a54545SSubhransu S. Prusty 			goto error;
228b8a54545SSubhransu S. Prusty 		}
229b8a54545SSubhransu S. Prusty 		buf[i] = val;
230b8a54545SSubhransu S. Prusty 	}
231b8a54545SSubhransu S. Prusty 
232b8a54545SSubhransu S. Prusty 	*eld_size = size;
233b8a54545SSubhransu S. Prusty error:
234b8a54545SSubhransu S. Prusty 	return ret;
235b8a54545SSubhransu S. Prusty }
236b8a54545SSubhransu S. Prusty 
237b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
238b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
239b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
240b0362adbSSubhransu S. Prusty {
241b0362adbSSubhransu S. Prusty 	unsigned int val;
242b0362adbSSubhransu S. Prusty 
243b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
244b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
245b0362adbSSubhransu S. Prusty 
246b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
247b0362adbSSubhransu S. Prusty 
248b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
249b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
250b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
251b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
252b0362adbSSubhransu S. Prusty 
253b0362adbSSubhransu S. Prusty 	return 0;
254b0362adbSSubhransu S. Prusty }
255b0362adbSSubhransu S. Prusty 
256a657f1d0SSubhransu S. Prusty static void
257a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
258a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
259a657f1d0SSubhransu S. Prusty {
260a657f1d0SSubhransu S. Prusty 	int val;
261a657f1d0SSubhransu S. Prusty 
262a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
263a657f1d0SSubhransu S. Prusty 
264a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
265a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
266a657f1d0SSubhransu S. Prusty }
267a657f1d0SSubhransu S. Prusty 
268478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
269478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
270478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
271478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
272478f544eSSubhransu S. Prusty 
273478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
274478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
275478f544eSSubhransu S. Prusty 	u8 CXT04;
276478f544eSSubhransu S. Prusty 	u8 CA;
277478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
278478f544eSSubhransu S. Prusty };
279478f544eSSubhransu S. Prusty 
280a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
281a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
282a657f1d0SSubhransu S. Prusty {
283a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
284a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
285478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
286478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
287478f544eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
288478f544eSSubhransu S. Prusty 	u8 *dip;
289a657f1d0SSubhransu S. Prusty 	int ret;
290a657f1d0SSubhransu S. Prusty 	int i;
291478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
292478f544eSSubhransu S. Prusty 	u8 conn_type;
293bcced704SSubhransu S. Prusty 	int channels, ca;
294a657f1d0SSubhransu S. Prusty 
295478f544eSSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
296478f544eSSubhransu S. Prusty 		if (pin->nid == pin_nid)
297478f544eSSubhransu S. Prusty 			break;
298478f544eSSubhransu S. Prusty 	}
299a657f1d0SSubhransu S. Prusty 
300bcced704SSubhransu S. Prusty 	ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
301bcced704SSubhransu S. Prusty 			pin->channels, pin->chmap_set, true, pin->chmap);
302bcced704SSubhransu S. Prusty 
303bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
304bcced704SSubhransu S. Prusty 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
305bcced704SSubhransu S. Prusty 
306bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
307bcced704SSubhransu S. Prusty 				pin->channels, pin->chmap, pin->chmap_set);
308bcced704SSubhransu S. Prusty 
309478f544eSSubhransu S. Prusty 	eld_buf = pin->eld.eld_buffer;
310478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
311a657f1d0SSubhransu S. Prusty 
312478f544eSSubhransu S. Prusty 	switch (conn_type) {
313478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
314478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
315478f544eSSubhransu S. Prusty 
316478f544eSSubhransu S. Prusty 		frame.channels = channels;
317bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
318a657f1d0SSubhransu S. Prusty 
319a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
320a657f1d0SSubhransu S. Prusty 		if (ret < 0)
321a657f1d0SSubhransu S. Prusty 			return ret;
322a657f1d0SSubhransu S. Prusty 
323478f544eSSubhransu S. Prusty 		break;
324478f544eSSubhransu S. Prusty 
325478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
326478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
327478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
328478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
329478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
330478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
331bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
332478f544eSSubhransu S. Prusty 
333478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
334478f544eSSubhransu S. Prusty 		break;
335478f544eSSubhransu S. Prusty 
336478f544eSSubhransu S. Prusty 	default:
337478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
338478f544eSSubhransu S. Prusty 						conn_type);
339478f544eSSubhransu S. Prusty 		return -EIO;
340478f544eSSubhransu S. Prusty 	}
341478f544eSSubhransu S. Prusty 
342a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
343a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
344a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
345a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
346a657f1d0SSubhransu S. Prusty 
347a657f1d0SSubhransu S. Prusty 
348a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
349a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
350478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
351391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
352a657f1d0SSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
353391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
354478f544eSSubhransu S. Prusty 	} else {
355478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
356478f544eSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
357478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
358478f544eSSubhransu S. Prusty 	}
359a657f1d0SSubhransu S. Prusty 
360a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
361a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
362a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
363a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
364a657f1d0SSubhransu S. Prusty 
365a657f1d0SSubhransu S. Prusty 	return 0;
366a657f1d0SSubhransu S. Prusty }
367a657f1d0SSubhransu S. Prusty 
368b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
369b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
370b0362adbSSubhransu S. Prusty {
371b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
37215b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
37315b91447SSubhransu S. Prusty 						pwr_state))
37415b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
375b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
376b0362adbSSubhransu S. Prusty 
377b0362adbSSubhransu S. Prusty 	/* Power up converter */
37815b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
37915b91447SSubhransu S. Prusty 						pwr_state))
38015b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
381b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
382b0362adbSSubhransu S. Prusty }
383b0362adbSSubhransu S. Prusty 
384b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
385b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
386b0362adbSSubhransu S. Prusty {
387b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
388b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
389b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
390bcced704SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
391b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
392a657f1d0SSubhransu S. Prusty 	int ret;
393b0362adbSSubhransu S. Prusty 
394b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
395bcced704SSubhransu S. Prusty 	pin = dai_map->pin;
396b0362adbSSubhransu S. Prusty 
397b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
398b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
399b0362adbSSubhransu S. Prusty 			dd->stream_tag,	dd->format);
400b0362adbSSubhransu S. Prusty 
401bcced704SSubhransu S. Prusty 	mutex_lock(&pin->lock);
402bcced704SSubhransu S. Prusty 	pin->channels = substream->runtime->channels;
403bcced704SSubhransu S. Prusty 
40415b91447SSubhransu S. Prusty 	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
40515b91447SSubhransu S. Prusty 						dai_map->pin->nid);
406bcced704SSubhransu S. Prusty 	mutex_unlock(&pin->lock);
407a657f1d0SSubhransu S. Prusty 	if (ret < 0)
408a657f1d0SSubhransu S. Prusty 		return ret;
409a657f1d0SSubhransu S. Prusty 
41015b91447SSubhransu S. Prusty 	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
41115b91447SSubhransu S. Prusty 			dai_map->pin->nid, dd->stream_tag, dd->format);
412b0362adbSSubhransu S. Prusty }
413b0362adbSSubhransu S. Prusty 
414b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
415b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
416b0362adbSSubhransu S. Prusty {
417b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
41854dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
41954dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
42054dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
421b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
422b0362adbSSubhransu S. Prusty 
42354dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
42454dfa1eaSSubhransu S. Prusty 	pin = dai_map->pin;
42554dfa1eaSSubhransu S. Prusty 
42654dfa1eaSSubhransu S. Prusty 	if (!pin)
42754dfa1eaSSubhransu S. Prusty 		return -ENODEV;
42854dfa1eaSSubhransu S. Prusty 
42954dfa1eaSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
43054dfa1eaSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
43154dfa1eaSSubhransu S. Prusty 								pin->nid);
432b0362adbSSubhransu S. Prusty 		return -ENODEV;
433b0362adbSSubhransu S. Prusty 	}
434b0362adbSSubhransu S. Prusty 
4356793a3d7SSubhransu S. Prusty 	dd = snd_soc_dai_get_dma_data(dai, substream);
4366793a3d7SSubhransu S. Prusty 	if (!dd) {
437b0362adbSSubhransu S. Prusty 		dd = kzalloc(sizeof(*dd), GFP_KERNEL);
4388d33ab24SSudip Mukherjee 		if (!dd)
4398d33ab24SSudip Mukherjee 			return -ENOMEM;
4406793a3d7SSubhransu S. Prusty 	}
4416793a3d7SSubhransu S. Prusty 
442b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
443b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
444b0362adbSSubhransu S. Prusty 			24, 0);
445b0362adbSSubhransu S. Prusty 
446b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
447b0362adbSSubhransu S. Prusty 
448b0362adbSSubhransu S. Prusty 	return 0;
449b0362adbSSubhransu S. Prusty }
450b0362adbSSubhransu S. Prusty 
451b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
452b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
453b0362adbSSubhransu S. Prusty {
454b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
455b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
456b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
457b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
458b0362adbSSubhransu S. Prusty 
459b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
460b0362adbSSubhransu S. Prusty 
461b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
462b0362adbSSubhransu S. Prusty 
4636793a3d7SSubhransu S. Prusty 	if (dd) {
4646793a3d7SSubhransu S. Prusty 		snd_soc_dai_set_dma_data(dai, substream, NULL);
465b0362adbSSubhransu S. Prusty 		kfree(dd);
4666793a3d7SSubhransu S. Prusty 	}
467b0362adbSSubhransu S. Prusty 
468b0362adbSSubhransu S. Prusty 	return 0;
469b0362adbSSubhransu S. Prusty }
470b0362adbSSubhransu S. Prusty 
471ab85f5b3SSubhransu S. Prusty static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
472ab85f5b3SSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
473ab85f5b3SSubhransu S. Prusty {
474ab85f5b3SSubhransu S. Prusty 	/* Enable transmission */
475ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
476ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_1, 1);
477ab85f5b3SSubhransu S. Prusty 
478ab85f5b3SSubhransu S. Prusty 	/* Category Code (CC) to zero */
479ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
480ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_2, 0);
481ab85f5b3SSubhransu S. Prusty }
482ab85f5b3SSubhransu S. Prusty 
483148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
484148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
485148569fdSSubhransu S. Prusty {
486148569fdSSubhransu S. Prusty 	int mux_idx;
487148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
488148569fdSSubhransu S. Prusty 
489148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
490148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
491148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
492148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
493148569fdSSubhransu S. Prusty 			break;
494148569fdSSubhransu S. Prusty 		}
495148569fdSSubhransu S. Prusty 	}
496148569fdSSubhransu S. Prusty 
497148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
498148569fdSSubhransu S. Prusty 		return -EIO;
499148569fdSSubhransu S. Prusty 
500148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
501148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
502148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
503148569fdSSubhransu S. Prusty 
504148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
505148569fdSSubhransu S. Prusty 
506148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
507148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
508148569fdSSubhransu S. Prusty 
509148569fdSSubhransu S. Prusty 	return 0;
510148569fdSSubhransu S. Prusty }
511148569fdSSubhransu S. Prusty 
512148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
513148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
514148569fdSSubhransu S. Prusty {
515148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
516148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
517148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
518148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
519148569fdSSubhransu S. Prusty 		return -EINVAL;
520148569fdSSubhransu S. Prusty 	}
521148569fdSSubhransu S. Prusty 
522148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
523148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
524148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
525148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
526148569fdSSubhransu S. Prusty 								pin->nid);
527148569fdSSubhransu S. Prusty 
528148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
529148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
530148569fdSSubhransu S. Prusty 
531148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
532148569fdSSubhransu S. Prusty }
533148569fdSSubhransu S. Prusty 
534148569fdSSubhransu S. Prusty /*
535148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
536148569fdSSubhransu S. Prusty  *
537148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
538148569fdSSubhransu S. Prusty  *
539148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
540148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
541148569fdSSubhransu S. Prusty  * connected.
542148569fdSSubhransu S. Prusty  */
543148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
544148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
545148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
546148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
547148569fdSSubhransu S. Prusty {
548148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
549148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
550148569fdSSubhransu S. Prusty 	int ret, i;
551148569fdSSubhransu S. Prusty 
552148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
553148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
554148569fdSSubhransu S. Prusty 			pin = pcm->pin;
555148569fdSSubhransu S. Prusty 			break;
556148569fdSSubhransu S. Prusty 		}
557148569fdSSubhransu S. Prusty 	}
558148569fdSSubhransu S. Prusty 
559148569fdSSubhransu S. Prusty 	if (pin) {
560148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
561148569fdSSubhransu S. Prusty 		if (ret < 0)
562148569fdSSubhransu S. Prusty 			return NULL;
563148569fdSSubhransu S. Prusty 
564148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
565148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
566148569fdSSubhransu S. Prusty 				return pin;
567148569fdSSubhransu S. Prusty 		}
568148569fdSSubhransu S. Prusty 	}
569148569fdSSubhransu S. Prusty 
570148569fdSSubhransu S. Prusty 	return NULL;
571148569fdSSubhransu S. Prusty }
572148569fdSSubhransu S. Prusty 
57354dfa1eaSSubhransu S. Prusty /*
57454dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
57554dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
57654dfa1eaSSubhransu S. Prusty  * doesn't fail.
57754dfa1eaSSubhransu S. Prusty  */
578b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
579b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
580b0362adbSSubhransu S. Prusty {
581b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
582b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
583b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
584148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
585148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
5862428bca3SSubhransu S. Prusty 	int ret;
587b0362adbSSubhransu S. Prusty 
588b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
589b0362adbSSubhransu S. Prusty 
590148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
591148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
59254dfa1eaSSubhransu S. Prusty 
59354dfa1eaSSubhransu S. Prusty 	/*
59454dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
59554dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
59654dfa1eaSSubhransu S. Prusty 	 */
597148569fdSSubhransu S. Prusty 	if (!pin)
59854dfa1eaSSubhransu S. Prusty 		return 0;
599148569fdSSubhransu S. Prusty 
600148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
601148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
602b0362adbSSubhransu S. Prusty 
60354dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
604148569fdSSubhransu S. Prusty 			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
605148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
606b8a54545SSubhransu S. Prusty 
60754dfa1eaSSubhransu S. Prusty 		return 0;
608b0362adbSSubhransu S. Prusty 	}
609b0362adbSSubhransu S. Prusty 
610148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
611b0362adbSSubhransu S. Prusty 
612ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_enable_cvt(hdac, dai_map);
613148569fdSSubhransu S. Prusty 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
614148569fdSSubhransu S. Prusty 	if (ret < 0)
615148569fdSSubhransu S. Prusty 		return ret;
616b0362adbSSubhransu S. Prusty 
6172428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
618148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
6192428bca3SSubhransu S. Prusty 	if (ret < 0)
6202428bca3SSubhransu S. Prusty 		return ret;
621b0362adbSSubhransu S. Prusty 
6222428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
623148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
624b0362adbSSubhransu S. Prusty }
625b0362adbSSubhransu S. Prusty 
626571d5078SJeeja KP static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
627571d5078SJeeja KP 		struct snd_soc_dai *dai)
628571d5078SJeeja KP {
629571d5078SJeeja KP 	struct hdac_hdmi_dai_pin_map *dai_map;
630571d5078SJeeja KP 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
631571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
632571d5078SJeeja KP 	int ret;
633571d5078SJeeja KP 
634571d5078SJeeja KP 	dai_map = &hdmi->dai_map[dai->id];
635571d5078SJeeja KP 	if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
636571d5078SJeeja KP 		ret = hdac_hdmi_enable_pin(hdac, dai_map);
637571d5078SJeeja KP 		if (ret < 0)
638571d5078SJeeja KP 			return ret;
639571d5078SJeeja KP 
640571d5078SJeeja KP 		return hdac_hdmi_playback_prepare(substream, dai);
641571d5078SJeeja KP 	}
642571d5078SJeeja KP 
643571d5078SJeeja KP 	return 0;
644571d5078SJeeja KP }
645571d5078SJeeja KP 
646b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
647b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
648b0362adbSSubhransu S. Prusty {
649b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
650b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
651b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
652b0362adbSSubhransu S. Prusty 
653b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
654b0362adbSSubhransu S. Prusty 
65554dfa1eaSSubhransu S. Prusty 	if (dai_map->pin) {
65654dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
65754dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
65854dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
65954dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
66054dfa1eaSSubhransu S. Prusty 
661b0362adbSSubhransu S. Prusty 		hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
662b0362adbSSubhransu S. Prusty 
66315b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
664b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
665148569fdSSubhransu S. Prusty 
666bcced704SSubhransu S. Prusty 		mutex_lock(&dai_map->pin->lock);
667bcced704SSubhransu S. Prusty 		dai_map->pin->channels = 0;
668bcced704SSubhransu S. Prusty 		mutex_unlock(&dai_map->pin->lock);
669bcced704SSubhransu S. Prusty 
670148569fdSSubhransu S. Prusty 		dai_map->pin = NULL;
671b0362adbSSubhransu S. Prusty 	}
67254dfa1eaSSubhransu S. Prusty }
673b0362adbSSubhransu S. Prusty 
67418382eadSSubhransu S. Prusty static int
67518382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
67618382eadSSubhransu S. Prusty {
677bcced704SSubhransu S. Prusty 	unsigned int chans;
678bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
679bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
68018382eadSSubhransu S. Prusty 	int err;
68118382eadSSubhransu S. Prusty 
682bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
683bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
684bcced704SSubhransu S. Prusty 
685bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
686bcced704SSubhransu S. Prusty 
687bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
688bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
689bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
69018382eadSSubhransu S. Prusty 
69118382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
69218382eadSSubhransu S. Prusty 			&cvt->params.rates,
69318382eadSSubhransu S. Prusty 			&cvt->params.formats,
69418382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
69518382eadSSubhransu S. Prusty 	if (err < 0)
69618382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
69718382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
69818382eadSSubhransu S. Prusty 			cvt->nid, err);
69918382eadSSubhransu S. Prusty 
70018382eadSSubhransu S. Prusty 	return err;
70118382eadSSubhransu S. Prusty }
70218382eadSSubhransu S. Prusty 
70379f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
70479f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
70579f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
70679f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
70779f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
70818382eadSSubhransu S. Prusty {
70918382eadSSubhransu S. Prusty 	w->id = id;
71079f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
71179f4e922SSubhransu S. Prusty 	if (!w->name)
71279f4e922SSubhransu S. Prusty 		return -ENOMEM;
71379f4e922SSubhransu S. Prusty 
71418382eadSSubhransu S. Prusty 	w->sname = stream;
71518382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
71618382eadSSubhransu S. Prusty 	w->shift = 0;
71779f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
71879f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
71979f4e922SSubhransu S. Prusty 	w->priv = priv;
72079f4e922SSubhransu S. Prusty 
72179f4e922SSubhransu S. Prusty 	return 0;
72218382eadSSubhransu S. Prusty }
72318382eadSSubhransu S. Prusty 
72418382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
72579f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
72679f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
72779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
72818382eadSSubhransu S. Prusty {
72918382eadSSubhransu S. Prusty 	route->sink = sink;
73018382eadSSubhransu S. Prusty 	route->source = src;
73118382eadSSubhransu S. Prusty 	route->control = control;
73279f4e922SSubhransu S. Prusty 	route->connected = handler;
73318382eadSSubhransu S. Prusty }
73418382eadSSubhransu S. Prusty 
7354a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
7364a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
7374a3478deSJeeja KP {
7384a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7394a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7404a3478deSJeeja KP 
7414a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7424a3478deSJeeja KP 		if (pcm->pin == pin)
7434a3478deSJeeja KP 			return pcm;
7444a3478deSJeeja KP 	}
7454a3478deSJeeja KP 
7464a3478deSJeeja KP 	return NULL;
7474a3478deSJeeja KP }
7484a3478deSJeeja KP 
7494a3478deSJeeja KP /*
7504a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7514a3478deSJeeja KP  */
7524a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
7534a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7544a3478deSJeeja KP {
7554a3478deSJeeja KP 	int ret;
7564a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7574a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7584a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
7594a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
7604a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7614a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7624a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7634a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7644a3478deSJeeja KP 
7654a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7664a3478deSJeeja KP 	if (ret < 0)
7674a3478deSJeeja KP 		return ret;
7684a3478deSJeeja KP 
7694a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7704a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7714a3478deSJeeja KP 		if (pcm->pin == pin)
7724a3478deSJeeja KP 			pcm->pin = NULL;
7734a3478deSJeeja KP 
7744a3478deSJeeja KP 		/*
7754a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7764a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7774a3478deSJeeja KP 		 */
7784a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
7794a3478deSJeeja KP 			pcm->pin = pin;
7804a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
7814a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7824a3478deSJeeja KP 					"jack report for pcm=%d\n",
7834a3478deSJeeja KP 					pcm->pcm_id);
7844a3478deSJeeja KP 
7854a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
7864a3478deSJeeja KP 			}
7874a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
7884a3478deSJeeja KP 			return ret;
7894a3478deSJeeja KP 		}
7904a3478deSJeeja KP 	}
7914a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
7924a3478deSJeeja KP 
7934a3478deSJeeja KP 	return ret;
7944a3478deSJeeja KP }
7954a3478deSJeeja KP 
79679f4e922SSubhransu S. Prusty /*
79779f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
79879f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
79979f4e922SSubhransu S. Prusty  * widget runtime.
80079f4e922SSubhransu S. Prusty  *
80179f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
80279f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
80379f4e922SSubhransu S. Prusty  * "NONE"
80479f4e922SSubhransu S. Prusty  */
80579f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
80679f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
80779f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
80879f4e922SSubhransu S. Prusty 				const char *widget_name)
80918382eadSSubhransu S. Prusty {
81079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
81179f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
81279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
81379f4e922SSubhransu S. Prusty 	struct soc_enum *se;
81479f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
81579f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
81679f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
81779f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
81879f4e922SSubhransu S. Prusty 	int i = 0;
81979f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
82018382eadSSubhransu S. Prusty 
82179f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
82279f4e922SSubhransu S. Prusty 	if (!kc)
82379f4e922SSubhransu S. Prusty 		return -ENOMEM;
82418382eadSSubhransu S. Prusty 
82579f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
82679f4e922SSubhransu S. Prusty 	if (!se)
82779f4e922SSubhransu S. Prusty 		return -ENOMEM;
82818382eadSSubhransu S. Prusty 
82979f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
83079f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
83179f4e922SSubhransu S. Prusty 	if (!kc->name)
83279f4e922SSubhransu S. Prusty 		return -ENOMEM;
83318382eadSSubhransu S. Prusty 
83479f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
83579f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
83679f4e922SSubhransu S. Prusty 	kc->access = 0;
83779f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
8384a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
83979f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
84079f4e922SSubhransu S. Prusty 
84179f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
84279f4e922SSubhransu S. Prusty 
84379f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
84479f4e922SSubhransu S. Prusty 	se->items = num_items;
84579f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
84679f4e922SSubhransu S. Prusty 
84779f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
84879f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
84979f4e922SSubhransu S. Prusty 	if (!items[i])
85079f4e922SSubhransu S. Prusty 		return -ENOMEM;
85179f4e922SSubhransu S. Prusty 
85279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
85379f4e922SSubhransu S. Prusty 		i++;
85479f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
85579f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
85679f4e922SSubhransu S. Prusty 		if (!items[i])
85779f4e922SSubhransu S. Prusty 			return -ENOMEM;
85879f4e922SSubhransu S. Prusty 	}
85979f4e922SSubhransu S. Prusty 
86079f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
86179f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
86279f4e922SSubhransu S. Prusty 	if (!se->texts)
86379f4e922SSubhransu S. Prusty 		return -ENOMEM;
86479f4e922SSubhransu S. Prusty 
86579f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
8664a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
86779f4e922SSubhransu S. Prusty }
86879f4e922SSubhransu S. Prusty 
86979f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
87079f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
87179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
87279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
87379f4e922SSubhransu S. Prusty {
87479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
87579f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
87679f4e922SSubhransu S. Prusty 	struct soc_enum *se;
87779f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
87879f4e922SSubhransu S. Prusty 	int i, j;
87979f4e922SSubhransu S. Prusty 
88079f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
88179f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
88279f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
88379f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
88479f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
88579f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
88679f4e922SSubhransu S. Prusty 					se->texts[j + 1],
88779f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
88879f4e922SSubhransu S. Prusty 
88979f4e922SSubhransu S. Prusty 			rindex++;
89079f4e922SSubhransu S. Prusty 		}
89179f4e922SSubhransu S. Prusty 
89279f4e922SSubhransu S. Prusty 		mux_index++;
89379f4e922SSubhransu S. Prusty 	}
89479f4e922SSubhransu S. Prusty }
89579f4e922SSubhransu S. Prusty 
89679f4e922SSubhransu S. Prusty /*
89779f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
89879f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
89979f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
90079f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
90179f4e922SSubhransu S. Prusty  *
90279f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
90379f4e922SSubhransu S. Prusty  *
90479f4e922SSubhransu S. Prusty  * Routes are added as below:
90579f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
90679f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
90779f4e922SSubhransu S. Prusty  *
90879f4e922SSubhransu S. Prusty  * Total route elements:
90979f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
91079f4e922SSubhransu S. Prusty  */
91179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
91279f4e922SSubhransu S. Prusty {
91379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
91479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
91579f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
91679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
91779f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
91879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
91979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
92079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
92179f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
92279f4e922SSubhransu S. Prusty 
92379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
92479f4e922SSubhransu S. Prusty 		return -EINVAL;
92579f4e922SSubhransu S. Prusty 
92679f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
92779f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
92879f4e922SSubhransu S. Prusty 		GFP_KERNEL);
92979f4e922SSubhransu S. Prusty 
93079f4e922SSubhransu S. Prusty 	if (!widgets)
93179f4e922SSubhransu S. Prusty 		return -ENOMEM;
93279f4e922SSubhransu S. Prusty 
93379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
93479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
93579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
93679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
93779f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
93879f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
93979f4e922SSubhransu S. Prusty 		if (ret < 0)
94079f4e922SSubhransu S. Prusty 			return ret;
94179f4e922SSubhransu S. Prusty 		i++;
94279f4e922SSubhransu S. Prusty 	}
94379f4e922SSubhransu S. Prusty 
94479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
94579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
94679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
94779f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
94879f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
94979f4e922SSubhransu S. Prusty 		if (ret < 0)
95079f4e922SSubhransu S. Prusty 			return ret;
95179f4e922SSubhransu S. Prusty 		i++;
95279f4e922SSubhransu S. Prusty 	}
95379f4e922SSubhransu S. Prusty 
95479f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
95579f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
95679f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
95779f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
95879f4e922SSubhransu S. Prusty 							widget_name);
95979f4e922SSubhransu S. Prusty 		if (ret < 0)
96079f4e922SSubhransu S. Prusty 			return ret;
96179f4e922SSubhransu S. Prusty 		i++;
96279f4e922SSubhransu S. Prusty 
96379f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
96479f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
96579f4e922SSubhransu S. Prusty 
96679f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
96779f4e922SSubhransu S. Prusty 		num_routes++;
96879f4e922SSubhransu S. Prusty 	}
96979f4e922SSubhransu S. Prusty 
97079f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
97179f4e922SSubhransu S. Prusty 							GFP_KERNEL);
97279f4e922SSubhransu S. Prusty 	if (!route)
97379f4e922SSubhransu S. Prusty 		return -ENOMEM;
97479f4e922SSubhransu S. Prusty 
97579f4e922SSubhransu S. Prusty 	i = 0;
97679f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
97779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
97879f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
97979f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
98079f4e922SSubhransu S. Prusty 
98179f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
98279f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
98379f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
98479f4e922SSubhransu S. Prusty 		i++;
98579f4e922SSubhransu S. Prusty 
98679f4e922SSubhransu S. Prusty 	}
98779f4e922SSubhransu S. Prusty 
98879f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
98979f4e922SSubhransu S. Prusty 
99079f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
99179f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
99279f4e922SSubhransu S. Prusty 
99379f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
99479f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
99579f4e922SSubhransu S. Prusty 
99679f4e922SSubhransu S. Prusty 	return 0;
99779f4e922SSubhransu S. Prusty 
99818382eadSSubhransu S. Prusty }
99918382eadSSubhransu S. Prusty 
100015b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
100118382eadSSubhransu S. Prusty {
100215b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1003148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
100415b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1005148569fdSSubhransu S. Prusty 	int dai_id = 0;
100618382eadSSubhransu S. Prusty 
1007148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
100815b91447SSubhransu S. Prusty 		return -EINVAL;
100918382eadSSubhransu S. Prusty 
1010148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1011148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1012148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
101315b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
101418382eadSSubhransu S. Prusty 
1015148569fdSSubhransu S. Prusty 		dai_id++;
1016148569fdSSubhransu S. Prusty 
1017148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1018148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1019148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1020148569fdSSubhransu S. Prusty 			break;
1021148569fdSSubhransu S. Prusty 		}
1022148569fdSSubhransu S. Prusty 	}
102318382eadSSubhransu S. Prusty 
102415b91447SSubhransu S. Prusty 	return 0;
102515b91447SSubhransu S. Prusty }
102615b91447SSubhransu S. Prusty 
102715b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
102815b91447SSubhransu S. Prusty {
102915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
103015b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10314a3478deSJeeja KP 	char name[NAME_SIZE];
103215b91447SSubhransu S. Prusty 
103315b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
103415b91447SSubhransu S. Prusty 	if (!cvt)
103515b91447SSubhransu S. Prusty 		return -ENOMEM;
103615b91447SSubhransu S. Prusty 
103715b91447SSubhransu S. Prusty 	cvt->nid = nid;
10384a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10394a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
104015b91447SSubhransu S. Prusty 
104115b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
104215b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
104315b91447SSubhransu S. Prusty 
104415b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
104515b91447SSubhransu S. Prusty }
104615b91447SSubhransu S. Prusty 
1047b7756edeSSubhransu S. Prusty static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1048b7756edeSSubhransu S. Prusty 			struct hdac_hdmi_pin *pin)
1049b7756edeSSubhransu S. Prusty {
1050b7756edeSSubhransu S. Prusty 	pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1051b7756edeSSubhransu S. Prusty }
1052b7756edeSSubhransu S. Prusty 
1053b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1054b8a54545SSubhransu S. Prusty {
1055b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10564a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10574a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1058b8a54545SSubhransu S. Prusty 	int val;
1059b8a54545SSubhransu S. Prusty 
1060b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
1061b8a54545SSubhransu S. Prusty 
1062b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
1063b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1064b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
1065b8a54545SSubhransu S. Prusty 
1066b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1067b8a54545SSubhransu S. Prusty 						val, pin->nid);
1068b8a54545SSubhransu S. Prusty 
10694a3478deSJeeja KP 
10704a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1071b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1072b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1073b8a54545SSubhransu S. Prusty 
10744a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
10754a3478deSJeeja KP 
1076b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1077b8a54545SSubhransu S. Prusty 
1078b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1079b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
10804a3478deSJeeja KP 
10814a3478deSJeeja KP 		/*
10824a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
10834a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
10844a3478deSJeeja KP 		 * control select.
10854a3478deSJeeja KP 		 */
10864a3478deSJeeja KP 		if (pcm) {
10874a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
10884a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
10894a3478deSJeeja KP 
10904a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
10914a3478deSJeeja KP 		}
10924a3478deSJeeja KP 
10934a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1094b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
1095b8a54545SSubhransu S. Prusty 	}
1096b8a54545SSubhransu S. Prusty 
1097b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
1098b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
1099b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1100b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
1101b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
1102b8a54545SSubhransu S. Prusty 
11034a3478deSJeeja KP 			if (pcm) {
11044a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11054a3478deSJeeja KP 					"jack report for pcm=%d\n",
11064a3478deSJeeja KP 					pcm->pcm_id);
11074a3478deSJeeja KP 
11084a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
11094a3478deSJeeja KP 			}
1110b7756edeSSubhransu S. Prusty 			hdac_hdmi_parse_eld(edev, pin);
11114a3478deSJeeja KP 
1112b8a54545SSubhransu S. Prusty 			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1113b8a54545SSubhransu S. Prusty 					pin->eld.eld_buffer, pin->eld.eld_size);
1114b8a54545SSubhransu S. Prusty 		} else {
1115b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
1116b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
11174a3478deSJeeja KP 
11184a3478deSJeeja KP 			if (pcm) {
11194a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11204a3478deSJeeja KP 					"jack report for pcm=%d\n",
11214a3478deSJeeja KP 					pcm->pcm_id);
11224a3478deSJeeja KP 
11234a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
1124b8a54545SSubhransu S. Prusty 			}
1125b8a54545SSubhransu S. Prusty 		}
11264a3478deSJeeja KP 	}
11274a3478deSJeeja KP 
11284a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
1129b8a54545SSubhransu S. Prusty 
1130b8a54545SSubhransu S. Prusty 	/*
1131b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
1132b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
1133b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
1134b8a54545SSubhransu S. Prusty 	 */
1135b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1136b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1137b8a54545SSubhransu S. Prusty 
1138b8a54545SSubhransu S. Prusty put_hdac_device:
1139b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1140b8a54545SSubhransu S. Prusty }
1141b8a54545SSubhransu S. Prusty 
1142b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1143b8a54545SSubhransu S. Prusty {
1144b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1145b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1146b8a54545SSubhransu S. Prusty 
1147b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1148b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1149b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1150b8a54545SSubhransu S. Prusty 
1151b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1152b8a54545SSubhransu S. Prusty }
1153b8a54545SSubhransu S. Prusty 
115415b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
115515b91447SSubhransu S. Prusty {
115615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
115715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
115815b91447SSubhransu S. Prusty 
115915b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
116015b91447SSubhransu S. Prusty 	if (!pin)
116115b91447SSubhransu S. Prusty 		return -ENOMEM;
116215b91447SSubhransu S. Prusty 
116315b91447SSubhransu S. Prusty 	pin->nid = nid;
116415b91447SSubhransu S. Prusty 
116515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
116615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
116715b91447SSubhransu S. Prusty 
1168b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1169bcced704SSubhransu S. Prusty 	mutex_init(&pin->lock);
1170b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1171b8a54545SSubhransu S. Prusty 
117215b91447SSubhransu S. Prusty 	return 0;
117318382eadSSubhransu S. Prusty }
117418382eadSSubhransu S. Prusty 
1175211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1176211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1177211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1178211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1179211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1180211caab7SSubhransu S. Prusty 
1181211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1182211caab7SSubhransu S. Prusty {
1183211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1184211caab7SSubhransu S. Prusty 
1185211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1186211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1187211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1188211caab7SSubhransu S. Prusty 		return;
1189211caab7SSubhransu S. Prusty 
1190211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1191211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1192211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1193211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1194211caab7SSubhransu S. Prusty 		return;
1195211caab7SSubhransu S. Prusty }
1196211caab7SSubhransu S. Prusty 
1197211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1198211caab7SSubhransu S. Prusty {
1199211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1200211caab7SSubhransu S. Prusty 
1201211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1202211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1203211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1204211caab7SSubhransu S. Prusty 		return;
1205211caab7SSubhransu S. Prusty 
1206211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1207211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1208211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1209211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1210211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1211211caab7SSubhransu S. Prusty 		return;
1212211caab7SSubhransu S. Prusty 
1213211caab7SSubhransu S. Prusty }
1214211caab7SSubhransu S. Prusty 
121517a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
121617a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
121717a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
121817a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
121917a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
1220571d5078SJeeja KP 	.trigger = hdac_hdmi_trigger,
122117a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
122217a42c45SSubhransu S. Prusty };
122317a42c45SSubhransu S. Prusty 
122417a42c45SSubhransu S. Prusty /*
122517a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
122617a42c45SSubhransu S. Prusty  * based on the number of converter queried.
122717a42c45SSubhransu S. Prusty  */
122817a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
122917a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
123017a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
123117a42c45SSubhransu S. Prusty {
123217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
123317a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
123417a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
123517a42c45SSubhransu S. Prusty 	int i = 0;
123617a42c45SSubhransu S. Prusty 	u32 rates, bps;
123717a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
123817a42c45SSubhransu S. Prusty 	u64 formats;
123917a42c45SSubhransu S. Prusty 	int ret;
124017a42c45SSubhransu S. Prusty 
124117a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
124217a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
124317a42c45SSubhransu S. Prusty 			GFP_KERNEL);
124417a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
124517a42c45SSubhransu S. Prusty 		return -ENOMEM;
124617a42c45SSubhransu S. Prusty 
124717a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
124817a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
124917a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
125017a42c45SSubhransu S. Prusty 		if (ret)
125117a42c45SSubhransu S. Prusty 			return ret;
125217a42c45SSubhransu S. Prusty 
125317a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
125417a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
125517a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
125617a42c45SSubhransu S. Prusty 
125717a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
125817a42c45SSubhransu S. Prusty 			return -ENOMEM;
125917a42c45SSubhransu S. Prusty 
126017a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
126117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
126217a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
126317a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
126417a42c45SSubhransu S. Prusty 			return -ENOMEM;
126517a42c45SSubhransu S. Prusty 
126617a42c45SSubhransu S. Prusty 		/*
126717a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
126817a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
126917a42c45SSubhransu S. Prusty 		 */
127017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
127117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
127217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
127317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
127417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
127517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
127617a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
127717a42c45SSubhransu S. Prusty 
127817a42c45SSubhransu S. Prusty 		i++;
127917a42c45SSubhransu S. Prusty 	}
128017a42c45SSubhransu S. Prusty 
128117a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
128217a42c45SSubhransu S. Prusty 
128317a42c45SSubhransu S. Prusty 	return 0;
128417a42c45SSubhransu S. Prusty }
128517a42c45SSubhransu S. Prusty 
128618382eadSSubhransu S. Prusty /*
128718382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
128818382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
128918382eadSSubhransu S. Prusty  */
129017a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
129117a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
129218382eadSSubhransu S. Prusty {
129318382eadSSubhransu S. Prusty 	hda_nid_t nid;
12943c83ac23SSudip Mukherjee 	int i, num_nodes;
129518382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
129618382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
129715b91447SSubhransu S. Prusty 	int ret;
129818382eadSSubhransu S. Prusty 
1299211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1300211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1301211caab7SSubhransu S. Prusty 
13023c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1303541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
130418382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
130518382eadSSubhransu S. Prusty 		return -EINVAL;
130618382eadSSubhransu S. Prusty 	}
130718382eadSSubhransu S. Prusty 
13083c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
130918382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
131018382eadSSubhransu S. Prusty 
131118382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
131218382eadSSubhransu S. Prusty 		unsigned int caps;
131318382eadSSubhransu S. Prusty 		unsigned int type;
131418382eadSSubhransu S. Prusty 
131518382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
131618382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
131718382eadSSubhransu S. Prusty 
131818382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
131918382eadSSubhransu S. Prusty 			continue;
132018382eadSSubhransu S. Prusty 
132118382eadSSubhransu S. Prusty 		switch (type) {
132218382eadSSubhransu S. Prusty 
132318382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
132415b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
132515b91447SSubhransu S. Prusty 			if (ret < 0)
132615b91447SSubhransu S. Prusty 				return ret;
132718382eadSSubhransu S. Prusty 			break;
132818382eadSSubhransu S. Prusty 
132918382eadSSubhransu S. Prusty 		case AC_WID_PIN:
133015b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
133115b91447SSubhransu S. Prusty 			if (ret < 0)
133215b91447SSubhransu S. Prusty 				return ret;
133318382eadSSubhransu S. Prusty 			break;
133418382eadSSubhransu S. Prusty 		}
133518382eadSSubhransu S. Prusty 	}
133618382eadSSubhransu S. Prusty 
133718382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
133818382eadSSubhransu S. Prusty 
133915b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
134018382eadSSubhransu S. Prusty 		return -EIO;
134118382eadSSubhransu S. Prusty 
134217a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
134317a42c45SSubhransu S. Prusty 	if (ret) {
134417a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
134517a42c45SSubhransu S. Prusty 							ret);
134617a42c45SSubhransu S. Prusty 		return ret;
134717a42c45SSubhransu S. Prusty 	}
134817a42c45SSubhransu S. Prusty 
134917a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
135017a42c45SSubhransu S. Prusty 
135115b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
135218382eadSSubhransu S. Prusty }
135318382eadSSubhransu S. Prusty 
1354b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1355b8a54545SSubhransu S. Prusty {
1356b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1357b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1358b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1359b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1360b8a54545SSubhransu S. Prusty 
1361b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1362b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1363b8a54545SSubhransu S. Prusty 
1364b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1365b8a54545SSubhransu S. Prusty 
1366b8a54545SSubhransu S. Prusty 	/*
1367b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1368b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1369b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1370b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1371b8a54545SSubhransu S. Prusty 	 */
1372b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1373b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1374b8a54545SSubhransu S. Prusty 		return;
1375b8a54545SSubhransu S. Prusty 
1376b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1377b8a54545SSubhransu S. Prusty 		return;
1378b8a54545SSubhransu S. Prusty 
1379b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1380b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1381b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1382b8a54545SSubhransu S. Prusty 	}
1383b8a54545SSubhransu S. Prusty }
1384b8a54545SSubhransu S. Prusty 
1385b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1386b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1387b8a54545SSubhransu S. Prusty };
1388b8a54545SSubhransu S. Prusty 
13894a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
13904a3478deSJeeja KP {
13914a3478deSJeeja KP 	char jack_name[NAME_SIZE];
13924a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
13934a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
13944a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
13954a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
13964a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
13974a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
13984a3478deSJeeja KP 
13994a3478deSJeeja KP 	/*
14004a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
14014a3478deSJeeja KP 	 * add to the pcm list
14024a3478deSJeeja KP 	 */
14034a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
14044a3478deSJeeja KP 	if (!pcm)
14054a3478deSJeeja KP 		return -ENOMEM;
14064a3478deSJeeja KP 	pcm->pcm_id = device;
14074a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
14084a3478deSJeeja KP 
14094a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
14104a3478deSJeeja KP 
14114a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
14124a3478deSJeeja KP 
14134a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
14144a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
14154a3478deSJeeja KP }
14164a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
14174a3478deSJeeja KP 
141818382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
141918382eadSSubhransu S. Prusty {
142018382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
142118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
142218382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
142318382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1424b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1425b8a54545SSubhransu S. Prusty 	int ret;
142618382eadSSubhransu S. Prusty 
142718382eadSSubhransu S. Prusty 	edev->scodec = codec;
142818382eadSSubhransu S. Prusty 
142979f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
143079f4e922SSubhransu S. Prusty 	if (ret < 0)
143179f4e922SSubhransu S. Prusty 		return ret;
143218382eadSSubhransu S. Prusty 
1433b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1434b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1435b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1436b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1437b8a54545SSubhransu S. Prusty 				ret);
1438b8a54545SSubhransu S. Prusty 		return ret;
1439b8a54545SSubhransu S. Prusty 	}
1440b8a54545SSubhransu S. Prusty 
1441b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1442b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1443b8a54545SSubhransu S. Prusty 
144418382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
144518382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
144618382eadSSubhransu S. Prusty 
1447e342ac08SSubhransu S. Prusty 	/*
1448e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1449e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1450e342ac08SSubhransu S. Prusty 	 */
1451e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1452e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1453e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1454e342ac08SSubhransu S. Prusty 
1455e342ac08SSubhransu S. Prusty 	return 0;
1456e342ac08SSubhransu S. Prusty }
1457e342ac08SSubhransu S. Prusty 
1458e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1459e342ac08SSubhransu S. Prusty {
1460e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1461e342ac08SSubhransu S. Prusty 
1462e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
146318382eadSSubhransu S. Prusty 	return 0;
146418382eadSSubhransu S. Prusty }
146518382eadSSubhransu S. Prusty 
1466571d5078SJeeja KP #ifdef CONFIG_PM
1467571d5078SJeeja KP static int hdmi_codec_resume(struct snd_soc_codec *codec)
1468571d5078SJeeja KP {
1469571d5078SJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1470571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1471571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1472571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
1473571d5078SJeeja KP 	struct hdac_bus *bus = hdac->bus;
1474571d5078SJeeja KP 	int err;
1475571d5078SJeeja KP 	unsigned long timeout;
1476571d5078SJeeja KP 
1477571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1478571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1479571d5078SJeeja KP 
1480571d5078SJeeja KP 	/* Power up afg */
1481571d5078SJeeja KP 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1482571d5078SJeeja KP 
1483571d5078SJeeja KP 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1484571d5078SJeeja KP 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1485571d5078SJeeja KP 
1486571d5078SJeeja KP 		/* Wait till power state is set to D0 */
1487571d5078SJeeja KP 		timeout = jiffies + msecs_to_jiffies(1000);
1488571d5078SJeeja KP 		while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1489571d5078SJeeja KP 				&& time_before(jiffies, timeout)) {
1490571d5078SJeeja KP 			msleep(50);
1491571d5078SJeeja KP 		}
1492571d5078SJeeja KP 	}
1493571d5078SJeeja KP 
1494571d5078SJeeja KP 	/*
1495571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1496571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1497571d5078SJeeja KP 	 * all pins here.
1498571d5078SJeeja KP 	 */
1499571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1500571d5078SJeeja KP 		hdac_hdmi_present_sense(pin, 1);
1501571d5078SJeeja KP 
1502571d5078SJeeja KP 	/*
1503571d5078SJeeja KP 	 * Codec power is turned ON during controller resume.
1504571d5078SJeeja KP 	 * Turn it OFF here
1505571d5078SJeeja KP 	 */
1506571d5078SJeeja KP 	err = snd_hdac_display_power(bus, false);
1507571d5078SJeeja KP 	if (err < 0) {
1508571d5078SJeeja KP 		dev_err(bus->dev,
1509571d5078SJeeja KP 			"Cannot turn OFF display power on i915, err: %d\n",
1510571d5078SJeeja KP 			err);
1511571d5078SJeeja KP 		return err;
1512571d5078SJeeja KP 	}
1513571d5078SJeeja KP 
1514571d5078SJeeja KP 	return 0;
1515571d5078SJeeja KP }
1516571d5078SJeeja KP #else
1517571d5078SJeeja KP #define hdmi_codec_resume NULL
1518571d5078SJeeja KP #endif
1519571d5078SJeeja KP 
152018382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
152118382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1522e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
1523571d5078SJeeja KP 	.resume		= hdmi_codec_resume,
152418382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
152518382eadSSubhransu S. Prusty };
152618382eadSSubhransu S. Prusty 
152718382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
152818382eadSSubhransu S. Prusty {
152918382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
153018382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
153117a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
153217a42c45SSubhransu S. Prusty 	int num_dais = 0;
153318382eadSSubhransu S. Prusty 	int ret = 0;
153418382eadSSubhransu S. Prusty 
153518382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
153618382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
153718382eadSSubhransu S. Prusty 		return -ENOMEM;
153818382eadSSubhransu S. Prusty 
153918382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1540bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
154118382eadSSubhransu S. Prusty 
154218382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
154318382eadSSubhransu S. Prusty 
154415b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
154515b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
15464a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
15474a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
154815b91447SSubhransu S. Prusty 
1549aeaccef0SRamesh Babu 	/*
1550aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1551aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1552aeaccef0SRamesh Babu 	 */
1553aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1554aeaccef0SRamesh Babu 	if (ret < 0) {
1555aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1556aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1557aeaccef0SRamesh Babu 			ret);
1558aeaccef0SRamesh Babu 		return ret;
1559aeaccef0SRamesh Babu 	}
1560aeaccef0SRamesh Babu 
156117a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
156217a42c45SSubhransu S. Prusty 	if (ret < 0) {
156317a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
156417a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
156518382eadSSubhransu S. Prusty 		return ret;
156617a42c45SSubhransu S. Prusty 	}
156718382eadSSubhransu S. Prusty 
156818382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
156918382eadSSubhransu S. Prusty 	return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
157017a42c45SSubhransu S. Prusty 			hdmi_dais, num_dais);
157118382eadSSubhransu S. Prusty }
157218382eadSSubhransu S. Prusty 
157318382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
157418382eadSSubhransu S. Prusty {
157515b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
157615b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
157715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
15784a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
157915b91447SSubhransu S. Prusty 
158018382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
158118382eadSSubhransu S. Prusty 
15824a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
15834a3478deSJeeja KP 		pcm->cvt = NULL;
15844a3478deSJeeja KP 		pcm->pin = NULL;
15854a3478deSJeeja KP 		list_del(&pcm->head);
15864a3478deSJeeja KP 		kfree(pcm);
15874a3478deSJeeja KP 	}
15884a3478deSJeeja KP 
158915b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
159015b91447SSubhransu S. Prusty 		list_del(&cvt->head);
15914a3478deSJeeja KP 		kfree(cvt->name);
159215b91447SSubhransu S. Prusty 		kfree(cvt);
159315b91447SSubhransu S. Prusty 	}
159415b91447SSubhransu S. Prusty 
159515b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
159615b91447SSubhransu S. Prusty 		list_del(&pin->head);
159715b91447SSubhransu S. Prusty 		kfree(pin);
159815b91447SSubhransu S. Prusty 	}
159915b91447SSubhransu S. Prusty 
160018382eadSSubhransu S. Prusty 	return 0;
160118382eadSSubhransu S. Prusty }
160218382eadSSubhransu S. Prusty 
1603e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1604e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1605e342ac08SSubhransu S. Prusty {
1606e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1607e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
160807f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
16097ed49700SSubhransu S. Prusty 	unsigned long timeout;
161007f083abSSubhransu S. Prusty 	int err;
1611e342ac08SSubhransu S. Prusty 
1612e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1613e342ac08SSubhransu S. Prusty 
161407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
161507f083abSSubhransu S. Prusty 	if (!bus)
161607f083abSSubhransu S. Prusty 		return 0;
161707f083abSSubhransu S. Prusty 
1618e342ac08SSubhransu S. Prusty 	/* Power down afg */
16197ed49700SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
1620e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1621e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1622e342ac08SSubhransu S. Prusty 
16237ed49700SSubhransu S. Prusty 		/* Wait till power state is set to D3 */
16247ed49700SSubhransu S. Prusty 		timeout = jiffies + msecs_to_jiffies(1000);
16257ed49700SSubhransu S. Prusty 		while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
16267ed49700SSubhransu S. Prusty 				&& time_before(jiffies, timeout)) {
16277ed49700SSubhransu S. Prusty 
16287ed49700SSubhransu S. Prusty 			msleep(50);
16297ed49700SSubhransu S. Prusty 		}
16307ed49700SSubhransu S. Prusty 	}
16317ed49700SSubhransu S. Prusty 
163207f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
163307f083abSSubhransu S. Prusty 	if (err < 0) {
163407f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
163507f083abSSubhransu S. Prusty 		return err;
163607f083abSSubhransu S. Prusty 	}
163707f083abSSubhransu S. Prusty 
1638e342ac08SSubhransu S. Prusty 	return 0;
1639e342ac08SSubhransu S. Prusty }
1640e342ac08SSubhransu S. Prusty 
1641e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1642e342ac08SSubhransu S. Prusty {
1643e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1644e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
164507f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
164607f083abSSubhransu S. Prusty 	int err;
1647e342ac08SSubhransu S. Prusty 
1648e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1649e342ac08SSubhransu S. Prusty 
165007f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
165107f083abSSubhransu S. Prusty 	if (!bus)
165207f083abSSubhransu S. Prusty 		return 0;
165307f083abSSubhransu S. Prusty 
165407f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
165507f083abSSubhransu S. Prusty 	if (err < 0) {
165607f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
165707f083abSSubhransu S. Prusty 		return err;
165807f083abSSubhransu S. Prusty 	}
165907f083abSSubhransu S. Prusty 
1660ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1661ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1662ab85f5b3SSubhransu S. Prusty 
1663e342ac08SSubhransu S. Prusty 	/* Power up afg */
1664e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1665e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1666e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1667e342ac08SSubhransu S. Prusty 
1668e342ac08SSubhransu S. Prusty 	return 0;
1669e342ac08SSubhransu S. Prusty }
1670e342ac08SSubhransu S. Prusty #else
1671e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1672e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1673e342ac08SSubhransu S. Prusty #endif
1674e342ac08SSubhransu S. Prusty 
1675e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1676e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1677e342ac08SSubhransu S. Prusty };
1678e342ac08SSubhransu S. Prusty 
167918382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
168018382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1681e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
168218382eadSSubhransu S. Prusty 	{}
168318382eadSSubhransu S. Prusty };
168418382eadSSubhransu S. Prusty 
168518382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
168618382eadSSubhransu S. Prusty 
168718382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
168818382eadSSubhransu S. Prusty 	. hdac = {
168918382eadSSubhransu S. Prusty 		.driver = {
169018382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1691e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
169218382eadSSubhransu S. Prusty 		},
169318382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
169418382eadSSubhransu S. Prusty 	},
169518382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
169618382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
169718382eadSSubhransu S. Prusty };
169818382eadSSubhransu S. Prusty 
169918382eadSSubhransu S. Prusty static int __init hdmi_init(void)
170018382eadSSubhransu S. Prusty {
170118382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
170218382eadSSubhransu S. Prusty }
170318382eadSSubhransu S. Prusty 
170418382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
170518382eadSSubhransu S. Prusty {
170618382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
170718382eadSSubhransu S. Prusty }
170818382eadSSubhransu S. Prusty 
170918382eadSSubhransu S. Prusty module_init(hdmi_init);
171018382eadSSubhransu S. Prusty module_exit(hdmi_exit);
171118382eadSSubhransu S. Prusty 
171218382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
171318382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
171418382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
171518382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1716