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; 883787a398SRakesh Ughreja struct hdac_device *hdev; 89754695f9SJeeja KP }; 90754695f9SJeeja KP 91754695f9SJeeja KP struct hdac_hdmi_port { 92e0e5d3e5SJeeja KP struct list_head head; 93754695f9SJeeja KP int id; 94754695f9SJeeja KP struct hdac_hdmi_pin *pin; 9518382eadSSubhransu S. Prusty int num_mux_nids; 9618382eadSSubhransu S. Prusty hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; 97b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld eld; 980324e51bSJeeja KP const char *jack_pin; 990324e51bSJeeja KP struct snd_soc_dapm_context *dapm; 1000324e51bSJeeja KP const char *output_pin; 10118382eadSSubhransu S. Prusty }; 10218382eadSSubhransu S. Prusty 1034a3478deSJeeja KP struct hdac_hdmi_pcm { 1044a3478deSJeeja KP struct list_head head; 1054a3478deSJeeja KP int pcm_id; 106e0e5d3e5SJeeja KP struct list_head port_list; 1074a3478deSJeeja KP struct hdac_hdmi_cvt *cvt; 10862490016SJeeja KP struct snd_soc_jack *jack; 109c9bfb5d7SJeeja KP int stream_tag; 110c9bfb5d7SJeeja KP int channels; 111c9bfb5d7SJeeja KP int format; 112ab1eea19SJeeja KP bool chmap_set; 113ab1eea19SJeeja KP unsigned char chmap[8]; /* ALSA API channel-map */ 114ab1eea19SJeeja KP struct mutex lock; 115e0e5d3e5SJeeja KP int jack_event; 1164a3478deSJeeja KP }; 1174a3478deSJeeja KP 118754695f9SJeeja KP struct hdac_hdmi_dai_port_map { 11918382eadSSubhransu S. Prusty int dai_id; 120754695f9SJeeja KP struct hdac_hdmi_port *port; 12115b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 12218382eadSSubhransu S. Prusty }; 12318382eadSSubhransu S. Prusty 1245622bc95SPradeep Tewani struct hdac_hdmi_drv_data { 1255622bc95SPradeep Tewani unsigned int vendor_nid; 1265622bc95SPradeep Tewani }; 1275622bc95SPradeep Tewani 12818382eadSSubhransu S. Prusty struct hdac_hdmi_priv { 1293787a398SRakesh Ughreja struct hdac_device *hdev; 1303787a398SRakesh Ughreja struct snd_soc_component *component; 1313787a398SRakesh Ughreja struct snd_card *card; 132754695f9SJeeja KP struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS]; 13315b91447SSubhransu S. Prusty struct list_head pin_list; 13415b91447SSubhransu S. Prusty struct list_head cvt_list; 1354a3478deSJeeja KP struct list_head pcm_list; 13615b91447SSubhransu S. Prusty int num_pin; 13715b91447SSubhransu S. Prusty int num_cvt; 138754695f9SJeeja KP int num_ports; 1394a3478deSJeeja KP struct mutex pin_mutex; 140bcced704SSubhransu S. Prusty struct hdac_chmap chmap; 1415622bc95SPradeep Tewani struct hdac_hdmi_drv_data *drv_data; 1421e02dac3SKuninori Morimoto struct snd_soc_dai_driver *dai_drv; 14318382eadSSubhransu S. Prusty }; 14418382eadSSubhransu S. Prusty 1453787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev) 146b09b1c3bSUghreja, Rakesh A 147c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm * 148c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi, 149c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt) 150c9bfb5d7SJeeja KP { 151c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 1521de777feSJeeja KP 153c9bfb5d7SJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 154c9bfb5d7SJeeja KP if (pcm->cvt == cvt) 155c9bfb5d7SJeeja KP break; 156c9bfb5d7SJeeja KP } 157c9bfb5d7SJeeja KP 158c9bfb5d7SJeeja KP return pcm; 159c9bfb5d7SJeeja KP } 1601de777feSJeeja KP 161e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm, 162e0e5d3e5SJeeja KP struct hdac_hdmi_port *port, bool is_connect) 163e0e5d3e5SJeeja KP { 1643787a398SRakesh Ughreja struct hdac_device *hdev = port->pin->hdev; 165e0e5d3e5SJeeja KP 1660324e51bSJeeja KP if (is_connect) 1670324e51bSJeeja KP snd_soc_dapm_enable_pin(port->dapm, port->jack_pin); 1680324e51bSJeeja KP else 1690324e51bSJeeja KP snd_soc_dapm_disable_pin(port->dapm, port->jack_pin); 1700324e51bSJeeja KP 171e0e5d3e5SJeeja KP if (is_connect) { 172e0e5d3e5SJeeja KP /* 173e0e5d3e5SJeeja KP * Report Jack connect event when a device is connected 174e0e5d3e5SJeeja KP * for the first time where same PCM is attached to multiple 175e0e5d3e5SJeeja KP * ports. 176e0e5d3e5SJeeja KP */ 177e0e5d3e5SJeeja KP if (pcm->jack_event == 0) { 1783787a398SRakesh Ughreja dev_dbg(&hdev->dev, 179e0e5d3e5SJeeja KP "jack report for pcm=%d\n", 180e0e5d3e5SJeeja KP pcm->pcm_id); 18162490016SJeeja KP snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT, 18262490016SJeeja KP SND_JACK_AVOUT); 183e0e5d3e5SJeeja KP } 184e0e5d3e5SJeeja KP pcm->jack_event++; 185e0e5d3e5SJeeja KP } else { 186e0e5d3e5SJeeja KP /* 187e0e5d3e5SJeeja KP * Report Jack disconnect event when a device is disconnected 188e0e5d3e5SJeeja KP * is the only last connected device when same PCM is attached 189e0e5d3e5SJeeja KP * to multiple ports. 190e0e5d3e5SJeeja KP */ 191e0e5d3e5SJeeja KP if (pcm->jack_event == 1) 19262490016SJeeja KP snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT); 193e0e5d3e5SJeeja KP if (pcm->jack_event > 0) 194e0e5d3e5SJeeja KP pcm->jack_event--; 195e0e5d3e5SJeeja KP } 1960324e51bSJeeja KP 1970324e51bSJeeja KP snd_soc_dapm_sync(port->dapm); 198e0e5d3e5SJeeja KP } 199e0e5d3e5SJeeja KP 200fc181b04SJeeja KP /* MST supported verbs */ 201fc181b04SJeeja KP /* 202fc181b04SJeeja KP * Get the no devices that can be connected to a port on the Pin widget. 203fc181b04SJeeja KP */ 2043787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid) 205fc181b04SJeeja KP { 206fc181b04SJeeja KP unsigned int caps; 207fc181b04SJeeja KP unsigned int type, param; 208fc181b04SJeeja KP 2093787a398SRakesh Ughreja caps = get_wcaps(hdev, nid); 210fc181b04SJeeja KP type = get_wcaps_type(caps); 211fc181b04SJeeja KP 212fc181b04SJeeja KP if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN)) 213fc181b04SJeeja KP return 0; 214fc181b04SJeeja KP 2153787a398SRakesh Ughreja param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN); 216fc181b04SJeeja KP if (param == -1) 217fc181b04SJeeja KP return param; 218fc181b04SJeeja KP 219fc181b04SJeeja KP return param & AC_DEV_LIST_LEN_MASK; 220fc181b04SJeeja KP } 221fc181b04SJeeja KP 222fc181b04SJeeja KP /* 223fc181b04SJeeja KP * Get the port entry select on the pin. Return the port entry 224fc181b04SJeeja KP * id selected on the pin. Return 0 means the first port entry 225fc181b04SJeeja KP * is selected or MST is not supported. 226fc181b04SJeeja KP */ 2273787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev, 228fc181b04SJeeja KP struct hdac_hdmi_port *port) 229fc181b04SJeeja KP { 2303787a398SRakesh Ughreja return snd_hdac_codec_read(hdev, port->pin->nid, 231fc181b04SJeeja KP 0, AC_VERB_GET_DEVICE_SEL, 0); 232fc181b04SJeeja KP } 233fc181b04SJeeja KP 234fc181b04SJeeja KP /* 235fc181b04SJeeja KP * Sets the selected port entry for the configuring Pin widget verb. 236fc181b04SJeeja KP * returns error if port set is not equal to port get otherwise success 237fc181b04SJeeja KP */ 2383787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev, 239fc181b04SJeeja KP struct hdac_hdmi_port *port) 240fc181b04SJeeja KP { 241fc181b04SJeeja KP int num_ports; 242fc181b04SJeeja KP 243fc181b04SJeeja KP if (!port->pin->mst_capable) 244fc181b04SJeeja KP return 0; 245fc181b04SJeeja KP 246fc181b04SJeeja KP /* AC_PAR_DEVLIST_LEN is 0 based. */ 2473787a398SRakesh Ughreja num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid); 248fc181b04SJeeja KP if (num_ports < 0) 249fc181b04SJeeja KP return -EIO; 250fc181b04SJeeja KP /* 251fc181b04SJeeja KP * Device List Length is a 0 based integer value indicating the 252fc181b04SJeeja KP * number of sink device that a MST Pin Widget can support. 253fc181b04SJeeja KP */ 254fc181b04SJeeja KP if (num_ports + 1 < port->id) 255fc181b04SJeeja KP return 0; 256fc181b04SJeeja KP 2573787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 258fc181b04SJeeja KP AC_VERB_SET_DEVICE_SEL, port->id); 259fc181b04SJeeja KP 2603787a398SRakesh Ughreja if (port->id != hdac_hdmi_port_select_get(hdev, port)) 261fc181b04SJeeja KP return -EIO; 262fc181b04SJeeja KP 2633787a398SRakesh Ughreja dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id); 264fc181b04SJeeja KP 265fc181b04SJeeja KP return 0; 266fc181b04SJeeja KP } 267fc181b04SJeeja KP 2682889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi, 2692889099eSSubhransu S. Prusty int pcm_idx) 2702889099eSSubhransu S. Prusty { 2712889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 2722889099eSSubhransu S. Prusty 2732889099eSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 2742889099eSSubhransu S. Prusty if (pcm->pcm_id == pcm_idx) 2752889099eSSubhransu S. Prusty return pcm; 2762889099eSSubhransu S. Prusty } 2772889099eSSubhransu S. Prusty 2782889099eSSubhransu S. Prusty return NULL; 2792889099eSSubhransu S. Prusty } 2802889099eSSubhransu S. Prusty 2812428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad) 2822428bca3SSubhransu S. Prusty { 2832428bca3SSubhransu S. Prusty return ((sad[0] >> 0x3) & 0x1f); 2842428bca3SSubhransu S. Prusty } 2852428bca3SSubhransu S. Prusty 2862428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad) 2872428bca3SSubhransu S. Prusty { 2882428bca3SSubhransu S. Prusty return (sad[2] & 7); 2892428bca3SSubhransu S. Prusty } 2902428bca3SSubhransu S. Prusty 2912428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime, 2922428bca3SSubhransu S. Prusty void *eld) 2932428bca3SSubhransu S. Prusty { 2942428bca3SSubhransu S. Prusty u64 formats = SNDRV_PCM_FMTBIT_S16; 2952428bca3SSubhransu S. Prusty int i; 2962428bca3SSubhransu S. Prusty const u8 *sad, *eld_buf = eld; 2972428bca3SSubhransu S. Prusty 2982428bca3SSubhransu S. Prusty sad = drm_eld_sad(eld_buf); 2992428bca3SSubhransu S. Prusty if (!sad) 3002428bca3SSubhransu S. Prusty goto format_constraint; 3012428bca3SSubhransu S. Prusty 3022428bca3SSubhransu S. Prusty for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) { 3032428bca3SSubhransu S. Prusty if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */ 3042428bca3SSubhransu S. Prusty 3052428bca3SSubhransu S. Prusty /* 3062428bca3SSubhransu S. Prusty * the controller support 20 and 24 bits in 32 bit 3072428bca3SSubhransu S. Prusty * container so we set S32 3082428bca3SSubhransu S. Prusty */ 3092428bca3SSubhransu S. Prusty if (sad_sample_bits_lpcm(sad) & 0x6) 3102428bca3SSubhransu S. Prusty formats |= SNDRV_PCM_FMTBIT_S32; 3112428bca3SSubhransu S. Prusty } 3122428bca3SSubhransu S. Prusty } 3132428bca3SSubhransu S. Prusty 3142428bca3SSubhransu S. Prusty format_constraint: 3152428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, 3162428bca3SSubhransu S. Prusty formats); 3172428bca3SSubhransu S. Prusty 3182428bca3SSubhransu S. Prusty } 3192428bca3SSubhransu S. Prusty 320a657f1d0SSubhransu S. Prusty static void 3213787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid, 322a657f1d0SSubhransu S. Prusty int packet_index, int byte_index) 323a657f1d0SSubhransu S. Prusty { 324a657f1d0SSubhransu S. Prusty int val; 325a657f1d0SSubhransu S. Prusty 326a657f1d0SSubhransu S. Prusty val = (packet_index << 5) | (byte_index & 0x1f); 3273787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val); 328a657f1d0SSubhransu S. Prusty } 329a657f1d0SSubhransu S. Prusty 330478f544eSSubhransu S. Prusty struct dp_audio_infoframe { 331478f544eSSubhransu S. Prusty u8 type; /* 0x84 */ 332478f544eSSubhransu S. Prusty u8 len; /* 0x1b */ 333478f544eSSubhransu S. Prusty u8 ver; /* 0x11 << 2 */ 334478f544eSSubhransu S. Prusty 335478f544eSSubhransu S. Prusty u8 CC02_CT47; /* match with HDMI infoframe from this on */ 336478f544eSSubhransu S. Prusty u8 SS01_SF24; 337478f544eSSubhransu S. Prusty u8 CXT04; 338478f544eSSubhransu S. Prusty u8 CA; 339478f544eSSubhransu S. Prusty u8 LFEPBL01_LSV36_DM_INH7; 340478f544eSSubhransu S. Prusty }; 341478f544eSSubhransu S. Prusty 3423787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev, 343754695f9SJeeja KP struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port) 344a657f1d0SSubhransu S. Prusty { 345a657f1d0SSubhransu S. Prusty uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE]; 346a657f1d0SSubhransu S. Prusty struct hdmi_audio_infoframe frame; 347754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 348478f544eSSubhransu S. Prusty struct dp_audio_infoframe dp_ai; 3493787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 350ab1eea19SJeeja KP struct hdac_hdmi_cvt *cvt = pcm->cvt; 351478f544eSSubhransu S. Prusty u8 *dip; 352a657f1d0SSubhransu S. Prusty int ret; 353a657f1d0SSubhransu S. Prusty int i; 354478f544eSSubhransu S. Prusty const u8 *eld_buf; 355478f544eSSubhransu S. Prusty u8 conn_type; 356bcced704SSubhransu S. Prusty int channels, ca; 357a657f1d0SSubhransu S. Prusty 3583787a398SRakesh Ughreja ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc, 359ab1eea19SJeeja KP pcm->channels, pcm->chmap_set, true, pcm->chmap); 360bcced704SSubhransu S. Prusty 361bcced704SSubhransu S. Prusty channels = snd_hdac_get_active_channels(ca); 3623787a398SRakesh Ughreja hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels); 363bcced704SSubhransu S. Prusty 364bcced704SSubhransu S. Prusty snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca, 365ab1eea19SJeeja KP pcm->channels, pcm->chmap, pcm->chmap_set); 366bcced704SSubhransu S. Prusty 367754695f9SJeeja KP eld_buf = port->eld.eld_buffer; 368478f544eSSubhransu S. Prusty conn_type = drm_eld_get_conn_type(eld_buf); 369a657f1d0SSubhransu S. Prusty 370478f544eSSubhransu S. Prusty switch (conn_type) { 371478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_HDMI: 372478f544eSSubhransu S. Prusty hdmi_audio_infoframe_init(&frame); 373478f544eSSubhransu S. Prusty 374478f544eSSubhransu S. Prusty frame.channels = channels; 375bcced704SSubhransu S. Prusty frame.channel_allocation = ca; 376a657f1d0SSubhransu S. Prusty 377a657f1d0SSubhransu S. Prusty ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); 378a657f1d0SSubhransu S. Prusty if (ret < 0) 379a657f1d0SSubhransu S. Prusty return ret; 380a657f1d0SSubhransu S. Prusty 381478f544eSSubhransu S. Prusty break; 382478f544eSSubhransu S. Prusty 383478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_DP: 384478f544eSSubhransu S. Prusty memset(&dp_ai, 0, sizeof(dp_ai)); 385478f544eSSubhransu S. Prusty dp_ai.type = 0x84; 386478f544eSSubhransu S. Prusty dp_ai.len = 0x1b; 387478f544eSSubhransu S. Prusty dp_ai.ver = 0x11 << 2; 388478f544eSSubhransu S. Prusty dp_ai.CC02_CT47 = channels - 1; 389bcced704SSubhransu S. Prusty dp_ai.CA = ca; 390478f544eSSubhransu S. Prusty 391478f544eSSubhransu S. Prusty dip = (u8 *)&dp_ai; 392478f544eSSubhransu S. Prusty break; 393478f544eSSubhransu S. Prusty 394478f544eSSubhransu S. Prusty default: 3953787a398SRakesh Ughreja dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type); 396478f544eSSubhransu S. Prusty return -EIO; 397478f544eSSubhransu S. Prusty } 398478f544eSSubhransu S. Prusty 399a657f1d0SSubhransu S. Prusty /* stop infoframe transmission */ 4003787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 4013787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 402a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE); 403a657f1d0SSubhransu S. Prusty 404a657f1d0SSubhransu S. Prusty 405a657f1d0SSubhransu S. Prusty /* Fill infoframe. Index auto-incremented */ 4063787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 407478f544eSSubhransu S. Prusty if (conn_type == DRM_ELD_CONN_TYPE_HDMI) { 408391005e8SSubhransu S. Prusty for (i = 0; i < sizeof(buffer); i++) 4093787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 410391005e8SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, buffer[i]); 411478f544eSSubhransu S. Prusty } else { 412478f544eSSubhransu S. Prusty for (i = 0; i < sizeof(dp_ai); i++) 4133787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 414478f544eSSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, dip[i]); 415478f544eSSubhransu S. Prusty } 416a657f1d0SSubhransu S. Prusty 417a657f1d0SSubhransu S. Prusty /* Start infoframe */ 4183787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 4193787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 420a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST); 421a657f1d0SSubhransu S. Prusty 422a657f1d0SSubhransu S. Prusty return 0; 423a657f1d0SSubhransu S. Prusty } 424a657f1d0SSubhransu S. Prusty 425c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai, 426c9bfb5d7SJeeja KP unsigned int tx_mask, unsigned int rx_mask, 427c9bfb5d7SJeeja KP int slots, int slot_width) 428b0362adbSSubhransu S. Prusty { 4293787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 4303787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 431754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 432c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 433c9bfb5d7SJeeja KP 4343787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask); 435b0362adbSSubhransu S. Prusty 436b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 437b0362adbSSubhransu S. Prusty 438c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 439b0362adbSSubhransu S. Prusty 440c9bfb5d7SJeeja KP if (pcm) 441c9bfb5d7SJeeja KP pcm->stream_tag = (tx_mask << 4); 442bcced704SSubhransu S. Prusty 443c9bfb5d7SJeeja KP return 0; 444b0362adbSSubhransu S. Prusty } 445b0362adbSSubhransu S. Prusty 446b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream, 447b0362adbSSubhransu S. Prusty struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai) 448b0362adbSSubhransu S. Prusty { 4493787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 4503787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 451754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 452754695f9SJeeja KP struct hdac_hdmi_port *port; 453c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 454c9bfb5d7SJeeja KP int format; 455b0362adbSSubhransu S. Prusty 45654dfa1eaSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 457754695f9SJeeja KP port = dai_map->port; 45854dfa1eaSSubhransu S. Prusty 459754695f9SJeeja KP if (!port) 46054dfa1eaSSubhransu S. Prusty return -ENODEV; 46154dfa1eaSSubhransu S. Prusty 462754695f9SJeeja KP if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) { 4633787a398SRakesh Ughreja dev_err(&hdev->dev, 464754695f9SJeeja KP "device is not configured for this pin:port%d:%d\n", 465754695f9SJeeja KP port->pin->nid, port->id); 466b0362adbSSubhransu S. Prusty return -ENODEV; 467b0362adbSSubhransu S. Prusty } 468b0362adbSSubhransu S. Prusty 469c9bfb5d7SJeeja KP format = snd_hdac_calc_stream_format(params_rate(hparams), 470b0362adbSSubhransu S. Prusty params_channels(hparams), params_format(hparams), 47166d6bbc6SJeeja KP dai->driver->playback.sig_bits, 0); 472b0362adbSSubhransu S. Prusty 473c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 474c9bfb5d7SJeeja KP if (!pcm) 475148569fdSSubhransu S. Prusty return -EIO; 476148569fdSSubhransu S. Prusty 477c9bfb5d7SJeeja KP pcm->format = format; 478c9bfb5d7SJeeja KP pcm->channels = params_channels(hparams); 479148569fdSSubhransu S. Prusty 480148569fdSSubhransu S. Prusty return 0; 481148569fdSSubhransu S. Prusty } 482148569fdSSubhransu S. Prusty 4833787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev, 484754695f9SJeeja KP struct hdac_hdmi_pin *pin, 485754695f9SJeeja KP struct hdac_hdmi_port *port) 486148569fdSSubhransu S. Prusty { 4873787a398SRakesh Ughreja if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) { 4883787a398SRakesh Ughreja dev_warn(&hdev->dev, 489148569fdSSubhransu S. Prusty "HDMI: pin %d wcaps %#x does not support connection list\n", 4903787a398SRakesh Ughreja pin->nid, get_wcaps(hdev, pin->nid)); 491148569fdSSubhransu S. Prusty return -EINVAL; 492148569fdSSubhransu S. Prusty } 493148569fdSSubhransu S. Prusty 4943787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 4951b46ebd1SJeeja KP return -EIO; 4961b46ebd1SJeeja KP 4973787a398SRakesh Ughreja port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid, 498754695f9SJeeja KP port->mux_nids, HDA_MAX_CONNECTIONS); 499754695f9SJeeja KP if (port->num_mux_nids == 0) 5003787a398SRakesh Ughreja dev_warn(&hdev->dev, 501754695f9SJeeja KP "No connections found for pin:port %d:%d\n", 502754695f9SJeeja KP pin->nid, port->id); 503148569fdSSubhransu S. Prusty 5043787a398SRakesh Ughreja dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n", 505754695f9SJeeja KP port->num_mux_nids, pin->nid, port->id); 506148569fdSSubhransu S. Prusty 507754695f9SJeeja KP return port->num_mux_nids; 508148569fdSSubhransu S. Prusty } 509148569fdSSubhransu S. Prusty 510148569fdSSubhransu S. Prusty /* 511754695f9SJeeja KP * Query pcm list and return port to which stream is routed. 512148569fdSSubhransu S. Prusty * 513754695f9SJeeja KP * Also query connection list of the pin, to validate the cvt to port map. 514148569fdSSubhransu S. Prusty * 515754695f9SJeeja KP * Same stream rendering to multiple ports simultaneously can be done 516754695f9SJeeja KP * possibly, but not supported for now in driver. So return the first port 517148569fdSSubhransu S. Prusty * connected. 518148569fdSSubhransu S. Prusty */ 519754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt( 5203787a398SRakesh Ughreja struct hdac_device *hdev, 521148569fdSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, 522148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt) 523148569fdSSubhransu S. Prusty { 524148569fdSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 525754695f9SJeeja KP struct hdac_hdmi_port *port = NULL; 526148569fdSSubhransu S. Prusty int ret, i; 527148569fdSSubhransu S. Prusty 528148569fdSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 529148569fdSSubhransu S. Prusty if (pcm->cvt == cvt) { 530e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 531e0e5d3e5SJeeja KP continue; 532148569fdSSubhransu S. Prusty 533e0e5d3e5SJeeja KP list_for_each_entry(port, &pcm->port_list, head) { 534e0e5d3e5SJeeja KP mutex_lock(&pcm->lock); 5353787a398SRakesh Ughreja ret = hdac_hdmi_query_port_connlist(hdev, 536e0e5d3e5SJeeja KP port->pin, port); 537e0e5d3e5SJeeja KP mutex_unlock(&pcm->lock); 538148569fdSSubhransu S. Prusty if (ret < 0) 539e0e5d3e5SJeeja KP continue; 540148569fdSSubhransu S. Prusty 541754695f9SJeeja KP for (i = 0; i < port->num_mux_nids; i++) { 542e0e5d3e5SJeeja KP if (port->mux_nids[i] == cvt->nid && 543e0e5d3e5SJeeja KP port->eld.monitor_present && 544e0e5d3e5SJeeja KP port->eld.eld_valid) 545754695f9SJeeja KP return port; 546148569fdSSubhransu S. Prusty } 547148569fdSSubhransu S. Prusty } 548e0e5d3e5SJeeja KP } 549e0e5d3e5SJeeja KP } 550148569fdSSubhransu S. Prusty 551148569fdSSubhransu S. Prusty return NULL; 552148569fdSSubhransu S. Prusty } 553148569fdSSubhransu S. Prusty 55454dfa1eaSSubhransu S. Prusty /* 55554dfa1eaSSubhransu S. Prusty * This tries to get a valid pin and set the HW constraints based on the 55654dfa1eaSSubhransu S. Prusty * ELD. Even if a valid pin is not found return success so that device open 55754dfa1eaSSubhransu S. Prusty * doesn't fail. 55854dfa1eaSSubhransu S. Prusty */ 559b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream, 560b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 561b0362adbSSubhransu S. Prusty { 5623787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 5633787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 564754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 565148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 566754695f9SJeeja KP struct hdac_hdmi_port *port; 5672428bca3SSubhransu S. Prusty int ret; 568b0362adbSSubhransu S. Prusty 569b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 570b0362adbSSubhransu S. Prusty 571148569fdSSubhransu S. Prusty cvt = dai_map->cvt; 5723787a398SRakesh Ughreja port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt); 57354dfa1eaSSubhransu S. Prusty 57454dfa1eaSSubhransu S. Prusty /* 57554dfa1eaSSubhransu S. Prusty * To make PA and other userland happy. 57654dfa1eaSSubhransu S. Prusty * userland scans devices so returning error does not help. 57754dfa1eaSSubhransu S. Prusty */ 578754695f9SJeeja KP if (!port) 57954dfa1eaSSubhransu S. Prusty return 0; 580754695f9SJeeja KP if ((!port->eld.monitor_present) || 581754695f9SJeeja KP (!port->eld.eld_valid)) { 582b0362adbSSubhransu S. Prusty 5833787a398SRakesh Ughreja dev_warn(&hdev->dev, 584754695f9SJeeja KP "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n", 585754695f9SJeeja KP port->eld.monitor_present, port->eld.eld_valid, 586754695f9SJeeja KP port->pin->nid, port->id); 587b8a54545SSubhransu S. Prusty 58854dfa1eaSSubhransu S. Prusty return 0; 589b0362adbSSubhransu S. Prusty } 590b0362adbSSubhransu S. Prusty 591754695f9SJeeja KP dai_map->port = port; 592b0362adbSSubhransu S. Prusty 5932428bca3SSubhransu S. Prusty ret = hdac_hdmi_eld_limit_formats(substream->runtime, 594754695f9SJeeja KP port->eld.eld_buffer); 5952428bca3SSubhransu S. Prusty if (ret < 0) 5962428bca3SSubhransu S. Prusty return ret; 597b0362adbSSubhransu S. Prusty 5982428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_eld(substream->runtime, 599754695f9SJeeja KP port->eld.eld_buffer); 600b0362adbSSubhransu S. Prusty } 601b0362adbSSubhransu S. Prusty 602b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream, 603b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 604b0362adbSSubhransu S. Prusty { 6053787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 606754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 607ab1eea19SJeeja KP struct hdac_hdmi_pcm *pcm; 608b0362adbSSubhransu S. Prusty 609b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 610b0362adbSSubhransu S. Prusty 611ab1eea19SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 612bcced704SSubhransu S. Prusty 613ab1eea19SJeeja KP if (pcm) { 614ab1eea19SJeeja KP mutex_lock(&pcm->lock); 615ab1eea19SJeeja KP pcm->chmap_set = false; 616ab1eea19SJeeja KP memset(pcm->chmap, 0, sizeof(pcm->chmap)); 617ab1eea19SJeeja KP pcm->channels = 0; 618ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 619b0362adbSSubhransu S. Prusty } 620ab1eea19SJeeja KP 621754695f9SJeeja KP if (dai_map->port) 622754695f9SJeeja KP dai_map->port = NULL; 62354dfa1eaSSubhransu S. Prusty } 624b0362adbSSubhransu S. Prusty 62518382eadSSubhransu S. Prusty static int 626f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt) 62718382eadSSubhransu S. Prusty { 628bcced704SSubhransu S. Prusty unsigned int chans; 629f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 63018382eadSSubhransu S. Prusty int err; 63118382eadSSubhransu S. Prusty 632f0c5ebebSUghreja, Rakesh A chans = get_wcaps(hdev, cvt->nid); 633bcced704SSubhransu S. Prusty chans = get_wcaps_channels(chans); 634bcced704SSubhransu S. Prusty 635bcced704SSubhransu S. Prusty cvt->params.channels_min = 2; 636bcced704SSubhransu S. Prusty 637bcced704SSubhransu S. Prusty cvt->params.channels_max = chans; 638bcced704SSubhransu S. Prusty if (chans > hdmi->chmap.channels_max) 639bcced704SSubhransu S. Prusty hdmi->chmap.channels_max = chans; 64018382eadSSubhransu S. Prusty 641f0c5ebebSUghreja, Rakesh A err = snd_hdac_query_supported_pcm(hdev, cvt->nid, 64218382eadSSubhransu S. Prusty &cvt->params.rates, 64318382eadSSubhransu S. Prusty &cvt->params.formats, 64418382eadSSubhransu S. Prusty &cvt->params.maxbps); 64518382eadSSubhransu S. Prusty if (err < 0) 646f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, 64718382eadSSubhransu S. Prusty "Failed to query pcm params for nid %d: %d\n", 64818382eadSSubhransu S. Prusty cvt->nid, err); 64918382eadSSubhransu S. Prusty 65018382eadSSubhransu S. Prusty return err; 65118382eadSSubhransu S. Prusty } 65218382eadSSubhransu S. Prusty 65379f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev, 654c9bfb5d7SJeeja KP struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id, 655c9bfb5d7SJeeja KP void *priv, const char *wname, const char *stream, 656c9bfb5d7SJeeja KP struct snd_kcontrol_new *wc, int numkc, 657c9bfb5d7SJeeja KP int (*event)(struct snd_soc_dapm_widget *, 658c9bfb5d7SJeeja KP struct snd_kcontrol *, int), unsigned short event_flags) 65918382eadSSubhransu S. Prusty { 66018382eadSSubhransu S. Prusty w->id = id; 66179f4e922SSubhransu S. Prusty w->name = devm_kstrdup(dev, wname, GFP_KERNEL); 66279f4e922SSubhransu S. Prusty if (!w->name) 66379f4e922SSubhransu S. Prusty return -ENOMEM; 66479f4e922SSubhransu S. Prusty 66518382eadSSubhransu S. Prusty w->sname = stream; 66618382eadSSubhransu S. Prusty w->reg = SND_SOC_NOPM; 66718382eadSSubhransu S. Prusty w->shift = 0; 66879f4e922SSubhransu S. Prusty w->kcontrol_news = wc; 66979f4e922SSubhransu S. Prusty w->num_kcontrols = numkc; 67079f4e922SSubhransu S. Prusty w->priv = priv; 671c9bfb5d7SJeeja KP w->event = event; 672c9bfb5d7SJeeja KP w->event_flags = event_flags; 67379f4e922SSubhransu S. Prusty 67479f4e922SSubhransu S. Prusty return 0; 67518382eadSSubhransu S. Prusty } 67618382eadSSubhransu S. Prusty 67718382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route, 67879f4e922SSubhransu S. Prusty const char *sink, const char *control, const char *src, 67979f4e922SSubhransu S. Prusty int (*handler)(struct snd_soc_dapm_widget *src, 68079f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *sink)) 68118382eadSSubhransu S. Prusty { 68218382eadSSubhransu S. Prusty route->sink = sink; 68318382eadSSubhransu S. Prusty route->source = src; 68418382eadSSubhransu S. Prusty route->control = control; 68579f4e922SSubhransu S. Prusty route->connected = handler; 68618382eadSSubhransu S. Prusty } 68718382eadSSubhransu S. Prusty 6883787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev, 689754695f9SJeeja KP struct hdac_hdmi_port *port) 6904a3478deSJeeja KP { 6913787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 6924a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 693e0e5d3e5SJeeja KP struct hdac_hdmi_port *p; 6944a3478deSJeeja KP 6954a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 696e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 697754695f9SJeeja KP continue; 698754695f9SJeeja KP 699e0e5d3e5SJeeja KP list_for_each_entry(p, &pcm->port_list, head) { 700e0e5d3e5SJeeja KP if (p->id == port->id && port->pin == p->pin) 7014a3478deSJeeja KP return pcm; 7024a3478deSJeeja KP } 703e0e5d3e5SJeeja KP } 7044a3478deSJeeja KP 7054a3478deSJeeja KP return NULL; 7064a3478deSJeeja KP } 7074a3478deSJeeja KP 7083787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev, 709c9bfb5d7SJeeja KP hda_nid_t nid, unsigned int pwr_state) 710c9bfb5d7SJeeja KP { 711753597fbSAbhijeet Kumar int count; 712753597fbSAbhijeet Kumar unsigned int state; 713753597fbSAbhijeet Kumar 7143787a398SRakesh Ughreja if (get_wcaps(hdev, nid) & AC_WCAP_POWER) { 7153787a398SRakesh Ughreja if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) { 716753597fbSAbhijeet Kumar for (count = 0; count < 10; count++) { 7173787a398SRakesh Ughreja snd_hdac_codec_read(hdev, nid, 0, 718753597fbSAbhijeet Kumar AC_VERB_SET_POWER_STATE, 719753597fbSAbhijeet Kumar pwr_state); 7203787a398SRakesh Ughreja state = snd_hdac_sync_power_state(hdev, 721753597fbSAbhijeet Kumar nid, pwr_state); 722753597fbSAbhijeet Kumar if (!(state & AC_PWRST_ERROR)) 723753597fbSAbhijeet Kumar break; 724753597fbSAbhijeet Kumar } 725753597fbSAbhijeet Kumar } 726c9bfb5d7SJeeja KP } 727c9bfb5d7SJeeja KP } 728c9bfb5d7SJeeja KP 7293787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev, 730c9bfb5d7SJeeja KP hda_nid_t nid, int val) 731c9bfb5d7SJeeja KP { 7323787a398SRakesh Ughreja if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP) 7333787a398SRakesh Ughreja snd_hdac_codec_write(hdev, nid, 0, 734c9bfb5d7SJeeja KP AC_VERB_SET_AMP_GAIN_MUTE, val); 735c9bfb5d7SJeeja KP } 736c9bfb5d7SJeeja KP 737c9bfb5d7SJeeja KP 738c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w, 739c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 740c9bfb5d7SJeeja KP { 741754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 7423787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 743c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 744c9bfb5d7SJeeja KP 7453787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 746c9bfb5d7SJeeja KP __func__, w->name, event); 747c9bfb5d7SJeeja KP 7483787a398SRakesh Ughreja pcm = hdac_hdmi_get_pcm(hdev, port); 749c9bfb5d7SJeeja KP if (!pcm) 750c9bfb5d7SJeeja KP return -EIO; 751c9bfb5d7SJeeja KP 7521b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 7533787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 7541b46ebd1SJeeja KP return -EIO; 7551b46ebd1SJeeja KP 756c9bfb5d7SJeeja KP switch (event) { 757c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 7583787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0); 759c9bfb5d7SJeeja KP 760c9bfb5d7SJeeja KP /* Enable out path for this pin widget */ 7613787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 762c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); 763c9bfb5d7SJeeja KP 7643787a398SRakesh Ughreja hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE); 765c9bfb5d7SJeeja KP 7663787a398SRakesh Ughreja return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port); 767c9bfb5d7SJeeja KP 768c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 7693787a398SRakesh Ughreja hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE); 770c9bfb5d7SJeeja KP 771c9bfb5d7SJeeja KP /* Disable out path for this pin widget */ 7723787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 773c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, 0); 774c9bfb5d7SJeeja KP 7753787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3); 776c9bfb5d7SJeeja KP break; 777c9bfb5d7SJeeja KP 778c9bfb5d7SJeeja KP } 779c9bfb5d7SJeeja KP 780c9bfb5d7SJeeja KP return 0; 781c9bfb5d7SJeeja KP } 782c9bfb5d7SJeeja KP 783c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w, 784c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 785c9bfb5d7SJeeja KP { 786c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt = w->priv; 7873787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 7883787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 789c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 790c9bfb5d7SJeeja KP 7913787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 792c9bfb5d7SJeeja KP __func__, w->name, event); 793c9bfb5d7SJeeja KP 794c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt); 795c9bfb5d7SJeeja KP if (!pcm) 796c9bfb5d7SJeeja KP return -EIO; 797c9bfb5d7SJeeja KP 798c9bfb5d7SJeeja KP switch (event) { 799c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 8003787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0); 801c9bfb5d7SJeeja KP 802c9bfb5d7SJeeja KP /* Enable transmission */ 8033787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 804c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_1, 1); 805c9bfb5d7SJeeja KP 806c9bfb5d7SJeeja KP /* Category Code (CC) to zero */ 8073787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 808c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_2, 0); 809c9bfb5d7SJeeja KP 8103787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 811c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag); 8123787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 813c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, pcm->format); 814c9bfb5d7SJeeja KP break; 815c9bfb5d7SJeeja KP 816c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 8173787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 818c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, 0); 8193787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 820c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, 0); 821c9bfb5d7SJeeja KP 8223787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3); 823c9bfb5d7SJeeja KP break; 824c9bfb5d7SJeeja KP 825c9bfb5d7SJeeja KP } 826c9bfb5d7SJeeja KP 827c9bfb5d7SJeeja KP return 0; 828c9bfb5d7SJeeja KP } 829c9bfb5d7SJeeja KP 830c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w, 831c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 832c9bfb5d7SJeeja KP { 833754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 8343787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 835c9bfb5d7SJeeja KP int mux_idx; 836c9bfb5d7SJeeja KP 8373787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 838c9bfb5d7SJeeja KP __func__, w->name, event); 839c9bfb5d7SJeeja KP 840c9bfb5d7SJeeja KP if (!kc) 841c9bfb5d7SJeeja KP kc = w->kcontrols[0]; 842c9bfb5d7SJeeja KP 843c9bfb5d7SJeeja KP mux_idx = dapm_kcontrol_get_value(kc); 8441b46ebd1SJeeja KP 8451b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 8463787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 8471b46ebd1SJeeja KP return -EIO; 8481b46ebd1SJeeja KP 849c9bfb5d7SJeeja KP if (mux_idx > 0) { 8503787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 851c9bfb5d7SJeeja KP AC_VERB_SET_CONNECT_SEL, (mux_idx - 1)); 852c9bfb5d7SJeeja KP } 853c9bfb5d7SJeeja KP 854c9bfb5d7SJeeja KP return 0; 855c9bfb5d7SJeeja KP } 856c9bfb5d7SJeeja KP 8574a3478deSJeeja KP /* 8584a3478deSJeeja KP * Based on user selection, map the PINs with the PCMs. 8594a3478deSJeeja KP */ 860754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol, 8614a3478deSJeeja KP struct snd_ctl_elem_value *ucontrol) 8624a3478deSJeeja KP { 8634a3478deSJeeja KP int ret; 864e0e5d3e5SJeeja KP struct hdac_hdmi_port *p, *p_next; 8654a3478deSJeeja KP struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 8664a3478deSJeeja KP struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 8674a3478deSJeeja KP struct snd_soc_dapm_context *dapm = w->dapm; 868754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 8693787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev); 8703787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 8714a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 8724a3478deSJeeja KP const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]]; 8734a3478deSJeeja KP 8744a3478deSJeeja KP ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 8754a3478deSJeeja KP if (ret < 0) 8764a3478deSJeeja KP return ret; 8774a3478deSJeeja KP 878754695f9SJeeja KP if (port == NULL) 879754695f9SJeeja KP return -EINVAL; 880754695f9SJeeja KP 8814a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 8824a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 883e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 884e0e5d3e5SJeeja KP continue; 885e0e5d3e5SJeeja KP 886e0e5d3e5SJeeja KP list_for_each_entry_safe(p, p_next, &pcm->port_list, head) { 887e0e5d3e5SJeeja KP if (p == port && p->id == port->id && 888e0e5d3e5SJeeja KP p->pin == port->pin) { 889e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, false); 890e0e5d3e5SJeeja KP list_del(&p->head); 891e0e5d3e5SJeeja KP } 892e0e5d3e5SJeeja KP } 893e0e5d3e5SJeeja KP } 8944a3478deSJeeja KP 8954a3478deSJeeja KP /* 8964a3478deSJeeja KP * Jack status is not reported during device probe as the 8974a3478deSJeeja KP * PCMs are not registered by then. So report it here. 8984a3478deSJeeja KP */ 899e0e5d3e5SJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 900e0e5d3e5SJeeja KP if (!strcmp(cvt_name, pcm->cvt->name)) { 901e0e5d3e5SJeeja KP list_add_tail(&port->head, &pcm->port_list); 902754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 903e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, true); 9044a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 9054a3478deSJeeja KP return ret; 9064a3478deSJeeja KP } 9074a3478deSJeeja KP } 908e0e5d3e5SJeeja KP } 9094a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 9104a3478deSJeeja KP 9114a3478deSJeeja KP return ret; 9124a3478deSJeeja KP } 9134a3478deSJeeja KP 91479f4e922SSubhransu S. Prusty /* 91579f4e922SSubhransu S. Prusty * Ideally the Mux inputs should be based on the num_muxs enumerated, but 91679f4e922SSubhransu S. Prusty * the display driver seem to be programming the connection list for the pin 91779f4e922SSubhransu S. Prusty * widget runtime. 91879f4e922SSubhransu S. Prusty * 91979f4e922SSubhransu S. Prusty * So programming all the possible inputs for the mux, the user has to take 92079f4e922SSubhransu S. Prusty * care of selecting the right one and leaving all other inputs selected to 92179f4e922SSubhransu S. Prusty * "NONE" 92279f4e922SSubhransu S. Prusty */ 9233787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev, 924754695f9SJeeja KP struct hdac_hdmi_port *port, 92579f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widget, 92679f4e922SSubhransu S. Prusty const char *widget_name) 92718382eadSSubhransu S. Prusty { 9283787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 929754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 93079f4e922SSubhransu S. Prusty struct snd_kcontrol_new *kc; 93179f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 93279f4e922SSubhransu S. Prusty struct soc_enum *se; 93379f4e922SSubhransu S. Prusty char kc_name[NAME_SIZE]; 93479f4e922SSubhransu S. Prusty char mux_items[NAME_SIZE]; 93579f4e922SSubhransu S. Prusty /* To hold inputs to the Pin mux */ 93679f4e922SSubhransu S. Prusty char *items[HDA_MAX_CONNECTIONS]; 93779f4e922SSubhransu S. Prusty int i = 0; 93879f4e922SSubhransu S. Prusty int num_items = hdmi->num_cvt + 1; 93918382eadSSubhransu S. Prusty 9403787a398SRakesh Ughreja kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL); 94179f4e922SSubhransu S. Prusty if (!kc) 94279f4e922SSubhransu S. Prusty return -ENOMEM; 94318382eadSSubhransu S. Prusty 9443787a398SRakesh Ughreja se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL); 94579f4e922SSubhransu S. Prusty if (!se) 94679f4e922SSubhransu S. Prusty return -ENOMEM; 94718382eadSSubhransu S. Prusty 94870e97a2dSSubhransu S. Prusty snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input", 94970e97a2dSSubhransu S. Prusty pin->nid, port->id); 9503787a398SRakesh Ughreja kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL); 95179f4e922SSubhransu S. Prusty if (!kc->name) 95279f4e922SSubhransu S. Prusty return -ENOMEM; 95318382eadSSubhransu S. Prusty 95479f4e922SSubhransu S. Prusty kc->private_value = (long)se; 95579f4e922SSubhransu S. Prusty kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 95679f4e922SSubhransu S. Prusty kc->access = 0; 95779f4e922SSubhransu S. Prusty kc->info = snd_soc_info_enum_double; 958754695f9SJeeja KP kc->put = hdac_hdmi_set_pin_port_mux; 95979f4e922SSubhransu S. Prusty kc->get = snd_soc_dapm_get_enum_double; 96079f4e922SSubhransu S. Prusty 96179f4e922SSubhransu S. Prusty se->reg = SND_SOC_NOPM; 96279f4e922SSubhransu S. Prusty 96379f4e922SSubhransu S. Prusty /* enum texts: ["NONE", "cvt #", "cvt #", ...] */ 96479f4e922SSubhransu S. Prusty se->items = num_items; 96579f4e922SSubhransu S. Prusty se->mask = roundup_pow_of_two(se->items) - 1; 96679f4e922SSubhransu S. Prusty 96779f4e922SSubhransu S. Prusty sprintf(mux_items, "NONE"); 9683787a398SRakesh Ughreja items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL); 96979f4e922SSubhransu S. Prusty if (!items[i]) 97079f4e922SSubhransu S. Prusty return -ENOMEM; 97179f4e922SSubhransu S. Prusty 97279f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 97379f4e922SSubhransu S. Prusty i++; 97479f4e922SSubhransu S. Prusty sprintf(mux_items, "cvt %d", cvt->nid); 9753787a398SRakesh Ughreja items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL); 97679f4e922SSubhransu S. Prusty if (!items[i]) 97779f4e922SSubhransu S. Prusty return -ENOMEM; 97879f4e922SSubhransu S. Prusty } 97979f4e922SSubhransu S. Prusty 9803787a398SRakesh Ughreja se->texts = devm_kmemdup(&hdev->dev, items, 98179f4e922SSubhransu S. Prusty (num_items * sizeof(char *)), GFP_KERNEL); 98279f4e922SSubhransu S. Prusty if (!se->texts) 98379f4e922SSubhransu S. Prusty return -ENOMEM; 98479f4e922SSubhransu S. Prusty 9853787a398SRakesh Ughreja return hdac_hdmi_fill_widget_info(&hdev->dev, widget, 986754695f9SJeeja KP snd_soc_dapm_mux, port, widget_name, NULL, kc, 1, 987c9bfb5d7SJeeja KP hdac_hdmi_pin_mux_widget_event, 988c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG); 98979f4e922SSubhransu S. Prusty } 99079f4e922SSubhransu S. Prusty 99179f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */ 9923787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev, 99379f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets, 99479f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route, int rindex) 99579f4e922SSubhransu S. Prusty { 9963787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 99779f4e922SSubhransu S. Prusty const struct snd_kcontrol_new *kc; 99879f4e922SSubhransu S. Prusty struct soc_enum *se; 999754695f9SJeeja KP int mux_index = hdmi->num_cvt + hdmi->num_ports; 100079f4e922SSubhransu S. Prusty int i, j; 100179f4e922SSubhransu S. Prusty 1002754695f9SJeeja KP for (i = 0; i < hdmi->num_ports; i++) { 100379f4e922SSubhransu S. Prusty kc = widgets[mux_index].kcontrol_news; 100479f4e922SSubhransu S. Prusty se = (struct soc_enum *)kc->private_value; 100579f4e922SSubhransu S. Prusty for (j = 0; j < hdmi->num_cvt; j++) { 100679f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[rindex], 100779f4e922SSubhransu S. Prusty widgets[mux_index].name, 100879f4e922SSubhransu S. Prusty se->texts[j + 1], 100979f4e922SSubhransu S. Prusty widgets[j].name, NULL); 101079f4e922SSubhransu S. Prusty 101179f4e922SSubhransu S. Prusty rindex++; 101279f4e922SSubhransu S. Prusty } 101379f4e922SSubhransu S. Prusty 101479f4e922SSubhransu S. Prusty mux_index++; 101579f4e922SSubhransu S. Prusty } 101679f4e922SSubhransu S. Prusty } 101779f4e922SSubhransu S. Prusty 101879f4e922SSubhransu S. Prusty /* 101979f4e922SSubhransu S. Prusty * Widgets are added in the below sequence 102079f4e922SSubhransu S. Prusty * Converter widgets for num converters enumerated 1021754695f9SJeeja KP * Pin-port widgets for num ports for Pins enumerated 1022754695f9SJeeja KP * Pin-port mux widgets to represent connenction list of pin widget 102379f4e922SSubhransu S. Prusty * 1024754695f9SJeeja KP * For each port, one Mux and One output widget is added 1025754695f9SJeeja KP * Total widgets elements = num_cvt + (num_ports * 2); 102679f4e922SSubhransu S. Prusty * 102779f4e922SSubhransu S. Prusty * Routes are added as below: 1028754695f9SJeeja KP * pin-port mux -> pin (based on num_ports) 1029754695f9SJeeja KP * cvt -> "Input sel control" -> pin-port_mux 103079f4e922SSubhransu S. Prusty * 103179f4e922SSubhransu S. Prusty * Total route elements: 1032754695f9SJeeja KP * num_ports + (pin_muxes * num_cvt) 103379f4e922SSubhransu S. Prusty */ 103479f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm) 103579f4e922SSubhransu S. Prusty { 103679f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets; 103779f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route; 10383787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev); 10393787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 10401e02dac3SKuninori Morimoto struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv; 104179f4e922SSubhransu S. Prusty char widget_name[NAME_SIZE]; 104279f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 104379f4e922SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 1044754695f9SJeeja KP int ret, i = 0, num_routes = 0, j; 104579f4e922SSubhransu S. Prusty 104679f4e922SSubhransu S. Prusty if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list)) 104779f4e922SSubhransu S. Prusty return -EINVAL; 104879f4e922SSubhransu S. Prusty 1049754695f9SJeeja KP widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) * 1050754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)), 105179f4e922SSubhransu S. Prusty GFP_KERNEL); 105279f4e922SSubhransu S. Prusty 105379f4e922SSubhransu S. Prusty if (!widgets) 105479f4e922SSubhransu S. Prusty return -ENOMEM; 105579f4e922SSubhransu S. Prusty 105679f4e922SSubhransu S. Prusty /* DAPM widgets to represent each converter widget */ 105779f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 105879f4e922SSubhransu S. Prusty sprintf(widget_name, "Converter %d", cvt->nid); 105979f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 1060c9bfb5d7SJeeja KP snd_soc_dapm_aif_in, cvt, 1061c9bfb5d7SJeeja KP widget_name, dai_drv[i].playback.stream_name, NULL, 0, 1062c9bfb5d7SJeeja KP hdac_hdmi_cvt_output_widget_event, 1063c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD); 106479f4e922SSubhransu S. Prusty if (ret < 0) 106579f4e922SSubhransu S. Prusty return ret; 106679f4e922SSubhransu S. Prusty i++; 106779f4e922SSubhransu S. Prusty } 106879f4e922SSubhransu S. Prusty 106979f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1070754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1071754695f9SJeeja KP sprintf(widget_name, "hif%d-%d Output", 1072754695f9SJeeja KP pin->nid, pin->ports[j].id); 107379f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 1074754695f9SJeeja KP snd_soc_dapm_output, &pin->ports[j], 1075c9bfb5d7SJeeja KP widget_name, NULL, NULL, 0, 1076c9bfb5d7SJeeja KP hdac_hdmi_pin_output_widget_event, 1077754695f9SJeeja KP SND_SOC_DAPM_PRE_PMU | 1078754695f9SJeeja KP SND_SOC_DAPM_POST_PMD); 107979f4e922SSubhransu S. Prusty if (ret < 0) 108079f4e922SSubhransu S. Prusty return ret; 10810324e51bSJeeja KP pin->ports[j].output_pin = widgets[i].name; 108279f4e922SSubhransu S. Prusty i++; 108379f4e922SSubhransu S. Prusty } 1084754695f9SJeeja KP } 108579f4e922SSubhransu S. Prusty 108679f4e922SSubhransu S. Prusty /* DAPM widgets to represent the connection list to pin widget */ 108779f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1088754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1089754695f9SJeeja KP sprintf(widget_name, "Pin%d-Port%d Mux", 1090754695f9SJeeja KP pin->nid, pin->ports[j].id); 10913787a398SRakesh Ughreja ret = hdac_hdmi_create_pin_port_muxs(hdev, 1092754695f9SJeeja KP &pin->ports[j], &widgets[i], 109379f4e922SSubhransu S. Prusty widget_name); 109479f4e922SSubhransu S. Prusty if (ret < 0) 109579f4e922SSubhransu S. Prusty return ret; 109679f4e922SSubhransu S. Prusty i++; 109779f4e922SSubhransu S. Prusty 109879f4e922SSubhransu S. Prusty /* For cvt to pin_mux mapping */ 109979f4e922SSubhransu S. Prusty num_routes += hdmi->num_cvt; 110079f4e922SSubhransu S. Prusty 110179f4e922SSubhransu S. Prusty /* For pin_mux to pin mapping */ 110279f4e922SSubhransu S. Prusty num_routes++; 110379f4e922SSubhransu S. Prusty } 1104754695f9SJeeja KP } 110579f4e922SSubhransu S. Prusty 110679f4e922SSubhransu S. Prusty route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes), 110779f4e922SSubhransu S. Prusty GFP_KERNEL); 110879f4e922SSubhransu S. Prusty if (!route) 110979f4e922SSubhransu S. Prusty return -ENOMEM; 111079f4e922SSubhransu S. Prusty 111179f4e922SSubhransu S. Prusty i = 0; 111279f4e922SSubhransu S. Prusty /* Add pin <- NULL <- mux route map */ 111379f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1114754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 111579f4e922SSubhransu S. Prusty int sink_index = i + hdmi->num_cvt; 1116754695f9SJeeja KP int src_index = sink_index + pin->num_ports * 1117754695f9SJeeja KP hdmi->num_pin; 111879f4e922SSubhransu S. Prusty 111979f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[i], 112079f4e922SSubhransu S. Prusty widgets[sink_index].name, NULL, 112179f4e922SSubhransu S. Prusty widgets[src_index].name, NULL); 112279f4e922SSubhransu S. Prusty i++; 1123754695f9SJeeja KP } 112479f4e922SSubhransu S. Prusty } 112579f4e922SSubhransu S. Prusty 11263787a398SRakesh Ughreja hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i); 112779f4e922SSubhransu S. Prusty 112879f4e922SSubhransu S. Prusty snd_soc_dapm_new_controls(dapm, widgets, 1129754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)); 113079f4e922SSubhransu S. Prusty 113179f4e922SSubhransu S. Prusty snd_soc_dapm_add_routes(dapm, route, num_routes); 113279f4e922SSubhransu S. Prusty snd_soc_dapm_new_widgets(dapm->card); 113379f4e922SSubhransu S. Prusty 113479f4e922SSubhransu S. Prusty return 0; 113579f4e922SSubhransu S. Prusty 113618382eadSSubhransu S. Prusty } 113718382eadSSubhransu S. Prusty 11383787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev) 113918382eadSSubhransu S. Prusty { 11403787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 1141754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 114215b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 1143148569fdSSubhransu S. Prusty int dai_id = 0; 114418382eadSSubhransu S. Prusty 1145148569fdSSubhransu S. Prusty if (list_empty(&hdmi->cvt_list)) 114615b91447SSubhransu S. Prusty return -EINVAL; 114718382eadSSubhransu S. Prusty 1148148569fdSSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 1149148569fdSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai_id]; 1150148569fdSSubhransu S. Prusty dai_map->dai_id = dai_id; 115115b91447SSubhransu S. Prusty dai_map->cvt = cvt; 115218382eadSSubhransu S. Prusty 1153148569fdSSubhransu S. Prusty dai_id++; 1154148569fdSSubhransu S. Prusty 1155148569fdSSubhransu S. Prusty if (dai_id == HDA_MAX_CVTS) { 11563787a398SRakesh Ughreja dev_warn(&hdev->dev, 1157148569fdSSubhransu S. Prusty "Max dais supported: %d\n", dai_id); 1158148569fdSSubhransu S. Prusty break; 1159148569fdSSubhransu S. Prusty } 1160148569fdSSubhransu S. Prusty } 116118382eadSSubhransu S. Prusty 116215b91447SSubhransu S. Prusty return 0; 116315b91447SSubhransu S. Prusty } 116415b91447SSubhransu S. Prusty 11653787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid) 116615b91447SSubhransu S. Prusty { 11673787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 116815b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 11694a3478deSJeeja KP char name[NAME_SIZE]; 117015b91447SSubhransu S. Prusty 117115b91447SSubhransu S. Prusty cvt = kzalloc(sizeof(*cvt), GFP_KERNEL); 117215b91447SSubhransu S. Prusty if (!cvt) 117315b91447SSubhransu S. Prusty return -ENOMEM; 117415b91447SSubhransu S. Prusty 117515b91447SSubhransu S. Prusty cvt->nid = nid; 11764a3478deSJeeja KP sprintf(name, "cvt %d", cvt->nid); 11774a3478deSJeeja KP cvt->name = kstrdup(name, GFP_KERNEL); 117815b91447SSubhransu S. Prusty 117915b91447SSubhransu S. Prusty list_add_tail(&cvt->head, &hdmi->cvt_list); 118015b91447SSubhransu S. Prusty hdmi->num_cvt++; 118115b91447SSubhransu S. Prusty 11823787a398SRakesh Ughreja return hdac_hdmi_query_cvt_params(hdev, cvt); 118315b91447SSubhransu S. Prusty } 118415b91447SSubhransu S. Prusty 11853787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev, 1186754695f9SJeeja KP struct hdac_hdmi_port *port) 1187b7756edeSSubhransu S. Prusty { 1188f6fa11a3SSandeep Tayal unsigned int ver, mnl; 1189f6fa11a3SSandeep Tayal 1190754695f9SJeeja KP ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK) 1191f6fa11a3SSandeep Tayal >> DRM_ELD_VER_SHIFT; 1192f6fa11a3SSandeep Tayal 1193f6fa11a3SSandeep Tayal if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) { 11943787a398SRakesh Ughreja dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver); 1195f6fa11a3SSandeep Tayal return -EINVAL; 1196b7756edeSSubhransu S. Prusty } 1197b7756edeSSubhransu S. Prusty 1198754695f9SJeeja KP mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] & 1199f6fa11a3SSandeep Tayal DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT; 1200f6fa11a3SSandeep Tayal 1201f6fa11a3SSandeep Tayal if (mnl > ELD_MAX_MNL) { 12023787a398SRakesh Ughreja dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl); 1203f6fa11a3SSandeep Tayal return -EINVAL; 1204f6fa11a3SSandeep Tayal } 1205f6fa11a3SSandeep Tayal 1206754695f9SJeeja KP port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER]; 1207f6fa11a3SSandeep Tayal 1208f6fa11a3SSandeep Tayal return 0; 1209f6fa11a3SSandeep Tayal } 1210f6fa11a3SSandeep Tayal 1211754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, 1212754695f9SJeeja KP struct hdac_hdmi_port *port) 1213b8a54545SSubhransu S. Prusty { 12143787a398SRakesh Ughreja struct hdac_device *hdev = pin->hdev; 12153787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 12164a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 1217754695f9SJeeja KP int size = 0; 12182acd8309SJeeja KP int port_id = -1; 1219754695f9SJeeja KP 1220754695f9SJeeja KP if (!hdmi) 1221754695f9SJeeja KP return; 12224a3478deSJeeja KP 12232acd8309SJeeja KP /* 12242acd8309SJeeja KP * In case of non MST pin, get_eld info API expectes port 12252acd8309SJeeja KP * to be -1. 12262acd8309SJeeja KP */ 12274a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 1228754695f9SJeeja KP port->eld.monitor_present = false; 1229f6fa11a3SSandeep Tayal 12302acd8309SJeeja KP if (pin->mst_capable) 12312acd8309SJeeja KP port_id = port->id; 12322acd8309SJeeja KP 12333787a398SRakesh Ughreja size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id, 1234754695f9SJeeja KP &port->eld.monitor_present, 1235754695f9SJeeja KP port->eld.eld_buffer, 1236f6fa11a3SSandeep Tayal ELD_MAX_SIZE); 1237f6fa11a3SSandeep Tayal 1238f6fa11a3SSandeep Tayal if (size > 0) { 1239f6fa11a3SSandeep Tayal size = min(size, ELD_MAX_SIZE); 12403787a398SRakesh Ughreja if (hdac_hdmi_parse_eld(hdev, port) < 0) 1241f6fa11a3SSandeep Tayal size = -EINVAL; 1242f6fa11a3SSandeep Tayal } 1243f6fa11a3SSandeep Tayal 1244f6fa11a3SSandeep Tayal if (size > 0) { 1245754695f9SJeeja KP port->eld.eld_valid = true; 1246754695f9SJeeja KP port->eld.eld_size = size; 1247f6fa11a3SSandeep Tayal } else { 1248754695f9SJeeja KP port->eld.eld_valid = false; 1249754695f9SJeeja KP port->eld.eld_size = 0; 1250f6fa11a3SSandeep Tayal } 1251b8a54545SSubhransu S. Prusty 12523787a398SRakesh Ughreja pcm = hdac_hdmi_get_pcm(hdev, port); 12534a3478deSJeeja KP 1254754695f9SJeeja KP if (!port->eld.monitor_present || !port->eld.eld_valid) { 1255b8a54545SSubhransu S. Prusty 12563787a398SRakesh Ughreja dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n", 1257754695f9SJeeja KP __func__, pin->nid, port->id); 12584a3478deSJeeja KP 12594a3478deSJeeja KP /* 12604a3478deSJeeja KP * PCMs are not registered during device probe, so don't 12614a3478deSJeeja KP * report jack here. It will be done in usermode mux 12624a3478deSJeeja KP * control select. 12634a3478deSJeeja KP */ 1264e0e5d3e5SJeeja KP if (pcm) 1265e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, false); 12664a3478deSJeeja KP 12674a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 1268f6fa11a3SSandeep Tayal return; 1269b8a54545SSubhransu S. Prusty } 1270b8a54545SSubhransu S. Prusty 1271754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 1272e0e5d3e5SJeeja KP if (pcm) 1273e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, true); 12744a3478deSJeeja KP 1275f6fa11a3SSandeep Tayal print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1, 1276754695f9SJeeja KP port->eld.eld_buffer, port->eld.eld_size, false); 1277754695f9SJeeja KP 1278754695f9SJeeja KP } 1279754695f9SJeeja KP mutex_unlock(&hdmi->pin_mutex); 12804a3478deSJeeja KP } 12814a3478deSJeeja KP 1282754695f9SJeeja KP static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi, 1283754695f9SJeeja KP struct hdac_hdmi_pin *pin) 1284754695f9SJeeja KP { 1285754695f9SJeeja KP struct hdac_hdmi_port *ports; 1286754695f9SJeeja KP int max_ports = HDA_MAX_PORTS; 1287754695f9SJeeja KP int i; 1288754695f9SJeeja KP 1289754695f9SJeeja KP /* 1290754695f9SJeeja KP * FIXME: max_port may vary for each platform, so pass this as 1291754695f9SJeeja KP * as driver data or query from i915 interface when this API is 1292754695f9SJeeja KP * implemented. 1293754695f9SJeeja KP */ 1294754695f9SJeeja KP 1295754695f9SJeeja KP ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL); 1296754695f9SJeeja KP if (!ports) 1297754695f9SJeeja KP return -ENOMEM; 1298754695f9SJeeja KP 1299754695f9SJeeja KP for (i = 0; i < max_ports; i++) { 1300754695f9SJeeja KP ports[i].id = i; 1301754695f9SJeeja KP ports[i].pin = pin; 1302754695f9SJeeja KP } 1303754695f9SJeeja KP pin->ports = ports; 1304754695f9SJeeja KP pin->num_ports = max_ports; 1305754695f9SJeeja KP return 0; 1306b8a54545SSubhransu S. Prusty } 1307b8a54545SSubhransu S. Prusty 13083787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid) 130915b91447SSubhransu S. Prusty { 13103787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 131115b91447SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 1312754695f9SJeeja KP int ret; 131315b91447SSubhransu S. Prusty 131415b91447SSubhransu S. Prusty pin = kzalloc(sizeof(*pin), GFP_KERNEL); 131515b91447SSubhransu S. Prusty if (!pin) 131615b91447SSubhransu S. Prusty return -ENOMEM; 131715b91447SSubhransu S. Prusty 131815b91447SSubhransu S. Prusty pin->nid = nid; 13192acd8309SJeeja KP pin->mst_capable = false; 13203787a398SRakesh Ughreja pin->hdev = hdev; 1321754695f9SJeeja KP ret = hdac_hdmi_add_ports(hdmi, pin); 1322754695f9SJeeja KP if (ret < 0) 1323754695f9SJeeja KP return ret; 132415b91447SSubhransu S. Prusty 132515b91447SSubhransu S. Prusty list_add_tail(&pin->head, &hdmi->pin_list); 132615b91447SSubhransu S. Prusty hdmi->num_pin++; 1327754695f9SJeeja KP hdmi->num_ports += pin->num_ports; 1328b8a54545SSubhransu S. Prusty 132915b91447SSubhransu S. Prusty return 0; 133018382eadSSubhransu S. Prusty } 133118382eadSSubhransu S. Prusty 1332211caab7SSubhransu S. Prusty #define INTEL_VENDOR_NID 0x08 13335622bc95SPradeep Tewani #define INTEL_GLK_VENDOR_NID 0x0b 1334211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81 1335211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781 1336211caab7SSubhransu S. Prusty #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */ 1337211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */ 1338211caab7SSubhransu S. Prusty 1339f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev) 1340211caab7SSubhransu S. Prusty { 1341211caab7SSubhransu S. Prusty unsigned int vendor_param; 1342f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 13435622bc95SPradeep Tewani unsigned int vendor_nid = hdmi->drv_data->vendor_nid; 1344211caab7SSubhransu S. Prusty 1345f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1346211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1347211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS) 1348211caab7SSubhransu S. Prusty return; 1349211caab7SSubhransu S. Prusty 1350211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_ALL_PIN_CVTS; 1351f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1352211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1353211caab7SSubhransu S. Prusty if (vendor_param == -1) 1354211caab7SSubhransu S. Prusty return; 1355211caab7SSubhransu S. Prusty } 1356211caab7SSubhransu S. Prusty 1357f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev) 1358211caab7SSubhransu S. Prusty { 1359211caab7SSubhransu S. Prusty unsigned int vendor_param; 1360f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 13615622bc95SPradeep Tewani unsigned int vendor_nid = hdmi->drv_data->vendor_nid; 1362211caab7SSubhransu S. Prusty 1363f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1364211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1365211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_DP12) 1366211caab7SSubhransu S. Prusty return; 1367211caab7SSubhransu S. Prusty 1368211caab7SSubhransu S. Prusty /* enable DP1.2 mode */ 1369211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_DP12; 1370f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1371211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1372211caab7SSubhransu S. Prusty if (vendor_param == -1) 1373211caab7SSubhransu S. Prusty return; 1374211caab7SSubhransu S. Prusty 1375211caab7SSubhransu S. Prusty } 1376211caab7SSubhransu S. Prusty 137761b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = { 137817a42c45SSubhransu S. Prusty .startup = hdac_hdmi_pcm_open, 137917a42c45SSubhransu S. Prusty .shutdown = hdac_hdmi_pcm_close, 138017a42c45SSubhransu S. Prusty .hw_params = hdac_hdmi_set_hw_params, 1381c9bfb5d7SJeeja KP .set_tdm_slot = hdac_hdmi_set_tdm_slot, 138217a42c45SSubhransu S. Prusty }; 138317a42c45SSubhransu S. Prusty 138417a42c45SSubhransu S. Prusty /* 138517a42c45SSubhransu S. Prusty * Each converter can support a stream independently. So a dai is created 138617a42c45SSubhransu S. Prusty * based on the number of converter queried. 138717a42c45SSubhransu S. Prusty */ 1388f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev, 138917a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, 139017a42c45SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, int num_dais) 139117a42c45SSubhransu S. Prusty { 139217a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais; 139317a42c45SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 139417a42c45SSubhransu S. Prusty char name[NAME_SIZE], dai_name[NAME_SIZE]; 139517a42c45SSubhransu S. Prusty int i = 0; 139617a42c45SSubhransu S. Prusty u32 rates, bps; 139717a42c45SSubhransu S. Prusty unsigned int rate_max = 384000, rate_min = 8000; 139817a42c45SSubhransu S. Prusty u64 formats; 139917a42c45SSubhransu S. Prusty int ret; 140017a42c45SSubhransu S. Prusty 1401f0c5ebebSUghreja, Rakesh A hdmi_dais = devm_kzalloc(&hdev->dev, 140217a42c45SSubhransu S. Prusty (sizeof(*hdmi_dais) * num_dais), 140317a42c45SSubhransu S. Prusty GFP_KERNEL); 140417a42c45SSubhransu S. Prusty if (!hdmi_dais) 140517a42c45SSubhransu S. Prusty return -ENOMEM; 140617a42c45SSubhransu S. Prusty 140717a42c45SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 1408f0c5ebebSUghreja, Rakesh A ret = snd_hdac_query_supported_pcm(hdev, cvt->nid, 140917a42c45SSubhransu S. Prusty &rates, &formats, &bps); 141017a42c45SSubhransu S. Prusty if (ret) 141117a42c45SSubhransu S. Prusty return ret; 141217a42c45SSubhransu S. Prusty 14133b857472SYong Zhi /* Filter out 44.1, 88.2 and 176.4Khz */ 14143b857472SYong Zhi rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 | 14153b857472SYong Zhi SNDRV_PCM_RATE_176400); 14163b857472SYong Zhi if (!rates) 14173b857472SYong Zhi return -EINVAL; 14183b857472SYong Zhi 141917a42c45SSubhransu S. Prusty sprintf(dai_name, "intel-hdmi-hifi%d", i+1); 1420f0c5ebebSUghreja, Rakesh A hdmi_dais[i].name = devm_kstrdup(&hdev->dev, 142117a42c45SSubhransu S. Prusty dai_name, GFP_KERNEL); 142217a42c45SSubhransu S. Prusty 142317a42c45SSubhransu S. Prusty if (!hdmi_dais[i].name) 142417a42c45SSubhransu S. Prusty return -ENOMEM; 142517a42c45SSubhransu S. Prusty 142617a42c45SSubhransu S. Prusty snprintf(name, sizeof(name), "hifi%d", i+1); 142717a42c45SSubhransu S. Prusty hdmi_dais[i].playback.stream_name = 1428f0c5ebebSUghreja, Rakesh A devm_kstrdup(&hdev->dev, name, GFP_KERNEL); 142917a42c45SSubhransu S. Prusty if (!hdmi_dais[i].playback.stream_name) 143017a42c45SSubhransu S. Prusty return -ENOMEM; 143117a42c45SSubhransu S. Prusty 143217a42c45SSubhransu S. Prusty /* 143317a42c45SSubhransu S. Prusty * Set caps based on capability queried from the converter. 143417a42c45SSubhransu S. Prusty * It will be constrained runtime based on ELD queried. 143517a42c45SSubhransu S. Prusty */ 143617a42c45SSubhransu S. Prusty hdmi_dais[i].playback.formats = formats; 143717a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rates = rates; 143817a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_max = rate_max; 143917a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_min = rate_min; 144017a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_min = 2; 144117a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_max = 2; 144266d6bbc6SJeeja KP hdmi_dais[i].playback.sig_bits = bps; 144317a42c45SSubhransu S. Prusty hdmi_dais[i].ops = &hdmi_dai_ops; 144417a42c45SSubhransu S. Prusty i++; 144517a42c45SSubhransu S. Prusty } 144617a42c45SSubhransu S. Prusty 144717a42c45SSubhransu S. Prusty *dais = hdmi_dais; 14481e02dac3SKuninori Morimoto hdmi->dai_drv = hdmi_dais; 144917a42c45SSubhransu S. Prusty 145017a42c45SSubhransu S. Prusty return 0; 145117a42c45SSubhransu S. Prusty } 145217a42c45SSubhransu S. Prusty 145318382eadSSubhransu S. Prusty /* 145418382eadSSubhransu S. Prusty * Parse all nodes and store the cvt/pin nids in array 145518382eadSSubhransu S. Prusty * Add one time initialization for pin and cvt widgets 145618382eadSSubhransu S. Prusty */ 14573787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev, 145817a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, int *num_dais) 145918382eadSSubhransu S. Prusty { 146018382eadSSubhransu S. Prusty hda_nid_t nid; 14613c83ac23SSudip Mukherjee int i, num_nodes; 14621c0a7de2SSubhransu S. Prusty struct hdac_hdmi_cvt *temp_cvt, *cvt_next; 14631c0a7de2SSubhransu S. Prusty struct hdac_hdmi_pin *temp_pin, *pin_next; 14643787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 146515b91447SSubhransu S. Prusty int ret; 146618382eadSSubhransu S. Prusty 1467f0c5ebebSUghreja, Rakesh A hdac_hdmi_skl_enable_all_pins(hdev); 1468f0c5ebebSUghreja, Rakesh A hdac_hdmi_skl_enable_dp12(hdev); 1469211caab7SSubhransu S. Prusty 1470f0c5ebebSUghreja, Rakesh A num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid); 1471541140d4SSubhransu S. Prusty if (!nid || num_nodes <= 0) { 1472f0c5ebebSUghreja, Rakesh A dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n"); 147318382eadSSubhransu S. Prusty return -EINVAL; 147418382eadSSubhransu S. Prusty } 147518382eadSSubhransu S. Prusty 147645a6008bSPuneeth Prabhu for (i = 0; i < num_nodes; i++, nid++) { 147718382eadSSubhransu S. Prusty unsigned int caps; 147818382eadSSubhransu S. Prusty unsigned int type; 147918382eadSSubhransu S. Prusty 1480f0c5ebebSUghreja, Rakesh A caps = get_wcaps(hdev, nid); 148118382eadSSubhransu S. Prusty type = get_wcaps_type(caps); 148218382eadSSubhransu S. Prusty 148318382eadSSubhransu S. Prusty if (!(caps & AC_WCAP_DIGITAL)) 148418382eadSSubhransu S. Prusty continue; 148518382eadSSubhransu S. Prusty 148618382eadSSubhransu S. Prusty switch (type) { 148718382eadSSubhransu S. Prusty 148818382eadSSubhransu S. Prusty case AC_WID_AUD_OUT: 14893787a398SRakesh Ughreja ret = hdac_hdmi_add_cvt(hdev, nid); 149015b91447SSubhransu S. Prusty if (ret < 0) 14911c0a7de2SSubhransu S. Prusty goto free_widgets; 149218382eadSSubhransu S. Prusty break; 149318382eadSSubhransu S. Prusty 149418382eadSSubhransu S. Prusty case AC_WID_PIN: 14953787a398SRakesh Ughreja ret = hdac_hdmi_add_pin(hdev, nid); 149615b91447SSubhransu S. Prusty if (ret < 0) 14971c0a7de2SSubhransu S. Prusty goto free_widgets; 149818382eadSSubhransu S. Prusty break; 149918382eadSSubhransu S. Prusty } 150018382eadSSubhransu S. Prusty } 150118382eadSSubhransu S. Prusty 15021c0a7de2SSubhransu S. Prusty if (!hdmi->num_pin || !hdmi->num_cvt) { 15031c0a7de2SSubhransu S. Prusty ret = -EIO; 15041c0a7de2SSubhransu S. Prusty goto free_widgets; 15051c0a7de2SSubhransu S. Prusty } 150618382eadSSubhransu S. Prusty 1507f0c5ebebSUghreja, Rakesh A ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt); 150817a42c45SSubhransu S. Prusty if (ret) { 1509f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, "Failed to create dais with err: %d\n", 151017a42c45SSubhransu S. Prusty ret); 15111c0a7de2SSubhransu S. Prusty goto free_widgets; 151217a42c45SSubhransu S. Prusty } 151317a42c45SSubhransu S. Prusty 151417a42c45SSubhransu S. Prusty *num_dais = hdmi->num_cvt; 15153787a398SRakesh Ughreja ret = hdac_hdmi_init_dai_map(hdev); 15161c0a7de2SSubhransu S. Prusty if (ret < 0) 15171c0a7de2SSubhransu S. Prusty goto free_widgets; 151817a42c45SSubhransu S. Prusty 15191c0a7de2SSubhransu S. Prusty return ret; 15201c0a7de2SSubhransu S. Prusty 15211c0a7de2SSubhransu S. Prusty free_widgets: 15221c0a7de2SSubhransu S. Prusty list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) { 15231c0a7de2SSubhransu S. Prusty list_del(&temp_cvt->head); 15241c0a7de2SSubhransu S. Prusty kfree(temp_cvt->name); 15251c0a7de2SSubhransu S. Prusty kfree(temp_cvt); 15261c0a7de2SSubhransu S. Prusty } 15271c0a7de2SSubhransu S. Prusty 15281c0a7de2SSubhransu S. Prusty list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) { 15291c0a7de2SSubhransu S. Prusty for (i = 0; i < temp_pin->num_ports; i++) 15301c0a7de2SSubhransu S. Prusty temp_pin->ports[i].pin = NULL; 15311c0a7de2SSubhransu S. Prusty kfree(temp_pin->ports); 15321c0a7de2SSubhransu S. Prusty list_del(&temp_pin->head); 15331c0a7de2SSubhransu S. Prusty kfree(temp_pin); 15341c0a7de2SSubhransu S. Prusty } 15351c0a7de2SSubhransu S. Prusty 15361c0a7de2SSubhransu S. Prusty return ret; 153718382eadSSubhransu S. Prusty } 153818382eadSSubhransu S. Prusty 1539a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin) 1540a57942bfSTakashi Iwai { 1541a57942bfSTakashi Iwai return pin - 4; /* map NID 0x05 -> port #1 */ 1542a57942bfSTakashi Iwai } 1543a57942bfSTakashi Iwai 1544f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe) 1545b8a54545SSubhransu S. Prusty { 15463787a398SRakesh Ughreja struct hdac_device *hdev = aptr; 15473787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 1548754695f9SJeeja KP struct hdac_hdmi_pin *pin = NULL; 1549754695f9SJeeja KP struct hdac_hdmi_port *hport = NULL; 15503787a398SRakesh Ughreja struct snd_soc_component *component = hdmi->component; 15512acd8309SJeeja KP int i; 1552b8a54545SSubhransu S. Prusty 1553b8a54545SSubhransu S. Prusty /* Don't know how this mapping is derived */ 1554b8a54545SSubhransu S. Prusty hda_nid_t pin_nid = port + 0x04; 1555b8a54545SSubhransu S. Prusty 15563787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__, 1557754695f9SJeeja KP pin_nid, pipe); 1558b8a54545SSubhransu S. Prusty 1559b8a54545SSubhransu S. Prusty /* 1560b8a54545SSubhransu S. Prusty * skip notification during system suspend (but not in runtime PM); 1561b8a54545SSubhransu S. Prusty * the state will be updated at resume. Also since the ELD and 1562b8a54545SSubhransu S. Prusty * connection states are updated in anyway at the end of the resume, 1563b8a54545SSubhransu S. Prusty * we can skip it when received during PM process. 1564b8a54545SSubhransu S. Prusty */ 156545101122SKuninori Morimoto if (snd_power_get_state(component->card->snd_card) != 1566b8a54545SSubhransu S. Prusty SNDRV_CTL_POWER_D0) 1567b8a54545SSubhransu S. Prusty return; 1568b8a54545SSubhransu S. Prusty 15693787a398SRakesh Ughreja if (atomic_read(&hdev->in_pm)) 1570b8a54545SSubhransu S. Prusty return; 1571b8a54545SSubhransu S. Prusty 1572b8a54545SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1573754695f9SJeeja KP if (pin->nid != pin_nid) 1574754695f9SJeeja KP continue; 1575754695f9SJeeja KP 1576754695f9SJeeja KP /* In case of non MST pin, pipe is -1 */ 1577754695f9SJeeja KP if (pipe == -1) { 15782acd8309SJeeja KP pin->mst_capable = false; 1579754695f9SJeeja KP /* if not MST, default is port[0] */ 1580754695f9SJeeja KP hport = &pin->ports[0]; 15812acd8309SJeeja KP } else { 15822acd8309SJeeja KP for (i = 0; i < pin->num_ports; i++) { 15832acd8309SJeeja KP pin->mst_capable = true; 15842acd8309SJeeja KP if (pin->ports[i].id == pipe) { 15852acd8309SJeeja KP hport = &pin->ports[i]; 158604c8f2bfSJeeja KP break; 15872acd8309SJeeja KP } 1588b8a54545SSubhransu S. Prusty } 1589b8a54545SSubhransu S. Prusty } 1590b8a54545SSubhransu S. Prusty 159104c8f2bfSJeeja KP if (hport) 1592754695f9SJeeja KP hdac_hdmi_present_sense(pin, hport); 1593754695f9SJeeja KP } 1594754695f9SJeeja KP 159504c8f2bfSJeeja KP } 159604c8f2bfSJeeja KP 1597ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = { 1598a57942bfSTakashi Iwai .pin2port = hdac_hdmi_pin2port, 1599b8a54545SSubhransu S. Prusty .pin_eld_notify = hdac_hdmi_eld_notify_cb, 1600b8a54545SSubhransu S. Prusty }; 1601b8a54545SSubhransu S. Prusty 16022889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card, 16032889099eSSubhransu S. Prusty int device) 16042889099eSSubhransu S. Prusty { 16052889099eSSubhransu S. Prusty struct snd_soc_pcm_runtime *rtd; 16062889099eSSubhransu S. Prusty 1607bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 16082889099eSSubhransu S. Prusty if (rtd->pcm && (rtd->pcm->device == device)) 16092889099eSSubhransu S. Prusty return rtd->pcm; 16102889099eSSubhransu S. Prusty } 16112889099eSSubhransu S. Prusty 16122889099eSSubhransu S. Prusty return NULL; 16132889099eSSubhransu S. Prusty } 16142889099eSSubhransu S. Prusty 16150324e51bSJeeja KP /* create jack pin kcontrols */ 16160324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card, 16173787a398SRakesh Ughreja struct hdac_device *hdev) 16180324e51bSJeeja KP { 16190324e51bSJeeja KP struct hdac_hdmi_pin *pin; 16200324e51bSJeeja KP struct snd_kcontrol_new *kc; 16210324e51bSJeeja KP char kc_name[NAME_SIZE], xname[NAME_SIZE]; 16220324e51bSJeeja KP char *name; 16230324e51bSJeeja KP int i = 0, j; 16243787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 16253787a398SRakesh Ughreja struct snd_soc_component *component = hdmi->component; 16260324e51bSJeeja KP 162745101122SKuninori Morimoto kc = devm_kcalloc(component->dev, hdmi->num_ports, 16280324e51bSJeeja KP sizeof(*kc), GFP_KERNEL); 16290324e51bSJeeja KP 16300324e51bSJeeja KP if (!kc) 16310324e51bSJeeja KP return -ENOMEM; 16320324e51bSJeeja KP 16330324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 16340324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) { 16350324e51bSJeeja KP snprintf(xname, sizeof(xname), "hif%d-%d Jack", 16360324e51bSJeeja KP pin->nid, pin->ports[j].id); 163745101122SKuninori Morimoto name = devm_kstrdup(component->dev, xname, GFP_KERNEL); 16380324e51bSJeeja KP if (!name) 16390324e51bSJeeja KP return -ENOMEM; 16400324e51bSJeeja KP snprintf(kc_name, sizeof(kc_name), "%s Switch", xname); 164145101122SKuninori Morimoto kc[i].name = devm_kstrdup(component->dev, kc_name, 16420324e51bSJeeja KP GFP_KERNEL); 16430324e51bSJeeja KP if (!kc[i].name) 16440324e51bSJeeja KP return -ENOMEM; 16450324e51bSJeeja KP 16460324e51bSJeeja KP kc[i].private_value = (unsigned long)name; 16470324e51bSJeeja KP kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 16480324e51bSJeeja KP kc[i].access = 0; 16490324e51bSJeeja KP kc[i].info = snd_soc_dapm_info_pin_switch; 16500324e51bSJeeja KP kc[i].put = snd_soc_dapm_put_pin_switch; 16510324e51bSJeeja KP kc[i].get = snd_soc_dapm_get_pin_switch; 16520324e51bSJeeja KP i++; 16530324e51bSJeeja KP } 16540324e51bSJeeja KP } 16550324e51bSJeeja KP 16560324e51bSJeeja KP return snd_soc_add_card_controls(card, kc, i); 16570324e51bSJeeja KP } 16580324e51bSJeeja KP 165945101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component, 16600324e51bSJeeja KP struct snd_soc_dapm_context *dapm) 16610324e51bSJeeja KP { 16623787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 16633787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 16640324e51bSJeeja KP struct hdac_hdmi_pin *pin; 16650324e51bSJeeja KP struct snd_soc_dapm_widget *widgets; 16660324e51bSJeeja KP struct snd_soc_dapm_route *route; 16670324e51bSJeeja KP char w_name[NAME_SIZE]; 16680324e51bSJeeja KP int i = 0, j, ret; 16690324e51bSJeeja KP 16700324e51bSJeeja KP widgets = devm_kcalloc(dapm->dev, hdmi->num_ports, 16710324e51bSJeeja KP sizeof(*widgets), GFP_KERNEL); 16720324e51bSJeeja KP 16730324e51bSJeeja KP if (!widgets) 16740324e51bSJeeja KP return -ENOMEM; 16750324e51bSJeeja KP 16760324e51bSJeeja KP route = devm_kcalloc(dapm->dev, hdmi->num_ports, 16770324e51bSJeeja KP sizeof(*route), GFP_KERNEL); 16780324e51bSJeeja KP if (!route) 16790324e51bSJeeja KP return -ENOMEM; 16800324e51bSJeeja KP 16810324e51bSJeeja KP /* create Jack DAPM widget */ 16820324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 16830324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) { 16840324e51bSJeeja KP snprintf(w_name, sizeof(w_name), "hif%d-%d Jack", 16850324e51bSJeeja KP pin->nid, pin->ports[j].id); 16860324e51bSJeeja KP 16870324e51bSJeeja KP ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 16880324e51bSJeeja KP snd_soc_dapm_spk, NULL, 16890324e51bSJeeja KP w_name, NULL, NULL, 0, NULL, 0); 16900324e51bSJeeja KP if (ret < 0) 16910324e51bSJeeja KP return ret; 16920324e51bSJeeja KP 16930324e51bSJeeja KP pin->ports[j].jack_pin = widgets[i].name; 16940324e51bSJeeja KP pin->ports[j].dapm = dapm; 16950324e51bSJeeja KP 16960324e51bSJeeja KP /* add to route from Jack widget to output */ 16970324e51bSJeeja KP hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin, 16980324e51bSJeeja KP NULL, pin->ports[j].output_pin, NULL); 16990324e51bSJeeja KP 17000324e51bSJeeja KP i++; 17010324e51bSJeeja KP } 17020324e51bSJeeja KP } 17030324e51bSJeeja KP 17040324e51bSJeeja KP /* Add Route from Jack widget to the output widget */ 17050324e51bSJeeja KP ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports); 17060324e51bSJeeja KP if (ret < 0) 17070324e51bSJeeja KP return ret; 17080324e51bSJeeja KP 17090324e51bSJeeja KP ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports); 17100324e51bSJeeja KP if (ret < 0) 17110324e51bSJeeja KP return ret; 17120324e51bSJeeja KP 17130324e51bSJeeja KP ret = snd_soc_dapm_new_widgets(dapm->card); 17140324e51bSJeeja KP if (ret < 0) 17150324e51bSJeeja KP return ret; 17160324e51bSJeeja KP 17170324e51bSJeeja KP /* Add Jack Pin switch Kcontrol */ 17183787a398SRakesh Ughreja ret = create_fill_jack_kcontrols(dapm->card, hdev); 17190324e51bSJeeja KP 17200324e51bSJeeja KP if (ret < 0) 17210324e51bSJeeja KP return ret; 17220324e51bSJeeja KP 17230324e51bSJeeja KP /* default set the Jack Pin switch to OFF */ 17240324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 17250324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) 17260324e51bSJeeja KP snd_soc_dapm_disable_pin(pin->ports[j].dapm, 17270324e51bSJeeja KP pin->ports[j].jack_pin); 17280324e51bSJeeja KP } 17290324e51bSJeeja KP 17300324e51bSJeeja KP return 0; 17310324e51bSJeeja KP } 17320324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init); 17330324e51bSJeeja KP 173462490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device, 173562490016SJeeja KP struct snd_soc_jack *jack) 17364a3478deSJeeja KP { 173745101122SKuninori Morimoto struct snd_soc_component *component = dai->component; 17383787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 17393787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 17404a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 17412889099eSSubhransu S. Prusty struct snd_pcm *snd_pcm; 17422889099eSSubhransu S. Prusty int err; 17434a3478deSJeeja KP 17444a3478deSJeeja KP /* 17454a3478deSJeeja KP * this is a new PCM device, create new pcm and 17464a3478deSJeeja KP * add to the pcm list 17474a3478deSJeeja KP */ 17484a3478deSJeeja KP pcm = kzalloc(sizeof(*pcm), GFP_KERNEL); 17494a3478deSJeeja KP if (!pcm) 17504a3478deSJeeja KP return -ENOMEM; 17514a3478deSJeeja KP pcm->pcm_id = device; 17524a3478deSJeeja KP pcm->cvt = hdmi->dai_map[dai->id].cvt; 1753e0e5d3e5SJeeja KP pcm->jack_event = 0; 175462490016SJeeja KP pcm->jack = jack; 1755ab1eea19SJeeja KP mutex_init(&pcm->lock); 1756e0e5d3e5SJeeja KP INIT_LIST_HEAD(&pcm->port_list); 17572889099eSSubhransu S. Prusty snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device); 17582889099eSSubhransu S. Prusty if (snd_pcm) { 17592889099eSSubhransu S. Prusty err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap); 17602889099eSSubhransu S. Prusty if (err < 0) { 17613787a398SRakesh Ughreja dev_err(&hdev->dev, 17622889099eSSubhransu S. Prusty "chmap control add failed with err: %d for pcm: %d\n", 17632889099eSSubhransu S. Prusty err, device); 17642889099eSSubhransu S. Prusty kfree(pcm); 17652889099eSSubhransu S. Prusty return err; 17662889099eSSubhransu S. Prusty } 17672889099eSSubhransu S. Prusty } 17682889099eSSubhransu S. Prusty 17694a3478deSJeeja KP list_add_tail(&pcm->head, &hdmi->pcm_list); 17704a3478deSJeeja KP 177162490016SJeeja KP return 0; 17724a3478deSJeeja KP } 17734a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init); 17744a3478deSJeeja KP 17753787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev, 1776a9ce96bcSJeeja KP struct hdac_hdmi_priv *hdmi, bool detect_pin_caps) 1777a9ce96bcSJeeja KP { 1778a9ce96bcSJeeja KP int i; 1779a9ce96bcSJeeja KP struct hdac_hdmi_pin *pin; 1780a9ce96bcSJeeja KP 1781a9ce96bcSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 1782a9ce96bcSJeeja KP if (detect_pin_caps) { 1783a9ce96bcSJeeja KP 17843787a398SRakesh Ughreja if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0) 1785a9ce96bcSJeeja KP pin->mst_capable = false; 1786a9ce96bcSJeeja KP else 1787a9ce96bcSJeeja KP pin->mst_capable = true; 1788a9ce96bcSJeeja KP } 1789a9ce96bcSJeeja KP 1790a9ce96bcSJeeja KP for (i = 0; i < pin->num_ports; i++) { 1791a9ce96bcSJeeja KP if (!pin->mst_capable && i > 0) 1792a9ce96bcSJeeja KP continue; 1793a9ce96bcSJeeja KP 1794a9ce96bcSJeeja KP hdac_hdmi_present_sense(pin, &pin->ports[i]); 1795a9ce96bcSJeeja KP } 1796a9ce96bcSJeeja KP } 1797a9ce96bcSJeeja KP } 1798a9ce96bcSJeeja KP 179945101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component) 180018382eadSSubhransu S. Prusty { 18013787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 18023787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 180318382eadSSubhransu S. Prusty struct snd_soc_dapm_context *dapm = 180445101122SKuninori Morimoto snd_soc_component_get_dapm(component); 1805b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 1806a9ce96bcSJeeja KP int ret; 180718382eadSSubhransu S. Prusty 18083787a398SRakesh Ughreja hdmi->component = component; 180918382eadSSubhransu S. Prusty 1810b2047e99SVinod Koul /* 1811b2047e99SVinod Koul * hold the ref while we probe, also no need to drop the ref on 1812b2047e99SVinod Koul * exit, we call pm_runtime_suspend() so that will do for us 1813b2047e99SVinod Koul */ 181476f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 1815500e06b9SVinod Koul if (!hlink) { 18163787a398SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 1817500e06b9SVinod Koul return -EIO; 1818500e06b9SVinod Koul } 1819500e06b9SVinod Koul 182076f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 1821b2047e99SVinod Koul 182279f4e922SSubhransu S. Prusty ret = create_fill_widget_route_map(dapm); 182379f4e922SSubhransu S. Prusty if (ret < 0) 182479f4e922SSubhransu S. Prusty return ret; 182518382eadSSubhransu S. Prusty 18263787a398SRakesh Ughreja aops.audio_ptr = hdev; 1827a57942bfSTakashi Iwai ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops); 1828b8a54545SSubhransu S. Prusty if (ret < 0) { 18293787a398SRakesh Ughreja dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret); 1830b8a54545SSubhransu S. Prusty return ret; 1831b8a54545SSubhransu S. Prusty } 1832b8a54545SSubhransu S. Prusty 18333787a398SRakesh Ughreja hdac_hdmi_present_sense_all_pins(hdev, hdmi, true); 183418382eadSSubhransu S. Prusty /* Imp: Store the card pointer in hda_codec */ 18353787a398SRakesh Ughreja hdmi->card = dapm->card->snd_card; 183618382eadSSubhransu S. Prusty 1837e342ac08SSubhransu S. Prusty /* 1838e342ac08SSubhransu S. Prusty * hdac_device core already sets the state to active and calls 1839e342ac08SSubhransu S. Prusty * get_noresume. So enable runtime and set the device to suspend. 1840e342ac08SSubhransu S. Prusty */ 18413787a398SRakesh Ughreja pm_runtime_enable(&hdev->dev); 18423787a398SRakesh Ughreja pm_runtime_put(&hdev->dev); 18433787a398SRakesh Ughreja pm_runtime_suspend(&hdev->dev); 1844e342ac08SSubhransu S. Prusty 1845e342ac08SSubhransu S. Prusty return 0; 1846e342ac08SSubhransu S. Prusty } 1847e342ac08SSubhransu S. Prusty 184845101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component) 1849e342ac08SSubhransu S. Prusty { 18503787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 18513787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 1852e342ac08SSubhransu S. Prusty 18533787a398SRakesh Ughreja pm_runtime_disable(&hdev->dev); 185418382eadSSubhransu S. Prusty } 185518382eadSSubhransu S. Prusty 1856571d5078SJeeja KP #ifdef CONFIG_PM 18571b377ccdSSubhransu S. Prusty static int hdmi_codec_prepare(struct device *dev) 18581b377ccdSSubhransu S. Prusty { 18593787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 18601b377ccdSSubhransu S. Prusty 18613787a398SRakesh Ughreja pm_runtime_get_sync(&hdev->dev); 18621b377ccdSSubhransu S. Prusty 18631b377ccdSSubhransu S. Prusty /* 18641b377ccdSSubhransu S. Prusty * Power down afg. 18651b377ccdSSubhransu S. Prusty * codec_read is preferred over codec_write to set the power state. 18661b377ccdSSubhransu S. Prusty * This way verb is send to set the power state and response 18671b377ccdSSubhransu S. Prusty * is received. So setting power state is ensured without using loop 18681b377ccdSSubhransu S. Prusty * to read the state. 18691b377ccdSSubhransu S. Prusty */ 1870f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 18711b377ccdSSubhransu S. Prusty AC_PWRST_D3); 18721b377ccdSSubhransu S. Prusty 18731b377ccdSSubhransu S. Prusty return 0; 18741b377ccdSSubhransu S. Prusty } 18751b377ccdSSubhransu S. Prusty 18760fee1798SSubhransu S. Prusty static void hdmi_codec_complete(struct device *dev) 1877571d5078SJeeja KP { 18783787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 18793787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 18801b377ccdSSubhransu S. Prusty 18811b377ccdSSubhransu S. Prusty /* Power up afg */ 1882f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 18831b377ccdSSubhransu S. Prusty AC_PWRST_D0); 1884571d5078SJeeja KP 18853787a398SRakesh Ughreja hdac_hdmi_skl_enable_all_pins(hdev); 18863787a398SRakesh Ughreja hdac_hdmi_skl_enable_dp12(hdev); 1887571d5078SJeeja KP 1888571d5078SJeeja KP /* 1889571d5078SJeeja KP * As the ELD notify callback request is not entertained while the 1890571d5078SJeeja KP * device is in suspend state. Need to manually check detection of 1891a9ce96bcSJeeja KP * all pins here. pin capablity change is not support, so use the 1892a9ce96bcSJeeja KP * already set pin caps. 1893571d5078SJeeja KP */ 18943787a398SRakesh Ughreja hdac_hdmi_present_sense_all_pins(hdev, hdmi, false); 1895571d5078SJeeja KP 18963787a398SRakesh Ughreja pm_runtime_put_sync(&hdev->dev); 1897571d5078SJeeja KP } 1898571d5078SJeeja KP #else 18991b377ccdSSubhransu S. Prusty #define hdmi_codec_prepare NULL 19000fee1798SSubhransu S. Prusty #define hdmi_codec_complete NULL 1901571d5078SJeeja KP #endif 1902571d5078SJeeja KP 190345101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = { 190418382eadSSubhransu S. Prusty .probe = hdmi_codec_probe, 1905e342ac08SSubhransu S. Prusty .remove = hdmi_codec_remove, 190645101122SKuninori Morimoto .use_pmdown_time = 1, 190745101122SKuninori Morimoto .endianness = 1, 190845101122SKuninori Morimoto .non_legacy_dai_naming = 1, 190918382eadSSubhransu S. Prusty }; 191018382eadSSubhransu S. Prusty 1911f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx, 19122889099eSSubhransu S. Prusty unsigned char *chmap) 19132889099eSSubhransu S. Prusty { 1914f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 19152889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 19162889099eSSubhransu S. Prusty 1917ab1eea19SJeeja KP memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap)); 19182889099eSSubhransu S. Prusty } 19192889099eSSubhransu S. Prusty 1920f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx, 19212889099eSSubhransu S. Prusty unsigned char *chmap, int prepared) 19222889099eSSubhransu S. Prusty { 1923f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 19242889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 1925e0e5d3e5SJeeja KP struct hdac_hdmi_port *port; 1926e0e5d3e5SJeeja KP 1927eb50fa17SSubhransu S. Prusty if (!pcm) 1928eb50fa17SSubhransu S. Prusty return; 1929eb50fa17SSubhransu S. Prusty 1930e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 1931e0e5d3e5SJeeja KP return; 19322889099eSSubhransu S. Prusty 1933ab1eea19SJeeja KP mutex_lock(&pcm->lock); 1934ab1eea19SJeeja KP pcm->chmap_set = true; 1935ab1eea19SJeeja KP memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap)); 1936e0e5d3e5SJeeja KP list_for_each_entry(port, &pcm->port_list, head) 19372889099eSSubhransu S. Prusty if (prepared) 19383787a398SRakesh Ughreja hdac_hdmi_setup_audio_infoframe(hdev, pcm, port); 1939ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 19402889099eSSubhransu S. Prusty } 19412889099eSSubhransu S. Prusty 1942f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx) 19432889099eSSubhransu S. Prusty { 1944f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 19452889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 19462889099eSSubhransu S. Prusty 1947eb50fa17SSubhransu S. Prusty if (!pcm) 1948eb50fa17SSubhransu S. Prusty return false; 1949eb50fa17SSubhransu S. Prusty 1950e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 1951e0e5d3e5SJeeja KP return false; 1952e0e5d3e5SJeeja KP 1953e0e5d3e5SJeeja KP return true; 19542889099eSSubhransu S. Prusty } 19552889099eSSubhransu S. Prusty 1956f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx) 19572889099eSSubhransu S. Prusty { 1958f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 19592889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 1960e0e5d3e5SJeeja KP struct hdac_hdmi_port *port; 1961e0e5d3e5SJeeja KP 1962eb50fa17SSubhransu S. Prusty if (!pcm) 1963eb50fa17SSubhransu S. Prusty return 0; 1964eb50fa17SSubhransu S. Prusty 1965e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 1966e0e5d3e5SJeeja KP return 0; 1967e0e5d3e5SJeeja KP 1968e0e5d3e5SJeeja KP port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head); 1969e0e5d3e5SJeeja KP 1970754695f9SJeeja KP if (!port || !port->eld.eld_valid) 19712889099eSSubhransu S. Prusty return 0; 19722889099eSSubhransu S. Prusty 1973754695f9SJeeja KP return port->eld.info.spk_alloc; 19742889099eSSubhransu S. Prusty } 19752889099eSSubhransu S. Prusty 19765622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data = { 19775622bc95SPradeep Tewani .vendor_nid = INTEL_GLK_VENDOR_NID, 19785622bc95SPradeep Tewani }; 19795622bc95SPradeep Tewani 19805622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data = { 19815622bc95SPradeep Tewani .vendor_nid = INTEL_VENDOR_NID, 19825622bc95SPradeep Tewani }; 19835622bc95SPradeep Tewani 19843787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev) 198518382eadSSubhransu S. Prusty { 19863787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi_priv = NULL; 198717a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais = NULL; 1988b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 198917a42c45SSubhransu S. Prusty int num_dais = 0; 199018382eadSSubhransu S. Prusty int ret = 0; 1991f0c5ebebSUghreja, Rakesh A struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver); 1992f0c5ebebSUghreja, Rakesh A const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv); 199318382eadSSubhransu S. Prusty 1994b2047e99SVinod Koul /* hold the ref while we probe */ 199576f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 1996500e06b9SVinod Koul if (!hlink) { 19973787a398SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 1998500e06b9SVinod Koul return -EIO; 1999500e06b9SVinod Koul } 2000500e06b9SVinod Koul 200176f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 2002b2047e99SVinod Koul 2003f0c5ebebSUghreja, Rakesh A hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL); 200418382eadSSubhransu S. Prusty if (hdmi_priv == NULL) 200518382eadSSubhransu S. Prusty return -ENOMEM; 200618382eadSSubhransu S. Prusty 2007f0c5ebebSUghreja, Rakesh A snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap); 20082889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap; 20092889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap; 20102889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached; 20112889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc; 20123787a398SRakesh Ughreja hdmi_priv->hdev = hdev; 201318382eadSSubhransu S. Prusty 2014eb50fa17SSubhransu S. Prusty if (!hdac_id) 2015eb50fa17SSubhransu S. Prusty return -ENODEV; 2016eb50fa17SSubhransu S. Prusty 20175622bc95SPradeep Tewani if (hdac_id->driver_data) 20185622bc95SPradeep Tewani hdmi_priv->drv_data = 20195622bc95SPradeep Tewani (struct hdac_hdmi_drv_data *)hdac_id->driver_data; 20205622bc95SPradeep Tewani else 20215622bc95SPradeep Tewani hdmi_priv->drv_data = &intel_drv_data; 20225622bc95SPradeep Tewani 20233787a398SRakesh Ughreja dev_set_drvdata(&hdev->dev, hdmi_priv); 202418382eadSSubhransu S. Prusty 202515b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->pin_list); 202615b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->cvt_list); 20274a3478deSJeeja KP INIT_LIST_HEAD(&hdmi_priv->pcm_list); 20284a3478deSJeeja KP mutex_init(&hdmi_priv->pin_mutex); 202915b91447SSubhransu S. Prusty 2030aeaccef0SRamesh Babu /* 2031aeaccef0SRamesh Babu * Turned off in the runtime_suspend during the first explicit 2032aeaccef0SRamesh Babu * pm_runtime_suspend call. 2033aeaccef0SRamesh Babu */ 20343787a398SRakesh Ughreja ret = snd_hdac_display_power(hdev->bus, true); 2035aeaccef0SRamesh Babu if (ret < 0) { 20363787a398SRakesh Ughreja dev_err(&hdev->dev, 2037aeaccef0SRamesh Babu "Cannot turn on display power on i915 err: %d\n", 2038aeaccef0SRamesh Babu ret); 2039aeaccef0SRamesh Babu return ret; 2040aeaccef0SRamesh Babu } 2041aeaccef0SRamesh Babu 20423787a398SRakesh Ughreja ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais); 204317a42c45SSubhransu S. Prusty if (ret < 0) { 2044f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, 204517a42c45SSubhransu S. Prusty "Failed in parse and map nid with err: %d\n", ret); 204618382eadSSubhransu S. Prusty return ret; 204717a42c45SSubhransu S. Prusty } 20480fb02ba3SPuneeth Prabhu snd_hdac_refresh_widgets(hdev, true); 204918382eadSSubhransu S. Prusty 205018382eadSSubhransu S. Prusty /* ASoC specific initialization */ 205145101122SKuninori Morimoto ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec, 205217a42c45SSubhransu S. Prusty hdmi_dais, num_dais); 2053b2047e99SVinod Koul 205476f56faeSRakesh Ughreja snd_hdac_ext_bus_link_put(hdev->bus, hlink); 2055b2047e99SVinod Koul 2056b2047e99SVinod Koul return ret; 205718382eadSSubhransu S. Prusty } 205818382eadSSubhransu S. Prusty 20593787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev) 206018382eadSSubhransu S. Prusty { 20613787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 206215b91447SSubhransu S. Prusty struct hdac_hdmi_pin *pin, *pin_next; 206315b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt, *cvt_next; 20644a3478deSJeeja KP struct hdac_hdmi_pcm *pcm, *pcm_next; 20652fe42dd0SJeeja KP struct hdac_hdmi_port *port, *port_next; 2066754695f9SJeeja KP int i; 206715b91447SSubhransu S. Prusty 20684a3478deSJeeja KP list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) { 20694a3478deSJeeja KP pcm->cvt = NULL; 2070e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 2071e0e5d3e5SJeeja KP continue; 2072e0e5d3e5SJeeja KP 20732fe42dd0SJeeja KP list_for_each_entry_safe(port, port_next, 20742fe42dd0SJeeja KP &pcm->port_list, head) 20752fe42dd0SJeeja KP list_del(&port->head); 2076e0e5d3e5SJeeja KP 20774a3478deSJeeja KP list_del(&pcm->head); 20784a3478deSJeeja KP kfree(pcm); 20794a3478deSJeeja KP } 20804a3478deSJeeja KP 208115b91447SSubhransu S. Prusty list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) { 208215b91447SSubhransu S. Prusty list_del(&cvt->head); 20834a3478deSJeeja KP kfree(cvt->name); 208415b91447SSubhransu S. Prusty kfree(cvt); 208515b91447SSubhransu S. Prusty } 208615b91447SSubhransu S. Prusty 208715b91447SSubhransu S. Prusty list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) { 2088754695f9SJeeja KP for (i = 0; i < pin->num_ports; i++) 2089754695f9SJeeja KP pin->ports[i].pin = NULL; 2090754695f9SJeeja KP kfree(pin->ports); 209115b91447SSubhransu S. Prusty list_del(&pin->head); 209215b91447SSubhransu S. Prusty kfree(pin); 209315b91447SSubhransu S. Prusty } 209415b91447SSubhransu S. Prusty 209518382eadSSubhransu S. Prusty return 0; 209618382eadSSubhransu S. Prusty } 209718382eadSSubhransu S. Prusty 2098e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM 2099bf270262SSriram Periyasamy /* 2100bf270262SSriram Periyasamy * Power management sequences 2101bf270262SSriram Periyasamy * ========================== 2102bf270262SSriram Periyasamy * 2103bf270262SSriram Periyasamy * The following explains the PM handling of HDAC HDMI with its parent 2104bf270262SSriram Periyasamy * device SKL and display power usage 2105bf270262SSriram Periyasamy * 2106bf270262SSriram Periyasamy * Probe 2107bf270262SSriram Periyasamy * ----- 2108bf270262SSriram Periyasamy * In SKL probe, 2109bf270262SSriram Periyasamy * 1. skl_probe_work() powers up the display (refcount++ -> 1) 2110bf270262SSriram Periyasamy * 2. enumerates the codecs on the link 2111bf270262SSriram Periyasamy * 3. powers down the display (refcount-- -> 0) 2112bf270262SSriram Periyasamy * 2113bf270262SSriram Periyasamy * In HDAC HDMI probe, 2114bf270262SSriram Periyasamy * 1. hdac_hdmi_dev_probe() powers up the display (refcount++ -> 1) 2115bf270262SSriram Periyasamy * 2. probe the codec 2116bf270262SSriram Periyasamy * 3. put the HDAC HDMI device to runtime suspend 2117bf270262SSriram Periyasamy * 4. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0) 2118bf270262SSriram Periyasamy * 2119bf270262SSriram Periyasamy * Once children are runtime suspended, SKL device also goes to runtime 2120bf270262SSriram Periyasamy * suspend 2121bf270262SSriram Periyasamy * 2122bf270262SSriram Periyasamy * HDMI Playback 2123bf270262SSriram Periyasamy * ------------- 2124bf270262SSriram Periyasamy * Open HDMI device, 2125bf270262SSriram Periyasamy * 1. skl_runtime_resume() invoked 2126bf270262SSriram Periyasamy * 2. hdac_hdmi_runtime_resume() powers up the display (refcount++ -> 1) 2127bf270262SSriram Periyasamy * 2128bf270262SSriram Periyasamy * Close HDMI device, 2129bf270262SSriram Periyasamy * 1. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0) 2130bf270262SSriram Periyasamy * 2. skl_runtime_suspend() invoked 2131bf270262SSriram Periyasamy * 2132bf270262SSriram Periyasamy * S0/S3 Cycle with playback in progress 2133bf270262SSriram Periyasamy * ------------------------------------- 2134bf270262SSriram Periyasamy * When the device is opened for playback, the device is runtime active 2135bf270262SSriram Periyasamy * already and the display refcount is 1 as explained above. 2136bf270262SSriram Periyasamy * 2137bf270262SSriram Periyasamy * Entering to S3, 2138bf270262SSriram Periyasamy * 1. hdmi_codec_prepare() invoke the runtime resume of codec which just 2139bf270262SSriram Periyasamy * increments the PM runtime usage count of the codec since the device 2140bf270262SSriram Periyasamy * is in use already 2141bf270262SSriram Periyasamy * 2. skl_suspend() powers down the display (refcount-- -> 0) 2142bf270262SSriram Periyasamy * 2143bf270262SSriram Periyasamy * Wakeup from S3, 2144bf270262SSriram Periyasamy * 1. skl_resume() powers up the display (refcount++ -> 1) 2145bf270262SSriram Periyasamy * 2. hdmi_codec_complete() invokes the runtime suspend of codec which just 2146bf270262SSriram Periyasamy * decrements the PM runtime usage count of the codec since the device 2147bf270262SSriram Periyasamy * is in use already 2148bf270262SSriram Periyasamy * 2149bf270262SSriram Periyasamy * Once playback is stopped, the display refcount is set to 0 as explained 2150bf270262SSriram Periyasamy * above in the HDMI playback sequence. The PM handlings are designed in 2151bf270262SSriram Periyasamy * such way that to balance the refcount of display power when the codec 2152bf270262SSriram Periyasamy * device put to S3 while playback is going on. 2153bf270262SSriram Periyasamy * 2154bf270262SSriram Periyasamy * S0/S3 Cycle without playback in progress 2155bf270262SSriram Periyasamy * ---------------------------------------- 2156bf270262SSriram Periyasamy * Entering to S3, 2157bf270262SSriram Periyasamy * 1. hdmi_codec_prepare() invoke the runtime resume of codec 2158bf270262SSriram Periyasamy * 2. skl_runtime_resume() invoked 2159bf270262SSriram Periyasamy * 3. hdac_hdmi_runtime_resume() powers up the display (refcount++ -> 1) 2160bf270262SSriram Periyasamy * 4. skl_suspend() powers down the display (refcount-- -> 0) 2161bf270262SSriram Periyasamy * 2162bf270262SSriram Periyasamy * Wakeup from S3, 2163bf270262SSriram Periyasamy * 1. skl_resume() powers up the display (refcount++ -> 1) 2164bf270262SSriram Periyasamy * 2. hdmi_codec_complete() invokes the runtime suspend of codec 2165bf270262SSriram Periyasamy * 3. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0) 2166bf270262SSriram Periyasamy * 4. skl_runtime_suspend() invoked 2167bf270262SSriram Periyasamy */ 2168e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev) 2169e342ac08SSubhransu S. Prusty { 21703787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 2171f0c5ebebSUghreja, Rakesh A struct hdac_bus *bus = hdev->bus; 2172b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 217307f083abSSubhransu S. Prusty int err; 2174e342ac08SSubhransu S. Prusty 2175e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 2176e342ac08SSubhransu S. Prusty 217707f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 217807f083abSSubhransu S. Prusty if (!bus) 217907f083abSSubhransu S. Prusty return 0; 218007f083abSSubhransu S. Prusty 21811b377ccdSSubhransu S. Prusty /* 21821b377ccdSSubhransu S. Prusty * Power down afg. 21831b377ccdSSubhransu S. Prusty * codec_read is preferred over codec_write to set the power state. 21841b377ccdSSubhransu S. Prusty * This way verb is send to set the power state and response 21851b377ccdSSubhransu S. Prusty * is received. So setting power state is ensured without using loop 21861b377ccdSSubhransu S. Prusty * to read the state. 21871b377ccdSSubhransu S. Prusty */ 2188f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 21891b377ccdSSubhransu S. Prusty AC_PWRST_D3); 219007f083abSSubhransu S. Prusty 219176f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev)); 2192500e06b9SVinod Koul if (!hlink) { 2193500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 2194500e06b9SVinod Koul return -EIO; 2195500e06b9SVinod Koul } 2196500e06b9SVinod Koul 219776f56faeSRakesh Ughreja snd_hdac_ext_bus_link_put(bus, hlink); 2198b2047e99SVinod Koul 2199*4c10473dSPierre-Louis Bossart err = snd_hdac_display_power(bus, false); 2200*4c10473dSPierre-Louis Bossart if (err < 0) 2201*4c10473dSPierre-Louis Bossart dev_err(dev, "Cannot turn off display power on i915\n"); 2202*4c10473dSPierre-Louis Bossart 2203*4c10473dSPierre-Louis Bossart return err; 2204e342ac08SSubhransu S. Prusty } 2205e342ac08SSubhransu S. Prusty 2206e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev) 2207e342ac08SSubhransu S. Prusty { 22083787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 2209f0c5ebebSUghreja, Rakesh A struct hdac_bus *bus = hdev->bus; 2210b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 221107f083abSSubhransu S. Prusty int err; 2212e342ac08SSubhransu S. Prusty 2213e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 2214e342ac08SSubhransu S. Prusty 221507f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 221607f083abSSubhransu S. Prusty if (!bus) 221707f083abSSubhransu S. Prusty return 0; 221807f083abSSubhransu S. Prusty 221976f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev)); 2220500e06b9SVinod Koul if (!hlink) { 2221500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 2222500e06b9SVinod Koul return -EIO; 2223500e06b9SVinod Koul } 2224500e06b9SVinod Koul 222576f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(bus, hlink); 2226b2047e99SVinod Koul 222707f083abSSubhransu S. Prusty err = snd_hdac_display_power(bus, true); 222807f083abSSubhransu S. Prusty if (err < 0) { 22293787a398SRakesh Ughreja dev_err(dev, "Cannot turn on display power on i915\n"); 223007f083abSSubhransu S. Prusty return err; 223107f083abSSubhransu S. Prusty } 223207f083abSSubhransu S. Prusty 22333787a398SRakesh Ughreja hdac_hdmi_skl_enable_all_pins(hdev); 22343787a398SRakesh Ughreja hdac_hdmi_skl_enable_dp12(hdev); 2235ab85f5b3SSubhransu S. Prusty 2236e342ac08SSubhransu S. Prusty /* Power up afg */ 2237f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 22381b377ccdSSubhransu S. Prusty AC_PWRST_D0); 2239e342ac08SSubhransu S. Prusty 2240e342ac08SSubhransu S. Prusty return 0; 2241e342ac08SSubhransu S. Prusty } 2242e342ac08SSubhransu S. Prusty #else 2243e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL 2244e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL 2245e342ac08SSubhransu S. Prusty #endif 2246e342ac08SSubhransu S. Prusty 2247e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = { 2248e342ac08SSubhransu S. Prusty SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL) 22491b377ccdSSubhransu S. Prusty .prepare = hdmi_codec_prepare, 22500fee1798SSubhransu S. Prusty .complete = hdmi_codec_complete, 2251e342ac08SSubhransu S. Prusty }; 2252e342ac08SSubhransu S. Prusty 225318382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = { 225418382eadSSubhransu S. Prusty HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0), 2255e2304803SJeeja KP HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0), 2256cc216887SShreyas NC HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0), 22575fb6e0a1SGuneshwor Singh HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI", 22585fb6e0a1SGuneshwor Singh &intel_glk_drv_data), 22595622bc95SPradeep Tewani HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI", 22605622bc95SPradeep Tewani &intel_glk_drv_data), 226118382eadSSubhransu S. Prusty {} 226218382eadSSubhransu S. Prusty }; 226318382eadSSubhransu S. Prusty 226418382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list); 226518382eadSSubhransu S. Prusty 2266e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = { 226718382eadSSubhransu S. Prusty .driver = { 226818382eadSSubhransu S. Prusty .name = "HDMI HDA Codec", 2269e342ac08SSubhransu S. Prusty .pm = &hdac_hdmi_pm, 227018382eadSSubhransu S. Prusty }, 227118382eadSSubhransu S. Prusty .id_table = hdmi_list, 227218382eadSSubhransu S. Prusty .probe = hdac_hdmi_dev_probe, 227318382eadSSubhransu S. Prusty .remove = hdac_hdmi_dev_remove, 227418382eadSSubhransu S. Prusty }; 227518382eadSSubhransu S. Prusty 227618382eadSSubhransu S. Prusty static int __init hdmi_init(void) 227718382eadSSubhransu S. Prusty { 227818382eadSSubhransu S. Prusty return snd_hda_ext_driver_register(&hdmi_driver); 227918382eadSSubhransu S. Prusty } 228018382eadSSubhransu S. Prusty 228118382eadSSubhransu S. Prusty static void __exit hdmi_exit(void) 228218382eadSSubhransu S. Prusty { 228318382eadSSubhransu S. Prusty snd_hda_ext_driver_unregister(&hdmi_driver); 228418382eadSSubhransu S. Prusty } 228518382eadSSubhransu S. Prusty 228618382eadSSubhransu S. Prusty module_init(hdmi_init); 228718382eadSSubhransu S. Prusty module_exit(hdmi_exit); 228818382eadSSubhransu S. Prusty 228918382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2"); 229018382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec"); 229118382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>"); 229218382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>"); 2293