File Index
Files and directories
Sign in
FILE

hdac_hdmi.c

File preview with syntax highlighting.

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