staging: comedi: check s->async for poll(), read() and write()
[pandora-kernel.git] / drivers / staging / intel_sst / intel_sst_drv_interface.c
1 /*
2  *  intel_sst_interface.c - Intel SST Driver for audio engine
3  *
4  *  Copyright (C) 2008-10 Intel Corp
5  *  Authors:    Vinod Koul <vinod.koul@intel.com>
6  *              Harsha Priya <priya.harsha@intel.com>
7  *              Dharageswari R <dharageswari.r@intel.com)
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; version 2 of the License.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  *  This driver exposes the audio engine functionalities to the ALSA
25  *      and middleware.
26  *  Upper layer interfaces (MAD driver, MMF) to SST driver
27  */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/fs.h>
34 #include <linux/firmware.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/export.h>
37 #include "intel_sst.h"
38 #include "intel_sst_ioctl.h"
39 #include "intel_sst_fw_ipc.h"
40 #include "intel_sst_common.h"
41
42
43 /*
44  * sst_download_fw - download the audio firmware to DSP
45  *
46  * This function is called when the FW needs to be downloaded to SST DSP engine
47  */
48 int sst_download_fw(void)
49 {
50         int retval;
51         const struct firmware *fw_sst;
52         char name[20];
53
54         if (sst_drv_ctx->sst_state != SST_UN_INIT)
55                 return -EPERM;
56
57         /* Reload firmware is not needed for MRST */
58         if ( (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) && sst_drv_ctx->fw_downloaded) {
59                 pr_debug("FW already downloaded, skip for MRST platform\n");
60                 sst_drv_ctx->sst_state = SST_FW_RUNNING;
61                 return 0;
62         }
63
64         snprintf(name, sizeof(name), "%s%04x%s", "fw_sst_",
65                                         sst_drv_ctx->pci_id, ".bin");
66
67         pr_debug("Downloading %s FW now...\n", name);
68         retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev);
69         if (retval) {
70                 pr_err("request fw failed %d\n", retval);
71                 return retval;
72         }
73         sst_drv_ctx->alloc_block[0].sst_id = FW_DWNL_ID;
74         sst_drv_ctx->alloc_block[0].ops_block.condition = false;
75         retval = sst_load_fw(fw_sst, NULL);
76         if (retval)
77                 goto end_restore;
78
79         retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]);
80         if (retval)
81                 pr_err("fw download failed %d\n" , retval);
82         else
83                 sst_drv_ctx->fw_downloaded = 1;
84
85 end_restore:
86         release_firmware(fw_sst);
87         sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT;
88         return retval;
89 }
90
91
92 /*
93  * sst_stalled - this function checks if the lpe is in stalled state
94  */
95 int sst_stalled(void)
96 {
97         int retry = 1000;
98         int retval = -1;
99
100         while (retry) {
101                 if (!sst_drv_ctx->lpe_stalled)
102                         return 0;
103                 /*wait for time and re-check*/
104                 msleep(1);
105
106                 retry--;
107         }
108         pr_debug("in Stalled State\n");
109         return retval;
110 }
111
112 void free_stream_context(unsigned int str_id)
113 {
114         struct stream_info *stream;
115
116         if (!sst_validate_strid(str_id)) {
117                 /* str_id is valid, so stream is alloacted */
118                 stream = &sst_drv_ctx->streams[str_id];
119                 if (sst_free_stream(str_id))
120                         sst_clean_stream(&sst_drv_ctx->streams[str_id]);
121                 if (stream->ops == STREAM_OPS_PLAYBACK ||
122                                 stream->ops == STREAM_OPS_PLAYBACK_DRM) {
123                         sst_drv_ctx->pb_streams--;
124                         if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
125                                 sst_drv_ctx->scard_ops->power_down_pmic_pb(
126                                                 stream->device);
127                         else {
128                                 if (sst_drv_ctx->pb_streams == 0)
129                                         sst_drv_ctx->scard_ops->
130                                         power_down_pmic_pb(stream->device);
131                         }
132                 } else if (stream->ops == STREAM_OPS_CAPTURE) {
133                         sst_drv_ctx->cp_streams--;
134                         if (sst_drv_ctx->cp_streams == 0)
135                                 sst_drv_ctx->scard_ops->power_down_pmic_cp(
136                                                 stream->device);
137                 }
138                 if (sst_drv_ctx->pb_streams == 0
139                                 && sst_drv_ctx->cp_streams == 0)
140                         sst_drv_ctx->scard_ops->power_down_pmic();
141         }
142 }
143
144 /*
145  * sst_get_stream_allocated - this function gets a stream allocated with
146  * the given params
147  *
148  * @str_param : stream params
149  * @lib_dnld : pointer to pointer of lib downlaod struct
150  *
151  * This creates new stream id for a stream, in case lib is to be downloaded to
152  * DSP, it downloads that
153  */
154 int sst_get_stream_allocated(struct snd_sst_params *str_param,
155                 struct snd_sst_lib_download **lib_dnld)
156 {
157         int retval, str_id;
158         struct stream_info *str_info;
159
160         retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
161                                 str_param->codec, str_param->device_type);
162         if (retval < 0) {
163                 pr_err("sst_alloc_stream failed %d\n", retval);
164                 return retval;
165         }
166         pr_debug("Stream allocated %d\n", retval);
167         str_id = retval;
168         str_info = &sst_drv_ctx->streams[str_id];
169         /* Block the call for reply */
170         retval = sst_wait_interruptible_timeout(sst_drv_ctx,
171                         &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
172         if ((retval != 0) || (str_info->ctrl_blk.ret_code != 0)) {
173                 pr_debug("FW alloc failed retval %d, ret_code %d\n",
174                                 retval, str_info->ctrl_blk.ret_code);
175                 str_id = -str_info->ctrl_blk.ret_code; /*return error*/
176                 *lib_dnld = str_info->ctrl_blk.data;
177                 sst_clean_stream(str_info);
178         } else
179                 pr_debug("FW Stream allocated success\n");
180         return str_id; /*will ret either error (in above if) or correct str id*/
181 }
182
183 /*
184  * sst_get_sfreq - this function returns the frequency of the stream
185  *
186  * @str_param : stream params
187  */
188 static int sst_get_sfreq(struct snd_sst_params *str_param)
189 {
190         switch (str_param->codec) {
191         case SST_CODEC_TYPE_PCM:
192                 return 48000; /*str_param->sparams.uc.pcm_params.sfreq;*/
193         case SST_CODEC_TYPE_MP3:
194                 return str_param->sparams.uc.mp3_params.sfreq;
195         case SST_CODEC_TYPE_AAC:
196                 return str_param->sparams.uc.aac_params.sfreq;
197         case SST_CODEC_TYPE_WMA9:
198                 return str_param->sparams.uc.wma_params.sfreq;
199         default:
200                 return 0;
201         }
202 }
203
204 /*
205  * sst_get_stream - this function prepares for stream allocation
206  *
207  * @str_param : stream param
208  */
209 int sst_get_stream(struct snd_sst_params *str_param)
210 {
211         int i, retval;
212         struct stream_info *str_info;
213         struct snd_sst_lib_download *lib_dnld;
214
215         /* stream is not allocated, we are allocating */
216         retval = sst_get_stream_allocated(str_param, &lib_dnld);
217         if (retval == -(SST_LIB_ERR_LIB_DNLD_REQUIRED)) {
218                 /* codec download is required */
219                 struct snd_sst_alloc_response *response;
220
221                 pr_debug("Codec is required.... trying that\n");
222                 if (lib_dnld == NULL) {
223                         pr_err("lib download null!!! abort\n");
224                         return -EIO;
225                 }
226                 i = sst_get_block_stream(sst_drv_ctx);
227                 response = sst_drv_ctx->alloc_block[i].ops_block.data;
228                 pr_debug("alloc block allocated = %d\n", i);
229                 if (i < 0) {
230                         kfree(lib_dnld);
231                         return -ENOMEM;
232                 }
233                 retval = sst_load_library(lib_dnld, str_param->ops);
234                 kfree(lib_dnld);
235
236                 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
237                 if (!retval) {
238                         pr_debug("codec was downloaded successfully\n");
239
240                         retval = sst_get_stream_allocated(str_param, &lib_dnld);
241                         if (retval <= 0)
242                                 goto err;
243
244                         pr_debug("Alloc done stream id %d\n", retval);
245                 } else {
246                         pr_debug("codec download failed\n");
247                         retval = -EIO;
248                         goto err;
249                 }
250         } else if  (retval <= 0)
251                 goto err;
252         /*else
253                 set_port_params(str_param, str_param->ops);*/
254
255         /* store sampling freq */
256         str_info = &sst_drv_ctx->streams[retval];
257         str_info->sfreq = sst_get_sfreq(str_param);
258
259         /* power on the analog, if reqd */
260         if (str_param->ops == STREAM_OPS_PLAYBACK ||
261                         str_param->ops == STREAM_OPS_PLAYBACK_DRM) {
262                 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
263                         sst_drv_ctx->scard_ops->power_up_pmic_pb(
264                                         sst_drv_ctx->pmic_port_instance);
265                 else
266                         sst_drv_ctx->scard_ops->power_up_pmic_pb(
267                                                         str_info->device);
268                 /*Only if the playback is MP3 - Send a message*/
269                 sst_drv_ctx->pb_streams++;
270         } else if (str_param->ops == STREAM_OPS_CAPTURE) {
271
272                 sst_drv_ctx->scard_ops->power_up_pmic_cp(
273                                 sst_drv_ctx->pmic_port_instance);
274                 /*Send a messageif not sent already*/
275                 sst_drv_ctx->cp_streams++;
276         }
277
278 err:
279         return retval;
280 }
281
282 void sst_process_mad_ops(struct work_struct *work)
283 {
284
285         struct mad_ops_wq *mad_ops =
286                         container_of(work, struct mad_ops_wq, wq);
287         int retval = 0;
288
289         switch (mad_ops->control_op) {
290         case SST_SND_PAUSE:
291                 retval = sst_pause_stream(mad_ops->stream_id);
292                 break;
293         case SST_SND_RESUME:
294                 retval = sst_resume_stream(mad_ops->stream_id);
295                 break;
296         case SST_SND_DROP:
297                 retval = sst_drop_stream(mad_ops->stream_id);
298                 break;
299         case SST_SND_START:
300                         pr_debug("SST Debug: start stream\n");
301                 retval = sst_start_stream(mad_ops->stream_id);
302                 break;
303         case SST_SND_STREAM_PROCESS:
304                 pr_debug("play/capt frames...\n");
305                 break;
306         default:
307                 pr_err(" wrong control_ops reported\n");
308         }
309         return;
310 }
311
312 void send_intial_rx_timeslot(void)
313 {
314         if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID &&
315                         sst_drv_ctx->rx_time_slot_status != RX_TIMESLOT_UNINIT
316                         && sst_drv_ctx->pmic_vendor != SND_NC)
317                 sst_enable_rx_timeslot(sst_drv_ctx->rx_time_slot_status);
318 }
319
320 /*
321  * sst_open_pcm_stream - Open PCM interface
322  *
323  * @str_param: parameters of pcm stream
324  *
325  * This function is called by MID sound card driver to open
326  * a new pcm interface
327  */
328 int sst_open_pcm_stream(struct snd_sst_params *str_param)
329 {
330         struct stream_info *str_info;
331         int retval;
332
333         pm_runtime_get_sync(&sst_drv_ctx->pci->dev);
334
335         if (sst_drv_ctx->sst_state == SST_SUSPENDED) {
336                 /* LPE is suspended, resume it before proceeding*/
337                 pr_debug("Resuming from Suspended state\n");
338                 retval = intel_sst_resume(sst_drv_ctx->pci);
339                 if (retval) {
340                         pr_err("Resume Failed = %#x, abort\n", retval);
341                         pm_runtime_put(&sst_drv_ctx->pci->dev);
342                         return retval;
343                 }
344         }
345         if (sst_drv_ctx->sst_state == SST_UN_INIT) {
346                 /* FW is not downloaded */
347                 pr_debug("DSP Downloading FW now...\n");
348                 retval = sst_download_fw();
349                 if (retval) {
350                         pr_err("FW download fail %x, abort\n", retval);
351                         pm_runtime_put(&sst_drv_ctx->pci->dev);
352                         return retval;
353                 }
354                 send_intial_rx_timeslot();
355         }
356
357         if (!str_param) {
358                 pm_runtime_put(&sst_drv_ctx->pci->dev);
359                 return -EINVAL;
360         }
361
362         retval = sst_get_stream(str_param);
363         if (retval > 0) {
364                 sst_drv_ctx->stream_cnt++;
365                 str_info = &sst_drv_ctx->streams[retval];
366                 str_info->src = MAD_DRV;
367         } else
368                 pm_runtime_put(&sst_drv_ctx->pci->dev);
369
370         return retval;
371 }
372
373 /*
374  * sst_close_pcm_stream - Close PCM interface
375  *
376  * @str_id: stream id to be closed
377  *
378  * This function is called by MID sound card driver to close
379  * an existing pcm interface
380  */
381 int sst_close_pcm_stream(unsigned int str_id)
382 {
383         struct stream_info *stream;
384
385         pr_debug("sst: stream free called\n");
386         if (sst_validate_strid(str_id))
387                 return -EINVAL;
388         stream = &sst_drv_ctx->streams[str_id];
389         free_stream_context(str_id);
390         stream->pcm_substream = NULL;
391         stream->status = STREAM_UN_INIT;
392         stream->period_elapsed = NULL;
393         sst_drv_ctx->stream_cnt--;
394         pr_debug("sst: will call runtime put now\n");
395         pm_runtime_put(&sst_drv_ctx->pci->dev);
396         return 0;
397 }
398
399 /*
400  * sst_device_control - Set Control params
401  *
402  * @cmd: control cmd to be set
403  * @arg: command argument
404  *
405  * This function is called by MID sound card driver to set
406  * SST/Sound card controls for an opened stream.
407  * This is registered with MID driver
408  */
409 int sst_device_control(int cmd, void *arg)
410 {
411         int retval = 0, str_id = 0;
412
413         switch (cmd) {
414         case SST_SND_PAUSE:
415         case SST_SND_RESUME:
416         case SST_SND_DROP:
417         case SST_SND_START:
418                 sst_drv_ctx->mad_ops.control_op = cmd;
419                 sst_drv_ctx->mad_ops.stream_id = *(int *)arg;
420                 queue_work(sst_drv_ctx->mad_wq, &sst_drv_ctx->mad_ops.wq);
421                 break;
422
423         case SST_SND_STREAM_INIT: {
424                 struct pcm_stream_info *str_info;
425                 struct stream_info *stream;
426
427                 pr_debug("stream init called\n");
428                 str_info = (struct pcm_stream_info *)arg;
429                 str_id = str_info->str_id;
430                 retval = sst_validate_strid(str_id);
431                 if (retval)
432                         break;
433
434                 stream = &sst_drv_ctx->streams[str_id];
435                 pr_debug("setting the period ptrs\n");
436                 stream->pcm_substream = str_info->mad_substream;
437                 stream->period_elapsed = str_info->period_elapsed;
438                 stream->sfreq = str_info->sfreq;
439                 stream->prev = stream->status;
440                 stream->status = STREAM_INIT;
441                 break;
442         }
443
444         case SST_SND_BUFFER_POINTER: {
445                 struct pcm_stream_info *stream_info;
446                 struct snd_sst_tstamp fw_tstamp = {0,};
447                 struct stream_info *stream;
448
449
450                 stream_info = (struct pcm_stream_info *)arg;
451                 str_id = stream_info->str_id;
452                 retval = sst_validate_strid(str_id);
453                 if (retval)
454                         break;
455                 stream = &sst_drv_ctx->streams[str_id];
456
457                 if (!stream->pcm_substream)
458                         break;
459                 memcpy_fromio(&fw_tstamp,
460                         ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
461                         +(str_id * sizeof(fw_tstamp))),
462                         sizeof(fw_tstamp));
463
464                 pr_debug("Pointer Query on strid = %d ops %d\n",
465                                                 str_id, stream->ops);
466
467                 if (stream->ops == STREAM_OPS_PLAYBACK)
468                         stream_info->buffer_ptr = fw_tstamp.samples_rendered;
469                 else
470                         stream_info->buffer_ptr = fw_tstamp.samples_processed;
471                 pr_debug("Samples rendered = %llu, buffer ptr %llu\n",
472                         fw_tstamp.samples_rendered, stream_info->buffer_ptr);
473                 break;
474         }
475         case SST_ENABLE_RX_TIME_SLOT: {
476                 int status = *(int *)arg;
477                 sst_drv_ctx->rx_time_slot_status = status ;
478                 sst_enable_rx_timeslot(status);
479                 break;
480         }
481         default:
482                 /* Illegal case */
483                 pr_warn("illegal req\n");
484                 return -EINVAL;
485         }
486
487         return retval;
488 }
489
490
491 struct intel_sst_pcm_control pcm_ops = {
492         .open = sst_open_pcm_stream,
493         .device_control = sst_device_control,
494         .close = sst_close_pcm_stream,
495 };
496
497 struct intel_sst_card_ops sst_pmic_ops = {
498         .pcm_control = &pcm_ops,
499 };
500
501 /*
502  * register_sst_card - function for sound card to register
503  *
504  * @card: pointer to structure of operations
505  *
506  * This function is called card driver loads and is ready for registration
507  */
508 int register_sst_card(struct intel_sst_card_ops *card)
509 {
510         if (!sst_drv_ctx) {
511                 pr_err("No SST driver register card reject\n");
512                 return -ENODEV;
513         }
514
515         if (!card || !card->module_name) {
516                 pr_err("Null Pointer Passed\n");
517                 return -EINVAL;
518         }
519         if (sst_drv_ctx->pmic_state == SND_MAD_UN_INIT) {
520                 /* register this driver */
521                 if ((strncmp(SST_CARD_NAMES, card->module_name,
522                                 strlen(SST_CARD_NAMES))) == 0) {
523                         sst_drv_ctx->pmic_vendor = card->vendor_id;
524                         sst_drv_ctx->scard_ops =  card->scard_ops;
525                         sst_pmic_ops.module_name = card->module_name;
526                         sst_drv_ctx->pmic_state = SND_MAD_INIT_DONE;
527                         sst_drv_ctx->rx_time_slot_status = 0; /*default AMIC*/
528                         card->pcm_control = sst_pmic_ops.pcm_control;
529                         return 0;
530                 } else {
531                         pr_err("strcmp fail %s\n", card->module_name);
532                         return -EINVAL;
533                 }
534
535         } else {
536                 /* already registered a driver */
537                 pr_err("Repeat for registration..denied\n");
538                 return -EBADRQC;
539         }
540         /* The ASoC code doesn't set scard_ops */
541         if (sst_drv_ctx->scard_ops)
542                 sst_drv_ctx->scard_ops->card_status = SND_CARD_UN_INIT;
543         return 0;
544 }
545 EXPORT_SYMBOL_GPL(register_sst_card);
546
547 /*
548  * unregister_sst_card- function for sound card to un-register
549  *
550  * @card: pointer to structure of operations
551  *
552  * This function is called when card driver unloads
553  */
554 void unregister_sst_card(struct intel_sst_card_ops *card)
555 {
556         if (sst_pmic_ops.pcm_control == card->pcm_control) {
557                 /* unreg */
558                 sst_pmic_ops.module_name = "";
559                 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
560                 pr_debug("Unregistered %s\n", card->module_name);
561         }
562         return;
563 }
564 EXPORT_SYMBOL_GPL(unregister_sst_card);