blob: c171def2fcc9c381323d1d0ccedf16f36edb2227 [file] [log] [blame]
Jeeja KPa40e6932015-07-09 15:20:08 +05301/*
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
28#define HDA_MONO 1
29#define HDA_STEREO 2
30
31static struct snd_pcm_hardware azx_pcm_hw = {
32 .info = (SNDRV_PCM_INFO_MMAP |
33 SNDRV_PCM_INFO_INTERLEAVED |
34 SNDRV_PCM_INFO_BLOCK_TRANSFER |
35 SNDRV_PCM_INFO_MMAP_VALID |
36 SNDRV_PCM_INFO_PAUSE |
37 SNDRV_PCM_INFO_SYNC_START |
38 SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
39 SNDRV_PCM_INFO_HAS_LINK_ATIME |
40 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
41 .formats = SNDRV_PCM_FMTBIT_S16_LE,
42 .rates = SNDRV_PCM_RATE_48000,
43 .rate_min = 48000,
44 .rate_max = 48000,
45 .channels_min = 2,
46 .channels_max = 2,
47 .buffer_bytes_max = AZX_MAX_BUF_SIZE,
48 .period_bytes_min = 128,
49 .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
50 .periods_min = 2,
51 .periods_max = AZX_MAX_FRAG,
52 .fifo_size = 0,
53};
54
55static inline
56struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
57{
58 return substream->runtime->private_data;
59}
60
61static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
62{
63 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
64 struct hdac_stream *hstream = hdac_stream(stream);
65 struct hdac_bus *bus = hstream->bus;
66
67 return hbus_to_ebus(bus);
68}
69
70static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
71 struct snd_pcm_substream *substream,
72 size_t size)
73{
74 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
75
76 hdac_stream(stream)->bufsize = 0;
77 hdac_stream(stream)->period_bytes = 0;
78 hdac_stream(stream)->format_val = 0;
79
80 return snd_pcm_lib_malloc_pages(substream, size);
81}
82
83static int skl_substream_free_pages(struct hdac_bus *bus,
84 struct snd_pcm_substream *substream)
85{
86 return snd_pcm_lib_free_pages(substream);
87}
88
89static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
90 struct snd_pcm_runtime *runtime)
91{
92 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
93
94 /* avoid wrap-around with wall-clock */
95 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
96 20, 178000000);
97}
98
99static int skl_pcm_open(struct snd_pcm_substream *substream,
100 struct snd_soc_dai *dai)
101{
102 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
103 struct hdac_ext_stream *stream;
104 struct snd_pcm_runtime *runtime = substream->runtime;
105 struct skl_dma_params *dma_params;
106 int ret;
107
108 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
109 ret = pm_runtime_get_sync(dai->dev);
110 if (ret)
111 return ret;
112
113 stream = snd_hdac_ext_stream_assign(ebus, substream,
114 HDAC_EXT_STREAM_TYPE_COUPLED);
115 if (stream == NULL)
116 return -EBUSY;
117
118 skl_set_pcm_constrains(ebus, runtime);
119
120 /*
121 * disable WALLCLOCK timestamps for capture streams
122 * until we figure out how to handle digital inputs
123 */
124 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
125 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
126 runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
127 }
128
129 runtime->private_data = stream;
130
131 dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
132 if (!dma_params)
133 return -ENOMEM;
134
135 dma_params->stream_tag = hdac_stream(stream)->stream_tag;
136 snd_soc_dai_set_dma_data(dai, substream, dma_params);
137
138 dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
139 dma_params->stream_tag);
140 snd_pcm_set_sync(substream);
141
142 return 0;
143}
144
145static int skl_get_format(struct snd_pcm_substream *substream,
146 struct snd_soc_dai *dai)
147{
148 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
149 struct skl_dma_params *dma_params;
150 int format_val = 0;
151 struct snd_soc_dai *codec_dai = rtd->codec_dai;
152
153 dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
154 if (dma_params)
155 format_val = dma_params->format;
156
157 return format_val;
158}
159
160static int skl_pcm_prepare(struct snd_pcm_substream *substream,
161 struct snd_soc_dai *dai)
162{
163 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
164 unsigned int format_val;
165 int err;
166
167 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
168 if (hdac_stream(stream)->prepared) {
169 dev_dbg(dai->dev, "already stream is prepared - returning\n");
170 return 0;
171 }
172
173 format_val = skl_get_format(substream, dai);
174 dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
175 hdac_stream(stream)->stream_tag, format_val);
176 snd_hdac_stream_reset(hdac_stream(stream));
177
178 err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
179 if (err < 0)
180 return err;
181
182 err = snd_hdac_stream_setup(hdac_stream(stream));
183 if (err < 0)
184 return err;
185
186 hdac_stream(stream)->prepared = 1;
187
188 return err;
189}
190
191static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
192 struct snd_pcm_hw_params *params,
193 struct snd_soc_dai *dai)
194{
195 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
196 struct snd_pcm_runtime *runtime = substream->runtime;
197 int ret;
198
199 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
200 ret = skl_substream_alloc_pages(ebus, substream,
201 params_buffer_bytes(params));
202 if (ret < 0)
203 return ret;
204
205 dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
206 runtime->rate, runtime->channels, runtime->format);
207
208 return 0;
209}
210
211static void skl_pcm_close(struct snd_pcm_substream *substream,
212 struct snd_soc_dai *dai)
213{
214 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
215 struct skl_dma_params *dma_params = NULL;
216
217 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
218 snd_hdac_ext_stream_release(stream, HDAC_EXT_STREAM_TYPE_COUPLED);
219
220 dma_params = snd_soc_dai_get_dma_data(dai, substream);
221 /*
222 * now we should set this to NULL as we are freeing by the
223 * dma_params
224 */
225 snd_soc_dai_set_dma_data(dai, substream, NULL);
226
227 pm_runtime_mark_last_busy(dai->dev);
228 pm_runtime_put_autosuspend(dai->dev);
229 kfree(dma_params);
230}
231
232static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
233 struct snd_soc_dai *dai)
234{
235 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
236 struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
237
238 dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
239
240 snd_hdac_stream_cleanup(hdac_stream(stream));
241 hdac_stream(stream)->prepared = 0;
242
243 return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
244}
245
246static struct snd_soc_dai_ops skl_pcm_dai_ops = {
247 .startup = skl_pcm_open,
248 .shutdown = skl_pcm_close,
249 .prepare = skl_pcm_prepare,
250 .hw_params = skl_pcm_hw_params,
251 .hw_free = skl_pcm_hw_free,
252};
253
254static struct snd_soc_dai_driver skl_platform_dai[] = {
255{
256 .name = "System Pin",
257 .ops = &skl_pcm_dai_ops,
258 .playback = {
259 .stream_name = "System Playback",
260 .channels_min = HDA_MONO,
261 .channels_max = HDA_STEREO,
262 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
263 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
264 },
265 .capture = {
266 .stream_name = "System Capture",
267 .channels_min = HDA_MONO,
268 .channels_max = HDA_STEREO,
269 .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
270 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
271 },
272},
273{
274 .name = "Deepbuffer Pin",
275 .ops = &skl_pcm_dai_ops,
276 .playback = {
277 .stream_name = "Deepbuffer Playback",
278 .channels_min = HDA_STEREO,
279 .channels_max = HDA_STEREO,
280 .rates = SNDRV_PCM_RATE_48000,
281 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
282 },
283},
284{
285 .name = "LowLatency Pin",
286 .ops = &skl_pcm_dai_ops,
287 .playback = {
288 .stream_name = "Low Latency Playback",
289 .channels_min = HDA_STEREO,
290 .channels_max = HDA_STEREO,
291 .rates = SNDRV_PCM_RATE_48000,
292 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
293 },
294},
295};
296
297static int skl_platform_open(struct snd_pcm_substream *substream)
298{
299 struct snd_pcm_runtime *runtime;
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 struct snd_soc_dai_link *dai_link = rtd->dai_link;
302
303 dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
304 dai_link->cpu_dai_name);
305
306 runtime = substream->runtime;
307 snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
308
309 return 0;
310}
311
312static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
313 int cmd)
314{
315 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
316 struct hdac_bus *bus = ebus_to_hbus(ebus);
317 struct hdac_ext_stream *stream;
318 struct snd_pcm_substream *s;
319 bool start;
320 int sbits = 0;
321 unsigned long cookie;
322 struct hdac_stream *hstr;
323
324 stream = get_hdac_ext_stream(substream);
325 hstr = hdac_stream(stream);
326
327 dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
328
329 if (!hstr->prepared)
330 return -EPIPE;
331
332 switch (cmd) {
333 case SNDRV_PCM_TRIGGER_START:
334 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
335 case SNDRV_PCM_TRIGGER_RESUME:
336 start = true;
337 break;
338
339 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
340 case SNDRV_PCM_TRIGGER_SUSPEND:
341 case SNDRV_PCM_TRIGGER_STOP:
342 start = false;
343 break;
344
345 default:
346 return -EINVAL;
347 }
348
349 snd_pcm_group_for_each_entry(s, substream) {
350 if (s->pcm->card != substream->pcm->card)
351 continue;
352 stream = get_hdac_ext_stream(s);
353 sbits |= 1 << hdac_stream(stream)->index;
354 snd_pcm_trigger_done(s, substream);
355 }
356
357 spin_lock_irqsave(&bus->reg_lock, cookie);
358
359 /* first, set SYNC bits of corresponding streams */
360 snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
361
362 snd_pcm_group_for_each_entry(s, substream) {
363 if (s->pcm->card != substream->pcm->card)
364 continue;
365 stream = get_hdac_ext_stream(s);
366 if (start)
367 snd_hdac_stream_start(hdac_stream(stream), true);
368 else
369 snd_hdac_stream_stop(hdac_stream(stream));
370 }
371 spin_unlock_irqrestore(&bus->reg_lock, cookie);
372
373 snd_hdac_stream_sync(hstr, start, sbits);
374
375 spin_lock_irqsave(&bus->reg_lock, cookie);
376
377 /* reset SYNC bits */
378 snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
379 if (start)
380 snd_hdac_stream_timecounter_init(hstr, sbits);
381 spin_unlock_irqrestore(&bus->reg_lock, cookie);
382
383 return 0;
384}
385
386/* calculate runtime delay from LPIB */
387static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
388 struct hdac_ext_stream *sstream,
389 unsigned int pos)
390{
391 struct hdac_bus *bus = ebus_to_hbus(ebus);
392 struct hdac_stream *hstream = hdac_stream(sstream);
393 struct snd_pcm_substream *substream = hstream->substream;
394 int stream = substream->stream;
395 unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
396 int delay;
397
398 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
399 delay = pos - lpib_pos;
400 else
401 delay = lpib_pos - pos;
402
403 if (delay < 0) {
404 if (delay >= hstream->delay_negative_threshold)
405 delay = 0;
406 else
407 delay += hstream->bufsize;
408 }
409
410 if (delay >= hstream->period_bytes) {
411 dev_info(bus->dev,
412 "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
413 delay, hstream->period_bytes);
414 delay = 0;
415 }
416
417 return bytes_to_frames(substream->runtime, delay);
418}
419
420static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
421 int codec_delay)
422{
423 struct hdac_stream *hstr = hdac_stream(hstream);
424 struct snd_pcm_substream *substream = hstr->substream;
425 struct hdac_ext_bus *ebus = get_bus_ctx(substream);
426 unsigned int pos;
427 int delay;
428
429 /* use the position buffer as default */
430 pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
431
432 if (pos >= hdac_stream(hstream)->bufsize)
433 pos = 0;
434
435 if (substream->runtime) {
436 delay = skl_get_delay_from_lpib(ebus, hstream, pos)
437 + codec_delay;
438 substream->runtime->delay += delay;
439 }
440
441 return pos;
442}
443
444static snd_pcm_uframes_t skl_platform_pcm_pointer
445 (struct snd_pcm_substream *substream)
446{
447 struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
448
449 return bytes_to_frames(substream->runtime,
450 skl_get_position(hstream, 0));
451}
452
453static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
454 u64 nsec)
455{
456 struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
457 struct snd_soc_dai *codec_dai = rtd->codec_dai;
458 u64 codec_frames, codec_nsecs;
459
460 if (!codec_dai->driver->ops->delay)
461 return nsec;
462
463 codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
464 codec_nsecs = div_u64(codec_frames * 1000000000LL,
465 substream->runtime->rate);
466
467 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
468 return nsec + codec_nsecs;
469
470 return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
471}
472
473static int skl_get_time_info(struct snd_pcm_substream *substream,
474 struct timespec *system_ts, struct timespec *audio_ts,
475 struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
476 struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
477{
478 struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
479 struct hdac_stream *hstr = hdac_stream(sstream);
480 u64 nsec;
481
482 if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
483 (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
484
485 snd_pcm_gettime(substream->runtime, system_ts);
486
487 nsec = timecounter_read(&hstr->tc);
488 nsec = div_u64(nsec, 3); /* can be optimized */
489 if (audio_tstamp_config->report_delay)
490 nsec = skl_adjust_codec_delay(substream, nsec);
491
492 *audio_ts = ns_to_timespec(nsec);
493
494 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
495 audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
496 audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
497
498 } else {
499 audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
500 }
501
502 return 0;
503}
504
505static struct snd_pcm_ops skl_platform_ops = {
506 .open = skl_platform_open,
507 .ioctl = snd_pcm_lib_ioctl,
508 .trigger = skl_platform_pcm_trigger,
509 .pointer = skl_platform_pcm_pointer,
510 .get_time_info = skl_get_time_info,
511 .mmap = snd_pcm_lib_default_mmap,
512 .page = snd_pcm_sgbuf_ops_page,
513};
514
515static void skl_pcm_free(struct snd_pcm *pcm)
516{
517 snd_pcm_lib_preallocate_free_for_all(pcm);
518}
519
520#define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
521
522static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
523{
524 struct snd_soc_dai *dai = rtd->cpu_dai;
525 struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
526 struct snd_pcm *pcm = rtd->pcm;
527 unsigned int size;
528 int retval = 0;
529 struct skl *skl = ebus_to_skl(ebus);
530
531 if (dai->driver->playback.channels_min ||
532 dai->driver->capture.channels_min) {
533 /* buffer pre-allocation */
534 size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
535 if (size > MAX_PREALLOC_SIZE)
536 size = MAX_PREALLOC_SIZE;
537 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
538 SNDRV_DMA_TYPE_DEV_SG,
539 snd_dma_pci_data(skl->pci),
540 size, MAX_PREALLOC_SIZE);
541 if (retval) {
542 dev_err(dai->dev, "dma buffer allocationf fail\n");
543 return retval;
544 }
545 }
546
547 return retval;
548}
549
550static struct snd_soc_platform_driver skl_platform_drv = {
551 .ops = &skl_platform_ops,
552 .pcm_new = skl_pcm_new,
553 .pcm_free = skl_pcm_free,
554};
555
556static const struct snd_soc_component_driver skl_component = {
557 .name = "pcm",
558};
559
560int skl_platform_register(struct device *dev)
561{
562 int ret;
563
564 ret = snd_soc_register_platform(dev, &skl_platform_drv);
565 if (ret) {
566 dev_err(dev, "soc platform registration failed %d\n", ret);
567 return ret;
568 }
569 ret = snd_soc_register_component(dev, &skl_component,
570 skl_platform_dai,
571 ARRAY_SIZE(skl_platform_dai));
572 if (ret) {
573 dev_err(dev, "soc component registration failed %d\n", ret);
574 snd_soc_unregister_platform(dev);
575 }
576
577 return ret;
578
579}
580
581int skl_platform_unregister(struct device *dev)
582{
583 snd_soc_unregister_component(dev);
584 snd_soc_unregister_platform(dev);
585 return 0;
586}