2 * sst_stream.c - Intel SST Driver for audio engine
4 * Copyright (C) 2008-14 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 * KP Jeeja <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/pci.h>
23 #include <linux/firmware.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/pm_runtime.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/soc.h>
30 #include <sound/compress_driver.h>
31 #include <asm/platform_sst_audio.h>
32 #include "../sst-mfld-platform.h"
34 #include "../../common/sst-dsp.h"
36 int sst_alloc_stream_mrfld(struct intel_sst_drv *sst_drv_ctx, void *params)
38 struct snd_pcm_params *pcm_params;
39 struct snd_sst_params *str_params;
40 struct snd_sst_tstamp fw_tstamp;
41 struct stream_info *str_info;
42 int i, num_ch, str_id;
44 dev_dbg(sst_drv_ctx->dev, "Enter\n");
46 str_params = (struct snd_sst_params *)params;
47 str_id = str_params->stream_id;
48 str_info = get_stream_info(sst_drv_ctx, str_id);
52 memset(&str_info->alloc_param, 0, sizeof(str_info->alloc_param));
53 str_info->alloc_param.operation = str_params->ops;
54 str_info->alloc_param.codec_type = str_params->codec;
55 str_info->alloc_param.sg_count = str_params->aparams.sg_count;
56 str_info->alloc_param.ring_buf_info[0].addr =
57 str_params->aparams.ring_buf_info[0].addr;
58 str_info->alloc_param.ring_buf_info[0].size =
59 str_params->aparams.ring_buf_info[0].size;
60 str_info->alloc_param.frag_size = str_params->aparams.frag_size;
62 memcpy(&str_info->alloc_param.codec_params, &str_params->sparams,
63 sizeof(struct snd_sst_stream_params));
66 * fill channel map params for multichannel support.
67 * Ideally channel map should be received from upper layers
68 * for multichannel support.
69 * Currently hardcoding as per FW reqm.
71 num_ch = sst_get_num_channel(str_params);
72 pcm_params = &str_info->alloc_param.codec_params.uc.pcm_params;
73 for (i = 0; i < 8; i++) {
75 pcm_params->channel_map[i] = i;
77 pcm_params->channel_map[i] = 0xff;
80 sst_drv_ctx->streams[str_id].status = STREAM_INIT;
81 sst_drv_ctx->streams[str_id].prev = STREAM_UN_INIT;
82 sst_drv_ctx->streams[str_id].pipe_id = str_params->device_type;
83 sst_drv_ctx->streams[str_id].task_id = str_params->task;
84 sst_drv_ctx->streams[str_id].num_ch = num_ch;
86 if (sst_drv_ctx->info.lpe_viewpt_rqd)
87 str_info->alloc_param.ts = sst_drv_ctx->info.mailbox_start +
88 sst_drv_ctx->tstamp + (str_id * sizeof(fw_tstamp));
90 str_info->alloc_param.ts = sst_drv_ctx->mailbox_add +
91 sst_drv_ctx->tstamp + (str_id * sizeof(fw_tstamp));
93 dev_dbg(sst_drv_ctx->dev, "alloc tstamp location = 0x%x\n",
94 str_info->alloc_param.ts);
95 dev_dbg(sst_drv_ctx->dev, "assigned pipe id 0x%x to task %d\n",
96 str_info->pipe_id, str_info->task_id);
98 return sst_realloc_stream(sst_drv_ctx, str_id);
102 * sst_realloc_stream - Send msg for (re-)allocating a stream using the
103 * @sst_drv_ctx intel_sst_drv context pointer
106 * Send a msg for (re-)allocating a stream using the parameters previously
107 * passed to sst_alloc_stream_mrfld() for the same stream ID.
108 * Return: 0 or negative errno value.
110 int sst_realloc_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
112 struct snd_sst_alloc_response *response;
113 struct stream_info *str_info;
117 str_info = get_stream_info(sst_drv_ctx, str_id);
121 dev_dbg(sst_drv_ctx->dev, "Alloc for str %d pipe %#x\n",
122 str_id, str_info->pipe_id);
124 ret = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id, IPC_CMD,
125 IPC_IA_ALLOC_STREAM_MRFLD, str_info->pipe_id,
126 sizeof(str_info->alloc_param), &str_info->alloc_param,
127 &data, true, true, false, true);
130 dev_err(sst_drv_ctx->dev, "FW alloc failed ret %d\n", ret);
131 /* alloc failed, so reset the state to uninit */
132 str_info->status = STREAM_UN_INIT;
135 response = (struct snd_sst_alloc_response *)data;
136 ret = response->str_type.result;
139 dev_err(sst_drv_ctx->dev, "FW alloc failed ret %d\n", ret);
140 if (ret == SST_ERR_STREAM_IN_USE) {
141 dev_err(sst_drv_ctx->dev,
142 "FW not in clean state, send free for:%d\n", str_id);
143 sst_free_stream(sst_drv_ctx, str_id);
153 * sst_start_stream - Send msg for a starting stream
156 * This function is called by any function which wants to start
159 int sst_start_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
162 struct stream_info *str_info;
165 dev_dbg(sst_drv_ctx->dev, "sst_start_stream for %d\n", str_id);
166 str_info = get_stream_info(sst_drv_ctx, str_id);
169 if (str_info->status != STREAM_RUNNING)
172 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id,
173 IPC_CMD, IPC_IA_START_STREAM_MRFLD, str_info->pipe_id,
174 sizeof(u16), &data, NULL, true, true, true, false);
179 int sst_send_byte_stream_mrfld(struct intel_sst_drv *sst_drv_ctx,
180 struct snd_sst_bytes_v2 *bytes)
181 { struct ipc_post *msg = NULL;
184 struct sst_block *block = NULL;
186 dev_dbg(sst_drv_ctx->dev,
187 "type:%u ipc_msg:%u block:%u task_id:%u pipe: %#x length:%#x\n",
188 bytes->type, bytes->ipc_msg, bytes->block, bytes->task_id,
189 bytes->pipe_id, bytes->len);
191 if (sst_create_ipc_msg(&msg, true))
194 pvt_id = sst_assign_pvt_id(sst_drv_ctx);
195 sst_fill_header_mrfld(&msg->mrfld_header, bytes->ipc_msg,
196 bytes->task_id, 1, pvt_id);
197 msg->mrfld_header.p.header_high.part.res_rqd = bytes->block;
199 msg->mrfld_header.p.header_low_payload = length;
200 dev_dbg(sst_drv_ctx->dev, "length is %d\n", length);
201 memcpy(msg->mailbox_data, &bytes->bytes, bytes->len);
203 block = sst_create_block(sst_drv_ctx, bytes->ipc_msg, pvt_id);
211 sst_add_to_dispatch_list_and_post(sst_drv_ctx, msg);
212 dev_dbg(sst_drv_ctx->dev, "msg->mrfld_header.p.header_low_payload:%d",
213 msg->mrfld_header.p.header_low_payload);
216 ret = sst_wait_timeout(sst_drv_ctx, block);
218 dev_err(sst_drv_ctx->dev, "fw returned err %d\n", ret);
219 sst_free_block(sst_drv_ctx, block);
223 if (bytes->type == SND_SST_BYTES_GET) {
225 * copy the reply and send back
226 * we need to update only sz and payload
229 unsigned char *r = block->data;
231 dev_dbg(sst_drv_ctx->dev, "read back %d bytes",
233 memcpy(bytes->bytes, r, bytes->len);
237 sst_free_block(sst_drv_ctx, block);
239 test_and_clear_bit(pvt_id, &sst_drv_ctx->pvt_id);
244 * sst_pause_stream - Send msg for a pausing stream
247 * This function is called by any function which wants to pause
248 * an already running stream.
250 int sst_pause_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
253 struct stream_info *str_info;
255 dev_dbg(sst_drv_ctx->dev, "SST DBG:sst_pause_stream for %d\n", str_id);
256 str_info = get_stream_info(sst_drv_ctx, str_id);
259 if (str_info->status == STREAM_PAUSED)
261 if (str_info->status == STREAM_RUNNING ||
262 str_info->status == STREAM_INIT) {
263 if (str_info->prev == STREAM_UN_INIT)
266 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id, IPC_CMD,
267 IPC_IA_PAUSE_STREAM_MRFLD, str_info->pipe_id,
268 0, NULL, NULL, true, true, false, true);
271 str_info->prev = str_info->status;
272 str_info->status = STREAM_PAUSED;
273 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
275 mutex_lock(&sst_drv_ctx->sst_lock);
276 sst_clean_stream(str_info);
277 mutex_unlock(&sst_drv_ctx->sst_lock);
281 dev_dbg(sst_drv_ctx->dev, "SST DBG:BADRQC for stream\n");
288 * sst_resume_stream - Send msg for resuming stream
291 * This function is called by any function which wants to resume
292 * an already paused stream.
294 int sst_resume_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
297 struct stream_info *str_info;
299 dev_dbg(sst_drv_ctx->dev, "SST DBG:sst_resume_stream for %d\n", str_id);
300 str_info = get_stream_info(sst_drv_ctx, str_id);
303 if (str_info->status == STREAM_RUNNING)
306 if (str_info->resume_status == STREAM_PAUSED &&
307 str_info->resume_prev == STREAM_RUNNING) {
309 * Stream was running before suspend and re-created on resume,
310 * start it to get back to running state.
312 dev_dbg(sst_drv_ctx->dev, "restart recreated stream after resume\n");
313 str_info->status = STREAM_RUNNING;
314 str_info->prev = STREAM_PAUSED;
315 retval = sst_start_stream(sst_drv_ctx, str_id);
316 str_info->resume_status = STREAM_UN_INIT;
317 } else if (str_info->resume_status == STREAM_PAUSED &&
318 str_info->resume_prev == STREAM_INIT) {
320 * Stream was idle before suspend and re-created on resume,
323 dev_dbg(sst_drv_ctx->dev, "leaving recreated stream idle after resume\n");
324 str_info->status = STREAM_INIT;
325 str_info->prev = STREAM_PAUSED;
326 str_info->resume_status = STREAM_UN_INIT;
327 } else if (str_info->status == STREAM_PAUSED) {
328 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id,
329 IPC_CMD, IPC_IA_RESUME_STREAM_MRFLD,
330 str_info->pipe_id, 0, NULL, NULL,
331 true, true, false, true);
334 if (str_info->prev == STREAM_RUNNING)
335 str_info->status = STREAM_RUNNING;
337 str_info->status = STREAM_INIT;
338 str_info->prev = STREAM_PAUSED;
339 } else if (retval == -SST_ERR_INVALID_STREAM_ID) {
341 mutex_lock(&sst_drv_ctx->sst_lock);
342 sst_clean_stream(str_info);
343 mutex_unlock(&sst_drv_ctx->sst_lock);
347 dev_err(sst_drv_ctx->dev, "SST ERR: BADQRC for stream\n");
355 * sst_drop_stream - Send msg for stopping stream
358 * This function is called by any function which wants to stop
361 int sst_drop_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
364 struct stream_info *str_info;
366 dev_dbg(sst_drv_ctx->dev, "SST DBG:sst_drop_stream for %d\n", str_id);
367 str_info = get_stream_info(sst_drv_ctx, str_id);
371 if (str_info->status != STREAM_UN_INIT) {
372 str_info->prev = STREAM_UN_INIT;
373 str_info->status = STREAM_INIT;
374 str_info->cumm_bytes = 0;
375 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id,
376 IPC_CMD, IPC_IA_DROP_STREAM_MRFLD,
377 str_info->pipe_id, 0, NULL, NULL,
378 true, true, true, false);
381 dev_dbg(sst_drv_ctx->dev, "BADQRC for stream, state %x\n",
388 * sst_drain_stream - Send msg for draining stream
391 * This function is called by any function which wants to drain
394 int sst_drain_stream(struct intel_sst_drv *sst_drv_ctx,
395 int str_id, bool partial_drain)
398 struct stream_info *str_info;
400 dev_dbg(sst_drv_ctx->dev, "SST DBG:sst_drain_stream for %d\n", str_id);
401 str_info = get_stream_info(sst_drv_ctx, str_id);
404 if (str_info->status != STREAM_RUNNING &&
405 str_info->status != STREAM_INIT &&
406 str_info->status != STREAM_PAUSED) {
407 dev_err(sst_drv_ctx->dev, "SST ERR: BADQRC for stream = %d\n",
412 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id, IPC_CMD,
413 IPC_IA_DRAIN_STREAM_MRFLD, str_info->pipe_id,
414 sizeof(u8), &partial_drain, NULL, true, true, false, false);
416 * with new non blocked drain implementation in core we dont need to
417 * wait for respsonse, and need to only invoke callback for drain
425 * sst_free_stream - Frees a stream
428 * This function is called by any function which wants to free
431 int sst_free_stream(struct intel_sst_drv *sst_drv_ctx, int str_id)
434 struct stream_info *str_info;
436 dev_dbg(sst_drv_ctx->dev, "SST DBG:sst_free_stream for %d\n", str_id);
438 mutex_lock(&sst_drv_ctx->sst_lock);
439 if (sst_drv_ctx->sst_state == SST_RESET) {
440 mutex_unlock(&sst_drv_ctx->sst_lock);
443 mutex_unlock(&sst_drv_ctx->sst_lock);
444 str_info = get_stream_info(sst_drv_ctx, str_id);
448 mutex_lock(&str_info->lock);
449 if (str_info->status != STREAM_UN_INIT) {
450 str_info->prev = str_info->status;
451 str_info->status = STREAM_UN_INIT;
452 mutex_unlock(&str_info->lock);
454 dev_dbg(sst_drv_ctx->dev, "Free for str %d pipe %#x\n",
455 str_id, str_info->pipe_id);
456 retval = sst_prepare_and_post_msg(sst_drv_ctx, str_info->task_id, IPC_CMD,
457 IPC_IA_FREE_STREAM_MRFLD, str_info->pipe_id, 0,
458 NULL, NULL, true, true, false, true);
460 dev_dbg(sst_drv_ctx->dev, "sst: wait for free returned %d\n",
462 mutex_lock(&sst_drv_ctx->sst_lock);
463 sst_clean_stream(str_info);
464 mutex_unlock(&sst_drv_ctx->sst_lock);
465 dev_dbg(sst_drv_ctx->dev, "SST DBG:Stream freed\n");
467 mutex_unlock(&str_info->lock);
469 dev_dbg(sst_drv_ctx->dev, "SST DBG:BADQRC for stream\n");