xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 489f231e0f4c44d4d019aa5c26e1c3f147875f13)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
3318382eadSSubhransu S. Prusty #include "../../hda/local.h"
344a3478deSJeeja KP #include "hdac_hdmi.h"
3518382eadSSubhransu S. Prusty 
3617a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3717a42c45SSubhransu S. Prusty 
38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
4018382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
41b0362adbSSubhransu S. Prusty 
4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4318382eadSSubhransu S. Prusty 
44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
45754695f9SJeeja KP #define HDA_MAX_PORTS		3
46148569fdSSubhransu S. Prusty 
47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
49b8a54545SSubhransu S. Prusty 
50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
53f6fa11a3SSandeep Tayal 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5518382eadSSubhransu S. Prusty 	unsigned int channels_min;
5618382eadSSubhransu S. Prusty 	unsigned int channels_max;
5718382eadSSubhransu S. Prusty 	u32 rates;
5818382eadSSubhransu S. Prusty 	u64 formats;
5918382eadSSubhransu S. Prusty 	unsigned int maxbps;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
6315b91447SSubhransu S. Prusty 	struct list_head head;
6418382eadSSubhransu S. Prusty 	hda_nid_t nid;
654a3478deSJeeja KP 	const char *name;
6618382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6718382eadSSubhransu S. Prusty };
6818382eadSSubhransu S. Prusty 
69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
71b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
72b7756edeSSubhransu S. Prusty };
73b7756edeSSubhransu S. Prusty 
74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
75b8a54545SSubhransu S. Prusty 	bool	monitor_present;
76b8a54545SSubhransu S. Prusty 	bool	eld_valid;
77b8a54545SSubhransu S. Prusty 	int	eld_size;
78b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
79b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
80b8a54545SSubhransu S. Prusty };
81b8a54545SSubhransu S. Prusty 
8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
8315b91447SSubhransu S. Prusty 	struct list_head head;
8418382eadSSubhransu S. Prusty 	hda_nid_t nid;
852acd8309SJeeja KP 	bool mst_capable;
86754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
87754695f9SJeeja KP 	int num_ports;
883787a398SRakesh Ughreja 	struct hdac_device *hdev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
980324e51bSJeeja KP 	const char *jack_pin;
990324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
1000324e51bSJeeja KP 	const char *output_pin;
10118382eadSSubhransu S. Prusty };
10218382eadSSubhransu S. Prusty 
1034a3478deSJeeja KP struct hdac_hdmi_pcm {
1044a3478deSJeeja KP 	struct list_head head;
1054a3478deSJeeja KP 	int pcm_id;
106e0e5d3e5SJeeja KP 	struct list_head port_list;
1074a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10862490016SJeeja KP 	struct snd_soc_jack *jack;
109c9bfb5d7SJeeja KP 	int stream_tag;
110c9bfb5d7SJeeja KP 	int channels;
111c9bfb5d7SJeeja KP 	int format;
112ab1eea19SJeeja KP 	bool chmap_set;
113ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
114ab1eea19SJeeja KP 	struct mutex lock;
115e0e5d3e5SJeeja KP 	int jack_event;
1164a3478deSJeeja KP };
1174a3478deSJeeja KP 
118754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11918382eadSSubhransu S. Prusty 	int dai_id;
120754695f9SJeeja KP 	struct hdac_hdmi_port *port;
12115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12218382eadSSubhransu S. Prusty };
12318382eadSSubhransu S. Prusty 
124019033c8SBard liao /*
125019033c8SBard liao  * pin to port mapping table where the value indicate the pin number and
126019033c8SBard liao  * the index indicate the port number with 1 base.
127019033c8SBard liao  */
128019033c8SBard liao static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
129019033c8SBard liao 
1305622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1315622bc95SPradeep Tewani 	unsigned int vendor_nid;
132019033c8SBard liao 	const int *port_map; /* pin to port mapping table */
133019033c8SBard liao 	int port_num;
1345622bc95SPradeep Tewani };
1355622bc95SPradeep Tewani 
13618382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1373787a398SRakesh Ughreja 	struct hdac_device *hdev;
1383787a398SRakesh Ughreja 	struct snd_soc_component *component;
1393787a398SRakesh Ughreja 	struct snd_card *card;
140754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
14115b91447SSubhransu S. Prusty 	struct list_head pin_list;
14215b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1434a3478deSJeeja KP 	struct list_head pcm_list;
14415b91447SSubhransu S. Prusty 	int num_pin;
14515b91447SSubhransu S. Prusty 	int num_cvt;
146754695f9SJeeja KP 	int num_ports;
1474a3478deSJeeja KP 	struct mutex pin_mutex;
148bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1495622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1501e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
15118382eadSSubhransu S. Prusty };
15218382eadSSubhransu S. Prusty 
1533787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
154b09b1c3bSUghreja, Rakesh A 
155c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
156c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
157c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
158c9bfb5d7SJeeja KP {
159c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1601de777feSJeeja KP 
161c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
162c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
163c9bfb5d7SJeeja KP 			break;
164c9bfb5d7SJeeja KP 	}
165c9bfb5d7SJeeja KP 
166c9bfb5d7SJeeja KP 	return pcm;
167c9bfb5d7SJeeja KP }
1681de777feSJeeja KP 
169e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
170e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
171e0e5d3e5SJeeja KP {
1723787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
173e0e5d3e5SJeeja KP 
1740324e51bSJeeja KP 	if (is_connect)
1750324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1760324e51bSJeeja KP 	else
1770324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1780324e51bSJeeja KP 
179e0e5d3e5SJeeja KP 	if (is_connect) {
180e0e5d3e5SJeeja KP 		/*
181e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
182e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
183e0e5d3e5SJeeja KP 		 * ports.
184e0e5d3e5SJeeja KP 		 */
185e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1863787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
187e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
188e0e5d3e5SJeeja KP 					pcm->pcm_id);
18962490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
19062490016SJeeja KP 						SND_JACK_AVOUT);
191e0e5d3e5SJeeja KP 		}
192e0e5d3e5SJeeja KP 		pcm->jack_event++;
193e0e5d3e5SJeeja KP 	} else {
194e0e5d3e5SJeeja KP 		/*
195e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
196e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
197e0e5d3e5SJeeja KP 		 * to multiple ports.
198e0e5d3e5SJeeja KP 		 */
199e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
20062490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
201e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
202e0e5d3e5SJeeja KP 			pcm->jack_event--;
203e0e5d3e5SJeeja KP 	}
2040324e51bSJeeja KP 
2050324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
206e0e5d3e5SJeeja KP }
207e0e5d3e5SJeeja KP 
208fc181b04SJeeja KP /* MST supported verbs */
209fc181b04SJeeja KP /*
210fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
211fc181b04SJeeja KP  */
2123787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
213fc181b04SJeeja KP {
214fc181b04SJeeja KP 	unsigned int caps;
215fc181b04SJeeja KP 	unsigned int type, param;
216fc181b04SJeeja KP 
2173787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
218fc181b04SJeeja KP 	type = get_wcaps_type(caps);
219fc181b04SJeeja KP 
220fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
221fc181b04SJeeja KP 		return 0;
222fc181b04SJeeja KP 
2233787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
224fc181b04SJeeja KP 	if (param == -1)
225fc181b04SJeeja KP 		return param;
226fc181b04SJeeja KP 
227fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
228fc181b04SJeeja KP }
229fc181b04SJeeja KP 
230fc181b04SJeeja KP /*
231fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
232fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
233fc181b04SJeeja KP  * is selected or MST is not supported.
234fc181b04SJeeja KP  */
2353787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
236fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
237fc181b04SJeeja KP {
2383787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
239fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
240fc181b04SJeeja KP }
241fc181b04SJeeja KP 
242fc181b04SJeeja KP /*
243fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
244fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
245fc181b04SJeeja KP  */
2463787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
247fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
248fc181b04SJeeja KP {
249fc181b04SJeeja KP 	int num_ports;
250fc181b04SJeeja KP 
251fc181b04SJeeja KP 	if (!port->pin->mst_capable)
252fc181b04SJeeja KP 		return 0;
253fc181b04SJeeja KP 
254fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2553787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
256fc181b04SJeeja KP 	if (num_ports < 0)
257fc181b04SJeeja KP 		return -EIO;
258fc181b04SJeeja KP 	/*
259fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
260fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
261fc181b04SJeeja KP 	 */
262fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
263fc181b04SJeeja KP 		return 0;
264fc181b04SJeeja KP 
2653787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
266fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
267fc181b04SJeeja KP 
2683787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
269fc181b04SJeeja KP 		return -EIO;
270fc181b04SJeeja KP 
2713787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
272fc181b04SJeeja KP 
273fc181b04SJeeja KP 	return 0;
274fc181b04SJeeja KP }
275fc181b04SJeeja KP 
2762889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2772889099eSSubhransu S. Prusty 						int pcm_idx)
2782889099eSSubhransu S. Prusty {
2792889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2802889099eSSubhransu S. Prusty 
2812889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2822889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2832889099eSSubhransu S. Prusty 			return pcm;
2842889099eSSubhransu S. Prusty 	}
2852889099eSSubhransu S. Prusty 
2862889099eSSubhransu S. Prusty 	return NULL;
2872889099eSSubhransu S. Prusty }
2882889099eSSubhransu S. Prusty 
2892428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2902428bca3SSubhransu S. Prusty {
2912428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2922428bca3SSubhransu S. Prusty }
2932428bca3SSubhransu S. Prusty 
2942428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2952428bca3SSubhransu S. Prusty {
2962428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2972428bca3SSubhransu S. Prusty }
2982428bca3SSubhransu S. Prusty 
2992428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
3002428bca3SSubhransu S. Prusty 						void *eld)
3012428bca3SSubhransu S. Prusty {
3022428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3032428bca3SSubhransu S. Prusty 	int i;
3042428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3052428bca3SSubhransu S. Prusty 
3062428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3072428bca3SSubhransu S. Prusty 	if (!sad)
3082428bca3SSubhransu S. Prusty 		goto format_constraint;
3092428bca3SSubhransu S. Prusty 
3102428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3112428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3122428bca3SSubhransu S. Prusty 
3132428bca3SSubhransu S. Prusty 			/*
3142428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3152428bca3SSubhransu S. Prusty 			 * container so we set S32
3162428bca3SSubhransu S. Prusty 			 */
3172428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3182428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3192428bca3SSubhransu S. Prusty 		}
3202428bca3SSubhransu S. Prusty 	}
3212428bca3SSubhransu S. Prusty 
3222428bca3SSubhransu S. Prusty format_constraint:
3232428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3242428bca3SSubhransu S. Prusty 				formats);
3252428bca3SSubhransu S. Prusty 
3262428bca3SSubhransu S. Prusty }
3272428bca3SSubhransu S. Prusty 
328a657f1d0SSubhransu S. Prusty static void
3293787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
330a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
331a657f1d0SSubhransu S. Prusty {
332a657f1d0SSubhransu S. Prusty 	int val;
333a657f1d0SSubhransu S. Prusty 
334a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3353787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
336a657f1d0SSubhransu S. Prusty }
337a657f1d0SSubhransu S. Prusty 
338478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
339478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
340478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
341478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
342478f544eSSubhransu S. Prusty 
343478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
344478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
345478f544eSSubhransu S. Prusty 	u8 CXT04;
346478f544eSSubhransu S. Prusty 	u8 CA;
347478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
348478f544eSSubhransu S. Prusty };
349478f544eSSubhransu S. Prusty 
3503787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
351754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
352a657f1d0SSubhransu S. Prusty {
353a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
354a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
355754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
356478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
3573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
358ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
359478f544eSSubhransu S. Prusty 	u8 *dip;
360a657f1d0SSubhransu S. Prusty 	int ret;
361a657f1d0SSubhransu S. Prusty 	int i;
362478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
363478f544eSSubhransu S. Prusty 	u8 conn_type;
364bcced704SSubhransu S. Prusty 	int channels, ca;
365a657f1d0SSubhransu S. Prusty 
3663787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
367ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
368bcced704SSubhransu S. Prusty 
369bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
3703787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
371bcced704SSubhransu S. Prusty 
372bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
373ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
374bcced704SSubhransu S. Prusty 
375754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
376478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
377a657f1d0SSubhransu S. Prusty 
378478f544eSSubhransu S. Prusty 	switch (conn_type) {
379478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
380478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
381478f544eSSubhransu S. Prusty 
382478f544eSSubhransu S. Prusty 		frame.channels = channels;
383bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
384a657f1d0SSubhransu S. Prusty 
385a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
386a657f1d0SSubhransu S. Prusty 		if (ret < 0)
387a657f1d0SSubhransu S. Prusty 			return ret;
388a657f1d0SSubhransu S. Prusty 
389478f544eSSubhransu S. Prusty 		break;
390478f544eSSubhransu S. Prusty 
391478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
392478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
393478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
394478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
395478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
396478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
397bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
398478f544eSSubhransu S. Prusty 
399478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
400478f544eSSubhransu S. Prusty 		break;
401478f544eSSubhransu S. Prusty 
402478f544eSSubhransu S. Prusty 	default:
4033787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
404478f544eSSubhransu S. Prusty 		return -EIO;
405478f544eSSubhransu S. Prusty 	}
406478f544eSSubhransu S. Prusty 
407a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4083787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4093787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
410a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
411a657f1d0SSubhransu S. Prusty 
412a657f1d0SSubhransu S. Prusty 
413a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4143787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
415478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
416391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4173787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
418391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
419478f544eSSubhransu S. Prusty 	} else {
420478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4213787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
422478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
423478f544eSSubhransu S. Prusty 	}
424a657f1d0SSubhransu S. Prusty 
425a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4263787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4273787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
428a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
429a657f1d0SSubhransu S. Prusty 
430a657f1d0SSubhransu S. Prusty 	return 0;
431a657f1d0SSubhransu S. Prusty }
432a657f1d0SSubhransu S. Prusty 
433c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
434c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
435c9bfb5d7SJeeja KP 		int slots, int slot_width)
436b0362adbSSubhransu S. Prusty {
4373787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4383787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
439754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
440c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
441c9bfb5d7SJeeja KP 
4423787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
443b0362adbSSubhransu S. Prusty 
444b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
445b0362adbSSubhransu S. Prusty 
446c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
447b0362adbSSubhransu S. Prusty 
448c9bfb5d7SJeeja KP 	if (pcm)
449c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
450bcced704SSubhransu S. Prusty 
451c9bfb5d7SJeeja KP 	return 0;
452b0362adbSSubhransu S. Prusty }
453b0362adbSSubhransu S. Prusty 
454b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
455b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
456b0362adbSSubhransu S. Prusty {
4573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
458754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
459c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
460c9bfb5d7SJeeja KP 	int format;
461b0362adbSSubhransu S. Prusty 
46254dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
463b0362adbSSubhransu S. Prusty 
464c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
465b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
46666d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
467b0362adbSSubhransu S. Prusty 
468c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
469c9bfb5d7SJeeja KP 	if (!pcm)
470148569fdSSubhransu S. Prusty 		return -EIO;
471148569fdSSubhransu S. Prusty 
472c9bfb5d7SJeeja KP 	pcm->format = format;
473c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
474148569fdSSubhransu S. Prusty 
475148569fdSSubhransu S. Prusty 	return 0;
476148569fdSSubhransu S. Prusty }
477148569fdSSubhransu S. Prusty 
4783787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
479754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
480754695f9SJeeja KP 					struct hdac_hdmi_port *port)
481148569fdSSubhransu S. Prusty {
4823787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4833787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
484148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4853787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
486148569fdSSubhransu S. Prusty 		return -EINVAL;
487148569fdSSubhransu S. Prusty 	}
488148569fdSSubhransu S. Prusty 
4893787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
4901b46ebd1SJeeja KP 		return -EIO;
4911b46ebd1SJeeja KP 
4923787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
493754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
494754695f9SJeeja KP 	if (port->num_mux_nids == 0)
4953787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
496754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
497754695f9SJeeja KP 						pin->nid, port->id);
498148569fdSSubhransu S. Prusty 
4993787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
500754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
501148569fdSSubhransu S. Prusty 
502754695f9SJeeja KP 	return port->num_mux_nids;
503148569fdSSubhransu S. Prusty }
504148569fdSSubhransu S. Prusty 
505148569fdSSubhransu S. Prusty /*
506754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
507148569fdSSubhransu S. Prusty  *
508754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
509148569fdSSubhransu S. Prusty  *
510754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
511754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
512148569fdSSubhransu S. Prusty  * connected.
513148569fdSSubhransu S. Prusty  */
514754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5153787a398SRakesh Ughreja 			struct hdac_device *hdev,
516148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
517148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
518148569fdSSubhransu S. Prusty {
519148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
520754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
521148569fdSSubhransu S. Prusty 	int ret, i;
522148569fdSSubhransu S. Prusty 
523148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
524148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
525e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
526e0e5d3e5SJeeja KP 				continue;
527148569fdSSubhransu S. Prusty 
528e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
529e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5303787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
531e0e5d3e5SJeeja KP 							port->pin, port);
532e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
533148569fdSSubhransu S. Prusty 				if (ret < 0)
534e0e5d3e5SJeeja KP 					continue;
535148569fdSSubhransu S. Prusty 
536754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
537e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
538e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
539e0e5d3e5SJeeja KP 						port->eld.eld_valid)
540754695f9SJeeja KP 						return port;
541148569fdSSubhransu S. Prusty 				}
542148569fdSSubhransu S. Prusty 			}
543e0e5d3e5SJeeja KP 		}
544e0e5d3e5SJeeja KP 	}
545148569fdSSubhransu S. Prusty 
546148569fdSSubhransu S. Prusty 	return NULL;
547148569fdSSubhransu S. Prusty }
548148569fdSSubhransu S. Prusty 
54954dfa1eaSSubhransu S. Prusty /*
550*489f231eSKai Vehmanen  * Go through all converters and ensure connection is set to
551*489f231eSKai Vehmanen  * the correct pin as set via kcontrols.
552*489f231eSKai Vehmanen  */
553*489f231eSKai Vehmanen static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
554*489f231eSKai Vehmanen {
555*489f231eSKai Vehmanen 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
556*489f231eSKai Vehmanen 	struct hdac_hdmi_port *port;
557*489f231eSKai Vehmanen 	struct hdac_hdmi_cvt *cvt;
558*489f231eSKai Vehmanen 	int cvt_idx = 0;
559*489f231eSKai Vehmanen 
560*489f231eSKai Vehmanen 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
561*489f231eSKai Vehmanen 		port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
562*489f231eSKai Vehmanen 		if (port && port->pin) {
563*489f231eSKai Vehmanen 			snd_hdac_codec_write(hdev, port->pin->nid, 0,
564*489f231eSKai Vehmanen 					     AC_VERB_SET_CONNECT_SEL, cvt_idx);
565*489f231eSKai Vehmanen 			dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
566*489f231eSKai Vehmanen 				__func__, cvt->name, port->pin->nid, cvt_idx);
567*489f231eSKai Vehmanen 		}
568*489f231eSKai Vehmanen 		++cvt_idx;
569*489f231eSKai Vehmanen 	}
570*489f231eSKai Vehmanen }
571*489f231eSKai Vehmanen 
572*489f231eSKai Vehmanen /*
57354dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
57454dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
57554dfa1eaSSubhransu S. Prusty  * doesn't fail.
57654dfa1eaSSubhransu S. Prusty  */
577b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
578b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
579b0362adbSSubhransu S. Prusty {
5803787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5813787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
582754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
583148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
584754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5852428bca3SSubhransu S. Prusty 	int ret;
586b0362adbSSubhransu S. Prusty 
587b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
588b0362adbSSubhransu S. Prusty 
589148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5903787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
59154dfa1eaSSubhransu S. Prusty 
59254dfa1eaSSubhransu S. Prusty 	/*
59354dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
59454dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
59554dfa1eaSSubhransu S. Prusty 	 */
596754695f9SJeeja KP 	if (!port)
59754dfa1eaSSubhransu S. Prusty 		return 0;
598754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
599754695f9SJeeja KP 			(!port->eld.eld_valid)) {
600b0362adbSSubhransu S. Prusty 
6013787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
602754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
603754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
604754695f9SJeeja KP 			port->pin->nid, port->id);
605b8a54545SSubhransu S. Prusty 
60654dfa1eaSSubhransu S. Prusty 		return 0;
607b0362adbSSubhransu S. Prusty 	}
608b0362adbSSubhransu S. Prusty 
609754695f9SJeeja KP 	dai_map->port = port;
610b0362adbSSubhransu S. Prusty 
6112428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
612754695f9SJeeja KP 				port->eld.eld_buffer);
6132428bca3SSubhransu S. Prusty 	if (ret < 0)
6142428bca3SSubhransu S. Prusty 		return ret;
615b0362adbSSubhransu S. Prusty 
6162428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
617754695f9SJeeja KP 				port->eld.eld_buffer);
618b0362adbSSubhransu S. Prusty }
619b0362adbSSubhransu S. Prusty 
620b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
621b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
622b0362adbSSubhransu S. Prusty {
6233787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
624754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
625ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
626b0362adbSSubhransu S. Prusty 
627b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
628b0362adbSSubhransu S. Prusty 
629ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
630bcced704SSubhransu S. Prusty 
631ab1eea19SJeeja KP 	if (pcm) {
632ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
633ab1eea19SJeeja KP 		pcm->chmap_set = false;
634ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
635ab1eea19SJeeja KP 		pcm->channels = 0;
636ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
637b0362adbSSubhransu S. Prusty 	}
638ab1eea19SJeeja KP 
639754695f9SJeeja KP 	if (dai_map->port)
640754695f9SJeeja KP 		dai_map->port = NULL;
64154dfa1eaSSubhransu S. Prusty }
642b0362adbSSubhransu S. Prusty 
64318382eadSSubhransu S. Prusty static int
644f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
64518382eadSSubhransu S. Prusty {
646bcced704SSubhransu S. Prusty 	unsigned int chans;
647f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
64818382eadSSubhransu S. Prusty 	int err;
64918382eadSSubhransu S. Prusty 
650f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
651bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
652bcced704SSubhransu S. Prusty 
653bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
654bcced704SSubhransu S. Prusty 
655bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
656bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
657bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
65818382eadSSubhransu S. Prusty 
659f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
66018382eadSSubhransu S. Prusty 			&cvt->params.rates,
66118382eadSSubhransu S. Prusty 			&cvt->params.formats,
66218382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
66318382eadSSubhransu S. Prusty 	if (err < 0)
664f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
66518382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
66618382eadSSubhransu S. Prusty 			cvt->nid, err);
66718382eadSSubhransu S. Prusty 
66818382eadSSubhransu S. Prusty 	return err;
66918382eadSSubhransu S. Prusty }
67018382eadSSubhransu S. Prusty 
67179f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
672c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
673c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
674c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
675c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
676c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
67718382eadSSubhransu S. Prusty {
67818382eadSSubhransu S. Prusty 	w->id = id;
67979f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
68079f4e922SSubhransu S. Prusty 	if (!w->name)
68179f4e922SSubhransu S. Prusty 		return -ENOMEM;
68279f4e922SSubhransu S. Prusty 
68318382eadSSubhransu S. Prusty 	w->sname = stream;
68418382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
68518382eadSSubhransu S. Prusty 	w->shift = 0;
68679f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
68779f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
68879f4e922SSubhransu S. Prusty 	w->priv = priv;
689c9bfb5d7SJeeja KP 	w->event = event;
690c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
69179f4e922SSubhransu S. Prusty 
69279f4e922SSubhransu S. Prusty 	return 0;
69318382eadSSubhransu S. Prusty }
69418382eadSSubhransu S. Prusty 
69518382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
69679f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
69779f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
69879f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
69918382eadSSubhransu S. Prusty {
70018382eadSSubhransu S. Prusty 	route->sink = sink;
70118382eadSSubhransu S. Prusty 	route->source = src;
70218382eadSSubhransu S. Prusty 	route->control = control;
70379f4e922SSubhransu S. Prusty 	route->connected = handler;
70418382eadSSubhransu S. Prusty }
70518382eadSSubhransu S. Prusty 
7063787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
707754695f9SJeeja KP 					struct hdac_hdmi_port *port)
7084a3478deSJeeja KP {
7093787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
7104a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
711e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7124a3478deSJeeja KP 
7134a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
714e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
715754695f9SJeeja KP 			continue;
716754695f9SJeeja KP 
717e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
718e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7194a3478deSJeeja KP 				return pcm;
7204a3478deSJeeja KP 		}
721e0e5d3e5SJeeja KP 	}
7224a3478deSJeeja KP 
7234a3478deSJeeja KP 	return NULL;
7244a3478deSJeeja KP }
7254a3478deSJeeja KP 
7263787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
727c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
728c9bfb5d7SJeeja KP {
729753597fbSAbhijeet Kumar 	int count;
730753597fbSAbhijeet Kumar 	unsigned int state;
731753597fbSAbhijeet Kumar 
7323787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7333787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
734753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7353787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
736753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
737753597fbSAbhijeet Kumar 						pwr_state);
7383787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
739753597fbSAbhijeet Kumar 						nid, pwr_state);
740753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
741753597fbSAbhijeet Kumar 					break;
742753597fbSAbhijeet Kumar 			}
743753597fbSAbhijeet Kumar 		}
744c9bfb5d7SJeeja KP 	}
745c9bfb5d7SJeeja KP }
746c9bfb5d7SJeeja KP 
7473787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
748c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
749c9bfb5d7SJeeja KP {
7503787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7513787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
752c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
753c9bfb5d7SJeeja KP }
754c9bfb5d7SJeeja KP 
755c9bfb5d7SJeeja KP 
756c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
757c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
758c9bfb5d7SJeeja KP {
759754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7603787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
761c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
762c9bfb5d7SJeeja KP 
7633787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
764c9bfb5d7SJeeja KP 			__func__, w->name, event);
765c9bfb5d7SJeeja KP 
7663787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
767c9bfb5d7SJeeja KP 	if (!pcm)
768c9bfb5d7SJeeja KP 		return -EIO;
769c9bfb5d7SJeeja KP 
7701b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7713787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7721b46ebd1SJeeja KP 		return -EIO;
7731b46ebd1SJeeja KP 
774c9bfb5d7SJeeja KP 	switch (event) {
775c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7763787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
777c9bfb5d7SJeeja KP 
778c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7793787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
780c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
781c9bfb5d7SJeeja KP 
7823787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
783c9bfb5d7SJeeja KP 
7843787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
785c9bfb5d7SJeeja KP 
786c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7873787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
788c9bfb5d7SJeeja KP 
789c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
7903787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
791c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
792c9bfb5d7SJeeja KP 
7933787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
794c9bfb5d7SJeeja KP 		break;
795c9bfb5d7SJeeja KP 
796c9bfb5d7SJeeja KP 	}
797c9bfb5d7SJeeja KP 
798c9bfb5d7SJeeja KP 	return 0;
799c9bfb5d7SJeeja KP }
800c9bfb5d7SJeeja KP 
801c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
802c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
803c9bfb5d7SJeeja KP {
804c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
8053787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
8063787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
807c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
808c9bfb5d7SJeeja KP 
8093787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
810c9bfb5d7SJeeja KP 			__func__, w->name, event);
811c9bfb5d7SJeeja KP 
812c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
813c9bfb5d7SJeeja KP 	if (!pcm)
814c9bfb5d7SJeeja KP 		return -EIO;
815c9bfb5d7SJeeja KP 
816c9bfb5d7SJeeja KP 	switch (event) {
817c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
8183787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
819c9bfb5d7SJeeja KP 
820c9bfb5d7SJeeja KP 		/* Enable transmission */
8213787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
822c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
823c9bfb5d7SJeeja KP 
824c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8253787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
826c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
827c9bfb5d7SJeeja KP 
8283787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
829c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8303787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
831c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
832*489f231eSKai Vehmanen 
833*489f231eSKai Vehmanen 		/*
834*489f231eSKai Vehmanen 		 * The connection indices are shared by all converters and
835*489f231eSKai Vehmanen 		 * may interfere with each other. Ensure correct
836*489f231eSKai Vehmanen 		 * routing for all converters at stream start.
837*489f231eSKai Vehmanen 		 */
838*489f231eSKai Vehmanen 		hdac_hdmi_verify_connect_sel_all_pins(hdev);
839*489f231eSKai Vehmanen 
840c9bfb5d7SJeeja KP 		break;
841c9bfb5d7SJeeja KP 
842c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8433787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
844c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8453787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
846c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
847c9bfb5d7SJeeja KP 
8483787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
849c9bfb5d7SJeeja KP 		break;
850c9bfb5d7SJeeja KP 
851c9bfb5d7SJeeja KP 	}
852c9bfb5d7SJeeja KP 
853c9bfb5d7SJeeja KP 	return 0;
854c9bfb5d7SJeeja KP }
855c9bfb5d7SJeeja KP 
856c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
857c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
858c9bfb5d7SJeeja KP {
859754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8603787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
861c9bfb5d7SJeeja KP 	int mux_idx;
862c9bfb5d7SJeeja KP 
8633787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
864c9bfb5d7SJeeja KP 			__func__, w->name, event);
865c9bfb5d7SJeeja KP 
866c9bfb5d7SJeeja KP 	if (!kc)
867c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
868c9bfb5d7SJeeja KP 
869c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8701b46ebd1SJeeja KP 
8711b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8723787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8731b46ebd1SJeeja KP 		return -EIO;
8741b46ebd1SJeeja KP 
875c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8763787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
877c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
878c9bfb5d7SJeeja KP 	}
879c9bfb5d7SJeeja KP 
880c9bfb5d7SJeeja KP 	return 0;
881c9bfb5d7SJeeja KP }
882c9bfb5d7SJeeja KP 
8834a3478deSJeeja KP /*
8844a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8854a3478deSJeeja KP  */
886754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8874a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8884a3478deSJeeja KP {
8894a3478deSJeeja KP 	int ret;
890e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8914a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8924a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8934a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
894754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8953787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
8963787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
8974a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8984a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8994a3478deSJeeja KP 
9004a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
9014a3478deSJeeja KP 	if (ret < 0)
9024a3478deSJeeja KP 		return ret;
9034a3478deSJeeja KP 
904754695f9SJeeja KP 	if (port == NULL)
905754695f9SJeeja KP 		return -EINVAL;
906754695f9SJeeja KP 
9074a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
9084a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
909e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
910e0e5d3e5SJeeja KP 			continue;
911e0e5d3e5SJeeja KP 
912e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
913e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
914e0e5d3e5SJeeja KP 					p->pin == port->pin) {
915e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
916e0e5d3e5SJeeja KP 				list_del(&p->head);
917e0e5d3e5SJeeja KP 			}
918e0e5d3e5SJeeja KP 		}
919e0e5d3e5SJeeja KP 	}
9204a3478deSJeeja KP 
9214a3478deSJeeja KP 	/*
9224a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
9234a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
9244a3478deSJeeja KP 	 */
925e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
926e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
927e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
928754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
929e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
9304a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9314a3478deSJeeja KP 				return ret;
9324a3478deSJeeja KP 			}
9334a3478deSJeeja KP 		}
934e0e5d3e5SJeeja KP 	}
9354a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9364a3478deSJeeja KP 
9374a3478deSJeeja KP 	return ret;
9384a3478deSJeeja KP }
9394a3478deSJeeja KP 
94079f4e922SSubhransu S. Prusty /*
94179f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
94279f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
94379f4e922SSubhransu S. Prusty  * widget runtime.
94479f4e922SSubhransu S. Prusty  *
94579f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
94679f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
94779f4e922SSubhransu S. Prusty  * "NONE"
94879f4e922SSubhransu S. Prusty  */
9493787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
950754695f9SJeeja KP 				struct hdac_hdmi_port *port,
95179f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
95279f4e922SSubhransu S. Prusty 				const char *widget_name)
95318382eadSSubhransu S. Prusty {
9543787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
955754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
95679f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
95779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
95879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
95979f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
96079f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
96179f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
96279f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
96379f4e922SSubhransu S. Prusty 	int i = 0;
96479f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
96518382eadSSubhransu S. Prusty 
9663787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
96779f4e922SSubhransu S. Prusty 	if (!kc)
96879f4e922SSubhransu S. Prusty 		return -ENOMEM;
96918382eadSSubhransu S. Prusty 
9703787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
97179f4e922SSubhransu S. Prusty 	if (!se)
97279f4e922SSubhransu S. Prusty 		return -ENOMEM;
97318382eadSSubhransu S. Prusty 
97470e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
97570e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9763787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
97779f4e922SSubhransu S. Prusty 	if (!kc->name)
97879f4e922SSubhransu S. Prusty 		return -ENOMEM;
97918382eadSSubhransu S. Prusty 
98079f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
98179f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
98279f4e922SSubhransu S. Prusty 	kc->access = 0;
98379f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
984754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
98579f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
98679f4e922SSubhransu S. Prusty 
98779f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
98879f4e922SSubhransu S. Prusty 
98979f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
99079f4e922SSubhransu S. Prusty 	se->items = num_items;
99179f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
99279f4e922SSubhransu S. Prusty 
99379f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9943787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
99579f4e922SSubhransu S. Prusty 	if (!items[i])
99679f4e922SSubhransu S. Prusty 		return -ENOMEM;
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
99979f4e922SSubhransu S. Prusty 		i++;
100079f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
10013787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
100279f4e922SSubhransu S. Prusty 		if (!items[i])
100379f4e922SSubhransu S. Prusty 			return -ENOMEM;
100479f4e922SSubhransu S. Prusty 	}
100579f4e922SSubhransu S. Prusty 
10063787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
100779f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
100879f4e922SSubhransu S. Prusty 	if (!se->texts)
100979f4e922SSubhransu S. Prusty 		return -ENOMEM;
101079f4e922SSubhransu S. Prusty 
10113787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1012754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1013c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
1014c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
101579f4e922SSubhransu S. Prusty }
101679f4e922SSubhransu S. Prusty 
101779f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
10183787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
101979f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
102079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
102179f4e922SSubhransu S. Prusty {
10223787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
102379f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
102479f4e922SSubhransu S. Prusty 	struct soc_enum *se;
1025754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
102679f4e922SSubhransu S. Prusty 	int i, j;
102779f4e922SSubhransu S. Prusty 
1028754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
102979f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
103079f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
103179f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
103279f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
103379f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
103479f4e922SSubhransu S. Prusty 					se->texts[j + 1],
103579f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
103679f4e922SSubhransu S. Prusty 
103779f4e922SSubhransu S. Prusty 			rindex++;
103879f4e922SSubhransu S. Prusty 		}
103979f4e922SSubhransu S. Prusty 
104079f4e922SSubhransu S. Prusty 		mux_index++;
104179f4e922SSubhransu S. Prusty 	}
104279f4e922SSubhransu S. Prusty }
104379f4e922SSubhransu S. Prusty 
104479f4e922SSubhransu S. Prusty /*
104579f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
104679f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1047754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1048754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
104979f4e922SSubhransu S. Prusty  *
1050754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1051754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
105279f4e922SSubhransu S. Prusty  *
105379f4e922SSubhransu S. Prusty  * Routes are added as below:
1054754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1055754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
105679f4e922SSubhransu S. Prusty  *
105779f4e922SSubhransu S. Prusty  * Total route elements:
1058754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
105979f4e922SSubhransu S. Prusty  */
106079f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
106179f4e922SSubhransu S. Prusty {
106279f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
106379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10643787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10653787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10661e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
106779f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
106879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
106979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1070754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
107179f4e922SSubhransu S. Prusty 
107279f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
107379f4e922SSubhransu S. Prusty 		return -EINVAL;
107479f4e922SSubhransu S. Prusty 
1075754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1076754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
107779f4e922SSubhransu S. Prusty 				GFP_KERNEL);
107879f4e922SSubhransu S. Prusty 
107979f4e922SSubhransu S. Prusty 	if (!widgets)
108079f4e922SSubhransu S. Prusty 		return -ENOMEM;
108179f4e922SSubhransu S. Prusty 
108279f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
108379f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
108479f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
108579f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1086c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1087c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1088c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1089c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
109079f4e922SSubhransu S. Prusty 		if (ret < 0)
109179f4e922SSubhransu S. Prusty 			return ret;
109279f4e922SSubhransu S. Prusty 		i++;
109379f4e922SSubhransu S. Prusty 	}
109479f4e922SSubhransu S. Prusty 
109579f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1096754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1097754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1098754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
109979f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1100754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1101c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1102c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1103754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1104754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
110579f4e922SSubhransu S. Prusty 			if (ret < 0)
110679f4e922SSubhransu S. Prusty 				return ret;
11070324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
110879f4e922SSubhransu S. Prusty 			i++;
110979f4e922SSubhransu S. Prusty 		}
1110754695f9SJeeja KP 	}
111179f4e922SSubhransu S. Prusty 
111279f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
111379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1114754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1115754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1116754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
11173787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1118754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
111979f4e922SSubhransu S. Prusty 						widget_name);
112079f4e922SSubhransu S. Prusty 			if (ret < 0)
112179f4e922SSubhransu S. Prusty 				return ret;
112279f4e922SSubhransu S. Prusty 			i++;
112379f4e922SSubhransu S. Prusty 
112479f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
112579f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
112679f4e922SSubhransu S. Prusty 
112779f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
112879f4e922SSubhransu S. Prusty 			num_routes++;
112979f4e922SSubhransu S. Prusty 		}
1130754695f9SJeeja KP 	}
113179f4e922SSubhransu S. Prusty 
113279f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
113379f4e922SSubhransu S. Prusty 							GFP_KERNEL);
113479f4e922SSubhransu S. Prusty 	if (!route)
113579f4e922SSubhransu S. Prusty 		return -ENOMEM;
113679f4e922SSubhransu S. Prusty 
113779f4e922SSubhransu S. Prusty 	i = 0;
113879f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
113979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1140754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
114179f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1142754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1143754695f9SJeeja KP 						hdmi->num_pin;
114479f4e922SSubhransu S. Prusty 
114579f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
114679f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
114779f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
114879f4e922SSubhransu S. Prusty 			i++;
1149754695f9SJeeja KP 		}
115079f4e922SSubhransu S. Prusty 	}
115179f4e922SSubhransu S. Prusty 
11523787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
115379f4e922SSubhransu S. Prusty 
115479f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1155754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
115679f4e922SSubhransu S. Prusty 
115779f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
115879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
115979f4e922SSubhransu S. Prusty 
116079f4e922SSubhransu S. Prusty 	return 0;
116179f4e922SSubhransu S. Prusty 
116218382eadSSubhransu S. Prusty }
116318382eadSSubhransu S. Prusty 
11643787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
116518382eadSSubhransu S. Prusty {
11663787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1167754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
116815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1169148569fdSSubhransu S. Prusty 	int dai_id = 0;
117018382eadSSubhransu S. Prusty 
1171148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
117215b91447SSubhransu S. Prusty 		return -EINVAL;
117318382eadSSubhransu S. Prusty 
1174148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1175148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1176148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
117715b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
117818382eadSSubhransu S. Prusty 
1179148569fdSSubhransu S. Prusty 		dai_id++;
1180148569fdSSubhransu S. Prusty 
1181148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11823787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1183148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1184148569fdSSubhransu S. Prusty 			break;
1185148569fdSSubhransu S. Prusty 		}
1186148569fdSSubhransu S. Prusty 	}
118718382eadSSubhransu S. Prusty 
118815b91447SSubhransu S. Prusty 	return 0;
118915b91447SSubhransu S. Prusty }
119015b91447SSubhransu S. Prusty 
11913787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
119215b91447SSubhransu S. Prusty {
11933787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
119415b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11954a3478deSJeeja KP 	char name[NAME_SIZE];
119615b91447SSubhransu S. Prusty 
1197c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
119815b91447SSubhransu S. Prusty 	if (!cvt)
119915b91447SSubhransu S. Prusty 		return -ENOMEM;
120015b91447SSubhransu S. Prusty 
120115b91447SSubhransu S. Prusty 	cvt->nid = nid;
12024a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1203c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1204c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1205c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
120615b91447SSubhransu S. Prusty 
120715b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
120815b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
120915b91447SSubhransu S. Prusty 
12103787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
121115b91447SSubhransu S. Prusty }
121215b91447SSubhransu S. Prusty 
12133787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1214754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1215b7756edeSSubhransu S. Prusty {
1216f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1217f6fa11a3SSandeep Tayal 
1218754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1219f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1220f6fa11a3SSandeep Tayal 
1221f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
12223787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1223f6fa11a3SSandeep Tayal 		return -EINVAL;
1224b7756edeSSubhransu S. Prusty 	}
1225b7756edeSSubhransu S. Prusty 
1226754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1227f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1228f6fa11a3SSandeep Tayal 
1229f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
12303787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1231f6fa11a3SSandeep Tayal 		return -EINVAL;
1232f6fa11a3SSandeep Tayal 	}
1233f6fa11a3SSandeep Tayal 
1234754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1235f6fa11a3SSandeep Tayal 
1236f6fa11a3SSandeep Tayal 	return 0;
1237f6fa11a3SSandeep Tayal }
1238f6fa11a3SSandeep Tayal 
1239754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1240754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1241b8a54545SSubhransu S. Prusty {
12423787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12433787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12444a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1245754695f9SJeeja KP 	int size = 0;
12462acd8309SJeeja KP 	int port_id = -1;
1247754695f9SJeeja KP 
1248754695f9SJeeja KP 	if (!hdmi)
1249754695f9SJeeja KP 		return;
12504a3478deSJeeja KP 
12512acd8309SJeeja KP 	/*
12522acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12532acd8309SJeeja KP 	 * to be -1.
12542acd8309SJeeja KP 	 */
12554a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1256754695f9SJeeja KP 	port->eld.monitor_present = false;
1257f6fa11a3SSandeep Tayal 
12582acd8309SJeeja KP 	if (pin->mst_capable)
12592acd8309SJeeja KP 		port_id = port->id;
12602acd8309SJeeja KP 
12613787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1262754695f9SJeeja KP 				&port->eld.monitor_present,
1263754695f9SJeeja KP 				port->eld.eld_buffer,
1264f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1265f6fa11a3SSandeep Tayal 
1266f6fa11a3SSandeep Tayal 	if (size > 0) {
1267f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12683787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1269f6fa11a3SSandeep Tayal 			size = -EINVAL;
1270f6fa11a3SSandeep Tayal 	}
1271f6fa11a3SSandeep Tayal 
1272f6fa11a3SSandeep Tayal 	if (size > 0) {
1273754695f9SJeeja KP 		port->eld.eld_valid = true;
1274754695f9SJeeja KP 		port->eld.eld_size = size;
1275f6fa11a3SSandeep Tayal 	} else {
1276754695f9SJeeja KP 		port->eld.eld_valid = false;
1277754695f9SJeeja KP 		port->eld.eld_size = 0;
1278f6fa11a3SSandeep Tayal 	}
1279b8a54545SSubhransu S. Prusty 
12803787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12814a3478deSJeeja KP 
1282754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1283b8a54545SSubhransu S. Prusty 
12843787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1285754695f9SJeeja KP 						__func__, pin->nid, port->id);
12864a3478deSJeeja KP 
12874a3478deSJeeja KP 		/*
12884a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12894a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12904a3478deSJeeja KP 		 * control select.
12914a3478deSJeeja KP 		 */
1292e0e5d3e5SJeeja KP 		if (pcm)
1293e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12944a3478deSJeeja KP 
12954a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1296f6fa11a3SSandeep Tayal 		return;
1297b8a54545SSubhransu S. Prusty 	}
1298b8a54545SSubhransu S. Prusty 
1299754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1300e0e5d3e5SJeeja KP 		if (pcm)
1301e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
13024a3478deSJeeja KP 
1303f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1304754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1305754695f9SJeeja KP 
1306754695f9SJeeja KP 	}
1307754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
13084a3478deSJeeja KP }
13094a3478deSJeeja KP 
1310c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1311754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1312754695f9SJeeja KP {
1313754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1314754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1315754695f9SJeeja KP 	int i;
1316754695f9SJeeja KP 
1317754695f9SJeeja KP 	/*
1318754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1319754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1320754695f9SJeeja KP 	 * implemented.
1321754695f9SJeeja KP 	 */
1322754695f9SJeeja KP 
1323c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1324754695f9SJeeja KP 	if (!ports)
1325754695f9SJeeja KP 		return -ENOMEM;
1326754695f9SJeeja KP 
1327754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1328754695f9SJeeja KP 		ports[i].id = i;
1329754695f9SJeeja KP 		ports[i].pin = pin;
1330754695f9SJeeja KP 	}
1331754695f9SJeeja KP 	pin->ports = ports;
1332754695f9SJeeja KP 	pin->num_ports = max_ports;
1333754695f9SJeeja KP 	return 0;
1334b8a54545SSubhransu S. Prusty }
1335b8a54545SSubhransu S. Prusty 
13363787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
133715b91447SSubhransu S. Prusty {
13383787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
133915b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1340754695f9SJeeja KP 	int ret;
134115b91447SSubhransu S. Prusty 
1342c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
134315b91447SSubhransu S. Prusty 	if (!pin)
134415b91447SSubhransu S. Prusty 		return -ENOMEM;
134515b91447SSubhransu S. Prusty 
134615b91447SSubhransu S. Prusty 	pin->nid = nid;
13472acd8309SJeeja KP 	pin->mst_capable = false;
13483787a398SRakesh Ughreja 	pin->hdev = hdev;
1349c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1350754695f9SJeeja KP 	if (ret < 0)
1351754695f9SJeeja KP 		return ret;
135215b91447SSubhransu S. Prusty 
135315b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
135415b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1355754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1356b8a54545SSubhransu S. Prusty 
135715b91447SSubhransu S. Prusty 	return 0;
135818382eadSSubhransu S. Prusty }
135918382eadSSubhransu S. Prusty 
1360019033c8SBard liao #define INTEL_VENDOR_NID_0x2 0x02
1361019033c8SBard liao #define INTEL_VENDOR_NID_0x8 0x08
1362019033c8SBard liao #define INTEL_VENDOR_NID_0xb 0x0b
1363211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1364211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1365211caab7SSubhransu S. Prusty #define INTEL_EN_DP12		0x02 /* enable DP 1.2 features */
1366211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1367211caab7SSubhransu S. Prusty 
1368f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1369211caab7SSubhransu S. Prusty {
1370211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1371f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13725622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1373211caab7SSubhransu S. Prusty 
1374f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1375211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1376211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1377211caab7SSubhransu S. Prusty 		return;
1378211caab7SSubhransu S. Prusty 
1379211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1380f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1381211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1382211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1383211caab7SSubhransu S. Prusty 		return;
1384211caab7SSubhransu S. Prusty }
1385211caab7SSubhransu S. Prusty 
1386f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1387211caab7SSubhransu S. Prusty {
1388211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1389f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13905622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1391211caab7SSubhransu S. Prusty 
1392f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1393211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1394211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1395211caab7SSubhransu S. Prusty 		return;
1396211caab7SSubhransu S. Prusty 
1397211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1398211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1399f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1400211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1401211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1402211caab7SSubhransu S. Prusty 		return;
1403211caab7SSubhransu S. Prusty 
1404211caab7SSubhransu S. Prusty }
1405211caab7SSubhransu S. Prusty 
140661b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
140717a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
140817a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
140917a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1410c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
141117a42c45SSubhransu S. Prusty };
141217a42c45SSubhransu S. Prusty 
141317a42c45SSubhransu S. Prusty /*
141417a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
141517a42c45SSubhransu S. Prusty  * based on the number of converter queried.
141617a42c45SSubhransu S. Prusty  */
1417f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
141817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
141917a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
142017a42c45SSubhransu S. Prusty {
142117a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
142217a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
142317a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
142417a42c45SSubhransu S. Prusty 	int i = 0;
142517a42c45SSubhransu S. Prusty 	u32 rates, bps;
142617a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
142717a42c45SSubhransu S. Prusty 	u64 formats;
142817a42c45SSubhransu S. Prusty 	int ret;
142917a42c45SSubhransu S. Prusty 
1430f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
143117a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
143217a42c45SSubhransu S. Prusty 			GFP_KERNEL);
143317a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
143417a42c45SSubhransu S. Prusty 		return -ENOMEM;
143517a42c45SSubhransu S. Prusty 
143617a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1437f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
143817a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
143917a42c45SSubhransu S. Prusty 		if (ret)
144017a42c45SSubhransu S. Prusty 			return ret;
144117a42c45SSubhransu S. Prusty 
14423b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
14433b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
14443b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
14453b857472SYong Zhi 		if (!rates)
14463b857472SYong Zhi 			return -EINVAL;
14473b857472SYong Zhi 
144817a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1449f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
145017a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
145117a42c45SSubhransu S. Prusty 
145217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
145317a42c45SSubhransu S. Prusty 			return -ENOMEM;
145417a42c45SSubhransu S. Prusty 
145517a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
145617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1457f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
145817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
145917a42c45SSubhransu S. Prusty 			return -ENOMEM;
146017a42c45SSubhransu S. Prusty 
146117a42c45SSubhransu S. Prusty 		/*
146217a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
146317a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
146417a42c45SSubhransu S. Prusty 		 */
146517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
146617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
146717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
146817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
146917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
147017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
147166d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
147217a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
147317a42c45SSubhransu S. Prusty 		i++;
147417a42c45SSubhransu S. Prusty 	}
147517a42c45SSubhransu S. Prusty 
147617a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14771e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
147817a42c45SSubhransu S. Prusty 
147917a42c45SSubhransu S. Prusty 	return 0;
148017a42c45SSubhransu S. Prusty }
148117a42c45SSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty /*
148318382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
148418382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
148518382eadSSubhransu S. Prusty  */
14863787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
148717a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
148818382eadSSubhransu S. Prusty {
148918382eadSSubhransu S. Prusty 	hda_nid_t nid;
14903c83ac23SSudip Mukherjee 	int i, num_nodes;
14913787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
149215b91447SSubhransu S. Prusty 	int ret;
149318382eadSSubhransu S. Prusty 
1494f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1495f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1496211caab7SSubhransu S. Prusty 
1497f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1498541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1499f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
150018382eadSSubhransu S. Prusty 		return -EINVAL;
150118382eadSSubhransu S. Prusty 	}
150218382eadSSubhransu S. Prusty 
150345a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
150418382eadSSubhransu S. Prusty 		unsigned int caps;
150518382eadSSubhransu S. Prusty 		unsigned int type;
150618382eadSSubhransu S. Prusty 
1507f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
150818382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
150918382eadSSubhransu S. Prusty 
151018382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
151118382eadSSubhransu S. Prusty 			continue;
151218382eadSSubhransu S. Prusty 
151318382eadSSubhransu S. Prusty 		switch (type) {
151418382eadSSubhransu S. Prusty 
151518382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
15163787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
151715b91447SSubhransu S. Prusty 			if (ret < 0)
1518c7ba4e53SPierre-Louis Bossart 				return ret;
151918382eadSSubhransu S. Prusty 			break;
152018382eadSSubhransu S. Prusty 
152118382eadSSubhransu S. Prusty 		case AC_WID_PIN:
15223787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
152315b91447SSubhransu S. Prusty 			if (ret < 0)
1524c7ba4e53SPierre-Louis Bossart 				return ret;
152518382eadSSubhransu S. Prusty 			break;
152618382eadSSubhransu S. Prusty 		}
152718382eadSSubhransu S. Prusty 	}
152818382eadSSubhransu S. Prusty 
15291c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
15301c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1531c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1532c7ba4e53SPierre-Louis Bossart 		return ret;
15331c0a7de2SSubhransu S. Prusty 	}
153418382eadSSubhransu S. Prusty 
1535f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
153617a42c45SSubhransu S. Prusty 	if (ret) {
1537f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
153817a42c45SSubhransu S. Prusty 			ret);
1539c7ba4e53SPierre-Louis Bossart 		return ret;
154017a42c45SSubhransu S. Prusty 	}
154117a42c45SSubhransu S. Prusty 
154217a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15433787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15441c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1545c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1546c7ba4e53SPierre-Louis Bossart 			ret);
15471c0a7de2SSubhransu S. Prusty 	return ret;
154818382eadSSubhransu S. Prusty }
154918382eadSSubhransu S. Prusty 
1550a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1551a57942bfSTakashi Iwai {
1552019033c8SBard liao 	struct hdac_device *hdev = aptr;
1553019033c8SBard liao 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1554019033c8SBard liao 	const int *map = hdmi->drv_data->port_map;
1555019033c8SBard liao 	int i;
1556019033c8SBard liao 
1557019033c8SBard liao 	if (!hdmi->drv_data->port_num)
1558a57942bfSTakashi Iwai 		return pin - 4; /* map NID 0x05 -> port #1 */
1559019033c8SBard liao 
1560019033c8SBard liao 	/*
1561019033c8SBard liao 	 * looking for the pin number in the mapping table and return
1562019033c8SBard liao 	 * the index which indicate the port number
1563019033c8SBard liao 	 */
1564019033c8SBard liao 	for (i = 0; i < hdmi->drv_data->port_num; i++) {
1565019033c8SBard liao 		if (pin == map[i])
1566019033c8SBard liao 			return i + 1;
1567019033c8SBard liao 	}
1568019033c8SBard liao 
1569019033c8SBard liao 	/* return -1 if pin number exceeds our expectation */
1570019033c8SBard liao 	dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1571019033c8SBard liao 	return -1;
1572a57942bfSTakashi Iwai }
1573a57942bfSTakashi Iwai 
1574f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1575b8a54545SSubhransu S. Prusty {
15763787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1578754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1579754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15803787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15812acd8309SJeeja KP 	int i;
1582019033c8SBard liao 	hda_nid_t pin_nid;
1583b8a54545SSubhransu S. Prusty 
1584019033c8SBard liao 	if (!hdmi->drv_data->port_num) {
1585019033c8SBard liao 		/* for legacy platforms */
1586019033c8SBard liao 		pin_nid = port + 0x04;
1587019033c8SBard liao 	} else if (port < hdmi->drv_data->port_num) {
1588019033c8SBard liao 		/* get pin number from the pin2port mapping table */
1589019033c8SBard liao 		pin_nid = hdmi->drv_data->port_map[port - 1];
1590019033c8SBard liao 	} else {
1591019033c8SBard liao 		dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1592019033c8SBard liao 		return;
1593019033c8SBard liao 	}
1594b8a54545SSubhransu S. Prusty 
15953787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1596754695f9SJeeja KP 							pin_nid, pipe);
1597b8a54545SSubhransu S. Prusty 
1598b8a54545SSubhransu S. Prusty 	/*
1599b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1600b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1601b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1602b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1603b8a54545SSubhransu S. Prusty 	 */
160445101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1605b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1606b8a54545SSubhransu S. Prusty 		return;
1607b8a54545SSubhransu S. Prusty 
16083787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1609b8a54545SSubhransu S. Prusty 		return;
1610b8a54545SSubhransu S. Prusty 
1611b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1612754695f9SJeeja KP 		if (pin->nid != pin_nid)
1613754695f9SJeeja KP 			continue;
1614754695f9SJeeja KP 
1615754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1616754695f9SJeeja KP 		if (pipe == -1) {
16172acd8309SJeeja KP 			pin->mst_capable = false;
1618754695f9SJeeja KP 			/* if not MST, default is port[0] */
1619754695f9SJeeja KP 			hport = &pin->ports[0];
16202acd8309SJeeja KP 		} else {
16212acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
16222acd8309SJeeja KP 				pin->mst_capable = true;
16232acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
16242acd8309SJeeja KP 					hport = &pin->ports[i];
162504c8f2bfSJeeja KP 					break;
16262acd8309SJeeja KP 				}
1627b8a54545SSubhransu S. Prusty 			}
1628b8a54545SSubhransu S. Prusty 		}
1629b8a54545SSubhransu S. Prusty 
163004c8f2bfSJeeja KP 		if (hport)
1631754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1632754695f9SJeeja KP 	}
1633754695f9SJeeja KP 
163404c8f2bfSJeeja KP }
163504c8f2bfSJeeja KP 
1636ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1637a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1638b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1639b8a54545SSubhransu S. Prusty };
1640b8a54545SSubhransu S. Prusty 
16412889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
16422889099eSSubhransu S. Prusty 						int device)
16432889099eSSubhransu S. Prusty {
16442889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
16452889099eSSubhransu S. Prusty 
1646bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
16472889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16482889099eSSubhransu S. Prusty 			return rtd->pcm;
16492889099eSSubhransu S. Prusty 	}
16502889099eSSubhransu S. Prusty 
16512889099eSSubhransu S. Prusty 	return NULL;
16522889099eSSubhransu S. Prusty }
16532889099eSSubhransu S. Prusty 
16540324e51bSJeeja KP /* create jack pin kcontrols */
16550324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16563787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16570324e51bSJeeja KP {
16580324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16590324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16600324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16610324e51bSJeeja KP 	char *name;
16620324e51bSJeeja KP 	int i = 0, j;
16633787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16643787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16650324e51bSJeeja KP 
166645101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16670324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16680324e51bSJeeja KP 
16690324e51bSJeeja KP 	if (!kc)
16700324e51bSJeeja KP 		return -ENOMEM;
16710324e51bSJeeja KP 
16720324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16730324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16740324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16750324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
167645101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16770324e51bSJeeja KP 			if (!name)
16780324e51bSJeeja KP 				return -ENOMEM;
16790324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
168045101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16810324e51bSJeeja KP 							GFP_KERNEL);
16820324e51bSJeeja KP 			if (!kc[i].name)
16830324e51bSJeeja KP 				return -ENOMEM;
16840324e51bSJeeja KP 
16850324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16860324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16870324e51bSJeeja KP 			kc[i].access = 0;
16880324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16890324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16900324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16910324e51bSJeeja KP 			i++;
16920324e51bSJeeja KP 		}
16930324e51bSJeeja KP 	}
16940324e51bSJeeja KP 
16950324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16960324e51bSJeeja KP }
16970324e51bSJeeja KP 
169845101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16990324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
17000324e51bSJeeja KP {
17013787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17023787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17030324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
17040324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
17050324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
17060324e51bSJeeja KP 	char w_name[NAME_SIZE];
17070324e51bSJeeja KP 	int i = 0, j, ret;
17080324e51bSJeeja KP 
17090324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
17100324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
17110324e51bSJeeja KP 
17120324e51bSJeeja KP 	if (!widgets)
17130324e51bSJeeja KP 		return -ENOMEM;
17140324e51bSJeeja KP 
17150324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
17160324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
17170324e51bSJeeja KP 	if (!route)
17180324e51bSJeeja KP 		return -ENOMEM;
17190324e51bSJeeja KP 
17200324e51bSJeeja KP 	/* create Jack DAPM widget */
17210324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17220324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
17230324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
17240324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
17250324e51bSJeeja KP 
17260324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
17270324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
17280324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
17290324e51bSJeeja KP 			if (ret < 0)
17300324e51bSJeeja KP 				return ret;
17310324e51bSJeeja KP 
17320324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
17330324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
17340324e51bSJeeja KP 
17350324e51bSJeeja KP 			/* add to route from Jack widget to output */
17360324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
17370324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
17380324e51bSJeeja KP 
17390324e51bSJeeja KP 			i++;
17400324e51bSJeeja KP 		}
17410324e51bSJeeja KP 	}
17420324e51bSJeeja KP 
17430324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
17440324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
17450324e51bSJeeja KP 	if (ret < 0)
17460324e51bSJeeja KP 		return ret;
17470324e51bSJeeja KP 
17480324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17490324e51bSJeeja KP 	if (ret < 0)
17500324e51bSJeeja KP 		return ret;
17510324e51bSJeeja KP 
17520324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17530324e51bSJeeja KP 	if (ret < 0)
17540324e51bSJeeja KP 		return ret;
17550324e51bSJeeja KP 
17560324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17573787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17580324e51bSJeeja KP 
17590324e51bSJeeja KP 	if (ret < 0)
17600324e51bSJeeja KP 		return ret;
17610324e51bSJeeja KP 
17620324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17630324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17640324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17650324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17660324e51bSJeeja KP 						pin->ports[j].jack_pin);
17670324e51bSJeeja KP 	}
17680324e51bSJeeja KP 
17690324e51bSJeeja KP 	return 0;
17700324e51bSJeeja KP }
17710324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17720324e51bSJeeja KP 
177362490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
177462490016SJeeja KP 				struct snd_soc_jack *jack)
17754a3478deSJeeja KP {
177645101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17783787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17794a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17802889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17812889099eSSubhransu S. Prusty 	int err;
17824a3478deSJeeja KP 
17834a3478deSJeeja KP 	/*
17844a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17854a3478deSJeeja KP 	 * add to the pcm list
17864a3478deSJeeja KP 	 */
1787c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
17884a3478deSJeeja KP 	if (!pcm)
17894a3478deSJeeja KP 		return -ENOMEM;
17904a3478deSJeeja KP 	pcm->pcm_id = device;
17914a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1792e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
179362490016SJeeja KP 	pcm->jack = jack;
1794ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1795e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17962889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17972889099eSSubhransu S. Prusty 	if (snd_pcm) {
17982889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17992889099eSSubhransu S. Prusty 		if (err < 0) {
18003787a398SRakesh Ughreja 			dev_err(&hdev->dev,
18012889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
18022889099eSSubhransu S. Prusty 				err, device);
18032889099eSSubhransu S. Prusty 			return err;
18042889099eSSubhransu S. Prusty 		}
18052889099eSSubhransu S. Prusty 	}
18062889099eSSubhransu S. Prusty 
18074a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
18084a3478deSJeeja KP 
180962490016SJeeja KP 	return 0;
18104a3478deSJeeja KP }
18114a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
18124a3478deSJeeja KP 
18133787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1814a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1815a9ce96bcSJeeja KP {
1816a9ce96bcSJeeja KP 	int i;
1817a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1818a9ce96bcSJeeja KP 
1819a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1820a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1821a9ce96bcSJeeja KP 
18223787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1823a9ce96bcSJeeja KP 				pin->mst_capable = false;
1824a9ce96bcSJeeja KP 			else
1825a9ce96bcSJeeja KP 				pin->mst_capable = true;
1826a9ce96bcSJeeja KP 		}
1827a9ce96bcSJeeja KP 
1828a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1829a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1830a9ce96bcSJeeja KP 				continue;
1831a9ce96bcSJeeja KP 
1832a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1833a9ce96bcSJeeja KP 		}
1834a9ce96bcSJeeja KP 	}
1835a9ce96bcSJeeja KP }
1836a9ce96bcSJeeja KP 
183745101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
183818382eadSSubhransu S. Prusty {
18393787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18403787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
184118382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
184245101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1843b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1844a9ce96bcSJeeja KP 	int ret;
184518382eadSSubhransu S. Prusty 
18463787a398SRakesh Ughreja 	hdmi->component = component;
184718382eadSSubhransu S. Prusty 
1848b2047e99SVinod Koul 	/*
1849b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1850b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1851b2047e99SVinod Koul 	 */
185276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1853500e06b9SVinod Koul 	if (!hlink) {
18543787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1855500e06b9SVinod Koul 		return -EIO;
1856500e06b9SVinod Koul 	}
1857500e06b9SVinod Koul 
185876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1859b2047e99SVinod Koul 
186079f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
186179f4e922SSubhransu S. Prusty 	if (ret < 0)
186279f4e922SSubhransu S. Prusty 		return ret;
186318382eadSSubhransu S. Prusty 
18643787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1865a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1866b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18673787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1868b8a54545SSubhransu S. Prusty 		return ret;
1869b8a54545SSubhransu S. Prusty 	}
1870b8a54545SSubhransu S. Prusty 
18713787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
187218382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18733787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
187418382eadSSubhransu S. Prusty 
1875e342ac08SSubhransu S. Prusty 	/*
187601c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
187701c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
187801c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
187901c83276SLibin Yang 	 * domain in display power will be always turned on before operating
188001c83276SLibin Yang 	 * on the HDMI audio codec registers.
188101c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
188201c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
188301c83276SLibin Yang 	 */
188401c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
188501c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
188601c83276SLibin Yang 	/*
1887e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1888e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1889e342ac08SSubhransu S. Prusty 	 */
18903787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18913787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18923787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1893e342ac08SSubhransu S. Prusty 
1894e342ac08SSubhransu S. Prusty 	return 0;
1895e342ac08SSubhransu S. Prusty }
1896e342ac08SSubhransu S. Prusty 
189745101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1898e342ac08SSubhransu S. Prusty {
18993787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
19003787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
1901e342ac08SSubhransu S. Prusty 
19023787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
190318382eadSSubhransu S. Prusty }
190418382eadSSubhransu S. Prusty 
1905687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
1906687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
1907571d5078SJeeja KP {
19083787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
19093787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1910687ae9e2STakashi Iwai 	int ret;
19111b377ccdSSubhransu S. Prusty 
1912687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
1913687ae9e2STakashi Iwai 	if (ret < 0)
1914687ae9e2STakashi Iwai 		return ret;
1915571d5078SJeeja KP 	/*
1916571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1917571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1918a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1919a9ce96bcSJeeja KP 	 * already set pin caps.
1920687ae9e2STakashi Iwai 	 *
1921687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1922687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
1923687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
1924571d5078SJeeja KP 	 */
19253787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1926687ae9e2STakashi Iwai 	return 0;
1927571d5078SJeeja KP }
1928571d5078SJeeja KP #else
1929687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
1930571d5078SJeeja KP #endif
1931571d5078SJeeja KP 
193245101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
193318382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1934e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
193545101122SKuninori Morimoto 	.use_pmdown_time	= 1,
193645101122SKuninori Morimoto 	.endianness		= 1,
193745101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
193818382eadSSubhransu S. Prusty };
193918382eadSSubhransu S. Prusty 
1940f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19412889099eSSubhransu S. Prusty 					unsigned char *chmap)
19422889099eSSubhransu S. Prusty {
1943f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19442889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19452889099eSSubhransu S. Prusty 
1946ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19472889099eSSubhransu S. Prusty }
19482889099eSSubhransu S. Prusty 
1949f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19502889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19512889099eSSubhransu S. Prusty {
1952f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19532889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1954e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1955e0e5d3e5SJeeja KP 
1956eb50fa17SSubhransu S. Prusty 	if (!pcm)
1957eb50fa17SSubhransu S. Prusty 		return;
1958eb50fa17SSubhransu S. Prusty 
1959e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1960e0e5d3e5SJeeja KP 		return;
19612889099eSSubhransu S. Prusty 
1962ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1963ab1eea19SJeeja KP 	pcm->chmap_set = true;
1964ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1965e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19662889099eSSubhransu S. Prusty 		if (prepared)
19673787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1968ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19692889099eSSubhransu S. Prusty }
19702889099eSSubhransu S. Prusty 
1971f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19722889099eSSubhransu S. Prusty {
1973f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19742889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19752889099eSSubhransu S. Prusty 
1976eb50fa17SSubhransu S. Prusty 	if (!pcm)
1977eb50fa17SSubhransu S. Prusty 		return false;
1978eb50fa17SSubhransu S. Prusty 
1979e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1980e0e5d3e5SJeeja KP 		return false;
1981e0e5d3e5SJeeja KP 
1982e0e5d3e5SJeeja KP 	return true;
19832889099eSSubhransu S. Prusty }
19842889099eSSubhransu S. Prusty 
1985f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19862889099eSSubhransu S. Prusty {
1987f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19882889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1989e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1990e0e5d3e5SJeeja KP 
1991eb50fa17SSubhransu S. Prusty 	if (!pcm)
1992eb50fa17SSubhransu S. Prusty 		return 0;
1993eb50fa17SSubhransu S. Prusty 
1994e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1995e0e5d3e5SJeeja KP 		return 0;
1996e0e5d3e5SJeeja KP 
1997e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1998e0e5d3e5SJeeja KP 
1999754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
20002889099eSSubhransu S. Prusty 		return 0;
20012889099eSSubhransu S. Prusty 
2002754695f9SJeeja KP 	return port->eld.info.spk_alloc;
20032889099eSSubhransu S. Prusty }
20042889099eSSubhransu S. Prusty 
2005019033c8SBard liao static struct hdac_hdmi_drv_data intel_icl_drv_data  = {
2006019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x2,
2007019033c8SBard liao 	.port_map = icl_pin2port_map,
2008019033c8SBard liao 	.port_num = ARRAY_SIZE(icl_pin2port_map),
2009019033c8SBard liao };
2010019033c8SBard liao 
20115622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
2012019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0xb,
20135622bc95SPradeep Tewani };
20145622bc95SPradeep Tewani 
20155622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
2016019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x8,
20175622bc95SPradeep Tewani };
20185622bc95SPradeep Tewani 
20193787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
202018382eadSSubhransu S. Prusty {
20213787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi_priv = NULL;
202217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
2023b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
202417a42c45SSubhransu S. Prusty 	int num_dais = 0;
202518382eadSSubhransu S. Prusty 	int ret = 0;
2026f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2027f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
202818382eadSSubhransu S. Prusty 
2029b2047e99SVinod Koul 	/* hold the ref while we probe */
203076f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2031500e06b9SVinod Koul 	if (!hlink) {
20323787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
2033500e06b9SVinod Koul 		return -EIO;
2034500e06b9SVinod Koul 	}
2035500e06b9SVinod Koul 
203676f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2037b2047e99SVinod Koul 
2038f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
203918382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
204018382eadSSubhransu S. Prusty 		return -ENOMEM;
204118382eadSSubhransu S. Prusty 
2042f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20432889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20442889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20452889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20462889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20473787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
204818382eadSSubhransu S. Prusty 
2049eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2050eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2051eb50fa17SSubhransu S. Prusty 
20525622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20535622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20545622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20555622bc95SPradeep Tewani 	else
20565622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20575622bc95SPradeep Tewani 
20583787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
205918382eadSSubhransu S. Prusty 
206015b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
206115b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20624a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20634a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
206415b91447SSubhransu S. Prusty 
2065aeaccef0SRamesh Babu 	/*
2066aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2067aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2068aeaccef0SRamesh Babu 	 */
20694f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
20704f799e73STakashi Iwai 
20713787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
207217a42c45SSubhransu S. Prusty 	if (ret < 0) {
2073f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
207417a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
207518382eadSSubhransu S. Prusty 		return ret;
207617a42c45SSubhransu S. Prusty 	}
20770fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
207818382eadSSubhransu S. Prusty 
207918382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
208045101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
208117a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2082b2047e99SVinod Koul 
208376f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2084b2047e99SVinod Koul 
2085b2047e99SVinod Koul 	return ret;
208618382eadSSubhransu S. Prusty }
208718382eadSSubhransu S. Prusty 
20883787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
208918382eadSSubhransu S. Prusty {
209077a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
209177a49672STakashi Iwai 
209218382eadSSubhransu S. Prusty 	return 0;
209318382eadSSubhransu S. Prusty }
209418382eadSSubhransu S. Prusty 
2095e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2096e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2097e342ac08SSubhransu S. Prusty {
20983787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2099f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2100b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2101e342ac08SSubhransu S. Prusty 
2102e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2103e342ac08SSubhransu S. Prusty 
210407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
210507f083abSSubhransu S. Prusty 	if (!bus)
210607f083abSSubhransu S. Prusty 		return 0;
210707f083abSSubhransu S. Prusty 
21081b377ccdSSubhransu S. Prusty 	/*
21091b377ccdSSubhransu S. Prusty 	 * Power down afg.
21101b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21111b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21121b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21131b377ccdSSubhransu S. Prusty 	 * to read the state.
21141b377ccdSSubhransu S. Prusty 	 */
2115f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21161b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
211707f083abSSubhransu S. Prusty 
211876f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2119500e06b9SVinod Koul 	if (!hlink) {
2120500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2121500e06b9SVinod Koul 		return -EIO;
2122500e06b9SVinod Koul 	}
2123500e06b9SVinod Koul 
212476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2125b2047e99SVinod Koul 
21264f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
21274c10473dSPierre-Louis Bossart 
21284f799e73STakashi Iwai 	return 0;
2129e342ac08SSubhransu S. Prusty }
2130e342ac08SSubhransu S. Prusty 
2131e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2132e342ac08SSubhransu S. Prusty {
21333787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2134f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2135b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2136e342ac08SSubhransu S. Prusty 
2137e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2138e342ac08SSubhransu S. Prusty 
213907f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
214007f083abSSubhransu S. Prusty 	if (!bus)
214107f083abSSubhransu S. Prusty 		return 0;
214207f083abSSubhransu S. Prusty 
214376f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2144500e06b9SVinod Koul 	if (!hlink) {
2145500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2146500e06b9SVinod Koul 		return -EIO;
2147500e06b9SVinod Koul 	}
2148500e06b9SVinod Koul 
214976f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2150b2047e99SVinod Koul 
21514f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
215207f083abSSubhransu S. Prusty 
21533787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21543787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2155ab85f5b3SSubhransu S. Prusty 
2156e342ac08SSubhransu S. Prusty 	/* Power up afg */
2157f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21581b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2159e342ac08SSubhransu S. Prusty 
2160e342ac08SSubhransu S. Prusty 	return 0;
2161e342ac08SSubhransu S. Prusty }
2162e342ac08SSubhransu S. Prusty #else
2163e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2164e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2165e342ac08SSubhransu S. Prusty #endif
2166e342ac08SSubhransu S. Prusty 
2167e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2168e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2169687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2170e342ac08SSubhransu S. Prusty };
2171e342ac08SSubhransu S. Prusty 
217218382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
217318382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2174e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2175cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21765fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21775fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21785622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21795622bc95SPradeep Tewani 						   &intel_glk_drv_data),
2180019033c8SBard liao 	HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2181019033c8SBard liao 						   &intel_icl_drv_data),
218218382eadSSubhransu S. Prusty 	{}
218318382eadSSubhransu S. Prusty };
218418382eadSSubhransu S. Prusty 
218518382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
218618382eadSSubhransu S. Prusty 
2187e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
218818382eadSSubhransu S. Prusty 	.driver = {
218918382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2190e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
219118382eadSSubhransu S. Prusty 	},
219218382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
219318382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
219418382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
219518382eadSSubhransu S. Prusty };
219618382eadSSubhransu S. Prusty 
219718382eadSSubhransu S. Prusty static int __init hdmi_init(void)
219818382eadSSubhransu S. Prusty {
219918382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
220018382eadSSubhransu S. Prusty }
220118382eadSSubhransu S. Prusty 
220218382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
220318382eadSSubhransu S. Prusty {
220418382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
220518382eadSSubhransu S. Prusty }
220618382eadSSubhransu S. Prusty 
220718382eadSSubhransu S. Prusty module_init(hdmi_init);
220818382eadSSubhransu S. Prusty module_exit(hdmi_exit);
220918382eadSSubhransu S. Prusty 
221018382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
221118382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
221218382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
221318382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2214