Merge 3.9-rc5 into staging-next
[deliverable/linux.git] / drivers / staging / comedi / drivers / cb_pcimdas.c
CommitLineData
f4bd8abb
RB
1/*
2 comedi/drivers/cb_pcimdas.c
3 Comedi driver for Computer Boards PCIM-DAS1602/16
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/*
24Driver: cb_pcimdas
25Description: Measurement Computing PCI Migration series boards
26Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas)
27Author: Richard Bytheway
28Updated: Wed, 13 Nov 2002 12:34:56 +0000
29Status: experimental
30
31Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
32
33Configuration Options:
34 [0] - PCI bus number
35 [1] - PCI slot number
36
37Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org).
38Only supports DIO, AO and simple AI in it's present form.
39No interrupts, multi channel or FIFO AI, although the card looks like it could support this.
631dd1a8 40See http://www.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details.
f4bd8abb
RB
41*/
42
33782dd5 43#include <linux/pci.h>
f4bd8abb 44#include <linux/delay.h>
25436dc9 45#include <linux/interrupt.h>
f4bd8abb 46
33782dd5
HS
47#include "../comedidev.h"
48
f4bd8abb
RB
49#include "plx9052.h"
50#include "8255.h"
51
6eef3af5 52/* #define CBPCIMDAS_DEBUG */
f4bd8abb
RB
53#undef CBPCIMDAS_DEBUG
54
55/* Registers for the PCIM-DAS1602/16 */
56
6eef3af5 57/* sizes of io regions (bytes) */
f4bd8abb 58#define BADR3_SIZE 16
f4bd8abb 59
6eef3af5 60/* DAC Offsets */
f4bd8abb
RB
61#define ADC_TRIG 0
62#define DAC0_OFFSET 2
63#define DAC1_OFFSET 4
64
6eef3af5 65/* AI and Counter Constants */
f4bd8abb
RB
66#define MUX_LIMITS 0
67#define MAIN_CONN_DIO 1
68#define ADC_STAT 2
69#define ADC_CONV_STAT 3
70#define ADC_INT 4
71#define ADC_PACER 5
72#define BURST_MODE 6
73#define PROG_GAIN 7
74#define CLK8254_1_DATA 8
75#define CLK8254_2_DATA 9
76#define CLK8254_3_DATA 10
77#define CLK8254_CONTROL 11
78#define USER_COUNTER 12
79#define RESID_COUNT_H 13
80#define RESID_COUNT_L 14
81
190908ba
RKM
82/*
83 * this structure is for data unique to this hardware driver. If
84 * several hardware drivers keep similar information in this structure,
85 * feel free to suggest moving the variable to the struct comedi_device
86 * struct.
87 */
b90c76fd 88struct cb_pcimdas_private {
6eef3af5 89 /* base addresses */
f4bd8abb 90 unsigned long BADR3;
f4bd8abb
RB
91
92 /* Used for AO readback */
790c5541 93 unsigned int ao_readback[2];
b90c76fd 94};
f4bd8abb 95
2682b2dc
HS
96/*
97 * "instructions" read/write data in "one-shot" or "software-triggered"
98 * mode.
99 */
0a85b6f0
MT
100static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
101 struct comedi_subdevice *s,
2682b2dc
HS
102 struct comedi_insn *insn, unsigned int *data)
103{
2682b2dc
HS
104 struct cb_pcimdas_private *devpriv = dev->private;
105 int n, i;
106 unsigned int d;
107 unsigned int busy;
108 int chan = CR_CHAN(insn->chanspec);
109 unsigned short chanlims;
110 int maxchans;
111
112 /* only support sw initiated reads from a single channel */
113
114 /* check channel number */
115 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
0f904604 116 maxchans = s->n_chan / 2;
2682b2dc 117 else
0f904604 118 maxchans = s->n_chan;
2682b2dc
HS
119
120 if (chan > (maxchans - 1))
121 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
122
123 /* configure for sw initiated read */
124 d = inb(devpriv->BADR3 + 5);
125 if ((d & 0x03) > 0) { /* only reset if needed. */
126 d = d & 0xfd;
127 outb(d, devpriv->BADR3 + 5);
128 }
129 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
130 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
131
132 /*
133 * write channel limits to multiplexer, set Low (bits 0-3) and
134 * High (bits 4-7) channels to chan.
135 */
136 chanlims = chan | (chan << 4);
137 outb(chanlims, devpriv->BADR3 + 0);
138
139 /* convert n samples */
140 for (n = 0; n < insn->n; n++) {
141 /* trigger conversion */
142 outw(0, dev->iobase + 0);
143
144#define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
145 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
146
147 /* wait for conversion to end */
148 for (i = 0; i < TIMEOUT; i++) {
149 busy = inb(devpriv->BADR3 + 2) & 0x80;
150 if (!busy)
151 break;
152 }
153 if (i == TIMEOUT) {
154 printk("timeout\n");
155 return -ETIMEDOUT;
156 }
157 /* read data */
0f904604 158 data[n] = inw(dev->iobase + 0);
2682b2dc
HS
159 }
160
161 /* return the number of samples read/written */
162 return n;
163}
164
0a85b6f0
MT
165static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
166 struct comedi_subdevice *s,
2682b2dc
HS
167 struct comedi_insn *insn, unsigned int *data)
168{
169 struct cb_pcimdas_private *devpriv = dev->private;
170 int i;
171 int chan = CR_CHAN(insn->chanspec);
172
173 /* Writing a list of values to an AO channel is probably not
174 * very useful, but that's how the interface is defined. */
175 for (i = 0; i < insn->n; i++) {
176 switch (chan) {
177 case 0:
178 outw(data[i] & 0x0FFF, dev->iobase + DAC0_OFFSET);
179 break;
180 case 1:
181 outw(data[i] & 0x0FFF, dev->iobase + DAC1_OFFSET);
182 break;
183 default:
184 return -1;
185 }
186 devpriv->ao_readback[chan] = data[i];
187 }
188
189 /* return the number of samples read/written */
190 return i;
191}
192
193/* AO subdevices should have a read insn as well as a write insn.
194 * Usually this means copying a value stored in devpriv. */
0a85b6f0
MT
195static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
196 struct comedi_subdevice *s,
2682b2dc
HS
197 struct comedi_insn *insn, unsigned int *data)
198{
199 struct cb_pcimdas_private *devpriv = dev->private;
200 int i;
201 int chan = CR_CHAN(insn->chanspec);
202
203 for (i = 0; i < insn->n; i++)
204 data[i] = devpriv->ao_readback[chan];
205
206 return i;
207}
f4bd8abb 208
a690b7e5 209static int cb_pcimdas_auto_attach(struct comedi_device *dev,
750af5e5 210 unsigned long context_unused)
46a4ff0d 211{
750af5e5 212 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
5f7cab0a 213 struct cb_pcimdas_private *devpriv;
46a4ff0d 214 struct comedi_subdevice *s;
8e2f2384 215 unsigned long iobase_8255;
46a4ff0d 216 int ret;
f4bd8abb 217
0f904604 218 dev->board_name = dev->driver->driver_name;
2d069fbe 219
c34fa261
HS
220 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
221 if (!devpriv)
222 return -ENOMEM;
223 dev->private = devpriv;
46a4ff0d 224
818f569f 225 ret = comedi_pci_enable(dev);
2d069fbe
HS
226 if (ret)
227 return ret;
f4bd8abb 228
16dd84f5
HS
229 dev->iobase = pci_resource_start(pcidev, 2);
230 devpriv->BADR3 = pci_resource_start(pcidev, 3);
231 iobase_8255 = pci_resource_start(pcidev, 4);
f4bd8abb 232
6eef3af5
BP
233/* Dont support IRQ yet */
234/* get irq */
16dd84f5 235/* if(request_irq(pcidev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
6eef3af5 236/* { */
16dd84f5 237/* printk(" unable to allocate irq %u\n", pcidev->irq); */
6eef3af5
BP
238/* return -EINVAL; */
239/* } */
16dd84f5 240/* dev->irq = pcidev->irq; */
f4bd8abb 241
8b6c5694
HS
242 ret = comedi_alloc_subdevices(dev, 3);
243 if (ret)
244 return ret;
f4bd8abb 245
13201d88 246 s = &dev->subdevices[0];
6eef3af5
BP
247 /* dev->read_subdev=s; */
248 /* analog input subdevice */
f4bd8abb
RB
249 s->type = COMEDI_SUBD_AI;
250 s->subdev_flags = SDF_READABLE | SDF_GROUND;
0f904604
HS
251 s->n_chan = 16;
252 s->maxdata = 0xffff;
f4bd8abb 253 s->range_table = &range_unknown;
6eef3af5
BP
254 s->len_chanlist = 1; /* This is the maximum chanlist length that */
255 /* the board can handle */
f4bd8abb
RB
256 s->insn_read = cb_pcimdas_ai_rinsn;
257
13201d88 258 s = &dev->subdevices[1];
6eef3af5 259 /* analog output subdevice */
f4bd8abb
RB
260 s->type = COMEDI_SUBD_AO;
261 s->subdev_flags = SDF_WRITABLE;
0f904604
HS
262 s->n_chan = 2;
263 s->maxdata = 0xfff;
190908ba
RKM
264 /* ranges are hardware settable, but not software readable. */
265 s->range_table = &range_unknown;
f4bd8abb
RB
266 s->insn_write = &cb_pcimdas_ao_winsn;
267 s->insn_read = &cb_pcimdas_ao_rinsn;
268
13201d88 269 s = &dev->subdevices[2];
f4bd8abb 270 /* digital i/o subdevice */
0f904604 271 subdev_8255_init(dev, s, NULL, iobase_8255);
f4bd8abb 272
2d069fbe
HS
273 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
274
275 return 0;
f4bd8abb
RB
276}
277
484ecc95 278static void cb_pcimdas_detach(struct comedi_device *dev)
f4bd8abb 279{
f4bd8abb 280 if (dev->irq)
5f74ea14 281 free_irq(dev->irq, dev);
7f072f54 282 comedi_pci_disable(dev);
f4bd8abb
RB
283}
284
0835145d
HS
285static struct comedi_driver cb_pcimdas_driver = {
286 .driver_name = "cb_pcimdas",
287 .module = THIS_MODULE,
750af5e5 288 .auto_attach = cb_pcimdas_auto_attach,
0835145d
HS
289 .detach = cb_pcimdas_detach,
290};
291
a690b7e5 292static int cb_pcimdas_pci_probe(struct pci_dev *dev,
b8f4ac23 293 const struct pci_device_id *id)
727b286b 294{
b8f4ac23
HS
295 return comedi_pci_auto_config(dev, &cb_pcimdas_driver,
296 id->driver_data);
727b286b
AT
297}
298
0835145d 299static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
cb3e9d86 300 { PCI_DEVICE(PCI_VENDOR_ID_CB, 0x0056) },
0835145d 301 { 0 }
727b286b 302};
0835145d 303MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
727b286b 304
0835145d
HS
305static struct pci_driver cb_pcimdas_pci_driver = {
306 .name = "cb_pcimdas",
307 .id_table = cb_pcimdas_pci_table,
308 .probe = cb_pcimdas_pci_probe,
9901a4d7 309 .remove = comedi_pci_auto_unconfig,
0835145d
HS
310};
311module_comedi_pci_driver(cb_pcimdas_driver, cb_pcimdas_pci_driver);
90f703d3
AT
312
313MODULE_AUTHOR("Comedi http://www.comedi.org");
314MODULE_DESCRIPTION("Comedi low-level driver");
315MODULE_LICENSE("GPL");
This page took 0.553543 seconds and 5 git commands to generate.