Merge tag 'master-2014-11-25' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[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_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
142 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(cmpnt);
143 struct soc_mixer_control *mc =
144 (struct soc_mixer_control *)kcontrol->private_value;
145 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
146 struct sst_hsw *hsw = pdata->hsw;
147 u32 volume;
148
149 mutex_lock(&pcm_data->mutex);
150
151 if (!pcm_data->stream) {
152 pcm_data->volume[0] =
153 hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
154 pcm_data->volume[1] =
155 hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
156 mutex_unlock(&pcm_data->mutex);
157 return 0;
158 }
159
160 if (ucontrol->value.integer.value[0] ==
161 ucontrol->value.integer.value[1]) {
162 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
163 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 2, volume);
164 } else {
165 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
166 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
167 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
168 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
169 }
170
171 mutex_unlock(&pcm_data->mutex);
172 return 0;
173 }
174
175 static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
176 struct snd_ctl_elem_value *ucontrol)
177 {
178 struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
179 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(cmpnt);
180 struct soc_mixer_control *mc =
181 (struct soc_mixer_control *)kcontrol->private_value;
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_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
210 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(cmpnt);
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_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
235 struct hsw_priv_data *pdata = snd_soc_component_get_drvdata(cmpnt);
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 /* check if we are being called a subsequent time */
326 if (pcm_data->allocated) {
327 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
328 if (ret < 0)
329 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
330 ret);
331
332 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
333 if (ret < 0) {
334 dev_dbg(rtd->dev, "error: free stream failed %d\n",
335 ret);
336 return ret;
337 }
338 pcm_data->allocated = false;
339
340 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
341 hsw_notify_pointer, pcm_data);
342 if (pcm_data->stream == NULL) {
343 dev_err(rtd->dev, "error: failed to create stream\n");
344 return -EINVAL;
345 }
346 }
347
348 /* stream direction */
349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
350 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
351 else
352 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
353
354 /* DSP stream type depends on DAI ID */
355 switch (rtd->cpu_dai->id) {
356 case 0:
357 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
358 module_id = SST_HSW_MODULE_PCM_SYSTEM;
359 break;
360 case 1:
361 case 2:
362 stream_type = SST_HSW_STREAM_TYPE_RENDER;
363 module_id = SST_HSW_MODULE_PCM;
364 break;
365 case 3:
366 /* path ID needs to be OUT for loopback */
367 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
368 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
369 module_id = SST_HSW_MODULE_PCM_REFERENCE;
370 break;
371 case 4:
372 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
373 module_id = SST_HSW_MODULE_PCM_CAPTURE;
374 break;
375 default:
376 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
377 rtd->cpu_dai->id);
378 return -EINVAL;
379 };
380
381 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
382 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
383 if (ret < 0) {
384 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
385 return ret;
386 }
387
388 rate = params_rate(params);
389 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
390 if (ret < 0) {
391 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
392 return ret;
393 }
394
395 switch (params_format(params)) {
396 case SNDRV_PCM_FORMAT_S16_LE:
397 bits = SST_HSW_DEPTH_16BIT;
398 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
399 break;
400 case SNDRV_PCM_FORMAT_S24_LE:
401 bits = SST_HSW_DEPTH_32BIT;
402 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
403 break;
404 case SNDRV_PCM_FORMAT_S8:
405 bits = SST_HSW_DEPTH_8BIT;
406 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
407 break;
408 case SNDRV_PCM_FORMAT_S32_LE:
409 bits = SST_HSW_DEPTH_32BIT;
410 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
411 break;
412 default:
413 dev_err(rtd->dev, "error: invalid format %d\n",
414 params_format(params));
415 return -EINVAL;
416 }
417
418 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
419 if (ret < 0) {
420 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
421 return ret;
422 }
423
424 /* we only support stereo atm */
425 channels = params_channels(params);
426 if (channels != 2) {
427 dev_err(rtd->dev, "error: invalid channels %d\n", channels);
428 return -EINVAL;
429 }
430
431 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
432 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
433 map, SST_HSW_CHANNEL_CONFIG_STEREO);
434
435 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
436 if (ret < 0) {
437 dev_err(rtd->dev, "error: could not set channels %d\n",
438 channels);
439 return ret;
440 }
441
442 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
443 if (ret < 0) {
444 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
445 params_buffer_bytes(params), ret);
446 return ret;
447 }
448
449 dmab = snd_pcm_get_dma_buf(substream);
450
451 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
452 runtime->dma_bytes, rtd->cpu_dai->id);
453 if (ret < 0)
454 return ret;
455
456 sst_hsw_stream_set_style(hsw, pcm_data->stream,
457 SST_HSW_INTERLEAVING_PER_CHANNEL);
458
459 if (runtime->dma_bytes % PAGE_SIZE)
460 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
461 else
462 pages = runtime->dma_bytes / PAGE_SIZE;
463
464 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
465 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
466 pages, runtime->dma_bytes, 0,
467 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
468 if (ret < 0) {
469 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
470 return ret;
471 }
472
473 dsp = sst_hsw_get_dsp(hsw);
474
475 module_data = sst_module_get_from_id(dsp, module_id);
476 if (module_data == NULL) {
477 dev_err(rtd->dev, "error: failed to get module config\n");
478 return -EINVAL;
479 }
480
481 /* we use hardcoded memory offsets atm, will be updated for new FW */
482 if (stream_type == SST_HSW_STREAM_TYPE_CAPTURE) {
483 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
484 SST_HSW_MODULE_PCM_CAPTURE, module_data->entry);
485 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
486 0x449400, 0x4000);
487 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
488 0x400000, 0);
489 } else { /* stream_type == SST_HSW_STREAM_TYPE_SYSTEM */
490 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
491 SST_HSW_MODULE_PCM_SYSTEM, module_data->entry);
492
493 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
494 module_data->offset, module_data->size);
495 sst_hsw_stream_set_pmemory_info(hsw, pcm_data->stream,
496 0x44d400, 0x3800);
497
498 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
499 module_data->offset, module_data->size);
500 sst_hsw_stream_set_smemory_info(hsw, pcm_data->stream,
501 0x400000, 0);
502 }
503
504 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
505 if (ret < 0) {
506 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
507 return ret;
508 }
509 pcm_data->allocated = true;
510
511 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
512 if (ret < 0)
513 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
514
515 return 0;
516 }
517
518 static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
519 {
520 snd_pcm_lib_free_pages(substream);
521 return 0;
522 }
523
524 static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
525 {
526 struct snd_soc_pcm_runtime *rtd = substream->private_data;
527 struct hsw_priv_data *pdata =
528 snd_soc_platform_get_drvdata(rtd->platform);
529 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
530 struct sst_hsw *hsw = pdata->hsw;
531
532 switch (cmd) {
533 case SNDRV_PCM_TRIGGER_START:
534 case SNDRV_PCM_TRIGGER_RESUME:
535 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
536 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
537 break;
538 case SNDRV_PCM_TRIGGER_STOP:
539 case SNDRV_PCM_TRIGGER_SUSPEND:
540 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
541 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
542 break;
543 default:
544 break;
545 }
546
547 return 0;
548 }
549
550 static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
551 {
552 struct hsw_pcm_data *pcm_data = data;
553 struct snd_pcm_substream *substream = pcm_data->substream;
554 struct snd_pcm_runtime *runtime = substream->runtime;
555 struct snd_soc_pcm_runtime *rtd = substream->private_data;
556 u32 pos;
557
558 pos = frames_to_bytes(runtime,
559 (runtime->control->appl_ptr % runtime->buffer_size));
560
561 dev_dbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
562
563 /* let alsa know we have play a period */
564 snd_pcm_period_elapsed(substream);
565 return pos;
566 }
567
568 static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
569 {
570 struct snd_soc_pcm_runtime *rtd = substream->private_data;
571 struct snd_pcm_runtime *runtime = substream->runtime;
572 struct hsw_priv_data *pdata =
573 snd_soc_platform_get_drvdata(rtd->platform);
574 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
575 struct sst_hsw *hsw = pdata->hsw;
576 snd_pcm_uframes_t offset;
577 uint64_t ppos;
578 u32 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
579
580 offset = bytes_to_frames(runtime, position);
581 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
582
583 dev_dbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
584 position, ppos);
585 return offset;
586 }
587
588 static int hsw_pcm_open(struct snd_pcm_substream *substream)
589 {
590 struct snd_soc_pcm_runtime *rtd = substream->private_data;
591 struct hsw_priv_data *pdata =
592 snd_soc_platform_get_drvdata(rtd->platform);
593 struct hsw_pcm_data *pcm_data;
594 struct sst_hsw *hsw = pdata->hsw;
595
596 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
597
598 mutex_lock(&pcm_data->mutex);
599
600 snd_soc_pcm_set_drvdata(rtd, pcm_data);
601 pcm_data->substream = substream;
602
603 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
604
605 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
606 hsw_notify_pointer, pcm_data);
607 if (pcm_data->stream == NULL) {
608 dev_err(rtd->dev, "error: failed to create stream\n");
609 mutex_unlock(&pcm_data->mutex);
610 return -EINVAL;
611 }
612
613 /* Set previous saved volume */
614 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
615 0, pcm_data->volume[0]);
616 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
617 1, pcm_data->volume[1]);
618
619 mutex_unlock(&pcm_data->mutex);
620 return 0;
621 }
622
623 static int hsw_pcm_close(struct snd_pcm_substream *substream)
624 {
625 struct snd_soc_pcm_runtime *rtd = substream->private_data;
626 struct hsw_priv_data *pdata =
627 snd_soc_platform_get_drvdata(rtd->platform);
628 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
629 struct sst_hsw *hsw = pdata->hsw;
630 int ret;
631
632 mutex_lock(&pcm_data->mutex);
633 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
634 if (ret < 0) {
635 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
636 goto out;
637 }
638
639 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
640 if (ret < 0) {
641 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
642 goto out;
643 }
644 pcm_data->allocated = 0;
645 pcm_data->stream = NULL;
646
647 out:
648 mutex_unlock(&pcm_data->mutex);
649 return ret;
650 }
651
652 static struct snd_pcm_ops hsw_pcm_ops = {
653 .open = hsw_pcm_open,
654 .close = hsw_pcm_close,
655 .ioctl = snd_pcm_lib_ioctl,
656 .hw_params = hsw_pcm_hw_params,
657 .hw_free = hsw_pcm_hw_free,
658 .trigger = hsw_pcm_trigger,
659 .pointer = hsw_pcm_pointer,
660 .page = snd_pcm_sgbuf_ops_page,
661 };
662
663 static void hsw_pcm_free(struct snd_pcm *pcm)
664 {
665 snd_pcm_lib_preallocate_free_for_all(pcm);
666 }
667
668 static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
669 {
670 struct snd_pcm *pcm = rtd->pcm;
671 struct snd_soc_platform *platform = rtd->platform;
672 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
673 struct device *dev = pdata->dma_dev;
674 int ret = 0;
675
676 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
677 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
678 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
679 SNDRV_DMA_TYPE_DEV_SG,
680 dev,
681 hsw_pcm_hardware.buffer_bytes_max,
682 hsw_pcm_hardware.buffer_bytes_max);
683 if (ret) {
684 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
685 ret);
686 return ret;
687 }
688 }
689
690 return ret;
691 }
692
693 #define HSW_FORMATS \
694 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
695
696 static struct snd_soc_dai_driver hsw_dais[] = {
697 {
698 .name = "System Pin",
699 .playback = {
700 .stream_name = "System Playback",
701 .channels_min = 2,
702 .channels_max = 2,
703 .rates = SNDRV_PCM_RATE_48000,
704 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
705 },
706 },
707 {
708 /* PCM */
709 .name = "Offload0 Pin",
710 .playback = {
711 .stream_name = "Offload0 Playback",
712 .channels_min = 2,
713 .channels_max = 2,
714 .rates = SNDRV_PCM_RATE_8000_192000,
715 .formats = HSW_FORMATS,
716 },
717 },
718 {
719 /* PCM */
720 .name = "Offload1 Pin",
721 .playback = {
722 .stream_name = "Offload1 Playback",
723 .channels_min = 2,
724 .channels_max = 2,
725 .rates = SNDRV_PCM_RATE_8000_192000,
726 .formats = HSW_FORMATS,
727 },
728 },
729 {
730 .name = "Loopback Pin",
731 .capture = {
732 .stream_name = "Loopback Capture",
733 .channels_min = 2,
734 .channels_max = 2,
735 .rates = SNDRV_PCM_RATE_48000,
736 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
737 },
738 },
739 {
740 .name = "Capture Pin",
741 .capture = {
742 .stream_name = "Analog Capture",
743 .channels_min = 2,
744 .channels_max = 2,
745 .rates = SNDRV_PCM_RATE_48000,
746 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
747 },
748 },
749 };
750
751 static const struct snd_soc_dapm_widget widgets[] = {
752
753 /* Backend DAIs */
754 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
755 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
756 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
757 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
758
759 /* Global Playback Mixer */
760 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
761 };
762
763 static const struct snd_soc_dapm_route graph[] = {
764
765 /* Playback Mixer */
766 {"Playback VMixer", NULL, "System Playback"},
767 {"Playback VMixer", NULL, "Offload0 Playback"},
768 {"Playback VMixer", NULL, "Offload1 Playback"},
769
770 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
771
772 {"Analog Capture", NULL, "SSP0 CODEC IN"},
773 };
774
775 static int hsw_pcm_probe(struct snd_soc_platform *platform)
776 {
777 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
778 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
779 struct device *dma_dev = pdata->dma_dev;
780 int i, ret = 0;
781
782 /* allocate DSP buffer page tables */
783 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
784
785 mutex_init(&priv_data->pcm[i].mutex);
786
787 /* playback */
788 if (hsw_dais[i].playback.channels_min) {
789 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
790 PAGE_SIZE, &priv_data->dmab[i][0]);
791 if (ret < 0)
792 goto err;
793 }
794
795 /* capture */
796 if (hsw_dais[i].capture.channels_min) {
797 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
798 PAGE_SIZE, &priv_data->dmab[i][1]);
799 if (ret < 0)
800 goto err;
801 }
802 }
803
804 return 0;
805
806 err:
807 for (;i >= 0; i--) {
808 if (hsw_dais[i].playback.channels_min)
809 snd_dma_free_pages(&priv_data->dmab[i][0]);
810 if (hsw_dais[i].capture.channels_min)
811 snd_dma_free_pages(&priv_data->dmab[i][1]);
812 }
813 return ret;
814 }
815
816 static int hsw_pcm_remove(struct snd_soc_platform *platform)
817 {
818 struct hsw_priv_data *priv_data =
819 snd_soc_platform_get_drvdata(platform);
820 int i;
821
822 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
823 if (hsw_dais[i].playback.channels_min)
824 snd_dma_free_pages(&priv_data->dmab[i][0]);
825 if (hsw_dais[i].capture.channels_min)
826 snd_dma_free_pages(&priv_data->dmab[i][1]);
827 }
828
829 return 0;
830 }
831
832 static struct snd_soc_platform_driver hsw_soc_platform = {
833 .probe = hsw_pcm_probe,
834 .remove = hsw_pcm_remove,
835 .ops = &hsw_pcm_ops,
836 .pcm_new = hsw_pcm_new,
837 .pcm_free = hsw_pcm_free,
838 };
839
840 static const struct snd_soc_component_driver hsw_dai_component = {
841 .name = "haswell-dai",
842 .controls = hsw_volume_controls,
843 .num_controls = ARRAY_SIZE(hsw_volume_controls),
844 .dapm_widgets = widgets,
845 .num_dapm_widgets = ARRAY_SIZE(widgets),
846 .dapm_routes = graph,
847 .num_dapm_routes = ARRAY_SIZE(graph),
848 };
849
850 static int hsw_pcm_dev_probe(struct platform_device *pdev)
851 {
852 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
853 struct hsw_priv_data *priv_data;
854 int ret;
855
856 if (!sst_pdata)
857 return -EINVAL;
858
859 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
860 if (!priv_data)
861 return -ENOMEM;
862
863 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
864 if (ret < 0)
865 return -ENODEV;
866
867 priv_data->hsw = sst_pdata->dsp;
868 platform_set_drvdata(pdev, priv_data);
869
870 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
871 if (ret < 0)
872 goto err_plat;
873
874 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
875 hsw_dais, ARRAY_SIZE(hsw_dais));
876 if (ret < 0)
877 goto err_comp;
878
879 return 0;
880
881 err_comp:
882 snd_soc_unregister_platform(&pdev->dev);
883 err_plat:
884 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
885 return 0;
886 }
887
888 static int hsw_pcm_dev_remove(struct platform_device *pdev)
889 {
890 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
891
892 snd_soc_unregister_platform(&pdev->dev);
893 snd_soc_unregister_component(&pdev->dev);
894 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
895
896 return 0;
897 }
898
899 static struct platform_driver hsw_pcm_driver = {
900 .driver = {
901 .name = "haswell-pcm-audio",
902 .owner = THIS_MODULE,
903 },
904
905 .probe = hsw_pcm_dev_probe,
906 .remove = hsw_pcm_dev_remove,
907 };
908 module_platform_driver(hsw_pcm_driver);
909
910 MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
911 MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
912 MODULE_LICENSE("GPL v2");
913 MODULE_ALIAS("platform:haswell-pcm-audio");
This page took 0.051361 seconds and 5 git commands to generate.