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 "../../hda/local.h"
33 #include "hdac_hdmi.h"
37 #define AMP_OUT_MUTE 0xb080
38 #define AMP_OUT_UNMUTE 0xb000
39 #define PIN_OUT (AC_PINCTL_OUT_EN)
41 #define HDA_MAX_CONNECTIONS 32
43 #define HDA_MAX_CVTS 3
45 #define ELD_MAX_SIZE 256
46 #define ELD_FIXED_BYTES 20
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 struct hdac_hdmi_eld {
67 char eld_buffer[ELD_MAX_SIZE];
70 struct hdac_hdmi_pin {
71 struct list_head head;
74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
78 struct delayed_work work;
81 struct hdac_hdmi_pcm {
82 struct list_head head;
84 struct hdac_hdmi_pin *pin;
85 struct hdac_hdmi_cvt *cvt;
86 struct snd_jack *jack;
89 struct hdac_hdmi_dai_pin_map {
91 struct hdac_hdmi_pin *pin;
92 struct hdac_hdmi_cvt *cvt;
95 struct hdac_hdmi_priv {
96 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
97 struct list_head pin_list;
98 struct list_head cvt_list;
99 struct list_head pcm_list;
102 struct mutex pin_mutex;
105 static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
107 struct hdac_device *hdac = dev_to_hdac_dev(dev);
109 return to_ehdac_device(hdac);
112 static unsigned int sad_format(const u8 *sad)
114 return ((sad[0] >> 0x3) & 0x1f);
117 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
122 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
125 u64 formats = SNDRV_PCM_FMTBIT_S16;
127 const u8 *sad, *eld_buf = eld;
129 sad = drm_eld_sad(eld_buf);
131 goto format_constraint;
133 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
134 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
137 * the controller support 20 and 24 bits in 32 bit
138 * container so we set S32
140 if (sad_sample_bits_lpcm(sad) & 0x6)
141 formats |= SNDRV_PCM_FMTBIT_S32;
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
151 /* HDMI ELD routines */
152 static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153 hda_nid_t nid, int byte_index)
157 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
160 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
166 static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
168 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
173 * This function queries the ELD size and ELD data and fills in the buffer
176 static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177 unsigned char *buf, int *eld_size)
179 int i, size, ret = 0;
182 * ELD size is initialized to zero in caller function. If no errors and
183 * ELD is valid, actual eld_size is assigned.
186 size = hdac_hdmi_get_eld_size(codec, nid);
187 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
188 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
193 for (i = 0; i < size; i++) {
194 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
196 * Graphics driver might be writing to ELD buffer right now.
197 * Just abort. The caller will repoll after a while.
199 if (!(val & AC_ELDD_ELD_VALID)) {
201 "HDMI: invalid ELD data byte %d\n", i);
205 val &= AC_ELDD_ELD_DATA;
207 * The first byte cannot be zero. This can happen on some DVI
208 * connections. Some Intel chips may also need some 250ms delay
209 * to return non-zero ELD data, even when the graphics driver
210 * correctly writes ELD content before setting ELD_valid bit.
213 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
225 static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
226 hda_nid_t cvt_nid, hda_nid_t pin_nid,
227 u32 stream_tag, int format)
231 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
232 cvt_nid, pin_nid, stream_tag, format);
234 val = (stream_tag << 4);
236 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
237 AC_VERB_SET_CHANNEL_STREAMID, val);
238 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
239 AC_VERB_SET_STREAM_FORMAT, format);
245 hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246 int packet_index, int byte_index)
250 val = (packet_index << 5) | (byte_index & 0x1f);
252 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
253 AC_VERB_SET_HDMI_DIP_INDEX, val);
256 struct dp_audio_infoframe {
259 u8 ver; /* 0x11 << 2 */
261 u8 CC02_CT47; /* match with HDMI infoframe from this on */
265 u8 LFEPBL01_LSV36_DM_INH7;
268 static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
269 hda_nid_t cvt_nid, hda_nid_t pin_nid)
271 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
272 struct hdmi_audio_infoframe frame;
273 struct dp_audio_infoframe dp_ai;
274 struct hdac_hdmi_priv *hdmi = hdac->private_data;
275 struct hdac_hdmi_pin *pin;
283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
288 eld_buf = pin->eld.eld_buffer;
289 conn_type = drm_eld_get_conn_type(eld_buf);
291 /* setup channel count */
292 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
293 AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
296 case DRM_ELD_CONN_TYPE_HDMI:
297 hdmi_audio_infoframe_init(&frame);
299 /* Default stereo for now */
300 frame.channels = channels;
302 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
309 case DRM_ELD_CONN_TYPE_DP:
310 memset(&dp_ai, 0, sizeof(dp_ai));
313 dp_ai.ver = 0x11 << 2;
314 dp_ai.CC02_CT47 = channels - 1;
321 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
326 /* stop infoframe transmission */
327 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
328 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
329 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
332 /* Fill infoframe. Index auto-incremented */
333 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
334 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
335 for (i = 0; i < sizeof(frame); i++)
336 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
337 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
339 for (i = 0; i < sizeof(dp_ai); i++)
340 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
341 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
344 /* Start infoframe */
345 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
346 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
347 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
352 static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
353 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
355 /* Power up pin widget */
356 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
358 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
359 AC_VERB_SET_POWER_STATE, pwr_state);
361 /* Power up converter */
362 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
364 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
365 AC_VERB_SET_POWER_STATE, pwr_state);
368 static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
369 struct snd_soc_dai *dai)
371 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
372 struct hdac_hdmi_priv *hdmi = hdac->private_data;
373 struct hdac_hdmi_dai_pin_map *dai_map;
374 struct hdac_ext_dma_params *dd;
378 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
382 dai_map = &hdmi->dai_map[dai->id];
384 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
385 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
386 dd->stream_tag, dd->format);
388 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
393 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
394 dai_map->pin->nid, dd->stream_tag, dd->format);
397 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
398 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
400 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
401 struct hdac_ext_dma_params *dd;
404 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
408 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
411 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
412 params_channels(hparams), params_format(hparams),
415 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
420 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
421 struct snd_soc_dai *dai)
423 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
424 struct hdac_ext_dma_params *dd;
425 struct hdac_hdmi_priv *hdmi = edev->private_data;
426 struct hdac_hdmi_dai_pin_map *dai_map;
428 dai_map = &hdmi->dai_map[dai->id];
430 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
431 AC_VERB_SET_CHANNEL_STREAMID, 0);
432 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
433 AC_VERB_SET_STREAM_FORMAT, 0);
435 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
436 snd_soc_dai_set_dma_data(dai, substream, NULL);
443 static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
444 struct hdac_hdmi_dai_pin_map *dai_map)
447 struct hdac_hdmi_pin *pin = dai_map->pin;
449 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
450 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
451 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
452 AC_VERB_SET_CONNECT_SEL, mux_idx);
457 if (mux_idx == pin->num_mux_nids)
460 /* Enable out path for this pin widget */
461 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
462 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
464 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
466 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
467 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
472 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
473 struct hdac_hdmi_pin *pin)
475 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
476 dev_warn(&hdac->hdac.dev,
477 "HDMI: pin %d wcaps %#x does not support connection list\n",
478 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
482 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
483 pin->mux_nids, HDA_MAX_CONNECTIONS);
484 if (pin->num_mux_nids == 0)
485 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
488 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
489 pin->num_mux_nids, pin->nid);
491 return pin->num_mux_nids;
495 * Query pcm list and return pin widget to which stream is routed.
497 * Also query connection list of the pin, to validate the cvt to pin map.
499 * Same stream rendering to multiple pins simultaneously can be done
500 * possibly, but not supported for now in driver. So return the first pin
503 static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
504 struct hdac_ext_device *edev,
505 struct hdac_hdmi_priv *hdmi,
506 struct hdac_hdmi_cvt *cvt)
508 struct hdac_hdmi_pcm *pcm;
509 struct hdac_hdmi_pin *pin = NULL;
512 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
513 if (pcm->cvt == cvt) {
520 ret = hdac_hdmi_query_pin_connlist(edev, pin);
524 for (i = 0; i < pin->num_mux_nids; i++) {
525 if (pin->mux_nids[i] == cvt->nid)
533 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
534 struct snd_soc_dai *dai)
536 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
537 struct hdac_hdmi_priv *hdmi = hdac->private_data;
538 struct hdac_hdmi_dai_pin_map *dai_map;
539 struct hdac_hdmi_cvt *cvt;
540 struct hdac_hdmi_pin *pin;
543 dai_map = &hdmi->dai_map[dai->id];
546 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
550 if ((!pin->eld.monitor_present) ||
551 (!pin->eld.eld_valid)) {
553 dev_err(&hdac->hdac.dev,
554 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
555 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
562 ret = hdac_hdmi_enable_pin(hdac, dai_map);
566 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
567 pin->eld.eld_buffer);
571 return snd_pcm_hw_constraint_eld(substream->runtime,
572 pin->eld.eld_buffer);
575 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
576 struct snd_soc_dai *dai)
578 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
579 struct hdac_hdmi_priv *hdmi = hdac->private_data;
580 struct hdac_hdmi_dai_pin_map *dai_map;
582 dai_map = &hdmi->dai_map[dai->id];
584 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
586 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
587 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
593 hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
597 /* Only stereo supported as of now */
598 cvt->params.channels_min = cvt->params.channels_max = 2;
600 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
602 &cvt->params.formats,
603 &cvt->params.maxbps);
606 "Failed to query pcm params for nid %d: %d\n",
612 static int hdac_hdmi_fill_widget_info(struct device *dev,
613 struct snd_soc_dapm_widget *w,
614 enum snd_soc_dapm_type id, void *priv,
615 const char *wname, const char *stream,
616 struct snd_kcontrol_new *wc, int numkc)
619 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
624 w->reg = SND_SOC_NOPM;
626 w->kcontrol_news = wc;
627 w->num_kcontrols = numkc;
633 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
634 const char *sink, const char *control, const char *src,
635 int (*handler)(struct snd_soc_dapm_widget *src,
636 struct snd_soc_dapm_widget *sink))
640 route->control = control;
641 route->connected = handler;
644 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
645 struct hdac_hdmi_pin *pin)
647 struct hdac_hdmi_priv *hdmi = edev->private_data;
648 struct hdac_hdmi_pcm *pcm = NULL;
650 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
659 * Based on user selection, map the PINs with the PCMs.
661 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
662 struct snd_ctl_elem_value *ucontrol)
665 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
666 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
667 struct snd_soc_dapm_context *dapm = w->dapm;
668 struct hdac_hdmi_pin *pin = w->priv;
669 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
670 struct hdac_hdmi_priv *hdmi = edev->private_data;
671 struct hdac_hdmi_pcm *pcm = NULL;
672 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
674 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
678 mutex_lock(&hdmi->pin_mutex);
679 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
684 * Jack status is not reported during device probe as the
685 * PCMs are not registered by then. So report it here.
687 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
689 if (pin->eld.monitor_present && pin->eld.eld_valid) {
690 dev_dbg(&edev->hdac.dev,
691 "jack report for pcm=%d\n",
694 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
696 mutex_unlock(&hdmi->pin_mutex);
700 mutex_unlock(&hdmi->pin_mutex);
706 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
707 * the display driver seem to be programming the connection list for the pin
710 * So programming all the possible inputs for the mux, the user has to take
711 * care of selecting the right one and leaving all other inputs selected to
714 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
715 struct hdac_hdmi_pin *pin,
716 struct snd_soc_dapm_widget *widget,
717 const char *widget_name)
719 struct hdac_hdmi_priv *hdmi = edev->private_data;
720 struct snd_kcontrol_new *kc;
721 struct hdac_hdmi_cvt *cvt;
723 char kc_name[NAME_SIZE];
724 char mux_items[NAME_SIZE];
725 /* To hold inputs to the Pin mux */
726 char *items[HDA_MAX_CONNECTIONS];
728 int num_items = hdmi->num_cvt + 1;
730 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
734 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
738 sprintf(kc_name, "Pin %d Input", pin->nid);
739 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
743 kc->private_value = (long)se;
744 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
746 kc->info = snd_soc_info_enum_double;
747 kc->put = hdac_hdmi_set_pin_mux;
748 kc->get = snd_soc_dapm_get_enum_double;
750 se->reg = SND_SOC_NOPM;
752 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
753 se->items = num_items;
754 se->mask = roundup_pow_of_two(se->items) - 1;
756 sprintf(mux_items, "NONE");
757 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
761 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
763 sprintf(mux_items, "cvt %d", cvt->nid);
764 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
769 se->texts = devm_kmemdup(&edev->hdac.dev, items,
770 (num_items * sizeof(char *)), GFP_KERNEL);
774 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
775 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
778 /* Add cvt <- input <- mux route map */
779 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
780 struct snd_soc_dapm_widget *widgets,
781 struct snd_soc_dapm_route *route, int rindex)
783 struct hdac_hdmi_priv *hdmi = edev->private_data;
784 const struct snd_kcontrol_new *kc;
786 int mux_index = hdmi->num_cvt + hdmi->num_pin;
789 for (i = 0; i < hdmi->num_pin; i++) {
790 kc = widgets[mux_index].kcontrol_news;
791 se = (struct soc_enum *)kc->private_value;
792 for (j = 0; j < hdmi->num_cvt; j++) {
793 hdac_hdmi_fill_route(&route[rindex],
794 widgets[mux_index].name,
796 widgets[j].name, NULL);
806 * Widgets are added in the below sequence
807 * Converter widgets for num converters enumerated
808 * Pin widgets for num pins enumerated
809 * Pin mux widgets to represent connenction list of pin widget
811 * Total widgets elements = num_cvt + num_pin + num_pin;
813 * Routes are added as below:
814 * pin mux -> pin (based on num_pins)
815 * cvt -> "Input sel control" -> pin_mux
817 * Total route elements:
818 * num_pins + (pin_muxes * num_cvt)
820 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
822 struct snd_soc_dapm_widget *widgets;
823 struct snd_soc_dapm_route *route;
824 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
825 struct hdac_hdmi_priv *hdmi = edev->private_data;
826 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
827 char widget_name[NAME_SIZE];
828 struct hdac_hdmi_cvt *cvt;
829 struct hdac_hdmi_pin *pin;
830 int ret, i = 0, num_routes = 0;
832 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
835 widgets = devm_kzalloc(dapm->dev,
836 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
842 /* DAPM widgets to represent each converter widget */
843 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
844 sprintf(widget_name, "Converter %d", cvt->nid);
845 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
846 snd_soc_dapm_aif_in, &cvt->nid,
847 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
853 list_for_each_entry(pin, &hdmi->pin_list, head) {
854 sprintf(widget_name, "hif%d Output", pin->nid);
855 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
856 snd_soc_dapm_output, &pin->nid,
857 widget_name, NULL, NULL, 0);
863 /* DAPM widgets to represent the connection list to pin widget */
864 list_for_each_entry(pin, &hdmi->pin_list, head) {
865 sprintf(widget_name, "Pin %d Mux", pin->nid);
866 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
872 /* For cvt to pin_mux mapping */
873 num_routes += hdmi->num_cvt;
875 /* For pin_mux to pin mapping */
879 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
885 /* Add pin <- NULL <- mux route map */
886 list_for_each_entry(pin, &hdmi->pin_list, head) {
887 int sink_index = i + hdmi->num_cvt;
888 int src_index = sink_index + hdmi->num_pin;
890 hdac_hdmi_fill_route(&route[i],
891 widgets[sink_index].name, NULL,
892 widgets[src_index].name, NULL);
897 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
899 snd_soc_dapm_new_controls(dapm, widgets,
900 ((2 * hdmi->num_pin) + hdmi->num_cvt));
902 snd_soc_dapm_add_routes(dapm, route, num_routes);
903 snd_soc_dapm_new_widgets(dapm->card);
909 static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
911 struct hdac_hdmi_priv *hdmi = edev->private_data;
912 struct hdac_hdmi_dai_pin_map *dai_map;
913 struct hdac_hdmi_cvt *cvt;
916 if (list_empty(&hdmi->cvt_list))
919 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
920 dai_map = &hdmi->dai_map[dai_id];
921 dai_map->dai_id = dai_id;
924 /* Enable transmission */
925 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
926 AC_VERB_SET_DIGI_CONVERT_1, 1);
928 /* Category Code (CC) to zero */
929 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
930 AC_VERB_SET_DIGI_CONVERT_2, 0);
934 if (dai_id == HDA_MAX_CVTS) {
935 dev_warn(&edev->hdac.dev,
936 "Max dais supported: %d\n", dai_id);
944 static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
946 struct hdac_hdmi_priv *hdmi = edev->private_data;
947 struct hdac_hdmi_cvt *cvt;
948 char name[NAME_SIZE];
950 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
955 sprintf(name, "cvt %d", cvt->nid);
956 cvt->name = kstrdup(name, GFP_KERNEL);
958 list_add_tail(&cvt->head, &hdmi->cvt_list);
961 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
964 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
966 struct hdac_ext_device *edev = pin->edev;
967 struct hdac_hdmi_priv *hdmi = edev->private_data;
968 struct hdac_hdmi_pcm *pcm;
971 pin->repoll_count = repoll;
973 pm_runtime_get_sync(&edev->hdac.dev);
974 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
975 AC_VERB_GET_PIN_SENSE, 0);
977 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
981 mutex_lock(&hdmi->pin_mutex);
982 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
983 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
985 pcm = hdac_hdmi_get_pcm(edev, pin);
987 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
989 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
993 * PCMs are not registered during device probe, so don't
994 * report jack here. It will be done in usermode mux
998 dev_dbg(&edev->hdac.dev,
999 "jack report for pcm=%d\n", pcm->pcm_id);
1001 snd_jack_report(pcm->jack, 0);
1004 mutex_unlock(&hdmi->pin_mutex);
1005 goto put_hdac_device;
1008 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1009 /* TODO: use i915 component for reading ELD later */
1010 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1011 pin->eld.eld_buffer,
1012 &pin->eld.eld_size) == 0) {
1015 dev_dbg(&edev->hdac.dev,
1016 "jack report for pcm=%d\n",
1019 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1022 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1023 pin->eld.eld_buffer, pin->eld.eld_size);
1025 pin->eld.monitor_present = false;
1026 pin->eld.eld_valid = false;
1029 dev_dbg(&edev->hdac.dev,
1030 "jack report for pcm=%d\n",
1033 snd_jack_report(pcm->jack, 0);
1038 mutex_unlock(&hdmi->pin_mutex);
1041 * Sometimes the pin_sense may present invalid monitor
1042 * present and eld_valid. If ELD data is not valid, loop few
1043 * more times to get correct pin sense and valid ELD.
1045 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1046 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1049 pm_runtime_put_sync(&edev->hdac.dev);
1052 static void hdac_hdmi_repoll_eld(struct work_struct *work)
1054 struct hdac_hdmi_pin *pin =
1055 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1057 /* picked from legacy HDA driver */
1058 if (pin->repoll_count++ > 6)
1059 pin->repoll_count = 0;
1061 hdac_hdmi_present_sense(pin, pin->repoll_count);
1064 static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1066 struct hdac_hdmi_priv *hdmi = edev->private_data;
1067 struct hdac_hdmi_pin *pin;
1069 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1075 list_add_tail(&pin->head, &hdmi->pin_list);
1079 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1084 #define INTEL_VENDOR_NID 0x08
1085 #define INTEL_GET_VENDOR_VERB 0xf81
1086 #define INTEL_SET_VENDOR_VERB 0x781
1087 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1088 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1090 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1092 unsigned int vendor_param;
1094 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1095 INTEL_GET_VENDOR_VERB, 0);
1096 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1099 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1100 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1101 INTEL_SET_VENDOR_VERB, vendor_param);
1102 if (vendor_param == -1)
1106 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1108 unsigned int vendor_param;
1110 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1111 INTEL_GET_VENDOR_VERB, 0);
1112 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1115 /* enable DP1.2 mode */
1116 vendor_param |= INTEL_EN_DP12;
1117 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1118 INTEL_SET_VENDOR_VERB, vendor_param);
1119 if (vendor_param == -1)
1124 static struct snd_soc_dai_ops hdmi_dai_ops = {
1125 .startup = hdac_hdmi_pcm_open,
1126 .shutdown = hdac_hdmi_pcm_close,
1127 .hw_params = hdac_hdmi_set_hw_params,
1128 .prepare = hdac_hdmi_playback_prepare,
1129 .hw_free = hdac_hdmi_playback_cleanup,
1133 * Each converter can support a stream independently. So a dai is created
1134 * based on the number of converter queried.
1136 static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1137 struct snd_soc_dai_driver **dais,
1138 struct hdac_hdmi_priv *hdmi, int num_dais)
1140 struct snd_soc_dai_driver *hdmi_dais;
1141 struct hdac_hdmi_cvt *cvt;
1142 char name[NAME_SIZE], dai_name[NAME_SIZE];
1145 unsigned int rate_max = 384000, rate_min = 8000;
1149 hdmi_dais = devm_kzalloc(&hdac->dev,
1150 (sizeof(*hdmi_dais) * num_dais),
1155 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1156 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1157 &rates, &formats, &bps);
1161 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1162 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1163 dai_name, GFP_KERNEL);
1165 if (!hdmi_dais[i].name)
1168 snprintf(name, sizeof(name), "hifi%d", i+1);
1169 hdmi_dais[i].playback.stream_name =
1170 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1171 if (!hdmi_dais[i].playback.stream_name)
1175 * Set caps based on capability queried from the converter.
1176 * It will be constrained runtime based on ELD queried.
1178 hdmi_dais[i].playback.formats = formats;
1179 hdmi_dais[i].playback.rates = rates;
1180 hdmi_dais[i].playback.rate_max = rate_max;
1181 hdmi_dais[i].playback.rate_min = rate_min;
1182 hdmi_dais[i].playback.channels_min = 2;
1183 hdmi_dais[i].playback.channels_max = 2;
1184 hdmi_dais[i].ops = &hdmi_dai_ops;
1195 * Parse all nodes and store the cvt/pin nids in array
1196 * Add one time initialization for pin and cvt widgets
1198 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1199 struct snd_soc_dai_driver **dais, int *num_dais)
1203 struct hdac_device *hdac = &edev->hdac;
1204 struct hdac_hdmi_priv *hdmi = edev->private_data;
1207 hdac_hdmi_skl_enable_all_pins(hdac);
1208 hdac_hdmi_skl_enable_dp12(hdac);
1210 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1211 if (!nid || num_nodes <= 0) {
1212 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1216 hdac->num_nodes = num_nodes;
1217 hdac->start_nid = nid;
1219 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1223 caps = get_wcaps(hdac, nid);
1224 type = get_wcaps_type(caps);
1226 if (!(caps & AC_WCAP_DIGITAL))
1231 case AC_WID_AUD_OUT:
1232 ret = hdac_hdmi_add_cvt(edev, nid);
1238 ret = hdac_hdmi_add_pin(edev, nid);
1245 hdac->end_nid = nid;
1247 if (!hdmi->num_pin || !hdmi->num_cvt)
1250 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1252 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1257 *num_dais = hdmi->num_cvt;
1259 return hdac_hdmi_init_dai_map(edev);
1262 static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1264 struct hdac_ext_device *edev = aptr;
1265 struct hdac_hdmi_priv *hdmi = edev->private_data;
1266 struct hdac_hdmi_pin *pin;
1267 struct snd_soc_codec *codec = edev->scodec;
1269 /* Don't know how this mapping is derived */
1270 hda_nid_t pin_nid = port + 0x04;
1272 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1275 * skip notification during system suspend (but not in runtime PM);
1276 * the state will be updated at resume. Also since the ELD and
1277 * connection states are updated in anyway at the end of the resume,
1278 * we can skip it when received during PM process.
1280 if (snd_power_get_state(codec->component.card->snd_card) !=
1284 if (atomic_read(&edev->hdac.in_pm))
1287 list_for_each_entry(pin, &hdmi->pin_list, head) {
1288 if (pin->nid == pin_nid)
1289 hdac_hdmi_present_sense(pin, 1);
1293 static struct i915_audio_component_audio_ops aops = {
1294 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1297 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1299 char jack_name[NAME_SIZE];
1300 struct snd_soc_codec *codec = dai->codec;
1301 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1302 struct snd_soc_dapm_context *dapm =
1303 snd_soc_component_get_dapm(&codec->component);
1304 struct hdac_hdmi_priv *hdmi = edev->private_data;
1305 struct hdac_hdmi_pcm *pcm;
1308 * this is a new PCM device, create new pcm and
1309 * add to the pcm list
1311 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1314 pcm->pcm_id = device;
1315 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1317 list_add_tail(&pcm->head, &hdmi->pcm_list);
1319 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1321 return snd_jack_new(dapm->card->snd_card, jack_name,
1322 SND_JACK_AVOUT, &pcm->jack, true, false);
1324 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1326 static int hdmi_codec_probe(struct snd_soc_codec *codec)
1328 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1329 struct hdac_hdmi_priv *hdmi = edev->private_data;
1330 struct snd_soc_dapm_context *dapm =
1331 snd_soc_component_get_dapm(&codec->component);
1332 struct hdac_hdmi_pin *pin;
1335 edev->scodec = codec;
1337 ret = create_fill_widget_route_map(dapm);
1341 aops.audio_ptr = edev;
1342 ret = snd_hdac_i915_register_notifier(&aops);
1344 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1349 list_for_each_entry(pin, &hdmi->pin_list, head)
1350 hdac_hdmi_present_sense(pin, 1);
1352 /* Imp: Store the card pointer in hda_codec */
1353 edev->card = dapm->card->snd_card;
1356 * hdac_device core already sets the state to active and calls
1357 * get_noresume. So enable runtime and set the device to suspend.
1359 pm_runtime_enable(&edev->hdac.dev);
1360 pm_runtime_put(&edev->hdac.dev);
1361 pm_runtime_suspend(&edev->hdac.dev);
1366 static int hdmi_codec_remove(struct snd_soc_codec *codec)
1368 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1370 pm_runtime_disable(&edev->hdac.dev);
1374 static struct snd_soc_codec_driver hdmi_hda_codec = {
1375 .probe = hdmi_codec_probe,
1376 .remove = hdmi_codec_remove,
1377 .idle_bias_off = true,
1380 static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1382 struct hdac_device *codec = &edev->hdac;
1383 struct hdac_hdmi_priv *hdmi_priv;
1384 struct snd_soc_dai_driver *hdmi_dais = NULL;
1388 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1389 if (hdmi_priv == NULL)
1392 edev->private_data = hdmi_priv;
1394 dev_set_drvdata(&codec->dev, edev);
1396 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1397 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1398 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1399 mutex_init(&hdmi_priv->pin_mutex);
1401 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1403 dev_err(&codec->dev,
1404 "Failed in parse and map nid with err: %d\n", ret);
1408 /* ASoC specific initialization */
1409 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1410 hdmi_dais, num_dais);
1413 static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1415 struct hdac_hdmi_priv *hdmi = edev->private_data;
1416 struct hdac_hdmi_pin *pin, *pin_next;
1417 struct hdac_hdmi_cvt *cvt, *cvt_next;
1418 struct hdac_hdmi_pcm *pcm, *pcm_next;
1420 snd_soc_unregister_codec(&edev->hdac.dev);
1422 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1425 list_del(&pcm->head);
1429 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1430 list_del(&cvt->head);
1435 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1436 list_del(&pin->head);
1444 static int hdac_hdmi_runtime_suspend(struct device *dev)
1446 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1447 struct hdac_device *hdac = &edev->hdac;
1448 struct hdac_bus *bus = hdac->bus;
1451 dev_dbg(dev, "Enter: %s\n", __func__);
1453 /* controller may not have been initialized for the first time */
1457 /* Power down afg */
1458 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1459 snd_hdac_codec_write(hdac, hdac->afg, 0,
1460 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1462 err = snd_hdac_display_power(bus, false);
1464 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1471 static int hdac_hdmi_runtime_resume(struct device *dev)
1473 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1474 struct hdac_device *hdac = &edev->hdac;
1475 struct hdac_bus *bus = hdac->bus;
1478 dev_dbg(dev, "Enter: %s\n", __func__);
1480 /* controller may not have been initialized for the first time */
1484 err = snd_hdac_display_power(bus, true);
1486 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1491 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1492 snd_hdac_codec_write(hdac, hdac->afg, 0,
1493 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1498 #define hdac_hdmi_runtime_suspend NULL
1499 #define hdac_hdmi_runtime_resume NULL
1502 static const struct dev_pm_ops hdac_hdmi_pm = {
1503 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1506 static const struct hda_device_id hdmi_list[] = {
1507 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1511 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1513 static struct hdac_ext_driver hdmi_driver = {
1516 .name = "HDMI HDA Codec",
1517 .pm = &hdac_hdmi_pm,
1519 .id_table = hdmi_list,
1521 .probe = hdac_hdmi_dev_probe,
1522 .remove = hdac_hdmi_dev_remove,
1525 static int __init hdmi_init(void)
1527 return snd_hda_ext_driver_register(&hdmi_driver);
1530 static void __exit hdmi_exit(void)
1532 snd_hda_ext_driver_unregister(&hdmi_driver);
1535 module_init(hdmi_init);
1536 module_exit(hdmi_exit);
1538 MODULE_LICENSE("GPL v2");
1539 MODULE_DESCRIPTION("HDMI HD codec");
1540 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1541 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");