18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 218382eadSSubhransu S. Prusty /* 318382eadSSubhransu S. Prusty * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms 418382eadSSubhransu S. Prusty * 518382eadSSubhransu S. Prusty * Copyright (C) 2014-2015 Intel Corp 618382eadSSubhransu S. Prusty * Author: Samreen Nilofer <samreen.nilofer@intel.com> 718382eadSSubhransu S. Prusty * Subhransu S. Prusty <subhransu.s.prusty@intel.com> 818382eadSSubhransu S. Prusty * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 918382eadSSubhransu S. Prusty * 1018382eadSSubhransu S. Prusty * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1118382eadSSubhransu S. Prusty */ 12*1f53bcb3SBrent Lu 1318382eadSSubhransu S. Prusty #include <linux/init.h> 1418382eadSSubhransu S. Prusty #include <linux/delay.h> 1518382eadSSubhransu S. Prusty #include <linux/module.h> 1618382eadSSubhransu S. Prusty #include <linux/pm_runtime.h> 17a657f1d0SSubhransu S. Prusty #include <linux/hdmi.h> 182428bca3SSubhransu S. Prusty #include <drm/drm_edid.h> 1918382eadSSubhransu S. Prusty #include <sound/pcm_params.h> 204a3478deSJeeja KP #include <sound/jack.h> 2118382eadSSubhransu S. Prusty #include <sound/soc.h> 2218382eadSSubhransu S. Prusty #include <sound/hdaudio_ext.h> 2307f083abSSubhransu S. Prusty #include <sound/hda_i915.h> 242428bca3SSubhransu S. Prusty #include <sound/pcm_drm_eld.h> 25bcced704SSubhransu S. Prusty #include <sound/hda_chmap.h> 2618382eadSSubhransu S. Prusty #include "../../hda/local.h" 274a3478deSJeeja KP #include "hdac_hdmi.h" 2818382eadSSubhransu S. Prusty 2917a42c45SSubhransu S. Prusty #define NAME_SIZE 32 3017a42c45SSubhransu S. Prusty 31b0362adbSSubhransu S. Prusty #define AMP_OUT_MUTE 0xb080 32b0362adbSSubhransu S. Prusty #define AMP_OUT_UNMUTE 0xb000 3318382eadSSubhransu S. Prusty #define PIN_OUT (AC_PINCTL_OUT_EN) 34b0362adbSSubhransu S. Prusty 3518382eadSSubhransu S. Prusty #define HDA_MAX_CONNECTIONS 32 3618382eadSSubhransu S. Prusty 37148569fdSSubhransu S. Prusty #define HDA_MAX_CVTS 3 38754695f9SJeeja KP #define HDA_MAX_PORTS 3 39148569fdSSubhransu S. Prusty 40b8a54545SSubhransu S. Prusty #define ELD_MAX_SIZE 256 41b8a54545SSubhransu S. Prusty #define ELD_FIXED_BYTES 20 42b8a54545SSubhransu S. Prusty 43f6fa11a3SSandeep Tayal #define ELD_VER_CEA_861D 2 44f6fa11a3SSandeep Tayal #define ELD_VER_PARTIAL 31 45f6fa11a3SSandeep Tayal #define ELD_MAX_MNL 16 46f6fa11a3SSandeep Tayal 4718382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params { 4818382eadSSubhransu S. Prusty unsigned int channels_min; 4918382eadSSubhransu S. Prusty unsigned int channels_max; 5018382eadSSubhransu S. Prusty u32 rates; 5118382eadSSubhransu S. Prusty u64 formats; 5218382eadSSubhransu S. Prusty unsigned int maxbps; 5318382eadSSubhransu S. Prusty }; 5418382eadSSubhransu S. Prusty 5518382eadSSubhransu S. Prusty struct hdac_hdmi_cvt { 5615b91447SSubhransu S. Prusty struct list_head head; 5718382eadSSubhransu S. Prusty hda_nid_t nid; 584a3478deSJeeja KP const char *name; 5918382eadSSubhransu S. Prusty struct hdac_hdmi_cvt_params params; 6018382eadSSubhransu S. Prusty }; 6118382eadSSubhransu S. Prusty 62b7756edeSSubhransu S. Prusty /* Currently only spk_alloc, more to be added */ 63b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld { 64b7756edeSSubhransu S. Prusty u8 spk_alloc; 65b7756edeSSubhransu S. Prusty }; 66b7756edeSSubhransu S. Prusty 67b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld { 68b8a54545SSubhransu S. Prusty bool monitor_present; 69b8a54545SSubhransu S. Prusty bool eld_valid; 70b8a54545SSubhransu S. Prusty int eld_size; 71b8a54545SSubhransu S. Prusty char eld_buffer[ELD_MAX_SIZE]; 72b7756edeSSubhransu S. Prusty struct hdac_hdmi_parsed_eld info; 73b8a54545SSubhransu S. Prusty }; 74b8a54545SSubhransu S. Prusty 7518382eadSSubhransu S. Prusty struct hdac_hdmi_pin { 7615b91447SSubhransu S. Prusty struct list_head head; 7718382eadSSubhransu S. Prusty hda_nid_t nid; 782acd8309SJeeja KP bool mst_capable; 79754695f9SJeeja KP struct hdac_hdmi_port *ports; 80754695f9SJeeja KP int num_ports; 813787a398SRakesh Ughreja struct hdac_device *hdev; 82754695f9SJeeja KP }; 83754695f9SJeeja KP 84754695f9SJeeja KP struct hdac_hdmi_port { 85e0e5d3e5SJeeja KP struct list_head head; 86754695f9SJeeja KP int id; 87754695f9SJeeja KP struct hdac_hdmi_pin *pin; 8818382eadSSubhransu S. Prusty int num_mux_nids; 8918382eadSSubhransu S. Prusty hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; 90b8a54545SSubhransu S. Prusty struct hdac_hdmi_eld eld; 910324e51bSJeeja KP const char *jack_pin; 92332ccf00STakashi Iwai bool is_connect; 930324e51bSJeeja KP struct snd_soc_dapm_context *dapm; 940324e51bSJeeja KP const char *output_pin; 95332ccf00STakashi Iwai struct work_struct dapm_work; 9618382eadSSubhransu S. Prusty }; 9718382eadSSubhransu S. Prusty 984a3478deSJeeja KP struct hdac_hdmi_pcm { 994a3478deSJeeja KP struct list_head head; 1004a3478deSJeeja KP int pcm_id; 101e0e5d3e5SJeeja KP struct list_head port_list; 1024a3478deSJeeja KP struct hdac_hdmi_cvt *cvt; 10362490016SJeeja KP struct snd_soc_jack *jack; 104c9bfb5d7SJeeja KP int stream_tag; 105c9bfb5d7SJeeja KP int channels; 106c9bfb5d7SJeeja KP int format; 107ab1eea19SJeeja KP bool chmap_set; 108ab1eea19SJeeja KP unsigned char chmap[8]; /* ALSA API channel-map */ 109ab1eea19SJeeja KP struct mutex lock; 110e0e5d3e5SJeeja KP int jack_event; 111*1f53bcb3SBrent Lu struct snd_kcontrol *eld_ctl; 1124a3478deSJeeja KP }; 1134a3478deSJeeja KP 114754695f9SJeeja KP struct hdac_hdmi_dai_port_map { 11518382eadSSubhransu S. Prusty int dai_id; 116754695f9SJeeja KP struct hdac_hdmi_port *port; 11715b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 11818382eadSSubhransu S. Prusty }; 11918382eadSSubhransu S. Prusty 1205622bc95SPradeep Tewani struct hdac_hdmi_drv_data { 1215622bc95SPradeep Tewani unsigned int vendor_nid; 1225622bc95SPradeep Tewani }; 1235622bc95SPradeep Tewani 12418382eadSSubhransu S. Prusty struct hdac_hdmi_priv { 1253787a398SRakesh Ughreja struct hdac_device *hdev; 1263787a398SRakesh Ughreja struct snd_soc_component *component; 1273787a398SRakesh Ughreja struct snd_card *card; 128754695f9SJeeja KP struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS]; 12915b91447SSubhransu S. Prusty struct list_head pin_list; 13015b91447SSubhransu S. Prusty struct list_head cvt_list; 1314a3478deSJeeja KP struct list_head pcm_list; 13215b91447SSubhransu S. Prusty int num_pin; 13315b91447SSubhransu S. Prusty int num_cvt; 134754695f9SJeeja KP int num_ports; 1354a3478deSJeeja KP struct mutex pin_mutex; 136bcced704SSubhransu S. Prusty struct hdac_chmap chmap; 1375622bc95SPradeep Tewani struct hdac_hdmi_drv_data *drv_data; 1381e02dac3SKuninori Morimoto struct snd_soc_dai_driver *dai_drv; 13918382eadSSubhransu S. Prusty }; 14018382eadSSubhransu S. Prusty 1413787a398SRakesh Ughreja #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev) 142b09b1c3bSUghreja, Rakesh A 143c9bfb5d7SJeeja KP static struct hdac_hdmi_pcm * 144c9bfb5d7SJeeja KP hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi, 145c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt) 146c9bfb5d7SJeeja KP { 147326b5092SAmadeusz Sławiński struct hdac_hdmi_pcm *pcm; 1481de777feSJeeja KP 149c9bfb5d7SJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 150c9bfb5d7SJeeja KP if (pcm->cvt == cvt) 151326b5092SAmadeusz Sławiński return pcm; 152c9bfb5d7SJeeja KP } 153c9bfb5d7SJeeja KP 154326b5092SAmadeusz Sławiński return NULL; 155c9bfb5d7SJeeja KP } 1561de777feSJeeja KP 157e0e5d3e5SJeeja KP static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm, 158e0e5d3e5SJeeja KP struct hdac_hdmi_port *port, bool is_connect) 159e0e5d3e5SJeeja KP { 1603787a398SRakesh Ughreja struct hdac_device *hdev = port->pin->hdev; 161e0e5d3e5SJeeja KP 162332ccf00STakashi Iwai port->is_connect = is_connect; 163e0e5d3e5SJeeja KP if (is_connect) { 164e0e5d3e5SJeeja KP /* 165e0e5d3e5SJeeja KP * Report Jack connect event when a device is connected 166e0e5d3e5SJeeja KP * for the first time where same PCM is attached to multiple 167e0e5d3e5SJeeja KP * ports. 168e0e5d3e5SJeeja KP */ 169e0e5d3e5SJeeja KP if (pcm->jack_event == 0) { 1703787a398SRakesh Ughreja dev_dbg(&hdev->dev, 171e0e5d3e5SJeeja KP "jack report for pcm=%d\n", 172e0e5d3e5SJeeja KP pcm->pcm_id); 17362490016SJeeja KP snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT, 17462490016SJeeja KP SND_JACK_AVOUT); 175e0e5d3e5SJeeja KP } 176e0e5d3e5SJeeja KP pcm->jack_event++; 177e0e5d3e5SJeeja KP } else { 178e0e5d3e5SJeeja KP /* 179e0e5d3e5SJeeja KP * Report Jack disconnect event when a device is disconnected 180e0e5d3e5SJeeja KP * is the only last connected device when same PCM is attached 181e0e5d3e5SJeeja KP * to multiple ports. 182e0e5d3e5SJeeja KP */ 183e0e5d3e5SJeeja KP if (pcm->jack_event == 1) 18462490016SJeeja KP snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT); 185e0e5d3e5SJeeja KP if (pcm->jack_event > 0) 186e0e5d3e5SJeeja KP pcm->jack_event--; 187e0e5d3e5SJeeja KP } 188332ccf00STakashi Iwai } 1890324e51bSJeeja KP 190332ccf00STakashi Iwai static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port) 191332ccf00STakashi Iwai { 192332ccf00STakashi Iwai if (port->is_connect) 193332ccf00STakashi Iwai snd_soc_dapm_enable_pin(port->dapm, port->jack_pin); 194332ccf00STakashi Iwai else 195332ccf00STakashi Iwai snd_soc_dapm_disable_pin(port->dapm, port->jack_pin); 1960324e51bSJeeja KP snd_soc_dapm_sync(port->dapm); 197e0e5d3e5SJeeja KP } 198e0e5d3e5SJeeja KP 199332ccf00STakashi Iwai static void hdac_hdmi_jack_dapm_work(struct work_struct *work) 200332ccf00STakashi Iwai { 201332ccf00STakashi Iwai struct hdac_hdmi_port *port; 202332ccf00STakashi Iwai 203332ccf00STakashi Iwai port = container_of(work, struct hdac_hdmi_port, dapm_work); 204332ccf00STakashi Iwai hdac_hdmi_port_dapm_update(port); 205332ccf00STakashi Iwai } 206332ccf00STakashi Iwai 207332ccf00STakashi Iwai static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm, 208332ccf00STakashi Iwai struct hdac_hdmi_port *port, bool is_connect) 209332ccf00STakashi Iwai { 210332ccf00STakashi Iwai hdac_hdmi_jack_report(pcm, port, is_connect); 211332ccf00STakashi Iwai hdac_hdmi_port_dapm_update(port); 212332ccf00STakashi Iwai } 213332ccf00STakashi Iwai 214fc181b04SJeeja KP /* MST supported verbs */ 215fc181b04SJeeja KP /* 216fc181b04SJeeja KP * Get the no devices that can be connected to a port on the Pin widget. 217fc181b04SJeeja KP */ 2183787a398SRakesh Ughreja static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid) 219fc181b04SJeeja KP { 220fc181b04SJeeja KP unsigned int caps; 221fc181b04SJeeja KP unsigned int type, param; 222fc181b04SJeeja KP 2233787a398SRakesh Ughreja caps = get_wcaps(hdev, nid); 224fc181b04SJeeja KP type = get_wcaps_type(caps); 225fc181b04SJeeja KP 226fc181b04SJeeja KP if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN)) 227fc181b04SJeeja KP return 0; 228fc181b04SJeeja KP 2293787a398SRakesh Ughreja param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN); 230fc181b04SJeeja KP if (param == -1) 231fc181b04SJeeja KP return param; 232fc181b04SJeeja KP 233fc181b04SJeeja KP return param & AC_DEV_LIST_LEN_MASK; 234fc181b04SJeeja KP } 235fc181b04SJeeja KP 236fc181b04SJeeja KP /* 237fc181b04SJeeja KP * Get the port entry select on the pin. Return the port entry 238fc181b04SJeeja KP * id selected on the pin. Return 0 means the first port entry 239fc181b04SJeeja KP * is selected or MST is not supported. 240fc181b04SJeeja KP */ 2413787a398SRakesh Ughreja static int hdac_hdmi_port_select_get(struct hdac_device *hdev, 242fc181b04SJeeja KP struct hdac_hdmi_port *port) 243fc181b04SJeeja KP { 2443787a398SRakesh Ughreja return snd_hdac_codec_read(hdev, port->pin->nid, 245fc181b04SJeeja KP 0, AC_VERB_GET_DEVICE_SEL, 0); 246fc181b04SJeeja KP } 247fc181b04SJeeja KP 248fc181b04SJeeja KP /* 249fc181b04SJeeja KP * Sets the selected port entry for the configuring Pin widget verb. 250fc181b04SJeeja KP * returns error if port set is not equal to port get otherwise success 251fc181b04SJeeja KP */ 2523787a398SRakesh Ughreja static int hdac_hdmi_port_select_set(struct hdac_device *hdev, 253fc181b04SJeeja KP struct hdac_hdmi_port *port) 254fc181b04SJeeja KP { 255fc181b04SJeeja KP int num_ports; 256fc181b04SJeeja KP 257fc181b04SJeeja KP if (!port->pin->mst_capable) 258fc181b04SJeeja KP return 0; 259fc181b04SJeeja KP 260fc181b04SJeeja KP /* AC_PAR_DEVLIST_LEN is 0 based. */ 2613787a398SRakesh Ughreja num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid); 262fc181b04SJeeja KP if (num_ports < 0) 263fc181b04SJeeja KP return -EIO; 264fc181b04SJeeja KP /* 265fc181b04SJeeja KP * Device List Length is a 0 based integer value indicating the 266fc181b04SJeeja KP * number of sink device that a MST Pin Widget can support. 267fc181b04SJeeja KP */ 268fc181b04SJeeja KP if (num_ports + 1 < port->id) 269fc181b04SJeeja KP return 0; 270fc181b04SJeeja KP 2713787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 272fc181b04SJeeja KP AC_VERB_SET_DEVICE_SEL, port->id); 273fc181b04SJeeja KP 2743787a398SRakesh Ughreja if (port->id != hdac_hdmi_port_select_get(hdev, port)) 275fc181b04SJeeja KP return -EIO; 276fc181b04SJeeja KP 2773787a398SRakesh Ughreja dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id); 278fc181b04SJeeja KP 279fc181b04SJeeja KP return 0; 280fc181b04SJeeja KP } 281fc181b04SJeeja KP 2822889099eSSubhransu S. Prusty static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi, 2832889099eSSubhransu S. Prusty int pcm_idx) 2842889099eSSubhransu S. Prusty { 2852889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 2862889099eSSubhransu S. Prusty 2872889099eSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 2882889099eSSubhransu S. Prusty if (pcm->pcm_id == pcm_idx) 2892889099eSSubhransu S. Prusty return pcm; 2902889099eSSubhransu S. Prusty } 2912889099eSSubhransu S. Prusty 2922889099eSSubhransu S. Prusty return NULL; 2932889099eSSubhransu S. Prusty } 2942889099eSSubhransu S. Prusty 2952428bca3SSubhransu S. Prusty static unsigned int sad_format(const u8 *sad) 2962428bca3SSubhransu S. Prusty { 2972428bca3SSubhransu S. Prusty return ((sad[0] >> 0x3) & 0x1f); 2982428bca3SSubhransu S. Prusty } 2992428bca3SSubhransu S. Prusty 3002428bca3SSubhransu S. Prusty static unsigned int sad_sample_bits_lpcm(const u8 *sad) 3012428bca3SSubhransu S. Prusty { 3022428bca3SSubhransu S. Prusty return (sad[2] & 7); 3032428bca3SSubhransu S. Prusty } 3042428bca3SSubhransu S. Prusty 3052428bca3SSubhransu S. Prusty static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime, 3062428bca3SSubhransu S. Prusty void *eld) 3072428bca3SSubhransu S. Prusty { 3082428bca3SSubhransu S. Prusty u64 formats = SNDRV_PCM_FMTBIT_S16; 3092428bca3SSubhransu S. Prusty int i; 3102428bca3SSubhransu S. Prusty const u8 *sad, *eld_buf = eld; 3112428bca3SSubhransu S. Prusty 3122428bca3SSubhransu S. Prusty sad = drm_eld_sad(eld_buf); 3132428bca3SSubhransu S. Prusty if (!sad) 3142428bca3SSubhransu S. Prusty goto format_constraint; 3152428bca3SSubhransu S. Prusty 3162428bca3SSubhransu S. Prusty for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) { 3172428bca3SSubhransu S. Prusty if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */ 3182428bca3SSubhransu S. Prusty 3192428bca3SSubhransu S. Prusty /* 3202428bca3SSubhransu S. Prusty * the controller support 20 and 24 bits in 32 bit 3212428bca3SSubhransu S. Prusty * container so we set S32 3222428bca3SSubhransu S. Prusty */ 3232428bca3SSubhransu S. Prusty if (sad_sample_bits_lpcm(sad) & 0x6) 3242428bca3SSubhransu S. Prusty formats |= SNDRV_PCM_FMTBIT_S32; 3252428bca3SSubhransu S. Prusty } 3262428bca3SSubhransu S. Prusty } 3272428bca3SSubhransu S. Prusty 3282428bca3SSubhransu S. Prusty format_constraint: 3292428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, 3302428bca3SSubhransu S. Prusty formats); 3312428bca3SSubhransu S. Prusty 3322428bca3SSubhransu S. Prusty } 3332428bca3SSubhransu S. Prusty 334a657f1d0SSubhransu S. Prusty static void 3353787a398SRakesh Ughreja hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid, 336a657f1d0SSubhransu S. Prusty int packet_index, int byte_index) 337a657f1d0SSubhransu S. Prusty { 338a657f1d0SSubhransu S. Prusty int val; 339a657f1d0SSubhransu S. Prusty 340a657f1d0SSubhransu S. Prusty val = (packet_index << 5) | (byte_index & 0x1f); 3413787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val); 342a657f1d0SSubhransu S. Prusty } 343a657f1d0SSubhransu S. Prusty 344478f544eSSubhransu S. Prusty struct dp_audio_infoframe { 345478f544eSSubhransu S. Prusty u8 type; /* 0x84 */ 346478f544eSSubhransu S. Prusty u8 len; /* 0x1b */ 347478f544eSSubhransu S. Prusty u8 ver; /* 0x11 << 2 */ 348478f544eSSubhransu S. Prusty 349478f544eSSubhransu S. Prusty u8 CC02_CT47; /* match with HDMI infoframe from this on */ 350478f544eSSubhransu S. Prusty u8 SS01_SF24; 351478f544eSSubhransu S. Prusty u8 CXT04; 352478f544eSSubhransu S. Prusty u8 CA; 353478f544eSSubhransu S. Prusty u8 LFEPBL01_LSV36_DM_INH7; 354478f544eSSubhransu S. Prusty }; 355478f544eSSubhransu S. Prusty 3563787a398SRakesh Ughreja static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev, 357754695f9SJeeja KP struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port) 358a657f1d0SSubhransu S. Prusty { 359a657f1d0SSubhransu S. Prusty uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE]; 360a657f1d0SSubhransu S. Prusty struct hdmi_audio_infoframe frame; 361754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 362478f544eSSubhransu S. Prusty struct dp_audio_infoframe dp_ai; 3633787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 364ab1eea19SJeeja KP struct hdac_hdmi_cvt *cvt = pcm->cvt; 365478f544eSSubhransu S. Prusty u8 *dip; 366a657f1d0SSubhransu S. Prusty int ret; 367a657f1d0SSubhransu S. Prusty int i; 368478f544eSSubhransu S. Prusty const u8 *eld_buf; 369478f544eSSubhransu S. Prusty u8 conn_type; 370bcced704SSubhransu S. Prusty int channels, ca; 371a657f1d0SSubhransu S. Prusty 3723787a398SRakesh Ughreja ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc, 373ab1eea19SJeeja KP pcm->channels, pcm->chmap_set, true, pcm->chmap); 374bcced704SSubhransu S. Prusty 375bcced704SSubhransu S. Prusty channels = snd_hdac_get_active_channels(ca); 3763787a398SRakesh Ughreja hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels); 377bcced704SSubhransu S. Prusty 378bcced704SSubhransu S. Prusty snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca, 379ab1eea19SJeeja KP pcm->channels, pcm->chmap, pcm->chmap_set); 380bcced704SSubhransu S. Prusty 381754695f9SJeeja KP eld_buf = port->eld.eld_buffer; 382478f544eSSubhransu S. Prusty conn_type = drm_eld_get_conn_type(eld_buf); 383a657f1d0SSubhransu S. Prusty 384478f544eSSubhransu S. Prusty switch (conn_type) { 385478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_HDMI: 386478f544eSSubhransu S. Prusty hdmi_audio_infoframe_init(&frame); 387478f544eSSubhransu S. Prusty 388478f544eSSubhransu S. Prusty frame.channels = channels; 389bcced704SSubhransu S. Prusty frame.channel_allocation = ca; 390a657f1d0SSubhransu S. Prusty 391a657f1d0SSubhransu S. Prusty ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); 392a657f1d0SSubhransu S. Prusty if (ret < 0) 393a657f1d0SSubhransu S. Prusty return ret; 394a657f1d0SSubhransu S. Prusty 395478f544eSSubhransu S. Prusty break; 396478f544eSSubhransu S. Prusty 397478f544eSSubhransu S. Prusty case DRM_ELD_CONN_TYPE_DP: 398478f544eSSubhransu S. Prusty memset(&dp_ai, 0, sizeof(dp_ai)); 399478f544eSSubhransu S. Prusty dp_ai.type = 0x84; 400478f544eSSubhransu S. Prusty dp_ai.len = 0x1b; 401478f544eSSubhransu S. Prusty dp_ai.ver = 0x11 << 2; 402478f544eSSubhransu S. Prusty dp_ai.CC02_CT47 = channels - 1; 403bcced704SSubhransu S. Prusty dp_ai.CA = ca; 404478f544eSSubhransu S. Prusty 405478f544eSSubhransu S. Prusty dip = (u8 *)&dp_ai; 406478f544eSSubhransu S. Prusty break; 407478f544eSSubhransu S. Prusty 408478f544eSSubhransu S. Prusty default: 4093787a398SRakesh Ughreja dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type); 410478f544eSSubhransu S. Prusty return -EIO; 411478f544eSSubhransu S. Prusty } 412478f544eSSubhransu S. Prusty 413a657f1d0SSubhransu S. Prusty /* stop infoframe transmission */ 4143787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 4153787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 416a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE); 417a657f1d0SSubhransu S. Prusty 418a657f1d0SSubhransu S. Prusty 419a657f1d0SSubhransu S. Prusty /* Fill infoframe. Index auto-incremented */ 4203787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 421478f544eSSubhransu S. Prusty if (conn_type == DRM_ELD_CONN_TYPE_HDMI) { 422391005e8SSubhransu S. Prusty for (i = 0; i < sizeof(buffer); i++) 4233787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 424391005e8SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, buffer[i]); 425478f544eSSubhransu S. Prusty } else { 426478f544eSSubhransu S. Prusty for (i = 0; i < sizeof(dp_ai); i++) 4273787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 428478f544eSSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_DATA, dip[i]); 429478f544eSSubhransu S. Prusty } 430a657f1d0SSubhransu S. Prusty 431a657f1d0SSubhransu S. Prusty /* Start infoframe */ 4323787a398SRakesh Ughreja hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0); 4333787a398SRakesh Ughreja snd_hdac_codec_write(hdev, pin->nid, 0, 434a657f1d0SSubhransu S. Prusty AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST); 435a657f1d0SSubhransu S. Prusty 436a657f1d0SSubhransu S. Prusty return 0; 437a657f1d0SSubhransu S. Prusty } 438a657f1d0SSubhransu S. Prusty 439c9bfb5d7SJeeja KP static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai, 440c9bfb5d7SJeeja KP unsigned int tx_mask, unsigned int rx_mask, 441c9bfb5d7SJeeja KP int slots, int slot_width) 442b0362adbSSubhransu S. Prusty { 4433787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 4443787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 445754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 446c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 447c9bfb5d7SJeeja KP 4483787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask); 449b0362adbSSubhransu S. Prusty 450b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 451b0362adbSSubhransu S. Prusty 452c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 453b0362adbSSubhransu S. Prusty 454c9bfb5d7SJeeja KP if (pcm) 455c9bfb5d7SJeeja KP pcm->stream_tag = (tx_mask << 4); 456bcced704SSubhransu S. Prusty 457c9bfb5d7SJeeja KP return 0; 458b0362adbSSubhransu S. Prusty } 459b0362adbSSubhransu S. Prusty 460b0362adbSSubhransu S. Prusty static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream, 461b0362adbSSubhransu S. Prusty struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai) 462b0362adbSSubhransu S. Prusty { 4633787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 464754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 465c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 466c9bfb5d7SJeeja KP int format; 467b0362adbSSubhransu S. Prusty 46854dfa1eaSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 469b0362adbSSubhransu S. Prusty 470c9bfb5d7SJeeja KP format = snd_hdac_calc_stream_format(params_rate(hparams), 471b0362adbSSubhransu S. Prusty params_channels(hparams), params_format(hparams), 47266d6bbc6SJeeja KP dai->driver->playback.sig_bits, 0); 473b0362adbSSubhransu S. Prusty 474c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 475c9bfb5d7SJeeja KP if (!pcm) 476148569fdSSubhransu S. Prusty return -EIO; 477148569fdSSubhransu S. Prusty 478c9bfb5d7SJeeja KP pcm->format = format; 479c9bfb5d7SJeeja KP pcm->channels = params_channels(hparams); 480148569fdSSubhransu S. Prusty 481148569fdSSubhransu S. Prusty return 0; 482148569fdSSubhransu S. Prusty } 483148569fdSSubhransu S. Prusty 4843787a398SRakesh Ughreja static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev, 485754695f9SJeeja KP struct hdac_hdmi_pin *pin, 486754695f9SJeeja KP struct hdac_hdmi_port *port) 487148569fdSSubhransu S. Prusty { 4883787a398SRakesh Ughreja if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) { 4893787a398SRakesh Ughreja dev_warn(&hdev->dev, 490148569fdSSubhransu S. Prusty "HDMI: pin %d wcaps %#x does not support connection list\n", 4913787a398SRakesh Ughreja pin->nid, get_wcaps(hdev, pin->nid)); 492148569fdSSubhransu S. Prusty return -EINVAL; 493148569fdSSubhransu S. Prusty } 494148569fdSSubhransu S. Prusty 4953787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 4961b46ebd1SJeeja KP return -EIO; 4971b46ebd1SJeeja KP 4983787a398SRakesh Ughreja port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid, 499754695f9SJeeja KP port->mux_nids, HDA_MAX_CONNECTIONS); 500754695f9SJeeja KP if (port->num_mux_nids == 0) 5013787a398SRakesh Ughreja dev_warn(&hdev->dev, 502754695f9SJeeja KP "No connections found for pin:port %d:%d\n", 503754695f9SJeeja KP pin->nid, port->id); 504148569fdSSubhransu S. Prusty 5053787a398SRakesh Ughreja dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n", 506754695f9SJeeja KP port->num_mux_nids, pin->nid, port->id); 507148569fdSSubhransu S. Prusty 508754695f9SJeeja KP return port->num_mux_nids; 509148569fdSSubhransu S. Prusty } 510148569fdSSubhransu S. Prusty 511148569fdSSubhransu S. Prusty /* 512754695f9SJeeja KP * Query pcm list and return port to which stream is routed. 513148569fdSSubhransu S. Prusty * 514754695f9SJeeja KP * Also query connection list of the pin, to validate the cvt to port map. 515148569fdSSubhransu S. Prusty * 516754695f9SJeeja KP * Same stream rendering to multiple ports simultaneously can be done 517754695f9SJeeja KP * possibly, but not supported for now in driver. So return the first port 518148569fdSSubhransu S. Prusty * connected. 519148569fdSSubhransu S. Prusty */ 520754695f9SJeeja KP static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt( 5213787a398SRakesh Ughreja struct hdac_device *hdev, 522148569fdSSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, 523148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt) 524148569fdSSubhransu S. Prusty { 525148569fdSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm; 526754695f9SJeeja KP struct hdac_hdmi_port *port = NULL; 527148569fdSSubhransu S. Prusty int ret, i; 528148569fdSSubhransu S. Prusty 529148569fdSSubhransu S. Prusty list_for_each_entry(pcm, &hdmi->pcm_list, head) { 530148569fdSSubhransu S. Prusty if (pcm->cvt == cvt) { 531e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 532e0e5d3e5SJeeja KP continue; 533148569fdSSubhransu S. Prusty 534e0e5d3e5SJeeja KP list_for_each_entry(port, &pcm->port_list, head) { 535e0e5d3e5SJeeja KP mutex_lock(&pcm->lock); 5363787a398SRakesh Ughreja ret = hdac_hdmi_query_port_connlist(hdev, 537e0e5d3e5SJeeja KP port->pin, port); 538e0e5d3e5SJeeja KP mutex_unlock(&pcm->lock); 539148569fdSSubhransu S. Prusty if (ret < 0) 540e0e5d3e5SJeeja KP continue; 541148569fdSSubhransu S. Prusty 542754695f9SJeeja KP for (i = 0; i < port->num_mux_nids; i++) { 543e0e5d3e5SJeeja KP if (port->mux_nids[i] == cvt->nid && 544e0e5d3e5SJeeja KP port->eld.monitor_present && 545e0e5d3e5SJeeja KP port->eld.eld_valid) 546754695f9SJeeja KP return port; 547148569fdSSubhransu S. Prusty } 548148569fdSSubhransu S. Prusty } 549e0e5d3e5SJeeja KP } 550e0e5d3e5SJeeja KP } 551148569fdSSubhransu S. Prusty 552148569fdSSubhransu S. Prusty return NULL; 553148569fdSSubhransu S. Prusty } 554148569fdSSubhransu S. Prusty 55554dfa1eaSSubhransu S. Prusty /* 556489f231eSKai Vehmanen * Go through all converters and ensure connection is set to 557489f231eSKai Vehmanen * the correct pin as set via kcontrols. 558489f231eSKai Vehmanen */ 559489f231eSKai Vehmanen static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev) 560489f231eSKai Vehmanen { 561489f231eSKai Vehmanen struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 562489f231eSKai Vehmanen struct hdac_hdmi_port *port; 563489f231eSKai Vehmanen struct hdac_hdmi_cvt *cvt; 564489f231eSKai Vehmanen int cvt_idx = 0; 565489f231eSKai Vehmanen 566489f231eSKai Vehmanen list_for_each_entry(cvt, &hdmi->cvt_list, head) { 567489f231eSKai Vehmanen port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt); 568489f231eSKai Vehmanen if (port && port->pin) { 569489f231eSKai Vehmanen snd_hdac_codec_write(hdev, port->pin->nid, 0, 570489f231eSKai Vehmanen AC_VERB_SET_CONNECT_SEL, cvt_idx); 571489f231eSKai Vehmanen dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n", 572489f231eSKai Vehmanen __func__, cvt->name, port->pin->nid, cvt_idx); 573489f231eSKai Vehmanen } 574489f231eSKai Vehmanen ++cvt_idx; 575489f231eSKai Vehmanen } 576489f231eSKai Vehmanen } 577489f231eSKai Vehmanen 578489f231eSKai Vehmanen /* 57954dfa1eaSSubhransu S. Prusty * This tries to get a valid pin and set the HW constraints based on the 58054dfa1eaSSubhransu S. Prusty * ELD. Even if a valid pin is not found return success so that device open 58154dfa1eaSSubhransu S. Prusty * doesn't fail. 58254dfa1eaSSubhransu S. Prusty */ 583b0362adbSSubhransu S. Prusty static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream, 584b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 585b0362adbSSubhransu S. Prusty { 5863787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 5873787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 588754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 589148569fdSSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 590754695f9SJeeja KP struct hdac_hdmi_port *port; 5912428bca3SSubhransu S. Prusty int ret; 592b0362adbSSubhransu S. Prusty 593b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 594b0362adbSSubhransu S. Prusty 595148569fdSSubhransu S. Prusty cvt = dai_map->cvt; 5963787a398SRakesh Ughreja port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt); 59754dfa1eaSSubhransu S. Prusty 59854dfa1eaSSubhransu S. Prusty /* 59954dfa1eaSSubhransu S. Prusty * To make PA and other userland happy. 60054dfa1eaSSubhransu S. Prusty * userland scans devices so returning error does not help. 60154dfa1eaSSubhransu S. Prusty */ 602754695f9SJeeja KP if (!port) 60354dfa1eaSSubhransu S. Prusty return 0; 604754695f9SJeeja KP if ((!port->eld.monitor_present) || 605754695f9SJeeja KP (!port->eld.eld_valid)) { 606b0362adbSSubhransu S. Prusty 6073787a398SRakesh Ughreja dev_warn(&hdev->dev, 608754695f9SJeeja KP "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n", 609754695f9SJeeja KP port->eld.monitor_present, port->eld.eld_valid, 610754695f9SJeeja KP port->pin->nid, port->id); 611b8a54545SSubhransu S. Prusty 61254dfa1eaSSubhransu S. Prusty return 0; 613b0362adbSSubhransu S. Prusty } 614b0362adbSSubhransu S. Prusty 615754695f9SJeeja KP dai_map->port = port; 616b0362adbSSubhransu S. Prusty 6172428bca3SSubhransu S. Prusty ret = hdac_hdmi_eld_limit_formats(substream->runtime, 618754695f9SJeeja KP port->eld.eld_buffer); 6192428bca3SSubhransu S. Prusty if (ret < 0) 6202428bca3SSubhransu S. Prusty return ret; 621b0362adbSSubhransu S. Prusty 6222428bca3SSubhransu S. Prusty return snd_pcm_hw_constraint_eld(substream->runtime, 623754695f9SJeeja KP port->eld.eld_buffer); 624b0362adbSSubhransu S. Prusty } 625b0362adbSSubhransu S. Prusty 626b0362adbSSubhransu S. Prusty static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream, 627b0362adbSSubhransu S. Prusty struct snd_soc_dai *dai) 628b0362adbSSubhransu S. Prusty { 6293787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai); 630754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 631ab1eea19SJeeja KP struct hdac_hdmi_pcm *pcm; 632b0362adbSSubhransu S. Prusty 633b0362adbSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai->id]; 634b0362adbSSubhransu S. Prusty 635ab1eea19SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt); 636bcced704SSubhransu S. Prusty 637ab1eea19SJeeja KP if (pcm) { 638ab1eea19SJeeja KP mutex_lock(&pcm->lock); 639ab1eea19SJeeja KP pcm->chmap_set = false; 640ab1eea19SJeeja KP memset(pcm->chmap, 0, sizeof(pcm->chmap)); 641ab1eea19SJeeja KP pcm->channels = 0; 642ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 643b0362adbSSubhransu S. Prusty } 644ab1eea19SJeeja KP 645754695f9SJeeja KP if (dai_map->port) 646754695f9SJeeja KP dai_map->port = NULL; 64754dfa1eaSSubhransu S. Prusty } 648b0362adbSSubhransu S. Prusty 64918382eadSSubhransu S. Prusty static int 650f0c5ebebSUghreja, Rakesh A hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt) 65118382eadSSubhransu S. Prusty { 652bcced704SSubhransu S. Prusty unsigned int chans; 653f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 65418382eadSSubhransu S. Prusty int err; 65518382eadSSubhransu S. Prusty 656f0c5ebebSUghreja, Rakesh A chans = get_wcaps(hdev, cvt->nid); 657bcced704SSubhransu S. Prusty chans = get_wcaps_channels(chans); 658bcced704SSubhransu S. Prusty 659bcced704SSubhransu S. Prusty cvt->params.channels_min = 2; 660bcced704SSubhransu S. Prusty 661bcced704SSubhransu S. Prusty cvt->params.channels_max = chans; 662bcced704SSubhransu S. Prusty if (chans > hdmi->chmap.channels_max) 663bcced704SSubhransu S. Prusty hdmi->chmap.channels_max = chans; 66418382eadSSubhransu S. Prusty 665f0c5ebebSUghreja, Rakesh A err = snd_hdac_query_supported_pcm(hdev, cvt->nid, 66618382eadSSubhransu S. Prusty &cvt->params.rates, 66718382eadSSubhransu S. Prusty &cvt->params.formats, 66818382eadSSubhransu S. Prusty &cvt->params.maxbps); 66918382eadSSubhransu S. Prusty if (err < 0) 670f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, 67118382eadSSubhransu S. Prusty "Failed to query pcm params for nid %d: %d\n", 67218382eadSSubhransu S. Prusty cvt->nid, err); 67318382eadSSubhransu S. Prusty 67418382eadSSubhransu S. Prusty return err; 67518382eadSSubhransu S. Prusty } 67618382eadSSubhransu S. Prusty 67779f4e922SSubhransu S. Prusty static int hdac_hdmi_fill_widget_info(struct device *dev, 678c9bfb5d7SJeeja KP struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id, 679c9bfb5d7SJeeja KP void *priv, const char *wname, const char *stream, 680c9bfb5d7SJeeja KP struct snd_kcontrol_new *wc, int numkc, 681c9bfb5d7SJeeja KP int (*event)(struct snd_soc_dapm_widget *, 682c9bfb5d7SJeeja KP struct snd_kcontrol *, int), unsigned short event_flags) 68318382eadSSubhransu S. Prusty { 68418382eadSSubhransu S. Prusty w->id = id; 68579f4e922SSubhransu S. Prusty w->name = devm_kstrdup(dev, wname, GFP_KERNEL); 68679f4e922SSubhransu S. Prusty if (!w->name) 68779f4e922SSubhransu S. Prusty return -ENOMEM; 68879f4e922SSubhransu S. Prusty 68918382eadSSubhransu S. Prusty w->sname = stream; 69018382eadSSubhransu S. Prusty w->reg = SND_SOC_NOPM; 69118382eadSSubhransu S. Prusty w->shift = 0; 69279f4e922SSubhransu S. Prusty w->kcontrol_news = wc; 69379f4e922SSubhransu S. Prusty w->num_kcontrols = numkc; 69479f4e922SSubhransu S. Prusty w->priv = priv; 695c9bfb5d7SJeeja KP w->event = event; 696c9bfb5d7SJeeja KP w->event_flags = event_flags; 69779f4e922SSubhransu S. Prusty 69879f4e922SSubhransu S. Prusty return 0; 69918382eadSSubhransu S. Prusty } 70018382eadSSubhransu S. Prusty 70118382eadSSubhransu S. Prusty static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route, 70279f4e922SSubhransu S. Prusty const char *sink, const char *control, const char *src, 70379f4e922SSubhransu S. Prusty int (*handler)(struct snd_soc_dapm_widget *src, 70479f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *sink)) 70518382eadSSubhransu S. Prusty { 70618382eadSSubhransu S. Prusty route->sink = sink; 70718382eadSSubhransu S. Prusty route->source = src; 70818382eadSSubhransu S. Prusty route->control = control; 70979f4e922SSubhransu S. Prusty route->connected = handler; 71018382eadSSubhransu S. Prusty } 71118382eadSSubhransu S. Prusty 7123787a398SRakesh Ughreja static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev, 713754695f9SJeeja KP struct hdac_hdmi_port *port) 7144a3478deSJeeja KP { 7153787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 7164a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 717e0e5d3e5SJeeja KP struct hdac_hdmi_port *p; 7184a3478deSJeeja KP 7194a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 720e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 721754695f9SJeeja KP continue; 722754695f9SJeeja KP 723e0e5d3e5SJeeja KP list_for_each_entry(p, &pcm->port_list, head) { 724e0e5d3e5SJeeja KP if (p->id == port->id && port->pin == p->pin) 7254a3478deSJeeja KP return pcm; 7264a3478deSJeeja KP } 727e0e5d3e5SJeeja KP } 7284a3478deSJeeja KP 7294a3478deSJeeja KP return NULL; 7304a3478deSJeeja KP } 7314a3478deSJeeja KP 7323787a398SRakesh Ughreja static void hdac_hdmi_set_power_state(struct hdac_device *hdev, 733c9bfb5d7SJeeja KP hda_nid_t nid, unsigned int pwr_state) 734c9bfb5d7SJeeja KP { 735753597fbSAbhijeet Kumar int count; 736753597fbSAbhijeet Kumar unsigned int state; 737753597fbSAbhijeet Kumar 7383787a398SRakesh Ughreja if (get_wcaps(hdev, nid) & AC_WCAP_POWER) { 7393787a398SRakesh Ughreja if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) { 740753597fbSAbhijeet Kumar for (count = 0; count < 10; count++) { 7413787a398SRakesh Ughreja snd_hdac_codec_read(hdev, nid, 0, 742753597fbSAbhijeet Kumar AC_VERB_SET_POWER_STATE, 743753597fbSAbhijeet Kumar pwr_state); 7443787a398SRakesh Ughreja state = snd_hdac_sync_power_state(hdev, 745753597fbSAbhijeet Kumar nid, pwr_state); 746753597fbSAbhijeet Kumar if (!(state & AC_PWRST_ERROR)) 747753597fbSAbhijeet Kumar break; 748753597fbSAbhijeet Kumar } 749753597fbSAbhijeet Kumar } 750c9bfb5d7SJeeja KP } 751c9bfb5d7SJeeja KP } 752c9bfb5d7SJeeja KP 7533787a398SRakesh Ughreja static void hdac_hdmi_set_amp(struct hdac_device *hdev, 754c9bfb5d7SJeeja KP hda_nid_t nid, int val) 755c9bfb5d7SJeeja KP { 7563787a398SRakesh Ughreja if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP) 7573787a398SRakesh Ughreja snd_hdac_codec_write(hdev, nid, 0, 758c9bfb5d7SJeeja KP AC_VERB_SET_AMP_GAIN_MUTE, val); 759c9bfb5d7SJeeja KP } 760c9bfb5d7SJeeja KP 761c9bfb5d7SJeeja KP 762c9bfb5d7SJeeja KP static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w, 763c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 764c9bfb5d7SJeeja KP { 765754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 7663787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 767c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 768c9bfb5d7SJeeja KP 7693787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 770c9bfb5d7SJeeja KP __func__, w->name, event); 771c9bfb5d7SJeeja KP 7723787a398SRakesh Ughreja pcm = hdac_hdmi_get_pcm(hdev, port); 773c9bfb5d7SJeeja KP if (!pcm) 774c9bfb5d7SJeeja KP return -EIO; 775c9bfb5d7SJeeja KP 7761b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 7773787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 7781b46ebd1SJeeja KP return -EIO; 7791b46ebd1SJeeja KP 780c9bfb5d7SJeeja KP switch (event) { 781c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 7823787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0); 783c9bfb5d7SJeeja KP 784c9bfb5d7SJeeja KP /* Enable out path for this pin widget */ 7853787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 786c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); 787c9bfb5d7SJeeja KP 7883787a398SRakesh Ughreja hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE); 789c9bfb5d7SJeeja KP 7903787a398SRakesh Ughreja return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port); 791c9bfb5d7SJeeja KP 792c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 7933787a398SRakesh Ughreja hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE); 794c9bfb5d7SJeeja KP 795c9bfb5d7SJeeja KP /* Disable out path for this pin widget */ 7963787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 797c9bfb5d7SJeeja KP AC_VERB_SET_PIN_WIDGET_CONTROL, 0); 798c9bfb5d7SJeeja KP 7993787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3); 800c9bfb5d7SJeeja KP break; 801c9bfb5d7SJeeja KP 802c9bfb5d7SJeeja KP } 803c9bfb5d7SJeeja KP 804c9bfb5d7SJeeja KP return 0; 805c9bfb5d7SJeeja KP } 806c9bfb5d7SJeeja KP 807c9bfb5d7SJeeja KP static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w, 808c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 809c9bfb5d7SJeeja KP { 810c9bfb5d7SJeeja KP struct hdac_hdmi_cvt *cvt = w->priv; 8113787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 8123787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 813c9bfb5d7SJeeja KP struct hdac_hdmi_pcm *pcm; 814c9bfb5d7SJeeja KP 8153787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 816c9bfb5d7SJeeja KP __func__, w->name, event); 817c9bfb5d7SJeeja KP 818c9bfb5d7SJeeja KP pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt); 819c9bfb5d7SJeeja KP if (!pcm) 820c9bfb5d7SJeeja KP return -EIO; 821c9bfb5d7SJeeja KP 822c9bfb5d7SJeeja KP switch (event) { 823c9bfb5d7SJeeja KP case SND_SOC_DAPM_PRE_PMU: 8243787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0); 825c9bfb5d7SJeeja KP 826c9bfb5d7SJeeja KP /* Enable transmission */ 8273787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 828c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_1, 1); 829c9bfb5d7SJeeja KP 830c9bfb5d7SJeeja KP /* Category Code (CC) to zero */ 8313787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 832c9bfb5d7SJeeja KP AC_VERB_SET_DIGI_CONVERT_2, 0); 833c9bfb5d7SJeeja KP 8343787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 835c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag); 8363787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 837c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, pcm->format); 838489f231eSKai Vehmanen 839489f231eSKai Vehmanen /* 840489f231eSKai Vehmanen * The connection indices are shared by all converters and 841489f231eSKai Vehmanen * may interfere with each other. Ensure correct 842489f231eSKai Vehmanen * routing for all converters at stream start. 843489f231eSKai Vehmanen */ 844489f231eSKai Vehmanen hdac_hdmi_verify_connect_sel_all_pins(hdev); 845489f231eSKai Vehmanen 846c9bfb5d7SJeeja KP break; 847c9bfb5d7SJeeja KP 848c9bfb5d7SJeeja KP case SND_SOC_DAPM_POST_PMD: 8493787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 850c9bfb5d7SJeeja KP AC_VERB_SET_CHANNEL_STREAMID, 0); 8513787a398SRakesh Ughreja snd_hdac_codec_write(hdev, cvt->nid, 0, 852c9bfb5d7SJeeja KP AC_VERB_SET_STREAM_FORMAT, 0); 853c9bfb5d7SJeeja KP 8543787a398SRakesh Ughreja hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3); 855c9bfb5d7SJeeja KP break; 856c9bfb5d7SJeeja KP 857c9bfb5d7SJeeja KP } 858c9bfb5d7SJeeja KP 859c9bfb5d7SJeeja KP return 0; 860c9bfb5d7SJeeja KP } 861c9bfb5d7SJeeja KP 862c9bfb5d7SJeeja KP static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w, 863c9bfb5d7SJeeja KP struct snd_kcontrol *kc, int event) 864c9bfb5d7SJeeja KP { 865754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 8663787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev); 867c9bfb5d7SJeeja KP int mux_idx; 868c9bfb5d7SJeeja KP 8693787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n", 870c9bfb5d7SJeeja KP __func__, w->name, event); 871c9bfb5d7SJeeja KP 872c9bfb5d7SJeeja KP if (!kc) 873c9bfb5d7SJeeja KP kc = w->kcontrols[0]; 874c9bfb5d7SJeeja KP 875c9bfb5d7SJeeja KP mux_idx = dapm_kcontrol_get_value(kc); 8761b46ebd1SJeeja KP 8771b46ebd1SJeeja KP /* set the device if pin is mst_capable */ 8783787a398SRakesh Ughreja if (hdac_hdmi_port_select_set(hdev, port) < 0) 8791b46ebd1SJeeja KP return -EIO; 8801b46ebd1SJeeja KP 881c9bfb5d7SJeeja KP if (mux_idx > 0) { 8823787a398SRakesh Ughreja snd_hdac_codec_write(hdev, port->pin->nid, 0, 883c9bfb5d7SJeeja KP AC_VERB_SET_CONNECT_SEL, (mux_idx - 1)); 884c9bfb5d7SJeeja KP } 885c9bfb5d7SJeeja KP 886c9bfb5d7SJeeja KP return 0; 887c9bfb5d7SJeeja KP } 888c9bfb5d7SJeeja KP 8894a3478deSJeeja KP /* 8904a3478deSJeeja KP * Based on user selection, map the PINs with the PCMs. 8914a3478deSJeeja KP */ 892754695f9SJeeja KP static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol, 8934a3478deSJeeja KP struct snd_ctl_elem_value *ucontrol) 8944a3478deSJeeja KP { 8954a3478deSJeeja KP int ret; 896e0e5d3e5SJeeja KP struct hdac_hdmi_port *p, *p_next; 8974a3478deSJeeja KP struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 8984a3478deSJeeja KP struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol); 8994a3478deSJeeja KP struct snd_soc_dapm_context *dapm = w->dapm; 900754695f9SJeeja KP struct hdac_hdmi_port *port = w->priv; 9013787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev); 9023787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 9034a3478deSJeeja KP struct hdac_hdmi_pcm *pcm = NULL; 9044a3478deSJeeja KP const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]]; 9054a3478deSJeeja KP 9064a3478deSJeeja KP ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol); 9074a3478deSJeeja KP if (ret < 0) 9084a3478deSJeeja KP return ret; 9094a3478deSJeeja KP 910754695f9SJeeja KP if (port == NULL) 911754695f9SJeeja KP return -EINVAL; 912754695f9SJeeja KP 9134a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 9144a3478deSJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 915e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 916e0e5d3e5SJeeja KP continue; 917e0e5d3e5SJeeja KP 918e0e5d3e5SJeeja KP list_for_each_entry_safe(p, p_next, &pcm->port_list, head) { 919e0e5d3e5SJeeja KP if (p == port && p->id == port->id && 920e0e5d3e5SJeeja KP p->pin == port->pin) { 921332ccf00STakashi Iwai hdac_hdmi_jack_report_sync(pcm, port, false); 922e0e5d3e5SJeeja KP list_del(&p->head); 923e0e5d3e5SJeeja KP } 924e0e5d3e5SJeeja KP } 925e0e5d3e5SJeeja KP } 9264a3478deSJeeja KP 9274a3478deSJeeja KP /* 9284a3478deSJeeja KP * Jack status is not reported during device probe as the 9294a3478deSJeeja KP * PCMs are not registered by then. So report it here. 9304a3478deSJeeja KP */ 931e0e5d3e5SJeeja KP list_for_each_entry(pcm, &hdmi->pcm_list, head) { 932e0e5d3e5SJeeja KP if (!strcmp(cvt_name, pcm->cvt->name)) { 933e0e5d3e5SJeeja KP list_add_tail(&port->head, &pcm->port_list); 934754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 935332ccf00STakashi Iwai hdac_hdmi_jack_report_sync(pcm, port, true); 9364a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 9374a3478deSJeeja KP return ret; 9384a3478deSJeeja KP } 9394a3478deSJeeja KP } 940e0e5d3e5SJeeja KP } 9414a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 9424a3478deSJeeja KP 9434a3478deSJeeja KP return ret; 9444a3478deSJeeja KP } 9454a3478deSJeeja KP 94679f4e922SSubhransu S. Prusty /* 94779f4e922SSubhransu S. Prusty * Ideally the Mux inputs should be based on the num_muxs enumerated, but 94879f4e922SSubhransu S. Prusty * the display driver seem to be programming the connection list for the pin 94979f4e922SSubhransu S. Prusty * widget runtime. 95079f4e922SSubhransu S. Prusty * 95179f4e922SSubhransu S. Prusty * So programming all the possible inputs for the mux, the user has to take 95279f4e922SSubhransu S. Prusty * care of selecting the right one and leaving all other inputs selected to 95379f4e922SSubhransu S. Prusty * "NONE" 95479f4e922SSubhransu S. Prusty */ 9553787a398SRakesh Ughreja static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev, 956754695f9SJeeja KP struct hdac_hdmi_port *port, 95779f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widget, 95879f4e922SSubhransu S. Prusty const char *widget_name) 95918382eadSSubhransu S. Prusty { 9603787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 961754695f9SJeeja KP struct hdac_hdmi_pin *pin = port->pin; 96279f4e922SSubhransu S. Prusty struct snd_kcontrol_new *kc; 96379f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 96479f4e922SSubhransu S. Prusty struct soc_enum *se; 96579f4e922SSubhransu S. Prusty char kc_name[NAME_SIZE]; 96679f4e922SSubhransu S. Prusty char mux_items[NAME_SIZE]; 96779f4e922SSubhransu S. Prusty /* To hold inputs to the Pin mux */ 96879f4e922SSubhransu S. Prusty char *items[HDA_MAX_CONNECTIONS]; 96979f4e922SSubhransu S. Prusty int i = 0; 97079f4e922SSubhransu S. Prusty int num_items = hdmi->num_cvt + 1; 97118382eadSSubhransu S. Prusty 9723787a398SRakesh Ughreja kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL); 97379f4e922SSubhransu S. Prusty if (!kc) 97479f4e922SSubhransu S. Prusty return -ENOMEM; 97518382eadSSubhransu S. Prusty 9763787a398SRakesh Ughreja se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL); 97779f4e922SSubhransu S. Prusty if (!se) 97879f4e922SSubhransu S. Prusty return -ENOMEM; 97918382eadSSubhransu S. Prusty 98070e97a2dSSubhransu S. Prusty snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input", 98170e97a2dSSubhransu S. Prusty pin->nid, port->id); 9823787a398SRakesh Ughreja kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL); 98379f4e922SSubhransu S. Prusty if (!kc->name) 98479f4e922SSubhransu S. Prusty return -ENOMEM; 98518382eadSSubhransu S. Prusty 98679f4e922SSubhransu S. Prusty kc->private_value = (long)se; 98779f4e922SSubhransu S. Prusty kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 98879f4e922SSubhransu S. Prusty kc->access = 0; 98979f4e922SSubhransu S. Prusty kc->info = snd_soc_info_enum_double; 990754695f9SJeeja KP kc->put = hdac_hdmi_set_pin_port_mux; 99179f4e922SSubhransu S. Prusty kc->get = snd_soc_dapm_get_enum_double; 99279f4e922SSubhransu S. Prusty 99379f4e922SSubhransu S. Prusty se->reg = SND_SOC_NOPM; 99479f4e922SSubhransu S. Prusty 99579f4e922SSubhransu S. Prusty /* enum texts: ["NONE", "cvt #", "cvt #", ...] */ 99679f4e922SSubhransu S. Prusty se->items = num_items; 99779f4e922SSubhransu S. Prusty se->mask = roundup_pow_of_two(se->items) - 1; 99879f4e922SSubhransu S. Prusty 99979f4e922SSubhransu S. Prusty sprintf(mux_items, "NONE"); 10003787a398SRakesh Ughreja items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL); 100179f4e922SSubhransu S. Prusty if (!items[i]) 100279f4e922SSubhransu S. Prusty return -ENOMEM; 100379f4e922SSubhransu S. Prusty 100479f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 100579f4e922SSubhransu S. Prusty i++; 100679f4e922SSubhransu S. Prusty sprintf(mux_items, "cvt %d", cvt->nid); 10073787a398SRakesh Ughreja items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL); 100879f4e922SSubhransu S. Prusty if (!items[i]) 100979f4e922SSubhransu S. Prusty return -ENOMEM; 101079f4e922SSubhransu S. Prusty } 101179f4e922SSubhransu S. Prusty 10123787a398SRakesh Ughreja se->texts = devm_kmemdup(&hdev->dev, items, 101379f4e922SSubhransu S. Prusty (num_items * sizeof(char *)), GFP_KERNEL); 101479f4e922SSubhransu S. Prusty if (!se->texts) 101579f4e922SSubhransu S. Prusty return -ENOMEM; 101679f4e922SSubhransu S. Prusty 10173787a398SRakesh Ughreja return hdac_hdmi_fill_widget_info(&hdev->dev, widget, 1018754695f9SJeeja KP snd_soc_dapm_mux, port, widget_name, NULL, kc, 1, 1019c9bfb5d7SJeeja KP hdac_hdmi_pin_mux_widget_event, 1020c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG); 102179f4e922SSubhransu S. Prusty } 102279f4e922SSubhransu S. Prusty 102379f4e922SSubhransu S. Prusty /* Add cvt <- input <- mux route map */ 10243787a398SRakesh Ughreja static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev, 102579f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets, 102679f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route, int rindex) 102779f4e922SSubhransu S. Prusty { 10283787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 102979f4e922SSubhransu S. Prusty const struct snd_kcontrol_new *kc; 103079f4e922SSubhransu S. Prusty struct soc_enum *se; 1031754695f9SJeeja KP int mux_index = hdmi->num_cvt + hdmi->num_ports; 103279f4e922SSubhransu S. Prusty int i, j; 103379f4e922SSubhransu S. Prusty 1034754695f9SJeeja KP for (i = 0; i < hdmi->num_ports; i++) { 103579f4e922SSubhransu S. Prusty kc = widgets[mux_index].kcontrol_news; 103679f4e922SSubhransu S. Prusty se = (struct soc_enum *)kc->private_value; 103779f4e922SSubhransu S. Prusty for (j = 0; j < hdmi->num_cvt; j++) { 103879f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[rindex], 103979f4e922SSubhransu S. Prusty widgets[mux_index].name, 104079f4e922SSubhransu S. Prusty se->texts[j + 1], 104179f4e922SSubhransu S. Prusty widgets[j].name, NULL); 104279f4e922SSubhransu S. Prusty 104379f4e922SSubhransu S. Prusty rindex++; 104479f4e922SSubhransu S. Prusty } 104579f4e922SSubhransu S. Prusty 104679f4e922SSubhransu S. Prusty mux_index++; 104779f4e922SSubhransu S. Prusty } 104879f4e922SSubhransu S. Prusty } 104979f4e922SSubhransu S. Prusty 105079f4e922SSubhransu S. Prusty /* 105179f4e922SSubhransu S. Prusty * Widgets are added in the below sequence 105279f4e922SSubhransu S. Prusty * Converter widgets for num converters enumerated 1053754695f9SJeeja KP * Pin-port widgets for num ports for Pins enumerated 1054754695f9SJeeja KP * Pin-port mux widgets to represent connenction list of pin widget 105579f4e922SSubhransu S. Prusty * 1056754695f9SJeeja KP * For each port, one Mux and One output widget is added 1057754695f9SJeeja KP * Total widgets elements = num_cvt + (num_ports * 2); 105879f4e922SSubhransu S. Prusty * 105979f4e922SSubhransu S. Prusty * Routes are added as below: 1060754695f9SJeeja KP * pin-port mux -> pin (based on num_ports) 1061754695f9SJeeja KP * cvt -> "Input sel control" -> pin-port_mux 106279f4e922SSubhransu S. Prusty * 106379f4e922SSubhransu S. Prusty * Total route elements: 1064754695f9SJeeja KP * num_ports + (pin_muxes * num_cvt) 106579f4e922SSubhransu S. Prusty */ 106679f4e922SSubhransu S. Prusty static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm) 106779f4e922SSubhransu S. Prusty { 106879f4e922SSubhransu S. Prusty struct snd_soc_dapm_widget *widgets; 106979f4e922SSubhransu S. Prusty struct snd_soc_dapm_route *route; 10703787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev); 10713787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 10721e02dac3SKuninori Morimoto struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv; 107379f4e922SSubhransu S. Prusty char widget_name[NAME_SIZE]; 107479f4e922SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 107579f4e922SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 1076754695f9SJeeja KP int ret, i = 0, num_routes = 0, j; 107779f4e922SSubhransu S. Prusty 107879f4e922SSubhransu S. Prusty if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list)) 107979f4e922SSubhransu S. Prusty return -EINVAL; 108079f4e922SSubhransu S. Prusty 1081754695f9SJeeja KP widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) * 1082754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)), 108379f4e922SSubhransu S. Prusty GFP_KERNEL); 108479f4e922SSubhransu S. Prusty 108579f4e922SSubhransu S. Prusty if (!widgets) 108679f4e922SSubhransu S. Prusty return -ENOMEM; 108779f4e922SSubhransu S. Prusty 108879f4e922SSubhransu S. Prusty /* DAPM widgets to represent each converter widget */ 108979f4e922SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 109079f4e922SSubhransu S. Prusty sprintf(widget_name, "Converter %d", cvt->nid); 109179f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 1092c9bfb5d7SJeeja KP snd_soc_dapm_aif_in, cvt, 1093c9bfb5d7SJeeja KP widget_name, dai_drv[i].playback.stream_name, NULL, 0, 1094c9bfb5d7SJeeja KP hdac_hdmi_cvt_output_widget_event, 1095c9bfb5d7SJeeja KP SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD); 109679f4e922SSubhransu S. Prusty if (ret < 0) 109779f4e922SSubhransu S. Prusty return ret; 109879f4e922SSubhransu S. Prusty i++; 109979f4e922SSubhransu S. Prusty } 110079f4e922SSubhransu S. Prusty 110179f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1102754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1103754695f9SJeeja KP sprintf(widget_name, "hif%d-%d Output", 1104754695f9SJeeja KP pin->nid, pin->ports[j].id); 110579f4e922SSubhransu S. Prusty ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 1106754695f9SJeeja KP snd_soc_dapm_output, &pin->ports[j], 1107c9bfb5d7SJeeja KP widget_name, NULL, NULL, 0, 1108c9bfb5d7SJeeja KP hdac_hdmi_pin_output_widget_event, 1109754695f9SJeeja KP SND_SOC_DAPM_PRE_PMU | 1110754695f9SJeeja KP SND_SOC_DAPM_POST_PMD); 111179f4e922SSubhransu S. Prusty if (ret < 0) 111279f4e922SSubhransu S. Prusty return ret; 11130324e51bSJeeja KP pin->ports[j].output_pin = widgets[i].name; 111479f4e922SSubhransu S. Prusty i++; 111579f4e922SSubhransu S. Prusty } 1116754695f9SJeeja KP } 111779f4e922SSubhransu S. Prusty 111879f4e922SSubhransu S. Prusty /* DAPM widgets to represent the connection list to pin widget */ 111979f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1120754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 1121754695f9SJeeja KP sprintf(widget_name, "Pin%d-Port%d Mux", 1122754695f9SJeeja KP pin->nid, pin->ports[j].id); 11233787a398SRakesh Ughreja ret = hdac_hdmi_create_pin_port_muxs(hdev, 1124754695f9SJeeja KP &pin->ports[j], &widgets[i], 112579f4e922SSubhransu S. Prusty widget_name); 112679f4e922SSubhransu S. Prusty if (ret < 0) 112779f4e922SSubhransu S. Prusty return ret; 112879f4e922SSubhransu S. Prusty i++; 112979f4e922SSubhransu S. Prusty 113079f4e922SSubhransu S. Prusty /* For cvt to pin_mux mapping */ 113179f4e922SSubhransu S. Prusty num_routes += hdmi->num_cvt; 113279f4e922SSubhransu S. Prusty 113379f4e922SSubhransu S. Prusty /* For pin_mux to pin mapping */ 113479f4e922SSubhransu S. Prusty num_routes++; 113579f4e922SSubhransu S. Prusty } 1136754695f9SJeeja KP } 113779f4e922SSubhransu S. Prusty 113879f4e922SSubhransu S. Prusty route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes), 113979f4e922SSubhransu S. Prusty GFP_KERNEL); 114079f4e922SSubhransu S. Prusty if (!route) 114179f4e922SSubhransu S. Prusty return -ENOMEM; 114279f4e922SSubhransu S. Prusty 114379f4e922SSubhransu S. Prusty i = 0; 114479f4e922SSubhransu S. Prusty /* Add pin <- NULL <- mux route map */ 114579f4e922SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1146754695f9SJeeja KP for (j = 0; j < pin->num_ports; j++) { 114779f4e922SSubhransu S. Prusty int sink_index = i + hdmi->num_cvt; 1148754695f9SJeeja KP int src_index = sink_index + pin->num_ports * 1149754695f9SJeeja KP hdmi->num_pin; 115079f4e922SSubhransu S. Prusty 115179f4e922SSubhransu S. Prusty hdac_hdmi_fill_route(&route[i], 115279f4e922SSubhransu S. Prusty widgets[sink_index].name, NULL, 115379f4e922SSubhransu S. Prusty widgets[src_index].name, NULL); 115479f4e922SSubhransu S. Prusty i++; 1155754695f9SJeeja KP } 115679f4e922SSubhransu S. Prusty } 115779f4e922SSubhransu S. Prusty 11583787a398SRakesh Ughreja hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i); 115979f4e922SSubhransu S. Prusty 116079f4e922SSubhransu S. Prusty snd_soc_dapm_new_controls(dapm, widgets, 1161754695f9SJeeja KP ((2 * hdmi->num_ports) + hdmi->num_cvt)); 116279f4e922SSubhransu S. Prusty 116379f4e922SSubhransu S. Prusty snd_soc_dapm_add_routes(dapm, route, num_routes); 116479f4e922SSubhransu S. Prusty snd_soc_dapm_new_widgets(dapm->card); 116579f4e922SSubhransu S. Prusty 116679f4e922SSubhransu S. Prusty return 0; 116779f4e922SSubhransu S. Prusty 116818382eadSSubhransu S. Prusty } 116918382eadSSubhransu S. Prusty 11703787a398SRakesh Ughreja static int hdac_hdmi_init_dai_map(struct hdac_device *hdev) 117118382eadSSubhransu S. Prusty { 11723787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 1173754695f9SJeeja KP struct hdac_hdmi_dai_port_map *dai_map; 117415b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 1175148569fdSSubhransu S. Prusty int dai_id = 0; 117618382eadSSubhransu S. Prusty 1177148569fdSSubhransu S. Prusty if (list_empty(&hdmi->cvt_list)) 117815b91447SSubhransu S. Prusty return -EINVAL; 117918382eadSSubhransu S. Prusty 1180148569fdSSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 1181148569fdSSubhransu S. Prusty dai_map = &hdmi->dai_map[dai_id]; 1182148569fdSSubhransu S. Prusty dai_map->dai_id = dai_id; 118315b91447SSubhransu S. Prusty dai_map->cvt = cvt; 118418382eadSSubhransu S. Prusty 1185148569fdSSubhransu S. Prusty dai_id++; 1186148569fdSSubhransu S. Prusty 1187148569fdSSubhransu S. Prusty if (dai_id == HDA_MAX_CVTS) { 11883787a398SRakesh Ughreja dev_warn(&hdev->dev, 1189148569fdSSubhransu S. Prusty "Max dais supported: %d\n", dai_id); 1190148569fdSSubhransu S. Prusty break; 1191148569fdSSubhransu S. Prusty } 1192148569fdSSubhransu S. Prusty } 119318382eadSSubhransu S. Prusty 119415b91447SSubhransu S. Prusty return 0; 119515b91447SSubhransu S. Prusty } 119615b91447SSubhransu S. Prusty 11973787a398SRakesh Ughreja static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid) 119815b91447SSubhransu S. Prusty { 11993787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 120015b91447SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 12014a3478deSJeeja KP char name[NAME_SIZE]; 120215b91447SSubhransu S. Prusty 1203c7ba4e53SPierre-Louis Bossart cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL); 120415b91447SSubhransu S. Prusty if (!cvt) 120515b91447SSubhransu S. Prusty return -ENOMEM; 120615b91447SSubhransu S. Prusty 120715b91447SSubhransu S. Prusty cvt->nid = nid; 12084a3478deSJeeja KP sprintf(name, "cvt %d", cvt->nid); 1209c7ba4e53SPierre-Louis Bossart cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL); 1210c7ba4e53SPierre-Louis Bossart if (!cvt->name) 1211c7ba4e53SPierre-Louis Bossart return -ENOMEM; 121215b91447SSubhransu S. Prusty 121315b91447SSubhransu S. Prusty list_add_tail(&cvt->head, &hdmi->cvt_list); 121415b91447SSubhransu S. Prusty hdmi->num_cvt++; 121515b91447SSubhransu S. Prusty 12163787a398SRakesh Ughreja return hdac_hdmi_query_cvt_params(hdev, cvt); 121715b91447SSubhransu S. Prusty } 121815b91447SSubhransu S. Prusty 12193787a398SRakesh Ughreja static int hdac_hdmi_parse_eld(struct hdac_device *hdev, 1220754695f9SJeeja KP struct hdac_hdmi_port *port) 1221b7756edeSSubhransu S. Prusty { 1222f6fa11a3SSandeep Tayal unsigned int ver, mnl; 1223f6fa11a3SSandeep Tayal 1224754695f9SJeeja KP ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK) 1225f6fa11a3SSandeep Tayal >> DRM_ELD_VER_SHIFT; 1226f6fa11a3SSandeep Tayal 1227f6fa11a3SSandeep Tayal if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) { 12283787a398SRakesh Ughreja dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver); 1229f6fa11a3SSandeep Tayal return -EINVAL; 1230b7756edeSSubhransu S. Prusty } 1231b7756edeSSubhransu S. Prusty 1232754695f9SJeeja KP mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] & 1233f6fa11a3SSandeep Tayal DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT; 1234f6fa11a3SSandeep Tayal 1235f6fa11a3SSandeep Tayal if (mnl > ELD_MAX_MNL) { 12363787a398SRakesh Ughreja dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl); 1237f6fa11a3SSandeep Tayal return -EINVAL; 1238f6fa11a3SSandeep Tayal } 1239f6fa11a3SSandeep Tayal 1240754695f9SJeeja KP port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER]; 1241f6fa11a3SSandeep Tayal 1242f6fa11a3SSandeep Tayal return 0; 1243f6fa11a3SSandeep Tayal } 1244f6fa11a3SSandeep Tayal 1245754695f9SJeeja KP static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, 1246754695f9SJeeja KP struct hdac_hdmi_port *port) 1247b8a54545SSubhransu S. Prusty { 12483787a398SRakesh Ughreja struct hdac_device *hdev = pin->hdev; 12493787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 12504a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 1251754695f9SJeeja KP int size = 0; 12522acd8309SJeeja KP int port_id = -1; 1253*1f53bcb3SBrent Lu bool eld_valid, eld_changed; 1254754695f9SJeeja KP 1255754695f9SJeeja KP if (!hdmi) 1256754695f9SJeeja KP return; 12574a3478deSJeeja KP 12582acd8309SJeeja KP /* 12592acd8309SJeeja KP * In case of non MST pin, get_eld info API expectes port 12602acd8309SJeeja KP * to be -1. 12612acd8309SJeeja KP */ 12624a3478deSJeeja KP mutex_lock(&hdmi->pin_mutex); 1263754695f9SJeeja KP port->eld.monitor_present = false; 1264f6fa11a3SSandeep Tayal 12652acd8309SJeeja KP if (pin->mst_capable) 12662acd8309SJeeja KP port_id = port->id; 12672acd8309SJeeja KP 12683787a398SRakesh Ughreja size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id, 1269754695f9SJeeja KP &port->eld.monitor_present, 1270754695f9SJeeja KP port->eld.eld_buffer, 1271f6fa11a3SSandeep Tayal ELD_MAX_SIZE); 1272f6fa11a3SSandeep Tayal 1273f6fa11a3SSandeep Tayal if (size > 0) { 1274f6fa11a3SSandeep Tayal size = min(size, ELD_MAX_SIZE); 12753787a398SRakesh Ughreja if (hdac_hdmi_parse_eld(hdev, port) < 0) 1276f6fa11a3SSandeep Tayal size = -EINVAL; 1277f6fa11a3SSandeep Tayal } 1278f6fa11a3SSandeep Tayal 1279*1f53bcb3SBrent Lu eld_valid = port->eld.eld_valid; 1280*1f53bcb3SBrent Lu 1281f6fa11a3SSandeep Tayal if (size > 0) { 1282754695f9SJeeja KP port->eld.eld_valid = true; 1283754695f9SJeeja KP port->eld.eld_size = size; 1284f6fa11a3SSandeep Tayal } else { 1285754695f9SJeeja KP port->eld.eld_valid = false; 1286754695f9SJeeja KP port->eld.eld_size = 0; 1287f6fa11a3SSandeep Tayal } 1288b8a54545SSubhransu S. Prusty 1289*1f53bcb3SBrent Lu eld_changed = (eld_valid != port->eld.eld_valid); 1290*1f53bcb3SBrent Lu 12913787a398SRakesh Ughreja pcm = hdac_hdmi_get_pcm(hdev, port); 12924a3478deSJeeja KP 1293754695f9SJeeja KP if (!port->eld.monitor_present || !port->eld.eld_valid) { 1294b8a54545SSubhransu S. Prusty 12953787a398SRakesh Ughreja dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n", 1296754695f9SJeeja KP __func__, pin->nid, port->id); 12974a3478deSJeeja KP 12984a3478deSJeeja KP /* 12994a3478deSJeeja KP * PCMs are not registered during device probe, so don't 13004a3478deSJeeja KP * report jack here. It will be done in usermode mux 13014a3478deSJeeja KP * control select. 13024a3478deSJeeja KP */ 1303332ccf00STakashi Iwai if (pcm) { 1304e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, false); 1305332ccf00STakashi Iwai schedule_work(&port->dapm_work); 1306332ccf00STakashi Iwai } 13074a3478deSJeeja KP 13084a3478deSJeeja KP mutex_unlock(&hdmi->pin_mutex); 1309f6fa11a3SSandeep Tayal return; 1310b8a54545SSubhransu S. Prusty } 1311b8a54545SSubhransu S. Prusty 1312754695f9SJeeja KP if (port->eld.monitor_present && port->eld.eld_valid) { 1313332ccf00STakashi Iwai if (pcm) { 1314e0e5d3e5SJeeja KP hdac_hdmi_jack_report(pcm, port, true); 1315332ccf00STakashi Iwai schedule_work(&port->dapm_work); 1316332ccf00STakashi Iwai } 13174a3478deSJeeja KP 1318f6fa11a3SSandeep Tayal print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1, 1319754695f9SJeeja KP port->eld.eld_buffer, port->eld.eld_size, false); 1320754695f9SJeeja KP 1321754695f9SJeeja KP } 1322754695f9SJeeja KP mutex_unlock(&hdmi->pin_mutex); 1323*1f53bcb3SBrent Lu 1324*1f53bcb3SBrent Lu if (eld_changed && pcm) 1325*1f53bcb3SBrent Lu snd_ctl_notify(hdmi->card, 1326*1f53bcb3SBrent Lu SNDRV_CTL_EVENT_MASK_VALUE | 1327*1f53bcb3SBrent Lu SNDRV_CTL_EVENT_MASK_INFO, 1328*1f53bcb3SBrent Lu &pcm->eld_ctl->id); 13294a3478deSJeeja KP } 13304a3478deSJeeja KP 1331c7ba4e53SPierre-Louis Bossart static int hdac_hdmi_add_ports(struct hdac_device *hdev, 1332754695f9SJeeja KP struct hdac_hdmi_pin *pin) 1333754695f9SJeeja KP { 1334754695f9SJeeja KP struct hdac_hdmi_port *ports; 1335754695f9SJeeja KP int max_ports = HDA_MAX_PORTS; 1336754695f9SJeeja KP int i; 1337754695f9SJeeja KP 1338754695f9SJeeja KP /* 1339754695f9SJeeja KP * FIXME: max_port may vary for each platform, so pass this as 1340754695f9SJeeja KP * as driver data or query from i915 interface when this API is 1341754695f9SJeeja KP * implemented. 1342754695f9SJeeja KP */ 1343754695f9SJeeja KP 1344c7ba4e53SPierre-Louis Bossart ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL); 1345754695f9SJeeja KP if (!ports) 1346754695f9SJeeja KP return -ENOMEM; 1347754695f9SJeeja KP 1348754695f9SJeeja KP for (i = 0; i < max_ports; i++) { 1349754695f9SJeeja KP ports[i].id = i; 1350754695f9SJeeja KP ports[i].pin = pin; 1351332ccf00STakashi Iwai INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work); 1352754695f9SJeeja KP } 1353754695f9SJeeja KP pin->ports = ports; 1354754695f9SJeeja KP pin->num_ports = max_ports; 1355754695f9SJeeja KP return 0; 1356b8a54545SSubhransu S. Prusty } 1357b8a54545SSubhransu S. Prusty 13583787a398SRakesh Ughreja static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid) 135915b91447SSubhransu S. Prusty { 13603787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 136115b91447SSubhransu S. Prusty struct hdac_hdmi_pin *pin; 1362754695f9SJeeja KP int ret; 136315b91447SSubhransu S. Prusty 1364c7ba4e53SPierre-Louis Bossart pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL); 136515b91447SSubhransu S. Prusty if (!pin) 136615b91447SSubhransu S. Prusty return -ENOMEM; 136715b91447SSubhransu S. Prusty 136815b91447SSubhransu S. Prusty pin->nid = nid; 13692acd8309SJeeja KP pin->mst_capable = false; 13703787a398SRakesh Ughreja pin->hdev = hdev; 1371c7ba4e53SPierre-Louis Bossart ret = hdac_hdmi_add_ports(hdev, pin); 1372754695f9SJeeja KP if (ret < 0) 1373754695f9SJeeja KP return ret; 137415b91447SSubhransu S. Prusty 137515b91447SSubhransu S. Prusty list_add_tail(&pin->head, &hdmi->pin_list); 137615b91447SSubhransu S. Prusty hdmi->num_pin++; 1377754695f9SJeeja KP hdmi->num_ports += pin->num_ports; 1378b8a54545SSubhransu S. Prusty 137915b91447SSubhransu S. Prusty return 0; 138018382eadSSubhransu S. Prusty } 138118382eadSSubhransu S. Prusty 1382573892b6SKai Vehmanen #define INTEL_VENDOR_NID 0x08 1383573892b6SKai Vehmanen #define INTEL_GLK_VENDOR_NID 0x0b 1384211caab7SSubhransu S. Prusty #define INTEL_GET_VENDOR_VERB 0xf81 1385211caab7SSubhransu S. Prusty #define INTEL_SET_VENDOR_VERB 0x781 1386211caab7SSubhransu S. Prusty #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */ 1387211caab7SSubhransu S. Prusty #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */ 1388211caab7SSubhransu S. Prusty 1389f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev) 1390211caab7SSubhransu S. Prusty { 1391211caab7SSubhransu S. Prusty unsigned int vendor_param; 1392f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 13935622bc95SPradeep Tewani unsigned int vendor_nid = hdmi->drv_data->vendor_nid; 1394211caab7SSubhransu S. Prusty 1395f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1396211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1397211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS) 1398211caab7SSubhransu S. Prusty return; 1399211caab7SSubhransu S. Prusty 1400211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_ALL_PIN_CVTS; 1401f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1402211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1403211caab7SSubhransu S. Prusty if (vendor_param == -1) 1404211caab7SSubhransu S. Prusty return; 1405211caab7SSubhransu S. Prusty } 1406211caab7SSubhransu S. Prusty 1407f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev) 1408211caab7SSubhransu S. Prusty { 1409211caab7SSubhransu S. Prusty unsigned int vendor_param; 1410f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 14115622bc95SPradeep Tewani unsigned int vendor_nid = hdmi->drv_data->vendor_nid; 1412211caab7SSubhransu S. Prusty 1413f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1414211caab7SSubhransu S. Prusty INTEL_GET_VENDOR_VERB, 0); 1415211caab7SSubhransu S. Prusty if (vendor_param == -1 || vendor_param & INTEL_EN_DP12) 1416211caab7SSubhransu S. Prusty return; 1417211caab7SSubhransu S. Prusty 1418211caab7SSubhransu S. Prusty /* enable DP1.2 mode */ 1419211caab7SSubhransu S. Prusty vendor_param |= INTEL_EN_DP12; 1420f0c5ebebSUghreja, Rakesh A vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0, 1421211caab7SSubhransu S. Prusty INTEL_SET_VENDOR_VERB, vendor_param); 1422211caab7SSubhransu S. Prusty if (vendor_param == -1) 1423211caab7SSubhransu S. Prusty return; 1424211caab7SSubhransu S. Prusty 1425211caab7SSubhransu S. Prusty } 1426211caab7SSubhransu S. Prusty 1427*1f53bcb3SBrent Lu static int hdac_hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol, 1428*1f53bcb3SBrent Lu struct snd_ctl_elem_info *uinfo) 1429*1f53bcb3SBrent Lu { 1430*1f53bcb3SBrent Lu struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1431*1f53bcb3SBrent Lu struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 1432*1f53bcb3SBrent Lu struct hdac_hdmi_pcm *pcm; 1433*1f53bcb3SBrent Lu struct hdac_hdmi_port *port; 1434*1f53bcb3SBrent Lu struct hdac_hdmi_eld *eld; 1435*1f53bcb3SBrent Lu 1436*1f53bcb3SBrent Lu uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 1437*1f53bcb3SBrent Lu uinfo->count = 0; 1438*1f53bcb3SBrent Lu 1439*1f53bcb3SBrent Lu pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device); 1440*1f53bcb3SBrent Lu if (!pcm) { 1441*1f53bcb3SBrent Lu dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__, 1442*1f53bcb3SBrent Lu kcontrol->id.device); 1443*1f53bcb3SBrent Lu return 0; 1444*1f53bcb3SBrent Lu } 1445*1f53bcb3SBrent Lu 1446*1f53bcb3SBrent Lu if (list_empty(&pcm->port_list)) { 1447*1f53bcb3SBrent Lu dev_dbg(component->dev, "%s: empty port list, device %d\n", 1448*1f53bcb3SBrent Lu __func__, kcontrol->id.device); 1449*1f53bcb3SBrent Lu return 0; 1450*1f53bcb3SBrent Lu } 1451*1f53bcb3SBrent Lu 1452*1f53bcb3SBrent Lu mutex_lock(&hdmi->pin_mutex); 1453*1f53bcb3SBrent Lu 1454*1f53bcb3SBrent Lu list_for_each_entry(port, &pcm->port_list, head) { 1455*1f53bcb3SBrent Lu eld = &port->eld; 1456*1f53bcb3SBrent Lu 1457*1f53bcb3SBrent Lu if (eld->eld_valid) { 1458*1f53bcb3SBrent Lu uinfo->count = eld->eld_size; 1459*1f53bcb3SBrent Lu break; 1460*1f53bcb3SBrent Lu } 1461*1f53bcb3SBrent Lu } 1462*1f53bcb3SBrent Lu 1463*1f53bcb3SBrent Lu mutex_unlock(&hdmi->pin_mutex); 1464*1f53bcb3SBrent Lu 1465*1f53bcb3SBrent Lu return 0; 1466*1f53bcb3SBrent Lu } 1467*1f53bcb3SBrent Lu 1468*1f53bcb3SBrent Lu static int hdac_hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol, 1469*1f53bcb3SBrent Lu struct snd_ctl_elem_value *ucontrol) 1470*1f53bcb3SBrent Lu { 1471*1f53bcb3SBrent Lu struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); 1472*1f53bcb3SBrent Lu struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 1473*1f53bcb3SBrent Lu struct hdac_hdmi_pcm *pcm; 1474*1f53bcb3SBrent Lu struct hdac_hdmi_port *port; 1475*1f53bcb3SBrent Lu struct hdac_hdmi_eld *eld; 1476*1f53bcb3SBrent Lu 1477*1f53bcb3SBrent Lu memset(ucontrol->value.bytes.data, 0, ARRAY_SIZE(ucontrol->value.bytes.data)); 1478*1f53bcb3SBrent Lu 1479*1f53bcb3SBrent Lu pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device); 1480*1f53bcb3SBrent Lu if (!pcm) { 1481*1f53bcb3SBrent Lu dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__, 1482*1f53bcb3SBrent Lu kcontrol->id.device); 1483*1f53bcb3SBrent Lu return 0; 1484*1f53bcb3SBrent Lu } 1485*1f53bcb3SBrent Lu 1486*1f53bcb3SBrent Lu if (list_empty(&pcm->port_list)) { 1487*1f53bcb3SBrent Lu dev_dbg(component->dev, "%s: empty port list, device %d\n", 1488*1f53bcb3SBrent Lu __func__, kcontrol->id.device); 1489*1f53bcb3SBrent Lu return 0; 1490*1f53bcb3SBrent Lu } 1491*1f53bcb3SBrent Lu 1492*1f53bcb3SBrent Lu mutex_lock(&hdmi->pin_mutex); 1493*1f53bcb3SBrent Lu 1494*1f53bcb3SBrent Lu list_for_each_entry(port, &pcm->port_list, head) { 1495*1f53bcb3SBrent Lu eld = &port->eld; 1496*1f53bcb3SBrent Lu 1497*1f53bcb3SBrent Lu if (!eld->eld_valid) 1498*1f53bcb3SBrent Lu continue; 1499*1f53bcb3SBrent Lu 1500*1f53bcb3SBrent Lu if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) || 1501*1f53bcb3SBrent Lu eld->eld_size > ELD_MAX_SIZE) { 1502*1f53bcb3SBrent Lu mutex_unlock(&hdmi->pin_mutex); 1503*1f53bcb3SBrent Lu 1504*1f53bcb3SBrent Lu dev_err(component->dev, "%s: buffer too small, device %d eld_size %d\n", 1505*1f53bcb3SBrent Lu __func__, kcontrol->id.device, eld->eld_size); 1506*1f53bcb3SBrent Lu snd_BUG(); 1507*1f53bcb3SBrent Lu return -EINVAL; 1508*1f53bcb3SBrent Lu } 1509*1f53bcb3SBrent Lu 1510*1f53bcb3SBrent Lu memcpy(ucontrol->value.bytes.data, eld->eld_buffer, 1511*1f53bcb3SBrent Lu eld->eld_size); 1512*1f53bcb3SBrent Lu break; 1513*1f53bcb3SBrent Lu } 1514*1f53bcb3SBrent Lu 1515*1f53bcb3SBrent Lu mutex_unlock(&hdmi->pin_mutex); 1516*1f53bcb3SBrent Lu 1517*1f53bcb3SBrent Lu return 0; 1518*1f53bcb3SBrent Lu } 1519*1f53bcb3SBrent Lu 1520*1f53bcb3SBrent Lu static int hdac_hdmi_create_eld_ctl(struct snd_soc_component *component, struct hdac_hdmi_pcm *pcm) 1521*1f53bcb3SBrent Lu { 1522*1f53bcb3SBrent Lu struct snd_kcontrol *kctl; 1523*1f53bcb3SBrent Lu struct snd_kcontrol_new hdmi_eld_ctl = { 1524*1f53bcb3SBrent Lu .access = SNDRV_CTL_ELEM_ACCESS_READ | 1525*1f53bcb3SBrent Lu SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1526*1f53bcb3SBrent Lu .iface = SNDRV_CTL_ELEM_IFACE_PCM, 1527*1f53bcb3SBrent Lu .name = "ELD", 1528*1f53bcb3SBrent Lu .info = hdac_hdmi_eld_ctl_info, 1529*1f53bcb3SBrent Lu .get = hdac_hdmi_eld_ctl_get, 1530*1f53bcb3SBrent Lu .device = pcm->pcm_id, 1531*1f53bcb3SBrent Lu }; 1532*1f53bcb3SBrent Lu 1533*1f53bcb3SBrent Lu /* add ELD ctl with the device number corresponding to the PCM stream */ 1534*1f53bcb3SBrent Lu kctl = snd_ctl_new1(&hdmi_eld_ctl, component); 1535*1f53bcb3SBrent Lu if (!kctl) 1536*1f53bcb3SBrent Lu return -ENOMEM; 1537*1f53bcb3SBrent Lu 1538*1f53bcb3SBrent Lu pcm->eld_ctl = kctl; 1539*1f53bcb3SBrent Lu 1540*1f53bcb3SBrent Lu return snd_ctl_add(component->card->snd_card, kctl); 1541*1f53bcb3SBrent Lu } 1542*1f53bcb3SBrent Lu 154361b3b3ccSGustavo A. R. Silva static const struct snd_soc_dai_ops hdmi_dai_ops = { 154417a42c45SSubhransu S. Prusty .startup = hdac_hdmi_pcm_open, 154517a42c45SSubhransu S. Prusty .shutdown = hdac_hdmi_pcm_close, 154617a42c45SSubhransu S. Prusty .hw_params = hdac_hdmi_set_hw_params, 1547c9bfb5d7SJeeja KP .set_tdm_slot = hdac_hdmi_set_tdm_slot, 154817a42c45SSubhransu S. Prusty }; 154917a42c45SSubhransu S. Prusty 155017a42c45SSubhransu S. Prusty /* 155117a42c45SSubhransu S. Prusty * Each converter can support a stream independently. So a dai is created 155217a42c45SSubhransu S. Prusty * based on the number of converter queried. 155317a42c45SSubhransu S. Prusty */ 1554f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_create_dais(struct hdac_device *hdev, 155517a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, 155617a42c45SSubhransu S. Prusty struct hdac_hdmi_priv *hdmi, int num_dais) 155717a42c45SSubhransu S. Prusty { 155817a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais; 155917a42c45SSubhransu S. Prusty struct hdac_hdmi_cvt *cvt; 156017a42c45SSubhransu S. Prusty char name[NAME_SIZE], dai_name[NAME_SIZE]; 156117a42c45SSubhransu S. Prusty int i = 0; 156217a42c45SSubhransu S. Prusty u32 rates, bps; 156317a42c45SSubhransu S. Prusty unsigned int rate_max = 384000, rate_min = 8000; 156417a42c45SSubhransu S. Prusty u64 formats; 156517a42c45SSubhransu S. Prusty int ret; 156617a42c45SSubhransu S. Prusty 1567f0c5ebebSUghreja, Rakesh A hdmi_dais = devm_kzalloc(&hdev->dev, 156817a42c45SSubhransu S. Prusty (sizeof(*hdmi_dais) * num_dais), 156917a42c45SSubhransu S. Prusty GFP_KERNEL); 157017a42c45SSubhransu S. Prusty if (!hdmi_dais) 157117a42c45SSubhransu S. Prusty return -ENOMEM; 157217a42c45SSubhransu S. Prusty 157317a42c45SSubhransu S. Prusty list_for_each_entry(cvt, &hdmi->cvt_list, head) { 1574f0c5ebebSUghreja, Rakesh A ret = snd_hdac_query_supported_pcm(hdev, cvt->nid, 157517a42c45SSubhransu S. Prusty &rates, &formats, &bps); 157617a42c45SSubhransu S. Prusty if (ret) 157717a42c45SSubhransu S. Prusty return ret; 157817a42c45SSubhransu S. Prusty 15793b857472SYong Zhi /* Filter out 44.1, 88.2 and 176.4Khz */ 15803b857472SYong Zhi rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 | 15813b857472SYong Zhi SNDRV_PCM_RATE_176400); 15823b857472SYong Zhi if (!rates) 15833b857472SYong Zhi return -EINVAL; 15843b857472SYong Zhi 158517a42c45SSubhransu S. Prusty sprintf(dai_name, "intel-hdmi-hifi%d", i+1); 1586f0c5ebebSUghreja, Rakesh A hdmi_dais[i].name = devm_kstrdup(&hdev->dev, 158717a42c45SSubhransu S. Prusty dai_name, GFP_KERNEL); 158817a42c45SSubhransu S. Prusty 158917a42c45SSubhransu S. Prusty if (!hdmi_dais[i].name) 159017a42c45SSubhransu S. Prusty return -ENOMEM; 159117a42c45SSubhransu S. Prusty 159217a42c45SSubhransu S. Prusty snprintf(name, sizeof(name), "hifi%d", i+1); 159317a42c45SSubhransu S. Prusty hdmi_dais[i].playback.stream_name = 1594f0c5ebebSUghreja, Rakesh A devm_kstrdup(&hdev->dev, name, GFP_KERNEL); 159517a42c45SSubhransu S. Prusty if (!hdmi_dais[i].playback.stream_name) 159617a42c45SSubhransu S. Prusty return -ENOMEM; 159717a42c45SSubhransu S. Prusty 159817a42c45SSubhransu S. Prusty /* 159917a42c45SSubhransu S. Prusty * Set caps based on capability queried from the converter. 160017a42c45SSubhransu S. Prusty * It will be constrained runtime based on ELD queried. 160117a42c45SSubhransu S. Prusty */ 160217a42c45SSubhransu S. Prusty hdmi_dais[i].playback.formats = formats; 160317a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rates = rates; 160417a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_max = rate_max; 160517a42c45SSubhransu S. Prusty hdmi_dais[i].playback.rate_min = rate_min; 160617a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_min = 2; 160717a42c45SSubhransu S. Prusty hdmi_dais[i].playback.channels_max = 2; 160866d6bbc6SJeeja KP hdmi_dais[i].playback.sig_bits = bps; 160917a42c45SSubhransu S. Prusty hdmi_dais[i].ops = &hdmi_dai_ops; 161017a42c45SSubhransu S. Prusty i++; 161117a42c45SSubhransu S. Prusty } 161217a42c45SSubhransu S. Prusty 161317a42c45SSubhransu S. Prusty *dais = hdmi_dais; 16141e02dac3SKuninori Morimoto hdmi->dai_drv = hdmi_dais; 161517a42c45SSubhransu S. Prusty 161617a42c45SSubhransu S. Prusty return 0; 161717a42c45SSubhransu S. Prusty } 161817a42c45SSubhransu S. Prusty 161918382eadSSubhransu S. Prusty /* 162018382eadSSubhransu S. Prusty * Parse all nodes and store the cvt/pin nids in array 162118382eadSSubhransu S. Prusty * Add one time initialization for pin and cvt widgets 162218382eadSSubhransu S. Prusty */ 16233787a398SRakesh Ughreja static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev, 162417a42c45SSubhransu S. Prusty struct snd_soc_dai_driver **dais, int *num_dais) 162518382eadSSubhransu S. Prusty { 162618382eadSSubhransu S. Prusty hda_nid_t nid; 16273c83ac23SSudip Mukherjee int i, num_nodes; 16283787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 162915b91447SSubhransu S. Prusty int ret; 163018382eadSSubhransu S. Prusty 1631f0c5ebebSUghreja, Rakesh A hdac_hdmi_skl_enable_all_pins(hdev); 1632f0c5ebebSUghreja, Rakesh A hdac_hdmi_skl_enable_dp12(hdev); 1633211caab7SSubhransu S. Prusty 1634f0c5ebebSUghreja, Rakesh A num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid); 1635541140d4SSubhransu S. Prusty if (!nid || num_nodes <= 0) { 1636f0c5ebebSUghreja, Rakesh A dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n"); 163718382eadSSubhransu S. Prusty return -EINVAL; 163818382eadSSubhransu S. Prusty } 163918382eadSSubhransu S. Prusty 164045a6008bSPuneeth Prabhu for (i = 0; i < num_nodes; i++, nid++) { 164118382eadSSubhransu S. Prusty unsigned int caps; 164218382eadSSubhransu S. Prusty unsigned int type; 164318382eadSSubhransu S. Prusty 1644f0c5ebebSUghreja, Rakesh A caps = get_wcaps(hdev, nid); 164518382eadSSubhransu S. Prusty type = get_wcaps_type(caps); 164618382eadSSubhransu S. Prusty 164718382eadSSubhransu S. Prusty if (!(caps & AC_WCAP_DIGITAL)) 164818382eadSSubhransu S. Prusty continue; 164918382eadSSubhransu S. Prusty 165018382eadSSubhransu S. Prusty switch (type) { 165118382eadSSubhransu S. Prusty 165218382eadSSubhransu S. Prusty case AC_WID_AUD_OUT: 16533787a398SRakesh Ughreja ret = hdac_hdmi_add_cvt(hdev, nid); 165415b91447SSubhransu S. Prusty if (ret < 0) 1655c7ba4e53SPierre-Louis Bossart return ret; 165618382eadSSubhransu S. Prusty break; 165718382eadSSubhransu S. Prusty 165818382eadSSubhransu S. Prusty case AC_WID_PIN: 16593787a398SRakesh Ughreja ret = hdac_hdmi_add_pin(hdev, nid); 166015b91447SSubhransu S. Prusty if (ret < 0) 1661c7ba4e53SPierre-Louis Bossart return ret; 166218382eadSSubhransu S. Prusty break; 166318382eadSSubhransu S. Prusty } 166418382eadSSubhransu S. Prusty } 166518382eadSSubhransu S. Prusty 16661c0a7de2SSubhransu S. Prusty if (!hdmi->num_pin || !hdmi->num_cvt) { 16671c0a7de2SSubhransu S. Prusty ret = -EIO; 1668c7ba4e53SPierre-Louis Bossart dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__); 1669c7ba4e53SPierre-Louis Bossart return ret; 16701c0a7de2SSubhransu S. Prusty } 167118382eadSSubhransu S. Prusty 1672f0c5ebebSUghreja, Rakesh A ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt); 167317a42c45SSubhransu S. Prusty if (ret) { 1674f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, "Failed to create dais with err: %d\n", 167517a42c45SSubhransu S. Prusty ret); 1676c7ba4e53SPierre-Louis Bossart return ret; 167717a42c45SSubhransu S. Prusty } 167817a42c45SSubhransu S. Prusty 167917a42c45SSubhransu S. Prusty *num_dais = hdmi->num_cvt; 16803787a398SRakesh Ughreja ret = hdac_hdmi_init_dai_map(hdev); 16811c0a7de2SSubhransu S. Prusty if (ret < 0) 1682c7ba4e53SPierre-Louis Bossart dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n", 1683c7ba4e53SPierre-Louis Bossart ret); 16841c0a7de2SSubhransu S. Prusty return ret; 168518382eadSSubhransu S. Prusty } 168618382eadSSubhransu S. Prusty 1687a57942bfSTakashi Iwai static int hdac_hdmi_pin2port(void *aptr, int pin) 1688a57942bfSTakashi Iwai { 1689a57942bfSTakashi Iwai return pin - 4; /* map NID 0x05 -> port #1 */ 1690a57942bfSTakashi Iwai } 1691a57942bfSTakashi Iwai 1692f9318941SPandiyan, Dhinakaran static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe) 1693b8a54545SSubhransu S. Prusty { 16943787a398SRakesh Ughreja struct hdac_device *hdev = aptr; 16953787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 1696754695f9SJeeja KP struct hdac_hdmi_pin *pin = NULL; 1697754695f9SJeeja KP struct hdac_hdmi_port *hport = NULL; 16983787a398SRakesh Ughreja struct snd_soc_component *component = hdmi->component; 16992acd8309SJeeja KP int i; 1700b8a54545SSubhransu S. Prusty 1701573892b6SKai Vehmanen /* Don't know how this mapping is derived */ 1702573892b6SKai Vehmanen hda_nid_t pin_nid = port + 0x04; 1703b8a54545SSubhransu S. Prusty 17043787a398SRakesh Ughreja dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__, 1705754695f9SJeeja KP pin_nid, pipe); 1706b8a54545SSubhransu S. Prusty 1707b8a54545SSubhransu S. Prusty /* 1708b8a54545SSubhransu S. Prusty * skip notification during system suspend (but not in runtime PM); 1709b8a54545SSubhransu S. Prusty * the state will be updated at resume. Also since the ELD and 1710b8a54545SSubhransu S. Prusty * connection states are updated in anyway at the end of the resume, 1711b8a54545SSubhransu S. Prusty * we can skip it when received during PM process. 1712b8a54545SSubhransu S. Prusty */ 171345101122SKuninori Morimoto if (snd_power_get_state(component->card->snd_card) != 1714b8a54545SSubhransu S. Prusty SNDRV_CTL_POWER_D0) 1715b8a54545SSubhransu S. Prusty return; 1716b8a54545SSubhransu S. Prusty 17173787a398SRakesh Ughreja if (atomic_read(&hdev->in_pm)) 1718b8a54545SSubhransu S. Prusty return; 1719b8a54545SSubhransu S. Prusty 1720b8a54545SSubhransu S. Prusty list_for_each_entry(pin, &hdmi->pin_list, head) { 1721754695f9SJeeja KP if (pin->nid != pin_nid) 1722754695f9SJeeja KP continue; 1723754695f9SJeeja KP 1724754695f9SJeeja KP /* In case of non MST pin, pipe is -1 */ 1725754695f9SJeeja KP if (pipe == -1) { 17262acd8309SJeeja KP pin->mst_capable = false; 1727754695f9SJeeja KP /* if not MST, default is port[0] */ 1728754695f9SJeeja KP hport = &pin->ports[0]; 17292acd8309SJeeja KP } else { 17302acd8309SJeeja KP for (i = 0; i < pin->num_ports; i++) { 17312acd8309SJeeja KP pin->mst_capable = true; 17322acd8309SJeeja KP if (pin->ports[i].id == pipe) { 17332acd8309SJeeja KP hport = &pin->ports[i]; 173404c8f2bfSJeeja KP break; 17352acd8309SJeeja KP } 1736b8a54545SSubhransu S. Prusty } 1737b8a54545SSubhransu S. Prusty } 1738b8a54545SSubhransu S. Prusty 173904c8f2bfSJeeja KP if (hport) 1740754695f9SJeeja KP hdac_hdmi_present_sense(pin, hport); 1741754695f9SJeeja KP } 1742754695f9SJeeja KP 174304c8f2bfSJeeja KP } 174404c8f2bfSJeeja KP 1745ae891abeSTakashi Iwai static struct drm_audio_component_audio_ops aops = { 1746a57942bfSTakashi Iwai .pin2port = hdac_hdmi_pin2port, 1747b8a54545SSubhransu S. Prusty .pin_eld_notify = hdac_hdmi_eld_notify_cb, 1748b8a54545SSubhransu S. Prusty }; 1749b8a54545SSubhransu S. Prusty 17502889099eSSubhransu S. Prusty static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card, 17512889099eSSubhransu S. Prusty int device) 17522889099eSSubhransu S. Prusty { 17532889099eSSubhransu S. Prusty struct snd_soc_pcm_runtime *rtd; 17542889099eSSubhransu S. Prusty 1755bcb1fd1fSKuninori Morimoto for_each_card_rtds(card, rtd) { 17562889099eSSubhransu S. Prusty if (rtd->pcm && (rtd->pcm->device == device)) 17572889099eSSubhransu S. Prusty return rtd->pcm; 17582889099eSSubhransu S. Prusty } 17592889099eSSubhransu S. Prusty 17602889099eSSubhransu S. Prusty return NULL; 17612889099eSSubhransu S. Prusty } 17622889099eSSubhransu S. Prusty 17630324e51bSJeeja KP /* create jack pin kcontrols */ 17640324e51bSJeeja KP static int create_fill_jack_kcontrols(struct snd_soc_card *card, 17653787a398SRakesh Ughreja struct hdac_device *hdev) 17660324e51bSJeeja KP { 17670324e51bSJeeja KP struct hdac_hdmi_pin *pin; 17680324e51bSJeeja KP struct snd_kcontrol_new *kc; 17690324e51bSJeeja KP char kc_name[NAME_SIZE], xname[NAME_SIZE]; 17700324e51bSJeeja KP char *name; 17710324e51bSJeeja KP int i = 0, j; 17723787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 17733787a398SRakesh Ughreja struct snd_soc_component *component = hdmi->component; 17740324e51bSJeeja KP 177545101122SKuninori Morimoto kc = devm_kcalloc(component->dev, hdmi->num_ports, 17760324e51bSJeeja KP sizeof(*kc), GFP_KERNEL); 17770324e51bSJeeja KP 17780324e51bSJeeja KP if (!kc) 17790324e51bSJeeja KP return -ENOMEM; 17800324e51bSJeeja KP 17810324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 17820324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) { 17830324e51bSJeeja KP snprintf(xname, sizeof(xname), "hif%d-%d Jack", 17840324e51bSJeeja KP pin->nid, pin->ports[j].id); 178545101122SKuninori Morimoto name = devm_kstrdup(component->dev, xname, GFP_KERNEL); 17860324e51bSJeeja KP if (!name) 17870324e51bSJeeja KP return -ENOMEM; 17880324e51bSJeeja KP snprintf(kc_name, sizeof(kc_name), "%s Switch", xname); 178945101122SKuninori Morimoto kc[i].name = devm_kstrdup(component->dev, kc_name, 17900324e51bSJeeja KP GFP_KERNEL); 17910324e51bSJeeja KP if (!kc[i].name) 17920324e51bSJeeja KP return -ENOMEM; 17930324e51bSJeeja KP 17940324e51bSJeeja KP kc[i].private_value = (unsigned long)name; 17950324e51bSJeeja KP kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 17960324e51bSJeeja KP kc[i].access = 0; 17970324e51bSJeeja KP kc[i].info = snd_soc_dapm_info_pin_switch; 17980324e51bSJeeja KP kc[i].put = snd_soc_dapm_put_pin_switch; 17990324e51bSJeeja KP kc[i].get = snd_soc_dapm_get_pin_switch; 18000324e51bSJeeja KP i++; 18010324e51bSJeeja KP } 18020324e51bSJeeja KP } 18030324e51bSJeeja KP 18040324e51bSJeeja KP return snd_soc_add_card_controls(card, kc, i); 18050324e51bSJeeja KP } 18060324e51bSJeeja KP 180745101122SKuninori Morimoto int hdac_hdmi_jack_port_init(struct snd_soc_component *component, 18080324e51bSJeeja KP struct snd_soc_dapm_context *dapm) 18090324e51bSJeeja KP { 18103787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 18113787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 18120324e51bSJeeja KP struct hdac_hdmi_pin *pin; 18130324e51bSJeeja KP struct snd_soc_dapm_widget *widgets; 18140324e51bSJeeja KP struct snd_soc_dapm_route *route; 18150324e51bSJeeja KP char w_name[NAME_SIZE]; 18160324e51bSJeeja KP int i = 0, j, ret; 18170324e51bSJeeja KP 18180324e51bSJeeja KP widgets = devm_kcalloc(dapm->dev, hdmi->num_ports, 18190324e51bSJeeja KP sizeof(*widgets), GFP_KERNEL); 18200324e51bSJeeja KP 18210324e51bSJeeja KP if (!widgets) 18220324e51bSJeeja KP return -ENOMEM; 18230324e51bSJeeja KP 18240324e51bSJeeja KP route = devm_kcalloc(dapm->dev, hdmi->num_ports, 18250324e51bSJeeja KP sizeof(*route), GFP_KERNEL); 18260324e51bSJeeja KP if (!route) 18270324e51bSJeeja KP return -ENOMEM; 18280324e51bSJeeja KP 18290324e51bSJeeja KP /* create Jack DAPM widget */ 18300324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 18310324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) { 18320324e51bSJeeja KP snprintf(w_name, sizeof(w_name), "hif%d-%d Jack", 18330324e51bSJeeja KP pin->nid, pin->ports[j].id); 18340324e51bSJeeja KP 18350324e51bSJeeja KP ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i], 18360324e51bSJeeja KP snd_soc_dapm_spk, NULL, 18370324e51bSJeeja KP w_name, NULL, NULL, 0, NULL, 0); 18380324e51bSJeeja KP if (ret < 0) 18390324e51bSJeeja KP return ret; 18400324e51bSJeeja KP 18410324e51bSJeeja KP pin->ports[j].jack_pin = widgets[i].name; 18420324e51bSJeeja KP pin->ports[j].dapm = dapm; 18430324e51bSJeeja KP 18440324e51bSJeeja KP /* add to route from Jack widget to output */ 18450324e51bSJeeja KP hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin, 18460324e51bSJeeja KP NULL, pin->ports[j].output_pin, NULL); 18470324e51bSJeeja KP 18480324e51bSJeeja KP i++; 18490324e51bSJeeja KP } 18500324e51bSJeeja KP } 18510324e51bSJeeja KP 18520324e51bSJeeja KP /* Add Route from Jack widget to the output widget */ 18530324e51bSJeeja KP ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports); 18540324e51bSJeeja KP if (ret < 0) 18550324e51bSJeeja KP return ret; 18560324e51bSJeeja KP 18570324e51bSJeeja KP ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports); 18580324e51bSJeeja KP if (ret < 0) 18590324e51bSJeeja KP return ret; 18600324e51bSJeeja KP 18610324e51bSJeeja KP ret = snd_soc_dapm_new_widgets(dapm->card); 18620324e51bSJeeja KP if (ret < 0) 18630324e51bSJeeja KP return ret; 18640324e51bSJeeja KP 18650324e51bSJeeja KP /* Add Jack Pin switch Kcontrol */ 18663787a398SRakesh Ughreja ret = create_fill_jack_kcontrols(dapm->card, hdev); 18670324e51bSJeeja KP 18680324e51bSJeeja KP if (ret < 0) 18690324e51bSJeeja KP return ret; 18700324e51bSJeeja KP 18710324e51bSJeeja KP /* default set the Jack Pin switch to OFF */ 18720324e51bSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 18730324e51bSJeeja KP for (j = 0; j < pin->num_ports; j++) 18740324e51bSJeeja KP snd_soc_dapm_disable_pin(pin->ports[j].dapm, 18750324e51bSJeeja KP pin->ports[j].jack_pin); 18760324e51bSJeeja KP } 18770324e51bSJeeja KP 18780324e51bSJeeja KP return 0; 18790324e51bSJeeja KP } 18800324e51bSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init); 18810324e51bSJeeja KP 188262490016SJeeja KP int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device, 188362490016SJeeja KP struct snd_soc_jack *jack) 18844a3478deSJeeja KP { 188545101122SKuninori Morimoto struct snd_soc_component *component = dai->component; 18863787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 18873787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 18884a3478deSJeeja KP struct hdac_hdmi_pcm *pcm; 18892889099eSSubhransu S. Prusty struct snd_pcm *snd_pcm; 18902889099eSSubhransu S. Prusty int err; 18914a3478deSJeeja KP 18924a3478deSJeeja KP /* 18934a3478deSJeeja KP * this is a new PCM device, create new pcm and 18944a3478deSJeeja KP * add to the pcm list 18954a3478deSJeeja KP */ 1896c7ba4e53SPierre-Louis Bossart pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL); 18974a3478deSJeeja KP if (!pcm) 18984a3478deSJeeja KP return -ENOMEM; 18994a3478deSJeeja KP pcm->pcm_id = device; 19004a3478deSJeeja KP pcm->cvt = hdmi->dai_map[dai->id].cvt; 1901e0e5d3e5SJeeja KP pcm->jack_event = 0; 190262490016SJeeja KP pcm->jack = jack; 1903ab1eea19SJeeja KP mutex_init(&pcm->lock); 1904e0e5d3e5SJeeja KP INIT_LIST_HEAD(&pcm->port_list); 19052889099eSSubhransu S. Prusty snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device); 19062889099eSSubhransu S. Prusty if (snd_pcm) { 19072889099eSSubhransu S. Prusty err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap); 19082889099eSSubhransu S. Prusty if (err < 0) { 19093787a398SRakesh Ughreja dev_err(&hdev->dev, 19102889099eSSubhransu S. Prusty "chmap control add failed with err: %d for pcm: %d\n", 19112889099eSSubhransu S. Prusty err, device); 19122889099eSSubhransu S. Prusty return err; 19132889099eSSubhransu S. Prusty } 19142889099eSSubhransu S. Prusty } 19152889099eSSubhransu S. Prusty 1916*1f53bcb3SBrent Lu /* add control for ELD Bytes */ 1917*1f53bcb3SBrent Lu err = hdac_hdmi_create_eld_ctl(component, pcm); 1918*1f53bcb3SBrent Lu if (err < 0) { 1919*1f53bcb3SBrent Lu dev_err(&hdev->dev, 1920*1f53bcb3SBrent Lu "eld control add failed with err: %d for pcm: %d\n", 1921*1f53bcb3SBrent Lu err, device); 1922*1f53bcb3SBrent Lu return err; 1923*1f53bcb3SBrent Lu } 1924*1f53bcb3SBrent Lu 19254a3478deSJeeja KP list_add_tail(&pcm->head, &hdmi->pcm_list); 19264a3478deSJeeja KP 192762490016SJeeja KP return 0; 19284a3478deSJeeja KP } 19294a3478deSJeeja KP EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init); 19304a3478deSJeeja KP 19313787a398SRakesh Ughreja static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev, 1932a9ce96bcSJeeja KP struct hdac_hdmi_priv *hdmi, bool detect_pin_caps) 1933a9ce96bcSJeeja KP { 1934a9ce96bcSJeeja KP int i; 1935a9ce96bcSJeeja KP struct hdac_hdmi_pin *pin; 1936a9ce96bcSJeeja KP 1937a9ce96bcSJeeja KP list_for_each_entry(pin, &hdmi->pin_list, head) { 1938a9ce96bcSJeeja KP if (detect_pin_caps) { 1939a9ce96bcSJeeja KP 19403787a398SRakesh Ughreja if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0) 1941a9ce96bcSJeeja KP pin->mst_capable = false; 1942a9ce96bcSJeeja KP else 1943a9ce96bcSJeeja KP pin->mst_capable = true; 1944a9ce96bcSJeeja KP } 1945a9ce96bcSJeeja KP 1946a9ce96bcSJeeja KP for (i = 0; i < pin->num_ports; i++) { 1947a9ce96bcSJeeja KP if (!pin->mst_capable && i > 0) 1948a9ce96bcSJeeja KP continue; 1949a9ce96bcSJeeja KP 1950a9ce96bcSJeeja KP hdac_hdmi_present_sense(pin, &pin->ports[i]); 1951a9ce96bcSJeeja KP } 1952a9ce96bcSJeeja KP } 1953a9ce96bcSJeeja KP } 1954a9ce96bcSJeeja KP 195545101122SKuninori Morimoto static int hdmi_codec_probe(struct snd_soc_component *component) 195618382eadSSubhransu S. Prusty { 19573787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 19583787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 195918382eadSSubhransu S. Prusty struct snd_soc_dapm_context *dapm = 196045101122SKuninori Morimoto snd_soc_component_get_dapm(component); 1961b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 1962a9ce96bcSJeeja KP int ret; 196318382eadSSubhransu S. Prusty 19643787a398SRakesh Ughreja hdmi->component = component; 196518382eadSSubhransu S. Prusty 1966b2047e99SVinod Koul /* 1967b2047e99SVinod Koul * hold the ref while we probe, also no need to drop the ref on 1968b2047e99SVinod Koul * exit, we call pm_runtime_suspend() so that will do for us 1969b2047e99SVinod Koul */ 197076f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 1971500e06b9SVinod Koul if (!hlink) { 19723787a398SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 1973500e06b9SVinod Koul return -EIO; 1974500e06b9SVinod Koul } 1975500e06b9SVinod Koul 197676f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 1977b2047e99SVinod Koul 197879f4e922SSubhransu S. Prusty ret = create_fill_widget_route_map(dapm); 197979f4e922SSubhransu S. Prusty if (ret < 0) 198079f4e922SSubhransu S. Prusty return ret; 198118382eadSSubhransu S. Prusty 19823787a398SRakesh Ughreja aops.audio_ptr = hdev; 1983a57942bfSTakashi Iwai ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops); 1984b8a54545SSubhransu S. Prusty if (ret < 0) { 19853787a398SRakesh Ughreja dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret); 1986b8a54545SSubhransu S. Prusty return ret; 1987b8a54545SSubhransu S. Prusty } 1988b8a54545SSubhransu S. Prusty 19893787a398SRakesh Ughreja hdac_hdmi_present_sense_all_pins(hdev, hdmi, true); 199018382eadSSubhransu S. Prusty /* Imp: Store the card pointer in hda_codec */ 19913787a398SRakesh Ughreja hdmi->card = dapm->card->snd_card; 199218382eadSSubhransu S. Prusty 1993e342ac08SSubhransu S. Prusty /* 199401c83276SLibin Yang * Setup a device_link between card device and HDMI codec device. 199501c83276SLibin Yang * The card device is the consumer and the HDMI codec device is 199601c83276SLibin Yang * the supplier. With this setting, we can make sure that the audio 199701c83276SLibin Yang * domain in display power will be always turned on before operating 199801c83276SLibin Yang * on the HDMI audio codec registers. 199901c83276SLibin Yang * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make 200001c83276SLibin Yang * sure the device link is freed when the machine driver is removed. 200101c83276SLibin Yang */ 200201c83276SLibin Yang device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE | 200301c83276SLibin Yang DL_FLAG_AUTOREMOVE_CONSUMER); 200401c83276SLibin Yang /* 2005e342ac08SSubhransu S. Prusty * hdac_device core already sets the state to active and calls 2006e342ac08SSubhransu S. Prusty * get_noresume. So enable runtime and set the device to suspend. 2007e342ac08SSubhransu S. Prusty */ 20083787a398SRakesh Ughreja pm_runtime_enable(&hdev->dev); 20093787a398SRakesh Ughreja pm_runtime_put(&hdev->dev); 20103787a398SRakesh Ughreja pm_runtime_suspend(&hdev->dev); 2011e342ac08SSubhransu S. Prusty 2012e342ac08SSubhransu S. Prusty return 0; 2013e342ac08SSubhransu S. Prusty } 2014e342ac08SSubhransu S. Prusty 201545101122SKuninori Morimoto static void hdmi_codec_remove(struct snd_soc_component *component) 2016e342ac08SSubhransu S. Prusty { 20173787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component); 20183787a398SRakesh Ughreja struct hdac_device *hdev = hdmi->hdev; 20190f6ff785SAmadeusz Sławiński int ret; 20200f6ff785SAmadeusz Sławiński 20210f6ff785SAmadeusz Sławiński ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL); 20220f6ff785SAmadeusz Sławiński if (ret < 0) 20230f6ff785SAmadeusz Sławiński dev_err(&hdev->dev, "notifier unregister failed: err: %d\n", 20240f6ff785SAmadeusz Sławiński ret); 2025e342ac08SSubhransu S. Prusty 20263787a398SRakesh Ughreja pm_runtime_disable(&hdev->dev); 202718382eadSSubhransu S. Prusty } 202818382eadSSubhransu S. Prusty 2029687ae9e2STakashi Iwai #ifdef CONFIG_PM_SLEEP 2030687ae9e2STakashi Iwai static int hdmi_codec_resume(struct device *dev) 2031571d5078SJeeja KP { 20323787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 20333787a398SRakesh Ughreja struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 2034687ae9e2STakashi Iwai int ret; 20351b377ccdSSubhransu S. Prusty 2036687ae9e2STakashi Iwai ret = pm_runtime_force_resume(dev); 2037687ae9e2STakashi Iwai if (ret < 0) 2038687ae9e2STakashi Iwai return ret; 2039571d5078SJeeja KP /* 2040571d5078SJeeja KP * As the ELD notify callback request is not entertained while the 2041571d5078SJeeja KP * device is in suspend state. Need to manually check detection of 2042a9ce96bcSJeeja KP * all pins here. pin capablity change is not support, so use the 2043a9ce96bcSJeeja KP * already set pin caps. 2044687ae9e2STakashi Iwai * 2045687ae9e2STakashi Iwai * NOTE: this is safe to call even if the codec doesn't actually resume. 2046687ae9e2STakashi Iwai * The pin check involves only with DRM audio component hooks, so it 2047687ae9e2STakashi Iwai * works even if the HD-audio side is still dreaming peacefully. 2048571d5078SJeeja KP */ 20493787a398SRakesh Ughreja hdac_hdmi_present_sense_all_pins(hdev, hdmi, false); 2050687ae9e2STakashi Iwai return 0; 2051571d5078SJeeja KP } 2052571d5078SJeeja KP #else 2053687ae9e2STakashi Iwai #define hdmi_codec_resume NULL 2054571d5078SJeeja KP #endif 2055571d5078SJeeja KP 205645101122SKuninori Morimoto static const struct snd_soc_component_driver hdmi_hda_codec = { 205718382eadSSubhransu S. Prusty .probe = hdmi_codec_probe, 2058e342ac08SSubhransu S. Prusty .remove = hdmi_codec_remove, 205945101122SKuninori Morimoto .use_pmdown_time = 1, 206045101122SKuninori Morimoto .endianness = 1, 206145101122SKuninori Morimoto .non_legacy_dai_naming = 1, 206218382eadSSubhransu S. Prusty }; 206318382eadSSubhransu S. Prusty 2064f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx, 20652889099eSSubhransu S. Prusty unsigned char *chmap) 20662889099eSSubhransu S. Prusty { 2067f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 20682889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 20692889099eSSubhransu S. Prusty 2070ab1eea19SJeeja KP memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap)); 20712889099eSSubhransu S. Prusty } 20722889099eSSubhransu S. Prusty 2073f0c5ebebSUghreja, Rakesh A static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx, 20742889099eSSubhransu S. Prusty unsigned char *chmap, int prepared) 20752889099eSSubhransu S. Prusty { 2076f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 20772889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 2078e0e5d3e5SJeeja KP struct hdac_hdmi_port *port; 2079e0e5d3e5SJeeja KP 2080eb50fa17SSubhransu S. Prusty if (!pcm) 2081eb50fa17SSubhransu S. Prusty return; 2082eb50fa17SSubhransu S. Prusty 2083e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 2084e0e5d3e5SJeeja KP return; 20852889099eSSubhransu S. Prusty 2086ab1eea19SJeeja KP mutex_lock(&pcm->lock); 2087ab1eea19SJeeja KP pcm->chmap_set = true; 2088ab1eea19SJeeja KP memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap)); 2089e0e5d3e5SJeeja KP list_for_each_entry(port, &pcm->port_list, head) 20902889099eSSubhransu S. Prusty if (prepared) 20913787a398SRakesh Ughreja hdac_hdmi_setup_audio_infoframe(hdev, pcm, port); 2092ab1eea19SJeeja KP mutex_unlock(&pcm->lock); 20932889099eSSubhransu S. Prusty } 20942889099eSSubhransu S. Prusty 2095f0c5ebebSUghreja, Rakesh A static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx) 20962889099eSSubhransu S. Prusty { 2097f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 20982889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 20992889099eSSubhransu S. Prusty 2100eb50fa17SSubhransu S. Prusty if (!pcm) 2101eb50fa17SSubhransu S. Prusty return false; 2102eb50fa17SSubhransu S. Prusty 2103e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 2104e0e5d3e5SJeeja KP return false; 2105e0e5d3e5SJeeja KP 2106e0e5d3e5SJeeja KP return true; 21072889099eSSubhransu S. Prusty } 21082889099eSSubhransu S. Prusty 2109f0c5ebebSUghreja, Rakesh A static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx) 21102889099eSSubhransu S. Prusty { 2111f0c5ebebSUghreja, Rakesh A struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 21122889099eSSubhransu S. Prusty struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx); 2113e0e5d3e5SJeeja KP struct hdac_hdmi_port *port; 2114e0e5d3e5SJeeja KP 2115eb50fa17SSubhransu S. Prusty if (!pcm) 2116eb50fa17SSubhransu S. Prusty return 0; 2117eb50fa17SSubhransu S. Prusty 2118e0e5d3e5SJeeja KP if (list_empty(&pcm->port_list)) 2119e0e5d3e5SJeeja KP return 0; 2120e0e5d3e5SJeeja KP 2121e0e5d3e5SJeeja KP port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head); 2122e0e5d3e5SJeeja KP 2123754695f9SJeeja KP if (!port || !port->eld.eld_valid) 21242889099eSSubhransu S. Prusty return 0; 21252889099eSSubhransu S. Prusty 2126754695f9SJeeja KP return port->eld.info.spk_alloc; 21272889099eSSubhransu S. Prusty } 21282889099eSSubhransu S. Prusty 21295622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_glk_drv_data = { 2130573892b6SKai Vehmanen .vendor_nid = INTEL_GLK_VENDOR_NID, 21315622bc95SPradeep Tewani }; 21325622bc95SPradeep Tewani 21335622bc95SPradeep Tewani static struct hdac_hdmi_drv_data intel_drv_data = { 2134573892b6SKai Vehmanen .vendor_nid = INTEL_VENDOR_NID, 21355622bc95SPradeep Tewani }; 21365622bc95SPradeep Tewani 21373787a398SRakesh Ughreja static int hdac_hdmi_dev_probe(struct hdac_device *hdev) 213818382eadSSubhransu S. Prusty { 2139c4aafb33SGuennadi Liakhovetski struct hdac_hdmi_priv *hdmi_priv; 214017a42c45SSubhransu S. Prusty struct snd_soc_dai_driver *hdmi_dais = NULL; 2141c4aafb33SGuennadi Liakhovetski struct hdac_ext_link *hlink; 214217a42c45SSubhransu S. Prusty int num_dais = 0; 2143c4aafb33SGuennadi Liakhovetski int ret; 2144f0c5ebebSUghreja, Rakesh A struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver); 2145f0c5ebebSUghreja, Rakesh A const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv); 214618382eadSSubhransu S. Prusty 2147b2047e99SVinod Koul /* hold the ref while we probe */ 214876f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); 2149500e06b9SVinod Koul if (!hlink) { 21503787a398SRakesh Ughreja dev_err(&hdev->dev, "hdac link not found\n"); 2151500e06b9SVinod Koul return -EIO; 2152500e06b9SVinod Koul } 2153500e06b9SVinod Koul 215476f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(hdev->bus, hlink); 2155b2047e99SVinod Koul 2156f0c5ebebSUghreja, Rakesh A hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL); 215718382eadSSubhransu S. Prusty if (hdmi_priv == NULL) 215818382eadSSubhransu S. Prusty return -ENOMEM; 215918382eadSSubhransu S. Prusty 2160f0c5ebebSUghreja, Rakesh A snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap); 21612889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap; 21622889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap; 21632889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached; 21642889099eSSubhransu S. Prusty hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc; 21653787a398SRakesh Ughreja hdmi_priv->hdev = hdev; 216618382eadSSubhransu S. Prusty 2167eb50fa17SSubhransu S. Prusty if (!hdac_id) 2168eb50fa17SSubhransu S. Prusty return -ENODEV; 2169eb50fa17SSubhransu S. Prusty 21705622bc95SPradeep Tewani if (hdac_id->driver_data) 21715622bc95SPradeep Tewani hdmi_priv->drv_data = 21725622bc95SPradeep Tewani (struct hdac_hdmi_drv_data *)hdac_id->driver_data; 21735622bc95SPradeep Tewani else 21745622bc95SPradeep Tewani hdmi_priv->drv_data = &intel_drv_data; 21755622bc95SPradeep Tewani 21763787a398SRakesh Ughreja dev_set_drvdata(&hdev->dev, hdmi_priv); 217718382eadSSubhransu S. Prusty 217815b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->pin_list); 217915b91447SSubhransu S. Prusty INIT_LIST_HEAD(&hdmi_priv->cvt_list); 21804a3478deSJeeja KP INIT_LIST_HEAD(&hdmi_priv->pcm_list); 21814a3478deSJeeja KP mutex_init(&hdmi_priv->pin_mutex); 218215b91447SSubhransu S. Prusty 2183aeaccef0SRamesh Babu /* 2184aeaccef0SRamesh Babu * Turned off in the runtime_suspend during the first explicit 2185aeaccef0SRamesh Babu * pm_runtime_suspend call. 2186aeaccef0SRamesh Babu */ 21874f799e73STakashi Iwai snd_hdac_display_power(hdev->bus, hdev->addr, true); 21884f799e73STakashi Iwai 21893787a398SRakesh Ughreja ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais); 219017a42c45SSubhransu S. Prusty if (ret < 0) { 2191f0c5ebebSUghreja, Rakesh A dev_err(&hdev->dev, 219217a42c45SSubhransu S. Prusty "Failed in parse and map nid with err: %d\n", ret); 219318382eadSSubhransu S. Prusty return ret; 219417a42c45SSubhransu S. Prusty } 2195774a075aSTakashi Iwai snd_hdac_refresh_widgets(hdev); 219618382eadSSubhransu S. Prusty 219718382eadSSubhransu S. Prusty /* ASoC specific initialization */ 219845101122SKuninori Morimoto ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec, 219917a42c45SSubhransu S. Prusty hdmi_dais, num_dais); 2200b2047e99SVinod Koul 220176f56faeSRakesh Ughreja snd_hdac_ext_bus_link_put(hdev->bus, hlink); 2202b2047e99SVinod Koul 2203b2047e99SVinod Koul return ret; 220418382eadSSubhransu S. Prusty } 220518382eadSSubhransu S. Prusty 2206332ccf00STakashi Iwai static void clear_dapm_works(struct hdac_device *hdev) 2207332ccf00STakashi Iwai { 2208332ccf00STakashi Iwai struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev); 2209332ccf00STakashi Iwai struct hdac_hdmi_pin *pin; 2210332ccf00STakashi Iwai int i; 2211332ccf00STakashi Iwai 2212332ccf00STakashi Iwai list_for_each_entry(pin, &hdmi->pin_list, head) 2213332ccf00STakashi Iwai for (i = 0; i < pin->num_ports; i++) 2214332ccf00STakashi Iwai cancel_work_sync(&pin->ports[i].dapm_work); 2215332ccf00STakashi Iwai } 2216332ccf00STakashi Iwai 22173787a398SRakesh Ughreja static int hdac_hdmi_dev_remove(struct hdac_device *hdev) 221818382eadSSubhransu S. Prusty { 2219332ccf00STakashi Iwai clear_dapm_works(hdev); 222077a49672STakashi Iwai snd_hdac_display_power(hdev->bus, hdev->addr, false); 222177a49672STakashi Iwai 222218382eadSSubhransu S. Prusty return 0; 222318382eadSSubhransu S. Prusty } 222418382eadSSubhransu S. Prusty 2225e342ac08SSubhransu S. Prusty #ifdef CONFIG_PM 2226e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_suspend(struct device *dev) 2227e342ac08SSubhransu S. Prusty { 22283787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 2229f0c5ebebSUghreja, Rakesh A struct hdac_bus *bus = hdev->bus; 2230b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 2231e342ac08SSubhransu S. Prusty 2232e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 2233e342ac08SSubhransu S. Prusty 223407f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 223507f083abSSubhransu S. Prusty if (!bus) 223607f083abSSubhransu S. Prusty return 0; 223707f083abSSubhransu S. Prusty 2238332ccf00STakashi Iwai clear_dapm_works(hdev); 2239332ccf00STakashi Iwai 22401b377ccdSSubhransu S. Prusty /* 22411b377ccdSSubhransu S. Prusty * Power down afg. 22421b377ccdSSubhransu S. Prusty * codec_read is preferred over codec_write to set the power state. 22431b377ccdSSubhransu S. Prusty * This way verb is send to set the power state and response 22441b377ccdSSubhransu S. Prusty * is received. So setting power state is ensured without using loop 22451b377ccdSSubhransu S. Prusty * to read the state. 22461b377ccdSSubhransu S. Prusty */ 2247f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 22481b377ccdSSubhransu S. Prusty AC_PWRST_D3); 224907f083abSSubhransu S. Prusty 225076f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev)); 2251500e06b9SVinod Koul if (!hlink) { 2252500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 2253500e06b9SVinod Koul return -EIO; 2254500e06b9SVinod Koul } 2255500e06b9SVinod Koul 2256e62bccf8SKai Vehmanen snd_hdac_codec_link_down(hdev); 225776f56faeSRakesh Ughreja snd_hdac_ext_bus_link_put(bus, hlink); 2258b2047e99SVinod Koul 22594f799e73STakashi Iwai snd_hdac_display_power(bus, hdev->addr, false); 22604c10473dSPierre-Louis Bossart 22614f799e73STakashi Iwai return 0; 2262e342ac08SSubhransu S. Prusty } 2263e342ac08SSubhransu S. Prusty 2264e342ac08SSubhransu S. Prusty static int hdac_hdmi_runtime_resume(struct device *dev) 2265e342ac08SSubhransu S. Prusty { 22663787a398SRakesh Ughreja struct hdac_device *hdev = dev_to_hdac_dev(dev); 2267f0c5ebebSUghreja, Rakesh A struct hdac_bus *bus = hdev->bus; 2268b2047e99SVinod Koul struct hdac_ext_link *hlink = NULL; 2269e342ac08SSubhransu S. Prusty 2270e342ac08SSubhransu S. Prusty dev_dbg(dev, "Enter: %s\n", __func__); 2271e342ac08SSubhransu S. Prusty 227207f083abSSubhransu S. Prusty /* controller may not have been initialized for the first time */ 227307f083abSSubhransu S. Prusty if (!bus) 227407f083abSSubhransu S. Prusty return 0; 227507f083abSSubhransu S. Prusty 227676f56faeSRakesh Ughreja hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev)); 2277500e06b9SVinod Koul if (!hlink) { 2278500e06b9SVinod Koul dev_err(dev, "hdac link not found\n"); 2279500e06b9SVinod Koul return -EIO; 2280500e06b9SVinod Koul } 2281500e06b9SVinod Koul 228276f56faeSRakesh Ughreja snd_hdac_ext_bus_link_get(bus, hlink); 2283e62bccf8SKai Vehmanen snd_hdac_codec_link_up(hdev); 2284b2047e99SVinod Koul 22854f799e73STakashi Iwai snd_hdac_display_power(bus, hdev->addr, true); 228607f083abSSubhransu S. Prusty 22873787a398SRakesh Ughreja hdac_hdmi_skl_enable_all_pins(hdev); 22883787a398SRakesh Ughreja hdac_hdmi_skl_enable_dp12(hdev); 2289ab85f5b3SSubhransu S. Prusty 2290e342ac08SSubhransu S. Prusty /* Power up afg */ 2291f0c5ebebSUghreja, Rakesh A snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE, 22921b377ccdSSubhransu S. Prusty AC_PWRST_D0); 2293e342ac08SSubhransu S. Prusty 2294e342ac08SSubhransu S. Prusty return 0; 2295e342ac08SSubhransu S. Prusty } 2296e342ac08SSubhransu S. Prusty #else 2297e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_suspend NULL 2298e342ac08SSubhransu S. Prusty #define hdac_hdmi_runtime_resume NULL 2299e342ac08SSubhransu S. Prusty #endif 2300e342ac08SSubhransu S. Prusty 2301e342ac08SSubhransu S. Prusty static const struct dev_pm_ops hdac_hdmi_pm = { 2302e342ac08SSubhransu S. Prusty SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL) 2303687ae9e2STakashi Iwai SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume) 2304e342ac08SSubhransu S. Prusty }; 2305e342ac08SSubhransu S. Prusty 230618382eadSSubhransu S. Prusty static const struct hda_device_id hdmi_list[] = { 230718382eadSSubhransu S. Prusty HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0), 2308e2304803SJeeja KP HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0), 2309cc216887SShreyas NC HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0), 23105fb6e0a1SGuneshwor Singh HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI", 23115fb6e0a1SGuneshwor Singh &intel_glk_drv_data), 23125622bc95SPradeep Tewani HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI", 23135622bc95SPradeep Tewani &intel_glk_drv_data), 231418382eadSSubhransu S. Prusty {} 231518382eadSSubhransu S. Prusty }; 231618382eadSSubhransu S. Prusty 231718382eadSSubhransu S. Prusty MODULE_DEVICE_TABLE(hdaudio, hdmi_list); 231818382eadSSubhransu S. Prusty 2319e1df9317SRakesh Ughreja static struct hdac_driver hdmi_driver = { 232018382eadSSubhransu S. Prusty .driver = { 232118382eadSSubhransu S. Prusty .name = "HDMI HDA Codec", 2322e342ac08SSubhransu S. Prusty .pm = &hdac_hdmi_pm, 232318382eadSSubhransu S. Prusty }, 232418382eadSSubhransu S. Prusty .id_table = hdmi_list, 232518382eadSSubhransu S. Prusty .probe = hdac_hdmi_dev_probe, 232618382eadSSubhransu S. Prusty .remove = hdac_hdmi_dev_remove, 232718382eadSSubhransu S. Prusty }; 232818382eadSSubhransu S. Prusty 232918382eadSSubhransu S. Prusty static int __init hdmi_init(void) 233018382eadSSubhransu S. Prusty { 233118382eadSSubhransu S. Prusty return snd_hda_ext_driver_register(&hdmi_driver); 233218382eadSSubhransu S. Prusty } 233318382eadSSubhransu S. Prusty 233418382eadSSubhransu S. Prusty static void __exit hdmi_exit(void) 233518382eadSSubhransu S. Prusty { 233618382eadSSubhransu S. Prusty snd_hda_ext_driver_unregister(&hdmi_driver); 233718382eadSSubhransu S. Prusty } 233818382eadSSubhransu S. Prusty 233918382eadSSubhransu S. Prusty module_init(hdmi_init); 234018382eadSSubhransu S. Prusty module_exit(hdmi_exit); 234118382eadSSubhransu S. Prusty 234218382eadSSubhransu S. Prusty MODULE_LICENSE("GPL v2"); 234318382eadSSubhransu S. Prusty MODULE_DESCRIPTION("HDMI HD codec"); 234418382eadSSubhransu S. Prusty MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>"); 234518382eadSSubhransu S. Prusty MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>"); 2346