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