]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/ke_counter.c
Merge branch 'drm-intel-fixes' of git://people.freedesktop.org/~danvet/drm-intel...
[karo-tx-linux.git] / drivers / staging / comedi / drivers / ke_counter.c
1 /*
2     comedi/drivers/ke_counter.c
3     Comedi driver for Kolter-Electronic PCI Counter 1 Card
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: ke_counter
25 Description: Driver for Kolter Electronic Counter Card
26 Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
27 Author: Michael Hillmann
28 Updated: Mon, 14 Apr 2008 15:42:42 +0100
29 Status: tested
30
31 Configuration Options:
32   [0] - PCI bus of device (optional)
33   [1] - PCI slot of device (optional)
34   If bus/slot is not specified, the first supported
35   PCI device found will be used.
36
37 This driver is a simple driver to read the counter values from
38 Kolter Electronic PCI Counter Card.
39 */
40
41 #include "../comedidev.h"
42
43 #define CNT_DRIVER_NAME         "ke_counter"
44 #define PCI_VENDOR_ID_KOLTER    0x1001
45 #define CNT_CARD_DEVICE_ID      0x0014
46
47 /*-- board specification structure ------------------------------------------*/
48
49 struct cnt_board_struct {
50
51         const char *name;
52         int device_id;
53         int cnt_channel_nbr;
54         int cnt_bits;
55 };
56
57 static const struct cnt_board_struct cnt_boards[] = {
58         {
59          .name = CNT_DRIVER_NAME,
60          .device_id = CNT_CARD_DEVICE_ID,
61          .cnt_channel_nbr = 3,
62          .cnt_bits = 24}
63 };
64
65 /*-- counter write ----------------------------------------------------------*/
66
67 /* This should be used only for resetting the counters; maybe it is better
68    to make a special command 'reset'. */
69 static int cnt_winsn(struct comedi_device *dev,
70                      struct comedi_subdevice *s, struct comedi_insn *insn,
71                      unsigned int *data)
72 {
73         int chan = CR_CHAN(insn->chanspec);
74
75         outb((unsigned char)((data[0] >> 24) & 0xff),
76              dev->iobase + chan * 0x20 + 0x10);
77         outb((unsigned char)((data[0] >> 16) & 0xff),
78              dev->iobase + chan * 0x20 + 0x0c);
79         outb((unsigned char)((data[0] >> 8) & 0xff),
80              dev->iobase + chan * 0x20 + 0x08);
81         outb((unsigned char)((data[0] >> 0) & 0xff),
82              dev->iobase + chan * 0x20 + 0x04);
83
84         /* return the number of samples written */
85         return 1;
86 }
87
88 /*-- counter read -----------------------------------------------------------*/
89
90 static int cnt_rinsn(struct comedi_device *dev,
91                      struct comedi_subdevice *s, struct comedi_insn *insn,
92                      unsigned int *data)
93 {
94         unsigned char a0, a1, a2, a3, a4;
95         int chan = CR_CHAN(insn->chanspec);
96         int result;
97
98         a0 = inb(dev->iobase + chan * 0x20);
99         a1 = inb(dev->iobase + chan * 0x20 + 0x04);
100         a2 = inb(dev->iobase + chan * 0x20 + 0x08);
101         a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
102         a4 = inb(dev->iobase + chan * 0x20 + 0x10);
103
104         result = (a1 + (a2 * 256) + (a3 * 65536));
105         if (a4 > 0)
106                 result = result - s->maxdata;
107
108         *data = (unsigned int)result;
109
110         /* return the number of samples read */
111         return 1;
112 }
113
114 static struct pci_dev *cnt_find_pci_dev(struct comedi_device *dev,
115                                         struct comedi_devconfig *it)
116 {
117         const struct cnt_board_struct *board;
118         struct pci_dev *pcidev = NULL;
119         int bus = it->options[0];
120         int slot = it->options[1];
121         int i;
122
123         /* Probe the device to determine what device in the series it is. */
124         for_each_pci_dev(pcidev) {
125                 if (bus || slot) {
126                         if (pcidev->bus->number != bus ||
127                             PCI_SLOT(pcidev->devfn) != slot)
128                                 continue;
129                 }
130                 if (pcidev->vendor != PCI_VENDOR_ID_KOLTER)
131                         continue;
132
133                 for (i = 0; i < ARRAY_SIZE(cnt_boards); i++) {
134                         board = &cnt_boards[i];
135                         if (board->device_id != pcidev->device)
136                                 continue;
137
138                         dev->board_ptr = board;
139                         return pcidev;
140                 }
141         }
142         dev_err(dev->class_dev,
143                 "No supported board found! (req. bus %d, slot %d)\n",
144                 bus, slot);
145         return NULL;
146 }
147
148 static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it)
149 {
150         const struct cnt_board_struct *board;
151         struct pci_dev *pcidev;
152         struct comedi_subdevice *subdevice;
153         unsigned long io_base;
154         int error;
155
156         pcidev = cnt_find_pci_dev(dev, it);
157         if (!pcidev)
158                 return -EIO;
159         comedi_set_hw_dev(dev, &pcidev->dev);
160         board = comedi_board(dev);
161
162         dev->board_name = board->name;
163
164         /* enable PCI device and request regions */
165         error = comedi_pci_enable(pcidev, CNT_DRIVER_NAME);
166         if (error < 0) {
167                 printk(KERN_WARNING "comedi%d: "
168                        "failed to enable PCI device and request regions!\n",
169                        dev->minor);
170                 return error;
171         }
172
173         /* read register base address [PCI_BASE_ADDRESS #0] */
174         io_base = pci_resource_start(pcidev, 0);
175         dev->iobase = io_base;
176
177         error = comedi_alloc_subdevices(dev, 1);
178         if (error)
179                 return error;
180
181         subdevice = dev->subdevices + 0;
182         dev->read_subdev = subdevice;
183
184         subdevice->type = COMEDI_SUBD_COUNTER;
185         subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
186         subdevice->n_chan = board->cnt_channel_nbr;
187         subdevice->maxdata = (1 << board->cnt_bits) - 1;
188         subdevice->insn_read = cnt_rinsn;
189         subdevice->insn_write = cnt_winsn;
190
191         /*  select 20MHz clock */
192         outb(3, dev->iobase + 248);
193
194         /*  reset all counters */
195         outb(0, dev->iobase);
196         outb(0, dev->iobase + 0x20);
197         outb(0, dev->iobase + 0x40);
198
199         printk(KERN_INFO "comedi%d: " CNT_DRIVER_NAME " attached.\n",
200                dev->minor);
201         return 0;
202 }
203
204 static void cnt_detach(struct comedi_device *dev)
205 {
206         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
207
208         if (pcidev) {
209                 if (dev->iobase)
210                         comedi_pci_disable(pcidev);
211                 pci_dev_put(pcidev);
212         }
213 }
214
215 static struct comedi_driver ke_counter_driver = {
216         .driver_name    = "ke_counter",
217         .module         = THIS_MODULE,
218         .attach         = cnt_attach,
219         .detach         = cnt_detach,
220 };
221
222 static int __devinit ke_counter_pci_probe(struct pci_dev *dev,
223                                           const struct pci_device_id *ent)
224 {
225         return comedi_pci_auto_config(dev, &ke_counter_driver);
226 }
227
228 static void __devexit ke_counter_pci_remove(struct pci_dev *dev)
229 {
230         comedi_pci_auto_unconfig(dev);
231 }
232
233 static DEFINE_PCI_DEVICE_TABLE(ke_counter_pci_table) = {
234         { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
235         { 0 }
236 };
237 MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
238
239 static struct pci_driver ke_counter_pci_driver = {
240         .name           = "ke_counter",
241         .id_table       = ke_counter_pci_table,
242         .probe          = ke_counter_pci_probe,
243         .remove         = __devexit_p(ke_counter_pci_remove),
244 };
245 module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
246
247 MODULE_AUTHOR("Comedi http://www.comedi.org");
248 MODULE_DESCRIPTION("Comedi low-level driver");
249 MODULE_LICENSE("GPL");