blob: 36f12004ae9c86246a74bc2547ce7a35a338ef2c [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
408 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Sudip Mukherjee8d33ab22015-11-23 17:45:13 +0530409 if (!dd)
410 return -ENOMEM;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530411 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
412 params_channels(hparams), params_format(hparams),
413 24, 0);
414
415 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
416
417 return 0;
418}
419
420static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
421 struct snd_soc_dai *dai)
422{
423 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
424 struct hdac_ext_dma_params *dd;
425 struct hdac_hdmi_priv *hdmi = edev->private_data;
426 struct hdac_hdmi_dai_pin_map *dai_map;
427
428 dai_map = &hdmi->dai_map[dai->id];
429
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530430 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530431 AC_VERB_SET_CHANNEL_STREAMID, 0);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530432 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530433 AC_VERB_SET_STREAM_FORMAT, 0);
434
435 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
436 snd_soc_dai_set_dma_data(dai, substream, NULL);
437
438 kfree(dd);
439
440 return 0;
441}
442
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530443static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
444 struct hdac_hdmi_dai_pin_map *dai_map)
445{
446 int mux_idx;
447 struct hdac_hdmi_pin *pin = dai_map->pin;
448
449 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
450 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
451 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
452 AC_VERB_SET_CONNECT_SEL, mux_idx);
453 break;
454 }
455 }
456
457 if (mux_idx == pin->num_mux_nids)
458 return -EIO;
459
460 /* Enable out path for this pin widget */
461 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
462 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
463
464 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
465
466 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
467 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
468
469 return 0;
470}
471
472static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
473 struct hdac_hdmi_pin *pin)
474{
475 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
476 dev_warn(&hdac->hdac.dev,
477 "HDMI: pin %d wcaps %#x does not support connection list\n",
478 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
479 return -EINVAL;
480 }
481
482 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
483 pin->mux_nids, HDA_MAX_CONNECTIONS);
484 if (pin->num_mux_nids == 0)
485 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
486 pin->nid);
487
488 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
489 pin->num_mux_nids, pin->nid);
490
491 return pin->num_mux_nids;
492}
493
494/*
495 * Query pcm list and return pin widget to which stream is routed.
496 *
497 * Also query connection list of the pin, to validate the cvt to pin map.
498 *
499 * Same stream rendering to multiple pins simultaneously can be done
500 * possibly, but not supported for now in driver. So return the first pin
501 * connected.
502 */
503static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
504 struct hdac_ext_device *edev,
505 struct hdac_hdmi_priv *hdmi,
506 struct hdac_hdmi_cvt *cvt)
507{
508 struct hdac_hdmi_pcm *pcm;
509 struct hdac_hdmi_pin *pin = NULL;
510 int ret, i;
511
512 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
513 if (pcm->cvt == cvt) {
514 pin = pcm->pin;
515 break;
516 }
517 }
518
519 if (pin) {
520 ret = hdac_hdmi_query_pin_connlist(edev, pin);
521 if (ret < 0)
522 return NULL;
523
524 for (i = 0; i < pin->num_mux_nids; i++) {
525 if (pin->mux_nids[i] == cvt->nid)
526 return pin;
527 }
528 }
529
530 return NULL;
531}
532
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530533static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
534 struct snd_soc_dai *dai)
535{
536 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
537 struct hdac_hdmi_priv *hdmi = hdac->private_data;
538 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530539 struct hdac_hdmi_cvt *cvt;
540 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530541 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530542
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530543 dai_map = &hdmi->dai_map[dai->id];
544
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530545 cvt = dai_map->cvt;
546 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
547 if (!pin)
548 return -EIO;
549
550 if ((!pin->eld.monitor_present) ||
551 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530552
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530553 dev_err(&hdac->hdac.dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530554 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
555 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530556
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530557 return -ENODEV;
558 }
559
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530560 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530561
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530562 ret = hdac_hdmi_enable_pin(hdac, dai_map);
563 if (ret < 0)
564 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530565
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530566 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530567 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530568 if (ret < 0)
569 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530570
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530571 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530572 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530573}
574
575static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
576 struct snd_soc_dai *dai)
577{
578 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
579 struct hdac_hdmi_priv *hdmi = hdac->private_data;
580 struct hdac_hdmi_dai_pin_map *dai_map;
581
582 dai_map = &hdmi->dai_map[dai->id];
583
584 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
585
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530586 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530587 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530588
589 dai_map->pin = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530590}
591
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530592static int
593hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
594{
595 int err;
596
597 /* Only stereo supported as of now */
598 cvt->params.channels_min = cvt->params.channels_max = 2;
599
600 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
601 &cvt->params.rates,
602 &cvt->params.formats,
603 &cvt->params.maxbps);
604 if (err < 0)
605 dev_err(&hdac->dev,
606 "Failed to query pcm params for nid %d: %d\n",
607 cvt->nid, err);
608
609 return err;
610}
611
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530612static int hdac_hdmi_fill_widget_info(struct device *dev,
613 struct snd_soc_dapm_widget *w,
614 enum snd_soc_dapm_type id, void *priv,
615 const char *wname, const char *stream,
616 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530617{
618 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530619 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
620 if (!w->name)
621 return -ENOMEM;
622
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530623 w->sname = stream;
624 w->reg = SND_SOC_NOPM;
625 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530626 w->kcontrol_news = wc;
627 w->num_kcontrols = numkc;
628 w->priv = priv;
629
630 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530631}
632
633static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530634 const char *sink, const char *control, const char *src,
635 int (*handler)(struct snd_soc_dapm_widget *src,
636 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530637{
638 route->sink = sink;
639 route->source = src;
640 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530641 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530642}
643
Jeeja KP4a3478d2016-02-12 07:46:06 +0530644static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
645 struct hdac_hdmi_pin *pin)
646{
647 struct hdac_hdmi_priv *hdmi = edev->private_data;
648 struct hdac_hdmi_pcm *pcm = NULL;
649
650 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
651 if (pcm->pin == pin)
652 return pcm;
653 }
654
655 return NULL;
656}
657
658/*
659 * Based on user selection, map the PINs with the PCMs.
660 */
661static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
662 struct snd_ctl_elem_value *ucontrol)
663{
664 int ret;
665 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
666 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
667 struct snd_soc_dapm_context *dapm = w->dapm;
668 struct hdac_hdmi_pin *pin = w->priv;
669 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
670 struct hdac_hdmi_priv *hdmi = edev->private_data;
671 struct hdac_hdmi_pcm *pcm = NULL;
672 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
673
674 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
675 if (ret < 0)
676 return ret;
677
678 mutex_lock(&hdmi->pin_mutex);
679 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
680 if (pcm->pin == pin)
681 pcm->pin = NULL;
682
683 /*
684 * Jack status is not reported during device probe as the
685 * PCMs are not registered by then. So report it here.
686 */
687 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
688 pcm->pin = pin;
689 if (pin->eld.monitor_present && pin->eld.eld_valid) {
690 dev_dbg(&edev->hdac.dev,
691 "jack report for pcm=%d\n",
692 pcm->pcm_id);
693
694 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
695 }
696 mutex_unlock(&hdmi->pin_mutex);
697 return ret;
698 }
699 }
700 mutex_unlock(&hdmi->pin_mutex);
701
702 return ret;
703}
704
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530705/*
706 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
707 * the display driver seem to be programming the connection list for the pin
708 * widget runtime.
709 *
710 * So programming all the possible inputs for the mux, the user has to take
711 * care of selecting the right one and leaving all other inputs selected to
712 * "NONE"
713 */
714static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
715 struct hdac_hdmi_pin *pin,
716 struct snd_soc_dapm_widget *widget,
717 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530718{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530719 struct hdac_hdmi_priv *hdmi = edev->private_data;
720 struct snd_kcontrol_new *kc;
721 struct hdac_hdmi_cvt *cvt;
722 struct soc_enum *se;
723 char kc_name[NAME_SIZE];
724 char mux_items[NAME_SIZE];
725 /* To hold inputs to the Pin mux */
726 char *items[HDA_MAX_CONNECTIONS];
727 int i = 0;
728 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530729
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530730 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
731 if (!kc)
732 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530733
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530734 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
735 if (!se)
736 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530737
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530738 sprintf(kc_name, "Pin %d Input", pin->nid);
739 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
740 if (!kc->name)
741 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530742
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530743 kc->private_value = (long)se;
744 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
745 kc->access = 0;
746 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530747 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530748 kc->get = snd_soc_dapm_get_enum_double;
749
750 se->reg = SND_SOC_NOPM;
751
752 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
753 se->items = num_items;
754 se->mask = roundup_pow_of_two(se->items) - 1;
755
756 sprintf(mux_items, "NONE");
757 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
758 if (!items[i])
759 return -ENOMEM;
760
761 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
762 i++;
763 sprintf(mux_items, "cvt %d", cvt->nid);
764 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
765 if (!items[i])
766 return -ENOMEM;
767 }
768
769 se->texts = devm_kmemdup(&edev->hdac.dev, items,
770 (num_items * sizeof(char *)), GFP_KERNEL);
771 if (!se->texts)
772 return -ENOMEM;
773
774 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530775 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530776}
777
778/* Add cvt <- input <- mux route map */
779static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
780 struct snd_soc_dapm_widget *widgets,
781 struct snd_soc_dapm_route *route, int rindex)
782{
783 struct hdac_hdmi_priv *hdmi = edev->private_data;
784 const struct snd_kcontrol_new *kc;
785 struct soc_enum *se;
786 int mux_index = hdmi->num_cvt + hdmi->num_pin;
787 int i, j;
788
789 for (i = 0; i < hdmi->num_pin; i++) {
790 kc = widgets[mux_index].kcontrol_news;
791 se = (struct soc_enum *)kc->private_value;
792 for (j = 0; j < hdmi->num_cvt; j++) {
793 hdac_hdmi_fill_route(&route[rindex],
794 widgets[mux_index].name,
795 se->texts[j + 1],
796 widgets[j].name, NULL);
797
798 rindex++;
799 }
800
801 mux_index++;
802 }
803}
804
805/*
806 * Widgets are added in the below sequence
807 * Converter widgets for num converters enumerated
808 * Pin widgets for num pins enumerated
809 * Pin mux widgets to represent connenction list of pin widget
810 *
811 * Total widgets elements = num_cvt + num_pin + num_pin;
812 *
813 * Routes are added as below:
814 * pin mux -> pin (based on num_pins)
815 * cvt -> "Input sel control" -> pin_mux
816 *
817 * Total route elements:
818 * num_pins + (pin_muxes * num_cvt)
819 */
820static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
821{
822 struct snd_soc_dapm_widget *widgets;
823 struct snd_soc_dapm_route *route;
824 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
825 struct hdac_hdmi_priv *hdmi = edev->private_data;
826 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
827 char widget_name[NAME_SIZE];
828 struct hdac_hdmi_cvt *cvt;
829 struct hdac_hdmi_pin *pin;
830 int ret, i = 0, num_routes = 0;
831
832 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
833 return -EINVAL;
834
835 widgets = devm_kzalloc(dapm->dev,
836 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
837 GFP_KERNEL);
838
839 if (!widgets)
840 return -ENOMEM;
841
842 /* DAPM widgets to represent each converter widget */
843 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
844 sprintf(widget_name, "Converter %d", cvt->nid);
845 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
846 snd_soc_dapm_aif_in, &cvt->nid,
847 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
848 if (ret < 0)
849 return ret;
850 i++;
851 }
852
853 list_for_each_entry(pin, &hdmi->pin_list, head) {
854 sprintf(widget_name, "hif%d Output", pin->nid);
855 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
856 snd_soc_dapm_output, &pin->nid,
857 widget_name, NULL, NULL, 0);
858 if (ret < 0)
859 return ret;
860 i++;
861 }
862
863 /* DAPM widgets to represent the connection list to pin widget */
864 list_for_each_entry(pin, &hdmi->pin_list, head) {
865 sprintf(widget_name, "Pin %d Mux", pin->nid);
866 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
867 widget_name);
868 if (ret < 0)
869 return ret;
870 i++;
871
872 /* For cvt to pin_mux mapping */
873 num_routes += hdmi->num_cvt;
874
875 /* For pin_mux to pin mapping */
876 num_routes++;
877 }
878
879 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
880 GFP_KERNEL);
881 if (!route)
882 return -ENOMEM;
883
884 i = 0;
885 /* Add pin <- NULL <- mux route map */
886 list_for_each_entry(pin, &hdmi->pin_list, head) {
887 int sink_index = i + hdmi->num_cvt;
888 int src_index = sink_index + hdmi->num_pin;
889
890 hdac_hdmi_fill_route(&route[i],
891 widgets[sink_index].name, NULL,
892 widgets[src_index].name, NULL);
893 i++;
894
895 }
896
897 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
898
899 snd_soc_dapm_new_controls(dapm, widgets,
900 ((2 * hdmi->num_pin) + hdmi->num_cvt));
901
902 snd_soc_dapm_add_routes(dapm, route, num_routes);
903 snd_soc_dapm_new_widgets(dapm->card);
904
905 return 0;
906
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530907}
908
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530909static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530910{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530911 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530912 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530913 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530914 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530915
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530916 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530917 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530918
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530919 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
920 dai_map = &hdmi->dai_map[dai_id];
921 dai_map->dai_id = dai_id;
922 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530923
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530924 /* Enable transmission */
925 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
926 AC_VERB_SET_DIGI_CONVERT_1, 1);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530927
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530928 /* Category Code (CC) to zero */
929 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
930 AC_VERB_SET_DIGI_CONVERT_2, 0);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530931
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530932 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530933
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530934 if (dai_id == HDA_MAX_CVTS) {
935 dev_warn(&edev->hdac.dev,
936 "Max dais supported: %d\n", dai_id);
937 break;
938 }
939 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530940
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530941 return 0;
942}
943
944static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
945{
946 struct hdac_hdmi_priv *hdmi = edev->private_data;
947 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530948 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530949
950 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
951 if (!cvt)
952 return -ENOMEM;
953
954 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530955 sprintf(name, "cvt %d", cvt->nid);
956 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530957
958 list_add_tail(&cvt->head, &hdmi->cvt_list);
959 hdmi->num_cvt++;
960
961 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
962}
963
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530964static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
965{
966 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530967 struct hdac_hdmi_priv *hdmi = edev->private_data;
968 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530969 int val;
970
971 if (!edev)
972 return;
973
974 pin->repoll_count = repoll;
975
976 pm_runtime_get_sync(&edev->hdac.dev);
977 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
978 AC_VERB_GET_PIN_SENSE, 0);
979
980 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
981 val, pin->nid);
982
Jeeja KP4a3478d2016-02-12 07:46:06 +0530983
984 mutex_lock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530985 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
986 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
987
Jeeja KP4a3478d2016-02-12 07:46:06 +0530988 pcm = hdac_hdmi_get_pcm(edev, pin);
989
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530990 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
991
992 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
993 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530994
995 /*
996 * PCMs are not registered during device probe, so don't
997 * report jack here. It will be done in usermode mux
998 * control select.
999 */
1000 if (pcm) {
1001 dev_dbg(&edev->hdac.dev,
1002 "jack report for pcm=%d\n", pcm->pcm_id);
1003
1004 snd_jack_report(pcm->jack, 0);
1005 }
1006
1007 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301008 goto put_hdac_device;
1009 }
1010
1011 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1012 /* TODO: use i915 component for reading ELD later */
1013 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1014 pin->eld.eld_buffer,
1015 &pin->eld.eld_size) == 0) {
1016
Jeeja KP4a3478d2016-02-12 07:46:06 +05301017 if (pcm) {
1018 dev_dbg(&edev->hdac.dev,
1019 "jack report for pcm=%d\n",
1020 pcm->pcm_id);
1021
1022 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1023 }
1024
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301025 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1026 pin->eld.eld_buffer, pin->eld.eld_size);
1027 } else {
1028 pin->eld.monitor_present = false;
1029 pin->eld.eld_valid = false;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301030
1031 if (pcm) {
1032 dev_dbg(&edev->hdac.dev,
1033 "jack report for pcm=%d\n",
1034 pcm->pcm_id);
1035
1036 snd_jack_report(pcm->jack, 0);
1037 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301038 }
1039 }
1040
Jeeja KP4a3478d2016-02-12 07:46:06 +05301041 mutex_unlock(&hdmi->pin_mutex);
1042
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301043 /*
1044 * Sometimes the pin_sense may present invalid monitor
1045 * present and eld_valid. If ELD data is not valid, loop few
1046 * more times to get correct pin sense and valid ELD.
1047 */
1048 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1049 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1050
1051put_hdac_device:
1052 pm_runtime_put_sync(&edev->hdac.dev);
1053}
1054
1055static void hdac_hdmi_repoll_eld(struct work_struct *work)
1056{
1057 struct hdac_hdmi_pin *pin =
1058 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1059
1060 /* picked from legacy HDA driver */
1061 if (pin->repoll_count++ > 6)
1062 pin->repoll_count = 0;
1063
1064 hdac_hdmi_present_sense(pin, pin->repoll_count);
1065}
1066
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301067static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1068{
1069 struct hdac_hdmi_priv *hdmi = edev->private_data;
1070 struct hdac_hdmi_pin *pin;
1071
1072 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1073 if (!pin)
1074 return -ENOMEM;
1075
1076 pin->nid = nid;
1077
1078 list_add_tail(&pin->head, &hdmi->pin_list);
1079 hdmi->num_pin++;
1080
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301081 pin->edev = edev;
1082 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1083
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301084 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301085}
1086
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301087#define INTEL_VENDOR_NID 0x08
1088#define INTEL_GET_VENDOR_VERB 0xf81
1089#define INTEL_SET_VENDOR_VERB 0x781
1090#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1091#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1092
1093static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1094{
1095 unsigned int vendor_param;
1096
1097 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1098 INTEL_GET_VENDOR_VERB, 0);
1099 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1100 return;
1101
1102 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1103 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1104 INTEL_SET_VENDOR_VERB, vendor_param);
1105 if (vendor_param == -1)
1106 return;
1107}
1108
1109static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1110{
1111 unsigned int vendor_param;
1112
1113 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1114 INTEL_GET_VENDOR_VERB, 0);
1115 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1116 return;
1117
1118 /* enable DP1.2 mode */
1119 vendor_param |= INTEL_EN_DP12;
1120 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1121 INTEL_SET_VENDOR_VERB, vendor_param);
1122 if (vendor_param == -1)
1123 return;
1124
1125}
1126
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301127static struct snd_soc_dai_ops hdmi_dai_ops = {
1128 .startup = hdac_hdmi_pcm_open,
1129 .shutdown = hdac_hdmi_pcm_close,
1130 .hw_params = hdac_hdmi_set_hw_params,
1131 .prepare = hdac_hdmi_playback_prepare,
1132 .hw_free = hdac_hdmi_playback_cleanup,
1133};
1134
1135/*
1136 * Each converter can support a stream independently. So a dai is created
1137 * based on the number of converter queried.
1138 */
1139static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1140 struct snd_soc_dai_driver **dais,
1141 struct hdac_hdmi_priv *hdmi, int num_dais)
1142{
1143 struct snd_soc_dai_driver *hdmi_dais;
1144 struct hdac_hdmi_cvt *cvt;
1145 char name[NAME_SIZE], dai_name[NAME_SIZE];
1146 int i = 0;
1147 u32 rates, bps;
1148 unsigned int rate_max = 384000, rate_min = 8000;
1149 u64 formats;
1150 int ret;
1151
1152 hdmi_dais = devm_kzalloc(&hdac->dev,
1153 (sizeof(*hdmi_dais) * num_dais),
1154 GFP_KERNEL);
1155 if (!hdmi_dais)
1156 return -ENOMEM;
1157
1158 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1159 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1160 &rates, &formats, &bps);
1161 if (ret)
1162 return ret;
1163
1164 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1165 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1166 dai_name, GFP_KERNEL);
1167
1168 if (!hdmi_dais[i].name)
1169 return -ENOMEM;
1170
1171 snprintf(name, sizeof(name), "hifi%d", i+1);
1172 hdmi_dais[i].playback.stream_name =
1173 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1174 if (!hdmi_dais[i].playback.stream_name)
1175 return -ENOMEM;
1176
1177 /*
1178 * Set caps based on capability queried from the converter.
1179 * It will be constrained runtime based on ELD queried.
1180 */
1181 hdmi_dais[i].playback.formats = formats;
1182 hdmi_dais[i].playback.rates = rates;
1183 hdmi_dais[i].playback.rate_max = rate_max;
1184 hdmi_dais[i].playback.rate_min = rate_min;
1185 hdmi_dais[i].playback.channels_min = 2;
1186 hdmi_dais[i].playback.channels_max = 2;
1187 hdmi_dais[i].ops = &hdmi_dai_ops;
1188
1189 i++;
1190 }
1191
1192 *dais = hdmi_dais;
1193
1194 return 0;
1195}
1196
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301197/*
1198 * Parse all nodes and store the cvt/pin nids in array
1199 * Add one time initialization for pin and cvt widgets
1200 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301201static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1202 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301203{
1204 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301205 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301206 struct hdac_device *hdac = &edev->hdac;
1207 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301208 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301209
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301210 hdac_hdmi_skl_enable_all_pins(hdac);
1211 hdac_hdmi_skl_enable_dp12(hdac);
1212
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301213 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301214 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301215 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1216 return -EINVAL;
1217 }
1218
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301219 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301220 hdac->start_nid = nid;
1221
1222 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1223 unsigned int caps;
1224 unsigned int type;
1225
1226 caps = get_wcaps(hdac, nid);
1227 type = get_wcaps_type(caps);
1228
1229 if (!(caps & AC_WCAP_DIGITAL))
1230 continue;
1231
1232 switch (type) {
1233
1234 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301235 ret = hdac_hdmi_add_cvt(edev, nid);
1236 if (ret < 0)
1237 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301238 break;
1239
1240 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301241 ret = hdac_hdmi_add_pin(edev, nid);
1242 if (ret < 0)
1243 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301244 break;
1245 }
1246 }
1247
1248 hdac->end_nid = nid;
1249
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301250 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301251 return -EIO;
1252
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301253 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1254 if (ret) {
1255 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1256 ret);
1257 return ret;
1258 }
1259
1260 *num_dais = hdmi->num_cvt;
1261
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301262 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301263}
1264
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301265static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1266{
1267 struct hdac_ext_device *edev = aptr;
1268 struct hdac_hdmi_priv *hdmi = edev->private_data;
1269 struct hdac_hdmi_pin *pin;
1270 struct snd_soc_codec *codec = edev->scodec;
1271
1272 /* Don't know how this mapping is derived */
1273 hda_nid_t pin_nid = port + 0x04;
1274
1275 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1276
1277 /*
1278 * skip notification during system suspend (but not in runtime PM);
1279 * the state will be updated at resume. Also since the ELD and
1280 * connection states are updated in anyway at the end of the resume,
1281 * we can skip it when received during PM process.
1282 */
1283 if (snd_power_get_state(codec->component.card->snd_card) !=
1284 SNDRV_CTL_POWER_D0)
1285 return;
1286
1287 if (atomic_read(&edev->hdac.in_pm))
1288 return;
1289
1290 list_for_each_entry(pin, &hdmi->pin_list, head) {
1291 if (pin->nid == pin_nid)
1292 hdac_hdmi_present_sense(pin, 1);
1293 }
1294}
1295
1296static struct i915_audio_component_audio_ops aops = {
1297 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1298};
1299
Jeeja KP4a3478d2016-02-12 07:46:06 +05301300int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1301{
1302 char jack_name[NAME_SIZE];
1303 struct snd_soc_codec *codec = dai->codec;
1304 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1305 struct snd_soc_dapm_context *dapm =
1306 snd_soc_component_get_dapm(&codec->component);
1307 struct hdac_hdmi_priv *hdmi = edev->private_data;
1308 struct hdac_hdmi_pcm *pcm;
1309
1310 /*
1311 * this is a new PCM device, create new pcm and
1312 * add to the pcm list
1313 */
1314 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1315 if (!pcm)
1316 return -ENOMEM;
1317 pcm->pcm_id = device;
1318 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1319
1320 list_add_tail(&pcm->head, &hdmi->pcm_list);
1321
1322 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1323
1324 return snd_jack_new(dapm->card->snd_card, jack_name,
1325 SND_JACK_AVOUT, &pcm->jack, true, false);
1326}
1327EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1328
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301329static int hdmi_codec_probe(struct snd_soc_codec *codec)
1330{
1331 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1332 struct hdac_hdmi_priv *hdmi = edev->private_data;
1333 struct snd_soc_dapm_context *dapm =
1334 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301335 struct hdac_hdmi_pin *pin;
1336 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301337
1338 edev->scodec = codec;
1339
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301340 ret = create_fill_widget_route_map(dapm);
1341 if (ret < 0)
1342 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301343
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301344 aops.audio_ptr = edev;
1345 ret = snd_hdac_i915_register_notifier(&aops);
1346 if (ret < 0) {
1347 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1348 ret);
1349 return ret;
1350 }
1351
1352 list_for_each_entry(pin, &hdmi->pin_list, head)
1353 hdac_hdmi_present_sense(pin, 1);
1354
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301355 /* Imp: Store the card pointer in hda_codec */
1356 edev->card = dapm->card->snd_card;
1357
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301358 /*
1359 * hdac_device core already sets the state to active and calls
1360 * get_noresume. So enable runtime and set the device to suspend.
1361 */
1362 pm_runtime_enable(&edev->hdac.dev);
1363 pm_runtime_put(&edev->hdac.dev);
1364 pm_runtime_suspend(&edev->hdac.dev);
1365
1366 return 0;
1367}
1368
1369static int hdmi_codec_remove(struct snd_soc_codec *codec)
1370{
1371 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1372
1373 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301374 return 0;
1375}
1376
1377static struct snd_soc_codec_driver hdmi_hda_codec = {
1378 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301379 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301380 .idle_bias_off = true,
1381};
1382
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301383static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1384{
1385 struct hdac_device *codec = &edev->hdac;
1386 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301387 struct snd_soc_dai_driver *hdmi_dais = NULL;
1388 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301389 int ret = 0;
1390
1391 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1392 if (hdmi_priv == NULL)
1393 return -ENOMEM;
1394
1395 edev->private_data = hdmi_priv;
1396
1397 dev_set_drvdata(&codec->dev, edev);
1398
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301399 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1400 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301401 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1402 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301403
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301404 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1405 if (ret < 0) {
1406 dev_err(&codec->dev,
1407 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301408 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301409 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301410
1411 /* ASoC specific initialization */
1412 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301413 hdmi_dais, num_dais);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301414}
1415
1416static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1417{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301418 struct hdac_hdmi_priv *hdmi = edev->private_data;
1419 struct hdac_hdmi_pin *pin, *pin_next;
1420 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301421 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301422
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301423 snd_soc_unregister_codec(&edev->hdac.dev);
1424
Jeeja KP4a3478d2016-02-12 07:46:06 +05301425 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1426 pcm->cvt = NULL;
1427 pcm->pin = NULL;
1428 list_del(&pcm->head);
1429 kfree(pcm);
1430 }
1431
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301432 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1433 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301434 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301435 kfree(cvt);
1436 }
1437
1438 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1439 list_del(&pin->head);
1440 kfree(pin);
1441 }
1442
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301443 return 0;
1444}
1445
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301446#ifdef CONFIG_PM
1447static int hdac_hdmi_runtime_suspend(struct device *dev)
1448{
1449 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1450 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301451 struct hdac_bus *bus = hdac->bus;
1452 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301453
1454 dev_dbg(dev, "Enter: %s\n", __func__);
1455
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301456 /* controller may not have been initialized for the first time */
1457 if (!bus)
1458 return 0;
1459
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301460 /* Power down afg */
1461 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1462 snd_hdac_codec_write(hdac, hdac->afg, 0,
1463 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1464
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301465 err = snd_hdac_display_power(bus, false);
1466 if (err < 0) {
1467 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1468 return err;
1469 }
1470
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301471 return 0;
1472}
1473
1474static int hdac_hdmi_runtime_resume(struct device *dev)
1475{
1476 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1477 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301478 struct hdac_bus *bus = hdac->bus;
1479 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301480
1481 dev_dbg(dev, "Enter: %s\n", __func__);
1482
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301483 /* controller may not have been initialized for the first time */
1484 if (!bus)
1485 return 0;
1486
1487 err = snd_hdac_display_power(bus, true);
1488 if (err < 0) {
1489 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1490 return err;
1491 }
1492
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301493 /* Power up afg */
1494 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1495 snd_hdac_codec_write(hdac, hdac->afg, 0,
1496 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1497
1498 return 0;
1499}
1500#else
1501#define hdac_hdmi_runtime_suspend NULL
1502#define hdac_hdmi_runtime_resume NULL
1503#endif
1504
1505static const struct dev_pm_ops hdac_hdmi_pm = {
1506 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1507};
1508
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301509static const struct hda_device_id hdmi_list[] = {
1510 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1511 {}
1512};
1513
1514MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1515
1516static struct hdac_ext_driver hdmi_driver = {
1517 . hdac = {
1518 .driver = {
1519 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301520 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301521 },
1522 .id_table = hdmi_list,
1523 },
1524 .probe = hdac_hdmi_dev_probe,
1525 .remove = hdac_hdmi_dev_remove,
1526};
1527
1528static int __init hdmi_init(void)
1529{
1530 return snd_hda_ext_driver_register(&hdmi_driver);
1531}
1532
1533static void __exit hdmi_exit(void)
1534{
1535 snd_hda_ext_driver_unregister(&hdmi_driver);
1536}
1537
1538module_init(hdmi_init);
1539module_exit(hdmi_exit);
1540
1541MODULE_LICENSE("GPL v2");
1542MODULE_DESCRIPTION("HDMI HD codec");
1543MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1544MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");