2 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Samreen Nilofer <samreen.nilofer@intel.com>
6 * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/module.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/hdmi.h>
25 #include <drm/drm_edid.h>
26 #include <sound/pcm_params.h>
27 #include <sound/jack.h>
28 #include <sound/soc.h>
29 #include <sound/hdaudio_ext.h>
30 #include <sound/hda_i915.h>
31 #include <sound/pcm_drm_eld.h>
32 #include <sound/hda_chmap.h>
33 #include "../../hda/local.h"
34 #include "hdac_hdmi.h"
38 #define AMP_OUT_MUTE 0xb080
39 #define AMP_OUT_UNMUTE 0xb000
40 #define PIN_OUT (AC_PINCTL_OUT_EN)
42 #define HDA_MAX_CONNECTIONS 32
44 #define HDA_MAX_CVTS 3
45 #define HDA_MAX_PORTS 3
47 #define ELD_MAX_SIZE 256
48 #define ELD_FIXED_BYTES 20
50 #define ELD_VER_CEA_861D 2
51 #define ELD_VER_PARTIAL 31
52 #define ELD_MAX_MNL 16
54 struct hdac_hdmi_cvt_params {
55 unsigned int channels_min;
56 unsigned int channels_max;
62 struct hdac_hdmi_cvt {
63 struct list_head head;
66 struct hdac_hdmi_cvt_params params;
69 /* Currently only spk_alloc, more to be added */
70 struct hdac_hdmi_parsed_eld {
74 struct hdac_hdmi_eld {
78 char eld_buffer[ELD_MAX_SIZE];
79 struct hdac_hdmi_parsed_eld info;
82 struct hdac_hdmi_pin {
83 struct list_head head;
86 struct hdac_hdmi_port *ports;
88 struct hdac_device *hdev;
91 struct hdac_hdmi_port {
92 struct list_head head;
94 struct hdac_hdmi_pin *pin;
96 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
97 struct hdac_hdmi_eld eld;
99 struct snd_soc_dapm_context *dapm;
100 const char *output_pin;
103 struct hdac_hdmi_pcm {
104 struct list_head head;
106 struct list_head port_list;
107 struct hdac_hdmi_cvt *cvt;
108 struct snd_soc_jack *jack;
113 unsigned char chmap[8]; /* ALSA API channel-map */
118 struct hdac_hdmi_dai_port_map {
120 struct hdac_hdmi_port *port;
121 struct hdac_hdmi_cvt *cvt;
125 * pin to port mapping table where the value indicate the pin number and
126 * the index indicate the port number with 1 base.
128 static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
130 struct hdac_hdmi_drv_data {
131 unsigned int vendor_nid;
132 const int *port_map; /* pin to port mapping table */
136 struct hdac_hdmi_priv {
137 struct hdac_device *hdev;
138 struct snd_soc_component *component;
139 struct snd_card *card;
140 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
141 struct list_head pin_list;
142 struct list_head cvt_list;
143 struct list_head pcm_list;
147 struct mutex pin_mutex;
148 struct hdac_chmap chmap;
149 struct hdac_hdmi_drv_data *drv_data;
150 struct snd_soc_dai_driver *dai_drv;
153 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
155 static struct hdac_hdmi_pcm *
156 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
157 struct hdac_hdmi_cvt *cvt)
159 struct hdac_hdmi_pcm *pcm = NULL;
161 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
169 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
170 struct hdac_hdmi_port *port, bool is_connect)
172 struct hdac_device *hdev = port->pin->hdev;
175 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
177 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
181 * Report Jack connect event when a device is connected
182 * for the first time where same PCM is attached to multiple
185 if (pcm->jack_event == 0) {
187 "jack report for pcm=%d\n",
189 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
195 * Report Jack disconnect event when a device is disconnected
196 * is the only last connected device when same PCM is attached
199 if (pcm->jack_event == 1)
200 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
201 if (pcm->jack_event > 0)
205 snd_soc_dapm_sync(port->dapm);
208 /* MST supported verbs */
210 * Get the no devices that can be connected to a port on the Pin widget.
212 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
215 unsigned int type, param;
217 caps = get_wcaps(hdev, nid);
218 type = get_wcaps_type(caps);
220 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
223 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
227 return param & AC_DEV_LIST_LEN_MASK;
231 * Get the port entry select on the pin. Return the port entry
232 * id selected on the pin. Return 0 means the first port entry
233 * is selected or MST is not supported.
235 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
236 struct hdac_hdmi_port *port)
238 return snd_hdac_codec_read(hdev, port->pin->nid,
239 0, AC_VERB_GET_DEVICE_SEL, 0);
243 * Sets the selected port entry for the configuring Pin widget verb.
244 * returns error if port set is not equal to port get otherwise success
246 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
247 struct hdac_hdmi_port *port)
251 if (!port->pin->mst_capable)
254 /* AC_PAR_DEVLIST_LEN is 0 based. */
255 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
259 * Device List Length is a 0 based integer value indicating the
260 * number of sink device that a MST Pin Widget can support.
262 if (num_ports + 1 < port->id)
265 snd_hdac_codec_write(hdev, port->pin->nid, 0,
266 AC_VERB_SET_DEVICE_SEL, port->id);
268 if (port->id != hdac_hdmi_port_select_get(hdev, port))
271 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
276 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
279 struct hdac_hdmi_pcm *pcm;
281 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
282 if (pcm->pcm_id == pcm_idx)
289 static unsigned int sad_format(const u8 *sad)
291 return ((sad[0] >> 0x3) & 0x1f);
294 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
299 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
302 u64 formats = SNDRV_PCM_FMTBIT_S16;
304 const u8 *sad, *eld_buf = eld;
306 sad = drm_eld_sad(eld_buf);
308 goto format_constraint;
310 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
311 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
314 * the controller support 20 and 24 bits in 32 bit
315 * container so we set S32
317 if (sad_sample_bits_lpcm(sad) & 0x6)
318 formats |= SNDRV_PCM_FMTBIT_S32;
323 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
329 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
330 int packet_index, int byte_index)
334 val = (packet_index << 5) | (byte_index & 0x1f);
335 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
338 struct dp_audio_infoframe {
341 u8 ver; /* 0x11 << 2 */
343 u8 CC02_CT47; /* match with HDMI infoframe from this on */
347 u8 LFEPBL01_LSV36_DM_INH7;
350 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
351 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
353 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
354 struct hdmi_audio_infoframe frame;
355 struct hdac_hdmi_pin *pin = port->pin;
356 struct dp_audio_infoframe dp_ai;
357 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
358 struct hdac_hdmi_cvt *cvt = pcm->cvt;
366 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
367 pcm->channels, pcm->chmap_set, true, pcm->chmap);
369 channels = snd_hdac_get_active_channels(ca);
370 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
372 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
373 pcm->channels, pcm->chmap, pcm->chmap_set);
375 eld_buf = port->eld.eld_buffer;
376 conn_type = drm_eld_get_conn_type(eld_buf);
379 case DRM_ELD_CONN_TYPE_HDMI:
380 hdmi_audio_infoframe_init(&frame);
382 frame.channels = channels;
383 frame.channel_allocation = ca;
385 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
391 case DRM_ELD_CONN_TYPE_DP:
392 memset(&dp_ai, 0, sizeof(dp_ai));
395 dp_ai.ver = 0x11 << 2;
396 dp_ai.CC02_CT47 = channels - 1;
403 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
407 /* stop infoframe transmission */
408 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
409 snd_hdac_codec_write(hdev, pin->nid, 0,
410 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
413 /* Fill infoframe. Index auto-incremented */
414 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
415 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
416 for (i = 0; i < sizeof(buffer); i++)
417 snd_hdac_codec_write(hdev, pin->nid, 0,
418 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
420 for (i = 0; i < sizeof(dp_ai); i++)
421 snd_hdac_codec_write(hdev, pin->nid, 0,
422 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
425 /* Start infoframe */
426 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
427 snd_hdac_codec_write(hdev, pin->nid, 0,
428 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
433 static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
434 unsigned int tx_mask, unsigned int rx_mask,
435 int slots, int slot_width)
437 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
438 struct hdac_device *hdev = hdmi->hdev;
439 struct hdac_hdmi_dai_port_map *dai_map;
440 struct hdac_hdmi_pcm *pcm;
442 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
444 dai_map = &hdmi->dai_map[dai->id];
446 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
449 pcm->stream_tag = (tx_mask << 4);
454 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
455 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
457 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
458 struct hdac_hdmi_dai_port_map *dai_map;
459 struct hdac_hdmi_pcm *pcm;
462 dai_map = &hdmi->dai_map[dai->id];
464 format = snd_hdac_calc_stream_format(params_rate(hparams),
465 params_channels(hparams), params_format(hparams),
466 dai->driver->playback.sig_bits, 0);
468 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
472 pcm->format = format;
473 pcm->channels = params_channels(hparams);
478 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
479 struct hdac_hdmi_pin *pin,
480 struct hdac_hdmi_port *port)
482 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
484 "HDMI: pin %d wcaps %#x does not support connection list\n",
485 pin->nid, get_wcaps(hdev, pin->nid));
489 if (hdac_hdmi_port_select_set(hdev, port) < 0)
492 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
493 port->mux_nids, HDA_MAX_CONNECTIONS);
494 if (port->num_mux_nids == 0)
496 "No connections found for pin:port %d:%d\n",
499 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
500 port->num_mux_nids, pin->nid, port->id);
502 return port->num_mux_nids;
506 * Query pcm list and return port to which stream is routed.
508 * Also query connection list of the pin, to validate the cvt to port map.
510 * Same stream rendering to multiple ports simultaneously can be done
511 * possibly, but not supported for now in driver. So return the first port
514 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
515 struct hdac_device *hdev,
516 struct hdac_hdmi_priv *hdmi,
517 struct hdac_hdmi_cvt *cvt)
519 struct hdac_hdmi_pcm *pcm;
520 struct hdac_hdmi_port *port = NULL;
523 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
524 if (pcm->cvt == cvt) {
525 if (list_empty(&pcm->port_list))
528 list_for_each_entry(port, &pcm->port_list, head) {
529 mutex_lock(&pcm->lock);
530 ret = hdac_hdmi_query_port_connlist(hdev,
532 mutex_unlock(&pcm->lock);
536 for (i = 0; i < port->num_mux_nids; i++) {
537 if (port->mux_nids[i] == cvt->nid &&
538 port->eld.monitor_present &&
550 * This tries to get a valid pin and set the HW constraints based on the
551 * ELD. Even if a valid pin is not found return success so that device open
554 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
555 struct snd_soc_dai *dai)
557 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
558 struct hdac_device *hdev = hdmi->hdev;
559 struct hdac_hdmi_dai_port_map *dai_map;
560 struct hdac_hdmi_cvt *cvt;
561 struct hdac_hdmi_port *port;
564 dai_map = &hdmi->dai_map[dai->id];
567 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
570 * To make PA and other userland happy.
571 * userland scans devices so returning error does not help.
575 if ((!port->eld.monitor_present) ||
576 (!port->eld.eld_valid)) {
579 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
580 port->eld.monitor_present, port->eld.eld_valid,
581 port->pin->nid, port->id);
586 dai_map->port = port;
588 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
589 port->eld.eld_buffer);
593 return snd_pcm_hw_constraint_eld(substream->runtime,
594 port->eld.eld_buffer);
597 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
598 struct snd_soc_dai *dai)
600 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
601 struct hdac_hdmi_dai_port_map *dai_map;
602 struct hdac_hdmi_pcm *pcm;
604 dai_map = &hdmi->dai_map[dai->id];
606 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
609 mutex_lock(&pcm->lock);
610 pcm->chmap_set = false;
611 memset(pcm->chmap, 0, sizeof(pcm->chmap));
613 mutex_unlock(&pcm->lock);
617 dai_map->port = NULL;
621 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
624 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
627 chans = get_wcaps(hdev, cvt->nid);
628 chans = get_wcaps_channels(chans);
630 cvt->params.channels_min = 2;
632 cvt->params.channels_max = chans;
633 if (chans > hdmi->chmap.channels_max)
634 hdmi->chmap.channels_max = chans;
636 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
638 &cvt->params.formats,
639 &cvt->params.maxbps);
642 "Failed to query pcm params for nid %d: %d\n",
648 static int hdac_hdmi_fill_widget_info(struct device *dev,
649 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
650 void *priv, const char *wname, const char *stream,
651 struct snd_kcontrol_new *wc, int numkc,
652 int (*event)(struct snd_soc_dapm_widget *,
653 struct snd_kcontrol *, int), unsigned short event_flags)
656 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
661 w->reg = SND_SOC_NOPM;
663 w->kcontrol_news = wc;
664 w->num_kcontrols = numkc;
667 w->event_flags = event_flags;
672 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
673 const char *sink, const char *control, const char *src,
674 int (*handler)(struct snd_soc_dapm_widget *src,
675 struct snd_soc_dapm_widget *sink))
679 route->control = control;
680 route->connected = handler;
683 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
684 struct hdac_hdmi_port *port)
686 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
687 struct hdac_hdmi_pcm *pcm = NULL;
688 struct hdac_hdmi_port *p;
690 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
691 if (list_empty(&pcm->port_list))
694 list_for_each_entry(p, &pcm->port_list, head) {
695 if (p->id == port->id && port->pin == p->pin)
703 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
704 hda_nid_t nid, unsigned int pwr_state)
709 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
710 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
711 for (count = 0; count < 10; count++) {
712 snd_hdac_codec_read(hdev, nid, 0,
713 AC_VERB_SET_POWER_STATE,
715 state = snd_hdac_sync_power_state(hdev,
717 if (!(state & AC_PWRST_ERROR))
724 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
725 hda_nid_t nid, int val)
727 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
728 snd_hdac_codec_write(hdev, nid, 0,
729 AC_VERB_SET_AMP_GAIN_MUTE, val);
733 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
734 struct snd_kcontrol *kc, int event)
736 struct hdac_hdmi_port *port = w->priv;
737 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
738 struct hdac_hdmi_pcm *pcm;
740 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
741 __func__, w->name, event);
743 pcm = hdac_hdmi_get_pcm(hdev, port);
747 /* set the device if pin is mst_capable */
748 if (hdac_hdmi_port_select_set(hdev, port) < 0)
752 case SND_SOC_DAPM_PRE_PMU:
753 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
755 /* Enable out path for this pin widget */
756 snd_hdac_codec_write(hdev, port->pin->nid, 0,
757 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
759 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
761 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
763 case SND_SOC_DAPM_POST_PMD:
764 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
766 /* Disable out path for this pin widget */
767 snd_hdac_codec_write(hdev, port->pin->nid, 0,
768 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
770 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
778 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
779 struct snd_kcontrol *kc, int event)
781 struct hdac_hdmi_cvt *cvt = w->priv;
782 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
783 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
784 struct hdac_hdmi_pcm *pcm;
786 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
787 __func__, w->name, event);
789 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
794 case SND_SOC_DAPM_PRE_PMU:
795 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
797 /* Enable transmission */
798 snd_hdac_codec_write(hdev, cvt->nid, 0,
799 AC_VERB_SET_DIGI_CONVERT_1, 1);
801 /* Category Code (CC) to zero */
802 snd_hdac_codec_write(hdev, cvt->nid, 0,
803 AC_VERB_SET_DIGI_CONVERT_2, 0);
805 snd_hdac_codec_write(hdev, cvt->nid, 0,
806 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
807 snd_hdac_codec_write(hdev, cvt->nid, 0,
808 AC_VERB_SET_STREAM_FORMAT, pcm->format);
811 case SND_SOC_DAPM_POST_PMD:
812 snd_hdac_codec_write(hdev, cvt->nid, 0,
813 AC_VERB_SET_CHANNEL_STREAMID, 0);
814 snd_hdac_codec_write(hdev, cvt->nid, 0,
815 AC_VERB_SET_STREAM_FORMAT, 0);
817 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
825 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
826 struct snd_kcontrol *kc, int event)
828 struct hdac_hdmi_port *port = w->priv;
829 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
832 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
833 __func__, w->name, event);
836 kc = w->kcontrols[0];
838 mux_idx = dapm_kcontrol_get_value(kc);
840 /* set the device if pin is mst_capable */
841 if (hdac_hdmi_port_select_set(hdev, port) < 0)
845 snd_hdac_codec_write(hdev, port->pin->nid, 0,
846 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
853 * Based on user selection, map the PINs with the PCMs.
855 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
859 struct hdac_hdmi_port *p, *p_next;
860 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
861 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
862 struct snd_soc_dapm_context *dapm = w->dapm;
863 struct hdac_hdmi_port *port = w->priv;
864 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
865 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
866 struct hdac_hdmi_pcm *pcm = NULL;
867 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
869 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
876 mutex_lock(&hdmi->pin_mutex);
877 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
878 if (list_empty(&pcm->port_list))
881 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
882 if (p == port && p->id == port->id &&
883 p->pin == port->pin) {
884 hdac_hdmi_jack_report(pcm, port, false);
891 * Jack status is not reported during device probe as the
892 * PCMs are not registered by then. So report it here.
894 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
895 if (!strcmp(cvt_name, pcm->cvt->name)) {
896 list_add_tail(&port->head, &pcm->port_list);
897 if (port->eld.monitor_present && port->eld.eld_valid) {
898 hdac_hdmi_jack_report(pcm, port, true);
899 mutex_unlock(&hdmi->pin_mutex);
904 mutex_unlock(&hdmi->pin_mutex);
910 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
911 * the display driver seem to be programming the connection list for the pin
914 * So programming all the possible inputs for the mux, the user has to take
915 * care of selecting the right one and leaving all other inputs selected to
918 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
919 struct hdac_hdmi_port *port,
920 struct snd_soc_dapm_widget *widget,
921 const char *widget_name)
923 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
924 struct hdac_hdmi_pin *pin = port->pin;
925 struct snd_kcontrol_new *kc;
926 struct hdac_hdmi_cvt *cvt;
928 char kc_name[NAME_SIZE];
929 char mux_items[NAME_SIZE];
930 /* To hold inputs to the Pin mux */
931 char *items[HDA_MAX_CONNECTIONS];
933 int num_items = hdmi->num_cvt + 1;
935 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
939 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
943 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
945 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
949 kc->private_value = (long)se;
950 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
952 kc->info = snd_soc_info_enum_double;
953 kc->put = hdac_hdmi_set_pin_port_mux;
954 kc->get = snd_soc_dapm_get_enum_double;
956 se->reg = SND_SOC_NOPM;
958 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
959 se->items = num_items;
960 se->mask = roundup_pow_of_two(se->items) - 1;
962 sprintf(mux_items, "NONE");
963 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
967 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
969 sprintf(mux_items, "cvt %d", cvt->nid);
970 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
975 se->texts = devm_kmemdup(&hdev->dev, items,
976 (num_items * sizeof(char *)), GFP_KERNEL);
980 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
981 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
982 hdac_hdmi_pin_mux_widget_event,
983 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
986 /* Add cvt <- input <- mux route map */
987 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
988 struct snd_soc_dapm_widget *widgets,
989 struct snd_soc_dapm_route *route, int rindex)
991 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
992 const struct snd_kcontrol_new *kc;
994 int mux_index = hdmi->num_cvt + hdmi->num_ports;
997 for (i = 0; i < hdmi->num_ports; i++) {
998 kc = widgets[mux_index].kcontrol_news;
999 se = (struct soc_enum *)kc->private_value;
1000 for (j = 0; j < hdmi->num_cvt; j++) {
1001 hdac_hdmi_fill_route(&route[rindex],
1002 widgets[mux_index].name,
1004 widgets[j].name, NULL);
1014 * Widgets are added in the below sequence
1015 * Converter widgets for num converters enumerated
1016 * Pin-port widgets for num ports for Pins enumerated
1017 * Pin-port mux widgets to represent connenction list of pin widget
1019 * For each port, one Mux and One output widget is added
1020 * Total widgets elements = num_cvt + (num_ports * 2);
1022 * Routes are added as below:
1023 * pin-port mux -> pin (based on num_ports)
1024 * cvt -> "Input sel control" -> pin-port_mux
1026 * Total route elements:
1027 * num_ports + (pin_muxes * num_cvt)
1029 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1031 struct snd_soc_dapm_widget *widgets;
1032 struct snd_soc_dapm_route *route;
1033 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1034 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1035 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1036 char widget_name[NAME_SIZE];
1037 struct hdac_hdmi_cvt *cvt;
1038 struct hdac_hdmi_pin *pin;
1039 int ret, i = 0, num_routes = 0, j;
1041 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1044 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1045 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1051 /* DAPM widgets to represent each converter widget */
1052 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1053 sprintf(widget_name, "Converter %d", cvt->nid);
1054 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1055 snd_soc_dapm_aif_in, cvt,
1056 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1057 hdac_hdmi_cvt_output_widget_event,
1058 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1064 list_for_each_entry(pin, &hdmi->pin_list, head) {
1065 for (j = 0; j < pin->num_ports; j++) {
1066 sprintf(widget_name, "hif%d-%d Output",
1067 pin->nid, pin->ports[j].id);
1068 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1069 snd_soc_dapm_output, &pin->ports[j],
1070 widget_name, NULL, NULL, 0,
1071 hdac_hdmi_pin_output_widget_event,
1072 SND_SOC_DAPM_PRE_PMU |
1073 SND_SOC_DAPM_POST_PMD);
1076 pin->ports[j].output_pin = widgets[i].name;
1081 /* DAPM widgets to represent the connection list to pin widget */
1082 list_for_each_entry(pin, &hdmi->pin_list, head) {
1083 for (j = 0; j < pin->num_ports; j++) {
1084 sprintf(widget_name, "Pin%d-Port%d Mux",
1085 pin->nid, pin->ports[j].id);
1086 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1087 &pin->ports[j], &widgets[i],
1093 /* For cvt to pin_mux mapping */
1094 num_routes += hdmi->num_cvt;
1096 /* For pin_mux to pin mapping */
1101 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1107 /* Add pin <- NULL <- mux route map */
1108 list_for_each_entry(pin, &hdmi->pin_list, head) {
1109 for (j = 0; j < pin->num_ports; j++) {
1110 int sink_index = i + hdmi->num_cvt;
1111 int src_index = sink_index + pin->num_ports *
1114 hdac_hdmi_fill_route(&route[i],
1115 widgets[sink_index].name, NULL,
1116 widgets[src_index].name, NULL);
1121 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1123 snd_soc_dapm_new_controls(dapm, widgets,
1124 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1126 snd_soc_dapm_add_routes(dapm, route, num_routes);
1127 snd_soc_dapm_new_widgets(dapm->card);
1133 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1135 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1136 struct hdac_hdmi_dai_port_map *dai_map;
1137 struct hdac_hdmi_cvt *cvt;
1140 if (list_empty(&hdmi->cvt_list))
1143 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1144 dai_map = &hdmi->dai_map[dai_id];
1145 dai_map->dai_id = dai_id;
1150 if (dai_id == HDA_MAX_CVTS) {
1151 dev_warn(&hdev->dev,
1152 "Max dais supported: %d\n", dai_id);
1160 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1162 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1163 struct hdac_hdmi_cvt *cvt;
1164 char name[NAME_SIZE];
1166 cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1171 sprintf(name, "cvt %d", cvt->nid);
1172 cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1176 list_add_tail(&cvt->head, &hdmi->cvt_list);
1179 return hdac_hdmi_query_cvt_params(hdev, cvt);
1182 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1183 struct hdac_hdmi_port *port)
1185 unsigned int ver, mnl;
1187 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1188 >> DRM_ELD_VER_SHIFT;
1190 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1191 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1195 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1196 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1198 if (mnl > ELD_MAX_MNL) {
1199 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1203 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1208 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1209 struct hdac_hdmi_port *port)
1211 struct hdac_device *hdev = pin->hdev;
1212 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1213 struct hdac_hdmi_pcm *pcm;
1221 * In case of non MST pin, get_eld info API expectes port
1224 mutex_lock(&hdmi->pin_mutex);
1225 port->eld.monitor_present = false;
1227 if (pin->mst_capable)
1230 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1231 &port->eld.monitor_present,
1232 port->eld.eld_buffer,
1236 size = min(size, ELD_MAX_SIZE);
1237 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1242 port->eld.eld_valid = true;
1243 port->eld.eld_size = size;
1245 port->eld.eld_valid = false;
1246 port->eld.eld_size = 0;
1249 pcm = hdac_hdmi_get_pcm(hdev, port);
1251 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1253 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1254 __func__, pin->nid, port->id);
1257 * PCMs are not registered during device probe, so don't
1258 * report jack here. It will be done in usermode mux
1262 hdac_hdmi_jack_report(pcm, port, false);
1264 mutex_unlock(&hdmi->pin_mutex);
1268 if (port->eld.monitor_present && port->eld.eld_valid) {
1270 hdac_hdmi_jack_report(pcm, port, true);
1272 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1273 port->eld.eld_buffer, port->eld.eld_size, false);
1276 mutex_unlock(&hdmi->pin_mutex);
1279 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1280 struct hdac_hdmi_pin *pin)
1282 struct hdac_hdmi_port *ports;
1283 int max_ports = HDA_MAX_PORTS;
1287 * FIXME: max_port may vary for each platform, so pass this as
1288 * as driver data or query from i915 interface when this API is
1292 ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1296 for (i = 0; i < max_ports; i++) {
1301 pin->num_ports = max_ports;
1305 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1307 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1308 struct hdac_hdmi_pin *pin;
1311 pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1316 pin->mst_capable = false;
1318 ret = hdac_hdmi_add_ports(hdev, pin);
1322 list_add_tail(&pin->head, &hdmi->pin_list);
1324 hdmi->num_ports += pin->num_ports;
1329 #define INTEL_VENDOR_NID_0x2 0x02
1330 #define INTEL_VENDOR_NID_0x8 0x08
1331 #define INTEL_VENDOR_NID_0xb 0x0b
1332 #define INTEL_GET_VENDOR_VERB 0xf81
1333 #define INTEL_SET_VENDOR_VERB 0x781
1334 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1335 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1337 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1339 unsigned int vendor_param;
1340 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1341 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1343 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1344 INTEL_GET_VENDOR_VERB, 0);
1345 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1348 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1349 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1350 INTEL_SET_VENDOR_VERB, vendor_param);
1351 if (vendor_param == -1)
1355 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1357 unsigned int vendor_param;
1358 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1359 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1361 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1362 INTEL_GET_VENDOR_VERB, 0);
1363 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1366 /* enable DP1.2 mode */
1367 vendor_param |= INTEL_EN_DP12;
1368 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1369 INTEL_SET_VENDOR_VERB, vendor_param);
1370 if (vendor_param == -1)
1375 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1376 .startup = hdac_hdmi_pcm_open,
1377 .shutdown = hdac_hdmi_pcm_close,
1378 .hw_params = hdac_hdmi_set_hw_params,
1379 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
1383 * Each converter can support a stream independently. So a dai is created
1384 * based on the number of converter queried.
1386 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1387 struct snd_soc_dai_driver **dais,
1388 struct hdac_hdmi_priv *hdmi, int num_dais)
1390 struct snd_soc_dai_driver *hdmi_dais;
1391 struct hdac_hdmi_cvt *cvt;
1392 char name[NAME_SIZE], dai_name[NAME_SIZE];
1395 unsigned int rate_max = 384000, rate_min = 8000;
1399 hdmi_dais = devm_kzalloc(&hdev->dev,
1400 (sizeof(*hdmi_dais) * num_dais),
1405 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1406 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1407 &rates, &formats, &bps);
1411 /* Filter out 44.1, 88.2 and 176.4Khz */
1412 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1413 SNDRV_PCM_RATE_176400);
1417 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1418 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1419 dai_name, GFP_KERNEL);
1421 if (!hdmi_dais[i].name)
1424 snprintf(name, sizeof(name), "hifi%d", i+1);
1425 hdmi_dais[i].playback.stream_name =
1426 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1427 if (!hdmi_dais[i].playback.stream_name)
1431 * Set caps based on capability queried from the converter.
1432 * It will be constrained runtime based on ELD queried.
1434 hdmi_dais[i].playback.formats = formats;
1435 hdmi_dais[i].playback.rates = rates;
1436 hdmi_dais[i].playback.rate_max = rate_max;
1437 hdmi_dais[i].playback.rate_min = rate_min;
1438 hdmi_dais[i].playback.channels_min = 2;
1439 hdmi_dais[i].playback.channels_max = 2;
1440 hdmi_dais[i].playback.sig_bits = bps;
1441 hdmi_dais[i].ops = &hdmi_dai_ops;
1446 hdmi->dai_drv = hdmi_dais;
1452 * Parse all nodes and store the cvt/pin nids in array
1453 * Add one time initialization for pin and cvt widgets
1455 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1456 struct snd_soc_dai_driver **dais, int *num_dais)
1460 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1463 hdac_hdmi_skl_enable_all_pins(hdev);
1464 hdac_hdmi_skl_enable_dp12(hdev);
1466 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1467 if (!nid || num_nodes <= 0) {
1468 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1472 for (i = 0; i < num_nodes; i++, nid++) {
1476 caps = get_wcaps(hdev, nid);
1477 type = get_wcaps_type(caps);
1479 if (!(caps & AC_WCAP_DIGITAL))
1484 case AC_WID_AUD_OUT:
1485 ret = hdac_hdmi_add_cvt(hdev, nid);
1491 ret = hdac_hdmi_add_pin(hdev, nid);
1498 if (!hdmi->num_pin || !hdmi->num_cvt) {
1500 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1504 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1506 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1511 *num_dais = hdmi->num_cvt;
1512 ret = hdac_hdmi_init_dai_map(hdev);
1514 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1519 static int hdac_hdmi_pin2port(void *aptr, int pin)
1521 struct hdac_device *hdev = aptr;
1522 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1523 const int *map = hdmi->drv_data->port_map;
1526 if (!hdmi->drv_data->port_num)
1527 return pin - 4; /* map NID 0x05 -> port #1 */
1530 * looking for the pin number in the mapping table and return
1531 * the index which indicate the port number
1533 for (i = 0; i < hdmi->drv_data->port_num; i++) {
1538 /* return -1 if pin number exceeds our expectation */
1539 dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1543 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1545 struct hdac_device *hdev = aptr;
1546 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1547 struct hdac_hdmi_pin *pin = NULL;
1548 struct hdac_hdmi_port *hport = NULL;
1549 struct snd_soc_component *component = hdmi->component;
1553 if (!hdmi->drv_data->port_num) {
1554 /* for legacy platforms */
1555 pin_nid = port + 0x04;
1556 } else if (port < hdmi->drv_data->port_num) {
1557 /* get pin number from the pin2port mapping table */
1558 pin_nid = hdmi->drv_data->port_map[port - 1];
1560 dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1564 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1568 * skip notification during system suspend (but not in runtime PM);
1569 * the state will be updated at resume. Also since the ELD and
1570 * connection states are updated in anyway at the end of the resume,
1571 * we can skip it when received during PM process.
1573 if (snd_power_get_state(component->card->snd_card) !=
1577 if (atomic_read(&hdev->in_pm))
1580 list_for_each_entry(pin, &hdmi->pin_list, head) {
1581 if (pin->nid != pin_nid)
1584 /* In case of non MST pin, pipe is -1 */
1586 pin->mst_capable = false;
1587 /* if not MST, default is port[0] */
1588 hport = &pin->ports[0];
1590 for (i = 0; i < pin->num_ports; i++) {
1591 pin->mst_capable = true;
1592 if (pin->ports[i].id == pipe) {
1593 hport = &pin->ports[i];
1600 hdac_hdmi_present_sense(pin, hport);
1605 static struct drm_audio_component_audio_ops aops = {
1606 .pin2port = hdac_hdmi_pin2port,
1607 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1610 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1613 struct snd_soc_pcm_runtime *rtd;
1615 for_each_card_rtds(card, rtd) {
1616 if (rtd->pcm && (rtd->pcm->device == device))
1623 /* create jack pin kcontrols */
1624 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1625 struct hdac_device *hdev)
1627 struct hdac_hdmi_pin *pin;
1628 struct snd_kcontrol_new *kc;
1629 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1632 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1633 struct snd_soc_component *component = hdmi->component;
1635 kc = devm_kcalloc(component->dev, hdmi->num_ports,
1636 sizeof(*kc), GFP_KERNEL);
1641 list_for_each_entry(pin, &hdmi->pin_list, head) {
1642 for (j = 0; j < pin->num_ports; j++) {
1643 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1644 pin->nid, pin->ports[j].id);
1645 name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1648 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1649 kc[i].name = devm_kstrdup(component->dev, kc_name,
1654 kc[i].private_value = (unsigned long)name;
1655 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1657 kc[i].info = snd_soc_dapm_info_pin_switch;
1658 kc[i].put = snd_soc_dapm_put_pin_switch;
1659 kc[i].get = snd_soc_dapm_get_pin_switch;
1664 return snd_soc_add_card_controls(card, kc, i);
1667 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1668 struct snd_soc_dapm_context *dapm)
1670 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1671 struct hdac_device *hdev = hdmi->hdev;
1672 struct hdac_hdmi_pin *pin;
1673 struct snd_soc_dapm_widget *widgets;
1674 struct snd_soc_dapm_route *route;
1675 char w_name[NAME_SIZE];
1678 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1679 sizeof(*widgets), GFP_KERNEL);
1684 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1685 sizeof(*route), GFP_KERNEL);
1689 /* create Jack DAPM widget */
1690 list_for_each_entry(pin, &hdmi->pin_list, head) {
1691 for (j = 0; j < pin->num_ports; j++) {
1692 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1693 pin->nid, pin->ports[j].id);
1695 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1696 snd_soc_dapm_spk, NULL,
1697 w_name, NULL, NULL, 0, NULL, 0);
1701 pin->ports[j].jack_pin = widgets[i].name;
1702 pin->ports[j].dapm = dapm;
1704 /* add to route from Jack widget to output */
1705 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1706 NULL, pin->ports[j].output_pin, NULL);
1712 /* Add Route from Jack widget to the output widget */
1713 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1717 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1721 ret = snd_soc_dapm_new_widgets(dapm->card);
1725 /* Add Jack Pin switch Kcontrol */
1726 ret = create_fill_jack_kcontrols(dapm->card, hdev);
1731 /* default set the Jack Pin switch to OFF */
1732 list_for_each_entry(pin, &hdmi->pin_list, head) {
1733 for (j = 0; j < pin->num_ports; j++)
1734 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1735 pin->ports[j].jack_pin);
1740 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1742 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1743 struct snd_soc_jack *jack)
1745 struct snd_soc_component *component = dai->component;
1746 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1747 struct hdac_device *hdev = hdmi->hdev;
1748 struct hdac_hdmi_pcm *pcm;
1749 struct snd_pcm *snd_pcm;
1753 * this is a new PCM device, create new pcm and
1754 * add to the pcm list
1756 pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1759 pcm->pcm_id = device;
1760 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1761 pcm->jack_event = 0;
1763 mutex_init(&pcm->lock);
1764 INIT_LIST_HEAD(&pcm->port_list);
1765 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1767 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1770 "chmap control add failed with err: %d for pcm: %d\n",
1776 list_add_tail(&pcm->head, &hdmi->pcm_list);
1780 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1782 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1783 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1786 struct hdac_hdmi_pin *pin;
1788 list_for_each_entry(pin, &hdmi->pin_list, head) {
1789 if (detect_pin_caps) {
1791 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1792 pin->mst_capable = false;
1794 pin->mst_capable = true;
1797 for (i = 0; i < pin->num_ports; i++) {
1798 if (!pin->mst_capable && i > 0)
1801 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1806 static int hdmi_codec_probe(struct snd_soc_component *component)
1808 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1809 struct hdac_device *hdev = hdmi->hdev;
1810 struct snd_soc_dapm_context *dapm =
1811 snd_soc_component_get_dapm(component);
1812 struct hdac_ext_link *hlink = NULL;
1815 hdmi->component = component;
1818 * hold the ref while we probe, also no need to drop the ref on
1819 * exit, we call pm_runtime_suspend() so that will do for us
1821 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1823 dev_err(&hdev->dev, "hdac link not found\n");
1827 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1829 ret = create_fill_widget_route_map(dapm);
1833 aops.audio_ptr = hdev;
1834 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1836 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1840 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1841 /* Imp: Store the card pointer in hda_codec */
1842 hdmi->card = dapm->card->snd_card;
1845 * Setup a device_link between card device and HDMI codec device.
1846 * The card device is the consumer and the HDMI codec device is
1847 * the supplier. With this setting, we can make sure that the audio
1848 * domain in display power will be always turned on before operating
1849 * on the HDMI audio codec registers.
1850 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1851 * sure the device link is freed when the machine driver is removed.
1853 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1854 DL_FLAG_AUTOREMOVE_CONSUMER);
1856 * hdac_device core already sets the state to active and calls
1857 * get_noresume. So enable runtime and set the device to suspend.
1859 pm_runtime_enable(&hdev->dev);
1860 pm_runtime_put(&hdev->dev);
1861 pm_runtime_suspend(&hdev->dev);
1866 static void hdmi_codec_remove(struct snd_soc_component *component)
1868 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1869 struct hdac_device *hdev = hdmi->hdev;
1871 pm_runtime_disable(&hdev->dev);
1874 #ifdef CONFIG_PM_SLEEP
1875 static int hdmi_codec_resume(struct device *dev)
1877 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1878 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1881 ret = pm_runtime_force_resume(dev);
1885 * As the ELD notify callback request is not entertained while the
1886 * device is in suspend state. Need to manually check detection of
1887 * all pins here. pin capablity change is not support, so use the
1888 * already set pin caps.
1890 * NOTE: this is safe to call even if the codec doesn't actually resume.
1891 * The pin check involves only with DRM audio component hooks, so it
1892 * works even if the HD-audio side is still dreaming peacefully.
1894 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1898 #define hdmi_codec_resume NULL
1901 static const struct snd_soc_component_driver hdmi_hda_codec = {
1902 .probe = hdmi_codec_probe,
1903 .remove = hdmi_codec_remove,
1904 .use_pmdown_time = 1,
1906 .non_legacy_dai_naming = 1,
1909 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1910 unsigned char *chmap)
1912 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1913 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1915 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1918 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1919 unsigned char *chmap, int prepared)
1921 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1922 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1923 struct hdac_hdmi_port *port;
1928 if (list_empty(&pcm->port_list))
1931 mutex_lock(&pcm->lock);
1932 pcm->chmap_set = true;
1933 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1934 list_for_each_entry(port, &pcm->port_list, head)
1936 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1937 mutex_unlock(&pcm->lock);
1940 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1942 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1943 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1948 if (list_empty(&pcm->port_list))
1954 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
1956 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1957 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1958 struct hdac_hdmi_port *port;
1963 if (list_empty(&pcm->port_list))
1966 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1968 if (!port || !port->eld.eld_valid)
1971 return port->eld.info.spk_alloc;
1974 static struct hdac_hdmi_drv_data intel_icl_drv_data = {
1975 .vendor_nid = INTEL_VENDOR_NID_0x2,
1976 .port_map = icl_pin2port_map,
1977 .port_num = ARRAY_SIZE(icl_pin2port_map),
1980 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
1981 .vendor_nid = INTEL_VENDOR_NID_0xb,
1984 static struct hdac_hdmi_drv_data intel_drv_data = {
1985 .vendor_nid = INTEL_VENDOR_NID_0x8,
1988 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
1990 struct hdac_hdmi_priv *hdmi_priv = NULL;
1991 struct snd_soc_dai_driver *hdmi_dais = NULL;
1992 struct hdac_ext_link *hlink = NULL;
1995 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1996 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
1998 /* hold the ref while we probe */
1999 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2001 dev_err(&hdev->dev, "hdac link not found\n");
2005 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2007 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2008 if (hdmi_priv == NULL)
2011 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2012 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2013 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2014 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2015 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2016 hdmi_priv->hdev = hdev;
2021 if (hdac_id->driver_data)
2022 hdmi_priv->drv_data =
2023 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2025 hdmi_priv->drv_data = &intel_drv_data;
2027 dev_set_drvdata(&hdev->dev, hdmi_priv);
2029 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2030 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2031 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2032 mutex_init(&hdmi_priv->pin_mutex);
2035 * Turned off in the runtime_suspend during the first explicit
2036 * pm_runtime_suspend call.
2038 snd_hdac_display_power(hdev->bus, hdev->addr, true);
2040 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2043 "Failed in parse and map nid with err: %d\n", ret);
2046 snd_hdac_refresh_widgets(hdev, true);
2048 /* ASoC specific initialization */
2049 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2050 hdmi_dais, num_dais);
2052 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2057 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2059 snd_hdac_display_power(hdev->bus, hdev->addr, false);
2065 static int hdac_hdmi_runtime_suspend(struct device *dev)
2067 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2068 struct hdac_bus *bus = hdev->bus;
2069 struct hdac_ext_link *hlink = NULL;
2071 dev_dbg(dev, "Enter: %s\n", __func__);
2073 /* controller may not have been initialized for the first time */
2079 * codec_read is preferred over codec_write to set the power state.
2080 * This way verb is send to set the power state and response
2081 * is received. So setting power state is ensured without using loop
2082 * to read the state.
2084 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2087 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2089 dev_err(dev, "hdac link not found\n");
2093 snd_hdac_ext_bus_link_put(bus, hlink);
2095 snd_hdac_display_power(bus, hdev->addr, false);
2100 static int hdac_hdmi_runtime_resume(struct device *dev)
2102 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2103 struct hdac_bus *bus = hdev->bus;
2104 struct hdac_ext_link *hlink = NULL;
2106 dev_dbg(dev, "Enter: %s\n", __func__);
2108 /* controller may not have been initialized for the first time */
2112 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2114 dev_err(dev, "hdac link not found\n");
2118 snd_hdac_ext_bus_link_get(bus, hlink);
2120 snd_hdac_display_power(bus, hdev->addr, true);
2122 hdac_hdmi_skl_enable_all_pins(hdev);
2123 hdac_hdmi_skl_enable_dp12(hdev);
2126 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2132 #define hdac_hdmi_runtime_suspend NULL
2133 #define hdac_hdmi_runtime_resume NULL
2136 static const struct dev_pm_ops hdac_hdmi_pm = {
2137 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2138 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2141 static const struct hda_device_id hdmi_list[] = {
2142 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2143 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2144 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2145 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2146 &intel_glk_drv_data),
2147 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2148 &intel_glk_drv_data),
2149 HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2150 &intel_icl_drv_data),
2154 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2156 static struct hdac_driver hdmi_driver = {
2158 .name = "HDMI HDA Codec",
2159 .pm = &hdac_hdmi_pm,
2161 .id_table = hdmi_list,
2162 .probe = hdac_hdmi_dev_probe,
2163 .remove = hdac_hdmi_dev_remove,
2166 static int __init hdmi_init(void)
2168 return snd_hda_ext_driver_register(&hdmi_driver);
2171 static void __exit hdmi_exit(void)
2173 snd_hda_ext_driver_unregister(&hdmi_driver);
2176 module_init(hdmi_init);
2177 module_exit(hdmi_exit);
2179 MODULE_LICENSE("GPL v2");
2180 MODULE_DESCRIPTION("HDMI HD codec");
2181 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2182 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");