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