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