blob: 6c6229ae4a0259a3f16969708254445bb87d45a2 [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
Libin Yang1b006992015-02-10 10:02:47 +080039#define SST_OLD_POSITION(d, r, o) ((d) + \
40 frames_to_bytes(r, o))
41#define SST_SAMPLES(r, x) (bytes_to_samples(r, \
42 frames_to_bytes(r, (x))))
43
Mark Browna4b12992014-03-12 23:04:35 +000044/* simple volume table */
45static const u32 volume_map[] = {
46 HSW_VOLUME_MAX >> 30,
47 HSW_VOLUME_MAX >> 29,
48 HSW_VOLUME_MAX >> 28,
49 HSW_VOLUME_MAX >> 27,
50 HSW_VOLUME_MAX >> 26,
51 HSW_VOLUME_MAX >> 25,
52 HSW_VOLUME_MAX >> 24,
53 HSW_VOLUME_MAX >> 23,
54 HSW_VOLUME_MAX >> 22,
55 HSW_VOLUME_MAX >> 21,
56 HSW_VOLUME_MAX >> 20,
57 HSW_VOLUME_MAX >> 19,
58 HSW_VOLUME_MAX >> 18,
59 HSW_VOLUME_MAX >> 17,
60 HSW_VOLUME_MAX >> 16,
61 HSW_VOLUME_MAX >> 15,
62 HSW_VOLUME_MAX >> 14,
63 HSW_VOLUME_MAX >> 13,
64 HSW_VOLUME_MAX >> 12,
65 HSW_VOLUME_MAX >> 11,
66 HSW_VOLUME_MAX >> 10,
67 HSW_VOLUME_MAX >> 9,
68 HSW_VOLUME_MAX >> 8,
69 HSW_VOLUME_MAX >> 7,
70 HSW_VOLUME_MAX >> 6,
71 HSW_VOLUME_MAX >> 5,
72 HSW_VOLUME_MAX >> 4,
73 HSW_VOLUME_MAX >> 3,
74 HSW_VOLUME_MAX >> 2,
75 HSW_VOLUME_MAX >> 1,
76 HSW_VOLUME_MAX >> 0,
77};
78
79#define HSW_PCM_PERIODS_MAX 64
80#define HSW_PCM_PERIODS_MIN 2
81
Liam Girdwood2e4f75912014-10-29 17:40:43 +000082#define HSW_PCM_DAI_ID_SYSTEM 0
83#define HSW_PCM_DAI_ID_OFFLOAD0 1
84#define HSW_PCM_DAI_ID_OFFLOAD1 2
85#define HSW_PCM_DAI_ID_LOOPBACK 3
Liam Girdwood2e4f75912014-10-29 17:40:43 +000086
87
Mark Browna4b12992014-03-12 23:04:35 +000088static const struct snd_pcm_hardware hsw_pcm_hardware = {
89 .info = SNDRV_PCM_INFO_MMAP |
90 SNDRV_PCM_INFO_MMAP_VALID |
91 SNDRV_PCM_INFO_INTERLEAVED |
92 SNDRV_PCM_INFO_PAUSE |
93 SNDRV_PCM_INFO_RESUME |
Libin Yang9fd37812015-02-17 12:28:23 +080094 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
95 SNDRV_PCM_INFO_DRAIN_TRIGGER,
Jie Yang8e897612014-07-14 17:37:36 +080096 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
Mark Browna4b12992014-03-12 23:04:35 +000097 SNDRV_PCM_FMTBIT_S32_LE,
98 .period_bytes_min = PAGE_SIZE,
99 .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE,
100 .periods_min = HSW_PCM_PERIODS_MIN,
101 .periods_max = HSW_PCM_PERIODS_MAX,
102 .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE,
103};
104
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000105struct hsw_pcm_module_map {
106 int dai_id;
Jie Yang98b9c1d2015-01-08 14:32:20 +0800107 int stream;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000108 enum sst_hsw_module_id mod_id;
109};
110
Mark Browna4b12992014-03-12 23:04:35 +0000111/* private data for each PCM DSP stream */
112struct hsw_pcm_data {
113 int dai_id;
114 struct sst_hsw_stream *stream;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000115 struct sst_module_runtime *runtime;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000116 struct sst_module_runtime_context context;
117 struct snd_pcm *hsw_pcm;
Mark Browna4b12992014-03-12 23:04:35 +0000118 u32 volume[2];
119 struct snd_pcm_substream *substream;
120 struct snd_compr_stream *cstream;
121 unsigned int wpos;
122 struct mutex mutex;
Liam Girdwood916152c2014-05-02 16:56:32 +0100123 bool allocated;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000124 int persistent_offset;
Mark Browna4b12992014-03-12 23:04:35 +0000125};
126
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000127enum hsw_pm_state {
Jie Yangcd311dd2015-02-05 22:56:48 +0800128 HSW_PM_STATE_D0 = 0,
129 HSW_PM_STATE_RTD3 = 1,
130 HSW_PM_STATE_D3 = 2,
Mark Browna4b12992014-03-12 23:04:35 +0000131};
132
133/* private data for the driver */
134struct hsw_priv_data {
135 /* runtime DSP */
136 struct sst_hsw *hsw;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000137 struct device *dev;
138 enum hsw_pm_state pm_state;
139 struct snd_soc_card *soc_card;
Mark Browna4b12992014-03-12 23:04:35 +0000140
141 /* page tables */
Liam Girdwood0b708c82014-05-02 16:56:30 +0100142 struct snd_dma_buffer dmab[HSW_PCM_COUNT][2];
Mark Browna4b12992014-03-12 23:04:35 +0000143
144 /* DAI data */
Jie Yang7ff9d672015-01-09 14:36:36 +0800145 struct hsw_pcm_data pcm[HSW_PCM_COUNT][2];
146};
147
148
149/* static mappings between PCMs and modules - may be dynamic in future */
150static struct hsw_pcm_module_map mod_map[] = {
151 {HSW_PCM_DAI_ID_SYSTEM, 0, SST_HSW_MODULE_PCM_SYSTEM},
152 {HSW_PCM_DAI_ID_OFFLOAD0, 0, SST_HSW_MODULE_PCM},
153 {HSW_PCM_DAI_ID_OFFLOAD1, 0, SST_HSW_MODULE_PCM},
154 {HSW_PCM_DAI_ID_LOOPBACK, 1, SST_HSW_MODULE_PCM_REFERENCE},
155 {HSW_PCM_DAI_ID_SYSTEM, 1, SST_HSW_MODULE_PCM_CAPTURE},
Mark Browna4b12992014-03-12 23:04:35 +0000156};
157
Liam Girdwood916152c2014-05-02 16:56:32 +0100158static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data);
159
Mark Browna4b12992014-03-12 23:04:35 +0000160static inline u32 hsw_mixer_to_ipc(unsigned int value)
161{
162 if (value >= ARRAY_SIZE(volume_map))
163 return volume_map[0];
164 else
165 return volume_map[value];
166}
167
168static inline unsigned int hsw_ipc_to_mixer(u32 value)
169{
170 int i;
171
172 for (i = 0; i < ARRAY_SIZE(volume_map); i++) {
173 if (volume_map[i] >= value)
174 return i;
175 }
176
177 return i - 1;
178}
179
180static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
182{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000183 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
Mark Browna4b12992014-03-12 23:04:35 +0000184 struct soc_mixer_control *mc =
185 (struct soc_mixer_control *)kcontrol->private_value;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000186 struct hsw_priv_data *pdata =
187 snd_soc_platform_get_drvdata(platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800188 struct hsw_pcm_data *pcm_data;
Mark Browna4b12992014-03-12 23:04:35 +0000189 struct sst_hsw *hsw = pdata->hsw;
190 u32 volume;
Jie Yang7ff9d672015-01-09 14:36:36 +0800191 int dai, stream;
192
193 dai = mod_map[mc->reg].dai_id;
194 stream = mod_map[mc->reg].stream;
195 pcm_data = &pdata->pcm[dai][stream];
Mark Browna4b12992014-03-12 23:04:35 +0000196
197 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000198 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000199
200 if (!pcm_data->stream) {
201 pcm_data->volume[0] =
202 hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
203 pcm_data->volume[1] =
204 hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000205 pm_runtime_mark_last_busy(pdata->dev);
206 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000207 mutex_unlock(&pcm_data->mutex);
208 return 0;
209 }
210
211 if (ucontrol->value.integer.value[0] ==
212 ucontrol->value.integer.value[1]) {
213 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
Jie Yangf1e59822014-11-25 21:00:53 +0800214 /* apply volume value to all channels */
215 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, SST_HSW_CHANNELS_ALL, volume);
Mark Browna4b12992014-03-12 23:04:35 +0000216 } else {
217 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
218 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume);
219 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
220 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume);
221 }
222
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000223 pm_runtime_mark_last_busy(pdata->dev);
224 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000225 mutex_unlock(&pcm_data->mutex);
226 return 0;
227}
228
229static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
231{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000232 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
Mark Browna4b12992014-03-12 23:04:35 +0000233 struct soc_mixer_control *mc =
234 (struct soc_mixer_control *)kcontrol->private_value;
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000235 struct hsw_priv_data *pdata =
236 snd_soc_platform_get_drvdata(platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800237 struct hsw_pcm_data *pcm_data;
Mark Browna4b12992014-03-12 23:04:35 +0000238 struct sst_hsw *hsw = pdata->hsw;
239 u32 volume;
Jie Yang7ff9d672015-01-09 14:36:36 +0800240 int dai, stream;
241
242 dai = mod_map[mc->reg].dai_id;
243 stream = mod_map[mc->reg].stream;
244 pcm_data = &pdata->pcm[dai][stream];
Mark Browna4b12992014-03-12 23:04:35 +0000245
246 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000247 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000248
249 if (!pcm_data->stream) {
250 ucontrol->value.integer.value[0] =
251 hsw_ipc_to_mixer(pcm_data->volume[0]);
252 ucontrol->value.integer.value[1] =
253 hsw_ipc_to_mixer(pcm_data->volume[1]);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000254 pm_runtime_mark_last_busy(pdata->dev);
255 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000256 mutex_unlock(&pcm_data->mutex);
257 return 0;
258 }
259
260 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume);
261 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
262 sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume);
263 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000264
265 pm_runtime_mark_last_busy(pdata->dev);
266 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000267 mutex_unlock(&pcm_data->mutex);
268
269 return 0;
270}
271
272static int hsw_volume_put(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
274{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000275 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
276 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000277 struct sst_hsw *hsw = pdata->hsw;
278 u32 volume;
279
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000280 pm_runtime_get_sync(pdata->dev);
281
Mark Browna4b12992014-03-12 23:04:35 +0000282 if (ucontrol->value.integer.value[0] ==
283 ucontrol->value.integer.value[1]) {
284
285 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
Jie Yangf1e59822014-11-25 21:00:53 +0800286 sst_hsw_mixer_set_volume(hsw, 0, SST_HSW_CHANNELS_ALL, volume);
Mark Browna4b12992014-03-12 23:04:35 +0000287
288 } else {
289 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]);
290 sst_hsw_mixer_set_volume(hsw, 0, 0, volume);
291
292 volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]);
293 sst_hsw_mixer_set_volume(hsw, 0, 1, volume);
294 }
295
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000296 pm_runtime_mark_last_busy(pdata->dev);
297 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000298 return 0;
299}
300
301static int hsw_volume_get(struct snd_kcontrol *kcontrol,
302 struct snd_ctl_elem_value *ucontrol)
303{
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000304 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
305 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +0000306 struct sst_hsw *hsw = pdata->hsw;
307 unsigned int volume = 0;
308
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000309 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000310 sst_hsw_mixer_get_volume(hsw, 0, 0, &volume);
311 ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume);
312
313 sst_hsw_mixer_get_volume(hsw, 0, 1, &volume);
314 ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume);
315
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000316 pm_runtime_mark_last_busy(pdata->dev);
317 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000318 return 0;
319}
320
Lu, Han76c07b82015-03-12 13:53:00 +0800321static int hsw_waves_switch_get(struct snd_kcontrol *kcontrol,
322 struct snd_ctl_elem_value *ucontrol)
323{
324 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
325 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
326 struct sst_hsw *hsw = pdata->hsw;
327 enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
328
329 ucontrol->value.integer.value[0] =
330 (sst_hsw_is_module_active(hsw, id) ||
331 sst_hsw_is_module_enabled_rtd3(hsw, id));
332 return 0;
333}
334
335static int hsw_waves_switch_put(struct snd_kcontrol *kcontrol,
336 struct snd_ctl_elem_value *ucontrol)
337{
338 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
339 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
340 struct sst_hsw *hsw = pdata->hsw;
341 int ret = 0;
342 enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
343 bool switch_on = (bool)ucontrol->value.integer.value[0];
344
345 /* if module is in RAM on the DSP, apply user settings to module through
346 * ipc. If module is not in RAM on the DSP, store user setting for
347 * track */
348 if (sst_hsw_is_module_loaded(hsw, id)) {
349 if (switch_on == sst_hsw_is_module_active(hsw, id))
350 return 0;
351
352 if (switch_on)
353 ret = sst_hsw_module_enable(hsw, id, 0);
354 else
355 ret = sst_hsw_module_disable(hsw, id, 0);
356 } else {
357 if (switch_on == sst_hsw_is_module_enabled_rtd3(hsw, id))
358 return 0;
359
360 if (switch_on)
361 sst_hsw_set_module_enabled_rtd3(hsw, id);
362 else
363 sst_hsw_set_module_disabled_rtd3(hsw, id);
364 }
365
366 return ret;
367}
368
Lu, Han3814c202015-03-12 13:53:02 +0800369static int hsw_waves_param_get(struct snd_kcontrol *kcontrol,
370 struct snd_ctl_elem_value *ucontrol)
371{
372 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
373 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
374 struct sst_hsw *hsw = pdata->hsw;
375
376 /* return a matching line from param buffer */
377 return sst_hsw_load_param_line(hsw, ucontrol->value.bytes.data);
378}
379
380static int hsw_waves_param_put(struct snd_kcontrol *kcontrol,
381 struct snd_ctl_elem_value *ucontrol)
382{
383 struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol);
384 struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform);
385 struct sst_hsw *hsw = pdata->hsw;
386 int ret;
387 enum sst_hsw_module_id id = SST_HSW_MODULE_WAVES;
388 int param_id = ucontrol->value.bytes.data[0];
389 int param_size = WAVES_PARAM_COUNT;
390
391 /* clear param buffer and reset buffer index */
392 if (param_id == 0xFF) {
393 sst_hsw_reset_param_buf(hsw);
394 return 0;
395 }
396
397 /* store params into buffer */
398 ret = sst_hsw_store_param_line(hsw, ucontrol->value.bytes.data);
399 if (ret < 0)
400 return ret;
401
Lu, Han5d5b2752015-03-19 08:38:00 +0800402 if (sst_hsw_is_module_active(hsw, id))
Lu, Han3814c202015-03-12 13:53:02 +0800403 ret = sst_hsw_module_set_param(hsw, id, 0, param_id,
404 param_size, ucontrol->value.bytes.data);
Lu, Han3814c202015-03-12 13:53:02 +0800405 return ret;
406}
407
Mark Browna4b12992014-03-12 23:04:35 +0000408/* TLV used by both global and stream volumes */
409static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1);
410
411/* System Pin has no volume control */
412static const struct snd_kcontrol_new hsw_volume_controls[] = {
413 /* Global DSP volume */
414 SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800415 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000416 hsw_volume_get, hsw_volume_put, hsw_vol_tlv),
417 /* Offload 0 volume */
418 SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800419 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000420 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
421 /* Offload 1 volume */
422 SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800423 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000424 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
425 /* Mic Capture volume */
Jie Yang7ff9d672015-01-09 14:36:36 +0800426 SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8,
Jie Yangc5f04062014-11-28 10:41:28 +0800427 ARRAY_SIZE(volume_map) - 1, 0,
Mark Browna4b12992014-03-12 23:04:35 +0000428 hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv),
Lu, Han76c07b82015-03-12 13:53:00 +0800429 /* enable/disable module waves */
430 SOC_SINGLE_BOOL_EXT("Waves Switch", 0,
431 hsw_waves_switch_get, hsw_waves_switch_put),
Lu, Han3814c202015-03-12 13:53:02 +0800432 /* set parameters to module waves */
433 SND_SOC_BYTES_EXT("Waves Set Param", WAVES_PARAM_COUNT,
434 hsw_waves_param_get, hsw_waves_param_put),
Mark Browna4b12992014-03-12 23:04:35 +0000435};
436
437/* Create DMA buffer page table for DSP */
Liam Girdwood0b708c82014-05-02 16:56:30 +0100438static int create_adsp_page_table(struct snd_pcm_substream *substream,
439 struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd,
440 unsigned char *dma_area, size_t size, int pcm)
Mark Browna4b12992014-03-12 23:04:35 +0000441{
Liam Girdwood0b708c82014-05-02 16:56:30 +0100442 struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
443 int i, pages, stream = substream->stream;
Mark Browna4b12992014-03-12 23:04:35 +0000444
Liam Girdwood0b708c82014-05-02 16:56:30 +0100445 pages = snd_sgbuf_aligned_pages(size);
Mark Browna4b12992014-03-12 23:04:35 +0000446
447 dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n",
448 dma_area, size, pages);
449
450 for (i = 0; i < pages; i++) {
451 u32 idx = (((i << 2) + i)) >> 1;
Liam Girdwood0b708c82014-05-02 16:56:30 +0100452 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
Mark Browna4b12992014-03-12 23:04:35 +0000453 u32 *pg_table;
454
455 dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
456
Liam Girdwood0b708c82014-05-02 16:56:30 +0100457 pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx);
Mark Browna4b12992014-03-12 23:04:35 +0000458
459 if (i & 1)
460 *pg_table |= (pfn << 4);
461 else
462 *pg_table |= pfn;
463 }
464
465 return 0;
466}
467
468/* this may get called several times by oss emulation */
469static int hsw_pcm_hw_params(struct snd_pcm_substream *substream,
470 struct snd_pcm_hw_params *params)
471{
472 struct snd_soc_pcm_runtime *rtd = substream->private_data;
473 struct snd_pcm_runtime *runtime = substream->runtime;
474 struct hsw_priv_data *pdata =
475 snd_soc_platform_get_drvdata(rtd->platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800476 struct hsw_pcm_data *pcm_data;
Mark Browna4b12992014-03-12 23:04:35 +0000477 struct sst_hsw *hsw = pdata->hsw;
478 struct sst_module *module_data;
479 struct sst_dsp *dsp;
Liam Girdwood0b708c82014-05-02 16:56:30 +0100480 struct snd_dma_buffer *dmab;
Mark Browna4b12992014-03-12 23:04:35 +0000481 enum sst_hsw_stream_type stream_type;
482 enum sst_hsw_stream_path_id path_id;
483 u32 rate, bits, map, pages, module_id;
484 u8 channels;
Jie Yang7ff9d672015-01-09 14:36:36 +0800485 int ret, dai;
486
487 dai = mod_map[rtd->cpu_dai->id].dai_id;
488 pcm_data = &pdata->pcm[dai][substream->stream];
Mark Browna4b12992014-03-12 23:04:35 +0000489
Liam Girdwood916152c2014-05-02 16:56:32 +0100490 /* check if we are being called a subsequent time */
491 if (pcm_data->allocated) {
492 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
493 if (ret < 0)
494 dev_dbg(rtd->dev, "error: reset stream failed %d\n",
495 ret);
496
497 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
498 if (ret < 0) {
499 dev_dbg(rtd->dev, "error: free stream failed %d\n",
500 ret);
501 return ret;
502 }
503 pcm_data->allocated = false;
504
505 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
506 hsw_notify_pointer, pcm_data);
507 if (pcm_data->stream == NULL) {
508 dev_err(rtd->dev, "error: failed to create stream\n");
509 return -EINVAL;
510 }
511 }
512
Mark Browna4b12992014-03-12 23:04:35 +0000513 /* stream direction */
514 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
515 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
516 else
517 path_id = SST_HSW_STREAM_PATH_SSP0_IN;
518
519 /* DSP stream type depends on DAI ID */
520 switch (rtd->cpu_dai->id) {
521 case 0:
Jie Yang7bb73cb2014-11-28 21:52:11 +0800522 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
523 stream_type = SST_HSW_STREAM_TYPE_SYSTEM;
524 module_id = SST_HSW_MODULE_PCM_SYSTEM;
525 }
526 else {
527 stream_type = SST_HSW_STREAM_TYPE_CAPTURE;
528 module_id = SST_HSW_MODULE_PCM_CAPTURE;
529 }
Mark Browna4b12992014-03-12 23:04:35 +0000530 break;
531 case 1:
532 case 2:
533 stream_type = SST_HSW_STREAM_TYPE_RENDER;
534 module_id = SST_HSW_MODULE_PCM;
535 break;
536 case 3:
537 /* path ID needs to be OUT for loopback */
538 stream_type = SST_HSW_STREAM_TYPE_LOOPBACK;
539 path_id = SST_HSW_STREAM_PATH_SSP0_OUT;
540 module_id = SST_HSW_MODULE_PCM_REFERENCE;
541 break;
Mark Browna4b12992014-03-12 23:04:35 +0000542 default:
543 dev_err(rtd->dev, "error: invalid DAI ID %d\n",
544 rtd->cpu_dai->id);
545 return -EINVAL;
546 };
547
548 ret = sst_hsw_stream_format(hsw, pcm_data->stream,
549 path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT);
550 if (ret < 0) {
551 dev_err(rtd->dev, "error: failed to set format %d\n", ret);
552 return ret;
553 }
554
555 rate = params_rate(params);
556 ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate);
557 if (ret < 0) {
558 dev_err(rtd->dev, "error: could not set rate %d\n", rate);
559 return ret;
560 }
561
562 switch (params_format(params)) {
563 case SNDRV_PCM_FORMAT_S16_LE:
564 bits = SST_HSW_DEPTH_16BIT;
565 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16);
566 break;
567 case SNDRV_PCM_FORMAT_S24_LE:
Jie Yang8e897612014-07-14 17:37:36 +0800568 bits = SST_HSW_DEPTH_32BIT;
569 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24);
570 break;
571 case SNDRV_PCM_FORMAT_S8:
572 bits = SST_HSW_DEPTH_8BIT;
573 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8);
574 break;
575 case SNDRV_PCM_FORMAT_S32_LE:
576 bits = SST_HSW_DEPTH_32BIT;
Mark Browna4b12992014-03-12 23:04:35 +0000577 sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32);
578 break;
579 default:
580 dev_err(rtd->dev, "error: invalid format %d\n",
581 params_format(params));
582 return -EINVAL;
583 }
584
585 ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits);
586 if (ret < 0) {
587 dev_err(rtd->dev, "error: could not set bits %d\n", bits);
588 return ret;
589 }
590
Mark Browna4b12992014-03-12 23:04:35 +0000591 channels = params_channels(params);
Mark Browna4b12992014-03-12 23:04:35 +0000592 map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO);
593 sst_hsw_stream_set_map_config(hsw, pcm_data->stream,
594 map, SST_HSW_CHANNEL_CONFIG_STEREO);
595
596 ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels);
597 if (ret < 0) {
598 dev_err(rtd->dev, "error: could not set channels %d\n",
599 channels);
600 return ret;
601 }
602
603 ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
604 if (ret < 0) {
605 dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n",
606 params_buffer_bytes(params), ret);
607 return ret;
608 }
609
Liam Girdwood0b708c82014-05-02 16:56:30 +0100610 dmab = snd_pcm_get_dma_buf(substream);
611
612 ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area,
613 runtime->dma_bytes, rtd->cpu_dai->id);
Mark Browna4b12992014-03-12 23:04:35 +0000614 if (ret < 0)
615 return ret;
616
617 sst_hsw_stream_set_style(hsw, pcm_data->stream,
618 SST_HSW_INTERLEAVING_PER_CHANNEL);
619
620 if (runtime->dma_bytes % PAGE_SIZE)
621 pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
622 else
623 pages = runtime->dma_bytes / PAGE_SIZE;
624
625 ret = sst_hsw_stream_buffer(hsw, pcm_data->stream,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100626 pdata->dmab[rtd->cpu_dai->id][substream->stream].addr,
Mark Browna4b12992014-03-12 23:04:35 +0000627 pages, runtime->dma_bytes, 0,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100628 snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT);
Mark Browna4b12992014-03-12 23:04:35 +0000629 if (ret < 0) {
630 dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret);
631 return ret;
632 }
633
634 dsp = sst_hsw_get_dsp(hsw);
635
636 module_data = sst_module_get_from_id(dsp, module_id);
637 if (module_data == NULL) {
638 dev_err(rtd->dev, "error: failed to get module config\n");
639 return -EINVAL;
640 }
641
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000642 sst_hsw_stream_set_module_info(hsw, pcm_data->stream,
643 pcm_data->runtime);
Mark Browna4b12992014-03-12 23:04:35 +0000644
645 ret = sst_hsw_stream_commit(hsw, pcm_data->stream);
646 if (ret < 0) {
647 dev_err(rtd->dev, "error: failed to commit stream %d\n", ret);
648 return ret;
649 }
Jie Yangf1e59822014-11-25 21:00:53 +0800650
651 if (!pcm_data->allocated) {
652 /* Set previous saved volume */
653 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
654 0, pcm_data->volume[0]);
655 sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0,
656 1, pcm_data->volume[1]);
657 pcm_data->allocated = true;
658 }
Mark Browna4b12992014-03-12 23:04:35 +0000659
660 ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1);
661 if (ret < 0)
662 dev_err(rtd->dev, "error: failed to pause %d\n", ret);
663
664 return 0;
665}
666
667static int hsw_pcm_hw_free(struct snd_pcm_substream *substream)
668{
669 snd_pcm_lib_free_pages(substream);
670 return 0;
671}
672
673static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
674{
675 struct snd_soc_pcm_runtime *rtd = substream->private_data;
676 struct hsw_priv_data *pdata =
677 snd_soc_platform_get_drvdata(rtd->platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800678 struct hsw_pcm_data *pcm_data;
Libin Yang1b006992015-02-10 10:02:47 +0800679 struct sst_hsw_stream *sst_stream;
Mark Browna4b12992014-03-12 23:04:35 +0000680 struct sst_hsw *hsw = pdata->hsw;
Libin Yang1b006992015-02-10 10:02:47 +0800681 struct snd_pcm_runtime *runtime = substream->runtime;
682 snd_pcm_uframes_t pos;
Jie Yang7ff9d672015-01-09 14:36:36 +0800683 int dai;
684
685 dai = mod_map[rtd->cpu_dai->id].dai_id;
686 pcm_data = &pdata->pcm[dai][substream->stream];
Libin Yang1b006992015-02-10 10:02:47 +0800687 sst_stream = pcm_data->stream;
Mark Browna4b12992014-03-12 23:04:35 +0000688
689 switch (cmd) {
690 case SNDRV_PCM_TRIGGER_START:
691 case SNDRV_PCM_TRIGGER_RESUME:
692 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Libin Yang1b006992015-02-10 10:02:47 +0800693 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
Mark Browna4b12992014-03-12 23:04:35 +0000694 sst_hsw_stream_resume(hsw, pcm_data->stream, 0);
695 break;
696 case SNDRV_PCM_TRIGGER_STOP:
697 case SNDRV_PCM_TRIGGER_SUSPEND:
698 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Libin Yang1b006992015-02-10 10:02:47 +0800699 sst_hsw_stream_set_silence_start(hsw, sst_stream, false);
Mark Browna4b12992014-03-12 23:04:35 +0000700 sst_hsw_stream_pause(hsw, pcm_data->stream, 0);
701 break;
Libin Yang1b006992015-02-10 10:02:47 +0800702 case SNDRV_PCM_TRIGGER_DRAIN:
703 pos = runtime->control->appl_ptr % runtime->buffer_size;
704 sst_hsw_stream_set_old_position(hsw, pcm_data->stream, pos);
705 sst_hsw_stream_set_silence_start(hsw, sst_stream, true);
706 break;
Mark Browna4b12992014-03-12 23:04:35 +0000707 default:
708 break;
709 }
710
711 return 0;
712}
713
714static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data)
715{
716 struct hsw_pcm_data *pcm_data = data;
717 struct snd_pcm_substream *substream = pcm_data->substream;
718 struct snd_pcm_runtime *runtime = substream->runtime;
719 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Libin Yang1b006992015-02-10 10:02:47 +0800720 struct hsw_priv_data *pdata =
721 snd_soc_platform_get_drvdata(rtd->platform);
722 struct sst_hsw *hsw = pdata->hsw;
Mark Browna4b12992014-03-12 23:04:35 +0000723 u32 pos;
Libin Yang1b006992015-02-10 10:02:47 +0800724 snd_pcm_uframes_t position = bytes_to_frames(runtime,
725 sst_hsw_get_dsp_position(hsw, pcm_data->stream));
726 unsigned char *dma_area = runtime->dma_area;
727 snd_pcm_uframes_t dma_frames =
728 bytes_to_frames(runtime, runtime->dma_bytes);
729 snd_pcm_uframes_t old_position;
730 ssize_t samples;
Mark Browna4b12992014-03-12 23:04:35 +0000731
732 pos = frames_to_bytes(runtime,
733 (runtime->control->appl_ptr % runtime->buffer_size));
734
Liam Girdwood80462492014-10-16 15:29:17 +0100735 dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos);
Mark Browna4b12992014-03-12 23:04:35 +0000736
Libin Yang1b006992015-02-10 10:02:47 +0800737 /* SST fw don't know where to stop dma
738 * So, SST driver need to clean the data which has been consumed
739 */
740 if (dma_area == NULL || dma_frames <= 0
741 || (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
742 || !sst_hsw_stream_get_silence_start(hsw, stream)) {
743 snd_pcm_period_elapsed(substream);
744 return pos;
745 }
746
747 old_position = sst_hsw_stream_get_old_position(hsw, stream);
748 if (position > old_position) {
749 if (position < dma_frames) {
750 samples = SST_SAMPLES(runtime, position - old_position);
751 snd_pcm_format_set_silence(runtime->format,
752 SST_OLD_POSITION(dma_area,
753 runtime, old_position),
754 samples);
755 } else
756 dev_err(rtd->dev, "PCM: position is wrong\n");
757 } else {
758 if (old_position < dma_frames) {
759 samples = SST_SAMPLES(runtime,
760 dma_frames - old_position);
761 snd_pcm_format_set_silence(runtime->format,
762 SST_OLD_POSITION(dma_area,
763 runtime, old_position),
764 samples);
765 } else
766 dev_err(rtd->dev, "PCM: dma_bytes is wrong\n");
767 if (position < dma_frames) {
768 samples = SST_SAMPLES(runtime, position);
769 snd_pcm_format_set_silence(runtime->format,
770 dma_area, samples);
771 } else
772 dev_err(rtd->dev, "PCM: position is wrong\n");
773 }
774 sst_hsw_stream_set_old_position(hsw, stream, position);
775
Mark Browna4b12992014-03-12 23:04:35 +0000776 /* let alsa know we have play a period */
777 snd_pcm_period_elapsed(substream);
778 return pos;
779}
780
781static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream)
782{
783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
784 struct snd_pcm_runtime *runtime = substream->runtime;
785 struct hsw_priv_data *pdata =
786 snd_soc_platform_get_drvdata(rtd->platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800787 struct hsw_pcm_data *pcm_data;
Mark Browna4b12992014-03-12 23:04:35 +0000788 struct sst_hsw *hsw = pdata->hsw;
789 snd_pcm_uframes_t offset;
Liam Girdwood51b4e242014-05-02 16:56:33 +0100790 uint64_t ppos;
Jie Yang7ff9d672015-01-09 14:36:36 +0800791 u32 position;
792 int dai;
793
794 dai = mod_map[rtd->cpu_dai->id].dai_id;
795 pcm_data = &pdata->pcm[dai][substream->stream];
796 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream);
Mark Browna4b12992014-03-12 23:04:35 +0000797
Liam Girdwood51b4e242014-05-02 16:56:33 +0100798 offset = bytes_to_frames(runtime, position);
799 ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream);
Mark Browna4b12992014-03-12 23:04:35 +0000800
Liam Girdwood80462492014-10-16 15:29:17 +0100801 dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n",
Liam Girdwood51b4e242014-05-02 16:56:33 +0100802 position, ppos);
Mark Browna4b12992014-03-12 23:04:35 +0000803 return offset;
804}
805
806static int hsw_pcm_open(struct snd_pcm_substream *substream)
807{
808 struct snd_soc_pcm_runtime *rtd = substream->private_data;
809 struct hsw_priv_data *pdata =
810 snd_soc_platform_get_drvdata(rtd->platform);
811 struct hsw_pcm_data *pcm_data;
812 struct sst_hsw *hsw = pdata->hsw;
Jie Yang7ff9d672015-01-09 14:36:36 +0800813 int dai;
Mark Browna4b12992014-03-12 23:04:35 +0000814
Jie Yang7ff9d672015-01-09 14:36:36 +0800815 dai = mod_map[rtd->cpu_dai->id].dai_id;
816 pcm_data = &pdata->pcm[dai][substream->stream];
Mark Browna4b12992014-03-12 23:04:35 +0000817
818 mutex_lock(&pcm_data->mutex);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000819 pm_runtime_get_sync(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000820
821 snd_soc_pcm_set_drvdata(rtd, pcm_data);
822 pcm_data->substream = substream;
823
824 snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware);
825
826 pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id,
827 hsw_notify_pointer, pcm_data);
828 if (pcm_data->stream == NULL) {
829 dev_err(rtd->dev, "error: failed to create stream\n");
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000830 pm_runtime_mark_last_busy(pdata->dev);
831 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000832 mutex_unlock(&pcm_data->mutex);
833 return -EINVAL;
834 }
835
Mark Browna4b12992014-03-12 23:04:35 +0000836 mutex_unlock(&pcm_data->mutex);
837 return 0;
838}
839
840static int hsw_pcm_close(struct snd_pcm_substream *substream)
841{
842 struct snd_soc_pcm_runtime *rtd = substream->private_data;
843 struct hsw_priv_data *pdata =
844 snd_soc_platform_get_drvdata(rtd->platform);
Jie Yang7ff9d672015-01-09 14:36:36 +0800845 struct hsw_pcm_data *pcm_data;
Mark Browna4b12992014-03-12 23:04:35 +0000846 struct sst_hsw *hsw = pdata->hsw;
Jie Yang7ff9d672015-01-09 14:36:36 +0800847 int ret, dai;
848
849 dai = mod_map[rtd->cpu_dai->id].dai_id;
850 pcm_data = &pdata->pcm[dai][substream->stream];
Mark Browna4b12992014-03-12 23:04:35 +0000851
852 mutex_lock(&pcm_data->mutex);
853 ret = sst_hsw_stream_reset(hsw, pcm_data->stream);
854 if (ret < 0) {
855 dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret);
856 goto out;
857 }
858
859 ret = sst_hsw_stream_free(hsw, pcm_data->stream);
860 if (ret < 0) {
861 dev_dbg(rtd->dev, "error: free stream failed %d\n", ret);
862 goto out;
863 }
Liam Girdwood916152c2014-05-02 16:56:32 +0100864 pcm_data->allocated = 0;
Mark Browna4b12992014-03-12 23:04:35 +0000865 pcm_data->stream = NULL;
866
867out:
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000868 pm_runtime_mark_last_busy(pdata->dev);
869 pm_runtime_put_autosuspend(pdata->dev);
Mark Browna4b12992014-03-12 23:04:35 +0000870 mutex_unlock(&pcm_data->mutex);
871 return ret;
872}
873
874static struct snd_pcm_ops hsw_pcm_ops = {
875 .open = hsw_pcm_open,
876 .close = hsw_pcm_close,
877 .ioctl = snd_pcm_lib_ioctl,
878 .hw_params = hsw_pcm_hw_params,
879 .hw_free = hsw_pcm_hw_free,
880 .trigger = hsw_pcm_trigger,
881 .pointer = hsw_pcm_pointer,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100882 .page = snd_pcm_sgbuf_ops_page,
Mark Browna4b12992014-03-12 23:04:35 +0000883};
884
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000885static int hsw_pcm_create_modules(struct hsw_priv_data *pdata)
886{
887 struct sst_hsw *hsw = pdata->hsw;
888 struct hsw_pcm_data *pcm_data;
889 int i;
890
891 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Jie Yang7ff9d672015-01-09 14:36:36 +0800892 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000893
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000894 /* create new runtime module, use same offset if recreated */
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000895 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
896 mod_map[i].mod_id, pcm_data->persistent_offset);
897 if (pcm_data->runtime == NULL)
898 goto err;
899 pcm_data->persistent_offset =
900 pcm_data->runtime->persistent_offset;
901 }
902
Lu, Han8c43fc22015-03-10 10:41:21 +0800903 /* create runtime blocks for module waves */
904 if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
905 pcm_data = &pdata->pcm[HSW_PCM_COUNT-1][0];
906 pcm_data->runtime = sst_hsw_runtime_module_create(hsw,
907 SST_HSW_MODULE_WAVES, pcm_data->persistent_offset);
908 if (pcm_data->runtime == NULL)
909 goto err;
910 pcm_data->persistent_offset =
911 pcm_data->runtime->persistent_offset;
912 }
913
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000914 return 0;
915
916err:
917 for (--i; i >= 0; i--) {
Jie Yang7ff9d672015-01-09 14:36:36 +0800918 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000919 sst_hsw_runtime_module_free(pcm_data->runtime);
920 }
921
922 return -ENODEV;
923}
924
925static void hsw_pcm_free_modules(struct hsw_priv_data *pdata)
926{
Lu, Han8c43fc22015-03-10 10:41:21 +0800927 struct sst_hsw *hsw = pdata->hsw;
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000928 struct hsw_pcm_data *pcm_data;
929 int i;
930
931 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Jie Yang7ff9d672015-01-09 14:36:36 +0800932 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
Lu, Han8c43fc22015-03-10 10:41:21 +0800933 sst_hsw_runtime_module_free(pcm_data->runtime);
934 }
935 if (sst_hsw_is_module_loaded(hsw, SST_HSW_MODULE_WAVES)) {
936 pcm_data = &pdata->pcm[HSW_PCM_COUNT-1][0];
Liam Girdwoode9600bc2014-10-28 17:37:12 +0000937 sst_hsw_runtime_module_free(pcm_data->runtime);
938 }
939}
940
Mark Browna4b12992014-03-12 23:04:35 +0000941static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd)
942{
943 struct snd_pcm *pcm = rtd->pcm;
Liam Girdwood10df3502014-05-02 16:56:31 +0100944 struct snd_soc_platform *platform = rtd->platform;
945 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000946 struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev);
Liam Girdwood10df3502014-05-02 16:56:31 +0100947 struct device *dev = pdata->dma_dev;
Mark Browna4b12992014-03-12 23:04:35 +0000948 int ret = 0;
949
Mark Browna4b12992014-03-12 23:04:35 +0000950 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
951 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
952 ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
Liam Girdwood0b708c82014-05-02 16:56:30 +0100953 SNDRV_DMA_TYPE_DEV_SG,
Liam Girdwood10df3502014-05-02 16:56:31 +0100954 dev,
Mark Browna4b12992014-03-12 23:04:35 +0000955 hsw_pcm_hardware.buffer_bytes_max,
956 hsw_pcm_hardware.buffer_bytes_max);
957 if (ret) {
958 dev_err(rtd->dev, "dma buffer allocation failed %d\n",
959 ret);
960 return ret;
961 }
962 }
Jie Yang7ff9d672015-01-09 14:36:36 +0800963 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream)
964 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_PLAYBACK].hsw_pcm = pcm;
965 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream)
966 priv_data->pcm[rtd->cpu_dai->id][SNDRV_PCM_STREAM_CAPTURE].hsw_pcm = pcm;
Mark Browna4b12992014-03-12 23:04:35 +0000967
968 return ret;
969}
970
971#define HSW_FORMATS \
Liam Girdwood2ccf3bd2014-10-16 15:29:14 +0100972 (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
Mark Browna4b12992014-03-12 23:04:35 +0000973
974static struct snd_soc_dai_driver hsw_dais[] = {
975 {
976 .name = "System Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000977 .id = HSW_PCM_DAI_ID_SYSTEM,
Mark Browna4b12992014-03-12 23:04:35 +0000978 .playback = {
979 .stream_name = "System Playback",
980 .channels_min = 2,
981 .channels_max = 2,
982 .rates = SNDRV_PCM_RATE_48000,
Jie Yang8e897612014-07-14 17:37:36 +0800983 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
Mark Browna4b12992014-03-12 23:04:35 +0000984 },
Jie Yang7bb73cb2014-11-28 21:52:11 +0800985 .capture = {
986 .stream_name = "Analog Capture",
987 .channels_min = 2,
988 .channels_max = 4,
989 .rates = SNDRV_PCM_RATE_48000,
990 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
991 },
Mark Browna4b12992014-03-12 23:04:35 +0000992 },
993 {
994 /* PCM */
995 .name = "Offload0 Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +0000996 .id = HSW_PCM_DAI_ID_OFFLOAD0,
Mark Browna4b12992014-03-12 23:04:35 +0000997 .playback = {
998 .stream_name = "Offload0 Playback",
999 .channels_min = 2,
1000 .channels_max = 2,
1001 .rates = SNDRV_PCM_RATE_8000_192000,
1002 .formats = HSW_FORMATS,
1003 },
1004 },
1005 {
1006 /* PCM */
1007 .name = "Offload1 Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001008 .id = HSW_PCM_DAI_ID_OFFLOAD1,
Mark Browna4b12992014-03-12 23:04:35 +00001009 .playback = {
1010 .stream_name = "Offload1 Playback",
1011 .channels_min = 2,
1012 .channels_max = 2,
1013 .rates = SNDRV_PCM_RATE_8000_192000,
1014 .formats = HSW_FORMATS,
1015 },
1016 },
1017 {
1018 .name = "Loopback Pin",
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001019 .id = HSW_PCM_DAI_ID_LOOPBACK,
Mark Browna4b12992014-03-12 23:04:35 +00001020 .capture = {
1021 .stream_name = "Loopback Capture",
1022 .channels_min = 2,
1023 .channels_max = 2,
Jie Yang8e897612014-07-14 17:37:36 +08001024 .rates = SNDRV_PCM_RATE_48000,
1025 .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE,
Mark Browna4b12992014-03-12 23:04:35 +00001026 },
1027 },
Mark Browna4b12992014-03-12 23:04:35 +00001028};
1029
1030static const struct snd_soc_dapm_widget widgets[] = {
1031
1032 /* Backend DAIs */
1033 SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1034 SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1035 SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0),
1036 SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
1037
1038 /* Global Playback Mixer */
1039 SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1040};
1041
1042static const struct snd_soc_dapm_route graph[] = {
1043
1044 /* Playback Mixer */
1045 {"Playback VMixer", NULL, "System Playback"},
1046 {"Playback VMixer", NULL, "Offload0 Playback"},
1047 {"Playback VMixer", NULL, "Offload1 Playback"},
1048
1049 {"SSP0 CODEC OUT", NULL, "Playback VMixer"},
1050
1051 {"Analog Capture", NULL, "SSP0 CODEC IN"},
1052};
1053
1054static int hsw_pcm_probe(struct snd_soc_platform *platform)
1055{
Lars-Peter Clausenbd033802014-08-20 13:08:47 +02001056 struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform);
Mark Browna4b12992014-03-12 23:04:35 +00001057 struct sst_pdata *pdata = dev_get_platdata(platform->dev);
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001058 struct device *dma_dev, *dev;
Liam Girdwood0b708c82014-05-02 16:56:30 +01001059 int i, ret = 0;
Mark Browna4b12992014-03-12 23:04:35 +00001060
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001061 if (!pdata)
1062 return -ENODEV;
1063
1064 dev = platform->dev;
1065 dma_dev = pdata->dma_dev;
1066
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001067 priv_data->hsw = pdata->dsp;
1068 priv_data->dev = platform->dev;
1069 priv_data->pm_state = HSW_PM_STATE_D0;
1070 priv_data->soc_card = platform->component.card;
1071
Mark Browna4b12992014-03-12 23:04:35 +00001072 /* allocate DSP buffer page tables */
1073 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1074
Mark Browna4b12992014-03-12 23:04:35 +00001075 /* playback */
1076 if (hsw_dais[i].playback.channels_min) {
Jie Yang7ff9d672015-01-09 14:36:36 +08001077 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_PLAYBACK].mutex);
Liam Girdwood0b708c82014-05-02 16:56:30 +01001078 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
1079 PAGE_SIZE, &priv_data->dmab[i][0]);
1080 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +00001081 goto err;
1082 }
1083
1084 /* capture */
1085 if (hsw_dais[i].capture.channels_min) {
Jie Yang7ff9d672015-01-09 14:36:36 +08001086 mutex_init(&priv_data->pcm[i][SNDRV_PCM_STREAM_CAPTURE].mutex);
Liam Girdwood0b708c82014-05-02 16:56:30 +01001087 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev,
1088 PAGE_SIZE, &priv_data->dmab[i][1]);
1089 if (ret < 0)
Mark Browna4b12992014-03-12 23:04:35 +00001090 goto err;
1091 }
1092 }
1093
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001094 /* allocate runtime modules */
Lu, Han8c43fc22015-03-10 10:41:21 +08001095 ret = hsw_pcm_create_modules(priv_data);
1096 if (ret < 0)
1097 goto err;
Liam Girdwoode9600bc2014-10-28 17:37:12 +00001098
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001099 /* enable runtime PM with auto suspend */
1100 pm_runtime_set_autosuspend_delay(platform->dev,
1101 SST_RUNTIME_SUSPEND_DELAY);
1102 pm_runtime_use_autosuspend(platform->dev);
1103 pm_runtime_enable(platform->dev);
1104 pm_runtime_idle(platform->dev);
1105
Mark Browna4b12992014-03-12 23:04:35 +00001106 return 0;
1107
1108err:
1109 for (;i >= 0; i--) {
1110 if (hsw_dais[i].playback.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +01001111 snd_dma_free_pages(&priv_data->dmab[i][0]);
Mark Browna4b12992014-03-12 23:04:35 +00001112 if (hsw_dais[i].capture.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +01001113 snd_dma_free_pages(&priv_data->dmab[i][1]);
Mark Browna4b12992014-03-12 23:04:35 +00001114 }
Liam Girdwood0b708c82014-05-02 16:56:30 +01001115 return ret;
Mark Browna4b12992014-03-12 23:04:35 +00001116}
1117
1118static int hsw_pcm_remove(struct snd_soc_platform *platform)
1119{
1120 struct hsw_priv_data *priv_data =
1121 snd_soc_platform_get_drvdata(platform);
1122 int i;
1123
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001124 pm_runtime_disable(platform->dev);
1125 hsw_pcm_free_modules(priv_data);
1126
Mark Browna4b12992014-03-12 23:04:35 +00001127 for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) {
1128 if (hsw_dais[i].playback.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +01001129 snd_dma_free_pages(&priv_data->dmab[i][0]);
Mark Browna4b12992014-03-12 23:04:35 +00001130 if (hsw_dais[i].capture.channels_min)
Liam Girdwood0b708c82014-05-02 16:56:30 +01001131 snd_dma_free_pages(&priv_data->dmab[i][1]);
Mark Browna4b12992014-03-12 23:04:35 +00001132 }
1133
1134 return 0;
1135}
1136
1137static struct snd_soc_platform_driver hsw_soc_platform = {
1138 .probe = hsw_pcm_probe,
1139 .remove = hsw_pcm_remove,
1140 .ops = &hsw_pcm_ops,
1141 .pcm_new = hsw_pcm_new,
Mark Browna4b12992014-03-12 23:04:35 +00001142};
1143
1144static const struct snd_soc_component_driver hsw_dai_component = {
Lars-Peter Clausen923976a2014-08-20 13:08:48 +02001145 .name = "haswell-dai",
1146 .controls = hsw_volume_controls,
1147 .num_controls = ARRAY_SIZE(hsw_volume_controls),
1148 .dapm_widgets = widgets,
1149 .num_dapm_widgets = ARRAY_SIZE(widgets),
1150 .dapm_routes = graph,
1151 .num_dapm_routes = ARRAY_SIZE(graph),
Mark Browna4b12992014-03-12 23:04:35 +00001152};
1153
1154static int hsw_pcm_dev_probe(struct platform_device *pdev)
1155{
1156 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
Lars-Peter Clausenbd033802014-08-20 13:08:47 +02001157 struct hsw_priv_data *priv_data;
Mark Browna4b12992014-03-12 23:04:35 +00001158 int ret;
1159
Lars-Peter Clausenbd033802014-08-20 13:08:47 +02001160 if (!sst_pdata)
1161 return -EINVAL;
1162
1163 priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL);
1164 if (!priv_data)
1165 return -ENOMEM;
1166
Mark Browna4b12992014-03-12 23:04:35 +00001167 ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata);
1168 if (ret < 0)
1169 return -ENODEV;
1170
Lars-Peter Clausenbd033802014-08-20 13:08:47 +02001171 priv_data->hsw = sst_pdata->dsp;
1172 platform_set_drvdata(pdev, priv_data);
1173
Mark Browna4b12992014-03-12 23:04:35 +00001174 ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform);
1175 if (ret < 0)
1176 goto err_plat;
1177
1178 ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component,
1179 hsw_dais, ARRAY_SIZE(hsw_dais));
1180 if (ret < 0)
1181 goto err_comp;
1182
1183 return 0;
1184
1185err_comp:
1186 snd_soc_unregister_platform(&pdev->dev);
1187err_plat:
1188 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1189 return 0;
1190}
1191
1192static int hsw_pcm_dev_remove(struct platform_device *pdev)
1193{
1194 struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
1195
1196 snd_soc_unregister_platform(&pdev->dev);
1197 snd_soc_unregister_component(&pdev->dev);
1198 sst_hsw_dsp_free(&pdev->dev, sst_pdata);
1199
1200 return 0;
1201}
1202
Rafael J. Wysocki45544c82014-12-19 15:26:46 +01001203#ifdef CONFIG_PM
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001204
1205static int hsw_pcm_runtime_idle(struct device *dev)
1206{
1207 return 0;
1208}
1209
1210static int hsw_pcm_runtime_suspend(struct device *dev)
1211{
1212 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1213 struct sst_hsw *hsw = pdata->hsw;
Lu, Han76c07b82015-03-12 13:53:00 +08001214 int ret;
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001215
Jie Yangcd311dd2015-02-05 22:56:48 +08001216 if (pdata->pm_state >= HSW_PM_STATE_RTD3)
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001217 return 0;
1218
Lu, Han76c07b82015-03-12 13:53:00 +08001219 /* fw modules will be unloaded on RTD3, set flag to track */
1220 if (sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
1221 ret = sst_hsw_module_disable(hsw, SST_HSW_MODULE_WAVES, 0);
1222 if (ret < 0)
1223 return ret;
1224 sst_hsw_set_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
1225 }
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001226 sst_hsw_dsp_runtime_suspend(hsw);
1227 sst_hsw_dsp_runtime_sleep(hsw);
Jie Yangcd311dd2015-02-05 22:56:48 +08001228 pdata->pm_state = HSW_PM_STATE_RTD3;
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001229
1230 return 0;
1231}
1232
1233static int hsw_pcm_runtime_resume(struct device *dev)
1234{
1235 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1236 struct sst_hsw *hsw = pdata->hsw;
1237 int ret;
1238
Jie Yangcd311dd2015-02-05 22:56:48 +08001239 if (pdata->pm_state != HSW_PM_STATE_RTD3)
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001240 return 0;
1241
1242 ret = sst_hsw_dsp_load(hsw);
1243 if (ret < 0) {
1244 dev_err(dev, "failed to reload %d\n", ret);
1245 return ret;
1246 }
1247
1248 ret = hsw_pcm_create_modules(pdata);
1249 if (ret < 0) {
1250 dev_err(dev, "failed to create modules %d\n", ret);
1251 return ret;
1252 }
1253
1254 ret = sst_hsw_dsp_runtime_resume(hsw);
1255 if (ret < 0)
1256 return ret;
1257 else if (ret == 1) /* no action required */
1258 return 0;
1259
Lu, Han76c07b82015-03-12 13:53:00 +08001260 /* check flag when resume */
1261 if (sst_hsw_is_module_enabled_rtd3(hsw, SST_HSW_MODULE_WAVES)) {
1262 ret = sst_hsw_module_enable(hsw, SST_HSW_MODULE_WAVES, 0);
1263 if (ret < 0)
1264 return ret;
Lu, Han3814c202015-03-12 13:53:02 +08001265 /* put parameters from buffer to dsp */
1266 ret = sst_hsw_launch_param_buf(hsw);
1267 if (ret < 0)
1268 return ret;
Lu, Han76c07b82015-03-12 13:53:00 +08001269 /* unset flag */
1270 sst_hsw_set_module_disabled_rtd3(hsw, SST_HSW_MODULE_WAVES);
1271 }
1272
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001273 pdata->pm_state = HSW_PM_STATE_D0;
1274 return ret;
1275}
1276
Liam Girdwood35e03a82014-10-30 14:58:19 +00001277#else
1278#define hsw_pcm_runtime_idle NULL
1279#define hsw_pcm_runtime_suspend NULL
1280#define hsw_pcm_runtime_resume NULL
1281#endif
1282
Rafael J. Wysocki45544c82014-12-19 15:26:46 +01001283#ifdef CONFIG_PM
Liam Girdwood7b8ef672014-11-04 13:27:45 +00001284
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001285static void hsw_pcm_complete(struct device *dev)
1286{
1287 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1288 struct sst_hsw *hsw = pdata->hsw;
1289 struct hsw_pcm_data *pcm_data;
1290 int i, err;
1291
Jie Yangcd311dd2015-02-05 22:56:48 +08001292 if (pdata->pm_state != HSW_PM_STATE_D3)
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001293 return;
1294
1295 err = sst_hsw_dsp_load(hsw);
1296 if (err < 0) {
1297 dev_err(dev, "failed to reload %d\n", err);
1298 return;
1299 }
1300
1301 err = hsw_pcm_create_modules(pdata);
1302 if (err < 0) {
1303 dev_err(dev, "failed to create modules %d\n", err);
1304 return;
1305 }
1306
Jie Yang98b9c1d2015-01-08 14:32:20 +08001307 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
Jie Yang7ff9d672015-01-09 14:36:36 +08001308 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001309
1310 if (!pcm_data->substream)
1311 continue;
1312
1313 err = sst_module_runtime_restore(pcm_data->runtime,
1314 &pcm_data->context);
1315 if (err < 0)
1316 dev_err(dev, "failed to restore context for PCM %d\n", i);
1317 }
1318
1319 snd_soc_resume(pdata->soc_card->dev);
1320
1321 err = sst_hsw_dsp_runtime_resume(hsw);
1322 if (err < 0)
1323 return;
1324 else if (err == 1) /* no action required */
1325 return;
1326
1327 pdata->pm_state = HSW_PM_STATE_D0;
1328 return;
1329}
1330
1331static int hsw_pcm_prepare(struct device *dev)
1332{
1333 struct hsw_priv_data *pdata = dev_get_drvdata(dev);
1334 struct sst_hsw *hsw = pdata->hsw;
1335 struct hsw_pcm_data *pcm_data;
1336 int i, err;
1337
1338 if (pdata->pm_state == HSW_PM_STATE_D3)
1339 return 0;
Jie Yangcd311dd2015-02-05 22:56:48 +08001340 else if (pdata->pm_state == HSW_PM_STATE_D0) {
1341 /* suspend all active streams */
1342 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1343 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001344
Jie Yangcd311dd2015-02-05 22:56:48 +08001345 if (!pcm_data->substream)
1346 continue;
1347 dev_dbg(dev, "suspending pcm %d\n", i);
1348 snd_pcm_suspend_all(pcm_data->hsw_pcm);
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001349
Jie Yangcd311dd2015-02-05 22:56:48 +08001350 /* We need to wait until the DSP FW stops the streams */
1351 msleep(2);
1352 }
1353
1354 /* preserve persistent memory */
1355 for (i = 0; i < ARRAY_SIZE(mod_map); i++) {
1356 pcm_data = &pdata->pcm[mod_map[i].dai_id][mod_map[i].stream];
1357
1358 if (!pcm_data->substream)
1359 continue;
1360
1361 dev_dbg(dev, "saving context pcm %d\n", i);
1362 err = sst_module_runtime_save(pcm_data->runtime,
1363 &pcm_data->context);
1364 if (err < 0)
1365 dev_err(dev, "failed to save context for PCM %d\n", i);
1366 }
1367 /* enter D3 state and stall */
1368 sst_hsw_dsp_runtime_suspend(hsw);
1369 /* put the DSP to sleep */
1370 sst_hsw_dsp_runtime_sleep(hsw);
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001371 }
1372
1373 snd_soc_suspend(pdata->soc_card->dev);
1374 snd_soc_poweroff(pdata->soc_card->dev);
1375
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001376 pdata->pm_state = HSW_PM_STATE_D3;
1377
1378 return 0;
1379}
1380
Liam Girdwood7b8ef672014-11-04 13:27:45 +00001381#else
1382#define hsw_pcm_prepare NULL
1383#define hsw_pcm_complete NULL
1384#endif
1385
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001386static const struct dev_pm_ops hsw_pcm_pm = {
1387 .runtime_idle = hsw_pcm_runtime_idle,
1388 .runtime_suspend = hsw_pcm_runtime_suspend,
1389 .runtime_resume = hsw_pcm_runtime_resume,
1390 .prepare = hsw_pcm_prepare,
1391 .complete = hsw_pcm_complete,
1392};
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001393
Mark Browna4b12992014-03-12 23:04:35 +00001394static struct platform_driver hsw_pcm_driver = {
1395 .driver = {
1396 .name = "haswell-pcm-audio",
Liam Girdwood2e4f75912014-10-29 17:40:43 +00001397 .pm = &hsw_pcm_pm,
Mark Browna4b12992014-03-12 23:04:35 +00001398 },
1399
1400 .probe = hsw_pcm_dev_probe,
1401 .remove = hsw_pcm_dev_remove,
1402};
1403module_platform_driver(hsw_pcm_driver);
1404
1405MODULE_AUTHOR("Liam Girdwood, Xingchao Wang");
1406MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM");
1407MODULE_LICENSE("GPL v2");
1408MODULE_ALIAS("platform:haswell-pcm-audio");