blob: f96bd2fb634b8d082be213fd99474b42d1b36a94 [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>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/hdaudio_ext.h>
27#include "../../hda/local.h"
28
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053029#define AMP_OUT_MUTE 0xb080
30#define AMP_OUT_UNMUTE 0xb000
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053031#define PIN_OUT (AC_PINCTL_OUT_EN)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053032
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053033#define HDA_MAX_CONNECTIONS 32
34
35struct hdac_hdmi_cvt_params {
36 unsigned int channels_min;
37 unsigned int channels_max;
38 u32 rates;
39 u64 formats;
40 unsigned int maxbps;
41};
42
43struct hdac_hdmi_cvt {
44 hda_nid_t nid;
45 struct hdac_hdmi_cvt_params params;
46};
47
48struct hdac_hdmi_pin {
49 hda_nid_t nid;
50 int num_mux_nids;
51 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
52};
53
54struct hdac_hdmi_dai_pin_map {
55 int dai_id;
56 struct hdac_hdmi_pin pin;
57 struct hdac_hdmi_cvt cvt;
58};
59
60struct hdac_hdmi_priv {
61 hda_nid_t pin_nid[3];
62 hda_nid_t cvt_nid[3];
63 struct hdac_hdmi_dai_pin_map dai_map[3];
64};
65
Subhransu S. Prustye342ac02015-11-10 18:42:07 +053066static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
67{
68 struct hdac_device *hdac = container_of(dev, struct hdac_device, dev);
69
70 return container_of(hdac, struct hdac_ext_device, hdac);
71}
72
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053073static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
74 hda_nid_t cvt_nid, hda_nid_t pin_nid,
75 u32 stream_tag, int format)
76{
77 unsigned int val;
78
79 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
80 cvt_nid, pin_nid, stream_tag, format);
81
82 val = (stream_tag << 4);
83
84 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
85 AC_VERB_SET_CHANNEL_STREAMID, val);
86 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
87 AC_VERB_SET_STREAM_FORMAT, format);
88
89 return 0;
90}
91
92static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
93 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
94{
95 /* Power up pin widget */
96 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin.nid, pwr_state))
97 snd_hdac_codec_write(&edev->hdac, dai_map->pin.nid, 0,
98 AC_VERB_SET_POWER_STATE, pwr_state);
99
100 /* Power up converter */
101 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt.nid, pwr_state))
102 snd_hdac_codec_write(&edev->hdac, dai_map->cvt.nid, 0,
103 AC_VERB_SET_POWER_STATE, pwr_state);
104}
105
106static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
107 struct snd_soc_dai *dai)
108{
109 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
110 struct hdac_hdmi_priv *hdmi = hdac->private_data;
111 struct hdac_hdmi_dai_pin_map *dai_map;
112 struct hdac_ext_dma_params *dd;
113
114 if (dai->id > 0) {
115 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
116 return -ENODEV;
117 }
118
119 dai_map = &hdmi->dai_map[dai->id];
120
121 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
122 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
123 dd->stream_tag, dd->format);
124
125 return hdac_hdmi_setup_stream(hdac, dai_map->cvt.nid, dai_map->pin.nid,
126 dd->stream_tag, dd->format);
127}
128
129static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
130 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
131{
132 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
133 struct hdac_ext_dma_params *dd;
134
135 if (dai->id > 0) {
136 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
137 return -ENODEV;
138 }
139
140 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
141 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
142 params_channels(hparams), params_format(hparams),
143 24, 0);
144
145 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
146
147 return 0;
148}
149
150static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
151 struct snd_soc_dai *dai)
152{
153 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
154 struct hdac_ext_dma_params *dd;
155 struct hdac_hdmi_priv *hdmi = edev->private_data;
156 struct hdac_hdmi_dai_pin_map *dai_map;
157
158 dai_map = &hdmi->dai_map[dai->id];
159
160 snd_hdac_codec_write(&edev->hdac, dai_map->cvt.nid, 0,
161 AC_VERB_SET_CHANNEL_STREAMID, 0);
162 snd_hdac_codec_write(&edev->hdac, dai_map->cvt.nid, 0,
163 AC_VERB_SET_STREAM_FORMAT, 0);
164
165 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
166 snd_soc_dai_set_dma_data(dai, substream, NULL);
167
168 kfree(dd);
169
170 return 0;
171}
172
173static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
174 struct snd_soc_dai *dai)
175{
176 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
177 struct hdac_hdmi_priv *hdmi = hdac->private_data;
178 struct hdac_hdmi_dai_pin_map *dai_map;
179 int val;
180
181 if (dai->id > 0) {
182 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
183 return -ENODEV;
184 }
185
186 dai_map = &hdmi->dai_map[dai->id];
187
188 val = snd_hdac_codec_read(&hdac->hdac, dai_map->pin.nid, 0,
189 AC_VERB_GET_PIN_SENSE, 0);
190 dev_info(&hdac->hdac.dev, "Val for AC_VERB_GET_PIN_SENSE: %x\n", val);
191
192 if ((!(val & AC_PINSENSE_PRESENCE)) || (!(val & AC_PINSENSE_ELDV))) {
193 dev_err(&hdac->hdac.dev, "Monitor presence invalid with val: %x\n", val);
194 return -ENODEV;
195 }
196
197 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
198
199 snd_hdac_codec_write(&hdac->hdac, dai_map->pin.nid, 0,
200 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
201
202 snd_pcm_hw_constraint_step(substream->runtime, 0,
203 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
204
205 return 0;
206}
207
208static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
209 struct snd_soc_dai *dai)
210{
211 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
212 struct hdac_hdmi_priv *hdmi = hdac->private_data;
213 struct hdac_hdmi_dai_pin_map *dai_map;
214
215 dai_map = &hdmi->dai_map[dai->id];
216
217 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
218
219 snd_hdac_codec_write(&hdac->hdac, dai_map->pin.nid, 0,
220 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
221}
222
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530223static int
224hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
225{
226 int err;
227
228 /* Only stereo supported as of now */
229 cvt->params.channels_min = cvt->params.channels_max = 2;
230
231 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
232 &cvt->params.rates,
233 &cvt->params.formats,
234 &cvt->params.maxbps);
235 if (err < 0)
236 dev_err(&hdac->dev,
237 "Failed to query pcm params for nid %d: %d\n",
238 cvt->nid, err);
239
240 return err;
241}
242
243static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
244 struct hdac_hdmi_pin *pin)
245{
246 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
247 dev_warn(&hdac->hdac.dev,
248 "HDMI: pin %d wcaps %#x does not support connection list\n",
249 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
250 return -EINVAL;
251 }
252
253 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
254 pin->mux_nids, HDA_MAX_CONNECTIONS);
255 if (pin->num_mux_nids == 0) {
256 dev_err(&hdac->hdac.dev, "No connections found\n");
257 return -ENODEV;
258 }
259
260 return pin->num_mux_nids;
261}
262
263static void hdac_hdmi_fill_widget_info(struct snd_soc_dapm_widget *w,
264 enum snd_soc_dapm_type id,
265 const char *wname, const char *stream)
266{
267 w->id = id;
268 w->name = wname;
269 w->sname = stream;
270 w->reg = SND_SOC_NOPM;
271 w->shift = 0;
272 w->kcontrol_news = NULL;
273 w->num_kcontrols = 0;
274 w->priv = NULL;
275}
276
277static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
278 const char *sink, const char *control, const char *src)
279{
280 route->sink = sink;
281 route->source = src;
282 route->control = control;
283 route->connected = NULL;
284}
285
286static void create_fill_widget_route_map(struct snd_soc_dapm_context *dapm,
287 struct hdac_hdmi_dai_pin_map *dai_map)
288{
289 struct snd_soc_dapm_route route[1];
290 struct snd_soc_dapm_widget widgets[2] = { {0} };
291
292 memset(&route, 0, sizeof(route));
293
294 hdac_hdmi_fill_widget_info(&widgets[0], snd_soc_dapm_output,
295 "hif1 Output", NULL);
296 hdac_hdmi_fill_widget_info(&widgets[1], snd_soc_dapm_aif_in,
297 "Coverter 1", "hif1");
298
299 hdac_hdmi_fill_route(&route[0], "hif1 Output", NULL, "Coverter 1");
300
301 snd_soc_dapm_new_controls(dapm, widgets, ARRAY_SIZE(widgets));
302 snd_soc_dapm_add_routes(dapm, route, ARRAY_SIZE(route));
303}
304
305static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev,
306 struct hdac_hdmi_dai_pin_map *dai_map,
307 hda_nid_t pin_nid, hda_nid_t cvt_nid, int dai_id)
308{
309 int ret;
310
311 dai_map->dai_id = dai_id;
312 dai_map->pin.nid = pin_nid;
313
314 ret = hdac_hdmi_query_pin_connlist(edev, &dai_map->pin);
315 if (ret < 0) {
316 dev_err(&edev->hdac.dev,
317 "Error querying connection list: %d\n", ret);
318 return ret;
319 }
320
321 dai_map->cvt.nid = cvt_nid;
322
323 /* Enable out path for this pin widget */
324 snd_hdac_codec_write(&edev->hdac, pin_nid, 0,
325 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
326
327 /* Enable transmission */
328 snd_hdac_codec_write(&edev->hdac, cvt_nid, 0,
329 AC_VERB_SET_DIGI_CONVERT_1, 1);
330
331 /* Category Code (CC) to zero */
332 snd_hdac_codec_write(&edev->hdac, cvt_nid, 0,
333 AC_VERB_SET_DIGI_CONVERT_2, 0);
334
335 snd_hdac_codec_write(&edev->hdac, pin_nid, 0,
336 AC_VERB_SET_CONNECT_SEL, 0);
337
338 return hdac_hdmi_query_cvt_params(&edev->hdac, &dai_map->cvt);
339}
340
341/*
342 * Parse all nodes and store the cvt/pin nids in array
343 * Add one time initialization for pin and cvt widgets
344 */
345static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev)
346{
347 hda_nid_t nid;
348 int i;
349 struct hdac_device *hdac = &edev->hdac;
350 struct hdac_hdmi_priv *hdmi = edev->private_data;
351 int cvt_nid = 0, pin_nid = 0;
352
353 hdac->num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
354 if (!nid || hdac->num_nodes < 0) {
355 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
356 return -EINVAL;
357 }
358
359 hdac->start_nid = nid;
360
361 for (i = 0; i < hdac->num_nodes; i++, nid++) {
362 unsigned int caps;
363 unsigned int type;
364
365 caps = get_wcaps(hdac, nid);
366 type = get_wcaps_type(caps);
367
368 if (!(caps & AC_WCAP_DIGITAL))
369 continue;
370
371 switch (type) {
372
373 case AC_WID_AUD_OUT:
374 hdmi->cvt_nid[cvt_nid] = nid;
375 cvt_nid++;
376 break;
377
378 case AC_WID_PIN:
379 hdmi->pin_nid[pin_nid] = nid;
380 pin_nid++;
381 break;
382 }
383 }
384
385 hdac->end_nid = nid;
386
387 if (!pin_nid || !cvt_nid)
388 return -EIO;
389
390 /*
391 * Currently on board only 1 pin and 1 converter is enabled for
392 * simplification, more will be added eventually
393 * So using fixed map for dai_id:pin:cvt
394 */
395 return hdac_hdmi_init_dai_map(edev, &hdmi->dai_map[0], hdmi->pin_nid[0],
396 hdmi->cvt_nid[0], 0);
397}
398
399static int hdmi_codec_probe(struct snd_soc_codec *codec)
400{
401 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
402 struct hdac_hdmi_priv *hdmi = edev->private_data;
403 struct snd_soc_dapm_context *dapm =
404 snd_soc_component_get_dapm(&codec->component);
405
406 edev->scodec = codec;
407
408 create_fill_widget_route_map(dapm, &hdmi->dai_map[0]);
409
410 /* Imp: Store the card pointer in hda_codec */
411 edev->card = dapm->card->snd_card;
412
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530413 /*
414 * hdac_device core already sets the state to active and calls
415 * get_noresume. So enable runtime and set the device to suspend.
416 */
417 pm_runtime_enable(&edev->hdac.dev);
418 pm_runtime_put(&edev->hdac.dev);
419 pm_runtime_suspend(&edev->hdac.dev);
420
421 return 0;
422}
423
424static int hdmi_codec_remove(struct snd_soc_codec *codec)
425{
426 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
427
428 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530429 return 0;
430}
431
432static struct snd_soc_codec_driver hdmi_hda_codec = {
433 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530434 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530435 .idle_bias_off = true,
436};
437
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530438static struct snd_soc_dai_ops hdmi_dai_ops = {
439 .startup = hdac_hdmi_pcm_open,
440 .shutdown = hdac_hdmi_pcm_close,
441 .hw_params = hdac_hdmi_set_hw_params,
442 .prepare = hdac_hdmi_playback_prepare,
443 .hw_free = hdac_hdmi_playback_cleanup,
444};
445
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530446static struct snd_soc_dai_driver hdmi_dais[] = {
447 { .name = "intel-hdmi-hif1",
448 .playback = {
449 .stream_name = "hif1",
450 .channels_min = 2,
451 .channels_max = 2,
452 .rates = SNDRV_PCM_RATE_32000 |
453 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
454 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
455 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
456 .formats = SNDRV_PCM_FMTBIT_S16_LE |
457 SNDRV_PCM_FMTBIT_S20_3LE |
458 SNDRV_PCM_FMTBIT_S24_LE |
459 SNDRV_PCM_FMTBIT_S32_LE,
460
461 },
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530462 .ops = &hdmi_dai_ops,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530463 },
464};
465
466static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
467{
468 struct hdac_device *codec = &edev->hdac;
469 struct hdac_hdmi_priv *hdmi_priv;
470 int ret = 0;
471
472 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
473 if (hdmi_priv == NULL)
474 return -ENOMEM;
475
476 edev->private_data = hdmi_priv;
477
478 dev_set_drvdata(&codec->dev, edev);
479
480 ret = hdac_hdmi_parse_and_map_nid(edev);
481 if (ret < 0)
482 return ret;
483
484 /* ASoC specific initialization */
485 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
486 hdmi_dais, ARRAY_SIZE(hdmi_dais));
487}
488
489static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
490{
491 snd_soc_unregister_codec(&edev->hdac.dev);
492
493 return 0;
494}
495
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530496#ifdef CONFIG_PM
497static int hdac_hdmi_runtime_suspend(struct device *dev)
498{
499 struct hdac_ext_device *edev = to_hda_ext_device(dev);
500 struct hdac_device *hdac = &edev->hdac;
501
502 dev_dbg(dev, "Enter: %s\n", __func__);
503
504 /* Power down afg */
505 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
506 snd_hdac_codec_write(hdac, hdac->afg, 0,
507 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
508
509 return 0;
510}
511
512static int hdac_hdmi_runtime_resume(struct device *dev)
513{
514 struct hdac_ext_device *edev = to_hda_ext_device(dev);
515 struct hdac_device *hdac = &edev->hdac;
516
517 dev_dbg(dev, "Enter: %s\n", __func__);
518
519 /* Power up afg */
520 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
521 snd_hdac_codec_write(hdac, hdac->afg, 0,
522 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
523
524 return 0;
525}
526#else
527#define hdac_hdmi_runtime_suspend NULL
528#define hdac_hdmi_runtime_resume NULL
529#endif
530
531static const struct dev_pm_ops hdac_hdmi_pm = {
532 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
533};
534
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530535static const struct hda_device_id hdmi_list[] = {
536 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
537 {}
538};
539
540MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
541
542static struct hdac_ext_driver hdmi_driver = {
543 . hdac = {
544 .driver = {
545 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530546 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530547 },
548 .id_table = hdmi_list,
549 },
550 .probe = hdac_hdmi_dev_probe,
551 .remove = hdac_hdmi_dev_remove,
552};
553
554static int __init hdmi_init(void)
555{
556 return snd_hda_ext_driver_register(&hdmi_driver);
557}
558
559static void __exit hdmi_exit(void)
560{
561 snd_hda_ext_driver_unregister(&hdmi_driver);
562}
563
564module_init(hdmi_init);
565module_exit(hdmi_exit);
566
567MODULE_LICENSE("GPL v2");
568MODULE_DESCRIPTION("HDMI HD codec");
569MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
570MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");