2 * Intel SST Haswell/Broadwell PCM Support
4 * Copyright (C) 2013, Intel Corporation. All rights reserved.
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.
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.
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>
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>
32 #include "sst-haswell-ipc.h"
33 #include "sst-dsp-priv.h"
36 #define HSW_PCM_COUNT 6
37 #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */
39 /* simple volume table */
40 static const u32 volume_map
[] = {
74 #define HSW_PCM_PERIODS_MAX 64
75 #define HSW_PCM_PERIODS_MIN 2
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
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
,
100 struct hsw_pcm_module_map
{
102 enum sst_hsw_module_id mod_id
;
105 /* private data for each PCM DSP stream */
106 struct hsw_pcm_data
{
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
;
113 struct snd_pcm_substream
*substream
;
114 struct snd_compr_stream
*cstream
;
118 int persistent_offset
;
126 /* private data for the driver */
127 struct hsw_priv_data
{
131 enum hsw_pm_state pm_state
;
132 struct snd_soc_card
*soc_card
;
135 struct snd_dma_buffer dmab
[HSW_PCM_COUNT
][2];
138 struct hsw_pcm_data pcm
[HSW_PCM_COUNT
];
141 static u32
hsw_notify_pointer(struct sst_hsw_stream
*stream
, void *data
);
143 static inline u32
hsw_mixer_to_ipc(unsigned int value
)
145 if (value
>= ARRAY_SIZE(volume_map
))
146 return volume_map
[0];
148 return volume_map
[value
];
151 static inline unsigned int hsw_ipc_to_mixer(u32 value
)
155 for (i
= 0; i
< ARRAY_SIZE(volume_map
); i
++) {
156 if (volume_map
[i
] >= value
)
163 static int hsw_stream_volume_put(struct snd_kcontrol
*kcontrol
,
164 struct snd_ctl_elem_value
*ucontrol
)
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
;
175 mutex_lock(&pcm_data
->mutex
);
176 pm_runtime_get_sync(pdata
->dev
);
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
);
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
);
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
);
201 pm_runtime_mark_last_busy(pdata
->dev
);
202 pm_runtime_put_autosuspend(pdata
->dev
);
203 mutex_unlock(&pcm_data
->mutex
);
207 static int hsw_stream_volume_get(struct snd_kcontrol
*kcontrol
,
208 struct snd_ctl_elem_value
*ucontrol
)
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
;
219 mutex_lock(&pcm_data
->mutex
);
220 pm_runtime_get_sync(pdata
->dev
);
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
);
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
);
238 pm_runtime_mark_last_busy(pdata
->dev
);
239 pm_runtime_put_autosuspend(pdata
->dev
);
240 mutex_unlock(&pcm_data
->mutex
);
245 static int hsw_volume_put(struct snd_kcontrol
*kcontrol
,
246 struct snd_ctl_elem_value
*ucontrol
)
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
;
253 pm_runtime_get_sync(pdata
->dev
);
255 if (ucontrol
->value
.integer
.value
[0] ==
256 ucontrol
->value
.integer
.value
[1]) {
258 volume
= hsw_mixer_to_ipc(ucontrol
->value
.integer
.value
[0]);
259 sst_hsw_mixer_set_volume(hsw
, 0, SST_HSW_CHANNELS_ALL
, volume
);
262 volume
= hsw_mixer_to_ipc(ucontrol
->value
.integer
.value
[0]);
263 sst_hsw_mixer_set_volume(hsw
, 0, 0, volume
);
265 volume
= hsw_mixer_to_ipc(ucontrol
->value
.integer
.value
[1]);
266 sst_hsw_mixer_set_volume(hsw
, 0, 1, volume
);
269 pm_runtime_mark_last_busy(pdata
->dev
);
270 pm_runtime_put_autosuspend(pdata
->dev
);
274 static int hsw_volume_get(struct snd_kcontrol
*kcontrol
,
275 struct snd_ctl_elem_value
*ucontrol
)
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;
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
);
286 sst_hsw_mixer_get_volume(hsw
, 0, 1, &volume
);
287 ucontrol
->value
.integer
.value
[1] = hsw_ipc_to_mixer(volume
);
289 pm_runtime_mark_last_busy(pdata
->dev
);
290 pm_runtime_put_autosuspend(pdata
->dev
);
294 /* TLV used by both global and stream volumes */
295 static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv
, -9000, 300, 1);
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
),
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
)
322 struct snd_dma_buffer
*dmab
= snd_pcm_get_dma_buf(substream
);
323 int i
, pages
, stream
= substream
->stream
;
325 pages
= snd_sgbuf_aligned_pages(size
);
327 dev_dbg(rtd
->dev
, "generating page table for %p size 0x%zu pages %d\n",
328 dma_area
, size
, pages
);
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
;
335 dev_dbg(rtd
->dev
, "pfn i %i idx %d pfn %x\n", i
, idx
, pfn
);
337 pg_table
= (u32
*)(pdata
->dmab
[pcm
][stream
].area
+ idx
);
340 *pg_table
|= (pfn
<< 4);
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
)
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
;
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
;
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
);
371 dev_dbg(rtd
->dev
, "error: reset stream failed %d\n",
374 ret
= sst_hsw_stream_free(hsw
, pcm_data
->stream
);
376 dev_dbg(rtd
->dev
, "error: free stream failed %d\n",
380 pcm_data
->allocated
= false;
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");
390 /* stream direction */
391 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
392 path_id
= SST_HSW_STREAM_PATH_SSP0_OUT
;
394 path_id
= SST_HSW_STREAM_PATH_SSP0_IN
;
396 /* DSP stream type depends on DAI ID */
397 switch (rtd
->cpu_dai
->id
) {
399 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
400 stream_type
= SST_HSW_STREAM_TYPE_SYSTEM
;
401 module_id
= SST_HSW_MODULE_PCM_SYSTEM
;
404 stream_type
= SST_HSW_STREAM_TYPE_CAPTURE
;
405 module_id
= SST_HSW_MODULE_PCM_CAPTURE
;
410 stream_type
= SST_HSW_STREAM_TYPE_RENDER
;
411 module_id
= SST_HSW_MODULE_PCM
;
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
;
420 dev_err(rtd
->dev
, "error: invalid DAI ID %d\n",
425 ret
= sst_hsw_stream_format(hsw
, pcm_data
->stream
,
426 path_id
, stream_type
, SST_HSW_STREAM_FORMAT_PCM_FORMAT
);
428 dev_err(rtd
->dev
, "error: failed to set format %d\n", ret
);
432 rate
= params_rate(params
);
433 ret
= sst_hsw_stream_set_rate(hsw
, pcm_data
->stream
, rate
);
435 dev_err(rtd
->dev
, "error: could not set rate %d\n", rate
);
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);
444 case SNDRV_PCM_FORMAT_S24_LE
:
445 bits
= SST_HSW_DEPTH_32BIT
;
446 sst_hsw_stream_set_valid(hsw
, pcm_data
->stream
, 24);
448 case SNDRV_PCM_FORMAT_S8
:
449 bits
= SST_HSW_DEPTH_8BIT
;
450 sst_hsw_stream_set_valid(hsw
, pcm_data
->stream
, 8);
452 case SNDRV_PCM_FORMAT_S32_LE
:
453 bits
= SST_HSW_DEPTH_32BIT
;
454 sst_hsw_stream_set_valid(hsw
, pcm_data
->stream
, 32);
457 dev_err(rtd
->dev
, "error: invalid format %d\n",
458 params_format(params
));
462 ret
= sst_hsw_stream_set_bits(hsw
, pcm_data
->stream
, bits
);
464 dev_err(rtd
->dev
, "error: could not set bits %d\n", bits
);
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
);
473 ret
= sst_hsw_stream_set_channels(hsw
, pcm_data
->stream
, channels
);
475 dev_err(rtd
->dev
, "error: could not set channels %d\n",
480 ret
= snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
482 dev_err(rtd
->dev
, "error: could not allocate %d bytes for PCM %d\n",
483 params_buffer_bytes(params
), ret
);
487 dmab
= snd_pcm_get_dma_buf(substream
);
489 ret
= create_adsp_page_table(substream
, pdata
, rtd
, runtime
->dma_area
,
490 runtime
->dma_bytes
, rtd
->cpu_dai
->id
);
494 sst_hsw_stream_set_style(hsw
, pcm_data
->stream
,
495 SST_HSW_INTERLEAVING_PER_CHANNEL
);
497 if (runtime
->dma_bytes
% PAGE_SIZE
)
498 pages
= (runtime
->dma_bytes
/ PAGE_SIZE
) + 1;
500 pages
= runtime
->dma_bytes
/ PAGE_SIZE
;
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
);
507 dev_err(rtd
->dev
, "error: failed to set DMA buffer %d\n", ret
);
511 dsp
= sst_hsw_get_dsp(hsw
);
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");
519 sst_hsw_stream_set_module_info(hsw
, pcm_data
->stream
,
522 ret
= sst_hsw_stream_commit(hsw
, pcm_data
->stream
);
524 dev_err(rtd
->dev
, "error: failed to commit stream %d\n", ret
);
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;
537 ret
= sst_hsw_stream_pause(hsw
, pcm_data
->stream
, 1);
539 dev_err(rtd
->dev
, "error: failed to pause %d\n", ret
);
544 static int hsw_pcm_hw_free(struct snd_pcm_substream
*substream
)
546 snd_pcm_lib_free_pages(substream
);
550 static int hsw_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
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
;
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);
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);
576 static u32
hsw_notify_pointer(struct sst_hsw_stream
*stream
, void *data
)
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
;
584 pos
= frames_to_bytes(runtime
,
585 (runtime
->control
->appl_ptr
% runtime
->buffer_size
));
587 dev_vdbg(rtd
->dev
, "PCM: App pointer %d bytes\n", pos
);
589 /* let alsa know we have play a period */
590 snd_pcm_period_elapsed(substream
);
594 static snd_pcm_uframes_t
hsw_pcm_pointer(struct snd_pcm_substream
*substream
)
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
;
604 u32 position
= sst_hsw_get_dsp_position(hsw
, pcm_data
->stream
);
606 offset
= bytes_to_frames(runtime
, position
);
607 ppos
= sst_hsw_get_dsp_presentation_position(hsw
, pcm_data
->stream
);
609 dev_vdbg(rtd
->dev
, "PCM: DMA pointer %du bytes, pos %llu\n",
614 static int hsw_pcm_open(struct snd_pcm_substream
*substream
)
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
;
622 pcm_data
= &pdata
->pcm
[rtd
->cpu_dai
->id
];
624 mutex_lock(&pcm_data
->mutex
);
625 pm_runtime_get_sync(pdata
->dev
);
627 snd_soc_pcm_set_drvdata(rtd
, pcm_data
);
628 pcm_data
->substream
= substream
;
630 snd_soc_set_runtime_hwparams(substream
, &hsw_pcm_hardware
);
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
);
642 mutex_unlock(&pcm_data
->mutex
);
646 static int hsw_pcm_close(struct snd_pcm_substream
*substream
)
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
;
655 mutex_lock(&pcm_data
->mutex
);
656 ret
= sst_hsw_stream_reset(hsw
, pcm_data
->stream
);
658 dev_dbg(rtd
->dev
, "error: reset stream failed %d\n", ret
);
662 ret
= sst_hsw_stream_free(hsw
, pcm_data
->stream
);
664 dev_dbg(rtd
->dev
, "error: free stream failed %d\n", ret
);
667 pcm_data
->allocated
= 0;
668 pcm_data
->stream
= NULL
;
671 pm_runtime_mark_last_busy(pdata
->dev
);
672 pm_runtime_put_autosuspend(pdata
->dev
);
673 mutex_unlock(&pcm_data
->mutex
);
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
,
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
},
697 static int hsw_pcm_create_modules(struct hsw_priv_data
*pdata
)
699 struct sst_hsw
*hsw
= pdata
->hsw
;
700 struct hsw_pcm_data
*pcm_data
;
703 for (i
= 0; i
< ARRAY_SIZE(mod_map
); i
++) {
704 pcm_data
= &pdata
->pcm
[i
];
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
)
711 pcm_data
->persistent_offset
=
712 pcm_data
->runtime
->persistent_offset
;
718 for (--i
; i
>= 0; i
--) {
719 pcm_data
= &pdata
->pcm
[i
];
720 sst_hsw_runtime_module_free(pcm_data
->runtime
);
726 static void hsw_pcm_free_modules(struct hsw_priv_data
*pdata
)
728 struct hsw_pcm_data
*pcm_data
;
731 for (i
= 0; i
< ARRAY_SIZE(mod_map
); i
++) {
732 pcm_data
= &pdata
->pcm
[i
];
734 sst_hsw_runtime_module_free(pcm_data
->runtime
);
738 static void hsw_pcm_free(struct snd_pcm
*pcm
)
740 snd_pcm_lib_preallocate_free_for_all(pcm
);
743 static int hsw_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
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
;
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
,
757 hsw_pcm_hardware
.buffer_bytes_max
,
758 hsw_pcm_hardware
.buffer_bytes_max
);
760 dev_err(rtd
->dev
, "dma buffer allocation failed %d\n",
765 priv_data
->pcm
[rtd
->cpu_dai
->id
].hsw_pcm
= pcm
;
770 #define HSW_FORMATS \
771 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
773 static struct snd_soc_dai_driver hsw_dais
[] = {
775 .name
= "System Pin",
776 .id
= HSW_PCM_DAI_ID_SYSTEM
,
778 .stream_name
= "System Playback",
781 .rates
= SNDRV_PCM_RATE_48000
,
782 .formats
= SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S16_LE
,
785 .stream_name
= "Analog Capture",
788 .rates
= SNDRV_PCM_RATE_48000
,
789 .formats
= SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S16_LE
,
794 .name
= "Offload0 Pin",
795 .id
= HSW_PCM_DAI_ID_OFFLOAD0
,
797 .stream_name
= "Offload0 Playback",
800 .rates
= SNDRV_PCM_RATE_8000_192000
,
801 .formats
= HSW_FORMATS
,
806 .name
= "Offload1 Pin",
807 .id
= HSW_PCM_DAI_ID_OFFLOAD1
,
809 .stream_name
= "Offload1 Playback",
812 .rates
= SNDRV_PCM_RATE_8000_192000
,
813 .formats
= HSW_FORMATS
,
817 .name
= "Loopback Pin",
818 .id
= HSW_PCM_DAI_ID_LOOPBACK
,
820 .stream_name
= "Loopback Capture",
823 .rates
= SNDRV_PCM_RATE_48000
,
824 .formats
= SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S16_LE
,
829 static const struct snd_soc_dapm_widget widgets
[] = {
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),
837 /* Global Playback Mixer */
838 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM
, 0, 0, NULL
, 0),
841 static const struct snd_soc_dapm_route graph
[] = {
844 {"Playback VMixer", NULL
, "System Playback"},
845 {"Playback VMixer", NULL
, "Offload0 Playback"},
846 {"Playback VMixer", NULL
, "Offload1 Playback"},
848 {"SSP0 CODEC OUT", NULL
, "Playback VMixer"},
850 {"Analog Capture", NULL
, "SSP0 CODEC IN"},
853 static int hsw_pcm_probe(struct snd_soc_platform
*platform
)
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
;
864 dma_dev
= pdata
->dma_dev
;
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
;
871 /* allocate DSP buffer page tables */
872 for (i
= 0; i
< ARRAY_SIZE(hsw_dais
); i
++) {
874 mutex_init(&priv_data
->pcm
[i
].mutex
);
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]);
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]);
893 /* allocate runtime modules */
894 hsw_pcm_create_modules(priv_data
);
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
);
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]);
915 static int hsw_pcm_remove(struct snd_soc_platform
*platform
)
917 struct hsw_priv_data
*priv_data
=
918 snd_soc_platform_get_drvdata(platform
);
921 pm_runtime_disable(platform
->dev
);
922 hsw_pcm_free_modules(priv_data
);
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]);
934 static struct snd_soc_platform_driver hsw_soc_platform
= {
935 .probe
= hsw_pcm_probe
,
936 .remove
= hsw_pcm_remove
,
938 .pcm_new
= hsw_pcm_new
,
939 .pcm_free
= hsw_pcm_free
,
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
),
952 static int hsw_pcm_dev_probe(struct platform_device
*pdev
)
954 struct sst_pdata
*sst_pdata
= dev_get_platdata(&pdev
->dev
);
955 struct hsw_priv_data
*priv_data
;
961 priv_data
= devm_kzalloc(&pdev
->dev
, sizeof(*priv_data
), GFP_KERNEL
);
965 ret
= sst_hsw_dsp_init(&pdev
->dev
, sst_pdata
);
969 priv_data
->hsw
= sst_pdata
->dsp
;
970 platform_set_drvdata(pdev
, priv_data
);
972 ret
= snd_soc_register_platform(&pdev
->dev
, &hsw_soc_platform
);
976 ret
= snd_soc_register_component(&pdev
->dev
, &hsw_dai_component
,
977 hsw_dais
, ARRAY_SIZE(hsw_dais
));
984 snd_soc_unregister_platform(&pdev
->dev
);
986 sst_hsw_dsp_free(&pdev
->dev
, sst_pdata
);
990 static int hsw_pcm_dev_remove(struct platform_device
*pdev
)
992 struct sst_pdata
*sst_pdata
= dev_get_platdata(&pdev
->dev
);
994 snd_soc_unregister_platform(&pdev
->dev
);
995 snd_soc_unregister_component(&pdev
->dev
);
996 sst_hsw_dsp_free(&pdev
->dev
, sst_pdata
);
1003 static int hsw_pcm_runtime_idle(struct device
*dev
)
1008 static int hsw_pcm_runtime_suspend(struct device
*dev
)
1010 struct hsw_priv_data
*pdata
= dev_get_drvdata(dev
);
1011 struct sst_hsw
*hsw
= pdata
->hsw
;
1013 if (pdata
->pm_state
== HSW_PM_STATE_D3
)
1016 sst_hsw_dsp_runtime_suspend(hsw
);
1017 sst_hsw_dsp_runtime_sleep(hsw
);
1018 pdata
->pm_state
= HSW_PM_STATE_D3
;
1023 static int hsw_pcm_runtime_resume(struct device
*dev
)
1025 struct hsw_priv_data
*pdata
= dev_get_drvdata(dev
);
1026 struct sst_hsw
*hsw
= pdata
->hsw
;
1029 if (pdata
->pm_state
== HSW_PM_STATE_D0
)
1032 ret
= sst_hsw_dsp_load(hsw
);
1034 dev_err(dev
, "failed to reload %d\n", ret
);
1038 ret
= hsw_pcm_create_modules(pdata
);
1040 dev_err(dev
, "failed to create modules %d\n", ret
);
1044 ret
= sst_hsw_dsp_runtime_resume(hsw
);
1047 else if (ret
== 1) /* no action required */
1050 pdata
->pm_state
= HSW_PM_STATE_D0
;
1055 #define hsw_pcm_runtime_idle NULL
1056 #define hsw_pcm_runtime_suspend NULL
1057 #define hsw_pcm_runtime_resume NULL
1062 static void hsw_pcm_complete(struct device
*dev
)
1064 struct hsw_priv_data
*pdata
= dev_get_drvdata(dev
);
1065 struct sst_hsw
*hsw
= pdata
->hsw
;
1066 struct hsw_pcm_data
*pcm_data
;
1069 if (pdata
->pm_state
== HSW_PM_STATE_D0
)
1072 err
= sst_hsw_dsp_load(hsw
);
1074 dev_err(dev
, "failed to reload %d\n", err
);
1078 err
= hsw_pcm_create_modules(pdata
);
1080 dev_err(dev
, "failed to create modules %d\n", err
);
1084 for (i
= 0; i
< HSW_PCM_DAI_ID_CAPTURE
+ 1; i
++) {
1085 pcm_data
= &pdata
->pcm
[i
];
1087 if (!pcm_data
->substream
)
1090 err
= sst_module_runtime_restore(pcm_data
->runtime
,
1091 &pcm_data
->context
);
1093 dev_err(dev
, "failed to restore context for PCM %d\n", i
);
1096 snd_soc_resume(pdata
->soc_card
->dev
);
1098 err
= sst_hsw_dsp_runtime_resume(hsw
);
1101 else if (err
== 1) /* no action required */
1104 pdata
->pm_state
= HSW_PM_STATE_D0
;
1108 static int hsw_pcm_prepare(struct device
*dev
)
1110 struct hsw_priv_data
*pdata
= dev_get_drvdata(dev
);
1111 struct sst_hsw
*hsw
= pdata
->hsw
;
1112 struct hsw_pcm_data
*pcm_data
;
1115 if (pdata
->pm_state
== HSW_PM_STATE_D3
)
1117 /* suspend all active streams */
1118 for (i
= 0; i
< HSW_PCM_DAI_ID_CAPTURE
+ 1; i
++) {
1119 pcm_data
= &pdata
->pcm
[i
];
1121 if (!pcm_data
->substream
)
1123 dev_dbg(dev
, "suspending pcm %d\n", i
);
1124 snd_pcm_suspend_all(pcm_data
->hsw_pcm
);
1126 /* We need to wait until the DSP FW stops the streams */
1130 snd_soc_suspend(pdata
->soc_card
->dev
);
1131 snd_soc_poweroff(pdata
->soc_card
->dev
);
1133 /* enter D3 state and stall */
1134 sst_hsw_dsp_runtime_suspend(hsw
);
1136 /* preserve persistent memory */
1137 for (i
= 0; i
< HSW_PCM_DAI_ID_CAPTURE
+ 1; i
++) {
1138 pcm_data
= &pdata
->pcm
[i
];
1140 if (!pcm_data
->substream
)
1143 dev_dbg(dev
, "saving context pcm %d\n", i
);
1144 err
= sst_module_runtime_save(pcm_data
->runtime
,
1145 &pcm_data
->context
);
1147 dev_err(dev
, "failed to save context for PCM %d\n", i
);
1150 /* put the DSP to sleep */
1151 sst_hsw_dsp_runtime_sleep(hsw
);
1152 pdata
->pm_state
= HSW_PM_STATE_D3
;
1158 #define hsw_pcm_prepare NULL
1159 #define hsw_pcm_complete NULL
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
,
1170 static struct platform_driver hsw_pcm_driver
= {
1172 .name
= "haswell-pcm-audio",
1176 .probe
= hsw_pcm_dev_probe
,
1177 .remove
= hsw_pcm_dev_remove
,
1179 module_platform_driver(hsw_pcm_driver
);
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");