~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/intel/haswell/sst-haswell-pcm.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp