2 * intel_sst_interface.c - Intel SST Driver for audio engine
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 * This driver exposes the audio engine functionalities to the ALSA
26 * Upper layer interfaces (MAD driver, MMF) to SST driver
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/delay.h>
32 #include <linux/pci.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"
43 * sst_download_fw - download the audio firmware to DSP
45 * This function is called when the FW needs to be downloaded to SST DSP engine
47 int sst_download_fw(void)
50 const struct firmware *fw_sst;
53 if (sst_drv_ctx->sst_state != SST_UN_INIT)
56 snprintf(name, sizeof(name), "%s%04x%s", "fw_sst_",
57 sst_drv_ctx->pci_id, ".bin");
59 pr_debug("Downloading %s FW now...\n", name);
60 retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev);
62 pr_err("request fw failed %d\n", retval);
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);
71 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]);
73 pr_err("fw download failed %d\n" , retval);
75 release_firmware(fw_sst);
76 sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT;
82 * sst_stalled - this function checks if the lpe is in stalled state
90 if (!sst_drv_ctx->lpe_stalled)
92 /*wait for time and re-check*/
97 pr_debug("in Stalled State\n");
101 void free_stream_context(unsigned int str_id)
103 struct stream_info *stream;
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(
117 if (sst_drv_ctx->pb_streams == 0)
118 sst_drv_ctx->scard_ops->
119 power_down_pmic_pb(stream->device);
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(
127 if (sst_drv_ctx->pb_streams == 0
128 && sst_drv_ctx->cp_streams == 0)
129 sst_drv_ctx->scard_ops->power_down_pmic();
134 * sst_get_stream_allocated - this function gets a stream allocated with
137 * @str_param : stream params
138 * @lib_dnld : pointer to pointer of lib downlaod struct
140 * This creates new stream id for a stream, in case lib is to be downloaded to
141 * DSP, it downloads that
143 int sst_get_stream_allocated(struct snd_sst_params *str_param,
144 struct snd_sst_lib_download **lib_dnld)
147 struct stream_info *str_info;
149 retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
150 str_param->codec, str_param->device_type);
152 pr_err("sst_alloc_stream failed %d\n", retval);
155 pr_debug("Stream allocated %d\n", 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);
168 pr_debug("FW Stream allocated success\n");
169 return str_id; /*will ret either error (in above if) or correct str id*/
173 * sst_get_sfreq - this function returns the frequency of the stream
175 * @str_param : stream params
177 static int sst_get_sfreq(struct snd_sst_params *str_param)
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;
194 * sst_get_stream - this function prepares for stream allocation
196 * @str_param : stream param
198 int sst_get_stream(struct snd_sst_params *str_param)
201 struct stream_info *str_info;
202 struct snd_sst_lib_download *lib_dnld;
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;
210 pr_debug("Codec is required.... trying that\n");
211 if (lib_dnld == NULL) {
212 pr_err("lib download null!!! abort\n");
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);
222 retval = sst_load_library(lib_dnld, str_param->ops);
225 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
227 pr_debug("codec was downloaded successfully\n");
229 retval = sst_get_stream_allocated(str_param, &lib_dnld);
233 pr_debug("Alloc done stream id %d\n", retval);
235 pr_debug("codec download failed\n");
239 } else if (retval <= 0)
242 set_port_params(str_param, str_param->ops);*/
244 /* store sampling freq */
245 str_info = &sst_drv_ctx->streams[retval];
246 str_info->sfreq = sst_get_sfreq(str_param);
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);
255 sst_drv_ctx->scard_ops->power_up_pmic_pb(
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) {
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++;
271 void sst_process_mad_ops(struct work_struct *work)
274 struct mad_ops_wq *mad_ops =
275 container_of(work, struct mad_ops_wq, wq);
278 switch (mad_ops->control_op) {
280 retval = sst_pause_stream(mad_ops->stream_id);
283 retval = sst_resume_stream(mad_ops->stream_id);
286 retval = sst_drop_stream(mad_ops->stream_id);
289 pr_debug("SST Debug: start stream\n");
290 retval = sst_start_stream(mad_ops->stream_id);
292 case SST_SND_STREAM_PROCESS:
293 pr_debug("play/capt frames...\n");
296 pr_err(" wrong control_ops reported\n");
301 void send_intial_rx_timeslot(void)
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);
310 * sst_open_pcm_stream - Open PCM interface
312 * @str_param: parameters of pcm stream
314 * This function is called by MID sound card driver to open
315 * a new pcm interface
317 int sst_open_pcm_stream(struct snd_sst_params *str_param)
319 struct stream_info *str_info;
322 pm_runtime_get_sync(&sst_drv_ctx->pci->dev);
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);
329 pr_err("Resume Failed = %#x, abort\n", retval);
330 pm_runtime_put(&sst_drv_ctx->pci->dev);
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();
339 pr_err("FW download fail %x, abort\n", retval);
340 pm_runtime_put(&sst_drv_ctx->pci->dev);
343 send_intial_rx_timeslot();
347 pm_runtime_put(&sst_drv_ctx->pci->dev);
351 retval = sst_get_stream(str_param);
353 sst_drv_ctx->stream_cnt++;
354 str_info = &sst_drv_ctx->streams[retval];
355 str_info->src = MAD_DRV;
357 pm_runtime_put(&sst_drv_ctx->pci->dev);
363 * sst_close_pcm_stream - Close PCM interface
365 * @str_id: stream id to be closed
367 * This function is called by MID sound card driver to close
368 * an existing pcm interface
370 int sst_close_pcm_stream(unsigned int str_id)
372 struct stream_info *stream;
374 pr_debug("sst: stream free called\n");
375 if (sst_validate_strid(str_id))
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);
389 * sst_device_control - Set Control params
391 * @cmd: control cmd to be set
392 * @arg: command argument
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
398 int sst_device_control(int cmd, void *arg)
400 int retval = 0, str_id = 0;
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);
412 case SST_SND_STREAM_INIT: {
413 struct pcm_stream_info *str_info;
414 struct stream_info *stream;
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);
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;
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;
439 stream_info = (struct pcm_stream_info *)arg;
440 str_id = stream_info->str_id;
441 retval = sst_validate_strid(str_id);
444 stream = &sst_drv_ctx->streams[str_id];
446 if (!stream->pcm_substream)
448 memcpy_fromio(&fw_tstamp,
449 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
450 +(str_id * sizeof(fw_tstamp))),
453 pr_debug("Pointer Query on strid = %d ops %d\n",
454 str_id, stream->ops);
456 if (stream->ops == STREAM_OPS_PLAYBACK)
457 stream_info->buffer_ptr = fw_tstamp.samples_rendered;
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);
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);
472 pr_warn("illegal req\n");
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,
486 struct intel_sst_card_ops sst_pmic_ops = {
487 .pcm_control = &pcm_ops,
491 * register_sst_card - function for sound card to register
493 * @card: pointer to structure of operations
495 * This function is called card driver loads and is ready for registration
497 int register_sst_card(struct intel_sst_card_ops *card)
500 pr_err("No SST driver register card reject\n");
504 if (!card || !card->module_name) {
505 pr_err("Null Pointer Passed\n");
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;
520 pr_err("strcmp fail %s\n", card->module_name);
525 /* already registered a driver */
526 pr_err("Repeat for registration..denied\n");
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;
534 EXPORT_SYMBOL_GPL(register_sst_card);
537 * unregister_sst_card- function for sound card to un-register
539 * @card: pointer to structure of operations
541 * This function is called when card driver unloads
543 void unregister_sst_card(struct intel_sst_card_ops *card)
545 if (sst_pmic_ops.pcm_control == card->pcm_control) {
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);
553 EXPORT_SYMBOL_GPL(unregister_sst_card);