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