OSDN Git Service

Merge tag 'trace-v6.3-rc5-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[tomoyo/tomoyo-test1.git] / sound / soc / codecs / hdac_hdmi.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
4  *
5  *  Copyright (C) 2014-2015 Intel Corp
6  *  Author: Samreen Nilofer <samreen.nilofer@intel.com>
7  *          Subhransu S. Prusty <subhransu.s.prusty@intel.com>
8  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  */
12
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/hdmi.h>
18 #include <drm/drm_edid.h>
19 #include <sound/pcm_params.h>
20 #include <sound/jack.h>
21 #include <sound/soc.h>
22 #include <sound/hdaudio_ext.h>
23 #include <sound/hda_i915.h>
24 #include <sound/pcm_drm_eld.h>
25 #include <sound/hda_chmap.h>
26 #include "../../hda/local.h"
27 #include "hdac_hdmi.h"
28
29 #define NAME_SIZE       32
30
31 #define AMP_OUT_MUTE            0xb080
32 #define AMP_OUT_UNMUTE          0xb000
33 #define PIN_OUT                 (AC_PINCTL_OUT_EN)
34
35 #define HDA_MAX_CONNECTIONS     32
36
37 #define HDA_MAX_CVTS            3
38 #define HDA_MAX_PORTS           3
39
40 #define ELD_MAX_SIZE    256
41 #define ELD_FIXED_BYTES 20
42
43 #define ELD_VER_CEA_861D 2
44 #define ELD_VER_PARTIAL 31
45 #define ELD_MAX_MNL     16
46
47 struct hdac_hdmi_cvt_params {
48         unsigned int channels_min;
49         unsigned int channels_max;
50         u32 rates;
51         u64 formats;
52         unsigned int maxbps;
53 };
54
55 struct hdac_hdmi_cvt {
56         struct list_head head;
57         hda_nid_t nid;
58         const char *name;
59         struct hdac_hdmi_cvt_params params;
60 };
61
62 /* Currently only spk_alloc, more to be added */
63 struct hdac_hdmi_parsed_eld {
64         u8 spk_alloc;
65 };
66
67 struct hdac_hdmi_eld {
68         bool    monitor_present;
69         bool    eld_valid;
70         int     eld_size;
71         char    eld_buffer[ELD_MAX_SIZE];
72         struct  hdac_hdmi_parsed_eld info;
73 };
74
75 struct hdac_hdmi_pin {
76         struct list_head head;
77         hda_nid_t nid;
78         bool mst_capable;
79         struct hdac_hdmi_port *ports;
80         int num_ports;
81         struct hdac_device *hdev;
82 };
83
84 struct hdac_hdmi_port {
85         struct list_head head;
86         int id;
87         struct hdac_hdmi_pin *pin;
88         int num_mux_nids;
89         hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
90         struct hdac_hdmi_eld eld;
91         const char *jack_pin;
92         bool is_connect;
93         struct snd_soc_dapm_context *dapm;
94         const char *output_pin;
95         struct work_struct dapm_work;
96 };
97
98 struct hdac_hdmi_pcm {
99         struct list_head head;
100         int pcm_id;
101         struct list_head port_list;
102         struct hdac_hdmi_cvt *cvt;
103         struct snd_soc_jack *jack;
104         int stream_tag;
105         int channels;
106         int format;
107         bool chmap_set;
108         unsigned char chmap[8]; /* ALSA API channel-map */
109         struct mutex lock;
110         int jack_event;
111         struct snd_kcontrol *eld_ctl;
112 };
113
114 struct hdac_hdmi_dai_port_map {
115         int dai_id;
116         struct hdac_hdmi_port *port;
117         struct hdac_hdmi_cvt *cvt;
118 };
119
120 struct hdac_hdmi_drv_data {
121         unsigned int vendor_nid;
122 };
123
124 struct hdac_hdmi_priv {
125         struct hdac_device *hdev;
126         struct snd_soc_component *component;
127         struct snd_card *card;
128         struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
129         struct list_head pin_list;
130         struct list_head cvt_list;
131         struct list_head pcm_list;
132         int num_pin;
133         int num_cvt;
134         int num_ports;
135         struct mutex pin_mutex;
136         struct hdac_chmap chmap;
137         struct hdac_hdmi_drv_data *drv_data;
138         struct snd_soc_dai_driver *dai_drv;
139 };
140
141 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
142
143 static struct hdac_hdmi_pcm *
144 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
145                            struct hdac_hdmi_cvt *cvt)
146 {
147         struct hdac_hdmi_pcm *pcm;
148
149         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
150                 if (pcm->cvt == cvt)
151                         return pcm;
152         }
153
154         return NULL;
155 }
156
157 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
158                 struct hdac_hdmi_port *port, bool is_connect)
159 {
160         struct hdac_device *hdev = port->pin->hdev;
161
162         port->is_connect = is_connect;
163         if (is_connect) {
164                 /*
165                  * Report Jack connect event when a device is connected
166                  * for the first time where same PCM is attached to multiple
167                  * ports.
168                  */
169                 if (pcm->jack_event == 0) {
170                         dev_dbg(&hdev->dev,
171                                         "jack report for pcm=%d\n",
172                                         pcm->pcm_id);
173                         snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
174                                                 SND_JACK_AVOUT);
175                 }
176                 pcm->jack_event++;
177         } else {
178                 /*
179                  * Report Jack disconnect event when a device is disconnected
180                  * is the only last connected device when same PCM is attached
181                  * to multiple ports.
182                  */
183                 if (pcm->jack_event == 1)
184                         snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
185                 if (pcm->jack_event > 0)
186                         pcm->jack_event--;
187         }
188 }
189
190 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
191 {
192         if (port->is_connect)
193                 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
194         else
195                 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
196         snd_soc_dapm_sync(port->dapm);
197 }
198
199 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
200 {
201         struct hdac_hdmi_port *port;
202
203         port = container_of(work, struct hdac_hdmi_port, dapm_work);
204         hdac_hdmi_port_dapm_update(port);
205 }
206
207 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
208                 struct hdac_hdmi_port *port, bool is_connect)
209 {
210         hdac_hdmi_jack_report(pcm, port, is_connect);
211         hdac_hdmi_port_dapm_update(port);
212 }
213
214 /* MST supported verbs */
215 /*
216  * Get the no devices that can be connected to a port on the Pin widget.
217  */
218 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
219 {
220         unsigned int caps;
221         unsigned int type, param;
222
223         caps = get_wcaps(hdev, nid);
224         type = get_wcaps_type(caps);
225
226         if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
227                 return 0;
228
229         param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
230         if (param == -1)
231                 return param;
232
233         return param & AC_DEV_LIST_LEN_MASK;
234 }
235
236 /*
237  * Get the port entry select on the pin. Return the port entry
238  * id selected on the pin. Return 0 means the first port entry
239  * is selected or MST is not supported.
240  */
241 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
242                                         struct hdac_hdmi_port *port)
243 {
244         return snd_hdac_codec_read(hdev, port->pin->nid,
245                                 0, AC_VERB_GET_DEVICE_SEL, 0);
246 }
247
248 /*
249  * Sets the selected port entry for the configuring Pin widget verb.
250  * returns error if port set is not equal to port get otherwise success
251  */
252 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
253                                         struct hdac_hdmi_port *port)
254 {
255         int num_ports;
256
257         if (!port->pin->mst_capable)
258                 return 0;
259
260         /* AC_PAR_DEVLIST_LEN is 0 based. */
261         num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
262         if (num_ports < 0)
263                 return -EIO;
264         /*
265          * Device List Length is a 0 based integer value indicating the
266          * number of sink device that a MST Pin Widget can support.
267          */
268         if (num_ports + 1  < port->id)
269                 return 0;
270
271         snd_hdac_codec_write(hdev, port->pin->nid, 0,
272                         AC_VERB_SET_DEVICE_SEL, port->id);
273
274         if (port->id != hdac_hdmi_port_select_get(hdev, port))
275                 return -EIO;
276
277         dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
278
279         return 0;
280 }
281
282 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
283                                                 int pcm_idx)
284 {
285         struct hdac_hdmi_pcm *pcm;
286
287         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
288                 if (pcm->pcm_id == pcm_idx)
289                         return pcm;
290         }
291
292         return NULL;
293 }
294
295 static unsigned int sad_format(const u8 *sad)
296 {
297         return ((sad[0] >> 0x3) & 0x1f);
298 }
299
300 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
301 {
302         return (sad[2] & 7);
303 }
304
305 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
306                                                 void *eld)
307 {
308         u64 formats = SNDRV_PCM_FMTBIT_S16;
309         int i;
310         const u8 *sad, *eld_buf = eld;
311
312         sad = drm_eld_sad(eld_buf);
313         if (!sad)
314                 goto format_constraint;
315
316         for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
317                 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
318
319                         /*
320                          * the controller support 20 and 24 bits in 32 bit
321                          * container so we set S32
322                          */
323                         if (sad_sample_bits_lpcm(sad) & 0x6)
324                                 formats |= SNDRV_PCM_FMTBIT_S32;
325                 }
326         }
327
328 format_constraint:
329         return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
330                                 formats);
331
332 }
333
334 static void
335 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
336                                 int packet_index, int byte_index)
337 {
338         int val;
339
340         val = (packet_index << 5) | (byte_index & 0x1f);
341         snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
342 }
343
344 struct dp_audio_infoframe {
345         u8 type; /* 0x84 */
346         u8 len;  /* 0x1b */
347         u8 ver;  /* 0x11 << 2 */
348
349         u8 CC02_CT47;   /* match with HDMI infoframe from this on */
350         u8 SS01_SF24;
351         u8 CXT04;
352         u8 CA;
353         u8 LFEPBL01_LSV36_DM_INH7;
354 };
355
356 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
357                    struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
358 {
359         uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
360         struct hdmi_audio_infoframe frame;
361         struct hdac_hdmi_pin *pin = port->pin;
362         struct dp_audio_infoframe dp_ai;
363         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
364         struct hdac_hdmi_cvt *cvt = pcm->cvt;
365         u8 *dip;
366         int ret;
367         int i;
368         const u8 *eld_buf;
369         u8 conn_type;
370         int channels, ca;
371
372         ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
373                         pcm->channels, pcm->chmap_set, true, pcm->chmap);
374
375         channels = snd_hdac_get_active_channels(ca);
376         hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
377
378         snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
379                                 pcm->channels, pcm->chmap, pcm->chmap_set);
380
381         eld_buf = port->eld.eld_buffer;
382         conn_type = drm_eld_get_conn_type(eld_buf);
383
384         switch (conn_type) {
385         case DRM_ELD_CONN_TYPE_HDMI:
386                 hdmi_audio_infoframe_init(&frame);
387
388                 frame.channels = channels;
389                 frame.channel_allocation = ca;
390
391                 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
392                 if (ret < 0)
393                         return ret;
394
395                 break;
396
397         case DRM_ELD_CONN_TYPE_DP:
398                 memset(&dp_ai, 0, sizeof(dp_ai));
399                 dp_ai.type      = 0x84;
400                 dp_ai.len       = 0x1b;
401                 dp_ai.ver       = 0x11 << 2;
402                 dp_ai.CC02_CT47 = channels - 1;
403                 dp_ai.CA        = ca;
404
405                 dip = (u8 *)&dp_ai;
406                 break;
407
408         default:
409                 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
410                 return -EIO;
411         }
412
413         /* stop infoframe transmission */
414         hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
415         snd_hdac_codec_write(hdev, pin->nid, 0,
416                         AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
417
418
419         /*  Fill infoframe. Index auto-incremented */
420         hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
421         if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
422                 for (i = 0; i < sizeof(buffer); i++)
423                         snd_hdac_codec_write(hdev, pin->nid, 0,
424                                 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
425         } else {
426                 for (i = 0; i < sizeof(dp_ai); i++)
427                         snd_hdac_codec_write(hdev, pin->nid, 0,
428                                 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
429         }
430
431         /* Start infoframe */
432         hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
433         snd_hdac_codec_write(hdev, pin->nid, 0,
434                         AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
435
436         return 0;
437 }
438
439 static int hdac_hdmi_set_stream(struct snd_soc_dai *dai,
440                                 void *stream, int direction)
441 {
442         struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
443         struct hdac_device *hdev = hdmi->hdev;
444         struct hdac_hdmi_dai_port_map *dai_map;
445         struct hdac_hdmi_pcm *pcm;
446         struct hdac_stream *hstream;
447
448         if (!stream)
449                 return -EINVAL;
450
451         hstream = (struct hdac_stream *)stream;
452
453         dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, hstream->stream_tag);
454
455         dai_map = &hdmi->dai_map[dai->id];
456
457         pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
458
459         if (pcm)
460                 pcm->stream_tag = (hstream->stream_tag << 4);
461
462         return 0;
463 }
464
465 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
466         struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
467 {
468         struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
469         struct hdac_hdmi_dai_port_map *dai_map;
470         struct hdac_hdmi_pcm *pcm;
471         int format;
472
473         dai_map = &hdmi->dai_map[dai->id];
474
475         format = snd_hdac_calc_stream_format(params_rate(hparams),
476                         params_channels(hparams), params_format(hparams),
477                         dai->driver->playback.sig_bits, 0);
478
479         pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
480         if (!pcm)
481                 return -EIO;
482
483         pcm->format = format;
484         pcm->channels = params_channels(hparams);
485
486         return 0;
487 }
488
489 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
490                                         struct hdac_hdmi_pin *pin,
491                                         struct hdac_hdmi_port *port)
492 {
493         if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
494                 dev_warn(&hdev->dev,
495                         "HDMI: pin %d wcaps %#x does not support connection list\n",
496                         pin->nid, get_wcaps(hdev, pin->nid));
497                 return -EINVAL;
498         }
499
500         if (hdac_hdmi_port_select_set(hdev, port) < 0)
501                 return -EIO;
502
503         port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
504                         port->mux_nids, HDA_MAX_CONNECTIONS);
505         if (port->num_mux_nids == 0)
506                 dev_warn(&hdev->dev,
507                         "No connections found for pin:port %d:%d\n",
508                                                 pin->nid, port->id);
509
510         dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
511                         port->num_mux_nids, pin->nid, port->id);
512
513         return port->num_mux_nids;
514 }
515
516 /*
517  * Query pcm list and return port to which stream is routed.
518  *
519  * Also query connection list of the pin, to validate the cvt to port map.
520  *
521  * Same stream rendering to multiple ports simultaneously can be done
522  * possibly, but not supported for now in driver. So return the first port
523  * connected.
524  */
525 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
526                         struct hdac_device *hdev,
527                         struct hdac_hdmi_priv *hdmi,
528                         struct hdac_hdmi_cvt *cvt)
529 {
530         struct hdac_hdmi_pcm *pcm;
531         struct hdac_hdmi_port *port;
532         int ret, i;
533
534         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
535                 if (pcm->cvt == cvt) {
536                         if (list_empty(&pcm->port_list))
537                                 continue;
538
539                         list_for_each_entry(port, &pcm->port_list, head) {
540                                 mutex_lock(&pcm->lock);
541                                 ret = hdac_hdmi_query_port_connlist(hdev,
542                                                         port->pin, port);
543                                 mutex_unlock(&pcm->lock);
544                                 if (ret < 0)
545                                         continue;
546
547                                 for (i = 0; i < port->num_mux_nids; i++) {
548                                         if (port->mux_nids[i] == cvt->nid &&
549                                                 port->eld.monitor_present &&
550                                                 port->eld.eld_valid)
551                                                 return port;
552                                 }
553                         }
554                 }
555         }
556
557         return NULL;
558 }
559
560 /*
561  * Go through all converters and ensure connection is set to
562  * the correct pin as set via kcontrols.
563  */
564 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
565 {
566         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
567         struct hdac_hdmi_port *port;
568         struct hdac_hdmi_cvt *cvt;
569         int cvt_idx = 0;
570
571         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
572                 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
573                 if (port && port->pin) {
574                         snd_hdac_codec_write(hdev, port->pin->nid, 0,
575                                              AC_VERB_SET_CONNECT_SEL, cvt_idx);
576                         dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
577                                 __func__, cvt->name, port->pin->nid, cvt_idx);
578                 }
579                 ++cvt_idx;
580         }
581 }
582
583 /*
584  * This tries to get a valid pin and set the HW constraints based on the
585  * ELD. Even if a valid pin is not found return success so that device open
586  * doesn't fail.
587  */
588 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
589                         struct snd_soc_dai *dai)
590 {
591         struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
592         struct hdac_device *hdev = hdmi->hdev;
593         struct hdac_hdmi_dai_port_map *dai_map;
594         struct hdac_hdmi_cvt *cvt;
595         struct hdac_hdmi_port *port;
596         int ret;
597
598         dai_map = &hdmi->dai_map[dai->id];
599
600         cvt = dai_map->cvt;
601         port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
602
603         /*
604          * To make PA and other userland happy.
605          * userland scans devices so returning error does not help.
606          */
607         if (!port)
608                 return 0;
609         if ((!port->eld.monitor_present) ||
610                         (!port->eld.eld_valid)) {
611
612                 dev_warn(&hdev->dev,
613                         "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
614                         port->eld.monitor_present, port->eld.eld_valid,
615                         port->pin->nid, port->id);
616
617                 return 0;
618         }
619
620         dai_map->port = port;
621
622         ret = hdac_hdmi_eld_limit_formats(substream->runtime,
623                                 port->eld.eld_buffer);
624         if (ret < 0)
625                 return ret;
626
627         return snd_pcm_hw_constraint_eld(substream->runtime,
628                                 port->eld.eld_buffer);
629 }
630
631 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
632                 struct snd_soc_dai *dai)
633 {
634         struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
635         struct hdac_hdmi_dai_port_map *dai_map;
636         struct hdac_hdmi_pcm *pcm;
637
638         dai_map = &hdmi->dai_map[dai->id];
639
640         pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
641
642         if (pcm) {
643                 mutex_lock(&pcm->lock);
644                 pcm->chmap_set = false;
645                 memset(pcm->chmap, 0, sizeof(pcm->chmap));
646                 pcm->channels = 0;
647                 mutex_unlock(&pcm->lock);
648         }
649
650         if (dai_map->port)
651                 dai_map->port = NULL;
652 }
653
654 static int
655 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
656 {
657         unsigned int chans;
658         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
659         int err;
660
661         chans = get_wcaps(hdev, cvt->nid);
662         chans = get_wcaps_channels(chans);
663
664         cvt->params.channels_min = 2;
665
666         cvt->params.channels_max = chans;
667         if (chans > hdmi->chmap.channels_max)
668                 hdmi->chmap.channels_max = chans;
669
670         err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
671                         &cvt->params.rates,
672                         &cvt->params.formats,
673                         &cvt->params.maxbps);
674         if (err < 0)
675                 dev_err(&hdev->dev,
676                         "Failed to query pcm params for nid %d: %d\n",
677                         cvt->nid, err);
678
679         return err;
680 }
681
682 static int hdac_hdmi_fill_widget_info(struct device *dev,
683                 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
684                 void *priv, const char *wname, const char *stream,
685                 struct snd_kcontrol_new *wc, int numkc,
686                 int (*event)(struct snd_soc_dapm_widget *,
687                 struct snd_kcontrol *, int), unsigned short event_flags)
688 {
689         w->id = id;
690         w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
691         if (!w->name)
692                 return -ENOMEM;
693
694         w->sname = stream;
695         w->reg = SND_SOC_NOPM;
696         w->shift = 0;
697         w->kcontrol_news = wc;
698         w->num_kcontrols = numkc;
699         w->priv = priv;
700         w->event = event;
701         w->event_flags = event_flags;
702
703         return 0;
704 }
705
706 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
707                 const char *sink, const char *control, const char *src,
708                 int (*handler)(struct snd_soc_dapm_widget *src,
709                         struct snd_soc_dapm_widget *sink))
710 {
711         route->sink = sink;
712         route->source = src;
713         route->control = control;
714         route->connected = handler;
715 }
716
717 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
718                                         struct hdac_hdmi_port *port)
719 {
720         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
721         struct hdac_hdmi_pcm *pcm;
722         struct hdac_hdmi_port *p;
723
724         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
725                 if (list_empty(&pcm->port_list))
726                         continue;
727
728                 list_for_each_entry(p, &pcm->port_list, head) {
729                         if (p->id == port->id && port->pin == p->pin)
730                                 return pcm;
731                 }
732         }
733
734         return NULL;
735 }
736
737 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
738                              hda_nid_t nid, unsigned int pwr_state)
739 {
740         int count;
741         unsigned int state;
742
743         if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
744                 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
745                         for (count = 0; count < 10; count++) {
746                                 snd_hdac_codec_read(hdev, nid, 0,
747                                                 AC_VERB_SET_POWER_STATE,
748                                                 pwr_state);
749                                 state = snd_hdac_sync_power_state(hdev,
750                                                 nid, pwr_state);
751                                 if (!(state & AC_PWRST_ERROR))
752                                         break;
753                         }
754                 }
755         }
756 }
757
758 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
759                                    hda_nid_t nid, int val)
760 {
761         if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
762                 snd_hdac_codec_write(hdev, nid, 0,
763                                         AC_VERB_SET_AMP_GAIN_MUTE, val);
764 }
765
766
767 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
768                                         struct snd_kcontrol *kc, int event)
769 {
770         struct hdac_hdmi_port *port = w->priv;
771         struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
772         struct hdac_hdmi_pcm *pcm;
773
774         dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
775                         __func__, w->name, event);
776
777         pcm = hdac_hdmi_get_pcm(hdev, port);
778         if (!pcm)
779                 return -EIO;
780
781         /* set the device if pin is mst_capable */
782         if (hdac_hdmi_port_select_set(hdev, port) < 0)
783                 return -EIO;
784
785         switch (event) {
786         case SND_SOC_DAPM_PRE_PMU:
787                 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
788
789                 /* Enable out path for this pin widget */
790                 snd_hdac_codec_write(hdev, port->pin->nid, 0,
791                                 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
792
793                 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
794
795                 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
796
797         case SND_SOC_DAPM_POST_PMD:
798                 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
799
800                 /* Disable out path for this pin widget */
801                 snd_hdac_codec_write(hdev, port->pin->nid, 0,
802                                 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
803
804                 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
805                 break;
806
807         }
808
809         return 0;
810 }
811
812 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
813                                         struct snd_kcontrol *kc, int event)
814 {
815         struct hdac_hdmi_cvt *cvt = w->priv;
816         struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
817         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
818         struct hdac_hdmi_pcm *pcm;
819
820         dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
821                         __func__, w->name, event);
822
823         pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
824         if (!pcm)
825                 return -EIO;
826
827         switch (event) {
828         case SND_SOC_DAPM_PRE_PMU:
829                 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
830
831                 /* Enable transmission */
832                 snd_hdac_codec_write(hdev, cvt->nid, 0,
833                         AC_VERB_SET_DIGI_CONVERT_1, 1);
834
835                 /* Category Code (CC) to zero */
836                 snd_hdac_codec_write(hdev, cvt->nid, 0,
837                         AC_VERB_SET_DIGI_CONVERT_2, 0);
838
839                 snd_hdac_codec_write(hdev, cvt->nid, 0,
840                                 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
841                 snd_hdac_codec_write(hdev, cvt->nid, 0,
842                                 AC_VERB_SET_STREAM_FORMAT, pcm->format);
843
844                 /*
845                  * The connection indices are shared by all converters and
846                  * may interfere with each other. Ensure correct
847                  * routing for all converters at stream start.
848                  */
849                 hdac_hdmi_verify_connect_sel_all_pins(hdev);
850
851                 break;
852
853         case SND_SOC_DAPM_POST_PMD:
854                 snd_hdac_codec_write(hdev, cvt->nid, 0,
855                                 AC_VERB_SET_CHANNEL_STREAMID, 0);
856                 snd_hdac_codec_write(hdev, cvt->nid, 0,
857                                 AC_VERB_SET_STREAM_FORMAT, 0);
858
859                 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
860                 break;
861
862         }
863
864         return 0;
865 }
866
867 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
868                                         struct snd_kcontrol *kc, int event)
869 {
870         struct hdac_hdmi_port *port = w->priv;
871         struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
872         int mux_idx;
873
874         dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
875                         __func__, w->name, event);
876
877         if (!kc)
878                 kc  = w->kcontrols[0];
879
880         mux_idx = dapm_kcontrol_get_value(kc);
881
882         /* set the device if pin is mst_capable */
883         if (hdac_hdmi_port_select_set(hdev, port) < 0)
884                 return -EIO;
885
886         if (mux_idx > 0) {
887                 snd_hdac_codec_write(hdev, port->pin->nid, 0,
888                         AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
889         }
890
891         return 0;
892 }
893
894 /*
895  * Based on user selection, map the PINs with the PCMs.
896  */
897 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
898                 struct snd_ctl_elem_value *ucontrol)
899 {
900         int ret;
901         struct hdac_hdmi_port *p, *p_next;
902         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
903         struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
904         struct snd_soc_dapm_context *dapm = w->dapm;
905         struct hdac_hdmi_port *port = w->priv;
906         struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
907         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
908         struct hdac_hdmi_pcm *pcm;
909         const char *cvt_name =  e->texts[ucontrol->value.enumerated.item[0]];
910
911         ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
912         if (ret < 0)
913                 return ret;
914
915         if (port == NULL)
916                 return -EINVAL;
917
918         mutex_lock(&hdmi->pin_mutex);
919         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
920                 if (list_empty(&pcm->port_list))
921                         continue;
922
923                 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
924                         if (p == port && p->id == port->id &&
925                                         p->pin == port->pin) {
926                                 hdac_hdmi_jack_report_sync(pcm, port, false);
927                                 list_del(&p->head);
928                         }
929                 }
930         }
931
932         /*
933          * Jack status is not reported during device probe as the
934          * PCMs are not registered by then. So report it here.
935          */
936         list_for_each_entry(pcm, &hdmi->pcm_list, head) {
937                 if (!strcmp(cvt_name, pcm->cvt->name)) {
938                         list_add_tail(&port->head, &pcm->port_list);
939                         if (port->eld.monitor_present && port->eld.eld_valid) {
940                                 hdac_hdmi_jack_report_sync(pcm, port, true);
941                                 mutex_unlock(&hdmi->pin_mutex);
942                                 return ret;
943                         }
944                 }
945         }
946         mutex_unlock(&hdmi->pin_mutex);
947
948         return ret;
949 }
950
951 /*
952  * Ideally the Mux inputs should be based on the num_muxs enumerated, but
953  * the display driver seem to be programming the connection list for the pin
954  * widget runtime.
955  *
956  * So programming all the possible inputs for the mux, the user has to take
957  * care of selecting the right one and leaving all other inputs selected to
958  * "NONE"
959  */
960 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
961                                 struct hdac_hdmi_port *port,
962                                 struct snd_soc_dapm_widget *widget,
963                                 const char *widget_name)
964 {
965         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
966         struct hdac_hdmi_pin *pin = port->pin;
967         struct snd_kcontrol_new *kc;
968         struct hdac_hdmi_cvt *cvt;
969         struct soc_enum *se;
970         char kc_name[NAME_SIZE];
971         char mux_items[NAME_SIZE];
972         /* To hold inputs to the Pin mux */
973         char *items[HDA_MAX_CONNECTIONS];
974         int i = 0;
975         int num_items = hdmi->num_cvt + 1;
976
977         kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
978         if (!kc)
979                 return -ENOMEM;
980
981         se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
982         if (!se)
983                 return -ENOMEM;
984
985         snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
986                                                 pin->nid, port->id);
987         kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
988         if (!kc->name)
989                 return -ENOMEM;
990
991         kc->private_value = (long)se;
992         kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
993         kc->access = 0;
994         kc->info = snd_soc_info_enum_double;
995         kc->put = hdac_hdmi_set_pin_port_mux;
996         kc->get = snd_soc_dapm_get_enum_double;
997
998         se->reg = SND_SOC_NOPM;
999
1000         /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
1001         se->items = num_items;
1002         se->mask = roundup_pow_of_two(se->items) - 1;
1003
1004         sprintf(mux_items, "NONE");
1005         items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1006         if (!items[i])
1007                 return -ENOMEM;
1008
1009         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1010                 i++;
1011                 sprintf(mux_items, "cvt %d", cvt->nid);
1012                 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1013                 if (!items[i])
1014                         return -ENOMEM;
1015         }
1016
1017         se->texts = devm_kmemdup(&hdev->dev, items,
1018                         (num_items  * sizeof(char *)), GFP_KERNEL);
1019         if (!se->texts)
1020                 return -ENOMEM;
1021
1022         return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1023                         snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1024                         hdac_hdmi_pin_mux_widget_event,
1025                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
1026 }
1027
1028 /* Add cvt <- input <- mux route map */
1029 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
1030                         struct snd_soc_dapm_widget *widgets,
1031                         struct snd_soc_dapm_route *route, int rindex)
1032 {
1033         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1034         const struct snd_kcontrol_new *kc;
1035         struct soc_enum *se;
1036         int mux_index = hdmi->num_cvt + hdmi->num_ports;
1037         int i, j;
1038
1039         for (i = 0; i < hdmi->num_ports; i++) {
1040                 kc = widgets[mux_index].kcontrol_news;
1041                 se = (struct soc_enum *)kc->private_value;
1042                 for (j = 0; j < hdmi->num_cvt; j++) {
1043                         hdac_hdmi_fill_route(&route[rindex],
1044                                         widgets[mux_index].name,
1045                                         se->texts[j + 1],
1046                                         widgets[j].name, NULL);
1047
1048                         rindex++;
1049                 }
1050
1051                 mux_index++;
1052         }
1053 }
1054
1055 /*
1056  * Widgets are added in the below sequence
1057  *      Converter widgets for num converters enumerated
1058  *      Pin-port widgets for num ports for Pins enumerated
1059  *      Pin-port mux widgets to represent connenction list of pin widget
1060  *
1061  * For each port, one Mux and One output widget is added
1062  * Total widgets elements = num_cvt + (num_ports * 2);
1063  *
1064  * Routes are added as below:
1065  *      pin-port mux -> pin (based on num_ports)
1066  *      cvt -> "Input sel control" -> pin-port_mux
1067  *
1068  * Total route elements:
1069  *      num_ports + (pin_muxes * num_cvt)
1070  */
1071 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1072 {
1073         struct snd_soc_dapm_widget *widgets;
1074         struct snd_soc_dapm_route *route;
1075         struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1076         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1077         struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1078         char widget_name[NAME_SIZE];
1079         struct hdac_hdmi_cvt *cvt;
1080         struct hdac_hdmi_pin *pin;
1081         int ret, i = 0, num_routes = 0, j;
1082
1083         if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1084                 return -EINVAL;
1085
1086         widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1087                                 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1088                                 GFP_KERNEL);
1089
1090         if (!widgets)
1091                 return -ENOMEM;
1092
1093         /* DAPM widgets to represent each converter widget */
1094         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1095                 sprintf(widget_name, "Converter %d", cvt->nid);
1096                 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1097                         snd_soc_dapm_aif_in, cvt,
1098                         widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1099                         hdac_hdmi_cvt_output_widget_event,
1100                         SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1101                 if (ret < 0)
1102                         return ret;
1103                 i++;
1104         }
1105
1106         list_for_each_entry(pin, &hdmi->pin_list, head) {
1107                 for (j = 0; j < pin->num_ports; j++) {
1108                         sprintf(widget_name, "hif%d-%d Output",
1109                                 pin->nid, pin->ports[j].id);
1110                         ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1111                                         snd_soc_dapm_output, &pin->ports[j],
1112                                         widget_name, NULL, NULL, 0,
1113                                         hdac_hdmi_pin_output_widget_event,
1114                                         SND_SOC_DAPM_PRE_PMU |
1115                                         SND_SOC_DAPM_POST_PMD);
1116                         if (ret < 0)
1117                                 return ret;
1118                         pin->ports[j].output_pin = widgets[i].name;
1119                         i++;
1120                 }
1121         }
1122
1123         /* DAPM widgets to represent the connection list to pin widget */
1124         list_for_each_entry(pin, &hdmi->pin_list, head) {
1125                 for (j = 0; j < pin->num_ports; j++) {
1126                         sprintf(widget_name, "Pin%d-Port%d Mux",
1127                                 pin->nid, pin->ports[j].id);
1128                         ret = hdac_hdmi_create_pin_port_muxs(hdev,
1129                                                 &pin->ports[j], &widgets[i],
1130                                                 widget_name);
1131                         if (ret < 0)
1132                                 return ret;
1133                         i++;
1134
1135                         /* For cvt to pin_mux mapping */
1136                         num_routes += hdmi->num_cvt;
1137
1138                         /* For pin_mux to pin mapping */
1139                         num_routes++;
1140                 }
1141         }
1142
1143         route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1144                                                         GFP_KERNEL);
1145         if (!route)
1146                 return -ENOMEM;
1147
1148         i = 0;
1149         /* Add pin <- NULL <- mux route map */
1150         list_for_each_entry(pin, &hdmi->pin_list, head) {
1151                 for (j = 0; j < pin->num_ports; j++) {
1152                         int sink_index = i + hdmi->num_cvt;
1153                         int src_index = sink_index + pin->num_ports *
1154                                                 hdmi->num_pin;
1155
1156                         hdac_hdmi_fill_route(&route[i],
1157                                 widgets[sink_index].name, NULL,
1158                                 widgets[src_index].name, NULL);
1159                         i++;
1160                 }
1161         }
1162
1163         hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1164
1165         snd_soc_dapm_new_controls(dapm, widgets,
1166                 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1167
1168         snd_soc_dapm_add_routes(dapm, route, num_routes);
1169         snd_soc_dapm_new_widgets(dapm->card);
1170
1171         return 0;
1172
1173 }
1174
1175 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1176 {
1177         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1178         struct hdac_hdmi_dai_port_map *dai_map;
1179         struct hdac_hdmi_cvt *cvt;
1180         int dai_id = 0;
1181
1182         if (list_empty(&hdmi->cvt_list))
1183                 return -EINVAL;
1184
1185         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1186                 dai_map = &hdmi->dai_map[dai_id];
1187                 dai_map->dai_id = dai_id;
1188                 dai_map->cvt = cvt;
1189
1190                 dai_id++;
1191
1192                 if (dai_id == HDA_MAX_CVTS) {
1193                         dev_warn(&hdev->dev,
1194                                 "Max dais supported: %d\n", dai_id);
1195                         break;
1196                 }
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1203 {
1204         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1205         struct hdac_hdmi_cvt *cvt;
1206         char name[NAME_SIZE];
1207
1208         cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1209         if (!cvt)
1210                 return -ENOMEM;
1211
1212         cvt->nid = nid;
1213         sprintf(name, "cvt %d", cvt->nid);
1214         cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1215         if (!cvt->name)
1216                 return -ENOMEM;
1217
1218         list_add_tail(&cvt->head, &hdmi->cvt_list);
1219         hdmi->num_cvt++;
1220
1221         return hdac_hdmi_query_cvt_params(hdev, cvt);
1222 }
1223
1224 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1225                         struct hdac_hdmi_port *port)
1226 {
1227         unsigned int ver, mnl;
1228
1229         ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1230                                                 >> DRM_ELD_VER_SHIFT;
1231
1232         if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1233                 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1234                 return -EINVAL;
1235         }
1236
1237         mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1238                 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1239
1240         if (mnl > ELD_MAX_MNL) {
1241                 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1242                 return -EINVAL;
1243         }
1244
1245         port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1246
1247         return 0;
1248 }
1249
1250 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1251                                     struct hdac_hdmi_port *port)
1252 {
1253         struct hdac_device *hdev = pin->hdev;
1254         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1255         struct hdac_hdmi_pcm *pcm;
1256         int size = 0;
1257         int port_id = -1;
1258         bool eld_valid, eld_changed;
1259
1260         if (!hdmi)
1261                 return;
1262
1263         /*
1264          * In case of non MST pin, get_eld info API expectes port
1265          * to be -1.
1266          */
1267         mutex_lock(&hdmi->pin_mutex);
1268         port->eld.monitor_present = false;
1269
1270         if (pin->mst_capable)
1271                 port_id = port->id;
1272
1273         size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1274                                 &port->eld.monitor_present,
1275                                 port->eld.eld_buffer,
1276                                 ELD_MAX_SIZE);
1277
1278         if (size > 0) {
1279                 size = min(size, ELD_MAX_SIZE);
1280                 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1281                         size = -EINVAL;
1282         }
1283
1284         eld_valid = port->eld.eld_valid;
1285
1286         if (size > 0) {
1287                 port->eld.eld_valid = true;
1288                 port->eld.eld_size = size;
1289         } else {
1290                 port->eld.eld_valid = false;
1291                 port->eld.eld_size = 0;
1292         }
1293
1294         eld_changed = (eld_valid != port->eld.eld_valid);
1295
1296         pcm = hdac_hdmi_get_pcm(hdev, port);
1297
1298         if (!port->eld.monitor_present || !port->eld.eld_valid) {
1299
1300                 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1301                                                 __func__, pin->nid, port->id);
1302
1303                 /*
1304                  * PCMs are not registered during device probe, so don't
1305                  * report jack here. It will be done in usermode mux
1306                  * control select.
1307                  */
1308                 if (pcm) {
1309                         hdac_hdmi_jack_report(pcm, port, false);
1310                         schedule_work(&port->dapm_work);
1311                 }
1312
1313                 mutex_unlock(&hdmi->pin_mutex);
1314                 return;
1315         }
1316
1317         if (port->eld.monitor_present && port->eld.eld_valid) {
1318                 if (pcm) {
1319                         hdac_hdmi_jack_report(pcm, port, true);
1320                         schedule_work(&port->dapm_work);
1321                 }
1322
1323                 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1324                           port->eld.eld_buffer, port->eld.eld_size, false);
1325
1326         }
1327         mutex_unlock(&hdmi->pin_mutex);
1328
1329         if (eld_changed && pcm)
1330                 snd_ctl_notify(hdmi->card,
1331                                SNDRV_CTL_EVENT_MASK_VALUE |
1332                                SNDRV_CTL_EVENT_MASK_INFO,
1333                                &pcm->eld_ctl->id);
1334 }
1335
1336 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1337                                struct hdac_hdmi_pin *pin)
1338 {
1339         struct hdac_hdmi_port *ports;
1340         int max_ports = HDA_MAX_PORTS;
1341         int i;
1342
1343         /*
1344          * FIXME: max_port may vary for each platform, so pass this as
1345          * as driver data or query from i915 interface when this API is
1346          * implemented.
1347          */
1348
1349         ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1350         if (!ports)
1351                 return -ENOMEM;
1352
1353         for (i = 0; i < max_ports; i++) {
1354                 ports[i].id = i;
1355                 ports[i].pin = pin;
1356                 INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1357         }
1358         pin->ports = ports;
1359         pin->num_ports = max_ports;
1360         return 0;
1361 }
1362
1363 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1364 {
1365         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1366         struct hdac_hdmi_pin *pin;
1367         int ret;
1368
1369         pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1370         if (!pin)
1371                 return -ENOMEM;
1372
1373         pin->nid = nid;
1374         pin->mst_capable = false;
1375         pin->hdev = hdev;
1376         ret = hdac_hdmi_add_ports(hdev, pin);
1377         if (ret < 0)
1378                 return ret;
1379
1380         list_add_tail(&pin->head, &hdmi->pin_list);
1381         hdmi->num_pin++;
1382         hdmi->num_ports += pin->num_ports;
1383
1384         return 0;
1385 }
1386
1387 #define INTEL_VENDOR_NID 0x08
1388 #define INTEL_GLK_VENDOR_NID 0x0b
1389 #define INTEL_GET_VENDOR_VERB 0xf81
1390 #define INTEL_SET_VENDOR_VERB 0x781
1391 #define INTEL_EN_DP12                   0x02 /* enable DP 1.2 features */
1392 #define INTEL_EN_ALL_PIN_CVTS   0x01 /* enable 2nd & 3rd pins and convertors */
1393
1394 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1395 {
1396         unsigned int vendor_param;
1397         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1398         unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1399
1400         vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1401                                 INTEL_GET_VENDOR_VERB, 0);
1402         if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1403                 return;
1404
1405         vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1406         vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1407                                 INTEL_SET_VENDOR_VERB, vendor_param);
1408         if (vendor_param == -1)
1409                 return;
1410 }
1411
1412 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1413 {
1414         unsigned int vendor_param;
1415         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1416         unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1417
1418         vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1419                                 INTEL_GET_VENDOR_VERB, 0);
1420         if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1421                 return;
1422
1423         /* enable DP1.2 mode */
1424         vendor_param |= INTEL_EN_DP12;
1425         vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1426                                 INTEL_SET_VENDOR_VERB, vendor_param);
1427         if (vendor_param == -1)
1428                 return;
1429
1430 }
1431
1432 static int hdac_hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
1433                              struct snd_ctl_elem_info *uinfo)
1434 {
1435         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1436         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1437         struct hdac_hdmi_pcm *pcm;
1438         struct hdac_hdmi_port *port;
1439         struct hdac_hdmi_eld *eld;
1440
1441         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1442         uinfo->count = 0;
1443
1444         pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1445         if (!pcm) {
1446                 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1447                         kcontrol->id.device);
1448                 return 0;
1449         }
1450
1451         if (list_empty(&pcm->port_list)) {
1452                 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1453                         __func__, kcontrol->id.device);
1454                 return 0;
1455         }
1456
1457         mutex_lock(&hdmi->pin_mutex);
1458
1459         list_for_each_entry(port, &pcm->port_list, head) {
1460                 eld = &port->eld;
1461
1462                 if (eld->eld_valid) {
1463                         uinfo->count = eld->eld_size;
1464                         break;
1465                 }
1466         }
1467
1468         mutex_unlock(&hdmi->pin_mutex);
1469
1470         return 0;
1471 }
1472
1473 static int hdac_hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
1474                             struct snd_ctl_elem_value *ucontrol)
1475 {
1476         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1477         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1478         struct hdac_hdmi_pcm *pcm;
1479         struct hdac_hdmi_port *port;
1480         struct hdac_hdmi_eld *eld;
1481
1482         memset(ucontrol->value.bytes.data, 0, sizeof(ucontrol->value.bytes.data));
1483
1484         pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1485         if (!pcm) {
1486                 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1487                         kcontrol->id.device);
1488                 return 0;
1489         }
1490
1491         if (list_empty(&pcm->port_list)) {
1492                 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1493                         __func__, kcontrol->id.device);
1494                 return 0;
1495         }
1496
1497         mutex_lock(&hdmi->pin_mutex);
1498
1499         list_for_each_entry(port, &pcm->port_list, head) {
1500                 eld = &port->eld;
1501
1502                 if (!eld->eld_valid)
1503                         continue;
1504
1505                 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
1506                     eld->eld_size > ELD_MAX_SIZE) {
1507                         mutex_unlock(&hdmi->pin_mutex);
1508
1509                         dev_err(component->dev, "%s: buffer too small, device %d eld_size %d\n",
1510                                 __func__, kcontrol->id.device, eld->eld_size);
1511                         snd_BUG();
1512                         return -EINVAL;
1513                 }
1514
1515                 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
1516                        eld->eld_size);
1517                 break;
1518         }
1519
1520         mutex_unlock(&hdmi->pin_mutex);
1521
1522         return 0;
1523 }
1524
1525 static int hdac_hdmi_create_eld_ctl(struct snd_soc_component *component, struct hdac_hdmi_pcm *pcm)
1526 {
1527         struct snd_kcontrol *kctl;
1528         struct snd_kcontrol_new hdmi_eld_ctl = {
1529                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1530                           SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1531                 .iface  = SNDRV_CTL_ELEM_IFACE_PCM,
1532                 .name   = "ELD",
1533                 .info   = hdac_hdmi_eld_ctl_info,
1534                 .get    = hdac_hdmi_eld_ctl_get,
1535                 .device = pcm->pcm_id,
1536         };
1537
1538         /* add ELD ctl with the device number corresponding to the PCM stream */
1539         kctl = snd_ctl_new1(&hdmi_eld_ctl, component);
1540         if (!kctl)
1541                 return -ENOMEM;
1542
1543         pcm->eld_ctl = kctl;
1544
1545         return snd_ctl_add(component->card->snd_card, kctl);
1546 }
1547
1548 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1549         .startup = hdac_hdmi_pcm_open,
1550         .shutdown = hdac_hdmi_pcm_close,
1551         .hw_params = hdac_hdmi_set_hw_params,
1552         .set_stream = hdac_hdmi_set_stream,
1553 };
1554
1555 /*
1556  * Each converter can support a stream independently. So a dai is created
1557  * based on the number of converter queried.
1558  */
1559 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1560                 struct snd_soc_dai_driver **dais,
1561                 struct hdac_hdmi_priv *hdmi, int num_dais)
1562 {
1563         struct snd_soc_dai_driver *hdmi_dais;
1564         struct hdac_hdmi_cvt *cvt;
1565         char name[NAME_SIZE], dai_name[NAME_SIZE];
1566         int i = 0;
1567         u32 rates, bps;
1568         unsigned int rate_max = 384000, rate_min = 8000;
1569         u64 formats;
1570         int ret;
1571
1572         hdmi_dais = devm_kzalloc(&hdev->dev,
1573                         (sizeof(*hdmi_dais) * num_dais),
1574                         GFP_KERNEL);
1575         if (!hdmi_dais)
1576                 return -ENOMEM;
1577
1578         list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1579                 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1580                                         &rates, &formats, &bps);
1581                 if (ret)
1582                         return ret;
1583
1584                 /* Filter out 44.1, 88.2 and 176.4Khz */
1585                 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1586                            SNDRV_PCM_RATE_176400);
1587                 if (!rates)
1588                         return -EINVAL;
1589
1590                 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1591                 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1592                                         dai_name, GFP_KERNEL);
1593
1594                 if (!hdmi_dais[i].name)
1595                         return -ENOMEM;
1596
1597                 snprintf(name, sizeof(name), "hifi%d", i+1);
1598                 hdmi_dais[i].playback.stream_name =
1599                                 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1600                 if (!hdmi_dais[i].playback.stream_name)
1601                         return -ENOMEM;
1602
1603                 /*
1604                  * Set caps based on capability queried from the converter.
1605                  * It will be constrained runtime based on ELD queried.
1606                  */
1607                 hdmi_dais[i].playback.formats = formats;
1608                 hdmi_dais[i].playback.rates = rates;
1609                 hdmi_dais[i].playback.rate_max = rate_max;
1610                 hdmi_dais[i].playback.rate_min = rate_min;
1611                 hdmi_dais[i].playback.channels_min = 2;
1612                 hdmi_dais[i].playback.channels_max = 2;
1613                 hdmi_dais[i].playback.sig_bits = bps;
1614                 hdmi_dais[i].ops = &hdmi_dai_ops;
1615                 i++;
1616         }
1617
1618         *dais = hdmi_dais;
1619         hdmi->dai_drv = hdmi_dais;
1620
1621         return 0;
1622 }
1623
1624 /*
1625  * Parse all nodes and store the cvt/pin nids in array
1626  * Add one time initialization for pin and cvt widgets
1627  */
1628 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1629                 struct snd_soc_dai_driver **dais, int *num_dais)
1630 {
1631         hda_nid_t nid;
1632         int i, num_nodes;
1633         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1634         int ret;
1635
1636         hdac_hdmi_skl_enable_all_pins(hdev);
1637         hdac_hdmi_skl_enable_dp12(hdev);
1638
1639         num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1640         if (!nid || num_nodes <= 0) {
1641                 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1642                 return -EINVAL;
1643         }
1644
1645         for (i = 0; i < num_nodes; i++, nid++) {
1646                 unsigned int caps;
1647                 unsigned int type;
1648
1649                 caps = get_wcaps(hdev, nid);
1650                 type = get_wcaps_type(caps);
1651
1652                 if (!(caps & AC_WCAP_DIGITAL))
1653                         continue;
1654
1655                 switch (type) {
1656
1657                 case AC_WID_AUD_OUT:
1658                         ret = hdac_hdmi_add_cvt(hdev, nid);
1659                         if (ret < 0)
1660                                 return ret;
1661                         break;
1662
1663                 case AC_WID_PIN:
1664                         ret = hdac_hdmi_add_pin(hdev, nid);
1665                         if (ret < 0)
1666                                 return ret;
1667                         break;
1668                 }
1669         }
1670
1671         if (!hdmi->num_pin || !hdmi->num_cvt) {
1672                 ret = -EIO;
1673                 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1674                 return ret;
1675         }
1676
1677         ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1678         if (ret) {
1679                 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1680                         ret);
1681                 return ret;
1682         }
1683
1684         *num_dais = hdmi->num_cvt;
1685         ret = hdac_hdmi_init_dai_map(hdev);
1686         if (ret < 0)
1687                 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1688                         ret);
1689         return ret;
1690 }
1691
1692 static int hdac_hdmi_pin2port(void *aptr, int pin)
1693 {
1694         return pin - 4; /* map NID 0x05 -> port #1 */
1695 }
1696
1697 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1698 {
1699         struct hdac_device *hdev = aptr;
1700         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1701         struct hdac_hdmi_pin *pin;
1702         struct hdac_hdmi_port *hport = NULL;
1703         struct snd_soc_component *component = hdmi->component;
1704         int i;
1705
1706         /* Don't know how this mapping is derived */
1707         hda_nid_t pin_nid = port + 0x04;
1708
1709         dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1710                                                         pin_nid, pipe);
1711
1712         /*
1713          * skip notification during system suspend (but not in runtime PM);
1714          * the state will be updated at resume. Also since the ELD and
1715          * connection states are updated in anyway at the end of the resume,
1716          * we can skip it when received during PM process.
1717          */
1718         if (snd_power_get_state(component->card->snd_card) !=
1719                         SNDRV_CTL_POWER_D0)
1720                 return;
1721
1722         if (atomic_read(&hdev->in_pm))
1723                 return;
1724
1725         list_for_each_entry(pin, &hdmi->pin_list, head) {
1726                 if (pin->nid != pin_nid)
1727                         continue;
1728
1729                 /* In case of non MST pin, pipe is -1 */
1730                 if (pipe == -1) {
1731                         pin->mst_capable = false;
1732                         /* if not MST, default is port[0] */
1733                         hport = &pin->ports[0];
1734                 } else {
1735                         for (i = 0; i < pin->num_ports; i++) {
1736                                 pin->mst_capable = true;
1737                                 if (pin->ports[i].id == pipe) {
1738                                         hport = &pin->ports[i];
1739                                         break;
1740                                 }
1741                         }
1742                 }
1743
1744                 if (hport)
1745                         hdac_hdmi_present_sense(pin, hport);
1746         }
1747
1748 }
1749
1750 static struct drm_audio_component_audio_ops aops = {
1751         .pin2port       = hdac_hdmi_pin2port,
1752         .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1753 };
1754
1755 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1756                                                 int device)
1757 {
1758         struct snd_soc_pcm_runtime *rtd;
1759
1760         for_each_card_rtds(card, rtd) {
1761                 if (rtd->pcm && (rtd->pcm->device == device))
1762                         return rtd->pcm;
1763         }
1764
1765         return NULL;
1766 }
1767
1768 /* create jack pin kcontrols */
1769 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1770                                     struct hdac_device *hdev)
1771 {
1772         struct hdac_hdmi_pin *pin;
1773         struct snd_kcontrol_new *kc;
1774         char kc_name[NAME_SIZE], xname[NAME_SIZE];
1775         char *name;
1776         int i = 0, j;
1777         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1778         struct snd_soc_component *component = hdmi->component;
1779
1780         kc = devm_kcalloc(component->dev, hdmi->num_ports,
1781                                 sizeof(*kc), GFP_KERNEL);
1782
1783         if (!kc)
1784                 return -ENOMEM;
1785
1786         list_for_each_entry(pin, &hdmi->pin_list, head) {
1787                 for (j = 0; j < pin->num_ports; j++) {
1788                         snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1789                                                 pin->nid, pin->ports[j].id);
1790                         name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1791                         if (!name)
1792                                 return -ENOMEM;
1793                         snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1794                         kc[i].name = devm_kstrdup(component->dev, kc_name,
1795                                                         GFP_KERNEL);
1796                         if (!kc[i].name)
1797                                 return -ENOMEM;
1798
1799                         kc[i].private_value = (unsigned long)name;
1800                         kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1801                         kc[i].access = 0;
1802                         kc[i].info = snd_soc_dapm_info_pin_switch;
1803                         kc[i].put = snd_soc_dapm_put_pin_switch;
1804                         kc[i].get = snd_soc_dapm_get_pin_switch;
1805                         i++;
1806                 }
1807         }
1808
1809         return snd_soc_add_card_controls(card, kc, i);
1810 }
1811
1812 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1813                         struct snd_soc_dapm_context *dapm)
1814 {
1815         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1816         struct hdac_device *hdev = hdmi->hdev;
1817         struct hdac_hdmi_pin *pin;
1818         struct snd_soc_dapm_widget *widgets;
1819         struct snd_soc_dapm_route *route;
1820         char w_name[NAME_SIZE];
1821         int i = 0, j, ret;
1822
1823         widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1824                                 sizeof(*widgets), GFP_KERNEL);
1825
1826         if (!widgets)
1827                 return -ENOMEM;
1828
1829         route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1830                                 sizeof(*route), GFP_KERNEL);
1831         if (!route)
1832                 return -ENOMEM;
1833
1834         /* create Jack DAPM widget */
1835         list_for_each_entry(pin, &hdmi->pin_list, head) {
1836                 for (j = 0; j < pin->num_ports; j++) {
1837                         snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1838                                                 pin->nid, pin->ports[j].id);
1839
1840                         ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1841                                         snd_soc_dapm_spk, NULL,
1842                                         w_name, NULL, NULL, 0, NULL, 0);
1843                         if (ret < 0)
1844                                 return ret;
1845
1846                         pin->ports[j].jack_pin = widgets[i].name;
1847                         pin->ports[j].dapm = dapm;
1848
1849                         /* add to route from Jack widget to output */
1850                         hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1851                                         NULL, pin->ports[j].output_pin, NULL);
1852
1853                         i++;
1854                 }
1855         }
1856
1857         /* Add Route from Jack widget to the output widget */
1858         ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1859         if (ret < 0)
1860                 return ret;
1861
1862         ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1863         if (ret < 0)
1864                 return ret;
1865
1866         ret = snd_soc_dapm_new_widgets(dapm->card);
1867         if (ret < 0)
1868                 return ret;
1869
1870         /* Add Jack Pin switch Kcontrol */
1871         ret = create_fill_jack_kcontrols(dapm->card, hdev);
1872
1873         if (ret < 0)
1874                 return ret;
1875
1876         /* default set the Jack Pin switch to OFF */
1877         list_for_each_entry(pin, &hdmi->pin_list, head) {
1878                 for (j = 0; j < pin->num_ports; j++)
1879                         snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1880                                                 pin->ports[j].jack_pin);
1881         }
1882
1883         return 0;
1884 }
1885 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1886
1887 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1888                                 struct snd_soc_jack *jack)
1889 {
1890         struct snd_soc_component *component = dai->component;
1891         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1892         struct hdac_device *hdev = hdmi->hdev;
1893         struct hdac_hdmi_pcm *pcm;
1894         struct snd_pcm *snd_pcm;
1895         int err;
1896
1897         /*
1898          * this is a new PCM device, create new pcm and
1899          * add to the pcm list
1900          */
1901         pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1902         if (!pcm)
1903                 return -ENOMEM;
1904         pcm->pcm_id = device;
1905         pcm->cvt = hdmi->dai_map[dai->id].cvt;
1906         pcm->jack_event = 0;
1907         pcm->jack = jack;
1908         mutex_init(&pcm->lock);
1909         INIT_LIST_HEAD(&pcm->port_list);
1910         snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1911         if (snd_pcm) {
1912                 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1913                 if (err < 0) {
1914                         dev_err(&hdev->dev,
1915                                 "chmap control add failed with err: %d for pcm: %d\n",
1916                                 err, device);
1917                         return err;
1918                 }
1919         }
1920
1921         /* add control for ELD Bytes */
1922         err = hdac_hdmi_create_eld_ctl(component, pcm);
1923         if (err < 0) {
1924                 dev_err(&hdev->dev,
1925                         "eld control add failed with err: %d for pcm: %d\n",
1926                         err, device);
1927                 return err;
1928         }
1929
1930         list_add_tail(&pcm->head, &hdmi->pcm_list);
1931
1932         return 0;
1933 }
1934 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1935
1936 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1937                         struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1938 {
1939         int i;
1940         struct hdac_hdmi_pin *pin;
1941
1942         list_for_each_entry(pin, &hdmi->pin_list, head) {
1943                 if (detect_pin_caps) {
1944
1945                         if (hdac_hdmi_get_port_len(hdev, pin->nid)  == 0)
1946                                 pin->mst_capable = false;
1947                         else
1948                                 pin->mst_capable = true;
1949                 }
1950
1951                 for (i = 0; i < pin->num_ports; i++) {
1952                         if (!pin->mst_capable && i > 0)
1953                                 continue;
1954
1955                         hdac_hdmi_present_sense(pin, &pin->ports[i]);
1956                 }
1957         }
1958 }
1959
1960 static int hdmi_codec_probe(struct snd_soc_component *component)
1961 {
1962         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1963         struct hdac_device *hdev = hdmi->hdev;
1964         struct snd_soc_dapm_context *dapm =
1965                 snd_soc_component_get_dapm(component);
1966         struct hdac_ext_link *hlink;
1967         int ret;
1968
1969         hdmi->component = component;
1970
1971         /*
1972          * hold the ref while we probe, also no need to drop the ref on
1973          * exit, we call pm_runtime_suspend() so that will do for us
1974          */
1975         hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1976         if (!hlink) {
1977                 dev_err(&hdev->dev, "hdac link not found\n");
1978                 return -EIO;
1979         }
1980
1981         snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1982
1983         ret = create_fill_widget_route_map(dapm);
1984         if (ret < 0)
1985                 return ret;
1986
1987         aops.audio_ptr = hdev;
1988         ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1989         if (ret < 0) {
1990                 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1991                 return ret;
1992         }
1993
1994         hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1995         /* Imp: Store the card pointer in hda_codec */
1996         hdmi->card = dapm->card->snd_card;
1997
1998         /*
1999          * Setup a device_link between card device and HDMI codec device.
2000          * The card device is the consumer and the HDMI codec device is
2001          * the supplier. With this setting, we can make sure that the audio
2002          * domain in display power will be always turned on before operating
2003          * on the HDMI audio codec registers.
2004          * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
2005          * sure the device link is freed when the machine driver is removed.
2006          */
2007         device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
2008                         DL_FLAG_AUTOREMOVE_CONSUMER);
2009         /*
2010          * hdac_device core already sets the state to active and calls
2011          * get_noresume. So enable runtime and set the device to suspend.
2012          */
2013         pm_runtime_enable(&hdev->dev);
2014         pm_runtime_put(&hdev->dev);
2015         pm_runtime_suspend(&hdev->dev);
2016
2017         return 0;
2018 }
2019
2020 static void hdmi_codec_remove(struct snd_soc_component *component)
2021 {
2022         struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
2023         struct hdac_device *hdev = hdmi->hdev;
2024         int ret;
2025
2026         ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
2027         if (ret < 0)
2028                 dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
2029                                 ret);
2030
2031         pm_runtime_disable(&hdev->dev);
2032 }
2033
2034 #ifdef CONFIG_PM_SLEEP
2035 static int hdmi_codec_resume(struct device *dev)
2036 {
2037         struct hdac_device *hdev = dev_to_hdac_dev(dev);
2038         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2039         int ret;
2040
2041         ret = pm_runtime_force_resume(dev);
2042         if (ret < 0)
2043                 return ret;
2044         /*
2045          * As the ELD notify callback request is not entertained while the
2046          * device is in suspend state. Need to manually check detection of
2047          * all pins here. pin capablity change is not support, so use the
2048          * already set pin caps.
2049          *
2050          * NOTE: this is safe to call even if the codec doesn't actually resume.
2051          * The pin check involves only with DRM audio component hooks, so it
2052          * works even if the HD-audio side is still dreaming peacefully.
2053          */
2054         hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
2055         return 0;
2056 }
2057 #else
2058 #define hdmi_codec_resume NULL
2059 #endif
2060
2061 static const struct snd_soc_component_driver hdmi_hda_codec = {
2062         .probe                  = hdmi_codec_probe,
2063         .remove                 = hdmi_codec_remove,
2064         .use_pmdown_time        = 1,
2065         .endianness             = 1,
2066 };
2067
2068 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
2069                                         unsigned char *chmap)
2070 {
2071         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2072         struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2073
2074         memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
2075 }
2076
2077 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
2078                                 unsigned char *chmap, int prepared)
2079 {
2080         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2081         struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2082         struct hdac_hdmi_port *port;
2083
2084         if (!pcm)
2085                 return;
2086
2087         if (list_empty(&pcm->port_list))
2088                 return;
2089
2090         mutex_lock(&pcm->lock);
2091         pcm->chmap_set = true;
2092         memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
2093         list_for_each_entry(port, &pcm->port_list, head)
2094                 if (prepared)
2095                         hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
2096         mutex_unlock(&pcm->lock);
2097 }
2098
2099 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
2100 {
2101         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2102         struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2103
2104         if (!pcm)
2105                 return false;
2106
2107         if (list_empty(&pcm->port_list))
2108                 return false;
2109
2110         return true;
2111 }
2112
2113 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
2114 {
2115         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2116         struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2117         struct hdac_hdmi_port *port;
2118
2119         if (!pcm)
2120                 return 0;
2121
2122         if (list_empty(&pcm->port_list))
2123                 return 0;
2124
2125         port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
2126
2127         if (!port || !port->eld.eld_valid)
2128                 return 0;
2129
2130         return port->eld.info.spk_alloc;
2131 }
2132
2133 static struct hdac_hdmi_drv_data intel_glk_drv_data  = {
2134         .vendor_nid = INTEL_GLK_VENDOR_NID,
2135 };
2136
2137 static struct hdac_hdmi_drv_data intel_drv_data  = {
2138         .vendor_nid = INTEL_VENDOR_NID,
2139 };
2140
2141 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
2142 {
2143         struct hdac_hdmi_priv *hdmi_priv;
2144         struct snd_soc_dai_driver *hdmi_dais = NULL;
2145         struct hdac_ext_link *hlink;
2146         int num_dais = 0;
2147         int ret;
2148         struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2149         const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
2150
2151         /* hold the ref while we probe */
2152         hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
2153         if (!hlink) {
2154                 dev_err(&hdev->dev, "hdac link not found\n");
2155                 return -EIO;
2156         }
2157
2158         snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2159
2160         hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2161         if (hdmi_priv == NULL)
2162                 return -ENOMEM;
2163
2164         snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2165         hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2166         hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2167         hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2168         hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2169         hdmi_priv->hdev = hdev;
2170
2171         if (!hdac_id)
2172                 return -ENODEV;
2173
2174         if (hdac_id->driver_data)
2175                 hdmi_priv->drv_data =
2176                         (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2177         else
2178                 hdmi_priv->drv_data = &intel_drv_data;
2179
2180         dev_set_drvdata(&hdev->dev, hdmi_priv);
2181
2182         INIT_LIST_HEAD(&hdmi_priv->pin_list);
2183         INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2184         INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2185         mutex_init(&hdmi_priv->pin_mutex);
2186
2187         /*
2188          * Turned off in the runtime_suspend during the first explicit
2189          * pm_runtime_suspend call.
2190          */
2191         snd_hdac_display_power(hdev->bus, hdev->addr, true);
2192
2193         ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2194         if (ret < 0) {
2195                 dev_err(&hdev->dev,
2196                         "Failed in parse and map nid with err: %d\n", ret);
2197                 return ret;
2198         }
2199         snd_hdac_refresh_widgets(hdev);
2200
2201         /* ASoC specific initialization */
2202         ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2203                                         hdmi_dais, num_dais);
2204
2205         snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2206
2207         return ret;
2208 }
2209
2210 static void clear_dapm_works(struct hdac_device *hdev)
2211 {
2212         struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2213         struct hdac_hdmi_pin *pin;
2214         int i;
2215
2216         list_for_each_entry(pin, &hdmi->pin_list, head)
2217                 for (i = 0; i < pin->num_ports; i++)
2218                         cancel_work_sync(&pin->ports[i].dapm_work);
2219 }
2220
2221 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2222 {
2223         clear_dapm_works(hdev);
2224         snd_hdac_display_power(hdev->bus, hdev->addr, false);
2225
2226         return 0;
2227 }
2228
2229 #ifdef CONFIG_PM
2230 static int hdac_hdmi_runtime_suspend(struct device *dev)
2231 {
2232         struct hdac_device *hdev = dev_to_hdac_dev(dev);
2233         struct hdac_bus *bus = hdev->bus;
2234         struct hdac_ext_link *hlink;
2235
2236         dev_dbg(dev, "Enter: %s\n", __func__);
2237
2238         /* controller may not have been initialized for the first time */
2239         if (!bus)
2240                 return 0;
2241
2242         /*
2243          * Power down afg.
2244          * codec_read is preferred over codec_write to set the power state.
2245          * This way verb is send to set the power state and response
2246          * is received. So setting power state is ensured without using loop
2247          * to read the state.
2248          */
2249         snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2250                                                         AC_PWRST_D3);
2251
2252         hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2253         if (!hlink) {
2254                 dev_err(dev, "hdac link not found\n");
2255                 return -EIO;
2256         }
2257
2258         snd_hdac_codec_link_down(hdev);
2259         snd_hdac_ext_bus_link_put(bus, hlink);
2260
2261         snd_hdac_display_power(bus, hdev->addr, false);
2262
2263         return 0;
2264 }
2265
2266 static int hdac_hdmi_runtime_resume(struct device *dev)
2267 {
2268         struct hdac_device *hdev = dev_to_hdac_dev(dev);
2269         struct hdac_bus *bus = hdev->bus;
2270         struct hdac_ext_link *hlink;
2271
2272         dev_dbg(dev, "Enter: %s\n", __func__);
2273
2274         /* controller may not have been initialized for the first time */
2275         if (!bus)
2276                 return 0;
2277
2278         hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
2279         if (!hlink) {
2280                 dev_err(dev, "hdac link not found\n");
2281                 return -EIO;
2282         }
2283
2284         snd_hdac_ext_bus_link_get(bus, hlink);
2285         snd_hdac_codec_link_up(hdev);
2286
2287         snd_hdac_display_power(bus, hdev->addr, true);
2288
2289         hdac_hdmi_skl_enable_all_pins(hdev);
2290         hdac_hdmi_skl_enable_dp12(hdev);
2291
2292         /* Power up afg */
2293         snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2294                                                         AC_PWRST_D0);
2295
2296         return 0;
2297 }
2298 #else
2299 #define hdac_hdmi_runtime_suspend NULL
2300 #define hdac_hdmi_runtime_resume NULL
2301 #endif
2302
2303 static const struct dev_pm_ops hdac_hdmi_pm = {
2304         SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2305         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2306 };
2307
2308 static const struct hda_device_id hdmi_list[] = {
2309         HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2310         HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2311         HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2312         HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2313                                                    &intel_glk_drv_data),
2314         HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2315                                                    &intel_glk_drv_data),
2316         {}
2317 };
2318
2319 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2320
2321 static struct hdac_driver hdmi_driver = {
2322         .driver = {
2323                 .name   = "HDMI HDA Codec",
2324                 .pm = &hdac_hdmi_pm,
2325         },
2326         .id_table       = hdmi_list,
2327         .probe          = hdac_hdmi_dev_probe,
2328         .remove         = hdac_hdmi_dev_remove,
2329 };
2330
2331 static int __init hdmi_init(void)
2332 {
2333         return snd_hda_ext_driver_register(&hdmi_driver);
2334 }
2335
2336 static void __exit hdmi_exit(void)
2337 {
2338         snd_hda_ext_driver_unregister(&hdmi_driver);
2339 }
2340
2341 module_init(hdmi_init);
2342 module_exit(hdmi_exit);
2343
2344 MODULE_LICENSE("GPL v2");
2345 MODULE_DESCRIPTION("HDMI HD codec");
2346 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2347 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");