xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 148569fddbc6d3ce97236c4344068b1293e51838)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
3218382eadSSubhransu S. Prusty #include "../../hda/local.h"
334a3478deSJeeja KP #include "hdac_hdmi.h"
3418382eadSSubhransu S. Prusty 
3517a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3617a42c45SSubhransu S. Prusty 
37b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
38b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
3918382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
40b0362adbSSubhransu S. Prusty 
4118382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4218382eadSSubhransu S. Prusty 
43*148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
44*148569fdSSubhransu S. Prusty 
45b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
46b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
47b8a54545SSubhransu S. Prusty 
4818382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
4918382eadSSubhransu S. Prusty 	unsigned int channels_min;
5018382eadSSubhransu S. Prusty 	unsigned int channels_max;
5118382eadSSubhransu S. Prusty 	u32 rates;
5218382eadSSubhransu S. Prusty 	u64 formats;
5318382eadSSubhransu S. Prusty 	unsigned int maxbps;
5418382eadSSubhransu S. Prusty };
5518382eadSSubhransu S. Prusty 
5618382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5715b91447SSubhransu S. Prusty 	struct list_head head;
5818382eadSSubhransu S. Prusty 	hda_nid_t nid;
594a3478deSJeeja KP 	const char *name;
6018382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6118382eadSSubhransu S. Prusty };
6218382eadSSubhransu S. Prusty 
63b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
64b8a54545SSubhransu S. Prusty 	bool	monitor_present;
65b8a54545SSubhransu S. Prusty 	bool	eld_valid;
66b8a54545SSubhransu S. Prusty 	int	eld_size;
67b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
68b8a54545SSubhransu S. Prusty };
69b8a54545SSubhransu S. Prusty 
7018382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7115b91447SSubhransu S. Prusty 	struct list_head head;
7218382eadSSubhransu S. Prusty 	hda_nid_t nid;
7318382eadSSubhransu S. Prusty 	int num_mux_nids;
7418382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
76b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev;
77b8a54545SSubhransu S. Prusty 	int repoll_count;
78b8a54545SSubhransu S. Prusty 	struct delayed_work work;
7918382eadSSubhransu S. Prusty };
8018382eadSSubhransu S. Prusty 
814a3478deSJeeja KP struct hdac_hdmi_pcm {
824a3478deSJeeja KP 	struct list_head head;
834a3478deSJeeja KP 	int pcm_id;
844a3478deSJeeja KP 	struct hdac_hdmi_pin *pin;
854a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
864a3478deSJeeja KP 	struct snd_jack *jack;
874a3478deSJeeja KP };
884a3478deSJeeja KP 
8918382eadSSubhransu S. Prusty struct hdac_hdmi_dai_pin_map {
9018382eadSSubhransu S. Prusty 	int dai_id;
9115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
9215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
9318382eadSSubhransu S. Prusty };
9418382eadSSubhransu S. Prusty 
9518382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
96*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
9715b91447SSubhransu S. Prusty 	struct list_head pin_list;
9815b91447SSubhransu S. Prusty 	struct list_head cvt_list;
994a3478deSJeeja KP 	struct list_head pcm_list;
10015b91447SSubhransu S. Prusty 	int num_pin;
10115b91447SSubhransu S. Prusty 	int num_cvt;
1024a3478deSJeeja KP 	struct mutex pin_mutex;
10318382eadSSubhransu S. Prusty };
10418382eadSSubhransu S. Prusty 
105e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106e342ac08SSubhransu S. Prusty {
10751b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
108e342ac08SSubhransu S. Prusty 
10951b2c425SGeliang Tang 	return to_ehdac_device(hdac);
110e342ac08SSubhransu S. Prusty }
111e342ac08SSubhransu S. Prusty 
1122428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
1132428bca3SSubhransu S. Prusty {
1142428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
1152428bca3SSubhransu S. Prusty }
1162428bca3SSubhransu S. Prusty 
1172428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
1182428bca3SSubhransu S. Prusty {
1192428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
1202428bca3SSubhransu S. Prusty }
1212428bca3SSubhransu S. Prusty 
1222428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
1232428bca3SSubhransu S. Prusty 						void *eld)
1242428bca3SSubhransu S. Prusty {
1252428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
1262428bca3SSubhransu S. Prusty 	int i;
1272428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
1282428bca3SSubhransu S. Prusty 
1292428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
1302428bca3SSubhransu S. Prusty 	if (!sad)
1312428bca3SSubhransu S. Prusty 		goto format_constraint;
1322428bca3SSubhransu S. Prusty 
1332428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
1342428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
1352428bca3SSubhransu S. Prusty 
1362428bca3SSubhransu S. Prusty 			/*
1372428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
1382428bca3SSubhransu S. Prusty 			 * container so we set S32
1392428bca3SSubhransu S. Prusty 			 */
1402428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
1412428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
1422428bca3SSubhransu S. Prusty 		}
1432428bca3SSubhransu S. Prusty 	}
1442428bca3SSubhransu S. Prusty 
1452428bca3SSubhransu S. Prusty format_constraint:
1462428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
1472428bca3SSubhransu S. Prusty 				formats);
1482428bca3SSubhransu S. Prusty 
1492428bca3SSubhransu S. Prusty }
1502428bca3SSubhransu S. Prusty 
151b8a54545SSubhransu S. Prusty  /* HDMI ELD routines */
152b8a54545SSubhransu S. Prusty static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153b8a54545SSubhransu S. Prusty 				hda_nid_t nid, int byte_index)
154b8a54545SSubhransu S. Prusty {
155b8a54545SSubhransu S. Prusty 	unsigned int val;
156b8a54545SSubhransu S. Prusty 
157b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158b8a54545SSubhransu S. Prusty 							byte_index);
159b8a54545SSubhransu S. Prusty 
160b8a54545SSubhransu S. Prusty 	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161b8a54545SSubhransu S. Prusty 					byte_index, val);
162b8a54545SSubhransu S. Prusty 
163b8a54545SSubhransu S. Prusty 	return val;
164b8a54545SSubhransu S. Prusty }
165b8a54545SSubhransu S. Prusty 
166b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
167b8a54545SSubhransu S. Prusty {
168b8a54545SSubhransu S. Prusty 	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
169b8a54545SSubhransu S. Prusty 						 AC_DIPSIZE_ELD_BUF);
170b8a54545SSubhransu S. Prusty }
171b8a54545SSubhransu S. Prusty 
172b8a54545SSubhransu S. Prusty /*
173b8a54545SSubhransu S. Prusty  * This function queries the ELD size and ELD data and fills in the buffer
174b8a54545SSubhransu S. Prusty  * passed by user
175b8a54545SSubhransu S. Prusty  */
176b8a54545SSubhransu S. Prusty static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177b8a54545SSubhransu S. Prusty 			     unsigned char *buf, int *eld_size)
178b8a54545SSubhransu S. Prusty {
179b8a54545SSubhransu S. Prusty 	int i, size, ret = 0;
180b8a54545SSubhransu S. Prusty 
181b8a54545SSubhransu S. Prusty 	/*
182b8a54545SSubhransu S. Prusty 	 * ELD size is initialized to zero in caller function. If no errors and
183b8a54545SSubhransu S. Prusty 	 * ELD is valid, actual eld_size is assigned.
184b8a54545SSubhransu S. Prusty 	 */
185b8a54545SSubhransu S. Prusty 
186b8a54545SSubhransu S. Prusty 	size = hdac_hdmi_get_eld_size(codec, nid);
187b8a54545SSubhransu S. Prusty 	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
188b8a54545SSubhransu S. Prusty 		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
189b8a54545SSubhransu S. Prusty 		return -ERANGE;
190b8a54545SSubhransu S. Prusty 	}
191b8a54545SSubhransu S. Prusty 
192b8a54545SSubhransu S. Prusty 	/* set ELD buffer */
193b8a54545SSubhransu S. Prusty 	for (i = 0; i < size; i++) {
194b8a54545SSubhransu S. Prusty 		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
195b8a54545SSubhransu S. Prusty 		/*
196b8a54545SSubhransu S. Prusty 		 * Graphics driver might be writing to ELD buffer right now.
197b8a54545SSubhransu S. Prusty 		 * Just abort. The caller will repoll after a while.
198b8a54545SSubhransu S. Prusty 		 */
199b8a54545SSubhransu S. Prusty 		if (!(val & AC_ELDD_ELD_VALID)) {
200b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev,
201b8a54545SSubhransu S. Prusty 				"HDMI: invalid ELD data byte %d\n", i);
202b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
203b8a54545SSubhransu S. Prusty 			goto error;
204b8a54545SSubhransu S. Prusty 		}
205b8a54545SSubhransu S. Prusty 		val &= AC_ELDD_ELD_DATA;
206b8a54545SSubhransu S. Prusty 		/*
207b8a54545SSubhransu S. Prusty 		 * The first byte cannot be zero. This can happen on some DVI
208b8a54545SSubhransu S. Prusty 		 * connections. Some Intel chips may also need some 250ms delay
209b8a54545SSubhransu S. Prusty 		 * to return non-zero ELD data, even when the graphics driver
210b8a54545SSubhransu S. Prusty 		 * correctly writes ELD content before setting ELD_valid bit.
211b8a54545SSubhransu S. Prusty 		 */
212b8a54545SSubhransu S. Prusty 		if (!val && !i) {
213b8a54545SSubhransu S. Prusty 			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214b8a54545SSubhransu S. Prusty 			ret = -EINVAL;
215b8a54545SSubhransu S. Prusty 			goto error;
216b8a54545SSubhransu S. Prusty 		}
217b8a54545SSubhransu S. Prusty 		buf[i] = val;
218b8a54545SSubhransu S. Prusty 	}
219b8a54545SSubhransu S. Prusty 
220b8a54545SSubhransu S. Prusty 	*eld_size = size;
221b8a54545SSubhransu S. Prusty error:
222b8a54545SSubhransu S. Prusty 	return ret;
223b8a54545SSubhransu S. Prusty }
224b8a54545SSubhransu S. Prusty 
225b0362adbSSubhransu S. Prusty static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
226b0362adbSSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid,
227b0362adbSSubhransu S. Prusty 				u32 stream_tag, int format)
228b0362adbSSubhransu S. Prusty {
229b0362adbSSubhransu S. Prusty 	unsigned int val;
230b0362adbSSubhransu S. Prusty 
231b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
232b0362adbSSubhransu S. Prusty 			cvt_nid, pin_nid, stream_tag, format);
233b0362adbSSubhransu S. Prusty 
234b0362adbSSubhransu S. Prusty 	val = (stream_tag << 4);
235b0362adbSSubhransu S. Prusty 
236b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
237b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, val);
238b0362adbSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
239b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, format);
240b0362adbSSubhransu S. Prusty 
241b0362adbSSubhransu S. Prusty 	return 0;
242b0362adbSSubhransu S. Prusty }
243b0362adbSSubhransu S. Prusty 
244a657f1d0SSubhransu S. Prusty static void
245a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
247a657f1d0SSubhransu S. Prusty {
248a657f1d0SSubhransu S. Prusty 	int val;
249a657f1d0SSubhransu S. Prusty 
250a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
251a657f1d0SSubhransu S. Prusty 
252a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
253a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
254a657f1d0SSubhransu S. Prusty }
255a657f1d0SSubhransu S. Prusty 
256a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
257a657f1d0SSubhransu S. Prusty 				hda_nid_t cvt_nid, hda_nid_t pin_nid)
258a657f1d0SSubhransu S. Prusty {
259a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
260a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
261a657f1d0SSubhransu S. Prusty 	u8 *dip = (u8 *)&frame;
262a657f1d0SSubhransu S. Prusty 	int ret;
263a657f1d0SSubhransu S. Prusty 	int i;
264a657f1d0SSubhransu S. Prusty 
265a657f1d0SSubhransu S. Prusty 	hdmi_audio_infoframe_init(&frame);
266a657f1d0SSubhransu S. Prusty 
267a657f1d0SSubhransu S. Prusty 	/* Default stereo for now */
268a657f1d0SSubhransu S. Prusty 	frame.channels = 2;
269a657f1d0SSubhransu S. Prusty 
270a657f1d0SSubhransu S. Prusty 	/* setup channel count */
271a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
272a657f1d0SSubhransu S. Prusty 			    AC_VERB_SET_CVT_CHAN_COUNT, frame.channels - 1);
273a657f1d0SSubhransu S. Prusty 
274a657f1d0SSubhransu S. Prusty 	ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
275a657f1d0SSubhransu S. Prusty 	if (ret < 0)
276a657f1d0SSubhransu S. Prusty 		return ret;
277a657f1d0SSubhransu S. Prusty 
278a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
279a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
280a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
281a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
282a657f1d0SSubhransu S. Prusty 
283a657f1d0SSubhransu S. Prusty 
284a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
285a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
286a657f1d0SSubhransu S. Prusty 	for (i = 0; i < sizeof(frame); i++)
287a657f1d0SSubhransu S. Prusty 		snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
288a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
289a657f1d0SSubhransu S. Prusty 
290a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
291a657f1d0SSubhransu S. Prusty 	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
292a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
293a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
294a657f1d0SSubhransu S. Prusty 
295a657f1d0SSubhransu S. Prusty 	return 0;
296a657f1d0SSubhransu S. Prusty }
297a657f1d0SSubhransu S. Prusty 
298b0362adbSSubhransu S. Prusty static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
299b0362adbSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
300b0362adbSSubhransu S. Prusty {
301b0362adbSSubhransu S. Prusty 	/* Power up pin widget */
30215b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
30315b91447SSubhransu S. Prusty 						pwr_state))
30415b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
305b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
306b0362adbSSubhransu S. Prusty 
307b0362adbSSubhransu S. Prusty 	/* Power up converter */
30815b91447SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
30915b91447SSubhransu S. Prusty 						pwr_state))
31015b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
311b0362adbSSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, pwr_state);
312b0362adbSSubhransu S. Prusty }
313b0362adbSSubhransu S. Prusty 
314b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
315b0362adbSSubhransu S. Prusty 				struct snd_soc_dai *dai)
316b0362adbSSubhransu S. Prusty {
317b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
318b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
319b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
320b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
321a657f1d0SSubhransu S. Prusty 	int ret;
322b0362adbSSubhransu S. Prusty 
323b0362adbSSubhransu S. Prusty 	if (dai->id > 0) {
324b0362adbSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
325b0362adbSSubhransu S. Prusty 		return -ENODEV;
326b0362adbSSubhransu S. Prusty 	}
327b0362adbSSubhransu S. Prusty 
328b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
329b0362adbSSubhransu S. Prusty 
330b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
331b0362adbSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
332b0362adbSSubhransu S. Prusty 			dd->stream_tag,	dd->format);
333b0362adbSSubhransu S. Prusty 
33415b91447SSubhransu S. Prusty 	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
33515b91447SSubhransu S. Prusty 						dai_map->pin->nid);
336a657f1d0SSubhransu S. Prusty 	if (ret < 0)
337a657f1d0SSubhransu S. Prusty 		return ret;
338a657f1d0SSubhransu S. Prusty 
33915b91447SSubhransu S. Prusty 	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
34015b91447SSubhransu S. Prusty 			dai_map->pin->nid, dd->stream_tag, dd->format);
341b0362adbSSubhransu S. Prusty }
342b0362adbSSubhransu S. Prusty 
343b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
344b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
345b0362adbSSubhransu S. Prusty {
346b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
347b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
348b0362adbSSubhransu S. Prusty 
349b0362adbSSubhransu S. Prusty 	if (dai->id > 0) {
350b0362adbSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
351b0362adbSSubhransu S. Prusty 		return -ENODEV;
352b0362adbSSubhransu S. Prusty 	}
353b0362adbSSubhransu S. Prusty 
354b0362adbSSubhransu S. Prusty 	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
3558d33ab24SSudip Mukherjee 	if (!dd)
3568d33ab24SSudip Mukherjee 		return -ENOMEM;
357b0362adbSSubhransu S. Prusty 	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
358b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
359b0362adbSSubhransu S. Prusty 			24, 0);
360b0362adbSSubhransu S. Prusty 
361b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
362b0362adbSSubhransu S. Prusty 
363b0362adbSSubhransu S. Prusty 	return 0;
364b0362adbSSubhransu S. Prusty }
365b0362adbSSubhransu S. Prusty 
366b0362adbSSubhransu S. Prusty static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
367b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
368b0362adbSSubhransu S. Prusty {
369b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
370b0362adbSSubhransu S. Prusty 	struct hdac_ext_dma_params *dd;
371b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
372b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
373b0362adbSSubhransu S. Prusty 
374b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
375b0362adbSSubhransu S. Prusty 
37615b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
377b0362adbSSubhransu S. Prusty 				AC_VERB_SET_CHANNEL_STREAMID, 0);
37815b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
379b0362adbSSubhransu S. Prusty 				AC_VERB_SET_STREAM_FORMAT, 0);
380b0362adbSSubhransu S. Prusty 
381b0362adbSSubhransu S. Prusty 	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
382b0362adbSSubhransu S. Prusty 	snd_soc_dai_set_dma_data(dai, substream, NULL);
383b0362adbSSubhransu S. Prusty 
384b0362adbSSubhransu S. Prusty 	kfree(dd);
385b0362adbSSubhransu S. Prusty 
386b0362adbSSubhransu S. Prusty 	return 0;
387b0362adbSSubhransu S. Prusty }
388b0362adbSSubhransu S. Prusty 
389*148569fdSSubhransu S. Prusty static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
390*148569fdSSubhransu S. Prusty 		struct hdac_hdmi_dai_pin_map *dai_map)
391*148569fdSSubhransu S. Prusty {
392*148569fdSSubhransu S. Prusty 	int mux_idx;
393*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = dai_map->pin;
394*148569fdSSubhransu S. Prusty 
395*148569fdSSubhransu S. Prusty 	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
396*148569fdSSubhransu S. Prusty 		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
397*148569fdSSubhransu S. Prusty 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
398*148569fdSSubhransu S. Prusty 					AC_VERB_SET_CONNECT_SEL, mux_idx);
399*148569fdSSubhransu S. Prusty 			break;
400*148569fdSSubhransu S. Prusty 		}
401*148569fdSSubhransu S. Prusty 	}
402*148569fdSSubhransu S. Prusty 
403*148569fdSSubhransu S. Prusty 	if (mux_idx == pin->num_mux_nids)
404*148569fdSSubhransu S. Prusty 		return -EIO;
405*148569fdSSubhransu S. Prusty 
406*148569fdSSubhransu S. Prusty 	/* Enable out path for this pin widget */
407*148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
408*148569fdSSubhransu S. Prusty 			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
409*148569fdSSubhransu S. Prusty 
410*148569fdSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
411*148569fdSSubhransu S. Prusty 
412*148569fdSSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
413*148569fdSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
414*148569fdSSubhransu S. Prusty 
415*148569fdSSubhransu S. Prusty 	return 0;
416*148569fdSSubhransu S. Prusty }
417*148569fdSSubhransu S. Prusty 
418*148569fdSSubhransu S. Prusty static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
419*148569fdSSubhransu S. Prusty 					struct hdac_hdmi_pin *pin)
420*148569fdSSubhransu S. Prusty {
421*148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
422*148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
423*148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
424*148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
425*148569fdSSubhransu S. Prusty 		return -EINVAL;
426*148569fdSSubhransu S. Prusty 	}
427*148569fdSSubhransu S. Prusty 
428*148569fdSSubhransu S. Prusty 	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
429*148569fdSSubhransu S. Prusty 			pin->mux_nids, HDA_MAX_CONNECTIONS);
430*148569fdSSubhransu S. Prusty 	if (pin->num_mux_nids == 0)
431*148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
432*148569fdSSubhransu S. Prusty 								pin->nid);
433*148569fdSSubhransu S. Prusty 
434*148569fdSSubhransu S. Prusty 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
435*148569fdSSubhransu S. Prusty 			pin->num_mux_nids, pin->nid);
436*148569fdSSubhransu S. Prusty 
437*148569fdSSubhransu S. Prusty 	return pin->num_mux_nids;
438*148569fdSSubhransu S. Prusty }
439*148569fdSSubhransu S. Prusty 
440*148569fdSSubhransu S. Prusty /*
441*148569fdSSubhransu S. Prusty  * Query pcm list and return pin widget to which stream is routed.
442*148569fdSSubhransu S. Prusty  *
443*148569fdSSubhransu S. Prusty  * Also query connection list of the pin, to validate the cvt to pin map.
444*148569fdSSubhransu S. Prusty  *
445*148569fdSSubhransu S. Prusty  * Same stream rendering to multiple pins simultaneously can be done
446*148569fdSSubhransu S. Prusty  * possibly, but not supported for now in driver. So return the first pin
447*148569fdSSubhransu S. Prusty  * connected.
448*148569fdSSubhransu S. Prusty  */
449*148569fdSSubhransu S. Prusty static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
450*148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
451*148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
452*148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
453*148569fdSSubhransu S. Prusty {
454*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
455*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin = NULL;
456*148569fdSSubhransu S. Prusty 	int ret, i;
457*148569fdSSubhransu S. Prusty 
458*148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
459*148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
460*148569fdSSubhransu S. Prusty 			pin = pcm->pin;
461*148569fdSSubhransu S. Prusty 			break;
462*148569fdSSubhransu S. Prusty 		}
463*148569fdSSubhransu S. Prusty 	}
464*148569fdSSubhransu S. Prusty 
465*148569fdSSubhransu S. Prusty 	if (pin) {
466*148569fdSSubhransu S. Prusty 		ret = hdac_hdmi_query_pin_connlist(edev, pin);
467*148569fdSSubhransu S. Prusty 		if (ret < 0)
468*148569fdSSubhransu S. Prusty 			return NULL;
469*148569fdSSubhransu S. Prusty 
470*148569fdSSubhransu S. Prusty 		for (i = 0; i < pin->num_mux_nids; i++) {
471*148569fdSSubhransu S. Prusty 			if (pin->mux_nids[i] == cvt->nid)
472*148569fdSSubhransu S. Prusty 				return pin;
473*148569fdSSubhransu S. Prusty 		}
474*148569fdSSubhransu S. Prusty 	}
475*148569fdSSubhransu S. Prusty 
476*148569fdSSubhransu S. Prusty 	return NULL;
477*148569fdSSubhransu S. Prusty }
478*148569fdSSubhransu S. Prusty 
479b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
480b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
481b0362adbSSubhransu S. Prusty {
482b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
483b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
484b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
485*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
486*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
4872428bca3SSubhransu S. Prusty 	int ret;
488b0362adbSSubhransu S. Prusty 
489b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
490b0362adbSSubhransu S. Prusty 
491*148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
492*148569fdSSubhransu S. Prusty 	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
493*148569fdSSubhransu S. Prusty 	if (!pin)
494*148569fdSSubhransu S. Prusty 		return -EIO;
495*148569fdSSubhransu S. Prusty 
496*148569fdSSubhransu S. Prusty 	if ((!pin->eld.monitor_present) ||
497*148569fdSSubhransu S. Prusty 			(!pin->eld.eld_valid)) {
498b0362adbSSubhransu S. Prusty 
499b8a54545SSubhransu S. Prusty 		dev_err(&hdac->hdac.dev,
500*148569fdSSubhransu S. Prusty 			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
501*148569fdSSubhransu S. Prusty 			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
502b8a54545SSubhransu S. Prusty 
503b0362adbSSubhransu S. Prusty 		return -ENODEV;
504b0362adbSSubhransu S. Prusty 	}
505b0362adbSSubhransu S. Prusty 
506*148569fdSSubhransu S. Prusty 	dai_map->pin = pin;
507b0362adbSSubhransu S. Prusty 
508*148569fdSSubhransu S. Prusty 	ret = hdac_hdmi_enable_pin(hdac, dai_map);
509*148569fdSSubhransu S. Prusty 	if (ret < 0)
510*148569fdSSubhransu S. Prusty 		return ret;
511b0362adbSSubhransu S. Prusty 
5122428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
513*148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
5142428bca3SSubhransu S. Prusty 	if (ret < 0)
5152428bca3SSubhransu S. Prusty 		return ret;
516b0362adbSSubhransu S. Prusty 
5172428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
518*148569fdSSubhransu S. Prusty 				pin->eld.eld_buffer);
519b0362adbSSubhransu S. Prusty }
520b0362adbSSubhransu S. Prusty 
521b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
522b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
523b0362adbSSubhransu S. Prusty {
524b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
525b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
526b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
527b0362adbSSubhransu S. Prusty 
528b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
529b0362adbSSubhransu S. Prusty 
530b0362adbSSubhransu S. Prusty 	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
531b0362adbSSubhransu S. Prusty 
53215b91447SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
533b0362adbSSubhransu S. Prusty 			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
534*148569fdSSubhransu S. Prusty 
535*148569fdSSubhransu S. Prusty 	dai_map->pin = NULL;
536b0362adbSSubhransu S. Prusty }
537b0362adbSSubhransu S. Prusty 
53818382eadSSubhransu S. Prusty static int
53918382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
54018382eadSSubhransu S. Prusty {
54118382eadSSubhransu S. Prusty 	int err;
54218382eadSSubhransu S. Prusty 
54318382eadSSubhransu S. Prusty 	/* Only stereo supported as of now */
54418382eadSSubhransu S. Prusty 	cvt->params.channels_min = cvt->params.channels_max = 2;
54518382eadSSubhransu S. Prusty 
54618382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
54718382eadSSubhransu S. Prusty 			&cvt->params.rates,
54818382eadSSubhransu S. Prusty 			&cvt->params.formats,
54918382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
55018382eadSSubhransu S. Prusty 	if (err < 0)
55118382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
55218382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
55318382eadSSubhransu S. Prusty 			cvt->nid, err);
55418382eadSSubhransu S. Prusty 
55518382eadSSubhransu S. Prusty 	return err;
55618382eadSSubhransu S. Prusty }
55718382eadSSubhransu S. Prusty 
55879f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
55979f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *w,
56079f4e922SSubhransu S. Prusty 				enum snd_soc_dapm_type id, void *priv,
56179f4e922SSubhransu S. Prusty 				const char *wname, const char *stream,
56279f4e922SSubhransu S. Prusty 				struct snd_kcontrol_new *wc, int numkc)
56318382eadSSubhransu S. Prusty {
56418382eadSSubhransu S. Prusty 	w->id = id;
56579f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
56679f4e922SSubhransu S. Prusty 	if (!w->name)
56779f4e922SSubhransu S. Prusty 		return -ENOMEM;
56879f4e922SSubhransu S. Prusty 
56918382eadSSubhransu S. Prusty 	w->sname = stream;
57018382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
57118382eadSSubhransu S. Prusty 	w->shift = 0;
57279f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
57379f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
57479f4e922SSubhransu S. Prusty 	w->priv = priv;
57579f4e922SSubhransu S. Prusty 
57679f4e922SSubhransu S. Prusty 	return 0;
57718382eadSSubhransu S. Prusty }
57818382eadSSubhransu S. Prusty 
57918382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
58079f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
58179f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
58279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
58318382eadSSubhransu S. Prusty {
58418382eadSSubhransu S. Prusty 	route->sink = sink;
58518382eadSSubhransu S. Prusty 	route->source = src;
58618382eadSSubhransu S. Prusty 	route->control = control;
58779f4e922SSubhransu S. Prusty 	route->connected = handler;
58818382eadSSubhransu S. Prusty }
58918382eadSSubhransu S. Prusty 
5904a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
5914a3478deSJeeja KP 					struct hdac_hdmi_pin *pin)
5924a3478deSJeeja KP {
5934a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
5944a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
5954a3478deSJeeja KP 
5964a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
5974a3478deSJeeja KP 		if (pcm->pin == pin)
5984a3478deSJeeja KP 			return pcm;
5994a3478deSJeeja KP 	}
6004a3478deSJeeja KP 
6014a3478deSJeeja KP 	return NULL;
6024a3478deSJeeja KP }
6034a3478deSJeeja KP 
6044a3478deSJeeja KP /*
6054a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
6064a3478deSJeeja KP  */
6074a3478deSJeeja KP static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
6084a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
6094a3478deSJeeja KP {
6104a3478deSJeeja KP 	int ret;
6114a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
6124a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
6134a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
6144a3478deSJeeja KP 	struct hdac_hdmi_pin *pin = w->priv;
6154a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
6164a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6174a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
6184a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
6194a3478deSJeeja KP 
6204a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
6214a3478deSJeeja KP 	if (ret < 0)
6224a3478deSJeeja KP 		return ret;
6234a3478deSJeeja KP 
6244a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
6254a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
6264a3478deSJeeja KP 		if (pcm->pin == pin)
6274a3478deSJeeja KP 			pcm->pin = NULL;
6284a3478deSJeeja KP 
6294a3478deSJeeja KP 		/*
6304a3478deSJeeja KP 		 * Jack status is not reported during device probe as the
6314a3478deSJeeja KP 		 * PCMs are not registered by then. So report it here.
6324a3478deSJeeja KP 		 */
6334a3478deSJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
6344a3478deSJeeja KP 			pcm->pin = pin;
6354a3478deSJeeja KP 			if (pin->eld.monitor_present && pin->eld.eld_valid) {
6364a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
6374a3478deSJeeja KP 					"jack report for pcm=%d\n",
6384a3478deSJeeja KP 					pcm->pcm_id);
6394a3478deSJeeja KP 
6404a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
6414a3478deSJeeja KP 			}
6424a3478deSJeeja KP 			mutex_unlock(&hdmi->pin_mutex);
6434a3478deSJeeja KP 			return ret;
6444a3478deSJeeja KP 		}
6454a3478deSJeeja KP 	}
6464a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
6474a3478deSJeeja KP 
6484a3478deSJeeja KP 	return ret;
6494a3478deSJeeja KP }
6504a3478deSJeeja KP 
65179f4e922SSubhransu S. Prusty /*
65279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
65379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
65479f4e922SSubhransu S. Prusty  * widget runtime.
65579f4e922SSubhransu S. Prusty  *
65679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
65779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
65879f4e922SSubhransu S. Prusty  * "NONE"
65979f4e922SSubhransu S. Prusty  */
66079f4e922SSubhransu S. Prusty static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
66179f4e922SSubhransu S. Prusty 				struct hdac_hdmi_pin *pin,
66279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
66379f4e922SSubhransu S. Prusty 				const char *widget_name)
66418382eadSSubhransu S. Prusty {
66579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
66679f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
66779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
66879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
66979f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
67079f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
67179f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
67279f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
67379f4e922SSubhransu S. Prusty 	int i = 0;
67479f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
67518382eadSSubhransu S. Prusty 
67679f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
67779f4e922SSubhransu S. Prusty 	if (!kc)
67879f4e922SSubhransu S. Prusty 		return -ENOMEM;
67918382eadSSubhransu S. Prusty 
68079f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
68179f4e922SSubhransu S. Prusty 	if (!se)
68279f4e922SSubhransu S. Prusty 		return -ENOMEM;
68318382eadSSubhransu S. Prusty 
68479f4e922SSubhransu S. Prusty 	sprintf(kc_name, "Pin %d Input", pin->nid);
68579f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
68679f4e922SSubhransu S. Prusty 	if (!kc->name)
68779f4e922SSubhransu S. Prusty 		return -ENOMEM;
68818382eadSSubhransu S. Prusty 
68979f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
69079f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
69179f4e922SSubhransu S. Prusty 	kc->access = 0;
69279f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
6934a3478deSJeeja KP 	kc->put = hdac_hdmi_set_pin_mux;
69479f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
69579f4e922SSubhransu S. Prusty 
69679f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
69779f4e922SSubhransu S. Prusty 
69879f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
69979f4e922SSubhransu S. Prusty 	se->items = num_items;
70079f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
70179f4e922SSubhransu S. Prusty 
70279f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
70379f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
70479f4e922SSubhransu S. Prusty 	if (!items[i])
70579f4e922SSubhransu S. Prusty 		return -ENOMEM;
70679f4e922SSubhransu S. Prusty 
70779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
70879f4e922SSubhransu S. Prusty 		i++;
70979f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
71079f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
71179f4e922SSubhransu S. Prusty 		if (!items[i])
71279f4e922SSubhransu S. Prusty 			return -ENOMEM;
71379f4e922SSubhransu S. Prusty 	}
71479f4e922SSubhransu S. Prusty 
71579f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
71679f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
71779f4e922SSubhransu S. Prusty 	if (!se->texts)
71879f4e922SSubhransu S. Prusty 		return -ENOMEM;
71979f4e922SSubhransu S. Prusty 
72079f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
7214a3478deSJeeja KP 			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
72279f4e922SSubhransu S. Prusty }
72379f4e922SSubhransu S. Prusty 
72479f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
72579f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
72679f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
72779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
72879f4e922SSubhransu S. Prusty {
72979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
73079f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
73179f4e922SSubhransu S. Prusty 	struct soc_enum *se;
73279f4e922SSubhransu S. Prusty 	int mux_index = hdmi->num_cvt + hdmi->num_pin;
73379f4e922SSubhransu S. Prusty 	int i, j;
73479f4e922SSubhransu S. Prusty 
73579f4e922SSubhransu S. Prusty 	for (i = 0; i < hdmi->num_pin; i++) {
73679f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
73779f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
73879f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
73979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
74079f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
74179f4e922SSubhransu S. Prusty 					se->texts[j + 1],
74279f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
74379f4e922SSubhransu S. Prusty 
74479f4e922SSubhransu S. Prusty 			rindex++;
74579f4e922SSubhransu S. Prusty 		}
74679f4e922SSubhransu S. Prusty 
74779f4e922SSubhransu S. Prusty 		mux_index++;
74879f4e922SSubhransu S. Prusty 	}
74979f4e922SSubhransu S. Prusty }
75079f4e922SSubhransu S. Prusty 
75179f4e922SSubhransu S. Prusty /*
75279f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
75379f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
75479f4e922SSubhransu S. Prusty  *	Pin widgets for num pins enumerated
75579f4e922SSubhransu S. Prusty  *	Pin mux widgets to represent connenction list of pin widget
75679f4e922SSubhransu S. Prusty  *
75779f4e922SSubhransu S. Prusty  * Total widgets elements = num_cvt + num_pin + num_pin;
75879f4e922SSubhransu S. Prusty  *
75979f4e922SSubhransu S. Prusty  * Routes are added as below:
76079f4e922SSubhransu S. Prusty  *	pin mux -> pin (based on num_pins)
76179f4e922SSubhransu S. Prusty  *	cvt -> "Input sel control" -> pin_mux
76279f4e922SSubhransu S. Prusty  *
76379f4e922SSubhransu S. Prusty  * Total route elements:
76479f4e922SSubhransu S. Prusty  *	num_pins + (pin_muxes * num_cvt)
76579f4e922SSubhransu S. Prusty  */
76679f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
76779f4e922SSubhransu S. Prusty {
76879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
76979f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
77079f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
77179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
77279f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
77379f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
77479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
77579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
77679f4e922SSubhransu S. Prusty 	int ret, i = 0, num_routes = 0;
77779f4e922SSubhransu S. Prusty 
77879f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
77979f4e922SSubhransu S. Prusty 		return -EINVAL;
78079f4e922SSubhransu S. Prusty 
78179f4e922SSubhransu S. Prusty 	widgets = devm_kzalloc(dapm->dev,
78279f4e922SSubhransu S. Prusty 		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
78379f4e922SSubhransu S. Prusty 		GFP_KERNEL);
78479f4e922SSubhransu S. Prusty 
78579f4e922SSubhransu S. Prusty 	if (!widgets)
78679f4e922SSubhransu S. Prusty 		return -ENOMEM;
78779f4e922SSubhransu S. Prusty 
78879f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
78979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
79079f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
79179f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
79279f4e922SSubhransu S. Prusty 			snd_soc_dapm_aif_in, &cvt->nid,
79379f4e922SSubhransu S. Prusty 			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
79479f4e922SSubhransu S. Prusty 		if (ret < 0)
79579f4e922SSubhransu S. Prusty 			return ret;
79679f4e922SSubhransu S. Prusty 		i++;
79779f4e922SSubhransu S. Prusty 	}
79879f4e922SSubhransu S. Prusty 
79979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
80079f4e922SSubhransu S. Prusty 		sprintf(widget_name, "hif%d Output", pin->nid);
80179f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
80279f4e922SSubhransu S. Prusty 				snd_soc_dapm_output, &pin->nid,
80379f4e922SSubhransu S. Prusty 				widget_name, NULL, NULL, 0);
80479f4e922SSubhransu S. Prusty 		if (ret < 0)
80579f4e922SSubhransu S. Prusty 			return ret;
80679f4e922SSubhransu S. Prusty 		i++;
80779f4e922SSubhransu S. Prusty 	}
80879f4e922SSubhransu S. Prusty 
80979f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
81079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
81179f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Pin %d Mux", pin->nid);
81279f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
81379f4e922SSubhransu S. Prusty 							widget_name);
81479f4e922SSubhransu S. Prusty 		if (ret < 0)
81579f4e922SSubhransu S. Prusty 			return ret;
81679f4e922SSubhransu S. Prusty 		i++;
81779f4e922SSubhransu S. Prusty 
81879f4e922SSubhransu S. Prusty 		/* For cvt to pin_mux mapping */
81979f4e922SSubhransu S. Prusty 		num_routes += hdmi->num_cvt;
82079f4e922SSubhransu S. Prusty 
82179f4e922SSubhransu S. Prusty 		/* For pin_mux to pin mapping */
82279f4e922SSubhransu S. Prusty 		num_routes++;
82379f4e922SSubhransu S. Prusty 	}
82479f4e922SSubhransu S. Prusty 
82579f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
82679f4e922SSubhransu S. Prusty 							GFP_KERNEL);
82779f4e922SSubhransu S. Prusty 	if (!route)
82879f4e922SSubhransu S. Prusty 		return -ENOMEM;
82979f4e922SSubhransu S. Prusty 
83079f4e922SSubhransu S. Prusty 	i = 0;
83179f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
83279f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
83379f4e922SSubhransu S. Prusty 		int sink_index = i + hdmi->num_cvt;
83479f4e922SSubhransu S. Prusty 		int src_index = sink_index + hdmi->num_pin;
83579f4e922SSubhransu S. Prusty 
83679f4e922SSubhransu S. Prusty 		hdac_hdmi_fill_route(&route[i],
83779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
83879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
83979f4e922SSubhransu S. Prusty 		i++;
84079f4e922SSubhransu S. Prusty 
84179f4e922SSubhransu S. Prusty 	}
84279f4e922SSubhransu S. Prusty 
84379f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
84479f4e922SSubhransu S. Prusty 
84579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
84679f4e922SSubhransu S. Prusty 		((2 * hdmi->num_pin) + hdmi->num_cvt));
84779f4e922SSubhransu S. Prusty 
84879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
84979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
85079f4e922SSubhransu S. Prusty 
85179f4e922SSubhransu S. Prusty 	return 0;
85279f4e922SSubhransu S. Prusty 
85318382eadSSubhransu S. Prusty }
85418382eadSSubhransu S. Prusty 
85515b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
85618382eadSSubhransu S. Prusty {
85715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
858*148569fdSSubhransu S. Prusty 	struct hdac_hdmi_dai_pin_map *dai_map;
85915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
860*148569fdSSubhransu S. Prusty 	int dai_id = 0;
86118382eadSSubhransu S. Prusty 
862*148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
86315b91447SSubhransu S. Prusty 		return -EINVAL;
86418382eadSSubhransu S. Prusty 
865*148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
866*148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
867*148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
86815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
86918382eadSSubhransu S. Prusty 
87018382eadSSubhransu S. Prusty 		/* Enable transmission */
87115b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
87218382eadSSubhransu S. Prusty 				AC_VERB_SET_DIGI_CONVERT_1, 1);
87318382eadSSubhransu S. Prusty 
87418382eadSSubhransu S. Prusty 		/* Category Code (CC) to zero */
87515b91447SSubhransu S. Prusty 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
87618382eadSSubhransu S. Prusty 				AC_VERB_SET_DIGI_CONVERT_2, 0);
87718382eadSSubhransu S. Prusty 
878*148569fdSSubhransu S. Prusty 		dai_id++;
879*148569fdSSubhransu S. Prusty 
880*148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
881*148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
882*148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
883*148569fdSSubhransu S. Prusty 			break;
884*148569fdSSubhransu S. Prusty 		}
885*148569fdSSubhransu S. Prusty 	}
88618382eadSSubhransu S. Prusty 
88715b91447SSubhransu S. Prusty 	return 0;
88815b91447SSubhransu S. Prusty }
88915b91447SSubhransu S. Prusty 
89015b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
89115b91447SSubhransu S. Prusty {
89215b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
89315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
8944a3478deSJeeja KP 	char name[NAME_SIZE];
89515b91447SSubhransu S. Prusty 
89615b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
89715b91447SSubhransu S. Prusty 	if (!cvt)
89815b91447SSubhransu S. Prusty 		return -ENOMEM;
89915b91447SSubhransu S. Prusty 
90015b91447SSubhransu S. Prusty 	cvt->nid = nid;
9014a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
9024a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
90315b91447SSubhransu S. Prusty 
90415b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
90515b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
90615b91447SSubhransu S. Prusty 
90715b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
90815b91447SSubhransu S. Prusty }
90915b91447SSubhransu S. Prusty 
910b8a54545SSubhransu S. Prusty static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
911b8a54545SSubhransu S. Prusty {
912b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
9134a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
9144a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
915b8a54545SSubhransu S. Prusty 	int val;
916b8a54545SSubhransu S. Prusty 
917b8a54545SSubhransu S. Prusty 	if (!edev)
918b8a54545SSubhransu S. Prusty 		return;
919b8a54545SSubhransu S. Prusty 
920b8a54545SSubhransu S. Prusty 	pin->repoll_count = repoll;
921b8a54545SSubhransu S. Prusty 
922b8a54545SSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
923b8a54545SSubhransu S. Prusty 	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
924b8a54545SSubhransu S. Prusty 					AC_VERB_GET_PIN_SENSE, 0);
925b8a54545SSubhransu S. Prusty 
926b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
927b8a54545SSubhransu S. Prusty 						val, pin->nid);
928b8a54545SSubhransu S. Prusty 
9294a3478deSJeeja KP 
9304a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
931b8a54545SSubhransu S. Prusty 	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
932b8a54545SSubhransu S. Prusty 	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
933b8a54545SSubhransu S. Prusty 
9344a3478deSJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, pin);
9354a3478deSJeeja KP 
936b8a54545SSubhransu S. Prusty 	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
937b8a54545SSubhransu S. Prusty 
938b8a54545SSubhransu S. Prusty 		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
939b8a54545SSubhransu S. Prusty 						__func__, pin->nid);
9404a3478deSJeeja KP 
9414a3478deSJeeja KP 		/*
9424a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
9434a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
9444a3478deSJeeja KP 		 * control select.
9454a3478deSJeeja KP 		 */
9464a3478deSJeeja KP 		if (pcm) {
9474a3478deSJeeja KP 			dev_dbg(&edev->hdac.dev,
9484a3478deSJeeja KP 				"jack report for pcm=%d\n", pcm->pcm_id);
9494a3478deSJeeja KP 
9504a3478deSJeeja KP 			snd_jack_report(pcm->jack, 0);
9514a3478deSJeeja KP 		}
9524a3478deSJeeja KP 
9534a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
954b8a54545SSubhransu S. Prusty 		goto put_hdac_device;
955b8a54545SSubhransu S. Prusty 	}
956b8a54545SSubhransu S. Prusty 
957b8a54545SSubhransu S. Prusty 	if (pin->eld.monitor_present && pin->eld.eld_valid) {
958b8a54545SSubhransu S. Prusty 		/* TODO: use i915 component for reading ELD later */
959b8a54545SSubhransu S. Prusty 		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
960b8a54545SSubhransu S. Prusty 				pin->eld.eld_buffer,
961b8a54545SSubhransu S. Prusty 				&pin->eld.eld_size) == 0) {
962b8a54545SSubhransu S. Prusty 
9634a3478deSJeeja KP 			if (pcm) {
9644a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
9654a3478deSJeeja KP 					"jack report for pcm=%d\n",
9664a3478deSJeeja KP 					pcm->pcm_id);
9674a3478deSJeeja KP 
9684a3478deSJeeja KP 				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
9694a3478deSJeeja KP 			}
9704a3478deSJeeja KP 
971b8a54545SSubhransu S. Prusty 			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
972b8a54545SSubhransu S. Prusty 					pin->eld.eld_buffer, pin->eld.eld_size);
973b8a54545SSubhransu S. Prusty 		} else {
974b8a54545SSubhransu S. Prusty 			pin->eld.monitor_present = false;
975b8a54545SSubhransu S. Prusty 			pin->eld.eld_valid = false;
9764a3478deSJeeja KP 
9774a3478deSJeeja KP 			if (pcm) {
9784a3478deSJeeja KP 				dev_dbg(&edev->hdac.dev,
9794a3478deSJeeja KP 					"jack report for pcm=%d\n",
9804a3478deSJeeja KP 					pcm->pcm_id);
9814a3478deSJeeja KP 
9824a3478deSJeeja KP 				snd_jack_report(pcm->jack, 0);
983b8a54545SSubhransu S. Prusty 			}
984b8a54545SSubhransu S. Prusty 		}
9854a3478deSJeeja KP 	}
9864a3478deSJeeja KP 
9874a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
988b8a54545SSubhransu S. Prusty 
989b8a54545SSubhransu S. Prusty 	/*
990b8a54545SSubhransu S. Prusty 	 * Sometimes the pin_sense may present invalid monitor
991b8a54545SSubhransu S. Prusty 	 * present and eld_valid. If ELD data is not valid, loop few
992b8a54545SSubhransu S. Prusty 	 * more times to get correct pin sense and valid ELD.
993b8a54545SSubhransu S. Prusty 	 */
994b8a54545SSubhransu S. Prusty 	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
995b8a54545SSubhransu S. Prusty 		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
996b8a54545SSubhransu S. Prusty 
997b8a54545SSubhransu S. Prusty put_hdac_device:
998b8a54545SSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
999b8a54545SSubhransu S. Prusty }
1000b8a54545SSubhransu S. Prusty 
1001b8a54545SSubhransu S. Prusty static void hdac_hdmi_repoll_eld(struct work_struct *work)
1002b8a54545SSubhransu S. Prusty {
1003b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin =
1004b8a54545SSubhransu S. Prusty 		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1005b8a54545SSubhransu S. Prusty 
1006b8a54545SSubhransu S. Prusty 	/* picked from legacy HDA driver */
1007b8a54545SSubhransu S. Prusty 	if (pin->repoll_count++ > 6)
1008b8a54545SSubhransu S. Prusty 		pin->repoll_count = 0;
1009b8a54545SSubhransu S. Prusty 
1010b8a54545SSubhransu S. Prusty 	hdac_hdmi_present_sense(pin, pin->repoll_count);
1011b8a54545SSubhransu S. Prusty }
1012b8a54545SSubhransu S. Prusty 
101315b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
101415b91447SSubhransu S. Prusty {
101515b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
101615b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
101715b91447SSubhransu S. Prusty 
101815b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
101915b91447SSubhransu S. Prusty 	if (!pin)
102015b91447SSubhransu S. Prusty 		return -ENOMEM;
102115b91447SSubhransu S. Prusty 
102215b91447SSubhransu S. Prusty 	pin->nid = nid;
102315b91447SSubhransu S. Prusty 
102415b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
102515b91447SSubhransu S. Prusty 	hdmi->num_pin++;
102615b91447SSubhransu S. Prusty 
1027b8a54545SSubhransu S. Prusty 	pin->edev = edev;
1028b8a54545SSubhransu S. Prusty 	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1029b8a54545SSubhransu S. Prusty 
103015b91447SSubhransu S. Prusty 	return 0;
103118382eadSSubhransu S. Prusty }
103218382eadSSubhransu S. Prusty 
1033211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1034211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1035211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1036211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1037211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1038211caab7SSubhransu S. Prusty 
1039211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1040211caab7SSubhransu S. Prusty {
1041211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1042211caab7SSubhransu S. Prusty 
1043211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1044211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1045211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1046211caab7SSubhransu S. Prusty 		return;
1047211caab7SSubhransu S. Prusty 
1048211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1049211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1050211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1051211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1052211caab7SSubhransu S. Prusty 		return;
1053211caab7SSubhransu S. Prusty }
1054211caab7SSubhransu S. Prusty 
1055211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1056211caab7SSubhransu S. Prusty {
1057211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1058211caab7SSubhransu S. Prusty 
1059211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1060211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1061211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1062211caab7SSubhransu S. Prusty 		return;
1063211caab7SSubhransu S. Prusty 
1064211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1065211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1066211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1067211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1068211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1069211caab7SSubhransu S. Prusty 		return;
1070211caab7SSubhransu S. Prusty 
1071211caab7SSubhransu S. Prusty }
1072211caab7SSubhransu S. Prusty 
107317a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
107417a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
107517a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
107617a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
107717a42c45SSubhransu S. Prusty 	.prepare = hdac_hdmi_playback_prepare,
107817a42c45SSubhransu S. Prusty 	.hw_free = hdac_hdmi_playback_cleanup,
107917a42c45SSubhransu S. Prusty };
108017a42c45SSubhransu S. Prusty 
108117a42c45SSubhransu S. Prusty /*
108217a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
108317a42c45SSubhransu S. Prusty  * based on the number of converter queried.
108417a42c45SSubhransu S. Prusty  */
108517a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
108617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
108717a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
108817a42c45SSubhransu S. Prusty {
108917a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
109017a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
109117a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
109217a42c45SSubhransu S. Prusty 	int i = 0;
109317a42c45SSubhransu S. Prusty 	u32 rates, bps;
109417a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
109517a42c45SSubhransu S. Prusty 	u64 formats;
109617a42c45SSubhransu S. Prusty 	int ret;
109717a42c45SSubhransu S. Prusty 
109817a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
109917a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
110017a42c45SSubhransu S. Prusty 			GFP_KERNEL);
110117a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
110217a42c45SSubhransu S. Prusty 		return -ENOMEM;
110317a42c45SSubhransu S. Prusty 
110417a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
110517a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
110617a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
110717a42c45SSubhransu S. Prusty 		if (ret)
110817a42c45SSubhransu S. Prusty 			return ret;
110917a42c45SSubhransu S. Prusty 
111017a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
111117a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
111217a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
111317a42c45SSubhransu S. Prusty 
111417a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
111517a42c45SSubhransu S. Prusty 			return -ENOMEM;
111617a42c45SSubhransu S. Prusty 
111717a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
111817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
111917a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
112017a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
112117a42c45SSubhransu S. Prusty 			return -ENOMEM;
112217a42c45SSubhransu S. Prusty 
112317a42c45SSubhransu S. Prusty 		/*
112417a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
112517a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
112617a42c45SSubhransu S. Prusty 		 */
112717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
112817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
112917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
113017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
113117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
113217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
113317a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
113417a42c45SSubhransu S. Prusty 
113517a42c45SSubhransu S. Prusty 		i++;
113617a42c45SSubhransu S. Prusty 	}
113717a42c45SSubhransu S. Prusty 
113817a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
113917a42c45SSubhransu S. Prusty 
114017a42c45SSubhransu S. Prusty 	return 0;
114117a42c45SSubhransu S. Prusty }
114217a42c45SSubhransu S. Prusty 
114318382eadSSubhransu S. Prusty /*
114418382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
114518382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
114618382eadSSubhransu S. Prusty  */
114717a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
114817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
114918382eadSSubhransu S. Prusty {
115018382eadSSubhransu S. Prusty 	hda_nid_t nid;
11513c83ac23SSudip Mukherjee 	int i, num_nodes;
115218382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
115318382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
115415b91447SSubhransu S. Prusty 	int ret;
115518382eadSSubhransu S. Prusty 
1156211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1157211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1158211caab7SSubhransu S. Prusty 
11593c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1160541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
116118382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
116218382eadSSubhransu S. Prusty 		return -EINVAL;
116318382eadSSubhransu S. Prusty 	}
116418382eadSSubhransu S. Prusty 
11653c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
116618382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
116718382eadSSubhransu S. Prusty 
116818382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
116918382eadSSubhransu S. Prusty 		unsigned int caps;
117018382eadSSubhransu S. Prusty 		unsigned int type;
117118382eadSSubhransu S. Prusty 
117218382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
117318382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
117418382eadSSubhransu S. Prusty 
117518382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
117618382eadSSubhransu S. Prusty 			continue;
117718382eadSSubhransu S. Prusty 
117818382eadSSubhransu S. Prusty 		switch (type) {
117918382eadSSubhransu S. Prusty 
118018382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
118115b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
118215b91447SSubhransu S. Prusty 			if (ret < 0)
118315b91447SSubhransu S. Prusty 				return ret;
118418382eadSSubhransu S. Prusty 			break;
118518382eadSSubhransu S. Prusty 
118618382eadSSubhransu S. Prusty 		case AC_WID_PIN:
118715b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
118815b91447SSubhransu S. Prusty 			if (ret < 0)
118915b91447SSubhransu S. Prusty 				return ret;
119018382eadSSubhransu S. Prusty 			break;
119118382eadSSubhransu S. Prusty 		}
119218382eadSSubhransu S. Prusty 	}
119318382eadSSubhransu S. Prusty 
119418382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
119518382eadSSubhransu S. Prusty 
119615b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
119718382eadSSubhransu S. Prusty 		return -EIO;
119818382eadSSubhransu S. Prusty 
119917a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
120017a42c45SSubhransu S. Prusty 	if (ret) {
120117a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
120217a42c45SSubhransu S. Prusty 							ret);
120317a42c45SSubhransu S. Prusty 		return ret;
120417a42c45SSubhransu S. Prusty 	}
120517a42c45SSubhransu S. Prusty 
120617a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
120717a42c45SSubhransu S. Prusty 
120815b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
120918382eadSSubhransu S. Prusty }
121018382eadSSubhransu S. Prusty 
1211b8a54545SSubhransu S. Prusty static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1212b8a54545SSubhransu S. Prusty {
1213b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1214b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1215b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1216b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
1217b8a54545SSubhransu S. Prusty 
1218b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1219b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1220b8a54545SSubhransu S. Prusty 
1221b8a54545SSubhransu S. Prusty 	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1222b8a54545SSubhransu S. Prusty 
1223b8a54545SSubhransu S. Prusty 	/*
1224b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1225b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1226b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1227b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1228b8a54545SSubhransu S. Prusty 	 */
1229b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1230b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1231b8a54545SSubhransu S. Prusty 		return;
1232b8a54545SSubhransu S. Prusty 
1233b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1234b8a54545SSubhransu S. Prusty 		return;
1235b8a54545SSubhransu S. Prusty 
1236b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1237b8a54545SSubhransu S. Prusty 		if (pin->nid == pin_nid)
1238b8a54545SSubhransu S. Prusty 			hdac_hdmi_present_sense(pin, 1);
1239b8a54545SSubhransu S. Prusty 	}
1240b8a54545SSubhransu S. Prusty }
1241b8a54545SSubhransu S. Prusty 
1242b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1243b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1244b8a54545SSubhransu S. Prusty };
1245b8a54545SSubhransu S. Prusty 
12464a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
12474a3478deSJeeja KP {
12484a3478deSJeeja KP 	char jack_name[NAME_SIZE];
12494a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
12504a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
12514a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
12524a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
12534a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
12544a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
12554a3478deSJeeja KP 
12564a3478deSJeeja KP 	/*
12574a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
12584a3478deSJeeja KP 	 * add to the pcm list
12594a3478deSJeeja KP 	 */
12604a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
12614a3478deSJeeja KP 	if (!pcm)
12624a3478deSJeeja KP 		return -ENOMEM;
12634a3478deSJeeja KP 	pcm->pcm_id = device;
12644a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
12654a3478deSJeeja KP 
12664a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
12674a3478deSJeeja KP 
12684a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
12694a3478deSJeeja KP 
12704a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
12714a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
12724a3478deSJeeja KP }
12734a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
12744a3478deSJeeja KP 
127518382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
127618382eadSSubhransu S. Prusty {
127718382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
127818382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
127918382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
128018382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1281b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1282b8a54545SSubhransu S. Prusty 	int ret;
128318382eadSSubhransu S. Prusty 
128418382eadSSubhransu S. Prusty 	edev->scodec = codec;
128518382eadSSubhransu S. Prusty 
128679f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
128779f4e922SSubhransu S. Prusty 	if (ret < 0)
128879f4e922SSubhransu S. Prusty 		return ret;
128918382eadSSubhransu S. Prusty 
1290b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1291b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1292b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1293b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1294b8a54545SSubhransu S. Prusty 				ret);
1295b8a54545SSubhransu S. Prusty 		return ret;
1296b8a54545SSubhransu S. Prusty 	}
1297b8a54545SSubhransu S. Prusty 
1298b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head)
1299b8a54545SSubhransu S. Prusty 		hdac_hdmi_present_sense(pin, 1);
1300b8a54545SSubhransu S. Prusty 
130118382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
130218382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
130318382eadSSubhransu S. Prusty 
1304e342ac08SSubhransu S. Prusty 	/*
1305e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1306e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1307e342ac08SSubhransu S. Prusty 	 */
1308e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1309e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1310e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1311e342ac08SSubhransu S. Prusty 
1312e342ac08SSubhransu S. Prusty 	return 0;
1313e342ac08SSubhransu S. Prusty }
1314e342ac08SSubhransu S. Prusty 
1315e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1316e342ac08SSubhransu S. Prusty {
1317e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1318e342ac08SSubhransu S. Prusty 
1319e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
132018382eadSSubhransu S. Prusty 	return 0;
132118382eadSSubhransu S. Prusty }
132218382eadSSubhransu S. Prusty 
132318382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
132418382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1325e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
132618382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
132718382eadSSubhransu S. Prusty };
132818382eadSSubhransu S. Prusty 
132918382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
133018382eadSSubhransu S. Prusty {
133118382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
133218382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
133317a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
133417a42c45SSubhransu S. Prusty 	int num_dais = 0;
133518382eadSSubhransu S. Prusty 	int ret = 0;
133618382eadSSubhransu S. Prusty 
133718382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
133818382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
133918382eadSSubhransu S. Prusty 		return -ENOMEM;
134018382eadSSubhransu S. Prusty 
134118382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
134218382eadSSubhransu S. Prusty 
134318382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
134418382eadSSubhransu S. Prusty 
134515b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
134615b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
13474a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
13484a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
134915b91447SSubhransu S. Prusty 
135017a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
135117a42c45SSubhransu S. Prusty 	if (ret < 0) {
135217a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
135317a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
135418382eadSSubhransu S. Prusty 		return ret;
135517a42c45SSubhransu S. Prusty 	}
135618382eadSSubhransu S. Prusty 
135718382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
135818382eadSSubhransu S. Prusty 	return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
135917a42c45SSubhransu S. Prusty 			hdmi_dais, num_dais);
136018382eadSSubhransu S. Prusty }
136118382eadSSubhransu S. Prusty 
136218382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
136318382eadSSubhransu S. Prusty {
136415b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
136515b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
136615b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
13674a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
136815b91447SSubhransu S. Prusty 
136918382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
137018382eadSSubhransu S. Prusty 
13714a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
13724a3478deSJeeja KP 		pcm->cvt = NULL;
13734a3478deSJeeja KP 		pcm->pin = NULL;
13744a3478deSJeeja KP 		list_del(&pcm->head);
13754a3478deSJeeja KP 		kfree(pcm);
13764a3478deSJeeja KP 	}
13774a3478deSJeeja KP 
137815b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
137915b91447SSubhransu S. Prusty 		list_del(&cvt->head);
13804a3478deSJeeja KP 		kfree(cvt->name);
138115b91447SSubhransu S. Prusty 		kfree(cvt);
138215b91447SSubhransu S. Prusty 	}
138315b91447SSubhransu S. Prusty 
138415b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
138515b91447SSubhransu S. Prusty 		list_del(&pin->head);
138615b91447SSubhransu S. Prusty 		kfree(pin);
138715b91447SSubhransu S. Prusty 	}
138815b91447SSubhransu S. Prusty 
138918382eadSSubhransu S. Prusty 	return 0;
139018382eadSSubhransu S. Prusty }
139118382eadSSubhransu S. Prusty 
1392e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1393e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1394e342ac08SSubhransu S. Prusty {
1395e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1396e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
139707f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
139807f083abSSubhransu S. Prusty 	int err;
1399e342ac08SSubhransu S. Prusty 
1400e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1401e342ac08SSubhransu S. Prusty 
140207f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
140307f083abSSubhransu S. Prusty 	if (!bus)
140407f083abSSubhransu S. Prusty 		return 0;
140507f083abSSubhransu S. Prusty 
1406e342ac08SSubhransu S. Prusty 	/* Power down afg */
1407e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1408e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1409e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1410e342ac08SSubhransu S. Prusty 
141107f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
141207f083abSSubhransu S. Prusty 	if (err < 0) {
141307f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
141407f083abSSubhransu S. Prusty 		return err;
141507f083abSSubhransu S. Prusty 	}
141607f083abSSubhransu S. Prusty 
1417e342ac08SSubhransu S. Prusty 	return 0;
1418e342ac08SSubhransu S. Prusty }
1419e342ac08SSubhransu S. Prusty 
1420e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1421e342ac08SSubhransu S. Prusty {
1422e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1423e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
142407f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
142507f083abSSubhransu S. Prusty 	int err;
1426e342ac08SSubhransu S. Prusty 
1427e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1428e342ac08SSubhransu S. Prusty 
142907f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
143007f083abSSubhransu S. Prusty 	if (!bus)
143107f083abSSubhransu S. Prusty 		return 0;
143207f083abSSubhransu S. Prusty 
143307f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
143407f083abSSubhransu S. Prusty 	if (err < 0) {
143507f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
143607f083abSSubhransu S. Prusty 		return err;
143707f083abSSubhransu S. Prusty 	}
143807f083abSSubhransu S. Prusty 
1439e342ac08SSubhransu S. Prusty 	/* Power up afg */
1440e342ac08SSubhransu S. Prusty 	if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1441e342ac08SSubhransu S. Prusty 		snd_hdac_codec_write(hdac, hdac->afg, 0,
1442e342ac08SSubhransu S. Prusty 			AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1443e342ac08SSubhransu S. Prusty 
1444e342ac08SSubhransu S. Prusty 	return 0;
1445e342ac08SSubhransu S. Prusty }
1446e342ac08SSubhransu S. Prusty #else
1447e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1448e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1449e342ac08SSubhransu S. Prusty #endif
1450e342ac08SSubhransu S. Prusty 
1451e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1452e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1453e342ac08SSubhransu S. Prusty };
1454e342ac08SSubhransu S. Prusty 
145518382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
145618382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
145718382eadSSubhransu S. Prusty 	{}
145818382eadSSubhransu S. Prusty };
145918382eadSSubhransu S. Prusty 
146018382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
146118382eadSSubhransu S. Prusty 
146218382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
146318382eadSSubhransu S. Prusty 	. hdac = {
146418382eadSSubhransu S. Prusty 		.driver = {
146518382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
1466e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
146718382eadSSubhransu S. Prusty 		},
146818382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
146918382eadSSubhransu S. Prusty 	},
147018382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
147118382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
147218382eadSSubhransu S. Prusty };
147318382eadSSubhransu S. Prusty 
147418382eadSSubhransu S. Prusty static int __init hdmi_init(void)
147518382eadSSubhransu S. Prusty {
147618382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
147718382eadSSubhransu S. Prusty }
147818382eadSSubhransu S. Prusty 
147918382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
148018382eadSSubhransu S. Prusty {
148118382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
148218382eadSSubhransu S. Prusty }
148318382eadSSubhransu S. Prusty 
148418382eadSSubhransu S. Prusty module_init(hdmi_init);
148518382eadSSubhransu S. Prusty module_exit(hdmi_exit);
148618382eadSSubhransu S. Prusty 
148718382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
148818382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
148918382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
149018382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
1491