xref: /openbmc/linux/sound/soc/intel/skylake/skl-pcm.c (revision 2c684d89)
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 <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include "skl.h"
27 #include "skl-topology.h"
28 
29 #define HDA_MONO 1
30 #define HDA_STEREO 2
31 
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,
44 	.rate_min =		48000,
45 	.rate_max =		48000,
46 	.channels_min =		2,
47 	.channels_max =		2,
48 	.buffer_bytes_max =	AZX_MAX_BUF_SIZE,
49 	.period_bytes_min =	128,
50 	.period_bytes_max =	AZX_MAX_BUF_SIZE / 2,
51 	.periods_min =		2,
52 	.periods_max =		AZX_MAX_FRAG,
53 	.fifo_size =		0,
54 };
55 
56 static inline
57 struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
58 {
59 	return substream->runtime->private_data;
60 }
61 
62 static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
63 {
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;
67 
68 	return hbus_to_ebus(bus);
69 }
70 
71 static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
72 				 struct snd_pcm_substream *substream,
73 				 size_t size)
74 {
75 	struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
76 
77 	hdac_stream(stream)->bufsize = 0;
78 	hdac_stream(stream)->period_bytes = 0;
79 	hdac_stream(stream)->format_val = 0;
80 
81 	return snd_pcm_lib_malloc_pages(substream, size);
82 }
83 
84 static int skl_substream_free_pages(struct hdac_bus *bus,
85 				struct snd_pcm_substream *substream)
86 {
87 	return snd_pcm_lib_free_pages(substream);
88 }
89 
90 static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
91 				 struct snd_pcm_runtime *runtime)
92 {
93 	snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
94 
95 	/* avoid wrap-around with wall-clock */
96 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
97 				     20, 178000000);
98 }
99 
100 static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
101 {
102 	if (ebus->ppcap)
103 		return HDAC_EXT_STREAM_TYPE_HOST;
104 	else
105 		return HDAC_EXT_STREAM_TYPE_COUPLED;
106 }
107 
108 static int skl_pcm_open(struct snd_pcm_substream *substream,
109 		struct snd_soc_dai *dai)
110 {
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;
115 	int ret;
116 
117 	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
118 	ret = pm_runtime_get_sync(dai->dev);
119 	if (ret < 0)
120 		return ret;
121 
122 	stream = snd_hdac_ext_stream_assign(ebus, substream,
123 					skl_get_host_stream_type(ebus));
124 	if (stream == NULL)
125 		return -EBUSY;
126 
127 	skl_set_pcm_constrains(ebus, runtime);
128 
129 	/*
130 	 * disable WALLCLOCK timestamps for capture streams
131 	 * until we figure out how to handle digital inputs
132 	 */
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;
136 	}
137 
138 	runtime->private_data = stream;
139 
140 	dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
141 	if (!dma_params)
142 		return -ENOMEM;
143 
144 	dma_params->stream_tag = hdac_stream(stream)->stream_tag;
145 	snd_soc_dai_set_dma_data(dai, substream, dma_params);
146 
147 	dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
148 				 dma_params->stream_tag);
149 	snd_pcm_set_sync(substream);
150 
151 	return 0;
152 }
153 
154 static int skl_get_format(struct snd_pcm_substream *substream,
155 		struct snd_soc_dai *dai)
156 {
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);
160 	int format_val = 0;
161 
162 	if (ebus->ppcap) {
163 		struct snd_pcm_runtime *runtime = substream->runtime;
164 
165 		format_val = snd_hdac_calc_stream_format(runtime->rate,
166 						runtime->channels,
167 						runtime->format,
168 						32, 0);
169 	} else {
170 		struct snd_soc_dai *codec_dai = rtd->codec_dai;
171 
172 		dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
173 		if (dma_params)
174 			format_val = dma_params->format;
175 	}
176 
177 	return format_val;
178 }
179 
180 static int skl_pcm_prepare(struct snd_pcm_substream *substream,
181 		struct snd_soc_dai *dai)
182 {
183 	struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
184 	unsigned int format_val;
185 	int err;
186 
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");
190 		return 0;
191 	}
192 
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));
197 
198 	err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
199 	if (err < 0)
200 		return err;
201 
202 	err = snd_hdac_stream_setup(hdac_stream(stream));
203 	if (err < 0)
204 		return err;
205 
206 	hdac_stream(stream)->prepared = 1;
207 
208 	return err;
209 }
210 
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)
214 {
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;
220 	int ret, dma_id;
221 
222 	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
223 	ret = skl_substream_alloc_pages(ebus, substream,
224 					  params_buffer_bytes(params));
225 	if (ret < 0)
226 		return ret;
227 
228 	dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
229 			runtime->rate, runtime->channels, runtime->format);
230 
231 	dma_id = hdac_stream(stream)->stream_tag - 1;
232 	dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
233 
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;
239 
240 	m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
241 	if (m_cfg)
242 		skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
243 
244 	return 0;
245 }
246 
247 static void skl_pcm_close(struct snd_pcm_substream *substream,
248 		struct snd_soc_dai *dai)
249 {
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;
253 
254 	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
255 
256 	snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
257 
258 	dma_params = snd_soc_dai_get_dma_data(dai, substream);
259 	/*
260 	 * now we should set this to NULL as we are freeing by the
261 	 * dma_params
262 	 */
263 	snd_soc_dai_set_dma_data(dai, substream, NULL);
264 
265 	pm_runtime_mark_last_busy(dai->dev);
266 	pm_runtime_put_autosuspend(dai->dev);
267 	kfree(dma_params);
268 }
269 
270 static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
271 		struct snd_soc_dai *dai)
272 {
273 	struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
274 	struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
275 
276 	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
277 
278 	snd_hdac_stream_cleanup(hdac_stream(stream));
279 	hdac_stream(stream)->prepared = 0;
280 
281 	return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
282 }
283 
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)
287 {
288 	struct skl_pipe_params p_params = {0};
289 
290 	p_params.s_fmt = snd_pcm_format_width(params_format(params));
291 	p_params.ch = params_channels(params);
292 	p_params.s_freq = params_rate(params);
293 	p_params.stream = substream->stream;
294 	skl_tplg_be_update_params(dai, &p_params);
295 
296 	return 0;
297 }
298 
299 static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
300 		struct snd_soc_dai *dai)
301 {
302 	struct skl *skl = get_skl_ctx(dai->dev);
303 	struct skl_sst *ctx = skl->skl_sst;
304 	struct skl_module_cfg *mconfig;
305 
306 	mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
307 	if (!mconfig)
308 		return -EIO;
309 
310 	switch (cmd) {
311 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
312 	case SNDRV_PCM_TRIGGER_RESUME:
313 		return skl_run_pipe(ctx, mconfig->pipe);
314 
315 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
316 	case SNDRV_PCM_TRIGGER_SUSPEND:
317 		return skl_stop_pipe(ctx, mconfig->pipe);
318 
319 	default:
320 		return 0;
321 	}
322 }
323 
324 static int skl_link_hw_params(struct snd_pcm_substream *substream,
325 				struct snd_pcm_hw_params *params,
326 				struct snd_soc_dai *dai)
327 {
328 	struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
329 	struct hdac_ext_stream *link_dev;
330 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
331 	struct skl_dma_params *dma_params;
332 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
333 	struct skl_pipe_params p_params = {0};
334 
335 	link_dev = snd_hdac_ext_stream_assign(ebus, substream,
336 					HDAC_EXT_STREAM_TYPE_LINK);
337 	if (!link_dev)
338 		return -EBUSY;
339 
340 	snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
341 
342 	/* set the stream tag in the codec dai dma params  */
343 	dma_params = (struct skl_dma_params *)
344 			snd_soc_dai_get_dma_data(codec_dai, substream);
345 	if (dma_params)
346 		dma_params->stream_tag =  hdac_stream(link_dev)->stream_tag;
347 	snd_soc_dai_set_dma_data(codec_dai, substream, (void *)dma_params);
348 
349 	p_params.s_fmt = snd_pcm_format_width(params_format(params));
350 	p_params.ch = params_channels(params);
351 	p_params.s_freq = params_rate(params);
352 	p_params.stream = substream->stream;
353 	p_params.link_dma_id = hdac_stream(link_dev)->stream_tag - 1;
354 
355 	skl_tplg_be_update_params(dai, &p_params);
356 
357 	return 0;
358 }
359 
360 static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
361 		struct snd_soc_dai *dai)
362 {
363 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
364 	struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
365 	struct hdac_ext_stream *link_dev =
366 			snd_soc_dai_get_dma_data(dai, substream);
367 	unsigned int format_val = 0;
368 	struct skl_dma_params *dma_params;
369 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
370 	struct hdac_ext_link *link;
371 
372 	if (link_dev->link_prepared) {
373 		dev_dbg(dai->dev, "already stream is prepared - returning\n");
374 		return 0;
375 	}
376 
377 	dma_params  = (struct skl_dma_params *)
378 			snd_soc_dai_get_dma_data(codec_dai, substream);
379 	if (dma_params)
380 		format_val = dma_params->format;
381 	dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
382 			hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
383 
384 	snd_hdac_ext_link_stream_reset(link_dev);
385 
386 	snd_hdac_ext_link_stream_setup(link_dev, format_val);
387 
388 	link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
389 	if (!link)
390 		return -EINVAL;
391 
392 	snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
393 	link_dev->link_prepared = 1;
394 
395 	return 0;
396 }
397 
398 static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
399 	int cmd, struct snd_soc_dai *dai)
400 {
401 	struct hdac_ext_stream *link_dev =
402 				snd_soc_dai_get_dma_data(dai, substream);
403 
404 	dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
405 	switch (cmd) {
406 	case SNDRV_PCM_TRIGGER_START:
407 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
408 	case SNDRV_PCM_TRIGGER_RESUME:
409 		snd_hdac_ext_link_stream_start(link_dev);
410 		break;
411 
412 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
413 	case SNDRV_PCM_TRIGGER_SUSPEND:
414 	case SNDRV_PCM_TRIGGER_STOP:
415 		snd_hdac_ext_link_stream_clear(link_dev);
416 		break;
417 
418 	default:
419 		return -EINVAL;
420 	}
421 	return 0;
422 }
423 
424 static int skl_link_hw_free(struct snd_pcm_substream *substream,
425 		struct snd_soc_dai *dai)
426 {
427 	struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
428 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
429 	struct hdac_ext_stream *link_dev =
430 				snd_soc_dai_get_dma_data(dai, substream);
431 	struct hdac_ext_link *link;
432 
433 	dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
434 
435 	link_dev->link_prepared = 0;
436 
437 	link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
438 	if (!link)
439 		return -EINVAL;
440 
441 	snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
442 	snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
443 	return 0;
444 }
445 
446 static int skl_be_startup(struct snd_pcm_substream *substream,
447 		struct snd_soc_dai *dai)
448 {
449 	return pm_runtime_get_sync(dai->dev);
450 }
451 
452 static void skl_be_shutdown(struct snd_pcm_substream *substream,
453 		struct snd_soc_dai *dai)
454 {
455 	pm_runtime_mark_last_busy(dai->dev);
456 	pm_runtime_put_autosuspend(dai->dev);
457 }
458 
459 static struct snd_soc_dai_ops skl_pcm_dai_ops = {
460 	.startup = skl_pcm_open,
461 	.shutdown = skl_pcm_close,
462 	.prepare = skl_pcm_prepare,
463 	.hw_params = skl_pcm_hw_params,
464 	.hw_free = skl_pcm_hw_free,
465 	.trigger = skl_pcm_trigger,
466 };
467 
468 static struct snd_soc_dai_ops skl_dmic_dai_ops = {
469 	.startup = skl_be_startup,
470 	.hw_params = skl_be_hw_params,
471 	.shutdown = skl_be_shutdown,
472 };
473 
474 static struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
475 	.startup = skl_be_startup,
476 	.hw_params = skl_be_hw_params,
477 	.shutdown = skl_be_shutdown,
478 };
479 
480 static struct snd_soc_dai_ops skl_link_dai_ops = {
481 	.startup = skl_be_startup,
482 	.prepare = skl_link_pcm_prepare,
483 	.hw_params = skl_link_hw_params,
484 	.hw_free = skl_link_hw_free,
485 	.trigger = skl_link_pcm_trigger,
486 	.shutdown = skl_be_shutdown,
487 };
488 
489 static struct snd_soc_dai_driver skl_platform_dai[] = {
490 {
491 	.name = "System Pin",
492 	.ops = &skl_pcm_dai_ops,
493 	.playback = {
494 		.stream_name = "System Playback",
495 		.channels_min = HDA_MONO,
496 		.channels_max = HDA_STEREO,
497 		.rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
498 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
499 	},
500 	.capture = {
501 		.stream_name = "System Capture",
502 		.channels_min = HDA_MONO,
503 		.channels_max = HDA_STEREO,
504 		.rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
505 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
506 	},
507 },
508 {
509 	.name = "Reference Pin",
510 	.ops = &skl_pcm_dai_ops,
511 	.capture = {
512 		.stream_name = "Reference Capture",
513 		.channels_min = HDA_MONO,
514 		.channels_max = HDA_STEREO,
515 		.rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
516 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
517 	},
518 },
519 {
520 	.name = "Deepbuffer Pin",
521 	.ops = &skl_pcm_dai_ops,
522 	.playback = {
523 		.stream_name = "Deepbuffer Playback",
524 		.channels_min = HDA_STEREO,
525 		.channels_max = HDA_STEREO,
526 		.rates = SNDRV_PCM_RATE_48000,
527 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
528 	},
529 },
530 {
531 	.name = "LowLatency Pin",
532 	.ops = &skl_pcm_dai_ops,
533 	.playback = {
534 		.stream_name = "Low Latency Playback",
535 		.channels_min = HDA_STEREO,
536 		.channels_max = HDA_STEREO,
537 		.rates = SNDRV_PCM_RATE_48000,
538 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
539 	},
540 },
541 /* BE CPU  Dais */
542 {
543 	.name = "SSP0 Pin",
544 	.ops = &skl_be_ssp_dai_ops,
545 	.playback = {
546 		.stream_name = "ssp0 Tx",
547 		.channels_min = HDA_STEREO,
548 		.channels_max = HDA_STEREO,
549 		.rates = SNDRV_PCM_RATE_48000,
550 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
551 	},
552 	.capture = {
553 		.stream_name = "ssp0 Rx",
554 		.channels_min = HDA_STEREO,
555 		.channels_max = HDA_STEREO,
556 		.rates = SNDRV_PCM_RATE_48000,
557 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
558 	},
559 },
560 {
561 	.name = "iDisp Pin",
562 	.ops = &skl_link_dai_ops,
563 	.playback = {
564 		.stream_name = "iDisp Tx",
565 		.channels_min = HDA_STEREO,
566 		.channels_max = HDA_STEREO,
567 		.rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
568 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
569 	},
570 },
571 {
572 	.name = "DMIC01 Pin",
573 	.ops = &skl_dmic_dai_ops,
574 	.capture = {
575 		.stream_name = "DMIC01 Rx",
576 		.channels_min = HDA_STEREO,
577 		.channels_max = HDA_STEREO,
578 		.rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
579 		.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
580 	},
581 },
582 {
583 	.name = "HD-Codec Pin",
584 	.ops = &skl_link_dai_ops,
585 	.playback = {
586 		.stream_name = "HD-Codec Tx",
587 		.channels_min = HDA_STEREO,
588 		.channels_max = HDA_STEREO,
589 		.rates = SNDRV_PCM_RATE_48000,
590 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
591 	},
592 	.capture = {
593 		.stream_name = "HD-Codec Rx",
594 		.channels_min = HDA_STEREO,
595 		.channels_max = HDA_STEREO,
596 		.rates = SNDRV_PCM_RATE_48000,
597 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
598 	},
599 },
600 };
601 
602 static int skl_platform_open(struct snd_pcm_substream *substream)
603 {
604 	struct snd_pcm_runtime *runtime;
605 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
606 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
607 
608 	dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
609 					dai_link->cpu_dai_name);
610 
611 	runtime = substream->runtime;
612 	snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
613 
614 	return 0;
615 }
616 
617 static int skl_coupled_trigger(struct snd_pcm_substream *substream,
618 					int cmd)
619 {
620 	struct hdac_ext_bus *ebus = get_bus_ctx(substream);
621 	struct hdac_bus *bus = ebus_to_hbus(ebus);
622 	struct hdac_ext_stream *stream;
623 	struct snd_pcm_substream *s;
624 	bool start;
625 	int sbits = 0;
626 	unsigned long cookie;
627 	struct hdac_stream *hstr;
628 
629 	stream = get_hdac_ext_stream(substream);
630 	hstr = hdac_stream(stream);
631 
632 	dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
633 
634 	if (!hstr->prepared)
635 		return -EPIPE;
636 
637 	switch (cmd) {
638 	case SNDRV_PCM_TRIGGER_START:
639 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
640 	case SNDRV_PCM_TRIGGER_RESUME:
641 		start = true;
642 		break;
643 
644 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
645 	case SNDRV_PCM_TRIGGER_SUSPEND:
646 	case SNDRV_PCM_TRIGGER_STOP:
647 		start = false;
648 		break;
649 
650 	default:
651 		return -EINVAL;
652 	}
653 
654 	snd_pcm_group_for_each_entry(s, substream) {
655 		if (s->pcm->card != substream->pcm->card)
656 			continue;
657 		stream = get_hdac_ext_stream(s);
658 		sbits |= 1 << hdac_stream(stream)->index;
659 		snd_pcm_trigger_done(s, substream);
660 	}
661 
662 	spin_lock_irqsave(&bus->reg_lock, cookie);
663 
664 	/* first, set SYNC bits of corresponding streams */
665 	snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
666 
667 	snd_pcm_group_for_each_entry(s, substream) {
668 		if (s->pcm->card != substream->pcm->card)
669 			continue;
670 		stream = get_hdac_ext_stream(s);
671 		if (start)
672 			snd_hdac_stream_start(hdac_stream(stream), true);
673 		else
674 			snd_hdac_stream_stop(hdac_stream(stream));
675 	}
676 	spin_unlock_irqrestore(&bus->reg_lock, cookie);
677 
678 	snd_hdac_stream_sync(hstr, start, sbits);
679 
680 	spin_lock_irqsave(&bus->reg_lock, cookie);
681 
682 	/* reset SYNC bits */
683 	snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
684 	if (start)
685 		snd_hdac_stream_timecounter_init(hstr, sbits);
686 	spin_unlock_irqrestore(&bus->reg_lock, cookie);
687 
688 	return 0;
689 }
690 
691 static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
692 		int cmd)
693 {
694 	struct hdac_ext_bus *ebus = get_bus_ctx(substream);
695 	struct hdac_bus *bus = ebus_to_hbus(ebus);
696 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
697 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
698 	struct hdac_ext_stream *stream;
699 	int start;
700 	unsigned long cookie;
701 	struct hdac_stream *hstr;
702 
703 	dev_dbg(bus->dev, "In %s cmd=%d streamname=%s\n", __func__, cmd, cpu_dai->name);
704 
705 	stream = get_hdac_ext_stream(substream);
706 	hstr = hdac_stream(stream);
707 
708 	if (!hstr->prepared)
709 		return -EPIPE;
710 
711 	switch (cmd) {
712 	case SNDRV_PCM_TRIGGER_START:
713 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
714 	case SNDRV_PCM_TRIGGER_RESUME:
715 		start = 1;
716 		break;
717 
718 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
719 	case SNDRV_PCM_TRIGGER_SUSPEND:
720 	case SNDRV_PCM_TRIGGER_STOP:
721 		start = 0;
722 		break;
723 
724 	default:
725 		return -EINVAL;
726 	}
727 
728 	spin_lock_irqsave(&bus->reg_lock, cookie);
729 
730 	if (start)
731 		snd_hdac_stream_start(hdac_stream(stream), true);
732 	else
733 		snd_hdac_stream_stop(hdac_stream(stream));
734 
735 	if (start)
736 		snd_hdac_stream_timecounter_init(hstr, 0);
737 
738 	spin_unlock_irqrestore(&bus->reg_lock, cookie);
739 
740 	return 0;
741 }
742 static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
743 					int cmd)
744 {
745 	struct hdac_ext_bus *ebus = get_bus_ctx(substream);
746 
747 	if (ebus->ppcap)
748 		return skl_decoupled_trigger(substream, cmd);
749 	else
750 		return skl_coupled_trigger(substream, cmd);
751 }
752 
753 /* calculate runtime delay from LPIB */
754 static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
755 				struct hdac_ext_stream *sstream,
756 				unsigned int pos)
757 {
758 	struct hdac_bus *bus = ebus_to_hbus(ebus);
759 	struct hdac_stream *hstream = hdac_stream(sstream);
760 	struct snd_pcm_substream *substream = hstream->substream;
761 	int stream = substream->stream;
762 	unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
763 	int delay;
764 
765 	if (stream == SNDRV_PCM_STREAM_PLAYBACK)
766 		delay = pos - lpib_pos;
767 	else
768 		delay = lpib_pos - pos;
769 
770 	if (delay < 0) {
771 		if (delay >= hstream->delay_negative_threshold)
772 			delay = 0;
773 		else
774 			delay += hstream->bufsize;
775 	}
776 
777 	if (delay >= hstream->period_bytes) {
778 		dev_info(bus->dev,
779 			 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
780 			 delay, hstream->period_bytes);
781 		delay = 0;
782 	}
783 
784 	return bytes_to_frames(substream->runtime, delay);
785 }
786 
787 static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
788 					int codec_delay)
789 {
790 	struct hdac_stream *hstr = hdac_stream(hstream);
791 	struct snd_pcm_substream *substream = hstr->substream;
792 	struct hdac_ext_bus *ebus = get_bus_ctx(substream);
793 	unsigned int pos;
794 	int delay;
795 
796 	/* use the position buffer as default */
797 	pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
798 
799 	if (pos >= hdac_stream(hstream)->bufsize)
800 		pos = 0;
801 
802 	if (substream->runtime) {
803 		delay = skl_get_delay_from_lpib(ebus, hstream, pos)
804 						 + codec_delay;
805 		substream->runtime->delay += delay;
806 	}
807 
808 	return pos;
809 }
810 
811 static snd_pcm_uframes_t skl_platform_pcm_pointer
812 			(struct snd_pcm_substream *substream)
813 {
814 	struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
815 
816 	return bytes_to_frames(substream->runtime,
817 			       skl_get_position(hstream, 0));
818 }
819 
820 static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
821 				u64 nsec)
822 {
823 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
824 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
825 	u64 codec_frames, codec_nsecs;
826 
827 	if (!codec_dai->driver->ops->delay)
828 		return nsec;
829 
830 	codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
831 	codec_nsecs = div_u64(codec_frames * 1000000000LL,
832 			      substream->runtime->rate);
833 
834 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
835 		return nsec + codec_nsecs;
836 
837 	return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
838 }
839 
840 static int skl_get_time_info(struct snd_pcm_substream *substream,
841 			struct timespec *system_ts, struct timespec *audio_ts,
842 			struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
843 			struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
844 {
845 	struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
846 	struct hdac_stream *hstr = hdac_stream(sstream);
847 	u64 nsec;
848 
849 	if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
850 		(audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
851 
852 		snd_pcm_gettime(substream->runtime, system_ts);
853 
854 		nsec = timecounter_read(&hstr->tc);
855 		nsec = div_u64(nsec, 3); /* can be optimized */
856 		if (audio_tstamp_config->report_delay)
857 			nsec = skl_adjust_codec_delay(substream, nsec);
858 
859 		*audio_ts = ns_to_timespec(nsec);
860 
861 		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
862 		audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
863 		audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
864 
865 	} else {
866 		audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
867 	}
868 
869 	return 0;
870 }
871 
872 static struct snd_pcm_ops skl_platform_ops = {
873 	.open = skl_platform_open,
874 	.ioctl = snd_pcm_lib_ioctl,
875 	.trigger = skl_platform_pcm_trigger,
876 	.pointer = skl_platform_pcm_pointer,
877 	.get_time_info =  skl_get_time_info,
878 	.mmap = snd_pcm_lib_default_mmap,
879 	.page = snd_pcm_sgbuf_ops_page,
880 };
881 
882 static void skl_pcm_free(struct snd_pcm *pcm)
883 {
884 	snd_pcm_lib_preallocate_free_for_all(pcm);
885 }
886 
887 #define MAX_PREALLOC_SIZE	(32 * 1024 * 1024)
888 
889 static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
890 {
891 	struct snd_soc_dai *dai = rtd->cpu_dai;
892 	struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
893 	struct snd_pcm *pcm = rtd->pcm;
894 	unsigned int size;
895 	int retval = 0;
896 	struct skl *skl = ebus_to_skl(ebus);
897 
898 	if (dai->driver->playback.channels_min ||
899 		dai->driver->capture.channels_min) {
900 		/* buffer pre-allocation */
901 		size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
902 		if (size > MAX_PREALLOC_SIZE)
903 			size = MAX_PREALLOC_SIZE;
904 		retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
905 						SNDRV_DMA_TYPE_DEV_SG,
906 						snd_dma_pci_data(skl->pci),
907 						size, MAX_PREALLOC_SIZE);
908 		if (retval) {
909 			dev_err(dai->dev, "dma buffer allocationf fail\n");
910 			return retval;
911 		}
912 	}
913 
914 	return retval;
915 }
916 
917 static int skl_platform_soc_probe(struct snd_soc_platform *platform)
918 {
919 	struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
920 
921 	if (ebus->ppcap)
922 		return skl_tplg_init(platform, ebus);
923 
924 	return 0;
925 }
926 static struct snd_soc_platform_driver skl_platform_drv  = {
927 	.probe		= skl_platform_soc_probe,
928 	.ops		= &skl_platform_ops,
929 	.pcm_new	= skl_pcm_new,
930 	.pcm_free	= skl_pcm_free,
931 };
932 
933 static const struct snd_soc_component_driver skl_component = {
934 	.name           = "pcm",
935 };
936 
937 int skl_platform_register(struct device *dev)
938 {
939 	int ret;
940 	struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
941 	struct skl *skl = ebus_to_skl(ebus);
942 
943 	INIT_LIST_HEAD(&skl->ppl_list);
944 	INIT_LIST_HEAD(&skl->dapm_path_list);
945 
946 	ret = snd_soc_register_platform(dev, &skl_platform_drv);
947 	if (ret) {
948 		dev_err(dev, "soc platform registration failed %d\n", ret);
949 		return ret;
950 	}
951 	ret = snd_soc_register_component(dev, &skl_component,
952 				skl_platform_dai,
953 				ARRAY_SIZE(skl_platform_dai));
954 	if (ret) {
955 		dev_err(dev, "soc component registration failed %d\n", ret);
956 		snd_soc_unregister_platform(dev);
957 	}
958 
959 	return ret;
960 
961 }
962 
963 int skl_platform_unregister(struct device *dev)
964 {
965 	snd_soc_unregister_component(dev);
966 	snd_soc_unregister_platform(dev);
967 	return 0;
968 }
969