xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 500e06b9)
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 
1172889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
1182889099eSSubhransu S. Prusty 						int pcm_idx)
1192889099eSSubhransu S. Prusty {
1202889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
1212889099eSSubhransu S. Prusty 
1222889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
1232889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
1242889099eSSubhransu S. Prusty 			return pcm;
1252889099eSSubhransu S. Prusty 	}
1262889099eSSubhransu S. Prusty 
1272889099eSSubhransu S. Prusty 	return NULL;
1282889099eSSubhransu S. Prusty }
1292889099eSSubhransu S. Prusty 
130e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
131e342ac08SSubhransu S. Prusty {
13251b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
133e342ac08SSubhransu S. Prusty 
13451b2c425SGeliang Tang 	return to_ehdac_device(hdac);
135e342ac08SSubhransu S. Prusty }
136e342ac08SSubhransu S. Prusty 
1372428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1382428bca3SSubhransu S. Prusty {
1392428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1402428bca3SSubhransu S. Prusty }
1412428bca3SSubhransu S. Prusty 
1422428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1432428bca3SSubhransu S. Prusty {
1442428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1452428bca3SSubhransu S. Prusty }
1462428bca3SSubhransu S. Prusty 
1472428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1482428bca3SSubhransu S. Prusty 						void *eld)
1492428bca3SSubhransu S. Prusty {
1502428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1512428bca3SSubhransu S. Prusty 	int i;
1522428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1532428bca3SSubhransu S. Prusty 
1542428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1552428bca3SSubhransu S. Prusty 	if (!sad)
1562428bca3SSubhransu S. Prusty 		goto format_constraint;
1572428bca3SSubhransu S. Prusty 
1582428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1592428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1602428bca3SSubhransu S. Prusty 
1612428bca3SSubhransu S. Prusty 			/*
1622428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1632428bca3SSubhransu S. Prusty 			 * container so we set S32
1642428bca3SSubhransu S. Prusty 			 */
1652428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1662428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1672428bca3SSubhransu S. Prusty 		}
1682428bca3SSubhransu S. Prusty 	}
1692428bca3SSubhransu S. Prusty 
1702428bca3SSubhransu S. Prusty format_constraint:
1712428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1722428bca3SSubhransu S. Prusty 				formats);
1732428bca3SSubhransu S. Prusty 
1742428bca3SSubhransu S. Prusty }
1752428bca3SSubhransu S. Prusty 
176b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
177b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
178b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
179b8a54545SSubhransu S. Prusty {
180b8a54545SSubhransu S. Prusty 	unsigned int val;
181b8a54545SSubhransu S. Prusty 
182b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
183b8a54545SSubhransu S. Prusty 							byte_index);
184b8a54545SSubhransu S. Prusty 
185b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
186b8a54545SSubhransu S. Prusty 					byte_index, val);
187b8a54545SSubhransu S. Prusty 
188b8a54545SSubhransu S. Prusty 	return val;
189b8a54545SSubhransu S. Prusty }
190b8a54545SSubhransu S. Prusty 
191b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
192b8a54545SSubhransu S. Prusty {
193b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
194b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
195b8a54545SSubhransu S. Prusty }
196b8a54545SSubhransu S. Prusty 
197b8a54545SSubhransu S. Prusty /*
198b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
199b8a54545SSubhransu S. Prusty  * passed by user
200b8a54545SSubhransu S. Prusty  */
201b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
202b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
203b8a54545SSubhransu S. Prusty {
204b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
205b8a54545SSubhransu S. Prusty 
206b8a54545SSubhransu S. Prusty 	/*
207b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
208b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
209b8a54545SSubhransu S. Prusty 	 */
210b8a54545SSubhransu S. Prusty 
211b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
212b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
213b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
214b8a54545SSubhransu S. Prusty 		return -ERANGE;
215b8a54545SSubhransu S. Prusty 	}
216b8a54545SSubhransu S. Prusty 
217b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
218b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
219b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
220b8a54545SSubhransu S. Prusty 		/*
221b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
222b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
223b8a54545SSubhransu S. Prusty 		 */
224b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
225b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
226b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
227b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
228b8a54545SSubhransu S. Prusty 			goto error;
229b8a54545SSubhransu S. Prusty 		}
230b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
231b8a54545SSubhransu S. Prusty 		/*
232b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
233b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
234b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
235b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
236b8a54545SSubhransu S. Prusty 		 */
237b8a54545SSubhransu S. Prusty 		if (!val && !i) {
238b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
239b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
240b8a54545SSubhransu S. Prusty 			goto error;
241b8a54545SSubhransu S. Prusty 		}
242b8a54545SSubhransu S. Prusty 		buf[i] = val;
243b8a54545SSubhransu S. Prusty 	}
244b8a54545SSubhransu S. Prusty 
245b8a54545SSubhransu S. Prusty 	*eld_size = size;
246b8a54545SSubhransu S. Prusty error:
247b8a54545SSubhransu S. Prusty 	return ret;
248b8a54545SSubhransu S. Prusty }
249b8a54545SSubhransu S. Prusty 
250b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
251b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
252b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
253b0362adbSSubhransu S. Prusty {
254b0362adbSSubhransu S. Prusty 	unsigned int val;
255b0362adbSSubhransu S. Prusty 
256b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
257b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
258b0362adbSSubhransu S. Prusty 
259b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
260b0362adbSSubhransu S. Prusty 
261b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
262b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
263b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
264b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
265b0362adbSSubhransu S. Prusty 
266b0362adbSSubhransu S. Prusty 	return 0;
267b0362adbSSubhransu S. Prusty }
268b0362adbSSubhransu S. Prusty 
269a657f1d0SSubhransu S. Prusty static void
270a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
271a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
272a657f1d0SSubhransu S. Prusty {
273a657f1d0SSubhransu S. Prusty 	int val;
274a657f1d0SSubhransu S. Prusty 
275a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
276a657f1d0SSubhransu S. Prusty 
277a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
278a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
279a657f1d0SSubhransu S. Prusty }
280a657f1d0SSubhransu S. Prusty 
281478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
282478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
283478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
284478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
285478f544eSSubhransu S. Prusty 
286478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
287478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
288478f544eSSubhransu S. Prusty 	u8 CXT04;
289478f544eSSubhransu S. Prusty 	u8 CA;
290478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
291478f544eSSubhransu S. Prusty };
292478f544eSSubhransu S. Prusty 
293a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
294a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
295a657f1d0SSubhransu S. Prusty {
296a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
297a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
298478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
299478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
300478f544eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
301478f544eSSubhransu S. Prusty 	u8 *dip;
302a657f1d0SSubhransu S. Prusty 	int ret;
303a657f1d0SSubhransu S. Prusty 	int i;
304478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
305478f544eSSubhransu S. Prusty 	u8 conn_type;
306bcced704SSubhransu S. Prusty 	int channels, ca;
307a657f1d0SSubhransu S. Prusty 
308478f544eSSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
309478f544eSSubhransu S. Prusty 		if (pin->nid == pin_nid)
310478f544eSSubhransu S. Prusty 			break;
311478f544eSSubhransu S. Prusty 	}
312a657f1d0SSubhransu S. Prusty 
313bcced704SSubhransu S. Prusty 	ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
314bcced704SSubhransu S. Prusty 			pin->channels, pin->chmap_set, true, pin->chmap);
315bcced704SSubhransu S. Prusty 
316bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
317bcced704SSubhransu S. Prusty 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
318bcced704SSubhransu S. Prusty 
319bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
320bcced704SSubhransu S. Prusty 				pin->channels, pin->chmap, pin->chmap_set);
321bcced704SSubhransu S. Prusty 
322478f544eSSubhransu S. Prusty 	eld_buf = pin->eld.eld_buffer;
323478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
324a657f1d0SSubhransu S. Prusty 
325478f544eSSubhransu S. Prusty 	switch (conn_type) {
326478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
327478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
328478f544eSSubhransu S. Prusty 
329478f544eSSubhransu S. Prusty 		frame.channels = channels;
330bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
331a657f1d0SSubhransu S. Prusty 
332a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
333a657f1d0SSubhransu S. Prusty 		if (ret < 0)
334a657f1d0SSubhransu S. Prusty 			return ret;
335a657f1d0SSubhransu S. Prusty 
336478f544eSSubhransu S. Prusty 		break;
337478f544eSSubhransu S. Prusty 
338478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
339478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
340478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
341478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
342478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
343478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
344bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
345478f544eSSubhransu S. Prusty 
346478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
347478f544eSSubhransu S. Prusty 		break;
348478f544eSSubhransu S. Prusty 
349478f544eSSubhransu S. Prusty 	default:
350478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
351478f544eSSubhransu S. Prusty 						conn_type);
352478f544eSSubhransu S. Prusty 		return -EIO;
353478f544eSSubhransu S. Prusty 	}
354478f544eSSubhransu S. Prusty 
355a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
356a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
357a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
358a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
359a657f1d0SSubhransu S. Prusty 
360a657f1d0SSubhransu S. Prusty 
361a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
362a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
363478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
364391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
365a657f1d0SSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
366391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
367478f544eSSubhransu S. Prusty 	} else {
368478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
369478f544eSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
370478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
371478f544eSSubhransu S. Prusty 	}
372a657f1d0SSubhransu S. Prusty 
373a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
374a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
375a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
376a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
377a657f1d0SSubhransu S. Prusty 
378a657f1d0SSubhransu S. Prusty 	return 0;
379a657f1d0SSubhransu S. Prusty }
380a657f1d0SSubhransu S. Prusty 
381b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
382b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
383b0362adbSSubhransu S. Prusty {
384b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
38515b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
38615b91447SSubhransu S. Prusty 						pwr_state))
38715b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
388b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
389b0362adbSSubhransu S. Prusty 
390b0362adbSSubhransu S. Prusty 	/* Power up converter */
39115b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
39215b91447SSubhransu S. Prusty 						pwr_state))
39315b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
394b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
395b0362adbSSubhransu S. Prusty }
396b0362adbSSubhransu S. Prusty 
397b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
398b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
399b0362adbSSubhransu S. Prusty {
400b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
401b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
402b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
403bcced704SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
404b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
405a657f1d0SSubhransu S. Prusty 	int ret;
406b0362adbSSubhransu S. Prusty 
407b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
408bcced704SSubhransu S. Prusty 	pin = dai_map->pin;
409b0362adbSSubhransu S. Prusty 
410b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
411b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
412b0362adbSSubhransu S. Prusty 			dd->stream_tag,	dd->format);
413b0362adbSSubhransu S. Prusty 
414bcced704SSubhransu S. Prusty 	mutex_lock(&pin->lock);
415bcced704SSubhransu S. Prusty 	pin->channels = substream->runtime->channels;
416bcced704SSubhransu S. Prusty 
41715b91447SSubhransu S. Prusty 	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
41815b91447SSubhransu S. Prusty 						dai_map->pin->nid);
419bcced704SSubhransu S. Prusty 	mutex_unlock(&pin->lock);
420a657f1d0SSubhransu S. Prusty 	if (ret < 0)
421a657f1d0SSubhransu S. Prusty 		return ret;
422a657f1d0SSubhransu S. Prusty 
42315b91447SSubhransu S. Prusty 	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
42415b91447SSubhransu S. Prusty 			dai_map->pin->nid, dd->stream_tag, dd->format);
425b0362adbSSubhransu S. Prusty }
426b0362adbSSubhransu S. Prusty 
427b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
428b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
429b0362adbSSubhransu S. Prusty {
430b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
43154dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
43254dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
43354dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
434b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
435b0362adbSSubhransu S. Prusty 
43654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
43754dfa1eaSSubhransu S. Prusty 	pin = dai_map->pin;
43854dfa1eaSSubhransu S. Prusty 
43954dfa1eaSSubhransu S. Prusty 	if (!pin)
44054dfa1eaSSubhransu S. Prusty 		return -ENODEV;
44154dfa1eaSSubhransu S. Prusty 
44254dfa1eaSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
44354dfa1eaSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
44454dfa1eaSSubhransu S. Prusty 								pin->nid);
445b0362adbSSubhransu S. Prusty 		return -ENODEV;
446b0362adbSSubhransu S. Prusty 	}
447b0362adbSSubhransu S. Prusty 
4486793a3d7SSubhransu S. Prusty 	dd = snd_soc_dai_get_dma_data(dai, substream);
4496793a3d7SSubhransu S. Prusty 	if (!dd) {
450b0362adbSSubhransu S. Prusty 		dd = kzalloc(sizeof(*dd), GFP_KERNEL);
4518d33ab24SSudip Mukherjee 		if (!dd)
4528d33ab24SSudip Mukherjee 			return -ENOMEM;
4536793a3d7SSubhransu S. Prusty 	}
4546793a3d7SSubhransu S. Prusty 
455b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
456b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
457b0362adbSSubhransu S. Prusty 			24, 0);
458b0362adbSSubhransu S. Prusty 
459b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
460b0362adbSSubhransu S. Prusty 
461b0362adbSSubhransu S. Prusty 	return 0;
462b0362adbSSubhransu S. Prusty }
463b0362adbSSubhransu S. Prusty 
464b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
465b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
466b0362adbSSubhransu S. Prusty {
467b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
468b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
469b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
470b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
471b0362adbSSubhransu S. Prusty 
472b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
473b0362adbSSubhransu S. Prusty 
474b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
475b0362adbSSubhransu S. Prusty 
4766793a3d7SSubhransu S. Prusty 	if (dd) {
4776793a3d7SSubhransu S. Prusty 		snd_soc_dai_set_dma_data(dai, substream, NULL);
478b0362adbSSubhransu S. Prusty 		kfree(dd);
4796793a3d7SSubhransu S. Prusty 	}
480b0362adbSSubhransu S. Prusty 
481b0362adbSSubhransu S. Prusty 	return 0;
482b0362adbSSubhransu S. Prusty }
483b0362adbSSubhransu S. Prusty 
484ab85f5b3SSubhransu S. Prusty static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
485ab85f5b3SSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
486ab85f5b3SSubhransu S. Prusty {
487ab85f5b3SSubhransu S. Prusty 	/* Enable transmission */
488ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
489ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_1, 1);
490ab85f5b3SSubhransu S. Prusty 
491ab85f5b3SSubhransu S. Prusty 	/* Category Code (CC) to zero */
492ab85f5b3SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
493ab85f5b3SSubhransu S. Prusty 			AC_VERB_SET_DIGI_CONVERT_2, 0);
494ab85f5b3SSubhransu S. Prusty }
495ab85f5b3SSubhransu S. Prusty 
496148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
497148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
498148569fdSSubhransu S. Prusty {
499148569fdSSubhransu S. Prusty 	int mux_idx;
500148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
501148569fdSSubhransu S. Prusty 
502148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
503148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
504148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
505148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
506148569fdSSubhransu S. Prusty 			break;
507148569fdSSubhransu S. Prusty 		}
508148569fdSSubhransu S. Prusty 	}
509148569fdSSubhransu S. Prusty 
510148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
511148569fdSSubhransu S. Prusty 		return -EIO;
512148569fdSSubhransu S. Prusty 
513148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
514148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
515148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
516148569fdSSubhransu S. Prusty 
517148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
518148569fdSSubhransu S. Prusty 
519148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
520148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
521148569fdSSubhransu S. Prusty 
522148569fdSSubhransu S. Prusty 	return 0;
523148569fdSSubhransu S. Prusty }
524148569fdSSubhransu S. Prusty 
525148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
526148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
527148569fdSSubhransu S. Prusty {
528148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
529148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
530148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
531148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
532148569fdSSubhransu S. Prusty 		return -EINVAL;
533148569fdSSubhransu S. Prusty 	}
534148569fdSSubhransu S. Prusty 
535148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
536148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
537148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
538148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
539148569fdSSubhransu S. Prusty 								pin->nid);
540148569fdSSubhransu S. Prusty 
541148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
542148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
543148569fdSSubhransu S. Prusty 
544148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
545148569fdSSubhransu S. Prusty }
546148569fdSSubhransu S. Prusty 
547148569fdSSubhransu S. Prusty /*
548148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
549148569fdSSubhransu S. Prusty  *
550148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
551148569fdSSubhransu S. Prusty  *
552148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
553148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
554148569fdSSubhransu S. Prusty  * connected.
555148569fdSSubhransu S. Prusty  */
556148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
557148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
558148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
559148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
560148569fdSSubhransu S. Prusty {
561148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
562148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
563148569fdSSubhransu S. Prusty 	int ret, i;
564148569fdSSubhransu S. Prusty 
565148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
566148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
567148569fdSSubhransu S. Prusty 			pin = pcm->pin;
568148569fdSSubhransu S. Prusty 			break;
569148569fdSSubhransu S. Prusty 		}
570148569fdSSubhransu S. Prusty 	}
571148569fdSSubhransu S. Prusty 
572148569fdSSubhransu S. Prusty 	if (pin) {
573148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
574148569fdSSubhransu S. Prusty 		if (ret < 0)
575148569fdSSubhransu S. Prusty 			return NULL;
576148569fdSSubhransu S. Prusty 
577148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
578148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
579148569fdSSubhransu S. Prusty 				return pin;
580148569fdSSubhransu S. Prusty 		}
581148569fdSSubhransu S. Prusty 	}
582148569fdSSubhransu S. Prusty 
583148569fdSSubhransu S. Prusty 	return NULL;
584148569fdSSubhransu S. Prusty }
585148569fdSSubhransu S. Prusty 
58654dfa1eaSSubhransu S. Prusty /*
58754dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
58854dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
58954dfa1eaSSubhransu S. Prusty  * doesn't fail.
59054dfa1eaSSubhransu S. Prusty  */
591b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
592b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
593b0362adbSSubhransu S. Prusty {
594b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
595b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
596b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
597148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
598148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
5992428bca3SSubhransu S. Prusty 	int ret;
600b0362adbSSubhransu S. Prusty 
601b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
602b0362adbSSubhransu S. Prusty 
603148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
604148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
60554dfa1eaSSubhransu S. Prusty 
60654dfa1eaSSubhransu S. Prusty 	/*
60754dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
60854dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
60954dfa1eaSSubhransu S. Prusty 	 */
610148569fdSSubhransu S. Prusty 	if (!pin)
61154dfa1eaSSubhransu S. Prusty 		return 0;
612148569fdSSubhransu S. Prusty 
613148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
614148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
615b0362adbSSubhransu S. Prusty 
61654dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
617148569fdSSubhransu S. Prusty 			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
618148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
619b8a54545SSubhransu S. Prusty 
62054dfa1eaSSubhransu S. Prusty 		return 0;
621b0362adbSSubhransu S. Prusty 	}
622b0362adbSSubhransu S. Prusty 
623148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
624b0362adbSSubhransu S. Prusty 
625ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_enable_cvt(hdac, dai_map);
626148569fdSSubhransu S. Prusty 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
627148569fdSSubhransu S. Prusty 	if (ret < 0)
628148569fdSSubhransu S. Prusty 		return ret;
629b0362adbSSubhransu S. Prusty 
6302428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
631148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
6322428bca3SSubhransu S. Prusty 	if (ret < 0)
6332428bca3SSubhransu S. Prusty 		return ret;
634b0362adbSSubhransu S. Prusty 
6352428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
636148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
637b0362adbSSubhransu S. Prusty }
638b0362adbSSubhransu S. Prusty 
639571d5078SJeeja KP static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
640571d5078SJeeja KP 		struct snd_soc_dai *dai)
641571d5078SJeeja KP {
642571d5078SJeeja KP 	struct hdac_hdmi_dai_pin_map *dai_map;
643571d5078SJeeja KP 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
644571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
645571d5078SJeeja KP 	int ret;
646571d5078SJeeja KP 
647571d5078SJeeja KP 	dai_map = &hdmi->dai_map[dai->id];
648571d5078SJeeja KP 	if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
649571d5078SJeeja KP 		ret = hdac_hdmi_enable_pin(hdac, dai_map);
650571d5078SJeeja KP 		if (ret < 0)
651571d5078SJeeja KP 			return ret;
652571d5078SJeeja KP 
653571d5078SJeeja KP 		return hdac_hdmi_playback_prepare(substream, dai);
654571d5078SJeeja KP 	}
655571d5078SJeeja KP 
656571d5078SJeeja KP 	return 0;
657571d5078SJeeja KP }
658571d5078SJeeja KP 
659b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
660b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
661b0362adbSSubhransu S. Prusty {
662b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
663b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
664b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
665b0362adbSSubhransu S. Prusty 
666b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
667b0362adbSSubhransu S. Prusty 
66854dfa1eaSSubhransu S. Prusty 	if (dai_map->pin) {
66954dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
67054dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
67154dfa1eaSSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
67254dfa1eaSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
67354dfa1eaSSubhransu S. Prusty 
674b0362adbSSubhransu S. Prusty 		hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
675b0362adbSSubhransu S. Prusty 
67615b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
677b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
678148569fdSSubhransu S. Prusty 
679bcced704SSubhransu S. Prusty 		mutex_lock(&dai_map->pin->lock);
6802889099eSSubhransu S. Prusty 		dai_map->pin->chmap_set = false;
6812889099eSSubhransu S. Prusty 		memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
682bcced704SSubhransu S. Prusty 		dai_map->pin->channels = 0;
683bcced704SSubhransu S. Prusty 		mutex_unlock(&dai_map->pin->lock);
684bcced704SSubhransu S. Prusty 
685148569fdSSubhransu S. Prusty 		dai_map->pin = NULL;
686b0362adbSSubhransu S. Prusty 	}
68754dfa1eaSSubhransu S. Prusty }
688b0362adbSSubhransu S. Prusty 
68918382eadSSubhransu S. Prusty static int
69018382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
69118382eadSSubhransu S. Prusty {
692bcced704SSubhransu S. Prusty 	unsigned int chans;
693bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
694bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
69518382eadSSubhransu S. Prusty 	int err;
69618382eadSSubhransu S. Prusty 
697bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
698bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
699bcced704SSubhransu S. Prusty 
700bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
701bcced704SSubhransu S. Prusty 
702bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
703bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
704bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
70518382eadSSubhransu S. Prusty 
70618382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
70718382eadSSubhransu S. Prusty 			&cvt->params.rates,
70818382eadSSubhransu S. Prusty 			&cvt->params.formats,
70918382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
71018382eadSSubhransu S. Prusty 	if (err < 0)
71118382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
71218382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
71318382eadSSubhransu S. Prusty 			cvt->nid, err);
71418382eadSSubhransu S. Prusty 
71518382eadSSubhransu S. Prusty 	return err;
71618382eadSSubhransu S. Prusty }
71718382eadSSubhransu S. Prusty 
71879f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
71979f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
72079f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
72179f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
72279f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
72318382eadSSubhransu S. Prusty {
72418382eadSSubhransu S. Prusty 	w->id = id;
72579f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
72679f4e922SSubhransu S. Prusty 	if (!w->name)
72779f4e922SSubhransu S. Prusty 		return -ENOMEM;
72879f4e922SSubhransu S. Prusty 
72918382eadSSubhransu S. Prusty 	w->sname = stream;
73018382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
73118382eadSSubhransu S. Prusty 	w->shift = 0;
73279f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
73379f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
73479f4e922SSubhransu S. Prusty 	w->priv = priv;
73579f4e922SSubhransu S. Prusty 
73679f4e922SSubhransu S. Prusty 	return 0;
73718382eadSSubhransu S. Prusty }
73818382eadSSubhransu S. Prusty 
73918382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
74079f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
74179f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
74279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
74318382eadSSubhransu S. Prusty {
74418382eadSSubhransu S. Prusty 	route->sink = sink;
74518382eadSSubhransu S. Prusty 	route->source = src;
74618382eadSSubhransu S. Prusty 	route->control = control;
74779f4e922SSubhransu S. Prusty 	route->connected = handler;
74818382eadSSubhransu S. Prusty }
74918382eadSSubhransu S. Prusty 
7504a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
7514a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
7524a3478deSJeeja KP {
7534a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7544a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7554a3478deSJeeja KP 
7564a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7574a3478deSJeeja KP 		if (pcm->pin == pin)
7584a3478deSJeeja KP 			return pcm;
7594a3478deSJeeja KP 	}
7604a3478deSJeeja KP 
7614a3478deSJeeja KP 	return NULL;
7624a3478deSJeeja KP }
7634a3478deSJeeja KP 
7644a3478deSJeeja KP /*
7654a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
7664a3478deSJeeja KP  */
7674a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
7684a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
7694a3478deSJeeja KP {
7704a3478deSJeeja KP 	int ret;
7714a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
7724a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
7734a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
7744a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
7754a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
7764a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7774a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
7784a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
7794a3478deSJeeja KP 
7804a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
7814a3478deSJeeja KP 	if (ret < 0)
7824a3478deSJeeja KP 		return ret;
7834a3478deSJeeja KP 
7844a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
7854a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
7864a3478deSJeeja KP 		if (pcm->pin == pin)
7874a3478deSJeeja KP 			pcm->pin = NULL;
7884a3478deSJeeja KP 
7894a3478deSJeeja KP 		/*
7904a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
7914a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
7924a3478deSJeeja KP 		 */
7934a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
7944a3478deSJeeja KP 			pcm->pin = pin;
7954a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
7964a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
7974a3478deSJeeja KP 					"jack report for pcm=%d\n",
7984a3478deSJeeja KP 					pcm->pcm_id);
7994a3478deSJeeja KP 
8004a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
8014a3478deSJeeja KP 			}
8024a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
8034a3478deSJeeja KP 			return ret;
8044a3478deSJeeja KP 		}
8054a3478deSJeeja KP 	}
8064a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
8074a3478deSJeeja KP 
8084a3478deSJeeja KP 	return ret;
8094a3478deSJeeja KP }
8104a3478deSJeeja KP 
81179f4e922SSubhransu S. Prusty /*
81279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
81379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
81479f4e922SSubhransu S. Prusty  * widget runtime.
81579f4e922SSubhransu S. Prusty  *
81679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
81779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
81879f4e922SSubhransu S. Prusty  * "NONE"
81979f4e922SSubhransu S. Prusty  */
82079f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
82179f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
82279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
82379f4e922SSubhransu S. Prusty 				const char *widget_name)
82418382eadSSubhransu S. Prusty {
82579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
82679f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
82779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
82879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
82979f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
83079f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
83179f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
83279f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
83379f4e922SSubhransu S. Prusty 	int i = 0;
83479f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
83518382eadSSubhransu S. Prusty 
83679f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
83779f4e922SSubhransu S. Prusty 	if (!kc)
83879f4e922SSubhransu S. Prusty 		return -ENOMEM;
83918382eadSSubhransu S. Prusty 
84079f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
84179f4e922SSubhransu S. Prusty 	if (!se)
84279f4e922SSubhransu S. Prusty 		return -ENOMEM;
84318382eadSSubhransu S. Prusty 
84479f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
84579f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
84679f4e922SSubhransu S. Prusty 	if (!kc->name)
84779f4e922SSubhransu S. Prusty 		return -ENOMEM;
84818382eadSSubhransu S. Prusty 
84979f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
85079f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
85179f4e922SSubhransu S. Prusty 	kc->access = 0;
85279f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
8534a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
85479f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
85579f4e922SSubhransu S. Prusty 
85679f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
85779f4e922SSubhransu S. Prusty 
85879f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
85979f4e922SSubhransu S. Prusty 	se->items = num_items;
86079f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
86179f4e922SSubhransu S. Prusty 
86279f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
86379f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
86479f4e922SSubhransu S. Prusty 	if (!items[i])
86579f4e922SSubhransu S. Prusty 		return -ENOMEM;
86679f4e922SSubhransu S. Prusty 
86779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
86879f4e922SSubhransu S. Prusty 		i++;
86979f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
87079f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
87179f4e922SSubhransu S. Prusty 		if (!items[i])
87279f4e922SSubhransu S. Prusty 			return -ENOMEM;
87379f4e922SSubhransu S. Prusty 	}
87479f4e922SSubhransu S. Prusty 
87579f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
87679f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
87779f4e922SSubhransu S. Prusty 	if (!se->texts)
87879f4e922SSubhransu S. Prusty 		return -ENOMEM;
87979f4e922SSubhransu S. Prusty 
88079f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
8814a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
88279f4e922SSubhransu S. Prusty }
88379f4e922SSubhransu S. Prusty 
88479f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
88579f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
88679f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
88779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
88879f4e922SSubhransu S. Prusty {
88979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
89079f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
89179f4e922SSubhransu S. Prusty 	struct soc_enum *se;
89279f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
89379f4e922SSubhransu S. Prusty 	int i, j;
89479f4e922SSubhransu S. Prusty 
89579f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
89679f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
89779f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
89879f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
89979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
90079f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
90179f4e922SSubhransu S. Prusty 					se->texts[j + 1],
90279f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
90379f4e922SSubhransu S. Prusty 
90479f4e922SSubhransu S. Prusty 			rindex++;
90579f4e922SSubhransu S. Prusty 		}
90679f4e922SSubhransu S. Prusty 
90779f4e922SSubhransu S. Prusty 		mux_index++;
90879f4e922SSubhransu S. Prusty 	}
90979f4e922SSubhransu S. Prusty }
91079f4e922SSubhransu S. Prusty 
91179f4e922SSubhransu S. Prusty /*
91279f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
91379f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
91479f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
91579f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
91679f4e922SSubhransu S. Prusty  *
91779f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
91879f4e922SSubhransu S. Prusty  *
91979f4e922SSubhransu S. Prusty  * Routes are added as below:
92079f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
92179f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
92279f4e922SSubhransu S. Prusty  *
92379f4e922SSubhransu S. Prusty  * Total route elements:
92479f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
92579f4e922SSubhransu S. Prusty  */
92679f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
92779f4e922SSubhransu S. Prusty {
92879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
92979f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
93079f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
93179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
93279f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
93379f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
93479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
93579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
93679f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
93779f4e922SSubhransu S. Prusty 
93879f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
93979f4e922SSubhransu S. Prusty 		return -EINVAL;
94079f4e922SSubhransu S. Prusty 
94179f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
94279f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
94379f4e922SSubhransu S. Prusty 		GFP_KERNEL);
94479f4e922SSubhransu S. Prusty 
94579f4e922SSubhransu S. Prusty 	if (!widgets)
94679f4e922SSubhransu S. Prusty 		return -ENOMEM;
94779f4e922SSubhransu S. Prusty 
94879f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
94979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
95079f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
95179f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
95279f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
95379f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
95479f4e922SSubhransu S. Prusty 		if (ret < 0)
95579f4e922SSubhransu S. Prusty 			return ret;
95679f4e922SSubhransu S. Prusty 		i++;
95779f4e922SSubhransu S. Prusty 	}
95879f4e922SSubhransu S. Prusty 
95979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
96079f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
96179f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
96279f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
96379f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
96479f4e922SSubhransu S. Prusty 		if (ret < 0)
96579f4e922SSubhransu S. Prusty 			return ret;
96679f4e922SSubhransu S. Prusty 		i++;
96779f4e922SSubhransu S. Prusty 	}
96879f4e922SSubhransu S. Prusty 
96979f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
97079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
97179f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
97279f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
97379f4e922SSubhransu S. Prusty 							widget_name);
97479f4e922SSubhransu S. Prusty 		if (ret < 0)
97579f4e922SSubhransu S. Prusty 			return ret;
97679f4e922SSubhransu S. Prusty 		i++;
97779f4e922SSubhransu S. Prusty 
97879f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
97979f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
98079f4e922SSubhransu S. Prusty 
98179f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
98279f4e922SSubhransu S. Prusty 		num_routes++;
98379f4e922SSubhransu S. Prusty 	}
98479f4e922SSubhransu S. Prusty 
98579f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
98679f4e922SSubhransu S. Prusty 							GFP_KERNEL);
98779f4e922SSubhransu S. Prusty 	if (!route)
98879f4e922SSubhransu S. Prusty 		return -ENOMEM;
98979f4e922SSubhransu S. Prusty 
99079f4e922SSubhransu S. Prusty 	i = 0;
99179f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
99279f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
99379f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
99479f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
99579f4e922SSubhransu S. Prusty 
99679f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
99779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
99879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
99979f4e922SSubhransu S. Prusty 		i++;
100079f4e922SSubhransu S. Prusty 
100179f4e922SSubhransu S. Prusty 	}
100279f4e922SSubhransu S. Prusty 
100379f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
100479f4e922SSubhransu S. Prusty 
100579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
100679f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
100779f4e922SSubhransu S. Prusty 
100879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
100979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
101079f4e922SSubhransu S. Prusty 
101179f4e922SSubhransu S. Prusty 	return 0;
101279f4e922SSubhransu S. Prusty 
101318382eadSSubhransu S. Prusty }
101418382eadSSubhransu S. Prusty 
101515b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
101618382eadSSubhransu S. Prusty {
101715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1018148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
101915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1020148569fdSSubhransu S. Prusty 	int dai_id = 0;
102118382eadSSubhransu S. Prusty 
1022148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
102315b91447SSubhransu S. Prusty 		return -EINVAL;
102418382eadSSubhransu S. Prusty 
1025148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1026148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1027148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
102815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
102918382eadSSubhransu S. Prusty 
1030148569fdSSubhransu S. Prusty 		dai_id++;
1031148569fdSSubhransu S. Prusty 
1032148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1033148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1034148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1035148569fdSSubhransu S. Prusty 			break;
1036148569fdSSubhransu S. Prusty 		}
1037148569fdSSubhransu S. Prusty 	}
103818382eadSSubhransu S. Prusty 
103915b91447SSubhransu S. Prusty 	return 0;
104015b91447SSubhransu S. Prusty }
104115b91447SSubhransu S. Prusty 
104215b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
104315b91447SSubhransu S. Prusty {
104415b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
104515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
10464a3478deSJeeja KP 	char name[NAME_SIZE];
104715b91447SSubhransu S. Prusty 
104815b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
104915b91447SSubhransu S. Prusty 	if (!cvt)
105015b91447SSubhransu S. Prusty 		return -ENOMEM;
105115b91447SSubhransu S. Prusty 
105215b91447SSubhransu S. Prusty 	cvt->nid = nid;
10534a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
10544a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
105515b91447SSubhransu S. Prusty 
105615b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
105715b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
105815b91447SSubhransu S. Prusty 
105915b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
106015b91447SSubhransu S. Prusty }
106115b91447SSubhransu S. Prusty 
1062b7756edeSSubhransu S. Prusty static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1063b7756edeSSubhransu S. Prusty 			struct hdac_hdmi_pin *pin)
1064b7756edeSSubhransu S. Prusty {
1065b7756edeSSubhransu S. Prusty 	pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1066b7756edeSSubhransu S. Prusty }
1067b7756edeSSubhransu S. Prusty 
1068b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1069b8a54545SSubhransu S. Prusty {
1070b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
10714a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
10724a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1073b8a54545SSubhransu S. Prusty 	int val;
1074b8a54545SSubhransu S. Prusty 
1075b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
1076b8a54545SSubhransu S. Prusty 
1077b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
1078b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1079b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
1080b8a54545SSubhransu S. Prusty 
1081b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1082b8a54545SSubhransu S. Prusty 						val, pin->nid);
1083b8a54545SSubhransu S. Prusty 
10844a3478deSJeeja KP 
10854a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1086b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1087b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1088b8a54545SSubhransu S. Prusty 
10894a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
10904a3478deSJeeja KP 
1091b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1092b8a54545SSubhransu S. Prusty 
1093b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1094b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
10954a3478deSJeeja KP 
10964a3478deSJeeja KP 		/*
10974a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
10984a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
10994a3478deSJeeja KP 		 * control select.
11004a3478deSJeeja KP 		 */
11014a3478deSJeeja KP 		if (pcm) {
11024a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
11034a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
11044a3478deSJeeja KP 
11054a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
11064a3478deSJeeja KP 		}
11074a3478deSJeeja KP 
11084a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1109b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
1110b8a54545SSubhransu S. Prusty 	}
1111b8a54545SSubhransu S. Prusty 
1112b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
1113b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
1114b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1115b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
1116b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
1117b8a54545SSubhransu S. Prusty 
11184a3478deSJeeja KP 			if (pcm) {
11194a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11204a3478deSJeeja KP 					"jack report for pcm=%d\n",
11214a3478deSJeeja KP 					pcm->pcm_id);
11224a3478deSJeeja KP 
11234a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
11244a3478deSJeeja KP 			}
1125b7756edeSSubhransu S. Prusty 			hdac_hdmi_parse_eld(edev, pin);
11264a3478deSJeeja KP 
1127b8a54545SSubhransu S. Prusty 			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1128b8a54545SSubhransu S. Prusty 					pin->eld.eld_buffer, pin->eld.eld_size);
1129b8a54545SSubhransu S. Prusty 		} else {
1130b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
1131b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
11324a3478deSJeeja KP 
11334a3478deSJeeja KP 			if (pcm) {
11344a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
11354a3478deSJeeja KP 					"jack report for pcm=%d\n",
11364a3478deSJeeja KP 					pcm->pcm_id);
11374a3478deSJeeja KP 
11384a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
1139b8a54545SSubhransu S. Prusty 			}
1140b8a54545SSubhransu S. Prusty 		}
11414a3478deSJeeja KP 	}
11424a3478deSJeeja KP 
11434a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
1144b8a54545SSubhransu S. Prusty 
1145b8a54545SSubhransu S. Prusty 	/*
1146b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
1147b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
1148b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
1149b8a54545SSubhransu S. Prusty 	 */
1150b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1151b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1152b8a54545SSubhransu S. Prusty 
1153b8a54545SSubhransu S. Prusty put_hdac_device:
1154b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1155b8a54545SSubhransu S. Prusty }
1156b8a54545SSubhransu S. Prusty 
1157b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1158b8a54545SSubhransu S. Prusty {
1159b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1160b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1161b8a54545SSubhransu S. Prusty 
1162b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1163b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1164b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1165b8a54545SSubhransu S. Prusty 
1166b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1167b8a54545SSubhransu S. Prusty }
1168b8a54545SSubhransu S. Prusty 
116915b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
117015b91447SSubhransu S. Prusty {
117115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
117215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
117315b91447SSubhransu S. Prusty 
117415b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
117515b91447SSubhransu S. Prusty 	if (!pin)
117615b91447SSubhransu S. Prusty 		return -ENOMEM;
117715b91447SSubhransu S. Prusty 
117815b91447SSubhransu S. Prusty 	pin->nid = nid;
117915b91447SSubhransu S. Prusty 
118015b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
118115b91447SSubhransu S. Prusty 	hdmi->num_pin++;
118215b91447SSubhransu S. Prusty 
1183b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1184bcced704SSubhransu S. Prusty 	mutex_init(&pin->lock);
1185b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1186b8a54545SSubhransu S. Prusty 
118715b91447SSubhransu S. Prusty 	return 0;
118818382eadSSubhransu S. Prusty }
118918382eadSSubhransu S. Prusty 
1190211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1191211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1192211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1193211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1194211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1195211caab7SSubhransu S. Prusty 
1196211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1197211caab7SSubhransu S. Prusty {
1198211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1199211caab7SSubhransu S. Prusty 
1200211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1201211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1202211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1203211caab7SSubhransu S. Prusty 		return;
1204211caab7SSubhransu S. Prusty 
1205211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1206211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1207211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1208211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1209211caab7SSubhransu S. Prusty 		return;
1210211caab7SSubhransu S. Prusty }
1211211caab7SSubhransu S. Prusty 
1212211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1213211caab7SSubhransu S. Prusty {
1214211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1215211caab7SSubhransu S. Prusty 
1216211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1217211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1218211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1219211caab7SSubhransu S. Prusty 		return;
1220211caab7SSubhransu S. Prusty 
1221211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1222211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1223211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1224211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1225211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1226211caab7SSubhransu S. Prusty 		return;
1227211caab7SSubhransu S. Prusty 
1228211caab7SSubhransu S. Prusty }
1229211caab7SSubhransu S. Prusty 
123017a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
123117a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
123217a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
123317a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
123417a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
1235571d5078SJeeja KP 	.trigger = hdac_hdmi_trigger,
123617a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
123717a42c45SSubhransu S. Prusty };
123817a42c45SSubhransu S. Prusty 
123917a42c45SSubhransu S. Prusty /*
124017a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
124117a42c45SSubhransu S. Prusty  * based on the number of converter queried.
124217a42c45SSubhransu S. Prusty  */
124317a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
124417a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
124517a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
124617a42c45SSubhransu S. Prusty {
124717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
124817a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
124917a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
125017a42c45SSubhransu S. Prusty 	int i = 0;
125117a42c45SSubhransu S. Prusty 	u32 rates, bps;
125217a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
125317a42c45SSubhransu S. Prusty 	u64 formats;
125417a42c45SSubhransu S. Prusty 	int ret;
125517a42c45SSubhransu S. Prusty 
125617a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
125717a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
125817a42c45SSubhransu S. Prusty 			GFP_KERNEL);
125917a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
126017a42c45SSubhransu S. Prusty 		return -ENOMEM;
126117a42c45SSubhransu S. Prusty 
126217a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
126317a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
126417a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
126517a42c45SSubhransu S. Prusty 		if (ret)
126617a42c45SSubhransu S. Prusty 			return ret;
126717a42c45SSubhransu S. Prusty 
126817a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
126917a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
127017a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
127117a42c45SSubhransu S. Prusty 
127217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
127317a42c45SSubhransu S. Prusty 			return -ENOMEM;
127417a42c45SSubhransu S. Prusty 
127517a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
127617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
127717a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
127817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
127917a42c45SSubhransu S. Prusty 			return -ENOMEM;
128017a42c45SSubhransu S. Prusty 
128117a42c45SSubhransu S. Prusty 		/*
128217a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
128317a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
128417a42c45SSubhransu S. Prusty 		 */
128517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
128617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
128717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
128817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
128917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
129017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
129117a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
129217a42c45SSubhransu S. Prusty 
129317a42c45SSubhransu S. Prusty 		i++;
129417a42c45SSubhransu S. Prusty 	}
129517a42c45SSubhransu S. Prusty 
129617a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
129717a42c45SSubhransu S. Prusty 
129817a42c45SSubhransu S. Prusty 	return 0;
129917a42c45SSubhransu S. Prusty }
130017a42c45SSubhransu S. Prusty 
130118382eadSSubhransu S. Prusty /*
130218382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
130318382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
130418382eadSSubhransu S. Prusty  */
130517a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
130617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
130718382eadSSubhransu S. Prusty {
130818382eadSSubhransu S. Prusty 	hda_nid_t nid;
13093c83ac23SSudip Mukherjee 	int i, num_nodes;
131018382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
131118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
131215b91447SSubhransu S. Prusty 	int ret;
131318382eadSSubhransu S. Prusty 
1314211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1315211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1316211caab7SSubhransu S. Prusty 
13173c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1318541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
131918382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
132018382eadSSubhransu S. Prusty 		return -EINVAL;
132118382eadSSubhransu S. Prusty 	}
132218382eadSSubhransu S. Prusty 
13233c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
132418382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
132518382eadSSubhransu S. Prusty 
132618382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
132718382eadSSubhransu S. Prusty 		unsigned int caps;
132818382eadSSubhransu S. Prusty 		unsigned int type;
132918382eadSSubhransu S. Prusty 
133018382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
133118382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
133218382eadSSubhransu S. Prusty 
133318382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
133418382eadSSubhransu S. Prusty 			continue;
133518382eadSSubhransu S. Prusty 
133618382eadSSubhransu S. Prusty 		switch (type) {
133718382eadSSubhransu S. Prusty 
133818382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
133915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
134015b91447SSubhransu S. Prusty 			if (ret < 0)
134115b91447SSubhransu S. Prusty 				return ret;
134218382eadSSubhransu S. Prusty 			break;
134318382eadSSubhransu S. Prusty 
134418382eadSSubhransu S. Prusty 		case AC_WID_PIN:
134515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
134615b91447SSubhransu S. Prusty 			if (ret < 0)
134715b91447SSubhransu S. Prusty 				return ret;
134818382eadSSubhransu S. Prusty 			break;
134918382eadSSubhransu S. Prusty 		}
135018382eadSSubhransu S. Prusty 	}
135118382eadSSubhransu S. Prusty 
135218382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
135318382eadSSubhransu S. Prusty 
135415b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
135518382eadSSubhransu S. Prusty 		return -EIO;
135618382eadSSubhransu S. Prusty 
135717a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
135817a42c45SSubhransu S. Prusty 	if (ret) {
135917a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
136017a42c45SSubhransu S. Prusty 							ret);
136117a42c45SSubhransu S. Prusty 		return ret;
136217a42c45SSubhransu S. Prusty 	}
136317a42c45SSubhransu S. Prusty 
136417a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
136517a42c45SSubhransu S. Prusty 
136615b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
136718382eadSSubhransu S. Prusty }
136818382eadSSubhransu S. Prusty 
1369b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1370b8a54545SSubhransu S. Prusty {
1371b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1372b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1373b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1374b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1375b8a54545SSubhransu S. Prusty 
1376b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1377b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1378b8a54545SSubhransu S. Prusty 
1379b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1380b8a54545SSubhransu S. Prusty 
1381b8a54545SSubhransu S. Prusty 	/*
1382b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1383b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1384b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1385b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1386b8a54545SSubhransu S. Prusty 	 */
1387b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1388b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1389b8a54545SSubhransu S. Prusty 		return;
1390b8a54545SSubhransu S. Prusty 
1391b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1392b8a54545SSubhransu S. Prusty 		return;
1393b8a54545SSubhransu S. Prusty 
1394b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1395b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1396b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1397b8a54545SSubhransu S. Prusty 	}
1398b8a54545SSubhransu S. Prusty }
1399b8a54545SSubhransu S. Prusty 
1400b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1401b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1402b8a54545SSubhransu S. Prusty };
1403b8a54545SSubhransu S. Prusty 
14042889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
14052889099eSSubhransu S. Prusty 						int device)
14062889099eSSubhransu S. Prusty {
14072889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
14082889099eSSubhransu S. Prusty 
14092889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
14102889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
14112889099eSSubhransu S. Prusty 			return rtd->pcm;
14122889099eSSubhransu S. Prusty 	}
14132889099eSSubhransu S. Prusty 
14142889099eSSubhransu S. Prusty 	return NULL;
14152889099eSSubhransu S. Prusty }
14162889099eSSubhransu S. Prusty 
14174a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
14184a3478deSJeeja KP {
14194a3478deSJeeja KP 	char jack_name[NAME_SIZE];
14204a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
14214a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
14224a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
14234a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
14244a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
14254a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
14262889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
14272889099eSSubhransu S. Prusty 	int err;
14284a3478deSJeeja KP 
14294a3478deSJeeja KP 	/*
14304a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
14314a3478deSJeeja KP 	 * add to the pcm list
14324a3478deSJeeja KP 	 */
14334a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
14344a3478deSJeeja KP 	if (!pcm)
14354a3478deSJeeja KP 		return -ENOMEM;
14364a3478deSJeeja KP 	pcm->pcm_id = device;
14374a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
14384a3478deSJeeja KP 
14392889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
14402889099eSSubhransu S. Prusty 	if (snd_pcm) {
14412889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
14422889099eSSubhransu S. Prusty 		if (err < 0) {
14432889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
14442889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
14452889099eSSubhransu S. Prusty 				err, device);
14462889099eSSubhransu S. Prusty 			kfree(pcm);
14472889099eSSubhransu S. Prusty 			return err;
14482889099eSSubhransu S. Prusty 		}
14492889099eSSubhransu S. Prusty 	}
14502889099eSSubhransu S. Prusty 
14514a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
14524a3478deSJeeja KP 
14534a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
14544a3478deSJeeja KP 
14554a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
14564a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
14574a3478deSJeeja KP }
14584a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
14594a3478deSJeeja KP 
146018382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
146118382eadSSubhransu S. Prusty {
146218382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
146318382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
146418382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
146518382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1466b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1467b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1468b8a54545SSubhransu S. Prusty 	int ret;
146918382eadSSubhransu S. Prusty 
147018382eadSSubhransu S. Prusty 	edev->scodec = codec;
147118382eadSSubhransu S. Prusty 
1472b2047e99SVinod Koul 	/*
1473b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1474b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1475b2047e99SVinod Koul 	 */
1476b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1477500e06b9SVinod Koul 	if (!hlink) {
1478500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1479500e06b9SVinod Koul 		return -EIO;
1480500e06b9SVinod Koul 	}
1481500e06b9SVinod Koul 
1482b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1483b2047e99SVinod Koul 
148479f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
148579f4e922SSubhransu S. Prusty 	if (ret < 0)
148679f4e922SSubhransu S. Prusty 		return ret;
148718382eadSSubhransu S. Prusty 
1488b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1489b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1490b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1491b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1492b8a54545SSubhransu S. Prusty 				ret);
1493b8a54545SSubhransu S. Prusty 		return ret;
1494b8a54545SSubhransu S. Prusty 	}
1495b8a54545SSubhransu S. Prusty 
1496b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1497b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1498b8a54545SSubhransu S. Prusty 
149918382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
150018382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
150118382eadSSubhransu S. Prusty 
1502e342ac08SSubhransu S. Prusty 	/*
1503e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1504e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1505e342ac08SSubhransu S. Prusty 	 */
1506e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1507e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1508e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1509e342ac08SSubhransu S. Prusty 
1510e342ac08SSubhransu S. Prusty 	return 0;
1511e342ac08SSubhransu S. Prusty }
1512e342ac08SSubhransu S. Prusty 
1513e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1514e342ac08SSubhransu S. Prusty {
1515e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1516e342ac08SSubhransu S. Prusty 
1517e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
151818382eadSSubhransu S. Prusty 	return 0;
151918382eadSSubhransu S. Prusty }
152018382eadSSubhransu S. Prusty 
1521571d5078SJeeja KP #ifdef CONFIG_PM
15221b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
15231b377ccdSSubhransu S. Prusty {
15241b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
15251b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
15261b377ccdSSubhransu S. Prusty 
15271b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
15281b377ccdSSubhransu S. Prusty 
15291b377ccdSSubhransu S. Prusty 	/*
15301b377ccdSSubhransu S. Prusty 	 * Power down afg.
15311b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
15321b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
15331b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
15341b377ccdSSubhransu S. Prusty 	 * to read the state.
15351b377ccdSSubhransu S. Prusty 	 */
15361b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15371b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
15381b377ccdSSubhransu S. Prusty 
15391b377ccdSSubhransu S. Prusty 	return 0;
15401b377ccdSSubhransu S. Prusty }
15411b377ccdSSubhransu S. Prusty 
15420fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1543571d5078SJeeja KP {
15440fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1545571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1546571d5078SJeeja KP 	struct hdac_hdmi_pin *pin;
1547571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
15481b377ccdSSubhransu S. Prusty 
15491b377ccdSSubhransu S. Prusty 	/* Power up afg */
15501b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
15511b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1552571d5078SJeeja KP 
1553571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1554571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1555571d5078SJeeja KP 
1556571d5078SJeeja KP 	/*
1557571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1558571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1559571d5078SJeeja KP 	 * all pins here.
1560571d5078SJeeja KP 	 */
1561571d5078SJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head)
1562571d5078SJeeja KP 		hdac_hdmi_present_sense(pin, 1);
1563571d5078SJeeja KP 
15641b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1565571d5078SJeeja KP }
1566571d5078SJeeja KP #else
15671b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
15680fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1569571d5078SJeeja KP #endif
1570571d5078SJeeja KP 
157118382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
157218382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1573e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
157418382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
157518382eadSSubhransu S. Prusty };
157618382eadSSubhransu S. Prusty 
15772889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
15782889099eSSubhransu S. Prusty 					unsigned char *chmap)
15792889099eSSubhransu S. Prusty {
15802889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15812889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15822889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
15832889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
15842889099eSSubhransu S. Prusty 
15852889099eSSubhransu S. Prusty 	/* chmap is already set to 0 in caller */
15862889099eSSubhransu S. Prusty 	if (!pin)
15872889099eSSubhransu S. Prusty 		return;
15882889099eSSubhransu S. Prusty 
15892889099eSSubhransu S. Prusty 	memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
15902889099eSSubhransu S. Prusty }
15912889099eSSubhransu S. Prusty 
15922889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
15932889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
15942889099eSSubhransu S. Prusty {
15952889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
15962889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15972889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
15982889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
15992889099eSSubhransu S. Prusty 
16002889099eSSubhransu S. Prusty 	mutex_lock(&pin->lock);
16012889099eSSubhransu S. Prusty 	pin->chmap_set = true;
16022889099eSSubhransu S. Prusty 	memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
16032889099eSSubhransu S. Prusty 	if (prepared)
16042889099eSSubhransu S. Prusty 		hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
16052889099eSSubhransu S. Prusty 	mutex_unlock(&pin->lock);
16062889099eSSubhransu S. Prusty }
16072889099eSSubhransu S. Prusty 
16082889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
16092889099eSSubhransu S. Prusty {
16102889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16112889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16122889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
16132889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
16142889099eSSubhransu S. Prusty 
16152889099eSSubhransu S. Prusty 	return pin ? true:false;
16162889099eSSubhransu S. Prusty }
16172889099eSSubhransu S. Prusty 
16182889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
16192889099eSSubhransu S. Prusty {
16202889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
16212889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16222889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
16232889099eSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = pcm->pin;
16242889099eSSubhransu S. Prusty 
16258f658815SDan Carpenter 	if (!pin || !pin->eld.eld_valid)
16262889099eSSubhransu S. Prusty 		return 0;
16272889099eSSubhransu S. Prusty 
16282889099eSSubhransu S. Prusty 	return pin->eld.info.spk_alloc;
16292889099eSSubhransu S. Prusty }
16302889099eSSubhransu S. Prusty 
163118382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
163218382eadSSubhransu S. Prusty {
163318382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
163418382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
163517a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1636b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
163717a42c45SSubhransu S. Prusty 	int num_dais = 0;
163818382eadSSubhransu S. Prusty 	int ret = 0;
163918382eadSSubhransu S. Prusty 
1640b2047e99SVinod Koul 	/* hold the ref while we probe */
1641b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1642500e06b9SVinod Koul 	if (!hlink) {
1643500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1644500e06b9SVinod Koul 		return -EIO;
1645500e06b9SVinod Koul 	}
1646500e06b9SVinod Koul 
1647b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1648b2047e99SVinod Koul 
164918382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
165018382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
165118382eadSSubhransu S. Prusty 		return -ENOMEM;
165218382eadSSubhransu S. Prusty 
165318382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1654bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
16552889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
16562889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
16572889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
16582889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
165918382eadSSubhransu S. Prusty 
166018382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
166118382eadSSubhransu S. Prusty 
166215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
166315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
16644a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
16654a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
166615b91447SSubhransu S. Prusty 
1667aeaccef0SRamesh Babu 	/*
1668aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1669aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1670aeaccef0SRamesh Babu 	 */
1671aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1672aeaccef0SRamesh Babu 	if (ret < 0) {
1673aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1674aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1675aeaccef0SRamesh Babu 			ret);
1676aeaccef0SRamesh Babu 		return ret;
1677aeaccef0SRamesh Babu 	}
1678aeaccef0SRamesh Babu 
167917a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
168017a42c45SSubhransu S. Prusty 	if (ret < 0) {
168117a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
168217a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
168318382eadSSubhransu S. Prusty 		return ret;
168417a42c45SSubhransu S. Prusty 	}
168518382eadSSubhransu S. Prusty 
168618382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1687b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
168817a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1689b2047e99SVinod Koul 
1690b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1691b2047e99SVinod Koul 
1692b2047e99SVinod Koul 	return ret;
169318382eadSSubhransu S. Prusty }
169418382eadSSubhransu S. Prusty 
169518382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
169618382eadSSubhransu S. Prusty {
169715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
169815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
169915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
17004a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
170115b91447SSubhransu S. Prusty 
170218382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
170318382eadSSubhransu S. Prusty 
17044a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
17054a3478deSJeeja KP 		pcm->cvt = NULL;
17064a3478deSJeeja KP 		pcm->pin = NULL;
17074a3478deSJeeja KP 		list_del(&pcm->head);
17084a3478deSJeeja KP 		kfree(pcm);
17094a3478deSJeeja KP 	}
17104a3478deSJeeja KP 
171115b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
171215b91447SSubhransu S. Prusty 		list_del(&cvt->head);
17134a3478deSJeeja KP 		kfree(cvt->name);
171415b91447SSubhransu S. Prusty 		kfree(cvt);
171515b91447SSubhransu S. Prusty 	}
171615b91447SSubhransu S. Prusty 
171715b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
171815b91447SSubhransu S. Prusty 		list_del(&pin->head);
171915b91447SSubhransu S. Prusty 		kfree(pin);
172015b91447SSubhransu S. Prusty 	}
172115b91447SSubhransu S. Prusty 
172218382eadSSubhransu S. Prusty 	return 0;
172318382eadSSubhransu S. Prusty }
172418382eadSSubhransu S. Prusty 
1725e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1726e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1727e342ac08SSubhransu S. Prusty {
1728e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1729e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
173007f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1731b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1732b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
173307f083abSSubhransu S. Prusty 	int err;
1734e342ac08SSubhransu S. Prusty 
1735e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1736e342ac08SSubhransu S. Prusty 
173707f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
173807f083abSSubhransu S. Prusty 	if (!bus)
173907f083abSSubhransu S. Prusty 		return 0;
174007f083abSSubhransu S. Prusty 
17411b377ccdSSubhransu S. Prusty 	/*
17421b377ccdSSubhransu S. Prusty 	 * Power down afg.
17431b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
17441b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
17451b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
17461b377ccdSSubhransu S. Prusty 	 * to read the state.
17471b377ccdSSubhransu S. Prusty 	 */
17481b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17491b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
175007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
175107f083abSSubhransu S. Prusty 	if (err < 0) {
175207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
175307f083abSSubhransu S. Prusty 		return err;
175407f083abSSubhransu S. Prusty 	}
175507f083abSSubhransu S. Prusty 
1756b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1757500e06b9SVinod Koul 	if (!hlink) {
1758500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1759500e06b9SVinod Koul 		return -EIO;
1760500e06b9SVinod Koul 	}
1761500e06b9SVinod Koul 
1762b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1763b2047e99SVinod Koul 
1764e342ac08SSubhransu S. Prusty 	return 0;
1765e342ac08SSubhransu S. Prusty }
1766e342ac08SSubhransu S. Prusty 
1767e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1768e342ac08SSubhransu S. Prusty {
1769e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1770e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
177107f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1772b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1773b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
177407f083abSSubhransu S. Prusty 	int err;
1775e342ac08SSubhransu S. Prusty 
1776e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1777e342ac08SSubhransu S. Prusty 
177807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
177907f083abSSubhransu S. Prusty 	if (!bus)
178007f083abSSubhransu S. Prusty 		return 0;
178107f083abSSubhransu S. Prusty 
1782b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1783500e06b9SVinod Koul 	if (!hlink) {
1784500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1785500e06b9SVinod Koul 		return -EIO;
1786500e06b9SVinod Koul 	}
1787500e06b9SVinod Koul 
1788b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1789b2047e99SVinod Koul 
179007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
179107f083abSSubhransu S. Prusty 	if (err < 0) {
179207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
179307f083abSSubhransu S. Prusty 		return err;
179407f083abSSubhransu S. Prusty 	}
179507f083abSSubhransu S. Prusty 
1796ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1797ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1798ab85f5b3SSubhransu S. Prusty 
1799e342ac08SSubhransu S. Prusty 	/* Power up afg */
18001b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18011b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1802e342ac08SSubhransu S. Prusty 
1803e342ac08SSubhransu S. Prusty 	return 0;
1804e342ac08SSubhransu S. Prusty }
1805e342ac08SSubhransu S. Prusty #else
1806e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1807e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1808e342ac08SSubhransu S. Prusty #endif
1809e342ac08SSubhransu S. Prusty 
1810e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1811e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
18121b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
18130fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1814e342ac08SSubhransu S. Prusty };
1815e342ac08SSubhransu S. Prusty 
181618382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
181718382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1818e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
181918382eadSSubhransu S. Prusty 	{}
182018382eadSSubhransu S. Prusty };
182118382eadSSubhransu S. Prusty 
182218382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
182318382eadSSubhransu S. Prusty 
182418382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
182518382eadSSubhransu S. Prusty 	. hdac = {
182618382eadSSubhransu S. Prusty 		.driver = {
182718382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1828e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
182918382eadSSubhransu S. Prusty 		},
183018382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
183118382eadSSubhransu S. Prusty 	},
183218382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
183318382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
183418382eadSSubhransu S. Prusty };
183518382eadSSubhransu S. Prusty 
183618382eadSSubhransu S. Prusty static int __init hdmi_init(void)
183718382eadSSubhransu S. Prusty {
183818382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
183918382eadSSubhransu S. Prusty }
184018382eadSSubhransu S. Prusty 
184118382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
184218382eadSSubhransu S. Prusty {
184318382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
184418382eadSSubhransu S. Prusty }
184518382eadSSubhransu S. Prusty 
184618382eadSSubhransu S. Prusty module_init(hdmi_init);
184718382eadSSubhransu S. Prusty module_exit(hdmi_exit);
184818382eadSSubhransu S. Prusty 
184918382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
185018382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
185118382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
185218382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1853