hdac_hdmi.c 46.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
 *
 *  Copyright (C) 2014-2015 Intel Corp
 *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
 *	    Subhransu S. Prusty <subhransu.s.prusty@intel.com>
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
24
#include <linux/hdmi.h>
25
#include <drm/drm_edid.h>
26
#include <sound/pcm_params.h>
J
Jeeja KP 已提交
27
#include <sound/jack.h>
28 29
#include <sound/soc.h>
#include <sound/hdaudio_ext.h>
30
#include <sound/hda_i915.h>
31
#include <sound/pcm_drm_eld.h>
32
#include <sound/hda_chmap.h>
33
#include "../../hda/local.h"
J
Jeeja KP 已提交
34
#include "hdac_hdmi.h"
35

36 37
#define NAME_SIZE	32

38 39
#define AMP_OUT_MUTE		0xb080
#define AMP_OUT_UNMUTE		0xb000
40
#define PIN_OUT			(AC_PINCTL_OUT_EN)
41

42 43
#define HDA_MAX_CONNECTIONS     32

44 45
#define HDA_MAX_CVTS		3

46 47 48
#define ELD_MAX_SIZE    256
#define ELD_FIXED_BYTES	20

49 50 51 52 53 54 55 56 57
struct hdac_hdmi_cvt_params {
	unsigned int channels_min;
	unsigned int channels_max;
	u32 rates;
	u64 formats;
	unsigned int maxbps;
};

struct hdac_hdmi_cvt {
58
	struct list_head head;
59
	hda_nid_t nid;
J
Jeeja KP 已提交
60
	const char *name;
61 62 63
	struct hdac_hdmi_cvt_params params;
};

64 65 66 67 68
/* Currently only spk_alloc, more to be added */
struct hdac_hdmi_parsed_eld {
	u8 spk_alloc;
};

69 70 71 72 73
struct hdac_hdmi_eld {
	bool	monitor_present;
	bool	eld_valid;
	int	eld_size;
	char    eld_buffer[ELD_MAX_SIZE];
74
	struct	hdac_hdmi_parsed_eld info;
75 76
};

77
struct hdac_hdmi_pin {
78
	struct list_head head;
79 80 81
	hda_nid_t nid;
	int num_mux_nids;
	hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
82 83 84 85
	struct hdac_hdmi_eld eld;
	struct hdac_ext_device *edev;
	int repoll_count;
	struct delayed_work work;
86 87 88 89
	struct mutex lock;
	bool chmap_set;
	unsigned char chmap[8]; /* ALSA API channel-map */
	int channels; /* current number of channels */
90 91
};

J
Jeeja KP 已提交
92 93 94 95 96 97 98 99
struct hdac_hdmi_pcm {
	struct list_head head;
	int pcm_id;
	struct hdac_hdmi_pin *pin;
	struct hdac_hdmi_cvt *cvt;
	struct snd_jack *jack;
};

100 101
struct hdac_hdmi_dai_pin_map {
	int dai_id;
102 103
	struct hdac_hdmi_pin *pin;
	struct hdac_hdmi_cvt *cvt;
104 105 106
};

struct hdac_hdmi_priv {
107
	struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
108 109
	struct list_head pin_list;
	struct list_head cvt_list;
J
Jeeja KP 已提交
110
	struct list_head pcm_list;
111 112
	int num_pin;
	int num_cvt;
J
Jeeja KP 已提交
113
	struct mutex pin_mutex;
114
	struct hdac_chmap chmap;
115 116
};

117 118 119 120 121 122 123 124 125 126 127 128 129
static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
						int pcm_idx)
{
	struct hdac_hdmi_pcm *pcm;

	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
		if (pcm->pcm_id == pcm_idx)
			return pcm;
	}

	return NULL;
}

130 131
static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
{
132
	struct hdac_device *hdac = dev_to_hdac_dev(dev);
133

134
	return to_ehdac_device(hdac);
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static unsigned int sad_format(const u8 *sad)
{
	return ((sad[0] >> 0x3) & 0x1f);
}

static unsigned int sad_sample_bits_lpcm(const u8 *sad)
{
	return (sad[2] & 7);
}

static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
						void *eld)
{
	u64 formats = SNDRV_PCM_FMTBIT_S16;
	int i;
	const u8 *sad, *eld_buf = eld;

	sad = drm_eld_sad(eld_buf);
	if (!sad)
		goto format_constraint;

	for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
		if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */

			/*
			 * the controller support 20 and 24 bits in 32 bit
			 * container so we set S32
			 */
			if (sad_sample_bits_lpcm(sad) & 0x6)
				formats |= SNDRV_PCM_FMTBIT_S32;
		}
	}

format_constraint:
	return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
				formats);

}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
 /* HDMI ELD routines */
static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
				hda_nid_t nid, int byte_index)
{
	unsigned int val;

	val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
							byte_index);

	dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
					byte_index, val);

	return val;
}

static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
{
	return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
						 AC_DIPSIZE_ELD_BUF);
}

/*
 * This function queries the ELD size and ELD data and fills in the buffer
 * passed by user
 */
static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
			     unsigned char *buf, int *eld_size)
{
	int i, size, ret = 0;

	/*
	 * ELD size is initialized to zero in caller function. If no errors and
	 * ELD is valid, actual eld_size is assigned.
	 */

	size = hdac_hdmi_get_eld_size(codec, nid);
	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
		dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
		return -ERANGE;
	}

	/* set ELD buffer */
	for (i = 0; i < size; i++) {
		unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
		/*
		 * Graphics driver might be writing to ELD buffer right now.
		 * Just abort. The caller will repoll after a while.
		 */
		if (!(val & AC_ELDD_ELD_VALID)) {
			dev_err(&codec->dev,
				"HDMI: invalid ELD data byte %d\n", i);
			ret = -EINVAL;
			goto error;
		}
		val &= AC_ELDD_ELD_DATA;
		/*
		 * The first byte cannot be zero. This can happen on some DVI
		 * connections. Some Intel chips may also need some 250ms delay
		 * to return non-zero ELD data, even when the graphics driver
		 * correctly writes ELD content before setting ELD_valid bit.
		 */
		if (!val && !i) {
			dev_err(&codec->dev, "HDMI: 0 ELD data\n");
			ret = -EINVAL;
			goto error;
		}
		buf[i] = val;
	}

	*eld_size = size;
error:
	return ret;
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
				hda_nid_t cvt_nid, hda_nid_t pin_nid,
				u32 stream_tag, int format)
{
	unsigned int val;

	dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
			cvt_nid, pin_nid, stream_tag, format);

	val = (stream_tag << 4);

	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
				AC_VERB_SET_CHANNEL_STREAMID, val);
	snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
				AC_VERB_SET_STREAM_FORMAT, format);

	return 0;
}

269 270 271 272 273 274 275 276 277 278 279 280
static void
hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
				int packet_index, int byte_index)
{
	int val;

	val = (packet_index << 5) | (byte_index & 0x1f);

	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
				AC_VERB_SET_HDMI_DIP_INDEX, val);
}

281 282 283 284 285 286 287 288 289 290 291 292
struct dp_audio_infoframe {
	u8 type; /* 0x84 */
	u8 len;  /* 0x1b */
	u8 ver;  /* 0x11 << 2 */

	u8 CC02_CT47;	/* match with HDMI infoframe from this on */
	u8 SS01_SF24;
	u8 CXT04;
	u8 CA;
	u8 LFEPBL01_LSV36_DM_INH7;
};

293 294 295 296 297
static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
				hda_nid_t cvt_nid, hda_nid_t pin_nid)
{
	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
	struct hdmi_audio_infoframe frame;
298 299 300 301
	struct dp_audio_infoframe dp_ai;
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	struct hdac_hdmi_pin *pin;
	u8 *dip;
302 303
	int ret;
	int i;
304 305
	const u8 *eld_buf;
	u8 conn_type;
306
	int channels, ca;
307

308 309 310 311
	list_for_each_entry(pin, &hdmi->pin_list, head) {
		if (pin->nid == pin_nid)
			break;
	}
312

313 314 315 316 317 318 319 320 321
	ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
			pin->channels, pin->chmap_set, true, pin->chmap);

	channels = snd_hdac_get_active_channels(ca);
	hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);

	snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
				pin->channels, pin->chmap, pin->chmap_set);

322 323
	eld_buf = pin->eld.eld_buffer;
	conn_type = drm_eld_get_conn_type(eld_buf);
324

325 326 327 328 329
	switch (conn_type) {
	case DRM_ELD_CONN_TYPE_HDMI:
		hdmi_audio_infoframe_init(&frame);

		frame.channels = channels;
330
		frame.channel_allocation = ca;
331 332 333 334

		ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
		if (ret < 0)
			return ret;
335

336 337 338 339 340 341 342 343
		break;

	case DRM_ELD_CONN_TYPE_DP:
		memset(&dp_ai, 0, sizeof(dp_ai));
		dp_ai.type	= 0x84;
		dp_ai.len	= 0x1b;
		dp_ai.ver	= 0x11 << 2;
		dp_ai.CC02_CT47	= channels - 1;
344
		dp_ai.CA	= ca;
345 346 347 348 349 350 351 352 353

		dip = (u8 *)&dp_ai;
		break;

	default:
		dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
						conn_type);
		return -EIO;
	}
354 355 356 357 358 359 360 361 362

	/* stop infoframe transmission */
	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);


	/*  Fill infoframe. Index auto-incremented */
	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
363
	if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
364
		for (i = 0; i < sizeof(buffer); i++)
365
			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
366
				AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
367 368 369
	} else {
		for (i = 0; i < sizeof(dp_ai); i++)
			snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
370
				AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
371
	}
372 373 374 375 376 377 378 379 380

	/* Start infoframe */
	hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
	snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
			AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);

	return 0;
}

381 382 383 384
static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
		struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
{
	/* Power up pin widget */
385 386 387
	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
						pwr_state))
		snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
388 389 390
			AC_VERB_SET_POWER_STATE, pwr_state);

	/* Power up converter */
391 392 393
	if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
						pwr_state))
		snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
394 395 396 397 398 399 400 401 402
			AC_VERB_SET_POWER_STATE, pwr_state);
}

static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	struct hdac_hdmi_dai_pin_map *dai_map;
403
	struct hdac_hdmi_pin *pin;
404
	struct hdac_ext_dma_params *dd;
405
	int ret;
406 407

	dai_map = &hdmi->dai_map[dai->id];
408
	pin = dai_map->pin;
409 410 411 412 413

	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
	dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
			dd->stream_tag,	dd->format);

414 415 416
	mutex_lock(&pin->lock);
	pin->channels = substream->runtime->channels;

417 418
	ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
						dai_map->pin->nid);
419
	mutex_unlock(&pin->lock);
420 421 422
	if (ret < 0)
		return ret;

423 424
	return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
			dai_map->pin->nid, dd->stream_tag, dd->format);
425 426 427 428 429 430
}

static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
{
	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
431 432 433
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	struct hdac_hdmi_dai_pin_map *dai_map;
	struct hdac_hdmi_pin *pin;
434 435
	struct hdac_ext_dma_params *dd;

436 437 438 439 440 441 442 443 444
	dai_map = &hdmi->dai_map[dai->id];
	pin = dai_map->pin;

	if (!pin)
		return -ENODEV;

	if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
		dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
								pin->nid);
445 446 447
		return -ENODEV;
	}

448 449 450 451 452 453 454
	dd = snd_soc_dai_get_dma_data(dai, substream);
	if (!dd) {
		dd = kzalloc(sizeof(*dd), GFP_KERNEL);
		if (!dd)
			return -ENOMEM;
	}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
			params_channels(hparams), params_format(hparams),
			24, 0);

	snd_soc_dai_set_dma_data(dai, substream, (void *)dd);

	return 0;
}

static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
		struct snd_soc_dai *dai)
{
	struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
	struct hdac_ext_dma_params *dd;
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_dai_pin_map *dai_map;

	dai_map = &hdmi->dai_map[dai->id];

	dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);

476 477 478 479
	if (dd) {
		snd_soc_dai_set_dma_data(dai, substream, NULL);
		kfree(dd);
	}
480 481 482 483

	return 0;
}

484 485 486 487 488 489 490 491 492 493 494 495
static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
		struct hdac_hdmi_dai_pin_map *dai_map)
{
	/* Enable transmission */
	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
			AC_VERB_SET_DIGI_CONVERT_1, 1);

	/* Category Code (CC) to zero */
	snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
			AC_VERB_SET_DIGI_CONVERT_2, 0);
}

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
		struct hdac_hdmi_dai_pin_map *dai_map)
{
	int mux_idx;
	struct hdac_hdmi_pin *pin = dai_map->pin;

	for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
		if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
			snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
					AC_VERB_SET_CONNECT_SEL, mux_idx);
			break;
		}
	}

	if (mux_idx == pin->num_mux_nids)
		return -EIO;

	/* Enable out path for this pin widget */
	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
			AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);

	hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);

	snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);

	return 0;
}

static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
					struct hdac_hdmi_pin *pin)
{
	if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
		dev_warn(&hdac->hdac.dev,
			"HDMI: pin %d wcaps %#x does not support connection list\n",
			pin->nid, get_wcaps(&hdac->hdac, pin->nid));
		return -EINVAL;
	}

	pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
			pin->mux_nids, HDA_MAX_CONNECTIONS);
	if (pin->num_mux_nids == 0)
		dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
								pin->nid);

	dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
			pin->num_mux_nids, pin->nid);

	return pin->num_mux_nids;
}

/*
 * Query pcm list and return pin widget to which stream is routed.
 *
 * Also query connection list of the pin, to validate the cvt to pin map.
 *
 * Same stream rendering to multiple pins simultaneously can be done
 * possibly, but not supported for now in driver. So return the first pin
 * connected.
 */
static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
			struct hdac_ext_device *edev,
			struct hdac_hdmi_priv *hdmi,
			struct hdac_hdmi_cvt *cvt)
{
	struct hdac_hdmi_pcm *pcm;
	struct hdac_hdmi_pin *pin = NULL;
	int ret, i;

	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
		if (pcm->cvt == cvt) {
			pin = pcm->pin;
			break;
		}
	}

	if (pin) {
		ret = hdac_hdmi_query_pin_connlist(edev, pin);
		if (ret < 0)
			return NULL;

		for (i = 0; i < pin->num_mux_nids; i++) {
			if (pin->mux_nids[i] == cvt->nid)
				return pin;
		}
	}

	return NULL;
}

586 587 588 589 590
/*
 * This tries to get a valid pin and set the HW constraints based on the
 * ELD. Even if a valid pin is not found return success so that device open
 * doesn't fail.
 */
591 592 593 594 595 596
static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	struct hdac_hdmi_dai_pin_map *dai_map;
597 598
	struct hdac_hdmi_cvt *cvt;
	struct hdac_hdmi_pin *pin;
599
	int ret;
600 601 602

	dai_map = &hdmi->dai_map[dai->id];

603 604
	cvt = dai_map->cvt;
	pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
605 606 607 608 609

	/*
	 * To make PA and other userland happy.
	 * userland scans devices so returning error does not help.
	 */
610
	if (!pin)
611
		return 0;
612 613 614

	if ((!pin->eld.monitor_present) ||
			(!pin->eld.eld_valid)) {
615

616
		dev_warn(&hdac->hdac.dev,
617 618
			"Failed: montior present? %d ELD valid?: %d for pin: %d\n",
			pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
619

620
		return 0;
621 622
	}

623
	dai_map->pin = pin;
624

625
	hdac_hdmi_enable_cvt(hdac, dai_map);
626 627 628
	ret = hdac_hdmi_enable_pin(hdac, dai_map);
	if (ret < 0)
		return ret;
629

630
	ret = hdac_hdmi_eld_limit_formats(substream->runtime,
631
				pin->eld.eld_buffer);
632 633
	if (ret < 0)
		return ret;
634

635
	return snd_pcm_hw_constraint_eld(substream->runtime,
636
				pin->eld.eld_buffer);
637 638
}

J
Jeeja KP 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
		struct snd_soc_dai *dai)
{
	struct hdac_hdmi_dai_pin_map *dai_map;
	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	int ret;

	dai_map = &hdmi->dai_map[dai->id];
	if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
		ret = hdac_hdmi_enable_pin(hdac, dai_map);
		if (ret < 0)
			return ret;

		return hdac_hdmi_playback_prepare(substream, dai);
	}

	return 0;
}

659 660 661 662 663 664 665 666 667
static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
		struct snd_soc_dai *dai)
{
	struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
	struct hdac_hdmi_priv *hdmi = hdac->private_data;
	struct hdac_hdmi_dai_pin_map *dai_map;

	dai_map = &hdmi->dai_map[dai->id];

668 669 670 671 672 673 674
	if (dai_map->pin) {
		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
				AC_VERB_SET_CHANNEL_STREAMID, 0);
		snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
				AC_VERB_SET_STREAM_FORMAT, 0);

		hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
675

676
		snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
677
			AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
678

679
		mutex_lock(&dai_map->pin->lock);
680 681
		dai_map->pin->chmap_set = false;
		memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
682 683 684
		dai_map->pin->channels = 0;
		mutex_unlock(&dai_map->pin->lock);

685 686
		dai_map->pin = NULL;
	}
687 688
}

689 690 691
static int
hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
{
692 693 694
	unsigned int chans;
	struct hdac_ext_device *edev = to_ehdac_device(hdac);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
695 696
	int err;

697 698 699 700 701 702 703 704
	chans = get_wcaps(hdac, cvt->nid);
	chans = get_wcaps_channels(chans);

	cvt->params.channels_min = 2;

	cvt->params.channels_max = chans;
	if (chans > hdmi->chmap.channels_max)
		hdmi->chmap.channels_max = chans;
705 706 707 708 709 710 711 712 713 714 715 716 717

	err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
			&cvt->params.rates,
			&cvt->params.formats,
			&cvt->params.maxbps);
	if (err < 0)
		dev_err(&hdac->dev,
			"Failed to query pcm params for nid %d: %d\n",
			cvt->nid, err);

	return err;
}

718 719 720 721 722
static int hdac_hdmi_fill_widget_info(struct device *dev,
				struct snd_soc_dapm_widget *w,
				enum snd_soc_dapm_type id, void *priv,
				const char *wname, const char *stream,
				struct snd_kcontrol_new *wc, int numkc)
723 724
{
	w->id = id;
725 726 727 728
	w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
	if (!w->name)
		return -ENOMEM;

729 730 731
	w->sname = stream;
	w->reg = SND_SOC_NOPM;
	w->shift = 0;
732 733 734 735 736
	w->kcontrol_news = wc;
	w->num_kcontrols = numkc;
	w->priv = priv;

	return 0;
737 738 739
}

static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
740 741 742
		const char *sink, const char *control, const char *src,
		int (*handler)(struct snd_soc_dapm_widget *src,
			struct snd_soc_dapm_widget *sink))
743 744 745 746
{
	route->sink = sink;
	route->source = src;
	route->control = control;
747
	route->connected = handler;
748 749
}

J
Jeeja KP 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
					struct hdac_hdmi_pin *pin)
{
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = NULL;

	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
		if (pcm->pin == pin)
			return pcm;
	}

	return NULL;
}

/*
 * Based on user selection, map the PINs with the PCMs.
 */
static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
		struct snd_ctl_elem_value *ucontrol)
{
	int ret;
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
	struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
	struct snd_soc_dapm_context *dapm = w->dapm;
	struct hdac_hdmi_pin *pin = w->priv;
	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = NULL;
	const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];

	ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
	if (ret < 0)
		return ret;

	mutex_lock(&hdmi->pin_mutex);
	list_for_each_entry(pcm, &hdmi->pcm_list, head) {
		if (pcm->pin == pin)
			pcm->pin = NULL;

		/*
		 * Jack status is not reported during device probe as the
		 * PCMs are not registered by then. So report it here.
		 */
		if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
			pcm->pin = pin;
			if (pin->eld.monitor_present && pin->eld.eld_valid) {
				dev_dbg(&edev->hdac.dev,
					"jack report for pcm=%d\n",
					pcm->pcm_id);

				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
			}
			mutex_unlock(&hdmi->pin_mutex);
			return ret;
		}
	}
	mutex_unlock(&hdmi->pin_mutex);

	return ret;
}

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
/*
 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
 * the display driver seem to be programming the connection list for the pin
 * widget runtime.
 *
 * So programming all the possible inputs for the mux, the user has to take
 * care of selecting the right one and leaving all other inputs selected to
 * "NONE"
 */
static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
				struct hdac_hdmi_pin *pin,
				struct snd_soc_dapm_widget *widget,
				const char *widget_name)
{
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct snd_kcontrol_new *kc;
	struct hdac_hdmi_cvt *cvt;
	struct soc_enum *se;
	char kc_name[NAME_SIZE];
	char mux_items[NAME_SIZE];
	/* To hold inputs to the Pin mux */
	char *items[HDA_MAX_CONNECTIONS];
	int i = 0;
	int num_items = hdmi->num_cvt + 1;

	kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
	if (!kc)
		return -ENOMEM;

	se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
	if (!se)
		return -ENOMEM;

	sprintf(kc_name, "Pin %d Input", pin->nid);
	kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
	if (!kc->name)
		return -ENOMEM;

	kc->private_value = (long)se;
	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	kc->access = 0;
	kc->info = snd_soc_info_enum_double;
J
Jeeja KP 已提交
853
	kc->put = hdac_hdmi_set_pin_mux;
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
	kc->get = snd_soc_dapm_get_enum_double;

	se->reg = SND_SOC_NOPM;

	/* enum texts: ["NONE", "cvt #", "cvt #", ...] */
	se->items = num_items;
	se->mask = roundup_pow_of_two(se->items) - 1;

	sprintf(mux_items, "NONE");
	items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
	if (!items[i])
		return -ENOMEM;

	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
		i++;
		sprintf(mux_items, "cvt %d", cvt->nid);
		items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
		if (!items[i])
			return -ENOMEM;
	}

	se->texts = devm_kmemdup(&edev->hdac.dev, items,
			(num_items  * sizeof(char *)), GFP_KERNEL);
	if (!se->texts)
		return -ENOMEM;

	return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
J
Jeeja KP 已提交
881
			snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
}

/* Add cvt <- input <- mux route map */
static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
			struct snd_soc_dapm_widget *widgets,
			struct snd_soc_dapm_route *route, int rindex)
{
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	const struct snd_kcontrol_new *kc;
	struct soc_enum *se;
	int mux_index = hdmi->num_cvt + hdmi->num_pin;
	int i, j;

	for (i = 0; i < hdmi->num_pin; i++) {
		kc = widgets[mux_index].kcontrol_news;
		se = (struct soc_enum *)kc->private_value;
		for (j = 0; j < hdmi->num_cvt; j++) {
			hdac_hdmi_fill_route(&route[rindex],
					widgets[mux_index].name,
					se->texts[j + 1],
					widgets[j].name, NULL);

			rindex++;
		}

		mux_index++;
	}
}

/*
 * Widgets are added in the below sequence
 *	Converter widgets for num converters enumerated
 *	Pin widgets for num pins enumerated
 *	Pin mux widgets to represent connenction list of pin widget
 *
 * Total widgets elements = num_cvt + num_pin + num_pin;
 *
 * Routes are added as below:
 *	pin mux -> pin (based on num_pins)
 *	cvt -> "Input sel control" -> pin_mux
 *
 * Total route elements:
 *	num_pins + (pin_muxes * num_cvt)
 */
static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
927
{
928 929 930 931 932 933 934 935 936
	struct snd_soc_dapm_widget *widgets;
	struct snd_soc_dapm_route *route;
	struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
	char widget_name[NAME_SIZE];
	struct hdac_hdmi_cvt *cvt;
	struct hdac_hdmi_pin *pin;
	int ret, i = 0, num_routes = 0;
937

938 939
	if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
		return -EINVAL;
940

941 942 943
	widgets = devm_kzalloc(dapm->dev,
		(sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
		GFP_KERNEL);
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if (!widgets)
		return -ENOMEM;

	/* DAPM widgets to represent each converter widget */
	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
		sprintf(widget_name, "Converter %d", cvt->nid);
		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
			snd_soc_dapm_aif_in, &cvt->nid,
			widget_name, dai_drv[i].playback.stream_name, NULL, 0);
		if (ret < 0)
			return ret;
		i++;
	}

	list_for_each_entry(pin, &hdmi->pin_list, head) {
		sprintf(widget_name, "hif%d Output", pin->nid);
		ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
				snd_soc_dapm_output, &pin->nid,
				widget_name, NULL, NULL, 0);
		if (ret < 0)
			return ret;
		i++;
	}

	/* DAPM widgets to represent the connection list to pin widget */
	list_for_each_entry(pin, &hdmi->pin_list, head) {
		sprintf(widget_name, "Pin %d Mux", pin->nid);
		ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
							widget_name);
		if (ret < 0)
			return ret;
		i++;

		/* For cvt to pin_mux mapping */
		num_routes += hdmi->num_cvt;

		/* For pin_mux to pin mapping */
		num_routes++;
	}

	route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
							GFP_KERNEL);
	if (!route)
		return -ENOMEM;

	i = 0;
	/* Add pin <- NULL <- mux route map */
	list_for_each_entry(pin, &hdmi->pin_list, head) {
		int sink_index = i + hdmi->num_cvt;
		int src_index = sink_index + hdmi->num_pin;

		hdac_hdmi_fill_route(&route[i],
				widgets[sink_index].name, NULL,
				widgets[src_index].name, NULL);
		i++;

	}

	hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);

	snd_soc_dapm_new_controls(dapm, widgets,
		((2 * hdmi->num_pin) + hdmi->num_cvt));

	snd_soc_dapm_add_routes(dapm, route, num_routes);
	snd_soc_dapm_new_widgets(dapm->card);

	return 0;
1012 1013 1014

}

1015
static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
1016
{
1017
	struct hdac_hdmi_priv *hdmi = edev->private_data;
1018
	struct hdac_hdmi_dai_pin_map *dai_map;
1019
	struct hdac_hdmi_cvt *cvt;
1020
	int dai_id = 0;
1021

1022
	if (list_empty(&hdmi->cvt_list))
1023
		return -EINVAL;
1024

1025 1026 1027 1028
	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
		dai_map = &hdmi->dai_map[dai_id];
		dai_map->dai_id = dai_id;
		dai_map->cvt = cvt;
1029

1030
		dai_id++;
1031

1032 1033 1034 1035 1036 1037
		if (dai_id == HDA_MAX_CVTS) {
			dev_warn(&edev->hdac.dev,
				"Max dais supported: %d\n", dai_id);
			break;
		}
	}
1038

1039 1040 1041 1042 1043 1044 1045
	return 0;
}

static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
{
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_cvt *cvt;
J
Jeeja KP 已提交
1046
	char name[NAME_SIZE];
1047 1048 1049 1050 1051 1052

	cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
	if (!cvt)
		return -ENOMEM;

	cvt->nid = nid;
J
Jeeja KP 已提交
1053 1054
	sprintf(name, "cvt %d", cvt->nid);
	cvt->name = kstrdup(name, GFP_KERNEL);
1055 1056 1057 1058 1059 1060 1061

	list_add_tail(&cvt->head, &hdmi->cvt_list);
	hdmi->num_cvt++;

	return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
}

1062 1063 1064 1065 1066 1067
static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
			struct hdac_hdmi_pin *pin)
{
	pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
}

1068 1069 1070
static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
{
	struct hdac_ext_device *edev = pin->edev;
J
Jeeja KP 已提交
1071 1072
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	int val;

	pin->repoll_count = repoll;

	pm_runtime_get_sync(&edev->hdac.dev);
	val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
					AC_VERB_GET_PIN_SENSE, 0);

	dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
						val, pin->nid);

J
Jeeja KP 已提交
1084 1085

	mutex_lock(&hdmi->pin_mutex);
1086 1087 1088
	pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
	pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);

J
Jeeja KP 已提交
1089 1090
	pcm = hdac_hdmi_get_pcm(edev, pin);

1091 1092 1093 1094
	if (!pin->eld.monitor_present || !pin->eld.eld_valid) {

		dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
						__func__, pin->nid);
J
Jeeja KP 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

		/*
		 * PCMs are not registered during device probe, so don't
		 * report jack here. It will be done in usermode mux
		 * control select.
		 */
		if (pcm) {
			dev_dbg(&edev->hdac.dev,
				"jack report for pcm=%d\n", pcm->pcm_id);

			snd_jack_report(pcm->jack, 0);
		}

		mutex_unlock(&hdmi->pin_mutex);
1109 1110 1111 1112 1113 1114 1115 1116 1117
		goto put_hdac_device;
	}

	if (pin->eld.monitor_present && pin->eld.eld_valid) {
		/* TODO: use i915 component for reading ELD later */
		if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
				pin->eld.eld_buffer,
				&pin->eld.eld_size) == 0) {

J
Jeeja KP 已提交
1118 1119 1120 1121 1122 1123 1124
			if (pcm) {
				dev_dbg(&edev->hdac.dev,
					"jack report for pcm=%d\n",
					pcm->pcm_id);

				snd_jack_report(pcm->jack, SND_JACK_AVOUT);
			}
1125
			hdac_hdmi_parse_eld(edev, pin);
J
Jeeja KP 已提交
1126

1127 1128 1129 1130 1131
			print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
					pin->eld.eld_buffer, pin->eld.eld_size);
		} else {
			pin->eld.monitor_present = false;
			pin->eld.eld_valid = false;
J
Jeeja KP 已提交
1132 1133 1134 1135 1136 1137 1138 1139

			if (pcm) {
				dev_dbg(&edev->hdac.dev,
					"jack report for pcm=%d\n",
					pcm->pcm_id);

				snd_jack_report(pcm->jack, 0);
			}
1140 1141 1142
		}
	}

J
Jeeja KP 已提交
1143 1144
	mutex_unlock(&hdmi->pin_mutex);

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	/*
	 * Sometimes the pin_sense may present invalid monitor
	 * present and eld_valid. If ELD data is not valid, loop few
	 * more times to get correct pin sense and valid ELD.
	 */
	if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
		schedule_delayed_work(&pin->work, msecs_to_jiffies(300));

put_hdac_device:
	pm_runtime_put_sync(&edev->hdac.dev);
}

static void hdac_hdmi_repoll_eld(struct work_struct *work)
{
	struct hdac_hdmi_pin *pin =
		container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);

	/* picked from legacy HDA driver */
	if (pin->repoll_count++ > 6)
		pin->repoll_count = 0;

	hdac_hdmi_present_sense(pin, pin->repoll_count);
}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
{
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pin *pin;

	pin = kzalloc(sizeof(*pin), GFP_KERNEL);
	if (!pin)
		return -ENOMEM;

	pin->nid = nid;

	list_add_tail(&pin->head, &hdmi->pin_list);
	hdmi->num_pin++;

1183
	pin->edev = edev;
1184
	mutex_init(&pin->lock);
1185 1186
	INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);

1187
	return 0;
1188 1189
}

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
#define INTEL_VENDOR_NID 0x08
#define INTEL_GET_VENDOR_VERB 0xf81
#define INTEL_SET_VENDOR_VERB 0x781
#define INTEL_EN_DP12			0x02 /* enable DP 1.2 features */
#define INTEL_EN_ALL_PIN_CVTS	0x01 /* enable 2nd & 3rd pins and convertors */

static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
{
	unsigned int vendor_param;

	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
				INTEL_GET_VENDOR_VERB, 0);
	if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
		return;

	vendor_param |= INTEL_EN_ALL_PIN_CVTS;
	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
				INTEL_SET_VENDOR_VERB, vendor_param);
	if (vendor_param == -1)
		return;
}

static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
{
	unsigned int vendor_param;

	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
				INTEL_GET_VENDOR_VERB, 0);
	if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
		return;

	/* enable DP1.2 mode */
	vendor_param |= INTEL_EN_DP12;
	vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
				INTEL_SET_VENDOR_VERB, vendor_param);
	if (vendor_param == -1)
		return;

}

1230 1231 1232 1233 1234
static struct snd_soc_dai_ops hdmi_dai_ops = {
	.startup = hdac_hdmi_pcm_open,
	.shutdown = hdac_hdmi_pcm_close,
	.hw_params = hdac_hdmi_set_hw_params,
	.prepare = hdac_hdmi_playback_prepare,
J
Jeeja KP 已提交
1235
	.trigger = hdac_hdmi_trigger,
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	.hw_free = hdac_hdmi_playback_cleanup,
};

/*
 * Each converter can support a stream independently. So a dai is created
 * based on the number of converter queried.
 */
static int hdac_hdmi_create_dais(struct hdac_device *hdac,
		struct snd_soc_dai_driver **dais,
		struct hdac_hdmi_priv *hdmi, int num_dais)
{
	struct snd_soc_dai_driver *hdmi_dais;
	struct hdac_hdmi_cvt *cvt;
	char name[NAME_SIZE], dai_name[NAME_SIZE];
	int i = 0;
	u32 rates, bps;
	unsigned int rate_max = 384000, rate_min = 8000;
	u64 formats;
	int ret;

	hdmi_dais = devm_kzalloc(&hdac->dev,
			(sizeof(*hdmi_dais) * num_dais),
			GFP_KERNEL);
	if (!hdmi_dais)
		return -ENOMEM;

	list_for_each_entry(cvt, &hdmi->cvt_list, head) {
		ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
					&rates,	&formats, &bps);
		if (ret)
			return ret;

		sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
		hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
					dai_name, GFP_KERNEL);

		if (!hdmi_dais[i].name)
			return -ENOMEM;

		snprintf(name, sizeof(name), "hifi%d", i+1);
		hdmi_dais[i].playback.stream_name =
				devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
		if (!hdmi_dais[i].playback.stream_name)
			return -ENOMEM;

		/*
		 * Set caps based on capability queried from the converter.
		 * It will be constrained runtime based on ELD queried.
		 */
		hdmi_dais[i].playback.formats = formats;
		hdmi_dais[i].playback.rates = rates;
		hdmi_dais[i].playback.rate_max = rate_max;
		hdmi_dais[i].playback.rate_min = rate_min;
		hdmi_dais[i].playback.channels_min = 2;
		hdmi_dais[i].playback.channels_max = 2;
		hdmi_dais[i].ops = &hdmi_dai_ops;

		i++;
	}

	*dais = hdmi_dais;

	return 0;
}

1301 1302 1303 1304
/*
 * Parse all nodes and store the cvt/pin nids in array
 * Add one time initialization for pin and cvt widgets
 */
1305 1306
static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
		struct snd_soc_dai_driver **dais, int *num_dais)
1307 1308
{
	hda_nid_t nid;
1309
	int i, num_nodes;
1310 1311
	struct hdac_device *hdac = &edev->hdac;
	struct hdac_hdmi_priv *hdmi = edev->private_data;
1312
	int ret;
1313

1314 1315 1316
	hdac_hdmi_skl_enable_all_pins(hdac);
	hdac_hdmi_skl_enable_dp12(hdac);

1317
	num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1318
	if (!nid || num_nodes <= 0) {
1319 1320 1321 1322
		dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
		return -EINVAL;
	}

1323
	hdac->num_nodes = num_nodes;
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	hdac->start_nid = nid;

	for (i = 0; i < hdac->num_nodes; i++, nid++) {
		unsigned int caps;
		unsigned int type;

		caps = get_wcaps(hdac, nid);
		type = get_wcaps_type(caps);

		if (!(caps & AC_WCAP_DIGITAL))
			continue;

		switch (type) {

		case AC_WID_AUD_OUT:
1339 1340 1341
			ret = hdac_hdmi_add_cvt(edev, nid);
			if (ret < 0)
				return ret;
1342 1343 1344
			break;

		case AC_WID_PIN:
1345 1346 1347
			ret = hdac_hdmi_add_pin(edev, nid);
			if (ret < 0)
				return ret;
1348 1349 1350 1351 1352 1353
			break;
		}
	}

	hdac->end_nid = nid;

1354
	if (!hdmi->num_pin || !hdmi->num_cvt)
1355 1356
		return -EIO;

1357 1358 1359 1360 1361 1362 1363 1364 1365
	ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
	if (ret) {
		dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
							ret);
		return ret;
	}

	*num_dais = hdmi->num_cvt;

1366
	return hdac_hdmi_init_dai_map(edev);
1367 1368
}

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
{
	struct hdac_ext_device *edev = aptr;
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pin *pin;
	struct snd_soc_codec *codec = edev->scodec;

	/* Don't know how this mapping is derived */
	hda_nid_t pin_nid = port + 0x04;

	dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);

	/*
	 * skip notification during system suspend (but not in runtime PM);
	 * the state will be updated at resume. Also since the ELD and
	 * connection states are updated in anyway at the end of the resume,
	 * we can skip it when received during PM process.
	 */
	if (snd_power_get_state(codec->component.card->snd_card) !=
			SNDRV_CTL_POWER_D0)
		return;

	if (atomic_read(&edev->hdac.in_pm))
		return;

	list_for_each_entry(pin, &hdmi->pin_list, head) {
		if (pin->nid == pin_nid)
			hdac_hdmi_present_sense(pin, 1);
	}
}

static struct i915_audio_component_audio_ops aops = {
	.pin_eld_notify	= hdac_hdmi_eld_notify_cb,
};

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
						int device)
{
	struct snd_soc_pcm_runtime *rtd;

	list_for_each_entry(rtd, &card->rtd_list, list) {
		if (rtd->pcm && (rtd->pcm->device == device))
			return rtd->pcm;
	}

	return NULL;
}

J
Jeeja KP 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425
int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
{
	char jack_name[NAME_SIZE];
	struct snd_soc_codec *codec = dai->codec;
	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
	struct snd_soc_dapm_context *dapm =
		snd_soc_component_get_dapm(&codec->component);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm;
1426 1427
	struct snd_pcm *snd_pcm;
	int err;
J
Jeeja KP 已提交
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

	/*
	 * this is a new PCM device, create new pcm and
	 * add to the pcm list
	 */
	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
	if (!pcm)
		return -ENOMEM;
	pcm->pcm_id = device;
	pcm->cvt = hdmi->dai_map[dai->id].cvt;

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
	snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
	if (snd_pcm) {
		err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
		if (err < 0) {
			dev_err(&edev->hdac.dev,
				"chmap control add failed with err: %d for pcm: %d\n",
				err, device);
			kfree(pcm);
			return err;
		}
	}

J
Jeeja KP 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459
	list_add_tail(&pcm->head, &hdmi->pcm_list);

	sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);

	return snd_jack_new(dapm->card->snd_card, jack_name,
		SND_JACK_AVOUT,	&pcm->jack, true, false);
}
EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);

1460 1461 1462 1463 1464 1465
static int hdmi_codec_probe(struct snd_soc_codec *codec)
{
	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct snd_soc_dapm_context *dapm =
		snd_soc_component_get_dapm(&codec->component);
1466
	struct hdac_hdmi_pin *pin;
1467
	struct hdac_ext_link *hlink = NULL;
1468
	int ret;
1469 1470 1471

	edev->scodec = codec;

1472 1473 1474 1475 1476 1477 1478
	/*
	 * hold the ref while we probe, also no need to drop the ref on
	 * exit, we call pm_runtime_suspend() so that will do for us
	 */
	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
	snd_hdac_ext_bus_link_get(edev->ebus, hlink);

1479 1480 1481
	ret = create_fill_widget_route_map(dapm);
	if (ret < 0)
		return ret;
1482

1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	aops.audio_ptr = edev;
	ret = snd_hdac_i915_register_notifier(&aops);
	if (ret < 0) {
		dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
				ret);
		return ret;
	}

	list_for_each_entry(pin, &hdmi->pin_list, head)
		hdac_hdmi_present_sense(pin, 1);

1494 1495 1496
	/* Imp: Store the card pointer in hda_codec */
	edev->card = dapm->card->snd_card;

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	/*
	 * hdac_device core already sets the state to active and calls
	 * get_noresume. So enable runtime and set the device to suspend.
	 */
	pm_runtime_enable(&edev->hdac.dev);
	pm_runtime_put(&edev->hdac.dev);
	pm_runtime_suspend(&edev->hdac.dev);

	return 0;
}

static int hdmi_codec_remove(struct snd_soc_codec *codec)
{
	struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);

	pm_runtime_disable(&edev->hdac.dev);
1513 1514 1515
	return 0;
}

J
Jeeja KP 已提交
1516
#ifdef CONFIG_PM
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static int hdmi_codec_prepare(struct device *dev)
{
	struct hdac_ext_device *edev = to_hda_ext_device(dev);
	struct hdac_device *hdac = &edev->hdac;

	pm_runtime_get_sync(&edev->hdac.dev);

	/*
	 * Power down afg.
	 * codec_read is preferred over codec_write to set the power state.
	 * This way verb is send to set the power state and response
	 * is received. So setting power state is ensured without using loop
	 * to read the state.
	 */
	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
							AC_PWRST_D3);

	return 0;
}

1537
static void hdmi_codec_complete(struct device *dev)
J
Jeeja KP 已提交
1538
{
1539
	struct hdac_ext_device *edev = to_hda_ext_device(dev);
J
Jeeja KP 已提交
1540 1541 1542 1543 1544
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pin *pin;
	struct hdac_device *hdac = &edev->hdac;

	/* Power up afg */
1545 1546
	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
							AC_PWRST_D0);
J
Jeeja KP 已提交
1547

1548 1549
	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
	hdac_hdmi_skl_enable_dp12(&edev->hdac);
J
Jeeja KP 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558

	/*
	 * As the ELD notify callback request is not entertained while the
	 * device is in suspend state. Need to manually check detection of
	 * all pins here.
	 */
	list_for_each_entry(pin, &hdmi->pin_list, head)
		hdac_hdmi_present_sense(pin, 1);

1559
	pm_runtime_put_sync(&edev->hdac.dev);
J
Jeeja KP 已提交
1560 1561
}
#else
1562
#define hdmi_codec_prepare NULL
1563
#define hdmi_codec_complete NULL
J
Jeeja KP 已提交
1564 1565
#endif

1566 1567
static struct snd_soc_codec_driver hdmi_hda_codec = {
	.probe		= hdmi_codec_probe,
1568
	.remove		= hdmi_codec_remove,
1569 1570 1571
	.idle_bias_off	= true,
};

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
					unsigned char *chmap)
{
	struct hdac_ext_device *edev = to_ehdac_device(hdac);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
	struct hdac_hdmi_pin *pin = pcm->pin;

	/* chmap is already set to 0 in caller */
	if (!pin)
		return;

	memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
}

static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
				unsigned char *chmap, int prepared)
{
	struct hdac_ext_device *edev = to_ehdac_device(hdac);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
	struct hdac_hdmi_pin *pin = pcm->pin;

	mutex_lock(&pin->lock);
	pin->chmap_set = true;
	memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
	if (prepared)
		hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
	mutex_unlock(&pin->lock);
}

static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
{
	struct hdac_ext_device *edev = to_ehdac_device(hdac);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
	struct hdac_hdmi_pin *pin = pcm->pin;

	return pin ? true:false;
}

static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
{
	struct hdac_ext_device *edev = to_ehdac_device(hdac);
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
	struct hdac_hdmi_pin *pin = pcm->pin;

1620
	if (!pin || !pin->eld.eld_valid)
1621 1622 1623 1624 1625
		return 0;

	return pin->eld.info.spk_alloc;
}

1626 1627 1628 1629
static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
{
	struct hdac_device *codec = &edev->hdac;
	struct hdac_hdmi_priv *hdmi_priv;
1630
	struct snd_soc_dai_driver *hdmi_dais = NULL;
1631
	struct hdac_ext_link *hlink = NULL;
1632
	int num_dais = 0;
1633 1634
	int ret = 0;

1635 1636 1637 1638
	/* hold the ref while we probe */
	hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
	snd_hdac_ext_bus_link_get(edev->ebus, hlink);

1639 1640 1641 1642 1643
	hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
	if (hdmi_priv == NULL)
		return -ENOMEM;

	edev->private_data = hdmi_priv;
1644
	snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
1645 1646 1647 1648
	hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
	hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
	hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
	hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
1649 1650 1651

	dev_set_drvdata(&codec->dev, edev);

1652 1653
	INIT_LIST_HEAD(&hdmi_priv->pin_list);
	INIT_LIST_HEAD(&hdmi_priv->cvt_list);
J
Jeeja KP 已提交
1654 1655
	INIT_LIST_HEAD(&hdmi_priv->pcm_list);
	mutex_init(&hdmi_priv->pin_mutex);
1656

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
	/*
	 * Turned off in the runtime_suspend during the first explicit
	 * pm_runtime_suspend call.
	 */
	ret = snd_hdac_display_power(edev->hdac.bus, true);
	if (ret < 0) {
		dev_err(&edev->hdac.dev,
			"Cannot turn on display power on i915 err: %d\n",
			ret);
		return ret;
	}

1669 1670 1671 1672
	ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
	if (ret < 0) {
		dev_err(&codec->dev,
			"Failed in parse and map nid with err: %d\n", ret);
1673
		return ret;
1674
	}
1675 1676

	/* ASoC specific initialization */
1677 1678 1679 1680 1681 1682
	ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
					hdmi_dais, num_dais);

	snd_hdac_ext_bus_link_put(edev->ebus, hlink);

	return ret;
1683 1684 1685 1686
}

static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
{
1687 1688 1689
	struct hdac_hdmi_priv *hdmi = edev->private_data;
	struct hdac_hdmi_pin *pin, *pin_next;
	struct hdac_hdmi_cvt *cvt, *cvt_next;
J
Jeeja KP 已提交
1690
	struct hdac_hdmi_pcm *pcm, *pcm_next;
1691

1692 1693
	snd_soc_unregister_codec(&edev->hdac.dev);

J
Jeeja KP 已提交
1694 1695 1696 1697 1698 1699 1700
	list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
		pcm->cvt = NULL;
		pcm->pin = NULL;
		list_del(&pcm->head);
		kfree(pcm);
	}

1701 1702
	list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
		list_del(&cvt->head);
J
Jeeja KP 已提交
1703
		kfree(cvt->name);
1704 1705 1706 1707 1708 1709 1710 1711
		kfree(cvt);
	}

	list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
		list_del(&pin->head);
		kfree(pin);
	}

1712 1713 1714
	return 0;
}

1715 1716 1717 1718 1719
#ifdef CONFIG_PM
static int hdac_hdmi_runtime_suspend(struct device *dev)
{
	struct hdac_ext_device *edev = to_hda_ext_device(dev);
	struct hdac_device *hdac = &edev->hdac;
1720
	struct hdac_bus *bus = hdac->bus;
1721 1722
	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
	struct hdac_ext_link *hlink = NULL;
1723
	int err;
1724 1725 1726

	dev_dbg(dev, "Enter: %s\n", __func__);

1727 1728 1729 1730
	/* controller may not have been initialized for the first time */
	if (!bus)
		return 0;

1731 1732 1733 1734 1735 1736 1737 1738 1739
	/*
	 * Power down afg.
	 * codec_read is preferred over codec_write to set the power state.
	 * This way verb is send to set the power state and response
	 * is received. So setting power state is ensured without using loop
	 * to read the state.
	 */
	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
							AC_PWRST_D3);
1740 1741 1742 1743 1744 1745
	err = snd_hdac_display_power(bus, false);
	if (err < 0) {
		dev_err(bus->dev, "Cannot turn on display power on i915\n");
		return err;
	}

1746 1747 1748
	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
	snd_hdac_ext_bus_link_put(ebus, hlink);

1749 1750 1751 1752 1753 1754 1755
	return 0;
}

static int hdac_hdmi_runtime_resume(struct device *dev)
{
	struct hdac_ext_device *edev = to_hda_ext_device(dev);
	struct hdac_device *hdac = &edev->hdac;
1756
	struct hdac_bus *bus = hdac->bus;
1757 1758
	struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
	struct hdac_ext_link *hlink = NULL;
1759
	int err;
1760 1761 1762

	dev_dbg(dev, "Enter: %s\n", __func__);

1763 1764 1765 1766
	/* controller may not have been initialized for the first time */
	if (!bus)
		return 0;

1767 1768 1769
	hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
	snd_hdac_ext_bus_link_get(ebus, hlink);

1770 1771 1772 1773 1774 1775
	err = snd_hdac_display_power(bus, true);
	if (err < 0) {
		dev_err(bus->dev, "Cannot turn on display power on i915\n");
		return err;
	}

1776 1777 1778
	hdac_hdmi_skl_enable_all_pins(&edev->hdac);
	hdac_hdmi_skl_enable_dp12(&edev->hdac);

1779
	/* Power up afg */
1780 1781
	snd_hdac_codec_read(hdac, hdac->afg, 0,	AC_VERB_SET_POWER_STATE,
							AC_PWRST_D0);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791

	return 0;
}
#else
#define hdac_hdmi_runtime_suspend NULL
#define hdac_hdmi_runtime_resume NULL
#endif

static const struct dev_pm_ops hdac_hdmi_pm = {
	SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1792
	.prepare = hdmi_codec_prepare,
1793
	.complete = hdmi_codec_complete,
1794 1795
};

1796 1797
static const struct hda_device_id hdmi_list[] = {
	HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1798
	HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1799 1800 1801 1802 1803 1804 1805 1806 1807
	{}
};

MODULE_DEVICE_TABLE(hdaudio, hdmi_list);

static struct hdac_ext_driver hdmi_driver = {
	. hdac = {
		.driver = {
			.name   = "HDMI HDA Codec",
1808
			.pm = &hdac_hdmi_pm,
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
		},
		.id_table       = hdmi_list,
	},
	.probe          = hdac_hdmi_dev_probe,
	.remove         = hdac_hdmi_dev_remove,
};

static int __init hdmi_init(void)
{
	return snd_hda_ext_driver_register(&hdmi_driver);
}

static void __exit hdmi_exit(void)
{
	snd_hda_ext_driver_unregister(&hdmi_driver);
}

module_init(hdmi_init);
module_exit(hdmi_exit);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("HDMI HD codec");
MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");