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