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