ASoC: Intel: Skylake: Disable clock gating during firmware and library download
[sfrench/cifs-2.6.git] / sound / soc / intel / skylake / skl-pcm.c
1 /*
2  *  skl-pcm.c -ASoC HDA Platform driver file implementing PCM functionality
3  *
4  *  Copyright (C) 2014-2015 Intel Corp
5  *  Author:  Jeeja KP <jeeja.kp@intel.com>
6  *
7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
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.
12  *
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.
17  *
18  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19  *
20  */
21
22 #include <linux/pci.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/delay.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include "skl.h"
28 #include "skl-topology.h"
29 #include "skl-sst-dsp.h"
30 #include "skl-sst-ipc.h"
31
32 #define HDA_MONO 1
33 #define HDA_STEREO 2
34 #define HDA_QUAD 4
35
36 static const struct snd_pcm_hardware azx_pcm_hw = {
37         .info =                 (SNDRV_PCM_INFO_MMAP |
38                                  SNDRV_PCM_INFO_INTERLEAVED |
39                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
40                                  SNDRV_PCM_INFO_MMAP_VALID |
41                                  SNDRV_PCM_INFO_PAUSE |
42                                  SNDRV_PCM_INFO_RESUME |
43                                  SNDRV_PCM_INFO_SYNC_START |
44                                  SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
45                                  SNDRV_PCM_INFO_HAS_LINK_ATIME |
46                                  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
47         .formats =              SNDRV_PCM_FMTBIT_S16_LE |
48                                 SNDRV_PCM_FMTBIT_S32_LE |
49                                 SNDRV_PCM_FMTBIT_S24_LE,
50         .rates =                SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
51                                 SNDRV_PCM_RATE_8000,
52         .rate_min =             8000,
53         .rate_max =             48000,
54         .channels_min =         1,
55         .channels_max =         8,
56         .buffer_bytes_max =     AZX_MAX_BUF_SIZE,
57         .period_bytes_min =     128,
58         .period_bytes_max =     AZX_MAX_BUF_SIZE / 2,
59         .periods_min =          2,
60         .periods_max =          AZX_MAX_FRAG,
61         .fifo_size =            0,
62 };
63
64 static inline
65 struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
66 {
67         return substream->runtime->private_data;
68 }
69
70 static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
71 {
72         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
73         struct hdac_stream *hstream = hdac_stream(stream);
74         struct hdac_bus *bus = hstream->bus;
75
76         return hbus_to_ebus(bus);
77 }
78
79 static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
80                                  struct snd_pcm_substream *substream,
81                                  size_t size)
82 {
83         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
84
85         hdac_stream(stream)->bufsize = 0;
86         hdac_stream(stream)->period_bytes = 0;
87         hdac_stream(stream)->format_val = 0;
88
89         return snd_pcm_lib_malloc_pages(substream, size);
90 }
91
92 static int skl_substream_free_pages(struct hdac_bus *bus,
93                                 struct snd_pcm_substream *substream)
94 {
95         return snd_pcm_lib_free_pages(substream);
96 }
97
98 static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
99                                  struct snd_pcm_runtime *runtime)
100 {
101         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
102
103         /* avoid wrap-around with wall-clock */
104         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
105                                      20, 178000000);
106 }
107
108 static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
109 {
110         if ((ebus_to_hbus(ebus))->ppcap)
111                 return HDAC_EXT_STREAM_TYPE_HOST;
112         else
113                 return HDAC_EXT_STREAM_TYPE_COUPLED;
114 }
115
116 /*
117  * check if the stream opened is marked as ignore_suspend by machine, if so
118  * then enable suspend_active refcount
119  *
120  * The count supend_active does not need lock as it is used in open/close
121  * and suspend context
122  */
123 static void skl_set_suspend_active(struct snd_pcm_substream *substream,
124                                          struct snd_soc_dai *dai, bool enable)
125 {
126         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
127         struct snd_soc_dapm_widget *w;
128         struct skl *skl = ebus_to_skl(ebus);
129
130         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
131                 w = dai->playback_widget;
132         else
133                 w = dai->capture_widget;
134
135         if (w->ignore_suspend && enable)
136                 skl->supend_active++;
137         else if (w->ignore_suspend && !enable)
138                 skl->supend_active--;
139 }
140
141 int skl_pcm_host_dma_prepare(struct device *dev, struct skl_pipe_params *params)
142 {
143         struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
144         struct hdac_bus *bus = ebus_to_hbus(ebus);
145         unsigned int format_val;
146         struct hdac_stream *hstream;
147         struct hdac_ext_stream *stream;
148         int err;
149
150         hstream = snd_hdac_get_stream(bus, params->stream,
151                                         params->host_dma_id + 1);
152         if (!hstream)
153                 return -EINVAL;
154
155         stream = stream_to_hdac_ext_stream(hstream);
156         snd_hdac_ext_stream_decouple(ebus, stream, true);
157
158         format_val = snd_hdac_calc_stream_format(params->s_freq,
159                         params->ch, params->format, params->host_bps, 0);
160
161         dev_dbg(dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
162                 format_val, params->s_freq, params->ch, params->format);
163
164         snd_hdac_stream_reset(hdac_stream(stream));
165         err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
166         if (err < 0)
167                 return err;
168
169         err = snd_hdac_stream_setup(hdac_stream(stream));
170         if (err < 0)
171                 return err;
172
173         hdac_stream(stream)->prepared = 1;
174
175         return 0;
176 }
177
178 int skl_pcm_link_dma_prepare(struct device *dev, struct skl_pipe_params *params)
179 {
180         struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
181         struct hdac_bus *bus = ebus_to_hbus(ebus);
182         unsigned int format_val;
183         struct hdac_stream *hstream;
184         struct hdac_ext_stream *stream;
185         struct hdac_ext_link *link;
186
187         hstream = snd_hdac_get_stream(bus, params->stream,
188                                         params->link_dma_id + 1);
189         if (!hstream)
190                 return -EINVAL;
191
192         stream = stream_to_hdac_ext_stream(hstream);
193         snd_hdac_ext_stream_decouple(ebus, stream, true);
194         format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
195                                         params->format, params->link_bps, 0);
196
197         dev_dbg(dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
198                 format_val, params->s_freq, params->ch, params->format);
199
200         snd_hdac_ext_link_stream_reset(stream);
201
202         snd_hdac_ext_link_stream_setup(stream, format_val);
203
204         list_for_each_entry(link, &ebus->hlink_list, list) {
205                 if (link->index == params->link_index)
206                         snd_hdac_ext_link_set_stream_id(link,
207                                         hstream->stream_tag);
208         }
209
210         stream->link_prepared = 1;
211
212         return 0;
213 }
214
215 static int skl_pcm_open(struct snd_pcm_substream *substream,
216                 struct snd_soc_dai *dai)
217 {
218         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
219         struct hdac_ext_stream *stream;
220         struct snd_pcm_runtime *runtime = substream->runtime;
221         struct skl_dma_params *dma_params;
222         struct skl *skl = get_skl_ctx(dai->dev);
223         struct skl_module_cfg *mconfig;
224
225         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
226
227         stream = snd_hdac_ext_stream_assign(ebus, substream,
228                                         skl_get_host_stream_type(ebus));
229         if (stream == NULL)
230                 return -EBUSY;
231
232         skl_set_pcm_constrains(ebus, runtime);
233
234         /*
235          * disable WALLCLOCK timestamps for capture streams
236          * until we figure out how to handle digital inputs
237          */
238         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
239                 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
240                 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
241         }
242
243         runtime->private_data = stream;
244
245         dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
246         if (!dma_params)
247                 return -ENOMEM;
248
249         dma_params->stream_tag = hdac_stream(stream)->stream_tag;
250         snd_soc_dai_set_dma_data(dai, substream, dma_params);
251
252         dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
253                                  dma_params->stream_tag);
254         skl_set_suspend_active(substream, dai, true);
255         snd_pcm_set_sync(substream);
256
257         mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
258         if (!mconfig)
259                 return -EINVAL;
260
261         skl_tplg_d0i3_get(skl, mconfig->d0i3_caps);
262
263         return 0;
264 }
265
266 static int skl_pcm_prepare(struct snd_pcm_substream *substream,
267                 struct snd_soc_dai *dai)
268 {
269         struct skl *skl = get_skl_ctx(dai->dev);
270         struct skl_module_cfg *mconfig;
271
272         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
273
274         mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
275
276         /* In case of XRUN recovery, reset the FW pipe to clean state */
277         if (mconfig && (substream->runtime->status->state ==
278                                         SNDRV_PCM_STATE_XRUN))
279                 skl_reset_pipe(skl->skl_sst, mconfig->pipe);
280
281         return 0;
282 }
283
284 static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
285                                 struct snd_pcm_hw_params *params,
286                                 struct snd_soc_dai *dai)
287 {
288         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
289         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
290         struct snd_pcm_runtime *runtime = substream->runtime;
291         struct skl_pipe_params p_params = {0};
292         struct skl_module_cfg *m_cfg;
293         int ret, dma_id;
294
295         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
296         ret = skl_substream_alloc_pages(ebus, substream,
297                                           params_buffer_bytes(params));
298         if (ret < 0)
299                 return ret;
300
301         dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
302                         runtime->rate, runtime->channels, runtime->format);
303
304         dma_id = hdac_stream(stream)->stream_tag - 1;
305         dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
306
307         p_params.s_fmt = snd_pcm_format_width(params_format(params));
308         p_params.ch = params_channels(params);
309         p_params.s_freq = params_rate(params);
310         p_params.host_dma_id = dma_id;
311         p_params.stream = substream->stream;
312         p_params.format = params_format(params);
313         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
314                 p_params.host_bps = dai->driver->playback.sig_bits;
315         else
316                 p_params.host_bps = dai->driver->capture.sig_bits;
317
318
319         m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
320         if (m_cfg)
321                 skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
322
323         return 0;
324 }
325
326 static void skl_pcm_close(struct snd_pcm_substream *substream,
327                 struct snd_soc_dai *dai)
328 {
329         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
330         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
331         struct skl_dma_params *dma_params = NULL;
332         struct skl *skl = ebus_to_skl(ebus);
333         struct skl_module_cfg *mconfig;
334
335         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
336
337         snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
338
339         dma_params = snd_soc_dai_get_dma_data(dai, substream);
340         /*
341          * now we should set this to NULL as we are freeing by the
342          * dma_params
343          */
344         snd_soc_dai_set_dma_data(dai, substream, NULL);
345         skl_set_suspend_active(substream, dai, false);
346
347         /*
348          * check if close is for "Reference Pin" and set back the
349          * CGCTL.MISCBDCGE if disabled by driver
350          */
351         if (!strncmp(dai->name, "Reference Pin", 13) &&
352                         skl->skl_sst->miscbdcg_disabled) {
353                 skl->skl_sst->enable_miscbdcge(dai->dev, true);
354                 skl->skl_sst->miscbdcg_disabled = false;
355         }
356
357         mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
358         skl_tplg_d0i3_put(skl, mconfig->d0i3_caps);
359
360         kfree(dma_params);
361 }
362
363 static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
364                 struct snd_soc_dai *dai)
365 {
366         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
367         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
368
369         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
370
371         snd_hdac_stream_cleanup(hdac_stream(stream));
372         hdac_stream(stream)->prepared = 0;
373
374         return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
375 }
376
377 static int skl_be_hw_params(struct snd_pcm_substream *substream,
378                                 struct snd_pcm_hw_params *params,
379                                 struct snd_soc_dai *dai)
380 {
381         struct skl_pipe_params p_params = {0};
382
383         p_params.s_fmt = snd_pcm_format_width(params_format(params));
384         p_params.ch = params_channels(params);
385         p_params.s_freq = params_rate(params);
386         p_params.stream = substream->stream;
387
388         return skl_tplg_be_update_params(dai, &p_params);
389 }
390
391 static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
392                 int cmd)
393 {
394         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
395         struct hdac_bus *bus = ebus_to_hbus(ebus);
396         struct hdac_ext_stream *stream;
397         int start;
398         unsigned long cookie;
399         struct hdac_stream *hstr;
400
401         stream = get_hdac_ext_stream(substream);
402         hstr = hdac_stream(stream);
403
404         if (!hstr->prepared)
405                 return -EPIPE;
406
407         switch (cmd) {
408         case SNDRV_PCM_TRIGGER_START:
409         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
410         case SNDRV_PCM_TRIGGER_RESUME:
411                 start = 1;
412                 break;
413
414         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
415         case SNDRV_PCM_TRIGGER_SUSPEND:
416         case SNDRV_PCM_TRIGGER_STOP:
417                 start = 0;
418                 break;
419
420         default:
421                 return -EINVAL;
422         }
423
424         spin_lock_irqsave(&bus->reg_lock, cookie);
425
426         if (start) {
427                 snd_hdac_stream_start(hdac_stream(stream), true);
428                 snd_hdac_stream_timecounter_init(hstr, 0);
429         } else {
430                 snd_hdac_stream_stop(hdac_stream(stream));
431         }
432
433         spin_unlock_irqrestore(&bus->reg_lock, cookie);
434
435         return 0;
436 }
437
438 static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
439                 struct snd_soc_dai *dai)
440 {
441         struct skl *skl = get_skl_ctx(dai->dev);
442         struct skl_sst *ctx = skl->skl_sst;
443         struct skl_module_cfg *mconfig;
444         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
445         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
446         struct snd_soc_dapm_widget *w;
447         int ret;
448
449         mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
450         if (!mconfig)
451                 return -EIO;
452
453         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
454                 w = dai->playback_widget;
455         else
456                 w = dai->capture_widget;
457
458         switch (cmd) {
459         case SNDRV_PCM_TRIGGER_RESUME:
460                 if (!w->ignore_suspend) {
461                         /*
462                          * enable DMA Resume enable bit for the stream, set the
463                          * dpib & lpib position to resume before starting the
464                          * DMA
465                          */
466                         snd_hdac_ext_stream_drsm_enable(ebus, true,
467                                                 hdac_stream(stream)->index);
468                         snd_hdac_ext_stream_set_dpibr(ebus, stream,
469                                                         stream->lpib);
470                         snd_hdac_ext_stream_set_lpib(stream, stream->lpib);
471                 }
472
473         case SNDRV_PCM_TRIGGER_START:
474         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
475                 /*
476                  * Start HOST DMA and Start FE Pipe.This is to make sure that
477                  * there are no underrun/overrun in the case when the FE
478                  * pipeline is started but there is a delay in starting the
479                  * DMA channel on the host.
480                  */
481                 ret = skl_decoupled_trigger(substream, cmd);
482                 if (ret < 0)
483                         return ret;
484                 return skl_run_pipe(ctx, mconfig->pipe);
485                 break;
486
487         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
488         case SNDRV_PCM_TRIGGER_SUSPEND:
489         case SNDRV_PCM_TRIGGER_STOP:
490                 /*
491                  * Stop FE Pipe first and stop DMA. This is to make sure that
492                  * there are no underrun/overrun in the case if there is a delay
493                  * between the two operations.
494                  */
495                 ret = skl_stop_pipe(ctx, mconfig->pipe);
496                 if (ret < 0)
497                         return ret;
498
499                 ret = skl_decoupled_trigger(substream, cmd);
500                 if ((cmd == SNDRV_PCM_TRIGGER_SUSPEND) && !w->ignore_suspend) {
501                         /* save the dpib and lpib positions */
502                         stream->dpib = readl(ebus->bus.remap_addr +
503                                         AZX_REG_VS_SDXDPIB_XBASE +
504                                         (AZX_REG_VS_SDXDPIB_XINTERVAL *
505                                         hdac_stream(stream)->index));
506
507                         stream->lpib = snd_hdac_stream_get_pos_lpib(
508                                                         hdac_stream(stream));
509                         snd_hdac_ext_stream_decouple(ebus, stream, false);
510                 }
511                 break;
512
513         default:
514                 return -EINVAL;
515         }
516
517         return 0;
518 }
519
520 static int skl_link_hw_params(struct snd_pcm_substream *substream,
521                                 struct snd_pcm_hw_params *params,
522                                 struct snd_soc_dai *dai)
523 {
524         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
525         struct hdac_ext_stream *link_dev;
526         struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
527         struct snd_soc_dai *codec_dai = rtd->codec_dai;
528         struct skl_pipe_params p_params = {0};
529         struct hdac_ext_link *link;
530         int stream_tag;
531
532         link_dev = snd_hdac_ext_stream_assign(ebus, substream,
533                                         HDAC_EXT_STREAM_TYPE_LINK);
534         if (!link_dev)
535                 return -EBUSY;
536
537         snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
538
539         link = snd_hdac_ext_bus_get_link(ebus, codec_dai->component->name);
540         if (!link)
541                 return -EINVAL;
542
543         stream_tag = hdac_stream(link_dev)->stream_tag;
544
545         /* set the stream tag in the codec dai dma params  */
546         snd_soc_dai_set_tdm_slot(codec_dai, stream_tag, 0, 0, 0);
547
548         p_params.s_fmt = snd_pcm_format_width(params_format(params));
549         p_params.ch = params_channels(params);
550         p_params.s_freq = params_rate(params);
551         p_params.stream = substream->stream;
552         p_params.link_dma_id = stream_tag - 1;
553         p_params.link_index = link->index;
554         p_params.format = params_format(params);
555
556         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
557                 p_params.link_bps = codec_dai->driver->playback.sig_bits;
558         else
559                 p_params.link_bps = codec_dai->driver->capture.sig_bits;
560
561         return skl_tplg_be_update_params(dai, &p_params);
562 }
563
564 static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
565                 struct snd_soc_dai *dai)
566 {
567         struct skl *skl = get_skl_ctx(dai->dev);
568         struct skl_module_cfg *mconfig = NULL;
569
570         /* In case of XRUN recovery, reset the FW pipe to clean state */
571         mconfig = skl_tplg_be_get_cpr_module(dai, substream->stream);
572         if (mconfig && !mconfig->pipe->passthru &&
573                 (substream->runtime->status->state == SNDRV_PCM_STATE_XRUN))
574                 skl_reset_pipe(skl->skl_sst, mconfig->pipe);
575
576         return 0;
577 }
578
579 static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
580         int cmd, struct snd_soc_dai *dai)
581 {
582         struct hdac_ext_stream *link_dev =
583                                 snd_soc_dai_get_dma_data(dai, substream);
584         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
585         struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
586
587         dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
588         switch (cmd) {
589         case SNDRV_PCM_TRIGGER_RESUME:
590         case SNDRV_PCM_TRIGGER_START:
591         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
592                 snd_hdac_ext_link_stream_start(link_dev);
593                 break;
594
595         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
596         case SNDRV_PCM_TRIGGER_SUSPEND:
597         case SNDRV_PCM_TRIGGER_STOP:
598                 snd_hdac_ext_link_stream_clear(link_dev);
599                 if (cmd == SNDRV_PCM_TRIGGER_SUSPEND)
600                         snd_hdac_ext_stream_decouple(ebus, stream, false);
601                 break;
602
603         default:
604                 return -EINVAL;
605         }
606         return 0;
607 }
608
609 static int skl_link_hw_free(struct snd_pcm_substream *substream,
610                 struct snd_soc_dai *dai)
611 {
612         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
613         struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
614         struct hdac_ext_stream *link_dev =
615                                 snd_soc_dai_get_dma_data(dai, substream);
616         struct hdac_ext_link *link;
617
618         dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
619
620         link_dev->link_prepared = 0;
621
622         link = snd_hdac_ext_bus_get_link(ebus, rtd->codec_dai->component->name);
623         if (!link)
624                 return -EINVAL;
625
626         snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
627         snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
628         return 0;
629 }
630
631 static const struct snd_soc_dai_ops skl_pcm_dai_ops = {
632         .startup = skl_pcm_open,
633         .shutdown = skl_pcm_close,
634         .prepare = skl_pcm_prepare,
635         .hw_params = skl_pcm_hw_params,
636         .hw_free = skl_pcm_hw_free,
637         .trigger = skl_pcm_trigger,
638 };
639
640 static const struct snd_soc_dai_ops skl_dmic_dai_ops = {
641         .hw_params = skl_be_hw_params,
642 };
643
644 static const struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
645         .hw_params = skl_be_hw_params,
646 };
647
648 static const struct snd_soc_dai_ops skl_link_dai_ops = {
649         .prepare = skl_link_pcm_prepare,
650         .hw_params = skl_link_hw_params,
651         .hw_free = skl_link_hw_free,
652         .trigger = skl_link_pcm_trigger,
653 };
654
655 static struct snd_soc_dai_driver skl_fe_dai[] = {
656 {
657         .name = "System Pin",
658         .ops = &skl_pcm_dai_ops,
659         .playback = {
660                 .stream_name = "System Playback",
661                 .channels_min = HDA_MONO,
662                 .channels_max = HDA_STEREO,
663                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
664                 .formats = SNDRV_PCM_FMTBIT_S16_LE |
665                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
666                 .sig_bits = 32,
667         },
668         .capture = {
669                 .stream_name = "System Capture",
670                 .channels_min = HDA_MONO,
671                 .channels_max = HDA_STEREO,
672                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
673                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
674                 .sig_bits = 32,
675         },
676 },
677 {
678         .name = "System Pin2",
679         .ops = &skl_pcm_dai_ops,
680         .playback = {
681                 .stream_name = "Headset Playback",
682                 .channels_min = HDA_MONO,
683                 .channels_max = HDA_STEREO,
684                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
685                         SNDRV_PCM_RATE_8000,
686                 .formats = SNDRV_PCM_FMTBIT_S16_LE |
687                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
688         },
689 },
690 {
691         .name = "Echoref Pin",
692         .ops = &skl_pcm_dai_ops,
693         .capture = {
694                 .stream_name = "Echoreference Capture",
695                 .channels_min = HDA_STEREO,
696                 .channels_max = HDA_STEREO,
697                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
698                         SNDRV_PCM_RATE_8000,
699                 .formats = SNDRV_PCM_FMTBIT_S16_LE |
700                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
701         },
702 },
703 {
704         .name = "Reference Pin",
705         .ops = &skl_pcm_dai_ops,
706         .capture = {
707                 .stream_name = "Reference Capture",
708                 .channels_min = HDA_MONO,
709                 .channels_max = HDA_QUAD,
710                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
711                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
712                 .sig_bits = 32,
713         },
714 },
715 {
716         .name = "Deepbuffer Pin",
717         .ops = &skl_pcm_dai_ops,
718         .playback = {
719                 .stream_name = "Deepbuffer Playback",
720                 .channels_min = HDA_STEREO,
721                 .channels_max = HDA_STEREO,
722                 .rates = SNDRV_PCM_RATE_48000,
723                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
724                 .sig_bits = 32,
725         },
726 },
727 {
728         .name = "LowLatency Pin",
729         .ops = &skl_pcm_dai_ops,
730         .playback = {
731                 .stream_name = "Low Latency Playback",
732                 .channels_min = HDA_STEREO,
733                 .channels_max = HDA_STEREO,
734                 .rates = SNDRV_PCM_RATE_48000,
735                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
736                 .sig_bits = 32,
737         },
738 },
739 {
740         .name = "DMIC Pin",
741         .ops = &skl_pcm_dai_ops,
742         .capture = {
743                 .stream_name = "DMIC Capture",
744                 .channels_min = HDA_MONO,
745                 .channels_max = HDA_QUAD,
746                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
747                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
748                 .sig_bits = 32,
749         },
750 },
751 {
752         .name = "HDMI1 Pin",
753         .ops = &skl_pcm_dai_ops,
754         .playback = {
755                 .stream_name = "HDMI1 Playback",
756                 .channels_min = HDA_STEREO,
757                 .channels_max = 8,
758                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
759                         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
760                         SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
761                         SNDRV_PCM_RATE_192000,
762                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
763                         SNDRV_PCM_FMTBIT_S32_LE,
764                 .sig_bits = 32,
765         },
766 },
767 {
768         .name = "HDMI2 Pin",
769         .ops = &skl_pcm_dai_ops,
770         .playback = {
771                 .stream_name = "HDMI2 Playback",
772                 .channels_min = HDA_STEREO,
773                 .channels_max = 8,
774                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
775                         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
776                         SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
777                         SNDRV_PCM_RATE_192000,
778                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
779                         SNDRV_PCM_FMTBIT_S32_LE,
780                 .sig_bits = 32,
781         },
782 },
783 {
784         .name = "HDMI3 Pin",
785         .ops = &skl_pcm_dai_ops,
786         .playback = {
787                 .stream_name = "HDMI3 Playback",
788                 .channels_min = HDA_STEREO,
789                 .channels_max = 8,
790                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
791                         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
792                         SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
793                         SNDRV_PCM_RATE_192000,
794                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
795                         SNDRV_PCM_FMTBIT_S32_LE,
796                 .sig_bits = 32,
797         },
798 },
799 };
800
801 /* BE CPU  Dais */
802 static struct snd_soc_dai_driver skl_platform_dai[] = {
803 {
804         .name = "SSP0 Pin",
805         .ops = &skl_be_ssp_dai_ops,
806         .playback = {
807                 .stream_name = "ssp0 Tx",
808                 .channels_min = HDA_STEREO,
809                 .channels_max = HDA_STEREO,
810                 .rates = SNDRV_PCM_RATE_48000,
811                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
812         },
813         .capture = {
814                 .stream_name = "ssp0 Rx",
815                 .channels_min = HDA_STEREO,
816                 .channels_max = HDA_STEREO,
817                 .rates = SNDRV_PCM_RATE_48000,
818                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
819         },
820 },
821 {
822         .name = "SSP1 Pin",
823         .ops = &skl_be_ssp_dai_ops,
824         .playback = {
825                 .stream_name = "ssp1 Tx",
826                 .channels_min = HDA_STEREO,
827                 .channels_max = HDA_STEREO,
828                 .rates = SNDRV_PCM_RATE_48000,
829                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
830         },
831         .capture = {
832                 .stream_name = "ssp1 Rx",
833                 .channels_min = HDA_STEREO,
834                 .channels_max = HDA_STEREO,
835                 .rates = SNDRV_PCM_RATE_48000,
836                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
837         },
838 },
839 {
840         .name = "SSP2 Pin",
841         .ops = &skl_be_ssp_dai_ops,
842         .playback = {
843                 .stream_name = "ssp2 Tx",
844                 .channels_min = HDA_STEREO,
845                 .channels_max = HDA_STEREO,
846                 .rates = SNDRV_PCM_RATE_48000,
847                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
848         },
849         .capture = {
850                 .stream_name = "ssp2 Rx",
851                 .channels_min = HDA_STEREO,
852                 .channels_max = HDA_STEREO,
853                 .rates = SNDRV_PCM_RATE_48000,
854                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
855         },
856 },
857 {
858         .name = "SSP3 Pin",
859         .ops = &skl_be_ssp_dai_ops,
860         .playback = {
861                 .stream_name = "ssp3 Tx",
862                 .channels_min = HDA_STEREO,
863                 .channels_max = HDA_STEREO,
864                 .rates = SNDRV_PCM_RATE_48000,
865                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
866         },
867         .capture = {
868                 .stream_name = "ssp3 Rx",
869                 .channels_min = HDA_STEREO,
870                 .channels_max = HDA_STEREO,
871                 .rates = SNDRV_PCM_RATE_48000,
872                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
873         },
874 },
875 {
876         .name = "SSP4 Pin",
877         .ops = &skl_be_ssp_dai_ops,
878         .playback = {
879                 .stream_name = "ssp4 Tx",
880                 .channels_min = HDA_STEREO,
881                 .channels_max = HDA_STEREO,
882                 .rates = SNDRV_PCM_RATE_48000,
883                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
884         },
885         .capture = {
886                 .stream_name = "ssp4 Rx",
887                 .channels_min = HDA_STEREO,
888                 .channels_max = HDA_STEREO,
889                 .rates = SNDRV_PCM_RATE_48000,
890                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
891         },
892 },
893 {
894         .name = "SSP5 Pin",
895         .ops = &skl_be_ssp_dai_ops,
896         .playback = {
897                 .stream_name = "ssp5 Tx",
898                 .channels_min = HDA_STEREO,
899                 .channels_max = HDA_STEREO,
900                 .rates = SNDRV_PCM_RATE_48000,
901                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
902         },
903         .capture = {
904                 .stream_name = "ssp5 Rx",
905                 .channels_min = HDA_STEREO,
906                 .channels_max = HDA_STEREO,
907                 .rates = SNDRV_PCM_RATE_48000,
908                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
909         },
910 },
911 {
912         .name = "iDisp1 Pin",
913         .ops = &skl_link_dai_ops,
914         .playback = {
915                 .stream_name = "iDisp1 Tx",
916                 .channels_min = HDA_STEREO,
917                 .channels_max = 8,
918                 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
919                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
920                         SNDRV_PCM_FMTBIT_S24_LE,
921         },
922 },
923 {
924         .name = "iDisp2 Pin",
925         .ops = &skl_link_dai_ops,
926         .playback = {
927                 .stream_name = "iDisp2 Tx",
928                 .channels_min = HDA_STEREO,
929                 .channels_max = 8,
930                 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
931                         SNDRV_PCM_RATE_48000,
932                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
933                         SNDRV_PCM_FMTBIT_S24_LE,
934         },
935 },
936 {
937         .name = "iDisp3 Pin",
938         .ops = &skl_link_dai_ops,
939         .playback = {
940                 .stream_name = "iDisp3 Tx",
941                 .channels_min = HDA_STEREO,
942                 .channels_max = 8,
943                 .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
944                         SNDRV_PCM_RATE_48000,
945                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
946                         SNDRV_PCM_FMTBIT_S24_LE,
947         },
948 },
949 {
950         .name = "DMIC01 Pin",
951         .ops = &skl_dmic_dai_ops,
952         .capture = {
953                 .stream_name = "DMIC01 Rx",
954                 .channels_min = HDA_MONO,
955                 .channels_max = HDA_QUAD,
956                 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
957                 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
958         },
959 },
960 {
961         .name = "HD-Codec Pin",
962         .ops = &skl_link_dai_ops,
963         .playback = {
964                 .stream_name = "HD-Codec Tx",
965                 .channels_min = HDA_STEREO,
966                 .channels_max = HDA_STEREO,
967                 .rates = SNDRV_PCM_RATE_48000,
968                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
969         },
970         .capture = {
971                 .stream_name = "HD-Codec Rx",
972                 .channels_min = HDA_STEREO,
973                 .channels_max = HDA_STEREO,
974                 .rates = SNDRV_PCM_RATE_48000,
975                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
976         },
977 },
978 };
979
980 int skl_dai_load(struct snd_soc_component *cmp,
981                  struct snd_soc_dai_driver *pcm_dai)
982 {
983         pcm_dai->ops = &skl_pcm_dai_ops;
984
985         return 0;
986 }
987
988 static int skl_platform_open(struct snd_pcm_substream *substream)
989 {
990         struct snd_soc_pcm_runtime *rtd = substream->private_data;
991         struct snd_soc_dai_link *dai_link = rtd->dai_link;
992
993         dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
994                                         dai_link->cpu_dai_name);
995
996         snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
997
998         return 0;
999 }
1000
1001 static int skl_coupled_trigger(struct snd_pcm_substream *substream,
1002                                         int cmd)
1003 {
1004         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
1005         struct hdac_bus *bus = ebus_to_hbus(ebus);
1006         struct hdac_ext_stream *stream;
1007         struct snd_pcm_substream *s;
1008         bool start;
1009         int sbits = 0;
1010         unsigned long cookie;
1011         struct hdac_stream *hstr;
1012
1013         stream = get_hdac_ext_stream(substream);
1014         hstr = hdac_stream(stream);
1015
1016         dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
1017
1018         if (!hstr->prepared)
1019                 return -EPIPE;
1020
1021         switch (cmd) {
1022         case SNDRV_PCM_TRIGGER_START:
1023         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1024         case SNDRV_PCM_TRIGGER_RESUME:
1025                 start = true;
1026                 break;
1027
1028         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1029         case SNDRV_PCM_TRIGGER_SUSPEND:
1030         case SNDRV_PCM_TRIGGER_STOP:
1031                 start = false;
1032                 break;
1033
1034         default:
1035                 return -EINVAL;
1036         }
1037
1038         snd_pcm_group_for_each_entry(s, substream) {
1039                 if (s->pcm->card != substream->pcm->card)
1040                         continue;
1041                 stream = get_hdac_ext_stream(s);
1042                 sbits |= 1 << hdac_stream(stream)->index;
1043                 snd_pcm_trigger_done(s, substream);
1044         }
1045
1046         spin_lock_irqsave(&bus->reg_lock, cookie);
1047
1048         /* first, set SYNC bits of corresponding streams */
1049         snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
1050
1051         snd_pcm_group_for_each_entry(s, substream) {
1052                 if (s->pcm->card != substream->pcm->card)
1053                         continue;
1054                 stream = get_hdac_ext_stream(s);
1055                 if (start)
1056                         snd_hdac_stream_start(hdac_stream(stream), true);
1057                 else
1058                         snd_hdac_stream_stop(hdac_stream(stream));
1059         }
1060         spin_unlock_irqrestore(&bus->reg_lock, cookie);
1061
1062         snd_hdac_stream_sync(hstr, start, sbits);
1063
1064         spin_lock_irqsave(&bus->reg_lock, cookie);
1065
1066         /* reset SYNC bits */
1067         snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
1068         if (start)
1069                 snd_hdac_stream_timecounter_init(hstr, sbits);
1070         spin_unlock_irqrestore(&bus->reg_lock, cookie);
1071
1072         return 0;
1073 }
1074
1075 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
1076                                         int cmd)
1077 {
1078         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
1079
1080         if (!(ebus_to_hbus(ebus))->ppcap)
1081                 return skl_coupled_trigger(substream, cmd);
1082
1083         return 0;
1084 }
1085
1086 static snd_pcm_uframes_t skl_platform_pcm_pointer
1087                         (struct snd_pcm_substream *substream)
1088 {
1089         struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
1090         struct hdac_ext_bus *ebus = get_bus_ctx(substream);
1091         unsigned int pos;
1092
1093         /*
1094          * Use DPIB for Playback stream as the periodic DMA Position-in-
1095          * Buffer Writes may be scheduled at the same time or later than
1096          * the MSI and does not guarantee to reflect the Position of the
1097          * last buffer that was transferred. Whereas DPIB register in
1098          * HAD space reflects the actual data that is transferred.
1099          * Use the position buffer for capture, as DPIB write gets
1100          * completed earlier than the actual data written to the DDR.
1101          *
1102          * For capture stream following workaround is required to fix the
1103          * incorrect position reporting.
1104          *
1105          * 1. Wait for 20us before reading the DMA position in buffer once
1106          * the interrupt is generated for stream completion as update happens
1107          * on the HDA frame boundary i.e. 20.833uSec.
1108          * 2. Read DPIB register to flush the DMA position value. This dummy
1109          * read is required to flush DMA position value.
1110          * 3. Read the DMA Position-in-Buffer. This value now will be equal to
1111          * or greater than period boundary.
1112          */
1113
1114         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1115                 pos = readl(ebus->bus.remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
1116                                 (AZX_REG_VS_SDXDPIB_XINTERVAL *
1117                                 hdac_stream(hstream)->index));
1118         } else {
1119                 udelay(20);
1120                 readl(ebus->bus.remap_addr +
1121                                 AZX_REG_VS_SDXDPIB_XBASE +
1122                                 (AZX_REG_VS_SDXDPIB_XINTERVAL *
1123                                  hdac_stream(hstream)->index));
1124                 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
1125         }
1126
1127         if (pos >= hdac_stream(hstream)->bufsize)
1128                 pos = 0;
1129
1130         return bytes_to_frames(substream->runtime, pos);
1131 }
1132
1133 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
1134                                 u64 nsec)
1135 {
1136         struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
1137         struct snd_soc_dai *codec_dai = rtd->codec_dai;
1138         u64 codec_frames, codec_nsecs;
1139
1140         if (!codec_dai->driver->ops->delay)
1141                 return nsec;
1142
1143         codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
1144         codec_nsecs = div_u64(codec_frames * 1000000000LL,
1145                               substream->runtime->rate);
1146
1147         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
1148                 return nsec + codec_nsecs;
1149
1150         return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
1151 }
1152
1153 static int skl_get_time_info(struct snd_pcm_substream *substream,
1154                         struct timespec *system_ts, struct timespec *audio_ts,
1155                         struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
1156                         struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
1157 {
1158         struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
1159         struct hdac_stream *hstr = hdac_stream(sstream);
1160         u64 nsec;
1161
1162         if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
1163                 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
1164
1165                 snd_pcm_gettime(substream->runtime, system_ts);
1166
1167                 nsec = timecounter_read(&hstr->tc);
1168                 nsec = div_u64(nsec, 3); /* can be optimized */
1169                 if (audio_tstamp_config->report_delay)
1170                         nsec = skl_adjust_codec_delay(substream, nsec);
1171
1172                 *audio_ts = ns_to_timespec(nsec);
1173
1174                 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
1175                 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
1176                 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
1177
1178         } else {
1179                 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
1180         }
1181
1182         return 0;
1183 }
1184
1185 static const struct snd_pcm_ops skl_platform_ops = {
1186         .open = skl_platform_open,
1187         .ioctl = snd_pcm_lib_ioctl,
1188         .trigger = skl_platform_pcm_trigger,
1189         .pointer = skl_platform_pcm_pointer,
1190         .get_time_info =  skl_get_time_info,
1191         .mmap = snd_pcm_lib_default_mmap,
1192         .page = snd_pcm_sgbuf_ops_page,
1193 };
1194
1195 static void skl_pcm_free(struct snd_pcm *pcm)
1196 {
1197         snd_pcm_lib_preallocate_free_for_all(pcm);
1198 }
1199
1200 #define MAX_PREALLOC_SIZE       (32 * 1024 * 1024)
1201
1202 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
1203 {
1204         struct snd_soc_dai *dai = rtd->cpu_dai;
1205         struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
1206         struct snd_pcm *pcm = rtd->pcm;
1207         unsigned int size;
1208         int retval = 0;
1209         struct skl *skl = ebus_to_skl(ebus);
1210
1211         if (dai->driver->playback.channels_min ||
1212                 dai->driver->capture.channels_min) {
1213                 /* buffer pre-allocation */
1214                 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
1215                 if (size > MAX_PREALLOC_SIZE)
1216                         size = MAX_PREALLOC_SIZE;
1217                 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1218                                                 SNDRV_DMA_TYPE_DEV_SG,
1219                                                 snd_dma_pci_data(skl->pci),
1220                                                 size, MAX_PREALLOC_SIZE);
1221                 if (retval) {
1222                         dev_err(dai->dev, "dma buffer allocation fail\n");
1223                         return retval;
1224                 }
1225         }
1226
1227         return retval;
1228 }
1229
1230 static int skl_get_module_info(struct skl *skl, struct skl_module_cfg *mconfig)
1231 {
1232         struct skl_sst *ctx = skl->skl_sst;
1233         struct skl_module_inst_id *pin_id;
1234         uuid_le *uuid_mod, *uuid_tplg;
1235         struct skl_module *skl_module;
1236         struct uuid_module *module;
1237         int i, ret = -EIO;
1238
1239         uuid_mod = (uuid_le *)mconfig->guid;
1240
1241         if (list_empty(&ctx->uuid_list)) {
1242                 dev_err(ctx->dev, "Module list is empty\n");
1243                 return -EIO;
1244         }
1245
1246         list_for_each_entry(module, &ctx->uuid_list, list) {
1247                 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
1248                         mconfig->id.module_id = module->id;
1249                         if (mconfig->module)
1250                                 mconfig->module->loadable = module->is_loadable;
1251                         ret = 0;
1252                         break;
1253                 }
1254         }
1255
1256         if (ret)
1257                 return ret;
1258
1259         uuid_mod = &module->uuid;
1260         ret = -EIO;
1261         for (i = 0; i < skl->nr_modules; i++) {
1262                 skl_module = skl->modules[i];
1263                 uuid_tplg = &skl_module->uuid;
1264                 if (!uuid_le_cmp(*uuid_mod, *uuid_tplg)) {
1265                         mconfig->module = skl_module;
1266                         ret = 0;
1267                         break;
1268                 }
1269         }
1270         if (skl->nr_modules && ret)
1271                 return ret;
1272
1273         list_for_each_entry(module, &ctx->uuid_list, list) {
1274                 for (i = 0; i < MAX_IN_QUEUE; i++) {
1275                         pin_id = &mconfig->m_in_pin[i].id;
1276                         if (!uuid_le_cmp(pin_id->mod_uuid, module->uuid))
1277                                 pin_id->module_id = module->id;
1278                 }
1279
1280                 for (i = 0; i < MAX_OUT_QUEUE; i++) {
1281                         pin_id = &mconfig->m_out_pin[i].id;
1282                         if (!uuid_le_cmp(pin_id->mod_uuid, module->uuid))
1283                                 pin_id->module_id = module->id;
1284                 }
1285         }
1286
1287         return 0;
1288 }
1289
1290 static int skl_populate_modules(struct skl *skl)
1291 {
1292         struct skl_pipeline *p;
1293         struct skl_pipe_module *m;
1294         struct snd_soc_dapm_widget *w;
1295         struct skl_module_cfg *mconfig;
1296         int ret = 0;
1297
1298         list_for_each_entry(p, &skl->ppl_list, node) {
1299                 list_for_each_entry(m, &p->pipe->w_list, node) {
1300                         w = m->w;
1301                         mconfig = w->priv;
1302
1303                         ret = skl_get_module_info(skl, mconfig);
1304                         if (ret < 0) {
1305                                 dev_err(skl->skl_sst->dev,
1306                                         "query module info failed\n");
1307                                 return ret;
1308                         }
1309                 }
1310         }
1311
1312         return ret;
1313 }
1314
1315 static int skl_platform_soc_probe(struct snd_soc_platform *platform)
1316 {
1317         struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
1318         struct skl *skl = ebus_to_skl(ebus);
1319         const struct skl_dsp_ops *ops;
1320         int ret;
1321
1322         pm_runtime_get_sync(platform->dev);
1323         if ((ebus_to_hbus(ebus))->ppcap) {
1324                 skl->platform = platform;
1325
1326                 /* init debugfs */
1327                 skl->debugfs = skl_debugfs_init(skl);
1328
1329                 ret = skl_tplg_init(platform, ebus);
1330                 if (ret < 0) {
1331                         dev_err(platform->dev, "Failed to init topology!\n");
1332                         return ret;
1333                 }
1334
1335                 /* load the firmwares, since all is set */
1336                 ops = skl_get_dsp_ops(skl->pci->device);
1337                 if (!ops)
1338                         return -EIO;
1339
1340                 if (skl->skl_sst->is_first_boot == false) {
1341                         dev_err(platform->dev, "DSP reports first boot done!!!\n");
1342                         return -EIO;
1343                 }
1344
1345                 /* disable dynamic clock gating during fw and lib download */
1346                 skl->skl_sst->enable_miscbdcge(platform->dev, false);
1347
1348                 ret = ops->init_fw(platform->dev, skl->skl_sst);
1349                 skl->skl_sst->enable_miscbdcge(platform->dev, true);
1350                 if (ret < 0) {
1351                         dev_err(platform->dev, "Failed to boot first fw: %d\n", ret);
1352                         return ret;
1353                 }
1354                 skl_populate_modules(skl);
1355                 skl->skl_sst->update_d0i3c = skl_update_d0i3c;
1356                 skl_dsp_enable_notification(skl->skl_sst, false);
1357
1358                 if (skl->cfg.astate_cfg != NULL) {
1359                         skl_dsp_set_astate_cfg(skl->skl_sst,
1360                                         skl->cfg.astate_cfg->count,
1361                                         skl->cfg.astate_cfg);
1362                 }
1363         }
1364         pm_runtime_mark_last_busy(platform->dev);
1365         pm_runtime_put_autosuspend(platform->dev);
1366
1367         return 0;
1368 }
1369 static const struct snd_soc_platform_driver skl_platform_drv  = {
1370         .probe          = skl_platform_soc_probe,
1371         .ops            = &skl_platform_ops,
1372         .pcm_new        = skl_pcm_new,
1373         .pcm_free       = skl_pcm_free,
1374 };
1375
1376 static const struct snd_soc_component_driver skl_component = {
1377         .name           = "pcm",
1378 };
1379
1380 int skl_platform_register(struct device *dev)
1381 {
1382         int ret;
1383         struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
1384         struct skl *skl = ebus_to_skl(ebus);
1385         struct snd_soc_dai_driver *dais;
1386         int num_dais = ARRAY_SIZE(skl_platform_dai);
1387
1388         INIT_LIST_HEAD(&skl->ppl_list);
1389         INIT_LIST_HEAD(&skl->bind_list);
1390
1391         ret = snd_soc_register_platform(dev, &skl_platform_drv);
1392         if (ret) {
1393                 dev_err(dev, "soc platform registration failed %d\n", ret);
1394                 return ret;
1395         }
1396
1397         skl->dais = kmemdup(skl_platform_dai, sizeof(skl_platform_dai),
1398                             GFP_KERNEL);
1399         if (!skl->dais) {
1400                 ret = -ENOMEM;
1401                 goto err;
1402         }
1403
1404         if (!skl->use_tplg_pcm) {
1405                 dais = krealloc(skl->dais, sizeof(skl_fe_dai) +
1406                                 sizeof(skl_platform_dai), GFP_KERNEL);
1407                 if (!dais) {
1408                         ret = -ENOMEM;
1409                         goto err;
1410                 }
1411
1412                 skl->dais = dais;
1413                 memcpy(&skl->dais[ARRAY_SIZE(skl_platform_dai)], skl_fe_dai,
1414                        sizeof(skl_fe_dai));
1415                 num_dais += ARRAY_SIZE(skl_fe_dai);
1416         }
1417
1418         ret = snd_soc_register_component(dev, &skl_component,
1419                                          skl->dais, num_dais);
1420         if (ret) {
1421                 dev_err(dev, "soc component registration failed %d\n", ret);
1422                 goto err;
1423         }
1424
1425         return 0;
1426 err:
1427         snd_soc_unregister_platform(dev);
1428         return ret;
1429
1430 }
1431
1432 int skl_platform_unregister(struct device *dev)
1433 {
1434         struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
1435         struct skl *skl = ebus_to_skl(ebus);
1436         struct skl_module_deferred_bind *modules, *tmp;
1437
1438         if (!list_empty(&skl->bind_list)) {
1439                 list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
1440                         list_del(&modules->node);
1441                         kfree(modules);
1442                 }
1443         }
1444
1445         snd_soc_unregister_component(dev);
1446         snd_soc_unregister_platform(dev);
1447         kfree(skl->dais);
1448
1449         return 0;
1450 }