f6a9acf6a4ef7ffe1dfddc2449a05cb14f9297b3
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / sound / soc / intel / sst-haswell-pcm.c
1 /*
2 * Intel SST Haswell/Broadwell PCM Support
3 *
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/delay.h>
21 #include <linux/pm_runtime.h>
22 #include <asm/page.h>
23 #include <asm/pgtable.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/dmaengine_pcm.h>
28 #include <sound/soc.h>
29 #include <sound/tlv.h>
30 #include <sound/compress_driver.h>
31
32 #include "sst-haswell-ipc.h"
33 #include "sst-dsp-priv.h"
34 #include "sst-dsp.h"
35
36 #define HSW_PCM_COUNT 6
37 #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
38
39 /* simple volume table */
40 static const u32 volume_map[] = {
41 HSW_VOLUME_MAX >> 30,
42 HSW_VOLUME_MAX >> 29,
43 HSW_VOLUME_MAX >> 28,
44 HSW_VOLUME_MAX >> 27,
45 HSW_VOLUME_MAX >> 26,
46 HSW_VOLUME_MAX >> 25,
47 HSW_VOLUME_MAX >> 24,
48 HSW_VOLUME_MAX >> 23,
49 HSW_VOLUME_MAX >> 22,
50 HSW_VOLUME_MAX >> 21,
51 HSW_VOLUME_MAX >> 20,
52 HSW_VOLUME_MAX >> 19,
53 HSW_VOLUME_MAX >> 18,
54 HSW_VOLUME_MAX >> 17,
55 HSW_VOLUME_MAX >> 16,
56 HSW_VOLUME_MAX >> 15,
57 HSW_VOLUME_MAX >> 14,
58 HSW_VOLUME_MAX >> 13,
59 HSW_VOLUME_MAX >> 12,
60 HSW_VOLUME_MAX >> 11,
61 HSW_VOLUME_MAX >> 10,
62 HSW_VOLUME_MAX >> 9,
63 HSW_VOLUME_MAX >> 8,
64 HSW_VOLUME_MAX >> 7,
65 HSW_VOLUME_MAX >> 6,
66 HSW_VOLUME_MAX >> 5,
67 HSW_VOLUME_MAX >> 4,
68 HSW_VOLUME_MAX >> 3,
69 HSW_VOLUME_MAX >> 2,
70 HSW_VOLUME_MAX >> 1,
71 HSW_VOLUME_MAX >> 0,
72 };
73
74 #define HSW_PCM_PERIODS_MAX 64
75 #define HSW_PCM_PERIODS_MIN 2
76
77 #define HSW_PCM_DAI_ID_SYSTEM 0
78 #define HSW_PCM_DAI_ID_OFFLOAD0 1
79 #define HSW_PCM_DAI_ID_OFFLOAD1 2
80 #define HSW_PCM_DAI_ID_LOOPBACK 3
81 #define HSW_PCM_DAI_ID_CAPTURE 4
82
83
84 static const struct snd_pcm_hardware hsw_pcm_hardware = {
85 .info = SNDRV_PCM_INFO_MMAP |
86 SNDRV_PCM_INFO_MMAP_VALID |
87 SNDRV_PCM_INFO_INTERLEAVED |
88 SNDRV_PCM_INFO_PAUSE |
89 SNDRV_PCM_INFO_RESUME |
90 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
91 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
92 SNDRV_PCM_FMTBIT_S32_LE,
93 .period_bytes_min = PAGE_SIZE,
94 .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
95 .periods_min = HSW_PCM_PERIODS_MIN,
96 .periods_max = HSW_PCM_PERIODS_MAX,
97 .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
98 };
99
100 struct hsw_pcm_module_map {
101 int dai_id;
102 enum sst_hsw_module_id mod_id;
103 };
104
105 /* private data for each PCM DSP stream */
106 struct hsw_pcm_data {
107 int dai_id;
108 struct sst_hsw_stream *stream;
109 struct sst_module_runtime *runtime;
110 struct sst_module_runtime_context context;
111 struct snd_pcm *hsw_pcm;
112 u32 volume[2];
113 struct snd_pcm_substream *substream;
114 struct snd_compr_stream *cstream;
115 unsigned int wpos;
116 struct mutex mutex;
117 bool allocated;
118 int persistent_offset;
119 };
120
121 enum hsw_pm_state {
122 HSW_PM_STATE_D3 = 0,
123 HSW_PM_STATE_D0 = 1,
124 };
125
126 /* private data for the driver */
127 struct hsw_priv_data {
128 /* runtime DSP */
129 struct sst_hsw *hsw;
130 struct device *dev;
131 enum hsw_pm_state pm_state;
132 struct snd_soc_card *soc_card;
133
134 /* page tables */
135 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
136
137 /* DAI data */
138 struct hsw_pcm_data pcm[HSW_PCM_COUNT];
139 };
140
141 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
142
143 static inline u32 hsw_mixer_to_ipc(unsigned int value)
144 {
145 if (value >= ARRAY_SIZE(volume_map))
146 return volume_map[0];
147 else
148 return volume_map[value];
149 }
150
151 static inline unsigned int hsw_ipc_to_mixer(u32 value)
152 {
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
156 if (volume_map[i] >= value)
157 return i;
158 }
159
160 return i - 1;
161 }
162
163 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
164 struct snd_ctl_elem_value *ucontrol)
165 {
166 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
167 struct soc_mixer_control *mc =
168 (struct soc_mixer_control *)kcontrol->private_value;
169 struct hsw_priv_data *pdata =
170 snd_soc_platform_get_drvdata(platform);
171 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
172 struct sst_hsw *hsw = pdata->hsw;
173 u32 volume;
174
175 mutex_lock(&pcm_data->mutex);
176 pm_runtime_get_sync(pdata->dev);
177
178 if (!pcm_data->stream) {
179 pcm_data->volume[0] =
180 hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
181 pcm_data->volume[1] =
182 hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
183 pm_runtime_mark_last_busy(pdata->dev);
184 pm_runtime_put_autosuspend(pdata->dev);
185 mutex_unlock(&pcm_data->mutex);
186 return 0;
187 }
188
189 if (ucontrol->value.integer.value[0] ==
190 ucontrol->value.integer.value[1]) {
191 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
192 /* apply volume value to all channels */
193 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
194 } else {
195 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
196 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
197 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
198 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
199 }
200
201 pm_runtime_mark_last_busy(pdata->dev);
202 pm_runtime_put_autosuspend(pdata->dev);
203 mutex_unlock(&pcm_data->mutex);
204 return 0;
205 }
206
207 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
208 struct snd_ctl_elem_value *ucontrol)
209 {
210 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
211 struct soc_mixer_control *mc =
212 (struct soc_mixer_control *)kcontrol->private_value;
213 struct hsw_priv_data *pdata =
214 snd_soc_platform_get_drvdata(platform);
215 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
216 struct sst_hsw *hsw = pdata->hsw;
217 u32 volume;
218
219 mutex_lock(&pcm_data->mutex);
220 pm_runtime_get_sync(pdata->dev);
221
222 if (!pcm_data->stream) {
223 ucontrol->value.integer.value[0] =
224 hsw_ipc_to_mixer(pcm_data->volume[0]);
225 ucontrol->value.integer.value[1] =
226 hsw_ipc_to_mixer(pcm_data->volume[1]);
227 pm_runtime_mark_last_busy(pdata->dev);
228 pm_runtime_put_autosuspend(pdata->dev);
229 mutex_unlock(&pcm_data->mutex);
230 return 0;
231 }
232
233 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
234 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
235 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
236 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
237
238 pm_runtime_mark_last_busy(pdata->dev);
239 pm_runtime_put_autosuspend(pdata->dev);
240 mutex_unlock(&pcm_data->mutex);
241
242 return 0;
243 }
244
245 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_value *ucontrol)
247 {
248 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
249 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
250 struct sst_hsw *hsw = pdata->hsw;
251 u32 volume;
252
253 pm_runtime_get_sync(pdata->dev);
254
255 if (ucontrol->value.integer.value[0] ==
256 ucontrol->value.integer.value[1]) {
257
258 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
259 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
260
261 } else {
262 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
263 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
264
265 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
266 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
267 }
268
269 pm_runtime_mark_last_busy(pdata->dev);
270 pm_runtime_put_autosuspend(pdata->dev);
271 return 0;
272 }
273
274 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
275 struct snd_ctl_elem_value *ucontrol)
276 {
277 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
278 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
279 struct sst_hsw *hsw = pdata->hsw;
280 unsigned int volume = 0;
281
282 pm_runtime_get_sync(pdata->dev);
283 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
284 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
285
286 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
287 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
288
289 pm_runtime_mark_last_busy(pdata->dev);
290 pm_runtime_put_autosuspend(pdata->dev);
291 return 0;
292 }
293
294 /* TLV used by both global and stream volumes */
295 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
296
297 /* System Pin has no volume control */
298 static const struct snd_kcontrol_new hsw_volume_controls[] = {
299 /* Global DSP volume */
300 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
301 ARRAY_SIZE(volume_map) - 1, 0,
302 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
303 /* Offload 0 volume */
304 SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
305 ARRAY_SIZE(volume_map) - 1, 0,
306 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
307 /* Offload 1 volume */
308 SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
309 ARRAY_SIZE(volume_map) - 1, 0,
310 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
311 /* Mic Capture volume */
312 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 0, 0, 8,
313 ARRAY_SIZE(volume_map) - 1, 0,
314 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
315 };
316
317 /* Create DMA buffer page table for DSP */
318 static int create_adsp_page_table(struct snd_pcm_substream *substream,
319 struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
320 unsigned char *dma_area, size_t size, int pcm)
321 {
322 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
323 int i, pages, stream = substream->stream;
324
325 pages = snd_sgbuf_aligned_pages(size);
326
327 dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
328 dma_area, size, pages);
329
330 for (i = 0; i < pages; i++) {
331 u32 idx = (((i << 2) + i)) >> 1;
332 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
333 u32 *pg_table;
334
335 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
336
337 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
338
339 if (i & 1)
340 *pg_table |= (pfn << 4);
341 else
342 *pg_table |= pfn;
343 }
344
345 return 0;
346 }
347
348 /* this may get called several times by oss emulation */
349 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
350 struct snd_pcm_hw_params *params)
351 {
352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct hsw_priv_data *pdata =
355 snd_soc_platform_get_drvdata(rtd->platform);
356 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
357 struct sst_hsw *hsw = pdata->hsw;
358 struct sst_module *module_data;
359 struct sst_dsp *dsp;
360 struct snd_dma_buffer *dmab;
361 enum sst_hsw_stream_type stream_type;
362 enum sst_hsw_stream_path_id path_id;
363 u32 rate, bits, map, pages, module_id;
364 u8 channels;
365 int ret;
366
367 /* check if we are being called a subsequent time */
368 if (pcm_data->allocated) {
369 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
370 if (ret < 0)
371 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
372 ret);
373
374 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
375 if (ret < 0) {
376 dev_dbg(rtd->dev, "error: free stream failed %d\n",
377 ret);
378 return ret;
379 }
380 pcm_data->allocated = false;
381
382 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
383 hsw_notify_pointer, pcm_data);
384 if (pcm_data->stream == NULL) {
385 dev_err(rtd->dev, "error: failed to create stream\n");
386 return -EINVAL;
387 }
388 }
389
390 /* stream direction */
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
393 else
394 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
395
396 /* DSP stream type depends on DAI ID */
397 switch (rtd->cpu_dai->id) {
398 case 0:
399 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
400 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
401 module_id = SST_HSW_MODULE_PCM_SYSTEM;
402 }
403 else {
404 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
405 module_id = SST_HSW_MODULE_PCM_CAPTURE;
406 }
407 break;
408 case 1:
409 case 2:
410 stream_type = SST_HSW_STREAM_TYPE_RENDER;
411 module_id = SST_HSW_MODULE_PCM;
412 break;
413 case 3:
414 /* path ID needs to be OUT for loopback */
415 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
416 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
417 module_id = SST_HSW_MODULE_PCM_REFERENCE;
418 break;
419 default:
420 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
421 rtd->cpu_dai->id);
422 return -EINVAL;
423 };
424
425 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
426 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
427 if (ret < 0) {
428 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
429 return ret;
430 }
431
432 rate = params_rate(params);
433 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
434 if (ret < 0) {
435 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
436 return ret;
437 }
438
439 switch (params_format(params)) {
440 case SNDRV_PCM_FORMAT_S16_LE:
441 bits = SST_HSW_DEPTH_16BIT;
442 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
443 break;
444 case SNDRV_PCM_FORMAT_S24_LE:
445 bits = SST_HSW_DEPTH_32BIT;
446 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
447 break;
448 case SNDRV_PCM_FORMAT_S8:
449 bits = SST_HSW_DEPTH_8BIT;
450 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
451 break;
452 case SNDRV_PCM_FORMAT_S32_LE:
453 bits = SST_HSW_DEPTH_32BIT;
454 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
455 break;
456 default:
457 dev_err(rtd->dev, "error: invalid format %d\n",
458 params_format(params));
459 return -EINVAL;
460 }
461
462 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
463 if (ret < 0) {
464 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
465 return ret;
466 }
467
468 channels = params_channels(params);
469 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
470 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
471 map, SST_HSW_CHANNEL_CONFIG_STEREO);
472
473 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
474 if (ret < 0) {
475 dev_err(rtd->dev, "error: could not set channels %d\n",
476 channels);
477 return ret;
478 }
479
480 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
481 if (ret < 0) {
482 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
483 params_buffer_bytes(params), ret);
484 return ret;
485 }
486
487 dmab = snd_pcm_get_dma_buf(substream);
488
489 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
490 runtime->dma_bytes, rtd->cpu_dai->id);
491 if (ret < 0)
492 return ret;
493
494 sst_hsw_stream_set_style(hsw, pcm_data->stream,
495 SST_HSW_INTERLEAVING_PER_CHANNEL);
496
497 if (runtime->dma_bytes % PAGE_SIZE)
498 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
499 else
500 pages = runtime->dma_bytes / PAGE_SIZE;
501
502 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
503 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
504 pages, runtime->dma_bytes, 0,
505 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
506 if (ret < 0) {
507 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
508 return ret;
509 }
510
511 dsp = sst_hsw_get_dsp(hsw);
512
513 module_data = sst_module_get_from_id(dsp, module_id);
514 if (module_data == NULL) {
515 dev_err(rtd->dev, "error: failed to get module config\n");
516 return -EINVAL;
517 }
518
519 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
520 pcm_data->runtime);
521
522 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
523 if (ret < 0) {
524 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
525 return ret;
526 }
527
528 if (!pcm_data->allocated) {
529 /* Set previous saved volume */
530 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
531 0, pcm_data->volume[0]);
532 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
533 1, pcm_data->volume[1]);
534 pcm_data->allocated = true;
535 }
536
537 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
538 if (ret < 0)
539 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
540
541 return 0;
542 }
543
544 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
545 {
546 snd_pcm_lib_free_pages(substream);
547 return 0;
548 }
549
550 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
551 {
552 struct snd_soc_pcm_runtime *rtd = substream->private_data;
553 struct hsw_priv_data *pdata =
554 snd_soc_platform_get_drvdata(rtd->platform);
555 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
556 struct sst_hsw *hsw = pdata->hsw;
557
558 switch (cmd) {
559 case SNDRV_PCM_TRIGGER_START:
560 case SNDRV_PCM_TRIGGER_RESUME:
561 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
562 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
563 break;
564 case SNDRV_PCM_TRIGGER_STOP:
565 case SNDRV_PCM_TRIGGER_SUSPEND:
566 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
567 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
568 break;
569 default:
570 break;
571 }
572
573 return 0;
574 }
575
576 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
577 {
578 struct hsw_pcm_data *pcm_data = data;
579 struct snd_pcm_substream *substream = pcm_data->substream;
580 struct snd_pcm_runtime *runtime = substream->runtime;
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 u32 pos;
583
584 pos = frames_to_bytes(runtime,
585 (runtime->control->appl_ptr % runtime->buffer_size));
586
587 dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
588
589 /* let alsa know we have play a period */
590 snd_pcm_period_elapsed(substream);
591 return pos;
592 }
593
594 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
595 {
596 struct snd_soc_pcm_runtime *rtd = substream->private_data;
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct hsw_priv_data *pdata =
599 snd_soc_platform_get_drvdata(rtd->platform);
600 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
601 struct sst_hsw *hsw = pdata->hsw;
602 snd_pcm_uframes_t offset;
603 uint64_t ppos;
604 u32 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
605
606 offset = bytes_to_frames(runtime, position);
607 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
608
609 dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
610 position, ppos);
611 return offset;
612 }
613
614 static int hsw_pcm_open(struct snd_pcm_substream *substream)
615 {
616 struct snd_soc_pcm_runtime *rtd = substream->private_data;
617 struct hsw_priv_data *pdata =
618 snd_soc_platform_get_drvdata(rtd->platform);
619 struct hsw_pcm_data *pcm_data;
620 struct sst_hsw *hsw = pdata->hsw;
621
622 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
623
624 mutex_lock(&pcm_data->mutex);
625 pm_runtime_get_sync(pdata->dev);
626
627 snd_soc_pcm_set_drvdata(rtd, pcm_data);
628 pcm_data->substream = substream;
629
630 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
631
632 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
633 hsw_notify_pointer, pcm_data);
634 if (pcm_data->stream == NULL) {
635 dev_err(rtd->dev, "error: failed to create stream\n");
636 pm_runtime_mark_last_busy(pdata->dev);
637 pm_runtime_put_autosuspend(pdata->dev);
638 mutex_unlock(&pcm_data->mutex);
639 return -EINVAL;
640 }
641
642 mutex_unlock(&pcm_data->mutex);
643 return 0;
644 }
645
646 static int hsw_pcm_close(struct snd_pcm_substream *substream)
647 {
648 struct snd_soc_pcm_runtime *rtd = substream->private_data;
649 struct hsw_priv_data *pdata =
650 snd_soc_platform_get_drvdata(rtd->platform);
651 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
652 struct sst_hsw *hsw = pdata->hsw;
653 int ret;
654
655 mutex_lock(&pcm_data->mutex);
656 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
657 if (ret < 0) {
658 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
659 goto out;
660 }
661
662 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
663 if (ret < 0) {
664 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
665 goto out;
666 }
667 pcm_data->allocated = 0;
668 pcm_data->stream = NULL;
669
670 out:
671 pm_runtime_mark_last_busy(pdata->dev);
672 pm_runtime_put_autosuspend(pdata->dev);
673 mutex_unlock(&pcm_data->mutex);
674 return ret;
675 }
676
677 static struct snd_pcm_ops hsw_pcm_ops = {
678 .open = hsw_pcm_open,
679 .close = hsw_pcm_close,
680 .ioctl = snd_pcm_lib_ioctl,
681 .hw_params = hsw_pcm_hw_params,
682 .hw_free = hsw_pcm_hw_free,
683 .trigger = hsw_pcm_trigger,
684 .pointer = hsw_pcm_pointer,
685 .page = snd_pcm_sgbuf_ops_page,
686 };
687
688 /* static mappings between PCMs and modules - may be dynamic in future */
689 static struct hsw_pcm_module_map mod_map[] = {
690 {HSW_PCM_DAI_ID_SYSTEM, SST_HSW_MODULE_PCM_SYSTEM},
691 {HSW_PCM_DAI_ID_OFFLOAD0, SST_HSW_MODULE_PCM},
692 {HSW_PCM_DAI_ID_OFFLOAD1, SST_HSW_MODULE_PCM},
693 {HSW_PCM_DAI_ID_LOOPBACK, SST_HSW_MODULE_PCM_REFERENCE},
694 {HSW_PCM_DAI_ID_CAPTURE, SST_HSW_MODULE_PCM_CAPTURE},
695 };
696
697 static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
698 {
699 struct sst_hsw *hsw = pdata->hsw;
700 struct hsw_pcm_data *pcm_data;
701 int i;
702
703 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
704 pcm_data = &pdata->pcm[i];
705
706 /* create new runtime module, use same offset if recreated */
707 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
708 mod_map[i].mod_id, pcm_data->persistent_offset);
709 if (pcm_data->runtime == NULL)
710 goto err;
711 pcm_data->persistent_offset =
712 pcm_data->runtime->persistent_offset;
713 }
714
715 return 0;
716
717 err:
718 for (--i; i >= 0; i--) {
719 pcm_data = &pdata->pcm[i];
720 sst_hsw_runtime_module_free(pcm_data->runtime);
721 }
722
723 return -ENODEV;
724 }
725
726 static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
727 {
728 struct hsw_pcm_data *pcm_data;
729 int i;
730
731 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
732 pcm_data = &pdata->pcm[i];
733
734 sst_hsw_runtime_module_free(pcm_data->runtime);
735 }
736 }
737
738 static void hsw_pcm_free(struct snd_pcm *pcm)
739 {
740 snd_pcm_lib_preallocate_free_for_all(pcm);
741 }
742
743 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
744 {
745 struct snd_pcm *pcm = rtd->pcm;
746 struct snd_soc_platform *platform = rtd->platform;
747 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
748 struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
749 struct device *dev = pdata->dma_dev;
750 int ret = 0;
751
752 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
753 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
754 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
755 SNDRV_DMA_TYPE_DEV_SG,
756 dev,
757 hsw_pcm_hardware.buffer_bytes_max,
758 hsw_pcm_hardware.buffer_bytes_max);
759 if (ret) {
760 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
761 ret);
762 return ret;
763 }
764 }
765 priv_data->pcm[rtd->cpu_dai->id].hsw_pcm = pcm;
766
767 return ret;
768 }
769
770 #define HSW_FORMATS \
771 (SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S24_LE | \
772 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE |\
773 SNDRV_PCM_FMTBIT_S8)
774
775 static struct snd_soc_dai_driver hsw_dais[] = {
776 {
777 .name = "System Pin",
778 .id = HSW_PCM_DAI_ID_SYSTEM,
779 .playback = {
780 .stream_name = "System Playback",
781 .channels_min = 2,
782 .channels_max = 2,
783 .rates = SNDRV_PCM_RATE_48000,
784 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
785 },
786 .capture = {
787 .stream_name = "Analog Capture",
788 .channels_min = 2,
789 .channels_max = 4,
790 .rates = SNDRV_PCM_RATE_48000,
791 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
792 },
793 },
794 {
795 /* PCM */
796 .name = "Offload0 Pin",
797 .id = HSW_PCM_DAI_ID_OFFLOAD0,
798 .playback = {
799 .stream_name = "Offload0 Playback",
800 .channels_min = 2,
801 .channels_max = 2,
802 .rates = SNDRV_PCM_RATE_8000_192000,
803 .formats = HSW_FORMATS,
804 },
805 },
806 {
807 /* PCM */
808 .name = "Offload1 Pin",
809 .id = HSW_PCM_DAI_ID_OFFLOAD1,
810 .playback = {
811 .stream_name = "Offload1 Playback",
812 .channels_min = 2,
813 .channels_max = 2,
814 .rates = SNDRV_PCM_RATE_8000_192000,
815 .formats = HSW_FORMATS,
816 },
817 },
818 {
819 .name = "Loopback Pin",
820 .id = HSW_PCM_DAI_ID_LOOPBACK,
821 .capture = {
822 .stream_name = "Loopback Capture",
823 .channels_min = 2,
824 .channels_max = 2,
825 .rates = SNDRV_PCM_RATE_48000,
826 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
827 },
828 },
829 };
830
831 static const struct snd_soc_dapm_widget widgets[] = {
832
833 /* Backend DAIs */
834 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
835 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
836 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
837 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
838
839 /* Global Playback Mixer */
840 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
841 };
842
843 static const struct snd_soc_dapm_route graph[] = {
844
845 /* Playback Mixer */
846 {"Playback VMixer", NULL, "System Playback"},
847 {"Playback VMixer", NULL, "Offload0 Playback"},
848 {"Playback VMixer", NULL, "Offload1 Playback"},
849
850 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
851
852 {"Analog Capture", NULL, "SSP0 CODEC IN"},
853 };
854
855 static int hsw_pcm_probe(struct snd_soc_platform *platform)
856 {
857 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
858 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
859 struct device *dma_dev, *dev;
860 int i, ret = 0;
861
862 if (!pdata)
863 return -ENODEV;
864
865 dev = platform->dev;
866 dma_dev = pdata->dma_dev;
867
868 priv_data->hsw = pdata->dsp;
869 priv_data->dev = platform->dev;
870 priv_data->pm_state = HSW_PM_STATE_D0;
871 priv_data->soc_card = platform->component.card;
872
873 /* allocate DSP buffer page tables */
874 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
875
876 mutex_init(&priv_data->pcm[i].mutex);
877
878 /* playback */
879 if (hsw_dais[i].playback.channels_min) {
880 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
881 PAGE_SIZE, &priv_data->dmab[i][0]);
882 if (ret < 0)
883 goto err;
884 }
885
886 /* capture */
887 if (hsw_dais[i].capture.channels_min) {
888 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
889 PAGE_SIZE, &priv_data->dmab[i][1]);
890 if (ret < 0)
891 goto err;
892 }
893 }
894
895 /* allocate runtime modules */
896 hsw_pcm_create_modules(priv_data);
897
898 /* enable runtime PM with auto suspend */
899 pm_runtime_set_autosuspend_delay(platform->dev,
900 SST_RUNTIME_SUSPEND_DELAY);
901 pm_runtime_use_autosuspend(platform->dev);
902 pm_runtime_enable(platform->dev);
903 pm_runtime_idle(platform->dev);
904
905 return 0;
906
907 err:
908 for (;i >= 0; i--) {
909 if (hsw_dais[i].playback.channels_min)
910 snd_dma_free_pages(&priv_data->dmab[i][0]);
911 if (hsw_dais[i].capture.channels_min)
912 snd_dma_free_pages(&priv_data->dmab[i][1]);
913 }
914 return ret;
915 }
916
917 static int hsw_pcm_remove(struct snd_soc_platform *platform)
918 {
919 struct hsw_priv_data *priv_data =
920 snd_soc_platform_get_drvdata(platform);
921 int i;
922
923 pm_runtime_disable(platform->dev);
924 hsw_pcm_free_modules(priv_data);
925
926 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
927 if (hsw_dais[i].playback.channels_min)
928 snd_dma_free_pages(&priv_data->dmab[i][0]);
929 if (hsw_dais[i].capture.channels_min)
930 snd_dma_free_pages(&priv_data->dmab[i][1]);
931 }
932
933 return 0;
934 }
935
936 static struct snd_soc_platform_driver hsw_soc_platform = {
937 .probe = hsw_pcm_probe,
938 .remove = hsw_pcm_remove,
939 .ops = &hsw_pcm_ops,
940 .pcm_new = hsw_pcm_new,
941 .pcm_free = hsw_pcm_free,
942 };
943
944 static const struct snd_soc_component_driver hsw_dai_component = {
945 .name = "haswell-dai",
946 .controls = hsw_volume_controls,
947 .num_controls = ARRAY_SIZE(hsw_volume_controls),
948 .dapm_widgets = widgets,
949 .num_dapm_widgets = ARRAY_SIZE(widgets),
950 .dapm_routes = graph,
951 .num_dapm_routes = ARRAY_SIZE(graph),
952 };
953
954 static int hsw_pcm_dev_probe(struct platform_device *pdev)
955 {
956 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
957 struct hsw_priv_data *priv_data;
958 int ret;
959
960 if (!sst_pdata)
961 return -EINVAL;
962
963 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
964 if (!priv_data)
965 return -ENOMEM;
966
967 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
968 if (ret < 0)
969 return -ENODEV;
970
971 priv_data->hsw = sst_pdata->dsp;
972 platform_set_drvdata(pdev, priv_data);
973
974 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
975 if (ret < 0)
976 goto err_plat;
977
978 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
979 hsw_dais, ARRAY_SIZE(hsw_dais));
980 if (ret < 0)
981 goto err_comp;
982
983 return 0;
984
985 err_comp:
986 snd_soc_unregister_platform(&pdev->dev);
987 err_plat:
988 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
989 return 0;
990 }
991
992 static int hsw_pcm_dev_remove(struct platform_device *pdev)
993 {
994 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
995
996 snd_soc_unregister_platform(&pdev->dev);
997 snd_soc_unregister_component(&pdev->dev);
998 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
999
1000 return 0;
1001 }
1002
1003 #ifdef CONFIG_PM_RUNTIME
1004
1005 static int hsw_pcm_runtime_idle(struct device *dev)
1006 {
1007 return 0;
1008 }
1009
1010 static int hsw_pcm_runtime_suspend(struct device *dev)
1011 {
1012 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1013 struct sst_hsw *hsw = pdata->hsw;
1014
1015 if (pdata->pm_state == HSW_PM_STATE_D3)
1016 return 0;
1017
1018 sst_hsw_dsp_runtime_suspend(hsw);
1019 sst_hsw_dsp_runtime_sleep(hsw);
1020 pdata->pm_state = HSW_PM_STATE_D3;
1021
1022 return 0;
1023 }
1024
1025 static int hsw_pcm_runtime_resume(struct device *dev)
1026 {
1027 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1028 struct sst_hsw *hsw = pdata->hsw;
1029 int ret;
1030
1031 if (pdata->pm_state == HSW_PM_STATE_D0)
1032 return 0;
1033
1034 ret = sst_hsw_dsp_load(hsw);
1035 if (ret < 0) {
1036 dev_err(dev, "failed to reload %d\n", ret);
1037 return ret;
1038 }
1039
1040 ret = hsw_pcm_create_modules(pdata);
1041 if (ret < 0) {
1042 dev_err(dev, "failed to create modules %d\n", ret);
1043 return ret;
1044 }
1045
1046 ret = sst_hsw_dsp_runtime_resume(hsw);
1047 if (ret < 0)
1048 return ret;
1049 else if (ret == 1) /* no action required */
1050 return 0;
1051
1052 pdata->pm_state = HSW_PM_STATE_D0;
1053 return ret;
1054 }
1055
1056 #else
1057 #define hsw_pcm_runtime_idle NULL
1058 #define hsw_pcm_runtime_suspend NULL
1059 #define hsw_pcm_runtime_resume NULL
1060 #endif
1061
1062 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_RUNTIME)
1063
1064 static void hsw_pcm_complete(struct device *dev)
1065 {
1066 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1067 struct sst_hsw *hsw = pdata->hsw;
1068 struct hsw_pcm_data *pcm_data;
1069 int i, err;
1070
1071 if (pdata->pm_state == HSW_PM_STATE_D0)
1072 return;
1073
1074 err = sst_hsw_dsp_load(hsw);
1075 if (err < 0) {
1076 dev_err(dev, "failed to reload %d\n", err);
1077 return;
1078 }
1079
1080 err = hsw_pcm_create_modules(pdata);
1081 if (err < 0) {
1082 dev_err(dev, "failed to create modules %d\n", err);
1083 return;
1084 }
1085
1086 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1087 pcm_data = &pdata->pcm[i];
1088
1089 if (!pcm_data->substream)
1090 continue;
1091
1092 err = sst_module_runtime_restore(pcm_data->runtime,
1093 &pcm_data->context);
1094 if (err < 0)
1095 dev_err(dev, "failed to restore context for PCM %d\n", i);
1096 }
1097
1098 snd_soc_resume(pdata->soc_card->dev);
1099
1100 err = sst_hsw_dsp_runtime_resume(hsw);
1101 if (err < 0)
1102 return;
1103 else if (err == 1) /* no action required */
1104 return;
1105
1106 pdata->pm_state = HSW_PM_STATE_D0;
1107 return;
1108 }
1109
1110 static int hsw_pcm_prepare(struct device *dev)
1111 {
1112 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1113 struct sst_hsw *hsw = pdata->hsw;
1114 struct hsw_pcm_data *pcm_data;
1115 int i, err;
1116
1117 if (pdata->pm_state == HSW_PM_STATE_D3)
1118 return 0;
1119 /* suspend all active streams */
1120 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1121 pcm_data = &pdata->pcm[i];
1122
1123 if (!pcm_data->substream)
1124 continue;
1125 dev_dbg(dev, "suspending pcm %d\n", i);
1126 snd_pcm_suspend_all(pcm_data->hsw_pcm);
1127
1128 /* We need to wait until the DSP FW stops the streams */
1129 msleep(2);
1130 }
1131
1132 snd_soc_suspend(pdata->soc_card->dev);
1133 snd_soc_poweroff(pdata->soc_card->dev);
1134
1135 /* enter D3 state and stall */
1136 sst_hsw_dsp_runtime_suspend(hsw);
1137
1138 /* preserve persistent memory */
1139 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1140 pcm_data = &pdata->pcm[i];
1141
1142 if (!pcm_data->substream)
1143 continue;
1144
1145 dev_dbg(dev, "saving context pcm %d\n", i);
1146 err = sst_module_runtime_save(pcm_data->runtime,
1147 &pcm_data->context);
1148 if (err < 0)
1149 dev_err(dev, "failed to save context for PCM %d\n", i);
1150 }
1151
1152 /* put the DSP to sleep */
1153 sst_hsw_dsp_runtime_sleep(hsw);
1154 pdata->pm_state = HSW_PM_STATE_D3;
1155
1156 return 0;
1157 }
1158
1159 #else
1160 #define hsw_pcm_prepare NULL
1161 #define hsw_pcm_complete NULL
1162 #endif
1163
1164 static const struct dev_pm_ops hsw_pcm_pm = {
1165 .runtime_idle = hsw_pcm_runtime_idle,
1166 .runtime_suspend = hsw_pcm_runtime_suspend,
1167 .runtime_resume = hsw_pcm_runtime_resume,
1168 .prepare = hsw_pcm_prepare,
1169 .complete = hsw_pcm_complete,
1170 };
1171
1172 static struct platform_driver hsw_pcm_driver = {
1173 .driver = {
1174 .name = "haswell-pcm-audio",
1175 .owner = THIS_MODULE,
1176 .pm = &hsw_pcm_pm,
1177
1178 },
1179
1180 .probe = hsw_pcm_dev_probe,
1181 .remove = hsw_pcm_dev_remove,
1182 };
1183 module_platform_driver(hsw_pcm_driver);
1184
1185 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1186 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1187 MODULE_LICENSE("GPL v2");
1188 MODULE_ALIAS("platform:haswell-pcm-audio");