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;
124 struct hdac_hdmi_drv_data {
125 unsigned int vendor_nid;
128 struct hdac_hdmi_priv {
129 struct hdac_device *hdev;
130 struct snd_soc_component *component;
131 struct snd_card *card;
132 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
133 struct list_head pin_list;
134 struct list_head cvt_list;
135 struct list_head pcm_list;
139 struct mutex pin_mutex;
140 struct hdac_chmap chmap;
141 struct hdac_hdmi_drv_data *drv_data;
142 struct snd_soc_dai_driver *dai_drv;
145 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
147 static struct hdac_hdmi_pcm *
148 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
149 struct hdac_hdmi_cvt *cvt)
151 struct hdac_hdmi_pcm *pcm = NULL;
153 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
161 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
162 struct hdac_hdmi_port *port, bool is_connect)
164 struct hdac_device *hdev = port->pin->hdev;
167 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
169 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
173 * Report Jack connect event when a device is connected
174 * for the first time where same PCM is attached to multiple
177 if (pcm->jack_event == 0) {
179 "jack report for pcm=%d\n",
181 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
187 * Report Jack disconnect event when a device is disconnected
188 * is the only last connected device when same PCM is attached
191 if (pcm->jack_event == 1)
192 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
193 if (pcm->jack_event > 0)
197 snd_soc_dapm_sync(port->dapm);
200 /* MST supported verbs */
202 * Get the no devices that can be connected to a port on the Pin widget.
204 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
207 unsigned int type, param;
209 caps = get_wcaps(hdev, nid);
210 type = get_wcaps_type(caps);
212 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
215 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
219 return param & AC_DEV_LIST_LEN_MASK;
223 * Get the port entry select on the pin. Return the port entry
224 * id selected on the pin. Return 0 means the first port entry
225 * is selected or MST is not supported.
227 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
228 struct hdac_hdmi_port *port)
230 return snd_hdac_codec_read(hdev, port->pin->nid,
231 0, AC_VERB_GET_DEVICE_SEL, 0);
235 * Sets the selected port entry for the configuring Pin widget verb.
236 * returns error if port set is not equal to port get otherwise success
238 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
239 struct hdac_hdmi_port *port)
243 if (!port->pin->mst_capable)
246 /* AC_PAR_DEVLIST_LEN is 0 based. */
247 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
251 * Device List Length is a 0 based integer value indicating the
252 * number of sink device that a MST Pin Widget can support.
254 if (num_ports + 1 < port->id)
257 snd_hdac_codec_write(hdev, port->pin->nid, 0,
258 AC_VERB_SET_DEVICE_SEL, port->id);
260 if (port->id != hdac_hdmi_port_select_get(hdev, port))
263 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
268 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
271 struct hdac_hdmi_pcm *pcm;
273 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
274 if (pcm->pcm_id == pcm_idx)
281 static unsigned int sad_format(const u8 *sad)
283 return ((sad[0] >> 0x3) & 0x1f);
286 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
291 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
294 u64 formats = SNDRV_PCM_FMTBIT_S16;
296 const u8 *sad, *eld_buf = eld;
298 sad = drm_eld_sad(eld_buf);
300 goto format_constraint;
302 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
303 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
306 * the controller support 20 and 24 bits in 32 bit
307 * container so we set S32
309 if (sad_sample_bits_lpcm(sad) & 0x6)
310 formats |= SNDRV_PCM_FMTBIT_S32;
315 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
321 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
322 int packet_index, int byte_index)
326 val = (packet_index << 5) | (byte_index & 0x1f);
327 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
330 struct dp_audio_infoframe {
333 u8 ver; /* 0x11 << 2 */
335 u8 CC02_CT47; /* match with HDMI infoframe from this on */
339 u8 LFEPBL01_LSV36_DM_INH7;
342 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
343 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
345 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
346 struct hdmi_audio_infoframe frame;
347 struct hdac_hdmi_pin *pin = port->pin;
348 struct dp_audio_infoframe dp_ai;
349 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
350 struct hdac_hdmi_cvt *cvt = pcm->cvt;
358 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
359 pcm->channels, pcm->chmap_set, true, pcm->chmap);
361 channels = snd_hdac_get_active_channels(ca);
362 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
364 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
365 pcm->channels, pcm->chmap, pcm->chmap_set);
367 eld_buf = port->eld.eld_buffer;
368 conn_type = drm_eld_get_conn_type(eld_buf);
371 case DRM_ELD_CONN_TYPE_HDMI:
372 hdmi_audio_infoframe_init(&frame);
374 frame.channels = channels;
375 frame.channel_allocation = ca;
377 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
383 case DRM_ELD_CONN_TYPE_DP:
384 memset(&dp_ai, 0, sizeof(dp_ai));
387 dp_ai.ver = 0x11 << 2;
388 dp_ai.CC02_CT47 = channels - 1;
395 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
399 /* stop infoframe transmission */
400 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
401 snd_hdac_codec_write(hdev, pin->nid, 0,
402 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
405 /* Fill infoframe. Index auto-incremented */
406 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
407 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
408 for (i = 0; i < sizeof(buffer); i++)
409 snd_hdac_codec_write(hdev, pin->nid, 0,
410 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
412 for (i = 0; i < sizeof(dp_ai); i++)
413 snd_hdac_codec_write(hdev, pin->nid, 0,
414 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
417 /* Start infoframe */
418 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
419 snd_hdac_codec_write(hdev, pin->nid, 0,
420 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
425 static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
426 unsigned int tx_mask, unsigned int rx_mask,
427 int slots, int slot_width)
429 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
430 struct hdac_device *hdev = hdmi->hdev;
431 struct hdac_hdmi_dai_port_map *dai_map;
432 struct hdac_hdmi_pcm *pcm;
434 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
436 dai_map = &hdmi->dai_map[dai->id];
438 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
441 pcm->stream_tag = (tx_mask << 4);
446 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
449 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
450 struct hdac_device *hdev = hdmi->hdev;
451 struct hdac_hdmi_dai_port_map *dai_map;
452 struct hdac_hdmi_port *port;
453 struct hdac_hdmi_pcm *pcm;
456 dai_map = &hdmi->dai_map[dai->id];
457 port = dai_map->port;
462 if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
464 "device is not configured for this pin:port%d:%d\n",
465 port->pin->nid, port->id);
469 format = snd_hdac_calc_stream_format(params_rate(hparams),
470 params_channels(hparams), params_format(hparams),
471 dai->driver->playback.sig_bits, 0);
473 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
477 pcm->format = format;
478 pcm->channels = params_channels(hparams);
483 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
484 struct hdac_hdmi_pin *pin,
485 struct hdac_hdmi_port *port)
487 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
489 "HDMI: pin %d wcaps %#x does not support connection list\n",
490 pin->nid, get_wcaps(hdev, pin->nid));
494 if (hdac_hdmi_port_select_set(hdev, port) < 0)
497 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
498 port->mux_nids, HDA_MAX_CONNECTIONS);
499 if (port->num_mux_nids == 0)
501 "No connections found for pin:port %d:%d\n",
504 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
505 port->num_mux_nids, pin->nid, port->id);
507 return port->num_mux_nids;
511 * Query pcm list and return port to which stream is routed.
513 * Also query connection list of the pin, to validate the cvt to port map.
515 * Same stream rendering to multiple ports simultaneously can be done
516 * possibly, but not supported for now in driver. So return the first port
519 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
520 struct hdac_device *hdev,
521 struct hdac_hdmi_priv *hdmi,
522 struct hdac_hdmi_cvt *cvt)
524 struct hdac_hdmi_pcm *pcm;
525 struct hdac_hdmi_port *port = NULL;
528 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
529 if (pcm->cvt == cvt) {
530 if (list_empty(&pcm->port_list))
533 list_for_each_entry(port, &pcm->port_list, head) {
534 mutex_lock(&pcm->lock);
535 ret = hdac_hdmi_query_port_connlist(hdev,
537 mutex_unlock(&pcm->lock);
541 for (i = 0; i < port->num_mux_nids; i++) {
542 if (port->mux_nids[i] == cvt->nid &&
543 port->eld.monitor_present &&
555 * This tries to get a valid pin and set the HW constraints based on the
556 * ELD. Even if a valid pin is not found return success so that device open
559 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
560 struct snd_soc_dai *dai)
562 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
563 struct hdac_device *hdev = hdmi->hdev;
564 struct hdac_hdmi_dai_port_map *dai_map;
565 struct hdac_hdmi_cvt *cvt;
566 struct hdac_hdmi_port *port;
569 dai_map = &hdmi->dai_map[dai->id];
572 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
575 * To make PA and other userland happy.
576 * userland scans devices so returning error does not help.
580 if ((!port->eld.monitor_present) ||
581 (!port->eld.eld_valid)) {
584 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
585 port->eld.monitor_present, port->eld.eld_valid,
586 port->pin->nid, port->id);
591 dai_map->port = port;
593 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
594 port->eld.eld_buffer);
598 return snd_pcm_hw_constraint_eld(substream->runtime,
599 port->eld.eld_buffer);
602 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
603 struct snd_soc_dai *dai)
605 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
606 struct hdac_hdmi_dai_port_map *dai_map;
607 struct hdac_hdmi_pcm *pcm;
609 dai_map = &hdmi->dai_map[dai->id];
611 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
614 mutex_lock(&pcm->lock);
615 pcm->chmap_set = false;
616 memset(pcm->chmap, 0, sizeof(pcm->chmap));
618 mutex_unlock(&pcm->lock);
622 dai_map->port = NULL;
626 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
629 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
632 chans = get_wcaps(hdev, cvt->nid);
633 chans = get_wcaps_channels(chans);
635 cvt->params.channels_min = 2;
637 cvt->params.channels_max = chans;
638 if (chans > hdmi->chmap.channels_max)
639 hdmi->chmap.channels_max = chans;
641 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
643 &cvt->params.formats,
644 &cvt->params.maxbps);
647 "Failed to query pcm params for nid %d: %d\n",
653 static int hdac_hdmi_fill_widget_info(struct device *dev,
654 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
655 void *priv, const char *wname, const char *stream,
656 struct snd_kcontrol_new *wc, int numkc,
657 int (*event)(struct snd_soc_dapm_widget *,
658 struct snd_kcontrol *, int), unsigned short event_flags)
661 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
666 w->reg = SND_SOC_NOPM;
668 w->kcontrol_news = wc;
669 w->num_kcontrols = numkc;
672 w->event_flags = event_flags;
677 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
678 const char *sink, const char *control, const char *src,
679 int (*handler)(struct snd_soc_dapm_widget *src,
680 struct snd_soc_dapm_widget *sink))
684 route->control = control;
685 route->connected = handler;
688 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
689 struct hdac_hdmi_port *port)
691 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
692 struct hdac_hdmi_pcm *pcm = NULL;
693 struct hdac_hdmi_port *p;
695 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
696 if (list_empty(&pcm->port_list))
699 list_for_each_entry(p, &pcm->port_list, head) {
700 if (p->id == port->id && port->pin == p->pin)
708 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
709 hda_nid_t nid, unsigned int pwr_state)
714 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
715 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
716 for (count = 0; count < 10; count++) {
717 snd_hdac_codec_read(hdev, nid, 0,
718 AC_VERB_SET_POWER_STATE,
720 state = snd_hdac_sync_power_state(hdev,
722 if (!(state & AC_PWRST_ERROR))
729 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
730 hda_nid_t nid, int val)
732 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
733 snd_hdac_codec_write(hdev, nid, 0,
734 AC_VERB_SET_AMP_GAIN_MUTE, val);
738 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
739 struct snd_kcontrol *kc, int event)
741 struct hdac_hdmi_port *port = w->priv;
742 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
743 struct hdac_hdmi_pcm *pcm;
745 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
746 __func__, w->name, event);
748 pcm = hdac_hdmi_get_pcm(hdev, port);
752 /* set the device if pin is mst_capable */
753 if (hdac_hdmi_port_select_set(hdev, port) < 0)
757 case SND_SOC_DAPM_PRE_PMU:
758 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
760 /* Enable out path for this pin widget */
761 snd_hdac_codec_write(hdev, port->pin->nid, 0,
762 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
764 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
766 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
768 case SND_SOC_DAPM_POST_PMD:
769 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
771 /* Disable out path for this pin widget */
772 snd_hdac_codec_write(hdev, port->pin->nid, 0,
773 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
775 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
783 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
784 struct snd_kcontrol *kc, int event)
786 struct hdac_hdmi_cvt *cvt = w->priv;
787 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
788 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
789 struct hdac_hdmi_pcm *pcm;
791 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
792 __func__, w->name, event);
794 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
799 case SND_SOC_DAPM_PRE_PMU:
800 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
802 /* Enable transmission */
803 snd_hdac_codec_write(hdev, cvt->nid, 0,
804 AC_VERB_SET_DIGI_CONVERT_1, 1);
806 /* Category Code (CC) to zero */
807 snd_hdac_codec_write(hdev, cvt->nid, 0,
808 AC_VERB_SET_DIGI_CONVERT_2, 0);
810 snd_hdac_codec_write(hdev, cvt->nid, 0,
811 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
812 snd_hdac_codec_write(hdev, cvt->nid, 0,
813 AC_VERB_SET_STREAM_FORMAT, pcm->format);
816 case SND_SOC_DAPM_POST_PMD:
817 snd_hdac_codec_write(hdev, cvt->nid, 0,
818 AC_VERB_SET_CHANNEL_STREAMID, 0);
819 snd_hdac_codec_write(hdev, cvt->nid, 0,
820 AC_VERB_SET_STREAM_FORMAT, 0);
822 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
830 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
831 struct snd_kcontrol *kc, int event)
833 struct hdac_hdmi_port *port = w->priv;
834 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
837 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
838 __func__, w->name, event);
841 kc = w->kcontrols[0];
843 mux_idx = dapm_kcontrol_get_value(kc);
845 /* set the device if pin is mst_capable */
846 if (hdac_hdmi_port_select_set(hdev, port) < 0)
850 snd_hdac_codec_write(hdev, port->pin->nid, 0,
851 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
858 * Based on user selection, map the PINs with the PCMs.
860 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
861 struct snd_ctl_elem_value *ucontrol)
864 struct hdac_hdmi_port *p, *p_next;
865 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
866 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
867 struct snd_soc_dapm_context *dapm = w->dapm;
868 struct hdac_hdmi_port *port = w->priv;
869 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
870 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
871 struct hdac_hdmi_pcm *pcm = NULL;
872 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
874 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
881 mutex_lock(&hdmi->pin_mutex);
882 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
883 if (list_empty(&pcm->port_list))
886 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
887 if (p == port && p->id == port->id &&
888 p->pin == port->pin) {
889 hdac_hdmi_jack_report(pcm, port, false);
896 * Jack status is not reported during device probe as the
897 * PCMs are not registered by then. So report it here.
899 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
900 if (!strcmp(cvt_name, pcm->cvt->name)) {
901 list_add_tail(&port->head, &pcm->port_list);
902 if (port->eld.monitor_present && port->eld.eld_valid) {
903 hdac_hdmi_jack_report(pcm, port, true);
904 mutex_unlock(&hdmi->pin_mutex);
909 mutex_unlock(&hdmi->pin_mutex);
915 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
916 * the display driver seem to be programming the connection list for the pin
919 * So programming all the possible inputs for the mux, the user has to take
920 * care of selecting the right one and leaving all other inputs selected to
923 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
924 struct hdac_hdmi_port *port,
925 struct snd_soc_dapm_widget *widget,
926 const char *widget_name)
928 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
929 struct hdac_hdmi_pin *pin = port->pin;
930 struct snd_kcontrol_new *kc;
931 struct hdac_hdmi_cvt *cvt;
933 char kc_name[NAME_SIZE];
934 char mux_items[NAME_SIZE];
935 /* To hold inputs to the Pin mux */
936 char *items[HDA_MAX_CONNECTIONS];
938 int num_items = hdmi->num_cvt + 1;
940 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
944 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
948 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
950 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
954 kc->private_value = (long)se;
955 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
957 kc->info = snd_soc_info_enum_double;
958 kc->put = hdac_hdmi_set_pin_port_mux;
959 kc->get = snd_soc_dapm_get_enum_double;
961 se->reg = SND_SOC_NOPM;
963 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
964 se->items = num_items;
965 se->mask = roundup_pow_of_two(se->items) - 1;
967 sprintf(mux_items, "NONE");
968 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
972 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
974 sprintf(mux_items, "cvt %d", cvt->nid);
975 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
980 se->texts = devm_kmemdup(&hdev->dev, items,
981 (num_items * sizeof(char *)), GFP_KERNEL);
985 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
986 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
987 hdac_hdmi_pin_mux_widget_event,
988 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
991 /* Add cvt <- input <- mux route map */
992 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
993 struct snd_soc_dapm_widget *widgets,
994 struct snd_soc_dapm_route *route, int rindex)
996 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
997 const struct snd_kcontrol_new *kc;
999 int mux_index = hdmi->num_cvt + hdmi->num_ports;
1002 for (i = 0; i < hdmi->num_ports; i++) {
1003 kc = widgets[mux_index].kcontrol_news;
1004 se = (struct soc_enum *)kc->private_value;
1005 for (j = 0; j < hdmi->num_cvt; j++) {
1006 hdac_hdmi_fill_route(&route[rindex],
1007 widgets[mux_index].name,
1009 widgets[j].name, NULL);
1019 * Widgets are added in the below sequence
1020 * Converter widgets for num converters enumerated
1021 * Pin-port widgets for num ports for Pins enumerated
1022 * Pin-port mux widgets to represent connenction list of pin widget
1024 * For each port, one Mux and One output widget is added
1025 * Total widgets elements = num_cvt + (num_ports * 2);
1027 * Routes are added as below:
1028 * pin-port mux -> pin (based on num_ports)
1029 * cvt -> "Input sel control" -> pin-port_mux
1031 * Total route elements:
1032 * num_ports + (pin_muxes * num_cvt)
1034 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1036 struct snd_soc_dapm_widget *widgets;
1037 struct snd_soc_dapm_route *route;
1038 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1039 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1040 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1041 char widget_name[NAME_SIZE];
1042 struct hdac_hdmi_cvt *cvt;
1043 struct hdac_hdmi_pin *pin;
1044 int ret, i = 0, num_routes = 0, j;
1046 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1049 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1050 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1056 /* DAPM widgets to represent each converter widget */
1057 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1058 sprintf(widget_name, "Converter %d", cvt->nid);
1059 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1060 snd_soc_dapm_aif_in, cvt,
1061 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1062 hdac_hdmi_cvt_output_widget_event,
1063 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1069 list_for_each_entry(pin, &hdmi->pin_list, head) {
1070 for (j = 0; j < pin->num_ports; j++) {
1071 sprintf(widget_name, "hif%d-%d Output",
1072 pin->nid, pin->ports[j].id);
1073 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1074 snd_soc_dapm_output, &pin->ports[j],
1075 widget_name, NULL, NULL, 0,
1076 hdac_hdmi_pin_output_widget_event,
1077 SND_SOC_DAPM_PRE_PMU |
1078 SND_SOC_DAPM_POST_PMD);
1081 pin->ports[j].output_pin = widgets[i].name;
1086 /* DAPM widgets to represent the connection list to pin widget */
1087 list_for_each_entry(pin, &hdmi->pin_list, head) {
1088 for (j = 0; j < pin->num_ports; j++) {
1089 sprintf(widget_name, "Pin%d-Port%d Mux",
1090 pin->nid, pin->ports[j].id);
1091 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1092 &pin->ports[j], &widgets[i],
1098 /* For cvt to pin_mux mapping */
1099 num_routes += hdmi->num_cvt;
1101 /* For pin_mux to pin mapping */
1106 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1112 /* Add pin <- NULL <- mux route map */
1113 list_for_each_entry(pin, &hdmi->pin_list, head) {
1114 for (j = 0; j < pin->num_ports; j++) {
1115 int sink_index = i + hdmi->num_cvt;
1116 int src_index = sink_index + pin->num_ports *
1119 hdac_hdmi_fill_route(&route[i],
1120 widgets[sink_index].name, NULL,
1121 widgets[src_index].name, NULL);
1126 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1128 snd_soc_dapm_new_controls(dapm, widgets,
1129 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1131 snd_soc_dapm_add_routes(dapm, route, num_routes);
1132 snd_soc_dapm_new_widgets(dapm->card);
1138 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1140 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1141 struct hdac_hdmi_dai_port_map *dai_map;
1142 struct hdac_hdmi_cvt *cvt;
1145 if (list_empty(&hdmi->cvt_list))
1148 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1149 dai_map = &hdmi->dai_map[dai_id];
1150 dai_map->dai_id = dai_id;
1155 if (dai_id == HDA_MAX_CVTS) {
1156 dev_warn(&hdev->dev,
1157 "Max dais supported: %d\n", dai_id);
1165 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1167 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1168 struct hdac_hdmi_cvt *cvt;
1169 char name[NAME_SIZE];
1171 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1176 sprintf(name, "cvt %d", cvt->nid);
1177 cvt->name = kstrdup(name, GFP_KERNEL);
1179 list_add_tail(&cvt->head, &hdmi->cvt_list);
1182 return hdac_hdmi_query_cvt_params(hdev, cvt);
1185 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1186 struct hdac_hdmi_port *port)
1188 unsigned int ver, mnl;
1190 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1191 >> DRM_ELD_VER_SHIFT;
1193 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1194 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1198 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1199 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1201 if (mnl > ELD_MAX_MNL) {
1202 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1206 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1211 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1212 struct hdac_hdmi_port *port)
1214 struct hdac_device *hdev = pin->hdev;
1215 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1216 struct hdac_hdmi_pcm *pcm;
1224 * In case of non MST pin, get_eld info API expectes port
1227 mutex_lock(&hdmi->pin_mutex);
1228 port->eld.monitor_present = false;
1230 if (pin->mst_capable)
1233 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1234 &port->eld.monitor_present,
1235 port->eld.eld_buffer,
1239 size = min(size, ELD_MAX_SIZE);
1240 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1245 port->eld.eld_valid = true;
1246 port->eld.eld_size = size;
1248 port->eld.eld_valid = false;
1249 port->eld.eld_size = 0;
1252 pcm = hdac_hdmi_get_pcm(hdev, port);
1254 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1256 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1257 __func__, pin->nid, port->id);
1260 * PCMs are not registered during device probe, so don't
1261 * report jack here. It will be done in usermode mux
1265 hdac_hdmi_jack_report(pcm, port, false);
1267 mutex_unlock(&hdmi->pin_mutex);
1271 if (port->eld.monitor_present && port->eld.eld_valid) {
1273 hdac_hdmi_jack_report(pcm, port, true);
1275 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1276 port->eld.eld_buffer, port->eld.eld_size, false);
1279 mutex_unlock(&hdmi->pin_mutex);
1282 static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1283 struct hdac_hdmi_pin *pin)
1285 struct hdac_hdmi_port *ports;
1286 int max_ports = HDA_MAX_PORTS;
1290 * FIXME: max_port may vary for each platform, so pass this as
1291 * as driver data or query from i915 interface when this API is
1295 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1299 for (i = 0; i < max_ports; i++) {
1304 pin->num_ports = max_ports;
1308 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1310 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1311 struct hdac_hdmi_pin *pin;
1314 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1319 pin->mst_capable = false;
1321 ret = hdac_hdmi_add_ports(hdmi, pin);
1325 list_add_tail(&pin->head, &hdmi->pin_list);
1327 hdmi->num_ports += pin->num_ports;
1332 #define INTEL_VENDOR_NID 0x08
1333 #define INTEL_GLK_VENDOR_NID 0x0b
1334 #define INTEL_GET_VENDOR_VERB 0xf81
1335 #define INTEL_SET_VENDOR_VERB 0x781
1336 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1337 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1339 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1341 unsigned int vendor_param;
1342 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1343 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1345 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1346 INTEL_GET_VENDOR_VERB, 0);
1347 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1350 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1351 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1352 INTEL_SET_VENDOR_VERB, vendor_param);
1353 if (vendor_param == -1)
1357 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1359 unsigned int vendor_param;
1360 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1361 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1363 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1364 INTEL_GET_VENDOR_VERB, 0);
1365 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1368 /* enable DP1.2 mode */
1369 vendor_param |= INTEL_EN_DP12;
1370 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1371 INTEL_SET_VENDOR_VERB, vendor_param);
1372 if (vendor_param == -1)
1377 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1378 .startup = hdac_hdmi_pcm_open,
1379 .shutdown = hdac_hdmi_pcm_close,
1380 .hw_params = hdac_hdmi_set_hw_params,
1381 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
1385 * Each converter can support a stream independently. So a dai is created
1386 * based on the number of converter queried.
1388 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1389 struct snd_soc_dai_driver **dais,
1390 struct hdac_hdmi_priv *hdmi, int num_dais)
1392 struct snd_soc_dai_driver *hdmi_dais;
1393 struct hdac_hdmi_cvt *cvt;
1394 char name[NAME_SIZE], dai_name[NAME_SIZE];
1397 unsigned int rate_max = 384000, rate_min = 8000;
1401 hdmi_dais = devm_kzalloc(&hdev->dev,
1402 (sizeof(*hdmi_dais) * num_dais),
1407 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1408 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1409 &rates, &formats, &bps);
1413 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1414 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1415 dai_name, GFP_KERNEL);
1417 if (!hdmi_dais[i].name)
1420 snprintf(name, sizeof(name), "hifi%d", i+1);
1421 hdmi_dais[i].playback.stream_name =
1422 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1423 if (!hdmi_dais[i].playback.stream_name)
1427 * Set caps based on capability queried from the converter.
1428 * It will be constrained runtime based on ELD queried.
1430 hdmi_dais[i].playback.formats = formats;
1431 hdmi_dais[i].playback.rates = rates;
1432 hdmi_dais[i].playback.rate_max = rate_max;
1433 hdmi_dais[i].playback.rate_min = rate_min;
1434 hdmi_dais[i].playback.channels_min = 2;
1435 hdmi_dais[i].playback.channels_max = 2;
1436 hdmi_dais[i].playback.sig_bits = bps;
1437 hdmi_dais[i].ops = &hdmi_dai_ops;
1442 hdmi->dai_drv = hdmi_dais;
1448 * Parse all nodes and store the cvt/pin nids in array
1449 * Add one time initialization for pin and cvt widgets
1451 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1452 struct snd_soc_dai_driver **dais, int *num_dais)
1456 struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
1457 struct hdac_hdmi_pin *temp_pin, *pin_next;
1458 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1461 hdac_hdmi_skl_enable_all_pins(hdev);
1462 hdac_hdmi_skl_enable_dp12(hdev);
1464 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1465 if (!nid || num_nodes <= 0) {
1466 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1470 for (i = 0; i < num_nodes; i++, nid++) {
1474 caps = get_wcaps(hdev, nid);
1475 type = get_wcaps_type(caps);
1477 if (!(caps & AC_WCAP_DIGITAL))
1482 case AC_WID_AUD_OUT:
1483 ret = hdac_hdmi_add_cvt(hdev, nid);
1489 ret = hdac_hdmi_add_pin(hdev, nid);
1496 if (!hdmi->num_pin || !hdmi->num_cvt) {
1501 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1503 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1508 *num_dais = hdmi->num_cvt;
1509 ret = hdac_hdmi_init_dai_map(hdev);
1516 list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
1517 list_del(&temp_cvt->head);
1518 kfree(temp_cvt->name);
1522 list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
1523 for (i = 0; i < temp_pin->num_ports; i++)
1524 temp_pin->ports[i].pin = NULL;
1525 kfree(temp_pin->ports);
1526 list_del(&temp_pin->head);
1533 static int hdac_hdmi_pin2port(void *aptr, int pin)
1535 return pin - 4; /* map NID 0x05 -> port #1 */
1538 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1540 struct hdac_device *hdev = aptr;
1541 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1542 struct hdac_hdmi_pin *pin = NULL;
1543 struct hdac_hdmi_port *hport = NULL;
1544 struct snd_soc_component *component = hdmi->component;
1547 /* Don't know how this mapping is derived */
1548 hda_nid_t pin_nid = port + 0x04;
1550 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1554 * skip notification during system suspend (but not in runtime PM);
1555 * the state will be updated at resume. Also since the ELD and
1556 * connection states are updated in anyway at the end of the resume,
1557 * we can skip it when received during PM process.
1559 if (snd_power_get_state(component->card->snd_card) !=
1563 if (atomic_read(&hdev->in_pm))
1566 list_for_each_entry(pin, &hdmi->pin_list, head) {
1567 if (pin->nid != pin_nid)
1570 /* In case of non MST pin, pipe is -1 */
1572 pin->mst_capable = false;
1573 /* if not MST, default is port[0] */
1574 hport = &pin->ports[0];
1576 for (i = 0; i < pin->num_ports; i++) {
1577 pin->mst_capable = true;
1578 if (pin->ports[i].id == pipe) {
1579 hport = &pin->ports[i];
1586 hdac_hdmi_present_sense(pin, hport);
1591 static struct drm_audio_component_audio_ops aops = {
1592 .pin2port = hdac_hdmi_pin2port,
1593 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1596 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1599 struct snd_soc_pcm_runtime *rtd;
1601 list_for_each_entry(rtd, &card->rtd_list, list) {
1602 if (rtd->pcm && (rtd->pcm->device == device))
1609 /* create jack pin kcontrols */
1610 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1611 struct hdac_device *hdev)
1613 struct hdac_hdmi_pin *pin;
1614 struct snd_kcontrol_new *kc;
1615 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1618 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1619 struct snd_soc_component *component = hdmi->component;
1621 kc = devm_kcalloc(component->dev, hdmi->num_ports,
1622 sizeof(*kc), GFP_KERNEL);
1627 list_for_each_entry(pin, &hdmi->pin_list, head) {
1628 for (j = 0; j < pin->num_ports; j++) {
1629 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1630 pin->nid, pin->ports[j].id);
1631 name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1634 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1635 kc[i].name = devm_kstrdup(component->dev, kc_name,
1640 kc[i].private_value = (unsigned long)name;
1641 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1643 kc[i].info = snd_soc_dapm_info_pin_switch;
1644 kc[i].put = snd_soc_dapm_put_pin_switch;
1645 kc[i].get = snd_soc_dapm_get_pin_switch;
1650 return snd_soc_add_card_controls(card, kc, i);
1653 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1654 struct snd_soc_dapm_context *dapm)
1656 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1657 struct hdac_device *hdev = hdmi->hdev;
1658 struct hdac_hdmi_pin *pin;
1659 struct snd_soc_dapm_widget *widgets;
1660 struct snd_soc_dapm_route *route;
1661 char w_name[NAME_SIZE];
1664 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1665 sizeof(*widgets), GFP_KERNEL);
1670 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1671 sizeof(*route), GFP_KERNEL);
1675 /* create Jack DAPM widget */
1676 list_for_each_entry(pin, &hdmi->pin_list, head) {
1677 for (j = 0; j < pin->num_ports; j++) {
1678 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1679 pin->nid, pin->ports[j].id);
1681 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1682 snd_soc_dapm_spk, NULL,
1683 w_name, NULL, NULL, 0, NULL, 0);
1687 pin->ports[j].jack_pin = widgets[i].name;
1688 pin->ports[j].dapm = dapm;
1690 /* add to route from Jack widget to output */
1691 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1692 NULL, pin->ports[j].output_pin, NULL);
1698 /* Add Route from Jack widget to the output widget */
1699 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1703 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1707 ret = snd_soc_dapm_new_widgets(dapm->card);
1711 /* Add Jack Pin switch Kcontrol */
1712 ret = create_fill_jack_kcontrols(dapm->card, hdev);
1717 /* default set the Jack Pin switch to OFF */
1718 list_for_each_entry(pin, &hdmi->pin_list, head) {
1719 for (j = 0; j < pin->num_ports; j++)
1720 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1721 pin->ports[j].jack_pin);
1726 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1728 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1729 struct snd_soc_jack *jack)
1731 struct snd_soc_component *component = dai->component;
1732 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1733 struct hdac_device *hdev = hdmi->hdev;
1734 struct hdac_hdmi_pcm *pcm;
1735 struct snd_pcm *snd_pcm;
1739 * this is a new PCM device, create new pcm and
1740 * add to the pcm list
1742 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1745 pcm->pcm_id = device;
1746 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1747 pcm->jack_event = 0;
1749 mutex_init(&pcm->lock);
1750 INIT_LIST_HEAD(&pcm->port_list);
1751 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1753 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1756 "chmap control add failed with err: %d for pcm: %d\n",
1763 list_add_tail(&pcm->head, &hdmi->pcm_list);
1767 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1769 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1770 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1773 struct hdac_hdmi_pin *pin;
1775 list_for_each_entry(pin, &hdmi->pin_list, head) {
1776 if (detect_pin_caps) {
1778 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1779 pin->mst_capable = false;
1781 pin->mst_capable = true;
1784 for (i = 0; i < pin->num_ports; i++) {
1785 if (!pin->mst_capable && i > 0)
1788 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1793 static int hdmi_codec_probe(struct snd_soc_component *component)
1795 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1796 struct hdac_device *hdev = hdmi->hdev;
1797 struct snd_soc_dapm_context *dapm =
1798 snd_soc_component_get_dapm(component);
1799 struct hdac_ext_link *hlink = NULL;
1802 hdmi->component = component;
1805 * hold the ref while we probe, also no need to drop the ref on
1806 * exit, we call pm_runtime_suspend() so that will do for us
1808 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1810 dev_err(&hdev->dev, "hdac link not found\n");
1814 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1816 ret = create_fill_widget_route_map(dapm);
1820 aops.audio_ptr = hdev;
1821 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1823 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1827 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1828 /* Imp: Store the card pointer in hda_codec */
1829 hdmi->card = dapm->card->snd_card;
1832 * Setup a device_link between card device and HDMI codec device.
1833 * The card device is the consumer and the HDMI codec device is
1834 * the supplier. With this setting, we can make sure that the audio
1835 * domain in display power will be always turned on before operating
1836 * on the HDMI audio codec registers.
1837 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1838 * sure the device link is freed when the machine driver is removed.
1840 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1841 DL_FLAG_AUTOREMOVE_CONSUMER);
1843 * hdac_device core already sets the state to active and calls
1844 * get_noresume. So enable runtime and set the device to suspend.
1846 pm_runtime_enable(&hdev->dev);
1847 pm_runtime_put(&hdev->dev);
1848 pm_runtime_suspend(&hdev->dev);
1853 static void hdmi_codec_remove(struct snd_soc_component *component)
1855 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1856 struct hdac_device *hdev = hdmi->hdev;
1858 pm_runtime_disable(&hdev->dev);
1862 static int hdmi_codec_prepare(struct device *dev)
1864 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1866 pm_runtime_get_sync(&hdev->dev);
1870 * codec_read is preferred over codec_write to set the power state.
1871 * This way verb is send to set the power state and response
1872 * is received. So setting power state is ensured without using loop
1873 * to read the state.
1875 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
1881 static void hdmi_codec_complete(struct device *dev)
1883 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1884 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1887 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
1890 hdac_hdmi_skl_enable_all_pins(hdev);
1891 hdac_hdmi_skl_enable_dp12(hdev);
1894 * As the ELD notify callback request is not entertained while the
1895 * device is in suspend state. Need to manually check detection of
1896 * all pins here. pin capablity change is not support, so use the
1897 * already set pin caps.
1899 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1901 pm_runtime_put_sync(&hdev->dev);
1904 #define hdmi_codec_prepare NULL
1905 #define hdmi_codec_complete NULL
1908 static const struct snd_soc_component_driver hdmi_hda_codec = {
1909 .probe = hdmi_codec_probe,
1910 .remove = hdmi_codec_remove,
1911 .use_pmdown_time = 1,
1913 .non_legacy_dai_naming = 1,
1916 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1917 unsigned char *chmap)
1919 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1920 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1922 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1925 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1926 unsigned char *chmap, int prepared)
1928 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1929 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1930 struct hdac_hdmi_port *port;
1935 if (list_empty(&pcm->port_list))
1938 mutex_lock(&pcm->lock);
1939 pcm->chmap_set = true;
1940 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1941 list_for_each_entry(port, &pcm->port_list, head)
1943 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1944 mutex_unlock(&pcm->lock);
1947 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1949 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1950 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1955 if (list_empty(&pcm->port_list))
1961 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
1963 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1964 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1965 struct hdac_hdmi_port *port;
1970 if (list_empty(&pcm->port_list))
1973 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1978 if (!port || !port->eld.eld_valid)
1981 return port->eld.info.spk_alloc;
1984 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
1985 .vendor_nid = INTEL_GLK_VENDOR_NID,
1988 static struct hdac_hdmi_drv_data intel_drv_data = {
1989 .vendor_nid = INTEL_VENDOR_NID,
1992 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
1994 struct hdac_hdmi_priv *hdmi_priv = NULL;
1995 struct snd_soc_dai_driver *hdmi_dais = NULL;
1996 struct hdac_ext_link *hlink = NULL;
1999 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2000 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
2002 /* hold the ref while we probe */
2003 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2005 dev_err(&hdev->dev, "hdac link not found\n");
2009 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2011 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2012 if (hdmi_priv == NULL)
2015 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2016 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2017 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2018 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2019 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2020 hdmi_priv->hdev = hdev;
2025 if (hdac_id->driver_data)
2026 hdmi_priv->drv_data =
2027 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2029 hdmi_priv->drv_data = &intel_drv_data;
2031 dev_set_drvdata(&hdev->dev, hdmi_priv);
2033 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2034 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2035 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2036 mutex_init(&hdmi_priv->pin_mutex);
2039 * Turned off in the runtime_suspend during the first explicit
2040 * pm_runtime_suspend call.
2042 ret = snd_hdac_display_power(hdev->bus, true);
2045 "Cannot turn on display power on i915 err: %d\n",
2050 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2053 "Failed in parse and map nid with err: %d\n", ret);
2056 snd_hdac_refresh_widgets(hdev, true);
2058 /* ASoC specific initialization */
2059 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2060 hdmi_dais, num_dais);
2062 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2067 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2069 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2070 struct hdac_hdmi_pin *pin, *pin_next;
2071 struct hdac_hdmi_cvt *cvt, *cvt_next;
2072 struct hdac_hdmi_pcm *pcm, *pcm_next;
2073 struct hdac_hdmi_port *port, *port_next;
2076 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
2078 if (list_empty(&pcm->port_list))
2081 list_for_each_entry_safe(port, port_next,
2082 &pcm->port_list, head)
2083 list_del(&port->head);
2085 list_del(&pcm->head);
2089 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
2090 list_del(&cvt->head);
2095 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
2096 for (i = 0; i < pin->num_ports; i++)
2097 pin->ports[i].pin = NULL;
2099 list_del(&pin->head);
2108 * Power management sequences
2109 * ==========================
2111 * The following explains the PM handling of HDAC HDMI with its parent
2112 * device SKL and display power usage
2117 * 1. skl_probe_work() powers up the display (refcount++ -> 1)
2118 * 2. enumerates the codecs on the link
2119 * 3. powers down the display (refcount-- -> 0)
2121 * In HDAC HDMI probe,
2122 * 1. hdac_hdmi_dev_probe() powers up the display (refcount++ -> 1)
2123 * 2. probe the codec
2124 * 3. put the HDAC HDMI device to runtime suspend
2125 * 4. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0)
2127 * Once children are runtime suspended, SKL device also goes to runtime
2133 * 1. skl_runtime_resume() invoked
2134 * 2. hdac_hdmi_runtime_resume() powers up the display (refcount++ -> 1)
2136 * Close HDMI device,
2137 * 1. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0)
2138 * 2. skl_runtime_suspend() invoked
2140 * S0/S3 Cycle with playback in progress
2141 * -------------------------------------
2142 * When the device is opened for playback, the device is runtime active
2143 * already and the display refcount is 1 as explained above.
2146 * 1. hdmi_codec_prepare() invoke the runtime resume of codec which just
2147 * increments the PM runtime usage count of the codec since the device
2149 * 2. skl_suspend() powers down the display (refcount-- -> 0)
2152 * 1. skl_resume() powers up the display (refcount++ -> 1)
2153 * 2. hdmi_codec_complete() invokes the runtime suspend of codec which just
2154 * decrements the PM runtime usage count of the codec since the device
2157 * Once playback is stopped, the display refcount is set to 0 as explained
2158 * above in the HDMI playback sequence. The PM handlings are designed in
2159 * such way that to balance the refcount of display power when the codec
2160 * device put to S3 while playback is going on.
2162 * S0/S3 Cycle without playback in progress
2163 * ----------------------------------------
2165 * 1. hdmi_codec_prepare() invoke the runtime resume of codec
2166 * 2. skl_runtime_resume() invoked
2167 * 3. hdac_hdmi_runtime_resume() powers up the display (refcount++ -> 1)
2168 * 4. skl_suspend() powers down the display (refcount-- -> 0)
2171 * 1. skl_resume() powers up the display (refcount++ -> 1)
2172 * 2. hdmi_codec_complete() invokes the runtime suspend of codec
2173 * 3. hdac_hdmi_runtime_suspend() powers down the display (refcount-- -> 0)
2174 * 4. skl_runtime_suspend() invoked
2176 static int hdac_hdmi_runtime_suspend(struct device *dev)
2178 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2179 struct hdac_bus *bus = hdev->bus;
2180 struct hdac_ext_link *hlink = NULL;
2183 dev_dbg(dev, "Enter: %s\n", __func__);
2185 /* controller may not have been initialized for the first time */
2191 * codec_read is preferred over codec_write to set the power state.
2192 * This way verb is send to set the power state and response
2193 * is received. So setting power state is ensured without using loop
2194 * to read the state.
2196 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2199 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2201 dev_err(dev, "hdac link not found\n");
2205 snd_hdac_ext_bus_link_put(bus, hlink);
2207 err = snd_hdac_display_power(bus, false);
2209 dev_err(dev, "Cannot turn off display power on i915\n");
2214 static int hdac_hdmi_runtime_resume(struct device *dev)
2216 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2217 struct hdac_bus *bus = hdev->bus;
2218 struct hdac_ext_link *hlink = NULL;
2221 dev_dbg(dev, "Enter: %s\n", __func__);
2223 /* controller may not have been initialized for the first time */
2227 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2229 dev_err(dev, "hdac link not found\n");
2233 snd_hdac_ext_bus_link_get(bus, hlink);
2235 err = snd_hdac_display_power(bus, true);
2237 dev_err(dev, "Cannot turn on display power on i915\n");
2241 hdac_hdmi_skl_enable_all_pins(hdev);
2242 hdac_hdmi_skl_enable_dp12(hdev);
2245 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2251 #define hdac_hdmi_runtime_suspend NULL
2252 #define hdac_hdmi_runtime_resume NULL
2255 static const struct dev_pm_ops hdac_hdmi_pm = {
2256 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2257 .prepare = hdmi_codec_prepare,
2258 .complete = hdmi_codec_complete,
2261 static const struct hda_device_id hdmi_list[] = {
2262 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2263 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2264 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2265 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2266 &intel_glk_drv_data),
2267 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2268 &intel_glk_drv_data),
2272 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2274 static struct hdac_driver hdmi_driver = {
2276 .name = "HDMI HDA Codec",
2277 .pm = &hdac_hdmi_pm,
2279 .id_table = hdmi_list,
2280 .probe = hdac_hdmi_dev_probe,
2281 .remove = hdac_hdmi_dev_remove,
2284 static int __init hdmi_init(void)
2286 return snd_hda_ext_driver_register(&hdmi_driver);
2289 static void __exit hdmi_exit(void)
2291 snd_hda_ext_driver_unregister(&hdmi_driver);
2294 module_init(hdmi_init);
2295 module_exit(hdmi_exit);
2297 MODULE_LICENSE("GPL v2");
2298 MODULE_DESCRIPTION("HDMI HD codec");
2299 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2300 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");