xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 5622bc955898f4d5fa2ce8f507e5991b08367824)
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 
124*5622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
125*5622bc95SPradeep Tewani 	unsigned int vendor_nid;
126*5622bc95SPradeep Tewani };
127*5622bc95SPradeep 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;
138*5622bc95SPradeep 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 
945754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
94679f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
94779f4e922SSubhransu S. Prusty 	if (!kc->name)
94879f4e922SSubhransu S. Prusty 		return -ENOMEM;
94918382eadSSubhransu S. Prusty 
95079f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
95179f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
95279f4e922SSubhransu S. Prusty 	kc->access = 0;
95379f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
954754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95579f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
95679f4e922SSubhransu S. Prusty 
95779f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
95879f4e922SSubhransu S. Prusty 
95979f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
96079f4e922SSubhransu S. Prusty 	se->items = num_items;
96179f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
96279f4e922SSubhransu S. Prusty 
96379f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
96479f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
96579f4e922SSubhransu S. Prusty 	if (!items[i])
96679f4e922SSubhransu S. Prusty 		return -ENOMEM;
96779f4e922SSubhransu S. Prusty 
96879f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
96979f4e922SSubhransu S. Prusty 		i++;
97079f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
97179f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
97279f4e922SSubhransu S. Prusty 		if (!items[i])
97379f4e922SSubhransu S. Prusty 			return -ENOMEM;
97479f4e922SSubhransu S. Prusty 	}
97579f4e922SSubhransu S. Prusty 
97679f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
97779f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
97879f4e922SSubhransu S. Prusty 	if (!se->texts)
97979f4e922SSubhransu S. Prusty 		return -ENOMEM;
98079f4e922SSubhransu S. Prusty 
98179f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
982754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
983c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
984c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98579f4e922SSubhransu S. Prusty }
98679f4e922SSubhransu S. Prusty 
98779f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
98879f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
98979f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
99079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
99179f4e922SSubhransu S. Prusty {
99279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
99379f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
99479f4e922SSubhransu S. Prusty 	struct soc_enum *se;
995754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
99679f4e922SSubhransu S. Prusty 	int i, j;
99779f4e922SSubhransu S. Prusty 
998754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
99979f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
100079f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
100179f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
100279f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
100379f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
100479f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100579f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
100679f4e922SSubhransu S. Prusty 
100779f4e922SSubhransu S. Prusty 			rindex++;
100879f4e922SSubhransu S. Prusty 		}
100979f4e922SSubhransu S. Prusty 
101079f4e922SSubhransu S. Prusty 		mux_index++;
101179f4e922SSubhransu S. Prusty 	}
101279f4e922SSubhransu S. Prusty }
101379f4e922SSubhransu S. Prusty 
101479f4e922SSubhransu S. Prusty /*
101579f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
101679f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1017754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1018754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
101979f4e922SSubhransu S. Prusty  *
1020754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1021754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
102279f4e922SSubhransu S. Prusty  *
102379f4e922SSubhransu S. Prusty  * Routes are added as below:
1024754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1025754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
102679f4e922SSubhransu S. Prusty  *
102779f4e922SSubhransu S. Prusty  * Total route elements:
1028754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
102979f4e922SSubhransu S. Prusty  */
103079f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
103179f4e922SSubhransu S. Prusty {
103279f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
103379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
103479f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
103579f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
103679f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
103779f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
103879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
103979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1040754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
104179f4e922SSubhransu S. Prusty 
104279f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
104379f4e922SSubhransu S. Prusty 		return -EINVAL;
104479f4e922SSubhransu S. Prusty 
1045754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1046754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
104779f4e922SSubhransu S. Prusty 				GFP_KERNEL);
104879f4e922SSubhransu S. Prusty 
104979f4e922SSubhransu S. Prusty 	if (!widgets)
105079f4e922SSubhransu S. Prusty 		return -ENOMEM;
105179f4e922SSubhransu S. Prusty 
105279f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
105379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
105479f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105579f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1056c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1057c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1058c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1059c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
106079f4e922SSubhransu S. Prusty 		if (ret < 0)
106179f4e922SSubhransu S. Prusty 			return ret;
106279f4e922SSubhransu S. Prusty 		i++;
106379f4e922SSubhransu S. Prusty 	}
106479f4e922SSubhransu S. Prusty 
106579f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1066754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1067754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1068754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
106979f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1070754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1071c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1072c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1073754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1074754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107579f4e922SSubhransu S. Prusty 			if (ret < 0)
107679f4e922SSubhransu S. Prusty 				return ret;
10770324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
107879f4e922SSubhransu S. Prusty 			i++;
107979f4e922SSubhransu S. Prusty 		}
1080754695f9SJeeja KP 	}
108179f4e922SSubhransu S. Prusty 
108279f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
108379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1084754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1085754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1086754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1087754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1088754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
108979f4e922SSubhransu S. Prusty 						widget_name);
109079f4e922SSubhransu S. Prusty 			if (ret < 0)
109179f4e922SSubhransu S. Prusty 				return ret;
109279f4e922SSubhransu S. Prusty 			i++;
109379f4e922SSubhransu S. Prusty 
109479f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109579f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
109679f4e922SSubhransu S. Prusty 
109779f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
109879f4e922SSubhransu S. Prusty 			num_routes++;
109979f4e922SSubhransu S. Prusty 		}
1100754695f9SJeeja KP 	}
110179f4e922SSubhransu S. Prusty 
110279f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
110379f4e922SSubhransu S. Prusty 							GFP_KERNEL);
110479f4e922SSubhransu S. Prusty 	if (!route)
110579f4e922SSubhransu S. Prusty 		return -ENOMEM;
110679f4e922SSubhransu S. Prusty 
110779f4e922SSubhransu S. Prusty 	i = 0;
110879f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
110979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1110754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
111179f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1112754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1113754695f9SJeeja KP 						hdmi->num_pin;
111479f4e922SSubhransu S. Prusty 
111579f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
111679f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
111779f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
111879f4e922SSubhransu S. Prusty 			i++;
1119754695f9SJeeja KP 		}
112079f4e922SSubhransu S. Prusty 	}
112179f4e922SSubhransu S. Prusty 
112279f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
112379f4e922SSubhransu S. Prusty 
112479f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1125754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
112679f4e922SSubhransu S. Prusty 
112779f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
112879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
112979f4e922SSubhransu S. Prusty 
113079f4e922SSubhransu S. Prusty 	return 0;
113179f4e922SSubhransu S. Prusty 
113218382eadSSubhransu S. Prusty }
113318382eadSSubhransu S. Prusty 
113415b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
113518382eadSSubhransu S. Prusty {
113615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1137754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
113815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1139148569fdSSubhransu S. Prusty 	int dai_id = 0;
114018382eadSSubhransu S. Prusty 
1141148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
114215b91447SSubhransu S. Prusty 		return -EINVAL;
114318382eadSSubhransu S. Prusty 
1144148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1145148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1146148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
114715b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
114818382eadSSubhransu S. Prusty 
1149148569fdSSubhransu S. Prusty 		dai_id++;
1150148569fdSSubhransu S. Prusty 
1151148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1152148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1153148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1154148569fdSSubhransu S. Prusty 			break;
1155148569fdSSubhransu S. Prusty 		}
1156148569fdSSubhransu S. Prusty 	}
115718382eadSSubhransu S. Prusty 
115815b91447SSubhransu S. Prusty 	return 0;
115915b91447SSubhransu S. Prusty }
116015b91447SSubhransu S. Prusty 
116115b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
116215b91447SSubhransu S. Prusty {
116315b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
116415b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11654a3478deSJeeja KP 	char name[NAME_SIZE];
116615b91447SSubhransu S. Prusty 
116715b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
116815b91447SSubhransu S. Prusty 	if (!cvt)
116915b91447SSubhransu S. Prusty 		return -ENOMEM;
117015b91447SSubhransu S. Prusty 
117115b91447SSubhransu S. Prusty 	cvt->nid = nid;
11724a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11734a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
117415b91447SSubhransu S. Prusty 
117515b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
117615b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
117715b91447SSubhransu S. Prusty 
117815b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
117915b91447SSubhransu S. Prusty }
118015b91447SSubhransu S. Prusty 
1181f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1182754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1183b7756edeSSubhransu S. Prusty {
1184f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1185f6fa11a3SSandeep Tayal 
1186754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1187f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1188f6fa11a3SSandeep Tayal 
1189f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1190f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1191f6fa11a3SSandeep Tayal 		return -EINVAL;
1192b7756edeSSubhransu S. Prusty 	}
1193b7756edeSSubhransu S. Prusty 
1194754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1195f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1196f6fa11a3SSandeep Tayal 
1197f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1198f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1199f6fa11a3SSandeep Tayal 		return -EINVAL;
1200f6fa11a3SSandeep Tayal 	}
1201f6fa11a3SSandeep Tayal 
1202754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1203f6fa11a3SSandeep Tayal 
1204f6fa11a3SSandeep Tayal 	return 0;
1205f6fa11a3SSandeep Tayal }
1206f6fa11a3SSandeep Tayal 
1207754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1208754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1209b8a54545SSubhransu S. Prusty {
1210b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
12114a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
12124a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1213754695f9SJeeja KP 	int size = 0;
12142acd8309SJeeja KP 	int port_id = -1;
1215754695f9SJeeja KP 
1216754695f9SJeeja KP 	if (!hdmi)
1217754695f9SJeeja KP 		return;
12184a3478deSJeeja KP 
12192acd8309SJeeja KP 	/*
12202acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12212acd8309SJeeja KP 	 * to be -1.
12222acd8309SJeeja KP 	 */
12234a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1224754695f9SJeeja KP 	port->eld.monitor_present = false;
1225f6fa11a3SSandeep Tayal 
12262acd8309SJeeja KP 	if (pin->mst_capable)
12272acd8309SJeeja KP 		port_id = port->id;
12282acd8309SJeeja KP 
12292acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1230754695f9SJeeja KP 				&port->eld.monitor_present,
1231754695f9SJeeja KP 				port->eld.eld_buffer,
1232f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1233f6fa11a3SSandeep Tayal 
1234f6fa11a3SSandeep Tayal 	if (size > 0) {
1235f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1236754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1237f6fa11a3SSandeep Tayal 			size = -EINVAL;
1238f6fa11a3SSandeep Tayal 	}
1239f6fa11a3SSandeep Tayal 
1240f6fa11a3SSandeep Tayal 	if (size > 0) {
1241754695f9SJeeja KP 		port->eld.eld_valid = true;
1242754695f9SJeeja KP 		port->eld.eld_size = size;
1243f6fa11a3SSandeep Tayal 	} else {
1244754695f9SJeeja KP 		port->eld.eld_valid = false;
1245754695f9SJeeja KP 		port->eld.eld_size = 0;
1246f6fa11a3SSandeep Tayal 	}
1247b8a54545SSubhransu S. Prusty 
1248754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12494a3478deSJeeja KP 
1250754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1251b8a54545SSubhransu S. Prusty 
1252e0e5d3e5SJeeja KP 		dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1253754695f9SJeeja KP 						__func__, pin->nid, port->id);
12544a3478deSJeeja KP 
12554a3478deSJeeja KP 		/*
12564a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12574a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12584a3478deSJeeja KP 		 * control select.
12594a3478deSJeeja KP 		 */
1260e0e5d3e5SJeeja KP 		if (pcm)
1261e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12624a3478deSJeeja KP 
12634a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1264f6fa11a3SSandeep Tayal 		return;
1265b8a54545SSubhransu S. Prusty 	}
1266b8a54545SSubhransu S. Prusty 
1267754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1268e0e5d3e5SJeeja KP 		if (pcm)
1269e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12704a3478deSJeeja KP 
1271f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1272754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1273754695f9SJeeja KP 
1274754695f9SJeeja KP 	}
1275754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12764a3478deSJeeja KP }
12774a3478deSJeeja KP 
1278754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1279754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1280754695f9SJeeja KP {
1281754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1282754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1283754695f9SJeeja KP 	int i;
1284754695f9SJeeja KP 
1285754695f9SJeeja KP 	/*
1286754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1287754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1288754695f9SJeeja KP 	 * implemented.
1289754695f9SJeeja KP 	 */
1290754695f9SJeeja KP 
1291754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1292754695f9SJeeja KP 	if (!ports)
1293754695f9SJeeja KP 		return -ENOMEM;
1294754695f9SJeeja KP 
1295754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1296754695f9SJeeja KP 		ports[i].id = i;
1297754695f9SJeeja KP 		ports[i].pin = pin;
1298754695f9SJeeja KP 	}
1299754695f9SJeeja KP 	pin->ports = ports;
1300754695f9SJeeja KP 	pin->num_ports = max_ports;
1301754695f9SJeeja KP 	return 0;
1302b8a54545SSubhransu S. Prusty }
1303b8a54545SSubhransu S. Prusty 
130415b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
130515b91447SSubhransu S. Prusty {
130615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
130715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1308754695f9SJeeja KP 	int ret;
130915b91447SSubhransu S. Prusty 
131015b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
131115b91447SSubhransu S. Prusty 	if (!pin)
131215b91447SSubhransu S. Prusty 		return -ENOMEM;
131315b91447SSubhransu S. Prusty 
131415b91447SSubhransu S. Prusty 	pin->nid = nid;
13152acd8309SJeeja KP 	pin->mst_capable = false;
1316754695f9SJeeja KP 	pin->edev = edev;
1317754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1318754695f9SJeeja KP 	if (ret < 0)
1319754695f9SJeeja KP 		return ret;
132015b91447SSubhransu S. Prusty 
132115b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
132215b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1323754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1324b8a54545SSubhransu S. Prusty 
132515b91447SSubhransu S. Prusty 	return 0;
132618382eadSSubhransu S. Prusty }
132718382eadSSubhransu S. Prusty 
1328211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1329*5622bc95SPradeep Tewani #define INTEL_GLK_VENDOR_NID 0x0b
1330211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1331211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1332211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1333211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1334211caab7SSubhransu S. Prusty 
1335211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1336211caab7SSubhransu S. Prusty {
1337211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1338*5622bc95SPradeep Tewani 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
1339*5622bc95SPradeep Tewani 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1340*5622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1341211caab7SSubhransu S. Prusty 
1342*5622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1343211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1344211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1345211caab7SSubhransu S. Prusty 		return;
1346211caab7SSubhransu S. Prusty 
1347211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1348*5622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1349211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1350211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1351211caab7SSubhransu S. Prusty 		return;
1352211caab7SSubhransu S. Prusty }
1353211caab7SSubhransu S. Prusty 
1354211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1355211caab7SSubhransu S. Prusty {
1356211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1357*5622bc95SPradeep Tewani 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
1358*5622bc95SPradeep Tewani 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1359*5622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1360211caab7SSubhransu S. Prusty 
1361*5622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1362211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1363211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1364211caab7SSubhransu S. Prusty 		return;
1365211caab7SSubhransu S. Prusty 
1366211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1367211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1368*5622bc95SPradeep Tewani 	vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
1369211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1370211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1371211caab7SSubhransu S. Prusty 		return;
1372211caab7SSubhransu S. Prusty 
1373211caab7SSubhransu S. Prusty }
1374211caab7SSubhransu S. Prusty 
137517a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
137617a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
137717a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
137817a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1379c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
138017a42c45SSubhransu S. Prusty };
138117a42c45SSubhransu S. Prusty 
138217a42c45SSubhransu S. Prusty /*
138317a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
138417a42c45SSubhransu S. Prusty  * based on the number of converter queried.
138517a42c45SSubhransu S. Prusty  */
138617a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
138717a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
138817a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
138917a42c45SSubhransu S. Prusty {
139017a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
139117a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
139217a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
139317a42c45SSubhransu S. Prusty 	int i = 0;
139417a42c45SSubhransu S. Prusty 	u32 rates, bps;
139517a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
139617a42c45SSubhransu S. Prusty 	u64 formats;
139717a42c45SSubhransu S. Prusty 	int ret;
139817a42c45SSubhransu S. Prusty 
139917a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
140017a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
140117a42c45SSubhransu S. Prusty 			GFP_KERNEL);
140217a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
140317a42c45SSubhransu S. Prusty 		return -ENOMEM;
140417a42c45SSubhransu S. Prusty 
140517a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
140617a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
140717a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
140817a42c45SSubhransu S. Prusty 		if (ret)
140917a42c45SSubhransu S. Prusty 			return ret;
141017a42c45SSubhransu S. Prusty 
141117a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
141217a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
141317a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
141417a42c45SSubhransu S. Prusty 
141517a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
141617a42c45SSubhransu S. Prusty 			return -ENOMEM;
141717a42c45SSubhransu S. Prusty 
141817a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
141917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
142017a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
142117a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142217a42c45SSubhransu S. Prusty 			return -ENOMEM;
142317a42c45SSubhransu S. Prusty 
142417a42c45SSubhransu S. Prusty 		/*
142517a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
142617a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
142717a42c45SSubhransu S. Prusty 		 */
142817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
142917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
143017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
143117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
143466d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
143517a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
143617a42c45SSubhransu S. Prusty 		i++;
143717a42c45SSubhransu S. Prusty 	}
143817a42c45SSubhransu S. Prusty 
143917a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
144017a42c45SSubhransu S. Prusty 
144117a42c45SSubhransu S. Prusty 	return 0;
144217a42c45SSubhransu S. Prusty }
144317a42c45SSubhransu S. Prusty 
144418382eadSSubhransu S. Prusty /*
144518382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
144618382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
144718382eadSSubhransu S. Prusty  */
144817a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
144917a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
145018382eadSSubhransu S. Prusty {
145118382eadSSubhransu S. Prusty 	hda_nid_t nid;
14523c83ac23SSudip Mukherjee 	int i, num_nodes;
145318382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
145418382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
145515b91447SSubhransu S. Prusty 	int ret;
145618382eadSSubhransu S. Prusty 
1457211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1458211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1459211caab7SSubhransu S. Prusty 
14603c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1461541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
146218382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
146318382eadSSubhransu S. Prusty 		return -EINVAL;
146418382eadSSubhransu S. Prusty 	}
146518382eadSSubhransu S. Prusty 
14663c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
146718382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
146818382eadSSubhransu S. Prusty 
146918382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
147018382eadSSubhransu S. Prusty 		unsigned int caps;
147118382eadSSubhransu S. Prusty 		unsigned int type;
147218382eadSSubhransu S. Prusty 
147318382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
147418382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147518382eadSSubhransu S. Prusty 
147618382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
147718382eadSSubhransu S. Prusty 			continue;
147818382eadSSubhransu S. Prusty 
147918382eadSSubhransu S. Prusty 		switch (type) {
148018382eadSSubhransu S. Prusty 
148118382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
148215b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
148315b91447SSubhransu S. Prusty 			if (ret < 0)
148415b91447SSubhransu S. Prusty 				return ret;
148518382eadSSubhransu S. Prusty 			break;
148618382eadSSubhransu S. Prusty 
148718382eadSSubhransu S. Prusty 		case AC_WID_PIN:
148815b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
148915b91447SSubhransu S. Prusty 			if (ret < 0)
149015b91447SSubhransu S. Prusty 				return ret;
149118382eadSSubhransu S. Prusty 			break;
149218382eadSSubhransu S. Prusty 		}
149318382eadSSubhransu S. Prusty 	}
149418382eadSSubhransu S. Prusty 
149518382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
149618382eadSSubhransu S. Prusty 
149715b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
149818382eadSSubhransu S. Prusty 		return -EIO;
149918382eadSSubhransu S. Prusty 
150017a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
150117a42c45SSubhransu S. Prusty 	if (ret) {
150217a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
150317a42c45SSubhransu S. Prusty 							ret);
150417a42c45SSubhransu S. Prusty 		return ret;
150517a42c45SSubhransu S. Prusty 	}
150617a42c45SSubhransu S. Prusty 
150717a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
150817a42c45SSubhransu S. Prusty 
150915b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
151018382eadSSubhransu S. Prusty }
151118382eadSSubhransu S. Prusty 
1512f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1513b8a54545SSubhransu S. Prusty {
1514b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1515b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1516754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1517754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1518b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
15192acd8309SJeeja KP 	int i;
1520b8a54545SSubhransu S. Prusty 
1521b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1522b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1523b8a54545SSubhransu S. Prusty 
1524754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1525754695f9SJeeja KP 							pin_nid, pipe);
1526b8a54545SSubhransu S. Prusty 
1527b8a54545SSubhransu S. Prusty 	/*
1528b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1529b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1530b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1531b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1532b8a54545SSubhransu S. Prusty 	 */
1533b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1534b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1535b8a54545SSubhransu S. Prusty 		return;
1536b8a54545SSubhransu S. Prusty 
1537b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1538b8a54545SSubhransu S. Prusty 		return;
1539b8a54545SSubhransu S. Prusty 
1540b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1541754695f9SJeeja KP 		if (pin->nid != pin_nid)
1542754695f9SJeeja KP 			continue;
1543754695f9SJeeja KP 
1544754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1545754695f9SJeeja KP 		if (pipe == -1) {
15462acd8309SJeeja KP 			pin->mst_capable = false;
1547754695f9SJeeja KP 			/* if not MST, default is port[0] */
1548754695f9SJeeja KP 			hport = &pin->ports[0];
15492acd8309SJeeja KP 		} else {
15502acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15512acd8309SJeeja KP 				pin->mst_capable = true;
15522acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15532acd8309SJeeja KP 					hport = &pin->ports[i];
155404c8f2bfSJeeja KP 					break;
15552acd8309SJeeja KP 				}
1556b8a54545SSubhransu S. Prusty 			}
1557b8a54545SSubhransu S. Prusty 		}
1558b8a54545SSubhransu S. Prusty 
155904c8f2bfSJeeja KP 		if (hport)
1560754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1561754695f9SJeeja KP 	}
1562754695f9SJeeja KP 
156304c8f2bfSJeeja KP }
156404c8f2bfSJeeja KP 
1565b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1566b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1567b8a54545SSubhransu S. Prusty };
1568b8a54545SSubhransu S. Prusty 
15692889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15702889099eSSubhransu S. Prusty 						int device)
15712889099eSSubhransu S. Prusty {
15722889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15732889099eSSubhransu S. Prusty 
15742889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15752889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15762889099eSSubhransu S. Prusty 			return rtd->pcm;
15772889099eSSubhransu S. Prusty 	}
15782889099eSSubhransu S. Prusty 
15792889099eSSubhransu S. Prusty 	return NULL;
15802889099eSSubhransu S. Prusty }
15812889099eSSubhransu S. Prusty 
15820324e51bSJeeja KP /* create jack pin kcontrols */
15830324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
15840324e51bSJeeja KP 				    struct hdac_ext_device *edev)
15850324e51bSJeeja KP {
15860324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
15870324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
15880324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
15890324e51bSJeeja KP 	char *name;
15900324e51bSJeeja KP 	int i = 0, j;
15910324e51bSJeeja KP 	struct snd_soc_codec *codec = edev->scodec;
15920324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15930324e51bSJeeja KP 
15940324e51bSJeeja KP 	kc = devm_kcalloc(codec->dev, hdmi->num_ports,
15950324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
15960324e51bSJeeja KP 
15970324e51bSJeeja KP 	if (!kc)
15980324e51bSJeeja KP 		return -ENOMEM;
15990324e51bSJeeja KP 
16000324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16010324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16020324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16030324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16040324e51bSJeeja KP 			name = devm_kstrdup(codec->dev, xname, GFP_KERNEL);
16050324e51bSJeeja KP 			if (!name)
16060324e51bSJeeja KP 				return -ENOMEM;
16070324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
16080324e51bSJeeja KP 			kc[i].name = devm_kstrdup(codec->dev, kc_name,
16090324e51bSJeeja KP 							GFP_KERNEL);
16100324e51bSJeeja KP 			if (!kc[i].name)
16110324e51bSJeeja KP 				return -ENOMEM;
16120324e51bSJeeja KP 
16130324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16140324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16150324e51bSJeeja KP 			kc[i].access = 0;
16160324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16170324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16180324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16190324e51bSJeeja KP 			i++;
16200324e51bSJeeja KP 		}
16210324e51bSJeeja KP 	}
16220324e51bSJeeja KP 
16230324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16240324e51bSJeeja KP }
16250324e51bSJeeja KP 
16260324e51bSJeeja KP int hdac_hdmi_jack_port_init(struct snd_soc_codec *codec,
16270324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16280324e51bSJeeja KP {
16290324e51bSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
16300324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16310324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16320324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16330324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16340324e51bSJeeja KP 	char w_name[NAME_SIZE];
16350324e51bSJeeja KP 	int i = 0, j, ret;
16360324e51bSJeeja KP 
16370324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16380324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16390324e51bSJeeja KP 
16400324e51bSJeeja KP 	if (!widgets)
16410324e51bSJeeja KP 		return -ENOMEM;
16420324e51bSJeeja KP 
16430324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16440324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16450324e51bSJeeja KP 	if (!route)
16460324e51bSJeeja KP 		return -ENOMEM;
16470324e51bSJeeja KP 
16480324e51bSJeeja KP 	/* create Jack DAPM widget */
16490324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16500324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16510324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16520324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16530324e51bSJeeja KP 
16540324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16550324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16560324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16570324e51bSJeeja KP 			if (ret < 0)
16580324e51bSJeeja KP 				return ret;
16590324e51bSJeeja KP 
16600324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16610324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16620324e51bSJeeja KP 
16630324e51bSJeeja KP 			/* add to route from Jack widget to output */
16640324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16650324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16660324e51bSJeeja KP 
16670324e51bSJeeja KP 			i++;
16680324e51bSJeeja KP 		}
16690324e51bSJeeja KP 	}
16700324e51bSJeeja KP 
16710324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
16720324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
16730324e51bSJeeja KP 	if (ret < 0)
16740324e51bSJeeja KP 		return ret;
16750324e51bSJeeja KP 
16760324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
16770324e51bSJeeja KP 	if (ret < 0)
16780324e51bSJeeja KP 		return ret;
16790324e51bSJeeja KP 
16800324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
16810324e51bSJeeja KP 	if (ret < 0)
16820324e51bSJeeja KP 		return ret;
16830324e51bSJeeja KP 
16840324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
16850324e51bSJeeja KP 	ret = create_fill_jack_kcontrols(dapm->card, edev);
16860324e51bSJeeja KP 
16870324e51bSJeeja KP 	if (ret < 0)
16880324e51bSJeeja KP 		return ret;
16890324e51bSJeeja KP 
16900324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
16910324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16920324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
16930324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
16940324e51bSJeeja KP 						pin->ports[j].jack_pin);
16950324e51bSJeeja KP 	}
16960324e51bSJeeja KP 
16970324e51bSJeeja KP 	return 0;
16980324e51bSJeeja KP }
16990324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17000324e51bSJeeja KP 
170162490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
170262490016SJeeja KP 				struct snd_soc_jack *jack)
17034a3478deSJeeja KP {
17044a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
17054a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
17064a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17074a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17082889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17092889099eSSubhransu S. Prusty 	int err;
17104a3478deSJeeja KP 
17114a3478deSJeeja KP 	/*
17124a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17134a3478deSJeeja KP 	 * add to the pcm list
17144a3478deSJeeja KP 	 */
17154a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
17164a3478deSJeeja KP 	if (!pcm)
17174a3478deSJeeja KP 		return -ENOMEM;
17184a3478deSJeeja KP 	pcm->pcm_id = device;
17194a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1720e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
172162490016SJeeja KP 	pcm->jack = jack;
1722ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1723e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17242889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17252889099eSSubhransu S. Prusty 	if (snd_pcm) {
17262889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17272889099eSSubhransu S. Prusty 		if (err < 0) {
17282889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
17292889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17302889099eSSubhransu S. Prusty 				err, device);
17312889099eSSubhransu S. Prusty 			kfree(pcm);
17322889099eSSubhransu S. Prusty 			return err;
17332889099eSSubhransu S. Prusty 		}
17342889099eSSubhransu S. Prusty 	}
17352889099eSSubhransu S. Prusty 
17364a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17374a3478deSJeeja KP 
173862490016SJeeja KP 	return 0;
17394a3478deSJeeja KP }
17404a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17414a3478deSJeeja KP 
1742a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1743a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1744a9ce96bcSJeeja KP {
1745a9ce96bcSJeeja KP 	int i;
1746a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1747a9ce96bcSJeeja KP 
1748a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1749a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1750a9ce96bcSJeeja KP 
1751a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1752a9ce96bcSJeeja KP 				pin->mst_capable = false;
1753a9ce96bcSJeeja KP 			else
1754a9ce96bcSJeeja KP 				pin->mst_capable = true;
1755a9ce96bcSJeeja KP 		}
1756a9ce96bcSJeeja KP 
1757a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1758a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1759a9ce96bcSJeeja KP 				continue;
1760a9ce96bcSJeeja KP 
1761a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1762a9ce96bcSJeeja KP 		}
1763a9ce96bcSJeeja KP 	}
1764a9ce96bcSJeeja KP }
1765a9ce96bcSJeeja KP 
176618382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
176718382eadSSubhransu S. Prusty {
176818382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
176918382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
177018382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
177118382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1772b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1773a9ce96bcSJeeja KP 	int ret;
177418382eadSSubhransu S. Prusty 
177518382eadSSubhransu S. Prusty 	edev->scodec = codec;
177618382eadSSubhransu S. Prusty 
1777b2047e99SVinod Koul 	/*
1778b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1779b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1780b2047e99SVinod Koul 	 */
1781b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1782500e06b9SVinod Koul 	if (!hlink) {
1783500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1784500e06b9SVinod Koul 		return -EIO;
1785500e06b9SVinod Koul 	}
1786500e06b9SVinod Koul 
1787b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1788b2047e99SVinod Koul 
178979f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
179079f4e922SSubhransu S. Prusty 	if (ret < 0)
179179f4e922SSubhransu S. Prusty 		return ret;
179218382eadSSubhransu S. Prusty 
1793b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1794b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1795b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1796b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1797b8a54545SSubhransu S. Prusty 				ret);
1798b8a54545SSubhransu S. Prusty 		return ret;
1799b8a54545SSubhransu S. Prusty 	}
1800b8a54545SSubhransu S. Prusty 
1801a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
180218382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
180318382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
180418382eadSSubhransu S. Prusty 
1805e342ac08SSubhransu S. Prusty 	/*
1806e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1807e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1808e342ac08SSubhransu S. Prusty 	 */
1809e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1810e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1811e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1812e342ac08SSubhransu S. Prusty 
1813e342ac08SSubhransu S. Prusty 	return 0;
1814e342ac08SSubhransu S. Prusty }
1815e342ac08SSubhransu S. Prusty 
1816e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1817e342ac08SSubhransu S. Prusty {
1818e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1819e342ac08SSubhransu S. Prusty 
1820e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
182118382eadSSubhransu S. Prusty 	return 0;
182218382eadSSubhransu S. Prusty }
182318382eadSSubhransu S. Prusty 
1824571d5078SJeeja KP #ifdef CONFIG_PM
18251b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
18261b377ccdSSubhransu S. Prusty {
18271b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
18281b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
18291b377ccdSSubhransu S. Prusty 
18301b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
18311b377ccdSSubhransu S. Prusty 
18321b377ccdSSubhransu S. Prusty 	/*
18331b377ccdSSubhransu S. Prusty 	 * Power down afg.
18341b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
18351b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
18361b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
18371b377ccdSSubhransu S. Prusty 	 * to read the state.
18381b377ccdSSubhransu S. Prusty 	 */
18391b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18401b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
18411b377ccdSSubhransu S. Prusty 
18421b377ccdSSubhransu S. Prusty 	return 0;
18431b377ccdSSubhransu S. Prusty }
18441b377ccdSSubhransu S. Prusty 
18450fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1846571d5078SJeeja KP {
18470fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1848571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1849571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
18501b377ccdSSubhransu S. Prusty 
18511b377ccdSSubhransu S. Prusty 	/* Power up afg */
18521b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18531b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1854571d5078SJeeja KP 
1855571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1856571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1857571d5078SJeeja KP 
1858571d5078SJeeja KP 	/*
1859571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1860571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1861a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1862a9ce96bcSJeeja KP 	 * already set pin caps.
1863571d5078SJeeja KP 	 */
1864a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1865571d5078SJeeja KP 
18661b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1867571d5078SJeeja KP }
1868571d5078SJeeja KP #else
18691b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
18700fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1871571d5078SJeeja KP #endif
1872571d5078SJeeja KP 
187318382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
187418382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1875e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
187618382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
187718382eadSSubhransu S. Prusty };
187818382eadSSubhransu S. Prusty 
18792889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
18802889099eSSubhransu S. Prusty 					unsigned char *chmap)
18812889099eSSubhransu S. Prusty {
18822889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
18832889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
18842889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
18852889099eSSubhransu S. Prusty 
1886ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
18872889099eSSubhransu S. Prusty }
18882889099eSSubhransu S. Prusty 
18892889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
18902889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
18912889099eSSubhransu S. Prusty {
18922889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
18932889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
18942889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1895e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1896e0e5d3e5SJeeja KP 
1897e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1898e0e5d3e5SJeeja KP 		return;
18992889099eSSubhransu S. Prusty 
1900ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1901ab1eea19SJeeja KP 	pcm->chmap_set = true;
1902ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1903e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19042889099eSSubhransu S. Prusty 		if (prepared)
1905754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1906ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19072889099eSSubhransu S. Prusty }
19082889099eSSubhransu S. Prusty 
19092889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
19102889099eSSubhransu S. Prusty {
19112889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19122889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19132889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19142889099eSSubhransu S. Prusty 
1915e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1916e0e5d3e5SJeeja KP 		return false;
1917e0e5d3e5SJeeja KP 
1918e0e5d3e5SJeeja KP 	return true;
19192889099eSSubhransu S. Prusty }
19202889099eSSubhransu S. Prusty 
19212889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
19222889099eSSubhransu S. Prusty {
19232889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19242889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19252889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1926e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1927e0e5d3e5SJeeja KP 
1928e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1929e0e5d3e5SJeeja KP 		return 0;
1930e0e5d3e5SJeeja KP 
1931e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1932e0e5d3e5SJeeja KP 
1933e0e5d3e5SJeeja KP 	if (!port)
1934e0e5d3e5SJeeja KP 		return 0;
19352889099eSSubhransu S. Prusty 
1936754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19372889099eSSubhransu S. Prusty 		return 0;
19382889099eSSubhransu S. Prusty 
1939754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19402889099eSSubhransu S. Prusty }
19412889099eSSubhransu S. Prusty 
1942*5622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
1943*5622bc95SPradeep Tewani 	.vendor_nid = INTEL_GLK_VENDOR_NID,
1944*5622bc95SPradeep Tewani };
1945*5622bc95SPradeep Tewani 
1946*5622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
1947*5622bc95SPradeep Tewani 	.vendor_nid = INTEL_VENDOR_NID,
1948*5622bc95SPradeep Tewani };
1949*5622bc95SPradeep Tewani 
195018382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
195118382eadSSubhransu S. Prusty {
195218382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
195318382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
195417a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1955b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
195617a42c45SSubhransu S. Prusty 	int num_dais = 0;
195718382eadSSubhransu S. Prusty 	int ret = 0;
1958*5622bc95SPradeep Tewani 	struct hdac_driver *hdrv = drv_to_hdac_driver(codec->dev.driver);
1959*5622bc95SPradeep Tewani 	const struct hda_device_id *hdac_id = hdac_get_device_id(codec, hdrv);
196018382eadSSubhransu S. Prusty 
1961b2047e99SVinod Koul 	/* hold the ref while we probe */
1962b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1963500e06b9SVinod Koul 	if (!hlink) {
1964500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1965500e06b9SVinod Koul 		return -EIO;
1966500e06b9SVinod Koul 	}
1967500e06b9SVinod Koul 
1968b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1969b2047e99SVinod Koul 
197018382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
197118382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
197218382eadSSubhransu S. Prusty 		return -ENOMEM;
197318382eadSSubhransu S. Prusty 
197418382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1975bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
19762889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
19772889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
19782889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
19792889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
198018382eadSSubhransu S. Prusty 
1981*5622bc95SPradeep Tewani 	if (hdac_id->driver_data)
1982*5622bc95SPradeep Tewani 		hdmi_priv->drv_data =
1983*5622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
1984*5622bc95SPradeep Tewani 	else
1985*5622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
1986*5622bc95SPradeep Tewani 
198718382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
198818382eadSSubhransu S. Prusty 
198915b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
199015b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
19914a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
19924a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
199315b91447SSubhransu S. Prusty 
1994aeaccef0SRamesh Babu 	/*
1995aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1996aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1997aeaccef0SRamesh Babu 	 */
1998aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1999aeaccef0SRamesh Babu 	if (ret < 0) {
2000aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
2001aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
2002aeaccef0SRamesh Babu 			ret);
2003aeaccef0SRamesh Babu 		return ret;
2004aeaccef0SRamesh Babu 	}
2005aeaccef0SRamesh Babu 
200617a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
200717a42c45SSubhransu S. Prusty 	if (ret < 0) {
200817a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
200917a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
201018382eadSSubhransu S. Prusty 		return ret;
201117a42c45SSubhransu S. Prusty 	}
201218382eadSSubhransu S. Prusty 
201318382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
2014b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
201517a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2016b2047e99SVinod Koul 
2017b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
2018b2047e99SVinod Koul 
2019b2047e99SVinod Koul 	return ret;
202018382eadSSubhransu S. Prusty }
202118382eadSSubhransu S. Prusty 
202218382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
202318382eadSSubhransu S. Prusty {
202415b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
202515b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
202615b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
20274a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
20282fe42dd0SJeeja KP 	struct hdac_hdmi_port *port, *port_next;
2029754695f9SJeeja KP 	int i;
203015b91447SSubhransu S. Prusty 
203118382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
203218382eadSSubhransu S. Prusty 
20334a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
20344a3478deSJeeja KP 		pcm->cvt = NULL;
2035e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
2036e0e5d3e5SJeeja KP 			continue;
2037e0e5d3e5SJeeja KP 
20382fe42dd0SJeeja KP 		list_for_each_entry_safe(port, port_next,
20392fe42dd0SJeeja KP 					&pcm->port_list, head)
20402fe42dd0SJeeja KP 			list_del(&port->head);
2041e0e5d3e5SJeeja KP 
20424a3478deSJeeja KP 		list_del(&pcm->head);
20434a3478deSJeeja KP 		kfree(pcm);
20444a3478deSJeeja KP 	}
20454a3478deSJeeja KP 
204615b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
204715b91447SSubhransu S. Prusty 		list_del(&cvt->head);
20484a3478deSJeeja KP 		kfree(cvt->name);
204915b91447SSubhransu S. Prusty 		kfree(cvt);
205015b91447SSubhransu S. Prusty 	}
205115b91447SSubhransu S. Prusty 
205215b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2053754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
2054754695f9SJeeja KP 			pin->ports[i].pin = NULL;
2055754695f9SJeeja KP 		kfree(pin->ports);
205615b91447SSubhransu S. Prusty 		list_del(&pin->head);
205715b91447SSubhransu S. Prusty 		kfree(pin);
205815b91447SSubhransu S. Prusty 	}
205915b91447SSubhransu S. Prusty 
206018382eadSSubhransu S. Prusty 	return 0;
206118382eadSSubhransu S. Prusty }
206218382eadSSubhransu S. Prusty 
2063e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2064e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2065e342ac08SSubhransu S. Prusty {
2066e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2067e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
206807f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2069b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2070b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
207107f083abSSubhransu S. Prusty 	int err;
2072e342ac08SSubhransu S. Prusty 
2073e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2074e342ac08SSubhransu S. Prusty 
207507f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
207607f083abSSubhransu S. Prusty 	if (!bus)
207707f083abSSubhransu S. Prusty 		return 0;
207807f083abSSubhransu S. Prusty 
20791b377ccdSSubhransu S. Prusty 	/*
20801b377ccdSSubhransu S. Prusty 	 * Power down afg.
20811b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
20821b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
20831b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
20841b377ccdSSubhransu S. Prusty 	 * to read the state.
20851b377ccdSSubhransu S. Prusty 	 */
20861b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
20871b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
208807f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
208907f083abSSubhransu S. Prusty 	if (err < 0) {
209007f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
209107f083abSSubhransu S. Prusty 		return err;
209207f083abSSubhransu S. Prusty 	}
209307f083abSSubhransu S. Prusty 
2094b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2095500e06b9SVinod Koul 	if (!hlink) {
2096500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2097500e06b9SVinod Koul 		return -EIO;
2098500e06b9SVinod Koul 	}
2099500e06b9SVinod Koul 
2100b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
2101b2047e99SVinod Koul 
2102e342ac08SSubhransu S. Prusty 	return 0;
2103e342ac08SSubhransu S. Prusty }
2104e342ac08SSubhransu S. Prusty 
2105e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2106e342ac08SSubhransu S. Prusty {
2107e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2108e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
210907f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2110b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2111b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
211207f083abSSubhransu S. Prusty 	int err;
2113e342ac08SSubhransu S. Prusty 
2114e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2115e342ac08SSubhransu S. Prusty 
211607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
211707f083abSSubhransu S. Prusty 	if (!bus)
211807f083abSSubhransu S. Prusty 		return 0;
211907f083abSSubhransu S. Prusty 
2120b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2121500e06b9SVinod Koul 	if (!hlink) {
2122500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2123500e06b9SVinod Koul 		return -EIO;
2124500e06b9SVinod Koul 	}
2125500e06b9SVinod Koul 
2126b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
2127b2047e99SVinod Koul 
212807f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
212907f083abSSubhransu S. Prusty 	if (err < 0) {
213007f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
213107f083abSSubhransu S. Prusty 		return err;
213207f083abSSubhransu S. Prusty 	}
213307f083abSSubhransu S. Prusty 
2134ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
2135ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
2136ab85f5b3SSubhransu S. Prusty 
2137e342ac08SSubhransu S. Prusty 	/* Power up afg */
21381b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
21391b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2140e342ac08SSubhransu S. Prusty 
2141e342ac08SSubhransu S. Prusty 	return 0;
2142e342ac08SSubhransu S. Prusty }
2143e342ac08SSubhransu S. Prusty #else
2144e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2145e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2146e342ac08SSubhransu S. Prusty #endif
2147e342ac08SSubhransu S. Prusty 
2148e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2149e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
21501b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
21510fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
2152e342ac08SSubhransu S. Prusty };
2153e342ac08SSubhransu S. Prusty 
215418382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
215518382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2156e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2157cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2158*5622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2159*5622bc95SPradeep Tewani 						   &intel_glk_drv_data),
216018382eadSSubhransu S. Prusty 	{}
216118382eadSSubhransu S. Prusty };
216218382eadSSubhransu S. Prusty 
216318382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
216418382eadSSubhransu S. Prusty 
216518382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
216618382eadSSubhransu S. Prusty 	. hdac = {
216718382eadSSubhransu S. Prusty 		.driver = {
216818382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2169e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
217018382eadSSubhransu S. Prusty 		},
217118382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
217218382eadSSubhransu S. Prusty 	},
217318382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
217418382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
217518382eadSSubhransu S. Prusty };
217618382eadSSubhransu S. Prusty 
217718382eadSSubhransu S. Prusty static int __init hdmi_init(void)
217818382eadSSubhransu S. Prusty {
217918382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
218018382eadSSubhransu S. Prusty }
218118382eadSSubhransu S. Prusty 
218218382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
218318382eadSSubhransu S. Prusty {
218418382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
218518382eadSSubhransu S. Prusty }
218618382eadSSubhransu S. Prusty 
218718382eadSSubhransu S. Prusty module_init(hdmi_init);
218818382eadSSubhransu S. Prusty module_exit(hdmi_exit);
218918382eadSSubhransu S. Prusty 
219018382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
219118382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
219218382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
219318382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2194