xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision e0e5d3e5a53b3bc354c18030b78b7ebcb33e004b)
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 {
92*e0e5d3e5SJeeja 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;
9818382eadSSubhransu S. Prusty };
9918382eadSSubhransu S. Prusty 
1004a3478deSJeeja KP struct hdac_hdmi_pcm {
1014a3478deSJeeja KP 	struct list_head head;
1024a3478deSJeeja KP 	int pcm_id;
103*e0e5d3e5SJeeja KP 	struct list_head port_list;
1044a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
1054a3478deSJeeja KP 	struct snd_jack *jack;
106c9bfb5d7SJeeja KP 	int stream_tag;
107c9bfb5d7SJeeja KP 	int channels;
108c9bfb5d7SJeeja KP 	int format;
109ab1eea19SJeeja KP 	bool chmap_set;
110ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
111ab1eea19SJeeja KP 	struct mutex lock;
112*e0e5d3e5SJeeja KP 	int jack_event;
1134a3478deSJeeja KP };
1144a3478deSJeeja KP 
115754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11618382eadSSubhransu S. Prusty 	int dai_id;
117754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11918382eadSSubhransu S. Prusty };
12018382eadSSubhransu S. Prusty 
12118382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
122754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12315b91447SSubhransu S. Prusty 	struct list_head pin_list;
12415b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1254a3478deSJeeja KP 	struct list_head pcm_list;
12615b91447SSubhransu S. Prusty 	int num_pin;
12715b91447SSubhransu S. Prusty 	int num_cvt;
128754695f9SJeeja KP 	int num_ports;
1294a3478deSJeeja KP 	struct mutex pin_mutex;
130bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
13118382eadSSubhransu S. Prusty };
13218382eadSSubhransu S. Prusty 
133c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
134c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
135c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
136c9bfb5d7SJeeja KP {
137c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1381de777feSJeeja KP 
139c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
140c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
141c9bfb5d7SJeeja KP 			break;
142c9bfb5d7SJeeja KP 	}
143c9bfb5d7SJeeja KP 
144c9bfb5d7SJeeja KP 	return pcm;
145c9bfb5d7SJeeja KP }
1461de777feSJeeja KP 
147*e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
148*e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
149*e0e5d3e5SJeeja KP {
150*e0e5d3e5SJeeja KP 	struct hdac_ext_device *edev = port->pin->edev;
151*e0e5d3e5SJeeja KP 
152*e0e5d3e5SJeeja KP 	if (is_connect) {
153*e0e5d3e5SJeeja KP 		/*
154*e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
155*e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
156*e0e5d3e5SJeeja KP 		 * ports.
157*e0e5d3e5SJeeja KP 		 */
158*e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
159*e0e5d3e5SJeeja KP 			dev_dbg(&edev->hdac.dev,
160*e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
161*e0e5d3e5SJeeja KP 					pcm->pcm_id);
162*e0e5d3e5SJeeja KP 			snd_jack_report(pcm->jack, SND_JACK_AVOUT);
163*e0e5d3e5SJeeja KP 		}
164*e0e5d3e5SJeeja KP 		pcm->jack_event++;
165*e0e5d3e5SJeeja KP 	} else {
166*e0e5d3e5SJeeja KP 		/*
167*e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
168*e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
169*e0e5d3e5SJeeja KP 		 * to multiple ports.
170*e0e5d3e5SJeeja KP 		 */
171*e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
172*e0e5d3e5SJeeja KP 			snd_jack_report(pcm->jack, 0);
173*e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
174*e0e5d3e5SJeeja KP 			pcm->jack_event--;
175*e0e5d3e5SJeeja KP 	}
176*e0e5d3e5SJeeja KP }
177*e0e5d3e5SJeeja KP 
178fc181b04SJeeja KP /* MST supported verbs */
179fc181b04SJeeja KP /*
180fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
181fc181b04SJeeja KP  */
182fc181b04SJeeja KP static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
183fc181b04SJeeja KP {
184fc181b04SJeeja KP 	unsigned int caps;
185fc181b04SJeeja KP 	unsigned int type, param;
186fc181b04SJeeja KP 
187fc181b04SJeeja KP 	caps = get_wcaps(&hdac->hdac, nid);
188fc181b04SJeeja KP 	type = get_wcaps_type(caps);
189fc181b04SJeeja KP 
190fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
191fc181b04SJeeja KP 		return 0;
192fc181b04SJeeja KP 
193fc181b04SJeeja KP 	param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
194fc181b04SJeeja KP 					AC_PAR_DEVLIST_LEN);
195fc181b04SJeeja KP 	if (param == -1)
196fc181b04SJeeja KP 		return param;
197fc181b04SJeeja KP 
198fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
199fc181b04SJeeja KP }
200fc181b04SJeeja KP 
201fc181b04SJeeja KP /*
202fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
203fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
204fc181b04SJeeja KP  * is selected or MST is not supported.
205fc181b04SJeeja KP  */
206fc181b04SJeeja KP static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
207fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
208fc181b04SJeeja KP {
209fc181b04SJeeja KP 	return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
210fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
211fc181b04SJeeja KP }
212fc181b04SJeeja KP 
213fc181b04SJeeja KP /*
214fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
215fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
216fc181b04SJeeja KP  */
217fc181b04SJeeja KP static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
218fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
219fc181b04SJeeja KP {
220fc181b04SJeeja KP 	int num_ports;
221fc181b04SJeeja KP 
222fc181b04SJeeja KP 	if (!port->pin->mst_capable)
223fc181b04SJeeja KP 		return 0;
224fc181b04SJeeja KP 
225fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
226fc181b04SJeeja KP 	num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
227fc181b04SJeeja KP 
228fc181b04SJeeja KP 	if (num_ports < 0)
229fc181b04SJeeja KP 		return -EIO;
230fc181b04SJeeja KP 	/*
231fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
232fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
233fc181b04SJeeja KP 	 */
234fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
235fc181b04SJeeja KP 		return 0;
236fc181b04SJeeja KP 
237fc181b04SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
238fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
239fc181b04SJeeja KP 
240fc181b04SJeeja KP 	if (port->id != hdac_hdmi_port_select_get(hdac, port))
241fc181b04SJeeja KP 		return -EIO;
242fc181b04SJeeja KP 
243fc181b04SJeeja KP 	dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
244fc181b04SJeeja KP 
245fc181b04SJeeja KP 	return 0;
246fc181b04SJeeja KP }
247fc181b04SJeeja KP 
2482889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2492889099eSSubhransu S. Prusty 						int pcm_idx)
2502889099eSSubhransu S. Prusty {
2512889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2522889099eSSubhransu S. Prusty 
2532889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2542889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2552889099eSSubhransu S. Prusty 			return pcm;
2562889099eSSubhransu S. Prusty 	}
2572889099eSSubhransu S. Prusty 
2582889099eSSubhransu S. Prusty 	return NULL;
2592889099eSSubhransu S. Prusty }
2602889099eSSubhransu S. Prusty 
261e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
262e342ac08SSubhransu S. Prusty {
26351b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
264e342ac08SSubhransu S. Prusty 
26551b2c425SGeliang Tang 	return to_ehdac_device(hdac);
266e342ac08SSubhransu S. Prusty }
267e342ac08SSubhransu S. Prusty 
2682428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2692428bca3SSubhransu S. Prusty {
2702428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2712428bca3SSubhransu S. Prusty }
2722428bca3SSubhransu S. Prusty 
2732428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2742428bca3SSubhransu S. Prusty {
2752428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2762428bca3SSubhransu S. Prusty }
2772428bca3SSubhransu S. Prusty 
2782428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2792428bca3SSubhransu S. Prusty 						void *eld)
2802428bca3SSubhransu S. Prusty {
2812428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
2822428bca3SSubhransu S. Prusty 	int i;
2832428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
2842428bca3SSubhransu S. Prusty 
2852428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
2862428bca3SSubhransu S. Prusty 	if (!sad)
2872428bca3SSubhransu S. Prusty 		goto format_constraint;
2882428bca3SSubhransu S. Prusty 
2892428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
2902428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
2912428bca3SSubhransu S. Prusty 
2922428bca3SSubhransu S. Prusty 			/*
2932428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
2942428bca3SSubhransu S. Prusty 			 * container so we set S32
2952428bca3SSubhransu S. Prusty 			 */
2962428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
2972428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
2982428bca3SSubhransu S. Prusty 		}
2992428bca3SSubhransu S. Prusty 	}
3002428bca3SSubhransu S. Prusty 
3012428bca3SSubhransu S. Prusty format_constraint:
3022428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3032428bca3SSubhransu S. Prusty 				formats);
3042428bca3SSubhransu S. Prusty 
3052428bca3SSubhransu S. Prusty }
3062428bca3SSubhransu S. Prusty 
307a657f1d0SSubhransu S. Prusty static void
308a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
309a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
310a657f1d0SSubhransu S. Prusty {
311a657f1d0SSubhransu S. Prusty 	int val;
312a657f1d0SSubhransu S. Prusty 
313a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
314a657f1d0SSubhransu S. Prusty 
315a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
316a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
317a657f1d0SSubhransu S. Prusty }
318a657f1d0SSubhransu S. Prusty 
319478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
320478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
321478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
322478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
323478f544eSSubhransu S. Prusty 
324478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
325478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
326478f544eSSubhransu S. Prusty 	u8 CXT04;
327478f544eSSubhransu S. Prusty 	u8 CA;
328478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
329478f544eSSubhransu S. Prusty };
330478f544eSSubhransu S. Prusty 
331a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
332754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
333a657f1d0SSubhransu S. Prusty {
334a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
335a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
336754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
337478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
338478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
339ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
340478f544eSSubhransu S. Prusty 	u8 *dip;
341a657f1d0SSubhransu S. Prusty 	int ret;
342a657f1d0SSubhransu S. Prusty 	int i;
343478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
344478f544eSSubhransu S. Prusty 	u8 conn_type;
345bcced704SSubhransu S. Prusty 	int channels, ca;
346a657f1d0SSubhransu S. Prusty 
347754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
348ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
349bcced704SSubhransu S. Prusty 
350bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
351ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
352bcced704SSubhransu S. Prusty 
353bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
354ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
355bcced704SSubhransu S. Prusty 
356754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
357478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
358a657f1d0SSubhransu S. Prusty 
359478f544eSSubhransu S. Prusty 	switch (conn_type) {
360478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
361478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
362478f544eSSubhransu S. Prusty 
363478f544eSSubhransu S. Prusty 		frame.channels = channels;
364bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
365a657f1d0SSubhransu S. Prusty 
366a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
367a657f1d0SSubhransu S. Prusty 		if (ret < 0)
368a657f1d0SSubhransu S. Prusty 			return ret;
369a657f1d0SSubhransu S. Prusty 
370478f544eSSubhransu S. Prusty 		break;
371478f544eSSubhransu S. Prusty 
372478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
373478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
374478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
375478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
376478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
377478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
378bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
379478f544eSSubhransu S. Prusty 
380478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
381478f544eSSubhransu S. Prusty 		break;
382478f544eSSubhransu S. Prusty 
383478f544eSSubhransu S. Prusty 	default:
384478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
385478f544eSSubhransu S. Prusty 						conn_type);
386478f544eSSubhransu S. Prusty 		return -EIO;
387478f544eSSubhransu S. Prusty 	}
388478f544eSSubhransu S. Prusty 
389a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
390ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
391ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
392a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
393a657f1d0SSubhransu S. Prusty 
394a657f1d0SSubhransu S. Prusty 
395a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
396ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
397478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
398391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
399ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
400391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
401478f544eSSubhransu S. Prusty 	} else {
402478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
403ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
404478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
405478f544eSSubhransu S. Prusty 	}
406a657f1d0SSubhransu S. Prusty 
407a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
408ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
409ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
410a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
411a657f1d0SSubhransu S. Prusty 
412a657f1d0SSubhransu S. Prusty 	return 0;
413a657f1d0SSubhransu S. Prusty }
414a657f1d0SSubhransu S. Prusty 
415c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
416c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
417c9bfb5d7SJeeja KP 		int slots, int slot_width)
418b0362adbSSubhransu S. Prusty {
419c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
420c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
421754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
422c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
423c9bfb5d7SJeeja KP 
424c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
425b0362adbSSubhransu S. Prusty 
426b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
427b0362adbSSubhransu S. Prusty 
428c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
429b0362adbSSubhransu S. Prusty 
430c9bfb5d7SJeeja KP 	if (pcm)
431c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
432bcced704SSubhransu S. Prusty 
433c9bfb5d7SJeeja KP 	return 0;
434b0362adbSSubhransu S. Prusty }
435b0362adbSSubhransu S. Prusty 
436b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
437b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
438b0362adbSSubhransu S. Prusty {
439b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
44054dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
441754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
442754695f9SJeeja KP 	struct hdac_hdmi_port *port;
443c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
444c9bfb5d7SJeeja KP 	int format;
445b0362adbSSubhransu S. Prusty 
44654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
447754695f9SJeeja KP 	port = dai_map->port;
44854dfa1eaSSubhransu S. Prusty 
449754695f9SJeeja KP 	if (!port)
45054dfa1eaSSubhransu S. Prusty 		return -ENODEV;
45154dfa1eaSSubhransu S. Prusty 
452754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
453754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
454754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
455754695f9SJeeja KP 					port->pin->nid, port->id);
456b0362adbSSubhransu S. Prusty 		return -ENODEV;
457b0362adbSSubhransu S. Prusty 	}
458b0362adbSSubhransu S. Prusty 
459c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
460b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
461b0362adbSSubhransu S. Prusty 			24, 0);
462b0362adbSSubhransu S. Prusty 
463c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
464c9bfb5d7SJeeja KP 	if (!pcm)
465148569fdSSubhransu S. Prusty 		return -EIO;
466148569fdSSubhransu S. Prusty 
467c9bfb5d7SJeeja KP 	pcm->format = format;
468c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
469148569fdSSubhransu S. Prusty 
470148569fdSSubhransu S. Prusty 	return 0;
471148569fdSSubhransu S. Prusty }
472148569fdSSubhransu S. Prusty 
473754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
474754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
475754695f9SJeeja KP 					struct hdac_hdmi_port *port)
476148569fdSSubhransu S. Prusty {
477148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
478148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
479148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
480148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
481148569fdSSubhransu S. Prusty 		return -EINVAL;
482148569fdSSubhransu S. Prusty 	}
483148569fdSSubhransu S. Prusty 
4841b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(hdac, port) < 0)
4851b46ebd1SJeeja KP 		return -EIO;
4861b46ebd1SJeeja KP 
487754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
488754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
489754695f9SJeeja KP 	if (port->num_mux_nids == 0)
490754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
491754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
492754695f9SJeeja KP 						pin->nid, port->id);
493148569fdSSubhransu S. Prusty 
494754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
495754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
496148569fdSSubhransu S. Prusty 
497754695f9SJeeja KP 	return port->num_mux_nids;
498148569fdSSubhransu S. Prusty }
499148569fdSSubhransu S. Prusty 
500148569fdSSubhransu S. Prusty /*
501754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
502148569fdSSubhransu S. Prusty  *
503754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
504148569fdSSubhransu S. Prusty  *
505754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
506754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
507148569fdSSubhransu S. Prusty  * connected.
508148569fdSSubhransu S. Prusty  */
509754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
510148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
511148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
512148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
513148569fdSSubhransu S. Prusty {
514148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
515754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
516148569fdSSubhransu S. Prusty 	int ret, i;
517148569fdSSubhransu S. Prusty 
518148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
519148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
520*e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
521*e0e5d3e5SJeeja KP 				continue;
522148569fdSSubhransu S. Prusty 
523*e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
524*e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
525*e0e5d3e5SJeeja KP 				ret = hdac_hdmi_query_port_connlist(edev,
526*e0e5d3e5SJeeja KP 							port->pin, port);
527*e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
528148569fdSSubhransu S. Prusty 				if (ret < 0)
529*e0e5d3e5SJeeja KP 					continue;
530148569fdSSubhransu S. Prusty 
531754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
532*e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
533*e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
534*e0e5d3e5SJeeja KP 						port->eld.eld_valid)
535754695f9SJeeja KP 						return port;
536148569fdSSubhransu S. Prusty 				}
537148569fdSSubhransu S. Prusty 			}
538*e0e5d3e5SJeeja KP 		}
539*e0e5d3e5SJeeja KP 	}
540148569fdSSubhransu S. Prusty 
541148569fdSSubhransu S. Prusty 	return NULL;
542148569fdSSubhransu S. Prusty }
543148569fdSSubhransu S. Prusty 
54454dfa1eaSSubhransu S. Prusty /*
54554dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
54654dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
54754dfa1eaSSubhransu S. Prusty  * doesn't fail.
54854dfa1eaSSubhransu S. Prusty  */
549b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
550b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
551b0362adbSSubhransu S. Prusty {
552b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
553b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
554754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
555148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
556754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5572428bca3SSubhransu S. Prusty 	int ret;
558b0362adbSSubhransu S. Prusty 
559b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
560b0362adbSSubhransu S. Prusty 
561148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
562754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
56354dfa1eaSSubhransu S. Prusty 
56454dfa1eaSSubhransu S. Prusty 	/*
56554dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
56654dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
56754dfa1eaSSubhransu S. Prusty 	 */
568754695f9SJeeja KP 	if (!port)
56954dfa1eaSSubhransu S. Prusty 		return 0;
570754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
571754695f9SJeeja KP 			(!port->eld.eld_valid)) {
572b0362adbSSubhransu S. Prusty 
57354dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
574754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
575754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
576754695f9SJeeja KP 			port->pin->nid, port->id);
577b8a54545SSubhransu S. Prusty 
57854dfa1eaSSubhransu S. Prusty 		return 0;
579b0362adbSSubhransu S. Prusty 	}
580b0362adbSSubhransu S. Prusty 
581754695f9SJeeja KP 	dai_map->port = port;
582b0362adbSSubhransu S. Prusty 
5832428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
584754695f9SJeeja KP 				port->eld.eld_buffer);
5852428bca3SSubhransu S. Prusty 	if (ret < 0)
5862428bca3SSubhransu S. Prusty 		return ret;
587b0362adbSSubhransu S. Prusty 
5882428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
589754695f9SJeeja KP 				port->eld.eld_buffer);
590b0362adbSSubhransu S. Prusty }
591b0362adbSSubhransu S. Prusty 
592b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
593b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
594b0362adbSSubhransu S. Prusty {
595b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
596b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
597754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
598ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
599b0362adbSSubhransu S. Prusty 
600b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
601b0362adbSSubhransu S. Prusty 
602ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
603bcced704SSubhransu S. Prusty 
604ab1eea19SJeeja KP 	if (pcm) {
605ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
606ab1eea19SJeeja KP 		pcm->chmap_set = false;
607ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
608ab1eea19SJeeja KP 		pcm->channels = 0;
609ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
610b0362adbSSubhransu S. Prusty 	}
611ab1eea19SJeeja KP 
612754695f9SJeeja KP 	if (dai_map->port)
613754695f9SJeeja KP 		dai_map->port = NULL;
61454dfa1eaSSubhransu S. Prusty }
615b0362adbSSubhransu S. Prusty 
61618382eadSSubhransu S. Prusty static int
61718382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
61818382eadSSubhransu S. Prusty {
619bcced704SSubhransu S. Prusty 	unsigned int chans;
620bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
621bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
62218382eadSSubhransu S. Prusty 	int err;
62318382eadSSubhransu S. Prusty 
624bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
625bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
626bcced704SSubhransu S. Prusty 
627bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
628bcced704SSubhransu S. Prusty 
629bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
630bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
631bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
63218382eadSSubhransu S. Prusty 
63318382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
63418382eadSSubhransu S. Prusty 			&cvt->params.rates,
63518382eadSSubhransu S. Prusty 			&cvt->params.formats,
63618382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
63718382eadSSubhransu S. Prusty 	if (err < 0)
63818382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
63918382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
64018382eadSSubhransu S. Prusty 			cvt->nid, err);
64118382eadSSubhransu S. Prusty 
64218382eadSSubhransu S. Prusty 	return err;
64318382eadSSubhransu S. Prusty }
64418382eadSSubhransu S. Prusty 
64579f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
646c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
647c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
648c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
649c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
650c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
65118382eadSSubhransu S. Prusty {
65218382eadSSubhransu S. Prusty 	w->id = id;
65379f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
65479f4e922SSubhransu S. Prusty 	if (!w->name)
65579f4e922SSubhransu S. Prusty 		return -ENOMEM;
65679f4e922SSubhransu S. Prusty 
65718382eadSSubhransu S. Prusty 	w->sname = stream;
65818382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
65918382eadSSubhransu S. Prusty 	w->shift = 0;
66079f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
66179f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
66279f4e922SSubhransu S. Prusty 	w->priv = priv;
663c9bfb5d7SJeeja KP 	w->event = event;
664c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
66579f4e922SSubhransu S. Prusty 
66679f4e922SSubhransu S. Prusty 	return 0;
66718382eadSSubhransu S. Prusty }
66818382eadSSubhransu S. Prusty 
66918382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
67079f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
67179f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
67279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
67318382eadSSubhransu S. Prusty {
67418382eadSSubhransu S. Prusty 	route->sink = sink;
67518382eadSSubhransu S. Prusty 	route->source = src;
67618382eadSSubhransu S. Prusty 	route->control = control;
67779f4e922SSubhransu S. Prusty 	route->connected = handler;
67818382eadSSubhransu S. Prusty }
67918382eadSSubhransu S. Prusty 
6804a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
681754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6824a3478deSJeeja KP {
6834a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6844a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
685*e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6864a3478deSJeeja KP 
6874a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
688*e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
689754695f9SJeeja KP 			continue;
690754695f9SJeeja KP 
691*e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
692*e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
6934a3478deSJeeja KP 				return pcm;
6944a3478deSJeeja KP 		}
695*e0e5d3e5SJeeja KP 	}
6964a3478deSJeeja KP 
6974a3478deSJeeja KP 	return NULL;
6984a3478deSJeeja KP }
6994a3478deSJeeja KP 
700c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
701c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
702c9bfb5d7SJeeja KP {
703c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
704c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
705c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
706c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
707c9bfb5d7SJeeja KP 	}
708c9bfb5d7SJeeja KP }
709c9bfb5d7SJeeja KP 
710c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
711c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
712c9bfb5d7SJeeja KP {
713c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
714c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
715c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
716c9bfb5d7SJeeja KP }
717c9bfb5d7SJeeja KP 
718c9bfb5d7SJeeja KP 
719c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
720c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
721c9bfb5d7SJeeja KP {
722754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
723c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
724c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
725c9bfb5d7SJeeja KP 
726c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
727c9bfb5d7SJeeja KP 			__func__, w->name, event);
728c9bfb5d7SJeeja KP 
729754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
730c9bfb5d7SJeeja KP 	if (!pcm)
731c9bfb5d7SJeeja KP 		return -EIO;
732c9bfb5d7SJeeja KP 
7331b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7341b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
7351b46ebd1SJeeja KP 		return -EIO;
7361b46ebd1SJeeja KP 
737c9bfb5d7SJeeja KP 	switch (event) {
738c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
739754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
740c9bfb5d7SJeeja KP 
741c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
742754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
743c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
744c9bfb5d7SJeeja KP 
745754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
746c9bfb5d7SJeeja KP 
747754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
748c9bfb5d7SJeeja KP 
749c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
750754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
751c9bfb5d7SJeeja KP 
752c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
753754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
754c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
755c9bfb5d7SJeeja KP 
756754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
757c9bfb5d7SJeeja KP 		break;
758c9bfb5d7SJeeja KP 
759c9bfb5d7SJeeja KP 	}
760c9bfb5d7SJeeja KP 
761c9bfb5d7SJeeja KP 	return 0;
762c9bfb5d7SJeeja KP }
763c9bfb5d7SJeeja KP 
764c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
765c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
766c9bfb5d7SJeeja KP {
767c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
768c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
769c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
770c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
771c9bfb5d7SJeeja KP 
772c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
773c9bfb5d7SJeeja KP 			__func__, w->name, event);
774c9bfb5d7SJeeja KP 
775c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
776c9bfb5d7SJeeja KP 	if (!pcm)
777c9bfb5d7SJeeja KP 		return -EIO;
778c9bfb5d7SJeeja KP 
779c9bfb5d7SJeeja KP 	switch (event) {
780c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
781c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
782c9bfb5d7SJeeja KP 
783c9bfb5d7SJeeja KP 		/* Enable transmission */
784c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
785c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
786c9bfb5d7SJeeja KP 
787c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
788c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
789c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
790c9bfb5d7SJeeja KP 
791c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
792c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
793c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
794c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
795c9bfb5d7SJeeja KP 		break;
796c9bfb5d7SJeeja KP 
797c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
798c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
799c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
800c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
801c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
802c9bfb5d7SJeeja KP 
803c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
804c9bfb5d7SJeeja KP 		break;
805c9bfb5d7SJeeja KP 
806c9bfb5d7SJeeja KP 	}
807c9bfb5d7SJeeja KP 
808c9bfb5d7SJeeja KP 	return 0;
809c9bfb5d7SJeeja KP }
810c9bfb5d7SJeeja KP 
811c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
812c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
813c9bfb5d7SJeeja KP {
814754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
815c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
816c9bfb5d7SJeeja KP 	int mux_idx;
817c9bfb5d7SJeeja KP 
818c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
819c9bfb5d7SJeeja KP 			__func__, w->name, event);
820c9bfb5d7SJeeja KP 
821c9bfb5d7SJeeja KP 	if (!kc)
822c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
823c9bfb5d7SJeeja KP 
824c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8251b46ebd1SJeeja KP 
8261b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8271b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
8281b46ebd1SJeeja KP 		return -EIO;
8291b46ebd1SJeeja KP 
830c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
831754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
832c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
833c9bfb5d7SJeeja KP 	}
834c9bfb5d7SJeeja KP 
835c9bfb5d7SJeeja KP 	return 0;
836c9bfb5d7SJeeja KP }
837c9bfb5d7SJeeja KP 
8384a3478deSJeeja KP /*
8394a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8404a3478deSJeeja KP  */
841754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8424a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8434a3478deSJeeja KP {
8444a3478deSJeeja KP 	int ret;
845*e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8464a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8474a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8484a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
849754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8504a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
8514a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
8524a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8534a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8544a3478deSJeeja KP 
8554a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8564a3478deSJeeja KP 	if (ret < 0)
8574a3478deSJeeja KP 		return ret;
8584a3478deSJeeja KP 
859754695f9SJeeja KP 	if (port == NULL)
860754695f9SJeeja KP 		return -EINVAL;
861754695f9SJeeja KP 
8624a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8634a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
864*e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
865*e0e5d3e5SJeeja KP 			continue;
866*e0e5d3e5SJeeja KP 
867*e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
868*e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
869*e0e5d3e5SJeeja KP 					p->pin == port->pin) {
870*e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
871*e0e5d3e5SJeeja KP 				list_del(&p->head);
872*e0e5d3e5SJeeja KP 			}
873*e0e5d3e5SJeeja KP 		}
874*e0e5d3e5SJeeja KP 	}
8754a3478deSJeeja KP 
8764a3478deSJeeja KP 	/*
8774a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8784a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8794a3478deSJeeja KP 	 */
880*e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
881*e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
882*e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
883754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
884*e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
8854a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
8864a3478deSJeeja KP 				return ret;
8874a3478deSJeeja KP 			}
8884a3478deSJeeja KP 		}
889*e0e5d3e5SJeeja KP 	}
8904a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
8914a3478deSJeeja KP 
8924a3478deSJeeja KP 	return ret;
8934a3478deSJeeja KP }
8944a3478deSJeeja KP 
89579f4e922SSubhransu S. Prusty /*
89679f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
89779f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
89879f4e922SSubhransu S. Prusty  * widget runtime.
89979f4e922SSubhransu S. Prusty  *
90079f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
90179f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
90279f4e922SSubhransu S. Prusty  * "NONE"
90379f4e922SSubhransu S. Prusty  */
904754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
905754695f9SJeeja KP 				struct hdac_hdmi_port *port,
90679f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
90779f4e922SSubhransu S. Prusty 				const char *widget_name)
90818382eadSSubhransu S. Prusty {
90979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
910754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
91179f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
91279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
91379f4e922SSubhransu S. Prusty 	struct soc_enum *se;
91479f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
91579f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
91679f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
91779f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
91879f4e922SSubhransu S. Prusty 	int i = 0;
91979f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
92018382eadSSubhransu S. Prusty 
92179f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
92279f4e922SSubhransu S. Prusty 	if (!kc)
92379f4e922SSubhransu S. Prusty 		return -ENOMEM;
92418382eadSSubhransu S. Prusty 
92579f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
92679f4e922SSubhransu S. Prusty 	if (!se)
92779f4e922SSubhransu S. Prusty 		return -ENOMEM;
92818382eadSSubhransu S. Prusty 
929754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
93079f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
93179f4e922SSubhransu S. Prusty 	if (!kc->name)
93279f4e922SSubhransu S. Prusty 		return -ENOMEM;
93318382eadSSubhransu S. Prusty 
93479f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
93579f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
93679f4e922SSubhransu S. Prusty 	kc->access = 0;
93779f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
938754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
93979f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
94079f4e922SSubhransu S. Prusty 
94179f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
94279f4e922SSubhransu S. Prusty 
94379f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
94479f4e922SSubhransu S. Prusty 	se->items = num_items;
94579f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
94679f4e922SSubhransu S. Prusty 
94779f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
94879f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
94979f4e922SSubhransu S. Prusty 	if (!items[i])
95079f4e922SSubhransu S. Prusty 		return -ENOMEM;
95179f4e922SSubhransu S. Prusty 
95279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
95379f4e922SSubhransu S. Prusty 		i++;
95479f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
95579f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
95679f4e922SSubhransu S. Prusty 		if (!items[i])
95779f4e922SSubhransu S. Prusty 			return -ENOMEM;
95879f4e922SSubhransu S. Prusty 	}
95979f4e922SSubhransu S. Prusty 
96079f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
96179f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
96279f4e922SSubhransu S. Prusty 	if (!se->texts)
96379f4e922SSubhransu S. Prusty 		return -ENOMEM;
96479f4e922SSubhransu S. Prusty 
96579f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
966754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
967c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
968c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
96979f4e922SSubhransu S. Prusty }
97079f4e922SSubhransu S. Prusty 
97179f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
97279f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
97379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
97479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
97579f4e922SSubhransu S. Prusty {
97679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
97779f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
97879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
979754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
98079f4e922SSubhransu S. Prusty 	int i, j;
98179f4e922SSubhransu S. Prusty 
982754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
98379f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
98479f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
98579f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
98679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
98779f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
98879f4e922SSubhransu S. Prusty 					se->texts[j + 1],
98979f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
99079f4e922SSubhransu S. Prusty 
99179f4e922SSubhransu S. Prusty 			rindex++;
99279f4e922SSubhransu S. Prusty 		}
99379f4e922SSubhransu S. Prusty 
99479f4e922SSubhransu S. Prusty 		mux_index++;
99579f4e922SSubhransu S. Prusty 	}
99679f4e922SSubhransu S. Prusty }
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty /*
99979f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
100079f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1001754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1002754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
100379f4e922SSubhransu S. Prusty  *
1004754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1005754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
100679f4e922SSubhransu S. Prusty  *
100779f4e922SSubhransu S. Prusty  * Routes are added as below:
1008754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1009754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
101079f4e922SSubhransu S. Prusty  *
101179f4e922SSubhransu S. Prusty  * Total route elements:
1012754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
101379f4e922SSubhransu S. Prusty  */
101479f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
101579f4e922SSubhransu S. Prusty {
101679f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
101779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
101879f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
101979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
102079f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
102179f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
102279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
102379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1024754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
102579f4e922SSubhransu S. Prusty 
102679f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
102779f4e922SSubhransu S. Prusty 		return -EINVAL;
102879f4e922SSubhransu S. Prusty 
1029754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1030754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
103179f4e922SSubhransu S. Prusty 				GFP_KERNEL);
103279f4e922SSubhransu S. Prusty 
103379f4e922SSubhransu S. Prusty 	if (!widgets)
103479f4e922SSubhransu S. Prusty 		return -ENOMEM;
103579f4e922SSubhransu S. Prusty 
103679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
103779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
103879f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
103979f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1040c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1041c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1042c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1043c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
104479f4e922SSubhransu S. Prusty 		if (ret < 0)
104579f4e922SSubhransu S. Prusty 			return ret;
104679f4e922SSubhransu S. Prusty 		i++;
104779f4e922SSubhransu S. Prusty 	}
104879f4e922SSubhransu S. Prusty 
104979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1050754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1051754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1052754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
105379f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1054754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1055c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1056c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1057754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1058754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
105979f4e922SSubhransu S. Prusty 			if (ret < 0)
106079f4e922SSubhransu S. Prusty 				return ret;
106179f4e922SSubhransu S. Prusty 			i++;
106279f4e922SSubhransu S. Prusty 		}
1063754695f9SJeeja KP 	}
106479f4e922SSubhransu S. Prusty 
106579f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
106679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1067754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1068754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1069754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1070754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1071754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
107279f4e922SSubhransu S. Prusty 						widget_name);
107379f4e922SSubhransu S. Prusty 			if (ret < 0)
107479f4e922SSubhransu S. Prusty 				return ret;
107579f4e922SSubhransu S. Prusty 			i++;
107679f4e922SSubhransu S. Prusty 
107779f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
107879f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
107979f4e922SSubhransu S. Prusty 
108079f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
108179f4e922SSubhransu S. Prusty 			num_routes++;
108279f4e922SSubhransu S. Prusty 		}
1083754695f9SJeeja KP 	}
108479f4e922SSubhransu S. Prusty 
108579f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
108679f4e922SSubhransu S. Prusty 							GFP_KERNEL);
108779f4e922SSubhransu S. Prusty 	if (!route)
108879f4e922SSubhransu S. Prusty 		return -ENOMEM;
108979f4e922SSubhransu S. Prusty 
109079f4e922SSubhransu S. Prusty 	i = 0;
109179f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
109279f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1093754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
109479f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1095754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1096754695f9SJeeja KP 						hdmi->num_pin;
109779f4e922SSubhransu S. Prusty 
109879f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
109979f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
110079f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
110179f4e922SSubhransu S. Prusty 			i++;
1102754695f9SJeeja KP 		}
110379f4e922SSubhransu S. Prusty 	}
110479f4e922SSubhransu S. Prusty 
110579f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
110679f4e922SSubhransu S. Prusty 
110779f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1108754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
110979f4e922SSubhransu S. Prusty 
111079f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
111179f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
111279f4e922SSubhransu S. Prusty 
111379f4e922SSubhransu S. Prusty 	return 0;
111479f4e922SSubhransu S. Prusty 
111518382eadSSubhransu S. Prusty }
111618382eadSSubhransu S. Prusty 
111715b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
111818382eadSSubhransu S. Prusty {
111915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1120754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
112115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1122148569fdSSubhransu S. Prusty 	int dai_id = 0;
112318382eadSSubhransu S. Prusty 
1124148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
112515b91447SSubhransu S. Prusty 		return -EINVAL;
112618382eadSSubhransu S. Prusty 
1127148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1128148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1129148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
113015b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
113118382eadSSubhransu S. Prusty 
1132148569fdSSubhransu S. Prusty 		dai_id++;
1133148569fdSSubhransu S. Prusty 
1134148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1135148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1136148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1137148569fdSSubhransu S. Prusty 			break;
1138148569fdSSubhransu S. Prusty 		}
1139148569fdSSubhransu S. Prusty 	}
114018382eadSSubhransu S. Prusty 
114115b91447SSubhransu S. Prusty 	return 0;
114215b91447SSubhransu S. Prusty }
114315b91447SSubhransu S. Prusty 
114415b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
114515b91447SSubhransu S. Prusty {
114615b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
114715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11484a3478deSJeeja KP 	char name[NAME_SIZE];
114915b91447SSubhransu S. Prusty 
115015b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
115115b91447SSubhransu S. Prusty 	if (!cvt)
115215b91447SSubhransu S. Prusty 		return -ENOMEM;
115315b91447SSubhransu S. Prusty 
115415b91447SSubhransu S. Prusty 	cvt->nid = nid;
11554a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11564a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
115715b91447SSubhransu S. Prusty 
115815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
115915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
116015b91447SSubhransu S. Prusty 
116115b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
116215b91447SSubhransu S. Prusty }
116315b91447SSubhransu S. Prusty 
1164f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1165754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1166b7756edeSSubhransu S. Prusty {
1167f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1168f6fa11a3SSandeep Tayal 
1169754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1170f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1171f6fa11a3SSandeep Tayal 
1172f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1173f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1174f6fa11a3SSandeep Tayal 		return -EINVAL;
1175b7756edeSSubhransu S. Prusty 	}
1176b7756edeSSubhransu S. Prusty 
1177754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1178f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1179f6fa11a3SSandeep Tayal 
1180f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1181f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1182f6fa11a3SSandeep Tayal 		return -EINVAL;
1183f6fa11a3SSandeep Tayal 	}
1184f6fa11a3SSandeep Tayal 
1185754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1186f6fa11a3SSandeep Tayal 
1187f6fa11a3SSandeep Tayal 	return 0;
1188f6fa11a3SSandeep Tayal }
1189f6fa11a3SSandeep Tayal 
1190754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1191754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1192b8a54545SSubhransu S. Prusty {
1193b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
11944a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
11954a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1196754695f9SJeeja KP 	int size = 0;
11972acd8309SJeeja KP 	int port_id = -1;
1198754695f9SJeeja KP 
1199754695f9SJeeja KP 	if (!hdmi)
1200754695f9SJeeja KP 		return;
12014a3478deSJeeja KP 
12022acd8309SJeeja KP 	/*
12032acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12042acd8309SJeeja KP 	 * to be -1.
12052acd8309SJeeja KP 	 */
12064a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1207754695f9SJeeja KP 	port->eld.monitor_present = false;
1208f6fa11a3SSandeep Tayal 
12092acd8309SJeeja KP 	if (pin->mst_capable)
12102acd8309SJeeja KP 		port_id = port->id;
12112acd8309SJeeja KP 
12122acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1213754695f9SJeeja KP 				&port->eld.monitor_present,
1214754695f9SJeeja KP 				port->eld.eld_buffer,
1215f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1216f6fa11a3SSandeep Tayal 
1217f6fa11a3SSandeep Tayal 	if (size > 0) {
1218f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1219754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1220f6fa11a3SSandeep Tayal 			size = -EINVAL;
1221f6fa11a3SSandeep Tayal 	}
1222f6fa11a3SSandeep Tayal 
1223f6fa11a3SSandeep Tayal 	if (size > 0) {
1224754695f9SJeeja KP 		port->eld.eld_valid = true;
1225754695f9SJeeja KP 		port->eld.eld_size = size;
1226f6fa11a3SSandeep Tayal 	} else {
1227754695f9SJeeja KP 		port->eld.eld_valid = false;
1228754695f9SJeeja KP 		port->eld.eld_size = 0;
1229f6fa11a3SSandeep Tayal 	}
1230b8a54545SSubhransu S. Prusty 
1231754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12324a3478deSJeeja KP 
1233754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1234b8a54545SSubhransu S. Prusty 
1235*e0e5d3e5SJeeja KP 		dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1236754695f9SJeeja KP 						__func__, pin->nid, port->id);
12374a3478deSJeeja KP 
12384a3478deSJeeja KP 		/*
12394a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12404a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12414a3478deSJeeja KP 		 * control select.
12424a3478deSJeeja KP 		 */
1243*e0e5d3e5SJeeja KP 		if (pcm)
1244*e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12454a3478deSJeeja KP 
12464a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1247f6fa11a3SSandeep Tayal 		return;
1248b8a54545SSubhransu S. Prusty 	}
1249b8a54545SSubhransu S. Prusty 
1250754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1251*e0e5d3e5SJeeja KP 		if (pcm)
1252*e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12534a3478deSJeeja KP 
1254f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1255754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1256754695f9SJeeja KP 
1257754695f9SJeeja KP 	}
1258754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12594a3478deSJeeja KP }
12604a3478deSJeeja KP 
1261754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1262754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1263754695f9SJeeja KP {
1264754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1265754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1266754695f9SJeeja KP 	int i;
1267754695f9SJeeja KP 
1268754695f9SJeeja KP 	/*
1269754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1270754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1271754695f9SJeeja KP 	 * implemented.
1272754695f9SJeeja KP 	 */
1273754695f9SJeeja KP 
1274754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1275754695f9SJeeja KP 	if (!ports)
1276754695f9SJeeja KP 		return -ENOMEM;
1277754695f9SJeeja KP 
1278754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1279754695f9SJeeja KP 		ports[i].id = i;
1280754695f9SJeeja KP 		ports[i].pin = pin;
1281754695f9SJeeja KP 	}
1282754695f9SJeeja KP 	pin->ports = ports;
1283754695f9SJeeja KP 	pin->num_ports = max_ports;
1284754695f9SJeeja KP 	return 0;
1285b8a54545SSubhransu S. Prusty }
1286b8a54545SSubhransu S. Prusty 
128715b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
128815b91447SSubhransu S. Prusty {
128915b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
129015b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1291754695f9SJeeja KP 	int ret;
129215b91447SSubhransu S. Prusty 
129315b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
129415b91447SSubhransu S. Prusty 	if (!pin)
129515b91447SSubhransu S. Prusty 		return -ENOMEM;
129615b91447SSubhransu S. Prusty 
129715b91447SSubhransu S. Prusty 	pin->nid = nid;
12982acd8309SJeeja KP 	pin->mst_capable = false;
1299754695f9SJeeja KP 	pin->edev = edev;
1300754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1301754695f9SJeeja KP 	if (ret < 0)
1302754695f9SJeeja KP 		return ret;
130315b91447SSubhransu S. Prusty 
130415b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
130515b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1306754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1307b8a54545SSubhransu S. Prusty 
130815b91447SSubhransu S. Prusty 	return 0;
130918382eadSSubhransu S. Prusty }
131018382eadSSubhransu S. Prusty 
1311211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1312211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1313211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1314211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1315211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1316211caab7SSubhransu S. Prusty 
1317211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1318211caab7SSubhransu S. Prusty {
1319211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1320211caab7SSubhransu S. Prusty 
1321211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1322211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1323211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1324211caab7SSubhransu S. Prusty 		return;
1325211caab7SSubhransu S. Prusty 
1326211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1327211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1328211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1329211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1330211caab7SSubhransu S. Prusty 		return;
1331211caab7SSubhransu S. Prusty }
1332211caab7SSubhransu S. Prusty 
1333211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1334211caab7SSubhransu S. Prusty {
1335211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1336211caab7SSubhransu S. Prusty 
1337211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1338211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1339211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1340211caab7SSubhransu S. Prusty 		return;
1341211caab7SSubhransu S. Prusty 
1342211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1343211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1344211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1345211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1346211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1347211caab7SSubhransu S. Prusty 		return;
1348211caab7SSubhransu S. Prusty 
1349211caab7SSubhransu S. Prusty }
1350211caab7SSubhransu S. Prusty 
135117a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
135217a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
135317a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
135417a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1355c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
135617a42c45SSubhransu S. Prusty };
135717a42c45SSubhransu S. Prusty 
135817a42c45SSubhransu S. Prusty /*
135917a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
136017a42c45SSubhransu S. Prusty  * based on the number of converter queried.
136117a42c45SSubhransu S. Prusty  */
136217a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
136317a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
136417a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
136517a42c45SSubhransu S. Prusty {
136617a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
136717a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
136817a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
136917a42c45SSubhransu S. Prusty 	int i = 0;
137017a42c45SSubhransu S. Prusty 	u32 rates, bps;
137117a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
137217a42c45SSubhransu S. Prusty 	u64 formats;
137317a42c45SSubhransu S. Prusty 	int ret;
137417a42c45SSubhransu S. Prusty 
137517a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
137617a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
137717a42c45SSubhransu S. Prusty 			GFP_KERNEL);
137817a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
137917a42c45SSubhransu S. Prusty 		return -ENOMEM;
138017a42c45SSubhransu S. Prusty 
138117a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
138217a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
138317a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
138417a42c45SSubhransu S. Prusty 		if (ret)
138517a42c45SSubhransu S. Prusty 			return ret;
138617a42c45SSubhransu S. Prusty 
138717a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
138817a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
138917a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
139017a42c45SSubhransu S. Prusty 
139117a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
139217a42c45SSubhransu S. Prusty 			return -ENOMEM;
139317a42c45SSubhransu S. Prusty 
139417a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
139517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
139617a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
139717a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
139817a42c45SSubhransu S. Prusty 			return -ENOMEM;
139917a42c45SSubhransu S. Prusty 
140017a42c45SSubhransu S. Prusty 		/*
140117a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
140217a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
140317a42c45SSubhransu S. Prusty 		 */
140417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
140517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
140617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
140717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
140817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
140917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
141017a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
141117a42c45SSubhransu S. Prusty 
141217a42c45SSubhransu S. Prusty 		i++;
141317a42c45SSubhransu S. Prusty 	}
141417a42c45SSubhransu S. Prusty 
141517a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
141617a42c45SSubhransu S. Prusty 
141717a42c45SSubhransu S. Prusty 	return 0;
141817a42c45SSubhransu S. Prusty }
141917a42c45SSubhransu S. Prusty 
142018382eadSSubhransu S. Prusty /*
142118382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
142218382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
142318382eadSSubhransu S. Prusty  */
142417a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
142517a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
142618382eadSSubhransu S. Prusty {
142718382eadSSubhransu S. Prusty 	hda_nid_t nid;
14283c83ac23SSudip Mukherjee 	int i, num_nodes;
142918382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
143018382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
143115b91447SSubhransu S. Prusty 	int ret;
143218382eadSSubhransu S. Prusty 
1433211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1434211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1435211caab7SSubhransu S. Prusty 
14363c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1437541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
143818382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
143918382eadSSubhransu S. Prusty 		return -EINVAL;
144018382eadSSubhransu S. Prusty 	}
144118382eadSSubhransu S. Prusty 
14423c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
144318382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
144418382eadSSubhransu S. Prusty 
144518382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
144618382eadSSubhransu S. Prusty 		unsigned int caps;
144718382eadSSubhransu S. Prusty 		unsigned int type;
144818382eadSSubhransu S. Prusty 
144918382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
145018382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
145118382eadSSubhransu S. Prusty 
145218382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
145318382eadSSubhransu S. Prusty 			continue;
145418382eadSSubhransu S. Prusty 
145518382eadSSubhransu S. Prusty 		switch (type) {
145618382eadSSubhransu S. Prusty 
145718382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
145815b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
145915b91447SSubhransu S. Prusty 			if (ret < 0)
146015b91447SSubhransu S. Prusty 				return ret;
146118382eadSSubhransu S. Prusty 			break;
146218382eadSSubhransu S. Prusty 
146318382eadSSubhransu S. Prusty 		case AC_WID_PIN:
146415b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
146515b91447SSubhransu S. Prusty 			if (ret < 0)
146615b91447SSubhransu S. Prusty 				return ret;
146718382eadSSubhransu S. Prusty 			break;
146818382eadSSubhransu S. Prusty 		}
146918382eadSSubhransu S. Prusty 	}
147018382eadSSubhransu S. Prusty 
147118382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
147218382eadSSubhransu S. Prusty 
147315b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
147418382eadSSubhransu S. Prusty 		return -EIO;
147518382eadSSubhransu S. Prusty 
147617a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
147717a42c45SSubhransu S. Prusty 	if (ret) {
147817a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
147917a42c45SSubhransu S. Prusty 							ret);
148017a42c45SSubhransu S. Prusty 		return ret;
148117a42c45SSubhransu S. Prusty 	}
148217a42c45SSubhransu S. Prusty 
148317a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
148417a42c45SSubhransu S. Prusty 
148515b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
148618382eadSSubhransu S. Prusty }
148718382eadSSubhransu S. Prusty 
1488f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1489b8a54545SSubhransu S. Prusty {
1490b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1491b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1492754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1493754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1494b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
14952acd8309SJeeja KP 	int i;
1496b8a54545SSubhransu S. Prusty 
1497b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1498b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1499b8a54545SSubhransu S. Prusty 
1500754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1501754695f9SJeeja KP 							pin_nid, pipe);
1502b8a54545SSubhransu S. Prusty 
1503b8a54545SSubhransu S. Prusty 	/*
1504b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1505b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1506b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1507b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1508b8a54545SSubhransu S. Prusty 	 */
1509b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1510b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1511b8a54545SSubhransu S. Prusty 		return;
1512b8a54545SSubhransu S. Prusty 
1513b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1514b8a54545SSubhransu S. Prusty 		return;
1515b8a54545SSubhransu S. Prusty 
1516b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1517754695f9SJeeja KP 		if (pin->nid != pin_nid)
1518754695f9SJeeja KP 			continue;
1519754695f9SJeeja KP 
1520754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1521754695f9SJeeja KP 		if (pipe == -1) {
15222acd8309SJeeja KP 			pin->mst_capable = false;
1523754695f9SJeeja KP 			/* if not MST, default is port[0] */
1524754695f9SJeeja KP 			hport = &pin->ports[0];
15252acd8309SJeeja KP 			goto out;
15262acd8309SJeeja KP 		} else {
15272acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15282acd8309SJeeja KP 				pin->mst_capable = true;
15292acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15302acd8309SJeeja KP 					hport = &pin->ports[i];
15312acd8309SJeeja KP 					goto out;
15322acd8309SJeeja KP 				}
15332acd8309SJeeja KP 			}
1534b8a54545SSubhransu S. Prusty 		}
1535b8a54545SSubhransu S. Prusty 	}
1536b8a54545SSubhransu S. Prusty 
15372acd8309SJeeja KP out:
15382acd8309SJeeja KP 	if (pin && hport)
1539754695f9SJeeja KP 		hdac_hdmi_present_sense(pin, hport);
1540754695f9SJeeja KP }
1541754695f9SJeeja KP 
1542b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1543b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1544b8a54545SSubhransu S. Prusty };
1545b8a54545SSubhransu S. Prusty 
15462889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15472889099eSSubhransu S. Prusty 						int device)
15482889099eSSubhransu S. Prusty {
15492889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15502889099eSSubhransu S. Prusty 
15512889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15522889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15532889099eSSubhransu S. Prusty 			return rtd->pcm;
15542889099eSSubhransu S. Prusty 	}
15552889099eSSubhransu S. Prusty 
15562889099eSSubhransu S. Prusty 	return NULL;
15572889099eSSubhransu S. Prusty }
15582889099eSSubhransu S. Prusty 
15594a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
15604a3478deSJeeja KP {
15614a3478deSJeeja KP 	char jack_name[NAME_SIZE];
15624a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
15634a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
15644a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm =
15654a3478deSJeeja KP 		snd_soc_component_get_dapm(&codec->component);
15664a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15674a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
15682889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
15692889099eSSubhransu S. Prusty 	int err;
15704a3478deSJeeja KP 
15714a3478deSJeeja KP 	/*
15724a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
15734a3478deSJeeja KP 	 * add to the pcm list
15744a3478deSJeeja KP 	 */
15754a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
15764a3478deSJeeja KP 	if (!pcm)
15774a3478deSJeeja KP 		return -ENOMEM;
15784a3478deSJeeja KP 	pcm->pcm_id = device;
15794a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1580*e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
1581ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1582*e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
15832889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
15842889099eSSubhransu S. Prusty 	if (snd_pcm) {
15852889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
15862889099eSSubhransu S. Prusty 		if (err < 0) {
15872889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
15882889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
15892889099eSSubhransu S. Prusty 				err, device);
15902889099eSSubhransu S. Prusty 			kfree(pcm);
15912889099eSSubhransu S. Prusty 			return err;
15922889099eSSubhransu S. Prusty 		}
15932889099eSSubhransu S. Prusty 	}
15942889099eSSubhransu S. Prusty 
15954a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
15964a3478deSJeeja KP 
15974a3478deSJeeja KP 	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
15984a3478deSJeeja KP 
15994a3478deSJeeja KP 	return snd_jack_new(dapm->card->snd_card, jack_name,
16004a3478deSJeeja KP 		SND_JACK_AVOUT,	&pcm->jack, true, false);
16014a3478deSJeeja KP }
16024a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
16034a3478deSJeeja KP 
1604a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1605a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1606a9ce96bcSJeeja KP {
1607a9ce96bcSJeeja KP 	int i;
1608a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1609a9ce96bcSJeeja KP 
1610a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1611a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1612a9ce96bcSJeeja KP 
1613a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1614a9ce96bcSJeeja KP 				pin->mst_capable = false;
1615a9ce96bcSJeeja KP 			else
1616a9ce96bcSJeeja KP 				pin->mst_capable = true;
1617a9ce96bcSJeeja KP 		}
1618a9ce96bcSJeeja KP 
1619a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1620a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1621a9ce96bcSJeeja KP 				continue;
1622a9ce96bcSJeeja KP 
1623a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1624a9ce96bcSJeeja KP 		}
1625a9ce96bcSJeeja KP 	}
1626a9ce96bcSJeeja KP }
1627a9ce96bcSJeeja KP 
162818382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
162918382eadSSubhransu S. Prusty {
163018382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
163118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
163218382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
163318382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1634b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1635a9ce96bcSJeeja KP 	int ret;
163618382eadSSubhransu S. Prusty 
163718382eadSSubhransu S. Prusty 	edev->scodec = codec;
163818382eadSSubhransu S. Prusty 
1639b2047e99SVinod Koul 	/*
1640b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1641b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1642b2047e99SVinod Koul 	 */
1643b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1644500e06b9SVinod Koul 	if (!hlink) {
1645500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1646500e06b9SVinod Koul 		return -EIO;
1647500e06b9SVinod Koul 	}
1648500e06b9SVinod Koul 
1649b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1650b2047e99SVinod Koul 
165179f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
165279f4e922SSubhransu S. Prusty 	if (ret < 0)
165379f4e922SSubhransu S. Prusty 		return ret;
165418382eadSSubhransu S. Prusty 
1655b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1656b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1657b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1658b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1659b8a54545SSubhransu S. Prusty 				ret);
1660b8a54545SSubhransu S. Prusty 		return ret;
1661b8a54545SSubhransu S. Prusty 	}
1662b8a54545SSubhransu S. Prusty 
1663a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
166418382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
166518382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
166618382eadSSubhransu S. Prusty 
1667e342ac08SSubhransu S. Prusty 	/*
1668e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1669e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1670e342ac08SSubhransu S. Prusty 	 */
1671e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1672e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1673e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1674e342ac08SSubhransu S. Prusty 
1675e342ac08SSubhransu S. Prusty 	return 0;
1676e342ac08SSubhransu S. Prusty }
1677e342ac08SSubhransu S. Prusty 
1678e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1679e342ac08SSubhransu S. Prusty {
1680e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1681e342ac08SSubhransu S. Prusty 
1682e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
168318382eadSSubhransu S. Prusty 	return 0;
168418382eadSSubhransu S. Prusty }
168518382eadSSubhransu S. Prusty 
1686571d5078SJeeja KP #ifdef CONFIG_PM
16871b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
16881b377ccdSSubhransu S. Prusty {
16891b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
16901b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
16911b377ccdSSubhransu S. Prusty 
16921b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
16931b377ccdSSubhransu S. Prusty 
16941b377ccdSSubhransu S. Prusty 	/*
16951b377ccdSSubhransu S. Prusty 	 * Power down afg.
16961b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
16971b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
16981b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
16991b377ccdSSubhransu S. Prusty 	 * to read the state.
17001b377ccdSSubhransu S. Prusty 	 */
17011b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17021b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
17031b377ccdSSubhransu S. Prusty 
17041b377ccdSSubhransu S. Prusty 	return 0;
17051b377ccdSSubhransu S. Prusty }
17061b377ccdSSubhransu S. Prusty 
17070fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1708571d5078SJeeja KP {
17090fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1710571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1711571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
17121b377ccdSSubhransu S. Prusty 
17131b377ccdSSubhransu S. Prusty 	/* Power up afg */
17141b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
17151b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1716571d5078SJeeja KP 
1717571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1718571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1719571d5078SJeeja KP 
1720571d5078SJeeja KP 	/*
1721571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1722571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1723a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1724a9ce96bcSJeeja KP 	 * already set pin caps.
1725571d5078SJeeja KP 	 */
1726a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1727571d5078SJeeja KP 
17281b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1729571d5078SJeeja KP }
1730571d5078SJeeja KP #else
17311b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
17320fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1733571d5078SJeeja KP #endif
1734571d5078SJeeja KP 
173518382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
173618382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1737e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
173818382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
173918382eadSSubhransu S. Prusty };
174018382eadSSubhransu S. Prusty 
17412889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
17422889099eSSubhransu S. Prusty 					unsigned char *chmap)
17432889099eSSubhransu S. Prusty {
17442889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17452889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17462889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
17472889099eSSubhransu S. Prusty 
1748ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
17492889099eSSubhransu S. Prusty }
17502889099eSSubhransu S. Prusty 
17512889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
17522889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
17532889099eSSubhransu S. Prusty {
17542889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17552889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17562889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1757*e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1758*e0e5d3e5SJeeja KP 
1759*e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1760*e0e5d3e5SJeeja KP 		return;
17612889099eSSubhransu S. Prusty 
1762ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1763ab1eea19SJeeja KP 	pcm->chmap_set = true;
1764ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1765*e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
17662889099eSSubhransu S. Prusty 		if (prepared)
1767754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1768ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
17692889099eSSubhransu S. Prusty }
17702889099eSSubhransu S. Prusty 
17712889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
17722889099eSSubhransu S. Prusty {
17732889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17742889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17752889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
17762889099eSSubhransu S. Prusty 
1777*e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1778*e0e5d3e5SJeeja KP 		return false;
1779*e0e5d3e5SJeeja KP 
1780*e0e5d3e5SJeeja KP 	return true;
17812889099eSSubhransu S. Prusty }
17822889099eSSubhransu S. Prusty 
17832889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
17842889099eSSubhransu S. Prusty {
17852889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
17862889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
17872889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1788*e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1789*e0e5d3e5SJeeja KP 
1790*e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1791*e0e5d3e5SJeeja KP 		return 0;
1792*e0e5d3e5SJeeja KP 
1793*e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1794*e0e5d3e5SJeeja KP 
1795*e0e5d3e5SJeeja KP 	if (!port)
1796*e0e5d3e5SJeeja KP 		return 0;
17972889099eSSubhransu S. Prusty 
1798754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
17992889099eSSubhransu S. Prusty 		return 0;
18002889099eSSubhransu S. Prusty 
1801754695f9SJeeja KP 	return port->eld.info.spk_alloc;
18022889099eSSubhransu S. Prusty }
18032889099eSSubhransu S. Prusty 
180418382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
180518382eadSSubhransu S. Prusty {
180618382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
180718382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
180817a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1809b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
181017a42c45SSubhransu S. Prusty 	int num_dais = 0;
181118382eadSSubhransu S. Prusty 	int ret = 0;
181218382eadSSubhransu S. Prusty 
1813b2047e99SVinod Koul 	/* hold the ref while we probe */
1814b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1815500e06b9SVinod Koul 	if (!hlink) {
1816500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1817500e06b9SVinod Koul 		return -EIO;
1818500e06b9SVinod Koul 	}
1819500e06b9SVinod Koul 
1820b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1821b2047e99SVinod Koul 
182218382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
182318382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
182418382eadSSubhransu S. Prusty 		return -ENOMEM;
182518382eadSSubhransu S. Prusty 
182618382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1827bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
18282889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
18292889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
18302889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
18312889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
183218382eadSSubhransu S. Prusty 
183318382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
183418382eadSSubhransu S. Prusty 
183515b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
183615b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
18374a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
18384a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
183915b91447SSubhransu S. Prusty 
1840aeaccef0SRamesh Babu 	/*
1841aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1842aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1843aeaccef0SRamesh Babu 	 */
1844aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1845aeaccef0SRamesh Babu 	if (ret < 0) {
1846aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1847aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1848aeaccef0SRamesh Babu 			ret);
1849aeaccef0SRamesh Babu 		return ret;
1850aeaccef0SRamesh Babu 	}
1851aeaccef0SRamesh Babu 
185217a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
185317a42c45SSubhransu S. Prusty 	if (ret < 0) {
185417a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
185517a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
185618382eadSSubhransu S. Prusty 		return ret;
185717a42c45SSubhransu S. Prusty 	}
185818382eadSSubhransu S. Prusty 
185918382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1860b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
186117a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1862b2047e99SVinod Koul 
1863b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1864b2047e99SVinod Koul 
1865b2047e99SVinod Koul 	return ret;
186618382eadSSubhransu S. Prusty }
186718382eadSSubhransu S. Prusty 
186818382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
186918382eadSSubhransu S. Prusty {
187015b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
187115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
187215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
18734a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
1874*e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1875754695f9SJeeja KP 	int i;
187615b91447SSubhransu S. Prusty 
187718382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
187818382eadSSubhransu S. Prusty 
18794a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
18804a3478deSJeeja KP 		pcm->cvt = NULL;
1881*e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
1882*e0e5d3e5SJeeja KP 			continue;
1883*e0e5d3e5SJeeja KP 
1884*e0e5d3e5SJeeja KP 		list_for_each_entry(port, &pcm->port_list, head)
1885*e0e5d3e5SJeeja KP 			port = NULL;
1886*e0e5d3e5SJeeja KP 
18874a3478deSJeeja KP 		list_del(&pcm->head);
18884a3478deSJeeja KP 		kfree(pcm);
18894a3478deSJeeja KP 	}
18904a3478deSJeeja KP 
189115b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
189215b91447SSubhransu S. Prusty 		list_del(&cvt->head);
18934a3478deSJeeja KP 		kfree(cvt->name);
189415b91447SSubhransu S. Prusty 		kfree(cvt);
189515b91447SSubhransu S. Prusty 	}
189615b91447SSubhransu S. Prusty 
189715b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1898754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
1899754695f9SJeeja KP 			pin->ports[i].pin = NULL;
1900754695f9SJeeja KP 		kfree(pin->ports);
190115b91447SSubhransu S. Prusty 		list_del(&pin->head);
190215b91447SSubhransu S. Prusty 		kfree(pin);
190315b91447SSubhransu S. Prusty 	}
190415b91447SSubhransu S. Prusty 
190518382eadSSubhransu S. Prusty 	return 0;
190618382eadSSubhransu S. Prusty }
190718382eadSSubhransu S. Prusty 
1908e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
1909e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
1910e342ac08SSubhransu S. Prusty {
1911e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1912e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
191307f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1914b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1915b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
191607f083abSSubhransu S. Prusty 	int err;
1917e342ac08SSubhransu S. Prusty 
1918e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1919e342ac08SSubhransu S. Prusty 
192007f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
192107f083abSSubhransu S. Prusty 	if (!bus)
192207f083abSSubhransu S. Prusty 		return 0;
192307f083abSSubhransu S. Prusty 
19241b377ccdSSubhransu S. Prusty 	/*
19251b377ccdSSubhransu S. Prusty 	 * Power down afg.
19261b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
19271b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
19281b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
19291b377ccdSSubhransu S. Prusty 	 * to read the state.
19301b377ccdSSubhransu S. Prusty 	 */
19311b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
19321b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
193307f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
193407f083abSSubhransu S. Prusty 	if (err < 0) {
193507f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
193607f083abSSubhransu S. Prusty 		return err;
193707f083abSSubhransu S. Prusty 	}
193807f083abSSubhransu S. Prusty 
1939b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1940500e06b9SVinod Koul 	if (!hlink) {
1941500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1942500e06b9SVinod Koul 		return -EIO;
1943500e06b9SVinod Koul 	}
1944500e06b9SVinod Koul 
1945b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
1946b2047e99SVinod Koul 
1947e342ac08SSubhransu S. Prusty 	return 0;
1948e342ac08SSubhransu S. Prusty }
1949e342ac08SSubhransu S. Prusty 
1950e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
1951e342ac08SSubhransu S. Prusty {
1952e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1953e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
195407f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
1955b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1956b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
195707f083abSSubhransu S. Prusty 	int err;
1958e342ac08SSubhransu S. Prusty 
1959e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
1960e342ac08SSubhransu S. Prusty 
196107f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
196207f083abSSubhransu S. Prusty 	if (!bus)
196307f083abSSubhransu S. Prusty 		return 0;
196407f083abSSubhransu S. Prusty 
1965b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1966500e06b9SVinod Koul 	if (!hlink) {
1967500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
1968500e06b9SVinod Koul 		return -EIO;
1969500e06b9SVinod Koul 	}
1970500e06b9SVinod Koul 
1971b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
1972b2047e99SVinod Koul 
197307f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
197407f083abSSubhransu S. Prusty 	if (err < 0) {
197507f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
197607f083abSSubhransu S. Prusty 		return err;
197707f083abSSubhransu S. Prusty 	}
197807f083abSSubhransu S. Prusty 
1979ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1980ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1981ab85f5b3SSubhransu S. Prusty 
1982e342ac08SSubhransu S. Prusty 	/* Power up afg */
19831b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
19841b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1985e342ac08SSubhransu S. Prusty 
1986e342ac08SSubhransu S. Prusty 	return 0;
1987e342ac08SSubhransu S. Prusty }
1988e342ac08SSubhransu S. Prusty #else
1989e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
1990e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
1991e342ac08SSubhransu S. Prusty #endif
1992e342ac08SSubhransu S. Prusty 
1993e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
1994e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
19951b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
19960fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
1997e342ac08SSubhransu S. Prusty };
1998e342ac08SSubhransu S. Prusty 
199918382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
200018382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2001e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2002cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
200318382eadSSubhransu S. Prusty 	{}
200418382eadSSubhransu S. Prusty };
200518382eadSSubhransu S. Prusty 
200618382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
200718382eadSSubhransu S. Prusty 
200818382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
200918382eadSSubhransu S. Prusty 	. hdac = {
201018382eadSSubhransu S. Prusty 		.driver = {
201118382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2012e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
201318382eadSSubhransu S. Prusty 		},
201418382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
201518382eadSSubhransu S. Prusty 	},
201618382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
201718382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
201818382eadSSubhransu S. Prusty };
201918382eadSSubhransu S. Prusty 
202018382eadSSubhransu S. Prusty static int __init hdmi_init(void)
202118382eadSSubhransu S. Prusty {
202218382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
202318382eadSSubhransu S. Prusty }
202418382eadSSubhransu S. Prusty 
202518382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
202618382eadSSubhransu S. Prusty {
202718382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
202818382eadSSubhransu S. Prusty }
202918382eadSSubhransu S. Prusty 
203018382eadSSubhransu S. Prusty module_init(hdmi_init);
203118382eadSSubhransu S. Prusty module_exit(hdmi_exit);
203218382eadSSubhransu S. Prusty 
203318382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
203418382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
203518382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
203618382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2037