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