ASoC: hdac_hdmi: Fix to wait for D3 before powering off codec
[deliverable/linux.git] / sound / soc / codecs / hdac_hdmi.c
CommitLineData
18382ead
SP
1/*
2 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
3 *
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
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.
12 *
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.
17 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/module.h>
23#include <linux/pm_runtime.h>
a657f1d0 24#include <linux/hdmi.h>
2428bca3 25#include <drm/drm_edid.h>
18382ead 26#include <sound/pcm_params.h>
4a3478de 27#include <sound/jack.h>
18382ead
SP
28#include <sound/soc.h>
29#include <sound/hdaudio_ext.h>
07f083ab 30#include <sound/hda_i915.h>
2428bca3 31#include <sound/pcm_drm_eld.h>
18382ead 32#include "../../hda/local.h"
4a3478de 33#include "hdac_hdmi.h"
18382ead 34
17a42c45
SP
35#define NAME_SIZE 32
36
b0362adb
SP
37#define AMP_OUT_MUTE 0xb080
38#define AMP_OUT_UNMUTE 0xb000
18382ead 39#define PIN_OUT (AC_PINCTL_OUT_EN)
b0362adb 40
18382ead
SP
41#define HDA_MAX_CONNECTIONS 32
42
148569fd
SP
43#define HDA_MAX_CVTS 3
44
b8a54545
SP
45#define ELD_MAX_SIZE 256
46#define ELD_FIXED_BYTES 20
47
18382ead
SP
48struct hdac_hdmi_cvt_params {
49 unsigned int channels_min;
50 unsigned int channels_max;
51 u32 rates;
52 u64 formats;
53 unsigned int maxbps;
54};
55
56struct hdac_hdmi_cvt {
15b91447 57 struct list_head head;
18382ead 58 hda_nid_t nid;
4a3478de 59 const char *name;
18382ead
SP
60 struct hdac_hdmi_cvt_params params;
61};
62
b8a54545
SP
63struct hdac_hdmi_eld {
64 bool monitor_present;
65 bool eld_valid;
66 int eld_size;
67 char eld_buffer[ELD_MAX_SIZE];
68};
69
18382ead 70struct hdac_hdmi_pin {
15b91447 71 struct list_head head;
18382ead
SP
72 hda_nid_t nid;
73 int num_mux_nids;
74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
b8a54545
SP
75 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
77 int repoll_count;
78 struct delayed_work work;
18382ead
SP
79};
80
4a3478de
JK
81struct 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;
87};
88
18382ead
SP
89struct hdac_hdmi_dai_pin_map {
90 int dai_id;
15b91447
SP
91 struct hdac_hdmi_pin *pin;
92 struct hdac_hdmi_cvt *cvt;
18382ead
SP
93};
94
95struct hdac_hdmi_priv {
148569fd 96 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
15b91447
SP
97 struct list_head pin_list;
98 struct list_head cvt_list;
4a3478de 99 struct list_head pcm_list;
15b91447
SP
100 int num_pin;
101 int num_cvt;
4a3478de 102 struct mutex pin_mutex;
18382ead
SP
103};
104
e342ac08
SP
105static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106{
51b2c425 107 struct hdac_device *hdac = dev_to_hdac_dev(dev);
e342ac08 108
51b2c425 109 return to_ehdac_device(hdac);
e342ac08
SP
110}
111
2428bca3
SP
112static unsigned int sad_format(const u8 *sad)
113{
114 return ((sad[0] >> 0x3) & 0x1f);
115}
116
117static unsigned int sad_sample_bits_lpcm(const u8 *sad)
118{
119 return (sad[2] & 7);
120}
121
122static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
123 void *eld)
124{
125 u64 formats = SNDRV_PCM_FMTBIT_S16;
126 int i;
127 const u8 *sad, *eld_buf = eld;
128
129 sad = drm_eld_sad(eld_buf);
130 if (!sad)
131 goto format_constraint;
132
133 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
134 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
135
136 /*
137 * the controller support 20 and 24 bits in 32 bit
138 * container so we set S32
139 */
140 if (sad_sample_bits_lpcm(sad) & 0x6)
141 formats |= SNDRV_PCM_FMTBIT_S32;
142 }
143 }
144
145format_constraint:
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
147 formats);
148
149}
150
b8a54545
SP
151 /* HDMI ELD routines */
152static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153 hda_nid_t nid, int byte_index)
154{
155 unsigned int val;
156
157 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158 byte_index);
159
160 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161 byte_index, val);
162
163 return val;
164}
165
166static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
167{
168 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
169 AC_DIPSIZE_ELD_BUF);
170}
171
172/*
173 * This function queries the ELD size and ELD data and fills in the buffer
174 * passed by user
175 */
176static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177 unsigned char *buf, int *eld_size)
178{
179 int i, size, ret = 0;
180
181 /*
182 * ELD size is initialized to zero in caller function. If no errors and
183 * ELD is valid, actual eld_size is assigned.
184 */
185
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;
190 }
191
192 /* set ELD buffer */
193 for (i = 0; i < size; i++) {
194 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
195 /*
196 * Graphics driver might be writing to ELD buffer right now.
197 * Just abort. The caller will repoll after a while.
198 */
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;
204 }
205 val &= AC_ELDD_ELD_DATA;
206 /*
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.
211 */
212 if (!val && !i) {
213 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214 ret = -EINVAL;
215 goto error;
216 }
217 buf[i] = val;
218 }
219
220 *eld_size = size;
221error:
222 return ret;
223}
224
b0362adb
SP
225static 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)
228{
229 unsigned int val;
230
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);
233
234 val = (stream_tag << 4);
235
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);
240
241 return 0;
242}
243
a657f1d0
SP
244static void
245hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246 int packet_index, int byte_index)
247{
248 int val;
249
250 val = (packet_index << 5) | (byte_index & 0x1f);
251
252 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
253 AC_VERB_SET_HDMI_DIP_INDEX, val);
254}
255
478f544e
SP
256struct dp_audio_infoframe {
257 u8 type; /* 0x84 */
258 u8 len; /* 0x1b */
259 u8 ver; /* 0x11 << 2 */
260
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;
266};
267
a657f1d0
SP
268static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
269 hda_nid_t cvt_nid, hda_nid_t pin_nid)
270{
271 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
272 struct hdmi_audio_infoframe frame;
478f544e
SP
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;
a657f1d0
SP
277 int ret;
278 int i;
478f544e
SP
279 const u8 *eld_buf;
280 u8 conn_type;
281 int channels = 2;
a657f1d0 282
478f544e
SP
283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
285 break;
286 }
a657f1d0 287
478f544e
SP
288 eld_buf = pin->eld.eld_buffer;
289 conn_type = drm_eld_get_conn_type(eld_buf);
a657f1d0
SP
290
291 /* setup channel count */
292 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
478f544e
SP
293 AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
294
295 switch (conn_type) {
296 case DRM_ELD_CONN_TYPE_HDMI:
297 hdmi_audio_infoframe_init(&frame);
298
299 /* Default stereo for now */
300 frame.channels = channels;
301
302 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
303 if (ret < 0)
304 return ret;
a657f1d0 305
478f544e
SP
306 dip = (u8 *)&frame;
307 break;
308
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;
316
317 dip = (u8 *)&dp_ai;
318 break;
319
320 default:
321 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
322 conn_type);
323 return -EIO;
324 }
a657f1d0
SP
325
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);
330
331
332 /* Fill infoframe. Index auto-incremented */
333 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
478f544e
SP
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,
a657f1d0 341 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
478f544e 342 }
a657f1d0
SP
343
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);
348
349 return 0;
350}
351
b0362adb
SP
352static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
353 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
354{
355 /* Power up pin widget */
15b91447
SP
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,
b0362adb
SP
359 AC_VERB_SET_POWER_STATE, pwr_state);
360
361 /* Power up converter */
15b91447
SP
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,
b0362adb
SP
365 AC_VERB_SET_POWER_STATE, pwr_state);
366}
367
368static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
369 struct snd_soc_dai *dai)
370{
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;
a657f1d0 375 int ret;
b0362adb 376
b0362adb
SP
377 dai_map = &hdmi->dai_map[dai->id];
378
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);
382
15b91447
SP
383 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
384 dai_map->pin->nid);
a657f1d0
SP
385 if (ret < 0)
386 return ret;
387
15b91447
SP
388 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
389 dai_map->pin->nid, dd->stream_tag, dd->format);
b0362adb
SP
390}
391
392static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
393 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
394{
395 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
54dfa1ea
SP
396 struct hdac_hdmi_priv *hdmi = hdac->private_data;
397 struct hdac_hdmi_dai_pin_map *dai_map;
398 struct hdac_hdmi_pin *pin;
b0362adb
SP
399 struct hdac_ext_dma_params *dd;
400
54dfa1ea
SP
401 dai_map = &hdmi->dai_map[dai->id];
402 pin = dai_map->pin;
403
404 if (!pin)
405 return -ENODEV;
406
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);
b0362adb
SP
410 return -ENODEV;
411 }
412
6793a3d7
SP
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;
418 }
419
b0362adb
SP
420 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
421 params_channels(hparams), params_format(hparams),
422 24, 0);
423
424 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
425
426 return 0;
427}
428
429static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
430 struct snd_soc_dai *dai)
431{
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;
436
437 dai_map = &hdmi->dai_map[dai->id];
438
b0362adb 439 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
b0362adb 440
6793a3d7
SP
441 if (dd) {
442 snd_soc_dai_set_dma_data(dai, substream, NULL);
443 kfree(dd);
444 }
b0362adb
SP
445
446 return 0;
447}
448
ab85f5b3
SP
449static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
450 struct hdac_hdmi_dai_pin_map *dai_map)
451{
452 /* Enable transmission */
453 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
454 AC_VERB_SET_DIGI_CONVERT_1, 1);
455
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);
459}
460
148569fd
SP
461static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
462 struct hdac_hdmi_dai_pin_map *dai_map)
463{
464 int mux_idx;
465 struct hdac_hdmi_pin *pin = dai_map->pin;
466
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;
472 }
473 }
474
475 if (mux_idx == pin->num_mux_nids)
476 return -EIO;
477
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);
481
482 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
483
484 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
485 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
486
487 return 0;
488}
489
490static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
491 struct hdac_hdmi_pin *pin)
492{
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;
498 }
499
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);
505
506 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
507 pin->num_mux_nids, pin->nid);
508
509 return pin->num_mux_nids;
510}
511
512/*
513 * Query pcm list and return pin widget to which stream is routed.
514 *
515 * Also query connection list of the pin, to validate the cvt to pin map.
516 *
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.
520 */
521static 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)
525{
526 struct hdac_hdmi_pcm *pcm;
527 struct hdac_hdmi_pin *pin = NULL;
528 int ret, i;
529
530 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
531 if (pcm->cvt == cvt) {
532 pin = pcm->pin;
533 break;
534 }
535 }
536
537 if (pin) {
538 ret = hdac_hdmi_query_pin_connlist(edev, pin);
539 if (ret < 0)
540 return NULL;
541
542 for (i = 0; i < pin->num_mux_nids; i++) {
543 if (pin->mux_nids[i] == cvt->nid)
544 return pin;
545 }
546 }
547
548 return NULL;
549}
550
54dfa1ea
SP
551/*
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.
555 */
b0362adb
SP
556static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
557 struct snd_soc_dai *dai)
558{
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;
148569fd
SP
562 struct hdac_hdmi_cvt *cvt;
563 struct hdac_hdmi_pin *pin;
2428bca3 564 int ret;
b0362adb 565
b0362adb
SP
566 dai_map = &hdmi->dai_map[dai->id];
567
148569fd
SP
568 cvt = dai_map->cvt;
569 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
54dfa1ea
SP
570
571 /*
572 * To make PA and other userland happy.
573 * userland scans devices so returning error does not help.
574 */
148569fd 575 if (!pin)
54dfa1ea 576 return 0;
148569fd
SP
577
578 if ((!pin->eld.monitor_present) ||
579 (!pin->eld.eld_valid)) {
b8a54545 580
54dfa1ea 581 dev_warn(&hdac->hdac.dev,
148569fd
SP
582 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
583 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
b0362adb 584
54dfa1ea 585 return 0;
b0362adb
SP
586 }
587
148569fd 588 dai_map->pin = pin;
b0362adb 589
ab85f5b3 590 hdac_hdmi_enable_cvt(hdac, dai_map);
148569fd
SP
591 ret = hdac_hdmi_enable_pin(hdac, dai_map);
592 if (ret < 0)
593 return ret;
b0362adb 594
2428bca3 595 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
148569fd 596 pin->eld.eld_buffer);
2428bca3
SP
597 if (ret < 0)
598 return ret;
b0362adb 599
2428bca3 600 return snd_pcm_hw_constraint_eld(substream->runtime,
148569fd 601 pin->eld.eld_buffer);
b0362adb
SP
602}
603
604static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
605 struct snd_soc_dai *dai)
606{
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;
610
611 dai_map = &hdmi->dai_map[dai->id];
612
54dfa1ea
SP
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);
618
619 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
b0362adb 620
54dfa1ea 621 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
b0362adb 622 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
148569fd 623
54dfa1ea
SP
624 dai_map->pin = NULL;
625 }
b0362adb
SP
626}
627
18382ead
SP
628static int
629hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
630{
631 int err;
632
633 /* Only stereo supported as of now */
634 cvt->params.channels_min = cvt->params.channels_max = 2;
635
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);
644
645 return err;
646}
647
79f4e922
SP
648static 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)
18382ead
SP
653{
654 w->id = id;
79f4e922
SP
655 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
656 if (!w->name)
657 return -ENOMEM;
658
18382ead
SP
659 w->sname = stream;
660 w->reg = SND_SOC_NOPM;
661 w->shift = 0;
79f4e922
SP
662 w->kcontrol_news = wc;
663 w->num_kcontrols = numkc;
664 w->priv = priv;
665
666 return 0;
18382ead
SP
667}
668
669static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
79f4e922
SP
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))
18382ead
SP
673{
674 route->sink = sink;
675 route->source = src;
676 route->control = control;
79f4e922 677 route->connected = handler;
18382ead
SP
678}
679
4a3478de
JK
680static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
681 struct hdac_hdmi_pin *pin)
682{
683 struct hdac_hdmi_priv *hdmi = edev->private_data;
684 struct hdac_hdmi_pcm *pcm = NULL;
685
686 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
687 if (pcm->pin == pin)
688 return pcm;
689 }
690
691 return NULL;
692}
693
694/*
695 * Based on user selection, map the PINs with the PCMs.
696 */
697static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
698 struct snd_ctl_elem_value *ucontrol)
699{
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]];
709
710 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
711 if (ret < 0)
712 return ret;
713
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;
718
719 /*
720 * Jack status is not reported during device probe as the
721 * PCMs are not registered by then. So report it here.
722 */
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);
729
730 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
731 }
732 mutex_unlock(&hdmi->pin_mutex);
733 return ret;
734 }
735 }
736 mutex_unlock(&hdmi->pin_mutex);
737
738 return ret;
739}
740
79f4e922
SP
741/*
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.
745 *
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"
749 */
750static 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)
754{
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;
765
766 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
767 if (!kc)
768 return -ENOMEM;
769
770 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
771 if (!se)
772 return -ENOMEM;
773
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;
778
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;
4a3478de 783 kc->put = hdac_hdmi_set_pin_mux;
79f4e922
SP
784 kc->get = snd_soc_dapm_get_enum_double;
785
786 se->reg = SND_SOC_NOPM;
787
788 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
789 se->items = num_items;
790 se->mask = roundup_pow_of_two(se->items) - 1;
791
792 sprintf(mux_items, "NONE");
793 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
794 if (!items[i])
795 return -ENOMEM;
796
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;
803 }
804
805 se->texts = devm_kmemdup(&edev->hdac.dev, items,
806 (num_items * sizeof(char *)), GFP_KERNEL);
807 if (!se->texts)
808 return -ENOMEM;
809
810 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
4a3478de 811 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
79f4e922
SP
812}
813
814/* Add cvt <- input <- mux route map */
815static 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)
818{
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;
824
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);
833
834 rindex++;
835 }
836
837 mux_index++;
838 }
839}
840
841/*
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
846 *
847 * Total widgets elements = num_cvt + num_pin + num_pin;
848 *
849 * Routes are added as below:
850 * pin mux -> pin (based on num_pins)
851 * cvt -> "Input sel control" -> pin_mux
852 *
853 * Total route elements:
854 * num_pins + (pin_muxes * num_cvt)
855 */
856static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
18382ead 857{
79f4e922
SP
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;
18382ead 867
79f4e922
SP
868 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
869 return -EINVAL;
18382ead 870
79f4e922
SP
871 widgets = devm_kzalloc(dapm->dev,
872 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
873 GFP_KERNEL);
18382ead 874
79f4e922
SP
875 if (!widgets)
876 return -ENOMEM;
877
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++;
887 }
888
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++;
897 }
898
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++;
907
908 /* For cvt to pin_mux mapping */
909 num_routes += hdmi->num_cvt;
910
911 /* For pin_mux to pin mapping */
912 num_routes++;
913 }
914
915 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
916 GFP_KERNEL);
917 if (!route)
918 return -ENOMEM;
919
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;
925
926 hdac_hdmi_fill_route(&route[i],
927 widgets[sink_index].name, NULL,
928 widgets[src_index].name, NULL);
929 i++;
930
931 }
932
933 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
934
935 snd_soc_dapm_new_controls(dapm, widgets,
936 ((2 * hdmi->num_pin) + hdmi->num_cvt));
937
938 snd_soc_dapm_add_routes(dapm, route, num_routes);
939 snd_soc_dapm_new_widgets(dapm->card);
940
941 return 0;
18382ead 942
18382ead
SP
943}
944
15b91447 945static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
18382ead 946{
15b91447 947 struct hdac_hdmi_priv *hdmi = edev->private_data;
148569fd 948 struct hdac_hdmi_dai_pin_map *dai_map;
15b91447 949 struct hdac_hdmi_cvt *cvt;
148569fd 950 int dai_id = 0;
18382ead 951
148569fd 952 if (list_empty(&hdmi->cvt_list))
15b91447 953 return -EINVAL;
18382ead 954
148569fd
SP
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;
18382ead 959
148569fd 960 dai_id++;
18382ead 961
148569fd
SP
962 if (dai_id == HDA_MAX_CVTS) {
963 dev_warn(&edev->hdac.dev,
964 "Max dais supported: %d\n", dai_id);
965 break;
966 }
967 }
18382ead 968
15b91447
SP
969 return 0;
970}
971
972static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
973{
974 struct hdac_hdmi_priv *hdmi = edev->private_data;
975 struct hdac_hdmi_cvt *cvt;
4a3478de 976 char name[NAME_SIZE];
15b91447
SP
977
978 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
979 if (!cvt)
980 return -ENOMEM;
981
982 cvt->nid = nid;
4a3478de
JK
983 sprintf(name, "cvt %d", cvt->nid);
984 cvt->name = kstrdup(name, GFP_KERNEL);
15b91447
SP
985
986 list_add_tail(&cvt->head, &hdmi->cvt_list);
987 hdmi->num_cvt++;
988
989 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
990}
991
b8a54545
SP
992static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
993{
994 struct hdac_ext_device *edev = pin->edev;
4a3478de
JK
995 struct hdac_hdmi_priv *hdmi = edev->private_data;
996 struct hdac_hdmi_pcm *pcm;
b8a54545
SP
997 int val;
998
b8a54545
SP
999 pin->repoll_count = repoll;
1000
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);
1004
1005 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1006 val, pin->nid);
1007
4a3478de
JK
1008
1009 mutex_lock(&hdmi->pin_mutex);
b8a54545
SP
1010 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1011 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1012
4a3478de
JK
1013 pcm = hdac_hdmi_get_pcm(edev, pin);
1014
b8a54545
SP
1015 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1016
1017 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1018 __func__, pin->nid);
4a3478de
JK
1019
1020 /*
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.
1024 */
1025 if (pcm) {
1026 dev_dbg(&edev->hdac.dev,
1027 "jack report for pcm=%d\n", pcm->pcm_id);
1028
1029 snd_jack_report(pcm->jack, 0);
1030 }
1031
1032 mutex_unlock(&hdmi->pin_mutex);
b8a54545
SP
1033 goto put_hdac_device;
1034 }
1035
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) {
1041
4a3478de
JK
1042 if (pcm) {
1043 dev_dbg(&edev->hdac.dev,
1044 "jack report for pcm=%d\n",
1045 pcm->pcm_id);
1046
1047 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1048 }
1049
b8a54545
SP
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;
4a3478de
JK
1055
1056 if (pcm) {
1057 dev_dbg(&edev->hdac.dev,
1058 "jack report for pcm=%d\n",
1059 pcm->pcm_id);
1060
1061 snd_jack_report(pcm->jack, 0);
1062 }
b8a54545
SP
1063 }
1064 }
1065
4a3478de
JK
1066 mutex_unlock(&hdmi->pin_mutex);
1067
b8a54545
SP
1068 /*
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.
1072 */
1073 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1074 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1075
1076put_hdac_device:
1077 pm_runtime_put_sync(&edev->hdac.dev);
1078}
1079
1080static void hdac_hdmi_repoll_eld(struct work_struct *work)
1081{
1082 struct hdac_hdmi_pin *pin =
1083 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1084
1085 /* picked from legacy HDA driver */
1086 if (pin->repoll_count++ > 6)
1087 pin->repoll_count = 0;
1088
1089 hdac_hdmi_present_sense(pin, pin->repoll_count);
1090}
1091
15b91447
SP
1092static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1093{
1094 struct hdac_hdmi_priv *hdmi = edev->private_data;
1095 struct hdac_hdmi_pin *pin;
1096
1097 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1098 if (!pin)
1099 return -ENOMEM;
1100
1101 pin->nid = nid;
1102
1103 list_add_tail(&pin->head, &hdmi->pin_list);
1104 hdmi->num_pin++;
1105
b8a54545
SP
1106 pin->edev = edev;
1107 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1108
15b91447 1109 return 0;
18382ead
SP
1110}
1111
211caab7
SP
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 */
1117
1118static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1119{
1120 unsigned int vendor_param;
1121
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;
1126
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;
1132}
1133
1134static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1135{
1136 unsigned int vendor_param;
1137
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;
1142
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;
1149
1150}
1151
17a42c45
SP
1152static 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,
1158};
1159
1160/*
1161 * Each converter can support a stream independently. So a dai is created
1162 * based on the number of converter queried.
1163 */
1164static 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)
1167{
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;
1176
1177 hdmi_dais = devm_kzalloc(&hdac->dev,
1178 (sizeof(*hdmi_dais) * num_dais),
1179 GFP_KERNEL);
1180 if (!hdmi_dais)
1181 return -ENOMEM;
1182
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;
1188
1189 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1190 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1191 dai_name, GFP_KERNEL);
1192
1193 if (!hdmi_dais[i].name)
1194 return -ENOMEM;
1195
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;
1201
1202 /*
1203 * Set caps based on capability queried from the converter.
1204 * It will be constrained runtime based on ELD queried.
1205 */
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;
1213
1214 i++;
1215 }
1216
1217 *dais = hdmi_dais;
1218
1219 return 0;
1220}
1221
18382ead
SP
1222/*
1223 * Parse all nodes and store the cvt/pin nids in array
1224 * Add one time initialization for pin and cvt widgets
1225 */
17a42c45
SP
1226static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1227 struct snd_soc_dai_driver **dais, int *num_dais)
18382ead
SP
1228{
1229 hda_nid_t nid;
3c83ac23 1230 int i, num_nodes;
18382ead
SP
1231 struct hdac_device *hdac = &edev->hdac;
1232 struct hdac_hdmi_priv *hdmi = edev->private_data;
15b91447 1233 int ret;
18382ead 1234
211caab7
SP
1235 hdac_hdmi_skl_enable_all_pins(hdac);
1236 hdac_hdmi_skl_enable_dp12(hdac);
1237
3c83ac23 1238 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
541140d4 1239 if (!nid || num_nodes <= 0) {
18382ead
SP
1240 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1241 return -EINVAL;
1242 }
1243
3c83ac23 1244 hdac->num_nodes = num_nodes;
18382ead
SP
1245 hdac->start_nid = nid;
1246
1247 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1248 unsigned int caps;
1249 unsigned int type;
1250
1251 caps = get_wcaps(hdac, nid);
1252 type = get_wcaps_type(caps);
1253
1254 if (!(caps & AC_WCAP_DIGITAL))
1255 continue;
1256
1257 switch (type) {
1258
1259 case AC_WID_AUD_OUT:
15b91447
SP
1260 ret = hdac_hdmi_add_cvt(edev, nid);
1261 if (ret < 0)
1262 return ret;
18382ead
SP
1263 break;
1264
1265 case AC_WID_PIN:
15b91447
SP
1266 ret = hdac_hdmi_add_pin(edev, nid);
1267 if (ret < 0)
1268 return ret;
18382ead
SP
1269 break;
1270 }
1271 }
1272
1273 hdac->end_nid = nid;
1274
15b91447 1275 if (!hdmi->num_pin || !hdmi->num_cvt)
18382ead
SP
1276 return -EIO;
1277
17a42c45
SP
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;
1283 }
1284
1285 *num_dais = hdmi->num_cvt;
1286
15b91447 1287 return hdac_hdmi_init_dai_map(edev);
18382ead
SP
1288}
1289
b8a54545
SP
1290static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1291{
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;
1296
1297 /* Don't know how this mapping is derived */
1298 hda_nid_t pin_nid = port + 0x04;
1299
1300 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1301
1302 /*
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.
1307 */
1308 if (snd_power_get_state(codec->component.card->snd_card) !=
1309 SNDRV_CTL_POWER_D0)
1310 return;
1311
1312 if (atomic_read(&edev->hdac.in_pm))
1313 return;
1314
1315 list_for_each_entry(pin, &hdmi->pin_list, head) {
1316 if (pin->nid == pin_nid)
1317 hdac_hdmi_present_sense(pin, 1);
1318 }
1319}
1320
1321static struct i915_audio_component_audio_ops aops = {
1322 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1323};
1324
4a3478de
JK
1325int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1326{
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;
1334
1335 /*
1336 * this is a new PCM device, create new pcm and
1337 * add to the pcm list
1338 */
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;
1344
1345 list_add_tail(&pcm->head, &hdmi->pcm_list);
1346
1347 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1348
1349 return snd_jack_new(dapm->card->snd_card, jack_name,
1350 SND_JACK_AVOUT, &pcm->jack, true, false);
1351}
1352EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1353
18382ead
SP
1354static int hdmi_codec_probe(struct snd_soc_codec *codec)
1355{
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);
b8a54545
SP
1360 struct hdac_hdmi_pin *pin;
1361 int ret;
18382ead
SP
1362
1363 edev->scodec = codec;
1364
79f4e922
SP
1365 ret = create_fill_widget_route_map(dapm);
1366 if (ret < 0)
1367 return ret;
18382ead 1368
b8a54545
SP
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;
1375 }
1376
1377 list_for_each_entry(pin, &hdmi->pin_list, head)
1378 hdac_hdmi_present_sense(pin, 1);
1379
18382ead
SP
1380 /* Imp: Store the card pointer in hda_codec */
1381 edev->card = dapm->card->snd_card;
1382
e342ac08
SP
1383 /*
1384 * hdac_device core already sets the state to active and calls
1385 * get_noresume. So enable runtime and set the device to suspend.
1386 */
1387 pm_runtime_enable(&edev->hdac.dev);
1388 pm_runtime_put(&edev->hdac.dev);
1389 pm_runtime_suspend(&edev->hdac.dev);
1390
1391 return 0;
1392}
1393
1394static int hdmi_codec_remove(struct snd_soc_codec *codec)
1395{
1396 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1397
1398 pm_runtime_disable(&edev->hdac.dev);
18382ead
SP
1399 return 0;
1400}
1401
1402static struct snd_soc_codec_driver hdmi_hda_codec = {
1403 .probe = hdmi_codec_probe,
e342ac08 1404 .remove = hdmi_codec_remove,
18382ead
SP
1405 .idle_bias_off = true,
1406};
1407
18382ead
SP
1408static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1409{
1410 struct hdac_device *codec = &edev->hdac;
1411 struct hdac_hdmi_priv *hdmi_priv;
17a42c45
SP
1412 struct snd_soc_dai_driver *hdmi_dais = NULL;
1413 int num_dais = 0;
18382ead
SP
1414 int ret = 0;
1415
1416 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1417 if (hdmi_priv == NULL)
1418 return -ENOMEM;
1419
1420 edev->private_data = hdmi_priv;
1421
1422 dev_set_drvdata(&codec->dev, edev);
1423
15b91447
SP
1424 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1425 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
4a3478de
JK
1426 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1427 mutex_init(&hdmi_priv->pin_mutex);
15b91447 1428
aeaccef0
RB
1429 /*
1430 * Turned off in the runtime_suspend during the first explicit
1431 * pm_runtime_suspend call.
1432 */
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;
1439 }
1440
17a42c45
SP
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);
18382ead 1445 return ret;
17a42c45 1446 }
18382ead
SP
1447
1448 /* ASoC specific initialization */
1449 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
17a42c45 1450 hdmi_dais, num_dais);
18382ead
SP
1451}
1452
1453static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1454{
15b91447
SP
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;
4a3478de 1458 struct hdac_hdmi_pcm *pcm, *pcm_next;
15b91447 1459
18382ead
SP
1460 snd_soc_unregister_codec(&edev->hdac.dev);
1461
4a3478de
JK
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);
1467 }
1468
15b91447
SP
1469 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1470 list_del(&cvt->head);
4a3478de 1471 kfree(cvt->name);
15b91447
SP
1472 kfree(cvt);
1473 }
1474
1475 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1476 list_del(&pin->head);
1477 kfree(pin);
1478 }
1479
18382ead
SP
1480 return 0;
1481}
1482
e342ac08
SP
1483#ifdef CONFIG_PM
1484static int hdac_hdmi_runtime_suspend(struct device *dev)
1485{
1486 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1487 struct hdac_device *hdac = &edev->hdac;
07f083ab 1488 struct hdac_bus *bus = hdac->bus;
7ed49700 1489 unsigned long timeout;
07f083ab 1490 int err;
e342ac08
SP
1491
1492 dev_dbg(dev, "Enter: %s\n", __func__);
1493
07f083ab
SP
1494 /* controller may not have been initialized for the first time */
1495 if (!bus)
1496 return 0;
1497
e342ac08 1498 /* Power down afg */
7ed49700 1499 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
e342ac08
SP
1500 snd_hdac_codec_write(hdac, hdac->afg, 0,
1501 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1502
7ed49700
SP
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)) {
1507
1508 msleep(50);
1509 }
1510 }
1511
07f083ab
SP
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;
1516 }
1517
e342ac08
SP
1518 return 0;
1519}
1520
1521static int hdac_hdmi_runtime_resume(struct device *dev)
1522{
1523 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1524 struct hdac_device *hdac = &edev->hdac;
07f083ab
SP
1525 struct hdac_bus *bus = hdac->bus;
1526 int err;
e342ac08
SP
1527
1528 dev_dbg(dev, "Enter: %s\n", __func__);
1529
07f083ab
SP
1530 /* controller may not have been initialized for the first time */
1531 if (!bus)
1532 return 0;
1533
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;
1538 }
1539
ab85f5b3
SP
1540 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1541 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1542
e342ac08
SP
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);
1547
1548 return 0;
1549}
1550#else
1551#define hdac_hdmi_runtime_suspend NULL
1552#define hdac_hdmi_runtime_resume NULL
1553#endif
1554
1555static const struct dev_pm_ops hdac_hdmi_pm = {
1556 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1557};
1558
18382ead
SP
1559static const struct hda_device_id hdmi_list[] = {
1560 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1561 {}
1562};
1563
1564MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1565
1566static struct hdac_ext_driver hdmi_driver = {
1567 . hdac = {
1568 .driver = {
1569 .name = "HDMI HDA Codec",
e342ac08 1570 .pm = &hdac_hdmi_pm,
18382ead
SP
1571 },
1572 .id_table = hdmi_list,
1573 },
1574 .probe = hdac_hdmi_dev_probe,
1575 .remove = hdac_hdmi_dev_remove,
1576};
1577
1578static int __init hdmi_init(void)
1579{
1580 return snd_hda_ext_driver_register(&hdmi_driver);
1581}
1582
1583static void __exit hdmi_exit(void)
1584{
1585 snd_hda_ext_driver_unregister(&hdmi_driver);
1586}
1587
1588module_init(hdmi_init);
1589module_exit(hdmi_exit);
1590
1591MODULE_LICENSE("GPL v2");
1592MODULE_DESCRIPTION("HDMI HD codec");
1593MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1594MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
This page took 0.169956 seconds and 5 git commands to generate.