Commit | Line | Data |
---|---|---|
fffa1cca VK |
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 | ||
d0f40c50 JP |
29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
30 | ||
1dd8b732 | 31 | #include <linux/delay.h> |
fffa1cca VK |
32 | #include <linux/pci.h> |
33 | #include <linux/fs.h> | |
34 | #include <linux/firmware.h> | |
35 | #include "intel_sst.h" | |
36 | #include "intel_sst_ioctl.h" | |
37 | #include "intel_sst_fw_ipc.h" | |
38 | #include "intel_sst_common.h" | |
39 | ||
40 | ||
41 | /* | |
42 | * sst_download_fw - download the audio firmware to DSP | |
43 | * | |
44 | * This function is called when the FW needs to be downloaded to SST DSP engine | |
45 | */ | |
46 | int sst_download_fw(void) | |
47 | { | |
48 | int retval; | |
49 | const struct firmware *fw_sst; | |
50 | const char *name; | |
51 | if (sst_drv_ctx->sst_state != SST_UN_INIT) | |
52 | return -EPERM; | |
53 | if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) | |
54 | name = SST_FW_FILENAME_MRST; | |
55 | else | |
56 | name = SST_FW_FILENAME_MFLD; | |
d0f40c50 | 57 | pr_debug("Downloading %s FW now...\n", name); |
fffa1cca VK |
58 | retval = request_firmware(&fw_sst, name, &sst_drv_ctx->pci->dev); |
59 | if (retval) { | |
d0f40c50 | 60 | pr_err("request fw failed %d\n", retval); |
fffa1cca VK |
61 | return retval; |
62 | } | |
63 | sst_drv_ctx->alloc_block[0].sst_id = FW_DWNL_ID; | |
64 | sst_drv_ctx->alloc_block[0].ops_block.condition = false; | |
65 | retval = sst_load_fw(fw_sst, NULL); | |
66 | if (retval) | |
67 | goto end_restore; | |
68 | ||
69 | retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[0]); | |
70 | if (retval) | |
d0f40c50 | 71 | pr_err("fw download failed %d\n" , retval); |
fffa1cca VK |
72 | end_restore: |
73 | release_firmware(fw_sst); | |
74 | sst_drv_ctx->alloc_block[0].sst_id = BLOCK_UNINIT; | |
75 | return retval; | |
76 | } | |
77 | ||
78 | ||
79 | /* | |
80 | * sst_stalled - this function checks if the lpe is in stalled state | |
81 | */ | |
82 | int sst_stalled(void) | |
83 | { | |
84 | int retry = 1000; | |
85 | int retval = -1; | |
86 | ||
87 | while (retry) { | |
88 | if (!sst_drv_ctx->lpe_stalled) | |
89 | return 0; | |
90 | /*wait for time and re-check*/ | |
91 | msleep(1); | |
92 | ||
93 | retry--; | |
94 | } | |
d0f40c50 | 95 | pr_debug("in Stalled State\n"); |
fffa1cca VK |
96 | return retval; |
97 | } | |
98 | ||
99 | void free_stream_context(unsigned int str_id) | |
100 | { | |
101 | struct stream_info *stream; | |
102 | ||
103 | if (!sst_validate_strid(str_id)) { | |
104 | /* str_id is valid, so stream is alloacted */ | |
105 | stream = &sst_drv_ctx->streams[str_id]; | |
106 | if (stream->ops == STREAM_OPS_PLAYBACK || | |
107 | stream->ops == STREAM_OPS_PLAYBACK_DRM) { | |
108 | sst_drv_ctx->pb_streams--; | |
109 | if (sst_drv_ctx->pb_streams == 0) | |
110 | sst_drv_ctx->scard_ops->power_down_pmic_pb(); | |
111 | } else if (stream->ops == STREAM_OPS_CAPTURE) { | |
112 | sst_drv_ctx->cp_streams--; | |
113 | if (sst_drv_ctx->cp_streams == 0) | |
114 | sst_drv_ctx->scard_ops->power_down_pmic_cp(); | |
115 | } | |
116 | if (sst_drv_ctx->pb_streams == 0 | |
117 | && sst_drv_ctx->cp_streams == 0) | |
118 | sst_drv_ctx->scard_ops->power_down_pmic(); | |
119 | if (sst_free_stream(str_id)) | |
120 | sst_clean_stream(&sst_drv_ctx->streams[str_id]); | |
121 | } | |
122 | } | |
123 | ||
124 | /* | |
125 | * sst_get_stream_allocated - this function gets a stream allocated with | |
126 | * the given params | |
127 | * | |
128 | * @str_param : stream params | |
129 | * @lib_dnld : pointer to pointer of lib downlaod struct | |
130 | * | |
131 | * This creates new stream id for a stream, in case lib is to be downloaded to | |
132 | * DSP, it downloads that | |
133 | */ | |
134 | int sst_get_stream_allocated(struct snd_sst_params *str_param, | |
135 | struct snd_sst_lib_download **lib_dnld) | |
136 | { | |
137 | int retval, str_id; | |
138 | struct stream_info *str_info; | |
139 | ||
140 | retval = sst_alloc_stream((char *) &str_param->sparams, str_param->ops, | |
141 | str_param->codec, str_param->device_type); | |
142 | if (retval < 0) { | |
d0f40c50 | 143 | pr_err("sst_alloc_stream failed %d\n", retval); |
fffa1cca VK |
144 | return retval; |
145 | } | |
d0f40c50 | 146 | pr_debug("Stream allocated %d\n", retval); |
fffa1cca VK |
147 | str_id = retval; |
148 | str_info = &sst_drv_ctx->streams[str_id]; | |
149 | /* Block the call for reply */ | |
150 | retval = sst_wait_interruptible_timeout(sst_drv_ctx, | |
151 | &str_info->ctrl_blk, SST_BLOCK_TIMEOUT); | |
152 | if ((retval != 0) || (str_info->ctrl_blk.ret_code != 0)) { | |
d0f40c50 | 153 | pr_debug("FW alloc failed retval %d, ret_code %d\n", |
fffa1cca VK |
154 | retval, str_info->ctrl_blk.ret_code); |
155 | str_id = -str_info->ctrl_blk.ret_code; /*return error*/ | |
156 | *lib_dnld = str_info->ctrl_blk.data; | |
157 | sst_clean_stream(str_info); | |
158 | } else | |
d0f40c50 | 159 | pr_debug("FW Stream allocated success\n"); |
fffa1cca VK |
160 | return str_id; /*will ret either error (in above if) or correct str id*/ |
161 | } | |
162 | ||
163 | /* | |
164 | * sst_get_sfreq - this function returns the frequency of the stream | |
165 | * | |
166 | * @str_param : stream params | |
167 | */ | |
168 | static int sst_get_sfreq(struct snd_sst_params *str_param) | |
169 | { | |
170 | switch (str_param->codec) { | |
171 | case SST_CODEC_TYPE_PCM: | |
172 | return 48000; /*str_param->sparams.uc.pcm_params.sfreq;*/ | |
173 | case SST_CODEC_TYPE_MP3: | |
174 | return str_param->sparams.uc.mp3_params.sfreq; | |
175 | case SST_CODEC_TYPE_AAC: | |
176 | return str_param->sparams.uc.aac_params.sfreq;; | |
177 | case SST_CODEC_TYPE_WMA9: | |
178 | return str_param->sparams.uc.wma_params.sfreq;; | |
179 | default: | |
180 | return 0; | |
181 | } | |
182 | } | |
183 | ||
184 | /* | |
185 | * sst_get_stream - this function prepares for stream allocation | |
186 | * | |
187 | * @str_param : stream param | |
188 | */ | |
189 | int sst_get_stream(struct snd_sst_params *str_param) | |
190 | { | |
191 | int i, retval; | |
192 | struct stream_info *str_info; | |
193 | struct snd_sst_lib_download *lib_dnld; | |
194 | ||
195 | /* stream is not allocated, we are allocating */ | |
196 | retval = sst_get_stream_allocated(str_param, &lib_dnld); | |
197 | if (retval == -(SST_LIB_ERR_LIB_DNLD_REQUIRED)) { | |
198 | /* codec download is required */ | |
199 | struct snd_sst_alloc_response *response; | |
200 | ||
d0f40c50 | 201 | pr_debug("Codec is required.... trying that\n"); |
fffa1cca | 202 | if (lib_dnld == NULL) { |
d0f40c50 | 203 | pr_err("lib download null!!! abort\n"); |
fffa1cca VK |
204 | return -EIO; |
205 | } | |
206 | i = sst_get_block_stream(sst_drv_ctx); | |
207 | response = sst_drv_ctx->alloc_block[i].ops_block.data; | |
d0f40c50 | 208 | pr_debug("alloc block allocated = %d\n", i); |
fffa1cca VK |
209 | if (i < 0) { |
210 | kfree(lib_dnld); | |
211 | return -ENOMEM; | |
212 | } | |
213 | retval = sst_load_library(lib_dnld, str_param->ops); | |
214 | kfree(lib_dnld); | |
215 | ||
216 | sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT; | |
217 | if (!retval) { | |
d0f40c50 | 218 | pr_debug("codec was downloaded successfully\n"); |
fffa1cca VK |
219 | |
220 | retval = sst_get_stream_allocated(str_param, &lib_dnld); | |
221 | if (retval <= 0) | |
222 | goto err; | |
223 | ||
d0f40c50 | 224 | pr_debug("Alloc done stream id %d\n", retval); |
fffa1cca | 225 | } else { |
d0f40c50 | 226 | pr_debug("codec download failed\n"); |
fffa1cca VK |
227 | retval = -EIO; |
228 | goto err; | |
229 | } | |
230 | } else if (retval <= 0) | |
231 | goto err; | |
232 | /*else | |
233 | set_port_params(str_param, str_param->ops);*/ | |
234 | ||
235 | /* store sampling freq */ | |
236 | str_info = &sst_drv_ctx->streams[retval]; | |
237 | str_info->sfreq = sst_get_sfreq(str_param); | |
238 | ||
239 | /* power on the analog, if reqd */ | |
240 | if (str_param->ops == STREAM_OPS_PLAYBACK || | |
241 | str_param->ops == STREAM_OPS_PLAYBACK_DRM) { | |
242 | if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) | |
243 | sst_drv_ctx->scard_ops->power_up_pmic_pb( | |
244 | sst_drv_ctx->pmic_port_instance); | |
245 | else | |
246 | sst_drv_ctx->scard_ops->power_up_pmic_pb( | |
247 | str_info->device); | |
248 | /*Only if the playback is MP3 - Send a message*/ | |
249 | sst_drv_ctx->pb_streams++; | |
250 | } else if (str_param->ops == STREAM_OPS_CAPTURE) { | |
251 | ||
252 | sst_drv_ctx->scard_ops->power_up_pmic_cp( | |
253 | sst_drv_ctx->pmic_port_instance); | |
254 | /*Send a messageif not sent already*/ | |
255 | sst_drv_ctx->cp_streams++; | |
256 | } | |
257 | ||
258 | err: | |
259 | return retval; | |
260 | } | |
261 | ||
262 | void sst_process_mad_ops(struct work_struct *work) | |
263 | { | |
264 | ||
265 | struct mad_ops_wq *mad_ops = | |
266 | container_of(work, struct mad_ops_wq, wq); | |
267 | int retval = 0; | |
268 | ||
269 | switch (mad_ops->control_op) { | |
270 | case SST_SND_PAUSE: | |
271 | retval = sst_pause_stream(mad_ops->stream_id); | |
272 | break; | |
273 | case SST_SND_RESUME: | |
274 | retval = sst_resume_stream(mad_ops->stream_id); | |
275 | break; | |
276 | case SST_SND_DROP: | |
277 | /* retval = sst_drop_stream(mad_ops->stream_id); | |
278 | */ break; | |
279 | case SST_SND_START: | |
280 | pr_debug("SST Debug: start stream\n"); | |
281 | retval = sst_start_stream(mad_ops->stream_id); | |
282 | break; | |
283 | case SST_SND_STREAM_PROCESS: | |
d0f40c50 | 284 | pr_debug("play/capt frames...\n"); |
fffa1cca VK |
285 | break; |
286 | default: | |
d0f40c50 | 287 | pr_err(" wrong control_ops reported\n"); |
fffa1cca VK |
288 | } |
289 | return; | |
290 | } | |
291 | /* | |
292 | * sst_control_set - Set Control params | |
293 | * | |
294 | * @control_list: list of controls to be set | |
295 | * | |
296 | * This function is called by MID sound card driver to set | |
297 | * SST/Sound card controls. This is registered with MID driver | |
298 | */ | |
299 | int sst_control_set(int control_element, void *value) | |
300 | { | |
301 | int retval = 0, str_id = 0; | |
302 | struct stream_info *stream; | |
303 | ||
304 | if (sst_drv_ctx->sst_state == SST_SUSPENDED) { | |
305 | /*LPE is suspended, resume it before proceding*/ | |
d0f40c50 | 306 | pr_debug("Resuming from Suspended state\n"); |
fffa1cca VK |
307 | retval = intel_sst_resume(sst_drv_ctx->pci); |
308 | if (retval) { | |
d0f40c50 | 309 | pr_err("Resume Failed = %#x, abort\n", retval); |
fffa1cca VK |
310 | return retval; |
311 | } | |
312 | } | |
313 | if (sst_drv_ctx->sst_state == SST_UN_INIT) { | |
314 | /* FW is not downloaded */ | |
d0f40c50 | 315 | pr_debug("DSP Downloading FW now...\n"); |
fffa1cca VK |
316 | retval = sst_download_fw(); |
317 | if (retval) { | |
d0f40c50 | 318 | pr_err("FW download fail %x, abort\n", retval); |
fffa1cca VK |
319 | return retval; |
320 | } | |
321 | if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID && | |
322 | sst_drv_ctx->rx_time_slot_status != RX_TIMESLOT_UNINIT | |
323 | && sst_drv_ctx->pmic_vendor != SND_NC) | |
324 | sst_enable_rx_timeslot( | |
325 | sst_drv_ctx->rx_time_slot_status); | |
326 | } | |
327 | ||
328 | switch (control_element) { | |
329 | case SST_SND_ALLOC: { | |
330 | struct snd_sst_params *str_param; | |
331 | struct stream_info *str_info; | |
332 | ||
333 | str_param = (struct snd_sst_params *)value; | |
334 | BUG_ON(!str_param); | |
335 | retval = sst_get_stream(str_param); | |
336 | if (retval >= 0) | |
337 | sst_drv_ctx->stream_cnt++; | |
338 | str_info = &sst_drv_ctx->streams[retval]; | |
339 | str_info->src = MAD_DRV; | |
340 | break; | |
341 | } | |
342 | ||
343 | case SST_SND_PAUSE: | |
344 | case SST_SND_RESUME: | |
345 | case SST_SND_DROP: | |
346 | case SST_SND_START: | |
347 | sst_drv_ctx->mad_ops.control_op = control_element; | |
348 | sst_drv_ctx->mad_ops.stream_id = *(int *)value; | |
349 | queue_work(sst_drv_ctx->mad_wq, &sst_drv_ctx->mad_ops.wq); | |
350 | break; | |
351 | ||
352 | case SST_SND_FREE: | |
353 | str_id = *(int *)value; | |
354 | stream = &sst_drv_ctx->streams[str_id]; | |
355 | free_stream_context(str_id); | |
356 | stream->pcm_substream = NULL; | |
357 | stream->status = STREAM_UN_INIT; | |
358 | stream->period_elapsed = NULL; | |
359 | sst_drv_ctx->stream_cnt--; | |
360 | break; | |
361 | ||
362 | case SST_SND_STREAM_INIT: { | |
363 | struct pcm_stream_info *str_info; | |
364 | struct stream_info *stream; | |
365 | ||
d0f40c50 | 366 | pr_debug("stream init called\n"); |
fffa1cca VK |
367 | str_info = (struct pcm_stream_info *)value; |
368 | str_id = str_info->str_id; | |
369 | retval = sst_validate_strid(str_id); | |
370 | if (retval) | |
371 | break; | |
372 | ||
373 | stream = &sst_drv_ctx->streams[str_id]; | |
d0f40c50 | 374 | pr_debug("setting the period ptrs\n"); |
fffa1cca VK |
375 | stream->pcm_substream = str_info->mad_substream; |
376 | stream->period_elapsed = str_info->period_elapsed; | |
377 | stream->sfreq = str_info->sfreq; | |
378 | stream->prev = stream->status; | |
379 | stream->status = STREAM_INIT; | |
380 | break; | |
381 | } | |
382 | ||
383 | case SST_SND_BUFFER_POINTER: { | |
384 | struct pcm_stream_info *stream_info; | |
385 | struct snd_sst_tstamp fw_tstamp = {0,}; | |
386 | struct stream_info *stream; | |
387 | ||
388 | ||
389 | stream_info = (struct pcm_stream_info *)value; | |
390 | str_id = stream_info->str_id; | |
391 | retval = sst_validate_strid(str_id); | |
392 | if (retval) | |
393 | break; | |
394 | stream = &sst_drv_ctx->streams[str_id]; | |
395 | ||
396 | if (!stream->pcm_substream) | |
397 | break; | |
398 | memcpy_fromio(&fw_tstamp, | |
399 | ((void *)(sst_drv_ctx->mailbox + SST_TIME_STAMP) | |
400 | +(str_id * sizeof(fw_tstamp))), | |
401 | sizeof(fw_tstamp)); | |
402 | ||
d0f40c50 | 403 | pr_debug("Pointer Query on strid = %d ops %d\n", |
fffa1cca VK |
404 | str_id, stream->ops); |
405 | ||
406 | if (stream->ops == STREAM_OPS_PLAYBACK) | |
407 | stream_info->buffer_ptr = fw_tstamp.samples_rendered; | |
408 | else | |
409 | stream_info->buffer_ptr = fw_tstamp.samples_processed; | |
d0f40c50 | 410 | pr_debug("Samples rendered = %llu, buffer ptr %llu\n", |
fffa1cca VK |
411 | fw_tstamp.samples_rendered, stream_info->buffer_ptr); |
412 | break; | |
413 | } | |
414 | case SST_ENABLE_RX_TIME_SLOT: { | |
415 | int status = *(int *)value; | |
416 | sst_drv_ctx->rx_time_slot_status = status ; | |
417 | sst_enable_rx_timeslot(status); | |
418 | break; | |
419 | } | |
420 | default: | |
421 | /* Illegal case */ | |
d0f40c50 | 422 | pr_warn("illegal req\n"); |
fffa1cca VK |
423 | return -EINVAL; |
424 | } | |
425 | ||
426 | return retval; | |
427 | } | |
428 | ||
429 | ||
430 | struct intel_sst_card_ops sst_pmic_ops = { | |
431 | .control_set = sst_control_set, | |
432 | }; | |
433 | ||
434 | /* | |
435 | * register_sst_card - function for sound card to register | |
436 | * | |
437 | * @card: pointer to structure of operations | |
438 | * | |
439 | * This function is called card driver loads and is ready for registration | |
440 | */ | |
441 | int register_sst_card(struct intel_sst_card_ops *card) | |
442 | { | |
443 | if (!sst_drv_ctx) { | |
d0f40c50 | 444 | pr_err("No SST driver register card reject\n"); |
fffa1cca VK |
445 | return -ENODEV; |
446 | } | |
447 | ||
448 | if (!card || !card->module_name) { | |
d0f40c50 | 449 | pr_err("Null Pointer Passed\n"); |
fffa1cca VK |
450 | return -EINVAL; |
451 | } | |
452 | if (sst_drv_ctx->pmic_state == SND_MAD_UN_INIT) { | |
453 | /* register this driver */ | |
454 | if ((strncmp(SST_CARD_NAMES, card->module_name, | |
455 | strlen(SST_CARD_NAMES))) == 0) { | |
456 | sst_drv_ctx->pmic_vendor = card->vendor_id; | |
457 | sst_drv_ctx->scard_ops = card->scard_ops; | |
458 | sst_pmic_ops.module_name = card->module_name; | |
459 | sst_drv_ctx->pmic_state = SND_MAD_INIT_DONE; | |
460 | sst_drv_ctx->rx_time_slot_status = 0; /*default AMIC*/ | |
461 | card->control_set = sst_pmic_ops.control_set; | |
462 | sst_drv_ctx->scard_ops->card_status = SND_CARD_UN_INIT; | |
463 | return 0; | |
464 | } else { | |
d0f40c50 | 465 | pr_err("strcmp fail %s\n", card->module_name); |
fffa1cca VK |
466 | return -EINVAL; |
467 | } | |
468 | ||
469 | } else { | |
470 | /* already registered a driver */ | |
d0f40c50 | 471 | pr_err("Repeat for registration..denied\n"); |
fffa1cca VK |
472 | return -EBADRQC; |
473 | } | |
474 | return 0; | |
475 | } | |
476 | EXPORT_SYMBOL_GPL(register_sst_card); | |
477 | ||
478 | /* | |
479 | * unregister_sst_card- function for sound card to un-register | |
480 | * | |
481 | * @card: pointer to structure of operations | |
482 | * | |
483 | * This function is called when card driver unloads | |
484 | */ | |
485 | void unregister_sst_card(struct intel_sst_card_ops *card) | |
486 | { | |
487 | if (sst_pmic_ops.control_set == card->control_set) { | |
488 | /* unreg */ | |
489 | sst_pmic_ops.module_name = ""; | |
490 | sst_drv_ctx->pmic_state = SND_MAD_UN_INIT; | |
d0f40c50 | 491 | pr_debug("Unregistered %s\n", card->module_name); |
fffa1cca VK |
492 | } |
493 | return; | |
494 | } | |
495 | EXPORT_SYMBOL_GPL(unregister_sst_card); |