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"
32 static struct snd_pcm_hardware azx_pcm_hw = {
33 .info = (SNDRV_PCM_INFO_MMAP |
34 SNDRV_PCM_INFO_INTERLEAVED |
35 SNDRV_PCM_INFO_BLOCK_TRANSFER |
36 SNDRV_PCM_INFO_MMAP_VALID |
37 SNDRV_PCM_INFO_PAUSE |
38 SNDRV_PCM_INFO_SYNC_START |
39 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
40 SNDRV_PCM_INFO_HAS_LINK_ATIME |
41 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
42 .formats = SNDRV_PCM_FMTBIT_S16_LE,
43 .rates = SNDRV_PCM_RATE_48000,
48 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
49 .period_bytes_min = 128,
50 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
52 .periods_max = AZX_MAX_FRAG,
57 struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
59 return substream->runtime->private_data;
62 static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
64 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
65 struct hdac_stream *hstream = hdac_stream(stream);
66 struct hdac_bus *bus = hstream->bus;
68 return hbus_to_ebus(bus);
71 static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
72 struct snd_pcm_substream *substream,
75 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
77 hdac_stream(stream)->bufsize = 0;
78 hdac_stream(stream)->period_bytes = 0;
79 hdac_stream(stream)->format_val = 0;
81 return snd_pcm_lib_malloc_pages(substream, size);
84 static int skl_substream_free_pages(struct hdac_bus *bus,
85 struct snd_pcm_substream *substream)
87 return snd_pcm_lib_free_pages(substream);
90 static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
91 struct snd_pcm_runtime *runtime)
93 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
95 /* avoid wrap-around with wall-clock */
96 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
100 static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
103 return HDAC_EXT_STREAM_TYPE_HOST;
105 return HDAC_EXT_STREAM_TYPE_COUPLED;
108 static int skl_pcm_open(struct snd_pcm_substream *substream,
109 struct snd_soc_dai *dai)
111 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
112 struct hdac_ext_stream *stream;
113 struct snd_pcm_runtime *runtime = substream->runtime;
114 struct skl_dma_params *dma_params;
117 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
118 ret = pm_runtime_get_sync(dai->dev);
122 stream = snd_hdac_ext_stream_assign(ebus, substream,
123 skl_get_host_stream_type(ebus));
127 skl_set_pcm_constrains(ebus, runtime);
130 * disable WALLCLOCK timestamps for capture streams
131 * until we figure out how to handle digital inputs
133 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
134 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
135 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
138 runtime->private_data = stream;
140 dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
144 dma_params->stream_tag = hdac_stream(stream)->stream_tag;
145 snd_soc_dai_set_dma_data(dai, substream, dma_params);
147 dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
148 dma_params->stream_tag);
149 snd_pcm_set_sync(substream);
154 static int skl_get_format(struct snd_pcm_substream *substream,
155 struct snd_soc_dai *dai)
157 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
158 struct skl_dma_params *dma_params;
159 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
163 struct snd_pcm_runtime *runtime = substream->runtime;
165 format_val = snd_hdac_calc_stream_format(runtime->rate,
170 struct snd_soc_dai *codec_dai = rtd->codec_dai;
172 dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
174 format_val = dma_params->format;
180 static int skl_pcm_prepare(struct snd_pcm_substream *substream,
181 struct snd_soc_dai *dai)
183 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
184 unsigned int format_val;
187 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
188 if (hdac_stream(stream)->prepared) {
189 dev_dbg(dai->dev, "already stream is prepared - returning\n");
193 format_val = skl_get_format(substream, dai);
194 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
195 hdac_stream(stream)->stream_tag, format_val);
196 snd_hdac_stream_reset(hdac_stream(stream));
198 err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
202 err = snd_hdac_stream_setup(hdac_stream(stream));
206 hdac_stream(stream)->prepared = 1;
211 static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
212 struct snd_pcm_hw_params *params,
213 struct snd_soc_dai *dai)
215 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
216 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
217 struct snd_pcm_runtime *runtime = substream->runtime;
218 struct skl_pipe_params p_params = {0};
219 struct skl_module_cfg *m_cfg;
222 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
223 ret = skl_substream_alloc_pages(ebus, substream,
224 params_buffer_bytes(params));
228 dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
229 runtime->rate, runtime->channels, runtime->format);
231 dma_id = hdac_stream(stream)->stream_tag - 1;
232 dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
234 p_params.s_fmt = snd_pcm_format_width(params_format(params));
235 p_params.ch = params_channels(params);
236 p_params.s_freq = params_rate(params);
237 p_params.host_dma_id = dma_id;
238 p_params.stream = substream->stream;
240 m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
242 skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
247 static void skl_pcm_close(struct snd_pcm_substream *substream,
248 struct snd_soc_dai *dai)
250 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
251 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
252 struct skl_dma_params *dma_params = NULL;
254 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
256 snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
258 dma_params = snd_soc_dai_get_dma_data(dai, substream);
260 * now we should set this to NULL as we are freeing by the
263 snd_soc_dai_set_dma_data(dai, substream, NULL);
265 pm_runtime_mark_last_busy(dai->dev);
266 pm_runtime_put_autosuspend(dai->dev);
270 static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
271 struct snd_soc_dai *dai)
273 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
274 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
276 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
278 snd_hdac_stream_cleanup(hdac_stream(stream));
279 hdac_stream(stream)->prepared = 0;
281 return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
284 static int skl_be_hw_params(struct snd_pcm_substream *substream,
285 struct snd_pcm_hw_params *params,
286 struct snd_soc_dai *dai)
288 struct skl_pipe_params p_params = {0};
290 p_params.s_fmt = snd_pcm_format_width(params_format(params));
291 p_params.ch = params_channels(params);
292 p_params.s_freq = params_rate(params);
293 p_params.stream = substream->stream;
294 skl_tplg_be_update_params(dai, &p_params);
299 static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
300 struct snd_soc_dai *dai)
302 struct skl *skl = get_skl_ctx(dai->dev);
303 struct skl_sst *ctx = skl->skl_sst;
304 struct skl_module_cfg *mconfig;
306 mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
311 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
312 case SNDRV_PCM_TRIGGER_RESUME:
313 return skl_run_pipe(ctx, mconfig->pipe);
315 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
316 case SNDRV_PCM_TRIGGER_SUSPEND:
317 return skl_stop_pipe(ctx, mconfig->pipe);
324 static int skl_link_hw_params(struct snd_pcm_substream *substream,
325 struct snd_pcm_hw_params *params,
326 struct snd_soc_dai *dai)
328 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
329 struct hdac_ext_stream *link_dev;
330 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
331 struct skl_dma_params *dma_params;
332 struct snd_soc_dai *codec_dai = rtd->codec_dai;
333 struct skl_pipe_params p_params = {0};
335 link_dev = snd_hdac_ext_stream_assign(ebus, substream,
336 HDAC_EXT_STREAM_TYPE_LINK);
340 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
342 /* set the stream tag in the codec dai dma params */
343 dma_params = (struct skl_dma_params *)
344 snd_soc_dai_get_dma_data(codec_dai, substream);
346 dma_params->stream_tag = hdac_stream(link_dev)->stream_tag;
347 snd_soc_dai_set_dma_data(codec_dai, substream, (void *)dma_params);
349 p_params.s_fmt = snd_pcm_format_width(params_format(params));
350 p_params.ch = params_channels(params);
351 p_params.s_freq = params_rate(params);
352 p_params.stream = substream->stream;
353 p_params.link_dma_id = hdac_stream(link_dev)->stream_tag - 1;
355 skl_tplg_be_update_params(dai, &p_params);
360 static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
361 struct snd_soc_dai *dai)
363 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
364 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
365 struct hdac_ext_stream *link_dev =
366 snd_soc_dai_get_dma_data(dai, substream);
367 unsigned int format_val = 0;
368 struct skl_dma_params *dma_params;
369 struct snd_soc_dai *codec_dai = rtd->codec_dai;
370 struct hdac_ext_link *link;
372 if (link_dev->link_prepared) {
373 dev_dbg(dai->dev, "already stream is prepared - returning\n");
377 dma_params = (struct skl_dma_params *)
378 snd_soc_dai_get_dma_data(codec_dai, substream);
380 format_val = dma_params->format;
381 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
382 hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
384 snd_hdac_ext_link_stream_reset(link_dev);
386 snd_hdac_ext_link_stream_setup(link_dev, format_val);
388 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
392 snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
393 link_dev->link_prepared = 1;
398 static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
399 int cmd, struct snd_soc_dai *dai)
401 struct hdac_ext_stream *link_dev =
402 snd_soc_dai_get_dma_data(dai, substream);
404 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
406 case SNDRV_PCM_TRIGGER_START:
407 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
408 case SNDRV_PCM_TRIGGER_RESUME:
409 snd_hdac_ext_link_stream_start(link_dev);
412 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
413 case SNDRV_PCM_TRIGGER_SUSPEND:
414 case SNDRV_PCM_TRIGGER_STOP:
415 snd_hdac_ext_link_stream_clear(link_dev);
424 static int skl_link_hw_free(struct snd_pcm_substream *substream,
425 struct snd_soc_dai *dai)
427 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
428 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
429 struct hdac_ext_stream *link_dev =
430 snd_soc_dai_get_dma_data(dai, substream);
431 struct hdac_ext_link *link;
433 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
435 link_dev->link_prepared = 0;
437 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
441 snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
442 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
446 static int skl_be_startup(struct snd_pcm_substream *substream,
447 struct snd_soc_dai *dai)
449 return pm_runtime_get_sync(dai->dev);
452 static void skl_be_shutdown(struct snd_pcm_substream *substream,
453 struct snd_soc_dai *dai)
455 pm_runtime_mark_last_busy(dai->dev);
456 pm_runtime_put_autosuspend(dai->dev);
459 static struct snd_soc_dai_ops skl_pcm_dai_ops = {
460 .startup = skl_pcm_open,
461 .shutdown = skl_pcm_close,
462 .prepare = skl_pcm_prepare,
463 .hw_params = skl_pcm_hw_params,
464 .hw_free = skl_pcm_hw_free,
465 .trigger = skl_pcm_trigger,
468 static struct snd_soc_dai_ops skl_dmic_dai_ops = {
469 .startup = skl_be_startup,
470 .hw_params = skl_be_hw_params,
471 .shutdown = skl_be_shutdown,
474 static struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
475 .startup = skl_be_startup,
476 .hw_params = skl_be_hw_params,
477 .shutdown = skl_be_shutdown,
480 static struct snd_soc_dai_ops skl_link_dai_ops = {
481 .startup = skl_be_startup,
482 .prepare = skl_link_pcm_prepare,
483 .hw_params = skl_link_hw_params,
484 .hw_free = skl_link_hw_free,
485 .trigger = skl_link_pcm_trigger,
486 .shutdown = skl_be_shutdown,
489 static struct snd_soc_dai_driver skl_platform_dai[] = {
491 .name = "System Pin",
492 .ops = &skl_pcm_dai_ops,
494 .stream_name = "System Playback",
495 .channels_min = HDA_MONO,
496 .channels_max = HDA_STEREO,
497 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
498 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
501 .stream_name = "System Capture",
502 .channels_min = HDA_MONO,
503 .channels_max = HDA_STEREO,
504 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
505 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
509 .name = "Reference Pin",
510 .ops = &skl_pcm_dai_ops,
512 .stream_name = "Reference Capture",
513 .channels_min = HDA_MONO,
514 .channels_max = HDA_STEREO,
515 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
516 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
520 .name = "Deepbuffer Pin",
521 .ops = &skl_pcm_dai_ops,
523 .stream_name = "Deepbuffer Playback",
524 .channels_min = HDA_STEREO,
525 .channels_max = HDA_STEREO,
526 .rates = SNDRV_PCM_RATE_48000,
527 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
531 .name = "LowLatency Pin",
532 .ops = &skl_pcm_dai_ops,
534 .stream_name = "Low Latency Playback",
535 .channels_min = HDA_STEREO,
536 .channels_max = HDA_STEREO,
537 .rates = SNDRV_PCM_RATE_48000,
538 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
544 .ops = &skl_be_ssp_dai_ops,
546 .stream_name = "ssp0 Tx",
547 .channels_min = HDA_STEREO,
548 .channels_max = HDA_STEREO,
549 .rates = SNDRV_PCM_RATE_48000,
550 .formats = SNDRV_PCM_FMTBIT_S16_LE,
553 .stream_name = "ssp0 Rx",
554 .channels_min = HDA_STEREO,
555 .channels_max = HDA_STEREO,
556 .rates = SNDRV_PCM_RATE_48000,
557 .formats = SNDRV_PCM_FMTBIT_S16_LE,
562 .ops = &skl_link_dai_ops,
564 .stream_name = "iDisp Tx",
565 .channels_min = HDA_STEREO,
566 .channels_max = HDA_STEREO,
567 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
568 .formats = SNDRV_PCM_FMTBIT_S16_LE,
572 .name = "DMIC01 Pin",
573 .ops = &skl_dmic_dai_ops,
575 .stream_name = "DMIC01 Rx",
576 .channels_min = HDA_STEREO,
577 .channels_max = HDA_STEREO,
578 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
579 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
583 .name = "HD-Codec Pin",
584 .ops = &skl_link_dai_ops,
586 .stream_name = "HD-Codec Tx",
587 .channels_min = HDA_STEREO,
588 .channels_max = HDA_STEREO,
589 .rates = SNDRV_PCM_RATE_48000,
590 .formats = SNDRV_PCM_FMTBIT_S16_LE,
593 .stream_name = "HD-Codec Rx",
594 .channels_min = HDA_STEREO,
595 .channels_max = HDA_STEREO,
596 .rates = SNDRV_PCM_RATE_48000,
597 .formats = SNDRV_PCM_FMTBIT_S16_LE,
602 static int skl_platform_open(struct snd_pcm_substream *substream)
604 struct snd_pcm_runtime *runtime;
605 struct snd_soc_pcm_runtime *rtd = substream->private_data;
606 struct snd_soc_dai_link *dai_link = rtd->dai_link;
608 dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
609 dai_link->cpu_dai_name);
611 runtime = substream->runtime;
612 snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
617 static int skl_coupled_trigger(struct snd_pcm_substream *substream,
620 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
621 struct hdac_bus *bus = ebus_to_hbus(ebus);
622 struct hdac_ext_stream *stream;
623 struct snd_pcm_substream *s;
626 unsigned long cookie;
627 struct hdac_stream *hstr;
629 stream = get_hdac_ext_stream(substream);
630 hstr = hdac_stream(stream);
632 dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
638 case SNDRV_PCM_TRIGGER_START:
639 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
640 case SNDRV_PCM_TRIGGER_RESUME:
644 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
645 case SNDRV_PCM_TRIGGER_SUSPEND:
646 case SNDRV_PCM_TRIGGER_STOP:
654 snd_pcm_group_for_each_entry(s, substream) {
655 if (s->pcm->card != substream->pcm->card)
657 stream = get_hdac_ext_stream(s);
658 sbits |= 1 << hdac_stream(stream)->index;
659 snd_pcm_trigger_done(s, substream);
662 spin_lock_irqsave(&bus->reg_lock, cookie);
664 /* first, set SYNC bits of corresponding streams */
665 snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
667 snd_pcm_group_for_each_entry(s, substream) {
668 if (s->pcm->card != substream->pcm->card)
670 stream = get_hdac_ext_stream(s);
672 snd_hdac_stream_start(hdac_stream(stream), true);
674 snd_hdac_stream_stop(hdac_stream(stream));
676 spin_unlock_irqrestore(&bus->reg_lock, cookie);
678 snd_hdac_stream_sync(hstr, start, sbits);
680 spin_lock_irqsave(&bus->reg_lock, cookie);
682 /* reset SYNC bits */
683 snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
685 snd_hdac_stream_timecounter_init(hstr, sbits);
686 spin_unlock_irqrestore(&bus->reg_lock, cookie);
691 static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
694 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
695 struct hdac_bus *bus = ebus_to_hbus(ebus);
696 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
697 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
698 struct hdac_ext_stream *stream;
700 unsigned long cookie;
701 struct hdac_stream *hstr;
703 dev_dbg(bus->dev, "In %s cmd=%d streamname=%s\n", __func__, cmd, cpu_dai->name);
705 stream = get_hdac_ext_stream(substream);
706 hstr = hdac_stream(stream);
712 case SNDRV_PCM_TRIGGER_START:
713 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
714 case SNDRV_PCM_TRIGGER_RESUME:
718 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
719 case SNDRV_PCM_TRIGGER_SUSPEND:
720 case SNDRV_PCM_TRIGGER_STOP:
728 spin_lock_irqsave(&bus->reg_lock, cookie);
731 snd_hdac_stream_start(hdac_stream(stream), true);
733 snd_hdac_stream_stop(hdac_stream(stream));
736 snd_hdac_stream_timecounter_init(hstr, 0);
738 spin_unlock_irqrestore(&bus->reg_lock, cookie);
742 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
745 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
748 return skl_decoupled_trigger(substream, cmd);
750 return skl_coupled_trigger(substream, cmd);
753 /* calculate runtime delay from LPIB */
754 static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
755 struct hdac_ext_stream *sstream,
758 struct hdac_bus *bus = ebus_to_hbus(ebus);
759 struct hdac_stream *hstream = hdac_stream(sstream);
760 struct snd_pcm_substream *substream = hstream->substream;
761 int stream = substream->stream;
762 unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
765 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
766 delay = pos - lpib_pos;
768 delay = lpib_pos - pos;
771 if (delay >= hstream->delay_negative_threshold)
774 delay += hstream->bufsize;
777 if (delay >= hstream->period_bytes) {
779 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
780 delay, hstream->period_bytes);
784 return bytes_to_frames(substream->runtime, delay);
787 static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
790 struct hdac_stream *hstr = hdac_stream(hstream);
791 struct snd_pcm_substream *substream = hstr->substream;
792 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
796 /* use the position buffer as default */
797 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
799 if (pos >= hdac_stream(hstream)->bufsize)
802 if (substream->runtime) {
803 delay = skl_get_delay_from_lpib(ebus, hstream, pos)
805 substream->runtime->delay += delay;
811 static snd_pcm_uframes_t skl_platform_pcm_pointer
812 (struct snd_pcm_substream *substream)
814 struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
816 return bytes_to_frames(substream->runtime,
817 skl_get_position(hstream, 0));
820 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
823 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
824 struct snd_soc_dai *codec_dai = rtd->codec_dai;
825 u64 codec_frames, codec_nsecs;
827 if (!codec_dai->driver->ops->delay)
830 codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
831 codec_nsecs = div_u64(codec_frames * 1000000000LL,
832 substream->runtime->rate);
834 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
835 return nsec + codec_nsecs;
837 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
840 static int skl_get_time_info(struct snd_pcm_substream *substream,
841 struct timespec *system_ts, struct timespec *audio_ts,
842 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
843 struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
845 struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
846 struct hdac_stream *hstr = hdac_stream(sstream);
849 if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
850 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
852 snd_pcm_gettime(substream->runtime, system_ts);
854 nsec = timecounter_read(&hstr->tc);
855 nsec = div_u64(nsec, 3); /* can be optimized */
856 if (audio_tstamp_config->report_delay)
857 nsec = skl_adjust_codec_delay(substream, nsec);
859 *audio_ts = ns_to_timespec(nsec);
861 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
862 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
863 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
866 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
872 static struct snd_pcm_ops skl_platform_ops = {
873 .open = skl_platform_open,
874 .ioctl = snd_pcm_lib_ioctl,
875 .trigger = skl_platform_pcm_trigger,
876 .pointer = skl_platform_pcm_pointer,
877 .get_time_info = skl_get_time_info,
878 .mmap = snd_pcm_lib_default_mmap,
879 .page = snd_pcm_sgbuf_ops_page,
882 static void skl_pcm_free(struct snd_pcm *pcm)
884 snd_pcm_lib_preallocate_free_for_all(pcm);
887 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
889 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
891 struct snd_soc_dai *dai = rtd->cpu_dai;
892 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
893 struct snd_pcm *pcm = rtd->pcm;
896 struct skl *skl = ebus_to_skl(ebus);
898 if (dai->driver->playback.channels_min ||
899 dai->driver->capture.channels_min) {
900 /* buffer pre-allocation */
901 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
902 if (size > MAX_PREALLOC_SIZE)
903 size = MAX_PREALLOC_SIZE;
904 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
905 SNDRV_DMA_TYPE_DEV_SG,
906 snd_dma_pci_data(skl->pci),
907 size, MAX_PREALLOC_SIZE);
909 dev_err(dai->dev, "dma buffer allocationf fail\n");
917 static int skl_platform_soc_probe(struct snd_soc_platform *platform)
919 struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
922 return skl_tplg_init(platform, ebus);
926 static struct snd_soc_platform_driver skl_platform_drv = {
927 .probe = skl_platform_soc_probe,
928 .ops = &skl_platform_ops,
929 .pcm_new = skl_pcm_new,
930 .pcm_free = skl_pcm_free,
933 static const struct snd_soc_component_driver skl_component = {
937 int skl_platform_register(struct device *dev)
940 struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
941 struct skl *skl = ebus_to_skl(ebus);
943 INIT_LIST_HEAD(&skl->ppl_list);
944 INIT_LIST_HEAD(&skl->dapm_path_list);
946 ret = snd_soc_register_platform(dev, &skl_platform_drv);
948 dev_err(dev, "soc platform registration failed %d\n", ret);
951 ret = snd_soc_register_component(dev, &skl_component,
953 ARRAY_SIZE(skl_platform_dai));
955 dev_err(dev, "soc component registration failed %d\n", ret);
956 snd_soc_unregister_platform(dev);
963 int skl_platform_unregister(struct device *dev)
965 snd_soc_unregister_component(dev);
966 snd_soc_unregister_platform(dev);