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