xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 1c0a7de2)
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
45754695f9SJeeja KP #define HDA_MAX_PORTS		3
46148569fdSSubhransu S. Prusty 
47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
49b8a54545SSubhransu S. Prusty 
50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
53f6fa11a3SSandeep Tayal 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5518382eadSSubhransu S. Prusty 	unsigned int channels_min;
5618382eadSSubhransu S. Prusty 	unsigned int channels_max;
5718382eadSSubhransu S. Prusty 	u32 rates;
5818382eadSSubhransu S. Prusty 	u64 formats;
5918382eadSSubhransu S. Prusty 	unsigned int maxbps;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
6315b91447SSubhransu S. Prusty 	struct list_head head;
6418382eadSSubhransu S. Prusty 	hda_nid_t nid;
654a3478deSJeeja KP 	const char *name;
6618382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6718382eadSSubhransu S. Prusty };
6818382eadSSubhransu S. Prusty 
69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
71b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
72b7756edeSSubhransu S. Prusty };
73b7756edeSSubhransu S. Prusty 
74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
75b8a54545SSubhransu S. Prusty 	bool	monitor_present;
76b8a54545SSubhransu S. Prusty 	bool	eld_valid;
77b8a54545SSubhransu S. Prusty 	int	eld_size;
78b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
79b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
80b8a54545SSubhransu S. Prusty };
81b8a54545SSubhransu S. Prusty 
8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
8315b91447SSubhransu S. Prusty 	struct list_head head;
8418382eadSSubhransu S. Prusty 	hda_nid_t nid;
852acd8309SJeeja KP 	bool mst_capable;
86754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
87754695f9SJeeja KP 	int num_ports;
88754695f9SJeeja KP 	struct hdac_ext_device *edev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
980324e51bSJeeja KP 	const char *jack_pin;
990324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
1000324e51bSJeeja KP 	const char *output_pin;
10118382eadSSubhransu S. Prusty };
10218382eadSSubhransu S. Prusty 
1034a3478deSJeeja KP struct hdac_hdmi_pcm {
1044a3478deSJeeja KP 	struct list_head head;
1054a3478deSJeeja KP 	int pcm_id;
106e0e5d3e5SJeeja KP 	struct list_head port_list;
1074a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10862490016SJeeja KP 	struct snd_soc_jack *jack;
109c9bfb5d7SJeeja KP 	int stream_tag;
110c9bfb5d7SJeeja KP 	int channels;
111c9bfb5d7SJeeja KP 	int format;
112ab1eea19SJeeja KP 	bool chmap_set;
113ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
114ab1eea19SJeeja KP 	struct mutex lock;
115e0e5d3e5SJeeja KP 	int jack_event;
1164a3478deSJeeja KP };
1174a3478deSJeeja KP 
118754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11918382eadSSubhransu S. Prusty 	int dai_id;
120754695f9SJeeja KP 	struct hdac_hdmi_port *port;
12115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12218382eadSSubhransu S. Prusty };
12318382eadSSubhransu S. Prusty 
1245622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1255622bc95SPradeep Tewani 	unsigned int vendor_nid;
1265622bc95SPradeep Tewani };
1275622bc95SPradeep Tewani 
12818382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
129754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
13015b91447SSubhransu S. Prusty 	struct list_head pin_list;
13115b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1324a3478deSJeeja KP 	struct list_head pcm_list;
13315b91447SSubhransu S. Prusty 	int num_pin;
13415b91447SSubhransu S. Prusty 	int num_cvt;
135754695f9SJeeja KP 	int num_ports;
1364a3478deSJeeja KP 	struct mutex pin_mutex;
137bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1385622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
13918382eadSSubhransu S. Prusty };
14018382eadSSubhransu S. Prusty 
141c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
142c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
143c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
144c9bfb5d7SJeeja KP {
145c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1461de777feSJeeja KP 
147c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
148c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
149c9bfb5d7SJeeja KP 			break;
150c9bfb5d7SJeeja KP 	}
151c9bfb5d7SJeeja KP 
152c9bfb5d7SJeeja KP 	return pcm;
153c9bfb5d7SJeeja KP }
1541de777feSJeeja KP 
155e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
156e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
157e0e5d3e5SJeeja KP {
158e0e5d3e5SJeeja KP 	struct hdac_ext_device *edev = port->pin->edev;
159e0e5d3e5SJeeja KP 
1600324e51bSJeeja KP 	if (is_connect)
1610324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1620324e51bSJeeja KP 	else
1630324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1640324e51bSJeeja KP 
165e0e5d3e5SJeeja KP 	if (is_connect) {
166e0e5d3e5SJeeja KP 		/*
167e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
168e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
169e0e5d3e5SJeeja KP 		 * ports.
170e0e5d3e5SJeeja KP 		 */
171e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
172e0e5d3e5SJeeja KP 			dev_dbg(&edev->hdac.dev,
173e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
174e0e5d3e5SJeeja KP 					pcm->pcm_id);
17562490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
17662490016SJeeja KP 						SND_JACK_AVOUT);
177e0e5d3e5SJeeja KP 		}
178e0e5d3e5SJeeja KP 		pcm->jack_event++;
179e0e5d3e5SJeeja KP 	} else {
180e0e5d3e5SJeeja KP 		/*
181e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
182e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
183e0e5d3e5SJeeja KP 		 * to multiple ports.
184e0e5d3e5SJeeja KP 		 */
185e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
18662490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
187e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
188e0e5d3e5SJeeja KP 			pcm->jack_event--;
189e0e5d3e5SJeeja KP 	}
1900324e51bSJeeja KP 
1910324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
192e0e5d3e5SJeeja KP }
193e0e5d3e5SJeeja KP 
194fc181b04SJeeja KP /* MST supported verbs */
195fc181b04SJeeja KP /*
196fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
197fc181b04SJeeja KP  */
198fc181b04SJeeja KP static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
199fc181b04SJeeja KP {
200fc181b04SJeeja KP 	unsigned int caps;
201fc181b04SJeeja KP 	unsigned int type, param;
202fc181b04SJeeja KP 
203fc181b04SJeeja KP 	caps = get_wcaps(&hdac->hdac, nid);
204fc181b04SJeeja KP 	type = get_wcaps_type(caps);
205fc181b04SJeeja KP 
206fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
207fc181b04SJeeja KP 		return 0;
208fc181b04SJeeja KP 
209fc181b04SJeeja KP 	param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
210fc181b04SJeeja KP 					AC_PAR_DEVLIST_LEN);
211fc181b04SJeeja KP 	if (param == -1)
212fc181b04SJeeja KP 		return param;
213fc181b04SJeeja KP 
214fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
215fc181b04SJeeja KP }
216fc181b04SJeeja KP 
217fc181b04SJeeja KP /*
218fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
219fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
220fc181b04SJeeja KP  * is selected or MST is not supported.
221fc181b04SJeeja KP  */
222fc181b04SJeeja KP static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
223fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
224fc181b04SJeeja KP {
225fc181b04SJeeja KP 	return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
226fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
227fc181b04SJeeja KP }
228fc181b04SJeeja KP 
229fc181b04SJeeja KP /*
230fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
231fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
232fc181b04SJeeja KP  */
233fc181b04SJeeja KP static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
234fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
235fc181b04SJeeja KP {
236fc181b04SJeeja KP 	int num_ports;
237fc181b04SJeeja KP 
238fc181b04SJeeja KP 	if (!port->pin->mst_capable)
239fc181b04SJeeja KP 		return 0;
240fc181b04SJeeja KP 
241fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
242fc181b04SJeeja KP 	num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
243fc181b04SJeeja KP 
244fc181b04SJeeja KP 	if (num_ports < 0)
245fc181b04SJeeja KP 		return -EIO;
246fc181b04SJeeja KP 	/*
247fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
248fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
249fc181b04SJeeja KP 	 */
250fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
251fc181b04SJeeja KP 		return 0;
252fc181b04SJeeja KP 
253fc181b04SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
254fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
255fc181b04SJeeja KP 
256fc181b04SJeeja KP 	if (port->id != hdac_hdmi_port_select_get(hdac, port))
257fc181b04SJeeja KP 		return -EIO;
258fc181b04SJeeja KP 
259fc181b04SJeeja KP 	dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
260fc181b04SJeeja KP 
261fc181b04SJeeja KP 	return 0;
262fc181b04SJeeja KP }
263fc181b04SJeeja KP 
2642889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2652889099eSSubhransu S. Prusty 						int pcm_idx)
2662889099eSSubhransu S. Prusty {
2672889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2682889099eSSubhransu S. Prusty 
2692889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2702889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2712889099eSSubhransu S. Prusty 			return pcm;
2722889099eSSubhransu S. Prusty 	}
2732889099eSSubhransu S. Prusty 
2742889099eSSubhransu S. Prusty 	return NULL;
2752889099eSSubhransu S. Prusty }
2762889099eSSubhransu S. Prusty 
277e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
278e342ac08SSubhransu S. Prusty {
27951b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
280e342ac08SSubhransu S. Prusty 
28151b2c425SGeliang Tang 	return to_ehdac_device(hdac);
282e342ac08SSubhransu S. Prusty }
283e342ac08SSubhransu S. Prusty 
2842428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2852428bca3SSubhransu S. Prusty {
2862428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2872428bca3SSubhransu S. Prusty }
2882428bca3SSubhransu S. Prusty 
2892428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2902428bca3SSubhransu S. Prusty {
2912428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2922428bca3SSubhransu S. Prusty }
2932428bca3SSubhransu S. Prusty 
2942428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2952428bca3SSubhransu S. Prusty 						void *eld)
2962428bca3SSubhransu S. Prusty {
2972428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
2982428bca3SSubhransu S. Prusty 	int i;
2992428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3002428bca3SSubhransu S. Prusty 
3012428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3022428bca3SSubhransu S. Prusty 	if (!sad)
3032428bca3SSubhransu S. Prusty 		goto format_constraint;
3042428bca3SSubhransu S. Prusty 
3052428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3062428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3072428bca3SSubhransu S. Prusty 
3082428bca3SSubhransu S. Prusty 			/*
3092428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3102428bca3SSubhransu S. Prusty 			 * container so we set S32
3112428bca3SSubhransu S. Prusty 			 */
3122428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3132428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3142428bca3SSubhransu S. Prusty 		}
3152428bca3SSubhransu S. Prusty 	}
3162428bca3SSubhransu S. Prusty 
3172428bca3SSubhransu S. Prusty format_constraint:
3182428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3192428bca3SSubhransu S. Prusty 				formats);
3202428bca3SSubhransu S. Prusty 
3212428bca3SSubhransu S. Prusty }
3222428bca3SSubhransu S. Prusty 
323a657f1d0SSubhransu S. Prusty static void
324a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
325a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
326a657f1d0SSubhransu S. Prusty {
327a657f1d0SSubhransu S. Prusty 	int val;
328a657f1d0SSubhransu S. Prusty 
329a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
330a657f1d0SSubhransu S. Prusty 
331a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
332a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
333a657f1d0SSubhransu S. Prusty }
334a657f1d0SSubhransu S. Prusty 
335478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
336478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
337478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
338478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
339478f544eSSubhransu S. Prusty 
340478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
341478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
342478f544eSSubhransu S. Prusty 	u8 CXT04;
343478f544eSSubhransu S. Prusty 	u8 CA;
344478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
345478f544eSSubhransu S. Prusty };
346478f544eSSubhransu S. Prusty 
347a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
348754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
349a657f1d0SSubhransu S. Prusty {
350a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
351a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
352754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
353478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
354478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
355ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
356478f544eSSubhransu S. Prusty 	u8 *dip;
357a657f1d0SSubhransu S. Prusty 	int ret;
358a657f1d0SSubhransu S. Prusty 	int i;
359478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
360478f544eSSubhransu S. Prusty 	u8 conn_type;
361bcced704SSubhransu S. Prusty 	int channels, ca;
362a657f1d0SSubhransu S. Prusty 
363754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
364ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
365bcced704SSubhransu S. Prusty 
366bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
367ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
368bcced704SSubhransu S. Prusty 
369bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
370ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
371bcced704SSubhransu S. Prusty 
372754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
373478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
374a657f1d0SSubhransu S. Prusty 
375478f544eSSubhransu S. Prusty 	switch (conn_type) {
376478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
377478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
378478f544eSSubhransu S. Prusty 
379478f544eSSubhransu S. Prusty 		frame.channels = channels;
380bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
381a657f1d0SSubhransu S. Prusty 
382a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
383a657f1d0SSubhransu S. Prusty 		if (ret < 0)
384a657f1d0SSubhransu S. Prusty 			return ret;
385a657f1d0SSubhransu S. Prusty 
386478f544eSSubhransu S. Prusty 		break;
387478f544eSSubhransu S. Prusty 
388478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
389478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
390478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
391478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
392478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
393478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
394bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
395478f544eSSubhransu S. Prusty 
396478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
397478f544eSSubhransu S. Prusty 		break;
398478f544eSSubhransu S. Prusty 
399478f544eSSubhransu S. Prusty 	default:
400478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
401478f544eSSubhransu S. Prusty 						conn_type);
402478f544eSSubhransu S. Prusty 		return -EIO;
403478f544eSSubhransu S. Prusty 	}
404478f544eSSubhransu S. Prusty 
405a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
406ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
407ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
408a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
409a657f1d0SSubhransu S. Prusty 
410a657f1d0SSubhransu S. Prusty 
411a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
412ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
413478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
414391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
415ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
416391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
417478f544eSSubhransu S. Prusty 	} else {
418478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
419ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
420478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
421478f544eSSubhransu S. Prusty 	}
422a657f1d0SSubhransu S. Prusty 
423a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
424ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
425ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
426a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
427a657f1d0SSubhransu S. Prusty 
428a657f1d0SSubhransu S. Prusty 	return 0;
429a657f1d0SSubhransu S. Prusty }
430a657f1d0SSubhransu S. Prusty 
431c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
432c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
433c9bfb5d7SJeeja KP 		int slots, int slot_width)
434b0362adbSSubhransu S. Prusty {
435c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
436c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
437754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
438c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
439c9bfb5d7SJeeja KP 
440c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
441b0362adbSSubhransu S. Prusty 
442b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
443b0362adbSSubhransu S. Prusty 
444c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
445b0362adbSSubhransu S. Prusty 
446c9bfb5d7SJeeja KP 	if (pcm)
447c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
448bcced704SSubhransu S. Prusty 
449c9bfb5d7SJeeja KP 	return 0;
450b0362adbSSubhransu S. Prusty }
451b0362adbSSubhransu S. Prusty 
452b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
453b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
454b0362adbSSubhransu S. Prusty {
455b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
45654dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
457754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
458754695f9SJeeja KP 	struct hdac_hdmi_port *port;
459c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
460c9bfb5d7SJeeja KP 	int format;
461b0362adbSSubhransu S. Prusty 
46254dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
463754695f9SJeeja KP 	port = dai_map->port;
46454dfa1eaSSubhransu S. Prusty 
465754695f9SJeeja KP 	if (!port)
46654dfa1eaSSubhransu S. Prusty 		return -ENODEV;
46754dfa1eaSSubhransu S. Prusty 
468754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
469754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
470754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
471754695f9SJeeja KP 					port->pin->nid, port->id);
472b0362adbSSubhransu S. Prusty 		return -ENODEV;
473b0362adbSSubhransu S. Prusty 	}
474b0362adbSSubhransu S. Prusty 
475c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
476b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47766d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
478b0362adbSSubhransu S. Prusty 
479c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
480c9bfb5d7SJeeja KP 	if (!pcm)
481148569fdSSubhransu S. Prusty 		return -EIO;
482148569fdSSubhransu S. Prusty 
483c9bfb5d7SJeeja KP 	pcm->format = format;
484c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
485148569fdSSubhransu S. Prusty 
486148569fdSSubhransu S. Prusty 	return 0;
487148569fdSSubhransu S. Prusty }
488148569fdSSubhransu S. Prusty 
489754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
490754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
491754695f9SJeeja KP 					struct hdac_hdmi_port *port)
492148569fdSSubhransu S. Prusty {
493148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
494148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
495148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
496148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
497148569fdSSubhransu S. Prusty 		return -EINVAL;
498148569fdSSubhransu S. Prusty 	}
499148569fdSSubhransu S. Prusty 
5001b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(hdac, port) < 0)
5011b46ebd1SJeeja KP 		return -EIO;
5021b46ebd1SJeeja KP 
503754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
504754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
505754695f9SJeeja KP 	if (port->num_mux_nids == 0)
506754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
507754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
508754695f9SJeeja KP 						pin->nid, port->id);
509148569fdSSubhransu S. Prusty 
510754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
511754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
512148569fdSSubhransu S. Prusty 
513754695f9SJeeja KP 	return port->num_mux_nids;
514148569fdSSubhransu S. Prusty }
515148569fdSSubhransu S. Prusty 
516148569fdSSubhransu S. Prusty /*
517754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
518148569fdSSubhransu S. Prusty  *
519754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
520148569fdSSubhransu S. Prusty  *
521754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
522754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
523148569fdSSubhransu S. Prusty  * connected.
524148569fdSSubhransu S. Prusty  */
525754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
526148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
527148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
528148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
529148569fdSSubhransu S. Prusty {
530148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
531754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
532148569fdSSubhransu S. Prusty 	int ret, i;
533148569fdSSubhransu S. Prusty 
534148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
535148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
536e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
537e0e5d3e5SJeeja KP 				continue;
538148569fdSSubhransu S. Prusty 
539e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
540e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
541e0e5d3e5SJeeja KP 				ret = hdac_hdmi_query_port_connlist(edev,
542e0e5d3e5SJeeja KP 							port->pin, port);
543e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
544148569fdSSubhransu S. Prusty 				if (ret < 0)
545e0e5d3e5SJeeja KP 					continue;
546148569fdSSubhransu S. Prusty 
547754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
548e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
549e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
550e0e5d3e5SJeeja KP 						port->eld.eld_valid)
551754695f9SJeeja KP 						return port;
552148569fdSSubhransu S. Prusty 				}
553148569fdSSubhransu S. Prusty 			}
554e0e5d3e5SJeeja KP 		}
555e0e5d3e5SJeeja KP 	}
556148569fdSSubhransu S. Prusty 
557148569fdSSubhransu S. Prusty 	return NULL;
558148569fdSSubhransu S. Prusty }
559148569fdSSubhransu S. Prusty 
56054dfa1eaSSubhransu S. Prusty /*
56154dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
56254dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
56354dfa1eaSSubhransu S. Prusty  * doesn't fail.
56454dfa1eaSSubhransu S. Prusty  */
565b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
566b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
567b0362adbSSubhransu S. Prusty {
568b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
569b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
570754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
571148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
572754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5732428bca3SSubhransu S. Prusty 	int ret;
574b0362adbSSubhransu S. Prusty 
575b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
576b0362adbSSubhransu S. Prusty 
577148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
578754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
57954dfa1eaSSubhransu S. Prusty 
58054dfa1eaSSubhransu S. Prusty 	/*
58154dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
58254dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
58354dfa1eaSSubhransu S. Prusty 	 */
584754695f9SJeeja KP 	if (!port)
58554dfa1eaSSubhransu S. Prusty 		return 0;
586754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
587754695f9SJeeja KP 			(!port->eld.eld_valid)) {
588b0362adbSSubhransu S. Prusty 
58954dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
590754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
591754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
592754695f9SJeeja KP 			port->pin->nid, port->id);
593b8a54545SSubhransu S. Prusty 
59454dfa1eaSSubhransu S. Prusty 		return 0;
595b0362adbSSubhransu S. Prusty 	}
596b0362adbSSubhransu S. Prusty 
597754695f9SJeeja KP 	dai_map->port = port;
598b0362adbSSubhransu S. Prusty 
5992428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
600754695f9SJeeja KP 				port->eld.eld_buffer);
6012428bca3SSubhransu S. Prusty 	if (ret < 0)
6022428bca3SSubhransu S. Prusty 		return ret;
603b0362adbSSubhransu S. Prusty 
6042428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
605754695f9SJeeja KP 				port->eld.eld_buffer);
606b0362adbSSubhransu S. Prusty }
607b0362adbSSubhransu S. Prusty 
608b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
609b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
610b0362adbSSubhransu S. Prusty {
611b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
612b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
613754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
614ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
615b0362adbSSubhransu S. Prusty 
616b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
617b0362adbSSubhransu S. Prusty 
618ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
619bcced704SSubhransu S. Prusty 
620ab1eea19SJeeja KP 	if (pcm) {
621ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
622ab1eea19SJeeja KP 		pcm->chmap_set = false;
623ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
624ab1eea19SJeeja KP 		pcm->channels = 0;
625ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
626b0362adbSSubhransu S. Prusty 	}
627ab1eea19SJeeja KP 
628754695f9SJeeja KP 	if (dai_map->port)
629754695f9SJeeja KP 		dai_map->port = NULL;
63054dfa1eaSSubhransu S. Prusty }
631b0362adbSSubhransu S. Prusty 
63218382eadSSubhransu S. Prusty static int
63318382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
63418382eadSSubhransu S. Prusty {
635bcced704SSubhransu S. Prusty 	unsigned int chans;
636bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
637bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
63818382eadSSubhransu S. Prusty 	int err;
63918382eadSSubhransu S. Prusty 
640bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
641bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
642bcced704SSubhransu S. Prusty 
643bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
644bcced704SSubhransu S. Prusty 
645bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
646bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
647bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
64818382eadSSubhransu S. Prusty 
64918382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
65018382eadSSubhransu S. Prusty 			&cvt->params.rates,
65118382eadSSubhransu S. Prusty 			&cvt->params.formats,
65218382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
65318382eadSSubhransu S. Prusty 	if (err < 0)
65418382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
65518382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
65618382eadSSubhransu S. Prusty 			cvt->nid, err);
65718382eadSSubhransu S. Prusty 
65818382eadSSubhransu S. Prusty 	return err;
65918382eadSSubhransu S. Prusty }
66018382eadSSubhransu S. Prusty 
66179f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
662c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
663c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
664c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
665c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
666c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
66718382eadSSubhransu S. Prusty {
66818382eadSSubhransu S. Prusty 	w->id = id;
66979f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
67079f4e922SSubhransu S. Prusty 	if (!w->name)
67179f4e922SSubhransu S. Prusty 		return -ENOMEM;
67279f4e922SSubhransu S. Prusty 
67318382eadSSubhransu S. Prusty 	w->sname = stream;
67418382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
67518382eadSSubhransu S. Prusty 	w->shift = 0;
67679f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
67779f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
67879f4e922SSubhransu S. Prusty 	w->priv = priv;
679c9bfb5d7SJeeja KP 	w->event = event;
680c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
68179f4e922SSubhransu S. Prusty 
68279f4e922SSubhransu S. Prusty 	return 0;
68318382eadSSubhransu S. Prusty }
68418382eadSSubhransu S. Prusty 
68518382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
68679f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
68779f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
68879f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
68918382eadSSubhransu S. Prusty {
69018382eadSSubhransu S. Prusty 	route->sink = sink;
69118382eadSSubhransu S. Prusty 	route->source = src;
69218382eadSSubhransu S. Prusty 	route->control = control;
69379f4e922SSubhransu S. Prusty 	route->connected = handler;
69418382eadSSubhransu S. Prusty }
69518382eadSSubhransu S. Prusty 
6964a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
697754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6984a3478deSJeeja KP {
6994a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
7004a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
701e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7024a3478deSJeeja KP 
7034a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
704e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
705754695f9SJeeja KP 			continue;
706754695f9SJeeja KP 
707e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
708e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7094a3478deSJeeja KP 				return pcm;
7104a3478deSJeeja KP 		}
711e0e5d3e5SJeeja KP 	}
7124a3478deSJeeja KP 
7134a3478deSJeeja KP 	return NULL;
7144a3478deSJeeja KP }
7154a3478deSJeeja KP 
716c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
717c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
718c9bfb5d7SJeeja KP {
719c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
720c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
721c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
722c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
723c9bfb5d7SJeeja KP 	}
724c9bfb5d7SJeeja KP }
725c9bfb5d7SJeeja KP 
726c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
727c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
728c9bfb5d7SJeeja KP {
729c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
730c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
731c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
732c9bfb5d7SJeeja KP }
733c9bfb5d7SJeeja KP 
734c9bfb5d7SJeeja KP 
735c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
736c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
737c9bfb5d7SJeeja KP {
738754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
739c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
740c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
741c9bfb5d7SJeeja KP 
742c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
743c9bfb5d7SJeeja KP 			__func__, w->name, event);
744c9bfb5d7SJeeja KP 
745754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
746c9bfb5d7SJeeja KP 	if (!pcm)
747c9bfb5d7SJeeja KP 		return -EIO;
748c9bfb5d7SJeeja KP 
7491b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7501b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
7511b46ebd1SJeeja KP 		return -EIO;
7521b46ebd1SJeeja KP 
753c9bfb5d7SJeeja KP 	switch (event) {
754c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
755754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
756c9bfb5d7SJeeja KP 
757c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
758754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
759c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
760c9bfb5d7SJeeja KP 
761754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
762c9bfb5d7SJeeja KP 
763754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
764c9bfb5d7SJeeja KP 
765c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
766754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
767c9bfb5d7SJeeja KP 
768c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
769754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
770c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
771c9bfb5d7SJeeja KP 
772754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
773c9bfb5d7SJeeja KP 		break;
774c9bfb5d7SJeeja KP 
775c9bfb5d7SJeeja KP 	}
776c9bfb5d7SJeeja KP 
777c9bfb5d7SJeeja KP 	return 0;
778c9bfb5d7SJeeja KP }
779c9bfb5d7SJeeja KP 
780c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
781c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
782c9bfb5d7SJeeja KP {
783c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
784c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
785c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
786c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
787c9bfb5d7SJeeja KP 
788c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
789c9bfb5d7SJeeja KP 			__func__, w->name, event);
790c9bfb5d7SJeeja KP 
791c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
792c9bfb5d7SJeeja KP 	if (!pcm)
793c9bfb5d7SJeeja KP 		return -EIO;
794c9bfb5d7SJeeja KP 
795c9bfb5d7SJeeja KP 	switch (event) {
796c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
797c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
798c9bfb5d7SJeeja KP 
799c9bfb5d7SJeeja KP 		/* Enable transmission */
800c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
801c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
802c9bfb5d7SJeeja KP 
803c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
804c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
805c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
806c9bfb5d7SJeeja KP 
807c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
808c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
809c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
810c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
811c9bfb5d7SJeeja KP 		break;
812c9bfb5d7SJeeja KP 
813c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
814c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
815c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
816c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
817c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
818c9bfb5d7SJeeja KP 
819c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
820c9bfb5d7SJeeja KP 		break;
821c9bfb5d7SJeeja KP 
822c9bfb5d7SJeeja KP 	}
823c9bfb5d7SJeeja KP 
824c9bfb5d7SJeeja KP 	return 0;
825c9bfb5d7SJeeja KP }
826c9bfb5d7SJeeja KP 
827c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
828c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
829c9bfb5d7SJeeja KP {
830754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
831c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
832c9bfb5d7SJeeja KP 	int mux_idx;
833c9bfb5d7SJeeja KP 
834c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
835c9bfb5d7SJeeja KP 			__func__, w->name, event);
836c9bfb5d7SJeeja KP 
837c9bfb5d7SJeeja KP 	if (!kc)
838c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
839c9bfb5d7SJeeja KP 
840c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8411b46ebd1SJeeja KP 
8421b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8431b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
8441b46ebd1SJeeja KP 		return -EIO;
8451b46ebd1SJeeja KP 
846c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
847754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
848c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
849c9bfb5d7SJeeja KP 	}
850c9bfb5d7SJeeja KP 
851c9bfb5d7SJeeja KP 	return 0;
852c9bfb5d7SJeeja KP }
853c9bfb5d7SJeeja KP 
8544a3478deSJeeja KP /*
8554a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8564a3478deSJeeja KP  */
857754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8584a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8594a3478deSJeeja KP {
8604a3478deSJeeja KP 	int ret;
861e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8624a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8634a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8644a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
865754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8664a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
8674a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
8684a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8694a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8704a3478deSJeeja KP 
8714a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8724a3478deSJeeja KP 	if (ret < 0)
8734a3478deSJeeja KP 		return ret;
8744a3478deSJeeja KP 
875754695f9SJeeja KP 	if (port == NULL)
876754695f9SJeeja KP 		return -EINVAL;
877754695f9SJeeja KP 
8784a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8794a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
880e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
881e0e5d3e5SJeeja KP 			continue;
882e0e5d3e5SJeeja KP 
883e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
884e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
885e0e5d3e5SJeeja KP 					p->pin == port->pin) {
886e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
887e0e5d3e5SJeeja KP 				list_del(&p->head);
888e0e5d3e5SJeeja KP 			}
889e0e5d3e5SJeeja KP 		}
890e0e5d3e5SJeeja KP 	}
8914a3478deSJeeja KP 
8924a3478deSJeeja KP 	/*
8934a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8944a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8954a3478deSJeeja KP 	 */
896e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
897e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
898e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
899754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
900e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
9014a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9024a3478deSJeeja KP 				return ret;
9034a3478deSJeeja KP 			}
9044a3478deSJeeja KP 		}
905e0e5d3e5SJeeja KP 	}
9064a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9074a3478deSJeeja KP 
9084a3478deSJeeja KP 	return ret;
9094a3478deSJeeja KP }
9104a3478deSJeeja KP 
91179f4e922SSubhransu S. Prusty /*
91279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
91379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
91479f4e922SSubhransu S. Prusty  * widget runtime.
91579f4e922SSubhransu S. Prusty  *
91679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
91779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
91879f4e922SSubhransu S. Prusty  * "NONE"
91979f4e922SSubhransu S. Prusty  */
920754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
921754695f9SJeeja KP 				struct hdac_hdmi_port *port,
92279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
92379f4e922SSubhransu S. Prusty 				const char *widget_name)
92418382eadSSubhransu S. Prusty {
92579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
926754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
92779f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
92879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
92979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
93079f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
93179f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
93279f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
93379f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
93479f4e922SSubhransu S. Prusty 	int i = 0;
93579f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
93618382eadSSubhransu S. Prusty 
93779f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
93879f4e922SSubhransu S. Prusty 	if (!kc)
93979f4e922SSubhransu S. Prusty 		return -ENOMEM;
94018382eadSSubhransu S. Prusty 
94179f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
94279f4e922SSubhransu S. Prusty 	if (!se)
94379f4e922SSubhransu S. Prusty 		return -ENOMEM;
94418382eadSSubhransu S. Prusty 
94570e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
94670e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
94779f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
94879f4e922SSubhransu S. Prusty 	if (!kc->name)
94979f4e922SSubhransu S. Prusty 		return -ENOMEM;
95018382eadSSubhransu S. Prusty 
95179f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
95279f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
95379f4e922SSubhransu S. Prusty 	kc->access = 0;
95479f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
955754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95679f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
95779f4e922SSubhransu S. Prusty 
95879f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
95979f4e922SSubhransu S. Prusty 
96079f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
96179f4e922SSubhransu S. Prusty 	se->items = num_items;
96279f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
96379f4e922SSubhransu S. Prusty 
96479f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
96579f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
96679f4e922SSubhransu S. Prusty 	if (!items[i])
96779f4e922SSubhransu S. Prusty 		return -ENOMEM;
96879f4e922SSubhransu S. Prusty 
96979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
97079f4e922SSubhransu S. Prusty 		i++;
97179f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
97279f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
97379f4e922SSubhransu S. Prusty 		if (!items[i])
97479f4e922SSubhransu S. Prusty 			return -ENOMEM;
97579f4e922SSubhransu S. Prusty 	}
97679f4e922SSubhransu S. Prusty 
97779f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
97879f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
97979f4e922SSubhransu S. Prusty 	if (!se->texts)
98079f4e922SSubhransu S. Prusty 		return -ENOMEM;
98179f4e922SSubhransu S. Prusty 
98279f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
983754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
984c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
985c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98679f4e922SSubhransu S. Prusty }
98779f4e922SSubhransu S. Prusty 
98879f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
98979f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
99079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
99179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
99279f4e922SSubhransu S. Prusty {
99379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
99479f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
99579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
996754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
99779f4e922SSubhransu S. Prusty 	int i, j;
99879f4e922SSubhransu S. Prusty 
999754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
100079f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
100179f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
100279f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
100379f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
100479f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
100579f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100679f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
100779f4e922SSubhransu S. Prusty 
100879f4e922SSubhransu S. Prusty 			rindex++;
100979f4e922SSubhransu S. Prusty 		}
101079f4e922SSubhransu S. Prusty 
101179f4e922SSubhransu S. Prusty 		mux_index++;
101279f4e922SSubhransu S. Prusty 	}
101379f4e922SSubhransu S. Prusty }
101479f4e922SSubhransu S. Prusty 
101579f4e922SSubhransu S. Prusty /*
101679f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
101779f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1018754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1019754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
102079f4e922SSubhransu S. Prusty  *
1021754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1022754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
102379f4e922SSubhransu S. Prusty  *
102479f4e922SSubhransu S. Prusty  * Routes are added as below:
1025754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1026754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
102779f4e922SSubhransu S. Prusty  *
102879f4e922SSubhransu S. Prusty  * Total route elements:
1029754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
103079f4e922SSubhransu S. Prusty  */
103179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
103279f4e922SSubhransu S. Prusty {
103379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
103479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
103579f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
103679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
103779f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
103879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
103979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
104079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1041754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
104279f4e922SSubhransu S. Prusty 
104379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
104479f4e922SSubhransu S. Prusty 		return -EINVAL;
104579f4e922SSubhransu S. Prusty 
1046754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1047754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
104879f4e922SSubhransu S. Prusty 				GFP_KERNEL);
104979f4e922SSubhransu S. Prusty 
105079f4e922SSubhransu S. Prusty 	if (!widgets)
105179f4e922SSubhransu S. Prusty 		return -ENOMEM;
105279f4e922SSubhransu S. Prusty 
105379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
105479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
105579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1057c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1058c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1059c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1060c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
106179f4e922SSubhransu S. Prusty 		if (ret < 0)
106279f4e922SSubhransu S. Prusty 			return ret;
106379f4e922SSubhransu S. Prusty 		i++;
106479f4e922SSubhransu S. Prusty 	}
106579f4e922SSubhransu S. Prusty 
106679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1067754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1068754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1069754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
107079f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1071754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1072c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1073c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1074754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1075754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107679f4e922SSubhransu S. Prusty 			if (ret < 0)
107779f4e922SSubhransu S. Prusty 				return ret;
10780324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
107979f4e922SSubhransu S. Prusty 			i++;
108079f4e922SSubhransu S. Prusty 		}
1081754695f9SJeeja KP 	}
108279f4e922SSubhransu S. Prusty 
108379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
108479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1085754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1086754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1087754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1088754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1089754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
109079f4e922SSubhransu S. Prusty 						widget_name);
109179f4e922SSubhransu S. Prusty 			if (ret < 0)
109279f4e922SSubhransu S. Prusty 				return ret;
109379f4e922SSubhransu S. Prusty 			i++;
109479f4e922SSubhransu S. Prusty 
109579f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109679f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
109779f4e922SSubhransu S. Prusty 
109879f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
109979f4e922SSubhransu S. Prusty 			num_routes++;
110079f4e922SSubhransu S. Prusty 		}
1101754695f9SJeeja KP 	}
110279f4e922SSubhransu S. Prusty 
110379f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
110479f4e922SSubhransu S. Prusty 							GFP_KERNEL);
110579f4e922SSubhransu S. Prusty 	if (!route)
110679f4e922SSubhransu S. Prusty 		return -ENOMEM;
110779f4e922SSubhransu S. Prusty 
110879f4e922SSubhransu S. Prusty 	i = 0;
110979f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
111079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1111754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
111279f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1113754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1114754695f9SJeeja KP 						hdmi->num_pin;
111579f4e922SSubhransu S. Prusty 
111679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
111779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
111879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
111979f4e922SSubhransu S. Prusty 			i++;
1120754695f9SJeeja KP 		}
112179f4e922SSubhransu S. Prusty 	}
112279f4e922SSubhransu S. Prusty 
112379f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
112479f4e922SSubhransu S. Prusty 
112579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1126754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
112779f4e922SSubhransu S. Prusty 
112879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
112979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
113079f4e922SSubhransu S. Prusty 
113179f4e922SSubhransu S. Prusty 	return 0;
113279f4e922SSubhransu S. Prusty 
113318382eadSSubhransu S. Prusty }
113418382eadSSubhransu S. Prusty 
113515b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
113618382eadSSubhransu S. Prusty {
113715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1138754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
113915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1140148569fdSSubhransu S. Prusty 	int dai_id = 0;
114118382eadSSubhransu S. Prusty 
1142148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
114315b91447SSubhransu S. Prusty 		return -EINVAL;
114418382eadSSubhransu S. Prusty 
1145148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1146148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1147148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
114815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
114918382eadSSubhransu S. Prusty 
1150148569fdSSubhransu S. Prusty 		dai_id++;
1151148569fdSSubhransu S. Prusty 
1152148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1153148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1154148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1155148569fdSSubhransu S. Prusty 			break;
1156148569fdSSubhransu S. Prusty 		}
1157148569fdSSubhransu S. Prusty 	}
115818382eadSSubhransu S. Prusty 
115915b91447SSubhransu S. Prusty 	return 0;
116015b91447SSubhransu S. Prusty }
116115b91447SSubhransu S. Prusty 
116215b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
116315b91447SSubhransu S. Prusty {
116415b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
116515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11664a3478deSJeeja KP 	char name[NAME_SIZE];
116715b91447SSubhransu S. Prusty 
116815b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
116915b91447SSubhransu S. Prusty 	if (!cvt)
117015b91447SSubhransu S. Prusty 		return -ENOMEM;
117115b91447SSubhransu S. Prusty 
117215b91447SSubhransu S. Prusty 	cvt->nid = nid;
11734a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11744a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
117515b91447SSubhransu S. Prusty 
117615b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
117715b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
117815b91447SSubhransu S. Prusty 
117915b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
118015b91447SSubhransu S. Prusty }
118115b91447SSubhransu S. Prusty 
1182f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1183754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1184b7756edeSSubhransu S. Prusty {
1185f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1186f6fa11a3SSandeep Tayal 
1187754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1188f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1189f6fa11a3SSandeep Tayal 
1190f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1191f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1192f6fa11a3SSandeep Tayal 		return -EINVAL;
1193b7756edeSSubhransu S. Prusty 	}
1194b7756edeSSubhransu S. Prusty 
1195754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1196f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1197f6fa11a3SSandeep Tayal 
1198f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1199f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1200f6fa11a3SSandeep Tayal 		return -EINVAL;
1201f6fa11a3SSandeep Tayal 	}
1202f6fa11a3SSandeep Tayal 
1203754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1204f6fa11a3SSandeep Tayal 
1205f6fa11a3SSandeep Tayal 	return 0;
1206f6fa11a3SSandeep Tayal }
1207f6fa11a3SSandeep Tayal 
1208754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1209754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1210b8a54545SSubhransu S. Prusty {
1211b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
12124a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
12134a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1214754695f9SJeeja KP 	int size = 0;
12152acd8309SJeeja KP 	int port_id = -1;
1216754695f9SJeeja KP 
1217754695f9SJeeja KP 	if (!hdmi)
1218754695f9SJeeja KP 		return;
12194a3478deSJeeja KP 
12202acd8309SJeeja KP 	/*
12212acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12222acd8309SJeeja KP 	 * to be -1.
12232acd8309SJeeja KP 	 */
12244a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1225754695f9SJeeja KP 	port->eld.monitor_present = false;
1226f6fa11a3SSandeep Tayal 
12272acd8309SJeeja KP 	if (pin->mst_capable)
12282acd8309SJeeja KP 		port_id = port->id;
12292acd8309SJeeja KP 
12302acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1231754695f9SJeeja KP 				&port->eld.monitor_present,
1232754695f9SJeeja KP 				port->eld.eld_buffer,
1233f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1234f6fa11a3SSandeep Tayal 
1235f6fa11a3SSandeep Tayal 	if (size > 0) {
1236f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1237754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1238f6fa11a3SSandeep Tayal 			size = -EINVAL;
1239f6fa11a3SSandeep Tayal 	}
1240f6fa11a3SSandeep Tayal 
1241f6fa11a3SSandeep Tayal 	if (size > 0) {
1242754695f9SJeeja KP 		port->eld.eld_valid = true;
1243754695f9SJeeja KP 		port->eld.eld_size = size;
1244f6fa11a3SSandeep Tayal 	} else {
1245754695f9SJeeja KP 		port->eld.eld_valid = false;
1246754695f9SJeeja KP 		port->eld.eld_size = 0;
1247f6fa11a3SSandeep Tayal 	}
1248b8a54545SSubhransu S. Prusty 
1249754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12504a3478deSJeeja KP 
1251754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1252b8a54545SSubhransu S. Prusty 
1253e0e5d3e5SJeeja KP 		dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1254754695f9SJeeja KP 						__func__, pin->nid, port->id);
12554a3478deSJeeja KP 
12564a3478deSJeeja KP 		/*
12574a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12584a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12594a3478deSJeeja KP 		 * control select.
12604a3478deSJeeja KP 		 */
1261e0e5d3e5SJeeja KP 		if (pcm)
1262e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12634a3478deSJeeja KP 
12644a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1265f6fa11a3SSandeep Tayal 		return;
1266b8a54545SSubhransu S. Prusty 	}
1267b8a54545SSubhransu S. Prusty 
1268754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1269e0e5d3e5SJeeja KP 		if (pcm)
1270e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12714a3478deSJeeja KP 
1272f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1273754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1274754695f9SJeeja KP 
1275754695f9SJeeja KP 	}
1276754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12774a3478deSJeeja KP }
12784a3478deSJeeja KP 
1279754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1280754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1281754695f9SJeeja KP {
1282754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1283754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1284754695f9SJeeja KP 	int i;
1285754695f9SJeeja KP 
1286754695f9SJeeja KP 	/*
1287754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1288754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1289754695f9SJeeja KP 	 * implemented.
1290754695f9SJeeja KP 	 */
1291754695f9SJeeja KP 
1292754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1293754695f9SJeeja KP 	if (!ports)
1294754695f9SJeeja KP 		return -ENOMEM;
1295754695f9SJeeja KP 
1296754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1297754695f9SJeeja KP 		ports[i].id = i;
1298754695f9SJeeja KP 		ports[i].pin = pin;
1299754695f9SJeeja KP 	}
1300754695f9SJeeja KP 	pin->ports = ports;
1301754695f9SJeeja KP 	pin->num_ports = max_ports;
1302754695f9SJeeja KP 	return 0;
1303b8a54545SSubhransu S. Prusty }
1304b8a54545SSubhransu S. Prusty 
130515b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
130615b91447SSubhransu S. Prusty {
130715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
130815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1309754695f9SJeeja KP 	int ret;
131015b91447SSubhransu S. Prusty 
131115b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
131215b91447SSubhransu S. Prusty 	if (!pin)
131315b91447SSubhransu S. Prusty 		return -ENOMEM;
131415b91447SSubhransu S. Prusty 
131515b91447SSubhransu S. Prusty 	pin->nid = nid;
13162acd8309SJeeja KP 	pin->mst_capable = false;
1317754695f9SJeeja KP 	pin->edev = edev;
1318754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1319754695f9SJeeja KP 	if (ret < 0)
1320754695f9SJeeja KP 		return ret;
132115b91447SSubhransu S. Prusty 
132215b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
132315b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1324754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1325b8a54545SSubhransu S. Prusty 
132615b91447SSubhransu S. Prusty 	return 0;
132718382eadSSubhransu S. Prusty }
132818382eadSSubhransu S. Prusty 
1329211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
13305622bc95SPradeep Tewani #define INTEL_GLK_VENDOR_NID 0x0b
1331211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1332211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1333211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1334211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1335211caab7SSubhransu S. Prusty 
1336211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1337211caab7SSubhransu S. Prusty {
1338211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
13395622bc95SPradeep Tewani 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
13405622bc95SPradeep Tewani 	struct hdac_hdmi_priv *hdmi = edev->private_data;
13415622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1342211caab7SSubhransu S. Prusty 
13435622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1344211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1345211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1346211caab7SSubhransu S. Prusty 		return;
1347211caab7SSubhransu S. Prusty 
1348211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
13495622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1350211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1351211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1352211caab7SSubhransu S. Prusty 		return;
1353211caab7SSubhransu S. Prusty }
1354211caab7SSubhransu S. Prusty 
1355211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1356211caab7SSubhransu S. Prusty {
1357211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
13585622bc95SPradeep Tewani 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
13595622bc95SPradeep Tewani 	struct hdac_hdmi_priv *hdmi = edev->private_data;
13605622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1361211caab7SSubhransu S. Prusty 
13625622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1363211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1364211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1365211caab7SSubhransu S. Prusty 		return;
1366211caab7SSubhransu S. Prusty 
1367211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1368211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
13695622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1370211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1371211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1372211caab7SSubhransu S. Prusty 		return;
1373211caab7SSubhransu S. Prusty 
1374211caab7SSubhransu S. Prusty }
1375211caab7SSubhransu S. Prusty 
137661b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
137717a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
137817a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
137917a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1380c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
138117a42c45SSubhransu S. Prusty };
138217a42c45SSubhransu S. Prusty 
138317a42c45SSubhransu S. Prusty /*
138417a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
138517a42c45SSubhransu S. Prusty  * based on the number of converter queried.
138617a42c45SSubhransu S. Prusty  */
138717a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
138817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
138917a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
139017a42c45SSubhransu S. Prusty {
139117a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
139217a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
139317a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
139417a42c45SSubhransu S. Prusty 	int i = 0;
139517a42c45SSubhransu S. Prusty 	u32 rates, bps;
139617a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
139717a42c45SSubhransu S. Prusty 	u64 formats;
139817a42c45SSubhransu S. Prusty 	int ret;
139917a42c45SSubhransu S. Prusty 
140017a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
140117a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
140217a42c45SSubhransu S. Prusty 			GFP_KERNEL);
140317a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
140417a42c45SSubhransu S. Prusty 		return -ENOMEM;
140517a42c45SSubhransu S. Prusty 
140617a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
140717a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
140817a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
140917a42c45SSubhransu S. Prusty 		if (ret)
141017a42c45SSubhransu S. Prusty 			return ret;
141117a42c45SSubhransu S. Prusty 
141217a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
141317a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
141417a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
141517a42c45SSubhransu S. Prusty 
141617a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
141717a42c45SSubhransu S. Prusty 			return -ENOMEM;
141817a42c45SSubhransu S. Prusty 
141917a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
142017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
142117a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
142217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142317a42c45SSubhransu S. Prusty 			return -ENOMEM;
142417a42c45SSubhransu S. Prusty 
142517a42c45SSubhransu S. Prusty 		/*
142617a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
142717a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
142817a42c45SSubhransu S. Prusty 		 */
142917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
143017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
143117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
143217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
143566d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
143617a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
143717a42c45SSubhransu S. Prusty 		i++;
143817a42c45SSubhransu S. Prusty 	}
143917a42c45SSubhransu S. Prusty 
144017a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
144117a42c45SSubhransu S. Prusty 
144217a42c45SSubhransu S. Prusty 	return 0;
144317a42c45SSubhransu S. Prusty }
144417a42c45SSubhransu S. Prusty 
144518382eadSSubhransu S. Prusty /*
144618382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
144718382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
144818382eadSSubhransu S. Prusty  */
144917a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
145017a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
145118382eadSSubhransu S. Prusty {
145218382eadSSubhransu S. Prusty 	hda_nid_t nid;
14533c83ac23SSudip Mukherjee 	int i, num_nodes;
145418382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
145518382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
14561c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
14571c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_pin *temp_pin, *pin_next;
145815b91447SSubhransu S. Prusty 	int ret;
145918382eadSSubhransu S. Prusty 
1460211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1461211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1462211caab7SSubhransu S. Prusty 
14633c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1464541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
146518382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
146618382eadSSubhransu S. Prusty 		return -EINVAL;
146718382eadSSubhransu S. Prusty 	}
146818382eadSSubhransu S. Prusty 
14693c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
147018382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
147118382eadSSubhransu S. Prusty 
147218382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
147318382eadSSubhransu S. Prusty 		unsigned int caps;
147418382eadSSubhransu S. Prusty 		unsigned int type;
147518382eadSSubhransu S. Prusty 
147618382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
147718382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147818382eadSSubhransu S. Prusty 
147918382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
148018382eadSSubhransu S. Prusty 			continue;
148118382eadSSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty 		switch (type) {
148318382eadSSubhransu S. Prusty 
148418382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
148515b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
148615b91447SSubhransu S. Prusty 			if (ret < 0)
14871c0a7de2SSubhransu S. Prusty 				goto free_widgets;
148818382eadSSubhransu S. Prusty 			break;
148918382eadSSubhransu S. Prusty 
149018382eadSSubhransu S. Prusty 		case AC_WID_PIN:
149115b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
149215b91447SSubhransu S. Prusty 			if (ret < 0)
14931c0a7de2SSubhransu S. Prusty 				goto free_widgets;
149418382eadSSubhransu S. Prusty 			break;
149518382eadSSubhransu S. Prusty 		}
149618382eadSSubhransu S. Prusty 	}
149718382eadSSubhransu S. Prusty 
149818382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
149918382eadSSubhransu S. Prusty 
15001c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
15011c0a7de2SSubhransu S. Prusty 		ret = -EIO;
15021c0a7de2SSubhransu S. Prusty 		goto free_widgets;
15031c0a7de2SSubhransu S. Prusty 	}
150418382eadSSubhransu S. Prusty 
150517a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
150617a42c45SSubhransu S. Prusty 	if (ret) {
150717a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
150817a42c45SSubhransu S. Prusty 							ret);
15091c0a7de2SSubhransu S. Prusty 		goto free_widgets;
151017a42c45SSubhransu S. Prusty 	}
151117a42c45SSubhransu S. Prusty 
151217a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15131c0a7de2SSubhransu S. Prusty 	ret = hdac_hdmi_init_dai_map(edev);
15141c0a7de2SSubhransu S. Prusty 	if (ret < 0)
15151c0a7de2SSubhransu S. Prusty 		goto free_widgets;
151617a42c45SSubhransu S. Prusty 
15171c0a7de2SSubhransu S. Prusty 	return ret;
15181c0a7de2SSubhransu S. Prusty 
15191c0a7de2SSubhransu S. Prusty free_widgets:
15201c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
15211c0a7de2SSubhransu S. Prusty 		list_del(&temp_cvt->head);
15221c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt->name);
15231c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt);
15241c0a7de2SSubhransu S. Prusty 	}
15251c0a7de2SSubhransu S. Prusty 
15261c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
15271c0a7de2SSubhransu S. Prusty 		for (i = 0; i < temp_pin->num_ports; i++)
15281c0a7de2SSubhransu S. Prusty 			temp_pin->ports[i].pin = NULL;
15291c0a7de2SSubhransu S. Prusty 		kfree(temp_pin->ports);
15301c0a7de2SSubhransu S. Prusty 		list_del(&temp_pin->head);
15311c0a7de2SSubhransu S. Prusty 		kfree(temp_pin);
15321c0a7de2SSubhransu S. Prusty 	}
15331c0a7de2SSubhransu S. Prusty 
15341c0a7de2SSubhransu S. Prusty 	return ret;
153518382eadSSubhransu S. Prusty }
153618382eadSSubhransu S. Prusty 
1537f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1538b8a54545SSubhransu S. Prusty {
1539b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1540b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1541754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1542754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1543b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
15442acd8309SJeeja KP 	int i;
1545b8a54545SSubhransu S. Prusty 
1546b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1547b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1548b8a54545SSubhransu S. Prusty 
1549754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1550754695f9SJeeja KP 							pin_nid, pipe);
1551b8a54545SSubhransu S. Prusty 
1552b8a54545SSubhransu S. Prusty 	/*
1553b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1554b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1555b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1556b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1557b8a54545SSubhransu S. Prusty 	 */
1558b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1559b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1560b8a54545SSubhransu S. Prusty 		return;
1561b8a54545SSubhransu S. Prusty 
1562b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1563b8a54545SSubhransu S. Prusty 		return;
1564b8a54545SSubhransu S. Prusty 
1565b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1566754695f9SJeeja KP 		if (pin->nid != pin_nid)
1567754695f9SJeeja KP 			continue;
1568754695f9SJeeja KP 
1569754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1570754695f9SJeeja KP 		if (pipe == -1) {
15712acd8309SJeeja KP 			pin->mst_capable = false;
1572754695f9SJeeja KP 			/* if not MST, default is port[0] */
1573754695f9SJeeja KP 			hport = &pin->ports[0];
15742acd8309SJeeja KP 		} else {
15752acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15762acd8309SJeeja KP 				pin->mst_capable = true;
15772acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15782acd8309SJeeja KP 					hport = &pin->ports[i];
157904c8f2bfSJeeja KP 					break;
15802acd8309SJeeja KP 				}
1581b8a54545SSubhransu S. Prusty 			}
1582b8a54545SSubhransu S. Prusty 		}
1583b8a54545SSubhransu S. Prusty 
158404c8f2bfSJeeja KP 		if (hport)
1585754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1586754695f9SJeeja KP 	}
1587754695f9SJeeja KP 
158804c8f2bfSJeeja KP }
158904c8f2bfSJeeja KP 
1590b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1591b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1592b8a54545SSubhransu S. Prusty };
1593b8a54545SSubhransu S. Prusty 
15942889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15952889099eSSubhransu S. Prusty 						int device)
15962889099eSSubhransu S. Prusty {
15972889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15982889099eSSubhransu S. Prusty 
15992889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
16002889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16012889099eSSubhransu S. Prusty 			return rtd->pcm;
16022889099eSSubhransu S. Prusty 	}
16032889099eSSubhransu S. Prusty 
16042889099eSSubhransu S. Prusty 	return NULL;
16052889099eSSubhransu S. Prusty }
16062889099eSSubhransu S. Prusty 
16070324e51bSJeeja KP /* create jack pin kcontrols */
16080324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16090324e51bSJeeja KP 				    struct hdac_ext_device *edev)
16100324e51bSJeeja KP {
16110324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16120324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16130324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16140324e51bSJeeja KP 	char *name;
16150324e51bSJeeja KP 	int i = 0, j;
16160324e51bSJeeja KP 	struct snd_soc_codec *codec = edev->scodec;
16170324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16180324e51bSJeeja KP 
16190324e51bSJeeja KP 	kc = devm_kcalloc(codec->dev, hdmi->num_ports,
16200324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16210324e51bSJeeja KP 
16220324e51bSJeeja KP 	if (!kc)
16230324e51bSJeeja KP 		return -ENOMEM;
16240324e51bSJeeja KP 
16250324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16260324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16270324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16280324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16290324e51bSJeeja KP 			name = devm_kstrdup(codec->dev, xname, GFP_KERNEL);
16300324e51bSJeeja KP 			if (!name)
16310324e51bSJeeja KP 				return -ENOMEM;
16320324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
16330324e51bSJeeja KP 			kc[i].name = devm_kstrdup(codec->dev, kc_name,
16340324e51bSJeeja KP 							GFP_KERNEL);
16350324e51bSJeeja KP 			if (!kc[i].name)
16360324e51bSJeeja KP 				return -ENOMEM;
16370324e51bSJeeja KP 
16380324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16390324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16400324e51bSJeeja KP 			kc[i].access = 0;
16410324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16420324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16430324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16440324e51bSJeeja KP 			i++;
16450324e51bSJeeja KP 		}
16460324e51bSJeeja KP 	}
16470324e51bSJeeja KP 
16480324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16490324e51bSJeeja KP }
16500324e51bSJeeja KP 
16510324e51bSJeeja KP int hdac_hdmi_jack_port_init(struct snd_soc_codec *codec,
16520324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16530324e51bSJeeja KP {
16540324e51bSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
16550324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16560324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16570324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16580324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16590324e51bSJeeja KP 	char w_name[NAME_SIZE];
16600324e51bSJeeja KP 	int i = 0, j, ret;
16610324e51bSJeeja KP 
16620324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16630324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16640324e51bSJeeja KP 
16650324e51bSJeeja KP 	if (!widgets)
16660324e51bSJeeja KP 		return -ENOMEM;
16670324e51bSJeeja KP 
16680324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16690324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16700324e51bSJeeja KP 	if (!route)
16710324e51bSJeeja KP 		return -ENOMEM;
16720324e51bSJeeja KP 
16730324e51bSJeeja KP 	/* create Jack DAPM widget */
16740324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16750324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16760324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16770324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16780324e51bSJeeja KP 
16790324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16800324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16810324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16820324e51bSJeeja KP 			if (ret < 0)
16830324e51bSJeeja KP 				return ret;
16840324e51bSJeeja KP 
16850324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16860324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16870324e51bSJeeja KP 
16880324e51bSJeeja KP 			/* add to route from Jack widget to output */
16890324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16900324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16910324e51bSJeeja KP 
16920324e51bSJeeja KP 			i++;
16930324e51bSJeeja KP 		}
16940324e51bSJeeja KP 	}
16950324e51bSJeeja KP 
16960324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
16970324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
16980324e51bSJeeja KP 	if (ret < 0)
16990324e51bSJeeja KP 		return ret;
17000324e51bSJeeja KP 
17010324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17020324e51bSJeeja KP 	if (ret < 0)
17030324e51bSJeeja KP 		return ret;
17040324e51bSJeeja KP 
17050324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17060324e51bSJeeja KP 	if (ret < 0)
17070324e51bSJeeja KP 		return ret;
17080324e51bSJeeja KP 
17090324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17100324e51bSJeeja KP 	ret = create_fill_jack_kcontrols(dapm->card, edev);
17110324e51bSJeeja KP 
17120324e51bSJeeja KP 	if (ret < 0)
17130324e51bSJeeja KP 		return ret;
17140324e51bSJeeja KP 
17150324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17160324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17170324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17180324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17190324e51bSJeeja KP 						pin->ports[j].jack_pin);
17200324e51bSJeeja KP 	}
17210324e51bSJeeja KP 
17220324e51bSJeeja KP 	return 0;
17230324e51bSJeeja KP }
17240324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17250324e51bSJeeja KP 
172662490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
172762490016SJeeja KP 				struct snd_soc_jack *jack)
17284a3478deSJeeja KP {
17294a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
17304a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
17314a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17324a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17332889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17342889099eSSubhransu S. Prusty 	int err;
17354a3478deSJeeja KP 
17364a3478deSJeeja KP 	/*
17374a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17384a3478deSJeeja KP 	 * add to the pcm list
17394a3478deSJeeja KP 	 */
17404a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
17414a3478deSJeeja KP 	if (!pcm)
17424a3478deSJeeja KP 		return -ENOMEM;
17434a3478deSJeeja KP 	pcm->pcm_id = device;
17444a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1745e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
174662490016SJeeja KP 	pcm->jack = jack;
1747ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1748e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17492889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17502889099eSSubhransu S. Prusty 	if (snd_pcm) {
17512889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17522889099eSSubhransu S. Prusty 		if (err < 0) {
17532889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
17542889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17552889099eSSubhransu S. Prusty 				err, device);
17562889099eSSubhransu S. Prusty 			kfree(pcm);
17572889099eSSubhransu S. Prusty 			return err;
17582889099eSSubhransu S. Prusty 		}
17592889099eSSubhransu S. Prusty 	}
17602889099eSSubhransu S. Prusty 
17614a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17624a3478deSJeeja KP 
176362490016SJeeja KP 	return 0;
17644a3478deSJeeja KP }
17654a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17664a3478deSJeeja KP 
1767a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1768a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1769a9ce96bcSJeeja KP {
1770a9ce96bcSJeeja KP 	int i;
1771a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1772a9ce96bcSJeeja KP 
1773a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1774a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1775a9ce96bcSJeeja KP 
1776a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1777a9ce96bcSJeeja KP 				pin->mst_capable = false;
1778a9ce96bcSJeeja KP 			else
1779a9ce96bcSJeeja KP 				pin->mst_capable = true;
1780a9ce96bcSJeeja KP 		}
1781a9ce96bcSJeeja KP 
1782a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1783a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1784a9ce96bcSJeeja KP 				continue;
1785a9ce96bcSJeeja KP 
1786a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1787a9ce96bcSJeeja KP 		}
1788a9ce96bcSJeeja KP 	}
1789a9ce96bcSJeeja KP }
1790a9ce96bcSJeeja KP 
179118382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
179218382eadSSubhransu S. Prusty {
179318382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
179418382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
179518382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
179618382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1797b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1798a9ce96bcSJeeja KP 	int ret;
179918382eadSSubhransu S. Prusty 
180018382eadSSubhransu S. Prusty 	edev->scodec = codec;
180118382eadSSubhransu S. Prusty 
1802b2047e99SVinod Koul 	/*
1803b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1804b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1805b2047e99SVinod Koul 	 */
1806b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1807500e06b9SVinod Koul 	if (!hlink) {
1808500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1809500e06b9SVinod Koul 		return -EIO;
1810500e06b9SVinod Koul 	}
1811500e06b9SVinod Koul 
1812b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1813b2047e99SVinod Koul 
181479f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
181579f4e922SSubhransu S. Prusty 	if (ret < 0)
181679f4e922SSubhransu S. Prusty 		return ret;
181718382eadSSubhransu S. Prusty 
1818b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1819b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1820b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1821b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1822b8a54545SSubhransu S. Prusty 				ret);
1823b8a54545SSubhransu S. Prusty 		return ret;
1824b8a54545SSubhransu S. Prusty 	}
1825b8a54545SSubhransu S. Prusty 
1826a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
182718382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
182818382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
182918382eadSSubhransu S. Prusty 
1830e342ac08SSubhransu S. Prusty 	/*
1831e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1832e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1833e342ac08SSubhransu S. Prusty 	 */
1834e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1835e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1836e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1837e342ac08SSubhransu S. Prusty 
1838e342ac08SSubhransu S. Prusty 	return 0;
1839e342ac08SSubhransu S. Prusty }
1840e342ac08SSubhransu S. Prusty 
1841e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1842e342ac08SSubhransu S. Prusty {
1843e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1844e342ac08SSubhransu S. Prusty 
1845e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
184618382eadSSubhransu S. Prusty 	return 0;
184718382eadSSubhransu S. Prusty }
184818382eadSSubhransu S. Prusty 
1849571d5078SJeeja KP #ifdef CONFIG_PM
18501b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
18511b377ccdSSubhransu S. Prusty {
18521b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
18531b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
18541b377ccdSSubhransu S. Prusty 
18551b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
18561b377ccdSSubhransu S. Prusty 
18571b377ccdSSubhransu S. Prusty 	/*
18581b377ccdSSubhransu S. Prusty 	 * Power down afg.
18591b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
18601b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
18611b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
18621b377ccdSSubhransu S. Prusty 	 * to read the state.
18631b377ccdSSubhransu S. Prusty 	 */
18641b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18651b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
18661b377ccdSSubhransu S. Prusty 
18671b377ccdSSubhransu S. Prusty 	return 0;
18681b377ccdSSubhransu S. Prusty }
18691b377ccdSSubhransu S. Prusty 
18700fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1871571d5078SJeeja KP {
18720fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1873571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1874571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
18751b377ccdSSubhransu S. Prusty 
18761b377ccdSSubhransu S. Prusty 	/* Power up afg */
18771b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18781b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1879571d5078SJeeja KP 
1880571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1881571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1882571d5078SJeeja KP 
1883571d5078SJeeja KP 	/*
1884571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1885571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1886a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1887a9ce96bcSJeeja KP 	 * already set pin caps.
1888571d5078SJeeja KP 	 */
1889a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1890571d5078SJeeja KP 
18911b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1892571d5078SJeeja KP }
1893571d5078SJeeja KP #else
18941b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
18950fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1896571d5078SJeeja KP #endif
1897571d5078SJeeja KP 
1898a180ba45SBhumika Goyal static const struct snd_soc_codec_driver hdmi_hda_codec = {
189918382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1900e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
190118382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
190218382eadSSubhransu S. Prusty };
190318382eadSSubhransu S. Prusty 
19042889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
19052889099eSSubhransu S. Prusty 					unsigned char *chmap)
19062889099eSSubhransu S. Prusty {
19072889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19082889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19092889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19102889099eSSubhransu S. Prusty 
1911ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19122889099eSSubhransu S. Prusty }
19132889099eSSubhransu S. Prusty 
19142889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
19152889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19162889099eSSubhransu S. Prusty {
19172889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19182889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19192889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1920e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1921e0e5d3e5SJeeja KP 
1922eb50fa17SSubhransu S. Prusty 	if (!pcm)
1923eb50fa17SSubhransu S. Prusty 		return;
1924eb50fa17SSubhransu S. Prusty 
1925e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1926e0e5d3e5SJeeja KP 		return;
19272889099eSSubhransu S. Prusty 
1928ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1929ab1eea19SJeeja KP 	pcm->chmap_set = true;
1930ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1931e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19322889099eSSubhransu S. Prusty 		if (prepared)
1933754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1934ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19352889099eSSubhransu S. Prusty }
19362889099eSSubhransu S. Prusty 
19372889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
19382889099eSSubhransu S. Prusty {
19392889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19402889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19412889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19422889099eSSubhransu S. Prusty 
1943eb50fa17SSubhransu S. Prusty 	if (!pcm)
1944eb50fa17SSubhransu S. Prusty 		return false;
1945eb50fa17SSubhransu S. Prusty 
1946e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1947e0e5d3e5SJeeja KP 		return false;
1948e0e5d3e5SJeeja KP 
1949e0e5d3e5SJeeja KP 	return true;
19502889099eSSubhransu S. Prusty }
19512889099eSSubhransu S. Prusty 
19522889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
19532889099eSSubhransu S. Prusty {
19542889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19552889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19562889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1957e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1958e0e5d3e5SJeeja KP 
1959eb50fa17SSubhransu S. Prusty 	if (!pcm)
1960eb50fa17SSubhransu S. Prusty 		return 0;
1961eb50fa17SSubhransu S. Prusty 
1962e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1963e0e5d3e5SJeeja KP 		return 0;
1964e0e5d3e5SJeeja KP 
1965e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1966e0e5d3e5SJeeja KP 
1967e0e5d3e5SJeeja KP 	if (!port)
1968e0e5d3e5SJeeja KP 		return 0;
19692889099eSSubhransu S. Prusty 
1970754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19712889099eSSubhransu S. Prusty 		return 0;
19722889099eSSubhransu S. Prusty 
1973754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19742889099eSSubhransu S. Prusty }
19752889099eSSubhransu S. Prusty 
19765622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
19775622bc95SPradeep Tewani 	.vendor_nid = INTEL_GLK_VENDOR_NID,
19785622bc95SPradeep Tewani };
19795622bc95SPradeep Tewani 
19805622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
19815622bc95SPradeep Tewani 	.vendor_nid = INTEL_VENDOR_NID,
19825622bc95SPradeep Tewani };
19835622bc95SPradeep Tewani 
198418382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
198518382eadSSubhransu S. Prusty {
198618382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
198718382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
198817a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1989b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
199017a42c45SSubhransu S. Prusty 	int num_dais = 0;
199118382eadSSubhransu S. Prusty 	int ret = 0;
19925622bc95SPradeep Tewani 	struct hdac_driver *hdrv = drv_to_hdac_driver(codec->dev.driver);
19935622bc95SPradeep Tewani 	const struct hda_device_id *hdac_id = hdac_get_device_id(codec, hdrv);
199418382eadSSubhransu S. Prusty 
1995b2047e99SVinod Koul 	/* hold the ref while we probe */
1996b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1997500e06b9SVinod Koul 	if (!hlink) {
1998500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1999500e06b9SVinod Koul 		return -EIO;
2000500e06b9SVinod Koul 	}
2001500e06b9SVinod Koul 
2002b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
2003b2047e99SVinod Koul 
200418382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
200518382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
200618382eadSSubhransu S. Prusty 		return -ENOMEM;
200718382eadSSubhransu S. Prusty 
200818382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
2009bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
20102889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20112889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20122889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20132889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
201418382eadSSubhransu S. Prusty 
2015eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2016eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2017eb50fa17SSubhransu S. Prusty 
20185622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20195622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20205622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20215622bc95SPradeep Tewani 	else
20225622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20235622bc95SPradeep Tewani 
202418382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
202518382eadSSubhransu S. Prusty 
202615b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
202715b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20284a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20294a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
203015b91447SSubhransu S. Prusty 
2031aeaccef0SRamesh Babu 	/*
2032aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2033aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2034aeaccef0SRamesh Babu 	 */
2035aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
2036aeaccef0SRamesh Babu 	if (ret < 0) {
2037aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
2038aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
2039aeaccef0SRamesh Babu 			ret);
2040aeaccef0SRamesh Babu 		return ret;
2041aeaccef0SRamesh Babu 	}
2042aeaccef0SRamesh Babu 
204317a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
204417a42c45SSubhransu S. Prusty 	if (ret < 0) {
204517a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
204617a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
204718382eadSSubhransu S. Prusty 		return ret;
204817a42c45SSubhransu S. Prusty 	}
204918382eadSSubhransu S. Prusty 
205018382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
2051b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
205217a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2053b2047e99SVinod Koul 
2054b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
2055b2047e99SVinod Koul 
2056b2047e99SVinod Koul 	return ret;
205718382eadSSubhransu S. Prusty }
205818382eadSSubhransu S. Prusty 
205918382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
206018382eadSSubhransu S. Prusty {
206115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
206215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
206315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
20644a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
20652fe42dd0SJeeja KP 	struct hdac_hdmi_port *port, *port_next;
2066754695f9SJeeja KP 	int i;
206715b91447SSubhransu S. Prusty 
206818382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
206918382eadSSubhransu S. Prusty 
20704a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
20714a3478deSJeeja KP 		pcm->cvt = NULL;
2072e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
2073e0e5d3e5SJeeja KP 			continue;
2074e0e5d3e5SJeeja KP 
20752fe42dd0SJeeja KP 		list_for_each_entry_safe(port, port_next,
20762fe42dd0SJeeja KP 					&pcm->port_list, head)
20772fe42dd0SJeeja KP 			list_del(&port->head);
2078e0e5d3e5SJeeja KP 
20794a3478deSJeeja KP 		list_del(&pcm->head);
20804a3478deSJeeja KP 		kfree(pcm);
20814a3478deSJeeja KP 	}
20824a3478deSJeeja KP 
208315b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
208415b91447SSubhransu S. Prusty 		list_del(&cvt->head);
20854a3478deSJeeja KP 		kfree(cvt->name);
208615b91447SSubhransu S. Prusty 		kfree(cvt);
208715b91447SSubhransu S. Prusty 	}
208815b91447SSubhransu S. Prusty 
208915b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2090754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
2091754695f9SJeeja KP 			pin->ports[i].pin = NULL;
2092754695f9SJeeja KP 		kfree(pin->ports);
209315b91447SSubhransu S. Prusty 		list_del(&pin->head);
209415b91447SSubhransu S. Prusty 		kfree(pin);
209515b91447SSubhransu S. Prusty 	}
209615b91447SSubhransu S. Prusty 
209718382eadSSubhransu S. Prusty 	return 0;
209818382eadSSubhransu S. Prusty }
209918382eadSSubhransu S. Prusty 
2100e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2101e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2102e342ac08SSubhransu S. Prusty {
2103e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2104e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
210507f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2106b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2107b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
210807f083abSSubhransu S. Prusty 	int err;
2109e342ac08SSubhransu S. Prusty 
2110e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2111e342ac08SSubhransu S. Prusty 
211207f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
211307f083abSSubhransu S. Prusty 	if (!bus)
211407f083abSSubhransu S. Prusty 		return 0;
211507f083abSSubhransu S. Prusty 
21161b377ccdSSubhransu S. Prusty 	/*
21171b377ccdSSubhransu S. Prusty 	 * Power down afg.
21181b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21191b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21201b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21211b377ccdSSubhransu S. Prusty 	 * to read the state.
21221b377ccdSSubhransu S. Prusty 	 */
21231b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
21241b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
212507f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
212607f083abSSubhransu S. Prusty 	if (err < 0) {
212707f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
212807f083abSSubhransu S. Prusty 		return err;
212907f083abSSubhransu S. Prusty 	}
213007f083abSSubhransu S. Prusty 
2131b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2132500e06b9SVinod Koul 	if (!hlink) {
2133500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2134500e06b9SVinod Koul 		return -EIO;
2135500e06b9SVinod Koul 	}
2136500e06b9SVinod Koul 
2137b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
2138b2047e99SVinod Koul 
2139e342ac08SSubhransu S. Prusty 	return 0;
2140e342ac08SSubhransu S. Prusty }
2141e342ac08SSubhransu S. Prusty 
2142e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2143e342ac08SSubhransu S. Prusty {
2144e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2145e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
214607f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2147b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2148b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
214907f083abSSubhransu S. Prusty 	int err;
2150e342ac08SSubhransu S. Prusty 
2151e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2152e342ac08SSubhransu S. Prusty 
215307f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
215407f083abSSubhransu S. Prusty 	if (!bus)
215507f083abSSubhransu S. Prusty 		return 0;
215607f083abSSubhransu S. Prusty 
2157b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2158500e06b9SVinod Koul 	if (!hlink) {
2159500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2160500e06b9SVinod Koul 		return -EIO;
2161500e06b9SVinod Koul 	}
2162500e06b9SVinod Koul 
2163b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
2164b2047e99SVinod Koul 
216507f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
216607f083abSSubhransu S. Prusty 	if (err < 0) {
216707f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
216807f083abSSubhransu S. Prusty 		return err;
216907f083abSSubhransu S. Prusty 	}
217007f083abSSubhransu S. Prusty 
2171ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
2172ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
2173ab85f5b3SSubhransu S. Prusty 
2174e342ac08SSubhransu S. Prusty 	/* Power up afg */
21751b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
21761b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2177e342ac08SSubhransu S. Prusty 
2178e342ac08SSubhransu S. Prusty 	return 0;
2179e342ac08SSubhransu S. Prusty }
2180e342ac08SSubhransu S. Prusty #else
2181e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2182e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2183e342ac08SSubhransu S. Prusty #endif
2184e342ac08SSubhransu S. Prusty 
2185e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2186e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
21871b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
21880fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
2189e342ac08SSubhransu S. Prusty };
2190e342ac08SSubhransu S. Prusty 
219118382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
219218382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2193e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2194cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21955622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21965622bc95SPradeep Tewani 						   &intel_glk_drv_data),
219718382eadSSubhransu S. Prusty 	{}
219818382eadSSubhransu S. Prusty };
219918382eadSSubhransu S. Prusty 
220018382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
220118382eadSSubhransu S. Prusty 
220218382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
220318382eadSSubhransu S. Prusty 	. hdac = {
220418382eadSSubhransu S. Prusty 		.driver = {
220518382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2206e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
220718382eadSSubhransu S. Prusty 		},
220818382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
220918382eadSSubhransu S. Prusty 	},
221018382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
221118382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
221218382eadSSubhransu S. Prusty };
221318382eadSSubhransu S. Prusty 
221418382eadSSubhransu S. Prusty static int __init hdmi_init(void)
221518382eadSSubhransu S. Prusty {
221618382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
221718382eadSSubhransu S. Prusty }
221818382eadSSubhransu S. Prusty 
221918382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
222018382eadSSubhransu S. Prusty {
222118382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
222218382eadSSubhransu S. Prusty }
222318382eadSSubhransu S. Prusty 
222418382eadSSubhransu S. Prusty module_init(hdmi_init);
222518382eadSSubhransu S. Prusty module_exit(hdmi_exit);
222618382eadSSubhransu S. Prusty 
222718382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
222818382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
222918382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
223018382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2231