Merge tag 'hsi-for-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[cascardo/linux.git] / drivers / staging / comedi / drivers / ni_labpc_pci.c
1 /*
2  * comedi/drivers/ni_labpc_pci.c
3  * Driver for National Instruments Lab-PC PCI-1200
4  * Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16
17 /*
18  * Driver: ni_labpc_pci
19  * Description: National Instruments Lab-PC PCI-1200
20  * Devices: (National Instruments) PCI-1200 [ni_pci-1200]
21  * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
22  * Status: works
23  *
24  * This is the PCI-specific support split off from the ni_labpc driver.
25  *
26  * Configuration Options: not applicable, uses PCI auto config
27  *
28  * NI manuals:
29  * 340914a (pci-1200)
30  */
31
32 #include <linux/module.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35
36 #include "../comedidev.h"
37
38 #include "ni_labpc.h"
39
40 enum labpc_pci_boardid {
41         BOARD_NI_PCI1200,
42 };
43
44 static const struct labpc_boardinfo labpc_pci_boards[] = {
45         [BOARD_NI_PCI1200] = {
46                 .name                   = "ni_pci-1200",
47                 .ai_speed               = 10000,
48                 .ai_scan_up             = 1,
49                 .has_ao                 = 1,
50                 .is_labpc1200           = 1,
51         },
52 };
53
54 /* ripped from mite.h and mite_setup2() to avoid mite dependancy */
55 #define MITE_IODWBSR    0xc0     /* IO Device Window Base Size Register */
56 #define WENAB           (1 << 7) /* window enable */
57
58 static int labpc_pci_mite_init(struct pci_dev *pcidev)
59 {
60         void __iomem *mite_base;
61         u32 main_phys_addr;
62
63         /* ioremap the MITE registers (BAR 0) temporarily */
64         mite_base = pci_ioremap_bar(pcidev, 0);
65         if (!mite_base)
66                 return -ENOMEM;
67
68         /* set data window to main registers (BAR 1) */
69         main_phys_addr = pci_resource_start(pcidev, 1);
70         writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
71
72         /* finished with MITE registers */
73         iounmap(mite_base);
74         return 0;
75 }
76
77 static int labpc_pci_auto_attach(struct comedi_device *dev,
78                                  unsigned long context)
79 {
80         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
81         const struct labpc_boardinfo *board = NULL;
82         struct labpc_private *devpriv;
83         int ret;
84
85         if (context < ARRAY_SIZE(labpc_pci_boards))
86                 board = &labpc_pci_boards[context];
87         if (!board)
88                 return -ENODEV;
89         dev->board_ptr = board;
90         dev->board_name = board->name;
91
92         ret = comedi_pci_enable(dev);
93         if (ret)
94                 return ret;
95
96         ret = labpc_pci_mite_init(pcidev);
97         if (ret)
98                 return ret;
99
100         dev->mmio = pci_ioremap_bar(pcidev, 1);
101         if (!dev->mmio)
102                 return -ENOMEM;
103
104         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
105         if (!devpriv)
106                 return -ENOMEM;
107
108         return labpc_common_attach(dev, pcidev->irq, IRQF_SHARED);
109 }
110
111 static void labpc_pci_detach(struct comedi_device *dev)
112 {
113         if (dev->mmio)
114                 iounmap(dev->mmio);
115         if (dev->irq)
116                 free_irq(dev->irq, dev);
117         comedi_pci_disable(dev);
118 }
119
120 static struct comedi_driver labpc_pci_comedi_driver = {
121         .driver_name    = "labpc_pci",
122         .module         = THIS_MODULE,
123         .auto_attach    = labpc_pci_auto_attach,
124         .detach         = labpc_pci_detach,
125 };
126
127 static const struct pci_device_id labpc_pci_table[] = {
128         { PCI_VDEVICE(NI, 0x161), BOARD_NI_PCI1200 },
129         { 0 }
130 };
131 MODULE_DEVICE_TABLE(pci, labpc_pci_table);
132
133 static int labpc_pci_probe(struct pci_dev *dev,
134                            const struct pci_device_id *id)
135 {
136         return comedi_pci_auto_config(dev, &labpc_pci_comedi_driver,
137                                       id->driver_data);
138 }
139
140 static struct pci_driver labpc_pci_driver = {
141         .name           = "labpc_pci",
142         .id_table       = labpc_pci_table,
143         .probe          = labpc_pci_probe,
144         .remove         = comedi_pci_auto_unconfig,
145 };
146 module_comedi_pci_driver(labpc_pci_comedi_driver, labpc_pci_driver);
147
148 MODULE_DESCRIPTION("Comedi: National Instruments Lab-PC PCI-1200 driver");
149 MODULE_AUTHOR("Comedi http://www.comedi.org");
150 MODULE_LICENSE("GPL");