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>
31 static struct snd_pcm_hardware azx_pcm_hw = {
32 .info = (SNDRV_PCM_INFO_MMAP |
33 SNDRV_PCM_INFO_INTERLEAVED |
34 SNDRV_PCM_INFO_BLOCK_TRANSFER |
35 SNDRV_PCM_INFO_MMAP_VALID |
36 SNDRV_PCM_INFO_PAUSE |
37 SNDRV_PCM_INFO_SYNC_START |
38 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
39 SNDRV_PCM_INFO_HAS_LINK_ATIME |
40 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
41 .formats = SNDRV_PCM_FMTBIT_S16_LE,
42 .rates = SNDRV_PCM_RATE_48000,
47 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
48 .period_bytes_min = 128,
49 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
51 .periods_max = AZX_MAX_FRAG,
56 struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
58 return substream->runtime->private_data;
61 static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
63 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
64 struct hdac_stream *hstream = hdac_stream(stream);
65 struct hdac_bus *bus = hstream->bus;
67 return hbus_to_ebus(bus);
70 static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
71 struct snd_pcm_substream *substream,
74 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
76 hdac_stream(stream)->bufsize = 0;
77 hdac_stream(stream)->period_bytes = 0;
78 hdac_stream(stream)->format_val = 0;
80 return snd_pcm_lib_malloc_pages(substream, size);
83 static int skl_substream_free_pages(struct hdac_bus *bus,
84 struct snd_pcm_substream *substream)
86 return snd_pcm_lib_free_pages(substream);
89 static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
90 struct snd_pcm_runtime *runtime)
92 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
94 /* avoid wrap-around with wall-clock */
95 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
99 static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
102 return HDAC_EXT_STREAM_TYPE_HOST;
104 return HDAC_EXT_STREAM_TYPE_COUPLED;
107 static int skl_pcm_open(struct snd_pcm_substream *substream,
108 struct snd_soc_dai *dai)
110 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
111 struct hdac_ext_stream *stream;
112 struct snd_pcm_runtime *runtime = substream->runtime;
113 struct skl_dma_params *dma_params;
116 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
117 ret = pm_runtime_get_sync(dai->dev);
121 stream = snd_hdac_ext_stream_assign(ebus, substream,
122 skl_get_host_stream_type(ebus));
126 skl_set_pcm_constrains(ebus, runtime);
129 * disable WALLCLOCK timestamps for capture streams
130 * until we figure out how to handle digital inputs
132 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
133 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
134 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
137 runtime->private_data = stream;
139 dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
143 dma_params->stream_tag = hdac_stream(stream)->stream_tag;
144 snd_soc_dai_set_dma_data(dai, substream, dma_params);
146 dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
147 dma_params->stream_tag);
148 snd_pcm_set_sync(substream);
153 static int skl_get_format(struct snd_pcm_substream *substream,
154 struct snd_soc_dai *dai)
156 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
157 struct skl_dma_params *dma_params;
158 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
162 struct snd_pcm_runtime *runtime = substream->runtime;
164 format_val = snd_hdac_calc_stream_format(runtime->rate,
169 struct snd_soc_dai *codec_dai = rtd->codec_dai;
171 dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
173 format_val = dma_params->format;
179 static int skl_pcm_prepare(struct snd_pcm_substream *substream,
180 struct snd_soc_dai *dai)
182 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
183 unsigned int format_val;
186 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
187 if (hdac_stream(stream)->prepared) {
188 dev_dbg(dai->dev, "already stream is prepared - returning\n");
192 format_val = skl_get_format(substream, dai);
193 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
194 hdac_stream(stream)->stream_tag, format_val);
195 snd_hdac_stream_reset(hdac_stream(stream));
197 err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
201 err = snd_hdac_stream_setup(hdac_stream(stream));
205 hdac_stream(stream)->prepared = 1;
210 static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
211 struct snd_pcm_hw_params *params,
212 struct snd_soc_dai *dai)
214 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
215 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
216 struct snd_pcm_runtime *runtime = substream->runtime;
219 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
220 ret = skl_substream_alloc_pages(ebus, substream,
221 params_buffer_bytes(params));
225 dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
226 runtime->rate, runtime->channels, runtime->format);
228 dma_id = hdac_stream(stream)->stream_tag - 1;
229 dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
234 static void skl_pcm_close(struct snd_pcm_substream *substream,
235 struct snd_soc_dai *dai)
237 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
238 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
239 struct skl_dma_params *dma_params = NULL;
241 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
243 snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
245 dma_params = snd_soc_dai_get_dma_data(dai, substream);
247 * now we should set this to NULL as we are freeing by the
250 snd_soc_dai_set_dma_data(dai, substream, NULL);
252 pm_runtime_mark_last_busy(dai->dev);
253 pm_runtime_put_autosuspend(dai->dev);
257 static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
258 struct snd_soc_dai *dai)
260 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
261 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
263 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
265 snd_hdac_stream_cleanup(hdac_stream(stream));
266 hdac_stream(stream)->prepared = 0;
268 return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
271 static int skl_link_hw_params(struct snd_pcm_substream *substream,
272 struct snd_pcm_hw_params *params,
273 struct snd_soc_dai *dai)
275 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
276 struct hdac_ext_stream *link_dev;
277 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
278 struct skl_dma_params *dma_params;
279 struct snd_soc_dai *codec_dai = rtd->codec_dai;
282 pr_debug("%s\n", __func__);
283 link_dev = snd_hdac_ext_stream_assign(ebus, substream,
284 HDAC_EXT_STREAM_TYPE_LINK);
288 snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
290 /* set the stream tag in the codec dai dma params */
291 dma_params = (struct skl_dma_params *)
292 snd_soc_dai_get_dma_data(codec_dai, substream);
294 dma_params->stream_tag = hdac_stream(link_dev)->stream_tag;
295 snd_soc_dai_set_dma_data(codec_dai, substream, (void *)dma_params);
296 dma_id = hdac_stream(link_dev)->stream_tag - 1;
301 static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
302 struct snd_soc_dai *dai)
304 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
305 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
306 struct hdac_ext_stream *link_dev =
307 snd_soc_dai_get_dma_data(dai, substream);
308 unsigned int format_val = 0;
309 struct skl_dma_params *dma_params;
310 struct snd_soc_dai *codec_dai = rtd->codec_dai;
311 struct snd_pcm_hw_params *params;
312 struct snd_interval *channels, *rate;
313 struct hdac_ext_link *link;
315 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
316 if (link_dev->link_prepared) {
317 dev_dbg(dai->dev, "already stream is prepared - returning\n");
320 params = devm_kzalloc(dai->dev, sizeof(*params), GFP_KERNEL);
324 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
325 channels->min = channels->max = substream->runtime->channels;
326 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
327 rate->min = rate->max = substream->runtime->rate;
328 snd_mask_set(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT -
329 SNDRV_PCM_HW_PARAM_FIRST_MASK],
330 substream->runtime->format);
333 dma_params = (struct skl_dma_params *)
334 snd_soc_dai_get_dma_data(codec_dai, substream);
336 format_val = dma_params->format;
337 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
338 hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
340 snd_hdac_ext_link_stream_reset(link_dev);
342 snd_hdac_ext_link_stream_setup(link_dev, format_val);
344 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
348 snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
349 link_dev->link_prepared = 1;
354 static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
355 int cmd, struct snd_soc_dai *dai)
357 struct hdac_ext_stream *link_dev =
358 snd_soc_dai_get_dma_data(dai, substream);
360 dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
362 case SNDRV_PCM_TRIGGER_START:
363 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
364 case SNDRV_PCM_TRIGGER_RESUME:
365 snd_hdac_ext_link_stream_start(link_dev);
368 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
369 case SNDRV_PCM_TRIGGER_SUSPEND:
370 case SNDRV_PCM_TRIGGER_STOP:
371 snd_hdac_ext_link_stream_clear(link_dev);
380 static int skl_link_hw_free(struct snd_pcm_substream *substream,
381 struct snd_soc_dai *dai)
383 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
384 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
385 struct hdac_ext_stream *link_dev =
386 snd_soc_dai_get_dma_data(dai, substream);
387 struct hdac_ext_link *link;
389 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
391 link_dev->link_prepared = 0;
393 link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
397 snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
398 snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
402 static int skl_hda_be_startup(struct snd_pcm_substream *substream,
403 struct snd_soc_dai *dai)
405 return pm_runtime_get_sync(dai->dev);
408 static void skl_hda_be_shutdown(struct snd_pcm_substream *substream,
409 struct snd_soc_dai *dai)
411 pm_runtime_mark_last_busy(dai->dev);
412 pm_runtime_put_autosuspend(dai->dev);
415 static struct snd_soc_dai_ops skl_pcm_dai_ops = {
416 .startup = skl_pcm_open,
417 .shutdown = skl_pcm_close,
418 .prepare = skl_pcm_prepare,
419 .hw_params = skl_pcm_hw_params,
420 .hw_free = skl_pcm_hw_free,
423 static struct snd_soc_dai_ops skl_dmic_dai_ops = {
424 .startup = skl_hda_be_startup,
425 .shutdown = skl_hda_be_shutdown,
428 static struct snd_soc_dai_ops skl_link_dai_ops = {
429 .startup = skl_hda_be_startup,
430 .prepare = skl_link_pcm_prepare,
431 .hw_params = skl_link_hw_params,
432 .hw_free = skl_link_hw_free,
433 .trigger = skl_link_pcm_trigger,
434 .shutdown = skl_hda_be_shutdown,
437 static struct snd_soc_dai_driver skl_platform_dai[] = {
439 .name = "System Pin",
440 .ops = &skl_pcm_dai_ops,
442 .stream_name = "System Playback",
443 .channels_min = HDA_MONO,
444 .channels_max = HDA_STEREO,
445 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
446 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
449 .stream_name = "System Capture",
450 .channels_min = HDA_MONO,
451 .channels_max = HDA_STEREO,
452 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
453 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
457 .name = "Reference Pin",
458 .ops = &skl_pcm_dai_ops,
460 .stream_name = "Reference Capture",
461 .channels_min = HDA_MONO,
462 .channels_max = HDA_STEREO,
463 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
464 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
468 .name = "Deepbuffer Pin",
469 .ops = &skl_pcm_dai_ops,
471 .stream_name = "Deepbuffer Playback",
472 .channels_min = HDA_STEREO,
473 .channels_max = HDA_STEREO,
474 .rates = SNDRV_PCM_RATE_48000,
475 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
479 .name = "LowLatency Pin",
480 .ops = &skl_pcm_dai_ops,
482 .stream_name = "Low Latency Playback",
483 .channels_min = HDA_STEREO,
484 .channels_max = HDA_STEREO,
485 .rates = SNDRV_PCM_RATE_48000,
486 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
492 .ops = &skl_link_dai_ops,
494 .stream_name = "iDisp Tx",
495 .channels_min = HDA_STEREO,
496 .channels_max = HDA_STEREO,
497 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
498 .formats = SNDRV_PCM_FMTBIT_S16_LE,
502 .name = "DMIC01 Pin",
503 .ops = &skl_dmic_dai_ops,
505 .stream_name = "DMIC01 Rx",
506 .channels_min = HDA_STEREO,
507 .channels_max = HDA_STEREO,
508 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
509 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
513 .name = "DMIC23 Pin",
514 .ops = &skl_dmic_dai_ops,
516 .stream_name = "DMIC23 Rx",
517 .channels_min = HDA_STEREO,
518 .channels_max = HDA_STEREO,
519 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
520 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
524 .name = "HD-Codec Pin",
525 .ops = &skl_link_dai_ops,
527 .stream_name = "HD-Codec Tx",
528 .channels_min = HDA_STEREO,
529 .channels_max = HDA_STEREO,
530 .rates = SNDRV_PCM_RATE_48000,
531 .formats = SNDRV_PCM_FMTBIT_S16_LE,
534 .stream_name = "HD-Codec Rx",
535 .channels_min = HDA_STEREO,
536 .channels_max = HDA_STEREO,
537 .rates = SNDRV_PCM_RATE_48000,
538 .formats = SNDRV_PCM_FMTBIT_S16_LE,
542 .name = "HD-Codec-SPK Pin",
543 .ops = &skl_link_dai_ops,
545 .stream_name = "HD-Codec-SPK Tx",
546 .channels_min = HDA_STEREO,
547 .channels_max = HDA_STEREO,
548 .rates = SNDRV_PCM_RATE_48000,
549 .formats = SNDRV_PCM_FMTBIT_S16_LE,
553 .name = "HD-Codec-AMIC Pin",
554 .ops = &skl_link_dai_ops,
556 .stream_name = "HD-Codec-AMIC Rx",
557 .channels_min = HDA_STEREO,
558 .channels_max = HDA_STEREO,
559 .rates = SNDRV_PCM_RATE_48000,
560 .formats = SNDRV_PCM_FMTBIT_S16_LE,
565 static int skl_platform_open(struct snd_pcm_substream *substream)
567 struct snd_pcm_runtime *runtime;
568 struct snd_soc_pcm_runtime *rtd = substream->private_data;
569 struct snd_soc_dai_link *dai_link = rtd->dai_link;
571 dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
572 dai_link->cpu_dai_name);
574 runtime = substream->runtime;
575 snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
580 static int skl_pcm_trigger(struct snd_pcm_substream *substream,
583 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
584 struct hdac_bus *bus = ebus_to_hbus(ebus);
585 struct hdac_ext_stream *stream;
586 struct snd_pcm_substream *s;
589 unsigned long cookie;
590 struct hdac_stream *hstr;
592 stream = get_hdac_ext_stream(substream);
593 hstr = hdac_stream(stream);
595 dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
601 case SNDRV_PCM_TRIGGER_START:
602 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
603 case SNDRV_PCM_TRIGGER_RESUME:
607 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
608 case SNDRV_PCM_TRIGGER_SUSPEND:
609 case SNDRV_PCM_TRIGGER_STOP:
617 snd_pcm_group_for_each_entry(s, substream) {
618 if (s->pcm->card != substream->pcm->card)
620 stream = get_hdac_ext_stream(s);
621 sbits |= 1 << hdac_stream(stream)->index;
622 snd_pcm_trigger_done(s, substream);
625 spin_lock_irqsave(&bus->reg_lock, cookie);
627 /* first, set SYNC bits of corresponding streams */
628 snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
630 snd_pcm_group_for_each_entry(s, substream) {
631 if (s->pcm->card != substream->pcm->card)
633 stream = get_hdac_ext_stream(s);
635 snd_hdac_stream_start(hdac_stream(stream), true);
637 snd_hdac_stream_stop(hdac_stream(stream));
639 spin_unlock_irqrestore(&bus->reg_lock, cookie);
641 snd_hdac_stream_sync(hstr, start, sbits);
643 spin_lock_irqsave(&bus->reg_lock, cookie);
645 /* reset SYNC bits */
646 snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
648 snd_hdac_stream_timecounter_init(hstr, sbits);
649 spin_unlock_irqrestore(&bus->reg_lock, cookie);
654 static int skl_dsp_trigger(struct snd_pcm_substream *substream,
657 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
658 struct hdac_bus *bus = ebus_to_hbus(ebus);
659 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
660 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
661 struct hdac_ext_stream *stream;
663 unsigned long cookie;
664 struct hdac_stream *hstr;
666 dev_dbg(bus->dev, "In %s cmd=%d streamname=%s\n", __func__, cmd, cpu_dai->name);
668 stream = get_hdac_ext_stream(substream);
669 hstr = hdac_stream(stream);
675 case SNDRV_PCM_TRIGGER_START:
676 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
677 case SNDRV_PCM_TRIGGER_RESUME:
681 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
682 case SNDRV_PCM_TRIGGER_SUSPEND:
683 case SNDRV_PCM_TRIGGER_STOP:
691 spin_lock_irqsave(&bus->reg_lock, cookie);
694 snd_hdac_stream_start(hdac_stream(stream), true);
696 snd_hdac_stream_stop(hdac_stream(stream));
699 snd_hdac_stream_timecounter_init(hstr, 0);
701 spin_unlock_irqrestore(&bus->reg_lock, cookie);
705 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
708 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
711 return skl_dsp_trigger(substream, cmd);
713 return skl_pcm_trigger(substream, cmd);
716 /* calculate runtime delay from LPIB */
717 static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
718 struct hdac_ext_stream *sstream,
721 struct hdac_bus *bus = ebus_to_hbus(ebus);
722 struct hdac_stream *hstream = hdac_stream(sstream);
723 struct snd_pcm_substream *substream = hstream->substream;
724 int stream = substream->stream;
725 unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
728 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
729 delay = pos - lpib_pos;
731 delay = lpib_pos - pos;
734 if (delay >= hstream->delay_negative_threshold)
737 delay += hstream->bufsize;
740 if (delay >= hstream->period_bytes) {
742 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
743 delay, hstream->period_bytes);
747 return bytes_to_frames(substream->runtime, delay);
750 static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
753 struct hdac_stream *hstr = hdac_stream(hstream);
754 struct snd_pcm_substream *substream = hstr->substream;
755 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
759 /* use the position buffer as default */
760 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
762 if (pos >= hdac_stream(hstream)->bufsize)
765 if (substream->runtime) {
766 delay = skl_get_delay_from_lpib(ebus, hstream, pos)
768 substream->runtime->delay += delay;
774 static snd_pcm_uframes_t skl_platform_pcm_pointer
775 (struct snd_pcm_substream *substream)
777 struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
779 return bytes_to_frames(substream->runtime,
780 skl_get_position(hstream, 0));
783 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
786 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
787 struct snd_soc_dai *codec_dai = rtd->codec_dai;
788 u64 codec_frames, codec_nsecs;
790 if (!codec_dai->driver->ops->delay)
793 codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
794 codec_nsecs = div_u64(codec_frames * 1000000000LL,
795 substream->runtime->rate);
797 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
798 return nsec + codec_nsecs;
800 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
803 static int skl_get_time_info(struct snd_pcm_substream *substream,
804 struct timespec *system_ts, struct timespec *audio_ts,
805 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
806 struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
808 struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
809 struct hdac_stream *hstr = hdac_stream(sstream);
812 if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
813 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
815 snd_pcm_gettime(substream->runtime, system_ts);
817 nsec = timecounter_read(&hstr->tc);
818 nsec = div_u64(nsec, 3); /* can be optimized */
819 if (audio_tstamp_config->report_delay)
820 nsec = skl_adjust_codec_delay(substream, nsec);
822 *audio_ts = ns_to_timespec(nsec);
824 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
825 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
826 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
829 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
835 static struct snd_pcm_ops skl_platform_ops = {
836 .open = skl_platform_open,
837 .ioctl = snd_pcm_lib_ioctl,
838 .trigger = skl_platform_pcm_trigger,
839 .pointer = skl_platform_pcm_pointer,
840 .get_time_info = skl_get_time_info,
841 .mmap = snd_pcm_lib_default_mmap,
842 .page = snd_pcm_sgbuf_ops_page,
845 static void skl_pcm_free(struct snd_pcm *pcm)
847 snd_pcm_lib_preallocate_free_for_all(pcm);
850 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
852 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
854 struct snd_soc_dai *dai = rtd->cpu_dai;
855 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
856 struct snd_pcm *pcm = rtd->pcm;
859 struct skl *skl = ebus_to_skl(ebus);
861 if (dai->driver->playback.channels_min ||
862 dai->driver->capture.channels_min) {
863 /* buffer pre-allocation */
864 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
865 if (size > MAX_PREALLOC_SIZE)
866 size = MAX_PREALLOC_SIZE;
867 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
868 SNDRV_DMA_TYPE_DEV_SG,
869 snd_dma_pci_data(skl->pci),
870 size, MAX_PREALLOC_SIZE);
872 dev_err(dai->dev, "dma buffer allocationf fail\n");
880 static struct snd_soc_platform_driver skl_platform_drv = {
881 .ops = &skl_platform_ops,
882 .pcm_new = skl_pcm_new,
883 .pcm_free = skl_pcm_free,
886 static const struct snd_soc_component_driver skl_component = {
890 int skl_platform_register(struct device *dev)
894 ret = snd_soc_register_platform(dev, &skl_platform_drv);
896 dev_err(dev, "soc platform registration failed %d\n", ret);
899 ret = snd_soc_register_component(dev, &skl_component,
901 ARRAY_SIZE(skl_platform_dai));
903 dev_err(dev, "soc component registration failed %d\n", ret);
904 snd_soc_unregister_platform(dev);
911 int skl_platform_unregister(struct device *dev)
913 snd_soc_unregister_component(dev);
914 snd_soc_unregister_platform(dev);