Commit | Line | Data |
---|---|---|
a4b12990 MB |
1 | /* |
2 | * Intel SST Haswell/Broadwell PCM Support | |
3 | * | |
4 | * Copyright (C) 2013, Intel Corporation. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License version | |
8 | * 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/dma-mapping.h> | |
19 | #include <linux/slab.h> | |
a4b12990 | 20 | #include <linux/delay.h> |
2e4f7591 | 21 | #include <linux/pm_runtime.h> |
a4b12990 MB |
22 | #include <asm/page.h> |
23 | #include <asm/pgtable.h> | |
24 | #include <sound/core.h> | |
25 | #include <sound/pcm.h> | |
26 | #include <sound/pcm_params.h> | |
27 | #include <sound/dmaengine_pcm.h> | |
28 | #include <sound/soc.h> | |
29 | #include <sound/tlv.h> | |
30 | #include <sound/compress_driver.h> | |
31 | ||
32 | #include "sst-haswell-ipc.h" | |
33 | #include "sst-dsp-priv.h" | |
34 | #include "sst-dsp.h" | |
35 | ||
36 | #define HSW_PCM_COUNT 6 | |
37 | #define HSW_VOLUME_MAX 0x7FFFFFFF /* 0dB */ | |
38 | ||
39 | /* simple volume table */ | |
40 | static const u32 volume_map[] = { | |
41 | HSW_VOLUME_MAX >> 30, | |
42 | HSW_VOLUME_MAX >> 29, | |
43 | HSW_VOLUME_MAX >> 28, | |
44 | HSW_VOLUME_MAX >> 27, | |
45 | HSW_VOLUME_MAX >> 26, | |
46 | HSW_VOLUME_MAX >> 25, | |
47 | HSW_VOLUME_MAX >> 24, | |
48 | HSW_VOLUME_MAX >> 23, | |
49 | HSW_VOLUME_MAX >> 22, | |
50 | HSW_VOLUME_MAX >> 21, | |
51 | HSW_VOLUME_MAX >> 20, | |
52 | HSW_VOLUME_MAX >> 19, | |
53 | HSW_VOLUME_MAX >> 18, | |
54 | HSW_VOLUME_MAX >> 17, | |
55 | HSW_VOLUME_MAX >> 16, | |
56 | HSW_VOLUME_MAX >> 15, | |
57 | HSW_VOLUME_MAX >> 14, | |
58 | HSW_VOLUME_MAX >> 13, | |
59 | HSW_VOLUME_MAX >> 12, | |
60 | HSW_VOLUME_MAX >> 11, | |
61 | HSW_VOLUME_MAX >> 10, | |
62 | HSW_VOLUME_MAX >> 9, | |
63 | HSW_VOLUME_MAX >> 8, | |
64 | HSW_VOLUME_MAX >> 7, | |
65 | HSW_VOLUME_MAX >> 6, | |
66 | HSW_VOLUME_MAX >> 5, | |
67 | HSW_VOLUME_MAX >> 4, | |
68 | HSW_VOLUME_MAX >> 3, | |
69 | HSW_VOLUME_MAX >> 2, | |
70 | HSW_VOLUME_MAX >> 1, | |
71 | HSW_VOLUME_MAX >> 0, | |
72 | }; | |
73 | ||
74 | #define HSW_PCM_PERIODS_MAX 64 | |
75 | #define HSW_PCM_PERIODS_MIN 2 | |
76 | ||
2e4f7591 LG |
77 | #define HSW_PCM_DAI_ID_SYSTEM 0 |
78 | #define HSW_PCM_DAI_ID_OFFLOAD0 1 | |
79 | #define HSW_PCM_DAI_ID_OFFLOAD1 2 | |
80 | #define HSW_PCM_DAI_ID_LOOPBACK 3 | |
81 | #define HSW_PCM_DAI_ID_CAPTURE 4 | |
82 | ||
83 | ||
a4b12990 MB |
84 | static const struct snd_pcm_hardware hsw_pcm_hardware = { |
85 | .info = SNDRV_PCM_INFO_MMAP | | |
86 | SNDRV_PCM_INFO_MMAP_VALID | | |
87 | SNDRV_PCM_INFO_INTERLEAVED | | |
88 | SNDRV_PCM_INFO_PAUSE | | |
89 | SNDRV_PCM_INFO_RESUME | | |
90 | SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, | |
8e897618 | 91 | .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | |
a4b12990 MB |
92 | SNDRV_PCM_FMTBIT_S32_LE, |
93 | .period_bytes_min = PAGE_SIZE, | |
94 | .period_bytes_max = (HSW_PCM_PERIODS_MAX / HSW_PCM_PERIODS_MIN) * PAGE_SIZE, | |
95 | .periods_min = HSW_PCM_PERIODS_MIN, | |
96 | .periods_max = HSW_PCM_PERIODS_MAX, | |
97 | .buffer_bytes_max = HSW_PCM_PERIODS_MAX * PAGE_SIZE, | |
98 | }; | |
99 | ||
e9600bc1 LG |
100 | struct hsw_pcm_module_map { |
101 | int dai_id; | |
102 | enum sst_hsw_module_id mod_id; | |
103 | }; | |
104 | ||
a4b12990 MB |
105 | /* private data for each PCM DSP stream */ |
106 | struct hsw_pcm_data { | |
107 | int dai_id; | |
108 | struct sst_hsw_stream *stream; | |
e9600bc1 | 109 | struct sst_module_runtime *runtime; |
2e4f7591 LG |
110 | struct sst_module_runtime_context context; |
111 | struct snd_pcm *hsw_pcm; | |
a4b12990 MB |
112 | u32 volume[2]; |
113 | struct snd_pcm_substream *substream; | |
114 | struct snd_compr_stream *cstream; | |
115 | unsigned int wpos; | |
116 | struct mutex mutex; | |
916152c4 | 117 | bool allocated; |
e9600bc1 | 118 | int persistent_offset; |
a4b12990 MB |
119 | }; |
120 | ||
2e4f7591 LG |
121 | enum hsw_pm_state { |
122 | HSW_PM_STATE_D3 = 0, | |
123 | HSW_PM_STATE_D0 = 1, | |
124 | }; | |
125 | ||
a4b12990 MB |
126 | /* private data for the driver */ |
127 | struct hsw_priv_data { | |
128 | /* runtime DSP */ | |
129 | struct sst_hsw *hsw; | |
2e4f7591 LG |
130 | struct device *dev; |
131 | enum hsw_pm_state pm_state; | |
132 | struct snd_soc_card *soc_card; | |
a4b12990 MB |
133 | |
134 | /* page tables */ | |
0b708c87 | 135 | struct snd_dma_buffer dmab[HSW_PCM_COUNT][2]; |
a4b12990 MB |
136 | |
137 | /* DAI data */ | |
138 | struct hsw_pcm_data pcm[HSW_PCM_COUNT]; | |
139 | }; | |
140 | ||
916152c4 LG |
141 | static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data); |
142 | ||
a4b12990 MB |
143 | static 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 | ||
151 | static 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 | ||
163 | static int hsw_stream_volume_put(struct snd_kcontrol *kcontrol, | |
164 | struct snd_ctl_elem_value *ucontrol) | |
165 | { | |
2e4f7591 | 166 | struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol); |
a4b12990 MB |
167 | struct soc_mixer_control *mc = |
168 | (struct soc_mixer_control *)kcontrol->private_value; | |
2e4f7591 LG |
169 | struct hsw_priv_data *pdata = |
170 | snd_soc_platform_get_drvdata(platform); | |
a4b12990 MB |
171 | 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); | |
2e4f7591 | 176 | pm_runtime_get_sync(pdata->dev); |
a4b12990 MB |
177 | |
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]); | |
2e4f7591 LG |
183 | pm_runtime_mark_last_busy(pdata->dev); |
184 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
185 | 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]); | |
192 | sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 2, volume); | |
193 | } else { | |
194 | volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]); | |
195 | sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 0, volume); | |
196 | volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]); | |
197 | sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, 1, volume); | |
198 | } | |
199 | ||
2e4f7591 LG |
200 | pm_runtime_mark_last_busy(pdata->dev); |
201 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
202 | mutex_unlock(&pcm_data->mutex); |
203 | return 0; | |
204 | } | |
205 | ||
206 | static int hsw_stream_volume_get(struct snd_kcontrol *kcontrol, | |
207 | struct snd_ctl_elem_value *ucontrol) | |
208 | { | |
2e4f7591 | 209 | struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol); |
a4b12990 MB |
210 | struct soc_mixer_control *mc = |
211 | (struct soc_mixer_control *)kcontrol->private_value; | |
2e4f7591 LG |
212 | struct hsw_priv_data *pdata = |
213 | snd_soc_platform_get_drvdata(platform); | |
a4b12990 MB |
214 | struct hsw_pcm_data *pcm_data = &pdata->pcm[mc->reg]; |
215 | struct sst_hsw *hsw = pdata->hsw; | |
216 | u32 volume; | |
217 | ||
218 | mutex_lock(&pcm_data->mutex); | |
2e4f7591 | 219 | pm_runtime_get_sync(pdata->dev); |
a4b12990 MB |
220 | |
221 | if (!pcm_data->stream) { | |
222 | ucontrol->value.integer.value[0] = | |
223 | hsw_ipc_to_mixer(pcm_data->volume[0]); | |
224 | ucontrol->value.integer.value[1] = | |
225 | hsw_ipc_to_mixer(pcm_data->volume[1]); | |
2e4f7591 LG |
226 | pm_runtime_mark_last_busy(pdata->dev); |
227 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
228 | mutex_unlock(&pcm_data->mutex); |
229 | return 0; | |
230 | } | |
231 | ||
232 | sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 0, &volume); | |
233 | ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume); | |
234 | sst_hsw_stream_get_volume(hsw, pcm_data->stream, 0, 1, &volume); | |
235 | ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume); | |
2e4f7591 LG |
236 | |
237 | pm_runtime_mark_last_busy(pdata->dev); | |
238 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
239 | mutex_unlock(&pcm_data->mutex); |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | static int hsw_volume_put(struct snd_kcontrol *kcontrol, | |
245 | struct snd_ctl_elem_value *ucontrol) | |
246 | { | |
2e4f7591 LG |
247 | struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol); |
248 | struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform); | |
a4b12990 MB |
249 | struct sst_hsw *hsw = pdata->hsw; |
250 | u32 volume; | |
251 | ||
2e4f7591 LG |
252 | pm_runtime_get_sync(pdata->dev); |
253 | ||
a4b12990 MB |
254 | if (ucontrol->value.integer.value[0] == |
255 | ucontrol->value.integer.value[1]) { | |
256 | ||
257 | volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]); | |
258 | sst_hsw_mixer_set_volume(hsw, 0, 2, volume); | |
259 | ||
260 | } else { | |
261 | volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[0]); | |
262 | sst_hsw_mixer_set_volume(hsw, 0, 0, volume); | |
263 | ||
264 | volume = hsw_mixer_to_ipc(ucontrol->value.integer.value[1]); | |
265 | sst_hsw_mixer_set_volume(hsw, 0, 1, volume); | |
266 | } | |
267 | ||
2e4f7591 LG |
268 | pm_runtime_mark_last_busy(pdata->dev); |
269 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
270 | return 0; |
271 | } | |
272 | ||
273 | static int hsw_volume_get(struct snd_kcontrol *kcontrol, | |
274 | struct snd_ctl_elem_value *ucontrol) | |
275 | { | |
2e4f7591 LG |
276 | struct snd_soc_platform *platform = snd_soc_kcontrol_platform(kcontrol); |
277 | struct hsw_priv_data *pdata = snd_soc_platform_get_drvdata(platform); | |
a4b12990 MB |
278 | struct sst_hsw *hsw = pdata->hsw; |
279 | unsigned int volume = 0; | |
280 | ||
2e4f7591 | 281 | pm_runtime_get_sync(pdata->dev); |
a4b12990 MB |
282 | sst_hsw_mixer_get_volume(hsw, 0, 0, &volume); |
283 | ucontrol->value.integer.value[0] = hsw_ipc_to_mixer(volume); | |
284 | ||
285 | sst_hsw_mixer_get_volume(hsw, 0, 1, &volume); | |
286 | ucontrol->value.integer.value[1] = hsw_ipc_to_mixer(volume); | |
287 | ||
2e4f7591 LG |
288 | pm_runtime_mark_last_busy(pdata->dev); |
289 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
290 | return 0; |
291 | } | |
292 | ||
293 | /* TLV used by both global and stream volumes */ | |
294 | static const DECLARE_TLV_DB_SCALE(hsw_vol_tlv, -9000, 300, 1); | |
295 | ||
296 | /* System Pin has no volume control */ | |
297 | static const struct snd_kcontrol_new hsw_volume_controls[] = { | |
298 | /* Global DSP volume */ | |
299 | SOC_DOUBLE_EXT_TLV("Master Playback Volume", 0, 0, 8, | |
300 | ARRAY_SIZE(volume_map) -1, 0, | |
301 | hsw_volume_get, hsw_volume_put, hsw_vol_tlv), | |
302 | /* Offload 0 volume */ | |
303 | SOC_DOUBLE_EXT_TLV("Media0 Playback Volume", 1, 0, 8, | |
304 | ARRAY_SIZE(volume_map), 0, | |
305 | hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv), | |
306 | /* Offload 1 volume */ | |
307 | SOC_DOUBLE_EXT_TLV("Media1 Playback Volume", 2, 0, 8, | |
308 | ARRAY_SIZE(volume_map), 0, | |
309 | hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv), | |
310 | /* Loopback volume */ | |
311 | SOC_DOUBLE_EXT_TLV("Loopback Capture Volume", 3, 0, 8, | |
312 | ARRAY_SIZE(volume_map), 0, | |
313 | hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv), | |
314 | /* Mic Capture volume */ | |
315 | SOC_DOUBLE_EXT_TLV("Mic Capture Volume", 4, 0, 8, | |
316 | ARRAY_SIZE(volume_map), 0, | |
317 | hsw_stream_volume_get, hsw_stream_volume_put, hsw_vol_tlv), | |
318 | }; | |
319 | ||
320 | /* Create DMA buffer page table for DSP */ | |
0b708c87 LG |
321 | static int create_adsp_page_table(struct snd_pcm_substream *substream, |
322 | struct hsw_priv_data *pdata, struct snd_soc_pcm_runtime *rtd, | |
323 | unsigned char *dma_area, size_t size, int pcm) | |
a4b12990 | 324 | { |
0b708c87 LG |
325 | struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream); |
326 | int i, pages, stream = substream->stream; | |
a4b12990 | 327 | |
0b708c87 | 328 | pages = snd_sgbuf_aligned_pages(size); |
a4b12990 MB |
329 | |
330 | dev_dbg(rtd->dev, "generating page table for %p size 0x%zu pages %d\n", | |
331 | dma_area, size, pages); | |
332 | ||
333 | for (i = 0; i < pages; i++) { | |
334 | u32 idx = (((i << 2) + i)) >> 1; | |
0b708c87 | 335 | u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT; |
a4b12990 MB |
336 | u32 *pg_table; |
337 | ||
338 | dev_dbg(rtd->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn); | |
339 | ||
0b708c87 | 340 | pg_table = (u32 *)(pdata->dmab[pcm][stream].area + idx); |
a4b12990 MB |
341 | |
342 | if (i & 1) | |
343 | *pg_table |= (pfn << 4); | |
344 | else | |
345 | *pg_table |= pfn; | |
346 | } | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
351 | /* this may get called several times by oss emulation */ | |
352 | static int hsw_pcm_hw_params(struct snd_pcm_substream *substream, | |
353 | struct snd_pcm_hw_params *params) | |
354 | { | |
355 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
356 | struct snd_pcm_runtime *runtime = substream->runtime; | |
357 | struct hsw_priv_data *pdata = | |
358 | snd_soc_platform_get_drvdata(rtd->platform); | |
359 | struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd); | |
360 | struct sst_hsw *hsw = pdata->hsw; | |
361 | struct sst_module *module_data; | |
362 | struct sst_dsp *dsp; | |
0b708c87 | 363 | struct snd_dma_buffer *dmab; |
a4b12990 MB |
364 | enum sst_hsw_stream_type stream_type; |
365 | enum sst_hsw_stream_path_id path_id; | |
366 | u32 rate, bits, map, pages, module_id; | |
367 | u8 channels; | |
368 | int ret; | |
369 | ||
916152c4 LG |
370 | /* check if we are being called a subsequent time */ |
371 | if (pcm_data->allocated) { | |
372 | ret = sst_hsw_stream_reset(hsw, pcm_data->stream); | |
373 | if (ret < 0) | |
374 | dev_dbg(rtd->dev, "error: reset stream failed %d\n", | |
375 | ret); | |
376 | ||
377 | ret = sst_hsw_stream_free(hsw, pcm_data->stream); | |
378 | if (ret < 0) { | |
379 | dev_dbg(rtd->dev, "error: free stream failed %d\n", | |
380 | ret); | |
381 | return ret; | |
382 | } | |
383 | pcm_data->allocated = false; | |
384 | ||
385 | pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id, | |
386 | hsw_notify_pointer, pcm_data); | |
387 | if (pcm_data->stream == NULL) { | |
388 | dev_err(rtd->dev, "error: failed to create stream\n"); | |
389 | return -EINVAL; | |
390 | } | |
391 | } | |
392 | ||
a4b12990 MB |
393 | /* stream direction */ |
394 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
395 | path_id = SST_HSW_STREAM_PATH_SSP0_OUT; | |
396 | else | |
397 | path_id = SST_HSW_STREAM_PATH_SSP0_IN; | |
398 | ||
399 | /* DSP stream type depends on DAI ID */ | |
400 | switch (rtd->cpu_dai->id) { | |
401 | case 0: | |
402 | stream_type = SST_HSW_STREAM_TYPE_SYSTEM; | |
403 | module_id = SST_HSW_MODULE_PCM_SYSTEM; | |
404 | break; | |
405 | case 1: | |
406 | case 2: | |
407 | stream_type = SST_HSW_STREAM_TYPE_RENDER; | |
408 | module_id = SST_HSW_MODULE_PCM; | |
409 | break; | |
410 | case 3: | |
411 | /* path ID needs to be OUT for loopback */ | |
412 | stream_type = SST_HSW_STREAM_TYPE_LOOPBACK; | |
413 | path_id = SST_HSW_STREAM_PATH_SSP0_OUT; | |
414 | module_id = SST_HSW_MODULE_PCM_REFERENCE; | |
415 | break; | |
416 | case 4: | |
417 | stream_type = SST_HSW_STREAM_TYPE_CAPTURE; | |
418 | module_id = SST_HSW_MODULE_PCM_CAPTURE; | |
419 | break; | |
420 | default: | |
421 | dev_err(rtd->dev, "error: invalid DAI ID %d\n", | |
422 | rtd->cpu_dai->id); | |
423 | return -EINVAL; | |
424 | }; | |
425 | ||
426 | ret = sst_hsw_stream_format(hsw, pcm_data->stream, | |
427 | path_id, stream_type, SST_HSW_STREAM_FORMAT_PCM_FORMAT); | |
428 | if (ret < 0) { | |
429 | dev_err(rtd->dev, "error: failed to set format %d\n", ret); | |
430 | return ret; | |
431 | } | |
432 | ||
433 | rate = params_rate(params); | |
434 | ret = sst_hsw_stream_set_rate(hsw, pcm_data->stream, rate); | |
435 | if (ret < 0) { | |
436 | dev_err(rtd->dev, "error: could not set rate %d\n", rate); | |
437 | return ret; | |
438 | } | |
439 | ||
440 | switch (params_format(params)) { | |
441 | case SNDRV_PCM_FORMAT_S16_LE: | |
442 | bits = SST_HSW_DEPTH_16BIT; | |
443 | sst_hsw_stream_set_valid(hsw, pcm_data->stream, 16); | |
444 | break; | |
445 | case SNDRV_PCM_FORMAT_S24_LE: | |
8e897618 JY |
446 | bits = SST_HSW_DEPTH_32BIT; |
447 | sst_hsw_stream_set_valid(hsw, pcm_data->stream, 24); | |
448 | break; | |
449 | case SNDRV_PCM_FORMAT_S8: | |
450 | bits = SST_HSW_DEPTH_8BIT; | |
451 | sst_hsw_stream_set_valid(hsw, pcm_data->stream, 8); | |
452 | break; | |
453 | case SNDRV_PCM_FORMAT_S32_LE: | |
454 | bits = SST_HSW_DEPTH_32BIT; | |
a4b12990 MB |
455 | sst_hsw_stream_set_valid(hsw, pcm_data->stream, 32); |
456 | break; | |
457 | default: | |
458 | dev_err(rtd->dev, "error: invalid format %d\n", | |
459 | params_format(params)); | |
460 | return -EINVAL; | |
461 | } | |
462 | ||
463 | ret = sst_hsw_stream_set_bits(hsw, pcm_data->stream, bits); | |
464 | if (ret < 0) { | |
465 | dev_err(rtd->dev, "error: could not set bits %d\n", bits); | |
466 | return ret; | |
467 | } | |
468 | ||
a4b12990 | 469 | channels = params_channels(params); |
a4b12990 MB |
470 | map = create_channel_map(SST_HSW_CHANNEL_CONFIG_STEREO); |
471 | sst_hsw_stream_set_map_config(hsw, pcm_data->stream, | |
472 | map, SST_HSW_CHANNEL_CONFIG_STEREO); | |
473 | ||
474 | ret = sst_hsw_stream_set_channels(hsw, pcm_data->stream, channels); | |
475 | if (ret < 0) { | |
476 | dev_err(rtd->dev, "error: could not set channels %d\n", | |
477 | channels); | |
478 | return ret; | |
479 | } | |
480 | ||
481 | ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params)); | |
482 | if (ret < 0) { | |
483 | dev_err(rtd->dev, "error: could not allocate %d bytes for PCM %d\n", | |
484 | params_buffer_bytes(params), ret); | |
485 | return ret; | |
486 | } | |
487 | ||
0b708c87 LG |
488 | dmab = snd_pcm_get_dma_buf(substream); |
489 | ||
490 | ret = create_adsp_page_table(substream, pdata, rtd, runtime->dma_area, | |
491 | runtime->dma_bytes, rtd->cpu_dai->id); | |
a4b12990 MB |
492 | if (ret < 0) |
493 | return ret; | |
494 | ||
495 | sst_hsw_stream_set_style(hsw, pcm_data->stream, | |
496 | SST_HSW_INTERLEAVING_PER_CHANNEL); | |
497 | ||
498 | if (runtime->dma_bytes % PAGE_SIZE) | |
499 | pages = (runtime->dma_bytes / PAGE_SIZE) + 1; | |
500 | else | |
501 | pages = runtime->dma_bytes / PAGE_SIZE; | |
502 | ||
503 | ret = sst_hsw_stream_buffer(hsw, pcm_data->stream, | |
0b708c87 | 504 | pdata->dmab[rtd->cpu_dai->id][substream->stream].addr, |
a4b12990 | 505 | pages, runtime->dma_bytes, 0, |
0b708c87 | 506 | snd_sgbuf_get_addr(dmab, 0) >> PAGE_SHIFT); |
a4b12990 MB |
507 | if (ret < 0) { |
508 | dev_err(rtd->dev, "error: failed to set DMA buffer %d\n", ret); | |
509 | return ret; | |
510 | } | |
511 | ||
512 | dsp = sst_hsw_get_dsp(hsw); | |
513 | ||
514 | module_data = sst_module_get_from_id(dsp, module_id); | |
515 | if (module_data == NULL) { | |
516 | dev_err(rtd->dev, "error: failed to get module config\n"); | |
517 | return -EINVAL; | |
518 | } | |
519 | ||
e9600bc1 LG |
520 | sst_hsw_stream_set_module_info(hsw, pcm_data->stream, |
521 | pcm_data->runtime); | |
a4b12990 MB |
522 | |
523 | ret = sst_hsw_stream_commit(hsw, pcm_data->stream); | |
524 | if (ret < 0) { | |
525 | dev_err(rtd->dev, "error: failed to commit stream %d\n", ret); | |
526 | return ret; | |
527 | } | |
916152c4 | 528 | pcm_data->allocated = true; |
a4b12990 MB |
529 | |
530 | ret = sst_hsw_stream_pause(hsw, pcm_data->stream, 1); | |
531 | if (ret < 0) | |
532 | dev_err(rtd->dev, "error: failed to pause %d\n", ret); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | static int hsw_pcm_hw_free(struct snd_pcm_substream *substream) | |
538 | { | |
539 | snd_pcm_lib_free_pages(substream); | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static int hsw_pcm_trigger(struct snd_pcm_substream *substream, int cmd) | |
544 | { | |
545 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
546 | struct hsw_priv_data *pdata = | |
547 | snd_soc_platform_get_drvdata(rtd->platform); | |
548 | struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd); | |
549 | struct sst_hsw *hsw = pdata->hsw; | |
550 | ||
551 | switch (cmd) { | |
552 | case SNDRV_PCM_TRIGGER_START: | |
553 | case SNDRV_PCM_TRIGGER_RESUME: | |
554 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
555 | sst_hsw_stream_resume(hsw, pcm_data->stream, 0); | |
556 | break; | |
557 | case SNDRV_PCM_TRIGGER_STOP: | |
558 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
559 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
560 | sst_hsw_stream_pause(hsw, pcm_data->stream, 0); | |
561 | break; | |
562 | default: | |
563 | break; | |
564 | } | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | static u32 hsw_notify_pointer(struct sst_hsw_stream *stream, void *data) | |
570 | { | |
571 | struct hsw_pcm_data *pcm_data = data; | |
572 | struct snd_pcm_substream *substream = pcm_data->substream; | |
573 | struct snd_pcm_runtime *runtime = substream->runtime; | |
574 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
575 | u32 pos; | |
576 | ||
577 | pos = frames_to_bytes(runtime, | |
578 | (runtime->control->appl_ptr % runtime->buffer_size)); | |
579 | ||
8046249d | 580 | dev_vdbg(rtd->dev, "PCM: App pointer %d bytes\n", pos); |
a4b12990 MB |
581 | |
582 | /* let alsa know we have play a period */ | |
583 | snd_pcm_period_elapsed(substream); | |
584 | return pos; | |
585 | } | |
586 | ||
587 | static snd_pcm_uframes_t hsw_pcm_pointer(struct snd_pcm_substream *substream) | |
588 | { | |
589 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
590 | struct snd_pcm_runtime *runtime = substream->runtime; | |
591 | struct hsw_priv_data *pdata = | |
592 | snd_soc_platform_get_drvdata(rtd->platform); | |
593 | struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd); | |
594 | struct sst_hsw *hsw = pdata->hsw; | |
595 | snd_pcm_uframes_t offset; | |
51b4e24f LG |
596 | uint64_t ppos; |
597 | u32 position = sst_hsw_get_dsp_position(hsw, pcm_data->stream); | |
a4b12990 | 598 | |
51b4e24f LG |
599 | offset = bytes_to_frames(runtime, position); |
600 | ppos = sst_hsw_get_dsp_presentation_position(hsw, pcm_data->stream); | |
a4b12990 | 601 | |
8046249d | 602 | dev_vdbg(rtd->dev, "PCM: DMA pointer %du bytes, pos %llu\n", |
51b4e24f | 603 | position, ppos); |
a4b12990 MB |
604 | return offset; |
605 | } | |
606 | ||
607 | static int hsw_pcm_open(struct snd_pcm_substream *substream) | |
608 | { | |
609 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
610 | struct hsw_priv_data *pdata = | |
611 | snd_soc_platform_get_drvdata(rtd->platform); | |
612 | struct hsw_pcm_data *pcm_data; | |
613 | struct sst_hsw *hsw = pdata->hsw; | |
614 | ||
615 | pcm_data = &pdata->pcm[rtd->cpu_dai->id]; | |
616 | ||
617 | mutex_lock(&pcm_data->mutex); | |
2e4f7591 | 618 | pm_runtime_get_sync(pdata->dev); |
a4b12990 MB |
619 | |
620 | snd_soc_pcm_set_drvdata(rtd, pcm_data); | |
621 | pcm_data->substream = substream; | |
622 | ||
623 | snd_soc_set_runtime_hwparams(substream, &hsw_pcm_hardware); | |
624 | ||
625 | pcm_data->stream = sst_hsw_stream_new(hsw, rtd->cpu_dai->id, | |
626 | hsw_notify_pointer, pcm_data); | |
627 | if (pcm_data->stream == NULL) { | |
628 | dev_err(rtd->dev, "error: failed to create stream\n"); | |
2e4f7591 LG |
629 | pm_runtime_mark_last_busy(pdata->dev); |
630 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
631 | mutex_unlock(&pcm_data->mutex); |
632 | return -EINVAL; | |
633 | } | |
634 | ||
635 | /* Set previous saved volume */ | |
636 | sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, | |
637 | 0, pcm_data->volume[0]); | |
638 | sst_hsw_stream_set_volume(hsw, pcm_data->stream, 0, | |
639 | 1, pcm_data->volume[1]); | |
640 | ||
641 | mutex_unlock(&pcm_data->mutex); | |
642 | return 0; | |
643 | } | |
644 | ||
645 | static int hsw_pcm_close(struct snd_pcm_substream *substream) | |
646 | { | |
647 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
648 | struct hsw_priv_data *pdata = | |
649 | snd_soc_platform_get_drvdata(rtd->platform); | |
650 | struct hsw_pcm_data *pcm_data = snd_soc_pcm_get_drvdata(rtd); | |
651 | struct sst_hsw *hsw = pdata->hsw; | |
652 | int ret; | |
653 | ||
654 | mutex_lock(&pcm_data->mutex); | |
655 | ret = sst_hsw_stream_reset(hsw, pcm_data->stream); | |
656 | if (ret < 0) { | |
657 | dev_dbg(rtd->dev, "error: reset stream failed %d\n", ret); | |
658 | goto out; | |
659 | } | |
660 | ||
661 | ret = sst_hsw_stream_free(hsw, pcm_data->stream); | |
662 | if (ret < 0) { | |
663 | dev_dbg(rtd->dev, "error: free stream failed %d\n", ret); | |
664 | goto out; | |
665 | } | |
916152c4 | 666 | pcm_data->allocated = 0; |
a4b12990 MB |
667 | pcm_data->stream = NULL; |
668 | ||
669 | out: | |
2e4f7591 LG |
670 | pm_runtime_mark_last_busy(pdata->dev); |
671 | pm_runtime_put_autosuspend(pdata->dev); | |
a4b12990 MB |
672 | mutex_unlock(&pcm_data->mutex); |
673 | return ret; | |
674 | } | |
675 | ||
676 | static struct snd_pcm_ops hsw_pcm_ops = { | |
677 | .open = hsw_pcm_open, | |
678 | .close = hsw_pcm_close, | |
679 | .ioctl = snd_pcm_lib_ioctl, | |
680 | .hw_params = hsw_pcm_hw_params, | |
681 | .hw_free = hsw_pcm_hw_free, | |
682 | .trigger = hsw_pcm_trigger, | |
683 | .pointer = hsw_pcm_pointer, | |
0b708c87 | 684 | .page = snd_pcm_sgbuf_ops_page, |
a4b12990 MB |
685 | }; |
686 | ||
e9600bc1 LG |
687 | /* static mappings between PCMs and modules - may be dynamic in future */ |
688 | static struct hsw_pcm_module_map mod_map[] = { | |
2e4f7591 LG |
689 | {HSW_PCM_DAI_ID_SYSTEM, SST_HSW_MODULE_PCM_SYSTEM}, |
690 | {HSW_PCM_DAI_ID_OFFLOAD0, SST_HSW_MODULE_PCM}, | |
691 | {HSW_PCM_DAI_ID_OFFLOAD1, SST_HSW_MODULE_PCM}, | |
692 | {HSW_PCM_DAI_ID_LOOPBACK, SST_HSW_MODULE_PCM_REFERENCE}, | |
693 | {HSW_PCM_DAI_ID_CAPTURE, SST_HSW_MODULE_PCM_CAPTURE}, | |
e9600bc1 LG |
694 | }; |
695 | ||
696 | static int hsw_pcm_create_modules(struct hsw_priv_data *pdata) | |
697 | { | |
698 | struct sst_hsw *hsw = pdata->hsw; | |
699 | struct hsw_pcm_data *pcm_data; | |
700 | int i; | |
701 | ||
702 | for (i = 0; i < ARRAY_SIZE(mod_map); i++) { | |
703 | pcm_data = &pdata->pcm[i]; | |
704 | ||
2e4f7591 | 705 | /* create new runtime module, use same offset if recreated */ |
e9600bc1 LG |
706 | pcm_data->runtime = sst_hsw_runtime_module_create(hsw, |
707 | mod_map[i].mod_id, pcm_data->persistent_offset); | |
708 | if (pcm_data->runtime == NULL) | |
709 | goto err; | |
710 | pcm_data->persistent_offset = | |
711 | pcm_data->runtime->persistent_offset; | |
712 | } | |
713 | ||
714 | return 0; | |
715 | ||
716 | err: | |
717 | for (--i; i >= 0; i--) { | |
718 | pcm_data = &pdata->pcm[i]; | |
719 | sst_hsw_runtime_module_free(pcm_data->runtime); | |
720 | } | |
721 | ||
722 | return -ENODEV; | |
723 | } | |
724 | ||
725 | static void hsw_pcm_free_modules(struct hsw_priv_data *pdata) | |
726 | { | |
727 | struct hsw_pcm_data *pcm_data; | |
728 | int i; | |
729 | ||
730 | for (i = 0; i < ARRAY_SIZE(mod_map); i++) { | |
731 | pcm_data = &pdata->pcm[i]; | |
732 | ||
733 | sst_hsw_runtime_module_free(pcm_data->runtime); | |
734 | } | |
735 | } | |
736 | ||
a4b12990 MB |
737 | static void hsw_pcm_free(struct snd_pcm *pcm) |
738 | { | |
739 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
740 | } | |
741 | ||
742 | static int hsw_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
743 | { | |
744 | struct snd_pcm *pcm = rtd->pcm; | |
10df3509 LG |
745 | struct snd_soc_platform *platform = rtd->platform; |
746 | struct sst_pdata *pdata = dev_get_platdata(platform->dev); | |
2e4f7591 | 747 | struct hsw_priv_data *priv_data = dev_get_drvdata(platform->dev); |
10df3509 | 748 | struct device *dev = pdata->dma_dev; |
a4b12990 MB |
749 | int ret = 0; |
750 | ||
a4b12990 MB |
751 | if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream || |
752 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) { | |
753 | ret = snd_pcm_lib_preallocate_pages_for_all(pcm, | |
0b708c87 | 754 | SNDRV_DMA_TYPE_DEV_SG, |
10df3509 | 755 | dev, |
a4b12990 MB |
756 | hsw_pcm_hardware.buffer_bytes_max, |
757 | hsw_pcm_hardware.buffer_bytes_max); | |
758 | if (ret) { | |
759 | dev_err(rtd->dev, "dma buffer allocation failed %d\n", | |
760 | ret); | |
761 | return ret; | |
762 | } | |
763 | } | |
2e4f7591 | 764 | priv_data->pcm[rtd->cpu_dai->id].hsw_pcm = pcm; |
a4b12990 MB |
765 | |
766 | return ret; | |
767 | } | |
768 | ||
769 | #define HSW_FORMATS \ | |
8e897618 JY |
770 | (SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S24_LE | \ |
771 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE |\ | |
772 | SNDRV_PCM_FMTBIT_S8) | |
a4b12990 MB |
773 | |
774 | static struct snd_soc_dai_driver hsw_dais[] = { | |
775 | { | |
776 | .name = "System Pin", | |
2e4f7591 | 777 | .id = HSW_PCM_DAI_ID_SYSTEM, |
a4b12990 MB |
778 | .playback = { |
779 | .stream_name = "System Playback", | |
780 | .channels_min = 2, | |
781 | .channels_max = 2, | |
782 | .rates = SNDRV_PCM_RATE_48000, | |
8e897618 | 783 | .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE, |
a4b12990 MB |
784 | }, |
785 | }, | |
786 | { | |
787 | /* PCM */ | |
788 | .name = "Offload0 Pin", | |
2e4f7591 | 789 | .id = HSW_PCM_DAI_ID_OFFLOAD0, |
a4b12990 MB |
790 | .playback = { |
791 | .stream_name = "Offload0 Playback", | |
792 | .channels_min = 2, | |
793 | .channels_max = 2, | |
794 | .rates = SNDRV_PCM_RATE_8000_192000, | |
795 | .formats = HSW_FORMATS, | |
796 | }, | |
797 | }, | |
798 | { | |
799 | /* PCM */ | |
800 | .name = "Offload1 Pin", | |
2e4f7591 | 801 | .id = HSW_PCM_DAI_ID_OFFLOAD1, |
a4b12990 MB |
802 | .playback = { |
803 | .stream_name = "Offload1 Playback", | |
804 | .channels_min = 2, | |
805 | .channels_max = 2, | |
806 | .rates = SNDRV_PCM_RATE_8000_192000, | |
807 | .formats = HSW_FORMATS, | |
808 | }, | |
809 | }, | |
810 | { | |
811 | .name = "Loopback Pin", | |
2e4f7591 | 812 | .id = HSW_PCM_DAI_ID_LOOPBACK, |
a4b12990 MB |
813 | .capture = { |
814 | .stream_name = "Loopback Capture", | |
815 | .channels_min = 2, | |
816 | .channels_max = 2, | |
8e897618 JY |
817 | .rates = SNDRV_PCM_RATE_48000, |
818 | .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE, | |
a4b12990 MB |
819 | }, |
820 | }, | |
821 | { | |
822 | .name = "Capture Pin", | |
2e4f7591 | 823 | .id = HSW_PCM_DAI_ID_CAPTURE, |
a4b12990 MB |
824 | .capture = { |
825 | .stream_name = "Analog Capture", | |
826 | .channels_min = 2, | |
48dc326f | 827 | .channels_max = 4, |
8e897618 JY |
828 | .rates = SNDRV_PCM_RATE_48000, |
829 | .formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE, | |
a4b12990 MB |
830 | }, |
831 | }, | |
832 | }; | |
833 | ||
834 | static const struct snd_soc_dapm_widget widgets[] = { | |
835 | ||
836 | /* Backend DAIs */ | |
837 | SND_SOC_DAPM_AIF_IN("SSP0 CODEC IN", NULL, 0, SND_SOC_NOPM, 0, 0), | |
838 | SND_SOC_DAPM_AIF_OUT("SSP0 CODEC OUT", NULL, 0, SND_SOC_NOPM, 0, 0), | |
839 | SND_SOC_DAPM_AIF_IN("SSP1 BT IN", NULL, 0, SND_SOC_NOPM, 0, 0), | |
840 | SND_SOC_DAPM_AIF_OUT("SSP1 BT OUT", NULL, 0, SND_SOC_NOPM, 0, 0), | |
841 | ||
842 | /* Global Playback Mixer */ | |
843 | SND_SOC_DAPM_MIXER("Playback VMixer", SND_SOC_NOPM, 0, 0, NULL, 0), | |
844 | }; | |
845 | ||
846 | static const struct snd_soc_dapm_route graph[] = { | |
847 | ||
848 | /* Playback Mixer */ | |
849 | {"Playback VMixer", NULL, "System Playback"}, | |
850 | {"Playback VMixer", NULL, "Offload0 Playback"}, | |
851 | {"Playback VMixer", NULL, "Offload1 Playback"}, | |
852 | ||
853 | {"SSP0 CODEC OUT", NULL, "Playback VMixer"}, | |
854 | ||
855 | {"Analog Capture", NULL, "SSP0 CODEC IN"}, | |
856 | }; | |
857 | ||
858 | static int hsw_pcm_probe(struct snd_soc_platform *platform) | |
859 | { | |
bd033808 | 860 | struct hsw_priv_data *priv_data = snd_soc_platform_get_drvdata(platform); |
a4b12990 | 861 | struct sst_pdata *pdata = dev_get_platdata(platform->dev); |
2e4f7591 | 862 | struct device *dma_dev, *dev; |
0b708c87 | 863 | int i, ret = 0; |
a4b12990 | 864 | |
2e4f7591 LG |
865 | if (!pdata) |
866 | return -ENODEV; | |
867 | ||
868 | dev = platform->dev; | |
869 | dma_dev = pdata->dma_dev; | |
870 | ||
871 | priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data), GFP_KERNEL); | |
872 | priv_data->hsw = pdata->dsp; | |
873 | priv_data->dev = platform->dev; | |
874 | priv_data->pm_state = HSW_PM_STATE_D0; | |
875 | priv_data->soc_card = platform->component.card; | |
876 | ||
a4b12990 MB |
877 | /* allocate DSP buffer page tables */ |
878 | for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) { | |
879 | ||
880 | mutex_init(&priv_data->pcm[i].mutex); | |
881 | ||
882 | /* playback */ | |
883 | if (hsw_dais[i].playback.channels_min) { | |
0b708c87 LG |
884 | ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev, |
885 | PAGE_SIZE, &priv_data->dmab[i][0]); | |
886 | if (ret < 0) | |
a4b12990 MB |
887 | goto err; |
888 | } | |
889 | ||
890 | /* capture */ | |
891 | if (hsw_dais[i].capture.channels_min) { | |
0b708c87 LG |
892 | ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, dma_dev, |
893 | PAGE_SIZE, &priv_data->dmab[i][1]); | |
894 | if (ret < 0) | |
a4b12990 MB |
895 | goto err; |
896 | } | |
897 | } | |
898 | ||
e9600bc1 LG |
899 | /* allocate runtime modules */ |
900 | hsw_pcm_create_modules(priv_data); | |
901 | ||
2e4f7591 LG |
902 | /* enable runtime PM with auto suspend */ |
903 | pm_runtime_set_autosuspend_delay(platform->dev, | |
904 | SST_RUNTIME_SUSPEND_DELAY); | |
905 | pm_runtime_use_autosuspend(platform->dev); | |
906 | pm_runtime_enable(platform->dev); | |
907 | pm_runtime_idle(platform->dev); | |
908 | ||
a4b12990 MB |
909 | return 0; |
910 | ||
911 | err: | |
912 | for (;i >= 0; i--) { | |
913 | if (hsw_dais[i].playback.channels_min) | |
0b708c87 | 914 | snd_dma_free_pages(&priv_data->dmab[i][0]); |
a4b12990 | 915 | if (hsw_dais[i].capture.channels_min) |
0b708c87 | 916 | snd_dma_free_pages(&priv_data->dmab[i][1]); |
a4b12990 | 917 | } |
0b708c87 | 918 | return ret; |
a4b12990 MB |
919 | } |
920 | ||
921 | static int hsw_pcm_remove(struct snd_soc_platform *platform) | |
922 | { | |
923 | struct hsw_priv_data *priv_data = | |
924 | snd_soc_platform_get_drvdata(platform); | |
925 | int i; | |
926 | ||
2e4f7591 LG |
927 | pm_runtime_disable(platform->dev); |
928 | hsw_pcm_free_modules(priv_data); | |
929 | ||
a4b12990 MB |
930 | for (i = 0; i < ARRAY_SIZE(hsw_dais); i++) { |
931 | if (hsw_dais[i].playback.channels_min) | |
0b708c87 | 932 | snd_dma_free_pages(&priv_data->dmab[i][0]); |
a4b12990 | 933 | if (hsw_dais[i].capture.channels_min) |
0b708c87 | 934 | snd_dma_free_pages(&priv_data->dmab[i][1]); |
a4b12990 MB |
935 | } |
936 | ||
937 | return 0; | |
938 | } | |
939 | ||
940 | static struct snd_soc_platform_driver hsw_soc_platform = { | |
941 | .probe = hsw_pcm_probe, | |
942 | .remove = hsw_pcm_remove, | |
943 | .ops = &hsw_pcm_ops, | |
944 | .pcm_new = hsw_pcm_new, | |
945 | .pcm_free = hsw_pcm_free, | |
a4b12990 MB |
946 | }; |
947 | ||
948 | static const struct snd_soc_component_driver hsw_dai_component = { | |
923976a3 LPC |
949 | .name = "haswell-dai", |
950 | .controls = hsw_volume_controls, | |
951 | .num_controls = ARRAY_SIZE(hsw_volume_controls), | |
952 | .dapm_widgets = widgets, | |
953 | .num_dapm_widgets = ARRAY_SIZE(widgets), | |
954 | .dapm_routes = graph, | |
955 | .num_dapm_routes = ARRAY_SIZE(graph), | |
a4b12990 MB |
956 | }; |
957 | ||
958 | static int hsw_pcm_dev_probe(struct platform_device *pdev) | |
959 | { | |
960 | struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev); | |
bd033808 | 961 | struct hsw_priv_data *priv_data; |
a4b12990 MB |
962 | int ret; |
963 | ||
bd033808 LPC |
964 | if (!sst_pdata) |
965 | return -EINVAL; | |
966 | ||
967 | priv_data = devm_kzalloc(&pdev->dev, sizeof(*priv_data), GFP_KERNEL); | |
968 | if (!priv_data) | |
969 | return -ENOMEM; | |
970 | ||
a4b12990 MB |
971 | ret = sst_hsw_dsp_init(&pdev->dev, sst_pdata); |
972 | if (ret < 0) | |
973 | return -ENODEV; | |
974 | ||
bd033808 LPC |
975 | priv_data->hsw = sst_pdata->dsp; |
976 | platform_set_drvdata(pdev, priv_data); | |
977 | ||
a4b12990 MB |
978 | ret = snd_soc_register_platform(&pdev->dev, &hsw_soc_platform); |
979 | if (ret < 0) | |
980 | goto err_plat; | |
981 | ||
982 | ret = snd_soc_register_component(&pdev->dev, &hsw_dai_component, | |
983 | hsw_dais, ARRAY_SIZE(hsw_dais)); | |
984 | if (ret < 0) | |
985 | goto err_comp; | |
986 | ||
987 | return 0; | |
988 | ||
989 | err_comp: | |
990 | snd_soc_unregister_platform(&pdev->dev); | |
991 | err_plat: | |
992 | sst_hsw_dsp_free(&pdev->dev, sst_pdata); | |
993 | return 0; | |
994 | } | |
995 | ||
996 | static int hsw_pcm_dev_remove(struct platform_device *pdev) | |
997 | { | |
998 | struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev); | |
999 | ||
1000 | snd_soc_unregister_platform(&pdev->dev); | |
1001 | snd_soc_unregister_component(&pdev->dev); | |
1002 | sst_hsw_dsp_free(&pdev->dev, sst_pdata); | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
2e4f7591 LG |
1007 | #ifdef CONFIG_PM |
1008 | #ifdef CONFIG_PM_RUNTIME | |
1009 | ||
1010 | static int hsw_pcm_runtime_idle(struct device *dev) | |
1011 | { | |
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | static int hsw_pcm_runtime_suspend(struct device *dev) | |
1016 | { | |
1017 | struct hsw_priv_data *pdata = dev_get_drvdata(dev); | |
1018 | struct sst_hsw *hsw = pdata->hsw; | |
1019 | ||
1020 | if (pdata->pm_state == HSW_PM_STATE_D3) | |
1021 | return 0; | |
1022 | ||
1023 | sst_hsw_dsp_runtime_suspend(hsw); | |
1024 | sst_hsw_dsp_runtime_sleep(hsw); | |
1025 | pdata->pm_state = HSW_PM_STATE_D3; | |
1026 | ||
1027 | return 0; | |
1028 | } | |
1029 | ||
1030 | static int hsw_pcm_runtime_resume(struct device *dev) | |
1031 | { | |
1032 | struct hsw_priv_data *pdata = dev_get_drvdata(dev); | |
1033 | struct sst_hsw *hsw = pdata->hsw; | |
1034 | int ret; | |
1035 | ||
1036 | if (pdata->pm_state == HSW_PM_STATE_D0) | |
1037 | return 0; | |
1038 | ||
1039 | ret = sst_hsw_dsp_load(hsw); | |
1040 | if (ret < 0) { | |
1041 | dev_err(dev, "failed to reload %d\n", ret); | |
1042 | return ret; | |
1043 | } | |
1044 | ||
1045 | ret = hsw_pcm_create_modules(pdata); | |
1046 | if (ret < 0) { | |
1047 | dev_err(dev, "failed to create modules %d\n", ret); | |
1048 | return ret; | |
1049 | } | |
1050 | ||
1051 | ret = sst_hsw_dsp_runtime_resume(hsw); | |
1052 | if (ret < 0) | |
1053 | return ret; | |
1054 | else if (ret == 1) /* no action required */ | |
1055 | return 0; | |
1056 | ||
1057 | pdata->pm_state = HSW_PM_STATE_D0; | |
1058 | return ret; | |
1059 | } | |
1060 | ||
1061 | static void hsw_pcm_complete(struct device *dev) | |
1062 | { | |
1063 | struct hsw_priv_data *pdata = dev_get_drvdata(dev); | |
1064 | struct sst_hsw *hsw = pdata->hsw; | |
1065 | struct hsw_pcm_data *pcm_data; | |
1066 | int i, err; | |
1067 | ||
1068 | if (pdata->pm_state == HSW_PM_STATE_D0) | |
1069 | return; | |
1070 | ||
1071 | err = sst_hsw_dsp_load(hsw); | |
1072 | if (err < 0) { | |
1073 | dev_err(dev, "failed to reload %d\n", err); | |
1074 | return; | |
1075 | } | |
1076 | ||
1077 | err = hsw_pcm_create_modules(pdata); | |
1078 | if (err < 0) { | |
1079 | dev_err(dev, "failed to create modules %d\n", err); | |
1080 | return; | |
1081 | } | |
1082 | ||
1083 | for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) { | |
1084 | pcm_data = &pdata->pcm[i]; | |
1085 | ||
1086 | if (!pcm_data->substream) | |
1087 | continue; | |
1088 | ||
1089 | err = sst_module_runtime_restore(pcm_data->runtime, | |
1090 | &pcm_data->context); | |
1091 | if (err < 0) | |
1092 | dev_err(dev, "failed to restore context for PCM %d\n", i); | |
1093 | } | |
1094 | ||
1095 | snd_soc_resume(pdata->soc_card->dev); | |
1096 | ||
1097 | err = sst_hsw_dsp_runtime_resume(hsw); | |
1098 | if (err < 0) | |
1099 | return; | |
1100 | else if (err == 1) /* no action required */ | |
1101 | return; | |
1102 | ||
1103 | pdata->pm_state = HSW_PM_STATE_D0; | |
1104 | return; | |
1105 | } | |
1106 | ||
1107 | static int hsw_pcm_prepare(struct device *dev) | |
1108 | { | |
1109 | struct hsw_priv_data *pdata = dev_get_drvdata(dev); | |
1110 | struct sst_hsw *hsw = pdata->hsw; | |
1111 | struct hsw_pcm_data *pcm_data; | |
1112 | int i, err; | |
1113 | ||
1114 | if (pdata->pm_state == HSW_PM_STATE_D3) | |
1115 | return 0; | |
1116 | /* suspend all active streams */ | |
1117 | for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) { | |
1118 | pcm_data = &pdata->pcm[i]; | |
1119 | ||
1120 | if (!pcm_data->substream) | |
1121 | continue; | |
1122 | dev_dbg(dev, "suspending pcm %d\n", i); | |
1123 | snd_pcm_suspend_all(pcm_data->hsw_pcm); | |
1124 | ||
1125 | /* We need to wait until the DSP FW stops the streams */ | |
1126 | msleep(2); | |
1127 | } | |
1128 | ||
1129 | snd_soc_suspend(pdata->soc_card->dev); | |
1130 | snd_soc_poweroff(pdata->soc_card->dev); | |
1131 | ||
1132 | /* enter D3 state and stall */ | |
1133 | sst_hsw_dsp_runtime_suspend(hsw); | |
1134 | ||
1135 | /* preserve persistent memory */ | |
1136 | for (i = 0; i < HSW_PCM_DAI_ID_CAPTURE + 1; i++) { | |
1137 | pcm_data = &pdata->pcm[i]; | |
1138 | ||
1139 | if (!pcm_data->substream) | |
1140 | continue; | |
1141 | ||
1142 | dev_dbg(dev, "saving context pcm %d\n", i); | |
1143 | err = sst_module_runtime_save(pcm_data->runtime, | |
1144 | &pcm_data->context); | |
1145 | if (err < 0) | |
1146 | dev_err(dev, "failed to save context for PCM %d\n", i); | |
1147 | } | |
1148 | ||
1149 | /* put the DSP to sleep */ | |
1150 | sst_hsw_dsp_runtime_sleep(hsw); | |
1151 | pdata->pm_state = HSW_PM_STATE_D3; | |
1152 | ||
1153 | return 0; | |
1154 | } | |
1155 | ||
1156 | #else | |
1157 | #define hsw_pcm_runtime_idle NULL | |
1158 | #define hsw_pcm_runtime_suspend NULL | |
1159 | #define hsw_pcm_runtime_resume NULL | |
1160 | #define hsw_pcm_runtime_complete NULL | |
1161 | #define hsw_pcm_runtime_prepare NULL | |
1162 | #endif | |
1163 | ||
1164 | static const struct dev_pm_ops hsw_pcm_pm = { | |
1165 | .runtime_idle = hsw_pcm_runtime_idle, | |
1166 | .runtime_suspend = hsw_pcm_runtime_suspend, | |
1167 | .runtime_resume = hsw_pcm_runtime_resume, | |
1168 | .prepare = hsw_pcm_prepare, | |
1169 | .complete = hsw_pcm_complete, | |
1170 | }; | |
1171 | #else | |
1172 | #define hsw_pcm_pm NULL | |
1173 | #endif | |
1174 | ||
a4b12990 MB |
1175 | static struct platform_driver hsw_pcm_driver = { |
1176 | .driver = { | |
1177 | .name = "haswell-pcm-audio", | |
1178 | .owner = THIS_MODULE, | |
2e4f7591 LG |
1179 | .pm = &hsw_pcm_pm, |
1180 | ||
a4b12990 MB |
1181 | }, |
1182 | ||
1183 | .probe = hsw_pcm_dev_probe, | |
1184 | .remove = hsw_pcm_dev_remove, | |
1185 | }; | |
1186 | module_platform_driver(hsw_pcm_driver); | |
1187 | ||
1188 | MODULE_AUTHOR("Liam Girdwood, Xingchao Wang"); | |
1189 | MODULE_DESCRIPTION("Haswell/Lynxpoint + Broadwell/Wildcatpoint PCM"); | |
1190 | MODULE_LICENSE("GPL v2"); | |
1191 | MODULE_ALIAS("platform:haswell-pcm-audio"); |