Merge git://git.kvack.org/~bcrl/aio-next
[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 <asm/page.h>
22 #include <asm/pgtable.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/dmaengine_pcm.h>
27 #include <sound/soc.h>
28 #include <sound/tlv.h>
29 #include <sound/compress_driver.h>
30
31 #include "sst-haswell-ipc.h"
32 #include "sst-dsp-priv.h"
33 #include "sst-dsp.h"
34
35 #define HSW_PCM_COUNT 6
36 #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
37
38 /* simple volume table */
39 static const u32 volume_map[] = {
40 HSW_VOLUME_MAX >> 30,
41 HSW_VOLUME_MAX >> 29,
42 HSW_VOLUME_MAX >> 28,
43 HSW_VOLUME_MAX >> 27,
44 HSW_VOLUME_MAX >> 26,
45 HSW_VOLUME_MAX >> 25,
46 HSW_VOLUME_MAX >> 24,
47 HSW_VOLUME_MAX >> 23,
48 HSW_VOLUME_MAX >> 22,
49 HSW_VOLUME_MAX >> 21,
50 HSW_VOLUME_MAX >> 20,
51 HSW_VOLUME_MAX >> 19,
52 HSW_VOLUME_MAX >> 18,
53 HSW_VOLUME_MAX >> 17,
54 HSW_VOLUME_MAX >> 16,
55 HSW_VOLUME_MAX >> 15,
56 HSW_VOLUME_MAX >> 14,
57 HSW_VOLUME_MAX >> 13,
58 HSW_VOLUME_MAX >> 12,
59 HSW_VOLUME_MAX >> 11,
60 HSW_VOLUME_MAX >> 10,
61 HSW_VOLUME_MAX >> 9,
62 HSW_VOLUME_MAX >> 8,
63 HSW_VOLUME_MAX >> 7,
64 HSW_VOLUME_MAX >> 6,
65 HSW_VOLUME_MAX >> 5,
66 HSW_VOLUME_MAX >> 4,
67 HSW_VOLUME_MAX >> 3,
68 HSW_VOLUME_MAX >> 2,
69 HSW_VOLUME_MAX >> 1,
70 HSW_VOLUME_MAX >> 0,
71 };
72
73 #define HSW_PCM_PERIODS_MAX 64
74 #define HSW_PCM_PERIODS_MIN 2
75
76 static const struct snd_pcm_hardware hsw_pcm_hardware = {
77 .info = SNDRV_PCM_INFO_MMAP |
78 SNDRV_PCM_INFO_MMAP_VALID |
79 SNDRV_PCM_INFO_INTERLEAVED |
80 SNDRV_PCM_INFO_PAUSE |
81 SNDRV_PCM_INFO_RESUME |
82 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
83 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
84 SNDRV_PCM_FMTBIT_S32_LE,
85 .period_bytes_min = PAGE_SIZE,
86 .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
87 .periods_min = HSW_PCM_PERIODS_MIN,
88 .periods_max = HSW_PCM_PERIODS_MAX,
89 .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
90 };
91
92 /* private data for each PCM DSP stream */
93 struct hsw_pcm_data {
94 int dai_id;
95 struct sst_hsw_stream *stream;
96 u32 volume[2];
97 struct snd_pcm_substream *substream;
98 struct snd_compr_stream *cstream;
99 unsigned int wpos;
100 struct mutex mutex;
101 bool allocated;
102 };
103
104 /* private data for the driver */
105 struct hsw_priv_data {
106 /* runtime DSP */
107 struct sst_hsw *hsw;
108
109 /* page tables */
110 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
111
112 /* DAI data */
113 struct hsw_pcm_data pcm[HSW_PCM_COUNT];
114 };
115
116 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
117
118 static inline u32 hsw_mixer_to_ipc(unsigned int value)
119 {
120 if (value >= ARRAY_SIZE(volume_map))
121 return volume_map[0];
122 else
123 return volume_map[value];
124 }
125
126 static inline unsigned int hsw_ipc_to_mixer(u32 value)
127 {
128 int i;
129
130 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
131 if (volume_map[i] >= value)
132 return i;
133 }
134
135 return i - 1;
136 }
137
138 static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
139 struct snd_ctl_elem_value *ucontrol)
140 {
141 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
142 struct soc_mixer_control *mc =
143 (struct soc_mixer_control *)kcontrol->private_value;
144 struct hsw_priv_data *pdata =
145 snd_soc_platform_get_drvdata(platform);
146 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
147 struct sst_hsw *hsw = pdata->hsw;
148 u32 volume;
149
150 mutex_lock(&pcm_data->mutex);
151
152 if (!pcm_data->stream) {
153 pcm_data->volume[0] =
154 hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
155 pcm_data->volume[1] =
156 hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
157 mutex_unlock(&pcm_data->mutex);
158 return 0;
159 }
160
161 if (ucontrol->value.integer.value[0] ==
162 ucontrol->value.integer.value[1]) {
163 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
164 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 2, volume);
165 } else {
166 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
167 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
168 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
169 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
170 }
171
172 mutex_unlock(&pcm_data->mutex);
173 return 0;
174 }
175
176 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
177 struct snd_ctl_elem_value *ucontrol)
178 {
179 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
180 struct soc_mixer_control *mc =
181 (struct soc_mixer_control *)kcontrol->private_value;
182 struct hsw_priv_data *pdata =
183 snd_soc_platform_get_drvdata(platform);
184 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
185 struct sst_hsw *hsw = pdata->hsw;
186 u32 volume;
187
188 mutex_lock(&pcm_data->mutex);
189
190 if (!pcm_data->stream) {
191 ucontrol->value.integer.value[0] =
192 hsw_ipc_to_mixer(pcm_data->volume[0]);
193 ucontrol->value.integer.value[1] =
194 hsw_ipc_to_mixer(pcm_data->volume[1]);
195 mutex_unlock(&pcm_data->mutex);
196 return 0;
197 }
198
199 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
200 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
201 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
202 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
203 mutex_unlock(&pcm_data->mutex);
204
205 return 0;
206 }
207
208 static int hsw_volume_put(struct snd_kcontrol *kcontrol,
209 struct snd_ctl_elem_value *ucontrol)
210 {
211 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
212 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
213 struct sst_hsw *hsw = pdata->hsw;
214 u32 volume;
215
216 if (ucontrol->value.integer.value[0] ==
217 ucontrol->value.integer.value[1]) {
218
219 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
220 sst_hsw_mixer_set_volume(hsw, 0, 2, volume);
221
222 } else {
223 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
224 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
225
226 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
227 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
228 }
229
230 return 0;
231 }
232
233 static int hsw_volume_get(struct snd_kcontrol *kcontrol,
234 struct snd_ctl_elem_value *ucontrol)
235 {
236 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
237 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
238 struct sst_hsw *hsw = pdata->hsw;
239 unsigned int volume = 0;
240
241 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
242 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
243
244 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
245 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
246
247 return 0;
248 }
249
250 /* TLV used by both global and stream volumes */
251 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
252
253 /* System Pin has no volume control */
254 static const struct snd_kcontrol_new hsw_volume_controls[] = {
255 /* Global DSP volume */
256 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
257 ARRAY_SIZE(volume_map) -1, 0,
258 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
259 /* Offload 0 volume */
260 SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
261 ARRAY_SIZE(volume_map), 0,
262 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
263 /* Offload 1 volume */
264 SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
265 ARRAY_SIZE(volume_map), 0,
266 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
267 /* Loopback volume */
268 SOC_DOUBLE_EXT_TLV("Loopback Capture Volume", 3, 0, 8,
269 ARRAY_SIZE(volume_map), 0,
270 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
271 /* Mic Capture volume */
272 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
273 ARRAY_SIZE(volume_map), 0,
274 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
275 };
276
277 /* Create DMA buffer page table for DSP */
278 static int create_adsp_page_table(struct snd_pcm_substream *substream,
279 struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
280 unsigned char *dma_area, size_t size, int pcm)
281 {
282 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
283 int i, pages, stream = substream->stream;
284
285 pages = snd_sgbuf_aligned_pages(size);
286
287 dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
288 dma_area, size, pages);
289
290 for (i = 0; i < pages; i++) {
291 u32 idx = (((i << 2) + i)) >> 1;
292 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
293 u32 *pg_table;
294
295 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
296
297 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
298
299 if (i & 1)
300 *pg_table |= (pfn << 4);
301 else
302 *pg_table |= pfn;
303 }
304
305 return 0;
306 }
307
308 /* this may get called several times by oss emulation */
309 static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
310 struct snd_pcm_hw_params *params)
311 {
312 struct snd_soc_pcm_runtime *rtd = substream->private_data;
313 struct snd_pcm_runtime *runtime = substream->runtime;
314 struct hsw_priv_data *pdata =
315 snd_soc_platform_get_drvdata(rtd->platform);
316 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
317 struct sst_hsw *hsw = pdata->hsw;
318 struct sst_module *module_data;
319 struct sst_dsp *dsp;
320 struct snd_dma_buffer *dmab;
321 enum sst_hsw_stream_type stream_type;
322 enum sst_hsw_stream_path_id path_id;
323 u32 rate, bits, map, pages, module_id;
324 u8 channels;
325 int ret;
326
327 /* check if we are being called a subsequent time */
328 if (pcm_data->allocated) {
329 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
330 if (ret < 0)
331 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
332 ret);
333
334 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
335 if (ret < 0) {
336 dev_dbg(rtd->dev, "error: free stream failed %d\n",
337 ret);
338 return ret;
339 }
340 pcm_data->allocated = false;
341
342 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
343 hsw_notify_pointer, pcm_data);
344 if (pcm_data->stream == NULL) {
345 dev_err(rtd->dev, "error: failed to create stream\n");
346 return -EINVAL;
347 }
348 }
349
350 /* stream direction */
351 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
352 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
353 else
354 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
355
356 /* DSP stream type depends on DAI ID */
357 switch (rtd->cpu_dai->id) {
358 case 0:
359 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
360 module_id = SST_HSW_MODULE_PCM_SYSTEM;
361 break;
362 case 1:
363 case 2:
364 stream_type = SST_HSW_STREAM_TYPE_RENDER;
365 module_id = SST_HSW_MODULE_PCM;
366 break;
367 case 3:
368 /* path ID needs to be OUT for loopback */
369 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
370 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
371 module_id = SST_HSW_MODULE_PCM_REFERENCE;
372 break;
373 case 4:
374 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
375 module_id = SST_HSW_MODULE_PCM_CAPTURE;
376 break;
377 default:
378 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
379 rtd->cpu_dai->id);
380 return -EINVAL;
381 };
382
383 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
384 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
385 if (ret < 0) {
386 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
387 return ret;
388 }
389
390 rate = params_rate(params);
391 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
392 if (ret < 0) {
393 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
394 return ret;
395 }
396
397 switch (params_format(params)) {
398 case SNDRV_PCM_FORMAT_S16_LE:
399 bits = SST_HSW_DEPTH_16BIT;
400 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
401 break;
402 case SNDRV_PCM_FORMAT_S24_LE:
403 bits = SST_HSW_DEPTH_32BIT;
404 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
405 break;
406 case SNDRV_PCM_FORMAT_S8:
407 bits = SST_HSW_DEPTH_8BIT;
408 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
409 break;
410 case SNDRV_PCM_FORMAT_S32_LE:
411 bits = SST_HSW_DEPTH_32BIT;
412 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
413 break;
414 default:
415 dev_err(rtd->dev, "error: invalid format %d\n",
416 params_format(params));
417 return -EINVAL;
418 }
419
420 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
421 if (ret < 0) {
422 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
423 return ret;
424 }
425
426 /* we only support stereo atm */
427 channels = params_channels(params);
428 if (channels != 2) {
429 dev_err(rtd->dev, "error: invalid channels %d\n", channels);
430 return -EINVAL;
431 }
432
433 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
434 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
435 map, SST_HSW_CHANNEL_CONFIG_STEREO);
436
437 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
438 if (ret < 0) {
439 dev_err(rtd->dev, "error: could not set channels %d\n",
440 channels);
441 return ret;
442 }
443
444 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
445 if (ret < 0) {
446 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
447 params_buffer_bytes(params), ret);
448 return ret;
449 }
450
451 dmab = snd_pcm_get_dma_buf(substream);
452
453 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
454 runtime->dma_bytes, rtd->cpu_dai->id);
455 if (ret < 0)
456 return ret;
457
458 sst_hsw_stream_set_style(hsw, pcm_data->stream,
459 SST_HSW_INTERLEAVING_PER_CHANNEL);
460
461 if (runtime->dma_bytes % PAGE_SIZE)
462 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
463 else
464 pages = runtime->dma_bytes / PAGE_SIZE;
465
466 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
467 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
468 pages, runtime->dma_bytes, 0,
469 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
470 if (ret < 0) {
471 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
472 return ret;
473 }
474
475 dsp = sst_hsw_get_dsp(hsw);
476
477 module_data = sst_module_get_from_id(dsp, module_id);
478 if (module_data == NULL) {
479 dev_err(rtd->dev, "error: failed to get module config\n");
480 return -EINVAL;
481 }
482
483 /* we use hardcoded memory offsets atm, will be updated for new FW */
484 if (stream_type == SST_HSW_STREAM_TYPE_CAPTURE) {
485 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
486 SST_HSW_MODULE_PCM_CAPTURE, module_data->entry);
487 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
488 0x449400, 0x4000);
489 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
490 0x400000, 0);
491 } else { /* stream_type == SST_HSW_STREAM_TYPE_SYSTEM */
492 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
493 SST_HSW_MODULE_PCM_SYSTEM, module_data->entry);
494
495 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
496 module_data->offset, module_data->size);
497 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
498 0x44d400, 0x3800);
499
500 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
501 module_data->offset, module_data->size);
502 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
503 0x400000, 0);
504 }
505
506 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
507 if (ret < 0) {
508 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
509 return ret;
510 }
511 pcm_data->allocated = true;
512
513 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
514 if (ret < 0)
515 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
516
517 return 0;
518 }
519
520 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
521 {
522 snd_pcm_lib_free_pages(substream);
523 return 0;
524 }
525
526 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
527 {
528 struct snd_soc_pcm_runtime *rtd = substream->private_data;
529 struct hsw_priv_data *pdata =
530 snd_soc_platform_get_drvdata(rtd->platform);
531 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
532 struct sst_hsw *hsw = pdata->hsw;
533
534 switch (cmd) {
535 case SNDRV_PCM_TRIGGER_START:
536 case SNDRV_PCM_TRIGGER_RESUME:
537 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
538 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
539 break;
540 case SNDRV_PCM_TRIGGER_STOP:
541 case SNDRV_PCM_TRIGGER_SUSPEND:
542 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
543 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
544 break;
545 default:
546 break;
547 }
548
549 return 0;
550 }
551
552 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
553 {
554 struct hsw_pcm_data *pcm_data = data;
555 struct snd_pcm_substream *substream = pcm_data->substream;
556 struct snd_pcm_runtime *runtime = substream->runtime;
557 struct snd_soc_pcm_runtime *rtd = substream->private_data;
558 u32 pos;
559
560 pos = frames_to_bytes(runtime,
561 (runtime->control->appl_ptr % runtime->buffer_size));
562
563 dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
564
565 /* let alsa know we have play a period */
566 snd_pcm_period_elapsed(substream);
567 return pos;
568 }
569
570 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
571 {
572 struct snd_soc_pcm_runtime *rtd = substream->private_data;
573 struct snd_pcm_runtime *runtime = substream->runtime;
574 struct hsw_priv_data *pdata =
575 snd_soc_platform_get_drvdata(rtd->platform);
576 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
577 struct sst_hsw *hsw = pdata->hsw;
578 snd_pcm_uframes_t offset;
579 uint64_t ppos;
580 u32 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
581
582 offset = bytes_to_frames(runtime, position);
583 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
584
585 dev_dbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
586 position, ppos);
587 return offset;
588 }
589
590 static int hsw_pcm_open(struct snd_pcm_substream *substream)
591 {
592 struct snd_soc_pcm_runtime *rtd = substream->private_data;
593 struct hsw_priv_data *pdata =
594 snd_soc_platform_get_drvdata(rtd->platform);
595 struct hsw_pcm_data *pcm_data;
596 struct sst_hsw *hsw = pdata->hsw;
597
598 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
599
600 mutex_lock(&pcm_data->mutex);
601
602 snd_soc_pcm_set_drvdata(rtd, pcm_data);
603 pcm_data->substream = substream;
604
605 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
606
607 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
608 hsw_notify_pointer, pcm_data);
609 if (pcm_data->stream == NULL) {
610 dev_err(rtd->dev, "error: failed to create stream\n");
611 mutex_unlock(&pcm_data->mutex);
612 return -EINVAL;
613 }
614
615 /* Set previous saved volume */
616 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
617 0, pcm_data->volume[0]);
618 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
619 1, pcm_data->volume[1]);
620
621 mutex_unlock(&pcm_data->mutex);
622 return 0;
623 }
624
625 static int hsw_pcm_close(struct snd_pcm_substream *substream)
626 {
627 struct snd_soc_pcm_runtime *rtd = substream->private_data;
628 struct hsw_priv_data *pdata =
629 snd_soc_platform_get_drvdata(rtd->platform);
630 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
631 struct sst_hsw *hsw = pdata->hsw;
632 int ret;
633
634 mutex_lock(&pcm_data->mutex);
635 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
636 if (ret < 0) {
637 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
638 goto out;
639 }
640
641 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
642 if (ret < 0) {
643 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
644 goto out;
645 }
646 pcm_data->allocated = 0;
647 pcm_data->stream = NULL;
648
649 out:
650 mutex_unlock(&pcm_data->mutex);
651 return ret;
652 }
653
654 static struct snd_pcm_ops hsw_pcm_ops = {
655 .open = hsw_pcm_open,
656 .close = hsw_pcm_close,
657 .ioctl = snd_pcm_lib_ioctl,
658 .hw_params = hsw_pcm_hw_params,
659 .hw_free = hsw_pcm_hw_free,
660 .trigger = hsw_pcm_trigger,
661 .pointer = hsw_pcm_pointer,
662 .page = snd_pcm_sgbuf_ops_page,
663 };
664
665 static void hsw_pcm_free(struct snd_pcm *pcm)
666 {
667 snd_pcm_lib_preallocate_free_for_all(pcm);
668 }
669
670 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
671 {
672 struct snd_pcm *pcm = rtd->pcm;
673 struct snd_soc_platform *platform = rtd->platform;
674 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
675 struct device *dev = pdata->dma_dev;
676 int ret = 0;
677
678 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
679 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
680 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
681 SNDRV_DMA_TYPE_DEV_SG,
682 dev,
683 hsw_pcm_hardware.buffer_bytes_max,
684 hsw_pcm_hardware.buffer_bytes_max);
685 if (ret) {
686 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
687 ret);
688 return ret;
689 }
690 }
691
692 return ret;
693 }
694
695 #define HSW_FORMATS \
696 (SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S24_LE | \
697 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE |\
698 SNDRV_PCM_FMTBIT_S8)
699
700 static struct snd_soc_dai_driver hsw_dais[] = {
701 {
702 .name = "System Pin",
703 .playback = {
704 .stream_name = "System Playback",
705 .channels_min = 2,
706 .channels_max = 2,
707 .rates = SNDRV_PCM_RATE_48000,
708 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
709 },
710 },
711 {
712 /* PCM */
713 .name = "Offload0 Pin",
714 .playback = {
715 .stream_name = "Offload0 Playback",
716 .channels_min = 2,
717 .channels_max = 2,
718 .rates = SNDRV_PCM_RATE_8000_192000,
719 .formats = HSW_FORMATS,
720 },
721 },
722 {
723 /* PCM */
724 .name = "Offload1 Pin",
725 .playback = {
726 .stream_name = "Offload1 Playback",
727 .channels_min = 2,
728 .channels_max = 2,
729 .rates = SNDRV_PCM_RATE_8000_192000,
730 .formats = HSW_FORMATS,
731 },
732 },
733 {
734 .name = "Loopback Pin",
735 .capture = {
736 .stream_name = "Loopback Capture",
737 .channels_min = 2,
738 .channels_max = 2,
739 .rates = SNDRV_PCM_RATE_48000,
740 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
741 },
742 },
743 {
744 .name = "Capture Pin",
745 .capture = {
746 .stream_name = "Analog Capture",
747 .channels_min = 2,
748 .channels_max = 2,
749 .rates = SNDRV_PCM_RATE_48000,
750 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
751 },
752 },
753 };
754
755 static const struct snd_soc_dapm_widget widgets[] = {
756
757 /* Backend DAIs */
758 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
759 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
760 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
761 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
762
763 /* Global Playback Mixer */
764 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
765 };
766
767 static const struct snd_soc_dapm_route graph[] = {
768
769 /* Playback Mixer */
770 {"Playback VMixer", NULL, "System Playback"},
771 {"Playback VMixer", NULL, "Offload0 Playback"},
772 {"Playback VMixer", NULL, "Offload1 Playback"},
773
774 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
775
776 {"Analog Capture", NULL, "SSP0 CODEC IN"},
777 };
778
779 static int hsw_pcm_probe(struct snd_soc_platform *platform)
780 {
781 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
782 struct hsw_priv_data *priv_data;
783 struct device *dma_dev;
784 int i, ret = 0;
785
786 if (!pdata)
787 return -ENODEV;
788
789 dma_dev = pdata->dma_dev;
790
791 priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data), GFP_KERNEL);
792 priv_data->hsw = pdata->dsp;
793 snd_soc_platform_set_drvdata(platform, priv_data);
794
795 /* allocate DSP buffer page tables */
796 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
797
798 mutex_init(&priv_data->pcm[i].mutex);
799
800 /* playback */
801 if (hsw_dais[i].playback.channels_min) {
802 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
803 PAGE_SIZE, &priv_data->dmab[i][0]);
804 if (ret < 0)
805 goto err;
806 }
807
808 /* capture */
809 if (hsw_dais[i].capture.channels_min) {
810 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
811 PAGE_SIZE, &priv_data->dmab[i][1]);
812 if (ret < 0)
813 goto err;
814 }
815 }
816
817 return 0;
818
819 err:
820 for (;i >= 0; i--) {
821 if (hsw_dais[i].playback.channels_min)
822 snd_dma_free_pages(&priv_data->dmab[i][0]);
823 if (hsw_dais[i].capture.channels_min)
824 snd_dma_free_pages(&priv_data->dmab[i][1]);
825 }
826 return ret;
827 }
828
829 static int hsw_pcm_remove(struct snd_soc_platform *platform)
830 {
831 struct hsw_priv_data *priv_data =
832 snd_soc_platform_get_drvdata(platform);
833 int i;
834
835 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
836 if (hsw_dais[i].playback.channels_min)
837 snd_dma_free_pages(&priv_data->dmab[i][0]);
838 if (hsw_dais[i].capture.channels_min)
839 snd_dma_free_pages(&priv_data->dmab[i][1]);
840 }
841
842 return 0;
843 }
844
845 static struct snd_soc_platform_driver hsw_soc_platform = {
846 .probe = hsw_pcm_probe,
847 .remove = hsw_pcm_remove,
848 .ops = &hsw_pcm_ops,
849 .pcm_new = hsw_pcm_new,
850 .pcm_free = hsw_pcm_free,
851 .controls = hsw_volume_controls,
852 .num_controls = ARRAY_SIZE(hsw_volume_controls),
853 .dapm_widgets = widgets,
854 .num_dapm_widgets = ARRAY_SIZE(widgets),
855 .dapm_routes = graph,
856 .num_dapm_routes = ARRAY_SIZE(graph),
857 };
858
859 static const struct snd_soc_component_driver hsw_dai_component = {
860 .name = "haswell-dai",
861 };
862
863 static int hsw_pcm_dev_probe(struct platform_device *pdev)
864 {
865 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
866 int ret;
867
868 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
869 if (ret < 0)
870 return -ENODEV;
871
872 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
873 if (ret < 0)
874 goto err_plat;
875
876 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
877 hsw_dais, ARRAY_SIZE(hsw_dais));
878 if (ret < 0)
879 goto err_comp;
880
881 return 0;
882
883 err_comp:
884 snd_soc_unregister_platform(&pdev->dev);
885 err_plat:
886 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
887 return 0;
888 }
889
890 static int hsw_pcm_dev_remove(struct platform_device *pdev)
891 {
892 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
893
894 snd_soc_unregister_platform(&pdev->dev);
895 snd_soc_unregister_component(&pdev->dev);
896 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
897
898 return 0;
899 }
900
901 static struct platform_driver hsw_pcm_driver = {
902 .driver = {
903 .name = "haswell-pcm-audio",
904 .owner = THIS_MODULE,
905 },
906
907 .probe = hsw_pcm_dev_probe,
908 .remove = hsw_pcm_dev_remove,
909 };
910 module_platform_driver(hsw_pcm_driver);
911
912 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
913 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
914 MODULE_LICENSE("GPL v2");
915 MODULE_ALIAS("platform:haswell-pcm-audio");
This page took 0.058262 seconds and 5 git commands to generate.