OSDN Git Service

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