blob: f12af62a208467492a416cf1db1fe7a88a742d65 [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
377 if (dai->id > 0) {
378 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
379 return -ENODEV;
380 }
381
382 dai_map = &hdmi->dai_map[dai->id];
383
384 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
385 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
386 dd->stream_tag, dd->format);
387
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530388 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
389 dai_map->pin->nid);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530390 if (ret < 0)
391 return ret;
392
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530393 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
394 dai_map->pin->nid, dd->stream_tag, dd->format);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530395}
396
397static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
398 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
399{
400 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530401 struct hdac_hdmi_priv *hdmi = hdac->private_data;
402 struct hdac_hdmi_dai_pin_map *dai_map;
403 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530404 struct hdac_ext_dma_params *dd;
405
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530406 dai_map = &hdmi->dai_map[dai->id];
407 pin = dai_map->pin;
408
409 if (!pin)
410 return -ENODEV;
411
412 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
413 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
414 pin->nid);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530415 return -ENODEV;
416 }
417
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530418 dd = snd_soc_dai_get_dma_data(dai, substream);
419 if (!dd) {
420 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
421 if (!dd)
422 return -ENOMEM;
423 }
424
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530425 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
426 params_channels(hparams), params_format(hparams),
427 24, 0);
428
429 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
430
431 return 0;
432}
433
434static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
435 struct snd_soc_dai *dai)
436{
437 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
438 struct hdac_ext_dma_params *dd;
439 struct hdac_hdmi_priv *hdmi = edev->private_data;
440 struct hdac_hdmi_dai_pin_map *dai_map;
441
442 dai_map = &hdmi->dai_map[dai->id];
443
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530444 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530445
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530446 if (dd) {
447 snd_soc_dai_set_dma_data(dai, substream, NULL);
448 kfree(dd);
449 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530450
451 return 0;
452}
453
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530454static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
455 struct hdac_hdmi_dai_pin_map *dai_map)
456{
457 int mux_idx;
458 struct hdac_hdmi_pin *pin = dai_map->pin;
459
460 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
461 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
462 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
463 AC_VERB_SET_CONNECT_SEL, mux_idx);
464 break;
465 }
466 }
467
468 if (mux_idx == pin->num_mux_nids)
469 return -EIO;
470
471 /* Enable out path for this pin widget */
472 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
473 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
474
475 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
476
477 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
478 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
479
480 return 0;
481}
482
483static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
484 struct hdac_hdmi_pin *pin)
485{
486 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
487 dev_warn(&hdac->hdac.dev,
488 "HDMI: pin %d wcaps %#x does not support connection list\n",
489 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
490 return -EINVAL;
491 }
492
493 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
494 pin->mux_nids, HDA_MAX_CONNECTIONS);
495 if (pin->num_mux_nids == 0)
496 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
497 pin->nid);
498
499 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
500 pin->num_mux_nids, pin->nid);
501
502 return pin->num_mux_nids;
503}
504
505/*
506 * Query pcm list and return pin widget to which stream is routed.
507 *
508 * Also query connection list of the pin, to validate the cvt to pin map.
509 *
510 * Same stream rendering to multiple pins simultaneously can be done
511 * possibly, but not supported for now in driver. So return the first pin
512 * connected.
513 */
514static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
515 struct hdac_ext_device *edev,
516 struct hdac_hdmi_priv *hdmi,
517 struct hdac_hdmi_cvt *cvt)
518{
519 struct hdac_hdmi_pcm *pcm;
520 struct hdac_hdmi_pin *pin = NULL;
521 int ret, i;
522
523 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
524 if (pcm->cvt == cvt) {
525 pin = pcm->pin;
526 break;
527 }
528 }
529
530 if (pin) {
531 ret = hdac_hdmi_query_pin_connlist(edev, pin);
532 if (ret < 0)
533 return NULL;
534
535 for (i = 0; i < pin->num_mux_nids; i++) {
536 if (pin->mux_nids[i] == cvt->nid)
537 return pin;
538 }
539 }
540
541 return NULL;
542}
543
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530544/*
545 * This tries to get a valid pin and set the HW constraints based on the
546 * ELD. Even if a valid pin is not found return success so that device open
547 * doesn't fail.
548 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530549static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
550 struct snd_soc_dai *dai)
551{
552 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
553 struct hdac_hdmi_priv *hdmi = hdac->private_data;
554 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530555 struct hdac_hdmi_cvt *cvt;
556 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530557 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530558
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530559 dai_map = &hdmi->dai_map[dai->id];
560
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530561 cvt = dai_map->cvt;
562 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530563
564 /*
565 * To make PA and other userland happy.
566 * userland scans devices so returning error does not help.
567 */
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530568 if (!pin)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530569 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530570
571 if ((!pin->eld.monitor_present) ||
572 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530573
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530574 dev_warn(&hdac->hdac.dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530575 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
576 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530577
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530578 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530579 }
580
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530581 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530582
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530583 ret = hdac_hdmi_enable_pin(hdac, dai_map);
584 if (ret < 0)
585 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530586
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530587 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530588 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530589 if (ret < 0)
590 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530591
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530592 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530593 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530594}
595
596static void hdac_hdmi_pcm_close(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;
602
603 dai_map = &hdmi->dai_map[dai->id];
604
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530605 if (dai_map->pin) {
606 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
607 AC_VERB_SET_CHANNEL_STREAMID, 0);
608 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
609 AC_VERB_SET_STREAM_FORMAT, 0);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530610
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530611 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
612
613 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530614 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530615
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530616 dai_map->pin = NULL;
617 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530618}
619
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530620static int
621hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
622{
623 int err;
624
625 /* Only stereo supported as of now */
626 cvt->params.channels_min = cvt->params.channels_max = 2;
627
628 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
629 &cvt->params.rates,
630 &cvt->params.formats,
631 &cvt->params.maxbps);
632 if (err < 0)
633 dev_err(&hdac->dev,
634 "Failed to query pcm params for nid %d: %d\n",
635 cvt->nid, err);
636
637 return err;
638}
639
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530640static int hdac_hdmi_fill_widget_info(struct device *dev,
641 struct snd_soc_dapm_widget *w,
642 enum snd_soc_dapm_type id, void *priv,
643 const char *wname, const char *stream,
644 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530645{
646 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530647 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
648 if (!w->name)
649 return -ENOMEM;
650
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530651 w->sname = stream;
652 w->reg = SND_SOC_NOPM;
653 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530654 w->kcontrol_news = wc;
655 w->num_kcontrols = numkc;
656 w->priv = priv;
657
658 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530659}
660
661static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530662 const char *sink, const char *control, const char *src,
663 int (*handler)(struct snd_soc_dapm_widget *src,
664 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530665{
666 route->sink = sink;
667 route->source = src;
668 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530669 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530670}
671
Jeeja KP4a3478d2016-02-12 07:46:06 +0530672static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
673 struct hdac_hdmi_pin *pin)
674{
675 struct hdac_hdmi_priv *hdmi = edev->private_data;
676 struct hdac_hdmi_pcm *pcm = NULL;
677
678 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
679 if (pcm->pin == pin)
680 return pcm;
681 }
682
683 return NULL;
684}
685
686/*
687 * Based on user selection, map the PINs with the PCMs.
688 */
689static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
690 struct snd_ctl_elem_value *ucontrol)
691{
692 int ret;
693 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
694 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
695 struct snd_soc_dapm_context *dapm = w->dapm;
696 struct hdac_hdmi_pin *pin = w->priv;
697 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
698 struct hdac_hdmi_priv *hdmi = edev->private_data;
699 struct hdac_hdmi_pcm *pcm = NULL;
700 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
701
702 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
703 if (ret < 0)
704 return ret;
705
706 mutex_lock(&hdmi->pin_mutex);
707 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
708 if (pcm->pin == pin)
709 pcm->pin = NULL;
710
711 /*
712 * Jack status is not reported during device probe as the
713 * PCMs are not registered by then. So report it here.
714 */
715 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
716 pcm->pin = pin;
717 if (pin->eld.monitor_present && pin->eld.eld_valid) {
718 dev_dbg(&edev->hdac.dev,
719 "jack report for pcm=%d\n",
720 pcm->pcm_id);
721
722 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
723 }
724 mutex_unlock(&hdmi->pin_mutex);
725 return ret;
726 }
727 }
728 mutex_unlock(&hdmi->pin_mutex);
729
730 return ret;
731}
732
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530733/*
734 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
735 * the display driver seem to be programming the connection list for the pin
736 * widget runtime.
737 *
738 * So programming all the possible inputs for the mux, the user has to take
739 * care of selecting the right one and leaving all other inputs selected to
740 * "NONE"
741 */
742static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
743 struct hdac_hdmi_pin *pin,
744 struct snd_soc_dapm_widget *widget,
745 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530746{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530747 struct hdac_hdmi_priv *hdmi = edev->private_data;
748 struct snd_kcontrol_new *kc;
749 struct hdac_hdmi_cvt *cvt;
750 struct soc_enum *se;
751 char kc_name[NAME_SIZE];
752 char mux_items[NAME_SIZE];
753 /* To hold inputs to the Pin mux */
754 char *items[HDA_MAX_CONNECTIONS];
755 int i = 0;
756 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530757
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530758 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
759 if (!kc)
760 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530761
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530762 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
763 if (!se)
764 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530765
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530766 sprintf(kc_name, "Pin %d Input", pin->nid);
767 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
768 if (!kc->name)
769 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530770
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530771 kc->private_value = (long)se;
772 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
773 kc->access = 0;
774 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530775 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530776 kc->get = snd_soc_dapm_get_enum_double;
777
778 se->reg = SND_SOC_NOPM;
779
780 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
781 se->items = num_items;
782 se->mask = roundup_pow_of_two(se->items) - 1;
783
784 sprintf(mux_items, "NONE");
785 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
786 if (!items[i])
787 return -ENOMEM;
788
789 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
790 i++;
791 sprintf(mux_items, "cvt %d", cvt->nid);
792 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
793 if (!items[i])
794 return -ENOMEM;
795 }
796
797 se->texts = devm_kmemdup(&edev->hdac.dev, items,
798 (num_items * sizeof(char *)), GFP_KERNEL);
799 if (!se->texts)
800 return -ENOMEM;
801
802 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530803 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530804}
805
806/* Add cvt <- input <- mux route map */
807static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
808 struct snd_soc_dapm_widget *widgets,
809 struct snd_soc_dapm_route *route, int rindex)
810{
811 struct hdac_hdmi_priv *hdmi = edev->private_data;
812 const struct snd_kcontrol_new *kc;
813 struct soc_enum *se;
814 int mux_index = hdmi->num_cvt + hdmi->num_pin;
815 int i, j;
816
817 for (i = 0; i < hdmi->num_pin; i++) {
818 kc = widgets[mux_index].kcontrol_news;
819 se = (struct soc_enum *)kc->private_value;
820 for (j = 0; j < hdmi->num_cvt; j++) {
821 hdac_hdmi_fill_route(&route[rindex],
822 widgets[mux_index].name,
823 se->texts[j + 1],
824 widgets[j].name, NULL);
825
826 rindex++;
827 }
828
829 mux_index++;
830 }
831}
832
833/*
834 * Widgets are added in the below sequence
835 * Converter widgets for num converters enumerated
836 * Pin widgets for num pins enumerated
837 * Pin mux widgets to represent connenction list of pin widget
838 *
839 * Total widgets elements = num_cvt + num_pin + num_pin;
840 *
841 * Routes are added as below:
842 * pin mux -> pin (based on num_pins)
843 * cvt -> "Input sel control" -> pin_mux
844 *
845 * Total route elements:
846 * num_pins + (pin_muxes * num_cvt)
847 */
848static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
849{
850 struct snd_soc_dapm_widget *widgets;
851 struct snd_soc_dapm_route *route;
852 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
853 struct hdac_hdmi_priv *hdmi = edev->private_data;
854 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
855 char widget_name[NAME_SIZE];
856 struct hdac_hdmi_cvt *cvt;
857 struct hdac_hdmi_pin *pin;
858 int ret, i = 0, num_routes = 0;
859
860 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
861 return -EINVAL;
862
863 widgets = devm_kzalloc(dapm->dev,
864 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
865 GFP_KERNEL);
866
867 if (!widgets)
868 return -ENOMEM;
869
870 /* DAPM widgets to represent each converter widget */
871 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
872 sprintf(widget_name, "Converter %d", cvt->nid);
873 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
874 snd_soc_dapm_aif_in, &cvt->nid,
875 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
876 if (ret < 0)
877 return ret;
878 i++;
879 }
880
881 list_for_each_entry(pin, &hdmi->pin_list, head) {
882 sprintf(widget_name, "hif%d Output", pin->nid);
883 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
884 snd_soc_dapm_output, &pin->nid,
885 widget_name, NULL, NULL, 0);
886 if (ret < 0)
887 return ret;
888 i++;
889 }
890
891 /* DAPM widgets to represent the connection list to pin widget */
892 list_for_each_entry(pin, &hdmi->pin_list, head) {
893 sprintf(widget_name, "Pin %d Mux", pin->nid);
894 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
895 widget_name);
896 if (ret < 0)
897 return ret;
898 i++;
899
900 /* For cvt to pin_mux mapping */
901 num_routes += hdmi->num_cvt;
902
903 /* For pin_mux to pin mapping */
904 num_routes++;
905 }
906
907 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
908 GFP_KERNEL);
909 if (!route)
910 return -ENOMEM;
911
912 i = 0;
913 /* Add pin <- NULL <- mux route map */
914 list_for_each_entry(pin, &hdmi->pin_list, head) {
915 int sink_index = i + hdmi->num_cvt;
916 int src_index = sink_index + hdmi->num_pin;
917
918 hdac_hdmi_fill_route(&route[i],
919 widgets[sink_index].name, NULL,
920 widgets[src_index].name, NULL);
921 i++;
922
923 }
924
925 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
926
927 snd_soc_dapm_new_controls(dapm, widgets,
928 ((2 * hdmi->num_pin) + hdmi->num_cvt));
929
930 snd_soc_dapm_add_routes(dapm, route, num_routes);
931 snd_soc_dapm_new_widgets(dapm->card);
932
933 return 0;
934
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530935}
936
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530937static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530938{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530939 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530940 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530941 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530942 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530943
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530944 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530945 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530946
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530947 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
948 dai_map = &hdmi->dai_map[dai_id];
949 dai_map->dai_id = dai_id;
950 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530951
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530952 /* Enable transmission */
953 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
954 AC_VERB_SET_DIGI_CONVERT_1, 1);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530955
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530956 /* Category Code (CC) to zero */
957 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
958 AC_VERB_SET_DIGI_CONVERT_2, 0);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530959
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530960 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530961
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530962 if (dai_id == HDA_MAX_CVTS) {
963 dev_warn(&edev->hdac.dev,
964 "Max dais supported: %d\n", dai_id);
965 break;
966 }
967 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530968
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530969 return 0;
970}
971
972static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
973{
974 struct hdac_hdmi_priv *hdmi = edev->private_data;
975 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530976 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530977
978 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
979 if (!cvt)
980 return -ENOMEM;
981
982 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530983 sprintf(name, "cvt %d", cvt->nid);
984 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530985
986 list_add_tail(&cvt->head, &hdmi->cvt_list);
987 hdmi->num_cvt++;
988
989 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
990}
991
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530992static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
993{
994 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530995 struct hdac_hdmi_priv *hdmi = edev->private_data;
996 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530997 int val;
998
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530999 pin->repoll_count = repoll;
1000
1001 pm_runtime_get_sync(&edev->hdac.dev);
1002 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1003 AC_VERB_GET_PIN_SENSE, 0);
1004
1005 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1006 val, pin->nid);
1007
Jeeja KP4a3478d2016-02-12 07:46:06 +05301008
1009 mutex_lock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301010 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1011 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1012
Jeeja KP4a3478d2016-02-12 07:46:06 +05301013 pcm = hdac_hdmi_get_pcm(edev, pin);
1014
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301015 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1016
1017 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1018 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301019
1020 /*
1021 * PCMs are not registered during device probe, so don't
1022 * report jack here. It will be done in usermode mux
1023 * control select.
1024 */
1025 if (pcm) {
1026 dev_dbg(&edev->hdac.dev,
1027 "jack report for pcm=%d\n", pcm->pcm_id);
1028
1029 snd_jack_report(pcm->jack, 0);
1030 }
1031
1032 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301033 goto put_hdac_device;
1034 }
1035
1036 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1037 /* TODO: use i915 component for reading ELD later */
1038 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1039 pin->eld.eld_buffer,
1040 &pin->eld.eld_size) == 0) {
1041
Jeeja KP4a3478d2016-02-12 07:46:06 +05301042 if (pcm) {
1043 dev_dbg(&edev->hdac.dev,
1044 "jack report for pcm=%d\n",
1045 pcm->pcm_id);
1046
1047 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1048 }
1049
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301050 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1051 pin->eld.eld_buffer, pin->eld.eld_size);
1052 } else {
1053 pin->eld.monitor_present = false;
1054 pin->eld.eld_valid = false;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301055
1056 if (pcm) {
1057 dev_dbg(&edev->hdac.dev,
1058 "jack report for pcm=%d\n",
1059 pcm->pcm_id);
1060
1061 snd_jack_report(pcm->jack, 0);
1062 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301063 }
1064 }
1065
Jeeja KP4a3478d2016-02-12 07:46:06 +05301066 mutex_unlock(&hdmi->pin_mutex);
1067
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301068 /*
1069 * Sometimes the pin_sense may present invalid monitor
1070 * present and eld_valid. If ELD data is not valid, loop few
1071 * more times to get correct pin sense and valid ELD.
1072 */
1073 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1074 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1075
1076put_hdac_device:
1077 pm_runtime_put_sync(&edev->hdac.dev);
1078}
1079
1080static void hdac_hdmi_repoll_eld(struct work_struct *work)
1081{
1082 struct hdac_hdmi_pin *pin =
1083 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1084
1085 /* picked from legacy HDA driver */
1086 if (pin->repoll_count++ > 6)
1087 pin->repoll_count = 0;
1088
1089 hdac_hdmi_present_sense(pin, pin->repoll_count);
1090}
1091
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301092static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1093{
1094 struct hdac_hdmi_priv *hdmi = edev->private_data;
1095 struct hdac_hdmi_pin *pin;
1096
1097 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1098 if (!pin)
1099 return -ENOMEM;
1100
1101 pin->nid = nid;
1102
1103 list_add_tail(&pin->head, &hdmi->pin_list);
1104 hdmi->num_pin++;
1105
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301106 pin->edev = edev;
1107 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1108
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301109 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301110}
1111
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301112#define INTEL_VENDOR_NID 0x08
1113#define INTEL_GET_VENDOR_VERB 0xf81
1114#define INTEL_SET_VENDOR_VERB 0x781
1115#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1116#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1117
1118static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1119{
1120 unsigned int vendor_param;
1121
1122 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1123 INTEL_GET_VENDOR_VERB, 0);
1124 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1125 return;
1126
1127 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1128 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1129 INTEL_SET_VENDOR_VERB, vendor_param);
1130 if (vendor_param == -1)
1131 return;
1132}
1133
1134static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1135{
1136 unsigned int vendor_param;
1137
1138 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1139 INTEL_GET_VENDOR_VERB, 0);
1140 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1141 return;
1142
1143 /* enable DP1.2 mode */
1144 vendor_param |= INTEL_EN_DP12;
1145 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1146 INTEL_SET_VENDOR_VERB, vendor_param);
1147 if (vendor_param == -1)
1148 return;
1149
1150}
1151
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301152static struct snd_soc_dai_ops hdmi_dai_ops = {
1153 .startup = hdac_hdmi_pcm_open,
1154 .shutdown = hdac_hdmi_pcm_close,
1155 .hw_params = hdac_hdmi_set_hw_params,
1156 .prepare = hdac_hdmi_playback_prepare,
1157 .hw_free = hdac_hdmi_playback_cleanup,
1158};
1159
1160/*
1161 * Each converter can support a stream independently. So a dai is created
1162 * based on the number of converter queried.
1163 */
1164static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1165 struct snd_soc_dai_driver **dais,
1166 struct hdac_hdmi_priv *hdmi, int num_dais)
1167{
1168 struct snd_soc_dai_driver *hdmi_dais;
1169 struct hdac_hdmi_cvt *cvt;
1170 char name[NAME_SIZE], dai_name[NAME_SIZE];
1171 int i = 0;
1172 u32 rates, bps;
1173 unsigned int rate_max = 384000, rate_min = 8000;
1174 u64 formats;
1175 int ret;
1176
1177 hdmi_dais = devm_kzalloc(&hdac->dev,
1178 (sizeof(*hdmi_dais) * num_dais),
1179 GFP_KERNEL);
1180 if (!hdmi_dais)
1181 return -ENOMEM;
1182
1183 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1184 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1185 &rates, &formats, &bps);
1186 if (ret)
1187 return ret;
1188
1189 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1190 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1191 dai_name, GFP_KERNEL);
1192
1193 if (!hdmi_dais[i].name)
1194 return -ENOMEM;
1195
1196 snprintf(name, sizeof(name), "hifi%d", i+1);
1197 hdmi_dais[i].playback.stream_name =
1198 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1199 if (!hdmi_dais[i].playback.stream_name)
1200 return -ENOMEM;
1201
1202 /*
1203 * Set caps based on capability queried from the converter.
1204 * It will be constrained runtime based on ELD queried.
1205 */
1206 hdmi_dais[i].playback.formats = formats;
1207 hdmi_dais[i].playback.rates = rates;
1208 hdmi_dais[i].playback.rate_max = rate_max;
1209 hdmi_dais[i].playback.rate_min = rate_min;
1210 hdmi_dais[i].playback.channels_min = 2;
1211 hdmi_dais[i].playback.channels_max = 2;
1212 hdmi_dais[i].ops = &hdmi_dai_ops;
1213
1214 i++;
1215 }
1216
1217 *dais = hdmi_dais;
1218
1219 return 0;
1220}
1221
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301222/*
1223 * Parse all nodes and store the cvt/pin nids in array
1224 * Add one time initialization for pin and cvt widgets
1225 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301226static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1227 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301228{
1229 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301230 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301231 struct hdac_device *hdac = &edev->hdac;
1232 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301233 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301234
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301235 hdac_hdmi_skl_enable_all_pins(hdac);
1236 hdac_hdmi_skl_enable_dp12(hdac);
1237
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301238 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301239 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301240 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1241 return -EINVAL;
1242 }
1243
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301244 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301245 hdac->start_nid = nid;
1246
1247 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1248 unsigned int caps;
1249 unsigned int type;
1250
1251 caps = get_wcaps(hdac, nid);
1252 type = get_wcaps_type(caps);
1253
1254 if (!(caps & AC_WCAP_DIGITAL))
1255 continue;
1256
1257 switch (type) {
1258
1259 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301260 ret = hdac_hdmi_add_cvt(edev, nid);
1261 if (ret < 0)
1262 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301263 break;
1264
1265 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301266 ret = hdac_hdmi_add_pin(edev, nid);
1267 if (ret < 0)
1268 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301269 break;
1270 }
1271 }
1272
1273 hdac->end_nid = nid;
1274
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301275 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301276 return -EIO;
1277
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301278 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1279 if (ret) {
1280 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1281 ret);
1282 return ret;
1283 }
1284
1285 *num_dais = hdmi->num_cvt;
1286
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301287 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301288}
1289
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301290static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1291{
1292 struct hdac_ext_device *edev = aptr;
1293 struct hdac_hdmi_priv *hdmi = edev->private_data;
1294 struct hdac_hdmi_pin *pin;
1295 struct snd_soc_codec *codec = edev->scodec;
1296
1297 /* Don't know how this mapping is derived */
1298 hda_nid_t pin_nid = port + 0x04;
1299
1300 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1301
1302 /*
1303 * skip notification during system suspend (but not in runtime PM);
1304 * the state will be updated at resume. Also since the ELD and
1305 * connection states are updated in anyway at the end of the resume,
1306 * we can skip it when received during PM process.
1307 */
1308 if (snd_power_get_state(codec->component.card->snd_card) !=
1309 SNDRV_CTL_POWER_D0)
1310 return;
1311
1312 if (atomic_read(&edev->hdac.in_pm))
1313 return;
1314
1315 list_for_each_entry(pin, &hdmi->pin_list, head) {
1316 if (pin->nid == pin_nid)
1317 hdac_hdmi_present_sense(pin, 1);
1318 }
1319}
1320
1321static struct i915_audio_component_audio_ops aops = {
1322 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1323};
1324
Jeeja KP4a3478d2016-02-12 07:46:06 +05301325int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1326{
1327 char jack_name[NAME_SIZE];
1328 struct snd_soc_codec *codec = dai->codec;
1329 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1330 struct snd_soc_dapm_context *dapm =
1331 snd_soc_component_get_dapm(&codec->component);
1332 struct hdac_hdmi_priv *hdmi = edev->private_data;
1333 struct hdac_hdmi_pcm *pcm;
1334
1335 /*
1336 * this is a new PCM device, create new pcm and
1337 * add to the pcm list
1338 */
1339 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1340 if (!pcm)
1341 return -ENOMEM;
1342 pcm->pcm_id = device;
1343 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1344
1345 list_add_tail(&pcm->head, &hdmi->pcm_list);
1346
1347 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1348
1349 return snd_jack_new(dapm->card->snd_card, jack_name,
1350 SND_JACK_AVOUT, &pcm->jack, true, false);
1351}
1352EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1353
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301354static int hdmi_codec_probe(struct snd_soc_codec *codec)
1355{
1356 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1357 struct hdac_hdmi_priv *hdmi = edev->private_data;
1358 struct snd_soc_dapm_context *dapm =
1359 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301360 struct hdac_hdmi_pin *pin;
1361 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301362
1363 edev->scodec = codec;
1364
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301365 ret = create_fill_widget_route_map(dapm);
1366 if (ret < 0)
1367 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301368
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301369 aops.audio_ptr = edev;
1370 ret = snd_hdac_i915_register_notifier(&aops);
1371 if (ret < 0) {
1372 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1373 ret);
1374 return ret;
1375 }
1376
1377 list_for_each_entry(pin, &hdmi->pin_list, head)
1378 hdac_hdmi_present_sense(pin, 1);
1379
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301380 /* Imp: Store the card pointer in hda_codec */
1381 edev->card = dapm->card->snd_card;
1382
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301383 /*
1384 * hdac_device core already sets the state to active and calls
1385 * get_noresume. So enable runtime and set the device to suspend.
1386 */
1387 pm_runtime_enable(&edev->hdac.dev);
1388 pm_runtime_put(&edev->hdac.dev);
1389 pm_runtime_suspend(&edev->hdac.dev);
1390
1391 return 0;
1392}
1393
1394static int hdmi_codec_remove(struct snd_soc_codec *codec)
1395{
1396 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1397
1398 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301399 return 0;
1400}
1401
1402static struct snd_soc_codec_driver hdmi_hda_codec = {
1403 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301404 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301405 .idle_bias_off = true,
1406};
1407
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301408static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1409{
1410 struct hdac_device *codec = &edev->hdac;
1411 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301412 struct snd_soc_dai_driver *hdmi_dais = NULL;
1413 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301414 int ret = 0;
1415
1416 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1417 if (hdmi_priv == NULL)
1418 return -ENOMEM;
1419
1420 edev->private_data = hdmi_priv;
1421
1422 dev_set_drvdata(&codec->dev, edev);
1423
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301424 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1425 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301426 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1427 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301428
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301429 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1430 if (ret < 0) {
1431 dev_err(&codec->dev,
1432 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301433 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301434 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301435
1436 /* ASoC specific initialization */
1437 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301438 hdmi_dais, num_dais);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301439}
1440
1441static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1442{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301443 struct hdac_hdmi_priv *hdmi = edev->private_data;
1444 struct hdac_hdmi_pin *pin, *pin_next;
1445 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301446 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301447
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301448 snd_soc_unregister_codec(&edev->hdac.dev);
1449
Jeeja KP4a3478d2016-02-12 07:46:06 +05301450 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1451 pcm->cvt = NULL;
1452 pcm->pin = NULL;
1453 list_del(&pcm->head);
1454 kfree(pcm);
1455 }
1456
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301457 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1458 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301459 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301460 kfree(cvt);
1461 }
1462
1463 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1464 list_del(&pin->head);
1465 kfree(pin);
1466 }
1467
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301468 return 0;
1469}
1470
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301471#ifdef CONFIG_PM
1472static int hdac_hdmi_runtime_suspend(struct device *dev)
1473{
1474 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1475 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301476 struct hdac_bus *bus = hdac->bus;
1477 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301478
1479 dev_dbg(dev, "Enter: %s\n", __func__);
1480
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301481 /* controller may not have been initialized for the first time */
1482 if (!bus)
1483 return 0;
1484
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301485 /* Power down afg */
1486 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1487 snd_hdac_codec_write(hdac, hdac->afg, 0,
1488 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1489
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301490 err = snd_hdac_display_power(bus, false);
1491 if (err < 0) {
1492 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1493 return err;
1494 }
1495
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301496 return 0;
1497}
1498
1499static int hdac_hdmi_runtime_resume(struct device *dev)
1500{
1501 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1502 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301503 struct hdac_bus *bus = hdac->bus;
1504 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301505
1506 dev_dbg(dev, "Enter: %s\n", __func__);
1507
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301508 /* controller may not have been initialized for the first time */
1509 if (!bus)
1510 return 0;
1511
1512 err = snd_hdac_display_power(bus, true);
1513 if (err < 0) {
1514 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1515 return err;
1516 }
1517
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301518 /* Power up afg */
1519 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1520 snd_hdac_codec_write(hdac, hdac->afg, 0,
1521 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1522
1523 return 0;
1524}
1525#else
1526#define hdac_hdmi_runtime_suspend NULL
1527#define hdac_hdmi_runtime_resume NULL
1528#endif
1529
1530static const struct dev_pm_ops hdac_hdmi_pm = {
1531 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1532};
1533
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301534static const struct hda_device_id hdmi_list[] = {
1535 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1536 {}
1537};
1538
1539MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1540
1541static struct hdac_ext_driver hdmi_driver = {
1542 . hdac = {
1543 .driver = {
1544 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301545 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301546 },
1547 .id_table = hdmi_list,
1548 },
1549 .probe = hdac_hdmi_dev_probe,
1550 .remove = hdac_hdmi_dev_remove,
1551};
1552
1553static int __init hdmi_init(void)
1554{
1555 return snd_hda_ext_driver_register(&hdmi_driver);
1556}
1557
1558static void __exit hdmi_exit(void)
1559{
1560 snd_hda_ext_driver_unregister(&hdmi_driver);
1561}
1562
1563module_init(hdmi_init);
1564module_exit(hdmi_exit);
1565
1566MODULE_LICENSE("GPL v2");
1567MODULE_DESCRIPTION("HDMI HD codec");
1568MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1569MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");