xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 45101122)
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;
88754695f9SJeeja KP 	struct hdac_ext_device *edev;
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 
1245622bc95SPradeep Tewani struct hdac_hdmi_drv_data {
1255622bc95SPradeep Tewani 	unsigned int vendor_nid;
1265622bc95SPradeep Tewani };
1275622bc95SPradeep Tewani 
12818382eadSSubhransu S. Prusty struct hdac_hdmi_priv {
129754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
13015b91447SSubhransu S. Prusty 	struct list_head pin_list;
13115b91447SSubhransu S. Prusty 	struct list_head cvt_list;
1324a3478deSJeeja KP 	struct list_head pcm_list;
13315b91447SSubhransu S. Prusty 	int num_pin;
13415b91447SSubhransu S. Prusty 	int num_cvt;
135754695f9SJeeja KP 	int num_ports;
1364a3478deSJeeja KP 	struct mutex pin_mutex;
137bcced704SSubhransu S. Prusty 	struct hdac_chmap chmap;
1385622bc95SPradeep Tewani 	struct hdac_hdmi_drv_data *drv_data;
1391e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv;
14018382eadSSubhransu S. Prusty };
14118382eadSSubhransu S. Prusty 
142b09b1c3bSUghreja, Rakesh A #define hdev_to_hdmi_priv(_hdev) ((to_ehdac_device(_hdev))->private_data)
143b09b1c3bSUghreja, Rakesh A 
144c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm *
145c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
146c9bfb5d7SJeeja KP 			   struct hdac_hdmi_cvt *cvt)
147c9bfb5d7SJeeja KP {
148c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
1491de777feSJeeja KP 
150c9bfb5d7SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
151c9bfb5d7SJeeja KP 		if (pcm->cvt == cvt)
152c9bfb5d7SJeeja KP 			break;
153c9bfb5d7SJeeja KP 	}
154c9bfb5d7SJeeja KP 
155c9bfb5d7SJeeja KP 	return pcm;
156c9bfb5d7SJeeja KP }
1571de777feSJeeja KP 
158e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
159e0e5d3e5SJeeja KP 		struct hdac_hdmi_port *port, bool is_connect)
160e0e5d3e5SJeeja KP {
161e0e5d3e5SJeeja KP 	struct hdac_ext_device *edev = port->pin->edev;
162e0e5d3e5SJeeja KP 
1630324e51bSJeeja KP 	if (is_connect)
1640324e51bSJeeja KP 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
1650324e51bSJeeja KP 	else
1660324e51bSJeeja KP 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
1670324e51bSJeeja KP 
168e0e5d3e5SJeeja KP 	if (is_connect) {
169e0e5d3e5SJeeja KP 		/*
170e0e5d3e5SJeeja KP 		 * Report Jack connect event when a device is connected
171e0e5d3e5SJeeja KP 		 * for the first time where same PCM is attached to multiple
172e0e5d3e5SJeeja KP 		 * ports.
173e0e5d3e5SJeeja KP 		 */
174e0e5d3e5SJeeja KP 		if (pcm->jack_event == 0) {
175f0c5ebebSUghreja, Rakesh A 			dev_dbg(&edev->hdev.dev,
176e0e5d3e5SJeeja KP 					"jack report for pcm=%d\n",
177e0e5d3e5SJeeja KP 					pcm->pcm_id);
17862490016SJeeja KP 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
17962490016SJeeja KP 						SND_JACK_AVOUT);
180e0e5d3e5SJeeja KP 		}
181e0e5d3e5SJeeja KP 		pcm->jack_event++;
182e0e5d3e5SJeeja KP 	} else {
183e0e5d3e5SJeeja KP 		/*
184e0e5d3e5SJeeja KP 		 * Report Jack disconnect event when a device is disconnected
185e0e5d3e5SJeeja KP 		 * is the only last connected device when same PCM is attached
186e0e5d3e5SJeeja KP 		 * to multiple ports.
187e0e5d3e5SJeeja KP 		 */
188e0e5d3e5SJeeja KP 		if (pcm->jack_event == 1)
18962490016SJeeja KP 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
190e0e5d3e5SJeeja KP 		if (pcm->jack_event > 0)
191e0e5d3e5SJeeja KP 			pcm->jack_event--;
192e0e5d3e5SJeeja KP 	}
1930324e51bSJeeja KP 
1940324e51bSJeeja KP 	snd_soc_dapm_sync(port->dapm);
195e0e5d3e5SJeeja KP }
196e0e5d3e5SJeeja KP 
197fc181b04SJeeja KP /* MST supported verbs */
198fc181b04SJeeja KP /*
199fc181b04SJeeja KP  * Get the no devices that can be connected to a port on the Pin widget.
200fc181b04SJeeja KP  */
20172bc39cfSUghreja, Rakesh A static int hdac_hdmi_get_port_len(struct hdac_ext_device *edev, hda_nid_t nid)
202fc181b04SJeeja KP {
203fc181b04SJeeja KP 	unsigned int caps;
204fc181b04SJeeja KP 	unsigned int type, param;
205fc181b04SJeeja KP 
206f0c5ebebSUghreja, Rakesh A 	caps = get_wcaps(&edev->hdev, nid);
207fc181b04SJeeja KP 	type = get_wcaps_type(caps);
208fc181b04SJeeja KP 
209fc181b04SJeeja KP 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
210fc181b04SJeeja KP 		return 0;
211fc181b04SJeeja KP 
212f0c5ebebSUghreja, Rakesh A 	param = snd_hdac_read_parm_uncached(&edev->hdev, nid,
213fc181b04SJeeja KP 					AC_PAR_DEVLIST_LEN);
214fc181b04SJeeja KP 	if (param == -1)
215fc181b04SJeeja KP 		return param;
216fc181b04SJeeja KP 
217fc181b04SJeeja KP 	return param & AC_DEV_LIST_LEN_MASK;
218fc181b04SJeeja KP }
219fc181b04SJeeja KP 
220fc181b04SJeeja KP /*
221fc181b04SJeeja KP  * Get the port entry select on the pin. Return the port entry
222fc181b04SJeeja KP  * id selected on the pin. Return 0 means the first port entry
223fc181b04SJeeja KP  * is selected or MST is not supported.
224fc181b04SJeeja KP  */
22572bc39cfSUghreja, Rakesh A static int hdac_hdmi_port_select_get(struct hdac_ext_device *edev,
226fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
227fc181b04SJeeja KP {
228f0c5ebebSUghreja, Rakesh A 	return snd_hdac_codec_read(&edev->hdev, port->pin->nid,
229fc181b04SJeeja KP 				0, AC_VERB_GET_DEVICE_SEL, 0);
230fc181b04SJeeja KP }
231fc181b04SJeeja KP 
232fc181b04SJeeja KP /*
233fc181b04SJeeja KP  * Sets the selected port entry for the configuring Pin widget verb.
234fc181b04SJeeja KP  * returns error if port set is not equal to port get otherwise success
235fc181b04SJeeja KP  */
23672bc39cfSUghreja, Rakesh A static int hdac_hdmi_port_select_set(struct hdac_ext_device *edev,
237fc181b04SJeeja KP 					struct hdac_hdmi_port *port)
238fc181b04SJeeja KP {
239fc181b04SJeeja KP 	int num_ports;
240fc181b04SJeeja KP 
241fc181b04SJeeja KP 	if (!port->pin->mst_capable)
242fc181b04SJeeja KP 		return 0;
243fc181b04SJeeja KP 
244fc181b04SJeeja KP 	/* AC_PAR_DEVLIST_LEN is 0 based. */
24572bc39cfSUghreja, Rakesh A 	num_ports = hdac_hdmi_get_port_len(edev, port->pin->nid);
246fc181b04SJeeja KP 
247fc181b04SJeeja KP 	if (num_ports < 0)
248fc181b04SJeeja KP 		return -EIO;
249fc181b04SJeeja KP 	/*
250fc181b04SJeeja KP 	 * Device List Length is a 0 based integer value indicating the
251fc181b04SJeeja KP 	 * number of sink device that a MST Pin Widget can support.
252fc181b04SJeeja KP 	 */
253fc181b04SJeeja KP 	if (num_ports + 1  < port->id)
254fc181b04SJeeja KP 		return 0;
255fc181b04SJeeja KP 
256f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_write(&edev->hdev, port->pin->nid, 0,
257fc181b04SJeeja KP 			AC_VERB_SET_DEVICE_SEL, port->id);
258fc181b04SJeeja KP 
25972bc39cfSUghreja, Rakesh A 	if (port->id != hdac_hdmi_port_select_get(edev, port))
260fc181b04SJeeja KP 		return -EIO;
261fc181b04SJeeja KP 
262f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "Selected the port=%d\n", port->id);
263fc181b04SJeeja KP 
264fc181b04SJeeja KP 	return 0;
265fc181b04SJeeja KP }
266fc181b04SJeeja KP 
2672889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
2682889099eSSubhransu S. Prusty 						int pcm_idx)
2692889099eSSubhransu S. Prusty {
2702889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
2712889099eSSubhransu S. Prusty 
2722889099eSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
2732889099eSSubhransu S. Prusty 		if (pcm->pcm_id == pcm_idx)
2742889099eSSubhransu S. Prusty 			return pcm;
2752889099eSSubhransu S. Prusty 	}
2762889099eSSubhransu S. Prusty 
2772889099eSSubhransu S. Prusty 	return NULL;
2782889099eSSubhransu S. Prusty }
2792889099eSSubhransu S. Prusty 
280e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
281e342ac08SSubhransu S. Prusty {
282f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
283e342ac08SSubhransu S. Prusty 
284f0c5ebebSUghreja, Rakesh A 	return to_ehdac_device(hdev);
285e342ac08SSubhransu S. Prusty }
286e342ac08SSubhransu S. Prusty 
2872428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad)
2882428bca3SSubhransu S. Prusty {
2892428bca3SSubhransu S. Prusty 	return ((sad[0] >> 0x3) & 0x1f);
2902428bca3SSubhransu S. Prusty }
2912428bca3SSubhransu S. Prusty 
2922428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad)
2932428bca3SSubhransu S. Prusty {
2942428bca3SSubhransu S. Prusty 	return (sad[2] & 7);
2952428bca3SSubhransu S. Prusty }
2962428bca3SSubhransu S. Prusty 
2972428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
2982428bca3SSubhransu S. Prusty 						void *eld)
2992428bca3SSubhransu S. Prusty {
3002428bca3SSubhransu S. Prusty 	u64 formats = SNDRV_PCM_FMTBIT_S16;
3012428bca3SSubhransu S. Prusty 	int i;
3022428bca3SSubhransu S. Prusty 	const u8 *sad, *eld_buf = eld;
3032428bca3SSubhransu S. Prusty 
3042428bca3SSubhransu S. Prusty 	sad = drm_eld_sad(eld_buf);
3052428bca3SSubhransu S. Prusty 	if (!sad)
3062428bca3SSubhransu S. Prusty 		goto format_constraint;
3072428bca3SSubhransu S. Prusty 
3082428bca3SSubhransu S. Prusty 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
3092428bca3SSubhransu S. Prusty 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
3102428bca3SSubhransu S. Prusty 
3112428bca3SSubhransu S. Prusty 			/*
3122428bca3SSubhransu S. Prusty 			 * the controller support 20 and 24 bits in 32 bit
3132428bca3SSubhransu S. Prusty 			 * container so we set S32
3142428bca3SSubhransu S. Prusty 			 */
3152428bca3SSubhransu S. Prusty 			if (sad_sample_bits_lpcm(sad) & 0x6)
3162428bca3SSubhransu S. Prusty 				formats |= SNDRV_PCM_FMTBIT_S32;
3172428bca3SSubhransu S. Prusty 		}
3182428bca3SSubhransu S. Prusty 	}
3192428bca3SSubhransu S. Prusty 
3202428bca3SSubhransu S. Prusty format_constraint:
3212428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
3222428bca3SSubhransu S. Prusty 				formats);
3232428bca3SSubhransu S. Prusty 
3242428bca3SSubhransu S. Prusty }
3252428bca3SSubhransu S. Prusty 
326a657f1d0SSubhransu S. Prusty static void
32772bc39cfSUghreja, Rakesh A hdac_hdmi_set_dip_index(struct hdac_ext_device *edev, hda_nid_t pin_nid,
328a657f1d0SSubhransu S. Prusty 				int packet_index, int byte_index)
329a657f1d0SSubhransu S. Prusty {
330a657f1d0SSubhransu S. Prusty 	int val;
331a657f1d0SSubhransu S. Prusty 
332a657f1d0SSubhransu S. Prusty 	val = (packet_index << 5) | (byte_index & 0x1f);
333a657f1d0SSubhransu S. Prusty 
334f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_write(&edev->hdev, pin_nid, 0,
335a657f1d0SSubhransu S. Prusty 				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 
35072bc39cfSUghreja, Rakesh A static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *edev,
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;
357f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->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 
366f0c5ebebSUghreja, Rakesh A 	ca = snd_hdac_channel_allocation(&edev->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);
370f0c5ebebSUghreja, Rakesh A 	hdmi->chmap.ops.set_channel_count(&edev->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:
403f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "Invalid connection type: %d\n",
404478f544eSSubhransu S. Prusty 						conn_type);
405478f544eSSubhransu S. Prusty 		return -EIO;
406478f544eSSubhransu S. Prusty 	}
407478f544eSSubhransu S. Prusty 
408a657f1d0SSubhransu S. Prusty 	/* stop infoframe transmission */
40972bc39cfSUghreja, Rakesh A 	hdac_hdmi_set_dip_index(edev, pin->nid, 0x0, 0x0);
410f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_write(&edev->hdev, pin->nid, 0,
411a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
412a657f1d0SSubhransu S. Prusty 
413a657f1d0SSubhransu S. Prusty 
414a657f1d0SSubhransu S. Prusty 	/*  Fill infoframe. Index auto-incremented */
41572bc39cfSUghreja, Rakesh A 	hdac_hdmi_set_dip_index(edev, pin->nid, 0x0, 0x0);
416478f544eSSubhransu S. Prusty 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
417391005e8SSubhransu S. Prusty 		for (i = 0; i < sizeof(buffer); i++)
418f0c5ebebSUghreja, Rakesh A 			snd_hdac_codec_write(&edev->hdev, pin->nid, 0,
419391005e8SSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
420478f544eSSubhransu S. Prusty 	} else {
421478f544eSSubhransu S. Prusty 		for (i = 0; i < sizeof(dp_ai); i++)
422f0c5ebebSUghreja, Rakesh A 			snd_hdac_codec_write(&edev->hdev, pin->nid, 0,
423478f544eSSubhransu S. Prusty 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
424478f544eSSubhransu S. Prusty 	}
425a657f1d0SSubhransu S. Prusty 
426a657f1d0SSubhransu S. Prusty 	/* Start infoframe */
42772bc39cfSUghreja, Rakesh A 	hdac_hdmi_set_dip_index(edev, pin->nid, 0x0, 0x0);
428f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_write(&edev->hdev, pin->nid, 0,
429a657f1d0SSubhransu S. Prusty 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
430a657f1d0SSubhransu S. Prusty 
431a657f1d0SSubhransu S. Prusty 	return 0;
432a657f1d0SSubhransu S. Prusty }
433a657f1d0SSubhransu S. Prusty 
434c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
435c9bfb5d7SJeeja KP 		unsigned int tx_mask, unsigned int rx_mask,
436c9bfb5d7SJeeja KP 		int slots, int slot_width)
437b0362adbSSubhransu S. Prusty {
438c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
439f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
440754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
441c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
442c9bfb5d7SJeeja KP 
443f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
444b0362adbSSubhransu S. Prusty 
445b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
446b0362adbSSubhransu S. Prusty 
447c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
448b0362adbSSubhransu S. Prusty 
449c9bfb5d7SJeeja KP 	if (pcm)
450c9bfb5d7SJeeja KP 		pcm->stream_tag = (tx_mask << 4);
451bcced704SSubhransu S. Prusty 
452c9bfb5d7SJeeja KP 	return 0;
453b0362adbSSubhransu S. Prusty }
454b0362adbSSubhransu S. Prusty 
455b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
456b0362adbSSubhransu S. Prusty 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
457b0362adbSSubhransu S. Prusty {
45872bc39cfSUghreja, Rakesh A 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
459f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
460754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
461754695f9SJeeja KP 	struct hdac_hdmi_port *port;
462c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
463c9bfb5d7SJeeja KP 	int format;
464b0362adbSSubhransu S. Prusty 
46554dfa1eaSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
466754695f9SJeeja KP 	port = dai_map->port;
46754dfa1eaSSubhransu S. Prusty 
468754695f9SJeeja KP 	if (!port)
46954dfa1eaSSubhransu S. Prusty 		return -ENODEV;
47054dfa1eaSSubhransu S. Prusty 
471754695f9SJeeja KP 	if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
472f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev,
473754695f9SJeeja KP 			"device is not configured for this pin:port%d:%d\n",
474754695f9SJeeja KP 					port->pin->nid, port->id);
475b0362adbSSubhransu S. Prusty 		return -ENODEV;
476b0362adbSSubhransu S. Prusty 	}
477b0362adbSSubhransu S. Prusty 
478c9bfb5d7SJeeja KP 	format = snd_hdac_calc_stream_format(params_rate(hparams),
479b0362adbSSubhransu S. Prusty 			params_channels(hparams), params_format(hparams),
48066d6bbc6SJeeja KP 			dai->driver->playback.sig_bits, 0);
481b0362adbSSubhransu S. Prusty 
482c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
483c9bfb5d7SJeeja KP 	if (!pcm)
484148569fdSSubhransu S. Prusty 		return -EIO;
485148569fdSSubhransu S. Prusty 
486c9bfb5d7SJeeja KP 	pcm->format = format;
487c9bfb5d7SJeeja KP 	pcm->channels = params_channels(hparams);
488148569fdSSubhransu S. Prusty 
489148569fdSSubhransu S. Prusty 	return 0;
490148569fdSSubhransu S. Prusty }
491148569fdSSubhransu S. Prusty 
49272bc39cfSUghreja, Rakesh A static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *edev,
493754695f9SJeeja KP 					struct hdac_hdmi_pin *pin,
494754695f9SJeeja KP 					struct hdac_hdmi_port *port)
495148569fdSSubhransu S. Prusty {
496f0c5ebebSUghreja, Rakesh A 	if (!(get_wcaps(&edev->hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
497f0c5ebebSUghreja, Rakesh A 		dev_warn(&edev->hdev.dev,
498148569fdSSubhransu S. Prusty 			"HDMI: pin %d wcaps %#x does not support connection list\n",
499f0c5ebebSUghreja, Rakesh A 			pin->nid, get_wcaps(&edev->hdev, pin->nid));
500148569fdSSubhransu S. Prusty 		return -EINVAL;
501148569fdSSubhransu S. Prusty 	}
502148569fdSSubhransu S. Prusty 
50372bc39cfSUghreja, Rakesh A 	if (hdac_hdmi_port_select_set(edev, port) < 0)
5041b46ebd1SJeeja KP 		return -EIO;
5051b46ebd1SJeeja KP 
506f0c5ebebSUghreja, Rakesh A 	port->num_mux_nids = snd_hdac_get_connections(&edev->hdev, pin->nid,
507754695f9SJeeja KP 			port->mux_nids, HDA_MAX_CONNECTIONS);
508754695f9SJeeja KP 	if (port->num_mux_nids == 0)
509f0c5ebebSUghreja, Rakesh A 		dev_warn(&edev->hdev.dev,
510754695f9SJeeja KP 			"No connections found for pin:port %d:%d\n",
511754695f9SJeeja KP 						pin->nid, port->id);
512148569fdSSubhransu S. Prusty 
513f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "num_mux_nids %d for pin:port %d:%d\n",
514754695f9SJeeja KP 			port->num_mux_nids, pin->nid, port->id);
515148569fdSSubhransu S. Prusty 
516754695f9SJeeja KP 	return port->num_mux_nids;
517148569fdSSubhransu S. Prusty }
518148569fdSSubhransu S. Prusty 
519148569fdSSubhransu S. Prusty /*
520754695f9SJeeja KP  * Query pcm list and return port to which stream is routed.
521148569fdSSubhransu S. Prusty  *
522754695f9SJeeja KP  * Also query connection list of the pin, to validate the cvt to port map.
523148569fdSSubhransu S. Prusty  *
524754695f9SJeeja KP  * Same stream rendering to multiple ports simultaneously can be done
525754695f9SJeeja KP  * possibly, but not supported for now in driver. So return the first port
526148569fdSSubhransu S. Prusty  * connected.
527148569fdSSubhransu S. Prusty  */
528754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
529148569fdSSubhransu S. Prusty 			struct hdac_ext_device *edev,
530148569fdSSubhransu S. Prusty 			struct hdac_hdmi_priv *hdmi,
531148569fdSSubhransu S. Prusty 			struct hdac_hdmi_cvt *cvt)
532148569fdSSubhransu S. Prusty {
533148569fdSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm;
534754695f9SJeeja KP 	struct hdac_hdmi_port *port = NULL;
535148569fdSSubhransu S. Prusty 	int ret, i;
536148569fdSSubhransu S. Prusty 
537148569fdSSubhransu S. Prusty 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
538148569fdSSubhransu S. Prusty 		if (pcm->cvt == cvt) {
539e0e5d3e5SJeeja KP 			if (list_empty(&pcm->port_list))
540e0e5d3e5SJeeja KP 				continue;
541148569fdSSubhransu S. Prusty 
542e0e5d3e5SJeeja KP 			list_for_each_entry(port, &pcm->port_list, head) {
543e0e5d3e5SJeeja KP 				mutex_lock(&pcm->lock);
544e0e5d3e5SJeeja KP 				ret = hdac_hdmi_query_port_connlist(edev,
545e0e5d3e5SJeeja KP 							port->pin, port);
546e0e5d3e5SJeeja KP 				mutex_unlock(&pcm->lock);
547148569fdSSubhransu S. Prusty 				if (ret < 0)
548e0e5d3e5SJeeja KP 					continue;
549148569fdSSubhransu S. Prusty 
550754695f9SJeeja KP 				for (i = 0; i < port->num_mux_nids; i++) {
551e0e5d3e5SJeeja KP 					if (port->mux_nids[i] == cvt->nid &&
552e0e5d3e5SJeeja KP 						port->eld.monitor_present &&
553e0e5d3e5SJeeja KP 						port->eld.eld_valid)
554754695f9SJeeja KP 						return port;
555148569fdSSubhransu S. Prusty 				}
556148569fdSSubhransu S. Prusty 			}
557e0e5d3e5SJeeja KP 		}
558e0e5d3e5SJeeja KP 	}
559148569fdSSubhransu S. Prusty 
560148569fdSSubhransu S. Prusty 	return NULL;
561148569fdSSubhransu S. Prusty }
562148569fdSSubhransu S. Prusty 
56354dfa1eaSSubhransu S. Prusty /*
56454dfa1eaSSubhransu S. Prusty  * This tries to get a valid pin and set the HW constraints based on the
56554dfa1eaSSubhransu S. Prusty  * ELD. Even if a valid pin is not found return success so that device open
56654dfa1eaSSubhransu S. Prusty  * doesn't fail.
56754dfa1eaSSubhransu S. Prusty  */
568b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
569b0362adbSSubhransu S. Prusty 			struct snd_soc_dai *dai)
570b0362adbSSubhransu S. Prusty {
57172bc39cfSUghreja, Rakesh A 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
572f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
573754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
574148569fdSSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
575754695f9SJeeja KP 	struct hdac_hdmi_port *port;
5762428bca3SSubhransu S. Prusty 	int ret;
577b0362adbSSubhransu S. Prusty 
578b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
579b0362adbSSubhransu S. Prusty 
580148569fdSSubhransu S. Prusty 	cvt = dai_map->cvt;
58172bc39cfSUghreja, Rakesh A 	port = hdac_hdmi_get_port_from_cvt(edev, hdmi, cvt);
58254dfa1eaSSubhransu S. Prusty 
58354dfa1eaSSubhransu S. Prusty 	/*
58454dfa1eaSSubhransu S. Prusty 	 * To make PA and other userland happy.
58554dfa1eaSSubhransu S. Prusty 	 * userland scans devices so returning error does not help.
58654dfa1eaSSubhransu S. Prusty 	 */
587754695f9SJeeja KP 	if (!port)
58854dfa1eaSSubhransu S. Prusty 		return 0;
589754695f9SJeeja KP 	if ((!port->eld.monitor_present) ||
590754695f9SJeeja KP 			(!port->eld.eld_valid)) {
591b0362adbSSubhransu S. Prusty 
592f0c5ebebSUghreja, Rakesh A 		dev_warn(&edev->hdev.dev,
593754695f9SJeeja KP 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
594754695f9SJeeja KP 			port->eld.monitor_present, port->eld.eld_valid,
595754695f9SJeeja KP 			port->pin->nid, port->id);
596b8a54545SSubhransu S. Prusty 
59754dfa1eaSSubhransu S. Prusty 		return 0;
598b0362adbSSubhransu S. Prusty 	}
599b0362adbSSubhransu S. Prusty 
600754695f9SJeeja KP 	dai_map->port = port;
601b0362adbSSubhransu S. Prusty 
6022428bca3SSubhransu S. Prusty 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
603754695f9SJeeja KP 				port->eld.eld_buffer);
6042428bca3SSubhransu S. Prusty 	if (ret < 0)
6052428bca3SSubhransu S. Prusty 		return ret;
606b0362adbSSubhransu S. Prusty 
6072428bca3SSubhransu S. Prusty 	return snd_pcm_hw_constraint_eld(substream->runtime,
608754695f9SJeeja KP 				port->eld.eld_buffer);
609b0362adbSSubhransu S. Prusty }
610b0362adbSSubhransu S. Prusty 
611b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
612b0362adbSSubhransu S. Prusty 		struct snd_soc_dai *dai)
613b0362adbSSubhransu S. Prusty {
61472bc39cfSUghreja, Rakesh A 	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
615f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
616754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
617ab1eea19SJeeja KP 	struct hdac_hdmi_pcm *pcm;
618b0362adbSSubhransu S. Prusty 
619b0362adbSSubhransu S. Prusty 	dai_map = &hdmi->dai_map[dai->id];
620b0362adbSSubhransu S. Prusty 
621ab1eea19SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
622bcced704SSubhransu S. Prusty 
623ab1eea19SJeeja KP 	if (pcm) {
624ab1eea19SJeeja KP 		mutex_lock(&pcm->lock);
625ab1eea19SJeeja KP 		pcm->chmap_set = false;
626ab1eea19SJeeja KP 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
627ab1eea19SJeeja KP 		pcm->channels = 0;
628ab1eea19SJeeja KP 		mutex_unlock(&pcm->lock);
629b0362adbSSubhransu S. Prusty 	}
630ab1eea19SJeeja KP 
631754695f9SJeeja KP 	if (dai_map->port)
632754695f9SJeeja KP 		dai_map->port = NULL;
63354dfa1eaSSubhransu S. Prusty }
634b0362adbSSubhransu S. Prusty 
63518382eadSSubhransu S. Prusty static int
636f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
63718382eadSSubhransu S. Prusty {
638bcced704SSubhransu S. Prusty 	unsigned int chans;
639f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
64018382eadSSubhransu S. Prusty 	int err;
64118382eadSSubhransu S. Prusty 
642f0c5ebebSUghreja, Rakesh A 	chans = get_wcaps(hdev, cvt->nid);
643bcced704SSubhransu S. Prusty 	chans = get_wcaps_channels(chans);
644bcced704SSubhransu S. Prusty 
645bcced704SSubhransu S. Prusty 	cvt->params.channels_min = 2;
646bcced704SSubhransu S. Prusty 
647bcced704SSubhransu S. Prusty 	cvt->params.channels_max = chans;
648bcced704SSubhransu S. Prusty 	if (chans > hdmi->chmap.channels_max)
649bcced704SSubhransu S. Prusty 		hdmi->chmap.channels_max = chans;
65018382eadSSubhransu S. Prusty 
651f0c5ebebSUghreja, Rakesh A 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
65218382eadSSubhransu S. Prusty 			&cvt->params.rates,
65318382eadSSubhransu S. Prusty 			&cvt->params.formats,
65418382eadSSubhransu S. Prusty 			&cvt->params.maxbps);
65518382eadSSubhransu S. Prusty 	if (err < 0)
656f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
65718382eadSSubhransu S. Prusty 			"Failed to query pcm params for nid %d: %d\n",
65818382eadSSubhransu S. Prusty 			cvt->nid, err);
65918382eadSSubhransu S. Prusty 
66018382eadSSubhransu S. Prusty 	return err;
66118382eadSSubhransu S. Prusty }
66218382eadSSubhransu S. Prusty 
66379f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev,
664c9bfb5d7SJeeja KP 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
665c9bfb5d7SJeeja KP 		void *priv, const char *wname, const char *stream,
666c9bfb5d7SJeeja KP 		struct snd_kcontrol_new *wc, int numkc,
667c9bfb5d7SJeeja KP 		int (*event)(struct snd_soc_dapm_widget *,
668c9bfb5d7SJeeja KP 		struct snd_kcontrol *, int), unsigned short event_flags)
66918382eadSSubhransu S. Prusty {
67018382eadSSubhransu S. Prusty 	w->id = id;
67179f4e922SSubhransu S. Prusty 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
67279f4e922SSubhransu S. Prusty 	if (!w->name)
67379f4e922SSubhransu S. Prusty 		return -ENOMEM;
67479f4e922SSubhransu S. Prusty 
67518382eadSSubhransu S. Prusty 	w->sname = stream;
67618382eadSSubhransu S. Prusty 	w->reg = SND_SOC_NOPM;
67718382eadSSubhransu S. Prusty 	w->shift = 0;
67879f4e922SSubhransu S. Prusty 	w->kcontrol_news = wc;
67979f4e922SSubhransu S. Prusty 	w->num_kcontrols = numkc;
68079f4e922SSubhransu S. Prusty 	w->priv = priv;
681c9bfb5d7SJeeja KP 	w->event = event;
682c9bfb5d7SJeeja KP 	w->event_flags = event_flags;
68379f4e922SSubhransu S. Prusty 
68479f4e922SSubhransu S. Prusty 	return 0;
68518382eadSSubhransu S. Prusty }
68618382eadSSubhransu S. Prusty 
68718382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
68879f4e922SSubhransu S. Prusty 		const char *sink, const char *control, const char *src,
68979f4e922SSubhransu S. Prusty 		int (*handler)(struct snd_soc_dapm_widget *src,
69079f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *sink))
69118382eadSSubhransu S. Prusty {
69218382eadSSubhransu S. Prusty 	route->sink = sink;
69318382eadSSubhransu S. Prusty 	route->source = src;
69418382eadSSubhransu S. Prusty 	route->control = control;
69579f4e922SSubhransu S. Prusty 	route->connected = handler;
69618382eadSSubhransu S. Prusty }
69718382eadSSubhransu S. Prusty 
6984a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
699754695f9SJeeja KP 					struct hdac_hdmi_port *port)
7004a3478deSJeeja KP {
701f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
7024a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
703e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p;
7044a3478deSJeeja KP 
7054a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
706e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
707754695f9SJeeja KP 			continue;
708754695f9SJeeja KP 
709e0e5d3e5SJeeja KP 		list_for_each_entry(p, &pcm->port_list, head) {
710e0e5d3e5SJeeja KP 			if (p->id == port->id && port->pin == p->pin)
7114a3478deSJeeja KP 				return pcm;
7124a3478deSJeeja KP 		}
713e0e5d3e5SJeeja KP 	}
7144a3478deSJeeja KP 
7154a3478deSJeeja KP 	return NULL;
7164a3478deSJeeja KP }
7174a3478deSJeeja KP 
718c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
719c9bfb5d7SJeeja KP 			     hda_nid_t nid, unsigned int pwr_state)
720c9bfb5d7SJeeja KP {
721f0c5ebebSUghreja, Rakesh A 	if (get_wcaps(&edev->hdev, nid) & AC_WCAP_POWER) {
722f0c5ebebSUghreja, Rakesh A 		if (!snd_hdac_check_power_state(&edev->hdev, nid, pwr_state))
723f0c5ebebSUghreja, Rakesh A 			snd_hdac_codec_write(&edev->hdev, nid, 0,
724c9bfb5d7SJeeja KP 				AC_VERB_SET_POWER_STATE, pwr_state);
725c9bfb5d7SJeeja KP 	}
726c9bfb5d7SJeeja KP }
727c9bfb5d7SJeeja KP 
728c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
729c9bfb5d7SJeeja KP 				   hda_nid_t nid, int val)
730c9bfb5d7SJeeja KP {
731f0c5ebebSUghreja, Rakesh A 	if (get_wcaps(&edev->hdev, nid) & AC_WCAP_OUT_AMP)
732f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, nid, 0,
733c9bfb5d7SJeeja KP 					AC_VERB_SET_AMP_GAIN_MUTE, val);
734c9bfb5d7SJeeja KP }
735c9bfb5d7SJeeja KP 
736c9bfb5d7SJeeja KP 
737c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
738c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
739c9bfb5d7SJeeja KP {
740754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
741c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
742c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
743c9bfb5d7SJeeja KP 
744f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "%s: widget: %s event: %x\n",
745c9bfb5d7SJeeja KP 			__func__, w->name, event);
746c9bfb5d7SJeeja KP 
747754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
748c9bfb5d7SJeeja KP 	if (!pcm)
749c9bfb5d7SJeeja KP 		return -EIO;
750c9bfb5d7SJeeja KP 
7511b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
7521b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
7531b46ebd1SJeeja KP 		return -EIO;
7541b46ebd1SJeeja KP 
755c9bfb5d7SJeeja KP 	switch (event) {
756c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
757754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
758c9bfb5d7SJeeja KP 
759c9bfb5d7SJeeja KP 		/* Enable out path for this pin widget */
760f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, port->pin->nid, 0,
761c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
762c9bfb5d7SJeeja KP 
763754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
764c9bfb5d7SJeeja KP 
765754695f9SJeeja KP 		return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
766c9bfb5d7SJeeja KP 
767c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
768754695f9SJeeja KP 		hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
769c9bfb5d7SJeeja KP 
770c9bfb5d7SJeeja KP 		/* Disable out path for this pin widget */
771f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, port->pin->nid, 0,
772c9bfb5d7SJeeja KP 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
773c9bfb5d7SJeeja KP 
774754695f9SJeeja KP 		hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
775c9bfb5d7SJeeja KP 		break;
776c9bfb5d7SJeeja KP 
777c9bfb5d7SJeeja KP 	}
778c9bfb5d7SJeeja KP 
779c9bfb5d7SJeeja KP 	return 0;
780c9bfb5d7SJeeja KP }
781c9bfb5d7SJeeja KP 
782c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
783c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
784c9bfb5d7SJeeja KP {
785c9bfb5d7SJeeja KP 	struct hdac_hdmi_cvt *cvt = w->priv;
786c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
787f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
788c9bfb5d7SJeeja KP 	struct hdac_hdmi_pcm *pcm;
789c9bfb5d7SJeeja KP 
790f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "%s: widget: %s event: %x\n",
791c9bfb5d7SJeeja KP 			__func__, w->name, event);
792c9bfb5d7SJeeja KP 
793c9bfb5d7SJeeja KP 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
794c9bfb5d7SJeeja KP 	if (!pcm)
795c9bfb5d7SJeeja KP 		return -EIO;
796c9bfb5d7SJeeja KP 
797c9bfb5d7SJeeja KP 	switch (event) {
798c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_PRE_PMU:
799c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
800c9bfb5d7SJeeja KP 
801c9bfb5d7SJeeja KP 		/* Enable transmission */
802f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
803c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_1, 1);
804c9bfb5d7SJeeja KP 
805c9bfb5d7SJeeja KP 		/* Category Code (CC) to zero */
806f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
807c9bfb5d7SJeeja KP 			AC_VERB_SET_DIGI_CONVERT_2, 0);
808c9bfb5d7SJeeja KP 
809f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
810c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
811f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
812c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
813c9bfb5d7SJeeja KP 		break;
814c9bfb5d7SJeeja KP 
815c9bfb5d7SJeeja KP 	case SND_SOC_DAPM_POST_PMD:
816f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
817c9bfb5d7SJeeja KP 				AC_VERB_SET_CHANNEL_STREAMID, 0);
818f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, cvt->nid, 0,
819c9bfb5d7SJeeja KP 				AC_VERB_SET_STREAM_FORMAT, 0);
820c9bfb5d7SJeeja KP 
821c9bfb5d7SJeeja KP 		hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
822c9bfb5d7SJeeja KP 		break;
823c9bfb5d7SJeeja KP 
824c9bfb5d7SJeeja KP 	}
825c9bfb5d7SJeeja KP 
826c9bfb5d7SJeeja KP 	return 0;
827c9bfb5d7SJeeja KP }
828c9bfb5d7SJeeja KP 
829c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
830c9bfb5d7SJeeja KP 					struct snd_kcontrol *kc, int event)
831c9bfb5d7SJeeja KP {
832754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
833c9bfb5d7SJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
834c9bfb5d7SJeeja KP 	int mux_idx;
835c9bfb5d7SJeeja KP 
836f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "%s: widget: %s event: %x\n",
837c9bfb5d7SJeeja KP 			__func__, w->name, event);
838c9bfb5d7SJeeja KP 
839c9bfb5d7SJeeja KP 	if (!kc)
840c9bfb5d7SJeeja KP 		kc  = w->kcontrols[0];
841c9bfb5d7SJeeja KP 
842c9bfb5d7SJeeja KP 	mux_idx = dapm_kcontrol_get_value(kc);
8431b46ebd1SJeeja KP 
8441b46ebd1SJeeja KP 	/* set the device if pin is mst_capable */
8451b46ebd1SJeeja KP 	if (hdac_hdmi_port_select_set(edev, port) < 0)
8461b46ebd1SJeeja KP 		return -EIO;
8471b46ebd1SJeeja KP 
848c9bfb5d7SJeeja KP 	if (mux_idx > 0) {
849f0c5ebebSUghreja, Rakesh A 		snd_hdac_codec_write(&edev->hdev, port->pin->nid, 0,
850c9bfb5d7SJeeja KP 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
851c9bfb5d7SJeeja KP 	}
852c9bfb5d7SJeeja KP 
853c9bfb5d7SJeeja KP 	return 0;
854c9bfb5d7SJeeja KP }
855c9bfb5d7SJeeja KP 
8564a3478deSJeeja KP /*
8574a3478deSJeeja KP  * Based on user selection, map the PINs with the PCMs.
8584a3478deSJeeja KP  */
859754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
8604a3478deSJeeja KP 		struct snd_ctl_elem_value *ucontrol)
8614a3478deSJeeja KP {
8624a3478deSJeeja KP 	int ret;
863e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *p, *p_next;
8644a3478deSJeeja KP 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
8654a3478deSJeeja KP 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
8664a3478deSJeeja KP 	struct snd_soc_dapm_context *dapm = w->dapm;
867754695f9SJeeja KP 	struct hdac_hdmi_port *port = w->priv;
8684a3478deSJeeja KP 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
869f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
8704a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm = NULL;
8714a3478deSJeeja KP 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
8724a3478deSJeeja KP 
8734a3478deSJeeja KP 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
8744a3478deSJeeja KP 	if (ret < 0)
8754a3478deSJeeja KP 		return ret;
8764a3478deSJeeja KP 
877754695f9SJeeja KP 	if (port == NULL)
878754695f9SJeeja KP 		return -EINVAL;
879754695f9SJeeja KP 
8804a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
8814a3478deSJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
882e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
883e0e5d3e5SJeeja KP 			continue;
884e0e5d3e5SJeeja KP 
885e0e5d3e5SJeeja KP 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
886e0e5d3e5SJeeja KP 			if (p == port && p->id == port->id &&
887e0e5d3e5SJeeja KP 					p->pin == port->pin) {
888e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, false);
889e0e5d3e5SJeeja KP 				list_del(&p->head);
890e0e5d3e5SJeeja KP 			}
891e0e5d3e5SJeeja KP 		}
892e0e5d3e5SJeeja KP 	}
8934a3478deSJeeja KP 
8944a3478deSJeeja KP 	/*
8954a3478deSJeeja KP 	 * Jack status is not reported during device probe as the
8964a3478deSJeeja KP 	 * PCMs are not registered by then. So report it here.
8974a3478deSJeeja KP 	 */
898e0e5d3e5SJeeja KP 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
899e0e5d3e5SJeeja KP 		if (!strcmp(cvt_name, pcm->cvt->name)) {
900e0e5d3e5SJeeja KP 			list_add_tail(&port->head, &pcm->port_list);
901754695f9SJeeja KP 			if (port->eld.monitor_present && port->eld.eld_valid) {
902e0e5d3e5SJeeja KP 				hdac_hdmi_jack_report(pcm, port, true);
9034a3478deSJeeja KP 				mutex_unlock(&hdmi->pin_mutex);
9044a3478deSJeeja KP 				return ret;
9054a3478deSJeeja KP 			}
9064a3478deSJeeja KP 		}
907e0e5d3e5SJeeja KP 	}
9084a3478deSJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
9094a3478deSJeeja KP 
9104a3478deSJeeja KP 	return ret;
9114a3478deSJeeja KP }
9124a3478deSJeeja KP 
91379f4e922SSubhransu S. Prusty /*
91479f4e922SSubhransu S. Prusty  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
91579f4e922SSubhransu S. Prusty  * the display driver seem to be programming the connection list for the pin
91679f4e922SSubhransu S. Prusty  * widget runtime.
91779f4e922SSubhransu S. Prusty  *
91879f4e922SSubhransu S. Prusty  * So programming all the possible inputs for the mux, the user has to take
91979f4e922SSubhransu S. Prusty  * care of selecting the right one and leaving all other inputs selected to
92079f4e922SSubhransu S. Prusty  * "NONE"
92179f4e922SSubhransu S. Prusty  */
922754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
923754695f9SJeeja KP 				struct hdac_hdmi_port *port,
92479f4e922SSubhransu S. Prusty 				struct snd_soc_dapm_widget *widget,
92579f4e922SSubhransu S. Prusty 				const char *widget_name)
92618382eadSSubhransu S. Prusty {
927f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
928754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = port->pin;
92979f4e922SSubhransu S. Prusty 	struct snd_kcontrol_new *kc;
93079f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
93179f4e922SSubhransu S. Prusty 	struct soc_enum *se;
93279f4e922SSubhransu S. Prusty 	char kc_name[NAME_SIZE];
93379f4e922SSubhransu S. Prusty 	char mux_items[NAME_SIZE];
93479f4e922SSubhransu S. Prusty 	/* To hold inputs to the Pin mux */
93579f4e922SSubhransu S. Prusty 	char *items[HDA_MAX_CONNECTIONS];
93679f4e922SSubhransu S. Prusty 	int i = 0;
93779f4e922SSubhransu S. Prusty 	int num_items = hdmi->num_cvt + 1;
93818382eadSSubhransu S. Prusty 
939f0c5ebebSUghreja, Rakesh A 	kc = devm_kzalloc(&edev->hdev.dev, sizeof(*kc), GFP_KERNEL);
94079f4e922SSubhransu S. Prusty 	if (!kc)
94179f4e922SSubhransu S. Prusty 		return -ENOMEM;
94218382eadSSubhransu S. Prusty 
943f0c5ebebSUghreja, Rakesh A 	se = devm_kzalloc(&edev->hdev.dev, sizeof(*se), GFP_KERNEL);
94479f4e922SSubhransu S. Prusty 	if (!se)
94579f4e922SSubhransu S. Prusty 		return -ENOMEM;
94618382eadSSubhransu S. Prusty 
94770e97a2dSSubhransu S. Prusty 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
94870e97a2dSSubhransu S. Prusty 						pin->nid, port->id);
949f0c5ebebSUghreja, Rakesh A 	kc->name = devm_kstrdup(&edev->hdev.dev, kc_name, GFP_KERNEL);
95079f4e922SSubhransu S. Prusty 	if (!kc->name)
95179f4e922SSubhransu S. Prusty 		return -ENOMEM;
95218382eadSSubhransu S. Prusty 
95379f4e922SSubhransu S. Prusty 	kc->private_value = (long)se;
95479f4e922SSubhransu S. Prusty 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
95579f4e922SSubhransu S. Prusty 	kc->access = 0;
95679f4e922SSubhransu S. Prusty 	kc->info = snd_soc_info_enum_double;
957754695f9SJeeja KP 	kc->put = hdac_hdmi_set_pin_port_mux;
95879f4e922SSubhransu S. Prusty 	kc->get = snd_soc_dapm_get_enum_double;
95979f4e922SSubhransu S. Prusty 
96079f4e922SSubhransu S. Prusty 	se->reg = SND_SOC_NOPM;
96179f4e922SSubhransu S. Prusty 
96279f4e922SSubhransu S. Prusty 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
96379f4e922SSubhransu S. Prusty 	se->items = num_items;
96479f4e922SSubhransu S. Prusty 	se->mask = roundup_pow_of_two(se->items) - 1;
96579f4e922SSubhransu S. Prusty 
96679f4e922SSubhransu S. Prusty 	sprintf(mux_items, "NONE");
967f0c5ebebSUghreja, Rakesh A 	items[i] = devm_kstrdup(&edev->hdev.dev, mux_items, GFP_KERNEL);
96879f4e922SSubhransu S. Prusty 	if (!items[i])
96979f4e922SSubhransu S. Prusty 		return -ENOMEM;
97079f4e922SSubhransu S. Prusty 
97179f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
97279f4e922SSubhransu S. Prusty 		i++;
97379f4e922SSubhransu S. Prusty 		sprintf(mux_items, "cvt %d", cvt->nid);
974f0c5ebebSUghreja, Rakesh A 		items[i] = devm_kstrdup(&edev->hdev.dev, mux_items, GFP_KERNEL);
97579f4e922SSubhransu S. Prusty 		if (!items[i])
97679f4e922SSubhransu S. Prusty 			return -ENOMEM;
97779f4e922SSubhransu S. Prusty 	}
97879f4e922SSubhransu S. Prusty 
979f0c5ebebSUghreja, Rakesh A 	se->texts = devm_kmemdup(&edev->hdev.dev, items,
98079f4e922SSubhransu S. Prusty 			(num_items  * sizeof(char *)), GFP_KERNEL);
98179f4e922SSubhransu S. Prusty 	if (!se->texts)
98279f4e922SSubhransu S. Prusty 		return -ENOMEM;
98379f4e922SSubhransu S. Prusty 
984f0c5ebebSUghreja, Rakesh A 	return hdac_hdmi_fill_widget_info(&edev->hdev.dev, widget,
985754695f9SJeeja KP 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
986c9bfb5d7SJeeja KP 			hdac_hdmi_pin_mux_widget_event,
987c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
98879f4e922SSubhransu S. Prusty }
98979f4e922SSubhransu S. Prusty 
99079f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */
99179f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
99279f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_widget *widgets,
99379f4e922SSubhransu S. Prusty 			struct snd_soc_dapm_route *route, int rindex)
99479f4e922SSubhransu S. Prusty {
995f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
99679f4e922SSubhransu S. Prusty 	const struct snd_kcontrol_new *kc;
99779f4e922SSubhransu S. Prusty 	struct soc_enum *se;
998754695f9SJeeja KP 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
99979f4e922SSubhransu S. Prusty 	int i, j;
100079f4e922SSubhransu S. Prusty 
1001754695f9SJeeja KP 	for (i = 0; i < hdmi->num_ports; i++) {
100279f4e922SSubhransu S. Prusty 		kc = widgets[mux_index].kcontrol_news;
100379f4e922SSubhransu S. Prusty 		se = (struct soc_enum *)kc->private_value;
100479f4e922SSubhransu S. Prusty 		for (j = 0; j < hdmi->num_cvt; j++) {
100579f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[rindex],
100679f4e922SSubhransu S. Prusty 					widgets[mux_index].name,
100779f4e922SSubhransu S. Prusty 					se->texts[j + 1],
100879f4e922SSubhransu S. Prusty 					widgets[j].name, NULL);
100979f4e922SSubhransu S. Prusty 
101079f4e922SSubhransu S. Prusty 			rindex++;
101179f4e922SSubhransu S. Prusty 		}
101279f4e922SSubhransu S. Prusty 
101379f4e922SSubhransu S. Prusty 		mux_index++;
101479f4e922SSubhransu S. Prusty 	}
101579f4e922SSubhransu S. Prusty }
101679f4e922SSubhransu S. Prusty 
101779f4e922SSubhransu S. Prusty /*
101879f4e922SSubhransu S. Prusty  * Widgets are added in the below sequence
101979f4e922SSubhransu S. Prusty  *	Converter widgets for num converters enumerated
1020754695f9SJeeja KP  *	Pin-port widgets for num ports for Pins enumerated
1021754695f9SJeeja KP  *	Pin-port mux widgets to represent connenction list of pin widget
102279f4e922SSubhransu S. Prusty  *
1023754695f9SJeeja KP  * For each port, one Mux and One output widget is added
1024754695f9SJeeja KP  * Total widgets elements = num_cvt + (num_ports * 2);
102579f4e922SSubhransu S. Prusty  *
102679f4e922SSubhransu S. Prusty  * Routes are added as below:
1027754695f9SJeeja KP  *	pin-port mux -> pin (based on num_ports)
1028754695f9SJeeja KP  *	cvt -> "Input sel control" -> pin-port_mux
102979f4e922SSubhransu S. Prusty  *
103079f4e922SSubhransu S. Prusty  * Total route elements:
1031754695f9SJeeja KP  *	num_ports + (pin_muxes * num_cvt)
103279f4e922SSubhransu S. Prusty  */
103379f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
103479f4e922SSubhransu S. Prusty {
103579f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_widget *widgets;
103679f4e922SSubhransu S. Prusty 	struct snd_soc_dapm_route *route;
103779f4e922SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
1038f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
10391e02dac3SKuninori Morimoto 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
104079f4e922SSubhransu S. Prusty 	char widget_name[NAME_SIZE];
104179f4e922SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
104279f4e922SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1043754695f9SJeeja KP 	int ret, i = 0, num_routes = 0, j;
104479f4e922SSubhransu S. Prusty 
104579f4e922SSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
104679f4e922SSubhransu S. Prusty 		return -EINVAL;
104779f4e922SSubhransu S. Prusty 
1048754695f9SJeeja KP 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1049754695f9SJeeja KP 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
105079f4e922SSubhransu S. Prusty 				GFP_KERNEL);
105179f4e922SSubhransu S. Prusty 
105279f4e922SSubhransu S. Prusty 	if (!widgets)
105379f4e922SSubhransu S. Prusty 		return -ENOMEM;
105479f4e922SSubhransu S. Prusty 
105579f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent each converter widget */
105679f4e922SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
105779f4e922SSubhransu S. Prusty 		sprintf(widget_name, "Converter %d", cvt->nid);
105879f4e922SSubhransu S. Prusty 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1059c9bfb5d7SJeeja KP 			snd_soc_dapm_aif_in, cvt,
1060c9bfb5d7SJeeja KP 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1061c9bfb5d7SJeeja KP 			hdac_hdmi_cvt_output_widget_event,
1062c9bfb5d7SJeeja KP 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
106379f4e922SSubhransu S. Prusty 		if (ret < 0)
106479f4e922SSubhransu S. Prusty 			return ret;
106579f4e922SSubhransu S. Prusty 		i++;
106679f4e922SSubhransu S. Prusty 	}
106779f4e922SSubhransu S. Prusty 
106879f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1069754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1070754695f9SJeeja KP 			sprintf(widget_name, "hif%d-%d Output",
1071754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
107279f4e922SSubhransu S. Prusty 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1073754695f9SJeeja KP 					snd_soc_dapm_output, &pin->ports[j],
1074c9bfb5d7SJeeja KP 					widget_name, NULL, NULL, 0,
1075c9bfb5d7SJeeja KP 					hdac_hdmi_pin_output_widget_event,
1076754695f9SJeeja KP 					SND_SOC_DAPM_PRE_PMU |
1077754695f9SJeeja KP 					SND_SOC_DAPM_POST_PMD);
107879f4e922SSubhransu S. Prusty 			if (ret < 0)
107979f4e922SSubhransu S. Prusty 				return ret;
10800324e51bSJeeja KP 			pin->ports[j].output_pin = widgets[i].name;
108179f4e922SSubhransu S. Prusty 			i++;
108279f4e922SSubhransu S. Prusty 		}
1083754695f9SJeeja KP 	}
108479f4e922SSubhransu S. Prusty 
108579f4e922SSubhransu S. Prusty 	/* DAPM widgets to represent the connection list to pin widget */
108679f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1087754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
1088754695f9SJeeja KP 			sprintf(widget_name, "Pin%d-Port%d Mux",
1089754695f9SJeeja KP 				pin->nid, pin->ports[j].id);
1090754695f9SJeeja KP 			ret = hdac_hdmi_create_pin_port_muxs(edev,
1091754695f9SJeeja KP 						&pin->ports[j], &widgets[i],
109279f4e922SSubhransu S. Prusty 						widget_name);
109379f4e922SSubhransu S. Prusty 			if (ret < 0)
109479f4e922SSubhransu S. Prusty 				return ret;
109579f4e922SSubhransu S. Prusty 			i++;
109679f4e922SSubhransu S. Prusty 
109779f4e922SSubhransu S. Prusty 			/* For cvt to pin_mux mapping */
109879f4e922SSubhransu S. Prusty 			num_routes += hdmi->num_cvt;
109979f4e922SSubhransu S. Prusty 
110079f4e922SSubhransu S. Prusty 			/* For pin_mux to pin mapping */
110179f4e922SSubhransu S. Prusty 			num_routes++;
110279f4e922SSubhransu S. Prusty 		}
1103754695f9SJeeja KP 	}
110479f4e922SSubhransu S. Prusty 
110579f4e922SSubhransu S. Prusty 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
110679f4e922SSubhransu S. Prusty 							GFP_KERNEL);
110779f4e922SSubhransu S. Prusty 	if (!route)
110879f4e922SSubhransu S. Prusty 		return -ENOMEM;
110979f4e922SSubhransu S. Prusty 
111079f4e922SSubhransu S. Prusty 	i = 0;
111179f4e922SSubhransu S. Prusty 	/* Add pin <- NULL <- mux route map */
111279f4e922SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1113754695f9SJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
111479f4e922SSubhransu S. Prusty 			int sink_index = i + hdmi->num_cvt;
1115754695f9SJeeja KP 			int src_index = sink_index + pin->num_ports *
1116754695f9SJeeja KP 						hdmi->num_pin;
111779f4e922SSubhransu S. Prusty 
111879f4e922SSubhransu S. Prusty 			hdac_hdmi_fill_route(&route[i],
111979f4e922SSubhransu S. Prusty 				widgets[sink_index].name, NULL,
112079f4e922SSubhransu S. Prusty 				widgets[src_index].name, NULL);
112179f4e922SSubhransu S. Prusty 			i++;
1122754695f9SJeeja KP 		}
112379f4e922SSubhransu S. Prusty 	}
112479f4e922SSubhransu S. Prusty 
112579f4e922SSubhransu S. Prusty 	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
112679f4e922SSubhransu S. Prusty 
112779f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_controls(dapm, widgets,
1128754695f9SJeeja KP 		((2 * hdmi->num_ports) + hdmi->num_cvt));
112979f4e922SSubhransu S. Prusty 
113079f4e922SSubhransu S. Prusty 	snd_soc_dapm_add_routes(dapm, route, num_routes);
113179f4e922SSubhransu S. Prusty 	snd_soc_dapm_new_widgets(dapm->card);
113279f4e922SSubhransu S. Prusty 
113379f4e922SSubhransu S. Prusty 	return 0;
113479f4e922SSubhransu S. Prusty 
113518382eadSSubhransu S. Prusty }
113618382eadSSubhransu S. Prusty 
113715b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
113818382eadSSubhransu S. Prusty {
1139f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
1140754695f9SJeeja KP 	struct hdac_hdmi_dai_port_map *dai_map;
114115b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
1142148569fdSSubhransu S. Prusty 	int dai_id = 0;
114318382eadSSubhransu S. Prusty 
1144148569fdSSubhransu S. Prusty 	if (list_empty(&hdmi->cvt_list))
114515b91447SSubhransu S. Prusty 		return -EINVAL;
114618382eadSSubhransu S. Prusty 
1147148569fdSSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1148148569fdSSubhransu S. Prusty 		dai_map = &hdmi->dai_map[dai_id];
1149148569fdSSubhransu S. Prusty 		dai_map->dai_id = dai_id;
115015b91447SSubhransu S. Prusty 		dai_map->cvt = cvt;
115118382eadSSubhransu S. Prusty 
1152148569fdSSubhransu S. Prusty 		dai_id++;
1153148569fdSSubhransu S. Prusty 
1154148569fdSSubhransu S. Prusty 		if (dai_id == HDA_MAX_CVTS) {
1155f0c5ebebSUghreja, Rakesh A 			dev_warn(&edev->hdev.dev,
1156148569fdSSubhransu S. Prusty 				"Max dais supported: %d\n", dai_id);
1157148569fdSSubhransu S. Prusty 			break;
1158148569fdSSubhransu S. Prusty 		}
1159148569fdSSubhransu S. Prusty 	}
116018382eadSSubhransu S. Prusty 
116115b91447SSubhransu S. Prusty 	return 0;
116215b91447SSubhransu S. Prusty }
116315b91447SSubhransu S. Prusty 
116415b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
116515b91447SSubhransu S. Prusty {
1166f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
116715b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
11684a3478deSJeeja KP 	char name[NAME_SIZE];
116915b91447SSubhransu S. Prusty 
117015b91447SSubhransu S. Prusty 	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
117115b91447SSubhransu S. Prusty 	if (!cvt)
117215b91447SSubhransu S. Prusty 		return -ENOMEM;
117315b91447SSubhransu S. Prusty 
117415b91447SSubhransu S. Prusty 	cvt->nid = nid;
11754a3478deSJeeja KP 	sprintf(name, "cvt %d", cvt->nid);
11764a3478deSJeeja KP 	cvt->name = kstrdup(name, GFP_KERNEL);
117715b91447SSubhransu S. Prusty 
117815b91447SSubhransu S. Prusty 	list_add_tail(&cvt->head, &hdmi->cvt_list);
117915b91447SSubhransu S. Prusty 	hdmi->num_cvt++;
118015b91447SSubhransu S. Prusty 
1181f0c5ebebSUghreja, Rakesh A 	return hdac_hdmi_query_cvt_params(&edev->hdev, cvt);
118215b91447SSubhransu S. Prusty }
118315b91447SSubhransu S. Prusty 
1184f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1185754695f9SJeeja KP 			struct hdac_hdmi_port *port)
1186b7756edeSSubhransu S. Prusty {
1187f6fa11a3SSandeep Tayal 	unsigned int ver, mnl;
1188f6fa11a3SSandeep Tayal 
1189754695f9SJeeja KP 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1190f6fa11a3SSandeep Tayal 						>> DRM_ELD_VER_SHIFT;
1191f6fa11a3SSandeep Tayal 
1192f6fa11a3SSandeep Tayal 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1193f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "HDMI: Unknown ELD version %d\n", ver);
1194f6fa11a3SSandeep Tayal 		return -EINVAL;
1195b7756edeSSubhransu S. Prusty 	}
1196b7756edeSSubhransu S. Prusty 
1197754695f9SJeeja KP 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1198f6fa11a3SSandeep Tayal 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1199f6fa11a3SSandeep Tayal 
1200f6fa11a3SSandeep Tayal 	if (mnl > ELD_MAX_MNL) {
1201f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "HDMI: MNL Invalid %d\n", mnl);
1202f6fa11a3SSandeep Tayal 		return -EINVAL;
1203f6fa11a3SSandeep Tayal 	}
1204f6fa11a3SSandeep Tayal 
1205754695f9SJeeja KP 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1206f6fa11a3SSandeep Tayal 
1207f6fa11a3SSandeep Tayal 	return 0;
1208f6fa11a3SSandeep Tayal }
1209f6fa11a3SSandeep Tayal 
1210754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1211754695f9SJeeja KP 				    struct hdac_hdmi_port *port)
1212b8a54545SSubhransu S. Prusty {
1213b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = pin->edev;
1214f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
12154a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
1216754695f9SJeeja KP 	int size = 0;
12172acd8309SJeeja KP 	int port_id = -1;
1218754695f9SJeeja KP 
1219754695f9SJeeja KP 	if (!hdmi)
1220754695f9SJeeja KP 		return;
12214a3478deSJeeja KP 
12222acd8309SJeeja KP 	/*
12232acd8309SJeeja KP 	 * In case of non MST pin, get_eld info API expectes port
12242acd8309SJeeja KP 	 * to be -1.
12252acd8309SJeeja KP 	 */
12264a3478deSJeeja KP 	mutex_lock(&hdmi->pin_mutex);
1227754695f9SJeeja KP 	port->eld.monitor_present = false;
1228f6fa11a3SSandeep Tayal 
12292acd8309SJeeja KP 	if (pin->mst_capable)
12302acd8309SJeeja KP 		port_id = port->id;
12312acd8309SJeeja KP 
1232f0c5ebebSUghreja, Rakesh A 	size = snd_hdac_acomp_get_eld(&edev->hdev, pin->nid, port_id,
1233754695f9SJeeja KP 				&port->eld.monitor_present,
1234754695f9SJeeja KP 				port->eld.eld_buffer,
1235f6fa11a3SSandeep Tayal 				ELD_MAX_SIZE);
1236f6fa11a3SSandeep Tayal 
1237f6fa11a3SSandeep Tayal 	if (size > 0) {
1238f6fa11a3SSandeep Tayal 		size = min(size, ELD_MAX_SIZE);
1239754695f9SJeeja KP 		if (hdac_hdmi_parse_eld(edev, port) < 0)
1240f6fa11a3SSandeep Tayal 			size = -EINVAL;
1241f6fa11a3SSandeep Tayal 	}
1242f6fa11a3SSandeep Tayal 
1243f6fa11a3SSandeep Tayal 	if (size > 0) {
1244754695f9SJeeja KP 		port->eld.eld_valid = true;
1245754695f9SJeeja KP 		port->eld.eld_size = size;
1246f6fa11a3SSandeep Tayal 	} else {
1247754695f9SJeeja KP 		port->eld.eld_valid = false;
1248754695f9SJeeja KP 		port->eld.eld_size = 0;
1249f6fa11a3SSandeep Tayal 	}
1250b8a54545SSubhransu S. Prusty 
1251754695f9SJeeja KP 	pcm = hdac_hdmi_get_pcm(edev, port);
12524a3478deSJeeja KP 
1253754695f9SJeeja KP 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1254b8a54545SSubhransu S. Prusty 
1255f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "%s: disconnect for pin:port %d:%d\n",
1256754695f9SJeeja KP 						__func__, pin->nid, port->id);
12574a3478deSJeeja KP 
12584a3478deSJeeja KP 		/*
12594a3478deSJeeja KP 		 * PCMs are not registered during device probe, so don't
12604a3478deSJeeja KP 		 * report jack here. It will be done in usermode mux
12614a3478deSJeeja KP 		 * control select.
12624a3478deSJeeja KP 		 */
1263e0e5d3e5SJeeja KP 		if (pcm)
1264e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, false);
12654a3478deSJeeja KP 
12664a3478deSJeeja KP 		mutex_unlock(&hdmi->pin_mutex);
1267f6fa11a3SSandeep Tayal 		return;
1268b8a54545SSubhransu S. Prusty 	}
1269b8a54545SSubhransu S. Prusty 
1270754695f9SJeeja KP 	if (port->eld.monitor_present && port->eld.eld_valid) {
1271e0e5d3e5SJeeja KP 		if (pcm)
1272e0e5d3e5SJeeja KP 			hdac_hdmi_jack_report(pcm, port, true);
12734a3478deSJeeja KP 
1274f6fa11a3SSandeep Tayal 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1275754695f9SJeeja KP 			  port->eld.eld_buffer, port->eld.eld_size, false);
1276754695f9SJeeja KP 
1277754695f9SJeeja KP 	}
1278754695f9SJeeja KP 	mutex_unlock(&hdmi->pin_mutex);
12794a3478deSJeeja KP }
12804a3478deSJeeja KP 
1281754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1282754695f9SJeeja KP 				struct hdac_hdmi_pin *pin)
1283754695f9SJeeja KP {
1284754695f9SJeeja KP 	struct hdac_hdmi_port *ports;
1285754695f9SJeeja KP 	int max_ports = HDA_MAX_PORTS;
1286754695f9SJeeja KP 	int i;
1287754695f9SJeeja KP 
1288754695f9SJeeja KP 	/*
1289754695f9SJeeja KP 	 * FIXME: max_port may vary for each platform, so pass this as
1290754695f9SJeeja KP 	 * as driver data or query from i915 interface when this API is
1291754695f9SJeeja KP 	 * implemented.
1292754695f9SJeeja KP 	 */
1293754695f9SJeeja KP 
1294754695f9SJeeja KP 	ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1295754695f9SJeeja KP 	if (!ports)
1296754695f9SJeeja KP 		return -ENOMEM;
1297754695f9SJeeja KP 
1298754695f9SJeeja KP 	for (i = 0; i < max_ports; i++) {
1299754695f9SJeeja KP 		ports[i].id = i;
1300754695f9SJeeja KP 		ports[i].pin = pin;
1301754695f9SJeeja KP 	}
1302754695f9SJeeja KP 	pin->ports = ports;
1303754695f9SJeeja KP 	pin->num_ports = max_ports;
1304754695f9SJeeja KP 	return 0;
1305b8a54545SSubhransu S. Prusty }
1306b8a54545SSubhransu S. Prusty 
130715b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
130815b91447SSubhransu S. Prusty {
1309f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
131015b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin;
1311754695f9SJeeja KP 	int ret;
131215b91447SSubhransu S. Prusty 
131315b91447SSubhransu S. Prusty 	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
131415b91447SSubhransu S. Prusty 	if (!pin)
131515b91447SSubhransu S. Prusty 		return -ENOMEM;
131615b91447SSubhransu S. Prusty 
131715b91447SSubhransu S. Prusty 	pin->nid = nid;
13182acd8309SJeeja KP 	pin->mst_capable = false;
1319754695f9SJeeja KP 	pin->edev = edev;
1320754695f9SJeeja KP 	ret = hdac_hdmi_add_ports(hdmi, pin);
1321754695f9SJeeja KP 	if (ret < 0)
1322754695f9SJeeja KP 		return ret;
132315b91447SSubhransu S. Prusty 
132415b91447SSubhransu S. Prusty 	list_add_tail(&pin->head, &hdmi->pin_list);
132515b91447SSubhransu S. Prusty 	hdmi->num_pin++;
1326754695f9SJeeja KP 	hdmi->num_ports += pin->num_ports;
1327b8a54545SSubhransu S. Prusty 
132815b91447SSubhransu S. Prusty 	return 0;
132918382eadSSubhransu S. Prusty }
133018382eadSSubhransu S. Prusty 
1331211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08
13325622bc95SPradeep Tewani #define INTEL_GLK_VENDOR_NID 0x0b
1333211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81
1334211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781
1335211caab7SSubhransu S. Prusty #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1336211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1337211caab7SSubhransu S. Prusty 
1338f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1339211caab7SSubhransu S. Prusty {
1340211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1341f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13425622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1343211caab7SSubhransu S. Prusty 
1344f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1345211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1346211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1347211caab7SSubhransu S. Prusty 		return;
1348211caab7SSubhransu S. Prusty 
1349211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1350f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1351211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1352211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1353211caab7SSubhransu S. Prusty 		return;
1354211caab7SSubhransu S. Prusty }
1355211caab7SSubhransu S. Prusty 
1356f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1357211caab7SSubhransu S. Prusty {
1358211caab7SSubhransu S. Prusty 	unsigned int vendor_param;
1359f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
13605622bc95SPradeep Tewani 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1361211caab7SSubhransu S. Prusty 
1362f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1363211caab7SSubhransu S. Prusty 				INTEL_GET_VENDOR_VERB, 0);
1364211caab7SSubhransu S. Prusty 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1365211caab7SSubhransu S. Prusty 		return;
1366211caab7SSubhransu S. Prusty 
1367211caab7SSubhransu S. Prusty 	/* enable DP1.2 mode */
1368211caab7SSubhransu S. Prusty 	vendor_param |= INTEL_EN_DP12;
1369f0c5ebebSUghreja, Rakesh A 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1370211caab7SSubhransu S. Prusty 				INTEL_SET_VENDOR_VERB, vendor_param);
1371211caab7SSubhransu S. Prusty 	if (vendor_param == -1)
1372211caab7SSubhransu S. Prusty 		return;
1373211caab7SSubhransu S. Prusty 
1374211caab7SSubhransu S. Prusty }
1375211caab7SSubhransu S. Prusty 
137661b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = {
137717a42c45SSubhransu S. Prusty 	.startup = hdac_hdmi_pcm_open,
137817a42c45SSubhransu S. Prusty 	.shutdown = hdac_hdmi_pcm_close,
137917a42c45SSubhransu S. Prusty 	.hw_params = hdac_hdmi_set_hw_params,
1380c9bfb5d7SJeeja KP 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
138117a42c45SSubhransu S. Prusty };
138217a42c45SSubhransu S. Prusty 
138317a42c45SSubhransu S. Prusty /*
138417a42c45SSubhransu S. Prusty  * Each converter can support a stream independently. So a dai is created
138517a42c45SSubhransu S. Prusty  * based on the number of converter queried.
138617a42c45SSubhransu S. Prusty  */
1387f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev,
138817a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais,
138917a42c45SSubhransu S. Prusty 		struct hdac_hdmi_priv *hdmi, int num_dais)
139017a42c45SSubhransu S. Prusty {
139117a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais;
139217a42c45SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt;
139317a42c45SSubhransu S. Prusty 	char name[NAME_SIZE], dai_name[NAME_SIZE];
139417a42c45SSubhransu S. Prusty 	int i = 0;
139517a42c45SSubhransu S. Prusty 	u32 rates, bps;
139617a42c45SSubhransu S. Prusty 	unsigned int rate_max = 384000, rate_min = 8000;
139717a42c45SSubhransu S. Prusty 	u64 formats;
139817a42c45SSubhransu S. Prusty 	int ret;
139917a42c45SSubhransu S. Prusty 
1400f0c5ebebSUghreja, Rakesh A 	hdmi_dais = devm_kzalloc(&hdev->dev,
140117a42c45SSubhransu S. Prusty 			(sizeof(*hdmi_dais) * num_dais),
140217a42c45SSubhransu S. Prusty 			GFP_KERNEL);
140317a42c45SSubhransu S. Prusty 	if (!hdmi_dais)
140417a42c45SSubhransu S. Prusty 		return -ENOMEM;
140517a42c45SSubhransu S. Prusty 
140617a42c45SSubhransu S. Prusty 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1407f0c5ebebSUghreja, Rakesh A 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
140817a42c45SSubhransu S. Prusty 					&rates,	&formats, &bps);
140917a42c45SSubhransu S. Prusty 		if (ret)
141017a42c45SSubhransu S. Prusty 			return ret;
141117a42c45SSubhransu S. Prusty 
141217a42c45SSubhransu S. Prusty 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1413f0c5ebebSUghreja, Rakesh A 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
141417a42c45SSubhransu S. Prusty 					dai_name, GFP_KERNEL);
141517a42c45SSubhransu S. Prusty 
141617a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].name)
141717a42c45SSubhransu S. Prusty 			return -ENOMEM;
141817a42c45SSubhransu S. Prusty 
141917a42c45SSubhransu S. Prusty 		snprintf(name, sizeof(name), "hifi%d", i+1);
142017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.stream_name =
1421f0c5ebebSUghreja, Rakesh A 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
142217a42c45SSubhransu S. Prusty 		if (!hdmi_dais[i].playback.stream_name)
142317a42c45SSubhransu S. Prusty 			return -ENOMEM;
142417a42c45SSubhransu S. Prusty 
142517a42c45SSubhransu S. Prusty 		/*
142617a42c45SSubhransu S. Prusty 		 * Set caps based on capability queried from the converter.
142717a42c45SSubhransu S. Prusty 		 * It will be constrained runtime based on ELD queried.
142817a42c45SSubhransu S. Prusty 		 */
142917a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.formats = formats;
143017a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rates = rates;
143117a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_max = rate_max;
143217a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.rate_min = rate_min;
143317a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_min = 2;
143417a42c45SSubhransu S. Prusty 		hdmi_dais[i].playback.channels_max = 2;
143566d6bbc6SJeeja KP 		hdmi_dais[i].playback.sig_bits = bps;
143617a42c45SSubhransu S. Prusty 		hdmi_dais[i].ops = &hdmi_dai_ops;
143717a42c45SSubhransu S. Prusty 		i++;
143817a42c45SSubhransu S. Prusty 	}
143917a42c45SSubhransu S. Prusty 
144017a42c45SSubhransu S. Prusty 	*dais = hdmi_dais;
14411e02dac3SKuninori Morimoto 	hdmi->dai_drv = hdmi_dais;
144217a42c45SSubhransu S. Prusty 
144317a42c45SSubhransu S. Prusty 	return 0;
144417a42c45SSubhransu S. Prusty }
144517a42c45SSubhransu S. Prusty 
144618382eadSSubhransu S. Prusty /*
144718382eadSSubhransu S. Prusty  * Parse all nodes and store the cvt/pin nids in array
144818382eadSSubhransu S. Prusty  * Add one time initialization for pin and cvt widgets
144918382eadSSubhransu S. Prusty  */
145017a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
145117a42c45SSubhransu S. Prusty 		struct snd_soc_dai_driver **dais, int *num_dais)
145218382eadSSubhransu S. Prusty {
145318382eadSSubhransu S. Prusty 	hda_nid_t nid;
14543c83ac23SSudip Mukherjee 	int i, num_nodes;
14551c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
14561c0a7de2SSubhransu S. Prusty 	struct hdac_hdmi_pin *temp_pin, *pin_next;
1457f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
1458f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
145915b91447SSubhransu S. Prusty 	int ret;
146018382eadSSubhransu S. Prusty 
1461f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(hdev);
1462f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(hdev);
1463211caab7SSubhransu S. Prusty 
1464f0c5ebebSUghreja, Rakesh A 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1465541140d4SSubhransu S. Prusty 	if (!nid || num_nodes <= 0) {
1466f0c5ebebSUghreja, Rakesh A 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
146718382eadSSubhransu S. Prusty 		return -EINVAL;
146818382eadSSubhransu S. Prusty 	}
146918382eadSSubhransu S. Prusty 
147045a6008bSPuneeth Prabhu 	for (i = 0; i < num_nodes; i++, nid++) {
147118382eadSSubhransu S. Prusty 		unsigned int caps;
147218382eadSSubhransu S. Prusty 		unsigned int type;
147318382eadSSubhransu S. Prusty 
1474f0c5ebebSUghreja, Rakesh A 		caps = get_wcaps(hdev, nid);
147518382eadSSubhransu S. Prusty 		type = get_wcaps_type(caps);
147618382eadSSubhransu S. Prusty 
147718382eadSSubhransu S. Prusty 		if (!(caps & AC_WCAP_DIGITAL))
147818382eadSSubhransu S. Prusty 			continue;
147918382eadSSubhransu S. Prusty 
148018382eadSSubhransu S. Prusty 		switch (type) {
148118382eadSSubhransu S. Prusty 
148218382eadSSubhransu S. Prusty 		case AC_WID_AUD_OUT:
148315b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_cvt(edev, nid);
148415b91447SSubhransu S. Prusty 			if (ret < 0)
14851c0a7de2SSubhransu S. Prusty 				goto free_widgets;
148618382eadSSubhransu S. Prusty 			break;
148718382eadSSubhransu S. Prusty 
148818382eadSSubhransu S. Prusty 		case AC_WID_PIN:
148915b91447SSubhransu S. Prusty 			ret = hdac_hdmi_add_pin(edev, nid);
149015b91447SSubhransu S. Prusty 			if (ret < 0)
14911c0a7de2SSubhransu S. Prusty 				goto free_widgets;
149218382eadSSubhransu S. Prusty 			break;
149318382eadSSubhransu S. Prusty 		}
149418382eadSSubhransu S. Prusty 	}
149518382eadSSubhransu S. Prusty 
14961c0a7de2SSubhransu S. Prusty 	if (!hdmi->num_pin || !hdmi->num_cvt) {
14971c0a7de2SSubhransu S. Prusty 		ret = -EIO;
14981c0a7de2SSubhransu S. Prusty 		goto free_widgets;
14991c0a7de2SSubhransu S. Prusty 	}
150018382eadSSubhransu S. Prusty 
1501f0c5ebebSUghreja, Rakesh A 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
150217a42c45SSubhransu S. Prusty 	if (ret) {
1503f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
150417a42c45SSubhransu S. Prusty 							ret);
15051c0a7de2SSubhransu S. Prusty 		goto free_widgets;
150617a42c45SSubhransu S. Prusty 	}
150717a42c45SSubhransu S. Prusty 
150817a42c45SSubhransu S. Prusty 	*num_dais = hdmi->num_cvt;
15091c0a7de2SSubhransu S. Prusty 	ret = hdac_hdmi_init_dai_map(edev);
15101c0a7de2SSubhransu S. Prusty 	if (ret < 0)
15111c0a7de2SSubhransu S. Prusty 		goto free_widgets;
151217a42c45SSubhransu S. Prusty 
15131c0a7de2SSubhransu S. Prusty 	return ret;
15141c0a7de2SSubhransu S. Prusty 
15151c0a7de2SSubhransu S. Prusty free_widgets:
15161c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
15171c0a7de2SSubhransu S. Prusty 		list_del(&temp_cvt->head);
15181c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt->name);
15191c0a7de2SSubhransu S. Prusty 		kfree(temp_cvt);
15201c0a7de2SSubhransu S. Prusty 	}
15211c0a7de2SSubhransu S. Prusty 
15221c0a7de2SSubhransu S. Prusty 	list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
15231c0a7de2SSubhransu S. Prusty 		for (i = 0; i < temp_pin->num_ports; i++)
15241c0a7de2SSubhransu S. Prusty 			temp_pin->ports[i].pin = NULL;
15251c0a7de2SSubhransu S. Prusty 		kfree(temp_pin->ports);
15261c0a7de2SSubhransu S. Prusty 		list_del(&temp_pin->head);
15271c0a7de2SSubhransu S. Prusty 		kfree(temp_pin);
15281c0a7de2SSubhransu S. Prusty 	}
15291c0a7de2SSubhransu S. Prusty 
15301c0a7de2SSubhransu S. Prusty 	return ret;
153118382eadSSubhransu S. Prusty }
153218382eadSSubhransu S. Prusty 
1533f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1534b8a54545SSubhransu S. Prusty {
1535b8a54545SSubhransu S. Prusty 	struct hdac_ext_device *edev = aptr;
1536f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
1537754695f9SJeeja KP 	struct hdac_hdmi_pin *pin = NULL;
1538754695f9SJeeja KP 	struct hdac_hdmi_port *hport = NULL;
153945101122SKuninori Morimoto 	struct snd_soc_component *component = edev->scodec;
15402acd8309SJeeja KP 	int i;
1541b8a54545SSubhransu S. Prusty 
1542b8a54545SSubhransu S. Prusty 	/* Don't know how this mapping is derived */
1543b8a54545SSubhransu S. Prusty 	hda_nid_t pin_nid = port + 0x04;
1544b8a54545SSubhransu S. Prusty 
1545f0c5ebebSUghreja, Rakesh A 	dev_dbg(&edev->hdev.dev, "%s: for pin:%d port=%d\n", __func__,
1546754695f9SJeeja KP 							pin_nid, pipe);
1547b8a54545SSubhransu S. Prusty 
1548b8a54545SSubhransu S. Prusty 	/*
1549b8a54545SSubhransu S. Prusty 	 * skip notification during system suspend (but not in runtime PM);
1550b8a54545SSubhransu S. Prusty 	 * the state will be updated at resume. Also since the ELD and
1551b8a54545SSubhransu S. Prusty 	 * connection states are updated in anyway at the end of the resume,
1552b8a54545SSubhransu S. Prusty 	 * we can skip it when received during PM process.
1553b8a54545SSubhransu S. Prusty 	 */
155445101122SKuninori Morimoto 	if (snd_power_get_state(component->card->snd_card) !=
1555b8a54545SSubhransu S. Prusty 			SNDRV_CTL_POWER_D0)
1556b8a54545SSubhransu S. Prusty 		return;
1557b8a54545SSubhransu S. Prusty 
1558f0c5ebebSUghreja, Rakesh A 	if (atomic_read(&edev->hdev.in_pm))
1559b8a54545SSubhransu S. Prusty 		return;
1560b8a54545SSubhransu S. Prusty 
1561b8a54545SSubhransu S. Prusty 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1562754695f9SJeeja KP 		if (pin->nid != pin_nid)
1563754695f9SJeeja KP 			continue;
1564754695f9SJeeja KP 
1565754695f9SJeeja KP 		/* In case of non MST pin, pipe is -1 */
1566754695f9SJeeja KP 		if (pipe == -1) {
15672acd8309SJeeja KP 			pin->mst_capable = false;
1568754695f9SJeeja KP 			/* if not MST, default is port[0] */
1569754695f9SJeeja KP 			hport = &pin->ports[0];
15702acd8309SJeeja KP 		} else {
15712acd8309SJeeja KP 			for (i = 0; i < pin->num_ports; i++) {
15722acd8309SJeeja KP 				pin->mst_capable = true;
15732acd8309SJeeja KP 				if (pin->ports[i].id == pipe) {
15742acd8309SJeeja KP 					hport = &pin->ports[i];
157504c8f2bfSJeeja KP 					break;
15762acd8309SJeeja KP 				}
1577b8a54545SSubhransu S. Prusty 			}
1578b8a54545SSubhransu S. Prusty 		}
1579b8a54545SSubhransu S. Prusty 
158004c8f2bfSJeeja KP 		if (hport)
1581754695f9SJeeja KP 			hdac_hdmi_present_sense(pin, hport);
1582754695f9SJeeja KP 	}
1583754695f9SJeeja KP 
158404c8f2bfSJeeja KP }
158504c8f2bfSJeeja KP 
1586b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = {
1587b8a54545SSubhransu S. Prusty 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1588b8a54545SSubhransu S. Prusty };
1589b8a54545SSubhransu S. Prusty 
15902889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
15912889099eSSubhransu S. Prusty 						int device)
15922889099eSSubhransu S. Prusty {
15932889099eSSubhransu S. Prusty 	struct snd_soc_pcm_runtime *rtd;
15942889099eSSubhransu S. Prusty 
15952889099eSSubhransu S. Prusty 	list_for_each_entry(rtd, &card->rtd_list, list) {
15962889099eSSubhransu S. Prusty 		if (rtd->pcm && (rtd->pcm->device == device))
15972889099eSSubhransu S. Prusty 			return rtd->pcm;
15982889099eSSubhransu S. Prusty 	}
15992889099eSSubhransu S. Prusty 
16002889099eSSubhransu S. Prusty 	return NULL;
16012889099eSSubhransu S. Prusty }
16022889099eSSubhransu S. Prusty 
16030324e51bSJeeja KP /* create jack pin kcontrols */
16040324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card,
16050324e51bSJeeja KP 				    struct hdac_ext_device *edev)
16060324e51bSJeeja KP {
16070324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16080324e51bSJeeja KP 	struct snd_kcontrol_new *kc;
16090324e51bSJeeja KP 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
16100324e51bSJeeja KP 	char *name;
16110324e51bSJeeja KP 	int i = 0, j;
161245101122SKuninori Morimoto 	struct snd_soc_component *component = edev->scodec;
1613f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
16140324e51bSJeeja KP 
161545101122SKuninori Morimoto 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
16160324e51bSJeeja KP 				sizeof(*kc), GFP_KERNEL);
16170324e51bSJeeja KP 
16180324e51bSJeeja KP 	if (!kc)
16190324e51bSJeeja KP 		return -ENOMEM;
16200324e51bSJeeja KP 
16210324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16220324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16230324e51bSJeeja KP 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
16240324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
162545101122SKuninori Morimoto 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
16260324e51bSJeeja KP 			if (!name)
16270324e51bSJeeja KP 				return -ENOMEM;
16280324e51bSJeeja KP 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
162945101122SKuninori Morimoto 			kc[i].name = devm_kstrdup(component->dev, kc_name,
16300324e51bSJeeja KP 							GFP_KERNEL);
16310324e51bSJeeja KP 			if (!kc[i].name)
16320324e51bSJeeja KP 				return -ENOMEM;
16330324e51bSJeeja KP 
16340324e51bSJeeja KP 			kc[i].private_value = (unsigned long)name;
16350324e51bSJeeja KP 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
16360324e51bSJeeja KP 			kc[i].access = 0;
16370324e51bSJeeja KP 			kc[i].info = snd_soc_dapm_info_pin_switch;
16380324e51bSJeeja KP 			kc[i].put = snd_soc_dapm_put_pin_switch;
16390324e51bSJeeja KP 			kc[i].get = snd_soc_dapm_get_pin_switch;
16400324e51bSJeeja KP 			i++;
16410324e51bSJeeja KP 		}
16420324e51bSJeeja KP 	}
16430324e51bSJeeja KP 
16440324e51bSJeeja KP 	return snd_soc_add_card_controls(card, kc, i);
16450324e51bSJeeja KP }
16460324e51bSJeeja KP 
164745101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
16480324e51bSJeeja KP 			struct snd_soc_dapm_context *dapm)
16490324e51bSJeeja KP {
165045101122SKuninori Morimoto 	struct hdac_ext_device *edev = snd_soc_component_get_drvdata(component);
1651f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
16520324e51bSJeeja KP 	struct hdac_hdmi_pin *pin;
16530324e51bSJeeja KP 	struct snd_soc_dapm_widget *widgets;
16540324e51bSJeeja KP 	struct snd_soc_dapm_route *route;
16550324e51bSJeeja KP 	char w_name[NAME_SIZE];
16560324e51bSJeeja KP 	int i = 0, j, ret;
16570324e51bSJeeja KP 
16580324e51bSJeeja KP 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
16590324e51bSJeeja KP 				sizeof(*widgets), GFP_KERNEL);
16600324e51bSJeeja KP 
16610324e51bSJeeja KP 	if (!widgets)
16620324e51bSJeeja KP 		return -ENOMEM;
16630324e51bSJeeja KP 
16640324e51bSJeeja KP 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
16650324e51bSJeeja KP 				sizeof(*route), GFP_KERNEL);
16660324e51bSJeeja KP 	if (!route)
16670324e51bSJeeja KP 		return -ENOMEM;
16680324e51bSJeeja KP 
16690324e51bSJeeja KP 	/* create Jack DAPM widget */
16700324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
16710324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++) {
16720324e51bSJeeja KP 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
16730324e51bSJeeja KP 						pin->nid, pin->ports[j].id);
16740324e51bSJeeja KP 
16750324e51bSJeeja KP 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
16760324e51bSJeeja KP 					snd_soc_dapm_spk, NULL,
16770324e51bSJeeja KP 					w_name, NULL, NULL, 0, NULL, 0);
16780324e51bSJeeja KP 			if (ret < 0)
16790324e51bSJeeja KP 				return ret;
16800324e51bSJeeja KP 
16810324e51bSJeeja KP 			pin->ports[j].jack_pin = widgets[i].name;
16820324e51bSJeeja KP 			pin->ports[j].dapm = dapm;
16830324e51bSJeeja KP 
16840324e51bSJeeja KP 			/* add to route from Jack widget to output */
16850324e51bSJeeja KP 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
16860324e51bSJeeja KP 					NULL, pin->ports[j].output_pin, NULL);
16870324e51bSJeeja KP 
16880324e51bSJeeja KP 			i++;
16890324e51bSJeeja KP 		}
16900324e51bSJeeja KP 	}
16910324e51bSJeeja KP 
16920324e51bSJeeja KP 	/* Add Route from Jack widget to the output widget */
16930324e51bSJeeja KP 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
16940324e51bSJeeja KP 	if (ret < 0)
16950324e51bSJeeja KP 		return ret;
16960324e51bSJeeja KP 
16970324e51bSJeeja KP 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
16980324e51bSJeeja KP 	if (ret < 0)
16990324e51bSJeeja KP 		return ret;
17000324e51bSJeeja KP 
17010324e51bSJeeja KP 	ret = snd_soc_dapm_new_widgets(dapm->card);
17020324e51bSJeeja KP 	if (ret < 0)
17030324e51bSJeeja KP 		return ret;
17040324e51bSJeeja KP 
17050324e51bSJeeja KP 	/* Add Jack Pin switch Kcontrol */
17060324e51bSJeeja KP 	ret = create_fill_jack_kcontrols(dapm->card, edev);
17070324e51bSJeeja KP 
17080324e51bSJeeja KP 	if (ret < 0)
17090324e51bSJeeja KP 		return ret;
17100324e51bSJeeja KP 
17110324e51bSJeeja KP 	/* default set the Jack Pin switch to OFF */
17120324e51bSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
17130324e51bSJeeja KP 		for (j = 0; j < pin->num_ports; j++)
17140324e51bSJeeja KP 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
17150324e51bSJeeja KP 						pin->ports[j].jack_pin);
17160324e51bSJeeja KP 	}
17170324e51bSJeeja KP 
17180324e51bSJeeja KP 	return 0;
17190324e51bSJeeja KP }
17200324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
17210324e51bSJeeja KP 
172262490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
172362490016SJeeja KP 				struct snd_soc_jack *jack)
17244a3478deSJeeja KP {
172545101122SKuninori Morimoto 	struct snd_soc_component *component = dai->component;
172645101122SKuninori Morimoto 	struct hdac_ext_device *edev = snd_soc_component_get_drvdata(component);
1727f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
17284a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm;
17292889099eSSubhransu S. Prusty 	struct snd_pcm *snd_pcm;
17302889099eSSubhransu S. Prusty 	int err;
17314a3478deSJeeja KP 
17324a3478deSJeeja KP 	/*
17334a3478deSJeeja KP 	 * this is a new PCM device, create new pcm and
17344a3478deSJeeja KP 	 * add to the pcm list
17354a3478deSJeeja KP 	 */
17364a3478deSJeeja KP 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
17374a3478deSJeeja KP 	if (!pcm)
17384a3478deSJeeja KP 		return -ENOMEM;
17394a3478deSJeeja KP 	pcm->pcm_id = device;
17404a3478deSJeeja KP 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1741e0e5d3e5SJeeja KP 	pcm->jack_event = 0;
174262490016SJeeja KP 	pcm->jack = jack;
1743ab1eea19SJeeja KP 	mutex_init(&pcm->lock);
1744e0e5d3e5SJeeja KP 	INIT_LIST_HEAD(&pcm->port_list);
17452889099eSSubhransu S. Prusty 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
17462889099eSSubhransu S. Prusty 	if (snd_pcm) {
17472889099eSSubhransu S. Prusty 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
17482889099eSSubhransu S. Prusty 		if (err < 0) {
1749f0c5ebebSUghreja, Rakesh A 			dev_err(&edev->hdev.dev,
17502889099eSSubhransu S. Prusty 				"chmap control add failed with err: %d for pcm: %d\n",
17512889099eSSubhransu S. Prusty 				err, device);
17522889099eSSubhransu S. Prusty 			kfree(pcm);
17532889099eSSubhransu S. Prusty 			return err;
17542889099eSSubhransu S. Prusty 		}
17552889099eSSubhransu S. Prusty 	}
17562889099eSSubhransu S. Prusty 
17574a3478deSJeeja KP 	list_add_tail(&pcm->head, &hdmi->pcm_list);
17584a3478deSJeeja KP 
175962490016SJeeja KP 	return 0;
17604a3478deSJeeja KP }
17614a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
17624a3478deSJeeja KP 
1763a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1764a9ce96bcSJeeja KP 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1765a9ce96bcSJeeja KP {
1766a9ce96bcSJeeja KP 	int i;
1767a9ce96bcSJeeja KP 	struct hdac_hdmi_pin *pin;
1768a9ce96bcSJeeja KP 
1769a9ce96bcSJeeja KP 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1770a9ce96bcSJeeja KP 		if (detect_pin_caps) {
1771a9ce96bcSJeeja KP 
1772a9ce96bcSJeeja KP 			if (hdac_hdmi_get_port_len(edev, pin->nid)  == 0)
1773a9ce96bcSJeeja KP 				pin->mst_capable = false;
1774a9ce96bcSJeeja KP 			else
1775a9ce96bcSJeeja KP 				pin->mst_capable = true;
1776a9ce96bcSJeeja KP 		}
1777a9ce96bcSJeeja KP 
1778a9ce96bcSJeeja KP 		for (i = 0; i < pin->num_ports; i++) {
1779a9ce96bcSJeeja KP 			if (!pin->mst_capable && i > 0)
1780a9ce96bcSJeeja KP 				continue;
1781a9ce96bcSJeeja KP 
1782a9ce96bcSJeeja KP 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1783a9ce96bcSJeeja KP 		}
1784a9ce96bcSJeeja KP 	}
1785a9ce96bcSJeeja KP }
1786a9ce96bcSJeeja KP 
178745101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component)
178818382eadSSubhransu S. Prusty {
178945101122SKuninori Morimoto 	struct hdac_ext_device *edev = snd_soc_component_get_drvdata(component);
1790f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
179118382eadSSubhransu S. Prusty 	struct snd_soc_dapm_context *dapm =
179245101122SKuninori Morimoto 		snd_soc_component_get_dapm(component);
1793b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
1794a9ce96bcSJeeja KP 	int ret;
179518382eadSSubhransu S. Prusty 
179645101122SKuninori Morimoto 	edev->scodec = component;
179718382eadSSubhransu S. Prusty 
1798b2047e99SVinod Koul 	/*
1799b2047e99SVinod Koul 	 * hold the ref while we probe, also no need to drop the ref on
1800b2047e99SVinod Koul 	 * exit, we call pm_runtime_suspend() so that will do for us
1801b2047e99SVinod Koul 	 */
1802f0c5ebebSUghreja, Rakesh A 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdev.dev));
1803500e06b9SVinod Koul 	if (!hlink) {
1804f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "hdac link not found\n");
1805500e06b9SVinod Koul 		return -EIO;
1806500e06b9SVinod Koul 	}
1807500e06b9SVinod Koul 
1808b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1809b2047e99SVinod Koul 
181079f4e922SSubhransu S. Prusty 	ret = create_fill_widget_route_map(dapm);
181179f4e922SSubhransu S. Prusty 	if (ret < 0)
181279f4e922SSubhransu S. Prusty 		return ret;
181318382eadSSubhransu S. Prusty 
1814b8a54545SSubhransu S. Prusty 	aops.audio_ptr = edev;
1815b8a54545SSubhransu S. Prusty 	ret = snd_hdac_i915_register_notifier(&aops);
1816b8a54545SSubhransu S. Prusty 	if (ret < 0) {
1817f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "notifier register failed: err: %d\n",
1818b8a54545SSubhransu S. Prusty 				ret);
1819b8a54545SSubhransu S. Prusty 		return ret;
1820b8a54545SSubhransu S. Prusty 	}
1821b8a54545SSubhransu S. Prusty 
1822a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
182318382eadSSubhransu S. Prusty 	/* Imp: Store the card pointer in hda_codec */
182418382eadSSubhransu S. Prusty 	edev->card = dapm->card->snd_card;
182518382eadSSubhransu S. Prusty 
1826e342ac08SSubhransu S. Prusty 	/*
1827e342ac08SSubhransu S. Prusty 	 * hdac_device core already sets the state to active and calls
1828e342ac08SSubhransu S. Prusty 	 * get_noresume. So enable runtime and set the device to suspend.
1829e342ac08SSubhransu S. Prusty 	 */
1830f0c5ebebSUghreja, Rakesh A 	pm_runtime_enable(&edev->hdev.dev);
1831f0c5ebebSUghreja, Rakesh A 	pm_runtime_put(&edev->hdev.dev);
1832f0c5ebebSUghreja, Rakesh A 	pm_runtime_suspend(&edev->hdev.dev);
1833e342ac08SSubhransu S. Prusty 
1834e342ac08SSubhransu S. Prusty 	return 0;
1835e342ac08SSubhransu S. Prusty }
1836e342ac08SSubhransu S. Prusty 
183745101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component)
1838e342ac08SSubhransu S. Prusty {
183945101122SKuninori Morimoto 	struct hdac_ext_device *edev = snd_soc_component_get_drvdata(component);
1840e342ac08SSubhransu S. Prusty 
1841f0c5ebebSUghreja, Rakesh A 	pm_runtime_disable(&edev->hdev.dev);
184218382eadSSubhransu S. Prusty }
184318382eadSSubhransu S. Prusty 
1844571d5078SJeeja KP #ifdef CONFIG_PM
18451b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev)
18461b377ccdSSubhransu S. Prusty {
18471b377ccdSSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1848f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
18491b377ccdSSubhransu S. Prusty 
1850f0c5ebebSUghreja, Rakesh A 	pm_runtime_get_sync(&edev->hdev.dev);
18511b377ccdSSubhransu S. Prusty 
18521b377ccdSSubhransu S. Prusty 	/*
18531b377ccdSSubhransu S. Prusty 	 * Power down afg.
18541b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
18551b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
18561b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
18571b377ccdSSubhransu S. Prusty 	 * to read the state.
18581b377ccdSSubhransu S. Prusty 	 */
1859f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
18601b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
18611b377ccdSSubhransu S. Prusty 
18621b377ccdSSubhransu S. Prusty 	return 0;
18631b377ccdSSubhransu S. Prusty }
18641b377ccdSSubhransu S. Prusty 
18650fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev)
1866571d5078SJeeja KP {
18670fee1798SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
1868f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
1869f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
18701b377ccdSSubhransu S. Prusty 
18711b377ccdSSubhransu S. Prusty 	/* Power up afg */
1872f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
18731b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
1874571d5078SJeeja KP 
1875f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(&edev->hdev);
1876f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(&edev->hdev);
1877571d5078SJeeja KP 
1878571d5078SJeeja KP 	/*
1879571d5078SJeeja KP 	 * As the ELD notify callback request is not entertained while the
1880571d5078SJeeja KP 	 * device is in suspend state. Need to manually check detection of
1881a9ce96bcSJeeja KP 	 * all pins here. pin capablity change is not support, so use the
1882a9ce96bcSJeeja KP 	 * already set pin caps.
1883571d5078SJeeja KP 	 */
1884a9ce96bcSJeeja KP 	hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
1885571d5078SJeeja KP 
1886f0c5ebebSUghreja, Rakesh A 	pm_runtime_put_sync(&edev->hdev.dev);
1887571d5078SJeeja KP }
1888571d5078SJeeja KP #else
18891b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL
18900fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL
1891571d5078SJeeja KP #endif
1892571d5078SJeeja KP 
189345101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = {
189418382eadSSubhransu S. Prusty 	.probe			= hdmi_codec_probe,
1895e342ac08SSubhransu S. Prusty 	.remove			= hdmi_codec_remove,
189645101122SKuninori Morimoto 	.use_pmdown_time	= 1,
189745101122SKuninori Morimoto 	.endianness		= 1,
189845101122SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
189918382eadSSubhransu S. Prusty };
190018382eadSSubhransu S. Prusty 
1901f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
19022889099eSSubhransu S. Prusty 					unsigned char *chmap)
19032889099eSSubhransu S. Prusty {
1904f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19052889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19062889099eSSubhransu S. Prusty 
1907ab1eea19SJeeja KP 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
19082889099eSSubhransu S. Prusty }
19092889099eSSubhransu S. Prusty 
1910f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
19112889099eSSubhransu S. Prusty 				unsigned char *chmap, int prepared)
19122889099eSSubhransu S. Prusty {
1913f0c5ebebSUghreja, Rakesh A 	struct hdac_ext_device *edev = to_ehdac_device(hdev);
1914f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19152889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1916e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1917e0e5d3e5SJeeja KP 
1918eb50fa17SSubhransu S. Prusty 	if (!pcm)
1919eb50fa17SSubhransu S. Prusty 		return;
1920eb50fa17SSubhransu S. Prusty 
1921e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1922e0e5d3e5SJeeja KP 		return;
19232889099eSSubhransu S. Prusty 
1924ab1eea19SJeeja KP 	mutex_lock(&pcm->lock);
1925ab1eea19SJeeja KP 	pcm->chmap_set = true;
1926ab1eea19SJeeja KP 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1927e0e5d3e5SJeeja KP 	list_for_each_entry(port, &pcm->port_list, head)
19282889099eSSubhransu S. Prusty 		if (prepared)
1929754695f9SJeeja KP 			hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
1930ab1eea19SJeeja KP 	mutex_unlock(&pcm->lock);
19312889099eSSubhransu S. Prusty }
19322889099eSSubhransu S. Prusty 
1933f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
19342889099eSSubhransu S. Prusty {
1935f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19362889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
19372889099eSSubhransu S. Prusty 
1938eb50fa17SSubhransu S. Prusty 	if (!pcm)
1939eb50fa17SSubhransu S. Prusty 		return false;
1940eb50fa17SSubhransu S. Prusty 
1941e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1942e0e5d3e5SJeeja KP 		return false;
1943e0e5d3e5SJeeja KP 
1944e0e5d3e5SJeeja KP 	return true;
19452889099eSSubhransu S. Prusty }
19462889099eSSubhransu S. Prusty 
1947f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
19482889099eSSubhransu S. Prusty {
1949f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
19502889099eSSubhransu S. Prusty 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1951e0e5d3e5SJeeja KP 	struct hdac_hdmi_port *port;
1952e0e5d3e5SJeeja KP 
1953eb50fa17SSubhransu S. Prusty 	if (!pcm)
1954eb50fa17SSubhransu S. Prusty 		return 0;
1955eb50fa17SSubhransu S. Prusty 
1956e0e5d3e5SJeeja KP 	if (list_empty(&pcm->port_list))
1957e0e5d3e5SJeeja KP 		return 0;
1958e0e5d3e5SJeeja KP 
1959e0e5d3e5SJeeja KP 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1960e0e5d3e5SJeeja KP 
1961e0e5d3e5SJeeja KP 	if (!port)
1962e0e5d3e5SJeeja KP 		return 0;
19632889099eSSubhransu S. Prusty 
1964754695f9SJeeja KP 	if (!port || !port->eld.eld_valid)
19652889099eSSubhransu S. Prusty 		return 0;
19662889099eSSubhransu S. Prusty 
1967754695f9SJeeja KP 	return port->eld.info.spk_alloc;
19682889099eSSubhransu S. Prusty }
19692889099eSSubhransu S. Prusty 
19705622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
19715622bc95SPradeep Tewani 	.vendor_nid = INTEL_GLK_VENDOR_NID,
19725622bc95SPradeep Tewani };
19735622bc95SPradeep Tewani 
19745622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data  = {
19755622bc95SPradeep Tewani 	.vendor_nid = INTEL_VENDOR_NID,
19765622bc95SPradeep Tewani };
19775622bc95SPradeep Tewani 
197818382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
197918382eadSSubhransu S. Prusty {
1980f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
198118382eadSSubhransu S. Prusty 	struct hdac_hdmi_priv *hdmi_priv;
198217a42c45SSubhransu S. Prusty 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1983b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
198417a42c45SSubhransu S. Prusty 	int num_dais = 0;
198518382eadSSubhransu S. Prusty 	int ret = 0;
1986f0c5ebebSUghreja, Rakesh A 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1987f0c5ebebSUghreja, Rakesh A 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
198818382eadSSubhransu S. Prusty 
1989b2047e99SVinod Koul 	/* hold the ref while we probe */
1990f0c5ebebSUghreja, Rakesh A 	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdev.dev));
1991500e06b9SVinod Koul 	if (!hlink) {
1992f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev, "hdac link not found\n");
1993500e06b9SVinod Koul 		return -EIO;
1994500e06b9SVinod Koul 	}
1995500e06b9SVinod Koul 
1996b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1997b2047e99SVinod Koul 
1998f0c5ebebSUghreja, Rakesh A 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
199918382eadSSubhransu S. Prusty 	if (hdmi_priv == NULL)
200018382eadSSubhransu S. Prusty 		return -ENOMEM;
200118382eadSSubhransu S. Prusty 
200218382eadSSubhransu S. Prusty 	edev->private_data = hdmi_priv;
2003f0c5ebebSUghreja, Rakesh A 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
20042889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
20052889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
20062889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
20072889099eSSubhransu S. Prusty 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
200818382eadSSubhransu S. Prusty 
2009eb50fa17SSubhransu S. Prusty 	if (!hdac_id)
2010eb50fa17SSubhransu S. Prusty 		return -ENODEV;
2011eb50fa17SSubhransu S. Prusty 
20125622bc95SPradeep Tewani 	if (hdac_id->driver_data)
20135622bc95SPradeep Tewani 		hdmi_priv->drv_data =
20145622bc95SPradeep Tewani 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
20155622bc95SPradeep Tewani 	else
20165622bc95SPradeep Tewani 		hdmi_priv->drv_data = &intel_drv_data;
20175622bc95SPradeep Tewani 
2018f0c5ebebSUghreja, Rakesh A 	dev_set_drvdata(&hdev->dev, edev);
201918382eadSSubhransu S. Prusty 
202015b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
202115b91447SSubhransu S. Prusty 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
20224a3478deSJeeja KP 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
20234a3478deSJeeja KP 	mutex_init(&hdmi_priv->pin_mutex);
202415b91447SSubhransu S. Prusty 
2025aeaccef0SRamesh Babu 	/*
2026aeaccef0SRamesh Babu 	 * Turned off in the runtime_suspend during the first explicit
2027aeaccef0SRamesh Babu 	 * pm_runtime_suspend call.
2028aeaccef0SRamesh Babu 	 */
2029f0c5ebebSUghreja, Rakesh A 	ret = snd_hdac_display_power(edev->hdev.bus, true);
2030aeaccef0SRamesh Babu 	if (ret < 0) {
2031f0c5ebebSUghreja, Rakesh A 		dev_err(&edev->hdev.dev,
2032aeaccef0SRamesh Babu 			"Cannot turn on display power on i915 err: %d\n",
2033aeaccef0SRamesh Babu 			ret);
2034aeaccef0SRamesh Babu 		return ret;
2035aeaccef0SRamesh Babu 	}
2036aeaccef0SRamesh Babu 
203717a42c45SSubhransu S. Prusty 	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
203817a42c45SSubhransu S. Prusty 	if (ret < 0) {
2039f0c5ebebSUghreja, Rakesh A 		dev_err(&hdev->dev,
204017a42c45SSubhransu S. Prusty 			"Failed in parse and map nid with err: %d\n", ret);
204118382eadSSubhransu S. Prusty 		return ret;
204217a42c45SSubhransu S. Prusty 	}
20430fb02ba3SPuneeth Prabhu 	snd_hdac_refresh_widgets(hdev, true);
204418382eadSSubhransu S. Prusty 
204518382eadSSubhransu S. Prusty 	/* ASoC specific initialization */
204645101122SKuninori Morimoto 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
204717a42c45SSubhransu S. Prusty 					hdmi_dais, num_dais);
2048b2047e99SVinod Koul 
2049b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(edev->ebus, hlink);
2050b2047e99SVinod Koul 
2051b2047e99SVinod Koul 	return ret;
205218382eadSSubhransu S. Prusty }
205318382eadSSubhransu S. Prusty 
205418382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
205518382eadSSubhransu S. Prusty {
2056f0c5ebebSUghreja, Rakesh A 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdev);
205715b91447SSubhransu S. Prusty 	struct hdac_hdmi_pin *pin, *pin_next;
205815b91447SSubhransu S. Prusty 	struct hdac_hdmi_cvt *cvt, *cvt_next;
20594a3478deSJeeja KP 	struct hdac_hdmi_pcm *pcm, *pcm_next;
20602fe42dd0SJeeja KP 	struct hdac_hdmi_port *port, *port_next;
2061754695f9SJeeja KP 	int i;
206215b91447SSubhransu S. Prusty 
20634a3478deSJeeja KP 	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
20644a3478deSJeeja KP 		pcm->cvt = NULL;
2065e0e5d3e5SJeeja KP 		if (list_empty(&pcm->port_list))
2066e0e5d3e5SJeeja KP 			continue;
2067e0e5d3e5SJeeja KP 
20682fe42dd0SJeeja KP 		list_for_each_entry_safe(port, port_next,
20692fe42dd0SJeeja KP 					&pcm->port_list, head)
20702fe42dd0SJeeja KP 			list_del(&port->head);
2071e0e5d3e5SJeeja KP 
20724a3478deSJeeja KP 		list_del(&pcm->head);
20734a3478deSJeeja KP 		kfree(pcm);
20744a3478deSJeeja KP 	}
20754a3478deSJeeja KP 
207615b91447SSubhransu S. Prusty 	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
207715b91447SSubhransu S. Prusty 		list_del(&cvt->head);
20784a3478deSJeeja KP 		kfree(cvt->name);
207915b91447SSubhransu S. Prusty 		kfree(cvt);
208015b91447SSubhransu S. Prusty 	}
208115b91447SSubhransu S. Prusty 
208215b91447SSubhransu S. Prusty 	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2083754695f9SJeeja KP 		for (i = 0; i < pin->num_ports; i++)
2084754695f9SJeeja KP 			pin->ports[i].pin = NULL;
2085754695f9SJeeja KP 		kfree(pin->ports);
208615b91447SSubhransu S. Prusty 		list_del(&pin->head);
208715b91447SSubhransu S. Prusty 		kfree(pin);
208815b91447SSubhransu S. Prusty 	}
208915b91447SSubhransu S. Prusty 
209018382eadSSubhransu S. Prusty 	return 0;
209118382eadSSubhransu S. Prusty }
209218382eadSSubhransu S. Prusty 
2093e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM
2094e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev)
2095e342ac08SSubhransu S. Prusty {
2096e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2097f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
2098f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2099b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2100b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
210107f083abSSubhransu S. Prusty 	int err;
2102e342ac08SSubhransu S. Prusty 
2103e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2104e342ac08SSubhransu S. Prusty 
210507f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
210607f083abSSubhransu S. Prusty 	if (!bus)
210707f083abSSubhransu S. Prusty 		return 0;
210807f083abSSubhransu S. Prusty 
21091b377ccdSSubhransu S. Prusty 	/*
21101b377ccdSSubhransu S. Prusty 	 * Power down afg.
21111b377ccdSSubhransu S. Prusty 	 * codec_read is preferred over codec_write to set the power state.
21121b377ccdSSubhransu S. Prusty 	 * This way verb is send to set the power state and response
21131b377ccdSSubhransu S. Prusty 	 * is received. So setting power state is ensured without using loop
21141b377ccdSSubhransu S. Prusty 	 * to read the state.
21151b377ccdSSubhransu S. Prusty 	 */
2116f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21171b377ccdSSubhransu S. Prusty 							AC_PWRST_D3);
211807f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, false);
211907f083abSSubhransu S. Prusty 	if (err < 0) {
212007f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
212107f083abSSubhransu S. Prusty 		return err;
212207f083abSSubhransu S. Prusty 	}
212307f083abSSubhransu S. Prusty 
2124b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2125500e06b9SVinod Koul 	if (!hlink) {
2126500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2127500e06b9SVinod Koul 		return -EIO;
2128500e06b9SVinod Koul 	}
2129500e06b9SVinod Koul 
2130b2047e99SVinod Koul 	snd_hdac_ext_bus_link_put(ebus, hlink);
2131b2047e99SVinod Koul 
2132e342ac08SSubhransu S. Prusty 	return 0;
2133e342ac08SSubhransu S. Prusty }
2134e342ac08SSubhransu S. Prusty 
2135e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev)
2136e342ac08SSubhransu S. Prusty {
2137e342ac08SSubhransu S. Prusty 	struct hdac_ext_device *edev = to_hda_ext_device(dev);
2138f0c5ebebSUghreja, Rakesh A 	struct hdac_device *hdev = &edev->hdev;
2139f0c5ebebSUghreja, Rakesh A 	struct hdac_bus *bus = hdev->bus;
2140b2047e99SVinod Koul 	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2141b2047e99SVinod Koul 	struct hdac_ext_link *hlink = NULL;
214207f083abSSubhransu S. Prusty 	int err;
2143e342ac08SSubhransu S. Prusty 
2144e342ac08SSubhransu S. Prusty 	dev_dbg(dev, "Enter: %s\n", __func__);
2145e342ac08SSubhransu S. Prusty 
214607f083abSSubhransu S. Prusty 	/* controller may not have been initialized for the first time */
214707f083abSSubhransu S. Prusty 	if (!bus)
214807f083abSSubhransu S. Prusty 		return 0;
214907f083abSSubhransu S. Prusty 
2150b2047e99SVinod Koul 	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
2151500e06b9SVinod Koul 	if (!hlink) {
2152500e06b9SVinod Koul 		dev_err(dev, "hdac link not found\n");
2153500e06b9SVinod Koul 		return -EIO;
2154500e06b9SVinod Koul 	}
2155500e06b9SVinod Koul 
2156b2047e99SVinod Koul 	snd_hdac_ext_bus_link_get(ebus, hlink);
2157b2047e99SVinod Koul 
215807f083abSSubhransu S. Prusty 	err = snd_hdac_display_power(bus, true);
215907f083abSSubhransu S. Prusty 	if (err < 0) {
216007f083abSSubhransu S. Prusty 		dev_err(bus->dev, "Cannot turn on display power on i915\n");
216107f083abSSubhransu S. Prusty 		return err;
216207f083abSSubhransu S. Prusty 	}
216307f083abSSubhransu S. Prusty 
2164f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_all_pins(&edev->hdev);
2165f0c5ebebSUghreja, Rakesh A 	hdac_hdmi_skl_enable_dp12(&edev->hdev);
2166ab85f5b3SSubhransu S. Prusty 
2167e342ac08SSubhransu S. Prusty 	/* Power up afg */
2168f0c5ebebSUghreja, Rakesh A 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
21691b377ccdSSubhransu S. Prusty 							AC_PWRST_D0);
2170e342ac08SSubhransu S. Prusty 
2171e342ac08SSubhransu S. Prusty 	return 0;
2172e342ac08SSubhransu S. Prusty }
2173e342ac08SSubhransu S. Prusty #else
2174e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL
2175e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL
2176e342ac08SSubhransu S. Prusty #endif
2177e342ac08SSubhransu S. Prusty 
2178e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = {
2179e342ac08SSubhransu S. Prusty 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
21801b377ccdSSubhransu S. Prusty 	.prepare = hdmi_codec_prepare,
21810fee1798SSubhransu S. Prusty 	.complete = hdmi_codec_complete,
2182e342ac08SSubhransu S. Prusty };
2183e342ac08SSubhransu S. Prusty 
218418382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = {
218518382eadSSubhransu S. Prusty 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2186e2304803SJeeja KP 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2187cc216887SShreyas NC 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
21885fb6e0a1SGuneshwor Singh 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
21895fb6e0a1SGuneshwor Singh 						   &intel_glk_drv_data),
21905622bc95SPradeep Tewani 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
21915622bc95SPradeep Tewani 						   &intel_glk_drv_data),
219218382eadSSubhransu S. Prusty 	{}
219318382eadSSubhransu S. Prusty };
219418382eadSSubhransu S. Prusty 
219518382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
219618382eadSSubhransu S. Prusty 
219718382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = {
219818382eadSSubhransu S. Prusty 	. hdac = {
219918382eadSSubhransu S. Prusty 		.driver = {
220018382eadSSubhransu S. Prusty 			.name   = "HDMI HDA Codec",
2201e342ac08SSubhransu S. Prusty 			.pm = &hdac_hdmi_pm,
220218382eadSSubhransu S. Prusty 		},
220318382eadSSubhransu S. Prusty 		.id_table       = hdmi_list,
220418382eadSSubhransu S. Prusty 	},
220518382eadSSubhransu S. Prusty 	.probe          = hdac_hdmi_dev_probe,
220618382eadSSubhransu S. Prusty 	.remove         = hdac_hdmi_dev_remove,
220718382eadSSubhransu S. Prusty };
220818382eadSSubhransu S. Prusty 
220918382eadSSubhransu S. Prusty static int __init hdmi_init(void)
221018382eadSSubhransu S. Prusty {
221118382eadSSubhransu S. Prusty 	return snd_hda_ext_driver_register(&hdmi_driver);
221218382eadSSubhransu S. Prusty }
221318382eadSSubhransu S. Prusty 
221418382eadSSubhransu S. Prusty static void __exit hdmi_exit(void)
221518382eadSSubhransu S. Prusty {
221618382eadSSubhransu S. Prusty 	snd_hda_ext_driver_unregister(&hdmi_driver);
221718382eadSSubhransu S. Prusty }
221818382eadSSubhransu S. Prusty 
221918382eadSSubhransu S. Prusty module_init(hdmi_init);
222018382eadSSubhransu S. Prusty module_exit(hdmi_exit);
222118382eadSSubhransu S. Prusty 
222218382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2");
222318382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec");
222418382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
222518382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2226