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