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 <linux/export.h>
37 #include "intel_sst.h"
38 #include "intel_sst_ioctl.h"
39 #include "intel_sst_fw_ipc.h"
40 #include "intel_sst_common.h"
44 * sst_download_fw - download the audio firmware to DSP
46 * This function is called when the FW needs to be downloaded to SST DSP engine
48 int sst_download_fw(void)
51 const struct firmware *fw_sst;
54 if (sst_drv_ctx->sst_state != SST_UN_INIT)
57 /* Reload firmware is not needed for MRST */
58 if ( (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) && sst_drv_ctx->fw_downloaded) {
59 pr_debug("FW already downloaded, skip for MRST platform\n");
60 sst_drv_ctx->sst_state = SST_FW_RUNNING;
64 snprintf(name, sizeof(name), "%s%04x%s", "fw_sst_",
65 sst_drv_ctx->pci_id, ".bin");
67 pr_debug("Downloading %s FW now...\n", name);
68 retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev);
70 pr_err("request fw failed %d\n", retval);
73 sst_drv_ctx->alloc_block[0].sst_id = FW_DWNL_ID;
74 sst_drv_ctx->alloc_block[0].ops_block.condition = false;
75 retval = sst_load_fw(fw_sst, NULL);
79 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]);
81 pr_err("fw download failed %d\n" , retval);
83 sst_drv_ctx->fw_downloaded = 1;
86 release_firmware(fw_sst);
87 sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT;
93 * sst_stalled - this function checks if the lpe is in stalled state
101 if (!sst_drv_ctx->lpe_stalled)
103 /*wait for time and re-check*/
108 pr_debug("in Stalled State\n");
112 void free_stream_context(unsigned int str_id)
114 struct stream_info *stream;
116 if (!sst_validate_strid(str_id)) {
117 /* str_id is valid, so stream is alloacted */
118 stream = &sst_drv_ctx->streams[str_id];
119 if (sst_free_stream(str_id))
120 sst_clean_stream(&sst_drv_ctx->streams[str_id]);
121 if (stream->ops == STREAM_OPS_PLAYBACK ||
122 stream->ops == STREAM_OPS_PLAYBACK_DRM) {
123 sst_drv_ctx->pb_streams--;
124 if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
125 sst_drv_ctx->scard_ops->power_down_pmic_pb(
128 if (sst_drv_ctx->pb_streams == 0)
129 sst_drv_ctx->scard_ops->
130 power_down_pmic_pb(stream->device);
132 } else if (stream->ops == STREAM_OPS_CAPTURE) {
133 sst_drv_ctx->cp_streams--;
134 if (sst_drv_ctx->cp_streams == 0)
135 sst_drv_ctx->scard_ops->power_down_pmic_cp(
138 if (sst_drv_ctx->pb_streams == 0
139 && sst_drv_ctx->cp_streams == 0)
140 sst_drv_ctx->scard_ops->power_down_pmic();
145 * sst_get_stream_allocated - this function gets a stream allocated with
148 * @str_param : stream params
149 * @lib_dnld : pointer to pointer of lib downlaod struct
151 * This creates new stream id for a stream, in case lib is to be downloaded to
152 * DSP, it downloads that
154 int sst_get_stream_allocated(struct snd_sst_params *str_param,
155 struct snd_sst_lib_download **lib_dnld)
158 struct stream_info *str_info;
160 retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
161 str_param->codec, str_param->device_type);
163 pr_err("sst_alloc_stream failed %d\n", retval);
166 pr_debug("Stream allocated %d\n", retval);
168 str_info = &sst_drv_ctx->streams[str_id];
169 /* Block the call for reply */
170 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
171 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
172 if ((retval != 0) || (str_info->ctrl_blk.ret_code != 0)) {
173 pr_debug("FW alloc failed retval %d, ret_code %d\n",
174 retval, str_info->ctrl_blk.ret_code);
175 str_id = -str_info->ctrl_blk.ret_code; /*return error*/
176 *lib_dnld = str_info->ctrl_blk.data;
177 sst_clean_stream(str_info);
179 pr_debug("FW Stream allocated success\n");
180 return str_id; /*will ret either error (in above if) or correct str id*/
184 * sst_get_sfreq - this function returns the frequency of the stream
186 * @str_param : stream params
188 static int sst_get_sfreq(struct snd_sst_params *str_param)
190 switch (str_param->codec) {
191 case SST_CODEC_TYPE_PCM:
192 return 48000; /*str_param->sparams.uc.pcm_params.sfreq;*/
193 case SST_CODEC_TYPE_MP3:
194 return str_param->sparams.uc.mp3_params.sfreq;
195 case SST_CODEC_TYPE_AAC:
196 return str_param->sparams.uc.aac_params.sfreq;
197 case SST_CODEC_TYPE_WMA9:
198 return str_param->sparams.uc.wma_params.sfreq;
205 * sst_get_stream - this function prepares for stream allocation
207 * @str_param : stream param
209 int sst_get_stream(struct snd_sst_params *str_param)
212 struct stream_info *str_info;
213 struct snd_sst_lib_download *lib_dnld;
215 /* stream is not allocated, we are allocating */
216 retval = sst_get_stream_allocated(str_param, &lib_dnld);
217 if (retval == -(SST_LIB_ERR_LIB_DNLD_REQUIRED)) {
218 /* codec download is required */
219 struct snd_sst_alloc_response *response;
221 pr_debug("Codec is required.... trying that\n");
222 if (lib_dnld == NULL) {
223 pr_err("lib download null!!! abort\n");
226 i = sst_get_block_stream(sst_drv_ctx);
227 response = sst_drv_ctx->alloc_block[i].ops_block.data;
228 pr_debug("alloc block allocated = %d\n", i);
233 retval = sst_load_library(lib_dnld, str_param->ops);
236 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
238 pr_debug("codec was downloaded successfully\n");
240 retval = sst_get_stream_allocated(str_param, &lib_dnld);
244 pr_debug("Alloc done stream id %d\n", retval);
246 pr_debug("codec download failed\n");
250 } else if (retval <= 0)
253 set_port_params(str_param, str_param->ops);*/
255 /* store sampling freq */
256 str_info = &sst_drv_ctx->streams[retval];
257 str_info->sfreq = sst_get_sfreq(str_param);
259 /* power on the analog, if reqd */
260 if (str_param->ops == STREAM_OPS_PLAYBACK ||
261 str_param->ops == STREAM_OPS_PLAYBACK_DRM) {
262 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
263 sst_drv_ctx->scard_ops->power_up_pmic_pb(
264 sst_drv_ctx->pmic_port_instance);
266 sst_drv_ctx->scard_ops->power_up_pmic_pb(
268 /*Only if the playback is MP3 - Send a message*/
269 sst_drv_ctx->pb_streams++;
270 } else if (str_param->ops == STREAM_OPS_CAPTURE) {
272 sst_drv_ctx->scard_ops->power_up_pmic_cp(
273 sst_drv_ctx->pmic_port_instance);
274 /*Send a messageif not sent already*/
275 sst_drv_ctx->cp_streams++;
282 void sst_process_mad_ops(struct work_struct *work)
285 struct mad_ops_wq *mad_ops =
286 container_of(work, struct mad_ops_wq, wq);
289 switch (mad_ops->control_op) {
291 retval = sst_pause_stream(mad_ops->stream_id);
294 retval = sst_resume_stream(mad_ops->stream_id);
297 retval = sst_drop_stream(mad_ops->stream_id);
300 pr_debug("SST Debug: start stream\n");
301 retval = sst_start_stream(mad_ops->stream_id);
303 case SST_SND_STREAM_PROCESS:
304 pr_debug("play/capt frames...\n");
307 pr_err(" wrong control_ops reported\n");
312 void send_intial_rx_timeslot(void)
314 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID &&
315 sst_drv_ctx->rx_time_slot_status != RX_TIMESLOT_UNINIT
316 && sst_drv_ctx->pmic_vendor != SND_NC)
317 sst_enable_rx_timeslot(sst_drv_ctx->rx_time_slot_status);
321 * sst_open_pcm_stream - Open PCM interface
323 * @str_param: parameters of pcm stream
325 * This function is called by MID sound card driver to open
326 * a new pcm interface
328 int sst_open_pcm_stream(struct snd_sst_params *str_param)
330 struct stream_info *str_info;
333 pm_runtime_get_sync(&sst_drv_ctx->pci->dev);
335 if (sst_drv_ctx->sst_state == SST_SUSPENDED) {
336 /* LPE is suspended, resume it before proceeding*/
337 pr_debug("Resuming from Suspended state\n");
338 retval = intel_sst_resume(sst_drv_ctx->pci);
340 pr_err("Resume Failed = %#x, abort\n", retval);
341 pm_runtime_put(&sst_drv_ctx->pci->dev);
345 if (sst_drv_ctx->sst_state == SST_UN_INIT) {
346 /* FW is not downloaded */
347 pr_debug("DSP Downloading FW now...\n");
348 retval = sst_download_fw();
350 pr_err("FW download fail %x, abort\n", retval);
351 pm_runtime_put(&sst_drv_ctx->pci->dev);
354 send_intial_rx_timeslot();
358 pm_runtime_put(&sst_drv_ctx->pci->dev);
362 retval = sst_get_stream(str_param);
364 sst_drv_ctx->stream_cnt++;
365 str_info = &sst_drv_ctx->streams[retval];
366 str_info->src = MAD_DRV;
368 pm_runtime_put(&sst_drv_ctx->pci->dev);
374 * sst_close_pcm_stream - Close PCM interface
376 * @str_id: stream id to be closed
378 * This function is called by MID sound card driver to close
379 * an existing pcm interface
381 int sst_close_pcm_stream(unsigned int str_id)
383 struct stream_info *stream;
385 pr_debug("sst: stream free called\n");
386 if (sst_validate_strid(str_id))
388 stream = &sst_drv_ctx->streams[str_id];
389 free_stream_context(str_id);
390 stream->pcm_substream = NULL;
391 stream->status = STREAM_UN_INIT;
392 stream->period_elapsed = NULL;
393 sst_drv_ctx->stream_cnt--;
394 pr_debug("sst: will call runtime put now\n");
395 pm_runtime_put(&sst_drv_ctx->pci->dev);
400 * sst_device_control - Set Control params
402 * @cmd: control cmd to be set
403 * @arg: command argument
405 * This function is called by MID sound card driver to set
406 * SST/Sound card controls for an opened stream.
407 * This is registered with MID driver
409 int sst_device_control(int cmd, void *arg)
411 int retval = 0, str_id = 0;
418 sst_drv_ctx->mad_ops.control_op = cmd;
419 sst_drv_ctx->mad_ops.stream_id = *(int *)arg;
420 queue_work(sst_drv_ctx->mad_wq, &sst_drv_ctx->mad_ops.wq);
423 case SST_SND_STREAM_INIT: {
424 struct pcm_stream_info *str_info;
425 struct stream_info *stream;
427 pr_debug("stream init called\n");
428 str_info = (struct pcm_stream_info *)arg;
429 str_id = str_info->str_id;
430 retval = sst_validate_strid(str_id);
434 stream = &sst_drv_ctx->streams[str_id];
435 pr_debug("setting the period ptrs\n");
436 stream->pcm_substream = str_info->mad_substream;
437 stream->period_elapsed = str_info->period_elapsed;
438 stream->sfreq = str_info->sfreq;
439 stream->prev = stream->status;
440 stream->status = STREAM_INIT;
444 case SST_SND_BUFFER_POINTER: {
445 struct pcm_stream_info *stream_info;
446 struct snd_sst_tstamp fw_tstamp = {0,};
447 struct stream_info *stream;
450 stream_info = (struct pcm_stream_info *)arg;
451 str_id = stream_info->str_id;
452 retval = sst_validate_strid(str_id);
455 stream = &sst_drv_ctx->streams[str_id];
457 if (!stream->pcm_substream)
459 memcpy_fromio(&fw_tstamp,
460 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
461 +(str_id * sizeof(fw_tstamp))),
464 pr_debug("Pointer Query on strid = %d ops %d\n",
465 str_id, stream->ops);
467 if (stream->ops == STREAM_OPS_PLAYBACK)
468 stream_info->buffer_ptr = fw_tstamp.samples_rendered;
470 stream_info->buffer_ptr = fw_tstamp.samples_processed;
471 pr_debug("Samples rendered = %llu, buffer ptr %llu\n",
472 fw_tstamp.samples_rendered, stream_info->buffer_ptr);
475 case SST_ENABLE_RX_TIME_SLOT: {
476 int status = *(int *)arg;
477 sst_drv_ctx->rx_time_slot_status = status ;
478 sst_enable_rx_timeslot(status);
483 pr_warn("illegal req\n");
491 struct intel_sst_pcm_control pcm_ops = {
492 .open = sst_open_pcm_stream,
493 .device_control = sst_device_control,
494 .close = sst_close_pcm_stream,
497 struct intel_sst_card_ops sst_pmic_ops = {
498 .pcm_control = &pcm_ops,
502 * register_sst_card - function for sound card to register
504 * @card: pointer to structure of operations
506 * This function is called card driver loads and is ready for registration
508 int register_sst_card(struct intel_sst_card_ops *card)
511 pr_err("No SST driver register card reject\n");
515 if (!card || !card->module_name) {
516 pr_err("Null Pointer Passed\n");
519 if (sst_drv_ctx->pmic_state == SND_MAD_UN_INIT) {
520 /* register this driver */
521 if ((strncmp(SST_CARD_NAMES, card->module_name,
522 strlen(SST_CARD_NAMES))) == 0) {
523 sst_drv_ctx->pmic_vendor = card->vendor_id;
524 sst_drv_ctx->scard_ops = card->scard_ops;
525 sst_pmic_ops.module_name = card->module_name;
526 sst_drv_ctx->pmic_state = SND_MAD_INIT_DONE;
527 sst_drv_ctx->rx_time_slot_status = 0; /*default AMIC*/
528 card->pcm_control = sst_pmic_ops.pcm_control;
531 pr_err("strcmp fail %s\n", card->module_name);
536 /* already registered a driver */
537 pr_err("Repeat for registration..denied\n");
540 /* The ASoC code doesn't set scard_ops */
541 if (sst_drv_ctx->scard_ops)
542 sst_drv_ctx->scard_ops->card_status = SND_CARD_UN_INIT;
545 EXPORT_SYMBOL_GPL(register_sst_card);
548 * unregister_sst_card- function for sound card to un-register
550 * @card: pointer to structure of operations
552 * This function is called when card driver unloads
554 void unregister_sst_card(struct intel_sst_card_ops *card)
556 if (sst_pmic_ops.pcm_control == card->pcm_control) {
558 sst_pmic_ops.module_name = "";
559 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
560 pr_debug("Unregistered %s\n", card->module_name);
564 EXPORT_SYMBOL_GPL(unregister_sst_card);