xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 326b509238171d37402dbe308e154cc234ed1960)
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 
1185622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1195622bc95SPradeep Tewani 	unsigned int vendor_nid;
1205622bc95SPradeep Tewani };
1215622bc95SPradeep Tewani 
12218382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1233787a398SRakesh Ughreja 	struct hdac_device *hdev;
1243787a398SRakesh Ughreja 	struct snd_soc_component *component;
1253787a398SRakesh Ughreja 	struct snd_card *card;
126754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12715b91447SSubhransu S. Prusty 	struct list_head pin_list;
12815b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1294a3478deSJeeja KP 	struct list_head pcm_list;
13015b91447SSubhransu S. Prusty 	int num_pin;
13115b91447SSubhransu S. Prusty 	int num_cvt;
132754695f9SJeeja KP 	int num_ports;
1334a3478deSJeeja KP 	struct mutex pin_mutex;
134bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1355622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1361e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
13718382eadSSubhransu S. Prusty };
13818382eadSSubhransu S. Prusty 
1393787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
140b09b1c3bSUghreja, Rakesh A 
141c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
142c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
143c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
144c9bfb5d7SJeeja KP {
145*326b5092SAmadeusz Sławiński 	struct hdac_hdmi_pcm *pcm;
1461de777feSJeeja KP 
147c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
148c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
149*326b5092SAmadeusz Sławiński 			return pcm;
150c9bfb5d7SJeeja KP 	}
151c9bfb5d7SJeeja KP 
152*326b5092SAmadeusz Sławiński 	return NULL;
153c9bfb5d7SJeeja KP }
1541de777feSJeeja KP 
155e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
156e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
157e0e5d3e5SJeeja KP {
1583787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
159e0e5d3e5SJeeja KP 
160332ccf00STakashi Iwai 	port->is_connect = is_connect;
161e0e5d3e5SJeeja KP 	if (is_connect) {
162e0e5d3e5SJeeja KP 		/*
163e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
164e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
165e0e5d3e5SJeeja KP 		 * ports.
166e0e5d3e5SJeeja KP 		 */
167e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1683787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
169e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
170e0e5d3e5SJeeja KP 					pcm->pcm_id);
17162490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
17262490016SJeeja KP 						SND_JACK_AVOUT);
173e0e5d3e5SJeeja KP 		}
174e0e5d3e5SJeeja KP 		pcm->jack_event++;
175e0e5d3e5SJeeja KP 	} else {
176e0e5d3e5SJeeja KP 		/*
177e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
178e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
179e0e5d3e5SJeeja KP 		 * to multiple ports.
180e0e5d3e5SJeeja KP 		 */
181e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
18262490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
183e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
184e0e5d3e5SJeeja KP 			pcm->jack_event--;
185e0e5d3e5SJeeja KP 	}
186332ccf00STakashi Iwai }
1870324e51bSJeeja KP 
188332ccf00STakashi Iwai static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
189332ccf00STakashi Iwai {
190332ccf00STakashi Iwai 	if (port->is_connect)
191332ccf00STakashi Iwai 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
192332ccf00STakashi Iwai 	else
193332ccf00STakashi Iwai 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1940324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
195e0e5d3e5SJeeja KP }
196e0e5d3e5SJeeja KP 
197332ccf00STakashi Iwai static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
198332ccf00STakashi Iwai {
199332ccf00STakashi Iwai 	struct hdac_hdmi_port *port;
200332ccf00STakashi Iwai 
201332ccf00STakashi Iwai 	port = container_of(work, struct hdac_hdmi_port, dapm_work);
202332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
203332ccf00STakashi Iwai }
204332ccf00STakashi Iwai 
205332ccf00STakashi Iwai static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
206332ccf00STakashi Iwai 		struct hdac_hdmi_port *port, bool is_connect)
207332ccf00STakashi Iwai {
208332ccf00STakashi Iwai 	hdac_hdmi_jack_report(pcm, port, is_connect);
209332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
210332ccf00STakashi Iwai }
211332ccf00STakashi Iwai 
212fc181b04SJeeja KP /* MST supported verbs */
213fc181b04SJeeja KP /*
214fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
215fc181b04SJeeja KP  */
2163787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
217fc181b04SJeeja KP {
218fc181b04SJeeja KP 	unsigned int caps;
219fc181b04SJeeja KP 	unsigned int type, param;
220fc181b04SJeeja KP 
2213787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
222fc181b04SJeeja KP 	type = get_wcaps_type(caps);
223fc181b04SJeeja KP 
224fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
225fc181b04SJeeja KP 		return 0;
226fc181b04SJeeja KP 
2273787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
228fc181b04SJeeja KP 	if (param == -1)
229fc181b04SJeeja KP 		return param;
230fc181b04SJeeja KP 
231fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
232fc181b04SJeeja KP }
233fc181b04SJeeja KP 
234fc181b04SJeeja KP /*
235fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
236fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
237fc181b04SJeeja KP  * is selected or MST is not supported.
238fc181b04SJeeja KP  */
2393787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
240fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
241fc181b04SJeeja KP {
2423787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
243fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
244fc181b04SJeeja KP }
245fc181b04SJeeja KP 
246fc181b04SJeeja KP /*
247fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
248fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
249fc181b04SJeeja KP  */
2503787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
251fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
252fc181b04SJeeja KP {
253fc181b04SJeeja KP 	int num_ports;
254fc181b04SJeeja KP 
255fc181b04SJeeja KP 	if (!port->pin->mst_capable)
256fc181b04SJeeja KP 		return 0;
257fc181b04SJeeja KP 
258fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2593787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
260fc181b04SJeeja KP 	if (num_ports < 0)
261fc181b04SJeeja KP 		return -EIO;
262fc181b04SJeeja KP 	/*
263fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
264fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
265fc181b04SJeeja KP 	 */
266fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
267fc181b04SJeeja KP 		return 0;
268fc181b04SJeeja KP 
2693787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
270fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
271fc181b04SJeeja KP 
2723787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
273fc181b04SJeeja KP 		return -EIO;
274fc181b04SJeeja KP 
2753787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
276fc181b04SJeeja KP 
277fc181b04SJeeja KP 	return 0;
278fc181b04SJeeja KP }
279fc181b04SJeeja KP 
2802889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2812889099eSSubhransu S. Prusty 						int pcm_idx)
2822889099eSSubhransu S. Prusty {
2832889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2842889099eSSubhransu S. Prusty 
2852889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2862889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2872889099eSSubhransu S. Prusty 			return pcm;
2882889099eSSubhransu S. Prusty 	}
2892889099eSSubhransu S. Prusty 
2902889099eSSubhransu S. Prusty 	return NULL;
2912889099eSSubhransu S. Prusty }
2922889099eSSubhransu S. Prusty 
2932428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2942428bca3SSubhransu S. Prusty {
2952428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2962428bca3SSubhransu S. Prusty }
2972428bca3SSubhransu S. Prusty 
2982428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2992428bca3SSubhransu S. Prusty {
3002428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
3012428bca3SSubhransu S. Prusty }
3022428bca3SSubhransu S. Prusty 
3032428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
3042428bca3SSubhransu S. Prusty 						void *eld)
3052428bca3SSubhransu S. Prusty {
3062428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3072428bca3SSubhransu S. Prusty 	int i;
3082428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3092428bca3SSubhransu S. Prusty 
3102428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3112428bca3SSubhransu S. Prusty 	if (!sad)
3122428bca3SSubhransu S. Prusty 		goto format_constraint;
3132428bca3SSubhransu S. Prusty 
3142428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3152428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3162428bca3SSubhransu S. Prusty 
3172428bca3SSubhransu S. Prusty 			/*
3182428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3192428bca3SSubhransu S. Prusty 			 * container so we set S32
3202428bca3SSubhransu S. Prusty 			 */
3212428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3222428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3232428bca3SSubhransu S. Prusty 		}
3242428bca3SSubhransu S. Prusty 	}
3252428bca3SSubhransu S. Prusty 
3262428bca3SSubhransu S. Prusty format_constraint:
3272428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3282428bca3SSubhransu S. Prusty 				formats);
3292428bca3SSubhransu S. Prusty 
3302428bca3SSubhransu S. Prusty }
3312428bca3SSubhransu S. Prusty 
332a657f1d0SSubhransu S. Prusty static void
3333787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
334a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
335a657f1d0SSubhransu S. Prusty {
336a657f1d0SSubhransu S. Prusty 	int val;
337a657f1d0SSubhransu S. Prusty 
338a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3393787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
340a657f1d0SSubhransu S. Prusty }
341a657f1d0SSubhransu S. Prusty 
342478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
343478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
344478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
345478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
346478f544eSSubhransu S. Prusty 
347478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
348478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
349478f544eSSubhransu S. Prusty 	u8 CXT04;
350478f544eSSubhransu S. Prusty 	u8 CA;
351478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
352478f544eSSubhransu S. Prusty };
353478f544eSSubhransu S. Prusty 
3543787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
355754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
356a657f1d0SSubhransu S. Prusty {
357a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
358a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
359754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
360478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
3613787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
362ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
363478f544eSSubhransu S. Prusty 	u8 *dip;
364a657f1d0SSubhransu S. Prusty 	int ret;
365a657f1d0SSubhransu S. Prusty 	int i;
366478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
367478f544eSSubhransu S. Prusty 	u8 conn_type;
368bcced704SSubhransu S. Prusty 	int channels, ca;
369a657f1d0SSubhransu S. Prusty 
3703787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
371ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
372bcced704SSubhransu S. Prusty 
373bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
3743787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
375bcced704SSubhransu S. Prusty 
376bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
377ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
378bcced704SSubhransu S. Prusty 
379754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
380478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
381a657f1d0SSubhransu S. Prusty 
382478f544eSSubhransu S. Prusty 	switch (conn_type) {
383478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
384478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
385478f544eSSubhransu S. Prusty 
386478f544eSSubhransu S. Prusty 		frame.channels = channels;
387bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
388a657f1d0SSubhransu S. Prusty 
389a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
390a657f1d0SSubhransu S. Prusty 		if (ret < 0)
391a657f1d0SSubhransu S. Prusty 			return ret;
392a657f1d0SSubhransu S. Prusty 
393478f544eSSubhransu S. Prusty 		break;
394478f544eSSubhransu S. Prusty 
395478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
396478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
397478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
398478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
399478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
400478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
401bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
402478f544eSSubhransu S. Prusty 
403478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
404478f544eSSubhransu S. Prusty 		break;
405478f544eSSubhransu S. Prusty 
406478f544eSSubhransu S. Prusty 	default:
4073787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
408478f544eSSubhransu S. Prusty 		return -EIO;
409478f544eSSubhransu S. Prusty 	}
410478f544eSSubhransu S. Prusty 
411a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4123787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4133787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
414a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
415a657f1d0SSubhransu S. Prusty 
416a657f1d0SSubhransu S. Prusty 
417a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4183787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
419478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
420391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4213787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
422391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
423478f544eSSubhransu S. Prusty 	} else {
424478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4253787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
426478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
427478f544eSSubhransu S. Prusty 	}
428a657f1d0SSubhransu S. Prusty 
429a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4303787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4313787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
432a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
433a657f1d0SSubhransu S. Prusty 
434a657f1d0SSubhransu S. Prusty 	return 0;
435a657f1d0SSubhransu S. Prusty }
436a657f1d0SSubhransu S. Prusty 
437c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
438c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
439c9bfb5d7SJeeja KP 		int slots, int slot_width)
440b0362adbSSubhransu S. Prusty {
4413787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4423787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
443754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
444c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
445c9bfb5d7SJeeja KP 
4463787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
447b0362adbSSubhransu S. Prusty 
448b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
449b0362adbSSubhransu S. Prusty 
450c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
451b0362adbSSubhransu S. Prusty 
452c9bfb5d7SJeeja KP 	if (pcm)
453c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
454bcced704SSubhransu S. Prusty 
455c9bfb5d7SJeeja KP 	return 0;
456b0362adbSSubhransu S. Prusty }
457b0362adbSSubhransu S. Prusty 
458b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
459b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
460b0362adbSSubhransu S. Prusty {
4613787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
462754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
463c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
464c9bfb5d7SJeeja KP 	int format;
465b0362adbSSubhransu S. Prusty 
46654dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
467b0362adbSSubhransu S. Prusty 
468c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
469b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47066d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
471b0362adbSSubhransu S. Prusty 
472c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
473c9bfb5d7SJeeja KP 	if (!pcm)
474148569fdSSubhransu S. Prusty 		return -EIO;
475148569fdSSubhransu S. Prusty 
476c9bfb5d7SJeeja KP 	pcm->format = format;
477c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
478148569fdSSubhransu S. Prusty 
479148569fdSSubhransu S. Prusty 	return 0;
480148569fdSSubhransu S. Prusty }
481148569fdSSubhransu S. Prusty 
4823787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
483754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
484754695f9SJeeja KP 					struct hdac_hdmi_port *port)
485148569fdSSubhransu S. Prusty {
4863787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4873787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
488148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4893787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
490148569fdSSubhransu S. Prusty 		return -EINVAL;
491148569fdSSubhransu S. Prusty 	}
492148569fdSSubhransu S. Prusty 
4933787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
4941b46ebd1SJeeja KP 		return -EIO;
4951b46ebd1SJeeja KP 
4963787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
497754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
498754695f9SJeeja KP 	if (port->num_mux_nids == 0)
4993787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
500754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
501754695f9SJeeja KP 						pin->nid, port->id);
502148569fdSSubhransu S. Prusty 
5033787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
504754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
505148569fdSSubhransu S. Prusty 
506754695f9SJeeja KP 	return port->num_mux_nids;
507148569fdSSubhransu S. Prusty }
508148569fdSSubhransu S. Prusty 
509148569fdSSubhransu S. Prusty /*
510754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
511148569fdSSubhransu S. Prusty  *
512754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
513148569fdSSubhransu S. Prusty  *
514754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
515754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
516148569fdSSubhransu S. Prusty  * connected.
517148569fdSSubhransu S. Prusty  */
518754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5193787a398SRakesh Ughreja 			struct hdac_device *hdev,
520148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
521148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
522148569fdSSubhransu S. Prusty {
523148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
524754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
525148569fdSSubhransu S. Prusty 	int ret, i;
526148569fdSSubhransu S. Prusty 
527148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
528148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
529e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
530e0e5d3e5SJeeja KP 				continue;
531148569fdSSubhransu S. Prusty 
532e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
533e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5343787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
535e0e5d3e5SJeeja KP 							port->pin, port);
536e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
537148569fdSSubhransu S. Prusty 				if (ret < 0)
538e0e5d3e5SJeeja KP 					continue;
539148569fdSSubhransu S. Prusty 
540754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
541e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
542e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
543e0e5d3e5SJeeja KP 						port->eld.eld_valid)
544754695f9SJeeja KP 						return port;
545148569fdSSubhransu S. Prusty 				}
546148569fdSSubhransu S. Prusty 			}
547e0e5d3e5SJeeja KP 		}
548e0e5d3e5SJeeja KP 	}
549148569fdSSubhransu S. Prusty 
550148569fdSSubhransu S. Prusty 	return NULL;
551148569fdSSubhransu S. Prusty }
552148569fdSSubhransu S. Prusty 
55354dfa1eaSSubhransu S. Prusty /*
554489f231eSKai Vehmanen  * Go through all converters and ensure connection is set to
555489f231eSKai Vehmanen  * the correct pin as set via kcontrols.
556489f231eSKai Vehmanen  */
557489f231eSKai Vehmanen static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
558489f231eSKai Vehmanen {
559489f231eSKai Vehmanen 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
560489f231eSKai Vehmanen 	struct hdac_hdmi_port *port;
561489f231eSKai Vehmanen 	struct hdac_hdmi_cvt *cvt;
562489f231eSKai Vehmanen 	int cvt_idx = 0;
563489f231eSKai Vehmanen 
564489f231eSKai Vehmanen 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
565489f231eSKai Vehmanen 		port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
566489f231eSKai Vehmanen 		if (port && port->pin) {
567489f231eSKai Vehmanen 			snd_hdac_codec_write(hdev, port->pin->nid, 0,
568489f231eSKai Vehmanen 					     AC_VERB_SET_CONNECT_SEL, cvt_idx);
569489f231eSKai Vehmanen 			dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
570489f231eSKai Vehmanen 				__func__, cvt->name, port->pin->nid, cvt_idx);
571489f231eSKai Vehmanen 		}
572489f231eSKai Vehmanen 		++cvt_idx;
573489f231eSKai Vehmanen 	}
574489f231eSKai Vehmanen }
575489f231eSKai Vehmanen 
576489f231eSKai Vehmanen /*
57754dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
57854dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
57954dfa1eaSSubhransu S. Prusty  * doesn't fail.
58054dfa1eaSSubhransu S. Prusty  */
581b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
582b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
583b0362adbSSubhransu S. Prusty {
5843787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5853787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
586754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
587148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
588754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5892428bca3SSubhransu S. Prusty 	int ret;
590b0362adbSSubhransu S. Prusty 
591b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
592b0362adbSSubhransu S. Prusty 
593148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5943787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
59554dfa1eaSSubhransu S. Prusty 
59654dfa1eaSSubhransu S. Prusty 	/*
59754dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
59854dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
59954dfa1eaSSubhransu S. Prusty 	 */
600754695f9SJeeja KP 	if (!port)
60154dfa1eaSSubhransu S. Prusty 		return 0;
602754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
603754695f9SJeeja KP 			(!port->eld.eld_valid)) {
604b0362adbSSubhransu S. Prusty 
6053787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
606754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
607754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
608754695f9SJeeja KP 			port->pin->nid, port->id);
609b8a54545SSubhransu S. Prusty 
61054dfa1eaSSubhransu S. Prusty 		return 0;
611b0362adbSSubhransu S. Prusty 	}
612b0362adbSSubhransu S. Prusty 
613754695f9SJeeja KP 	dai_map->port = port;
614b0362adbSSubhransu S. Prusty 
6152428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
616754695f9SJeeja KP 				port->eld.eld_buffer);
6172428bca3SSubhransu S. Prusty 	if (ret < 0)
6182428bca3SSubhransu S. Prusty 		return ret;
619b0362adbSSubhransu S. Prusty 
6202428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
621754695f9SJeeja KP 				port->eld.eld_buffer);
622b0362adbSSubhransu S. Prusty }
623b0362adbSSubhransu S. Prusty 
624b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
625b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
626b0362adbSSubhransu S. Prusty {
6273787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
628754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
629ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
630b0362adbSSubhransu S. Prusty 
631b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
632b0362adbSSubhransu S. Prusty 
633ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
634bcced704SSubhransu S. Prusty 
635ab1eea19SJeeja KP 	if (pcm) {
636ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
637ab1eea19SJeeja KP 		pcm->chmap_set = false;
638ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
639ab1eea19SJeeja KP 		pcm->channels = 0;
640ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
641b0362adbSSubhransu S. Prusty 	}
642ab1eea19SJeeja KP 
643754695f9SJeeja KP 	if (dai_map->port)
644754695f9SJeeja KP 		dai_map->port = NULL;
64554dfa1eaSSubhransu S. Prusty }
646b0362adbSSubhransu S. Prusty 
64718382eadSSubhransu S. Prusty static int
648f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
64918382eadSSubhransu S. Prusty {
650bcced704SSubhransu S. Prusty 	unsigned int chans;
651f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
65218382eadSSubhransu S. Prusty 	int err;
65318382eadSSubhransu S. Prusty 
654f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
655bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
656bcced704SSubhransu S. Prusty 
657bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
658bcced704SSubhransu S. Prusty 
659bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
660bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
661bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
66218382eadSSubhransu S. Prusty 
663f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
66418382eadSSubhransu S. Prusty 			&cvt->params.rates,
66518382eadSSubhransu S. Prusty 			&cvt->params.formats,
66618382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
66718382eadSSubhransu S. Prusty 	if (err < 0)
668f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
66918382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
67018382eadSSubhransu S. Prusty 			cvt->nid, err);
67118382eadSSubhransu S. Prusty 
67218382eadSSubhransu S. Prusty 	return err;
67318382eadSSubhransu S. Prusty }
67418382eadSSubhransu S. Prusty 
67579f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
676c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
677c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
678c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
679c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
680c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
68118382eadSSubhransu S. Prusty {
68218382eadSSubhransu S. Prusty 	w->id = id;
68379f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
68479f4e922SSubhransu S. Prusty 	if (!w->name)
68579f4e922SSubhransu S. Prusty 		return -ENOMEM;
68679f4e922SSubhransu S. Prusty 
68718382eadSSubhransu S. Prusty 	w->sname = stream;
68818382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
68918382eadSSubhransu S. Prusty 	w->shift = 0;
69079f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
69179f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
69279f4e922SSubhransu S. Prusty 	w->priv = priv;
693c9bfb5d7SJeeja KP 	w->event = event;
694c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
69579f4e922SSubhransu S. Prusty 
69679f4e922SSubhransu S. Prusty 	return 0;
69718382eadSSubhransu S. Prusty }
69818382eadSSubhransu S. Prusty 
69918382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
70079f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
70179f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
70279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
70318382eadSSubhransu S. Prusty {
70418382eadSSubhransu S. Prusty 	route->sink = sink;
70518382eadSSubhransu S. Prusty 	route->source = src;
70618382eadSSubhransu S. Prusty 	route->control = control;
70779f4e922SSubhransu S. Prusty 	route->connected = handler;
70818382eadSSubhransu S. Prusty }
70918382eadSSubhransu S. Prusty 
7103787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
711754695f9SJeeja KP 					struct hdac_hdmi_port *port)
7124a3478deSJeeja KP {
7133787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
7144a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
715e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7164a3478deSJeeja KP 
7174a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
718e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
719754695f9SJeeja KP 			continue;
720754695f9SJeeja KP 
721e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
722e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7234a3478deSJeeja KP 				return pcm;
7244a3478deSJeeja KP 		}
725e0e5d3e5SJeeja KP 	}
7264a3478deSJeeja KP 
7274a3478deSJeeja KP 	return NULL;
7284a3478deSJeeja KP }
7294a3478deSJeeja KP 
7303787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
731c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
732c9bfb5d7SJeeja KP {
733753597fbSAbhijeet Kumar 	int count;
734753597fbSAbhijeet Kumar 	unsigned int state;
735753597fbSAbhijeet Kumar 
7363787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7373787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
738753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7393787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
740753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
741753597fbSAbhijeet Kumar 						pwr_state);
7423787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
743753597fbSAbhijeet Kumar 						nid, pwr_state);
744753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
745753597fbSAbhijeet Kumar 					break;
746753597fbSAbhijeet Kumar 			}
747753597fbSAbhijeet Kumar 		}
748c9bfb5d7SJeeja KP 	}
749c9bfb5d7SJeeja KP }
750c9bfb5d7SJeeja KP 
7513787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
752c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
753c9bfb5d7SJeeja KP {
7543787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7553787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
756c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
757c9bfb5d7SJeeja KP }
758c9bfb5d7SJeeja KP 
759c9bfb5d7SJeeja KP 
760c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
761c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
762c9bfb5d7SJeeja KP {
763754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7643787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
765c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
766c9bfb5d7SJeeja KP 
7673787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
768c9bfb5d7SJeeja KP 			__func__, w->name, event);
769c9bfb5d7SJeeja KP 
7703787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
771c9bfb5d7SJeeja KP 	if (!pcm)
772c9bfb5d7SJeeja KP 		return -EIO;
773c9bfb5d7SJeeja KP 
7741b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7753787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7761b46ebd1SJeeja KP 		return -EIO;
7771b46ebd1SJeeja KP 
778c9bfb5d7SJeeja KP 	switch (event) {
779c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7803787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
781c9bfb5d7SJeeja KP 
782c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7833787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
784c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
785c9bfb5d7SJeeja KP 
7863787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
787c9bfb5d7SJeeja KP 
7883787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
789c9bfb5d7SJeeja KP 
790c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7913787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
792c9bfb5d7SJeeja KP 
793c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
7943787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
795c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
796c9bfb5d7SJeeja KP 
7973787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
798c9bfb5d7SJeeja KP 		break;
799c9bfb5d7SJeeja KP 
800c9bfb5d7SJeeja KP 	}
801c9bfb5d7SJeeja KP 
802c9bfb5d7SJeeja KP 	return 0;
803c9bfb5d7SJeeja KP }
804c9bfb5d7SJeeja KP 
805c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
806c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
807c9bfb5d7SJeeja KP {
808c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
8093787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
8103787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
811c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
812c9bfb5d7SJeeja KP 
8133787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
814c9bfb5d7SJeeja KP 			__func__, w->name, event);
815c9bfb5d7SJeeja KP 
816c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
817c9bfb5d7SJeeja KP 	if (!pcm)
818c9bfb5d7SJeeja KP 		return -EIO;
819c9bfb5d7SJeeja KP 
820c9bfb5d7SJeeja KP 	switch (event) {
821c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
8223787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
823c9bfb5d7SJeeja KP 
824c9bfb5d7SJeeja KP 		/* Enable transmission */
8253787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
826c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
827c9bfb5d7SJeeja KP 
828c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8293787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
830c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
831c9bfb5d7SJeeja KP 
8323787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
833c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8343787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
835c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
836489f231eSKai Vehmanen 
837489f231eSKai Vehmanen 		/*
838489f231eSKai Vehmanen 		 * The connection indices are shared by all converters and
839489f231eSKai Vehmanen 		 * may interfere with each other. Ensure correct
840489f231eSKai Vehmanen 		 * routing for all converters at stream start.
841489f231eSKai Vehmanen 		 */
842489f231eSKai Vehmanen 		hdac_hdmi_verify_connect_sel_all_pins(hdev);
843489f231eSKai Vehmanen 
844c9bfb5d7SJeeja KP 		break;
845c9bfb5d7SJeeja KP 
846c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8473787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
848c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8493787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
850c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
851c9bfb5d7SJeeja KP 
8523787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
853c9bfb5d7SJeeja KP 		break;
854c9bfb5d7SJeeja KP 
855c9bfb5d7SJeeja KP 	}
856c9bfb5d7SJeeja KP 
857c9bfb5d7SJeeja KP 	return 0;
858c9bfb5d7SJeeja KP }
859c9bfb5d7SJeeja KP 
860c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
861c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
862c9bfb5d7SJeeja KP {
863754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8643787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
865c9bfb5d7SJeeja KP 	int mux_idx;
866c9bfb5d7SJeeja KP 
8673787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
868c9bfb5d7SJeeja KP 			__func__, w->name, event);
869c9bfb5d7SJeeja KP 
870c9bfb5d7SJeeja KP 	if (!kc)
871c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
872c9bfb5d7SJeeja KP 
873c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8741b46ebd1SJeeja KP 
8751b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8763787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8771b46ebd1SJeeja KP 		return -EIO;
8781b46ebd1SJeeja KP 
879c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8803787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
881c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
882c9bfb5d7SJeeja KP 	}
883c9bfb5d7SJeeja KP 
884c9bfb5d7SJeeja KP 	return 0;
885c9bfb5d7SJeeja KP }
886c9bfb5d7SJeeja KP 
8874a3478deSJeeja KP /*
8884a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8894a3478deSJeeja KP  */
890754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8914a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8924a3478deSJeeja KP {
8934a3478deSJeeja KP 	int ret;
894e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8954a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8964a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8974a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
898754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8993787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
9003787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
9014a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
9024a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
9034a3478deSJeeja KP 
9044a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
9054a3478deSJeeja KP 	if (ret < 0)
9064a3478deSJeeja KP 		return ret;
9074a3478deSJeeja KP 
908754695f9SJeeja KP 	if (port == NULL)
909754695f9SJeeja KP 		return -EINVAL;
910754695f9SJeeja KP 
9114a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
9124a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
913e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
914e0e5d3e5SJeeja KP 			continue;
915e0e5d3e5SJeeja KP 
916e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
917e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
918e0e5d3e5SJeeja KP 					p->pin == port->pin) {
919332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, false);
920e0e5d3e5SJeeja KP 				list_del(&p->head);
921e0e5d3e5SJeeja KP 			}
922e0e5d3e5SJeeja KP 		}
923e0e5d3e5SJeeja KP 	}
9244a3478deSJeeja KP 
9254a3478deSJeeja KP 	/*
9264a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
9274a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
9284a3478deSJeeja KP 	 */
929e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
930e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
931e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
932754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
933332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, true);
9344a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9354a3478deSJeeja KP 				return ret;
9364a3478deSJeeja KP 			}
9374a3478deSJeeja KP 		}
938e0e5d3e5SJeeja KP 	}
9394a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9404a3478deSJeeja KP 
9414a3478deSJeeja KP 	return ret;
9424a3478deSJeeja KP }
9434a3478deSJeeja KP 
94479f4e922SSubhransu S. Prusty /*
94579f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
94679f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
94779f4e922SSubhransu S. Prusty  * widget runtime.
94879f4e922SSubhransu S. Prusty  *
94979f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
95079f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
95179f4e922SSubhransu S. Prusty  * "NONE"
95279f4e922SSubhransu S. Prusty  */
9533787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
954754695f9SJeeja KP 				struct hdac_hdmi_port *port,
95579f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
95679f4e922SSubhransu S. Prusty 				const char *widget_name)
95718382eadSSubhransu S. Prusty {
9583787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
959754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
96079f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
96179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
96279f4e922SSubhransu S. Prusty 	struct soc_enum *se;
96379f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
96479f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
96579f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
96679f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
96779f4e922SSubhransu S. Prusty 	int i = 0;
96879f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
96918382eadSSubhransu S. Prusty 
9703787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
97179f4e922SSubhransu S. Prusty 	if (!kc)
97279f4e922SSubhransu S. Prusty 		return -ENOMEM;
97318382eadSSubhransu S. Prusty 
9743787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
97579f4e922SSubhransu S. Prusty 	if (!se)
97679f4e922SSubhransu S. Prusty 		return -ENOMEM;
97718382eadSSubhransu S. Prusty 
97870e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
97970e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9803787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
98179f4e922SSubhransu S. Prusty 	if (!kc->name)
98279f4e922SSubhransu S. Prusty 		return -ENOMEM;
98318382eadSSubhransu S. Prusty 
98479f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
98579f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
98679f4e922SSubhransu S. Prusty 	kc->access = 0;
98779f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
988754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
98979f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
99079f4e922SSubhransu S. Prusty 
99179f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
99279f4e922SSubhransu S. Prusty 
99379f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
99479f4e922SSubhransu S. Prusty 	se->items = num_items;
99579f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
99679f4e922SSubhransu S. Prusty 
99779f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9983787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
99979f4e922SSubhransu S. Prusty 	if (!items[i])
100079f4e922SSubhransu S. Prusty 		return -ENOMEM;
100179f4e922SSubhransu S. Prusty 
100279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
100379f4e922SSubhransu S. Prusty 		i++;
100479f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
10053787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
100679f4e922SSubhransu S. Prusty 		if (!items[i])
100779f4e922SSubhransu S. Prusty 			return -ENOMEM;
100879f4e922SSubhransu S. Prusty 	}
100979f4e922SSubhransu S. Prusty 
10103787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
101179f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
101279f4e922SSubhransu S. Prusty 	if (!se->texts)
101379f4e922SSubhransu S. Prusty 		return -ENOMEM;
101479f4e922SSubhransu S. Prusty 
10153787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1016754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1017c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
1018c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
101979f4e922SSubhransu S. Prusty }
102079f4e922SSubhransu S. Prusty 
102179f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
10223787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
102379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
102479f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
102579f4e922SSubhransu S. Prusty {
10263787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
102779f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
102879f4e922SSubhransu S. Prusty 	struct soc_enum *se;
1029754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
103079f4e922SSubhransu S. Prusty 	int i, j;
103179f4e922SSubhransu S. Prusty 
1032754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
103379f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
103479f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
103579f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
103679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
103779f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
103879f4e922SSubhransu S. Prusty 					se->texts[j + 1],
103979f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
104079f4e922SSubhransu S. Prusty 
104179f4e922SSubhransu S. Prusty 			rindex++;
104279f4e922SSubhransu S. Prusty 		}
104379f4e922SSubhransu S. Prusty 
104479f4e922SSubhransu S. Prusty 		mux_index++;
104579f4e922SSubhransu S. Prusty 	}
104679f4e922SSubhransu S. Prusty }
104779f4e922SSubhransu S. Prusty 
104879f4e922SSubhransu S. Prusty /*
104979f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
105079f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1051754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1052754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
105379f4e922SSubhransu S. Prusty  *
1054754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1055754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
105679f4e922SSubhransu S. Prusty  *
105779f4e922SSubhransu S. Prusty  * Routes are added as below:
1058754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1059754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
106079f4e922SSubhransu S. Prusty  *
106179f4e922SSubhransu S. Prusty  * Total route elements:
1062754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
106379f4e922SSubhransu S. Prusty  */
106479f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
106579f4e922SSubhransu S. Prusty {
106679f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
106779f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10683787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10693787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10701e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
107179f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
107279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
107379f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1074754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
107579f4e922SSubhransu S. Prusty 
107679f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
107779f4e922SSubhransu S. Prusty 		return -EINVAL;
107879f4e922SSubhransu S. Prusty 
1079754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1080754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
108179f4e922SSubhransu S. Prusty 				GFP_KERNEL);
108279f4e922SSubhransu S. Prusty 
108379f4e922SSubhransu S. Prusty 	if (!widgets)
108479f4e922SSubhransu S. Prusty 		return -ENOMEM;
108579f4e922SSubhransu S. Prusty 
108679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
108779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
108879f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
108979f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1090c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1091c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1092c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1093c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
109479f4e922SSubhransu S. Prusty 		if (ret < 0)
109579f4e922SSubhransu S. Prusty 			return ret;
109679f4e922SSubhransu S. Prusty 		i++;
109779f4e922SSubhransu S. Prusty 	}
109879f4e922SSubhransu S. Prusty 
109979f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1100754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1101754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1102754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
110379f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1104754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1105c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1106c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1107754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1108754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
110979f4e922SSubhransu S. Prusty 			if (ret < 0)
111079f4e922SSubhransu S. Prusty 				return ret;
11110324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
111279f4e922SSubhransu S. Prusty 			i++;
111379f4e922SSubhransu S. Prusty 		}
1114754695f9SJeeja KP 	}
111579f4e922SSubhransu S. Prusty 
111679f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
111779f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1118754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1119754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1120754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
11213787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1122754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
112379f4e922SSubhransu S. Prusty 						widget_name);
112479f4e922SSubhransu S. Prusty 			if (ret < 0)
112579f4e922SSubhransu S. Prusty 				return ret;
112679f4e922SSubhransu S. Prusty 			i++;
112779f4e922SSubhransu S. Prusty 
112879f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
112979f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
113079f4e922SSubhransu S. Prusty 
113179f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
113279f4e922SSubhransu S. Prusty 			num_routes++;
113379f4e922SSubhransu S. Prusty 		}
1134754695f9SJeeja KP 	}
113579f4e922SSubhransu S. Prusty 
113679f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
113779f4e922SSubhransu S. Prusty 							GFP_KERNEL);
113879f4e922SSubhransu S. Prusty 	if (!route)
113979f4e922SSubhransu S. Prusty 		return -ENOMEM;
114079f4e922SSubhransu S. Prusty 
114179f4e922SSubhransu S. Prusty 	i = 0;
114279f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
114379f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1144754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
114579f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1146754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1147754695f9SJeeja KP 						hdmi->num_pin;
114879f4e922SSubhransu S. Prusty 
114979f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
115079f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
115179f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
115279f4e922SSubhransu S. Prusty 			i++;
1153754695f9SJeeja KP 		}
115479f4e922SSubhransu S. Prusty 	}
115579f4e922SSubhransu S. Prusty 
11563787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
115779f4e922SSubhransu S. Prusty 
115879f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1159754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
116079f4e922SSubhransu S. Prusty 
116179f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
116279f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
116379f4e922SSubhransu S. Prusty 
116479f4e922SSubhransu S. Prusty 	return 0;
116579f4e922SSubhransu S. Prusty 
116618382eadSSubhransu S. Prusty }
116718382eadSSubhransu S. Prusty 
11683787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
116918382eadSSubhransu S. Prusty {
11703787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1171754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
117215b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1173148569fdSSubhransu S. Prusty 	int dai_id = 0;
117418382eadSSubhransu S. Prusty 
1175148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
117615b91447SSubhransu S. Prusty 		return -EINVAL;
117718382eadSSubhransu S. Prusty 
1178148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1179148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1180148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
118115b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
118218382eadSSubhransu S. Prusty 
1183148569fdSSubhransu S. Prusty 		dai_id++;
1184148569fdSSubhransu S. Prusty 
1185148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11863787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1187148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1188148569fdSSubhransu S. Prusty 			break;
1189148569fdSSubhransu S. Prusty 		}
1190148569fdSSubhransu S. Prusty 	}
119118382eadSSubhransu S. Prusty 
119215b91447SSubhransu S. Prusty 	return 0;
119315b91447SSubhransu S. Prusty }
119415b91447SSubhransu S. Prusty 
11953787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
119615b91447SSubhransu S. Prusty {
11973787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
119815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11994a3478deSJeeja KP 	char name[NAME_SIZE];
120015b91447SSubhransu S. Prusty 
1201c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
120215b91447SSubhransu S. Prusty 	if (!cvt)
120315b91447SSubhransu S. Prusty 		return -ENOMEM;
120415b91447SSubhransu S. Prusty 
120515b91447SSubhransu S. Prusty 	cvt->nid = nid;
12064a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1207c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1208c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1209c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
121015b91447SSubhransu S. Prusty 
121115b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
121215b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
121315b91447SSubhransu S. Prusty 
12143787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
121515b91447SSubhransu S. Prusty }
121615b91447SSubhransu S. Prusty 
12173787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1218754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1219b7756edeSSubhransu S. Prusty {
1220f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1221f6fa11a3SSandeep Tayal 
1222754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1223f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1224f6fa11a3SSandeep Tayal 
1225f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
12263787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1227f6fa11a3SSandeep Tayal 		return -EINVAL;
1228b7756edeSSubhransu S. Prusty 	}
1229b7756edeSSubhransu S. Prusty 
1230754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1231f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1232f6fa11a3SSandeep Tayal 
1233f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
12343787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1235f6fa11a3SSandeep Tayal 		return -EINVAL;
1236f6fa11a3SSandeep Tayal 	}
1237f6fa11a3SSandeep Tayal 
1238754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1239f6fa11a3SSandeep Tayal 
1240f6fa11a3SSandeep Tayal 	return 0;
1241f6fa11a3SSandeep Tayal }
1242f6fa11a3SSandeep Tayal 
1243754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1244754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1245b8a54545SSubhransu S. Prusty {
12463787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12473787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12484a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1249754695f9SJeeja KP 	int size = 0;
12502acd8309SJeeja KP 	int port_id = -1;
1251754695f9SJeeja KP 
1252754695f9SJeeja KP 	if (!hdmi)
1253754695f9SJeeja KP 		return;
12544a3478deSJeeja KP 
12552acd8309SJeeja KP 	/*
12562acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12572acd8309SJeeja KP 	 * to be -1.
12582acd8309SJeeja KP 	 */
12594a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1260754695f9SJeeja KP 	port->eld.monitor_present = false;
1261f6fa11a3SSandeep Tayal 
12622acd8309SJeeja KP 	if (pin->mst_capable)
12632acd8309SJeeja KP 		port_id = port->id;
12642acd8309SJeeja KP 
12653787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1266754695f9SJeeja KP 				&port->eld.monitor_present,
1267754695f9SJeeja KP 				port->eld.eld_buffer,
1268f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1269f6fa11a3SSandeep Tayal 
1270f6fa11a3SSandeep Tayal 	if (size > 0) {
1271f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12723787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1273f6fa11a3SSandeep Tayal 			size = -EINVAL;
1274f6fa11a3SSandeep Tayal 	}
1275f6fa11a3SSandeep Tayal 
1276f6fa11a3SSandeep Tayal 	if (size > 0) {
1277754695f9SJeeja KP 		port->eld.eld_valid = true;
1278754695f9SJeeja KP 		port->eld.eld_size = size;
1279f6fa11a3SSandeep Tayal 	} else {
1280754695f9SJeeja KP 		port->eld.eld_valid = false;
1281754695f9SJeeja KP 		port->eld.eld_size = 0;
1282f6fa11a3SSandeep Tayal 	}
1283b8a54545SSubhransu S. Prusty 
12843787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12854a3478deSJeeja KP 
1286754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1287b8a54545SSubhransu S. Prusty 
12883787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1289754695f9SJeeja KP 						__func__, pin->nid, port->id);
12904a3478deSJeeja KP 
12914a3478deSJeeja KP 		/*
12924a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12934a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12944a3478deSJeeja KP 		 * control select.
12954a3478deSJeeja KP 		 */
1296332ccf00STakashi Iwai 		if (pcm) {
1297e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
1298332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1299332ccf00STakashi Iwai 		}
13004a3478deSJeeja KP 
13014a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1302f6fa11a3SSandeep Tayal 		return;
1303b8a54545SSubhransu S. Prusty 	}
1304b8a54545SSubhransu S. Prusty 
1305754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1306332ccf00STakashi Iwai 		if (pcm) {
1307e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
1308332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1309332ccf00STakashi Iwai 		}
13104a3478deSJeeja KP 
1311f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1312754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1313754695f9SJeeja KP 
1314754695f9SJeeja KP 	}
1315754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
13164a3478deSJeeja KP }
13174a3478deSJeeja KP 
1318c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1319754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1320754695f9SJeeja KP {
1321754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1322754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1323754695f9SJeeja KP 	int i;
1324754695f9SJeeja KP 
1325754695f9SJeeja KP 	/*
1326754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1327754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1328754695f9SJeeja KP 	 * implemented.
1329754695f9SJeeja KP 	 */
1330754695f9SJeeja KP 
1331c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1332754695f9SJeeja KP 	if (!ports)
1333754695f9SJeeja KP 		return -ENOMEM;
1334754695f9SJeeja KP 
1335754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1336754695f9SJeeja KP 		ports[i].id = i;
1337754695f9SJeeja KP 		ports[i].pin = pin;
1338332ccf00STakashi Iwai 		INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1339754695f9SJeeja KP 	}
1340754695f9SJeeja KP 	pin->ports = ports;
1341754695f9SJeeja KP 	pin->num_ports = max_ports;
1342754695f9SJeeja KP 	return 0;
1343b8a54545SSubhransu S. Prusty }
1344b8a54545SSubhransu S. Prusty 
13453787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
134615b91447SSubhransu S. Prusty {
13473787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
134815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1349754695f9SJeeja KP 	int ret;
135015b91447SSubhransu S. Prusty 
1351c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
135215b91447SSubhransu S. Prusty 	if (!pin)
135315b91447SSubhransu S. Prusty 		return -ENOMEM;
135415b91447SSubhransu S. Prusty 
135515b91447SSubhransu S. Prusty 	pin->nid = nid;
13562acd8309SJeeja KP 	pin->mst_capable = false;
13573787a398SRakesh Ughreja 	pin->hdev = hdev;
1358c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1359754695f9SJeeja KP 	if (ret < 0)
1360754695f9SJeeja KP 		return ret;
136115b91447SSubhransu S. Prusty 
136215b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
136315b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1364754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1365b8a54545SSubhransu S. Prusty 
136615b91447SSubhransu S. Prusty 	return 0;
136718382eadSSubhransu S. Prusty }
136818382eadSSubhransu S. Prusty 
1369573892b6SKai Vehmanen #define INTEL_VENDOR_NID 0x08
1370573892b6SKai Vehmanen #define INTEL_GLK_VENDOR_NID 0x0b
1371211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1372211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1373211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1374211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1375211caab7SSubhransu S. Prusty 
1376f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1377211caab7SSubhransu S. Prusty {
1378211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1379f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13805622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1381211caab7SSubhransu S. Prusty 
1382f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1383211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1384211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1385211caab7SSubhransu S. Prusty 		return;
1386211caab7SSubhransu S. Prusty 
1387211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1388f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1389211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1390211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1391211caab7SSubhransu S. Prusty 		return;
1392211caab7SSubhransu S. Prusty }
1393211caab7SSubhransu S. Prusty 
1394f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1395211caab7SSubhransu S. Prusty {
1396211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1397f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13985622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1399211caab7SSubhransu S. Prusty 
1400f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1401211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1402211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1403211caab7SSubhransu S. Prusty 		return;
1404211caab7SSubhransu S. Prusty 
1405211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1406211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1407f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1408211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1409211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1410211caab7SSubhransu S. Prusty 		return;
1411211caab7SSubhransu S. Prusty 
1412211caab7SSubhransu S. Prusty }
1413211caab7SSubhransu S. Prusty 
141461b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
141517a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
141617a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
141717a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1418c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
141917a42c45SSubhransu S. Prusty };
142017a42c45SSubhransu S. Prusty 
142117a42c45SSubhransu S. Prusty /*
142217a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
142317a42c45SSubhransu S. Prusty  * based on the number of converter queried.
142417a42c45SSubhransu S. Prusty  */
1425f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
142617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
142717a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
142817a42c45SSubhransu S. Prusty {
142917a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
143017a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
143117a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
143217a42c45SSubhransu S. Prusty 	int i = 0;
143317a42c45SSubhransu S. Prusty 	u32 rates, bps;
143417a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
143517a42c45SSubhransu S. Prusty 	u64 formats;
143617a42c45SSubhransu S. Prusty 	int ret;
143717a42c45SSubhransu S. Prusty 
1438f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
143917a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
144017a42c45SSubhransu S. Prusty 			GFP_KERNEL);
144117a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
144217a42c45SSubhransu S. Prusty 		return -ENOMEM;
144317a42c45SSubhransu S. Prusty 
144417a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1445f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
144617a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
144717a42c45SSubhransu S. Prusty 		if (ret)
144817a42c45SSubhransu S. Prusty 			return ret;
144917a42c45SSubhransu S. Prusty 
14503b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
14513b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
14523b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
14533b857472SYong Zhi 		if (!rates)
14543b857472SYong Zhi 			return -EINVAL;
14553b857472SYong Zhi 
145617a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1457f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
145817a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
145917a42c45SSubhransu S. Prusty 
146017a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
146117a42c45SSubhransu S. Prusty 			return -ENOMEM;
146217a42c45SSubhransu S. Prusty 
146317a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
146417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1465f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
146617a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
146717a42c45SSubhransu S. Prusty 			return -ENOMEM;
146817a42c45SSubhransu S. Prusty 
146917a42c45SSubhransu S. Prusty 		/*
147017a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
147117a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
147217a42c45SSubhransu S. Prusty 		 */
147317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
147417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
147517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
147617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
147717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
147817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
147966d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
148017a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
148117a42c45SSubhransu S. Prusty 		i++;
148217a42c45SSubhransu S. Prusty 	}
148317a42c45SSubhransu S. Prusty 
148417a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14851e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
148617a42c45SSubhransu S. Prusty 
148717a42c45SSubhransu S. Prusty 	return 0;
148817a42c45SSubhransu S. Prusty }
148917a42c45SSubhransu S. Prusty 
149018382eadSSubhransu S. Prusty /*
149118382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
149218382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
149318382eadSSubhransu S. Prusty  */
14943787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
149517a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
149618382eadSSubhransu S. Prusty {
149718382eadSSubhransu S. Prusty 	hda_nid_t nid;
14983c83ac23SSudip Mukherjee 	int i, num_nodes;
14993787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
150015b91447SSubhransu S. Prusty 	int ret;
150118382eadSSubhransu S. Prusty 
1502f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1503f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1504211caab7SSubhransu S. Prusty 
1505f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1506541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1507f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
150818382eadSSubhransu S. Prusty 		return -EINVAL;
150918382eadSSubhransu S. Prusty 	}
151018382eadSSubhransu S. Prusty 
151145a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
151218382eadSSubhransu S. Prusty 		unsigned int caps;
151318382eadSSubhransu S. Prusty 		unsigned int type;
151418382eadSSubhransu S. Prusty 
1515f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
151618382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
151718382eadSSubhransu S. Prusty 
151818382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
151918382eadSSubhransu S. Prusty 			continue;
152018382eadSSubhransu S. Prusty 
152118382eadSSubhransu S. Prusty 		switch (type) {
152218382eadSSubhransu S. Prusty 
152318382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
15243787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
152515b91447SSubhransu S. Prusty 			if (ret < 0)
1526c7ba4e53SPierre-Louis Bossart 				return ret;
152718382eadSSubhransu S. Prusty 			break;
152818382eadSSubhransu S. Prusty 
152918382eadSSubhransu S. Prusty 		case AC_WID_PIN:
15303787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
153115b91447SSubhransu S. Prusty 			if (ret < 0)
1532c7ba4e53SPierre-Louis Bossart 				return ret;
153318382eadSSubhransu S. Prusty 			break;
153418382eadSSubhransu S. Prusty 		}
153518382eadSSubhransu S. Prusty 	}
153618382eadSSubhransu S. Prusty 
15371c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
15381c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1539c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1540c7ba4e53SPierre-Louis Bossart 		return ret;
15411c0a7de2SSubhransu S. Prusty 	}
154218382eadSSubhransu S. Prusty 
1543f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
154417a42c45SSubhransu S. Prusty 	if (ret) {
1545f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
154617a42c45SSubhransu S. Prusty 			ret);
1547c7ba4e53SPierre-Louis Bossart 		return ret;
154817a42c45SSubhransu S. Prusty 	}
154917a42c45SSubhransu S. Prusty 
155017a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15513787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15521c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1553c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1554c7ba4e53SPierre-Louis Bossart 			ret);
15551c0a7de2SSubhransu S. Prusty 	return ret;
155618382eadSSubhransu S. Prusty }
155718382eadSSubhransu S. Prusty 
1558a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1559a57942bfSTakashi Iwai {
1560a57942bfSTakashi Iwai 	return pin - 4; /* map NID 0x05 -> port #1 */
1561a57942bfSTakashi Iwai }
1562a57942bfSTakashi Iwai 
1563f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1564b8a54545SSubhransu S. Prusty {
15653787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15663787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1567754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1568754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15693787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15702acd8309SJeeja KP 	int i;
1571b8a54545SSubhransu S. Prusty 
1572573892b6SKai Vehmanen 	/* Don't know how this mapping is derived */
1573573892b6SKai Vehmanen 	hda_nid_t pin_nid = port + 0x04;
1574b8a54545SSubhransu S. Prusty 
15753787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1576754695f9SJeeja KP 							pin_nid, pipe);
1577b8a54545SSubhransu S. Prusty 
1578b8a54545SSubhransu S. Prusty 	/*
1579b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1580b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1581b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1582b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1583b8a54545SSubhransu S. Prusty 	 */
158445101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1585b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1586b8a54545SSubhransu S. Prusty 		return;
1587b8a54545SSubhransu S. Prusty 
15883787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1589b8a54545SSubhransu S. Prusty 		return;
1590b8a54545SSubhransu S. Prusty 
1591b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1592754695f9SJeeja KP 		if (pin->nid != pin_nid)
1593754695f9SJeeja KP 			continue;
1594754695f9SJeeja KP 
1595754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1596754695f9SJeeja KP 		if (pipe == -1) {
15972acd8309SJeeja KP 			pin->mst_capable = false;
1598754695f9SJeeja KP 			/* if not MST, default is port[0] */
1599754695f9SJeeja KP 			hport = &pin->ports[0];
16002acd8309SJeeja KP 		} else {
16012acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
16022acd8309SJeeja KP 				pin->mst_capable = true;
16032acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
16042acd8309SJeeja KP 					hport = &pin->ports[i];
160504c8f2bfSJeeja KP 					break;
16062acd8309SJeeja KP 				}
1607b8a54545SSubhransu S. Prusty 			}
1608b8a54545SSubhransu S. Prusty 		}
1609b8a54545SSubhransu S. Prusty 
161004c8f2bfSJeeja KP 		if (hport)
1611754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1612754695f9SJeeja KP 	}
1613754695f9SJeeja KP 
161404c8f2bfSJeeja KP }
161504c8f2bfSJeeja KP 
1616ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1617a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1618b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1619b8a54545SSubhransu S. Prusty };
1620b8a54545SSubhransu S. Prusty 
16212889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
16222889099eSSubhransu S. Prusty 						int device)
16232889099eSSubhransu S. Prusty {
16242889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
16252889099eSSubhransu S. Prusty 
1626bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
16272889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16282889099eSSubhransu S. Prusty 			return rtd->pcm;
16292889099eSSubhransu S. Prusty 	}
16302889099eSSubhransu S. Prusty 
16312889099eSSubhransu S. Prusty 	return NULL;
16322889099eSSubhransu S. Prusty }
16332889099eSSubhransu S. Prusty 
16340324e51bSJeeja KP /* create jack pin kcontrols */
16350324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16363787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16370324e51bSJeeja KP {
16380324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16390324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16400324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16410324e51bSJeeja KP 	char *name;
16420324e51bSJeeja KP 	int i = 0, j;
16433787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16443787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16450324e51bSJeeja KP 
164645101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16470324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16480324e51bSJeeja KP 
16490324e51bSJeeja KP 	if (!kc)
16500324e51bSJeeja KP 		return -ENOMEM;
16510324e51bSJeeja KP 
16520324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16530324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16540324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16550324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
165645101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16570324e51bSJeeja KP 			if (!name)
16580324e51bSJeeja KP 				return -ENOMEM;
16590324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
166045101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16610324e51bSJeeja KP 							GFP_KERNEL);
16620324e51bSJeeja KP 			if (!kc[i].name)
16630324e51bSJeeja KP 				return -ENOMEM;
16640324e51bSJeeja KP 
16650324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16660324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16670324e51bSJeeja KP 			kc[i].access = 0;
16680324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16690324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16700324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16710324e51bSJeeja KP 			i++;
16720324e51bSJeeja KP 		}
16730324e51bSJeeja KP 	}
16740324e51bSJeeja KP 
16750324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16760324e51bSJeeja KP }
16770324e51bSJeeja KP 
167845101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16790324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16800324e51bSJeeja KP {
16813787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
16823787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
16830324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16840324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16850324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16860324e51bSJeeja KP 	char w_name[NAME_SIZE];
16870324e51bSJeeja KP 	int i = 0, j, ret;
16880324e51bSJeeja KP 
16890324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16900324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16910324e51bSJeeja KP 
16920324e51bSJeeja KP 	if (!widgets)
16930324e51bSJeeja KP 		return -ENOMEM;
16940324e51bSJeeja KP 
16950324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16960324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16970324e51bSJeeja KP 	if (!route)
16980324e51bSJeeja KP 		return -ENOMEM;
16990324e51bSJeeja KP 
17000324e51bSJeeja KP 	/* create Jack DAPM widget */
17010324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17020324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
17030324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
17040324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
17050324e51bSJeeja KP 
17060324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
17070324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
17080324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
17090324e51bSJeeja KP 			if (ret < 0)
17100324e51bSJeeja KP 				return ret;
17110324e51bSJeeja KP 
17120324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
17130324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
17140324e51bSJeeja KP 
17150324e51bSJeeja KP 			/* add to route from Jack widget to output */
17160324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
17170324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
17180324e51bSJeeja KP 
17190324e51bSJeeja KP 			i++;
17200324e51bSJeeja KP 		}
17210324e51bSJeeja KP 	}
17220324e51bSJeeja KP 
17230324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
17240324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
17250324e51bSJeeja KP 	if (ret < 0)
17260324e51bSJeeja KP 		return ret;
17270324e51bSJeeja KP 
17280324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17290324e51bSJeeja KP 	if (ret < 0)
17300324e51bSJeeja KP 		return ret;
17310324e51bSJeeja KP 
17320324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17330324e51bSJeeja KP 	if (ret < 0)
17340324e51bSJeeja KP 		return ret;
17350324e51bSJeeja KP 
17360324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17373787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17380324e51bSJeeja KP 
17390324e51bSJeeja KP 	if (ret < 0)
17400324e51bSJeeja KP 		return ret;
17410324e51bSJeeja KP 
17420324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17430324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17440324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17450324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17460324e51bSJeeja KP 						pin->ports[j].jack_pin);
17470324e51bSJeeja KP 	}
17480324e51bSJeeja KP 
17490324e51bSJeeja KP 	return 0;
17500324e51bSJeeja KP }
17510324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17520324e51bSJeeja KP 
175362490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
175462490016SJeeja KP 				struct snd_soc_jack *jack)
17554a3478deSJeeja KP {
175645101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17583787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17594a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17602889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17612889099eSSubhransu S. Prusty 	int err;
17624a3478deSJeeja KP 
17634a3478deSJeeja KP 	/*
17644a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17654a3478deSJeeja KP 	 * add to the pcm list
17664a3478deSJeeja KP 	 */
1767c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
17684a3478deSJeeja KP 	if (!pcm)
17694a3478deSJeeja KP 		return -ENOMEM;
17704a3478deSJeeja KP 	pcm->pcm_id = device;
17714a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1772e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
177362490016SJeeja KP 	pcm->jack = jack;
1774ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1775e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17762889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17772889099eSSubhransu S. Prusty 	if (snd_pcm) {
17782889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17792889099eSSubhransu S. Prusty 		if (err < 0) {
17803787a398SRakesh Ughreja 			dev_err(&hdev->dev,
17812889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17822889099eSSubhransu S. Prusty 				err, device);
17832889099eSSubhransu S. Prusty 			return err;
17842889099eSSubhransu S. Prusty 		}
17852889099eSSubhransu S. Prusty 	}
17862889099eSSubhransu S. Prusty 
17874a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17884a3478deSJeeja KP 
178962490016SJeeja KP 	return 0;
17904a3478deSJeeja KP }
17914a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17924a3478deSJeeja KP 
17933787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1794a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1795a9ce96bcSJeeja KP {
1796a9ce96bcSJeeja KP 	int i;
1797a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1798a9ce96bcSJeeja KP 
1799a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1800a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1801a9ce96bcSJeeja KP 
18023787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1803a9ce96bcSJeeja KP 				pin->mst_capable = false;
1804a9ce96bcSJeeja KP 			else
1805a9ce96bcSJeeja KP 				pin->mst_capable = true;
1806a9ce96bcSJeeja KP 		}
1807a9ce96bcSJeeja KP 
1808a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1809a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1810a9ce96bcSJeeja KP 				continue;
1811a9ce96bcSJeeja KP 
1812a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1813a9ce96bcSJeeja KP 		}
1814a9ce96bcSJeeja KP 	}
1815a9ce96bcSJeeja KP }
1816a9ce96bcSJeeja KP 
181745101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
181818382eadSSubhransu S. Prusty {
18193787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18203787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
182118382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
182245101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1823b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1824a9ce96bcSJeeja KP 	int ret;
182518382eadSSubhransu S. Prusty 
18263787a398SRakesh Ughreja 	hdmi->component = component;
182718382eadSSubhransu S. Prusty 
1828b2047e99SVinod Koul 	/*
1829b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1830b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1831b2047e99SVinod Koul 	 */
183276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1833500e06b9SVinod Koul 	if (!hlink) {
18343787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1835500e06b9SVinod Koul 		return -EIO;
1836500e06b9SVinod Koul 	}
1837500e06b9SVinod Koul 
183876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1839b2047e99SVinod Koul 
184079f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
184179f4e922SSubhransu S. Prusty 	if (ret < 0)
184279f4e922SSubhransu S. Prusty 		return ret;
184318382eadSSubhransu S. Prusty 
18443787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1845a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1846b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18473787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1848b8a54545SSubhransu S. Prusty 		return ret;
1849b8a54545SSubhransu S. Prusty 	}
1850b8a54545SSubhransu S. Prusty 
18513787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
185218382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18533787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
185418382eadSSubhransu S. Prusty 
1855e342ac08SSubhransu S. Prusty 	/*
185601c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
185701c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
185801c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
185901c83276SLibin Yang 	 * domain in display power will be always turned on before operating
186001c83276SLibin Yang 	 * on the HDMI audio codec registers.
186101c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
186201c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
186301c83276SLibin Yang 	 */
186401c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
186501c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
186601c83276SLibin Yang 	/*
1867e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1868e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1869e342ac08SSubhransu S. Prusty 	 */
18703787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18713787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18723787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1873e342ac08SSubhransu S. Prusty 
1874e342ac08SSubhransu S. Prusty 	return 0;
1875e342ac08SSubhransu S. Prusty }
1876e342ac08SSubhransu S. Prusty 
187745101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1878e342ac08SSubhransu S. Prusty {
18793787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18803787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
18810f6ff785SAmadeusz Sławiński 	int ret;
18820f6ff785SAmadeusz Sławiński 
18830f6ff785SAmadeusz Sławiński 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
18840f6ff785SAmadeusz Sławiński 	if (ret < 0)
18850f6ff785SAmadeusz Sławiński 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
18860f6ff785SAmadeusz Sławiński 				ret);
1887e342ac08SSubhransu S. Prusty 
18883787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
188918382eadSSubhransu S. Prusty }
189018382eadSSubhransu S. Prusty 
1891687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
1892687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
1893571d5078SJeeja KP {
18943787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
18953787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1896687ae9e2STakashi Iwai 	int ret;
18971b377ccdSSubhransu S. Prusty 
1898687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
1899687ae9e2STakashi Iwai 	if (ret < 0)
1900687ae9e2STakashi Iwai 		return ret;
1901571d5078SJeeja KP 	/*
1902571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1903571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1904a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1905a9ce96bcSJeeja KP 	 * already set pin caps.
1906687ae9e2STakashi Iwai 	 *
1907687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1908687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
1909687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
1910571d5078SJeeja KP 	 */
19113787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1912687ae9e2STakashi Iwai 	return 0;
1913571d5078SJeeja KP }
1914571d5078SJeeja KP #else
1915687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
1916571d5078SJeeja KP #endif
1917571d5078SJeeja KP 
191845101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
191918382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1920e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
192145101122SKuninori Morimoto 	.use_pmdown_time	= 1,
192245101122SKuninori Morimoto 	.endianness		= 1,
192345101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
192418382eadSSubhransu S. Prusty };
192518382eadSSubhransu S. Prusty 
1926f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19272889099eSSubhransu S. Prusty 					unsigned char *chmap)
19282889099eSSubhransu S. Prusty {
1929f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19302889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19312889099eSSubhransu S. Prusty 
1932ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19332889099eSSubhransu S. Prusty }
19342889099eSSubhransu S. Prusty 
1935f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19362889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19372889099eSSubhransu S. Prusty {
1938f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19392889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1940e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1941e0e5d3e5SJeeja KP 
1942eb50fa17SSubhransu S. Prusty 	if (!pcm)
1943eb50fa17SSubhransu S. Prusty 		return;
1944eb50fa17SSubhransu S. Prusty 
1945e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1946e0e5d3e5SJeeja KP 		return;
19472889099eSSubhransu S. Prusty 
1948ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1949ab1eea19SJeeja KP 	pcm->chmap_set = true;
1950ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1951e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19522889099eSSubhransu S. Prusty 		if (prepared)
19533787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1954ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19552889099eSSubhransu S. Prusty }
19562889099eSSubhransu S. Prusty 
1957f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19582889099eSSubhransu S. Prusty {
1959f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19602889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19612889099eSSubhransu S. Prusty 
1962eb50fa17SSubhransu S. Prusty 	if (!pcm)
1963eb50fa17SSubhransu S. Prusty 		return false;
1964eb50fa17SSubhransu S. Prusty 
1965e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1966e0e5d3e5SJeeja KP 		return false;
1967e0e5d3e5SJeeja KP 
1968e0e5d3e5SJeeja KP 	return true;
19692889099eSSubhransu S. Prusty }
19702889099eSSubhransu S. Prusty 
1971f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19722889099eSSubhransu S. Prusty {
1973f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19742889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1975e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1976e0e5d3e5SJeeja KP 
1977eb50fa17SSubhransu S. Prusty 	if (!pcm)
1978eb50fa17SSubhransu S. Prusty 		return 0;
1979eb50fa17SSubhransu S. Prusty 
1980e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1981e0e5d3e5SJeeja KP 		return 0;
1982e0e5d3e5SJeeja KP 
1983e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1984e0e5d3e5SJeeja KP 
1985754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19862889099eSSubhransu S. Prusty 		return 0;
19872889099eSSubhransu S. Prusty 
1988754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19892889099eSSubhransu S. Prusty }
19902889099eSSubhransu S. Prusty 
19915622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
1992573892b6SKai Vehmanen 	.vendor_nid = INTEL_GLK_VENDOR_NID,
19935622bc95SPradeep Tewani };
19945622bc95SPradeep Tewani 
19955622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
1996573892b6SKai Vehmanen 	.vendor_nid = INTEL_VENDOR_NID,
19975622bc95SPradeep Tewani };
19985622bc95SPradeep Tewani 
19993787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
200018382eadSSubhransu S. Prusty {
2001c4aafb33SGuennadi Liakhovetski 	struct hdac_hdmi_priv *hdmi_priv;
200217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
2003c4aafb33SGuennadi Liakhovetski 	struct hdac_ext_link *hlink;
200417a42c45SSubhransu S. Prusty 	int num_dais = 0;
2005c4aafb33SGuennadi Liakhovetski 	int ret;
2006f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2007f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
200818382eadSSubhransu S. Prusty 
2009b2047e99SVinod Koul 	/* hold the ref while we probe */
201076f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2011500e06b9SVinod Koul 	if (!hlink) {
20123787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
2013500e06b9SVinod Koul 		return -EIO;
2014500e06b9SVinod Koul 	}
2015500e06b9SVinod Koul 
201676f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2017b2047e99SVinod Koul 
2018f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
201918382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
202018382eadSSubhransu S. Prusty 		return -ENOMEM;
202118382eadSSubhransu S. Prusty 
2022f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20232889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20242889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20252889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20262889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20273787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
202818382eadSSubhransu S. Prusty 
2029eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2030eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2031eb50fa17SSubhransu S. Prusty 
20325622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20335622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20345622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20355622bc95SPradeep Tewani 	else
20365622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20375622bc95SPradeep Tewani 
20383787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
203918382eadSSubhransu S. Prusty 
204015b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
204115b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20424a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20434a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
204415b91447SSubhransu S. Prusty 
2045aeaccef0SRamesh Babu 	/*
2046aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2047aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2048aeaccef0SRamesh Babu 	 */
20494f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
20504f799e73STakashi Iwai 
20513787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
205217a42c45SSubhransu S. Prusty 	if (ret < 0) {
2053f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
205417a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
205518382eadSSubhransu S. Prusty 		return ret;
205617a42c45SSubhransu S. Prusty 	}
2057774a075aSTakashi Iwai 	snd_hdac_refresh_widgets(hdev);
205818382eadSSubhransu S. Prusty 
205918382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
206045101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
206117a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2062b2047e99SVinod Koul 
206376f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2064b2047e99SVinod Koul 
2065b2047e99SVinod Koul 	return ret;
206618382eadSSubhransu S. Prusty }
206718382eadSSubhransu S. Prusty 
2068332ccf00STakashi Iwai static void clear_dapm_works(struct hdac_device *hdev)
2069332ccf00STakashi Iwai {
2070332ccf00STakashi Iwai 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2071332ccf00STakashi Iwai 	struct hdac_hdmi_pin *pin;
2072332ccf00STakashi Iwai 	int i;
2073332ccf00STakashi Iwai 
2074332ccf00STakashi Iwai 	list_for_each_entry(pin, &hdmi->pin_list, head)
2075332ccf00STakashi Iwai 		for (i = 0; i < pin->num_ports; i++)
2076332ccf00STakashi Iwai 			cancel_work_sync(&pin->ports[i].dapm_work);
2077332ccf00STakashi Iwai }
2078332ccf00STakashi Iwai 
20793787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
208018382eadSSubhransu S. Prusty {
2081332ccf00STakashi Iwai 	clear_dapm_works(hdev);
208277a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
208377a49672STakashi Iwai 
208418382eadSSubhransu S. Prusty 	return 0;
208518382eadSSubhransu S. Prusty }
208618382eadSSubhransu S. Prusty 
2087e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2088e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2089e342ac08SSubhransu S. Prusty {
20903787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2091f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2092b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2093e342ac08SSubhransu S. Prusty 
2094e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2095e342ac08SSubhransu S. Prusty 
209607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
209707f083abSSubhransu S. Prusty 	if (!bus)
209807f083abSSubhransu S. Prusty 		return 0;
209907f083abSSubhransu S. Prusty 
2100332ccf00STakashi Iwai 	clear_dapm_works(hdev);
2101332ccf00STakashi Iwai 
21021b377ccdSSubhransu S. Prusty 	/*
21031b377ccdSSubhransu S. Prusty 	 * Power down afg.
21041b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21051b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21061b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21071b377ccdSSubhransu S. Prusty 	 * to read the state.
21081b377ccdSSubhransu S. Prusty 	 */
2109f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21101b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
211107f083abSSubhransu S. Prusty 
211276f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2113500e06b9SVinod Koul 	if (!hlink) {
2114500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2115500e06b9SVinod Koul 		return -EIO;
2116500e06b9SVinod Koul 	}
2117500e06b9SVinod Koul 
2118e62bccf8SKai Vehmanen 	snd_hdac_codec_link_down(hdev);
211976f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2120b2047e99SVinod Koul 
21214f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
21224c10473dSPierre-Louis Bossart 
21234f799e73STakashi Iwai 	return 0;
2124e342ac08SSubhransu S. Prusty }
2125e342ac08SSubhransu S. Prusty 
2126e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2127e342ac08SSubhransu S. Prusty {
21283787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2129f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2130b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2131e342ac08SSubhransu S. Prusty 
2132e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2133e342ac08SSubhransu S. Prusty 
213407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
213507f083abSSubhransu S. Prusty 	if (!bus)
213607f083abSSubhransu S. Prusty 		return 0;
213707f083abSSubhransu S. Prusty 
213876f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2139500e06b9SVinod Koul 	if (!hlink) {
2140500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2141500e06b9SVinod Koul 		return -EIO;
2142500e06b9SVinod Koul 	}
2143500e06b9SVinod Koul 
214476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2145e62bccf8SKai Vehmanen 	snd_hdac_codec_link_up(hdev);
2146b2047e99SVinod Koul 
21474f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
214807f083abSSubhransu S. Prusty 
21493787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21503787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2151ab85f5b3SSubhransu S. Prusty 
2152e342ac08SSubhransu S. Prusty 	/* Power up afg */
2153f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21541b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2155e342ac08SSubhransu S. Prusty 
2156e342ac08SSubhransu S. Prusty 	return 0;
2157e342ac08SSubhransu S. Prusty }
2158e342ac08SSubhransu S. Prusty #else
2159e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2160e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2161e342ac08SSubhransu S. Prusty #endif
2162e342ac08SSubhransu S. Prusty 
2163e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2164e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2165687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2166e342ac08SSubhransu S. Prusty };
2167e342ac08SSubhransu S. Prusty 
216818382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
216918382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2170e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2171cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21725fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21735fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21745622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21755622bc95SPradeep Tewani 						   &intel_glk_drv_data),
217618382eadSSubhransu S. Prusty 	{}
217718382eadSSubhransu S. Prusty };
217818382eadSSubhransu S. Prusty 
217918382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
218018382eadSSubhransu S. Prusty 
2181e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
218218382eadSSubhransu S. Prusty 	.driver = {
218318382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2184e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
218518382eadSSubhransu S. Prusty 	},
218618382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
218718382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
218818382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
218918382eadSSubhransu S. Prusty };
219018382eadSSubhransu S. Prusty 
219118382eadSSubhransu S. Prusty static int __init hdmi_init(void)
219218382eadSSubhransu S. Prusty {
219318382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
219418382eadSSubhransu S. Prusty }
219518382eadSSubhransu S. Prusty 
219618382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
219718382eadSSubhransu S. Prusty {
219818382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
219918382eadSSubhransu S. Prusty }
220018382eadSSubhransu S. Prusty 
220118382eadSSubhransu S. Prusty module_init(hdmi_init);
220218382eadSSubhransu S. Prusty module_exit(hdmi_exit);
220318382eadSSubhransu S. Prusty 
220418382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
220518382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
220618382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
220718382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2208