Staging: comedi: drivers: fix coding style issues in pcl812.c
[GitHub/mt8127/android_kernel_alcatel_ttab.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.
40See http://www.measurementcomputing.com/PDFManuals/pcim-das1602_16.pdf for more details.
41*/
42
43#include "../comedidev.h"
44
45#include <linux/delay.h>
25436dc9 46#include <linux/interrupt.h>
f4bd8abb
RB
47
48#include "comedi_pci.h"
49#include "plx9052.h"
50#include "8255.h"
51
6eef3af5 52/* #define CBPCIMDAS_DEBUG */
f4bd8abb
RB
53#undef CBPCIMDAS_DEBUG
54
558587e2
GKH
55#define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
56
f4bd8abb
RB
57/* Registers for the PCIM-DAS1602/16 */
58
6eef3af5
BP
59/* sizes of io regions (bytes) */
60#define BADR0_SIZE 2 /* ?? */
f4bd8abb
RB
61#define BADR1_SIZE 4
62#define BADR2_SIZE 6
63#define BADR3_SIZE 16
64#define BADR4_SIZE 4
65
6eef3af5 66/* DAC Offsets */
f4bd8abb
RB
67#define ADC_TRIG 0
68#define DAC0_OFFSET 2
69#define DAC1_OFFSET 4
70
6eef3af5 71/* AI and Counter Constants */
f4bd8abb
RB
72#define MUX_LIMITS 0
73#define MAIN_CONN_DIO 1
74#define ADC_STAT 2
75#define ADC_CONV_STAT 3
76#define ADC_INT 4
77#define ADC_PACER 5
78#define BURST_MODE 6
79#define PROG_GAIN 7
80#define CLK8254_1_DATA 8
81#define CLK8254_2_DATA 9
82#define CLK8254_3_DATA 10
83#define CLK8254_CONTROL 11
84#define USER_COUNTER 12
85#define RESID_COUNT_H 13
86#define RESID_COUNT_L 14
87
88/* Board description */
cfee7b99 89struct cb_pcimdas_board {
f4bd8abb
RB
90 const char *name;
91 unsigned short device_id;
6eef3af5
BP
92 int ai_se_chans; /* Inputs in single-ended mode */
93 int ai_diff_chans; /* Inputs in differential mode */
94 int ai_bits; /* analog input resolution */
95 int ai_speed; /* fastest conversion period in ns */
96 int ao_nchan; /* number of analog out channels */
97 int ao_bits; /* analogue output resolution */
98 int has_ao_fifo; /* analog output has fifo */
99 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */
100 int fifo_size; /* number of samples fifo can hold */
101 int dio_bits; /* number of dio bits */
102 int has_dio; /* has DIO */
9ced1de6 103 const struct comedi_lrange *ranges;
cfee7b99 104};
f4bd8abb 105
cfee7b99 106static const struct cb_pcimdas_board cb_pcimdas_boards[] = {
f4bd8abb 107 {
0a85b6f0
MT
108 .name = "PCIM-DAS1602/16",
109 .device_id = 0x56,
110 .ai_se_chans = 16,
111 .ai_diff_chans = 8,
112 .ai_bits = 16,
113 .ai_speed = 10000, /* ?? */
114 .ao_nchan = 2,
115 .ao_bits = 12,
116 .has_ao_fifo = 0, /* ?? */
117 .ao_scan_speed = 10000,
118 /* ?? */
119 .fifo_size = 1024,
120 .dio_bits = 24,
121 .has_dio = 1,
68c3dbff 122/* .ranges = &cb_pcimdas_ranges, */
0a85b6f0 123 },
f4bd8abb
RB
124};
125
126/* This is used by modprobe to translate PCI IDs to drivers. Should
127 * only be used for PCI and ISA-PnP devices */
128static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
0a85b6f0
MT
129 {
130 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
131 {
132 0}
f4bd8abb
RB
133};
134
135MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
136
6eef3af5 137#define N_BOARDS 1 /* Max number of boards supported */
f4bd8abb
RB
138
139/*
140 * Useful for shorthand access to the particular board structure
141 */
cfee7b99 142#define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr)
f4bd8abb
RB
143
144/* this structure is for data unique to this hardware driver. If
145 several hardware drivers keep similar information in this structure,
71b5f4f1 146 feel free to suggest moving the variable to the struct comedi_device struct. */
b90c76fd 147struct cb_pcimdas_private {
f4bd8abb
RB
148 int data;
149
6eef3af5 150 /* would be useful for a PCI device */
f4bd8abb
RB
151 struct pci_dev *pci_dev;
152
6eef3af5 153 /* base addresses */
f4bd8abb
RB
154 unsigned long BADR0;
155 unsigned long BADR1;
156 unsigned long BADR2;
157 unsigned long BADR3;
158 unsigned long BADR4;
159
160 /* Used for AO readback */
790c5541 161 unsigned int ao_readback[2];
f4bd8abb 162
6eef3af5
BP
163 /* Used for DIO */
164 unsigned short int port_a; /* copy of BADR4+0 */
165 unsigned short int port_b; /* copy of BADR4+1 */
166 unsigned short int port_c; /* copy of BADR4+2 */
167 unsigned short int dio_mode; /* copy of BADR4+3 */
f4bd8abb 168
b90c76fd 169};
f4bd8abb
RB
170
171/*
172 * most drivers define the following macro to make it easy to
173 * access the private structure.
174 */
b90c76fd 175#define devpriv ((struct cb_pcimdas_private *)dev->private)
f4bd8abb
RB
176
177/*
139dfbdf 178 * The struct comedi_driver structure tells the Comedi core module
f4bd8abb
RB
179 * which functions to call to configure/deconfigure (attach/detach)
180 * the board, and also about the kernel module that contains
181 * the device code.
182 */
0a85b6f0
MT
183static int cb_pcimdas_attach(struct comedi_device *dev,
184 struct comedi_devconfig *it);
da91b269 185static int cb_pcimdas_detach(struct comedi_device *dev);
139dfbdf 186static struct comedi_driver driver_cb_pcimdas = {
68c3dbff
BP
187 .driver_name = "cb_pcimdas",
188 .module = THIS_MODULE,
189 .attach = cb_pcimdas_attach,
190 .detach = cb_pcimdas_detach,
f4bd8abb
RB
191};
192
0a85b6f0
MT
193static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
194 struct comedi_subdevice *s,
195 struct comedi_insn *insn, unsigned int *data);
196static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
197 struct comedi_subdevice *s,
198 struct comedi_insn *insn, unsigned int *data);
199static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
200 struct comedi_subdevice *s,
201 struct comedi_insn *insn, unsigned int *data);
f4bd8abb
RB
202
203/*
204 * Attach is called by the Comedi core to configure the driver
205 * for a particular board. If you specified a board_name array
206 * in the driver structure, dev->board_ptr contains that
207 * address.
208 */
0a85b6f0
MT
209static int cb_pcimdas_attach(struct comedi_device *dev,
210 struct comedi_devconfig *it)
f4bd8abb 211{
34c43922 212 struct comedi_subdevice *s;
f4bd8abb
RB
213 struct pci_dev *pcidev;
214 int index;
6eef3af5 215 /* int i; */
f4bd8abb
RB
216
217 printk("comedi%d: cb_pcimdas: ", dev->minor);
218
219/*
220 * Allocate the private structure area.
221 */
b90c76fd 222 if (alloc_private(dev, sizeof(struct cb_pcimdas_private)) < 0)
f4bd8abb
RB
223 return -ENOMEM;
224
225/*
226 * Probe the device to determine what device in the series it is.
227 */
228 printk("\n");
229
230 for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
0a85b6f0
MT
231 pcidev != NULL;
232 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
6eef3af5 233 /* is it not a computer boards card? */
f4bd8abb
RB
234 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
235 continue;
6eef3af5 236 /* loop through cards supported by this driver */
f4bd8abb
RB
237 for (index = 0; index < N_BOARDS; index++) {
238 if (cb_pcimdas_boards[index].device_id !=
0a85b6f0 239 pcidev->device)
f4bd8abb 240 continue;
6eef3af5 241 /* was a particular bus/slot requested? */
f4bd8abb 242 if (it->options[0] || it->options[1]) {
6eef3af5 243 /* are we on the wrong bus/slot? */
f4bd8abb 244 if (pcidev->bus->number != it->options[0] ||
0a85b6f0 245 PCI_SLOT(pcidev->devfn) != it->options[1]) {
f4bd8abb
RB
246 continue;
247 }
248 }
249 devpriv->pci_dev = pcidev;
250 dev->board_ptr = cb_pcimdas_boards + index;
251 goto found;
252 }
253 }
254
255 printk("No supported ComputerBoards/MeasurementComputing card found on "
0a85b6f0 256 "requested position\n");
f4bd8abb
RB
257 return -EIO;
258
0a85b6f0 259found:
f4bd8abb
RB
260
261 printk("Found %s on bus %i, slot %i\n", cb_pcimdas_boards[index].name,
0a85b6f0 262 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
f4bd8abb 263
6eef3af5 264 /* Warn about non-tested features */
f4bd8abb
RB
265 switch (thisboard->device_id) {
266 case 0x56:
267 break;
268 default:
269 printk("THIS CARD IS UNSUPPORTED.\n"
0a85b6f0 270 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
f4bd8abb
RB
271 };
272
273 if (comedi_pci_enable(pcidev, "cb_pcimdas")) {
274 printk(" Failed to enable PCI device and request regions\n");
275 return -EIO;
276 }
277
278 devpriv->BADR0 = pci_resource_start(devpriv->pci_dev, 0);
279 devpriv->BADR1 = pci_resource_start(devpriv->pci_dev, 1);
280 devpriv->BADR2 = pci_resource_start(devpriv->pci_dev, 2);
281 devpriv->BADR3 = pci_resource_start(devpriv->pci_dev, 3);
282 devpriv->BADR4 = pci_resource_start(devpriv->pci_dev, 4);
283
284#ifdef CBPCIMDAS_DEBUG
285 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
286 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
287 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
288 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
289 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
290#endif
291
6eef3af5
BP
292/* Dont support IRQ yet */
293/* get irq */
5f74ea14 294/* if(request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
6eef3af5
BP
295/* { */
296/* printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq); */
297/* return -EINVAL; */
298/* } */
299/* dev->irq = devpriv->pci_dev->irq; */
f4bd8abb 300
6eef3af5 301 /* Initialize dev->board_name */
f4bd8abb
RB
302 dev->board_name = thisboard->name;
303
304/*
305 * Allocate the subdevice structures. alloc_subdevice() is a
306 * convenient macro defined in comedidev.h.
307 */
308 if (alloc_subdevices(dev, 3) < 0)
309 return -ENOMEM;
310
311 s = dev->subdevices + 0;
6eef3af5
BP
312 /* dev->read_subdev=s; */
313 /* analog input subdevice */
f4bd8abb
RB
314 s->type = COMEDI_SUBD_AI;
315 s->subdev_flags = SDF_READABLE | SDF_GROUND;
316 s->n_chan = thisboard->ai_se_chans;
317 s->maxdata = (1 << thisboard->ai_bits) - 1;
318 s->range_table = &range_unknown;
6eef3af5
BP
319 s->len_chanlist = 1; /* This is the maximum chanlist length that */
320 /* the board can handle */
f4bd8abb
RB
321 s->insn_read = cb_pcimdas_ai_rinsn;
322
323 s = dev->subdevices + 1;
6eef3af5 324 /* analog output subdevice */
f4bd8abb
RB
325 s->type = COMEDI_SUBD_AO;
326 s->subdev_flags = SDF_WRITABLE;
327 s->n_chan = thisboard->ao_nchan;
328 s->maxdata = 1 << thisboard->ao_bits;
6eef3af5 329 s->range_table = &range_unknown; /* ranges are hardware settable, but not software readable. */
f4bd8abb
RB
330 s->insn_write = &cb_pcimdas_ao_winsn;
331 s->insn_read = &cb_pcimdas_ao_rinsn;
332
333 s = dev->subdevices + 2;
334 /* digital i/o subdevice */
7c2f1946 335 if (thisboard->has_dio)
f4bd8abb 336 subdev_8255_init(dev, s, NULL, devpriv->BADR4);
7c2f1946 337 else
f4bd8abb 338 s->type = COMEDI_SUBD_UNUSED;
f4bd8abb
RB
339
340 printk("attached\n");
341
342 return 1;
343}
344
345/*
346 * _detach is called to deconfigure a device. It should deallocate
347 * resources.
348 * This function is also called when _attach() fails, so it should be
349 * careful not to release resources that were not necessarily
350 * allocated by _attach(). dev->private and dev->subdevices are
351 * deallocated automatically by the core.
352 */
da91b269 353static int cb_pcimdas_detach(struct comedi_device *dev)
f4bd8abb
RB
354{
355#ifdef CBPCIMDAS_DEBUG
356 if (devpriv) {
357 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
358 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
359 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
360 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
361 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
362 }
363#endif
364 printk("comedi%d: cb_pcimdas: remove\n", dev->minor);
365 if (dev->irq)
5f74ea14 366 free_irq(dev->irq, dev);
f4bd8abb
RB
367 if (devpriv) {
368 if (devpriv->pci_dev) {
7c2f1946 369 if (devpriv->BADR0)
f4bd8abb 370 comedi_pci_disable(devpriv->pci_dev);
f4bd8abb
RB
371 pci_dev_put(devpriv->pci_dev);
372 }
373 }
374
375 return 0;
376}
377
378/*
379 * "instructions" read/write data in "one-shot" or "software-triggered"
380 * mode.
381 */
0a85b6f0
MT
382static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
383 struct comedi_subdevice *s,
384 struct comedi_insn *insn, unsigned int *data)
f4bd8abb
RB
385{
386 int n, i;
387 unsigned int d;
388 unsigned int busy;
389 int chan = CR_CHAN(insn->chanspec);
390 unsigned short chanlims;
391 int maxchans;
392
6eef3af5 393 /* only support sw initiated reads from a single channel */
f4bd8abb 394
6eef3af5
BP
395 /* check channel number */
396 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
f4bd8abb
RB
397 maxchans = thisboard->ai_diff_chans;
398 else
399 maxchans = thisboard->ai_se_chans;
400
401 if (chan > (maxchans - 1))
6eef3af5 402 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
f4bd8abb 403
6eef3af5 404 /* configure for sw initiated read */
f4bd8abb 405 d = inb(devpriv->BADR3 + 5);
6eef3af5 406 if ((d & 0x03) > 0) { /* only reset if needed. */
f4bd8abb
RB
407 d = d & 0xfd;
408 outb(d, devpriv->BADR3 + 5);
409 }
6eef3af5
BP
410 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
411 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
f4bd8abb 412
6eef3af5 413 /* write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan. */
f4bd8abb
RB
414 chanlims = chan | (chan << 4);
415 outb(chanlims, devpriv->BADR3 + 0);
416
417 /* convert n samples */
418 for (n = 0; n < insn->n; n++) {
419 /* trigger conversion */
420 outw(0, devpriv->BADR2 + 0);
421
6eef3af5
BP
422#define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
423 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
f4bd8abb
RB
424
425 /* wait for conversion to end */
426 for (i = 0; i < TIMEOUT; i++) {
427 busy = inb(devpriv->BADR3 + 2) & 0x80;
428 if (!busy)
429 break;
430 }
431 if (i == TIMEOUT) {
432 printk("timeout\n");
433 return -ETIMEDOUT;
434 }
435 /* read data */
436 d = inw(devpriv->BADR2 + 0);
437
438 /* mangle the data as necessary */
6eef3af5 439 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
f4bd8abb
RB
440
441 data[n] = d;
442 }
443
444 /* return the number of samples read/written */
445 return n;
446}
447
0a85b6f0
MT
448static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
449 struct comedi_subdevice *s,
450 struct comedi_insn *insn, unsigned int *data)
f4bd8abb
RB
451{
452 int i;
453 int chan = CR_CHAN(insn->chanspec);
454
455 /* Writing a list of values to an AO channel is probably not
456 * very useful, but that's how the interface is defined. */
457 for (i = 0; i < insn->n; i++) {
458 switch (chan) {
459 case 0:
460 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC0_OFFSET);
461 break;
462 case 1:
463 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC1_OFFSET);
464 break;
465 default:
466 return -1;
467 }
468 devpriv->ao_readback[chan] = data[i];
469 }
470
471 /* return the number of samples read/written */
472 return i;
473}
474
475/* AO subdevices should have a read insn as well as a write insn.
476 * Usually this means copying a value stored in devpriv. */
0a85b6f0
MT
477static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
478 struct comedi_subdevice *s,
479 struct comedi_insn *insn, unsigned int *data)
f4bd8abb
RB
480{
481 int i;
482 int chan = CR_CHAN(insn->chanspec);
483
484 for (i = 0; i < insn->n; i++)
485 data[i] = devpriv->ao_readback[chan];
486
487 return i;
488}
489
490/*
491 * A convenient macro that defines init_module() and cleanup_module(),
492 * as necessary.
493 */
727b286b
AT
494static int __devinit driver_cb_pcimdas_pci_probe(struct pci_dev *dev,
495 const struct pci_device_id
496 *ent)
497{
498 return comedi_pci_auto_config(dev, driver_cb_pcimdas.driver_name);
499}
500
501static void __devexit driver_cb_pcimdas_pci_remove(struct pci_dev *dev)
502{
503 comedi_pci_auto_unconfig(dev);
504}
505
506static struct pci_driver driver_cb_pcimdas_pci_driver = {
507 .id_table = cb_pcimdas_pci_table,
508 .probe = &driver_cb_pcimdas_pci_probe,
509 .remove = __devexit_p(&driver_cb_pcimdas_pci_remove)
510};
511
512static int __init driver_cb_pcimdas_init_module(void)
513{
514 int retval;
515
516 retval = comedi_driver_register(&driver_cb_pcimdas);
517 if (retval < 0)
518 return retval;
519
520 driver_cb_pcimdas_pci_driver.name =
521 (char *)driver_cb_pcimdas.driver_name;
522 return pci_register_driver(&driver_cb_pcimdas_pci_driver);
523}
524
525static void __exit driver_cb_pcimdas_cleanup_module(void)
526{
527 pci_unregister_driver(&driver_cb_pcimdas_pci_driver);
528 comedi_driver_unregister(&driver_cb_pcimdas);
529}
530
531module_init(driver_cb_pcimdas_init_module);
532module_exit(driver_cb_pcimdas_cleanup_module);
90f703d3
AT
533
534MODULE_AUTHOR("Comedi http://www.comedi.org");
535MODULE_DESCRIPTION("Comedi low-level driver");
536MODULE_LICENSE("GPL");