Commit | Line | Data |
---|---|---|
ef2ccffb DS |
1 | /* |
2 | comedi/drivers/ni_6527.c | |
3 | driver for National Instruments PCI-6527 | |
4 | ||
5 | COMEDI - Linux Control and Measurement Device Interface | |
6 | Copyright (C) 1999,2002,2003 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: ni_6527 | |
25 | Description: National Instruments 6527 | |
26 | Author: ds | |
27 | Status: works | |
28 | Devices: [National Instruments] PCI-6527 (ni6527), PXI-6527 | |
29 | Updated: Sat, 25 Jan 2003 13:24:40 -0800 | |
30 | ||
31 | ||
32 | */ | |
33 | ||
34 | /* | |
35 | Manuals (available from ftp://ftp.natinst.com/support/manuals) | |
36 | ||
37 | 370106b.pdf 6527 Register Level Programmer Manual | |
38 | ||
39 | */ | |
40 | ||
41 | #define DEBUG 1 | |
42 | #define DEBUG_FLAGS | |
43 | ||
44 | #include "../comedidev.h" | |
45 | ||
46 | #include "mite.h" | |
47 | ||
48 | #define NI6527_DIO_SIZE 4096 | |
49 | #define NI6527_MITE_SIZE 4096 | |
50 | ||
51 | #define Port_Register(x) (0x00+(x)) | |
52 | #define ID_Register 0x06 | |
53 | ||
54 | #define Clear_Register 0x07 | |
55 | #define ClrEdge 0x08 | |
56 | #define ClrOverflow 0x04 | |
57 | #define ClrFilter 0x02 | |
58 | #define ClrInterval 0x01 | |
59 | ||
60 | #define Filter_Interval(x) (0x08+(x)) | |
61 | #define Filter_Enable(x) (0x0c+(x)) | |
62 | ||
63 | #define Change_Status 0x14 | |
64 | #define MasterInterruptStatus 0x04 | |
65 | #define Overflow 0x02 | |
66 | #define EdgeStatus 0x01 | |
67 | ||
68 | #define Master_Interrupt_Control 0x15 | |
69 | #define FallingEdgeIntEnable 0x10 | |
70 | #define RisingEdgeIntEnable 0x08 | |
71 | #define MasterInterruptEnable 0x04 | |
72 | #define OverflowIntEnable 0x02 | |
73 | #define EdgeIntEnable 0x01 | |
74 | ||
75 | #define Rising_Edge_Detection_Enable(x) (0x018+(x)) | |
76 | #define Falling_Edge_Detection_Enable(x) (0x020+(x)) | |
77 | ||
da91b269 BP |
78 | static int ni6527_attach(struct comedi_device *dev, struct comedi_devconfig *it); |
79 | static int ni6527_detach(struct comedi_device *dev); | |
139dfbdf | 80 | static struct comedi_driver driver_ni6527 = { |
68c3dbff BP |
81 | .driver_name = "ni6527", |
82 | .module = THIS_MODULE, | |
83 | .attach = ni6527_attach, | |
84 | .detach = ni6527_detach, | |
ef2ccffb DS |
85 | }; |
86 | ||
16d38ca3 BP |
87 | struct ni6527_board { |
88 | ||
ef2ccffb DS |
89 | int dev_id; |
90 | const char *name; | |
16d38ca3 BP |
91 | }; |
92 | ||
93 | static const struct ni6527_board ni6527_boards[] = { | |
ef2ccffb | 94 | { |
68c3dbff BP |
95 | .dev_id = 0x2b20, |
96 | .name = "pci-6527", | |
ef2ccffb DS |
97 | }, |
98 | { | |
68c3dbff BP |
99 | .dev_id = 0x2b10, |
100 | .name = "pxi-6527", | |
ef2ccffb DS |
101 | }, |
102 | }; | |
103 | ||
104 | #define n_ni6527_boards (sizeof(ni6527_boards)/sizeof(ni6527_boards[0])) | |
16d38ca3 | 105 | #define this_board ((const struct ni6527_board *)dev->board_ptr) |
ef2ccffb DS |
106 | |
107 | static DEFINE_PCI_DEVICE_TABLE(ni6527_pci_table) = { | |
108 | {PCI_VENDOR_ID_NATINST, 0x2b10, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, | |
109 | {PCI_VENDOR_ID_NATINST, 0x2b20, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, | |
110 | {0} | |
111 | }; | |
112 | ||
113 | MODULE_DEVICE_TABLE(pci, ni6527_pci_table); | |
114 | ||
f4c6b319 | 115 | struct ni6527_private { |
ef2ccffb DS |
116 | struct mite_struct *mite; |
117 | unsigned int filter_interval; | |
118 | unsigned int filter_enable; | |
f4c6b319 BP |
119 | }; |
120 | ||
121 | #define devpriv ((struct ni6527_private *)dev->private) | |
ef2ccffb | 122 | |
da91b269 | 123 | static int ni6527_find_device(struct comedi_device *dev, int bus, int slot); |
ef2ccffb | 124 | |
da91b269 BP |
125 | static int ni6527_di_insn_config(struct comedi_device *dev, struct comedi_subdevice *s, |
126 | struct comedi_insn *insn, unsigned int *data) | |
ef2ccffb DS |
127 | { |
128 | int chan = CR_CHAN(insn->chanspec); | |
129 | unsigned int interval; | |
130 | ||
131 | if (insn->n != 2) | |
132 | return -EINVAL; | |
133 | ||
134 | if (data[0] != INSN_CONFIG_FILTER) | |
135 | return -EINVAL; | |
136 | ||
137 | if (data[1]) { | |
138 | interval = (data[1] + 100) / 200; | |
139 | data[1] = interval * 200; | |
140 | ||
141 | if (interval != devpriv->filter_interval) { | |
142 | writeb(interval & 0xff, | |
143 | devpriv->mite->daq_io_addr + | |
144 | Filter_Interval(0)); | |
145 | writeb((interval >> 8) & 0xff, | |
146 | devpriv->mite->daq_io_addr + | |
147 | Filter_Interval(1)); | |
148 | writeb((interval >> 16) & 0x0f, | |
149 | devpriv->mite->daq_io_addr + | |
150 | Filter_Interval(2)); | |
151 | ||
152 | writeb(ClrInterval, | |
153 | devpriv->mite->daq_io_addr + Clear_Register); | |
154 | ||
155 | devpriv->filter_interval = interval; | |
156 | } | |
157 | ||
158 | devpriv->filter_enable |= 1 << chan; | |
159 | } else { | |
160 | devpriv->filter_enable &= ~(1 << chan); | |
161 | } | |
162 | ||
163 | writeb(devpriv->filter_enable, | |
164 | devpriv->mite->daq_io_addr + Filter_Enable(0)); | |
165 | writeb(devpriv->filter_enable >> 8, | |
166 | devpriv->mite->daq_io_addr + Filter_Enable(1)); | |
167 | writeb(devpriv->filter_enable >> 16, | |
168 | devpriv->mite->daq_io_addr + Filter_Enable(2)); | |
169 | ||
170 | return 2; | |
171 | } | |
172 | ||
da91b269 BP |
173 | static int ni6527_di_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, |
174 | struct comedi_insn *insn, unsigned int *data) | |
ef2ccffb DS |
175 | { |
176 | if (insn->n != 2) | |
177 | return -EINVAL; | |
178 | ||
179 | data[1] = readb(devpriv->mite->daq_io_addr + Port_Register(0)); | |
180 | data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(1)) << 8; | |
181 | data[1] |= readb(devpriv->mite->daq_io_addr + Port_Register(2)) << 16; | |
182 | ||
183 | return 2; | |
184 | } | |
185 | ||
da91b269 BP |
186 | static int ni6527_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, |
187 | struct comedi_insn *insn, unsigned int *data) | |
ef2ccffb DS |
188 | { |
189 | if (insn->n != 2) | |
190 | return -EINVAL; | |
191 | if (data[0]) { | |
192 | s->state &= ~data[0]; | |
193 | s->state |= (data[0] & data[1]); | |
194 | ||
195 | /* The open relay state on the board cooresponds to 1, | |
196 | * but in Comedi, it is represented by 0. */ | |
197 | if (data[0] & 0x0000ff) { | |
198 | writeb((s->state ^ 0xff), | |
199 | devpriv->mite->daq_io_addr + Port_Register(3)); | |
200 | } | |
201 | if (data[0] & 0x00ff00) { | |
202 | writeb((s->state >> 8) ^ 0xff, | |
203 | devpriv->mite->daq_io_addr + Port_Register(4)); | |
204 | } | |
205 | if (data[0] & 0xff0000) { | |
206 | writeb((s->state >> 16) ^ 0xff, | |
207 | devpriv->mite->daq_io_addr + Port_Register(5)); | |
208 | } | |
209 | } | |
210 | data[1] = s->state; | |
211 | ||
212 | return 2; | |
213 | } | |
214 | ||
70265d24 | 215 | static irqreturn_t ni6527_interrupt(int irq, void *d) |
ef2ccffb | 216 | { |
71b5f4f1 | 217 | struct comedi_device *dev = d; |
34c43922 | 218 | struct comedi_subdevice *s = dev->subdevices + 2; |
ef2ccffb DS |
219 | unsigned int status; |
220 | ||
221 | status = readb(devpriv->mite->daq_io_addr + Change_Status); | |
222 | if ((status & MasterInterruptStatus) == 0) | |
223 | return IRQ_NONE; | |
224 | if ((status & EdgeStatus) == 0) | |
225 | return IRQ_NONE; | |
226 | ||
227 | writeb(ClrEdge | ClrOverflow, | |
228 | devpriv->mite->daq_io_addr + Clear_Register); | |
229 | ||
230 | comedi_buf_put(s->async, 0); | |
231 | s->async->events |= COMEDI_CB_EOS; | |
232 | comedi_event(dev, s); | |
233 | return IRQ_HANDLED; | |
234 | } | |
235 | ||
da91b269 BP |
236 | static int ni6527_intr_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, |
237 | struct comedi_cmd *cmd) | |
ef2ccffb DS |
238 | { |
239 | int err = 0; | |
240 | int tmp; | |
241 | ||
242 | /* step 1: make sure trigger sources are trivially valid */ | |
243 | ||
244 | tmp = cmd->start_src; | |
245 | cmd->start_src &= TRIG_NOW; | |
246 | if (!cmd->start_src || tmp != cmd->start_src) | |
247 | err++; | |
248 | ||
249 | tmp = cmd->scan_begin_src; | |
250 | cmd->scan_begin_src &= TRIG_OTHER; | |
251 | if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src) | |
252 | err++; | |
253 | ||
254 | tmp = cmd->convert_src; | |
255 | cmd->convert_src &= TRIG_FOLLOW; | |
256 | if (!cmd->convert_src || tmp != cmd->convert_src) | |
257 | err++; | |
258 | ||
259 | tmp = cmd->scan_end_src; | |
260 | cmd->scan_end_src &= TRIG_COUNT; | |
261 | if (!cmd->scan_end_src || tmp != cmd->scan_end_src) | |
262 | err++; | |
263 | ||
264 | tmp = cmd->stop_src; | |
265 | cmd->stop_src &= TRIG_COUNT; | |
266 | if (!cmd->stop_src || tmp != cmd->stop_src) | |
267 | err++; | |
268 | ||
269 | if (err) | |
270 | return 1; | |
271 | ||
272 | /* step 2: make sure trigger sources are unique and mutually compatible */ | |
273 | ||
274 | if (err) | |
275 | return 2; | |
276 | ||
277 | /* step 3: make sure arguments are trivially compatible */ | |
278 | ||
279 | if (cmd->start_arg != 0) { | |
280 | cmd->start_arg = 0; | |
281 | err++; | |
282 | } | |
283 | if (cmd->scan_begin_arg != 0) { | |
284 | cmd->scan_begin_arg = 0; | |
285 | err++; | |
286 | } | |
287 | if (cmd->convert_arg != 0) { | |
288 | cmd->convert_arg = 0; | |
289 | err++; | |
290 | } | |
291 | ||
292 | if (cmd->scan_end_arg != 1) { | |
293 | cmd->scan_end_arg = 1; | |
294 | err++; | |
295 | } | |
296 | if (cmd->stop_arg != 0) { | |
297 | cmd->stop_arg = 0; | |
298 | err++; | |
299 | } | |
300 | ||
301 | if (err) | |
302 | return 3; | |
303 | ||
304 | /* step 4: fix up any arguments */ | |
305 | ||
306 | if (err) | |
307 | return 4; | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
da91b269 | 312 | static int ni6527_intr_cmd(struct comedi_device *dev, struct comedi_subdevice *s) |
ef2ccffb | 313 | { |
2696fb57 | 314 | /* struct comedi_cmd *cmd = &s->async->cmd; */ |
ef2ccffb DS |
315 | |
316 | writeb(ClrEdge | ClrOverflow, | |
317 | devpriv->mite->daq_io_addr + Clear_Register); | |
318 | writeb(FallingEdgeIntEnable | RisingEdgeIntEnable | | |
319 | MasterInterruptEnable | EdgeIntEnable, | |
320 | devpriv->mite->daq_io_addr + Master_Interrupt_Control); | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
da91b269 | 325 | static int ni6527_intr_cancel(struct comedi_device *dev, struct comedi_subdevice *s) |
ef2ccffb DS |
326 | { |
327 | writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control); | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
da91b269 BP |
332 | static int ni6527_intr_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, |
333 | struct comedi_insn *insn, unsigned int *data) | |
ef2ccffb DS |
334 | { |
335 | if (insn->n < 1) | |
336 | return -EINVAL; | |
337 | ||
338 | data[1] = 0; | |
339 | return 2; | |
340 | } | |
341 | ||
da91b269 BP |
342 | static int ni6527_intr_insn_config(struct comedi_device *dev, struct comedi_subdevice *s, |
343 | struct comedi_insn *insn, unsigned int *data) | |
ef2ccffb DS |
344 | { |
345 | if (insn->n < 1) | |
346 | return -EINVAL; | |
347 | if (data[0] != INSN_CONFIG_CHANGE_NOTIFY) | |
348 | return -EINVAL; | |
349 | ||
350 | writeb(data[1], | |
351 | devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(0)); | |
352 | writeb(data[1] >> 8, | |
353 | devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(1)); | |
354 | writeb(data[1] >> 16, | |
355 | devpriv->mite->daq_io_addr + Rising_Edge_Detection_Enable(2)); | |
356 | ||
357 | writeb(data[2], | |
358 | devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(0)); | |
359 | writeb(data[2] >> 8, | |
360 | devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(1)); | |
361 | writeb(data[2] >> 16, | |
362 | devpriv->mite->daq_io_addr + Falling_Edge_Detection_Enable(2)); | |
363 | ||
364 | return 2; | |
365 | } | |
366 | ||
da91b269 | 367 | static int ni6527_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
ef2ccffb | 368 | { |
34c43922 | 369 | struct comedi_subdevice *s; |
ef2ccffb DS |
370 | int ret; |
371 | ||
372 | printk("comedi%d: ni6527:", dev->minor); | |
373 | ||
c3744138 BP |
374 | ret = alloc_private(dev, sizeof(struct ni6527_private)); |
375 | if (ret < 0) | |
ef2ccffb DS |
376 | return ret; |
377 | ||
378 | ret = ni6527_find_device(dev, it->options[0], it->options[1]); | |
379 | if (ret < 0) | |
380 | return ret; | |
381 | ||
382 | ret = mite_setup(devpriv->mite); | |
383 | if (ret < 0) { | |
384 | printk("error setting up mite\n"); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | dev->board_name = this_board->name; | |
389 | printk(" %s", dev->board_name); | |
390 | ||
391 | printk(" ID=0x%02x", readb(devpriv->mite->daq_io_addr + ID_Register)); | |
392 | ||
c3744138 BP |
393 | ret = alloc_subdevices(dev, 3); |
394 | if (ret < 0) | |
ef2ccffb DS |
395 | return ret; |
396 | ||
397 | s = dev->subdevices + 0; | |
398 | s->type = COMEDI_SUBD_DI; | |
399 | s->subdev_flags = SDF_READABLE; | |
400 | s->n_chan = 24; | |
401 | s->range_table = &range_digital; | |
402 | s->maxdata = 1; | |
403 | s->insn_config = ni6527_di_insn_config; | |
404 | s->insn_bits = ni6527_di_insn_bits; | |
405 | ||
406 | s = dev->subdevices + 1; | |
407 | s->type = COMEDI_SUBD_DO; | |
408 | s->subdev_flags = SDF_READABLE | SDF_WRITABLE; | |
409 | s->n_chan = 24; | |
410 | s->range_table = &range_unknown; /* FIXME: actually conductance */ | |
411 | s->maxdata = 1; | |
412 | s->insn_bits = ni6527_do_insn_bits; | |
413 | ||
414 | s = dev->subdevices + 2; | |
415 | dev->read_subdev = s; | |
416 | s->type = COMEDI_SUBD_DI; | |
417 | s->subdev_flags = SDF_READABLE | SDF_CMD_READ; | |
418 | s->n_chan = 1; | |
419 | s->range_table = &range_unknown; | |
420 | s->maxdata = 1; | |
421 | s->do_cmdtest = ni6527_intr_cmdtest; | |
422 | s->do_cmd = ni6527_intr_cmd; | |
423 | s->cancel = ni6527_intr_cancel; | |
424 | s->insn_bits = ni6527_intr_insn_bits; | |
425 | s->insn_config = ni6527_intr_insn_config; | |
426 | ||
427 | writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(0)); | |
428 | writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(1)); | |
429 | writeb(0x00, devpriv->mite->daq_io_addr + Filter_Enable(2)); | |
430 | ||
431 | writeb(ClrEdge | ClrOverflow | ClrFilter | ClrInterval, | |
432 | devpriv->mite->daq_io_addr + Clear_Register); | |
433 | writeb(0x00, devpriv->mite->daq_io_addr + Master_Interrupt_Control); | |
434 | ||
5f74ea14 GKH |
435 | ret = request_irq(mite_irq(devpriv->mite), ni6527_interrupt, |
436 | IRQF_SHARED, "ni6527", dev); | |
ef2ccffb DS |
437 | if (ret < 0) { |
438 | printk(" irq not available"); | |
439 | } else | |
440 | dev->irq = mite_irq(devpriv->mite); | |
441 | ||
442 | printk("\n"); | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
da91b269 | 447 | static int ni6527_detach(struct comedi_device *dev) |
ef2ccffb DS |
448 | { |
449 | if (devpriv && devpriv->mite && devpriv->mite->daq_io_addr) { | |
450 | writeb(0x00, | |
451 | devpriv->mite->daq_io_addr + Master_Interrupt_Control); | |
452 | } | |
453 | ||
454 | if (dev->irq) { | |
5f74ea14 | 455 | free_irq(dev->irq, dev); |
ef2ccffb DS |
456 | } |
457 | ||
458 | if (devpriv && devpriv->mite) { | |
459 | mite_unsetup(devpriv->mite); | |
460 | } | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
da91b269 | 465 | static int ni6527_find_device(struct comedi_device *dev, int bus, int slot) |
ef2ccffb DS |
466 | { |
467 | struct mite_struct *mite; | |
468 | int i; | |
469 | ||
470 | for (mite = mite_devices; mite; mite = mite->next) { | |
471 | if (mite->used) | |
472 | continue; | |
473 | if (bus || slot) { | |
474 | if (bus != mite->pcidev->bus->number || | |
475 | slot != PCI_SLOT(mite->pcidev->devfn)) | |
476 | continue; | |
477 | } | |
478 | for (i = 0; i < n_ni6527_boards; i++) { | |
479 | if (mite_device_id(mite) == ni6527_boards[i].dev_id) { | |
480 | dev->board_ptr = ni6527_boards + i; | |
481 | devpriv->mite = mite; | |
482 | return 0; | |
483 | } | |
484 | } | |
485 | } | |
486 | printk("no device found\n"); | |
487 | mite_list_devices(); | |
488 | return -EIO; | |
489 | } | |
490 | ||
491 | COMEDI_PCI_INITCLEANUP(driver_ni6527, ni6527_pci_table); |