xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 8e8e69d67e5fad1a1edf97acebd649a6c8f1febd)
1*8e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
218382eadSSubhransu S. Prusty /*
318382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
418382eadSSubhransu S. Prusty  *
518382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
618382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
718382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
818382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
918382eadSSubhransu S. Prusty  *
1018382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1118382eadSSubhransu S. Prusty  */
1218382eadSSubhransu S. Prusty #include <linux/init.h>
1318382eadSSubhransu S. Prusty #include <linux/delay.h>
1418382eadSSubhransu S. Prusty #include <linux/module.h>
1518382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
16a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
172428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
1818382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
194a3478deSJeeja KP #include <sound/jack.h>
2018382eadSSubhransu S. Prusty #include <sound/soc.h>
2118382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
2207f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
232428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
24bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
2518382eadSSubhransu S. Prusty #include "../../hda/local.h"
264a3478deSJeeja KP #include "hdac_hdmi.h"
2718382eadSSubhransu S. Prusty 
2817a42c45SSubhransu S. Prusty #define NAME_SIZE	32
2917a42c45SSubhransu S. Prusty 
30b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
31b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
3218382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
33b0362adbSSubhransu S. Prusty 
3418382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
3518382eadSSubhransu S. Prusty 
36148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
37754695f9SJeeja KP #define HDA_MAX_PORTS		3
38148569fdSSubhransu S. Prusty 
39b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
40b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
41b8a54545SSubhransu S. Prusty 
42f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
43f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
44f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
45f6fa11a3SSandeep Tayal 
4618382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
4718382eadSSubhransu S. Prusty 	unsigned int channels_min;
4818382eadSSubhransu S. Prusty 	unsigned int channels_max;
4918382eadSSubhransu S. Prusty 	u32 rates;
5018382eadSSubhransu S. Prusty 	u64 formats;
5118382eadSSubhransu S. Prusty 	unsigned int maxbps;
5218382eadSSubhransu S. Prusty };
5318382eadSSubhransu S. Prusty 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5515b91447SSubhransu S. Prusty 	struct list_head head;
5618382eadSSubhransu S. Prusty 	hda_nid_t nid;
574a3478deSJeeja KP 	const char *name;
5818382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
5918382eadSSubhransu S. Prusty };
6018382eadSSubhransu S. Prusty 
61b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
62b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
63b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
64b7756edeSSubhransu S. Prusty };
65b7756edeSSubhransu S. Prusty 
66b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
67b8a54545SSubhransu S. Prusty 	bool	monitor_present;
68b8a54545SSubhransu S. Prusty 	bool	eld_valid;
69b8a54545SSubhransu S. Prusty 	int	eld_size;
70b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
71b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
72b8a54545SSubhransu S. Prusty };
73b8a54545SSubhransu S. Prusty 
7418382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7515b91447SSubhransu S. Prusty 	struct list_head head;
7618382eadSSubhransu S. Prusty 	hda_nid_t nid;
772acd8309SJeeja KP 	bool mst_capable;
78754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
79754695f9SJeeja KP 	int num_ports;
803787a398SRakesh Ughreja 	struct hdac_device *hdev;
81754695f9SJeeja KP };
82754695f9SJeeja KP 
83754695f9SJeeja KP struct hdac_hdmi_port {
84e0e5d3e5SJeeja KP 	struct list_head head;
85754695f9SJeeja KP 	int id;
86754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
8718382eadSSubhransu S. Prusty 	int num_mux_nids;
8818382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
89b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
900324e51bSJeeja KP 	const char *jack_pin;
910324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
920324e51bSJeeja KP 	const char *output_pin;
9318382eadSSubhransu S. Prusty };
9418382eadSSubhransu S. Prusty 
954a3478deSJeeja KP struct hdac_hdmi_pcm {
964a3478deSJeeja KP 	struct list_head head;
974a3478deSJeeja KP 	int pcm_id;
98e0e5d3e5SJeeja KP 	struct list_head port_list;
994a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10062490016SJeeja KP 	struct snd_soc_jack *jack;
101c9bfb5d7SJeeja KP 	int stream_tag;
102c9bfb5d7SJeeja KP 	int channels;
103c9bfb5d7SJeeja KP 	int format;
104ab1eea19SJeeja KP 	bool chmap_set;
105ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
106ab1eea19SJeeja KP 	struct mutex lock;
107e0e5d3e5SJeeja KP 	int jack_event;
1084a3478deSJeeja KP };
1094a3478deSJeeja KP 
110754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11118382eadSSubhransu S. Prusty 	int dai_id;
112754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11418382eadSSubhransu S. Prusty };
11518382eadSSubhransu S. Prusty 
116019033c8SBard liao /*
117019033c8SBard liao  * pin to port mapping table where the value indicate the pin number and
118019033c8SBard liao  * the index indicate the port number with 1 base.
119019033c8SBard liao  */
120019033c8SBard liao static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
121019033c8SBard liao 
1225622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1235622bc95SPradeep Tewani 	unsigned int vendor_nid;
124019033c8SBard liao 	const int *port_map; /* pin to port mapping table */
125019033c8SBard liao 	int port_num;
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);
450754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
451c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
452c9bfb5d7SJeeja KP 	int format;
453b0362adbSSubhransu S. Prusty 
45454dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
455b0362adbSSubhransu S. Prusty 
456c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
457b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
45866d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
459b0362adbSSubhransu S. Prusty 
460c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
461c9bfb5d7SJeeja KP 	if (!pcm)
462148569fdSSubhransu S. Prusty 		return -EIO;
463148569fdSSubhransu S. Prusty 
464c9bfb5d7SJeeja KP 	pcm->format = format;
465c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
466148569fdSSubhransu S. Prusty 
467148569fdSSubhransu S. Prusty 	return 0;
468148569fdSSubhransu S. Prusty }
469148569fdSSubhransu S. Prusty 
4703787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
471754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
472754695f9SJeeja KP 					struct hdac_hdmi_port *port)
473148569fdSSubhransu S. Prusty {
4743787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4753787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
476148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4773787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
478148569fdSSubhransu S. Prusty 		return -EINVAL;
479148569fdSSubhransu S. Prusty 	}
480148569fdSSubhransu S. Prusty 
4813787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
4821b46ebd1SJeeja KP 		return -EIO;
4831b46ebd1SJeeja KP 
4843787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
485754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
486754695f9SJeeja KP 	if (port->num_mux_nids == 0)
4873787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
488754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
489754695f9SJeeja KP 						pin->nid, port->id);
490148569fdSSubhransu S. Prusty 
4913787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
492754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
493148569fdSSubhransu S. Prusty 
494754695f9SJeeja KP 	return port->num_mux_nids;
495148569fdSSubhransu S. Prusty }
496148569fdSSubhransu S. Prusty 
497148569fdSSubhransu S. Prusty /*
498754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
499148569fdSSubhransu S. Prusty  *
500754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
501148569fdSSubhransu S. Prusty  *
502754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
503754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
504148569fdSSubhransu S. Prusty  * connected.
505148569fdSSubhransu S. Prusty  */
506754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5073787a398SRakesh Ughreja 			struct hdac_device *hdev,
508148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
509148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
510148569fdSSubhransu S. Prusty {
511148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
512754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
513148569fdSSubhransu S. Prusty 	int ret, i;
514148569fdSSubhransu S. Prusty 
515148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
516148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
517e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
518e0e5d3e5SJeeja KP 				continue;
519148569fdSSubhransu S. Prusty 
520e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
521e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5223787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
523e0e5d3e5SJeeja KP 							port->pin, port);
524e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
525148569fdSSubhransu S. Prusty 				if (ret < 0)
526e0e5d3e5SJeeja KP 					continue;
527148569fdSSubhransu S. Prusty 
528754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
529e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
530e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
531e0e5d3e5SJeeja KP 						port->eld.eld_valid)
532754695f9SJeeja KP 						return port;
533148569fdSSubhransu S. Prusty 				}
534148569fdSSubhransu S. Prusty 			}
535e0e5d3e5SJeeja KP 		}
536e0e5d3e5SJeeja KP 	}
537148569fdSSubhransu S. Prusty 
538148569fdSSubhransu S. Prusty 	return NULL;
539148569fdSSubhransu S. Prusty }
540148569fdSSubhransu S. Prusty 
54154dfa1eaSSubhransu S. Prusty /*
54254dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
54354dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
54454dfa1eaSSubhransu S. Prusty  * doesn't fail.
54554dfa1eaSSubhransu S. Prusty  */
546b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
547b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
548b0362adbSSubhransu S. Prusty {
5493787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5503787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
551754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
552148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
553754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5542428bca3SSubhransu S. Prusty 	int ret;
555b0362adbSSubhransu S. Prusty 
556b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
557b0362adbSSubhransu S. Prusty 
558148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5593787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
56054dfa1eaSSubhransu S. Prusty 
56154dfa1eaSSubhransu S. Prusty 	/*
56254dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
56354dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
56454dfa1eaSSubhransu S. Prusty 	 */
565754695f9SJeeja KP 	if (!port)
56654dfa1eaSSubhransu S. Prusty 		return 0;
567754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
568754695f9SJeeja KP 			(!port->eld.eld_valid)) {
569b0362adbSSubhransu S. Prusty 
5703787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
571754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
572754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
573754695f9SJeeja KP 			port->pin->nid, port->id);
574b8a54545SSubhransu S. Prusty 
57554dfa1eaSSubhransu S. Prusty 		return 0;
576b0362adbSSubhransu S. Prusty 	}
577b0362adbSSubhransu S. Prusty 
578754695f9SJeeja KP 	dai_map->port = port;
579b0362adbSSubhransu S. Prusty 
5802428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
581754695f9SJeeja KP 				port->eld.eld_buffer);
5822428bca3SSubhransu S. Prusty 	if (ret < 0)
5832428bca3SSubhransu S. Prusty 		return ret;
584b0362adbSSubhransu S. Prusty 
5852428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
586754695f9SJeeja KP 				port->eld.eld_buffer);
587b0362adbSSubhransu S. Prusty }
588b0362adbSSubhransu S. Prusty 
589b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
590b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
591b0362adbSSubhransu S. Prusty {
5923787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
593754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
594ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
595b0362adbSSubhransu S. Prusty 
596b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
597b0362adbSSubhransu S. Prusty 
598ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
599bcced704SSubhransu S. Prusty 
600ab1eea19SJeeja KP 	if (pcm) {
601ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
602ab1eea19SJeeja KP 		pcm->chmap_set = false;
603ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
604ab1eea19SJeeja KP 		pcm->channels = 0;
605ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
606b0362adbSSubhransu S. Prusty 	}
607ab1eea19SJeeja KP 
608754695f9SJeeja KP 	if (dai_map->port)
609754695f9SJeeja KP 		dai_map->port = NULL;
61054dfa1eaSSubhransu S. Prusty }
611b0362adbSSubhransu S. Prusty 
61218382eadSSubhransu S. Prusty static int
613f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
61418382eadSSubhransu S. Prusty {
615bcced704SSubhransu S. Prusty 	unsigned int chans;
616f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
61718382eadSSubhransu S. Prusty 	int err;
61818382eadSSubhransu S. Prusty 
619f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
620bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
621bcced704SSubhransu S. Prusty 
622bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
623bcced704SSubhransu S. Prusty 
624bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
625bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
626bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
62718382eadSSubhransu S. Prusty 
628f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
62918382eadSSubhransu S. Prusty 			&cvt->params.rates,
63018382eadSSubhransu S. Prusty 			&cvt->params.formats,
63118382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
63218382eadSSubhransu S. Prusty 	if (err < 0)
633f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
63418382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
63518382eadSSubhransu S. Prusty 			cvt->nid, err);
63618382eadSSubhransu S. Prusty 
63718382eadSSubhransu S. Prusty 	return err;
63818382eadSSubhransu S. Prusty }
63918382eadSSubhransu S. Prusty 
64079f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
641c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
642c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
643c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
644c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
645c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
64618382eadSSubhransu S. Prusty {
64718382eadSSubhransu S. Prusty 	w->id = id;
64879f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
64979f4e922SSubhransu S. Prusty 	if (!w->name)
65079f4e922SSubhransu S. Prusty 		return -ENOMEM;
65179f4e922SSubhransu S. Prusty 
65218382eadSSubhransu S. Prusty 	w->sname = stream;
65318382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
65418382eadSSubhransu S. Prusty 	w->shift = 0;
65579f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
65679f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
65779f4e922SSubhransu S. Prusty 	w->priv = priv;
658c9bfb5d7SJeeja KP 	w->event = event;
659c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
66079f4e922SSubhransu S. Prusty 
66179f4e922SSubhransu S. Prusty 	return 0;
66218382eadSSubhransu S. Prusty }
66318382eadSSubhransu S. Prusty 
66418382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
66579f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
66679f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
66779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
66818382eadSSubhransu S. Prusty {
66918382eadSSubhransu S. Prusty 	route->sink = sink;
67018382eadSSubhransu S. Prusty 	route->source = src;
67118382eadSSubhransu S. Prusty 	route->control = control;
67279f4e922SSubhransu S. Prusty 	route->connected = handler;
67318382eadSSubhransu S. Prusty }
67418382eadSSubhransu S. Prusty 
6753787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
676754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6774a3478deSJeeja KP {
6783787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
6794a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
680e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6814a3478deSJeeja KP 
6824a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
683e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
684754695f9SJeeja KP 			continue;
685754695f9SJeeja KP 
686e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
687e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
6884a3478deSJeeja KP 				return pcm;
6894a3478deSJeeja KP 		}
690e0e5d3e5SJeeja KP 	}
6914a3478deSJeeja KP 
6924a3478deSJeeja KP 	return NULL;
6934a3478deSJeeja KP }
6944a3478deSJeeja KP 
6953787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
696c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
697c9bfb5d7SJeeja KP {
698753597fbSAbhijeet Kumar 	int count;
699753597fbSAbhijeet Kumar 	unsigned int state;
700753597fbSAbhijeet Kumar 
7013787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7023787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
703753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7043787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
705753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
706753597fbSAbhijeet Kumar 						pwr_state);
7073787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
708753597fbSAbhijeet Kumar 						nid, pwr_state);
709753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
710753597fbSAbhijeet Kumar 					break;
711753597fbSAbhijeet Kumar 			}
712753597fbSAbhijeet Kumar 		}
713c9bfb5d7SJeeja KP 	}
714c9bfb5d7SJeeja KP }
715c9bfb5d7SJeeja KP 
7163787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
717c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
718c9bfb5d7SJeeja KP {
7193787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7203787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
721c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
722c9bfb5d7SJeeja KP }
723c9bfb5d7SJeeja KP 
724c9bfb5d7SJeeja KP 
725c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
726c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
727c9bfb5d7SJeeja KP {
728754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7293787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
730c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
731c9bfb5d7SJeeja KP 
7323787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
733c9bfb5d7SJeeja KP 			__func__, w->name, event);
734c9bfb5d7SJeeja KP 
7353787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
736c9bfb5d7SJeeja KP 	if (!pcm)
737c9bfb5d7SJeeja KP 		return -EIO;
738c9bfb5d7SJeeja KP 
7391b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7403787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7411b46ebd1SJeeja KP 		return -EIO;
7421b46ebd1SJeeja KP 
743c9bfb5d7SJeeja KP 	switch (event) {
744c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7453787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
746c9bfb5d7SJeeja KP 
747c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7483787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
749c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
750c9bfb5d7SJeeja KP 
7513787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
752c9bfb5d7SJeeja KP 
7533787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
754c9bfb5d7SJeeja KP 
755c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7563787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
757c9bfb5d7SJeeja KP 
758c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
7593787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
760c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
761c9bfb5d7SJeeja KP 
7623787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
763c9bfb5d7SJeeja KP 		break;
764c9bfb5d7SJeeja KP 
765c9bfb5d7SJeeja KP 	}
766c9bfb5d7SJeeja KP 
767c9bfb5d7SJeeja KP 	return 0;
768c9bfb5d7SJeeja KP }
769c9bfb5d7SJeeja KP 
770c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
771c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
772c9bfb5d7SJeeja KP {
773c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
7743787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
7753787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
776c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
777c9bfb5d7SJeeja KP 
7783787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
779c9bfb5d7SJeeja KP 			__func__, w->name, event);
780c9bfb5d7SJeeja KP 
781c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
782c9bfb5d7SJeeja KP 	if (!pcm)
783c9bfb5d7SJeeja KP 		return -EIO;
784c9bfb5d7SJeeja KP 
785c9bfb5d7SJeeja KP 	switch (event) {
786c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7873787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
788c9bfb5d7SJeeja KP 
789c9bfb5d7SJeeja KP 		/* Enable transmission */
7903787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
791c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
792c9bfb5d7SJeeja KP 
793c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
7943787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
795c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
796c9bfb5d7SJeeja KP 
7973787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
798c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
7993787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
800c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
801c9bfb5d7SJeeja KP 		break;
802c9bfb5d7SJeeja KP 
803c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8043787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
805c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8063787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
807c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
808c9bfb5d7SJeeja KP 
8093787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
810c9bfb5d7SJeeja KP 		break;
811c9bfb5d7SJeeja KP 
812c9bfb5d7SJeeja KP 	}
813c9bfb5d7SJeeja KP 
814c9bfb5d7SJeeja KP 	return 0;
815c9bfb5d7SJeeja KP }
816c9bfb5d7SJeeja KP 
817c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
818c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
819c9bfb5d7SJeeja KP {
820754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8213787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
822c9bfb5d7SJeeja KP 	int mux_idx;
823c9bfb5d7SJeeja KP 
8243787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
825c9bfb5d7SJeeja KP 			__func__, w->name, event);
826c9bfb5d7SJeeja KP 
827c9bfb5d7SJeeja KP 	if (!kc)
828c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
829c9bfb5d7SJeeja KP 
830c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8311b46ebd1SJeeja KP 
8321b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8333787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8341b46ebd1SJeeja KP 		return -EIO;
8351b46ebd1SJeeja KP 
836c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8373787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
838c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
839c9bfb5d7SJeeja KP 	}
840c9bfb5d7SJeeja KP 
841c9bfb5d7SJeeja KP 	return 0;
842c9bfb5d7SJeeja KP }
843c9bfb5d7SJeeja KP 
8444a3478deSJeeja KP /*
8454a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8464a3478deSJeeja KP  */
847754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8484a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8494a3478deSJeeja KP {
8504a3478deSJeeja KP 	int ret;
851e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8524a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8534a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8544a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
855754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8563787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
8573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
8584a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8594a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8604a3478deSJeeja KP 
8614a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8624a3478deSJeeja KP 	if (ret < 0)
8634a3478deSJeeja KP 		return ret;
8644a3478deSJeeja KP 
865754695f9SJeeja KP 	if (port == NULL)
866754695f9SJeeja KP 		return -EINVAL;
867754695f9SJeeja KP 
8684a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8694a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
870e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
871e0e5d3e5SJeeja KP 			continue;
872e0e5d3e5SJeeja KP 
873e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
874e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
875e0e5d3e5SJeeja KP 					p->pin == port->pin) {
876e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
877e0e5d3e5SJeeja KP 				list_del(&p->head);
878e0e5d3e5SJeeja KP 			}
879e0e5d3e5SJeeja KP 		}
880e0e5d3e5SJeeja KP 	}
8814a3478deSJeeja KP 
8824a3478deSJeeja KP 	/*
8834a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8844a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8854a3478deSJeeja KP 	 */
886e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
887e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
888e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
889754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
890e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
8914a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
8924a3478deSJeeja KP 				return ret;
8934a3478deSJeeja KP 			}
8944a3478deSJeeja KP 		}
895e0e5d3e5SJeeja KP 	}
8964a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
8974a3478deSJeeja KP 
8984a3478deSJeeja KP 	return ret;
8994a3478deSJeeja KP }
9004a3478deSJeeja KP 
90179f4e922SSubhransu S. Prusty /*
90279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
90379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
90479f4e922SSubhransu S. Prusty  * widget runtime.
90579f4e922SSubhransu S. Prusty  *
90679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
90779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
90879f4e922SSubhransu S. Prusty  * "NONE"
90979f4e922SSubhransu S. Prusty  */
9103787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
911754695f9SJeeja KP 				struct hdac_hdmi_port *port,
91279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
91379f4e922SSubhransu S. Prusty 				const char *widget_name)
91418382eadSSubhransu S. Prusty {
9153787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
916754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
91779f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
91879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
91979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
92079f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
92179f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
92279f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
92379f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
92479f4e922SSubhransu S. Prusty 	int i = 0;
92579f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
92618382eadSSubhransu S. Prusty 
9273787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
92879f4e922SSubhransu S. Prusty 	if (!kc)
92979f4e922SSubhransu S. Prusty 		return -ENOMEM;
93018382eadSSubhransu S. Prusty 
9313787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
93279f4e922SSubhransu S. Prusty 	if (!se)
93379f4e922SSubhransu S. Prusty 		return -ENOMEM;
93418382eadSSubhransu S. Prusty 
93570e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
93670e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9373787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
93879f4e922SSubhransu S. Prusty 	if (!kc->name)
93979f4e922SSubhransu S. Prusty 		return -ENOMEM;
94018382eadSSubhransu S. Prusty 
94179f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
94279f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
94379f4e922SSubhransu S. Prusty 	kc->access = 0;
94479f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
945754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
94679f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
94779f4e922SSubhransu S. Prusty 
94879f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
94979f4e922SSubhransu S. Prusty 
95079f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
95179f4e922SSubhransu S. Prusty 	se->items = num_items;
95279f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
95379f4e922SSubhransu S. Prusty 
95479f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9553787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
95679f4e922SSubhransu S. Prusty 	if (!items[i])
95779f4e922SSubhransu S. Prusty 		return -ENOMEM;
95879f4e922SSubhransu S. Prusty 
95979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
96079f4e922SSubhransu S. Prusty 		i++;
96179f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
9623787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
96379f4e922SSubhransu S. Prusty 		if (!items[i])
96479f4e922SSubhransu S. Prusty 			return -ENOMEM;
96579f4e922SSubhransu S. Prusty 	}
96679f4e922SSubhransu S. Prusty 
9673787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
96879f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
96979f4e922SSubhransu S. Prusty 	if (!se->texts)
97079f4e922SSubhransu S. Prusty 		return -ENOMEM;
97179f4e922SSubhransu S. Prusty 
9723787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
973754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
974c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
975c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
97679f4e922SSubhransu S. Prusty }
97779f4e922SSubhransu S. Prusty 
97879f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
9793787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
98079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
98179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
98279f4e922SSubhransu S. Prusty {
9833787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
98479f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
98579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
986754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
98779f4e922SSubhransu S. Prusty 	int i, j;
98879f4e922SSubhransu S. Prusty 
989754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
99079f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
99179f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
99279f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
99379f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
99479f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
99579f4e922SSubhransu S. Prusty 					se->texts[j + 1],
99679f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty 			rindex++;
99979f4e922SSubhransu S. Prusty 		}
100079f4e922SSubhransu S. Prusty 
100179f4e922SSubhransu S. Prusty 		mux_index++;
100279f4e922SSubhransu S. Prusty 	}
100379f4e922SSubhransu S. Prusty }
100479f4e922SSubhransu S. Prusty 
100579f4e922SSubhransu S. Prusty /*
100679f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
100779f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1008754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1009754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
101079f4e922SSubhransu S. Prusty  *
1011754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1012754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
101379f4e922SSubhransu S. Prusty  *
101479f4e922SSubhransu S. Prusty  * Routes are added as below:
1015754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1016754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
101779f4e922SSubhransu S. Prusty  *
101879f4e922SSubhransu S. Prusty  * Total route elements:
1019754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
102079f4e922SSubhransu S. Prusty  */
102179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
102279f4e922SSubhransu S. Prusty {
102379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
102479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10253787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10263787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10271e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
102879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
102979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
103079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1031754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
103279f4e922SSubhransu S. Prusty 
103379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
103479f4e922SSubhransu S. Prusty 		return -EINVAL;
103579f4e922SSubhransu S. Prusty 
1036754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1037754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
103879f4e922SSubhransu S. Prusty 				GFP_KERNEL);
103979f4e922SSubhransu S. Prusty 
104079f4e922SSubhransu S. Prusty 	if (!widgets)
104179f4e922SSubhransu S. Prusty 		return -ENOMEM;
104279f4e922SSubhransu S. Prusty 
104379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
104479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
104579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
104679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1047c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1048c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1049c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1050c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
105179f4e922SSubhransu S. Prusty 		if (ret < 0)
105279f4e922SSubhransu S. Prusty 			return ret;
105379f4e922SSubhransu S. Prusty 		i++;
105479f4e922SSubhransu S. Prusty 	}
105579f4e922SSubhransu S. Prusty 
105679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1057754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1058754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1059754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
106079f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1061754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1062c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1063c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1064754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1065754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
106679f4e922SSubhransu S. Prusty 			if (ret < 0)
106779f4e922SSubhransu S. Prusty 				return ret;
10680324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
106979f4e922SSubhransu S. Prusty 			i++;
107079f4e922SSubhransu S. Prusty 		}
1071754695f9SJeeja KP 	}
107279f4e922SSubhransu S. Prusty 
107379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
107479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1075754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1076754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1077754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
10783787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1079754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
108079f4e922SSubhransu S. Prusty 						widget_name);
108179f4e922SSubhransu S. Prusty 			if (ret < 0)
108279f4e922SSubhransu S. Prusty 				return ret;
108379f4e922SSubhransu S. Prusty 			i++;
108479f4e922SSubhransu S. Prusty 
108579f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
108679f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
108779f4e922SSubhransu S. Prusty 
108879f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
108979f4e922SSubhransu S. Prusty 			num_routes++;
109079f4e922SSubhransu S. Prusty 		}
1091754695f9SJeeja KP 	}
109279f4e922SSubhransu S. Prusty 
109379f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
109479f4e922SSubhransu S. Prusty 							GFP_KERNEL);
109579f4e922SSubhransu S. Prusty 	if (!route)
109679f4e922SSubhransu S. Prusty 		return -ENOMEM;
109779f4e922SSubhransu S. Prusty 
109879f4e922SSubhransu S. Prusty 	i = 0;
109979f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
110079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1101754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
110279f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1103754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1104754695f9SJeeja KP 						hdmi->num_pin;
110579f4e922SSubhransu S. Prusty 
110679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
110779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
110879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
110979f4e922SSubhransu S. Prusty 			i++;
1110754695f9SJeeja KP 		}
111179f4e922SSubhransu S. Prusty 	}
111279f4e922SSubhransu S. Prusty 
11133787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
111479f4e922SSubhransu S. Prusty 
111579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1116754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
111779f4e922SSubhransu S. Prusty 
111879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
111979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
112079f4e922SSubhransu S. Prusty 
112179f4e922SSubhransu S. Prusty 	return 0;
112279f4e922SSubhransu S. Prusty 
112318382eadSSubhransu S. Prusty }
112418382eadSSubhransu S. Prusty 
11253787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
112618382eadSSubhransu S. Prusty {
11273787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1128754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
112915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1130148569fdSSubhransu S. Prusty 	int dai_id = 0;
113118382eadSSubhransu S. Prusty 
1132148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
113315b91447SSubhransu S. Prusty 		return -EINVAL;
113418382eadSSubhransu S. Prusty 
1135148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1136148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1137148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
113815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
113918382eadSSubhransu S. Prusty 
1140148569fdSSubhransu S. Prusty 		dai_id++;
1141148569fdSSubhransu S. Prusty 
1142148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11433787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1144148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1145148569fdSSubhransu S. Prusty 			break;
1146148569fdSSubhransu S. Prusty 		}
1147148569fdSSubhransu S. Prusty 	}
114818382eadSSubhransu S. Prusty 
114915b91447SSubhransu S. Prusty 	return 0;
115015b91447SSubhransu S. Prusty }
115115b91447SSubhransu S. Prusty 
11523787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
115315b91447SSubhransu S. Prusty {
11543787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
115515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11564a3478deSJeeja KP 	char name[NAME_SIZE];
115715b91447SSubhransu S. Prusty 
1158c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
115915b91447SSubhransu S. Prusty 	if (!cvt)
116015b91447SSubhransu S. Prusty 		return -ENOMEM;
116115b91447SSubhransu S. Prusty 
116215b91447SSubhransu S. Prusty 	cvt->nid = nid;
11634a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1164c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1165c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1166c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
116715b91447SSubhransu S. Prusty 
116815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
116915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
117015b91447SSubhransu S. Prusty 
11713787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
117215b91447SSubhransu S. Prusty }
117315b91447SSubhransu S. Prusty 
11743787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1175754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1176b7756edeSSubhransu S. Prusty {
1177f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1178f6fa11a3SSandeep Tayal 
1179754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1180f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1181f6fa11a3SSandeep Tayal 
1182f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
11833787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1184f6fa11a3SSandeep Tayal 		return -EINVAL;
1185b7756edeSSubhransu S. Prusty 	}
1186b7756edeSSubhransu S. Prusty 
1187754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1188f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1189f6fa11a3SSandeep Tayal 
1190f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
11913787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1192f6fa11a3SSandeep Tayal 		return -EINVAL;
1193f6fa11a3SSandeep Tayal 	}
1194f6fa11a3SSandeep Tayal 
1195754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1196f6fa11a3SSandeep Tayal 
1197f6fa11a3SSandeep Tayal 	return 0;
1198f6fa11a3SSandeep Tayal }
1199f6fa11a3SSandeep Tayal 
1200754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1201754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1202b8a54545SSubhransu S. Prusty {
12033787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12043787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12054a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1206754695f9SJeeja KP 	int size = 0;
12072acd8309SJeeja KP 	int port_id = -1;
1208754695f9SJeeja KP 
1209754695f9SJeeja KP 	if (!hdmi)
1210754695f9SJeeja KP 		return;
12114a3478deSJeeja KP 
12122acd8309SJeeja KP 	/*
12132acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12142acd8309SJeeja KP 	 * to be -1.
12152acd8309SJeeja KP 	 */
12164a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1217754695f9SJeeja KP 	port->eld.monitor_present = false;
1218f6fa11a3SSandeep Tayal 
12192acd8309SJeeja KP 	if (pin->mst_capable)
12202acd8309SJeeja KP 		port_id = port->id;
12212acd8309SJeeja KP 
12223787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1223754695f9SJeeja KP 				&port->eld.monitor_present,
1224754695f9SJeeja KP 				port->eld.eld_buffer,
1225f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1226f6fa11a3SSandeep Tayal 
1227f6fa11a3SSandeep Tayal 	if (size > 0) {
1228f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12293787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1230f6fa11a3SSandeep Tayal 			size = -EINVAL;
1231f6fa11a3SSandeep Tayal 	}
1232f6fa11a3SSandeep Tayal 
1233f6fa11a3SSandeep Tayal 	if (size > 0) {
1234754695f9SJeeja KP 		port->eld.eld_valid = true;
1235754695f9SJeeja KP 		port->eld.eld_size = size;
1236f6fa11a3SSandeep Tayal 	} else {
1237754695f9SJeeja KP 		port->eld.eld_valid = false;
1238754695f9SJeeja KP 		port->eld.eld_size = 0;
1239f6fa11a3SSandeep Tayal 	}
1240b8a54545SSubhransu S. Prusty 
12413787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12424a3478deSJeeja KP 
1243754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1244b8a54545SSubhransu S. Prusty 
12453787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1246754695f9SJeeja KP 						__func__, pin->nid, port->id);
12474a3478deSJeeja KP 
12484a3478deSJeeja KP 		/*
12494a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12504a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12514a3478deSJeeja KP 		 * control select.
12524a3478deSJeeja KP 		 */
1253e0e5d3e5SJeeja KP 		if (pcm)
1254e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12554a3478deSJeeja KP 
12564a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1257f6fa11a3SSandeep Tayal 		return;
1258b8a54545SSubhransu S. Prusty 	}
1259b8a54545SSubhransu S. Prusty 
1260754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1261e0e5d3e5SJeeja KP 		if (pcm)
1262e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12634a3478deSJeeja KP 
1264f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1265754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1266754695f9SJeeja KP 
1267754695f9SJeeja KP 	}
1268754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12694a3478deSJeeja KP }
12704a3478deSJeeja KP 
1271c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1272754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1273754695f9SJeeja KP {
1274754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1275754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1276754695f9SJeeja KP 	int i;
1277754695f9SJeeja KP 
1278754695f9SJeeja KP 	/*
1279754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1280754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1281754695f9SJeeja KP 	 * implemented.
1282754695f9SJeeja KP 	 */
1283754695f9SJeeja KP 
1284c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1285754695f9SJeeja KP 	if (!ports)
1286754695f9SJeeja KP 		return -ENOMEM;
1287754695f9SJeeja KP 
1288754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1289754695f9SJeeja KP 		ports[i].id = i;
1290754695f9SJeeja KP 		ports[i].pin = pin;
1291754695f9SJeeja KP 	}
1292754695f9SJeeja KP 	pin->ports = ports;
1293754695f9SJeeja KP 	pin->num_ports = max_ports;
1294754695f9SJeeja KP 	return 0;
1295b8a54545SSubhransu S. Prusty }
1296b8a54545SSubhransu S. Prusty 
12973787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
129815b91447SSubhransu S. Prusty {
12993787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
130015b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1301754695f9SJeeja KP 	int ret;
130215b91447SSubhransu S. Prusty 
1303c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
130415b91447SSubhransu S. Prusty 	if (!pin)
130515b91447SSubhransu S. Prusty 		return -ENOMEM;
130615b91447SSubhransu S. Prusty 
130715b91447SSubhransu S. Prusty 	pin->nid = nid;
13082acd8309SJeeja KP 	pin->mst_capable = false;
13093787a398SRakesh Ughreja 	pin->hdev = hdev;
1310c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1311754695f9SJeeja KP 	if (ret < 0)
1312754695f9SJeeja KP 		return ret;
131315b91447SSubhransu S. Prusty 
131415b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
131515b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1316754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1317b8a54545SSubhransu S. Prusty 
131815b91447SSubhransu S. Prusty 	return 0;
131918382eadSSubhransu S. Prusty }
132018382eadSSubhransu S. Prusty 
1321019033c8SBard liao #define INTEL_VENDOR_NID_0x2 0x02
1322019033c8SBard liao #define INTEL_VENDOR_NID_0x8 0x08
1323019033c8SBard liao #define INTEL_VENDOR_NID_0xb 0x0b
1324211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1325211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1326211caab7SSubhransu S. Prusty #define INTEL_EN_DP12		0x02 /* enable DP 1.2 features */
1327211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1328211caab7SSubhransu S. Prusty 
1329f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1330211caab7SSubhransu S. Prusty {
1331211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1332f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13335622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1334211caab7SSubhransu S. Prusty 
1335f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1336211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1337211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1338211caab7SSubhransu S. Prusty 		return;
1339211caab7SSubhransu S. Prusty 
1340211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1341f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1342211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1343211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1344211caab7SSubhransu S. Prusty 		return;
1345211caab7SSubhransu S. Prusty }
1346211caab7SSubhransu S. Prusty 
1347f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1348211caab7SSubhransu S. Prusty {
1349211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1350f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13515622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1352211caab7SSubhransu S. Prusty 
1353f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1354211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1355211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1356211caab7SSubhransu S. Prusty 		return;
1357211caab7SSubhransu S. Prusty 
1358211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1359211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1360f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1361211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1362211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1363211caab7SSubhransu S. Prusty 		return;
1364211caab7SSubhransu S. Prusty 
1365211caab7SSubhransu S. Prusty }
1366211caab7SSubhransu S. Prusty 
136761b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
136817a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
136917a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
137017a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1371c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
137217a42c45SSubhransu S. Prusty };
137317a42c45SSubhransu S. Prusty 
137417a42c45SSubhransu S. Prusty /*
137517a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
137617a42c45SSubhransu S. Prusty  * based on the number of converter queried.
137717a42c45SSubhransu S. Prusty  */
1378f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
137917a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
138017a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
138117a42c45SSubhransu S. Prusty {
138217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
138317a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
138417a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
138517a42c45SSubhransu S. Prusty 	int i = 0;
138617a42c45SSubhransu S. Prusty 	u32 rates, bps;
138717a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
138817a42c45SSubhransu S. Prusty 	u64 formats;
138917a42c45SSubhransu S. Prusty 	int ret;
139017a42c45SSubhransu S. Prusty 
1391f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
139217a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
139317a42c45SSubhransu S. Prusty 			GFP_KERNEL);
139417a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
139517a42c45SSubhransu S. Prusty 		return -ENOMEM;
139617a42c45SSubhransu S. Prusty 
139717a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1398f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
139917a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
140017a42c45SSubhransu S. Prusty 		if (ret)
140117a42c45SSubhransu S. Prusty 			return ret;
140217a42c45SSubhransu S. Prusty 
14033b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
14043b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
14053b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
14063b857472SYong Zhi 		if (!rates)
14073b857472SYong Zhi 			return -EINVAL;
14083b857472SYong Zhi 
140917a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1410f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
141117a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
141217a42c45SSubhransu S. Prusty 
141317a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
141417a42c45SSubhransu S. Prusty 			return -ENOMEM;
141517a42c45SSubhransu S. Prusty 
141617a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
141717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1418f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
141917a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142017a42c45SSubhransu S. Prusty 			return -ENOMEM;
142117a42c45SSubhransu S. Prusty 
142217a42c45SSubhransu S. Prusty 		/*
142317a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
142417a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
142517a42c45SSubhransu S. Prusty 		 */
142617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
142717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
142817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
142917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
143266d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
143317a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
143417a42c45SSubhransu S. Prusty 		i++;
143517a42c45SSubhransu S. Prusty 	}
143617a42c45SSubhransu S. Prusty 
143717a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14381e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
143917a42c45SSubhransu S. Prusty 
144017a42c45SSubhransu S. Prusty 	return 0;
144117a42c45SSubhransu S. Prusty }
144217a42c45SSubhransu S. Prusty 
144318382eadSSubhransu S. Prusty /*
144418382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
144518382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
144618382eadSSubhransu S. Prusty  */
14473787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
144817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
144918382eadSSubhransu S. Prusty {
145018382eadSSubhransu S. Prusty 	hda_nid_t nid;
14513c83ac23SSudip Mukherjee 	int i, num_nodes;
14523787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
145315b91447SSubhransu S. Prusty 	int ret;
145418382eadSSubhransu S. Prusty 
1455f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1456f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1457211caab7SSubhransu S. Prusty 
1458f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1459541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1460f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
146118382eadSSubhransu S. Prusty 		return -EINVAL;
146218382eadSSubhransu S. Prusty 	}
146318382eadSSubhransu S. Prusty 
146445a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
146518382eadSSubhransu S. Prusty 		unsigned int caps;
146618382eadSSubhransu S. Prusty 		unsigned int type;
146718382eadSSubhransu S. Prusty 
1468f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
146918382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147018382eadSSubhransu S. Prusty 
147118382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
147218382eadSSubhransu S. Prusty 			continue;
147318382eadSSubhransu S. Prusty 
147418382eadSSubhransu S. Prusty 		switch (type) {
147518382eadSSubhransu S. Prusty 
147618382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
14773787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
147815b91447SSubhransu S. Prusty 			if (ret < 0)
1479c7ba4e53SPierre-Louis Bossart 				return ret;
148018382eadSSubhransu S. Prusty 			break;
148118382eadSSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty 		case AC_WID_PIN:
14833787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
148415b91447SSubhransu S. Prusty 			if (ret < 0)
1485c7ba4e53SPierre-Louis Bossart 				return ret;
148618382eadSSubhransu S. Prusty 			break;
148718382eadSSubhransu S. Prusty 		}
148818382eadSSubhransu S. Prusty 	}
148918382eadSSubhransu S. Prusty 
14901c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
14911c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1492c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1493c7ba4e53SPierre-Louis Bossart 		return ret;
14941c0a7de2SSubhransu S. Prusty 	}
149518382eadSSubhransu S. Prusty 
1496f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
149717a42c45SSubhransu S. Prusty 	if (ret) {
1498f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
149917a42c45SSubhransu S. Prusty 			ret);
1500c7ba4e53SPierre-Louis Bossart 		return ret;
150117a42c45SSubhransu S. Prusty 	}
150217a42c45SSubhransu S. Prusty 
150317a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15043787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15051c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1506c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1507c7ba4e53SPierre-Louis Bossart 			ret);
15081c0a7de2SSubhransu S. Prusty 	return ret;
150918382eadSSubhransu S. Prusty }
151018382eadSSubhransu S. Prusty 
1511a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1512a57942bfSTakashi Iwai {
1513019033c8SBard liao 	struct hdac_device *hdev = aptr;
1514019033c8SBard liao 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1515019033c8SBard liao 	const int *map = hdmi->drv_data->port_map;
1516019033c8SBard liao 	int i;
1517019033c8SBard liao 
1518019033c8SBard liao 	if (!hdmi->drv_data->port_num)
1519a57942bfSTakashi Iwai 		return pin - 4; /* map NID 0x05 -> port #1 */
1520019033c8SBard liao 
1521019033c8SBard liao 	/*
1522019033c8SBard liao 	 * looking for the pin number in the mapping table and return
1523019033c8SBard liao 	 * the index which indicate the port number
1524019033c8SBard liao 	 */
1525019033c8SBard liao 	for (i = 0; i < hdmi->drv_data->port_num; i++) {
1526019033c8SBard liao 		if (pin == map[i])
1527019033c8SBard liao 			return i + 1;
1528019033c8SBard liao 	}
1529019033c8SBard liao 
1530019033c8SBard liao 	/* return -1 if pin number exceeds our expectation */
1531019033c8SBard liao 	dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1532019033c8SBard liao 	return -1;
1533a57942bfSTakashi Iwai }
1534a57942bfSTakashi Iwai 
1535f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1536b8a54545SSubhransu S. Prusty {
15373787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15383787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1539754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1540754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15413787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15422acd8309SJeeja KP 	int i;
1543019033c8SBard liao 	hda_nid_t pin_nid;
1544b8a54545SSubhransu S. Prusty 
1545019033c8SBard liao 	if (!hdmi->drv_data->port_num) {
1546019033c8SBard liao 		/* for legacy platforms */
1547019033c8SBard liao 		pin_nid = port + 0x04;
1548019033c8SBard liao 	} else if (port < hdmi->drv_data->port_num) {
1549019033c8SBard liao 		/* get pin number from the pin2port mapping table */
1550019033c8SBard liao 		pin_nid = hdmi->drv_data->port_map[port - 1];
1551019033c8SBard liao 	} else {
1552019033c8SBard liao 		dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1553019033c8SBard liao 		return;
1554019033c8SBard liao 	}
1555b8a54545SSubhransu S. Prusty 
15563787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1557754695f9SJeeja KP 							pin_nid, pipe);
1558b8a54545SSubhransu S. Prusty 
1559b8a54545SSubhransu S. Prusty 	/*
1560b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1561b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1562b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1563b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1564b8a54545SSubhransu S. Prusty 	 */
156545101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1566b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1567b8a54545SSubhransu S. Prusty 		return;
1568b8a54545SSubhransu S. Prusty 
15693787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1570b8a54545SSubhransu S. Prusty 		return;
1571b8a54545SSubhransu S. Prusty 
1572b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1573754695f9SJeeja KP 		if (pin->nid != pin_nid)
1574754695f9SJeeja KP 			continue;
1575754695f9SJeeja KP 
1576754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1577754695f9SJeeja KP 		if (pipe == -1) {
15782acd8309SJeeja KP 			pin->mst_capable = false;
1579754695f9SJeeja KP 			/* if not MST, default is port[0] */
1580754695f9SJeeja KP 			hport = &pin->ports[0];
15812acd8309SJeeja KP 		} else {
15822acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15832acd8309SJeeja KP 				pin->mst_capable = true;
15842acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15852acd8309SJeeja KP 					hport = &pin->ports[i];
158604c8f2bfSJeeja KP 					break;
15872acd8309SJeeja KP 				}
1588b8a54545SSubhransu S. Prusty 			}
1589b8a54545SSubhransu S. Prusty 		}
1590b8a54545SSubhransu S. Prusty 
159104c8f2bfSJeeja KP 		if (hport)
1592754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1593754695f9SJeeja KP 	}
1594754695f9SJeeja KP 
159504c8f2bfSJeeja KP }
159604c8f2bfSJeeja KP 
1597ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1598a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1599b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1600b8a54545SSubhransu S. Prusty };
1601b8a54545SSubhransu S. Prusty 
16022889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
16032889099eSSubhransu S. Prusty 						int device)
16042889099eSSubhransu S. Prusty {
16052889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
16062889099eSSubhransu S. Prusty 
1607bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
16082889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16092889099eSSubhransu S. Prusty 			return rtd->pcm;
16102889099eSSubhransu S. Prusty 	}
16112889099eSSubhransu S. Prusty 
16122889099eSSubhransu S. Prusty 	return NULL;
16132889099eSSubhransu S. Prusty }
16142889099eSSubhransu S. Prusty 
16150324e51bSJeeja KP /* create jack pin kcontrols */
16160324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16173787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16180324e51bSJeeja KP {
16190324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16200324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16210324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16220324e51bSJeeja KP 	char *name;
16230324e51bSJeeja KP 	int i = 0, j;
16243787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16253787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16260324e51bSJeeja KP 
162745101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16280324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16290324e51bSJeeja KP 
16300324e51bSJeeja KP 	if (!kc)
16310324e51bSJeeja KP 		return -ENOMEM;
16320324e51bSJeeja KP 
16330324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16340324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16350324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16360324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
163745101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16380324e51bSJeeja KP 			if (!name)
16390324e51bSJeeja KP 				return -ENOMEM;
16400324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
164145101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16420324e51bSJeeja KP 							GFP_KERNEL);
16430324e51bSJeeja KP 			if (!kc[i].name)
16440324e51bSJeeja KP 				return -ENOMEM;
16450324e51bSJeeja KP 
16460324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16470324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16480324e51bSJeeja KP 			kc[i].access = 0;
16490324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16500324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16510324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16520324e51bSJeeja KP 			i++;
16530324e51bSJeeja KP 		}
16540324e51bSJeeja KP 	}
16550324e51bSJeeja KP 
16560324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16570324e51bSJeeja KP }
16580324e51bSJeeja KP 
165945101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16600324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16610324e51bSJeeja KP {
16623787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
16633787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
16640324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16650324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16660324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16670324e51bSJeeja KP 	char w_name[NAME_SIZE];
16680324e51bSJeeja KP 	int i = 0, j, ret;
16690324e51bSJeeja KP 
16700324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16710324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16720324e51bSJeeja KP 
16730324e51bSJeeja KP 	if (!widgets)
16740324e51bSJeeja KP 		return -ENOMEM;
16750324e51bSJeeja KP 
16760324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16770324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16780324e51bSJeeja KP 	if (!route)
16790324e51bSJeeja KP 		return -ENOMEM;
16800324e51bSJeeja KP 
16810324e51bSJeeja KP 	/* create Jack DAPM widget */
16820324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16830324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16840324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16850324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16860324e51bSJeeja KP 
16870324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16880324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16890324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16900324e51bSJeeja KP 			if (ret < 0)
16910324e51bSJeeja KP 				return ret;
16920324e51bSJeeja KP 
16930324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16940324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16950324e51bSJeeja KP 
16960324e51bSJeeja KP 			/* add to route from Jack widget to output */
16970324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16980324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16990324e51bSJeeja KP 
17000324e51bSJeeja KP 			i++;
17010324e51bSJeeja KP 		}
17020324e51bSJeeja KP 	}
17030324e51bSJeeja KP 
17040324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
17050324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
17060324e51bSJeeja KP 	if (ret < 0)
17070324e51bSJeeja KP 		return ret;
17080324e51bSJeeja KP 
17090324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17100324e51bSJeeja KP 	if (ret < 0)
17110324e51bSJeeja KP 		return ret;
17120324e51bSJeeja KP 
17130324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17140324e51bSJeeja KP 	if (ret < 0)
17150324e51bSJeeja KP 		return ret;
17160324e51bSJeeja KP 
17170324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17183787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17190324e51bSJeeja KP 
17200324e51bSJeeja KP 	if (ret < 0)
17210324e51bSJeeja KP 		return ret;
17220324e51bSJeeja KP 
17230324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17240324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17250324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17260324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17270324e51bSJeeja KP 						pin->ports[j].jack_pin);
17280324e51bSJeeja KP 	}
17290324e51bSJeeja KP 
17300324e51bSJeeja KP 	return 0;
17310324e51bSJeeja KP }
17320324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17330324e51bSJeeja KP 
173462490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
173562490016SJeeja KP 				struct snd_soc_jack *jack)
17364a3478deSJeeja KP {
173745101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17383787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17393787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17404a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17412889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17422889099eSSubhransu S. Prusty 	int err;
17434a3478deSJeeja KP 
17444a3478deSJeeja KP 	/*
17454a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17464a3478deSJeeja KP 	 * add to the pcm list
17474a3478deSJeeja KP 	 */
1748c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
17494a3478deSJeeja KP 	if (!pcm)
17504a3478deSJeeja KP 		return -ENOMEM;
17514a3478deSJeeja KP 	pcm->pcm_id = device;
17524a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1753e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
175462490016SJeeja KP 	pcm->jack = jack;
1755ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1756e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17572889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17582889099eSSubhransu S. Prusty 	if (snd_pcm) {
17592889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17602889099eSSubhransu S. Prusty 		if (err < 0) {
17613787a398SRakesh Ughreja 			dev_err(&hdev->dev,
17622889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17632889099eSSubhransu S. Prusty 				err, device);
17642889099eSSubhransu S. Prusty 			return err;
17652889099eSSubhransu S. Prusty 		}
17662889099eSSubhransu S. Prusty 	}
17672889099eSSubhransu S. Prusty 
17684a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17694a3478deSJeeja KP 
177062490016SJeeja KP 	return 0;
17714a3478deSJeeja KP }
17724a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17734a3478deSJeeja KP 
17743787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1775a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1776a9ce96bcSJeeja KP {
1777a9ce96bcSJeeja KP 	int i;
1778a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1779a9ce96bcSJeeja KP 
1780a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1781a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1782a9ce96bcSJeeja KP 
17833787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1784a9ce96bcSJeeja KP 				pin->mst_capable = false;
1785a9ce96bcSJeeja KP 			else
1786a9ce96bcSJeeja KP 				pin->mst_capable = true;
1787a9ce96bcSJeeja KP 		}
1788a9ce96bcSJeeja KP 
1789a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1790a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1791a9ce96bcSJeeja KP 				continue;
1792a9ce96bcSJeeja KP 
1793a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1794a9ce96bcSJeeja KP 		}
1795a9ce96bcSJeeja KP 	}
1796a9ce96bcSJeeja KP }
1797a9ce96bcSJeeja KP 
179845101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
179918382eadSSubhransu S. Prusty {
18003787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18013787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
180218382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
180345101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1804b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1805a9ce96bcSJeeja KP 	int ret;
180618382eadSSubhransu S. Prusty 
18073787a398SRakesh Ughreja 	hdmi->component = component;
180818382eadSSubhransu S. Prusty 
1809b2047e99SVinod Koul 	/*
1810b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1811b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1812b2047e99SVinod Koul 	 */
181376f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1814500e06b9SVinod Koul 	if (!hlink) {
18153787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1816500e06b9SVinod Koul 		return -EIO;
1817500e06b9SVinod Koul 	}
1818500e06b9SVinod Koul 
181976f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1820b2047e99SVinod Koul 
182179f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
182279f4e922SSubhransu S. Prusty 	if (ret < 0)
182379f4e922SSubhransu S. Prusty 		return ret;
182418382eadSSubhransu S. Prusty 
18253787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1826a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1827b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18283787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1829b8a54545SSubhransu S. Prusty 		return ret;
1830b8a54545SSubhransu S. Prusty 	}
1831b8a54545SSubhransu S. Prusty 
18323787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
183318382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18343787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
183518382eadSSubhransu S. Prusty 
1836e342ac08SSubhransu S. Prusty 	/*
183701c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
183801c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
183901c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
184001c83276SLibin Yang 	 * domain in display power will be always turned on before operating
184101c83276SLibin Yang 	 * on the HDMI audio codec registers.
184201c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
184301c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
184401c83276SLibin Yang 	 */
184501c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
184601c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
184701c83276SLibin Yang 	/*
1848e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1849e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1850e342ac08SSubhransu S. Prusty 	 */
18513787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18523787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18533787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1854e342ac08SSubhransu S. Prusty 
1855e342ac08SSubhransu S. Prusty 	return 0;
1856e342ac08SSubhransu S. Prusty }
1857e342ac08SSubhransu S. Prusty 
185845101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1859e342ac08SSubhransu S. Prusty {
18603787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18613787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
1862e342ac08SSubhransu S. Prusty 
18633787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
186418382eadSSubhransu S. Prusty }
186518382eadSSubhransu S. Prusty 
1866687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
1867687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
1868571d5078SJeeja KP {
18693787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
18703787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1871687ae9e2STakashi Iwai 	int ret;
18721b377ccdSSubhransu S. Prusty 
1873687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
1874687ae9e2STakashi Iwai 	if (ret < 0)
1875687ae9e2STakashi Iwai 		return ret;
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.
1881687ae9e2STakashi Iwai 	 *
1882687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1883687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
1884687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
1885571d5078SJeeja KP 	 */
18863787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1887687ae9e2STakashi Iwai 	return 0;
1888571d5078SJeeja KP }
1889571d5078SJeeja KP #else
1890687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
1891571d5078SJeeja KP #endif
1892571d5078SJeeja KP 
189345101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
189418382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1895e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
189645101122SKuninori Morimoto 	.use_pmdown_time	= 1,
189745101122SKuninori Morimoto 	.endianness		= 1,
189845101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
189918382eadSSubhransu S. Prusty };
190018382eadSSubhransu S. Prusty 
1901f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19022889099eSSubhransu S. Prusty 					unsigned char *chmap)
19032889099eSSubhransu S. Prusty {
1904f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19052889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19062889099eSSubhransu S. Prusty 
1907ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19082889099eSSubhransu S. Prusty }
19092889099eSSubhransu S. Prusty 
1910f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19112889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19122889099eSSubhransu S. Prusty {
1913f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19142889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1915e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1916e0e5d3e5SJeeja KP 
1917eb50fa17SSubhransu S. Prusty 	if (!pcm)
1918eb50fa17SSubhransu S. Prusty 		return;
1919eb50fa17SSubhransu S. Prusty 
1920e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1921e0e5d3e5SJeeja KP 		return;
19222889099eSSubhransu S. Prusty 
1923ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1924ab1eea19SJeeja KP 	pcm->chmap_set = true;
1925ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1926e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19272889099eSSubhransu S. Prusty 		if (prepared)
19283787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1929ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19302889099eSSubhransu S. Prusty }
19312889099eSSubhransu S. Prusty 
1932f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19332889099eSSubhransu S. Prusty {
1934f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19352889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19362889099eSSubhransu S. Prusty 
1937eb50fa17SSubhransu S. Prusty 	if (!pcm)
1938eb50fa17SSubhransu S. Prusty 		return false;
1939eb50fa17SSubhransu S. Prusty 
1940e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1941e0e5d3e5SJeeja KP 		return false;
1942e0e5d3e5SJeeja KP 
1943e0e5d3e5SJeeja KP 	return true;
19442889099eSSubhransu S. Prusty }
19452889099eSSubhransu S. Prusty 
1946f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19472889099eSSubhransu S. Prusty {
1948f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19492889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1950e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1951e0e5d3e5SJeeja KP 
1952eb50fa17SSubhransu S. Prusty 	if (!pcm)
1953eb50fa17SSubhransu S. Prusty 		return 0;
1954eb50fa17SSubhransu S. Prusty 
1955e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1956e0e5d3e5SJeeja KP 		return 0;
1957e0e5d3e5SJeeja KP 
1958e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1959e0e5d3e5SJeeja KP 
1960754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19612889099eSSubhransu S. Prusty 		return 0;
19622889099eSSubhransu S. Prusty 
1963754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19642889099eSSubhransu S. Prusty }
19652889099eSSubhransu S. Prusty 
1966019033c8SBard liao static struct hdac_hdmi_drv_data intel_icl_drv_data  = {
1967019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x2,
1968019033c8SBard liao 	.port_map = icl_pin2port_map,
1969019033c8SBard liao 	.port_num = ARRAY_SIZE(icl_pin2port_map),
1970019033c8SBard liao };
1971019033c8SBard liao 
19725622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
1973019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0xb,
19745622bc95SPradeep Tewani };
19755622bc95SPradeep Tewani 
19765622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
1977019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x8,
19785622bc95SPradeep Tewani };
19795622bc95SPradeep Tewani 
19803787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
198118382eadSSubhransu S. Prusty {
19823787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi_priv = NULL;
198317a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1984b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
198517a42c45SSubhransu S. Prusty 	int num_dais = 0;
198618382eadSSubhransu S. Prusty 	int ret = 0;
1987f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1988f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
198918382eadSSubhransu S. Prusty 
1990b2047e99SVinod Koul 	/* hold the ref while we probe */
199176f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1992500e06b9SVinod Koul 	if (!hlink) {
19933787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1994500e06b9SVinod Koul 		return -EIO;
1995500e06b9SVinod Koul 	}
1996500e06b9SVinod Koul 
199776f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1998b2047e99SVinod Koul 
1999f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
200018382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
200118382eadSSubhransu S. Prusty 		return -ENOMEM;
200218382eadSSubhransu S. Prusty 
2003f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20042889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20052889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20062889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20072889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20083787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
200918382eadSSubhransu S. Prusty 
2010eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2011eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2012eb50fa17SSubhransu S. Prusty 
20135622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20145622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20155622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20165622bc95SPradeep Tewani 	else
20175622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20185622bc95SPradeep Tewani 
20193787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
202018382eadSSubhransu S. Prusty 
202115b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
202215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20234a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20244a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
202515b91447SSubhransu S. Prusty 
2026aeaccef0SRamesh Babu 	/*
2027aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2028aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2029aeaccef0SRamesh Babu 	 */
20304f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
20314f799e73STakashi Iwai 
20323787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
203317a42c45SSubhransu S. Prusty 	if (ret < 0) {
2034f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
203517a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
203618382eadSSubhransu S. Prusty 		return ret;
203717a42c45SSubhransu S. Prusty 	}
20380fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
203918382eadSSubhransu S. Prusty 
204018382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
204145101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
204217a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2043b2047e99SVinod Koul 
204476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2045b2047e99SVinod Koul 
2046b2047e99SVinod Koul 	return ret;
204718382eadSSubhransu S. Prusty }
204818382eadSSubhransu S. Prusty 
20493787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
205018382eadSSubhransu S. Prusty {
205177a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
205277a49672STakashi Iwai 
205318382eadSSubhransu S. Prusty 	return 0;
205418382eadSSubhransu S. Prusty }
205518382eadSSubhransu S. Prusty 
2056e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2057e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2058e342ac08SSubhransu S. Prusty {
20593787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2060f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2061b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2062e342ac08SSubhransu S. Prusty 
2063e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2064e342ac08SSubhransu S. Prusty 
206507f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
206607f083abSSubhransu S. Prusty 	if (!bus)
206707f083abSSubhransu S. Prusty 		return 0;
206807f083abSSubhransu S. Prusty 
20691b377ccdSSubhransu S. Prusty 	/*
20701b377ccdSSubhransu S. Prusty 	 * Power down afg.
20711b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
20721b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
20731b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
20741b377ccdSSubhransu S. Prusty 	 * to read the state.
20751b377ccdSSubhransu S. Prusty 	 */
2076f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
20771b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
207807f083abSSubhransu S. Prusty 
207976f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2080500e06b9SVinod Koul 	if (!hlink) {
2081500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2082500e06b9SVinod Koul 		return -EIO;
2083500e06b9SVinod Koul 	}
2084500e06b9SVinod Koul 
208576f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2086b2047e99SVinod Koul 
20874f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
20884c10473dSPierre-Louis Bossart 
20894f799e73STakashi Iwai 	return 0;
2090e342ac08SSubhransu S. Prusty }
2091e342ac08SSubhransu S. Prusty 
2092e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2093e342ac08SSubhransu S. Prusty {
20943787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2095f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2096b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2097e342ac08SSubhransu S. Prusty 
2098e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2099e342ac08SSubhransu S. Prusty 
210007f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
210107f083abSSubhransu S. Prusty 	if (!bus)
210207f083abSSubhransu S. Prusty 		return 0;
210307f083abSSubhransu S. Prusty 
210476f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2105500e06b9SVinod Koul 	if (!hlink) {
2106500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2107500e06b9SVinod Koul 		return -EIO;
2108500e06b9SVinod Koul 	}
2109500e06b9SVinod Koul 
211076f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2111b2047e99SVinod Koul 
21124f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
211307f083abSSubhransu S. Prusty 
21143787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21153787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2116ab85f5b3SSubhransu S. Prusty 
2117e342ac08SSubhransu S. Prusty 	/* Power up afg */
2118f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21191b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2120e342ac08SSubhransu S. Prusty 
2121e342ac08SSubhransu S. Prusty 	return 0;
2122e342ac08SSubhransu S. Prusty }
2123e342ac08SSubhransu S. Prusty #else
2124e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2125e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2126e342ac08SSubhransu S. Prusty #endif
2127e342ac08SSubhransu S. Prusty 
2128e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2129e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2130687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2131e342ac08SSubhransu S. Prusty };
2132e342ac08SSubhransu S. Prusty 
213318382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
213418382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2135e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2136cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21375fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21385fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21395622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21405622bc95SPradeep Tewani 						   &intel_glk_drv_data),
2141019033c8SBard liao 	HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2142019033c8SBard liao 						   &intel_icl_drv_data),
214318382eadSSubhransu S. Prusty 	{}
214418382eadSSubhransu S. Prusty };
214518382eadSSubhransu S. Prusty 
214618382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
214718382eadSSubhransu S. Prusty 
2148e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
214918382eadSSubhransu S. Prusty 	.driver = {
215018382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2151e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
215218382eadSSubhransu S. Prusty 	},
215318382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
215418382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
215518382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
215618382eadSSubhransu S. Prusty };
215718382eadSSubhransu S. Prusty 
215818382eadSSubhransu S. Prusty static int __init hdmi_init(void)
215918382eadSSubhransu S. Prusty {
216018382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
216118382eadSSubhransu S. Prusty }
216218382eadSSubhransu S. Prusty 
216318382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
216418382eadSSubhransu S. Prusty {
216518382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
216618382eadSSubhransu S. Prusty }
216718382eadSSubhransu S. Prusty 
216818382eadSSubhransu S. Prusty module_init(hdmi_init);
216918382eadSSubhransu S. Prusty module_exit(hdmi_exit);
217018382eadSSubhransu S. Prusty 
217118382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
217218382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
217318382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
217418382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2175