2 * sst_mfld_platform.c - Intel MID Platform driver
4 * Copyright (C) 2010-2014 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/slab.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 <asm/platform_sst_audio.h>
31 #include "sst-mfld-platform.h"
32 #include "sst-atom-controls.h"
34 struct sst_device
*sst
;
35 static DEFINE_MUTEX(sst_lock
);
36 extern struct snd_compr_ops sst_platform_compr_ops
;
38 int sst_register_dsp(struct sst_device
*dev
)
42 if (!try_module_get(dev
->dev
->driver
->owner
))
44 mutex_lock(&sst_lock
);
46 dev_err(dev
->dev
, "we already have a device %s\n", sst
->name
);
47 module_put(dev
->dev
->driver
->owner
);
48 mutex_unlock(&sst_lock
);
51 dev_dbg(dev
->dev
, "registering device %s\n", dev
->name
);
53 mutex_unlock(&sst_lock
);
56 EXPORT_SYMBOL_GPL(sst_register_dsp
);
58 int sst_unregister_dsp(struct sst_device
*dev
)
65 mutex_lock(&sst_lock
);
68 mutex_unlock(&sst_lock
);
72 module_put(sst
->dev
->driver
->owner
);
73 dev_dbg(dev
->dev
, "unreg %s\n", sst
->name
);
75 mutex_unlock(&sst_lock
);
78 EXPORT_SYMBOL_GPL(sst_unregister_dsp
);
80 static struct snd_pcm_hardware sst_platform_pcm_hw
= {
81 .info
= (SNDRV_PCM_INFO_INTERLEAVED
|
82 SNDRV_PCM_INFO_DOUBLE
|
83 SNDRV_PCM_INFO_PAUSE
|
84 SNDRV_PCM_INFO_RESUME
|
86 SNDRV_PCM_INFO_MMAP_VALID
|
87 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
88 SNDRV_PCM_INFO_SYNC_START
),
89 .buffer_bytes_max
= SST_MAX_BUFFER
,
90 .period_bytes_min
= SST_MIN_PERIOD_BYTES
,
91 .period_bytes_max
= SST_MAX_PERIOD_BYTES
,
92 .periods_min
= SST_MIN_PERIODS
,
93 .periods_max
= SST_MAX_PERIODS
,
94 .fifo_size
= SST_FIFO_SIZE
,
97 static struct sst_dev_stream_map dpcm_strm_map
[] = {
98 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, /* Reserved, not in use */
99 {MERR_DPCM_AUDIO
, 0, SNDRV_PCM_STREAM_PLAYBACK
, PIPE_MEDIA1_IN
, SST_TASK_ID_MEDIA
, 0},
100 {MERR_DPCM_COMPR
, 0, SNDRV_PCM_STREAM_PLAYBACK
, PIPE_MEDIA0_IN
, SST_TASK_ID_MEDIA
, 0},
101 {MERR_DPCM_AUDIO
, 0, SNDRV_PCM_STREAM_CAPTURE
, PIPE_PCM1_OUT
, SST_TASK_ID_MEDIA
, 0},
105 static struct snd_soc_dai_driver sst_platform_dai
[] = {
107 .name
= "Headset-cpu-dai",
110 .channels_min
= SST_STEREO
,
111 .channels_max
= SST_STEREO
,
112 .rates
= SNDRV_PCM_RATE_48000
,
113 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
118 .rates
= SNDRV_PCM_RATE_48000
,
119 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
123 .name
= "Compress-cpu-dai",
126 .channels_min
= SST_STEREO
,
127 .channels_max
= SST_STEREO
,
128 .rates
= SNDRV_PCM_RATE_44100
|SNDRV_PCM_RATE_48000
,
129 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
134 /* helper functions */
135 void sst_set_stream_status(struct sst_runtime_stream
*stream
,
139 spin_lock_irqsave(&stream
->status_lock
, flags
);
140 stream
->stream_status
= state
;
141 spin_unlock_irqrestore(&stream
->status_lock
, flags
);
144 static inline int sst_get_stream_status(struct sst_runtime_stream
*stream
)
149 spin_lock_irqsave(&stream
->status_lock
, flags
);
150 state
= stream
->stream_status
;
151 spin_unlock_irqrestore(&stream
->status_lock
, flags
);
155 static void sst_fill_alloc_params(struct snd_pcm_substream
*substream
,
156 struct snd_sst_alloc_params_ext
*alloc_param
)
158 unsigned int channels
;
159 snd_pcm_uframes_t period_size
;
161 ssize_t buffer_bytes
= snd_pcm_lib_buffer_bytes(substream
);
162 u32 buffer_addr
= virt_to_phys(substream
->dma_buffer
.area
);
164 channels
= substream
->runtime
->channels
;
165 period_size
= substream
->runtime
->period_size
;
166 periodbytes
= samples_to_bytes(substream
->runtime
, period_size
);
167 alloc_param
->ring_buf_info
[0].addr
= buffer_addr
;
168 alloc_param
->ring_buf_info
[0].size
= buffer_bytes
;
169 alloc_param
->sg_count
= 1;
170 alloc_param
->reserved
= 0;
171 alloc_param
->frag_size
= periodbytes
* channels
;
174 static void sst_fill_pcm_params(struct snd_pcm_substream
*substream
,
175 struct snd_sst_stream_params
*param
)
177 param
->uc
.pcm_params
.num_chan
= (u8
) substream
->runtime
->channels
;
178 param
->uc
.pcm_params
.pcm_wd_sz
= substream
->runtime
->sample_bits
;
179 param
->uc
.pcm_params
.sfreq
= substream
->runtime
->rate
;
181 /* PCM stream via ALSA interface */
182 param
->uc
.pcm_params
.use_offload_path
= 0;
183 param
->uc
.pcm_params
.reserved2
= 0;
184 memset(param
->uc
.pcm_params
.channel_map
, 0, sizeof(u8
));
188 static int sst_get_stream_mapping(int dev
, int sdev
, int dir
,
189 struct sst_dev_stream_map
*map
, int size
)
197 /* index 0 is not used in stream map */
198 for (i
= 1; i
< size
; i
++) {
199 if ((map
[i
].dev_num
== dev
) && (map
[i
].direction
== dir
))
205 int sst_fill_stream_params(void *substream
,
206 const struct sst_data
*ctx
, struct snd_sst_params
*str_params
, bool is_compress
)
210 struct sst_dev_stream_map
*map
;
211 struct snd_pcm_substream
*pstream
= NULL
;
212 struct snd_compr_stream
*cstream
= NULL
;
214 map
= ctx
->pdata
->pdev_strm_map
;
215 map_size
= ctx
->pdata
->strm_map_size
;
217 if (is_compress
== true)
218 cstream
= (struct snd_compr_stream
*)substream
;
220 pstream
= (struct snd_pcm_substream
*)substream
;
222 str_params
->stream_type
= SST_STREAM_TYPE_MUSIC
;
224 /* For pcm streams */
226 index
= sst_get_stream_mapping(pstream
->pcm
->device
,
227 pstream
->number
, pstream
->stream
,
232 str_params
->stream_id
= index
;
233 str_params
->device_type
= map
[index
].device_id
;
234 str_params
->task
= map
[index
].task_id
;
236 str_params
->ops
= (u8
)pstream
->stream
;
240 index
= sst_get_stream_mapping(cstream
->device
->device
,
241 0, cstream
->direction
,
245 str_params
->stream_id
= index
;
246 str_params
->device_type
= map
[index
].device_id
;
247 str_params
->task
= map
[index
].task_id
;
249 str_params
->ops
= (u8
)cstream
->direction
;
254 static int sst_platform_alloc_stream(struct snd_pcm_substream
*substream
,
255 struct snd_soc_platform
*platform
)
257 struct sst_runtime_stream
*stream
=
258 substream
->runtime
->private_data
;
259 struct snd_sst_stream_params param
= {{{0,},},};
260 struct snd_sst_params str_params
= {0};
261 struct snd_sst_alloc_params_ext alloc_params
= {0};
263 struct sst_data
*ctx
= snd_soc_platform_get_drvdata(platform
);
265 /* set codec params and inform SST driver the same */
266 sst_fill_pcm_params(substream
, ¶m
);
267 sst_fill_alloc_params(substream
, &alloc_params
);
268 substream
->runtime
->dma_area
= substream
->dma_buffer
.area
;
269 str_params
.sparams
= param
;
270 str_params
.aparams
= alloc_params
;
271 str_params
.codec
= SST_CODEC_TYPE_PCM
;
273 /* fill the device type and stream id to pass to SST driver */
274 ret_val
= sst_fill_stream_params(substream
, ctx
, &str_params
, false);
278 stream
->stream_info
.str_id
= str_params
.stream_id
;
280 ret_val
= stream
->ops
->open(sst
->dev
, &str_params
);
288 static void sst_period_elapsed(void *arg
)
290 struct snd_pcm_substream
*substream
= arg
;
291 struct sst_runtime_stream
*stream
;
294 if (!substream
|| !substream
->runtime
)
296 stream
= substream
->runtime
->private_data
;
299 status
= sst_get_stream_status(stream
);
300 if (status
!= SST_PLATFORM_RUNNING
)
302 snd_pcm_period_elapsed(substream
);
305 static int sst_platform_init_stream(struct snd_pcm_substream
*substream
)
307 struct sst_runtime_stream
*stream
=
308 substream
->runtime
->private_data
;
309 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
312 dev_dbg(rtd
->dev
, "setting buffer ptr param\n");
313 sst_set_stream_status(stream
, SST_PLATFORM_INIT
);
314 stream
->stream_info
.period_elapsed
= sst_period_elapsed
;
315 stream
->stream_info
.arg
= substream
;
316 stream
->stream_info
.buffer_ptr
= 0;
317 stream
->stream_info
.sfreq
= substream
->runtime
->rate
;
318 ret_val
= stream
->ops
->stream_init(sst
->dev
, &stream
->stream_info
);
320 dev_err(rtd
->dev
, "control_set ret error %d\n", ret_val
);
325 static int sst_media_open(struct snd_pcm_substream
*substream
,
326 struct snd_soc_dai
*dai
)
329 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
330 struct sst_runtime_stream
*stream
;
332 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
335 spin_lock_init(&stream
->status_lock
);
337 /* get the sst ops */
338 mutex_lock(&sst_lock
);
340 !try_module_get(sst
->dev
->driver
->owner
)) {
341 dev_err(dai
->dev
, "no device available to run\n");
345 stream
->ops
= sst
->ops
;
346 mutex_unlock(&sst_lock
);
348 stream
->stream_info
.str_id
= 0;
350 stream
->stream_info
.arg
= substream
;
351 /* allocate memory for SST API set */
352 runtime
->private_data
= stream
;
354 /* Make sure, that the period size is always even */
355 snd_pcm_hw_constraint_step(substream
->runtime
, 0,
356 SNDRV_PCM_HW_PARAM_PERIODS
, 2);
358 return snd_pcm_hw_constraint_integer(runtime
,
359 SNDRV_PCM_HW_PARAM_PERIODS
);
362 mutex_unlock(&sst_lock
);
366 static void sst_media_close(struct snd_pcm_substream
*substream
,
367 struct snd_soc_dai
*dai
)
369 struct sst_runtime_stream
*stream
;
370 int ret_val
= 0, str_id
;
372 stream
= substream
->runtime
->private_data
;
373 str_id
= stream
->stream_info
.str_id
;
375 ret_val
= stream
->ops
->close(sst
->dev
, str_id
);
376 module_put(sst
->dev
->driver
->owner
);
380 static inline unsigned int get_current_pipe_id(struct snd_soc_platform
*platform
,
381 struct snd_pcm_substream
*substream
)
383 struct sst_data
*sst
= snd_soc_platform_get_drvdata(platform
);
384 struct sst_dev_stream_map
*map
= sst
->pdata
->pdev_strm_map
;
385 struct sst_runtime_stream
*stream
=
386 substream
->runtime
->private_data
;
387 u32 str_id
= stream
->stream_info
.str_id
;
388 unsigned int pipe_id
;
390 pipe_id
= map
[str_id
].device_id
;
392 dev_dbg(platform
->dev
, "got pipe_id = %#x for str_id = %d\n",
397 static int sst_media_prepare(struct snd_pcm_substream
*substream
,
398 struct snd_soc_dai
*dai
)
400 struct sst_runtime_stream
*stream
;
401 int ret_val
= 0, str_id
;
403 stream
= substream
->runtime
->private_data
;
404 str_id
= stream
->stream_info
.str_id
;
405 if (stream
->stream_info
.str_id
) {
406 ret_val
= stream
->ops
->stream_drop(sst
->dev
, str_id
);
410 ret_val
= sst_platform_alloc_stream(substream
, dai
->platform
);
413 snprintf(substream
->pcm
->id
, sizeof(substream
->pcm
->id
),
414 "%d", stream
->stream_info
.str_id
);
416 ret_val
= sst_platform_init_stream(substream
);
419 substream
->runtime
->hw
.info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
423 static int sst_media_hw_params(struct snd_pcm_substream
*substream
,
424 struct snd_pcm_hw_params
*params
,
425 struct snd_soc_dai
*dai
)
427 snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
428 memset(substream
->runtime
->dma_area
, 0, params_buffer_bytes(params
));
432 static int sst_media_hw_free(struct snd_pcm_substream
*substream
,
433 struct snd_soc_dai
*dai
)
435 return snd_pcm_lib_free_pages(substream
);
438 static struct snd_soc_dai_ops sst_media_dai_ops
= {
439 .startup
= sst_media_open
,
440 .shutdown
= sst_media_close
,
441 .prepare
= sst_media_prepare
,
442 .hw_params
= sst_media_hw_params
,
443 .hw_free
= sst_media_hw_free
,
446 static int sst_platform_open(struct snd_pcm_substream
*substream
)
448 struct snd_pcm_runtime
*runtime
;
450 if (substream
->pcm
->internal
)
453 runtime
= substream
->runtime
;
454 runtime
->hw
= sst_platform_pcm_hw
;
458 static int sst_platform_pcm_trigger(struct snd_pcm_substream
*substream
,
461 int ret_val
= 0, str_id
;
462 struct sst_runtime_stream
*stream
;
464 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
466 dev_dbg(rtd
->dev
, "sst_platform_pcm_trigger called\n");
467 if (substream
->pcm
->internal
)
469 stream
= substream
->runtime
->private_data
;
470 str_id
= stream
->stream_info
.str_id
;
472 case SNDRV_PCM_TRIGGER_START
:
473 dev_dbg(rtd
->dev
, "sst: Trigger Start\n");
474 status
= SST_PLATFORM_RUNNING
;
475 stream
->stream_info
.arg
= substream
;
476 ret_val
= stream
->ops
->stream_start(sst
->dev
, str_id
);
478 case SNDRV_PCM_TRIGGER_STOP
:
479 dev_dbg(rtd
->dev
, "sst: in stop\n");
480 status
= SST_PLATFORM_DROPPED
;
481 ret_val
= stream
->ops
->stream_drop(sst
->dev
, str_id
);
483 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
484 dev_dbg(rtd
->dev
, "sst: in pause\n");
485 status
= SST_PLATFORM_PAUSED
;
486 ret_val
= stream
->ops
->stream_pause(sst
->dev
, str_id
);
488 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
489 dev_dbg(rtd
->dev
, "sst: in pause release\n");
490 status
= SST_PLATFORM_RUNNING
;
491 ret_val
= stream
->ops
->stream_pause_release(sst
->dev
, str_id
);
498 sst_set_stream_status(stream
, status
);
504 static snd_pcm_uframes_t sst_platform_pcm_pointer
505 (struct snd_pcm_substream
*substream
)
507 struct sst_runtime_stream
*stream
;
509 struct pcm_stream_info
*str_info
;
510 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
512 stream
= substream
->runtime
->private_data
;
513 status
= sst_get_stream_status(stream
);
514 if (status
== SST_PLATFORM_INIT
)
516 str_info
= &stream
->stream_info
;
517 ret_val
= stream
->ops
->stream_read_tstamp(sst
->dev
, str_info
);
519 dev_err(rtd
->dev
, "sst: error code = %d\n", ret_val
);
522 substream
->runtime
->delay
= str_info
->pcm_delay
;
523 return str_info
->buffer_ptr
;
526 static struct snd_pcm_ops sst_platform_ops
= {
527 .open
= sst_platform_open
,
528 .ioctl
= snd_pcm_lib_ioctl
,
529 .trigger
= sst_platform_pcm_trigger
,
530 .pointer
= sst_platform_pcm_pointer
,
533 static void sst_pcm_free(struct snd_pcm
*pcm
)
535 dev_dbg(pcm
->dev
, "sst_pcm_free called\n");
536 snd_pcm_lib_preallocate_free_for_all(pcm
);
539 static int sst_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
541 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
542 struct snd_pcm
*pcm
= rtd
->pcm
;
545 if (dai
->driver
->playback
.channels_min
||
546 dai
->driver
->capture
.channels_min
) {
547 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
548 SNDRV_DMA_TYPE_CONTINUOUS
,
549 snd_dma_continuous_data(GFP_DMA
),
550 SST_MIN_BUFFER
, SST_MAX_BUFFER
);
552 dev_err(rtd
->dev
, "dma buffer allocationf fail\n");
559 static int sst_soc_probe(struct snd_soc_platform
*platform
)
561 return sst_dsp_init_v2_dpcm(platform
);
564 static struct snd_soc_platform_driver sst_soc_platform_drv
= {
565 .probe
= sst_soc_probe
,
566 .ops
= &sst_platform_ops
,
567 .compr_ops
= &sst_platform_compr_ops
,
568 .pcm_new
= sst_pcm_new
,
569 .pcm_free
= sst_pcm_free
,
572 static const struct snd_soc_component_driver sst_component
= {
577 static int sst_platform_probe(struct platform_device
*pdev
)
579 struct sst_data
*drv
;
581 struct sst_platform_data
*pdata
;
583 drv
= devm_kzalloc(&pdev
->dev
, sizeof(*drv
), GFP_KERNEL
);
588 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
593 pdata
->pdev_strm_map
= dpcm_strm_map
;
594 pdata
->strm_map_size
= ARRAY_SIZE(dpcm_strm_map
);
596 mutex_init(&drv
->lock
);
597 dev_set_drvdata(&pdev
->dev
, drv
);
599 ret
= snd_soc_register_platform(&pdev
->dev
, &sst_soc_platform_drv
);
601 dev_err(&pdev
->dev
, "registering soc platform failed\n");
605 ret
= snd_soc_register_component(&pdev
->dev
, &sst_component
,
606 sst_platform_dai
, ARRAY_SIZE(sst_platform_dai
));
608 dev_err(&pdev
->dev
, "registering cpu dais failed\n");
609 snd_soc_unregister_platform(&pdev
->dev
);
614 static int sst_platform_remove(struct platform_device
*pdev
)
617 snd_soc_unregister_component(&pdev
->dev
);
618 snd_soc_unregister_platform(&pdev
->dev
);
619 dev_dbg(&pdev
->dev
, "sst_platform_remove success\n");
623 static struct platform_driver sst_platform_driver
= {
625 .name
= "sst-mfld-platform",
626 .owner
= THIS_MODULE
,
628 .probe
= sst_platform_probe
,
629 .remove
= sst_platform_remove
,
632 module_platform_driver(sst_platform_driver
);
634 MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
635 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
636 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
637 MODULE_LICENSE("GPL v2");
638 MODULE_ALIAS("platform:sst-mfld-platform");