blob: 5448f79283b7f3a12da0146a0fb7d1e950c85740 [file] [log] [blame]
Mark Browna4b12992014-03-12 23:04:35 +00001/*
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>
Mark Browna4b12992014-03-12 23:04:35 +000020#include <linux/delay.h>
Liam Girdwood2e4f75912014-10-29 17:40:43 +000021#include <linux/pm_runtime.h>
Mark Browna4b12992014-03-12 23:04:35 +000022#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 */
40static 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
Liam Girdwood2e4f75912014-10-29 17:40:43 +000077#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
Liam Girdwood2e4f75912014-10-29 17:40:43 +000081
82
Mark Browna4b12992014-03-12 23:04:35 +000083static const struct snd_pcm_hardware hsw_pcm_hardware = {
84 .info = SNDRV_PCM_INFO_MMAP |
85 SNDRV_PCM_INFO_MMAP_VALID |
86 SNDRV_PCM_INFO_INTERLEAVED |
87 SNDRV_PCM_INFO_PAUSE |
88 SNDRV_PCM_INFO_RESUME |
89 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Jie Yang8e897612014-07-14 17:37:36 +080090 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
Mark Browna4b12992014-03-12 23:04:35 +000091 SNDRV_PCM_FMTBIT_S32_LE,
92 .period_bytes_min = PAGE_SIZE,
93 .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
94 .periods_min = HSW_PCM_PERIODS_MIN,
95 .periods_max = HSW_PCM_PERIODS_MAX,
96 .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
97};
98
Liam Girdwoode9600bc2014-10-28 17:37:12 +000099struct hsw_pcm_module_map {
100 int dai_id;
Jie Yang98b9c1d2015-01-08 14:32:20 +0800101 int stream;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000102 enum sst_hsw_module_id mod_id;
103};
104
Mark Browna4b12992014-03-12 23:04:35 +0000105/* private data for each PCM DSP stream */
106struct hsw_pcm_data {
107 int dai_id;
108 struct sst_hsw_stream *stream;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000109 struct sst_module_runtime *runtime;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000110 struct sst_module_runtime_context context;
111 struct snd_pcm *hsw_pcm;
Mark Browna4b12992014-03-12 23:04:35 +0000112 u32 volume[2];
113 struct snd_pcm_substream *substream;
114 struct snd_compr_stream *cstream;
115 unsigned int wpos;
116 struct mutex mutex;
Liam Girdwood916152c2014-05-02 16:56:32 +0100117 bool allocated;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000118 int persistent_offset;
Mark Browna4b12992014-03-12 23:04:35 +0000119};
120
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000121enum hsw_pm_state {
122 HSW_PM_STATE_D3 = 0,
123 HSW_PM_STATE_D0 = 1,
Mark Browna4b12992014-03-12 23:04:35 +0000124};
125
126/* private data for the driver */
127struct hsw_priv_data {
128 /* runtime DSP */
129 struct sst_hsw *hsw;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000130 struct device *dev;
131 enum hsw_pm_state pm_state;
132 struct snd_soc_card *soc_card;
Mark Browna4b12992014-03-12 23:04:35 +0000133
134 /* page tables */
Liam Girdwood0b708c82014-05-02 16:56:30 +0100135 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
Mark Browna4b12992014-03-12 23:04:35 +0000136
137 /* DAI data */
138 struct hsw_pcm_data pcm[HSW_PCM_COUNT];
139};
140
Liam Girdwood916152c2014-05-02 16:56:32 +0100141static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
142
Mark Browna4b12992014-03-12 23:04:35 +0000143static inline u32 hsw_mixer_to_ipc(unsigned int value)
144{
145 if (value >= ARRAY_SIZE(volume_map))
146 return volume_map[0];
147 else
148 return volume_map[value];
149}
150
151static inline unsigned int hsw_ipc_to_mixer(u32 value)
152{
153 int i;
154
155 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
156 if (volume_map[i] >= value)
157 return i;
158 }
159
160 return i - 1;
161}
162
163static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
164 struct snd_ctl_elem_value *ucontrol)
165{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000166 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
Mark Browna4b12992014-03-12 23:04:35 +0000167 struct soc_mixer_control *mc =
168 (struct soc_mixer_control *)kcontrol->private_value;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000169 struct hsw_priv_data *pdata =
170 snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000171 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
172 struct sst_hsw *hsw = pdata->hsw;
173 u32 volume;
174
175 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000176 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000177
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]);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000183 pm_runtime_mark_last_busy(pdata->dev);
184 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000185 mutex_unlock(&pcm_data->mutex);
186 return 0;
187 }
188
189 if (ucontrol->value.integer.value[0] ==
190 ucontrol->value.integer.value[1]) {
191 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
Jie Yangf1e59822014-11-25 21:00:53 +0800192 /* apply volume value to all channels */
193 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
Mark Browna4b12992014-03-12 23:04:35 +0000194 } else {
195 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
196 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
197 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
198 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
199 }
200
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000201 pm_runtime_mark_last_busy(pdata->dev);
202 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000203 mutex_unlock(&pcm_data->mutex);
204 return 0;
205}
206
207static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
208 struct snd_ctl_elem_value *ucontrol)
209{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000210 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
Mark Browna4b12992014-03-12 23:04:35 +0000211 struct soc_mixer_control *mc =
212 (struct soc_mixer_control *)kcontrol->private_value;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000213 struct hsw_priv_data *pdata =
214 snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000215 struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg];
216 struct sst_hsw *hsw = pdata->hsw;
217 u32 volume;
218
219 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000220 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000221
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]);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000227 pm_runtime_mark_last_busy(pdata->dev);
228 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000229 mutex_unlock(&pcm_data->mutex);
230 return 0;
231 }
232
233 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
234 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
235 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
236 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000237
238 pm_runtime_mark_last_busy(pdata->dev);
239 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000240 mutex_unlock(&pcm_data->mutex);
241
242 return 0;
243}
244
245static int hsw_volume_put(struct snd_kcontrol *kcontrol,
246 struct snd_ctl_elem_value *ucontrol)
247{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000248 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
249 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000250 struct sst_hsw *hsw = pdata->hsw;
251 u32 volume;
252
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000253 pm_runtime_get_sync(pdata->dev);
254
Mark Browna4b12992014-03-12 23:04:35 +0000255 if (ucontrol->value.integer.value[0] ==
256 ucontrol->value.integer.value[1]) {
257
258 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
Jie Yangf1e59822014-11-25 21:00:53 +0800259 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
Mark Browna4b12992014-03-12 23:04:35 +0000260
261 } else {
262 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
263 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
264
265 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
266 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
267 }
268
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000269 pm_runtime_mark_last_busy(pdata->dev);
270 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000271 return 0;
272}
273
274static int hsw_volume_get(struct snd_kcontrol *kcontrol,
275 struct snd_ctl_elem_value *ucontrol)
276{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000277 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
278 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000279 struct sst_hsw *hsw = pdata->hsw;
280 unsigned int volume = 0;
281
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000282 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000283 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
284 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
285
286 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
287 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
288
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000289 pm_runtime_mark_last_busy(pdata->dev);
290 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000291 return 0;
292}
293
294/* TLV used by both global and stream volumes */
295static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
296
297/* System Pin has no volume control */
298static const struct snd_kcontrol_new hsw_volume_controls[] = {
299 /* Global DSP volume */
300 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800301 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000302 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
303 /* Offload 0 volume */
304 SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800305 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000306 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,
Jie Yangc5f04062014-11-28 10:41:28 +0800309 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000310 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
311 /* Mic Capture volume */
Jie Yang7bb73cb2014-11-28 21:52:11 +0800312 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 0, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800313 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000314 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
315};
316
317/* Create DMA buffer page table for DSP */
Liam Girdwood0b708c82014-05-02 16:56:30 +0100318static 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)
Mark Browna4b12992014-03-12 23:04:35 +0000321{
Liam Girdwood0b708c82014-05-02 16:56:30 +0100322 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
323 int i, pages, stream = substream->stream;
Mark Browna4b12992014-03-12 23:04:35 +0000324
Liam Girdwood0b708c82014-05-02 16:56:30 +0100325 pages = snd_sgbuf_aligned_pages(size);
Mark Browna4b12992014-03-12 23:04:35 +0000326
327 dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
328 dma_area, size, pages);
329
330 for (i = 0; i < pages; i++) {
331 u32 idx = (((i << 2) + i)) >> 1;
Liam Girdwood0b708c82014-05-02 16:56:30 +0100332 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
Mark Browna4b12992014-03-12 23:04:35 +0000333 u32 *pg_table;
334
335 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
336
Liam Girdwood0b708c82014-05-02 16:56:30 +0100337 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
Mark Browna4b12992014-03-12 23:04:35 +0000338
339 if (i & 1)
340 *pg_table |= (pfn << 4);
341 else
342 *pg_table |= pfn;
343 }
344
345 return 0;
346}
347
348/* this may get called several times by oss emulation */
349static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
350 struct snd_pcm_hw_params *params)
351{
352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct hsw_priv_data *pdata =
355 snd_soc_platform_get_drvdata(rtd->platform);
356 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
357 struct sst_hsw *hsw = pdata->hsw;
358 struct sst_module *module_data;
359 struct sst_dsp *dsp;
Liam Girdwood0b708c82014-05-02 16:56:30 +0100360 struct snd_dma_buffer *dmab;
Mark Browna4b12992014-03-12 23:04:35 +0000361 enum sst_hsw_stream_type stream_type;
362 enum sst_hsw_stream_path_id path_id;
363 u32 rate, bits, map, pages, module_id;
364 u8 channels;
365 int ret;
366
Liam Girdwood916152c2014-05-02 16:56:32 +0100367 /* check if we are being called a subsequent time */
368 if (pcm_data->allocated) {
369 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
370 if (ret < 0)
371 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
372 ret);
373
374 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
375 if (ret < 0) {
376 dev_dbg(rtd->dev, "error: free stream failed %d\n",
377 ret);
378 return ret;
379 }
380 pcm_data->allocated = false;
381
382 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
383 hsw_notify_pointer, pcm_data);
384 if (pcm_data->stream == NULL) {
385 dev_err(rtd->dev, "error: failed to create stream\n");
386 return -EINVAL;
387 }
388 }
389
Mark Browna4b12992014-03-12 23:04:35 +0000390 /* stream direction */
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
393 else
394 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
395
396 /* DSP stream type depends on DAI ID */
397 switch (rtd->cpu_dai->id) {
398 case 0:
Jie Yang7bb73cb2014-11-28 21:52:11 +0800399 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
400 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
401 module_id = SST_HSW_MODULE_PCM_SYSTEM;
402 }
403 else {
404 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
405 module_id = SST_HSW_MODULE_PCM_CAPTURE;
406 }
Mark Browna4b12992014-03-12 23:04:35 +0000407 break;
408 case 1:
409 case 2:
410 stream_type = SST_HSW_STREAM_TYPE_RENDER;
411 module_id = SST_HSW_MODULE_PCM;
412 break;
413 case 3:
414 /* path ID needs to be OUT for loopback */
415 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
416 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
417 module_id = SST_HSW_MODULE_PCM_REFERENCE;
418 break;
Mark Browna4b12992014-03-12 23:04:35 +0000419 default:
420 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
421 rtd->cpu_dai->id);
422 return -EINVAL;
423 };
424
425 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
426 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
427 if (ret < 0) {
428 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
429 return ret;
430 }
431
432 rate = params_rate(params);
433 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
434 if (ret < 0) {
435 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
436 return ret;
437 }
438
439 switch (params_format(params)) {
440 case SNDRV_PCM_FORMAT_S16_LE:
441 bits = SST_HSW_DEPTH_16BIT;
442 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
443 break;
444 case SNDRV_PCM_FORMAT_S24_LE:
Jie Yang8e897612014-07-14 17:37:36 +0800445 bits = SST_HSW_DEPTH_32BIT;
446 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
447 break;
448 case SNDRV_PCM_FORMAT_S8:
449 bits = SST_HSW_DEPTH_8BIT;
450 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
451 break;
452 case SNDRV_PCM_FORMAT_S32_LE:
453 bits = SST_HSW_DEPTH_32BIT;
Mark Browna4b12992014-03-12 23:04:35 +0000454 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
455 break;
456 default:
457 dev_err(rtd->dev, "error: invalid format %d\n",
458 params_format(params));
459 return -EINVAL;
460 }
461
462 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
463 if (ret < 0) {
464 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
465 return ret;
466 }
467
Mark Browna4b12992014-03-12 23:04:35 +0000468 channels = params_channels(params);
Mark Browna4b12992014-03-12 23:04:35 +0000469 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
470 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
471 map, SST_HSW_CHANNEL_CONFIG_STEREO);
472
473 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
474 if (ret < 0) {
475 dev_err(rtd->dev, "error: could not set channels %d\n",
476 channels);
477 return ret;
478 }
479
480 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
481 if (ret < 0) {
482 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
483 params_buffer_bytes(params), ret);
484 return ret;
485 }
486
Liam Girdwood0b708c82014-05-02 16:56:30 +0100487 dmab = snd_pcm_get_dma_buf(substream);
488
489 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
490 runtime->dma_bytes, rtd->cpu_dai->id);
Mark Browna4b12992014-03-12 23:04:35 +0000491 if (ret < 0)
492 return ret;
493
494 sst_hsw_stream_set_style(hsw, pcm_data->stream,
495 SST_HSW_INTERLEAVING_PER_CHANNEL);
496
497 if (runtime->dma_bytes % PAGE_SIZE)
498 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
499 else
500 pages = runtime->dma_bytes / PAGE_SIZE;
501
502 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100503 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
Mark Browna4b12992014-03-12 23:04:35 +0000504 pages, runtime->dma_bytes, 0,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100505 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
Mark Browna4b12992014-03-12 23:04:35 +0000506 if (ret < 0) {
507 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
508 return ret;
509 }
510
511 dsp = sst_hsw_get_dsp(hsw);
512
513 module_data = sst_module_get_from_id(dsp, module_id);
514 if (module_data == NULL) {
515 dev_err(rtd->dev, "error: failed to get module config\n");
516 return -EINVAL;
517 }
518
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000519 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
520 pcm_data->runtime);
Mark Browna4b12992014-03-12 23:04:35 +0000521
522 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
523 if (ret < 0) {
524 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
525 return ret;
526 }
Jie Yangf1e59822014-11-25 21:00:53 +0800527
528 if (!pcm_data->allocated) {
529 /* Set previous saved volume */
530 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
531 0, pcm_data->volume[0]);
532 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
533 1, pcm_data->volume[1]);
534 pcm_data->allocated = true;
535 }
Mark Browna4b12992014-03-12 23:04:35 +0000536
537 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
538 if (ret < 0)
539 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
540
541 return 0;
542}
543
544static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
545{
546 snd_pcm_lib_free_pages(substream);
547 return 0;
548}
549
550static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
551{
552 struct snd_soc_pcm_runtime *rtd = substream->private_data;
553 struct hsw_priv_data *pdata =
554 snd_soc_platform_get_drvdata(rtd->platform);
555 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
556 struct sst_hsw *hsw = pdata->hsw;
557
558 switch (cmd) {
559 case SNDRV_PCM_TRIGGER_START:
560 case SNDRV_PCM_TRIGGER_RESUME:
561 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
562 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
563 break;
564 case SNDRV_PCM_TRIGGER_STOP:
565 case SNDRV_PCM_TRIGGER_SUSPEND:
566 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
567 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
568 break;
569 default:
570 break;
571 }
572
573 return 0;
574}
575
576static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
577{
578 struct hsw_pcm_data *pcm_data = data;
579 struct snd_pcm_substream *substream = pcm_data->substream;
580 struct snd_pcm_runtime *runtime = substream->runtime;
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 u32 pos;
583
584 pos = frames_to_bytes(runtime,
585 (runtime->control->appl_ptr % runtime->buffer_size));
586
Liam Girdwood80462492014-10-16 15:29:17 +0100587 dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
Mark Browna4b12992014-03-12 23:04:35 +0000588
589 /* let alsa know we have play a period */
590 snd_pcm_period_elapsed(substream);
591 return pos;
592}
593
594static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
595{
596 struct snd_soc_pcm_runtime *rtd = substream->private_data;
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct hsw_priv_data *pdata =
599 snd_soc_platform_get_drvdata(rtd->platform);
600 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
601 struct sst_hsw *hsw = pdata->hsw;
602 snd_pcm_uframes_t offset;
Liam Girdwood51b4e242014-05-02 16:56:33 +0100603 uint64_t ppos;
604 u32 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
Mark Browna4b12992014-03-12 23:04:35 +0000605
Liam Girdwood51b4e242014-05-02 16:56:33 +0100606 offset = bytes_to_frames(runtime, position);
607 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
Mark Browna4b12992014-03-12 23:04:35 +0000608
Liam Girdwood80462492014-10-16 15:29:17 +0100609 dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
Liam Girdwood51b4e242014-05-02 16:56:33 +0100610 position, ppos);
Mark Browna4b12992014-03-12 23:04:35 +0000611 return offset;
612}
613
614static int hsw_pcm_open(struct snd_pcm_substream *substream)
615{
616 struct snd_soc_pcm_runtime *rtd = substream->private_data;
617 struct hsw_priv_data *pdata =
618 snd_soc_platform_get_drvdata(rtd->platform);
619 struct hsw_pcm_data *pcm_data;
620 struct sst_hsw *hsw = pdata->hsw;
621
622 pcm_data = &pdata->pcm[rtd->cpu_dai->id];
623
624 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000625 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000626
627 snd_soc_pcm_set_drvdata(rtd, pcm_data);
628 pcm_data->substream = substream;
629
630 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
631
632 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
633 hsw_notify_pointer, pcm_data);
634 if (pcm_data->stream == NULL) {
635 dev_err(rtd->dev, "error: failed to create stream\n");
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000636 pm_runtime_mark_last_busy(pdata->dev);
637 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000638 mutex_unlock(&pcm_data->mutex);
639 return -EINVAL;
640 }
641
Mark Browna4b12992014-03-12 23:04:35 +0000642 mutex_unlock(&pcm_data->mutex);
643 return 0;
644}
645
646static int hsw_pcm_close(struct snd_pcm_substream *substream)
647{
648 struct snd_soc_pcm_runtime *rtd = substream->private_data;
649 struct hsw_priv_data *pdata =
650 snd_soc_platform_get_drvdata(rtd->platform);
651 struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd);
652 struct sst_hsw *hsw = pdata->hsw;
653 int ret;
654
655 mutex_lock(&pcm_data->mutex);
656 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
657 if (ret < 0) {
658 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
659 goto out;
660 }
661
662 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
663 if (ret < 0) {
664 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
665 goto out;
666 }
Liam Girdwood916152c2014-05-02 16:56:32 +0100667 pcm_data->allocated = 0;
Mark Browna4b12992014-03-12 23:04:35 +0000668 pcm_data->stream = NULL;
669
670out:
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000671 pm_runtime_mark_last_busy(pdata->dev);
672 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000673 mutex_unlock(&pcm_data->mutex);
674 return ret;
675}
676
677static 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,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100685 .page = snd_pcm_sgbuf_ops_page,
Mark Browna4b12992014-03-12 23:04:35 +0000686};
687
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000688/* static mappings between PCMs and modules - may be dynamic in future */
689static struct hsw_pcm_module_map mod_map[] = {
Jie Yang98b9c1d2015-01-08 14:32:20 +0800690 {HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
691 {HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
692 {HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
693 {HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
694 {HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000695};
696
697static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
698{
699 struct sst_hsw *hsw = pdata->hsw;
700 struct hsw_pcm_data *pcm_data;
701 int i;
702
703 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
704 pcm_data = &pdata->pcm[i];
705
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000706 /* create new runtime module, use same offset if recreated */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000707 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
708 mod_map[i].mod_id, pcm_data->persistent_offset);
709 if (pcm_data->runtime == NULL)
710 goto err;
711 pcm_data->persistent_offset =
712 pcm_data->runtime->persistent_offset;
713 }
714
715 return 0;
716
717err:
718 for (--i; i >= 0; i--) {
719 pcm_data = &pdata->pcm[i];
720 sst_hsw_runtime_module_free(pcm_data->runtime);
721 }
722
723 return -ENODEV;
724}
725
726static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
727{
728 struct hsw_pcm_data *pcm_data;
729 int i;
730
731 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
732 pcm_data = &pdata->pcm[i];
733
734 sst_hsw_runtime_module_free(pcm_data->runtime);
735 }
736}
737
Mark Browna4b12992014-03-12 23:04:35 +0000738static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
739{
740 struct snd_pcm *pcm = rtd->pcm;
Liam Girdwood10df3502014-05-02 16:56:31 +0100741 struct snd_soc_platform *platform = rtd->platform;
742 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000743 struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
Liam Girdwood10df3502014-05-02 16:56:31 +0100744 struct device *dev = pdata->dma_dev;
Mark Browna4b12992014-03-12 23:04:35 +0000745 int ret = 0;
746
Mark Browna4b12992014-03-12 23:04:35 +0000747 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
748 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
749 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100750 SNDRV_DMA_TYPE_DEV_SG,
Liam Girdwood10df3502014-05-02 16:56:31 +0100751 dev,
Mark Browna4b12992014-03-12 23:04:35 +0000752 hsw_pcm_hardware.buffer_bytes_max,
753 hsw_pcm_hardware.buffer_bytes_max);
754 if (ret) {
755 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
756 ret);
757 return ret;
758 }
759 }
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000760 priv_data->pcm[rtd->cpu_dai->id].hsw_pcm = pcm;
Mark Browna4b12992014-03-12 23:04:35 +0000761
762 return ret;
763}
764
765#define HSW_FORMATS \
Liam Girdwood2ccf3bd2014-10-16 15:29:14 +0100766 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
Mark Browna4b12992014-03-12 23:04:35 +0000767
768static struct snd_soc_dai_driver hsw_dais[] = {
769 {
770 .name = "System Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000771 .id = HSW_PCM_DAI_ID_SYSTEM,
Mark Browna4b12992014-03-12 23:04:35 +0000772 .playback = {
773 .stream_name = "System Playback",
774 .channels_min = 2,
775 .channels_max = 2,
776 .rates = SNDRV_PCM_RATE_48000,
Jie Yang8e897612014-07-14 17:37:36 +0800777 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
Mark Browna4b12992014-03-12 23:04:35 +0000778 },
Jie Yang7bb73cb2014-11-28 21:52:11 +0800779 .capture = {
780 .stream_name = "Analog Capture",
781 .channels_min = 2,
782 .channels_max = 4,
783 .rates = SNDRV_PCM_RATE_48000,
784 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
785 },
Mark Browna4b12992014-03-12 23:04:35 +0000786 },
787 {
788 /* PCM */
789 .name = "Offload0 Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000790 .id = HSW_PCM_DAI_ID_OFFLOAD0,
Mark Browna4b12992014-03-12 23:04:35 +0000791 .playback = {
792 .stream_name = "Offload0 Playback",
793 .channels_min = 2,
794 .channels_max = 2,
795 .rates = SNDRV_PCM_RATE_8000_192000,
796 .formats = HSW_FORMATS,
797 },
798 },
799 {
800 /* PCM */
801 .name = "Offload1 Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000802 .id = HSW_PCM_DAI_ID_OFFLOAD1,
Mark Browna4b12992014-03-12 23:04:35 +0000803 .playback = {
804 .stream_name = "Offload1 Playback",
805 .channels_min = 2,
806 .channels_max = 2,
807 .rates = SNDRV_PCM_RATE_8000_192000,
808 .formats = HSW_FORMATS,
809 },
810 },
811 {
812 .name = "Loopback Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000813 .id = HSW_PCM_DAI_ID_LOOPBACK,
Mark Browna4b12992014-03-12 23:04:35 +0000814 .capture = {
815 .stream_name = "Loopback Capture",
816 .channels_min = 2,
817 .channels_max = 2,
Jie Yang8e897612014-07-14 17:37:36 +0800818 .rates = SNDRV_PCM_RATE_48000,
819 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
Mark Browna4b12992014-03-12 23:04:35 +0000820 },
821 },
Mark Browna4b12992014-03-12 23:04:35 +0000822};
823
824static const struct snd_soc_dapm_widget widgets[] = {
825
826 /* Backend DAIs */
827 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
828 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
829 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
830 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
831
832 /* Global Playback Mixer */
833 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
834};
835
836static const struct snd_soc_dapm_route graph[] = {
837
838 /* Playback Mixer */
839 {"Playback VMixer", NULL, "System Playback"},
840 {"Playback VMixer", NULL, "Offload0 Playback"},
841 {"Playback VMixer", NULL, "Offload1 Playback"},
842
843 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
844
845 {"Analog Capture", NULL, "SSP0 CODEC IN"},
846};
847
848static int hsw_pcm_probe(struct snd_soc_platform *platform)
849{
Lars-Peter Clausenbd033802014-08-20 13:08:47 +0200850 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000851 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000852 struct device *dma_dev, *dev;
Liam Girdwood0b708c82014-05-02 16:56:30 +0100853 int i, ret = 0;
Mark Browna4b12992014-03-12 23:04:35 +0000854
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000855 if (!pdata)
856 return -ENODEV;
857
858 dev = platform->dev;
859 dma_dev = pdata->dma_dev;
860
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000861 priv_data->hsw = pdata->dsp;
862 priv_data->dev = platform->dev;
863 priv_data->pm_state = HSW_PM_STATE_D0;
864 priv_data->soc_card = platform->component.card;
865
Mark Browna4b12992014-03-12 23:04:35 +0000866 /* allocate DSP buffer page tables */
867 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
868
869 mutex_init(&priv_data->pcm[i].mutex);
870
871 /* playback */
872 if (hsw_dais[i].playback.channels_min) {
Liam Girdwood0b708c82014-05-02 16:56:30 +0100873 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
874 PAGE_SIZE, &priv_data->dmab[i][0]);
875 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +0000876 goto err;
877 }
878
879 /* capture */
880 if (hsw_dais[i].capture.channels_min) {
Liam Girdwood0b708c82014-05-02 16:56:30 +0100881 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
882 PAGE_SIZE, &priv_data->dmab[i][1]);
883 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +0000884 goto err;
885 }
886 }
887
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000888 /* allocate runtime modules */
889 hsw_pcm_create_modules(priv_data);
890
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000891 /* enable runtime PM with auto suspend */
892 pm_runtime_set_autosuspend_delay(platform->dev,
893 SST_RUNTIME_SUSPEND_DELAY);
894 pm_runtime_use_autosuspend(platform->dev);
895 pm_runtime_enable(platform->dev);
896 pm_runtime_idle(platform->dev);
897
Mark Browna4b12992014-03-12 23:04:35 +0000898 return 0;
899
900err:
901 for (;i >= 0; i--) {
902 if (hsw_dais[i].playback.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +0100903 snd_dma_free_pages(&priv_data->dmab[i][0]);
Mark Browna4b12992014-03-12 23:04:35 +0000904 if (hsw_dais[i].capture.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +0100905 snd_dma_free_pages(&priv_data->dmab[i][1]);
Mark Browna4b12992014-03-12 23:04:35 +0000906 }
Liam Girdwood0b708c82014-05-02 16:56:30 +0100907 return ret;
Mark Browna4b12992014-03-12 23:04:35 +0000908}
909
910static int hsw_pcm_remove(struct snd_soc_platform *platform)
911{
912 struct hsw_priv_data *priv_data =
913 snd_soc_platform_get_drvdata(platform);
914 int i;
915
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000916 pm_runtime_disable(platform->dev);
917 hsw_pcm_free_modules(priv_data);
918
Mark Browna4b12992014-03-12 23:04:35 +0000919 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
920 if (hsw_dais[i].playback.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +0100921 snd_dma_free_pages(&priv_data->dmab[i][0]);
Mark Browna4b12992014-03-12 23:04:35 +0000922 if (hsw_dais[i].capture.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +0100923 snd_dma_free_pages(&priv_data->dmab[i][1]);
Mark Browna4b12992014-03-12 23:04:35 +0000924 }
925
926 return 0;
927}
928
929static struct snd_soc_platform_driver hsw_soc_platform = {
930 .probe = hsw_pcm_probe,
931 .remove = hsw_pcm_remove,
932 .ops = &hsw_pcm_ops,
933 .pcm_new = hsw_pcm_new,
Mark Browna4b12992014-03-12 23:04:35 +0000934};
935
936static const struct snd_soc_component_driver hsw_dai_component = {
Lars-Peter Clausen923976a2014-08-20 13:08:48 +0200937 .name = "haswell-dai",
938 .controls = hsw_volume_controls,
939 .num_controls = ARRAY_SIZE(hsw_volume_controls),
940 .dapm_widgets = widgets,
941 .num_dapm_widgets = ARRAY_SIZE(widgets),
942 .dapm_routes = graph,
943 .num_dapm_routes = ARRAY_SIZE(graph),
Mark Browna4b12992014-03-12 23:04:35 +0000944};
945
946static int hsw_pcm_dev_probe(struct platform_device *pdev)
947{
948 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
Lars-Peter Clausenbd033802014-08-20 13:08:47 +0200949 struct hsw_priv_data *priv_data;
Mark Browna4b12992014-03-12 23:04:35 +0000950 int ret;
951
Lars-Peter Clausenbd033802014-08-20 13:08:47 +0200952 if (!sst_pdata)
953 return -EINVAL;
954
955 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
956 if (!priv_data)
957 return -ENOMEM;
958
Mark Browna4b12992014-03-12 23:04:35 +0000959 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
960 if (ret < 0)
961 return -ENODEV;
962
Lars-Peter Clausenbd033802014-08-20 13:08:47 +0200963 priv_data->hsw = sst_pdata->dsp;
964 platform_set_drvdata(pdev, priv_data);
965
Mark Browna4b12992014-03-12 23:04:35 +0000966 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
967 if (ret < 0)
968 goto err_plat;
969
970 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
971 hsw_dais, ARRAY_SIZE(hsw_dais));
972 if (ret < 0)
973 goto err_comp;
974
975 return 0;
976
977err_comp:
978 snd_soc_unregister_platform(&pdev->dev);
979err_plat:
980 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
981 return 0;
982}
983
984static int hsw_pcm_dev_remove(struct platform_device *pdev)
985{
986 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
987
988 snd_soc_unregister_platform(&pdev->dev);
989 snd_soc_unregister_component(&pdev->dev);
990 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
991
992 return 0;
993}
994
Rafael J. Wysocki45544c82014-12-19 15:26:46 +0100995#ifdef CONFIG_PM
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000996
997static int hsw_pcm_runtime_idle(struct device *dev)
998{
999 return 0;
1000}
1001
1002static int hsw_pcm_runtime_suspend(struct device *dev)
1003{
1004 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1005 struct sst_hsw *hsw = pdata->hsw;
1006
1007 if (pdata->pm_state == HSW_PM_STATE_D3)
1008 return 0;
1009
1010 sst_hsw_dsp_runtime_suspend(hsw);
1011 sst_hsw_dsp_runtime_sleep(hsw);
1012 pdata->pm_state = HSW_PM_STATE_D3;
1013
1014 return 0;
1015}
1016
1017static int hsw_pcm_runtime_resume(struct device *dev)
1018{
1019 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1020 struct sst_hsw *hsw = pdata->hsw;
1021 int ret;
1022
1023 if (pdata->pm_state == HSW_PM_STATE_D0)
1024 return 0;
1025
1026 ret = sst_hsw_dsp_load(hsw);
1027 if (ret < 0) {
1028 dev_err(dev, "failed to reload %d\n", ret);
1029 return ret;
1030 }
1031
1032 ret = hsw_pcm_create_modules(pdata);
1033 if (ret < 0) {
1034 dev_err(dev, "failed to create modules %d\n", ret);
1035 return ret;
1036 }
1037
1038 ret = sst_hsw_dsp_runtime_resume(hsw);
1039 if (ret < 0)
1040 return ret;
1041 else if (ret == 1) /* no action required */
1042 return 0;
1043
1044 pdata->pm_state = HSW_PM_STATE_D0;
1045 return ret;
1046}
1047
Liam Girdwood35e03a82014-10-30 14:58:19 +00001048#else
1049#define hsw_pcm_runtime_idle NULL
1050#define hsw_pcm_runtime_suspend NULL
1051#define hsw_pcm_runtime_resume NULL
1052#endif
1053
Rafael J. Wysocki45544c82014-12-19 15:26:46 +01001054#ifdef CONFIG_PM
Liam Girdwood7b8ef672014-11-04 13:27:45 +00001055
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001056static void hsw_pcm_complete(struct device *dev)
1057{
1058 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1059 struct sst_hsw *hsw = pdata->hsw;
1060 struct hsw_pcm_data *pcm_data;
1061 int i, err;
1062
1063 if (pdata->pm_state == HSW_PM_STATE_D0)
1064 return;
1065
1066 err = sst_hsw_dsp_load(hsw);
1067 if (err < 0) {
1068 dev_err(dev, "failed to reload %d\n", err);
1069 return;
1070 }
1071
1072 err = hsw_pcm_create_modules(pdata);
1073 if (err < 0) {
1074 dev_err(dev, "failed to create modules %d\n", err);
1075 return;
1076 }
1077
Jie Yang98b9c1d2015-01-08 14:32:20 +08001078 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001079 pcm_data = &pdata->pcm[i];
1080
1081 if (!pcm_data->substream)
1082 continue;
1083
1084 err = sst_module_runtime_restore(pcm_data->runtime,
1085 &pcm_data->context);
1086 if (err < 0)
1087 dev_err(dev, "failed to restore context for PCM %d\n", i);
1088 }
1089
1090 snd_soc_resume(pdata->soc_card->dev);
1091
1092 err = sst_hsw_dsp_runtime_resume(hsw);
1093 if (err < 0)
1094 return;
1095 else if (err == 1) /* no action required */
1096 return;
1097
1098 pdata->pm_state = HSW_PM_STATE_D0;
1099 return;
1100}
1101
1102static int hsw_pcm_prepare(struct device *dev)
1103{
1104 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1105 struct sst_hsw *hsw = pdata->hsw;
1106 struct hsw_pcm_data *pcm_data;
1107 int i, err;
1108
1109 if (pdata->pm_state == HSW_PM_STATE_D3)
1110 return 0;
1111 /* suspend all active streams */
Jie Yang98b9c1d2015-01-08 14:32:20 +08001112 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001113 pcm_data = &pdata->pcm[i];
1114
1115 if (!pcm_data->substream)
1116 continue;
1117 dev_dbg(dev, "suspending pcm %d\n", i);
1118 snd_pcm_suspend_all(pcm_data->hsw_pcm);
1119
1120 /* We need to wait until the DSP FW stops the streams */
1121 msleep(2);
1122 }
1123
1124 snd_soc_suspend(pdata->soc_card->dev);
1125 snd_soc_poweroff(pdata->soc_card->dev);
1126
1127 /* enter D3 state and stall */
1128 sst_hsw_dsp_runtime_suspend(hsw);
1129
1130 /* preserve persistent memory */
Jie Yang98b9c1d2015-01-08 14:32:20 +08001131 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001132 pcm_data = &pdata->pcm[i];
1133
1134 if (!pcm_data->substream)
1135 continue;
1136
1137 dev_dbg(dev, "saving context pcm %d\n", i);
1138 err = sst_module_runtime_save(pcm_data->runtime,
1139 &pcm_data->context);
1140 if (err < 0)
1141 dev_err(dev, "failed to save context for PCM %d\n", i);
1142 }
1143
1144 /* put the DSP to sleep */
1145 sst_hsw_dsp_runtime_sleep(hsw);
1146 pdata->pm_state = HSW_PM_STATE_D3;
1147
1148 return 0;
1149}
1150
Liam Girdwood7b8ef672014-11-04 13:27:45 +00001151#else
1152#define hsw_pcm_prepare NULL
1153#define hsw_pcm_complete NULL
1154#endif
1155
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001156static const struct dev_pm_ops hsw_pcm_pm = {
1157 .runtime_idle = hsw_pcm_runtime_idle,
1158 .runtime_suspend = hsw_pcm_runtime_suspend,
1159 .runtime_resume = hsw_pcm_runtime_resume,
1160 .prepare = hsw_pcm_prepare,
1161 .complete = hsw_pcm_complete,
1162};
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001163
Mark Browna4b12992014-03-12 23:04:35 +00001164static struct platform_driver hsw_pcm_driver = {
1165 .driver = {
1166 .name = "haswell-pcm-audio",
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001167 .pm = &hsw_pcm_pm,
Mark Browna4b12992014-03-12 23:04:35 +00001168 },
1169
1170 .probe = hsw_pcm_dev_probe,
1171 .remove = hsw_pcm_dev_remove,
1172};
1173module_platform_driver(hsw_pcm_driver);
1174
1175MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1176MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1177MODULE_LICENSE("GPL v2");
1178MODULE_ALIAS("platform:haswell-pcm-audio");