xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision e1df9317)
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;
883787a398SRakesh Ughreja 	struct hdac_device *hdev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
980324e51bSJeeja KP 	const char *jack_pin;
990324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
1000324e51bSJeeja KP 	const char *output_pin;
10118382eadSSubhransu S. Prusty };
10218382eadSSubhransu S. Prusty 
1034a3478deSJeeja KP struct hdac_hdmi_pcm {
1044a3478deSJeeja KP 	struct list_head head;
1054a3478deSJeeja KP 	int pcm_id;
106e0e5d3e5SJeeja KP 	struct list_head port_list;
1074a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10862490016SJeeja KP 	struct snd_soc_jack *jack;
109c9bfb5d7SJeeja KP 	int stream_tag;
110c9bfb5d7SJeeja KP 	int channels;
111c9bfb5d7SJeeja KP 	int format;
112ab1eea19SJeeja KP 	bool chmap_set;
113ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
114ab1eea19SJeeja KP 	struct mutex lock;
115e0e5d3e5SJeeja KP 	int jack_event;
1164a3478deSJeeja KP };
1174a3478deSJeeja KP 
118754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11918382eadSSubhransu S. Prusty 	int dai_id;
120754695f9SJeeja KP 	struct hdac_hdmi_port *port;
12115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12218382eadSSubhransu S. Prusty };
12318382eadSSubhransu S. Prusty 
1245622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1255622bc95SPradeep Tewani 	unsigned int vendor_nid;
1265622bc95SPradeep Tewani };
1275622bc95SPradeep Tewani 
12818382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1293787a398SRakesh Ughreja 	struct hdac_device *hdev;
1303787a398SRakesh Ughreja 	struct snd_soc_component *component;
1313787a398SRakesh Ughreja 	struct snd_card *card;
132754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
13315b91447SSubhransu S. Prusty 	struct list_head pin_list;
13415b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1354a3478deSJeeja KP 	struct list_head pcm_list;
13615b91447SSubhransu S. Prusty 	int num_pin;
13715b91447SSubhransu S. Prusty 	int num_cvt;
138754695f9SJeeja KP 	int num_ports;
1394a3478deSJeeja KP 	struct mutex pin_mutex;
140bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1415622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1421e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
14318382eadSSubhransu S. Prusty };
14418382eadSSubhransu S. Prusty 
1453787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
146b09b1c3bSUghreja, Rakesh A 
147c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
148c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
149c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
150c9bfb5d7SJeeja KP {
151c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1521de777feSJeeja KP 
153c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
154c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
155c9bfb5d7SJeeja KP 			break;
156c9bfb5d7SJeeja KP 	}
157c9bfb5d7SJeeja KP 
158c9bfb5d7SJeeja KP 	return pcm;
159c9bfb5d7SJeeja KP }
1601de777feSJeeja KP 
161e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
162e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
163e0e5d3e5SJeeja KP {
1643787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
165e0e5d3e5SJeeja KP 
1660324e51bSJeeja KP 	if (is_connect)
1670324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1680324e51bSJeeja KP 	else
1690324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1700324e51bSJeeja KP 
171e0e5d3e5SJeeja KP 	if (is_connect) {
172e0e5d3e5SJeeja KP 		/*
173e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
174e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
175e0e5d3e5SJeeja KP 		 * ports.
176e0e5d3e5SJeeja KP 		 */
177e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1783787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
179e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
180e0e5d3e5SJeeja KP 					pcm->pcm_id);
18162490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
18262490016SJeeja KP 						SND_JACK_AVOUT);
183e0e5d3e5SJeeja KP 		}
184e0e5d3e5SJeeja KP 		pcm->jack_event++;
185e0e5d3e5SJeeja KP 	} else {
186e0e5d3e5SJeeja KP 		/*
187e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
188e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
189e0e5d3e5SJeeja KP 		 * to multiple ports.
190e0e5d3e5SJeeja KP 		 */
191e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
19262490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
193e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
194e0e5d3e5SJeeja KP 			pcm->jack_event--;
195e0e5d3e5SJeeja KP 	}
1960324e51bSJeeja KP 
1970324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
198e0e5d3e5SJeeja KP }
199e0e5d3e5SJeeja KP 
200fc181b04SJeeja KP /* MST supported verbs */
201fc181b04SJeeja KP /*
202fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
203fc181b04SJeeja KP  */
2043787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
205fc181b04SJeeja KP {
206fc181b04SJeeja KP 	unsigned int caps;
207fc181b04SJeeja KP 	unsigned int type, param;
208fc181b04SJeeja KP 
2093787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
210fc181b04SJeeja KP 	type = get_wcaps_type(caps);
211fc181b04SJeeja KP 
212fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
213fc181b04SJeeja KP 		return 0;
214fc181b04SJeeja KP 
2153787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
216fc181b04SJeeja KP 	if (param == -1)
217fc181b04SJeeja KP 		return param;
218fc181b04SJeeja KP 
219fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
220fc181b04SJeeja KP }
221fc181b04SJeeja KP 
222fc181b04SJeeja KP /*
223fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
224fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
225fc181b04SJeeja KP  * is selected or MST is not supported.
226fc181b04SJeeja KP  */
2273787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
228fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
229fc181b04SJeeja KP {
2303787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
231fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
232fc181b04SJeeja KP }
233fc181b04SJeeja KP 
234fc181b04SJeeja KP /*
235fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
236fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
237fc181b04SJeeja KP  */
2383787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
239fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
240fc181b04SJeeja KP {
241fc181b04SJeeja KP 	int num_ports;
242fc181b04SJeeja KP 
243fc181b04SJeeja KP 	if (!port->pin->mst_capable)
244fc181b04SJeeja KP 		return 0;
245fc181b04SJeeja KP 
246fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2473787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
248fc181b04SJeeja KP 	if (num_ports < 0)
249fc181b04SJeeja KP 		return -EIO;
250fc181b04SJeeja KP 	/*
251fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
252fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
253fc181b04SJeeja KP 	 */
254fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
255fc181b04SJeeja KP 		return 0;
256fc181b04SJeeja KP 
2573787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
258fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
259fc181b04SJeeja KP 
2603787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
261fc181b04SJeeja KP 		return -EIO;
262fc181b04SJeeja KP 
2633787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
264fc181b04SJeeja KP 
265fc181b04SJeeja KP 	return 0;
266fc181b04SJeeja KP }
267fc181b04SJeeja KP 
2682889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2692889099eSSubhransu S. Prusty 						int pcm_idx)
2702889099eSSubhransu S. Prusty {
2712889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2722889099eSSubhransu S. Prusty 
2732889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2742889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2752889099eSSubhransu S. Prusty 			return pcm;
2762889099eSSubhransu S. Prusty 	}
2772889099eSSubhransu S. Prusty 
2782889099eSSubhransu S. Prusty 	return NULL;
2792889099eSSubhransu S. Prusty }
2802889099eSSubhransu S. Prusty 
2812428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2822428bca3SSubhransu S. Prusty {
2832428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2842428bca3SSubhransu S. Prusty }
2852428bca3SSubhransu S. Prusty 
2862428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2872428bca3SSubhransu S. Prusty {
2882428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2892428bca3SSubhransu S. Prusty }
2902428bca3SSubhransu S. Prusty 
2912428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2922428bca3SSubhransu S. Prusty 						void *eld)
2932428bca3SSubhransu S. Prusty {
2942428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
2952428bca3SSubhransu S. Prusty 	int i;
2962428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
2972428bca3SSubhransu S. Prusty 
2982428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
2992428bca3SSubhransu S. Prusty 	if (!sad)
3002428bca3SSubhransu S. Prusty 		goto format_constraint;
3012428bca3SSubhransu S. Prusty 
3022428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3032428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3042428bca3SSubhransu S. Prusty 
3052428bca3SSubhransu S. Prusty 			/*
3062428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3072428bca3SSubhransu S. Prusty 			 * container so we set S32
3082428bca3SSubhransu S. Prusty 			 */
3092428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3102428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3112428bca3SSubhransu S. Prusty 		}
3122428bca3SSubhransu S. Prusty 	}
3132428bca3SSubhransu S. Prusty 
3142428bca3SSubhransu S. Prusty format_constraint:
3152428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3162428bca3SSubhransu S. Prusty 				formats);
3172428bca3SSubhransu S. Prusty 
3182428bca3SSubhransu S. Prusty }
3192428bca3SSubhransu S. Prusty 
320a657f1d0SSubhransu S. Prusty static void
3213787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
322a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
323a657f1d0SSubhransu S. Prusty {
324a657f1d0SSubhransu S. Prusty 	int val;
325a657f1d0SSubhransu S. Prusty 
326a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3273787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, 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 
3423787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
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;
3493787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
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 
3583787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, 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);
3623787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, 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:
3953787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
396478f544eSSubhransu S. Prusty 		return -EIO;
397478f544eSSubhransu S. Prusty 	}
398478f544eSSubhransu S. Prusty 
399a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4003787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4013787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
402a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
403a657f1d0SSubhransu S. Prusty 
404a657f1d0SSubhransu S. Prusty 
405a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4063787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
407478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
408391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4093787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
410391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
411478f544eSSubhransu S. Prusty 	} else {
412478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4133787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
414478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
415478f544eSSubhransu S. Prusty 	}
416a657f1d0SSubhransu S. Prusty 
417a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4183787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4193787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
420a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
421a657f1d0SSubhransu S. Prusty 
422a657f1d0SSubhransu S. Prusty 	return 0;
423a657f1d0SSubhransu S. Prusty }
424a657f1d0SSubhransu S. Prusty 
425c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
426c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
427c9bfb5d7SJeeja KP 		int slots, int slot_width)
428b0362adbSSubhransu S. Prusty {
4293787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4303787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
431754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
432c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
433c9bfb5d7SJeeja KP 
4343787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
435b0362adbSSubhransu S. Prusty 
436b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
437b0362adbSSubhransu S. Prusty 
438c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
439b0362adbSSubhransu S. Prusty 
440c9bfb5d7SJeeja KP 	if (pcm)
441c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
442bcced704SSubhransu S. Prusty 
443c9bfb5d7SJeeja KP 	return 0;
444b0362adbSSubhransu S. Prusty }
445b0362adbSSubhransu S. Prusty 
446b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
447b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
448b0362adbSSubhransu S. Prusty {
4493787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4503787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
451754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
452754695f9SJeeja KP 	struct hdac_hdmi_port *port;
453c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
454c9bfb5d7SJeeja KP 	int format;
455b0362adbSSubhransu S. Prusty 
45654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
457754695f9SJeeja KP 	port = dai_map->port;
45854dfa1eaSSubhransu S. Prusty 
459754695f9SJeeja KP 	if (!port)
46054dfa1eaSSubhransu S. Prusty 		return -ENODEV;
46154dfa1eaSSubhransu S. Prusty 
462754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
4633787a398SRakesh Ughreja 		dev_err(&hdev->dev,
464754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
465754695f9SJeeja KP 					port->pin->nid, port->id);
466b0362adbSSubhransu S. Prusty 		return -ENODEV;
467b0362adbSSubhransu S. Prusty 	}
468b0362adbSSubhransu S. Prusty 
469c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
470b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47166d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
472b0362adbSSubhransu S. Prusty 
473c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
474c9bfb5d7SJeeja KP 	if (!pcm)
475148569fdSSubhransu S. Prusty 		return -EIO;
476148569fdSSubhransu S. Prusty 
477c9bfb5d7SJeeja KP 	pcm->format = format;
478c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
479148569fdSSubhransu S. Prusty 
480148569fdSSubhransu S. Prusty 	return 0;
481148569fdSSubhransu S. Prusty }
482148569fdSSubhransu S. Prusty 
4833787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
484754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
485754695f9SJeeja KP 					struct hdac_hdmi_port *port)
486148569fdSSubhransu S. Prusty {
4873787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4883787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
489148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4903787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
491148569fdSSubhransu S. Prusty 		return -EINVAL;
492148569fdSSubhransu S. Prusty 	}
493148569fdSSubhransu S. Prusty 
4943787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
4951b46ebd1SJeeja KP 		return -EIO;
4961b46ebd1SJeeja KP 
4973787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
498754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
499754695f9SJeeja KP 	if (port->num_mux_nids == 0)
5003787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
501754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
502754695f9SJeeja KP 						pin->nid, port->id);
503148569fdSSubhransu S. Prusty 
5043787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
505754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
506148569fdSSubhransu S. Prusty 
507754695f9SJeeja KP 	return port->num_mux_nids;
508148569fdSSubhransu S. Prusty }
509148569fdSSubhransu S. Prusty 
510148569fdSSubhransu S. Prusty /*
511754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
512148569fdSSubhransu S. Prusty  *
513754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
514148569fdSSubhransu S. Prusty  *
515754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
516754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
517148569fdSSubhransu S. Prusty  * connected.
518148569fdSSubhransu S. Prusty  */
519754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5203787a398SRakesh Ughreja 			struct hdac_device *hdev,
521148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
522148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
523148569fdSSubhransu S. Prusty {
524148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
525754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
526148569fdSSubhransu S. Prusty 	int ret, i;
527148569fdSSubhransu S. Prusty 
528148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
529148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
530e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
531e0e5d3e5SJeeja KP 				continue;
532148569fdSSubhransu S. Prusty 
533e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
534e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5353787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
536e0e5d3e5SJeeja KP 							port->pin, port);
537e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
538148569fdSSubhransu S. Prusty 				if (ret < 0)
539e0e5d3e5SJeeja KP 					continue;
540148569fdSSubhransu S. Prusty 
541754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
542e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
543e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
544e0e5d3e5SJeeja KP 						port->eld.eld_valid)
545754695f9SJeeja KP 						return port;
546148569fdSSubhransu S. Prusty 				}
547148569fdSSubhransu S. Prusty 			}
548e0e5d3e5SJeeja KP 		}
549e0e5d3e5SJeeja KP 	}
550148569fdSSubhransu S. Prusty 
551148569fdSSubhransu S. Prusty 	return NULL;
552148569fdSSubhransu S. Prusty }
553148569fdSSubhransu S. Prusty 
55454dfa1eaSSubhransu S. Prusty /*
55554dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
55654dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
55754dfa1eaSSubhransu S. Prusty  * doesn't fail.
55854dfa1eaSSubhransu S. Prusty  */
559b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
560b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
561b0362adbSSubhransu S. Prusty {
5623787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5633787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
564754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
565148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
566754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5672428bca3SSubhransu S. Prusty 	int ret;
568b0362adbSSubhransu S. Prusty 
569b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
570b0362adbSSubhransu S. Prusty 
571148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5723787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
57354dfa1eaSSubhransu S. Prusty 
57454dfa1eaSSubhransu S. Prusty 	/*
57554dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
57654dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
57754dfa1eaSSubhransu S. Prusty 	 */
578754695f9SJeeja KP 	if (!port)
57954dfa1eaSSubhransu S. Prusty 		return 0;
580754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
581754695f9SJeeja KP 			(!port->eld.eld_valid)) {
582b0362adbSSubhransu S. Prusty 
5833787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
584754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
585754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
586754695f9SJeeja KP 			port->pin->nid, port->id);
587b8a54545SSubhransu S. Prusty 
58854dfa1eaSSubhransu S. Prusty 		return 0;
589b0362adbSSubhransu S. Prusty 	}
590b0362adbSSubhransu S. Prusty 
591754695f9SJeeja KP 	dai_map->port = port;
592b0362adbSSubhransu S. Prusty 
5932428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
594754695f9SJeeja KP 				port->eld.eld_buffer);
5952428bca3SSubhransu S. Prusty 	if (ret < 0)
5962428bca3SSubhransu S. Prusty 		return ret;
597b0362adbSSubhransu S. Prusty 
5982428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
599754695f9SJeeja KP 				port->eld.eld_buffer);
600b0362adbSSubhransu S. Prusty }
601b0362adbSSubhransu S. Prusty 
602b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
603b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
604b0362adbSSubhransu S. Prusty {
6053787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
606754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
607ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
608b0362adbSSubhransu S. Prusty 
609b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
610b0362adbSSubhransu S. Prusty 
611ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
612bcced704SSubhransu S. Prusty 
613ab1eea19SJeeja KP 	if (pcm) {
614ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
615ab1eea19SJeeja KP 		pcm->chmap_set = false;
616ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
617ab1eea19SJeeja KP 		pcm->channels = 0;
618ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
619b0362adbSSubhransu S. Prusty 	}
620ab1eea19SJeeja KP 
621754695f9SJeeja KP 	if (dai_map->port)
622754695f9SJeeja KP 		dai_map->port = NULL;
62354dfa1eaSSubhransu S. Prusty }
624b0362adbSSubhransu S. Prusty 
62518382eadSSubhransu S. Prusty static int
626f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
62718382eadSSubhransu S. Prusty {
628bcced704SSubhransu S. Prusty 	unsigned int chans;
629f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
63018382eadSSubhransu S. Prusty 	int err;
63118382eadSSubhransu S. Prusty 
632f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
633bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
634bcced704SSubhransu S. Prusty 
635bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
636bcced704SSubhransu S. Prusty 
637bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
638bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
639bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
64018382eadSSubhransu S. Prusty 
641f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
64218382eadSSubhransu S. Prusty 			&cvt->params.rates,
64318382eadSSubhransu S. Prusty 			&cvt->params.formats,
64418382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
64518382eadSSubhransu S. Prusty 	if (err < 0)
646f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
64718382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
64818382eadSSubhransu S. Prusty 			cvt->nid, err);
64918382eadSSubhransu S. Prusty 
65018382eadSSubhransu S. Prusty 	return err;
65118382eadSSubhransu S. Prusty }
65218382eadSSubhransu S. Prusty 
65379f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
654c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
655c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
656c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
657c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
658c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
65918382eadSSubhransu S. Prusty {
66018382eadSSubhransu S. Prusty 	w->id = id;
66179f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
66279f4e922SSubhransu S. Prusty 	if (!w->name)
66379f4e922SSubhransu S. Prusty 		return -ENOMEM;
66479f4e922SSubhransu S. Prusty 
66518382eadSSubhransu S. Prusty 	w->sname = stream;
66618382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
66718382eadSSubhransu S. Prusty 	w->shift = 0;
66879f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
66979f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
67079f4e922SSubhransu S. Prusty 	w->priv = priv;
671c9bfb5d7SJeeja KP 	w->event = event;
672c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
67379f4e922SSubhransu S. Prusty 
67479f4e922SSubhransu S. Prusty 	return 0;
67518382eadSSubhransu S. Prusty }
67618382eadSSubhransu S. Prusty 
67718382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
67879f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
67979f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
68079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
68118382eadSSubhransu S. Prusty {
68218382eadSSubhransu S. Prusty 	route->sink = sink;
68318382eadSSubhransu S. Prusty 	route->source = src;
68418382eadSSubhransu S. Prusty 	route->control = control;
68579f4e922SSubhransu S. Prusty 	route->connected = handler;
68618382eadSSubhransu S. Prusty }
68718382eadSSubhransu S. Prusty 
6883787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
689754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6904a3478deSJeeja KP {
6913787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
6924a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
693e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6944a3478deSJeeja KP 
6954a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
696e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
697754695f9SJeeja KP 			continue;
698754695f9SJeeja KP 
699e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
700e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7014a3478deSJeeja KP 				return pcm;
7024a3478deSJeeja KP 		}
703e0e5d3e5SJeeja KP 	}
7044a3478deSJeeja KP 
7054a3478deSJeeja KP 	return NULL;
7064a3478deSJeeja KP }
7074a3478deSJeeja KP 
7083787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
709c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
710c9bfb5d7SJeeja KP {
711753597fbSAbhijeet Kumar 	int count;
712753597fbSAbhijeet Kumar 	unsigned int state;
713753597fbSAbhijeet Kumar 
7143787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7153787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
716753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7173787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
718753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
719753597fbSAbhijeet Kumar 						pwr_state);
7203787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
721753597fbSAbhijeet Kumar 						nid, pwr_state);
722753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
723753597fbSAbhijeet Kumar 					break;
724753597fbSAbhijeet Kumar 			}
725753597fbSAbhijeet Kumar 		}
726c9bfb5d7SJeeja KP 	}
727c9bfb5d7SJeeja KP }
728c9bfb5d7SJeeja KP 
7293787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
730c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
731c9bfb5d7SJeeja KP {
7323787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7333787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
734c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
735c9bfb5d7SJeeja KP }
736c9bfb5d7SJeeja KP 
737c9bfb5d7SJeeja KP 
738c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
739c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
740c9bfb5d7SJeeja KP {
741754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7423787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
743c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
744c9bfb5d7SJeeja KP 
7453787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
746c9bfb5d7SJeeja KP 			__func__, w->name, event);
747c9bfb5d7SJeeja KP 
7483787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
749c9bfb5d7SJeeja KP 	if (!pcm)
750c9bfb5d7SJeeja KP 		return -EIO;
751c9bfb5d7SJeeja KP 
7521b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7533787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7541b46ebd1SJeeja KP 		return -EIO;
7551b46ebd1SJeeja KP 
756c9bfb5d7SJeeja KP 	switch (event) {
757c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7583787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
759c9bfb5d7SJeeja KP 
760c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7613787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
762c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
763c9bfb5d7SJeeja KP 
7643787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
765c9bfb5d7SJeeja KP 
7663787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
767c9bfb5d7SJeeja KP 
768c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7693787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
770c9bfb5d7SJeeja KP 
771c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
7723787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
773c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
774c9bfb5d7SJeeja KP 
7753787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
776c9bfb5d7SJeeja KP 		break;
777c9bfb5d7SJeeja KP 
778c9bfb5d7SJeeja KP 	}
779c9bfb5d7SJeeja KP 
780c9bfb5d7SJeeja KP 	return 0;
781c9bfb5d7SJeeja KP }
782c9bfb5d7SJeeja KP 
783c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
784c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
785c9bfb5d7SJeeja KP {
786c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
7873787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
7883787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
789c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
790c9bfb5d7SJeeja KP 
7913787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
792c9bfb5d7SJeeja KP 			__func__, w->name, event);
793c9bfb5d7SJeeja KP 
794c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
795c9bfb5d7SJeeja KP 	if (!pcm)
796c9bfb5d7SJeeja KP 		return -EIO;
797c9bfb5d7SJeeja KP 
798c9bfb5d7SJeeja KP 	switch (event) {
799c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
8003787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
801c9bfb5d7SJeeja KP 
802c9bfb5d7SJeeja KP 		/* Enable transmission */
8033787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
804c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
805c9bfb5d7SJeeja KP 
806c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8073787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
808c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
809c9bfb5d7SJeeja KP 
8103787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
811c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8123787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
813c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
814c9bfb5d7SJeeja KP 		break;
815c9bfb5d7SJeeja KP 
816c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8173787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
818c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8193787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
820c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
821c9bfb5d7SJeeja KP 
8223787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
823c9bfb5d7SJeeja KP 		break;
824c9bfb5d7SJeeja KP 
825c9bfb5d7SJeeja KP 	}
826c9bfb5d7SJeeja KP 
827c9bfb5d7SJeeja KP 	return 0;
828c9bfb5d7SJeeja KP }
829c9bfb5d7SJeeja KP 
830c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
831c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
832c9bfb5d7SJeeja KP {
833754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8343787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
835c9bfb5d7SJeeja KP 	int mux_idx;
836c9bfb5d7SJeeja KP 
8373787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
838c9bfb5d7SJeeja KP 			__func__, w->name, event);
839c9bfb5d7SJeeja KP 
840c9bfb5d7SJeeja KP 	if (!kc)
841c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
842c9bfb5d7SJeeja KP 
843c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8441b46ebd1SJeeja KP 
8451b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8463787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8471b46ebd1SJeeja KP 		return -EIO;
8481b46ebd1SJeeja KP 
849c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8503787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
851c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
852c9bfb5d7SJeeja KP 	}
853c9bfb5d7SJeeja KP 
854c9bfb5d7SJeeja KP 	return 0;
855c9bfb5d7SJeeja KP }
856c9bfb5d7SJeeja KP 
8574a3478deSJeeja KP /*
8584a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8594a3478deSJeeja KP  */
860754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8614a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8624a3478deSJeeja KP {
8634a3478deSJeeja KP 	int ret;
864e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8654a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8664a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8674a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
868754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8693787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
8703787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
8714a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8724a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8734a3478deSJeeja KP 
8744a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8754a3478deSJeeja KP 	if (ret < 0)
8764a3478deSJeeja KP 		return ret;
8774a3478deSJeeja KP 
878754695f9SJeeja KP 	if (port == NULL)
879754695f9SJeeja KP 		return -EINVAL;
880754695f9SJeeja KP 
8814a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8824a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
883e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
884e0e5d3e5SJeeja KP 			continue;
885e0e5d3e5SJeeja KP 
886e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
887e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
888e0e5d3e5SJeeja KP 					p->pin == port->pin) {
889e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
890e0e5d3e5SJeeja KP 				list_del(&p->head);
891e0e5d3e5SJeeja KP 			}
892e0e5d3e5SJeeja KP 		}
893e0e5d3e5SJeeja KP 	}
8944a3478deSJeeja KP 
8954a3478deSJeeja KP 	/*
8964a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8974a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8984a3478deSJeeja KP 	 */
899e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
900e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
901e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
902754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
903e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
9044a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9054a3478deSJeeja KP 				return ret;
9064a3478deSJeeja KP 			}
9074a3478deSJeeja KP 		}
908e0e5d3e5SJeeja KP 	}
9094a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9104a3478deSJeeja KP 
9114a3478deSJeeja KP 	return ret;
9124a3478deSJeeja KP }
9134a3478deSJeeja KP 
91479f4e922SSubhransu S. Prusty /*
91579f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
91679f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
91779f4e922SSubhransu S. Prusty  * widget runtime.
91879f4e922SSubhransu S. Prusty  *
91979f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
92079f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
92179f4e922SSubhransu S. Prusty  * "NONE"
92279f4e922SSubhransu S. Prusty  */
9233787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
924754695f9SJeeja KP 				struct hdac_hdmi_port *port,
92579f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
92679f4e922SSubhransu S. Prusty 				const char *widget_name)
92718382eadSSubhransu S. Prusty {
9283787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
929754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
93079f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
93179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
93279f4e922SSubhransu S. Prusty 	struct soc_enum *se;
93379f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
93479f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
93579f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
93679f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
93779f4e922SSubhransu S. Prusty 	int i = 0;
93879f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
93918382eadSSubhransu S. Prusty 
9403787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
94179f4e922SSubhransu S. Prusty 	if (!kc)
94279f4e922SSubhransu S. Prusty 		return -ENOMEM;
94318382eadSSubhransu S. Prusty 
9443787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
94579f4e922SSubhransu S. Prusty 	if (!se)
94679f4e922SSubhransu S. Prusty 		return -ENOMEM;
94718382eadSSubhransu S. Prusty 
94870e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
94970e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9503787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
95179f4e922SSubhransu S. Prusty 	if (!kc->name)
95279f4e922SSubhransu S. Prusty 		return -ENOMEM;
95318382eadSSubhransu S. Prusty 
95479f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
95579f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
95679f4e922SSubhransu S. Prusty 	kc->access = 0;
95779f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
958754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95979f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
96079f4e922SSubhransu S. Prusty 
96179f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
96279f4e922SSubhransu S. Prusty 
96379f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
96479f4e922SSubhransu S. Prusty 	se->items = num_items;
96579f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
96679f4e922SSubhransu S. Prusty 
96779f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9683787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
96979f4e922SSubhransu S. Prusty 	if (!items[i])
97079f4e922SSubhransu S. Prusty 		return -ENOMEM;
97179f4e922SSubhransu S. Prusty 
97279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
97379f4e922SSubhransu S. Prusty 		i++;
97479f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
9753787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
97679f4e922SSubhransu S. Prusty 		if (!items[i])
97779f4e922SSubhransu S. Prusty 			return -ENOMEM;
97879f4e922SSubhransu S. Prusty 	}
97979f4e922SSubhransu S. Prusty 
9803787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
98179f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
98279f4e922SSubhransu S. Prusty 	if (!se->texts)
98379f4e922SSubhransu S. Prusty 		return -ENOMEM;
98479f4e922SSubhransu S. Prusty 
9853787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
986754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
987c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
988c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98979f4e922SSubhransu S. Prusty }
99079f4e922SSubhransu S. Prusty 
99179f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
9923787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
99379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
99479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
99579f4e922SSubhransu S. Prusty {
9963787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
99779f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
99879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
999754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
100079f4e922SSubhransu S. Prusty 	int i, j;
100179f4e922SSubhransu S. Prusty 
1002754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
100379f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
100479f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
100579f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
100679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
100779f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
100879f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100979f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
101079f4e922SSubhransu S. Prusty 
101179f4e922SSubhransu S. Prusty 			rindex++;
101279f4e922SSubhransu S. Prusty 		}
101379f4e922SSubhransu S. Prusty 
101479f4e922SSubhransu S. Prusty 		mux_index++;
101579f4e922SSubhransu S. Prusty 	}
101679f4e922SSubhransu S. Prusty }
101779f4e922SSubhransu S. Prusty 
101879f4e922SSubhransu S. Prusty /*
101979f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
102079f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1021754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1022754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
102379f4e922SSubhransu S. Prusty  *
1024754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1025754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
102679f4e922SSubhransu S. Prusty  *
102779f4e922SSubhransu S. Prusty  * Routes are added as below:
1028754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1029754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
103079f4e922SSubhransu S. Prusty  *
103179f4e922SSubhransu S. Prusty  * Total route elements:
1032754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
103379f4e922SSubhransu S. Prusty  */
103479f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
103579f4e922SSubhransu S. Prusty {
103679f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
103779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10383787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10393787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10401e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
104179f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
104279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
104379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1044754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
104579f4e922SSubhransu S. Prusty 
104679f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
104779f4e922SSubhransu S. Prusty 		return -EINVAL;
104879f4e922SSubhransu S. Prusty 
1049754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1050754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
105179f4e922SSubhransu S. Prusty 				GFP_KERNEL);
105279f4e922SSubhransu S. Prusty 
105379f4e922SSubhransu S. Prusty 	if (!widgets)
105479f4e922SSubhransu S. Prusty 		return -ENOMEM;
105579f4e922SSubhransu S. Prusty 
105679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
105779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
105879f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105979f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1060c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1061c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1062c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1063c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
106479f4e922SSubhransu S. Prusty 		if (ret < 0)
106579f4e922SSubhransu S. Prusty 			return ret;
106679f4e922SSubhransu S. Prusty 		i++;
106779f4e922SSubhransu S. Prusty 	}
106879f4e922SSubhransu S. Prusty 
106979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1070754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1071754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1072754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
107379f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1074754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1075c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1076c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1077754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1078754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107979f4e922SSubhransu S. Prusty 			if (ret < 0)
108079f4e922SSubhransu S. Prusty 				return ret;
10810324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
108279f4e922SSubhransu S. Prusty 			i++;
108379f4e922SSubhransu S. Prusty 		}
1084754695f9SJeeja KP 	}
108579f4e922SSubhransu S. Prusty 
108679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
108779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1088754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1089754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1090754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
10913787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1092754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
109379f4e922SSubhransu S. Prusty 						widget_name);
109479f4e922SSubhransu S. Prusty 			if (ret < 0)
109579f4e922SSubhransu S. Prusty 				return ret;
109679f4e922SSubhransu S. Prusty 			i++;
109779f4e922SSubhransu S. Prusty 
109879f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109979f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
110079f4e922SSubhransu S. Prusty 
110179f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
110279f4e922SSubhransu S. Prusty 			num_routes++;
110379f4e922SSubhransu S. Prusty 		}
1104754695f9SJeeja KP 	}
110579f4e922SSubhransu S. Prusty 
110679f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
110779f4e922SSubhransu S. Prusty 							GFP_KERNEL);
110879f4e922SSubhransu S. Prusty 	if (!route)
110979f4e922SSubhransu S. Prusty 		return -ENOMEM;
111079f4e922SSubhransu S. Prusty 
111179f4e922SSubhransu S. Prusty 	i = 0;
111279f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
111379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1114754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
111579f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1116754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1117754695f9SJeeja KP 						hdmi->num_pin;
111879f4e922SSubhransu S. Prusty 
111979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
112079f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
112179f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
112279f4e922SSubhransu S. Prusty 			i++;
1123754695f9SJeeja KP 		}
112479f4e922SSubhransu S. Prusty 	}
112579f4e922SSubhransu S. Prusty 
11263787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
112779f4e922SSubhransu S. Prusty 
112879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1129754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
113079f4e922SSubhransu S. Prusty 
113179f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
113279f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
113379f4e922SSubhransu S. Prusty 
113479f4e922SSubhransu S. Prusty 	return 0;
113579f4e922SSubhransu S. Prusty 
113618382eadSSubhransu S. Prusty }
113718382eadSSubhransu S. Prusty 
11383787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
113918382eadSSubhransu S. Prusty {
11403787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1141754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
114215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1143148569fdSSubhransu S. Prusty 	int dai_id = 0;
114418382eadSSubhransu S. Prusty 
1145148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
114615b91447SSubhransu S. Prusty 		return -EINVAL;
114718382eadSSubhransu S. Prusty 
1148148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1149148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1150148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
115115b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
115218382eadSSubhransu S. Prusty 
1153148569fdSSubhransu S. Prusty 		dai_id++;
1154148569fdSSubhransu S. Prusty 
1155148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11563787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1157148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1158148569fdSSubhransu S. Prusty 			break;
1159148569fdSSubhransu S. Prusty 		}
1160148569fdSSubhransu S. Prusty 	}
116118382eadSSubhransu S. Prusty 
116215b91447SSubhransu S. Prusty 	return 0;
116315b91447SSubhransu S. Prusty }
116415b91447SSubhransu S. Prusty 
11653787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
116615b91447SSubhransu S. Prusty {
11673787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
116815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11694a3478deSJeeja KP 	char name[NAME_SIZE];
117015b91447SSubhransu S. Prusty 
117115b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
117215b91447SSubhransu S. Prusty 	if (!cvt)
117315b91447SSubhransu S. Prusty 		return -ENOMEM;
117415b91447SSubhransu S. Prusty 
117515b91447SSubhransu S. Prusty 	cvt->nid = nid;
11764a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11774a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
117815b91447SSubhransu S. Prusty 
117915b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
118015b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
118115b91447SSubhransu S. Prusty 
11823787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
118315b91447SSubhransu S. Prusty }
118415b91447SSubhransu S. Prusty 
11853787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1186754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1187b7756edeSSubhransu S. Prusty {
1188f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1189f6fa11a3SSandeep Tayal 
1190754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1191f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1192f6fa11a3SSandeep Tayal 
1193f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
11943787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1195f6fa11a3SSandeep Tayal 		return -EINVAL;
1196b7756edeSSubhransu S. Prusty 	}
1197b7756edeSSubhransu S. Prusty 
1198754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1199f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1200f6fa11a3SSandeep Tayal 
1201f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
12023787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1203f6fa11a3SSandeep Tayal 		return -EINVAL;
1204f6fa11a3SSandeep Tayal 	}
1205f6fa11a3SSandeep Tayal 
1206754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1207f6fa11a3SSandeep Tayal 
1208f6fa11a3SSandeep Tayal 	return 0;
1209f6fa11a3SSandeep Tayal }
1210f6fa11a3SSandeep Tayal 
1211754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1212754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1213b8a54545SSubhransu S. Prusty {
12143787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12153787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12164a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1217754695f9SJeeja KP 	int size = 0;
12182acd8309SJeeja KP 	int port_id = -1;
1219754695f9SJeeja KP 
1220754695f9SJeeja KP 	if (!hdmi)
1221754695f9SJeeja KP 		return;
12224a3478deSJeeja KP 
12232acd8309SJeeja KP 	/*
12242acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12252acd8309SJeeja KP 	 * to be -1.
12262acd8309SJeeja KP 	 */
12274a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1228754695f9SJeeja KP 	port->eld.monitor_present = false;
1229f6fa11a3SSandeep Tayal 
12302acd8309SJeeja KP 	if (pin->mst_capable)
12312acd8309SJeeja KP 		port_id = port->id;
12322acd8309SJeeja KP 
12333787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1234754695f9SJeeja KP 				&port->eld.monitor_present,
1235754695f9SJeeja KP 				port->eld.eld_buffer,
1236f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1237f6fa11a3SSandeep Tayal 
1238f6fa11a3SSandeep Tayal 	if (size > 0) {
1239f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12403787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1241f6fa11a3SSandeep Tayal 			size = -EINVAL;
1242f6fa11a3SSandeep Tayal 	}
1243f6fa11a3SSandeep Tayal 
1244f6fa11a3SSandeep Tayal 	if (size > 0) {
1245754695f9SJeeja KP 		port->eld.eld_valid = true;
1246754695f9SJeeja KP 		port->eld.eld_size = size;
1247f6fa11a3SSandeep Tayal 	} else {
1248754695f9SJeeja KP 		port->eld.eld_valid = false;
1249754695f9SJeeja KP 		port->eld.eld_size = 0;
1250f6fa11a3SSandeep Tayal 	}
1251b8a54545SSubhransu S. Prusty 
12523787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12534a3478deSJeeja KP 
1254754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1255b8a54545SSubhransu S. Prusty 
12563787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1257754695f9SJeeja KP 						__func__, pin->nid, port->id);
12584a3478deSJeeja KP 
12594a3478deSJeeja KP 		/*
12604a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12614a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12624a3478deSJeeja KP 		 * control select.
12634a3478deSJeeja KP 		 */
1264e0e5d3e5SJeeja KP 		if (pcm)
1265e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12664a3478deSJeeja KP 
12674a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1268f6fa11a3SSandeep Tayal 		return;
1269b8a54545SSubhransu S. Prusty 	}
1270b8a54545SSubhransu S. Prusty 
1271754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1272e0e5d3e5SJeeja KP 		if (pcm)
1273e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12744a3478deSJeeja KP 
1275f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1276754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1277754695f9SJeeja KP 
1278754695f9SJeeja KP 	}
1279754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12804a3478deSJeeja KP }
12814a3478deSJeeja KP 
1282754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1283754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1284754695f9SJeeja KP {
1285754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1286754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1287754695f9SJeeja KP 	int i;
1288754695f9SJeeja KP 
1289754695f9SJeeja KP 	/*
1290754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1291754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1292754695f9SJeeja KP 	 * implemented.
1293754695f9SJeeja KP 	 */
1294754695f9SJeeja KP 
1295754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1296754695f9SJeeja KP 	if (!ports)
1297754695f9SJeeja KP 		return -ENOMEM;
1298754695f9SJeeja KP 
1299754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1300754695f9SJeeja KP 		ports[i].id = i;
1301754695f9SJeeja KP 		ports[i].pin = pin;
1302754695f9SJeeja KP 	}
1303754695f9SJeeja KP 	pin->ports = ports;
1304754695f9SJeeja KP 	pin->num_ports = max_ports;
1305754695f9SJeeja KP 	return 0;
1306b8a54545SSubhransu S. Prusty }
1307b8a54545SSubhransu S. Prusty 
13083787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
130915b91447SSubhransu S. Prusty {
13103787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
131115b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1312754695f9SJeeja KP 	int ret;
131315b91447SSubhransu S. Prusty 
131415b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
131515b91447SSubhransu S. Prusty 	if (!pin)
131615b91447SSubhransu S. Prusty 		return -ENOMEM;
131715b91447SSubhransu S. Prusty 
131815b91447SSubhransu S. Prusty 	pin->nid = nid;
13192acd8309SJeeja KP 	pin->mst_capable = false;
13203787a398SRakesh Ughreja 	pin->hdev = hdev;
1321754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1322754695f9SJeeja KP 	if (ret < 0)
1323754695f9SJeeja KP 		return ret;
132415b91447SSubhransu S. Prusty 
132515b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
132615b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1327754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1328b8a54545SSubhransu S. Prusty 
132915b91447SSubhransu S. Prusty 	return 0;
133018382eadSSubhransu S. Prusty }
133118382eadSSubhransu S. Prusty 
1332211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
13335622bc95SPradeep Tewani #define INTEL_GLK_VENDOR_NID 0x0b
1334211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1335211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1336211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1337211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1338211caab7SSubhransu S. Prusty 
1339f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1340211caab7SSubhransu S. Prusty {
1341211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1342f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13435622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1344211caab7SSubhransu S. Prusty 
1345f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1346211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1347211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1348211caab7SSubhransu S. Prusty 		return;
1349211caab7SSubhransu S. Prusty 
1350211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1351f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1352211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1353211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1354211caab7SSubhransu S. Prusty 		return;
1355211caab7SSubhransu S. Prusty }
1356211caab7SSubhransu S. Prusty 
1357f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1358211caab7SSubhransu S. Prusty {
1359211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1360f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13615622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1362211caab7SSubhransu S. Prusty 
1363f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1364211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1365211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1366211caab7SSubhransu S. Prusty 		return;
1367211caab7SSubhransu S. Prusty 
1368211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1369211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1370f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1371211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1372211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1373211caab7SSubhransu S. Prusty 		return;
1374211caab7SSubhransu S. Prusty 
1375211caab7SSubhransu S. Prusty }
1376211caab7SSubhransu S. Prusty 
137761b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
137817a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
137917a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
138017a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1381c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
138217a42c45SSubhransu S. Prusty };
138317a42c45SSubhransu S. Prusty 
138417a42c45SSubhransu S. Prusty /*
138517a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
138617a42c45SSubhransu S. Prusty  * based on the number of converter queried.
138717a42c45SSubhransu S. Prusty  */
1388f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
138917a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
139017a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
139117a42c45SSubhransu S. Prusty {
139217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
139317a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
139417a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
139517a42c45SSubhransu S. Prusty 	int i = 0;
139617a42c45SSubhransu S. Prusty 	u32 rates, bps;
139717a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
139817a42c45SSubhransu S. Prusty 	u64 formats;
139917a42c45SSubhransu S. Prusty 	int ret;
140017a42c45SSubhransu S. Prusty 
1401f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
140217a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
140317a42c45SSubhransu S. Prusty 			GFP_KERNEL);
140417a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
140517a42c45SSubhransu S. Prusty 		return -ENOMEM;
140617a42c45SSubhransu S. Prusty 
140717a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1408f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
140917a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
141017a42c45SSubhransu S. Prusty 		if (ret)
141117a42c45SSubhransu S. Prusty 			return ret;
141217a42c45SSubhransu S. Prusty 
141317a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1414f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
141517a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
141617a42c45SSubhransu S. Prusty 
141717a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
141817a42c45SSubhransu S. Prusty 			return -ENOMEM;
141917a42c45SSubhransu S. Prusty 
142017a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
142117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1422f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
142317a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142417a42c45SSubhransu S. Prusty 			return -ENOMEM;
142517a42c45SSubhransu S. Prusty 
142617a42c45SSubhransu S. Prusty 		/*
142717a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
142817a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
142917a42c45SSubhransu S. Prusty 		 */
143017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
143117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
143217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
143317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
143666d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
143717a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
143817a42c45SSubhransu S. Prusty 		i++;
143917a42c45SSubhransu S. Prusty 	}
144017a42c45SSubhransu S. Prusty 
144117a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14421e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
144317a42c45SSubhransu S. Prusty 
144417a42c45SSubhransu S. Prusty 	return 0;
144517a42c45SSubhransu S. Prusty }
144617a42c45SSubhransu S. Prusty 
144718382eadSSubhransu S. Prusty /*
144818382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
144918382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
145018382eadSSubhransu S. Prusty  */
14513787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
145217a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
145318382eadSSubhransu S. Prusty {
145418382eadSSubhransu S. Prusty 	hda_nid_t nid;
14553c83ac23SSudip Mukherjee 	int i, num_nodes;
14561c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
14571c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_pin *temp_pin, *pin_next;
14583787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
145915b91447SSubhransu S. Prusty 	int ret;
146018382eadSSubhransu S. Prusty 
1461f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1462f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1463211caab7SSubhransu S. Prusty 
1464f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1465541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1466f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
146718382eadSSubhransu S. Prusty 		return -EINVAL;
146818382eadSSubhransu S. Prusty 	}
146918382eadSSubhransu S. Prusty 
147045a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
147118382eadSSubhransu S. Prusty 		unsigned int caps;
147218382eadSSubhransu S. Prusty 		unsigned int type;
147318382eadSSubhransu S. Prusty 
1474f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
147518382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147618382eadSSubhransu S. Prusty 
147718382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
147818382eadSSubhransu S. Prusty 			continue;
147918382eadSSubhransu S. Prusty 
148018382eadSSubhransu S. Prusty 		switch (type) {
148118382eadSSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
14833787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
148415b91447SSubhransu S. Prusty 			if (ret < 0)
14851c0a7de2SSubhransu S. Prusty 				goto free_widgets;
148618382eadSSubhransu S. Prusty 			break;
148718382eadSSubhransu S. Prusty 
148818382eadSSubhransu S. Prusty 		case AC_WID_PIN:
14893787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
149015b91447SSubhransu S. Prusty 			if (ret < 0)
14911c0a7de2SSubhransu S. Prusty 				goto free_widgets;
149218382eadSSubhransu S. Prusty 			break;
149318382eadSSubhransu S. Prusty 		}
149418382eadSSubhransu S. Prusty 	}
149518382eadSSubhransu S. Prusty 
14961c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
14971c0a7de2SSubhransu S. Prusty 		ret = -EIO;
14981c0a7de2SSubhransu S. Prusty 		goto free_widgets;
14991c0a7de2SSubhransu S. Prusty 	}
150018382eadSSubhransu S. Prusty 
1501f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
150217a42c45SSubhransu S. Prusty 	if (ret) {
1503f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
150417a42c45SSubhransu S. Prusty 							ret);
15051c0a7de2SSubhransu S. Prusty 		goto free_widgets;
150617a42c45SSubhransu S. Prusty 	}
150717a42c45SSubhransu S. Prusty 
150817a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15093787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15101c0a7de2SSubhransu S. Prusty 	if (ret < 0)
15111c0a7de2SSubhransu S. Prusty 		goto free_widgets;
151217a42c45SSubhransu S. Prusty 
15131c0a7de2SSubhransu S. Prusty 	return ret;
15141c0a7de2SSubhransu S. Prusty 
15151c0a7de2SSubhransu S. Prusty free_widgets:
15161c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
15171c0a7de2SSubhransu S. Prusty 		list_del(&temp_cvt->head);
15181c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt->name);
15191c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt);
15201c0a7de2SSubhransu S. Prusty 	}
15211c0a7de2SSubhransu S. Prusty 
15221c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
15231c0a7de2SSubhransu S. Prusty 		for (i = 0; i < temp_pin->num_ports; i++)
15241c0a7de2SSubhransu S. Prusty 			temp_pin->ports[i].pin = NULL;
15251c0a7de2SSubhransu S. Prusty 		kfree(temp_pin->ports);
15261c0a7de2SSubhransu S. Prusty 		list_del(&temp_pin->head);
15271c0a7de2SSubhransu S. Prusty 		kfree(temp_pin);
15281c0a7de2SSubhransu S. Prusty 	}
15291c0a7de2SSubhransu S. Prusty 
15301c0a7de2SSubhransu S. Prusty 	return ret;
153118382eadSSubhransu S. Prusty }
153218382eadSSubhransu S. Prusty 
1533f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1534b8a54545SSubhransu S. Prusty {
15353787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15363787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1537754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1538754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15393787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15402acd8309SJeeja KP 	int i;
1541b8a54545SSubhransu S. Prusty 
1542b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1543b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1544b8a54545SSubhransu S. Prusty 
15453787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1546754695f9SJeeja KP 							pin_nid, pipe);
1547b8a54545SSubhransu S. Prusty 
1548b8a54545SSubhransu S. Prusty 	/*
1549b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1550b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1551b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1552b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1553b8a54545SSubhransu S. Prusty 	 */
155445101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1555b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1556b8a54545SSubhransu S. Prusty 		return;
1557b8a54545SSubhransu S. Prusty 
15583787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1559b8a54545SSubhransu S. Prusty 		return;
1560b8a54545SSubhransu S. Prusty 
1561b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1562754695f9SJeeja KP 		if (pin->nid != pin_nid)
1563754695f9SJeeja KP 			continue;
1564754695f9SJeeja KP 
1565754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1566754695f9SJeeja KP 		if (pipe == -1) {
15672acd8309SJeeja KP 			pin->mst_capable = false;
1568754695f9SJeeja KP 			/* if not MST, default is port[0] */
1569754695f9SJeeja KP 			hport = &pin->ports[0];
15702acd8309SJeeja KP 		} else {
15712acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15722acd8309SJeeja KP 				pin->mst_capable = true;
15732acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15742acd8309SJeeja KP 					hport = &pin->ports[i];
157504c8f2bfSJeeja KP 					break;
15762acd8309SJeeja KP 				}
1577b8a54545SSubhransu S. Prusty 			}
1578b8a54545SSubhransu S. Prusty 		}
1579b8a54545SSubhransu S. Prusty 
158004c8f2bfSJeeja KP 		if (hport)
1581754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1582754695f9SJeeja KP 	}
1583754695f9SJeeja KP 
158404c8f2bfSJeeja KP }
158504c8f2bfSJeeja KP 
1586b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1587b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1588b8a54545SSubhransu S. Prusty };
1589b8a54545SSubhransu S. Prusty 
15902889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15912889099eSSubhransu S. Prusty 						int device)
15922889099eSSubhransu S. Prusty {
15932889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15942889099eSSubhransu S. Prusty 
15952889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15962889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15972889099eSSubhransu S. Prusty 			return rtd->pcm;
15982889099eSSubhransu S. Prusty 	}
15992889099eSSubhransu S. Prusty 
16002889099eSSubhransu S. Prusty 	return NULL;
16012889099eSSubhransu S. Prusty }
16022889099eSSubhransu S. Prusty 
16030324e51bSJeeja KP /* create jack pin kcontrols */
16040324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16053787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16060324e51bSJeeja KP {
16070324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16080324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16090324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16100324e51bSJeeja KP 	char *name;
16110324e51bSJeeja KP 	int i = 0, j;
16123787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16133787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16140324e51bSJeeja KP 
161545101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16160324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16170324e51bSJeeja KP 
16180324e51bSJeeja KP 	if (!kc)
16190324e51bSJeeja KP 		return -ENOMEM;
16200324e51bSJeeja KP 
16210324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16220324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16230324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16240324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
162545101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16260324e51bSJeeja KP 			if (!name)
16270324e51bSJeeja KP 				return -ENOMEM;
16280324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
162945101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16300324e51bSJeeja KP 							GFP_KERNEL);
16310324e51bSJeeja KP 			if (!kc[i].name)
16320324e51bSJeeja KP 				return -ENOMEM;
16330324e51bSJeeja KP 
16340324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16350324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16360324e51bSJeeja KP 			kc[i].access = 0;
16370324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16380324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16390324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16400324e51bSJeeja KP 			i++;
16410324e51bSJeeja KP 		}
16420324e51bSJeeja KP 	}
16430324e51bSJeeja KP 
16440324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16450324e51bSJeeja KP }
16460324e51bSJeeja KP 
164745101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16480324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16490324e51bSJeeja KP {
16503787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
16513787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
16520324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16530324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16540324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16550324e51bSJeeja KP 	char w_name[NAME_SIZE];
16560324e51bSJeeja KP 	int i = 0, j, ret;
16570324e51bSJeeja KP 
16580324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16590324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16600324e51bSJeeja KP 
16610324e51bSJeeja KP 	if (!widgets)
16620324e51bSJeeja KP 		return -ENOMEM;
16630324e51bSJeeja KP 
16640324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16650324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16660324e51bSJeeja KP 	if (!route)
16670324e51bSJeeja KP 		return -ENOMEM;
16680324e51bSJeeja KP 
16690324e51bSJeeja KP 	/* create Jack DAPM widget */
16700324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16710324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16720324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16730324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16740324e51bSJeeja KP 
16750324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16760324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16770324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16780324e51bSJeeja KP 			if (ret < 0)
16790324e51bSJeeja KP 				return ret;
16800324e51bSJeeja KP 
16810324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16820324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16830324e51bSJeeja KP 
16840324e51bSJeeja KP 			/* add to route from Jack widget to output */
16850324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16860324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16870324e51bSJeeja KP 
16880324e51bSJeeja KP 			i++;
16890324e51bSJeeja KP 		}
16900324e51bSJeeja KP 	}
16910324e51bSJeeja KP 
16920324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
16930324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
16940324e51bSJeeja KP 	if (ret < 0)
16950324e51bSJeeja KP 		return ret;
16960324e51bSJeeja KP 
16970324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
16980324e51bSJeeja KP 	if (ret < 0)
16990324e51bSJeeja KP 		return ret;
17000324e51bSJeeja KP 
17010324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17020324e51bSJeeja KP 	if (ret < 0)
17030324e51bSJeeja KP 		return ret;
17040324e51bSJeeja KP 
17050324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17063787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17070324e51bSJeeja KP 
17080324e51bSJeeja KP 	if (ret < 0)
17090324e51bSJeeja KP 		return ret;
17100324e51bSJeeja KP 
17110324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17120324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17130324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17140324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17150324e51bSJeeja KP 						pin->ports[j].jack_pin);
17160324e51bSJeeja KP 	}
17170324e51bSJeeja KP 
17180324e51bSJeeja KP 	return 0;
17190324e51bSJeeja KP }
17200324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17210324e51bSJeeja KP 
172262490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
172362490016SJeeja KP 				struct snd_soc_jack *jack)
17244a3478deSJeeja KP {
172545101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17263787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17273787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17284a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17292889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17302889099eSSubhransu S. Prusty 	int err;
17314a3478deSJeeja KP 
17324a3478deSJeeja KP 	/*
17334a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17344a3478deSJeeja KP 	 * add to the pcm list
17354a3478deSJeeja KP 	 */
17364a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
17374a3478deSJeeja KP 	if (!pcm)
17384a3478deSJeeja KP 		return -ENOMEM;
17394a3478deSJeeja KP 	pcm->pcm_id = device;
17404a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1741e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
174262490016SJeeja KP 	pcm->jack = jack;
1743ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1744e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17452889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17462889099eSSubhransu S. Prusty 	if (snd_pcm) {
17472889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17482889099eSSubhransu S. Prusty 		if (err < 0) {
17493787a398SRakesh Ughreja 			dev_err(&hdev->dev,
17502889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17512889099eSSubhransu S. Prusty 				err, device);
17522889099eSSubhransu S. Prusty 			kfree(pcm);
17532889099eSSubhransu S. Prusty 			return err;
17542889099eSSubhransu S. Prusty 		}
17552889099eSSubhransu S. Prusty 	}
17562889099eSSubhransu S. Prusty 
17574a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17584a3478deSJeeja KP 
175962490016SJeeja KP 	return 0;
17604a3478deSJeeja KP }
17614a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17624a3478deSJeeja KP 
17633787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1764a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1765a9ce96bcSJeeja KP {
1766a9ce96bcSJeeja KP 	int i;
1767a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1768a9ce96bcSJeeja KP 
1769a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1770a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1771a9ce96bcSJeeja KP 
17723787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1773a9ce96bcSJeeja KP 				pin->mst_capable = false;
1774a9ce96bcSJeeja KP 			else
1775a9ce96bcSJeeja KP 				pin->mst_capable = true;
1776a9ce96bcSJeeja KP 		}
1777a9ce96bcSJeeja KP 
1778a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1779a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1780a9ce96bcSJeeja KP 				continue;
1781a9ce96bcSJeeja KP 
1782a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1783a9ce96bcSJeeja KP 		}
1784a9ce96bcSJeeja KP 	}
1785a9ce96bcSJeeja KP }
1786a9ce96bcSJeeja KP 
178745101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
178818382eadSSubhransu S. Prusty {
17893787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17903787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
179118382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
179245101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1793b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1794a9ce96bcSJeeja KP 	int ret;
179518382eadSSubhransu S. Prusty 
17963787a398SRakesh Ughreja 	hdmi->component = component;
179718382eadSSubhransu S. Prusty 
1798b2047e99SVinod Koul 	/*
1799b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1800b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1801b2047e99SVinod Koul 	 */
180276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1803500e06b9SVinod Koul 	if (!hlink) {
18043787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1805500e06b9SVinod Koul 		return -EIO;
1806500e06b9SVinod Koul 	}
1807500e06b9SVinod Koul 
180876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1809b2047e99SVinod Koul 
181079f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
181179f4e922SSubhransu S. Prusty 	if (ret < 0)
181279f4e922SSubhransu S. Prusty 		return ret;
181318382eadSSubhransu S. Prusty 
18143787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1815b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1816b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18173787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1818b8a54545SSubhransu S. Prusty 		return ret;
1819b8a54545SSubhransu S. Prusty 	}
1820b8a54545SSubhransu S. Prusty 
18213787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
182218382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18233787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
182418382eadSSubhransu S. Prusty 
1825e342ac08SSubhransu S. Prusty 	/*
1826e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1827e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1828e342ac08SSubhransu S. Prusty 	 */
18293787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18303787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18313787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1832e342ac08SSubhransu S. Prusty 
1833e342ac08SSubhransu S. Prusty 	return 0;
1834e342ac08SSubhransu S. Prusty }
1835e342ac08SSubhransu S. Prusty 
183645101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1837e342ac08SSubhransu S. Prusty {
18383787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18393787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
1840e342ac08SSubhransu S. Prusty 
18413787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
184218382eadSSubhransu S. Prusty }
184318382eadSSubhransu S. Prusty 
1844571d5078SJeeja KP #ifdef CONFIG_PM
18451b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
18461b377ccdSSubhransu S. Prusty {
18473787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
18481b377ccdSSubhransu S. Prusty 
18493787a398SRakesh Ughreja 	pm_runtime_get_sync(&hdev->dev);
18501b377ccdSSubhransu S. Prusty 
18511b377ccdSSubhransu S. Prusty 	/*
18521b377ccdSSubhransu S. Prusty 	 * Power down afg.
18531b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
18541b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
18551b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
18561b377ccdSSubhransu S. Prusty 	 * to read the state.
18571b377ccdSSubhransu S. Prusty 	 */
1858f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
18591b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
18601b377ccdSSubhransu S. Prusty 
18611b377ccdSSubhransu S. Prusty 	return 0;
18621b377ccdSSubhransu S. Prusty }
18631b377ccdSSubhransu S. Prusty 
18640fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1865571d5078SJeeja KP {
18663787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
18673787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
18681b377ccdSSubhransu S. Prusty 
18691b377ccdSSubhransu S. Prusty 	/* Power up afg */
1870f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
18711b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1872571d5078SJeeja KP 
18733787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
18743787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
1875571d5078SJeeja KP 
1876571d5078SJeeja KP 	/*
1877571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1878571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1879a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1880a9ce96bcSJeeja KP 	 * already set pin caps.
1881571d5078SJeeja KP 	 */
18823787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1883571d5078SJeeja KP 
18843787a398SRakesh Ughreja 	pm_runtime_put_sync(&hdev->dev);
1885571d5078SJeeja KP }
1886571d5078SJeeja KP #else
18871b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
18880fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1889571d5078SJeeja KP #endif
1890571d5078SJeeja KP 
189145101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
189218382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1893e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
189445101122SKuninori Morimoto 	.use_pmdown_time	= 1,
189545101122SKuninori Morimoto 	.endianness		= 1,
189645101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
189718382eadSSubhransu S. Prusty };
189818382eadSSubhransu S. Prusty 
1899f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19002889099eSSubhransu S. Prusty 					unsigned char *chmap)
19012889099eSSubhransu S. Prusty {
1902f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19032889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19042889099eSSubhransu S. Prusty 
1905ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19062889099eSSubhransu S. Prusty }
19072889099eSSubhransu S. Prusty 
1908f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19092889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19102889099eSSubhransu S. Prusty {
1911f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19122889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1913e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1914e0e5d3e5SJeeja KP 
1915eb50fa17SSubhransu S. Prusty 	if (!pcm)
1916eb50fa17SSubhransu S. Prusty 		return;
1917eb50fa17SSubhransu S. Prusty 
1918e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1919e0e5d3e5SJeeja KP 		return;
19202889099eSSubhransu S. Prusty 
1921ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1922ab1eea19SJeeja KP 	pcm->chmap_set = true;
1923ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1924e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19252889099eSSubhransu S. Prusty 		if (prepared)
19263787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1927ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19282889099eSSubhransu S. Prusty }
19292889099eSSubhransu S. Prusty 
1930f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19312889099eSSubhransu S. Prusty {
1932f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19332889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19342889099eSSubhransu S. Prusty 
1935eb50fa17SSubhransu S. Prusty 	if (!pcm)
1936eb50fa17SSubhransu S. Prusty 		return false;
1937eb50fa17SSubhransu S. Prusty 
1938e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1939e0e5d3e5SJeeja KP 		return false;
1940e0e5d3e5SJeeja KP 
1941e0e5d3e5SJeeja KP 	return true;
19422889099eSSubhransu S. Prusty }
19432889099eSSubhransu S. Prusty 
1944f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19452889099eSSubhransu S. Prusty {
1946f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19472889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1948e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1949e0e5d3e5SJeeja KP 
1950eb50fa17SSubhransu S. Prusty 	if (!pcm)
1951eb50fa17SSubhransu S. Prusty 		return 0;
1952eb50fa17SSubhransu S. Prusty 
1953e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1954e0e5d3e5SJeeja KP 		return 0;
1955e0e5d3e5SJeeja KP 
1956e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1957e0e5d3e5SJeeja KP 
1958e0e5d3e5SJeeja KP 	if (!port)
1959e0e5d3e5SJeeja KP 		return 0;
19602889099eSSubhransu S. Prusty 
1961754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19622889099eSSubhransu S. Prusty 		return 0;
19632889099eSSubhransu S. Prusty 
1964754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19652889099eSSubhransu S. Prusty }
19662889099eSSubhransu S. Prusty 
19675622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
19685622bc95SPradeep Tewani 	.vendor_nid = INTEL_GLK_VENDOR_NID,
19695622bc95SPradeep Tewani };
19705622bc95SPradeep Tewani 
19715622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
19725622bc95SPradeep Tewani 	.vendor_nid = INTEL_VENDOR_NID,
19735622bc95SPradeep Tewani };
19745622bc95SPradeep Tewani 
19753787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
197618382eadSSubhransu S. Prusty {
19773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi_priv = NULL;
197817a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1979b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
198017a42c45SSubhransu S. Prusty 	int num_dais = 0;
198118382eadSSubhransu S. Prusty 	int ret = 0;
1982f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1983f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
198418382eadSSubhransu S. Prusty 
1985b2047e99SVinod Koul 	/* hold the ref while we probe */
198676f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1987500e06b9SVinod Koul 	if (!hlink) {
19883787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1989500e06b9SVinod Koul 		return -EIO;
1990500e06b9SVinod Koul 	}
1991500e06b9SVinod Koul 
199276f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1993b2047e99SVinod Koul 
1994f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
199518382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
199618382eadSSubhransu S. Prusty 		return -ENOMEM;
199718382eadSSubhransu S. Prusty 
1998f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
19992889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20002889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20012889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20022889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20033787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
200418382eadSSubhransu S. Prusty 
2005eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2006eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2007eb50fa17SSubhransu S. Prusty 
20085622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20095622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20105622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20115622bc95SPradeep Tewani 	else
20125622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20135622bc95SPradeep Tewani 
20143787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
201518382eadSSubhransu S. Prusty 
201615b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
201715b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20184a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20194a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
202015b91447SSubhransu S. Prusty 
2021aeaccef0SRamesh Babu 	/*
2022aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2023aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2024aeaccef0SRamesh Babu 	 */
20253787a398SRakesh Ughreja 	ret = snd_hdac_display_power(hdev->bus, true);
2026aeaccef0SRamesh Babu 	if (ret < 0) {
20273787a398SRakesh Ughreja 		dev_err(&hdev->dev,
2028aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
2029aeaccef0SRamesh Babu 			ret);
2030aeaccef0SRamesh Babu 		return ret;
2031aeaccef0SRamesh Babu 	}
2032aeaccef0SRamesh Babu 
20333787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
203417a42c45SSubhransu S. Prusty 	if (ret < 0) {
2035f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
203617a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
203718382eadSSubhransu S. Prusty 		return ret;
203817a42c45SSubhransu S. Prusty 	}
20390fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
204018382eadSSubhransu S. Prusty 
204118382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
204245101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
204317a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2044b2047e99SVinod Koul 
204576f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2046b2047e99SVinod Koul 
2047b2047e99SVinod Koul 	return ret;
204818382eadSSubhransu S. Prusty }
204918382eadSSubhransu S. Prusty 
20503787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
205118382eadSSubhransu S. Prusty {
20523787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
205315b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
205415b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
20554a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
20562fe42dd0SJeeja KP 	struct hdac_hdmi_port *port, *port_next;
2057754695f9SJeeja KP 	int i;
205815b91447SSubhransu S. Prusty 
20594a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
20604a3478deSJeeja KP 		pcm->cvt = NULL;
2061e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
2062e0e5d3e5SJeeja KP 			continue;
2063e0e5d3e5SJeeja KP 
20642fe42dd0SJeeja KP 		list_for_each_entry_safe(port, port_next,
20652fe42dd0SJeeja KP 					&pcm->port_list, head)
20662fe42dd0SJeeja KP 			list_del(&port->head);
2067e0e5d3e5SJeeja KP 
20684a3478deSJeeja KP 		list_del(&pcm->head);
20694a3478deSJeeja KP 		kfree(pcm);
20704a3478deSJeeja KP 	}
20714a3478deSJeeja KP 
207215b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
207315b91447SSubhransu S. Prusty 		list_del(&cvt->head);
20744a3478deSJeeja KP 		kfree(cvt->name);
207515b91447SSubhransu S. Prusty 		kfree(cvt);
207615b91447SSubhransu S. Prusty 	}
207715b91447SSubhransu S. Prusty 
207815b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2079754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
2080754695f9SJeeja KP 			pin->ports[i].pin = NULL;
2081754695f9SJeeja KP 		kfree(pin->ports);
208215b91447SSubhransu S. Prusty 		list_del(&pin->head);
208315b91447SSubhransu S. Prusty 		kfree(pin);
208415b91447SSubhransu S. Prusty 	}
208515b91447SSubhransu S. Prusty 
208618382eadSSubhransu S. Prusty 	return 0;
208718382eadSSubhransu S. Prusty }
208818382eadSSubhransu S. Prusty 
2089e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2090e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2091e342ac08SSubhransu S. Prusty {
20923787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2093f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2094b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
209507f083abSSubhransu S. Prusty 	int err;
2096e342ac08SSubhransu S. Prusty 
2097e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2098e342ac08SSubhransu S. Prusty 
209907f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
210007f083abSSubhransu S. Prusty 	if (!bus)
210107f083abSSubhransu S. Prusty 		return 0;
210207f083abSSubhransu S. Prusty 
21031b377ccdSSubhransu S. Prusty 	/*
21041b377ccdSSubhransu S. Prusty 	 * Power down afg.
21051b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21061b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21071b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21081b377ccdSSubhransu S. Prusty 	 * to read the state.
21091b377ccdSSubhransu S. Prusty 	 */
2110f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21111b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
211207f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
211307f083abSSubhransu S. Prusty 	if (err < 0) {
21143787a398SRakesh Ughreja 		dev_err(dev, "Cannot turn on display power on i915\n");
211507f083abSSubhransu S. Prusty 		return err;
211607f083abSSubhransu S. Prusty 	}
211707f083abSSubhransu S. Prusty 
211876f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2119500e06b9SVinod Koul 	if (!hlink) {
2120500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2121500e06b9SVinod Koul 		return -EIO;
2122500e06b9SVinod Koul 	}
2123500e06b9SVinod Koul 
212476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2125b2047e99SVinod Koul 
2126e342ac08SSubhransu S. Prusty 	return 0;
2127e342ac08SSubhransu S. Prusty }
2128e342ac08SSubhransu S. Prusty 
2129e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2130e342ac08SSubhransu S. Prusty {
21313787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2132f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2133b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
213407f083abSSubhransu S. Prusty 	int err;
2135e342ac08SSubhransu S. Prusty 
2136e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2137e342ac08SSubhransu S. Prusty 
213807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
213907f083abSSubhransu S. Prusty 	if (!bus)
214007f083abSSubhransu S. Prusty 		return 0;
214107f083abSSubhransu S. Prusty 
214276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2143500e06b9SVinod Koul 	if (!hlink) {
2144500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2145500e06b9SVinod Koul 		return -EIO;
2146500e06b9SVinod Koul 	}
2147500e06b9SVinod Koul 
214876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2149b2047e99SVinod Koul 
215007f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
215107f083abSSubhransu S. Prusty 	if (err < 0) {
21523787a398SRakesh Ughreja 		dev_err(dev, "Cannot turn on display power on i915\n");
215307f083abSSubhransu S. Prusty 		return err;
215407f083abSSubhransu S. Prusty 	}
215507f083abSSubhransu S. Prusty 
21563787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21573787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2158ab85f5b3SSubhransu S. Prusty 
2159e342ac08SSubhransu S. Prusty 	/* Power up afg */
2160f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21611b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2162e342ac08SSubhransu S. Prusty 
2163e342ac08SSubhransu S. Prusty 	return 0;
2164e342ac08SSubhransu S. Prusty }
2165e342ac08SSubhransu S. Prusty #else
2166e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2167e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2168e342ac08SSubhransu S. Prusty #endif
2169e342ac08SSubhransu S. Prusty 
2170e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2171e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
21721b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
21730fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
2174e342ac08SSubhransu S. Prusty };
2175e342ac08SSubhransu S. Prusty 
217618382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
217718382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2178e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2179cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21805fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21815fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21825622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21835622bc95SPradeep Tewani 						   &intel_glk_drv_data),
218418382eadSSubhransu S. Prusty 	{}
218518382eadSSubhransu S. Prusty };
218618382eadSSubhransu S. Prusty 
218718382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
218818382eadSSubhransu S. Prusty 
2189e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
219018382eadSSubhransu S. Prusty 	.driver = {
219118382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2192e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
219318382eadSSubhransu S. Prusty 	},
219418382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
219518382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
219618382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
219718382eadSSubhransu S. Prusty };
219818382eadSSubhransu S. Prusty 
219918382eadSSubhransu S. Prusty static int __init hdmi_init(void)
220018382eadSSubhransu S. Prusty {
220118382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
220218382eadSSubhransu S. Prusty }
220318382eadSSubhransu S. Prusty 
220418382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
220518382eadSSubhransu S. Prusty {
220618382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
220718382eadSSubhransu S. Prusty }
220818382eadSSubhransu S. Prusty 
220918382eadSSubhransu S. Prusty module_init(hdmi_init);
221018382eadSSubhransu S. Prusty module_exit(hdmi_exit);
221118382eadSSubhransu S. Prusty 
221218382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
221318382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
221418382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
221518382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2216