xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 332ccf00)
18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
218382eadSSubhransu S. Prusty /*
318382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
418382eadSSubhransu S. Prusty  *
518382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
618382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
718382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
818382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
918382eadSSubhransu S. Prusty  *
1018382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1118382eadSSubhransu S. Prusty  */
1218382eadSSubhransu S. Prusty #include <linux/init.h>
1318382eadSSubhransu S. Prusty #include <linux/delay.h>
1418382eadSSubhransu S. Prusty #include <linux/module.h>
1518382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
16a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
172428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
1818382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
194a3478deSJeeja KP #include <sound/jack.h>
2018382eadSSubhransu S. Prusty #include <sound/soc.h>
2118382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
2207f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
232428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
24bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
2518382eadSSubhransu S. Prusty #include "../../hda/local.h"
264a3478deSJeeja KP #include "hdac_hdmi.h"
2718382eadSSubhransu S. Prusty 
2817a42c45SSubhransu S. Prusty #define NAME_SIZE	32
2917a42c45SSubhransu S. Prusty 
30b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
31b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
3218382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
33b0362adbSSubhransu S. Prusty 
3418382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
3518382eadSSubhransu S. Prusty 
36148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
37754695f9SJeeja KP #define HDA_MAX_PORTS		3
38148569fdSSubhransu S. Prusty 
39b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
40b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
41b8a54545SSubhransu S. Prusty 
42f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
43f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
44f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
45f6fa11a3SSandeep Tayal 
4618382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
4718382eadSSubhransu S. Prusty 	unsigned int channels_min;
4818382eadSSubhransu S. Prusty 	unsigned int channels_max;
4918382eadSSubhransu S. Prusty 	u32 rates;
5018382eadSSubhransu S. Prusty 	u64 formats;
5118382eadSSubhransu S. Prusty 	unsigned int maxbps;
5218382eadSSubhransu S. Prusty };
5318382eadSSubhransu S. Prusty 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5515b91447SSubhransu S. Prusty 	struct list_head head;
5618382eadSSubhransu S. Prusty 	hda_nid_t nid;
574a3478deSJeeja KP 	const char *name;
5818382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
5918382eadSSubhransu S. Prusty };
6018382eadSSubhransu S. Prusty 
61b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
62b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
63b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
64b7756edeSSubhransu S. Prusty };
65b7756edeSSubhransu S. Prusty 
66b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
67b8a54545SSubhransu S. Prusty 	bool	monitor_present;
68b8a54545SSubhransu S. Prusty 	bool	eld_valid;
69b8a54545SSubhransu S. Prusty 	int	eld_size;
70b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
71b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
72b8a54545SSubhransu S. Prusty };
73b8a54545SSubhransu S. Prusty 
7418382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7515b91447SSubhransu S. Prusty 	struct list_head head;
7618382eadSSubhransu S. Prusty 	hda_nid_t nid;
772acd8309SJeeja KP 	bool mst_capable;
78754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
79754695f9SJeeja KP 	int num_ports;
803787a398SRakesh Ughreja 	struct hdac_device *hdev;
81754695f9SJeeja KP };
82754695f9SJeeja KP 
83754695f9SJeeja KP struct hdac_hdmi_port {
84e0e5d3e5SJeeja KP 	struct list_head head;
85754695f9SJeeja KP 	int id;
86754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
8718382eadSSubhransu S. Prusty 	int num_mux_nids;
8818382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
89b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
900324e51bSJeeja KP 	const char *jack_pin;
91332ccf00STakashi Iwai 	bool is_connect;
920324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
930324e51bSJeeja KP 	const char *output_pin;
94332ccf00STakashi Iwai 	struct work_struct dapm_work;
9518382eadSSubhransu S. Prusty };
9618382eadSSubhransu S. Prusty 
974a3478deSJeeja KP struct hdac_hdmi_pcm {
984a3478deSJeeja KP 	struct list_head head;
994a3478deSJeeja KP 	int pcm_id;
100e0e5d3e5SJeeja KP 	struct list_head port_list;
1014a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10262490016SJeeja KP 	struct snd_soc_jack *jack;
103c9bfb5d7SJeeja KP 	int stream_tag;
104c9bfb5d7SJeeja KP 	int channels;
105c9bfb5d7SJeeja KP 	int format;
106ab1eea19SJeeja KP 	bool chmap_set;
107ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
108ab1eea19SJeeja KP 	struct mutex lock;
109e0e5d3e5SJeeja KP 	int jack_event;
1104a3478deSJeeja KP };
1114a3478deSJeeja KP 
112754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11318382eadSSubhransu S. Prusty 	int dai_id;
114754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11618382eadSSubhransu S. Prusty };
11718382eadSSubhransu S. Prusty 
118019033c8SBard liao /*
119019033c8SBard liao  * pin to port mapping table where the value indicate the pin number and
120019033c8SBard liao  * the index indicate the port number with 1 base.
121019033c8SBard liao  */
122019033c8SBard liao static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
123019033c8SBard liao 
1245622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1255622bc95SPradeep Tewani 	unsigned int vendor_nid;
126019033c8SBard liao 	const int *port_map; /* pin to port mapping table */
127019033c8SBard liao 	int port_num;
1285622bc95SPradeep Tewani };
1295622bc95SPradeep Tewani 
13018382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1313787a398SRakesh Ughreja 	struct hdac_device *hdev;
1323787a398SRakesh Ughreja 	struct snd_soc_component *component;
1333787a398SRakesh Ughreja 	struct snd_card *card;
134754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
13515b91447SSubhransu S. Prusty 	struct list_head pin_list;
13615b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1374a3478deSJeeja KP 	struct list_head pcm_list;
13815b91447SSubhransu S. Prusty 	int num_pin;
13915b91447SSubhransu S. Prusty 	int num_cvt;
140754695f9SJeeja KP 	int num_ports;
1414a3478deSJeeja KP 	struct mutex pin_mutex;
142bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1435622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1441e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
14518382eadSSubhransu S. Prusty };
14618382eadSSubhransu S. Prusty 
1473787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
148b09b1c3bSUghreja, Rakesh A 
149c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
150c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
151c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
152c9bfb5d7SJeeja KP {
153c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1541de777feSJeeja KP 
155c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
156c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
157c9bfb5d7SJeeja KP 			break;
158c9bfb5d7SJeeja KP 	}
159c9bfb5d7SJeeja KP 
160c9bfb5d7SJeeja KP 	return pcm;
161c9bfb5d7SJeeja KP }
1621de777feSJeeja KP 
163e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
164e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
165e0e5d3e5SJeeja KP {
1663787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
167e0e5d3e5SJeeja KP 
168332ccf00STakashi Iwai 	port->is_connect = is_connect;
169e0e5d3e5SJeeja KP 	if (is_connect) {
170e0e5d3e5SJeeja KP 		/*
171e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
172e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
173e0e5d3e5SJeeja KP 		 * ports.
174e0e5d3e5SJeeja KP 		 */
175e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1763787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
177e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
178e0e5d3e5SJeeja KP 					pcm->pcm_id);
17962490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
18062490016SJeeja KP 						SND_JACK_AVOUT);
181e0e5d3e5SJeeja KP 		}
182e0e5d3e5SJeeja KP 		pcm->jack_event++;
183e0e5d3e5SJeeja KP 	} else {
184e0e5d3e5SJeeja KP 		/*
185e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
186e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
187e0e5d3e5SJeeja KP 		 * to multiple ports.
188e0e5d3e5SJeeja KP 		 */
189e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
19062490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
191e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
192e0e5d3e5SJeeja KP 			pcm->jack_event--;
193e0e5d3e5SJeeja KP 	}
194332ccf00STakashi Iwai }
1950324e51bSJeeja KP 
196332ccf00STakashi Iwai static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
197332ccf00STakashi Iwai {
198332ccf00STakashi Iwai 	if (port->is_connect)
199332ccf00STakashi Iwai 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
200332ccf00STakashi Iwai 	else
201332ccf00STakashi Iwai 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
2020324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
203e0e5d3e5SJeeja KP }
204e0e5d3e5SJeeja KP 
205332ccf00STakashi Iwai static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
206332ccf00STakashi Iwai {
207332ccf00STakashi Iwai 	struct hdac_hdmi_port *port;
208332ccf00STakashi Iwai 
209332ccf00STakashi Iwai 	port = container_of(work, struct hdac_hdmi_port, dapm_work);
210332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
211332ccf00STakashi Iwai }
212332ccf00STakashi Iwai 
213332ccf00STakashi Iwai static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
214332ccf00STakashi Iwai 		struct hdac_hdmi_port *port, bool is_connect)
215332ccf00STakashi Iwai {
216332ccf00STakashi Iwai 	hdac_hdmi_jack_report(pcm, port, is_connect);
217332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
218332ccf00STakashi Iwai }
219332ccf00STakashi Iwai 
220fc181b04SJeeja KP /* MST supported verbs */
221fc181b04SJeeja KP /*
222fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
223fc181b04SJeeja KP  */
2243787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
225fc181b04SJeeja KP {
226fc181b04SJeeja KP 	unsigned int caps;
227fc181b04SJeeja KP 	unsigned int type, param;
228fc181b04SJeeja KP 
2293787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
230fc181b04SJeeja KP 	type = get_wcaps_type(caps);
231fc181b04SJeeja KP 
232fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
233fc181b04SJeeja KP 		return 0;
234fc181b04SJeeja KP 
2353787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
236fc181b04SJeeja KP 	if (param == -1)
237fc181b04SJeeja KP 		return param;
238fc181b04SJeeja KP 
239fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
240fc181b04SJeeja KP }
241fc181b04SJeeja KP 
242fc181b04SJeeja KP /*
243fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
244fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
245fc181b04SJeeja KP  * is selected or MST is not supported.
246fc181b04SJeeja KP  */
2473787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
248fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
249fc181b04SJeeja KP {
2503787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
251fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
252fc181b04SJeeja KP }
253fc181b04SJeeja KP 
254fc181b04SJeeja KP /*
255fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
256fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
257fc181b04SJeeja KP  */
2583787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
259fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
260fc181b04SJeeja KP {
261fc181b04SJeeja KP 	int num_ports;
262fc181b04SJeeja KP 
263fc181b04SJeeja KP 	if (!port->pin->mst_capable)
264fc181b04SJeeja KP 		return 0;
265fc181b04SJeeja KP 
266fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2673787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
268fc181b04SJeeja KP 	if (num_ports < 0)
269fc181b04SJeeja KP 		return -EIO;
270fc181b04SJeeja KP 	/*
271fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
272fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
273fc181b04SJeeja KP 	 */
274fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
275fc181b04SJeeja KP 		return 0;
276fc181b04SJeeja KP 
2773787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
278fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
279fc181b04SJeeja KP 
2803787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
281fc181b04SJeeja KP 		return -EIO;
282fc181b04SJeeja KP 
2833787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
284fc181b04SJeeja KP 
285fc181b04SJeeja KP 	return 0;
286fc181b04SJeeja KP }
287fc181b04SJeeja KP 
2882889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2892889099eSSubhransu S. Prusty 						int pcm_idx)
2902889099eSSubhransu S. Prusty {
2912889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2922889099eSSubhransu S. Prusty 
2932889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2942889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2952889099eSSubhransu S. Prusty 			return pcm;
2962889099eSSubhransu S. Prusty 	}
2972889099eSSubhransu S. Prusty 
2982889099eSSubhransu S. Prusty 	return NULL;
2992889099eSSubhransu S. Prusty }
3002889099eSSubhransu S. Prusty 
3012428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
3022428bca3SSubhransu S. Prusty {
3032428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
3042428bca3SSubhransu S. Prusty }
3052428bca3SSubhransu S. Prusty 
3062428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
3072428bca3SSubhransu S. Prusty {
3082428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
3092428bca3SSubhransu S. Prusty }
3102428bca3SSubhransu S. Prusty 
3112428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
3122428bca3SSubhransu S. Prusty 						void *eld)
3132428bca3SSubhransu S. Prusty {
3142428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3152428bca3SSubhransu S. Prusty 	int i;
3162428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3172428bca3SSubhransu S. Prusty 
3182428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3192428bca3SSubhransu S. Prusty 	if (!sad)
3202428bca3SSubhransu S. Prusty 		goto format_constraint;
3212428bca3SSubhransu S. Prusty 
3222428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3232428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3242428bca3SSubhransu S. Prusty 
3252428bca3SSubhransu S. Prusty 			/*
3262428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3272428bca3SSubhransu S. Prusty 			 * container so we set S32
3282428bca3SSubhransu S. Prusty 			 */
3292428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3302428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3312428bca3SSubhransu S. Prusty 		}
3322428bca3SSubhransu S. Prusty 	}
3332428bca3SSubhransu S. Prusty 
3342428bca3SSubhransu S. Prusty format_constraint:
3352428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3362428bca3SSubhransu S. Prusty 				formats);
3372428bca3SSubhransu S. Prusty 
3382428bca3SSubhransu S. Prusty }
3392428bca3SSubhransu S. Prusty 
340a657f1d0SSubhransu S. Prusty static void
3413787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
342a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
343a657f1d0SSubhransu S. Prusty {
344a657f1d0SSubhransu S. Prusty 	int val;
345a657f1d0SSubhransu S. Prusty 
346a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3473787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
348a657f1d0SSubhransu S. Prusty }
349a657f1d0SSubhransu S. Prusty 
350478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
351478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
352478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
353478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
354478f544eSSubhransu S. Prusty 
355478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
356478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
357478f544eSSubhransu S. Prusty 	u8 CXT04;
358478f544eSSubhransu S. Prusty 	u8 CA;
359478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
360478f544eSSubhransu S. Prusty };
361478f544eSSubhransu S. Prusty 
3623787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
363754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
364a657f1d0SSubhransu S. Prusty {
365a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
366a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
367754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
368478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
3693787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
370ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
371478f544eSSubhransu S. Prusty 	u8 *dip;
372a657f1d0SSubhransu S. Prusty 	int ret;
373a657f1d0SSubhransu S. Prusty 	int i;
374478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
375478f544eSSubhransu S. Prusty 	u8 conn_type;
376bcced704SSubhransu S. Prusty 	int channels, ca;
377a657f1d0SSubhransu S. Prusty 
3783787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
379ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
380bcced704SSubhransu S. Prusty 
381bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
3823787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
383bcced704SSubhransu S. Prusty 
384bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
385ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
386bcced704SSubhransu S. Prusty 
387754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
388478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
389a657f1d0SSubhransu S. Prusty 
390478f544eSSubhransu S. Prusty 	switch (conn_type) {
391478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
392478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
393478f544eSSubhransu S. Prusty 
394478f544eSSubhransu S. Prusty 		frame.channels = channels;
395bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
396a657f1d0SSubhransu S. Prusty 
397a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
398a657f1d0SSubhransu S. Prusty 		if (ret < 0)
399a657f1d0SSubhransu S. Prusty 			return ret;
400a657f1d0SSubhransu S. Prusty 
401478f544eSSubhransu S. Prusty 		break;
402478f544eSSubhransu S. Prusty 
403478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
404478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
405478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
406478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
407478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
408478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
409bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
410478f544eSSubhransu S. Prusty 
411478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
412478f544eSSubhransu S. Prusty 		break;
413478f544eSSubhransu S. Prusty 
414478f544eSSubhransu S. Prusty 	default:
4153787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
416478f544eSSubhransu S. Prusty 		return -EIO;
417478f544eSSubhransu S. Prusty 	}
418478f544eSSubhransu S. Prusty 
419a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4203787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4213787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
422a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
423a657f1d0SSubhransu S. Prusty 
424a657f1d0SSubhransu S. Prusty 
425a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4263787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
427478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
428391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4293787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
430391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
431478f544eSSubhransu S. Prusty 	} else {
432478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4333787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
434478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
435478f544eSSubhransu S. Prusty 	}
436a657f1d0SSubhransu S. Prusty 
437a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4383787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4393787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
440a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
441a657f1d0SSubhransu S. Prusty 
442a657f1d0SSubhransu S. Prusty 	return 0;
443a657f1d0SSubhransu S. Prusty }
444a657f1d0SSubhransu S. Prusty 
445c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
446c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
447c9bfb5d7SJeeja KP 		int slots, int slot_width)
448b0362adbSSubhransu S. Prusty {
4493787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4503787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
451754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
452c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
453c9bfb5d7SJeeja KP 
4543787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
455b0362adbSSubhransu S. Prusty 
456b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
457b0362adbSSubhransu S. Prusty 
458c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
459b0362adbSSubhransu S. Prusty 
460c9bfb5d7SJeeja KP 	if (pcm)
461c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
462bcced704SSubhransu S. Prusty 
463c9bfb5d7SJeeja KP 	return 0;
464b0362adbSSubhransu S. Prusty }
465b0362adbSSubhransu S. Prusty 
466b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
467b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
468b0362adbSSubhransu S. Prusty {
4693787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
470754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
471c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
472c9bfb5d7SJeeja KP 	int format;
473b0362adbSSubhransu S. Prusty 
47454dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
475b0362adbSSubhransu S. Prusty 
476c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
477b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47866d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
479b0362adbSSubhransu S. Prusty 
480c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
481c9bfb5d7SJeeja KP 	if (!pcm)
482148569fdSSubhransu S. Prusty 		return -EIO;
483148569fdSSubhransu S. Prusty 
484c9bfb5d7SJeeja KP 	pcm->format = format;
485c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
486148569fdSSubhransu S. Prusty 
487148569fdSSubhransu S. Prusty 	return 0;
488148569fdSSubhransu S. Prusty }
489148569fdSSubhransu S. Prusty 
4903787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
491754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
492754695f9SJeeja KP 					struct hdac_hdmi_port *port)
493148569fdSSubhransu S. Prusty {
4943787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4953787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
496148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4973787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
498148569fdSSubhransu S. Prusty 		return -EINVAL;
499148569fdSSubhransu S. Prusty 	}
500148569fdSSubhransu S. Prusty 
5013787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
5021b46ebd1SJeeja KP 		return -EIO;
5031b46ebd1SJeeja KP 
5043787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
505754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
506754695f9SJeeja KP 	if (port->num_mux_nids == 0)
5073787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
508754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
509754695f9SJeeja KP 						pin->nid, port->id);
510148569fdSSubhransu S. Prusty 
5113787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
512754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
513148569fdSSubhransu S. Prusty 
514754695f9SJeeja KP 	return port->num_mux_nids;
515148569fdSSubhransu S. Prusty }
516148569fdSSubhransu S. Prusty 
517148569fdSSubhransu S. Prusty /*
518754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
519148569fdSSubhransu S. Prusty  *
520754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
521148569fdSSubhransu S. Prusty  *
522754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
523754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
524148569fdSSubhransu S. Prusty  * connected.
525148569fdSSubhransu S. Prusty  */
526754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5273787a398SRakesh Ughreja 			struct hdac_device *hdev,
528148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
529148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
530148569fdSSubhransu S. Prusty {
531148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
532754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
533148569fdSSubhransu S. Prusty 	int ret, i;
534148569fdSSubhransu S. Prusty 
535148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
536148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
537e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
538e0e5d3e5SJeeja KP 				continue;
539148569fdSSubhransu S. Prusty 
540e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
541e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5423787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
543e0e5d3e5SJeeja KP 							port->pin, port);
544e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
545148569fdSSubhransu S. Prusty 				if (ret < 0)
546e0e5d3e5SJeeja KP 					continue;
547148569fdSSubhransu S. Prusty 
548754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
549e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
550e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
551e0e5d3e5SJeeja KP 						port->eld.eld_valid)
552754695f9SJeeja KP 						return port;
553148569fdSSubhransu S. Prusty 				}
554148569fdSSubhransu S. Prusty 			}
555e0e5d3e5SJeeja KP 		}
556e0e5d3e5SJeeja KP 	}
557148569fdSSubhransu S. Prusty 
558148569fdSSubhransu S. Prusty 	return NULL;
559148569fdSSubhransu S. Prusty }
560148569fdSSubhransu S. Prusty 
56154dfa1eaSSubhransu S. Prusty /*
56254dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
56354dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
56454dfa1eaSSubhransu S. Prusty  * doesn't fail.
56554dfa1eaSSubhransu S. Prusty  */
566b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
567b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
568b0362adbSSubhransu S. Prusty {
5693787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5703787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
571754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
572148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
573754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5742428bca3SSubhransu S. Prusty 	int ret;
575b0362adbSSubhransu S. Prusty 
576b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
577b0362adbSSubhransu S. Prusty 
578148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5793787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
58054dfa1eaSSubhransu S. Prusty 
58154dfa1eaSSubhransu S. Prusty 	/*
58254dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
58354dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
58454dfa1eaSSubhransu S. Prusty 	 */
585754695f9SJeeja KP 	if (!port)
58654dfa1eaSSubhransu S. Prusty 		return 0;
587754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
588754695f9SJeeja KP 			(!port->eld.eld_valid)) {
589b0362adbSSubhransu S. Prusty 
5903787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
591754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
592754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
593754695f9SJeeja KP 			port->pin->nid, port->id);
594b8a54545SSubhransu S. Prusty 
59554dfa1eaSSubhransu S. Prusty 		return 0;
596b0362adbSSubhransu S. Prusty 	}
597b0362adbSSubhransu S. Prusty 
598754695f9SJeeja KP 	dai_map->port = port;
599b0362adbSSubhransu S. Prusty 
6002428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
601754695f9SJeeja KP 				port->eld.eld_buffer);
6022428bca3SSubhransu S. Prusty 	if (ret < 0)
6032428bca3SSubhransu S. Prusty 		return ret;
604b0362adbSSubhransu S. Prusty 
6052428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
606754695f9SJeeja KP 				port->eld.eld_buffer);
607b0362adbSSubhransu S. Prusty }
608b0362adbSSubhransu S. Prusty 
609b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
610b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
611b0362adbSSubhransu S. Prusty {
6123787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
613754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
614ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
615b0362adbSSubhransu S. Prusty 
616b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
617b0362adbSSubhransu S. Prusty 
618ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
619bcced704SSubhransu S. Prusty 
620ab1eea19SJeeja KP 	if (pcm) {
621ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
622ab1eea19SJeeja KP 		pcm->chmap_set = false;
623ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
624ab1eea19SJeeja KP 		pcm->channels = 0;
625ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
626b0362adbSSubhransu S. Prusty 	}
627ab1eea19SJeeja KP 
628754695f9SJeeja KP 	if (dai_map->port)
629754695f9SJeeja KP 		dai_map->port = NULL;
63054dfa1eaSSubhransu S. Prusty }
631b0362adbSSubhransu S. Prusty 
63218382eadSSubhransu S. Prusty static int
633f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
63418382eadSSubhransu S. Prusty {
635bcced704SSubhransu S. Prusty 	unsigned int chans;
636f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
63718382eadSSubhransu S. Prusty 	int err;
63818382eadSSubhransu S. Prusty 
639f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
640bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
641bcced704SSubhransu S. Prusty 
642bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
643bcced704SSubhransu S. Prusty 
644bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
645bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
646bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
64718382eadSSubhransu S. Prusty 
648f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
64918382eadSSubhransu S. Prusty 			&cvt->params.rates,
65018382eadSSubhransu S. Prusty 			&cvt->params.formats,
65118382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
65218382eadSSubhransu S. Prusty 	if (err < 0)
653f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
65418382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
65518382eadSSubhransu S. Prusty 			cvt->nid, err);
65618382eadSSubhransu S. Prusty 
65718382eadSSubhransu S. Prusty 	return err;
65818382eadSSubhransu S. Prusty }
65918382eadSSubhransu S. Prusty 
66079f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
661c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
662c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
663c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
664c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
665c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
66618382eadSSubhransu S. Prusty {
66718382eadSSubhransu S. Prusty 	w->id = id;
66879f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
66979f4e922SSubhransu S. Prusty 	if (!w->name)
67079f4e922SSubhransu S. Prusty 		return -ENOMEM;
67179f4e922SSubhransu S. Prusty 
67218382eadSSubhransu S. Prusty 	w->sname = stream;
67318382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
67418382eadSSubhransu S. Prusty 	w->shift = 0;
67579f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
67679f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
67779f4e922SSubhransu S. Prusty 	w->priv = priv;
678c9bfb5d7SJeeja KP 	w->event = event;
679c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
68079f4e922SSubhransu S. Prusty 
68179f4e922SSubhransu S. Prusty 	return 0;
68218382eadSSubhransu S. Prusty }
68318382eadSSubhransu S. Prusty 
68418382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
68579f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
68679f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
68779f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
68818382eadSSubhransu S. Prusty {
68918382eadSSubhransu S. Prusty 	route->sink = sink;
69018382eadSSubhransu S. Prusty 	route->source = src;
69118382eadSSubhransu S. Prusty 	route->control = control;
69279f4e922SSubhransu S. Prusty 	route->connected = handler;
69318382eadSSubhransu S. Prusty }
69418382eadSSubhransu S. Prusty 
6953787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
696754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6974a3478deSJeeja KP {
6983787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
6994a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
700e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7014a3478deSJeeja KP 
7024a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
703e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
704754695f9SJeeja KP 			continue;
705754695f9SJeeja KP 
706e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
707e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7084a3478deSJeeja KP 				return pcm;
7094a3478deSJeeja KP 		}
710e0e5d3e5SJeeja KP 	}
7114a3478deSJeeja KP 
7124a3478deSJeeja KP 	return NULL;
7134a3478deSJeeja KP }
7144a3478deSJeeja KP 
7153787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
716c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
717c9bfb5d7SJeeja KP {
718753597fbSAbhijeet Kumar 	int count;
719753597fbSAbhijeet Kumar 	unsigned int state;
720753597fbSAbhijeet Kumar 
7213787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7223787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
723753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7243787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
725753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
726753597fbSAbhijeet Kumar 						pwr_state);
7273787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
728753597fbSAbhijeet Kumar 						nid, pwr_state);
729753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
730753597fbSAbhijeet Kumar 					break;
731753597fbSAbhijeet Kumar 			}
732753597fbSAbhijeet Kumar 		}
733c9bfb5d7SJeeja KP 	}
734c9bfb5d7SJeeja KP }
735c9bfb5d7SJeeja KP 
7363787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
737c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
738c9bfb5d7SJeeja KP {
7393787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7403787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
741c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
742c9bfb5d7SJeeja KP }
743c9bfb5d7SJeeja KP 
744c9bfb5d7SJeeja KP 
745c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
746c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
747c9bfb5d7SJeeja KP {
748754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7493787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
750c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
751c9bfb5d7SJeeja KP 
7523787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
753c9bfb5d7SJeeja KP 			__func__, w->name, event);
754c9bfb5d7SJeeja KP 
7553787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
756c9bfb5d7SJeeja KP 	if (!pcm)
757c9bfb5d7SJeeja KP 		return -EIO;
758c9bfb5d7SJeeja KP 
7591b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7603787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7611b46ebd1SJeeja KP 		return -EIO;
7621b46ebd1SJeeja KP 
763c9bfb5d7SJeeja KP 	switch (event) {
764c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7653787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
766c9bfb5d7SJeeja KP 
767c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7683787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
769c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
770c9bfb5d7SJeeja KP 
7713787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
772c9bfb5d7SJeeja KP 
7733787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
774c9bfb5d7SJeeja KP 
775c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7763787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
777c9bfb5d7SJeeja KP 
778c9bfb5d7SJeeja KP 		/* Disable 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, 0);
781c9bfb5d7SJeeja KP 
7823787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
783c9bfb5d7SJeeja KP 		break;
784c9bfb5d7SJeeja KP 
785c9bfb5d7SJeeja KP 	}
786c9bfb5d7SJeeja KP 
787c9bfb5d7SJeeja KP 	return 0;
788c9bfb5d7SJeeja KP }
789c9bfb5d7SJeeja KP 
790c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
791c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
792c9bfb5d7SJeeja KP {
793c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
7943787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
7953787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
796c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
797c9bfb5d7SJeeja KP 
7983787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
799c9bfb5d7SJeeja KP 			__func__, w->name, event);
800c9bfb5d7SJeeja KP 
801c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
802c9bfb5d7SJeeja KP 	if (!pcm)
803c9bfb5d7SJeeja KP 		return -EIO;
804c9bfb5d7SJeeja KP 
805c9bfb5d7SJeeja KP 	switch (event) {
806c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
8073787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
808c9bfb5d7SJeeja KP 
809c9bfb5d7SJeeja KP 		/* Enable transmission */
8103787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
811c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
812c9bfb5d7SJeeja KP 
813c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8143787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
815c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
816c9bfb5d7SJeeja KP 
8173787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
818c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8193787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
820c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
821c9bfb5d7SJeeja KP 		break;
822c9bfb5d7SJeeja KP 
823c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8243787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
825c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8263787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
827c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
828c9bfb5d7SJeeja KP 
8293787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
830c9bfb5d7SJeeja KP 		break;
831c9bfb5d7SJeeja KP 
832c9bfb5d7SJeeja KP 	}
833c9bfb5d7SJeeja KP 
834c9bfb5d7SJeeja KP 	return 0;
835c9bfb5d7SJeeja KP }
836c9bfb5d7SJeeja KP 
837c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
838c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
839c9bfb5d7SJeeja KP {
840754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8413787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
842c9bfb5d7SJeeja KP 	int mux_idx;
843c9bfb5d7SJeeja KP 
8443787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
845c9bfb5d7SJeeja KP 			__func__, w->name, event);
846c9bfb5d7SJeeja KP 
847c9bfb5d7SJeeja KP 	if (!kc)
848c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
849c9bfb5d7SJeeja KP 
850c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8511b46ebd1SJeeja KP 
8521b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8533787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8541b46ebd1SJeeja KP 		return -EIO;
8551b46ebd1SJeeja KP 
856c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8573787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
858c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
859c9bfb5d7SJeeja KP 	}
860c9bfb5d7SJeeja KP 
861c9bfb5d7SJeeja KP 	return 0;
862c9bfb5d7SJeeja KP }
863c9bfb5d7SJeeja KP 
8644a3478deSJeeja KP /*
8654a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8664a3478deSJeeja KP  */
867754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8684a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8694a3478deSJeeja KP {
8704a3478deSJeeja KP 	int ret;
871e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8724a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8734a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8744a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
875754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8763787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
8773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
8784a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8794a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8804a3478deSJeeja KP 
8814a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8824a3478deSJeeja KP 	if (ret < 0)
8834a3478deSJeeja KP 		return ret;
8844a3478deSJeeja KP 
885754695f9SJeeja KP 	if (port == NULL)
886754695f9SJeeja KP 		return -EINVAL;
887754695f9SJeeja KP 
8884a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8894a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
890e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
891e0e5d3e5SJeeja KP 			continue;
892e0e5d3e5SJeeja KP 
893e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
894e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
895e0e5d3e5SJeeja KP 					p->pin == port->pin) {
896332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, false);
897e0e5d3e5SJeeja KP 				list_del(&p->head);
898e0e5d3e5SJeeja KP 			}
899e0e5d3e5SJeeja KP 		}
900e0e5d3e5SJeeja KP 	}
9014a3478deSJeeja KP 
9024a3478deSJeeja KP 	/*
9034a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
9044a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
9054a3478deSJeeja KP 	 */
906e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
907e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
908e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
909754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
910332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, true);
9114a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9124a3478deSJeeja KP 				return ret;
9134a3478deSJeeja KP 			}
9144a3478deSJeeja KP 		}
915e0e5d3e5SJeeja KP 	}
9164a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9174a3478deSJeeja KP 
9184a3478deSJeeja KP 	return ret;
9194a3478deSJeeja KP }
9204a3478deSJeeja KP 
92179f4e922SSubhransu S. Prusty /*
92279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
92379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
92479f4e922SSubhransu S. Prusty  * widget runtime.
92579f4e922SSubhransu S. Prusty  *
92679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
92779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
92879f4e922SSubhransu S. Prusty  * "NONE"
92979f4e922SSubhransu S. Prusty  */
9303787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
931754695f9SJeeja KP 				struct hdac_hdmi_port *port,
93279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
93379f4e922SSubhransu S. Prusty 				const char *widget_name)
93418382eadSSubhransu S. Prusty {
9353787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
936754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
93779f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
93879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
93979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
94079f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
94179f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
94279f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
94379f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
94479f4e922SSubhransu S. Prusty 	int i = 0;
94579f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
94618382eadSSubhransu S. Prusty 
9473787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
94879f4e922SSubhransu S. Prusty 	if (!kc)
94979f4e922SSubhransu S. Prusty 		return -ENOMEM;
95018382eadSSubhransu S. Prusty 
9513787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
95279f4e922SSubhransu S. Prusty 	if (!se)
95379f4e922SSubhransu S. Prusty 		return -ENOMEM;
95418382eadSSubhransu S. Prusty 
95570e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
95670e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9573787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
95879f4e922SSubhransu S. Prusty 	if (!kc->name)
95979f4e922SSubhransu S. Prusty 		return -ENOMEM;
96018382eadSSubhransu S. Prusty 
96179f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
96279f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
96379f4e922SSubhransu S. Prusty 	kc->access = 0;
96479f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
965754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
96679f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
96779f4e922SSubhransu S. Prusty 
96879f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
96979f4e922SSubhransu S. Prusty 
97079f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
97179f4e922SSubhransu S. Prusty 	se->items = num_items;
97279f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
97379f4e922SSubhransu S. Prusty 
97479f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9753787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
97679f4e922SSubhransu S. Prusty 	if (!items[i])
97779f4e922SSubhransu S. Prusty 		return -ENOMEM;
97879f4e922SSubhransu S. Prusty 
97979f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
98079f4e922SSubhransu S. Prusty 		i++;
98179f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
9823787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
98379f4e922SSubhransu S. Prusty 		if (!items[i])
98479f4e922SSubhransu S. Prusty 			return -ENOMEM;
98579f4e922SSubhransu S. Prusty 	}
98679f4e922SSubhransu S. Prusty 
9873787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
98879f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
98979f4e922SSubhransu S. Prusty 	if (!se->texts)
99079f4e922SSubhransu S. Prusty 		return -ENOMEM;
99179f4e922SSubhransu S. Prusty 
9923787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
993754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
994c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
995c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
99679f4e922SSubhransu S. Prusty }
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
9993787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
100079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
100179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
100279f4e922SSubhransu S. Prusty {
10033787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
100479f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
100579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
1006754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
100779f4e922SSubhransu S. Prusty 	int i, j;
100879f4e922SSubhransu S. Prusty 
1009754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
101079f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
101179f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
101279f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
101379f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
101479f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
101579f4e922SSubhransu S. Prusty 					se->texts[j + 1],
101679f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
101779f4e922SSubhransu S. Prusty 
101879f4e922SSubhransu S. Prusty 			rindex++;
101979f4e922SSubhransu S. Prusty 		}
102079f4e922SSubhransu S. Prusty 
102179f4e922SSubhransu S. Prusty 		mux_index++;
102279f4e922SSubhransu S. Prusty 	}
102379f4e922SSubhransu S. Prusty }
102479f4e922SSubhransu S. Prusty 
102579f4e922SSubhransu S. Prusty /*
102679f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
102779f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1028754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1029754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
103079f4e922SSubhransu S. Prusty  *
1031754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1032754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
103379f4e922SSubhransu S. Prusty  *
103479f4e922SSubhransu S. Prusty  * Routes are added as below:
1035754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1036754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
103779f4e922SSubhransu S. Prusty  *
103879f4e922SSubhransu S. Prusty  * Total route elements:
1039754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
104079f4e922SSubhransu S. Prusty  */
104179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
104279f4e922SSubhransu S. Prusty {
104379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
104479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10453787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10463787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10471e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
104879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
104979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
105079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1051754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
105279f4e922SSubhransu S. Prusty 
105379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
105479f4e922SSubhransu S. Prusty 		return -EINVAL;
105579f4e922SSubhransu S. Prusty 
1056754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1057754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
105879f4e922SSubhransu S. Prusty 				GFP_KERNEL);
105979f4e922SSubhransu S. Prusty 
106079f4e922SSubhransu S. Prusty 	if (!widgets)
106179f4e922SSubhransu S. Prusty 		return -ENOMEM;
106279f4e922SSubhransu S. Prusty 
106379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
106479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
106579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
106679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1067c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1068c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1069c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1070c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
107179f4e922SSubhransu S. Prusty 		if (ret < 0)
107279f4e922SSubhransu S. Prusty 			return ret;
107379f4e922SSubhransu S. Prusty 		i++;
107479f4e922SSubhransu S. Prusty 	}
107579f4e922SSubhransu S. Prusty 
107679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1077754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1078754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1079754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
108079f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1081754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1082c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1083c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1084754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1085754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
108679f4e922SSubhransu S. Prusty 			if (ret < 0)
108779f4e922SSubhransu S. Prusty 				return ret;
10880324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
108979f4e922SSubhransu S. Prusty 			i++;
109079f4e922SSubhransu S. Prusty 		}
1091754695f9SJeeja KP 	}
109279f4e922SSubhransu S. Prusty 
109379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
109479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1095754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1096754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1097754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
10983787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1099754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
110079f4e922SSubhransu S. Prusty 						widget_name);
110179f4e922SSubhransu S. Prusty 			if (ret < 0)
110279f4e922SSubhransu S. Prusty 				return ret;
110379f4e922SSubhransu S. Prusty 			i++;
110479f4e922SSubhransu S. Prusty 
110579f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
110679f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
110779f4e922SSubhransu S. Prusty 
110879f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
110979f4e922SSubhransu S. Prusty 			num_routes++;
111079f4e922SSubhransu S. Prusty 		}
1111754695f9SJeeja KP 	}
111279f4e922SSubhransu S. Prusty 
111379f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
111479f4e922SSubhransu S. Prusty 							GFP_KERNEL);
111579f4e922SSubhransu S. Prusty 	if (!route)
111679f4e922SSubhransu S. Prusty 		return -ENOMEM;
111779f4e922SSubhransu S. Prusty 
111879f4e922SSubhransu S. Prusty 	i = 0;
111979f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
112079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1121754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
112279f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1123754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1124754695f9SJeeja KP 						hdmi->num_pin;
112579f4e922SSubhransu S. Prusty 
112679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
112779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
112879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
112979f4e922SSubhransu S. Prusty 			i++;
1130754695f9SJeeja KP 		}
113179f4e922SSubhransu S. Prusty 	}
113279f4e922SSubhransu S. Prusty 
11333787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
113479f4e922SSubhransu S. Prusty 
113579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1136754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
113779f4e922SSubhransu S. Prusty 
113879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
113979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
114079f4e922SSubhransu S. Prusty 
114179f4e922SSubhransu S. Prusty 	return 0;
114279f4e922SSubhransu S. Prusty 
114318382eadSSubhransu S. Prusty }
114418382eadSSubhransu S. Prusty 
11453787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
114618382eadSSubhransu S. Prusty {
11473787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1148754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
114915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1150148569fdSSubhransu S. Prusty 	int dai_id = 0;
115118382eadSSubhransu S. Prusty 
1152148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
115315b91447SSubhransu S. Prusty 		return -EINVAL;
115418382eadSSubhransu S. Prusty 
1155148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1156148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1157148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
115815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
115918382eadSSubhransu S. Prusty 
1160148569fdSSubhransu S. Prusty 		dai_id++;
1161148569fdSSubhransu S. Prusty 
1162148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11633787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1164148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1165148569fdSSubhransu S. Prusty 			break;
1166148569fdSSubhransu S. Prusty 		}
1167148569fdSSubhransu S. Prusty 	}
116818382eadSSubhransu S. Prusty 
116915b91447SSubhransu S. Prusty 	return 0;
117015b91447SSubhransu S. Prusty }
117115b91447SSubhransu S. Prusty 
11723787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
117315b91447SSubhransu S. Prusty {
11743787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
117515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11764a3478deSJeeja KP 	char name[NAME_SIZE];
117715b91447SSubhransu S. Prusty 
1178c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
117915b91447SSubhransu S. Prusty 	if (!cvt)
118015b91447SSubhransu S. Prusty 		return -ENOMEM;
118115b91447SSubhransu S. Prusty 
118215b91447SSubhransu S. Prusty 	cvt->nid = nid;
11834a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1184c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1185c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1186c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
118715b91447SSubhransu S. Prusty 
118815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
118915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
119015b91447SSubhransu S. Prusty 
11913787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
119215b91447SSubhransu S. Prusty }
119315b91447SSubhransu S. Prusty 
11943787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1195754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1196b7756edeSSubhransu S. Prusty {
1197f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1198f6fa11a3SSandeep Tayal 
1199754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1200f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1201f6fa11a3SSandeep Tayal 
1202f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
12033787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1204f6fa11a3SSandeep Tayal 		return -EINVAL;
1205b7756edeSSubhransu S. Prusty 	}
1206b7756edeSSubhransu S. Prusty 
1207754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1208f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1209f6fa11a3SSandeep Tayal 
1210f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
12113787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1212f6fa11a3SSandeep Tayal 		return -EINVAL;
1213f6fa11a3SSandeep Tayal 	}
1214f6fa11a3SSandeep Tayal 
1215754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1216f6fa11a3SSandeep Tayal 
1217f6fa11a3SSandeep Tayal 	return 0;
1218f6fa11a3SSandeep Tayal }
1219f6fa11a3SSandeep Tayal 
1220754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1221754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1222b8a54545SSubhransu S. Prusty {
12233787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12243787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12254a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1226754695f9SJeeja KP 	int size = 0;
12272acd8309SJeeja KP 	int port_id = -1;
1228754695f9SJeeja KP 
1229754695f9SJeeja KP 	if (!hdmi)
1230754695f9SJeeja KP 		return;
12314a3478deSJeeja KP 
12322acd8309SJeeja KP 	/*
12332acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12342acd8309SJeeja KP 	 * to be -1.
12352acd8309SJeeja KP 	 */
12364a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1237754695f9SJeeja KP 	port->eld.monitor_present = false;
1238f6fa11a3SSandeep Tayal 
12392acd8309SJeeja KP 	if (pin->mst_capable)
12402acd8309SJeeja KP 		port_id = port->id;
12412acd8309SJeeja KP 
12423787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1243754695f9SJeeja KP 				&port->eld.monitor_present,
1244754695f9SJeeja KP 				port->eld.eld_buffer,
1245f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1246f6fa11a3SSandeep Tayal 
1247f6fa11a3SSandeep Tayal 	if (size > 0) {
1248f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12493787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1250f6fa11a3SSandeep Tayal 			size = -EINVAL;
1251f6fa11a3SSandeep Tayal 	}
1252f6fa11a3SSandeep Tayal 
1253f6fa11a3SSandeep Tayal 	if (size > 0) {
1254754695f9SJeeja KP 		port->eld.eld_valid = true;
1255754695f9SJeeja KP 		port->eld.eld_size = size;
1256f6fa11a3SSandeep Tayal 	} else {
1257754695f9SJeeja KP 		port->eld.eld_valid = false;
1258754695f9SJeeja KP 		port->eld.eld_size = 0;
1259f6fa11a3SSandeep Tayal 	}
1260b8a54545SSubhransu S. Prusty 
12613787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12624a3478deSJeeja KP 
1263754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1264b8a54545SSubhransu S. Prusty 
12653787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1266754695f9SJeeja KP 						__func__, pin->nid, port->id);
12674a3478deSJeeja KP 
12684a3478deSJeeja KP 		/*
12694a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12704a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12714a3478deSJeeja KP 		 * control select.
12724a3478deSJeeja KP 		 */
1273332ccf00STakashi Iwai 		if (pcm) {
1274e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
1275332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1276332ccf00STakashi Iwai 		}
12774a3478deSJeeja KP 
12784a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1279f6fa11a3SSandeep Tayal 		return;
1280b8a54545SSubhransu S. Prusty 	}
1281b8a54545SSubhransu S. Prusty 
1282754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1283332ccf00STakashi Iwai 		if (pcm) {
1284e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
1285332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1286332ccf00STakashi Iwai 		}
12874a3478deSJeeja KP 
1288f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1289754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1290754695f9SJeeja KP 
1291754695f9SJeeja KP 	}
1292754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12934a3478deSJeeja KP }
12944a3478deSJeeja KP 
1295c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1296754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1297754695f9SJeeja KP {
1298754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1299754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1300754695f9SJeeja KP 	int i;
1301754695f9SJeeja KP 
1302754695f9SJeeja KP 	/*
1303754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1304754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1305754695f9SJeeja KP 	 * implemented.
1306754695f9SJeeja KP 	 */
1307754695f9SJeeja KP 
1308c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1309754695f9SJeeja KP 	if (!ports)
1310754695f9SJeeja KP 		return -ENOMEM;
1311754695f9SJeeja KP 
1312754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1313754695f9SJeeja KP 		ports[i].id = i;
1314754695f9SJeeja KP 		ports[i].pin = pin;
1315332ccf00STakashi Iwai 		INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1316754695f9SJeeja KP 	}
1317754695f9SJeeja KP 	pin->ports = ports;
1318754695f9SJeeja KP 	pin->num_ports = max_ports;
1319754695f9SJeeja KP 	return 0;
1320b8a54545SSubhransu S. Prusty }
1321b8a54545SSubhransu S. Prusty 
13223787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
132315b91447SSubhransu S. Prusty {
13243787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
132515b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1326754695f9SJeeja KP 	int ret;
132715b91447SSubhransu S. Prusty 
1328c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
132915b91447SSubhransu S. Prusty 	if (!pin)
133015b91447SSubhransu S. Prusty 		return -ENOMEM;
133115b91447SSubhransu S. Prusty 
133215b91447SSubhransu S. Prusty 	pin->nid = nid;
13332acd8309SJeeja KP 	pin->mst_capable = false;
13343787a398SRakesh Ughreja 	pin->hdev = hdev;
1335c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1336754695f9SJeeja KP 	if (ret < 0)
1337754695f9SJeeja KP 		return ret;
133815b91447SSubhransu S. Prusty 
133915b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
134015b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1341754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1342b8a54545SSubhransu S. Prusty 
134315b91447SSubhransu S. Prusty 	return 0;
134418382eadSSubhransu S. Prusty }
134518382eadSSubhransu S. Prusty 
1346019033c8SBard liao #define INTEL_VENDOR_NID_0x2 0x02
1347019033c8SBard liao #define INTEL_VENDOR_NID_0x8 0x08
1348019033c8SBard liao #define INTEL_VENDOR_NID_0xb 0x0b
1349211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1350211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1351211caab7SSubhransu S. Prusty #define INTEL_EN_DP12		0x02 /* enable DP 1.2 features */
1352211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1353211caab7SSubhransu S. Prusty 
1354f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1355211caab7SSubhransu S. Prusty {
1356211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1357f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13585622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1359211caab7SSubhransu S. Prusty 
1360f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1361211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1362211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1363211caab7SSubhransu S. Prusty 		return;
1364211caab7SSubhransu S. Prusty 
1365211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1366f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1367211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1368211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1369211caab7SSubhransu S. Prusty 		return;
1370211caab7SSubhransu S. Prusty }
1371211caab7SSubhransu S. Prusty 
1372f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1373211caab7SSubhransu S. Prusty {
1374211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1375f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13765622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1377211caab7SSubhransu S. Prusty 
1378f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1379211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1380211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1381211caab7SSubhransu S. Prusty 		return;
1382211caab7SSubhransu S. Prusty 
1383211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1384211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1385f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1386211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1387211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1388211caab7SSubhransu S. Prusty 		return;
1389211caab7SSubhransu S. Prusty 
1390211caab7SSubhransu S. Prusty }
1391211caab7SSubhransu S. Prusty 
139261b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
139317a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
139417a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
139517a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1396c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
139717a42c45SSubhransu S. Prusty };
139817a42c45SSubhransu S. Prusty 
139917a42c45SSubhransu S. Prusty /*
140017a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
140117a42c45SSubhransu S. Prusty  * based on the number of converter queried.
140217a42c45SSubhransu S. Prusty  */
1403f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
140417a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
140517a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
140617a42c45SSubhransu S. Prusty {
140717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
140817a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
140917a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
141017a42c45SSubhransu S. Prusty 	int i = 0;
141117a42c45SSubhransu S. Prusty 	u32 rates, bps;
141217a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
141317a42c45SSubhransu S. Prusty 	u64 formats;
141417a42c45SSubhransu S. Prusty 	int ret;
141517a42c45SSubhransu S. Prusty 
1416f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
141717a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
141817a42c45SSubhransu S. Prusty 			GFP_KERNEL);
141917a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
142017a42c45SSubhransu S. Prusty 		return -ENOMEM;
142117a42c45SSubhransu S. Prusty 
142217a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1423f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
142417a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
142517a42c45SSubhransu S. Prusty 		if (ret)
142617a42c45SSubhransu S. Prusty 			return ret;
142717a42c45SSubhransu S. Prusty 
14283b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
14293b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
14303b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
14313b857472SYong Zhi 		if (!rates)
14323b857472SYong Zhi 			return -EINVAL;
14333b857472SYong Zhi 
143417a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1435f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
143617a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
143717a42c45SSubhransu S. Prusty 
143817a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
143917a42c45SSubhransu S. Prusty 			return -ENOMEM;
144017a42c45SSubhransu S. Prusty 
144117a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
144217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1443f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
144417a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
144517a42c45SSubhransu S. Prusty 			return -ENOMEM;
144617a42c45SSubhransu S. Prusty 
144717a42c45SSubhransu S. Prusty 		/*
144817a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
144917a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
145017a42c45SSubhransu S. Prusty 		 */
145117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
145217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
145317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
145417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
145517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
145617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
145766d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
145817a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
145917a42c45SSubhransu S. Prusty 		i++;
146017a42c45SSubhransu S. Prusty 	}
146117a42c45SSubhransu S. Prusty 
146217a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14631e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
146417a42c45SSubhransu S. Prusty 
146517a42c45SSubhransu S. Prusty 	return 0;
146617a42c45SSubhransu S. Prusty }
146717a42c45SSubhransu S. Prusty 
146818382eadSSubhransu S. Prusty /*
146918382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
147018382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
147118382eadSSubhransu S. Prusty  */
14723787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
147317a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
147418382eadSSubhransu S. Prusty {
147518382eadSSubhransu S. Prusty 	hda_nid_t nid;
14763c83ac23SSudip Mukherjee 	int i, num_nodes;
14773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
147815b91447SSubhransu S. Prusty 	int ret;
147918382eadSSubhransu S. Prusty 
1480f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1481f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1482211caab7SSubhransu S. Prusty 
1483f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1484541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1485f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
148618382eadSSubhransu S. Prusty 		return -EINVAL;
148718382eadSSubhransu S. Prusty 	}
148818382eadSSubhransu S. Prusty 
148945a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
149018382eadSSubhransu S. Prusty 		unsigned int caps;
149118382eadSSubhransu S. Prusty 		unsigned int type;
149218382eadSSubhransu S. Prusty 
1493f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
149418382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
149518382eadSSubhransu S. Prusty 
149618382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
149718382eadSSubhransu S. Prusty 			continue;
149818382eadSSubhransu S. Prusty 
149918382eadSSubhransu S. Prusty 		switch (type) {
150018382eadSSubhransu S. Prusty 
150118382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
15023787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
150315b91447SSubhransu S. Prusty 			if (ret < 0)
1504c7ba4e53SPierre-Louis Bossart 				return ret;
150518382eadSSubhransu S. Prusty 			break;
150618382eadSSubhransu S. Prusty 
150718382eadSSubhransu S. Prusty 		case AC_WID_PIN:
15083787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
150915b91447SSubhransu S. Prusty 			if (ret < 0)
1510c7ba4e53SPierre-Louis Bossart 				return ret;
151118382eadSSubhransu S. Prusty 			break;
151218382eadSSubhransu S. Prusty 		}
151318382eadSSubhransu S. Prusty 	}
151418382eadSSubhransu S. Prusty 
15151c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
15161c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1517c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1518c7ba4e53SPierre-Louis Bossart 		return ret;
15191c0a7de2SSubhransu S. Prusty 	}
152018382eadSSubhransu S. Prusty 
1521f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
152217a42c45SSubhransu S. Prusty 	if (ret) {
1523f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
152417a42c45SSubhransu S. Prusty 			ret);
1525c7ba4e53SPierre-Louis Bossart 		return ret;
152617a42c45SSubhransu S. Prusty 	}
152717a42c45SSubhransu S. Prusty 
152817a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15293787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15301c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1531c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1532c7ba4e53SPierre-Louis Bossart 			ret);
15331c0a7de2SSubhransu S. Prusty 	return ret;
153418382eadSSubhransu S. Prusty }
153518382eadSSubhransu S. Prusty 
1536a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1537a57942bfSTakashi Iwai {
1538019033c8SBard liao 	struct hdac_device *hdev = aptr;
1539019033c8SBard liao 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1540019033c8SBard liao 	const int *map = hdmi->drv_data->port_map;
1541019033c8SBard liao 	int i;
1542019033c8SBard liao 
1543019033c8SBard liao 	if (!hdmi->drv_data->port_num)
1544a57942bfSTakashi Iwai 		return pin - 4; /* map NID 0x05 -> port #1 */
1545019033c8SBard liao 
1546019033c8SBard liao 	/*
1547019033c8SBard liao 	 * looking for the pin number in the mapping table and return
1548019033c8SBard liao 	 * the index which indicate the port number
1549019033c8SBard liao 	 */
1550019033c8SBard liao 	for (i = 0; i < hdmi->drv_data->port_num; i++) {
1551019033c8SBard liao 		if (pin == map[i])
1552019033c8SBard liao 			return i + 1;
1553019033c8SBard liao 	}
1554019033c8SBard liao 
1555019033c8SBard liao 	/* return -1 if pin number exceeds our expectation */
1556019033c8SBard liao 	dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1557019033c8SBard liao 	return -1;
1558a57942bfSTakashi Iwai }
1559a57942bfSTakashi Iwai 
1560f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1561b8a54545SSubhransu S. Prusty {
15623787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15633787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1564754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1565754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15663787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15672acd8309SJeeja KP 	int i;
1568019033c8SBard liao 	hda_nid_t pin_nid;
1569b8a54545SSubhransu S. Prusty 
1570019033c8SBard liao 	if (!hdmi->drv_data->port_num) {
1571019033c8SBard liao 		/* for legacy platforms */
1572019033c8SBard liao 		pin_nid = port + 0x04;
1573019033c8SBard liao 	} else if (port < hdmi->drv_data->port_num) {
1574019033c8SBard liao 		/* get pin number from the pin2port mapping table */
1575019033c8SBard liao 		pin_nid = hdmi->drv_data->port_map[port - 1];
1576019033c8SBard liao 	} else {
1577019033c8SBard liao 		dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1578019033c8SBard liao 		return;
1579019033c8SBard liao 	}
1580b8a54545SSubhransu S. Prusty 
15813787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1582754695f9SJeeja KP 							pin_nid, pipe);
1583b8a54545SSubhransu S. Prusty 
1584b8a54545SSubhransu S. Prusty 	/*
1585b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1586b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1587b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1588b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1589b8a54545SSubhransu S. Prusty 	 */
159045101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1591b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1592b8a54545SSubhransu S. Prusty 		return;
1593b8a54545SSubhransu S. Prusty 
15943787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1595b8a54545SSubhransu S. Prusty 		return;
1596b8a54545SSubhransu S. Prusty 
1597b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1598754695f9SJeeja KP 		if (pin->nid != pin_nid)
1599754695f9SJeeja KP 			continue;
1600754695f9SJeeja KP 
1601754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1602754695f9SJeeja KP 		if (pipe == -1) {
16032acd8309SJeeja KP 			pin->mst_capable = false;
1604754695f9SJeeja KP 			/* if not MST, default is port[0] */
1605754695f9SJeeja KP 			hport = &pin->ports[0];
16062acd8309SJeeja KP 		} else {
16072acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
16082acd8309SJeeja KP 				pin->mst_capable = true;
16092acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
16102acd8309SJeeja KP 					hport = &pin->ports[i];
161104c8f2bfSJeeja KP 					break;
16122acd8309SJeeja KP 				}
1613b8a54545SSubhransu S. Prusty 			}
1614b8a54545SSubhransu S. Prusty 		}
1615b8a54545SSubhransu S. Prusty 
161604c8f2bfSJeeja KP 		if (hport)
1617754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1618754695f9SJeeja KP 	}
1619754695f9SJeeja KP 
162004c8f2bfSJeeja KP }
162104c8f2bfSJeeja KP 
1622ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1623a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1624b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1625b8a54545SSubhransu S. Prusty };
1626b8a54545SSubhransu S. Prusty 
16272889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
16282889099eSSubhransu S. Prusty 						int device)
16292889099eSSubhransu S. Prusty {
16302889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
16312889099eSSubhransu S. Prusty 
1632bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
16332889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16342889099eSSubhransu S. Prusty 			return rtd->pcm;
16352889099eSSubhransu S. Prusty 	}
16362889099eSSubhransu S. Prusty 
16372889099eSSubhransu S. Prusty 	return NULL;
16382889099eSSubhransu S. Prusty }
16392889099eSSubhransu S. Prusty 
16400324e51bSJeeja KP /* create jack pin kcontrols */
16410324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16423787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16430324e51bSJeeja KP {
16440324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16450324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16460324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16470324e51bSJeeja KP 	char *name;
16480324e51bSJeeja KP 	int i = 0, j;
16493787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16503787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16510324e51bSJeeja KP 
165245101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16530324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16540324e51bSJeeja KP 
16550324e51bSJeeja KP 	if (!kc)
16560324e51bSJeeja KP 		return -ENOMEM;
16570324e51bSJeeja KP 
16580324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16590324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16600324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16610324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
166245101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16630324e51bSJeeja KP 			if (!name)
16640324e51bSJeeja KP 				return -ENOMEM;
16650324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
166645101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16670324e51bSJeeja KP 							GFP_KERNEL);
16680324e51bSJeeja KP 			if (!kc[i].name)
16690324e51bSJeeja KP 				return -ENOMEM;
16700324e51bSJeeja KP 
16710324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16720324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16730324e51bSJeeja KP 			kc[i].access = 0;
16740324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16750324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16760324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16770324e51bSJeeja KP 			i++;
16780324e51bSJeeja KP 		}
16790324e51bSJeeja KP 	}
16800324e51bSJeeja KP 
16810324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16820324e51bSJeeja KP }
16830324e51bSJeeja KP 
168445101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16850324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16860324e51bSJeeja KP {
16873787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
16883787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
16890324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16900324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16910324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16920324e51bSJeeja KP 	char w_name[NAME_SIZE];
16930324e51bSJeeja KP 	int i = 0, j, ret;
16940324e51bSJeeja KP 
16950324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16960324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16970324e51bSJeeja KP 
16980324e51bSJeeja KP 	if (!widgets)
16990324e51bSJeeja KP 		return -ENOMEM;
17000324e51bSJeeja KP 
17010324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
17020324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
17030324e51bSJeeja KP 	if (!route)
17040324e51bSJeeja KP 		return -ENOMEM;
17050324e51bSJeeja KP 
17060324e51bSJeeja KP 	/* create Jack DAPM widget */
17070324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17080324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
17090324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
17100324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
17110324e51bSJeeja KP 
17120324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
17130324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
17140324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
17150324e51bSJeeja KP 			if (ret < 0)
17160324e51bSJeeja KP 				return ret;
17170324e51bSJeeja KP 
17180324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
17190324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
17200324e51bSJeeja KP 
17210324e51bSJeeja KP 			/* add to route from Jack widget to output */
17220324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
17230324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
17240324e51bSJeeja KP 
17250324e51bSJeeja KP 			i++;
17260324e51bSJeeja KP 		}
17270324e51bSJeeja KP 	}
17280324e51bSJeeja KP 
17290324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
17300324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
17310324e51bSJeeja KP 	if (ret < 0)
17320324e51bSJeeja KP 		return ret;
17330324e51bSJeeja KP 
17340324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17350324e51bSJeeja KP 	if (ret < 0)
17360324e51bSJeeja KP 		return ret;
17370324e51bSJeeja KP 
17380324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17390324e51bSJeeja KP 	if (ret < 0)
17400324e51bSJeeja KP 		return ret;
17410324e51bSJeeja KP 
17420324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17433787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17440324e51bSJeeja KP 
17450324e51bSJeeja KP 	if (ret < 0)
17460324e51bSJeeja KP 		return ret;
17470324e51bSJeeja KP 
17480324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17490324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17500324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17510324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17520324e51bSJeeja KP 						pin->ports[j].jack_pin);
17530324e51bSJeeja KP 	}
17540324e51bSJeeja KP 
17550324e51bSJeeja KP 	return 0;
17560324e51bSJeeja KP }
17570324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17580324e51bSJeeja KP 
175962490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
176062490016SJeeja KP 				struct snd_soc_jack *jack)
17614a3478deSJeeja KP {
176245101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17633787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17643787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17654a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17662889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17672889099eSSubhransu S. Prusty 	int err;
17684a3478deSJeeja KP 
17694a3478deSJeeja KP 	/*
17704a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17714a3478deSJeeja KP 	 * add to the pcm list
17724a3478deSJeeja KP 	 */
1773c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
17744a3478deSJeeja KP 	if (!pcm)
17754a3478deSJeeja KP 		return -ENOMEM;
17764a3478deSJeeja KP 	pcm->pcm_id = device;
17774a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1778e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
177962490016SJeeja KP 	pcm->jack = jack;
1780ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1781e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17822889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17832889099eSSubhransu S. Prusty 	if (snd_pcm) {
17842889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17852889099eSSubhransu S. Prusty 		if (err < 0) {
17863787a398SRakesh Ughreja 			dev_err(&hdev->dev,
17872889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17882889099eSSubhransu S. Prusty 				err, device);
17892889099eSSubhransu S. Prusty 			return err;
17902889099eSSubhransu S. Prusty 		}
17912889099eSSubhransu S. Prusty 	}
17922889099eSSubhransu S. Prusty 
17934a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17944a3478deSJeeja KP 
179562490016SJeeja KP 	return 0;
17964a3478deSJeeja KP }
17974a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17984a3478deSJeeja KP 
17993787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1800a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1801a9ce96bcSJeeja KP {
1802a9ce96bcSJeeja KP 	int i;
1803a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1804a9ce96bcSJeeja KP 
1805a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1806a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1807a9ce96bcSJeeja KP 
18083787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1809a9ce96bcSJeeja KP 				pin->mst_capable = false;
1810a9ce96bcSJeeja KP 			else
1811a9ce96bcSJeeja KP 				pin->mst_capable = true;
1812a9ce96bcSJeeja KP 		}
1813a9ce96bcSJeeja KP 
1814a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1815a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1816a9ce96bcSJeeja KP 				continue;
1817a9ce96bcSJeeja KP 
1818a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1819a9ce96bcSJeeja KP 		}
1820a9ce96bcSJeeja KP 	}
1821a9ce96bcSJeeja KP }
1822a9ce96bcSJeeja KP 
182345101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
182418382eadSSubhransu S. Prusty {
18253787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18263787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
182718382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
182845101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1829b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1830a9ce96bcSJeeja KP 	int ret;
183118382eadSSubhransu S. Prusty 
18323787a398SRakesh Ughreja 	hdmi->component = component;
183318382eadSSubhransu S. Prusty 
1834b2047e99SVinod Koul 	/*
1835b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1836b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1837b2047e99SVinod Koul 	 */
183876f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1839500e06b9SVinod Koul 	if (!hlink) {
18403787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1841500e06b9SVinod Koul 		return -EIO;
1842500e06b9SVinod Koul 	}
1843500e06b9SVinod Koul 
184476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1845b2047e99SVinod Koul 
184679f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
184779f4e922SSubhransu S. Prusty 	if (ret < 0)
184879f4e922SSubhransu S. Prusty 		return ret;
184918382eadSSubhransu S. Prusty 
18503787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1851a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1852b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18533787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1854b8a54545SSubhransu S. Prusty 		return ret;
1855b8a54545SSubhransu S. Prusty 	}
1856b8a54545SSubhransu S. Prusty 
18573787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
185818382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18593787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
186018382eadSSubhransu S. Prusty 
1861e342ac08SSubhransu S. Prusty 	/*
186201c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
186301c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
186401c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
186501c83276SLibin Yang 	 * domain in display power will be always turned on before operating
186601c83276SLibin Yang 	 * on the HDMI audio codec registers.
186701c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
186801c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
186901c83276SLibin Yang 	 */
187001c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
187101c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
187201c83276SLibin Yang 	/*
1873e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1874e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1875e342ac08SSubhransu S. Prusty 	 */
18763787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18773787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18783787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1879e342ac08SSubhransu S. Prusty 
1880e342ac08SSubhransu S. Prusty 	return 0;
1881e342ac08SSubhransu S. Prusty }
1882e342ac08SSubhransu S. Prusty 
188345101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1884e342ac08SSubhransu S. Prusty {
18853787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18863787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
18870f6ff785SAmadeusz Sławiński 	int ret;
18880f6ff785SAmadeusz Sławiński 
18890f6ff785SAmadeusz Sławiński 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
18900f6ff785SAmadeusz Sławiński 	if (ret < 0)
18910f6ff785SAmadeusz Sławiński 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
18920f6ff785SAmadeusz Sławiński 				ret);
1893e342ac08SSubhransu S. Prusty 
18943787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
189518382eadSSubhransu S. Prusty }
189618382eadSSubhransu S. Prusty 
1897687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
1898687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
1899571d5078SJeeja KP {
19003787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
19013787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1902687ae9e2STakashi Iwai 	int ret;
19031b377ccdSSubhransu S. Prusty 
1904687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
1905687ae9e2STakashi Iwai 	if (ret < 0)
1906687ae9e2STakashi Iwai 		return ret;
1907571d5078SJeeja KP 	/*
1908571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1909571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1910a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1911a9ce96bcSJeeja KP 	 * already set pin caps.
1912687ae9e2STakashi Iwai 	 *
1913687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1914687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
1915687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
1916571d5078SJeeja KP 	 */
19173787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1918687ae9e2STakashi Iwai 	return 0;
1919571d5078SJeeja KP }
1920571d5078SJeeja KP #else
1921687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
1922571d5078SJeeja KP #endif
1923571d5078SJeeja KP 
192445101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
192518382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1926e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
192745101122SKuninori Morimoto 	.use_pmdown_time	= 1,
192845101122SKuninori Morimoto 	.endianness		= 1,
192945101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
193018382eadSSubhransu S. Prusty };
193118382eadSSubhransu S. Prusty 
1932f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19332889099eSSubhransu S. Prusty 					unsigned char *chmap)
19342889099eSSubhransu S. Prusty {
1935f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19362889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19372889099eSSubhransu S. Prusty 
1938ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19392889099eSSubhransu S. Prusty }
19402889099eSSubhransu S. Prusty 
1941f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19422889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19432889099eSSubhransu S. Prusty {
1944f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19452889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1946e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1947e0e5d3e5SJeeja KP 
1948eb50fa17SSubhransu S. Prusty 	if (!pcm)
1949eb50fa17SSubhransu S. Prusty 		return;
1950eb50fa17SSubhransu S. Prusty 
1951e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1952e0e5d3e5SJeeja KP 		return;
19532889099eSSubhransu S. Prusty 
1954ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1955ab1eea19SJeeja KP 	pcm->chmap_set = true;
1956ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1957e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19582889099eSSubhransu S. Prusty 		if (prepared)
19593787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1960ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19612889099eSSubhransu S. Prusty }
19622889099eSSubhransu S. Prusty 
1963f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19642889099eSSubhransu S. Prusty {
1965f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19662889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19672889099eSSubhransu S. Prusty 
1968eb50fa17SSubhransu S. Prusty 	if (!pcm)
1969eb50fa17SSubhransu S. Prusty 		return false;
1970eb50fa17SSubhransu S. Prusty 
1971e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1972e0e5d3e5SJeeja KP 		return false;
1973e0e5d3e5SJeeja KP 
1974e0e5d3e5SJeeja KP 	return true;
19752889099eSSubhransu S. Prusty }
19762889099eSSubhransu S. Prusty 
1977f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19782889099eSSubhransu S. Prusty {
1979f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19802889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1981e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1982e0e5d3e5SJeeja KP 
1983eb50fa17SSubhransu S. Prusty 	if (!pcm)
1984eb50fa17SSubhransu S. Prusty 		return 0;
1985eb50fa17SSubhransu S. Prusty 
1986e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1987e0e5d3e5SJeeja KP 		return 0;
1988e0e5d3e5SJeeja KP 
1989e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1990e0e5d3e5SJeeja KP 
1991754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19922889099eSSubhransu S. Prusty 		return 0;
19932889099eSSubhransu S. Prusty 
1994754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19952889099eSSubhransu S. Prusty }
19962889099eSSubhransu S. Prusty 
1997019033c8SBard liao static struct hdac_hdmi_drv_data intel_icl_drv_data  = {
1998019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x2,
1999019033c8SBard liao 	.port_map = icl_pin2port_map,
2000019033c8SBard liao 	.port_num = ARRAY_SIZE(icl_pin2port_map),
2001019033c8SBard liao };
2002019033c8SBard liao 
20035622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
2004019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0xb,
20055622bc95SPradeep Tewani };
20065622bc95SPradeep Tewani 
20075622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
2008019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x8,
20095622bc95SPradeep Tewani };
20105622bc95SPradeep Tewani 
20113787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
201218382eadSSubhransu S. Prusty {
20133787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi_priv = NULL;
201417a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
2015b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
201617a42c45SSubhransu S. Prusty 	int num_dais = 0;
201718382eadSSubhransu S. Prusty 	int ret = 0;
2018f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2019f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
202018382eadSSubhransu S. Prusty 
2021b2047e99SVinod Koul 	/* hold the ref while we probe */
202276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2023500e06b9SVinod Koul 	if (!hlink) {
20243787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
2025500e06b9SVinod Koul 		return -EIO;
2026500e06b9SVinod Koul 	}
2027500e06b9SVinod Koul 
202876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2029b2047e99SVinod Koul 
2030f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
203118382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
203218382eadSSubhransu S. Prusty 		return -ENOMEM;
203318382eadSSubhransu S. Prusty 
2034f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20352889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20362889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20372889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20382889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20393787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
204018382eadSSubhransu S. Prusty 
2041eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2042eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2043eb50fa17SSubhransu S. Prusty 
20445622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20455622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20465622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20475622bc95SPradeep Tewani 	else
20485622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20495622bc95SPradeep Tewani 
20503787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
205118382eadSSubhransu S. Prusty 
205215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
205315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20544a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20554a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
205615b91447SSubhransu S. Prusty 
2057aeaccef0SRamesh Babu 	/*
2058aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2059aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2060aeaccef0SRamesh Babu 	 */
20614f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
20624f799e73STakashi Iwai 
20633787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
206417a42c45SSubhransu S. Prusty 	if (ret < 0) {
2065f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
206617a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
206718382eadSSubhransu S. Prusty 		return ret;
206817a42c45SSubhransu S. Prusty 	}
20690fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
207018382eadSSubhransu S. Prusty 
207118382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
207245101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
207317a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2074b2047e99SVinod Koul 
207576f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2076b2047e99SVinod Koul 
2077b2047e99SVinod Koul 	return ret;
207818382eadSSubhransu S. Prusty }
207918382eadSSubhransu S. Prusty 
2080332ccf00STakashi Iwai static void clear_dapm_works(struct hdac_device *hdev)
2081332ccf00STakashi Iwai {
2082332ccf00STakashi Iwai 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2083332ccf00STakashi Iwai 	struct hdac_hdmi_pin *pin;
2084332ccf00STakashi Iwai 	int i;
2085332ccf00STakashi Iwai 
2086332ccf00STakashi Iwai 	list_for_each_entry(pin, &hdmi->pin_list, head)
2087332ccf00STakashi Iwai 		for (i = 0; i < pin->num_ports; i++)
2088332ccf00STakashi Iwai 			cancel_work_sync(&pin->ports[i].dapm_work);
2089332ccf00STakashi Iwai }
2090332ccf00STakashi Iwai 
20913787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
209218382eadSSubhransu S. Prusty {
2093332ccf00STakashi Iwai 	clear_dapm_works(hdev);
209477a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
209577a49672STakashi Iwai 
209618382eadSSubhransu S. Prusty 	return 0;
209718382eadSSubhransu S. Prusty }
209818382eadSSubhransu S. Prusty 
2099e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2100e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2101e342ac08SSubhransu S. Prusty {
21023787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2103f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2104b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2105e342ac08SSubhransu S. Prusty 
2106e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2107e342ac08SSubhransu S. Prusty 
210807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
210907f083abSSubhransu S. Prusty 	if (!bus)
211007f083abSSubhransu S. Prusty 		return 0;
211107f083abSSubhransu S. Prusty 
2112332ccf00STakashi Iwai 	clear_dapm_works(hdev);
2113332ccf00STakashi Iwai 
21141b377ccdSSubhransu S. Prusty 	/*
21151b377ccdSSubhransu S. Prusty 	 * Power down afg.
21161b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21171b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21181b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21191b377ccdSSubhransu S. Prusty 	 * to read the state.
21201b377ccdSSubhransu S. Prusty 	 */
2121f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21221b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
212307f083abSSubhransu S. Prusty 
212476f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2125500e06b9SVinod Koul 	if (!hlink) {
2126500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2127500e06b9SVinod Koul 		return -EIO;
2128500e06b9SVinod Koul 	}
2129500e06b9SVinod Koul 
2130e62bccf8SKai Vehmanen 	snd_hdac_codec_link_down(hdev);
213176f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2132b2047e99SVinod Koul 
21334f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
21344c10473dSPierre-Louis Bossart 
21354f799e73STakashi Iwai 	return 0;
2136e342ac08SSubhransu S. Prusty }
2137e342ac08SSubhransu S. Prusty 
2138e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2139e342ac08SSubhransu S. Prusty {
21403787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2141f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2142b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2143e342ac08SSubhransu S. Prusty 
2144e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2145e342ac08SSubhransu S. Prusty 
214607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
214707f083abSSubhransu S. Prusty 	if (!bus)
214807f083abSSubhransu S. Prusty 		return 0;
214907f083abSSubhransu S. Prusty 
215076f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2151500e06b9SVinod Koul 	if (!hlink) {
2152500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2153500e06b9SVinod Koul 		return -EIO;
2154500e06b9SVinod Koul 	}
2155500e06b9SVinod Koul 
215676f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2157e62bccf8SKai Vehmanen 	snd_hdac_codec_link_up(hdev);
2158b2047e99SVinod Koul 
21594f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
216007f083abSSubhransu S. Prusty 
21613787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21623787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2163ab85f5b3SSubhransu S. Prusty 
2164e342ac08SSubhransu S. Prusty 	/* Power up afg */
2165f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21661b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2167e342ac08SSubhransu S. Prusty 
2168e342ac08SSubhransu S. Prusty 	return 0;
2169e342ac08SSubhransu S. Prusty }
2170e342ac08SSubhransu S. Prusty #else
2171e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2172e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2173e342ac08SSubhransu S. Prusty #endif
2174e342ac08SSubhransu S. Prusty 
2175e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2176e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2177687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2178e342ac08SSubhransu S. Prusty };
2179e342ac08SSubhransu S. Prusty 
218018382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
218118382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2182e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2183cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21845fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21855fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21865622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21875622bc95SPradeep Tewani 						   &intel_glk_drv_data),
2188019033c8SBard liao 	HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2189019033c8SBard liao 						   &intel_icl_drv_data),
219018382eadSSubhransu S. Prusty 	{}
219118382eadSSubhransu S. Prusty };
219218382eadSSubhransu S. Prusty 
219318382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
219418382eadSSubhransu S. Prusty 
2195e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
219618382eadSSubhransu S. Prusty 	.driver = {
219718382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2198e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
219918382eadSSubhransu S. Prusty 	},
220018382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
220118382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
220218382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
220318382eadSSubhransu S. Prusty };
220418382eadSSubhransu S. Prusty 
220518382eadSSubhransu S. Prusty static int __init hdmi_init(void)
220618382eadSSubhransu S. Prusty {
220718382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
220818382eadSSubhransu S. Prusty }
220918382eadSSubhransu S. Prusty 
221018382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
221118382eadSSubhransu S. Prusty {
221218382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
221318382eadSSubhransu S. Prusty }
221418382eadSSubhransu S. Prusty 
221518382eadSSubhransu S. Prusty module_init(hdmi_init);
221618382eadSSubhransu S. Prusty module_exit(hdmi_exit);
221718382eadSSubhransu S. Prusty 
221818382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
221918382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
222018382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
222118382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2222