ASoC: Intel: boards: Update skl_nau88l25_ssm4567 driver to support chmap
[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>
bcced704 32#include <sound/hda_chmap.h>
18382ead 33#include "../../hda/local.h"
4a3478de 34#include "hdac_hdmi.h"
18382ead 35
17a42c45
SP
36#define NAME_SIZE 32
37
b0362adb
SP
38#define AMP_OUT_MUTE 0xb080
39#define AMP_OUT_UNMUTE 0xb000
18382ead 40#define PIN_OUT (AC_PINCTL_OUT_EN)
b0362adb 41
18382ead
SP
42#define HDA_MAX_CONNECTIONS 32
43
148569fd
SP
44#define HDA_MAX_CVTS 3
45
b8a54545
SP
46#define ELD_MAX_SIZE 256
47#define ELD_FIXED_BYTES 20
48
18382ead
SP
49struct hdac_hdmi_cvt_params {
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
56
57struct hdac_hdmi_cvt {
15b91447 58 struct list_head head;
18382ead 59 hda_nid_t nid;
4a3478de 60 const char *name;
18382ead
SP
61 struct hdac_hdmi_cvt_params params;
62};
63
b7756ede
SP
64/* Currently only spk_alloc, more to be added */
65struct hdac_hdmi_parsed_eld {
66 u8 spk_alloc;
67};
68
b8a54545
SP
69struct hdac_hdmi_eld {
70 bool monitor_present;
71 bool eld_valid;
72 int eld_size;
73 char eld_buffer[ELD_MAX_SIZE];
b7756ede 74 struct hdac_hdmi_parsed_eld info;
b8a54545
SP
75};
76
18382ead 77struct hdac_hdmi_pin {
15b91447 78 struct list_head head;
18382ead
SP
79 hda_nid_t nid;
80 int num_mux_nids;
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
b8a54545
SP
82 struct hdac_hdmi_eld eld;
83 struct hdac_ext_device *edev;
84 int repoll_count;
85 struct delayed_work work;
bcced704
SP
86 struct mutex lock;
87 bool chmap_set;
88 unsigned char chmap[8]; /* ALSA API channel-map */
89 int channels; /* current number of channels */
18382ead
SP
90};
91
4a3478de
JK
92struct hdac_hdmi_pcm {
93 struct list_head head;
94 int pcm_id;
95 struct hdac_hdmi_pin *pin;
96 struct hdac_hdmi_cvt *cvt;
97 struct snd_jack *jack;
98};
99
18382ead
SP
100struct hdac_hdmi_dai_pin_map {
101 int dai_id;
15b91447
SP
102 struct hdac_hdmi_pin *pin;
103 struct hdac_hdmi_cvt *cvt;
18382ead
SP
104};
105
106struct hdac_hdmi_priv {
148569fd 107 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
15b91447
SP
108 struct list_head pin_list;
109 struct list_head cvt_list;
4a3478de 110 struct list_head pcm_list;
15b91447
SP
111 int num_pin;
112 int num_cvt;
4a3478de 113 struct mutex pin_mutex;
bcced704 114 struct hdac_chmap chmap;
18382ead
SP
115};
116
e342ac08
SP
117static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
118{
51b2c425 119 struct hdac_device *hdac = dev_to_hdac_dev(dev);
e342ac08 120
51b2c425 121 return to_ehdac_device(hdac);
e342ac08
SP
122}
123
2428bca3
SP
124static unsigned int sad_format(const u8 *sad)
125{
126 return ((sad[0] >> 0x3) & 0x1f);
127}
128
129static unsigned int sad_sample_bits_lpcm(const u8 *sad)
130{
131 return (sad[2] & 7);
132}
133
134static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
135 void *eld)
136{
137 u64 formats = SNDRV_PCM_FMTBIT_S16;
138 int i;
139 const u8 *sad, *eld_buf = eld;
140
141 sad = drm_eld_sad(eld_buf);
142 if (!sad)
143 goto format_constraint;
144
145 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
146 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
147
148 /*
149 * the controller support 20 and 24 bits in 32 bit
150 * container so we set S32
151 */
152 if (sad_sample_bits_lpcm(sad) & 0x6)
153 formats |= SNDRV_PCM_FMTBIT_S32;
154 }
155 }
156
157format_constraint:
158 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
159 formats);
160
161}
162
b8a54545
SP
163 /* HDMI ELD routines */
164static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
165 hda_nid_t nid, int byte_index)
166{
167 unsigned int val;
168
169 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
170 byte_index);
171
172 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
173 byte_index, val);
174
175 return val;
176}
177
178static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
179{
180 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
181 AC_DIPSIZE_ELD_BUF);
182}
183
184/*
185 * This function queries the ELD size and ELD data and fills in the buffer
186 * passed by user
187 */
188static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
189 unsigned char *buf, int *eld_size)
190{
191 int i, size, ret = 0;
192
193 /*
194 * ELD size is initialized to zero in caller function. If no errors and
195 * ELD is valid, actual eld_size is assigned.
196 */
197
198 size = hdac_hdmi_get_eld_size(codec, nid);
199 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
200 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
201 return -ERANGE;
202 }
203
204 /* set ELD buffer */
205 for (i = 0; i < size; i++) {
206 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
207 /*
208 * Graphics driver might be writing to ELD buffer right now.
209 * Just abort. The caller will repoll after a while.
210 */
211 if (!(val & AC_ELDD_ELD_VALID)) {
212 dev_err(&codec->dev,
213 "HDMI: invalid ELD data byte %d\n", i);
214 ret = -EINVAL;
215 goto error;
216 }
217 val &= AC_ELDD_ELD_DATA;
218 /*
219 * The first byte cannot be zero. This can happen on some DVI
220 * connections. Some Intel chips may also need some 250ms delay
221 * to return non-zero ELD data, even when the graphics driver
222 * correctly writes ELD content before setting ELD_valid bit.
223 */
224 if (!val && !i) {
225 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
226 ret = -EINVAL;
227 goto error;
228 }
229 buf[i] = val;
230 }
231
232 *eld_size = size;
233error:
234 return ret;
235}
236
b0362adb
SP
237static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
238 hda_nid_t cvt_nid, hda_nid_t pin_nid,
239 u32 stream_tag, int format)
240{
241 unsigned int val;
242
243 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
244 cvt_nid, pin_nid, stream_tag, format);
245
246 val = (stream_tag << 4);
247
248 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
249 AC_VERB_SET_CHANNEL_STREAMID, val);
250 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
251 AC_VERB_SET_STREAM_FORMAT, format);
252
253 return 0;
254}
255
a657f1d0
SP
256static void
257hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
258 int packet_index, int byte_index)
259{
260 int val;
261
262 val = (packet_index << 5) | (byte_index & 0x1f);
263
264 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
265 AC_VERB_SET_HDMI_DIP_INDEX, val);
266}
267
478f544e
SP
268struct dp_audio_infoframe {
269 u8 type; /* 0x84 */
270 u8 len; /* 0x1b */
271 u8 ver; /* 0x11 << 2 */
272
273 u8 CC02_CT47; /* match with HDMI infoframe from this on */
274 u8 SS01_SF24;
275 u8 CXT04;
276 u8 CA;
277 u8 LFEPBL01_LSV36_DM_INH7;
278};
279
a657f1d0
SP
280static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
281 hda_nid_t cvt_nid, hda_nid_t pin_nid)
282{
283 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
284 struct hdmi_audio_infoframe frame;
478f544e
SP
285 struct dp_audio_infoframe dp_ai;
286 struct hdac_hdmi_priv *hdmi = hdac->private_data;
287 struct hdac_hdmi_pin *pin;
288 u8 *dip;
a657f1d0
SP
289 int ret;
290 int i;
478f544e
SP
291 const u8 *eld_buf;
292 u8 conn_type;
bcced704 293 int channels, ca;
a657f1d0 294
478f544e
SP
295 list_for_each_entry(pin, &hdmi->pin_list, head) {
296 if (pin->nid == pin_nid)
297 break;
298 }
a657f1d0 299
bcced704
SP
300 ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
301 pin->channels, pin->chmap_set, true, pin->chmap);
302
303 channels = snd_hdac_get_active_channels(ca);
304 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
305
306 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
307 pin->channels, pin->chmap, pin->chmap_set);
308
478f544e
SP
309 eld_buf = pin->eld.eld_buffer;
310 conn_type = drm_eld_get_conn_type(eld_buf);
a657f1d0 311
478f544e
SP
312 switch (conn_type) {
313 case DRM_ELD_CONN_TYPE_HDMI:
314 hdmi_audio_infoframe_init(&frame);
315
478f544e 316 frame.channels = channels;
bcced704 317 frame.channel_allocation = ca;
478f544e
SP
318
319 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
320 if (ret < 0)
321 return ret;
a657f1d0 322
478f544e
SP
323 break;
324
325 case DRM_ELD_CONN_TYPE_DP:
326 memset(&dp_ai, 0, sizeof(dp_ai));
327 dp_ai.type = 0x84;
328 dp_ai.len = 0x1b;
329 dp_ai.ver = 0x11 << 2;
330 dp_ai.CC02_CT47 = channels - 1;
bcced704 331 dp_ai.CA = ca;
478f544e
SP
332
333 dip = (u8 *)&dp_ai;
334 break;
335
336 default:
337 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
338 conn_type);
339 return -EIO;
340 }
a657f1d0
SP
341
342 /* stop infoframe transmission */
343 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
344 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
345 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
346
347
348 /* Fill infoframe. Index auto-incremented */
349 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
478f544e 350 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
391005e8 351 for (i = 0; i < sizeof(buffer); i++)
478f544e 352 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
391005e8 353 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
478f544e
SP
354 } else {
355 for (i = 0; i < sizeof(dp_ai); i++)
356 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
a657f1d0 357 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
478f544e 358 }
a657f1d0
SP
359
360 /* Start infoframe */
361 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
362 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
363 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
364
365 return 0;
366}
367
b0362adb
SP
368static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
369 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
370{
371 /* Power up pin widget */
15b91447
SP
372 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
373 pwr_state))
374 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
b0362adb
SP
375 AC_VERB_SET_POWER_STATE, pwr_state);
376
377 /* Power up converter */
15b91447
SP
378 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
379 pwr_state))
380 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
b0362adb
SP
381 AC_VERB_SET_POWER_STATE, pwr_state);
382}
383
384static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
385 struct snd_soc_dai *dai)
386{
387 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
388 struct hdac_hdmi_priv *hdmi = hdac->private_data;
389 struct hdac_hdmi_dai_pin_map *dai_map;
bcced704 390 struct hdac_hdmi_pin *pin;
b0362adb 391 struct hdac_ext_dma_params *dd;
a657f1d0 392 int ret;
b0362adb 393
b0362adb 394 dai_map = &hdmi->dai_map[dai->id];
bcced704 395 pin = dai_map->pin;
b0362adb
SP
396
397 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
398 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
399 dd->stream_tag, dd->format);
400
bcced704
SP
401 mutex_lock(&pin->lock);
402 pin->channels = substream->runtime->channels;
403
15b91447
SP
404 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
405 dai_map->pin->nid);
bcced704 406 mutex_unlock(&pin->lock);
a657f1d0
SP
407 if (ret < 0)
408 return ret;
409
15b91447
SP
410 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
411 dai_map->pin->nid, dd->stream_tag, dd->format);
b0362adb
SP
412}
413
414static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
415 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
416{
417 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
54dfa1ea
SP
418 struct hdac_hdmi_priv *hdmi = hdac->private_data;
419 struct hdac_hdmi_dai_pin_map *dai_map;
420 struct hdac_hdmi_pin *pin;
b0362adb
SP
421 struct hdac_ext_dma_params *dd;
422
54dfa1ea
SP
423 dai_map = &hdmi->dai_map[dai->id];
424 pin = dai_map->pin;
425
426 if (!pin)
427 return -ENODEV;
428
429 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
430 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
431 pin->nid);
b0362adb
SP
432 return -ENODEV;
433 }
434
6793a3d7
SP
435 dd = snd_soc_dai_get_dma_data(dai, substream);
436 if (!dd) {
437 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
438 if (!dd)
439 return -ENOMEM;
440 }
441
b0362adb
SP
442 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
443 params_channels(hparams), params_format(hparams),
444 24, 0);
445
446 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
447
448 return 0;
449}
450
451static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
452 struct snd_soc_dai *dai)
453{
454 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
455 struct hdac_ext_dma_params *dd;
456 struct hdac_hdmi_priv *hdmi = edev->private_data;
457 struct hdac_hdmi_dai_pin_map *dai_map;
458
459 dai_map = &hdmi->dai_map[dai->id];
460
b0362adb 461 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
b0362adb 462
6793a3d7
SP
463 if (dd) {
464 snd_soc_dai_set_dma_data(dai, substream, NULL);
465 kfree(dd);
466 }
b0362adb
SP
467
468 return 0;
469}
470
ab85f5b3
SP
471static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
472 struct hdac_hdmi_dai_pin_map *dai_map)
473{
474 /* Enable transmission */
475 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
476 AC_VERB_SET_DIGI_CONVERT_1, 1);
477
478 /* Category Code (CC) to zero */
479 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
480 AC_VERB_SET_DIGI_CONVERT_2, 0);
481}
482
148569fd
SP
483static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
484 struct hdac_hdmi_dai_pin_map *dai_map)
485{
486 int mux_idx;
487 struct hdac_hdmi_pin *pin = dai_map->pin;
488
489 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
490 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
491 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
492 AC_VERB_SET_CONNECT_SEL, mux_idx);
493 break;
494 }
495 }
496
497 if (mux_idx == pin->num_mux_nids)
498 return -EIO;
499
500 /* Enable out path for this pin widget */
501 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
502 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
503
504 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
505
506 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
507 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
508
509 return 0;
510}
511
512static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
513 struct hdac_hdmi_pin *pin)
514{
515 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
516 dev_warn(&hdac->hdac.dev,
517 "HDMI: pin %d wcaps %#x does not support connection list\n",
518 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
519 return -EINVAL;
520 }
521
522 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
523 pin->mux_nids, HDA_MAX_CONNECTIONS);
524 if (pin->num_mux_nids == 0)
525 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
526 pin->nid);
527
528 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
529 pin->num_mux_nids, pin->nid);
530
531 return pin->num_mux_nids;
532}
533
534/*
535 * Query pcm list and return pin widget to which stream is routed.
536 *
537 * Also query connection list of the pin, to validate the cvt to pin map.
538 *
539 * Same stream rendering to multiple pins simultaneously can be done
540 * possibly, but not supported for now in driver. So return the first pin
541 * connected.
542 */
543static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
544 struct hdac_ext_device *edev,
545 struct hdac_hdmi_priv *hdmi,
546 struct hdac_hdmi_cvt *cvt)
547{
548 struct hdac_hdmi_pcm *pcm;
549 struct hdac_hdmi_pin *pin = NULL;
550 int ret, i;
551
552 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
553 if (pcm->cvt == cvt) {
554 pin = pcm->pin;
555 break;
556 }
557 }
558
559 if (pin) {
560 ret = hdac_hdmi_query_pin_connlist(edev, pin);
561 if (ret < 0)
562 return NULL;
563
564 for (i = 0; i < pin->num_mux_nids; i++) {
565 if (pin->mux_nids[i] == cvt->nid)
566 return pin;
567 }
568 }
569
570 return NULL;
571}
572
54dfa1ea
SP
573/*
574 * This tries to get a valid pin and set the HW constraints based on the
575 * ELD. Even if a valid pin is not found return success so that device open
576 * doesn't fail.
577 */
b0362adb
SP
578static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
579 struct snd_soc_dai *dai)
580{
581 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
582 struct hdac_hdmi_priv *hdmi = hdac->private_data;
583 struct hdac_hdmi_dai_pin_map *dai_map;
148569fd
SP
584 struct hdac_hdmi_cvt *cvt;
585 struct hdac_hdmi_pin *pin;
2428bca3 586 int ret;
b0362adb 587
b0362adb
SP
588 dai_map = &hdmi->dai_map[dai->id];
589
148569fd
SP
590 cvt = dai_map->cvt;
591 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
54dfa1ea
SP
592
593 /*
594 * To make PA and other userland happy.
595 * userland scans devices so returning error does not help.
596 */
148569fd 597 if (!pin)
54dfa1ea 598 return 0;
148569fd
SP
599
600 if ((!pin->eld.monitor_present) ||
601 (!pin->eld.eld_valid)) {
b8a54545 602
54dfa1ea 603 dev_warn(&hdac->hdac.dev,
148569fd
SP
604 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
605 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
b0362adb 606
54dfa1ea 607 return 0;
b0362adb
SP
608 }
609
148569fd 610 dai_map->pin = pin;
b0362adb 611
ab85f5b3 612 hdac_hdmi_enable_cvt(hdac, dai_map);
148569fd
SP
613 ret = hdac_hdmi_enable_pin(hdac, dai_map);
614 if (ret < 0)
615 return ret;
b0362adb 616
2428bca3 617 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
148569fd 618 pin->eld.eld_buffer);
2428bca3
SP
619 if (ret < 0)
620 return ret;
b0362adb 621
2428bca3 622 return snd_pcm_hw_constraint_eld(substream->runtime,
148569fd 623 pin->eld.eld_buffer);
b0362adb
SP
624}
625
571d5078
JK
626static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
627 struct snd_soc_dai *dai)
628{
629 struct hdac_hdmi_dai_pin_map *dai_map;
630 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
631 struct hdac_hdmi_priv *hdmi = hdac->private_data;
632 int ret;
633
634 dai_map = &hdmi->dai_map[dai->id];
635 if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
636 ret = hdac_hdmi_enable_pin(hdac, dai_map);
637 if (ret < 0)
638 return ret;
639
640 return hdac_hdmi_playback_prepare(substream, dai);
641 }
642
643 return 0;
644}
645
b0362adb
SP
646static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
647 struct snd_soc_dai *dai)
648{
649 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
650 struct hdac_hdmi_priv *hdmi = hdac->private_data;
651 struct hdac_hdmi_dai_pin_map *dai_map;
652
653 dai_map = &hdmi->dai_map[dai->id];
654
54dfa1ea
SP
655 if (dai_map->pin) {
656 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
657 AC_VERB_SET_CHANNEL_STREAMID, 0);
658 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
659 AC_VERB_SET_STREAM_FORMAT, 0);
660
661 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
b0362adb 662
54dfa1ea 663 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
b0362adb 664 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
148569fd 665
bcced704
SP
666 mutex_lock(&dai_map->pin->lock);
667 dai_map->pin->channels = 0;
668 mutex_unlock(&dai_map->pin->lock);
669
54dfa1ea
SP
670 dai_map->pin = NULL;
671 }
b0362adb
SP
672}
673
18382ead
SP
674static int
675hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
676{
bcced704
SP
677 unsigned int chans;
678 struct hdac_ext_device *edev = to_ehdac_device(hdac);
679 struct hdac_hdmi_priv *hdmi = edev->private_data;
18382ead
SP
680 int err;
681
bcced704
SP
682 chans = get_wcaps(hdac, cvt->nid);
683 chans = get_wcaps_channels(chans);
684
685 cvt->params.channels_min = 2;
686
687 cvt->params.channels_max = chans;
688 if (chans > hdmi->chmap.channels_max)
689 hdmi->chmap.channels_max = chans;
18382ead
SP
690
691 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
692 &cvt->params.rates,
693 &cvt->params.formats,
694 &cvt->params.maxbps);
695 if (err < 0)
696 dev_err(&hdac->dev,
697 "Failed to query pcm params for nid %d: %d\n",
698 cvt->nid, err);
699
700 return err;
701}
702
79f4e922
SP
703static int hdac_hdmi_fill_widget_info(struct device *dev,
704 struct snd_soc_dapm_widget *w,
705 enum snd_soc_dapm_type id, void *priv,
706 const char *wname, const char *stream,
707 struct snd_kcontrol_new *wc, int numkc)
18382ead
SP
708{
709 w->id = id;
79f4e922
SP
710 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
711 if (!w->name)
712 return -ENOMEM;
713
18382ead
SP
714 w->sname = stream;
715 w->reg = SND_SOC_NOPM;
716 w->shift = 0;
79f4e922
SP
717 w->kcontrol_news = wc;
718 w->num_kcontrols = numkc;
719 w->priv = priv;
720
721 return 0;
18382ead
SP
722}
723
724static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
79f4e922
SP
725 const char *sink, const char *control, const char *src,
726 int (*handler)(struct snd_soc_dapm_widget *src,
727 struct snd_soc_dapm_widget *sink))
18382ead
SP
728{
729 route->sink = sink;
730 route->source = src;
731 route->control = control;
79f4e922 732 route->connected = handler;
18382ead
SP
733}
734
4a3478de
JK
735static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
736 struct hdac_hdmi_pin *pin)
737{
738 struct hdac_hdmi_priv *hdmi = edev->private_data;
739 struct hdac_hdmi_pcm *pcm = NULL;
740
741 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
742 if (pcm->pin == pin)
743 return pcm;
744 }
745
746 return NULL;
747}
748
749/*
750 * Based on user selection, map the PINs with the PCMs.
751 */
752static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
753 struct snd_ctl_elem_value *ucontrol)
754{
755 int ret;
756 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
757 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
758 struct snd_soc_dapm_context *dapm = w->dapm;
759 struct hdac_hdmi_pin *pin = w->priv;
760 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
761 struct hdac_hdmi_priv *hdmi = edev->private_data;
762 struct hdac_hdmi_pcm *pcm = NULL;
763 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
764
765 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
766 if (ret < 0)
767 return ret;
768
769 mutex_lock(&hdmi->pin_mutex);
770 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
771 if (pcm->pin == pin)
772 pcm->pin = NULL;
773
774 /*
775 * Jack status is not reported during device probe as the
776 * PCMs are not registered by then. So report it here.
777 */
778 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
779 pcm->pin = pin;
780 if (pin->eld.monitor_present && pin->eld.eld_valid) {
781 dev_dbg(&edev->hdac.dev,
782 "jack report for pcm=%d\n",
783 pcm->pcm_id);
784
785 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
786 }
787 mutex_unlock(&hdmi->pin_mutex);
788 return ret;
789 }
790 }
791 mutex_unlock(&hdmi->pin_mutex);
792
793 return ret;
794}
795
79f4e922
SP
796/*
797 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
798 * the display driver seem to be programming the connection list for the pin
799 * widget runtime.
800 *
801 * So programming all the possible inputs for the mux, the user has to take
802 * care of selecting the right one and leaving all other inputs selected to
803 * "NONE"
804 */
805static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
806 struct hdac_hdmi_pin *pin,
807 struct snd_soc_dapm_widget *widget,
808 const char *widget_name)
809{
810 struct hdac_hdmi_priv *hdmi = edev->private_data;
811 struct snd_kcontrol_new *kc;
812 struct hdac_hdmi_cvt *cvt;
813 struct soc_enum *se;
814 char kc_name[NAME_SIZE];
815 char mux_items[NAME_SIZE];
816 /* To hold inputs to the Pin mux */
817 char *items[HDA_MAX_CONNECTIONS];
818 int i = 0;
819 int num_items = hdmi->num_cvt + 1;
820
821 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
822 if (!kc)
823 return -ENOMEM;
824
825 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
826 if (!se)
827 return -ENOMEM;
828
829 sprintf(kc_name, "Pin %d Input", pin->nid);
830 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
831 if (!kc->name)
832 return -ENOMEM;
833
834 kc->private_value = (long)se;
835 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
836 kc->access = 0;
837 kc->info = snd_soc_info_enum_double;
4a3478de 838 kc->put = hdac_hdmi_set_pin_mux;
79f4e922
SP
839 kc->get = snd_soc_dapm_get_enum_double;
840
841 se->reg = SND_SOC_NOPM;
842
843 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
844 se->items = num_items;
845 se->mask = roundup_pow_of_two(se->items) - 1;
846
847 sprintf(mux_items, "NONE");
848 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
849 if (!items[i])
850 return -ENOMEM;
851
852 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
853 i++;
854 sprintf(mux_items, "cvt %d", cvt->nid);
855 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
856 if (!items[i])
857 return -ENOMEM;
858 }
859
860 se->texts = devm_kmemdup(&edev->hdac.dev, items,
861 (num_items * sizeof(char *)), GFP_KERNEL);
862 if (!se->texts)
863 return -ENOMEM;
864
865 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
4a3478de 866 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
79f4e922
SP
867}
868
869/* Add cvt <- input <- mux route map */
870static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
871 struct snd_soc_dapm_widget *widgets,
872 struct snd_soc_dapm_route *route, int rindex)
873{
874 struct hdac_hdmi_priv *hdmi = edev->private_data;
875 const struct snd_kcontrol_new *kc;
876 struct soc_enum *se;
877 int mux_index = hdmi->num_cvt + hdmi->num_pin;
878 int i, j;
879
880 for (i = 0; i < hdmi->num_pin; i++) {
881 kc = widgets[mux_index].kcontrol_news;
882 se = (struct soc_enum *)kc->private_value;
883 for (j = 0; j < hdmi->num_cvt; j++) {
884 hdac_hdmi_fill_route(&route[rindex],
885 widgets[mux_index].name,
886 se->texts[j + 1],
887 widgets[j].name, NULL);
888
889 rindex++;
890 }
891
892 mux_index++;
893 }
894}
895
896/*
897 * Widgets are added in the below sequence
898 * Converter widgets for num converters enumerated
899 * Pin widgets for num pins enumerated
900 * Pin mux widgets to represent connenction list of pin widget
901 *
902 * Total widgets elements = num_cvt + num_pin + num_pin;
903 *
904 * Routes are added as below:
905 * pin mux -> pin (based on num_pins)
906 * cvt -> "Input sel control" -> pin_mux
907 *
908 * Total route elements:
909 * num_pins + (pin_muxes * num_cvt)
910 */
911static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
18382ead 912{
79f4e922
SP
913 struct snd_soc_dapm_widget *widgets;
914 struct snd_soc_dapm_route *route;
915 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
916 struct hdac_hdmi_priv *hdmi = edev->private_data;
917 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
918 char widget_name[NAME_SIZE];
919 struct hdac_hdmi_cvt *cvt;
920 struct hdac_hdmi_pin *pin;
921 int ret, i = 0, num_routes = 0;
18382ead 922
79f4e922
SP
923 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
924 return -EINVAL;
18382ead 925
79f4e922
SP
926 widgets = devm_kzalloc(dapm->dev,
927 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
928 GFP_KERNEL);
18382ead 929
79f4e922
SP
930 if (!widgets)
931 return -ENOMEM;
932
933 /* DAPM widgets to represent each converter widget */
934 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
935 sprintf(widget_name, "Converter %d", cvt->nid);
936 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
937 snd_soc_dapm_aif_in, &cvt->nid,
938 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
939 if (ret < 0)
940 return ret;
941 i++;
942 }
943
944 list_for_each_entry(pin, &hdmi->pin_list, head) {
945 sprintf(widget_name, "hif%d Output", pin->nid);
946 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
947 snd_soc_dapm_output, &pin->nid,
948 widget_name, NULL, NULL, 0);
949 if (ret < 0)
950 return ret;
951 i++;
952 }
953
954 /* DAPM widgets to represent the connection list to pin widget */
955 list_for_each_entry(pin, &hdmi->pin_list, head) {
956 sprintf(widget_name, "Pin %d Mux", pin->nid);
957 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
958 widget_name);
959 if (ret < 0)
960 return ret;
961 i++;
962
963 /* For cvt to pin_mux mapping */
964 num_routes += hdmi->num_cvt;
965
966 /* For pin_mux to pin mapping */
967 num_routes++;
968 }
969
970 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
971 GFP_KERNEL);
972 if (!route)
973 return -ENOMEM;
974
975 i = 0;
976 /* Add pin <- NULL <- mux route map */
977 list_for_each_entry(pin, &hdmi->pin_list, head) {
978 int sink_index = i + hdmi->num_cvt;
979 int src_index = sink_index + hdmi->num_pin;
980
981 hdac_hdmi_fill_route(&route[i],
982 widgets[sink_index].name, NULL,
983 widgets[src_index].name, NULL);
984 i++;
985
986 }
987
988 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
989
990 snd_soc_dapm_new_controls(dapm, widgets,
991 ((2 * hdmi->num_pin) + hdmi->num_cvt));
992
993 snd_soc_dapm_add_routes(dapm, route, num_routes);
994 snd_soc_dapm_new_widgets(dapm->card);
995
996 return 0;
18382ead 997
18382ead
SP
998}
999
15b91447 1000static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
18382ead 1001{
15b91447 1002 struct hdac_hdmi_priv *hdmi = edev->private_data;
148569fd 1003 struct hdac_hdmi_dai_pin_map *dai_map;
15b91447 1004 struct hdac_hdmi_cvt *cvt;
148569fd 1005 int dai_id = 0;
18382ead 1006
148569fd 1007 if (list_empty(&hdmi->cvt_list))
15b91447 1008 return -EINVAL;
18382ead 1009
148569fd
SP
1010 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1011 dai_map = &hdmi->dai_map[dai_id];
1012 dai_map->dai_id = dai_id;
1013 dai_map->cvt = cvt;
18382ead 1014
148569fd 1015 dai_id++;
18382ead 1016
148569fd
SP
1017 if (dai_id == HDA_MAX_CVTS) {
1018 dev_warn(&edev->hdac.dev,
1019 "Max dais supported: %d\n", dai_id);
1020 break;
1021 }
1022 }
18382ead 1023
15b91447
SP
1024 return 0;
1025}
1026
1027static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1028{
1029 struct hdac_hdmi_priv *hdmi = edev->private_data;
1030 struct hdac_hdmi_cvt *cvt;
4a3478de 1031 char name[NAME_SIZE];
15b91447
SP
1032
1033 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1034 if (!cvt)
1035 return -ENOMEM;
1036
1037 cvt->nid = nid;
4a3478de
JK
1038 sprintf(name, "cvt %d", cvt->nid);
1039 cvt->name = kstrdup(name, GFP_KERNEL);
15b91447
SP
1040
1041 list_add_tail(&cvt->head, &hdmi->cvt_list);
1042 hdmi->num_cvt++;
1043
1044 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1045}
1046
b7756ede
SP
1047static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1048 struct hdac_hdmi_pin *pin)
1049{
1050 pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1051}
1052
b8a54545
SP
1053static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1054{
1055 struct hdac_ext_device *edev = pin->edev;
4a3478de
JK
1056 struct hdac_hdmi_priv *hdmi = edev->private_data;
1057 struct hdac_hdmi_pcm *pcm;
b8a54545
SP
1058 int val;
1059
b8a54545
SP
1060 pin->repoll_count = repoll;
1061
1062 pm_runtime_get_sync(&edev->hdac.dev);
1063 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1064 AC_VERB_GET_PIN_SENSE, 0);
1065
1066 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1067 val, pin->nid);
1068
4a3478de
JK
1069
1070 mutex_lock(&hdmi->pin_mutex);
b8a54545
SP
1071 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1072 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1073
4a3478de
JK
1074 pcm = hdac_hdmi_get_pcm(edev, pin);
1075
b8a54545
SP
1076 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1077
1078 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1079 __func__, pin->nid);
4a3478de
JK
1080
1081 /*
1082 * PCMs are not registered during device probe, so don't
1083 * report jack here. It will be done in usermode mux
1084 * control select.
1085 */
1086 if (pcm) {
1087 dev_dbg(&edev->hdac.dev,
1088 "jack report for pcm=%d\n", pcm->pcm_id);
1089
1090 snd_jack_report(pcm->jack, 0);
1091 }
1092
1093 mutex_unlock(&hdmi->pin_mutex);
b8a54545
SP
1094 goto put_hdac_device;
1095 }
1096
1097 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1098 /* TODO: use i915 component for reading ELD later */
1099 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1100 pin->eld.eld_buffer,
1101 &pin->eld.eld_size) == 0) {
1102
4a3478de
JK
1103 if (pcm) {
1104 dev_dbg(&edev->hdac.dev,
1105 "jack report for pcm=%d\n",
1106 pcm->pcm_id);
1107
1108 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1109 }
b7756ede 1110 hdac_hdmi_parse_eld(edev, pin);
4a3478de 1111
b8a54545
SP
1112 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1113 pin->eld.eld_buffer, pin->eld.eld_size);
1114 } else {
1115 pin->eld.monitor_present = false;
1116 pin->eld.eld_valid = false;
4a3478de
JK
1117
1118 if (pcm) {
1119 dev_dbg(&edev->hdac.dev,
1120 "jack report for pcm=%d\n",
1121 pcm->pcm_id);
1122
1123 snd_jack_report(pcm->jack, 0);
1124 }
b8a54545
SP
1125 }
1126 }
1127
4a3478de
JK
1128 mutex_unlock(&hdmi->pin_mutex);
1129
b8a54545
SP
1130 /*
1131 * Sometimes the pin_sense may present invalid monitor
1132 * present and eld_valid. If ELD data is not valid, loop few
1133 * more times to get correct pin sense and valid ELD.
1134 */
1135 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1136 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1137
1138put_hdac_device:
1139 pm_runtime_put_sync(&edev->hdac.dev);
1140}
1141
1142static void hdac_hdmi_repoll_eld(struct work_struct *work)
1143{
1144 struct hdac_hdmi_pin *pin =
1145 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1146
1147 /* picked from legacy HDA driver */
1148 if (pin->repoll_count++ > 6)
1149 pin->repoll_count = 0;
1150
1151 hdac_hdmi_present_sense(pin, pin->repoll_count);
1152}
1153
15b91447
SP
1154static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1155{
1156 struct hdac_hdmi_priv *hdmi = edev->private_data;
1157 struct hdac_hdmi_pin *pin;
1158
1159 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1160 if (!pin)
1161 return -ENOMEM;
1162
1163 pin->nid = nid;
1164
1165 list_add_tail(&pin->head, &hdmi->pin_list);
1166 hdmi->num_pin++;
1167
b8a54545 1168 pin->edev = edev;
bcced704 1169 mutex_init(&pin->lock);
b8a54545
SP
1170 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1171
15b91447 1172 return 0;
18382ead
SP
1173}
1174
211caab7
SP
1175#define INTEL_VENDOR_NID 0x08
1176#define INTEL_GET_VENDOR_VERB 0xf81
1177#define INTEL_SET_VENDOR_VERB 0x781
1178#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1179#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1180
1181static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1182{
1183 unsigned int vendor_param;
1184
1185 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1186 INTEL_GET_VENDOR_VERB, 0);
1187 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1188 return;
1189
1190 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1191 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1192 INTEL_SET_VENDOR_VERB, vendor_param);
1193 if (vendor_param == -1)
1194 return;
1195}
1196
1197static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1198{
1199 unsigned int vendor_param;
1200
1201 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1202 INTEL_GET_VENDOR_VERB, 0);
1203 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1204 return;
1205
1206 /* enable DP1.2 mode */
1207 vendor_param |= INTEL_EN_DP12;
1208 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1209 INTEL_SET_VENDOR_VERB, vendor_param);
1210 if (vendor_param == -1)
1211 return;
1212
1213}
1214
17a42c45
SP
1215static struct snd_soc_dai_ops hdmi_dai_ops = {
1216 .startup = hdac_hdmi_pcm_open,
1217 .shutdown = hdac_hdmi_pcm_close,
1218 .hw_params = hdac_hdmi_set_hw_params,
1219 .prepare = hdac_hdmi_playback_prepare,
571d5078 1220 .trigger = hdac_hdmi_trigger,
17a42c45
SP
1221 .hw_free = hdac_hdmi_playback_cleanup,
1222};
1223
1224/*
1225 * Each converter can support a stream independently. So a dai is created
1226 * based on the number of converter queried.
1227 */
1228static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1229 struct snd_soc_dai_driver **dais,
1230 struct hdac_hdmi_priv *hdmi, int num_dais)
1231{
1232 struct snd_soc_dai_driver *hdmi_dais;
1233 struct hdac_hdmi_cvt *cvt;
1234 char name[NAME_SIZE], dai_name[NAME_SIZE];
1235 int i = 0;
1236 u32 rates, bps;
1237 unsigned int rate_max = 384000, rate_min = 8000;
1238 u64 formats;
1239 int ret;
1240
1241 hdmi_dais = devm_kzalloc(&hdac->dev,
1242 (sizeof(*hdmi_dais) * num_dais),
1243 GFP_KERNEL);
1244 if (!hdmi_dais)
1245 return -ENOMEM;
1246
1247 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1248 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1249 &rates, &formats, &bps);
1250 if (ret)
1251 return ret;
1252
1253 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1254 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1255 dai_name, GFP_KERNEL);
1256
1257 if (!hdmi_dais[i].name)
1258 return -ENOMEM;
1259
1260 snprintf(name, sizeof(name), "hifi%d", i+1);
1261 hdmi_dais[i].playback.stream_name =
1262 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1263 if (!hdmi_dais[i].playback.stream_name)
1264 return -ENOMEM;
1265
1266 /*
1267 * Set caps based on capability queried from the converter.
1268 * It will be constrained runtime based on ELD queried.
1269 */
1270 hdmi_dais[i].playback.formats = formats;
1271 hdmi_dais[i].playback.rates = rates;
1272 hdmi_dais[i].playback.rate_max = rate_max;
1273 hdmi_dais[i].playback.rate_min = rate_min;
1274 hdmi_dais[i].playback.channels_min = 2;
1275 hdmi_dais[i].playback.channels_max = 2;
1276 hdmi_dais[i].ops = &hdmi_dai_ops;
1277
1278 i++;
1279 }
1280
1281 *dais = hdmi_dais;
1282
1283 return 0;
1284}
1285
18382ead
SP
1286/*
1287 * Parse all nodes and store the cvt/pin nids in array
1288 * Add one time initialization for pin and cvt widgets
1289 */
17a42c45
SP
1290static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1291 struct snd_soc_dai_driver **dais, int *num_dais)
18382ead
SP
1292{
1293 hda_nid_t nid;
3c83ac23 1294 int i, num_nodes;
18382ead
SP
1295 struct hdac_device *hdac = &edev->hdac;
1296 struct hdac_hdmi_priv *hdmi = edev->private_data;
15b91447 1297 int ret;
18382ead 1298
211caab7
SP
1299 hdac_hdmi_skl_enable_all_pins(hdac);
1300 hdac_hdmi_skl_enable_dp12(hdac);
1301
3c83ac23 1302 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
541140d4 1303 if (!nid || num_nodes <= 0) {
18382ead
SP
1304 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1305 return -EINVAL;
1306 }
1307
3c83ac23 1308 hdac->num_nodes = num_nodes;
18382ead
SP
1309 hdac->start_nid = nid;
1310
1311 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1312 unsigned int caps;
1313 unsigned int type;
1314
1315 caps = get_wcaps(hdac, nid);
1316 type = get_wcaps_type(caps);
1317
1318 if (!(caps & AC_WCAP_DIGITAL))
1319 continue;
1320
1321 switch (type) {
1322
1323 case AC_WID_AUD_OUT:
15b91447
SP
1324 ret = hdac_hdmi_add_cvt(edev, nid);
1325 if (ret < 0)
1326 return ret;
18382ead
SP
1327 break;
1328
1329 case AC_WID_PIN:
15b91447
SP
1330 ret = hdac_hdmi_add_pin(edev, nid);
1331 if (ret < 0)
1332 return ret;
18382ead
SP
1333 break;
1334 }
1335 }
1336
1337 hdac->end_nid = nid;
1338
15b91447 1339 if (!hdmi->num_pin || !hdmi->num_cvt)
18382ead
SP
1340 return -EIO;
1341
17a42c45
SP
1342 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1343 if (ret) {
1344 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1345 ret);
1346 return ret;
1347 }
1348
1349 *num_dais = hdmi->num_cvt;
1350
15b91447 1351 return hdac_hdmi_init_dai_map(edev);
18382ead
SP
1352}
1353
b8a54545
SP
1354static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1355{
1356 struct hdac_ext_device *edev = aptr;
1357 struct hdac_hdmi_priv *hdmi = edev->private_data;
1358 struct hdac_hdmi_pin *pin;
1359 struct snd_soc_codec *codec = edev->scodec;
1360
1361 /* Don't know how this mapping is derived */
1362 hda_nid_t pin_nid = port + 0x04;
1363
1364 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1365
1366 /*
1367 * skip notification during system suspend (but not in runtime PM);
1368 * the state will be updated at resume. Also since the ELD and
1369 * connection states are updated in anyway at the end of the resume,
1370 * we can skip it when received during PM process.
1371 */
1372 if (snd_power_get_state(codec->component.card->snd_card) !=
1373 SNDRV_CTL_POWER_D0)
1374 return;
1375
1376 if (atomic_read(&edev->hdac.in_pm))
1377 return;
1378
1379 list_for_each_entry(pin, &hdmi->pin_list, head) {
1380 if (pin->nid == pin_nid)
1381 hdac_hdmi_present_sense(pin, 1);
1382 }
1383}
1384
1385static struct i915_audio_component_audio_ops aops = {
1386 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1387};
1388
4a3478de
JK
1389int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1390{
1391 char jack_name[NAME_SIZE];
1392 struct snd_soc_codec *codec = dai->codec;
1393 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1394 struct snd_soc_dapm_context *dapm =
1395 snd_soc_component_get_dapm(&codec->component);
1396 struct hdac_hdmi_priv *hdmi = edev->private_data;
1397 struct hdac_hdmi_pcm *pcm;
1398
1399 /*
1400 * this is a new PCM device, create new pcm and
1401 * add to the pcm list
1402 */
1403 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1404 if (!pcm)
1405 return -ENOMEM;
1406 pcm->pcm_id = device;
1407 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1408
1409 list_add_tail(&pcm->head, &hdmi->pcm_list);
1410
1411 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1412
1413 return snd_jack_new(dapm->card->snd_card, jack_name,
1414 SND_JACK_AVOUT, &pcm->jack, true, false);
1415}
1416EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1417
18382ead
SP
1418static int hdmi_codec_probe(struct snd_soc_codec *codec)
1419{
1420 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1421 struct hdac_hdmi_priv *hdmi = edev->private_data;
1422 struct snd_soc_dapm_context *dapm =
1423 snd_soc_component_get_dapm(&codec->component);
b8a54545
SP
1424 struct hdac_hdmi_pin *pin;
1425 int ret;
18382ead
SP
1426
1427 edev->scodec = codec;
1428
79f4e922
SP
1429 ret = create_fill_widget_route_map(dapm);
1430 if (ret < 0)
1431 return ret;
18382ead 1432
b8a54545
SP
1433 aops.audio_ptr = edev;
1434 ret = snd_hdac_i915_register_notifier(&aops);
1435 if (ret < 0) {
1436 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1437 ret);
1438 return ret;
1439 }
1440
1441 list_for_each_entry(pin, &hdmi->pin_list, head)
1442 hdac_hdmi_present_sense(pin, 1);
1443
18382ead
SP
1444 /* Imp: Store the card pointer in hda_codec */
1445 edev->card = dapm->card->snd_card;
1446
e342ac08
SP
1447 /*
1448 * hdac_device core already sets the state to active and calls
1449 * get_noresume. So enable runtime and set the device to suspend.
1450 */
1451 pm_runtime_enable(&edev->hdac.dev);
1452 pm_runtime_put(&edev->hdac.dev);
1453 pm_runtime_suspend(&edev->hdac.dev);
1454
1455 return 0;
1456}
1457
1458static int hdmi_codec_remove(struct snd_soc_codec *codec)
1459{
1460 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1461
1462 pm_runtime_disable(&edev->hdac.dev);
18382ead
SP
1463 return 0;
1464}
1465
571d5078
JK
1466#ifdef CONFIG_PM
1467static int hdmi_codec_resume(struct snd_soc_codec *codec)
1468{
1469 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1470 struct hdac_hdmi_priv *hdmi = edev->private_data;
1471 struct hdac_hdmi_pin *pin;
1472 struct hdac_device *hdac = &edev->hdac;
1473 struct hdac_bus *bus = hdac->bus;
1474 int err;
1475 unsigned long timeout;
1476
1477 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1478 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1479
1480 /* Power up afg */
1481 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1482
1483 snd_hdac_codec_write(hdac, hdac->afg, 0,
1484 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1485
1486 /* Wait till power state is set to D0 */
1487 timeout = jiffies + msecs_to_jiffies(1000);
1488 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1489 && time_before(jiffies, timeout)) {
1490 msleep(50);
1491 }
1492 }
1493
1494 /*
1495 * As the ELD notify callback request is not entertained while the
1496 * device is in suspend state. Need to manually check detection of
1497 * all pins here.
1498 */
1499 list_for_each_entry(pin, &hdmi->pin_list, head)
1500 hdac_hdmi_present_sense(pin, 1);
1501
1502 /*
1503 * Codec power is turned ON during controller resume.
1504 * Turn it OFF here
1505 */
1506 err = snd_hdac_display_power(bus, false);
1507 if (err < 0) {
1508 dev_err(bus->dev,
1509 "Cannot turn OFF display power on i915, err: %d\n",
1510 err);
1511 return err;
1512 }
1513
1514 return 0;
1515}
1516#else
1517#define hdmi_codec_resume NULL
1518#endif
1519
18382ead
SP
1520static struct snd_soc_codec_driver hdmi_hda_codec = {
1521 .probe = hdmi_codec_probe,
e342ac08 1522 .remove = hdmi_codec_remove,
571d5078 1523 .resume = hdmi_codec_resume,
18382ead
SP
1524 .idle_bias_off = true,
1525};
1526
18382ead
SP
1527static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1528{
1529 struct hdac_device *codec = &edev->hdac;
1530 struct hdac_hdmi_priv *hdmi_priv;
17a42c45
SP
1531 struct snd_soc_dai_driver *hdmi_dais = NULL;
1532 int num_dais = 0;
18382ead
SP
1533 int ret = 0;
1534
1535 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1536 if (hdmi_priv == NULL)
1537 return -ENOMEM;
1538
1539 edev->private_data = hdmi_priv;
bcced704 1540 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
18382ead
SP
1541
1542 dev_set_drvdata(&codec->dev, edev);
1543
15b91447
SP
1544 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1545 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
4a3478de
JK
1546 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1547 mutex_init(&hdmi_priv->pin_mutex);
15b91447 1548
aeaccef0
RB
1549 /*
1550 * Turned off in the runtime_suspend during the first explicit
1551 * pm_runtime_suspend call.
1552 */
1553 ret = snd_hdac_display_power(edev->hdac.bus, true);
1554 if (ret < 0) {
1555 dev_err(&edev->hdac.dev,
1556 "Cannot turn on display power on i915 err: %d\n",
1557 ret);
1558 return ret;
1559 }
1560
17a42c45
SP
1561 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1562 if (ret < 0) {
1563 dev_err(&codec->dev,
1564 "Failed in parse and map nid with err: %d\n", ret);
18382ead 1565 return ret;
17a42c45 1566 }
18382ead
SP
1567
1568 /* ASoC specific initialization */
1569 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
17a42c45 1570 hdmi_dais, num_dais);
18382ead
SP
1571}
1572
1573static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1574{
15b91447
SP
1575 struct hdac_hdmi_priv *hdmi = edev->private_data;
1576 struct hdac_hdmi_pin *pin, *pin_next;
1577 struct hdac_hdmi_cvt *cvt, *cvt_next;
4a3478de 1578 struct hdac_hdmi_pcm *pcm, *pcm_next;
15b91447 1579
18382ead
SP
1580 snd_soc_unregister_codec(&edev->hdac.dev);
1581
4a3478de
JK
1582 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1583 pcm->cvt = NULL;
1584 pcm->pin = NULL;
1585 list_del(&pcm->head);
1586 kfree(pcm);
1587 }
1588
15b91447
SP
1589 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1590 list_del(&cvt->head);
4a3478de 1591 kfree(cvt->name);
15b91447
SP
1592 kfree(cvt);
1593 }
1594
1595 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1596 list_del(&pin->head);
1597 kfree(pin);
1598 }
1599
18382ead
SP
1600 return 0;
1601}
1602
e342ac08
SP
1603#ifdef CONFIG_PM
1604static int hdac_hdmi_runtime_suspend(struct device *dev)
1605{
1606 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1607 struct hdac_device *hdac = &edev->hdac;
07f083ab 1608 struct hdac_bus *bus = hdac->bus;
7ed49700 1609 unsigned long timeout;
07f083ab 1610 int err;
e342ac08
SP
1611
1612 dev_dbg(dev, "Enter: %s\n", __func__);
1613
07f083ab
SP
1614 /* controller may not have been initialized for the first time */
1615 if (!bus)
1616 return 0;
1617
e342ac08 1618 /* Power down afg */
7ed49700 1619 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
e342ac08
SP
1620 snd_hdac_codec_write(hdac, hdac->afg, 0,
1621 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1622
7ed49700
SP
1623 /* Wait till power state is set to D3 */
1624 timeout = jiffies + msecs_to_jiffies(1000);
1625 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
1626 && time_before(jiffies, timeout)) {
1627
1628 msleep(50);
1629 }
1630 }
1631
07f083ab
SP
1632 err = snd_hdac_display_power(bus, false);
1633 if (err < 0) {
1634 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1635 return err;
1636 }
1637
e342ac08
SP
1638 return 0;
1639}
1640
1641static int hdac_hdmi_runtime_resume(struct device *dev)
1642{
1643 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1644 struct hdac_device *hdac = &edev->hdac;
07f083ab
SP
1645 struct hdac_bus *bus = hdac->bus;
1646 int err;
e342ac08
SP
1647
1648 dev_dbg(dev, "Enter: %s\n", __func__);
1649
07f083ab
SP
1650 /* controller may not have been initialized for the first time */
1651 if (!bus)
1652 return 0;
1653
1654 err = snd_hdac_display_power(bus, true);
1655 if (err < 0) {
1656 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1657 return err;
1658 }
1659
ab85f5b3
SP
1660 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1661 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1662
e342ac08
SP
1663 /* Power up afg */
1664 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1665 snd_hdac_codec_write(hdac, hdac->afg, 0,
1666 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1667
1668 return 0;
1669}
1670#else
1671#define hdac_hdmi_runtime_suspend NULL
1672#define hdac_hdmi_runtime_resume NULL
1673#endif
1674
1675static const struct dev_pm_ops hdac_hdmi_pm = {
1676 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1677};
1678
18382ead
SP
1679static const struct hda_device_id hdmi_list[] = {
1680 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
e2304803 1681 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
18382ead
SP
1682 {}
1683};
1684
1685MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1686
1687static struct hdac_ext_driver hdmi_driver = {
1688 . hdac = {
1689 .driver = {
1690 .name = "HDMI HDA Codec",
e342ac08 1691 .pm = &hdac_hdmi_pm,
18382ead
SP
1692 },
1693 .id_table = hdmi_list,
1694 },
1695 .probe = hdac_hdmi_dev_probe,
1696 .remove = hdac_hdmi_dev_remove,
1697};
1698
1699static int __init hdmi_init(void)
1700{
1701 return snd_hda_ext_driver_register(&hdmi_driver);
1702}
1703
1704static void __exit hdmi_exit(void)
1705{
1706 snd_hda_ext_driver_unregister(&hdmi_driver);
1707}
1708
1709module_init(hdmi_init);
1710module_exit(hdmi_exit);
1711
1712MODULE_LICENSE("GPL v2");
1713MODULE_DESCRIPTION("HDMI HD codec");
1714MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1715MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
This page took 0.277994 seconds and 5 git commands to generate.