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 /* Currently only spk_alloc, more to be added */
64 struct hdac_hdmi_parsed_eld
{
68 struct hdac_hdmi_eld
{
72 char eld_buffer
[ELD_MAX_SIZE
];
73 struct hdac_hdmi_parsed_eld info
;
76 struct hdac_hdmi_pin
{
77 struct list_head head
;
80 hda_nid_t mux_nids
[HDA_MAX_CONNECTIONS
];
81 struct hdac_hdmi_eld eld
;
82 struct hdac_ext_device
*edev
;
84 struct delayed_work work
;
87 struct hdac_hdmi_pcm
{
88 struct list_head head
;
90 struct hdac_hdmi_pin
*pin
;
91 struct hdac_hdmi_cvt
*cvt
;
92 struct snd_jack
*jack
;
95 struct hdac_hdmi_dai_pin_map
{
97 struct hdac_hdmi_pin
*pin
;
98 struct hdac_hdmi_cvt
*cvt
;
101 struct hdac_hdmi_priv
{
102 struct hdac_hdmi_dai_pin_map dai_map
[HDA_MAX_CVTS
];
103 struct list_head pin_list
;
104 struct list_head cvt_list
;
105 struct list_head pcm_list
;
108 struct mutex pin_mutex
;
111 static inline struct hdac_ext_device
*to_hda_ext_device(struct device
*dev
)
113 struct hdac_device
*hdac
= dev_to_hdac_dev(dev
);
115 return to_ehdac_device(hdac
);
118 static unsigned int sad_format(const u8
*sad
)
120 return ((sad
[0] >> 0x3) & 0x1f);
123 static unsigned int sad_sample_bits_lpcm(const u8
*sad
)
128 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime
*runtime
,
131 u64 formats
= SNDRV_PCM_FMTBIT_S16
;
133 const u8
*sad
, *eld_buf
= eld
;
135 sad
= drm_eld_sad(eld_buf
);
137 goto format_constraint
;
139 for (i
= drm_eld_sad_count(eld_buf
); i
> 0; i
--, sad
+= 3) {
140 if (sad_format(sad
) == 1) { /* AUDIO_CODING_TYPE_LPCM */
143 * the controller support 20 and 24 bits in 32 bit
144 * container so we set S32
146 if (sad_sample_bits_lpcm(sad
) & 0x6)
147 formats
|= SNDRV_PCM_FMTBIT_S32
;
152 return snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
,
157 /* HDMI ELD routines */
158 static unsigned int hdac_hdmi_get_eld_data(struct hdac_device
*codec
,
159 hda_nid_t nid
, int byte_index
)
163 val
= snd_hdac_codec_read(codec
, nid
, 0, AC_VERB_GET_HDMI_ELDD
,
166 dev_dbg(&codec
->dev
, "HDMI: ELD data byte %d: 0x%x\n",
172 static int hdac_hdmi_get_eld_size(struct hdac_device
*codec
, hda_nid_t nid
)
174 return snd_hdac_codec_read(codec
, nid
, 0, AC_VERB_GET_HDMI_DIP_SIZE
,
179 * This function queries the ELD size and ELD data and fills in the buffer
182 static int hdac_hdmi_get_eld(struct hdac_device
*codec
, hda_nid_t nid
,
183 unsigned char *buf
, int *eld_size
)
185 int i
, size
, ret
= 0;
188 * ELD size is initialized to zero in caller function. If no errors and
189 * ELD is valid, actual eld_size is assigned.
192 size
= hdac_hdmi_get_eld_size(codec
, nid
);
193 if (size
< ELD_FIXED_BYTES
|| size
> ELD_MAX_SIZE
) {
194 dev_err(&codec
->dev
, "HDMI: invalid ELD buf size %d\n", size
);
199 for (i
= 0; i
< size
; i
++) {
200 unsigned int val
= hdac_hdmi_get_eld_data(codec
, nid
, i
);
202 * Graphics driver might be writing to ELD buffer right now.
203 * Just abort. The caller will repoll after a while.
205 if (!(val
& AC_ELDD_ELD_VALID
)) {
207 "HDMI: invalid ELD data byte %d\n", i
);
211 val
&= AC_ELDD_ELD_DATA
;
213 * The first byte cannot be zero. This can happen on some DVI
214 * connections. Some Intel chips may also need some 250ms delay
215 * to return non-zero ELD data, even when the graphics driver
216 * correctly writes ELD content before setting ELD_valid bit.
219 dev_err(&codec
->dev
, "HDMI: 0 ELD data\n");
231 static int hdac_hdmi_setup_stream(struct hdac_ext_device
*hdac
,
232 hda_nid_t cvt_nid
, hda_nid_t pin_nid
,
233 u32 stream_tag
, int format
)
237 dev_dbg(&hdac
->hdac
.dev
, "cvt nid %d pnid %d stream %d format 0x%x\n",
238 cvt_nid
, pin_nid
, stream_tag
, format
);
240 val
= (stream_tag
<< 4);
242 snd_hdac_codec_write(&hdac
->hdac
, cvt_nid
, 0,
243 AC_VERB_SET_CHANNEL_STREAMID
, val
);
244 snd_hdac_codec_write(&hdac
->hdac
, cvt_nid
, 0,
245 AC_VERB_SET_STREAM_FORMAT
, format
);
251 hdac_hdmi_set_dip_index(struct hdac_ext_device
*hdac
, hda_nid_t pin_nid
,
252 int packet_index
, int byte_index
)
256 val
= (packet_index
<< 5) | (byte_index
& 0x1f);
258 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
259 AC_VERB_SET_HDMI_DIP_INDEX
, val
);
262 struct dp_audio_infoframe
{
265 u8 ver
; /* 0x11 << 2 */
267 u8 CC02_CT47
; /* match with HDMI infoframe from this on */
271 u8 LFEPBL01_LSV36_DM_INH7
;
274 static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device
*hdac
,
275 hda_nid_t cvt_nid
, hda_nid_t pin_nid
)
277 uint8_t buffer
[HDMI_INFOFRAME_HEADER_SIZE
+ HDMI_AUDIO_INFOFRAME_SIZE
];
278 struct hdmi_audio_infoframe frame
;
279 struct dp_audio_infoframe dp_ai
;
280 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
281 struct hdac_hdmi_pin
*pin
;
289 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
290 if (pin
->nid
== pin_nid
)
294 eld_buf
= pin
->eld
.eld_buffer
;
295 conn_type
= drm_eld_get_conn_type(eld_buf
);
297 /* setup channel count */
298 snd_hdac_codec_write(&hdac
->hdac
, cvt_nid
, 0,
299 AC_VERB_SET_CVT_CHAN_COUNT
, channels
- 1);
302 case DRM_ELD_CONN_TYPE_HDMI
:
303 hdmi_audio_infoframe_init(&frame
);
305 /* Default stereo for now */
306 frame
.channels
= channels
;
308 ret
= hdmi_audio_infoframe_pack(&frame
, buffer
, sizeof(buffer
));
314 case DRM_ELD_CONN_TYPE_DP
:
315 memset(&dp_ai
, 0, sizeof(dp_ai
));
318 dp_ai
.ver
= 0x11 << 2;
319 dp_ai
.CC02_CT47
= channels
- 1;
326 dev_err(&hdac
->hdac
.dev
, "Invalid connection type: %d\n",
331 /* stop infoframe transmission */
332 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
333 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
334 AC_VERB_SET_HDMI_DIP_XMIT
, AC_DIPXMIT_DISABLE
);
337 /* Fill infoframe. Index auto-incremented */
338 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
339 if (conn_type
== DRM_ELD_CONN_TYPE_HDMI
) {
340 for (i
= 0; i
< sizeof(buffer
); i
++)
341 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
342 AC_VERB_SET_HDMI_DIP_DATA
, buffer
[i
]);
344 for (i
= 0; i
< sizeof(dp_ai
); i
++)
345 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
346 AC_VERB_SET_HDMI_DIP_DATA
, dip
[i
]);
349 /* Start infoframe */
350 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
351 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
352 AC_VERB_SET_HDMI_DIP_XMIT
, AC_DIPXMIT_BEST
);
357 static void hdac_hdmi_set_power_state(struct hdac_ext_device
*edev
,
358 struct hdac_hdmi_dai_pin_map
*dai_map
, unsigned int pwr_state
)
360 /* Power up pin widget */
361 if (!snd_hdac_check_power_state(&edev
->hdac
, dai_map
->pin
->nid
,
363 snd_hdac_codec_write(&edev
->hdac
, dai_map
->pin
->nid
, 0,
364 AC_VERB_SET_POWER_STATE
, pwr_state
);
366 /* Power up converter */
367 if (!snd_hdac_check_power_state(&edev
->hdac
, dai_map
->cvt
->nid
,
369 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
370 AC_VERB_SET_POWER_STATE
, pwr_state
);
373 static int hdac_hdmi_playback_prepare(struct snd_pcm_substream
*substream
,
374 struct snd_soc_dai
*dai
)
376 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
377 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
378 struct hdac_hdmi_dai_pin_map
*dai_map
;
379 struct hdac_ext_dma_params
*dd
;
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_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
;
406 dai_map
= &hdmi
->dai_map
[dai
->id
];
412 if ((!pin
->eld
.monitor_present
) || (!pin
->eld
.eld_valid
)) {
413 dev_err(&hdac
->hdac
.dev
, "device is not configured for this pin: %d\n",
418 dd
= snd_soc_dai_get_dma_data(dai
, substream
);
420 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
425 dd
->format
= snd_hdac_calc_stream_format(params_rate(hparams
),
426 params_channels(hparams
), params_format(hparams
),
429 snd_soc_dai_set_dma_data(dai
, substream
, (void *)dd
);
434 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream
*substream
,
435 struct snd_soc_dai
*dai
)
437 struct hdac_ext_device
*edev
= snd_soc_dai_get_drvdata(dai
);
438 struct hdac_ext_dma_params
*dd
;
439 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
440 struct hdac_hdmi_dai_pin_map
*dai_map
;
442 dai_map
= &hdmi
->dai_map
[dai
->id
];
444 dd
= (struct hdac_ext_dma_params
*)snd_soc_dai_get_dma_data(dai
, substream
);
447 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
454 static void hdac_hdmi_enable_cvt(struct hdac_ext_device
*edev
,
455 struct hdac_hdmi_dai_pin_map
*dai_map
)
457 /* Enable transmission */
458 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
459 AC_VERB_SET_DIGI_CONVERT_1
, 1);
461 /* Category Code (CC) to zero */
462 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
463 AC_VERB_SET_DIGI_CONVERT_2
, 0);
466 static int hdac_hdmi_enable_pin(struct hdac_ext_device
*hdac
,
467 struct hdac_hdmi_dai_pin_map
*dai_map
)
470 struct hdac_hdmi_pin
*pin
= dai_map
->pin
;
472 for (mux_idx
= 0; mux_idx
< pin
->num_mux_nids
; mux_idx
++) {
473 if (pin
->mux_nids
[mux_idx
] == dai_map
->cvt
->nid
) {
474 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
475 AC_VERB_SET_CONNECT_SEL
, mux_idx
);
480 if (mux_idx
== pin
->num_mux_nids
)
483 /* Enable out path for this pin widget */
484 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
485 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
487 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D0
);
489 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
490 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_UNMUTE
);
495 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device
*hdac
,
496 struct hdac_hdmi_pin
*pin
)
498 if (!(get_wcaps(&hdac
->hdac
, pin
->nid
) & AC_WCAP_CONN_LIST
)) {
499 dev_warn(&hdac
->hdac
.dev
,
500 "HDMI: pin %d wcaps %#x does not support connection list\n",
501 pin
->nid
, get_wcaps(&hdac
->hdac
, pin
->nid
));
505 pin
->num_mux_nids
= snd_hdac_get_connections(&hdac
->hdac
, pin
->nid
,
506 pin
->mux_nids
, HDA_MAX_CONNECTIONS
);
507 if (pin
->num_mux_nids
== 0)
508 dev_warn(&hdac
->hdac
.dev
, "No connections found for pin: %d\n",
511 dev_dbg(&hdac
->hdac
.dev
, "num_mux_nids %d for pin: %d\n",
512 pin
->num_mux_nids
, pin
->nid
);
514 return pin
->num_mux_nids
;
518 * Query pcm list and return pin widget to which stream is routed.
520 * Also query connection list of the pin, to validate the cvt to pin map.
522 * Same stream rendering to multiple pins simultaneously can be done
523 * possibly, but not supported for now in driver. So return the first pin
526 static struct hdac_hdmi_pin
*hdac_hdmi_get_pin_from_cvt(
527 struct hdac_ext_device
*edev
,
528 struct hdac_hdmi_priv
*hdmi
,
529 struct hdac_hdmi_cvt
*cvt
)
531 struct hdac_hdmi_pcm
*pcm
;
532 struct hdac_hdmi_pin
*pin
= NULL
;
535 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
536 if (pcm
->cvt
== cvt
) {
543 ret
= hdac_hdmi_query_pin_connlist(edev
, pin
);
547 for (i
= 0; i
< pin
->num_mux_nids
; i
++) {
548 if (pin
->mux_nids
[i
] == cvt
->nid
)
557 * This tries to get a valid pin and set the HW constraints based on the
558 * ELD. Even if a valid pin is not found return success so that device open
561 static int hdac_hdmi_pcm_open(struct snd_pcm_substream
*substream
,
562 struct snd_soc_dai
*dai
)
564 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
565 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
566 struct hdac_hdmi_dai_pin_map
*dai_map
;
567 struct hdac_hdmi_cvt
*cvt
;
568 struct hdac_hdmi_pin
*pin
;
571 dai_map
= &hdmi
->dai_map
[dai
->id
];
574 pin
= hdac_hdmi_get_pin_from_cvt(hdac
, hdmi
, cvt
);
577 * To make PA and other userland happy.
578 * userland scans devices so returning error does not help.
583 if ((!pin
->eld
.monitor_present
) ||
584 (!pin
->eld
.eld_valid
)) {
586 dev_warn(&hdac
->hdac
.dev
,
587 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
588 pin
->eld
.monitor_present
, pin
->eld
.eld_valid
, pin
->nid
);
595 hdac_hdmi_enable_cvt(hdac
, dai_map
);
596 ret
= hdac_hdmi_enable_pin(hdac
, dai_map
);
600 ret
= hdac_hdmi_eld_limit_formats(substream
->runtime
,
601 pin
->eld
.eld_buffer
);
605 return snd_pcm_hw_constraint_eld(substream
->runtime
,
606 pin
->eld
.eld_buffer
);
609 static int hdac_hdmi_trigger(struct snd_pcm_substream
*substream
, int cmd
,
610 struct snd_soc_dai
*dai
)
612 struct hdac_hdmi_dai_pin_map
*dai_map
;
613 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
614 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
617 dai_map
= &hdmi
->dai_map
[dai
->id
];
618 if (cmd
== SNDRV_PCM_TRIGGER_RESUME
) {
619 ret
= hdac_hdmi_enable_pin(hdac
, dai_map
);
623 return hdac_hdmi_playback_prepare(substream
, dai
);
629 static void hdac_hdmi_pcm_close(struct snd_pcm_substream
*substream
,
630 struct snd_soc_dai
*dai
)
632 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
633 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
634 struct hdac_hdmi_dai_pin_map
*dai_map
;
636 dai_map
= &hdmi
->dai_map
[dai
->id
];
639 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
640 AC_VERB_SET_CHANNEL_STREAMID
, 0);
641 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
642 AC_VERB_SET_STREAM_FORMAT
, 0);
644 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D3
);
646 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->pin
->nid
, 0,
647 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
654 hdac_hdmi_query_cvt_params(struct hdac_device
*hdac
, struct hdac_hdmi_cvt
*cvt
)
658 /* Only stereo supported as of now */
659 cvt
->params
.channels_min
= cvt
->params
.channels_max
= 2;
661 err
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
663 &cvt
->params
.formats
,
664 &cvt
->params
.maxbps
);
667 "Failed to query pcm params for nid %d: %d\n",
673 static int hdac_hdmi_fill_widget_info(struct device
*dev
,
674 struct snd_soc_dapm_widget
*w
,
675 enum snd_soc_dapm_type id
, void *priv
,
676 const char *wname
, const char *stream
,
677 struct snd_kcontrol_new
*wc
, int numkc
)
680 w
->name
= devm_kstrdup(dev
, wname
, GFP_KERNEL
);
685 w
->reg
= SND_SOC_NOPM
;
687 w
->kcontrol_news
= wc
;
688 w
->num_kcontrols
= numkc
;
694 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route
*route
,
695 const char *sink
, const char *control
, const char *src
,
696 int (*handler
)(struct snd_soc_dapm_widget
*src
,
697 struct snd_soc_dapm_widget
*sink
))
701 route
->control
= control
;
702 route
->connected
= handler
;
705 static struct hdac_hdmi_pcm
*hdac_hdmi_get_pcm(struct hdac_ext_device
*edev
,
706 struct hdac_hdmi_pin
*pin
)
708 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
709 struct hdac_hdmi_pcm
*pcm
= NULL
;
711 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
720 * Based on user selection, map the PINs with the PCMs.
722 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol
*kcontrol
,
723 struct snd_ctl_elem_value
*ucontrol
)
726 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
727 struct snd_soc_dapm_widget
*w
= snd_soc_dapm_kcontrol_widget(kcontrol
);
728 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
729 struct hdac_hdmi_pin
*pin
= w
->priv
;
730 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
731 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
732 struct hdac_hdmi_pcm
*pcm
= NULL
;
733 const char *cvt_name
= e
->texts
[ucontrol
->value
.enumerated
.item
[0]];
735 ret
= snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
739 mutex_lock(&hdmi
->pin_mutex
);
740 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
745 * Jack status is not reported during device probe as the
746 * PCMs are not registered by then. So report it here.
748 if (!strcmp(cvt_name
, pcm
->cvt
->name
) && !pcm
->pin
) {
750 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
751 dev_dbg(&edev
->hdac
.dev
,
752 "jack report for pcm=%d\n",
755 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
757 mutex_unlock(&hdmi
->pin_mutex
);
761 mutex_unlock(&hdmi
->pin_mutex
);
767 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
768 * the display driver seem to be programming the connection list for the pin
771 * So programming all the possible inputs for the mux, the user has to take
772 * care of selecting the right one and leaving all other inputs selected to
775 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device
*edev
,
776 struct hdac_hdmi_pin
*pin
,
777 struct snd_soc_dapm_widget
*widget
,
778 const char *widget_name
)
780 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
781 struct snd_kcontrol_new
*kc
;
782 struct hdac_hdmi_cvt
*cvt
;
784 char kc_name
[NAME_SIZE
];
785 char mux_items
[NAME_SIZE
];
786 /* To hold inputs to the Pin mux */
787 char *items
[HDA_MAX_CONNECTIONS
];
789 int num_items
= hdmi
->num_cvt
+ 1;
791 kc
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*kc
), GFP_KERNEL
);
795 se
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*se
), GFP_KERNEL
);
799 sprintf(kc_name
, "Pin %d Input", pin
->nid
);
800 kc
->name
= devm_kstrdup(&edev
->hdac
.dev
, kc_name
, GFP_KERNEL
);
804 kc
->private_value
= (long)se
;
805 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
807 kc
->info
= snd_soc_info_enum_double
;
808 kc
->put
= hdac_hdmi_set_pin_mux
;
809 kc
->get
= snd_soc_dapm_get_enum_double
;
811 se
->reg
= SND_SOC_NOPM
;
813 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
814 se
->items
= num_items
;
815 se
->mask
= roundup_pow_of_two(se
->items
) - 1;
817 sprintf(mux_items
, "NONE");
818 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
822 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
824 sprintf(mux_items
, "cvt %d", cvt
->nid
);
825 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
830 se
->texts
= devm_kmemdup(&edev
->hdac
.dev
, items
,
831 (num_items
* sizeof(char *)), GFP_KERNEL
);
835 return hdac_hdmi_fill_widget_info(&edev
->hdac
.dev
, widget
,
836 snd_soc_dapm_mux
, pin
, widget_name
, NULL
, kc
, 1);
839 /* Add cvt <- input <- mux route map */
840 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device
*edev
,
841 struct snd_soc_dapm_widget
*widgets
,
842 struct snd_soc_dapm_route
*route
, int rindex
)
844 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
845 const struct snd_kcontrol_new
*kc
;
847 int mux_index
= hdmi
->num_cvt
+ hdmi
->num_pin
;
850 for (i
= 0; i
< hdmi
->num_pin
; i
++) {
851 kc
= widgets
[mux_index
].kcontrol_news
;
852 se
= (struct soc_enum
*)kc
->private_value
;
853 for (j
= 0; j
< hdmi
->num_cvt
; j
++) {
854 hdac_hdmi_fill_route(&route
[rindex
],
855 widgets
[mux_index
].name
,
857 widgets
[j
].name
, NULL
);
867 * Widgets are added in the below sequence
868 * Converter widgets for num converters enumerated
869 * Pin widgets for num pins enumerated
870 * Pin mux widgets to represent connenction list of pin widget
872 * Total widgets elements = num_cvt + num_pin + num_pin;
874 * Routes are added as below:
875 * pin mux -> pin (based on num_pins)
876 * cvt -> "Input sel control" -> pin_mux
878 * Total route elements:
879 * num_pins + (pin_muxes * num_cvt)
881 static int create_fill_widget_route_map(struct snd_soc_dapm_context
*dapm
)
883 struct snd_soc_dapm_widget
*widgets
;
884 struct snd_soc_dapm_route
*route
;
885 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
886 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
887 struct snd_soc_dai_driver
*dai_drv
= dapm
->component
->dai_drv
;
888 char widget_name
[NAME_SIZE
];
889 struct hdac_hdmi_cvt
*cvt
;
890 struct hdac_hdmi_pin
*pin
;
891 int ret
, i
= 0, num_routes
= 0;
893 if (list_empty(&hdmi
->cvt_list
) || list_empty(&hdmi
->pin_list
))
896 widgets
= devm_kzalloc(dapm
->dev
,
897 (sizeof(*widgets
) * ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
)),
903 /* DAPM widgets to represent each converter widget */
904 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
905 sprintf(widget_name
, "Converter %d", cvt
->nid
);
906 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
907 snd_soc_dapm_aif_in
, &cvt
->nid
,
908 widget_name
, dai_drv
[i
].playback
.stream_name
, NULL
, 0);
914 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
915 sprintf(widget_name
, "hif%d Output", pin
->nid
);
916 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
917 snd_soc_dapm_output
, &pin
->nid
,
918 widget_name
, NULL
, NULL
, 0);
924 /* DAPM widgets to represent the connection list to pin widget */
925 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
926 sprintf(widget_name
, "Pin %d Mux", pin
->nid
);
927 ret
= hdac_hdmi_create_pin_muxs(edev
, pin
, &widgets
[i
],
933 /* For cvt to pin_mux mapping */
934 num_routes
+= hdmi
->num_cvt
;
936 /* For pin_mux to pin mapping */
940 route
= devm_kzalloc(dapm
->dev
, (sizeof(*route
) * num_routes
),
946 /* Add pin <- NULL <- mux route map */
947 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
948 int sink_index
= i
+ hdmi
->num_cvt
;
949 int src_index
= sink_index
+ hdmi
->num_pin
;
951 hdac_hdmi_fill_route(&route
[i
],
952 widgets
[sink_index
].name
, NULL
,
953 widgets
[src_index
].name
, NULL
);
958 hdac_hdmi_add_pinmux_cvt_route(edev
, widgets
, route
, i
);
960 snd_soc_dapm_new_controls(dapm
, widgets
,
961 ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
));
963 snd_soc_dapm_add_routes(dapm
, route
, num_routes
);
964 snd_soc_dapm_new_widgets(dapm
->card
);
970 static int hdac_hdmi_init_dai_map(struct hdac_ext_device
*edev
)
972 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
973 struct hdac_hdmi_dai_pin_map
*dai_map
;
974 struct hdac_hdmi_cvt
*cvt
;
977 if (list_empty(&hdmi
->cvt_list
))
980 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
981 dai_map
= &hdmi
->dai_map
[dai_id
];
982 dai_map
->dai_id
= dai_id
;
987 if (dai_id
== HDA_MAX_CVTS
) {
988 dev_warn(&edev
->hdac
.dev
,
989 "Max dais supported: %d\n", dai_id
);
997 static int hdac_hdmi_add_cvt(struct hdac_ext_device
*edev
, hda_nid_t nid
)
999 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1000 struct hdac_hdmi_cvt
*cvt
;
1001 char name
[NAME_SIZE
];
1003 cvt
= kzalloc(sizeof(*cvt
), GFP_KERNEL
);
1008 sprintf(name
, "cvt %d", cvt
->nid
);
1009 cvt
->name
= kstrdup(name
, GFP_KERNEL
);
1011 list_add_tail(&cvt
->head
, &hdmi
->cvt_list
);
1014 return hdac_hdmi_query_cvt_params(&edev
->hdac
, cvt
);
1017 static void hdac_hdmi_parse_eld(struct hdac_ext_device
*edev
,
1018 struct hdac_hdmi_pin
*pin
)
1020 pin
->eld
.info
.spk_alloc
= pin
->eld
.eld_buffer
[DRM_ELD_SPEAKER
];
1023 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin
*pin
, int repoll
)
1025 struct hdac_ext_device
*edev
= pin
->edev
;
1026 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1027 struct hdac_hdmi_pcm
*pcm
;
1030 pin
->repoll_count
= repoll
;
1032 pm_runtime_get_sync(&edev
->hdac
.dev
);
1033 val
= snd_hdac_codec_read(&edev
->hdac
, pin
->nid
, 0,
1034 AC_VERB_GET_PIN_SENSE
, 0);
1036 dev_dbg(&edev
->hdac
.dev
, "Pin sense val %x for pin: %d\n",
1040 mutex_lock(&hdmi
->pin_mutex
);
1041 pin
->eld
.monitor_present
= !!(val
& AC_PINSENSE_PRESENCE
);
1042 pin
->eld
.eld_valid
= !!(val
& AC_PINSENSE_ELDV
);
1044 pcm
= hdac_hdmi_get_pcm(edev
, pin
);
1046 if (!pin
->eld
.monitor_present
|| !pin
->eld
.eld_valid
) {
1048 dev_dbg(&edev
->hdac
.dev
, "%s: disconnect for pin %d\n",
1049 __func__
, pin
->nid
);
1052 * PCMs are not registered during device probe, so don't
1053 * report jack here. It will be done in usermode mux
1057 dev_dbg(&edev
->hdac
.dev
,
1058 "jack report for pcm=%d\n", pcm
->pcm_id
);
1060 snd_jack_report(pcm
->jack
, 0);
1063 mutex_unlock(&hdmi
->pin_mutex
);
1064 goto put_hdac_device
;
1067 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
1068 /* TODO: use i915 component for reading ELD later */
1069 if (hdac_hdmi_get_eld(&edev
->hdac
, pin
->nid
,
1070 pin
->eld
.eld_buffer
,
1071 &pin
->eld
.eld_size
) == 0) {
1074 dev_dbg(&edev
->hdac
.dev
,
1075 "jack report for pcm=%d\n",
1078 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
1080 hdac_hdmi_parse_eld(edev
, pin
);
1082 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET
,
1083 pin
->eld
.eld_buffer
, pin
->eld
.eld_size
);
1085 pin
->eld
.monitor_present
= false;
1086 pin
->eld
.eld_valid
= false;
1089 dev_dbg(&edev
->hdac
.dev
,
1090 "jack report for pcm=%d\n",
1093 snd_jack_report(pcm
->jack
, 0);
1098 mutex_unlock(&hdmi
->pin_mutex
);
1101 * Sometimes the pin_sense may present invalid monitor
1102 * present and eld_valid. If ELD data is not valid, loop few
1103 * more times to get correct pin sense and valid ELD.
1105 if ((!pin
->eld
.monitor_present
|| !pin
->eld
.eld_valid
) && repoll
)
1106 schedule_delayed_work(&pin
->work
, msecs_to_jiffies(300));
1109 pm_runtime_put_sync(&edev
->hdac
.dev
);
1112 static void hdac_hdmi_repoll_eld(struct work_struct
*work
)
1114 struct hdac_hdmi_pin
*pin
=
1115 container_of(to_delayed_work(work
), struct hdac_hdmi_pin
, work
);
1117 /* picked from legacy HDA driver */
1118 if (pin
->repoll_count
++ > 6)
1119 pin
->repoll_count
= 0;
1121 hdac_hdmi_present_sense(pin
, pin
->repoll_count
);
1124 static int hdac_hdmi_add_pin(struct hdac_ext_device
*edev
, hda_nid_t nid
)
1126 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1127 struct hdac_hdmi_pin
*pin
;
1129 pin
= kzalloc(sizeof(*pin
), GFP_KERNEL
);
1135 list_add_tail(&pin
->head
, &hdmi
->pin_list
);
1139 INIT_DELAYED_WORK(&pin
->work
, hdac_hdmi_repoll_eld
);
1144 #define INTEL_VENDOR_NID 0x08
1145 #define INTEL_GET_VENDOR_VERB 0xf81
1146 #define INTEL_SET_VENDOR_VERB 0x781
1147 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1148 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1150 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device
*hdac
)
1152 unsigned int vendor_param
;
1154 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1155 INTEL_GET_VENDOR_VERB
, 0);
1156 if (vendor_param
== -1 || vendor_param
& INTEL_EN_ALL_PIN_CVTS
)
1159 vendor_param
|= INTEL_EN_ALL_PIN_CVTS
;
1160 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1161 INTEL_SET_VENDOR_VERB
, vendor_param
);
1162 if (vendor_param
== -1)
1166 static void hdac_hdmi_skl_enable_dp12(struct hdac_device
*hdac
)
1168 unsigned int vendor_param
;
1170 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1171 INTEL_GET_VENDOR_VERB
, 0);
1172 if (vendor_param
== -1 || vendor_param
& INTEL_EN_DP12
)
1175 /* enable DP1.2 mode */
1176 vendor_param
|= INTEL_EN_DP12
;
1177 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1178 INTEL_SET_VENDOR_VERB
, vendor_param
);
1179 if (vendor_param
== -1)
1184 static struct snd_soc_dai_ops hdmi_dai_ops
= {
1185 .startup
= hdac_hdmi_pcm_open
,
1186 .shutdown
= hdac_hdmi_pcm_close
,
1187 .hw_params
= hdac_hdmi_set_hw_params
,
1188 .prepare
= hdac_hdmi_playback_prepare
,
1189 .trigger
= hdac_hdmi_trigger
,
1190 .hw_free
= hdac_hdmi_playback_cleanup
,
1194 * Each converter can support a stream independently. So a dai is created
1195 * based on the number of converter queried.
1197 static int hdac_hdmi_create_dais(struct hdac_device
*hdac
,
1198 struct snd_soc_dai_driver
**dais
,
1199 struct hdac_hdmi_priv
*hdmi
, int num_dais
)
1201 struct snd_soc_dai_driver
*hdmi_dais
;
1202 struct hdac_hdmi_cvt
*cvt
;
1203 char name
[NAME_SIZE
], dai_name
[NAME_SIZE
];
1206 unsigned int rate_max
= 384000, rate_min
= 8000;
1210 hdmi_dais
= devm_kzalloc(&hdac
->dev
,
1211 (sizeof(*hdmi_dais
) * num_dais
),
1216 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
1217 ret
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
1218 &rates
, &formats
, &bps
);
1222 sprintf(dai_name
, "intel-hdmi-hifi%d", i
+1);
1223 hdmi_dais
[i
].name
= devm_kstrdup(&hdac
->dev
,
1224 dai_name
, GFP_KERNEL
);
1226 if (!hdmi_dais
[i
].name
)
1229 snprintf(name
, sizeof(name
), "hifi%d", i
+1);
1230 hdmi_dais
[i
].playback
.stream_name
=
1231 devm_kstrdup(&hdac
->dev
, name
, GFP_KERNEL
);
1232 if (!hdmi_dais
[i
].playback
.stream_name
)
1236 * Set caps based on capability queried from the converter.
1237 * It will be constrained runtime based on ELD queried.
1239 hdmi_dais
[i
].playback
.formats
= formats
;
1240 hdmi_dais
[i
].playback
.rates
= rates
;
1241 hdmi_dais
[i
].playback
.rate_max
= rate_max
;
1242 hdmi_dais
[i
].playback
.rate_min
= rate_min
;
1243 hdmi_dais
[i
].playback
.channels_min
= 2;
1244 hdmi_dais
[i
].playback
.channels_max
= 2;
1245 hdmi_dais
[i
].ops
= &hdmi_dai_ops
;
1256 * Parse all nodes and store the cvt/pin nids in array
1257 * Add one time initialization for pin and cvt widgets
1259 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device
*edev
,
1260 struct snd_soc_dai_driver
**dais
, int *num_dais
)
1264 struct hdac_device
*hdac
= &edev
->hdac
;
1265 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1268 hdac_hdmi_skl_enable_all_pins(hdac
);
1269 hdac_hdmi_skl_enable_dp12(hdac
);
1271 num_nodes
= snd_hdac_get_sub_nodes(hdac
, hdac
->afg
, &nid
);
1272 if (!nid
|| num_nodes
<= 0) {
1273 dev_warn(&hdac
->dev
, "HDMI: failed to get afg sub nodes\n");
1277 hdac
->num_nodes
= num_nodes
;
1278 hdac
->start_nid
= nid
;
1280 for (i
= 0; i
< hdac
->num_nodes
; i
++, nid
++) {
1284 caps
= get_wcaps(hdac
, nid
);
1285 type
= get_wcaps_type(caps
);
1287 if (!(caps
& AC_WCAP_DIGITAL
))
1292 case AC_WID_AUD_OUT
:
1293 ret
= hdac_hdmi_add_cvt(edev
, nid
);
1299 ret
= hdac_hdmi_add_pin(edev
, nid
);
1306 hdac
->end_nid
= nid
;
1308 if (!hdmi
->num_pin
|| !hdmi
->num_cvt
)
1311 ret
= hdac_hdmi_create_dais(hdac
, dais
, hdmi
, hdmi
->num_cvt
);
1313 dev_err(&hdac
->dev
, "Failed to create dais with err: %d\n",
1318 *num_dais
= hdmi
->num_cvt
;
1320 return hdac_hdmi_init_dai_map(edev
);
1323 static void hdac_hdmi_eld_notify_cb(void *aptr
, int port
)
1325 struct hdac_ext_device
*edev
= aptr
;
1326 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1327 struct hdac_hdmi_pin
*pin
;
1328 struct snd_soc_codec
*codec
= edev
->scodec
;
1330 /* Don't know how this mapping is derived */
1331 hda_nid_t pin_nid
= port
+ 0x04;
1333 dev_dbg(&edev
->hdac
.dev
, "%s: for pin: %d\n", __func__
, pin_nid
);
1336 * skip notification during system suspend (but not in runtime PM);
1337 * the state will be updated at resume. Also since the ELD and
1338 * connection states are updated in anyway at the end of the resume,
1339 * we can skip it when received during PM process.
1341 if (snd_power_get_state(codec
->component
.card
->snd_card
) !=
1345 if (atomic_read(&edev
->hdac
.in_pm
))
1348 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
1349 if (pin
->nid
== pin_nid
)
1350 hdac_hdmi_present_sense(pin
, 1);
1354 static struct i915_audio_component_audio_ops aops
= {
1355 .pin_eld_notify
= hdac_hdmi_eld_notify_cb
,
1358 int hdac_hdmi_jack_init(struct snd_soc_dai
*dai
, int device
)
1360 char jack_name
[NAME_SIZE
];
1361 struct snd_soc_codec
*codec
= dai
->codec
;
1362 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1363 struct snd_soc_dapm_context
*dapm
=
1364 snd_soc_component_get_dapm(&codec
->component
);
1365 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1366 struct hdac_hdmi_pcm
*pcm
;
1369 * this is a new PCM device, create new pcm and
1370 * add to the pcm list
1372 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
1375 pcm
->pcm_id
= device
;
1376 pcm
->cvt
= hdmi
->dai_map
[dai
->id
].cvt
;
1378 list_add_tail(&pcm
->head
, &hdmi
->pcm_list
);
1380 sprintf(jack_name
, "HDMI/DP, pcm=%d Jack", device
);
1382 return snd_jack_new(dapm
->card
->snd_card
, jack_name
,
1383 SND_JACK_AVOUT
, &pcm
->jack
, true, false);
1385 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init
);
1387 static int hdmi_codec_probe(struct snd_soc_codec
*codec
)
1389 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1390 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1391 struct snd_soc_dapm_context
*dapm
=
1392 snd_soc_component_get_dapm(&codec
->component
);
1393 struct hdac_hdmi_pin
*pin
;
1396 edev
->scodec
= codec
;
1398 ret
= create_fill_widget_route_map(dapm
);
1402 aops
.audio_ptr
= edev
;
1403 ret
= snd_hdac_i915_register_notifier(&aops
);
1405 dev_err(&edev
->hdac
.dev
, "notifier register failed: err: %d\n",
1410 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1411 hdac_hdmi_present_sense(pin
, 1);
1413 /* Imp: Store the card pointer in hda_codec */
1414 edev
->card
= dapm
->card
->snd_card
;
1417 * hdac_device core already sets the state to active and calls
1418 * get_noresume. So enable runtime and set the device to suspend.
1420 pm_runtime_enable(&edev
->hdac
.dev
);
1421 pm_runtime_put(&edev
->hdac
.dev
);
1422 pm_runtime_suspend(&edev
->hdac
.dev
);
1427 static int hdmi_codec_remove(struct snd_soc_codec
*codec
)
1429 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1431 pm_runtime_disable(&edev
->hdac
.dev
);
1436 static int hdmi_codec_resume(struct snd_soc_codec
*codec
)
1438 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1439 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1440 struct hdac_hdmi_pin
*pin
;
1441 struct hdac_device
*hdac
= &edev
->hdac
;
1442 struct hdac_bus
*bus
= hdac
->bus
;
1444 unsigned long timeout
;
1446 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1447 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1450 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
)) {
1452 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1453 AC_VERB_SET_POWER_STATE
, AC_PWRST_D0
);
1455 /* Wait till power state is set to D0 */
1456 timeout
= jiffies
+ msecs_to_jiffies(1000);
1457 while (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
)
1458 && time_before(jiffies
, timeout
)) {
1464 * As the ELD notify callback request is not entertained while the
1465 * device is in suspend state. Need to manually check detection of
1468 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1469 hdac_hdmi_present_sense(pin
, 1);
1472 * Codec power is turned ON during controller resume.
1475 err
= snd_hdac_display_power(bus
, false);
1478 "Cannot turn OFF display power on i915, err: %d\n",
1486 #define hdmi_codec_resume NULL
1489 static struct snd_soc_codec_driver hdmi_hda_codec
= {
1490 .probe
= hdmi_codec_probe
,
1491 .remove
= hdmi_codec_remove
,
1492 .resume
= hdmi_codec_resume
,
1493 .idle_bias_off
= true,
1496 static int hdac_hdmi_dev_probe(struct hdac_ext_device
*edev
)
1498 struct hdac_device
*codec
= &edev
->hdac
;
1499 struct hdac_hdmi_priv
*hdmi_priv
;
1500 struct snd_soc_dai_driver
*hdmi_dais
= NULL
;
1504 hdmi_priv
= devm_kzalloc(&codec
->dev
, sizeof(*hdmi_priv
), GFP_KERNEL
);
1505 if (hdmi_priv
== NULL
)
1508 edev
->private_data
= hdmi_priv
;
1510 dev_set_drvdata(&codec
->dev
, edev
);
1512 INIT_LIST_HEAD(&hdmi_priv
->pin_list
);
1513 INIT_LIST_HEAD(&hdmi_priv
->cvt_list
);
1514 INIT_LIST_HEAD(&hdmi_priv
->pcm_list
);
1515 mutex_init(&hdmi_priv
->pin_mutex
);
1518 * Turned off in the runtime_suspend during the first explicit
1519 * pm_runtime_suspend call.
1521 ret
= snd_hdac_display_power(edev
->hdac
.bus
, true);
1523 dev_err(&edev
->hdac
.dev
,
1524 "Cannot turn on display power on i915 err: %d\n",
1529 ret
= hdac_hdmi_parse_and_map_nid(edev
, &hdmi_dais
, &num_dais
);
1531 dev_err(&codec
->dev
,
1532 "Failed in parse and map nid with err: %d\n", ret
);
1536 /* ASoC specific initialization */
1537 return snd_soc_register_codec(&codec
->dev
, &hdmi_hda_codec
,
1538 hdmi_dais
, num_dais
);
1541 static int hdac_hdmi_dev_remove(struct hdac_ext_device
*edev
)
1543 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1544 struct hdac_hdmi_pin
*pin
, *pin_next
;
1545 struct hdac_hdmi_cvt
*cvt
, *cvt_next
;
1546 struct hdac_hdmi_pcm
*pcm
, *pcm_next
;
1548 snd_soc_unregister_codec(&edev
->hdac
.dev
);
1550 list_for_each_entry_safe(pcm
, pcm_next
, &hdmi
->pcm_list
, head
) {
1553 list_del(&pcm
->head
);
1557 list_for_each_entry_safe(cvt
, cvt_next
, &hdmi
->cvt_list
, head
) {
1558 list_del(&cvt
->head
);
1563 list_for_each_entry_safe(pin
, pin_next
, &hdmi
->pin_list
, head
) {
1564 list_del(&pin
->head
);
1572 static int hdac_hdmi_runtime_suspend(struct device
*dev
)
1574 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1575 struct hdac_device
*hdac
= &edev
->hdac
;
1576 struct hdac_bus
*bus
= hdac
->bus
;
1577 unsigned long timeout
;
1580 dev_dbg(dev
, "Enter: %s\n", __func__
);
1582 /* controller may not have been initialized for the first time */
1586 /* Power down afg */
1587 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D3
)) {
1588 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1589 AC_VERB_SET_POWER_STATE
, AC_PWRST_D3
);
1591 /* Wait till power state is set to D3 */
1592 timeout
= jiffies
+ msecs_to_jiffies(1000);
1593 while (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D3
)
1594 && time_before(jiffies
, timeout
)) {
1600 err
= snd_hdac_display_power(bus
, false);
1602 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1609 static int hdac_hdmi_runtime_resume(struct device
*dev
)
1611 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1612 struct hdac_device
*hdac
= &edev
->hdac
;
1613 struct hdac_bus
*bus
= hdac
->bus
;
1616 dev_dbg(dev
, "Enter: %s\n", __func__
);
1618 /* controller may not have been initialized for the first time */
1622 err
= snd_hdac_display_power(bus
, true);
1624 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1628 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1629 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1632 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
))
1633 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1634 AC_VERB_SET_POWER_STATE
, AC_PWRST_D0
);
1639 #define hdac_hdmi_runtime_suspend NULL
1640 #define hdac_hdmi_runtime_resume NULL
1643 static const struct dev_pm_ops hdac_hdmi_pm
= {
1644 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend
, hdac_hdmi_runtime_resume
, NULL
)
1647 static const struct hda_device_id hdmi_list
[] = {
1648 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1649 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1653 MODULE_DEVICE_TABLE(hdaudio
, hdmi_list
);
1655 static struct hdac_ext_driver hdmi_driver
= {
1658 .name
= "HDMI HDA Codec",
1659 .pm
= &hdac_hdmi_pm
,
1661 .id_table
= hdmi_list
,
1663 .probe
= hdac_hdmi_dev_probe
,
1664 .remove
= hdac_hdmi_dev_remove
,
1667 static int __init
hdmi_init(void)
1669 return snd_hda_ext_driver_register(&hdmi_driver
);
1672 static void __exit
hdmi_exit(void)
1674 snd_hda_ext_driver_unregister(&hdmi_driver
);
1677 module_init(hdmi_init
);
1678 module_exit(hdmi_exit
);
1680 MODULE_LICENSE("GPL v2");
1681 MODULE_DESCRIPTION("HDMI HD codec");
1682 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1683 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");