xref: /openbmc/linux/sound/soc/codecs/hdac_hdmi.c (revision 3098f5eb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
4  *
5  *  Copyright (C) 2014-2015 Intel Corp
6  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
7  *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/hdmi.h>
17 #include <drm/drm_edid.h>
18 #include <sound/pcm_params.h>
19 #include <sound/jack.h>
20 #include <sound/soc.h>
21 #include <sound/hdaudio_ext.h>
22 #include <sound/hda_i915.h>
23 #include <sound/pcm_drm_eld.h>
24 #include <sound/hda_chmap.h>
25 #include "../../hda/local.h"
26 #include "hdac_hdmi.h"
27 
28 #define NAME_SIZE	32
29 
30 #define AMP_OUT_MUTE		0xb080
31 #define AMP_OUT_UNMUTE		0xb000
32 #define PIN_OUT			(AC_PINCTL_OUT_EN)
33 
34 #define HDA_MAX_CONNECTIONS     32
35 
36 #define HDA_MAX_CVTS		3
37 #define HDA_MAX_PORTS		3
38 
39 #define ELD_MAX_SIZE    256
40 #define ELD_FIXED_BYTES	20
41 
42 #define ELD_VER_CEA_861D 2
43 #define ELD_VER_PARTIAL 31
44 #define ELD_MAX_MNL     16
45 
46 struct hdac_hdmi_cvt_params {
47 	unsigned int channels_min;
48 	unsigned int channels_max;
49 	u32 rates;
50 	u64 formats;
51 	unsigned int maxbps;
52 };
53 
54 struct hdac_hdmi_cvt {
55 	struct list_head head;
56 	hda_nid_t nid;
57 	const char *name;
58 	struct hdac_hdmi_cvt_params params;
59 };
60 
61 /* Currently only spk_alloc, more to be added */
62 struct hdac_hdmi_parsed_eld {
63 	u8 spk_alloc;
64 };
65 
66 struct hdac_hdmi_eld {
67 	bool	monitor_present;
68 	bool	eld_valid;
69 	int	eld_size;
70 	char    eld_buffer[ELD_MAX_SIZE];
71 	struct	hdac_hdmi_parsed_eld info;
72 };
73 
74 struct hdac_hdmi_pin {
75 	struct list_head head;
76 	hda_nid_t nid;
77 	bool mst_capable;
78 	struct hdac_hdmi_port *ports;
79 	int num_ports;
80 	struct hdac_device *hdev;
81 };
82 
83 struct hdac_hdmi_port {
84 	struct list_head head;
85 	int id;
86 	struct hdac_hdmi_pin *pin;
87 	int num_mux_nids;
88 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
89 	struct hdac_hdmi_eld eld;
90 	const char *jack_pin;
91 	bool is_connect;
92 	struct snd_soc_dapm_context *dapm;
93 	const char *output_pin;
94 	struct work_struct dapm_work;
95 };
96 
97 struct hdac_hdmi_pcm {
98 	struct list_head head;
99 	int pcm_id;
100 	struct list_head port_list;
101 	struct hdac_hdmi_cvt *cvt;
102 	struct snd_soc_jack *jack;
103 	int stream_tag;
104 	int channels;
105 	int format;
106 	bool chmap_set;
107 	unsigned char chmap[8]; /* ALSA API channel-map */
108 	struct mutex lock;
109 	int jack_event;
110 };
111 
112 struct hdac_hdmi_dai_port_map {
113 	int dai_id;
114 	struct hdac_hdmi_port *port;
115 	struct hdac_hdmi_cvt *cvt;
116 };
117 
118 /*
119  * pin to port mapping table where the value indicate the pin number and
120  * the index indicate the port number with 1 base.
121  */
122 static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
123 
124 struct hdac_hdmi_drv_data {
125 	unsigned int vendor_nid;
126 	const int *port_map; /* pin to port mapping table */
127 	int port_num;
128 };
129 
130 struct hdac_hdmi_priv {
131 	struct hdac_device *hdev;
132 	struct snd_soc_component *component;
133 	struct snd_card *card;
134 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
135 	struct list_head pin_list;
136 	struct list_head cvt_list;
137 	struct list_head pcm_list;
138 	int num_pin;
139 	int num_cvt;
140 	int num_ports;
141 	struct mutex pin_mutex;
142 	struct hdac_chmap chmap;
143 	struct hdac_hdmi_drv_data *drv_data;
144 	struct snd_soc_dai_driver *dai_drv;
145 };
146 
147 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
148 
149 static struct hdac_hdmi_pcm *
150 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
151 			   struct hdac_hdmi_cvt *cvt)
152 {
153 	struct hdac_hdmi_pcm *pcm = NULL;
154 
155 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
156 		if (pcm->cvt == cvt)
157 			break;
158 	}
159 
160 	return pcm;
161 }
162 
163 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
164 		struct hdac_hdmi_port *port, bool is_connect)
165 {
166 	struct hdac_device *hdev = port->pin->hdev;
167 
168 	port->is_connect = is_connect;
169 	if (is_connect) {
170 		/*
171 		 * Report Jack connect event when a device is connected
172 		 * for the first time where same PCM is attached to multiple
173 		 * ports.
174 		 */
175 		if (pcm->jack_event == 0) {
176 			dev_dbg(&hdev->dev,
177 					"jack report for pcm=%d\n",
178 					pcm->pcm_id);
179 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
180 						SND_JACK_AVOUT);
181 		}
182 		pcm->jack_event++;
183 	} else {
184 		/*
185 		 * Report Jack disconnect event when a device is disconnected
186 		 * is the only last connected device when same PCM is attached
187 		 * to multiple ports.
188 		 */
189 		if (pcm->jack_event == 1)
190 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
191 		if (pcm->jack_event > 0)
192 			pcm->jack_event--;
193 	}
194 }
195 
196 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
197 {
198 	if (port->is_connect)
199 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
200 	else
201 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
202 	snd_soc_dapm_sync(port->dapm);
203 }
204 
205 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
206 {
207 	struct hdac_hdmi_port *port;
208 
209 	port = container_of(work, struct hdac_hdmi_port, dapm_work);
210 	hdac_hdmi_port_dapm_update(port);
211 }
212 
213 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
214 		struct hdac_hdmi_port *port, bool is_connect)
215 {
216 	hdac_hdmi_jack_report(pcm, port, is_connect);
217 	hdac_hdmi_port_dapm_update(port);
218 }
219 
220 /* MST supported verbs */
221 /*
222  * Get the no devices that can be connected to a port on the Pin widget.
223  */
224 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
225 {
226 	unsigned int caps;
227 	unsigned int type, param;
228 
229 	caps = get_wcaps(hdev, nid);
230 	type = get_wcaps_type(caps);
231 
232 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
233 		return 0;
234 
235 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
236 	if (param == -1)
237 		return param;
238 
239 	return param & AC_DEV_LIST_LEN_MASK;
240 }
241 
242 /*
243  * Get the port entry select on the pin. Return the port entry
244  * id selected on the pin. Return 0 means the first port entry
245  * is selected or MST is not supported.
246  */
247 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
248 					struct hdac_hdmi_port *port)
249 {
250 	return snd_hdac_codec_read(hdev, port->pin->nid,
251 				0, AC_VERB_GET_DEVICE_SEL, 0);
252 }
253 
254 /*
255  * Sets the selected port entry for the configuring Pin widget verb.
256  * returns error if port set is not equal to port get otherwise success
257  */
258 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
259 					struct hdac_hdmi_port *port)
260 {
261 	int num_ports;
262 
263 	if (!port->pin->mst_capable)
264 		return 0;
265 
266 	/* AC_PAR_DEVLIST_LEN is 0 based. */
267 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
268 	if (num_ports < 0)
269 		return -EIO;
270 	/*
271 	 * Device List Length is a 0 based integer value indicating the
272 	 * number of sink device that a MST Pin Widget can support.
273 	 */
274 	if (num_ports + 1  < port->id)
275 		return 0;
276 
277 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
278 			AC_VERB_SET_DEVICE_SEL, port->id);
279 
280 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
281 		return -EIO;
282 
283 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
284 
285 	return 0;
286 }
287 
288 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
289 						int pcm_idx)
290 {
291 	struct hdac_hdmi_pcm *pcm;
292 
293 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
294 		if (pcm->pcm_id == pcm_idx)
295 			return pcm;
296 	}
297 
298 	return NULL;
299 }
300 
301 static unsigned int sad_format(const u8 *sad)
302 {
303 	return ((sad[0] >> 0x3) & 0x1f);
304 }
305 
306 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
307 {
308 	return (sad[2] & 7);
309 }
310 
311 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
312 						void *eld)
313 {
314 	u64 formats = SNDRV_PCM_FMTBIT_S16;
315 	int i;
316 	const u8 *sad, *eld_buf = eld;
317 
318 	sad = drm_eld_sad(eld_buf);
319 	if (!sad)
320 		goto format_constraint;
321 
322 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
323 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
324 
325 			/*
326 			 * the controller support 20 and 24 bits in 32 bit
327 			 * container so we set S32
328 			 */
329 			if (sad_sample_bits_lpcm(sad) & 0x6)
330 				formats |= SNDRV_PCM_FMTBIT_S32;
331 		}
332 	}
333 
334 format_constraint:
335 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
336 				formats);
337 
338 }
339 
340 static void
341 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
342 				int packet_index, int byte_index)
343 {
344 	int val;
345 
346 	val = (packet_index << 5) | (byte_index & 0x1f);
347 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
348 }
349 
350 struct dp_audio_infoframe {
351 	u8 type; /* 0x84 */
352 	u8 len;  /* 0x1b */
353 	u8 ver;  /* 0x11 << 2 */
354 
355 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
356 	u8 SS01_SF24;
357 	u8 CXT04;
358 	u8 CA;
359 	u8 LFEPBL01_LSV36_DM_INH7;
360 };
361 
362 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
363 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
364 {
365 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
366 	struct hdmi_audio_infoframe frame;
367 	struct hdac_hdmi_pin *pin = port->pin;
368 	struct dp_audio_infoframe dp_ai;
369 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
370 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
371 	u8 *dip;
372 	int ret;
373 	int i;
374 	const u8 *eld_buf;
375 	u8 conn_type;
376 	int channels, ca;
377 
378 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
379 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
380 
381 	channels = snd_hdac_get_active_channels(ca);
382 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
383 
384 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
385 				pcm->channels, pcm->chmap, pcm->chmap_set);
386 
387 	eld_buf = port->eld.eld_buffer;
388 	conn_type = drm_eld_get_conn_type(eld_buf);
389 
390 	switch (conn_type) {
391 	case DRM_ELD_CONN_TYPE_HDMI:
392 		hdmi_audio_infoframe_init(&frame);
393 
394 		frame.channels = channels;
395 		frame.channel_allocation = ca;
396 
397 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
398 		if (ret < 0)
399 			return ret;
400 
401 		break;
402 
403 	case DRM_ELD_CONN_TYPE_DP:
404 		memset(&dp_ai, 0, sizeof(dp_ai));
405 		dp_ai.type	= 0x84;
406 		dp_ai.len	= 0x1b;
407 		dp_ai.ver	= 0x11 << 2;
408 		dp_ai.CC02_CT47	= channels - 1;
409 		dp_ai.CA	= ca;
410 
411 		dip = (u8 *)&dp_ai;
412 		break;
413 
414 	default:
415 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
416 		return -EIO;
417 	}
418 
419 	/* stop infoframe transmission */
420 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
421 	snd_hdac_codec_write(hdev, pin->nid, 0,
422 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
423 
424 
425 	/*  Fill infoframe. Index auto-incremented */
426 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
427 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
428 		for (i = 0; i < sizeof(buffer); i++)
429 			snd_hdac_codec_write(hdev, pin->nid, 0,
430 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
431 	} else {
432 		for (i = 0; i < sizeof(dp_ai); i++)
433 			snd_hdac_codec_write(hdev, pin->nid, 0,
434 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
435 	}
436 
437 	/* Start infoframe */
438 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
439 	snd_hdac_codec_write(hdev, pin->nid, 0,
440 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
441 
442 	return 0;
443 }
444 
445 static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
446 		unsigned int tx_mask, unsigned int rx_mask,
447 		int slots, int slot_width)
448 {
449 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
450 	struct hdac_device *hdev = hdmi->hdev;
451 	struct hdac_hdmi_dai_port_map *dai_map;
452 	struct hdac_hdmi_pcm *pcm;
453 
454 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
455 
456 	dai_map = &hdmi->dai_map[dai->id];
457 
458 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
459 
460 	if (pcm)
461 		pcm->stream_tag = (tx_mask << 4);
462 
463 	return 0;
464 }
465 
466 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
467 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
468 {
469 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
470 	struct hdac_hdmi_dai_port_map *dai_map;
471 	struct hdac_hdmi_pcm *pcm;
472 	int format;
473 
474 	dai_map = &hdmi->dai_map[dai->id];
475 
476 	format = snd_hdac_calc_stream_format(params_rate(hparams),
477 			params_channels(hparams), params_format(hparams),
478 			dai->driver->playback.sig_bits, 0);
479 
480 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
481 	if (!pcm)
482 		return -EIO;
483 
484 	pcm->format = format;
485 	pcm->channels = params_channels(hparams);
486 
487 	return 0;
488 }
489 
490 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
491 					struct hdac_hdmi_pin *pin,
492 					struct hdac_hdmi_port *port)
493 {
494 	if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
495 		dev_warn(&hdev->dev,
496 			"HDMI: pin %d wcaps %#x does not support connection list\n",
497 			pin->nid, get_wcaps(hdev, pin->nid));
498 		return -EINVAL;
499 	}
500 
501 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
502 		return -EIO;
503 
504 	port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
505 			port->mux_nids, HDA_MAX_CONNECTIONS);
506 	if (port->num_mux_nids == 0)
507 		dev_warn(&hdev->dev,
508 			"No connections found for pin:port %d:%d\n",
509 						pin->nid, port->id);
510 
511 	dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
512 			port->num_mux_nids, pin->nid, port->id);
513 
514 	return port->num_mux_nids;
515 }
516 
517 /*
518  * Query pcm list and return port to which stream is routed.
519  *
520  * Also query connection list of the pin, to validate the cvt to port map.
521  *
522  * Same stream rendering to multiple ports simultaneously can be done
523  * possibly, but not supported for now in driver. So return the first port
524  * connected.
525  */
526 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
527 			struct hdac_device *hdev,
528 			struct hdac_hdmi_priv *hdmi,
529 			struct hdac_hdmi_cvt *cvt)
530 {
531 	struct hdac_hdmi_pcm *pcm;
532 	struct hdac_hdmi_port *port = NULL;
533 	int ret, i;
534 
535 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
536 		if (pcm->cvt == cvt) {
537 			if (list_empty(&pcm->port_list))
538 				continue;
539 
540 			list_for_each_entry(port, &pcm->port_list, head) {
541 				mutex_lock(&pcm->lock);
542 				ret = hdac_hdmi_query_port_connlist(hdev,
543 							port->pin, port);
544 				mutex_unlock(&pcm->lock);
545 				if (ret < 0)
546 					continue;
547 
548 				for (i = 0; i < port->num_mux_nids; i++) {
549 					if (port->mux_nids[i] == cvt->nid &&
550 						port->eld.monitor_present &&
551 						port->eld.eld_valid)
552 						return port;
553 				}
554 			}
555 		}
556 	}
557 
558 	return NULL;
559 }
560 
561 /*
562  * Go through all converters and ensure connection is set to
563  * the correct pin as set via kcontrols.
564  */
565 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
566 {
567 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
568 	struct hdac_hdmi_port *port;
569 	struct hdac_hdmi_cvt *cvt;
570 	int cvt_idx = 0;
571 
572 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
573 		port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
574 		if (port && port->pin) {
575 			snd_hdac_codec_write(hdev, port->pin->nid, 0,
576 					     AC_VERB_SET_CONNECT_SEL, cvt_idx);
577 			dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
578 				__func__, cvt->name, port->pin->nid, cvt_idx);
579 		}
580 		++cvt_idx;
581 	}
582 }
583 
584 /*
585  * This tries to get a valid pin and set the HW constraints based on the
586  * ELD. Even if a valid pin is not found return success so that device open
587  * doesn't fail.
588  */
589 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
590 			struct snd_soc_dai *dai)
591 {
592 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
593 	struct hdac_device *hdev = hdmi->hdev;
594 	struct hdac_hdmi_dai_port_map *dai_map;
595 	struct hdac_hdmi_cvt *cvt;
596 	struct hdac_hdmi_port *port;
597 	int ret;
598 
599 	dai_map = &hdmi->dai_map[dai->id];
600 
601 	cvt = dai_map->cvt;
602 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
603 
604 	/*
605 	 * To make PA and other userland happy.
606 	 * userland scans devices so returning error does not help.
607 	 */
608 	if (!port)
609 		return 0;
610 	if ((!port->eld.monitor_present) ||
611 			(!port->eld.eld_valid)) {
612 
613 		dev_warn(&hdev->dev,
614 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
615 			port->eld.monitor_present, port->eld.eld_valid,
616 			port->pin->nid, port->id);
617 
618 		return 0;
619 	}
620 
621 	dai_map->port = port;
622 
623 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
624 				port->eld.eld_buffer);
625 	if (ret < 0)
626 		return ret;
627 
628 	return snd_pcm_hw_constraint_eld(substream->runtime,
629 				port->eld.eld_buffer);
630 }
631 
632 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
633 		struct snd_soc_dai *dai)
634 {
635 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
636 	struct hdac_hdmi_dai_port_map *dai_map;
637 	struct hdac_hdmi_pcm *pcm;
638 
639 	dai_map = &hdmi->dai_map[dai->id];
640 
641 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
642 
643 	if (pcm) {
644 		mutex_lock(&pcm->lock);
645 		pcm->chmap_set = false;
646 		memset(pcm->chmap, 0, sizeof(pcm->chmap));
647 		pcm->channels = 0;
648 		mutex_unlock(&pcm->lock);
649 	}
650 
651 	if (dai_map->port)
652 		dai_map->port = NULL;
653 }
654 
655 static int
656 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
657 {
658 	unsigned int chans;
659 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
660 	int err;
661 
662 	chans = get_wcaps(hdev, cvt->nid);
663 	chans = get_wcaps_channels(chans);
664 
665 	cvt->params.channels_min = 2;
666 
667 	cvt->params.channels_max = chans;
668 	if (chans > hdmi->chmap.channels_max)
669 		hdmi->chmap.channels_max = chans;
670 
671 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
672 			&cvt->params.rates,
673 			&cvt->params.formats,
674 			&cvt->params.maxbps);
675 	if (err < 0)
676 		dev_err(&hdev->dev,
677 			"Failed to query pcm params for nid %d: %d\n",
678 			cvt->nid, err);
679 
680 	return err;
681 }
682 
683 static int hdac_hdmi_fill_widget_info(struct device *dev,
684 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
685 		void *priv, const char *wname, const char *stream,
686 		struct snd_kcontrol_new *wc, int numkc,
687 		int (*event)(struct snd_soc_dapm_widget *,
688 		struct snd_kcontrol *, int), unsigned short event_flags)
689 {
690 	w->id = id;
691 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
692 	if (!w->name)
693 		return -ENOMEM;
694 
695 	w->sname = stream;
696 	w->reg = SND_SOC_NOPM;
697 	w->shift = 0;
698 	w->kcontrol_news = wc;
699 	w->num_kcontrols = numkc;
700 	w->priv = priv;
701 	w->event = event;
702 	w->event_flags = event_flags;
703 
704 	return 0;
705 }
706 
707 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
708 		const char *sink, const char *control, const char *src,
709 		int (*handler)(struct snd_soc_dapm_widget *src,
710 			struct snd_soc_dapm_widget *sink))
711 {
712 	route->sink = sink;
713 	route->source = src;
714 	route->control = control;
715 	route->connected = handler;
716 }
717 
718 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
719 					struct hdac_hdmi_port *port)
720 {
721 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
722 	struct hdac_hdmi_pcm *pcm = NULL;
723 	struct hdac_hdmi_port *p;
724 
725 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
726 		if (list_empty(&pcm->port_list))
727 			continue;
728 
729 		list_for_each_entry(p, &pcm->port_list, head) {
730 			if (p->id == port->id && port->pin == p->pin)
731 				return pcm;
732 		}
733 	}
734 
735 	return NULL;
736 }
737 
738 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
739 			     hda_nid_t nid, unsigned int pwr_state)
740 {
741 	int count;
742 	unsigned int state;
743 
744 	if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
745 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
746 			for (count = 0; count < 10; count++) {
747 				snd_hdac_codec_read(hdev, nid, 0,
748 						AC_VERB_SET_POWER_STATE,
749 						pwr_state);
750 				state = snd_hdac_sync_power_state(hdev,
751 						nid, pwr_state);
752 				if (!(state & AC_PWRST_ERROR))
753 					break;
754 			}
755 		}
756 	}
757 }
758 
759 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
760 				   hda_nid_t nid, int val)
761 {
762 	if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
763 		snd_hdac_codec_write(hdev, nid, 0,
764 					AC_VERB_SET_AMP_GAIN_MUTE, val);
765 }
766 
767 
768 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
769 					struct snd_kcontrol *kc, int event)
770 {
771 	struct hdac_hdmi_port *port = w->priv;
772 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
773 	struct hdac_hdmi_pcm *pcm;
774 
775 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
776 			__func__, w->name, event);
777 
778 	pcm = hdac_hdmi_get_pcm(hdev, port);
779 	if (!pcm)
780 		return -EIO;
781 
782 	/* set the device if pin is mst_capable */
783 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
784 		return -EIO;
785 
786 	switch (event) {
787 	case SND_SOC_DAPM_PRE_PMU:
788 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
789 
790 		/* Enable out path for this pin widget */
791 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
792 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
793 
794 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
795 
796 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
797 
798 	case SND_SOC_DAPM_POST_PMD:
799 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
800 
801 		/* Disable out path for this pin widget */
802 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
803 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
804 
805 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
806 		break;
807 
808 	}
809 
810 	return 0;
811 }
812 
813 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
814 					struct snd_kcontrol *kc, int event)
815 {
816 	struct hdac_hdmi_cvt *cvt = w->priv;
817 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
818 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
819 	struct hdac_hdmi_pcm *pcm;
820 
821 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
822 			__func__, w->name, event);
823 
824 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
825 	if (!pcm)
826 		return -EIO;
827 
828 	switch (event) {
829 	case SND_SOC_DAPM_PRE_PMU:
830 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
831 
832 		/* Enable transmission */
833 		snd_hdac_codec_write(hdev, cvt->nid, 0,
834 			AC_VERB_SET_DIGI_CONVERT_1, 1);
835 
836 		/* Category Code (CC) to zero */
837 		snd_hdac_codec_write(hdev, cvt->nid, 0,
838 			AC_VERB_SET_DIGI_CONVERT_2, 0);
839 
840 		snd_hdac_codec_write(hdev, cvt->nid, 0,
841 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
842 		snd_hdac_codec_write(hdev, cvt->nid, 0,
843 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
844 
845 		/*
846 		 * The connection indices are shared by all converters and
847 		 * may interfere with each other. Ensure correct
848 		 * routing for all converters at stream start.
849 		 */
850 		hdac_hdmi_verify_connect_sel_all_pins(hdev);
851 
852 		break;
853 
854 	case SND_SOC_DAPM_POST_PMD:
855 		snd_hdac_codec_write(hdev, cvt->nid, 0,
856 				AC_VERB_SET_CHANNEL_STREAMID, 0);
857 		snd_hdac_codec_write(hdev, cvt->nid, 0,
858 				AC_VERB_SET_STREAM_FORMAT, 0);
859 
860 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
861 		break;
862 
863 	}
864 
865 	return 0;
866 }
867 
868 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
869 					struct snd_kcontrol *kc, int event)
870 {
871 	struct hdac_hdmi_port *port = w->priv;
872 	struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
873 	int mux_idx;
874 
875 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
876 			__func__, w->name, event);
877 
878 	if (!kc)
879 		kc  = w->kcontrols[0];
880 
881 	mux_idx = dapm_kcontrol_get_value(kc);
882 
883 	/* set the device if pin is mst_capable */
884 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
885 		return -EIO;
886 
887 	if (mux_idx > 0) {
888 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
889 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
890 	}
891 
892 	return 0;
893 }
894 
895 /*
896  * Based on user selection, map the PINs with the PCMs.
897  */
898 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
899 		struct snd_ctl_elem_value *ucontrol)
900 {
901 	int ret;
902 	struct hdac_hdmi_port *p, *p_next;
903 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
904 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
905 	struct snd_soc_dapm_context *dapm = w->dapm;
906 	struct hdac_hdmi_port *port = w->priv;
907 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
908 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
909 	struct hdac_hdmi_pcm *pcm = NULL;
910 	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
911 
912 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
913 	if (ret < 0)
914 		return ret;
915 
916 	if (port == NULL)
917 		return -EINVAL;
918 
919 	mutex_lock(&hdmi->pin_mutex);
920 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
921 		if (list_empty(&pcm->port_list))
922 			continue;
923 
924 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
925 			if (p == port && p->id == port->id &&
926 					p->pin == port->pin) {
927 				hdac_hdmi_jack_report_sync(pcm, port, false);
928 				list_del(&p->head);
929 			}
930 		}
931 	}
932 
933 	/*
934 	 * Jack status is not reported during device probe as the
935 	 * PCMs are not registered by then. So report it here.
936 	 */
937 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
938 		if (!strcmp(cvt_name, pcm->cvt->name)) {
939 			list_add_tail(&port->head, &pcm->port_list);
940 			if (port->eld.monitor_present && port->eld.eld_valid) {
941 				hdac_hdmi_jack_report_sync(pcm, port, true);
942 				mutex_unlock(&hdmi->pin_mutex);
943 				return ret;
944 			}
945 		}
946 	}
947 	mutex_unlock(&hdmi->pin_mutex);
948 
949 	return ret;
950 }
951 
952 /*
953  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
954  * the display driver seem to be programming the connection list for the pin
955  * widget runtime.
956  *
957  * So programming all the possible inputs for the mux, the user has to take
958  * care of selecting the right one and leaving all other inputs selected to
959  * "NONE"
960  */
961 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
962 				struct hdac_hdmi_port *port,
963 				struct snd_soc_dapm_widget *widget,
964 				const char *widget_name)
965 {
966 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
967 	struct hdac_hdmi_pin *pin = port->pin;
968 	struct snd_kcontrol_new *kc;
969 	struct hdac_hdmi_cvt *cvt;
970 	struct soc_enum *se;
971 	char kc_name[NAME_SIZE];
972 	char mux_items[NAME_SIZE];
973 	/* To hold inputs to the Pin mux */
974 	char *items[HDA_MAX_CONNECTIONS];
975 	int i = 0;
976 	int num_items = hdmi->num_cvt + 1;
977 
978 	kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
979 	if (!kc)
980 		return -ENOMEM;
981 
982 	se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
983 	if (!se)
984 		return -ENOMEM;
985 
986 	snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
987 						pin->nid, port->id);
988 	kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
989 	if (!kc->name)
990 		return -ENOMEM;
991 
992 	kc->private_value = (long)se;
993 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
994 	kc->access = 0;
995 	kc->info = snd_soc_info_enum_double;
996 	kc->put = hdac_hdmi_set_pin_port_mux;
997 	kc->get = snd_soc_dapm_get_enum_double;
998 
999 	se->reg = SND_SOC_NOPM;
1000 
1001 	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
1002 	se->items = num_items;
1003 	se->mask = roundup_pow_of_two(se->items) - 1;
1004 
1005 	sprintf(mux_items, "NONE");
1006 	items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1007 	if (!items[i])
1008 		return -ENOMEM;
1009 
1010 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1011 		i++;
1012 		sprintf(mux_items, "cvt %d", cvt->nid);
1013 		items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1014 		if (!items[i])
1015 			return -ENOMEM;
1016 	}
1017 
1018 	se->texts = devm_kmemdup(&hdev->dev, items,
1019 			(num_items  * sizeof(char *)), GFP_KERNEL);
1020 	if (!se->texts)
1021 		return -ENOMEM;
1022 
1023 	return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1024 			snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1025 			hdac_hdmi_pin_mux_widget_event,
1026 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
1027 }
1028 
1029 /* Add cvt <- input <- mux route map */
1030 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
1031 			struct snd_soc_dapm_widget *widgets,
1032 			struct snd_soc_dapm_route *route, int rindex)
1033 {
1034 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1035 	const struct snd_kcontrol_new *kc;
1036 	struct soc_enum *se;
1037 	int mux_index = hdmi->num_cvt + hdmi->num_ports;
1038 	int i, j;
1039 
1040 	for (i = 0; i < hdmi->num_ports; i++) {
1041 		kc = widgets[mux_index].kcontrol_news;
1042 		se = (struct soc_enum *)kc->private_value;
1043 		for (j = 0; j < hdmi->num_cvt; j++) {
1044 			hdac_hdmi_fill_route(&route[rindex],
1045 					widgets[mux_index].name,
1046 					se->texts[j + 1],
1047 					widgets[j].name, NULL);
1048 
1049 			rindex++;
1050 		}
1051 
1052 		mux_index++;
1053 	}
1054 }
1055 
1056 /*
1057  * Widgets are added in the below sequence
1058  *	Converter widgets for num converters enumerated
1059  *	Pin-port widgets for num ports for Pins enumerated
1060  *	Pin-port mux widgets to represent connenction list of pin widget
1061  *
1062  * For each port, one Mux and One output widget is added
1063  * Total widgets elements = num_cvt + (num_ports * 2);
1064  *
1065  * Routes are added as below:
1066  *	pin-port mux -> pin (based on num_ports)
1067  *	cvt -> "Input sel control" -> pin-port_mux
1068  *
1069  * Total route elements:
1070  *	num_ports + (pin_muxes * num_cvt)
1071  */
1072 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1073 {
1074 	struct snd_soc_dapm_widget *widgets;
1075 	struct snd_soc_dapm_route *route;
1076 	struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1077 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1078 	struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1079 	char widget_name[NAME_SIZE];
1080 	struct hdac_hdmi_cvt *cvt;
1081 	struct hdac_hdmi_pin *pin;
1082 	int ret, i = 0, num_routes = 0, j;
1083 
1084 	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1085 		return -EINVAL;
1086 
1087 	widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1088 				((2 * hdmi->num_ports) + hdmi->num_cvt)),
1089 				GFP_KERNEL);
1090 
1091 	if (!widgets)
1092 		return -ENOMEM;
1093 
1094 	/* DAPM widgets to represent each converter widget */
1095 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1096 		sprintf(widget_name, "Converter %d", cvt->nid);
1097 		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1098 			snd_soc_dapm_aif_in, cvt,
1099 			widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1100 			hdac_hdmi_cvt_output_widget_event,
1101 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1102 		if (ret < 0)
1103 			return ret;
1104 		i++;
1105 	}
1106 
1107 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1108 		for (j = 0; j < pin->num_ports; j++) {
1109 			sprintf(widget_name, "hif%d-%d Output",
1110 				pin->nid, pin->ports[j].id);
1111 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1112 					snd_soc_dapm_output, &pin->ports[j],
1113 					widget_name, NULL, NULL, 0,
1114 					hdac_hdmi_pin_output_widget_event,
1115 					SND_SOC_DAPM_PRE_PMU |
1116 					SND_SOC_DAPM_POST_PMD);
1117 			if (ret < 0)
1118 				return ret;
1119 			pin->ports[j].output_pin = widgets[i].name;
1120 			i++;
1121 		}
1122 	}
1123 
1124 	/* DAPM widgets to represent the connection list to pin widget */
1125 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1126 		for (j = 0; j < pin->num_ports; j++) {
1127 			sprintf(widget_name, "Pin%d-Port%d Mux",
1128 				pin->nid, pin->ports[j].id);
1129 			ret = hdac_hdmi_create_pin_port_muxs(hdev,
1130 						&pin->ports[j], &widgets[i],
1131 						widget_name);
1132 			if (ret < 0)
1133 				return ret;
1134 			i++;
1135 
1136 			/* For cvt to pin_mux mapping */
1137 			num_routes += hdmi->num_cvt;
1138 
1139 			/* For pin_mux to pin mapping */
1140 			num_routes++;
1141 		}
1142 	}
1143 
1144 	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1145 							GFP_KERNEL);
1146 	if (!route)
1147 		return -ENOMEM;
1148 
1149 	i = 0;
1150 	/* Add pin <- NULL <- mux route map */
1151 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1152 		for (j = 0; j < pin->num_ports; j++) {
1153 			int sink_index = i + hdmi->num_cvt;
1154 			int src_index = sink_index + pin->num_ports *
1155 						hdmi->num_pin;
1156 
1157 			hdac_hdmi_fill_route(&route[i],
1158 				widgets[sink_index].name, NULL,
1159 				widgets[src_index].name, NULL);
1160 			i++;
1161 		}
1162 	}
1163 
1164 	hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1165 
1166 	snd_soc_dapm_new_controls(dapm, widgets,
1167 		((2 * hdmi->num_ports) + hdmi->num_cvt));
1168 
1169 	snd_soc_dapm_add_routes(dapm, route, num_routes);
1170 	snd_soc_dapm_new_widgets(dapm->card);
1171 
1172 	return 0;
1173 
1174 }
1175 
1176 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1177 {
1178 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1179 	struct hdac_hdmi_dai_port_map *dai_map;
1180 	struct hdac_hdmi_cvt *cvt;
1181 	int dai_id = 0;
1182 
1183 	if (list_empty(&hdmi->cvt_list))
1184 		return -EINVAL;
1185 
1186 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1187 		dai_map = &hdmi->dai_map[dai_id];
1188 		dai_map->dai_id = dai_id;
1189 		dai_map->cvt = cvt;
1190 
1191 		dai_id++;
1192 
1193 		if (dai_id == HDA_MAX_CVTS) {
1194 			dev_warn(&hdev->dev,
1195 				"Max dais supported: %d\n", dai_id);
1196 			break;
1197 		}
1198 	}
1199 
1200 	return 0;
1201 }
1202 
1203 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1204 {
1205 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1206 	struct hdac_hdmi_cvt *cvt;
1207 	char name[NAME_SIZE];
1208 
1209 	cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1210 	if (!cvt)
1211 		return -ENOMEM;
1212 
1213 	cvt->nid = nid;
1214 	sprintf(name, "cvt %d", cvt->nid);
1215 	cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1216 	if (!cvt->name)
1217 		return -ENOMEM;
1218 
1219 	list_add_tail(&cvt->head, &hdmi->cvt_list);
1220 	hdmi->num_cvt++;
1221 
1222 	return hdac_hdmi_query_cvt_params(hdev, cvt);
1223 }
1224 
1225 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1226 			struct hdac_hdmi_port *port)
1227 {
1228 	unsigned int ver, mnl;
1229 
1230 	ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1231 						>> DRM_ELD_VER_SHIFT;
1232 
1233 	if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1234 		dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1235 		return -EINVAL;
1236 	}
1237 
1238 	mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1239 		DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1240 
1241 	if (mnl > ELD_MAX_MNL) {
1242 		dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1243 		return -EINVAL;
1244 	}
1245 
1246 	port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1247 
1248 	return 0;
1249 }
1250 
1251 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1252 				    struct hdac_hdmi_port *port)
1253 {
1254 	struct hdac_device *hdev = pin->hdev;
1255 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1256 	struct hdac_hdmi_pcm *pcm;
1257 	int size = 0;
1258 	int port_id = -1;
1259 
1260 	if (!hdmi)
1261 		return;
1262 
1263 	/*
1264 	 * In case of non MST pin, get_eld info API expectes port
1265 	 * to be -1.
1266 	 */
1267 	mutex_lock(&hdmi->pin_mutex);
1268 	port->eld.monitor_present = false;
1269 
1270 	if (pin->mst_capable)
1271 		port_id = port->id;
1272 
1273 	size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1274 				&port->eld.monitor_present,
1275 				port->eld.eld_buffer,
1276 				ELD_MAX_SIZE);
1277 
1278 	if (size > 0) {
1279 		size = min(size, ELD_MAX_SIZE);
1280 		if (hdac_hdmi_parse_eld(hdev, port) < 0)
1281 			size = -EINVAL;
1282 	}
1283 
1284 	if (size > 0) {
1285 		port->eld.eld_valid = true;
1286 		port->eld.eld_size = size;
1287 	} else {
1288 		port->eld.eld_valid = false;
1289 		port->eld.eld_size = 0;
1290 	}
1291 
1292 	pcm = hdac_hdmi_get_pcm(hdev, port);
1293 
1294 	if (!port->eld.monitor_present || !port->eld.eld_valid) {
1295 
1296 		dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1297 						__func__, pin->nid, port->id);
1298 
1299 		/*
1300 		 * PCMs are not registered during device probe, so don't
1301 		 * report jack here. It will be done in usermode mux
1302 		 * control select.
1303 		 */
1304 		if (pcm) {
1305 			hdac_hdmi_jack_report(pcm, port, false);
1306 			schedule_work(&port->dapm_work);
1307 		}
1308 
1309 		mutex_unlock(&hdmi->pin_mutex);
1310 		return;
1311 	}
1312 
1313 	if (port->eld.monitor_present && port->eld.eld_valid) {
1314 		if (pcm) {
1315 			hdac_hdmi_jack_report(pcm, port, true);
1316 			schedule_work(&port->dapm_work);
1317 		}
1318 
1319 		print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1320 			  port->eld.eld_buffer, port->eld.eld_size, false);
1321 
1322 	}
1323 	mutex_unlock(&hdmi->pin_mutex);
1324 }
1325 
1326 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1327 			       struct hdac_hdmi_pin *pin)
1328 {
1329 	struct hdac_hdmi_port *ports;
1330 	int max_ports = HDA_MAX_PORTS;
1331 	int i;
1332 
1333 	/*
1334 	 * FIXME: max_port may vary for each platform, so pass this as
1335 	 * as driver data or query from i915 interface when this API is
1336 	 * implemented.
1337 	 */
1338 
1339 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1340 	if (!ports)
1341 		return -ENOMEM;
1342 
1343 	for (i = 0; i < max_ports; i++) {
1344 		ports[i].id = i;
1345 		ports[i].pin = pin;
1346 		INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1347 	}
1348 	pin->ports = ports;
1349 	pin->num_ports = max_ports;
1350 	return 0;
1351 }
1352 
1353 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1354 {
1355 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1356 	struct hdac_hdmi_pin *pin;
1357 	int ret;
1358 
1359 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1360 	if (!pin)
1361 		return -ENOMEM;
1362 
1363 	pin->nid = nid;
1364 	pin->mst_capable = false;
1365 	pin->hdev = hdev;
1366 	ret = hdac_hdmi_add_ports(hdev, pin);
1367 	if (ret < 0)
1368 		return ret;
1369 
1370 	list_add_tail(&pin->head, &hdmi->pin_list);
1371 	hdmi->num_pin++;
1372 	hdmi->num_ports += pin->num_ports;
1373 
1374 	return 0;
1375 }
1376 
1377 #define INTEL_VENDOR_NID_0x2 0x02
1378 #define INTEL_VENDOR_NID_0x8 0x08
1379 #define INTEL_VENDOR_NID_0xb 0x0b
1380 #define INTEL_GET_VENDOR_VERB 0xf81
1381 #define INTEL_SET_VENDOR_VERB 0x781
1382 #define INTEL_EN_DP12		0x02 /* enable DP 1.2 features */
1383 #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1384 
1385 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1386 {
1387 	unsigned int vendor_param;
1388 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1389 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1390 
1391 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1392 				INTEL_GET_VENDOR_VERB, 0);
1393 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1394 		return;
1395 
1396 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1397 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1398 				INTEL_SET_VENDOR_VERB, vendor_param);
1399 	if (vendor_param == -1)
1400 		return;
1401 }
1402 
1403 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1404 {
1405 	unsigned int vendor_param;
1406 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1407 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1408 
1409 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1410 				INTEL_GET_VENDOR_VERB, 0);
1411 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1412 		return;
1413 
1414 	/* enable DP1.2 mode */
1415 	vendor_param |= INTEL_EN_DP12;
1416 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1417 				INTEL_SET_VENDOR_VERB, vendor_param);
1418 	if (vendor_param == -1)
1419 		return;
1420 
1421 }
1422 
1423 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1424 	.startup = hdac_hdmi_pcm_open,
1425 	.shutdown = hdac_hdmi_pcm_close,
1426 	.hw_params = hdac_hdmi_set_hw_params,
1427 	.set_tdm_slot = hdac_hdmi_set_tdm_slot,
1428 };
1429 
1430 /*
1431  * Each converter can support a stream independently. So a dai is created
1432  * based on the number of converter queried.
1433  */
1434 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1435 		struct snd_soc_dai_driver **dais,
1436 		struct hdac_hdmi_priv *hdmi, int num_dais)
1437 {
1438 	struct snd_soc_dai_driver *hdmi_dais;
1439 	struct hdac_hdmi_cvt *cvt;
1440 	char name[NAME_SIZE], dai_name[NAME_SIZE];
1441 	int i = 0;
1442 	u32 rates, bps;
1443 	unsigned int rate_max = 384000, rate_min = 8000;
1444 	u64 formats;
1445 	int ret;
1446 
1447 	hdmi_dais = devm_kzalloc(&hdev->dev,
1448 			(sizeof(*hdmi_dais) * num_dais),
1449 			GFP_KERNEL);
1450 	if (!hdmi_dais)
1451 		return -ENOMEM;
1452 
1453 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1454 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1455 					&rates,	&formats, &bps);
1456 		if (ret)
1457 			return ret;
1458 
1459 		/* Filter out 44.1, 88.2 and 176.4Khz */
1460 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1461 			   SNDRV_PCM_RATE_176400);
1462 		if (!rates)
1463 			return -EINVAL;
1464 
1465 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1466 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1467 					dai_name, GFP_KERNEL);
1468 
1469 		if (!hdmi_dais[i].name)
1470 			return -ENOMEM;
1471 
1472 		snprintf(name, sizeof(name), "hifi%d", i+1);
1473 		hdmi_dais[i].playback.stream_name =
1474 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1475 		if (!hdmi_dais[i].playback.stream_name)
1476 			return -ENOMEM;
1477 
1478 		/*
1479 		 * Set caps based on capability queried from the converter.
1480 		 * It will be constrained runtime based on ELD queried.
1481 		 */
1482 		hdmi_dais[i].playback.formats = formats;
1483 		hdmi_dais[i].playback.rates = rates;
1484 		hdmi_dais[i].playback.rate_max = rate_max;
1485 		hdmi_dais[i].playback.rate_min = rate_min;
1486 		hdmi_dais[i].playback.channels_min = 2;
1487 		hdmi_dais[i].playback.channels_max = 2;
1488 		hdmi_dais[i].playback.sig_bits = bps;
1489 		hdmi_dais[i].ops = &hdmi_dai_ops;
1490 		i++;
1491 	}
1492 
1493 	*dais = hdmi_dais;
1494 	hdmi->dai_drv = hdmi_dais;
1495 
1496 	return 0;
1497 }
1498 
1499 /*
1500  * Parse all nodes and store the cvt/pin nids in array
1501  * Add one time initialization for pin and cvt widgets
1502  */
1503 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1504 		struct snd_soc_dai_driver **dais, int *num_dais)
1505 {
1506 	hda_nid_t nid;
1507 	int i, num_nodes;
1508 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1509 	int ret;
1510 
1511 	hdac_hdmi_skl_enable_all_pins(hdev);
1512 	hdac_hdmi_skl_enable_dp12(hdev);
1513 
1514 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1515 	if (!nid || num_nodes <= 0) {
1516 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1517 		return -EINVAL;
1518 	}
1519 
1520 	for (i = 0; i < num_nodes; i++, nid++) {
1521 		unsigned int caps;
1522 		unsigned int type;
1523 
1524 		caps = get_wcaps(hdev, nid);
1525 		type = get_wcaps_type(caps);
1526 
1527 		if (!(caps & AC_WCAP_DIGITAL))
1528 			continue;
1529 
1530 		switch (type) {
1531 
1532 		case AC_WID_AUD_OUT:
1533 			ret = hdac_hdmi_add_cvt(hdev, nid);
1534 			if (ret < 0)
1535 				return ret;
1536 			break;
1537 
1538 		case AC_WID_PIN:
1539 			ret = hdac_hdmi_add_pin(hdev, nid);
1540 			if (ret < 0)
1541 				return ret;
1542 			break;
1543 		}
1544 	}
1545 
1546 	if (!hdmi->num_pin || !hdmi->num_cvt) {
1547 		ret = -EIO;
1548 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1549 		return ret;
1550 	}
1551 
1552 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1553 	if (ret) {
1554 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1555 			ret);
1556 		return ret;
1557 	}
1558 
1559 	*num_dais = hdmi->num_cvt;
1560 	ret = hdac_hdmi_init_dai_map(hdev);
1561 	if (ret < 0)
1562 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1563 			ret);
1564 	return ret;
1565 }
1566 
1567 static int hdac_hdmi_pin2port(void *aptr, int pin)
1568 {
1569 	struct hdac_device *hdev = aptr;
1570 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1571 	const int *map = hdmi->drv_data->port_map;
1572 	int i;
1573 
1574 	if (!hdmi->drv_data->port_num)
1575 		return pin - 4; /* map NID 0x05 -> port #1 */
1576 
1577 	/*
1578 	 * looking for the pin number in the mapping table and return
1579 	 * the index which indicate the port number
1580 	 */
1581 	for (i = 0; i < hdmi->drv_data->port_num; i++) {
1582 		if (pin == map[i])
1583 			return i + 1;
1584 	}
1585 
1586 	/* return -1 if pin number exceeds our expectation */
1587 	dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1588 	return -1;
1589 }
1590 
1591 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1592 {
1593 	struct hdac_device *hdev = aptr;
1594 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1595 	struct hdac_hdmi_pin *pin = NULL;
1596 	struct hdac_hdmi_port *hport = NULL;
1597 	struct snd_soc_component *component = hdmi->component;
1598 	int i;
1599 	hda_nid_t pin_nid;
1600 
1601 	if (!hdmi->drv_data->port_num) {
1602 		/* for legacy platforms */
1603 		pin_nid = port + 0x04;
1604 	} else if (port < hdmi->drv_data->port_num) {
1605 		/* get pin number from the pin2port mapping table */
1606 		pin_nid = hdmi->drv_data->port_map[port - 1];
1607 	} else {
1608 		dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1609 		return;
1610 	}
1611 
1612 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1613 							pin_nid, pipe);
1614 
1615 	/*
1616 	 * skip notification during system suspend (but not in runtime PM);
1617 	 * the state will be updated at resume. Also since the ELD and
1618 	 * connection states are updated in anyway at the end of the resume,
1619 	 * we can skip it when received during PM process.
1620 	 */
1621 	if (snd_power_get_state(component->card->snd_card) !=
1622 			SNDRV_CTL_POWER_D0)
1623 		return;
1624 
1625 	if (atomic_read(&hdev->in_pm))
1626 		return;
1627 
1628 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1629 		if (pin->nid != pin_nid)
1630 			continue;
1631 
1632 		/* In case of non MST pin, pipe is -1 */
1633 		if (pipe == -1) {
1634 			pin->mst_capable = false;
1635 			/* if not MST, default is port[0] */
1636 			hport = &pin->ports[0];
1637 		} else {
1638 			for (i = 0; i < pin->num_ports; i++) {
1639 				pin->mst_capable = true;
1640 				if (pin->ports[i].id == pipe) {
1641 					hport = &pin->ports[i];
1642 					break;
1643 				}
1644 			}
1645 		}
1646 
1647 		if (hport)
1648 			hdac_hdmi_present_sense(pin, hport);
1649 	}
1650 
1651 }
1652 
1653 static struct drm_audio_component_audio_ops aops = {
1654 	.pin2port	= hdac_hdmi_pin2port,
1655 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1656 };
1657 
1658 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1659 						int device)
1660 {
1661 	struct snd_soc_pcm_runtime *rtd;
1662 
1663 	for_each_card_rtds(card, rtd) {
1664 		if (rtd->pcm && (rtd->pcm->device == device))
1665 			return rtd->pcm;
1666 	}
1667 
1668 	return NULL;
1669 }
1670 
1671 /* create jack pin kcontrols */
1672 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1673 				    struct hdac_device *hdev)
1674 {
1675 	struct hdac_hdmi_pin *pin;
1676 	struct snd_kcontrol_new *kc;
1677 	char kc_name[NAME_SIZE], xname[NAME_SIZE];
1678 	char *name;
1679 	int i = 0, j;
1680 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1681 	struct snd_soc_component *component = hdmi->component;
1682 
1683 	kc = devm_kcalloc(component->dev, hdmi->num_ports,
1684 				sizeof(*kc), GFP_KERNEL);
1685 
1686 	if (!kc)
1687 		return -ENOMEM;
1688 
1689 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1690 		for (j = 0; j < pin->num_ports; j++) {
1691 			snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1692 						pin->nid, pin->ports[j].id);
1693 			name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1694 			if (!name)
1695 				return -ENOMEM;
1696 			snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1697 			kc[i].name = devm_kstrdup(component->dev, kc_name,
1698 							GFP_KERNEL);
1699 			if (!kc[i].name)
1700 				return -ENOMEM;
1701 
1702 			kc[i].private_value = (unsigned long)name;
1703 			kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1704 			kc[i].access = 0;
1705 			kc[i].info = snd_soc_dapm_info_pin_switch;
1706 			kc[i].put = snd_soc_dapm_put_pin_switch;
1707 			kc[i].get = snd_soc_dapm_get_pin_switch;
1708 			i++;
1709 		}
1710 	}
1711 
1712 	return snd_soc_add_card_controls(card, kc, i);
1713 }
1714 
1715 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1716 			struct snd_soc_dapm_context *dapm)
1717 {
1718 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1719 	struct hdac_device *hdev = hdmi->hdev;
1720 	struct hdac_hdmi_pin *pin;
1721 	struct snd_soc_dapm_widget *widgets;
1722 	struct snd_soc_dapm_route *route;
1723 	char w_name[NAME_SIZE];
1724 	int i = 0, j, ret;
1725 
1726 	widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1727 				sizeof(*widgets), GFP_KERNEL);
1728 
1729 	if (!widgets)
1730 		return -ENOMEM;
1731 
1732 	route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1733 				sizeof(*route), GFP_KERNEL);
1734 	if (!route)
1735 		return -ENOMEM;
1736 
1737 	/* create Jack DAPM widget */
1738 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1739 		for (j = 0; j < pin->num_ports; j++) {
1740 			snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1741 						pin->nid, pin->ports[j].id);
1742 
1743 			ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1744 					snd_soc_dapm_spk, NULL,
1745 					w_name, NULL, NULL, 0, NULL, 0);
1746 			if (ret < 0)
1747 				return ret;
1748 
1749 			pin->ports[j].jack_pin = widgets[i].name;
1750 			pin->ports[j].dapm = dapm;
1751 
1752 			/* add to route from Jack widget to output */
1753 			hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1754 					NULL, pin->ports[j].output_pin, NULL);
1755 
1756 			i++;
1757 		}
1758 	}
1759 
1760 	/* Add Route from Jack widget to the output widget */
1761 	ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1762 	if (ret < 0)
1763 		return ret;
1764 
1765 	ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1766 	if (ret < 0)
1767 		return ret;
1768 
1769 	ret = snd_soc_dapm_new_widgets(dapm->card);
1770 	if (ret < 0)
1771 		return ret;
1772 
1773 	/* Add Jack Pin switch Kcontrol */
1774 	ret = create_fill_jack_kcontrols(dapm->card, hdev);
1775 
1776 	if (ret < 0)
1777 		return ret;
1778 
1779 	/* default set the Jack Pin switch to OFF */
1780 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1781 		for (j = 0; j < pin->num_ports; j++)
1782 			snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1783 						pin->ports[j].jack_pin);
1784 	}
1785 
1786 	return 0;
1787 }
1788 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1789 
1790 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1791 				struct snd_soc_jack *jack)
1792 {
1793 	struct snd_soc_component *component = dai->component;
1794 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1795 	struct hdac_device *hdev = hdmi->hdev;
1796 	struct hdac_hdmi_pcm *pcm;
1797 	struct snd_pcm *snd_pcm;
1798 	int err;
1799 
1800 	/*
1801 	 * this is a new PCM device, create new pcm and
1802 	 * add to the pcm list
1803 	 */
1804 	pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1805 	if (!pcm)
1806 		return -ENOMEM;
1807 	pcm->pcm_id = device;
1808 	pcm->cvt = hdmi->dai_map[dai->id].cvt;
1809 	pcm->jack_event = 0;
1810 	pcm->jack = jack;
1811 	mutex_init(&pcm->lock);
1812 	INIT_LIST_HEAD(&pcm->port_list);
1813 	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1814 	if (snd_pcm) {
1815 		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1816 		if (err < 0) {
1817 			dev_err(&hdev->dev,
1818 				"chmap control add failed with err: %d for pcm: %d\n",
1819 				err, device);
1820 			return err;
1821 		}
1822 	}
1823 
1824 	list_add_tail(&pcm->head, &hdmi->pcm_list);
1825 
1826 	return 0;
1827 }
1828 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1829 
1830 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1831 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1832 {
1833 	int i;
1834 	struct hdac_hdmi_pin *pin;
1835 
1836 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1837 		if (detect_pin_caps) {
1838 
1839 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1840 				pin->mst_capable = false;
1841 			else
1842 				pin->mst_capable = true;
1843 		}
1844 
1845 		for (i = 0; i < pin->num_ports; i++) {
1846 			if (!pin->mst_capable && i > 0)
1847 				continue;
1848 
1849 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1850 		}
1851 	}
1852 }
1853 
1854 static int hdmi_codec_probe(struct snd_soc_component *component)
1855 {
1856 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1857 	struct hdac_device *hdev = hdmi->hdev;
1858 	struct snd_soc_dapm_context *dapm =
1859 		snd_soc_component_get_dapm(component);
1860 	struct hdac_ext_link *hlink = NULL;
1861 	int ret;
1862 
1863 	hdmi->component = component;
1864 
1865 	/*
1866 	 * hold the ref while we probe, also no need to drop the ref on
1867 	 * exit, we call pm_runtime_suspend() so that will do for us
1868 	 */
1869 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1870 	if (!hlink) {
1871 		dev_err(&hdev->dev, "hdac link not found\n");
1872 		return -EIO;
1873 	}
1874 
1875 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1876 
1877 	ret = create_fill_widget_route_map(dapm);
1878 	if (ret < 0)
1879 		return ret;
1880 
1881 	aops.audio_ptr = hdev;
1882 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1883 	if (ret < 0) {
1884 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1885 		return ret;
1886 	}
1887 
1888 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1889 	/* Imp: Store the card pointer in hda_codec */
1890 	hdmi->card = dapm->card->snd_card;
1891 
1892 	/*
1893 	 * Setup a device_link between card device and HDMI codec device.
1894 	 * The card device is the consumer and the HDMI codec device is
1895 	 * the supplier. With this setting, we can make sure that the audio
1896 	 * domain in display power will be always turned on before operating
1897 	 * on the HDMI audio codec registers.
1898 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1899 	 * sure the device link is freed when the machine driver is removed.
1900 	 */
1901 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1902 			DL_FLAG_AUTOREMOVE_CONSUMER);
1903 	/*
1904 	 * hdac_device core already sets the state to active and calls
1905 	 * get_noresume. So enable runtime and set the device to suspend.
1906 	 */
1907 	pm_runtime_enable(&hdev->dev);
1908 	pm_runtime_put(&hdev->dev);
1909 	pm_runtime_suspend(&hdev->dev);
1910 
1911 	return 0;
1912 }
1913 
1914 static void hdmi_codec_remove(struct snd_soc_component *component)
1915 {
1916 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1917 	struct hdac_device *hdev = hdmi->hdev;
1918 	int ret;
1919 
1920 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
1921 	if (ret < 0)
1922 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
1923 				ret);
1924 
1925 	pm_runtime_disable(&hdev->dev);
1926 }
1927 
1928 #ifdef CONFIG_PM_SLEEP
1929 static int hdmi_codec_resume(struct device *dev)
1930 {
1931 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
1932 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1933 	int ret;
1934 
1935 	ret = pm_runtime_force_resume(dev);
1936 	if (ret < 0)
1937 		return ret;
1938 	/*
1939 	 * As the ELD notify callback request is not entertained while the
1940 	 * device is in suspend state. Need to manually check detection of
1941 	 * all pins here. pin capablity change is not support, so use the
1942 	 * already set pin caps.
1943 	 *
1944 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1945 	 * The pin check involves only with DRM audio component hooks, so it
1946 	 * works even if the HD-audio side is still dreaming peacefully.
1947 	 */
1948 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1949 	return 0;
1950 }
1951 #else
1952 #define hdmi_codec_resume NULL
1953 #endif
1954 
1955 static const struct snd_soc_component_driver hdmi_hda_codec = {
1956 	.probe			= hdmi_codec_probe,
1957 	.remove			= hdmi_codec_remove,
1958 	.use_pmdown_time	= 1,
1959 	.endianness		= 1,
1960 	.non_legacy_dai_naming	= 1,
1961 };
1962 
1963 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1964 					unsigned char *chmap)
1965 {
1966 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1967 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1968 
1969 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1970 }
1971 
1972 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1973 				unsigned char *chmap, int prepared)
1974 {
1975 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1976 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1977 	struct hdac_hdmi_port *port;
1978 
1979 	if (!pcm)
1980 		return;
1981 
1982 	if (list_empty(&pcm->port_list))
1983 		return;
1984 
1985 	mutex_lock(&pcm->lock);
1986 	pcm->chmap_set = true;
1987 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1988 	list_for_each_entry(port, &pcm->port_list, head)
1989 		if (prepared)
1990 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1991 	mutex_unlock(&pcm->lock);
1992 }
1993 
1994 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1995 {
1996 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1997 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1998 
1999 	if (!pcm)
2000 		return false;
2001 
2002 	if (list_empty(&pcm->port_list))
2003 		return false;
2004 
2005 	return true;
2006 }
2007 
2008 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
2009 {
2010 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2011 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2012 	struct hdac_hdmi_port *port;
2013 
2014 	if (!pcm)
2015 		return 0;
2016 
2017 	if (list_empty(&pcm->port_list))
2018 		return 0;
2019 
2020 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
2021 
2022 	if (!port || !port->eld.eld_valid)
2023 		return 0;
2024 
2025 	return port->eld.info.spk_alloc;
2026 }
2027 
2028 static struct hdac_hdmi_drv_data intel_icl_drv_data  = {
2029 	.vendor_nid = INTEL_VENDOR_NID_0x2,
2030 	.port_map = icl_pin2port_map,
2031 	.port_num = ARRAY_SIZE(icl_pin2port_map),
2032 };
2033 
2034 static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
2035 	.vendor_nid = INTEL_VENDOR_NID_0xb,
2036 };
2037 
2038 static struct hdac_hdmi_drv_data intel_drv_data  = {
2039 	.vendor_nid = INTEL_VENDOR_NID_0x8,
2040 };
2041 
2042 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
2043 {
2044 	struct hdac_hdmi_priv *hdmi_priv = NULL;
2045 	struct snd_soc_dai_driver *hdmi_dais = NULL;
2046 	struct hdac_ext_link *hlink = NULL;
2047 	int num_dais = 0;
2048 	int ret = 0;
2049 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2050 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
2051 
2052 	/* hold the ref while we probe */
2053 	hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2054 	if (!hlink) {
2055 		dev_err(&hdev->dev, "hdac link not found\n");
2056 		return -EIO;
2057 	}
2058 
2059 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2060 
2061 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2062 	if (hdmi_priv == NULL)
2063 		return -ENOMEM;
2064 
2065 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2066 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2067 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2068 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2069 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2070 	hdmi_priv->hdev = hdev;
2071 
2072 	if (!hdac_id)
2073 		return -ENODEV;
2074 
2075 	if (hdac_id->driver_data)
2076 		hdmi_priv->drv_data =
2077 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2078 	else
2079 		hdmi_priv->drv_data = &intel_drv_data;
2080 
2081 	dev_set_drvdata(&hdev->dev, hdmi_priv);
2082 
2083 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
2084 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2085 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2086 	mutex_init(&hdmi_priv->pin_mutex);
2087 
2088 	/*
2089 	 * Turned off in the runtime_suspend during the first explicit
2090 	 * pm_runtime_suspend call.
2091 	 */
2092 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
2093 
2094 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2095 	if (ret < 0) {
2096 		dev_err(&hdev->dev,
2097 			"Failed in parse and map nid with err: %d\n", ret);
2098 		return ret;
2099 	}
2100 	snd_hdac_refresh_widgets(hdev);
2101 
2102 	/* ASoC specific initialization */
2103 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2104 					hdmi_dais, num_dais);
2105 
2106 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2107 
2108 	return ret;
2109 }
2110 
2111 static void clear_dapm_works(struct hdac_device *hdev)
2112 {
2113 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2114 	struct hdac_hdmi_pin *pin;
2115 	int i;
2116 
2117 	list_for_each_entry(pin, &hdmi->pin_list, head)
2118 		for (i = 0; i < pin->num_ports; i++)
2119 			cancel_work_sync(&pin->ports[i].dapm_work);
2120 }
2121 
2122 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2123 {
2124 	clear_dapm_works(hdev);
2125 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
2126 
2127 	return 0;
2128 }
2129 
2130 #ifdef CONFIG_PM
2131 static int hdac_hdmi_runtime_suspend(struct device *dev)
2132 {
2133 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2134 	struct hdac_bus *bus = hdev->bus;
2135 	struct hdac_ext_link *hlink = NULL;
2136 
2137 	dev_dbg(dev, "Enter: %s\n", __func__);
2138 
2139 	/* controller may not have been initialized for the first time */
2140 	if (!bus)
2141 		return 0;
2142 
2143 	clear_dapm_works(hdev);
2144 
2145 	/*
2146 	 * Power down afg.
2147 	 * codec_read is preferred over codec_write to set the power state.
2148 	 * This way verb is send to set the power state and response
2149 	 * is received. So setting power state is ensured without using loop
2150 	 * to read the state.
2151 	 */
2152 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
2153 							AC_PWRST_D3);
2154 
2155 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2156 	if (!hlink) {
2157 		dev_err(dev, "hdac link not found\n");
2158 		return -EIO;
2159 	}
2160 
2161 	snd_hdac_codec_link_down(hdev);
2162 	snd_hdac_ext_bus_link_put(bus, hlink);
2163 
2164 	snd_hdac_display_power(bus, hdev->addr, false);
2165 
2166 	return 0;
2167 }
2168 
2169 static int hdac_hdmi_runtime_resume(struct device *dev)
2170 {
2171 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
2172 	struct hdac_bus *bus = hdev->bus;
2173 	struct hdac_ext_link *hlink = NULL;
2174 
2175 	dev_dbg(dev, "Enter: %s\n", __func__);
2176 
2177 	/* controller may not have been initialized for the first time */
2178 	if (!bus)
2179 		return 0;
2180 
2181 	hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2182 	if (!hlink) {
2183 		dev_err(dev, "hdac link not found\n");
2184 		return -EIO;
2185 	}
2186 
2187 	snd_hdac_ext_bus_link_get(bus, hlink);
2188 	snd_hdac_codec_link_up(hdev);
2189 
2190 	snd_hdac_display_power(bus, hdev->addr, true);
2191 
2192 	hdac_hdmi_skl_enable_all_pins(hdev);
2193 	hdac_hdmi_skl_enable_dp12(hdev);
2194 
2195 	/* Power up afg */
2196 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
2197 							AC_PWRST_D0);
2198 
2199 	return 0;
2200 }
2201 #else
2202 #define hdac_hdmi_runtime_suspend NULL
2203 #define hdac_hdmi_runtime_resume NULL
2204 #endif
2205 
2206 static const struct dev_pm_ops hdac_hdmi_pm = {
2207 	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2208 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2209 };
2210 
2211 static const struct hda_device_id hdmi_list[] = {
2212 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2213 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2214 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2215 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2216 						   &intel_glk_drv_data),
2217 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2218 						   &intel_glk_drv_data),
2219 	HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2220 						   &intel_icl_drv_data),
2221 	{}
2222 };
2223 
2224 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2225 
2226 static struct hdac_driver hdmi_driver = {
2227 	.driver = {
2228 		.name   = "HDMI HDA Codec",
2229 		.pm = &hdac_hdmi_pm,
2230 	},
2231 	.id_table       = hdmi_list,
2232 	.probe          = hdac_hdmi_dev_probe,
2233 	.remove         = hdac_hdmi_dev_remove,
2234 };
2235 
2236 static int __init hdmi_init(void)
2237 {
2238 	return snd_hda_ext_driver_register(&hdmi_driver);
2239 }
2240 
2241 static void __exit hdmi_exit(void)
2242 {
2243 	snd_hda_ext_driver_unregister(&hdmi_driver);
2244 }
2245 
2246 module_init(hdmi_init);
2247 module_exit(hdmi_exit);
2248 
2249 MODULE_LICENSE("GPL v2");
2250 MODULE_DESCRIPTION("HDMI HD codec");
2251 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2252 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2253