Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

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