xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision f6887a71)
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  */
121f53bcb3SBrent Lu 
1318382eadSSubhransu S. Prusty #include <linux/init.h>
1418382eadSSubhransu S. Prusty #include <linux/delay.h>
1518382eadSSubhransu S. Prusty #include <linux/module.h>
1618382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
17a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
182428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
1918382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
204a3478deSJeeja KP #include <sound/jack.h>
2118382eadSSubhransu S. Prusty #include <sound/soc.h>
2218382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
2307f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
242428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
25bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
2618382eadSSubhransu S. Prusty #include "../../hda/local.h"
274a3478deSJeeja KP #include "hdac_hdmi.h"
2818382eadSSubhransu S. Prusty 
2917a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3017a42c45SSubhransu S. Prusty 
31b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
32b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
3318382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
34b0362adbSSubhransu S. Prusty 
3518382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
3618382eadSSubhransu S. Prusty 
37148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
38754695f9SJeeja KP #define HDA_MAX_PORTS		3
39148569fdSSubhransu S. Prusty 
40b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
41b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
42b8a54545SSubhransu S. Prusty 
43f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
44f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
45f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
46f6fa11a3SSandeep Tayal 
4718382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
4818382eadSSubhransu S. Prusty 	unsigned int channels_min;
4918382eadSSubhransu S. Prusty 	unsigned int channels_max;
5018382eadSSubhransu S. Prusty 	u32 rates;
5118382eadSSubhransu S. Prusty 	u64 formats;
5218382eadSSubhransu S. Prusty 	unsigned int maxbps;
5318382eadSSubhransu S. Prusty };
5418382eadSSubhransu S. Prusty 
5518382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
5615b91447SSubhransu S. Prusty 	struct list_head head;
5718382eadSSubhransu S. Prusty 	hda_nid_t nid;
584a3478deSJeeja KP 	const char *name;
5918382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
62b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
63b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
64b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
65b7756edeSSubhransu S. Prusty };
66b7756edeSSubhransu S. Prusty 
67b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
68b8a54545SSubhransu S. Prusty 	bool	monitor_present;
69b8a54545SSubhransu S. Prusty 	bool	eld_valid;
70b8a54545SSubhransu S. Prusty 	int	eld_size;
71b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
72b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
73b8a54545SSubhransu S. Prusty };
74b8a54545SSubhransu S. Prusty 
7518382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
7615b91447SSubhransu S. Prusty 	struct list_head head;
7718382eadSSubhransu S. Prusty 	hda_nid_t nid;
782acd8309SJeeja KP 	bool mst_capable;
79754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
80754695f9SJeeja KP 	int num_ports;
813787a398SRakesh Ughreja 	struct hdac_device *hdev;
82754695f9SJeeja KP };
83754695f9SJeeja KP 
84754695f9SJeeja KP struct hdac_hdmi_port {
85e0e5d3e5SJeeja KP 	struct list_head head;
86754695f9SJeeja KP 	int id;
87754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
8818382eadSSubhransu S. Prusty 	int num_mux_nids;
8918382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
90b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
910324e51bSJeeja KP 	const char *jack_pin;
92332ccf00STakashi Iwai 	bool is_connect;
930324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
940324e51bSJeeja KP 	const char *output_pin;
95332ccf00STakashi Iwai 	struct work_struct dapm_work;
9618382eadSSubhransu S. Prusty };
9718382eadSSubhransu S. Prusty 
984a3478deSJeeja KP struct hdac_hdmi_pcm {
994a3478deSJeeja KP 	struct list_head head;
1004a3478deSJeeja KP 	int pcm_id;
101e0e5d3e5SJeeja KP 	struct list_head port_list;
1024a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10362490016SJeeja KP 	struct snd_soc_jack *jack;
104c9bfb5d7SJeeja KP 	int stream_tag;
105c9bfb5d7SJeeja KP 	int channels;
106c9bfb5d7SJeeja KP 	int format;
107ab1eea19SJeeja KP 	bool chmap_set;
108ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
109ab1eea19SJeeja KP 	struct mutex lock;
110e0e5d3e5SJeeja KP 	int jack_event;
1111f53bcb3SBrent Lu 	struct snd_kcontrol *eld_ctl;
1124a3478deSJeeja KP };
1134a3478deSJeeja KP 
114754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11518382eadSSubhransu S. Prusty 	int dai_id;
116754695f9SJeeja KP 	struct hdac_hdmi_port *port;
11715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11818382eadSSubhransu S. Prusty };
11918382eadSSubhransu S. Prusty 
1205622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1215622bc95SPradeep Tewani 	unsigned int vendor_nid;
1225622bc95SPradeep Tewani };
1235622bc95SPradeep Tewani 
12418382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1253787a398SRakesh Ughreja 	struct hdac_device *hdev;
1263787a398SRakesh Ughreja 	struct snd_soc_component *component;
1273787a398SRakesh Ughreja 	struct snd_card *card;
128754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
12915b91447SSubhransu S. Prusty 	struct list_head pin_list;
13015b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1314a3478deSJeeja KP 	struct list_head pcm_list;
13215b91447SSubhransu S. Prusty 	int num_pin;
13315b91447SSubhransu S. Prusty 	int num_cvt;
134754695f9SJeeja KP 	int num_ports;
1354a3478deSJeeja KP 	struct mutex pin_mutex;
136bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1375622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1381e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
13918382eadSSubhransu S. Prusty };
14018382eadSSubhransu S. Prusty 
1413787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
142b09b1c3bSUghreja, Rakesh A 
143c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv * hdmi,struct hdac_hdmi_cvt * cvt)144c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
145c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
146c9bfb5d7SJeeja KP {
147326b5092SAmadeusz Sławiński 	struct hdac_hdmi_pcm *pcm;
1481de777feSJeeja KP 
149c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
150c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
151326b5092SAmadeusz Sławiński 			return pcm;
152c9bfb5d7SJeeja KP 	}
153c9bfb5d7SJeeja KP 
154326b5092SAmadeusz Sławiński 	return NULL;
155c9bfb5d7SJeeja KP }
1561de777feSJeeja KP 
hdac_hdmi_jack_report(struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port,bool is_connect)157e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
158e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
159e0e5d3e5SJeeja KP {
1603787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
161e0e5d3e5SJeeja KP 
162332ccf00STakashi Iwai 	port->is_connect = is_connect;
163e0e5d3e5SJeeja KP 	if (is_connect) {
164e0e5d3e5SJeeja KP 		/*
165e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
166e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
167e0e5d3e5SJeeja KP 		 * ports.
168e0e5d3e5SJeeja KP 		 */
169e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1703787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
171e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
172e0e5d3e5SJeeja KP 					pcm->pcm_id);
17362490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
17462490016SJeeja KP 						SND_JACK_AVOUT);
175e0e5d3e5SJeeja KP 		}
176e0e5d3e5SJeeja KP 		pcm->jack_event++;
177e0e5d3e5SJeeja KP 	} else {
178e0e5d3e5SJeeja KP 		/*
179e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
180e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
181e0e5d3e5SJeeja KP 		 * to multiple ports.
182e0e5d3e5SJeeja KP 		 */
183e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
18462490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
185e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
186e0e5d3e5SJeeja KP 			pcm->jack_event--;
187e0e5d3e5SJeeja KP 	}
188332ccf00STakashi Iwai }
1890324e51bSJeeja KP 
hdac_hdmi_port_dapm_update(struct hdac_hdmi_port * port)190332ccf00STakashi Iwai static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
191332ccf00STakashi Iwai {
192332ccf00STakashi Iwai 	if (port->is_connect)
193332ccf00STakashi Iwai 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
194332ccf00STakashi Iwai 	else
195332ccf00STakashi Iwai 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1960324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
197e0e5d3e5SJeeja KP }
198e0e5d3e5SJeeja KP 
hdac_hdmi_jack_dapm_work(struct work_struct * work)199332ccf00STakashi Iwai static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
200332ccf00STakashi Iwai {
201332ccf00STakashi Iwai 	struct hdac_hdmi_port *port;
202332ccf00STakashi Iwai 
203332ccf00STakashi Iwai 	port = container_of(work, struct hdac_hdmi_port, dapm_work);
204332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
205332ccf00STakashi Iwai }
206332ccf00STakashi Iwai 
hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port,bool is_connect)207332ccf00STakashi Iwai static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
208332ccf00STakashi Iwai 		struct hdac_hdmi_port *port, bool is_connect)
209332ccf00STakashi Iwai {
210332ccf00STakashi Iwai 	hdac_hdmi_jack_report(pcm, port, is_connect);
211332ccf00STakashi Iwai 	hdac_hdmi_port_dapm_update(port);
212332ccf00STakashi Iwai }
213332ccf00STakashi Iwai 
214fc181b04SJeeja KP /* MST supported verbs */
215fc181b04SJeeja KP /*
216fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
217fc181b04SJeeja KP  */
hdac_hdmi_get_port_len(struct hdac_device * hdev,hda_nid_t nid)2183787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
219fc181b04SJeeja KP {
220fc181b04SJeeja KP 	unsigned int caps;
221fc181b04SJeeja KP 	unsigned int type, param;
222fc181b04SJeeja KP 
2233787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
224fc181b04SJeeja KP 	type = get_wcaps_type(caps);
225fc181b04SJeeja KP 
226fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
227fc181b04SJeeja KP 		return 0;
228fc181b04SJeeja KP 
2293787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
230fc181b04SJeeja KP 	if (param == -1)
231fc181b04SJeeja KP 		return param;
232fc181b04SJeeja KP 
233fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
234fc181b04SJeeja KP }
235fc181b04SJeeja KP 
236fc181b04SJeeja KP /*
237fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
238fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
239fc181b04SJeeja KP  * is selected or MST is not supported.
240fc181b04SJeeja KP  */
hdac_hdmi_port_select_get(struct hdac_device * hdev,struct hdac_hdmi_port * port)2413787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
242fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
243fc181b04SJeeja KP {
2443787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
245fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
246fc181b04SJeeja KP }
247fc181b04SJeeja KP 
248fc181b04SJeeja KP /*
249fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
250fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
251fc181b04SJeeja KP  */
hdac_hdmi_port_select_set(struct hdac_device * hdev,struct hdac_hdmi_port * port)2523787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
253fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
254fc181b04SJeeja KP {
255fc181b04SJeeja KP 	int num_ports;
256fc181b04SJeeja KP 
257fc181b04SJeeja KP 	if (!port->pin->mst_capable)
258fc181b04SJeeja KP 		return 0;
259fc181b04SJeeja KP 
260fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2613787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
262fc181b04SJeeja KP 	if (num_ports < 0)
263fc181b04SJeeja KP 		return -EIO;
264fc181b04SJeeja KP 	/*
265fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
266fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
267fc181b04SJeeja KP 	 */
268fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
269fc181b04SJeeja KP 		return 0;
270fc181b04SJeeja KP 
2713787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
272fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
273fc181b04SJeeja KP 
2743787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
275fc181b04SJeeja KP 		return -EIO;
276fc181b04SJeeja KP 
2773787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
278fc181b04SJeeja KP 
279fc181b04SJeeja KP 	return 0;
280fc181b04SJeeja KP }
281fc181b04SJeeja KP 
get_hdmi_pcm_from_id(struct hdac_hdmi_priv * hdmi,int pcm_idx)2822889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2832889099eSSubhransu S. Prusty 						int pcm_idx)
2842889099eSSubhransu S. Prusty {
2852889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2862889099eSSubhransu S. Prusty 
2872889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2882889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2892889099eSSubhransu S. Prusty 			return pcm;
2902889099eSSubhransu S. Prusty 	}
2912889099eSSubhransu S. Prusty 
2922889099eSSubhransu S. Prusty 	return NULL;
2932889099eSSubhransu S. Prusty }
2942889099eSSubhransu S. Prusty 
sad_format(const u8 * sad)2952428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2962428bca3SSubhransu S. Prusty {
2972428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2982428bca3SSubhransu S. Prusty }
2992428bca3SSubhransu S. Prusty 
sad_sample_bits_lpcm(const u8 * sad)3002428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
3012428bca3SSubhransu S. Prusty {
3022428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
3032428bca3SSubhransu S. Prusty }
3042428bca3SSubhransu S. Prusty 
hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime * runtime,void * eld)3052428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
3062428bca3SSubhransu S. Prusty 						void *eld)
3072428bca3SSubhransu S. Prusty {
3082428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3092428bca3SSubhransu S. Prusty 	int i;
3102428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3112428bca3SSubhransu S. Prusty 
3122428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3132428bca3SSubhransu S. Prusty 	if (!sad)
3142428bca3SSubhransu S. Prusty 		goto format_constraint;
3152428bca3SSubhransu S. Prusty 
3162428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3172428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3182428bca3SSubhransu S. Prusty 
3192428bca3SSubhransu S. Prusty 			/*
3202428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3212428bca3SSubhransu S. Prusty 			 * container so we set S32
3222428bca3SSubhransu S. Prusty 			 */
3232428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3242428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3252428bca3SSubhransu S. Prusty 		}
3262428bca3SSubhransu S. Prusty 	}
3272428bca3SSubhransu S. Prusty 
3282428bca3SSubhransu S. Prusty format_constraint:
3292428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3302428bca3SSubhransu S. Prusty 				formats);
3312428bca3SSubhransu S. Prusty 
3322428bca3SSubhransu S. Prusty }
3332428bca3SSubhransu S. Prusty 
334a657f1d0SSubhransu S. Prusty static void
hdac_hdmi_set_dip_index(struct hdac_device * hdev,hda_nid_t pin_nid,int packet_index,int byte_index)3353787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
336a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
337a657f1d0SSubhransu S. Prusty {
338a657f1d0SSubhransu S. Prusty 	int val;
339a657f1d0SSubhransu S. Prusty 
340a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3413787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
342a657f1d0SSubhransu S. Prusty }
343a657f1d0SSubhransu S. Prusty 
344478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
345478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
346478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
347478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
348478f544eSSubhransu S. Prusty 
349478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
350478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
351478f544eSSubhransu S. Prusty 	u8 CXT04;
352478f544eSSubhransu S. Prusty 	u8 CA;
353478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
354478f544eSSubhransu S. Prusty };
355478f544eSSubhransu S. Prusty 
hdac_hdmi_setup_audio_infoframe(struct hdac_device * hdev,struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port)3563787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
357754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
358a657f1d0SSubhransu S. Prusty {
359a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
360a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
361754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
362478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
3633787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
364ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
365478f544eSSubhransu S. Prusty 	u8 *dip;
366a657f1d0SSubhransu S. Prusty 	int ret;
367a657f1d0SSubhransu S. Prusty 	int i;
368478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
369478f544eSSubhransu S. Prusty 	u8 conn_type;
370bcced704SSubhransu S. Prusty 	int channels, ca;
371a657f1d0SSubhransu S. Prusty 
3723787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
373ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
374bcced704SSubhransu S. Prusty 
375bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
3763787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
377bcced704SSubhransu S. Prusty 
378bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
379ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
380bcced704SSubhransu S. Prusty 
381754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
382478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
383a657f1d0SSubhransu S. Prusty 
384478f544eSSubhransu S. Prusty 	switch (conn_type) {
385478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
386478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
387478f544eSSubhransu S. Prusty 
388478f544eSSubhransu S. Prusty 		frame.channels = channels;
389bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
390a657f1d0SSubhransu S. Prusty 
391a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
392a657f1d0SSubhransu S. Prusty 		if (ret < 0)
393a657f1d0SSubhransu S. Prusty 			return ret;
394a657f1d0SSubhransu S. Prusty 
395478f544eSSubhransu S. Prusty 		break;
396478f544eSSubhransu S. Prusty 
397478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
398478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
399478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
400478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
401478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
402478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
403bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
404478f544eSSubhransu S. Prusty 
405478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
406478f544eSSubhransu S. Prusty 		break;
407478f544eSSubhransu S. Prusty 
408478f544eSSubhransu S. Prusty 	default:
4093787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
410478f544eSSubhransu S. Prusty 		return -EIO;
411478f544eSSubhransu S. Prusty 	}
412478f544eSSubhransu S. Prusty 
413a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4143787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4153787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
416a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
417a657f1d0SSubhransu S. Prusty 
418a657f1d0SSubhransu S. Prusty 
419a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4203787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
421478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
422391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4233787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
424391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
425478f544eSSubhransu S. Prusty 	} else {
426478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4273787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
428478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
429478f544eSSubhransu S. Prusty 	}
430a657f1d0SSubhransu S. Prusty 
431a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4323787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4333787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
434a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
435a657f1d0SSubhransu S. Prusty 
436a657f1d0SSubhransu S. Prusty 	return 0;
437a657f1d0SSubhransu S. Prusty }
438a657f1d0SSubhransu S. Prusty 
hdac_hdmi_set_stream(struct snd_soc_dai * dai,void * stream,int direction)439*f6887a71SJason Montleon static int hdac_hdmi_set_stream(struct snd_soc_dai *dai,
440*f6887a71SJason Montleon 				void *stream, int direction)
441b0362adbSSubhransu S. Prusty {
4423787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4433787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
444754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
445c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
446*f6887a71SJason Montleon 	struct hdac_stream *hstream;
447c9bfb5d7SJeeja KP 
448*f6887a71SJason Montleon 	if (!stream)
449*f6887a71SJason Montleon 		return -EINVAL;
450*f6887a71SJason Montleon 
451*f6887a71SJason Montleon 	hstream = (struct hdac_stream *)stream;
452*f6887a71SJason Montleon 
453*f6887a71SJason Montleon 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, hstream->stream_tag);
454b0362adbSSubhransu S. Prusty 
455b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
456b0362adbSSubhransu S. Prusty 
457c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
458b0362adbSSubhransu S. Prusty 
459c9bfb5d7SJeeja KP 	if (pcm)
460*f6887a71SJason Montleon 		pcm->stream_tag = (hstream->stream_tag << 4);
461bcced704SSubhransu S. Prusty 
462c9bfb5d7SJeeja KP 	return 0;
463b0362adbSSubhransu S. Prusty }
464b0362adbSSubhransu S. Prusty 
hdac_hdmi_set_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hparams,struct snd_soc_dai * dai)465b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
466b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
467b0362adbSSubhransu S. Prusty {
4683787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
469754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
470c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
471c9bfb5d7SJeeja KP 	int format;
472b0362adbSSubhransu S. Prusty 
47354dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
474b0362adbSSubhransu S. Prusty 
475c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
476b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
47766d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
478b0362adbSSubhransu S. Prusty 
479c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
480c9bfb5d7SJeeja KP 	if (!pcm)
481148569fdSSubhransu S. Prusty 		return -EIO;
482148569fdSSubhransu S. Prusty 
483c9bfb5d7SJeeja KP 	pcm->format = format;
484c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
485148569fdSSubhransu S. Prusty 
486148569fdSSubhransu S. Prusty 	return 0;
487148569fdSSubhransu S. Prusty }
488148569fdSSubhransu S. Prusty 
hdac_hdmi_query_port_connlist(struct hdac_device * hdev,struct hdac_hdmi_pin * pin,struct hdac_hdmi_port * port)4893787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
490754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
491754695f9SJeeja KP 					struct hdac_hdmi_port *port)
492148569fdSSubhransu S. Prusty {
4933787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4943787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
495148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4963787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
497148569fdSSubhransu S. Prusty 		return -EINVAL;
498148569fdSSubhransu S. Prusty 	}
499148569fdSSubhransu S. Prusty 
5003787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
5011b46ebd1SJeeja KP 		return -EIO;
5021b46ebd1SJeeja KP 
5033787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
504754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
505754695f9SJeeja KP 	if (port->num_mux_nids == 0)
5063787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
507754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
508754695f9SJeeja KP 						pin->nid, port->id);
509148569fdSSubhransu S. Prusty 
5103787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
511754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
512148569fdSSubhransu S. Prusty 
513754695f9SJeeja KP 	return port->num_mux_nids;
514148569fdSSubhransu S. Prusty }
515148569fdSSubhransu S. Prusty 
516148569fdSSubhransu S. Prusty /*
517754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
518148569fdSSubhransu S. Prusty  *
519754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
520148569fdSSubhransu S. Prusty  *
521754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
522754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
523148569fdSSubhransu S. Prusty  * connected.
524148569fdSSubhransu S. Prusty  */
hdac_hdmi_get_port_from_cvt(struct hdac_device * hdev,struct hdac_hdmi_priv * hdmi,struct hdac_hdmi_cvt * cvt)525754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5263787a398SRakesh Ughreja 			struct hdac_device *hdev,
527148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
528148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
529148569fdSSubhransu S. Prusty {
530148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
531426b3bbcSPierre-Louis Bossart 	struct hdac_hdmi_port *port;
532148569fdSSubhransu S. Prusty 	int ret, i;
533148569fdSSubhransu S. Prusty 
534148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
535148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
536e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
537e0e5d3e5SJeeja KP 				continue;
538148569fdSSubhransu S. Prusty 
539e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
540e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5413787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
542e0e5d3e5SJeeja KP 							port->pin, port);
543e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
544148569fdSSubhransu S. Prusty 				if (ret < 0)
545e0e5d3e5SJeeja KP 					continue;
546148569fdSSubhransu S. Prusty 
547754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
548e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
549e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
550e0e5d3e5SJeeja KP 						port->eld.eld_valid)
551754695f9SJeeja KP 						return port;
552148569fdSSubhransu S. Prusty 				}
553148569fdSSubhransu S. Prusty 			}
554e0e5d3e5SJeeja KP 		}
555e0e5d3e5SJeeja KP 	}
556148569fdSSubhransu S. Prusty 
557148569fdSSubhransu S. Prusty 	return NULL;
558148569fdSSubhransu S. Prusty }
559148569fdSSubhransu S. Prusty 
56054dfa1eaSSubhransu S. Prusty /*
561489f231eSKai Vehmanen  * Go through all converters and ensure connection is set to
562489f231eSKai Vehmanen  * the correct pin as set via kcontrols.
563489f231eSKai Vehmanen  */
hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device * hdev)564489f231eSKai Vehmanen static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
565489f231eSKai Vehmanen {
566489f231eSKai Vehmanen 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
567489f231eSKai Vehmanen 	struct hdac_hdmi_port *port;
568489f231eSKai Vehmanen 	struct hdac_hdmi_cvt *cvt;
569489f231eSKai Vehmanen 	int cvt_idx = 0;
570489f231eSKai Vehmanen 
571489f231eSKai Vehmanen 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
572489f231eSKai Vehmanen 		port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
573489f231eSKai Vehmanen 		if (port && port->pin) {
574489f231eSKai Vehmanen 			snd_hdac_codec_write(hdev, port->pin->nid, 0,
575489f231eSKai Vehmanen 					     AC_VERB_SET_CONNECT_SEL, cvt_idx);
576489f231eSKai Vehmanen 			dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
577489f231eSKai Vehmanen 				__func__, cvt->name, port->pin->nid, cvt_idx);
578489f231eSKai Vehmanen 		}
579489f231eSKai Vehmanen 		++cvt_idx;
580489f231eSKai Vehmanen 	}
581489f231eSKai Vehmanen }
582489f231eSKai Vehmanen 
583489f231eSKai Vehmanen /*
58454dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
58554dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
58654dfa1eaSSubhransu S. Prusty  * doesn't fail.
58754dfa1eaSSubhransu S. Prusty  */
hdac_hdmi_pcm_open(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)588b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
589b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
590b0362adbSSubhransu S. Prusty {
5913787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5923787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
593754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
594148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
595754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5962428bca3SSubhransu S. Prusty 	int ret;
597b0362adbSSubhransu S. Prusty 
598b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
599b0362adbSSubhransu S. Prusty 
600148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
6013787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
60254dfa1eaSSubhransu S. Prusty 
60354dfa1eaSSubhransu S. Prusty 	/*
60454dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
60554dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
60654dfa1eaSSubhransu S. Prusty 	 */
607754695f9SJeeja KP 	if (!port)
60854dfa1eaSSubhransu S. Prusty 		return 0;
609754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
610754695f9SJeeja KP 			(!port->eld.eld_valid)) {
611b0362adbSSubhransu S. Prusty 
6123787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
613754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
614754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
615754695f9SJeeja KP 			port->pin->nid, port->id);
616b8a54545SSubhransu S. Prusty 
61754dfa1eaSSubhransu S. Prusty 		return 0;
618b0362adbSSubhransu S. Prusty 	}
619b0362adbSSubhransu S. Prusty 
620754695f9SJeeja KP 	dai_map->port = port;
621b0362adbSSubhransu S. Prusty 
6222428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
623754695f9SJeeja KP 				port->eld.eld_buffer);
6242428bca3SSubhransu S. Prusty 	if (ret < 0)
6252428bca3SSubhransu S. Prusty 		return ret;
626b0362adbSSubhransu S. Prusty 
6272428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
628754695f9SJeeja KP 				port->eld.eld_buffer);
629b0362adbSSubhransu S. Prusty }
630b0362adbSSubhransu S. Prusty 
hdac_hdmi_pcm_close(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)631b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
632b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
633b0362adbSSubhransu S. Prusty {
6343787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
635754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
636ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
637b0362adbSSubhransu S. Prusty 
638b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
639b0362adbSSubhransu S. Prusty 
640ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
641bcced704SSubhransu S. Prusty 
642ab1eea19SJeeja KP 	if (pcm) {
643ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
644ab1eea19SJeeja KP 		pcm->chmap_set = false;
645ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
646ab1eea19SJeeja KP 		pcm->channels = 0;
647ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
648b0362adbSSubhransu S. Prusty 	}
649ab1eea19SJeeja KP 
650754695f9SJeeja KP 	if (dai_map->port)
651754695f9SJeeja KP 		dai_map->port = NULL;
65254dfa1eaSSubhransu S. Prusty }
653b0362adbSSubhransu S. Prusty 
65418382eadSSubhransu S. Prusty static int
hdac_hdmi_query_cvt_params(struct hdac_device * hdev,struct hdac_hdmi_cvt * cvt)655f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
65618382eadSSubhransu S. Prusty {
657bcced704SSubhransu S. Prusty 	unsigned int chans;
658f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
65918382eadSSubhransu S. Prusty 	int err;
66018382eadSSubhransu S. Prusty 
661f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
662bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
663bcced704SSubhransu S. Prusty 
664bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
665bcced704SSubhransu S. Prusty 
666bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
667bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
668bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
66918382eadSSubhransu S. Prusty 
670f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
67118382eadSSubhransu S. Prusty 			&cvt->params.rates,
67218382eadSSubhransu S. Prusty 			&cvt->params.formats,
67318382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
67418382eadSSubhransu S. Prusty 	if (err < 0)
675f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
67618382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
67718382eadSSubhransu S. Prusty 			cvt->nid, err);
67818382eadSSubhransu S. Prusty 
67918382eadSSubhransu S. Prusty 	return err;
68018382eadSSubhransu S. Prusty }
68118382eadSSubhransu S. Prusty 
hdac_hdmi_fill_widget_info(struct device * dev,struct snd_soc_dapm_widget * w,enum snd_soc_dapm_type id,void * priv,const char * wname,const char * stream,struct snd_kcontrol_new * wc,int numkc,int (* event)(struct snd_soc_dapm_widget *,struct snd_kcontrol *,int),unsigned short event_flags)68279f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
683c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
684c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
685c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
686c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
687c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
68818382eadSSubhransu S. Prusty {
68918382eadSSubhransu S. Prusty 	w->id = id;
69079f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
69179f4e922SSubhransu S. Prusty 	if (!w->name)
69279f4e922SSubhransu S. Prusty 		return -ENOMEM;
69379f4e922SSubhransu S. Prusty 
69418382eadSSubhransu S. Prusty 	w->sname = stream;
69518382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
69618382eadSSubhransu S. Prusty 	w->shift = 0;
69779f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
69879f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
69979f4e922SSubhransu S. Prusty 	w->priv = priv;
700c9bfb5d7SJeeja KP 	w->event = event;
701c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
70279f4e922SSubhransu S. Prusty 
70379f4e922SSubhransu S. Prusty 	return 0;
70418382eadSSubhransu S. Prusty }
70518382eadSSubhransu S. Prusty 
hdac_hdmi_fill_route(struct snd_soc_dapm_route * route,const char * sink,const char * control,const char * src,int (* handler)(struct snd_soc_dapm_widget * src,struct snd_soc_dapm_widget * sink))70618382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
70779f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
70879f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
70979f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
71018382eadSSubhransu S. Prusty {
71118382eadSSubhransu S. Prusty 	route->sink = sink;
71218382eadSSubhransu S. Prusty 	route->source = src;
71318382eadSSubhransu S. Prusty 	route->control = control;
71479f4e922SSubhransu S. Prusty 	route->connected = handler;
71518382eadSSubhransu S. Prusty }
71618382eadSSubhransu S. Prusty 
hdac_hdmi_get_pcm(struct hdac_device * hdev,struct hdac_hdmi_port * port)7173787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
718754695f9SJeeja KP 					struct hdac_hdmi_port *port)
7194a3478deSJeeja KP {
7203787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
721426b3bbcSPierre-Louis Bossart 	struct hdac_hdmi_pcm *pcm;
722e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7234a3478deSJeeja KP 
7244a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
725e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
726754695f9SJeeja KP 			continue;
727754695f9SJeeja KP 
728e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
729e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7304a3478deSJeeja KP 				return pcm;
7314a3478deSJeeja KP 		}
732e0e5d3e5SJeeja KP 	}
7334a3478deSJeeja KP 
7344a3478deSJeeja KP 	return NULL;
7354a3478deSJeeja KP }
7364a3478deSJeeja KP 
hdac_hdmi_set_power_state(struct hdac_device * hdev,hda_nid_t nid,unsigned int pwr_state)7373787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
738c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
739c9bfb5d7SJeeja KP {
740753597fbSAbhijeet Kumar 	int count;
741753597fbSAbhijeet Kumar 	unsigned int state;
742753597fbSAbhijeet Kumar 
7433787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7443787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
745753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7463787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
747753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
748753597fbSAbhijeet Kumar 						pwr_state);
7493787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
750753597fbSAbhijeet Kumar 						nid, pwr_state);
751753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
752753597fbSAbhijeet Kumar 					break;
753753597fbSAbhijeet Kumar 			}
754753597fbSAbhijeet Kumar 		}
755c9bfb5d7SJeeja KP 	}
756c9bfb5d7SJeeja KP }
757c9bfb5d7SJeeja KP 
hdac_hdmi_set_amp(struct hdac_device * hdev,hda_nid_t nid,int val)7583787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
759c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
760c9bfb5d7SJeeja KP {
7613787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7623787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
763c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
764c9bfb5d7SJeeja KP }
765c9bfb5d7SJeeja KP 
766c9bfb5d7SJeeja KP 
hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)767c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
768c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
769c9bfb5d7SJeeja KP {
770754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7713787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
772c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
773c9bfb5d7SJeeja KP 
7743787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
775c9bfb5d7SJeeja KP 			__func__, w->name, event);
776c9bfb5d7SJeeja KP 
7773787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
778c9bfb5d7SJeeja KP 	if (!pcm)
779c9bfb5d7SJeeja KP 		return -EIO;
780c9bfb5d7SJeeja KP 
7811b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7823787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7831b46ebd1SJeeja KP 		return -EIO;
7841b46ebd1SJeeja KP 
785c9bfb5d7SJeeja KP 	switch (event) {
786c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7873787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
788c9bfb5d7SJeeja KP 
789c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7903787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
791c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
792c9bfb5d7SJeeja KP 
7933787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
794c9bfb5d7SJeeja KP 
7953787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
796c9bfb5d7SJeeja KP 
797c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7983787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
799c9bfb5d7SJeeja KP 
800c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
8013787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
802c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
803c9bfb5d7SJeeja KP 
8043787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
805c9bfb5d7SJeeja KP 		break;
806c9bfb5d7SJeeja KP 
807c9bfb5d7SJeeja KP 	}
808c9bfb5d7SJeeja KP 
809c9bfb5d7SJeeja KP 	return 0;
810c9bfb5d7SJeeja KP }
811c9bfb5d7SJeeja KP 
hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)812c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
813c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
814c9bfb5d7SJeeja KP {
815c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
8163787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
8173787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
818c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
819c9bfb5d7SJeeja KP 
8203787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
821c9bfb5d7SJeeja KP 			__func__, w->name, event);
822c9bfb5d7SJeeja KP 
823c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
824c9bfb5d7SJeeja KP 	if (!pcm)
825c9bfb5d7SJeeja KP 		return -EIO;
826c9bfb5d7SJeeja KP 
827c9bfb5d7SJeeja KP 	switch (event) {
828c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
8293787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
830c9bfb5d7SJeeja KP 
831c9bfb5d7SJeeja KP 		/* Enable transmission */
8323787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
833c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
834c9bfb5d7SJeeja KP 
835c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8363787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
837c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
838c9bfb5d7SJeeja KP 
8393787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
840c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8413787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
842c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
843489f231eSKai Vehmanen 
844489f231eSKai Vehmanen 		/*
845489f231eSKai Vehmanen 		 * The connection indices are shared by all converters and
846489f231eSKai Vehmanen 		 * may interfere with each other. Ensure correct
847489f231eSKai Vehmanen 		 * routing for all converters at stream start.
848489f231eSKai Vehmanen 		 */
849489f231eSKai Vehmanen 		hdac_hdmi_verify_connect_sel_all_pins(hdev);
850489f231eSKai Vehmanen 
851c9bfb5d7SJeeja KP 		break;
852c9bfb5d7SJeeja KP 
853c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8543787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
855c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8563787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
857c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
858c9bfb5d7SJeeja KP 
8593787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
860c9bfb5d7SJeeja KP 		break;
861c9bfb5d7SJeeja KP 
862c9bfb5d7SJeeja KP 	}
863c9bfb5d7SJeeja KP 
864c9bfb5d7SJeeja KP 	return 0;
865c9bfb5d7SJeeja KP }
866c9bfb5d7SJeeja KP 
hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)867c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
868c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
869c9bfb5d7SJeeja KP {
870754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8713787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
872c9bfb5d7SJeeja KP 	int mux_idx;
873c9bfb5d7SJeeja KP 
8743787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
875c9bfb5d7SJeeja KP 			__func__, w->name, event);
876c9bfb5d7SJeeja KP 
877c9bfb5d7SJeeja KP 	if (!kc)
878c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
879c9bfb5d7SJeeja KP 
880c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8811b46ebd1SJeeja KP 
8821b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8833787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8841b46ebd1SJeeja KP 		return -EIO;
8851b46ebd1SJeeja KP 
886c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8873787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
888c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
889c9bfb5d7SJeeja KP 	}
890c9bfb5d7SJeeja KP 
891c9bfb5d7SJeeja KP 	return 0;
892c9bfb5d7SJeeja KP }
893c9bfb5d7SJeeja KP 
8944a3478deSJeeja KP /*
8954a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8964a3478deSJeeja KP  */
hdac_hdmi_set_pin_port_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)897754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8984a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8994a3478deSJeeja KP {
9004a3478deSJeeja KP 	int ret;
901e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
9024a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
9034a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
9044a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
905754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
9063787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
9073787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
908426b3bbcSPierre-Louis Bossart 	struct hdac_hdmi_pcm *pcm;
9094a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
9104a3478deSJeeja KP 
9114a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
9124a3478deSJeeja KP 	if (ret < 0)
9134a3478deSJeeja KP 		return ret;
9144a3478deSJeeja KP 
915754695f9SJeeja KP 	if (port == NULL)
916754695f9SJeeja KP 		return -EINVAL;
917754695f9SJeeja KP 
9184a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
9194a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
920e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
921e0e5d3e5SJeeja KP 			continue;
922e0e5d3e5SJeeja KP 
923e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
924e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
925e0e5d3e5SJeeja KP 					p->pin == port->pin) {
926332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, false);
927e0e5d3e5SJeeja KP 				list_del(&p->head);
928e0e5d3e5SJeeja KP 			}
929e0e5d3e5SJeeja KP 		}
930e0e5d3e5SJeeja KP 	}
9314a3478deSJeeja KP 
9324a3478deSJeeja KP 	/*
9334a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
9344a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
9354a3478deSJeeja KP 	 */
936e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
937e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
938e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
939754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
940332ccf00STakashi Iwai 				hdac_hdmi_jack_report_sync(pcm, port, true);
9414a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9424a3478deSJeeja KP 				return ret;
9434a3478deSJeeja KP 			}
9444a3478deSJeeja KP 		}
945e0e5d3e5SJeeja KP 	}
9464a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9474a3478deSJeeja KP 
9484a3478deSJeeja KP 	return ret;
9494a3478deSJeeja KP }
9504a3478deSJeeja KP 
95179f4e922SSubhransu S. Prusty /*
95279f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
95379f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
95479f4e922SSubhransu S. Prusty  * widget runtime.
95579f4e922SSubhransu S. Prusty  *
95679f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
95779f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
95879f4e922SSubhransu S. Prusty  * "NONE"
95979f4e922SSubhransu S. Prusty  */
hdac_hdmi_create_pin_port_muxs(struct hdac_device * hdev,struct hdac_hdmi_port * port,struct snd_soc_dapm_widget * widget,const char * widget_name)9603787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
961754695f9SJeeja KP 				struct hdac_hdmi_port *port,
96279f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
96379f4e922SSubhransu S. Prusty 				const char *widget_name)
96418382eadSSubhransu S. Prusty {
9653787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
966754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
96779f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
96879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
96979f4e922SSubhransu S. Prusty 	struct soc_enum *se;
97079f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
97179f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
97279f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
97379f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
97479f4e922SSubhransu S. Prusty 	int i = 0;
97579f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
97618382eadSSubhransu S. Prusty 
9773787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
97879f4e922SSubhransu S. Prusty 	if (!kc)
97979f4e922SSubhransu S. Prusty 		return -ENOMEM;
98018382eadSSubhransu S. Prusty 
9813787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
98279f4e922SSubhransu S. Prusty 	if (!se)
98379f4e922SSubhransu S. Prusty 		return -ENOMEM;
98418382eadSSubhransu S. Prusty 
98570e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
98670e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9873787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
98879f4e922SSubhransu S. Prusty 	if (!kc->name)
98979f4e922SSubhransu S. Prusty 		return -ENOMEM;
99018382eadSSubhransu S. Prusty 
99179f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
99279f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
99379f4e922SSubhransu S. Prusty 	kc->access = 0;
99479f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
995754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
99679f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
99779f4e922SSubhransu S. Prusty 
99879f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
99979f4e922SSubhransu S. Prusty 
100079f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
100179f4e922SSubhransu S. Prusty 	se->items = num_items;
100279f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
100379f4e922SSubhransu S. Prusty 
100479f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
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 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
101079f4e922SSubhransu S. Prusty 		i++;
101179f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
10123787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
101379f4e922SSubhransu S. Prusty 		if (!items[i])
101479f4e922SSubhransu S. Prusty 			return -ENOMEM;
101579f4e922SSubhransu S. Prusty 	}
101679f4e922SSubhransu S. Prusty 
10173787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
101879f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
101979f4e922SSubhransu S. Prusty 	if (!se->texts)
102079f4e922SSubhransu S. Prusty 		return -ENOMEM;
102179f4e922SSubhransu S. Prusty 
10223787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1023754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1024c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
1025c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
102679f4e922SSubhransu S. Prusty }
102779f4e922SSubhransu S. Prusty 
102879f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
hdac_hdmi_add_pinmux_cvt_route(struct hdac_device * hdev,struct snd_soc_dapm_widget * widgets,struct snd_soc_dapm_route * route,int rindex)10293787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
103079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
103179f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
103279f4e922SSubhransu S. Prusty {
10333787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
103479f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
103579f4e922SSubhransu S. Prusty 	struct soc_enum *se;
1036754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
103779f4e922SSubhransu S. Prusty 	int i, j;
103879f4e922SSubhransu S. Prusty 
1039754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
104079f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
104179f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
104279f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
104379f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
104479f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
104579f4e922SSubhransu S. Prusty 					se->texts[j + 1],
104679f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
104779f4e922SSubhransu S. Prusty 
104879f4e922SSubhransu S. Prusty 			rindex++;
104979f4e922SSubhransu S. Prusty 		}
105079f4e922SSubhransu S. Prusty 
105179f4e922SSubhransu S. Prusty 		mux_index++;
105279f4e922SSubhransu S. Prusty 	}
105379f4e922SSubhransu S. Prusty }
105479f4e922SSubhransu S. Prusty 
105579f4e922SSubhransu S. Prusty /*
105679f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
105779f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1058754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1059754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
106079f4e922SSubhransu S. Prusty  *
1061754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1062754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
106379f4e922SSubhransu S. Prusty  *
106479f4e922SSubhransu S. Prusty  * Routes are added as below:
1065754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1066754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
106779f4e922SSubhransu S. Prusty  *
106879f4e922SSubhransu S. Prusty  * Total route elements:
1069754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
107079f4e922SSubhransu S. Prusty  */
create_fill_widget_route_map(struct snd_soc_dapm_context * dapm)107179f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
107279f4e922SSubhransu S. Prusty {
107379f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
107479f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10753787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10763787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10771e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
107879f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
107979f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
108079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1081754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
108279f4e922SSubhransu S. Prusty 
108379f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
108479f4e922SSubhransu S. Prusty 		return -EINVAL;
108579f4e922SSubhransu S. Prusty 
1086754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1087754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
108879f4e922SSubhransu S. Prusty 				GFP_KERNEL);
108979f4e922SSubhransu S. Prusty 
109079f4e922SSubhransu S. Prusty 	if (!widgets)
109179f4e922SSubhransu S. Prusty 		return -ENOMEM;
109279f4e922SSubhransu S. Prusty 
109379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
109479f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
109579f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
109679f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1097c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1098c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1099c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1100c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
110179f4e922SSubhransu S. Prusty 		if (ret < 0)
110279f4e922SSubhransu S. Prusty 			return ret;
110379f4e922SSubhransu S. Prusty 		i++;
110479f4e922SSubhransu S. Prusty 	}
110579f4e922SSubhransu S. Prusty 
110679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1107754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1108754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1109754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
111079f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1111754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1112c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1113c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1114754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1115754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
111679f4e922SSubhransu S. Prusty 			if (ret < 0)
111779f4e922SSubhransu S. Prusty 				return ret;
11180324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
111979f4e922SSubhransu S. Prusty 			i++;
112079f4e922SSubhransu S. Prusty 		}
1121754695f9SJeeja KP 	}
112279f4e922SSubhransu S. Prusty 
112379f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
112479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1125754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1126754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1127754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
11283787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1129754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
113079f4e922SSubhransu S. Prusty 						widget_name);
113179f4e922SSubhransu S. Prusty 			if (ret < 0)
113279f4e922SSubhransu S. Prusty 				return ret;
113379f4e922SSubhransu S. Prusty 			i++;
113479f4e922SSubhransu S. Prusty 
113579f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
113679f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
113779f4e922SSubhransu S. Prusty 
113879f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
113979f4e922SSubhransu S. Prusty 			num_routes++;
114079f4e922SSubhransu S. Prusty 		}
1141754695f9SJeeja KP 	}
114279f4e922SSubhransu S. Prusty 
114379f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
114479f4e922SSubhransu S. Prusty 							GFP_KERNEL);
114579f4e922SSubhransu S. Prusty 	if (!route)
114679f4e922SSubhransu S. Prusty 		return -ENOMEM;
114779f4e922SSubhransu S. Prusty 
114879f4e922SSubhransu S. Prusty 	i = 0;
114979f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
115079f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1151754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
115279f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1153754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1154754695f9SJeeja KP 						hdmi->num_pin;
115579f4e922SSubhransu S. Prusty 
115679f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
115779f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
115879f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
115979f4e922SSubhransu S. Prusty 			i++;
1160754695f9SJeeja KP 		}
116179f4e922SSubhransu S. Prusty 	}
116279f4e922SSubhransu S. Prusty 
11633787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
116479f4e922SSubhransu S. Prusty 
116579f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1166754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
116779f4e922SSubhransu S. Prusty 
116879f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
116979f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
117079f4e922SSubhransu S. Prusty 
117179f4e922SSubhransu S. Prusty 	return 0;
117279f4e922SSubhransu S. Prusty 
117318382eadSSubhransu S. Prusty }
117418382eadSSubhransu S. Prusty 
hdac_hdmi_init_dai_map(struct hdac_device * hdev)11753787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
117618382eadSSubhransu S. Prusty {
11773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1178754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
117915b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1180148569fdSSubhransu S. Prusty 	int dai_id = 0;
118118382eadSSubhransu S. Prusty 
1182148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
118315b91447SSubhransu S. Prusty 		return -EINVAL;
118418382eadSSubhransu S. Prusty 
1185148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1186148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1187148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
118815b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
118918382eadSSubhransu S. Prusty 
1190148569fdSSubhransu S. Prusty 		dai_id++;
1191148569fdSSubhransu S. Prusty 
1192148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11933787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1194148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1195148569fdSSubhransu S. Prusty 			break;
1196148569fdSSubhransu S. Prusty 		}
1197148569fdSSubhransu S. Prusty 	}
119818382eadSSubhransu S. Prusty 
119915b91447SSubhransu S. Prusty 	return 0;
120015b91447SSubhransu S. Prusty }
120115b91447SSubhransu S. Prusty 
hdac_hdmi_add_cvt(struct hdac_device * hdev,hda_nid_t nid)12023787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
120315b91447SSubhransu S. Prusty {
12043787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
120515b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12064a3478deSJeeja KP 	char name[NAME_SIZE];
120715b91447SSubhransu S. Prusty 
1208c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
120915b91447SSubhransu S. Prusty 	if (!cvt)
121015b91447SSubhransu S. Prusty 		return -ENOMEM;
121115b91447SSubhransu S. Prusty 
121215b91447SSubhransu S. Prusty 	cvt->nid = nid;
12134a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1214c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1215c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1216c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
121715b91447SSubhransu S. Prusty 
121815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
121915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
122015b91447SSubhransu S. Prusty 
12213787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
122215b91447SSubhransu S. Prusty }
122315b91447SSubhransu S. Prusty 
hdac_hdmi_parse_eld(struct hdac_device * hdev,struct hdac_hdmi_port * port)12243787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1225754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1226b7756edeSSubhransu S. Prusty {
1227f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1228f6fa11a3SSandeep Tayal 
1229754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1230f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1231f6fa11a3SSandeep Tayal 
1232f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
12333787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1234f6fa11a3SSandeep Tayal 		return -EINVAL;
1235b7756edeSSubhransu S. Prusty 	}
1236b7756edeSSubhransu S. Prusty 
1237754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1238f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1239f6fa11a3SSandeep Tayal 
1240f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
12413787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1242f6fa11a3SSandeep Tayal 		return -EINVAL;
1243f6fa11a3SSandeep Tayal 	}
1244f6fa11a3SSandeep Tayal 
1245754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1246f6fa11a3SSandeep Tayal 
1247f6fa11a3SSandeep Tayal 	return 0;
1248f6fa11a3SSandeep Tayal }
1249f6fa11a3SSandeep Tayal 
hdac_hdmi_present_sense(struct hdac_hdmi_pin * pin,struct hdac_hdmi_port * port)1250754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1251754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1252b8a54545SSubhransu S. Prusty {
12533787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12543787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12554a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1256754695f9SJeeja KP 	int size = 0;
12572acd8309SJeeja KP 	int port_id = -1;
12581f53bcb3SBrent Lu 	bool eld_valid, eld_changed;
1259754695f9SJeeja KP 
1260754695f9SJeeja KP 	if (!hdmi)
1261754695f9SJeeja KP 		return;
12624a3478deSJeeja KP 
12632acd8309SJeeja KP 	/*
12642acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12652acd8309SJeeja KP 	 * to be -1.
12662acd8309SJeeja KP 	 */
12674a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1268754695f9SJeeja KP 	port->eld.monitor_present = false;
1269f6fa11a3SSandeep Tayal 
12702acd8309SJeeja KP 	if (pin->mst_capable)
12712acd8309SJeeja KP 		port_id = port->id;
12722acd8309SJeeja KP 
12733787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1274754695f9SJeeja KP 				&port->eld.monitor_present,
1275754695f9SJeeja KP 				port->eld.eld_buffer,
1276f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1277f6fa11a3SSandeep Tayal 
1278f6fa11a3SSandeep Tayal 	if (size > 0) {
1279f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12803787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1281f6fa11a3SSandeep Tayal 			size = -EINVAL;
1282f6fa11a3SSandeep Tayal 	}
1283f6fa11a3SSandeep Tayal 
12841f53bcb3SBrent Lu 	eld_valid = port->eld.eld_valid;
12851f53bcb3SBrent Lu 
1286f6fa11a3SSandeep Tayal 	if (size > 0) {
1287754695f9SJeeja KP 		port->eld.eld_valid = true;
1288754695f9SJeeja KP 		port->eld.eld_size = size;
1289f6fa11a3SSandeep Tayal 	} else {
1290754695f9SJeeja KP 		port->eld.eld_valid = false;
1291754695f9SJeeja KP 		port->eld.eld_size = 0;
1292f6fa11a3SSandeep Tayal 	}
1293b8a54545SSubhransu S. Prusty 
12941f53bcb3SBrent Lu 	eld_changed = (eld_valid != port->eld.eld_valid);
12951f53bcb3SBrent Lu 
12963787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12974a3478deSJeeja KP 
1298754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1299b8a54545SSubhransu S. Prusty 
13003787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1301754695f9SJeeja KP 						__func__, pin->nid, port->id);
13024a3478deSJeeja KP 
13034a3478deSJeeja KP 		/*
13044a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
13054a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
13064a3478deSJeeja KP 		 * control select.
13074a3478deSJeeja KP 		 */
1308332ccf00STakashi Iwai 		if (pcm) {
1309e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
1310332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1311332ccf00STakashi Iwai 		}
13124a3478deSJeeja KP 
13134a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1314f6fa11a3SSandeep Tayal 		return;
1315b8a54545SSubhransu S. Prusty 	}
1316b8a54545SSubhransu S. Prusty 
1317754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1318332ccf00STakashi Iwai 		if (pcm) {
1319e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
1320332ccf00STakashi Iwai 			schedule_work(&port->dapm_work);
1321332ccf00STakashi Iwai 		}
13224a3478deSJeeja KP 
1323f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1324754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1325754695f9SJeeja KP 
1326754695f9SJeeja KP 	}
1327754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
13281f53bcb3SBrent Lu 
13291f53bcb3SBrent Lu 	if (eld_changed && pcm)
13301f53bcb3SBrent Lu 		snd_ctl_notify(hdmi->card,
13311f53bcb3SBrent Lu 			       SNDRV_CTL_EVENT_MASK_VALUE |
13321f53bcb3SBrent Lu 			       SNDRV_CTL_EVENT_MASK_INFO,
13331f53bcb3SBrent Lu 			       &pcm->eld_ctl->id);
13344a3478deSJeeja KP }
13354a3478deSJeeja KP 
hdac_hdmi_add_ports(struct hdac_device * hdev,struct hdac_hdmi_pin * pin)1336c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1337754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1338754695f9SJeeja KP {
1339754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1340754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1341754695f9SJeeja KP 	int i;
1342754695f9SJeeja KP 
1343754695f9SJeeja KP 	/*
1344754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1345754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1346754695f9SJeeja KP 	 * implemented.
1347754695f9SJeeja KP 	 */
1348754695f9SJeeja KP 
1349c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1350754695f9SJeeja KP 	if (!ports)
1351754695f9SJeeja KP 		return -ENOMEM;
1352754695f9SJeeja KP 
1353754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1354754695f9SJeeja KP 		ports[i].id = i;
1355754695f9SJeeja KP 		ports[i].pin = pin;
1356332ccf00STakashi Iwai 		INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1357754695f9SJeeja KP 	}
1358754695f9SJeeja KP 	pin->ports = ports;
1359754695f9SJeeja KP 	pin->num_ports = max_ports;
1360754695f9SJeeja KP 	return 0;
1361b8a54545SSubhransu S. Prusty }
1362b8a54545SSubhransu S. Prusty 
hdac_hdmi_add_pin(struct hdac_device * hdev,hda_nid_t nid)13633787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
136415b91447SSubhransu S. Prusty {
13653787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
136615b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1367754695f9SJeeja KP 	int ret;
136815b91447SSubhransu S. Prusty 
1369c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
137015b91447SSubhransu S. Prusty 	if (!pin)
137115b91447SSubhransu S. Prusty 		return -ENOMEM;
137215b91447SSubhransu S. Prusty 
137315b91447SSubhransu S. Prusty 	pin->nid = nid;
13742acd8309SJeeja KP 	pin->mst_capable = false;
13753787a398SRakesh Ughreja 	pin->hdev = hdev;
1376c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1377754695f9SJeeja KP 	if (ret < 0)
1378754695f9SJeeja KP 		return ret;
137915b91447SSubhransu S. Prusty 
138015b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
138115b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1382754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1383b8a54545SSubhransu S. Prusty 
138415b91447SSubhransu S. Prusty 	return 0;
138518382eadSSubhransu S. Prusty }
138618382eadSSubhransu S. Prusty 
1387573892b6SKai Vehmanen #define INTEL_VENDOR_NID 0x08
1388573892b6SKai Vehmanen #define INTEL_GLK_VENDOR_NID 0x0b
1389211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1390211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1391211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1392211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1393211caab7SSubhransu S. Prusty 
hdac_hdmi_skl_enable_all_pins(struct hdac_device * hdev)1394f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(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_ALL_PIN_CVTS)
1403211caab7SSubhransu S. Prusty 		return;
1404211caab7SSubhransu S. Prusty 
1405211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1406f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1407211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1408211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1409211caab7SSubhransu S. Prusty 		return;
1410211caab7SSubhransu S. Prusty }
1411211caab7SSubhransu S. Prusty 
hdac_hdmi_skl_enable_dp12(struct hdac_device * hdev)1412f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1413211caab7SSubhransu S. Prusty {
1414211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1415f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
14165622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1417211caab7SSubhransu S. Prusty 
1418f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1419211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1420211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1421211caab7SSubhransu S. Prusty 		return;
1422211caab7SSubhransu S. Prusty 
1423211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1424211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1425f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1426211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1427211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1428211caab7SSubhransu S. Prusty 		return;
1429211caab7SSubhransu S. Prusty 
1430211caab7SSubhransu S. Prusty }
1431211caab7SSubhransu S. Prusty 
hdac_hdmi_eld_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)14321f53bcb3SBrent Lu static int hdac_hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
14331f53bcb3SBrent Lu 			     struct snd_ctl_elem_info *uinfo)
14341f53bcb3SBrent Lu {
14351f53bcb3SBrent Lu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
14361f53bcb3SBrent Lu 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
14371f53bcb3SBrent Lu 	struct hdac_hdmi_pcm *pcm;
14381f53bcb3SBrent Lu 	struct hdac_hdmi_port *port;
14391f53bcb3SBrent Lu 	struct hdac_hdmi_eld *eld;
14401f53bcb3SBrent Lu 
14411f53bcb3SBrent Lu 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
14421f53bcb3SBrent Lu 	uinfo->count = 0;
14431f53bcb3SBrent Lu 
14441f53bcb3SBrent Lu 	pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
14451f53bcb3SBrent Lu 	if (!pcm) {
14461f53bcb3SBrent Lu 		dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
14471f53bcb3SBrent Lu 			kcontrol->id.device);
14481f53bcb3SBrent Lu 		return 0;
14491f53bcb3SBrent Lu 	}
14501f53bcb3SBrent Lu 
14511f53bcb3SBrent Lu 	if (list_empty(&pcm->port_list)) {
14521f53bcb3SBrent Lu 		dev_dbg(component->dev, "%s: empty port list, device %d\n",
14531f53bcb3SBrent Lu 			__func__, kcontrol->id.device);
14541f53bcb3SBrent Lu 		return 0;
14551f53bcb3SBrent Lu 	}
14561f53bcb3SBrent Lu 
14571f53bcb3SBrent Lu 	mutex_lock(&hdmi->pin_mutex);
14581f53bcb3SBrent Lu 
14591f53bcb3SBrent Lu 	list_for_each_entry(port, &pcm->port_list, head) {
14601f53bcb3SBrent Lu 		eld = &port->eld;
14611f53bcb3SBrent Lu 
14621f53bcb3SBrent Lu 		if (eld->eld_valid) {
14631f53bcb3SBrent Lu 			uinfo->count = eld->eld_size;
14641f53bcb3SBrent Lu 			break;
14651f53bcb3SBrent Lu 		}
14661f53bcb3SBrent Lu 	}
14671f53bcb3SBrent Lu 
14681f53bcb3SBrent Lu 	mutex_unlock(&hdmi->pin_mutex);
14691f53bcb3SBrent Lu 
14701f53bcb3SBrent Lu 	return 0;
14711f53bcb3SBrent Lu }
14721f53bcb3SBrent Lu 
hdac_hdmi_eld_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)14731f53bcb3SBrent Lu static int hdac_hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
14741f53bcb3SBrent Lu 			    struct snd_ctl_elem_value *ucontrol)
14751f53bcb3SBrent Lu {
14761f53bcb3SBrent Lu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
14771f53bcb3SBrent Lu 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
14781f53bcb3SBrent Lu 	struct hdac_hdmi_pcm *pcm;
14791f53bcb3SBrent Lu 	struct hdac_hdmi_port *port;
14801f53bcb3SBrent Lu 	struct hdac_hdmi_eld *eld;
14811f53bcb3SBrent Lu 
148267207297SDan Carpenter 	memset(ucontrol->value.bytes.data, 0, sizeof(ucontrol->value.bytes.data));
14831f53bcb3SBrent Lu 
14841f53bcb3SBrent Lu 	pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
14851f53bcb3SBrent Lu 	if (!pcm) {
14861f53bcb3SBrent Lu 		dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
14871f53bcb3SBrent Lu 			kcontrol->id.device);
14881f53bcb3SBrent Lu 		return 0;
14891f53bcb3SBrent Lu 	}
14901f53bcb3SBrent Lu 
14911f53bcb3SBrent Lu 	if (list_empty(&pcm->port_list)) {
14921f53bcb3SBrent Lu 		dev_dbg(component->dev, "%s: empty port list, device %d\n",
14931f53bcb3SBrent Lu 			__func__, kcontrol->id.device);
14941f53bcb3SBrent Lu 		return 0;
14951f53bcb3SBrent Lu 	}
14961f53bcb3SBrent Lu 
14971f53bcb3SBrent Lu 	mutex_lock(&hdmi->pin_mutex);
14981f53bcb3SBrent Lu 
14991f53bcb3SBrent Lu 	list_for_each_entry(port, &pcm->port_list, head) {
15001f53bcb3SBrent Lu 		eld = &port->eld;
15011f53bcb3SBrent Lu 
15021f53bcb3SBrent Lu 		if (!eld->eld_valid)
15031f53bcb3SBrent Lu 			continue;
15041f53bcb3SBrent Lu 
15051f53bcb3SBrent Lu 		if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
15061f53bcb3SBrent Lu 		    eld->eld_size > ELD_MAX_SIZE) {
15071f53bcb3SBrent Lu 			mutex_unlock(&hdmi->pin_mutex);
15081f53bcb3SBrent Lu 
15091f53bcb3SBrent Lu 			dev_err(component->dev, "%s: buffer too small, device %d eld_size %d\n",
15101f53bcb3SBrent Lu 				__func__, kcontrol->id.device, eld->eld_size);
15111f53bcb3SBrent Lu 			snd_BUG();
15121f53bcb3SBrent Lu 			return -EINVAL;
15131f53bcb3SBrent Lu 		}
15141f53bcb3SBrent Lu 
15151f53bcb3SBrent Lu 		memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
15161f53bcb3SBrent Lu 		       eld->eld_size);
15171f53bcb3SBrent Lu 		break;
15181f53bcb3SBrent Lu 	}
15191f53bcb3SBrent Lu 
15201f53bcb3SBrent Lu 	mutex_unlock(&hdmi->pin_mutex);
15211f53bcb3SBrent Lu 
15221f53bcb3SBrent Lu 	return 0;
15231f53bcb3SBrent Lu }
15241f53bcb3SBrent Lu 
hdac_hdmi_create_eld_ctl(struct snd_soc_component * component,struct hdac_hdmi_pcm * pcm)15251f53bcb3SBrent Lu static int hdac_hdmi_create_eld_ctl(struct snd_soc_component *component, struct hdac_hdmi_pcm *pcm)
15261f53bcb3SBrent Lu {
15271f53bcb3SBrent Lu 	struct snd_kcontrol *kctl;
15281f53bcb3SBrent Lu 	struct snd_kcontrol_new hdmi_eld_ctl = {
15291f53bcb3SBrent Lu 		.access	= SNDRV_CTL_ELEM_ACCESS_READ |
15301f53bcb3SBrent Lu 			  SNDRV_CTL_ELEM_ACCESS_VOLATILE,
15311f53bcb3SBrent Lu 		.iface	= SNDRV_CTL_ELEM_IFACE_PCM,
15321f53bcb3SBrent Lu 		.name	= "ELD",
15331f53bcb3SBrent Lu 		.info	= hdac_hdmi_eld_ctl_info,
15341f53bcb3SBrent Lu 		.get	= hdac_hdmi_eld_ctl_get,
15351f53bcb3SBrent Lu 		.device	= pcm->pcm_id,
15361f53bcb3SBrent Lu 	};
15371f53bcb3SBrent Lu 
15381f53bcb3SBrent Lu 	/* add ELD ctl with the device number corresponding to the PCM stream */
15391f53bcb3SBrent Lu 	kctl = snd_ctl_new1(&hdmi_eld_ctl, component);
15401f53bcb3SBrent Lu 	if (!kctl)
15411f53bcb3SBrent Lu 		return -ENOMEM;
15421f53bcb3SBrent Lu 
15431f53bcb3SBrent Lu 	pcm->eld_ctl = kctl;
15441f53bcb3SBrent Lu 
15451f53bcb3SBrent Lu 	return snd_ctl_add(component->card->snd_card, kctl);
15461f53bcb3SBrent Lu }
15471f53bcb3SBrent Lu 
154861b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
154917a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
155017a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
155117a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1552*f6887a71SJason Montleon 	.set_stream = hdac_hdmi_set_stream,
155317a42c45SSubhransu S. Prusty };
155417a42c45SSubhransu S. Prusty 
155517a42c45SSubhransu S. Prusty /*
155617a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
155717a42c45SSubhransu S. Prusty  * based on the number of converter queried.
155817a42c45SSubhransu S. Prusty  */
hdac_hdmi_create_dais(struct hdac_device * hdev,struct snd_soc_dai_driver ** dais,struct hdac_hdmi_priv * hdmi,int num_dais)1559f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
156017a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
156117a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
156217a42c45SSubhransu S. Prusty {
156317a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
156417a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
156517a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
156617a42c45SSubhransu S. Prusty 	int i = 0;
156717a42c45SSubhransu S. Prusty 	u32 rates, bps;
156817a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
156917a42c45SSubhransu S. Prusty 	u64 formats;
157017a42c45SSubhransu S. Prusty 	int ret;
157117a42c45SSubhransu S. Prusty 
1572f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
157317a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
157417a42c45SSubhransu S. Prusty 			GFP_KERNEL);
157517a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
157617a42c45SSubhransu S. Prusty 		return -ENOMEM;
157717a42c45SSubhransu S. Prusty 
157817a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1579f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
158017a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
158117a42c45SSubhransu S. Prusty 		if (ret)
158217a42c45SSubhransu S. Prusty 			return ret;
158317a42c45SSubhransu S. Prusty 
15843b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
15853b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
15863b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
15873b857472SYong Zhi 		if (!rates)
15883b857472SYong Zhi 			return -EINVAL;
15893b857472SYong Zhi 
159017a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1591f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
159217a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
159317a42c45SSubhransu S. Prusty 
159417a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
159517a42c45SSubhransu S. Prusty 			return -ENOMEM;
159617a42c45SSubhransu S. Prusty 
159717a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
159817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1599f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
160017a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
160117a42c45SSubhransu S. Prusty 			return -ENOMEM;
160217a42c45SSubhransu S. Prusty 
160317a42c45SSubhransu S. Prusty 		/*
160417a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
160517a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
160617a42c45SSubhransu S. Prusty 		 */
160717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
160817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
160917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
161017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
161117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
161217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
161366d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
161417a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
161517a42c45SSubhransu S. Prusty 		i++;
161617a42c45SSubhransu S. Prusty 	}
161717a42c45SSubhransu S. Prusty 
161817a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
16191e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
162017a42c45SSubhransu S. Prusty 
162117a42c45SSubhransu S. Prusty 	return 0;
162217a42c45SSubhransu S. Prusty }
162317a42c45SSubhransu S. Prusty 
162418382eadSSubhransu S. Prusty /*
162518382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
162618382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
162718382eadSSubhransu S. Prusty  */
hdac_hdmi_parse_and_map_nid(struct hdac_device * hdev,struct snd_soc_dai_driver ** dais,int * num_dais)16283787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
162917a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
163018382eadSSubhransu S. Prusty {
163118382eadSSubhransu S. Prusty 	hda_nid_t nid;
16323c83ac23SSudip Mukherjee 	int i, num_nodes;
16333787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
163415b91447SSubhransu S. Prusty 	int ret;
163518382eadSSubhransu S. Prusty 
1636f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1637f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1638211caab7SSubhransu S. Prusty 
1639f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1640541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1641f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
164218382eadSSubhransu S. Prusty 		return -EINVAL;
164318382eadSSubhransu S. Prusty 	}
164418382eadSSubhransu S. Prusty 
164545a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
164618382eadSSubhransu S. Prusty 		unsigned int caps;
164718382eadSSubhransu S. Prusty 		unsigned int type;
164818382eadSSubhransu S. Prusty 
1649f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
165018382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
165118382eadSSubhransu S. Prusty 
165218382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
165318382eadSSubhransu S. Prusty 			continue;
165418382eadSSubhransu S. Prusty 
165518382eadSSubhransu S. Prusty 		switch (type) {
165618382eadSSubhransu S. Prusty 
165718382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
16583787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
165915b91447SSubhransu S. Prusty 			if (ret < 0)
1660c7ba4e53SPierre-Louis Bossart 				return ret;
166118382eadSSubhransu S. Prusty 			break;
166218382eadSSubhransu S. Prusty 
166318382eadSSubhransu S. Prusty 		case AC_WID_PIN:
16643787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
166515b91447SSubhransu S. Prusty 			if (ret < 0)
1666c7ba4e53SPierre-Louis Bossart 				return ret;
166718382eadSSubhransu S. Prusty 			break;
166818382eadSSubhransu S. Prusty 		}
166918382eadSSubhransu S. Prusty 	}
167018382eadSSubhransu S. Prusty 
16711c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
16721c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1673c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1674c7ba4e53SPierre-Louis Bossart 		return ret;
16751c0a7de2SSubhransu S. Prusty 	}
167618382eadSSubhransu S. Prusty 
1677f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
167817a42c45SSubhransu S. Prusty 	if (ret) {
1679f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
168017a42c45SSubhransu S. Prusty 			ret);
1681c7ba4e53SPierre-Louis Bossart 		return ret;
168217a42c45SSubhransu S. Prusty 	}
168317a42c45SSubhransu S. Prusty 
168417a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
16853787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
16861c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1687c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1688c7ba4e53SPierre-Louis Bossart 			ret);
16891c0a7de2SSubhransu S. Prusty 	return ret;
169018382eadSSubhransu S. Prusty }
169118382eadSSubhransu S. Prusty 
hdac_hdmi_pin2port(void * aptr,int pin)1692a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1693a57942bfSTakashi Iwai {
1694a57942bfSTakashi Iwai 	return pin - 4; /* map NID 0x05 -> port #1 */
1695a57942bfSTakashi Iwai }
1696a57942bfSTakashi Iwai 
hdac_hdmi_eld_notify_cb(void * aptr,int port,int pipe)1697f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1698b8a54545SSubhransu S. Prusty {
16993787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
17003787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1701426b3bbcSPierre-Louis Bossart 	struct hdac_hdmi_pin *pin;
1702754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
17033787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
17042acd8309SJeeja KP 	int i;
1705b8a54545SSubhransu S. Prusty 
1706573892b6SKai Vehmanen 	/* Don't know how this mapping is derived */
1707573892b6SKai Vehmanen 	hda_nid_t pin_nid = port + 0x04;
1708b8a54545SSubhransu S. Prusty 
17093787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1710754695f9SJeeja KP 							pin_nid, pipe);
1711b8a54545SSubhransu S. Prusty 
1712b8a54545SSubhransu S. Prusty 	/*
1713b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1714b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1715b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1716b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1717b8a54545SSubhransu S. Prusty 	 */
171845101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1719b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1720b8a54545SSubhransu S. Prusty 		return;
1721b8a54545SSubhransu S. Prusty 
17223787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1723b8a54545SSubhransu S. Prusty 		return;
1724b8a54545SSubhransu S. Prusty 
1725b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1726754695f9SJeeja KP 		if (pin->nid != pin_nid)
1727754695f9SJeeja KP 			continue;
1728754695f9SJeeja KP 
1729754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1730754695f9SJeeja KP 		if (pipe == -1) {
17312acd8309SJeeja KP 			pin->mst_capable = false;
1732754695f9SJeeja KP 			/* if not MST, default is port[0] */
1733754695f9SJeeja KP 			hport = &pin->ports[0];
17342acd8309SJeeja KP 		} else {
17352acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
17362acd8309SJeeja KP 				pin->mst_capable = true;
17372acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
17382acd8309SJeeja KP 					hport = &pin->ports[i];
173904c8f2bfSJeeja KP 					break;
17402acd8309SJeeja KP 				}
1741b8a54545SSubhransu S. Prusty 			}
1742b8a54545SSubhransu S. Prusty 		}
1743b8a54545SSubhransu S. Prusty 
174404c8f2bfSJeeja KP 		if (hport)
1745754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1746754695f9SJeeja KP 	}
1747754695f9SJeeja KP 
174804c8f2bfSJeeja KP }
174904c8f2bfSJeeja KP 
1750ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1751a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1752b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1753b8a54545SSubhransu S. Prusty };
1754b8a54545SSubhransu S. Prusty 
hdac_hdmi_get_pcm_from_id(struct snd_soc_card * card,int device)17552889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
17562889099eSSubhransu S. Prusty 						int device)
17572889099eSSubhransu S. Prusty {
17582889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
17592889099eSSubhransu S. Prusty 
1760bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
17612889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
17622889099eSSubhransu S. Prusty 			return rtd->pcm;
17632889099eSSubhransu S. Prusty 	}
17642889099eSSubhransu S. Prusty 
17652889099eSSubhransu S. Prusty 	return NULL;
17662889099eSSubhransu S. Prusty }
17672889099eSSubhransu S. Prusty 
17680324e51bSJeeja KP /* create jack pin kcontrols */
create_fill_jack_kcontrols(struct snd_soc_card * card,struct hdac_device * hdev)17690324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
17703787a398SRakesh Ughreja 				    struct hdac_device *hdev)
17710324e51bSJeeja KP {
17720324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
17730324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
17740324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
17750324e51bSJeeja KP 	char *name;
17760324e51bSJeeja KP 	int i = 0, j;
17773787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
17783787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
17790324e51bSJeeja KP 
178045101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
17810324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
17820324e51bSJeeja KP 
17830324e51bSJeeja KP 	if (!kc)
17840324e51bSJeeja KP 		return -ENOMEM;
17850324e51bSJeeja KP 
17860324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17870324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
17880324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
17890324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
179045101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
17910324e51bSJeeja KP 			if (!name)
17920324e51bSJeeja KP 				return -ENOMEM;
17930324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
179445101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
17950324e51bSJeeja KP 							GFP_KERNEL);
17960324e51bSJeeja KP 			if (!kc[i].name)
17970324e51bSJeeja KP 				return -ENOMEM;
17980324e51bSJeeja KP 
17990324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
18000324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
18010324e51bSJeeja KP 			kc[i].access = 0;
18020324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
18030324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
18040324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
18050324e51bSJeeja KP 			i++;
18060324e51bSJeeja KP 		}
18070324e51bSJeeja KP 	}
18080324e51bSJeeja KP 
18090324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
18100324e51bSJeeja KP }
18110324e51bSJeeja KP 
hdac_hdmi_jack_port_init(struct snd_soc_component * component,struct snd_soc_dapm_context * dapm)181245101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
18130324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
18140324e51bSJeeja KP {
18153787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18163787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
18170324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
18180324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
18190324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
18200324e51bSJeeja KP 	char w_name[NAME_SIZE];
18210324e51bSJeeja KP 	int i = 0, j, ret;
18220324e51bSJeeja KP 
18230324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
18240324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
18250324e51bSJeeja KP 
18260324e51bSJeeja KP 	if (!widgets)
18270324e51bSJeeja KP 		return -ENOMEM;
18280324e51bSJeeja KP 
18290324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
18300324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
18310324e51bSJeeja KP 	if (!route)
18320324e51bSJeeja KP 		return -ENOMEM;
18330324e51bSJeeja KP 
18340324e51bSJeeja KP 	/* create Jack DAPM widget */
18350324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
18360324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
18370324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
18380324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
18390324e51bSJeeja KP 
18400324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
18410324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
18420324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
18430324e51bSJeeja KP 			if (ret < 0)
18440324e51bSJeeja KP 				return ret;
18450324e51bSJeeja KP 
18460324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
18470324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
18480324e51bSJeeja KP 
18490324e51bSJeeja KP 			/* add to route from Jack widget to output */
18500324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
18510324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
18520324e51bSJeeja KP 
18530324e51bSJeeja KP 			i++;
18540324e51bSJeeja KP 		}
18550324e51bSJeeja KP 	}
18560324e51bSJeeja KP 
18570324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
18580324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
18590324e51bSJeeja KP 	if (ret < 0)
18600324e51bSJeeja KP 		return ret;
18610324e51bSJeeja KP 
18620324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
18630324e51bSJeeja KP 	if (ret < 0)
18640324e51bSJeeja KP 		return ret;
18650324e51bSJeeja KP 
18660324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
18670324e51bSJeeja KP 	if (ret < 0)
18680324e51bSJeeja KP 		return ret;
18690324e51bSJeeja KP 
18700324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
18713787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
18720324e51bSJeeja KP 
18730324e51bSJeeja KP 	if (ret < 0)
18740324e51bSJeeja KP 		return ret;
18750324e51bSJeeja KP 
18760324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
18770324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
18780324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
18790324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
18800324e51bSJeeja KP 						pin->ports[j].jack_pin);
18810324e51bSJeeja KP 	}
18820324e51bSJeeja KP 
18830324e51bSJeeja KP 	return 0;
18840324e51bSJeeja KP }
18850324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
18860324e51bSJeeja KP 
hdac_hdmi_jack_init(struct snd_soc_dai * dai,int device,struct snd_soc_jack * jack)188762490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
188862490016SJeeja KP 				struct snd_soc_jack *jack)
18894a3478deSJeeja KP {
189045101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
18913787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18923787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
18934a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
18942889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
18952889099eSSubhransu S. Prusty 	int err;
18964a3478deSJeeja KP 
18974a3478deSJeeja KP 	/*
18984a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
18994a3478deSJeeja KP 	 * add to the pcm list
19004a3478deSJeeja KP 	 */
1901c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
19024a3478deSJeeja KP 	if (!pcm)
19034a3478deSJeeja KP 		return -ENOMEM;
19044a3478deSJeeja KP 	pcm->pcm_id = device;
19054a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1906e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
190762490016SJeeja KP 	pcm->jack = jack;
1908ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1909e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
19102889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
19112889099eSSubhransu S. Prusty 	if (snd_pcm) {
19122889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
19132889099eSSubhransu S. Prusty 		if (err < 0) {
19143787a398SRakesh Ughreja 			dev_err(&hdev->dev,
19152889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
19162889099eSSubhransu S. Prusty 				err, device);
19172889099eSSubhransu S. Prusty 			return err;
19182889099eSSubhransu S. Prusty 		}
19192889099eSSubhransu S. Prusty 	}
19202889099eSSubhransu S. Prusty 
19211f53bcb3SBrent Lu 	/* add control for ELD Bytes */
19221f53bcb3SBrent Lu 	err = hdac_hdmi_create_eld_ctl(component, pcm);
19231f53bcb3SBrent Lu 	if (err < 0) {
19241f53bcb3SBrent Lu 		dev_err(&hdev->dev,
19251f53bcb3SBrent Lu 			"eld control add failed with err: %d for pcm: %d\n",
19261f53bcb3SBrent Lu 			err, device);
19271f53bcb3SBrent Lu 		return err;
19281f53bcb3SBrent Lu 	}
19291f53bcb3SBrent Lu 
19304a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
19314a3478deSJeeja KP 
193262490016SJeeja KP 	return 0;
19334a3478deSJeeja KP }
19344a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
19354a3478deSJeeja KP 
hdac_hdmi_present_sense_all_pins(struct hdac_device * hdev,struct hdac_hdmi_priv * hdmi,bool detect_pin_caps)19363787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1937a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1938a9ce96bcSJeeja KP {
1939a9ce96bcSJeeja KP 	int i;
1940a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1941a9ce96bcSJeeja KP 
1942a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1943a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1944a9ce96bcSJeeja KP 
19453787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1946a9ce96bcSJeeja KP 				pin->mst_capable = false;
1947a9ce96bcSJeeja KP 			else
1948a9ce96bcSJeeja KP 				pin->mst_capable = true;
1949a9ce96bcSJeeja KP 		}
1950a9ce96bcSJeeja KP 
1951a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1952a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1953a9ce96bcSJeeja KP 				continue;
1954a9ce96bcSJeeja KP 
1955a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1956a9ce96bcSJeeja KP 		}
1957a9ce96bcSJeeja KP 	}
1958a9ce96bcSJeeja KP }
1959a9ce96bcSJeeja KP 
hdmi_codec_probe(struct snd_soc_component * component)196045101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
196118382eadSSubhransu S. Prusty {
19623787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
19633787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
196418382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
196545101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1966426b3bbcSPierre-Louis Bossart 	struct hdac_ext_link *hlink;
1967a9ce96bcSJeeja KP 	int ret;
196818382eadSSubhransu S. Prusty 
19693787a398SRakesh Ughreja 	hdmi->component = component;
197018382eadSSubhransu S. Prusty 
1971b2047e99SVinod Koul 	/*
1972b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1973b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1974b2047e99SVinod Koul 	 */
1975b0cd60f3SPierre-Louis Bossart 	hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1976500e06b9SVinod Koul 	if (!hlink) {
19773787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1978500e06b9SVinod Koul 		return -EIO;
1979500e06b9SVinod Koul 	}
1980500e06b9SVinod Koul 
198176f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1982b2047e99SVinod Koul 
198379f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
198479f4e922SSubhransu S. Prusty 	if (ret < 0)
198579f4e922SSubhransu S. Prusty 		return ret;
198618382eadSSubhransu S. Prusty 
19873787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1988a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1989b8a54545SSubhransu S. Prusty 	if (ret < 0) {
19903787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1991b8a54545SSubhransu S. Prusty 		return ret;
1992b8a54545SSubhransu S. Prusty 	}
1993b8a54545SSubhransu S. Prusty 
19943787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
199518382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
19963787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
199718382eadSSubhransu S. Prusty 
1998e342ac08SSubhransu S. Prusty 	/*
199901c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
200001c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
200101c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
200201c83276SLibin Yang 	 * domain in display power will be always turned on before operating
200301c83276SLibin Yang 	 * on the HDMI audio codec registers.
200401c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
200501c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
200601c83276SLibin Yang 	 */
200701c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
200801c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
200901c83276SLibin Yang 	/*
2010e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
2011e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
2012e342ac08SSubhransu S. Prusty 	 */
20133787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
20143787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
20153787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
2016e342ac08SSubhransu S. Prusty 
2017e342ac08SSubhransu S. Prusty 	return 0;
2018e342ac08SSubhransu S. Prusty }
2019e342ac08SSubhransu S. Prusty 
hdmi_codec_remove(struct snd_soc_component * component)202045101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
2021e342ac08SSubhransu S. Prusty {
20223787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
20233787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
20240f6ff785SAmadeusz Sławiński 	int ret;
20250f6ff785SAmadeusz Sławiński 
20260f6ff785SAmadeusz Sławiński 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
20270f6ff785SAmadeusz Sławiński 	if (ret < 0)
20280f6ff785SAmadeusz Sławiński 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
20290f6ff785SAmadeusz Sławiński 				ret);
2030e342ac08SSubhransu S. Prusty 
20313787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
203218382eadSSubhransu S. Prusty }
203318382eadSSubhransu S. Prusty 
2034687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
hdmi_codec_resume(struct device * dev)2035687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
2036571d5078SJeeja KP {
20373787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
20383787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2039687ae9e2STakashi Iwai 	int ret;
20401b377ccdSSubhransu S. Prusty 
2041687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
2042687ae9e2STakashi Iwai 	if (ret < 0)
2043687ae9e2STakashi Iwai 		return ret;
2044571d5078SJeeja KP 	/*
2045571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
2046571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
2047a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
2048a9ce96bcSJeeja KP 	 * already set pin caps.
2049687ae9e2STakashi Iwai 	 *
2050687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
2051687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
2052687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
2053571d5078SJeeja KP 	 */
20543787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
2055687ae9e2STakashi Iwai 	return 0;
2056571d5078SJeeja KP }
2057571d5078SJeeja KP #else
2058687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
2059571d5078SJeeja KP #endif
2060571d5078SJeeja KP 
206145101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
206218382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
2063e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
206445101122SKuninori Morimoto 	.use_pmdown_time	= 1,
206545101122SKuninori Morimoto 	.endianness		= 1,
206618382eadSSubhransu S. Prusty };
206718382eadSSubhransu S. Prusty 
hdac_hdmi_get_chmap(struct hdac_device * hdev,int pcm_idx,unsigned char * chmap)2068f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
20692889099eSSubhransu S. Prusty 					unsigned char *chmap)
20702889099eSSubhransu S. Prusty {
2071f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
20722889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
20732889099eSSubhransu S. Prusty 
2074ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
20752889099eSSubhransu S. Prusty }
20762889099eSSubhransu S. Prusty 
hdac_hdmi_set_chmap(struct hdac_device * hdev,int pcm_idx,unsigned char * chmap,int prepared)2077f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
20782889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
20792889099eSSubhransu S. Prusty {
2080f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
20812889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2082e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
2083e0e5d3e5SJeeja KP 
2084eb50fa17SSubhransu S. Prusty 	if (!pcm)
2085eb50fa17SSubhransu S. Prusty 		return;
2086eb50fa17SSubhransu S. Prusty 
2087e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
2088e0e5d3e5SJeeja KP 		return;
20892889099eSSubhransu S. Prusty 
2090ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
2091ab1eea19SJeeja KP 	pcm->chmap_set = true;
2092ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
2093e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
20942889099eSSubhransu S. Prusty 		if (prepared)
20953787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
2096ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
20972889099eSSubhransu S. Prusty }
20982889099eSSubhransu S. Prusty 
is_hdac_hdmi_pcm_attached(struct hdac_device * hdev,int pcm_idx)2099f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
21002889099eSSubhransu S. Prusty {
2101f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
21022889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
21032889099eSSubhransu S. Prusty 
2104eb50fa17SSubhransu S. Prusty 	if (!pcm)
2105eb50fa17SSubhransu S. Prusty 		return false;
2106eb50fa17SSubhransu S. Prusty 
2107e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
2108e0e5d3e5SJeeja KP 		return false;
2109e0e5d3e5SJeeja KP 
2110e0e5d3e5SJeeja KP 	return true;
21112889099eSSubhransu S. Prusty }
21122889099eSSubhransu S. Prusty 
hdac_hdmi_get_spk_alloc(struct hdac_device * hdev,int pcm_idx)2113f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
21142889099eSSubhransu S. Prusty {
2115f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
21162889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2117e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
2118e0e5d3e5SJeeja KP 
2119eb50fa17SSubhransu S. Prusty 	if (!pcm)
2120eb50fa17SSubhransu S. Prusty 		return 0;
2121eb50fa17SSubhransu S. Prusty 
2122e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
2123e0e5d3e5SJeeja KP 		return 0;
2124e0e5d3e5SJeeja KP 
2125e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
2126e0e5d3e5SJeeja KP 
2127754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
21282889099eSSubhransu S. Prusty 		return 0;
21292889099eSSubhransu S. Prusty 
2130754695f9SJeeja KP 	return port->eld.info.spk_alloc;
21312889099eSSubhransu S. Prusty }
21322889099eSSubhransu S. Prusty 
21335622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
2134573892b6SKai Vehmanen 	.vendor_nid = INTEL_GLK_VENDOR_NID,
21355622bc95SPradeep Tewani };
21365622bc95SPradeep Tewani 
21375622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
2138573892b6SKai Vehmanen 	.vendor_nid = INTEL_VENDOR_NID,
21395622bc95SPradeep Tewani };
21405622bc95SPradeep Tewani 
hdac_hdmi_dev_probe(struct hdac_device * hdev)21413787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
214218382eadSSubhransu S. Prusty {
2143c4aafb33SGuennadi Liakhovetski 	struct hdac_hdmi_priv *hdmi_priv;
214417a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
2145c4aafb33SGuennadi Liakhovetski 	struct hdac_ext_link *hlink;
214617a42c45SSubhransu S. Prusty 	int num_dais = 0;
2147c4aafb33SGuennadi Liakhovetski 	int ret;
2148f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2149f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
215018382eadSSubhransu S. Prusty 
2151b2047e99SVinod Koul 	/* hold the ref while we probe */
2152b0cd60f3SPierre-Louis Bossart 	hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
2153500e06b9SVinod Koul 	if (!hlink) {
21543787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
2155500e06b9SVinod Koul 		return -EIO;
2156500e06b9SVinod Koul 	}
2157500e06b9SVinod Koul 
215876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2159b2047e99SVinod Koul 
2160f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
216118382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
216218382eadSSubhransu S. Prusty 		return -ENOMEM;
216318382eadSSubhransu S. Prusty 
2164f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
21652889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
21662889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
21672889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
21682889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
21693787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
217018382eadSSubhransu S. Prusty 
2171eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2172eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2173eb50fa17SSubhransu S. Prusty 
21745622bc95SPradeep Tewani 	if (hdac_id->driver_data)
21755622bc95SPradeep Tewani 		hdmi_priv->drv_data =
21765622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
21775622bc95SPradeep Tewani 	else
21785622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
21795622bc95SPradeep Tewani 
21803787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
218118382eadSSubhransu S. Prusty 
218215b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
218315b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
21844a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
21854a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
218615b91447SSubhransu S. Prusty 
2187aeaccef0SRamesh Babu 	/*
2188aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2189aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2190aeaccef0SRamesh Babu 	 */
21914f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
21924f799e73STakashi Iwai 
21933787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
219417a42c45SSubhransu S. Prusty 	if (ret < 0) {
2195f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
219617a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
219718382eadSSubhransu S. Prusty 		return ret;
219817a42c45SSubhransu S. Prusty 	}
2199774a075aSTakashi Iwai 	snd_hdac_refresh_widgets(hdev);
220018382eadSSubhransu S. Prusty 
220118382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
220245101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
220317a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2204b2047e99SVinod Koul 
220576f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2206b2047e99SVinod Koul 
2207b2047e99SVinod Koul 	return ret;
220818382eadSSubhransu S. Prusty }
220918382eadSSubhransu S. Prusty 
clear_dapm_works(struct hdac_device * hdev)2210332ccf00STakashi Iwai static void clear_dapm_works(struct hdac_device *hdev)
2211332ccf00STakashi Iwai {
2212332ccf00STakashi Iwai 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2213332ccf00STakashi Iwai 	struct hdac_hdmi_pin *pin;
2214332ccf00STakashi Iwai 	int i;
2215332ccf00STakashi Iwai 
2216332ccf00STakashi Iwai 	list_for_each_entry(pin, &hdmi->pin_list, head)
2217332ccf00STakashi Iwai 		for (i = 0; i < pin->num_ports; i++)
2218332ccf00STakashi Iwai 			cancel_work_sync(&pin->ports[i].dapm_work);
2219332ccf00STakashi Iwai }
2220332ccf00STakashi Iwai 
hdac_hdmi_dev_remove(struct hdac_device * hdev)22213787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
222218382eadSSubhransu S. Prusty {
2223332ccf00STakashi Iwai 	clear_dapm_works(hdev);
222477a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
222577a49672STakashi Iwai 
222618382eadSSubhransu S. Prusty 	return 0;
222718382eadSSubhransu S. Prusty }
222818382eadSSubhransu S. Prusty 
2229e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
hdac_hdmi_runtime_suspend(struct device * dev)2230e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2231e342ac08SSubhransu S. Prusty {
22323787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2233f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2234426b3bbcSPierre-Louis Bossart 	struct hdac_ext_link *hlink;
2235e342ac08SSubhransu S. Prusty 
2236e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2237e342ac08SSubhransu S. Prusty 
223807f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
223907f083abSSubhransu S. Prusty 	if (!bus)
224007f083abSSubhransu S. Prusty 		return 0;
224107f083abSSubhransu S. Prusty 
22421b377ccdSSubhransu S. Prusty 	/*
22431b377ccdSSubhransu S. Prusty 	 * Power down afg.
22441b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
22451b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
22461b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
22471b377ccdSSubhransu S. Prusty 	 * to read the state.
22481b377ccdSSubhransu S. Prusty 	 */
2249f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
22501b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
225107f083abSSubhransu S. Prusty 
2252b0cd60f3SPierre-Louis Bossart 	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2253500e06b9SVinod Koul 	if (!hlink) {
2254500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2255500e06b9SVinod Koul 		return -EIO;
2256500e06b9SVinod Koul 	}
2257500e06b9SVinod Koul 
2258e62bccf8SKai Vehmanen 	snd_hdac_codec_link_down(hdev);
225976f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2260b2047e99SVinod Koul 
22614f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
22624c10473dSPierre-Louis Bossart 
22634f799e73STakashi Iwai 	return 0;
2264e342ac08SSubhransu S. Prusty }
2265e342ac08SSubhransu S. Prusty 
hdac_hdmi_runtime_resume(struct device * dev)2266e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2267e342ac08SSubhransu S. Prusty {
22683787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2269f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2270426b3bbcSPierre-Louis Bossart 	struct hdac_ext_link *hlink;
2271e342ac08SSubhransu S. Prusty 
2272e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2273e342ac08SSubhransu S. Prusty 
227407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
227507f083abSSubhransu S. Prusty 	if (!bus)
227607f083abSSubhransu S. Prusty 		return 0;
227707f083abSSubhransu S. Prusty 
2278b0cd60f3SPierre-Louis Bossart 	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2279500e06b9SVinod Koul 	if (!hlink) {
2280500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2281500e06b9SVinod Koul 		return -EIO;
2282500e06b9SVinod Koul 	}
2283500e06b9SVinod Koul 
228476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2285e62bccf8SKai Vehmanen 	snd_hdac_codec_link_up(hdev);
2286b2047e99SVinod Koul 
22874f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
228807f083abSSubhransu S. Prusty 
22893787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
22903787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2291ab85f5b3SSubhransu S. Prusty 
2292e342ac08SSubhransu S. Prusty 	/* Power up afg */
2293f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
22941b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2295e342ac08SSubhransu S. Prusty 
2296e342ac08SSubhransu S. Prusty 	return 0;
2297e342ac08SSubhransu S. Prusty }
2298e342ac08SSubhransu S. Prusty #else
2299e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2300e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2301e342ac08SSubhransu S. Prusty #endif
2302e342ac08SSubhransu S. Prusty 
2303e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2304e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2305687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2306e342ac08SSubhransu S. Prusty };
2307e342ac08SSubhransu S. Prusty 
230818382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
230918382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2310e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2311cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
23125fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
23135fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
23145622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
23155622bc95SPradeep Tewani 						   &intel_glk_drv_data),
231618382eadSSubhransu S. Prusty 	{}
231718382eadSSubhransu S. Prusty };
231818382eadSSubhransu S. Prusty 
231918382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
232018382eadSSubhransu S. Prusty 
2321e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
232218382eadSSubhransu S. Prusty 	.driver = {
232318382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2324e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
232518382eadSSubhransu S. Prusty 	},
232618382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
232718382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
232818382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
232918382eadSSubhransu S. Prusty };
233018382eadSSubhransu S. Prusty 
hdmi_init(void)233118382eadSSubhransu S. Prusty static int __init hdmi_init(void)
233218382eadSSubhransu S. Prusty {
233318382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
233418382eadSSubhransu S. Prusty }
233518382eadSSubhransu S. Prusty 
hdmi_exit(void)233618382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
233718382eadSSubhransu S. Prusty {
233818382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
233918382eadSSubhransu S. Prusty }
234018382eadSSubhransu S. Prusty 
234118382eadSSubhransu S. Prusty module_init(hdmi_init);
234218382eadSSubhransu S. Prusty module_exit(hdmi_exit);
234318382eadSSubhransu S. Prusty 
234418382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
234518382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
234618382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
234718382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2348