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
;
295 return skl_tplg_be_update_params(dai
, &p_params
);
298 static int skl_decoupled_trigger(struct snd_pcm_substream
*substream
,
301 struct hdac_ext_bus
*ebus
= get_bus_ctx(substream
);
302 struct hdac_bus
*bus
= ebus_to_hbus(ebus
);
303 struct hdac_ext_stream
*stream
;
305 unsigned long cookie
;
306 struct hdac_stream
*hstr
;
308 stream
= get_hdac_ext_stream(substream
);
309 hstr
= hdac_stream(stream
);
315 case SNDRV_PCM_TRIGGER_START
:
316 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
317 case SNDRV_PCM_TRIGGER_RESUME
:
321 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
322 case SNDRV_PCM_TRIGGER_SUSPEND
:
323 case SNDRV_PCM_TRIGGER_STOP
:
331 spin_lock_irqsave(&bus
->reg_lock
, cookie
);
334 snd_hdac_stream_start(hdac_stream(stream
), true);
335 snd_hdac_stream_timecounter_init(hstr
, 0);
337 snd_hdac_stream_stop(hdac_stream(stream
));
340 spin_unlock_irqrestore(&bus
->reg_lock
, cookie
);
345 static int skl_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
,
346 struct snd_soc_dai
*dai
)
348 struct skl
*skl
= get_skl_ctx(dai
->dev
);
349 struct skl_sst
*ctx
= skl
->skl_sst
;
350 struct skl_module_cfg
*mconfig
;
353 mconfig
= skl_tplg_fe_get_cpr_module(dai
, substream
->stream
);
358 case SNDRV_PCM_TRIGGER_START
:
359 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
360 case SNDRV_PCM_TRIGGER_RESUME
:
362 * Start HOST DMA and Start FE Pipe.This is to make sure that
363 * there are no underrun/overrun in the case when the FE
364 * pipeline is started but there is a delay in starting the
365 * DMA channel on the host.
367 ret
= skl_decoupled_trigger(substream
, cmd
);
370 return skl_run_pipe(ctx
, mconfig
->pipe
);
373 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
374 case SNDRV_PCM_TRIGGER_SUSPEND
:
375 case SNDRV_PCM_TRIGGER_STOP
:
377 * Stop FE Pipe first and stop DMA. This is to make sure that
378 * there are no underrun/overrun in the case if there is a delay
379 * between the two operations.
381 ret
= skl_stop_pipe(ctx
, mconfig
->pipe
);
385 ret
= skl_decoupled_trigger(substream
, cmd
);
395 static int skl_link_hw_params(struct snd_pcm_substream
*substream
,
396 struct snd_pcm_hw_params
*params
,
397 struct snd_soc_dai
*dai
)
399 struct hdac_ext_bus
*ebus
= dev_get_drvdata(dai
->dev
);
400 struct hdac_ext_stream
*link_dev
;
401 struct snd_soc_pcm_runtime
*rtd
= snd_pcm_substream_chip(substream
);
402 struct skl_dma_params
*dma_params
;
403 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
404 struct skl_pipe_params p_params
= {0};
406 link_dev
= snd_hdac_ext_stream_assign(ebus
, substream
,
407 HDAC_EXT_STREAM_TYPE_LINK
);
411 snd_soc_dai_set_dma_data(dai
, substream
, (void *)link_dev
);
413 /* set the stream tag in the codec dai dma params */
414 dma_params
= (struct skl_dma_params
*)
415 snd_soc_dai_get_dma_data(codec_dai
, substream
);
417 dma_params
->stream_tag
= hdac_stream(link_dev
)->stream_tag
;
418 snd_soc_dai_set_dma_data(codec_dai
, substream
, (void *)dma_params
);
420 p_params
.s_fmt
= snd_pcm_format_width(params_format(params
));
421 p_params
.ch
= params_channels(params
);
422 p_params
.s_freq
= params_rate(params
);
423 p_params
.stream
= substream
->stream
;
424 p_params
.link_dma_id
= hdac_stream(link_dev
)->stream_tag
- 1;
426 return skl_tplg_be_update_params(dai
, &p_params
);
429 static int skl_link_pcm_prepare(struct snd_pcm_substream
*substream
,
430 struct snd_soc_dai
*dai
)
432 struct snd_soc_pcm_runtime
*rtd
= snd_pcm_substream_chip(substream
);
433 struct hdac_ext_bus
*ebus
= dev_get_drvdata(dai
->dev
);
434 struct hdac_ext_stream
*link_dev
=
435 snd_soc_dai_get_dma_data(dai
, substream
);
436 unsigned int format_val
= 0;
437 struct skl_dma_params
*dma_params
;
438 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
439 struct hdac_ext_link
*link
;
441 if (link_dev
->link_prepared
) {
442 dev_dbg(dai
->dev
, "already stream is prepared - returning\n");
446 dma_params
= (struct skl_dma_params
*)
447 snd_soc_dai_get_dma_data(codec_dai
, substream
);
449 format_val
= dma_params
->format
;
450 dev_dbg(dai
->dev
, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
451 hdac_stream(link_dev
)->stream_tag
, format_val
, codec_dai
->name
);
453 snd_hdac_ext_link_stream_reset(link_dev
);
455 snd_hdac_ext_link_stream_setup(link_dev
, format_val
);
457 link
= snd_hdac_ext_bus_get_link(ebus
, rtd
->codec
->component
.name
);
461 snd_hdac_ext_link_set_stream_id(link
, hdac_stream(link_dev
)->stream_tag
);
462 link_dev
->link_prepared
= 1;
467 static int skl_link_pcm_trigger(struct snd_pcm_substream
*substream
,
468 int cmd
, struct snd_soc_dai
*dai
)
470 struct hdac_ext_stream
*link_dev
=
471 snd_soc_dai_get_dma_data(dai
, substream
);
473 dev_dbg(dai
->dev
, "In %s cmd=%d\n", __func__
, cmd
);
475 case SNDRV_PCM_TRIGGER_START
:
476 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
477 case SNDRV_PCM_TRIGGER_RESUME
:
478 snd_hdac_ext_link_stream_start(link_dev
);
481 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
482 case SNDRV_PCM_TRIGGER_SUSPEND
:
483 case SNDRV_PCM_TRIGGER_STOP
:
484 snd_hdac_ext_link_stream_clear(link_dev
);
493 static int skl_link_hw_free(struct snd_pcm_substream
*substream
,
494 struct snd_soc_dai
*dai
)
496 struct hdac_ext_bus
*ebus
= dev_get_drvdata(dai
->dev
);
497 struct snd_soc_pcm_runtime
*rtd
= snd_pcm_substream_chip(substream
);
498 struct hdac_ext_stream
*link_dev
=
499 snd_soc_dai_get_dma_data(dai
, substream
);
500 struct hdac_ext_link
*link
;
502 dev_dbg(dai
->dev
, "%s: %s\n", __func__
, dai
->name
);
504 link_dev
->link_prepared
= 0;
506 link
= snd_hdac_ext_bus_get_link(ebus
, rtd
->codec
->component
.name
);
510 snd_hdac_ext_link_clear_stream_id(link
, hdac_stream(link_dev
)->stream_tag
);
511 snd_hdac_ext_stream_release(link_dev
, HDAC_EXT_STREAM_TYPE_LINK
);
515 static int skl_be_startup(struct snd_pcm_substream
*substream
,
516 struct snd_soc_dai
*dai
)
518 return pm_runtime_get_sync(dai
->dev
);
521 static void skl_be_shutdown(struct snd_pcm_substream
*substream
,
522 struct snd_soc_dai
*dai
)
524 pm_runtime_mark_last_busy(dai
->dev
);
525 pm_runtime_put_autosuspend(dai
->dev
);
528 static struct snd_soc_dai_ops skl_pcm_dai_ops
= {
529 .startup
= skl_pcm_open
,
530 .shutdown
= skl_pcm_close
,
531 .prepare
= skl_pcm_prepare
,
532 .hw_params
= skl_pcm_hw_params
,
533 .hw_free
= skl_pcm_hw_free
,
534 .trigger
= skl_pcm_trigger
,
537 static struct snd_soc_dai_ops skl_dmic_dai_ops
= {
538 .startup
= skl_be_startup
,
539 .hw_params
= skl_be_hw_params
,
540 .shutdown
= skl_be_shutdown
,
543 static struct snd_soc_dai_ops skl_be_ssp_dai_ops
= {
544 .startup
= skl_be_startup
,
545 .hw_params
= skl_be_hw_params
,
546 .shutdown
= skl_be_shutdown
,
549 static struct snd_soc_dai_ops skl_link_dai_ops
= {
550 .startup
= skl_be_startup
,
551 .prepare
= skl_link_pcm_prepare
,
552 .hw_params
= skl_link_hw_params
,
553 .hw_free
= skl_link_hw_free
,
554 .trigger
= skl_link_pcm_trigger
,
555 .shutdown
= skl_be_shutdown
,
558 static struct snd_soc_dai_driver skl_platform_dai
[] = {
560 .name
= "System Pin",
561 .ops
= &skl_pcm_dai_ops
,
563 .stream_name
= "System Playback",
564 .channels_min
= HDA_MONO
,
565 .channels_max
= HDA_STEREO
,
566 .rates
= SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_16000
| SNDRV_PCM_RATE_8000
,
567 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
570 .stream_name
= "System Capture",
571 .channels_min
= HDA_MONO
,
572 .channels_max
= HDA_STEREO
,
573 .rates
= SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_16000
,
574 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
578 .name
= "Reference Pin",
579 .ops
= &skl_pcm_dai_ops
,
581 .stream_name
= "Reference Capture",
582 .channels_min
= HDA_MONO
,
583 .channels_max
= HDA_STEREO
,
584 .rates
= SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_16000
,
585 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
589 .name
= "Deepbuffer Pin",
590 .ops
= &skl_pcm_dai_ops
,
592 .stream_name
= "Deepbuffer Playback",
593 .channels_min
= HDA_STEREO
,
594 .channels_max
= HDA_STEREO
,
595 .rates
= SNDRV_PCM_RATE_48000
,
596 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
600 .name
= "LowLatency Pin",
601 .ops
= &skl_pcm_dai_ops
,
603 .stream_name
= "Low Latency Playback",
604 .channels_min
= HDA_STEREO
,
605 .channels_max
= HDA_STEREO
,
606 .rates
= SNDRV_PCM_RATE_48000
,
607 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
613 .ops
= &skl_be_ssp_dai_ops
,
615 .stream_name
= "ssp0 Tx",
616 .channels_min
= HDA_STEREO
,
617 .channels_max
= HDA_STEREO
,
618 .rates
= SNDRV_PCM_RATE_48000
,
619 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
622 .stream_name
= "ssp0 Rx",
623 .channels_min
= HDA_STEREO
,
624 .channels_max
= HDA_STEREO
,
625 .rates
= SNDRV_PCM_RATE_48000
,
626 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
631 .ops
= &skl_link_dai_ops
,
633 .stream_name
= "iDisp Tx",
634 .channels_min
= HDA_STEREO
,
635 .channels_max
= HDA_STEREO
,
636 .rates
= SNDRV_PCM_RATE_8000
|SNDRV_PCM_RATE_16000
|SNDRV_PCM_RATE_48000
,
637 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
641 .name
= "DMIC01 Pin",
642 .ops
= &skl_dmic_dai_ops
,
644 .stream_name
= "DMIC01 Rx",
645 .channels_min
= HDA_STEREO
,
646 .channels_max
= HDA_STEREO
,
647 .rates
= SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_16000
,
648 .formats
= SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_LE
,
652 .name
= "HD-Codec Pin",
653 .ops
= &skl_link_dai_ops
,
655 .stream_name
= "HD-Codec Tx",
656 .channels_min
= HDA_STEREO
,
657 .channels_max
= HDA_STEREO
,
658 .rates
= SNDRV_PCM_RATE_48000
,
659 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
662 .stream_name
= "HD-Codec Rx",
663 .channels_min
= HDA_STEREO
,
664 .channels_max
= HDA_STEREO
,
665 .rates
= SNDRV_PCM_RATE_48000
,
666 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
671 static int skl_platform_open(struct snd_pcm_substream
*substream
)
673 struct snd_pcm_runtime
*runtime
;
674 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
675 struct snd_soc_dai_link
*dai_link
= rtd
->dai_link
;
677 dev_dbg(rtd
->cpu_dai
->dev
, "In %s:%s\n", __func__
,
678 dai_link
->cpu_dai_name
);
680 runtime
= substream
->runtime
;
681 snd_soc_set_runtime_hwparams(substream
, &azx_pcm_hw
);
686 static int skl_coupled_trigger(struct snd_pcm_substream
*substream
,
689 struct hdac_ext_bus
*ebus
= get_bus_ctx(substream
);
690 struct hdac_bus
*bus
= ebus_to_hbus(ebus
);
691 struct hdac_ext_stream
*stream
;
692 struct snd_pcm_substream
*s
;
695 unsigned long cookie
;
696 struct hdac_stream
*hstr
;
698 stream
= get_hdac_ext_stream(substream
);
699 hstr
= hdac_stream(stream
);
701 dev_dbg(bus
->dev
, "In %s cmd=%d\n", __func__
, cmd
);
707 case SNDRV_PCM_TRIGGER_START
:
708 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
709 case SNDRV_PCM_TRIGGER_RESUME
:
713 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
714 case SNDRV_PCM_TRIGGER_SUSPEND
:
715 case SNDRV_PCM_TRIGGER_STOP
:
723 snd_pcm_group_for_each_entry(s
, substream
) {
724 if (s
->pcm
->card
!= substream
->pcm
->card
)
726 stream
= get_hdac_ext_stream(s
);
727 sbits
|= 1 << hdac_stream(stream
)->index
;
728 snd_pcm_trigger_done(s
, substream
);
731 spin_lock_irqsave(&bus
->reg_lock
, cookie
);
733 /* first, set SYNC bits of corresponding streams */
734 snd_hdac_stream_sync_trigger(hstr
, true, sbits
, AZX_REG_SSYNC
);
736 snd_pcm_group_for_each_entry(s
, substream
) {
737 if (s
->pcm
->card
!= substream
->pcm
->card
)
739 stream
= get_hdac_ext_stream(s
);
741 snd_hdac_stream_start(hdac_stream(stream
), true);
743 snd_hdac_stream_stop(hdac_stream(stream
));
745 spin_unlock_irqrestore(&bus
->reg_lock
, cookie
);
747 snd_hdac_stream_sync(hstr
, start
, sbits
);
749 spin_lock_irqsave(&bus
->reg_lock
, cookie
);
751 /* reset SYNC bits */
752 snd_hdac_stream_sync_trigger(hstr
, false, sbits
, AZX_REG_SSYNC
);
754 snd_hdac_stream_timecounter_init(hstr
, sbits
);
755 spin_unlock_irqrestore(&bus
->reg_lock
, cookie
);
760 static int skl_platform_pcm_trigger(struct snd_pcm_substream
*substream
,
763 struct hdac_ext_bus
*ebus
= get_bus_ctx(substream
);
766 return skl_coupled_trigger(substream
, cmd
);
771 /* calculate runtime delay from LPIB */
772 static int skl_get_delay_from_lpib(struct hdac_ext_bus
*ebus
,
773 struct hdac_ext_stream
*sstream
,
776 struct hdac_bus
*bus
= ebus_to_hbus(ebus
);
777 struct hdac_stream
*hstream
= hdac_stream(sstream
);
778 struct snd_pcm_substream
*substream
= hstream
->substream
;
779 int stream
= substream
->stream
;
780 unsigned int lpib_pos
= snd_hdac_stream_get_pos_lpib(hstream
);
783 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
784 delay
= pos
- lpib_pos
;
786 delay
= lpib_pos
- pos
;
789 if (delay
>= hstream
->delay_negative_threshold
)
792 delay
+= hstream
->bufsize
;
795 if (delay
>= hstream
->period_bytes
) {
797 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
798 delay
, hstream
->period_bytes
);
802 return bytes_to_frames(substream
->runtime
, delay
);
805 static unsigned int skl_get_position(struct hdac_ext_stream
*hstream
,
808 struct hdac_stream
*hstr
= hdac_stream(hstream
);
809 struct snd_pcm_substream
*substream
= hstr
->substream
;
810 struct hdac_ext_bus
*ebus
;
814 /* use the position buffer as default */
815 pos
= snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream
));
817 if (pos
>= hdac_stream(hstream
)->bufsize
)
820 if (substream
->runtime
) {
821 ebus
= get_bus_ctx(substream
);
822 delay
= skl_get_delay_from_lpib(ebus
, hstream
, pos
)
824 substream
->runtime
->delay
+= delay
;
830 static snd_pcm_uframes_t skl_platform_pcm_pointer
831 (struct snd_pcm_substream
*substream
)
833 struct hdac_ext_stream
*hstream
= get_hdac_ext_stream(substream
);
835 return bytes_to_frames(substream
->runtime
,
836 skl_get_position(hstream
, 0));
839 static u64
skl_adjust_codec_delay(struct snd_pcm_substream
*substream
,
842 struct snd_soc_pcm_runtime
*rtd
= snd_pcm_substream_chip(substream
);
843 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
844 u64 codec_frames
, codec_nsecs
;
846 if (!codec_dai
->driver
->ops
->delay
)
849 codec_frames
= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
850 codec_nsecs
= div_u64(codec_frames
* 1000000000LL,
851 substream
->runtime
->rate
);
853 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
854 return nsec
+ codec_nsecs
;
856 return (nsec
> codec_nsecs
) ? nsec
- codec_nsecs
: 0;
859 static int skl_get_time_info(struct snd_pcm_substream
*substream
,
860 struct timespec
*system_ts
, struct timespec
*audio_ts
,
861 struct snd_pcm_audio_tstamp_config
*audio_tstamp_config
,
862 struct snd_pcm_audio_tstamp_report
*audio_tstamp_report
)
864 struct hdac_ext_stream
*sstream
= get_hdac_ext_stream(substream
);
865 struct hdac_stream
*hstr
= hdac_stream(sstream
);
868 if ((substream
->runtime
->hw
.info
& SNDRV_PCM_INFO_HAS_LINK_ATIME
) &&
869 (audio_tstamp_config
->type_requested
== SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
)) {
871 snd_pcm_gettime(substream
->runtime
, system_ts
);
873 nsec
= timecounter_read(&hstr
->tc
);
874 nsec
= div_u64(nsec
, 3); /* can be optimized */
875 if (audio_tstamp_config
->report_delay
)
876 nsec
= skl_adjust_codec_delay(substream
, nsec
);
878 *audio_ts
= ns_to_timespec(nsec
);
880 audio_tstamp_report
->actual_type
= SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK
;
881 audio_tstamp_report
->accuracy_report
= 1; /* rest of struct is valid */
882 audio_tstamp_report
->accuracy
= 42; /* 24MHzWallClk == 42ns resolution */
885 audio_tstamp_report
->actual_type
= SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT
;
891 static struct snd_pcm_ops skl_platform_ops
= {
892 .open
= skl_platform_open
,
893 .ioctl
= snd_pcm_lib_ioctl
,
894 .trigger
= skl_platform_pcm_trigger
,
895 .pointer
= skl_platform_pcm_pointer
,
896 .get_time_info
= skl_get_time_info
,
897 .mmap
= snd_pcm_lib_default_mmap
,
898 .page
= snd_pcm_sgbuf_ops_page
,
901 static void skl_pcm_free(struct snd_pcm
*pcm
)
903 snd_pcm_lib_preallocate_free_for_all(pcm
);
906 #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
908 static int skl_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
910 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
911 struct hdac_ext_bus
*ebus
= dev_get_drvdata(dai
->dev
);
912 struct snd_pcm
*pcm
= rtd
->pcm
;
915 struct skl
*skl
= ebus_to_skl(ebus
);
917 if (dai
->driver
->playback
.channels_min
||
918 dai
->driver
->capture
.channels_min
) {
919 /* buffer pre-allocation */
920 size
= CONFIG_SND_HDA_PREALLOC_SIZE
* 1024;
921 if (size
> MAX_PREALLOC_SIZE
)
922 size
= MAX_PREALLOC_SIZE
;
923 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
924 SNDRV_DMA_TYPE_DEV_SG
,
925 snd_dma_pci_data(skl
->pci
),
926 size
, MAX_PREALLOC_SIZE
);
928 dev_err(dai
->dev
, "dma buffer allocationf fail\n");
936 static int skl_platform_soc_probe(struct snd_soc_platform
*platform
)
938 struct hdac_ext_bus
*ebus
= dev_get_drvdata(platform
->dev
);
941 return skl_tplg_init(platform
, ebus
);
945 static struct snd_soc_platform_driver skl_platform_drv
= {
946 .probe
= skl_platform_soc_probe
,
947 .ops
= &skl_platform_ops
,
948 .pcm_new
= skl_pcm_new
,
949 .pcm_free
= skl_pcm_free
,
952 static const struct snd_soc_component_driver skl_component
= {
956 int skl_platform_register(struct device
*dev
)
959 struct hdac_ext_bus
*ebus
= dev_get_drvdata(dev
);
960 struct skl
*skl
= ebus_to_skl(ebus
);
962 INIT_LIST_HEAD(&skl
->ppl_list
);
964 ret
= snd_soc_register_platform(dev
, &skl_platform_drv
);
966 dev_err(dev
, "soc platform registration failed %d\n", ret
);
969 ret
= snd_soc_register_component(dev
, &skl_component
,
971 ARRAY_SIZE(skl_platform_dai
));
973 dev_err(dev
, "soc component registration failed %d\n", ret
);
974 snd_soc_unregister_platform(dev
);
981 int skl_platform_unregister(struct device
*dev
)
983 snd_soc_unregister_component(dev
);
984 snd_soc_unregister_platform(dev
);