2 * skl-pcm.c -ASoC HDA Platform driver file implementing PCM functionality
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Jeeja KP <jeeja.kp@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/pci.h>
23 #include <linux/pm_runtime.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
27 #include "skl-topology.h"
33 static struct snd_pcm_hardware azx_pcm_hw = {
34 .info = (SNDRV_PCM_INFO_MMAP |
35 SNDRV_PCM_INFO_INTERLEAVED |
36 SNDRV_PCM_INFO_BLOCK_TRANSFER |
37 SNDRV_PCM_INFO_MMAP_VALID |
38 SNDRV_PCM_INFO_PAUSE |
39 SNDRV_PCM_INFO_SYNC_START |
40 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
41 SNDRV_PCM_INFO_HAS_LINK_ATIME |
42 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
43 .formats = SNDRV_PCM_FMTBIT_S16_LE |
44 SNDRV_PCM_FMTBIT_S32_LE |
45 SNDRV_PCM_FMTBIT_S24_LE,
46 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
51 .channels_max = HDA_QUAD,
52 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
53 .period_bytes_min = 128,
54 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
56 .periods_max = AZX_MAX_FRAG,
61 struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
63 return substream->runtime->private_data;
66 static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
68 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
69 struct hdac_stream *hstream = hdac_stream(stream);
70 struct hdac_bus *bus = hstream->bus;
72 return hbus_to_ebus(bus);
75 static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
76 struct snd_pcm_substream *substream,
79 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
81 hdac_stream(stream)->bufsize = 0;
82 hdac_stream(stream)->period_bytes = 0;
83 hdac_stream(stream)->format_val = 0;
85 return snd_pcm_lib_malloc_pages(substream, size);
88 static int skl_substream_free_pages(struct hdac_bus *bus,
89 struct snd_pcm_substream *substream)
91 return snd_pcm_lib_free_pages(substream);
94 static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
95 struct snd_pcm_runtime *runtime)
97 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
99 /* avoid wrap-around with wall-clock */
100 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
104 static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
107 return HDAC_EXT_STREAM_TYPE_HOST;
109 return HDAC_EXT_STREAM_TYPE_COUPLED;
113 * check if the stream opened is marked as ignore_suspend by machine, if so
114 * then enable suspend_active refcount
116 * The count supend_active does not need lock as it is used in open/close
117 * and suspend context
119 static void skl_set_suspend_active(struct snd_pcm_substream *substream,
120 struct snd_soc_dai *dai, bool enable)
122 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
123 struct snd_soc_dapm_widget *w;
124 struct skl *skl = ebus_to_skl(ebus);
126 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
127 w = dai->playback_widget;
129 w = dai->capture_widget;
131 if (w->ignore_suspend && enable)
132 skl->supend_active++;
133 else if (w->ignore_suspend && !enable)
134 skl->supend_active--;
137 static int skl_pcm_open(struct snd_pcm_substream *substream,
138 struct snd_soc_dai *dai)
140 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
141 struct hdac_ext_stream *stream;
142 struct snd_pcm_runtime *runtime = substream->runtime;
143 struct skl_dma_params *dma_params;
145 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
147 stream = snd_hdac_ext_stream_assign(ebus, substream,
148 skl_get_host_stream_type(ebus));
152 skl_set_pcm_constrains(ebus, runtime);
155 * disable WALLCLOCK timestamps for capture streams
156 * until we figure out how to handle digital inputs
158 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
159 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
160 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
163 runtime->private_data = stream;
165 dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
169 dma_params->stream_tag = hdac_stream(stream)->stream_tag;
170 snd_soc_dai_set_dma_data(dai, substream, dma_params);
172 dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
173 dma_params->stream_tag);
174 skl_set_suspend_active(substream, dai, true);
175 snd_pcm_set_sync(substream);
180 static int skl_get_format(struct snd_pcm_substream *substream,
181 struct snd_soc_dai *dai)
183 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
184 struct skl_dma_params *dma_params;
185 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
189 struct snd_pcm_runtime *runtime = substream->runtime;
191 format_val = snd_hdac_calc_stream_format(runtime->rate,
196 struct snd_soc_dai *codec_dai = rtd->codec_dai;
198 dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
200 format_val = dma_params->format;
206 static int skl_pcm_prepare(struct snd_pcm_substream *substream,
207 struct snd_soc_dai *dai)
209 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
210 unsigned int format_val;
213 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
215 format_val = skl_get_format(substream, dai);
216 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
217 hdac_stream(stream)->stream_tag, format_val);
218 snd_hdac_stream_reset(hdac_stream(stream));
220 err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
224 err = snd_hdac_stream_setup(hdac_stream(stream));
228 hdac_stream(stream)->prepared = 1;
233 static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params,
235 struct snd_soc_dai *dai)
237 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
238 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
239 struct snd_pcm_runtime *runtime = substream->runtime;
240 struct skl_pipe_params p_params = {0};
241 struct skl_module_cfg *m_cfg;
244 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
245 ret = skl_substream_alloc_pages(ebus, substream,
246 params_buffer_bytes(params));
250 dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
251 runtime->rate, runtime->channels, runtime->format);
253 dma_id = hdac_stream(stream)->stream_tag - 1;
254 dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
256 p_params.s_fmt = snd_pcm_format_width(params_format(params));
257 p_params.ch = params_channels(params);
258 p_params.s_freq = params_rate(params);
259 p_params.host_dma_id = dma_id;
260 p_params.stream = substream->stream;
262 m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
264 skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
269 static void skl_pcm_close(struct snd_pcm_substream *substream,
270 struct snd_soc_dai *dai)
272 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
273 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
274 struct skl_dma_params *dma_params = NULL;
276 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
278 snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
280 dma_params = snd_soc_dai_get_dma_data(dai, substream);
282 * now we should set this to NULL as we are freeing by the
285 snd_soc_dai_set_dma_data(dai, substream, NULL);
286 skl_set_suspend_active(substream, dai, false);
291 static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
292 struct snd_soc_dai *dai)
294 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
295 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
297 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
299 snd_hdac_stream_cleanup(hdac_stream(stream));
300 hdac_stream(stream)->prepared = 0;
302 return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
305 static int skl_be_hw_params(struct snd_pcm_substream *substream,
306 struct snd_pcm_hw_params *params,
307 struct snd_soc_dai *dai)
309 struct skl_pipe_params p_params = {0};
311 p_params.s_fmt = snd_pcm_format_width(params_format(params));
312 p_params.ch = params_channels(params);
313 p_params.s_freq = params_rate(params);
314 p_params.stream = substream->stream;
316 return skl_tplg_be_update_params(dai, &p_params);
319 static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
322 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
323 struct hdac_bus *bus = ebus_to_hbus(ebus);
324 struct hdac_ext_stream *stream;
326 unsigned long cookie;
327 struct hdac_stream *hstr;
329 stream = get_hdac_ext_stream(substream);
330 hstr = hdac_stream(stream);
336 case SNDRV_PCM_TRIGGER_START:
337 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
338 case SNDRV_PCM_TRIGGER_RESUME:
342 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
343 case SNDRV_PCM_TRIGGER_SUSPEND:
344 case SNDRV_PCM_TRIGGER_STOP:
352 spin_lock_irqsave(&bus->reg_lock, cookie);
355 snd_hdac_stream_start(hdac_stream(stream), true);
356 snd_hdac_stream_timecounter_init(hstr, 0);
358 snd_hdac_stream_stop(hdac_stream(stream));
361 spin_unlock_irqrestore(&bus->reg_lock, cookie);
366 static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
367 struct snd_soc_dai *dai)
369 struct skl *skl = get_skl_ctx(dai->dev);
370 struct skl_sst *ctx = skl->skl_sst;
371 struct skl_module_cfg *mconfig;
372 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
373 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
376 mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
381 case SNDRV_PCM_TRIGGER_RESUME:
382 skl_pcm_prepare(substream, dai);
383 case SNDRV_PCM_TRIGGER_START:
384 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
386 * Start HOST DMA and Start FE Pipe.This is to make sure that
387 * there are no underrun/overrun in the case when the FE
388 * pipeline is started but there is a delay in starting the
389 * DMA channel on the host.
391 snd_hdac_ext_stream_decouple(ebus, stream, true);
392 ret = skl_decoupled_trigger(substream, cmd);
395 return skl_run_pipe(ctx, mconfig->pipe);
398 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
399 case SNDRV_PCM_TRIGGER_SUSPEND:
400 case SNDRV_PCM_TRIGGER_STOP:
402 * Stop FE Pipe first and stop DMA. This is to make sure that
403 * there are no underrun/overrun in the case if there is a delay
404 * between the two operations.
406 ret = skl_stop_pipe(ctx, mconfig->pipe);
410 ret = skl_decoupled_trigger(substream, cmd);
411 if (cmd == SNDRV_PCM_TRIGGER_SUSPEND)
412 snd_hdac_ext_stream_decouple(ebus, stream, false);
422 static int skl_link_hw_params(struct snd_pcm_substream *substream,
423 struct snd_pcm_hw_params *params,
424 struct snd_soc_dai *dai)
426 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
427 struct hdac_ext_stream *link_dev;
428 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
429 struct skl_dma_params *dma_params;
430 struct snd_soc_dai *codec_dai = rtd->codec_dai;
431 struct skl_pipe_params p_params = {0};
433 link_dev = snd_hdac_ext_stream_assign(ebus, substream,
434 HDAC_EXT_STREAM_TYPE_LINK);
438 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
440 /* set the stream tag in the codec dai dma params */
441 dma_params = (struct skl_dma_params *)
442 snd_soc_dai_get_dma_data(codec_dai, substream);
444 dma_params->stream_tag = hdac_stream(link_dev)->stream_tag;
445 snd_soc_dai_set_dma_data(codec_dai, substream, (void *)dma_params);
447 p_params.s_fmt = snd_pcm_format_width(params_format(params));
448 p_params.ch = params_channels(params);
449 p_params.s_freq = params_rate(params);
450 p_params.stream = substream->stream;
451 p_params.link_dma_id = hdac_stream(link_dev)->stream_tag - 1;
453 return skl_tplg_be_update_params(dai, &p_params);
456 static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
457 struct snd_soc_dai *dai)
459 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
460 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
461 struct hdac_ext_stream *link_dev =
462 snd_soc_dai_get_dma_data(dai, substream);
463 unsigned int format_val = 0;
464 struct skl_dma_params *dma_params;
465 struct snd_soc_dai *codec_dai = rtd->codec_dai;
466 struct hdac_ext_link *link;
468 if (link_dev->link_prepared) {
469 dev_dbg(dai->dev, "already stream is prepared - returning\n");
473 dma_params = (struct skl_dma_params *)
474 snd_soc_dai_get_dma_data(codec_dai, substream);
476 format_val = dma_params->format;
477 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
478 hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
480 snd_hdac_ext_link_stream_reset(link_dev);
482 snd_hdac_ext_link_stream_setup(link_dev, format_val);
484 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
488 snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
489 link_dev->link_prepared = 1;
494 static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
495 int cmd, struct snd_soc_dai *dai)
497 struct hdac_ext_stream *link_dev =
498 snd_soc_dai_get_dma_data(dai, substream);
500 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
502 case SNDRV_PCM_TRIGGER_START:
503 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
504 case SNDRV_PCM_TRIGGER_RESUME:
505 snd_hdac_ext_link_stream_start(link_dev);
508 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
509 case SNDRV_PCM_TRIGGER_SUSPEND:
510 case SNDRV_PCM_TRIGGER_STOP:
511 snd_hdac_ext_link_stream_clear(link_dev);
520 static int skl_link_hw_free(struct snd_pcm_substream *substream,
521 struct snd_soc_dai *dai)
523 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
524 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
525 struct hdac_ext_stream *link_dev =
526 snd_soc_dai_get_dma_data(dai, substream);
527 struct hdac_ext_link *link;
529 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
531 link_dev->link_prepared = 0;
533 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
537 snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
538 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
542 static struct snd_soc_dai_ops skl_pcm_dai_ops = {
543 .startup = skl_pcm_open,
544 .shutdown = skl_pcm_close,
545 .prepare = skl_pcm_prepare,
546 .hw_params = skl_pcm_hw_params,
547 .hw_free = skl_pcm_hw_free,
548 .trigger = skl_pcm_trigger,
551 static struct snd_soc_dai_ops skl_dmic_dai_ops = {
552 .hw_params = skl_be_hw_params,
555 static struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
556 .hw_params = skl_be_hw_params,
559 static struct snd_soc_dai_ops skl_link_dai_ops = {
560 .prepare = skl_link_pcm_prepare,
561 .hw_params = skl_link_hw_params,
562 .hw_free = skl_link_hw_free,
563 .trigger = skl_link_pcm_trigger,
566 static struct snd_soc_dai_driver skl_platform_dai[] = {
568 .name = "System Pin",
569 .ops = &skl_pcm_dai_ops,
571 .stream_name = "System Playback",
572 .channels_min = HDA_MONO,
573 .channels_max = HDA_STEREO,
574 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
575 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
578 .stream_name = "System Capture",
579 .channels_min = HDA_MONO,
580 .channels_max = HDA_STEREO,
581 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
582 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
586 .name = "Reference Pin",
587 .ops = &skl_pcm_dai_ops,
589 .stream_name = "Reference Capture",
590 .channels_min = HDA_MONO,
591 .channels_max = HDA_QUAD,
592 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
593 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
597 .name = "Deepbuffer Pin",
598 .ops = &skl_pcm_dai_ops,
600 .stream_name = "Deepbuffer Playback",
601 .channels_min = HDA_STEREO,
602 .channels_max = HDA_STEREO,
603 .rates = SNDRV_PCM_RATE_48000,
604 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
608 .name = "LowLatency Pin",
609 .ops = &skl_pcm_dai_ops,
611 .stream_name = "Low Latency Playback",
612 .channels_min = HDA_STEREO,
613 .channels_max = HDA_STEREO,
614 .rates = SNDRV_PCM_RATE_48000,
615 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
620 .ops = &skl_pcm_dai_ops,
622 .stream_name = "DMIC Capture",
623 .channels_min = HDA_MONO,
624 .channels_max = HDA_QUAD,
625 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
626 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
633 .ops = &skl_be_ssp_dai_ops,
635 .stream_name = "ssp0 Tx",
636 .channels_min = HDA_STEREO,
637 .channels_max = HDA_STEREO,
638 .rates = SNDRV_PCM_RATE_48000,
639 .formats = SNDRV_PCM_FMTBIT_S16_LE,
642 .stream_name = "ssp0 Rx",
643 .channels_min = HDA_STEREO,
644 .channels_max = HDA_STEREO,
645 .rates = SNDRV_PCM_RATE_48000,
646 .formats = SNDRV_PCM_FMTBIT_S16_LE,
651 .ops = &skl_be_ssp_dai_ops,
653 .stream_name = "ssp1 Tx",
654 .channels_min = HDA_STEREO,
655 .channels_max = HDA_STEREO,
656 .rates = SNDRV_PCM_RATE_48000,
657 .formats = SNDRV_PCM_FMTBIT_S16_LE,
660 .stream_name = "ssp1 Rx",
661 .channels_min = HDA_STEREO,
662 .channels_max = HDA_STEREO,
663 .rates = SNDRV_PCM_RATE_48000,
664 .formats = SNDRV_PCM_FMTBIT_S16_LE,
669 .ops = &skl_link_dai_ops,
671 .stream_name = "iDisp Tx",
672 .channels_min = HDA_STEREO,
673 .channels_max = HDA_STEREO,
674 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
675 .formats = SNDRV_PCM_FMTBIT_S16_LE,
679 .name = "DMIC01 Pin",
680 .ops = &skl_dmic_dai_ops,
682 .stream_name = "DMIC01 Rx",
683 .channels_min = HDA_MONO,
684 .channels_max = HDA_QUAD,
685 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
686 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
690 .name = "HD-Codec Pin",
691 .ops = &skl_link_dai_ops,
693 .stream_name = "HD-Codec Tx",
694 .channels_min = HDA_STEREO,
695 .channels_max = HDA_STEREO,
696 .rates = SNDRV_PCM_RATE_48000,
697 .formats = SNDRV_PCM_FMTBIT_S16_LE,
700 .stream_name = "HD-Codec Rx",
701 .channels_min = HDA_STEREO,
702 .channels_max = HDA_STEREO,
703 .rates = SNDRV_PCM_RATE_48000,
704 .formats = SNDRV_PCM_FMTBIT_S16_LE,
709 static int skl_platform_open(struct snd_pcm_substream *substream)
711 struct snd_pcm_runtime *runtime;
712 struct snd_soc_pcm_runtime *rtd = substream->private_data;
713 struct snd_soc_dai_link *dai_link = rtd->dai_link;
715 dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
716 dai_link->cpu_dai_name);
718 runtime = substream->runtime;
719 snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
724 static int skl_coupled_trigger(struct snd_pcm_substream *substream,
727 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
728 struct hdac_bus *bus = ebus_to_hbus(ebus);
729 struct hdac_ext_stream *stream;
730 struct snd_pcm_substream *s;
733 unsigned long cookie;
734 struct hdac_stream *hstr;
736 stream = get_hdac_ext_stream(substream);
737 hstr = hdac_stream(stream);
739 dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
745 case SNDRV_PCM_TRIGGER_START:
746 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
747 case SNDRV_PCM_TRIGGER_RESUME:
751 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
752 case SNDRV_PCM_TRIGGER_SUSPEND:
753 case SNDRV_PCM_TRIGGER_STOP:
761 snd_pcm_group_for_each_entry(s, substream) {
762 if (s->pcm->card != substream->pcm->card)
764 stream = get_hdac_ext_stream(s);
765 sbits |= 1 << hdac_stream(stream)->index;
766 snd_pcm_trigger_done(s, substream);
769 spin_lock_irqsave(&bus->reg_lock, cookie);
771 /* first, set SYNC bits of corresponding streams */
772 snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
774 snd_pcm_group_for_each_entry(s, substream) {
775 if (s->pcm->card != substream->pcm->card)
777 stream = get_hdac_ext_stream(s);
779 snd_hdac_stream_start(hdac_stream(stream), true);
781 snd_hdac_stream_stop(hdac_stream(stream));
783 spin_unlock_irqrestore(&bus->reg_lock, cookie);
785 snd_hdac_stream_sync(hstr, start, sbits);
787 spin_lock_irqsave(&bus->reg_lock, cookie);
789 /* reset SYNC bits */
790 snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
792 snd_hdac_stream_timecounter_init(hstr, sbits);
793 spin_unlock_irqrestore(&bus->reg_lock, cookie);
798 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
801 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
804 return skl_coupled_trigger(substream, cmd);
809 /* calculate runtime delay from LPIB */
810 static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
811 struct hdac_ext_stream *sstream,
814 struct hdac_bus *bus = ebus_to_hbus(ebus);
815 struct hdac_stream *hstream = hdac_stream(sstream);
816 struct snd_pcm_substream *substream = hstream->substream;
817 int stream = substream->stream;
818 unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
821 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
822 delay = pos - lpib_pos;
824 delay = lpib_pos - pos;
827 if (delay >= hstream->delay_negative_threshold)
830 delay += hstream->bufsize;
833 if (delay >= hstream->period_bytes) {
835 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
836 delay, hstream->period_bytes);
840 return bytes_to_frames(substream->runtime, delay);
843 static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
846 struct hdac_stream *hstr = hdac_stream(hstream);
847 struct snd_pcm_substream *substream = hstr->substream;
848 struct hdac_ext_bus *ebus;
852 /* use the position buffer as default */
853 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
855 if (pos >= hdac_stream(hstream)->bufsize)
858 if (substream->runtime) {
859 ebus = get_bus_ctx(substream);
860 delay = skl_get_delay_from_lpib(ebus, hstream, pos)
862 substream->runtime->delay += delay;
868 static snd_pcm_uframes_t skl_platform_pcm_pointer
869 (struct snd_pcm_substream *substream)
871 struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
873 return bytes_to_frames(substream->runtime,
874 skl_get_position(hstream, 0));
877 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
880 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
881 struct snd_soc_dai *codec_dai = rtd->codec_dai;
882 u64 codec_frames, codec_nsecs;
884 if (!codec_dai->driver->ops->delay)
887 codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
888 codec_nsecs = div_u64(codec_frames * 1000000000LL,
889 substream->runtime->rate);
891 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
892 return nsec + codec_nsecs;
894 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
897 static int skl_get_time_info(struct snd_pcm_substream *substream,
898 struct timespec *system_ts, struct timespec *audio_ts,
899 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
900 struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
902 struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
903 struct hdac_stream *hstr = hdac_stream(sstream);
906 if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
907 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
909 snd_pcm_gettime(substream->runtime, system_ts);
911 nsec = timecounter_read(&hstr->tc);
912 nsec = div_u64(nsec, 3); /* can be optimized */
913 if (audio_tstamp_config->report_delay)
914 nsec = skl_adjust_codec_delay(substream, nsec);
916 *audio_ts = ns_to_timespec(nsec);
918 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
919 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
920 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
923 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
929 static struct snd_pcm_ops skl_platform_ops = {
930 .open = skl_platform_open,
931 .ioctl = snd_pcm_lib_ioctl,
932 .trigger = skl_platform_pcm_trigger,
933 .pointer = skl_platform_pcm_pointer,
934 .get_time_info = skl_get_time_info,
935 .mmap = snd_pcm_lib_default_mmap,
936 .page = snd_pcm_sgbuf_ops_page,
939 static void skl_pcm_free(struct snd_pcm *pcm)
941 snd_pcm_lib_preallocate_free_for_all(pcm);
944 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
946 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
948 struct snd_soc_dai *dai = rtd->cpu_dai;
949 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
950 struct snd_pcm *pcm = rtd->pcm;
953 struct skl *skl = ebus_to_skl(ebus);
955 if (dai->driver->playback.channels_min ||
956 dai->driver->capture.channels_min) {
957 /* buffer pre-allocation */
958 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
959 if (size > MAX_PREALLOC_SIZE)
960 size = MAX_PREALLOC_SIZE;
961 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
962 SNDRV_DMA_TYPE_DEV_SG,
963 snd_dma_pci_data(skl->pci),
964 size, MAX_PREALLOC_SIZE);
966 dev_err(dai->dev, "dma buffer allocationf fail\n");
974 static int skl_platform_soc_probe(struct snd_soc_platform *platform)
976 struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
979 return skl_tplg_init(platform, ebus);
983 static struct snd_soc_platform_driver skl_platform_drv = {
984 .probe = skl_platform_soc_probe,
985 .ops = &skl_platform_ops,
986 .pcm_new = skl_pcm_new,
987 .pcm_free = skl_pcm_free,
990 static const struct snd_soc_component_driver skl_component = {
994 int skl_platform_register(struct device *dev)
997 struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
998 struct skl *skl = ebus_to_skl(ebus);
1000 INIT_LIST_HEAD(&skl->ppl_list);
1002 ret = snd_soc_register_platform(dev, &skl_platform_drv);
1004 dev_err(dev, "soc platform registration failed %d\n", ret);
1007 ret = snd_soc_register_component(dev, &skl_component,
1009 ARRAY_SIZE(skl_platform_dai));
1011 dev_err(dev, "soc component registration failed %d\n", ret);
1012 snd_soc_unregister_platform(dev);
1019 int skl_platform_unregister(struct device *dev)
1021 snd_soc_unregister_component(dev);
1022 snd_soc_unregister_platform(dev);