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
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/hdmi.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_eld.h>
20 #include <sound/pcm_params.h>
21 #include <sound/jack.h>
22 #include <sound/soc.h>
23 #include <sound/hdaudio_ext.h>
24 #include <sound/hda_i915.h>
25 #include <sound/pcm_drm_eld.h>
26 #include <sound/hda_chmap.h>
27 #include "../../hda/local.h"
28 #include "hdac_hdmi.h"
32 #define AMP_OUT_MUTE 0xb080
33 #define AMP_OUT_UNMUTE 0xb000
34 #define PIN_OUT (AC_PINCTL_OUT_EN)
36 #define HDA_MAX_CONNECTIONS 32
38 #define HDA_MAX_CVTS 3
39 #define HDA_MAX_PORTS 3
41 #define ELD_MAX_SIZE 256
42 #define ELD_FIXED_BYTES 20
44 #define ELD_VER_CEA_861D 2
45 #define ELD_VER_PARTIAL 31
46 #define ELD_MAX_MNL 16
48 struct hdac_hdmi_cvt_params {
49 unsigned int channels_min;
50 unsigned int channels_max;
56 struct hdac_hdmi_cvt {
57 struct list_head head;
60 struct hdac_hdmi_cvt_params params;
63 /* Currently only spk_alloc, more to be added */
64 struct hdac_hdmi_parsed_eld {
68 struct hdac_hdmi_eld {
72 char eld_buffer[ELD_MAX_SIZE];
73 struct hdac_hdmi_parsed_eld info;
76 struct hdac_hdmi_pin {
77 struct list_head head;
80 struct hdac_hdmi_port *ports;
82 struct hdac_device *hdev;
85 struct hdac_hdmi_port {
86 struct list_head head;
88 struct hdac_hdmi_pin *pin;
90 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
91 struct hdac_hdmi_eld eld;
94 struct snd_soc_dapm_context *dapm;
95 const char *output_pin;
96 struct work_struct dapm_work;
99 struct hdac_hdmi_pcm {
100 struct list_head head;
102 struct list_head port_list;
103 struct hdac_hdmi_cvt *cvt;
104 struct snd_soc_jack *jack;
109 unsigned char chmap[8]; /* ALSA API channel-map */
112 struct snd_kcontrol *eld_ctl;
115 struct hdac_hdmi_dai_port_map {
117 struct hdac_hdmi_port *port;
118 struct hdac_hdmi_cvt *cvt;
121 struct hdac_hdmi_drv_data {
122 unsigned int vendor_nid;
125 struct hdac_hdmi_priv {
126 struct hdac_device *hdev;
127 struct snd_soc_component *component;
128 struct snd_card *card;
129 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
130 struct list_head pin_list;
131 struct list_head cvt_list;
132 struct list_head pcm_list;
136 struct mutex pin_mutex;
137 struct hdac_chmap chmap;
138 struct hdac_hdmi_drv_data *drv_data;
139 struct snd_soc_dai_driver *dai_drv;
142 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
144 static struct hdac_hdmi_pcm *
145 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
146 struct hdac_hdmi_cvt *cvt)
148 struct hdac_hdmi_pcm *pcm;
150 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
158 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
159 struct hdac_hdmi_port *port, bool is_connect)
161 struct hdac_device *hdev = port->pin->hdev;
163 port->is_connect = is_connect;
166 * Report Jack connect event when a device is connected
167 * for the first time where same PCM is attached to multiple
170 if (pcm->jack_event == 0) {
172 "jack report for pcm=%d\n",
174 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
180 * Report Jack disconnect event when a device is disconnected
181 * is the only last connected device when same PCM is attached
184 if (pcm->jack_event == 1)
185 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
186 if (pcm->jack_event > 0)
191 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
193 if (port->is_connect)
194 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
196 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
197 snd_soc_dapm_sync(port->dapm);
200 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
202 struct hdac_hdmi_port *port;
204 port = container_of(work, struct hdac_hdmi_port, dapm_work);
205 hdac_hdmi_port_dapm_update(port);
208 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
209 struct hdac_hdmi_port *port, bool is_connect)
211 hdac_hdmi_jack_report(pcm, port, is_connect);
212 hdac_hdmi_port_dapm_update(port);
215 /* MST supported verbs */
217 * Get the no devices that can be connected to a port on the Pin widget.
219 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
222 unsigned int type, param;
224 caps = get_wcaps(hdev, nid);
225 type = get_wcaps_type(caps);
227 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
230 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
234 return param & AC_DEV_LIST_LEN_MASK;
238 * Get the port entry select on the pin. Return the port entry
239 * id selected on the pin. Return 0 means the first port entry
240 * is selected or MST is not supported.
242 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
243 struct hdac_hdmi_port *port)
245 return snd_hdac_codec_read(hdev, port->pin->nid,
246 0, AC_VERB_GET_DEVICE_SEL, 0);
250 * Sets the selected port entry for the configuring Pin widget verb.
251 * returns error if port set is not equal to port get otherwise success
253 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
254 struct hdac_hdmi_port *port)
258 if (!port->pin->mst_capable)
261 /* AC_PAR_DEVLIST_LEN is 0 based. */
262 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
266 * Device List Length is a 0 based integer value indicating the
267 * number of sink device that a MST Pin Widget can support.
269 if (num_ports + 1 < port->id)
272 snd_hdac_codec_write(hdev, port->pin->nid, 0,
273 AC_VERB_SET_DEVICE_SEL, port->id);
275 if (port->id != hdac_hdmi_port_select_get(hdev, port))
278 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
283 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
286 struct hdac_hdmi_pcm *pcm;
288 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
289 if (pcm->pcm_id == pcm_idx)
296 static unsigned int sad_format(const u8 *sad)
298 return ((sad[0] >> 0x3) & 0x1f);
301 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
306 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
309 u64 formats = SNDRV_PCM_FMTBIT_S16;
311 const u8 *sad, *eld_buf = eld;
313 sad = drm_eld_sad(eld_buf);
315 goto format_constraint;
317 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
318 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
321 * the controller support 20 and 24 bits in 32 bit
322 * container so we set S32
324 if (sad_sample_bits_lpcm(sad) & 0x6)
325 formats |= SNDRV_PCM_FMTBIT_S32;
330 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
336 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
337 int packet_index, int byte_index)
341 val = (packet_index << 5) | (byte_index & 0x1f);
342 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
345 struct dp_audio_infoframe {
348 u8 ver; /* 0x11 << 2 */
350 u8 CC02_CT47; /* match with HDMI infoframe from this on */
354 u8 LFEPBL01_LSV36_DM_INH7;
357 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
358 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
360 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
361 struct hdmi_audio_infoframe frame;
362 struct hdac_hdmi_pin *pin = port->pin;
363 struct dp_audio_infoframe dp_ai;
364 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
365 struct hdac_hdmi_cvt *cvt = pcm->cvt;
373 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
374 pcm->channels, pcm->chmap_set, true, pcm->chmap);
376 channels = snd_hdac_get_active_channels(ca);
377 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
379 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
380 pcm->channels, pcm->chmap, pcm->chmap_set);
382 eld_buf = port->eld.eld_buffer;
383 conn_type = drm_eld_get_conn_type(eld_buf);
386 case DRM_ELD_CONN_TYPE_HDMI:
387 hdmi_audio_infoframe_init(&frame);
389 frame.channels = channels;
390 frame.channel_allocation = ca;
392 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
398 case DRM_ELD_CONN_TYPE_DP:
399 memset(&dp_ai, 0, sizeof(dp_ai));
402 dp_ai.ver = 0x11 << 2;
403 dp_ai.CC02_CT47 = channels - 1;
410 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
414 /* stop infoframe transmission */
415 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
416 snd_hdac_codec_write(hdev, pin->nid, 0,
417 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
420 /* Fill infoframe. Index auto-incremented */
421 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
422 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
423 for (i = 0; i < sizeof(buffer); i++)
424 snd_hdac_codec_write(hdev, pin->nid, 0,
425 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
427 for (i = 0; i < sizeof(dp_ai); i++)
428 snd_hdac_codec_write(hdev, pin->nid, 0,
429 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
432 /* Start infoframe */
433 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
434 snd_hdac_codec_write(hdev, pin->nid, 0,
435 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
440 static int hdac_hdmi_set_stream(struct snd_soc_dai *dai,
441 void *stream, int direction)
443 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
444 struct hdac_device *hdev = hdmi->hdev;
445 struct hdac_hdmi_dai_port_map *dai_map;
446 struct hdac_hdmi_pcm *pcm;
447 struct hdac_stream *hstream;
452 hstream = (struct hdac_stream *)stream;
454 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, hstream->stream_tag);
456 dai_map = &hdmi->dai_map[dai->id];
458 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
461 pcm->stream_tag = (hstream->stream_tag << 4);
466 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
469 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
470 struct hdac_hdmi_dai_port_map *dai_map;
471 struct hdac_hdmi_pcm *pcm;
475 dai_map = &hdmi->dai_map[dai->id];
477 bits = snd_hdac_stream_format_bits(params_format(hparams), SNDRV_PCM_SUBFORMAT_STD,
478 dai->driver->playback.sig_bits);
479 format = snd_hdac_stream_format(params_channels(hparams), bits, params_rate(hparams));
481 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
485 pcm->format = format;
486 pcm->channels = params_channels(hparams);
491 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
492 struct hdac_hdmi_pin *pin,
493 struct hdac_hdmi_port *port)
495 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
497 "HDMI: pin %d wcaps %#x does not support connection list\n",
498 pin->nid, get_wcaps(hdev, pin->nid));
502 if (hdac_hdmi_port_select_set(hdev, port) < 0)
505 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
506 port->mux_nids, HDA_MAX_CONNECTIONS);
507 if (port->num_mux_nids == 0)
509 "No connections found for pin:port %d:%d\n",
512 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
513 port->num_mux_nids, pin->nid, port->id);
515 return port->num_mux_nids;
519 * Query pcm list and return port to which stream is routed.
521 * Also query connection list of the pin, to validate the cvt to port map.
523 * Same stream rendering to multiple ports simultaneously can be done
524 * possibly, but not supported for now in driver. So return the first port
527 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
528 struct hdac_device *hdev,
529 struct hdac_hdmi_priv *hdmi,
530 struct hdac_hdmi_cvt *cvt)
532 struct hdac_hdmi_pcm *pcm;
533 struct hdac_hdmi_port *port;
536 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
537 if (pcm->cvt == cvt) {
538 if (list_empty(&pcm->port_list))
541 list_for_each_entry(port, &pcm->port_list, head) {
542 mutex_lock(&pcm->lock);
543 ret = hdac_hdmi_query_port_connlist(hdev,
545 mutex_unlock(&pcm->lock);
549 for (i = 0; i < port->num_mux_nids; i++) {
550 if (port->mux_nids[i] == cvt->nid &&
551 port->eld.monitor_present &&
563 * Go through all converters and ensure connection is set to
564 * the correct pin as set via kcontrols.
566 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
568 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
569 struct hdac_hdmi_port *port;
570 struct hdac_hdmi_cvt *cvt;
573 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
574 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
575 if (port && port->pin) {
576 snd_hdac_codec_write(hdev, port->pin->nid, 0,
577 AC_VERB_SET_CONNECT_SEL, cvt_idx);
578 dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
579 __func__, cvt->name, port->pin->nid, cvt_idx);
586 * This tries to get a valid pin and set the HW constraints based on the
587 * ELD. Even if a valid pin is not found return success so that device open
590 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
591 struct snd_soc_dai *dai)
593 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
594 struct hdac_device *hdev = hdmi->hdev;
595 struct hdac_hdmi_dai_port_map *dai_map;
596 struct hdac_hdmi_cvt *cvt;
597 struct hdac_hdmi_port *port;
600 dai_map = &hdmi->dai_map[dai->id];
603 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
606 * To make PA and other userland happy.
607 * userland scans devices so returning error does not help.
611 if ((!port->eld.monitor_present) ||
612 (!port->eld.eld_valid)) {
615 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
616 port->eld.monitor_present, port->eld.eld_valid,
617 port->pin->nid, port->id);
622 dai_map->port = port;
624 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
625 port->eld.eld_buffer);
629 return snd_pcm_hw_constraint_eld(substream->runtime,
630 port->eld.eld_buffer);
633 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
634 struct snd_soc_dai *dai)
636 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
637 struct hdac_hdmi_dai_port_map *dai_map;
638 struct hdac_hdmi_pcm *pcm;
640 dai_map = &hdmi->dai_map[dai->id];
642 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
645 mutex_lock(&pcm->lock);
646 pcm->chmap_set = false;
647 memset(pcm->chmap, 0, sizeof(pcm->chmap));
649 mutex_unlock(&pcm->lock);
653 dai_map->port = NULL;
657 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
660 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
663 chans = get_wcaps(hdev, cvt->nid);
664 chans = get_wcaps_channels(chans);
666 cvt->params.channels_min = 2;
668 cvt->params.channels_max = chans;
669 if (chans > hdmi->chmap.channels_max)
670 hdmi->chmap.channels_max = chans;
672 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
674 &cvt->params.formats,
676 &cvt->params.maxbps);
679 "Failed to query pcm params for nid %d: %d\n",
685 static int hdac_hdmi_fill_widget_info(struct device *dev,
686 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
687 void *priv, const char *wname, const char *stream,
688 struct snd_kcontrol_new *wc, int numkc,
689 int (*event)(struct snd_soc_dapm_widget *,
690 struct snd_kcontrol *, int), unsigned short event_flags)
693 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
698 w->reg = SND_SOC_NOPM;
700 w->kcontrol_news = wc;
701 w->num_kcontrols = numkc;
704 w->event_flags = event_flags;
709 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
710 const char *sink, const char *control, const char *src,
711 int (*handler)(struct snd_soc_dapm_widget *src,
712 struct snd_soc_dapm_widget *sink))
716 route->control = control;
717 route->connected = handler;
720 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
721 struct hdac_hdmi_port *port)
723 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
724 struct hdac_hdmi_pcm *pcm;
725 struct hdac_hdmi_port *p;
727 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
728 if (list_empty(&pcm->port_list))
731 list_for_each_entry(p, &pcm->port_list, head) {
732 if (p->id == port->id && port->pin == p->pin)
740 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
741 hda_nid_t nid, unsigned int pwr_state)
746 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
747 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
748 for (count = 0; count < 10; count++) {
749 snd_hdac_codec_read(hdev, nid, 0,
750 AC_VERB_SET_POWER_STATE,
752 state = snd_hdac_sync_power_state(hdev,
754 if (!(state & AC_PWRST_ERROR))
761 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
762 hda_nid_t nid, int val)
764 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
765 snd_hdac_codec_write(hdev, nid, 0,
766 AC_VERB_SET_AMP_GAIN_MUTE, val);
770 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
771 struct snd_kcontrol *kc, int event)
773 struct hdac_hdmi_port *port = w->priv;
774 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
775 struct hdac_hdmi_pcm *pcm;
777 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
778 __func__, w->name, event);
780 pcm = hdac_hdmi_get_pcm(hdev, port);
784 /* set the device if pin is mst_capable */
785 if (hdac_hdmi_port_select_set(hdev, port) < 0)
789 case SND_SOC_DAPM_PRE_PMU:
790 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
792 /* Enable out path for this pin widget */
793 snd_hdac_codec_write(hdev, port->pin->nid, 0,
794 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
796 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
798 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
800 case SND_SOC_DAPM_POST_PMD:
801 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
803 /* Disable out path for this pin widget */
804 snd_hdac_codec_write(hdev, port->pin->nid, 0,
805 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
807 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
815 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
816 struct snd_kcontrol *kc, int event)
818 struct hdac_hdmi_cvt *cvt = w->priv;
819 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
820 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
821 struct hdac_hdmi_pcm *pcm;
823 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
824 __func__, w->name, event);
826 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
831 case SND_SOC_DAPM_PRE_PMU:
832 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
834 /* Enable transmission */
835 snd_hdac_codec_write(hdev, cvt->nid, 0,
836 AC_VERB_SET_DIGI_CONVERT_1, 1);
838 /* Category Code (CC) to zero */
839 snd_hdac_codec_write(hdev, cvt->nid, 0,
840 AC_VERB_SET_DIGI_CONVERT_2, 0);
842 snd_hdac_codec_write(hdev, cvt->nid, 0,
843 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
844 snd_hdac_codec_write(hdev, cvt->nid, 0,
845 AC_VERB_SET_STREAM_FORMAT, pcm->format);
848 * The connection indices are shared by all converters and
849 * may interfere with each other. Ensure correct
850 * routing for all converters at stream start.
852 hdac_hdmi_verify_connect_sel_all_pins(hdev);
856 case SND_SOC_DAPM_POST_PMD:
857 snd_hdac_codec_write(hdev, cvt->nid, 0,
858 AC_VERB_SET_CHANNEL_STREAMID, 0);
859 snd_hdac_codec_write(hdev, cvt->nid, 0,
860 AC_VERB_SET_STREAM_FORMAT, 0);
862 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
870 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
871 struct snd_kcontrol *kc, int event)
873 struct hdac_hdmi_port *port = w->priv;
874 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
877 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
878 __func__, w->name, event);
881 kc = w->kcontrols[0];
883 mux_idx = dapm_kcontrol_get_value(kc);
885 /* set the device if pin is mst_capable */
886 if (hdac_hdmi_port_select_set(hdev, port) < 0)
890 snd_hdac_codec_write(hdev, port->pin->nid, 0,
891 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
898 * Based on user selection, map the PINs with the PCMs.
900 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
901 struct snd_ctl_elem_value *ucontrol)
904 struct hdac_hdmi_port *p, *p_next;
905 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
906 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
907 struct snd_soc_dapm_context *dapm = w->dapm;
908 struct hdac_hdmi_port *port = w->priv;
909 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
910 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
911 struct hdac_hdmi_pcm *pcm;
912 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
914 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
921 mutex_lock(&hdmi->pin_mutex);
922 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
923 if (list_empty(&pcm->port_list))
926 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
927 if (p == port && p->id == port->id &&
928 p->pin == port->pin) {
929 hdac_hdmi_jack_report_sync(pcm, port, false);
936 * Jack status is not reported during device probe as the
937 * PCMs are not registered by then. So report it here.
939 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
940 if (!strcmp(cvt_name, pcm->cvt->name)) {
941 list_add_tail(&port->head, &pcm->port_list);
942 if (port->eld.monitor_present && port->eld.eld_valid) {
943 hdac_hdmi_jack_report_sync(pcm, port, true);
944 mutex_unlock(&hdmi->pin_mutex);
949 mutex_unlock(&hdmi->pin_mutex);
955 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
956 * the display driver seem to be programming the connection list for the pin
959 * So programming all the possible inputs for the mux, the user has to take
960 * care of selecting the right one and leaving all other inputs selected to
963 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
964 struct hdac_hdmi_port *port,
965 struct snd_soc_dapm_widget *widget,
966 const char *widget_name)
968 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
969 struct hdac_hdmi_pin *pin = port->pin;
970 struct snd_kcontrol_new *kc;
971 struct hdac_hdmi_cvt *cvt;
973 char kc_name[NAME_SIZE];
974 char mux_items[NAME_SIZE];
975 /* To hold inputs to the Pin mux */
976 char *items[HDA_MAX_CONNECTIONS];
978 int num_items = hdmi->num_cvt + 1;
980 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
984 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
988 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
990 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
994 kc->private_value = (long)se;
995 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
997 kc->info = snd_soc_info_enum_double;
998 kc->put = hdac_hdmi_set_pin_port_mux;
999 kc->get = snd_soc_dapm_get_enum_double;
1001 se->reg = SND_SOC_NOPM;
1003 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
1004 se->items = num_items;
1005 se->mask = roundup_pow_of_two(se->items) - 1;
1007 sprintf(mux_items, "NONE");
1008 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1012 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1014 sprintf(mux_items, "cvt %d", cvt->nid);
1015 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1020 se->texts = devm_kmemdup(&hdev->dev, items,
1021 (num_items * sizeof(char *)), GFP_KERNEL);
1025 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1026 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1027 hdac_hdmi_pin_mux_widget_event,
1028 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
1031 /* Add cvt <- input <- mux route map */
1032 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
1033 struct snd_soc_dapm_widget *widgets,
1034 struct snd_soc_dapm_route *route, int rindex)
1036 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1037 const struct snd_kcontrol_new *kc;
1038 struct soc_enum *se;
1039 int mux_index = hdmi->num_cvt + hdmi->num_ports;
1042 for (i = 0; i < hdmi->num_ports; i++) {
1043 kc = widgets[mux_index].kcontrol_news;
1044 se = (struct soc_enum *)kc->private_value;
1045 for (j = 0; j < hdmi->num_cvt; j++) {
1046 hdac_hdmi_fill_route(&route[rindex],
1047 widgets[mux_index].name,
1049 widgets[j].name, NULL);
1059 * Widgets are added in the below sequence
1060 * Converter widgets for num converters enumerated
1061 * Pin-port widgets for num ports for Pins enumerated
1062 * Pin-port mux widgets to represent connenction list of pin widget
1064 * For each port, one Mux and One output widget is added
1065 * Total widgets elements = num_cvt + (num_ports * 2);
1067 * Routes are added as below:
1068 * pin-port mux -> pin (based on num_ports)
1069 * cvt -> "Input sel control" -> pin-port_mux
1071 * Total route elements:
1072 * num_ports + (pin_muxes * num_cvt)
1074 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1076 struct snd_soc_dapm_widget *widgets;
1077 struct snd_soc_dapm_route *route;
1078 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1079 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1080 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1081 char widget_name[NAME_SIZE];
1082 struct hdac_hdmi_cvt *cvt;
1083 struct hdac_hdmi_pin *pin;
1084 int ret, i = 0, num_routes = 0, j;
1086 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1089 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1090 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1096 /* DAPM widgets to represent each converter widget */
1097 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1098 sprintf(widget_name, "Converter %d", cvt->nid);
1099 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1100 snd_soc_dapm_aif_in, cvt,
1101 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1102 hdac_hdmi_cvt_output_widget_event,
1103 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1109 list_for_each_entry(pin, &hdmi->pin_list, head) {
1110 for (j = 0; j < pin->num_ports; j++) {
1111 sprintf(widget_name, "hif%d-%d Output",
1112 pin->nid, pin->ports[j].id);
1113 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1114 snd_soc_dapm_output, &pin->ports[j],
1115 widget_name, NULL, NULL, 0,
1116 hdac_hdmi_pin_output_widget_event,
1117 SND_SOC_DAPM_PRE_PMU |
1118 SND_SOC_DAPM_POST_PMD);
1121 pin->ports[j].output_pin = widgets[i].name;
1126 /* DAPM widgets to represent the connection list to pin widget */
1127 list_for_each_entry(pin, &hdmi->pin_list, head) {
1128 for (j = 0; j < pin->num_ports; j++) {
1129 sprintf(widget_name, "Pin%d-Port%d Mux",
1130 pin->nid, pin->ports[j].id);
1131 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1132 &pin->ports[j], &widgets[i],
1138 /* For cvt to pin_mux mapping */
1139 num_routes += hdmi->num_cvt;
1141 /* For pin_mux to pin mapping */
1146 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1152 /* Add pin <- NULL <- mux route map */
1153 list_for_each_entry(pin, &hdmi->pin_list, head) {
1154 for (j = 0; j < pin->num_ports; j++) {
1155 int sink_index = i + hdmi->num_cvt;
1156 int src_index = sink_index + pin->num_ports *
1159 hdac_hdmi_fill_route(&route[i],
1160 widgets[sink_index].name, NULL,
1161 widgets[src_index].name, NULL);
1166 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1168 snd_soc_dapm_new_controls(dapm, widgets,
1169 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1171 snd_soc_dapm_add_routes(dapm, route, num_routes);
1172 snd_soc_dapm_new_widgets(dapm->card);
1178 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1180 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1181 struct hdac_hdmi_dai_port_map *dai_map;
1182 struct hdac_hdmi_cvt *cvt;
1185 if (list_empty(&hdmi->cvt_list))
1188 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1189 dai_map = &hdmi->dai_map[dai_id];
1190 dai_map->dai_id = dai_id;
1195 if (dai_id == HDA_MAX_CVTS) {
1196 dev_warn(&hdev->dev,
1197 "Max dais supported: %d\n", dai_id);
1205 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1207 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1208 struct hdac_hdmi_cvt *cvt;
1209 char name[NAME_SIZE];
1211 cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1216 sprintf(name, "cvt %d", cvt->nid);
1217 cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1221 list_add_tail(&cvt->head, &hdmi->cvt_list);
1224 return hdac_hdmi_query_cvt_params(hdev, cvt);
1227 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1228 struct hdac_hdmi_port *port)
1230 unsigned int ver, mnl;
1232 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1233 >> DRM_ELD_VER_SHIFT;
1235 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1236 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1240 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1241 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1243 if (mnl > ELD_MAX_MNL) {
1244 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1248 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1253 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1254 struct hdac_hdmi_port *port)
1256 struct hdac_device *hdev = pin->hdev;
1257 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1258 struct hdac_hdmi_pcm *pcm;
1261 bool eld_valid, eld_changed;
1267 * In case of non MST pin, get_eld info API expectes port
1270 mutex_lock(&hdmi->pin_mutex);
1271 port->eld.monitor_present = false;
1273 if (pin->mst_capable)
1276 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1277 &port->eld.monitor_present,
1278 port->eld.eld_buffer,
1282 size = min(size, ELD_MAX_SIZE);
1283 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1287 eld_valid = port->eld.eld_valid;
1290 port->eld.eld_valid = true;
1291 port->eld.eld_size = size;
1293 port->eld.eld_valid = false;
1294 port->eld.eld_size = 0;
1297 eld_changed = (eld_valid != port->eld.eld_valid);
1299 pcm = hdac_hdmi_get_pcm(hdev, port);
1301 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1303 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1304 __func__, pin->nid, port->id);
1307 * PCMs are not registered during device probe, so don't
1308 * report jack here. It will be done in usermode mux
1312 hdac_hdmi_jack_report(pcm, port, false);
1313 schedule_work(&port->dapm_work);
1316 mutex_unlock(&hdmi->pin_mutex);
1320 if (port->eld.monitor_present && port->eld.eld_valid) {
1322 hdac_hdmi_jack_report(pcm, port, true);
1323 schedule_work(&port->dapm_work);
1326 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1327 port->eld.eld_buffer, port->eld.eld_size, false);
1330 mutex_unlock(&hdmi->pin_mutex);
1332 if (eld_changed && pcm)
1333 snd_ctl_notify(hdmi->card,
1334 SNDRV_CTL_EVENT_MASK_VALUE |
1335 SNDRV_CTL_EVENT_MASK_INFO,
1339 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1340 struct hdac_hdmi_pin *pin)
1342 struct hdac_hdmi_port *ports;
1343 int max_ports = HDA_MAX_PORTS;
1347 * FIXME: max_port may vary for each platform, so pass this as
1348 * as driver data or query from i915 interface when this API is
1352 ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1356 for (i = 0; i < max_ports; i++) {
1359 INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1362 pin->num_ports = max_ports;
1366 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1368 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1369 struct hdac_hdmi_pin *pin;
1372 pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1377 pin->mst_capable = false;
1379 ret = hdac_hdmi_add_ports(hdev, pin);
1383 list_add_tail(&pin->head, &hdmi->pin_list);
1385 hdmi->num_ports += pin->num_ports;
1390 #define INTEL_VENDOR_NID 0x08
1391 #define INTEL_GLK_VENDOR_NID 0x0b
1392 #define INTEL_GET_VENDOR_VERB 0xf81
1393 #define INTEL_SET_VENDOR_VERB 0x781
1394 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1395 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1397 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1399 unsigned int vendor_param;
1400 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1401 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1403 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1404 INTEL_GET_VENDOR_VERB, 0);
1405 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1408 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1409 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1410 INTEL_SET_VENDOR_VERB, vendor_param);
1411 if (vendor_param == -1)
1415 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1417 unsigned int vendor_param;
1418 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1419 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1421 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1422 INTEL_GET_VENDOR_VERB, 0);
1423 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1426 /* enable DP1.2 mode */
1427 vendor_param |= INTEL_EN_DP12;
1428 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1429 INTEL_SET_VENDOR_VERB, vendor_param);
1430 if (vendor_param == -1)
1435 static int hdac_hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
1436 struct snd_ctl_elem_info *uinfo)
1438 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1439 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1440 struct hdac_hdmi_pcm *pcm;
1441 struct hdac_hdmi_port *port;
1442 struct hdac_hdmi_eld *eld;
1444 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1447 pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1449 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1450 kcontrol->id.device);
1454 if (list_empty(&pcm->port_list)) {
1455 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1456 __func__, kcontrol->id.device);
1460 mutex_lock(&hdmi->pin_mutex);
1462 list_for_each_entry(port, &pcm->port_list, head) {
1465 if (eld->eld_valid) {
1466 uinfo->count = eld->eld_size;
1471 mutex_unlock(&hdmi->pin_mutex);
1476 static int hdac_hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
1477 struct snd_ctl_elem_value *ucontrol)
1479 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1480 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1481 struct hdac_hdmi_pcm *pcm;
1482 struct hdac_hdmi_port *port;
1483 struct hdac_hdmi_eld *eld;
1485 memset(ucontrol->value.bytes.data, 0, sizeof(ucontrol->value.bytes.data));
1487 pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1489 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1490 kcontrol->id.device);
1494 if (list_empty(&pcm->port_list)) {
1495 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1496 __func__, kcontrol->id.device);
1500 mutex_lock(&hdmi->pin_mutex);
1502 list_for_each_entry(port, &pcm->port_list, head) {
1505 if (!eld->eld_valid)
1508 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
1509 eld->eld_size > ELD_MAX_SIZE) {
1510 mutex_unlock(&hdmi->pin_mutex);
1512 dev_err(component->dev, "%s: buffer too small, device %d eld_size %d\n",
1513 __func__, kcontrol->id.device, eld->eld_size);
1518 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
1523 mutex_unlock(&hdmi->pin_mutex);
1528 static int hdac_hdmi_create_eld_ctl(struct snd_soc_component *component, struct hdac_hdmi_pcm *pcm)
1530 struct snd_kcontrol *kctl;
1531 struct snd_kcontrol_new hdmi_eld_ctl = {
1532 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1533 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1534 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1536 .info = hdac_hdmi_eld_ctl_info,
1537 .get = hdac_hdmi_eld_ctl_get,
1538 .device = pcm->pcm_id,
1541 /* add ELD ctl with the device number corresponding to the PCM stream */
1542 kctl = snd_ctl_new1(&hdmi_eld_ctl, component);
1546 pcm->eld_ctl = kctl;
1548 return snd_ctl_add(component->card->snd_card, kctl);
1551 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1552 .startup = hdac_hdmi_pcm_open,
1553 .shutdown = hdac_hdmi_pcm_close,
1554 .hw_params = hdac_hdmi_set_hw_params,
1555 .set_stream = hdac_hdmi_set_stream,
1559 * Each converter can support a stream independently. So a dai is created
1560 * based on the number of converter queried.
1562 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1563 struct snd_soc_dai_driver **dais,
1564 struct hdac_hdmi_priv *hdmi, int num_dais)
1566 struct snd_soc_dai_driver *hdmi_dais;
1567 struct hdac_hdmi_cvt *cvt;
1568 char name[NAME_SIZE], dai_name[NAME_SIZE];
1571 unsigned int rate_max = 384000, rate_min = 8000;
1575 hdmi_dais = devm_kzalloc(&hdev->dev,
1576 (sizeof(*hdmi_dais) * num_dais),
1581 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1582 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1583 &rates, &formats, NULL, &bps);
1587 /* Filter out 44.1, 88.2 and 176.4Khz */
1588 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1589 SNDRV_PCM_RATE_176400);
1593 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1594 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1595 dai_name, GFP_KERNEL);
1597 if (!hdmi_dais[i].name)
1600 snprintf(name, sizeof(name), "hifi%d", i+1);
1601 hdmi_dais[i].playback.stream_name =
1602 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1603 if (!hdmi_dais[i].playback.stream_name)
1607 * Set caps based on capability queried from the converter.
1608 * It will be constrained runtime based on ELD queried.
1610 hdmi_dais[i].playback.formats = formats;
1611 hdmi_dais[i].playback.rates = rates;
1612 hdmi_dais[i].playback.rate_max = rate_max;
1613 hdmi_dais[i].playback.rate_min = rate_min;
1614 hdmi_dais[i].playback.channels_min = 2;
1615 hdmi_dais[i].playback.channels_max = 2;
1616 hdmi_dais[i].playback.sig_bits = bps;
1617 hdmi_dais[i].ops = &hdmi_dai_ops;
1622 hdmi->dai_drv = hdmi_dais;
1628 * Parse all nodes and store the cvt/pin nids in array
1629 * Add one time initialization for pin and cvt widgets
1631 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1632 struct snd_soc_dai_driver **dais, int *num_dais)
1636 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1639 hdac_hdmi_skl_enable_all_pins(hdev);
1640 hdac_hdmi_skl_enable_dp12(hdev);
1642 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1643 if (!nid || num_nodes <= 0) {
1644 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1648 for (i = 0; i < num_nodes; i++, nid++) {
1652 caps = get_wcaps(hdev, nid);
1653 type = get_wcaps_type(caps);
1655 if (!(caps & AC_WCAP_DIGITAL))
1660 case AC_WID_AUD_OUT:
1661 ret = hdac_hdmi_add_cvt(hdev, nid);
1667 ret = hdac_hdmi_add_pin(hdev, nid);
1674 if (!hdmi->num_pin || !hdmi->num_cvt) {
1676 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1680 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1682 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1687 *num_dais = hdmi->num_cvt;
1688 ret = hdac_hdmi_init_dai_map(hdev);
1690 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1695 static int hdac_hdmi_pin2port(void *aptr, int pin)
1697 return pin - 4; /* map NID 0x05 -> port #1 */
1700 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1702 struct hdac_device *hdev = aptr;
1703 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1704 struct hdac_hdmi_pin *pin;
1705 struct hdac_hdmi_port *hport = NULL;
1706 struct snd_soc_component *component = hdmi->component;
1709 /* Don't know how this mapping is derived */
1710 hda_nid_t pin_nid = port + 0x04;
1712 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1716 * skip notification during system suspend (but not in runtime PM);
1717 * the state will be updated at resume. Also since the ELD and
1718 * connection states are updated in anyway at the end of the resume,
1719 * we can skip it when received during PM process.
1721 if (snd_power_get_state(component->card->snd_card) !=
1725 if (atomic_read(&hdev->in_pm))
1728 list_for_each_entry(pin, &hdmi->pin_list, head) {
1729 if (pin->nid != pin_nid)
1732 /* In case of non MST pin, pipe is -1 */
1734 pin->mst_capable = false;
1735 /* if not MST, default is port[0] */
1736 hport = &pin->ports[0];
1738 for (i = 0; i < pin->num_ports; i++) {
1739 pin->mst_capable = true;
1740 if (pin->ports[i].id == pipe) {
1741 hport = &pin->ports[i];
1748 hdac_hdmi_present_sense(pin, hport);
1753 static struct drm_audio_component_audio_ops aops = {
1754 .pin2port = hdac_hdmi_pin2port,
1755 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1758 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1761 struct snd_soc_pcm_runtime *rtd;
1763 for_each_card_rtds(card, rtd) {
1764 if (rtd->pcm && (rtd->pcm->device == device))
1771 /* create jack pin kcontrols */
1772 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1773 struct hdac_device *hdev)
1775 struct hdac_hdmi_pin *pin;
1776 struct snd_kcontrol_new *kc;
1779 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1780 struct snd_soc_component *component = hdmi->component;
1782 kc = devm_kcalloc(component->dev, hdmi->num_ports,
1783 sizeof(*kc), GFP_KERNEL);
1788 list_for_each_entry(pin, &hdmi->pin_list, head) {
1789 for (j = 0; j < pin->num_ports; j++) {
1790 name = devm_kasprintf(component->dev, GFP_KERNEL,
1792 pin->nid, pin->ports[j].id);
1796 kc[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
1801 kc[i].private_value = (unsigned long)name;
1802 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1804 kc[i].info = snd_soc_dapm_info_pin_switch;
1805 kc[i].put = snd_soc_dapm_put_pin_switch;
1806 kc[i].get = snd_soc_dapm_get_pin_switch;
1811 return snd_soc_add_card_controls(card, kc, i);
1814 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1815 struct snd_soc_dapm_context *dapm)
1817 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1818 struct hdac_device *hdev = hdmi->hdev;
1819 struct hdac_hdmi_pin *pin;
1820 struct snd_soc_dapm_widget *widgets;
1821 struct snd_soc_dapm_route *route;
1822 char w_name[NAME_SIZE];
1825 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1826 sizeof(*widgets), GFP_KERNEL);
1831 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1832 sizeof(*route), GFP_KERNEL);
1836 /* create Jack DAPM widget */
1837 list_for_each_entry(pin, &hdmi->pin_list, head) {
1838 for (j = 0; j < pin->num_ports; j++) {
1839 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1840 pin->nid, pin->ports[j].id);
1842 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1843 snd_soc_dapm_spk, NULL,
1844 w_name, NULL, NULL, 0, NULL, 0);
1848 pin->ports[j].jack_pin = widgets[i].name;
1849 pin->ports[j].dapm = dapm;
1851 /* add to route from Jack widget to output */
1852 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1853 NULL, pin->ports[j].output_pin, NULL);
1859 /* Add Route from Jack widget to the output widget */
1860 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1864 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1868 ret = snd_soc_dapm_new_widgets(dapm->card);
1872 /* Add Jack Pin switch Kcontrol */
1873 ret = create_fill_jack_kcontrols(dapm->card, hdev);
1878 /* default set the Jack Pin switch to OFF */
1879 list_for_each_entry(pin, &hdmi->pin_list, head) {
1880 for (j = 0; j < pin->num_ports; j++)
1881 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1882 pin->ports[j].jack_pin);
1887 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1889 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1890 struct snd_soc_jack *jack)
1892 struct snd_soc_component *component = dai->component;
1893 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1894 struct hdac_device *hdev = hdmi->hdev;
1895 struct hdac_hdmi_pcm *pcm;
1896 struct snd_pcm *snd_pcm;
1900 * this is a new PCM device, create new pcm and
1901 * add to the pcm list
1903 pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1906 pcm->pcm_id = device;
1907 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1908 pcm->jack_event = 0;
1910 mutex_init(&pcm->lock);
1911 INIT_LIST_HEAD(&pcm->port_list);
1912 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1914 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1917 "chmap control add failed with err: %d for pcm: %d\n",
1923 /* add control for ELD Bytes */
1924 err = hdac_hdmi_create_eld_ctl(component, pcm);
1927 "eld control add failed with err: %d for pcm: %d\n",
1932 list_add_tail(&pcm->head, &hdmi->pcm_list);
1936 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1938 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1939 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1942 struct hdac_hdmi_pin *pin;
1944 list_for_each_entry(pin, &hdmi->pin_list, head) {
1945 if (detect_pin_caps) {
1947 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1948 pin->mst_capable = false;
1950 pin->mst_capable = true;
1953 for (i = 0; i < pin->num_ports; i++) {
1954 if (!pin->mst_capable && i > 0)
1957 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1962 static int hdmi_codec_probe(struct snd_soc_component *component)
1964 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1965 struct hdac_device *hdev = hdmi->hdev;
1966 struct snd_soc_dapm_context *dapm =
1967 snd_soc_component_get_dapm(component);
1968 struct hdac_ext_link *hlink;
1971 hdmi->component = component;
1974 * hold the ref while we probe, also no need to drop the ref on
1975 * exit, we call pm_runtime_suspend() so that will do for us
1977 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1979 dev_err(&hdev->dev, "hdac link not found\n");
1983 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1985 ret = create_fill_widget_route_map(dapm);
1989 aops.audio_ptr = hdev;
1990 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1992 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1996 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1997 /* Imp: Store the card pointer in hda_codec */
1998 hdmi->card = dapm->card->snd_card;
2001 * Setup a device_link between card device and HDMI codec device.
2002 * The card device is the consumer and the HDMI codec device is
2003 * the supplier. With this setting, we can make sure that the audio
2004 * domain in display power will be always turned on before operating
2005 * on the HDMI audio codec registers.
2006 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
2007 * sure the device link is freed when the machine driver is removed.
2009 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
2010 DL_FLAG_AUTOREMOVE_CONSUMER);
2012 * hdac_device core already sets the state to active and calls
2013 * get_noresume. So enable runtime and set the device to suspend.
2015 pm_runtime_enable(&hdev->dev);
2016 pm_runtime_put(&hdev->dev);
2017 pm_runtime_suspend(&hdev->dev);
2022 static void hdmi_codec_remove(struct snd_soc_component *component)
2024 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
2025 struct hdac_device *hdev = hdmi->hdev;
2028 ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
2030 dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
2033 pm_runtime_disable(&hdev->dev);
2036 #ifdef CONFIG_PM_SLEEP
2037 static int hdmi_codec_resume(struct device *dev)
2039 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2040 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2043 ret = pm_runtime_force_resume(dev);
2047 * As the ELD notify callback request is not entertained while the
2048 * device is in suspend state. Need to manually check detection of
2049 * all pins here. pin capablity change is not support, so use the
2050 * already set pin caps.
2052 * NOTE: this is safe to call even if the codec doesn't actually resume.
2053 * The pin check involves only with DRM audio component hooks, so it
2054 * works even if the HD-audio side is still dreaming peacefully.
2056 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
2060 #define hdmi_codec_resume NULL
2063 static const struct snd_soc_component_driver hdmi_hda_codec = {
2064 .probe = hdmi_codec_probe,
2065 .remove = hdmi_codec_remove,
2066 .use_pmdown_time = 1,
2070 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
2071 unsigned char *chmap)
2073 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2074 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2076 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
2079 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
2080 unsigned char *chmap, int prepared)
2082 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2083 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2084 struct hdac_hdmi_port *port;
2089 if (list_empty(&pcm->port_list))
2092 mutex_lock(&pcm->lock);
2093 pcm->chmap_set = true;
2094 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
2095 list_for_each_entry(port, &pcm->port_list, head)
2097 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
2098 mutex_unlock(&pcm->lock);
2101 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
2103 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2104 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2109 if (list_empty(&pcm->port_list))
2115 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
2117 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2118 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2119 struct hdac_hdmi_port *port;
2124 if (list_empty(&pcm->port_list))
2127 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
2129 if (!port || !port->eld.eld_valid)
2132 return port->eld.info.spk_alloc;
2135 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
2136 .vendor_nid = INTEL_GLK_VENDOR_NID,
2139 static struct hdac_hdmi_drv_data intel_drv_data = {
2140 .vendor_nid = INTEL_VENDOR_NID,
2143 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
2145 struct hdac_hdmi_priv *hdmi_priv;
2146 struct snd_soc_dai_driver *hdmi_dais = NULL;
2147 struct hdac_ext_link *hlink;
2150 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2151 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
2153 /* hold the ref while we probe */
2154 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
2156 dev_err(&hdev->dev, "hdac link not found\n");
2160 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2162 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2163 if (hdmi_priv == NULL)
2166 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2167 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2168 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2169 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2170 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2171 hdmi_priv->hdev = hdev;
2176 if (hdac_id->driver_data)
2177 hdmi_priv->drv_data =
2178 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2180 hdmi_priv->drv_data = &intel_drv_data;
2182 dev_set_drvdata(&hdev->dev, hdmi_priv);
2184 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2185 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2186 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2187 mutex_init(&hdmi_priv->pin_mutex);
2190 * Turned off in the runtime_suspend during the first explicit
2191 * pm_runtime_suspend call.
2193 snd_hdac_display_power(hdev->bus, hdev->addr, true);
2195 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2198 "Failed in parse and map nid with err: %d\n", ret);
2201 snd_hdac_refresh_widgets(hdev);
2203 /* ASoC specific initialization */
2204 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2205 hdmi_dais, num_dais);
2207 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2212 static void clear_dapm_works(struct hdac_device *hdev)
2214 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2215 struct hdac_hdmi_pin *pin;
2218 list_for_each_entry(pin, &hdmi->pin_list, head)
2219 for (i = 0; i < pin->num_ports; i++)
2220 cancel_work_sync(&pin->ports[i].dapm_work);
2223 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2225 clear_dapm_works(hdev);
2226 snd_hdac_display_power(hdev->bus, hdev->addr, false);
2232 static int hdac_hdmi_runtime_suspend(struct device *dev)
2234 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2235 struct hdac_bus *bus = hdev->bus;
2236 struct hdac_ext_link *hlink;
2238 dev_dbg(dev, "Enter: %s\n", __func__);
2240 /* controller may not have been initialized for the first time */
2246 * codec_read is preferred over codec_write to set the power state.
2247 * This way verb is send to set the power state and response
2248 * is received. So setting power state is ensured without using loop
2249 * to read the state.
2251 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2254 hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2256 dev_err(dev, "hdac link not found\n");
2260 snd_hdac_codec_link_down(hdev);
2261 snd_hdac_ext_bus_link_put(bus, hlink);
2263 snd_hdac_display_power(bus, hdev->addr, false);
2268 static int hdac_hdmi_runtime_resume(struct device *dev)
2270 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2271 struct hdac_bus *bus = hdev->bus;
2272 struct hdac_ext_link *hlink;
2274 dev_dbg(dev, "Enter: %s\n", __func__);
2276 /* controller may not have been initialized for the first time */
2280 hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2282 dev_err(dev, "hdac link not found\n");
2286 snd_hdac_ext_bus_link_get(bus, hlink);
2287 snd_hdac_codec_link_up(hdev);
2289 snd_hdac_display_power(bus, hdev->addr, true);
2291 hdac_hdmi_skl_enable_all_pins(hdev);
2292 hdac_hdmi_skl_enable_dp12(hdev);
2295 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2301 #define hdac_hdmi_runtime_suspend NULL
2302 #define hdac_hdmi_runtime_resume NULL
2305 static const struct dev_pm_ops hdac_hdmi_pm = {
2306 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2307 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2310 static const struct hda_device_id hdmi_list[] = {
2311 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2312 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2313 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2314 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2315 &intel_glk_drv_data),
2316 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2317 &intel_glk_drv_data),
2321 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2323 static struct hdac_driver hdmi_driver = {
2325 .name = "HDMI HDA Codec",
2326 .pm = &hdac_hdmi_pm,
2328 .id_table = hdmi_list,
2329 .probe = hdac_hdmi_dev_probe,
2330 .remove = hdac_hdmi_dev_remove,
2333 static int __init hdmi_init(void)
2335 return snd_hda_ext_driver_register(&hdmi_driver);
2338 static void __exit hdmi_exit(void)
2340 snd_hda_ext_driver_unregister(&hdmi_driver);
2343 module_init(hdmi_init);
2344 module_exit(hdmi_exit);
2346 MODULE_LICENSE("GPL v2");
2347 MODULE_DESCRIPTION("HDMI HD codec");