2 * sst_mfld_platform.c - Intel MID Platform driver
4 * Copyright (C) 2010-2013 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 "sst-mfld-platform.h"
32 struct sst_device
*sst
;
33 static DEFINE_MUTEX(sst_lock
);
34 extern struct snd_compr_ops sst_platform_compr_ops
;
36 int sst_register_dsp(struct sst_device
*dev
)
40 if (!try_module_get(dev
->dev
->driver
->owner
))
42 mutex_lock(&sst_lock
);
44 pr_err("we already have a device %s\n", sst
->name
);
45 module_put(dev
->dev
->driver
->owner
);
46 mutex_unlock(&sst_lock
);
49 pr_debug("registering device %s\n", dev
->name
);
51 mutex_unlock(&sst_lock
);
54 EXPORT_SYMBOL_GPL(sst_register_dsp
);
56 int sst_unregister_dsp(struct sst_device
*dev
)
63 mutex_lock(&sst_lock
);
66 mutex_unlock(&sst_lock
);
70 module_put(sst
->dev
->driver
->owner
);
71 pr_debug("unreg %s\n", sst
->name
);
73 mutex_unlock(&sst_lock
);
76 EXPORT_SYMBOL_GPL(sst_unregister_dsp
);
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
|
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
,
96 static struct snd_soc_dai_driver sst_platform_dai
[] = {
98 .name
= "Headset-cpu-dai",
101 .channels_min
= SST_STEREO
,
102 .channels_max
= SST_STEREO
,
103 .rates
= SNDRV_PCM_RATE_48000
,
104 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
109 .rates
= SNDRV_PCM_RATE_48000
,
110 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
114 .name
= "Compress-cpu-dai",
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
,
125 /* helper functions */
126 void sst_set_stream_status(struct sst_runtime_stream
*stream
,
130 spin_lock_irqsave(&stream
->status_lock
, flags
);
131 stream
->stream_status
= state
;
132 spin_unlock_irqrestore(&stream
->status_lock
, flags
);
135 static inline int sst_get_stream_status(struct sst_runtime_stream
*stream
)
140 spin_lock_irqsave(&stream
->status_lock
, flags
);
141 state
= stream
->stream_status
;
142 spin_unlock_irqrestore(&stream
->status_lock
, flags
);
146 static void sst_fill_pcm_params(struct snd_pcm_substream
*substream
,
147 struct sst_pcm_params
*param
)
150 param
->num_chan
= (u8
) substream
->runtime
->channels
;
151 param
->pcm_wd_sz
= substream
->runtime
->sample_bits
;
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
);
161 static int sst_platform_alloc_stream(struct snd_pcm_substream
*substream
)
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};
169 /* set codec params and inform SST driver the same */
170 sst_fill_pcm_params(substream
, ¶m
);
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
);
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
);
185 ret_val
= stream
->ops
->open(&str_params
);
186 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val
);
190 stream
->stream_info
.str_id
= ret_val
;
191 pr_debug("str id : %d\n", stream
->stream_info
.str_id
);
195 static void sst_period_elapsed(void *arg
)
197 struct snd_pcm_substream
*substream
= arg
;
198 struct sst_runtime_stream
*stream
;
201 if (!substream
|| !substream
->runtime
)
203 stream
= substream
->runtime
->private_data
;
206 status
= sst_get_stream_status(stream
);
207 if (status
!= SST_PLATFORM_RUNNING
)
209 snd_pcm_period_elapsed(substream
);
212 static int sst_platform_init_stream(struct snd_pcm_substream
*substream
)
214 struct sst_runtime_stream
*stream
=
215 substream
->runtime
->private_data
;
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
.arg
= 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
);
227 pr_err("control_set ret error %d\n", ret_val
);
231 /* end -- helper functions */
233 static int sst_media_open(struct snd_pcm_substream
*substream
,
234 struct snd_soc_dai
*dai
)
237 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
238 struct sst_runtime_stream
*stream
;
240 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
243 spin_lock_init(&stream
->status_lock
);
245 /* get the sst ops */
246 mutex_lock(&sst_lock
);
248 !try_module_get(sst
->dev
->driver
->owner
)) {
249 pr_err("no device available to run\n");
253 stream
->ops
= sst
->ops
;
254 mutex_unlock(&sst_lock
);
256 stream
->stream_info
.str_id
= 0;
258 stream
->stream_info
.arg
= substream
;
259 /* allocate memory for SST API set */
260 runtime
->private_data
= stream
;
262 /* Make sure, that the period size is always even */
263 snd_pcm_hw_constraint_step(substream
->runtime
, 0,
264 SNDRV_PCM_HW_PARAM_PERIODS
, 2);
266 return snd_pcm_hw_constraint_integer(runtime
,
267 SNDRV_PCM_HW_PARAM_PERIODS
);
270 mutex_unlock(&sst_lock
);
274 static void sst_media_close(struct snd_pcm_substream
*substream
,
275 struct snd_soc_dai
*dai
)
277 struct sst_runtime_stream
*stream
;
278 int ret_val
= 0, str_id
;
280 stream
= substream
->runtime
->private_data
;
281 str_id
= stream
->stream_info
.str_id
;
283 ret_val
= stream
->ops
->close(str_id
);
284 module_put(sst
->dev
->driver
->owner
);
289 static int sst_media_prepare(struct snd_pcm_substream
*substream
,
290 struct snd_soc_dai
*dai
)
292 struct sst_runtime_stream
*stream
;
293 int ret_val
= 0, str_id
;
295 stream
= substream
->runtime
->private_data
;
296 str_id
= stream
->stream_info
.str_id
;
297 if (stream
->stream_info
.str_id
) {
298 ret_val
= stream
->ops
->device_control(
299 SST_SND_DROP
, &str_id
);
303 ret_val
= sst_platform_alloc_stream(substream
);
306 snprintf(substream
->pcm
->id
, sizeof(substream
->pcm
->id
),
307 "%d", stream
->stream_info
.str_id
);
309 ret_val
= sst_platform_init_stream(substream
);
312 substream
->runtime
->hw
.info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
;
316 static int sst_media_hw_params(struct snd_pcm_substream
*substream
,
317 struct snd_pcm_hw_params
*params
,
318 struct snd_soc_dai
*dai
)
320 snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(params
));
321 memset(substream
->runtime
->dma_area
, 0, params_buffer_bytes(params
));
325 static int sst_media_hw_free(struct snd_pcm_substream
*substream
,
326 struct snd_soc_dai
*dai
)
328 return snd_pcm_lib_free_pages(substream
);
331 static struct snd_soc_dai_ops sst_media_dai_ops
= {
332 .startup
= sst_media_open
,
333 .shutdown
= sst_media_close
,
334 .prepare
= sst_media_prepare
,
335 .hw_params
= sst_media_hw_params
,
336 .hw_free
= sst_media_hw_free
,
339 static int sst_platform_open(struct snd_pcm_substream
*substream
)
341 struct snd_pcm_runtime
*runtime
;
343 if (substream
->pcm
->internal
)
346 runtime
= substream
->runtime
;
347 runtime
->hw
= sst_platform_pcm_hw
;
351 static int sst_platform_pcm_trigger(struct snd_pcm_substream
*substream
,
354 int ret_val
= 0, str_id
;
355 struct sst_runtime_stream
*stream
;
358 pr_debug("sst_platform_pcm_trigger called\n");
359 stream
= substream
->runtime
->private_data
;
360 str_id
= stream
->stream_info
.str_id
;
362 case SNDRV_PCM_TRIGGER_START
:
363 pr_debug("sst: Trigger Start\n");
364 str_cmd
= SST_SND_START
;
365 status
= SST_PLATFORM_RUNNING
;
366 stream
->stream_info
.arg
= substream
;
368 case SNDRV_PCM_TRIGGER_STOP
:
369 pr_debug("sst: in stop\n");
370 str_cmd
= SST_SND_DROP
;
371 status
= SST_PLATFORM_DROPPED
;
373 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
374 pr_debug("sst: in pause\n");
375 str_cmd
= SST_SND_PAUSE
;
376 status
= SST_PLATFORM_PAUSED
;
378 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
379 pr_debug("sst: in pause release\n");
380 str_cmd
= SST_SND_RESUME
;
381 status
= SST_PLATFORM_RUNNING
;
386 ret_val
= stream
->ops
->device_control(str_cmd
, &str_id
);
388 sst_set_stream_status(stream
, status
);
394 static snd_pcm_uframes_t sst_platform_pcm_pointer
395 (struct snd_pcm_substream
*substream
)
397 struct sst_runtime_stream
*stream
;
399 struct pcm_stream_info
*str_info
;
401 stream
= substream
->runtime
->private_data
;
402 status
= sst_get_stream_status(stream
);
403 if (status
== SST_PLATFORM_INIT
)
405 str_info
= &stream
->stream_info
;
406 ret_val
= stream
->ops
->device_control(
407 SST_SND_BUFFER_POINTER
, str_info
);
409 pr_err("sst: error code = %d\n", ret_val
);
412 return str_info
->buffer_ptr
;
415 static struct snd_pcm_ops sst_platform_ops
= {
416 .open
= sst_platform_open
,
417 .ioctl
= snd_pcm_lib_ioctl
,
418 .trigger
= sst_platform_pcm_trigger
,
419 .pointer
= sst_platform_pcm_pointer
,
422 static void sst_pcm_free(struct snd_pcm
*pcm
)
424 pr_debug("sst_pcm_free called\n");
425 snd_pcm_lib_preallocate_free_for_all(pcm
);
428 static int sst_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
430 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
431 struct snd_pcm
*pcm
= rtd
->pcm
;
434 if (dai
->driver
->playback
.channels_min
||
435 dai
->driver
->capture
.channels_min
) {
436 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
437 SNDRV_DMA_TYPE_CONTINUOUS
,
438 snd_dma_continuous_data(GFP_DMA
),
439 SST_MIN_BUFFER
, SST_MAX_BUFFER
);
441 pr_err("dma buffer allocationf fail\n");
448 static struct snd_soc_platform_driver sst_soc_platform_drv
= {
449 .ops
= &sst_platform_ops
,
450 .compr_ops
= &sst_platform_compr_ops
,
451 .pcm_new
= sst_pcm_new
,
452 .pcm_free
= sst_pcm_free
,
455 static const struct snd_soc_component_driver sst_component
= {
460 static int sst_platform_probe(struct platform_device
*pdev
)
464 pr_debug("sst_platform_probe called\n");
466 ret
= snd_soc_register_platform(&pdev
->dev
, &sst_soc_platform_drv
);
468 pr_err("registering soc platform failed\n");
472 ret
= snd_soc_register_component(&pdev
->dev
, &sst_component
,
473 sst_platform_dai
, ARRAY_SIZE(sst_platform_dai
));
475 pr_err("registering cpu dais failed\n");
476 snd_soc_unregister_platform(&pdev
->dev
);
481 static int sst_platform_remove(struct platform_device
*pdev
)
484 snd_soc_unregister_component(&pdev
->dev
);
485 snd_soc_unregister_platform(&pdev
->dev
);
486 pr_debug("sst_platform_remove success\n");
490 static struct platform_driver sst_platform_driver
= {
492 .name
= "sst-mfld-platform",
493 .owner
= THIS_MODULE
,
495 .probe
= sst_platform_probe
,
496 .remove
= sst_platform_remove
,
499 module_platform_driver(sst_platform_driver
);
501 MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
502 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
503 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
504 MODULE_LICENSE("GPL v2");
505 MODULE_ALIAS("platform:sst-mfld-platform");