Merge remote-tracking branches 'asoc/topic/davinci', 'asoc/topic/fsl-card' and 'asoc...
[deliverable/linux.git] / sound / soc / codecs / hdac_hdmi.c
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>
24 #include <linux/hdmi.h>
25 #include <drm/drm_edid.h>
26 #include <sound/pcm_params.h>
27 #include <sound/jack.h>
28 #include <sound/soc.h>
29 #include <sound/hdaudio_ext.h>
30 #include <sound/hda_i915.h>
31 #include <sound/pcm_drm_eld.h>
32 #include "../../hda/local.h"
33 #include "hdac_hdmi.h"
34
35 #define NAME_SIZE 32
36
37 #define AMP_OUT_MUTE 0xb080
38 #define AMP_OUT_UNMUTE 0xb000
39 #define PIN_OUT (AC_PINCTL_OUT_EN)
40
41 #define HDA_MAX_CONNECTIONS 32
42
43 #define HDA_MAX_CVTS 3
44
45 #define ELD_MAX_SIZE 256
46 #define ELD_FIXED_BYTES 20
47
48 struct hdac_hdmi_cvt_params {
49 unsigned int channels_min;
50 unsigned int channels_max;
51 u32 rates;
52 u64 formats;
53 unsigned int maxbps;
54 };
55
56 struct hdac_hdmi_cvt {
57 struct list_head head;
58 hda_nid_t nid;
59 const char *name;
60 struct hdac_hdmi_cvt_params params;
61 };
62
63 struct hdac_hdmi_eld {
64 bool monitor_present;
65 bool eld_valid;
66 int eld_size;
67 char eld_buffer[ELD_MAX_SIZE];
68 };
69
70 struct hdac_hdmi_pin {
71 struct list_head head;
72 hda_nid_t nid;
73 int num_mux_nids;
74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
77 int repoll_count;
78 struct delayed_work work;
79 };
80
81 struct hdac_hdmi_pcm {
82 struct list_head head;
83 int pcm_id;
84 struct hdac_hdmi_pin *pin;
85 struct hdac_hdmi_cvt *cvt;
86 struct snd_jack *jack;
87 };
88
89 struct hdac_hdmi_dai_pin_map {
90 int dai_id;
91 struct hdac_hdmi_pin *pin;
92 struct hdac_hdmi_cvt *cvt;
93 };
94
95 struct hdac_hdmi_priv {
96 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
97 struct list_head pin_list;
98 struct list_head cvt_list;
99 struct list_head pcm_list;
100 int num_pin;
101 int num_cvt;
102 struct mutex pin_mutex;
103 };
104
105 static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106 {
107 struct hdac_device *hdac = dev_to_hdac_dev(dev);
108
109 return to_ehdac_device(hdac);
110 }
111
112 static unsigned int sad_format(const u8 *sad)
113 {
114 return ((sad[0] >> 0x3) & 0x1f);
115 }
116
117 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
118 {
119 return (sad[2] & 7);
120 }
121
122 static 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
145 format_constraint:
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
147 formats);
148
149 }
150
151 /* HDMI ELD routines */
152 static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153 hda_nid_t nid, int byte_index)
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
166 static 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 */
176 static 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;
221 error:
222 return ret;
223 }
224
225 static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
226 hda_nid_t cvt_nid, hda_nid_t pin_nid,
227 u32 stream_tag, int format)
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
244 static void
245 hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246 int packet_index, int byte_index)
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
256 struct 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
268 static 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;
273 struct dp_audio_infoframe dp_ai;
274 struct hdac_hdmi_priv *hdmi = hdac->private_data;
275 struct hdac_hdmi_pin *pin;
276 u8 *dip;
277 int ret;
278 int i;
279 const u8 *eld_buf;
280 u8 conn_type;
281 int channels = 2;
282
283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
285 break;
286 }
287
288 eld_buf = pin->eld.eld_buffer;
289 conn_type = drm_eld_get_conn_type(eld_buf);
290
291 /* setup channel count */
292 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
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;
305
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 }
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);
334 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
335 for (i = 0; i < sizeof(frame); i++)
336 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
337 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
338 } else {
339 for (i = 0; i < sizeof(dp_ai); i++)
340 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
341 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
342 }
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
352 static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
353 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
354 {
355 /* Power up pin widget */
356 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
357 pwr_state))
358 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
359 AC_VERB_SET_POWER_STATE, pwr_state);
360
361 /* Power up converter */
362 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
363 pwr_state))
364 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
365 AC_VERB_SET_POWER_STATE, pwr_state);
366 }
367
368 static 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;
375 int ret;
376
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
383 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
384 dai_map->pin->nid);
385 if (ret < 0)
386 return ret;
387
388 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
389 dai_map->pin->nid, dd->stream_tag, dd->format);
390 }
391
392 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
393 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
394 {
395 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
396 struct hdac_hdmi_priv *hdmi = hdac->private_data;
397 struct hdac_hdmi_dai_pin_map *dai_map;
398 struct hdac_hdmi_pin *pin;
399 struct hdac_ext_dma_params *dd;
400
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);
410 return -ENODEV;
411 }
412
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
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
429 static 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
439 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
440
441 if (dd) {
442 snd_soc_dai_set_dma_data(dai, substream, NULL);
443 kfree(dd);
444 }
445
446 return 0;
447 }
448
449 static 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
461 static 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
490 static 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 */
521 static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
522 struct hdac_ext_device *edev,
523 struct hdac_hdmi_priv *hdmi,
524 struct hdac_hdmi_cvt *cvt)
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
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 */
556 static 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;
562 struct hdac_hdmi_cvt *cvt;
563 struct hdac_hdmi_pin *pin;
564 int ret;
565
566 dai_map = &hdmi->dai_map[dai->id];
567
568 cvt = dai_map->cvt;
569 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
570
571 /*
572 * To make PA and other userland happy.
573 * userland scans devices so returning error does not help.
574 */
575 if (!pin)
576 return 0;
577
578 if ((!pin->eld.monitor_present) ||
579 (!pin->eld.eld_valid)) {
580
581 dev_warn(&hdac->hdac.dev,
582 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
583 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
584
585 return 0;
586 }
587
588 dai_map->pin = pin;
589
590 hdac_hdmi_enable_cvt(hdac, dai_map);
591 ret = hdac_hdmi_enable_pin(hdac, dai_map);
592 if (ret < 0)
593 return ret;
594
595 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
596 pin->eld.eld_buffer);
597 if (ret < 0)
598 return ret;
599
600 return snd_pcm_hw_constraint_eld(substream->runtime,
601 pin->eld.eld_buffer);
602 }
603
604 static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
605 struct snd_soc_dai *dai)
606 {
607 struct hdac_hdmi_dai_pin_map *dai_map;
608 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
609 struct hdac_hdmi_priv *hdmi = hdac->private_data;
610 int ret;
611
612 dai_map = &hdmi->dai_map[dai->id];
613 if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
614 ret = hdac_hdmi_enable_pin(hdac, dai_map);
615 if (ret < 0)
616 return ret;
617
618 return hdac_hdmi_playback_prepare(substream, dai);
619 }
620
621 return 0;
622 }
623
624 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
625 struct snd_soc_dai *dai)
626 {
627 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
628 struct hdac_hdmi_priv *hdmi = hdac->private_data;
629 struct hdac_hdmi_dai_pin_map *dai_map;
630
631 dai_map = &hdmi->dai_map[dai->id];
632
633 if (dai_map->pin) {
634 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
635 AC_VERB_SET_CHANNEL_STREAMID, 0);
636 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
637 AC_VERB_SET_STREAM_FORMAT, 0);
638
639 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
640
641 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
642 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
643
644 dai_map->pin = NULL;
645 }
646 }
647
648 static int
649 hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
650 {
651 int err;
652
653 /* Only stereo supported as of now */
654 cvt->params.channels_min = cvt->params.channels_max = 2;
655
656 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
657 &cvt->params.rates,
658 &cvt->params.formats,
659 &cvt->params.maxbps);
660 if (err < 0)
661 dev_err(&hdac->dev,
662 "Failed to query pcm params for nid %d: %d\n",
663 cvt->nid, err);
664
665 return err;
666 }
667
668 static int hdac_hdmi_fill_widget_info(struct device *dev,
669 struct snd_soc_dapm_widget *w,
670 enum snd_soc_dapm_type id, void *priv,
671 const char *wname, const char *stream,
672 struct snd_kcontrol_new *wc, int numkc)
673 {
674 w->id = id;
675 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
676 if (!w->name)
677 return -ENOMEM;
678
679 w->sname = stream;
680 w->reg = SND_SOC_NOPM;
681 w->shift = 0;
682 w->kcontrol_news = wc;
683 w->num_kcontrols = numkc;
684 w->priv = priv;
685
686 return 0;
687 }
688
689 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
690 const char *sink, const char *control, const char *src,
691 int (*handler)(struct snd_soc_dapm_widget *src,
692 struct snd_soc_dapm_widget *sink))
693 {
694 route->sink = sink;
695 route->source = src;
696 route->control = control;
697 route->connected = handler;
698 }
699
700 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
701 struct hdac_hdmi_pin *pin)
702 {
703 struct hdac_hdmi_priv *hdmi = edev->private_data;
704 struct hdac_hdmi_pcm *pcm = NULL;
705
706 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
707 if (pcm->pin == pin)
708 return pcm;
709 }
710
711 return NULL;
712 }
713
714 /*
715 * Based on user selection, map the PINs with the PCMs.
716 */
717 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
719 {
720 int ret;
721 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
722 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
723 struct snd_soc_dapm_context *dapm = w->dapm;
724 struct hdac_hdmi_pin *pin = w->priv;
725 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
726 struct hdac_hdmi_priv *hdmi = edev->private_data;
727 struct hdac_hdmi_pcm *pcm = NULL;
728 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
729
730 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
731 if (ret < 0)
732 return ret;
733
734 mutex_lock(&hdmi->pin_mutex);
735 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
736 if (pcm->pin == pin)
737 pcm->pin = NULL;
738
739 /*
740 * Jack status is not reported during device probe as the
741 * PCMs are not registered by then. So report it here.
742 */
743 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
744 pcm->pin = pin;
745 if (pin->eld.monitor_present && pin->eld.eld_valid) {
746 dev_dbg(&edev->hdac.dev,
747 "jack report for pcm=%d\n",
748 pcm->pcm_id);
749
750 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
751 }
752 mutex_unlock(&hdmi->pin_mutex);
753 return ret;
754 }
755 }
756 mutex_unlock(&hdmi->pin_mutex);
757
758 return ret;
759 }
760
761 /*
762 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
763 * the display driver seem to be programming the connection list for the pin
764 * widget runtime.
765 *
766 * So programming all the possible inputs for the mux, the user has to take
767 * care of selecting the right one and leaving all other inputs selected to
768 * "NONE"
769 */
770 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
771 struct hdac_hdmi_pin *pin,
772 struct snd_soc_dapm_widget *widget,
773 const char *widget_name)
774 {
775 struct hdac_hdmi_priv *hdmi = edev->private_data;
776 struct snd_kcontrol_new *kc;
777 struct hdac_hdmi_cvt *cvt;
778 struct soc_enum *se;
779 char kc_name[NAME_SIZE];
780 char mux_items[NAME_SIZE];
781 /* To hold inputs to the Pin mux */
782 char *items[HDA_MAX_CONNECTIONS];
783 int i = 0;
784 int num_items = hdmi->num_cvt + 1;
785
786 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
787 if (!kc)
788 return -ENOMEM;
789
790 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
791 if (!se)
792 return -ENOMEM;
793
794 sprintf(kc_name, "Pin %d Input", pin->nid);
795 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
796 if (!kc->name)
797 return -ENOMEM;
798
799 kc->private_value = (long)se;
800 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
801 kc->access = 0;
802 kc->info = snd_soc_info_enum_double;
803 kc->put = hdac_hdmi_set_pin_mux;
804 kc->get = snd_soc_dapm_get_enum_double;
805
806 se->reg = SND_SOC_NOPM;
807
808 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
809 se->items = num_items;
810 se->mask = roundup_pow_of_two(se->items) - 1;
811
812 sprintf(mux_items, "NONE");
813 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
814 if (!items[i])
815 return -ENOMEM;
816
817 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
818 i++;
819 sprintf(mux_items, "cvt %d", cvt->nid);
820 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
821 if (!items[i])
822 return -ENOMEM;
823 }
824
825 se->texts = devm_kmemdup(&edev->hdac.dev, items,
826 (num_items * sizeof(char *)), GFP_KERNEL);
827 if (!se->texts)
828 return -ENOMEM;
829
830 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
831 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
832 }
833
834 /* Add cvt <- input <- mux route map */
835 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
836 struct snd_soc_dapm_widget *widgets,
837 struct snd_soc_dapm_route *route, int rindex)
838 {
839 struct hdac_hdmi_priv *hdmi = edev->private_data;
840 const struct snd_kcontrol_new *kc;
841 struct soc_enum *se;
842 int mux_index = hdmi->num_cvt + hdmi->num_pin;
843 int i, j;
844
845 for (i = 0; i < hdmi->num_pin; i++) {
846 kc = widgets[mux_index].kcontrol_news;
847 se = (struct soc_enum *)kc->private_value;
848 for (j = 0; j < hdmi->num_cvt; j++) {
849 hdac_hdmi_fill_route(&route[rindex],
850 widgets[mux_index].name,
851 se->texts[j + 1],
852 widgets[j].name, NULL);
853
854 rindex++;
855 }
856
857 mux_index++;
858 }
859 }
860
861 /*
862 * Widgets are added in the below sequence
863 * Converter widgets for num converters enumerated
864 * Pin widgets for num pins enumerated
865 * Pin mux widgets to represent connenction list of pin widget
866 *
867 * Total widgets elements = num_cvt + num_pin + num_pin;
868 *
869 * Routes are added as below:
870 * pin mux -> pin (based on num_pins)
871 * cvt -> "Input sel control" -> pin_mux
872 *
873 * Total route elements:
874 * num_pins + (pin_muxes * num_cvt)
875 */
876 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
877 {
878 struct snd_soc_dapm_widget *widgets;
879 struct snd_soc_dapm_route *route;
880 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
881 struct hdac_hdmi_priv *hdmi = edev->private_data;
882 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
883 char widget_name[NAME_SIZE];
884 struct hdac_hdmi_cvt *cvt;
885 struct hdac_hdmi_pin *pin;
886 int ret, i = 0, num_routes = 0;
887
888 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
889 return -EINVAL;
890
891 widgets = devm_kzalloc(dapm->dev,
892 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
893 GFP_KERNEL);
894
895 if (!widgets)
896 return -ENOMEM;
897
898 /* DAPM widgets to represent each converter widget */
899 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
900 sprintf(widget_name, "Converter %d", cvt->nid);
901 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
902 snd_soc_dapm_aif_in, &cvt->nid,
903 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
904 if (ret < 0)
905 return ret;
906 i++;
907 }
908
909 list_for_each_entry(pin, &hdmi->pin_list, head) {
910 sprintf(widget_name, "hif%d Output", pin->nid);
911 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
912 snd_soc_dapm_output, &pin->nid,
913 widget_name, NULL, NULL, 0);
914 if (ret < 0)
915 return ret;
916 i++;
917 }
918
919 /* DAPM widgets to represent the connection list to pin widget */
920 list_for_each_entry(pin, &hdmi->pin_list, head) {
921 sprintf(widget_name, "Pin %d Mux", pin->nid);
922 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
923 widget_name);
924 if (ret < 0)
925 return ret;
926 i++;
927
928 /* For cvt to pin_mux mapping */
929 num_routes += hdmi->num_cvt;
930
931 /* For pin_mux to pin mapping */
932 num_routes++;
933 }
934
935 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
936 GFP_KERNEL);
937 if (!route)
938 return -ENOMEM;
939
940 i = 0;
941 /* Add pin <- NULL <- mux route map */
942 list_for_each_entry(pin, &hdmi->pin_list, head) {
943 int sink_index = i + hdmi->num_cvt;
944 int src_index = sink_index + hdmi->num_pin;
945
946 hdac_hdmi_fill_route(&route[i],
947 widgets[sink_index].name, NULL,
948 widgets[src_index].name, NULL);
949 i++;
950
951 }
952
953 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
954
955 snd_soc_dapm_new_controls(dapm, widgets,
956 ((2 * hdmi->num_pin) + hdmi->num_cvt));
957
958 snd_soc_dapm_add_routes(dapm, route, num_routes);
959 snd_soc_dapm_new_widgets(dapm->card);
960
961 return 0;
962
963 }
964
965 static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
966 {
967 struct hdac_hdmi_priv *hdmi = edev->private_data;
968 struct hdac_hdmi_dai_pin_map *dai_map;
969 struct hdac_hdmi_cvt *cvt;
970 int dai_id = 0;
971
972 if (list_empty(&hdmi->cvt_list))
973 return -EINVAL;
974
975 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
976 dai_map = &hdmi->dai_map[dai_id];
977 dai_map->dai_id = dai_id;
978 dai_map->cvt = cvt;
979
980 dai_id++;
981
982 if (dai_id == HDA_MAX_CVTS) {
983 dev_warn(&edev->hdac.dev,
984 "Max dais supported: %d\n", dai_id);
985 break;
986 }
987 }
988
989 return 0;
990 }
991
992 static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
993 {
994 struct hdac_hdmi_priv *hdmi = edev->private_data;
995 struct hdac_hdmi_cvt *cvt;
996 char name[NAME_SIZE];
997
998 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
999 if (!cvt)
1000 return -ENOMEM;
1001
1002 cvt->nid = nid;
1003 sprintf(name, "cvt %d", cvt->nid);
1004 cvt->name = kstrdup(name, GFP_KERNEL);
1005
1006 list_add_tail(&cvt->head, &hdmi->cvt_list);
1007 hdmi->num_cvt++;
1008
1009 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1010 }
1011
1012 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1013 {
1014 struct hdac_ext_device *edev = pin->edev;
1015 struct hdac_hdmi_priv *hdmi = edev->private_data;
1016 struct hdac_hdmi_pcm *pcm;
1017 int val;
1018
1019 pin->repoll_count = repoll;
1020
1021 pm_runtime_get_sync(&edev->hdac.dev);
1022 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1023 AC_VERB_GET_PIN_SENSE, 0);
1024
1025 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1026 val, pin->nid);
1027
1028
1029 mutex_lock(&hdmi->pin_mutex);
1030 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1031 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1032
1033 pcm = hdac_hdmi_get_pcm(edev, pin);
1034
1035 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1036
1037 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1038 __func__, pin->nid);
1039
1040 /*
1041 * PCMs are not registered during device probe, so don't
1042 * report jack here. It will be done in usermode mux
1043 * control select.
1044 */
1045 if (pcm) {
1046 dev_dbg(&edev->hdac.dev,
1047 "jack report for pcm=%d\n", pcm->pcm_id);
1048
1049 snd_jack_report(pcm->jack, 0);
1050 }
1051
1052 mutex_unlock(&hdmi->pin_mutex);
1053 goto put_hdac_device;
1054 }
1055
1056 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1057 /* TODO: use i915 component for reading ELD later */
1058 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1059 pin->eld.eld_buffer,
1060 &pin->eld.eld_size) == 0) {
1061
1062 if (pcm) {
1063 dev_dbg(&edev->hdac.dev,
1064 "jack report for pcm=%d\n",
1065 pcm->pcm_id);
1066
1067 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1068 }
1069
1070 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1071 pin->eld.eld_buffer, pin->eld.eld_size);
1072 } else {
1073 pin->eld.monitor_present = false;
1074 pin->eld.eld_valid = false;
1075
1076 if (pcm) {
1077 dev_dbg(&edev->hdac.dev,
1078 "jack report for pcm=%d\n",
1079 pcm->pcm_id);
1080
1081 snd_jack_report(pcm->jack, 0);
1082 }
1083 }
1084 }
1085
1086 mutex_unlock(&hdmi->pin_mutex);
1087
1088 /*
1089 * Sometimes the pin_sense may present invalid monitor
1090 * present and eld_valid. If ELD data is not valid, loop few
1091 * more times to get correct pin sense and valid ELD.
1092 */
1093 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1094 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1095
1096 put_hdac_device:
1097 pm_runtime_put_sync(&edev->hdac.dev);
1098 }
1099
1100 static void hdac_hdmi_repoll_eld(struct work_struct *work)
1101 {
1102 struct hdac_hdmi_pin *pin =
1103 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1104
1105 /* picked from legacy HDA driver */
1106 if (pin->repoll_count++ > 6)
1107 pin->repoll_count = 0;
1108
1109 hdac_hdmi_present_sense(pin, pin->repoll_count);
1110 }
1111
1112 static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1113 {
1114 struct hdac_hdmi_priv *hdmi = edev->private_data;
1115 struct hdac_hdmi_pin *pin;
1116
1117 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1118 if (!pin)
1119 return -ENOMEM;
1120
1121 pin->nid = nid;
1122
1123 list_add_tail(&pin->head, &hdmi->pin_list);
1124 hdmi->num_pin++;
1125
1126 pin->edev = edev;
1127 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1128
1129 return 0;
1130 }
1131
1132 #define INTEL_VENDOR_NID 0x08
1133 #define INTEL_GET_VENDOR_VERB 0xf81
1134 #define INTEL_SET_VENDOR_VERB 0x781
1135 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1136 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1137
1138 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1139 {
1140 unsigned int vendor_param;
1141
1142 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1143 INTEL_GET_VENDOR_VERB, 0);
1144 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1145 return;
1146
1147 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1148 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1149 INTEL_SET_VENDOR_VERB, vendor_param);
1150 if (vendor_param == -1)
1151 return;
1152 }
1153
1154 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1155 {
1156 unsigned int vendor_param;
1157
1158 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1159 INTEL_GET_VENDOR_VERB, 0);
1160 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1161 return;
1162
1163 /* enable DP1.2 mode */
1164 vendor_param |= INTEL_EN_DP12;
1165 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1166 INTEL_SET_VENDOR_VERB, vendor_param);
1167 if (vendor_param == -1)
1168 return;
1169
1170 }
1171
1172 static struct snd_soc_dai_ops hdmi_dai_ops = {
1173 .startup = hdac_hdmi_pcm_open,
1174 .shutdown = hdac_hdmi_pcm_close,
1175 .hw_params = hdac_hdmi_set_hw_params,
1176 .prepare = hdac_hdmi_playback_prepare,
1177 .trigger = hdac_hdmi_trigger,
1178 .hw_free = hdac_hdmi_playback_cleanup,
1179 };
1180
1181 /*
1182 * Each converter can support a stream independently. So a dai is created
1183 * based on the number of converter queried.
1184 */
1185 static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1186 struct snd_soc_dai_driver **dais,
1187 struct hdac_hdmi_priv *hdmi, int num_dais)
1188 {
1189 struct snd_soc_dai_driver *hdmi_dais;
1190 struct hdac_hdmi_cvt *cvt;
1191 char name[NAME_SIZE], dai_name[NAME_SIZE];
1192 int i = 0;
1193 u32 rates, bps;
1194 unsigned int rate_max = 384000, rate_min = 8000;
1195 u64 formats;
1196 int ret;
1197
1198 hdmi_dais = devm_kzalloc(&hdac->dev,
1199 (sizeof(*hdmi_dais) * num_dais),
1200 GFP_KERNEL);
1201 if (!hdmi_dais)
1202 return -ENOMEM;
1203
1204 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1205 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1206 &rates, &formats, &bps);
1207 if (ret)
1208 return ret;
1209
1210 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1211 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1212 dai_name, GFP_KERNEL);
1213
1214 if (!hdmi_dais[i].name)
1215 return -ENOMEM;
1216
1217 snprintf(name, sizeof(name), "hifi%d", i+1);
1218 hdmi_dais[i].playback.stream_name =
1219 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1220 if (!hdmi_dais[i].playback.stream_name)
1221 return -ENOMEM;
1222
1223 /*
1224 * Set caps based on capability queried from the converter.
1225 * It will be constrained runtime based on ELD queried.
1226 */
1227 hdmi_dais[i].playback.formats = formats;
1228 hdmi_dais[i].playback.rates = rates;
1229 hdmi_dais[i].playback.rate_max = rate_max;
1230 hdmi_dais[i].playback.rate_min = rate_min;
1231 hdmi_dais[i].playback.channels_min = 2;
1232 hdmi_dais[i].playback.channels_max = 2;
1233 hdmi_dais[i].ops = &hdmi_dai_ops;
1234
1235 i++;
1236 }
1237
1238 *dais = hdmi_dais;
1239
1240 return 0;
1241 }
1242
1243 /*
1244 * Parse all nodes and store the cvt/pin nids in array
1245 * Add one time initialization for pin and cvt widgets
1246 */
1247 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1248 struct snd_soc_dai_driver **dais, int *num_dais)
1249 {
1250 hda_nid_t nid;
1251 int i, num_nodes;
1252 struct hdac_device *hdac = &edev->hdac;
1253 struct hdac_hdmi_priv *hdmi = edev->private_data;
1254 int ret;
1255
1256 hdac_hdmi_skl_enable_all_pins(hdac);
1257 hdac_hdmi_skl_enable_dp12(hdac);
1258
1259 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1260 if (!nid || num_nodes <= 0) {
1261 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1262 return -EINVAL;
1263 }
1264
1265 hdac->num_nodes = num_nodes;
1266 hdac->start_nid = nid;
1267
1268 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1269 unsigned int caps;
1270 unsigned int type;
1271
1272 caps = get_wcaps(hdac, nid);
1273 type = get_wcaps_type(caps);
1274
1275 if (!(caps & AC_WCAP_DIGITAL))
1276 continue;
1277
1278 switch (type) {
1279
1280 case AC_WID_AUD_OUT:
1281 ret = hdac_hdmi_add_cvt(edev, nid);
1282 if (ret < 0)
1283 return ret;
1284 break;
1285
1286 case AC_WID_PIN:
1287 ret = hdac_hdmi_add_pin(edev, nid);
1288 if (ret < 0)
1289 return ret;
1290 break;
1291 }
1292 }
1293
1294 hdac->end_nid = nid;
1295
1296 if (!hdmi->num_pin || !hdmi->num_cvt)
1297 return -EIO;
1298
1299 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1300 if (ret) {
1301 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1302 ret);
1303 return ret;
1304 }
1305
1306 *num_dais = hdmi->num_cvt;
1307
1308 return hdac_hdmi_init_dai_map(edev);
1309 }
1310
1311 static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1312 {
1313 struct hdac_ext_device *edev = aptr;
1314 struct hdac_hdmi_priv *hdmi = edev->private_data;
1315 struct hdac_hdmi_pin *pin;
1316 struct snd_soc_codec *codec = edev->scodec;
1317
1318 /* Don't know how this mapping is derived */
1319 hda_nid_t pin_nid = port + 0x04;
1320
1321 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1322
1323 /*
1324 * skip notification during system suspend (but not in runtime PM);
1325 * the state will be updated at resume. Also since the ELD and
1326 * connection states are updated in anyway at the end of the resume,
1327 * we can skip it when received during PM process.
1328 */
1329 if (snd_power_get_state(codec->component.card->snd_card) !=
1330 SNDRV_CTL_POWER_D0)
1331 return;
1332
1333 if (atomic_read(&edev->hdac.in_pm))
1334 return;
1335
1336 list_for_each_entry(pin, &hdmi->pin_list, head) {
1337 if (pin->nid == pin_nid)
1338 hdac_hdmi_present_sense(pin, 1);
1339 }
1340 }
1341
1342 static struct i915_audio_component_audio_ops aops = {
1343 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1344 };
1345
1346 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1347 {
1348 char jack_name[NAME_SIZE];
1349 struct snd_soc_codec *codec = dai->codec;
1350 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1351 struct snd_soc_dapm_context *dapm =
1352 snd_soc_component_get_dapm(&codec->component);
1353 struct hdac_hdmi_priv *hdmi = edev->private_data;
1354 struct hdac_hdmi_pcm *pcm;
1355
1356 /*
1357 * this is a new PCM device, create new pcm and
1358 * add to the pcm list
1359 */
1360 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1361 if (!pcm)
1362 return -ENOMEM;
1363 pcm->pcm_id = device;
1364 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1365
1366 list_add_tail(&pcm->head, &hdmi->pcm_list);
1367
1368 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1369
1370 return snd_jack_new(dapm->card->snd_card, jack_name,
1371 SND_JACK_AVOUT, &pcm->jack, true, false);
1372 }
1373 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1374
1375 static int hdmi_codec_probe(struct snd_soc_codec *codec)
1376 {
1377 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1378 struct hdac_hdmi_priv *hdmi = edev->private_data;
1379 struct snd_soc_dapm_context *dapm =
1380 snd_soc_component_get_dapm(&codec->component);
1381 struct hdac_hdmi_pin *pin;
1382 int ret;
1383
1384 edev->scodec = codec;
1385
1386 ret = create_fill_widget_route_map(dapm);
1387 if (ret < 0)
1388 return ret;
1389
1390 aops.audio_ptr = edev;
1391 ret = snd_hdac_i915_register_notifier(&aops);
1392 if (ret < 0) {
1393 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1394 ret);
1395 return ret;
1396 }
1397
1398 list_for_each_entry(pin, &hdmi->pin_list, head)
1399 hdac_hdmi_present_sense(pin, 1);
1400
1401 /* Imp: Store the card pointer in hda_codec */
1402 edev->card = dapm->card->snd_card;
1403
1404 /*
1405 * hdac_device core already sets the state to active and calls
1406 * get_noresume. So enable runtime and set the device to suspend.
1407 */
1408 pm_runtime_enable(&edev->hdac.dev);
1409 pm_runtime_put(&edev->hdac.dev);
1410 pm_runtime_suspend(&edev->hdac.dev);
1411
1412 return 0;
1413 }
1414
1415 static int hdmi_codec_remove(struct snd_soc_codec *codec)
1416 {
1417 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1418
1419 pm_runtime_disable(&edev->hdac.dev);
1420 return 0;
1421 }
1422
1423 #ifdef CONFIG_PM
1424 static int hdmi_codec_resume(struct snd_soc_codec *codec)
1425 {
1426 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1427 struct hdac_hdmi_priv *hdmi = edev->private_data;
1428 struct hdac_hdmi_pin *pin;
1429 struct hdac_device *hdac = &edev->hdac;
1430 struct hdac_bus *bus = hdac->bus;
1431 int err;
1432 unsigned long timeout;
1433
1434 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1435 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1436
1437 /* Power up afg */
1438 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1439
1440 snd_hdac_codec_write(hdac, hdac->afg, 0,
1441 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1442
1443 /* Wait till power state is set to D0 */
1444 timeout = jiffies + msecs_to_jiffies(1000);
1445 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1446 && time_before(jiffies, timeout)) {
1447 msleep(50);
1448 }
1449 }
1450
1451 /*
1452 * As the ELD notify callback request is not entertained while the
1453 * device is in suspend state. Need to manually check detection of
1454 * all pins here.
1455 */
1456 list_for_each_entry(pin, &hdmi->pin_list, head)
1457 hdac_hdmi_present_sense(pin, 1);
1458
1459 /*
1460 * Codec power is turned ON during controller resume.
1461 * Turn it OFF here
1462 */
1463 err = snd_hdac_display_power(bus, false);
1464 if (err < 0) {
1465 dev_err(bus->dev,
1466 "Cannot turn OFF display power on i915, err: %d\n",
1467 err);
1468 return err;
1469 }
1470
1471 return 0;
1472 }
1473 #else
1474 #define hdmi_codec_resume NULL
1475 #endif
1476
1477 static struct snd_soc_codec_driver hdmi_hda_codec = {
1478 .probe = hdmi_codec_probe,
1479 .remove = hdmi_codec_remove,
1480 .resume = hdmi_codec_resume,
1481 .idle_bias_off = true,
1482 };
1483
1484 static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1485 {
1486 struct hdac_device *codec = &edev->hdac;
1487 struct hdac_hdmi_priv *hdmi_priv;
1488 struct snd_soc_dai_driver *hdmi_dais = NULL;
1489 int num_dais = 0;
1490 int ret = 0;
1491
1492 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1493 if (hdmi_priv == NULL)
1494 return -ENOMEM;
1495
1496 edev->private_data = hdmi_priv;
1497
1498 dev_set_drvdata(&codec->dev, edev);
1499
1500 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1501 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1502 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1503 mutex_init(&hdmi_priv->pin_mutex);
1504
1505 /*
1506 * Turned off in the runtime_suspend during the first explicit
1507 * pm_runtime_suspend call.
1508 */
1509 ret = snd_hdac_display_power(edev->hdac.bus, true);
1510 if (ret < 0) {
1511 dev_err(&edev->hdac.dev,
1512 "Cannot turn on display power on i915 err: %d\n",
1513 ret);
1514 return ret;
1515 }
1516
1517 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1518 if (ret < 0) {
1519 dev_err(&codec->dev,
1520 "Failed in parse and map nid with err: %d\n", ret);
1521 return ret;
1522 }
1523
1524 /* ASoC specific initialization */
1525 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1526 hdmi_dais, num_dais);
1527 }
1528
1529 static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1530 {
1531 struct hdac_hdmi_priv *hdmi = edev->private_data;
1532 struct hdac_hdmi_pin *pin, *pin_next;
1533 struct hdac_hdmi_cvt *cvt, *cvt_next;
1534 struct hdac_hdmi_pcm *pcm, *pcm_next;
1535
1536 snd_soc_unregister_codec(&edev->hdac.dev);
1537
1538 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1539 pcm->cvt = NULL;
1540 pcm->pin = NULL;
1541 list_del(&pcm->head);
1542 kfree(pcm);
1543 }
1544
1545 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1546 list_del(&cvt->head);
1547 kfree(cvt->name);
1548 kfree(cvt);
1549 }
1550
1551 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1552 list_del(&pin->head);
1553 kfree(pin);
1554 }
1555
1556 return 0;
1557 }
1558
1559 #ifdef CONFIG_PM
1560 static int hdac_hdmi_runtime_suspend(struct device *dev)
1561 {
1562 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1563 struct hdac_device *hdac = &edev->hdac;
1564 struct hdac_bus *bus = hdac->bus;
1565 unsigned long timeout;
1566 int err;
1567
1568 dev_dbg(dev, "Enter: %s\n", __func__);
1569
1570 /* controller may not have been initialized for the first time */
1571 if (!bus)
1572 return 0;
1573
1574 /* Power down afg */
1575 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
1576 snd_hdac_codec_write(hdac, hdac->afg, 0,
1577 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1578
1579 /* Wait till power state is set to D3 */
1580 timeout = jiffies + msecs_to_jiffies(1000);
1581 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
1582 && time_before(jiffies, timeout)) {
1583
1584 msleep(50);
1585 }
1586 }
1587
1588 err = snd_hdac_display_power(bus, false);
1589 if (err < 0) {
1590 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1591 return err;
1592 }
1593
1594 return 0;
1595 }
1596
1597 static int hdac_hdmi_runtime_resume(struct device *dev)
1598 {
1599 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1600 struct hdac_device *hdac = &edev->hdac;
1601 struct hdac_bus *bus = hdac->bus;
1602 int err;
1603
1604 dev_dbg(dev, "Enter: %s\n", __func__);
1605
1606 /* controller may not have been initialized for the first time */
1607 if (!bus)
1608 return 0;
1609
1610 err = snd_hdac_display_power(bus, true);
1611 if (err < 0) {
1612 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1613 return err;
1614 }
1615
1616 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1617 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1618
1619 /* Power up afg */
1620 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1621 snd_hdac_codec_write(hdac, hdac->afg, 0,
1622 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1623
1624 return 0;
1625 }
1626 #else
1627 #define hdac_hdmi_runtime_suspend NULL
1628 #define hdac_hdmi_runtime_resume NULL
1629 #endif
1630
1631 static const struct dev_pm_ops hdac_hdmi_pm = {
1632 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1633 };
1634
1635 static const struct hda_device_id hdmi_list[] = {
1636 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1637 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
1638 {}
1639 };
1640
1641 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1642
1643 static struct hdac_ext_driver hdmi_driver = {
1644 . hdac = {
1645 .driver = {
1646 .name = "HDMI HDA Codec",
1647 .pm = &hdac_hdmi_pm,
1648 },
1649 .id_table = hdmi_list,
1650 },
1651 .probe = hdac_hdmi_dev_probe,
1652 .remove = hdac_hdmi_dev_remove,
1653 };
1654
1655 static int __init hdmi_init(void)
1656 {
1657 return snd_hda_ext_driver_register(&hdmi_driver);
1658 }
1659
1660 static void __exit hdmi_exit(void)
1661 {
1662 snd_hda_ext_driver_unregister(&hdmi_driver);
1663 }
1664
1665 module_init(hdmi_init);
1666 module_exit(hdmi_exit);
1667
1668 MODULE_LICENSE("GPL v2");
1669 MODULE_DESCRIPTION("HDMI HD codec");
1670 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1671 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
This page took 0.074164 seconds and 5 git commands to generate.