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 = "HD-Codec Pin",
514 .ops = &skl_link_dai_ops,
516 .stream_name = "HD-Codec Tx",
517 .channels_min = HDA_STEREO,
518 .channels_max = HDA_STEREO,
519 .rates = SNDRV_PCM_RATE_48000,
520 .formats = SNDRV_PCM_FMTBIT_S16_LE,
523 .stream_name = "HD-Codec Rx",
524 .channels_min = HDA_STEREO,
525 .channels_max = HDA_STEREO,
526 .rates = SNDRV_PCM_RATE_48000,
527 .formats = SNDRV_PCM_FMTBIT_S16_LE,
532 static int skl_platform_open(struct snd_pcm_substream *substream)
534 struct snd_pcm_runtime *runtime;
535 struct snd_soc_pcm_runtime *rtd = substream->private_data;
536 struct snd_soc_dai_link *dai_link = rtd->dai_link;
538 dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
539 dai_link->cpu_dai_name);
541 runtime = substream->runtime;
542 snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
547 static int skl_pcm_trigger(struct snd_pcm_substream *substream,
550 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
551 struct hdac_bus *bus = ebus_to_hbus(ebus);
552 struct hdac_ext_stream *stream;
553 struct snd_pcm_substream *s;
556 unsigned long cookie;
557 struct hdac_stream *hstr;
559 stream = get_hdac_ext_stream(substream);
560 hstr = hdac_stream(stream);
562 dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
568 case SNDRV_PCM_TRIGGER_START:
569 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
570 case SNDRV_PCM_TRIGGER_RESUME:
574 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
575 case SNDRV_PCM_TRIGGER_SUSPEND:
576 case SNDRV_PCM_TRIGGER_STOP:
584 snd_pcm_group_for_each_entry(s, substream) {
585 if (s->pcm->card != substream->pcm->card)
587 stream = get_hdac_ext_stream(s);
588 sbits |= 1 << hdac_stream(stream)->index;
589 snd_pcm_trigger_done(s, substream);
592 spin_lock_irqsave(&bus->reg_lock, cookie);
594 /* first, set SYNC bits of corresponding streams */
595 snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
597 snd_pcm_group_for_each_entry(s, substream) {
598 if (s->pcm->card != substream->pcm->card)
600 stream = get_hdac_ext_stream(s);
602 snd_hdac_stream_start(hdac_stream(stream), true);
604 snd_hdac_stream_stop(hdac_stream(stream));
606 spin_unlock_irqrestore(&bus->reg_lock, cookie);
608 snd_hdac_stream_sync(hstr, start, sbits);
610 spin_lock_irqsave(&bus->reg_lock, cookie);
612 /* reset SYNC bits */
613 snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
615 snd_hdac_stream_timecounter_init(hstr, sbits);
616 spin_unlock_irqrestore(&bus->reg_lock, cookie);
621 static int skl_dsp_trigger(struct snd_pcm_substream *substream,
624 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
625 struct hdac_bus *bus = ebus_to_hbus(ebus);
626 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
627 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
628 struct hdac_ext_stream *stream;
630 unsigned long cookie;
631 struct hdac_stream *hstr;
633 dev_dbg(bus->dev, "In %s cmd=%d streamname=%s\n", __func__, cmd, cpu_dai->name);
635 stream = get_hdac_ext_stream(substream);
636 hstr = hdac_stream(stream);
642 case SNDRV_PCM_TRIGGER_START:
643 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
644 case SNDRV_PCM_TRIGGER_RESUME:
648 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
649 case SNDRV_PCM_TRIGGER_SUSPEND:
650 case SNDRV_PCM_TRIGGER_STOP:
658 spin_lock_irqsave(&bus->reg_lock, cookie);
661 snd_hdac_stream_start(hdac_stream(stream), true);
663 snd_hdac_stream_stop(hdac_stream(stream));
666 snd_hdac_stream_timecounter_init(hstr, 0);
668 spin_unlock_irqrestore(&bus->reg_lock, cookie);
672 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
675 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
678 return skl_dsp_trigger(substream, cmd);
680 return skl_pcm_trigger(substream, cmd);
683 /* calculate runtime delay from LPIB */
684 static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
685 struct hdac_ext_stream *sstream,
688 struct hdac_bus *bus = ebus_to_hbus(ebus);
689 struct hdac_stream *hstream = hdac_stream(sstream);
690 struct snd_pcm_substream *substream = hstream->substream;
691 int stream = substream->stream;
692 unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
695 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
696 delay = pos - lpib_pos;
698 delay = lpib_pos - pos;
701 if (delay >= hstream->delay_negative_threshold)
704 delay += hstream->bufsize;
707 if (delay >= hstream->period_bytes) {
709 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
710 delay, hstream->period_bytes);
714 return bytes_to_frames(substream->runtime, delay);
717 static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
720 struct hdac_stream *hstr = hdac_stream(hstream);
721 struct snd_pcm_substream *substream = hstr->substream;
722 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
726 /* use the position buffer as default */
727 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
729 if (pos >= hdac_stream(hstream)->bufsize)
732 if (substream->runtime) {
733 delay = skl_get_delay_from_lpib(ebus, hstream, pos)
735 substream->runtime->delay += delay;
741 static snd_pcm_uframes_t skl_platform_pcm_pointer
742 (struct snd_pcm_substream *substream)
744 struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
746 return bytes_to_frames(substream->runtime,
747 skl_get_position(hstream, 0));
750 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
753 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
754 struct snd_soc_dai *codec_dai = rtd->codec_dai;
755 u64 codec_frames, codec_nsecs;
757 if (!codec_dai->driver->ops->delay)
760 codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
761 codec_nsecs = div_u64(codec_frames * 1000000000LL,
762 substream->runtime->rate);
764 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
765 return nsec + codec_nsecs;
767 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
770 static int skl_get_time_info(struct snd_pcm_substream *substream,
771 struct timespec *system_ts, struct timespec *audio_ts,
772 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
773 struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
775 struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
776 struct hdac_stream *hstr = hdac_stream(sstream);
779 if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
780 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
782 snd_pcm_gettime(substream->runtime, system_ts);
784 nsec = timecounter_read(&hstr->tc);
785 nsec = div_u64(nsec, 3); /* can be optimized */
786 if (audio_tstamp_config->report_delay)
787 nsec = skl_adjust_codec_delay(substream, nsec);
789 *audio_ts = ns_to_timespec(nsec);
791 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
792 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
793 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
796 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
802 static struct snd_pcm_ops skl_platform_ops = {
803 .open = skl_platform_open,
804 .ioctl = snd_pcm_lib_ioctl,
805 .trigger = skl_platform_pcm_trigger,
806 .pointer = skl_platform_pcm_pointer,
807 .get_time_info = skl_get_time_info,
808 .mmap = snd_pcm_lib_default_mmap,
809 .page = snd_pcm_sgbuf_ops_page,
812 static void skl_pcm_free(struct snd_pcm *pcm)
814 snd_pcm_lib_preallocate_free_for_all(pcm);
817 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
819 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
821 struct snd_soc_dai *dai = rtd->cpu_dai;
822 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
823 struct snd_pcm *pcm = rtd->pcm;
826 struct skl *skl = ebus_to_skl(ebus);
828 if (dai->driver->playback.channels_min ||
829 dai->driver->capture.channels_min) {
830 /* buffer pre-allocation */
831 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
832 if (size > MAX_PREALLOC_SIZE)
833 size = MAX_PREALLOC_SIZE;
834 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
835 SNDRV_DMA_TYPE_DEV_SG,
836 snd_dma_pci_data(skl->pci),
837 size, MAX_PREALLOC_SIZE);
839 dev_err(dai->dev, "dma buffer allocationf fail\n");
847 static struct snd_soc_platform_driver skl_platform_drv = {
848 .ops = &skl_platform_ops,
849 .pcm_new = skl_pcm_new,
850 .pcm_free = skl_pcm_free,
853 static const struct snd_soc_component_driver skl_component = {
857 int skl_platform_register(struct device *dev)
861 ret = snd_soc_register_platform(dev, &skl_platform_drv);
863 dev_err(dev, "soc platform registration failed %d\n", ret);
866 ret = snd_soc_register_component(dev, &skl_component,
868 ARRAY_SIZE(skl_platform_dai));
870 dev_err(dev, "soc component registration failed %d\n", ret);
871 snd_soc_unregister_platform(dev);
878 int skl_platform_unregister(struct device *dev)
880 snd_soc_unregister_component(dev);
881 snd_soc_unregister_platform(dev);