blob: d2a2e1882fdd2288dcca04751b3b0567cffdcdc9 [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);
401 struct hdac_ext_dma_params *dd;
402
403 if (dai->id > 0) {
404 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
405 return -ENODEV;
406 }
407
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530408 dd = snd_soc_dai_get_dma_data(dai, substream);
409 if (!dd) {
410 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
411 if (!dd)
412 return -ENOMEM;
413 }
414
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530415 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
416 params_channels(hparams), params_format(hparams),
417 24, 0);
418
419 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
420
421 return 0;
422}
423
424static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
425 struct snd_soc_dai *dai)
426{
427 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
428 struct hdac_ext_dma_params *dd;
429 struct hdac_hdmi_priv *hdmi = edev->private_data;
430 struct hdac_hdmi_dai_pin_map *dai_map;
431
432 dai_map = &hdmi->dai_map[dai->id];
433
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530434 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530435 AC_VERB_SET_CHANNEL_STREAMID, 0);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530436 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530437 AC_VERB_SET_STREAM_FORMAT, 0);
438
439 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. Prusty148569f2016-02-12 07:46:07 +0530449static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
450 struct hdac_hdmi_dai_pin_map *dai_map)
451{
452 int mux_idx;
453 struct hdac_hdmi_pin *pin = dai_map->pin;
454
455 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
456 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
457 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
458 AC_VERB_SET_CONNECT_SEL, mux_idx);
459 break;
460 }
461 }
462
463 if (mux_idx == pin->num_mux_nids)
464 return -EIO;
465
466 /* Enable out path for this pin widget */
467 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
468 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
469
470 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
471
472 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
473 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
474
475 return 0;
476}
477
478static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
479 struct hdac_hdmi_pin *pin)
480{
481 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
482 dev_warn(&hdac->hdac.dev,
483 "HDMI: pin %d wcaps %#x does not support connection list\n",
484 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
485 return -EINVAL;
486 }
487
488 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
489 pin->mux_nids, HDA_MAX_CONNECTIONS);
490 if (pin->num_mux_nids == 0)
491 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
492 pin->nid);
493
494 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
495 pin->num_mux_nids, pin->nid);
496
497 return pin->num_mux_nids;
498}
499
500/*
501 * Query pcm list and return pin widget to which stream is routed.
502 *
503 * Also query connection list of the pin, to validate the cvt to pin map.
504 *
505 * Same stream rendering to multiple pins simultaneously can be done
506 * possibly, but not supported for now in driver. So return the first pin
507 * connected.
508 */
509static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
510 struct hdac_ext_device *edev,
511 struct hdac_hdmi_priv *hdmi,
512 struct hdac_hdmi_cvt *cvt)
513{
514 struct hdac_hdmi_pcm *pcm;
515 struct hdac_hdmi_pin *pin = NULL;
516 int ret, i;
517
518 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
519 if (pcm->cvt == cvt) {
520 pin = pcm->pin;
521 break;
522 }
523 }
524
525 if (pin) {
526 ret = hdac_hdmi_query_pin_connlist(edev, pin);
527 if (ret < 0)
528 return NULL;
529
530 for (i = 0; i < pin->num_mux_nids; i++) {
531 if (pin->mux_nids[i] == cvt->nid)
532 return pin;
533 }
534 }
535
536 return NULL;
537}
538
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530539static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
540 struct snd_soc_dai *dai)
541{
542 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
543 struct hdac_hdmi_priv *hdmi = hdac->private_data;
544 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530545 struct hdac_hdmi_cvt *cvt;
546 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530547 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530548
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530549 dai_map = &hdmi->dai_map[dai->id];
550
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530551 cvt = dai_map->cvt;
552 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
553 if (!pin)
554 return -EIO;
555
556 if ((!pin->eld.monitor_present) ||
557 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530558
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530559 dev_err(&hdac->hdac.dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530560 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
561 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530562
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530563 return -ENODEV;
564 }
565
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530566 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530567
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530568 ret = hdac_hdmi_enable_pin(hdac, dai_map);
569 if (ret < 0)
570 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530571
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530572 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530573 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530574 if (ret < 0)
575 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530576
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530577 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530578 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530579}
580
581static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
582 struct snd_soc_dai *dai)
583{
584 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
585 struct hdac_hdmi_priv *hdmi = hdac->private_data;
586 struct hdac_hdmi_dai_pin_map *dai_map;
587
588 dai_map = &hdmi->dai_map[dai->id];
589
590 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
591
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530592 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530593 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530594
595 dai_map->pin = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530596}
597
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530598static int
599hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
600{
601 int err;
602
603 /* Only stereo supported as of now */
604 cvt->params.channels_min = cvt->params.channels_max = 2;
605
606 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
607 &cvt->params.rates,
608 &cvt->params.formats,
609 &cvt->params.maxbps);
610 if (err < 0)
611 dev_err(&hdac->dev,
612 "Failed to query pcm params for nid %d: %d\n",
613 cvt->nid, err);
614
615 return err;
616}
617
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530618static int hdac_hdmi_fill_widget_info(struct device *dev,
619 struct snd_soc_dapm_widget *w,
620 enum snd_soc_dapm_type id, void *priv,
621 const char *wname, const char *stream,
622 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530623{
624 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530625 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
626 if (!w->name)
627 return -ENOMEM;
628
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530629 w->sname = stream;
630 w->reg = SND_SOC_NOPM;
631 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530632 w->kcontrol_news = wc;
633 w->num_kcontrols = numkc;
634 w->priv = priv;
635
636 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530637}
638
639static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530640 const char *sink, const char *control, const char *src,
641 int (*handler)(struct snd_soc_dapm_widget *src,
642 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530643{
644 route->sink = sink;
645 route->source = src;
646 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530647 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530648}
649
Jeeja KP4a3478d2016-02-12 07:46:06 +0530650static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
651 struct hdac_hdmi_pin *pin)
652{
653 struct hdac_hdmi_priv *hdmi = edev->private_data;
654 struct hdac_hdmi_pcm *pcm = NULL;
655
656 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
657 if (pcm->pin == pin)
658 return pcm;
659 }
660
661 return NULL;
662}
663
664/*
665 * Based on user selection, map the PINs with the PCMs.
666 */
667static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
668 struct snd_ctl_elem_value *ucontrol)
669{
670 int ret;
671 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
672 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
673 struct snd_soc_dapm_context *dapm = w->dapm;
674 struct hdac_hdmi_pin *pin = w->priv;
675 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
676 struct hdac_hdmi_priv *hdmi = edev->private_data;
677 struct hdac_hdmi_pcm *pcm = NULL;
678 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
679
680 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
681 if (ret < 0)
682 return ret;
683
684 mutex_lock(&hdmi->pin_mutex);
685 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
686 if (pcm->pin == pin)
687 pcm->pin = NULL;
688
689 /*
690 * Jack status is not reported during device probe as the
691 * PCMs are not registered by then. So report it here.
692 */
693 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
694 pcm->pin = pin;
695 if (pin->eld.monitor_present && pin->eld.eld_valid) {
696 dev_dbg(&edev->hdac.dev,
697 "jack report for pcm=%d\n",
698 pcm->pcm_id);
699
700 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
701 }
702 mutex_unlock(&hdmi->pin_mutex);
703 return ret;
704 }
705 }
706 mutex_unlock(&hdmi->pin_mutex);
707
708 return ret;
709}
710
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530711/*
712 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
713 * the display driver seem to be programming the connection list for the pin
714 * widget runtime.
715 *
716 * So programming all the possible inputs for the mux, the user has to take
717 * care of selecting the right one and leaving all other inputs selected to
718 * "NONE"
719 */
720static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
721 struct hdac_hdmi_pin *pin,
722 struct snd_soc_dapm_widget *widget,
723 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530724{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530725 struct hdac_hdmi_priv *hdmi = edev->private_data;
726 struct snd_kcontrol_new *kc;
727 struct hdac_hdmi_cvt *cvt;
728 struct soc_enum *se;
729 char kc_name[NAME_SIZE];
730 char mux_items[NAME_SIZE];
731 /* To hold inputs to the Pin mux */
732 char *items[HDA_MAX_CONNECTIONS];
733 int i = 0;
734 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530735
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530736 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
737 if (!kc)
738 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530739
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530740 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
741 if (!se)
742 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530743
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530744 sprintf(kc_name, "Pin %d Input", pin->nid);
745 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
746 if (!kc->name)
747 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530748
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530749 kc->private_value = (long)se;
750 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
751 kc->access = 0;
752 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530753 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530754 kc->get = snd_soc_dapm_get_enum_double;
755
756 se->reg = SND_SOC_NOPM;
757
758 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
759 se->items = num_items;
760 se->mask = roundup_pow_of_two(se->items) - 1;
761
762 sprintf(mux_items, "NONE");
763 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
764 if (!items[i])
765 return -ENOMEM;
766
767 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
768 i++;
769 sprintf(mux_items, "cvt %d", cvt->nid);
770 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
771 if (!items[i])
772 return -ENOMEM;
773 }
774
775 se->texts = devm_kmemdup(&edev->hdac.dev, items,
776 (num_items * sizeof(char *)), GFP_KERNEL);
777 if (!se->texts)
778 return -ENOMEM;
779
780 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530781 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530782}
783
784/* Add cvt <- input <- mux route map */
785static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
786 struct snd_soc_dapm_widget *widgets,
787 struct snd_soc_dapm_route *route, int rindex)
788{
789 struct hdac_hdmi_priv *hdmi = edev->private_data;
790 const struct snd_kcontrol_new *kc;
791 struct soc_enum *se;
792 int mux_index = hdmi->num_cvt + hdmi->num_pin;
793 int i, j;
794
795 for (i = 0; i < hdmi->num_pin; i++) {
796 kc = widgets[mux_index].kcontrol_news;
797 se = (struct soc_enum *)kc->private_value;
798 for (j = 0; j < hdmi->num_cvt; j++) {
799 hdac_hdmi_fill_route(&route[rindex],
800 widgets[mux_index].name,
801 se->texts[j + 1],
802 widgets[j].name, NULL);
803
804 rindex++;
805 }
806
807 mux_index++;
808 }
809}
810
811/*
812 * Widgets are added in the below sequence
813 * Converter widgets for num converters enumerated
814 * Pin widgets for num pins enumerated
815 * Pin mux widgets to represent connenction list of pin widget
816 *
817 * Total widgets elements = num_cvt + num_pin + num_pin;
818 *
819 * Routes are added as below:
820 * pin mux -> pin (based on num_pins)
821 * cvt -> "Input sel control" -> pin_mux
822 *
823 * Total route elements:
824 * num_pins + (pin_muxes * num_cvt)
825 */
826static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
827{
828 struct snd_soc_dapm_widget *widgets;
829 struct snd_soc_dapm_route *route;
830 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
831 struct hdac_hdmi_priv *hdmi = edev->private_data;
832 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
833 char widget_name[NAME_SIZE];
834 struct hdac_hdmi_cvt *cvt;
835 struct hdac_hdmi_pin *pin;
836 int ret, i = 0, num_routes = 0;
837
838 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
839 return -EINVAL;
840
841 widgets = devm_kzalloc(dapm->dev,
842 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
843 GFP_KERNEL);
844
845 if (!widgets)
846 return -ENOMEM;
847
848 /* DAPM widgets to represent each converter widget */
849 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
850 sprintf(widget_name, "Converter %d", cvt->nid);
851 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
852 snd_soc_dapm_aif_in, &cvt->nid,
853 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
854 if (ret < 0)
855 return ret;
856 i++;
857 }
858
859 list_for_each_entry(pin, &hdmi->pin_list, head) {
860 sprintf(widget_name, "hif%d Output", pin->nid);
861 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
862 snd_soc_dapm_output, &pin->nid,
863 widget_name, NULL, NULL, 0);
864 if (ret < 0)
865 return ret;
866 i++;
867 }
868
869 /* DAPM widgets to represent the connection list to pin widget */
870 list_for_each_entry(pin, &hdmi->pin_list, head) {
871 sprintf(widget_name, "Pin %d Mux", pin->nid);
872 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
873 widget_name);
874 if (ret < 0)
875 return ret;
876 i++;
877
878 /* For cvt to pin_mux mapping */
879 num_routes += hdmi->num_cvt;
880
881 /* For pin_mux to pin mapping */
882 num_routes++;
883 }
884
885 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
886 GFP_KERNEL);
887 if (!route)
888 return -ENOMEM;
889
890 i = 0;
891 /* Add pin <- NULL <- mux route map */
892 list_for_each_entry(pin, &hdmi->pin_list, head) {
893 int sink_index = i + hdmi->num_cvt;
894 int src_index = sink_index + hdmi->num_pin;
895
896 hdac_hdmi_fill_route(&route[i],
897 widgets[sink_index].name, NULL,
898 widgets[src_index].name, NULL);
899 i++;
900
901 }
902
903 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
904
905 snd_soc_dapm_new_controls(dapm, widgets,
906 ((2 * hdmi->num_pin) + hdmi->num_cvt));
907
908 snd_soc_dapm_add_routes(dapm, route, num_routes);
909 snd_soc_dapm_new_widgets(dapm->card);
910
911 return 0;
912
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530913}
914
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530915static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530916{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530917 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530918 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530919 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530920 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530921
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530922 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530923 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530924
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530925 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
926 dai_map = &hdmi->dai_map[dai_id];
927 dai_map->dai_id = dai_id;
928 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530929
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530930 /* Enable transmission */
931 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
932 AC_VERB_SET_DIGI_CONVERT_1, 1);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530933
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530934 /* Category Code (CC) to zero */
935 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
936 AC_VERB_SET_DIGI_CONVERT_2, 0);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530937
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530938 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530939
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530940 if (dai_id == HDA_MAX_CVTS) {
941 dev_warn(&edev->hdac.dev,
942 "Max dais supported: %d\n", dai_id);
943 break;
944 }
945 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530946
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530947 return 0;
948}
949
950static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
951{
952 struct hdac_hdmi_priv *hdmi = edev->private_data;
953 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530954 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530955
956 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
957 if (!cvt)
958 return -ENOMEM;
959
960 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530961 sprintf(name, "cvt %d", cvt->nid);
962 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530963
964 list_add_tail(&cvt->head, &hdmi->cvt_list);
965 hdmi->num_cvt++;
966
967 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
968}
969
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530970static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
971{
972 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530973 struct hdac_hdmi_priv *hdmi = edev->private_data;
974 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530975 int val;
976
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530977 pin->repoll_count = repoll;
978
979 pm_runtime_get_sync(&edev->hdac.dev);
980 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
981 AC_VERB_GET_PIN_SENSE, 0);
982
983 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
984 val, pin->nid);
985
Jeeja KP4a3478d2016-02-12 07:46:06 +0530986
987 mutex_lock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530988 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
989 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
990
Jeeja KP4a3478d2016-02-12 07:46:06 +0530991 pcm = hdac_hdmi_get_pcm(edev, pin);
992
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530993 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
994
995 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
996 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530997
998 /*
999 * PCMs are not registered during device probe, so don't
1000 * report jack here. It will be done in usermode mux
1001 * control select.
1002 */
1003 if (pcm) {
1004 dev_dbg(&edev->hdac.dev,
1005 "jack report for pcm=%d\n", pcm->pcm_id);
1006
1007 snd_jack_report(pcm->jack, 0);
1008 }
1009
1010 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301011 goto put_hdac_device;
1012 }
1013
1014 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1015 /* TODO: use i915 component for reading ELD later */
1016 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1017 pin->eld.eld_buffer,
1018 &pin->eld.eld_size) == 0) {
1019
Jeeja KP4a3478d2016-02-12 07:46:06 +05301020 if (pcm) {
1021 dev_dbg(&edev->hdac.dev,
1022 "jack report for pcm=%d\n",
1023 pcm->pcm_id);
1024
1025 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1026 }
1027
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301028 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1029 pin->eld.eld_buffer, pin->eld.eld_size);
1030 } else {
1031 pin->eld.monitor_present = false;
1032 pin->eld.eld_valid = false;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301033
1034 if (pcm) {
1035 dev_dbg(&edev->hdac.dev,
1036 "jack report for pcm=%d\n",
1037 pcm->pcm_id);
1038
1039 snd_jack_report(pcm->jack, 0);
1040 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301041 }
1042 }
1043
Jeeja KP4a3478d2016-02-12 07:46:06 +05301044 mutex_unlock(&hdmi->pin_mutex);
1045
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301046 /*
1047 * Sometimes the pin_sense may present invalid monitor
1048 * present and eld_valid. If ELD data is not valid, loop few
1049 * more times to get correct pin sense and valid ELD.
1050 */
1051 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1052 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1053
1054put_hdac_device:
1055 pm_runtime_put_sync(&edev->hdac.dev);
1056}
1057
1058static void hdac_hdmi_repoll_eld(struct work_struct *work)
1059{
1060 struct hdac_hdmi_pin *pin =
1061 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1062
1063 /* picked from legacy HDA driver */
1064 if (pin->repoll_count++ > 6)
1065 pin->repoll_count = 0;
1066
1067 hdac_hdmi_present_sense(pin, pin->repoll_count);
1068}
1069
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301070static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1071{
1072 struct hdac_hdmi_priv *hdmi = edev->private_data;
1073 struct hdac_hdmi_pin *pin;
1074
1075 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1076 if (!pin)
1077 return -ENOMEM;
1078
1079 pin->nid = nid;
1080
1081 list_add_tail(&pin->head, &hdmi->pin_list);
1082 hdmi->num_pin++;
1083
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301084 pin->edev = edev;
1085 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1086
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301087 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301088}
1089
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301090#define INTEL_VENDOR_NID 0x08
1091#define INTEL_GET_VENDOR_VERB 0xf81
1092#define INTEL_SET_VENDOR_VERB 0x781
1093#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1094#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1095
1096static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1097{
1098 unsigned int vendor_param;
1099
1100 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1101 INTEL_GET_VENDOR_VERB, 0);
1102 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1103 return;
1104
1105 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1106 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1107 INTEL_SET_VENDOR_VERB, vendor_param);
1108 if (vendor_param == -1)
1109 return;
1110}
1111
1112static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1113{
1114 unsigned int vendor_param;
1115
1116 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1117 INTEL_GET_VENDOR_VERB, 0);
1118 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1119 return;
1120
1121 /* enable DP1.2 mode */
1122 vendor_param |= INTEL_EN_DP12;
1123 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1124 INTEL_SET_VENDOR_VERB, vendor_param);
1125 if (vendor_param == -1)
1126 return;
1127
1128}
1129
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301130static struct snd_soc_dai_ops hdmi_dai_ops = {
1131 .startup = hdac_hdmi_pcm_open,
1132 .shutdown = hdac_hdmi_pcm_close,
1133 .hw_params = hdac_hdmi_set_hw_params,
1134 .prepare = hdac_hdmi_playback_prepare,
1135 .hw_free = hdac_hdmi_playback_cleanup,
1136};
1137
1138/*
1139 * Each converter can support a stream independently. So a dai is created
1140 * based on the number of converter queried.
1141 */
1142static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1143 struct snd_soc_dai_driver **dais,
1144 struct hdac_hdmi_priv *hdmi, int num_dais)
1145{
1146 struct snd_soc_dai_driver *hdmi_dais;
1147 struct hdac_hdmi_cvt *cvt;
1148 char name[NAME_SIZE], dai_name[NAME_SIZE];
1149 int i = 0;
1150 u32 rates, bps;
1151 unsigned int rate_max = 384000, rate_min = 8000;
1152 u64 formats;
1153 int ret;
1154
1155 hdmi_dais = devm_kzalloc(&hdac->dev,
1156 (sizeof(*hdmi_dais) * num_dais),
1157 GFP_KERNEL);
1158 if (!hdmi_dais)
1159 return -ENOMEM;
1160
1161 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1162 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1163 &rates, &formats, &bps);
1164 if (ret)
1165 return ret;
1166
1167 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1168 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1169 dai_name, GFP_KERNEL);
1170
1171 if (!hdmi_dais[i].name)
1172 return -ENOMEM;
1173
1174 snprintf(name, sizeof(name), "hifi%d", i+1);
1175 hdmi_dais[i].playback.stream_name =
1176 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1177 if (!hdmi_dais[i].playback.stream_name)
1178 return -ENOMEM;
1179
1180 /*
1181 * Set caps based on capability queried from the converter.
1182 * It will be constrained runtime based on ELD queried.
1183 */
1184 hdmi_dais[i].playback.formats = formats;
1185 hdmi_dais[i].playback.rates = rates;
1186 hdmi_dais[i].playback.rate_max = rate_max;
1187 hdmi_dais[i].playback.rate_min = rate_min;
1188 hdmi_dais[i].playback.channels_min = 2;
1189 hdmi_dais[i].playback.channels_max = 2;
1190 hdmi_dais[i].ops = &hdmi_dai_ops;
1191
1192 i++;
1193 }
1194
1195 *dais = hdmi_dais;
1196
1197 return 0;
1198}
1199
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301200/*
1201 * Parse all nodes and store the cvt/pin nids in array
1202 * Add one time initialization for pin and cvt widgets
1203 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301204static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1205 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301206{
1207 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301208 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301209 struct hdac_device *hdac = &edev->hdac;
1210 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301211 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301212
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301213 hdac_hdmi_skl_enable_all_pins(hdac);
1214 hdac_hdmi_skl_enable_dp12(hdac);
1215
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301216 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301217 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301218 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1219 return -EINVAL;
1220 }
1221
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301222 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301223 hdac->start_nid = nid;
1224
1225 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1226 unsigned int caps;
1227 unsigned int type;
1228
1229 caps = get_wcaps(hdac, nid);
1230 type = get_wcaps_type(caps);
1231
1232 if (!(caps & AC_WCAP_DIGITAL))
1233 continue;
1234
1235 switch (type) {
1236
1237 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301238 ret = hdac_hdmi_add_cvt(edev, nid);
1239 if (ret < 0)
1240 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301241 break;
1242
1243 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301244 ret = hdac_hdmi_add_pin(edev, nid);
1245 if (ret < 0)
1246 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301247 break;
1248 }
1249 }
1250
1251 hdac->end_nid = nid;
1252
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301253 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301254 return -EIO;
1255
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301256 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1257 if (ret) {
1258 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1259 ret);
1260 return ret;
1261 }
1262
1263 *num_dais = hdmi->num_cvt;
1264
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301265 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301266}
1267
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301268static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1269{
1270 struct hdac_ext_device *edev = aptr;
1271 struct hdac_hdmi_priv *hdmi = edev->private_data;
1272 struct hdac_hdmi_pin *pin;
1273 struct snd_soc_codec *codec = edev->scodec;
1274
1275 /* Don't know how this mapping is derived */
1276 hda_nid_t pin_nid = port + 0x04;
1277
1278 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1279
1280 /*
1281 * skip notification during system suspend (but not in runtime PM);
1282 * the state will be updated at resume. Also since the ELD and
1283 * connection states are updated in anyway at the end of the resume,
1284 * we can skip it when received during PM process.
1285 */
1286 if (snd_power_get_state(codec->component.card->snd_card) !=
1287 SNDRV_CTL_POWER_D0)
1288 return;
1289
1290 if (atomic_read(&edev->hdac.in_pm))
1291 return;
1292
1293 list_for_each_entry(pin, &hdmi->pin_list, head) {
1294 if (pin->nid == pin_nid)
1295 hdac_hdmi_present_sense(pin, 1);
1296 }
1297}
1298
1299static struct i915_audio_component_audio_ops aops = {
1300 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1301};
1302
Jeeja KP4a3478d2016-02-12 07:46:06 +05301303int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1304{
1305 char jack_name[NAME_SIZE];
1306 struct snd_soc_codec *codec = dai->codec;
1307 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1308 struct snd_soc_dapm_context *dapm =
1309 snd_soc_component_get_dapm(&codec->component);
1310 struct hdac_hdmi_priv *hdmi = edev->private_data;
1311 struct hdac_hdmi_pcm *pcm;
1312
1313 /*
1314 * this is a new PCM device, create new pcm and
1315 * add to the pcm list
1316 */
1317 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1318 if (!pcm)
1319 return -ENOMEM;
1320 pcm->pcm_id = device;
1321 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1322
1323 list_add_tail(&pcm->head, &hdmi->pcm_list);
1324
1325 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1326
1327 return snd_jack_new(dapm->card->snd_card, jack_name,
1328 SND_JACK_AVOUT, &pcm->jack, true, false);
1329}
1330EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1331
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301332static int hdmi_codec_probe(struct snd_soc_codec *codec)
1333{
1334 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1335 struct hdac_hdmi_priv *hdmi = edev->private_data;
1336 struct snd_soc_dapm_context *dapm =
1337 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301338 struct hdac_hdmi_pin *pin;
1339 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301340
1341 edev->scodec = codec;
1342
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301343 ret = create_fill_widget_route_map(dapm);
1344 if (ret < 0)
1345 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301346
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301347 aops.audio_ptr = edev;
1348 ret = snd_hdac_i915_register_notifier(&aops);
1349 if (ret < 0) {
1350 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1351 ret);
1352 return ret;
1353 }
1354
1355 list_for_each_entry(pin, &hdmi->pin_list, head)
1356 hdac_hdmi_present_sense(pin, 1);
1357
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301358 /* Imp: Store the card pointer in hda_codec */
1359 edev->card = dapm->card->snd_card;
1360
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301361 /*
1362 * hdac_device core already sets the state to active and calls
1363 * get_noresume. So enable runtime and set the device to suspend.
1364 */
1365 pm_runtime_enable(&edev->hdac.dev);
1366 pm_runtime_put(&edev->hdac.dev);
1367 pm_runtime_suspend(&edev->hdac.dev);
1368
1369 return 0;
1370}
1371
1372static int hdmi_codec_remove(struct snd_soc_codec *codec)
1373{
1374 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1375
1376 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301377 return 0;
1378}
1379
1380static struct snd_soc_codec_driver hdmi_hda_codec = {
1381 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301382 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301383 .idle_bias_off = true,
1384};
1385
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301386static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1387{
1388 struct hdac_device *codec = &edev->hdac;
1389 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301390 struct snd_soc_dai_driver *hdmi_dais = NULL;
1391 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301392 int ret = 0;
1393
1394 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1395 if (hdmi_priv == NULL)
1396 return -ENOMEM;
1397
1398 edev->private_data = hdmi_priv;
1399
1400 dev_set_drvdata(&codec->dev, edev);
1401
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301402 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1403 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301404 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1405 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301406
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301407 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1408 if (ret < 0) {
1409 dev_err(&codec->dev,
1410 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301411 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301412 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301413
1414 /* ASoC specific initialization */
1415 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301416 hdmi_dais, num_dais);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301417}
1418
1419static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1420{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301421 struct hdac_hdmi_priv *hdmi = edev->private_data;
1422 struct hdac_hdmi_pin *pin, *pin_next;
1423 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301424 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301425
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301426 snd_soc_unregister_codec(&edev->hdac.dev);
1427
Jeeja KP4a3478d2016-02-12 07:46:06 +05301428 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1429 pcm->cvt = NULL;
1430 pcm->pin = NULL;
1431 list_del(&pcm->head);
1432 kfree(pcm);
1433 }
1434
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301435 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1436 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301437 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301438 kfree(cvt);
1439 }
1440
1441 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1442 list_del(&pin->head);
1443 kfree(pin);
1444 }
1445
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301446 return 0;
1447}
1448
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301449#ifdef CONFIG_PM
1450static int hdac_hdmi_runtime_suspend(struct device *dev)
1451{
1452 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1453 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301454 struct hdac_bus *bus = hdac->bus;
1455 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301456
1457 dev_dbg(dev, "Enter: %s\n", __func__);
1458
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301459 /* controller may not have been initialized for the first time */
1460 if (!bus)
1461 return 0;
1462
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301463 /* Power down afg */
1464 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1465 snd_hdac_codec_write(hdac, hdac->afg, 0,
1466 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1467
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301468 err = snd_hdac_display_power(bus, false);
1469 if (err < 0) {
1470 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1471 return err;
1472 }
1473
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301474 return 0;
1475}
1476
1477static int hdac_hdmi_runtime_resume(struct device *dev)
1478{
1479 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1480 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301481 struct hdac_bus *bus = hdac->bus;
1482 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301483
1484 dev_dbg(dev, "Enter: %s\n", __func__);
1485
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301486 /* controller may not have been initialized for the first time */
1487 if (!bus)
1488 return 0;
1489
1490 err = snd_hdac_display_power(bus, true);
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 /* Power up afg */
1497 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1498 snd_hdac_codec_write(hdac, hdac->afg, 0,
1499 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1500
1501 return 0;
1502}
1503#else
1504#define hdac_hdmi_runtime_suspend NULL
1505#define hdac_hdmi_runtime_resume NULL
1506#endif
1507
1508static const struct dev_pm_ops hdac_hdmi_pm = {
1509 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1510};
1511
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301512static const struct hda_device_id hdmi_list[] = {
1513 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1514 {}
1515};
1516
1517MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1518
1519static struct hdac_ext_driver hdmi_driver = {
1520 . hdac = {
1521 .driver = {
1522 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301523 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301524 },
1525 .id_table = hdmi_list,
1526 },
1527 .probe = hdac_hdmi_dev_probe,
1528 .remove = hdac_hdmi_dev_remove,
1529};
1530
1531static int __init hdmi_init(void)
1532{
1533 return snd_hda_ext_driver_register(&hdmi_driver);
1534}
1535
1536static void __exit hdmi_exit(void)
1537{
1538 snd_hda_ext_driver_unregister(&hdmi_driver);
1539}
1540
1541module_init(hdmi_init);
1542module_exit(hdmi_exit);
1543
1544MODULE_LICENSE("GPL v2");
1545MODULE_DESCRIPTION("HDMI HD codec");
1546MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1547MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");