blob: 7851dd204a4901da8b343530adb4e7d50e35a156 [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. Prusty18382ea2015-11-10 18:42:06 +053032#include "../../hda/local.h"
Jeeja KP4a3478d2016-02-12 07:46:06 +053033#include "hdac_hdmi.h"
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053034
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +053035#define NAME_SIZE 32
36
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053037#define AMP_OUT_MUTE 0xb080
38#define AMP_OUT_UNMUTE 0xb000
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053039#define PIN_OUT (AC_PINCTL_OUT_EN)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053040
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053041#define HDA_MAX_CONNECTIONS 32
42
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053043#define HDA_MAX_CVTS 3
44
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053045#define ELD_MAX_SIZE 256
46#define ELD_FIXED_BYTES 20
47
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053048struct hdac_hdmi_cvt_params {
49 unsigned int channels_min;
50 unsigned int channels_max;
51 u32 rates;
52 u64 formats;
53 unsigned int maxbps;
54};
55
56struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053057 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053058 hda_nid_t nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +053059 const char *name;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053060 struct hdac_hdmi_cvt_params params;
61};
62
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053063struct hdac_hdmi_eld {
64 bool monitor_present;
65 bool eld_valid;
66 int eld_size;
67 char eld_buffer[ELD_MAX_SIZE];
68};
69
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053070struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053071 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053072 hda_nid_t nid;
73 int num_mux_nids;
74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053075 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
77 int repoll_count;
78 struct delayed_work work;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053079};
80
Jeeja KP4a3478d2016-02-12 07:46:06 +053081struct hdac_hdmi_pcm {
82 struct list_head head;
83 int pcm_id;
84 struct hdac_hdmi_pin *pin;
85 struct hdac_hdmi_cvt *cvt;
86 struct snd_jack *jack;
87};
88
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053089struct hdac_hdmi_dai_pin_map {
90 int dai_id;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053091 struct hdac_hdmi_pin *pin;
92 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053093};
94
95struct hdac_hdmi_priv {
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053096 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053097 struct list_head pin_list;
98 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +053099 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530100 int num_pin;
101 int num_cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530102 struct mutex pin_mutex;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530103};
104
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530105static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
106{
Geliang Tang51b2c422015-12-28 22:47:13 +0800107 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530108
Geliang Tang51b2c422015-12-28 22:47:13 +0800109 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530110}
111
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530112static unsigned int sad_format(const u8 *sad)
113{
114 return ((sad[0] >> 0x3) & 0x1f);
115}
116
117static unsigned int sad_sample_bits_lpcm(const u8 *sad)
118{
119 return (sad[2] & 7);
120}
121
122static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
123 void *eld)
124{
125 u64 formats = SNDRV_PCM_FMTBIT_S16;
126 int i;
127 const u8 *sad, *eld_buf = eld;
128
129 sad = drm_eld_sad(eld_buf);
130 if (!sad)
131 goto format_constraint;
132
133 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
134 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
135
136 /*
137 * the controller support 20 and 24 bits in 32 bit
138 * container so we set S32
139 */
140 if (sad_sample_bits_lpcm(sad) & 0x6)
141 formats |= SNDRV_PCM_FMTBIT_S32;
142 }
143 }
144
145format_constraint:
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
147 formats);
148
149}
150
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530151 /* HDMI ELD routines */
152static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153 hda_nid_t nid, int byte_index)
154{
155 unsigned int val;
156
157 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158 byte_index);
159
160 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161 byte_index, val);
162
163 return val;
164}
165
166static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
167{
168 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
169 AC_DIPSIZE_ELD_BUF);
170}
171
172/*
173 * This function queries the ELD size and ELD data and fills in the buffer
174 * passed by user
175 */
176static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177 unsigned char *buf, int *eld_size)
178{
179 int i, size, ret = 0;
180
181 /*
182 * ELD size is initialized to zero in caller function. If no errors and
183 * ELD is valid, actual eld_size is assigned.
184 */
185
186 size = hdac_hdmi_get_eld_size(codec, nid);
187 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
188 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
189 return -ERANGE;
190 }
191
192 /* set ELD buffer */
193 for (i = 0; i < size; i++) {
194 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
195 /*
196 * Graphics driver might be writing to ELD buffer right now.
197 * Just abort. The caller will repoll after a while.
198 */
199 if (!(val & AC_ELDD_ELD_VALID)) {
200 dev_err(&codec->dev,
201 "HDMI: invalid ELD data byte %d\n", i);
202 ret = -EINVAL;
203 goto error;
204 }
205 val &= AC_ELDD_ELD_DATA;
206 /*
207 * The first byte cannot be zero. This can happen on some DVI
208 * connections. Some Intel chips may also need some 250ms delay
209 * to return non-zero ELD data, even when the graphics driver
210 * correctly writes ELD content before setting ELD_valid bit.
211 */
212 if (!val && !i) {
213 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214 ret = -EINVAL;
215 goto error;
216 }
217 buf[i] = val;
218 }
219
220 *eld_size = size;
221error:
222 return ret;
223}
224
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530225static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
226 hda_nid_t cvt_nid, hda_nid_t pin_nid,
227 u32 stream_tag, int format)
228{
229 unsigned int val;
230
231 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
232 cvt_nid, pin_nid, stream_tag, format);
233
234 val = (stream_tag << 4);
235
236 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
237 AC_VERB_SET_CHANNEL_STREAMID, val);
238 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
239 AC_VERB_SET_STREAM_FORMAT, format);
240
241 return 0;
242}
243
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530244static void
245hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
246 int packet_index, int byte_index)
247{
248 int val;
249
250 val = (packet_index << 5) | (byte_index & 0x1f);
251
252 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
253 AC_VERB_SET_HDMI_DIP_INDEX, val);
254}
255
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530256struct dp_audio_infoframe {
257 u8 type; /* 0x84 */
258 u8 len; /* 0x1b */
259 u8 ver; /* 0x11 << 2 */
260
261 u8 CC02_CT47; /* match with HDMI infoframe from this on */
262 u8 SS01_SF24;
263 u8 CXT04;
264 u8 CA;
265 u8 LFEPBL01_LSV36_DM_INH7;
266};
267
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530268static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
269 hda_nid_t cvt_nid, hda_nid_t pin_nid)
270{
271 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
272 struct hdmi_audio_infoframe frame;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530273 struct dp_audio_infoframe dp_ai;
274 struct hdac_hdmi_priv *hdmi = hdac->private_data;
275 struct hdac_hdmi_pin *pin;
276 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530277 int ret;
278 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530279 const u8 *eld_buf;
280 u8 conn_type;
281 int channels = 2;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530282
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
285 break;
286 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530287
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530288 eld_buf = pin->eld.eld_buffer;
289 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530290
291 /* setup channel count */
292 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530293 AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530294
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530295 switch (conn_type) {
296 case DRM_ELD_CONN_TYPE_HDMI:
297 hdmi_audio_infoframe_init(&frame);
298
299 /* Default stereo for now */
300 frame.channels = channels;
301
302 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
303 if (ret < 0)
304 return ret;
305
306 dip = (u8 *)&frame;
307 break;
308
309 case DRM_ELD_CONN_TYPE_DP:
310 memset(&dp_ai, 0, sizeof(dp_ai));
311 dp_ai.type = 0x84;
312 dp_ai.len = 0x1b;
313 dp_ai.ver = 0x11 << 2;
314 dp_ai.CC02_CT47 = channels - 1;
315 dp_ai.CA = 0;
316
317 dip = (u8 *)&dp_ai;
318 break;
319
320 default:
321 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
322 conn_type);
323 return -EIO;
324 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530325
326 /* stop infoframe transmission */
327 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
328 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
329 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
330
331
332 /* Fill infoframe. Index auto-incremented */
333 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530334 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
335 for (i = 0; i < sizeof(frame); i++)
336 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530337 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530338 } else {
339 for (i = 0; i < sizeof(dp_ai); i++)
340 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
341 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
342 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530343
344 /* Start infoframe */
345 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
346 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
347 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
348
349 return 0;
350}
351
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530352static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
353 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
354{
355 /* Power up pin widget */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530356 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
357 pwr_state))
358 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530359 AC_VERB_SET_POWER_STATE, pwr_state);
360
361 /* Power up converter */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530362 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
363 pwr_state))
364 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530365 AC_VERB_SET_POWER_STATE, pwr_state);
366}
367
368static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
369 struct snd_soc_dai *dai)
370{
371 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
372 struct hdac_hdmi_priv *hdmi = hdac->private_data;
373 struct hdac_hdmi_dai_pin_map *dai_map;
374 struct hdac_ext_dma_params *dd;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530375 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530376
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530377 dai_map = &hdmi->dai_map[dai->id];
378
379 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
380 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
381 dd->stream_tag, dd->format);
382
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530383 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
384 dai_map->pin->nid);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530385 if (ret < 0)
386 return ret;
387
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530388 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
389 dai_map->pin->nid, dd->stream_tag, dd->format);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530390}
391
392static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
393 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
394{
395 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530396 struct hdac_hdmi_priv *hdmi = hdac->private_data;
397 struct hdac_hdmi_dai_pin_map *dai_map;
398 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530399 struct hdac_ext_dma_params *dd;
400
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530401 dai_map = &hdmi->dai_map[dai->id];
402 pin = dai_map->pin;
403
404 if (!pin)
405 return -ENODEV;
406
407 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
408 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
409 pin->nid);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530410 return -ENODEV;
411 }
412
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530413 dd = snd_soc_dai_get_dma_data(dai, substream);
414 if (!dd) {
415 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
416 if (!dd)
417 return -ENOMEM;
418 }
419
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530420 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
421 params_channels(hparams), params_format(hparams),
422 24, 0);
423
424 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
425
426 return 0;
427}
428
429static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
430 struct snd_soc_dai *dai)
431{
432 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
433 struct hdac_ext_dma_params *dd;
434 struct hdac_hdmi_priv *hdmi = edev->private_data;
435 struct hdac_hdmi_dai_pin_map *dai_map;
436
437 dai_map = &hdmi->dai_map[dai->id];
438
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530439 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530440
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530441 if (dd) {
442 snd_soc_dai_set_dma_data(dai, substream, NULL);
443 kfree(dd);
444 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530445
446 return 0;
447}
448
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +0530449static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
450 struct hdac_hdmi_dai_pin_map *dai_map)
451{
452 /* Enable transmission */
453 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
454 AC_VERB_SET_DIGI_CONVERT_1, 1);
455
456 /* Category Code (CC) to zero */
457 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
458 AC_VERB_SET_DIGI_CONVERT_2, 0);
459}
460
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530461static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
462 struct hdac_hdmi_dai_pin_map *dai_map)
463{
464 int mux_idx;
465 struct hdac_hdmi_pin *pin = dai_map->pin;
466
467 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
468 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
469 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
470 AC_VERB_SET_CONNECT_SEL, mux_idx);
471 break;
472 }
473 }
474
475 if (mux_idx == pin->num_mux_nids)
476 return -EIO;
477
478 /* Enable out path for this pin widget */
479 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
480 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
481
482 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
483
484 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
485 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
486
487 return 0;
488}
489
490static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
491 struct hdac_hdmi_pin *pin)
492{
493 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
494 dev_warn(&hdac->hdac.dev,
495 "HDMI: pin %d wcaps %#x does not support connection list\n",
496 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
497 return -EINVAL;
498 }
499
500 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
501 pin->mux_nids, HDA_MAX_CONNECTIONS);
502 if (pin->num_mux_nids == 0)
503 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
504 pin->nid);
505
506 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
507 pin->num_mux_nids, pin->nid);
508
509 return pin->num_mux_nids;
510}
511
512/*
513 * Query pcm list and return pin widget to which stream is routed.
514 *
515 * Also query connection list of the pin, to validate the cvt to pin map.
516 *
517 * Same stream rendering to multiple pins simultaneously can be done
518 * possibly, but not supported for now in driver. So return the first pin
519 * connected.
520 */
521static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
522 struct hdac_ext_device *edev,
523 struct hdac_hdmi_priv *hdmi,
524 struct hdac_hdmi_cvt *cvt)
525{
526 struct hdac_hdmi_pcm *pcm;
527 struct hdac_hdmi_pin *pin = NULL;
528 int ret, i;
529
530 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
531 if (pcm->cvt == cvt) {
532 pin = pcm->pin;
533 break;
534 }
535 }
536
537 if (pin) {
538 ret = hdac_hdmi_query_pin_connlist(edev, pin);
539 if (ret < 0)
540 return NULL;
541
542 for (i = 0; i < pin->num_mux_nids; i++) {
543 if (pin->mux_nids[i] == cvt->nid)
544 return pin;
545 }
546 }
547
548 return NULL;
549}
550
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530551/*
552 * This tries to get a valid pin and set the HW constraints based on the
553 * ELD. Even if a valid pin is not found return success so that device open
554 * doesn't fail.
555 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530556static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
557 struct snd_soc_dai *dai)
558{
559 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
560 struct hdac_hdmi_priv *hdmi = hdac->private_data;
561 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530562 struct hdac_hdmi_cvt *cvt;
563 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530564 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530565
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530566 dai_map = &hdmi->dai_map[dai->id];
567
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530568 cvt = dai_map->cvt;
569 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530570
571 /*
572 * To make PA and other userland happy.
573 * userland scans devices so returning error does not help.
574 */
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530575 if (!pin)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530576 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530577
578 if ((!pin->eld.monitor_present) ||
579 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530580
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530581 dev_warn(&hdac->hdac.dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530582 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
583 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530584
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530585 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530586 }
587
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530588 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530589
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +0530590 hdac_hdmi_enable_cvt(hdac, dai_map);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530591 ret = hdac_hdmi_enable_pin(hdac, dai_map);
592 if (ret < 0)
593 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530594
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530595 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530596 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530597 if (ret < 0)
598 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530599
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530600 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530601 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530602}
603
Jeeja KP571d5072016-02-22 07:50:33 +0530604static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
605 struct snd_soc_dai *dai)
606{
607 struct hdac_hdmi_dai_pin_map *dai_map;
608 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
609 struct hdac_hdmi_priv *hdmi = hdac->private_data;
610 int ret;
611
612 dai_map = &hdmi->dai_map[dai->id];
613 if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
614 ret = hdac_hdmi_enable_pin(hdac, dai_map);
615 if (ret < 0)
616 return ret;
617
618 return hdac_hdmi_playback_prepare(substream, dai);
619 }
620
621 return 0;
622}
623
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530624static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
625 struct snd_soc_dai *dai)
626{
627 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
628 struct hdac_hdmi_priv *hdmi = hdac->private_data;
629 struct hdac_hdmi_dai_pin_map *dai_map;
630
631 dai_map = &hdmi->dai_map[dai->id];
632
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530633 if (dai_map->pin) {
634 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
635 AC_VERB_SET_CHANNEL_STREAMID, 0);
636 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
637 AC_VERB_SET_STREAM_FORMAT, 0);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530638
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530639 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
640
641 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530642 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530643
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530644 dai_map->pin = NULL;
645 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530646}
647
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530648static int
649hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
650{
651 int err;
652
653 /* Only stereo supported as of now */
654 cvt->params.channels_min = cvt->params.channels_max = 2;
655
656 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
657 &cvt->params.rates,
658 &cvt->params.formats,
659 &cvt->params.maxbps);
660 if (err < 0)
661 dev_err(&hdac->dev,
662 "Failed to query pcm params for nid %d: %d\n",
663 cvt->nid, err);
664
665 return err;
666}
667
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530668static int hdac_hdmi_fill_widget_info(struct device *dev,
669 struct snd_soc_dapm_widget *w,
670 enum snd_soc_dapm_type id, void *priv,
671 const char *wname, const char *stream,
672 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530673{
674 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530675 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
676 if (!w->name)
677 return -ENOMEM;
678
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530679 w->sname = stream;
680 w->reg = SND_SOC_NOPM;
681 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530682 w->kcontrol_news = wc;
683 w->num_kcontrols = numkc;
684 w->priv = priv;
685
686 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530687}
688
689static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530690 const char *sink, const char *control, const char *src,
691 int (*handler)(struct snd_soc_dapm_widget *src,
692 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530693{
694 route->sink = sink;
695 route->source = src;
696 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530697 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530698}
699
Jeeja KP4a3478d2016-02-12 07:46:06 +0530700static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
701 struct hdac_hdmi_pin *pin)
702{
703 struct hdac_hdmi_priv *hdmi = edev->private_data;
704 struct hdac_hdmi_pcm *pcm = NULL;
705
706 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
707 if (pcm->pin == pin)
708 return pcm;
709 }
710
711 return NULL;
712}
713
714/*
715 * Based on user selection, map the PINs with the PCMs.
716 */
717static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
718 struct snd_ctl_elem_value *ucontrol)
719{
720 int ret;
721 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
722 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
723 struct snd_soc_dapm_context *dapm = w->dapm;
724 struct hdac_hdmi_pin *pin = w->priv;
725 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
726 struct hdac_hdmi_priv *hdmi = edev->private_data;
727 struct hdac_hdmi_pcm *pcm = NULL;
728 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
729
730 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
731 if (ret < 0)
732 return ret;
733
734 mutex_lock(&hdmi->pin_mutex);
735 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
736 if (pcm->pin == pin)
737 pcm->pin = NULL;
738
739 /*
740 * Jack status is not reported during device probe as the
741 * PCMs are not registered by then. So report it here.
742 */
743 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
744 pcm->pin = pin;
745 if (pin->eld.monitor_present && pin->eld.eld_valid) {
746 dev_dbg(&edev->hdac.dev,
747 "jack report for pcm=%d\n",
748 pcm->pcm_id);
749
750 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
751 }
752 mutex_unlock(&hdmi->pin_mutex);
753 return ret;
754 }
755 }
756 mutex_unlock(&hdmi->pin_mutex);
757
758 return ret;
759}
760
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530761/*
762 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
763 * the display driver seem to be programming the connection list for the pin
764 * widget runtime.
765 *
766 * So programming all the possible inputs for the mux, the user has to take
767 * care of selecting the right one and leaving all other inputs selected to
768 * "NONE"
769 */
770static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
771 struct hdac_hdmi_pin *pin,
772 struct snd_soc_dapm_widget *widget,
773 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530774{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530775 struct hdac_hdmi_priv *hdmi = edev->private_data;
776 struct snd_kcontrol_new *kc;
777 struct hdac_hdmi_cvt *cvt;
778 struct soc_enum *se;
779 char kc_name[NAME_SIZE];
780 char mux_items[NAME_SIZE];
781 /* To hold inputs to the Pin mux */
782 char *items[HDA_MAX_CONNECTIONS];
783 int i = 0;
784 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530785
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530786 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
787 if (!kc)
788 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530789
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530790 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
791 if (!se)
792 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530793
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530794 sprintf(kc_name, "Pin %d Input", pin->nid);
795 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
796 if (!kc->name)
797 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530798
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530799 kc->private_value = (long)se;
800 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
801 kc->access = 0;
802 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530803 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530804 kc->get = snd_soc_dapm_get_enum_double;
805
806 se->reg = SND_SOC_NOPM;
807
808 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
809 se->items = num_items;
810 se->mask = roundup_pow_of_two(se->items) - 1;
811
812 sprintf(mux_items, "NONE");
813 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
814 if (!items[i])
815 return -ENOMEM;
816
817 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
818 i++;
819 sprintf(mux_items, "cvt %d", cvt->nid);
820 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
821 if (!items[i])
822 return -ENOMEM;
823 }
824
825 se->texts = devm_kmemdup(&edev->hdac.dev, items,
826 (num_items * sizeof(char *)), GFP_KERNEL);
827 if (!se->texts)
828 return -ENOMEM;
829
830 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530831 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530832}
833
834/* Add cvt <- input <- mux route map */
835static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
836 struct snd_soc_dapm_widget *widgets,
837 struct snd_soc_dapm_route *route, int rindex)
838{
839 struct hdac_hdmi_priv *hdmi = edev->private_data;
840 const struct snd_kcontrol_new *kc;
841 struct soc_enum *se;
842 int mux_index = hdmi->num_cvt + hdmi->num_pin;
843 int i, j;
844
845 for (i = 0; i < hdmi->num_pin; i++) {
846 kc = widgets[mux_index].kcontrol_news;
847 se = (struct soc_enum *)kc->private_value;
848 for (j = 0; j < hdmi->num_cvt; j++) {
849 hdac_hdmi_fill_route(&route[rindex],
850 widgets[mux_index].name,
851 se->texts[j + 1],
852 widgets[j].name, NULL);
853
854 rindex++;
855 }
856
857 mux_index++;
858 }
859}
860
861/*
862 * Widgets are added in the below sequence
863 * Converter widgets for num converters enumerated
864 * Pin widgets for num pins enumerated
865 * Pin mux widgets to represent connenction list of pin widget
866 *
867 * Total widgets elements = num_cvt + num_pin + num_pin;
868 *
869 * Routes are added as below:
870 * pin mux -> pin (based on num_pins)
871 * cvt -> "Input sel control" -> pin_mux
872 *
873 * Total route elements:
874 * num_pins + (pin_muxes * num_cvt)
875 */
876static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
877{
878 struct snd_soc_dapm_widget *widgets;
879 struct snd_soc_dapm_route *route;
880 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
881 struct hdac_hdmi_priv *hdmi = edev->private_data;
882 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
883 char widget_name[NAME_SIZE];
884 struct hdac_hdmi_cvt *cvt;
885 struct hdac_hdmi_pin *pin;
886 int ret, i = 0, num_routes = 0;
887
888 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
889 return -EINVAL;
890
891 widgets = devm_kzalloc(dapm->dev,
892 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
893 GFP_KERNEL);
894
895 if (!widgets)
896 return -ENOMEM;
897
898 /* DAPM widgets to represent each converter widget */
899 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
900 sprintf(widget_name, "Converter %d", cvt->nid);
901 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
902 snd_soc_dapm_aif_in, &cvt->nid,
903 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
904 if (ret < 0)
905 return ret;
906 i++;
907 }
908
909 list_for_each_entry(pin, &hdmi->pin_list, head) {
910 sprintf(widget_name, "hif%d Output", pin->nid);
911 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
912 snd_soc_dapm_output, &pin->nid,
913 widget_name, NULL, NULL, 0);
914 if (ret < 0)
915 return ret;
916 i++;
917 }
918
919 /* DAPM widgets to represent the connection list to pin widget */
920 list_for_each_entry(pin, &hdmi->pin_list, head) {
921 sprintf(widget_name, "Pin %d Mux", pin->nid);
922 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
923 widget_name);
924 if (ret < 0)
925 return ret;
926 i++;
927
928 /* For cvt to pin_mux mapping */
929 num_routes += hdmi->num_cvt;
930
931 /* For pin_mux to pin mapping */
932 num_routes++;
933 }
934
935 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
936 GFP_KERNEL);
937 if (!route)
938 return -ENOMEM;
939
940 i = 0;
941 /* Add pin <- NULL <- mux route map */
942 list_for_each_entry(pin, &hdmi->pin_list, head) {
943 int sink_index = i + hdmi->num_cvt;
944 int src_index = sink_index + hdmi->num_pin;
945
946 hdac_hdmi_fill_route(&route[i],
947 widgets[sink_index].name, NULL,
948 widgets[src_index].name, NULL);
949 i++;
950
951 }
952
953 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
954
955 snd_soc_dapm_new_controls(dapm, widgets,
956 ((2 * hdmi->num_pin) + hdmi->num_cvt));
957
958 snd_soc_dapm_add_routes(dapm, route, num_routes);
959 snd_soc_dapm_new_widgets(dapm->card);
960
961 return 0;
962
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530963}
964
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530965static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530966{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530967 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530968 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530969 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530970 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530971
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530972 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530973 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530974
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530975 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
976 dai_map = &hdmi->dai_map[dai_id];
977 dai_map->dai_id = dai_id;
978 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530979
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530980 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530981
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530982 if (dai_id == HDA_MAX_CVTS) {
983 dev_warn(&edev->hdac.dev,
984 "Max dais supported: %d\n", dai_id);
985 break;
986 }
987 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530988
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530989 return 0;
990}
991
992static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
993{
994 struct hdac_hdmi_priv *hdmi = edev->private_data;
995 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530996 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530997
998 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
999 if (!cvt)
1000 return -ENOMEM;
1001
1002 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301003 sprintf(name, "cvt %d", cvt->nid);
1004 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301005
1006 list_add_tail(&cvt->head, &hdmi->cvt_list);
1007 hdmi->num_cvt++;
1008
1009 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1010}
1011
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301012static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1013{
1014 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301015 struct hdac_hdmi_priv *hdmi = edev->private_data;
1016 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301017 int val;
1018
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301019 pin->repoll_count = repoll;
1020
1021 pm_runtime_get_sync(&edev->hdac.dev);
1022 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1023 AC_VERB_GET_PIN_SENSE, 0);
1024
1025 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1026 val, pin->nid);
1027
Jeeja KP4a3478d2016-02-12 07:46:06 +05301028
1029 mutex_lock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301030 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1031 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1032
Jeeja KP4a3478d2016-02-12 07:46:06 +05301033 pcm = hdac_hdmi_get_pcm(edev, pin);
1034
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301035 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1036
1037 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1038 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301039
1040 /*
1041 * PCMs are not registered during device probe, so don't
1042 * report jack here. It will be done in usermode mux
1043 * control select.
1044 */
1045 if (pcm) {
1046 dev_dbg(&edev->hdac.dev,
1047 "jack report for pcm=%d\n", pcm->pcm_id);
1048
1049 snd_jack_report(pcm->jack, 0);
1050 }
1051
1052 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301053 goto put_hdac_device;
1054 }
1055
1056 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1057 /* TODO: use i915 component for reading ELD later */
1058 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1059 pin->eld.eld_buffer,
1060 &pin->eld.eld_size) == 0) {
1061
Jeeja KP4a3478d2016-02-12 07:46:06 +05301062 if (pcm) {
1063 dev_dbg(&edev->hdac.dev,
1064 "jack report for pcm=%d\n",
1065 pcm->pcm_id);
1066
1067 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1068 }
1069
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301070 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1071 pin->eld.eld_buffer, pin->eld.eld_size);
1072 } else {
1073 pin->eld.monitor_present = false;
1074 pin->eld.eld_valid = false;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301075
1076 if (pcm) {
1077 dev_dbg(&edev->hdac.dev,
1078 "jack report for pcm=%d\n",
1079 pcm->pcm_id);
1080
1081 snd_jack_report(pcm->jack, 0);
1082 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301083 }
1084 }
1085
Jeeja KP4a3478d2016-02-12 07:46:06 +05301086 mutex_unlock(&hdmi->pin_mutex);
1087
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301088 /*
1089 * Sometimes the pin_sense may present invalid monitor
1090 * present and eld_valid. If ELD data is not valid, loop few
1091 * more times to get correct pin sense and valid ELD.
1092 */
1093 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1094 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1095
1096put_hdac_device:
1097 pm_runtime_put_sync(&edev->hdac.dev);
1098}
1099
1100static void hdac_hdmi_repoll_eld(struct work_struct *work)
1101{
1102 struct hdac_hdmi_pin *pin =
1103 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1104
1105 /* picked from legacy HDA driver */
1106 if (pin->repoll_count++ > 6)
1107 pin->repoll_count = 0;
1108
1109 hdac_hdmi_present_sense(pin, pin->repoll_count);
1110}
1111
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301112static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1113{
1114 struct hdac_hdmi_priv *hdmi = edev->private_data;
1115 struct hdac_hdmi_pin *pin;
1116
1117 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1118 if (!pin)
1119 return -ENOMEM;
1120
1121 pin->nid = nid;
1122
1123 list_add_tail(&pin->head, &hdmi->pin_list);
1124 hdmi->num_pin++;
1125
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301126 pin->edev = edev;
1127 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1128
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301129 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301130}
1131
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301132#define INTEL_VENDOR_NID 0x08
1133#define INTEL_GET_VENDOR_VERB 0xf81
1134#define INTEL_SET_VENDOR_VERB 0x781
1135#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1136#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1137
1138static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1139{
1140 unsigned int vendor_param;
1141
1142 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1143 INTEL_GET_VENDOR_VERB, 0);
1144 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1145 return;
1146
1147 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1148 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1149 INTEL_SET_VENDOR_VERB, vendor_param);
1150 if (vendor_param == -1)
1151 return;
1152}
1153
1154static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1155{
1156 unsigned int vendor_param;
1157
1158 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1159 INTEL_GET_VENDOR_VERB, 0);
1160 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1161 return;
1162
1163 /* enable DP1.2 mode */
1164 vendor_param |= INTEL_EN_DP12;
1165 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1166 INTEL_SET_VENDOR_VERB, vendor_param);
1167 if (vendor_param == -1)
1168 return;
1169
1170}
1171
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301172static struct snd_soc_dai_ops hdmi_dai_ops = {
1173 .startup = hdac_hdmi_pcm_open,
1174 .shutdown = hdac_hdmi_pcm_close,
1175 .hw_params = hdac_hdmi_set_hw_params,
1176 .prepare = hdac_hdmi_playback_prepare,
Jeeja KP571d5072016-02-22 07:50:33 +05301177 .trigger = hdac_hdmi_trigger,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301178 .hw_free = hdac_hdmi_playback_cleanup,
1179};
1180
1181/*
1182 * Each converter can support a stream independently. So a dai is created
1183 * based on the number of converter queried.
1184 */
1185static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1186 struct snd_soc_dai_driver **dais,
1187 struct hdac_hdmi_priv *hdmi, int num_dais)
1188{
1189 struct snd_soc_dai_driver *hdmi_dais;
1190 struct hdac_hdmi_cvt *cvt;
1191 char name[NAME_SIZE], dai_name[NAME_SIZE];
1192 int i = 0;
1193 u32 rates, bps;
1194 unsigned int rate_max = 384000, rate_min = 8000;
1195 u64 formats;
1196 int ret;
1197
1198 hdmi_dais = devm_kzalloc(&hdac->dev,
1199 (sizeof(*hdmi_dais) * num_dais),
1200 GFP_KERNEL);
1201 if (!hdmi_dais)
1202 return -ENOMEM;
1203
1204 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1205 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1206 &rates, &formats, &bps);
1207 if (ret)
1208 return ret;
1209
1210 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1211 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1212 dai_name, GFP_KERNEL);
1213
1214 if (!hdmi_dais[i].name)
1215 return -ENOMEM;
1216
1217 snprintf(name, sizeof(name), "hifi%d", i+1);
1218 hdmi_dais[i].playback.stream_name =
1219 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1220 if (!hdmi_dais[i].playback.stream_name)
1221 return -ENOMEM;
1222
1223 /*
1224 * Set caps based on capability queried from the converter.
1225 * It will be constrained runtime based on ELD queried.
1226 */
1227 hdmi_dais[i].playback.formats = formats;
1228 hdmi_dais[i].playback.rates = rates;
1229 hdmi_dais[i].playback.rate_max = rate_max;
1230 hdmi_dais[i].playback.rate_min = rate_min;
1231 hdmi_dais[i].playback.channels_min = 2;
1232 hdmi_dais[i].playback.channels_max = 2;
1233 hdmi_dais[i].ops = &hdmi_dai_ops;
1234
1235 i++;
1236 }
1237
1238 *dais = hdmi_dais;
1239
1240 return 0;
1241}
1242
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301243/*
1244 * Parse all nodes and store the cvt/pin nids in array
1245 * Add one time initialization for pin and cvt widgets
1246 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301247static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1248 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301249{
1250 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301251 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301252 struct hdac_device *hdac = &edev->hdac;
1253 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301254 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301255
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301256 hdac_hdmi_skl_enable_all_pins(hdac);
1257 hdac_hdmi_skl_enable_dp12(hdac);
1258
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301259 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301260 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301261 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1262 return -EINVAL;
1263 }
1264
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301265 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301266 hdac->start_nid = nid;
1267
1268 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1269 unsigned int caps;
1270 unsigned int type;
1271
1272 caps = get_wcaps(hdac, nid);
1273 type = get_wcaps_type(caps);
1274
1275 if (!(caps & AC_WCAP_DIGITAL))
1276 continue;
1277
1278 switch (type) {
1279
1280 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301281 ret = hdac_hdmi_add_cvt(edev, nid);
1282 if (ret < 0)
1283 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301284 break;
1285
1286 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301287 ret = hdac_hdmi_add_pin(edev, nid);
1288 if (ret < 0)
1289 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301290 break;
1291 }
1292 }
1293
1294 hdac->end_nid = nid;
1295
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301296 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301297 return -EIO;
1298
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301299 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1300 if (ret) {
1301 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1302 ret);
1303 return ret;
1304 }
1305
1306 *num_dais = hdmi->num_cvt;
1307
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301308 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301309}
1310
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301311static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1312{
1313 struct hdac_ext_device *edev = aptr;
1314 struct hdac_hdmi_priv *hdmi = edev->private_data;
1315 struct hdac_hdmi_pin *pin;
1316 struct snd_soc_codec *codec = edev->scodec;
1317
1318 /* Don't know how this mapping is derived */
1319 hda_nid_t pin_nid = port + 0x04;
1320
1321 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1322
1323 /*
1324 * skip notification during system suspend (but not in runtime PM);
1325 * the state will be updated at resume. Also since the ELD and
1326 * connection states are updated in anyway at the end of the resume,
1327 * we can skip it when received during PM process.
1328 */
1329 if (snd_power_get_state(codec->component.card->snd_card) !=
1330 SNDRV_CTL_POWER_D0)
1331 return;
1332
1333 if (atomic_read(&edev->hdac.in_pm))
1334 return;
1335
1336 list_for_each_entry(pin, &hdmi->pin_list, head) {
1337 if (pin->nid == pin_nid)
1338 hdac_hdmi_present_sense(pin, 1);
1339 }
1340}
1341
1342static struct i915_audio_component_audio_ops aops = {
1343 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1344};
1345
Jeeja KP4a3478d2016-02-12 07:46:06 +05301346int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1347{
1348 char jack_name[NAME_SIZE];
1349 struct snd_soc_codec *codec = dai->codec;
1350 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1351 struct snd_soc_dapm_context *dapm =
1352 snd_soc_component_get_dapm(&codec->component);
1353 struct hdac_hdmi_priv *hdmi = edev->private_data;
1354 struct hdac_hdmi_pcm *pcm;
1355
1356 /*
1357 * this is a new PCM device, create new pcm and
1358 * add to the pcm list
1359 */
1360 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1361 if (!pcm)
1362 return -ENOMEM;
1363 pcm->pcm_id = device;
1364 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1365
1366 list_add_tail(&pcm->head, &hdmi->pcm_list);
1367
1368 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1369
1370 return snd_jack_new(dapm->card->snd_card, jack_name,
1371 SND_JACK_AVOUT, &pcm->jack, true, false);
1372}
1373EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1374
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301375static int hdmi_codec_probe(struct snd_soc_codec *codec)
1376{
1377 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1378 struct hdac_hdmi_priv *hdmi = edev->private_data;
1379 struct snd_soc_dapm_context *dapm =
1380 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301381 struct hdac_hdmi_pin *pin;
1382 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301383
1384 edev->scodec = codec;
1385
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301386 ret = create_fill_widget_route_map(dapm);
1387 if (ret < 0)
1388 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301389
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301390 aops.audio_ptr = edev;
1391 ret = snd_hdac_i915_register_notifier(&aops);
1392 if (ret < 0) {
1393 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1394 ret);
1395 return ret;
1396 }
1397
1398 list_for_each_entry(pin, &hdmi->pin_list, head)
1399 hdac_hdmi_present_sense(pin, 1);
1400
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301401 /* Imp: Store the card pointer in hda_codec */
1402 edev->card = dapm->card->snd_card;
1403
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301404 /*
1405 * hdac_device core already sets the state to active and calls
1406 * get_noresume. So enable runtime and set the device to suspend.
1407 */
1408 pm_runtime_enable(&edev->hdac.dev);
1409 pm_runtime_put(&edev->hdac.dev);
1410 pm_runtime_suspend(&edev->hdac.dev);
1411
1412 return 0;
1413}
1414
1415static int hdmi_codec_remove(struct snd_soc_codec *codec)
1416{
1417 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1418
1419 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301420 return 0;
1421}
1422
Jeeja KP571d5072016-02-22 07:50:33 +05301423#ifdef CONFIG_PM
1424static int hdmi_codec_resume(struct snd_soc_codec *codec)
1425{
1426 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1427 struct hdac_hdmi_priv *hdmi = edev->private_data;
1428 struct hdac_hdmi_pin *pin;
1429 struct hdac_device *hdac = &edev->hdac;
1430 struct hdac_bus *bus = hdac->bus;
1431 int err;
1432 unsigned long timeout;
1433
1434 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1435 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1436
1437 /* Power up afg */
1438 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1439
1440 snd_hdac_codec_write(hdac, hdac->afg, 0,
1441 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1442
1443 /* Wait till power state is set to D0 */
1444 timeout = jiffies + msecs_to_jiffies(1000);
1445 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1446 && time_before(jiffies, timeout)) {
1447 msleep(50);
1448 }
1449 }
1450
1451 /*
1452 * As the ELD notify callback request is not entertained while the
1453 * device is in suspend state. Need to manually check detection of
1454 * all pins here.
1455 */
1456 list_for_each_entry(pin, &hdmi->pin_list, head)
1457 hdac_hdmi_present_sense(pin, 1);
1458
1459 /*
1460 * Codec power is turned ON during controller resume.
1461 * Turn it OFF here
1462 */
1463 err = snd_hdac_display_power(bus, false);
1464 if (err < 0) {
1465 dev_err(bus->dev,
1466 "Cannot turn OFF display power on i915, err: %d\n",
1467 err);
1468 return err;
1469 }
1470
1471 return 0;
1472}
1473#else
1474#define hdmi_codec_resume NULL
1475#endif
1476
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301477static struct snd_soc_codec_driver hdmi_hda_codec = {
1478 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301479 .remove = hdmi_codec_remove,
Jeeja KP571d5072016-02-22 07:50:33 +05301480 .resume = hdmi_codec_resume,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301481 .idle_bias_off = true,
1482};
1483
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301484static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1485{
1486 struct hdac_device *codec = &edev->hdac;
1487 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301488 struct snd_soc_dai_driver *hdmi_dais = NULL;
1489 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301490 int ret = 0;
1491
1492 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1493 if (hdmi_priv == NULL)
1494 return -ENOMEM;
1495
1496 edev->private_data = hdmi_priv;
1497
1498 dev_set_drvdata(&codec->dev, edev);
1499
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301500 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1501 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301502 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1503 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301504
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301505 /*
1506 * Turned off in the runtime_suspend during the first explicit
1507 * pm_runtime_suspend call.
1508 */
1509 ret = snd_hdac_display_power(edev->hdac.bus, true);
1510 if (ret < 0) {
1511 dev_err(&edev->hdac.dev,
1512 "Cannot turn on display power on i915 err: %d\n",
1513 ret);
1514 return ret;
1515 }
1516
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301517 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1518 if (ret < 0) {
1519 dev_err(&codec->dev,
1520 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301521 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301522 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301523
1524 /* ASoC specific initialization */
1525 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301526 hdmi_dais, num_dais);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301527}
1528
1529static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1530{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301531 struct hdac_hdmi_priv *hdmi = edev->private_data;
1532 struct hdac_hdmi_pin *pin, *pin_next;
1533 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301534 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301535
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301536 snd_soc_unregister_codec(&edev->hdac.dev);
1537
Jeeja KP4a3478d2016-02-12 07:46:06 +05301538 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1539 pcm->cvt = NULL;
1540 pcm->pin = NULL;
1541 list_del(&pcm->head);
1542 kfree(pcm);
1543 }
1544
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301545 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1546 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301547 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301548 kfree(cvt);
1549 }
1550
1551 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1552 list_del(&pin->head);
1553 kfree(pin);
1554 }
1555
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301556 return 0;
1557}
1558
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301559#ifdef CONFIG_PM
1560static int hdac_hdmi_runtime_suspend(struct device *dev)
1561{
1562 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1563 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301564 struct hdac_bus *bus = hdac->bus;
Subhransu S. Prusty7ed49702016-02-22 07:50:32 +05301565 unsigned long timeout;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301566 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301567
1568 dev_dbg(dev, "Enter: %s\n", __func__);
1569
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301570 /* controller may not have been initialized for the first time */
1571 if (!bus)
1572 return 0;
1573
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301574 /* Power down afg */
Subhransu S. Prusty7ed49702016-02-22 07:50:32 +05301575 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301576 snd_hdac_codec_write(hdac, hdac->afg, 0,
1577 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1578
Subhransu S. Prusty7ed49702016-02-22 07:50:32 +05301579 /* Wait till power state is set to D3 */
1580 timeout = jiffies + msecs_to_jiffies(1000);
1581 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
1582 && time_before(jiffies, timeout)) {
1583
1584 msleep(50);
1585 }
1586 }
1587
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301588 err = snd_hdac_display_power(bus, false);
1589 if (err < 0) {
1590 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1591 return err;
1592 }
1593
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301594 return 0;
1595}
1596
1597static int hdac_hdmi_runtime_resume(struct device *dev)
1598{
1599 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1600 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301601 struct hdac_bus *bus = hdac->bus;
1602 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301603
1604 dev_dbg(dev, "Enter: %s\n", __func__);
1605
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301606 /* controller may not have been initialized for the first time */
1607 if (!bus)
1608 return 0;
1609
1610 err = snd_hdac_display_power(bus, true);
1611 if (err < 0) {
1612 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1613 return err;
1614 }
1615
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301616 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1617 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1618
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301619 /* Power up afg */
1620 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1621 snd_hdac_codec_write(hdac, hdac->afg, 0,
1622 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1623
1624 return 0;
1625}
1626#else
1627#define hdac_hdmi_runtime_suspend NULL
1628#define hdac_hdmi_runtime_resume NULL
1629#endif
1630
1631static const struct dev_pm_ops hdac_hdmi_pm = {
1632 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1633};
1634
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301635static const struct hda_device_id hdmi_list[] = {
1636 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1637 {}
1638};
1639
1640MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1641
1642static struct hdac_ext_driver hdmi_driver = {
1643 . hdac = {
1644 .driver = {
1645 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301646 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301647 },
1648 .id_table = hdmi_list,
1649 },
1650 .probe = hdac_hdmi_dev_probe,
1651 .remove = hdac_hdmi_dev_remove,
1652};
1653
1654static int __init hdmi_init(void)
1655{
1656 return snd_hda_ext_driver_register(&hdmi_driver);
1657}
1658
1659static void __exit hdmi_exit(void)
1660{
1661 snd_hda_ext_driver_unregister(&hdmi_driver);
1662}
1663
1664module_init(hdmi_init);
1665module_exit(hdmi_exit);
1666
1667MODULE_LICENSE("GPL v2");
1668MODULE_DESCRIPTION("HDMI HD codec");
1669MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1670MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");