xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 0f6ff785)
118382eadSSubhransu S. Prusty /*
218382eadSSubhransu S. Prusty  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
318382eadSSubhransu S. Prusty  *
418382eadSSubhransu S. Prusty  *  Copyright (C) 2014-2015 Intel Corp
518382eadSSubhransu S. Prusty  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
618382eadSSubhransu S. Prusty  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
718382eadSSubhransu S. Prusty  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
818382eadSSubhransu S. Prusty  *
918382eadSSubhransu S. Prusty  *  This program is free software; you can redistribute it and/or modify
1018382eadSSubhransu S. Prusty  *  it under the terms of the GNU General Public License as published by
1118382eadSSubhransu S. Prusty  *  the Free Software Foundation; version 2 of the License.
1218382eadSSubhransu S. Prusty  *
1318382eadSSubhransu S. Prusty  *  This program is distributed in the hope that it will be useful, but
1418382eadSSubhransu S. Prusty  *  WITHOUT ANY WARRANTY; without even the implied warranty of
1518382eadSSubhransu S. Prusty  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1618382eadSSubhransu S. Prusty  *  General Public License for more details.
1718382eadSSubhransu S. Prusty  *
1818382eadSSubhransu S. Prusty  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1918382eadSSubhransu S. Prusty  */
2018382eadSSubhransu S. Prusty #include <linux/init.h>
2118382eadSSubhransu S. Prusty #include <linux/delay.h>
2218382eadSSubhransu S. Prusty #include <linux/module.h>
2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h>
24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h>
252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h>
2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h>
274a3478deSJeeja KP #include <sound/jack.h>
2818382eadSSubhransu S. Prusty #include <sound/soc.h>
2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h>
3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h>
312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h>
32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h>
3318382eadSSubhransu S. Prusty #include "../../hda/local.h"
344a3478deSJeeja KP #include "hdac_hdmi.h"
3518382eadSSubhransu S. Prusty 
3617a42c45SSubhransu S. Prusty #define NAME_SIZE	32
3717a42c45SSubhransu S. Prusty 
38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE		0xb080
39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE		0xb000
4018382eadSSubhransu S. Prusty #define PIN_OUT			(AC_PINCTL_OUT_EN)
41b0362adbSSubhransu S. Prusty 
4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS     32
4318382eadSSubhransu S. Prusty 
44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS		3
45754695f9SJeeja KP #define HDA_MAX_PORTS		3
46148569fdSSubhransu S. Prusty 
47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE    256
48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES	20
49b8a54545SSubhransu S. Prusty 
50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2
51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31
52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL     16
53f6fa11a3SSandeep Tayal 
5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params {
5518382eadSSubhransu S. Prusty 	unsigned int channels_min;
5618382eadSSubhransu S. Prusty 	unsigned int channels_max;
5718382eadSSubhransu S. Prusty 	u32 rates;
5818382eadSSubhransu S. Prusty 	u64 formats;
5918382eadSSubhransu S. Prusty 	unsigned int maxbps;
6018382eadSSubhransu S. Prusty };
6118382eadSSubhransu S. Prusty 
6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt {
6315b91447SSubhransu S. Prusty 	struct list_head head;
6418382eadSSubhransu S. Prusty 	hda_nid_t nid;
654a3478deSJeeja KP 	const char *name;
6618382eadSSubhransu S. Prusty 	struct hdac_hdmi_cvt_params params;
6718382eadSSubhransu S. Prusty };
6818382eadSSubhransu S. Prusty 
69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */
70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld {
71b7756edeSSubhransu S. Prusty 	u8 spk_alloc;
72b7756edeSSubhransu S. Prusty };
73b7756edeSSubhransu S. Prusty 
74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld {
75b8a54545SSubhransu S. Prusty 	bool	monitor_present;
76b8a54545SSubhransu S. Prusty 	bool	eld_valid;
77b8a54545SSubhransu S. Prusty 	int	eld_size;
78b8a54545SSubhransu S. Prusty 	char    eld_buffer[ELD_MAX_SIZE];
79b7756edeSSubhransu S. Prusty 	struct	hdac_hdmi_parsed_eld info;
80b8a54545SSubhransu S. Prusty };
81b8a54545SSubhransu S. Prusty 
8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin {
8315b91447SSubhransu S. Prusty 	struct list_head head;
8418382eadSSubhransu S. Prusty 	hda_nid_t nid;
852acd8309SJeeja KP 	bool mst_capable;
86754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
87754695f9SJeeja KP 	int num_ports;
883787a398SRakesh Ughreja 	struct hdac_device *hdev;
89754695f9SJeeja KP };
90754695f9SJeeja KP 
91754695f9SJeeja KP struct hdac_hdmi_port {
92e0e5d3e5SJeeja KP 	struct list_head head;
93754695f9SJeeja KP 	int id;
94754695f9SJeeja KP 	struct hdac_hdmi_pin *pin;
9518382eadSSubhransu S. Prusty 	int num_mux_nids;
9618382eadSSubhransu S. Prusty 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97b8a54545SSubhransu S. Prusty 	struct hdac_hdmi_eld eld;
980324e51bSJeeja KP 	const char *jack_pin;
990324e51bSJeeja KP 	struct snd_soc_dapm_context *dapm;
1000324e51bSJeeja KP 	const char *output_pin;
10118382eadSSubhransu S. Prusty };
10218382eadSSubhransu S. Prusty 
1034a3478deSJeeja KP struct hdac_hdmi_pcm {
1044a3478deSJeeja KP 	struct list_head head;
1054a3478deSJeeja KP 	int pcm_id;
106e0e5d3e5SJeeja KP 	struct list_head port_list;
1074a3478deSJeeja KP 	struct hdac_hdmi_cvt *cvt;
10862490016SJeeja KP 	struct snd_soc_jack *jack;
109c9bfb5d7SJeeja KP 	int stream_tag;
110c9bfb5d7SJeeja KP 	int channels;
111c9bfb5d7SJeeja KP 	int format;
112ab1eea19SJeeja KP 	bool chmap_set;
113ab1eea19SJeeja KP 	unsigned char chmap[8]; /* ALSA API channel-map */
114ab1eea19SJeeja KP 	struct mutex lock;
115e0e5d3e5SJeeja KP 	int jack_event;
1164a3478deSJeeja KP };
1174a3478deSJeeja KP 
118754695f9SJeeja KP struct hdac_hdmi_dai_port_map {
11918382eadSSubhransu S. Prusty 	int dai_id;
120754695f9SJeeja KP 	struct hdac_hdmi_port *port;
12115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
12218382eadSSubhransu S. Prusty };
12318382eadSSubhransu S. Prusty 
124019033c8SBard liao /*
125019033c8SBard liao  * pin to port mapping table where the value indicate the pin number and
126019033c8SBard liao  * the index indicate the port number with 1 base.
127019033c8SBard liao  */
128019033c8SBard liao static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
129019033c8SBard liao 
1305622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1315622bc95SPradeep Tewani 	unsigned int vendor_nid;
132019033c8SBard liao 	const int *port_map; /* pin to port mapping table */
133019033c8SBard liao 	int port_num;
1345622bc95SPradeep Tewani };
1355622bc95SPradeep Tewani 
13618382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
1373787a398SRakesh Ughreja 	struct hdac_device *hdev;
1383787a398SRakesh Ughreja 	struct snd_soc_component *component;
1393787a398SRakesh Ughreja 	struct snd_card *card;
140754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
14115b91447SSubhransu S. Prusty 	struct list_head pin_list;
14215b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1434a3478deSJeeja KP 	struct list_head pcm_list;
14415b91447SSubhransu S. Prusty 	int num_pin;
14515b91447SSubhransu S. Prusty 	int num_cvt;
146754695f9SJeeja KP 	int num_ports;
1474a3478deSJeeja KP 	struct mutex pin_mutex;
148bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1495622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1501e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
15118382eadSSubhransu S. Prusty };
15218382eadSSubhransu S. Prusty 
1533787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
154b09b1c3bSUghreja, Rakesh A 
155c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
156c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
157c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
158c9bfb5d7SJeeja KP {
159c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1601de777feSJeeja KP 
161c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
162c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
163c9bfb5d7SJeeja KP 			break;
164c9bfb5d7SJeeja KP 	}
165c9bfb5d7SJeeja KP 
166c9bfb5d7SJeeja KP 	return pcm;
167c9bfb5d7SJeeja KP }
1681de777feSJeeja KP 
169e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
170e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
171e0e5d3e5SJeeja KP {
1723787a398SRakesh Ughreja 	struct hdac_device *hdev = port->pin->hdev;
173e0e5d3e5SJeeja KP 
1740324e51bSJeeja KP 	if (is_connect)
1750324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1760324e51bSJeeja KP 	else
1770324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1780324e51bSJeeja KP 
179e0e5d3e5SJeeja KP 	if (is_connect) {
180e0e5d3e5SJeeja KP 		/*
181e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
182e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
183e0e5d3e5SJeeja KP 		 * ports.
184e0e5d3e5SJeeja KP 		 */
185e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
1863787a398SRakesh Ughreja 			dev_dbg(&hdev->dev,
187e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
188e0e5d3e5SJeeja KP 					pcm->pcm_id);
18962490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
19062490016SJeeja KP 						SND_JACK_AVOUT);
191e0e5d3e5SJeeja KP 		}
192e0e5d3e5SJeeja KP 		pcm->jack_event++;
193e0e5d3e5SJeeja KP 	} else {
194e0e5d3e5SJeeja KP 		/*
195e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
196e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
197e0e5d3e5SJeeja KP 		 * to multiple ports.
198e0e5d3e5SJeeja KP 		 */
199e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
20062490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
201e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
202e0e5d3e5SJeeja KP 			pcm->jack_event--;
203e0e5d3e5SJeeja KP 	}
2040324e51bSJeeja KP 
2050324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
206e0e5d3e5SJeeja KP }
207e0e5d3e5SJeeja KP 
208fc181b04SJeeja KP /* MST supported verbs */
209fc181b04SJeeja KP /*
210fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
211fc181b04SJeeja KP  */
2123787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
213fc181b04SJeeja KP {
214fc181b04SJeeja KP 	unsigned int caps;
215fc181b04SJeeja KP 	unsigned int type, param;
216fc181b04SJeeja KP 
2173787a398SRakesh Ughreja 	caps = get_wcaps(hdev, nid);
218fc181b04SJeeja KP 	type = get_wcaps_type(caps);
219fc181b04SJeeja KP 
220fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
221fc181b04SJeeja KP 		return 0;
222fc181b04SJeeja KP 
2233787a398SRakesh Ughreja 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
224fc181b04SJeeja KP 	if (param == -1)
225fc181b04SJeeja KP 		return param;
226fc181b04SJeeja KP 
227fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
228fc181b04SJeeja KP }
229fc181b04SJeeja KP 
230fc181b04SJeeja KP /*
231fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
232fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
233fc181b04SJeeja KP  * is selected or MST is not supported.
234fc181b04SJeeja KP  */
2353787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
236fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
237fc181b04SJeeja KP {
2383787a398SRakesh Ughreja 	return snd_hdac_codec_read(hdev, port->pin->nid,
239fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
240fc181b04SJeeja KP }
241fc181b04SJeeja KP 
242fc181b04SJeeja KP /*
243fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
244fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
245fc181b04SJeeja KP  */
2463787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
247fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
248fc181b04SJeeja KP {
249fc181b04SJeeja KP 	int num_ports;
250fc181b04SJeeja KP 
251fc181b04SJeeja KP 	if (!port->pin->mst_capable)
252fc181b04SJeeja KP 		return 0;
253fc181b04SJeeja KP 
254fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
2553787a398SRakesh Ughreja 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
256fc181b04SJeeja KP 	if (num_ports < 0)
257fc181b04SJeeja KP 		return -EIO;
258fc181b04SJeeja KP 	/*
259fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
260fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
261fc181b04SJeeja KP 	 */
262fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
263fc181b04SJeeja KP 		return 0;
264fc181b04SJeeja KP 
2653787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
266fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
267fc181b04SJeeja KP 
2683787a398SRakesh Ughreja 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
269fc181b04SJeeja KP 		return -EIO;
270fc181b04SJeeja KP 
2713787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
272fc181b04SJeeja KP 
273fc181b04SJeeja KP 	return 0;
274fc181b04SJeeja KP }
275fc181b04SJeeja KP 
2762889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2772889099eSSubhransu S. Prusty 						int pcm_idx)
2782889099eSSubhransu S. Prusty {
2792889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2802889099eSSubhransu S. Prusty 
2812889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2822889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2832889099eSSubhransu S. Prusty 			return pcm;
2842889099eSSubhransu S. Prusty 	}
2852889099eSSubhransu S. Prusty 
2862889099eSSubhransu S. Prusty 	return NULL;
2872889099eSSubhransu S. Prusty }
2882889099eSSubhransu S. Prusty 
2892428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2902428bca3SSubhransu S. Prusty {
2912428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2922428bca3SSubhransu S. Prusty }
2932428bca3SSubhransu S. Prusty 
2942428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2952428bca3SSubhransu S. Prusty {
2962428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2972428bca3SSubhransu S. Prusty }
2982428bca3SSubhransu S. Prusty 
2992428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
3002428bca3SSubhransu S. Prusty 						void *eld)
3012428bca3SSubhransu S. Prusty {
3022428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3032428bca3SSubhransu S. Prusty 	int i;
3042428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3052428bca3SSubhransu S. Prusty 
3062428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3072428bca3SSubhransu S. Prusty 	if (!sad)
3082428bca3SSubhransu S. Prusty 		goto format_constraint;
3092428bca3SSubhransu S. Prusty 
3102428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3112428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3122428bca3SSubhransu S. Prusty 
3132428bca3SSubhransu S. Prusty 			/*
3142428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3152428bca3SSubhransu S. Prusty 			 * container so we set S32
3162428bca3SSubhransu S. Prusty 			 */
3172428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3182428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3192428bca3SSubhransu S. Prusty 		}
3202428bca3SSubhransu S. Prusty 	}
3212428bca3SSubhransu S. Prusty 
3222428bca3SSubhransu S. Prusty format_constraint:
3232428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3242428bca3SSubhransu S. Prusty 				formats);
3252428bca3SSubhransu S. Prusty 
3262428bca3SSubhransu S. Prusty }
3272428bca3SSubhransu S. Prusty 
328a657f1d0SSubhransu S. Prusty static void
3293787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
330a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
331a657f1d0SSubhransu S. Prusty {
332a657f1d0SSubhransu S. Prusty 	int val;
333a657f1d0SSubhransu S. Prusty 
334a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
3353787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
336a657f1d0SSubhransu S. Prusty }
337a657f1d0SSubhransu S. Prusty 
338478f544eSSubhransu S. Prusty struct dp_audio_infoframe {
339478f544eSSubhransu S. Prusty 	u8 type; /* 0x84 */
340478f544eSSubhransu S. Prusty 	u8 len;  /* 0x1b */
341478f544eSSubhransu S. Prusty 	u8 ver;  /* 0x11 << 2 */
342478f544eSSubhransu S. Prusty 
343478f544eSSubhransu S. Prusty 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
344478f544eSSubhransu S. Prusty 	u8 SS01_SF24;
345478f544eSSubhransu S. Prusty 	u8 CXT04;
346478f544eSSubhransu S. Prusty 	u8 CA;
347478f544eSSubhransu S. Prusty 	u8 LFEPBL01_LSV36_DM_INH7;
348478f544eSSubhransu S. Prusty };
349478f544eSSubhransu S. Prusty 
3503787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
351754695f9SJeeja KP 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
352a657f1d0SSubhransu S. Prusty {
353a657f1d0SSubhransu S. Prusty 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
354a657f1d0SSubhransu S. Prusty 	struct hdmi_audio_infoframe frame;
355754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
356478f544eSSubhransu S. Prusty 	struct dp_audio_infoframe dp_ai;
3573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
358ab1eea19SJeeja KP 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
359478f544eSSubhransu S. Prusty 	u8 *dip;
360a657f1d0SSubhransu S. Prusty 	int ret;
361a657f1d0SSubhransu S. Prusty 	int i;
362478f544eSSubhransu S. Prusty 	const u8 *eld_buf;
363478f544eSSubhransu S. Prusty 	u8 conn_type;
364bcced704SSubhransu S. Prusty 	int channels, ca;
365a657f1d0SSubhransu S. Prusty 
3663787a398SRakesh Ughreja 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
367ab1eea19SJeeja KP 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
368bcced704SSubhransu S. Prusty 
369bcced704SSubhransu S. Prusty 	channels = snd_hdac_get_active_channels(ca);
3703787a398SRakesh Ughreja 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
371bcced704SSubhransu S. Prusty 
372bcced704SSubhransu S. Prusty 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
373ab1eea19SJeeja KP 				pcm->channels, pcm->chmap, pcm->chmap_set);
374bcced704SSubhransu S. Prusty 
375754695f9SJeeja KP 	eld_buf = port->eld.eld_buffer;
376478f544eSSubhransu S. Prusty 	conn_type = drm_eld_get_conn_type(eld_buf);
377a657f1d0SSubhransu S. Prusty 
378478f544eSSubhransu S. Prusty 	switch (conn_type) {
379478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_HDMI:
380478f544eSSubhransu S. Prusty 		hdmi_audio_infoframe_init(&frame);
381478f544eSSubhransu S. Prusty 
382478f544eSSubhransu S. Prusty 		frame.channels = channels;
383bcced704SSubhransu S. Prusty 		frame.channel_allocation = ca;
384a657f1d0SSubhransu S. Prusty 
385a657f1d0SSubhransu S. Prusty 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
386a657f1d0SSubhransu S. Prusty 		if (ret < 0)
387a657f1d0SSubhransu S. Prusty 			return ret;
388a657f1d0SSubhransu S. Prusty 
389478f544eSSubhransu S. Prusty 		break;
390478f544eSSubhransu S. Prusty 
391478f544eSSubhransu S. Prusty 	case DRM_ELD_CONN_TYPE_DP:
392478f544eSSubhransu S. Prusty 		memset(&dp_ai, 0, sizeof(dp_ai));
393478f544eSSubhransu S. Prusty 		dp_ai.type	= 0x84;
394478f544eSSubhransu S. Prusty 		dp_ai.len	= 0x1b;
395478f544eSSubhransu S. Prusty 		dp_ai.ver	= 0x11 << 2;
396478f544eSSubhransu S. Prusty 		dp_ai.CC02_CT47	= channels - 1;
397bcced704SSubhransu S. Prusty 		dp_ai.CA	= ca;
398478f544eSSubhransu S. Prusty 
399478f544eSSubhransu S. Prusty 		dip = (u8 *)&dp_ai;
400478f544eSSubhransu S. Prusty 		break;
401478f544eSSubhransu S. Prusty 
402478f544eSSubhransu S. Prusty 	default:
4033787a398SRakesh Ughreja 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
404478f544eSSubhransu S. Prusty 		return -EIO;
405478f544eSSubhransu S. Prusty 	}
406478f544eSSubhransu S. Prusty 
407a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
4083787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4093787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
410a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
411a657f1d0SSubhransu S. Prusty 
412a657f1d0SSubhransu S. Prusty 
413a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
4143787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
415478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
416391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
4173787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
418391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
419478f544eSSubhransu S. Prusty 	} else {
420478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
4213787a398SRakesh Ughreja 			snd_hdac_codec_write(hdev, pin->nid, 0,
422478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
423478f544eSSubhransu S. Prusty 	}
424a657f1d0SSubhransu S. Prusty 
425a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
4263787a398SRakesh Ughreja 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
4273787a398SRakesh Ughreja 	snd_hdac_codec_write(hdev, pin->nid, 0,
428a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
429a657f1d0SSubhransu S. Prusty 
430a657f1d0SSubhransu S. Prusty 	return 0;
431a657f1d0SSubhransu S. Prusty }
432a657f1d0SSubhransu S. Prusty 
433c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
434c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
435c9bfb5d7SJeeja KP 		int slots, int slot_width)
436b0362adbSSubhransu S. Prusty {
4373787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
4383787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
439754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
440c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
441c9bfb5d7SJeeja KP 
4423787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
443b0362adbSSubhransu S. Prusty 
444b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
445b0362adbSSubhransu S. Prusty 
446c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
447b0362adbSSubhransu S. Prusty 
448c9bfb5d7SJeeja KP 	if (pcm)
449c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
450bcced704SSubhransu S. Prusty 
451c9bfb5d7SJeeja KP 	return 0;
452b0362adbSSubhransu S. Prusty }
453b0362adbSSubhransu S. Prusty 
454b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
455b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
456b0362adbSSubhransu S. Prusty {
4573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
458754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
459c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
460c9bfb5d7SJeeja KP 	int format;
461b0362adbSSubhransu S. Prusty 
46254dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
463b0362adbSSubhransu S. Prusty 
464c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
465b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
46666d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
467b0362adbSSubhransu S. Prusty 
468c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
469c9bfb5d7SJeeja KP 	if (!pcm)
470148569fdSSubhransu S. Prusty 		return -EIO;
471148569fdSSubhransu S. Prusty 
472c9bfb5d7SJeeja KP 	pcm->format = format;
473c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
474148569fdSSubhransu S. Prusty 
475148569fdSSubhransu S. Prusty 	return 0;
476148569fdSSubhransu S. Prusty }
477148569fdSSubhransu S. Prusty 
4783787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
479754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
480754695f9SJeeja KP 					struct hdac_hdmi_port *port)
481148569fdSSubhransu S. Prusty {
4823787a398SRakesh Ughreja 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
4833787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
484148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
4853787a398SRakesh Ughreja 			pin->nid, get_wcaps(hdev, pin->nid));
486148569fdSSubhransu S. Prusty 		return -EINVAL;
487148569fdSSubhransu S. Prusty 	}
488148569fdSSubhransu S. Prusty 
4893787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
4901b46ebd1SJeeja KP 		return -EIO;
4911b46ebd1SJeeja KP 
4923787a398SRakesh Ughreja 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
493754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
494754695f9SJeeja KP 	if (port->num_mux_nids == 0)
4953787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
496754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
497754695f9SJeeja KP 						pin->nid, port->id);
498148569fdSSubhransu S. Prusty 
4993787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
500754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
501148569fdSSubhransu S. Prusty 
502754695f9SJeeja KP 	return port->num_mux_nids;
503148569fdSSubhransu S. Prusty }
504148569fdSSubhransu S. Prusty 
505148569fdSSubhransu S. Prusty /*
506754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
507148569fdSSubhransu S. Prusty  *
508754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
509148569fdSSubhransu S. Prusty  *
510754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
511754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
512148569fdSSubhransu S. Prusty  * connected.
513148569fdSSubhransu S. Prusty  */
514754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
5153787a398SRakesh Ughreja 			struct hdac_device *hdev,
516148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
517148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
518148569fdSSubhransu S. Prusty {
519148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
520754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
521148569fdSSubhransu S. Prusty 	int ret, i;
522148569fdSSubhransu S. Prusty 
523148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
524148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
525e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
526e0e5d3e5SJeeja KP 				continue;
527148569fdSSubhransu S. Prusty 
528e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
529e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
5303787a398SRakesh Ughreja 				ret = hdac_hdmi_query_port_connlist(hdev,
531e0e5d3e5SJeeja KP 							port->pin, port);
532e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
533148569fdSSubhransu S. Prusty 				if (ret < 0)
534e0e5d3e5SJeeja KP 					continue;
535148569fdSSubhransu S. Prusty 
536754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
537e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
538e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
539e0e5d3e5SJeeja KP 						port->eld.eld_valid)
540754695f9SJeeja KP 						return port;
541148569fdSSubhransu S. Prusty 				}
542148569fdSSubhransu S. Prusty 			}
543e0e5d3e5SJeeja KP 		}
544e0e5d3e5SJeeja KP 	}
545148569fdSSubhransu S. Prusty 
546148569fdSSubhransu S. Prusty 	return NULL;
547148569fdSSubhransu S. Prusty }
548148569fdSSubhransu S. Prusty 
54954dfa1eaSSubhransu S. Prusty /*
55054dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
55154dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
55254dfa1eaSSubhransu S. Prusty  * doesn't fail.
55354dfa1eaSSubhransu S. Prusty  */
554b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
555b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
556b0362adbSSubhransu S. Prusty {
5573787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
5583787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
559754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
560148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
561754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5622428bca3SSubhransu S. Prusty 	int ret;
563b0362adbSSubhransu S. Prusty 
564b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
565b0362adbSSubhransu S. Prusty 
566148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
5673787a398SRakesh Ughreja 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
56854dfa1eaSSubhransu S. Prusty 
56954dfa1eaSSubhransu S. Prusty 	/*
57054dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
57154dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
57254dfa1eaSSubhransu S. Prusty 	 */
573754695f9SJeeja KP 	if (!port)
57454dfa1eaSSubhransu S. Prusty 		return 0;
575754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
576754695f9SJeeja KP 			(!port->eld.eld_valid)) {
577b0362adbSSubhransu S. Prusty 
5783787a398SRakesh Ughreja 		dev_warn(&hdev->dev,
579754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
580754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
581754695f9SJeeja KP 			port->pin->nid, port->id);
582b8a54545SSubhransu S. Prusty 
58354dfa1eaSSubhransu S. Prusty 		return 0;
584b0362adbSSubhransu S. Prusty 	}
585b0362adbSSubhransu S. Prusty 
586754695f9SJeeja KP 	dai_map->port = port;
587b0362adbSSubhransu S. Prusty 
5882428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
589754695f9SJeeja KP 				port->eld.eld_buffer);
5902428bca3SSubhransu S. Prusty 	if (ret < 0)
5912428bca3SSubhransu S. Prusty 		return ret;
592b0362adbSSubhransu S. Prusty 
5932428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
594754695f9SJeeja KP 				port->eld.eld_buffer);
595b0362adbSSubhransu S. Prusty }
596b0362adbSSubhransu S. Prusty 
597b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
598b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
599b0362adbSSubhransu S. Prusty {
6003787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
601754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
602ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
603b0362adbSSubhransu S. Prusty 
604b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
605b0362adbSSubhransu S. Prusty 
606ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
607bcced704SSubhransu S. Prusty 
608ab1eea19SJeeja KP 	if (pcm) {
609ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
610ab1eea19SJeeja KP 		pcm->chmap_set = false;
611ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
612ab1eea19SJeeja KP 		pcm->channels = 0;
613ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
614b0362adbSSubhransu S. Prusty 	}
615ab1eea19SJeeja KP 
616754695f9SJeeja KP 	if (dai_map->port)
617754695f9SJeeja KP 		dai_map->port = NULL;
61854dfa1eaSSubhransu S. Prusty }
619b0362adbSSubhransu S. Prusty 
62018382eadSSubhransu S. Prusty static int
621f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
62218382eadSSubhransu S. Prusty {
623bcced704SSubhransu S. Prusty 	unsigned int chans;
624f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
62518382eadSSubhransu S. Prusty 	int err;
62618382eadSSubhransu S. Prusty 
627f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
628bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
629bcced704SSubhransu S. Prusty 
630bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
631bcced704SSubhransu S. Prusty 
632bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
633bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
634bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
63518382eadSSubhransu S. Prusty 
636f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
63718382eadSSubhransu S. Prusty 			&cvt->params.rates,
63818382eadSSubhransu S. Prusty 			&cvt->params.formats,
63918382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
64018382eadSSubhransu S. Prusty 	if (err < 0)
641f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
64218382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
64318382eadSSubhransu S. Prusty 			cvt->nid, err);
64418382eadSSubhransu S. Prusty 
64518382eadSSubhransu S. Prusty 	return err;
64618382eadSSubhransu S. Prusty }
64718382eadSSubhransu S. Prusty 
64879f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
649c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
650c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
651c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
652c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
653c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
65418382eadSSubhransu S. Prusty {
65518382eadSSubhransu S. Prusty 	w->id = id;
65679f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
65779f4e922SSubhransu S. Prusty 	if (!w->name)
65879f4e922SSubhransu S. Prusty 		return -ENOMEM;
65979f4e922SSubhransu S. Prusty 
66018382eadSSubhransu S. Prusty 	w->sname = stream;
66118382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
66218382eadSSubhransu S. Prusty 	w->shift = 0;
66379f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
66479f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
66579f4e922SSubhransu S. Prusty 	w->priv = priv;
666c9bfb5d7SJeeja KP 	w->event = event;
667c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
66879f4e922SSubhransu S. Prusty 
66979f4e922SSubhransu S. Prusty 	return 0;
67018382eadSSubhransu S. Prusty }
67118382eadSSubhransu S. Prusty 
67218382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
67379f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
67479f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
67579f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
67618382eadSSubhransu S. Prusty {
67718382eadSSubhransu S. Prusty 	route->sink = sink;
67818382eadSSubhransu S. Prusty 	route->source = src;
67918382eadSSubhransu S. Prusty 	route->control = control;
68079f4e922SSubhransu S. Prusty 	route->connected = handler;
68118382eadSSubhransu S. Prusty }
68218382eadSSubhransu S. Prusty 
6833787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
684754695f9SJeeja KP 					struct hdac_hdmi_port *port)
6854a3478deSJeeja KP {
6863787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
6874a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
688e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
6894a3478deSJeeja KP 
6904a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
691e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
692754695f9SJeeja KP 			continue;
693754695f9SJeeja KP 
694e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
695e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
6964a3478deSJeeja KP 				return pcm;
6974a3478deSJeeja KP 		}
698e0e5d3e5SJeeja KP 	}
6994a3478deSJeeja KP 
7004a3478deSJeeja KP 	return NULL;
7014a3478deSJeeja KP }
7024a3478deSJeeja KP 
7033787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
704c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
705c9bfb5d7SJeeja KP {
706753597fbSAbhijeet Kumar 	int count;
707753597fbSAbhijeet Kumar 	unsigned int state;
708753597fbSAbhijeet Kumar 
7093787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
7103787a398SRakesh Ughreja 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
711753597fbSAbhijeet Kumar 			for (count = 0; count < 10; count++) {
7123787a398SRakesh Ughreja 				snd_hdac_codec_read(hdev, nid, 0,
713753597fbSAbhijeet Kumar 						AC_VERB_SET_POWER_STATE,
714753597fbSAbhijeet Kumar 						pwr_state);
7153787a398SRakesh Ughreja 				state = snd_hdac_sync_power_state(hdev,
716753597fbSAbhijeet Kumar 						nid, pwr_state);
717753597fbSAbhijeet Kumar 				if (!(state & AC_PWRST_ERROR))
718753597fbSAbhijeet Kumar 					break;
719753597fbSAbhijeet Kumar 			}
720753597fbSAbhijeet Kumar 		}
721c9bfb5d7SJeeja KP 	}
722c9bfb5d7SJeeja KP }
723c9bfb5d7SJeeja KP 
7243787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev,
725c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
726c9bfb5d7SJeeja KP {
7273787a398SRakesh Ughreja 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
7283787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, nid, 0,
729c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
730c9bfb5d7SJeeja KP }
731c9bfb5d7SJeeja KP 
732c9bfb5d7SJeeja KP 
733c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
734c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
735c9bfb5d7SJeeja KP {
736754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
7373787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
738c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
739c9bfb5d7SJeeja KP 
7403787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
741c9bfb5d7SJeeja KP 			__func__, w->name, event);
742c9bfb5d7SJeeja KP 
7433787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
744c9bfb5d7SJeeja KP 	if (!pcm)
745c9bfb5d7SJeeja KP 		return -EIO;
746c9bfb5d7SJeeja KP 
7471b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7483787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
7491b46ebd1SJeeja KP 		return -EIO;
7501b46ebd1SJeeja KP 
751c9bfb5d7SJeeja KP 	switch (event) {
752c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7533787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
754c9bfb5d7SJeeja KP 
755c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
7563787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
757c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
758c9bfb5d7SJeeja KP 
7593787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
760c9bfb5d7SJeeja KP 
7613787a398SRakesh Ughreja 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
762c9bfb5d7SJeeja KP 
763c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
7643787a398SRakesh Ughreja 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
765c9bfb5d7SJeeja KP 
766c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
7673787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
768c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
769c9bfb5d7SJeeja KP 
7703787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
771c9bfb5d7SJeeja KP 		break;
772c9bfb5d7SJeeja KP 
773c9bfb5d7SJeeja KP 	}
774c9bfb5d7SJeeja KP 
775c9bfb5d7SJeeja KP 	return 0;
776c9bfb5d7SJeeja KP }
777c9bfb5d7SJeeja KP 
778c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
779c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
780c9bfb5d7SJeeja KP {
781c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
7823787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
7833787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
784c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
785c9bfb5d7SJeeja KP 
7863787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
787c9bfb5d7SJeeja KP 			__func__, w->name, event);
788c9bfb5d7SJeeja KP 
789c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
790c9bfb5d7SJeeja KP 	if (!pcm)
791c9bfb5d7SJeeja KP 		return -EIO;
792c9bfb5d7SJeeja KP 
793c9bfb5d7SJeeja KP 	switch (event) {
794c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
7953787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
796c9bfb5d7SJeeja KP 
797c9bfb5d7SJeeja KP 		/* Enable transmission */
7983787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
799c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
800c9bfb5d7SJeeja KP 
801c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
8023787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
803c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
804c9bfb5d7SJeeja KP 
8053787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
806c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
8073787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
808c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
809c9bfb5d7SJeeja KP 		break;
810c9bfb5d7SJeeja KP 
811c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
8123787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
813c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
8143787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, cvt->nid, 0,
815c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
816c9bfb5d7SJeeja KP 
8173787a398SRakesh Ughreja 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
818c9bfb5d7SJeeja KP 		break;
819c9bfb5d7SJeeja KP 
820c9bfb5d7SJeeja KP 	}
821c9bfb5d7SJeeja KP 
822c9bfb5d7SJeeja KP 	return 0;
823c9bfb5d7SJeeja KP }
824c9bfb5d7SJeeja KP 
825c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
826c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
827c9bfb5d7SJeeja KP {
828754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8293787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
830c9bfb5d7SJeeja KP 	int mux_idx;
831c9bfb5d7SJeeja KP 
8323787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
833c9bfb5d7SJeeja KP 			__func__, w->name, event);
834c9bfb5d7SJeeja KP 
835c9bfb5d7SJeeja KP 	if (!kc)
836c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
837c9bfb5d7SJeeja KP 
838c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8391b46ebd1SJeeja KP 
8401b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8413787a398SRakesh Ughreja 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
8421b46ebd1SJeeja KP 		return -EIO;
8431b46ebd1SJeeja KP 
844c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
8453787a398SRakesh Ughreja 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
846c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
847c9bfb5d7SJeeja KP 	}
848c9bfb5d7SJeeja KP 
849c9bfb5d7SJeeja KP 	return 0;
850c9bfb5d7SJeeja KP }
851c9bfb5d7SJeeja KP 
8524a3478deSJeeja KP /*
8534a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8544a3478deSJeeja KP  */
855754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8564a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8574a3478deSJeeja KP {
8584a3478deSJeeja KP 	int ret;
859e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8604a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8614a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8624a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
863754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8643787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
8653787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
8664a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8674a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8684a3478deSJeeja KP 
8694a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8704a3478deSJeeja KP 	if (ret < 0)
8714a3478deSJeeja KP 		return ret;
8724a3478deSJeeja KP 
873754695f9SJeeja KP 	if (port == NULL)
874754695f9SJeeja KP 		return -EINVAL;
875754695f9SJeeja KP 
8764a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8774a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
878e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
879e0e5d3e5SJeeja KP 			continue;
880e0e5d3e5SJeeja KP 
881e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
882e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
883e0e5d3e5SJeeja KP 					p->pin == port->pin) {
884e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
885e0e5d3e5SJeeja KP 				list_del(&p->head);
886e0e5d3e5SJeeja KP 			}
887e0e5d3e5SJeeja KP 		}
888e0e5d3e5SJeeja KP 	}
8894a3478deSJeeja KP 
8904a3478deSJeeja KP 	/*
8914a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8924a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8934a3478deSJeeja KP 	 */
894e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
895e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
896e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
897754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
898e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
8994a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9004a3478deSJeeja KP 				return ret;
9014a3478deSJeeja KP 			}
9024a3478deSJeeja KP 		}
903e0e5d3e5SJeeja KP 	}
9044a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9054a3478deSJeeja KP 
9064a3478deSJeeja KP 	return ret;
9074a3478deSJeeja KP }
9084a3478deSJeeja KP 
90979f4e922SSubhransu S. Prusty /*
91079f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
91179f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
91279f4e922SSubhransu S. Prusty  * widget runtime.
91379f4e922SSubhransu S. Prusty  *
91479f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
91579f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
91679f4e922SSubhransu S. Prusty  * "NONE"
91779f4e922SSubhransu S. Prusty  */
9183787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
919754695f9SJeeja KP 				struct hdac_hdmi_port *port,
92079f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
92179f4e922SSubhransu S. Prusty 				const char *widget_name)
92218382eadSSubhransu S. Prusty {
9233787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
924754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
92579f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
92679f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
92779f4e922SSubhransu S. Prusty 	struct soc_enum *se;
92879f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
92979f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
93079f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
93179f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
93279f4e922SSubhransu S. Prusty 	int i = 0;
93379f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
93418382eadSSubhransu S. Prusty 
9353787a398SRakesh Ughreja 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
93679f4e922SSubhransu S. Prusty 	if (!kc)
93779f4e922SSubhransu S. Prusty 		return -ENOMEM;
93818382eadSSubhransu S. Prusty 
9393787a398SRakesh Ughreja 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
94079f4e922SSubhransu S. Prusty 	if (!se)
94179f4e922SSubhransu S. Prusty 		return -ENOMEM;
94218382eadSSubhransu S. Prusty 
94370e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
94470e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
9453787a398SRakesh Ughreja 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
94679f4e922SSubhransu S. Prusty 	if (!kc->name)
94779f4e922SSubhransu S. Prusty 		return -ENOMEM;
94818382eadSSubhransu S. Prusty 
94979f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
95079f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
95179f4e922SSubhransu S. Prusty 	kc->access = 0;
95279f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
953754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95479f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
95579f4e922SSubhransu S. Prusty 
95679f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
95779f4e922SSubhransu S. Prusty 
95879f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
95979f4e922SSubhransu S. Prusty 	se->items = num_items;
96079f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
96179f4e922SSubhransu S. Prusty 
96279f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
9633787a398SRakesh Ughreja 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
96479f4e922SSubhransu S. Prusty 	if (!items[i])
96579f4e922SSubhransu S. Prusty 		return -ENOMEM;
96679f4e922SSubhransu S. Prusty 
96779f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
96879f4e922SSubhransu S. Prusty 		i++;
96979f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
9703787a398SRakesh Ughreja 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
97179f4e922SSubhransu S. Prusty 		if (!items[i])
97279f4e922SSubhransu S. Prusty 			return -ENOMEM;
97379f4e922SSubhransu S. Prusty 	}
97479f4e922SSubhransu S. Prusty 
9753787a398SRakesh Ughreja 	se->texts = devm_kmemdup(&hdev->dev, items,
97679f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
97779f4e922SSubhransu S. Prusty 	if (!se->texts)
97879f4e922SSubhransu S. Prusty 		return -ENOMEM;
97979f4e922SSubhransu S. Prusty 
9803787a398SRakesh Ughreja 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
981754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
982c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
983c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98479f4e922SSubhransu S. Prusty }
98579f4e922SSubhransu S. Prusty 
98679f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
9873787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
98879f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
98979f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
99079f4e922SSubhransu S. Prusty {
9913787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
99279f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
99379f4e922SSubhransu S. Prusty 	struct soc_enum *se;
994754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
99579f4e922SSubhransu S. Prusty 	int i, j;
99679f4e922SSubhransu S. Prusty 
997754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
99879f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
99979f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
100079f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
100179f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
100279f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
100379f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100479f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
100579f4e922SSubhransu S. Prusty 
100679f4e922SSubhransu S. Prusty 			rindex++;
100779f4e922SSubhransu S. Prusty 		}
100879f4e922SSubhransu S. Prusty 
100979f4e922SSubhransu S. Prusty 		mux_index++;
101079f4e922SSubhransu S. Prusty 	}
101179f4e922SSubhransu S. Prusty }
101279f4e922SSubhransu S. Prusty 
101379f4e922SSubhransu S. Prusty /*
101479f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
101579f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1016754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1017754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
101879f4e922SSubhransu S. Prusty  *
1019754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1020754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
102179f4e922SSubhransu S. Prusty  *
102279f4e922SSubhransu S. Prusty  * Routes are added as below:
1023754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1024754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
102579f4e922SSubhransu S. Prusty  *
102679f4e922SSubhransu S. Prusty  * Total route elements:
1027754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
102879f4e922SSubhransu S. Prusty  */
102979f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
103079f4e922SSubhransu S. Prusty {
103179f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
103279f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
10333787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
10343787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
10351e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
103679f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
103779f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
103879f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1039754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
104079f4e922SSubhransu S. Prusty 
104179f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
104279f4e922SSubhransu S. Prusty 		return -EINVAL;
104379f4e922SSubhransu S. Prusty 
1044754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1045754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
104679f4e922SSubhransu S. Prusty 				GFP_KERNEL);
104779f4e922SSubhransu S. Prusty 
104879f4e922SSubhransu S. Prusty 	if (!widgets)
104979f4e922SSubhransu S. Prusty 		return -ENOMEM;
105079f4e922SSubhransu S. Prusty 
105179f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
105279f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
105379f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105479f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1055c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1056c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1057c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1058c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
105979f4e922SSubhransu S. Prusty 		if (ret < 0)
106079f4e922SSubhransu S. Prusty 			return ret;
106179f4e922SSubhransu S. Prusty 		i++;
106279f4e922SSubhransu S. Prusty 	}
106379f4e922SSubhransu S. Prusty 
106479f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1065754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1066754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1067754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
106879f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1069754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1070c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1071c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1072754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1073754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107479f4e922SSubhransu S. Prusty 			if (ret < 0)
107579f4e922SSubhransu S. Prusty 				return ret;
10760324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
107779f4e922SSubhransu S. Prusty 			i++;
107879f4e922SSubhransu S. Prusty 		}
1079754695f9SJeeja KP 	}
108079f4e922SSubhransu S. Prusty 
108179f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
108279f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1083754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1084754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1085754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
10863787a398SRakesh Ughreja 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1087754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
108879f4e922SSubhransu S. Prusty 						widget_name);
108979f4e922SSubhransu S. Prusty 			if (ret < 0)
109079f4e922SSubhransu S. Prusty 				return ret;
109179f4e922SSubhransu S. Prusty 			i++;
109279f4e922SSubhransu S. Prusty 
109379f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109479f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
109579f4e922SSubhransu S. Prusty 
109679f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
109779f4e922SSubhransu S. Prusty 			num_routes++;
109879f4e922SSubhransu S. Prusty 		}
1099754695f9SJeeja KP 	}
110079f4e922SSubhransu S. Prusty 
110179f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
110279f4e922SSubhransu S. Prusty 							GFP_KERNEL);
110379f4e922SSubhransu S. Prusty 	if (!route)
110479f4e922SSubhransu S. Prusty 		return -ENOMEM;
110579f4e922SSubhransu S. Prusty 
110679f4e922SSubhransu S. Prusty 	i = 0;
110779f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
110879f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1109754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
111079f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1111754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1112754695f9SJeeja KP 						hdmi->num_pin;
111379f4e922SSubhransu S. Prusty 
111479f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
111579f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
111679f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
111779f4e922SSubhransu S. Prusty 			i++;
1118754695f9SJeeja KP 		}
111979f4e922SSubhransu S. Prusty 	}
112079f4e922SSubhransu S. Prusty 
11213787a398SRakesh Ughreja 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
112279f4e922SSubhransu S. Prusty 
112379f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1124754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
112579f4e922SSubhransu S. Prusty 
112679f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
112779f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
112879f4e922SSubhransu S. Prusty 
112979f4e922SSubhransu S. Prusty 	return 0;
113079f4e922SSubhransu S. Prusty 
113118382eadSSubhransu S. Prusty }
113218382eadSSubhransu S. Prusty 
11333787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
113418382eadSSubhransu S. Prusty {
11353787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1136754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
113715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1138148569fdSSubhransu S. Prusty 	int dai_id = 0;
113918382eadSSubhransu S. Prusty 
1140148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
114115b91447SSubhransu S. Prusty 		return -EINVAL;
114218382eadSSubhransu S. Prusty 
1143148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1144148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1145148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
114615b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
114718382eadSSubhransu S. Prusty 
1148148569fdSSubhransu S. Prusty 		dai_id++;
1149148569fdSSubhransu S. Prusty 
1150148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
11513787a398SRakesh Ughreja 			dev_warn(&hdev->dev,
1152148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1153148569fdSSubhransu S. Prusty 			break;
1154148569fdSSubhransu S. Prusty 		}
1155148569fdSSubhransu S. Prusty 	}
115618382eadSSubhransu S. Prusty 
115715b91447SSubhransu S. Prusty 	return 0;
115815b91447SSubhransu S. Prusty }
115915b91447SSubhransu S. Prusty 
11603787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
116115b91447SSubhransu S. Prusty {
11623787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
116315b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11644a3478deSJeeja KP 	char name[NAME_SIZE];
116515b91447SSubhransu S. Prusty 
1166c7ba4e53SPierre-Louis Bossart 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
116715b91447SSubhransu S. Prusty 	if (!cvt)
116815b91447SSubhransu S. Prusty 		return -ENOMEM;
116915b91447SSubhransu S. Prusty 
117015b91447SSubhransu S. Prusty 	cvt->nid = nid;
11714a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
1172c7ba4e53SPierre-Louis Bossart 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1173c7ba4e53SPierre-Louis Bossart 	if (!cvt->name)
1174c7ba4e53SPierre-Louis Bossart 		return -ENOMEM;
117515b91447SSubhransu S. Prusty 
117615b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
117715b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
117815b91447SSubhransu S. Prusty 
11793787a398SRakesh Ughreja 	return hdac_hdmi_query_cvt_params(hdev, cvt);
118015b91447SSubhransu S. Prusty }
118115b91447SSubhransu S. Prusty 
11823787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1183754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1184b7756edeSSubhransu S. Prusty {
1185f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1186f6fa11a3SSandeep Tayal 
1187754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1188f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1189f6fa11a3SSandeep Tayal 
1190f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
11913787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1192f6fa11a3SSandeep Tayal 		return -EINVAL;
1193b7756edeSSubhransu S. Prusty 	}
1194b7756edeSSubhransu S. Prusty 
1195754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1196f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1197f6fa11a3SSandeep Tayal 
1198f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
11993787a398SRakesh Ughreja 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1200f6fa11a3SSandeep Tayal 		return -EINVAL;
1201f6fa11a3SSandeep Tayal 	}
1202f6fa11a3SSandeep Tayal 
1203754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1204f6fa11a3SSandeep Tayal 
1205f6fa11a3SSandeep Tayal 	return 0;
1206f6fa11a3SSandeep Tayal }
1207f6fa11a3SSandeep Tayal 
1208754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1209754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1210b8a54545SSubhransu S. Prusty {
12113787a398SRakesh Ughreja 	struct hdac_device *hdev = pin->hdev;
12123787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
12134a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1214754695f9SJeeja KP 	int size = 0;
12152acd8309SJeeja KP 	int port_id = -1;
1216754695f9SJeeja KP 
1217754695f9SJeeja KP 	if (!hdmi)
1218754695f9SJeeja KP 		return;
12194a3478deSJeeja KP 
12202acd8309SJeeja KP 	/*
12212acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12222acd8309SJeeja KP 	 * to be -1.
12232acd8309SJeeja KP 	 */
12244a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1225754695f9SJeeja KP 	port->eld.monitor_present = false;
1226f6fa11a3SSandeep Tayal 
12272acd8309SJeeja KP 	if (pin->mst_capable)
12282acd8309SJeeja KP 		port_id = port->id;
12292acd8309SJeeja KP 
12303787a398SRakesh Ughreja 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1231754695f9SJeeja KP 				&port->eld.monitor_present,
1232754695f9SJeeja KP 				port->eld.eld_buffer,
1233f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1234f6fa11a3SSandeep Tayal 
1235f6fa11a3SSandeep Tayal 	if (size > 0) {
1236f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
12373787a398SRakesh Ughreja 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1238f6fa11a3SSandeep Tayal 			size = -EINVAL;
1239f6fa11a3SSandeep Tayal 	}
1240f6fa11a3SSandeep Tayal 
1241f6fa11a3SSandeep Tayal 	if (size > 0) {
1242754695f9SJeeja KP 		port->eld.eld_valid = true;
1243754695f9SJeeja KP 		port->eld.eld_size = size;
1244f6fa11a3SSandeep Tayal 	} else {
1245754695f9SJeeja KP 		port->eld.eld_valid = false;
1246754695f9SJeeja KP 		port->eld.eld_size = 0;
1247f6fa11a3SSandeep Tayal 	}
1248b8a54545SSubhransu S. Prusty 
12493787a398SRakesh Ughreja 	pcm = hdac_hdmi_get_pcm(hdev, port);
12504a3478deSJeeja KP 
1251754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1252b8a54545SSubhransu S. Prusty 
12533787a398SRakesh Ughreja 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1254754695f9SJeeja KP 						__func__, pin->nid, port->id);
12554a3478deSJeeja KP 
12564a3478deSJeeja KP 		/*
12574a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12584a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12594a3478deSJeeja KP 		 * control select.
12604a3478deSJeeja KP 		 */
1261e0e5d3e5SJeeja KP 		if (pcm)
1262e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12634a3478deSJeeja KP 
12644a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1265f6fa11a3SSandeep Tayal 		return;
1266b8a54545SSubhransu S. Prusty 	}
1267b8a54545SSubhransu S. Prusty 
1268754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1269e0e5d3e5SJeeja KP 		if (pcm)
1270e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12714a3478deSJeeja KP 
1272f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1273754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1274754695f9SJeeja KP 
1275754695f9SJeeja KP 	}
1276754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12774a3478deSJeeja KP }
12784a3478deSJeeja KP 
1279c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1280754695f9SJeeja KP 			       struct hdac_hdmi_pin *pin)
1281754695f9SJeeja KP {
1282754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1283754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1284754695f9SJeeja KP 	int i;
1285754695f9SJeeja KP 
1286754695f9SJeeja KP 	/*
1287754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1288754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1289754695f9SJeeja KP 	 * implemented.
1290754695f9SJeeja KP 	 */
1291754695f9SJeeja KP 
1292c7ba4e53SPierre-Louis Bossart 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1293754695f9SJeeja KP 	if (!ports)
1294754695f9SJeeja KP 		return -ENOMEM;
1295754695f9SJeeja KP 
1296754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1297754695f9SJeeja KP 		ports[i].id = i;
1298754695f9SJeeja KP 		ports[i].pin = pin;
1299754695f9SJeeja KP 	}
1300754695f9SJeeja KP 	pin->ports = ports;
1301754695f9SJeeja KP 	pin->num_ports = max_ports;
1302754695f9SJeeja KP 	return 0;
1303b8a54545SSubhransu S. Prusty }
1304b8a54545SSubhransu S. Prusty 
13053787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
130615b91447SSubhransu S. Prusty {
13073787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
130815b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1309754695f9SJeeja KP 	int ret;
131015b91447SSubhransu S. Prusty 
1311c7ba4e53SPierre-Louis Bossart 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
131215b91447SSubhransu S. Prusty 	if (!pin)
131315b91447SSubhransu S. Prusty 		return -ENOMEM;
131415b91447SSubhransu S. Prusty 
131515b91447SSubhransu S. Prusty 	pin->nid = nid;
13162acd8309SJeeja KP 	pin->mst_capable = false;
13173787a398SRakesh Ughreja 	pin->hdev = hdev;
1318c7ba4e53SPierre-Louis Bossart 	ret = hdac_hdmi_add_ports(hdev, pin);
1319754695f9SJeeja KP 	if (ret < 0)
1320754695f9SJeeja KP 		return ret;
132115b91447SSubhransu S. Prusty 
132215b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
132315b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1324754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1325b8a54545SSubhransu S. Prusty 
132615b91447SSubhransu S. Prusty 	return 0;
132718382eadSSubhransu S. Prusty }
132818382eadSSubhransu S. Prusty 
1329019033c8SBard liao #define INTEL_VENDOR_NID_0x2 0x02
1330019033c8SBard liao #define INTEL_VENDOR_NID_0x8 0x08
1331019033c8SBard liao #define INTEL_VENDOR_NID_0xb 0x0b
1332211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1333211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1334211caab7SSubhransu S. Prusty #define INTEL_EN_DP12		0x02 /* enable DP 1.2 features */
1335211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1336211caab7SSubhransu S. Prusty 
1337f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1338211caab7SSubhransu S. Prusty {
1339211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1340f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13415622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1342211caab7SSubhransu S. Prusty 
1343f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1344211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1345211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1346211caab7SSubhransu S. Prusty 		return;
1347211caab7SSubhransu S. Prusty 
1348211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1349f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1350211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1351211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1352211caab7SSubhransu S. Prusty 		return;
1353211caab7SSubhransu S. Prusty }
1354211caab7SSubhransu S. Prusty 
1355f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1356211caab7SSubhransu S. Prusty {
1357211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1358f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13595622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1360211caab7SSubhransu S. Prusty 
1361f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1362211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1363211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1364211caab7SSubhransu S. Prusty 		return;
1365211caab7SSubhransu S. Prusty 
1366211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1367211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1368f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1369211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1370211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1371211caab7SSubhransu S. Prusty 		return;
1372211caab7SSubhransu S. Prusty 
1373211caab7SSubhransu S. Prusty }
1374211caab7SSubhransu S. Prusty 
137561b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
137617a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
137717a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
137817a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1379c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
138017a42c45SSubhransu S. Prusty };
138117a42c45SSubhransu S. Prusty 
138217a42c45SSubhransu S. Prusty /*
138317a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
138417a42c45SSubhransu S. Prusty  * based on the number of converter queried.
138517a42c45SSubhransu S. Prusty  */
1386f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
138717a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
138817a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
138917a42c45SSubhransu S. Prusty {
139017a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
139117a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
139217a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
139317a42c45SSubhransu S. Prusty 	int i = 0;
139417a42c45SSubhransu S. Prusty 	u32 rates, bps;
139517a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
139617a42c45SSubhransu S. Prusty 	u64 formats;
139717a42c45SSubhransu S. Prusty 	int ret;
139817a42c45SSubhransu S. Prusty 
1399f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
140017a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
140117a42c45SSubhransu S. Prusty 			GFP_KERNEL);
140217a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
140317a42c45SSubhransu S. Prusty 		return -ENOMEM;
140417a42c45SSubhransu S. Prusty 
140517a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1406f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
140717a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
140817a42c45SSubhransu S. Prusty 		if (ret)
140917a42c45SSubhransu S. Prusty 			return ret;
141017a42c45SSubhransu S. Prusty 
14113b857472SYong Zhi 		/* Filter out 44.1, 88.2 and 176.4Khz */
14123b857472SYong Zhi 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
14133b857472SYong Zhi 			   SNDRV_PCM_RATE_176400);
14143b857472SYong Zhi 		if (!rates)
14153b857472SYong Zhi 			return -EINVAL;
14163b857472SYong Zhi 
141717a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1418f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
141917a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
142017a42c45SSubhransu S. Prusty 
142117a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
142217a42c45SSubhransu S. Prusty 			return -ENOMEM;
142317a42c45SSubhransu S. Prusty 
142417a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
142517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1426f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
142717a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142817a42c45SSubhransu S. Prusty 			return -ENOMEM;
142917a42c45SSubhransu S. Prusty 
143017a42c45SSubhransu S. Prusty 		/*
143117a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
143217a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
143317a42c45SSubhransu S. Prusty 		 */
143417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
143517a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
143617a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
143717a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143817a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
144066d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
144117a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
144217a42c45SSubhransu S. Prusty 		i++;
144317a42c45SSubhransu S. Prusty 	}
144417a42c45SSubhransu S. Prusty 
144517a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14461e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
144717a42c45SSubhransu S. Prusty 
144817a42c45SSubhransu S. Prusty 	return 0;
144917a42c45SSubhransu S. Prusty }
145017a42c45SSubhransu S. Prusty 
145118382eadSSubhransu S. Prusty /*
145218382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
145318382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
145418382eadSSubhransu S. Prusty  */
14553787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
145617a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
145718382eadSSubhransu S. Prusty {
145818382eadSSubhransu S. Prusty 	hda_nid_t nid;
14593c83ac23SSudip Mukherjee 	int i, num_nodes;
14603787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
146115b91447SSubhransu S. Prusty 	int ret;
146218382eadSSubhransu S. Prusty 
1463f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1464f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1465211caab7SSubhransu S. Prusty 
1466f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1467541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1468f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
146918382eadSSubhransu S. Prusty 		return -EINVAL;
147018382eadSSubhransu S. Prusty 	}
147118382eadSSubhransu S. Prusty 
147245a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
147318382eadSSubhransu S. Prusty 		unsigned int caps;
147418382eadSSubhransu S. Prusty 		unsigned int type;
147518382eadSSubhransu S. Prusty 
1476f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
147718382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147818382eadSSubhransu S. Prusty 
147918382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
148018382eadSSubhransu S. Prusty 			continue;
148118382eadSSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty 		switch (type) {
148318382eadSSubhransu S. Prusty 
148418382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
14853787a398SRakesh Ughreja 			ret = hdac_hdmi_add_cvt(hdev, nid);
148615b91447SSubhransu S. Prusty 			if (ret < 0)
1487c7ba4e53SPierre-Louis Bossart 				return ret;
148818382eadSSubhransu S. Prusty 			break;
148918382eadSSubhransu S. Prusty 
149018382eadSSubhransu S. Prusty 		case AC_WID_PIN:
14913787a398SRakesh Ughreja 			ret = hdac_hdmi_add_pin(hdev, nid);
149215b91447SSubhransu S. Prusty 			if (ret < 0)
1493c7ba4e53SPierre-Louis Bossart 				return ret;
149418382eadSSubhransu S. Prusty 			break;
149518382eadSSubhransu S. Prusty 		}
149618382eadSSubhransu S. Prusty 	}
149718382eadSSubhransu S. Prusty 
14981c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
14991c0a7de2SSubhransu S. Prusty 		ret = -EIO;
1500c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1501c7ba4e53SPierre-Louis Bossart 		return ret;
15021c0a7de2SSubhransu S. Prusty 	}
150318382eadSSubhransu S. Prusty 
1504f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
150517a42c45SSubhransu S. Prusty 	if (ret) {
1506f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
150717a42c45SSubhransu S. Prusty 			ret);
1508c7ba4e53SPierre-Louis Bossart 		return ret;
150917a42c45SSubhransu S. Prusty 	}
151017a42c45SSubhransu S. Prusty 
151117a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15123787a398SRakesh Ughreja 	ret = hdac_hdmi_init_dai_map(hdev);
15131c0a7de2SSubhransu S. Prusty 	if (ret < 0)
1514c7ba4e53SPierre-Louis Bossart 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1515c7ba4e53SPierre-Louis Bossart 			ret);
15161c0a7de2SSubhransu S. Prusty 	return ret;
151718382eadSSubhransu S. Prusty }
151818382eadSSubhransu S. Prusty 
1519a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin)
1520a57942bfSTakashi Iwai {
1521019033c8SBard liao 	struct hdac_device *hdev = aptr;
1522019033c8SBard liao 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1523019033c8SBard liao 	const int *map = hdmi->drv_data->port_map;
1524019033c8SBard liao 	int i;
1525019033c8SBard liao 
1526019033c8SBard liao 	if (!hdmi->drv_data->port_num)
1527a57942bfSTakashi Iwai 		return pin - 4; /* map NID 0x05 -> port #1 */
1528019033c8SBard liao 
1529019033c8SBard liao 	/*
1530019033c8SBard liao 	 * looking for the pin number in the mapping table and return
1531019033c8SBard liao 	 * the index which indicate the port number
1532019033c8SBard liao 	 */
1533019033c8SBard liao 	for (i = 0; i < hdmi->drv_data->port_num; i++) {
1534019033c8SBard liao 		if (pin == map[i])
1535019033c8SBard liao 			return i + 1;
1536019033c8SBard liao 	}
1537019033c8SBard liao 
1538019033c8SBard liao 	/* return -1 if pin number exceeds our expectation */
1539019033c8SBard liao 	dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1540019033c8SBard liao 	return -1;
1541a57942bfSTakashi Iwai }
1542a57942bfSTakashi Iwai 
1543f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1544b8a54545SSubhransu S. Prusty {
15453787a398SRakesh Ughreja 	struct hdac_device *hdev = aptr;
15463787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1547754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1548754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
15493787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
15502acd8309SJeeja KP 	int i;
1551019033c8SBard liao 	hda_nid_t pin_nid;
1552b8a54545SSubhransu S. Prusty 
1553019033c8SBard liao 	if (!hdmi->drv_data->port_num) {
1554019033c8SBard liao 		/* for legacy platforms */
1555019033c8SBard liao 		pin_nid = port + 0x04;
1556019033c8SBard liao 	} else if (port < hdmi->drv_data->port_num) {
1557019033c8SBard liao 		/* get pin number from the pin2port mapping table */
1558019033c8SBard liao 		pin_nid = hdmi->drv_data->port_map[port - 1];
1559019033c8SBard liao 	} else {
1560019033c8SBard liao 		dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1561019033c8SBard liao 		return;
1562019033c8SBard liao 	}
1563b8a54545SSubhransu S. Prusty 
15643787a398SRakesh Ughreja 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1565754695f9SJeeja KP 							pin_nid, pipe);
1566b8a54545SSubhransu S. Prusty 
1567b8a54545SSubhransu S. Prusty 	/*
1568b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1569b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1570b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1571b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1572b8a54545SSubhransu S. Prusty 	 */
157345101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1574b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1575b8a54545SSubhransu S. Prusty 		return;
1576b8a54545SSubhransu S. Prusty 
15773787a398SRakesh Ughreja 	if (atomic_read(&hdev->in_pm))
1578b8a54545SSubhransu S. Prusty 		return;
1579b8a54545SSubhransu S. Prusty 
1580b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1581754695f9SJeeja KP 		if (pin->nid != pin_nid)
1582754695f9SJeeja KP 			continue;
1583754695f9SJeeja KP 
1584754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1585754695f9SJeeja KP 		if (pipe == -1) {
15862acd8309SJeeja KP 			pin->mst_capable = false;
1587754695f9SJeeja KP 			/* if not MST, default is port[0] */
1588754695f9SJeeja KP 			hport = &pin->ports[0];
15892acd8309SJeeja KP 		} else {
15902acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15912acd8309SJeeja KP 				pin->mst_capable = true;
15922acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15932acd8309SJeeja KP 					hport = &pin->ports[i];
159404c8f2bfSJeeja KP 					break;
15952acd8309SJeeja KP 				}
1596b8a54545SSubhransu S. Prusty 			}
1597b8a54545SSubhransu S. Prusty 		}
1598b8a54545SSubhransu S. Prusty 
159904c8f2bfSJeeja KP 		if (hport)
1600754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1601754695f9SJeeja KP 	}
1602754695f9SJeeja KP 
160304c8f2bfSJeeja KP }
160404c8f2bfSJeeja KP 
1605ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = {
1606a57942bfSTakashi Iwai 	.pin2port	= hdac_hdmi_pin2port,
1607b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1608b8a54545SSubhransu S. Prusty };
1609b8a54545SSubhransu S. Prusty 
16102889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
16112889099eSSubhransu S. Prusty 						int device)
16122889099eSSubhransu S. Prusty {
16132889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
16142889099eSSubhransu S. Prusty 
1615bcb1fd1fSKuninori Morimoto 	for_each_card_rtds(card, rtd) {
16162889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
16172889099eSSubhransu S. Prusty 			return rtd->pcm;
16182889099eSSubhransu S. Prusty 	}
16192889099eSSubhransu S. Prusty 
16202889099eSSubhransu S. Prusty 	return NULL;
16212889099eSSubhransu S. Prusty }
16222889099eSSubhransu S. Prusty 
16230324e51bSJeeja KP /* create jack pin kcontrols */
16240324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16253787a398SRakesh Ughreja 				    struct hdac_device *hdev)
16260324e51bSJeeja KP {
16270324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16280324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16290324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16300324e51bSJeeja KP 	char *name;
16310324e51bSJeeja KP 	int i = 0, j;
16323787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
16333787a398SRakesh Ughreja 	struct snd_soc_component *component = hdmi->component;
16340324e51bSJeeja KP 
163545101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16360324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16370324e51bSJeeja KP 
16380324e51bSJeeja KP 	if (!kc)
16390324e51bSJeeja KP 		return -ENOMEM;
16400324e51bSJeeja KP 
16410324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16420324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16430324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16440324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
164545101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16460324e51bSJeeja KP 			if (!name)
16470324e51bSJeeja KP 				return -ENOMEM;
16480324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
164945101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16500324e51bSJeeja KP 							GFP_KERNEL);
16510324e51bSJeeja KP 			if (!kc[i].name)
16520324e51bSJeeja KP 				return -ENOMEM;
16530324e51bSJeeja KP 
16540324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16550324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16560324e51bSJeeja KP 			kc[i].access = 0;
16570324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16580324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16590324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16600324e51bSJeeja KP 			i++;
16610324e51bSJeeja KP 		}
16620324e51bSJeeja KP 	}
16630324e51bSJeeja KP 
16640324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16650324e51bSJeeja KP }
16660324e51bSJeeja KP 
166745101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16680324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16690324e51bSJeeja KP {
16703787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
16713787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
16720324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16730324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16740324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16750324e51bSJeeja KP 	char w_name[NAME_SIZE];
16760324e51bSJeeja KP 	int i = 0, j, ret;
16770324e51bSJeeja KP 
16780324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16790324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16800324e51bSJeeja KP 
16810324e51bSJeeja KP 	if (!widgets)
16820324e51bSJeeja KP 		return -ENOMEM;
16830324e51bSJeeja KP 
16840324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16850324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16860324e51bSJeeja KP 	if (!route)
16870324e51bSJeeja KP 		return -ENOMEM;
16880324e51bSJeeja KP 
16890324e51bSJeeja KP 	/* create Jack DAPM widget */
16900324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16910324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16920324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16930324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16940324e51bSJeeja KP 
16950324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16960324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16970324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16980324e51bSJeeja KP 			if (ret < 0)
16990324e51bSJeeja KP 				return ret;
17000324e51bSJeeja KP 
17010324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
17020324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
17030324e51bSJeeja KP 
17040324e51bSJeeja KP 			/* add to route from Jack widget to output */
17050324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
17060324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
17070324e51bSJeeja KP 
17080324e51bSJeeja KP 			i++;
17090324e51bSJeeja KP 		}
17100324e51bSJeeja KP 	}
17110324e51bSJeeja KP 
17120324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
17130324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
17140324e51bSJeeja KP 	if (ret < 0)
17150324e51bSJeeja KP 		return ret;
17160324e51bSJeeja KP 
17170324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
17180324e51bSJeeja KP 	if (ret < 0)
17190324e51bSJeeja KP 		return ret;
17200324e51bSJeeja KP 
17210324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17220324e51bSJeeja KP 	if (ret < 0)
17230324e51bSJeeja KP 		return ret;
17240324e51bSJeeja KP 
17250324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17263787a398SRakesh Ughreja 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
17270324e51bSJeeja KP 
17280324e51bSJeeja KP 	if (ret < 0)
17290324e51bSJeeja KP 		return ret;
17300324e51bSJeeja KP 
17310324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17320324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17330324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17340324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17350324e51bSJeeja KP 						pin->ports[j].jack_pin);
17360324e51bSJeeja KP 	}
17370324e51bSJeeja KP 
17380324e51bSJeeja KP 	return 0;
17390324e51bSJeeja KP }
17400324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17410324e51bSJeeja KP 
174262490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
174362490016SJeeja KP 				struct snd_soc_jack *jack)
17444a3478deSJeeja KP {
174545101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
17463787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
17473787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
17484a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17492889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17502889099eSSubhransu S. Prusty 	int err;
17514a3478deSJeeja KP 
17524a3478deSJeeja KP 	/*
17534a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17544a3478deSJeeja KP 	 * add to the pcm list
17554a3478deSJeeja KP 	 */
1756c7ba4e53SPierre-Louis Bossart 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
17574a3478deSJeeja KP 	if (!pcm)
17584a3478deSJeeja KP 		return -ENOMEM;
17594a3478deSJeeja KP 	pcm->pcm_id = device;
17604a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1761e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
176262490016SJeeja KP 	pcm->jack = jack;
1763ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1764e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17652889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17662889099eSSubhransu S. Prusty 	if (snd_pcm) {
17672889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17682889099eSSubhransu S. Prusty 		if (err < 0) {
17693787a398SRakesh Ughreja 			dev_err(&hdev->dev,
17702889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17712889099eSSubhransu S. Prusty 				err, device);
17722889099eSSubhransu S. Prusty 			return err;
17732889099eSSubhransu S. Prusty 		}
17742889099eSSubhransu S. Prusty 	}
17752889099eSSubhransu S. Prusty 
17764a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17774a3478deSJeeja KP 
177862490016SJeeja KP 	return 0;
17794a3478deSJeeja KP }
17804a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17814a3478deSJeeja KP 
17823787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1783a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1784a9ce96bcSJeeja KP {
1785a9ce96bcSJeeja KP 	int i;
1786a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1787a9ce96bcSJeeja KP 
1788a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1789a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1790a9ce96bcSJeeja KP 
17913787a398SRakesh Ughreja 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1792a9ce96bcSJeeja KP 				pin->mst_capable = false;
1793a9ce96bcSJeeja KP 			else
1794a9ce96bcSJeeja KP 				pin->mst_capable = true;
1795a9ce96bcSJeeja KP 		}
1796a9ce96bcSJeeja KP 
1797a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1798a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1799a9ce96bcSJeeja KP 				continue;
1800a9ce96bcSJeeja KP 
1801a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1802a9ce96bcSJeeja KP 		}
1803a9ce96bcSJeeja KP 	}
1804a9ce96bcSJeeja KP }
1805a9ce96bcSJeeja KP 
180645101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
180718382eadSSubhransu S. Prusty {
18083787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18093787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
181018382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
181145101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1812b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1813a9ce96bcSJeeja KP 	int ret;
181418382eadSSubhransu S. Prusty 
18153787a398SRakesh Ughreja 	hdmi->component = component;
181618382eadSSubhransu S. Prusty 
1817b2047e99SVinod Koul 	/*
1818b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1819b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1820b2047e99SVinod Koul 	 */
182176f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1822500e06b9SVinod Koul 	if (!hlink) {
18233787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
1824500e06b9SVinod Koul 		return -EIO;
1825500e06b9SVinod Koul 	}
1826500e06b9SVinod Koul 
182776f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1828b2047e99SVinod Koul 
182979f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
183079f4e922SSubhransu S. Prusty 	if (ret < 0)
183179f4e922SSubhransu S. Prusty 		return ret;
183218382eadSSubhransu S. Prusty 
18333787a398SRakesh Ughreja 	aops.audio_ptr = hdev;
1834a57942bfSTakashi Iwai 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1835b8a54545SSubhransu S. Prusty 	if (ret < 0) {
18363787a398SRakesh Ughreja 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1837b8a54545SSubhransu S. Prusty 		return ret;
1838b8a54545SSubhransu S. Prusty 	}
1839b8a54545SSubhransu S. Prusty 
18403787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
184118382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
18423787a398SRakesh Ughreja 	hdmi->card = dapm->card->snd_card;
184318382eadSSubhransu S. Prusty 
1844e342ac08SSubhransu S. Prusty 	/*
184501c83276SLibin Yang 	 * Setup a device_link between card device and HDMI codec device.
184601c83276SLibin Yang 	 * The card device is the consumer and the HDMI codec device is
184701c83276SLibin Yang 	 * the supplier. With this setting, we can make sure that the audio
184801c83276SLibin Yang 	 * domain in display power will be always turned on before operating
184901c83276SLibin Yang 	 * on the HDMI audio codec registers.
185001c83276SLibin Yang 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
185101c83276SLibin Yang 	 * sure the device link is freed when the machine driver is removed.
185201c83276SLibin Yang 	 */
185301c83276SLibin Yang 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
185401c83276SLibin Yang 			DL_FLAG_AUTOREMOVE_CONSUMER);
185501c83276SLibin Yang 	/*
1856e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1857e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1858e342ac08SSubhransu S. Prusty 	 */
18593787a398SRakesh Ughreja 	pm_runtime_enable(&hdev->dev);
18603787a398SRakesh Ughreja 	pm_runtime_put(&hdev->dev);
18613787a398SRakesh Ughreja 	pm_runtime_suspend(&hdev->dev);
1862e342ac08SSubhransu S. Prusty 
1863e342ac08SSubhransu S. Prusty 	return 0;
1864e342ac08SSubhransu S. Prusty }
1865e342ac08SSubhransu S. Prusty 
186645101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1867e342ac08SSubhransu S. Prusty {
18683787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
18693787a398SRakesh Ughreja 	struct hdac_device *hdev = hdmi->hdev;
18700f6ff785SAmadeusz Sławiński 	int ret;
18710f6ff785SAmadeusz Sławiński 
18720f6ff785SAmadeusz Sławiński 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
18730f6ff785SAmadeusz Sławiński 	if (ret < 0)
18740f6ff785SAmadeusz Sławiński 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
18750f6ff785SAmadeusz Sławiński 				ret);
1876e342ac08SSubhransu S. Prusty 
18773787a398SRakesh Ughreja 	pm_runtime_disable(&hdev->dev);
187818382eadSSubhransu S. Prusty }
187918382eadSSubhransu S. Prusty 
1880687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP
1881687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev)
1882571d5078SJeeja KP {
18833787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
18843787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1885687ae9e2STakashi Iwai 	int ret;
18861b377ccdSSubhransu S. Prusty 
1887687ae9e2STakashi Iwai 	ret = pm_runtime_force_resume(dev);
1888687ae9e2STakashi Iwai 	if (ret < 0)
1889687ae9e2STakashi Iwai 		return ret;
1890571d5078SJeeja KP 	/*
1891571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1892571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1893a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1894a9ce96bcSJeeja KP 	 * already set pin caps.
1895687ae9e2STakashi Iwai 	 *
1896687ae9e2STakashi Iwai 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1897687ae9e2STakashi Iwai 	 * The pin check involves only with DRM audio component hooks, so it
1898687ae9e2STakashi Iwai 	 * works even if the HD-audio side is still dreaming peacefully.
1899571d5078SJeeja KP 	 */
19003787a398SRakesh Ughreja 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1901687ae9e2STakashi Iwai 	return 0;
1902571d5078SJeeja KP }
1903571d5078SJeeja KP #else
1904687ae9e2STakashi Iwai #define hdmi_codec_resume NULL
1905571d5078SJeeja KP #endif
1906571d5078SJeeja KP 
190745101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
190818382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1909e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
191045101122SKuninori Morimoto 	.use_pmdown_time	= 1,
191145101122SKuninori Morimoto 	.endianness		= 1,
191245101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
191318382eadSSubhransu S. Prusty };
191418382eadSSubhransu S. Prusty 
1915f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19162889099eSSubhransu S. Prusty 					unsigned char *chmap)
19172889099eSSubhransu S. Prusty {
1918f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19192889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19202889099eSSubhransu S. Prusty 
1921ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19222889099eSSubhransu S. Prusty }
19232889099eSSubhransu S. Prusty 
1924f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19252889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19262889099eSSubhransu S. Prusty {
1927f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19282889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1929e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1930e0e5d3e5SJeeja KP 
1931eb50fa17SSubhransu S. Prusty 	if (!pcm)
1932eb50fa17SSubhransu S. Prusty 		return;
1933eb50fa17SSubhransu S. Prusty 
1934e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1935e0e5d3e5SJeeja KP 		return;
19362889099eSSubhransu S. Prusty 
1937ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1938ab1eea19SJeeja KP 	pcm->chmap_set = true;
1939ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1940e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19412889099eSSubhransu S. Prusty 		if (prepared)
19423787a398SRakesh Ughreja 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1943ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19442889099eSSubhransu S. Prusty }
19452889099eSSubhransu S. Prusty 
1946f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19472889099eSSubhransu S. Prusty {
1948f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19492889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19502889099eSSubhransu S. Prusty 
1951eb50fa17SSubhransu S. Prusty 	if (!pcm)
1952eb50fa17SSubhransu S. Prusty 		return false;
1953eb50fa17SSubhransu S. Prusty 
1954e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1955e0e5d3e5SJeeja KP 		return false;
1956e0e5d3e5SJeeja KP 
1957e0e5d3e5SJeeja KP 	return true;
19582889099eSSubhransu S. Prusty }
19592889099eSSubhransu S. Prusty 
1960f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19612889099eSSubhransu S. Prusty {
1962f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19632889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1964e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1965e0e5d3e5SJeeja KP 
1966eb50fa17SSubhransu S. Prusty 	if (!pcm)
1967eb50fa17SSubhransu S. Prusty 		return 0;
1968eb50fa17SSubhransu S. Prusty 
1969e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1970e0e5d3e5SJeeja KP 		return 0;
1971e0e5d3e5SJeeja KP 
1972e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1973e0e5d3e5SJeeja KP 
1974754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19752889099eSSubhransu S. Prusty 		return 0;
19762889099eSSubhransu S. Prusty 
1977754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19782889099eSSubhransu S. Prusty }
19792889099eSSubhransu S. Prusty 
1980019033c8SBard liao static struct hdac_hdmi_drv_data intel_icl_drv_data  = {
1981019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x2,
1982019033c8SBard liao 	.port_map = icl_pin2port_map,
1983019033c8SBard liao 	.port_num = ARRAY_SIZE(icl_pin2port_map),
1984019033c8SBard liao };
1985019033c8SBard liao 
19865622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
1987019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0xb,
19885622bc95SPradeep Tewani };
19895622bc95SPradeep Tewani 
19905622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
1991019033c8SBard liao 	.vendor_nid = INTEL_VENDOR_NID_0x8,
19925622bc95SPradeep Tewani };
19935622bc95SPradeep Tewani 
19943787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
199518382eadSSubhransu S. Prusty {
19963787a398SRakesh Ughreja 	struct hdac_hdmi_priv *hdmi_priv = NULL;
199717a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1998b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
199917a42c45SSubhransu S. Prusty 	int num_dais = 0;
200018382eadSSubhransu S. Prusty 	int ret = 0;
2001f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2002f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
200318382eadSSubhransu S. Prusty 
2004b2047e99SVinod Koul 	/* hold the ref while we probe */
200576f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2006500e06b9SVinod Koul 	if (!hlink) {
20073787a398SRakesh Ughreja 		dev_err(&hdev->dev, "hdac link not found\n");
2008500e06b9SVinod Koul 		return -EIO;
2009500e06b9SVinod Koul 	}
2010500e06b9SVinod Koul 
201176f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2012b2047e99SVinod Koul 
2013f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
201418382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
201518382eadSSubhransu S. Prusty 		return -ENOMEM;
201618382eadSSubhransu S. Prusty 
2017f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20182889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20192889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20202889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20212889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
20223787a398SRakesh Ughreja 	hdmi_priv->hdev = hdev;
202318382eadSSubhransu S. Prusty 
2024eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2025eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2026eb50fa17SSubhransu S. Prusty 
20275622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20285622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20295622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20305622bc95SPradeep Tewani 	else
20315622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20325622bc95SPradeep Tewani 
20333787a398SRakesh Ughreja 	dev_set_drvdata(&hdev->dev, hdmi_priv);
203418382eadSSubhransu S. Prusty 
203515b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
203615b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20374a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20384a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
203915b91447SSubhransu S. Prusty 
2040aeaccef0SRamesh Babu 	/*
2041aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2042aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2043aeaccef0SRamesh Babu 	 */
20444f799e73STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
20454f799e73STakashi Iwai 
20463787a398SRakesh Ughreja 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
204717a42c45SSubhransu S. Prusty 	if (ret < 0) {
2048f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
204917a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
205018382eadSSubhransu S. Prusty 		return ret;
205117a42c45SSubhransu S. Prusty 	}
20520fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
205318382eadSSubhransu S. Prusty 
205418382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
205545101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
205617a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2057b2047e99SVinod Koul 
205876f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2059b2047e99SVinod Koul 
2060b2047e99SVinod Koul 	return ret;
206118382eadSSubhransu S. Prusty }
206218382eadSSubhransu S. Prusty 
20633787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
206418382eadSSubhransu S. Prusty {
206577a49672STakashi Iwai 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
206677a49672STakashi Iwai 
206718382eadSSubhransu S. Prusty 	return 0;
206818382eadSSubhransu S. Prusty }
206918382eadSSubhransu S. Prusty 
2070e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2071e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2072e342ac08SSubhransu S. Prusty {
20733787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2074f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2075b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2076e342ac08SSubhransu S. Prusty 
2077e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2078e342ac08SSubhransu S. Prusty 
207907f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
208007f083abSSubhransu S. Prusty 	if (!bus)
208107f083abSSubhransu S. Prusty 		return 0;
208207f083abSSubhransu S. Prusty 
20831b377ccdSSubhransu S. Prusty 	/*
20841b377ccdSSubhransu S. Prusty 	 * Power down afg.
20851b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
20861b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
20871b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
20881b377ccdSSubhransu S. Prusty 	 * to read the state.
20891b377ccdSSubhransu S. Prusty 	 */
2090f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
20911b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
209207f083abSSubhransu S. Prusty 
209376f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2094500e06b9SVinod Koul 	if (!hlink) {
2095500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2096500e06b9SVinod Koul 		return -EIO;
2097500e06b9SVinod Koul 	}
2098500e06b9SVinod Koul 
209976f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_put(bus, hlink);
2100b2047e99SVinod Koul 
21014f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, false);
21024c10473dSPierre-Louis Bossart 
21034f799e73STakashi Iwai 	return 0;
2104e342ac08SSubhransu S. Prusty }
2105e342ac08SSubhransu S. Prusty 
2106e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2107e342ac08SSubhransu S. Prusty {
21083787a398SRakesh Ughreja 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2109f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2110b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
2111e342ac08SSubhransu S. Prusty 
2112e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2113e342ac08SSubhransu S. Prusty 
211407f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
211507f083abSSubhransu S. Prusty 	if (!bus)
211607f083abSSubhransu S. Prusty 		return 0;
211707f083abSSubhransu S. Prusty 
211876f56faeSRakesh Ughreja 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2119500e06b9SVinod Koul 	if (!hlink) {
2120500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2121500e06b9SVinod Koul 		return -EIO;
2122500e06b9SVinod Koul 	}
2123500e06b9SVinod Koul 
212476f56faeSRakesh Ughreja 	snd_hdac_ext_bus_link_get(bus, hlink);
2125b2047e99SVinod Koul 
21264f799e73STakashi Iwai 	snd_hdac_display_power(bus, hdev->addr, true);
212707f083abSSubhransu S. Prusty 
21283787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_all_pins(hdev);
21293787a398SRakesh Ughreja 	hdac_hdmi_skl_enable_dp12(hdev);
2130ab85f5b3SSubhransu S. Prusty 
2131e342ac08SSubhransu S. Prusty 	/* Power up afg */
2132f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21331b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2134e342ac08SSubhransu S. Prusty 
2135e342ac08SSubhransu S. Prusty 	return 0;
2136e342ac08SSubhransu S. Prusty }
2137e342ac08SSubhransu S. Prusty #else
2138e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2139e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2140e342ac08SSubhransu S. Prusty #endif
2141e342ac08SSubhransu S. Prusty 
2142e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2143e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2144687ae9e2STakashi Iwai 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2145e342ac08SSubhransu S. Prusty };
2146e342ac08SSubhransu S. Prusty 
214718382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
214818382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2149e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2150cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21515fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21525fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21535622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21545622bc95SPradeep Tewani 						   &intel_glk_drv_data),
2155019033c8SBard liao 	HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2156019033c8SBard liao 						   &intel_icl_drv_data),
215718382eadSSubhransu S. Prusty 	{}
215818382eadSSubhransu S. Prusty };
215918382eadSSubhransu S. Prusty 
216018382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
216118382eadSSubhransu S. Prusty 
2162e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = {
216318382eadSSubhransu S. Prusty 	.driver = {
216418382eadSSubhransu S. Prusty 		.name   = "HDMI HDA Codec",
2165e342ac08SSubhransu S. Prusty 		.pm = &hdac_hdmi_pm,
216618382eadSSubhransu S. Prusty 	},
216718382eadSSubhransu S. Prusty 	.id_table       = hdmi_list,
216818382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
216918382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
217018382eadSSubhransu S. Prusty };
217118382eadSSubhransu S. Prusty 
217218382eadSSubhransu S. Prusty static int __init hdmi_init(void)
217318382eadSSubhransu S. Prusty {
217418382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
217518382eadSSubhransu S. Prusty }
217618382eadSSubhransu S. Prusty 
217718382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
217818382eadSSubhransu S. Prusty {
217918382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
218018382eadSSubhransu S. Prusty }
218118382eadSSubhransu S. Prusty 
218218382eadSSubhransu S. Prusty module_init(hdmi_init);
218318382eadSSubhransu S. Prusty module_exit(hdmi_exit);
218418382eadSSubhransu S. Prusty 
218518382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
218618382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
218718382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
218818382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2189