Merge branch 'topic/drm' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[deliverable/linux.git] / sound / soc / codecs / hdac_hdmi.c
CommitLineData
18382ead
SP
1/*
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>
a657f1d0 24#include <linux/hdmi.h>
2428bca3 25#include <drm/drm_edid.h>
18382ead 26#include <sound/pcm_params.h>
4a3478de 27#include <sound/jack.h>
18382ead
SP
28#include <sound/soc.h>
29#include <sound/hdaudio_ext.h>
07f083ab 30#include <sound/hda_i915.h>
2428bca3 31#include <sound/pcm_drm_eld.h>
18382ead 32#include "../../hda/local.h"
4a3478de 33#include "hdac_hdmi.h"
18382ead 34
17a42c45
SP
35#define NAME_SIZE 32
36
b0362adb
SP
37#define AMP_OUT_MUTE 0xb080
38#define AMP_OUT_UNMUTE 0xb000
18382ead 39#define PIN_OUT (AC_PINCTL_OUT_EN)
b0362adb 40
18382ead
SP
41#define HDA_MAX_CONNECTIONS 32
42
b8a54545
SP
43#define ELD_MAX_SIZE 256
44#define ELD_FIXED_BYTES 20
45
18382ead
SP
46struct hdac_hdmi_cvt_params {
47 unsigned int channels_min;
48 unsigned int channels_max;
49 u32 rates;
50 u64 formats;
51 unsigned int maxbps;
52};
53
54struct hdac_hdmi_cvt {
15b91447 55 struct list_head head;
18382ead 56 hda_nid_t nid;
4a3478de 57 const char *name;
18382ead
SP
58 struct hdac_hdmi_cvt_params params;
59};
60
b8a54545
SP
61struct hdac_hdmi_eld {
62 bool monitor_present;
63 bool eld_valid;
64 int eld_size;
65 char eld_buffer[ELD_MAX_SIZE];
66};
67
18382ead 68struct hdac_hdmi_pin {
15b91447 69 struct list_head head;
18382ead
SP
70 hda_nid_t nid;
71 int num_mux_nids;
72 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
b8a54545
SP
73 struct hdac_hdmi_eld eld;
74 struct hdac_ext_device *edev;
75 int repoll_count;
76 struct delayed_work work;
18382ead
SP
77};
78
4a3478de
JK
79struct hdac_hdmi_pcm {
80 struct list_head head;
81 int pcm_id;
82 struct hdac_hdmi_pin *pin;
83 struct hdac_hdmi_cvt *cvt;
84 struct snd_jack *jack;
85};
86
18382ead
SP
87struct hdac_hdmi_dai_pin_map {
88 int dai_id;
15b91447
SP
89 struct hdac_hdmi_pin *pin;
90 struct hdac_hdmi_cvt *cvt;
18382ead
SP
91};
92
93struct hdac_hdmi_priv {
18382ead 94 struct hdac_hdmi_dai_pin_map dai_map[3];
15b91447
SP
95 struct list_head pin_list;
96 struct list_head cvt_list;
4a3478de 97 struct list_head pcm_list;
15b91447
SP
98 int num_pin;
99 int num_cvt;
4a3478de 100 struct mutex pin_mutex;
18382ead
SP
101};
102
e342ac08
SP
103static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
104{
51b2c425 105 struct hdac_device *hdac = dev_to_hdac_dev(dev);
e342ac08 106
51b2c425 107 return to_ehdac_device(hdac);
e342ac08
SP
108}
109
2428bca3
SP
110static unsigned int sad_format(const u8 *sad)
111{
112 return ((sad[0] >> 0x3) & 0x1f);
113}
114
115static unsigned int sad_sample_bits_lpcm(const u8 *sad)
116{
117 return (sad[2] & 7);
118}
119
120static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
121 void *eld)
122{
123 u64 formats = SNDRV_PCM_FMTBIT_S16;
124 int i;
125 const u8 *sad, *eld_buf = eld;
126
127 sad = drm_eld_sad(eld_buf);
128 if (!sad)
129 goto format_constraint;
130
131 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
132 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
133
134 /*
135 * the controller support 20 and 24 bits in 32 bit
136 * container so we set S32
137 */
138 if (sad_sample_bits_lpcm(sad) & 0x6)
139 formats |= SNDRV_PCM_FMTBIT_S32;
140 }
141 }
142
143format_constraint:
144 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
145 formats);
146
147}
148
b8a54545
SP
149 /* HDMI ELD routines */
150static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
151 hda_nid_t nid, int byte_index)
152{
153 unsigned int val;
154
155 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
156 byte_index);
157
158 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
159 byte_index, val);
160
161 return val;
162}
163
164static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
165{
166 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
167 AC_DIPSIZE_ELD_BUF);
168}
169
170/*
171 * This function queries the ELD size and ELD data and fills in the buffer
172 * passed by user
173 */
174static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
175 unsigned char *buf, int *eld_size)
176{
177 int i, size, ret = 0;
178
179 /*
180 * ELD size is initialized to zero in caller function. If no errors and
181 * ELD is valid, actual eld_size is assigned.
182 */
183
184 size = hdac_hdmi_get_eld_size(codec, nid);
185 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
186 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
187 return -ERANGE;
188 }
189
190 /* set ELD buffer */
191 for (i = 0; i < size; i++) {
192 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
193 /*
194 * Graphics driver might be writing to ELD buffer right now.
195 * Just abort. The caller will repoll after a while.
196 */
197 if (!(val & AC_ELDD_ELD_VALID)) {
198 dev_err(&codec->dev,
199 "HDMI: invalid ELD data byte %d\n", i);
200 ret = -EINVAL;
201 goto error;
202 }
203 val &= AC_ELDD_ELD_DATA;
204 /*
205 * The first byte cannot be zero. This can happen on some DVI
206 * connections. Some Intel chips may also need some 250ms delay
207 * to return non-zero ELD data, even when the graphics driver
208 * correctly writes ELD content before setting ELD_valid bit.
209 */
210 if (!val && !i) {
211 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
212 ret = -EINVAL;
213 goto error;
214 }
215 buf[i] = val;
216 }
217
218 *eld_size = size;
219error:
220 return ret;
221}
222
b0362adb
SP
223static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
224 hda_nid_t cvt_nid, hda_nid_t pin_nid,
225 u32 stream_tag, int format)
226{
227 unsigned int val;
228
229 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
230 cvt_nid, pin_nid, stream_tag, format);
231
232 val = (stream_tag << 4);
233
234 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
235 AC_VERB_SET_CHANNEL_STREAMID, val);
236 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
237 AC_VERB_SET_STREAM_FORMAT, format);
238
239 return 0;
240}
241
a657f1d0
SP
242static void
243hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
244 int packet_index, int byte_index)
245{
246 int val;
247
248 val = (packet_index << 5) | (byte_index & 0x1f);
249
250 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
251 AC_VERB_SET_HDMI_DIP_INDEX, val);
252}
253
254static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
255 hda_nid_t cvt_nid, hda_nid_t pin_nid)
256{
257 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
258 struct hdmi_audio_infoframe frame;
259 u8 *dip = (u8 *)&frame;
260 int ret;
261 int i;
262
263 hdmi_audio_infoframe_init(&frame);
264
265 /* Default stereo for now */
266 frame.channels = 2;
267
268 /* setup channel count */
269 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
270 AC_VERB_SET_CVT_CHAN_COUNT, frame.channels - 1);
271
272 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
273 if (ret < 0)
274 return ret;
275
276 /* stop infoframe transmission */
277 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
278 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
279 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
280
281
282 /* Fill infoframe. Index auto-incremented */
283 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
284 for (i = 0; i < sizeof(frame); i++)
285 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
286 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
287
288 /* Start infoframe */
289 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
290 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
291 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
292
293 return 0;
294}
295
b0362adb
SP
296static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
297 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
298{
299 /* Power up pin widget */
15b91447
SP
300 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
301 pwr_state))
302 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
b0362adb
SP
303 AC_VERB_SET_POWER_STATE, pwr_state);
304
305 /* Power up converter */
15b91447
SP
306 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
307 pwr_state))
308 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
b0362adb
SP
309 AC_VERB_SET_POWER_STATE, pwr_state);
310}
311
312static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
313 struct snd_soc_dai *dai)
314{
315 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
316 struct hdac_hdmi_priv *hdmi = hdac->private_data;
317 struct hdac_hdmi_dai_pin_map *dai_map;
318 struct hdac_ext_dma_params *dd;
a657f1d0 319 int ret;
b0362adb
SP
320
321 if (dai->id > 0) {
322 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
323 return -ENODEV;
324 }
325
326 dai_map = &hdmi->dai_map[dai->id];
327
328 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
329 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
330 dd->stream_tag, dd->format);
331
15b91447
SP
332 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
333 dai_map->pin->nid);
a657f1d0
SP
334 if (ret < 0)
335 return ret;
336
15b91447
SP
337 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
338 dai_map->pin->nid, dd->stream_tag, dd->format);
b0362adb
SP
339}
340
341static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
342 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
343{
344 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
345 struct hdac_ext_dma_params *dd;
346
347 if (dai->id > 0) {
348 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
349 return -ENODEV;
350 }
351
352 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
8d33ab24
SM
353 if (!dd)
354 return -ENOMEM;
b0362adb
SP
355 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
356 params_channels(hparams), params_format(hparams),
357 24, 0);
358
359 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
360
361 return 0;
362}
363
364static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
365 struct snd_soc_dai *dai)
366{
367 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
368 struct hdac_ext_dma_params *dd;
369 struct hdac_hdmi_priv *hdmi = edev->private_data;
370 struct hdac_hdmi_dai_pin_map *dai_map;
371
372 dai_map = &hdmi->dai_map[dai->id];
373
15b91447 374 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
b0362adb 375 AC_VERB_SET_CHANNEL_STREAMID, 0);
15b91447 376 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
b0362adb
SP
377 AC_VERB_SET_STREAM_FORMAT, 0);
378
379 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
380 snd_soc_dai_set_dma_data(dai, substream, NULL);
381
382 kfree(dd);
383
384 return 0;
385}
386
387static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
388 struct snd_soc_dai *dai)
389{
390 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
391 struct hdac_hdmi_priv *hdmi = hdac->private_data;
392 struct hdac_hdmi_dai_pin_map *dai_map;
2428bca3 393 int ret;
b0362adb
SP
394
395 if (dai->id > 0) {
396 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
397 return -ENODEV;
398 }
399
400 dai_map = &hdmi->dai_map[dai->id];
401
b8a54545
SP
402 if ((!dai_map->pin->eld.monitor_present) ||
403 (!dai_map->pin->eld.eld_valid)) {
404
405 dev_err(&hdac->hdac.dev,
406 "Failed: montior present? %d ELD valid?: %d\n",
407 dai_map->pin->eld.monitor_present,
408 dai_map->pin->eld.eld_valid);
b0362adb 409
b0362adb
SP
410 return -ENODEV;
411 }
412
413 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
414
15b91447 415 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
b0362adb
SP
416 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
417
2428bca3
SP
418 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
419 dai_map->pin->eld.eld_buffer);
420 if (ret < 0)
421 return ret;
b0362adb 422
2428bca3
SP
423 return snd_pcm_hw_constraint_eld(substream->runtime,
424 dai_map->pin->eld.eld_buffer);
b0362adb
SP
425}
426
427static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
428 struct snd_soc_dai *dai)
429{
430 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
431 struct hdac_hdmi_priv *hdmi = hdac->private_data;
432 struct hdac_hdmi_dai_pin_map *dai_map;
433
434 dai_map = &hdmi->dai_map[dai->id];
435
436 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
437
15b91447 438 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
b0362adb
SP
439 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
440}
441
18382ead
SP
442static int
443hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
444{
445 int err;
446
447 /* Only stereo supported as of now */
448 cvt->params.channels_min = cvt->params.channels_max = 2;
449
450 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
451 &cvt->params.rates,
452 &cvt->params.formats,
453 &cvt->params.maxbps);
454 if (err < 0)
455 dev_err(&hdac->dev,
456 "Failed to query pcm params for nid %d: %d\n",
457 cvt->nid, err);
458
459 return err;
460}
461
79f4e922
SP
462static int hdac_hdmi_fill_widget_info(struct device *dev,
463 struct snd_soc_dapm_widget *w,
464 enum snd_soc_dapm_type id, void *priv,
465 const char *wname, const char *stream,
466 struct snd_kcontrol_new *wc, int numkc)
18382ead
SP
467{
468 w->id = id;
79f4e922
SP
469 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
470 if (!w->name)
471 return -ENOMEM;
472
18382ead
SP
473 w->sname = stream;
474 w->reg = SND_SOC_NOPM;
475 w->shift = 0;
79f4e922
SP
476 w->kcontrol_news = wc;
477 w->num_kcontrols = numkc;
478 w->priv = priv;
479
480 return 0;
18382ead
SP
481}
482
483static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
79f4e922
SP
484 const char *sink, const char *control, const char *src,
485 int (*handler)(struct snd_soc_dapm_widget *src,
486 struct snd_soc_dapm_widget *sink))
18382ead
SP
487{
488 route->sink = sink;
489 route->source = src;
490 route->control = control;
79f4e922 491 route->connected = handler;
18382ead
SP
492}
493
4a3478de
JK
494static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
495 struct hdac_hdmi_pin *pin)
496{
497 struct hdac_hdmi_priv *hdmi = edev->private_data;
498 struct hdac_hdmi_pcm *pcm = NULL;
499
500 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
501 if (pcm->pin == pin)
502 return pcm;
503 }
504
505 return NULL;
506}
507
508/*
509 * Based on user selection, map the PINs with the PCMs.
510 */
511static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
512 struct snd_ctl_elem_value *ucontrol)
513{
514 int ret;
515 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
516 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
517 struct snd_soc_dapm_context *dapm = w->dapm;
518 struct hdac_hdmi_pin *pin = w->priv;
519 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
520 struct hdac_hdmi_priv *hdmi = edev->private_data;
521 struct hdac_hdmi_pcm *pcm = NULL;
522 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
523
524 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
525 if (ret < 0)
526 return ret;
527
528 mutex_lock(&hdmi->pin_mutex);
529 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
530 if (pcm->pin == pin)
531 pcm->pin = NULL;
532
533 /*
534 * Jack status is not reported during device probe as the
535 * PCMs are not registered by then. So report it here.
536 */
537 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
538 pcm->pin = pin;
539 if (pin->eld.monitor_present && pin->eld.eld_valid) {
540 dev_dbg(&edev->hdac.dev,
541 "jack report for pcm=%d\n",
542 pcm->pcm_id);
543
544 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
545 }
546 mutex_unlock(&hdmi->pin_mutex);
547 return ret;
548 }
549 }
550 mutex_unlock(&hdmi->pin_mutex);
551
552 return ret;
553}
554
79f4e922
SP
555/*
556 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
557 * the display driver seem to be programming the connection list for the pin
558 * widget runtime.
559 *
560 * So programming all the possible inputs for the mux, the user has to take
561 * care of selecting the right one and leaving all other inputs selected to
562 * "NONE"
563 */
564static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
565 struct hdac_hdmi_pin *pin,
566 struct snd_soc_dapm_widget *widget,
567 const char *widget_name)
568{
569 struct hdac_hdmi_priv *hdmi = edev->private_data;
570 struct snd_kcontrol_new *kc;
571 struct hdac_hdmi_cvt *cvt;
572 struct soc_enum *se;
573 char kc_name[NAME_SIZE];
574 char mux_items[NAME_SIZE];
575 /* To hold inputs to the Pin mux */
576 char *items[HDA_MAX_CONNECTIONS];
577 int i = 0;
578 int num_items = hdmi->num_cvt + 1;
579
580 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
581 if (!kc)
582 return -ENOMEM;
583
584 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
585 if (!se)
586 return -ENOMEM;
587
588 sprintf(kc_name, "Pin %d Input", pin->nid);
589 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
590 if (!kc->name)
591 return -ENOMEM;
592
593 kc->private_value = (long)se;
594 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
595 kc->access = 0;
596 kc->info = snd_soc_info_enum_double;
4a3478de 597 kc->put = hdac_hdmi_set_pin_mux;
79f4e922
SP
598 kc->get = snd_soc_dapm_get_enum_double;
599
600 se->reg = SND_SOC_NOPM;
601
602 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
603 se->items = num_items;
604 se->mask = roundup_pow_of_two(se->items) - 1;
605
606 sprintf(mux_items, "NONE");
607 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
608 if (!items[i])
609 return -ENOMEM;
610
611 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
612 i++;
613 sprintf(mux_items, "cvt %d", cvt->nid);
614 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
615 if (!items[i])
616 return -ENOMEM;
617 }
618
619 se->texts = devm_kmemdup(&edev->hdac.dev, items,
620 (num_items * sizeof(char *)), GFP_KERNEL);
621 if (!se->texts)
622 return -ENOMEM;
623
624 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
4a3478de 625 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
79f4e922
SP
626}
627
628/* Add cvt <- input <- mux route map */
629static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
630 struct snd_soc_dapm_widget *widgets,
631 struct snd_soc_dapm_route *route, int rindex)
632{
633 struct hdac_hdmi_priv *hdmi = edev->private_data;
634 const struct snd_kcontrol_new *kc;
635 struct soc_enum *se;
636 int mux_index = hdmi->num_cvt + hdmi->num_pin;
637 int i, j;
638
639 for (i = 0; i < hdmi->num_pin; i++) {
640 kc = widgets[mux_index].kcontrol_news;
641 se = (struct soc_enum *)kc->private_value;
642 for (j = 0; j < hdmi->num_cvt; j++) {
643 hdac_hdmi_fill_route(&route[rindex],
644 widgets[mux_index].name,
645 se->texts[j + 1],
646 widgets[j].name, NULL);
647
648 rindex++;
649 }
650
651 mux_index++;
652 }
653}
654
655/*
656 * Widgets are added in the below sequence
657 * Converter widgets for num converters enumerated
658 * Pin widgets for num pins enumerated
659 * Pin mux widgets to represent connenction list of pin widget
660 *
661 * Total widgets elements = num_cvt + num_pin + num_pin;
662 *
663 * Routes are added as below:
664 * pin mux -> pin (based on num_pins)
665 * cvt -> "Input sel control" -> pin_mux
666 *
667 * Total route elements:
668 * num_pins + (pin_muxes * num_cvt)
669 */
670static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
18382ead 671{
79f4e922
SP
672 struct snd_soc_dapm_widget *widgets;
673 struct snd_soc_dapm_route *route;
674 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
675 struct hdac_hdmi_priv *hdmi = edev->private_data;
676 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
677 char widget_name[NAME_SIZE];
678 struct hdac_hdmi_cvt *cvt;
679 struct hdac_hdmi_pin *pin;
680 int ret, i = 0, num_routes = 0;
18382ead 681
79f4e922
SP
682 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
683 return -EINVAL;
18382ead 684
79f4e922
SP
685 widgets = devm_kzalloc(dapm->dev,
686 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
687 GFP_KERNEL);
18382ead 688
79f4e922
SP
689 if (!widgets)
690 return -ENOMEM;
691
692 /* DAPM widgets to represent each converter widget */
693 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
694 sprintf(widget_name, "Converter %d", cvt->nid);
695 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
696 snd_soc_dapm_aif_in, &cvt->nid,
697 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
698 if (ret < 0)
699 return ret;
700 i++;
701 }
702
703 list_for_each_entry(pin, &hdmi->pin_list, head) {
704 sprintf(widget_name, "hif%d Output", pin->nid);
705 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
706 snd_soc_dapm_output, &pin->nid,
707 widget_name, NULL, NULL, 0);
708 if (ret < 0)
709 return ret;
710 i++;
711 }
712
713 /* DAPM widgets to represent the connection list to pin widget */
714 list_for_each_entry(pin, &hdmi->pin_list, head) {
715 sprintf(widget_name, "Pin %d Mux", pin->nid);
716 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
717 widget_name);
718 if (ret < 0)
719 return ret;
720 i++;
721
722 /* For cvt to pin_mux mapping */
723 num_routes += hdmi->num_cvt;
724
725 /* For pin_mux to pin mapping */
726 num_routes++;
727 }
728
729 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
730 GFP_KERNEL);
731 if (!route)
732 return -ENOMEM;
733
734 i = 0;
735 /* Add pin <- NULL <- mux route map */
736 list_for_each_entry(pin, &hdmi->pin_list, head) {
737 int sink_index = i + hdmi->num_cvt;
738 int src_index = sink_index + hdmi->num_pin;
739
740 hdac_hdmi_fill_route(&route[i],
741 widgets[sink_index].name, NULL,
742 widgets[src_index].name, NULL);
743 i++;
744
745 }
746
747 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
748
749 snd_soc_dapm_new_controls(dapm, widgets,
750 ((2 * hdmi->num_pin) + hdmi->num_cvt));
751
752 snd_soc_dapm_add_routes(dapm, route, num_routes);
753 snd_soc_dapm_new_widgets(dapm->card);
754
755 return 0;
18382ead 756
18382ead
SP
757}
758
15b91447 759static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
18382ead 760{
15b91447
SP
761 struct hdac_hdmi_priv *hdmi = edev->private_data;
762 struct hdac_hdmi_dai_pin_map *dai_map = &hdmi->dai_map[0];
763 struct hdac_hdmi_cvt *cvt;
764 struct hdac_hdmi_pin *pin;
18382ead 765
15b91447
SP
766 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
767 return -EINVAL;
18382ead 768
15b91447
SP
769 /*
770 * Currently on board only 1 pin and 1 converter is enabled for
771 * simplification, more will be added eventually
772 * So using fixed map for dai_id:pin:cvt
773 */
774 cvt = list_first_entry(&hdmi->cvt_list, struct hdac_hdmi_cvt, head);
775 pin = list_first_entry(&hdmi->pin_list, struct hdac_hdmi_pin, head);
776
777 dai_map->dai_id = 0;
778 dai_map->pin = pin;
18382ead 779
15b91447 780 dai_map->cvt = cvt;
18382ead
SP
781
782 /* Enable out path for this pin widget */
15b91447 783 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
18382ead
SP
784 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
785
786 /* Enable transmission */
15b91447 787 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
18382ead
SP
788 AC_VERB_SET_DIGI_CONVERT_1, 1);
789
790 /* Category Code (CC) to zero */
15b91447 791 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
18382ead
SP
792 AC_VERB_SET_DIGI_CONVERT_2, 0);
793
15b91447 794 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
18382ead
SP
795 AC_VERB_SET_CONNECT_SEL, 0);
796
15b91447
SP
797 return 0;
798}
799
800static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
801{
802 struct hdac_hdmi_priv *hdmi = edev->private_data;
803 struct hdac_hdmi_cvt *cvt;
4a3478de 804 char name[NAME_SIZE];
15b91447
SP
805
806 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
807 if (!cvt)
808 return -ENOMEM;
809
810 cvt->nid = nid;
4a3478de
JK
811 sprintf(name, "cvt %d", cvt->nid);
812 cvt->name = kstrdup(name, GFP_KERNEL);
15b91447
SP
813
814 list_add_tail(&cvt->head, &hdmi->cvt_list);
815 hdmi->num_cvt++;
816
817 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
818}
819
b8a54545
SP
820static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
821{
822 struct hdac_ext_device *edev = pin->edev;
4a3478de
JK
823 struct hdac_hdmi_priv *hdmi = edev->private_data;
824 struct hdac_hdmi_pcm *pcm;
b8a54545
SP
825 int val;
826
827 if (!edev)
828 return;
829
830 pin->repoll_count = repoll;
831
832 pm_runtime_get_sync(&edev->hdac.dev);
833 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
834 AC_VERB_GET_PIN_SENSE, 0);
835
836 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
837 val, pin->nid);
838
4a3478de
JK
839
840 mutex_lock(&hdmi->pin_mutex);
b8a54545
SP
841 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
842 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
843
4a3478de
JK
844 pcm = hdac_hdmi_get_pcm(edev, pin);
845
b8a54545
SP
846 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
847
848 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
849 __func__, pin->nid);
4a3478de
JK
850
851 /*
852 * PCMs are not registered during device probe, so don't
853 * report jack here. It will be done in usermode mux
854 * control select.
855 */
856 if (pcm) {
857 dev_dbg(&edev->hdac.dev,
858 "jack report for pcm=%d\n", pcm->pcm_id);
859
860 snd_jack_report(pcm->jack, 0);
861 }
862
863 mutex_unlock(&hdmi->pin_mutex);
b8a54545
SP
864 goto put_hdac_device;
865 }
866
867 if (pin->eld.monitor_present && pin->eld.eld_valid) {
868 /* TODO: use i915 component for reading ELD later */
869 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
870 pin->eld.eld_buffer,
871 &pin->eld.eld_size) == 0) {
872
4a3478de
JK
873 if (pcm) {
874 dev_dbg(&edev->hdac.dev,
875 "jack report for pcm=%d\n",
876 pcm->pcm_id);
877
878 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
879 }
880
b8a54545
SP
881 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
882 pin->eld.eld_buffer, pin->eld.eld_size);
883 } else {
884 pin->eld.monitor_present = false;
885 pin->eld.eld_valid = false;
4a3478de
JK
886
887 if (pcm) {
888 dev_dbg(&edev->hdac.dev,
889 "jack report for pcm=%d\n",
890 pcm->pcm_id);
891
892 snd_jack_report(pcm->jack, 0);
893 }
b8a54545
SP
894 }
895 }
896
4a3478de
JK
897 mutex_unlock(&hdmi->pin_mutex);
898
b8a54545
SP
899 /*
900 * Sometimes the pin_sense may present invalid monitor
901 * present and eld_valid. If ELD data is not valid, loop few
902 * more times to get correct pin sense and valid ELD.
903 */
904 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
905 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
906
907put_hdac_device:
908 pm_runtime_put_sync(&edev->hdac.dev);
909}
910
911static void hdac_hdmi_repoll_eld(struct work_struct *work)
912{
913 struct hdac_hdmi_pin *pin =
914 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
915
916 /* picked from legacy HDA driver */
917 if (pin->repoll_count++ > 6)
918 pin->repoll_count = 0;
919
920 hdac_hdmi_present_sense(pin, pin->repoll_count);
921}
922
15b91447
SP
923static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
924{
925 struct hdac_hdmi_priv *hdmi = edev->private_data;
926 struct hdac_hdmi_pin *pin;
927
928 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
929 if (!pin)
930 return -ENOMEM;
931
932 pin->nid = nid;
933
934 list_add_tail(&pin->head, &hdmi->pin_list);
935 hdmi->num_pin++;
936
b8a54545
SP
937 pin->edev = edev;
938 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
939
15b91447 940 return 0;
18382ead
SP
941}
942
211caab7
SP
943#define INTEL_VENDOR_NID 0x08
944#define INTEL_GET_VENDOR_VERB 0xf81
945#define INTEL_SET_VENDOR_VERB 0x781
946#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
947#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
948
949static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
950{
951 unsigned int vendor_param;
952
953 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
954 INTEL_GET_VENDOR_VERB, 0);
955 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
956 return;
957
958 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
959 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
960 INTEL_SET_VENDOR_VERB, vendor_param);
961 if (vendor_param == -1)
962 return;
963}
964
965static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
966{
967 unsigned int vendor_param;
968
969 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
970 INTEL_GET_VENDOR_VERB, 0);
971 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
972 return;
973
974 /* enable DP1.2 mode */
975 vendor_param |= INTEL_EN_DP12;
976 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
977 INTEL_SET_VENDOR_VERB, vendor_param);
978 if (vendor_param == -1)
979 return;
980
981}
982
17a42c45
SP
983static struct snd_soc_dai_ops hdmi_dai_ops = {
984 .startup = hdac_hdmi_pcm_open,
985 .shutdown = hdac_hdmi_pcm_close,
986 .hw_params = hdac_hdmi_set_hw_params,
987 .prepare = hdac_hdmi_playback_prepare,
988 .hw_free = hdac_hdmi_playback_cleanup,
989};
990
991/*
992 * Each converter can support a stream independently. So a dai is created
993 * based on the number of converter queried.
994 */
995static int hdac_hdmi_create_dais(struct hdac_device *hdac,
996 struct snd_soc_dai_driver **dais,
997 struct hdac_hdmi_priv *hdmi, int num_dais)
998{
999 struct snd_soc_dai_driver *hdmi_dais;
1000 struct hdac_hdmi_cvt *cvt;
1001 char name[NAME_SIZE], dai_name[NAME_SIZE];
1002 int i = 0;
1003 u32 rates, bps;
1004 unsigned int rate_max = 384000, rate_min = 8000;
1005 u64 formats;
1006 int ret;
1007
1008 hdmi_dais = devm_kzalloc(&hdac->dev,
1009 (sizeof(*hdmi_dais) * num_dais),
1010 GFP_KERNEL);
1011 if (!hdmi_dais)
1012 return -ENOMEM;
1013
1014 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1015 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1016 &rates, &formats, &bps);
1017 if (ret)
1018 return ret;
1019
1020 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1021 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1022 dai_name, GFP_KERNEL);
1023
1024 if (!hdmi_dais[i].name)
1025 return -ENOMEM;
1026
1027 snprintf(name, sizeof(name), "hifi%d", i+1);
1028 hdmi_dais[i].playback.stream_name =
1029 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1030 if (!hdmi_dais[i].playback.stream_name)
1031 return -ENOMEM;
1032
1033 /*
1034 * Set caps based on capability queried from the converter.
1035 * It will be constrained runtime based on ELD queried.
1036 */
1037 hdmi_dais[i].playback.formats = formats;
1038 hdmi_dais[i].playback.rates = rates;
1039 hdmi_dais[i].playback.rate_max = rate_max;
1040 hdmi_dais[i].playback.rate_min = rate_min;
1041 hdmi_dais[i].playback.channels_min = 2;
1042 hdmi_dais[i].playback.channels_max = 2;
1043 hdmi_dais[i].ops = &hdmi_dai_ops;
1044
1045 i++;
1046 }
1047
1048 *dais = hdmi_dais;
1049
1050 return 0;
1051}
1052
18382ead
SP
1053/*
1054 * Parse all nodes and store the cvt/pin nids in array
1055 * Add one time initialization for pin and cvt widgets
1056 */
17a42c45
SP
1057static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1058 struct snd_soc_dai_driver **dais, int *num_dais)
18382ead
SP
1059{
1060 hda_nid_t nid;
3c83ac23 1061 int i, num_nodes;
18382ead
SP
1062 struct hdac_device *hdac = &edev->hdac;
1063 struct hdac_hdmi_priv *hdmi = edev->private_data;
15b91447 1064 int ret;
18382ead 1065
211caab7
SP
1066 hdac_hdmi_skl_enable_all_pins(hdac);
1067 hdac_hdmi_skl_enable_dp12(hdac);
1068
3c83ac23 1069 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
541140d4 1070 if (!nid || num_nodes <= 0) {
18382ead
SP
1071 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1072 return -EINVAL;
1073 }
1074
3c83ac23 1075 hdac->num_nodes = num_nodes;
18382ead
SP
1076 hdac->start_nid = nid;
1077
1078 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1079 unsigned int caps;
1080 unsigned int type;
1081
1082 caps = get_wcaps(hdac, nid);
1083 type = get_wcaps_type(caps);
1084
1085 if (!(caps & AC_WCAP_DIGITAL))
1086 continue;
1087
1088 switch (type) {
1089
1090 case AC_WID_AUD_OUT:
15b91447
SP
1091 ret = hdac_hdmi_add_cvt(edev, nid);
1092 if (ret < 0)
1093 return ret;
18382ead
SP
1094 break;
1095
1096 case AC_WID_PIN:
15b91447
SP
1097 ret = hdac_hdmi_add_pin(edev, nid);
1098 if (ret < 0)
1099 return ret;
18382ead
SP
1100 break;
1101 }
1102 }
1103
1104 hdac->end_nid = nid;
1105
15b91447 1106 if (!hdmi->num_pin || !hdmi->num_cvt)
18382ead
SP
1107 return -EIO;
1108
17a42c45
SP
1109 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1110 if (ret) {
1111 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1112 ret);
1113 return ret;
1114 }
1115
1116 *num_dais = hdmi->num_cvt;
1117
15b91447 1118 return hdac_hdmi_init_dai_map(edev);
18382ead
SP
1119}
1120
b8a54545
SP
1121static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1122{
1123 struct hdac_ext_device *edev = aptr;
1124 struct hdac_hdmi_priv *hdmi = edev->private_data;
1125 struct hdac_hdmi_pin *pin;
1126 struct snd_soc_codec *codec = edev->scodec;
1127
1128 /* Don't know how this mapping is derived */
1129 hda_nid_t pin_nid = port + 0x04;
1130
1131 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1132
1133 /*
1134 * skip notification during system suspend (but not in runtime PM);
1135 * the state will be updated at resume. Also since the ELD and
1136 * connection states are updated in anyway at the end of the resume,
1137 * we can skip it when received during PM process.
1138 */
1139 if (snd_power_get_state(codec->component.card->snd_card) !=
1140 SNDRV_CTL_POWER_D0)
1141 return;
1142
1143 if (atomic_read(&edev->hdac.in_pm))
1144 return;
1145
1146 list_for_each_entry(pin, &hdmi->pin_list, head) {
1147 if (pin->nid == pin_nid)
1148 hdac_hdmi_present_sense(pin, 1);
1149 }
1150}
1151
1152static struct i915_audio_component_audio_ops aops = {
1153 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1154};
1155
4a3478de
JK
1156int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1157{
1158 char jack_name[NAME_SIZE];
1159 struct snd_soc_codec *codec = dai->codec;
1160 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1161 struct snd_soc_dapm_context *dapm =
1162 snd_soc_component_get_dapm(&codec->component);
1163 struct hdac_hdmi_priv *hdmi = edev->private_data;
1164 struct hdac_hdmi_pcm *pcm;
1165
1166 /*
1167 * this is a new PCM device, create new pcm and
1168 * add to the pcm list
1169 */
1170 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1171 if (!pcm)
1172 return -ENOMEM;
1173 pcm->pcm_id = device;
1174 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1175
1176 list_add_tail(&pcm->head, &hdmi->pcm_list);
1177
1178 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1179
1180 return snd_jack_new(dapm->card->snd_card, jack_name,
1181 SND_JACK_AVOUT, &pcm->jack, true, false);
1182}
1183EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1184
18382ead
SP
1185static int hdmi_codec_probe(struct snd_soc_codec *codec)
1186{
1187 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1188 struct hdac_hdmi_priv *hdmi = edev->private_data;
1189 struct snd_soc_dapm_context *dapm =
1190 snd_soc_component_get_dapm(&codec->component);
b8a54545
SP
1191 struct hdac_hdmi_pin *pin;
1192 int ret;
18382ead
SP
1193
1194 edev->scodec = codec;
1195
79f4e922
SP
1196 ret = create_fill_widget_route_map(dapm);
1197 if (ret < 0)
1198 return ret;
18382ead 1199
b8a54545
SP
1200 aops.audio_ptr = edev;
1201 ret = snd_hdac_i915_register_notifier(&aops);
1202 if (ret < 0) {
1203 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1204 ret);
1205 return ret;
1206 }
1207
1208 list_for_each_entry(pin, &hdmi->pin_list, head)
1209 hdac_hdmi_present_sense(pin, 1);
1210
18382ead
SP
1211 /* Imp: Store the card pointer in hda_codec */
1212 edev->card = dapm->card->snd_card;
1213
e342ac08
SP
1214 /*
1215 * hdac_device core already sets the state to active and calls
1216 * get_noresume. So enable runtime and set the device to suspend.
1217 */
1218 pm_runtime_enable(&edev->hdac.dev);
1219 pm_runtime_put(&edev->hdac.dev);
1220 pm_runtime_suspend(&edev->hdac.dev);
1221
1222 return 0;
1223}
1224
1225static int hdmi_codec_remove(struct snd_soc_codec *codec)
1226{
1227 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1228
1229 pm_runtime_disable(&edev->hdac.dev);
18382ead
SP
1230 return 0;
1231}
1232
1233static struct snd_soc_codec_driver hdmi_hda_codec = {
1234 .probe = hdmi_codec_probe,
e342ac08 1235 .remove = hdmi_codec_remove,
18382ead
SP
1236 .idle_bias_off = true,
1237};
1238
18382ead
SP
1239static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1240{
1241 struct hdac_device *codec = &edev->hdac;
1242 struct hdac_hdmi_priv *hdmi_priv;
17a42c45
SP
1243 struct snd_soc_dai_driver *hdmi_dais = NULL;
1244 int num_dais = 0;
18382ead
SP
1245 int ret = 0;
1246
1247 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1248 if (hdmi_priv == NULL)
1249 return -ENOMEM;
1250
1251 edev->private_data = hdmi_priv;
1252
1253 dev_set_drvdata(&codec->dev, edev);
1254
15b91447
SP
1255 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1256 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
4a3478de
JK
1257 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1258 mutex_init(&hdmi_priv->pin_mutex);
15b91447 1259
17a42c45
SP
1260 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1261 if (ret < 0) {
1262 dev_err(&codec->dev,
1263 "Failed in parse and map nid with err: %d\n", ret);
18382ead 1264 return ret;
17a42c45 1265 }
18382ead
SP
1266
1267 /* ASoC specific initialization */
1268 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
17a42c45 1269 hdmi_dais, num_dais);
18382ead
SP
1270}
1271
1272static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1273{
15b91447
SP
1274 struct hdac_hdmi_priv *hdmi = edev->private_data;
1275 struct hdac_hdmi_pin *pin, *pin_next;
1276 struct hdac_hdmi_cvt *cvt, *cvt_next;
4a3478de 1277 struct hdac_hdmi_pcm *pcm, *pcm_next;
15b91447 1278
18382ead
SP
1279 snd_soc_unregister_codec(&edev->hdac.dev);
1280
4a3478de
JK
1281 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1282 pcm->cvt = NULL;
1283 pcm->pin = NULL;
1284 list_del(&pcm->head);
1285 kfree(pcm);
1286 }
1287
15b91447
SP
1288 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1289 list_del(&cvt->head);
4a3478de 1290 kfree(cvt->name);
15b91447
SP
1291 kfree(cvt);
1292 }
1293
1294 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1295 list_del(&pin->head);
1296 kfree(pin);
1297 }
1298
18382ead
SP
1299 return 0;
1300}
1301
e342ac08
SP
1302#ifdef CONFIG_PM
1303static int hdac_hdmi_runtime_suspend(struct device *dev)
1304{
1305 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1306 struct hdac_device *hdac = &edev->hdac;
07f083ab
SP
1307 struct hdac_bus *bus = hdac->bus;
1308 int err;
e342ac08
SP
1309
1310 dev_dbg(dev, "Enter: %s\n", __func__);
1311
07f083ab
SP
1312 /* controller may not have been initialized for the first time */
1313 if (!bus)
1314 return 0;
1315
e342ac08
SP
1316 /* Power down afg */
1317 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
1318 snd_hdac_codec_write(hdac, hdac->afg, 0,
1319 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
1320
07f083ab
SP
1321 err = snd_hdac_display_power(bus, false);
1322 if (err < 0) {
1323 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1324 return err;
1325 }
1326
e342ac08
SP
1327 return 0;
1328}
1329
1330static int hdac_hdmi_runtime_resume(struct device *dev)
1331{
1332 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1333 struct hdac_device *hdac = &edev->hdac;
07f083ab
SP
1334 struct hdac_bus *bus = hdac->bus;
1335 int err;
e342ac08
SP
1336
1337 dev_dbg(dev, "Enter: %s\n", __func__);
1338
07f083ab
SP
1339 /* controller may not have been initialized for the first time */
1340 if (!bus)
1341 return 0;
1342
1343 err = snd_hdac_display_power(bus, true);
1344 if (err < 0) {
1345 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1346 return err;
1347 }
1348
e342ac08
SP
1349 /* Power up afg */
1350 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
1351 snd_hdac_codec_write(hdac, hdac->afg, 0,
1352 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1353
1354 return 0;
1355}
1356#else
1357#define hdac_hdmi_runtime_suspend NULL
1358#define hdac_hdmi_runtime_resume NULL
1359#endif
1360
1361static const struct dev_pm_ops hdac_hdmi_pm = {
1362 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
1363};
1364
18382ead
SP
1365static const struct hda_device_id hdmi_list[] = {
1366 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1367 {}
1368};
1369
1370MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1371
1372static struct hdac_ext_driver hdmi_driver = {
1373 . hdac = {
1374 .driver = {
1375 .name = "HDMI HDA Codec",
e342ac08 1376 .pm = &hdac_hdmi_pm,
18382ead
SP
1377 },
1378 .id_table = hdmi_list,
1379 },
1380 .probe = hdac_hdmi_dev_probe,
1381 .remove = hdac_hdmi_dev_remove,
1382};
1383
1384static int __init hdmi_init(void)
1385{
1386 return snd_hda_ext_driver_register(&hdmi_driver);
1387}
1388
1389static void __exit hdmi_exit(void)
1390{
1391 snd_hda_ext_driver_unregister(&hdmi_driver);
1392}
1393
1394module_init(hdmi_init);
1395module_exit(hdmi_exit);
1396
1397MODULE_LICENSE("GPL v2");
1398MODULE_DESCRIPTION("HDMI HD codec");
1399MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1400MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
This page took 0.126137 seconds and 5 git commands to generate.