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 #include <linux/pci.h>
31 #include <linux/firmware.h>
32 #include "intel_sst.h"
33 #include "intel_sst_ioctl.h"
34 #include "intel_sst_fw_ipc.h"
35 #include "intel_sst_common.h"
39 * sst_download_fw - download the audio firmware to DSP
41 * This function is called when the FW needs to be downloaded to SST DSP engine
43 int sst_download_fw(void)
46 const struct firmware *fw_sst;
48 if (sst_drv_ctx->sst_state != SST_UN_INIT)
50 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
51 name = SST_FW_FILENAME_MRST;
53 name = SST_FW_FILENAME_MFLD;
54 pr_debug("sst: Downloading %s FW now...\n", name);
55 retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev);
57 pr_err("sst: request fw failed %d\n", retval);
60 sst_drv_ctx->alloc_block[0].sst_id = FW_DWNL_ID;
61 sst_drv_ctx->alloc_block[0].ops_block.condition = false;
62 retval = sst_load_fw(fw_sst, NULL);
66 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]);
68 pr_err("sst: fw download failed %d\n" , retval);
70 release_firmware(fw_sst);
71 sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT;
77 * sst_stalled - this function checks if the lpe is in stalled state
85 if (!sst_drv_ctx->lpe_stalled)
87 /*wait for time and re-check*/
92 pr_debug("sst: in Stalled State\n");
96 void free_stream_context(unsigned int str_id)
98 struct stream_info *stream;
100 if (!sst_validate_strid(str_id)) {
101 /* str_id is valid, so stream is alloacted */
102 stream = &sst_drv_ctx->streams[str_id];
103 if (stream->ops == STREAM_OPS_PLAYBACK ||
104 stream->ops == STREAM_OPS_PLAYBACK_DRM) {
105 sst_drv_ctx->pb_streams--;
106 if (sst_drv_ctx->pb_streams == 0)
107 sst_drv_ctx->scard_ops->power_down_pmic_pb();
108 } else if (stream->ops == STREAM_OPS_CAPTURE) {
109 sst_drv_ctx->cp_streams--;
110 if (sst_drv_ctx->cp_streams == 0)
111 sst_drv_ctx->scard_ops->power_down_pmic_cp();
113 if (sst_drv_ctx->pb_streams == 0
114 && sst_drv_ctx->cp_streams == 0)
115 sst_drv_ctx->scard_ops->power_down_pmic();
116 if (sst_free_stream(str_id))
117 sst_clean_stream(&sst_drv_ctx->streams[str_id]);
122 * sst_get_stream_allocated - this function gets a stream allocated with
125 * @str_param : stream params
126 * @lib_dnld : pointer to pointer of lib downlaod struct
128 * This creates new stream id for a stream, in case lib is to be downloaded to
129 * DSP, it downloads that
131 int sst_get_stream_allocated(struct snd_sst_params *str_param,
132 struct snd_sst_lib_download **lib_dnld)
135 struct stream_info *str_info;
137 retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
138 str_param->codec, str_param->device_type);
140 pr_err("sst: sst_alloc_stream failed %d\n", retval);
143 pr_debug("sst: Stream allocated %d\n", retval);
145 str_info = &sst_drv_ctx->streams[str_id];
146 /* Block the call for reply */
147 retval = sst_wait_interruptible_timeout(sst_drv_ctx,
148 &str_info->ctrl_blk, SST_BLOCK_TIMEOUT);
149 if ((retval != 0) || (str_info->ctrl_blk.ret_code != 0)) {
150 pr_debug("sst: FW alloc failed retval %d, ret_code %d\n",
151 retval, str_info->ctrl_blk.ret_code);
152 str_id = -str_info->ctrl_blk.ret_code; /*return error*/
153 *lib_dnld = str_info->ctrl_blk.data;
154 sst_clean_stream(str_info);
156 pr_debug("sst: FW Stream allocated sucess\n");
157 return str_id; /*will ret either error (in above if) or correct str id*/
161 * sst_get_sfreq - this function returns the frequency of the stream
163 * @str_param : stream params
165 static int sst_get_sfreq(struct snd_sst_params *str_param)
167 switch (str_param->codec) {
168 case SST_CODEC_TYPE_PCM:
169 return 48000; /*str_param->sparams.uc.pcm_params.sfreq;*/
170 case SST_CODEC_TYPE_MP3:
171 return str_param->sparams.uc.mp3_params.sfreq;
172 case SST_CODEC_TYPE_AAC:
173 return str_param->sparams.uc.aac_params.sfreq;;
174 case SST_CODEC_TYPE_WMA9:
175 return str_param->sparams.uc.wma_params.sfreq;;
182 * sst_get_stream - this function prepares for stream allocation
184 * @str_param : stream param
186 int sst_get_stream(struct snd_sst_params *str_param)
189 struct stream_info *str_info;
190 struct snd_sst_lib_download *lib_dnld;
192 /* stream is not allocated, we are allocating */
193 retval = sst_get_stream_allocated(str_param, &lib_dnld);
194 if (retval == -(SST_LIB_ERR_LIB_DNLD_REQUIRED)) {
195 /* codec download is required */
196 struct snd_sst_alloc_response *response;
198 pr_debug("sst: Codec is required.... trying that\n");
199 if (lib_dnld == NULL) {
200 pr_err("sst: lib download null!!! abort\n");
203 i = sst_get_block_stream(sst_drv_ctx);
204 response = sst_drv_ctx->alloc_block[i].ops_block.data;
205 pr_debug("sst: alloc block allocated = %d\n", i);
210 retval = sst_load_library(lib_dnld, str_param->ops);
213 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
215 pr_debug("sst: codec was downloaded sucesfully\n");
217 retval = sst_get_stream_allocated(str_param, &lib_dnld);
221 pr_debug("sst: Alloc done stream id %d\n", retval);
223 pr_debug("sst: codec download failed\n");
227 } else if (retval <= 0)
230 set_port_params(str_param, str_param->ops);*/
232 /* store sampling freq */
233 str_info = &sst_drv_ctx->streams[retval];
234 str_info->sfreq = sst_get_sfreq(str_param);
236 /* power on the analog, if reqd */
237 if (str_param->ops == STREAM_OPS_PLAYBACK ||
238 str_param->ops == STREAM_OPS_PLAYBACK_DRM) {
239 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
240 sst_drv_ctx->scard_ops->power_up_pmic_pb(
241 sst_drv_ctx->pmic_port_instance);
243 sst_drv_ctx->scard_ops->power_up_pmic_pb(
245 /*Only if the playback is MP3 - Send a message*/
246 sst_drv_ctx->pb_streams++;
247 } else if (str_param->ops == STREAM_OPS_CAPTURE) {
249 sst_drv_ctx->scard_ops->power_up_pmic_cp(
250 sst_drv_ctx->pmic_port_instance);
251 /*Send a messageif not sent already*/
252 sst_drv_ctx->cp_streams++;
259 void sst_process_mad_ops(struct work_struct *work)
262 struct mad_ops_wq *mad_ops =
263 container_of(work, struct mad_ops_wq, wq);
266 switch (mad_ops->control_op) {
268 retval = sst_pause_stream(mad_ops->stream_id);
271 retval = sst_resume_stream(mad_ops->stream_id);
274 /* retval = sst_drop_stream(mad_ops->stream_id);
277 pr_debug("SST Debug: start stream\n");
278 retval = sst_start_stream(mad_ops->stream_id);
280 case SST_SND_STREAM_PROCESS:
281 pr_debug("sst: play/capt frames...\n");
284 pr_err("sst: wrong control_ops reported\n");
289 * sst_control_set - Set Control params
291 * @control_list: list of controls to be set
293 * This function is called by MID sound card driver to set
294 * SST/Sound card controls. This is registered with MID driver
296 int sst_control_set(int control_element, void *value)
298 int retval = 0, str_id = 0;
299 struct stream_info *stream;
301 if (sst_drv_ctx->sst_state == SST_SUSPENDED) {
302 /*LPE is suspended, resume it before proceding*/
303 pr_debug("sst: Resuming from Suspended state\n");
304 retval = intel_sst_resume(sst_drv_ctx->pci);
306 pr_err("sst: Resume Failed = %#x, abort\n", retval);
310 if (sst_drv_ctx->sst_state == SST_UN_INIT) {
311 /* FW is not downloaded */
312 pr_debug("sst: DSP Downloading FW now...\n");
313 retval = sst_download_fw();
315 pr_err("sst: FW download fail %x, abort\n", retval);
318 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID &&
319 sst_drv_ctx->rx_time_slot_status != RX_TIMESLOT_UNINIT
320 && sst_drv_ctx->pmic_vendor != SND_NC)
321 sst_enable_rx_timeslot(
322 sst_drv_ctx->rx_time_slot_status);
325 switch (control_element) {
326 case SST_SND_ALLOC: {
327 struct snd_sst_params *str_param;
328 struct stream_info *str_info;
330 str_param = (struct snd_sst_params *)value;
332 retval = sst_get_stream(str_param);
334 sst_drv_ctx->stream_cnt++;
335 str_info = &sst_drv_ctx->streams[retval];
336 str_info->src = MAD_DRV;
344 sst_drv_ctx->mad_ops.control_op = control_element;
345 sst_drv_ctx->mad_ops.stream_id = *(int *)value;
346 queue_work(sst_drv_ctx->mad_wq, &sst_drv_ctx->mad_ops.wq);
350 str_id = *(int *)value;
351 stream = &sst_drv_ctx->streams[str_id];
352 free_stream_context(str_id);
353 stream->pcm_substream = NULL;
354 stream->status = STREAM_UN_INIT;
355 stream->period_elapsed = NULL;
356 sst_drv_ctx->stream_cnt--;
359 case SST_SND_STREAM_INIT: {
360 struct pcm_stream_info *str_info;
361 struct stream_info *stream;
363 pr_debug("sst: stream init called\n");
364 str_info = (struct pcm_stream_info *)value;
365 str_id = str_info->str_id;
366 retval = sst_validate_strid(str_id);
370 stream = &sst_drv_ctx->streams[str_id];
371 pr_debug("sst: setting the period ptrs\n");
372 stream->pcm_substream = str_info->mad_substream;
373 stream->period_elapsed = str_info->period_elapsed;
374 stream->sfreq = str_info->sfreq;
375 stream->prev = stream->status;
376 stream->status = STREAM_INIT;
380 case SST_SND_BUFFER_POINTER: {
381 struct pcm_stream_info *stream_info;
382 struct snd_sst_tstamp fw_tstamp = {0,};
383 struct stream_info *stream;
386 stream_info = (struct pcm_stream_info *)value;
387 str_id = stream_info->str_id;
388 retval = sst_validate_strid(str_id);
391 stream = &sst_drv_ctx->streams[str_id];
393 if (!stream->pcm_substream)
395 memcpy_fromio(&fw_tstamp,
396 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
397 +(str_id * sizeof(fw_tstamp))),
400 pr_debug("sst: Pointer Query on strid = %d ops %d\n",
401 str_id, stream->ops);
403 if (stream->ops == STREAM_OPS_PLAYBACK)
404 stream_info->buffer_ptr = fw_tstamp.samples_rendered;
406 stream_info->buffer_ptr = fw_tstamp.samples_processed;
407 pr_debug("sst: Samples rendered = %llu, buffer ptr %llu\n",
408 fw_tstamp.samples_rendered, stream_info->buffer_ptr);
411 case SST_ENABLE_RX_TIME_SLOT: {
412 int status = *(int *)value;
413 sst_drv_ctx->rx_time_slot_status = status ;
414 sst_enable_rx_timeslot(status);
419 pr_warn("sst: illegal req\n");
427 struct intel_sst_card_ops sst_pmic_ops = {
428 .control_set = sst_control_set,
432 * register_sst_card - function for sound card to register
434 * @card: pointer to structure of operations
436 * This function is called card driver loads and is ready for registration
438 int register_sst_card(struct intel_sst_card_ops *card)
441 pr_err("sst: No SST driver register card reject\n");
445 if (!card || !card->module_name) {
446 pr_err("sst: Null Pointer Passed\n");
449 if (sst_drv_ctx->pmic_state == SND_MAD_UN_INIT) {
450 /* register this driver */
451 if ((strncmp(SST_CARD_NAMES, card->module_name,
452 strlen(SST_CARD_NAMES))) == 0) {
453 sst_drv_ctx->pmic_vendor = card->vendor_id;
454 sst_drv_ctx->scard_ops = card->scard_ops;
455 sst_pmic_ops.module_name = card->module_name;
456 sst_drv_ctx->pmic_state = SND_MAD_INIT_DONE;
457 sst_drv_ctx->rx_time_slot_status = 0; /*default AMIC*/
458 card->control_set = sst_pmic_ops.control_set;
459 sst_drv_ctx->scard_ops->card_status = SND_CARD_UN_INIT;
462 pr_err("sst: strcmp fail %s\n", card->module_name);
467 /* already registered a driver */
468 pr_err("sst: Repeat for registeration..denied\n");
473 EXPORT_SYMBOL_GPL(register_sst_card);
476 * unregister_sst_card- function for sound card to un-register
478 * @card: pointer to structure of operations
480 * This function is called when card driver unloads
482 void unregister_sst_card(struct intel_sst_card_ops *card)
484 if (sst_pmic_ops.control_set == card->control_set) {
486 sst_pmic_ops.module_name = "";
487 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
488 pr_debug("sst: Unregistered %s\n", card->module_name);
492 EXPORT_SYMBOL_GPL(unregister_sst_card);