blob: 1da4405ee435b60c05e96f474b844af105bf4c7b [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
45
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053046#define ELD_MAX_SIZE 256
47#define ELD_FIXED_BYTES 20
48
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053049struct hdac_hdmi_cvt_params {
50 unsigned int channels_min;
51 unsigned int channels_max;
52 u32 rates;
53 u64 formats;
54 unsigned int maxbps;
55};
56
57struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053058 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053059 hda_nid_t nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +053060 const char *name;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053061 struct hdac_hdmi_cvt_params params;
62};
63
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053064/* Currently only spk_alloc, more to be added */
65struct hdac_hdmi_parsed_eld {
66 u8 spk_alloc;
67};
68
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053069struct hdac_hdmi_eld {
70 bool monitor_present;
71 bool eld_valid;
72 int eld_size;
73 char eld_buffer[ELD_MAX_SIZE];
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053074 struct hdac_hdmi_parsed_eld info;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053075};
76
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053077struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053078 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053079 hda_nid_t nid;
80 int num_mux_nids;
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053082 struct hdac_hdmi_eld eld;
83 struct hdac_ext_device *edev;
84 int repoll_count;
85 struct delayed_work work;
Subhransu S. Prustybcced702016-04-14 10:07:30 +053086 struct mutex lock;
87 bool chmap_set;
88 unsigned char chmap[8]; /* ALSA API channel-map */
89 int channels; /* current number of channels */
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053090};
91
Jeeja KP4a3478d2016-02-12 07:46:06 +053092struct hdac_hdmi_pcm {
93 struct list_head head;
94 int pcm_id;
95 struct hdac_hdmi_pin *pin;
96 struct hdac_hdmi_cvt *cvt;
97 struct snd_jack *jack;
98};
99
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530100struct hdac_hdmi_dai_pin_map {
101 int dai_id;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530102 struct hdac_hdmi_pin *pin;
103 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530104};
105
106struct hdac_hdmi_priv {
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530107 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530108 struct list_head pin_list;
109 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530110 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530111 int num_pin;
112 int num_cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530113 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530114 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530115};
116
Jeeja KP1de777f2017-01-10 17:57:48 +0530117static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
118 struct hdac_hdmi_dai_pin_map *dai_map);
119
120static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
121 struct hdac_hdmi_dai_pin_map *dai_map);
122
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530123static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
124 int pcm_idx)
125{
126 struct hdac_hdmi_pcm *pcm;
127
128 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
129 if (pcm->pcm_id == pcm_idx)
130 return pcm;
131 }
132
133 return NULL;
134}
135
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530136static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
137{
Geliang Tang51b2c422015-12-28 22:47:13 +0800138 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530139
Geliang Tang51b2c422015-12-28 22:47:13 +0800140 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530141}
142
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530143static unsigned int sad_format(const u8 *sad)
144{
145 return ((sad[0] >> 0x3) & 0x1f);
146}
147
148static unsigned int sad_sample_bits_lpcm(const u8 *sad)
149{
150 return (sad[2] & 7);
151}
152
153static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
154 void *eld)
155{
156 u64 formats = SNDRV_PCM_FMTBIT_S16;
157 int i;
158 const u8 *sad, *eld_buf = eld;
159
160 sad = drm_eld_sad(eld_buf);
161 if (!sad)
162 goto format_constraint;
163
164 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
165 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
166
167 /*
168 * the controller support 20 and 24 bits in 32 bit
169 * container so we set S32
170 */
171 if (sad_sample_bits_lpcm(sad) & 0x6)
172 formats |= SNDRV_PCM_FMTBIT_S32;
173 }
174 }
175
176format_constraint:
177 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
178 formats);
179
180}
181
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530182 /* HDMI ELD routines */
183static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
184 hda_nid_t nid, int byte_index)
185{
186 unsigned int val;
187
188 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
189 byte_index);
190
191 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
192 byte_index, val);
193
194 return val;
195}
196
197static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
198{
199 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
200 AC_DIPSIZE_ELD_BUF);
201}
202
203/*
204 * This function queries the ELD size and ELD data and fills in the buffer
205 * passed by user
206 */
207static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
208 unsigned char *buf, int *eld_size)
209{
210 int i, size, ret = 0;
211
212 /*
213 * ELD size is initialized to zero in caller function. If no errors and
214 * ELD is valid, actual eld_size is assigned.
215 */
216
217 size = hdac_hdmi_get_eld_size(codec, nid);
218 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
219 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
220 return -ERANGE;
221 }
222
223 /* set ELD buffer */
224 for (i = 0; i < size; i++) {
225 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
226 /*
227 * Graphics driver might be writing to ELD buffer right now.
228 * Just abort. The caller will repoll after a while.
229 */
230 if (!(val & AC_ELDD_ELD_VALID)) {
231 dev_err(&codec->dev,
232 "HDMI: invalid ELD data byte %d\n", i);
233 ret = -EINVAL;
234 goto error;
235 }
236 val &= AC_ELDD_ELD_DATA;
237 /*
238 * The first byte cannot be zero. This can happen on some DVI
239 * connections. Some Intel chips may also need some 250ms delay
240 * to return non-zero ELD data, even when the graphics driver
241 * correctly writes ELD content before setting ELD_valid bit.
242 */
243 if (!val && !i) {
244 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
245 ret = -EINVAL;
246 goto error;
247 }
248 buf[i] = val;
249 }
250
251 *eld_size = size;
252error:
253 return ret;
254}
255
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530256static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
257 hda_nid_t cvt_nid, hda_nid_t pin_nid,
258 u32 stream_tag, int format)
259{
260 unsigned int val;
261
262 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
263 cvt_nid, pin_nid, stream_tag, format);
264
265 val = (stream_tag << 4);
266
267 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
268 AC_VERB_SET_CHANNEL_STREAMID, val);
269 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
270 AC_VERB_SET_STREAM_FORMAT, format);
271
272 return 0;
273}
274
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530275static void
276hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
277 int packet_index, int byte_index)
278{
279 int val;
280
281 val = (packet_index << 5) | (byte_index & 0x1f);
282
283 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
284 AC_VERB_SET_HDMI_DIP_INDEX, val);
285}
286
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530287struct dp_audio_infoframe {
288 u8 type; /* 0x84 */
289 u8 len; /* 0x1b */
290 u8 ver; /* 0x11 << 2 */
291
292 u8 CC02_CT47; /* match with HDMI infoframe from this on */
293 u8 SS01_SF24;
294 u8 CXT04;
295 u8 CA;
296 u8 LFEPBL01_LSV36_DM_INH7;
297};
298
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530299static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
300 hda_nid_t cvt_nid, hda_nid_t pin_nid)
301{
302 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
303 struct hdmi_audio_infoframe frame;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530304 struct dp_audio_infoframe dp_ai;
305 struct hdac_hdmi_priv *hdmi = hdac->private_data;
306 struct hdac_hdmi_pin *pin;
307 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
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530314 list_for_each_entry(pin, &hdmi->pin_list, head) {
315 if (pin->nid == pin_nid)
316 break;
317 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530318
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530319 ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
320 pin->channels, pin->chmap_set, true, pin->chmap);
321
322 channels = snd_hdac_get_active_channels(ca);
323 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
324
325 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
326 pin->channels, pin->chmap, pin->chmap_set);
327
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530328 eld_buf = pin->eld.eld_buffer;
329 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530330
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530331 switch (conn_type) {
332 case DRM_ELD_CONN_TYPE_HDMI:
333 hdmi_audio_infoframe_init(&frame);
334
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530335 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530336 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530337
338 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
339 if (ret < 0)
340 return ret;
341
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530342 break;
343
344 case DRM_ELD_CONN_TYPE_DP:
345 memset(&dp_ai, 0, sizeof(dp_ai));
346 dp_ai.type = 0x84;
347 dp_ai.len = 0x1b;
348 dp_ai.ver = 0x11 << 2;
349 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530350 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530351
352 dip = (u8 *)&dp_ai;
353 break;
354
355 default:
356 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
357 conn_type);
358 return -EIO;
359 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530360
361 /* stop infoframe transmission */
362 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
363 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
364 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
365
366
367 /* Fill infoframe. Index auto-incremented */
368 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530369 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530370 for (i = 0; i < sizeof(buffer); i++)
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530371 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530372 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530373 } else {
374 for (i = 0; i < sizeof(dp_ai); i++)
375 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
376 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
377 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530378
379 /* Start infoframe */
380 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
381 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
382 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
383
384 return 0;
385}
386
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530387static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
388 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
389{
390 /* Power up pin widget */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530391 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
392 pwr_state))
393 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530394 AC_VERB_SET_POWER_STATE, pwr_state);
395
396 /* Power up converter */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530397 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
398 pwr_state))
399 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530400 AC_VERB_SET_POWER_STATE, pwr_state);
401}
402
403static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
404 struct snd_soc_dai *dai)
405{
406 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
407 struct hdac_hdmi_priv *hdmi = hdac->private_data;
408 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530409 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530410 struct hdac_ext_dma_params *dd;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530411 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530412
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530413 dai_map = &hdmi->dai_map[dai->id];
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530414 pin = dai_map->pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530415
416 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
417 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
418 dd->stream_tag, dd->format);
419
Jeeja KP1de777f2017-01-10 17:57:48 +0530420 hdac_hdmi_enable_cvt(hdac, dai_map);
421 ret = hdac_hdmi_enable_pin(hdac, dai_map);
422 if (ret < 0)
423 return ret;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530424 mutex_lock(&pin->lock);
425 pin->channels = substream->runtime->channels;
426
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530427 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
428 dai_map->pin->nid);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530429 mutex_unlock(&pin->lock);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530430 if (ret < 0)
431 return ret;
432
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530433 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
434 dai_map->pin->nid, dd->stream_tag, dd->format);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530435}
436
437static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
438 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
439{
440 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530441 struct hdac_hdmi_priv *hdmi = hdac->private_data;
442 struct hdac_hdmi_dai_pin_map *dai_map;
443 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530444 struct hdac_ext_dma_params *dd;
445
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530446 dai_map = &hdmi->dai_map[dai->id];
447 pin = dai_map->pin;
448
449 if (!pin)
450 return -ENODEV;
451
452 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
453 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
454 pin->nid);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530455 return -ENODEV;
456 }
457
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530458 dd = snd_soc_dai_get_dma_data(dai, substream);
459 if (!dd) {
460 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
461 if (!dd)
462 return -ENOMEM;
463 }
464
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530465 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
466 params_channels(hparams), params_format(hparams),
467 24, 0);
468
469 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
470
471 return 0;
472}
473
474static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
475 struct snd_soc_dai *dai)
476{
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530477 struct hdac_ext_dma_params *dd;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530478
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530479 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530480
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530481 if (dd) {
482 snd_soc_dai_set_dma_data(dai, substream, NULL);
483 kfree(dd);
484 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530485
486 return 0;
487}
488
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +0530489static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
490 struct hdac_hdmi_dai_pin_map *dai_map)
491{
492 /* Enable transmission */
493 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
494 AC_VERB_SET_DIGI_CONVERT_1, 1);
495
496 /* Category Code (CC) to zero */
497 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
498 AC_VERB_SET_DIGI_CONVERT_2, 0);
499}
500
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530501static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
502 struct hdac_hdmi_dai_pin_map *dai_map)
503{
504 int mux_idx;
505 struct hdac_hdmi_pin *pin = dai_map->pin;
506
507 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
508 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
509 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
510 AC_VERB_SET_CONNECT_SEL, mux_idx);
511 break;
512 }
513 }
514
515 if (mux_idx == pin->num_mux_nids)
516 return -EIO;
517
518 /* Enable out path for this pin widget */
519 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
520 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
521
522 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
523
524 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
525 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
526
527 return 0;
528}
529
530static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
531 struct hdac_hdmi_pin *pin)
532{
533 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
534 dev_warn(&hdac->hdac.dev,
535 "HDMI: pin %d wcaps %#x does not support connection list\n",
536 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
537 return -EINVAL;
538 }
539
540 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
541 pin->mux_nids, HDA_MAX_CONNECTIONS);
542 if (pin->num_mux_nids == 0)
543 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
544 pin->nid);
545
546 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
547 pin->num_mux_nids, pin->nid);
548
549 return pin->num_mux_nids;
550}
551
552/*
553 * Query pcm list and return pin widget to which stream is routed.
554 *
555 * Also query connection list of the pin, to validate the cvt to pin map.
556 *
557 * Same stream rendering to multiple pins simultaneously can be done
558 * possibly, but not supported for now in driver. So return the first pin
559 * connected.
560 */
561static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
562 struct hdac_ext_device *edev,
563 struct hdac_hdmi_priv *hdmi,
564 struct hdac_hdmi_cvt *cvt)
565{
566 struct hdac_hdmi_pcm *pcm;
567 struct hdac_hdmi_pin *pin = NULL;
568 int ret, i;
569
570 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
571 if (pcm->cvt == cvt) {
572 pin = pcm->pin;
573 break;
574 }
575 }
576
577 if (pin) {
578 ret = hdac_hdmi_query_pin_connlist(edev, pin);
579 if (ret < 0)
580 return NULL;
581
582 for (i = 0; i < pin->num_mux_nids; i++) {
583 if (pin->mux_nids[i] == cvt->nid)
584 return pin;
585 }
586 }
587
588 return NULL;
589}
590
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530591/*
592 * This tries to get a valid pin and set the HW constraints based on the
593 * ELD. Even if a valid pin is not found return success so that device open
594 * doesn't fail.
595 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530596static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
597 struct snd_soc_dai *dai)
598{
599 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
600 struct hdac_hdmi_priv *hdmi = hdac->private_data;
601 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530602 struct hdac_hdmi_cvt *cvt;
603 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530604 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530605
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530606 dai_map = &hdmi->dai_map[dai->id];
607
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530608 cvt = dai_map->cvt;
609 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530610
611 /*
612 * To make PA and other userland happy.
613 * userland scans devices so returning error does not help.
614 */
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530615 if (!pin)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530616 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530617
618 if ((!pin->eld.monitor_present) ||
619 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530620
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530621 dev_warn(&hdac->hdac.dev,
Colin Ian King3fb7b4e2016-08-26 19:17:25 +0100622 "Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530623 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530624
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530625 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530626 }
627
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530628 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530629
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530630 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530631 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530632 if (ret < 0)
633 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530634
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530635 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530636 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530637}
638
Jeeja KP571d5072016-02-22 07:50:33 +0530639static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
640 struct snd_soc_dai *dai)
641{
Jeeja KP571d5072016-02-22 07:50:33 +0530642
Jeeja KP1de777f2017-01-10 17:57:48 +0530643 switch (cmd) {
644 case SNDRV_PCM_TRIGGER_RESUME:
645 case SNDRV_PCM_TRIGGER_START:
646 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jeeja KP571d5072016-02-22 07:50:33 +0530647 return hdac_hdmi_playback_prepare(substream, dai);
Jeeja KP1de777f2017-01-10 17:57:48 +0530648
649 default:
650 return 0;
Jeeja KP571d5072016-02-22 07:50:33 +0530651 }
652
653 return 0;
654}
655
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530656static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
657 struct snd_soc_dai *dai)
658{
659 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
660 struct hdac_hdmi_priv *hdmi = hdac->private_data;
661 struct hdac_hdmi_dai_pin_map *dai_map;
662
663 dai_map = &hdmi->dai_map[dai->id];
664
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530665 if (dai_map->pin) {
666 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
667 AC_VERB_SET_CHANNEL_STREAMID, 0);
668 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
669 AC_VERB_SET_STREAM_FORMAT, 0);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530670
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530671 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
672
673 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530674 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530675
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530676 mutex_lock(&dai_map->pin->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530677 dai_map->pin->chmap_set = false;
678 memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530679 dai_map->pin->channels = 0;
680 mutex_unlock(&dai_map->pin->lock);
681
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530682 dai_map->pin = NULL;
683 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530684}
685
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530686static int
687hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
688{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530689 unsigned int chans;
690 struct hdac_ext_device *edev = to_ehdac_device(hdac);
691 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530692 int err;
693
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530694 chans = get_wcaps(hdac, cvt->nid);
695 chans = get_wcaps_channels(chans);
696
697 cvt->params.channels_min = 2;
698
699 cvt->params.channels_max = chans;
700 if (chans > hdmi->chmap.channels_max)
701 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530702
703 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
704 &cvt->params.rates,
705 &cvt->params.formats,
706 &cvt->params.maxbps);
707 if (err < 0)
708 dev_err(&hdac->dev,
709 "Failed to query pcm params for nid %d: %d\n",
710 cvt->nid, err);
711
712 return err;
713}
714
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530715static int hdac_hdmi_fill_widget_info(struct device *dev,
716 struct snd_soc_dapm_widget *w,
717 enum snd_soc_dapm_type id, void *priv,
718 const char *wname, const char *stream,
719 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530720{
721 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530722 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
723 if (!w->name)
724 return -ENOMEM;
725
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530726 w->sname = stream;
727 w->reg = SND_SOC_NOPM;
728 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530729 w->kcontrol_news = wc;
730 w->num_kcontrols = numkc;
731 w->priv = priv;
732
733 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530734}
735
736static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530737 const char *sink, const char *control, const char *src,
738 int (*handler)(struct snd_soc_dapm_widget *src,
739 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530740{
741 route->sink = sink;
742 route->source = src;
743 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530744 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530745}
746
Jeeja KP4a3478d2016-02-12 07:46:06 +0530747static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
748 struct hdac_hdmi_pin *pin)
749{
750 struct hdac_hdmi_priv *hdmi = edev->private_data;
751 struct hdac_hdmi_pcm *pcm = NULL;
752
753 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
754 if (pcm->pin == pin)
755 return pcm;
756 }
757
758 return NULL;
759}
760
761/*
762 * Based on user selection, map the PINs with the PCMs.
763 */
764static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
765 struct snd_ctl_elem_value *ucontrol)
766{
767 int ret;
768 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
769 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
770 struct snd_soc_dapm_context *dapm = w->dapm;
771 struct hdac_hdmi_pin *pin = w->priv;
772 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
773 struct hdac_hdmi_priv *hdmi = edev->private_data;
774 struct hdac_hdmi_pcm *pcm = NULL;
775 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
776
777 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
778 if (ret < 0)
779 return ret;
780
781 mutex_lock(&hdmi->pin_mutex);
782 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
783 if (pcm->pin == pin)
784 pcm->pin = NULL;
785
786 /*
787 * Jack status is not reported during device probe as the
788 * PCMs are not registered by then. So report it here.
789 */
790 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
791 pcm->pin = pin;
792 if (pin->eld.monitor_present && pin->eld.eld_valid) {
793 dev_dbg(&edev->hdac.dev,
794 "jack report for pcm=%d\n",
795 pcm->pcm_id);
796
797 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
798 }
799 mutex_unlock(&hdmi->pin_mutex);
800 return ret;
801 }
802 }
803 mutex_unlock(&hdmi->pin_mutex);
804
805 return ret;
806}
807
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530808/*
809 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
810 * the display driver seem to be programming the connection list for the pin
811 * widget runtime.
812 *
813 * So programming all the possible inputs for the mux, the user has to take
814 * care of selecting the right one and leaving all other inputs selected to
815 * "NONE"
816 */
817static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
818 struct hdac_hdmi_pin *pin,
819 struct snd_soc_dapm_widget *widget,
820 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530821{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530822 struct hdac_hdmi_priv *hdmi = edev->private_data;
823 struct snd_kcontrol_new *kc;
824 struct hdac_hdmi_cvt *cvt;
825 struct soc_enum *se;
826 char kc_name[NAME_SIZE];
827 char mux_items[NAME_SIZE];
828 /* To hold inputs to the Pin mux */
829 char *items[HDA_MAX_CONNECTIONS];
830 int i = 0;
831 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530832
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530833 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
834 if (!kc)
835 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530836
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530837 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
838 if (!se)
839 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530840
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530841 sprintf(kc_name, "Pin %d Input", pin->nid);
842 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
843 if (!kc->name)
844 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530845
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530846 kc->private_value = (long)se;
847 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
848 kc->access = 0;
849 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530850 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530851 kc->get = snd_soc_dapm_get_enum_double;
852
853 se->reg = SND_SOC_NOPM;
854
855 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
856 se->items = num_items;
857 se->mask = roundup_pow_of_two(se->items) - 1;
858
859 sprintf(mux_items, "NONE");
860 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
861 if (!items[i])
862 return -ENOMEM;
863
864 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
865 i++;
866 sprintf(mux_items, "cvt %d", cvt->nid);
867 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
868 if (!items[i])
869 return -ENOMEM;
870 }
871
872 se->texts = devm_kmemdup(&edev->hdac.dev, items,
873 (num_items * sizeof(char *)), GFP_KERNEL);
874 if (!se->texts)
875 return -ENOMEM;
876
877 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530878 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530879}
880
881/* Add cvt <- input <- mux route map */
882static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
883 struct snd_soc_dapm_widget *widgets,
884 struct snd_soc_dapm_route *route, int rindex)
885{
886 struct hdac_hdmi_priv *hdmi = edev->private_data;
887 const struct snd_kcontrol_new *kc;
888 struct soc_enum *se;
889 int mux_index = hdmi->num_cvt + hdmi->num_pin;
890 int i, j;
891
892 for (i = 0; i < hdmi->num_pin; i++) {
893 kc = widgets[mux_index].kcontrol_news;
894 se = (struct soc_enum *)kc->private_value;
895 for (j = 0; j < hdmi->num_cvt; j++) {
896 hdac_hdmi_fill_route(&route[rindex],
897 widgets[mux_index].name,
898 se->texts[j + 1],
899 widgets[j].name, NULL);
900
901 rindex++;
902 }
903
904 mux_index++;
905 }
906}
907
908/*
909 * Widgets are added in the below sequence
910 * Converter widgets for num converters enumerated
911 * Pin widgets for num pins enumerated
912 * Pin mux widgets to represent connenction list of pin widget
913 *
914 * Total widgets elements = num_cvt + num_pin + num_pin;
915 *
916 * Routes are added as below:
917 * pin mux -> pin (based on num_pins)
918 * cvt -> "Input sel control" -> pin_mux
919 *
920 * Total route elements:
921 * num_pins + (pin_muxes * num_cvt)
922 */
923static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
924{
925 struct snd_soc_dapm_widget *widgets;
926 struct snd_soc_dapm_route *route;
927 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
928 struct hdac_hdmi_priv *hdmi = edev->private_data;
929 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
930 char widget_name[NAME_SIZE];
931 struct hdac_hdmi_cvt *cvt;
932 struct hdac_hdmi_pin *pin;
933 int ret, i = 0, num_routes = 0;
934
935 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
936 return -EINVAL;
937
938 widgets = devm_kzalloc(dapm->dev,
939 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
940 GFP_KERNEL);
941
942 if (!widgets)
943 return -ENOMEM;
944
945 /* DAPM widgets to represent each converter widget */
946 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
947 sprintf(widget_name, "Converter %d", cvt->nid);
948 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
949 snd_soc_dapm_aif_in, &cvt->nid,
950 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
951 if (ret < 0)
952 return ret;
953 i++;
954 }
955
956 list_for_each_entry(pin, &hdmi->pin_list, head) {
957 sprintf(widget_name, "hif%d Output", pin->nid);
958 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
959 snd_soc_dapm_output, &pin->nid,
960 widget_name, NULL, NULL, 0);
961 if (ret < 0)
962 return ret;
963 i++;
964 }
965
966 /* DAPM widgets to represent the connection list to pin widget */
967 list_for_each_entry(pin, &hdmi->pin_list, head) {
968 sprintf(widget_name, "Pin %d Mux", pin->nid);
969 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
970 widget_name);
971 if (ret < 0)
972 return ret;
973 i++;
974
975 /* For cvt to pin_mux mapping */
976 num_routes += hdmi->num_cvt;
977
978 /* For pin_mux to pin mapping */
979 num_routes++;
980 }
981
982 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
983 GFP_KERNEL);
984 if (!route)
985 return -ENOMEM;
986
987 i = 0;
988 /* Add pin <- NULL <- mux route map */
989 list_for_each_entry(pin, &hdmi->pin_list, head) {
990 int sink_index = i + hdmi->num_cvt;
991 int src_index = sink_index + hdmi->num_pin;
992
993 hdac_hdmi_fill_route(&route[i],
994 widgets[sink_index].name, NULL,
995 widgets[src_index].name, NULL);
996 i++;
997
998 }
999
1000 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
1001
1002 snd_soc_dapm_new_controls(dapm, widgets,
1003 ((2 * hdmi->num_pin) + hdmi->num_cvt));
1004
1005 snd_soc_dapm_add_routes(dapm, route, num_routes);
1006 snd_soc_dapm_new_widgets(dapm->card);
1007
1008 return 0;
1009
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301010}
1011
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301012static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301013{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301014 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301015 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301016 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301017 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301018
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301019 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301020 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301021
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301022 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1023 dai_map = &hdmi->dai_map[dai_id];
1024 dai_map->dai_id = dai_id;
1025 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301026
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301027 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301028
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301029 if (dai_id == HDA_MAX_CVTS) {
1030 dev_warn(&edev->hdac.dev,
1031 "Max dais supported: %d\n", dai_id);
1032 break;
1033 }
1034 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301035
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301036 return 0;
1037}
1038
1039static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1040{
1041 struct hdac_hdmi_priv *hdmi = edev->private_data;
1042 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301043 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301044
1045 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1046 if (!cvt)
1047 return -ENOMEM;
1048
1049 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301050 sprintf(name, "cvt %d", cvt->nid);
1051 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301052
1053 list_add_tail(&cvt->head, &hdmi->cvt_list);
1054 hdmi->num_cvt++;
1055
1056 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1057}
1058
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301059static void hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1060 struct hdac_hdmi_pin *pin)
1061{
1062 pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
1063}
1064
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301065static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1066{
1067 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301068 struct hdac_hdmi_priv *hdmi = edev->private_data;
1069 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301070 int val;
1071
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301072 pin->repoll_count = repoll;
1073
1074 pm_runtime_get_sync(&edev->hdac.dev);
1075 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1076 AC_VERB_GET_PIN_SENSE, 0);
1077
1078 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1079 val, pin->nid);
1080
Jeeja KP4a3478d2016-02-12 07:46:06 +05301081
1082 mutex_lock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301083 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1084 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1085
Jeeja KP4a3478d2016-02-12 07:46:06 +05301086 pcm = hdac_hdmi_get_pcm(edev, pin);
1087
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301088 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1089
1090 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1091 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301092
1093 /*
1094 * PCMs are not registered during device probe, so don't
1095 * report jack here. It will be done in usermode mux
1096 * control select.
1097 */
1098 if (pcm) {
1099 dev_dbg(&edev->hdac.dev,
1100 "jack report for pcm=%d\n", pcm->pcm_id);
1101
1102 snd_jack_report(pcm->jack, 0);
1103 }
1104
1105 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301106 goto put_hdac_device;
1107 }
1108
1109 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1110 /* TODO: use i915 component for reading ELD later */
1111 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1112 pin->eld.eld_buffer,
1113 &pin->eld.eld_size) == 0) {
1114
Jeeja KP4a3478d2016-02-12 07:46:06 +05301115 if (pcm) {
1116 dev_dbg(&edev->hdac.dev,
1117 "jack report for pcm=%d\n",
1118 pcm->pcm_id);
1119
1120 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1121 }
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301122 hdac_hdmi_parse_eld(edev, pin);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301123
Vedang Patel3cb7cec2016-06-24 17:37:09 -07001124 print_hex_dump_debug("ELD: ",
1125 DUMP_PREFIX_OFFSET, 16, 1,
1126 pin->eld.eld_buffer, pin->eld.eld_size,
1127 true);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301128 } else {
1129 pin->eld.monitor_present = false;
1130 pin->eld.eld_valid = false;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301131
1132 if (pcm) {
1133 dev_dbg(&edev->hdac.dev,
1134 "jack report for pcm=%d\n",
1135 pcm->pcm_id);
1136
1137 snd_jack_report(pcm->jack, 0);
1138 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301139 }
1140 }
1141
Jeeja KP4a3478d2016-02-12 07:46:06 +05301142 mutex_unlock(&hdmi->pin_mutex);
1143
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301144 /*
1145 * Sometimes the pin_sense may present invalid monitor
1146 * present and eld_valid. If ELD data is not valid, loop few
1147 * more times to get correct pin sense and valid ELD.
1148 */
1149 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1150 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1151
1152put_hdac_device:
1153 pm_runtime_put_sync(&edev->hdac.dev);
1154}
1155
1156static void hdac_hdmi_repoll_eld(struct work_struct *work)
1157{
1158 struct hdac_hdmi_pin *pin =
1159 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1160
1161 /* picked from legacy HDA driver */
1162 if (pin->repoll_count++ > 6)
1163 pin->repoll_count = 0;
1164
1165 hdac_hdmi_present_sense(pin, pin->repoll_count);
1166}
1167
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301168static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1169{
1170 struct hdac_hdmi_priv *hdmi = edev->private_data;
1171 struct hdac_hdmi_pin *pin;
1172
1173 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1174 if (!pin)
1175 return -ENOMEM;
1176
1177 pin->nid = nid;
1178
1179 list_add_tail(&pin->head, &hdmi->pin_list);
1180 hdmi->num_pin++;
1181
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301182 pin->edev = edev;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301183 mutex_init(&pin->lock);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301184 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1185
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301186 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301187}
1188
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301189#define INTEL_VENDOR_NID 0x08
1190#define INTEL_GET_VENDOR_VERB 0xf81
1191#define INTEL_SET_VENDOR_VERB 0x781
1192#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1193#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1194
1195static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1196{
1197 unsigned int vendor_param;
1198
1199 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1200 INTEL_GET_VENDOR_VERB, 0);
1201 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1202 return;
1203
1204 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1205 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1206 INTEL_SET_VENDOR_VERB, vendor_param);
1207 if (vendor_param == -1)
1208 return;
1209}
1210
1211static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1212{
1213 unsigned int vendor_param;
1214
1215 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1216 INTEL_GET_VENDOR_VERB, 0);
1217 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1218 return;
1219
1220 /* enable DP1.2 mode */
1221 vendor_param |= INTEL_EN_DP12;
1222 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1223 INTEL_SET_VENDOR_VERB, vendor_param);
1224 if (vendor_param == -1)
1225 return;
1226
1227}
1228
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301229static struct snd_soc_dai_ops hdmi_dai_ops = {
1230 .startup = hdac_hdmi_pcm_open,
1231 .shutdown = hdac_hdmi_pcm_close,
1232 .hw_params = hdac_hdmi_set_hw_params,
1233 .prepare = hdac_hdmi_playback_prepare,
Jeeja KP571d5072016-02-22 07:50:33 +05301234 .trigger = hdac_hdmi_trigger,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301235 .hw_free = hdac_hdmi_playback_cleanup,
1236};
1237
1238/*
1239 * Each converter can support a stream independently. So a dai is created
1240 * based on the number of converter queried.
1241 */
1242static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1243 struct snd_soc_dai_driver **dais,
1244 struct hdac_hdmi_priv *hdmi, int num_dais)
1245{
1246 struct snd_soc_dai_driver *hdmi_dais;
1247 struct hdac_hdmi_cvt *cvt;
1248 char name[NAME_SIZE], dai_name[NAME_SIZE];
1249 int i = 0;
1250 u32 rates, bps;
1251 unsigned int rate_max = 384000, rate_min = 8000;
1252 u64 formats;
1253 int ret;
1254
1255 hdmi_dais = devm_kzalloc(&hdac->dev,
1256 (sizeof(*hdmi_dais) * num_dais),
1257 GFP_KERNEL);
1258 if (!hdmi_dais)
1259 return -ENOMEM;
1260
1261 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1262 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1263 &rates, &formats, &bps);
1264 if (ret)
1265 return ret;
1266
1267 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1268 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1269 dai_name, GFP_KERNEL);
1270
1271 if (!hdmi_dais[i].name)
1272 return -ENOMEM;
1273
1274 snprintf(name, sizeof(name), "hifi%d", i+1);
1275 hdmi_dais[i].playback.stream_name =
1276 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1277 if (!hdmi_dais[i].playback.stream_name)
1278 return -ENOMEM;
1279
1280 /*
1281 * Set caps based on capability queried from the converter.
1282 * It will be constrained runtime based on ELD queried.
1283 */
1284 hdmi_dais[i].playback.formats = formats;
1285 hdmi_dais[i].playback.rates = rates;
1286 hdmi_dais[i].playback.rate_max = rate_max;
1287 hdmi_dais[i].playback.rate_min = rate_min;
1288 hdmi_dais[i].playback.channels_min = 2;
1289 hdmi_dais[i].playback.channels_max = 2;
1290 hdmi_dais[i].ops = &hdmi_dai_ops;
1291
1292 i++;
1293 }
1294
1295 *dais = hdmi_dais;
1296
1297 return 0;
1298}
1299
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301300/*
1301 * Parse all nodes and store the cvt/pin nids in array
1302 * Add one time initialization for pin and cvt widgets
1303 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301304static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1305 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301306{
1307 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301308 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301309 struct hdac_device *hdac = &edev->hdac;
1310 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301311 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301312
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301313 hdac_hdmi_skl_enable_all_pins(hdac);
1314 hdac_hdmi_skl_enable_dp12(hdac);
1315
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301316 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301317 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301318 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1319 return -EINVAL;
1320 }
1321
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301322 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301323 hdac->start_nid = nid;
1324
1325 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1326 unsigned int caps;
1327 unsigned int type;
1328
1329 caps = get_wcaps(hdac, nid);
1330 type = get_wcaps_type(caps);
1331
1332 if (!(caps & AC_WCAP_DIGITAL))
1333 continue;
1334
1335 switch (type) {
1336
1337 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301338 ret = hdac_hdmi_add_cvt(edev, nid);
1339 if (ret < 0)
1340 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301341 break;
1342
1343 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301344 ret = hdac_hdmi_add_pin(edev, nid);
1345 if (ret < 0)
1346 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301347 break;
1348 }
1349 }
1350
1351 hdac->end_nid = nid;
1352
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301353 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301354 return -EIO;
1355
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301356 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1357 if (ret) {
1358 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1359 ret);
1360 return ret;
1361 }
1362
1363 *num_dais = hdmi->num_cvt;
1364
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301365 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301366}
1367
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301368static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1369{
1370 struct hdac_ext_device *edev = aptr;
1371 struct hdac_hdmi_priv *hdmi = edev->private_data;
1372 struct hdac_hdmi_pin *pin;
1373 struct snd_soc_codec *codec = edev->scodec;
1374
1375 /* Don't know how this mapping is derived */
1376 hda_nid_t pin_nid = port + 0x04;
1377
1378 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1379
1380 /*
1381 * skip notification during system suspend (but not in runtime PM);
1382 * the state will be updated at resume. Also since the ELD and
1383 * connection states are updated in anyway at the end of the resume,
1384 * we can skip it when received during PM process.
1385 */
1386 if (snd_power_get_state(codec->component.card->snd_card) !=
1387 SNDRV_CTL_POWER_D0)
1388 return;
1389
1390 if (atomic_read(&edev->hdac.in_pm))
1391 return;
1392
1393 list_for_each_entry(pin, &hdmi->pin_list, head) {
1394 if (pin->nid == pin_nid)
1395 hdac_hdmi_present_sense(pin, 1);
1396 }
1397}
1398
1399static struct i915_audio_component_audio_ops aops = {
1400 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1401};
1402
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301403static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1404 int device)
1405{
1406 struct snd_soc_pcm_runtime *rtd;
1407
1408 list_for_each_entry(rtd, &card->rtd_list, list) {
1409 if (rtd->pcm && (rtd->pcm->device == device))
1410 return rtd->pcm;
1411 }
1412
1413 return NULL;
1414}
1415
Jeeja KP4a3478d2016-02-12 07:46:06 +05301416int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1417{
1418 char jack_name[NAME_SIZE];
1419 struct snd_soc_codec *codec = dai->codec;
1420 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1421 struct snd_soc_dapm_context *dapm =
1422 snd_soc_component_get_dapm(&codec->component);
1423 struct hdac_hdmi_priv *hdmi = edev->private_data;
1424 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301425 struct snd_pcm *snd_pcm;
1426 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301427
1428 /*
1429 * this is a new PCM device, create new pcm and
1430 * add to the pcm list
1431 */
1432 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1433 if (!pcm)
1434 return -ENOMEM;
1435 pcm->pcm_id = device;
1436 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1437
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301438 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1439 if (snd_pcm) {
1440 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1441 if (err < 0) {
1442 dev_err(&edev->hdac.dev,
1443 "chmap control add failed with err: %d for pcm: %d\n",
1444 err, device);
1445 kfree(pcm);
1446 return err;
1447 }
1448 }
1449
Jeeja KP4a3478d2016-02-12 07:46:06 +05301450 list_add_tail(&pcm->head, &hdmi->pcm_list);
1451
1452 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1453
1454 return snd_jack_new(dapm->card->snd_card, jack_name,
1455 SND_JACK_AVOUT, &pcm->jack, true, false);
1456}
1457EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1458
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301459static int hdmi_codec_probe(struct snd_soc_codec *codec)
1460{
1461 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1462 struct hdac_hdmi_priv *hdmi = edev->private_data;
1463 struct snd_soc_dapm_context *dapm =
1464 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301465 struct hdac_hdmi_pin *pin;
Vinod Koulb2047e92016-05-12 08:58:55 +05301466 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301467 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301468
1469 edev->scodec = codec;
1470
Vinod Koulb2047e92016-05-12 08:58:55 +05301471 /*
1472 * hold the ref while we probe, also no need to drop the ref on
1473 * exit, we call pm_runtime_suspend() so that will do for us
1474 */
1475 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301476 if (!hlink) {
1477 dev_err(&edev->hdac.dev, "hdac link not found\n");
1478 return -EIO;
1479 }
1480
Vinod Koulb2047e92016-05-12 08:58:55 +05301481 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1482
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301483 ret = create_fill_widget_route_map(dapm);
1484 if (ret < 0)
1485 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301486
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301487 aops.audio_ptr = edev;
1488 ret = snd_hdac_i915_register_notifier(&aops);
1489 if (ret < 0) {
1490 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1491 ret);
1492 return ret;
1493 }
1494
1495 list_for_each_entry(pin, &hdmi->pin_list, head)
1496 hdac_hdmi_present_sense(pin, 1);
1497
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301498 /* Imp: Store the card pointer in hda_codec */
1499 edev->card = dapm->card->snd_card;
1500
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301501 /*
1502 * hdac_device core already sets the state to active and calls
1503 * get_noresume. So enable runtime and set the device to suspend.
1504 */
1505 pm_runtime_enable(&edev->hdac.dev);
1506 pm_runtime_put(&edev->hdac.dev);
1507 pm_runtime_suspend(&edev->hdac.dev);
1508
1509 return 0;
1510}
1511
1512static int hdmi_codec_remove(struct snd_soc_codec *codec)
1513{
1514 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1515
1516 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301517 return 0;
1518}
1519
Jeeja KP571d5072016-02-22 07:50:33 +05301520#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301521static int hdmi_codec_prepare(struct device *dev)
1522{
1523 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1524 struct hdac_device *hdac = &edev->hdac;
1525
1526 pm_runtime_get_sync(&edev->hdac.dev);
1527
1528 /*
1529 * Power down afg.
1530 * codec_read is preferred over codec_write to set the power state.
1531 * This way verb is send to set the power state and response
1532 * is received. So setting power state is ensured without using loop
1533 * to read the state.
1534 */
1535 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1536 AC_PWRST_D3);
1537
1538 return 0;
1539}
1540
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301541static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301542{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301543 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301544 struct hdac_hdmi_priv *hdmi = edev->private_data;
1545 struct hdac_hdmi_pin *pin;
1546 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301547
1548 /* Power up afg */
1549 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1550 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301551
1552 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1553 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1554
Jeeja KP571d5072016-02-22 07:50:33 +05301555 /*
1556 * As the ELD notify callback request is not entertained while the
1557 * device is in suspend state. Need to manually check detection of
1558 * all pins here.
1559 */
1560 list_for_each_entry(pin, &hdmi->pin_list, head)
1561 hdac_hdmi_present_sense(pin, 1);
1562
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301563 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301564}
1565#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301566#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301567#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301568#endif
1569
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301570static struct snd_soc_codec_driver hdmi_hda_codec = {
1571 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301572 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301573 .idle_bias_off = true,
1574};
1575
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301576static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1577 unsigned char *chmap)
1578{
1579 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1580 struct hdac_hdmi_priv *hdmi = edev->private_data;
1581 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1582 struct hdac_hdmi_pin *pin = pcm->pin;
1583
1584 /* chmap is already set to 0 in caller */
1585 if (!pin)
1586 return;
1587
1588 memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
1589}
1590
1591static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1592 unsigned char *chmap, int prepared)
1593{
1594 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1595 struct hdac_hdmi_priv *hdmi = edev->private_data;
1596 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1597 struct hdac_hdmi_pin *pin = pcm->pin;
1598
1599 mutex_lock(&pin->lock);
1600 pin->chmap_set = true;
1601 memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
1602 if (prepared)
1603 hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
1604 mutex_unlock(&pin->lock);
1605}
1606
1607static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1608{
1609 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1610 struct hdac_hdmi_priv *hdmi = edev->private_data;
1611 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1612 struct hdac_hdmi_pin *pin = pcm->pin;
1613
1614 return pin ? true:false;
1615}
1616
1617static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1618{
1619 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1620 struct hdac_hdmi_priv *hdmi = edev->private_data;
1621 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1622 struct hdac_hdmi_pin *pin = pcm->pin;
1623
Dan Carpenter8f658812016-05-03 10:42:58 +03001624 if (!pin || !pin->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301625 return 0;
1626
1627 return pin->eld.info.spk_alloc;
1628}
1629
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301630static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1631{
1632 struct hdac_device *codec = &edev->hdac;
1633 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301634 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301635 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301636 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301637 int ret = 0;
1638
Vinod Koulb2047e92016-05-12 08:58:55 +05301639 /* hold the ref while we probe */
1640 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301641 if (!hlink) {
1642 dev_err(&edev->hdac.dev, "hdac link not found\n");
1643 return -EIO;
1644 }
1645
Vinod Koulb2047e92016-05-12 08:58:55 +05301646 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1647
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301648 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1649 if (hdmi_priv == NULL)
1650 return -ENOMEM;
1651
1652 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301653 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301654 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1655 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1656 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1657 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301658
1659 dev_set_drvdata(&codec->dev, edev);
1660
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301661 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1662 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301663 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1664 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301665
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301666 /*
1667 * Turned off in the runtime_suspend during the first explicit
1668 * pm_runtime_suspend call.
1669 */
1670 ret = snd_hdac_display_power(edev->hdac.bus, true);
1671 if (ret < 0) {
1672 dev_err(&edev->hdac.dev,
1673 "Cannot turn on display power on i915 err: %d\n",
1674 ret);
1675 return ret;
1676 }
1677
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301678 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1679 if (ret < 0) {
1680 dev_err(&codec->dev,
1681 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301682 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301683 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301684
1685 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301686 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1687 hdmi_dais, num_dais);
1688
1689 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1690
1691 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301692}
1693
1694static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1695{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301696 struct hdac_hdmi_priv *hdmi = edev->private_data;
1697 struct hdac_hdmi_pin *pin, *pin_next;
1698 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301699 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301700
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301701 snd_soc_unregister_codec(&edev->hdac.dev);
1702
Jeeja KP4a3478d2016-02-12 07:46:06 +05301703 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1704 pcm->cvt = NULL;
1705 pcm->pin = NULL;
1706 list_del(&pcm->head);
1707 kfree(pcm);
1708 }
1709
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301710 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1711 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301712 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301713 kfree(cvt);
1714 }
1715
1716 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1717 list_del(&pin->head);
1718 kfree(pin);
1719 }
1720
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301721 return 0;
1722}
1723
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301724#ifdef CONFIG_PM
1725static int hdac_hdmi_runtime_suspend(struct device *dev)
1726{
1727 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1728 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301729 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301730 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1731 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301732 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301733
1734 dev_dbg(dev, "Enter: %s\n", __func__);
1735
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301736 /* controller may not have been initialized for the first time */
1737 if (!bus)
1738 return 0;
1739
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301740 /*
1741 * Power down afg.
1742 * codec_read is preferred over codec_write to set the power state.
1743 * This way verb is send to set the power state and response
1744 * is received. So setting power state is ensured without using loop
1745 * to read the state.
1746 */
1747 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1748 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301749 err = snd_hdac_display_power(bus, false);
1750 if (err < 0) {
1751 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1752 return err;
1753 }
1754
Vinod Koulb2047e92016-05-12 08:58:55 +05301755 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301756 if (!hlink) {
1757 dev_err(dev, "hdac link not found\n");
1758 return -EIO;
1759 }
1760
Vinod Koulb2047e92016-05-12 08:58:55 +05301761 snd_hdac_ext_bus_link_put(ebus, hlink);
1762
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301763 return 0;
1764}
1765
1766static int hdac_hdmi_runtime_resume(struct device *dev)
1767{
1768 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1769 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301770 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301771 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1772 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301773 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301774
1775 dev_dbg(dev, "Enter: %s\n", __func__);
1776
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301777 /* controller may not have been initialized for the first time */
1778 if (!bus)
1779 return 0;
1780
Vinod Koulb2047e92016-05-12 08:58:55 +05301781 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301782 if (!hlink) {
1783 dev_err(dev, "hdac link not found\n");
1784 return -EIO;
1785 }
1786
Vinod Koulb2047e92016-05-12 08:58:55 +05301787 snd_hdac_ext_bus_link_get(ebus, hlink);
1788
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301789 err = snd_hdac_display_power(bus, true);
1790 if (err < 0) {
1791 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1792 return err;
1793 }
1794
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301795 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1796 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1797
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301798 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301799 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1800 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301801
1802 return 0;
1803}
1804#else
1805#define hdac_hdmi_runtime_suspend NULL
1806#define hdac_hdmi_runtime_resume NULL
1807#endif
1808
1809static const struct dev_pm_ops hdac_hdmi_pm = {
1810 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301811 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301812 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301813};
1814
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301815static const struct hda_device_id hdmi_list[] = {
1816 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05301817 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05301818 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301819 {}
1820};
1821
1822MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1823
1824static struct hdac_ext_driver hdmi_driver = {
1825 . hdac = {
1826 .driver = {
1827 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301828 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301829 },
1830 .id_table = hdmi_list,
1831 },
1832 .probe = hdac_hdmi_dev_probe,
1833 .remove = hdac_hdmi_dev_remove,
1834};
1835
1836static int __init hdmi_init(void)
1837{
1838 return snd_hda_ext_driver_register(&hdmi_driver);
1839}
1840
1841static void __exit hdmi_exit(void)
1842{
1843 snd_hda_ext_driver_unregister(&hdmi_driver);
1844}
1845
1846module_init(hdmi_init);
1847module_exit(hdmi_exit);
1848
1849MODULE_LICENSE("GPL v2");
1850MODULE_DESCRIPTION("HDMI HD codec");
1851MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1852MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");