1 /*
2 * intel_sst_interface.c - Intel SST Driver for audio engine
3 *
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
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.
13 *
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.
18 *
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.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 * This driver exposes the audio engine functionalities to the ALSA
25 * and middleware.
26 * Upper layer interfaces (MAD driver, MMF) to SST driver
27 */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/fs.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"
40
41
42 /*
43 * sst_download_fw - download the audio firmware to DSP
44 *
45 * This function is called when the FW needs to be downloaded to SST DSP engine
46 */
sst_download_fw(void)47 int sst_download_fw(void)
48 {
49 int retval;
50 const struct firmware *fw_sst;
51 char name[20];
52
53 if (sst_drv_ctx->sst_state != SST_UN_INIT)
54 return -EPERM;
55
56 snprintf(name, sizeof(name), "%s%04x%s", "fw_sst_",
57 sst_drv_ctx->pci_id, ".bin");
58
59 pr_debug("Downloading %s FW now...\n", name);
60 retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev);
61 if (retval) {
62 pr_err("request fw failed %d\n", retval);
63 return retval;
64 }
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);
68 if (retval)
69 goto end_restore;
70
71 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]);
72 if (retval)
73 pr_err("fw download failed %d\n" , retval);
74 end_restore:
75 release_firmware(fw_sst);
76 sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT;
77 return retval;
78 }
79
80
81 /*
82 * sst_stalled - this function checks if the lpe is in stalled state
83 */
sst_stalled(void)84 int sst_stalled(void)
85 {
86 int retry = 1000;
87 int retval = -1;
88
89 while (retry) {
90 if (!sst_drv_ctx->lpe_stalled)
91 return 0;
92 /*wait for time and re-check*/
93 msleep(1);
94
95 retry--;
96 }
97 pr_debug("in Stalled State\n");
98 return retval;
99 }
100
free_stream_context(unsigned int str_id)101 void free_stream_context(unsigned int str_id)
102 {
103 struct stream_info *stream;
104
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();
117 }
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]);
123 }
124 }
125
126 /*
127 * sst_get_stream_allocated - this function gets a stream allocated with
128 * the given params
129 *
130 * @str_param : stream params
131 * @lib_dnld : pointer to pointer of lib downlaod struct
132 *
133 * This creates new stream id for a stream, in case lib is to be downloaded to
134 * DSP, it downloads that
135 */
sst_get_stream_allocated(struct snd_sst_params * str_param,struct snd_sst_lib_download ** lib_dnld)136 int sst_get_stream_allocated(struct snd_sst_params *str_param,
137 struct snd_sst_lib_download **lib_dnld)
138 {
139 int retval, str_id;
140 struct stream_info *str_info;
141
142 retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops,
143 str_param->codec, str_param->device_type);
144 if (retval < 0) {
145 pr_err("sst_alloc_stream failed %d\n", retval);
146 return retval;
147 }
148 pr_debug("Stream allocated %d\n", retval);
149 str_id = 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);
160 } else
161 pr_debug("FW Stream allocated success\n");
162 return str_id; /*will ret either error (in above if) or correct str id*/
163 }
164
165 /*
166 * sst_get_sfreq - this function returns the frequency of the stream
167 *
168 * @str_param : stream params
169 */
sst_get_sfreq(struct snd_sst_params * str_param)170 static int sst_get_sfreq(struct snd_sst_params *str_param)
171 {
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;
181 default:
182 return 0;
183 }
184 }
185
186 /*
187 * sst_get_stream - this function prepares for stream allocation
188 *
189 * @str_param : stream param
190 */
sst_get_stream(struct snd_sst_params * str_param)191 int sst_get_stream(struct snd_sst_params *str_param)
192 {
193 int i, retval;
194 struct stream_info *str_info;
195 struct snd_sst_lib_download *lib_dnld;
196
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;
202
203 pr_debug("Codec is required.... trying that\n");
204 if (lib_dnld == NULL) {
205 pr_err("lib download null!!! abort\n");
206 return -EIO;
207 }
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);
211 if (i < 0) {
212 kfree(lib_dnld);
213 return -ENOMEM;
214 }
215 retval = sst_load_library(lib_dnld, str_param->ops);
216 kfree(lib_dnld);
217
218 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
219 if (!retval) {
220 pr_debug("codec was downloaded successfully\n");
221
222 retval = sst_get_stream_allocated(str_param, &lib_dnld);
223 if (retval <= 0)
224 goto err;
225
226 pr_debug("Alloc done stream id %d\n", retval);
227 } else {
228 pr_debug("codec download failed\n");
229 retval = -EIO;
230 goto err;
231 }
232 } else if (retval <= 0)
233 goto err;
234 /*else
235 set_port_params(str_param, str_param->ops);*/
236
237 /* store sampling freq */
238 str_info = &sst_drv_ctx->streams[retval];
239 str_info->sfreq = sst_get_sfreq(str_param);
240
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);
247 else
248 sst_drv_ctx->scard_ops->power_up_pmic_pb(
249 str_info->device);
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) {
253
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++;
258 }
259
260 err:
261 return retval;
262 }
263
sst_process_mad_ops(struct work_struct * work)264 void sst_process_mad_ops(struct work_struct *work)
265 {
266
267 struct mad_ops_wq *mad_ops =
268 container_of(work, struct mad_ops_wq, wq);
269 int retval = 0;
270
271 switch (mad_ops->control_op) {
272 case SST_SND_PAUSE:
273 retval = sst_pause_stream(mad_ops->stream_id);
274 break;
275 case SST_SND_RESUME:
276 retval = sst_resume_stream(mad_ops->stream_id);
277 break;
278 case SST_SND_DROP:
279 /* retval = sst_drop_stream(mad_ops->stream_id);
280 */ break;
281 case SST_SND_START:
282 pr_debug("SST Debug: start stream\n");
283 retval = sst_start_stream(mad_ops->stream_id);
284 break;
285 case SST_SND_STREAM_PROCESS:
286 pr_debug("play/capt frames...\n");
287 break;
288 default:
289 pr_err(" wrong control_ops reported\n");
290 }
291 return;
292 }
293
send_intial_rx_timeslot(void)294 void send_intial_rx_timeslot(void)
295 {
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);
300 }
301
302 /*
303 * sst_open_pcm_stream - Open PCM interface
304 *
305 * @str_param: parameters of pcm stream
306 *
307 * This function is called by MID sound card driver to open
308 * a new pcm interface
309 */
sst_open_pcm_stream(struct snd_sst_params * str_param)310 int sst_open_pcm_stream(struct snd_sst_params *str_param)
311 {
312 struct stream_info *str_info;
313 int retval;
314
315 pm_runtime_get_sync(&sst_drv_ctx->pci->dev);
316
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);
321 if (retval) {
322 pr_err("Resume Failed = %#x, abort\n", retval);
323 pm_runtime_put(&sst_drv_ctx->pci->dev);
324 return retval;
325 }
326 }
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();
331 if (retval) {
332 pr_err("FW download fail %x, abort\n", retval);
333 pm_runtime_put(&sst_drv_ctx->pci->dev);
334 return retval;
335 }
336 send_intial_rx_timeslot();
337 }
338
339 if (!str_param) {
340 pm_runtime_put(&sst_drv_ctx->pci->dev);
341 return -EINVAL;
342 }
343
344 retval = sst_get_stream(str_param);
345 if (retval > 0) {
346 sst_drv_ctx->stream_cnt++;
347 str_info = &sst_drv_ctx->streams[retval];
348 str_info->src = MAD_DRV;
349 } else
350 pm_runtime_put(&sst_drv_ctx->pci->dev);
351
352 return retval;
353 }
354
355 /*
356 * sst_close_pcm_stream - Close PCM interface
357 *
358 * @str_id: stream id to be closed
359 *
360 * This function is called by MID sound card driver to close
361 * an existing pcm interface
362 */
sst_close_pcm_stream(unsigned int str_id)363 int sst_close_pcm_stream(unsigned int str_id)
364 {
365 struct stream_info *stream;
366
367 pr_debug("sst: stream free called\n");
368 if (sst_validate_strid(str_id))
369 return -EINVAL;
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);
378 return 0;
379 }
380
381 /*
382 * sst_device_control - Set Control params
383 *
384 * @cmd: control cmd to be set
385 * @arg: command argument
386 *
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
390 */
sst_device_control(int cmd,void * arg)391 int sst_device_control(int cmd, void *arg)
392 {
393 int retval = 0, str_id = 0;
394
395 switch (cmd) {
396 case SST_SND_PAUSE:
397 case SST_SND_RESUME:
398 case SST_SND_DROP:
399 case SST_SND_START:
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);
403 break;
404
405 case SST_SND_STREAM_INIT: {
406 struct pcm_stream_info *str_info;
407 struct stream_info *stream;
408
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);
413 if (retval)
414 break;
415
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;
423 break;
424 }
425
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;
430
431
432 stream_info = (struct pcm_stream_info *)arg;
433 str_id = stream_info->str_id;
434 retval = sst_validate_strid(str_id);
435 if (retval)
436 break;
437 stream = &sst_drv_ctx->streams[str_id];
438
439 if (!stream->pcm_substream)
440 break;
441 memcpy_fromio(&fw_tstamp,
442 ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP)
443 +(str_id * sizeof(fw_tstamp))),
444 sizeof(fw_tstamp));
445
446 pr_debug("Pointer Query on strid = %d ops %d\n",
447 str_id, stream->ops);
448
449 if (stream->ops == STREAM_OPS_PLAYBACK)
450 stream_info->buffer_ptr = fw_tstamp.samples_rendered;
451 else
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);
455 break;
456 }
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);
461 break;
462 }
463 default:
464 /* Illegal case */
465 pr_warn("illegal req\n");
466 return -EINVAL;
467 }
468
469 return retval;
470 }
471
472
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,
477 };
478
479 struct intel_sst_card_ops sst_pmic_ops = {
480 .pcm_control = &pcm_ops,
481 };
482
483 /*
484 * register_sst_card - function for sound card to register
485 *
486 * @card: pointer to structure of operations
487 *
488 * This function is called card driver loads and is ready for registration
489 */
register_sst_card(struct intel_sst_card_ops * card)490 int register_sst_card(struct intel_sst_card_ops *card)
491 {
492 if (!sst_drv_ctx) {
493 pr_err("No SST driver register card reject\n");
494 return -ENODEV;
495 }
496
497 if (!card || !card->module_name) {
498 pr_err("Null Pointer Passed\n");
499 return -EINVAL;
500 }
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;
512 return 0;
513 } else {
514 pr_err("strcmp fail %s\n", card->module_name);
515 return -EINVAL;
516 }
517
518 } else {
519 /* already registered a driver */
520 pr_err("Repeat for registration..denied\n");
521 return -EBADRQC;
522 }
523 return 0;
524 }
525 EXPORT_SYMBOL_GPL(register_sst_card);
526
527 /*
528 * unregister_sst_card- function for sound card to un-register
529 *
530 * @card: pointer to structure of operations
531 *
532 * This function is called when card driver unloads
533 */
unregister_sst_card(struct intel_sst_card_ops * card)534 void unregister_sst_card(struct intel_sst_card_ops *card)
535 {
536 if (sst_pmic_ops.pcm_control == card->pcm_control) {
537 /* unreg */
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);
541 }
542 return;
543 }
544 EXPORT_SYMBOL_GPL(unregister_sst_card);
545