ASoC: hdac_hdmi: Remove 'edev' NULL check
[cascardo/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         if (dai->id > 0) {
378                 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
379                 return -ENODEV;
380         }
381
382         dai_map = &hdmi->dai_map[dai->id];
383
384         dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
385         dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
386                         dd->stream_tag, dd->format);
387
388         ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
389                                                 dai_map->pin->nid);
390         if (ret < 0)
391                 return ret;
392
393         return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
394                         dai_map->pin->nid, dd->stream_tag, dd->format);
395 }
396
397 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
398         struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
399 {
400         struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
401         struct hdac_ext_dma_params *dd;
402
403         if (dai->id > 0) {
404                 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
405                 return -ENODEV;
406         }
407
408         dd = kzalloc(sizeof(*dd), GFP_KERNEL);
409         if (!dd)
410                 return -ENOMEM;
411         dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
412                         params_channels(hparams), params_format(hparams),
413                         24, 0);
414
415         snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
416
417         return 0;
418 }
419
420 static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
421                 struct snd_soc_dai *dai)
422 {
423         struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
424         struct hdac_ext_dma_params *dd;
425         struct hdac_hdmi_priv *hdmi = edev->private_data;
426         struct hdac_hdmi_dai_pin_map *dai_map;
427
428         dai_map = &hdmi->dai_map[dai->id];
429
430         snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
431                                 AC_VERB_SET_CHANNEL_STREAMID, 0);
432         snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
433                                 AC_VERB_SET_STREAM_FORMAT, 0);
434
435         dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
436         snd_soc_dai_set_dma_data(dai, substream, NULL);
437
438         kfree(dd);
439
440         return 0;
441 }
442
443 static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
444                 struct hdac_hdmi_dai_pin_map *dai_map)
445 {
446         int mux_idx;
447         struct hdac_hdmi_pin *pin = dai_map->pin;
448
449         for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
450                 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
451                         snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
452                                         AC_VERB_SET_CONNECT_SEL, mux_idx);
453                         break;
454                 }
455         }
456
457         if (mux_idx == pin->num_mux_nids)
458                 return -EIO;
459
460         /* Enable out path for this pin widget */
461         snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
462                         AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
463
464         hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
465
466         snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
467                         AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
468
469         return 0;
470 }
471
472 static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
473                                         struct hdac_hdmi_pin *pin)
474 {
475         if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
476                 dev_warn(&hdac->hdac.dev,
477                         "HDMI: pin %d wcaps %#x does not support connection list\n",
478                         pin->nid, get_wcaps(&hdac->hdac, pin->nid));
479                 return -EINVAL;
480         }
481
482         pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
483                         pin->mux_nids, HDA_MAX_CONNECTIONS);
484         if (pin->num_mux_nids == 0)
485                 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
486                                                                 pin->nid);
487
488         dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
489                         pin->num_mux_nids, pin->nid);
490
491         return pin->num_mux_nids;
492 }
493
494 /*
495  * Query pcm list and return pin widget to which stream is routed.
496  *
497  * Also query connection list of the pin, to validate the cvt to pin map.
498  *
499  * Same stream rendering to multiple pins simultaneously can be done
500  * possibly, but not supported for now in driver. So return the first pin
501  * connected.
502  */
503 static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
504                         struct hdac_ext_device *edev,
505                         struct hdac_hdmi_priv *hdmi,
506                         struct hdac_hdmi_cvt *cvt)
507 {
508         struct hdac_hdmi_pcm *pcm;
509         struct hdac_hdmi_pin *pin = NULL;
510         int ret, i;
511
512         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
513                 if (pcm->cvt == cvt) {
514                         pin = pcm->pin;
515                         break;
516                 }
517         }
518
519         if (pin) {
520                 ret = hdac_hdmi_query_pin_connlist(edev, pin);
521                 if (ret < 0)
522                         return NULL;
523
524                 for (i = 0; i < pin->num_mux_nids; i++) {
525                         if (pin->mux_nids[i] == cvt->nid)
526                                 return pin;
527                 }
528         }
529
530         return NULL;
531 }
532
533 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
534                         struct snd_soc_dai *dai)
535 {
536         struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
537         struct hdac_hdmi_priv *hdmi = hdac->private_data;
538         struct hdac_hdmi_dai_pin_map *dai_map;
539         struct hdac_hdmi_cvt *cvt;
540         struct hdac_hdmi_pin *pin;
541         int ret;
542
543         dai_map = &hdmi->dai_map[dai->id];
544
545         cvt = dai_map->cvt;
546         pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
547         if (!pin)
548                 return -EIO;
549
550         if ((!pin->eld.monitor_present) ||
551                         (!pin->eld.eld_valid)) {
552
553                 dev_err(&hdac->hdac.dev,
554                         "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
555                         pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
556
557                 return -ENODEV;
558         }
559
560         dai_map->pin = pin;
561
562         ret = hdac_hdmi_enable_pin(hdac, dai_map);
563         if (ret < 0)
564                 return ret;
565
566         ret = hdac_hdmi_eld_limit_formats(substream->runtime,
567                                 pin->eld.eld_buffer);
568         if (ret < 0)
569                 return ret;
570
571         return snd_pcm_hw_constraint_eld(substream->runtime,
572                                 pin->eld.eld_buffer);
573 }
574
575 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
576                 struct snd_soc_dai *dai)
577 {
578         struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
579         struct hdac_hdmi_priv *hdmi = hdac->private_data;
580         struct hdac_hdmi_dai_pin_map *dai_map;
581
582         dai_map = &hdmi->dai_map[dai->id];
583
584         hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
585
586         snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
587                         AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
588
589         dai_map->pin = NULL;
590 }
591
592 static int
593 hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
594 {
595         int err;
596
597         /* Only stereo supported as of now */
598         cvt->params.channels_min = cvt->params.channels_max = 2;
599
600         err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
601                         &cvt->params.rates,
602                         &cvt->params.formats,
603                         &cvt->params.maxbps);
604         if (err < 0)
605                 dev_err(&hdac->dev,
606                         "Failed to query pcm params for nid %d: %d\n",
607                         cvt->nid, err);
608
609         return err;
610 }
611
612 static int hdac_hdmi_fill_widget_info(struct device *dev,
613                                 struct snd_soc_dapm_widget *w,
614                                 enum snd_soc_dapm_type id, void *priv,
615                                 const char *wname, const char *stream,
616                                 struct snd_kcontrol_new *wc, int numkc)
617 {
618         w->id = id;
619         w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
620         if (!w->name)
621                 return -ENOMEM;
622
623         w->sname = stream;
624         w->reg = SND_SOC_NOPM;
625         w->shift = 0;
626         w->kcontrol_news = wc;
627         w->num_kcontrols = numkc;
628         w->priv = priv;
629
630         return 0;
631 }
632
633 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
634                 const char *sink, const char *control, const char *src,
635                 int (*handler)(struct snd_soc_dapm_widget *src,
636                         struct snd_soc_dapm_widget *sink))
637 {
638         route->sink = sink;
639         route->source = src;
640         route->control = control;
641         route->connected = handler;
642 }
643
644 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
645                                         struct hdac_hdmi_pin *pin)
646 {
647         struct hdac_hdmi_priv *hdmi = edev->private_data;
648         struct hdac_hdmi_pcm *pcm = NULL;
649
650         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
651                 if (pcm->pin == pin)
652                         return pcm;
653         }
654
655         return NULL;
656 }
657
658 /*
659  * Based on user selection, map the PINs with the PCMs.
660  */
661 static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
662                 struct snd_ctl_elem_value *ucontrol)
663 {
664         int ret;
665         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
666         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
667         struct snd_soc_dapm_context *dapm = w->dapm;
668         struct hdac_hdmi_pin *pin = w->priv;
669         struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
670         struct hdac_hdmi_priv *hdmi = edev->private_data;
671         struct hdac_hdmi_pcm *pcm = NULL;
672         const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
673
674         ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
675         if (ret < 0)
676                 return ret;
677
678         mutex_lock(&hdmi->pin_mutex);
679         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
680                 if (pcm->pin == pin)
681                         pcm->pin = NULL;
682
683                 /*
684                  * Jack status is not reported during device probe as the
685                  * PCMs are not registered by then. So report it here.
686                  */
687                 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
688                         pcm->pin = pin;
689                         if (pin->eld.monitor_present && pin->eld.eld_valid) {
690                                 dev_dbg(&edev->hdac.dev,
691                                         "jack report for pcm=%d\n",
692                                         pcm->pcm_id);
693
694                                 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
695                         }
696                         mutex_unlock(&hdmi->pin_mutex);
697                         return ret;
698                 }
699         }
700         mutex_unlock(&hdmi->pin_mutex);
701
702         return ret;
703 }
704
705 /*
706  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
707  * the display driver seem to be programming the connection list for the pin
708  * widget runtime.
709  *
710  * So programming all the possible inputs for the mux, the user has to take
711  * care of selecting the right one and leaving all other inputs selected to
712  * "NONE"
713  */
714 static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
715                                 struct hdac_hdmi_pin *pin,
716                                 struct snd_soc_dapm_widget *widget,
717                                 const char *widget_name)
718 {
719         struct hdac_hdmi_priv *hdmi = edev->private_data;
720         struct snd_kcontrol_new *kc;
721         struct hdac_hdmi_cvt *cvt;
722         struct soc_enum *se;
723         char kc_name[NAME_SIZE];
724         char mux_items[NAME_SIZE];
725         /* To hold inputs to the Pin mux */
726         char *items[HDA_MAX_CONNECTIONS];
727         int i = 0;
728         int num_items = hdmi->num_cvt + 1;
729
730         kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
731         if (!kc)
732                 return -ENOMEM;
733
734         se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
735         if (!se)
736                 return -ENOMEM;
737
738         sprintf(kc_name, "Pin %d Input", pin->nid);
739         kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
740         if (!kc->name)
741                 return -ENOMEM;
742
743         kc->private_value = (long)se;
744         kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
745         kc->access = 0;
746         kc->info = snd_soc_info_enum_double;
747         kc->put = hdac_hdmi_set_pin_mux;
748         kc->get = snd_soc_dapm_get_enum_double;
749
750         se->reg = SND_SOC_NOPM;
751
752         /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
753         se->items = num_items;
754         se->mask = roundup_pow_of_two(se->items) - 1;
755
756         sprintf(mux_items, "NONE");
757         items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
758         if (!items[i])
759                 return -ENOMEM;
760
761         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
762                 i++;
763                 sprintf(mux_items, "cvt %d", cvt->nid);
764                 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
765                 if (!items[i])
766                         return -ENOMEM;
767         }
768
769         se->texts = devm_kmemdup(&edev->hdac.dev, items,
770                         (num_items  * sizeof(char *)), GFP_KERNEL);
771         if (!se->texts)
772                 return -ENOMEM;
773
774         return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
775                         snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
776 }
777
778 /* Add cvt <- input <- mux route map */
779 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
780                         struct snd_soc_dapm_widget *widgets,
781                         struct snd_soc_dapm_route *route, int rindex)
782 {
783         struct hdac_hdmi_priv *hdmi = edev->private_data;
784         const struct snd_kcontrol_new *kc;
785         struct soc_enum *se;
786         int mux_index = hdmi->num_cvt + hdmi->num_pin;
787         int i, j;
788
789         for (i = 0; i < hdmi->num_pin; i++) {
790                 kc = widgets[mux_index].kcontrol_news;
791                 se = (struct soc_enum *)kc->private_value;
792                 for (j = 0; j < hdmi->num_cvt; j++) {
793                         hdac_hdmi_fill_route(&route[rindex],
794                                         widgets[mux_index].name,
795                                         se->texts[j + 1],
796                                         widgets[j].name, NULL);
797
798                         rindex++;
799                 }
800
801                 mux_index++;
802         }
803 }
804
805 /*
806  * Widgets are added in the below sequence
807  *      Converter widgets for num converters enumerated
808  *      Pin widgets for num pins enumerated
809  *      Pin mux widgets to represent connenction list of pin widget
810  *
811  * Total widgets elements = num_cvt + num_pin + num_pin;
812  *
813  * Routes are added as below:
814  *      pin mux -> pin (based on num_pins)
815  *      cvt -> "Input sel control" -> pin_mux
816  *
817  * Total route elements:
818  *      num_pins + (pin_muxes * num_cvt)
819  */
820 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
821 {
822         struct snd_soc_dapm_widget *widgets;
823         struct snd_soc_dapm_route *route;
824         struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
825         struct hdac_hdmi_priv *hdmi = edev->private_data;
826         struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
827         char widget_name[NAME_SIZE];
828         struct hdac_hdmi_cvt *cvt;
829         struct hdac_hdmi_pin *pin;
830         int ret, i = 0, num_routes = 0;
831
832         if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
833                 return -EINVAL;
834
835         widgets = devm_kzalloc(dapm->dev,
836                 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
837                 GFP_KERNEL);
838
839         if (!widgets)
840                 return -ENOMEM;
841
842         /* DAPM widgets to represent each converter widget */
843         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
844                 sprintf(widget_name, "Converter %d", cvt->nid);
845                 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
846                         snd_soc_dapm_aif_in, &cvt->nid,
847                         widget_name, dai_drv[i].playback.stream_name, NULL, 0);
848                 if (ret < 0)
849                         return ret;
850                 i++;
851         }
852
853         list_for_each_entry(pin, &hdmi->pin_list, head) {
854                 sprintf(widget_name, "hif%d Output", pin->nid);
855                 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
856                                 snd_soc_dapm_output, &pin->nid,
857                                 widget_name, NULL, NULL, 0);
858                 if (ret < 0)
859                         return ret;
860                 i++;
861         }
862
863         /* DAPM widgets to represent the connection list to pin widget */
864         list_for_each_entry(pin, &hdmi->pin_list, head) {
865                 sprintf(widget_name, "Pin %d Mux", pin->nid);
866                 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
867                                                         widget_name);
868                 if (ret < 0)
869                         return ret;
870                 i++;
871
872                 /* For cvt to pin_mux mapping */
873                 num_routes += hdmi->num_cvt;
874
875                 /* For pin_mux to pin mapping */
876                 num_routes++;
877         }
878
879         route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
880                                                         GFP_KERNEL);
881         if (!route)
882                 return -ENOMEM;
883
884         i = 0;
885         /* Add pin <- NULL <- mux route map */
886         list_for_each_entry(pin, &hdmi->pin_list, head) {
887                 int sink_index = i + hdmi->num_cvt;
888                 int src_index = sink_index + hdmi->num_pin;
889
890                 hdac_hdmi_fill_route(&route[i],
891                                 widgets[sink_index].name, NULL,
892                                 widgets[src_index].name, NULL);
893                 i++;
894
895         }
896
897         hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
898
899         snd_soc_dapm_new_controls(dapm, widgets,
900                 ((2 * hdmi->num_pin) + hdmi->num_cvt));
901
902         snd_soc_dapm_add_routes(dapm, route, num_routes);
903         snd_soc_dapm_new_widgets(dapm->card);
904
905         return 0;
906
907 }
908
909 static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
910 {
911         struct hdac_hdmi_priv *hdmi = edev->private_data;
912         struct hdac_hdmi_dai_pin_map *dai_map;
913         struct hdac_hdmi_cvt *cvt;
914         int dai_id = 0;
915
916         if (list_empty(&hdmi->cvt_list))
917                 return -EINVAL;
918
919         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
920                 dai_map = &hdmi->dai_map[dai_id];
921                 dai_map->dai_id = dai_id;
922                 dai_map->cvt = cvt;
923
924                 /* Enable transmission */
925                 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
926                                 AC_VERB_SET_DIGI_CONVERT_1, 1);
927
928                 /* Category Code (CC) to zero */
929                 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
930                                 AC_VERB_SET_DIGI_CONVERT_2, 0);
931
932                 dai_id++;
933
934                 if (dai_id == HDA_MAX_CVTS) {
935                         dev_warn(&edev->hdac.dev,
936                                 "Max dais supported: %d\n", dai_id);
937                         break;
938                 }
939         }
940
941         return 0;
942 }
943
944 static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
945 {
946         struct hdac_hdmi_priv *hdmi = edev->private_data;
947         struct hdac_hdmi_cvt *cvt;
948         char name[NAME_SIZE];
949
950         cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
951         if (!cvt)
952                 return -ENOMEM;
953
954         cvt->nid = nid;
955         sprintf(name, "cvt %d", cvt->nid);
956         cvt->name = kstrdup(name, GFP_KERNEL);
957
958         list_add_tail(&cvt->head, &hdmi->cvt_list);
959         hdmi->num_cvt++;
960
961         return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
962 }
963
964 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
965 {
966         struct hdac_ext_device *edev = pin->edev;
967         struct hdac_hdmi_priv *hdmi = edev->private_data;
968         struct hdac_hdmi_pcm *pcm;
969         int val;
970
971         pin->repoll_count = repoll;
972
973         pm_runtime_get_sync(&edev->hdac.dev);
974         val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
975                                         AC_VERB_GET_PIN_SENSE, 0);
976
977         dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
978                                                 val, pin->nid);
979
980
981         mutex_lock(&hdmi->pin_mutex);
982         pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
983         pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
984
985         pcm = hdac_hdmi_get_pcm(edev, pin);
986
987         if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
988
989                 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
990                                                 __func__, pin->nid);
991
992                 /*
993                  * PCMs are not registered during device probe, so don't
994                  * report jack here. It will be done in usermode mux
995                  * control select.
996                  */
997                 if (pcm) {
998                         dev_dbg(&edev->hdac.dev,
999                                 "jack report for pcm=%d\n", pcm->pcm_id);
1000
1001                         snd_jack_report(pcm->jack, 0);
1002                 }
1003
1004                 mutex_unlock(&hdmi->pin_mutex);
1005                 goto put_hdac_device;
1006         }
1007
1008         if (pin->eld.monitor_present && pin->eld.eld_valid) {
1009                 /* TODO: use i915 component for reading ELD later */
1010                 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1011                                 pin->eld.eld_buffer,
1012                                 &pin->eld.eld_size) == 0) {
1013
1014                         if (pcm) {
1015                                 dev_dbg(&edev->hdac.dev,
1016                                         "jack report for pcm=%d\n",
1017                                         pcm->pcm_id);
1018
1019                                 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1020                         }
1021
1022                         print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1023                                         pin->eld.eld_buffer, pin->eld.eld_size);
1024                 } else {
1025                         pin->eld.monitor_present = false;
1026                         pin->eld.eld_valid = false;
1027
1028                         if (pcm) {
1029                                 dev_dbg(&edev->hdac.dev,
1030                                         "jack report for pcm=%d\n",
1031                                         pcm->pcm_id);
1032
1033                                 snd_jack_report(pcm->jack, 0);
1034                         }
1035                 }
1036         }
1037
1038         mutex_unlock(&hdmi->pin_mutex);
1039
1040         /*
1041          * Sometimes the pin_sense may present invalid monitor
1042          * present and eld_valid. If ELD data is not valid, loop few
1043          * more times to get correct pin sense and valid ELD.
1044          */
1045         if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1046                 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1047
1048 put_hdac_device:
1049         pm_runtime_put_sync(&edev->hdac.dev);
1050 }
1051
1052 static void hdac_hdmi_repoll_eld(struct work_struct *work)
1053 {
1054         struct hdac_hdmi_pin *pin =
1055                 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1056
1057         /* picked from legacy HDA driver */
1058         if (pin->repoll_count++ > 6)
1059                 pin->repoll_count = 0;
1060
1061         hdac_hdmi_present_sense(pin, pin->repoll_count);
1062 }
1063
1064 static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1065 {
1066         struct hdac_hdmi_priv *hdmi = edev->private_data;
1067         struct hdac_hdmi_pin *pin;
1068
1069         pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1070         if (!pin)
1071                 return -ENOMEM;
1072
1073         pin->nid = nid;
1074
1075         list_add_tail(&pin->head, &hdmi->pin_list);
1076         hdmi->num_pin++;
1077
1078         pin->edev = edev;
1079         INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1080
1081         return 0;
1082 }
1083
1084 #define INTEL_VENDOR_NID 0x08
1085 #define INTEL_GET_VENDOR_VERB 0xf81
1086 #define INTEL_SET_VENDOR_VERB 0x781
1087 #define INTEL_EN_DP12                   0x02 /* enable DP 1.2 features */
1088 #define INTEL_EN_ALL_PIN_CVTS   0x01 /* enable 2nd & 3rd pins and convertors */
1089
1090 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1091 {
1092         unsigned int vendor_param;
1093
1094         vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1095                                 INTEL_GET_VENDOR_VERB, 0);
1096         if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1097                 return;
1098
1099         vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1100         vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1101                                 INTEL_SET_VENDOR_VERB, vendor_param);
1102         if (vendor_param == -1)
1103                 return;
1104 }
1105
1106 static void hdac_hdmi_skl_enable_dp12(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_DP12)
1113                 return;
1114
1115         /* enable DP1.2 mode */
1116         vendor_param |= INTEL_EN_DP12;
1117         vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1118                                 INTEL_SET_VENDOR_VERB, vendor_param);
1119         if (vendor_param == -1)
1120                 return;
1121
1122 }
1123
1124 static struct snd_soc_dai_ops hdmi_dai_ops = {
1125         .startup = hdac_hdmi_pcm_open,
1126         .shutdown = hdac_hdmi_pcm_close,
1127         .hw_params = hdac_hdmi_set_hw_params,
1128         .prepare = hdac_hdmi_playback_prepare,
1129         .hw_free = hdac_hdmi_playback_cleanup,
1130 };
1131
1132 /*
1133  * Each converter can support a stream independently. So a dai is created
1134  * based on the number of converter queried.
1135  */
1136 static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1137                 struct snd_soc_dai_driver **dais,
1138                 struct hdac_hdmi_priv *hdmi, int num_dais)
1139 {
1140         struct snd_soc_dai_driver *hdmi_dais;
1141         struct hdac_hdmi_cvt *cvt;
1142         char name[NAME_SIZE], dai_name[NAME_SIZE];
1143         int i = 0;
1144         u32 rates, bps;
1145         unsigned int rate_max = 384000, rate_min = 8000;
1146         u64 formats;
1147         int ret;
1148
1149         hdmi_dais = devm_kzalloc(&hdac->dev,
1150                         (sizeof(*hdmi_dais) * num_dais),
1151                         GFP_KERNEL);
1152         if (!hdmi_dais)
1153                 return -ENOMEM;
1154
1155         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1156                 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1157                                         &rates, &formats, &bps);
1158                 if (ret)
1159                         return ret;
1160
1161                 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1162                 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1163                                         dai_name, GFP_KERNEL);
1164
1165                 if (!hdmi_dais[i].name)
1166                         return -ENOMEM;
1167
1168                 snprintf(name, sizeof(name), "hifi%d", i+1);
1169                 hdmi_dais[i].playback.stream_name =
1170                                 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1171                 if (!hdmi_dais[i].playback.stream_name)
1172                         return -ENOMEM;
1173
1174                 /*
1175                  * Set caps based on capability queried from the converter.
1176                  * It will be constrained runtime based on ELD queried.
1177                  */
1178                 hdmi_dais[i].playback.formats = formats;
1179                 hdmi_dais[i].playback.rates = rates;
1180                 hdmi_dais[i].playback.rate_max = rate_max;
1181                 hdmi_dais[i].playback.rate_min = rate_min;
1182                 hdmi_dais[i].playback.channels_min = 2;
1183                 hdmi_dais[i].playback.channels_max = 2;
1184                 hdmi_dais[i].ops = &hdmi_dai_ops;
1185
1186                 i++;
1187         }
1188
1189         *dais = hdmi_dais;
1190
1191         return 0;
1192 }
1193
1194 /*
1195  * Parse all nodes and store the cvt/pin nids in array
1196  * Add one time initialization for pin and cvt widgets
1197  */
1198 static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1199                 struct snd_soc_dai_driver **dais, int *num_dais)
1200 {
1201         hda_nid_t nid;
1202         int i, num_nodes;
1203         struct hdac_device *hdac = &edev->hdac;
1204         struct hdac_hdmi_priv *hdmi = edev->private_data;
1205         int ret;
1206
1207         hdac_hdmi_skl_enable_all_pins(hdac);
1208         hdac_hdmi_skl_enable_dp12(hdac);
1209
1210         num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
1211         if (!nid || num_nodes <= 0) {
1212                 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1213                 return -EINVAL;
1214         }
1215
1216         hdac->num_nodes = num_nodes;
1217         hdac->start_nid = nid;
1218
1219         for (i = 0; i < hdac->num_nodes; i++, nid++) {
1220                 unsigned int caps;
1221                 unsigned int type;
1222
1223                 caps = get_wcaps(hdac, nid);
1224                 type = get_wcaps_type(caps);
1225
1226                 if (!(caps & AC_WCAP_DIGITAL))
1227                         continue;
1228
1229                 switch (type) {
1230
1231                 case AC_WID_AUD_OUT:
1232                         ret = hdac_hdmi_add_cvt(edev, nid);
1233                         if (ret < 0)
1234                                 return ret;
1235                         break;
1236
1237                 case AC_WID_PIN:
1238                         ret = hdac_hdmi_add_pin(edev, nid);
1239                         if (ret < 0)
1240                                 return ret;
1241                         break;
1242                 }
1243         }
1244
1245         hdac->end_nid = nid;
1246
1247         if (!hdmi->num_pin || !hdmi->num_cvt)
1248                 return -EIO;
1249
1250         ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1251         if (ret) {
1252                 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1253                                                         ret);
1254                 return ret;
1255         }
1256
1257         *num_dais = hdmi->num_cvt;
1258
1259         return hdac_hdmi_init_dai_map(edev);
1260 }
1261
1262 static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1263 {
1264         struct hdac_ext_device *edev = aptr;
1265         struct hdac_hdmi_priv *hdmi = edev->private_data;
1266         struct hdac_hdmi_pin *pin;
1267         struct snd_soc_codec *codec = edev->scodec;
1268
1269         /* Don't know how this mapping is derived */
1270         hda_nid_t pin_nid = port + 0x04;
1271
1272         dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1273
1274         /*
1275          * skip notification during system suspend (but not in runtime PM);
1276          * the state will be updated at resume. Also since the ELD and
1277          * connection states are updated in anyway at the end of the resume,
1278          * we can skip it when received during PM process.
1279          */
1280         if (snd_power_get_state(codec->component.card->snd_card) !=
1281                         SNDRV_CTL_POWER_D0)
1282                 return;
1283
1284         if (atomic_read(&edev->hdac.in_pm))
1285                 return;
1286
1287         list_for_each_entry(pin, &hdmi->pin_list, head) {
1288                 if (pin->nid == pin_nid)
1289                         hdac_hdmi_present_sense(pin, 1);
1290         }
1291 }
1292
1293 static struct i915_audio_component_audio_ops aops = {
1294         .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1295 };
1296
1297 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1298 {
1299         char jack_name[NAME_SIZE];
1300         struct snd_soc_codec *codec = dai->codec;
1301         struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1302         struct snd_soc_dapm_context *dapm =
1303                 snd_soc_component_get_dapm(&codec->component);
1304         struct hdac_hdmi_priv *hdmi = edev->private_data;
1305         struct hdac_hdmi_pcm *pcm;
1306
1307         /*
1308          * this is a new PCM device, create new pcm and
1309          * add to the pcm list
1310          */
1311         pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1312         if (!pcm)
1313                 return -ENOMEM;
1314         pcm->pcm_id = device;
1315         pcm->cvt = hdmi->dai_map[dai->id].cvt;
1316
1317         list_add_tail(&pcm->head, &hdmi->pcm_list);
1318
1319         sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1320
1321         return snd_jack_new(dapm->card->snd_card, jack_name,
1322                 SND_JACK_AVOUT, &pcm->jack, true, false);
1323 }
1324 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1325
1326 static int hdmi_codec_probe(struct snd_soc_codec *codec)
1327 {
1328         struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1329         struct hdac_hdmi_priv *hdmi = edev->private_data;
1330         struct snd_soc_dapm_context *dapm =
1331                 snd_soc_component_get_dapm(&codec->component);
1332         struct hdac_hdmi_pin *pin;
1333         int ret;
1334
1335         edev->scodec = codec;
1336
1337         ret = create_fill_widget_route_map(dapm);
1338         if (ret < 0)
1339                 return ret;
1340
1341         aops.audio_ptr = edev;
1342         ret = snd_hdac_i915_register_notifier(&aops);
1343         if (ret < 0) {
1344                 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1345                                 ret);
1346                 return ret;
1347         }
1348
1349         list_for_each_entry(pin, &hdmi->pin_list, head)
1350                 hdac_hdmi_present_sense(pin, 1);
1351
1352         /* Imp: Store the card pointer in hda_codec */
1353         edev->card = dapm->card->snd_card;
1354
1355         /*
1356          * hdac_device core already sets the state to active and calls
1357          * get_noresume. So enable runtime and set the device to suspend.
1358          */
1359         pm_runtime_enable(&edev->hdac.dev);
1360         pm_runtime_put(&edev->hdac.dev);
1361         pm_runtime_suspend(&edev->hdac.dev);
1362
1363         return 0;
1364 }
1365
1366 static int hdmi_codec_remove(struct snd_soc_codec *codec)
1367 {
1368         struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1369
1370         pm_runtime_disable(&edev->hdac.dev);
1371         return 0;
1372 }
1373
1374 static struct snd_soc_codec_driver hdmi_hda_codec = {
1375         .probe          = hdmi_codec_probe,
1376         .remove         = hdmi_codec_remove,
1377         .idle_bias_off  = true,
1378 };
1379
1380 static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1381 {
1382         struct hdac_device *codec = &edev->hdac;
1383         struct hdac_hdmi_priv *hdmi_priv;
1384         struct snd_soc_dai_driver *hdmi_dais = NULL;
1385         int num_dais = 0;
1386         int ret = 0;
1387
1388         hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1389         if (hdmi_priv == NULL)
1390                 return -ENOMEM;
1391
1392         edev->private_data = hdmi_priv;
1393
1394         dev_set_drvdata(&codec->dev, edev);
1395
1396         INIT_LIST_HEAD(&hdmi_priv->pin_list);
1397         INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1398         INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1399         mutex_init(&hdmi_priv->pin_mutex);
1400
1401         ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1402         if (ret < 0) {
1403                 dev_err(&codec->dev,
1404                         "Failed in parse and map nid with err: %d\n", ret);
1405                 return ret;
1406         }
1407
1408         /* ASoC specific initialization */
1409         return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1410                         hdmi_dais, num_dais);
1411 }
1412
1413 static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1414 {
1415         struct hdac_hdmi_priv *hdmi = edev->private_data;
1416         struct hdac_hdmi_pin *pin, *pin_next;
1417         struct hdac_hdmi_cvt *cvt, *cvt_next;
1418         struct hdac_hdmi_pcm *pcm, *pcm_next;
1419
1420         snd_soc_unregister_codec(&edev->hdac.dev);
1421
1422         list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1423                 pcm->cvt = NULL;
1424                 pcm->pin = NULL;
1425                 list_del(&pcm->head);
1426                 kfree(pcm);
1427         }
1428
1429         list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1430                 list_del(&cvt->head);
1431                 kfree(cvt->name);
1432                 kfree(cvt);
1433         }
1434
1435         list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1436                 list_del(&pin->head);
1437                 kfree(pin);
1438         }
1439
1440         return 0;
1441 }
1442
1443 #ifdef CONFIG_PM
1444 static int hdac_hdmi_runtime_suspend(struct device *dev)
1445 {
1446         struct hdac_ext_device *edev = to_hda_ext_device(dev);
1447         struct hdac_device *hdac = &edev->hdac;
1448         struct hdac_bus *bus = hdac->bus;
1449         int err;
1450
1451         dev_dbg(dev, "Enter: %s\n", __func__);
1452
1453         /* controller may not have been initialized for the first time */
1454         if (!bus)
1455                 return 0;
1456
1457         /* Power down afg */
1458         if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1459                 snd_hdac_codec_write(hdac, hdac->afg, 0,
1460                         AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1461
1462         err = snd_hdac_display_power(bus, false);
1463         if (err < 0) {
1464                 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1465                 return err;
1466         }
1467
1468         return 0;
1469 }
1470
1471 static int hdac_hdmi_runtime_resume(struct device *dev)
1472 {
1473         struct hdac_ext_device *edev = to_hda_ext_device(dev);
1474         struct hdac_device *hdac = &edev->hdac;
1475         struct hdac_bus *bus = hdac->bus;
1476         int err;
1477
1478         dev_dbg(dev, "Enter: %s\n", __func__);
1479
1480         /* controller may not have been initialized for the first time */
1481         if (!bus)
1482                 return 0;
1483
1484         err = snd_hdac_display_power(bus, true);
1485         if (err < 0) {
1486                 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1487                 return err;
1488         }
1489
1490         /* Power up afg */
1491         if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1492                 snd_hdac_codec_write(hdac, hdac->afg, 0,
1493                         AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1494
1495         return 0;
1496 }
1497 #else
1498 #define hdac_hdmi_runtime_suspend NULL
1499 #define hdac_hdmi_runtime_resume NULL
1500 #endif
1501
1502 static const struct dev_pm_ops hdac_hdmi_pm = {
1503         SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1504 };
1505
1506 static const struct hda_device_id hdmi_list[] = {
1507         HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1508         {}
1509 };
1510
1511 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1512
1513 static struct hdac_ext_driver hdmi_driver = {
1514         . hdac = {
1515                 .driver = {
1516                         .name   = "HDMI HDA Codec",
1517                         .pm = &hdac_hdmi_pm,
1518                 },
1519                 .id_table       = hdmi_list,
1520         },
1521         .probe          = hdac_hdmi_dev_probe,
1522         .remove         = hdac_hdmi_dev_remove,
1523 };
1524
1525 static int __init hdmi_init(void)
1526 {
1527         return snd_hda_ext_driver_register(&hdmi_driver);
1528 }
1529
1530 static void __exit hdmi_exit(void)
1531 {
1532         snd_hda_ext_driver_unregister(&hdmi_driver);
1533 }
1534
1535 module_init(hdmi_init);
1536 module_exit(hdmi_exit);
1537
1538 MODULE_LICENSE("GPL v2");
1539 MODULE_DESCRIPTION("HDMI HD codec");
1540 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1541 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");