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