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
;
377 dai_map
= &hdmi
->dai_map
[dai
->id
];
379 dd
= (struct hdac_ext_dma_params
*)snd_soc_dai_get_dma_data(dai
, substream
);
380 dev_dbg(&hdac
->hdac
.dev
, "stream tag from cpu dai %d format in cvt 0x%x\n",
381 dd
->stream_tag
, dd
->format
);
383 ret
= hdac_hdmi_setup_audio_infoframe(hdac
, dai_map
->cvt
->nid
,
388 return hdac_hdmi_setup_stream(hdac
, dai_map
->cvt
->nid
,
389 dai_map
->pin
->nid
, dd
->stream_tag
, dd
->format
);
392 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream
*substream
,
393 struct snd_pcm_hw_params
*hparams
, struct snd_soc_dai
*dai
)
395 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
396 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
397 struct hdac_hdmi_dai_pin_map
*dai_map
;
398 struct hdac_hdmi_pin
*pin
;
399 struct hdac_ext_dma_params
*dd
;
401 dai_map
= &hdmi
->dai_map
[dai
->id
];
407 if ((!pin
->eld
.monitor_present
) || (!pin
->eld
.eld_valid
)) {
408 dev_err(&hdac
->hdac
.dev
, "device is not configured for this pin: %d\n",
413 dd
= snd_soc_dai_get_dma_data(dai
, substream
);
415 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
420 dd
->format
= snd_hdac_calc_stream_format(params_rate(hparams
),
421 params_channels(hparams
), params_format(hparams
),
424 snd_soc_dai_set_dma_data(dai
, substream
, (void *)dd
);
429 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream
*substream
,
430 struct snd_soc_dai
*dai
)
432 struct hdac_ext_device
*edev
= snd_soc_dai_get_drvdata(dai
);
433 struct hdac_ext_dma_params
*dd
;
434 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
435 struct hdac_hdmi_dai_pin_map
*dai_map
;
437 dai_map
= &hdmi
->dai_map
[dai
->id
];
439 dd
= (struct hdac_ext_dma_params
*)snd_soc_dai_get_dma_data(dai
, substream
);
442 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
449 static void hdac_hdmi_enable_cvt(struct hdac_ext_device
*edev
,
450 struct hdac_hdmi_dai_pin_map
*dai_map
)
452 /* Enable transmission */
453 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
454 AC_VERB_SET_DIGI_CONVERT_1
, 1);
456 /* Category Code (CC) to zero */
457 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
458 AC_VERB_SET_DIGI_CONVERT_2
, 0);
461 static int hdac_hdmi_enable_pin(struct hdac_ext_device
*hdac
,
462 struct hdac_hdmi_dai_pin_map
*dai_map
)
465 struct hdac_hdmi_pin
*pin
= dai_map
->pin
;
467 for (mux_idx
= 0; mux_idx
< pin
->num_mux_nids
; mux_idx
++) {
468 if (pin
->mux_nids
[mux_idx
] == dai_map
->cvt
->nid
) {
469 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
470 AC_VERB_SET_CONNECT_SEL
, mux_idx
);
475 if (mux_idx
== pin
->num_mux_nids
)
478 /* Enable out path for this pin widget */
479 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
480 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
482 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D0
);
484 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
485 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_UNMUTE
);
490 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device
*hdac
,
491 struct hdac_hdmi_pin
*pin
)
493 if (!(get_wcaps(&hdac
->hdac
, pin
->nid
) & AC_WCAP_CONN_LIST
)) {
494 dev_warn(&hdac
->hdac
.dev
,
495 "HDMI: pin %d wcaps %#x does not support connection list\n",
496 pin
->nid
, get_wcaps(&hdac
->hdac
, pin
->nid
));
500 pin
->num_mux_nids
= snd_hdac_get_connections(&hdac
->hdac
, pin
->nid
,
501 pin
->mux_nids
, HDA_MAX_CONNECTIONS
);
502 if (pin
->num_mux_nids
== 0)
503 dev_warn(&hdac
->hdac
.dev
, "No connections found for pin: %d\n",
506 dev_dbg(&hdac
->hdac
.dev
, "num_mux_nids %d for pin: %d\n",
507 pin
->num_mux_nids
, pin
->nid
);
509 return pin
->num_mux_nids
;
513 * Query pcm list and return pin widget to which stream is routed.
515 * Also query connection list of the pin, to validate the cvt to pin map.
517 * Same stream rendering to multiple pins simultaneously can be done
518 * possibly, but not supported for now in driver. So return the first pin
521 static struct hdac_hdmi_pin
*hdac_hdmi_get_pin_from_cvt(
522 struct hdac_ext_device
*edev
,
523 struct hdac_hdmi_priv
*hdmi
,
524 struct hdac_hdmi_cvt
*cvt
)
526 struct hdac_hdmi_pcm
*pcm
;
527 struct hdac_hdmi_pin
*pin
= NULL
;
530 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
531 if (pcm
->cvt
== cvt
) {
538 ret
= hdac_hdmi_query_pin_connlist(edev
, pin
);
542 for (i
= 0; i
< pin
->num_mux_nids
; i
++) {
543 if (pin
->mux_nids
[i
] == cvt
->nid
)
552 * This tries to get a valid pin and set the HW constraints based on the
553 * ELD. Even if a valid pin is not found return success so that device open
556 static int hdac_hdmi_pcm_open(struct snd_pcm_substream
*substream
,
557 struct snd_soc_dai
*dai
)
559 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
560 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
561 struct hdac_hdmi_dai_pin_map
*dai_map
;
562 struct hdac_hdmi_cvt
*cvt
;
563 struct hdac_hdmi_pin
*pin
;
566 dai_map
= &hdmi
->dai_map
[dai
->id
];
569 pin
= hdac_hdmi_get_pin_from_cvt(hdac
, hdmi
, cvt
);
572 * To make PA and other userland happy.
573 * userland scans devices so returning error does not help.
578 if ((!pin
->eld
.monitor_present
) ||
579 (!pin
->eld
.eld_valid
)) {
581 dev_warn(&hdac
->hdac
.dev
,
582 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
583 pin
->eld
.monitor_present
, pin
->eld
.eld_valid
, pin
->nid
);
590 hdac_hdmi_enable_cvt(hdac
, dai_map
);
591 ret
= hdac_hdmi_enable_pin(hdac
, dai_map
);
595 ret
= hdac_hdmi_eld_limit_formats(substream
->runtime
,
596 pin
->eld
.eld_buffer
);
600 return snd_pcm_hw_constraint_eld(substream
->runtime
,
601 pin
->eld
.eld_buffer
);
604 static int hdac_hdmi_trigger(struct snd_pcm_substream
*substream
, int cmd
,
605 struct snd_soc_dai
*dai
)
607 struct hdac_hdmi_dai_pin_map
*dai_map
;
608 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
609 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
612 dai_map
= &hdmi
->dai_map
[dai
->id
];
613 if (cmd
== SNDRV_PCM_TRIGGER_RESUME
) {
614 ret
= hdac_hdmi_enable_pin(hdac
, dai_map
);
618 return hdac_hdmi_playback_prepare(substream
, dai
);
624 static void hdac_hdmi_pcm_close(struct snd_pcm_substream
*substream
,
625 struct snd_soc_dai
*dai
)
627 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
628 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
629 struct hdac_hdmi_dai_pin_map
*dai_map
;
631 dai_map
= &hdmi
->dai_map
[dai
->id
];
634 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
635 AC_VERB_SET_CHANNEL_STREAMID
, 0);
636 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
637 AC_VERB_SET_STREAM_FORMAT
, 0);
639 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D3
);
641 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->pin
->nid
, 0,
642 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
649 hdac_hdmi_query_cvt_params(struct hdac_device
*hdac
, struct hdac_hdmi_cvt
*cvt
)
653 /* Only stereo supported as of now */
654 cvt
->params
.channels_min
= cvt
->params
.channels_max
= 2;
656 err
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
658 &cvt
->params
.formats
,
659 &cvt
->params
.maxbps
);
662 "Failed to query pcm params for nid %d: %d\n",
668 static int hdac_hdmi_fill_widget_info(struct device
*dev
,
669 struct snd_soc_dapm_widget
*w
,
670 enum snd_soc_dapm_type id
, void *priv
,
671 const char *wname
, const char *stream
,
672 struct snd_kcontrol_new
*wc
, int numkc
)
675 w
->name
= devm_kstrdup(dev
, wname
, GFP_KERNEL
);
680 w
->reg
= SND_SOC_NOPM
;
682 w
->kcontrol_news
= wc
;
683 w
->num_kcontrols
= numkc
;
689 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route
*route
,
690 const char *sink
, const char *control
, const char *src
,
691 int (*handler
)(struct snd_soc_dapm_widget
*src
,
692 struct snd_soc_dapm_widget
*sink
))
696 route
->control
= control
;
697 route
->connected
= handler
;
700 static struct hdac_hdmi_pcm
*hdac_hdmi_get_pcm(struct hdac_ext_device
*edev
,
701 struct hdac_hdmi_pin
*pin
)
703 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
704 struct hdac_hdmi_pcm
*pcm
= NULL
;
706 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
715 * Based on user selection, map the PINs with the PCMs.
717 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol
*kcontrol
,
718 struct snd_ctl_elem_value
*ucontrol
)
721 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
722 struct snd_soc_dapm_widget
*w
= snd_soc_dapm_kcontrol_widget(kcontrol
);
723 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
724 struct hdac_hdmi_pin
*pin
= w
->priv
;
725 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
726 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
727 struct hdac_hdmi_pcm
*pcm
= NULL
;
728 const char *cvt_name
= e
->texts
[ucontrol
->value
.enumerated
.item
[0]];
730 ret
= snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
734 mutex_lock(&hdmi
->pin_mutex
);
735 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
740 * Jack status is not reported during device probe as the
741 * PCMs are not registered by then. So report it here.
743 if (!strcmp(cvt_name
, pcm
->cvt
->name
) && !pcm
->pin
) {
745 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
746 dev_dbg(&edev
->hdac
.dev
,
747 "jack report for pcm=%d\n",
750 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
752 mutex_unlock(&hdmi
->pin_mutex
);
756 mutex_unlock(&hdmi
->pin_mutex
);
762 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
763 * the display driver seem to be programming the connection list for the pin
766 * So programming all the possible inputs for the mux, the user has to take
767 * care of selecting the right one and leaving all other inputs selected to
770 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device
*edev
,
771 struct hdac_hdmi_pin
*pin
,
772 struct snd_soc_dapm_widget
*widget
,
773 const char *widget_name
)
775 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
776 struct snd_kcontrol_new
*kc
;
777 struct hdac_hdmi_cvt
*cvt
;
779 char kc_name
[NAME_SIZE
];
780 char mux_items
[NAME_SIZE
];
781 /* To hold inputs to the Pin mux */
782 char *items
[HDA_MAX_CONNECTIONS
];
784 int num_items
= hdmi
->num_cvt
+ 1;
786 kc
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*kc
), GFP_KERNEL
);
790 se
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*se
), GFP_KERNEL
);
794 sprintf(kc_name
, "Pin %d Input", pin
->nid
);
795 kc
->name
= devm_kstrdup(&edev
->hdac
.dev
, kc_name
, GFP_KERNEL
);
799 kc
->private_value
= (long)se
;
800 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
802 kc
->info
= snd_soc_info_enum_double
;
803 kc
->put
= hdac_hdmi_set_pin_mux
;
804 kc
->get
= snd_soc_dapm_get_enum_double
;
806 se
->reg
= SND_SOC_NOPM
;
808 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
809 se
->items
= num_items
;
810 se
->mask
= roundup_pow_of_two(se
->items
) - 1;
812 sprintf(mux_items
, "NONE");
813 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
817 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
819 sprintf(mux_items
, "cvt %d", cvt
->nid
);
820 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
825 se
->texts
= devm_kmemdup(&edev
->hdac
.dev
, items
,
826 (num_items
* sizeof(char *)), GFP_KERNEL
);
830 return hdac_hdmi_fill_widget_info(&edev
->hdac
.dev
, widget
,
831 snd_soc_dapm_mux
, pin
, widget_name
, NULL
, kc
, 1);
834 /* Add cvt <- input <- mux route map */
835 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device
*edev
,
836 struct snd_soc_dapm_widget
*widgets
,
837 struct snd_soc_dapm_route
*route
, int rindex
)
839 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
840 const struct snd_kcontrol_new
*kc
;
842 int mux_index
= hdmi
->num_cvt
+ hdmi
->num_pin
;
845 for (i
= 0; i
< hdmi
->num_pin
; i
++) {
846 kc
= widgets
[mux_index
].kcontrol_news
;
847 se
= (struct soc_enum
*)kc
->private_value
;
848 for (j
= 0; j
< hdmi
->num_cvt
; j
++) {
849 hdac_hdmi_fill_route(&route
[rindex
],
850 widgets
[mux_index
].name
,
852 widgets
[j
].name
, NULL
);
862 * Widgets are added in the below sequence
863 * Converter widgets for num converters enumerated
864 * Pin widgets for num pins enumerated
865 * Pin mux widgets to represent connenction list of pin widget
867 * Total widgets elements = num_cvt + num_pin + num_pin;
869 * Routes are added as below:
870 * pin mux -> pin (based on num_pins)
871 * cvt -> "Input sel control" -> pin_mux
873 * Total route elements:
874 * num_pins + (pin_muxes * num_cvt)
876 static int create_fill_widget_route_map(struct snd_soc_dapm_context
*dapm
)
878 struct snd_soc_dapm_widget
*widgets
;
879 struct snd_soc_dapm_route
*route
;
880 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
881 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
882 struct snd_soc_dai_driver
*dai_drv
= dapm
->component
->dai_drv
;
883 char widget_name
[NAME_SIZE
];
884 struct hdac_hdmi_cvt
*cvt
;
885 struct hdac_hdmi_pin
*pin
;
886 int ret
, i
= 0, num_routes
= 0;
888 if (list_empty(&hdmi
->cvt_list
) || list_empty(&hdmi
->pin_list
))
891 widgets
= devm_kzalloc(dapm
->dev
,
892 (sizeof(*widgets
) * ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
)),
898 /* DAPM widgets to represent each converter widget */
899 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
900 sprintf(widget_name
, "Converter %d", cvt
->nid
);
901 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
902 snd_soc_dapm_aif_in
, &cvt
->nid
,
903 widget_name
, dai_drv
[i
].playback
.stream_name
, NULL
, 0);
909 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
910 sprintf(widget_name
, "hif%d Output", pin
->nid
);
911 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
912 snd_soc_dapm_output
, &pin
->nid
,
913 widget_name
, NULL
, NULL
, 0);
919 /* DAPM widgets to represent the connection list to pin widget */
920 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
921 sprintf(widget_name
, "Pin %d Mux", pin
->nid
);
922 ret
= hdac_hdmi_create_pin_muxs(edev
, pin
, &widgets
[i
],
928 /* For cvt to pin_mux mapping */
929 num_routes
+= hdmi
->num_cvt
;
931 /* For pin_mux to pin mapping */
935 route
= devm_kzalloc(dapm
->dev
, (sizeof(*route
) * num_routes
),
941 /* Add pin <- NULL <- mux route map */
942 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
943 int sink_index
= i
+ hdmi
->num_cvt
;
944 int src_index
= sink_index
+ hdmi
->num_pin
;
946 hdac_hdmi_fill_route(&route
[i
],
947 widgets
[sink_index
].name
, NULL
,
948 widgets
[src_index
].name
, NULL
);
953 hdac_hdmi_add_pinmux_cvt_route(edev
, widgets
, route
, i
);
955 snd_soc_dapm_new_controls(dapm
, widgets
,
956 ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
));
958 snd_soc_dapm_add_routes(dapm
, route
, num_routes
);
959 snd_soc_dapm_new_widgets(dapm
->card
);
965 static int hdac_hdmi_init_dai_map(struct hdac_ext_device
*edev
)
967 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
968 struct hdac_hdmi_dai_pin_map
*dai_map
;
969 struct hdac_hdmi_cvt
*cvt
;
972 if (list_empty(&hdmi
->cvt_list
))
975 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
976 dai_map
= &hdmi
->dai_map
[dai_id
];
977 dai_map
->dai_id
= dai_id
;
982 if (dai_id
== HDA_MAX_CVTS
) {
983 dev_warn(&edev
->hdac
.dev
,
984 "Max dais supported: %d\n", dai_id
);
992 static int hdac_hdmi_add_cvt(struct hdac_ext_device
*edev
, hda_nid_t nid
)
994 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
995 struct hdac_hdmi_cvt
*cvt
;
996 char name
[NAME_SIZE
];
998 cvt
= kzalloc(sizeof(*cvt
), GFP_KERNEL
);
1003 sprintf(name
, "cvt %d", cvt
->nid
);
1004 cvt
->name
= kstrdup(name
, GFP_KERNEL
);
1006 list_add_tail(&cvt
->head
, &hdmi
->cvt_list
);
1009 return hdac_hdmi_query_cvt_params(&edev
->hdac
, cvt
);
1012 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin
*pin
, int repoll
)
1014 struct hdac_ext_device
*edev
= pin
->edev
;
1015 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1016 struct hdac_hdmi_pcm
*pcm
;
1019 pin
->repoll_count
= repoll
;
1021 pm_runtime_get_sync(&edev
->hdac
.dev
);
1022 val
= snd_hdac_codec_read(&edev
->hdac
, pin
->nid
, 0,
1023 AC_VERB_GET_PIN_SENSE
, 0);
1025 dev_dbg(&edev
->hdac
.dev
, "Pin sense val %x for pin: %d\n",
1029 mutex_lock(&hdmi
->pin_mutex
);
1030 pin
->eld
.monitor_present
= !!(val
& AC_PINSENSE_PRESENCE
);
1031 pin
->eld
.eld_valid
= !!(val
& AC_PINSENSE_ELDV
);
1033 pcm
= hdac_hdmi_get_pcm(edev
, pin
);
1035 if (!pin
->eld
.monitor_present
|| !pin
->eld
.eld_valid
) {
1037 dev_dbg(&edev
->hdac
.dev
, "%s: disconnect for pin %d\n",
1038 __func__
, pin
->nid
);
1041 * PCMs are not registered during device probe, so don't
1042 * report jack here. It will be done in usermode mux
1046 dev_dbg(&edev
->hdac
.dev
,
1047 "jack report for pcm=%d\n", pcm
->pcm_id
);
1049 snd_jack_report(pcm
->jack
, 0);
1052 mutex_unlock(&hdmi
->pin_mutex
);
1053 goto put_hdac_device
;
1056 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
1057 /* TODO: use i915 component for reading ELD later */
1058 if (hdac_hdmi_get_eld(&edev
->hdac
, pin
->nid
,
1059 pin
->eld
.eld_buffer
,
1060 &pin
->eld
.eld_size
) == 0) {
1063 dev_dbg(&edev
->hdac
.dev
,
1064 "jack report for pcm=%d\n",
1067 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
1070 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET
,
1071 pin
->eld
.eld_buffer
, pin
->eld
.eld_size
);
1073 pin
->eld
.monitor_present
= false;
1074 pin
->eld
.eld_valid
= false;
1077 dev_dbg(&edev
->hdac
.dev
,
1078 "jack report for pcm=%d\n",
1081 snd_jack_report(pcm
->jack
, 0);
1086 mutex_unlock(&hdmi
->pin_mutex
);
1089 * Sometimes the pin_sense may present invalid monitor
1090 * present and eld_valid. If ELD data is not valid, loop few
1091 * more times to get correct pin sense and valid ELD.
1093 if ((!pin
->eld
.monitor_present
|| !pin
->eld
.eld_valid
) && repoll
)
1094 schedule_delayed_work(&pin
->work
, msecs_to_jiffies(300));
1097 pm_runtime_put_sync(&edev
->hdac
.dev
);
1100 static void hdac_hdmi_repoll_eld(struct work_struct
*work
)
1102 struct hdac_hdmi_pin
*pin
=
1103 container_of(to_delayed_work(work
), struct hdac_hdmi_pin
, work
);
1105 /* picked from legacy HDA driver */
1106 if (pin
->repoll_count
++ > 6)
1107 pin
->repoll_count
= 0;
1109 hdac_hdmi_present_sense(pin
, pin
->repoll_count
);
1112 static int hdac_hdmi_add_pin(struct hdac_ext_device
*edev
, hda_nid_t nid
)
1114 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1115 struct hdac_hdmi_pin
*pin
;
1117 pin
= kzalloc(sizeof(*pin
), GFP_KERNEL
);
1123 list_add_tail(&pin
->head
, &hdmi
->pin_list
);
1127 INIT_DELAYED_WORK(&pin
->work
, hdac_hdmi_repoll_eld
);
1132 #define INTEL_VENDOR_NID 0x08
1133 #define INTEL_GET_VENDOR_VERB 0xf81
1134 #define INTEL_SET_VENDOR_VERB 0x781
1135 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1136 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1138 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device
*hdac
)
1140 unsigned int vendor_param
;
1142 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1143 INTEL_GET_VENDOR_VERB
, 0);
1144 if (vendor_param
== -1 || vendor_param
& INTEL_EN_ALL_PIN_CVTS
)
1147 vendor_param
|= INTEL_EN_ALL_PIN_CVTS
;
1148 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1149 INTEL_SET_VENDOR_VERB
, vendor_param
);
1150 if (vendor_param
== -1)
1154 static void hdac_hdmi_skl_enable_dp12(struct hdac_device
*hdac
)
1156 unsigned int vendor_param
;
1158 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1159 INTEL_GET_VENDOR_VERB
, 0);
1160 if (vendor_param
== -1 || vendor_param
& INTEL_EN_DP12
)
1163 /* enable DP1.2 mode */
1164 vendor_param
|= INTEL_EN_DP12
;
1165 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1166 INTEL_SET_VENDOR_VERB
, vendor_param
);
1167 if (vendor_param
== -1)
1172 static struct snd_soc_dai_ops hdmi_dai_ops
= {
1173 .startup
= hdac_hdmi_pcm_open
,
1174 .shutdown
= hdac_hdmi_pcm_close
,
1175 .hw_params
= hdac_hdmi_set_hw_params
,
1176 .prepare
= hdac_hdmi_playback_prepare
,
1177 .trigger
= hdac_hdmi_trigger
,
1178 .hw_free
= hdac_hdmi_playback_cleanup
,
1182 * Each converter can support a stream independently. So a dai is created
1183 * based on the number of converter queried.
1185 static int hdac_hdmi_create_dais(struct hdac_device
*hdac
,
1186 struct snd_soc_dai_driver
**dais
,
1187 struct hdac_hdmi_priv
*hdmi
, int num_dais
)
1189 struct snd_soc_dai_driver
*hdmi_dais
;
1190 struct hdac_hdmi_cvt
*cvt
;
1191 char name
[NAME_SIZE
], dai_name
[NAME_SIZE
];
1194 unsigned int rate_max
= 384000, rate_min
= 8000;
1198 hdmi_dais
= devm_kzalloc(&hdac
->dev
,
1199 (sizeof(*hdmi_dais
) * num_dais
),
1204 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
1205 ret
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
1206 &rates
, &formats
, &bps
);
1210 sprintf(dai_name
, "intel-hdmi-hifi%d", i
+1);
1211 hdmi_dais
[i
].name
= devm_kstrdup(&hdac
->dev
,
1212 dai_name
, GFP_KERNEL
);
1214 if (!hdmi_dais
[i
].name
)
1217 snprintf(name
, sizeof(name
), "hifi%d", i
+1);
1218 hdmi_dais
[i
].playback
.stream_name
=
1219 devm_kstrdup(&hdac
->dev
, name
, GFP_KERNEL
);
1220 if (!hdmi_dais
[i
].playback
.stream_name
)
1224 * Set caps based on capability queried from the converter.
1225 * It will be constrained runtime based on ELD queried.
1227 hdmi_dais
[i
].playback
.formats
= formats
;
1228 hdmi_dais
[i
].playback
.rates
= rates
;
1229 hdmi_dais
[i
].playback
.rate_max
= rate_max
;
1230 hdmi_dais
[i
].playback
.rate_min
= rate_min
;
1231 hdmi_dais
[i
].playback
.channels_min
= 2;
1232 hdmi_dais
[i
].playback
.channels_max
= 2;
1233 hdmi_dais
[i
].ops
= &hdmi_dai_ops
;
1244 * Parse all nodes and store the cvt/pin nids in array
1245 * Add one time initialization for pin and cvt widgets
1247 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device
*edev
,
1248 struct snd_soc_dai_driver
**dais
, int *num_dais
)
1252 struct hdac_device
*hdac
= &edev
->hdac
;
1253 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1256 hdac_hdmi_skl_enable_all_pins(hdac
);
1257 hdac_hdmi_skl_enable_dp12(hdac
);
1259 num_nodes
= snd_hdac_get_sub_nodes(hdac
, hdac
->afg
, &nid
);
1260 if (!nid
|| num_nodes
<= 0) {
1261 dev_warn(&hdac
->dev
, "HDMI: failed to get afg sub nodes\n");
1265 hdac
->num_nodes
= num_nodes
;
1266 hdac
->start_nid
= nid
;
1268 for (i
= 0; i
< hdac
->num_nodes
; i
++, nid
++) {
1272 caps
= get_wcaps(hdac
, nid
);
1273 type
= get_wcaps_type(caps
);
1275 if (!(caps
& AC_WCAP_DIGITAL
))
1280 case AC_WID_AUD_OUT
:
1281 ret
= hdac_hdmi_add_cvt(edev
, nid
);
1287 ret
= hdac_hdmi_add_pin(edev
, nid
);
1294 hdac
->end_nid
= nid
;
1296 if (!hdmi
->num_pin
|| !hdmi
->num_cvt
)
1299 ret
= hdac_hdmi_create_dais(hdac
, dais
, hdmi
, hdmi
->num_cvt
);
1301 dev_err(&hdac
->dev
, "Failed to create dais with err: %d\n",
1306 *num_dais
= hdmi
->num_cvt
;
1308 return hdac_hdmi_init_dai_map(edev
);
1311 static void hdac_hdmi_eld_notify_cb(void *aptr
, int port
)
1313 struct hdac_ext_device
*edev
= aptr
;
1314 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1315 struct hdac_hdmi_pin
*pin
;
1316 struct snd_soc_codec
*codec
= edev
->scodec
;
1318 /* Don't know how this mapping is derived */
1319 hda_nid_t pin_nid
= port
+ 0x04;
1321 dev_dbg(&edev
->hdac
.dev
, "%s: for pin: %d\n", __func__
, pin_nid
);
1324 * skip notification during system suspend (but not in runtime PM);
1325 * the state will be updated at resume. Also since the ELD and
1326 * connection states are updated in anyway at the end of the resume,
1327 * we can skip it when received during PM process.
1329 if (snd_power_get_state(codec
->component
.card
->snd_card
) !=
1333 if (atomic_read(&edev
->hdac
.in_pm
))
1336 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
1337 if (pin
->nid
== pin_nid
)
1338 hdac_hdmi_present_sense(pin
, 1);
1342 static struct i915_audio_component_audio_ops aops
= {
1343 .pin_eld_notify
= hdac_hdmi_eld_notify_cb
,
1346 int hdac_hdmi_jack_init(struct snd_soc_dai
*dai
, int device
)
1348 char jack_name
[NAME_SIZE
];
1349 struct snd_soc_codec
*codec
= dai
->codec
;
1350 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1351 struct snd_soc_dapm_context
*dapm
=
1352 snd_soc_component_get_dapm(&codec
->component
);
1353 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1354 struct hdac_hdmi_pcm
*pcm
;
1357 * this is a new PCM device, create new pcm and
1358 * add to the pcm list
1360 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
1363 pcm
->pcm_id
= device
;
1364 pcm
->cvt
= hdmi
->dai_map
[dai
->id
].cvt
;
1366 list_add_tail(&pcm
->head
, &hdmi
->pcm_list
);
1368 sprintf(jack_name
, "HDMI/DP, pcm=%d Jack", device
);
1370 return snd_jack_new(dapm
->card
->snd_card
, jack_name
,
1371 SND_JACK_AVOUT
, &pcm
->jack
, true, false);
1373 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init
);
1375 static int hdmi_codec_probe(struct snd_soc_codec
*codec
)
1377 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1378 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1379 struct snd_soc_dapm_context
*dapm
=
1380 snd_soc_component_get_dapm(&codec
->component
);
1381 struct hdac_hdmi_pin
*pin
;
1384 edev
->scodec
= codec
;
1386 ret
= create_fill_widget_route_map(dapm
);
1390 aops
.audio_ptr
= edev
;
1391 ret
= snd_hdac_i915_register_notifier(&aops
);
1393 dev_err(&edev
->hdac
.dev
, "notifier register failed: err: %d\n",
1398 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1399 hdac_hdmi_present_sense(pin
, 1);
1401 /* Imp: Store the card pointer in hda_codec */
1402 edev
->card
= dapm
->card
->snd_card
;
1405 * hdac_device core already sets the state to active and calls
1406 * get_noresume. So enable runtime and set the device to suspend.
1408 pm_runtime_enable(&edev
->hdac
.dev
);
1409 pm_runtime_put(&edev
->hdac
.dev
);
1410 pm_runtime_suspend(&edev
->hdac
.dev
);
1415 static int hdmi_codec_remove(struct snd_soc_codec
*codec
)
1417 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1419 pm_runtime_disable(&edev
->hdac
.dev
);
1424 static int hdmi_codec_resume(struct snd_soc_codec
*codec
)
1426 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1427 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1428 struct hdac_hdmi_pin
*pin
;
1429 struct hdac_device
*hdac
= &edev
->hdac
;
1430 struct hdac_bus
*bus
= hdac
->bus
;
1432 unsigned long timeout
;
1434 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1435 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1438 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
)) {
1440 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1441 AC_VERB_SET_POWER_STATE
, AC_PWRST_D0
);
1443 /* Wait till power state is set to D0 */
1444 timeout
= jiffies
+ msecs_to_jiffies(1000);
1445 while (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
)
1446 && time_before(jiffies
, timeout
)) {
1452 * As the ELD notify callback request is not entertained while the
1453 * device is in suspend state. Need to manually check detection of
1456 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1457 hdac_hdmi_present_sense(pin
, 1);
1460 * Codec power is turned ON during controller resume.
1463 err
= snd_hdac_display_power(bus
, false);
1466 "Cannot turn OFF display power on i915, err: %d\n",
1474 #define hdmi_codec_resume NULL
1477 static struct snd_soc_codec_driver hdmi_hda_codec
= {
1478 .probe
= hdmi_codec_probe
,
1479 .remove
= hdmi_codec_remove
,
1480 .resume
= hdmi_codec_resume
,
1481 .idle_bias_off
= true,
1484 static int hdac_hdmi_dev_probe(struct hdac_ext_device
*edev
)
1486 struct hdac_device
*codec
= &edev
->hdac
;
1487 struct hdac_hdmi_priv
*hdmi_priv
;
1488 struct snd_soc_dai_driver
*hdmi_dais
= NULL
;
1492 hdmi_priv
= devm_kzalloc(&codec
->dev
, sizeof(*hdmi_priv
), GFP_KERNEL
);
1493 if (hdmi_priv
== NULL
)
1496 edev
->private_data
= hdmi_priv
;
1498 dev_set_drvdata(&codec
->dev
, edev
);
1500 INIT_LIST_HEAD(&hdmi_priv
->pin_list
);
1501 INIT_LIST_HEAD(&hdmi_priv
->cvt_list
);
1502 INIT_LIST_HEAD(&hdmi_priv
->pcm_list
);
1503 mutex_init(&hdmi_priv
->pin_mutex
);
1506 * Turned off in the runtime_suspend during the first explicit
1507 * pm_runtime_suspend call.
1509 ret
= snd_hdac_display_power(edev
->hdac
.bus
, true);
1511 dev_err(&edev
->hdac
.dev
,
1512 "Cannot turn on display power on i915 err: %d\n",
1517 ret
= hdac_hdmi_parse_and_map_nid(edev
, &hdmi_dais
, &num_dais
);
1519 dev_err(&codec
->dev
,
1520 "Failed in parse and map nid with err: %d\n", ret
);
1524 /* ASoC specific initialization */
1525 return snd_soc_register_codec(&codec
->dev
, &hdmi_hda_codec
,
1526 hdmi_dais
, num_dais
);
1529 static int hdac_hdmi_dev_remove(struct hdac_ext_device
*edev
)
1531 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1532 struct hdac_hdmi_pin
*pin
, *pin_next
;
1533 struct hdac_hdmi_cvt
*cvt
, *cvt_next
;
1534 struct hdac_hdmi_pcm
*pcm
, *pcm_next
;
1536 snd_soc_unregister_codec(&edev
->hdac
.dev
);
1538 list_for_each_entry_safe(pcm
, pcm_next
, &hdmi
->pcm_list
, head
) {
1541 list_del(&pcm
->head
);
1545 list_for_each_entry_safe(cvt
, cvt_next
, &hdmi
->cvt_list
, head
) {
1546 list_del(&cvt
->head
);
1551 list_for_each_entry_safe(pin
, pin_next
, &hdmi
->pin_list
, head
) {
1552 list_del(&pin
->head
);
1560 static int hdac_hdmi_runtime_suspend(struct device
*dev
)
1562 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1563 struct hdac_device
*hdac
= &edev
->hdac
;
1564 struct hdac_bus
*bus
= hdac
->bus
;
1565 unsigned long timeout
;
1568 dev_dbg(dev
, "Enter: %s\n", __func__
);
1570 /* controller may not have been initialized for the first time */
1574 /* Power down afg */
1575 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D3
)) {
1576 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1577 AC_VERB_SET_POWER_STATE
, AC_PWRST_D3
);
1579 /* Wait till power state is set to D3 */
1580 timeout
= jiffies
+ msecs_to_jiffies(1000);
1581 while (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D3
)
1582 && time_before(jiffies
, timeout
)) {
1588 err
= snd_hdac_display_power(bus
, false);
1590 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1597 static int hdac_hdmi_runtime_resume(struct device
*dev
)
1599 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1600 struct hdac_device
*hdac
= &edev
->hdac
;
1601 struct hdac_bus
*bus
= hdac
->bus
;
1604 dev_dbg(dev
, "Enter: %s\n", __func__
);
1606 /* controller may not have been initialized for the first time */
1610 err
= snd_hdac_display_power(bus
, true);
1612 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1616 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1617 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1620 if (!snd_hdac_check_power_state(hdac
, hdac
->afg
, AC_PWRST_D0
))
1621 snd_hdac_codec_write(hdac
, hdac
->afg
, 0,
1622 AC_VERB_SET_POWER_STATE
, AC_PWRST_D0
);
1627 #define hdac_hdmi_runtime_suspend NULL
1628 #define hdac_hdmi_runtime_resume NULL
1631 static const struct dev_pm_ops hdac_hdmi_pm
= {
1632 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend
, hdac_hdmi_runtime_resume
, NULL
)
1635 static const struct hda_device_id hdmi_list
[] = {
1636 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1637 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1641 MODULE_DEVICE_TABLE(hdaudio
, hdmi_list
);
1643 static struct hdac_ext_driver hdmi_driver
= {
1646 .name
= "HDMI HDA Codec",
1647 .pm
= &hdac_hdmi_pm
,
1649 .id_table
= hdmi_list
,
1651 .probe
= hdac_hdmi_dev_probe
,
1652 .remove
= hdac_hdmi_dev_remove
,
1655 static int __init
hdmi_init(void)
1657 return snd_hda_ext_driver_register(&hdmi_driver
);
1660 static void __exit
hdmi_exit(void)
1662 snd_hda_ext_driver_unregister(&hdmi_driver
);
1665 module_init(hdmi_init
);
1666 module_exit(hdmi_exit
);
1668 MODULE_LICENSE("GPL v2");
1669 MODULE_DESCRIPTION("HDMI HD codec");
1670 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1671 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");