Merge remote-tracking branch 'asoc/topic/max98095' into asoc-next
[cascardo/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 /*
19 Driver: ke_counter
20 Description: Driver for Kolter Electronic Counter Card
21 Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
22 Author: Michael Hillmann
23 Updated: Mon, 14 Apr 2008 15:42:42 +0100
24 Status: tested
25
26 Configuration Options: not applicable, uses PCI auto config
27
28 This driver is a simple driver to read the counter values from
29 Kolter Electronic PCI Counter Card.
30 */
31
32 #include <linux/module.h>
33 #include <linux/pci.h>
34
35 #include "../comedidev.h"
36
37 #define CNT_CARD_DEVICE_ID      0x0014
38
39 /*-- counter write ----------------------------------------------------------*/
40
41 /* This should be used only for resetting the counters; maybe it is better
42    to make a special command 'reset'. */
43 static int cnt_winsn(struct comedi_device *dev,
44                      struct comedi_subdevice *s, struct comedi_insn *insn,
45                      unsigned int *data)
46 {
47         int chan = CR_CHAN(insn->chanspec);
48
49         outb((unsigned char)((data[0] >> 24) & 0xff),
50              dev->iobase + chan * 0x20 + 0x10);
51         outb((unsigned char)((data[0] >> 16) & 0xff),
52              dev->iobase + chan * 0x20 + 0x0c);
53         outb((unsigned char)((data[0] >> 8) & 0xff),
54              dev->iobase + chan * 0x20 + 0x08);
55         outb((unsigned char)((data[0] >> 0) & 0xff),
56              dev->iobase + chan * 0x20 + 0x04);
57
58         /* return the number of samples written */
59         return 1;
60 }
61
62 /*-- counter read -----------------------------------------------------------*/
63
64 static int cnt_rinsn(struct comedi_device *dev,
65                      struct comedi_subdevice *s, struct comedi_insn *insn,
66                      unsigned int *data)
67 {
68         unsigned char a0, a1, a2, a3, a4;
69         int chan = CR_CHAN(insn->chanspec);
70         int result;
71
72         a0 = inb(dev->iobase + chan * 0x20);
73         a1 = inb(dev->iobase + chan * 0x20 + 0x04);
74         a2 = inb(dev->iobase + chan * 0x20 + 0x08);
75         a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
76         a4 = inb(dev->iobase + chan * 0x20 + 0x10);
77
78         result = (a1 + (a2 * 256) + (a3 * 65536));
79         if (a4 > 0)
80                 result = result - s->maxdata;
81
82         *data = (unsigned int)result;
83
84         /* return the number of samples read */
85         return 1;
86 }
87
88 static int cnt_auto_attach(struct comedi_device *dev,
89                                      unsigned long context_unused)
90 {
91         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
92         struct comedi_subdevice *s;
93         int ret;
94
95         ret = comedi_pci_enable(dev);
96         if (ret)
97                 return ret;
98         dev->iobase = pci_resource_start(pcidev, 0);
99
100         ret = comedi_alloc_subdevices(dev, 1);
101         if (ret)
102                 return ret;
103
104         s = &dev->subdevices[0];
105         dev->read_subdev = s;
106
107         s->type = COMEDI_SUBD_COUNTER;
108         s->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
109         s->n_chan = 3;
110         s->maxdata = 0x00ffffff;
111         s->insn_read = cnt_rinsn;
112         s->insn_write = cnt_winsn;
113
114         /*  select 20MHz clock */
115         outb(3, dev->iobase + 248);
116
117         /*  reset all counters */
118         outb(0, dev->iobase);
119         outb(0, dev->iobase + 0x20);
120         outb(0, dev->iobase + 0x40);
121
122         dev_info(dev->class_dev, "%s: %s attached\n",
123                 dev->driver->driver_name, dev->board_name);
124
125         return 0;
126 }
127
128 static struct comedi_driver ke_counter_driver = {
129         .driver_name    = "ke_counter",
130         .module         = THIS_MODULE,
131         .auto_attach    = cnt_auto_attach,
132         .detach         = comedi_pci_disable,
133 };
134
135 static int ke_counter_pci_probe(struct pci_dev *dev,
136                                 const struct pci_device_id *id)
137 {
138         return comedi_pci_auto_config(dev, &ke_counter_driver,
139                                       id->driver_data);
140 }
141
142 static DEFINE_PCI_DEVICE_TABLE(ke_counter_pci_table) = {
143         { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
144         { 0 }
145 };
146 MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
147
148 static struct pci_driver ke_counter_pci_driver = {
149         .name           = "ke_counter",
150         .id_table       = ke_counter_pci_table,
151         .probe          = ke_counter_pci_probe,
152         .remove         = comedi_pci_auto_unconfig,
153 };
154 module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
155
156 MODULE_AUTHOR("Comedi http://www.comedi.org");
157 MODULE_DESCRIPTION("Comedi low-level driver");
158 MODULE_LICENSE("GPL");