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 #define ELD_VER_CEA_861D 2
50 #define ELD_VER_PARTIAL 31
51 #define ELD_MAX_MNL 16
53 struct hdac_hdmi_cvt_params
{
54 unsigned int channels_min
;
55 unsigned int channels_max
;
61 struct hdac_hdmi_cvt
{
62 struct list_head head
;
65 struct hdac_hdmi_cvt_params params
;
68 /* Currently only spk_alloc, more to be added */
69 struct hdac_hdmi_parsed_eld
{
73 struct hdac_hdmi_eld
{
77 char eld_buffer
[ELD_MAX_SIZE
];
78 struct hdac_hdmi_parsed_eld info
;
81 struct hdac_hdmi_pin
{
82 struct list_head head
;
85 hda_nid_t mux_nids
[HDA_MAX_CONNECTIONS
];
86 struct hdac_hdmi_eld eld
;
87 struct hdac_ext_device
*edev
;
90 unsigned char chmap
[8]; /* ALSA API channel-map */
91 int channels
; /* current number of channels */
94 struct hdac_hdmi_pcm
{
95 struct list_head head
;
97 struct hdac_hdmi_pin
*pin
;
98 struct hdac_hdmi_cvt
*cvt
;
99 struct snd_jack
*jack
;
102 struct hdac_hdmi_dai_pin_map
{
104 struct hdac_hdmi_pin
*pin
;
105 struct hdac_hdmi_cvt
*cvt
;
108 struct hdac_hdmi_priv
{
109 struct hdac_hdmi_dai_pin_map dai_map
[HDA_MAX_CVTS
];
110 struct list_head pin_list
;
111 struct list_head cvt_list
;
112 struct list_head pcm_list
;
115 struct mutex pin_mutex
;
116 struct hdac_chmap chmap
;
119 static void hdac_hdmi_enable_cvt(struct hdac_ext_device
*edev
,
120 struct hdac_hdmi_dai_pin_map
*dai_map
);
122 static int hdac_hdmi_enable_pin(struct hdac_ext_device
*hdac
,
123 struct hdac_hdmi_dai_pin_map
*dai_map
);
125 static struct hdac_hdmi_pcm
*get_hdmi_pcm_from_id(struct hdac_hdmi_priv
*hdmi
,
128 struct hdac_hdmi_pcm
*pcm
;
130 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
131 if (pcm
->pcm_id
== pcm_idx
)
138 static inline struct hdac_ext_device
*to_hda_ext_device(struct device
*dev
)
140 struct hdac_device
*hdac
= dev_to_hdac_dev(dev
);
142 return to_ehdac_device(hdac
);
145 static unsigned int sad_format(const u8
*sad
)
147 return ((sad
[0] >> 0x3) & 0x1f);
150 static unsigned int sad_sample_bits_lpcm(const u8
*sad
)
155 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime
*runtime
,
158 u64 formats
= SNDRV_PCM_FMTBIT_S16
;
160 const u8
*sad
, *eld_buf
= eld
;
162 sad
= drm_eld_sad(eld_buf
);
164 goto format_constraint
;
166 for (i
= drm_eld_sad_count(eld_buf
); i
> 0; i
--, sad
+= 3) {
167 if (sad_format(sad
) == 1) { /* AUDIO_CODING_TYPE_LPCM */
170 * the controller support 20 and 24 bits in 32 bit
171 * container so we set S32
173 if (sad_sample_bits_lpcm(sad
) & 0x6)
174 formats
|= SNDRV_PCM_FMTBIT_S32
;
179 return snd_pcm_hw_constraint_mask64(runtime
, SNDRV_PCM_HW_PARAM_FORMAT
,
184 static int hdac_hdmi_setup_stream(struct hdac_ext_device
*hdac
,
185 hda_nid_t cvt_nid
, hda_nid_t pin_nid
,
186 u32 stream_tag
, int format
)
190 dev_dbg(&hdac
->hdac
.dev
, "cvt nid %d pnid %d stream %d format 0x%x\n",
191 cvt_nid
, pin_nid
, stream_tag
, format
);
193 val
= (stream_tag
<< 4);
195 snd_hdac_codec_write(&hdac
->hdac
, cvt_nid
, 0,
196 AC_VERB_SET_CHANNEL_STREAMID
, val
);
197 snd_hdac_codec_write(&hdac
->hdac
, cvt_nid
, 0,
198 AC_VERB_SET_STREAM_FORMAT
, format
);
204 hdac_hdmi_set_dip_index(struct hdac_ext_device
*hdac
, hda_nid_t pin_nid
,
205 int packet_index
, int byte_index
)
209 val
= (packet_index
<< 5) | (byte_index
& 0x1f);
211 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
212 AC_VERB_SET_HDMI_DIP_INDEX
, val
);
215 struct dp_audio_infoframe
{
218 u8 ver
; /* 0x11 << 2 */
220 u8 CC02_CT47
; /* match with HDMI infoframe from this on */
224 u8 LFEPBL01_LSV36_DM_INH7
;
227 static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device
*hdac
,
228 hda_nid_t cvt_nid
, hda_nid_t pin_nid
)
230 uint8_t buffer
[HDMI_INFOFRAME_HEADER_SIZE
+ HDMI_AUDIO_INFOFRAME_SIZE
];
231 struct hdmi_audio_infoframe frame
;
232 struct dp_audio_infoframe dp_ai
;
233 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
234 struct hdac_hdmi_pin
*pin
;
242 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
243 if (pin
->nid
== pin_nid
)
247 ca
= snd_hdac_channel_allocation(&hdac
->hdac
, pin
->eld
.info
.spk_alloc
,
248 pin
->channels
, pin
->chmap_set
, true, pin
->chmap
);
250 channels
= snd_hdac_get_active_channels(ca
);
251 hdmi
->chmap
.ops
.set_channel_count(&hdac
->hdac
, cvt_nid
, channels
);
253 snd_hdac_setup_channel_mapping(&hdmi
->chmap
, pin
->nid
, false, ca
,
254 pin
->channels
, pin
->chmap
, pin
->chmap_set
);
256 eld_buf
= pin
->eld
.eld_buffer
;
257 conn_type
= drm_eld_get_conn_type(eld_buf
);
260 case DRM_ELD_CONN_TYPE_HDMI
:
261 hdmi_audio_infoframe_init(&frame
);
263 frame
.channels
= channels
;
264 frame
.channel_allocation
= ca
;
266 ret
= hdmi_audio_infoframe_pack(&frame
, buffer
, sizeof(buffer
));
272 case DRM_ELD_CONN_TYPE_DP
:
273 memset(&dp_ai
, 0, sizeof(dp_ai
));
276 dp_ai
.ver
= 0x11 << 2;
277 dp_ai
.CC02_CT47
= channels
- 1;
284 dev_err(&hdac
->hdac
.dev
, "Invalid connection type: %d\n",
289 /* stop infoframe transmission */
290 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
291 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
292 AC_VERB_SET_HDMI_DIP_XMIT
, AC_DIPXMIT_DISABLE
);
295 /* Fill infoframe. Index auto-incremented */
296 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
297 if (conn_type
== DRM_ELD_CONN_TYPE_HDMI
) {
298 for (i
= 0; i
< sizeof(buffer
); i
++)
299 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
300 AC_VERB_SET_HDMI_DIP_DATA
, buffer
[i
]);
302 for (i
= 0; i
< sizeof(dp_ai
); i
++)
303 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
304 AC_VERB_SET_HDMI_DIP_DATA
, dip
[i
]);
307 /* Start infoframe */
308 hdac_hdmi_set_dip_index(hdac
, pin_nid
, 0x0, 0x0);
309 snd_hdac_codec_write(&hdac
->hdac
, pin_nid
, 0,
310 AC_VERB_SET_HDMI_DIP_XMIT
, AC_DIPXMIT_BEST
);
315 static void hdac_hdmi_set_power_state(struct hdac_ext_device
*edev
,
316 struct hdac_hdmi_dai_pin_map
*dai_map
, unsigned int pwr_state
)
318 /* Power up pin widget */
319 if (!snd_hdac_check_power_state(&edev
->hdac
, dai_map
->pin
->nid
,
321 snd_hdac_codec_write(&edev
->hdac
, dai_map
->pin
->nid
, 0,
322 AC_VERB_SET_POWER_STATE
, pwr_state
);
324 /* Power up converter */
325 if (!snd_hdac_check_power_state(&edev
->hdac
, dai_map
->cvt
->nid
,
327 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
328 AC_VERB_SET_POWER_STATE
, pwr_state
);
331 static int hdac_hdmi_playback_prepare(struct snd_pcm_substream
*substream
,
332 struct snd_soc_dai
*dai
)
334 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
335 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
336 struct hdac_hdmi_dai_pin_map
*dai_map
;
337 struct hdac_hdmi_pin
*pin
;
338 struct hdac_ext_dma_params
*dd
;
341 dai_map
= &hdmi
->dai_map
[dai
->id
];
344 dd
= (struct hdac_ext_dma_params
*)snd_soc_dai_get_dma_data(dai
, substream
);
345 dev_dbg(&hdac
->hdac
.dev
, "stream tag from cpu dai %d format in cvt 0x%x\n",
346 dd
->stream_tag
, dd
->format
);
348 hdac_hdmi_enable_cvt(hdac
, dai_map
);
349 ret
= hdac_hdmi_enable_pin(hdac
, dai_map
);
352 mutex_lock(&pin
->lock
);
353 pin
->channels
= substream
->runtime
->channels
;
355 ret
= hdac_hdmi_setup_audio_infoframe(hdac
, dai_map
->cvt
->nid
,
357 mutex_unlock(&pin
->lock
);
361 return hdac_hdmi_setup_stream(hdac
, dai_map
->cvt
->nid
,
362 dai_map
->pin
->nid
, dd
->stream_tag
, dd
->format
);
365 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream
*substream
,
366 struct snd_pcm_hw_params
*hparams
, struct snd_soc_dai
*dai
)
368 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
369 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
370 struct hdac_hdmi_dai_pin_map
*dai_map
;
371 struct hdac_hdmi_pin
*pin
;
372 struct hdac_ext_dma_params
*dd
;
374 dai_map
= &hdmi
->dai_map
[dai
->id
];
380 if ((!pin
->eld
.monitor_present
) || (!pin
->eld
.eld_valid
)) {
381 dev_err(&hdac
->hdac
.dev
, "device is not configured for this pin: %d\n",
386 dd
= snd_soc_dai_get_dma_data(dai
, substream
);
388 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
393 dd
->format
= snd_hdac_calc_stream_format(params_rate(hparams
),
394 params_channels(hparams
), params_format(hparams
),
397 snd_soc_dai_set_dma_data(dai
, substream
, (void *)dd
);
402 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream
*substream
,
403 struct snd_soc_dai
*dai
)
405 struct hdac_ext_dma_params
*dd
;
407 dd
= (struct hdac_ext_dma_params
*)snd_soc_dai_get_dma_data(dai
, substream
);
410 snd_soc_dai_set_dma_data(dai
, substream
, NULL
);
417 static void hdac_hdmi_enable_cvt(struct hdac_ext_device
*edev
,
418 struct hdac_hdmi_dai_pin_map
*dai_map
)
420 /* Enable transmission */
421 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
422 AC_VERB_SET_DIGI_CONVERT_1
, 1);
424 /* Category Code (CC) to zero */
425 snd_hdac_codec_write(&edev
->hdac
, dai_map
->cvt
->nid
, 0,
426 AC_VERB_SET_DIGI_CONVERT_2
, 0);
429 static int hdac_hdmi_enable_pin(struct hdac_ext_device
*hdac
,
430 struct hdac_hdmi_dai_pin_map
*dai_map
)
433 struct hdac_hdmi_pin
*pin
= dai_map
->pin
;
435 for (mux_idx
= 0; mux_idx
< pin
->num_mux_nids
; mux_idx
++) {
436 if (pin
->mux_nids
[mux_idx
] == dai_map
->cvt
->nid
) {
437 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
438 AC_VERB_SET_CONNECT_SEL
, mux_idx
);
443 if (mux_idx
== pin
->num_mux_nids
)
446 /* Enable out path for this pin widget */
447 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
448 AC_VERB_SET_PIN_WIDGET_CONTROL
, PIN_OUT
);
450 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D0
);
452 snd_hdac_codec_write(&hdac
->hdac
, pin
->nid
, 0,
453 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_UNMUTE
);
458 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device
*hdac
,
459 struct hdac_hdmi_pin
*pin
)
461 if (!(get_wcaps(&hdac
->hdac
, pin
->nid
) & AC_WCAP_CONN_LIST
)) {
462 dev_warn(&hdac
->hdac
.dev
,
463 "HDMI: pin %d wcaps %#x does not support connection list\n",
464 pin
->nid
, get_wcaps(&hdac
->hdac
, pin
->nid
));
468 pin
->num_mux_nids
= snd_hdac_get_connections(&hdac
->hdac
, pin
->nid
,
469 pin
->mux_nids
, HDA_MAX_CONNECTIONS
);
470 if (pin
->num_mux_nids
== 0)
471 dev_warn(&hdac
->hdac
.dev
, "No connections found for pin: %d\n",
474 dev_dbg(&hdac
->hdac
.dev
, "num_mux_nids %d for pin: %d\n",
475 pin
->num_mux_nids
, pin
->nid
);
477 return pin
->num_mux_nids
;
481 * Query pcm list and return pin widget to which stream is routed.
483 * Also query connection list of the pin, to validate the cvt to pin map.
485 * Same stream rendering to multiple pins simultaneously can be done
486 * possibly, but not supported for now in driver. So return the first pin
489 static struct hdac_hdmi_pin
*hdac_hdmi_get_pin_from_cvt(
490 struct hdac_ext_device
*edev
,
491 struct hdac_hdmi_priv
*hdmi
,
492 struct hdac_hdmi_cvt
*cvt
)
494 struct hdac_hdmi_pcm
*pcm
;
495 struct hdac_hdmi_pin
*pin
= NULL
;
498 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
499 if (pcm
->cvt
== cvt
) {
506 ret
= hdac_hdmi_query_pin_connlist(edev
, pin
);
510 for (i
= 0; i
< pin
->num_mux_nids
; i
++) {
511 if (pin
->mux_nids
[i
] == cvt
->nid
)
520 * This tries to get a valid pin and set the HW constraints based on the
521 * ELD. Even if a valid pin is not found return success so that device open
524 static int hdac_hdmi_pcm_open(struct snd_pcm_substream
*substream
,
525 struct snd_soc_dai
*dai
)
527 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
528 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
529 struct hdac_hdmi_dai_pin_map
*dai_map
;
530 struct hdac_hdmi_cvt
*cvt
;
531 struct hdac_hdmi_pin
*pin
;
534 dai_map
= &hdmi
->dai_map
[dai
->id
];
537 pin
= hdac_hdmi_get_pin_from_cvt(hdac
, hdmi
, cvt
);
540 * To make PA and other userland happy.
541 * userland scans devices so returning error does not help.
546 if ((!pin
->eld
.monitor_present
) ||
547 (!pin
->eld
.eld_valid
)) {
549 dev_warn(&hdac
->hdac
.dev
,
550 "Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
551 pin
->eld
.monitor_present
, pin
->eld
.eld_valid
, pin
->nid
);
558 ret
= hdac_hdmi_eld_limit_formats(substream
->runtime
,
559 pin
->eld
.eld_buffer
);
563 return snd_pcm_hw_constraint_eld(substream
->runtime
,
564 pin
->eld
.eld_buffer
);
567 static int hdac_hdmi_trigger(struct snd_pcm_substream
*substream
, int cmd
,
568 struct snd_soc_dai
*dai
)
572 case SNDRV_PCM_TRIGGER_RESUME
:
573 case SNDRV_PCM_TRIGGER_START
:
574 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
575 return hdac_hdmi_playback_prepare(substream
, dai
);
584 static void hdac_hdmi_pcm_close(struct snd_pcm_substream
*substream
,
585 struct snd_soc_dai
*dai
)
587 struct hdac_ext_device
*hdac
= snd_soc_dai_get_drvdata(dai
);
588 struct hdac_hdmi_priv
*hdmi
= hdac
->private_data
;
589 struct hdac_hdmi_dai_pin_map
*dai_map
;
591 dai_map
= &hdmi
->dai_map
[dai
->id
];
594 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
595 AC_VERB_SET_CHANNEL_STREAMID
, 0);
596 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->cvt
->nid
, 0,
597 AC_VERB_SET_STREAM_FORMAT
, 0);
599 hdac_hdmi_set_power_state(hdac
, dai_map
, AC_PWRST_D3
);
601 snd_hdac_codec_write(&hdac
->hdac
, dai_map
->pin
->nid
, 0,
602 AC_VERB_SET_AMP_GAIN_MUTE
, AMP_OUT_MUTE
);
604 mutex_lock(&dai_map
->pin
->lock
);
605 dai_map
->pin
->chmap_set
= false;
606 memset(dai_map
->pin
->chmap
, 0, sizeof(dai_map
->pin
->chmap
));
607 dai_map
->pin
->channels
= 0;
608 mutex_unlock(&dai_map
->pin
->lock
);
615 hdac_hdmi_query_cvt_params(struct hdac_device
*hdac
, struct hdac_hdmi_cvt
*cvt
)
618 struct hdac_ext_device
*edev
= to_ehdac_device(hdac
);
619 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
622 chans
= get_wcaps(hdac
, cvt
->nid
);
623 chans
= get_wcaps_channels(chans
);
625 cvt
->params
.channels_min
= 2;
627 cvt
->params
.channels_max
= chans
;
628 if (chans
> hdmi
->chmap
.channels_max
)
629 hdmi
->chmap
.channels_max
= chans
;
631 err
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
633 &cvt
->params
.formats
,
634 &cvt
->params
.maxbps
);
637 "Failed to query pcm params for nid %d: %d\n",
643 static int hdac_hdmi_fill_widget_info(struct device
*dev
,
644 struct snd_soc_dapm_widget
*w
,
645 enum snd_soc_dapm_type id
, void *priv
,
646 const char *wname
, const char *stream
,
647 struct snd_kcontrol_new
*wc
, int numkc
)
650 w
->name
= devm_kstrdup(dev
, wname
, GFP_KERNEL
);
655 w
->reg
= SND_SOC_NOPM
;
657 w
->kcontrol_news
= wc
;
658 w
->num_kcontrols
= numkc
;
664 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route
*route
,
665 const char *sink
, const char *control
, const char *src
,
666 int (*handler
)(struct snd_soc_dapm_widget
*src
,
667 struct snd_soc_dapm_widget
*sink
))
671 route
->control
= control
;
672 route
->connected
= handler
;
675 static struct hdac_hdmi_pcm
*hdac_hdmi_get_pcm(struct hdac_ext_device
*edev
,
676 struct hdac_hdmi_pin
*pin
)
678 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
679 struct hdac_hdmi_pcm
*pcm
= NULL
;
681 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
690 * Based on user selection, map the PINs with the PCMs.
692 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol
*kcontrol
,
693 struct snd_ctl_elem_value
*ucontrol
)
696 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
697 struct snd_soc_dapm_widget
*w
= snd_soc_dapm_kcontrol_widget(kcontrol
);
698 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
699 struct hdac_hdmi_pin
*pin
= w
->priv
;
700 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
701 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
702 struct hdac_hdmi_pcm
*pcm
= NULL
;
703 const char *cvt_name
= e
->texts
[ucontrol
->value
.enumerated
.item
[0]];
705 ret
= snd_soc_dapm_put_enum_double(kcontrol
, ucontrol
);
709 mutex_lock(&hdmi
->pin_mutex
);
710 list_for_each_entry(pcm
, &hdmi
->pcm_list
, head
) {
715 * Jack status is not reported during device probe as the
716 * PCMs are not registered by then. So report it here.
718 if (!strcmp(cvt_name
, pcm
->cvt
->name
) && !pcm
->pin
) {
720 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
721 dev_dbg(&edev
->hdac
.dev
,
722 "jack report for pcm=%d\n",
725 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
727 mutex_unlock(&hdmi
->pin_mutex
);
731 mutex_unlock(&hdmi
->pin_mutex
);
737 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
738 * the display driver seem to be programming the connection list for the pin
741 * So programming all the possible inputs for the mux, the user has to take
742 * care of selecting the right one and leaving all other inputs selected to
745 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device
*edev
,
746 struct hdac_hdmi_pin
*pin
,
747 struct snd_soc_dapm_widget
*widget
,
748 const char *widget_name
)
750 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
751 struct snd_kcontrol_new
*kc
;
752 struct hdac_hdmi_cvt
*cvt
;
754 char kc_name
[NAME_SIZE
];
755 char mux_items
[NAME_SIZE
];
756 /* To hold inputs to the Pin mux */
757 char *items
[HDA_MAX_CONNECTIONS
];
759 int num_items
= hdmi
->num_cvt
+ 1;
761 kc
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*kc
), GFP_KERNEL
);
765 se
= devm_kzalloc(&edev
->hdac
.dev
, sizeof(*se
), GFP_KERNEL
);
769 sprintf(kc_name
, "Pin %d Input", pin
->nid
);
770 kc
->name
= devm_kstrdup(&edev
->hdac
.dev
, kc_name
, GFP_KERNEL
);
774 kc
->private_value
= (long)se
;
775 kc
->iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
777 kc
->info
= snd_soc_info_enum_double
;
778 kc
->put
= hdac_hdmi_set_pin_mux
;
779 kc
->get
= snd_soc_dapm_get_enum_double
;
781 se
->reg
= SND_SOC_NOPM
;
783 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
784 se
->items
= num_items
;
785 se
->mask
= roundup_pow_of_two(se
->items
) - 1;
787 sprintf(mux_items
, "NONE");
788 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
792 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
794 sprintf(mux_items
, "cvt %d", cvt
->nid
);
795 items
[i
] = devm_kstrdup(&edev
->hdac
.dev
, mux_items
, GFP_KERNEL
);
800 se
->texts
= devm_kmemdup(&edev
->hdac
.dev
, items
,
801 (num_items
* sizeof(char *)), GFP_KERNEL
);
805 return hdac_hdmi_fill_widget_info(&edev
->hdac
.dev
, widget
,
806 snd_soc_dapm_mux
, pin
, widget_name
, NULL
, kc
, 1);
809 /* Add cvt <- input <- mux route map */
810 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device
*edev
,
811 struct snd_soc_dapm_widget
*widgets
,
812 struct snd_soc_dapm_route
*route
, int rindex
)
814 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
815 const struct snd_kcontrol_new
*kc
;
817 int mux_index
= hdmi
->num_cvt
+ hdmi
->num_pin
;
820 for (i
= 0; i
< hdmi
->num_pin
; i
++) {
821 kc
= widgets
[mux_index
].kcontrol_news
;
822 se
= (struct soc_enum
*)kc
->private_value
;
823 for (j
= 0; j
< hdmi
->num_cvt
; j
++) {
824 hdac_hdmi_fill_route(&route
[rindex
],
825 widgets
[mux_index
].name
,
827 widgets
[j
].name
, NULL
);
837 * Widgets are added in the below sequence
838 * Converter widgets for num converters enumerated
839 * Pin widgets for num pins enumerated
840 * Pin mux widgets to represent connenction list of pin widget
842 * Total widgets elements = num_cvt + num_pin + num_pin;
844 * Routes are added as below:
845 * pin mux -> pin (based on num_pins)
846 * cvt -> "Input sel control" -> pin_mux
848 * Total route elements:
849 * num_pins + (pin_muxes * num_cvt)
851 static int create_fill_widget_route_map(struct snd_soc_dapm_context
*dapm
)
853 struct snd_soc_dapm_widget
*widgets
;
854 struct snd_soc_dapm_route
*route
;
855 struct hdac_ext_device
*edev
= to_hda_ext_device(dapm
->dev
);
856 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
857 struct snd_soc_dai_driver
*dai_drv
= dapm
->component
->dai_drv
;
858 char widget_name
[NAME_SIZE
];
859 struct hdac_hdmi_cvt
*cvt
;
860 struct hdac_hdmi_pin
*pin
;
861 int ret
, i
= 0, num_routes
= 0;
863 if (list_empty(&hdmi
->cvt_list
) || list_empty(&hdmi
->pin_list
))
866 widgets
= devm_kzalloc(dapm
->dev
,
867 (sizeof(*widgets
) * ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
)),
873 /* DAPM widgets to represent each converter widget */
874 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
875 sprintf(widget_name
, "Converter %d", cvt
->nid
);
876 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
877 snd_soc_dapm_aif_in
, &cvt
->nid
,
878 widget_name
, dai_drv
[i
].playback
.stream_name
, NULL
, 0);
884 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
885 sprintf(widget_name
, "hif%d Output", pin
->nid
);
886 ret
= hdac_hdmi_fill_widget_info(dapm
->dev
, &widgets
[i
],
887 snd_soc_dapm_output
, &pin
->nid
,
888 widget_name
, NULL
, NULL
, 0);
894 /* DAPM widgets to represent the connection list to pin widget */
895 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
896 sprintf(widget_name
, "Pin %d Mux", pin
->nid
);
897 ret
= hdac_hdmi_create_pin_muxs(edev
, pin
, &widgets
[i
],
903 /* For cvt to pin_mux mapping */
904 num_routes
+= hdmi
->num_cvt
;
906 /* For pin_mux to pin mapping */
910 route
= devm_kzalloc(dapm
->dev
, (sizeof(*route
) * num_routes
),
916 /* Add pin <- NULL <- mux route map */
917 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
918 int sink_index
= i
+ hdmi
->num_cvt
;
919 int src_index
= sink_index
+ hdmi
->num_pin
;
921 hdac_hdmi_fill_route(&route
[i
],
922 widgets
[sink_index
].name
, NULL
,
923 widgets
[src_index
].name
, NULL
);
928 hdac_hdmi_add_pinmux_cvt_route(edev
, widgets
, route
, i
);
930 snd_soc_dapm_new_controls(dapm
, widgets
,
931 ((2 * hdmi
->num_pin
) + hdmi
->num_cvt
));
933 snd_soc_dapm_add_routes(dapm
, route
, num_routes
);
934 snd_soc_dapm_new_widgets(dapm
->card
);
940 static int hdac_hdmi_init_dai_map(struct hdac_ext_device
*edev
)
942 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
943 struct hdac_hdmi_dai_pin_map
*dai_map
;
944 struct hdac_hdmi_cvt
*cvt
;
947 if (list_empty(&hdmi
->cvt_list
))
950 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
951 dai_map
= &hdmi
->dai_map
[dai_id
];
952 dai_map
->dai_id
= dai_id
;
957 if (dai_id
== HDA_MAX_CVTS
) {
958 dev_warn(&edev
->hdac
.dev
,
959 "Max dais supported: %d\n", dai_id
);
967 static int hdac_hdmi_add_cvt(struct hdac_ext_device
*edev
, hda_nid_t nid
)
969 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
970 struct hdac_hdmi_cvt
*cvt
;
971 char name
[NAME_SIZE
];
973 cvt
= kzalloc(sizeof(*cvt
), GFP_KERNEL
);
978 sprintf(name
, "cvt %d", cvt
->nid
);
979 cvt
->name
= kstrdup(name
, GFP_KERNEL
);
981 list_add_tail(&cvt
->head
, &hdmi
->cvt_list
);
984 return hdac_hdmi_query_cvt_params(&edev
->hdac
, cvt
);
987 static int hdac_hdmi_parse_eld(struct hdac_ext_device
*edev
,
988 struct hdac_hdmi_pin
*pin
)
990 unsigned int ver
, mnl
;
992 ver
= (pin
->eld
.eld_buffer
[DRM_ELD_VER
] & DRM_ELD_VER_MASK
)
993 >> DRM_ELD_VER_SHIFT
;
995 if (ver
!= ELD_VER_CEA_861D
&& ver
!= ELD_VER_PARTIAL
) {
996 dev_err(&edev
->hdac
.dev
, "HDMI: Unknown ELD version %d\n", ver
);
1000 mnl
= (pin
->eld
.eld_buffer
[DRM_ELD_CEA_EDID_VER_MNL
] &
1001 DRM_ELD_MNL_MASK
) >> DRM_ELD_MNL_SHIFT
;
1003 if (mnl
> ELD_MAX_MNL
) {
1004 dev_err(&edev
->hdac
.dev
, "HDMI: MNL Invalid %d\n", mnl
);
1008 pin
->eld
.info
.spk_alloc
= pin
->eld
.eld_buffer
[DRM_ELD_SPEAKER
];
1013 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin
*pin
)
1015 struct hdac_ext_device
*edev
= pin
->edev
;
1016 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1017 struct hdac_hdmi_pcm
*pcm
;
1020 mutex_lock(&hdmi
->pin_mutex
);
1021 pin
->eld
.monitor_present
= false;
1023 size
= snd_hdac_acomp_get_eld(&edev
->hdac
, pin
->nid
, -1,
1024 &pin
->eld
.monitor_present
, pin
->eld
.eld_buffer
,
1028 size
= min(size
, ELD_MAX_SIZE
);
1029 if (hdac_hdmi_parse_eld(edev
, pin
) < 0)
1034 pin
->eld
.eld_valid
= true;
1035 pin
->eld
.eld_size
= size
;
1037 pin
->eld
.eld_valid
= false;
1038 pin
->eld
.eld_size
= 0;
1041 pcm
= hdac_hdmi_get_pcm(edev
, pin
);
1043 if (!pin
->eld
.monitor_present
|| !pin
->eld
.eld_valid
) {
1045 dev_dbg(&edev
->hdac
.dev
, "%s: disconnect for pin %d\n",
1046 __func__
, pin
->nid
);
1049 * PCMs are not registered during device probe, so don't
1050 * report jack here. It will be done in usermode mux
1054 dev_dbg(&edev
->hdac
.dev
,
1055 "jack report for pcm=%d\n", pcm
->pcm_id
);
1057 snd_jack_report(pcm
->jack
, 0);
1060 mutex_unlock(&hdmi
->pin_mutex
);
1064 if (pin
->eld
.monitor_present
&& pin
->eld
.eld_valid
) {
1066 dev_dbg(&edev
->hdac
.dev
,
1067 "jack report for pcm=%d\n",
1070 snd_jack_report(pcm
->jack
, SND_JACK_AVOUT
);
1073 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET
, 16, 1,
1074 pin
->eld
.eld_buffer
, pin
->eld
.eld_size
, false);
1077 mutex_unlock(&hdmi
->pin_mutex
);
1080 static int hdac_hdmi_add_pin(struct hdac_ext_device
*edev
, hda_nid_t nid
)
1082 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1083 struct hdac_hdmi_pin
*pin
;
1085 pin
= kzalloc(sizeof(*pin
), GFP_KERNEL
);
1091 list_add_tail(&pin
->head
, &hdmi
->pin_list
);
1095 mutex_init(&pin
->lock
);
1100 #define INTEL_VENDOR_NID 0x08
1101 #define INTEL_GET_VENDOR_VERB 0xf81
1102 #define INTEL_SET_VENDOR_VERB 0x781
1103 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1104 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1106 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device
*hdac
)
1108 unsigned int vendor_param
;
1110 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1111 INTEL_GET_VENDOR_VERB
, 0);
1112 if (vendor_param
== -1 || vendor_param
& INTEL_EN_ALL_PIN_CVTS
)
1115 vendor_param
|= INTEL_EN_ALL_PIN_CVTS
;
1116 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1117 INTEL_SET_VENDOR_VERB
, vendor_param
);
1118 if (vendor_param
== -1)
1122 static void hdac_hdmi_skl_enable_dp12(struct hdac_device
*hdac
)
1124 unsigned int vendor_param
;
1126 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1127 INTEL_GET_VENDOR_VERB
, 0);
1128 if (vendor_param
== -1 || vendor_param
& INTEL_EN_DP12
)
1131 /* enable DP1.2 mode */
1132 vendor_param
|= INTEL_EN_DP12
;
1133 vendor_param
= snd_hdac_codec_read(hdac
, INTEL_VENDOR_NID
, 0,
1134 INTEL_SET_VENDOR_VERB
, vendor_param
);
1135 if (vendor_param
== -1)
1140 static struct snd_soc_dai_ops hdmi_dai_ops
= {
1141 .startup
= hdac_hdmi_pcm_open
,
1142 .shutdown
= hdac_hdmi_pcm_close
,
1143 .hw_params
= hdac_hdmi_set_hw_params
,
1144 .prepare
= hdac_hdmi_playback_prepare
,
1145 .trigger
= hdac_hdmi_trigger
,
1146 .hw_free
= hdac_hdmi_playback_cleanup
,
1150 * Each converter can support a stream independently. So a dai is created
1151 * based on the number of converter queried.
1153 static int hdac_hdmi_create_dais(struct hdac_device
*hdac
,
1154 struct snd_soc_dai_driver
**dais
,
1155 struct hdac_hdmi_priv
*hdmi
, int num_dais
)
1157 struct snd_soc_dai_driver
*hdmi_dais
;
1158 struct hdac_hdmi_cvt
*cvt
;
1159 char name
[NAME_SIZE
], dai_name
[NAME_SIZE
];
1162 unsigned int rate_max
= 384000, rate_min
= 8000;
1166 hdmi_dais
= devm_kzalloc(&hdac
->dev
,
1167 (sizeof(*hdmi_dais
) * num_dais
),
1172 list_for_each_entry(cvt
, &hdmi
->cvt_list
, head
) {
1173 ret
= snd_hdac_query_supported_pcm(hdac
, cvt
->nid
,
1174 &rates
, &formats
, &bps
);
1178 sprintf(dai_name
, "intel-hdmi-hifi%d", i
+1);
1179 hdmi_dais
[i
].name
= devm_kstrdup(&hdac
->dev
,
1180 dai_name
, GFP_KERNEL
);
1182 if (!hdmi_dais
[i
].name
)
1185 snprintf(name
, sizeof(name
), "hifi%d", i
+1);
1186 hdmi_dais
[i
].playback
.stream_name
=
1187 devm_kstrdup(&hdac
->dev
, name
, GFP_KERNEL
);
1188 if (!hdmi_dais
[i
].playback
.stream_name
)
1192 * Set caps based on capability queried from the converter.
1193 * It will be constrained runtime based on ELD queried.
1195 hdmi_dais
[i
].playback
.formats
= formats
;
1196 hdmi_dais
[i
].playback
.rates
= rates
;
1197 hdmi_dais
[i
].playback
.rate_max
= rate_max
;
1198 hdmi_dais
[i
].playback
.rate_min
= rate_min
;
1199 hdmi_dais
[i
].playback
.channels_min
= 2;
1200 hdmi_dais
[i
].playback
.channels_max
= 2;
1201 hdmi_dais
[i
].ops
= &hdmi_dai_ops
;
1212 * Parse all nodes and store the cvt/pin nids in array
1213 * Add one time initialization for pin and cvt widgets
1215 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device
*edev
,
1216 struct snd_soc_dai_driver
**dais
, int *num_dais
)
1220 struct hdac_device
*hdac
= &edev
->hdac
;
1221 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1224 hdac_hdmi_skl_enable_all_pins(hdac
);
1225 hdac_hdmi_skl_enable_dp12(hdac
);
1227 num_nodes
= snd_hdac_get_sub_nodes(hdac
, hdac
->afg
, &nid
);
1228 if (!nid
|| num_nodes
<= 0) {
1229 dev_warn(&hdac
->dev
, "HDMI: failed to get afg sub nodes\n");
1233 hdac
->num_nodes
= num_nodes
;
1234 hdac
->start_nid
= nid
;
1236 for (i
= 0; i
< hdac
->num_nodes
; i
++, nid
++) {
1240 caps
= get_wcaps(hdac
, nid
);
1241 type
= get_wcaps_type(caps
);
1243 if (!(caps
& AC_WCAP_DIGITAL
))
1248 case AC_WID_AUD_OUT
:
1249 ret
= hdac_hdmi_add_cvt(edev
, nid
);
1255 ret
= hdac_hdmi_add_pin(edev
, nid
);
1262 hdac
->end_nid
= nid
;
1264 if (!hdmi
->num_pin
|| !hdmi
->num_cvt
)
1267 ret
= hdac_hdmi_create_dais(hdac
, dais
, hdmi
, hdmi
->num_cvt
);
1269 dev_err(&hdac
->dev
, "Failed to create dais with err: %d\n",
1274 *num_dais
= hdmi
->num_cvt
;
1276 return hdac_hdmi_init_dai_map(edev
);
1279 static void hdac_hdmi_eld_notify_cb(void *aptr
, int port
, int pipe
)
1281 struct hdac_ext_device
*edev
= aptr
;
1282 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1283 struct hdac_hdmi_pin
*pin
;
1284 struct snd_soc_codec
*codec
= edev
->scodec
;
1286 /* Don't know how this mapping is derived */
1287 hda_nid_t pin_nid
= port
+ 0x04;
1289 dev_dbg(&edev
->hdac
.dev
, "%s: for pin: %d\n", __func__
, pin_nid
);
1292 * skip notification during system suspend (but not in runtime PM);
1293 * the state will be updated at resume. Also since the ELD and
1294 * connection states are updated in anyway at the end of the resume,
1295 * we can skip it when received during PM process.
1297 if (snd_power_get_state(codec
->component
.card
->snd_card
) !=
1301 if (atomic_read(&edev
->hdac
.in_pm
))
1304 list_for_each_entry(pin
, &hdmi
->pin_list
, head
) {
1305 if (pin
->nid
== pin_nid
)
1306 hdac_hdmi_present_sense(pin
);
1310 static struct i915_audio_component_audio_ops aops
= {
1311 .pin_eld_notify
= hdac_hdmi_eld_notify_cb
,
1314 static struct snd_pcm
*hdac_hdmi_get_pcm_from_id(struct snd_soc_card
*card
,
1317 struct snd_soc_pcm_runtime
*rtd
;
1319 list_for_each_entry(rtd
, &card
->rtd_list
, list
) {
1320 if (rtd
->pcm
&& (rtd
->pcm
->device
== device
))
1327 int hdac_hdmi_jack_init(struct snd_soc_dai
*dai
, int device
)
1329 char jack_name
[NAME_SIZE
];
1330 struct snd_soc_codec
*codec
= dai
->codec
;
1331 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1332 struct snd_soc_dapm_context
*dapm
=
1333 snd_soc_component_get_dapm(&codec
->component
);
1334 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1335 struct hdac_hdmi_pcm
*pcm
;
1336 struct snd_pcm
*snd_pcm
;
1340 * this is a new PCM device, create new pcm and
1341 * add to the pcm list
1343 pcm
= kzalloc(sizeof(*pcm
), GFP_KERNEL
);
1346 pcm
->pcm_id
= device
;
1347 pcm
->cvt
= hdmi
->dai_map
[dai
->id
].cvt
;
1349 snd_pcm
= hdac_hdmi_get_pcm_from_id(dai
->component
->card
, device
);
1351 err
= snd_hdac_add_chmap_ctls(snd_pcm
, device
, &hdmi
->chmap
);
1353 dev_err(&edev
->hdac
.dev
,
1354 "chmap control add failed with err: %d for pcm: %d\n",
1361 list_add_tail(&pcm
->head
, &hdmi
->pcm_list
);
1363 sprintf(jack_name
, "HDMI/DP, pcm=%d Jack", device
);
1365 return snd_jack_new(dapm
->card
->snd_card
, jack_name
,
1366 SND_JACK_AVOUT
, &pcm
->jack
, true, false);
1368 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init
);
1370 static int hdmi_codec_probe(struct snd_soc_codec
*codec
)
1372 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1373 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1374 struct snd_soc_dapm_context
*dapm
=
1375 snd_soc_component_get_dapm(&codec
->component
);
1376 struct hdac_hdmi_pin
*pin
;
1377 struct hdac_ext_link
*hlink
= NULL
;
1380 edev
->scodec
= codec
;
1383 * hold the ref while we probe, also no need to drop the ref on
1384 * exit, we call pm_runtime_suspend() so that will do for us
1386 hlink
= snd_hdac_ext_bus_get_link(edev
->ebus
, dev_name(&edev
->hdac
.dev
));
1388 dev_err(&edev
->hdac
.dev
, "hdac link not found\n");
1392 snd_hdac_ext_bus_link_get(edev
->ebus
, hlink
);
1394 ret
= create_fill_widget_route_map(dapm
);
1398 aops
.audio_ptr
= edev
;
1399 ret
= snd_hdac_i915_register_notifier(&aops
);
1401 dev_err(&edev
->hdac
.dev
, "notifier register failed: err: %d\n",
1406 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1407 hdac_hdmi_present_sense(pin
);
1409 /* Imp: Store the card pointer in hda_codec */
1410 edev
->card
= dapm
->card
->snd_card
;
1413 * hdac_device core already sets the state to active and calls
1414 * get_noresume. So enable runtime and set the device to suspend.
1416 pm_runtime_enable(&edev
->hdac
.dev
);
1417 pm_runtime_put(&edev
->hdac
.dev
);
1418 pm_runtime_suspend(&edev
->hdac
.dev
);
1423 static int hdmi_codec_remove(struct snd_soc_codec
*codec
)
1425 struct hdac_ext_device
*edev
= snd_soc_codec_get_drvdata(codec
);
1427 pm_runtime_disable(&edev
->hdac
.dev
);
1432 static int hdmi_codec_prepare(struct device
*dev
)
1434 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1435 struct hdac_device
*hdac
= &edev
->hdac
;
1437 pm_runtime_get_sync(&edev
->hdac
.dev
);
1441 * codec_read is preferred over codec_write to set the power state.
1442 * This way verb is send to set the power state and response
1443 * is received. So setting power state is ensured without using loop
1444 * to read the state.
1446 snd_hdac_codec_read(hdac
, hdac
->afg
, 0, AC_VERB_SET_POWER_STATE
,
1452 static void hdmi_codec_complete(struct device
*dev
)
1454 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1455 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1456 struct hdac_hdmi_pin
*pin
;
1457 struct hdac_device
*hdac
= &edev
->hdac
;
1460 snd_hdac_codec_read(hdac
, hdac
->afg
, 0, AC_VERB_SET_POWER_STATE
,
1463 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1464 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1467 * As the ELD notify callback request is not entertained while the
1468 * device is in suspend state. Need to manually check detection of
1471 list_for_each_entry(pin
, &hdmi
->pin_list
, head
)
1472 hdac_hdmi_present_sense(pin
);
1474 pm_runtime_put_sync(&edev
->hdac
.dev
);
1477 #define hdmi_codec_prepare NULL
1478 #define hdmi_codec_complete NULL
1481 static struct snd_soc_codec_driver hdmi_hda_codec
= {
1482 .probe
= hdmi_codec_probe
,
1483 .remove
= hdmi_codec_remove
,
1484 .idle_bias_off
= true,
1487 static void hdac_hdmi_get_chmap(struct hdac_device
*hdac
, int pcm_idx
,
1488 unsigned char *chmap
)
1490 struct hdac_ext_device
*edev
= to_ehdac_device(hdac
);
1491 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1492 struct hdac_hdmi_pcm
*pcm
= get_hdmi_pcm_from_id(hdmi
, pcm_idx
);
1493 struct hdac_hdmi_pin
*pin
= pcm
->pin
;
1495 /* chmap is already set to 0 in caller */
1499 memcpy(chmap
, pin
->chmap
, ARRAY_SIZE(pin
->chmap
));
1502 static void hdac_hdmi_set_chmap(struct hdac_device
*hdac
, int pcm_idx
,
1503 unsigned char *chmap
, int prepared
)
1505 struct hdac_ext_device
*edev
= to_ehdac_device(hdac
);
1506 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1507 struct hdac_hdmi_pcm
*pcm
= get_hdmi_pcm_from_id(hdmi
, pcm_idx
);
1508 struct hdac_hdmi_pin
*pin
= pcm
->pin
;
1510 mutex_lock(&pin
->lock
);
1511 pin
->chmap_set
= true;
1512 memcpy(pin
->chmap
, chmap
, ARRAY_SIZE(pin
->chmap
));
1514 hdac_hdmi_setup_audio_infoframe(edev
, pcm
->cvt
->nid
, pin
->nid
);
1515 mutex_unlock(&pin
->lock
);
1518 static bool is_hdac_hdmi_pcm_attached(struct hdac_device
*hdac
, int pcm_idx
)
1520 struct hdac_ext_device
*edev
= to_ehdac_device(hdac
);
1521 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1522 struct hdac_hdmi_pcm
*pcm
= get_hdmi_pcm_from_id(hdmi
, pcm_idx
);
1523 struct hdac_hdmi_pin
*pin
= pcm
->pin
;
1525 return pin
? true:false;
1528 static int hdac_hdmi_get_spk_alloc(struct hdac_device
*hdac
, int pcm_idx
)
1530 struct hdac_ext_device
*edev
= to_ehdac_device(hdac
);
1531 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1532 struct hdac_hdmi_pcm
*pcm
= get_hdmi_pcm_from_id(hdmi
, pcm_idx
);
1533 struct hdac_hdmi_pin
*pin
= pcm
->pin
;
1535 if (!pin
|| !pin
->eld
.eld_valid
)
1538 return pin
->eld
.info
.spk_alloc
;
1541 static int hdac_hdmi_dev_probe(struct hdac_ext_device
*edev
)
1543 struct hdac_device
*codec
= &edev
->hdac
;
1544 struct hdac_hdmi_priv
*hdmi_priv
;
1545 struct snd_soc_dai_driver
*hdmi_dais
= NULL
;
1546 struct hdac_ext_link
*hlink
= NULL
;
1550 /* hold the ref while we probe */
1551 hlink
= snd_hdac_ext_bus_get_link(edev
->ebus
, dev_name(&edev
->hdac
.dev
));
1553 dev_err(&edev
->hdac
.dev
, "hdac link not found\n");
1557 snd_hdac_ext_bus_link_get(edev
->ebus
, hlink
);
1559 hdmi_priv
= devm_kzalloc(&codec
->dev
, sizeof(*hdmi_priv
), GFP_KERNEL
);
1560 if (hdmi_priv
== NULL
)
1563 edev
->private_data
= hdmi_priv
;
1564 snd_hdac_register_chmap_ops(codec
, &hdmi_priv
->chmap
);
1565 hdmi_priv
->chmap
.ops
.get_chmap
= hdac_hdmi_get_chmap
;
1566 hdmi_priv
->chmap
.ops
.set_chmap
= hdac_hdmi_set_chmap
;
1567 hdmi_priv
->chmap
.ops
.is_pcm_attached
= is_hdac_hdmi_pcm_attached
;
1568 hdmi_priv
->chmap
.ops
.get_spk_alloc
= hdac_hdmi_get_spk_alloc
;
1570 dev_set_drvdata(&codec
->dev
, edev
);
1572 INIT_LIST_HEAD(&hdmi_priv
->pin_list
);
1573 INIT_LIST_HEAD(&hdmi_priv
->cvt_list
);
1574 INIT_LIST_HEAD(&hdmi_priv
->pcm_list
);
1575 mutex_init(&hdmi_priv
->pin_mutex
);
1578 * Turned off in the runtime_suspend during the first explicit
1579 * pm_runtime_suspend call.
1581 ret
= snd_hdac_display_power(edev
->hdac
.bus
, true);
1583 dev_err(&edev
->hdac
.dev
,
1584 "Cannot turn on display power on i915 err: %d\n",
1589 ret
= hdac_hdmi_parse_and_map_nid(edev
, &hdmi_dais
, &num_dais
);
1591 dev_err(&codec
->dev
,
1592 "Failed in parse and map nid with err: %d\n", ret
);
1596 /* ASoC specific initialization */
1597 ret
= snd_soc_register_codec(&codec
->dev
, &hdmi_hda_codec
,
1598 hdmi_dais
, num_dais
);
1600 snd_hdac_ext_bus_link_put(edev
->ebus
, hlink
);
1605 static int hdac_hdmi_dev_remove(struct hdac_ext_device
*edev
)
1607 struct hdac_hdmi_priv
*hdmi
= edev
->private_data
;
1608 struct hdac_hdmi_pin
*pin
, *pin_next
;
1609 struct hdac_hdmi_cvt
*cvt
, *cvt_next
;
1610 struct hdac_hdmi_pcm
*pcm
, *pcm_next
;
1612 snd_soc_unregister_codec(&edev
->hdac
.dev
);
1614 list_for_each_entry_safe(pcm
, pcm_next
, &hdmi
->pcm_list
, head
) {
1617 list_del(&pcm
->head
);
1621 list_for_each_entry_safe(cvt
, cvt_next
, &hdmi
->cvt_list
, head
) {
1622 list_del(&cvt
->head
);
1627 list_for_each_entry_safe(pin
, pin_next
, &hdmi
->pin_list
, head
) {
1628 list_del(&pin
->head
);
1636 static int hdac_hdmi_runtime_suspend(struct device
*dev
)
1638 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1639 struct hdac_device
*hdac
= &edev
->hdac
;
1640 struct hdac_bus
*bus
= hdac
->bus
;
1641 struct hdac_ext_bus
*ebus
= hbus_to_ebus(bus
);
1642 struct hdac_ext_link
*hlink
= NULL
;
1645 dev_dbg(dev
, "Enter: %s\n", __func__
);
1647 /* controller may not have been initialized for the first time */
1653 * codec_read is preferred over codec_write to set the power state.
1654 * This way verb is send to set the power state and response
1655 * is received. So setting power state is ensured without using loop
1656 * to read the state.
1658 snd_hdac_codec_read(hdac
, hdac
->afg
, 0, AC_VERB_SET_POWER_STATE
,
1660 err
= snd_hdac_display_power(bus
, false);
1662 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1666 hlink
= snd_hdac_ext_bus_get_link(ebus
, dev_name(dev
));
1668 dev_err(dev
, "hdac link not found\n");
1672 snd_hdac_ext_bus_link_put(ebus
, hlink
);
1677 static int hdac_hdmi_runtime_resume(struct device
*dev
)
1679 struct hdac_ext_device
*edev
= to_hda_ext_device(dev
);
1680 struct hdac_device
*hdac
= &edev
->hdac
;
1681 struct hdac_bus
*bus
= hdac
->bus
;
1682 struct hdac_ext_bus
*ebus
= hbus_to_ebus(bus
);
1683 struct hdac_ext_link
*hlink
= NULL
;
1686 dev_dbg(dev
, "Enter: %s\n", __func__
);
1688 /* controller may not have been initialized for the first time */
1692 hlink
= snd_hdac_ext_bus_get_link(ebus
, dev_name(dev
));
1694 dev_err(dev
, "hdac link not found\n");
1698 snd_hdac_ext_bus_link_get(ebus
, hlink
);
1700 err
= snd_hdac_display_power(bus
, true);
1702 dev_err(bus
->dev
, "Cannot turn on display power on i915\n");
1706 hdac_hdmi_skl_enable_all_pins(&edev
->hdac
);
1707 hdac_hdmi_skl_enable_dp12(&edev
->hdac
);
1710 snd_hdac_codec_read(hdac
, hdac
->afg
, 0, AC_VERB_SET_POWER_STATE
,
1716 #define hdac_hdmi_runtime_suspend NULL
1717 #define hdac_hdmi_runtime_resume NULL
1720 static const struct dev_pm_ops hdac_hdmi_pm
= {
1721 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend
, hdac_hdmi_runtime_resume
, NULL
)
1722 .prepare
= hdmi_codec_prepare
,
1723 .complete
= hdmi_codec_complete
,
1726 static const struct hda_device_id hdmi_list
[] = {
1727 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1728 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1729 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
1733 MODULE_DEVICE_TABLE(hdaudio
, hdmi_list
);
1735 static struct hdac_ext_driver hdmi_driver
= {
1738 .name
= "HDMI HDA Codec",
1739 .pm
= &hdac_hdmi_pm
,
1741 .id_table
= hdmi_list
,
1743 .probe
= hdac_hdmi_dev_probe
,
1744 .remove
= hdac_hdmi_dev_remove
,
1747 static int __init
hdmi_init(void)
1749 return snd_hda_ext_driver_register(&hdmi_driver
);
1752 static void __exit
hdmi_exit(void)
1754 snd_hda_ext_driver_unregister(&hdmi_driver
);
1757 module_init(hdmi_init
);
1758 module_exit(hdmi_exit
);
1760 MODULE_LICENSE("GPL v2");
1761 MODULE_DESCRIPTION("HDMI HD codec");
1762 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1763 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");