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

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

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ 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.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  sst_mfld_platform.c - Intel MID Platform driver
  3  *
  4  *  Copyright (C) 2010-2013 Intel Corp
  5  *  Author: Vinod Koul <vinod.koul@intel.com>
  6  *  Author: Harsha Priya <priya.harsha@intel.com>
  7  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8  *
  9  *  This program is free software; you can redistribute it and/or modify
 10  *  it under the terms of the GNU General Public License as published by
 11  *  the Free Software Foundation; version 2 of the License.
 12  *
 13  *  This program is distributed in the hope that it will be useful, but
 14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
 15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16  *  General Public License for more details.
 17  *
 18  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 19  */
 20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 21 
 22 #include <linux/slab.h>
 23 #include <linux/io.h>
 24 #include <linux/module.h>
 25 #include <sound/core.h>
 26 #include <sound/pcm.h>
 27 #include <sound/pcm_params.h>
 28 #include <sound/soc.h>
 29 #include <sound/compress_driver.h>
 30 #include "sst-mfld-platform.h"
 31 
 32 struct sst_device *sst;
 33 static DEFINE_MUTEX(sst_lock);
 34 extern struct snd_compr_ops sst_platform_compr_ops;
 35 
 36 int sst_register_dsp(struct sst_device *dev)
 37 {
 38         if (WARN_ON(!dev))
 39                 return -EINVAL;
 40         if (!try_module_get(dev->dev->driver->owner))
 41                 return -ENODEV;
 42         mutex_lock(&sst_lock);
 43         if (sst) {
 44                 pr_err("we already have a device %s\n", sst->name);
 45                 module_put(dev->dev->driver->owner);
 46                 mutex_unlock(&sst_lock);
 47                 return -EEXIST;
 48         }
 49         pr_debug("registering device %s\n", dev->name);
 50         sst = dev;
 51         mutex_unlock(&sst_lock);
 52         return 0;
 53 }
 54 EXPORT_SYMBOL_GPL(sst_register_dsp);
 55 
 56 int sst_unregister_dsp(struct sst_device *dev)
 57 {
 58         if (WARN_ON(!dev))
 59                 return -EINVAL;
 60         if (dev != sst)
 61                 return -EINVAL;
 62 
 63         mutex_lock(&sst_lock);
 64 
 65         if (!sst) {
 66                 mutex_unlock(&sst_lock);
 67                 return -EIO;
 68         }
 69 
 70         module_put(sst->dev->driver->owner);
 71         pr_debug("unreg %s\n", sst->name);
 72         sst = NULL;
 73         mutex_unlock(&sst_lock);
 74         return 0;
 75 }
 76 EXPORT_SYMBOL_GPL(sst_unregister_dsp);
 77 
 78 static struct snd_pcm_hardware sst_platform_pcm_hw = {
 79         .info = (SNDRV_PCM_INFO_INTERLEAVED |
 80                         SNDRV_PCM_INFO_DOUBLE |
 81                         SNDRV_PCM_INFO_PAUSE |
 82                         SNDRV_PCM_INFO_RESUME |
 83                         SNDRV_PCM_INFO_MMAP|
 84                         SNDRV_PCM_INFO_MMAP_VALID |
 85                         SNDRV_PCM_INFO_BLOCK_TRANSFER |
 86                         SNDRV_PCM_INFO_SYNC_START),
 87         .buffer_bytes_max = SST_MAX_BUFFER,
 88         .period_bytes_min = SST_MIN_PERIOD_BYTES,
 89         .period_bytes_max = SST_MAX_PERIOD_BYTES,
 90         .periods_min = SST_MIN_PERIODS,
 91         .periods_max = SST_MAX_PERIODS,
 92         .fifo_size = SST_FIFO_SIZE,
 93 };
 94 
 95 /* MFLD - MSIC */
 96 static struct snd_soc_dai_driver sst_platform_dai[] = {
 97 {
 98         .name = "Headset-cpu-dai",
 99         .id = 0,
100         .playback = {
101                 .channels_min = SST_STEREO,
102                 .channels_max = SST_STEREO,
103                 .rates = SNDRV_PCM_RATE_48000,
104                 .formats = SNDRV_PCM_FMTBIT_S24_LE,
105         },
106         .capture = {
107                 .channels_min = 1,
108                 .channels_max = 5,
109                 .rates = SNDRV_PCM_RATE_48000,
110                 .formats = SNDRV_PCM_FMTBIT_S24_LE,
111         },
112 },
113 {
114         .name = "Compress-cpu-dai",
115         .compress_dai = 1,
116         .playback = {
117                 .channels_min = SST_STEREO,
118                 .channels_max = SST_STEREO,
119                 .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
120                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
121         },
122 },
123 };
124 
125 /* helper functions */
126 void sst_set_stream_status(struct sst_runtime_stream *stream,
127                                         int state)
128 {
129         unsigned long flags;
130         spin_lock_irqsave(&stream->status_lock, flags);
131         stream->stream_status = state;
132         spin_unlock_irqrestore(&stream->status_lock, flags);
133 }
134 
135 static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
136 {
137         int state;
138         unsigned long flags;
139 
140         spin_lock_irqsave(&stream->status_lock, flags);
141         state = stream->stream_status;
142         spin_unlock_irqrestore(&stream->status_lock, flags);
143         return state;
144 }
145 
146 static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
147                                 struct sst_pcm_params *param)
148 {
149 
150         param->num_chan = (u8) substream->runtime->channels;
151         param->pcm_wd_sz = substream->runtime->sample_bits;
152         param->reserved = 0;
153         param->sfreq = substream->runtime->rate;
154         param->ring_buffer_size = snd_pcm_lib_buffer_bytes(substream);
155         param->period_count = substream->runtime->period_size;
156         param->ring_buffer_addr = virt_to_phys(substream->dma_buffer.area);
157         pr_debug("period_cnt = %d\n", param->period_count);
158         pr_debug("sfreq= %d, wd_sz = %d\n", param->sfreq, param->pcm_wd_sz);
159 }
160 
161 static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
162 {
163         struct sst_runtime_stream *stream =
164                         substream->runtime->private_data;
165         struct sst_pcm_params param = {0};
166         struct sst_stream_params str_params = {0};
167         int ret_val;
168 
169         /* set codec params and inform SST driver the same */
170         sst_fill_pcm_params(substream, &param);
171         substream->runtime->dma_area = substream->dma_buffer.area;
172         str_params.sparams = param;
173         str_params.codec =  param.codec;
174         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
175                 str_params.ops = STREAM_OPS_PLAYBACK;
176                 str_params.device_type = substream->pcm->device + 1;
177                 pr_debug("Playbck stream,Device %d\n",
178                                         substream->pcm->device);
179         } else {
180                 str_params.ops = STREAM_OPS_CAPTURE;
181                 str_params.device_type = SND_SST_DEVICE_CAPTURE;
182                 pr_debug("Capture stream,Device %d\n",
183                                         substream->pcm->device);
184         }
185         ret_val = stream->ops->open(&str_params);
186         pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
187         if (ret_val < 0)
188                 return ret_val;
189 
190         stream->stream_info.str_id = ret_val;
191         pr_debug("str id :  %d\n", stream->stream_info.str_id);
192         return ret_val;
193 }
194 
195 static void sst_period_elapsed(void *mad_substream)
196 {
197         struct snd_pcm_substream *substream = mad_substream;
198         struct sst_runtime_stream *stream;
199         int status;
200 
201         if (!substream || !substream->runtime)
202                 return;
203         stream = substream->runtime->private_data;
204         if (!stream)
205                 return;
206         status = sst_get_stream_status(stream);
207         if (status != SST_PLATFORM_RUNNING)
208                 return;
209         snd_pcm_period_elapsed(substream);
210 }
211 
212 static int sst_platform_init_stream(struct snd_pcm_substream *substream)
213 {
214         struct sst_runtime_stream *stream =
215                         substream->runtime->private_data;
216         int ret_val;
217 
218         pr_debug("setting buffer ptr param\n");
219         sst_set_stream_status(stream, SST_PLATFORM_INIT);
220         stream->stream_info.period_elapsed = sst_period_elapsed;
221         stream->stream_info.mad_substream = substream;
222         stream->stream_info.buffer_ptr = 0;
223         stream->stream_info.sfreq = substream->runtime->rate;
224         ret_val = stream->ops->device_control(
225                         SST_SND_STREAM_INIT, &stream->stream_info);
226         if (ret_val)
227                 pr_err("control_set ret error %d\n", ret_val);
228         return ret_val;
229 
230 }
231 /* end -- helper functions */
232 
233 static int sst_platform_open(struct snd_pcm_substream *substream)
234 {
235         struct snd_pcm_runtime *runtime = substream->runtime;
236         struct sst_runtime_stream *stream;
237         int ret_val;
238 
239         pr_debug("sst_platform_open called\n");
240 
241         snd_soc_set_runtime_hwparams(substream, &sst_platform_pcm_hw);
242         ret_val = snd_pcm_hw_constraint_integer(runtime,
243                                                 SNDRV_PCM_HW_PARAM_PERIODS);
244         if (ret_val < 0)
245                 return ret_val;
246 
247         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
248         if (!stream)
249                 return -ENOMEM;
250         spin_lock_init(&stream->status_lock);
251 
252         /* get the sst ops */
253         mutex_lock(&sst_lock);
254         if (!sst) {
255                 pr_err("no device available to run\n");
256                 mutex_unlock(&sst_lock);
257                 kfree(stream);
258                 return -ENODEV;
259         }
260         if (!try_module_get(sst->dev->driver->owner)) {
261                 mutex_unlock(&sst_lock);
262                 kfree(stream);
263                 return -ENODEV;
264         }
265         stream->ops = sst->ops;
266         mutex_unlock(&sst_lock);
267 
268         stream->stream_info.str_id = 0;
269         sst_set_stream_status(stream, SST_PLATFORM_INIT);
270         stream->stream_info.mad_substream = substream;
271         /* allocate memory for SST API set */
272         runtime->private_data = stream;
273 
274         return 0;
275 }
276 
277 static int sst_platform_close(struct snd_pcm_substream *substream)
278 {
279         struct sst_runtime_stream *stream;
280         int ret_val = 0, str_id;
281 
282         pr_debug("sst_platform_close called\n");
283         stream = substream->runtime->private_data;
284         str_id = stream->stream_info.str_id;
285         if (str_id)
286                 ret_val = stream->ops->close(str_id);
287         module_put(sst->dev->driver->owner);
288         kfree(stream);
289         return ret_val;
290 }
291 
292 static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
293 {
294         struct sst_runtime_stream *stream;
295         int ret_val = 0, str_id;
296 
297         pr_debug("sst_platform_pcm_prepare called\n");
298         stream = substream->runtime->private_data;
299         str_id = stream->stream_info.str_id;
300         if (stream->stream_info.str_id) {
301                 ret_val = stream->ops->device_control(
302                                 SST_SND_DROP, &str_id);
303                 return ret_val;
304         }
305 
306         ret_val = sst_platform_alloc_stream(substream);
307         if (ret_val < 0)
308                 return ret_val;
309         snprintf(substream->pcm->id, sizeof(substream->pcm->id),
310                         "%d", stream->stream_info.str_id);
311 
312         ret_val = sst_platform_init_stream(substream);
313         if (ret_val)
314                 return ret_val;
315         substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
316         return ret_val;
317 }
318 
319 static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
320                                         int cmd)
321 {
322         int ret_val = 0, str_id;
323         struct sst_runtime_stream *stream;
324         int str_cmd, status;
325 
326         pr_debug("sst_platform_pcm_trigger called\n");
327         stream = substream->runtime->private_data;
328         str_id = stream->stream_info.str_id;
329         switch (cmd) {
330         case SNDRV_PCM_TRIGGER_START:
331                 pr_debug("sst: Trigger Start\n");
332                 str_cmd = SST_SND_START;
333                 status = SST_PLATFORM_RUNNING;
334                 stream->stream_info.mad_substream = substream;
335                 break;
336         case SNDRV_PCM_TRIGGER_STOP:
337                 pr_debug("sst: in stop\n");
338                 str_cmd = SST_SND_DROP;
339                 status = SST_PLATFORM_DROPPED;
340                 break;
341         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
342                 pr_debug("sst: in pause\n");
343                 str_cmd = SST_SND_PAUSE;
344                 status = SST_PLATFORM_PAUSED;
345                 break;
346         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
347                 pr_debug("sst: in pause release\n");
348                 str_cmd = SST_SND_RESUME;
349                 status = SST_PLATFORM_RUNNING;
350                 break;
351         default:
352                 return -EINVAL;
353         }
354         ret_val = stream->ops->device_control(str_cmd, &str_id);
355         if (!ret_val)
356                 sst_set_stream_status(stream, status);
357 
358         return ret_val;
359 }
360 
361 
362 static snd_pcm_uframes_t sst_platform_pcm_pointer
363                         (struct snd_pcm_substream *substream)
364 {
365         struct sst_runtime_stream *stream;
366         int ret_val, status;
367         struct pcm_stream_info *str_info;
368 
369         stream = substream->runtime->private_data;
370         status = sst_get_stream_status(stream);
371         if (status == SST_PLATFORM_INIT)
372                 return 0;
373         str_info = &stream->stream_info;
374         ret_val = stream->ops->device_control(
375                                 SST_SND_BUFFER_POINTER, str_info);
376         if (ret_val) {
377                 pr_err("sst: error code = %d\n", ret_val);
378                 return ret_val;
379         }
380         return stream->stream_info.buffer_ptr;
381 }
382 
383 static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
384                 struct snd_pcm_hw_params *params)
385 {
386         int ret;
387 
388         ret =
389                 snd_pcm_lib_malloc_pages(substream,
390                                 params_buffer_bytes(params));
391         if (ret)
392                 return ret;
393         memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
394 
395         return 0;
396 }
397 
398 static int sst_platform_pcm_hw_free(struct snd_pcm_substream *substream)
399 {
400         return snd_pcm_lib_free_pages(substream);
401 }
402 
403 static struct snd_pcm_ops sst_platform_ops = {
404         .open = sst_platform_open,
405         .close = sst_platform_close,
406         .ioctl = snd_pcm_lib_ioctl,
407         .prepare = sst_platform_pcm_prepare,
408         .trigger = sst_platform_pcm_trigger,
409         .pointer = sst_platform_pcm_pointer,
410         .hw_params = sst_platform_pcm_hw_params,
411         .hw_free = sst_platform_pcm_hw_free,
412 };
413 
414 static void sst_pcm_free(struct snd_pcm *pcm)
415 {
416         pr_debug("sst_pcm_free called\n");
417         snd_pcm_lib_preallocate_free_for_all(pcm);
418 }
419 
420 static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
421 {
422         struct snd_pcm *pcm = rtd->pcm;
423         int retval = 0;
424 
425         pr_debug("sst_pcm_new called\n");
426         if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
427                         pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
428                 retval =  snd_pcm_lib_preallocate_pages_for_all(pcm,
429                         SNDRV_DMA_TYPE_CONTINUOUS,
430                         snd_dma_continuous_data(GFP_KERNEL),
431                         SST_MIN_BUFFER, SST_MAX_BUFFER);
432                 if (retval) {
433                         pr_err("dma buffer allocationf fail\n");
434                         return retval;
435                 }
436         }
437         return retval;
438 }
439 
440 static struct snd_soc_platform_driver sst_soc_platform_drv = {
441         .ops            = &sst_platform_ops,
442         .compr_ops      = &sst_platform_compr_ops,
443         .pcm_new        = sst_pcm_new,
444         .pcm_free       = sst_pcm_free,
445 };
446 
447 static const struct snd_soc_component_driver sst_component = {
448         .name           = "sst",
449 };
450 
451 
452 static int sst_platform_probe(struct platform_device *pdev)
453 {
454         int ret;
455 
456         pr_debug("sst_platform_probe called\n");
457         sst = NULL;
458         ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
459         if (ret) {
460                 pr_err("registering soc platform failed\n");
461                 return ret;
462         }
463 
464         ret = snd_soc_register_component(&pdev->dev, &sst_component,
465                                 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
466         if (ret) {
467                 pr_err("registering cpu dais failed\n");
468                 snd_soc_unregister_platform(&pdev->dev);
469         }
470         return ret;
471 }
472 
473 static int sst_platform_remove(struct platform_device *pdev)
474 {
475 
476         snd_soc_unregister_component(&pdev->dev);
477         snd_soc_unregister_platform(&pdev->dev);
478         pr_debug("sst_platform_remove success\n");
479         return 0;
480 }
481 
482 static struct platform_driver sst_platform_driver = {
483         .driver         = {
484                 .name           = "sst-mfld-platform",
485                 .owner          = THIS_MODULE,
486         },
487         .probe          = sst_platform_probe,
488         .remove         = sst_platform_remove,
489 };
490 
491 module_platform_driver(sst_platform_driver);
492 
493 MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
494 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
495 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
496 MODULE_LICENSE("GPL v2");
497 MODULE_ALIAS("platform:sst-mfld-platform");
498 

~ [ 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