1 // SPDX-License-Identifier: GPL-2.0-only
3 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Samreen Nilofer <samreen.nilofer@intel.com>
7 * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/hdmi.h>
17 #include <drm/drm_edid.h>
18 #include <sound/pcm_params.h>
19 #include <sound/jack.h>
20 #include <sound/soc.h>
21 #include <sound/hdaudio_ext.h>
22 #include <sound/hda_i915.h>
23 #include <sound/pcm_drm_eld.h>
24 #include <sound/hda_chmap.h>
25 #include "../../hda/local.h"
26 #include "hdac_hdmi.h"
30 #define AMP_OUT_MUTE 0xb080
31 #define AMP_OUT_UNMUTE 0xb000
32 #define PIN_OUT (AC_PINCTL_OUT_EN)
34 #define HDA_MAX_CONNECTIONS 32
36 #define HDA_MAX_CVTS 3
37 #define HDA_MAX_PORTS 3
39 #define ELD_MAX_SIZE 256
40 #define ELD_FIXED_BYTES 20
42 #define ELD_VER_CEA_861D 2
43 #define ELD_VER_PARTIAL 31
44 #define ELD_MAX_MNL 16
46 struct hdac_hdmi_cvt_params {
47 unsigned int channels_min;
48 unsigned int channels_max;
54 struct hdac_hdmi_cvt {
55 struct list_head head;
58 struct hdac_hdmi_cvt_params params;
61 /* Currently only spk_alloc, more to be added */
62 struct hdac_hdmi_parsed_eld {
66 struct hdac_hdmi_eld {
70 char eld_buffer[ELD_MAX_SIZE];
71 struct hdac_hdmi_parsed_eld info;
74 struct hdac_hdmi_pin {
75 struct list_head head;
78 struct hdac_hdmi_port *ports;
80 struct hdac_device *hdev;
83 struct hdac_hdmi_port {
84 struct list_head head;
86 struct hdac_hdmi_pin *pin;
88 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
89 struct hdac_hdmi_eld eld;
91 struct snd_soc_dapm_context *dapm;
92 const char *output_pin;
95 struct hdac_hdmi_pcm {
96 struct list_head head;
98 struct list_head port_list;
99 struct hdac_hdmi_cvt *cvt;
100 struct snd_soc_jack *jack;
105 unsigned char chmap[8]; /* ALSA API channel-map */
110 struct hdac_hdmi_dai_port_map {
112 struct hdac_hdmi_port *port;
113 struct hdac_hdmi_cvt *cvt;
117 * pin to port mapping table where the value indicate the pin number and
118 * the index indicate the port number with 1 base.
120 static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
122 struct hdac_hdmi_drv_data {
123 unsigned int vendor_nid;
124 const int *port_map; /* pin to port mapping table */
128 struct hdac_hdmi_priv {
129 struct hdac_device *hdev;
130 struct snd_soc_component *component;
131 struct snd_card *card;
132 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
133 struct list_head pin_list;
134 struct list_head cvt_list;
135 struct list_head pcm_list;
139 struct mutex pin_mutex;
140 struct hdac_chmap chmap;
141 struct hdac_hdmi_drv_data *drv_data;
142 struct snd_soc_dai_driver *dai_drv;
145 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
147 static struct hdac_hdmi_pcm *
148 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
149 struct hdac_hdmi_cvt *cvt)
151 struct hdac_hdmi_pcm *pcm = NULL;
153 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
161 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
162 struct hdac_hdmi_port *port, bool is_connect)
164 struct hdac_device *hdev = port->pin->hdev;
167 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
169 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
173 * Report Jack connect event when a device is connected
174 * for the first time where same PCM is attached to multiple
177 if (pcm->jack_event == 0) {
179 "jack report for pcm=%d\n",
181 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
187 * Report Jack disconnect event when a device is disconnected
188 * is the only last connected device when same PCM is attached
191 if (pcm->jack_event == 1)
192 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
193 if (pcm->jack_event > 0)
197 snd_soc_dapm_sync(port->dapm);
200 /* MST supported verbs */
202 * Get the no devices that can be connected to a port on the Pin widget.
204 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
207 unsigned int type, param;
209 caps = get_wcaps(hdev, nid);
210 type = get_wcaps_type(caps);
212 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
215 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
219 return param & AC_DEV_LIST_LEN_MASK;
223 * Get the port entry select on the pin. Return the port entry
224 * id selected on the pin. Return 0 means the first port entry
225 * is selected or MST is not supported.
227 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
228 struct hdac_hdmi_port *port)
230 return snd_hdac_codec_read(hdev, port->pin->nid,
231 0, AC_VERB_GET_DEVICE_SEL, 0);
235 * Sets the selected port entry for the configuring Pin widget verb.
236 * returns error if port set is not equal to port get otherwise success
238 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
239 struct hdac_hdmi_port *port)
243 if (!port->pin->mst_capable)
246 /* AC_PAR_DEVLIST_LEN is 0 based. */
247 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
251 * Device List Length is a 0 based integer value indicating the
252 * number of sink device that a MST Pin Widget can support.
254 if (num_ports + 1 < port->id)
257 snd_hdac_codec_write(hdev, port->pin->nid, 0,
258 AC_VERB_SET_DEVICE_SEL, port->id);
260 if (port->id != hdac_hdmi_port_select_get(hdev, port))
263 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
268 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
271 struct hdac_hdmi_pcm *pcm;
273 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
274 if (pcm->pcm_id == pcm_idx)
281 static unsigned int sad_format(const u8 *sad)
283 return ((sad[0] >> 0x3) & 0x1f);
286 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
291 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
294 u64 formats = SNDRV_PCM_FMTBIT_S16;
296 const u8 *sad, *eld_buf = eld;
298 sad = drm_eld_sad(eld_buf);
300 goto format_constraint;
302 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
303 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
306 * the controller support 20 and 24 bits in 32 bit
307 * container so we set S32
309 if (sad_sample_bits_lpcm(sad) & 0x6)
310 formats |= SNDRV_PCM_FMTBIT_S32;
315 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
321 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
322 int packet_index, int byte_index)
326 val = (packet_index << 5) | (byte_index & 0x1f);
327 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
330 struct dp_audio_infoframe {
333 u8 ver; /* 0x11 << 2 */
335 u8 CC02_CT47; /* match with HDMI infoframe from this on */
339 u8 LFEPBL01_LSV36_DM_INH7;
342 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
343 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
345 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
346 struct hdmi_audio_infoframe frame;
347 struct hdac_hdmi_pin *pin = port->pin;
348 struct dp_audio_infoframe dp_ai;
349 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
350 struct hdac_hdmi_cvt *cvt = pcm->cvt;
358 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
359 pcm->channels, pcm->chmap_set, true, pcm->chmap);
361 channels = snd_hdac_get_active_channels(ca);
362 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
364 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
365 pcm->channels, pcm->chmap, pcm->chmap_set);
367 eld_buf = port->eld.eld_buffer;
368 conn_type = drm_eld_get_conn_type(eld_buf);
371 case DRM_ELD_CONN_TYPE_HDMI:
372 hdmi_audio_infoframe_init(&frame);
374 frame.channels = channels;
375 frame.channel_allocation = ca;
377 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
383 case DRM_ELD_CONN_TYPE_DP:
384 memset(&dp_ai, 0, sizeof(dp_ai));
387 dp_ai.ver = 0x11 << 2;
388 dp_ai.CC02_CT47 = channels - 1;
395 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
399 /* stop infoframe transmission */
400 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
401 snd_hdac_codec_write(hdev, pin->nid, 0,
402 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
405 /* Fill infoframe. Index auto-incremented */
406 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
407 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
408 for (i = 0; i < sizeof(buffer); i++)
409 snd_hdac_codec_write(hdev, pin->nid, 0,
410 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
412 for (i = 0; i < sizeof(dp_ai); i++)
413 snd_hdac_codec_write(hdev, pin->nid, 0,
414 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
417 /* Start infoframe */
418 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
419 snd_hdac_codec_write(hdev, pin->nid, 0,
420 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
425 static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
426 unsigned int tx_mask, unsigned int rx_mask,
427 int slots, int slot_width)
429 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
430 struct hdac_device *hdev = hdmi->hdev;
431 struct hdac_hdmi_dai_port_map *dai_map;
432 struct hdac_hdmi_pcm *pcm;
434 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
436 dai_map = &hdmi->dai_map[dai->id];
438 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
441 pcm->stream_tag = (tx_mask << 4);
446 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
449 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
450 struct hdac_hdmi_dai_port_map *dai_map;
451 struct hdac_hdmi_pcm *pcm;
454 dai_map = &hdmi->dai_map[dai->id];
456 format = snd_hdac_calc_stream_format(params_rate(hparams),
457 params_channels(hparams), params_format(hparams),
458 dai->driver->playback.sig_bits, 0);
460 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
464 pcm->format = format;
465 pcm->channels = params_channels(hparams);
470 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
471 struct hdac_hdmi_pin *pin,
472 struct hdac_hdmi_port *port)
474 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
476 "HDMI: pin %d wcaps %#x does not support connection list\n",
477 pin->nid, get_wcaps(hdev, pin->nid));
481 if (hdac_hdmi_port_select_set(hdev, port) < 0)
484 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
485 port->mux_nids, HDA_MAX_CONNECTIONS);
486 if (port->num_mux_nids == 0)
488 "No connections found for pin:port %d:%d\n",
491 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
492 port->num_mux_nids, pin->nid, port->id);
494 return port->num_mux_nids;
498 * Query pcm list and return port to which stream is routed.
500 * Also query connection list of the pin, to validate the cvt to port map.
502 * Same stream rendering to multiple ports simultaneously can be done
503 * possibly, but not supported for now in driver. So return the first port
506 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
507 struct hdac_device *hdev,
508 struct hdac_hdmi_priv *hdmi,
509 struct hdac_hdmi_cvt *cvt)
511 struct hdac_hdmi_pcm *pcm;
512 struct hdac_hdmi_port *port = NULL;
515 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
516 if (pcm->cvt == cvt) {
517 if (list_empty(&pcm->port_list))
520 list_for_each_entry(port, &pcm->port_list, head) {
521 mutex_lock(&pcm->lock);
522 ret = hdac_hdmi_query_port_connlist(hdev,
524 mutex_unlock(&pcm->lock);
528 for (i = 0; i < port->num_mux_nids; i++) {
529 if (port->mux_nids[i] == cvt->nid &&
530 port->eld.monitor_present &&
542 * This tries to get a valid pin and set the HW constraints based on the
543 * ELD. Even if a valid pin is not found return success so that device open
546 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
547 struct snd_soc_dai *dai)
549 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
550 struct hdac_device *hdev = hdmi->hdev;
551 struct hdac_hdmi_dai_port_map *dai_map;
552 struct hdac_hdmi_cvt *cvt;
553 struct hdac_hdmi_port *port;
556 dai_map = &hdmi->dai_map[dai->id];
559 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
562 * To make PA and other userland happy.
563 * userland scans devices so returning error does not help.
567 if ((!port->eld.monitor_present) ||
568 (!port->eld.eld_valid)) {
571 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
572 port->eld.monitor_present, port->eld.eld_valid,
573 port->pin->nid, port->id);
578 dai_map->port = port;
580 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
581 port->eld.eld_buffer);
585 return snd_pcm_hw_constraint_eld(substream->runtime,
586 port->eld.eld_buffer);
589 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
590 struct snd_soc_dai *dai)
592 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
593 struct hdac_hdmi_dai_port_map *dai_map;
594 struct hdac_hdmi_pcm *pcm;
596 dai_map = &hdmi->dai_map[dai->id];
598 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
601 mutex_lock(&pcm->lock);
602 pcm->chmap_set = false;
603 memset(pcm->chmap, 0, sizeof(pcm->chmap));
605 mutex_unlock(&pcm->lock);
609 dai_map->port = NULL;
613 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
616 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
619 chans = get_wcaps(hdev, cvt->nid);
620 chans = get_wcaps_channels(chans);
622 cvt->params.channels_min = 2;
624 cvt->params.channels_max = chans;
625 if (chans > hdmi->chmap.channels_max)
626 hdmi->chmap.channels_max = chans;
628 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
630 &cvt->params.formats,
631 &cvt->params.maxbps);
634 "Failed to query pcm params for nid %d: %d\n",
640 static int hdac_hdmi_fill_widget_info(struct device *dev,
641 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
642 void *priv, const char *wname, const char *stream,
643 struct snd_kcontrol_new *wc, int numkc,
644 int (*event)(struct snd_soc_dapm_widget *,
645 struct snd_kcontrol *, int), unsigned short event_flags)
648 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
653 w->reg = SND_SOC_NOPM;
655 w->kcontrol_news = wc;
656 w->num_kcontrols = numkc;
659 w->event_flags = event_flags;
664 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
665 const char *sink, const char *control, const char *src,
666 int (*handler)(struct snd_soc_dapm_widget *src,
667 struct snd_soc_dapm_widget *sink))
671 route->control = control;
672 route->connected = handler;
675 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
676 struct hdac_hdmi_port *port)
678 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
679 struct hdac_hdmi_pcm *pcm = NULL;
680 struct hdac_hdmi_port *p;
682 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
683 if (list_empty(&pcm->port_list))
686 list_for_each_entry(p, &pcm->port_list, head) {
687 if (p->id == port->id && port->pin == p->pin)
695 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
696 hda_nid_t nid, unsigned int pwr_state)
701 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
702 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
703 for (count = 0; count < 10; count++) {
704 snd_hdac_codec_read(hdev, nid, 0,
705 AC_VERB_SET_POWER_STATE,
707 state = snd_hdac_sync_power_state(hdev,
709 if (!(state & AC_PWRST_ERROR))
716 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
717 hda_nid_t nid, int val)
719 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
720 snd_hdac_codec_write(hdev, nid, 0,
721 AC_VERB_SET_AMP_GAIN_MUTE, val);
725 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
726 struct snd_kcontrol *kc, int event)
728 struct hdac_hdmi_port *port = w->priv;
729 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
730 struct hdac_hdmi_pcm *pcm;
732 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
733 __func__, w->name, event);
735 pcm = hdac_hdmi_get_pcm(hdev, port);
739 /* set the device if pin is mst_capable */
740 if (hdac_hdmi_port_select_set(hdev, port) < 0)
744 case SND_SOC_DAPM_PRE_PMU:
745 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
747 /* Enable out path for this pin widget */
748 snd_hdac_codec_write(hdev, port->pin->nid, 0,
749 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
751 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
753 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
755 case SND_SOC_DAPM_POST_PMD:
756 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
758 /* Disable out path for this pin widget */
759 snd_hdac_codec_write(hdev, port->pin->nid, 0,
760 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
762 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
770 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
771 struct snd_kcontrol *kc, int event)
773 struct hdac_hdmi_cvt *cvt = w->priv;
774 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
775 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
776 struct hdac_hdmi_pcm *pcm;
778 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
779 __func__, w->name, event);
781 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
786 case SND_SOC_DAPM_PRE_PMU:
787 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
789 /* Enable transmission */
790 snd_hdac_codec_write(hdev, cvt->nid, 0,
791 AC_VERB_SET_DIGI_CONVERT_1, 1);
793 /* Category Code (CC) to zero */
794 snd_hdac_codec_write(hdev, cvt->nid, 0,
795 AC_VERB_SET_DIGI_CONVERT_2, 0);
797 snd_hdac_codec_write(hdev, cvt->nid, 0,
798 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
799 snd_hdac_codec_write(hdev, cvt->nid, 0,
800 AC_VERB_SET_STREAM_FORMAT, pcm->format);
803 case SND_SOC_DAPM_POST_PMD:
804 snd_hdac_codec_write(hdev, cvt->nid, 0,
805 AC_VERB_SET_CHANNEL_STREAMID, 0);
806 snd_hdac_codec_write(hdev, cvt->nid, 0,
807 AC_VERB_SET_STREAM_FORMAT, 0);
809 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
817 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
818 struct snd_kcontrol *kc, int event)
820 struct hdac_hdmi_port *port = w->priv;
821 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
824 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
825 __func__, w->name, event);
828 kc = w->kcontrols[0];
830 mux_idx = dapm_kcontrol_get_value(kc);
832 /* set the device if pin is mst_capable */
833 if (hdac_hdmi_port_select_set(hdev, port) < 0)
837 snd_hdac_codec_write(hdev, port->pin->nid, 0,
838 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
845 * Based on user selection, map the PINs with the PCMs.
847 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
848 struct snd_ctl_elem_value *ucontrol)
851 struct hdac_hdmi_port *p, *p_next;
852 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
853 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
854 struct snd_soc_dapm_context *dapm = w->dapm;
855 struct hdac_hdmi_port *port = w->priv;
856 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
857 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
858 struct hdac_hdmi_pcm *pcm = NULL;
859 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
861 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
868 mutex_lock(&hdmi->pin_mutex);
869 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
870 if (list_empty(&pcm->port_list))
873 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
874 if (p == port && p->id == port->id &&
875 p->pin == port->pin) {
876 hdac_hdmi_jack_report(pcm, port, false);
883 * Jack status is not reported during device probe as the
884 * PCMs are not registered by then. So report it here.
886 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
887 if (!strcmp(cvt_name, pcm->cvt->name)) {
888 list_add_tail(&port->head, &pcm->port_list);
889 if (port->eld.monitor_present && port->eld.eld_valid) {
890 hdac_hdmi_jack_report(pcm, port, true);
891 mutex_unlock(&hdmi->pin_mutex);
896 mutex_unlock(&hdmi->pin_mutex);
902 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
903 * the display driver seem to be programming the connection list for the pin
906 * So programming all the possible inputs for the mux, the user has to take
907 * care of selecting the right one and leaving all other inputs selected to
910 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
911 struct hdac_hdmi_port *port,
912 struct snd_soc_dapm_widget *widget,
913 const char *widget_name)
915 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
916 struct hdac_hdmi_pin *pin = port->pin;
917 struct snd_kcontrol_new *kc;
918 struct hdac_hdmi_cvt *cvt;
920 char kc_name[NAME_SIZE];
921 char mux_items[NAME_SIZE];
922 /* To hold inputs to the Pin mux */
923 char *items[HDA_MAX_CONNECTIONS];
925 int num_items = hdmi->num_cvt + 1;
927 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
931 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
935 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
937 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
941 kc->private_value = (long)se;
942 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
944 kc->info = snd_soc_info_enum_double;
945 kc->put = hdac_hdmi_set_pin_port_mux;
946 kc->get = snd_soc_dapm_get_enum_double;
948 se->reg = SND_SOC_NOPM;
950 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
951 se->items = num_items;
952 se->mask = roundup_pow_of_two(se->items) - 1;
954 sprintf(mux_items, "NONE");
955 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
959 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
961 sprintf(mux_items, "cvt %d", cvt->nid);
962 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
967 se->texts = devm_kmemdup(&hdev->dev, items,
968 (num_items * sizeof(char *)), GFP_KERNEL);
972 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
973 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
974 hdac_hdmi_pin_mux_widget_event,
975 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
978 /* Add cvt <- input <- mux route map */
979 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
980 struct snd_soc_dapm_widget *widgets,
981 struct snd_soc_dapm_route *route, int rindex)
983 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
984 const struct snd_kcontrol_new *kc;
986 int mux_index = hdmi->num_cvt + hdmi->num_ports;
989 for (i = 0; i < hdmi->num_ports; i++) {
990 kc = widgets[mux_index].kcontrol_news;
991 se = (struct soc_enum *)kc->private_value;
992 for (j = 0; j < hdmi->num_cvt; j++) {
993 hdac_hdmi_fill_route(&route[rindex],
994 widgets[mux_index].name,
996 widgets[j].name, NULL);
1006 * Widgets are added in the below sequence
1007 * Converter widgets for num converters enumerated
1008 * Pin-port widgets for num ports for Pins enumerated
1009 * Pin-port mux widgets to represent connenction list of pin widget
1011 * For each port, one Mux and One output widget is added
1012 * Total widgets elements = num_cvt + (num_ports * 2);
1014 * Routes are added as below:
1015 * pin-port mux -> pin (based on num_ports)
1016 * cvt -> "Input sel control" -> pin-port_mux
1018 * Total route elements:
1019 * num_ports + (pin_muxes * num_cvt)
1021 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1023 struct snd_soc_dapm_widget *widgets;
1024 struct snd_soc_dapm_route *route;
1025 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1026 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1027 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1028 char widget_name[NAME_SIZE];
1029 struct hdac_hdmi_cvt *cvt;
1030 struct hdac_hdmi_pin *pin;
1031 int ret, i = 0, num_routes = 0, j;
1033 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1036 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1037 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1043 /* DAPM widgets to represent each converter widget */
1044 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1045 sprintf(widget_name, "Converter %d", cvt->nid);
1046 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1047 snd_soc_dapm_aif_in, cvt,
1048 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1049 hdac_hdmi_cvt_output_widget_event,
1050 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1056 list_for_each_entry(pin, &hdmi->pin_list, head) {
1057 for (j = 0; j < pin->num_ports; j++) {
1058 sprintf(widget_name, "hif%d-%d Output",
1059 pin->nid, pin->ports[j].id);
1060 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1061 snd_soc_dapm_output, &pin->ports[j],
1062 widget_name, NULL, NULL, 0,
1063 hdac_hdmi_pin_output_widget_event,
1064 SND_SOC_DAPM_PRE_PMU |
1065 SND_SOC_DAPM_POST_PMD);
1068 pin->ports[j].output_pin = widgets[i].name;
1073 /* DAPM widgets to represent the connection list to pin widget */
1074 list_for_each_entry(pin, &hdmi->pin_list, head) {
1075 for (j = 0; j < pin->num_ports; j++) {
1076 sprintf(widget_name, "Pin%d-Port%d Mux",
1077 pin->nid, pin->ports[j].id);
1078 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1079 &pin->ports[j], &widgets[i],
1085 /* For cvt to pin_mux mapping */
1086 num_routes += hdmi->num_cvt;
1088 /* For pin_mux to pin mapping */
1093 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1099 /* Add pin <- NULL <- mux route map */
1100 list_for_each_entry(pin, &hdmi->pin_list, head) {
1101 for (j = 0; j < pin->num_ports; j++) {
1102 int sink_index = i + hdmi->num_cvt;
1103 int src_index = sink_index + pin->num_ports *
1106 hdac_hdmi_fill_route(&route[i],
1107 widgets[sink_index].name, NULL,
1108 widgets[src_index].name, NULL);
1113 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1115 snd_soc_dapm_new_controls(dapm, widgets,
1116 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1118 snd_soc_dapm_add_routes(dapm, route, num_routes);
1119 snd_soc_dapm_new_widgets(dapm->card);
1125 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1127 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1128 struct hdac_hdmi_dai_port_map *dai_map;
1129 struct hdac_hdmi_cvt *cvt;
1132 if (list_empty(&hdmi->cvt_list))
1135 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1136 dai_map = &hdmi->dai_map[dai_id];
1137 dai_map->dai_id = dai_id;
1142 if (dai_id == HDA_MAX_CVTS) {
1143 dev_warn(&hdev->dev,
1144 "Max dais supported: %d\n", dai_id);
1152 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1154 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1155 struct hdac_hdmi_cvt *cvt;
1156 char name[NAME_SIZE];
1158 cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1163 sprintf(name, "cvt %d", cvt->nid);
1164 cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1168 list_add_tail(&cvt->head, &hdmi->cvt_list);
1171 return hdac_hdmi_query_cvt_params(hdev, cvt);
1174 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1175 struct hdac_hdmi_port *port)
1177 unsigned int ver, mnl;
1179 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1180 >> DRM_ELD_VER_SHIFT;
1182 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1183 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1187 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1188 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1190 if (mnl > ELD_MAX_MNL) {
1191 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1195 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1200 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1201 struct hdac_hdmi_port *port)
1203 struct hdac_device *hdev = pin->hdev;
1204 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1205 struct hdac_hdmi_pcm *pcm;
1213 * In case of non MST pin, get_eld info API expectes port
1216 mutex_lock(&hdmi->pin_mutex);
1217 port->eld.monitor_present = false;
1219 if (pin->mst_capable)
1222 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1223 &port->eld.monitor_present,
1224 port->eld.eld_buffer,
1228 size = min(size, ELD_MAX_SIZE);
1229 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1234 port->eld.eld_valid = true;
1235 port->eld.eld_size = size;
1237 port->eld.eld_valid = false;
1238 port->eld.eld_size = 0;
1241 pcm = hdac_hdmi_get_pcm(hdev, port);
1243 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1245 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1246 __func__, pin->nid, port->id);
1249 * PCMs are not registered during device probe, so don't
1250 * report jack here. It will be done in usermode mux
1254 hdac_hdmi_jack_report(pcm, port, false);
1256 mutex_unlock(&hdmi->pin_mutex);
1260 if (port->eld.monitor_present && port->eld.eld_valid) {
1262 hdac_hdmi_jack_report(pcm, port, true);
1264 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1265 port->eld.eld_buffer, port->eld.eld_size, false);
1268 mutex_unlock(&hdmi->pin_mutex);
1271 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1272 struct hdac_hdmi_pin *pin)
1274 struct hdac_hdmi_port *ports;
1275 int max_ports = HDA_MAX_PORTS;
1279 * FIXME: max_port may vary for each platform, so pass this as
1280 * as driver data or query from i915 interface when this API is
1284 ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1288 for (i = 0; i < max_ports; i++) {
1293 pin->num_ports = max_ports;
1297 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1299 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1300 struct hdac_hdmi_pin *pin;
1303 pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1308 pin->mst_capable = false;
1310 ret = hdac_hdmi_add_ports(hdev, pin);
1314 list_add_tail(&pin->head, &hdmi->pin_list);
1316 hdmi->num_ports += pin->num_ports;
1321 #define INTEL_VENDOR_NID_0x2 0x02
1322 #define INTEL_VENDOR_NID_0x8 0x08
1323 #define INTEL_VENDOR_NID_0xb 0x0b
1324 #define INTEL_GET_VENDOR_VERB 0xf81
1325 #define INTEL_SET_VENDOR_VERB 0x781
1326 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1327 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1329 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1331 unsigned int vendor_param;
1332 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1333 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1335 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1336 INTEL_GET_VENDOR_VERB, 0);
1337 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1340 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1341 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1342 INTEL_SET_VENDOR_VERB, vendor_param);
1343 if (vendor_param == -1)
1347 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1349 unsigned int vendor_param;
1350 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1351 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1353 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1354 INTEL_GET_VENDOR_VERB, 0);
1355 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1358 /* enable DP1.2 mode */
1359 vendor_param |= INTEL_EN_DP12;
1360 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1361 INTEL_SET_VENDOR_VERB, vendor_param);
1362 if (vendor_param == -1)
1367 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1368 .startup = hdac_hdmi_pcm_open,
1369 .shutdown = hdac_hdmi_pcm_close,
1370 .hw_params = hdac_hdmi_set_hw_params,
1371 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
1375 * Each converter can support a stream independently. So a dai is created
1376 * based on the number of converter queried.
1378 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1379 struct snd_soc_dai_driver **dais,
1380 struct hdac_hdmi_priv *hdmi, int num_dais)
1382 struct snd_soc_dai_driver *hdmi_dais;
1383 struct hdac_hdmi_cvt *cvt;
1384 char name[NAME_SIZE], dai_name[NAME_SIZE];
1387 unsigned int rate_max = 384000, rate_min = 8000;
1391 hdmi_dais = devm_kzalloc(&hdev->dev,
1392 (sizeof(*hdmi_dais) * num_dais),
1397 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1398 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1399 &rates, &formats, &bps);
1403 /* Filter out 44.1, 88.2 and 176.4Khz */
1404 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1405 SNDRV_PCM_RATE_176400);
1409 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1410 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1411 dai_name, GFP_KERNEL);
1413 if (!hdmi_dais[i].name)
1416 snprintf(name, sizeof(name), "hifi%d", i+1);
1417 hdmi_dais[i].playback.stream_name =
1418 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1419 if (!hdmi_dais[i].playback.stream_name)
1423 * Set caps based on capability queried from the converter.
1424 * It will be constrained runtime based on ELD queried.
1426 hdmi_dais[i].playback.formats = formats;
1427 hdmi_dais[i].playback.rates = rates;
1428 hdmi_dais[i].playback.rate_max = rate_max;
1429 hdmi_dais[i].playback.rate_min = rate_min;
1430 hdmi_dais[i].playback.channels_min = 2;
1431 hdmi_dais[i].playback.channels_max = 2;
1432 hdmi_dais[i].playback.sig_bits = bps;
1433 hdmi_dais[i].ops = &hdmi_dai_ops;
1438 hdmi->dai_drv = hdmi_dais;
1444 * Parse all nodes and store the cvt/pin nids in array
1445 * Add one time initialization for pin and cvt widgets
1447 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1448 struct snd_soc_dai_driver **dais, int *num_dais)
1452 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1455 hdac_hdmi_skl_enable_all_pins(hdev);
1456 hdac_hdmi_skl_enable_dp12(hdev);
1458 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1459 if (!nid || num_nodes <= 0) {
1460 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1464 for (i = 0; i < num_nodes; i++, nid++) {
1468 caps = get_wcaps(hdev, nid);
1469 type = get_wcaps_type(caps);
1471 if (!(caps & AC_WCAP_DIGITAL))
1476 case AC_WID_AUD_OUT:
1477 ret = hdac_hdmi_add_cvt(hdev, nid);
1483 ret = hdac_hdmi_add_pin(hdev, nid);
1490 if (!hdmi->num_pin || !hdmi->num_cvt) {
1492 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1496 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1498 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1503 *num_dais = hdmi->num_cvt;
1504 ret = hdac_hdmi_init_dai_map(hdev);
1506 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1511 static int hdac_hdmi_pin2port(void *aptr, int pin)
1513 struct hdac_device *hdev = aptr;
1514 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1515 const int *map = hdmi->drv_data->port_map;
1518 if (!hdmi->drv_data->port_num)
1519 return pin - 4; /* map NID 0x05 -> port #1 */
1522 * looking for the pin number in the mapping table and return
1523 * the index which indicate the port number
1525 for (i = 0; i < hdmi->drv_data->port_num; i++) {
1530 /* return -1 if pin number exceeds our expectation */
1531 dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1535 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1537 struct hdac_device *hdev = aptr;
1538 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1539 struct hdac_hdmi_pin *pin = NULL;
1540 struct hdac_hdmi_port *hport = NULL;
1541 struct snd_soc_component *component = hdmi->component;
1545 if (!hdmi->drv_data->port_num) {
1546 /* for legacy platforms */
1547 pin_nid = port + 0x04;
1548 } else if (port < hdmi->drv_data->port_num) {
1549 /* get pin number from the pin2port mapping table */
1550 pin_nid = hdmi->drv_data->port_map[port - 1];
1552 dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1556 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1560 * skip notification during system suspend (but not in runtime PM);
1561 * the state will be updated at resume. Also since the ELD and
1562 * connection states are updated in anyway at the end of the resume,
1563 * we can skip it when received during PM process.
1565 if (snd_power_get_state(component->card->snd_card) !=
1569 if (atomic_read(&hdev->in_pm))
1572 list_for_each_entry(pin, &hdmi->pin_list, head) {
1573 if (pin->nid != pin_nid)
1576 /* In case of non MST pin, pipe is -1 */
1578 pin->mst_capable = false;
1579 /* if not MST, default is port[0] */
1580 hport = &pin->ports[0];
1582 for (i = 0; i < pin->num_ports; i++) {
1583 pin->mst_capable = true;
1584 if (pin->ports[i].id == pipe) {
1585 hport = &pin->ports[i];
1592 hdac_hdmi_present_sense(pin, hport);
1597 static struct drm_audio_component_audio_ops aops = {
1598 .pin2port = hdac_hdmi_pin2port,
1599 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1602 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1605 struct snd_soc_pcm_runtime *rtd;
1607 for_each_card_rtds(card, rtd) {
1608 if (rtd->pcm && (rtd->pcm->device == device))
1615 /* create jack pin kcontrols */
1616 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1617 struct hdac_device *hdev)
1619 struct hdac_hdmi_pin *pin;
1620 struct snd_kcontrol_new *kc;
1621 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1624 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1625 struct snd_soc_component *component = hdmi->component;
1627 kc = devm_kcalloc(component->dev, hdmi->num_ports,
1628 sizeof(*kc), GFP_KERNEL);
1633 list_for_each_entry(pin, &hdmi->pin_list, head) {
1634 for (j = 0; j < pin->num_ports; j++) {
1635 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1636 pin->nid, pin->ports[j].id);
1637 name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1640 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1641 kc[i].name = devm_kstrdup(component->dev, kc_name,
1646 kc[i].private_value = (unsigned long)name;
1647 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1649 kc[i].info = snd_soc_dapm_info_pin_switch;
1650 kc[i].put = snd_soc_dapm_put_pin_switch;
1651 kc[i].get = snd_soc_dapm_get_pin_switch;
1656 return snd_soc_add_card_controls(card, kc, i);
1659 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1660 struct snd_soc_dapm_context *dapm)
1662 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1663 struct hdac_device *hdev = hdmi->hdev;
1664 struct hdac_hdmi_pin *pin;
1665 struct snd_soc_dapm_widget *widgets;
1666 struct snd_soc_dapm_route *route;
1667 char w_name[NAME_SIZE];
1670 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1671 sizeof(*widgets), GFP_KERNEL);
1676 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1677 sizeof(*route), GFP_KERNEL);
1681 /* create Jack DAPM widget */
1682 list_for_each_entry(pin, &hdmi->pin_list, head) {
1683 for (j = 0; j < pin->num_ports; j++) {
1684 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1685 pin->nid, pin->ports[j].id);
1687 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1688 snd_soc_dapm_spk, NULL,
1689 w_name, NULL, NULL, 0, NULL, 0);
1693 pin->ports[j].jack_pin = widgets[i].name;
1694 pin->ports[j].dapm = dapm;
1696 /* add to route from Jack widget to output */
1697 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1698 NULL, pin->ports[j].output_pin, NULL);
1704 /* Add Route from Jack widget to the output widget */
1705 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1709 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1713 ret = snd_soc_dapm_new_widgets(dapm->card);
1717 /* Add Jack Pin switch Kcontrol */
1718 ret = create_fill_jack_kcontrols(dapm->card, hdev);
1723 /* default set the Jack Pin switch to OFF */
1724 list_for_each_entry(pin, &hdmi->pin_list, head) {
1725 for (j = 0; j < pin->num_ports; j++)
1726 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1727 pin->ports[j].jack_pin);
1732 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1734 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1735 struct snd_soc_jack *jack)
1737 struct snd_soc_component *component = dai->component;
1738 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1739 struct hdac_device *hdev = hdmi->hdev;
1740 struct hdac_hdmi_pcm *pcm;
1741 struct snd_pcm *snd_pcm;
1745 * this is a new PCM device, create new pcm and
1746 * add to the pcm list
1748 pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1751 pcm->pcm_id = device;
1752 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1753 pcm->jack_event = 0;
1755 mutex_init(&pcm->lock);
1756 INIT_LIST_HEAD(&pcm->port_list);
1757 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1759 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1762 "chmap control add failed with err: %d for pcm: %d\n",
1768 list_add_tail(&pcm->head, &hdmi->pcm_list);
1772 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1774 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1775 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1778 struct hdac_hdmi_pin *pin;
1780 list_for_each_entry(pin, &hdmi->pin_list, head) {
1781 if (detect_pin_caps) {
1783 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1784 pin->mst_capable = false;
1786 pin->mst_capable = true;
1789 for (i = 0; i < pin->num_ports; i++) {
1790 if (!pin->mst_capable && i > 0)
1793 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1798 static int hdmi_codec_probe(struct snd_soc_component *component)
1800 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1801 struct hdac_device *hdev = hdmi->hdev;
1802 struct snd_soc_dapm_context *dapm =
1803 snd_soc_component_get_dapm(component);
1804 struct hdac_ext_link *hlink = NULL;
1807 hdmi->component = component;
1810 * hold the ref while we probe, also no need to drop the ref on
1811 * exit, we call pm_runtime_suspend() so that will do for us
1813 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1815 dev_err(&hdev->dev, "hdac link not found\n");
1819 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1821 ret = create_fill_widget_route_map(dapm);
1825 aops.audio_ptr = hdev;
1826 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1828 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1832 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1833 /* Imp: Store the card pointer in hda_codec */
1834 hdmi->card = dapm->card->snd_card;
1837 * Setup a device_link between card device and HDMI codec device.
1838 * The card device is the consumer and the HDMI codec device is
1839 * the supplier. With this setting, we can make sure that the audio
1840 * domain in display power will be always turned on before operating
1841 * on the HDMI audio codec registers.
1842 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1843 * sure the device link is freed when the machine driver is removed.
1845 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1846 DL_FLAG_AUTOREMOVE_CONSUMER);
1848 * hdac_device core already sets the state to active and calls
1849 * get_noresume. So enable runtime and set the device to suspend.
1851 pm_runtime_enable(&hdev->dev);
1852 pm_runtime_put(&hdev->dev);
1853 pm_runtime_suspend(&hdev->dev);
1858 static void hdmi_codec_remove(struct snd_soc_component *component)
1860 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1861 struct hdac_device *hdev = hdmi->hdev;
1863 pm_runtime_disable(&hdev->dev);
1866 #ifdef CONFIG_PM_SLEEP
1867 static int hdmi_codec_resume(struct device *dev)
1869 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1870 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1873 ret = pm_runtime_force_resume(dev);
1877 * As the ELD notify callback request is not entertained while the
1878 * device is in suspend state. Need to manually check detection of
1879 * all pins here. pin capablity change is not support, so use the
1880 * already set pin caps.
1882 * NOTE: this is safe to call even if the codec doesn't actually resume.
1883 * The pin check involves only with DRM audio component hooks, so it
1884 * works even if the HD-audio side is still dreaming peacefully.
1886 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1890 #define hdmi_codec_resume NULL
1893 static const struct snd_soc_component_driver hdmi_hda_codec = {
1894 .probe = hdmi_codec_probe,
1895 .remove = hdmi_codec_remove,
1896 .use_pmdown_time = 1,
1898 .non_legacy_dai_naming = 1,
1901 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1902 unsigned char *chmap)
1904 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1905 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1907 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1910 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1911 unsigned char *chmap, int prepared)
1913 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1914 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1915 struct hdac_hdmi_port *port;
1920 if (list_empty(&pcm->port_list))
1923 mutex_lock(&pcm->lock);
1924 pcm->chmap_set = true;
1925 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1926 list_for_each_entry(port, &pcm->port_list, head)
1928 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1929 mutex_unlock(&pcm->lock);
1932 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1934 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1935 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1940 if (list_empty(&pcm->port_list))
1946 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
1948 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1949 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1950 struct hdac_hdmi_port *port;
1955 if (list_empty(&pcm->port_list))
1958 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1960 if (!port || !port->eld.eld_valid)
1963 return port->eld.info.spk_alloc;
1966 static struct hdac_hdmi_drv_data intel_icl_drv_data = {
1967 .vendor_nid = INTEL_VENDOR_NID_0x2,
1968 .port_map = icl_pin2port_map,
1969 .port_num = ARRAY_SIZE(icl_pin2port_map),
1972 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
1973 .vendor_nid = INTEL_VENDOR_NID_0xb,
1976 static struct hdac_hdmi_drv_data intel_drv_data = {
1977 .vendor_nid = INTEL_VENDOR_NID_0x8,
1980 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
1982 struct hdac_hdmi_priv *hdmi_priv = NULL;
1983 struct snd_soc_dai_driver *hdmi_dais = NULL;
1984 struct hdac_ext_link *hlink = NULL;
1987 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1988 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
1990 /* hold the ref while we probe */
1991 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1993 dev_err(&hdev->dev, "hdac link not found\n");
1997 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1999 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2000 if (hdmi_priv == NULL)
2003 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2004 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2005 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2006 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2007 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2008 hdmi_priv->hdev = hdev;
2013 if (hdac_id->driver_data)
2014 hdmi_priv->drv_data =
2015 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2017 hdmi_priv->drv_data = &intel_drv_data;
2019 dev_set_drvdata(&hdev->dev, hdmi_priv);
2021 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2022 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2023 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2024 mutex_init(&hdmi_priv->pin_mutex);
2027 * Turned off in the runtime_suspend during the first explicit
2028 * pm_runtime_suspend call.
2030 snd_hdac_display_power(hdev->bus, hdev->addr, true);
2032 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2035 "Failed in parse and map nid with err: %d\n", ret);
2038 snd_hdac_refresh_widgets(hdev, true);
2040 /* ASoC specific initialization */
2041 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2042 hdmi_dais, num_dais);
2044 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2049 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2051 snd_hdac_display_power(hdev->bus, hdev->addr, false);
2057 static int hdac_hdmi_runtime_suspend(struct device *dev)
2059 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2060 struct hdac_bus *bus = hdev->bus;
2061 struct hdac_ext_link *hlink = NULL;
2063 dev_dbg(dev, "Enter: %s\n", __func__);
2065 /* controller may not have been initialized for the first time */
2071 * codec_read is preferred over codec_write to set the power state.
2072 * This way verb is send to set the power state and response
2073 * is received. So setting power state is ensured without using loop
2074 * to read the state.
2076 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2079 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2081 dev_err(dev, "hdac link not found\n");
2085 snd_hdac_ext_bus_link_put(bus, hlink);
2087 snd_hdac_display_power(bus, hdev->addr, false);
2092 static int hdac_hdmi_runtime_resume(struct device *dev)
2094 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2095 struct hdac_bus *bus = hdev->bus;
2096 struct hdac_ext_link *hlink = NULL;
2098 dev_dbg(dev, "Enter: %s\n", __func__);
2100 /* controller may not have been initialized for the first time */
2104 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2106 dev_err(dev, "hdac link not found\n");
2110 snd_hdac_ext_bus_link_get(bus, hlink);
2112 snd_hdac_display_power(bus, hdev->addr, true);
2114 hdac_hdmi_skl_enable_all_pins(hdev);
2115 hdac_hdmi_skl_enable_dp12(hdev);
2118 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2124 #define hdac_hdmi_runtime_suspend NULL
2125 #define hdac_hdmi_runtime_resume NULL
2128 static const struct dev_pm_ops hdac_hdmi_pm = {
2129 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2130 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2133 static const struct hda_device_id hdmi_list[] = {
2134 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2135 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2136 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2137 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2138 &intel_glk_drv_data),
2139 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2140 &intel_glk_drv_data),
2141 HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2142 &intel_icl_drv_data),
2146 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2148 static struct hdac_driver hdmi_driver = {
2150 .name = "HDMI HDA Codec",
2151 .pm = &hdac_hdmi_pm,
2153 .id_table = hdmi_list,
2154 .probe = hdac_hdmi_dev_probe,
2155 .remove = hdac_hdmi_dev_remove,
2158 static int __init hdmi_init(void)
2160 return snd_hda_ext_driver_register(&hdmi_driver);
2163 static void __exit hdmi_exit(void)
2165 snd_hda_ext_driver_unregister(&hdmi_driver);
2168 module_init(hdmi_init);
2169 module_exit(hdmi_exit);
2171 MODULE_LICENSE("GPL v2");
2172 MODULE_DESCRIPTION("HDMI HD codec");
2173 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2174 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");