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