118382eadSSubhransu S. Prusty /* 218382eadSSubhransu S. Prusty * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms 318382eadSSubhransu S. Prusty * 418382eadSSubhransu S. Prusty * Copyright (C) 2014-2015 Intel Corp 518382eadSSubhransu S. Prusty * Author: Samreen Nilofer <samreen.nilofer@intel.com> 618382eadSSubhransu S. Prusty * Subhransu S. Prusty <subhransu.s.prusty@intel.com> 718382eadSSubhransu S. Prusty * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 818382eadSSubhransu S. Prusty * 918382eadSSubhransu S. Prusty * This program is free software; you can redistribute it and/or modify 1018382eadSSubhransu S. Prusty * it under the terms of the GNU General Public License as published by 1118382eadSSubhransu S. Prusty * the Free Software Foundation; version 2 of the License. 1218382eadSSubhransu S. Prusty * 1318382eadSSubhransu S. Prusty * This program is distributed in the hope that it will be useful, but 1418382eadSSubhransu S. Prusty * WITHOUT ANY WARRANTY; without even the implied warranty of 1518382eadSSubhransu S. Prusty * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1618382eadSSubhransu S. Prusty * General Public License for more details. 1718382eadSSubhransu S. Prusty * 1818382eadSSubhransu S. Prusty * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1918382eadSSubhransu S. Prusty */ 2018382eadSSubhransu S. Prusty #include <linux/init.h> 2118382eadSSubhransu S. Prusty #include <linux/delay.h> 2218382eadSSubhransu S. Prusty #include <linux/module.h> 2318382eadSSubhransu S. Prusty #include <linux/pm_runtime.h> 24a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h> 252428bca3SSubhransu S. Prusty #include <drm/drm_edid.h> 2618382eadSSubhransu S. Prusty #include <sound/pcm_params.h> 274a3478deSJeeja KP #include <sound/jack.h> 2818382eadSSubhransu S. Prusty #include <sound/soc.h> 2918382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h> 3007f083abSSubhransu S. Prusty #include <sound/hda_i915.h> 312428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h> 32bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h> 3318382eadSSubhransu S. Prusty #include "../../hda/local.h" 344a3478deSJeeja KP #include "hdac_hdmi.h" 3518382eadSSubhransu S. Prusty 3617a42c45SSubhransu S. Prusty #define NAME_SIZE 32 3717a42c45SSubhransu S. Prusty 38b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE 0xb080 39b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE 0xb000 4018382eadSSubhransu S. Prusty #define PIN_OUT (AC_PINCTL_OUT_EN) 41b0362adbSSubhransu S. Prusty 4218382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS 32 4318382eadSSubhransu S. Prusty 44148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS 3 45754695f9SJeeja KP #define HDA_MAX_PORTS 3 46148569fdSSubhransu S. Prusty 47b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE 256 48b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES 20 49b8a54545SSubhransu S. Prusty 50f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2 51f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31 52f6fa11a3SSandeep Tayal #define ELD_MAX_MNL 16 53f6fa11a3SSandeep Tayal 5418382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params { 5518382eadSSubhransu S. Prusty unsigned int channels_min; 5618382eadSSubhransu S. Prusty unsigned int channels_max; 5718382eadSSubhransu S. Prusty u32 rates; 5818382eadSSubhransu S. Prusty u64 formats; 5918382eadSSubhransu S. Prusty unsigned int maxbps; 6018382eadSSubhransu S. Prusty }; 6118382eadSSubhransu S. Prusty 6218382eadSSubhransu S. Prusty struct hdac_hdmi_cvt { 6315b91447SSubhransu S. Prusty struct list_head head; 6418382eadSSubhransu S. Prusty hda_nid_t nid; 654a3478deSJeeja KP const char *name; 6618382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params params; 6718382eadSSubhransu S. Prusty }; 6818382eadSSubhransu S. Prusty 69b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */ 70b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld { 71b7756edeSSubhransu S. Prusty u8 spk_alloc; 72b7756edeSSubhransu S. Prusty }; 73b7756edeSSubhransu S. Prusty 74b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld { 75b8a54545SSubhransu S. Prusty bool monitor_present; 76b8a54545SSubhransu S. Prusty bool eld_valid; 77b8a54545SSubhransu S. Prusty int eld_size; 78b8a54545SSubhransu S. Prusty char eld_buffer[ELD_MAX_SIZE]; 79b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld info; 80b8a54545SSubhransu S. Prusty }; 81b8a54545SSubhransu S. Prusty 8218382eadSSubhransu S. Prusty struct hdac_hdmi_pin { 8315b91447SSubhransu S. Prusty struct list_head head; 8418382eadSSubhransu S. Prusty hda_nid_t nid; 852acd8309SJeeja KP bool mst_capable; 86754695f9SJeeja KP struct hdac_hdmi_port *ports; 87754695f9SJeeja KP int num_ports; 88754695f9SJeeja KP struct hdac_ext_device *edev; 89754695f9SJeeja KP }; 90754695f9SJeeja KP 91754695f9SJeeja KP struct hdac_hdmi_port { 92754695f9SJeeja KP int id; 93754695f9SJeeja KP struct hdac_hdmi_pin *pin; 9418382eadSSubhransu S. Prusty int num_mux_nids; 9518382eadSSubhransu S. Prusty hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; 96b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld eld; 9718382eadSSubhransu S. Prusty }; 9818382eadSSubhransu S. Prusty 994a3478deSJeeja KP struct hdac_hdmi_pcm { 1004a3478deSJeeja KP struct list_head head; 1014a3478deSJeeja KP int pcm_id; 102754695f9SJeeja KP struct hdac_hdmi_port *port; 1034a3478deSJeeja KP struct hdac_hdmi_cvt *cvt; 1044a3478deSJeeja KP struct snd_jack *jack; 105c9bfb5d7SJeeja KP int stream_tag; 106c9bfb5d7SJeeja KP int channels; 107c9bfb5d7SJeeja KP int format; 108ab1eea19SJeeja KP bool chmap_set; 109ab1eea19SJeeja KP unsigned char chmap[8]; /* ALSA API channel-map */ 110ab1eea19SJeeja KP struct mutex lock; 1114a3478deSJeeja KP }; 1124a3478deSJeeja KP 113754695f9SJeeja KP struct hdac_hdmi_dai_port_map { 11418382eadSSubhransu S. Prusty int dai_id; 115754695f9SJeeja KP struct hdac_hdmi_port *port; 11615b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 11718382eadSSubhransu S. Prusty }; 11818382eadSSubhransu S. Prusty 11918382eadSSubhransu S. Prusty struct hdac_hdmi_priv { 120754695f9SJeeja KP struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS]; 12115b91447SSubhransu S. Prusty struct list_head pin_list; 12215b91447SSubhransu S. Prusty struct list_head cvt_list; 1234a3478deSJeeja KP struct list_head pcm_list; 12415b91447SSubhransu S. Prusty int num_pin; 12515b91447SSubhransu S. Prusty int num_cvt; 126754695f9SJeeja KP int num_ports; 1274a3478deSJeeja KP struct mutex pin_mutex; 128bcced704SSubhransu S. Prusty struct hdac_chmap chmap; 12918382eadSSubhransu S. Prusty }; 13018382eadSSubhransu S. Prusty 131c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm * 132c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi, 133c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt) 134c9bfb5d7SJeeja KP { 135c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 1361de777feSJeeja KP 137c9bfb5d7SJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 138c9bfb5d7SJeeja KP if (pcm->cvt == cvt) 139c9bfb5d7SJeeja KP break; 140c9bfb5d7SJeeja KP } 141c9bfb5d7SJeeja KP 142c9bfb5d7SJeeja KP return pcm; 143c9bfb5d7SJeeja KP } 1441de777feSJeeja KP 145fc181b04SJeeja KP /* MST supported verbs */ 146fc181b04SJeeja KP /* 147fc181b04SJeeja KP * Get the no devices that can be connected to a port on the Pin widget. 148fc181b04SJeeja KP */ 149fc181b04SJeeja KP static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid) 150fc181b04SJeeja KP { 151fc181b04SJeeja KP unsigned int caps; 152fc181b04SJeeja KP unsigned int type, param; 153fc181b04SJeeja KP 154fc181b04SJeeja KP caps = get_wcaps(&hdac->hdac, nid); 155fc181b04SJeeja KP type = get_wcaps_type(caps); 156fc181b04SJeeja KP 157fc181b04SJeeja KP if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN)) 158fc181b04SJeeja KP return 0; 159fc181b04SJeeja KP 160fc181b04SJeeja KP param = snd_hdac_read_parm_uncached(&hdac->hdac, nid, 161fc181b04SJeeja KP AC_PAR_DEVLIST_LEN); 162fc181b04SJeeja KP if (param == -1) 163fc181b04SJeeja KP return param; 164fc181b04SJeeja KP 165fc181b04SJeeja KP return param & AC_DEV_LIST_LEN_MASK; 166fc181b04SJeeja KP } 167fc181b04SJeeja KP 168fc181b04SJeeja KP /* 169fc181b04SJeeja KP * Get the port entry select on the pin. Return the port entry 170fc181b04SJeeja KP * id selected on the pin. Return 0 means the first port entry 171fc181b04SJeeja KP * is selected or MST is not supported. 172fc181b04SJeeja KP */ 173fc181b04SJeeja KP static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac, 174fc181b04SJeeja KP struct hdac_hdmi_port *port) 175fc181b04SJeeja KP { 176fc181b04SJeeja KP return snd_hdac_codec_read(&hdac->hdac, port->pin->nid, 177fc181b04SJeeja KP 0, AC_VERB_GET_DEVICE_SEL, 0); 178fc181b04SJeeja KP } 179fc181b04SJeeja KP 180fc181b04SJeeja KP /* 181fc181b04SJeeja KP * Sets the selected port entry for the configuring Pin widget verb. 182fc181b04SJeeja KP * returns error if port set is not equal to port get otherwise success 183fc181b04SJeeja KP */ 184fc181b04SJeeja KP static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac, 185fc181b04SJeeja KP struct hdac_hdmi_port *port) 186fc181b04SJeeja KP { 187fc181b04SJeeja KP int num_ports; 188fc181b04SJeeja KP 189fc181b04SJeeja KP if (!port->pin->mst_capable) 190fc181b04SJeeja KP return 0; 191fc181b04SJeeja KP 192fc181b04SJeeja KP /* AC_PAR_DEVLIST_LEN is 0 based. */ 193fc181b04SJeeja KP num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid); 194fc181b04SJeeja KP 195fc181b04SJeeja KP if (num_ports < 0) 196fc181b04SJeeja KP return -EIO; 197fc181b04SJeeja KP /* 198fc181b04SJeeja KP * Device List Length is a 0 based integer value indicating the 199fc181b04SJeeja KP * number of sink device that a MST Pin Widget can support. 200fc181b04SJeeja KP */ 201fc181b04SJeeja KP if (num_ports + 1 < port->id) 202fc181b04SJeeja KP return 0; 203fc181b04SJeeja KP 204fc181b04SJeeja KP snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0, 205fc181b04SJeeja KP AC_VERB_SET_DEVICE_SEL, port->id); 206fc181b04SJeeja KP 207fc181b04SJeeja KP if (port->id != hdac_hdmi_port_select_get(hdac, port)) 208fc181b04SJeeja KP return -EIO; 209fc181b04SJeeja KP 210fc181b04SJeeja KP dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id); 211fc181b04SJeeja KP 212fc181b04SJeeja KP return 0; 213fc181b04SJeeja KP } 214fc181b04SJeeja KP 2152889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi, 2162889099eSSubhransu S. Prusty int pcm_idx) 2172889099eSSubhransu S. Prusty { 2182889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 2192889099eSSubhransu S. Prusty 2202889099eSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 2212889099eSSubhransu S. Prusty if (pcm->pcm_id == pcm_idx) 2222889099eSSubhransu S. Prusty return pcm; 2232889099eSSubhransu S. Prusty } 2242889099eSSubhransu S. Prusty 2252889099eSSubhransu S. Prusty return NULL; 2262889099eSSubhransu S. Prusty } 2272889099eSSubhransu S. Prusty 228e342ac08SSubhransu S. Prusty static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev) 229e342ac08SSubhransu S. Prusty { 23051b2c425SGeliang Tang struct hdac_device *hdac = dev_to_hdac_dev(dev); 231e342ac08SSubhransu S. Prusty 23251b2c425SGeliang Tang return to_ehdac_device(hdac); 233e342ac08SSubhransu S. Prusty } 234e342ac08SSubhransu S. Prusty 2352428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad) 2362428bca3SSubhransu S. Prusty { 2372428bca3SSubhransu S. Prusty return ((sad[0] >> 0x3) & 0x1f); 2382428bca3SSubhransu S. Prusty } 2392428bca3SSubhransu S. Prusty 2402428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad) 2412428bca3SSubhransu S. Prusty { 2422428bca3SSubhransu S. Prusty return (sad[2] & 7); 2432428bca3SSubhransu S. Prusty } 2442428bca3SSubhransu S. Prusty 2452428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime, 2462428bca3SSubhransu S. Prusty void *eld) 2472428bca3SSubhransu S. Prusty { 2482428bca3SSubhransu S. Prusty u64 formats = SNDRV_PCM_FMTBIT_S16; 2492428bca3SSubhransu S. Prusty int i; 2502428bca3SSubhransu S. Prusty const u8 *sad, *eld_buf = eld; 2512428bca3SSubhransu S. Prusty 2522428bca3SSubhransu S. Prusty sad = drm_eld_sad(eld_buf); 2532428bca3SSubhransu S. Prusty if (!sad) 2542428bca3SSubhransu S. Prusty goto format_constraint; 2552428bca3SSubhransu S. Prusty 2562428bca3SSubhransu S. Prusty for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) { 2572428bca3SSubhransu S. Prusty if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */ 2582428bca3SSubhransu S. Prusty 2592428bca3SSubhransu S. Prusty /* 2602428bca3SSubhransu S. Prusty * the controller support 20 and 24 bits in 32 bit 2612428bca3SSubhransu S. Prusty * container so we set S32 2622428bca3SSubhransu S. Prusty */ 2632428bca3SSubhransu S. Prusty if (sad_sample_bits_lpcm(sad) & 0x6) 2642428bca3SSubhransu S. Prusty formats |= SNDRV_PCM_FMTBIT_S32; 2652428bca3SSubhransu S. Prusty } 2662428bca3SSubhransu S. Prusty } 2672428bca3SSubhransu S. Prusty 2682428bca3SSubhransu S. Prusty format_constraint: 2692428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, 2702428bca3SSubhransu S. Prusty formats); 2712428bca3SSubhransu S. Prusty 2722428bca3SSubhransu S. Prusty } 2732428bca3SSubhransu S. Prusty 274a657f1d0SSubhransu S. Prusty static void 275a657f1d0SSubhransu S. Prusty hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid, 276a657f1d0SSubhransu S. Prusty int packet_index, int byte_index) 277a657f1d0SSubhransu S. Prusty { 278a657f1d0SSubhransu S. Prusty int val; 279a657f1d0SSubhransu S. Prusty 280a657f1d0SSubhransu S. Prusty val = (packet_index << 5) | (byte_index & 0x1f); 281a657f1d0SSubhransu S. Prusty 282a657f1d0SSubhransu S. Prusty snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, 283a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_INDEX, val); 284a657f1d0SSubhransu S. Prusty } 285a657f1d0SSubhransu S. Prusty 286478f544eSSubhransu S. Prusty struct dp_audio_infoframe { 287478f544eSSubhransu S. Prusty u8 type; /* 0x84 */ 288478f544eSSubhransu S. Prusty u8 len; /* 0x1b */ 289478f544eSSubhransu S. Prusty u8 ver; /* 0x11 << 2 */ 290478f544eSSubhransu S. Prusty 291478f544eSSubhransu S. Prusty u8 CC02_CT47; /* match with HDMI infoframe from this on */ 292478f544eSSubhransu S. Prusty u8 SS01_SF24; 293478f544eSSubhransu S. Prusty u8 CXT04; 294478f544eSSubhransu S. Prusty u8 CA; 295478f544eSSubhransu S. Prusty u8 LFEPBL01_LSV36_DM_INH7; 296478f544eSSubhransu S. Prusty }; 297478f544eSSubhransu S. Prusty 298a657f1d0SSubhransu S. Prusty static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac, 299754695f9SJeeja KP struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port) 300a657f1d0SSubhransu S. Prusty { 301a657f1d0SSubhransu S. Prusty uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE]; 302a657f1d0SSubhransu S. Prusty struct hdmi_audio_infoframe frame; 303754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 304478f544eSSubhransu S. Prusty struct dp_audio_infoframe dp_ai; 305478f544eSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = hdac->private_data; 306ab1eea19SJeeja KP struct hdac_hdmi_cvt *cvt = pcm->cvt; 307478f544eSSubhransu S. Prusty u8 *dip; 308a657f1d0SSubhransu S. Prusty int ret; 309a657f1d0SSubhransu S. Prusty int i; 310478f544eSSubhransu S. Prusty const u8 *eld_buf; 311478f544eSSubhransu S. Prusty u8 conn_type; 312bcced704SSubhransu S. Prusty int channels, ca; 313a657f1d0SSubhransu S. Prusty 314754695f9SJeeja KP ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc, 315ab1eea19SJeeja KP pcm->channels, pcm->chmap_set, true, pcm->chmap); 316bcced704SSubhransu S. Prusty 317bcced704SSubhransu S. Prusty channels = snd_hdac_get_active_channels(ca); 318ab1eea19SJeeja KP hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels); 319bcced704SSubhransu S. Prusty 320bcced704SSubhransu S. Prusty snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca, 321ab1eea19SJeeja KP pcm->channels, pcm->chmap, pcm->chmap_set); 322bcced704SSubhransu S. Prusty 323754695f9SJeeja KP eld_buf = port->eld.eld_buffer; 324478f544eSSubhransu S. Prusty conn_type = drm_eld_get_conn_type(eld_buf); 325a657f1d0SSubhransu S. Prusty 326478f544eSSubhransu S. Prusty switch (conn_type) { 327478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_HDMI: 328478f544eSSubhransu S. Prusty hdmi_audio_infoframe_init(&frame); 329478f544eSSubhransu S. Prusty 330478f544eSSubhransu S. Prusty frame.channels = channels; 331bcced704SSubhransu S. Prusty frame.channel_allocation = ca; 332a657f1d0SSubhransu S. Prusty 333a657f1d0SSubhransu S. Prusty ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); 334a657f1d0SSubhransu S. Prusty if (ret < 0) 335a657f1d0SSubhransu S. Prusty return ret; 336a657f1d0SSubhransu S. Prusty 337478f544eSSubhransu S. Prusty break; 338478f544eSSubhransu S. Prusty 339478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_DP: 340478f544eSSubhransu S. Prusty memset(&dp_ai, 0, sizeof(dp_ai)); 341478f544eSSubhransu S. Prusty dp_ai.type = 0x84; 342478f544eSSubhransu S. Prusty dp_ai.len = 0x1b; 343478f544eSSubhransu S. Prusty dp_ai.ver = 0x11 << 2; 344478f544eSSubhransu S. Prusty dp_ai.CC02_CT47 = channels - 1; 345bcced704SSubhransu S. Prusty dp_ai.CA = ca; 346478f544eSSubhransu S. Prusty 347478f544eSSubhransu S. Prusty dip = (u8 *)&dp_ai; 348478f544eSSubhransu S. Prusty break; 349478f544eSSubhransu S. Prusty 350478f544eSSubhransu S. Prusty default: 351478f544eSSubhransu S. Prusty dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n", 352478f544eSSubhransu S. Prusty conn_type); 353478f544eSSubhransu S. Prusty return -EIO; 354478f544eSSubhransu S. Prusty } 355478f544eSSubhransu S. Prusty 356a657f1d0SSubhransu S. Prusty /* stop infoframe transmission */ 357ab1eea19SJeeja KP hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0); 358ab1eea19SJeeja KP snd_hdac_codec_write(&hdac->hdac, pin->nid, 0, 359a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE); 360a657f1d0SSubhransu S. Prusty 361a657f1d0SSubhransu S. Prusty 362a657f1d0SSubhransu S. Prusty /* Fill infoframe. Index auto-incremented */ 363ab1eea19SJeeja KP hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0); 364478f544eSSubhransu S. Prusty if (conn_type == DRM_ELD_CONN_TYPE_HDMI) { 365391005e8SSubhransu S. Prusty for (i = 0; i < sizeof(buffer); i++) 366ab1eea19SJeeja KP snd_hdac_codec_write(&hdac->hdac, pin->nid, 0, 367391005e8SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, buffer[i]); 368478f544eSSubhransu S. Prusty } else { 369478f544eSSubhransu S. Prusty for (i = 0; i < sizeof(dp_ai); i++) 370ab1eea19SJeeja KP snd_hdac_codec_write(&hdac->hdac, pin->nid, 0, 371478f544eSSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, dip[i]); 372478f544eSSubhransu S. Prusty } 373a657f1d0SSubhransu S. Prusty 374a657f1d0SSubhransu S. Prusty /* Start infoframe */ 375ab1eea19SJeeja KP hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0); 376ab1eea19SJeeja KP snd_hdac_codec_write(&hdac->hdac, pin->nid, 0, 377a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST); 378a657f1d0SSubhransu S. Prusty 379a657f1d0SSubhransu S. Prusty return 0; 380a657f1d0SSubhransu S. Prusty } 381a657f1d0SSubhransu S. Prusty 382c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai, 383c9bfb5d7SJeeja KP unsigned int tx_mask, unsigned int rx_mask, 384c9bfb5d7SJeeja KP int slots, int slot_width) 385b0362adbSSubhransu S. Prusty { 386c9bfb5d7SJeeja KP struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai); 387c9bfb5d7SJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 388754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 389c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 390c9bfb5d7SJeeja KP 391c9bfb5d7SJeeja KP dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask); 392b0362adbSSubhransu S. Prusty 393b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 394b0362adbSSubhransu S. Prusty 395c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 396b0362adbSSubhransu S. Prusty 397c9bfb5d7SJeeja KP if (pcm) 398c9bfb5d7SJeeja KP pcm->stream_tag = (tx_mask << 4); 399bcced704SSubhransu S. Prusty 400c9bfb5d7SJeeja KP return 0; 401b0362adbSSubhransu S. Prusty } 402b0362adbSSubhransu S. Prusty 403b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream, 404b0362adbSSubhransu S. Prusty struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai) 405b0362adbSSubhransu S. Prusty { 406b0362adbSSubhransu S. Prusty struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); 40754dfa1eaSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = hdac->private_data; 408754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 409754695f9SJeeja KP struct hdac_hdmi_port *port; 410c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 411c9bfb5d7SJeeja KP int format; 412b0362adbSSubhransu S. Prusty 41354dfa1eaSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 414754695f9SJeeja KP port = dai_map->port; 41554dfa1eaSSubhransu S. Prusty 416754695f9SJeeja KP if (!port) 41754dfa1eaSSubhransu S. Prusty return -ENODEV; 41854dfa1eaSSubhransu S. Prusty 419754695f9SJeeja KP if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) { 420754695f9SJeeja KP dev_err(&hdac->hdac.dev, 421754695f9SJeeja KP "device is not configured for this pin:port%d:%d\n", 422754695f9SJeeja KP port->pin->nid, port->id); 423b0362adbSSubhransu S. Prusty return -ENODEV; 424b0362adbSSubhransu S. Prusty } 425b0362adbSSubhransu S. Prusty 426c9bfb5d7SJeeja KP format = snd_hdac_calc_stream_format(params_rate(hparams), 427b0362adbSSubhransu S. Prusty params_channels(hparams), params_format(hparams), 428b0362adbSSubhransu S. Prusty 24, 0); 429b0362adbSSubhransu S. Prusty 430c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 431c9bfb5d7SJeeja KP if (!pcm) 432148569fdSSubhransu S. Prusty return -EIO; 433148569fdSSubhransu S. Prusty 434c9bfb5d7SJeeja KP pcm->format = format; 435c9bfb5d7SJeeja KP pcm->channels = params_channels(hparams); 436148569fdSSubhransu S. Prusty 437148569fdSSubhransu S. Prusty return 0; 438148569fdSSubhransu S. Prusty } 439148569fdSSubhransu S. Prusty 440754695f9SJeeja KP static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac, 441754695f9SJeeja KP struct hdac_hdmi_pin *pin, 442754695f9SJeeja KP struct hdac_hdmi_port *port) 443148569fdSSubhransu S. Prusty { 444148569fdSSubhransu S. Prusty if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) { 445148569fdSSubhransu S. Prusty dev_warn(&hdac->hdac.dev, 446148569fdSSubhransu S. Prusty "HDMI: pin %d wcaps %#x does not support connection list\n", 447148569fdSSubhransu S. Prusty pin->nid, get_wcaps(&hdac->hdac, pin->nid)); 448148569fdSSubhransu S. Prusty return -EINVAL; 449148569fdSSubhransu S. Prusty } 450148569fdSSubhransu S. Prusty 451*1b46ebd1SJeeja KP if (hdac_hdmi_port_select_set(hdac, port) < 0) 452*1b46ebd1SJeeja KP return -EIO; 453*1b46ebd1SJeeja KP 454754695f9SJeeja KP port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid, 455754695f9SJeeja KP port->mux_nids, HDA_MAX_CONNECTIONS); 456754695f9SJeeja KP if (port->num_mux_nids == 0) 457754695f9SJeeja KP dev_warn(&hdac->hdac.dev, 458754695f9SJeeja KP "No connections found for pin:port %d:%d\n", 459754695f9SJeeja KP pin->nid, port->id); 460148569fdSSubhransu S. Prusty 461754695f9SJeeja KP dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n", 462754695f9SJeeja KP port->num_mux_nids, pin->nid, port->id); 463148569fdSSubhransu S. Prusty 464754695f9SJeeja KP return port->num_mux_nids; 465148569fdSSubhransu S. Prusty } 466148569fdSSubhransu S. Prusty 467148569fdSSubhransu S. Prusty /* 468754695f9SJeeja KP * Query pcm list and return port to which stream is routed. 469148569fdSSubhransu S. Prusty * 470754695f9SJeeja KP * Also query connection list of the pin, to validate the cvt to port map. 471148569fdSSubhransu S. Prusty * 472754695f9SJeeja KP * Same stream rendering to multiple ports simultaneously can be done 473754695f9SJeeja KP * possibly, but not supported for now in driver. So return the first port 474148569fdSSubhransu S. Prusty * connected. 475148569fdSSubhransu S. Prusty */ 476754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt( 477148569fdSSubhransu S. Prusty struct hdac_ext_device *edev, 478148569fdSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, 479148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt) 480148569fdSSubhransu S. Prusty { 481148569fdSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 482754695f9SJeeja KP struct hdac_hdmi_port *port = NULL; 483148569fdSSubhransu S. Prusty int ret, i; 484148569fdSSubhransu S. Prusty 485148569fdSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 486148569fdSSubhransu S. Prusty if (pcm->cvt == cvt) { 487754695f9SJeeja KP port = pcm->port; 488148569fdSSubhransu S. Prusty break; 489148569fdSSubhransu S. Prusty } 490148569fdSSubhransu S. Prusty } 491148569fdSSubhransu S. Prusty 492754695f9SJeeja KP if (port) { 493754695f9SJeeja KP ret = hdac_hdmi_query_port_connlist(edev, port->pin, port); 494148569fdSSubhransu S. Prusty if (ret < 0) 495148569fdSSubhransu S. Prusty return NULL; 496148569fdSSubhransu S. Prusty 497754695f9SJeeja KP for (i = 0; i < port->num_mux_nids; i++) { 498754695f9SJeeja KP if (port->mux_nids[i] == cvt->nid) 499754695f9SJeeja KP return port; 500148569fdSSubhransu S. Prusty } 501148569fdSSubhransu S. Prusty } 502148569fdSSubhransu S. Prusty 503148569fdSSubhransu S. Prusty return NULL; 504148569fdSSubhransu S. Prusty } 505148569fdSSubhransu S. Prusty 50654dfa1eaSSubhransu S. Prusty /* 50754dfa1eaSSubhransu S. Prusty * This tries to get a valid pin and set the HW constraints based on the 50854dfa1eaSSubhransu S. Prusty * ELD. Even if a valid pin is not found return success so that device open 50954dfa1eaSSubhransu S. Prusty * doesn't fail. 51054dfa1eaSSubhransu S. Prusty */ 511b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream, 512b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 513b0362adbSSubhransu S. Prusty { 514b0362adbSSubhransu S. Prusty struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); 515b0362adbSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = hdac->private_data; 516754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 517148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 518754695f9SJeeja KP struct hdac_hdmi_port *port; 5192428bca3SSubhransu S. Prusty int ret; 520b0362adbSSubhransu S. Prusty 521b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 522b0362adbSSubhransu S. Prusty 523148569fdSSubhransu S. Prusty cvt = dai_map->cvt; 524754695f9SJeeja KP port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt); 52554dfa1eaSSubhransu S. Prusty 52654dfa1eaSSubhransu S. Prusty /* 52754dfa1eaSSubhransu S. Prusty * To make PA and other userland happy. 52854dfa1eaSSubhransu S. Prusty * userland scans devices so returning error does not help. 52954dfa1eaSSubhransu S. Prusty */ 530754695f9SJeeja KP if (!port) 53154dfa1eaSSubhransu S. Prusty return 0; 532148569fdSSubhransu S. Prusty 533754695f9SJeeja KP if ((!port->eld.monitor_present) || 534754695f9SJeeja KP (!port->eld.eld_valid)) { 535b0362adbSSubhransu S. Prusty 53654dfa1eaSSubhransu S. Prusty dev_warn(&hdac->hdac.dev, 537754695f9SJeeja KP "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n", 538754695f9SJeeja KP port->eld.monitor_present, port->eld.eld_valid, 539754695f9SJeeja KP port->pin->nid, port->id); 540b8a54545SSubhransu S. Prusty 54154dfa1eaSSubhransu S. Prusty return 0; 542b0362adbSSubhransu S. Prusty } 543b0362adbSSubhransu S. Prusty 544754695f9SJeeja KP dai_map->port = port; 545b0362adbSSubhransu S. Prusty 5462428bca3SSubhransu S. Prusty ret = hdac_hdmi_eld_limit_formats(substream->runtime, 547754695f9SJeeja KP port->eld.eld_buffer); 5482428bca3SSubhransu S. Prusty if (ret < 0) 5492428bca3SSubhransu S. Prusty return ret; 550b0362adbSSubhransu S. Prusty 5512428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_eld(substream->runtime, 552754695f9SJeeja KP port->eld.eld_buffer); 553b0362adbSSubhransu S. Prusty } 554b0362adbSSubhransu S. Prusty 555b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream, 556b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 557b0362adbSSubhransu S. Prusty { 558b0362adbSSubhransu S. Prusty struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); 559b0362adbSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = hdac->private_data; 560754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 561ab1eea19SJeeja KP struct hdac_hdmi_pcm *pcm; 562b0362adbSSubhransu S. Prusty 563b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 564b0362adbSSubhransu S. Prusty 565ab1eea19SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 566bcced704SSubhransu S. Prusty 567ab1eea19SJeeja KP if (pcm) { 568ab1eea19SJeeja KP mutex_lock(&pcm->lock); 569ab1eea19SJeeja KP pcm->chmap_set = false; 570ab1eea19SJeeja KP memset(pcm->chmap, 0, sizeof(pcm->chmap)); 571ab1eea19SJeeja KP pcm->channels = 0; 572ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 573b0362adbSSubhransu S. Prusty } 574ab1eea19SJeeja KP 575754695f9SJeeja KP if (dai_map->port) 576754695f9SJeeja KP dai_map->port = NULL; 57754dfa1eaSSubhransu S. Prusty } 578b0362adbSSubhransu S. Prusty 57918382eadSSubhransu S. Prusty static int 58018382eadSSubhransu S. Prusty hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt) 58118382eadSSubhransu S. Prusty { 582bcced704SSubhransu S. Prusty unsigned int chans; 583bcced704SSubhransu S. Prusty struct hdac_ext_device *edev = to_ehdac_device(hdac); 584bcced704SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 58518382eadSSubhransu S. Prusty int err; 58618382eadSSubhransu S. Prusty 587bcced704SSubhransu S. Prusty chans = get_wcaps(hdac, cvt->nid); 588bcced704SSubhransu S. Prusty chans = get_wcaps_channels(chans); 589bcced704SSubhransu S. Prusty 590bcced704SSubhransu S. Prusty cvt->params.channels_min = 2; 591bcced704SSubhransu S. Prusty 592bcced704SSubhransu S. Prusty cvt->params.channels_max = chans; 593bcced704SSubhransu S. Prusty if (chans > hdmi->chmap.channels_max) 594bcced704SSubhransu S. Prusty hdmi->chmap.channels_max = chans; 59518382eadSSubhransu S. Prusty 59618382eadSSubhransu S. Prusty err = snd_hdac_query_supported_pcm(hdac, cvt->nid, 59718382eadSSubhransu S. Prusty &cvt->params.rates, 59818382eadSSubhransu S. Prusty &cvt->params.formats, 59918382eadSSubhransu S. Prusty &cvt->params.maxbps); 60018382eadSSubhransu S. Prusty if (err < 0) 60118382eadSSubhransu S. Prusty dev_err(&hdac->dev, 60218382eadSSubhransu S. Prusty "Failed to query pcm params for nid %d: %d\n", 60318382eadSSubhransu S. Prusty cvt->nid, err); 60418382eadSSubhransu S. Prusty 60518382eadSSubhransu S. Prusty return err; 60618382eadSSubhransu S. Prusty } 60718382eadSSubhransu S. Prusty 60879f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev, 609c9bfb5d7SJeeja KP struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id, 610c9bfb5d7SJeeja KP void *priv, const char *wname, const char *stream, 611c9bfb5d7SJeeja KP struct snd_kcontrol_new *wc, int numkc, 612c9bfb5d7SJeeja KP int (*event)(struct snd_soc_dapm_widget *, 613c9bfb5d7SJeeja KP struct snd_kcontrol *, int), unsigned short event_flags) 61418382eadSSubhransu S. Prusty { 61518382eadSSubhransu S. Prusty w->id = id; 61679f4e922SSubhransu S. Prusty w->name = devm_kstrdup(dev, wname, GFP_KERNEL); 61779f4e922SSubhransu S. Prusty if (!w->name) 61879f4e922SSubhransu S. Prusty return -ENOMEM; 61979f4e922SSubhransu S. Prusty 62018382eadSSubhransu S. Prusty w->sname = stream; 62118382eadSSubhransu S. Prusty w->reg = SND_SOC_NOPM; 62218382eadSSubhransu S. Prusty w->shift = 0; 62379f4e922SSubhransu S. Prusty w->kcontrol_news = wc; 62479f4e922SSubhransu S. Prusty w->num_kcontrols = numkc; 62579f4e922SSubhransu S. Prusty w->priv = priv; 626c9bfb5d7SJeeja KP w->event = event; 627c9bfb5d7SJeeja KP w->event_flags = event_flags; 62879f4e922SSubhransu S. Prusty 62979f4e922SSubhransu S. Prusty return 0; 63018382eadSSubhransu S. Prusty } 63118382eadSSubhransu S. Prusty 63218382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route, 63379f4e922SSubhransu S. Prusty const char *sink, const char *control, const char *src, 63479f4e922SSubhransu S. Prusty int (*handler)(struct snd_soc_dapm_widget *src, 63579f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *sink)) 63618382eadSSubhransu S. Prusty { 63718382eadSSubhransu S. Prusty route->sink = sink; 63818382eadSSubhransu S. Prusty route->source = src; 63918382eadSSubhransu S. Prusty route->control = control; 64079f4e922SSubhransu S. Prusty route->connected = handler; 64118382eadSSubhransu S. Prusty } 64218382eadSSubhransu S. Prusty 6434a3478deSJeeja KP static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev, 644754695f9SJeeja KP struct hdac_hdmi_port *port) 6454a3478deSJeeja KP { 6464a3478deSJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 6474a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 6484a3478deSJeeja KP 6494a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 650754695f9SJeeja KP if (!pcm->port) 651754695f9SJeeja KP continue; 652754695f9SJeeja KP 653754695f9SJeeja KP if (pcm->port == port) 6544a3478deSJeeja KP return pcm; 6554a3478deSJeeja KP } 6564a3478deSJeeja KP 6574a3478deSJeeja KP return NULL; 6584a3478deSJeeja KP } 6594a3478deSJeeja KP 660c9bfb5d7SJeeja KP static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev, 661c9bfb5d7SJeeja KP hda_nid_t nid, unsigned int pwr_state) 662c9bfb5d7SJeeja KP { 663c9bfb5d7SJeeja KP if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) { 664c9bfb5d7SJeeja KP if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state)) 665c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, nid, 0, 666c9bfb5d7SJeeja KP AC_VERB_SET_POWER_STATE, pwr_state); 667c9bfb5d7SJeeja KP } 668c9bfb5d7SJeeja KP } 669c9bfb5d7SJeeja KP 670c9bfb5d7SJeeja KP static void hdac_hdmi_set_amp(struct hdac_ext_device *edev, 671c9bfb5d7SJeeja KP hda_nid_t nid, int val) 672c9bfb5d7SJeeja KP { 673c9bfb5d7SJeeja KP if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP) 674c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, nid, 0, 675c9bfb5d7SJeeja KP AC_VERB_SET_AMP_GAIN_MUTE, val); 676c9bfb5d7SJeeja KP } 677c9bfb5d7SJeeja KP 678c9bfb5d7SJeeja KP 679c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w, 680c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 681c9bfb5d7SJeeja KP { 682754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 683c9bfb5d7SJeeja KP struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev); 684c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 685c9bfb5d7SJeeja KP 686c9bfb5d7SJeeja KP dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n", 687c9bfb5d7SJeeja KP __func__, w->name, event); 688c9bfb5d7SJeeja KP 689754695f9SJeeja KP pcm = hdac_hdmi_get_pcm(edev, port); 690c9bfb5d7SJeeja KP if (!pcm) 691c9bfb5d7SJeeja KP return -EIO; 692c9bfb5d7SJeeja KP 693*1b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 694*1b46ebd1SJeeja KP if (hdac_hdmi_port_select_set(edev, port) < 0) 695*1b46ebd1SJeeja KP return -EIO; 696*1b46ebd1SJeeja KP 697c9bfb5d7SJeeja KP switch (event) { 698c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 699754695f9SJeeja KP hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0); 700c9bfb5d7SJeeja KP 701c9bfb5d7SJeeja KP /* Enable out path for this pin widget */ 702754695f9SJeeja KP snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0, 703c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); 704c9bfb5d7SJeeja KP 705754695f9SJeeja KP hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE); 706c9bfb5d7SJeeja KP 707754695f9SJeeja KP return hdac_hdmi_setup_audio_infoframe(edev, pcm, port); 708c9bfb5d7SJeeja KP 709c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 710754695f9SJeeja KP hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE); 711c9bfb5d7SJeeja KP 712c9bfb5d7SJeeja KP /* Disable out path for this pin widget */ 713754695f9SJeeja KP snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0, 714c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, 0); 715c9bfb5d7SJeeja KP 716754695f9SJeeja KP hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3); 717c9bfb5d7SJeeja KP break; 718c9bfb5d7SJeeja KP 719c9bfb5d7SJeeja KP } 720c9bfb5d7SJeeja KP 721c9bfb5d7SJeeja KP return 0; 722c9bfb5d7SJeeja KP } 723c9bfb5d7SJeeja KP 724c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w, 725c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 726c9bfb5d7SJeeja KP { 727c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt = w->priv; 728c9bfb5d7SJeeja KP struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev); 729c9bfb5d7SJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 730c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 731c9bfb5d7SJeeja KP 732c9bfb5d7SJeeja KP dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n", 733c9bfb5d7SJeeja KP __func__, w->name, event); 734c9bfb5d7SJeeja KP 735c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt); 736c9bfb5d7SJeeja KP if (!pcm) 737c9bfb5d7SJeeja KP return -EIO; 738c9bfb5d7SJeeja KP 739c9bfb5d7SJeeja KP switch (event) { 740c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 741c9bfb5d7SJeeja KP hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0); 742c9bfb5d7SJeeja KP 743c9bfb5d7SJeeja KP /* Enable transmission */ 744c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 745c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_1, 1); 746c9bfb5d7SJeeja KP 747c9bfb5d7SJeeja KP /* Category Code (CC) to zero */ 748c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 749c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_2, 0); 750c9bfb5d7SJeeja KP 751c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 752c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag); 753c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 754c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, pcm->format); 755c9bfb5d7SJeeja KP break; 756c9bfb5d7SJeeja KP 757c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 758c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 759c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, 0); 760c9bfb5d7SJeeja KP snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 761c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, 0); 762c9bfb5d7SJeeja KP 763c9bfb5d7SJeeja KP hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3); 764c9bfb5d7SJeeja KP break; 765c9bfb5d7SJeeja KP 766c9bfb5d7SJeeja KP } 767c9bfb5d7SJeeja KP 768c9bfb5d7SJeeja KP return 0; 769c9bfb5d7SJeeja KP } 770c9bfb5d7SJeeja KP 771c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w, 772c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 773c9bfb5d7SJeeja KP { 774754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 775c9bfb5d7SJeeja KP struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev); 776c9bfb5d7SJeeja KP int mux_idx; 777c9bfb5d7SJeeja KP 778c9bfb5d7SJeeja KP dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n", 779c9bfb5d7SJeeja KP __func__, w->name, event); 780c9bfb5d7SJeeja KP 781c9bfb5d7SJeeja KP if (!kc) 782c9bfb5d7SJeeja KP kc = w->kcontrols[0]; 783c9bfb5d7SJeeja KP 784c9bfb5d7SJeeja KP mux_idx = dapm_kcontrol_get_value(kc); 785*1b46ebd1SJeeja KP 786*1b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 787*1b46ebd1SJeeja KP if (hdac_hdmi_port_select_set(edev, port) < 0) 788*1b46ebd1SJeeja KP return -EIO; 789*1b46ebd1SJeeja KP 790c9bfb5d7SJeeja KP if (mux_idx > 0) { 791754695f9SJeeja KP snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0, 792c9bfb5d7SJeeja KP AC_VERB_SET_CONNECT_SEL, (mux_idx - 1)); 793c9bfb5d7SJeeja KP } 794c9bfb5d7SJeeja KP 795c9bfb5d7SJeeja KP return 0; 796c9bfb5d7SJeeja KP } 797c9bfb5d7SJeeja KP 7984a3478deSJeeja KP /* 7994a3478deSJeeja KP * Based on user selection, map the PINs with the PCMs. 8004a3478deSJeeja KP */ 801754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol, 8024a3478deSJeeja KP struct snd_ctl_elem_value *ucontrol) 8034a3478deSJeeja KP { 8044a3478deSJeeja KP int ret; 8054a3478deSJeeja KP struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 8064a3478deSJeeja KP struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 8074a3478deSJeeja KP struct snd_soc_dapm_context *dapm = w->dapm; 808754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 8094a3478deSJeeja KP struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev); 8104a3478deSJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 8114a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 8124a3478deSJeeja KP const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]]; 8134a3478deSJeeja KP 8144a3478deSJeeja KP ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 8154a3478deSJeeja KP if (ret < 0) 8164a3478deSJeeja KP return ret; 8174a3478deSJeeja KP 818754695f9SJeeja KP if (port == NULL) 819754695f9SJeeja KP return -EINVAL; 820754695f9SJeeja KP 8214a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 8224a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 823754695f9SJeeja KP if (!pcm->port && pcm->port == port && 824754695f9SJeeja KP pcm->port->id == port->id) 825754695f9SJeeja KP pcm->port = NULL; 8264a3478deSJeeja KP 8274a3478deSJeeja KP /* 8284a3478deSJeeja KP * Jack status is not reported during device probe as the 8294a3478deSJeeja KP * PCMs are not registered by then. So report it here. 8304a3478deSJeeja KP */ 831754695f9SJeeja KP if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->port) { 832754695f9SJeeja KP pcm->port = port; 833754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 8344a3478deSJeeja KP dev_dbg(&edev->hdac.dev, 8354a3478deSJeeja KP "jack report for pcm=%d\n", 8364a3478deSJeeja KP pcm->pcm_id); 8374a3478deSJeeja KP 8384a3478deSJeeja KP snd_jack_report(pcm->jack, SND_JACK_AVOUT); 8394a3478deSJeeja KP } 8404a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 8414a3478deSJeeja KP return ret; 8424a3478deSJeeja KP } 8434a3478deSJeeja KP } 8444a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 8454a3478deSJeeja KP 8464a3478deSJeeja KP return ret; 8474a3478deSJeeja KP } 8484a3478deSJeeja KP 84979f4e922SSubhransu S. Prusty /* 85079f4e922SSubhransu S. Prusty * Ideally the Mux inputs should be based on the num_muxs enumerated, but 85179f4e922SSubhransu S. Prusty * the display driver seem to be programming the connection list for the pin 85279f4e922SSubhransu S. Prusty * widget runtime. 85379f4e922SSubhransu S. Prusty * 85479f4e922SSubhransu S. Prusty * So programming all the possible inputs for the mux, the user has to take 85579f4e922SSubhransu S. Prusty * care of selecting the right one and leaving all other inputs selected to 85679f4e922SSubhransu S. Prusty * "NONE" 85779f4e922SSubhransu S. Prusty */ 858754695f9SJeeja KP static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev, 859754695f9SJeeja KP struct hdac_hdmi_port *port, 86079f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widget, 86179f4e922SSubhransu S. Prusty const char *widget_name) 86218382eadSSubhransu S. Prusty { 86379f4e922SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 864754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 86579f4e922SSubhransu S. Prusty struct snd_kcontrol_new *kc; 86679f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 86779f4e922SSubhransu S. Prusty struct soc_enum *se; 86879f4e922SSubhransu S. Prusty char kc_name[NAME_SIZE]; 86979f4e922SSubhransu S. Prusty char mux_items[NAME_SIZE]; 87079f4e922SSubhransu S. Prusty /* To hold inputs to the Pin mux */ 87179f4e922SSubhransu S. Prusty char *items[HDA_MAX_CONNECTIONS]; 87279f4e922SSubhransu S. Prusty int i = 0; 87379f4e922SSubhransu S. Prusty int num_items = hdmi->num_cvt + 1; 87418382eadSSubhransu S. Prusty 87579f4e922SSubhransu S. Prusty kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL); 87679f4e922SSubhransu S. Prusty if (!kc) 87779f4e922SSubhransu S. Prusty return -ENOMEM; 87818382eadSSubhransu S. Prusty 87979f4e922SSubhransu S. Prusty se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL); 88079f4e922SSubhransu S. Prusty if (!se) 88179f4e922SSubhransu S. Prusty return -ENOMEM; 88218382eadSSubhransu S. Prusty 883754695f9SJeeja KP sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id); 88479f4e922SSubhransu S. Prusty kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL); 88579f4e922SSubhransu S. Prusty if (!kc->name) 88679f4e922SSubhransu S. Prusty return -ENOMEM; 88718382eadSSubhransu S. Prusty 88879f4e922SSubhransu S. Prusty kc->private_value = (long)se; 88979f4e922SSubhransu S. Prusty kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 89079f4e922SSubhransu S. Prusty kc->access = 0; 89179f4e922SSubhransu S. Prusty kc->info = snd_soc_info_enum_double; 892754695f9SJeeja KP kc->put = hdac_hdmi_set_pin_port_mux; 89379f4e922SSubhransu S. Prusty kc->get = snd_soc_dapm_get_enum_double; 89479f4e922SSubhransu S. Prusty 89579f4e922SSubhransu S. Prusty se->reg = SND_SOC_NOPM; 89679f4e922SSubhransu S. Prusty 89779f4e922SSubhransu S. Prusty /* enum texts: ["NONE", "cvt #", "cvt #", ...] */ 89879f4e922SSubhransu S. Prusty se->items = num_items; 89979f4e922SSubhransu S. Prusty se->mask = roundup_pow_of_two(se->items) - 1; 90079f4e922SSubhransu S. Prusty 90179f4e922SSubhransu S. Prusty sprintf(mux_items, "NONE"); 90279f4e922SSubhransu S. Prusty items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL); 90379f4e922SSubhransu S. Prusty if (!items[i]) 90479f4e922SSubhransu S. Prusty return -ENOMEM; 90579f4e922SSubhransu S. Prusty 90679f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 90779f4e922SSubhransu S. Prusty i++; 90879f4e922SSubhransu S. Prusty sprintf(mux_items, "cvt %d", cvt->nid); 90979f4e922SSubhransu S. Prusty items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL); 91079f4e922SSubhransu S. Prusty if (!items[i]) 91179f4e922SSubhransu S. Prusty return -ENOMEM; 91279f4e922SSubhransu S. Prusty } 91379f4e922SSubhransu S. Prusty 91479f4e922SSubhransu S. Prusty se->texts = devm_kmemdup(&edev->hdac.dev, items, 91579f4e922SSubhransu S. Prusty (num_items * sizeof(char *)), GFP_KERNEL); 91679f4e922SSubhransu S. Prusty if (!se->texts) 91779f4e922SSubhransu S. Prusty return -ENOMEM; 91879f4e922SSubhransu S. Prusty 91979f4e922SSubhransu S. Prusty return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget, 920754695f9SJeeja KP snd_soc_dapm_mux, port, widget_name, NULL, kc, 1, 921c9bfb5d7SJeeja KP hdac_hdmi_pin_mux_widget_event, 922c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG); 92379f4e922SSubhransu S. Prusty } 92479f4e922SSubhransu S. Prusty 92579f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */ 92679f4e922SSubhransu S. Prusty static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev, 92779f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets, 92879f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route, int rindex) 92979f4e922SSubhransu S. Prusty { 93079f4e922SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 93179f4e922SSubhransu S. Prusty const struct snd_kcontrol_new *kc; 93279f4e922SSubhransu S. Prusty struct soc_enum *se; 933754695f9SJeeja KP int mux_index = hdmi->num_cvt + hdmi->num_ports; 93479f4e922SSubhransu S. Prusty int i, j; 93579f4e922SSubhransu S. Prusty 936754695f9SJeeja KP for (i = 0; i < hdmi->num_ports; i++) { 93779f4e922SSubhransu S. Prusty kc = widgets[mux_index].kcontrol_news; 93879f4e922SSubhransu S. Prusty se = (struct soc_enum *)kc->private_value; 93979f4e922SSubhransu S. Prusty for (j = 0; j < hdmi->num_cvt; j++) { 94079f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[rindex], 94179f4e922SSubhransu S. Prusty widgets[mux_index].name, 94279f4e922SSubhransu S. Prusty se->texts[j + 1], 94379f4e922SSubhransu S. Prusty widgets[j].name, NULL); 94479f4e922SSubhransu S. Prusty 94579f4e922SSubhransu S. Prusty rindex++; 94679f4e922SSubhransu S. Prusty } 94779f4e922SSubhransu S. Prusty 94879f4e922SSubhransu S. Prusty mux_index++; 94979f4e922SSubhransu S. Prusty } 95079f4e922SSubhransu S. Prusty } 95179f4e922SSubhransu S. Prusty 95279f4e922SSubhransu S. Prusty /* 95379f4e922SSubhransu S. Prusty * Widgets are added in the below sequence 95479f4e922SSubhransu S. Prusty * Converter widgets for num converters enumerated 955754695f9SJeeja KP * Pin-port widgets for num ports for Pins enumerated 956754695f9SJeeja KP * Pin-port mux widgets to represent connenction list of pin widget 95779f4e922SSubhransu S. Prusty * 958754695f9SJeeja KP * For each port, one Mux and One output widget is added 959754695f9SJeeja KP * Total widgets elements = num_cvt + (num_ports * 2); 96079f4e922SSubhransu S. Prusty * 96179f4e922SSubhransu S. Prusty * Routes are added as below: 962754695f9SJeeja KP * pin-port mux -> pin (based on num_ports) 963754695f9SJeeja KP * cvt -> "Input sel control" -> pin-port_mux 96479f4e922SSubhransu S. Prusty * 96579f4e922SSubhransu S. Prusty * Total route elements: 966754695f9SJeeja KP * num_ports + (pin_muxes * num_cvt) 96779f4e922SSubhransu S. Prusty */ 96879f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm) 96979f4e922SSubhransu S. Prusty { 97079f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets; 97179f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route; 97279f4e922SSubhransu S. Prusty struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev); 97379f4e922SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 97479f4e922SSubhransu S. Prusty struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv; 97579f4e922SSubhransu S. Prusty char widget_name[NAME_SIZE]; 97679f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 97779f4e922SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 978754695f9SJeeja KP int ret, i = 0, num_routes = 0, j; 97979f4e922SSubhransu S. Prusty 98079f4e922SSubhransu S. Prusty if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list)) 98179f4e922SSubhransu S. Prusty return -EINVAL; 98279f4e922SSubhransu S. Prusty 983754695f9SJeeja KP widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) * 984754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)), 98579f4e922SSubhransu S. Prusty GFP_KERNEL); 98679f4e922SSubhransu S. Prusty 98779f4e922SSubhransu S. Prusty if (!widgets) 98879f4e922SSubhransu S. Prusty return -ENOMEM; 98979f4e922SSubhransu S. Prusty 99079f4e922SSubhransu S. Prusty /* DAPM widgets to represent each converter widget */ 99179f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 99279f4e922SSubhransu S. Prusty sprintf(widget_name, "Converter %d", cvt->nid); 99379f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 994c9bfb5d7SJeeja KP snd_soc_dapm_aif_in, cvt, 995c9bfb5d7SJeeja KP widget_name, dai_drv[i].playback.stream_name, NULL, 0, 996c9bfb5d7SJeeja KP hdac_hdmi_cvt_output_widget_event, 997c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD); 99879f4e922SSubhransu S. Prusty if (ret < 0) 99979f4e922SSubhransu S. Prusty return ret; 100079f4e922SSubhransu S. Prusty i++; 100179f4e922SSubhransu S. Prusty } 100279f4e922SSubhransu S. Prusty 100379f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1004754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1005754695f9SJeeja KP sprintf(widget_name, "hif%d-%d Output", 1006754695f9SJeeja KP pin->nid, pin->ports[j].id); 100779f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 1008754695f9SJeeja KP snd_soc_dapm_output, &pin->ports[j], 1009c9bfb5d7SJeeja KP widget_name, NULL, NULL, 0, 1010c9bfb5d7SJeeja KP hdac_hdmi_pin_output_widget_event, 1011754695f9SJeeja KP SND_SOC_DAPM_PRE_PMU | 1012754695f9SJeeja KP SND_SOC_DAPM_POST_PMD); 101379f4e922SSubhransu S. Prusty if (ret < 0) 101479f4e922SSubhransu S. Prusty return ret; 101579f4e922SSubhransu S. Prusty i++; 101679f4e922SSubhransu S. Prusty } 1017754695f9SJeeja KP } 101879f4e922SSubhransu S. Prusty 101979f4e922SSubhransu S. Prusty /* DAPM widgets to represent the connection list to pin widget */ 102079f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1021754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1022754695f9SJeeja KP sprintf(widget_name, "Pin%d-Port%d Mux", 1023754695f9SJeeja KP pin->nid, pin->ports[j].id); 1024754695f9SJeeja KP ret = hdac_hdmi_create_pin_port_muxs(edev, 1025754695f9SJeeja KP &pin->ports[j], &widgets[i], 102679f4e922SSubhransu S. Prusty widget_name); 102779f4e922SSubhransu S. Prusty if (ret < 0) 102879f4e922SSubhransu S. Prusty return ret; 102979f4e922SSubhransu S. Prusty i++; 103079f4e922SSubhransu S. Prusty 103179f4e922SSubhransu S. Prusty /* For cvt to pin_mux mapping */ 103279f4e922SSubhransu S. Prusty num_routes += hdmi->num_cvt; 103379f4e922SSubhransu S. Prusty 103479f4e922SSubhransu S. Prusty /* For pin_mux to pin mapping */ 103579f4e922SSubhransu S. Prusty num_routes++; 103679f4e922SSubhransu S. Prusty } 1037754695f9SJeeja KP } 103879f4e922SSubhransu S. Prusty 103979f4e922SSubhransu S. Prusty route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes), 104079f4e922SSubhransu S. Prusty GFP_KERNEL); 104179f4e922SSubhransu S. Prusty if (!route) 104279f4e922SSubhransu S. Prusty return -ENOMEM; 104379f4e922SSubhransu S. Prusty 104479f4e922SSubhransu S. Prusty i = 0; 104579f4e922SSubhransu S. Prusty /* Add pin <- NULL <- mux route map */ 104679f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1047754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 104879f4e922SSubhransu S. Prusty int sink_index = i + hdmi->num_cvt; 1049754695f9SJeeja KP int src_index = sink_index + pin->num_ports * 1050754695f9SJeeja KP hdmi->num_pin; 105179f4e922SSubhransu S. Prusty 105279f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[i], 105379f4e922SSubhransu S. Prusty widgets[sink_index].name, NULL, 105479f4e922SSubhransu S. Prusty widgets[src_index].name, NULL); 105579f4e922SSubhransu S. Prusty i++; 1056754695f9SJeeja KP } 105779f4e922SSubhransu S. Prusty } 105879f4e922SSubhransu S. Prusty 105979f4e922SSubhransu S. Prusty hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i); 106079f4e922SSubhransu S. Prusty 106179f4e922SSubhransu S. Prusty snd_soc_dapm_new_controls(dapm, widgets, 1062754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)); 106379f4e922SSubhransu S. Prusty 106479f4e922SSubhransu S. Prusty snd_soc_dapm_add_routes(dapm, route, num_routes); 106579f4e922SSubhransu S. Prusty snd_soc_dapm_new_widgets(dapm->card); 106679f4e922SSubhransu S. Prusty 106779f4e922SSubhransu S. Prusty return 0; 106879f4e922SSubhransu S. Prusty 106918382eadSSubhransu S. Prusty } 107018382eadSSubhransu S. Prusty 107115b91447SSubhransu S. Prusty static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev) 107218382eadSSubhransu S. Prusty { 107315b91447SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 1074754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 107515b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 1076148569fdSSubhransu S. Prusty int dai_id = 0; 107718382eadSSubhransu S. Prusty 1078148569fdSSubhransu S. Prusty if (list_empty(&hdmi->cvt_list)) 107915b91447SSubhransu S. Prusty return -EINVAL; 108018382eadSSubhransu S. Prusty 1081148569fdSSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 1082148569fdSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai_id]; 1083148569fdSSubhransu S. Prusty dai_map->dai_id = dai_id; 108415b91447SSubhransu S. Prusty dai_map->cvt = cvt; 108518382eadSSubhransu S. Prusty 1086148569fdSSubhransu S. Prusty dai_id++; 1087148569fdSSubhransu S. Prusty 1088148569fdSSubhransu S. Prusty if (dai_id == HDA_MAX_CVTS) { 1089148569fdSSubhransu S. Prusty dev_warn(&edev->hdac.dev, 1090148569fdSSubhransu S. Prusty "Max dais supported: %d\n", dai_id); 1091148569fdSSubhransu S. Prusty break; 1092148569fdSSubhransu S. Prusty } 1093148569fdSSubhransu S. Prusty } 109418382eadSSubhransu S. Prusty 109515b91447SSubhransu S. Prusty return 0; 109615b91447SSubhransu S. Prusty } 109715b91447SSubhransu S. Prusty 109815b91447SSubhransu S. Prusty static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid) 109915b91447SSubhransu S. Prusty { 110015b91447SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 110115b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 11024a3478deSJeeja KP char name[NAME_SIZE]; 110315b91447SSubhransu S. Prusty 110415b91447SSubhransu S. Prusty cvt = kzalloc(sizeof(*cvt), GFP_KERNEL); 110515b91447SSubhransu S. Prusty if (!cvt) 110615b91447SSubhransu S. Prusty return -ENOMEM; 110715b91447SSubhransu S. Prusty 110815b91447SSubhransu S. Prusty cvt->nid = nid; 11094a3478deSJeeja KP sprintf(name, "cvt %d", cvt->nid); 11104a3478deSJeeja KP cvt->name = kstrdup(name, GFP_KERNEL); 111115b91447SSubhransu S. Prusty 111215b91447SSubhransu S. Prusty list_add_tail(&cvt->head, &hdmi->cvt_list); 111315b91447SSubhransu S. Prusty hdmi->num_cvt++; 111415b91447SSubhransu S. Prusty 111515b91447SSubhransu S. Prusty return hdac_hdmi_query_cvt_params(&edev->hdac, cvt); 111615b91447SSubhransu S. Prusty } 111715b91447SSubhransu S. Prusty 1118f6fa11a3SSandeep Tayal static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev, 1119754695f9SJeeja KP struct hdac_hdmi_port *port) 1120b7756edeSSubhransu S. Prusty { 1121f6fa11a3SSandeep Tayal unsigned int ver, mnl; 1122f6fa11a3SSandeep Tayal 1123754695f9SJeeja KP ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK) 1124f6fa11a3SSandeep Tayal >> DRM_ELD_VER_SHIFT; 1125f6fa11a3SSandeep Tayal 1126f6fa11a3SSandeep Tayal if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) { 1127f6fa11a3SSandeep Tayal dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver); 1128f6fa11a3SSandeep Tayal return -EINVAL; 1129b7756edeSSubhransu S. Prusty } 1130b7756edeSSubhransu S. Prusty 1131754695f9SJeeja KP mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] & 1132f6fa11a3SSandeep Tayal DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT; 1133f6fa11a3SSandeep Tayal 1134f6fa11a3SSandeep Tayal if (mnl > ELD_MAX_MNL) { 1135f6fa11a3SSandeep Tayal dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl); 1136f6fa11a3SSandeep Tayal return -EINVAL; 1137f6fa11a3SSandeep Tayal } 1138f6fa11a3SSandeep Tayal 1139754695f9SJeeja KP port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER]; 1140f6fa11a3SSandeep Tayal 1141f6fa11a3SSandeep Tayal return 0; 1142f6fa11a3SSandeep Tayal } 1143f6fa11a3SSandeep Tayal 1144754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, 1145754695f9SJeeja KP struct hdac_hdmi_port *port) 1146b8a54545SSubhransu S. Prusty { 1147b8a54545SSubhransu S. Prusty struct hdac_ext_device *edev = pin->edev; 11484a3478deSJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 11494a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 1150754695f9SJeeja KP int size = 0; 11512acd8309SJeeja KP int port_id = -1; 1152754695f9SJeeja KP 1153754695f9SJeeja KP if (!hdmi) 1154754695f9SJeeja KP return; 11554a3478deSJeeja KP 11562acd8309SJeeja KP /* 11572acd8309SJeeja KP * In case of non MST pin, get_eld info API expectes port 11582acd8309SJeeja KP * to be -1. 11592acd8309SJeeja KP */ 11604a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 1161754695f9SJeeja KP port->eld.monitor_present = false; 1162f6fa11a3SSandeep Tayal 11632acd8309SJeeja KP if (pin->mst_capable) 11642acd8309SJeeja KP port_id = port->id; 11652acd8309SJeeja KP 11662acd8309SJeeja KP size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id, 1167754695f9SJeeja KP &port->eld.monitor_present, 1168754695f9SJeeja KP port->eld.eld_buffer, 1169f6fa11a3SSandeep Tayal ELD_MAX_SIZE); 1170f6fa11a3SSandeep Tayal 1171f6fa11a3SSandeep Tayal if (size > 0) { 1172f6fa11a3SSandeep Tayal size = min(size, ELD_MAX_SIZE); 1173754695f9SJeeja KP if (hdac_hdmi_parse_eld(edev, port) < 0) 1174f6fa11a3SSandeep Tayal size = -EINVAL; 1175f6fa11a3SSandeep Tayal } 1176f6fa11a3SSandeep Tayal 1177f6fa11a3SSandeep Tayal if (size > 0) { 1178754695f9SJeeja KP port->eld.eld_valid = true; 1179754695f9SJeeja KP port->eld.eld_size = size; 1180f6fa11a3SSandeep Tayal } else { 1181754695f9SJeeja KP port->eld.eld_valid = false; 1182754695f9SJeeja KP port->eld.eld_size = 0; 1183f6fa11a3SSandeep Tayal } 1184b8a54545SSubhransu S. Prusty 1185754695f9SJeeja KP pcm = hdac_hdmi_get_pcm(edev, port); 11864a3478deSJeeja KP 1187754695f9SJeeja KP if (!port->eld.monitor_present || !port->eld.eld_valid) { 1188b8a54545SSubhransu S. Prusty 1189754695f9SJeeja KP dev_dbg(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n", 1190754695f9SJeeja KP __func__, pin->nid, port->id); 11914a3478deSJeeja KP 11924a3478deSJeeja KP /* 11934a3478deSJeeja KP * PCMs are not registered during device probe, so don't 11944a3478deSJeeja KP * report jack here. It will be done in usermode mux 11954a3478deSJeeja KP * control select. 11964a3478deSJeeja KP */ 11974a3478deSJeeja KP if (pcm) { 11984a3478deSJeeja KP dev_dbg(&edev->hdac.dev, 11994a3478deSJeeja KP "jack report for pcm=%d\n", pcm->pcm_id); 12004a3478deSJeeja KP 12014a3478deSJeeja KP snd_jack_report(pcm->jack, 0); 12024a3478deSJeeja KP } 12034a3478deSJeeja KP 12044a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 1205f6fa11a3SSandeep Tayal return; 1206b8a54545SSubhransu S. Prusty } 1207b8a54545SSubhransu S. Prusty 1208754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 12094a3478deSJeeja KP if (pcm) { 12104a3478deSJeeja KP dev_dbg(&edev->hdac.dev, 12114a3478deSJeeja KP "jack report for pcm=%d\n", 12124a3478deSJeeja KP pcm->pcm_id); 12134a3478deSJeeja KP 12144a3478deSJeeja KP snd_jack_report(pcm->jack, SND_JACK_AVOUT); 12154a3478deSJeeja KP } 12164a3478deSJeeja KP 1217f6fa11a3SSandeep Tayal print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1, 1218754695f9SJeeja KP port->eld.eld_buffer, port->eld.eld_size, false); 1219754695f9SJeeja KP 1220754695f9SJeeja KP } 1221754695f9SJeeja KP mutex_unlock(&hdmi->pin_mutex); 12224a3478deSJeeja KP } 12234a3478deSJeeja KP 1224754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi, 1225754695f9SJeeja KP struct hdac_hdmi_pin *pin) 1226754695f9SJeeja KP { 1227754695f9SJeeja KP struct hdac_hdmi_port *ports; 1228754695f9SJeeja KP int max_ports = HDA_MAX_PORTS; 1229754695f9SJeeja KP int i; 1230754695f9SJeeja KP 1231754695f9SJeeja KP /* 1232754695f9SJeeja KP * FIXME: max_port may vary for each platform, so pass this as 1233754695f9SJeeja KP * as driver data or query from i915 interface when this API is 1234754695f9SJeeja KP * implemented. 1235754695f9SJeeja KP */ 1236754695f9SJeeja KP 1237754695f9SJeeja KP ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL); 1238754695f9SJeeja KP if (!ports) 1239754695f9SJeeja KP return -ENOMEM; 1240754695f9SJeeja KP 1241754695f9SJeeja KP for (i = 0; i < max_ports; i++) { 1242754695f9SJeeja KP ports[i].id = i; 1243754695f9SJeeja KP ports[i].pin = pin; 1244754695f9SJeeja KP } 1245754695f9SJeeja KP pin->ports = ports; 1246754695f9SJeeja KP pin->num_ports = max_ports; 1247754695f9SJeeja KP return 0; 1248b8a54545SSubhransu S. Prusty } 1249b8a54545SSubhransu S. Prusty 125015b91447SSubhransu S. Prusty static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid) 125115b91447SSubhransu S. Prusty { 125215b91447SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 125315b91447SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 1254754695f9SJeeja KP int ret; 125515b91447SSubhransu S. Prusty 125615b91447SSubhransu S. Prusty pin = kzalloc(sizeof(*pin), GFP_KERNEL); 125715b91447SSubhransu S. Prusty if (!pin) 125815b91447SSubhransu S. Prusty return -ENOMEM; 125915b91447SSubhransu S. Prusty 126015b91447SSubhransu S. Prusty pin->nid = nid; 12612acd8309SJeeja KP pin->mst_capable = false; 1262754695f9SJeeja KP pin->edev = edev; 1263754695f9SJeeja KP ret = hdac_hdmi_add_ports(hdmi, pin); 1264754695f9SJeeja KP if (ret < 0) 1265754695f9SJeeja KP return ret; 126615b91447SSubhransu S. Prusty 126715b91447SSubhransu S. Prusty list_add_tail(&pin->head, &hdmi->pin_list); 126815b91447SSubhransu S. Prusty hdmi->num_pin++; 1269754695f9SJeeja KP hdmi->num_ports += pin->num_ports; 1270b8a54545SSubhransu S. Prusty 127115b91447SSubhransu S. Prusty return 0; 127218382eadSSubhransu S. Prusty } 127318382eadSSubhransu S. Prusty 1274211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08 1275211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81 1276211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781 1277211caab7SSubhransu S. Prusty #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */ 1278211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */ 1279211caab7SSubhransu S. Prusty 1280211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac) 1281211caab7SSubhransu S. Prusty { 1282211caab7SSubhransu S. Prusty unsigned int vendor_param; 1283211caab7SSubhransu S. Prusty 1284211caab7SSubhransu S. Prusty vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0, 1285211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1286211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS) 1287211caab7SSubhransu S. Prusty return; 1288211caab7SSubhransu S. Prusty 1289211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_ALL_PIN_CVTS; 1290211caab7SSubhransu S. Prusty vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0, 1291211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1292211caab7SSubhransu S. Prusty if (vendor_param == -1) 1293211caab7SSubhransu S. Prusty return; 1294211caab7SSubhransu S. Prusty } 1295211caab7SSubhransu S. Prusty 1296211caab7SSubhransu S. Prusty static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac) 1297211caab7SSubhransu S. Prusty { 1298211caab7SSubhransu S. Prusty unsigned int vendor_param; 1299211caab7SSubhransu S. Prusty 1300211caab7SSubhransu S. Prusty vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0, 1301211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1302211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_DP12) 1303211caab7SSubhransu S. Prusty return; 1304211caab7SSubhransu S. Prusty 1305211caab7SSubhransu S. Prusty /* enable DP1.2 mode */ 1306211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_DP12; 1307211caab7SSubhransu S. Prusty vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0, 1308211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1309211caab7SSubhransu S. Prusty if (vendor_param == -1) 1310211caab7SSubhransu S. Prusty return; 1311211caab7SSubhransu S. Prusty 1312211caab7SSubhransu S. Prusty } 1313211caab7SSubhransu S. Prusty 131417a42c45SSubhransu S. Prusty static struct snd_soc_dai_ops hdmi_dai_ops = { 131517a42c45SSubhransu S. Prusty .startup = hdac_hdmi_pcm_open, 131617a42c45SSubhransu S. Prusty .shutdown = hdac_hdmi_pcm_close, 131717a42c45SSubhransu S. Prusty .hw_params = hdac_hdmi_set_hw_params, 1318c9bfb5d7SJeeja KP .set_tdm_slot = hdac_hdmi_set_tdm_slot, 131917a42c45SSubhransu S. Prusty }; 132017a42c45SSubhransu S. Prusty 132117a42c45SSubhransu S. Prusty /* 132217a42c45SSubhransu S. Prusty * Each converter can support a stream independently. So a dai is created 132317a42c45SSubhransu S. Prusty * based on the number of converter queried. 132417a42c45SSubhransu S. Prusty */ 132517a42c45SSubhransu S. Prusty static int hdac_hdmi_create_dais(struct hdac_device *hdac, 132617a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, 132717a42c45SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, int num_dais) 132817a42c45SSubhransu S. Prusty { 132917a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais; 133017a42c45SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 133117a42c45SSubhransu S. Prusty char name[NAME_SIZE], dai_name[NAME_SIZE]; 133217a42c45SSubhransu S. Prusty int i = 0; 133317a42c45SSubhransu S. Prusty u32 rates, bps; 133417a42c45SSubhransu S. Prusty unsigned int rate_max = 384000, rate_min = 8000; 133517a42c45SSubhransu S. Prusty u64 formats; 133617a42c45SSubhransu S. Prusty int ret; 133717a42c45SSubhransu S. Prusty 133817a42c45SSubhransu S. Prusty hdmi_dais = devm_kzalloc(&hdac->dev, 133917a42c45SSubhransu S. Prusty (sizeof(*hdmi_dais) * num_dais), 134017a42c45SSubhransu S. Prusty GFP_KERNEL); 134117a42c45SSubhransu S. Prusty if (!hdmi_dais) 134217a42c45SSubhransu S. Prusty return -ENOMEM; 134317a42c45SSubhransu S. Prusty 134417a42c45SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 134517a42c45SSubhransu S. Prusty ret = snd_hdac_query_supported_pcm(hdac, cvt->nid, 134617a42c45SSubhransu S. Prusty &rates, &formats, &bps); 134717a42c45SSubhransu S. Prusty if (ret) 134817a42c45SSubhransu S. Prusty return ret; 134917a42c45SSubhransu S. Prusty 135017a42c45SSubhransu S. Prusty sprintf(dai_name, "intel-hdmi-hifi%d", i+1); 135117a42c45SSubhransu S. Prusty hdmi_dais[i].name = devm_kstrdup(&hdac->dev, 135217a42c45SSubhransu S. Prusty dai_name, GFP_KERNEL); 135317a42c45SSubhransu S. Prusty 135417a42c45SSubhransu S. Prusty if (!hdmi_dais[i].name) 135517a42c45SSubhransu S. Prusty return -ENOMEM; 135617a42c45SSubhransu S. Prusty 135717a42c45SSubhransu S. Prusty snprintf(name, sizeof(name), "hifi%d", i+1); 135817a42c45SSubhransu S. Prusty hdmi_dais[i].playback.stream_name = 135917a42c45SSubhransu S. Prusty devm_kstrdup(&hdac->dev, name, GFP_KERNEL); 136017a42c45SSubhransu S. Prusty if (!hdmi_dais[i].playback.stream_name) 136117a42c45SSubhransu S. Prusty return -ENOMEM; 136217a42c45SSubhransu S. Prusty 136317a42c45SSubhransu S. Prusty /* 136417a42c45SSubhransu S. Prusty * Set caps based on capability queried from the converter. 136517a42c45SSubhransu S. Prusty * It will be constrained runtime based on ELD queried. 136617a42c45SSubhransu S. Prusty */ 136717a42c45SSubhransu S. Prusty hdmi_dais[i].playback.formats = formats; 136817a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rates = rates; 136917a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_max = rate_max; 137017a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_min = rate_min; 137117a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_min = 2; 137217a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_max = 2; 137317a42c45SSubhransu S. Prusty hdmi_dais[i].ops = &hdmi_dai_ops; 137417a42c45SSubhransu S. Prusty 137517a42c45SSubhransu S. Prusty i++; 137617a42c45SSubhransu S. Prusty } 137717a42c45SSubhransu S. Prusty 137817a42c45SSubhransu S. Prusty *dais = hdmi_dais; 137917a42c45SSubhransu S. Prusty 138017a42c45SSubhransu S. Prusty return 0; 138117a42c45SSubhransu S. Prusty } 138217a42c45SSubhransu S. Prusty 138318382eadSSubhransu S. Prusty /* 138418382eadSSubhransu S. Prusty * Parse all nodes and store the cvt/pin nids in array 138518382eadSSubhransu S. Prusty * Add one time initialization for pin and cvt widgets 138618382eadSSubhransu S. Prusty */ 138717a42c45SSubhransu S. Prusty static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev, 138817a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, int *num_dais) 138918382eadSSubhransu S. Prusty { 139018382eadSSubhransu S. Prusty hda_nid_t nid; 13913c83ac23SSudip Mukherjee int i, num_nodes; 139218382eadSSubhransu S. Prusty struct hdac_device *hdac = &edev->hdac; 139318382eadSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 139415b91447SSubhransu S. Prusty int ret; 139518382eadSSubhransu S. Prusty 1396211caab7SSubhransu S. Prusty hdac_hdmi_skl_enable_all_pins(hdac); 1397211caab7SSubhransu S. Prusty hdac_hdmi_skl_enable_dp12(hdac); 1398211caab7SSubhransu S. Prusty 13993c83ac23SSudip Mukherjee num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid); 1400541140d4SSubhransu S. Prusty if (!nid || num_nodes <= 0) { 140118382eadSSubhransu S. Prusty dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n"); 140218382eadSSubhransu S. Prusty return -EINVAL; 140318382eadSSubhransu S. Prusty } 140418382eadSSubhransu S. Prusty 14053c83ac23SSudip Mukherjee hdac->num_nodes = num_nodes; 140618382eadSSubhransu S. Prusty hdac->start_nid = nid; 140718382eadSSubhransu S. Prusty 140818382eadSSubhransu S. Prusty for (i = 0; i < hdac->num_nodes; i++, nid++) { 140918382eadSSubhransu S. Prusty unsigned int caps; 141018382eadSSubhransu S. Prusty unsigned int type; 141118382eadSSubhransu S. Prusty 141218382eadSSubhransu S. Prusty caps = get_wcaps(hdac, nid); 141318382eadSSubhransu S. Prusty type = get_wcaps_type(caps); 141418382eadSSubhransu S. Prusty 141518382eadSSubhransu S. Prusty if (!(caps & AC_WCAP_DIGITAL)) 141618382eadSSubhransu S. Prusty continue; 141718382eadSSubhransu S. Prusty 141818382eadSSubhransu S. Prusty switch (type) { 141918382eadSSubhransu S. Prusty 142018382eadSSubhransu S. Prusty case AC_WID_AUD_OUT: 142115b91447SSubhransu S. Prusty ret = hdac_hdmi_add_cvt(edev, nid); 142215b91447SSubhransu S. Prusty if (ret < 0) 142315b91447SSubhransu S. Prusty return ret; 142418382eadSSubhransu S. Prusty break; 142518382eadSSubhransu S. Prusty 142618382eadSSubhransu S. Prusty case AC_WID_PIN: 142715b91447SSubhransu S. Prusty ret = hdac_hdmi_add_pin(edev, nid); 142815b91447SSubhransu S. Prusty if (ret < 0) 142915b91447SSubhransu S. Prusty return ret; 143018382eadSSubhransu S. Prusty break; 143118382eadSSubhransu S. Prusty } 143218382eadSSubhransu S. Prusty } 143318382eadSSubhransu S. Prusty 143418382eadSSubhransu S. Prusty hdac->end_nid = nid; 143518382eadSSubhransu S. Prusty 143615b91447SSubhransu S. Prusty if (!hdmi->num_pin || !hdmi->num_cvt) 143718382eadSSubhransu S. Prusty return -EIO; 143818382eadSSubhransu S. Prusty 143917a42c45SSubhransu S. Prusty ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt); 144017a42c45SSubhransu S. Prusty if (ret) { 144117a42c45SSubhransu S. Prusty dev_err(&hdac->dev, "Failed to create dais with err: %d\n", 144217a42c45SSubhransu S. Prusty ret); 144317a42c45SSubhransu S. Prusty return ret; 144417a42c45SSubhransu S. Prusty } 144517a42c45SSubhransu S. Prusty 144617a42c45SSubhransu S. Prusty *num_dais = hdmi->num_cvt; 144717a42c45SSubhransu S. Prusty 144815b91447SSubhransu S. Prusty return hdac_hdmi_init_dai_map(edev); 144918382eadSSubhransu S. Prusty } 145018382eadSSubhransu S. Prusty 1451f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe) 1452b8a54545SSubhransu S. Prusty { 1453b8a54545SSubhransu S. Prusty struct hdac_ext_device *edev = aptr; 1454b8a54545SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 1455754695f9SJeeja KP struct hdac_hdmi_pin *pin = NULL; 1456754695f9SJeeja KP struct hdac_hdmi_port *hport = NULL; 1457b8a54545SSubhransu S. Prusty struct snd_soc_codec *codec = edev->scodec; 14582acd8309SJeeja KP int i; 1459b8a54545SSubhransu S. Prusty 1460b8a54545SSubhransu S. Prusty /* Don't know how this mapping is derived */ 1461b8a54545SSubhransu S. Prusty hda_nid_t pin_nid = port + 0x04; 1462b8a54545SSubhransu S. Prusty 1463754695f9SJeeja KP dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__, 1464754695f9SJeeja KP pin_nid, pipe); 1465b8a54545SSubhransu S. Prusty 1466b8a54545SSubhransu S. Prusty /* 1467b8a54545SSubhransu S. Prusty * skip notification during system suspend (but not in runtime PM); 1468b8a54545SSubhransu S. Prusty * the state will be updated at resume. Also since the ELD and 1469b8a54545SSubhransu S. Prusty * connection states are updated in anyway at the end of the resume, 1470b8a54545SSubhransu S. Prusty * we can skip it when received during PM process. 1471b8a54545SSubhransu S. Prusty */ 1472b8a54545SSubhransu S. Prusty if (snd_power_get_state(codec->component.card->snd_card) != 1473b8a54545SSubhransu S. Prusty SNDRV_CTL_POWER_D0) 1474b8a54545SSubhransu S. Prusty return; 1475b8a54545SSubhransu S. Prusty 1476b8a54545SSubhransu S. Prusty if (atomic_read(&edev->hdac.in_pm)) 1477b8a54545SSubhransu S. Prusty return; 1478b8a54545SSubhransu S. Prusty 1479b8a54545SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1480754695f9SJeeja KP if (pin->nid != pin_nid) 1481754695f9SJeeja KP continue; 1482754695f9SJeeja KP 1483754695f9SJeeja KP /* In case of non MST pin, pipe is -1 */ 1484754695f9SJeeja KP if (pipe == -1) { 14852acd8309SJeeja KP pin->mst_capable = false; 1486754695f9SJeeja KP /* if not MST, default is port[0] */ 1487754695f9SJeeja KP hport = &pin->ports[0]; 14882acd8309SJeeja KP goto out; 14892acd8309SJeeja KP } else { 14902acd8309SJeeja KP for (i = 0; i < pin->num_ports; i++) { 14912acd8309SJeeja KP pin->mst_capable = true; 14922acd8309SJeeja KP if (pin->ports[i].id == pipe) { 14932acd8309SJeeja KP hport = &pin->ports[i]; 14942acd8309SJeeja KP goto out; 14952acd8309SJeeja KP } 14962acd8309SJeeja KP } 1497b8a54545SSubhransu S. Prusty } 1498b8a54545SSubhransu S. Prusty } 1499b8a54545SSubhransu S. Prusty 15002acd8309SJeeja KP out: 15012acd8309SJeeja KP if (pin && hport) 1502754695f9SJeeja KP hdac_hdmi_present_sense(pin, hport); 1503754695f9SJeeja KP } 1504754695f9SJeeja KP 1505b8a54545SSubhransu S. Prusty static struct i915_audio_component_audio_ops aops = { 1506b8a54545SSubhransu S. Prusty .pin_eld_notify = hdac_hdmi_eld_notify_cb, 1507b8a54545SSubhransu S. Prusty }; 1508b8a54545SSubhransu S. Prusty 15092889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card, 15102889099eSSubhransu S. Prusty int device) 15112889099eSSubhransu S. Prusty { 15122889099eSSubhransu S. Prusty struct snd_soc_pcm_runtime *rtd; 15132889099eSSubhransu S. Prusty 15142889099eSSubhransu S. Prusty list_for_each_entry(rtd, &card->rtd_list, list) { 15152889099eSSubhransu S. Prusty if (rtd->pcm && (rtd->pcm->device == device)) 15162889099eSSubhransu S. Prusty return rtd->pcm; 15172889099eSSubhransu S. Prusty } 15182889099eSSubhransu S. Prusty 15192889099eSSubhransu S. Prusty return NULL; 15202889099eSSubhransu S. Prusty } 15212889099eSSubhransu S. Prusty 15224a3478deSJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device) 15234a3478deSJeeja KP { 15244a3478deSJeeja KP char jack_name[NAME_SIZE]; 15254a3478deSJeeja KP struct snd_soc_codec *codec = dai->codec; 15264a3478deSJeeja KP struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); 15274a3478deSJeeja KP struct snd_soc_dapm_context *dapm = 15284a3478deSJeeja KP snd_soc_component_get_dapm(&codec->component); 15294a3478deSJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 15304a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 15312889099eSSubhransu S. Prusty struct snd_pcm *snd_pcm; 15322889099eSSubhransu S. Prusty int err; 15334a3478deSJeeja KP 15344a3478deSJeeja KP /* 15354a3478deSJeeja KP * this is a new PCM device, create new pcm and 15364a3478deSJeeja KP * add to the pcm list 15374a3478deSJeeja KP */ 15384a3478deSJeeja KP pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); 15394a3478deSJeeja KP if (!pcm) 15404a3478deSJeeja KP return -ENOMEM; 15414a3478deSJeeja KP pcm->pcm_id = device; 15424a3478deSJeeja KP pcm->cvt = hdmi->dai_map[dai->id].cvt; 1543ab1eea19SJeeja KP mutex_init(&pcm->lock); 15444a3478deSJeeja KP 15452889099eSSubhransu S. Prusty snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device); 15462889099eSSubhransu S. Prusty if (snd_pcm) { 15472889099eSSubhransu S. Prusty err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap); 15482889099eSSubhransu S. Prusty if (err < 0) { 15492889099eSSubhransu S. Prusty dev_err(&edev->hdac.dev, 15502889099eSSubhransu S. Prusty "chmap control add failed with err: %d for pcm: %d\n", 15512889099eSSubhransu S. Prusty err, device); 15522889099eSSubhransu S. Prusty kfree(pcm); 15532889099eSSubhransu S. Prusty return err; 15542889099eSSubhransu S. Prusty } 15552889099eSSubhransu S. Prusty } 15562889099eSSubhransu S. Prusty 15574a3478deSJeeja KP list_add_tail(&pcm->head, &hdmi->pcm_list); 15584a3478deSJeeja KP 15594a3478deSJeeja KP sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device); 15604a3478deSJeeja KP 15614a3478deSJeeja KP return snd_jack_new(dapm->card->snd_card, jack_name, 15624a3478deSJeeja KP SND_JACK_AVOUT, &pcm->jack, true, false); 15634a3478deSJeeja KP } 15644a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init); 15654a3478deSJeeja KP 1566a9ce96bcSJeeja KP static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev, 1567a9ce96bcSJeeja KP struct hdac_hdmi_priv *hdmi, bool detect_pin_caps) 1568a9ce96bcSJeeja KP { 1569a9ce96bcSJeeja KP int i; 1570a9ce96bcSJeeja KP struct hdac_hdmi_pin *pin; 1571a9ce96bcSJeeja KP 1572a9ce96bcSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 1573a9ce96bcSJeeja KP if (detect_pin_caps) { 1574a9ce96bcSJeeja KP 1575a9ce96bcSJeeja KP if (hdac_hdmi_get_port_len(edev, pin->nid) == 0) 1576a9ce96bcSJeeja KP pin->mst_capable = false; 1577a9ce96bcSJeeja KP else 1578a9ce96bcSJeeja KP pin->mst_capable = true; 1579a9ce96bcSJeeja KP } 1580a9ce96bcSJeeja KP 1581a9ce96bcSJeeja KP for (i = 0; i < pin->num_ports; i++) { 1582a9ce96bcSJeeja KP if (!pin->mst_capable && i > 0) 1583a9ce96bcSJeeja KP continue; 1584a9ce96bcSJeeja KP 1585a9ce96bcSJeeja KP hdac_hdmi_present_sense(pin, &pin->ports[i]); 1586a9ce96bcSJeeja KP } 1587a9ce96bcSJeeja KP } 1588a9ce96bcSJeeja KP } 1589a9ce96bcSJeeja KP 159018382eadSSubhransu S. Prusty static int hdmi_codec_probe(struct snd_soc_codec *codec) 159118382eadSSubhransu S. Prusty { 159218382eadSSubhransu S. Prusty struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); 159318382eadSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 159418382eadSSubhransu S. Prusty struct snd_soc_dapm_context *dapm = 159518382eadSSubhransu S. Prusty snd_soc_component_get_dapm(&codec->component); 1596b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 1597a9ce96bcSJeeja KP int ret; 159818382eadSSubhransu S. Prusty 159918382eadSSubhransu S. Prusty edev->scodec = codec; 160018382eadSSubhransu S. Prusty 1601b2047e99SVinod Koul /* 1602b2047e99SVinod Koul * hold the ref while we probe, also no need to drop the ref on 1603b2047e99SVinod Koul * exit, we call pm_runtime_suspend() so that will do for us 1604b2047e99SVinod Koul */ 1605b2047e99SVinod Koul hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev)); 1606500e06b9SVinod Koul if (!hlink) { 1607500e06b9SVinod Koul dev_err(&edev->hdac.dev, "hdac link not found\n"); 1608500e06b9SVinod Koul return -EIO; 1609500e06b9SVinod Koul } 1610500e06b9SVinod Koul 1611b2047e99SVinod Koul snd_hdac_ext_bus_link_get(edev->ebus, hlink); 1612b2047e99SVinod Koul 161379f4e922SSubhransu S. Prusty ret = create_fill_widget_route_map(dapm); 161479f4e922SSubhransu S. Prusty if (ret < 0) 161579f4e922SSubhransu S. Prusty return ret; 161618382eadSSubhransu S. Prusty 1617b8a54545SSubhransu S. Prusty aops.audio_ptr = edev; 1618b8a54545SSubhransu S. Prusty ret = snd_hdac_i915_register_notifier(&aops); 1619b8a54545SSubhransu S. Prusty if (ret < 0) { 1620b8a54545SSubhransu S. Prusty dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n", 1621b8a54545SSubhransu S. Prusty ret); 1622b8a54545SSubhransu S. Prusty return ret; 1623b8a54545SSubhransu S. Prusty } 1624b8a54545SSubhransu S. Prusty 1625a9ce96bcSJeeja KP hdac_hdmi_present_sense_all_pins(edev, hdmi, true); 162618382eadSSubhransu S. Prusty /* Imp: Store the card pointer in hda_codec */ 162718382eadSSubhransu S. Prusty edev->card = dapm->card->snd_card; 162818382eadSSubhransu S. Prusty 1629e342ac08SSubhransu S. Prusty /* 1630e342ac08SSubhransu S. Prusty * hdac_device core already sets the state to active and calls 1631e342ac08SSubhransu S. Prusty * get_noresume. So enable runtime and set the device to suspend. 1632e342ac08SSubhransu S. Prusty */ 1633e342ac08SSubhransu S. Prusty pm_runtime_enable(&edev->hdac.dev); 1634e342ac08SSubhransu S. Prusty pm_runtime_put(&edev->hdac.dev); 1635e342ac08SSubhransu S. Prusty pm_runtime_suspend(&edev->hdac.dev); 1636e342ac08SSubhransu S. Prusty 1637e342ac08SSubhransu S. Prusty return 0; 1638e342ac08SSubhransu S. Prusty } 1639e342ac08SSubhransu S. Prusty 1640e342ac08SSubhransu S. Prusty static int hdmi_codec_remove(struct snd_soc_codec *codec) 1641e342ac08SSubhransu S. Prusty { 1642e342ac08SSubhransu S. Prusty struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); 1643e342ac08SSubhransu S. Prusty 1644e342ac08SSubhransu S. Prusty pm_runtime_disable(&edev->hdac.dev); 164518382eadSSubhransu S. Prusty return 0; 164618382eadSSubhransu S. Prusty } 164718382eadSSubhransu S. Prusty 1648571d5078SJeeja KP #ifdef CONFIG_PM 16491b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev) 16501b377ccdSSubhransu S. Prusty { 16511b377ccdSSubhransu S. Prusty struct hdac_ext_device *edev = to_hda_ext_device(dev); 16521b377ccdSSubhransu S. Prusty struct hdac_device *hdac = &edev->hdac; 16531b377ccdSSubhransu S. Prusty 16541b377ccdSSubhransu S. Prusty pm_runtime_get_sync(&edev->hdac.dev); 16551b377ccdSSubhransu S. Prusty 16561b377ccdSSubhransu S. Prusty /* 16571b377ccdSSubhransu S. Prusty * Power down afg. 16581b377ccdSSubhransu S. Prusty * codec_read is preferred over codec_write to set the power state. 16591b377ccdSSubhransu S. Prusty * This way verb is send to set the power state and response 16601b377ccdSSubhransu S. Prusty * is received. So setting power state is ensured without using loop 16611b377ccdSSubhransu S. Prusty * to read the state. 16621b377ccdSSubhransu S. Prusty */ 16631b377ccdSSubhransu S. Prusty snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE, 16641b377ccdSSubhransu S. Prusty AC_PWRST_D3); 16651b377ccdSSubhransu S. Prusty 16661b377ccdSSubhransu S. Prusty return 0; 16671b377ccdSSubhransu S. Prusty } 16681b377ccdSSubhransu S. Prusty 16690fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev) 1670571d5078SJeeja KP { 16710fee1798SSubhransu S. Prusty struct hdac_ext_device *edev = to_hda_ext_device(dev); 1672571d5078SJeeja KP struct hdac_hdmi_priv *hdmi = edev->private_data; 1673571d5078SJeeja KP struct hdac_device *hdac = &edev->hdac; 16741b377ccdSSubhransu S. Prusty 16751b377ccdSSubhransu S. Prusty /* Power up afg */ 16761b377ccdSSubhransu S. Prusty snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE, 16771b377ccdSSubhransu S. Prusty AC_PWRST_D0); 1678571d5078SJeeja KP 1679571d5078SJeeja KP hdac_hdmi_skl_enable_all_pins(&edev->hdac); 1680571d5078SJeeja KP hdac_hdmi_skl_enable_dp12(&edev->hdac); 1681571d5078SJeeja KP 1682571d5078SJeeja KP /* 1683571d5078SJeeja KP * As the ELD notify callback request is not entertained while the 1684571d5078SJeeja KP * device is in suspend state. Need to manually check detection of 1685a9ce96bcSJeeja KP * all pins here. pin capablity change is not support, so use the 1686a9ce96bcSJeeja KP * already set pin caps. 1687571d5078SJeeja KP */ 1688a9ce96bcSJeeja KP hdac_hdmi_present_sense_all_pins(edev, hdmi, false); 1689571d5078SJeeja KP 16901b377ccdSSubhransu S. Prusty pm_runtime_put_sync(&edev->hdac.dev); 1691571d5078SJeeja KP } 1692571d5078SJeeja KP #else 16931b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL 16940fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL 1695571d5078SJeeja KP #endif 1696571d5078SJeeja KP 169718382eadSSubhransu S. Prusty static struct snd_soc_codec_driver hdmi_hda_codec = { 169818382eadSSubhransu S. Prusty .probe = hdmi_codec_probe, 1699e342ac08SSubhransu S. Prusty .remove = hdmi_codec_remove, 170018382eadSSubhransu S. Prusty .idle_bias_off = true, 170118382eadSSubhransu S. Prusty }; 170218382eadSSubhransu S. Prusty 17032889099eSSubhransu S. Prusty static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx, 17042889099eSSubhransu S. Prusty unsigned char *chmap) 17052889099eSSubhransu S. Prusty { 17062889099eSSubhransu S. Prusty struct hdac_ext_device *edev = to_ehdac_device(hdac); 17072889099eSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 17082889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 17092889099eSSubhransu S. Prusty 1710ab1eea19SJeeja KP memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap)); 17112889099eSSubhransu S. Prusty } 17122889099eSSubhransu S. Prusty 17132889099eSSubhransu S. Prusty static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx, 17142889099eSSubhransu S. Prusty unsigned char *chmap, int prepared) 17152889099eSSubhransu S. Prusty { 17162889099eSSubhransu S. Prusty struct hdac_ext_device *edev = to_ehdac_device(hdac); 17172889099eSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 17182889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 1719754695f9SJeeja KP struct hdac_hdmi_port *port = pcm->port; 17202889099eSSubhransu S. Prusty 1721ab1eea19SJeeja KP mutex_lock(&pcm->lock); 1722ab1eea19SJeeja KP pcm->chmap_set = true; 1723ab1eea19SJeeja KP memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap)); 17242889099eSSubhransu S. Prusty if (prepared) 1725754695f9SJeeja KP hdac_hdmi_setup_audio_infoframe(edev, pcm, port); 1726ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 17272889099eSSubhransu S. Prusty } 17282889099eSSubhransu S. Prusty 17292889099eSSubhransu S. Prusty static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx) 17302889099eSSubhransu S. Prusty { 17312889099eSSubhransu S. Prusty struct hdac_ext_device *edev = to_ehdac_device(hdac); 17322889099eSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 17332889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 1734754695f9SJeeja KP struct hdac_hdmi_port *port = pcm->port; 17352889099eSSubhransu S. Prusty 1736754695f9SJeeja KP return port ? true:false; 17372889099eSSubhransu S. Prusty } 17382889099eSSubhransu S. Prusty 17392889099eSSubhransu S. Prusty static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx) 17402889099eSSubhransu S. Prusty { 17412889099eSSubhransu S. Prusty struct hdac_ext_device *edev = to_ehdac_device(hdac); 17422889099eSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 17432889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 1744754695f9SJeeja KP struct hdac_hdmi_port *port = pcm->port; 17452889099eSSubhransu S. Prusty 1746754695f9SJeeja KP if (!port || !port->eld.eld_valid) 17472889099eSSubhransu S. Prusty return 0; 17482889099eSSubhransu S. Prusty 1749754695f9SJeeja KP return port->eld.info.spk_alloc; 17502889099eSSubhransu S. Prusty } 17512889099eSSubhransu S. Prusty 175218382eadSSubhransu S. Prusty static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev) 175318382eadSSubhransu S. Prusty { 175418382eadSSubhransu S. Prusty struct hdac_device *codec = &edev->hdac; 175518382eadSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi_priv; 175617a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais = NULL; 1757b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 175817a42c45SSubhransu S. Prusty int num_dais = 0; 175918382eadSSubhransu S. Prusty int ret = 0; 176018382eadSSubhransu S. Prusty 1761b2047e99SVinod Koul /* hold the ref while we probe */ 1762b2047e99SVinod Koul hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev)); 1763500e06b9SVinod Koul if (!hlink) { 1764500e06b9SVinod Koul dev_err(&edev->hdac.dev, "hdac link not found\n"); 1765500e06b9SVinod Koul return -EIO; 1766500e06b9SVinod Koul } 1767500e06b9SVinod Koul 1768b2047e99SVinod Koul snd_hdac_ext_bus_link_get(edev->ebus, hlink); 1769b2047e99SVinod Koul 177018382eadSSubhransu S. Prusty hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL); 177118382eadSSubhransu S. Prusty if (hdmi_priv == NULL) 177218382eadSSubhransu S. Prusty return -ENOMEM; 177318382eadSSubhransu S. Prusty 177418382eadSSubhransu S. Prusty edev->private_data = hdmi_priv; 1775bcced704SSubhransu S. Prusty snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap); 17762889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap; 17772889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap; 17782889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached; 17792889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc; 178018382eadSSubhransu S. Prusty 178118382eadSSubhransu S. Prusty dev_set_drvdata(&codec->dev, edev); 178218382eadSSubhransu S. Prusty 178315b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->pin_list); 178415b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->cvt_list); 17854a3478deSJeeja KP INIT_LIST_HEAD(&hdmi_priv->pcm_list); 17864a3478deSJeeja KP mutex_init(&hdmi_priv->pin_mutex); 178715b91447SSubhransu S. Prusty 1788aeaccef0SRamesh Babu /* 1789aeaccef0SRamesh Babu * Turned off in the runtime_suspend during the first explicit 1790aeaccef0SRamesh Babu * pm_runtime_suspend call. 1791aeaccef0SRamesh Babu */ 1792aeaccef0SRamesh Babu ret = snd_hdac_display_power(edev->hdac.bus, true); 1793aeaccef0SRamesh Babu if (ret < 0) { 1794aeaccef0SRamesh Babu dev_err(&edev->hdac.dev, 1795aeaccef0SRamesh Babu "Cannot turn on display power on i915 err: %d\n", 1796aeaccef0SRamesh Babu ret); 1797aeaccef0SRamesh Babu return ret; 1798aeaccef0SRamesh Babu } 1799aeaccef0SRamesh Babu 180017a42c45SSubhransu S. Prusty ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais); 180117a42c45SSubhransu S. Prusty if (ret < 0) { 180217a42c45SSubhransu S. Prusty dev_err(&codec->dev, 180317a42c45SSubhransu S. Prusty "Failed in parse and map nid with err: %d\n", ret); 180418382eadSSubhransu S. Prusty return ret; 180517a42c45SSubhransu S. Prusty } 180618382eadSSubhransu S. Prusty 180718382eadSSubhransu S. Prusty /* ASoC specific initialization */ 1808b2047e99SVinod Koul ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec, 180917a42c45SSubhransu S. Prusty hdmi_dais, num_dais); 1810b2047e99SVinod Koul 1811b2047e99SVinod Koul snd_hdac_ext_bus_link_put(edev->ebus, hlink); 1812b2047e99SVinod Koul 1813b2047e99SVinod Koul return ret; 181418382eadSSubhransu S. Prusty } 181518382eadSSubhransu S. Prusty 181618382eadSSubhransu S. Prusty static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev) 181718382eadSSubhransu S. Prusty { 181815b91447SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi = edev->private_data; 181915b91447SSubhransu S. Prusty struct hdac_hdmi_pin *pin, *pin_next; 182015b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt, *cvt_next; 18214a3478deSJeeja KP struct hdac_hdmi_pcm *pcm, *pcm_next; 1822754695f9SJeeja KP int i; 182315b91447SSubhransu S. Prusty 182418382eadSSubhransu S. Prusty snd_soc_unregister_codec(&edev->hdac.dev); 182518382eadSSubhransu S. Prusty 18264a3478deSJeeja KP list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) { 18274a3478deSJeeja KP pcm->cvt = NULL; 1828754695f9SJeeja KP pcm->port = NULL; 18294a3478deSJeeja KP list_del(&pcm->head); 18304a3478deSJeeja KP kfree(pcm); 18314a3478deSJeeja KP } 18324a3478deSJeeja KP 183315b91447SSubhransu S. Prusty list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) { 183415b91447SSubhransu S. Prusty list_del(&cvt->head); 18354a3478deSJeeja KP kfree(cvt->name); 183615b91447SSubhransu S. Prusty kfree(cvt); 183715b91447SSubhransu S. Prusty } 183815b91447SSubhransu S. Prusty 183915b91447SSubhransu S. Prusty list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) { 1840754695f9SJeeja KP for (i = 0; i < pin->num_ports; i++) 1841754695f9SJeeja KP pin->ports[i].pin = NULL; 1842754695f9SJeeja KP kfree(pin->ports); 184315b91447SSubhransu S. Prusty list_del(&pin->head); 184415b91447SSubhransu S. Prusty kfree(pin); 184515b91447SSubhransu S. Prusty } 184615b91447SSubhransu S. Prusty 184718382eadSSubhransu S. Prusty return 0; 184818382eadSSubhransu S. Prusty } 184918382eadSSubhransu S. Prusty 1850e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM 1851e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev) 1852e342ac08SSubhransu S. Prusty { 1853e342ac08SSubhransu S. Prusty struct hdac_ext_device *edev = to_hda_ext_device(dev); 1854e342ac08SSubhransu S. Prusty struct hdac_device *hdac = &edev->hdac; 185507f083abSSubhransu S. Prusty struct hdac_bus *bus = hdac->bus; 1856b2047e99SVinod Koul struct hdac_ext_bus *ebus = hbus_to_ebus(bus); 1857b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 185807f083abSSubhransu S. Prusty int err; 1859e342ac08SSubhransu S. Prusty 1860e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 1861e342ac08SSubhransu S. Prusty 186207f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 186307f083abSSubhransu S. Prusty if (!bus) 186407f083abSSubhransu S. Prusty return 0; 186507f083abSSubhransu S. Prusty 18661b377ccdSSubhransu S. Prusty /* 18671b377ccdSSubhransu S. Prusty * Power down afg. 18681b377ccdSSubhransu S. Prusty * codec_read is preferred over codec_write to set the power state. 18691b377ccdSSubhransu S. Prusty * This way verb is send to set the power state and response 18701b377ccdSSubhransu S. Prusty * is received. So setting power state is ensured without using loop 18711b377ccdSSubhransu S. Prusty * to read the state. 18721b377ccdSSubhransu S. Prusty */ 18731b377ccdSSubhransu S. Prusty snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE, 18741b377ccdSSubhransu S. Prusty AC_PWRST_D3); 187507f083abSSubhransu S. Prusty err = snd_hdac_display_power(bus, false); 187607f083abSSubhransu S. Prusty if (err < 0) { 187707f083abSSubhransu S. Prusty dev_err(bus->dev, "Cannot turn on display power on i915\n"); 187807f083abSSubhransu S. Prusty return err; 187907f083abSSubhransu S. Prusty } 188007f083abSSubhransu S. Prusty 1881b2047e99SVinod Koul hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev)); 1882500e06b9SVinod Koul if (!hlink) { 1883500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 1884500e06b9SVinod Koul return -EIO; 1885500e06b9SVinod Koul } 1886500e06b9SVinod Koul 1887b2047e99SVinod Koul snd_hdac_ext_bus_link_put(ebus, hlink); 1888b2047e99SVinod Koul 1889e342ac08SSubhransu S. Prusty return 0; 1890e342ac08SSubhransu S. Prusty } 1891e342ac08SSubhransu S. Prusty 1892e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev) 1893e342ac08SSubhransu S. Prusty { 1894e342ac08SSubhransu S. Prusty struct hdac_ext_device *edev = to_hda_ext_device(dev); 1895e342ac08SSubhransu S. Prusty struct hdac_device *hdac = &edev->hdac; 189607f083abSSubhransu S. Prusty struct hdac_bus *bus = hdac->bus; 1897b2047e99SVinod Koul struct hdac_ext_bus *ebus = hbus_to_ebus(bus); 1898b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 189907f083abSSubhransu S. Prusty int err; 1900e342ac08SSubhransu S. Prusty 1901e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 1902e342ac08SSubhransu S. Prusty 190307f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 190407f083abSSubhransu S. Prusty if (!bus) 190507f083abSSubhransu S. Prusty return 0; 190607f083abSSubhransu S. Prusty 1907b2047e99SVinod Koul hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev)); 1908500e06b9SVinod Koul if (!hlink) { 1909500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 1910500e06b9SVinod Koul return -EIO; 1911500e06b9SVinod Koul } 1912500e06b9SVinod Koul 1913b2047e99SVinod Koul snd_hdac_ext_bus_link_get(ebus, hlink); 1914b2047e99SVinod Koul 191507f083abSSubhransu S. Prusty err = snd_hdac_display_power(bus, true); 191607f083abSSubhransu S. Prusty if (err < 0) { 191707f083abSSubhransu S. Prusty dev_err(bus->dev, "Cannot turn on display power on i915\n"); 191807f083abSSubhransu S. Prusty return err; 191907f083abSSubhransu S. Prusty } 192007f083abSSubhransu S. Prusty 1921ab85f5b3SSubhransu S. Prusty hdac_hdmi_skl_enable_all_pins(&edev->hdac); 1922ab85f5b3SSubhransu S. Prusty hdac_hdmi_skl_enable_dp12(&edev->hdac); 1923ab85f5b3SSubhransu S. Prusty 1924e342ac08SSubhransu S. Prusty /* Power up afg */ 19251b377ccdSSubhransu S. Prusty snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE, 19261b377ccdSSubhransu S. Prusty AC_PWRST_D0); 1927e342ac08SSubhransu S. Prusty 1928e342ac08SSubhransu S. Prusty return 0; 1929e342ac08SSubhransu S. Prusty } 1930e342ac08SSubhransu S. Prusty #else 1931e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL 1932e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL 1933e342ac08SSubhransu S. Prusty #endif 1934e342ac08SSubhransu S. Prusty 1935e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = { 1936e342ac08SSubhransu S. Prusty SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL) 19371b377ccdSSubhransu S. Prusty .prepare = hdmi_codec_prepare, 19380fee1798SSubhransu S. Prusty .complete = hdmi_codec_complete, 1939e342ac08SSubhransu S. Prusty }; 1940e342ac08SSubhransu S. Prusty 194118382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = { 194218382eadSSubhransu S. Prusty HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0), 1943e2304803SJeeja KP HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0), 1944cc216887SShreyas NC HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0), 194518382eadSSubhransu S. Prusty {} 194618382eadSSubhransu S. Prusty }; 194718382eadSSubhransu S. Prusty 194818382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list); 194918382eadSSubhransu S. Prusty 195018382eadSSubhransu S. Prusty static struct hdac_ext_driver hdmi_driver = { 195118382eadSSubhransu S. Prusty . hdac = { 195218382eadSSubhransu S. Prusty .driver = { 195318382eadSSubhransu S. Prusty .name = "HDMI HDA Codec", 1954e342ac08SSubhransu S. Prusty .pm = &hdac_hdmi_pm, 195518382eadSSubhransu S. Prusty }, 195618382eadSSubhransu S. Prusty .id_table = hdmi_list, 195718382eadSSubhransu S. Prusty }, 195818382eadSSubhransu S. Prusty .probe = hdac_hdmi_dev_probe, 195918382eadSSubhransu S. Prusty .remove = hdac_hdmi_dev_remove, 196018382eadSSubhransu S. Prusty }; 196118382eadSSubhransu S. Prusty 196218382eadSSubhransu S. Prusty static int __init hdmi_init(void) 196318382eadSSubhransu S. Prusty { 196418382eadSSubhransu S. Prusty return snd_hda_ext_driver_register(&hdmi_driver); 196518382eadSSubhransu S. Prusty } 196618382eadSSubhransu S. Prusty 196718382eadSSubhransu S. Prusty static void __exit hdmi_exit(void) 196818382eadSSubhransu S. Prusty { 196918382eadSSubhransu S. Prusty snd_hda_ext_driver_unregister(&hdmi_driver); 197018382eadSSubhransu S. Prusty } 197118382eadSSubhransu S. Prusty 197218382eadSSubhransu S. Prusty module_init(hdmi_init); 197318382eadSSubhransu S. Prusty module_exit(hdmi_exit); 197418382eadSSubhransu S. Prusty 197518382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2"); 197618382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec"); 197718382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>"); 197818382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>"); 1979