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