Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / intel_sst / intel_sst.c
1 /*
2  *  intel_sst.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  *              KP Jeeja <jeeja.kp@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
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.
14  *
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.
19  *
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.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  *  This driver exposes the audio engine functionalities to the ALSA
27  *       and middleware.
28  *
29  *  This file contains all init functions
30  */
31
32 #include <linux/pci.h>
33 #include <linux/fs.h>
34 #include <linux/interrupt.h>
35 #include <linux/firmware.h>
36 #include <linux/miscdevice.h>
37 #include <asm/mrst.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"
42
43
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);
51
52 struct intel_sst_drv *sst_drv_ctx;
53 static struct mutex drv_ctx_lock;
54 struct class *sst_class;
55
56 /* fops Routines */
57 static const struct file_operations intel_sst_fops = {
58         .owner = THIS_MODULE,
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,
67 };
68 static const struct file_operations intel_sst_fops_cntrl = {
69         .owner = THIS_MODULE,
70         .open = intel_sst_open_cntrl,
71         .release = intel_sst_release_cntrl,
72         .unlocked_ioctl = intel_sst_ioctl,
73 };
74
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
79 };
80
81
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
86 };
87
88 /**
89 * intel_sst_interrupt - Interrupt service routine for SST
90 *
91 * @irq: irq number of interrupt
92 * @context: pointer to device structre
93 *
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
97 */
98 static irqreturn_t intel_sst_interrupt(int irq, void *context)
99 {
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 ;
106
107         /* Interrupt arrived, check src */
108         isr.full = sst_shim_read(drv->shim, SST_ISRX);
109
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);
118                         return IRQ_HANDLED;
119                 }
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);
128                 } else {
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);
134                 }
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);
139                 return IRQ_HANDLED;
140         } else if (isr.part.done_interrupt) {
141                 /* Clear done bit */
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);
151                 return IRQ_HANDLED;
152         } else
153                 return IRQ_NONE;
154
155 }
156
157
158 /*
159 * intel_sst_probe - PCI probe function
160 *
161 * @pci: PCI device structure
162 * @pci_id: PCI device ID structure
163 *
164 * This function is called by OS when a device is found
165 * This enables the device, interrupt etc
166 */
167 static int __devinit intel_sst_probe(struct pci_dev *pci,
168                         const struct pci_device_id *pci_id)
169 {
170         int i, ret = 0;
171
172         pr_debug("sst: Probe for DID %x\n", pci->device);
173         mutex_lock(&drv_ctx_lock);
174         if (sst_drv_ctx) {
175                 pr_err("sst: Only one sst handle is supported\n");
176                 mutex_unlock(&drv_ctx_lock);
177                 return -EBUSY;
178         }
179
180         sst_drv_ctx = kzalloc(sizeof(*sst_drv_ctx), GFP_KERNEL);
181         if (!sst_drv_ctx) {
182                 pr_err("sst: intel_sst malloc fail\n");
183                 mutex_unlock(&drv_ctx_lock);
184                 return -ENOMEM;
185         }
186         mutex_unlock(&drv_ctx_lock);
187
188         sst_drv_ctx->pci_id = pci->device;
189
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;
193
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;
201
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);
208
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)
214                 goto free_mad_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;
221
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;
225         }
226         spin_lock_init(&sst_drv_ctx->list_spin_lock);
227
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);
236         }
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);
247                                 break;
248                         }
249                         if (sst_drv_ctx->mmap_len < (SST_MMAP_STEP*PAGE_SIZE)) {
250                                 pr_err("sst: mem alloc fail...abort!!\n");
251                                 ret = -ENOMEM;
252                                 goto free_process_reply_wq;
253                         }
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);
257                 }
258         }
259
260         /* Init the device */
261         ret = pci_enable_device(pci);
262         if (ret) {
263                 pr_err("sst: device cant be enabled\n");
264                 goto do_free_mem;
265         }
266         sst_drv_ctx->pci = pci_dev_get(pci);
267         ret = pci_request_regions(pci, SST_DRV_NAME);
268         if (ret)
269                 goto do_disable_device;
270         /* map registers */
271         /* SST Shim */
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);
277
278         /* Shared SRAM */
279         sst_drv_ctx->mailbox = pci_ioremap_bar(pci, 2);
280         if (!sst_drv_ctx->mailbox)
281                 goto do_unmap_shim;
282         pr_debug("sst: SRAM Ptr %p\n", sst_drv_ctx->mailbox);
283
284         /* IRAM */
285         sst_drv_ctx->iram = pci_ioremap_bar(pci, 3);
286         if (!sst_drv_ctx->iram)
287                 goto do_unmap_sram;
288         pr_debug("sst:IRAM Ptr %p\n", sst_drv_ctx->iram);
289
290         /* DRAM */
291         sst_drv_ctx->dram = pci_ioremap_bar(pci, 4);
292         if (!sst_drv_ctx->dram)
293                 goto do_unmap_iram;
294         pr_debug("sst: DRAM Ptr %p\n", sst_drv_ctx->dram);
295
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);
302         if (ret)
303                 goto do_unmap_dram;
304         pr_debug("sst: Registered IRQ 0x%x\n", pci->irq);
305
306         if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) {
307                 ret = misc_register(&lpe_dev);
308                 if (ret) {
309                         pr_err("sst: couldn't register LPE device\n");
310                         goto do_free_irq;
311                 }
312
313                 /*Register LPE Control as misc driver*/
314                 ret = misc_register(&lpe_ctrl);
315                 if (ret) {
316                         pr_err("sst: couldn't register misc driver\n");
317                         goto do_free_irq;
318                 }
319         }
320         sst_drv_ctx->lpe_stalled = 0;
321         pr_debug("sst: ...successfully done!!!\n");
322         return ret;
323
324 do_free_irq:
325         free_irq(pci->irq, sst_drv_ctx);
326 do_unmap_dram:
327         iounmap(sst_drv_ctx->dram);
328 do_unmap_iram:
329         iounmap(sst_drv_ctx->iram);
330 do_unmap_sram:
331         iounmap(sst_drv_ctx->mailbox);
332 do_unmap_shim:
333         iounmap(sst_drv_ctx->shim);
334 do_release_regions:
335         pci_release_regions(pci);
336 do_disable_device:
337         pci_disable_device(pci);
338 do_free_mem:
339         kfree(sst_drv_ctx->mmap_mem);
340 free_process_reply_wq:
341         destroy_workqueue(sst_drv_ctx->process_reply_wq);
342 free_process_msg_wq:
343         destroy_workqueue(sst_drv_ctx->process_msg_wq);
344 free_post_msg_wq:
345         destroy_workqueue(sst_drv_ctx->post_msg_wq);
346 free_mad_wq:
347         destroy_workqueue(sst_drv_ctx->mad_wq);
348 do_free_drv_ctx:
349         kfree(sst_drv_ctx);
350         pr_err("sst: Probe failed with 0x%x\n", ret);
351         return ret;
352 }
353
354 /**
355 * intel_sst_remove - PCI remove function
356 *
357 * @pci: PCI device structure
358 *
359 * This function is called by OS when a device is unloaded
360 * This frees the interrupt etc
361 */
362 static void __devexit intel_sst_remove(struct pci_dev *pci)
363 {
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);
371         }
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);
385         kfree(sst_drv_ctx);
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);
392 }
393
394 /* Power Management */
395 /*
396 * intel_sst_suspend - PCI suspend function
397 *
398 * @pci: PCI device structure
399 * @state: PM message
400 *
401 * This function is called by OS when a power event occurs
402 */
403 int intel_sst_suspend(struct pci_dev *pci, pm_message_t state)
404 {
405         union config_status_reg csr;
406
407         pr_debug("sst: intel_sst_suspend called\n");
408
409         if (sst_drv_ctx->pb_streams != 0 || sst_drv_ctx->cp_streams != 0)
410                 return -EPERM;
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);
420         pci_save_state(pci);
421         pci_disable_device(pci);
422         pci_set_power_state(pci, PCI_D3hot);
423         return 0;
424 }
425
426 /**
427 * intel_sst_resume - PCI resume function
428 *
429 * @pci: PCI device structure
430 *
431 * This function is called by OS when a power event occurs
432 */
433 int intel_sst_resume(struct pci_dev *pci)
434 {
435         int ret = 0;
436
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");
440                 return -EPERM;
441         }
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);
446         if (ret)
447                 pr_err("sst: device cant be enabled\n");
448
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);
452         return 0;
453 }
454
455 /* PCI Routines */
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},
459         { 0, }
460 };
461 MODULE_DEVICE_TABLE(pci, intel_sst_ids);
462
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),
468 #ifdef CONFIG_PM
469         .suspend = intel_sst_suspend,
470         .resume = intel_sst_resume,
471 #endif
472 };
473
474 /**
475 * intel_sst_init - Module init function
476 *
477 * Registers with PCI
478 * Registers with /dev
479 * Init all data strutures
480 */
481 static int __init intel_sst_init(void)
482 {
483         /* Init all variables, data structure etc....*/
484         int ret = 0;
485         pr_debug("sst: INFO: ******** SST DRIVER loading.. Ver: %s\n",
486                                        SST_DRIVER_VERSION);
487
488         mutex_init(&drv_ctx_lock);
489         /* Register with PCI */
490         ret = pci_register_driver(&driver);
491         if (ret)
492                 pr_err("sst: PCI register failed\n");
493         return ret;
494 }
495
496 /**
497 * intel_sst_exit - Module exit function
498 *
499 * Unregisters with PCI
500 * Unregisters with /dev
501 * Frees all data strutures
502 */
503 static void __exit intel_sst_exit(void)
504 {
505         pci_unregister_driver(&driver);
506
507         pr_debug("sst: driver unloaded\n");
508         return;
509 }
510
511 module_init(intel_sst_init);
512 module_exit(intel_sst_exit);