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 (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();
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]);
127 * sst_get_stream_allocated - this function gets a stream allocated with
130 * @str_param : stream params
131 * @lib_dnld : pointer to pointer of lib downlaod struct
133 * This creates new stream id for a stream, in case lib is to be downloaded to
134 * DSP, it downloads that
136 int sst_get_stream_allocated(struct snd_sst_params *str_param,
137 struct snd_sst_lib_download **lib_dnld)
140 struct stream_info *str_info;
142 retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
143 str_param->codec, str_param->device_type);
145 pr_err("sst_alloc_stream failed %d\n", retval);
148 pr_debug("Stream allocated %d\n", 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)) {
155 pr_debug("FW alloc failed retval %d, ret_code %d\n",
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);
161 pr_debug("FW Stream allocated success\n");
162 return str_id; /*will ret either error (in above if) or correct str id*/
166 * sst_get_sfreq - this function returns the frequency of the stream
168 * @str_param : stream params
170 static int sst_get_sfreq(struct snd_sst_params *str_param)
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:
178 return str_param->sparams.uc.aac_params.sfreq;
179 case SST_CODEC_TYPE_WMA9:
180 return str_param->sparams.uc.wma_params.sfreq;
187 * sst_get_stream - this function prepares for stream allocation
189 * @str_param : stream param
191 int sst_get_stream(struct snd_sst_params *str_param)
194 struct stream_info *str_info;
195 struct snd_sst_lib_download *lib_dnld;
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;
203 pr_debug("Codec is required.... trying that\n");
204 if (lib_dnld == NULL) {
205 pr_err("lib download null!!! abort\n");
208 i = sst_get_block_stream(sst_drv_ctx);
209 response = sst_drv_ctx->alloc_block[i].ops_block.data;
210 pr_debug("alloc block allocated = %d\n", i);
215 retval = sst_load_library(lib_dnld, str_param->ops);
218 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
220 pr_debug("codec was downloaded successfully\n");
222 retval = sst_get_stream_allocated(str_param, &lib_dnld);
226 pr_debug("Alloc done stream id %d\n", retval);
228 pr_debug("codec download failed\n");
232 } else if (retval <= 0)
235 set_port_params(str_param, str_param->ops);*/
237 /* store sampling freq */
238 str_info = &sst_drv_ctx->streams[retval];
239 str_info->sfreq = sst_get_sfreq(str_param);
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);
248 sst_drv_ctx->scard_ops->power_up_pmic_pb(
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) {
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++;
264 void sst_process_mad_ops(struct work_struct *work)
267 struct mad_ops_wq *mad_ops =
268 container_of(work, struct mad_ops_wq, wq);
271 switch (mad_ops->control_op) {
273 retval = sst_pause_stream(mad_ops->stream_id);
276 retval = sst_resume_stream(mad_ops->stream_id);
279 /* retval = sst_drop_stream(mad_ops->stream_id);
282 pr_debug("SST Debug: start stream\n");
283 retval = sst_start_stream(mad_ops->stream_id);
285 case SST_SND_STREAM_PROCESS:
286 pr_debug("play/capt frames...\n");
289 pr_err(" wrong control_ops reported\n");
294 void send_intial_rx_timeslot(void)
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);
303 * sst_open_pcm_stream - Open PCM interface
305 * @str_param: parameters of pcm stream
307 * This function is called by MID sound card driver to open
308 * a new pcm interface
310 int sst_open_pcm_stream(struct snd_sst_params *str_param)
312 struct stream_info *str_info;
315 pm_runtime_get_sync(&sst_drv_ctx->pci->dev);
317 if (sst_drv_ctx->sst_state == SST_SUSPENDED) {
318 /* LPE is suspended, resume it before proceeding*/
319 pr_debug("Resuming from Suspended state\n");
320 retval = intel_sst_resume(sst_drv_ctx->pci);
322 pr_err("Resume Failed = %#x, abort\n", retval);
323 pm_runtime_put(&sst_drv_ctx->pci->dev);
327 if (sst_drv_ctx->sst_state == SST_UN_INIT) {
328 /* FW is not downloaded */
329 pr_debug("DSP Downloading FW now...\n");
330 retval = sst_download_fw();
332 pr_err("FW download fail %x, abort\n", retval);
333 pm_runtime_put(&sst_drv_ctx->pci->dev);
336 send_intial_rx_timeslot();
340 pm_runtime_put(&sst_drv_ctx->pci->dev);
344 retval = sst_get_stream(str_param);
346 sst_drv_ctx->stream_cnt++;
347 str_info = &sst_drv_ctx->streams[retval];
348 str_info->src = MAD_DRV;
350 pm_runtime_put(&sst_drv_ctx->pci->dev);
356 * sst_close_pcm_stream - Close PCM interface
358 * @str_id: stream id to be closed
360 * This function is called by MID sound card driver to close
361 * an existing pcm interface
363 int sst_close_pcm_stream(unsigned int str_id)
365 struct stream_info *stream;
367 pr_debug("sst: stream free called\n");
368 if (sst_validate_strid(str_id))
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");
377 pm_runtime_put(&sst_drv_ctx->pci->dev);
382 * sst_device_control - Set Control params
384 * @cmd: control cmd to be set
385 * @arg: command argument
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
391 int sst_device_control(int cmd, void *arg)
393 int retval = 0, str_id = 0;
400 sst_drv_ctx->mad_ops.control_op = cmd;
401 sst_drv_ctx->mad_ops.stream_id = *(int *)arg;
402 queue_work(sst_drv_ctx->mad_wq, &sst_drv_ctx->mad_ops.wq);
405 case SST_SND_STREAM_INIT: {
406 struct pcm_stream_info *str_info;
407 struct stream_info *stream;
409 pr_debug("stream init called\n");
410 str_info = (struct pcm_stream_info *)arg;
411 str_id = str_info->str_id;
412 retval = sst_validate_strid(str_id);
416 stream = &sst_drv_ctx->streams[str_id];
417 pr_debug("setting the period ptrs\n");
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;
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;
432 stream_info = (struct pcm_stream_info *)arg;
433 str_id = stream_info->str_id;
434 retval = sst_validate_strid(str_id);
437 stream = &sst_drv_ctx->streams[str_id];
439 if (!stream->pcm_substream)
441 memcpy_fromio(&fw_tstamp,
442 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
443 +(str_id * sizeof(fw_tstamp))),
446 pr_debug("Pointer Query on strid = %d ops %d\n",
447 str_id, stream->ops);
449 if (stream->ops == STREAM_OPS_PLAYBACK)
450 stream_info->buffer_ptr = fw_tstamp.samples_rendered;
452 stream_info->buffer_ptr = fw_tstamp.samples_processed;
453 pr_debug("Samples rendered = %llu, buffer ptr %llu\n",
454 fw_tstamp.samples_rendered, stream_info->buffer_ptr);
457 case SST_ENABLE_RX_TIME_SLOT: {
458 int status = *(int *)arg;
459 sst_drv_ctx->rx_time_slot_status = status ;
460 sst_enable_rx_timeslot(status);
465 pr_warn("illegal req\n");
473 struct intel_sst_pcm_control pcm_ops = {
474 .open = sst_open_pcm_stream,
475 .device_control = sst_device_control,
476 .close = sst_close_pcm_stream,
479 struct intel_sst_card_ops sst_pmic_ops = {
480 .pcm_control = &pcm_ops,
484 * register_sst_card - function for sound card to register
486 * @card: pointer to structure of operations
488 * This function is called card driver loads and is ready for registration
490 int register_sst_card(struct intel_sst_card_ops *card)
493 pr_err("No SST driver register card reject\n");
497 if (!card || !card->module_name) {
498 pr_err("Null Pointer Passed\n");
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*/
510 card->pcm_control = sst_pmic_ops.pcm_control;
511 sst_drv_ctx->scard_ops->card_status = SND_CARD_UN_INIT;
514 pr_err("strcmp fail %s\n", card->module_name);
519 /* already registered a driver */
520 pr_err("Repeat for registration..denied\n");
525 EXPORT_SYMBOL_GPL(register_sst_card);
528 * unregister_sst_card- function for sound card to un-register
530 * @card: pointer to structure of operations
532 * This function is called when card driver unloads
534 void unregister_sst_card(struct intel_sst_card_ops *card)
536 if (sst_pmic_ops.pcm_control == card->pcm_control) {
538 sst_pmic_ops.module_name = "";
539 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
540 pr_debug("Unregistered %s\n", card->module_name);
544 EXPORT_SYMBOL_GPL(unregister_sst_card);