Commit | Line | Data |
---|---|---|
d927fdae VK |
1 | /* |
2 | * sst_platform.c - Intel MID Platform driver | |
3 | * | |
4 | * Copyright (C) 2010 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 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | * | |
22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
23 | * | |
24 | * | |
25 | */ | |
26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
27 | ||
28 | #include <linux/slab.h> | |
29 | #include <linux/io.h> | |
30 | #include <sound/core.h> | |
31 | #include <sound/pcm.h> | |
32 | #include <sound/pcm_params.h> | |
33 | #include <sound/soc.h> | |
34 | #include "../../../drivers/staging/intel_sst/intel_sst_ioctl.h" | |
35 | #include "../../../drivers/staging/intel_sst/intel_sst.h" | |
36 | #include "sst_platform.h" | |
37 | ||
38 | static struct snd_pcm_hardware sst_platform_pcm_hw = { | |
39 | .info = (SNDRV_PCM_INFO_INTERLEAVED | | |
40 | SNDRV_PCM_INFO_DOUBLE | | |
41 | SNDRV_PCM_INFO_PAUSE | | |
42 | SNDRV_PCM_INFO_RESUME | | |
43 | SNDRV_PCM_INFO_MMAP| | |
44 | SNDRV_PCM_INFO_MMAP_VALID | | |
45 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
46 | SNDRV_PCM_INFO_SYNC_START), | |
47 | .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 | | |
48 | SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 | | |
49 | SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32), | |
50 | .rates = (SNDRV_PCM_RATE_8000| | |
51 | SNDRV_PCM_RATE_44100 | | |
52 | SNDRV_PCM_RATE_48000), | |
53 | .rate_min = SST_MIN_RATE, | |
54 | .rate_max = SST_MAX_RATE, | |
55 | .channels_min = SST_MIN_CHANNEL, | |
56 | .channels_max = SST_MAX_CHANNEL, | |
57 | .buffer_bytes_max = SST_MAX_BUFFER, | |
58 | .period_bytes_min = SST_MIN_PERIOD_BYTES, | |
59 | .period_bytes_max = SST_MAX_PERIOD_BYTES, | |
60 | .periods_min = SST_MIN_PERIODS, | |
61 | .periods_max = SST_MAX_PERIODS, | |
62 | .fifo_size = SST_FIFO_SIZE, | |
63 | }; | |
64 | ||
65 | /* MFLD - MSIC */ | |
66 | struct snd_soc_dai_driver sst_platform_dai[] = { | |
67 | { | |
68 | .name = "Headset-cpu-dai", | |
69 | .id = 0, | |
70 | .playback = { | |
71 | .channels_min = SST_STEREO, | |
72 | .channels_max = SST_STEREO, | |
73 | .rates = SNDRV_PCM_RATE_48000, | |
74 | .formats = SNDRV_PCM_FMTBIT_S24_LE, | |
75 | }, | |
76 | }, | |
77 | { | |
78 | .name = "Speaker-cpu-dai", | |
79 | .id = 1, | |
80 | .playback = { | |
81 | .channels_min = SST_MONO, | |
82 | .channels_max = SST_STEREO, | |
83 | .rates = SNDRV_PCM_RATE_48000, | |
84 | .formats = SNDRV_PCM_FMTBIT_S24_LE, | |
85 | }, | |
86 | }, | |
87 | { | |
88 | .name = "Vibra1-cpu-dai", | |
89 | .id = 2, | |
90 | .playback = { | |
91 | .channels_min = SST_MONO, | |
92 | .channels_max = SST_MONO, | |
93 | .rates = SNDRV_PCM_RATE_48000, | |
94 | .formats = SNDRV_PCM_FMTBIT_S24_LE, | |
95 | }, | |
96 | }, | |
97 | { | |
98 | .name = "Vibra2-cpu-dai", | |
99 | .id = 3, | |
100 | .playback = { | |
101 | .channels_min = SST_MONO, | |
102 | .channels_max = SST_STEREO, | |
103 | .rates = SNDRV_PCM_RATE_48000, | |
104 | .formats = SNDRV_PCM_FMTBIT_S24_LE, | |
105 | }, | |
106 | }, | |
107 | }; | |
5c68536f | 108 | |
d927fdae | 109 | /* helper functions */ |
5c68536f HP |
110 | static inline void sst_set_stream_status(struct sst_runtime_stream *stream, |
111 | int state) | |
112 | { | |
113 | spin_lock(&stream->status_lock); | |
114 | stream->stream_status = state; | |
115 | spin_unlock(&stream->status_lock); | |
116 | } | |
117 | ||
118 | static inline int sst_get_stream_status(struct sst_runtime_stream *stream) | |
119 | { | |
120 | int state; | |
121 | ||
122 | spin_lock(&stream->status_lock); | |
123 | state = stream->stream_status; | |
124 | spin_unlock(&stream->status_lock); | |
125 | return state; | |
126 | } | |
127 | ||
128 | static void sst_fill_pcm_params(struct snd_pcm_substream *substream, | |
129 | struct snd_sst_stream_params *param) | |
130 | { | |
131 | ||
132 | param->uc.pcm_params.codec = SST_CODEC_TYPE_PCM; | |
133 | param->uc.pcm_params.num_chan = (u8) substream->runtime->channels; | |
134 | param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits; | |
135 | param->uc.pcm_params.reserved = 0; | |
136 | param->uc.pcm_params.sfreq = substream->runtime->rate; | |
137 | param->uc.pcm_params.ring_buffer_size = | |
138 | snd_pcm_lib_buffer_bytes(substream); | |
139 | param->uc.pcm_params.period_count = substream->runtime->period_size; | |
140 | param->uc.pcm_params.ring_buffer_addr = | |
141 | virt_to_phys(substream->dma_buffer.area); | |
142 | pr_debug("period_cnt = %d\n", param->uc.pcm_params.period_count); | |
143 | pr_debug("sfreq= %d, wd_sz = %d\n", | |
144 | param->uc.pcm_params.sfreq, param->uc.pcm_params.pcm_wd_sz); | |
145 | } | |
146 | ||
d927fdae VK |
147 | static int sst_platform_alloc_stream(struct snd_pcm_substream *substream) |
148 | { | |
149 | struct sst_runtime_stream *stream = | |
150 | substream->runtime->private_data; | |
151 | struct snd_sst_stream_params param = {{{0,},},}; | |
152 | struct snd_sst_params str_params = {0}; | |
153 | int ret_val; | |
154 | ||
155 | /* set codec params and inform SST driver the same */ | |
5c68536f | 156 | sst_fill_pcm_params(substream, ¶m); |
d927fdae | 157 | substream->runtime->dma_area = substream->dma_buffer.area; |
d927fdae | 158 | str_params.sparams = param; |
5c68536f | 159 | str_params.codec = param.uc.pcm_params.codec; |
d927fdae VK |
160 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
161 | str_params.ops = STREAM_OPS_PLAYBACK; | |
162 | str_params.device_type = substream->pcm->device + 1; | |
163 | pr_debug("Playbck stream,Device %d\n", | |
164 | substream->pcm->device); | |
165 | } else { | |
166 | str_params.ops = STREAM_OPS_CAPTURE; | |
167 | str_params.device_type = SND_SST_DEVICE_CAPTURE; | |
168 | pr_debug("Capture stream,Device %d\n", | |
169 | substream->pcm->device); | |
170 | } | |
171 | ret_val = stream->sstdrv_ops->control_set(SST_SND_ALLOC, &str_params); | |
172 | pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val); | |
173 | if (ret_val < 0) | |
174 | return ret_val; | |
175 | ||
176 | stream->stream_info.str_id = ret_val; | |
177 | pr_debug("str id : %d\n", stream->stream_info.str_id); | |
d927fdae VK |
178 | return ret_val; |
179 | } | |
180 | ||
d927fdae VK |
181 | static void sst_period_elapsed(void *mad_substream) |
182 | { | |
183 | struct snd_pcm_substream *substream = mad_substream; | |
184 | struct sst_runtime_stream *stream; | |
5c68536f | 185 | int status; |
d927fdae VK |
186 | |
187 | if (!substream || !substream->runtime) | |
188 | return; | |
189 | stream = substream->runtime->private_data; | |
190 | if (!stream) | |
191 | return; | |
5c68536f HP |
192 | status = sst_get_stream_status(stream); |
193 | if (status != SST_PLATFORM_RUNNING) | |
d927fdae | 194 | return; |
d927fdae | 195 | snd_pcm_period_elapsed(substream); |
d927fdae VK |
196 | } |
197 | ||
198 | static int sst_platform_init_stream(struct snd_pcm_substream *substream) | |
199 | { | |
200 | struct sst_runtime_stream *stream = | |
201 | substream->runtime->private_data; | |
202 | int ret_val; | |
203 | ||
204 | pr_debug("setting buffer ptr param\n"); | |
5c68536f | 205 | sst_set_stream_status(stream, SST_PLATFORM_INIT); |
d927fdae VK |
206 | stream->stream_info.period_elapsed = sst_period_elapsed; |
207 | stream->stream_info.mad_substream = substream; | |
208 | stream->stream_info.buffer_ptr = 0; | |
209 | stream->stream_info.sfreq = substream->runtime->rate; | |
210 | ret_val = stream->sstdrv_ops->control_set(SST_SND_STREAM_INIT, | |
211 | &stream->stream_info); | |
212 | if (ret_val) | |
213 | pr_err("control_set ret error %d\n", ret_val); | |
214 | return ret_val; | |
215 | ||
216 | } | |
217 | /* end -- helper functions */ | |
218 | ||
219 | static int sst_platform_open(struct snd_pcm_substream *substream) | |
220 | { | |
221 | struct snd_pcm_runtime *runtime; | |
222 | struct sst_runtime_stream *stream; | |
223 | int ret_val = 0; | |
224 | ||
225 | pr_debug("sst_platform_open called\n"); | |
d927fdae VK |
226 | runtime = substream->runtime; |
227 | runtime->hw = sst_platform_pcm_hw; | |
d927fdae VK |
228 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
229 | if (!stream) | |
230 | return -ENOMEM; | |
d927fdae VK |
231 | spin_lock_init(&stream->status_lock); |
232 | stream->stream_info.str_id = 0; | |
5c68536f | 233 | sst_set_stream_status(stream, SST_PLATFORM_INIT); |
d927fdae VK |
234 | stream->stream_info.mad_substream = substream; |
235 | /* allocate memory for SST API set */ | |
236 | stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops), | |
237 | GFP_KERNEL); | |
238 | if (!stream->sstdrv_ops) { | |
239 | pr_err("sst: mem allocation for ops fail\n"); | |
240 | kfree(stream); | |
241 | return -ENOMEM; | |
242 | } | |
243 | stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID; | |
d927fdae VK |
244 | /* registering with SST driver to get access to SST APIs to use */ |
245 | ret_val = register_sst_card(stream->sstdrv_ops); | |
246 | if (ret_val) { | |
247 | pr_err("sst: sst card registration failed\n"); | |
248 | return ret_val; | |
249 | } | |
250 | runtime->private_data = stream; | |
251 | return snd_pcm_hw_constraint_integer(runtime, | |
252 | SNDRV_PCM_HW_PARAM_PERIODS); | |
253 | } | |
254 | ||
255 | static int sst_platform_close(struct snd_pcm_substream *substream) | |
256 | { | |
257 | struct sst_runtime_stream *stream; | |
258 | int ret_val = 0, str_id; | |
259 | ||
260 | pr_debug("sst_platform_close called\n"); | |
d927fdae VK |
261 | stream = substream->runtime->private_data; |
262 | str_id = stream->stream_info.str_id; | |
d927fdae VK |
263 | if (str_id) |
264 | ret_val = stream->sstdrv_ops->control_set( | |
265 | SST_SND_FREE, &str_id); | |
d927fdae VK |
266 | kfree(stream->sstdrv_ops); |
267 | kfree(stream); | |
268 | return ret_val; | |
269 | } | |
270 | ||
271 | static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream) | |
272 | { | |
273 | struct sst_runtime_stream *stream; | |
274 | int ret_val = 0, str_id; | |
275 | ||
276 | pr_debug("sst_platform_pcm_prepare called\n"); | |
d927fdae VK |
277 | stream = substream->runtime->private_data; |
278 | str_id = stream->stream_info.str_id; | |
279 | if (stream->stream_info.str_id) { | |
280 | ret_val = stream->sstdrv_ops->control_set( | |
281 | SST_SND_DROP, &str_id); | |
282 | return ret_val; | |
283 | } | |
284 | ||
285 | ret_val = sst_platform_alloc_stream(substream); | |
286 | if (ret_val < 0) | |
287 | return ret_val; | |
288 | snprintf(substream->pcm->id, sizeof(substream->pcm->id), | |
289 | "%d", stream->stream_info.str_id); | |
290 | ||
291 | ret_val = sst_platform_init_stream(substream); | |
292 | if (ret_val) | |
293 | return ret_val; | |
294 | substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER; | |
295 | return ret_val; | |
296 | } | |
297 | ||
298 | static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream, | |
299 | int cmd) | |
300 | { | |
301 | int ret_val = 0, str_id; | |
302 | struct sst_runtime_stream *stream; | |
303 | ||
304 | pr_debug("sst_platform_pcm_trigger called\n"); | |
d927fdae | 305 | stream = substream->runtime->private_data; |
d927fdae | 306 | str_id = stream->stream_info.str_id; |
d927fdae VK |
307 | switch (cmd) { |
308 | case SNDRV_PCM_TRIGGER_START: | |
309 | pr_debug("sst: Trigger Start\n"); | |
310 | ret_val = stream->sstdrv_ops->control_set( | |
311 | SST_SND_START, &str_id); | |
312 | if (ret_val) | |
313 | break; | |
5c68536f | 314 | sst_set_stream_status(stream, SST_PLATFORM_RUNNING); |
d927fdae VK |
315 | stream->stream_info.mad_substream = substream; |
316 | break; | |
317 | case SNDRV_PCM_TRIGGER_STOP: | |
318 | pr_debug("sst: in stop\n"); | |
319 | ret_val = stream->sstdrv_ops->control_set( | |
320 | SST_SND_DROP, &str_id); | |
321 | if (ret_val) | |
322 | break; | |
5c68536f | 323 | sst_set_stream_status(stream, SST_PLATFORM_DROPPED); |
d927fdae VK |
324 | break; |
325 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
326 | pr_debug("sst: in pause\n"); | |
327 | ret_val = stream->sstdrv_ops->control_set( | |
328 | SST_SND_PAUSE, &str_id); | |
329 | if (ret_val) | |
330 | break; | |
5c68536f | 331 | sst_set_stream_status(stream, SST_PLATFORM_PAUSED); |
d927fdae VK |
332 | break; |
333 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
334 | pr_debug("sst: in pause release\n"); | |
335 | ret_val = stream->sstdrv_ops->control_set( | |
336 | SST_SND_RESUME, &str_id); | |
337 | if (ret_val) | |
338 | break; | |
5c68536f | 339 | sst_set_stream_status(stream, SST_PLATFORM_RUNNING); |
d927fdae VK |
340 | break; |
341 | default: | |
342 | ret_val = -EINVAL; | |
343 | } | |
344 | return ret_val; | |
345 | } | |
346 | ||
347 | ||
348 | static snd_pcm_uframes_t sst_platform_pcm_pointer | |
349 | (struct snd_pcm_substream *substream) | |
350 | { | |
351 | struct sst_runtime_stream *stream; | |
5c68536f | 352 | int ret_val, status; |
d927fdae VK |
353 | struct pcm_stream_info *str_info; |
354 | ||
d927fdae | 355 | stream = substream->runtime->private_data; |
5c68536f HP |
356 | status = sst_get_stream_status(stream); |
357 | if (status == SST_PLATFORM_INIT) | |
d927fdae | 358 | return 0; |
d927fdae VK |
359 | str_info = &stream->stream_info; |
360 | ret_val = stream->sstdrv_ops->control_set( | |
361 | SST_SND_BUFFER_POINTER, str_info); | |
362 | if (ret_val) { | |
363 | pr_err("sst: error code = %d\n", ret_val); | |
364 | return ret_val; | |
365 | } | |
d927fdae VK |
366 | return stream->stream_info.buffer_ptr; |
367 | } | |
368 | ||
369 | ||
370 | static struct snd_pcm_ops sst_platform_ops = { | |
371 | .open = sst_platform_open, | |
372 | .close = sst_platform_close, | |
373 | .ioctl = snd_pcm_lib_ioctl, | |
374 | .prepare = sst_platform_pcm_prepare, | |
375 | .trigger = sst_platform_pcm_trigger, | |
376 | .pointer = sst_platform_pcm_pointer, | |
377 | }; | |
378 | ||
379 | static void sst_pcm_free(struct snd_pcm *pcm) | |
380 | { | |
381 | pr_debug("sst_pcm_free called\n"); | |
382 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
383 | } | |
384 | ||
385 | int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai, | |
386 | struct snd_pcm *pcm) | |
387 | { | |
388 | int retval = 0; | |
389 | ||
390 | pr_debug("sst_pcm_new called\n"); | |
d927fdae VK |
391 | if (dai->driver->playback.channels_min || |
392 | dai->driver->capture.channels_min) { | |
393 | retval = snd_pcm_lib_preallocate_pages_for_all(pcm, | |
394 | SNDRV_DMA_TYPE_CONTINUOUS, | |
395 | snd_dma_continuous_data(GFP_KERNEL), | |
396 | SST_MIN_BUFFER, SST_MAX_BUFFER); | |
397 | if (retval) { | |
398 | pr_err("dma buffer allocationf fail\n"); | |
399 | return retval; | |
400 | } | |
401 | } | |
d927fdae VK |
402 | return retval; |
403 | } | |
404 | struct snd_soc_platform_driver sst_soc_platform_drv = { | |
405 | .ops = &sst_platform_ops, | |
406 | .pcm_new = sst_pcm_new, | |
407 | .pcm_free = sst_pcm_free, | |
408 | }; | |
409 | ||
410 | static int sst_platform_probe(struct platform_device *pdev) | |
411 | { | |
412 | int ret; | |
413 | ||
414 | pr_debug("sst_platform_probe called\n"); | |
415 | ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv); | |
416 | if (ret) { | |
417 | pr_err("registering soc platform failed\n"); | |
418 | return ret; | |
419 | } | |
5c68536f | 420 | |
d927fdae VK |
421 | ret = snd_soc_register_dais(&pdev->dev, |
422 | sst_platform_dai, ARRAY_SIZE(sst_platform_dai)); | |
423 | if (ret) { | |
424 | pr_err("registering cpu dais failed\n"); | |
425 | snd_soc_unregister_platform(&pdev->dev); | |
426 | } | |
427 | return ret; | |
428 | } | |
429 | ||
430 | static int sst_platform_remove(struct platform_device *pdev) | |
431 | { | |
432 | ||
433 | snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai)); | |
d927fdae VK |
434 | snd_soc_unregister_platform(&pdev->dev); |
435 | pr_debug("sst_platform_remove sucess\n"); | |
d927fdae VK |
436 | return 0; |
437 | } | |
438 | ||
439 | static struct platform_driver sst_platform_driver = { | |
440 | .driver = { | |
441 | .name = "sst-platform", | |
442 | .owner = THIS_MODULE, | |
443 | }, | |
444 | .probe = sst_platform_probe, | |
445 | .remove = sst_platform_remove, | |
446 | }; | |
447 | ||
448 | static int __init sst_soc_platform_init(void) | |
449 | { | |
450 | pr_debug("sst_soc_platform_init called\n"); | |
451 | return platform_driver_register(&sst_platform_driver); | |
452 | } | |
453 | module_init(sst_soc_platform_init); | |
454 | ||
455 | static void __exit sst_soc_platform_exit(void) | |
456 | { | |
457 | platform_driver_unregister(&sst_platform_driver); | |
458 | pr_debug("sst_soc_platform_exit sucess\n"); | |
459 | } | |
460 | module_exit(sst_soc_platform_exit); | |
461 | ||
462 | MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver"); | |
463 | MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>"); | |
464 | MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>"); | |
465 | MODULE_LICENSE("GPL v2"); | |
f5c6b2aa | 466 | MODULE_ALIAS("platform:sst-platform"); |