ASoC: hdac_hdmi: Fix to wait for D3 before powering off codec
[linux-2.6/btrfs-unstable.git] / sound / soc / codecs / hdac_hdmi.c
blobaa953a5b57d41de5901166a601786c40458da9d6
1 /*
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"
35 #define NAME_SIZE 32
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;
51 u32 rates;
52 u64 formats;
53 unsigned int maxbps;
56 struct hdac_hdmi_cvt {
57 struct list_head head;
58 hda_nid_t nid;
59 const char *name;
60 struct hdac_hdmi_cvt_params params;
63 struct hdac_hdmi_eld {
64 bool monitor_present;
65 bool eld_valid;
66 int eld_size;
67 char eld_buffer[ELD_MAX_SIZE];
70 struct hdac_hdmi_pin {
71 struct list_head head;
72 hda_nid_t nid;
73 int num_mux_nids;
74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
77 int repoll_count;
78 struct delayed_work work;
81 struct hdac_hdmi_pcm {
82 struct list_head head;
83 int pcm_id;
84 struct hdac_hdmi_pin *pin;
85 struct hdac_hdmi_cvt *cvt;
86 struct snd_jack *jack;
89 struct hdac_hdmi_dai_pin_map {
90 int dai_id;
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;
100 int num_pin;
101 int num_cvt;
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)
119 return (sad[2] & 7);
122 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
123 void *eld)
125 u64 formats = SNDRV_PCM_FMTBIT_S16;
126 int i;
127 const u8 *sad, *eld_buf = eld;
129 sad = drm_eld_sad(eld_buf);
130 if (!sad)
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;
145 format_constraint:
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
147 formats);
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)
155 unsigned int val;
157 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158 byte_index);
160 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161 byte_index, val);
163 return val;
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,
169 AC_DIPSIZE_ELD_BUF);
173 * This function queries the ELD size and ELD data and fills in the buffer
174 * passed by user
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);
189 return -ERANGE;
192 /* set ELD buffer */
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)) {
200 dev_err(&codec->dev,
201 "HDMI: invalid ELD data byte %d\n", i);
202 ret = -EINVAL;
203 goto error;
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.
212 if (!val && !i) {
213 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214 ret = -EINVAL;
215 goto error;
217 buf[i] = val;
220 *eld_size = size;
221 error:
222 return ret;
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)
229 unsigned int val;
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);
241 return 0;
244 static void
245 hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246 int packet_index, int byte_index)
248 int val;
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 {
257 u8 type; /* 0x84 */
258 u8 len; /* 0x1b */
259 u8 ver; /* 0x11 << 2 */
261 u8 CC02_CT47; /* match with HDMI infoframe from this on */
262 u8 SS01_SF24;
263 u8 CXT04;
264 u8 CA;
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;
276 u8 *dip;
277 int ret;
278 int i;
279 const u8 *eld_buf;
280 u8 conn_type;
281 int channels = 2;
283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
285 break;
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);
295 switch (conn_type) {
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));
303 if (ret < 0)
304 return ret;
306 dip = (u8 *)&frame;
307 break;
309 case DRM_ELD_CONN_TYPE_DP:
310 memset(&dp_ai, 0, sizeof(dp_ai));
311 dp_ai.type = 0x84;
312 dp_ai.len = 0x1b;
313 dp_ai.ver = 0x11 << 2;
314 dp_ai.CC02_CT47 = channels - 1;
315 dp_ai.CA = 0;
317 dip = (u8 *)&dp_ai;
318 break;
320 default:
321 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
322 conn_type);
323 return -EIO;
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]);
338 } else {
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);
349 return 0;
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,
357 pwr_state))
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,
363 pwr_state))
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;
375 int ret;
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,
384 dai_map->pin->nid);
385 if (ret < 0)
386 return ret;
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];
402 pin = dai_map->pin;
404 if (!pin)
405 return -ENODEV;
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",
409 pin->nid);
410 return -ENODEV;
413 dd = snd_soc_dai_get_dma_data(dai, substream);
414 if (!dd) {
415 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
416 if (!dd)
417 return -ENOMEM;
420 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
421 params_channels(hparams), params_format(hparams),
422 24, 0);
424 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
426 return 0;
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);
441 if (dd) {
442 snd_soc_dai_set_dma_data(dai, substream, NULL);
443 kfree(dd);
446 return 0;
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)
464 int mux_idx;
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);
471 break;
475 if (mux_idx == pin->num_mux_nids)
476 return -EIO;
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);
487 return 0;
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));
497 return -EINVAL;
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",
504 pin->nid);
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
519 * connected.
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;
528 int ret, i;
530 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
531 if (pcm->cvt == cvt) {
532 pin = pcm->pin;
533 break;
537 if (pin) {
538 ret = hdac_hdmi_query_pin_connlist(edev, pin);
539 if (ret < 0)
540 return NULL;
542 for (i = 0; i < pin->num_mux_nids; i++) {
543 if (pin->mux_nids[i] == cvt->nid)
544 return pin;
548 return NULL;
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
554 * doesn't fail.
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;
564 int ret;
566 dai_map = &hdmi->dai_map[dai->id];
568 cvt = dai_map->cvt;
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.
575 if (!pin)
576 return 0;
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);
585 return 0;
588 dai_map->pin = pin;
590 hdac_hdmi_enable_cvt(hdac, dai_map);
591 ret = hdac_hdmi_enable_pin(hdac, dai_map);
592 if (ret < 0)
593 return ret;
595 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
596 pin->eld.eld_buffer);
597 if (ret < 0)
598 return ret;
600 return snd_pcm_hw_constraint_eld(substream->runtime,
601 pin->eld.eld_buffer);
604 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
605 struct snd_soc_dai *dai)
607 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
608 struct hdac_hdmi_priv *hdmi = hdac->private_data;
609 struct hdac_hdmi_dai_pin_map *dai_map;
611 dai_map = &hdmi->dai_map[dai->id];
613 if (dai_map->pin) {
614 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
615 AC_VERB_SET_CHANNEL_STREAMID, 0);
616 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
617 AC_VERB_SET_STREAM_FORMAT, 0);
619 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
621 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
622 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
624 dai_map->pin = NULL;
628 static int
629 hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
631 int err;
633 /* Only stereo supported as of now */
634 cvt->params.channels_min = cvt->params.channels_max = 2;
636 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
637 &cvt->params.rates,
638 &cvt->params.formats,
639 &cvt->params.maxbps);
640 if (err < 0)
641 dev_err(&hdac->dev,
642 "Failed to query pcm params for nid %d: %d\n",
643 cvt->nid, err);
645 return err;
648 static int hdac_hdmi_fill_widget_info(struct device *dev,
649 struct snd_soc_dapm_widget *w,
650 enum snd_soc_dapm_type id, void *priv,
651 const char *wname, const char *stream,
652 struct snd_kcontrol_new *wc, int numkc)
654 w->id = id;
655 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
656 if (!w->name)
657 return -ENOMEM;
659 w->sname = stream;
660 w->reg = SND_SOC_NOPM;
661 w->shift = 0;
662 w->kcontrol_news = wc;
663 w->num_kcontrols = numkc;
664 w->priv = priv;
666 return 0;
669 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
670 const char *sink, const char *control, const char *src,
671 int (*handler)(struct snd_soc_dapm_widget *src,
672 struct snd_soc_dapm_widget *sink))
674 route->sink = sink;
675 route->source = src;
676 route->control = control;
677 route->connected = handler;
680 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
681 struct hdac_hdmi_pin *pin)
683 struct hdac_hdmi_priv *hdmi = edev->private_data;
684 struct hdac_hdmi_pcm *pcm = NULL;
686 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
687 if (pcm->pin == pin)
688 return pcm;
691 return NULL;
695 * Based on user selection, map the PINs with the PCMs.
697 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
698 struct snd_ctl_elem_value *ucontrol)
700 int ret;
701 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
702 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
703 struct snd_soc_dapm_context *dapm = w->dapm;
704 struct hdac_hdmi_pin *pin = w->priv;
705 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
706 struct hdac_hdmi_priv *hdmi = edev->private_data;
707 struct hdac_hdmi_pcm *pcm = NULL;
708 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
710 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
711 if (ret < 0)
712 return ret;
714 mutex_lock(&hdmi->pin_mutex);
715 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
716 if (pcm->pin == pin)
717 pcm->pin = NULL;
720 * Jack status is not reported during device probe as the
721 * PCMs are not registered by then. So report it here.
723 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
724 pcm->pin = pin;
725 if (pin->eld.monitor_present && pin->eld.eld_valid) {
726 dev_dbg(&edev->hdac.dev,
727 "jack report for pcm=%d\n",
728 pcm->pcm_id);
730 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
732 mutex_unlock(&hdmi->pin_mutex);
733 return ret;
736 mutex_unlock(&hdmi->pin_mutex);
738 return ret;
742 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
743 * the display driver seem to be programming the connection list for the pin
744 * widget runtime.
746 * So programming all the possible inputs for the mux, the user has to take
747 * care of selecting the right one and leaving all other inputs selected to
748 * "NONE"
750 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
751 struct hdac_hdmi_pin *pin,
752 struct snd_soc_dapm_widget *widget,
753 const char *widget_name)
755 struct hdac_hdmi_priv *hdmi = edev->private_data;
756 struct snd_kcontrol_new *kc;
757 struct hdac_hdmi_cvt *cvt;
758 struct soc_enum *se;
759 char kc_name[NAME_SIZE];
760 char mux_items[NAME_SIZE];
761 /* To hold inputs to the Pin mux */
762 char *items[HDA_MAX_CONNECTIONS];
763 int i = 0;
764 int num_items = hdmi->num_cvt + 1;
766 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
767 if (!kc)
768 return -ENOMEM;
770 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
771 if (!se)
772 return -ENOMEM;
774 sprintf(kc_name, "Pin %d Input", pin->nid);
775 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
776 if (!kc->name)
777 return -ENOMEM;
779 kc->private_value = (long)se;
780 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781 kc->access = 0;
782 kc->info = snd_soc_info_enum_double;
783 kc->put = hdac_hdmi_set_pin_mux;
784 kc->get = snd_soc_dapm_get_enum_double;
786 se->reg = SND_SOC_NOPM;
788 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
789 se->items = num_items;
790 se->mask = roundup_pow_of_two(se->items) - 1;
792 sprintf(mux_items, "NONE");
793 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
794 if (!items[i])
795 return -ENOMEM;
797 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
798 i++;
799 sprintf(mux_items, "cvt %d", cvt->nid);
800 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
801 if (!items[i])
802 return -ENOMEM;
805 se->texts = devm_kmemdup(&edev->hdac.dev, items,
806 (num_items * sizeof(char *)), GFP_KERNEL);
807 if (!se->texts)
808 return -ENOMEM;
810 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
811 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
814 /* Add cvt <- input <- mux route map */
815 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
816 struct snd_soc_dapm_widget *widgets,
817 struct snd_soc_dapm_route *route, int rindex)
819 struct hdac_hdmi_priv *hdmi = edev->private_data;
820 const struct snd_kcontrol_new *kc;
821 struct soc_enum *se;
822 int mux_index = hdmi->num_cvt + hdmi->num_pin;
823 int i, j;
825 for (i = 0; i < hdmi->num_pin; i++) {
826 kc = widgets[mux_index].kcontrol_news;
827 se = (struct soc_enum *)kc->private_value;
828 for (j = 0; j < hdmi->num_cvt; j++) {
829 hdac_hdmi_fill_route(&route[rindex],
830 widgets[mux_index].name,
831 se->texts[j + 1],
832 widgets[j].name, NULL);
834 rindex++;
837 mux_index++;
842 * Widgets are added in the below sequence
843 * Converter widgets for num converters enumerated
844 * Pin widgets for num pins enumerated
845 * Pin mux widgets to represent connenction list of pin widget
847 * Total widgets elements = num_cvt + num_pin + num_pin;
849 * Routes are added as below:
850 * pin mux -> pin (based on num_pins)
851 * cvt -> "Input sel control" -> pin_mux
853 * Total route elements:
854 * num_pins + (pin_muxes * num_cvt)
856 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
858 struct snd_soc_dapm_widget *widgets;
859 struct snd_soc_dapm_route *route;
860 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
861 struct hdac_hdmi_priv *hdmi = edev->private_data;
862 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
863 char widget_name[NAME_SIZE];
864 struct hdac_hdmi_cvt *cvt;
865 struct hdac_hdmi_pin *pin;
866 int ret, i = 0, num_routes = 0;
868 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
869 return -EINVAL;
871 widgets = devm_kzalloc(dapm->dev,
872 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
873 GFP_KERNEL);
875 if (!widgets)
876 return -ENOMEM;
878 /* DAPM widgets to represent each converter widget */
879 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
880 sprintf(widget_name, "Converter %d", cvt->nid);
881 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
882 snd_soc_dapm_aif_in, &cvt->nid,
883 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
884 if (ret < 0)
885 return ret;
886 i++;
889 list_for_each_entry(pin, &hdmi->pin_list, head) {
890 sprintf(widget_name, "hif%d Output", pin->nid);
891 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
892 snd_soc_dapm_output, &pin->nid,
893 widget_name, NULL, NULL, 0);
894 if (ret < 0)
895 return ret;
896 i++;
899 /* DAPM widgets to represent the connection list to pin widget */
900 list_for_each_entry(pin, &hdmi->pin_list, head) {
901 sprintf(widget_name, "Pin %d Mux", pin->nid);
902 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
903 widget_name);
904 if (ret < 0)
905 return ret;
906 i++;
908 /* For cvt to pin_mux mapping */
909 num_routes += hdmi->num_cvt;
911 /* For pin_mux to pin mapping */
912 num_routes++;
915 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
916 GFP_KERNEL);
917 if (!route)
918 return -ENOMEM;
920 i = 0;
921 /* Add pin <- NULL <- mux route map */
922 list_for_each_entry(pin, &hdmi->pin_list, head) {
923 int sink_index = i + hdmi->num_cvt;
924 int src_index = sink_index + hdmi->num_pin;
926 hdac_hdmi_fill_route(&route[i],
927 widgets[sink_index].name, NULL,
928 widgets[src_index].name, NULL);
929 i++;
933 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
935 snd_soc_dapm_new_controls(dapm, widgets,
936 ((2 * hdmi->num_pin) + hdmi->num_cvt));
938 snd_soc_dapm_add_routes(dapm, route, num_routes);
939 snd_soc_dapm_new_widgets(dapm->card);
941 return 0;
945 static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
947 struct hdac_hdmi_priv *hdmi = edev->private_data;
948 struct hdac_hdmi_dai_pin_map *dai_map;
949 struct hdac_hdmi_cvt *cvt;
950 int dai_id = 0;
952 if (list_empty(&hdmi->cvt_list))
953 return -EINVAL;
955 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
956 dai_map = &hdmi->dai_map[dai_id];
957 dai_map->dai_id = dai_id;
958 dai_map->cvt = cvt;
960 dai_id++;
962 if (dai_id == HDA_MAX_CVTS) {
963 dev_warn(&edev->hdac.dev,
964 "Max dais supported: %d\n", dai_id);
965 break;
969 return 0;
972 static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
974 struct hdac_hdmi_priv *hdmi = edev->private_data;
975 struct hdac_hdmi_cvt *cvt;
976 char name[NAME_SIZE];
978 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
979 if (!cvt)
980 return -ENOMEM;
982 cvt->nid = nid;
983 sprintf(name, "cvt %d", cvt->nid);
984 cvt->name = kstrdup(name, GFP_KERNEL);
986 list_add_tail(&cvt->head, &hdmi->cvt_list);
987 hdmi->num_cvt++;
989 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
992 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
994 struct hdac_ext_device *edev = pin->edev;
995 struct hdac_hdmi_priv *hdmi = edev->private_data;
996 struct hdac_hdmi_pcm *pcm;
997 int val;
999 pin->repoll_count = repoll;
1001 pm_runtime_get_sync(&edev->hdac.dev);
1002 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1003 AC_VERB_GET_PIN_SENSE, 0);
1005 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1006 val, pin->nid);
1009 mutex_lock(&hdmi->pin_mutex);
1010 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1011 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1013 pcm = hdac_hdmi_get_pcm(edev, pin);
1015 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1017 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1018 __func__, pin->nid);
1021 * PCMs are not registered during device probe, so don't
1022 * report jack here. It will be done in usermode mux
1023 * control select.
1025 if (pcm) {
1026 dev_dbg(&edev->hdac.dev,
1027 "jack report for pcm=%d\n", pcm->pcm_id);
1029 snd_jack_report(pcm->jack, 0);
1032 mutex_unlock(&hdmi->pin_mutex);
1033 goto put_hdac_device;
1036 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1037 /* TODO: use i915 component for reading ELD later */
1038 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1039 pin->eld.eld_buffer,
1040 &pin->eld.eld_size) == 0) {
1042 if (pcm) {
1043 dev_dbg(&edev->hdac.dev,
1044 "jack report for pcm=%d\n",
1045 pcm->pcm_id);
1047 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1050 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1051 pin->eld.eld_buffer, pin->eld.eld_size);
1052 } else {
1053 pin->eld.monitor_present = false;
1054 pin->eld.eld_valid = false;
1056 if (pcm) {
1057 dev_dbg(&edev->hdac.dev,
1058 "jack report for pcm=%d\n",
1059 pcm->pcm_id);
1061 snd_jack_report(pcm->jack, 0);
1066 mutex_unlock(&hdmi->pin_mutex);
1069 * Sometimes the pin_sense may present invalid monitor
1070 * present and eld_valid. If ELD data is not valid, loop few
1071 * more times to get correct pin sense and valid ELD.
1073 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1074 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1076 put_hdac_device:
1077 pm_runtime_put_sync(&edev->hdac.dev);
1080 static void hdac_hdmi_repoll_eld(struct work_struct *work)
1082 struct hdac_hdmi_pin *pin =
1083 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1085 /* picked from legacy HDA driver */
1086 if (pin->repoll_count++ > 6)
1087 pin->repoll_count = 0;
1089 hdac_hdmi_present_sense(pin, pin->repoll_count);
1092 static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1094 struct hdac_hdmi_priv *hdmi = edev->private_data;
1095 struct hdac_hdmi_pin *pin;
1097 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1098 if (!pin)
1099 return -ENOMEM;
1101 pin->nid = nid;
1103 list_add_tail(&pin->head, &hdmi->pin_list);
1104 hdmi->num_pin++;
1106 pin->edev = edev;
1107 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1109 return 0;
1112 #define INTEL_VENDOR_NID 0x08
1113 #define INTEL_GET_VENDOR_VERB 0xf81
1114 #define INTEL_SET_VENDOR_VERB 0x781
1115 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1116 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1118 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1120 unsigned int vendor_param;
1122 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1123 INTEL_GET_VENDOR_VERB, 0);
1124 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1125 return;
1127 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1128 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1129 INTEL_SET_VENDOR_VERB, vendor_param);
1130 if (vendor_param == -1)
1131 return;
1134 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1136 unsigned int vendor_param;
1138 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1139 INTEL_GET_VENDOR_VERB, 0);
1140 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1141 return;
1143 /* enable DP1.2 mode */
1144 vendor_param |= INTEL_EN_DP12;
1145 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1146 INTEL_SET_VENDOR_VERB, vendor_param);
1147 if (vendor_param == -1)
1148 return;
1152 static struct snd_soc_dai_ops hdmi_dai_ops = {
1153 .startup = hdac_hdmi_pcm_open,
1154 .shutdown = hdac_hdmi_pcm_close,
1155 .hw_params = hdac_hdmi_set_hw_params,
1156 .prepare = hdac_hdmi_playback_prepare,
1157 .hw_free = hdac_hdmi_playback_cleanup,
1161 * Each converter can support a stream independently. So a dai is created
1162 * based on the number of converter queried.
1164 static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1165 struct snd_soc_dai_driver **dais,
1166 struct hdac_hdmi_priv *hdmi, int num_dais)
1168 struct snd_soc_dai_driver *hdmi_dais;
1169 struct hdac_hdmi_cvt *cvt;
1170 char name[NAME_SIZE], dai_name[NAME_SIZE];
1171 int i = 0;
1172 u32 rates, bps;
1173 unsigned int rate_max = 384000, rate_min = 8000;
1174 u64 formats;
1175 int ret;
1177 hdmi_dais = devm_kzalloc(&hdac->dev,
1178 (sizeof(*hdmi_dais) * num_dais),
1179 GFP_KERNEL);
1180 if (!hdmi_dais)
1181 return -ENOMEM;
1183 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1184 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1185 &rates, &formats, &bps);
1186 if (ret)
1187 return ret;
1189 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1190 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1191 dai_name, GFP_KERNEL);
1193 if (!hdmi_dais[i].name)
1194 return -ENOMEM;
1196 snprintf(name, sizeof(name), "hifi%d", i+1);
1197 hdmi_dais[i].playback.stream_name =
1198 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1199 if (!hdmi_dais[i].playback.stream_name)
1200 return -ENOMEM;
1203 * Set caps based on capability queried from the converter.
1204 * It will be constrained runtime based on ELD queried.
1206 hdmi_dais[i].playback.formats = formats;
1207 hdmi_dais[i].playback.rates = rates;
1208 hdmi_dais[i].playback.rate_max = rate_max;
1209 hdmi_dais[i].playback.rate_min = rate_min;
1210 hdmi_dais[i].playback.channels_min = 2;
1211 hdmi_dais[i].playback.channels_max = 2;
1212 hdmi_dais[i].ops = &hdmi_dai_ops;
1214 i++;
1217 *dais = hdmi_dais;
1219 return 0;
1223 * Parse all nodes and store the cvt/pin nids in array
1224 * Add one time initialization for pin and cvt widgets
1226 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1227 struct snd_soc_dai_driver **dais, int *num_dais)
1229 hda_nid_t nid;
1230 int i, num_nodes;
1231 struct hdac_device *hdac = &edev->hdac;
1232 struct hdac_hdmi_priv *hdmi = edev->private_data;
1233 int ret;
1235 hdac_hdmi_skl_enable_all_pins(hdac);
1236 hdac_hdmi_skl_enable_dp12(hdac);
1238 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1239 if (!nid || num_nodes <= 0) {
1240 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1241 return -EINVAL;
1244 hdac->num_nodes = num_nodes;
1245 hdac->start_nid = nid;
1247 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1248 unsigned int caps;
1249 unsigned int type;
1251 caps = get_wcaps(hdac, nid);
1252 type = get_wcaps_type(caps);
1254 if (!(caps & AC_WCAP_DIGITAL))
1255 continue;
1257 switch (type) {
1259 case AC_WID_AUD_OUT:
1260 ret = hdac_hdmi_add_cvt(edev, nid);
1261 if (ret < 0)
1262 return ret;
1263 break;
1265 case AC_WID_PIN:
1266 ret = hdac_hdmi_add_pin(edev, nid);
1267 if (ret < 0)
1268 return ret;
1269 break;
1273 hdac->end_nid = nid;
1275 if (!hdmi->num_pin || !hdmi->num_cvt)
1276 return -EIO;
1278 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1279 if (ret) {
1280 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1281 ret);
1282 return ret;
1285 *num_dais = hdmi->num_cvt;
1287 return hdac_hdmi_init_dai_map(edev);
1290 static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1292 struct hdac_ext_device *edev = aptr;
1293 struct hdac_hdmi_priv *hdmi = edev->private_data;
1294 struct hdac_hdmi_pin *pin;
1295 struct snd_soc_codec *codec = edev->scodec;
1297 /* Don't know how this mapping is derived */
1298 hda_nid_t pin_nid = port + 0x04;
1300 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1303 * skip notification during system suspend (but not in runtime PM);
1304 * the state will be updated at resume. Also since the ELD and
1305 * connection states are updated in anyway at the end of the resume,
1306 * we can skip it when received during PM process.
1308 if (snd_power_get_state(codec->component.card->snd_card) !=
1309 SNDRV_CTL_POWER_D0)
1310 return;
1312 if (atomic_read(&edev->hdac.in_pm))
1313 return;
1315 list_for_each_entry(pin, &hdmi->pin_list, head) {
1316 if (pin->nid == pin_nid)
1317 hdac_hdmi_present_sense(pin, 1);
1321 static struct i915_audio_component_audio_ops aops = {
1322 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1325 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1327 char jack_name[NAME_SIZE];
1328 struct snd_soc_codec *codec = dai->codec;
1329 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1330 struct snd_soc_dapm_context *dapm =
1331 snd_soc_component_get_dapm(&codec->component);
1332 struct hdac_hdmi_priv *hdmi = edev->private_data;
1333 struct hdac_hdmi_pcm *pcm;
1336 * this is a new PCM device, create new pcm and
1337 * add to the pcm list
1339 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1340 if (!pcm)
1341 return -ENOMEM;
1342 pcm->pcm_id = device;
1343 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1345 list_add_tail(&pcm->head, &hdmi->pcm_list);
1347 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1349 return snd_jack_new(dapm->card->snd_card, jack_name,
1350 SND_JACK_AVOUT, &pcm->jack, true, false);
1352 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1354 static int hdmi_codec_probe(struct snd_soc_codec *codec)
1356 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1357 struct hdac_hdmi_priv *hdmi = edev->private_data;
1358 struct snd_soc_dapm_context *dapm =
1359 snd_soc_component_get_dapm(&codec->component);
1360 struct hdac_hdmi_pin *pin;
1361 int ret;
1363 edev->scodec = codec;
1365 ret = create_fill_widget_route_map(dapm);
1366 if (ret < 0)
1367 return ret;
1369 aops.audio_ptr = edev;
1370 ret = snd_hdac_i915_register_notifier(&aops);
1371 if (ret < 0) {
1372 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1373 ret);
1374 return ret;
1377 list_for_each_entry(pin, &hdmi->pin_list, head)
1378 hdac_hdmi_present_sense(pin, 1);
1380 /* Imp: Store the card pointer in hda_codec */
1381 edev->card = dapm->card->snd_card;
1384 * hdac_device core already sets the state to active and calls
1385 * get_noresume. So enable runtime and set the device to suspend.
1387 pm_runtime_enable(&edev->hdac.dev);
1388 pm_runtime_put(&edev->hdac.dev);
1389 pm_runtime_suspend(&edev->hdac.dev);
1391 return 0;
1394 static int hdmi_codec_remove(struct snd_soc_codec *codec)
1396 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1398 pm_runtime_disable(&edev->hdac.dev);
1399 return 0;
1402 static struct snd_soc_codec_driver hdmi_hda_codec = {
1403 .probe = hdmi_codec_probe,
1404 .remove = hdmi_codec_remove,
1405 .idle_bias_off = true,
1408 static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1410 struct hdac_device *codec = &edev->hdac;
1411 struct hdac_hdmi_priv *hdmi_priv;
1412 struct snd_soc_dai_driver *hdmi_dais = NULL;
1413 int num_dais = 0;
1414 int ret = 0;
1416 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1417 if (hdmi_priv == NULL)
1418 return -ENOMEM;
1420 edev->private_data = hdmi_priv;
1422 dev_set_drvdata(&codec->dev, edev);
1424 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1425 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1426 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1427 mutex_init(&hdmi_priv->pin_mutex);
1430 * Turned off in the runtime_suspend during the first explicit
1431 * pm_runtime_suspend call.
1433 ret = snd_hdac_display_power(edev->hdac.bus, true);
1434 if (ret < 0) {
1435 dev_err(&edev->hdac.dev,
1436 "Cannot turn on display power on i915 err: %d\n",
1437 ret);
1438 return ret;
1441 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1442 if (ret < 0) {
1443 dev_err(&codec->dev,
1444 "Failed in parse and map nid with err: %d\n", ret);
1445 return ret;
1448 /* ASoC specific initialization */
1449 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1450 hdmi_dais, num_dais);
1453 static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1455 struct hdac_hdmi_priv *hdmi = edev->private_data;
1456 struct hdac_hdmi_pin *pin, *pin_next;
1457 struct hdac_hdmi_cvt *cvt, *cvt_next;
1458 struct hdac_hdmi_pcm *pcm, *pcm_next;
1460 snd_soc_unregister_codec(&edev->hdac.dev);
1462 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1463 pcm->cvt = NULL;
1464 pcm->pin = NULL;
1465 list_del(&pcm->head);
1466 kfree(pcm);
1469 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1470 list_del(&cvt->head);
1471 kfree(cvt->name);
1472 kfree(cvt);
1475 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1476 list_del(&pin->head);
1477 kfree(pin);
1480 return 0;
1483 #ifdef CONFIG_PM
1484 static int hdac_hdmi_runtime_suspend(struct device *dev)
1486 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1487 struct hdac_device *hdac = &edev->hdac;
1488 struct hdac_bus *bus = hdac->bus;
1489 unsigned long timeout;
1490 int err;
1492 dev_dbg(dev, "Enter: %s\n", __func__);
1494 /* controller may not have been initialized for the first time */
1495 if (!bus)
1496 return 0;
1498 /* Power down afg */
1499 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
1500 snd_hdac_codec_write(hdac, hdac->afg, 0,
1501 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1503 /* Wait till power state is set to D3 */
1504 timeout = jiffies + msecs_to_jiffies(1000);
1505 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
1506 && time_before(jiffies, timeout)) {
1508 msleep(50);
1512 err = snd_hdac_display_power(bus, false);
1513 if (err < 0) {
1514 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1515 return err;
1518 return 0;
1521 static int hdac_hdmi_runtime_resume(struct device *dev)
1523 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1524 struct hdac_device *hdac = &edev->hdac;
1525 struct hdac_bus *bus = hdac->bus;
1526 int err;
1528 dev_dbg(dev, "Enter: %s\n", __func__);
1530 /* controller may not have been initialized for the first time */
1531 if (!bus)
1532 return 0;
1534 err = snd_hdac_display_power(bus, true);
1535 if (err < 0) {
1536 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1537 return err;
1540 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1541 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1543 /* Power up afg */
1544 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1545 snd_hdac_codec_write(hdac, hdac->afg, 0,
1546 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1548 return 0;
1550 #else
1551 #define hdac_hdmi_runtime_suspend NULL
1552 #define hdac_hdmi_runtime_resume NULL
1553 #endif
1555 static const struct dev_pm_ops hdac_hdmi_pm = {
1556 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1559 static const struct hda_device_id hdmi_list[] = {
1560 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1564 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1566 static struct hdac_ext_driver hdmi_driver = {
1567 . hdac = {
1568 .driver = {
1569 .name = "HDMI HDA Codec",
1570 .pm = &hdac_hdmi_pm,
1572 .id_table = hdmi_list,
1574 .probe = hdac_hdmi_dev_probe,
1575 .remove = hdac_hdmi_dev_remove,
1578 static int __init hdmi_init(void)
1580 return snd_hda_ext_driver_register(&hdmi_driver);
1583 static void __exit hdmi_exit(void)
1585 snd_hda_ext_driver_unregister(&hdmi_driver);
1588 module_init(hdmi_init);
1589 module_exit(hdmi_exit);
1591 MODULE_LICENSE("GPL v2");
1592 MODULE_DESCRIPTION("HDMI HD codec");
1593 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1594 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");