xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 66d6bbc6)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
3318382eadSSubhransu S. Prusty #include "../../hda/local.h"
344a3478deSJeeja KP #include "hdac_hdmi.h"
3518382eadSSubhransu S. Prusty 
3617a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3717a42c45SSubhransu S. Prusty 
38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
4018382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
41b0362adbSSubhransu S. Prusty 
4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4318382eadSSubhransu S. Prusty 
44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
45754695f9SJeeja KP #define HDA_MAX_PORTS		3
46148569fdSSubhransu S. Prusty 
47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
49b8a54545SSubhransu S. Prusty 
50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
53f6fa11a3SSandeep Tayal 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5518382eadSSubhransu S. Prusty 	unsigned int channels_min;
5618382eadSSubhransu S. Prusty 	unsigned int channels_max;
5718382eadSSubhransu S. Prusty 	u32 rates;
5818382eadSSubhransu S. Prusty 	u64 formats;
5918382eadSSubhransu S. Prusty 	unsigned int maxbps;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
6315b91447SSubhransu S. Prusty 	struct list_head head;
6418382eadSSubhransu S. Prusty 	hda_nid_t nid;
654a3478deSJeeja KP 	const char *name;
6618382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6718382eadSSubhransu S. Prusty };
6818382eadSSubhransu S. Prusty 
69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
71b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
72b7756edeSSubhransu S. Prusty };
73b7756edeSSubhransu S. Prusty 
74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
75b8a54545SSubhransu S. Prusty 	bool	monitor_present;
76b8a54545SSubhransu S. Prusty 	bool	eld_valid;
77b8a54545SSubhransu S. Prusty 	int	eld_size;
78b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
79b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
80b8a54545SSubhransu S. Prusty };
81b8a54545SSubhransu S. Prusty 
8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
8315b91447SSubhransu S. Prusty 	struct list_head head;
8418382eadSSubhransu S. Prusty 	hda_nid_t nid;
852acd8309SJeeja KP 	bool mst_capable;
86754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
87754695f9SJeeja KP 	int num_ports;
88754695f9SJeeja KP 	struct hdac_ext_device *edev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
980324e51bSJeeja KP 	const char *jack_pin;
990324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
1000324e51bSJeeja KP 	const char *output_pin;
10118382eadSSubhransu S. Prusty };
10218382eadSSubhransu S. Prusty 
1034a3478deSJeeja KP struct hdac_hdmi_pcm {
1044a3478deSJeeja KP 	struct list_head head;
1054a3478deSJeeja KP 	int pcm_id;
106e0e5d3e5SJeeja KP 	struct list_head port_list;
1074a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10862490016SJeeja KP 	struct snd_soc_jack *jack;
109c9bfb5d7SJeeja KP 	int stream_tag;
110c9bfb5d7SJeeja KP 	int channels;
111c9bfb5d7SJeeja KP 	int format;
112ab1eea19SJeeja KP 	bool chmap_set;
113ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
114ab1eea19SJeeja KP 	struct mutex lock;
115e0e5d3e5SJeeja KP 	int jack_event;
1164a3478deSJeeja KP };
1174a3478deSJeeja KP 
118754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11918382eadSSubhransu S. Prusty 	int dai_id;
120754695f9SJeeja KP 	struct hdac_hdmi_port *port;
12115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12218382eadSSubhransu S. Prusty };
12318382eadSSubhransu S. Prusty 
12418382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
125754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12615b91447SSubhransu S. Prusty 	struct list_head pin_list;
12715b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1284a3478deSJeeja KP 	struct list_head pcm_list;
12915b91447SSubhransu S. Prusty 	int num_pin;
13015b91447SSubhransu S. Prusty 	int num_cvt;
131754695f9SJeeja KP 	int num_ports;
1324a3478deSJeeja KP 	struct mutex pin_mutex;
133bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
13418382eadSSubhransu S. Prusty };
13518382eadSSubhransu S. Prusty 
136c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
137c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
138c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
139c9bfb5d7SJeeja KP {
140c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1411de777feSJeeja KP 
142c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
143c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
144c9bfb5d7SJeeja KP 			break;
145c9bfb5d7SJeeja KP 	}
146c9bfb5d7SJeeja KP 
147c9bfb5d7SJeeja KP 	return pcm;
148c9bfb5d7SJeeja KP }
1491de777feSJeeja KP 
150e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
151e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
152e0e5d3e5SJeeja KP {
153e0e5d3e5SJeeja KP 	struct hdac_ext_device *edev = port->pin->edev;
154e0e5d3e5SJeeja KP 
1550324e51bSJeeja KP 	if (is_connect)
1560324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1570324e51bSJeeja KP 	else
1580324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1590324e51bSJeeja KP 
160e0e5d3e5SJeeja KP 	if (is_connect) {
161e0e5d3e5SJeeja KP 		/*
162e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
163e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
164e0e5d3e5SJeeja KP 		 * ports.
165e0e5d3e5SJeeja KP 		 */
166e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
167e0e5d3e5SJeeja KP 			dev_dbg(&edev->hdac.dev,
168e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
169e0e5d3e5SJeeja KP 					pcm->pcm_id);
17062490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
17162490016SJeeja KP 						SND_JACK_AVOUT);
172e0e5d3e5SJeeja KP 		}
173e0e5d3e5SJeeja KP 		pcm->jack_event++;
174e0e5d3e5SJeeja KP 	} else {
175e0e5d3e5SJeeja KP 		/*
176e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
177e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
178e0e5d3e5SJeeja KP 		 * to multiple ports.
179e0e5d3e5SJeeja KP 		 */
180e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
18162490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
182e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
183e0e5d3e5SJeeja KP 			pcm->jack_event--;
184e0e5d3e5SJeeja KP 	}
1850324e51bSJeeja KP 
1860324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
187e0e5d3e5SJeeja KP }
188e0e5d3e5SJeeja KP 
189fc181b04SJeeja KP /* MST supported verbs */
190fc181b04SJeeja KP /*
191fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
192fc181b04SJeeja KP  */
193fc181b04SJeeja KP static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
194fc181b04SJeeja KP {
195fc181b04SJeeja KP 	unsigned int caps;
196fc181b04SJeeja KP 	unsigned int type, param;
197fc181b04SJeeja KP 
198fc181b04SJeeja KP 	caps = get_wcaps(&hdac->hdac, nid);
199fc181b04SJeeja KP 	type = get_wcaps_type(caps);
200fc181b04SJeeja KP 
201fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
202fc181b04SJeeja KP 		return 0;
203fc181b04SJeeja KP 
204fc181b04SJeeja KP 	param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
205fc181b04SJeeja KP 					AC_PAR_DEVLIST_LEN);
206fc181b04SJeeja KP 	if (param == -1)
207fc181b04SJeeja KP 		return param;
208fc181b04SJeeja KP 
209fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
210fc181b04SJeeja KP }
211fc181b04SJeeja KP 
212fc181b04SJeeja KP /*
213fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
214fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
215fc181b04SJeeja KP  * is selected or MST is not supported.
216fc181b04SJeeja KP  */
217fc181b04SJeeja KP static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
218fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
219fc181b04SJeeja KP {
220fc181b04SJeeja KP 	return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
221fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
222fc181b04SJeeja KP }
223fc181b04SJeeja KP 
224fc181b04SJeeja KP /*
225fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
226fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
227fc181b04SJeeja KP  */
228fc181b04SJeeja KP static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
229fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
230fc181b04SJeeja KP {
231fc181b04SJeeja KP 	int num_ports;
232fc181b04SJeeja KP 
233fc181b04SJeeja KP 	if (!port->pin->mst_capable)
234fc181b04SJeeja KP 		return 0;
235fc181b04SJeeja KP 
236fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
237fc181b04SJeeja KP 	num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
238fc181b04SJeeja KP 
239fc181b04SJeeja KP 	if (num_ports < 0)
240fc181b04SJeeja KP 		return -EIO;
241fc181b04SJeeja KP 	/*
242fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
243fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
244fc181b04SJeeja KP 	 */
245fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
246fc181b04SJeeja KP 		return 0;
247fc181b04SJeeja KP 
248fc181b04SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
249fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
250fc181b04SJeeja KP 
251fc181b04SJeeja KP 	if (port->id != hdac_hdmi_port_select_get(hdac, port))
252fc181b04SJeeja KP 		return -EIO;
253fc181b04SJeeja KP 
254fc181b04SJeeja KP 	dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
255fc181b04SJeeja KP 
256fc181b04SJeeja KP 	return 0;
257fc181b04SJeeja KP }
258fc181b04SJeeja KP 
2592889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2602889099eSSubhransu S. Prusty 						int pcm_idx)
2612889099eSSubhransu S. Prusty {
2622889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2632889099eSSubhransu S. Prusty 
2642889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2652889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2662889099eSSubhransu S. Prusty 			return pcm;
2672889099eSSubhransu S. Prusty 	}
2682889099eSSubhransu S. Prusty 
2692889099eSSubhransu S. Prusty 	return NULL;
2702889099eSSubhransu S. Prusty }
2712889099eSSubhransu S. Prusty 
272e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
273e342ac08SSubhransu S. Prusty {
27451b2c425SGeliang Tang 	struct hdac_device *hdac = dev_to_hdac_dev(dev);
275e342ac08SSubhransu S. Prusty 
27651b2c425SGeliang Tang 	return to_ehdac_device(hdac);
277e342ac08SSubhransu S. Prusty }
278e342ac08SSubhransu S. Prusty 
2792428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2802428bca3SSubhransu S. Prusty {
2812428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2822428bca3SSubhransu S. Prusty }
2832428bca3SSubhransu S. Prusty 
2842428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2852428bca3SSubhransu S. Prusty {
2862428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2872428bca3SSubhransu S. Prusty }
2882428bca3SSubhransu S. Prusty 
2892428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2902428bca3SSubhransu S. Prusty 						void *eld)
2912428bca3SSubhransu S. Prusty {
2922428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
2932428bca3SSubhransu S. Prusty 	int i;
2942428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
2952428bca3SSubhransu S. Prusty 
2962428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
2972428bca3SSubhransu S. Prusty 	if (!sad)
2982428bca3SSubhransu S. Prusty 		goto format_constraint;
2992428bca3SSubhransu S. Prusty 
3002428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3012428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3022428bca3SSubhransu S. Prusty 
3032428bca3SSubhransu S. Prusty 			/*
3042428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3052428bca3SSubhransu S. Prusty 			 * container so we set S32
3062428bca3SSubhransu S. Prusty 			 */
3072428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3082428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3092428bca3SSubhransu S. Prusty 		}
3102428bca3SSubhransu S. Prusty 	}
3112428bca3SSubhransu S. Prusty 
3122428bca3SSubhransu S. Prusty format_constraint:
3132428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3142428bca3SSubhransu S. Prusty 				formats);
3152428bca3SSubhransu S. Prusty 
3162428bca3SSubhransu S. Prusty }
3172428bca3SSubhransu S. Prusty 
318a657f1d0SSubhransu S. Prusty static void
319a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
320a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
321a657f1d0SSubhransu S. Prusty {
322a657f1d0SSubhransu S. Prusty 	int val;
323a657f1d0SSubhransu S. Prusty 
324a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
325a657f1d0SSubhransu S. Prusty 
326a657f1d0SSubhransu S. Prusty 	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
327a657f1d0SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_INDEX, val);
328a657f1d0SSubhransu S. Prusty }
329a657f1d0SSubhransu S. Prusty 
330478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
331478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
332478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
333478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
334478f544eSSubhransu S. Prusty 
335478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
336478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
337478f544eSSubhransu S. Prusty 	u8 CXT04;
338478f544eSSubhransu S. Prusty 	u8 CA;
339478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
340478f544eSSubhransu S. Prusty };
341478f544eSSubhransu S. Prusty 
342a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
343754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
344a657f1d0SSubhransu S. Prusty {
345a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
346a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
347754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
348478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
349478f544eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
350ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
351478f544eSSubhransu S. Prusty 	u8 *dip;
352a657f1d0SSubhransu S. Prusty 	int ret;
353a657f1d0SSubhransu S. Prusty 	int i;
354478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
355478f544eSSubhransu S. Prusty 	u8 conn_type;
356bcced704SSubhransu S. Prusty 	int channels, ca;
357a657f1d0SSubhransu S. Prusty 
358754695f9SJeeja KP 	ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
359ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
360bcced704SSubhransu S. Prusty 
361bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
362ab1eea19SJeeja KP 	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
363bcced704SSubhransu S. Prusty 
364bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
365ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
366bcced704SSubhransu S. Prusty 
367754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
368478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
369a657f1d0SSubhransu S. Prusty 
370478f544eSSubhransu S. Prusty 	switch (conn_type) {
371478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
372478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
373478f544eSSubhransu S. Prusty 
374478f544eSSubhransu S. Prusty 		frame.channels = channels;
375bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
376a657f1d0SSubhransu S. Prusty 
377a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
378a657f1d0SSubhransu S. Prusty 		if (ret < 0)
379a657f1d0SSubhransu S. Prusty 			return ret;
380a657f1d0SSubhransu S. Prusty 
381478f544eSSubhransu S. Prusty 		break;
382478f544eSSubhransu S. Prusty 
383478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
384478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
385478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
386478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
387478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
388478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
389bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
390478f544eSSubhransu S. Prusty 
391478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
392478f544eSSubhransu S. Prusty 		break;
393478f544eSSubhransu S. Prusty 
394478f544eSSubhransu S. Prusty 	default:
395478f544eSSubhransu S. Prusty 		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
396478f544eSSubhransu S. Prusty 						conn_type);
397478f544eSSubhransu S. Prusty 		return -EIO;
398478f544eSSubhransu S. Prusty 	}
399478f544eSSubhransu S. Prusty 
400a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
401ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
402ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
403a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
404a657f1d0SSubhransu S. Prusty 
405a657f1d0SSubhransu S. Prusty 
406a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
407ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
408478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
409391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
410ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
411391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
412478f544eSSubhransu S. Prusty 	} else {
413478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
414ab1eea19SJeeja KP 			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
415478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
416478f544eSSubhransu S. Prusty 	}
417a657f1d0SSubhransu S. Prusty 
418a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
419ab1eea19SJeeja KP 	hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
420ab1eea19SJeeja KP 	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
421a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
422a657f1d0SSubhransu S. Prusty 
423a657f1d0SSubhransu S. Prusty 	return 0;
424a657f1d0SSubhransu S. Prusty }
425a657f1d0SSubhransu S. Prusty 
426c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
427c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
428c9bfb5d7SJeeja KP 		int slots, int slot_width)
429b0362adbSSubhransu S. Prusty {
430c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
431c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
432754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
433c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
434c9bfb5d7SJeeja KP 
435c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
436b0362adbSSubhransu S. Prusty 
437b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
438b0362adbSSubhransu S. Prusty 
439c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
440b0362adbSSubhransu S. Prusty 
441c9bfb5d7SJeeja KP 	if (pcm)
442c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
443bcced704SSubhransu S. Prusty 
444c9bfb5d7SJeeja KP 	return 0;
445b0362adbSSubhransu S. Prusty }
446b0362adbSSubhransu S. Prusty 
447b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
448b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
449b0362adbSSubhransu S. Prusty {
450b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
45154dfa1eaSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
452754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
453754695f9SJeeja KP 	struct hdac_hdmi_port *port;
454c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
455c9bfb5d7SJeeja KP 	int format;
456b0362adbSSubhransu S. Prusty 
45754dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
458754695f9SJeeja KP 	port = dai_map->port;
45954dfa1eaSSubhransu S. Prusty 
460754695f9SJeeja KP 	if (!port)
46154dfa1eaSSubhransu S. Prusty 		return -ENODEV;
46254dfa1eaSSubhransu S. Prusty 
463754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
464754695f9SJeeja KP 		dev_err(&hdac->hdac.dev,
465754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
466754695f9SJeeja KP 					port->pin->nid, port->id);
467b0362adbSSubhransu S. Prusty 		return -ENODEV;
468b0362adbSSubhransu S. Prusty 	}
469b0362adbSSubhransu S. Prusty 
470c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
471b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47266d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
473b0362adbSSubhransu S. Prusty 
474c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
475c9bfb5d7SJeeja KP 	if (!pcm)
476148569fdSSubhransu S. Prusty 		return -EIO;
477148569fdSSubhransu S. Prusty 
478c9bfb5d7SJeeja KP 	pcm->format = format;
479c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
480148569fdSSubhransu S. Prusty 
481148569fdSSubhransu S. Prusty 	return 0;
482148569fdSSubhransu S. Prusty }
483148569fdSSubhransu S. Prusty 
484754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
485754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
486754695f9SJeeja KP 					struct hdac_hdmi_port *port)
487148569fdSSubhransu S. Prusty {
488148569fdSSubhransu S. Prusty 	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
489148569fdSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
490148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
491148569fdSSubhransu S. Prusty 			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
492148569fdSSubhransu S. Prusty 		return -EINVAL;
493148569fdSSubhransu S. Prusty 	}
494148569fdSSubhransu S. Prusty 
4951b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(hdac, port) < 0)
4961b46ebd1SJeeja KP 		return -EIO;
4971b46ebd1SJeeja KP 
498754695f9SJeeja KP 	port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
499754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
500754695f9SJeeja KP 	if (port->num_mux_nids == 0)
501754695f9SJeeja KP 		dev_warn(&hdac->hdac.dev,
502754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
503754695f9SJeeja KP 						pin->nid, port->id);
504148569fdSSubhransu S. Prusty 
505754695f9SJeeja KP 	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
506754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
507148569fdSSubhransu S. Prusty 
508754695f9SJeeja KP 	return port->num_mux_nids;
509148569fdSSubhransu S. Prusty }
510148569fdSSubhransu S. Prusty 
511148569fdSSubhransu S. Prusty /*
512754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
513148569fdSSubhransu S. Prusty  *
514754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
515148569fdSSubhransu S. Prusty  *
516754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
517754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
518148569fdSSubhransu S. Prusty  * connected.
519148569fdSSubhransu S. Prusty  */
520754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
521148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
522148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
523148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
524148569fdSSubhransu S. Prusty {
525148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
526754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
527148569fdSSubhransu S. Prusty 	int ret, i;
528148569fdSSubhransu S. Prusty 
529148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
530148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
531e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
532e0e5d3e5SJeeja KP 				continue;
533148569fdSSubhransu S. Prusty 
534e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
535e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
536e0e5d3e5SJeeja KP 				ret = hdac_hdmi_query_port_connlist(edev,
537e0e5d3e5SJeeja KP 							port->pin, port);
538e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
539148569fdSSubhransu S. Prusty 				if (ret < 0)
540e0e5d3e5SJeeja KP 					continue;
541148569fdSSubhransu S. Prusty 
542754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
543e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
544e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
545e0e5d3e5SJeeja KP 						port->eld.eld_valid)
546754695f9SJeeja KP 						return port;
547148569fdSSubhransu S. Prusty 				}
548148569fdSSubhransu S. Prusty 			}
549e0e5d3e5SJeeja KP 		}
550e0e5d3e5SJeeja KP 	}
551148569fdSSubhransu S. Prusty 
552148569fdSSubhransu S. Prusty 	return NULL;
553148569fdSSubhransu S. Prusty }
554148569fdSSubhransu S. Prusty 
55554dfa1eaSSubhransu S. Prusty /*
55654dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
55754dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
55854dfa1eaSSubhransu S. Prusty  * doesn't fail.
55954dfa1eaSSubhransu S. Prusty  */
560b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
561b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
562b0362adbSSubhransu S. Prusty {
563b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
564b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
565754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
566148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
567754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5682428bca3SSubhransu S. Prusty 	int ret;
569b0362adbSSubhransu S. Prusty 
570b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
571b0362adbSSubhransu S. Prusty 
572148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
573754695f9SJeeja KP 	port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
57454dfa1eaSSubhransu S. Prusty 
57554dfa1eaSSubhransu S. Prusty 	/*
57654dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
57754dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
57854dfa1eaSSubhransu S. Prusty 	 */
579754695f9SJeeja KP 	if (!port)
58054dfa1eaSSubhransu S. Prusty 		return 0;
581754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
582754695f9SJeeja KP 			(!port->eld.eld_valid)) {
583b0362adbSSubhransu S. Prusty 
58454dfa1eaSSubhransu S. Prusty 		dev_warn(&hdac->hdac.dev,
585754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
586754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
587754695f9SJeeja KP 			port->pin->nid, port->id);
588b8a54545SSubhransu S. Prusty 
58954dfa1eaSSubhransu S. Prusty 		return 0;
590b0362adbSSubhransu S. Prusty 	}
591b0362adbSSubhransu S. Prusty 
592754695f9SJeeja KP 	dai_map->port = port;
593b0362adbSSubhransu S. Prusty 
5942428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
595754695f9SJeeja KP 				port->eld.eld_buffer);
5962428bca3SSubhransu S. Prusty 	if (ret < 0)
5972428bca3SSubhransu S. Prusty 		return ret;
598b0362adbSSubhransu S. Prusty 
5992428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
600754695f9SJeeja KP 				port->eld.eld_buffer);
601b0362adbSSubhransu S. Prusty }
602b0362adbSSubhransu S. Prusty 
603b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
604b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
605b0362adbSSubhransu S. Prusty {
606b0362adbSSubhransu S. Prusty 	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
607b0362adbSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = hdac->private_data;
608754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
609ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
610b0362adbSSubhransu S. Prusty 
611b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
612b0362adbSSubhransu S. Prusty 
613ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
614bcced704SSubhransu S. Prusty 
615ab1eea19SJeeja KP 	if (pcm) {
616ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
617ab1eea19SJeeja KP 		pcm->chmap_set = false;
618ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
619ab1eea19SJeeja KP 		pcm->channels = 0;
620ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
621b0362adbSSubhransu S. Prusty 	}
622ab1eea19SJeeja KP 
623754695f9SJeeja KP 	if (dai_map->port)
624754695f9SJeeja KP 		dai_map->port = NULL;
62554dfa1eaSSubhransu S. Prusty }
626b0362adbSSubhransu S. Prusty 
62718382eadSSubhransu S. Prusty static int
62818382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
62918382eadSSubhransu S. Prusty {
630bcced704SSubhransu S. Prusty 	unsigned int chans;
631bcced704SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
632bcced704SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
63318382eadSSubhransu S. Prusty 	int err;
63418382eadSSubhransu S. Prusty 
635bcced704SSubhransu S. Prusty 	chans = get_wcaps(hdac, cvt->nid);
636bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
637bcced704SSubhransu S. Prusty 
638bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
639bcced704SSubhransu S. Prusty 
640bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
641bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
642bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
64318382eadSSubhransu S. Prusty 
64418382eadSSubhransu S. Prusty 	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
64518382eadSSubhransu S. Prusty 			&cvt->params.rates,
64618382eadSSubhransu S. Prusty 			&cvt->params.formats,
64718382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
64818382eadSSubhransu S. Prusty 	if (err < 0)
64918382eadSSubhransu S. Prusty 		dev_err(&hdac->dev,
65018382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
65118382eadSSubhransu S. Prusty 			cvt->nid, err);
65218382eadSSubhransu S. Prusty 
65318382eadSSubhransu S. Prusty 	return err;
65418382eadSSubhransu S. Prusty }
65518382eadSSubhransu S. Prusty 
65679f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
657c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
658c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
659c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
660c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
661c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
66218382eadSSubhransu S. Prusty {
66318382eadSSubhransu S. Prusty 	w->id = id;
66479f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
66579f4e922SSubhransu S. Prusty 	if (!w->name)
66679f4e922SSubhransu S. Prusty 		return -ENOMEM;
66779f4e922SSubhransu S. Prusty 
66818382eadSSubhransu S. Prusty 	w->sname = stream;
66918382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
67018382eadSSubhransu S. Prusty 	w->shift = 0;
67179f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
67279f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
67379f4e922SSubhransu S. Prusty 	w->priv = priv;
674c9bfb5d7SJeeja KP 	w->event = event;
675c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
67679f4e922SSubhransu S. Prusty 
67779f4e922SSubhransu S. Prusty 	return 0;
67818382eadSSubhransu S. Prusty }
67918382eadSSubhransu S. Prusty 
68018382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
68179f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
68279f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
68379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
68418382eadSSubhransu S. Prusty {
68518382eadSSubhransu S. Prusty 	route->sink = sink;
68618382eadSSubhransu S. Prusty 	route->source = src;
68718382eadSSubhransu S. Prusty 	route->control = control;
68879f4e922SSubhransu S. Prusty 	route->connected = handler;
68918382eadSSubhransu S. Prusty }
69018382eadSSubhransu S. Prusty 
6914a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
692754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6934a3478deSJeeja KP {
6944a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
6954a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
696e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6974a3478deSJeeja KP 
6984a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
699e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
700754695f9SJeeja KP 			continue;
701754695f9SJeeja KP 
702e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
703e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7044a3478deSJeeja KP 				return pcm;
7054a3478deSJeeja KP 		}
706e0e5d3e5SJeeja KP 	}
7074a3478deSJeeja KP 
7084a3478deSJeeja KP 	return NULL;
7094a3478deSJeeja KP }
7104a3478deSJeeja KP 
711c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
712c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
713c9bfb5d7SJeeja KP {
714c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
715c9bfb5d7SJeeja KP 		if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
716c9bfb5d7SJeeja KP 			snd_hdac_codec_write(&edev->hdac, nid, 0,
717c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
718c9bfb5d7SJeeja KP 	}
719c9bfb5d7SJeeja KP }
720c9bfb5d7SJeeja KP 
721c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
722c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
723c9bfb5d7SJeeja KP {
724c9bfb5d7SJeeja KP 	if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
725c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, nid, 0,
726c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
727c9bfb5d7SJeeja KP }
728c9bfb5d7SJeeja KP 
729c9bfb5d7SJeeja KP 
730c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
731c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
732c9bfb5d7SJeeja KP {
733754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
734c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
735c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
736c9bfb5d7SJeeja KP 
737c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
738c9bfb5d7SJeeja KP 			__func__, w->name, event);
739c9bfb5d7SJeeja KP 
740754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
741c9bfb5d7SJeeja KP 	if (!pcm)
742c9bfb5d7SJeeja KP 		return -EIO;
743c9bfb5d7SJeeja KP 
7441b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7451b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
7461b46ebd1SJeeja KP 		return -EIO;
7471b46ebd1SJeeja KP 
748c9bfb5d7SJeeja KP 	switch (event) {
749c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
750754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
751c9bfb5d7SJeeja KP 
752c9bfb5d7SJeeja KP 		/* Enable 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, PIN_OUT);
755c9bfb5d7SJeeja KP 
756754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
757c9bfb5d7SJeeja KP 
758754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
759c9bfb5d7SJeeja KP 
760c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
761754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
762c9bfb5d7SJeeja KP 
763c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
764754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
765c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
766c9bfb5d7SJeeja KP 
767754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
768c9bfb5d7SJeeja KP 		break;
769c9bfb5d7SJeeja KP 
770c9bfb5d7SJeeja KP 	}
771c9bfb5d7SJeeja KP 
772c9bfb5d7SJeeja KP 	return 0;
773c9bfb5d7SJeeja KP }
774c9bfb5d7SJeeja KP 
775c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
776c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
777c9bfb5d7SJeeja KP {
778c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
779c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
780c9bfb5d7SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
781c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
782c9bfb5d7SJeeja KP 
783c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
784c9bfb5d7SJeeja KP 			__func__, w->name, event);
785c9bfb5d7SJeeja KP 
786c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
787c9bfb5d7SJeeja KP 	if (!pcm)
788c9bfb5d7SJeeja KP 		return -EIO;
789c9bfb5d7SJeeja KP 
790c9bfb5d7SJeeja KP 	switch (event) {
791c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
792c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
793c9bfb5d7SJeeja KP 
794c9bfb5d7SJeeja KP 		/* Enable transmission */
795c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
796c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
797c9bfb5d7SJeeja KP 
798c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
799c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
800c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
801c9bfb5d7SJeeja KP 
802c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
803c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
804c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
805c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
806c9bfb5d7SJeeja KP 		break;
807c9bfb5d7SJeeja KP 
808c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
809c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
810c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
811c9bfb5d7SJeeja KP 		snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
812c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
813c9bfb5d7SJeeja KP 
814c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
815c9bfb5d7SJeeja KP 		break;
816c9bfb5d7SJeeja KP 
817c9bfb5d7SJeeja KP 	}
818c9bfb5d7SJeeja KP 
819c9bfb5d7SJeeja KP 	return 0;
820c9bfb5d7SJeeja KP }
821c9bfb5d7SJeeja KP 
822c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
823c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
824c9bfb5d7SJeeja KP {
825754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
826c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
827c9bfb5d7SJeeja KP 	int mux_idx;
828c9bfb5d7SJeeja KP 
829c9bfb5d7SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
830c9bfb5d7SJeeja KP 			__func__, w->name, event);
831c9bfb5d7SJeeja KP 
832c9bfb5d7SJeeja KP 	if (!kc)
833c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
834c9bfb5d7SJeeja KP 
835c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8361b46ebd1SJeeja KP 
8371b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8381b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
8391b46ebd1SJeeja KP 		return -EIO;
8401b46ebd1SJeeja KP 
841c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
842754695f9SJeeja KP 		snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
843c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
844c9bfb5d7SJeeja KP 	}
845c9bfb5d7SJeeja KP 
846c9bfb5d7SJeeja KP 	return 0;
847c9bfb5d7SJeeja KP }
848c9bfb5d7SJeeja KP 
8494a3478deSJeeja KP /*
8504a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8514a3478deSJeeja KP  */
852754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8534a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8544a3478deSJeeja KP {
8554a3478deSJeeja KP 	int ret;
856e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8574a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8584a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8594a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
860754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8614a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
8624a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
8634a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8644a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8654a3478deSJeeja KP 
8664a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8674a3478deSJeeja KP 	if (ret < 0)
8684a3478deSJeeja KP 		return ret;
8694a3478deSJeeja KP 
870754695f9SJeeja KP 	if (port == NULL)
871754695f9SJeeja KP 		return -EINVAL;
872754695f9SJeeja KP 
8734a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8744a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
875e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
876e0e5d3e5SJeeja KP 			continue;
877e0e5d3e5SJeeja KP 
878e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
879e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
880e0e5d3e5SJeeja KP 					p->pin == port->pin) {
881e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
882e0e5d3e5SJeeja KP 				list_del(&p->head);
883e0e5d3e5SJeeja KP 			}
884e0e5d3e5SJeeja KP 		}
885e0e5d3e5SJeeja KP 	}
8864a3478deSJeeja KP 
8874a3478deSJeeja KP 	/*
8884a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8894a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8904a3478deSJeeja KP 	 */
891e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
892e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
893e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
894754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
895e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
8964a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
8974a3478deSJeeja KP 				return ret;
8984a3478deSJeeja KP 			}
8994a3478deSJeeja KP 		}
900e0e5d3e5SJeeja KP 	}
9014a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9024a3478deSJeeja KP 
9034a3478deSJeeja KP 	return ret;
9044a3478deSJeeja KP }
9054a3478deSJeeja KP 
90679f4e922SSubhransu S. Prusty /*
90779f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
90879f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
90979f4e922SSubhransu S. Prusty  * widget runtime.
91079f4e922SSubhransu S. Prusty  *
91179f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
91279f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
91379f4e922SSubhransu S. Prusty  * "NONE"
91479f4e922SSubhransu S. Prusty  */
915754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
916754695f9SJeeja KP 				struct hdac_hdmi_port *port,
91779f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
91879f4e922SSubhransu S. Prusty 				const char *widget_name)
91918382eadSSubhransu S. Prusty {
92079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
921754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
92279f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
92379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
92479f4e922SSubhransu S. Prusty 	struct soc_enum *se;
92579f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
92679f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
92779f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
92879f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
92979f4e922SSubhransu S. Prusty 	int i = 0;
93079f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
93118382eadSSubhransu S. Prusty 
93279f4e922SSubhransu S. Prusty 	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
93379f4e922SSubhransu S. Prusty 	if (!kc)
93479f4e922SSubhransu S. Prusty 		return -ENOMEM;
93518382eadSSubhransu S. Prusty 
93679f4e922SSubhransu S. Prusty 	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
93779f4e922SSubhransu S. Prusty 	if (!se)
93879f4e922SSubhransu S. Prusty 		return -ENOMEM;
93918382eadSSubhransu S. Prusty 
940754695f9SJeeja KP 	sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
94179f4e922SSubhransu S. Prusty 	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
94279f4e922SSubhransu S. Prusty 	if (!kc->name)
94379f4e922SSubhransu S. Prusty 		return -ENOMEM;
94418382eadSSubhransu S. Prusty 
94579f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
94679f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
94779f4e922SSubhransu S. Prusty 	kc->access = 0;
94879f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
949754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95079f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
95179f4e922SSubhransu S. Prusty 
95279f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
95379f4e922SSubhransu S. Prusty 
95479f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
95579f4e922SSubhransu S. Prusty 	se->items = num_items;
95679f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
95779f4e922SSubhransu S. Prusty 
95879f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
95979f4e922SSubhransu S. Prusty 	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
96079f4e922SSubhransu S. Prusty 	if (!items[i])
96179f4e922SSubhransu S. Prusty 		return -ENOMEM;
96279f4e922SSubhransu S. Prusty 
96379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
96479f4e922SSubhransu S. Prusty 		i++;
96579f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
96679f4e922SSubhransu S. Prusty 		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
96779f4e922SSubhransu S. Prusty 		if (!items[i])
96879f4e922SSubhransu S. Prusty 			return -ENOMEM;
96979f4e922SSubhransu S. Prusty 	}
97079f4e922SSubhransu S. Prusty 
97179f4e922SSubhransu S. Prusty 	se->texts = devm_kmemdup(&edev->hdac.dev, items,
97279f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
97379f4e922SSubhransu S. Prusty 	if (!se->texts)
97479f4e922SSubhransu S. Prusty 		return -ENOMEM;
97579f4e922SSubhransu S. Prusty 
97679f4e922SSubhransu S. Prusty 	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
977754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
978c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
979c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98079f4e922SSubhransu S. Prusty }
98179f4e922SSubhransu S. Prusty 
98279f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
98379f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
98479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
98579f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
98679f4e922SSubhransu S. Prusty {
98779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
98879f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
98979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
990754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
99179f4e922SSubhransu S. Prusty 	int i, j;
99279f4e922SSubhransu S. Prusty 
993754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
99479f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
99579f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
99679f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
99779f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
99879f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
99979f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100079f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
100179f4e922SSubhransu S. Prusty 
100279f4e922SSubhransu S. Prusty 			rindex++;
100379f4e922SSubhransu S. Prusty 		}
100479f4e922SSubhransu S. Prusty 
100579f4e922SSubhransu S. Prusty 		mux_index++;
100679f4e922SSubhransu S. Prusty 	}
100779f4e922SSubhransu S. Prusty }
100879f4e922SSubhransu S. Prusty 
100979f4e922SSubhransu S. Prusty /*
101079f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
101179f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1012754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1013754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
101479f4e922SSubhransu S. Prusty  *
1015754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1016754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
101779f4e922SSubhransu S. Prusty  *
101879f4e922SSubhransu S. Prusty  * Routes are added as below:
1019754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1020754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
102179f4e922SSubhransu S. Prusty  *
102279f4e922SSubhransu S. Prusty  * Total route elements:
1023754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
102479f4e922SSubhransu S. Prusty  */
102579f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
102679f4e922SSubhransu S. Prusty {
102779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
102879f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
102979f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
103079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
103179f4e922SSubhransu S. Prusty 	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
103279f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
103379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
103479f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1035754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
103679f4e922SSubhransu S. Prusty 
103779f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
103879f4e922SSubhransu S. Prusty 		return -EINVAL;
103979f4e922SSubhransu S. Prusty 
1040754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1041754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
104279f4e922SSubhransu S. Prusty 				GFP_KERNEL);
104379f4e922SSubhransu S. Prusty 
104479f4e922SSubhransu S. Prusty 	if (!widgets)
104579f4e922SSubhransu S. Prusty 		return -ENOMEM;
104679f4e922SSubhransu S. Prusty 
104779f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
104879f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
104979f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105079f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1051c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1052c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1053c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1054c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
105579f4e922SSubhransu S. Prusty 		if (ret < 0)
105679f4e922SSubhransu S. Prusty 			return ret;
105779f4e922SSubhransu S. Prusty 		i++;
105879f4e922SSubhransu S. Prusty 	}
105979f4e922SSubhransu S. Prusty 
106079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1061754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1062754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1063754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
106479f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1065754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1066c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1067c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1068754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1069754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107079f4e922SSubhransu S. Prusty 			if (ret < 0)
107179f4e922SSubhransu S. Prusty 				return ret;
10720324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
107379f4e922SSubhransu S. Prusty 			i++;
107479f4e922SSubhransu S. Prusty 		}
1075754695f9SJeeja KP 	}
107679f4e922SSubhransu S. Prusty 
107779f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
107879f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1079754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1080754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1081754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1082754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1083754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
108479f4e922SSubhransu S. Prusty 						widget_name);
108579f4e922SSubhransu S. Prusty 			if (ret < 0)
108679f4e922SSubhransu S. Prusty 				return ret;
108779f4e922SSubhransu S. Prusty 			i++;
108879f4e922SSubhransu S. Prusty 
108979f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109079f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
109179f4e922SSubhransu S. Prusty 
109279f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
109379f4e922SSubhransu S. Prusty 			num_routes++;
109479f4e922SSubhransu S. Prusty 		}
1095754695f9SJeeja KP 	}
109679f4e922SSubhransu S. Prusty 
109779f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
109879f4e922SSubhransu S. Prusty 							GFP_KERNEL);
109979f4e922SSubhransu S. Prusty 	if (!route)
110079f4e922SSubhransu S. Prusty 		return -ENOMEM;
110179f4e922SSubhransu S. Prusty 
110279f4e922SSubhransu S. Prusty 	i = 0;
110379f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
110479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1105754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
110679f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1107754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1108754695f9SJeeja KP 						hdmi->num_pin;
110979f4e922SSubhransu S. Prusty 
111079f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
111179f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
111279f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
111379f4e922SSubhransu S. Prusty 			i++;
1114754695f9SJeeja KP 		}
111579f4e922SSubhransu S. Prusty 	}
111679f4e922SSubhransu S. Prusty 
111779f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
111879f4e922SSubhransu S. Prusty 
111979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1120754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
112179f4e922SSubhransu S. Prusty 
112279f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
112379f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
112479f4e922SSubhransu S. Prusty 
112579f4e922SSubhransu S. Prusty 	return 0;
112679f4e922SSubhransu S. Prusty 
112718382eadSSubhransu S. Prusty }
112818382eadSSubhransu S. Prusty 
112915b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
113018382eadSSubhransu S. Prusty {
113115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1132754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
113315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1134148569fdSSubhransu S. Prusty 	int dai_id = 0;
113518382eadSSubhransu S. Prusty 
1136148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
113715b91447SSubhransu S. Prusty 		return -EINVAL;
113818382eadSSubhransu S. Prusty 
1139148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1140148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1141148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
114215b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
114318382eadSSubhransu S. Prusty 
1144148569fdSSubhransu S. Prusty 		dai_id++;
1145148569fdSSubhransu S. Prusty 
1146148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1147148569fdSSubhransu S. Prusty 			dev_warn(&edev->hdac.dev,
1148148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1149148569fdSSubhransu S. Prusty 			break;
1150148569fdSSubhransu S. Prusty 		}
1151148569fdSSubhransu S. Prusty 	}
115218382eadSSubhransu S. Prusty 
115315b91447SSubhransu S. Prusty 	return 0;
115415b91447SSubhransu S. Prusty }
115515b91447SSubhransu S. Prusty 
115615b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
115715b91447SSubhransu S. Prusty {
115815b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
115915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11604a3478deSJeeja KP 	char name[NAME_SIZE];
116115b91447SSubhransu S. Prusty 
116215b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
116315b91447SSubhransu S. Prusty 	if (!cvt)
116415b91447SSubhransu S. Prusty 		return -ENOMEM;
116515b91447SSubhransu S. Prusty 
116615b91447SSubhransu S. Prusty 	cvt->nid = nid;
11674a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11684a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
116915b91447SSubhransu S. Prusty 
117015b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
117115b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
117215b91447SSubhransu S. Prusty 
117315b91447SSubhransu S. Prusty 	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
117415b91447SSubhransu S. Prusty }
117515b91447SSubhransu S. Prusty 
1176f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1177754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1178b7756edeSSubhransu S. Prusty {
1179f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1180f6fa11a3SSandeep Tayal 
1181754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1182f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1183f6fa11a3SSandeep Tayal 
1184f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1185f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1186f6fa11a3SSandeep Tayal 		return -EINVAL;
1187b7756edeSSubhransu S. Prusty 	}
1188b7756edeSSubhransu S. Prusty 
1189754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1190f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1191f6fa11a3SSandeep Tayal 
1192f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1193f6fa11a3SSandeep Tayal 		dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1194f6fa11a3SSandeep Tayal 		return -EINVAL;
1195f6fa11a3SSandeep Tayal 	}
1196f6fa11a3SSandeep Tayal 
1197754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1198f6fa11a3SSandeep Tayal 
1199f6fa11a3SSandeep Tayal 	return 0;
1200f6fa11a3SSandeep Tayal }
1201f6fa11a3SSandeep Tayal 
1202754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1203754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1204b8a54545SSubhransu S. Prusty {
1205b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
12064a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
12074a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1208754695f9SJeeja KP 	int size = 0;
12092acd8309SJeeja KP 	int port_id = -1;
1210754695f9SJeeja KP 
1211754695f9SJeeja KP 	if (!hdmi)
1212754695f9SJeeja KP 		return;
12134a3478deSJeeja KP 
12142acd8309SJeeja KP 	/*
12152acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12162acd8309SJeeja KP 	 * to be -1.
12172acd8309SJeeja KP 	 */
12184a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1219754695f9SJeeja KP 	port->eld.monitor_present = false;
1220f6fa11a3SSandeep Tayal 
12212acd8309SJeeja KP 	if (pin->mst_capable)
12222acd8309SJeeja KP 		port_id = port->id;
12232acd8309SJeeja KP 
12242acd8309SJeeja KP 	size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
1225754695f9SJeeja KP 				&port->eld.monitor_present,
1226754695f9SJeeja KP 				port->eld.eld_buffer,
1227f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1228f6fa11a3SSandeep Tayal 
1229f6fa11a3SSandeep Tayal 	if (size > 0) {
1230f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1231754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1232f6fa11a3SSandeep Tayal 			size = -EINVAL;
1233f6fa11a3SSandeep Tayal 	}
1234f6fa11a3SSandeep Tayal 
1235f6fa11a3SSandeep Tayal 	if (size > 0) {
1236754695f9SJeeja KP 		port->eld.eld_valid = true;
1237754695f9SJeeja KP 		port->eld.eld_size = size;
1238f6fa11a3SSandeep Tayal 	} else {
1239754695f9SJeeja KP 		port->eld.eld_valid = false;
1240754695f9SJeeja KP 		port->eld.eld_size = 0;
1241f6fa11a3SSandeep Tayal 	}
1242b8a54545SSubhransu S. Prusty 
1243754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12444a3478deSJeeja KP 
1245754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1246b8a54545SSubhransu S. Prusty 
1247e0e5d3e5SJeeja KP 		dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1248754695f9SJeeja KP 						__func__, pin->nid, port->id);
12494a3478deSJeeja KP 
12504a3478deSJeeja KP 		/*
12514a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12524a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12534a3478deSJeeja KP 		 * control select.
12544a3478deSJeeja KP 		 */
1255e0e5d3e5SJeeja KP 		if (pcm)
1256e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12574a3478deSJeeja KP 
12584a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1259f6fa11a3SSandeep Tayal 		return;
1260b8a54545SSubhransu S. Prusty 	}
1261b8a54545SSubhransu S. Prusty 
1262754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1263e0e5d3e5SJeeja KP 		if (pcm)
1264e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12654a3478deSJeeja KP 
1266f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1267754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1268754695f9SJeeja KP 
1269754695f9SJeeja KP 	}
1270754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12714a3478deSJeeja KP }
12724a3478deSJeeja KP 
1273754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1274754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1275754695f9SJeeja KP {
1276754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1277754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1278754695f9SJeeja KP 	int i;
1279754695f9SJeeja KP 
1280754695f9SJeeja KP 	/*
1281754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1282754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1283754695f9SJeeja KP 	 * implemented.
1284754695f9SJeeja KP 	 */
1285754695f9SJeeja KP 
1286754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1287754695f9SJeeja KP 	if (!ports)
1288754695f9SJeeja KP 		return -ENOMEM;
1289754695f9SJeeja KP 
1290754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1291754695f9SJeeja KP 		ports[i].id = i;
1292754695f9SJeeja KP 		ports[i].pin = pin;
1293754695f9SJeeja KP 	}
1294754695f9SJeeja KP 	pin->ports = ports;
1295754695f9SJeeja KP 	pin->num_ports = max_ports;
1296754695f9SJeeja KP 	return 0;
1297b8a54545SSubhransu S. Prusty }
1298b8a54545SSubhransu S. Prusty 
129915b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
130015b91447SSubhransu S. Prusty {
130115b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
130215b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1303754695f9SJeeja KP 	int ret;
130415b91447SSubhransu S. Prusty 
130515b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
130615b91447SSubhransu S. Prusty 	if (!pin)
130715b91447SSubhransu S. Prusty 		return -ENOMEM;
130815b91447SSubhransu S. Prusty 
130915b91447SSubhransu S. Prusty 	pin->nid = nid;
13102acd8309SJeeja KP 	pin->mst_capable = false;
1311754695f9SJeeja KP 	pin->edev = edev;
1312754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1313754695f9SJeeja KP 	if (ret < 0)
1314754695f9SJeeja KP 		return ret;
131515b91447SSubhransu S. Prusty 
131615b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
131715b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1318754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1319b8a54545SSubhransu S. Prusty 
132015b91447SSubhransu S. Prusty 	return 0;
132118382eadSSubhransu S. Prusty }
132218382eadSSubhransu S. Prusty 
1323211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
1324211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1325211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1326211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1327211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1328211caab7SSubhransu S. Prusty 
1329211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1330211caab7SSubhransu S. Prusty {
1331211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1332211caab7SSubhransu S. Prusty 
1333211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1334211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1335211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1336211caab7SSubhransu S. Prusty 		return;
1337211caab7SSubhransu S. Prusty 
1338211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1339211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1340211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1341211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1342211caab7SSubhransu S. Prusty 		return;
1343211caab7SSubhransu S. Prusty }
1344211caab7SSubhransu S. Prusty 
1345211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1346211caab7SSubhransu S. Prusty {
1347211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1348211caab7SSubhransu S. Prusty 
1349211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1350211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1351211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1352211caab7SSubhransu S. Prusty 		return;
1353211caab7SSubhransu S. Prusty 
1354211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1355211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1356211caab7SSubhransu S. Prusty 	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1357211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1358211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1359211caab7SSubhransu S. Prusty 		return;
1360211caab7SSubhransu S. Prusty 
1361211caab7SSubhransu S. Prusty }
1362211caab7SSubhransu S. Prusty 
136317a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = {
136417a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
136517a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
136617a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1367c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
136817a42c45SSubhransu S. Prusty };
136917a42c45SSubhransu S. Prusty 
137017a42c45SSubhransu S. Prusty /*
137117a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
137217a42c45SSubhransu S. Prusty  * based on the number of converter queried.
137317a42c45SSubhransu S. Prusty  */
137417a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac,
137517a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
137617a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
137717a42c45SSubhransu S. Prusty {
137817a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
137917a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
138017a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
138117a42c45SSubhransu S. Prusty 	int i = 0;
138217a42c45SSubhransu S. Prusty 	u32 rates, bps;
138317a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
138417a42c45SSubhransu S. Prusty 	u64 formats;
138517a42c45SSubhransu S. Prusty 	int ret;
138617a42c45SSubhransu S. Prusty 
138717a42c45SSubhransu S. Prusty 	hdmi_dais = devm_kzalloc(&hdac->dev,
138817a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
138917a42c45SSubhransu S. Prusty 			GFP_KERNEL);
139017a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
139117a42c45SSubhransu S. Prusty 		return -ENOMEM;
139217a42c45SSubhransu S. Prusty 
139317a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
139417a42c45SSubhransu S. Prusty 		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
139517a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
139617a42c45SSubhransu S. Prusty 		if (ret)
139717a42c45SSubhransu S. Prusty 			return ret;
139817a42c45SSubhransu S. Prusty 
139917a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
140017a42c45SSubhransu S. Prusty 		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
140117a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
140217a42c45SSubhransu S. Prusty 
140317a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
140417a42c45SSubhransu S. Prusty 			return -ENOMEM;
140517a42c45SSubhransu S. Prusty 
140617a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
140717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
140817a42c45SSubhransu S. Prusty 				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
140917a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
141017a42c45SSubhransu S. Prusty 			return -ENOMEM;
141117a42c45SSubhransu S. Prusty 
141217a42c45SSubhransu S. Prusty 		/*
141317a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
141417a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
141517a42c45SSubhransu S. Prusty 		 */
141617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
141717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
141817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
141917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
142017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
142117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
142266d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
142317a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
142417a42c45SSubhransu S. Prusty 		i++;
142517a42c45SSubhransu S. Prusty 	}
142617a42c45SSubhransu S. Prusty 
142717a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
142817a42c45SSubhransu S. Prusty 
142917a42c45SSubhransu S. Prusty 	return 0;
143017a42c45SSubhransu S. Prusty }
143117a42c45SSubhransu S. Prusty 
143218382eadSSubhransu S. Prusty /*
143318382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
143418382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
143518382eadSSubhransu S. Prusty  */
143617a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
143717a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
143818382eadSSubhransu S. Prusty {
143918382eadSSubhransu S. Prusty 	hda_nid_t nid;
14403c83ac23SSudip Mukherjee 	int i, num_nodes;
144118382eadSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
144218382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
144315b91447SSubhransu S. Prusty 	int ret;
144418382eadSSubhransu S. Prusty 
1445211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(hdac);
1446211caab7SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(hdac);
1447211caab7SSubhransu S. Prusty 
14483c83ac23SSudip Mukherjee 	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1449541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
145018382eadSSubhransu S. Prusty 		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
145118382eadSSubhransu S. Prusty 		return -EINVAL;
145218382eadSSubhransu S. Prusty 	}
145318382eadSSubhransu S. Prusty 
14543c83ac23SSudip Mukherjee 	hdac->num_nodes = num_nodes;
145518382eadSSubhransu S. Prusty 	hdac->start_nid = nid;
145618382eadSSubhransu S. Prusty 
145718382eadSSubhransu S. Prusty 	for (i = 0; i < hdac->num_nodes; i++, nid++) {
145818382eadSSubhransu S. Prusty 		unsigned int caps;
145918382eadSSubhransu S. Prusty 		unsigned int type;
146018382eadSSubhransu S. Prusty 
146118382eadSSubhransu S. Prusty 		caps = get_wcaps(hdac, nid);
146218382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
146318382eadSSubhransu S. Prusty 
146418382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
146518382eadSSubhransu S. Prusty 			continue;
146618382eadSSubhransu S. Prusty 
146718382eadSSubhransu S. Prusty 		switch (type) {
146818382eadSSubhransu S. Prusty 
146918382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
147015b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
147115b91447SSubhransu S. Prusty 			if (ret < 0)
147215b91447SSubhransu S. Prusty 				return ret;
147318382eadSSubhransu S. Prusty 			break;
147418382eadSSubhransu S. Prusty 
147518382eadSSubhransu S. Prusty 		case AC_WID_PIN:
147615b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
147715b91447SSubhransu S. Prusty 			if (ret < 0)
147815b91447SSubhransu S. Prusty 				return ret;
147918382eadSSubhransu S. Prusty 			break;
148018382eadSSubhransu S. Prusty 		}
148118382eadSSubhransu S. Prusty 	}
148218382eadSSubhransu S. Prusty 
148318382eadSSubhransu S. Prusty 	hdac->end_nid = nid;
148418382eadSSubhransu S. Prusty 
148515b91447SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt)
148618382eadSSubhransu S. Prusty 		return -EIO;
148718382eadSSubhransu S. Prusty 
148817a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
148917a42c45SSubhransu S. Prusty 	if (ret) {
149017a42c45SSubhransu S. Prusty 		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
149117a42c45SSubhransu S. Prusty 							ret);
149217a42c45SSubhransu S. Prusty 		return ret;
149317a42c45SSubhransu S. Prusty 	}
149417a42c45SSubhransu S. Prusty 
149517a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
149617a42c45SSubhransu S. Prusty 
149715b91447SSubhransu S. Prusty 	return hdac_hdmi_init_dai_map(edev);
149818382eadSSubhransu S. Prusty }
149918382eadSSubhransu S. Prusty 
1500f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1501b8a54545SSubhransu S. Prusty {
1502b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1503b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1504754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1505754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
1506b8a54545SSubhransu S. Prusty 	struct snd_soc_codec *codec = edev->scodec;
15072acd8309SJeeja KP 	int i;
1508b8a54545SSubhransu S. Prusty 
1509b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1510b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1511b8a54545SSubhransu S. Prusty 
1512754695f9SJeeja KP 	dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1513754695f9SJeeja KP 							pin_nid, pipe);
1514b8a54545SSubhransu S. Prusty 
1515b8a54545SSubhransu S. Prusty 	/*
1516b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1517b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1518b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1519b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1520b8a54545SSubhransu S. Prusty 	 */
1521b8a54545SSubhransu S. Prusty 	if (snd_power_get_state(codec->component.card->snd_card) !=
1522b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1523b8a54545SSubhransu S. Prusty 		return;
1524b8a54545SSubhransu S. Prusty 
1525b8a54545SSubhransu S. Prusty 	if (atomic_read(&edev->hdac.in_pm))
1526b8a54545SSubhransu S. Prusty 		return;
1527b8a54545SSubhransu S. Prusty 
1528b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1529754695f9SJeeja KP 		if (pin->nid != pin_nid)
1530754695f9SJeeja KP 			continue;
1531754695f9SJeeja KP 
1532754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1533754695f9SJeeja KP 		if (pipe == -1) {
15342acd8309SJeeja KP 			pin->mst_capable = false;
1535754695f9SJeeja KP 			/* if not MST, default is port[0] */
1536754695f9SJeeja KP 			hport = &pin->ports[0];
15372acd8309SJeeja KP 			goto out;
15382acd8309SJeeja KP 		} else {
15392acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15402acd8309SJeeja KP 				pin->mst_capable = true;
15412acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15422acd8309SJeeja KP 					hport = &pin->ports[i];
15432acd8309SJeeja KP 					goto out;
15442acd8309SJeeja KP 				}
15452acd8309SJeeja KP 			}
1546b8a54545SSubhransu S. Prusty 		}
1547b8a54545SSubhransu S. Prusty 	}
1548b8a54545SSubhransu S. Prusty 
15492acd8309SJeeja KP out:
15502acd8309SJeeja KP 	if (pin && hport)
1551754695f9SJeeja KP 		hdac_hdmi_present_sense(pin, hport);
1552754695f9SJeeja KP }
1553754695f9SJeeja KP 
1554b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1555b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1556b8a54545SSubhransu S. Prusty };
1557b8a54545SSubhransu S. Prusty 
15582889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15592889099eSSubhransu S. Prusty 						int device)
15602889099eSSubhransu S. Prusty {
15612889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15622889099eSSubhransu S. Prusty 
15632889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15642889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15652889099eSSubhransu S. Prusty 			return rtd->pcm;
15662889099eSSubhransu S. Prusty 	}
15672889099eSSubhransu S. Prusty 
15682889099eSSubhransu S. Prusty 	return NULL;
15692889099eSSubhransu S. Prusty }
15702889099eSSubhransu S. Prusty 
15710324e51bSJeeja KP /* create jack pin kcontrols */
15720324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
15730324e51bSJeeja KP 				    struct hdac_ext_device *edev)
15740324e51bSJeeja KP {
15750324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
15760324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
15770324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
15780324e51bSJeeja KP 	char *name;
15790324e51bSJeeja KP 	int i = 0, j;
15800324e51bSJeeja KP 	struct snd_soc_codec *codec = edev->scodec;
15810324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
15820324e51bSJeeja KP 
15830324e51bSJeeja KP 	kc = devm_kcalloc(codec->dev, hdmi->num_ports,
15840324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
15850324e51bSJeeja KP 
15860324e51bSJeeja KP 	if (!kc)
15870324e51bSJeeja KP 		return -ENOMEM;
15880324e51bSJeeja KP 
15890324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
15900324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
15910324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
15920324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
15930324e51bSJeeja KP 			name = devm_kstrdup(codec->dev, xname, GFP_KERNEL);
15940324e51bSJeeja KP 			if (!name)
15950324e51bSJeeja KP 				return -ENOMEM;
15960324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
15970324e51bSJeeja KP 			kc[i].name = devm_kstrdup(codec->dev, kc_name,
15980324e51bSJeeja KP 							GFP_KERNEL);
15990324e51bSJeeja KP 			if (!kc[i].name)
16000324e51bSJeeja KP 				return -ENOMEM;
16010324e51bSJeeja KP 
16020324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16030324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16040324e51bSJeeja KP 			kc[i].access = 0;
16050324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16060324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16070324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16080324e51bSJeeja KP 			i++;
16090324e51bSJeeja KP 		}
16100324e51bSJeeja KP 	}
16110324e51bSJeeja KP 
16120324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16130324e51bSJeeja KP }
16140324e51bSJeeja KP 
16150324e51bSJeeja KP int hdac_hdmi_jack_port_init(struct snd_soc_codec *codec,
16160324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16170324e51bSJeeja KP {
16180324e51bSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
16190324e51bSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16200324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16210324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16220324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16230324e51bSJeeja KP 	char w_name[NAME_SIZE];
16240324e51bSJeeja KP 	int i = 0, j, ret;
16250324e51bSJeeja KP 
16260324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16270324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16280324e51bSJeeja KP 
16290324e51bSJeeja KP 	if (!widgets)
16300324e51bSJeeja KP 		return -ENOMEM;
16310324e51bSJeeja KP 
16320324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16330324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16340324e51bSJeeja KP 	if (!route)
16350324e51bSJeeja KP 		return -ENOMEM;
16360324e51bSJeeja KP 
16370324e51bSJeeja KP 	/* create Jack DAPM widget */
16380324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16390324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16400324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16410324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16420324e51bSJeeja KP 
16430324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16440324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16450324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16460324e51bSJeeja KP 			if (ret < 0)
16470324e51bSJeeja KP 				return ret;
16480324e51bSJeeja KP 
16490324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16500324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16510324e51bSJeeja KP 
16520324e51bSJeeja KP 			/* add to route from Jack widget to output */
16530324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16540324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16550324e51bSJeeja KP 
16560324e51bSJeeja KP 			i++;
16570324e51bSJeeja KP 		}
16580324e51bSJeeja KP 	}
16590324e51bSJeeja KP 
16600324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
16610324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
16620324e51bSJeeja KP 	if (ret < 0)
16630324e51bSJeeja KP 		return ret;
16640324e51bSJeeja KP 
16650324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
16660324e51bSJeeja KP 	if (ret < 0)
16670324e51bSJeeja KP 		return ret;
16680324e51bSJeeja KP 
16690324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
16700324e51bSJeeja KP 	if (ret < 0)
16710324e51bSJeeja KP 		return ret;
16720324e51bSJeeja KP 
16730324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
16740324e51bSJeeja KP 	ret = create_fill_jack_kcontrols(dapm->card, edev);
16750324e51bSJeeja KP 
16760324e51bSJeeja KP 	if (ret < 0)
16770324e51bSJeeja KP 		return ret;
16780324e51bSJeeja KP 
16790324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
16800324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16810324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
16820324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
16830324e51bSJeeja KP 						pin->ports[j].jack_pin);
16840324e51bSJeeja KP 	}
16850324e51bSJeeja KP 
16860324e51bSJeeja KP 	return 0;
16870324e51bSJeeja KP }
16880324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
16890324e51bSJeeja KP 
169062490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
169162490016SJeeja KP 				struct snd_soc_jack *jack)
16924a3478deSJeeja KP {
16934a3478deSJeeja KP 	struct snd_soc_codec *codec = dai->codec;
16944a3478deSJeeja KP 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
16954a3478deSJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
16964a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
16972889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
16982889099eSSubhransu S. Prusty 	int err;
16994a3478deSJeeja KP 
17004a3478deSJeeja KP 	/*
17014a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17024a3478deSJeeja KP 	 * add to the pcm list
17034a3478deSJeeja KP 	 */
17044a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
17054a3478deSJeeja KP 	if (!pcm)
17064a3478deSJeeja KP 		return -ENOMEM;
17074a3478deSJeeja KP 	pcm->pcm_id = device;
17084a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1709e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
171062490016SJeeja KP 	pcm->jack = jack;
1711ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1712e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17132889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17142889099eSSubhransu S. Prusty 	if (snd_pcm) {
17152889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17162889099eSSubhransu S. Prusty 		if (err < 0) {
17172889099eSSubhransu S. Prusty 			dev_err(&edev->hdac.dev,
17182889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17192889099eSSubhransu S. Prusty 				err, device);
17202889099eSSubhransu S. Prusty 			kfree(pcm);
17212889099eSSubhransu S. Prusty 			return err;
17222889099eSSubhransu S. Prusty 		}
17232889099eSSubhransu S. Prusty 	}
17242889099eSSubhransu S. Prusty 
17254a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17264a3478deSJeeja KP 
172762490016SJeeja KP 	return 0;
17284a3478deSJeeja KP }
17294a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17304a3478deSJeeja KP 
1731a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1732a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1733a9ce96bcSJeeja KP {
1734a9ce96bcSJeeja KP 	int i;
1735a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1736a9ce96bcSJeeja KP 
1737a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1738a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1739a9ce96bcSJeeja KP 
1740a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1741a9ce96bcSJeeja KP 				pin->mst_capable = false;
1742a9ce96bcSJeeja KP 			else
1743a9ce96bcSJeeja KP 				pin->mst_capable = true;
1744a9ce96bcSJeeja KP 		}
1745a9ce96bcSJeeja KP 
1746a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1747a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1748a9ce96bcSJeeja KP 				continue;
1749a9ce96bcSJeeja KP 
1750a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1751a9ce96bcSJeeja KP 		}
1752a9ce96bcSJeeja KP 	}
1753a9ce96bcSJeeja KP }
1754a9ce96bcSJeeja KP 
175518382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec)
175618382eadSSubhransu S. Prusty {
175718382eadSSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
175818382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
175918382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
176018382eadSSubhransu S. Prusty 		snd_soc_component_get_dapm(&codec->component);
1761b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1762a9ce96bcSJeeja KP 	int ret;
176318382eadSSubhransu S. Prusty 
176418382eadSSubhransu S. Prusty 	edev->scodec = codec;
176518382eadSSubhransu S. Prusty 
1766b2047e99SVinod Koul 	/*
1767b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1768b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1769b2047e99SVinod Koul 	 */
1770b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1771500e06b9SVinod Koul 	if (!hlink) {
1772500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1773500e06b9SVinod Koul 		return -EIO;
1774500e06b9SVinod Koul 	}
1775500e06b9SVinod Koul 
1776b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1777b2047e99SVinod Koul 
177879f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
177979f4e922SSubhransu S. Prusty 	if (ret < 0)
178079f4e922SSubhransu S. Prusty 		return ret;
178118382eadSSubhransu S. Prusty 
1782b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1783b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1784b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1785b8a54545SSubhransu S. Prusty 		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1786b8a54545SSubhransu S. Prusty 				ret);
1787b8a54545SSubhransu S. Prusty 		return ret;
1788b8a54545SSubhransu S. Prusty 	}
1789b8a54545SSubhransu S. Prusty 
1790a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
179118382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
179218382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
179318382eadSSubhransu S. Prusty 
1794e342ac08SSubhransu S. Prusty 	/*
1795e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1796e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1797e342ac08SSubhransu S. Prusty 	 */
1798e342ac08SSubhransu S. Prusty 	pm_runtime_enable(&edev->hdac.dev);
1799e342ac08SSubhransu S. Prusty 	pm_runtime_put(&edev->hdac.dev);
1800e342ac08SSubhransu S. Prusty 	pm_runtime_suspend(&edev->hdac.dev);
1801e342ac08SSubhransu S. Prusty 
1802e342ac08SSubhransu S. Prusty 	return 0;
1803e342ac08SSubhransu S. Prusty }
1804e342ac08SSubhransu S. Prusty 
1805e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec)
1806e342ac08SSubhransu S. Prusty {
1807e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1808e342ac08SSubhransu S. Prusty 
1809e342ac08SSubhransu S. Prusty 	pm_runtime_disable(&edev->hdac.dev);
181018382eadSSubhransu S. Prusty 	return 0;
181118382eadSSubhransu S. Prusty }
181218382eadSSubhransu S. Prusty 
1813571d5078SJeeja KP #ifdef CONFIG_PM
18141b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
18151b377ccdSSubhransu S. Prusty {
18161b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
18171b377ccdSSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
18181b377ccdSSubhransu S. Prusty 
18191b377ccdSSubhransu S. Prusty 	pm_runtime_get_sync(&edev->hdac.dev);
18201b377ccdSSubhransu S. Prusty 
18211b377ccdSSubhransu S. Prusty 	/*
18221b377ccdSSubhransu S. Prusty 	 * Power down afg.
18231b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
18241b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
18251b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
18261b377ccdSSubhransu S. Prusty 	 * to read the state.
18271b377ccdSSubhransu S. Prusty 	 */
18281b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18291b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
18301b377ccdSSubhransu S. Prusty 
18311b377ccdSSubhransu S. Prusty 	return 0;
18321b377ccdSSubhransu S. Prusty }
18331b377ccdSSubhransu S. Prusty 
18340fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1835571d5078SJeeja KP {
18360fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1837571d5078SJeeja KP 	struct hdac_hdmi_priv *hdmi = edev->private_data;
1838571d5078SJeeja KP 	struct hdac_device *hdac = &edev->hdac;
18391b377ccdSSubhransu S. Prusty 
18401b377ccdSSubhransu S. Prusty 	/* Power up afg */
18411b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
18421b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1843571d5078SJeeja KP 
1844571d5078SJeeja KP 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1845571d5078SJeeja KP 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
1846571d5078SJeeja KP 
1847571d5078SJeeja KP 	/*
1848571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1849571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1850a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1851a9ce96bcSJeeja KP 	 * already set pin caps.
1852571d5078SJeeja KP 	 */
1853a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1854571d5078SJeeja KP 
18551b377ccdSSubhransu S. Prusty 	pm_runtime_put_sync(&edev->hdac.dev);
1856571d5078SJeeja KP }
1857571d5078SJeeja KP #else
18581b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
18590fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1860571d5078SJeeja KP #endif
1861571d5078SJeeja KP 
186218382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = {
186318382eadSSubhransu S. Prusty 	.probe		= hdmi_codec_probe,
1864e342ac08SSubhransu S. Prusty 	.remove		= hdmi_codec_remove,
186518382eadSSubhransu S. Prusty 	.idle_bias_off	= true,
186618382eadSSubhransu S. Prusty };
186718382eadSSubhransu S. Prusty 
18682889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
18692889099eSSubhransu S. Prusty 					unsigned char *chmap)
18702889099eSSubhransu S. Prusty {
18712889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
18722889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
18732889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
18742889099eSSubhransu S. Prusty 
1875ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
18762889099eSSubhransu S. Prusty }
18772889099eSSubhransu S. Prusty 
18782889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
18792889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
18802889099eSSubhransu S. Prusty {
18812889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
18822889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
18832889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1884e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1885e0e5d3e5SJeeja KP 
1886e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1887e0e5d3e5SJeeja KP 		return;
18882889099eSSubhransu S. Prusty 
1889ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1890ab1eea19SJeeja KP 	pcm->chmap_set = true;
1891ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1892e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
18932889099eSSubhransu S. Prusty 		if (prepared)
1894754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1895ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
18962889099eSSubhransu S. Prusty }
18972889099eSSubhransu S. Prusty 
18982889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
18992889099eSSubhransu S. Prusty {
19002889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19012889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19022889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19032889099eSSubhransu S. Prusty 
1904e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1905e0e5d3e5SJeeja KP 		return false;
1906e0e5d3e5SJeeja KP 
1907e0e5d3e5SJeeja KP 	return true;
19082889099eSSubhransu S. Prusty }
19092889099eSSubhransu S. Prusty 
19102889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
19112889099eSSubhransu S. Prusty {
19122889099eSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_ehdac_device(hdac);
19132889099eSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
19142889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1915e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1916e0e5d3e5SJeeja KP 
1917e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1918e0e5d3e5SJeeja KP 		return 0;
1919e0e5d3e5SJeeja KP 
1920e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1921e0e5d3e5SJeeja KP 
1922e0e5d3e5SJeeja KP 	if (!port)
1923e0e5d3e5SJeeja KP 		return 0;
19242889099eSSubhransu S. Prusty 
1925754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19262889099eSSubhransu S. Prusty 		return 0;
19272889099eSSubhransu S. Prusty 
1928754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19292889099eSSubhransu S. Prusty }
19302889099eSSubhransu S. Prusty 
193118382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
193218382eadSSubhransu S. Prusty {
193318382eadSSubhransu S. Prusty 	struct hdac_device *codec = &edev->hdac;
193418382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
193517a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1936b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
193717a42c45SSubhransu S. Prusty 	int num_dais = 0;
193818382eadSSubhransu S. Prusty 	int ret = 0;
193918382eadSSubhransu S. Prusty 
1940b2047e99SVinod Koul 	/* hold the ref while we probe */
1941b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1942500e06b9SVinod Koul 	if (!hlink) {
1943500e06b9SVinod Koul 		dev_err(&edev->hdac.dev, "hdac link not found\n");
1944500e06b9SVinod Koul 		return -EIO;
1945500e06b9SVinod Koul 	}
1946500e06b9SVinod Koul 
1947b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1948b2047e99SVinod Koul 
194918382eadSSubhransu S. Prusty 	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
195018382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
195118382eadSSubhransu S. Prusty 		return -ENOMEM;
195218382eadSSubhransu S. Prusty 
195318382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
1954bcced704SSubhransu S. Prusty 	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
19552889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
19562889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
19572889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
19582889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
195918382eadSSubhransu S. Prusty 
196018382eadSSubhransu S. Prusty 	dev_set_drvdata(&codec->dev, edev);
196118382eadSSubhransu S. Prusty 
196215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
196315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
19644a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
19654a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
196615b91447SSubhransu S. Prusty 
1967aeaccef0SRamesh Babu 	/*
1968aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
1969aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
1970aeaccef0SRamesh Babu 	 */
1971aeaccef0SRamesh Babu 	ret = snd_hdac_display_power(edev->hdac.bus, true);
1972aeaccef0SRamesh Babu 	if (ret < 0) {
1973aeaccef0SRamesh Babu 		dev_err(&edev->hdac.dev,
1974aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
1975aeaccef0SRamesh Babu 			ret);
1976aeaccef0SRamesh Babu 		return ret;
1977aeaccef0SRamesh Babu 	}
1978aeaccef0SRamesh Babu 
197917a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
198017a42c45SSubhransu S. Prusty 	if (ret < 0) {
198117a42c45SSubhransu S. Prusty 		dev_err(&codec->dev,
198217a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
198318382eadSSubhransu S. Prusty 		return ret;
198417a42c45SSubhransu S. Prusty 	}
198518382eadSSubhransu S. Prusty 
198618382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
1987b2047e99SVinod Koul 	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
198817a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
1989b2047e99SVinod Koul 
1990b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1991b2047e99SVinod Koul 
1992b2047e99SVinod Koul 	return ret;
199318382eadSSubhransu S. Prusty }
199418382eadSSubhransu S. Prusty 
199518382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
199618382eadSSubhransu S. Prusty {
199715b91447SSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi = edev->private_data;
199815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
199915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
20004a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
2001e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
2002754695f9SJeeja KP 	int i;
200315b91447SSubhransu S. Prusty 
200418382eadSSubhransu S. Prusty 	snd_soc_unregister_codec(&edev->hdac.dev);
200518382eadSSubhransu S. Prusty 
20064a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
20074a3478deSJeeja KP 		pcm->cvt = NULL;
2008e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
2009e0e5d3e5SJeeja KP 			continue;
2010e0e5d3e5SJeeja KP 
2011e0e5d3e5SJeeja KP 		list_for_each_entry(port, &pcm->port_list, head)
2012e0e5d3e5SJeeja KP 			port = NULL;
2013e0e5d3e5SJeeja KP 
20144a3478deSJeeja KP 		list_del(&pcm->head);
20154a3478deSJeeja KP 		kfree(pcm);
20164a3478deSJeeja KP 	}
20174a3478deSJeeja KP 
201815b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
201915b91447SSubhransu S. Prusty 		list_del(&cvt->head);
20204a3478deSJeeja KP 		kfree(cvt->name);
202115b91447SSubhransu S. Prusty 		kfree(cvt);
202215b91447SSubhransu S. Prusty 	}
202315b91447SSubhransu S. Prusty 
202415b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2025754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
2026754695f9SJeeja KP 			pin->ports[i].pin = NULL;
2027754695f9SJeeja KP 		kfree(pin->ports);
202815b91447SSubhransu S. Prusty 		list_del(&pin->head);
202915b91447SSubhransu S. Prusty 		kfree(pin);
203015b91447SSubhransu S. Prusty 	}
203115b91447SSubhransu S. Prusty 
203218382eadSSubhransu S. Prusty 	return 0;
203318382eadSSubhransu S. Prusty }
203418382eadSSubhransu S. Prusty 
2035e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2036e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2037e342ac08SSubhransu S. Prusty {
2038e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2039e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
204007f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2041b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2042b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
204307f083abSSubhransu S. Prusty 	int err;
2044e342ac08SSubhransu S. Prusty 
2045e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2046e342ac08SSubhransu S. Prusty 
204707f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
204807f083abSSubhransu S. Prusty 	if (!bus)
204907f083abSSubhransu S. Prusty 		return 0;
205007f083abSSubhransu S. Prusty 
20511b377ccdSSubhransu S. Prusty 	/*
20521b377ccdSSubhransu S. Prusty 	 * Power down afg.
20531b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
20541b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
20551b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
20561b377ccdSSubhransu S. Prusty 	 * to read the state.
20571b377ccdSSubhransu S. Prusty 	 */
20581b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
20591b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
206007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
206107f083abSSubhransu S. Prusty 	if (err < 0) {
206207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
206307f083abSSubhransu S. Prusty 		return err;
206407f083abSSubhransu S. Prusty 	}
206507f083abSSubhransu S. Prusty 
2066b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2067500e06b9SVinod Koul 	if (!hlink) {
2068500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2069500e06b9SVinod Koul 		return -EIO;
2070500e06b9SVinod Koul 	}
2071500e06b9SVinod Koul 
2072b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
2073b2047e99SVinod Koul 
2074e342ac08SSubhransu S. Prusty 	return 0;
2075e342ac08SSubhransu S. Prusty }
2076e342ac08SSubhransu S. Prusty 
2077e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2078e342ac08SSubhransu S. Prusty {
2079e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2080e342ac08SSubhransu S. Prusty 	struct hdac_device *hdac = &edev->hdac;
208107f083abSSubhransu S. Prusty 	struct hdac_bus *bus = hdac->bus;
2082b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2083b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
208407f083abSSubhransu S. Prusty 	int err;
2085e342ac08SSubhransu S. Prusty 
2086e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2087e342ac08SSubhransu S. Prusty 
208807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
208907f083abSSubhransu S. Prusty 	if (!bus)
209007f083abSSubhransu S. Prusty 		return 0;
209107f083abSSubhransu S. Prusty 
2092b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2093500e06b9SVinod Koul 	if (!hlink) {
2094500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2095500e06b9SVinod Koul 		return -EIO;
2096500e06b9SVinod Koul 	}
2097500e06b9SVinod Koul 
2098b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
2099b2047e99SVinod Koul 
210007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
210107f083abSSubhransu S. Prusty 	if (err < 0) {
210207f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
210307f083abSSubhransu S. Prusty 		return err;
210407f083abSSubhransu S. Prusty 	}
210507f083abSSubhransu S. Prusty 
2106ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
2107ab85f5b3SSubhransu S. Prusty 	hdac_hdmi_skl_enable_dp12(&edev->hdac);
2108ab85f5b3SSubhransu S. Prusty 
2109e342ac08SSubhransu S. Prusty 	/* Power up afg */
21101b377ccdSSubhransu S. Prusty 	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
21111b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2112e342ac08SSubhransu S. Prusty 
2113e342ac08SSubhransu S. Prusty 	return 0;
2114e342ac08SSubhransu S. Prusty }
2115e342ac08SSubhransu S. Prusty #else
2116e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2117e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2118e342ac08SSubhransu S. Prusty #endif
2119e342ac08SSubhransu S. Prusty 
2120e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2121e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
21221b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
21230fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
2124e342ac08SSubhransu S. Prusty };
2125e342ac08SSubhransu S. Prusty 
212618382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
212718382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2128e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2129cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2130e3efb2adSVinod Koul 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI", 0),
213118382eadSSubhransu S. Prusty 	{}
213218382eadSSubhransu S. Prusty };
213318382eadSSubhransu S. Prusty 
213418382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
213518382eadSSubhransu S. Prusty 
213618382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
213718382eadSSubhransu S. Prusty 	. hdac = {
213818382eadSSubhransu S. Prusty 		.driver = {
213918382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2140e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
214118382eadSSubhransu S. Prusty 		},
214218382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
214318382eadSSubhransu S. Prusty 	},
214418382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
214518382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
214618382eadSSubhransu S. Prusty };
214718382eadSSubhransu S. Prusty 
214818382eadSSubhransu S. Prusty static int __init hdmi_init(void)
214918382eadSSubhransu S. Prusty {
215018382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
215118382eadSSubhransu S. Prusty }
215218382eadSSubhransu S. Prusty 
215318382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
215418382eadSSubhransu S. Prusty {
215518382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
215618382eadSSubhransu S. Prusty }
215718382eadSSubhransu S. Prusty 
215818382eadSSubhransu S. Prusty module_init(hdmi_init);
215918382eadSSubhransu S. Prusty module_exit(hdmi_exit);
216018382eadSSubhransu S. Prusty 
216118382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
216218382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
216318382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
216418382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2165