blob: c5527e81a4902d24b486e02e09697c0aa50d50a4 [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 {
92 int id;
93 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053094 int num_mux_nids;
95 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053096 struct hdac_hdmi_eld eld;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053097};
98
Jeeja KP4a3478d2016-02-12 07:46:06 +053099struct hdac_hdmi_pcm {
100 struct list_head head;
101 int pcm_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530102 struct hdac_hdmi_port *port;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530103 struct hdac_hdmi_cvt *cvt;
104 struct snd_jack *jack;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530105 int stream_tag;
106 int channels;
107 int format;
Jeeja KPab1eea12017-01-24 21:49:05 +0530108 bool chmap_set;
109 unsigned char chmap[8]; /* ALSA API channel-map */
110 struct mutex lock;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530111};
112
Jeeja KP754695f2017-02-06 12:09:14 +0530113struct hdac_hdmi_dai_port_map {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530114 int dai_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530115 struct hdac_hdmi_port *port;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530116 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530117};
118
119struct hdac_hdmi_priv {
Jeeja KP754695f2017-02-06 12:09:14 +0530120 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530121 struct list_head pin_list;
122 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530123 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530124 int num_pin;
125 int num_cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530126 int num_ports;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530127 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530128 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530129};
130
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530131static struct hdac_hdmi_pcm *
132hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
133 struct hdac_hdmi_cvt *cvt)
134{
135 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530136
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530137 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
138 if (pcm->cvt == cvt)
139 break;
140 }
141
142 return pcm;
143}
Jeeja KP1de777f2017-01-10 17:57:48 +0530144
Jeeja KPfc181b02017-02-07 19:09:45 +0530145/* MST supported verbs */
146/*
147 * Get the no devices that can be connected to a port on the Pin widget.
148 */
149static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
150{
151 unsigned int caps;
152 unsigned int type, param;
153
154 caps = get_wcaps(&hdac->hdac, nid);
155 type = get_wcaps_type(caps);
156
157 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
158 return 0;
159
160 param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
161 AC_PAR_DEVLIST_LEN);
162 if (param == -1)
163 return param;
164
165 return param & AC_DEV_LIST_LEN_MASK;
166}
167
168/*
169 * Get the port entry select on the pin. Return the port entry
170 * id selected on the pin. Return 0 means the first port entry
171 * is selected or MST is not supported.
172 */
173static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
174 struct hdac_hdmi_port *port)
175{
176 return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
177 0, AC_VERB_GET_DEVICE_SEL, 0);
178}
179
180/*
181 * Sets the selected port entry for the configuring Pin widget verb.
182 * returns error if port set is not equal to port get otherwise success
183 */
184static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
185 struct hdac_hdmi_port *port)
186{
187 int num_ports;
188
189 if (!port->pin->mst_capable)
190 return 0;
191
192 /* AC_PAR_DEVLIST_LEN is 0 based. */
193 num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
194
195 if (num_ports < 0)
196 return -EIO;
197 /*
198 * Device List Length is a 0 based integer value indicating the
199 * number of sink device that a MST Pin Widget can support.
200 */
201 if (num_ports + 1 < port->id)
202 return 0;
203
204 snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
205 AC_VERB_SET_DEVICE_SEL, port->id);
206
207 if (port->id != hdac_hdmi_port_select_get(hdac, port))
208 return -EIO;
209
210 dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
211
212 return 0;
213}
214
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530215static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
216 int pcm_idx)
217{
218 struct hdac_hdmi_pcm *pcm;
219
220 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
221 if (pcm->pcm_id == pcm_idx)
222 return pcm;
223 }
224
225 return NULL;
226}
227
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530228static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
229{
Geliang Tang51b2c422015-12-28 22:47:13 +0800230 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530231
Geliang Tang51b2c422015-12-28 22:47:13 +0800232 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530233}
234
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530235static unsigned int sad_format(const u8 *sad)
236{
237 return ((sad[0] >> 0x3) & 0x1f);
238}
239
240static unsigned int sad_sample_bits_lpcm(const u8 *sad)
241{
242 return (sad[2] & 7);
243}
244
245static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
246 void *eld)
247{
248 u64 formats = SNDRV_PCM_FMTBIT_S16;
249 int i;
250 const u8 *sad, *eld_buf = eld;
251
252 sad = drm_eld_sad(eld_buf);
253 if (!sad)
254 goto format_constraint;
255
256 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
257 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
258
259 /*
260 * the controller support 20 and 24 bits in 32 bit
261 * container so we set S32
262 */
263 if (sad_sample_bits_lpcm(sad) & 0x6)
264 formats |= SNDRV_PCM_FMTBIT_S32;
265 }
266 }
267
268format_constraint:
269 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
270 formats);
271
272}
273
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530274static void
275hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
276 int packet_index, int byte_index)
277{
278 int val;
279
280 val = (packet_index << 5) | (byte_index & 0x1f);
281
282 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
283 AC_VERB_SET_HDMI_DIP_INDEX, val);
284}
285
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530286struct dp_audio_infoframe {
287 u8 type; /* 0x84 */
288 u8 len; /* 0x1b */
289 u8 ver; /* 0x11 << 2 */
290
291 u8 CC02_CT47; /* match with HDMI infoframe from this on */
292 u8 SS01_SF24;
293 u8 CXT04;
294 u8 CA;
295 u8 LFEPBL01_LSV36_DM_INH7;
296};
297
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530298static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
Jeeja KP754695f2017-02-06 12:09:14 +0530299 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530300{
301 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
302 struct hdmi_audio_infoframe frame;
Jeeja KP754695f2017-02-06 12:09:14 +0530303 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530304 struct dp_audio_infoframe dp_ai;
305 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KPab1eea12017-01-24 21:49:05 +0530306 struct hdac_hdmi_cvt *cvt = pcm->cvt;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530307 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530308 int ret;
309 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530310 const u8 *eld_buf;
311 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530312 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530313
Jeeja KP754695f2017-02-06 12:09:14 +0530314 ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
Jeeja KPab1eea12017-01-24 21:49:05 +0530315 pcm->channels, pcm->chmap_set, true, pcm->chmap);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530316
317 channels = snd_hdac_get_active_channels(ca);
Jeeja KPab1eea12017-01-24 21:49:05 +0530318 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530319
320 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
Jeeja KPab1eea12017-01-24 21:49:05 +0530321 pcm->channels, pcm->chmap, pcm->chmap_set);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530322
Jeeja KP754695f2017-02-06 12:09:14 +0530323 eld_buf = port->eld.eld_buffer;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530324 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530325
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530326 switch (conn_type) {
327 case DRM_ELD_CONN_TYPE_HDMI:
328 hdmi_audio_infoframe_init(&frame);
329
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530330 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530331 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530332
333 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
334 if (ret < 0)
335 return ret;
336
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530337 break;
338
339 case DRM_ELD_CONN_TYPE_DP:
340 memset(&dp_ai, 0, sizeof(dp_ai));
341 dp_ai.type = 0x84;
342 dp_ai.len = 0x1b;
343 dp_ai.ver = 0x11 << 2;
344 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530345 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530346
347 dip = (u8 *)&dp_ai;
348 break;
349
350 default:
351 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
352 conn_type);
353 return -EIO;
354 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530355
356 /* stop infoframe transmission */
Jeeja KPab1eea12017-01-24 21:49:05 +0530357 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
358 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530359 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
360
361
362 /* Fill infoframe. Index auto-incremented */
Jeeja KPab1eea12017-01-24 21:49:05 +0530363 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530364 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530365 for (i = 0; i < sizeof(buffer); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530366 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530367 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530368 } else {
369 for (i = 0; i < sizeof(dp_ai); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530370 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530371 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
372 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530373
374 /* Start infoframe */
Jeeja KPab1eea12017-01-24 21:49:05 +0530375 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
376 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530377 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
378
379 return 0;
380}
381
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530382static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
383 unsigned int tx_mask, unsigned int rx_mask,
384 int slots, int slot_width)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530385{
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530386 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
387 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530388 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530389 struct hdac_hdmi_pcm *pcm;
390
391 dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530392
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530393 dai_map = &hdmi->dai_map[dai->id];
394
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530395 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530396
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530397 if (pcm)
398 pcm->stream_tag = (tx_mask << 4);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530399
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530400 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530401}
402
403static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
404 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
405{
406 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530407 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530408 struct hdac_hdmi_dai_port_map *dai_map;
409 struct hdac_hdmi_port *port;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530410 struct hdac_hdmi_pcm *pcm;
411 int format;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530412
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530413 dai_map = &hdmi->dai_map[dai->id];
Jeeja KP754695f2017-02-06 12:09:14 +0530414 port = dai_map->port;
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530415
Jeeja KP754695f2017-02-06 12:09:14 +0530416 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530417 return -ENODEV;
418
Jeeja KP754695f2017-02-06 12:09:14 +0530419 if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
420 dev_err(&hdac->hdac.dev,
421 "device is not configured for this pin:port%d:%d\n",
422 port->pin->nid, port->id);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530423 return -ENODEV;
424 }
425
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530426 format = snd_hdac_calc_stream_format(params_rate(hparams),
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530427 params_channels(hparams), params_format(hparams),
428 24, 0);
429
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530430 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
431 if (!pcm)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530432 return -EIO;
433
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530434 pcm->format = format;
435 pcm->channels = params_channels(hparams);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530436
437 return 0;
438}
439
Jeeja KP754695f2017-02-06 12:09:14 +0530440static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
441 struct hdac_hdmi_pin *pin,
442 struct hdac_hdmi_port *port)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530443{
444 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
445 dev_warn(&hdac->hdac.dev,
446 "HDMI: pin %d wcaps %#x does not support connection list\n",
447 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
448 return -EINVAL;
449 }
450
Jeeja KP754695f2017-02-06 12:09:14 +0530451 port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
452 port->mux_nids, HDA_MAX_CONNECTIONS);
453 if (port->num_mux_nids == 0)
454 dev_warn(&hdac->hdac.dev,
455 "No connections found for pin:port %d:%d\n",
456 pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530457
Jeeja KP754695f2017-02-06 12:09:14 +0530458 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
459 port->num_mux_nids, pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530460
Jeeja KP754695f2017-02-06 12:09:14 +0530461 return port->num_mux_nids;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530462}
463
464/*
Jeeja KP754695f2017-02-06 12:09:14 +0530465 * Query pcm list and return port to which stream is routed.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530466 *
Jeeja KP754695f2017-02-06 12:09:14 +0530467 * Also query connection list of the pin, to validate the cvt to port map.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530468 *
Jeeja KP754695f2017-02-06 12:09:14 +0530469 * Same stream rendering to multiple ports simultaneously can be done
470 * possibly, but not supported for now in driver. So return the first port
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530471 * connected.
472 */
Jeeja KP754695f2017-02-06 12:09:14 +0530473static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530474 struct hdac_ext_device *edev,
475 struct hdac_hdmi_priv *hdmi,
476 struct hdac_hdmi_cvt *cvt)
477{
478 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +0530479 struct hdac_hdmi_port *port = NULL;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530480 int ret, i;
481
482 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
483 if (pcm->cvt == cvt) {
Jeeja KP754695f2017-02-06 12:09:14 +0530484 port = pcm->port;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530485 break;
486 }
487 }
488
Jeeja KP754695f2017-02-06 12:09:14 +0530489 if (port) {
490 ret = hdac_hdmi_query_port_connlist(edev, port->pin, port);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530491 if (ret < 0)
492 return NULL;
493
Jeeja KP754695f2017-02-06 12:09:14 +0530494 for (i = 0; i < port->num_mux_nids; i++) {
495 if (port->mux_nids[i] == cvt->nid)
496 return port;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530497 }
498 }
499
500 return NULL;
501}
502
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530503/*
504 * This tries to get a valid pin and set the HW constraints based on the
505 * ELD. Even if a valid pin is not found return success so that device open
506 * doesn't fail.
507 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530508static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
509 struct snd_soc_dai *dai)
510{
511 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
512 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530513 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530514 struct hdac_hdmi_cvt *cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530515 struct hdac_hdmi_port *port;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530516 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530517
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530518 dai_map = &hdmi->dai_map[dai->id];
519
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530520 cvt = dai_map->cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530521 port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530522
523 /*
524 * To make PA and other userland happy.
525 * userland scans devices so returning error does not help.
526 */
Jeeja KP754695f2017-02-06 12:09:14 +0530527 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530528 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530529
Jeeja KP754695f2017-02-06 12:09:14 +0530530 if ((!port->eld.monitor_present) ||
531 (!port->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530532
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530533 dev_warn(&hdac->hdac.dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530534 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
535 port->eld.monitor_present, port->eld.eld_valid,
536 port->pin->nid, port->id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530537
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530538 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530539 }
540
Jeeja KP754695f2017-02-06 12:09:14 +0530541 dai_map->port = port;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530542
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530543 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530544 port->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530545 if (ret < 0)
546 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530547
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530548 return snd_pcm_hw_constraint_eld(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530549 port->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530550}
551
552static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
553 struct snd_soc_dai *dai)
554{
555 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
556 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530557 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPab1eea12017-01-24 21:49:05 +0530558 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530559
560 dai_map = &hdmi->dai_map[dai->id];
561
Jeeja KPab1eea12017-01-24 21:49:05 +0530562 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530563
Jeeja KPab1eea12017-01-24 21:49:05 +0530564 if (pcm) {
565 mutex_lock(&pcm->lock);
566 pcm->chmap_set = false;
567 memset(pcm->chmap, 0, sizeof(pcm->chmap));
568 pcm->channels = 0;
569 mutex_unlock(&pcm->lock);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530570 }
Jeeja KPab1eea12017-01-24 21:49:05 +0530571
Jeeja KP754695f2017-02-06 12:09:14 +0530572 if (dai_map->port)
573 dai_map->port = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530574}
575
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530576static int
577hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
578{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530579 unsigned int chans;
580 struct hdac_ext_device *edev = to_ehdac_device(hdac);
581 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530582 int err;
583
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530584 chans = get_wcaps(hdac, cvt->nid);
585 chans = get_wcaps_channels(chans);
586
587 cvt->params.channels_min = 2;
588
589 cvt->params.channels_max = chans;
590 if (chans > hdmi->chmap.channels_max)
591 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530592
593 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
594 &cvt->params.rates,
595 &cvt->params.formats,
596 &cvt->params.maxbps);
597 if (err < 0)
598 dev_err(&hdac->dev,
599 "Failed to query pcm params for nid %d: %d\n",
600 cvt->nid, err);
601
602 return err;
603}
604
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530605static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530606 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
607 void *priv, const char *wname, const char *stream,
608 struct snd_kcontrol_new *wc, int numkc,
609 int (*event)(struct snd_soc_dapm_widget *,
610 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530611{
612 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530613 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
614 if (!w->name)
615 return -ENOMEM;
616
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530617 w->sname = stream;
618 w->reg = SND_SOC_NOPM;
619 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530620 w->kcontrol_news = wc;
621 w->num_kcontrols = numkc;
622 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530623 w->event = event;
624 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530625
626 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530627}
628
629static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530630 const char *sink, const char *control, const char *src,
631 int (*handler)(struct snd_soc_dapm_widget *src,
632 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530633{
634 route->sink = sink;
635 route->source = src;
636 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530637 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530638}
639
Jeeja KP4a3478d2016-02-12 07:46:06 +0530640static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
Jeeja KP754695f2017-02-06 12:09:14 +0530641 struct hdac_hdmi_port *port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530642{
643 struct hdac_hdmi_priv *hdmi = edev->private_data;
644 struct hdac_hdmi_pcm *pcm = NULL;
645
646 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530647 if (!pcm->port)
648 continue;
649
650 if (pcm->port == port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530651 return pcm;
652 }
653
654 return NULL;
655}
656
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530657static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
658 hda_nid_t nid, unsigned int pwr_state)
659{
660 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
661 if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
662 snd_hdac_codec_write(&edev->hdac, nid, 0,
663 AC_VERB_SET_POWER_STATE, pwr_state);
664 }
665}
666
667static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
668 hda_nid_t nid, int val)
669{
670 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
671 snd_hdac_codec_write(&edev->hdac, nid, 0,
672 AC_VERB_SET_AMP_GAIN_MUTE, val);
673}
674
675
676static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
677 struct snd_kcontrol *kc, int event)
678{
Jeeja KP754695f2017-02-06 12:09:14 +0530679 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530680 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
681 struct hdac_hdmi_pcm *pcm;
682
683 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
684 __func__, w->name, event);
685
Jeeja KP754695f2017-02-06 12:09:14 +0530686 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530687 if (!pcm)
688 return -EIO;
689
690 switch (event) {
691 case SND_SOC_DAPM_PRE_PMU:
Jeeja KP754695f2017-02-06 12:09:14 +0530692 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530693
694 /* Enable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530695 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530696 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
697
Jeeja KP754695f2017-02-06 12:09:14 +0530698 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530699
Jeeja KP754695f2017-02-06 12:09:14 +0530700 return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530701
702 case SND_SOC_DAPM_POST_PMD:
Jeeja KP754695f2017-02-06 12:09:14 +0530703 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530704
705 /* Disable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530706 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530707 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
708
Jeeja KP754695f2017-02-06 12:09:14 +0530709 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530710 break;
711
712 }
713
714 return 0;
715}
716
717static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
718 struct snd_kcontrol *kc, int event)
719{
720 struct hdac_hdmi_cvt *cvt = w->priv;
721 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
722 struct hdac_hdmi_priv *hdmi = edev->private_data;
723 struct hdac_hdmi_pcm *pcm;
724
725 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
726 __func__, w->name, event);
727
728 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
729 if (!pcm)
730 return -EIO;
731
732 switch (event) {
733 case SND_SOC_DAPM_PRE_PMU:
734 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
735
736 /* Enable transmission */
737 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
738 AC_VERB_SET_DIGI_CONVERT_1, 1);
739
740 /* Category Code (CC) to zero */
741 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
742 AC_VERB_SET_DIGI_CONVERT_2, 0);
743
744 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
745 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
746 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
747 AC_VERB_SET_STREAM_FORMAT, pcm->format);
748 break;
749
750 case SND_SOC_DAPM_POST_PMD:
751 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
752 AC_VERB_SET_CHANNEL_STREAMID, 0);
753 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
754 AC_VERB_SET_STREAM_FORMAT, 0);
755
756 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
757 break;
758
759 }
760
761 return 0;
762}
763
764static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
765 struct snd_kcontrol *kc, int event)
766{
Jeeja KP754695f2017-02-06 12:09:14 +0530767 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530768 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
769 int mux_idx;
770
771 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
772 __func__, w->name, event);
773
774 if (!kc)
775 kc = w->kcontrols[0];
776
777 mux_idx = dapm_kcontrol_get_value(kc);
778 if (mux_idx > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +0530779 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530780 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
781 }
782
783 return 0;
784}
785
Jeeja KP4a3478d2016-02-12 07:46:06 +0530786/*
787 * Based on user selection, map the PINs with the PCMs.
788 */
Jeeja KP754695f2017-02-06 12:09:14 +0530789static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530790 struct snd_ctl_elem_value *ucontrol)
791{
792 int ret;
793 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
794 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
795 struct snd_soc_dapm_context *dapm = w->dapm;
Jeeja KP754695f2017-02-06 12:09:14 +0530796 struct hdac_hdmi_port *port = w->priv;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530797 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
798 struct hdac_hdmi_priv *hdmi = edev->private_data;
799 struct hdac_hdmi_pcm *pcm = NULL;
800 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
801
802 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
803 if (ret < 0)
804 return ret;
805
Jeeja KP754695f2017-02-06 12:09:14 +0530806 if (port == NULL)
807 return -EINVAL;
808
Jeeja KP4a3478d2016-02-12 07:46:06 +0530809 mutex_lock(&hdmi->pin_mutex);
810 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530811 if (!pcm->port && pcm->port == port &&
812 pcm->port->id == port->id)
813 pcm->port = NULL;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530814
815 /*
816 * Jack status is not reported during device probe as the
817 * PCMs are not registered by then. So report it here.
818 */
Jeeja KP754695f2017-02-06 12:09:14 +0530819 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->port) {
820 pcm->port = port;
821 if (port->eld.monitor_present && port->eld.eld_valid) {
Jeeja KP4a3478d2016-02-12 07:46:06 +0530822 dev_dbg(&edev->hdac.dev,
823 "jack report for pcm=%d\n",
824 pcm->pcm_id);
825
826 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
827 }
828 mutex_unlock(&hdmi->pin_mutex);
829 return ret;
830 }
831 }
832 mutex_unlock(&hdmi->pin_mutex);
833
834 return ret;
835}
836
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530837/*
838 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
839 * the display driver seem to be programming the connection list for the pin
840 * widget runtime.
841 *
842 * So programming all the possible inputs for the mux, the user has to take
843 * care of selecting the right one and leaving all other inputs selected to
844 * "NONE"
845 */
Jeeja KP754695f2017-02-06 12:09:14 +0530846static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
847 struct hdac_hdmi_port *port,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530848 struct snd_soc_dapm_widget *widget,
849 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530850{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530851 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530852 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530853 struct snd_kcontrol_new *kc;
854 struct hdac_hdmi_cvt *cvt;
855 struct soc_enum *se;
856 char kc_name[NAME_SIZE];
857 char mux_items[NAME_SIZE];
858 /* To hold inputs to the Pin mux */
859 char *items[HDA_MAX_CONNECTIONS];
860 int i = 0;
861 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530862
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530863 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
864 if (!kc)
865 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530866
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530867 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
868 if (!se)
869 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530870
Jeeja KP754695f2017-02-06 12:09:14 +0530871 sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530872 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
873 if (!kc->name)
874 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530875
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530876 kc->private_value = (long)se;
877 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
878 kc->access = 0;
879 kc->info = snd_soc_info_enum_double;
Jeeja KP754695f2017-02-06 12:09:14 +0530880 kc->put = hdac_hdmi_set_pin_port_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530881 kc->get = snd_soc_dapm_get_enum_double;
882
883 se->reg = SND_SOC_NOPM;
884
885 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
886 se->items = num_items;
887 se->mask = roundup_pow_of_two(se->items) - 1;
888
889 sprintf(mux_items, "NONE");
890 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
891 if (!items[i])
892 return -ENOMEM;
893
894 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
895 i++;
896 sprintf(mux_items, "cvt %d", cvt->nid);
897 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
898 if (!items[i])
899 return -ENOMEM;
900 }
901
902 se->texts = devm_kmemdup(&edev->hdac.dev, items,
903 (num_items * sizeof(char *)), GFP_KERNEL);
904 if (!se->texts)
905 return -ENOMEM;
906
907 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP754695f2017-02-06 12:09:14 +0530908 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530909 hdac_hdmi_pin_mux_widget_event,
910 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530911}
912
913/* Add cvt <- input <- mux route map */
914static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
915 struct snd_soc_dapm_widget *widgets,
916 struct snd_soc_dapm_route *route, int rindex)
917{
918 struct hdac_hdmi_priv *hdmi = edev->private_data;
919 const struct snd_kcontrol_new *kc;
920 struct soc_enum *se;
Jeeja KP754695f2017-02-06 12:09:14 +0530921 int mux_index = hdmi->num_cvt + hdmi->num_ports;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530922 int i, j;
923
Jeeja KP754695f2017-02-06 12:09:14 +0530924 for (i = 0; i < hdmi->num_ports; i++) {
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530925 kc = widgets[mux_index].kcontrol_news;
926 se = (struct soc_enum *)kc->private_value;
927 for (j = 0; j < hdmi->num_cvt; j++) {
928 hdac_hdmi_fill_route(&route[rindex],
929 widgets[mux_index].name,
930 se->texts[j + 1],
931 widgets[j].name, NULL);
932
933 rindex++;
934 }
935
936 mux_index++;
937 }
938}
939
940/*
941 * Widgets are added in the below sequence
942 * Converter widgets for num converters enumerated
Jeeja KP754695f2017-02-06 12:09:14 +0530943 * Pin-port widgets for num ports for Pins enumerated
944 * Pin-port mux widgets to represent connenction list of pin widget
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530945 *
Jeeja KP754695f2017-02-06 12:09:14 +0530946 * For each port, one Mux and One output widget is added
947 * Total widgets elements = num_cvt + (num_ports * 2);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530948 *
949 * Routes are added as below:
Jeeja KP754695f2017-02-06 12:09:14 +0530950 * pin-port mux -> pin (based on num_ports)
951 * cvt -> "Input sel control" -> pin-port_mux
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530952 *
953 * Total route elements:
Jeeja KP754695f2017-02-06 12:09:14 +0530954 * num_ports + (pin_muxes * num_cvt)
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530955 */
956static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
957{
958 struct snd_soc_dapm_widget *widgets;
959 struct snd_soc_dapm_route *route;
960 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
961 struct hdac_hdmi_priv *hdmi = edev->private_data;
962 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
963 char widget_name[NAME_SIZE];
964 struct hdac_hdmi_cvt *cvt;
965 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +0530966 int ret, i = 0, num_routes = 0, j;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530967
968 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
969 return -EINVAL;
970
Jeeja KP754695f2017-02-06 12:09:14 +0530971 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
972 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
973 GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530974
975 if (!widgets)
976 return -ENOMEM;
977
978 /* DAPM widgets to represent each converter widget */
979 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
980 sprintf(widget_name, "Converter %d", cvt->nid);
981 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530982 snd_soc_dapm_aif_in, cvt,
983 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
984 hdac_hdmi_cvt_output_widget_event,
985 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530986 if (ret < 0)
987 return ret;
988 i++;
989 }
990
991 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530992 for (j = 0; j < pin->num_ports; j++) {
993 sprintf(widget_name, "hif%d-%d Output",
994 pin->nid, pin->ports[j].id);
995 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
996 snd_soc_dapm_output, &pin->ports[j],
997 widget_name, NULL, NULL, 0,
998 hdac_hdmi_pin_output_widget_event,
999 SND_SOC_DAPM_PRE_PMU |
1000 SND_SOC_DAPM_POST_PMD);
1001 if (ret < 0)
1002 return ret;
1003 i++;
1004 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301005 }
1006
1007 /* DAPM widgets to represent the connection list to pin widget */
1008 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301009 for (j = 0; j < pin->num_ports; j++) {
1010 sprintf(widget_name, "Pin%d-Port%d Mux",
1011 pin->nid, pin->ports[j].id);
1012 ret = hdac_hdmi_create_pin_port_muxs(edev,
1013 &pin->ports[j], &widgets[i],
1014 widget_name);
1015 if (ret < 0)
1016 return ret;
1017 i++;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301018
Jeeja KP754695f2017-02-06 12:09:14 +05301019 /* For cvt to pin_mux mapping */
1020 num_routes += hdmi->num_cvt;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301021
Jeeja KP754695f2017-02-06 12:09:14 +05301022 /* For pin_mux to pin mapping */
1023 num_routes++;
1024 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301025 }
1026
1027 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1028 GFP_KERNEL);
1029 if (!route)
1030 return -ENOMEM;
1031
1032 i = 0;
1033 /* Add pin <- NULL <- mux route map */
1034 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301035 for (j = 0; j < pin->num_ports; j++) {
1036 int sink_index = i + hdmi->num_cvt;
1037 int src_index = sink_index + pin->num_ports *
1038 hdmi->num_pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301039
Jeeja KP754695f2017-02-06 12:09:14 +05301040 hdac_hdmi_fill_route(&route[i],
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301041 widgets[sink_index].name, NULL,
1042 widgets[src_index].name, NULL);
Jeeja KP754695f2017-02-06 12:09:14 +05301043 i++;
1044 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301045 }
1046
1047 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
1048
1049 snd_soc_dapm_new_controls(dapm, widgets,
Jeeja KP754695f2017-02-06 12:09:14 +05301050 ((2 * hdmi->num_ports) + hdmi->num_cvt));
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301051
1052 snd_soc_dapm_add_routes(dapm, route, num_routes);
1053 snd_soc_dapm_new_widgets(dapm->card);
1054
1055 return 0;
1056
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301057}
1058
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301059static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301060{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301061 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +05301062 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301063 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301064 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301065
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301066 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301067 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301068
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301069 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1070 dai_map = &hdmi->dai_map[dai_id];
1071 dai_map->dai_id = dai_id;
1072 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301073
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301074 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301075
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301076 if (dai_id == HDA_MAX_CVTS) {
1077 dev_warn(&edev->hdac.dev,
1078 "Max dais supported: %d\n", dai_id);
1079 break;
1080 }
1081 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301082
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301083 return 0;
1084}
1085
1086static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1087{
1088 struct hdac_hdmi_priv *hdmi = edev->private_data;
1089 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301090 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301091
1092 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1093 if (!cvt)
1094 return -ENOMEM;
1095
1096 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301097 sprintf(name, "cvt %d", cvt->nid);
1098 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301099
1100 list_add_tail(&cvt->head, &hdmi->cvt_list);
1101 hdmi->num_cvt++;
1102
1103 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1104}
1105
Jeeja KP754695f2017-02-06 12:09:14 +05301106static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1107 struct hdac_hdmi_port *port)
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301108{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301109 unsigned int ver, mnl;
1110
Jeeja KP754695f2017-02-06 12:09:14 +05301111 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301112 >> DRM_ELD_VER_SHIFT;
1113
1114 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1115 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1116 return -EINVAL;
1117 }
1118
Jeeja KP754695f2017-02-06 12:09:14 +05301119 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301120 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1121
1122 if (mnl > ELD_MAX_MNL) {
1123 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1124 return -EINVAL;
1125 }
1126
Jeeja KP754695f2017-02-06 12:09:14 +05301127 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301128
1129 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301130}
1131
Jeeja KP754695f2017-02-06 12:09:14 +05301132static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1133 struct hdac_hdmi_port *port)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301134{
1135 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301136 struct hdac_hdmi_priv *hdmi = edev->private_data;
1137 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +05301138 int size = 0;
Jeeja KP2acd8302017-02-06 12:09:18 +05301139 int port_id = -1;
Jeeja KP754695f2017-02-06 12:09:14 +05301140
1141 if (!hdmi)
1142 return;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301143
Jeeja KP2acd8302017-02-06 12:09:18 +05301144 /*
1145 * In case of non MST pin, get_eld info API expectes port
1146 * to be -1.
1147 */
Jeeja KP4a3478d2016-02-12 07:46:06 +05301148 mutex_lock(&hdmi->pin_mutex);
Jeeja KP754695f2017-02-06 12:09:14 +05301149 port->eld.monitor_present = false;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301150
Jeeja KP2acd8302017-02-06 12:09:18 +05301151 if (pin->mst_capable)
1152 port_id = port->id;
1153
1154 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
Jeeja KP754695f2017-02-06 12:09:14 +05301155 &port->eld.monitor_present,
1156 port->eld.eld_buffer,
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301157 ELD_MAX_SIZE);
1158
1159 if (size > 0) {
1160 size = min(size, ELD_MAX_SIZE);
Jeeja KP754695f2017-02-06 12:09:14 +05301161 if (hdac_hdmi_parse_eld(edev, port) < 0)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301162 size = -EINVAL;
1163 }
1164
1165 if (size > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +05301166 port->eld.eld_valid = true;
1167 port->eld.eld_size = size;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301168 } else {
Jeeja KP754695f2017-02-06 12:09:14 +05301169 port->eld.eld_valid = false;
1170 port->eld.eld_size = 0;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301171 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301172
Jeeja KP754695f2017-02-06 12:09:14 +05301173 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301174
Jeeja KP754695f2017-02-06 12:09:14 +05301175 if (!port->eld.monitor_present || !port->eld.eld_valid) {
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301176
Jeeja KP754695f2017-02-06 12:09:14 +05301177 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1178 __func__, pin->nid, port->id);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301179
1180 /*
1181 * PCMs are not registered during device probe, so don't
1182 * report jack here. It will be done in usermode mux
1183 * control select.
1184 */
1185 if (pcm) {
1186 dev_dbg(&edev->hdac.dev,
1187 "jack report for pcm=%d\n", pcm->pcm_id);
1188
1189 snd_jack_report(pcm->jack, 0);
1190 }
1191
1192 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301193 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301194 }
1195
Jeeja KP754695f2017-02-06 12:09:14 +05301196 if (port->eld.monitor_present && port->eld.eld_valid) {
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301197 if (pcm) {
1198 dev_dbg(&edev->hdac.dev,
1199 "jack report for pcm=%d\n",
1200 pcm->pcm_id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301201
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301202 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301203 }
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301204
1205 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
Jeeja KP754695f2017-02-06 12:09:14 +05301206 port->eld.eld_buffer, port->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301207
Jeeja KP754695f2017-02-06 12:09:14 +05301208 }
Jeeja KP4a3478d2016-02-12 07:46:06 +05301209 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301210}
1211
Jeeja KP754695f2017-02-06 12:09:14 +05301212static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1213 struct hdac_hdmi_pin *pin)
1214{
1215 struct hdac_hdmi_port *ports;
1216 int max_ports = HDA_MAX_PORTS;
1217 int i;
1218
1219 /*
1220 * FIXME: max_port may vary for each platform, so pass this as
1221 * as driver data or query from i915 interface when this API is
1222 * implemented.
1223 */
1224
1225 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1226 if (!ports)
1227 return -ENOMEM;
1228
1229 for (i = 0; i < max_ports; i++) {
1230 ports[i].id = i;
1231 ports[i].pin = pin;
1232 }
1233 pin->ports = ports;
1234 pin->num_ports = max_ports;
1235 return 0;
1236}
1237
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301238static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1239{
1240 struct hdac_hdmi_priv *hdmi = edev->private_data;
1241 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301242 int ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301243
1244 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1245 if (!pin)
1246 return -ENOMEM;
1247
1248 pin->nid = nid;
Jeeja KP2acd8302017-02-06 12:09:18 +05301249 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301250 pin->edev = edev;
1251 ret = hdac_hdmi_add_ports(hdmi, pin);
1252 if (ret < 0)
1253 return ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301254
1255 list_add_tail(&pin->head, &hdmi->pin_list);
1256 hdmi->num_pin++;
Jeeja KP754695f2017-02-06 12:09:14 +05301257 hdmi->num_ports += pin->num_ports;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301258
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301259 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301260}
1261
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301262#define INTEL_VENDOR_NID 0x08
1263#define INTEL_GET_VENDOR_VERB 0xf81
1264#define INTEL_SET_VENDOR_VERB 0x781
1265#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1266#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1267
1268static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1269{
1270 unsigned int vendor_param;
1271
1272 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1273 INTEL_GET_VENDOR_VERB, 0);
1274 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1275 return;
1276
1277 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1278 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1279 INTEL_SET_VENDOR_VERB, vendor_param);
1280 if (vendor_param == -1)
1281 return;
1282}
1283
1284static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1285{
1286 unsigned int vendor_param;
1287
1288 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1289 INTEL_GET_VENDOR_VERB, 0);
1290 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1291 return;
1292
1293 /* enable DP1.2 mode */
1294 vendor_param |= INTEL_EN_DP12;
1295 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1296 INTEL_SET_VENDOR_VERB, vendor_param);
1297 if (vendor_param == -1)
1298 return;
1299
1300}
1301
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301302static struct snd_soc_dai_ops hdmi_dai_ops = {
1303 .startup = hdac_hdmi_pcm_open,
1304 .shutdown = hdac_hdmi_pcm_close,
1305 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301306 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301307};
1308
1309/*
1310 * Each converter can support a stream independently. So a dai is created
1311 * based on the number of converter queried.
1312 */
1313static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1314 struct snd_soc_dai_driver **dais,
1315 struct hdac_hdmi_priv *hdmi, int num_dais)
1316{
1317 struct snd_soc_dai_driver *hdmi_dais;
1318 struct hdac_hdmi_cvt *cvt;
1319 char name[NAME_SIZE], dai_name[NAME_SIZE];
1320 int i = 0;
1321 u32 rates, bps;
1322 unsigned int rate_max = 384000, rate_min = 8000;
1323 u64 formats;
1324 int ret;
1325
1326 hdmi_dais = devm_kzalloc(&hdac->dev,
1327 (sizeof(*hdmi_dais) * num_dais),
1328 GFP_KERNEL);
1329 if (!hdmi_dais)
1330 return -ENOMEM;
1331
1332 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1333 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1334 &rates, &formats, &bps);
1335 if (ret)
1336 return ret;
1337
1338 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1339 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1340 dai_name, GFP_KERNEL);
1341
1342 if (!hdmi_dais[i].name)
1343 return -ENOMEM;
1344
1345 snprintf(name, sizeof(name), "hifi%d", i+1);
1346 hdmi_dais[i].playback.stream_name =
1347 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1348 if (!hdmi_dais[i].playback.stream_name)
1349 return -ENOMEM;
1350
1351 /*
1352 * Set caps based on capability queried from the converter.
1353 * It will be constrained runtime based on ELD queried.
1354 */
1355 hdmi_dais[i].playback.formats = formats;
1356 hdmi_dais[i].playback.rates = rates;
1357 hdmi_dais[i].playback.rate_max = rate_max;
1358 hdmi_dais[i].playback.rate_min = rate_min;
1359 hdmi_dais[i].playback.channels_min = 2;
1360 hdmi_dais[i].playback.channels_max = 2;
1361 hdmi_dais[i].ops = &hdmi_dai_ops;
1362
1363 i++;
1364 }
1365
1366 *dais = hdmi_dais;
1367
1368 return 0;
1369}
1370
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301371/*
1372 * Parse all nodes and store the cvt/pin nids in array
1373 * Add one time initialization for pin and cvt widgets
1374 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301375static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1376 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301377{
1378 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301379 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301380 struct hdac_device *hdac = &edev->hdac;
1381 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301382 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301383
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301384 hdac_hdmi_skl_enable_all_pins(hdac);
1385 hdac_hdmi_skl_enable_dp12(hdac);
1386
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301387 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301388 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301389 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1390 return -EINVAL;
1391 }
1392
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301393 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301394 hdac->start_nid = nid;
1395
1396 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1397 unsigned int caps;
1398 unsigned int type;
1399
1400 caps = get_wcaps(hdac, nid);
1401 type = get_wcaps_type(caps);
1402
1403 if (!(caps & AC_WCAP_DIGITAL))
1404 continue;
1405
1406 switch (type) {
1407
1408 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301409 ret = hdac_hdmi_add_cvt(edev, nid);
1410 if (ret < 0)
1411 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301412 break;
1413
1414 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301415 ret = hdac_hdmi_add_pin(edev, nid);
1416 if (ret < 0)
1417 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301418 break;
1419 }
1420 }
1421
1422 hdac->end_nid = nid;
1423
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301424 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301425 return -EIO;
1426
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301427 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1428 if (ret) {
1429 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1430 ret);
1431 return ret;
1432 }
1433
1434 *num_dais = hdmi->num_cvt;
1435
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301436 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301437}
1438
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001439static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301440{
1441 struct hdac_ext_device *edev = aptr;
1442 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +05301443 struct hdac_hdmi_pin *pin = NULL;
1444 struct hdac_hdmi_port *hport = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301445 struct snd_soc_codec *codec = edev->scodec;
Jeeja KP2acd8302017-02-06 12:09:18 +05301446 int i;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301447
1448 /* Don't know how this mapping is derived */
1449 hda_nid_t pin_nid = port + 0x04;
1450
Jeeja KP754695f2017-02-06 12:09:14 +05301451 dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1452 pin_nid, pipe);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301453
1454 /*
1455 * skip notification during system suspend (but not in runtime PM);
1456 * the state will be updated at resume. Also since the ELD and
1457 * connection states are updated in anyway at the end of the resume,
1458 * we can skip it when received during PM process.
1459 */
1460 if (snd_power_get_state(codec->component.card->snd_card) !=
1461 SNDRV_CTL_POWER_D0)
1462 return;
1463
1464 if (atomic_read(&edev->hdac.in_pm))
1465 return;
1466
1467 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301468 if (pin->nid != pin_nid)
1469 continue;
1470
1471 /* In case of non MST pin, pipe is -1 */
1472 if (pipe == -1) {
Jeeja KP2acd8302017-02-06 12:09:18 +05301473 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301474 /* if not MST, default is port[0] */
1475 hport = &pin->ports[0];
Jeeja KP2acd8302017-02-06 12:09:18 +05301476 goto out;
1477 } else {
1478 for (i = 0; i < pin->num_ports; i++) {
1479 pin->mst_capable = true;
1480 if (pin->ports[i].id == pipe) {
1481 hport = &pin->ports[i];
1482 goto out;
1483 }
1484 }
Jeeja KP754695f2017-02-06 12:09:14 +05301485 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301486 }
Jeeja KP754695f2017-02-06 12:09:14 +05301487
Jeeja KP2acd8302017-02-06 12:09:18 +05301488out:
1489 if (pin && hport)
Jeeja KP754695f2017-02-06 12:09:14 +05301490 hdac_hdmi_present_sense(pin, hport);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301491}
1492
1493static struct i915_audio_component_audio_ops aops = {
1494 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1495};
1496
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301497static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1498 int device)
1499{
1500 struct snd_soc_pcm_runtime *rtd;
1501
1502 list_for_each_entry(rtd, &card->rtd_list, list) {
1503 if (rtd->pcm && (rtd->pcm->device == device))
1504 return rtd->pcm;
1505 }
1506
1507 return NULL;
1508}
1509
Jeeja KP4a3478d2016-02-12 07:46:06 +05301510int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1511{
1512 char jack_name[NAME_SIZE];
1513 struct snd_soc_codec *codec = dai->codec;
1514 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1515 struct snd_soc_dapm_context *dapm =
1516 snd_soc_component_get_dapm(&codec->component);
1517 struct hdac_hdmi_priv *hdmi = edev->private_data;
1518 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301519 struct snd_pcm *snd_pcm;
1520 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301521
1522 /*
1523 * this is a new PCM device, create new pcm and
1524 * add to the pcm list
1525 */
1526 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1527 if (!pcm)
1528 return -ENOMEM;
1529 pcm->pcm_id = device;
1530 pcm->cvt = hdmi->dai_map[dai->id].cvt;
Jeeja KPab1eea12017-01-24 21:49:05 +05301531 mutex_init(&pcm->lock);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301532
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301533 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1534 if (snd_pcm) {
1535 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1536 if (err < 0) {
1537 dev_err(&edev->hdac.dev,
1538 "chmap control add failed with err: %d for pcm: %d\n",
1539 err, device);
1540 kfree(pcm);
1541 return err;
1542 }
1543 }
1544
Jeeja KP4a3478d2016-02-12 07:46:06 +05301545 list_add_tail(&pcm->head, &hdmi->pcm_list);
1546
1547 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1548
1549 return snd_jack_new(dapm->card->snd_card, jack_name,
1550 SND_JACK_AVOUT, &pcm->jack, true, false);
1551}
1552EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1553
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301554static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1555 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1556{
1557 int i;
1558 struct hdac_hdmi_pin *pin;
1559
1560 list_for_each_entry(pin, &hdmi->pin_list, head) {
1561 if (detect_pin_caps) {
1562
1563 if (hdac_hdmi_get_port_len(edev, pin->nid) == 0)
1564 pin->mst_capable = false;
1565 else
1566 pin->mst_capable = true;
1567 }
1568
1569 for (i = 0; i < pin->num_ports; i++) {
1570 if (!pin->mst_capable && i > 0)
1571 continue;
1572
1573 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1574 }
1575 }
1576}
1577
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301578static int hdmi_codec_probe(struct snd_soc_codec *codec)
1579{
1580 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1581 struct hdac_hdmi_priv *hdmi = edev->private_data;
1582 struct snd_soc_dapm_context *dapm =
1583 snd_soc_component_get_dapm(&codec->component);
Vinod Koulb2047e92016-05-12 08:58:55 +05301584 struct hdac_ext_link *hlink = NULL;
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301585 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301586
1587 edev->scodec = codec;
1588
Vinod Koulb2047e92016-05-12 08:58:55 +05301589 /*
1590 * hold the ref while we probe, also no need to drop the ref on
1591 * exit, we call pm_runtime_suspend() so that will do for us
1592 */
1593 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301594 if (!hlink) {
1595 dev_err(&edev->hdac.dev, "hdac link not found\n");
1596 return -EIO;
1597 }
1598
Vinod Koulb2047e92016-05-12 08:58:55 +05301599 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1600
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301601 ret = create_fill_widget_route_map(dapm);
1602 if (ret < 0)
1603 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301604
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301605 aops.audio_ptr = edev;
1606 ret = snd_hdac_i915_register_notifier(&aops);
1607 if (ret < 0) {
1608 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1609 ret);
1610 return ret;
1611 }
1612
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301613 hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301614 /* Imp: Store the card pointer in hda_codec */
1615 edev->card = dapm->card->snd_card;
1616
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301617 /*
1618 * hdac_device core already sets the state to active and calls
1619 * get_noresume. So enable runtime and set the device to suspend.
1620 */
1621 pm_runtime_enable(&edev->hdac.dev);
1622 pm_runtime_put(&edev->hdac.dev);
1623 pm_runtime_suspend(&edev->hdac.dev);
1624
1625 return 0;
1626}
1627
1628static int hdmi_codec_remove(struct snd_soc_codec *codec)
1629{
1630 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1631
1632 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301633 return 0;
1634}
1635
Jeeja KP571d5072016-02-22 07:50:33 +05301636#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301637static int hdmi_codec_prepare(struct device *dev)
1638{
1639 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1640 struct hdac_device *hdac = &edev->hdac;
1641
1642 pm_runtime_get_sync(&edev->hdac.dev);
1643
1644 /*
1645 * Power down afg.
1646 * codec_read is preferred over codec_write to set the power state.
1647 * This way verb is send to set the power state and response
1648 * is received. So setting power state is ensured without using loop
1649 * to read the state.
1650 */
1651 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1652 AC_PWRST_D3);
1653
1654 return 0;
1655}
1656
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301657static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301658{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301659 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301660 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP571d5072016-02-22 07:50:33 +05301661 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301662
1663 /* Power up afg */
1664 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1665 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301666
1667 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1668 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1669
Jeeja KP571d5072016-02-22 07:50:33 +05301670 /*
1671 * As the ELD notify callback request is not entertained while the
1672 * device is in suspend state. Need to manually check detection of
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301673 * all pins here. pin capablity change is not support, so use the
1674 * already set pin caps.
Jeeja KP571d5072016-02-22 07:50:33 +05301675 */
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301676 hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
Jeeja KP571d5072016-02-22 07:50:33 +05301677
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301678 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301679}
1680#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301681#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301682#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301683#endif
1684
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301685static struct snd_soc_codec_driver hdmi_hda_codec = {
1686 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301687 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301688 .idle_bias_off = true,
1689};
1690
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301691static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1692 unsigned char *chmap)
1693{
1694 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1695 struct hdac_hdmi_priv *hdmi = edev->private_data;
1696 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301697
Jeeja KPab1eea12017-01-24 21:49:05 +05301698 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301699}
1700
1701static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1702 unsigned char *chmap, int prepared)
1703{
1704 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1705 struct hdac_hdmi_priv *hdmi = edev->private_data;
1706 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301707 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301708
Jeeja KPab1eea12017-01-24 21:49:05 +05301709 mutex_lock(&pcm->lock);
1710 pcm->chmap_set = true;
1711 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301712 if (prepared)
Jeeja KP754695f2017-02-06 12:09:14 +05301713 hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPab1eea12017-01-24 21:49:05 +05301714 mutex_unlock(&pcm->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301715}
1716
1717static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1718{
1719 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1720 struct hdac_hdmi_priv *hdmi = edev->private_data;
1721 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301722 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301723
Jeeja KP754695f2017-02-06 12:09:14 +05301724 return port ? true:false;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301725}
1726
1727static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1728{
1729 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1730 struct hdac_hdmi_priv *hdmi = edev->private_data;
1731 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301732 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301733
Jeeja KP754695f2017-02-06 12:09:14 +05301734 if (!port || !port->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301735 return 0;
1736
Jeeja KP754695f2017-02-06 12:09:14 +05301737 return port->eld.info.spk_alloc;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301738}
1739
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301740static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1741{
1742 struct hdac_device *codec = &edev->hdac;
1743 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301744 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301745 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301746 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301747 int ret = 0;
1748
Vinod Koulb2047e92016-05-12 08:58:55 +05301749 /* hold the ref while we probe */
1750 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301751 if (!hlink) {
1752 dev_err(&edev->hdac.dev, "hdac link not found\n");
1753 return -EIO;
1754 }
1755
Vinod Koulb2047e92016-05-12 08:58:55 +05301756 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1757
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301758 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1759 if (hdmi_priv == NULL)
1760 return -ENOMEM;
1761
1762 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301763 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301764 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1765 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1766 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1767 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301768
1769 dev_set_drvdata(&codec->dev, edev);
1770
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301771 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1772 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301773 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1774 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301775
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301776 /*
1777 * Turned off in the runtime_suspend during the first explicit
1778 * pm_runtime_suspend call.
1779 */
1780 ret = snd_hdac_display_power(edev->hdac.bus, true);
1781 if (ret < 0) {
1782 dev_err(&edev->hdac.dev,
1783 "Cannot turn on display power on i915 err: %d\n",
1784 ret);
1785 return ret;
1786 }
1787
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301788 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1789 if (ret < 0) {
1790 dev_err(&codec->dev,
1791 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301792 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301793 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301794
1795 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301796 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1797 hdmi_dais, num_dais);
1798
1799 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1800
1801 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301802}
1803
1804static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1805{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301806 struct hdac_hdmi_priv *hdmi = edev->private_data;
1807 struct hdac_hdmi_pin *pin, *pin_next;
1808 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301809 struct hdac_hdmi_pcm *pcm, *pcm_next;
Jeeja KP754695f2017-02-06 12:09:14 +05301810 int i;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301811
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301812 snd_soc_unregister_codec(&edev->hdac.dev);
1813
Jeeja KP4a3478d2016-02-12 07:46:06 +05301814 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1815 pcm->cvt = NULL;
Jeeja KP754695f2017-02-06 12:09:14 +05301816 pcm->port = NULL;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301817 list_del(&pcm->head);
1818 kfree(pcm);
1819 }
1820
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301821 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1822 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301823 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301824 kfree(cvt);
1825 }
1826
1827 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301828 for (i = 0; i < pin->num_ports; i++)
1829 pin->ports[i].pin = NULL;
1830 kfree(pin->ports);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301831 list_del(&pin->head);
1832 kfree(pin);
1833 }
1834
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301835 return 0;
1836}
1837
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301838#ifdef CONFIG_PM
1839static int hdac_hdmi_runtime_suspend(struct device *dev)
1840{
1841 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1842 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301843 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301844 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1845 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301846 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301847
1848 dev_dbg(dev, "Enter: %s\n", __func__);
1849
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301850 /* controller may not have been initialized for the first time */
1851 if (!bus)
1852 return 0;
1853
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301854 /*
1855 * Power down afg.
1856 * codec_read is preferred over codec_write to set the power state.
1857 * This way verb is send to set the power state and response
1858 * is received. So setting power state is ensured without using loop
1859 * to read the state.
1860 */
1861 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1862 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301863 err = snd_hdac_display_power(bus, false);
1864 if (err < 0) {
1865 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1866 return err;
1867 }
1868
Vinod Koulb2047e92016-05-12 08:58:55 +05301869 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301870 if (!hlink) {
1871 dev_err(dev, "hdac link not found\n");
1872 return -EIO;
1873 }
1874
Vinod Koulb2047e92016-05-12 08:58:55 +05301875 snd_hdac_ext_bus_link_put(ebus, hlink);
1876
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301877 return 0;
1878}
1879
1880static int hdac_hdmi_runtime_resume(struct device *dev)
1881{
1882 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1883 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301884 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301885 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1886 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301887 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301888
1889 dev_dbg(dev, "Enter: %s\n", __func__);
1890
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301891 /* controller may not have been initialized for the first time */
1892 if (!bus)
1893 return 0;
1894
Vinod Koulb2047e92016-05-12 08:58:55 +05301895 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301896 if (!hlink) {
1897 dev_err(dev, "hdac link not found\n");
1898 return -EIO;
1899 }
1900
Vinod Koulb2047e92016-05-12 08:58:55 +05301901 snd_hdac_ext_bus_link_get(ebus, hlink);
1902
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301903 err = snd_hdac_display_power(bus, true);
1904 if (err < 0) {
1905 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1906 return err;
1907 }
1908
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301909 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1910 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1911
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301912 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301913 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1914 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301915
1916 return 0;
1917}
1918#else
1919#define hdac_hdmi_runtime_suspend NULL
1920#define hdac_hdmi_runtime_resume NULL
1921#endif
1922
1923static const struct dev_pm_ops hdac_hdmi_pm = {
1924 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301925 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301926 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301927};
1928
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301929static const struct hda_device_id hdmi_list[] = {
1930 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05301931 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05301932 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301933 {}
1934};
1935
1936MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1937
1938static struct hdac_ext_driver hdmi_driver = {
1939 . hdac = {
1940 .driver = {
1941 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301942 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301943 },
1944 .id_table = hdmi_list,
1945 },
1946 .probe = hdac_hdmi_dev_probe,
1947 .remove = hdac_hdmi_dev_remove,
1948};
1949
1950static int __init hdmi_init(void)
1951{
1952 return snd_hda_ext_driver_register(&hdmi_driver);
1953}
1954
1955static void __exit hdmi_exit(void)
1956{
1957 snd_hda_ext_driver_unregister(&hdmi_driver);
1958}
1959
1960module_init(hdmi_init);
1961module_exit(hdmi_exit);
1962
1963MODULE_LICENSE("GPL v2");
1964MODULE_DESCRIPTION("HDMI HD codec");
1965MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1966MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");