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
46 #define ELD_MAX_SIZE 256
47 #define ELD_FIXED_BYTES 20
49 struct hdac_hdmi_cvt_params {
50 unsigned int channels_min;
51 unsigned int channels_max;
57 struct hdac_hdmi_cvt {
58 struct list_head head;
61 struct hdac_hdmi_cvt_params params;
64 /* Currently only spk_alloc, more to be added */
65 struct hdac_hdmi_parsed_eld {
69 struct hdac_hdmi_eld {
73 char eld_buffer[ELD_MAX_SIZE];
74 struct hdac_hdmi_parsed_eld info;
77 struct hdac_hdmi_pin {
78 struct list_head head;
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
82 struct hdac_hdmi_eld eld;
83 struct hdac_ext_device *edev;
85 struct delayed_work work;
88 unsigned char chmap[8]; /* ALSA API channel-map */
89 int channels; /* current number of channels */
92 struct hdac_hdmi_pcm {
93 struct list_head head;
95 struct hdac_hdmi_pin *pin;
96 struct hdac_hdmi_cvt *cvt;
97 struct snd_jack *jack;
100 struct hdac_hdmi_dai_pin_map {
102 struct hdac_hdmi_pin *pin;
103 struct hdac_hdmi_cvt *cvt;
106 struct hdac_hdmi_priv {
107 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
108 struct list_head pin_list;
109 struct list_head cvt_list;
110 struct list_head pcm_list;
113 struct mutex pin_mutex;
114 struct hdac_chmap chmap;
117 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
120 struct hdac_hdmi_pcm *pcm;
122 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
123 if (pcm->pcm_id == pcm_idx)
130 static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
132 struct hdac_device *hdac = dev_to_hdac_dev(dev);
134 return to_ehdac_device(hdac);
137 static unsigned int sad_format(const u8 *sad)
139 return ((sad[0] >> 0x3) & 0x1f);
142 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
147 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
150 u64 formats = SNDRV_PCM_FMTBIT_S16;
152 const u8 *sad, *eld_buf = eld;
154 sad = drm_eld_sad(eld_buf);
156 goto format_constraint;
158 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
159 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
162 * the controller support 20 and 24 bits in 32 bit
163 * container so we set S32
165 if (sad_sample_bits_lpcm(sad) & 0x6)
166 formats |= SNDRV_PCM_FMTBIT_S32;
171 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
176 /* HDMI ELD routines */
177 static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
178 hda_nid_t nid, int byte_index)
182 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
185 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
191 static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
193 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
198 * This function queries the ELD size and ELD data and fills in the buffer
201 static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
202 unsigned char *buf, int *eld_size)
204 int i, size, ret = 0;
207 * ELD size is initialized to zero in caller function. If no errors and
208 * ELD is valid, actual eld_size is assigned.
211 size = hdac_hdmi_get_eld_size(codec, nid);
212 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
213 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
218 for (i = 0; i < size; i++) {
219 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
221 * Graphics driver might be writing to ELD buffer right now.
222 * Just abort. The caller will repoll after a while.
224 if (!(val & AC_ELDD_ELD_VALID)) {
226 "HDMI: invalid ELD data byte %d\n", i);
230 val &= AC_ELDD_ELD_DATA;
232 * The first byte cannot be zero. This can happen on some DVI
233 * connections. Some Intel chips may also need some 250ms delay
234 * to return non-zero ELD data, even when the graphics driver
235 * correctly writes ELD content before setting ELD_valid bit.
238 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
250 static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
251 hda_nid_t cvt_nid, hda_nid_t pin_nid,
252 u32 stream_tag, int format)
256 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
257 cvt_nid, pin_nid, stream_tag, format);
259 val = (stream_tag << 4);
261 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
262 AC_VERB_SET_CHANNEL_STREAMID, val);
263 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
264 AC_VERB_SET_STREAM_FORMAT, format);
270 hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
271 int packet_index, int byte_index)
275 val = (packet_index << 5) | (byte_index & 0x1f);
277 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
278 AC_VERB_SET_HDMI_DIP_INDEX, val);
281 struct dp_audio_infoframe {
284 u8 ver; /* 0x11 << 2 */
286 u8 CC02_CT47; /* match with HDMI infoframe from this on */
290 u8 LFEPBL01_LSV36_DM_INH7;
293 static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
294 hda_nid_t cvt_nid, hda_nid_t pin_nid)
296 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
297 struct hdmi_audio_infoframe frame;
298 struct dp_audio_infoframe dp_ai;
299 struct hdac_hdmi_priv *hdmi = hdac->private_data;
300 struct hdac_hdmi_pin *pin;
308 list_for_each_entry(pin, &hdmi->pin_list, head) {
309 if (pin->nid == pin_nid)
313 ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
314 pin->channels, pin->chmap_set, true, pin->chmap);
316 channels = snd_hdac_get_active_channels(ca);
317 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
319 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
320 pin->channels, pin->chmap, pin->chmap_set);
322 eld_buf = pin->eld.eld_buffer;
323 conn_type = drm_eld_get_conn_type(eld_buf);
326 case DRM_ELD_CONN_TYPE_HDMI:
327 hdmi_audio_infoframe_init(&frame);
329 frame.channels = channels;
330 frame.channel_allocation = ca;
332 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
338 case DRM_ELD_CONN_TYPE_DP:
339 memset(&dp_ai, 0, sizeof(dp_ai));
342 dp_ai.ver = 0x11 << 2;
343 dp_ai.CC02_CT47 = channels - 1;
350 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
355 /* stop infoframe transmission */
356 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
357 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
358 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
361 /* Fill infoframe. Index auto-incremented */
362 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
363 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
364 for (i = 0; i < sizeof(buffer); i++)
365 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
366 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
368 for (i = 0; i < sizeof(dp_ai); i++)
369 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
370 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
373 /* Start infoframe */
374 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
375 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
376 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
381 static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
382 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
384 /* Power up pin widget */
385 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
387 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
388 AC_VERB_SET_POWER_STATE, pwr_state);
390 /* Power up converter */
391 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
393 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
394 AC_VERB_SET_POWER_STATE, pwr_state);
397 static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
398 struct snd_soc_dai *dai)
400 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
401 struct hdac_hdmi_priv *hdmi = hdac->private_data;
402 struct hdac_hdmi_dai_pin_map *dai_map;
403 struct hdac_hdmi_pin *pin;
404 struct hdac_ext_dma_params *dd;
407 dai_map = &hdmi->dai_map[dai->id];
410 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
411 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
412 dd->stream_tag, dd->format);
414 mutex_lock(&pin->lock);
415 pin->channels = substream->runtime->channels;
417 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
419 mutex_unlock(&pin->lock);
423 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
424 dai_map->pin->nid, dd->stream_tag, dd->format);
427 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
428 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
430 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
431 struct hdac_hdmi_priv *hdmi = hdac->private_data;
432 struct hdac_hdmi_dai_pin_map *dai_map;
433 struct hdac_hdmi_pin *pin;
434 struct hdac_ext_dma_params *dd;
436 dai_map = &hdmi->dai_map[dai->id];
442 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
443 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
448 dd = snd_soc_dai_get_dma_data(dai, substream);
450 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
455 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
456 params_channels(hparams), params_format(hparams),
459 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
464 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
465 struct snd_soc_dai *dai)
467 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
468 struct hdac_ext_dma_params *dd;
469 struct hdac_hdmi_priv *hdmi = edev->private_data;
470 struct hdac_hdmi_dai_pin_map *dai_map;
472 dai_map = &hdmi->dai_map[dai->id];
474 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
477 snd_soc_dai_set_dma_data(dai, substream, NULL);
484 static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
485 struct hdac_hdmi_dai_pin_map *dai_map)
487 /* Enable transmission */
488 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
489 AC_VERB_SET_DIGI_CONVERT_1, 1);
491 /* Category Code (CC) to zero */
492 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
493 AC_VERB_SET_DIGI_CONVERT_2, 0);
496 static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
497 struct hdac_hdmi_dai_pin_map *dai_map)
500 struct hdac_hdmi_pin *pin = dai_map->pin;
502 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
503 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
504 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
505 AC_VERB_SET_CONNECT_SEL, mux_idx);
510 if (mux_idx == pin->num_mux_nids)
513 /* Enable out path for this pin widget */
514 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
515 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
517 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
519 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
520 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
525 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
526 struct hdac_hdmi_pin *pin)
528 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
529 dev_warn(&hdac->hdac.dev,
530 "HDMI: pin %d wcaps %#x does not support connection list\n",
531 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
535 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
536 pin->mux_nids, HDA_MAX_CONNECTIONS);
537 if (pin->num_mux_nids == 0)
538 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
541 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
542 pin->num_mux_nids, pin->nid);
544 return pin->num_mux_nids;
548 * Query pcm list and return pin widget to which stream is routed.
550 * Also query connection list of the pin, to validate the cvt to pin map.
552 * Same stream rendering to multiple pins simultaneously can be done
553 * possibly, but not supported for now in driver. So return the first pin
556 static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
557 struct hdac_ext_device *edev,
558 struct hdac_hdmi_priv *hdmi,
559 struct hdac_hdmi_cvt *cvt)
561 struct hdac_hdmi_pcm *pcm;
562 struct hdac_hdmi_pin *pin = NULL;
565 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
566 if (pcm->cvt == cvt) {
573 ret = hdac_hdmi_query_pin_connlist(edev, pin);
577 for (i = 0; i < pin->num_mux_nids; i++) {
578 if (pin->mux_nids[i] == cvt->nid)
587 * This tries to get a valid pin and set the HW constraints based on the
588 * ELD. Even if a valid pin is not found return success so that device open
591 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
592 struct snd_soc_dai *dai)
594 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
595 struct hdac_hdmi_priv *hdmi = hdac->private_data;
596 struct hdac_hdmi_dai_pin_map *dai_map;
597 struct hdac_hdmi_cvt *cvt;
598 struct hdac_hdmi_pin *pin;
601 dai_map = &hdmi->dai_map[dai->id];
604 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
607 * To make PA and other userland happy.
608 * userland scans devices so returning error does not help.
613 if ((!pin->eld.monitor_present) ||
614 (!pin->eld.eld_valid)) {
616 dev_warn(&hdac->hdac.dev,
617 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
618 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
625 hdac_hdmi_enable_cvt(hdac, dai_map);
626 ret = hdac_hdmi_enable_pin(hdac, dai_map);
630 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
631 pin->eld.eld_buffer);
635 return snd_pcm_hw_constraint_eld(substream->runtime,
636 pin->eld.eld_buffer);
639 static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
640 struct snd_soc_dai *dai)
642 struct hdac_hdmi_dai_pin_map *dai_map;
643 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
644 struct hdac_hdmi_priv *hdmi = hdac->private_data;
647 dai_map = &hdmi->dai_map[dai->id];
648 if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
649 ret = hdac_hdmi_enable_pin(hdac, dai_map);
653 return hdac_hdmi_playback_prepare(substream, dai);
659 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
660 struct snd_soc_dai *dai)
662 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
663 struct hdac_hdmi_priv *hdmi = hdac->private_data;
664 struct hdac_hdmi_dai_pin_map *dai_map;
666 dai_map = &hdmi->dai_map[dai->id];
669 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
670 AC_VERB_SET_CHANNEL_STREAMID, 0);
671 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
672 AC_VERB_SET_STREAM_FORMAT, 0);
674 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
676 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
677 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
679 mutex_lock(&dai_map->pin->lock);
680 dai_map->pin->chmap_set = false;
681 memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
682 dai_map->pin->channels = 0;
683 mutex_unlock(&dai_map->pin->lock);
690 hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
693 struct hdac_ext_device *edev = to_ehdac_device(hdac);
694 struct hdac_hdmi_priv *hdmi = edev->private_data;
697 chans = get_wcaps(hdac, cvt->nid);
698 chans = get_wcaps_channels(chans);
700 cvt->params.channels_min = 2;
702 cvt->params.channels_max = chans;
703 if (chans > hdmi->chmap.channels_max)
704 hdmi->chmap.channels_max = chans;
706 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
708 &cvt->params.formats,
709 &cvt->params.maxbps);
712 "Failed to query pcm params for nid %d: %d\n",
718 static int hdac_hdmi_fill_widget_info(struct device *dev,
719 struct snd_soc_dapm_widget *w,
720 enum snd_soc_dapm_type id, void *priv,
721 const char *wname, const char *stream,
722 struct snd_kcontrol_new *wc, int numkc)
725 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
730 w->reg = SND_SOC_NOPM;
732 w->kcontrol_news = wc;
733 w->num_kcontrols = numkc;
739 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
740 const char *sink, const char *control, const char *src,
741 int (*handler)(struct snd_soc_dapm_widget *src,
742 struct snd_soc_dapm_widget *sink))
746 route->control = control;
747 route->connected = handler;
750 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
751 struct hdac_hdmi_pin *pin)
753 struct hdac_hdmi_priv *hdmi = edev->private_data;
754 struct hdac_hdmi_pcm *pcm = NULL;
756 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
765 * Based on user selection, map the PINs with the PCMs.
767 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
768 struct snd_ctl_elem_value *ucontrol)
771 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
772 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
773 struct snd_soc_dapm_context *dapm = w->dapm;
774 struct hdac_hdmi_pin *pin = w->priv;
775 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
776 struct hdac_hdmi_priv *hdmi = edev->private_data;
777 struct hdac_hdmi_pcm *pcm = NULL;
778 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
780 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
784 mutex_lock(&hdmi->pin_mutex);
785 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
790 * Jack status is not reported during device probe as the
791 * PCMs are not registered by then. So report it here.
793 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
795 if (pin->eld.monitor_present && pin->eld.eld_valid) {
796 dev_dbg(&edev->hdac.dev,
797 "jack report for pcm=%d\n",
800 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
802 mutex_unlock(&hdmi->pin_mutex);
806 mutex_unlock(&hdmi->pin_mutex);
812 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
813 * the display driver seem to be programming the connection list for the pin
816 * So programming all the possible inputs for the mux, the user has to take
817 * care of selecting the right one and leaving all other inputs selected to
820 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
821 struct hdac_hdmi_pin *pin,
822 struct snd_soc_dapm_widget *widget,
823 const char *widget_name)
825 struct hdac_hdmi_priv *hdmi = edev->private_data;
826 struct snd_kcontrol_new *kc;
827 struct hdac_hdmi_cvt *cvt;
829 char kc_name[NAME_SIZE];
830 char mux_items[NAME_SIZE];
831 /* To hold inputs to the Pin mux */
832 char *items[HDA_MAX_CONNECTIONS];
834 int num_items = hdmi->num_cvt + 1;
836 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
840 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
844 sprintf(kc_name, "Pin %d Input", pin->nid);
845 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
849 kc->private_value = (long)se;
850 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
852 kc->info = snd_soc_info_enum_double;
853 kc->put = hdac_hdmi_set_pin_mux;
854 kc->get = snd_soc_dapm_get_enum_double;
856 se->reg = SND_SOC_NOPM;
858 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
859 se->items = num_items;
860 se->mask = roundup_pow_of_two(se->items) - 1;
862 sprintf(mux_items, "NONE");
863 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
867 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
869 sprintf(mux_items, "cvt %d", cvt->nid);
870 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
875 se->texts = devm_kmemdup(&edev->hdac.dev, items,
876 (num_items * sizeof(char *)), GFP_KERNEL);
880 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
881 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
884 /* Add cvt <- input <- mux route map */
885 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
886 struct snd_soc_dapm_widget *widgets,
887 struct snd_soc_dapm_route *route, int rindex)
889 struct hdac_hdmi_priv *hdmi = edev->private_data;
890 const struct snd_kcontrol_new *kc;
892 int mux_index = hdmi->num_cvt + hdmi->num_pin;
895 for (i = 0; i < hdmi->num_pin; i++) {
896 kc = widgets[mux_index].kcontrol_news;
897 se = (struct soc_enum *)kc->private_value;
898 for (j = 0; j < hdmi->num_cvt; j++) {
899 hdac_hdmi_fill_route(&route[rindex],
900 widgets[mux_index].name,
902 widgets[j].name, NULL);
912 * Widgets are added in the below sequence
913 * Converter widgets for num converters enumerated
914 * Pin widgets for num pins enumerated
915 * Pin mux widgets to represent connenction list of pin widget
917 * Total widgets elements = num_cvt + num_pin + num_pin;
919 * Routes are added as below:
920 * pin mux -> pin (based on num_pins)
921 * cvt -> "Input sel control" -> pin_mux
923 * Total route elements:
924 * num_pins + (pin_muxes * num_cvt)
926 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
928 struct snd_soc_dapm_widget *widgets;
929 struct snd_soc_dapm_route *route;
930 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
931 struct hdac_hdmi_priv *hdmi = edev->private_data;
932 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
933 char widget_name[NAME_SIZE];
934 struct hdac_hdmi_cvt *cvt;
935 struct hdac_hdmi_pin *pin;
936 int ret, i = 0, num_routes = 0;
938 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
941 widgets = devm_kzalloc(dapm->dev,
942 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
948 /* DAPM widgets to represent each converter widget */
949 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
950 sprintf(widget_name, "Converter %d", cvt->nid);
951 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
952 snd_soc_dapm_aif_in, &cvt->nid,
953 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
959 list_for_each_entry(pin, &hdmi->pin_list, head) {
960 sprintf(widget_name, "hif%d Output", pin->nid);
961 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
962 snd_soc_dapm_output, &pin->nid,
963 widget_name, NULL, NULL, 0);
969 /* DAPM widgets to represent the connection list to pin widget */
970 list_for_each_entry(pin, &hdmi->pin_list, head) {
971 sprintf(widget_name, "Pin %d Mux", pin->nid);
972 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
978 /* For cvt to pin_mux mapping */
979 num_routes += hdmi->num_cvt;
981 /* For pin_mux to pin mapping */
985 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
991 /* Add pin <- NULL <- mux route map */
992 list_for_each_entry(pin, &hdmi->pin_list, head) {
993 int sink_index = i + hdmi->num_cvt;
994 int src_index = sink_index + hdmi->num_pin;
996 hdac_hdmi_fill_route(&route[i],
997 widgets[sink_index].name, NULL,
998 widgets[src_index].name, NULL);
1003 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
1005 snd_soc_dapm_new_controls(dapm, widgets,
1006 ((2 * hdmi->num_pin) + hdmi->num_cvt));
1008 snd_soc_dapm_add_routes(dapm, route, num_routes);
1009 snd_soc_dapm_new_widgets(dapm->card);
1015 static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
1017 struct hdac_hdmi_priv *hdmi = edev->private_data;
1018 struct hdac_hdmi_dai_pin_map *dai_map;
1019 struct hdac_hdmi_cvt *cvt;
1022 if (list_empty(&hdmi->cvt_list))
1025 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1026 dai_map = &hdmi->dai_map[dai_id];
1027 dai_map->dai_id = dai_id;
1032 if (dai_id == HDA_MAX_CVTS) {
1033 dev_warn(&edev->hdac.dev,
1034 "Max dais supported: %d\n", dai_id);
1042 static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1044 struct hdac_hdmi_priv *hdmi = edev->private_data;
1045 struct hdac_hdmi_cvt *cvt;
1046 char name[NAME_SIZE];
1048 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1053 sprintf(name, "cvt %d", cvt->nid);
1054 cvt->name = kstrdup(name, GFP_KERNEL);
1056 list_add_tail(&cvt->head, &hdmi->cvt_list);
1059 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1062 static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1063 struct hdac_hdmi_pin *pin)
1065 pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1068 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1070 struct hdac_ext_device *edev = pin->edev;
1071 struct hdac_hdmi_priv *hdmi = edev->private_data;
1072 struct hdac_hdmi_pcm *pcm;
1075 pin->repoll_count = repoll;
1077 pm_runtime_get_sync(&edev->hdac.dev);
1078 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1079 AC_VERB_GET_PIN_SENSE, 0);
1081 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1085 mutex_lock(&hdmi->pin_mutex);
1086 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1087 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1089 pcm = hdac_hdmi_get_pcm(edev, pin);
1091 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1093 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1094 __func__, pin->nid);
1097 * PCMs are not registered during device probe, so don't
1098 * report jack here. It will be done in usermode mux
1102 dev_dbg(&edev->hdac.dev,
1103 "jack report for pcm=%d\n", pcm->pcm_id);
1105 snd_jack_report(pcm->jack, 0);
1108 mutex_unlock(&hdmi->pin_mutex);
1109 goto put_hdac_device;
1112 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1113 /* TODO: use i915 component for reading ELD later */
1114 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1115 pin->eld.eld_buffer,
1116 &pin->eld.eld_size) == 0) {
1119 dev_dbg(&edev->hdac.dev,
1120 "jack report for pcm=%d\n",
1123 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1125 hdac_hdmi_parse_eld(edev, pin);
1127 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1128 pin->eld.eld_buffer, pin->eld.eld_size);
1130 pin->eld.monitor_present = false;
1131 pin->eld.eld_valid = false;
1134 dev_dbg(&edev->hdac.dev,
1135 "jack report for pcm=%d\n",
1138 snd_jack_report(pcm->jack, 0);
1143 mutex_unlock(&hdmi->pin_mutex);
1146 * Sometimes the pin_sense may present invalid monitor
1147 * present and eld_valid. If ELD data is not valid, loop few
1148 * more times to get correct pin sense and valid ELD.
1150 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1151 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1154 pm_runtime_put_sync(&edev->hdac.dev);
1157 static void hdac_hdmi_repoll_eld(struct work_struct *work)
1159 struct hdac_hdmi_pin *pin =
1160 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1162 /* picked from legacy HDA driver */
1163 if (pin->repoll_count++ > 6)
1164 pin->repoll_count = 0;
1166 hdac_hdmi_present_sense(pin, pin->repoll_count);
1169 static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1171 struct hdac_hdmi_priv *hdmi = edev->private_data;
1172 struct hdac_hdmi_pin *pin;
1174 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1180 list_add_tail(&pin->head, &hdmi->pin_list);
1184 mutex_init(&pin->lock);
1185 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1190 #define INTEL_VENDOR_NID 0x08
1191 #define INTEL_GET_VENDOR_VERB 0xf81
1192 #define INTEL_SET_VENDOR_VERB 0x781
1193 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1194 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1196 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1198 unsigned int vendor_param;
1200 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1201 INTEL_GET_VENDOR_VERB, 0);
1202 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1205 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1206 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1207 INTEL_SET_VENDOR_VERB, vendor_param);
1208 if (vendor_param == -1)
1212 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1214 unsigned int vendor_param;
1216 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1217 INTEL_GET_VENDOR_VERB, 0);
1218 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1221 /* enable DP1.2 mode */
1222 vendor_param |= INTEL_EN_DP12;
1223 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1224 INTEL_SET_VENDOR_VERB, vendor_param);
1225 if (vendor_param == -1)
1230 static struct snd_soc_dai_ops hdmi_dai_ops = {
1231 .startup = hdac_hdmi_pcm_open,
1232 .shutdown = hdac_hdmi_pcm_close,
1233 .hw_params = hdac_hdmi_set_hw_params,
1234 .prepare = hdac_hdmi_playback_prepare,
1235 .trigger = hdac_hdmi_trigger,
1236 .hw_free = hdac_hdmi_playback_cleanup,
1240 * Each converter can support a stream independently. So a dai is created
1241 * based on the number of converter queried.
1243 static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1244 struct snd_soc_dai_driver **dais,
1245 struct hdac_hdmi_priv *hdmi, int num_dais)
1247 struct snd_soc_dai_driver *hdmi_dais;
1248 struct hdac_hdmi_cvt *cvt;
1249 char name[NAME_SIZE], dai_name[NAME_SIZE];
1252 unsigned int rate_max = 384000, rate_min = 8000;
1256 hdmi_dais = devm_kzalloc(&hdac->dev,
1257 (sizeof(*hdmi_dais) * num_dais),
1262 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1263 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1264 &rates, &formats, &bps);
1268 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1269 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1270 dai_name, GFP_KERNEL);
1272 if (!hdmi_dais[i].name)
1275 snprintf(name, sizeof(name), "hifi%d", i+1);
1276 hdmi_dais[i].playback.stream_name =
1277 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1278 if (!hdmi_dais[i].playback.stream_name)
1282 * Set caps based on capability queried from the converter.
1283 * It will be constrained runtime based on ELD queried.
1285 hdmi_dais[i].playback.formats = formats;
1286 hdmi_dais[i].playback.rates = rates;
1287 hdmi_dais[i].playback.rate_max = rate_max;
1288 hdmi_dais[i].playback.rate_min = rate_min;
1289 hdmi_dais[i].playback.channels_min = 2;
1290 hdmi_dais[i].playback.channels_max = 2;
1291 hdmi_dais[i].ops = &hdmi_dai_ops;
1302 * Parse all nodes and store the cvt/pin nids in array
1303 * Add one time initialization for pin and cvt widgets
1305 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1306 struct snd_soc_dai_driver **dais, int *num_dais)
1310 struct hdac_device *hdac = &edev->hdac;
1311 struct hdac_hdmi_priv *hdmi = edev->private_data;
1314 hdac_hdmi_skl_enable_all_pins(hdac);
1315 hdac_hdmi_skl_enable_dp12(hdac);
1317 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1318 if (!nid || num_nodes <= 0) {
1319 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1323 hdac->num_nodes = num_nodes;
1324 hdac->start_nid = nid;
1326 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1330 caps = get_wcaps(hdac, nid);
1331 type = get_wcaps_type(caps);
1333 if (!(caps & AC_WCAP_DIGITAL))
1338 case AC_WID_AUD_OUT:
1339 ret = hdac_hdmi_add_cvt(edev, nid);
1345 ret = hdac_hdmi_add_pin(edev, nid);
1352 hdac->end_nid = nid;
1354 if (!hdmi->num_pin || !hdmi->num_cvt)
1357 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1359 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1364 *num_dais = hdmi->num_cvt;
1366 return hdac_hdmi_init_dai_map(edev);
1369 static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1371 struct hdac_ext_device *edev = aptr;
1372 struct hdac_hdmi_priv *hdmi = edev->private_data;
1373 struct hdac_hdmi_pin *pin;
1374 struct snd_soc_codec *codec = edev->scodec;
1376 /* Don't know how this mapping is derived */
1377 hda_nid_t pin_nid = port + 0x04;
1379 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1382 * skip notification during system suspend (but not in runtime PM);
1383 * the state will be updated at resume. Also since the ELD and
1384 * connection states are updated in anyway at the end of the resume,
1385 * we can skip it when received during PM process.
1387 if (snd_power_get_state(codec->component.card->snd_card) !=
1391 if (atomic_read(&edev->hdac.in_pm))
1394 list_for_each_entry(pin, &hdmi->pin_list, head) {
1395 if (pin->nid == pin_nid)
1396 hdac_hdmi_present_sense(pin, 1);
1400 static struct i915_audio_component_audio_ops aops = {
1401 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1404 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1407 struct snd_soc_pcm_runtime *rtd;
1409 list_for_each_entry(rtd, &card->rtd_list, list) {
1410 if (rtd->pcm && (rtd->pcm->device == device))
1417 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1419 char jack_name[NAME_SIZE];
1420 struct snd_soc_codec *codec = dai->codec;
1421 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1422 struct snd_soc_dapm_context *dapm =
1423 snd_soc_component_get_dapm(&codec->component);
1424 struct hdac_hdmi_priv *hdmi = edev->private_data;
1425 struct hdac_hdmi_pcm *pcm;
1426 struct snd_pcm *snd_pcm;
1430 * this is a new PCM device, create new pcm and
1431 * add to the pcm list
1433 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1436 pcm->pcm_id = device;
1437 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1439 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1441 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1443 dev_err(&edev->hdac.dev,
1444 "chmap control add failed with err: %d for pcm: %d\n",
1451 list_add_tail(&pcm->head, &hdmi->pcm_list);
1453 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1455 return snd_jack_new(dapm->card->snd_card, jack_name,
1456 SND_JACK_AVOUT, &pcm->jack, true, false);
1458 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1460 static int hdmi_codec_probe(struct snd_soc_codec *codec)
1462 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1463 struct hdac_hdmi_priv *hdmi = edev->private_data;
1464 struct snd_soc_dapm_context *dapm =
1465 snd_soc_component_get_dapm(&codec->component);
1466 struct hdac_hdmi_pin *pin;
1467 struct hdac_ext_link *hlink = NULL;
1470 edev->scodec = codec;
1473 * hold the ref while we probe, also no need to drop the ref on
1474 * exit, we call pm_runtime_suspend() so that will do for us
1476 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1478 dev_err(&edev->hdac.dev, "hdac link not found\n");
1482 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1484 ret = create_fill_widget_route_map(dapm);
1488 aops.audio_ptr = edev;
1489 ret = snd_hdac_i915_register_notifier(&aops);
1491 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1496 list_for_each_entry(pin, &hdmi->pin_list, head)
1497 hdac_hdmi_present_sense(pin, 1);
1499 /* Imp: Store the card pointer in hda_codec */
1500 edev->card = dapm->card->snd_card;
1503 * hdac_device core already sets the state to active and calls
1504 * get_noresume. So enable runtime and set the device to suspend.
1506 pm_runtime_enable(&edev->hdac.dev);
1507 pm_runtime_put(&edev->hdac.dev);
1508 pm_runtime_suspend(&edev->hdac.dev);
1513 static int hdmi_codec_remove(struct snd_soc_codec *codec)
1515 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1517 pm_runtime_disable(&edev->hdac.dev);
1522 static int hdmi_codec_prepare(struct device *dev)
1524 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1525 struct hdac_device *hdac = &edev->hdac;
1527 pm_runtime_get_sync(&edev->hdac.dev);
1531 * codec_read is preferred over codec_write to set the power state.
1532 * This way verb is send to set the power state and response
1533 * is received. So setting power state is ensured without using loop
1534 * to read the state.
1536 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1542 static void hdmi_codec_complete(struct device *dev)
1544 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1545 struct hdac_hdmi_priv *hdmi = edev->private_data;
1546 struct hdac_hdmi_pin *pin;
1547 struct hdac_device *hdac = &edev->hdac;
1550 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1553 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1554 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1557 * As the ELD notify callback request is not entertained while the
1558 * device is in suspend state. Need to manually check detection of
1561 list_for_each_entry(pin, &hdmi->pin_list, head)
1562 hdac_hdmi_present_sense(pin, 1);
1564 pm_runtime_put_sync(&edev->hdac.dev);
1567 #define hdmi_codec_prepare NULL
1568 #define hdmi_codec_complete NULL
1571 static struct snd_soc_codec_driver hdmi_hda_codec = {
1572 .probe = hdmi_codec_probe,
1573 .remove = hdmi_codec_remove,
1574 .idle_bias_off = true,
1577 static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1578 unsigned char *chmap)
1580 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1581 struct hdac_hdmi_priv *hdmi = edev->private_data;
1582 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1583 struct hdac_hdmi_pin *pin = pcm->pin;
1585 /* chmap is already set to 0 in caller */
1589 memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
1592 static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1593 unsigned char *chmap, int prepared)
1595 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1596 struct hdac_hdmi_priv *hdmi = edev->private_data;
1597 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1598 struct hdac_hdmi_pin *pin = pcm->pin;
1600 mutex_lock(&pin->lock);
1601 pin->chmap_set = true;
1602 memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
1604 hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
1605 mutex_unlock(&pin->lock);
1608 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1610 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1611 struct hdac_hdmi_priv *hdmi = edev->private_data;
1612 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1613 struct hdac_hdmi_pin *pin = pcm->pin;
1615 return pin ? true:false;
1618 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1620 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1621 struct hdac_hdmi_priv *hdmi = edev->private_data;
1622 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1623 struct hdac_hdmi_pin *pin = pcm->pin;
1625 if (!pin || !pin->eld.eld_valid)
1628 return pin->eld.info.spk_alloc;
1631 static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1633 struct hdac_device *codec = &edev->hdac;
1634 struct hdac_hdmi_priv *hdmi_priv;
1635 struct snd_soc_dai_driver *hdmi_dais = NULL;
1636 struct hdac_ext_link *hlink = NULL;
1640 /* hold the ref while we probe */
1641 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
1643 dev_err(&edev->hdac.dev, "hdac link not found\n");
1647 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1649 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1650 if (hdmi_priv == NULL)
1653 edev->private_data = hdmi_priv;
1654 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
1655 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1656 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1657 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1658 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
1660 dev_set_drvdata(&codec->dev, edev);
1662 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1663 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1664 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1665 mutex_init(&hdmi_priv->pin_mutex);
1668 * Turned off in the runtime_suspend during the first explicit
1669 * pm_runtime_suspend call.
1671 ret = snd_hdac_display_power(edev->hdac.bus, true);
1673 dev_err(&edev->hdac.dev,
1674 "Cannot turn on display power on i915 err: %d\n",
1679 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1681 dev_err(&codec->dev,
1682 "Failed in parse and map nid with err: %d\n", ret);
1686 /* ASoC specific initialization */
1687 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1688 hdmi_dais, num_dais);
1690 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1695 static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1697 struct hdac_hdmi_priv *hdmi = edev->private_data;
1698 struct hdac_hdmi_pin *pin, *pin_next;
1699 struct hdac_hdmi_cvt *cvt, *cvt_next;
1700 struct hdac_hdmi_pcm *pcm, *pcm_next;
1702 snd_soc_unregister_codec(&edev->hdac.dev);
1704 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1707 list_del(&pcm->head);
1711 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1712 list_del(&cvt->head);
1717 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1718 list_del(&pin->head);
1726 static int hdac_hdmi_runtime_suspend(struct device *dev)
1728 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1729 struct hdac_device *hdac = &edev->hdac;
1730 struct hdac_bus *bus = hdac->bus;
1731 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1732 struct hdac_ext_link *hlink = NULL;
1735 dev_dbg(dev, "Enter: %s\n", __func__);
1737 /* controller may not have been initialized for the first time */
1743 * codec_read is preferred over codec_write to set the power state.
1744 * This way verb is send to set the power state and response
1745 * is received. So setting power state is ensured without using loop
1746 * to read the state.
1748 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1750 err = snd_hdac_display_power(bus, false);
1752 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1756 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1758 dev_err(dev, "hdac link not found\n");
1762 snd_hdac_ext_bus_link_put(ebus, hlink);
1767 static int hdac_hdmi_runtime_resume(struct device *dev)
1769 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1770 struct hdac_device *hdac = &edev->hdac;
1771 struct hdac_bus *bus = hdac->bus;
1772 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1773 struct hdac_ext_link *hlink = NULL;
1776 dev_dbg(dev, "Enter: %s\n", __func__);
1778 /* controller may not have been initialized for the first time */
1782 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
1784 dev_err(dev, "hdac link not found\n");
1788 snd_hdac_ext_bus_link_get(ebus, hlink);
1790 err = snd_hdac_display_power(bus, true);
1792 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1796 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1797 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1800 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1806 #define hdac_hdmi_runtime_suspend NULL
1807 #define hdac_hdmi_runtime_resume NULL
1810 static const struct dev_pm_ops hdac_hdmi_pm = {
1811 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1812 .prepare = hdmi_codec_prepare,
1813 .complete = hdmi_codec_complete,
1816 static const struct hda_device_id hdmi_list[] = {
1817 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1818 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1822 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1824 static struct hdac_ext_driver hdmi_driver = {
1827 .name = "HDMI HDA Codec",
1828 .pm = &hdac_hdmi_pm,
1830 .id_table = hdmi_list,
1832 .probe = hdac_hdmi_dev_probe,
1833 .remove = hdac_hdmi_dev_remove,
1836 static int __init hdmi_init(void)
1838 return snd_hda_ext_driver_register(&hdmi_driver);
1841 static void __exit hdmi_exit(void)
1843 snd_hda_ext_driver_unregister(&hdmi_driver);
1846 module_init(hdmi_init);
1847 module_exit(hdmi_exit);
1849 MODULE_LICENSE("GPL v2");
1850 MODULE_DESCRIPTION("HDMI HD codec");
1851 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1852 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");