blob: d567c322a2fb471788d7b8f47345a6b9fd6d5aed [file] [log] [blame]
Vinod Kould927fda2011-01-04 20:16:32 +05301/*
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
38static 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 */
66struct 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 },
Harsha Priyaa7bffdf2011-01-28 22:27:26 +053076 .capture = {
77 .channels_min = 1,
78 .channels_max = 5,
79 .rates = SNDRV_PCM_RATE_48000,
80 .formats = SNDRV_PCM_FMTBIT_S24_LE,
81 },
Vinod Kould927fda2011-01-04 20:16:32 +053082},
83{
84 .name = "Speaker-cpu-dai",
85 .id = 1,
86 .playback = {
87 .channels_min = SST_MONO,
88 .channels_max = SST_STEREO,
89 .rates = SNDRV_PCM_RATE_48000,
90 .formats = SNDRV_PCM_FMTBIT_S24_LE,
91 },
92},
93{
94 .name = "Vibra1-cpu-dai",
95 .id = 2,
96 .playback = {
97 .channels_min = SST_MONO,
98 .channels_max = SST_MONO,
99 .rates = SNDRV_PCM_RATE_48000,
100 .formats = SNDRV_PCM_FMTBIT_S24_LE,
101 },
102},
103{
104 .name = "Vibra2-cpu-dai",
105 .id = 3,
106 .playback = {
107 .channels_min = SST_MONO,
108 .channels_max = SST_STEREO,
109 .rates = SNDRV_PCM_RATE_48000,
110 .formats = SNDRV_PCM_FMTBIT_S24_LE,
111 },
112},
113};
Harsha Priya5c685362011-01-11 14:50:35 +0530114
Vinod Kould927fda2011-01-04 20:16:32 +0530115/* helper functions */
Harsha Priya5c685362011-01-11 14:50:35 +0530116static inline void sst_set_stream_status(struct sst_runtime_stream *stream,
117 int state)
118{
Lu Guanqund89b0a12011-04-08 15:38:48 +0800119 unsigned long flags;
120 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530121 stream->stream_status = state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800122 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530123}
124
125static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
126{
127 int state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800128 unsigned long flags;
Harsha Priya5c685362011-01-11 14:50:35 +0530129
Lu Guanqund89b0a12011-04-08 15:38:48 +0800130 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530131 state = stream->stream_status;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800132 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530133 return state;
134}
135
136static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
137 struct snd_sst_stream_params *param)
138{
139
140 param->uc.pcm_params.codec = SST_CODEC_TYPE_PCM;
141 param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
142 param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
143 param->uc.pcm_params.reserved = 0;
144 param->uc.pcm_params.sfreq = substream->runtime->rate;
145 param->uc.pcm_params.ring_buffer_size =
146 snd_pcm_lib_buffer_bytes(substream);
147 param->uc.pcm_params.period_count = substream->runtime->period_size;
148 param->uc.pcm_params.ring_buffer_addr =
149 virt_to_phys(substream->dma_buffer.area);
150 pr_debug("period_cnt = %d\n", param->uc.pcm_params.period_count);
151 pr_debug("sfreq= %d, wd_sz = %d\n",
152 param->uc.pcm_params.sfreq, param->uc.pcm_params.pcm_wd_sz);
153}
154
Vinod Kould927fda2011-01-04 20:16:32 +0530155static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
156{
157 struct sst_runtime_stream *stream =
158 substream->runtime->private_data;
159 struct snd_sst_stream_params param = {{{0,},},};
160 struct snd_sst_params str_params = {0};
161 int ret_val;
162
163 /* set codec params and inform SST driver the same */
Harsha Priya5c685362011-01-11 14:50:35 +0530164 sst_fill_pcm_params(substream, &param);
Vinod Kould927fda2011-01-04 20:16:32 +0530165 substream->runtime->dma_area = substream->dma_buffer.area;
Vinod Kould927fda2011-01-04 20:16:32 +0530166 str_params.sparams = param;
Harsha Priya5c685362011-01-11 14:50:35 +0530167 str_params.codec = param.uc.pcm_params.codec;
Vinod Kould927fda2011-01-04 20:16:32 +0530168 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
169 str_params.ops = STREAM_OPS_PLAYBACK;
170 str_params.device_type = substream->pcm->device + 1;
171 pr_debug("Playbck stream,Device %d\n",
172 substream->pcm->device);
173 } else {
174 str_params.ops = STREAM_OPS_CAPTURE;
175 str_params.device_type = SND_SST_DEVICE_CAPTURE;
176 pr_debug("Capture stream,Device %d\n",
177 substream->pcm->device);
178 }
Harsha Priyaa2117012011-01-11 14:50:59 +0530179 ret_val = stream->sstdrv_ops->pcm_control->open(&str_params);
Vinod Kould927fda2011-01-04 20:16:32 +0530180 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
181 if (ret_val < 0)
182 return ret_val;
183
184 stream->stream_info.str_id = ret_val;
185 pr_debug("str id : %d\n", stream->stream_info.str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530186 return ret_val;
187}
188
Vinod Kould927fda2011-01-04 20:16:32 +0530189static void sst_period_elapsed(void *mad_substream)
190{
191 struct snd_pcm_substream *substream = mad_substream;
192 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530193 int status;
Vinod Kould927fda2011-01-04 20:16:32 +0530194
195 if (!substream || !substream->runtime)
196 return;
197 stream = substream->runtime->private_data;
198 if (!stream)
199 return;
Harsha Priya5c685362011-01-11 14:50:35 +0530200 status = sst_get_stream_status(stream);
201 if (status != SST_PLATFORM_RUNNING)
Vinod Kould927fda2011-01-04 20:16:32 +0530202 return;
Vinod Kould927fda2011-01-04 20:16:32 +0530203 snd_pcm_period_elapsed(substream);
Vinod Kould927fda2011-01-04 20:16:32 +0530204}
205
206static int sst_platform_init_stream(struct snd_pcm_substream *substream)
207{
208 struct sst_runtime_stream *stream =
209 substream->runtime->private_data;
210 int ret_val;
211
212 pr_debug("setting buffer ptr param\n");
Harsha Priya5c685362011-01-11 14:50:35 +0530213 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530214 stream->stream_info.period_elapsed = sst_period_elapsed;
215 stream->stream_info.mad_substream = substream;
216 stream->stream_info.buffer_ptr = 0;
217 stream->stream_info.sfreq = substream->runtime->rate;
Harsha Priyaa2117012011-01-11 14:50:59 +0530218 ret_val = stream->sstdrv_ops->pcm_control->device_control(
219 SST_SND_STREAM_INIT, &stream->stream_info);
Vinod Kould927fda2011-01-04 20:16:32 +0530220 if (ret_val)
221 pr_err("control_set ret error %d\n", ret_val);
222 return ret_val;
223
224}
225/* end -- helper functions */
226
227static int sst_platform_open(struct snd_pcm_substream *substream)
228{
229 struct snd_pcm_runtime *runtime;
230 struct sst_runtime_stream *stream;
231 int ret_val = 0;
232
233 pr_debug("sst_platform_open called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530234 runtime = substream->runtime;
235 runtime->hw = sst_platform_pcm_hw;
Vinod Kould927fda2011-01-04 20:16:32 +0530236 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
237 if (!stream)
238 return -ENOMEM;
Vinod Kould927fda2011-01-04 20:16:32 +0530239 spin_lock_init(&stream->status_lock);
240 stream->stream_info.str_id = 0;
Harsha Priya5c685362011-01-11 14:50:35 +0530241 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530242 stream->stream_info.mad_substream = substream;
243 /* allocate memory for SST API set */
244 stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
245 GFP_KERNEL);
246 if (!stream->sstdrv_ops) {
247 pr_err("sst: mem allocation for ops fail\n");
248 kfree(stream);
249 return -ENOMEM;
250 }
251 stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
Vinod Kould927fda2011-01-04 20:16:32 +0530252 /* registering with SST driver to get access to SST APIs to use */
253 ret_val = register_sst_card(stream->sstdrv_ops);
254 if (ret_val) {
255 pr_err("sst: sst card registration failed\n");
256 return ret_val;
257 }
258 runtime->private_data = stream;
259 return snd_pcm_hw_constraint_integer(runtime,
260 SNDRV_PCM_HW_PARAM_PERIODS);
261}
262
263static int sst_platform_close(struct snd_pcm_substream *substream)
264{
265 struct sst_runtime_stream *stream;
266 int ret_val = 0, str_id;
267
268 pr_debug("sst_platform_close called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530269 stream = substream->runtime->private_data;
270 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530271 if (str_id)
Harsha Priyaa2117012011-01-11 14:50:59 +0530272 ret_val = stream->sstdrv_ops->pcm_control->close(str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530273 kfree(stream->sstdrv_ops);
274 kfree(stream);
275 return ret_val;
276}
277
278static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
279{
280 struct sst_runtime_stream *stream;
281 int ret_val = 0, str_id;
282
283 pr_debug("sst_platform_pcm_prepare called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530284 stream = substream->runtime->private_data;
285 str_id = stream->stream_info.str_id;
286 if (stream->stream_info.str_id) {
Harsha Priyaa2117012011-01-11 14:50:59 +0530287 ret_val = stream->sstdrv_ops->pcm_control->device_control(
Vinod Kould927fda2011-01-04 20:16:32 +0530288 SST_SND_DROP, &str_id);
289 return ret_val;
290 }
291
292 ret_val = sst_platform_alloc_stream(substream);
293 if (ret_val < 0)
294 return ret_val;
295 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
296 "%d", stream->stream_info.str_id);
297
298 ret_val = sst_platform_init_stream(substream);
299 if (ret_val)
300 return ret_val;
301 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
302 return ret_val;
303}
304
305static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
306 int cmd)
307{
308 int ret_val = 0, str_id;
309 struct sst_runtime_stream *stream;
Harsha Priyaa2117012011-01-11 14:50:59 +0530310 int str_cmd, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530311
312 pr_debug("sst_platform_pcm_trigger called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530313 stream = substream->runtime->private_data;
Vinod Kould927fda2011-01-04 20:16:32 +0530314 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530315 switch (cmd) {
316 case SNDRV_PCM_TRIGGER_START:
317 pr_debug("sst: Trigger Start\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530318 str_cmd = SST_SND_START;
319 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530320 stream->stream_info.mad_substream = substream;
321 break;
322 case SNDRV_PCM_TRIGGER_STOP:
323 pr_debug("sst: in stop\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530324 str_cmd = SST_SND_DROP;
325 status = SST_PLATFORM_DROPPED;
Vinod Kould927fda2011-01-04 20:16:32 +0530326 break;
327 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
328 pr_debug("sst: in pause\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530329 str_cmd = SST_SND_PAUSE;
330 status = SST_PLATFORM_PAUSED;
Vinod Kould927fda2011-01-04 20:16:32 +0530331 break;
332 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
333 pr_debug("sst: in pause release\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530334 str_cmd = SST_SND_RESUME;
335 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530336 break;
337 default:
Harsha Priyaa2117012011-01-11 14:50:59 +0530338 return -EINVAL;
Vinod Kould927fda2011-01-04 20:16:32 +0530339 }
Harsha Priyaa2117012011-01-11 14:50:59 +0530340 ret_val = stream->sstdrv_ops->pcm_control->device_control(str_cmd,
341 &str_id);
342 if (!ret_val)
343 sst_set_stream_status(stream, status);
344
Vinod Kould927fda2011-01-04 20:16:32 +0530345 return ret_val;
346}
347
348
349static snd_pcm_uframes_t sst_platform_pcm_pointer
350 (struct snd_pcm_substream *substream)
351{
352 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530353 int ret_val, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530354 struct pcm_stream_info *str_info;
355
Vinod Kould927fda2011-01-04 20:16:32 +0530356 stream = substream->runtime->private_data;
Harsha Priya5c685362011-01-11 14:50:35 +0530357 status = sst_get_stream_status(stream);
358 if (status == SST_PLATFORM_INIT)
Vinod Kould927fda2011-01-04 20:16:32 +0530359 return 0;
Vinod Kould927fda2011-01-04 20:16:32 +0530360 str_info = &stream->stream_info;
Harsha Priyaa2117012011-01-11 14:50:59 +0530361 ret_val = stream->sstdrv_ops->pcm_control->device_control(
Vinod Kould927fda2011-01-04 20:16:32 +0530362 SST_SND_BUFFER_POINTER, str_info);
363 if (ret_val) {
364 pr_err("sst: error code = %d\n", ret_val);
365 return ret_val;
366 }
Vinod Kould927fda2011-01-04 20:16:32 +0530367 return stream->stream_info.buffer_ptr;
368}
369
Vinod Koul5b499f82011-02-15 18:28:54 +0530370static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
371 struct snd_pcm_hw_params *params)
372{
373 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
374 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
375
376 return 0;
377}
Vinod Kould927fda2011-01-04 20:16:32 +0530378
379static struct snd_pcm_ops sst_platform_ops = {
380 .open = sst_platform_open,
381 .close = sst_platform_close,
382 .ioctl = snd_pcm_lib_ioctl,
383 .prepare = sst_platform_pcm_prepare,
384 .trigger = sst_platform_pcm_trigger,
385 .pointer = sst_platform_pcm_pointer,
Vinod Koul5b499f82011-02-15 18:28:54 +0530386 .hw_params = sst_platform_pcm_hw_params,
Vinod Kould927fda2011-01-04 20:16:32 +0530387};
388
389static void sst_pcm_free(struct snd_pcm *pcm)
390{
391 pr_debug("sst_pcm_free called\n");
392 snd_pcm_lib_preallocate_free_for_all(pcm);
393}
394
395int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
396 struct snd_pcm *pcm)
397{
398 int retval = 0;
399
400 pr_debug("sst_pcm_new called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530401 if (dai->driver->playback.channels_min ||
402 dai->driver->capture.channels_min) {
403 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
404 SNDRV_DMA_TYPE_CONTINUOUS,
405 snd_dma_continuous_data(GFP_KERNEL),
406 SST_MIN_BUFFER, SST_MAX_BUFFER);
407 if (retval) {
408 pr_err("dma buffer allocationf fail\n");
409 return retval;
410 }
411 }
Vinod Kould927fda2011-01-04 20:16:32 +0530412 return retval;
413}
414struct snd_soc_platform_driver sst_soc_platform_drv = {
415 .ops = &sst_platform_ops,
416 .pcm_new = sst_pcm_new,
417 .pcm_free = sst_pcm_free,
418};
419
420static int sst_platform_probe(struct platform_device *pdev)
421{
422 int ret;
423
424 pr_debug("sst_platform_probe called\n");
425 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
426 if (ret) {
427 pr_err("registering soc platform failed\n");
428 return ret;
429 }
Harsha Priya5c685362011-01-11 14:50:35 +0530430
Vinod Kould927fda2011-01-04 20:16:32 +0530431 ret = snd_soc_register_dais(&pdev->dev,
432 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
433 if (ret) {
434 pr_err("registering cpu dais failed\n");
435 snd_soc_unregister_platform(&pdev->dev);
436 }
437 return ret;
438}
439
440static int sst_platform_remove(struct platform_device *pdev)
441{
442
443 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
Vinod Kould927fda2011-01-04 20:16:32 +0530444 snd_soc_unregister_platform(&pdev->dev);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300445 pr_debug("sst_platform_remove success\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530446 return 0;
447}
448
449static struct platform_driver sst_platform_driver = {
450 .driver = {
451 .name = "sst-platform",
452 .owner = THIS_MODULE,
453 },
454 .probe = sst_platform_probe,
455 .remove = sst_platform_remove,
456};
457
458static int __init sst_soc_platform_init(void)
459{
460 pr_debug("sst_soc_platform_init called\n");
461 return platform_driver_register(&sst_platform_driver);
462}
463module_init(sst_soc_platform_init);
464
465static void __exit sst_soc_platform_exit(void)
466{
467 platform_driver_unregister(&sst_platform_driver);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300468 pr_debug("sst_soc_platform_exit success\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530469}
470module_exit(sst_soc_platform_exit);
471
472MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
473MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
474MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
475MODULE_LICENSE("GPL v2");
Vinod Koulf5c6b2a2011-01-07 16:20:56 +0530476MODULE_ALIAS("platform:sst-platform");