2 * intel_sst.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 * 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 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * This driver exposes the audio engine functionalities to the ALSA
29 * This file contains all init functions
32 #include <linux/pci.h>
34 #include <linux/interrupt.h>
35 #include <linux/firmware.h>
36 #include <linux/miscdevice.h>
38 #include "intel_sst.h"
39 #include "intel_sst_ioctl.h"
40 #include "intel_sst_fw_ipc.h"
41 #include "intel_sst_common.h"
44 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
45 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
46 MODULE_AUTHOR("Dharageswari R <dharageswari.r@intel.com>");
47 MODULE_AUTHOR("KP Jeeja <jeeja.kp@intel.com>");
48 MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
49 MODULE_LICENSE("GPL v2");
50 MODULE_VERSION(SST_DRIVER_VERSION);
52 struct intel_sst_drv *sst_drv_ctx;
53 static struct mutex drv_ctx_lock;
54 struct class *sst_class;
57 static const struct file_operations intel_sst_fops = {
59 .open = intel_sst_open,
60 .release = intel_sst_release,
61 .read = intel_sst_read,
62 .write = intel_sst_write,
63 .unlocked_ioctl = intel_sst_ioctl,
64 .mmap = intel_sst_mmap,
65 .aio_read = intel_sst_aio_read,
66 .aio_write = intel_sst_aio_write,
68 static const struct file_operations intel_sst_fops_cntrl = {
70 .open = intel_sst_open_cntrl,
71 .release = intel_sst_release_cntrl,
72 .unlocked_ioctl = intel_sst_ioctl,
75 static struct miscdevice lpe_dev = {
76 .minor = MISC_DYNAMIC_MINOR,/* dynamic allocation */
77 .name = "intel_sst",/* /dev/intel_sst */
78 .fops = &intel_sst_fops
82 static struct miscdevice lpe_ctrl = {
83 .minor = MISC_DYNAMIC_MINOR,/* dynamic allocation */
84 .name = "intel_sst_ctrl",/* /dev/intel_sst_ctrl */
85 .fops = &intel_sst_fops_cntrl
89 * intel_sst_interrupt - Interrupt service routine for SST
91 * @irq: irq number of interrupt
92 * @context: pointer to device structre
94 * This function is called by OS when SST device raises
95 * an interrupt. This will be result of write in IPC register
96 * Source can be busy or done interrupt
98 static irqreturn_t intel_sst_interrupt(int irq, void *context)
100 union interrupt_reg isr;
101 union ipc_header header;
102 union interrupt_reg imr;
103 struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
104 unsigned int size = 0, str_id;
105 struct stream_info *stream ;
107 /* Interrupt arrived, check src */
108 isr.full = sst_shim_read(drv->shim, SST_ISRX);
110 if (isr.part.busy_interrupt) {
111 header.full = sst_shim_read(drv->shim, SST_IPCD);
112 if (header.part.msg_id == IPC_SST_PERIOD_ELAPSED) {
113 sst_clear_interrupt();
114 str_id = header.part.str_id;
115 stream = &sst_drv_ctx->streams[str_id];
116 if (stream->period_elapsed)
117 stream->period_elapsed(stream->pcm_substream);
120 if (header.part.large)
121 size = header.part.data;
122 if (header.part.msg_id & REPLY_MSG) {
123 sst_drv_ctx->ipc_process_msg.header = header;
124 memcpy_fromio(sst_drv_ctx->ipc_process_msg.mailbox,
125 drv->mailbox + SST_MAILBOX_RCV, size);
126 queue_work(sst_drv_ctx->process_msg_wq,
127 &sst_drv_ctx->ipc_process_msg.wq);
129 sst_drv_ctx->ipc_process_reply.header = header;
130 memcpy_fromio(sst_drv_ctx->ipc_process_reply.mailbox,
131 drv->mailbox + SST_MAILBOX_RCV, size);
132 queue_work(sst_drv_ctx->process_reply_wq,
133 &sst_drv_ctx->ipc_process_reply.wq);
135 /* mask busy inetrrupt */
136 imr.full = sst_shim_read(drv->shim, SST_IMRX);
137 imr.part.busy_interrupt = 1;
138 sst_shim_write(sst_drv_ctx->shim, SST_IMRX, imr.full);
140 } else if (isr.part.done_interrupt) {
142 header.full = sst_shim_read(drv->shim, SST_IPCX);
143 header.part.done = 0;
144 sst_shim_write(sst_drv_ctx->shim, SST_IPCX, header.full);
145 /* write 1 to clear status register */;
146 isr.part.done_interrupt = 1;
147 /* dummy register for shim workaround */
148 sst_shim_write(sst_drv_ctx->shim, SST_ISRX, isr.full);
149 queue_work(sst_drv_ctx->post_msg_wq,
150 &sst_drv_ctx->ipc_post_msg.wq);
159 * intel_sst_probe - PCI probe function
161 * @pci: PCI device structure
162 * @pci_id: PCI device ID structure
164 * This function is called by OS when a device is found
165 * This enables the device, interrupt etc
167 static int __devinit intel_sst_probe(struct pci_dev *pci,
168 const struct pci_device_id *pci_id)
172 pr_debug("sst: Probe for DID %x\n", pci->device);
173 mutex_lock(&drv_ctx_lock);
175 pr_err("sst: Only one sst handle is supported\n");
176 mutex_unlock(&drv_ctx_lock);
180 sst_drv_ctx = kzalloc(sizeof(*sst_drv_ctx), GFP_KERNEL);
182 pr_err("sst: intel_sst malloc fail\n");
183 mutex_unlock(&drv_ctx_lock);
186 mutex_unlock(&drv_ctx_lock);
188 sst_drv_ctx->pci_id = pci->device;
190 mutex_init(&sst_drv_ctx->stream_lock);
191 mutex_init(&sst_drv_ctx->sst_lock);
192 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
194 sst_drv_ctx->stream_cnt = 0;
195 sst_drv_ctx->encoded_cnt = 0;
196 sst_drv_ctx->am_cnt = 0;
197 sst_drv_ctx->pb_streams = 0;
198 sst_drv_ctx->cp_streams = 0;
199 sst_drv_ctx->unique_id = 0;
200 sst_drv_ctx->pmic_port_instance = SST_DEFAULT_PMIC_PORT;
202 INIT_LIST_HEAD(&sst_drv_ctx->ipc_dispatch_list);
203 INIT_WORK(&sst_drv_ctx->ipc_post_msg.wq, sst_post_message);
204 INIT_WORK(&sst_drv_ctx->ipc_process_msg.wq, sst_process_message);
205 INIT_WORK(&sst_drv_ctx->ipc_process_reply.wq, sst_process_reply);
206 INIT_WORK(&sst_drv_ctx->mad_ops.wq, sst_process_mad_ops);
207 init_waitqueue_head(&sst_drv_ctx->wait_queue);
209 sst_drv_ctx->mad_wq = create_workqueue("sst_mad_wq");
210 if (!sst_drv_ctx->mad_wq)
211 goto do_free_drv_ctx;
212 sst_drv_ctx->post_msg_wq = create_workqueue("sst_post_msg_wq");
213 if (!sst_drv_ctx->post_msg_wq)
215 sst_drv_ctx->process_msg_wq = create_workqueue("sst_process_msg_wqq");
216 if (!sst_drv_ctx->process_msg_wq)
217 goto free_post_msg_wq;
218 sst_drv_ctx->process_reply_wq = create_workqueue("sst_proces_reply_wq");
219 if (!sst_drv_ctx->process_reply_wq)
220 goto free_process_msg_wq;
222 for (i = 0; i < MAX_ACTIVE_STREAM; i++) {
223 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
224 sst_drv_ctx->alloc_block[i].ops_block.condition = false;
226 spin_lock_init(&sst_drv_ctx->list_spin_lock);
228 sst_drv_ctx->max_streams = pci_id->driver_data;
229 pr_debug("sst: Got drv data max stream %d\n",
230 sst_drv_ctx->max_streams);
231 for (i = 1; i <= sst_drv_ctx->max_streams; i++) {
232 struct stream_info *stream = &sst_drv_ctx->streams[i];
233 INIT_LIST_HEAD(&stream->bufs);
234 mutex_init(&stream->lock);
235 spin_lock_init(&stream->pcm_lock);
237 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) {
238 sst_drv_ctx->mmap_mem = NULL;
239 sst_drv_ctx->mmap_len = SST_MMAP_PAGES * PAGE_SIZE;
240 while (sst_drv_ctx->mmap_len > 0) {
241 sst_drv_ctx->mmap_mem =
242 kzalloc(sst_drv_ctx->mmap_len, GFP_KERNEL);
243 if (sst_drv_ctx->mmap_mem) {
244 pr_debug("sst: Got memory %p size 0x%x\n",
245 sst_drv_ctx->mmap_mem,
246 sst_drv_ctx->mmap_len);
249 if (sst_drv_ctx->mmap_len < (SST_MMAP_STEP*PAGE_SIZE)) {
250 pr_err("sst: mem alloc fail...abort!!\n");
252 goto free_process_reply_wq;
254 sst_drv_ctx->mmap_len -= (SST_MMAP_STEP * PAGE_SIZE);
255 pr_debug("sst:mem alloc failed...trying %d\n",
256 sst_drv_ctx->mmap_len);
260 /* Init the device */
261 ret = pci_enable_device(pci);
263 pr_err("sst: device cant be enabled\n");
266 sst_drv_ctx->pci = pci_dev_get(pci);
267 ret = pci_request_regions(pci, SST_DRV_NAME);
269 goto do_disable_device;
272 sst_drv_ctx->shim_phy_add = pci_resource_start(pci, 1);
273 sst_drv_ctx->shim = pci_ioremap_bar(pci, 1);
274 if (!sst_drv_ctx->shim)
275 goto do_release_regions;
276 pr_debug("sst: SST Shim Ptr %p\n", sst_drv_ctx->shim);
279 sst_drv_ctx->mailbox = pci_ioremap_bar(pci, 2);
280 if (!sst_drv_ctx->mailbox)
282 pr_debug("sst: SRAM Ptr %p\n", sst_drv_ctx->mailbox);
285 sst_drv_ctx->iram = pci_ioremap_bar(pci, 3);
286 if (!sst_drv_ctx->iram)
288 pr_debug("sst:IRAM Ptr %p\n", sst_drv_ctx->iram);
291 sst_drv_ctx->dram = pci_ioremap_bar(pci, 4);
292 if (!sst_drv_ctx->dram)
294 pr_debug("sst: DRAM Ptr %p\n", sst_drv_ctx->dram);
296 mutex_lock(&sst_drv_ctx->sst_lock);
297 sst_drv_ctx->sst_state = SST_UN_INIT;
298 mutex_unlock(&sst_drv_ctx->sst_lock);
299 /* Register the ISR */
300 ret = request_irq(pci->irq, intel_sst_interrupt,
301 IRQF_SHARED, SST_DRV_NAME, sst_drv_ctx);
304 pr_debug("sst: Registered IRQ 0x%x\n", pci->irq);
306 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) {
307 ret = misc_register(&lpe_dev);
309 pr_err("sst: couldn't register LPE device\n");
313 /*Register LPE Control as misc driver*/
314 ret = misc_register(&lpe_ctrl);
316 pr_err("sst: couldn't register misc driver\n");
320 sst_drv_ctx->lpe_stalled = 0;
321 pr_debug("sst: ...successfully done!!!\n");
325 free_irq(pci->irq, sst_drv_ctx);
327 iounmap(sst_drv_ctx->dram);
329 iounmap(sst_drv_ctx->iram);
331 iounmap(sst_drv_ctx->mailbox);
333 iounmap(sst_drv_ctx->shim);
335 pci_release_regions(pci);
337 pci_disable_device(pci);
339 kfree(sst_drv_ctx->mmap_mem);
340 free_process_reply_wq:
341 destroy_workqueue(sst_drv_ctx->process_reply_wq);
343 destroy_workqueue(sst_drv_ctx->process_msg_wq);
345 destroy_workqueue(sst_drv_ctx->post_msg_wq);
347 destroy_workqueue(sst_drv_ctx->mad_wq);
350 pr_err("sst: Probe failed with 0x%x\n", ret);
355 * intel_sst_remove - PCI remove function
357 * @pci: PCI device structure
359 * This function is called by OS when a device is unloaded
360 * This frees the interrupt etc
362 static void __devexit intel_sst_remove(struct pci_dev *pci)
364 pci_dev_put(sst_drv_ctx->pci);
365 mutex_lock(&sst_drv_ctx->sst_lock);
366 sst_drv_ctx->sst_state = SST_UN_INIT;
367 mutex_unlock(&sst_drv_ctx->sst_lock);
368 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) {
369 misc_deregister(&lpe_dev);
370 misc_deregister(&lpe_ctrl);
372 free_irq(pci->irq, sst_drv_ctx);
373 iounmap(sst_drv_ctx->dram);
374 iounmap(sst_drv_ctx->iram);
375 iounmap(sst_drv_ctx->mailbox);
376 iounmap(sst_drv_ctx->shim);
377 sst_drv_ctx->pmic_state = SND_MAD_UN_INIT;
378 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
379 kfree(sst_drv_ctx->mmap_mem);
380 flush_scheduled_work();
381 destroy_workqueue(sst_drv_ctx->process_reply_wq);
382 destroy_workqueue(sst_drv_ctx->process_msg_wq);
383 destroy_workqueue(sst_drv_ctx->post_msg_wq);
384 destroy_workqueue(sst_drv_ctx->mad_wq);
386 pci_release_region(pci, 1);
387 pci_release_region(pci, 2);
388 pci_release_region(pci, 3);
389 pci_release_region(pci, 4);
390 pci_release_region(pci, 5);
391 pci_set_drvdata(pci, NULL);
394 /* Power Management */
396 * intel_sst_suspend - PCI suspend function
398 * @pci: PCI device structure
401 * This function is called by OS when a power event occurs
403 int intel_sst_suspend(struct pci_dev *pci, pm_message_t state)
405 union config_status_reg csr;
407 pr_debug("sst: intel_sst_suspend called\n");
409 if (sst_drv_ctx->pb_streams != 0 || sst_drv_ctx->cp_streams != 0)
411 /*Assert RESET on LPE Processor*/
412 csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
413 csr.full = csr.full | 0x2;
414 /* Move the SST state to Suspended */
415 mutex_lock(&sst_drv_ctx->sst_lock);
416 sst_drv_ctx->sst_state = SST_SUSPENDED;
417 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
418 mutex_unlock(&sst_drv_ctx->sst_lock);
419 pci_set_drvdata(pci, sst_drv_ctx);
421 pci_disable_device(pci);
422 pci_set_power_state(pci, PCI_D3hot);
427 * intel_sst_resume - PCI resume function
429 * @pci: PCI device structure
431 * This function is called by OS when a power event occurs
433 int intel_sst_resume(struct pci_dev *pci)
437 pr_debug("sst: intel_sst_resume called\n");
438 if (sst_drv_ctx->sst_state != SST_SUSPENDED) {
439 pr_err("sst: SST is not in suspended state\n");
442 sst_drv_ctx = pci_get_drvdata(pci);
443 pci_set_power_state(pci, PCI_D0);
444 pci_restore_state(pci);
445 ret = pci_enable_device(pci);
447 pr_err("sst: device cant be enabled\n");
449 mutex_lock(&sst_drv_ctx->sst_lock);
450 sst_drv_ctx->sst_state = SST_UN_INIT;
451 mutex_unlock(&sst_drv_ctx->sst_lock);
456 static struct pci_device_id intel_sst_ids[] = {
457 { PCI_VDEVICE(INTEL, SST_MRST_PCI_ID), 3},
458 { PCI_VDEVICE(INTEL, SST_MFLD_PCI_ID), 6},
461 MODULE_DEVICE_TABLE(pci, intel_sst_ids);
463 static struct pci_driver driver = {
464 .name = SST_DRV_NAME,
465 .id_table = intel_sst_ids,
466 .probe = intel_sst_probe,
467 .remove = __devexit_p(intel_sst_remove),
469 .suspend = intel_sst_suspend,
470 .resume = intel_sst_resume,
475 * intel_sst_init - Module init function
478 * Registers with /dev
479 * Init all data strutures
481 static int __init intel_sst_init(void)
483 /* Init all variables, data structure etc....*/
485 pr_debug("sst: INFO: ******** SST DRIVER loading.. Ver: %s\n",
488 mutex_init(&drv_ctx_lock);
489 /* Register with PCI */
490 ret = pci_register_driver(&driver);
492 pr_err("sst: PCI register failed\n");
497 * intel_sst_exit - Module exit function
499 * Unregisters with PCI
500 * Unregisters with /dev
501 * Frees all data strutures
503 static void __exit intel_sst_exit(void)
505 pci_unregister_driver(&driver);
507 pr_debug("sst: driver unloaded\n");
511 module_init(intel_sst_init);
512 module_exit(intel_sst_exit);