xref: /linux/sound/soc/codecs/hdac_hdmi.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
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/cleanup.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/hdmi.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_eld.h>
21 #include <sound/pcm_params.h>
22 #include <sound/jack.h>
23 #include <sound/soc.h>
24 #include <sound/hdaudio_ext.h>
25 #include <sound/hda_i915.h>
26 #include <sound/pcm_drm_eld.h>
27 #include <sound/hda_chmap.h>
28 
29 #define NAME_SIZE	32
30 
31 #define AMP_OUT_MUTE		0xb080
32 #define AMP_OUT_UNMUTE		0xb000
33 #define PIN_OUT			(AC_PINCTL_OUT_EN)
34 
35 #define HDA_MAX_CONNECTIONS     32
36 
37 #define HDA_MAX_CVTS		3
38 #define HDA_MAX_PORTS		3
39 
40 #define ELD_MAX_SIZE    256
41 #define ELD_FIXED_BYTES	20
42 
43 #define ELD_VER_CEA_861D 2
44 #define ELD_VER_PARTIAL 31
45 #define ELD_MAX_MNL     16
46 
47 struct hdac_hdmi_cvt_params {
48 	unsigned int channels_min;
49 	unsigned int channels_max;
50 	u32 rates;
51 	u64 formats;
52 	unsigned int maxbps;
53 };
54 
55 struct hdac_hdmi_cvt {
56 	struct list_head head;
57 	hda_nid_t nid;
58 	const char *name;
59 	struct hdac_hdmi_cvt_params params;
60 };
61 
62 /* Currently only spk_alloc, more to be added */
63 struct hdac_hdmi_parsed_eld {
64 	u8 spk_alloc;
65 };
66 
67 struct hdac_hdmi_eld {
68 	bool	monitor_present;
69 	bool	eld_valid;
70 	int	eld_size;
71 	char    eld_buffer[ELD_MAX_SIZE];
72 	struct	hdac_hdmi_parsed_eld info;
73 };
74 
75 struct hdac_hdmi_pin {
76 	struct list_head head;
77 	hda_nid_t nid;
78 	bool mst_capable;
79 	struct hdac_hdmi_port *ports;
80 	int num_ports;
81 	struct hdac_device *hdev;
82 };
83 
84 struct hdac_hdmi_port {
85 	struct list_head head;
86 	int id;
87 	struct hdac_hdmi_pin *pin;
88 	int num_mux_nids;
89 	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
90 	struct hdac_hdmi_eld eld;
91 	const char *jack_pin;
92 	bool is_connect;
93 	struct snd_soc_dapm_context *dapm;
94 	const char *output_pin;
95 	struct work_struct dapm_work;
96 };
97 
98 struct hdac_hdmi_pcm {
99 	struct list_head head;
100 	int pcm_id;
101 	struct list_head port_list;
102 	struct hdac_hdmi_cvt *cvt;
103 	struct snd_soc_jack *jack;
104 	int stream_tag;
105 	int channels;
106 	int format;
107 	bool chmap_set;
108 	unsigned char chmap[8]; /* ALSA API channel-map */
109 	struct mutex lock;
110 	int jack_event;
111 	struct snd_kcontrol *eld_ctl;
112 };
113 
114 struct hdac_hdmi_dai_port_map {
115 	int dai_id;
116 	struct hdac_hdmi_port *port;
117 	struct hdac_hdmi_cvt *cvt;
118 };
119 
120 struct hdac_hdmi_drv_data {
121 	unsigned int vendor_nid;
122 };
123 
124 struct hdac_hdmi_priv {
125 	struct hdac_device *hdev;
126 	struct snd_soc_component *component;
127 	struct snd_card *card;
128 	struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
129 	struct list_head pin_list;
130 	struct list_head cvt_list;
131 	struct list_head pcm_list;
132 	int num_pin;
133 	int num_cvt;
134 	int num_ports;
135 	struct mutex pin_mutex;
136 	struct hdac_chmap chmap;
137 	struct hdac_hdmi_drv_data *drv_data;
138 	struct snd_soc_dai_driver *dai_drv;
139 };
140 
141 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
142 
143 static struct hdac_hdmi_pcm *
144 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
145 			   struct hdac_hdmi_cvt *cvt)
146 {
147 	struct hdac_hdmi_pcm *pcm;
148 
149 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
150 		if (pcm->cvt == cvt)
151 			return pcm;
152 	}
153 
154 	return NULL;
155 }
156 
157 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
158 		struct hdac_hdmi_port *port, bool is_connect)
159 {
160 	struct hdac_device *hdev = port->pin->hdev;
161 
162 	port->is_connect = is_connect;
163 	if (is_connect) {
164 		/*
165 		 * Report Jack connect event when a device is connected
166 		 * for the first time where same PCM is attached to multiple
167 		 * ports.
168 		 */
169 		if (pcm->jack_event == 0) {
170 			dev_dbg(&hdev->dev,
171 					"jack report for pcm=%d\n",
172 					pcm->pcm_id);
173 			snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
174 						SND_JACK_AVOUT);
175 		}
176 		pcm->jack_event++;
177 	} else {
178 		/*
179 		 * Report Jack disconnect event when a device is disconnected
180 		 * is the only last connected device when same PCM is attached
181 		 * to multiple ports.
182 		 */
183 		if (pcm->jack_event == 1)
184 			snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
185 		if (pcm->jack_event > 0)
186 			pcm->jack_event--;
187 	}
188 }
189 
190 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
191 {
192 	if (port->is_connect)
193 		snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
194 	else
195 		snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
196 	snd_soc_dapm_sync(port->dapm);
197 }
198 
199 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
200 {
201 	struct hdac_hdmi_port *port;
202 
203 	port = container_of(work, struct hdac_hdmi_port, dapm_work);
204 	hdac_hdmi_port_dapm_update(port);
205 }
206 
207 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
208 		struct hdac_hdmi_port *port, bool is_connect)
209 {
210 	hdac_hdmi_jack_report(pcm, port, is_connect);
211 	hdac_hdmi_port_dapm_update(port);
212 }
213 
214 /* MST supported verbs */
215 /*
216  * Get the no devices that can be connected to a port on the Pin widget.
217  */
218 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
219 {
220 	unsigned int caps;
221 	unsigned int type, param;
222 
223 	caps = snd_hdac_get_wcaps(hdev, nid);
224 	type = snd_hdac_get_wcaps_type(caps);
225 
226 	if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
227 		return 0;
228 
229 	param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
230 	if (param == -1)
231 		return param;
232 
233 	return param & AC_DEV_LIST_LEN_MASK;
234 }
235 
236 /*
237  * Get the port entry select on the pin. Return the port entry
238  * id selected on the pin. Return 0 means the first port entry
239  * is selected or MST is not supported.
240  */
241 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
242 					struct hdac_hdmi_port *port)
243 {
244 	return snd_hdac_codec_read(hdev, port->pin->nid,
245 				0, AC_VERB_GET_DEVICE_SEL, 0);
246 }
247 
248 /*
249  * Sets the selected port entry for the configuring Pin widget verb.
250  * returns error if port set is not equal to port get otherwise success
251  */
252 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
253 					struct hdac_hdmi_port *port)
254 {
255 	int num_ports;
256 
257 	if (!port->pin->mst_capable)
258 		return 0;
259 
260 	/* AC_PAR_DEVLIST_LEN is 0 based. */
261 	num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
262 	if (num_ports < 0)
263 		return -EIO;
264 	/*
265 	 * Device List Length is a 0 based integer value indicating the
266 	 * number of sink device that a MST Pin Widget can support.
267 	 */
268 	if (num_ports + 1  < port->id)
269 		return 0;
270 
271 	snd_hdac_codec_write(hdev, port->pin->nid, 0,
272 			AC_VERB_SET_DEVICE_SEL, port->id);
273 
274 	if (port->id != hdac_hdmi_port_select_get(hdev, port))
275 		return -EIO;
276 
277 	dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
278 
279 	return 0;
280 }
281 
282 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
283 						int pcm_idx)
284 {
285 	struct hdac_hdmi_pcm *pcm;
286 
287 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
288 		if (pcm->pcm_id == pcm_idx)
289 			return pcm;
290 	}
291 
292 	return NULL;
293 }
294 
295 static unsigned int sad_format(const u8 *sad)
296 {
297 	return ((sad[0] >> 0x3) & 0x1f);
298 }
299 
300 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
301 {
302 	return (sad[2] & 7);
303 }
304 
305 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
306 						void *eld)
307 {
308 	u64 formats = SNDRV_PCM_FMTBIT_S16;
309 	int i;
310 	const u8 *sad, *eld_buf = eld;
311 
312 	sad = drm_eld_sad(eld_buf);
313 	if (!sad)
314 		goto format_constraint;
315 
316 	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
317 		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
318 
319 			/*
320 			 * the controller support 20 and 24 bits in 32 bit
321 			 * container so we set S32
322 			 */
323 			if (sad_sample_bits_lpcm(sad) & 0x6)
324 				formats |= SNDRV_PCM_FMTBIT_S32;
325 		}
326 	}
327 
328 format_constraint:
329 	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
330 				formats);
331 
332 }
333 
334 static void
335 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
336 				int packet_index, int byte_index)
337 {
338 	int val;
339 
340 	val = (packet_index << 5) | (byte_index & 0x1f);
341 	snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
342 }
343 
344 struct dp_audio_infoframe {
345 	u8 type; /* 0x84 */
346 	u8 len;  /* 0x1b */
347 	u8 ver;  /* 0x11 << 2 */
348 
349 	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
350 	u8 SS01_SF24;
351 	u8 CXT04;
352 	u8 CA;
353 	u8 LFEPBL01_LSV36_DM_INH7;
354 };
355 
356 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
357 		   struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
358 {
359 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
360 	struct hdmi_audio_infoframe frame;
361 	struct hdac_hdmi_pin *pin = port->pin;
362 	struct dp_audio_infoframe dp_ai;
363 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
364 	struct hdac_hdmi_cvt *cvt = pcm->cvt;
365 	u8 *dip;
366 	int ret;
367 	int i;
368 	const u8 *eld_buf;
369 	u8 conn_type;
370 	int channels, ca;
371 
372 	ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
373 			pcm->channels, pcm->chmap_set, true, pcm->chmap);
374 
375 	channels = snd_hdac_get_active_channels(ca);
376 	hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
377 
378 	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
379 				pcm->channels, pcm->chmap, pcm->chmap_set);
380 
381 	eld_buf = port->eld.eld_buffer;
382 	conn_type = drm_eld_get_conn_type(eld_buf);
383 
384 	switch (conn_type) {
385 	case DRM_ELD_CONN_TYPE_HDMI:
386 		hdmi_audio_infoframe_init(&frame);
387 
388 		frame.channels = channels;
389 		frame.channel_allocation = ca;
390 
391 		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
392 		if (ret < 0)
393 			return ret;
394 
395 		break;
396 
397 	case DRM_ELD_CONN_TYPE_DP:
398 		memset(&dp_ai, 0, sizeof(dp_ai));
399 		dp_ai.type	= 0x84;
400 		dp_ai.len	= 0x1b;
401 		dp_ai.ver	= 0x11 << 2;
402 		dp_ai.CC02_CT47	= channels - 1;
403 		dp_ai.CA	= ca;
404 
405 		dip = (u8 *)&dp_ai;
406 		break;
407 
408 	default:
409 		dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
410 		return -EIO;
411 	}
412 
413 	/* stop infoframe transmission */
414 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
415 	snd_hdac_codec_write(hdev, pin->nid, 0,
416 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
417 
418 
419 	/*  Fill infoframe. Index auto-incremented */
420 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
421 	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
422 		for (i = 0; i < sizeof(buffer); i++)
423 			snd_hdac_codec_write(hdev, pin->nid, 0,
424 				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
425 	} else {
426 		for (i = 0; i < sizeof(dp_ai); i++)
427 			snd_hdac_codec_write(hdev, pin->nid, 0,
428 				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
429 	}
430 
431 	/* Start infoframe */
432 	hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
433 	snd_hdac_codec_write(hdev, pin->nid, 0,
434 			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
435 
436 	return 0;
437 }
438 
439 static int hdac_hdmi_set_stream(struct snd_soc_dai *dai,
440 				void *stream, int direction)
441 {
442 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
443 	struct hdac_device *hdev = hdmi->hdev;
444 	struct hdac_hdmi_dai_port_map *dai_map;
445 	struct hdac_hdmi_pcm *pcm;
446 	struct hdac_stream *hstream;
447 
448 	if (!stream)
449 		return -EINVAL;
450 
451 	hstream = (struct hdac_stream *)stream;
452 
453 	dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, hstream->stream_tag);
454 
455 	dai_map = &hdmi->dai_map[dai->id];
456 
457 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
458 
459 	if (pcm)
460 		pcm->stream_tag = (hstream->stream_tag << 4);
461 
462 	return 0;
463 }
464 
465 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
466 	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
467 {
468 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
469 	struct hdac_hdmi_dai_port_map *dai_map;
470 	struct hdac_hdmi_pcm *pcm;
471 	unsigned int bits;
472 	int format;
473 
474 	dai_map = &hdmi->dai_map[dai->id];
475 
476 	bits = snd_hdac_stream_format_bits(params_format(hparams), SNDRV_PCM_SUBFORMAT_STD,
477 					   dai->driver->playback.sig_bits);
478 	format = snd_hdac_stream_format(params_channels(hparams), bits, params_rate(hparams));
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 (!(snd_hdac_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, snd_hdac_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;
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 				scoped_guard(mutex, &pcm->lock) {
542 					ret = hdac_hdmi_query_port_connlist(hdev,
543 									    port->pin,
544 									    port);
545 				}
546 				if (ret < 0)
547 					continue;
548 
549 				for (i = 0; i < port->num_mux_nids; i++) {
550 					if (port->mux_nids[i] == cvt->nid &&
551 						port->eld.monitor_present &&
552 						port->eld.eld_valid)
553 						return port;
554 				}
555 			}
556 		}
557 	}
558 
559 	return NULL;
560 }
561 
562 /*
563  * Go through all converters and ensure connection is set to
564  * the correct pin as set via kcontrols.
565  */
566 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
567 {
568 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
569 	struct hdac_hdmi_port *port;
570 	struct hdac_hdmi_cvt *cvt;
571 	int cvt_idx = 0;
572 
573 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
574 		port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
575 		if (port && port->pin) {
576 			snd_hdac_codec_write(hdev, port->pin->nid, 0,
577 					     AC_VERB_SET_CONNECT_SEL, cvt_idx);
578 			dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
579 				__func__, cvt->name, port->pin->nid, cvt_idx);
580 		}
581 		++cvt_idx;
582 	}
583 }
584 
585 /*
586  * This tries to get a valid pin and set the HW constraints based on the
587  * ELD. Even if a valid pin is not found return success so that device open
588  * doesn't fail.
589  */
590 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
591 			struct snd_soc_dai *dai)
592 {
593 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
594 	struct hdac_device *hdev = hdmi->hdev;
595 	struct hdac_hdmi_dai_port_map *dai_map;
596 	struct hdac_hdmi_cvt *cvt;
597 	struct hdac_hdmi_port *port;
598 	int ret;
599 
600 	dai_map = &hdmi->dai_map[dai->id];
601 
602 	cvt = dai_map->cvt;
603 	port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
604 
605 	/*
606 	 * To make PA and other userland happy.
607 	 * userland scans devices so returning error does not help.
608 	 */
609 	if (!port)
610 		return 0;
611 	if ((!port->eld.monitor_present) ||
612 			(!port->eld.eld_valid)) {
613 
614 		dev_warn(&hdev->dev,
615 			"Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
616 			port->eld.monitor_present, port->eld.eld_valid,
617 			port->pin->nid, port->id);
618 
619 		return 0;
620 	}
621 
622 	dai_map->port = port;
623 
624 	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
625 				port->eld.eld_buffer);
626 	if (ret < 0)
627 		return ret;
628 
629 	return snd_pcm_hw_constraint_eld(substream->runtime,
630 				port->eld.eld_buffer);
631 }
632 
633 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
634 		struct snd_soc_dai *dai)
635 {
636 	struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
637 	struct hdac_hdmi_dai_port_map *dai_map;
638 	struct hdac_hdmi_pcm *pcm;
639 
640 	dai_map = &hdmi->dai_map[dai->id];
641 
642 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
643 
644 	if (pcm) {
645 		scoped_guard(mutex, &pcm->lock) {
646 			pcm->chmap_set = false;
647 			memset(pcm->chmap, 0, sizeof(pcm->chmap));
648 			pcm->channels = 0;
649 		}
650 	}
651 
652 	if (dai_map->port)
653 		dai_map->port = NULL;
654 }
655 
656 static int
657 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
658 {
659 	unsigned int chans;
660 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
661 	int err;
662 
663 	chans = snd_hdac_get_wcaps(hdev, cvt->nid);
664 	chans = snd_hdac_get_wcaps_channels(chans);
665 
666 	cvt->params.channels_min = 2;
667 
668 	cvt->params.channels_max = chans;
669 	if (chans > hdmi->chmap.channels_max)
670 		hdmi->chmap.channels_max = chans;
671 
672 	err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
673 			&cvt->params.rates,
674 			&cvt->params.formats,
675 			NULL,
676 			&cvt->params.maxbps);
677 	if (err < 0)
678 		dev_err(&hdev->dev,
679 			"Failed to query pcm params for nid %d: %d\n",
680 			cvt->nid, err);
681 
682 	return err;
683 }
684 
685 static int hdac_hdmi_fill_widget_info(struct device *dev,
686 		struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
687 		void *priv, const char *wname, const char *stream,
688 		struct snd_kcontrol_new *wc, int numkc,
689 		int (*event)(struct snd_soc_dapm_widget *,
690 		struct snd_kcontrol *, int), unsigned short event_flags)
691 {
692 	w->id = id;
693 	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
694 	if (!w->name)
695 		return -ENOMEM;
696 
697 	w->sname = stream;
698 	w->reg = SND_SOC_NOPM;
699 	w->shift = 0;
700 	w->kcontrol_news = wc;
701 	w->num_kcontrols = numkc;
702 	w->priv = priv;
703 	w->event = event;
704 	w->event_flags = event_flags;
705 
706 	return 0;
707 }
708 
709 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
710 		const char *sink, const char *control, const char *src,
711 		int (*handler)(struct snd_soc_dapm_widget *src,
712 			struct snd_soc_dapm_widget *sink))
713 {
714 	route->sink = sink;
715 	route->source = src;
716 	route->control = control;
717 	route->connected = handler;
718 }
719 
720 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
721 					struct hdac_hdmi_port *port)
722 {
723 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
724 	struct hdac_hdmi_pcm *pcm;
725 	struct hdac_hdmi_port *p;
726 
727 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
728 		if (list_empty(&pcm->port_list))
729 			continue;
730 
731 		list_for_each_entry(p, &pcm->port_list, head) {
732 			if (p->id == port->id && port->pin == p->pin)
733 				return pcm;
734 		}
735 	}
736 
737 	return NULL;
738 }
739 
740 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
741 			     hda_nid_t nid, unsigned int pwr_state)
742 {
743 	int count;
744 	unsigned int state;
745 
746 	if (snd_hdac_get_wcaps(hdev, nid) & AC_WCAP_POWER) {
747 		if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
748 			for (count = 0; count < 10; count++) {
749 				snd_hdac_codec_read(hdev, nid, 0,
750 						AC_VERB_SET_POWER_STATE,
751 						pwr_state);
752 				state = snd_hdac_sync_power_state(hdev,
753 						nid, pwr_state);
754 				if (!(state & AC_PWRST_ERROR))
755 					break;
756 			}
757 		}
758 	}
759 }
760 
761 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
762 				   hda_nid_t nid, int val)
763 {
764 	if (snd_hdac_get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
765 		snd_hdac_codec_write(hdev, nid, 0,
766 					AC_VERB_SET_AMP_GAIN_MUTE, val);
767 }
768 
769 
770 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
771 					struct snd_kcontrol *kc, int event)
772 {
773 	struct hdac_hdmi_port *port = w->priv;
774 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
775 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
776 	struct hdac_hdmi_pcm *pcm;
777 
778 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
779 			__func__, w->name, event);
780 
781 	pcm = hdac_hdmi_get_pcm(hdev, port);
782 	if (!pcm)
783 		return -EIO;
784 
785 	/* set the device if pin is mst_capable */
786 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
787 		return -EIO;
788 
789 	switch (event) {
790 	case SND_SOC_DAPM_PRE_PMU:
791 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
792 
793 		/* Enable out path for this pin widget */
794 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
795 				AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
796 
797 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
798 
799 		return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
800 
801 	case SND_SOC_DAPM_POST_PMD:
802 		hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
803 
804 		/* Disable out path for this pin widget */
805 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
806 				AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
807 
808 		hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
809 		break;
810 
811 	}
812 
813 	return 0;
814 }
815 
816 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
817 					struct snd_kcontrol *kc, int event)
818 {
819 	struct hdac_hdmi_cvt *cvt = w->priv;
820 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
821 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
822 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
823 	struct hdac_hdmi_pcm *pcm;
824 
825 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
826 			__func__, w->name, event);
827 
828 	pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
829 	if (!pcm)
830 		return -EIO;
831 
832 	switch (event) {
833 	case SND_SOC_DAPM_PRE_PMU:
834 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
835 
836 		/* Enable transmission */
837 		snd_hdac_codec_write(hdev, cvt->nid, 0,
838 			AC_VERB_SET_DIGI_CONVERT_1, 1);
839 
840 		/* Category Code (CC) to zero */
841 		snd_hdac_codec_write(hdev, cvt->nid, 0,
842 			AC_VERB_SET_DIGI_CONVERT_2, 0);
843 
844 		snd_hdac_codec_write(hdev, cvt->nid, 0,
845 				AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
846 		snd_hdac_codec_write(hdev, cvt->nid, 0,
847 				AC_VERB_SET_STREAM_FORMAT, pcm->format);
848 
849 		/*
850 		 * The connection indices are shared by all converters and
851 		 * may interfere with each other. Ensure correct
852 		 * routing for all converters at stream start.
853 		 */
854 		hdac_hdmi_verify_connect_sel_all_pins(hdev);
855 
856 		break;
857 
858 	case SND_SOC_DAPM_POST_PMD:
859 		snd_hdac_codec_write(hdev, cvt->nid, 0,
860 				AC_VERB_SET_CHANNEL_STREAMID, 0);
861 		snd_hdac_codec_write(hdev, cvt->nid, 0,
862 				AC_VERB_SET_STREAM_FORMAT, 0);
863 
864 		hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
865 		break;
866 
867 	}
868 
869 	return 0;
870 }
871 
872 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
873 					struct snd_kcontrol *kc, int event)
874 {
875 	struct hdac_hdmi_port *port = w->priv;
876 	struct device *dev = snd_soc_dapm_to_dev(w->dapm);
877 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
878 	int mux_idx;
879 
880 	dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
881 			__func__, w->name, event);
882 
883 	if (!kc)
884 		kc  = w->kcontrols[0];
885 
886 	mux_idx = snd_soc_dapm_kcontrol_get_value(kc);
887 
888 	/* set the device if pin is mst_capable */
889 	if (hdac_hdmi_port_select_set(hdev, port) < 0)
890 		return -EIO;
891 
892 	if (mux_idx > 0) {
893 		snd_hdac_codec_write(hdev, port->pin->nid, 0,
894 			AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
895 	}
896 
897 	return 0;
898 }
899 
900 /*
901  * Based on user selection, map the PINs with the PCMs.
902  */
903 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
904 		struct snd_ctl_elem_value *ucontrol)
905 {
906 	int ret;
907 	struct hdac_hdmi_port *p, *p_next;
908 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
909 	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_to_widget(kcontrol);
910 	struct snd_soc_dapm_context *dapm = w->dapm;
911 	struct device *dev = snd_soc_dapm_to_dev(dapm);
912 	struct hdac_hdmi_port *port = w->priv;
913 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
914 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
915 	struct hdac_hdmi_pcm *pcm;
916 	const char *cvt_name;
917 
918 	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
919 	if (ret < 0)
920 		return ret;
921 
922 	cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
923 
924 	if (port == NULL)
925 		return -EINVAL;
926 
927 	guard(mutex)(&hdmi->pin_mutex);
928 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
929 		if (list_empty(&pcm->port_list))
930 			continue;
931 
932 		list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
933 			if (p == port && p->id == port->id &&
934 					p->pin == port->pin) {
935 				hdac_hdmi_jack_report_sync(pcm, port, false);
936 				list_del(&p->head);
937 			}
938 		}
939 	}
940 
941 	/*
942 	 * Jack status is not reported during device probe as the
943 	 * PCMs are not registered by then. So report it here.
944 	 */
945 	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
946 		if (!strcmp(cvt_name, pcm->cvt->name)) {
947 			list_add_tail(&port->head, &pcm->port_list);
948 			if (port->eld.monitor_present && port->eld.eld_valid) {
949 				hdac_hdmi_jack_report_sync(pcm, port, true);
950 				return ret;
951 			}
952 		}
953 	}
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 	scoped_guard(mutex, &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 			return;
1324 		}
1325 
1326 		if (port->eld.monitor_present && port->eld.eld_valid) {
1327 			if (pcm) {
1328 				hdac_hdmi_jack_report(pcm, port, true);
1329 				schedule_work(&port->dapm_work);
1330 			}
1331 
1332 			print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1333 					     port->eld.eld_buffer, port->eld.eld_size, false);
1334 		}
1335 	}
1336 
1337 	if (eld_changed && pcm)
1338 		snd_ctl_notify(hdmi->card,
1339 			       SNDRV_CTL_EVENT_MASK_VALUE |
1340 			       SNDRV_CTL_EVENT_MASK_INFO,
1341 			       &pcm->eld_ctl->id);
1342 }
1343 
1344 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1345 			       struct hdac_hdmi_pin *pin)
1346 {
1347 	struct hdac_hdmi_port *ports;
1348 	int max_ports = HDA_MAX_PORTS;
1349 	int i;
1350 
1351 	/*
1352 	 * FIXME: max_port may vary for each platform, so pass this as
1353 	 * as driver data or query from i915 interface when this API is
1354 	 * implemented.
1355 	 */
1356 
1357 	ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1358 	if (!ports)
1359 		return -ENOMEM;
1360 
1361 	for (i = 0; i < max_ports; i++) {
1362 		ports[i].id = i;
1363 		ports[i].pin = pin;
1364 		INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1365 	}
1366 	pin->ports = ports;
1367 	pin->num_ports = max_ports;
1368 	return 0;
1369 }
1370 
1371 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1372 {
1373 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1374 	struct hdac_hdmi_pin *pin;
1375 	int ret;
1376 
1377 	pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1378 	if (!pin)
1379 		return -ENOMEM;
1380 
1381 	pin->nid = nid;
1382 	pin->mst_capable = false;
1383 	pin->hdev = hdev;
1384 	ret = hdac_hdmi_add_ports(hdev, pin);
1385 	if (ret < 0)
1386 		return ret;
1387 
1388 	list_add_tail(&pin->head, &hdmi->pin_list);
1389 	hdmi->num_pin++;
1390 	hdmi->num_ports += pin->num_ports;
1391 
1392 	return 0;
1393 }
1394 
1395 #define INTEL_VENDOR_NID 0x08
1396 #define INTEL_GLK_VENDOR_NID 0x0b
1397 #define INTEL_GET_VENDOR_VERB 0xf81
1398 #define INTEL_SET_VENDOR_VERB 0x781
1399 #define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
1400 #define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */
1401 
1402 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1403 {
1404 	unsigned int vendor_param;
1405 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1406 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1407 
1408 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1409 				INTEL_GET_VENDOR_VERB, 0);
1410 	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1411 		return;
1412 
1413 	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1414 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1415 				INTEL_SET_VENDOR_VERB, vendor_param);
1416 	if (vendor_param == -1)
1417 		return;
1418 }
1419 
1420 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1421 {
1422 	unsigned int vendor_param;
1423 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1424 	unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1425 
1426 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1427 				INTEL_GET_VENDOR_VERB, 0);
1428 	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1429 		return;
1430 
1431 	/* enable DP1.2 mode */
1432 	vendor_param |= INTEL_EN_DP12;
1433 	vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1434 				INTEL_SET_VENDOR_VERB, vendor_param);
1435 	if (vendor_param == -1)
1436 		return;
1437 
1438 }
1439 
1440 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1441 	.startup = hdac_hdmi_pcm_open,
1442 	.shutdown = hdac_hdmi_pcm_close,
1443 	.hw_params = hdac_hdmi_set_hw_params,
1444 	.set_stream = hdac_hdmi_set_stream,
1445 };
1446 
1447 /*
1448  * Each converter can support a stream independently. So a dai is created
1449  * based on the number of converter queried.
1450  */
1451 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1452 		struct snd_soc_dai_driver **dais,
1453 		struct hdac_hdmi_priv *hdmi, int num_dais)
1454 {
1455 	struct snd_soc_dai_driver *hdmi_dais;
1456 	struct hdac_hdmi_cvt *cvt;
1457 	char name[NAME_SIZE], dai_name[NAME_SIZE];
1458 	int i = 0;
1459 	u32 rates, bps;
1460 	unsigned int rate_max = 384000, rate_min = 8000;
1461 	u64 formats;
1462 	int ret;
1463 
1464 	hdmi_dais = devm_kzalloc(&hdev->dev,
1465 			(sizeof(*hdmi_dais) * num_dais),
1466 			GFP_KERNEL);
1467 	if (!hdmi_dais)
1468 		return -ENOMEM;
1469 
1470 	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1471 		ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1472 					&rates,	&formats, NULL, &bps);
1473 		if (ret)
1474 			return ret;
1475 
1476 		/* Filter out 44.1, 88.2 and 176.4Khz */
1477 		rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1478 			   SNDRV_PCM_RATE_176400);
1479 		if (!rates)
1480 			return -EINVAL;
1481 
1482 		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1483 		hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1484 					dai_name, GFP_KERNEL);
1485 
1486 		if (!hdmi_dais[i].name)
1487 			return -ENOMEM;
1488 
1489 		snprintf(name, sizeof(name), "hifi%d", i+1);
1490 		hdmi_dais[i].playback.stream_name =
1491 				devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1492 		if (!hdmi_dais[i].playback.stream_name)
1493 			return -ENOMEM;
1494 
1495 		/*
1496 		 * Set caps based on capability queried from the converter.
1497 		 * It will be constrained runtime based on ELD queried.
1498 		 */
1499 		hdmi_dais[i].playback.formats = formats;
1500 		hdmi_dais[i].playback.rates = rates;
1501 		hdmi_dais[i].playback.rate_max = rate_max;
1502 		hdmi_dais[i].playback.rate_min = rate_min;
1503 		hdmi_dais[i].playback.channels_min = 2;
1504 		hdmi_dais[i].playback.channels_max = 2;
1505 		hdmi_dais[i].playback.sig_bits = bps;
1506 		hdmi_dais[i].ops = &hdmi_dai_ops;
1507 		i++;
1508 	}
1509 
1510 	*dais = hdmi_dais;
1511 	hdmi->dai_drv = hdmi_dais;
1512 
1513 	return 0;
1514 }
1515 
1516 /*
1517  * Parse all nodes and store the cvt/pin nids in array
1518  * Add one time initialization for pin and cvt widgets
1519  */
1520 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1521 		struct snd_soc_dai_driver **dais, int *num_dais)
1522 {
1523 	hda_nid_t nid;
1524 	int i, num_nodes;
1525 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1526 	int ret;
1527 
1528 	hdac_hdmi_skl_enable_all_pins(hdev);
1529 	hdac_hdmi_skl_enable_dp12(hdev);
1530 
1531 	num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1532 	if (!nid || num_nodes <= 0) {
1533 		dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1534 		return -EINVAL;
1535 	}
1536 
1537 	for (i = 0; i < num_nodes; i++, nid++) {
1538 		unsigned int caps;
1539 		unsigned int type;
1540 
1541 		caps = snd_hdac_get_wcaps(hdev, nid);
1542 		type = snd_hdac_get_wcaps_type(caps);
1543 
1544 		if (!(caps & AC_WCAP_DIGITAL))
1545 			continue;
1546 
1547 		switch (type) {
1548 
1549 		case AC_WID_AUD_OUT:
1550 			ret = hdac_hdmi_add_cvt(hdev, nid);
1551 			if (ret < 0)
1552 				return ret;
1553 			break;
1554 
1555 		case AC_WID_PIN:
1556 			ret = hdac_hdmi_add_pin(hdev, nid);
1557 			if (ret < 0)
1558 				return ret;
1559 			break;
1560 		}
1561 	}
1562 
1563 	if (!hdmi->num_pin || !hdmi->num_cvt) {
1564 		ret = -EIO;
1565 		dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1566 		return ret;
1567 	}
1568 
1569 	ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1570 	if (ret) {
1571 		dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1572 			ret);
1573 		return ret;
1574 	}
1575 
1576 	*num_dais = hdmi->num_cvt;
1577 	ret = hdac_hdmi_init_dai_map(hdev);
1578 	if (ret < 0)
1579 		dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1580 			ret);
1581 	return ret;
1582 }
1583 
1584 static int hdac_hdmi_pin2port(void *aptr, int pin)
1585 {
1586 	return pin - 4; /* map NID 0x05 -> port #1 */
1587 }
1588 
1589 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1590 {
1591 	struct hdac_device *hdev = aptr;
1592 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1593 	struct hdac_hdmi_pin *pin;
1594 	struct hdac_hdmi_port *hport = NULL;
1595 	struct snd_soc_component *component = hdmi->component;
1596 	int i;
1597 
1598 	/* Don't know how this mapping is derived */
1599 	hda_nid_t pin_nid = port + 0x04;
1600 
1601 	dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1602 							pin_nid, pipe);
1603 
1604 	/*
1605 	 * skip notification during system suspend (but not in runtime PM);
1606 	 * the state will be updated at resume. Also since the ELD and
1607 	 * connection states are updated in anyway at the end of the resume,
1608 	 * we can skip it when received during PM process.
1609 	 */
1610 	if (snd_power_get_state(component->card->snd_card) !=
1611 			SNDRV_CTL_POWER_D0)
1612 		return;
1613 
1614 	if (atomic_read(&hdev->in_pm))
1615 		return;
1616 
1617 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1618 		if (pin->nid != pin_nid)
1619 			continue;
1620 
1621 		/* In case of non MST pin, pipe is -1 */
1622 		if (pipe == -1) {
1623 			pin->mst_capable = false;
1624 			/* if not MST, default is port[0] */
1625 			hport = &pin->ports[0];
1626 		} else {
1627 			for (i = 0; i < pin->num_ports; i++) {
1628 				pin->mst_capable = true;
1629 				if (pin->ports[i].id == pipe) {
1630 					hport = &pin->ports[i];
1631 					break;
1632 				}
1633 			}
1634 		}
1635 
1636 		if (hport)
1637 			hdac_hdmi_present_sense(pin, hport);
1638 	}
1639 
1640 }
1641 
1642 static struct drm_audio_component_audio_ops aops = {
1643 	.pin2port	= hdac_hdmi_pin2port,
1644 	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
1645 };
1646 
1647 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1648 			struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1649 {
1650 	int i;
1651 	struct hdac_hdmi_pin *pin;
1652 
1653 	list_for_each_entry(pin, &hdmi->pin_list, head) {
1654 		if (detect_pin_caps) {
1655 
1656 			if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1657 				pin->mst_capable = false;
1658 			else
1659 				pin->mst_capable = true;
1660 		}
1661 
1662 		for (i = 0; i < pin->num_ports; i++) {
1663 			if (!pin->mst_capable && i > 0)
1664 				continue;
1665 
1666 			hdac_hdmi_present_sense(pin, &pin->ports[i]);
1667 		}
1668 	}
1669 }
1670 
1671 static int hdmi_codec_probe(struct snd_soc_component *component)
1672 {
1673 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1674 	struct hdac_device *hdev = hdmi->hdev;
1675 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1676 	struct hdac_ext_link *hlink;
1677 	int ret;
1678 
1679 	hdmi->component = component;
1680 
1681 	/*
1682 	 * hold the ref while we probe, also no need to drop the ref on
1683 	 * exit, we call pm_runtime_suspend() so that will do for us
1684 	 */
1685 	hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1686 	if (!hlink) {
1687 		dev_err(&hdev->dev, "hdac link not found\n");
1688 		return -EIO;
1689 	}
1690 
1691 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1692 
1693 	ret = create_fill_widget_route_map(dapm);
1694 	if (ret < 0)
1695 		return ret;
1696 
1697 	aops.audio_ptr = hdev;
1698 	ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1699 	if (ret < 0) {
1700 		dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1701 		return ret;
1702 	}
1703 
1704 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1705 	/* Imp: Store the card pointer in hda_codec */
1706 	hdmi->card = component->card->snd_card;
1707 
1708 	/*
1709 	 * Setup a device_link between card device and HDMI codec device.
1710 	 * The card device is the consumer and the HDMI codec device is
1711 	 * the supplier. With this setting, we can make sure that the audio
1712 	 * domain in display power will be always turned on before operating
1713 	 * on the HDMI audio codec registers.
1714 	 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1715 	 * sure the device link is freed when the machine driver is removed.
1716 	 */
1717 	device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1718 			DL_FLAG_AUTOREMOVE_CONSUMER);
1719 	/*
1720 	 * hdac_device core already sets the state to active and calls
1721 	 * get_noresume. So enable runtime and set the device to suspend.
1722 	 */
1723 	pm_runtime_enable(&hdev->dev);
1724 	pm_runtime_put(&hdev->dev);
1725 	pm_runtime_suspend(&hdev->dev);
1726 
1727 	return 0;
1728 }
1729 
1730 static void hdmi_codec_remove(struct snd_soc_component *component)
1731 {
1732 	struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1733 	struct hdac_device *hdev = hdmi->hdev;
1734 	int ret;
1735 
1736 	ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
1737 	if (ret < 0)
1738 		dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
1739 				ret);
1740 
1741 	pm_runtime_disable(&hdev->dev);
1742 }
1743 
1744 static int hdmi_codec_resume(struct device *dev)
1745 {
1746 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
1747 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1748 	int ret;
1749 
1750 	ret = pm_runtime_force_resume(dev);
1751 	if (ret < 0)
1752 		return ret;
1753 	/*
1754 	 * As the ELD notify callback request is not entertained while the
1755 	 * device is in suspend state. Need to manually check detection of
1756 	 * all pins here. pin capablity change is not support, so use the
1757 	 * already set pin caps.
1758 	 *
1759 	 * NOTE: this is safe to call even if the codec doesn't actually resume.
1760 	 * The pin check involves only with DRM audio component hooks, so it
1761 	 * works even if the HD-audio side is still dreaming peacefully.
1762 	 */
1763 	hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1764 	return 0;
1765 }
1766 
1767 static const struct snd_soc_component_driver hdmi_hda_codec = {
1768 	.probe			= hdmi_codec_probe,
1769 	.remove			= hdmi_codec_remove,
1770 	.use_pmdown_time	= 1,
1771 	.endianness		= 1,
1772 };
1773 
1774 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1775 					unsigned char *chmap)
1776 {
1777 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1778 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1779 
1780 	memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1781 }
1782 
1783 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1784 				unsigned char *chmap, int prepared)
1785 {
1786 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1787 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1788 	struct hdac_hdmi_port *port;
1789 
1790 	if (!pcm)
1791 		return;
1792 
1793 	if (list_empty(&pcm->port_list))
1794 		return;
1795 
1796 	guard(mutex)(&pcm->lock);
1797 	pcm->chmap_set = true;
1798 	memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1799 	list_for_each_entry(port, &pcm->port_list, head)
1800 		if (prepared)
1801 			hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1802 }
1803 
1804 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1805 {
1806 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1807 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1808 
1809 	if (!pcm)
1810 		return false;
1811 
1812 	if (list_empty(&pcm->port_list))
1813 		return false;
1814 
1815 	return true;
1816 }
1817 
1818 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
1819 {
1820 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1821 	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1822 	struct hdac_hdmi_port *port;
1823 
1824 	if (!pcm)
1825 		return 0;
1826 
1827 	if (list_empty(&pcm->port_list))
1828 		return 0;
1829 
1830 	port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1831 
1832 	if (!port || !port->eld.eld_valid)
1833 		return 0;
1834 
1835 	return port->eld.info.spk_alloc;
1836 }
1837 
1838 static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
1839 	.vendor_nid = INTEL_GLK_VENDOR_NID,
1840 };
1841 
1842 static struct hdac_hdmi_drv_data intel_drv_data  = {
1843 	.vendor_nid = INTEL_VENDOR_NID,
1844 };
1845 
1846 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
1847 {
1848 	struct hdac_hdmi_priv *hdmi_priv;
1849 	struct snd_soc_dai_driver *hdmi_dais = NULL;
1850 	struct hdac_ext_link *hlink;
1851 	int num_dais = 0;
1852 	int ret;
1853 	struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1854 	const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
1855 
1856 	/* hold the ref while we probe */
1857 	hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1858 	if (!hlink) {
1859 		dev_err(&hdev->dev, "hdac link not found\n");
1860 		return -EIO;
1861 	}
1862 
1863 	snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1864 
1865 	hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1866 	if (hdmi_priv == NULL) {
1867 		snd_hdac_ext_bus_link_put(hdev->bus, hlink);
1868 		return -ENOMEM;
1869 	}
1870 
1871 	snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
1872 	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1873 	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1874 	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1875 	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
1876 	hdmi_priv->hdev = hdev;
1877 
1878 	if (!hdac_id) {
1879 		snd_hdac_ext_bus_link_put(hdev->bus, hlink);
1880 		return -ENODEV;
1881 	}
1882 
1883 	if (hdac_id->driver_data)
1884 		hdmi_priv->drv_data =
1885 			(struct hdac_hdmi_drv_data *)hdac_id->driver_data;
1886 	else
1887 		hdmi_priv->drv_data = &intel_drv_data;
1888 
1889 	dev_set_drvdata(&hdev->dev, hdmi_priv);
1890 
1891 	INIT_LIST_HEAD(&hdmi_priv->pin_list);
1892 	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1893 	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1894 	mutex_init(&hdmi_priv->pin_mutex);
1895 
1896 	/*
1897 	 * Turned off in the runtime_suspend during the first explicit
1898 	 * pm_runtime_suspend call.
1899 	 */
1900 	snd_hdac_display_power(hdev->bus, hdev->addr, true);
1901 
1902 	ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
1903 	if (ret < 0) {
1904 		dev_err(&hdev->dev,
1905 			"Failed in parse and map nid with err: %d\n", ret);
1906 		snd_hdac_ext_bus_link_put(hdev->bus, hlink);
1907 		snd_hdac_display_power(hdev->bus, hdev->addr, false);
1908 		return ret;
1909 	}
1910 	snd_hdac_refresh_widgets(hdev);
1911 
1912 	/* ASoC specific initialization */
1913 	ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
1914 					hdmi_dais, num_dais);
1915 
1916 	snd_hdac_ext_bus_link_put(hdev->bus, hlink);
1917 
1918 	return ret;
1919 }
1920 
1921 static void clear_dapm_works(struct hdac_device *hdev)
1922 {
1923 	struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1924 	struct hdac_hdmi_pin *pin;
1925 	int i;
1926 
1927 	list_for_each_entry(pin, &hdmi->pin_list, head)
1928 		for (i = 0; i < pin->num_ports; i++)
1929 			cancel_work_sync(&pin->ports[i].dapm_work);
1930 }
1931 
1932 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
1933 {
1934 	clear_dapm_works(hdev);
1935 	snd_hdac_display_power(hdev->bus, hdev->addr, false);
1936 
1937 	return 0;
1938 }
1939 
1940 static int hdac_hdmi_runtime_suspend(struct device *dev)
1941 {
1942 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
1943 	struct hdac_bus *bus = hdev->bus;
1944 	struct hdac_ext_link *hlink;
1945 
1946 	dev_dbg(dev, "Enter: %s\n", __func__);
1947 
1948 	/* controller may not have been initialized for the first time */
1949 	if (!bus)
1950 		return 0;
1951 
1952 	/*
1953 	 * Power down afg.
1954 	 * codec_read is preferred over codec_write to set the power state.
1955 	 * This way verb is send to set the power state and response
1956 	 * is received. So setting power state is ensured without using loop
1957 	 * to read the state.
1958 	 */
1959 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
1960 							AC_PWRST_D3);
1961 
1962 	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
1963 	if (!hlink) {
1964 		dev_err(dev, "hdac link not found\n");
1965 		return -EIO;
1966 	}
1967 
1968 	snd_hdac_codec_link_down(hdev);
1969 	snd_hdac_ext_bus_link_put(bus, hlink);
1970 
1971 	snd_hdac_display_power(bus, hdev->addr, false);
1972 
1973 	return 0;
1974 }
1975 
1976 static int hdac_hdmi_runtime_resume(struct device *dev)
1977 {
1978 	struct hdac_device *hdev = dev_to_hdac_dev(dev);
1979 	struct hdac_bus *bus = hdev->bus;
1980 	struct hdac_ext_link *hlink;
1981 
1982 	dev_dbg(dev, "Enter: %s\n", __func__);
1983 
1984 	/* controller may not have been initialized for the first time */
1985 	if (!bus)
1986 		return 0;
1987 
1988 	hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
1989 	if (!hlink) {
1990 		dev_err(dev, "hdac link not found\n");
1991 		return -EIO;
1992 	}
1993 
1994 	snd_hdac_ext_bus_link_get(bus, hlink);
1995 	snd_hdac_codec_link_up(hdev);
1996 
1997 	snd_hdac_display_power(bus, hdev->addr, true);
1998 
1999 	hdac_hdmi_skl_enable_all_pins(hdev);
2000 	hdac_hdmi_skl_enable_dp12(hdev);
2001 
2002 	/* Power up afg */
2003 	snd_hdac_codec_read(hdev, hdev->afg, 0,	AC_VERB_SET_POWER_STATE,
2004 							AC_PWRST_D0);
2005 
2006 	return 0;
2007 }
2008 
2009 static const struct dev_pm_ops hdac_hdmi_pm = {
2010 	RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2011 	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2012 };
2013 
2014 static const struct hda_device_id hdmi_list[] = {
2015 	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2016 	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2017 	HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2018 	HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2019 						   &intel_glk_drv_data),
2020 	HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2021 						   &intel_glk_drv_data),
2022 	{}
2023 };
2024 
2025 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2026 
2027 static struct hdac_driver hdmi_driver = {
2028 	.driver = {
2029 		.name   = "HDMI HDA Codec",
2030 		.pm = pm_ptr(&hdac_hdmi_pm),
2031 	},
2032 	.id_table       = hdmi_list,
2033 	.probe          = hdac_hdmi_dev_probe,
2034 	.remove         = hdac_hdmi_dev_remove,
2035 };
2036 
2037 static int __init hdmi_init(void)
2038 {
2039 	return snd_hda_ext_driver_register(&hdmi_driver);
2040 }
2041 
2042 static void __exit hdmi_exit(void)
2043 {
2044 	snd_hda_ext_driver_unregister(&hdmi_driver);
2045 }
2046 
2047 module_init(hdmi_init);
2048 module_exit(hdmi_exit);
2049 
2050 MODULE_LICENSE("GPL v2");
2051 MODULE_DESCRIPTION("HDMI HD codec");
2052 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2053 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2054