blob: f8b6e9f1c6f6594d63add8777c21f0849ae04964 [file] [log] [blame]
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301/*
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>
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +053024#include <linux/hdmi.h>
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +053025#include <drm/drm_edid.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053026#include <sound/pcm_params.h>
Jeeja KP4a3478d2016-02-12 07:46:06 +053027#include <sound/jack.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053028#include <sound/soc.h>
29#include <sound/hdaudio_ext.h>
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +053030#include <sound/hda_i915.h>
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +053031#include <sound/pcm_drm_eld.h>
Subhransu S. Prustybcced702016-04-14 10:07:30 +053032#include <sound/hda_chmap.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053033#include "../../hda/local.h"
Jeeja KP4a3478d2016-02-12 07:46:06 +053034#include "hdac_hdmi.h"
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053035
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +053036#define NAME_SIZE 32
37
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053038#define AMP_OUT_MUTE 0xb080
39#define AMP_OUT_UNMUTE 0xb000
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053040#define PIN_OUT (AC_PINCTL_OUT_EN)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053041
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053042#define HDA_MAX_CONNECTIONS 32
43
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053044#define HDA_MAX_CVTS 3
Jeeja KP754695f2017-02-06 12:09:14 +053045#define HDA_MAX_PORTS 3
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053046
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053047#define ELD_MAX_SIZE 256
48#define ELD_FIXED_BYTES 20
49
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +053050#define ELD_VER_CEA_861D 2
51#define ELD_VER_PARTIAL 31
52#define ELD_MAX_MNL 16
53
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053054struct 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
62struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053063 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053064 hda_nid_t nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +053065 const char *name;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053066 struct hdac_hdmi_cvt_params params;
67};
68
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053069/* Currently only spk_alloc, more to be added */
70struct hdac_hdmi_parsed_eld {
71 u8 spk_alloc;
72};
73
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053074struct hdac_hdmi_eld {
75 bool monitor_present;
76 bool eld_valid;
77 int eld_size;
78 char eld_buffer[ELD_MAX_SIZE];
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053079 struct hdac_hdmi_parsed_eld info;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053080};
81
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053082struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053083 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053084 hda_nid_t nid;
Jeeja KP2acd8302017-02-06 12:09:18 +053085 bool mst_capable;
Jeeja KP754695f2017-02-06 12:09:14 +053086 struct hdac_hdmi_port *ports;
87 int num_ports;
88 struct hdac_ext_device *edev;
89};
90
91struct hdac_hdmi_port {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +053092 struct list_head head;
Jeeja KP754695f2017-02-06 12:09:14 +053093 int id;
94 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053095 int num_mux_nids;
96 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053097 struct hdac_hdmi_eld eld;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053098};
99
Jeeja KP4a3478d2016-02-12 07:46:06 +0530100struct hdac_hdmi_pcm {
101 struct list_head head;
102 int pcm_id;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530103 struct list_head port_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530104 struct hdac_hdmi_cvt *cvt;
105 struct snd_jack *jack;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530106 int stream_tag;
107 int channels;
108 int format;
Jeeja KPab1eea12017-01-24 21:49:05 +0530109 bool chmap_set;
110 unsigned char chmap[8]; /* ALSA API channel-map */
111 struct mutex lock;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530112 int jack_event;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530113};
114
Jeeja KP754695f2017-02-06 12:09:14 +0530115struct hdac_hdmi_dai_port_map {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530116 int dai_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530117 struct hdac_hdmi_port *port;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530118 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530119};
120
121struct hdac_hdmi_priv {
Jeeja KP754695f2017-02-06 12:09:14 +0530122 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530123 struct list_head pin_list;
124 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530125 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530126 int num_pin;
127 int num_cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530128 int num_ports;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530129 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530130 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530131};
132
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530133static struct hdac_hdmi_pcm *
134hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
135 struct hdac_hdmi_cvt *cvt)
136{
137 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530138
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530139 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
140 if (pcm->cvt == cvt)
141 break;
142 }
143
144 return pcm;
145}
Jeeja KP1de777f2017-01-10 17:57:48 +0530146
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530147static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
148 struct hdac_hdmi_port *port, bool is_connect)
149{
150 struct hdac_ext_device *edev = port->pin->edev;
151
152 if (is_connect) {
153 /*
154 * Report Jack connect event when a device is connected
155 * for the first time where same PCM is attached to multiple
156 * ports.
157 */
158 if (pcm->jack_event == 0) {
159 dev_dbg(&edev->hdac.dev,
160 "jack report for pcm=%d\n",
161 pcm->pcm_id);
162 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
163 }
164 pcm->jack_event++;
165 } else {
166 /*
167 * Report Jack disconnect event when a device is disconnected
168 * is the only last connected device when same PCM is attached
169 * to multiple ports.
170 */
171 if (pcm->jack_event == 1)
172 snd_jack_report(pcm->jack, 0);
173 if (pcm->jack_event > 0)
174 pcm->jack_event--;
175 }
176}
177
Jeeja KPfc181b02017-02-07 19:09:45 +0530178/* MST supported verbs */
179/*
180 * Get the no devices that can be connected to a port on the Pin widget.
181 */
182static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
183{
184 unsigned int caps;
185 unsigned int type, param;
186
187 caps = get_wcaps(&hdac->hdac, nid);
188 type = get_wcaps_type(caps);
189
190 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
191 return 0;
192
193 param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
194 AC_PAR_DEVLIST_LEN);
195 if (param == -1)
196 return param;
197
198 return param & AC_DEV_LIST_LEN_MASK;
199}
200
201/*
202 * Get the port entry select on the pin. Return the port entry
203 * id selected on the pin. Return 0 means the first port entry
204 * is selected or MST is not supported.
205 */
206static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
207 struct hdac_hdmi_port *port)
208{
209 return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
210 0, AC_VERB_GET_DEVICE_SEL, 0);
211}
212
213/*
214 * Sets the selected port entry for the configuring Pin widget verb.
215 * returns error if port set is not equal to port get otherwise success
216 */
217static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
218 struct hdac_hdmi_port *port)
219{
220 int num_ports;
221
222 if (!port->pin->mst_capable)
223 return 0;
224
225 /* AC_PAR_DEVLIST_LEN is 0 based. */
226 num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
227
228 if (num_ports < 0)
229 return -EIO;
230 /*
231 * Device List Length is a 0 based integer value indicating the
232 * number of sink device that a MST Pin Widget can support.
233 */
234 if (num_ports + 1 < port->id)
235 return 0;
236
237 snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
238 AC_VERB_SET_DEVICE_SEL, port->id);
239
240 if (port->id != hdac_hdmi_port_select_get(hdac, port))
241 return -EIO;
242
243 dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
244
245 return 0;
246}
247
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530248static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
249 int pcm_idx)
250{
251 struct hdac_hdmi_pcm *pcm;
252
253 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
254 if (pcm->pcm_id == pcm_idx)
255 return pcm;
256 }
257
258 return NULL;
259}
260
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530261static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
262{
Geliang Tang51b2c422015-12-28 22:47:13 +0800263 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530264
Geliang Tang51b2c422015-12-28 22:47:13 +0800265 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530266}
267
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530268static unsigned int sad_format(const u8 *sad)
269{
270 return ((sad[0] >> 0x3) & 0x1f);
271}
272
273static unsigned int sad_sample_bits_lpcm(const u8 *sad)
274{
275 return (sad[2] & 7);
276}
277
278static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
279 void *eld)
280{
281 u64 formats = SNDRV_PCM_FMTBIT_S16;
282 int i;
283 const u8 *sad, *eld_buf = eld;
284
285 sad = drm_eld_sad(eld_buf);
286 if (!sad)
287 goto format_constraint;
288
289 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
290 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
291
292 /*
293 * the controller support 20 and 24 bits in 32 bit
294 * container so we set S32
295 */
296 if (sad_sample_bits_lpcm(sad) & 0x6)
297 formats |= SNDRV_PCM_FMTBIT_S32;
298 }
299 }
300
301format_constraint:
302 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
303 formats);
304
305}
306
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530307static void
308hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
309 int packet_index, int byte_index)
310{
311 int val;
312
313 val = (packet_index << 5) | (byte_index & 0x1f);
314
315 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
316 AC_VERB_SET_HDMI_DIP_INDEX, val);
317}
318
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530319struct dp_audio_infoframe {
320 u8 type; /* 0x84 */
321 u8 len; /* 0x1b */
322 u8 ver; /* 0x11 << 2 */
323
324 u8 CC02_CT47; /* match with HDMI infoframe from this on */
325 u8 SS01_SF24;
326 u8 CXT04;
327 u8 CA;
328 u8 LFEPBL01_LSV36_DM_INH7;
329};
330
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530331static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
Jeeja KP754695f2017-02-06 12:09:14 +0530332 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530333{
334 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
335 struct hdmi_audio_infoframe frame;
Jeeja KP754695f2017-02-06 12:09:14 +0530336 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530337 struct dp_audio_infoframe dp_ai;
338 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KPab1eea12017-01-24 21:49:05 +0530339 struct hdac_hdmi_cvt *cvt = pcm->cvt;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530340 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530341 int ret;
342 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530343 const u8 *eld_buf;
344 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530345 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530346
Jeeja KP754695f2017-02-06 12:09:14 +0530347 ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
Jeeja KPab1eea12017-01-24 21:49:05 +0530348 pcm->channels, pcm->chmap_set, true, pcm->chmap);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530349
350 channels = snd_hdac_get_active_channels(ca);
Jeeja KPab1eea12017-01-24 21:49:05 +0530351 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530352
353 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
Jeeja KPab1eea12017-01-24 21:49:05 +0530354 pcm->channels, pcm->chmap, pcm->chmap_set);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530355
Jeeja KP754695f2017-02-06 12:09:14 +0530356 eld_buf = port->eld.eld_buffer;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530357 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530358
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530359 switch (conn_type) {
360 case DRM_ELD_CONN_TYPE_HDMI:
361 hdmi_audio_infoframe_init(&frame);
362
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530363 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530364 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530365
366 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
367 if (ret < 0)
368 return ret;
369
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530370 break;
371
372 case DRM_ELD_CONN_TYPE_DP:
373 memset(&dp_ai, 0, sizeof(dp_ai));
374 dp_ai.type = 0x84;
375 dp_ai.len = 0x1b;
376 dp_ai.ver = 0x11 << 2;
377 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530378 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530379
380 dip = (u8 *)&dp_ai;
381 break;
382
383 default:
384 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
385 conn_type);
386 return -EIO;
387 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530388
389 /* stop infoframe transmission */
Jeeja KPab1eea12017-01-24 21:49:05 +0530390 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
391 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530392 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
393
394
395 /* Fill infoframe. Index auto-incremented */
Jeeja KPab1eea12017-01-24 21:49:05 +0530396 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530397 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530398 for (i = 0; i < sizeof(buffer); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530399 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530400 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530401 } else {
402 for (i = 0; i < sizeof(dp_ai); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530403 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530404 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
405 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530406
407 /* Start infoframe */
Jeeja KPab1eea12017-01-24 21:49:05 +0530408 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
409 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530410 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
411
412 return 0;
413}
414
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530415static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
416 unsigned int tx_mask, unsigned int rx_mask,
417 int slots, int slot_width)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530418{
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530419 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
420 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530421 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530422 struct hdac_hdmi_pcm *pcm;
423
424 dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530425
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530426 dai_map = &hdmi->dai_map[dai->id];
427
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530428 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530429
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530430 if (pcm)
431 pcm->stream_tag = (tx_mask << 4);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530432
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530433 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530434}
435
436static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
437 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
438{
439 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530440 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530441 struct hdac_hdmi_dai_port_map *dai_map;
442 struct hdac_hdmi_port *port;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530443 struct hdac_hdmi_pcm *pcm;
444 int format;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530445
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530446 dai_map = &hdmi->dai_map[dai->id];
Jeeja KP754695f2017-02-06 12:09:14 +0530447 port = dai_map->port;
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530448
Jeeja KP754695f2017-02-06 12:09:14 +0530449 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530450 return -ENODEV;
451
Jeeja KP754695f2017-02-06 12:09:14 +0530452 if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
453 dev_err(&hdac->hdac.dev,
454 "device is not configured for this pin:port%d:%d\n",
455 port->pin->nid, port->id);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530456 return -ENODEV;
457 }
458
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530459 format = snd_hdac_calc_stream_format(params_rate(hparams),
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530460 params_channels(hparams), params_format(hparams),
461 24, 0);
462
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530463 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
464 if (!pcm)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530465 return -EIO;
466
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530467 pcm->format = format;
468 pcm->channels = params_channels(hparams);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530469
470 return 0;
471}
472
Jeeja KP754695f2017-02-06 12:09:14 +0530473static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
474 struct hdac_hdmi_pin *pin,
475 struct hdac_hdmi_port *port)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530476{
477 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
478 dev_warn(&hdac->hdac.dev,
479 "HDMI: pin %d wcaps %#x does not support connection list\n",
480 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
481 return -EINVAL;
482 }
483
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530484 if (hdac_hdmi_port_select_set(hdac, port) < 0)
485 return -EIO;
486
Jeeja KP754695f2017-02-06 12:09:14 +0530487 port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
488 port->mux_nids, HDA_MAX_CONNECTIONS);
489 if (port->num_mux_nids == 0)
490 dev_warn(&hdac->hdac.dev,
491 "No connections found for pin:port %d:%d\n",
492 pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530493
Jeeja KP754695f2017-02-06 12:09:14 +0530494 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
495 port->num_mux_nids, pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530496
Jeeja KP754695f2017-02-06 12:09:14 +0530497 return port->num_mux_nids;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530498}
499
500/*
Jeeja KP754695f2017-02-06 12:09:14 +0530501 * Query pcm list and return port to which stream is routed.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530502 *
Jeeja KP754695f2017-02-06 12:09:14 +0530503 * Also query connection list of the pin, to validate the cvt to port map.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530504 *
Jeeja KP754695f2017-02-06 12:09:14 +0530505 * Same stream rendering to multiple ports simultaneously can be done
506 * possibly, but not supported for now in driver. So return the first port
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530507 * connected.
508 */
Jeeja KP754695f2017-02-06 12:09:14 +0530509static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530510 struct hdac_ext_device *edev,
511 struct hdac_hdmi_priv *hdmi,
512 struct hdac_hdmi_cvt *cvt)
513{
514 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +0530515 struct hdac_hdmi_port *port = NULL;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530516 int ret, i;
517
518 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
519 if (pcm->cvt == cvt) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530520 if (list_empty(&pcm->port_list))
521 continue;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530522
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530523 list_for_each_entry(port, &pcm->port_list, head) {
524 mutex_lock(&pcm->lock);
525 ret = hdac_hdmi_query_port_connlist(edev,
526 port->pin, port);
527 mutex_unlock(&pcm->lock);
528 if (ret < 0)
529 continue;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530530
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530531 for (i = 0; i < port->num_mux_nids; i++) {
532 if (port->mux_nids[i] == cvt->nid &&
533 port->eld.monitor_present &&
534 port->eld.eld_valid)
535 return port;
536 }
537 }
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530538 }
539 }
540
541 return NULL;
542}
543
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530544/*
545 * This tries to get a valid pin and set the HW constraints based on the
546 * ELD. Even if a valid pin is not found return success so that device open
547 * doesn't fail.
548 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530549static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
550 struct snd_soc_dai *dai)
551{
552 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
553 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530554 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530555 struct hdac_hdmi_cvt *cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530556 struct hdac_hdmi_port *port;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530557 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530558
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530559 dai_map = &hdmi->dai_map[dai->id];
560
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530561 cvt = dai_map->cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530562 port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530563
564 /*
565 * To make PA and other userland happy.
566 * userland scans devices so returning error does not help.
567 */
Jeeja KP754695f2017-02-06 12:09:14 +0530568 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530569 return 0;
Jeeja KP754695f2017-02-06 12:09:14 +0530570 if ((!port->eld.monitor_present) ||
571 (!port->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530572
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530573 dev_warn(&hdac->hdac.dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530574 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
575 port->eld.monitor_present, port->eld.eld_valid,
576 port->pin->nid, port->id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530577
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530578 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530579 }
580
Jeeja KP754695f2017-02-06 12:09:14 +0530581 dai_map->port = port;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530582
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530583 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530584 port->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530585 if (ret < 0)
586 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530587
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530588 return snd_pcm_hw_constraint_eld(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530589 port->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530590}
591
592static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
593 struct snd_soc_dai *dai)
594{
595 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
596 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530597 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPab1eea12017-01-24 21:49:05 +0530598 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530599
600 dai_map = &hdmi->dai_map[dai->id];
601
Jeeja KPab1eea12017-01-24 21:49:05 +0530602 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530603
Jeeja KPab1eea12017-01-24 21:49:05 +0530604 if (pcm) {
605 mutex_lock(&pcm->lock);
606 pcm->chmap_set = false;
607 memset(pcm->chmap, 0, sizeof(pcm->chmap));
608 pcm->channels = 0;
609 mutex_unlock(&pcm->lock);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530610 }
Jeeja KPab1eea12017-01-24 21:49:05 +0530611
Jeeja KP754695f2017-02-06 12:09:14 +0530612 if (dai_map->port)
613 dai_map->port = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530614}
615
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530616static int
617hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
618{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530619 unsigned int chans;
620 struct hdac_ext_device *edev = to_ehdac_device(hdac);
621 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530622 int err;
623
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530624 chans = get_wcaps(hdac, cvt->nid);
625 chans = get_wcaps_channels(chans);
626
627 cvt->params.channels_min = 2;
628
629 cvt->params.channels_max = chans;
630 if (chans > hdmi->chmap.channels_max)
631 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530632
633 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
634 &cvt->params.rates,
635 &cvt->params.formats,
636 &cvt->params.maxbps);
637 if (err < 0)
638 dev_err(&hdac->dev,
639 "Failed to query pcm params for nid %d: %d\n",
640 cvt->nid, err);
641
642 return err;
643}
644
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530645static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530646 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
647 void *priv, const char *wname, const char *stream,
648 struct snd_kcontrol_new *wc, int numkc,
649 int (*event)(struct snd_soc_dapm_widget *,
650 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530651{
652 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530653 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
654 if (!w->name)
655 return -ENOMEM;
656
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530657 w->sname = stream;
658 w->reg = SND_SOC_NOPM;
659 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530660 w->kcontrol_news = wc;
661 w->num_kcontrols = numkc;
662 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530663 w->event = event;
664 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530665
666 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530667}
668
669static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530670 const char *sink, const char *control, const char *src,
671 int (*handler)(struct snd_soc_dapm_widget *src,
672 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530673{
674 route->sink = sink;
675 route->source = src;
676 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530677 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530678}
679
Jeeja KP4a3478d2016-02-12 07:46:06 +0530680static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
Jeeja KP754695f2017-02-06 12:09:14 +0530681 struct hdac_hdmi_port *port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530682{
683 struct hdac_hdmi_priv *hdmi = edev->private_data;
684 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530685 struct hdac_hdmi_port *p;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530686
687 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530688 if (list_empty(&pcm->port_list))
Jeeja KP754695f2017-02-06 12:09:14 +0530689 continue;
690
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530691 list_for_each_entry(p, &pcm->port_list, head) {
692 if (p->id == port->id && port->pin == p->pin)
693 return pcm;
694 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530695 }
696
697 return NULL;
698}
699
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530700static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
701 hda_nid_t nid, unsigned int pwr_state)
702{
703 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
704 if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
705 snd_hdac_codec_write(&edev->hdac, nid, 0,
706 AC_VERB_SET_POWER_STATE, pwr_state);
707 }
708}
709
710static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
711 hda_nid_t nid, int val)
712{
713 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
714 snd_hdac_codec_write(&edev->hdac, nid, 0,
715 AC_VERB_SET_AMP_GAIN_MUTE, val);
716}
717
718
719static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
720 struct snd_kcontrol *kc, int event)
721{
Jeeja KP754695f2017-02-06 12:09:14 +0530722 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530723 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
724 struct hdac_hdmi_pcm *pcm;
725
726 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
727 __func__, w->name, event);
728
Jeeja KP754695f2017-02-06 12:09:14 +0530729 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530730 if (!pcm)
731 return -EIO;
732
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530733 /* set the device if pin is mst_capable */
734 if (hdac_hdmi_port_select_set(edev, port) < 0)
735 return -EIO;
736
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530737 switch (event) {
738 case SND_SOC_DAPM_PRE_PMU:
Jeeja KP754695f2017-02-06 12:09:14 +0530739 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530740
741 /* Enable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530742 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530743 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
744
Jeeja KP754695f2017-02-06 12:09:14 +0530745 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530746
Jeeja KP754695f2017-02-06 12:09:14 +0530747 return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530748
749 case SND_SOC_DAPM_POST_PMD:
Jeeja KP754695f2017-02-06 12:09:14 +0530750 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530751
752 /* Disable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530753 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530754 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
755
Jeeja KP754695f2017-02-06 12:09:14 +0530756 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530757 break;
758
759 }
760
761 return 0;
762}
763
764static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
765 struct snd_kcontrol *kc, int event)
766{
767 struct hdac_hdmi_cvt *cvt = w->priv;
768 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
769 struct hdac_hdmi_priv *hdmi = edev->private_data;
770 struct hdac_hdmi_pcm *pcm;
771
772 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
773 __func__, w->name, event);
774
775 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
776 if (!pcm)
777 return -EIO;
778
779 switch (event) {
780 case SND_SOC_DAPM_PRE_PMU:
781 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
782
783 /* Enable transmission */
784 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
785 AC_VERB_SET_DIGI_CONVERT_1, 1);
786
787 /* Category Code (CC) to zero */
788 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
789 AC_VERB_SET_DIGI_CONVERT_2, 0);
790
791 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
792 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
793 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
794 AC_VERB_SET_STREAM_FORMAT, pcm->format);
795 break;
796
797 case SND_SOC_DAPM_POST_PMD:
798 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
799 AC_VERB_SET_CHANNEL_STREAMID, 0);
800 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
801 AC_VERB_SET_STREAM_FORMAT, 0);
802
803 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
804 break;
805
806 }
807
808 return 0;
809}
810
811static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
812 struct snd_kcontrol *kc, int event)
813{
Jeeja KP754695f2017-02-06 12:09:14 +0530814 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530815 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
816 int mux_idx;
817
818 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
819 __func__, w->name, event);
820
821 if (!kc)
822 kc = w->kcontrols[0];
823
824 mux_idx = dapm_kcontrol_get_value(kc);
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530825
826 /* set the device if pin is mst_capable */
827 if (hdac_hdmi_port_select_set(edev, port) < 0)
828 return -EIO;
829
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530830 if (mux_idx > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +0530831 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530832 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
833 }
834
835 return 0;
836}
837
Jeeja KP4a3478d2016-02-12 07:46:06 +0530838/*
839 * Based on user selection, map the PINs with the PCMs.
840 */
Jeeja KP754695f2017-02-06 12:09:14 +0530841static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530842 struct snd_ctl_elem_value *ucontrol)
843{
844 int ret;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530845 struct hdac_hdmi_port *p, *p_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530846 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
847 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
848 struct snd_soc_dapm_context *dapm = w->dapm;
Jeeja KP754695f2017-02-06 12:09:14 +0530849 struct hdac_hdmi_port *port = w->priv;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530850 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
851 struct hdac_hdmi_priv *hdmi = edev->private_data;
852 struct hdac_hdmi_pcm *pcm = NULL;
853 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
854
855 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
856 if (ret < 0)
857 return ret;
858
Jeeja KP754695f2017-02-06 12:09:14 +0530859 if (port == NULL)
860 return -EINVAL;
861
Jeeja KP4a3478d2016-02-12 07:46:06 +0530862 mutex_lock(&hdmi->pin_mutex);
863 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530864 if (list_empty(&pcm->port_list))
865 continue;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530866
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530867 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
868 if (p == port && p->id == port->id &&
869 p->pin == port->pin) {
870 hdac_hdmi_jack_report(pcm, port, false);
871 list_del(&p->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530872 }
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530873 }
874 }
875
876 /*
877 * Jack status is not reported during device probe as the
878 * PCMs are not registered by then. So report it here.
879 */
880 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
881 if (!strcmp(cvt_name, pcm->cvt->name)) {
882 list_add_tail(&port->head, &pcm->port_list);
883 if (port->eld.monitor_present && port->eld.eld_valid) {
884 hdac_hdmi_jack_report(pcm, port, true);
885 mutex_unlock(&hdmi->pin_mutex);
886 return ret;
887 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530888 }
889 }
890 mutex_unlock(&hdmi->pin_mutex);
891
892 return ret;
893}
894
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530895/*
896 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
897 * the display driver seem to be programming the connection list for the pin
898 * widget runtime.
899 *
900 * So programming all the possible inputs for the mux, the user has to take
901 * care of selecting the right one and leaving all other inputs selected to
902 * "NONE"
903 */
Jeeja KP754695f2017-02-06 12:09:14 +0530904static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
905 struct hdac_hdmi_port *port,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530906 struct snd_soc_dapm_widget *widget,
907 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530908{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530909 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530910 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530911 struct snd_kcontrol_new *kc;
912 struct hdac_hdmi_cvt *cvt;
913 struct soc_enum *se;
914 char kc_name[NAME_SIZE];
915 char mux_items[NAME_SIZE];
916 /* To hold inputs to the Pin mux */
917 char *items[HDA_MAX_CONNECTIONS];
918 int i = 0;
919 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530920
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530921 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
922 if (!kc)
923 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530924
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530925 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
926 if (!se)
927 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530928
Jeeja KP754695f2017-02-06 12:09:14 +0530929 sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530930 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
931 if (!kc->name)
932 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530933
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530934 kc->private_value = (long)se;
935 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
936 kc->access = 0;
937 kc->info = snd_soc_info_enum_double;
Jeeja KP754695f2017-02-06 12:09:14 +0530938 kc->put = hdac_hdmi_set_pin_port_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530939 kc->get = snd_soc_dapm_get_enum_double;
940
941 se->reg = SND_SOC_NOPM;
942
943 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
944 se->items = num_items;
945 se->mask = roundup_pow_of_two(se->items) - 1;
946
947 sprintf(mux_items, "NONE");
948 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
949 if (!items[i])
950 return -ENOMEM;
951
952 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
953 i++;
954 sprintf(mux_items, "cvt %d", cvt->nid);
955 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
956 if (!items[i])
957 return -ENOMEM;
958 }
959
960 se->texts = devm_kmemdup(&edev->hdac.dev, items,
961 (num_items * sizeof(char *)), GFP_KERNEL);
962 if (!se->texts)
963 return -ENOMEM;
964
965 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP754695f2017-02-06 12:09:14 +0530966 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530967 hdac_hdmi_pin_mux_widget_event,
968 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530969}
970
971/* Add cvt <- input <- mux route map */
972static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
973 struct snd_soc_dapm_widget *widgets,
974 struct snd_soc_dapm_route *route, int rindex)
975{
976 struct hdac_hdmi_priv *hdmi = edev->private_data;
977 const struct snd_kcontrol_new *kc;
978 struct soc_enum *se;
Jeeja KP754695f2017-02-06 12:09:14 +0530979 int mux_index = hdmi->num_cvt + hdmi->num_ports;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530980 int i, j;
981
Jeeja KP754695f2017-02-06 12:09:14 +0530982 for (i = 0; i < hdmi->num_ports; i++) {
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530983 kc = widgets[mux_index].kcontrol_news;
984 se = (struct soc_enum *)kc->private_value;
985 for (j = 0; j < hdmi->num_cvt; j++) {
986 hdac_hdmi_fill_route(&route[rindex],
987 widgets[mux_index].name,
988 se->texts[j + 1],
989 widgets[j].name, NULL);
990
991 rindex++;
992 }
993
994 mux_index++;
995 }
996}
997
998/*
999 * Widgets are added in the below sequence
1000 * Converter widgets for num converters enumerated
Jeeja KP754695f2017-02-06 12:09:14 +05301001 * Pin-port widgets for num ports for Pins enumerated
1002 * Pin-port mux widgets to represent connenction list of pin widget
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301003 *
Jeeja KP754695f2017-02-06 12:09:14 +05301004 * For each port, one Mux and One output widget is added
1005 * Total widgets elements = num_cvt + (num_ports * 2);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301006 *
1007 * Routes are added as below:
Jeeja KP754695f2017-02-06 12:09:14 +05301008 * pin-port mux -> pin (based on num_ports)
1009 * cvt -> "Input sel control" -> pin-port_mux
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301010 *
1011 * Total route elements:
Jeeja KP754695f2017-02-06 12:09:14 +05301012 * num_ports + (pin_muxes * num_cvt)
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301013 */
1014static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1015{
1016 struct snd_soc_dapm_widget *widgets;
1017 struct snd_soc_dapm_route *route;
1018 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
1019 struct hdac_hdmi_priv *hdmi = edev->private_data;
1020 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
1021 char widget_name[NAME_SIZE];
1022 struct hdac_hdmi_cvt *cvt;
1023 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301024 int ret, i = 0, num_routes = 0, j;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301025
1026 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1027 return -EINVAL;
1028
Jeeja KP754695f2017-02-06 12:09:14 +05301029 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1030 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1031 GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301032
1033 if (!widgets)
1034 return -ENOMEM;
1035
1036 /* DAPM widgets to represent each converter widget */
1037 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1038 sprintf(widget_name, "Converter %d", cvt->nid);
1039 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301040 snd_soc_dapm_aif_in, cvt,
1041 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1042 hdac_hdmi_cvt_output_widget_event,
1043 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301044 if (ret < 0)
1045 return ret;
1046 i++;
1047 }
1048
1049 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301050 for (j = 0; j < pin->num_ports; j++) {
1051 sprintf(widget_name, "hif%d-%d Output",
1052 pin->nid, pin->ports[j].id);
1053 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1054 snd_soc_dapm_output, &pin->ports[j],
1055 widget_name, NULL, NULL, 0,
1056 hdac_hdmi_pin_output_widget_event,
1057 SND_SOC_DAPM_PRE_PMU |
1058 SND_SOC_DAPM_POST_PMD);
1059 if (ret < 0)
1060 return ret;
1061 i++;
1062 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301063 }
1064
1065 /* DAPM widgets to represent the connection list to pin widget */
1066 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301067 for (j = 0; j < pin->num_ports; j++) {
1068 sprintf(widget_name, "Pin%d-Port%d Mux",
1069 pin->nid, pin->ports[j].id);
1070 ret = hdac_hdmi_create_pin_port_muxs(edev,
1071 &pin->ports[j], &widgets[i],
1072 widget_name);
1073 if (ret < 0)
1074 return ret;
1075 i++;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301076
Jeeja KP754695f2017-02-06 12:09:14 +05301077 /* For cvt to pin_mux mapping */
1078 num_routes += hdmi->num_cvt;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301079
Jeeja KP754695f2017-02-06 12:09:14 +05301080 /* For pin_mux to pin mapping */
1081 num_routes++;
1082 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301083 }
1084
1085 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1086 GFP_KERNEL);
1087 if (!route)
1088 return -ENOMEM;
1089
1090 i = 0;
1091 /* Add pin <- NULL <- mux route map */
1092 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301093 for (j = 0; j < pin->num_ports; j++) {
1094 int sink_index = i + hdmi->num_cvt;
1095 int src_index = sink_index + pin->num_ports *
1096 hdmi->num_pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301097
Jeeja KP754695f2017-02-06 12:09:14 +05301098 hdac_hdmi_fill_route(&route[i],
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301099 widgets[sink_index].name, NULL,
1100 widgets[src_index].name, NULL);
Jeeja KP754695f2017-02-06 12:09:14 +05301101 i++;
1102 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301103 }
1104
1105 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
1106
1107 snd_soc_dapm_new_controls(dapm, widgets,
Jeeja KP754695f2017-02-06 12:09:14 +05301108 ((2 * hdmi->num_ports) + hdmi->num_cvt));
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301109
1110 snd_soc_dapm_add_routes(dapm, route, num_routes);
1111 snd_soc_dapm_new_widgets(dapm->card);
1112
1113 return 0;
1114
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301115}
1116
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301117static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301118{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301119 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +05301120 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301121 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301122 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301123
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301124 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301125 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301126
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301127 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1128 dai_map = &hdmi->dai_map[dai_id];
1129 dai_map->dai_id = dai_id;
1130 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301131
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301132 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301133
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301134 if (dai_id == HDA_MAX_CVTS) {
1135 dev_warn(&edev->hdac.dev,
1136 "Max dais supported: %d\n", dai_id);
1137 break;
1138 }
1139 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301140
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301141 return 0;
1142}
1143
1144static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1145{
1146 struct hdac_hdmi_priv *hdmi = edev->private_data;
1147 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301148 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301149
1150 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1151 if (!cvt)
1152 return -ENOMEM;
1153
1154 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301155 sprintf(name, "cvt %d", cvt->nid);
1156 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301157
1158 list_add_tail(&cvt->head, &hdmi->cvt_list);
1159 hdmi->num_cvt++;
1160
1161 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1162}
1163
Jeeja KP754695f2017-02-06 12:09:14 +05301164static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1165 struct hdac_hdmi_port *port)
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301166{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301167 unsigned int ver, mnl;
1168
Jeeja KP754695f2017-02-06 12:09:14 +05301169 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301170 >> DRM_ELD_VER_SHIFT;
1171
1172 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1173 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1174 return -EINVAL;
1175 }
1176
Jeeja KP754695f2017-02-06 12:09:14 +05301177 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301178 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1179
1180 if (mnl > ELD_MAX_MNL) {
1181 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1182 return -EINVAL;
1183 }
1184
Jeeja KP754695f2017-02-06 12:09:14 +05301185 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301186
1187 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301188}
1189
Jeeja KP754695f2017-02-06 12:09:14 +05301190static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1191 struct hdac_hdmi_port *port)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301192{
1193 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301194 struct hdac_hdmi_priv *hdmi = edev->private_data;
1195 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +05301196 int size = 0;
Jeeja KP2acd8302017-02-06 12:09:18 +05301197 int port_id = -1;
Jeeja KP754695f2017-02-06 12:09:14 +05301198
1199 if (!hdmi)
1200 return;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301201
Jeeja KP2acd8302017-02-06 12:09:18 +05301202 /*
1203 * In case of non MST pin, get_eld info API expectes port
1204 * to be -1.
1205 */
Jeeja KP4a3478d2016-02-12 07:46:06 +05301206 mutex_lock(&hdmi->pin_mutex);
Jeeja KP754695f2017-02-06 12:09:14 +05301207 port->eld.monitor_present = false;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301208
Jeeja KP2acd8302017-02-06 12:09:18 +05301209 if (pin->mst_capable)
1210 port_id = port->id;
1211
1212 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
Jeeja KP754695f2017-02-06 12:09:14 +05301213 &port->eld.monitor_present,
1214 port->eld.eld_buffer,
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301215 ELD_MAX_SIZE);
1216
1217 if (size > 0) {
1218 size = min(size, ELD_MAX_SIZE);
Jeeja KP754695f2017-02-06 12:09:14 +05301219 if (hdac_hdmi_parse_eld(edev, port) < 0)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301220 size = -EINVAL;
1221 }
1222
1223 if (size > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +05301224 port->eld.eld_valid = true;
1225 port->eld.eld_size = size;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301226 } else {
Jeeja KP754695f2017-02-06 12:09:14 +05301227 port->eld.eld_valid = false;
1228 port->eld.eld_size = 0;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301229 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301230
Jeeja KP754695f2017-02-06 12:09:14 +05301231 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301232
Jeeja KP754695f2017-02-06 12:09:14 +05301233 if (!port->eld.monitor_present || !port->eld.eld_valid) {
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301234
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301235 dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
Jeeja KP754695f2017-02-06 12:09:14 +05301236 __func__, pin->nid, port->id);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301237
1238 /*
1239 * PCMs are not registered during device probe, so don't
1240 * report jack here. It will be done in usermode mux
1241 * control select.
1242 */
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301243 if (pcm)
1244 hdac_hdmi_jack_report(pcm, port, false);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301245
1246 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301247 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301248 }
1249
Jeeja KP754695f2017-02-06 12:09:14 +05301250 if (port->eld.monitor_present && port->eld.eld_valid) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301251 if (pcm)
1252 hdac_hdmi_jack_report(pcm, port, true);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301253
1254 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
Jeeja KP754695f2017-02-06 12:09:14 +05301255 port->eld.eld_buffer, port->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301256
Jeeja KP754695f2017-02-06 12:09:14 +05301257 }
Jeeja KP4a3478d2016-02-12 07:46:06 +05301258 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301259}
1260
Jeeja KP754695f2017-02-06 12:09:14 +05301261static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1262 struct hdac_hdmi_pin *pin)
1263{
1264 struct hdac_hdmi_port *ports;
1265 int max_ports = HDA_MAX_PORTS;
1266 int i;
1267
1268 /*
1269 * FIXME: max_port may vary for each platform, so pass this as
1270 * as driver data or query from i915 interface when this API is
1271 * implemented.
1272 */
1273
1274 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1275 if (!ports)
1276 return -ENOMEM;
1277
1278 for (i = 0; i < max_ports; i++) {
1279 ports[i].id = i;
1280 ports[i].pin = pin;
1281 }
1282 pin->ports = ports;
1283 pin->num_ports = max_ports;
1284 return 0;
1285}
1286
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301287static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1288{
1289 struct hdac_hdmi_priv *hdmi = edev->private_data;
1290 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301291 int ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301292
1293 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1294 if (!pin)
1295 return -ENOMEM;
1296
1297 pin->nid = nid;
Jeeja KP2acd8302017-02-06 12:09:18 +05301298 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301299 pin->edev = edev;
1300 ret = hdac_hdmi_add_ports(hdmi, pin);
1301 if (ret < 0)
1302 return ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301303
1304 list_add_tail(&pin->head, &hdmi->pin_list);
1305 hdmi->num_pin++;
Jeeja KP754695f2017-02-06 12:09:14 +05301306 hdmi->num_ports += pin->num_ports;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301307
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301308 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301309}
1310
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301311#define INTEL_VENDOR_NID 0x08
1312#define INTEL_GET_VENDOR_VERB 0xf81
1313#define INTEL_SET_VENDOR_VERB 0x781
1314#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1315#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1316
1317static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1318{
1319 unsigned int vendor_param;
1320
1321 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1322 INTEL_GET_VENDOR_VERB, 0);
1323 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1324 return;
1325
1326 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1327 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1328 INTEL_SET_VENDOR_VERB, vendor_param);
1329 if (vendor_param == -1)
1330 return;
1331}
1332
1333static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1334{
1335 unsigned int vendor_param;
1336
1337 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1338 INTEL_GET_VENDOR_VERB, 0);
1339 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1340 return;
1341
1342 /* enable DP1.2 mode */
1343 vendor_param |= INTEL_EN_DP12;
1344 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1345 INTEL_SET_VENDOR_VERB, vendor_param);
1346 if (vendor_param == -1)
1347 return;
1348
1349}
1350
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301351static struct snd_soc_dai_ops hdmi_dai_ops = {
1352 .startup = hdac_hdmi_pcm_open,
1353 .shutdown = hdac_hdmi_pcm_close,
1354 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301355 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301356};
1357
1358/*
1359 * Each converter can support a stream independently. So a dai is created
1360 * based on the number of converter queried.
1361 */
1362static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1363 struct snd_soc_dai_driver **dais,
1364 struct hdac_hdmi_priv *hdmi, int num_dais)
1365{
1366 struct snd_soc_dai_driver *hdmi_dais;
1367 struct hdac_hdmi_cvt *cvt;
1368 char name[NAME_SIZE], dai_name[NAME_SIZE];
1369 int i = 0;
1370 u32 rates, bps;
1371 unsigned int rate_max = 384000, rate_min = 8000;
1372 u64 formats;
1373 int ret;
1374
1375 hdmi_dais = devm_kzalloc(&hdac->dev,
1376 (sizeof(*hdmi_dais) * num_dais),
1377 GFP_KERNEL);
1378 if (!hdmi_dais)
1379 return -ENOMEM;
1380
1381 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1382 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1383 &rates, &formats, &bps);
1384 if (ret)
1385 return ret;
1386
1387 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1388 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1389 dai_name, GFP_KERNEL);
1390
1391 if (!hdmi_dais[i].name)
1392 return -ENOMEM;
1393
1394 snprintf(name, sizeof(name), "hifi%d", i+1);
1395 hdmi_dais[i].playback.stream_name =
1396 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1397 if (!hdmi_dais[i].playback.stream_name)
1398 return -ENOMEM;
1399
1400 /*
1401 * Set caps based on capability queried from the converter.
1402 * It will be constrained runtime based on ELD queried.
1403 */
1404 hdmi_dais[i].playback.formats = formats;
1405 hdmi_dais[i].playback.rates = rates;
1406 hdmi_dais[i].playback.rate_max = rate_max;
1407 hdmi_dais[i].playback.rate_min = rate_min;
1408 hdmi_dais[i].playback.channels_min = 2;
1409 hdmi_dais[i].playback.channels_max = 2;
1410 hdmi_dais[i].ops = &hdmi_dai_ops;
1411
1412 i++;
1413 }
1414
1415 *dais = hdmi_dais;
1416
1417 return 0;
1418}
1419
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301420/*
1421 * Parse all nodes and store the cvt/pin nids in array
1422 * Add one time initialization for pin and cvt widgets
1423 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301424static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1425 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301426{
1427 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301428 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301429 struct hdac_device *hdac = &edev->hdac;
1430 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301431 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301432
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301433 hdac_hdmi_skl_enable_all_pins(hdac);
1434 hdac_hdmi_skl_enable_dp12(hdac);
1435
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301436 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301437 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301438 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1439 return -EINVAL;
1440 }
1441
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301442 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301443 hdac->start_nid = nid;
1444
1445 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1446 unsigned int caps;
1447 unsigned int type;
1448
1449 caps = get_wcaps(hdac, nid);
1450 type = get_wcaps_type(caps);
1451
1452 if (!(caps & AC_WCAP_DIGITAL))
1453 continue;
1454
1455 switch (type) {
1456
1457 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301458 ret = hdac_hdmi_add_cvt(edev, nid);
1459 if (ret < 0)
1460 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301461 break;
1462
1463 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301464 ret = hdac_hdmi_add_pin(edev, nid);
1465 if (ret < 0)
1466 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301467 break;
1468 }
1469 }
1470
1471 hdac->end_nid = nid;
1472
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301473 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301474 return -EIO;
1475
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301476 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1477 if (ret) {
1478 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1479 ret);
1480 return ret;
1481 }
1482
1483 *num_dais = hdmi->num_cvt;
1484
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301485 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301486}
1487
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001488static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301489{
1490 struct hdac_ext_device *edev = aptr;
1491 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +05301492 struct hdac_hdmi_pin *pin = NULL;
1493 struct hdac_hdmi_port *hport = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301494 struct snd_soc_codec *codec = edev->scodec;
Jeeja KP2acd8302017-02-06 12:09:18 +05301495 int i;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301496
1497 /* Don't know how this mapping is derived */
1498 hda_nid_t pin_nid = port + 0x04;
1499
Jeeja KP754695f2017-02-06 12:09:14 +05301500 dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1501 pin_nid, pipe);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301502
1503 /*
1504 * skip notification during system suspend (but not in runtime PM);
1505 * the state will be updated at resume. Also since the ELD and
1506 * connection states are updated in anyway at the end of the resume,
1507 * we can skip it when received during PM process.
1508 */
1509 if (snd_power_get_state(codec->component.card->snd_card) !=
1510 SNDRV_CTL_POWER_D0)
1511 return;
1512
1513 if (atomic_read(&edev->hdac.in_pm))
1514 return;
1515
1516 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301517 if (pin->nid != pin_nid)
1518 continue;
1519
1520 /* In case of non MST pin, pipe is -1 */
1521 if (pipe == -1) {
Jeeja KP2acd8302017-02-06 12:09:18 +05301522 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301523 /* if not MST, default is port[0] */
1524 hport = &pin->ports[0];
Jeeja KP2acd8302017-02-06 12:09:18 +05301525 goto out;
1526 } else {
1527 for (i = 0; i < pin->num_ports; i++) {
1528 pin->mst_capable = true;
1529 if (pin->ports[i].id == pipe) {
1530 hport = &pin->ports[i];
1531 goto out;
1532 }
1533 }
Jeeja KP754695f2017-02-06 12:09:14 +05301534 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301535 }
Jeeja KP754695f2017-02-06 12:09:14 +05301536
Jeeja KP2acd8302017-02-06 12:09:18 +05301537out:
1538 if (pin && hport)
Jeeja KP754695f2017-02-06 12:09:14 +05301539 hdac_hdmi_present_sense(pin, hport);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301540}
1541
1542static struct i915_audio_component_audio_ops aops = {
1543 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1544};
1545
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301546static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1547 int device)
1548{
1549 struct snd_soc_pcm_runtime *rtd;
1550
1551 list_for_each_entry(rtd, &card->rtd_list, list) {
1552 if (rtd->pcm && (rtd->pcm->device == device))
1553 return rtd->pcm;
1554 }
1555
1556 return NULL;
1557}
1558
Jeeja KP4a3478d2016-02-12 07:46:06 +05301559int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1560{
1561 char jack_name[NAME_SIZE];
1562 struct snd_soc_codec *codec = dai->codec;
1563 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1564 struct snd_soc_dapm_context *dapm =
1565 snd_soc_component_get_dapm(&codec->component);
1566 struct hdac_hdmi_priv *hdmi = edev->private_data;
1567 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301568 struct snd_pcm *snd_pcm;
1569 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301570
1571 /*
1572 * this is a new PCM device, create new pcm and
1573 * add to the pcm list
1574 */
1575 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1576 if (!pcm)
1577 return -ENOMEM;
1578 pcm->pcm_id = device;
1579 pcm->cvt = hdmi->dai_map[dai->id].cvt;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301580 pcm->jack_event = 0;
Jeeja KPab1eea12017-01-24 21:49:05 +05301581 mutex_init(&pcm->lock);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301582 INIT_LIST_HEAD(&pcm->port_list);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301583 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1584 if (snd_pcm) {
1585 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1586 if (err < 0) {
1587 dev_err(&edev->hdac.dev,
1588 "chmap control add failed with err: %d for pcm: %d\n",
1589 err, device);
1590 kfree(pcm);
1591 return err;
1592 }
1593 }
1594
Jeeja KP4a3478d2016-02-12 07:46:06 +05301595 list_add_tail(&pcm->head, &hdmi->pcm_list);
1596
1597 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1598
1599 return snd_jack_new(dapm->card->snd_card, jack_name,
1600 SND_JACK_AVOUT, &pcm->jack, true, false);
1601}
1602EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1603
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301604static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1605 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1606{
1607 int i;
1608 struct hdac_hdmi_pin *pin;
1609
1610 list_for_each_entry(pin, &hdmi->pin_list, head) {
1611 if (detect_pin_caps) {
1612
1613 if (hdac_hdmi_get_port_len(edev, pin->nid) == 0)
1614 pin->mst_capable = false;
1615 else
1616 pin->mst_capable = true;
1617 }
1618
1619 for (i = 0; i < pin->num_ports; i++) {
1620 if (!pin->mst_capable && i > 0)
1621 continue;
1622
1623 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1624 }
1625 }
1626}
1627
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301628static int hdmi_codec_probe(struct snd_soc_codec *codec)
1629{
1630 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1631 struct hdac_hdmi_priv *hdmi = edev->private_data;
1632 struct snd_soc_dapm_context *dapm =
1633 snd_soc_component_get_dapm(&codec->component);
Vinod Koulb2047e92016-05-12 08:58:55 +05301634 struct hdac_ext_link *hlink = NULL;
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301635 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301636
1637 edev->scodec = codec;
1638
Vinod Koulb2047e92016-05-12 08:58:55 +05301639 /*
1640 * hold the ref while we probe, also no need to drop the ref on
1641 * exit, we call pm_runtime_suspend() so that will do for us
1642 */
1643 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301644 if (!hlink) {
1645 dev_err(&edev->hdac.dev, "hdac link not found\n");
1646 return -EIO;
1647 }
1648
Vinod Koulb2047e92016-05-12 08:58:55 +05301649 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1650
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301651 ret = create_fill_widget_route_map(dapm);
1652 if (ret < 0)
1653 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301654
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301655 aops.audio_ptr = edev;
1656 ret = snd_hdac_i915_register_notifier(&aops);
1657 if (ret < 0) {
1658 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1659 ret);
1660 return ret;
1661 }
1662
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301663 hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301664 /* Imp: Store the card pointer in hda_codec */
1665 edev->card = dapm->card->snd_card;
1666
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301667 /*
1668 * hdac_device core already sets the state to active and calls
1669 * get_noresume. So enable runtime and set the device to suspend.
1670 */
1671 pm_runtime_enable(&edev->hdac.dev);
1672 pm_runtime_put(&edev->hdac.dev);
1673 pm_runtime_suspend(&edev->hdac.dev);
1674
1675 return 0;
1676}
1677
1678static int hdmi_codec_remove(struct snd_soc_codec *codec)
1679{
1680 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1681
1682 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301683 return 0;
1684}
1685
Jeeja KP571d5072016-02-22 07:50:33 +05301686#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301687static int hdmi_codec_prepare(struct device *dev)
1688{
1689 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1690 struct hdac_device *hdac = &edev->hdac;
1691
1692 pm_runtime_get_sync(&edev->hdac.dev);
1693
1694 /*
1695 * Power down afg.
1696 * codec_read is preferred over codec_write to set the power state.
1697 * This way verb is send to set the power state and response
1698 * is received. So setting power state is ensured without using loop
1699 * to read the state.
1700 */
1701 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1702 AC_PWRST_D3);
1703
1704 return 0;
1705}
1706
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301707static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301708{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301709 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301710 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP571d5072016-02-22 07:50:33 +05301711 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301712
1713 /* Power up afg */
1714 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1715 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301716
1717 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1718 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1719
Jeeja KP571d5072016-02-22 07:50:33 +05301720 /*
1721 * As the ELD notify callback request is not entertained while the
1722 * device is in suspend state. Need to manually check detection of
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301723 * all pins here. pin capablity change is not support, so use the
1724 * already set pin caps.
Jeeja KP571d5072016-02-22 07:50:33 +05301725 */
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301726 hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
Jeeja KP571d5072016-02-22 07:50:33 +05301727
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301728 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301729}
1730#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301731#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301732#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301733#endif
1734
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301735static struct snd_soc_codec_driver hdmi_hda_codec = {
1736 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301737 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301738 .idle_bias_off = true,
1739};
1740
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301741static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1742 unsigned char *chmap)
1743{
1744 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1745 struct hdac_hdmi_priv *hdmi = edev->private_data;
1746 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301747
Jeeja KPab1eea12017-01-24 21:49:05 +05301748 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301749}
1750
1751static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1752 unsigned char *chmap, int prepared)
1753{
1754 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1755 struct hdac_hdmi_priv *hdmi = edev->private_data;
1756 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301757 struct hdac_hdmi_port *port;
1758
1759 if (list_empty(&pcm->port_list))
1760 return;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301761
Jeeja KPab1eea12017-01-24 21:49:05 +05301762 mutex_lock(&pcm->lock);
1763 pcm->chmap_set = true;
1764 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301765 list_for_each_entry(port, &pcm->port_list, head)
1766 if (prepared)
1767 hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPab1eea12017-01-24 21:49:05 +05301768 mutex_unlock(&pcm->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301769}
1770
1771static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1772{
1773 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1774 struct hdac_hdmi_priv *hdmi = edev->private_data;
1775 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301776
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301777 if (list_empty(&pcm->port_list))
1778 return false;
1779
1780 return true;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301781}
1782
1783static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1784{
1785 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1786 struct hdac_hdmi_priv *hdmi = edev->private_data;
1787 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301788 struct hdac_hdmi_port *port;
1789
1790 if (list_empty(&pcm->port_list))
1791 return 0;
1792
1793 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1794
1795 if (!port)
1796 return 0;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301797
Jeeja KP754695f2017-02-06 12:09:14 +05301798 if (!port || !port->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301799 return 0;
1800
Jeeja KP754695f2017-02-06 12:09:14 +05301801 return port->eld.info.spk_alloc;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301802}
1803
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301804static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1805{
1806 struct hdac_device *codec = &edev->hdac;
1807 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301808 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301809 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301810 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301811 int ret = 0;
1812
Vinod Koulb2047e92016-05-12 08:58:55 +05301813 /* hold the ref while we probe */
1814 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301815 if (!hlink) {
1816 dev_err(&edev->hdac.dev, "hdac link not found\n");
1817 return -EIO;
1818 }
1819
Vinod Koulb2047e92016-05-12 08:58:55 +05301820 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1821
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301822 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1823 if (hdmi_priv == NULL)
1824 return -ENOMEM;
1825
1826 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301827 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301828 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1829 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1830 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1831 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301832
1833 dev_set_drvdata(&codec->dev, edev);
1834
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301835 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1836 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301837 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1838 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301839
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301840 /*
1841 * Turned off in the runtime_suspend during the first explicit
1842 * pm_runtime_suspend call.
1843 */
1844 ret = snd_hdac_display_power(edev->hdac.bus, true);
1845 if (ret < 0) {
1846 dev_err(&edev->hdac.dev,
1847 "Cannot turn on display power on i915 err: %d\n",
1848 ret);
1849 return ret;
1850 }
1851
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301852 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1853 if (ret < 0) {
1854 dev_err(&codec->dev,
1855 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301856 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301857 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301858
1859 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301860 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1861 hdmi_dais, num_dais);
1862
1863 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1864
1865 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301866}
1867
1868static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1869{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301870 struct hdac_hdmi_priv *hdmi = edev->private_data;
1871 struct hdac_hdmi_pin *pin, *pin_next;
1872 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301873 struct hdac_hdmi_pcm *pcm, *pcm_next;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301874 struct hdac_hdmi_port *port;
Jeeja KP754695f2017-02-06 12:09:14 +05301875 int i;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301876
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301877 snd_soc_unregister_codec(&edev->hdac.dev);
1878
Jeeja KP4a3478d2016-02-12 07:46:06 +05301879 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1880 pcm->cvt = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301881 if (list_empty(&pcm->port_list))
1882 continue;
1883
1884 list_for_each_entry(port, &pcm->port_list, head)
1885 port = NULL;
1886
Jeeja KP4a3478d2016-02-12 07:46:06 +05301887 list_del(&pcm->head);
1888 kfree(pcm);
1889 }
1890
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301891 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1892 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301893 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301894 kfree(cvt);
1895 }
1896
1897 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301898 for (i = 0; i < pin->num_ports; i++)
1899 pin->ports[i].pin = NULL;
1900 kfree(pin->ports);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301901 list_del(&pin->head);
1902 kfree(pin);
1903 }
1904
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301905 return 0;
1906}
1907
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301908#ifdef CONFIG_PM
1909static int hdac_hdmi_runtime_suspend(struct device *dev)
1910{
1911 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1912 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301913 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301914 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1915 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301916 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301917
1918 dev_dbg(dev, "Enter: %s\n", __func__);
1919
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301920 /* controller may not have been initialized for the first time */
1921 if (!bus)
1922 return 0;
1923
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301924 /*
1925 * Power down afg.
1926 * codec_read is preferred over codec_write to set the power state.
1927 * This way verb is send to set the power state and response
1928 * is received. So setting power state is ensured without using loop
1929 * to read the state.
1930 */
1931 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1932 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301933 err = snd_hdac_display_power(bus, false);
1934 if (err < 0) {
1935 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1936 return err;
1937 }
1938
Vinod Koulb2047e92016-05-12 08:58:55 +05301939 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301940 if (!hlink) {
1941 dev_err(dev, "hdac link not found\n");
1942 return -EIO;
1943 }
1944
Vinod Koulb2047e92016-05-12 08:58:55 +05301945 snd_hdac_ext_bus_link_put(ebus, hlink);
1946
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301947 return 0;
1948}
1949
1950static int hdac_hdmi_runtime_resume(struct device *dev)
1951{
1952 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1953 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301954 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301955 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1956 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301957 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301958
1959 dev_dbg(dev, "Enter: %s\n", __func__);
1960
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301961 /* controller may not have been initialized for the first time */
1962 if (!bus)
1963 return 0;
1964
Vinod Koulb2047e92016-05-12 08:58:55 +05301965 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301966 if (!hlink) {
1967 dev_err(dev, "hdac link not found\n");
1968 return -EIO;
1969 }
1970
Vinod Koulb2047e92016-05-12 08:58:55 +05301971 snd_hdac_ext_bus_link_get(ebus, hlink);
1972
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301973 err = snd_hdac_display_power(bus, true);
1974 if (err < 0) {
1975 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1976 return err;
1977 }
1978
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301979 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1980 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1981
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301982 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301983 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1984 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301985
1986 return 0;
1987}
1988#else
1989#define hdac_hdmi_runtime_suspend NULL
1990#define hdac_hdmi_runtime_resume NULL
1991#endif
1992
1993static const struct dev_pm_ops hdac_hdmi_pm = {
1994 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301995 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301996 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301997};
1998
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301999static const struct hda_device_id hdmi_list[] = {
2000 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05302001 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05302002 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302003 {}
2004};
2005
2006MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2007
2008static struct hdac_ext_driver hdmi_driver = {
2009 . hdac = {
2010 .driver = {
2011 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302012 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302013 },
2014 .id_table = hdmi_list,
2015 },
2016 .probe = hdac_hdmi_dev_probe,
2017 .remove = hdac_hdmi_dev_remove,
2018};
2019
2020static int __init hdmi_init(void)
2021{
2022 return snd_hda_ext_driver_register(&hdmi_driver);
2023}
2024
2025static void __exit hdmi_exit(void)
2026{
2027 snd_hda_ext_driver_unregister(&hdmi_driver);
2028}
2029
2030module_init(hdmi_init);
2031module_exit(hdmi_exit);
2032
2033MODULE_LICENSE("GPL v2");
2034MODULE_DESCRIPTION("HDMI HD codec");
2035MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2036MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");