Merge tag 'pwm/for-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[deliverable/linux.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_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
772
773 static struct snd_soc_dai_driver hsw_dais[] = {
774 {
775 .name = "System Pin",
776 .id = HSW_PCM_DAI_ID_SYSTEM,
777 .playback = {
778 .stream_name = "System Playback",
779 .channels_min = 2,
780 .channels_max = 2,
781 .rates = SNDRV_PCM_RATE_48000,
782 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
783 },
784 .capture = {
785 .stream_name = "Analog Capture",
786 .channels_min = 2,
787 .channels_max = 4,
788 .rates = SNDRV_PCM_RATE_48000,
789 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
790 },
791 },
792 {
793 /* PCM */
794 .name = "Offload0 Pin",
795 .id = HSW_PCM_DAI_ID_OFFLOAD0,
796 .playback = {
797 .stream_name = "Offload0 Playback",
798 .channels_min = 2,
799 .channels_max = 2,
800 .rates = SNDRV_PCM_RATE_8000_192000,
801 .formats = HSW_FORMATS,
802 },
803 },
804 {
805 /* PCM */
806 .name = "Offload1 Pin",
807 .id = HSW_PCM_DAI_ID_OFFLOAD1,
808 .playback = {
809 .stream_name = "Offload1 Playback",
810 .channels_min = 2,
811 .channels_max = 2,
812 .rates = SNDRV_PCM_RATE_8000_192000,
813 .formats = HSW_FORMATS,
814 },
815 },
816 {
817 .name = "Loopback Pin",
818 .id = HSW_PCM_DAI_ID_LOOPBACK,
819 .capture = {
820 .stream_name = "Loopback Capture",
821 .channels_min = 2,
822 .channels_max = 2,
823 .rates = SNDRV_PCM_RATE_48000,
824 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
825 },
826 },
827 };
828
829 static const struct snd_soc_dapm_widget widgets[] = {
830
831 /* Backend DAIs */
832 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
833 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
834 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
835 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
836
837 /* Global Playback Mixer */
838 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
839 };
840
841 static const struct snd_soc_dapm_route graph[] = {
842
843 /* Playback Mixer */
844 {"Playback VMixer", NULL, "System Playback"},
845 {"Playback VMixer", NULL, "Offload0 Playback"},
846 {"Playback VMixer", NULL, "Offload1 Playback"},
847
848 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
849
850 {"Analog Capture", NULL, "SSP0 CODEC IN"},
851 };
852
853 static int hsw_pcm_probe(struct snd_soc_platform *platform)
854 {
855 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
856 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
857 struct device *dma_dev, *dev;
858 int i, ret = 0;
859
860 if (!pdata)
861 return -ENODEV;
862
863 dev = platform->dev;
864 dma_dev = pdata->dma_dev;
865
866 priv_data->hsw = pdata->dsp;
867 priv_data->dev = platform->dev;
868 priv_data->pm_state = HSW_PM_STATE_D0;
869 priv_data->soc_card = platform->component.card;
870
871 /* allocate DSP buffer page tables */
872 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
873
874 mutex_init(&priv_data->pcm[i].mutex);
875
876 /* playback */
877 if (hsw_dais[i].playback.channels_min) {
878 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
879 PAGE_SIZE, &priv_data->dmab[i][0]);
880 if (ret < 0)
881 goto err;
882 }
883
884 /* capture */
885 if (hsw_dais[i].capture.channels_min) {
886 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
887 PAGE_SIZE, &priv_data->dmab[i][1]);
888 if (ret < 0)
889 goto err;
890 }
891 }
892
893 /* allocate runtime modules */
894 hsw_pcm_create_modules(priv_data);
895
896 /* enable runtime PM with auto suspend */
897 pm_runtime_set_autosuspend_delay(platform->dev,
898 SST_RUNTIME_SUSPEND_DELAY);
899 pm_runtime_use_autosuspend(platform->dev);
900 pm_runtime_enable(platform->dev);
901 pm_runtime_idle(platform->dev);
902
903 return 0;
904
905 err:
906 for (;i >= 0; i--) {
907 if (hsw_dais[i].playback.channels_min)
908 snd_dma_free_pages(&priv_data->dmab[i][0]);
909 if (hsw_dais[i].capture.channels_min)
910 snd_dma_free_pages(&priv_data->dmab[i][1]);
911 }
912 return ret;
913 }
914
915 static int hsw_pcm_remove(struct snd_soc_platform *platform)
916 {
917 struct hsw_priv_data *priv_data =
918 snd_soc_platform_get_drvdata(platform);
919 int i;
920
921 pm_runtime_disable(platform->dev);
922 hsw_pcm_free_modules(priv_data);
923
924 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
925 if (hsw_dais[i].playback.channels_min)
926 snd_dma_free_pages(&priv_data->dmab[i][0]);
927 if (hsw_dais[i].capture.channels_min)
928 snd_dma_free_pages(&priv_data->dmab[i][1]);
929 }
930
931 return 0;
932 }
933
934 static struct snd_soc_platform_driver hsw_soc_platform = {
935 .probe = hsw_pcm_probe,
936 .remove = hsw_pcm_remove,
937 .ops = &hsw_pcm_ops,
938 .pcm_new = hsw_pcm_new,
939 .pcm_free = hsw_pcm_free,
940 };
941
942 static const struct snd_soc_component_driver hsw_dai_component = {
943 .name = "haswell-dai",
944 .controls = hsw_volume_controls,
945 .num_controls = ARRAY_SIZE(hsw_volume_controls),
946 .dapm_widgets = widgets,
947 .num_dapm_widgets = ARRAY_SIZE(widgets),
948 .dapm_routes = graph,
949 .num_dapm_routes = ARRAY_SIZE(graph),
950 };
951
952 static int hsw_pcm_dev_probe(struct platform_device *pdev)
953 {
954 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
955 struct hsw_priv_data *priv_data;
956 int ret;
957
958 if (!sst_pdata)
959 return -EINVAL;
960
961 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
962 if (!priv_data)
963 return -ENOMEM;
964
965 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
966 if (ret < 0)
967 return -ENODEV;
968
969 priv_data->hsw = sst_pdata->dsp;
970 platform_set_drvdata(pdev, priv_data);
971
972 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
973 if (ret < 0)
974 goto err_plat;
975
976 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
977 hsw_dais, ARRAY_SIZE(hsw_dais));
978 if (ret < 0)
979 goto err_comp;
980
981 return 0;
982
983 err_comp:
984 snd_soc_unregister_platform(&pdev->dev);
985 err_plat:
986 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
987 return 0;
988 }
989
990 static int hsw_pcm_dev_remove(struct platform_device *pdev)
991 {
992 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
993
994 snd_soc_unregister_platform(&pdev->dev);
995 snd_soc_unregister_component(&pdev->dev);
996 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
997
998 return 0;
999 }
1000
1001 #ifdef CONFIG_PM_RUNTIME
1002
1003 static int hsw_pcm_runtime_idle(struct device *dev)
1004 {
1005 return 0;
1006 }
1007
1008 static int hsw_pcm_runtime_suspend(struct device *dev)
1009 {
1010 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1011 struct sst_hsw *hsw = pdata->hsw;
1012
1013 if (pdata->pm_state == HSW_PM_STATE_D3)
1014 return 0;
1015
1016 sst_hsw_dsp_runtime_suspend(hsw);
1017 sst_hsw_dsp_runtime_sleep(hsw);
1018 pdata->pm_state = HSW_PM_STATE_D3;
1019
1020 return 0;
1021 }
1022
1023 static int hsw_pcm_runtime_resume(struct device *dev)
1024 {
1025 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1026 struct sst_hsw *hsw = pdata->hsw;
1027 int ret;
1028
1029 if (pdata->pm_state == HSW_PM_STATE_D0)
1030 return 0;
1031
1032 ret = sst_hsw_dsp_load(hsw);
1033 if (ret < 0) {
1034 dev_err(dev, "failed to reload %d\n", ret);
1035 return ret;
1036 }
1037
1038 ret = hsw_pcm_create_modules(pdata);
1039 if (ret < 0) {
1040 dev_err(dev, "failed to create modules %d\n", ret);
1041 return ret;
1042 }
1043
1044 ret = sst_hsw_dsp_runtime_resume(hsw);
1045 if (ret < 0)
1046 return ret;
1047 else if (ret == 1) /* no action required */
1048 return 0;
1049
1050 pdata->pm_state = HSW_PM_STATE_D0;
1051 return ret;
1052 }
1053
1054 #else
1055 #define hsw_pcm_runtime_idle NULL
1056 #define hsw_pcm_runtime_suspend NULL
1057 #define hsw_pcm_runtime_resume NULL
1058 #endif
1059
1060 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_RUNTIME)
1061
1062 static void hsw_pcm_complete(struct device *dev)
1063 {
1064 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1065 struct sst_hsw *hsw = pdata->hsw;
1066 struct hsw_pcm_data *pcm_data;
1067 int i, err;
1068
1069 if (pdata->pm_state == HSW_PM_STATE_D0)
1070 return;
1071
1072 err = sst_hsw_dsp_load(hsw);
1073 if (err < 0) {
1074 dev_err(dev, "failed to reload %d\n", err);
1075 return;
1076 }
1077
1078 err = hsw_pcm_create_modules(pdata);
1079 if (err < 0) {
1080 dev_err(dev, "failed to create modules %d\n", err);
1081 return;
1082 }
1083
1084 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1085 pcm_data = &pdata->pcm[i];
1086
1087 if (!pcm_data->substream)
1088 continue;
1089
1090 err = sst_module_runtime_restore(pcm_data->runtime,
1091 &pcm_data->context);
1092 if (err < 0)
1093 dev_err(dev, "failed to restore context for PCM %d\n", i);
1094 }
1095
1096 snd_soc_resume(pdata->soc_card->dev);
1097
1098 err = sst_hsw_dsp_runtime_resume(hsw);
1099 if (err < 0)
1100 return;
1101 else if (err == 1) /* no action required */
1102 return;
1103
1104 pdata->pm_state = HSW_PM_STATE_D0;
1105 return;
1106 }
1107
1108 static int hsw_pcm_prepare(struct device *dev)
1109 {
1110 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1111 struct sst_hsw *hsw = pdata->hsw;
1112 struct hsw_pcm_data *pcm_data;
1113 int i, err;
1114
1115 if (pdata->pm_state == HSW_PM_STATE_D3)
1116 return 0;
1117 /* suspend all active streams */
1118 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1119 pcm_data = &pdata->pcm[i];
1120
1121 if (!pcm_data->substream)
1122 continue;
1123 dev_dbg(dev, "suspending pcm %d\n", i);
1124 snd_pcm_suspend_all(pcm_data->hsw_pcm);
1125
1126 /* We need to wait until the DSP FW stops the streams */
1127 msleep(2);
1128 }
1129
1130 snd_soc_suspend(pdata->soc_card->dev);
1131 snd_soc_poweroff(pdata->soc_card->dev);
1132
1133 /* enter D3 state and stall */
1134 sst_hsw_dsp_runtime_suspend(hsw);
1135
1136 /* preserve persistent memory */
1137 for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) {
1138 pcm_data = &pdata->pcm[i];
1139
1140 if (!pcm_data->substream)
1141 continue;
1142
1143 dev_dbg(dev, "saving context pcm %d\n", i);
1144 err = sst_module_runtime_save(pcm_data->runtime,
1145 &pcm_data->context);
1146 if (err < 0)
1147 dev_err(dev, "failed to save context for PCM %d\n", i);
1148 }
1149
1150 /* put the DSP to sleep */
1151 sst_hsw_dsp_runtime_sleep(hsw);
1152 pdata->pm_state = HSW_PM_STATE_D3;
1153
1154 return 0;
1155 }
1156
1157 #else
1158 #define hsw_pcm_prepare NULL
1159 #define hsw_pcm_complete NULL
1160 #endif
1161
1162 static const struct dev_pm_ops hsw_pcm_pm = {
1163 .runtime_idle = hsw_pcm_runtime_idle,
1164 .runtime_suspend = hsw_pcm_runtime_suspend,
1165 .runtime_resume = hsw_pcm_runtime_resume,
1166 .prepare = hsw_pcm_prepare,
1167 .complete = hsw_pcm_complete,
1168 };
1169
1170 static struct platform_driver hsw_pcm_driver = {
1171 .driver = {
1172 .name = "haswell-pcm-audio",
1173 .pm = &hsw_pcm_pm,
1174 },
1175
1176 .probe = hsw_pcm_dev_probe,
1177 .remove = hsw_pcm_dev_remove,
1178 };
1179 module_platform_driver(hsw_pcm_driver);
1180
1181 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1182 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1183 MODULE_LICENSE("GPL v2");
1184 MODULE_ALIAS("platform:haswell-pcm-audio");
This page took 0.197813 seconds and 5 git commands to generate.