3 * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module.
4 * Project manager: Eric Stolz
9 * Tel: +19(0)7223/9493-0
10 * Fax: +49(0)7223/9493-92
11 * http://www.addi-data.com
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * You should also find the complete GPL in the COPYING file accompanying
32 #include <linux/pci.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
36 #include "../comedidev.h"
37 #include "comedi_fc.h"
38 #include "amcc_s5933.h"
41 * PCI bar 1 register I/O map
43 #define APCI3501_AO_CTRL_STATUS_REG 0x00
44 #define APCI3501_AO_CTRL_BIPOLAR (1 << 0)
45 #define APCI3501_AO_STATUS_READY (1 << 8)
46 #define APCI3501_AO_DATA_REG 0x04
47 #define APCI3501_AO_DATA_CHAN(x) ((x) << 0)
48 #define APCI3501_AO_DATA_VAL(x) ((x) << 8)
49 #define APCI3501_AO_DATA_BIPOLAR (1 << 31)
50 #define APCI3501_AO_TRIG_SCS_REG 0x08
51 #define APCI3501_TIMER_SYNC_REG 0x20
52 #define APCI3501_TIMER_RELOAD_REG 0x24
53 #define APCI3501_TIMER_TIMEBASE_REG 0x28
54 #define APCI3501_TIMER_CTRL_REG 0x2c
55 #define APCI3501_TIMER_STATUS_REG 0x30
56 #define APCI3501_TIMER_IRQ_REG 0x34
57 #define APCI3501_TIMER_WARN_RELOAD_REG 0x38
58 #define APCI3501_TIMER_WARN_TIMEBASE_REG 0x3c
59 #define APCI3501_DO_REG 0x40
60 #define APCI3501_DI_REG 0x50
65 #define NVRAM_USER_DATA_START 0x100
67 #define NVCMD_BEGIN_READ (0x7 << 5)
68 #define NVCMD_LOAD_LOW (0x4 << 5)
69 #define NVCMD_LOAD_HIGH (0x5 << 5)
72 * Function types stored in the eeprom
74 #define EEPROM_DIGITALINPUT 0
75 #define EEPROM_DIGITALOUTPUT 1
76 #define EEPROM_ANALOGINPUT 2
77 #define EEPROM_ANALOGOUTPUT 3
78 #define EEPROM_TIMER 4
79 #define EEPROM_WATCHDOG 5
80 #define EEPROM_TIMER_WATCHDOG_COUNTER 10
82 struct apci3501_private {
84 struct task_struct *tsk_Current;
85 unsigned char b_TimerSelectMode;
88 static struct comedi_lrange apci3501_ao_range = {
95 static int apci3501_wait_for_dac(struct comedi_device *dev)
100 status = inl(dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
101 } while (!(status & APCI3501_AO_STATUS_READY));
106 static int apci3501_ao_insn_write(struct comedi_device *dev,
107 struct comedi_subdevice *s,
108 struct comedi_insn *insn,
111 unsigned int chan = CR_CHAN(insn->chanspec);
112 unsigned int range = CR_RANGE(insn->chanspec);
113 unsigned int val = 0;
118 * All analog output channels have the same output range.
119 * 14-bit bipolar: 0-10V
120 * 13-bit unipolar: +/-10V
121 * Changing the range of one channel changes all of them!
124 outl(0, dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
126 val |= APCI3501_AO_DATA_BIPOLAR;
127 outl(APCI3501_AO_CTRL_BIPOLAR,
128 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
131 val |= APCI3501_AO_DATA_CHAN(chan);
133 for (i = 0; i < insn->n; i++) {
135 if (data[i] > 0x1fff) {
136 dev_err(dev->class_dev,
137 "Unipolar resolution is only 13-bits\n");
142 ret = apci3501_wait_for_dac(dev);
146 outl(val | APCI3501_AO_DATA_VAL(data[i]),
147 dev->iobase + APCI3501_AO_DATA_REG);
153 #include "addi-data/hwdrv_apci3501.c"
155 static int apci3501_di_insn_bits(struct comedi_device *dev,
156 struct comedi_subdevice *s,
157 struct comedi_insn *insn,
160 data[1] = inl(dev->iobase + APCI3501_DI_REG) & 0x3;
165 static int apci3501_do_insn_bits(struct comedi_device *dev,
166 struct comedi_subdevice *s,
167 struct comedi_insn *insn,
170 unsigned int mask = data[0];
171 unsigned int bits = data[1];
173 s->state = inl(dev->iobase + APCI3501_DO_REG);
176 s->state |= (bits & mask);
178 outl(s->state, dev->iobase + APCI3501_DO_REG);
186 static void apci3501_eeprom_wait(unsigned long iobase)
191 val = inb(iobase + AMCC_OP_REG_MCSR_NVCMD);
192 } while (val & 0x80);
195 static unsigned short apci3501_eeprom_readw(unsigned long iobase,
198 unsigned short val = 0;
202 /* Add the offset to the start of the user data */
203 addr += NVRAM_USER_DATA_START;
205 for (i = 0; i < 2; i++) {
206 /* Load the low 8 bit address */
207 outb(NVCMD_LOAD_LOW, iobase + AMCC_OP_REG_MCSR_NVCMD);
208 apci3501_eeprom_wait(iobase);
209 outb((addr + i) & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
210 apci3501_eeprom_wait(iobase);
212 /* Load the high 8 bit address */
213 outb(NVCMD_LOAD_HIGH, iobase + AMCC_OP_REG_MCSR_NVCMD);
214 apci3501_eeprom_wait(iobase);
215 outb(((addr + i) >> 8) & 0xff,
216 iobase + AMCC_OP_REG_MCSR_NVDATA);
217 apci3501_eeprom_wait(iobase);
219 /* Read the eeprom data byte */
220 outb(NVCMD_BEGIN_READ, iobase + AMCC_OP_REG_MCSR_NVCMD);
221 apci3501_eeprom_wait(iobase);
222 tmp = inb(iobase + AMCC_OP_REG_MCSR_NVDATA);
223 apci3501_eeprom_wait(iobase);
234 static int apci3501_eeprom_get_ao_n_chan(struct comedi_device *dev)
236 struct apci3501_private *devpriv = dev->private;
237 unsigned long iobase = devpriv->i_IobaseAmcc;
238 unsigned char nfuncs;
241 nfuncs = apci3501_eeprom_readw(iobase, 10) & 0xff;
243 /* Read functionality details */
244 for (i = 0; i < nfuncs; i++) {
245 unsigned short offset = i * 4;
250 func = apci3501_eeprom_readw(iobase, 12 + offset) & 0x3f;
251 addr = apci3501_eeprom_readw(iobase, 14 + offset);
253 if (func == EEPROM_ANALOGOUTPUT) {
254 val = apci3501_eeprom_readw(iobase, addr + 10);
255 return (val >> 4) & 0x3ff;
261 static int apci3501_eeprom_insn_read(struct comedi_device *dev,
262 struct comedi_subdevice *s,
263 struct comedi_insn *insn,
266 struct apci3501_private *devpriv = dev->private;
267 unsigned short addr = CR_CHAN(insn->chanspec);
269 data[0] = apci3501_eeprom_readw(devpriv->i_IobaseAmcc, 2 * addr);
274 static irqreturn_t apci3501_interrupt(int irq, void *d)
276 struct comedi_device *dev = d;
277 struct apci3501_private *devpriv = dev->private;
278 unsigned int ui_Timer_AOWatchdog;
279 unsigned long ul_Command1;
282 /* Disable Interrupt */
283 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
284 ul_Command1 = (ul_Command1 & 0xFFFFF9FDul);
285 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
287 ui_Timer_AOWatchdog = inl(dev->iobase + APCI3501_TIMER_IRQ_REG) & 0x1;
288 if ((!ui_Timer_AOWatchdog)) {
289 comedi_error(dev, "IRQ from unknown source");
293 /* Enable Interrupt Send a signal to from kernel to user space */
294 send_sig(SIGIO, devpriv->tsk_Current, 0);
295 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
296 ul_Command1 = ((ul_Command1 & 0xFFFFF9FDul) | 1 << 1);
297 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
298 i_temp = inl(dev->iobase + APCI3501_TIMER_STATUS_REG) & 0x1;
303 static int apci3501_reset(struct comedi_device *dev)
309 /* Reset all digital outputs to "0" */
310 outl(0x0, dev->iobase + APCI3501_DO_REG);
312 /* Default all analog outputs to 0V (bipolar) */
313 outl(APCI3501_AO_CTRL_BIPOLAR,
314 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
315 val = APCI3501_AO_DATA_BIPOLAR | APCI3501_AO_DATA_VAL(0);
317 /* Set all analog output channels */
318 for (chan = 0; chan < 8; chan++) {
319 ret = apci3501_wait_for_dac(dev);
321 dev_warn(dev->class_dev,
322 "%s: DAC not-ready for channel %i\n",
325 outl(val | APCI3501_AO_DATA_CHAN(chan),
326 dev->iobase + APCI3501_AO_DATA_REG);
333 static int apci3501_auto_attach(struct comedi_device *dev,
334 unsigned long context_unused)
336 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
337 struct apci3501_private *devpriv;
338 struct comedi_subdevice *s;
342 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
345 dev->private = devpriv;
347 ret = comedi_pci_enable(dev);
351 dev->iobase = pci_resource_start(pcidev, 1);
352 devpriv->i_IobaseAmcc = pci_resource_start(pcidev, 0);
354 ao_n_chan = apci3501_eeprom_get_ao_n_chan(dev);
356 if (pcidev->irq > 0) {
357 ret = request_irq(pcidev->irq, apci3501_interrupt, IRQF_SHARED,
358 dev->board_name, dev);
360 dev->irq = pcidev->irq;
363 ret = comedi_alloc_subdevices(dev, 5);
367 /* Initialize the analog output subdevice */
368 s = &dev->subdevices[0];
370 s->type = COMEDI_SUBD_AO;
371 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
372 s->n_chan = ao_n_chan;
374 s->range_table = &apci3501_ao_range;
375 s->insn_write = apci3501_ao_insn_write;
377 s->type = COMEDI_SUBD_UNUSED;
380 /* Initialize the digital input subdevice */
381 s = &dev->subdevices[1];
382 s->type = COMEDI_SUBD_DI;
383 s->subdev_flags = SDF_READABLE;
386 s->range_table = &range_digital;
387 s->insn_bits = apci3501_di_insn_bits;
389 /* Initialize the digital output subdevice */
390 s = &dev->subdevices[2];
391 s->type = COMEDI_SUBD_DO;
392 s->subdev_flags = SDF_WRITEABLE;
395 s->range_table = &range_digital;
396 s->insn_bits = apci3501_do_insn_bits;
398 /* Initialize the timer/watchdog subdevice */
399 s = &dev->subdevices[3];
400 s->type = COMEDI_SUBD_TIMER;
401 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
405 s->range_table = &range_digital;
406 s->insn_write = i_APCI3501_StartStopWriteTimerCounterWatchdog;
407 s->insn_read = i_APCI3501_ReadTimerCounterWatchdog;
408 s->insn_config = i_APCI3501_ConfigTimerCounterWatchdog;
410 /* Initialize the eeprom subdevice */
411 s = &dev->subdevices[4];
412 s->type = COMEDI_SUBD_MEMORY;
413 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
416 s->insn_read = apci3501_eeprom_insn_read;
422 static void apci3501_detach(struct comedi_device *dev)
427 free_irq(dev->irq, dev);
428 comedi_pci_disable(dev);
431 static struct comedi_driver apci3501_driver = {
432 .driver_name = "addi_apci_3501",
433 .module = THIS_MODULE,
434 .auto_attach = apci3501_auto_attach,
435 .detach = apci3501_detach,
438 static int apci3501_pci_probe(struct pci_dev *dev,
439 const struct pci_device_id *id)
441 return comedi_pci_auto_config(dev, &apci3501_driver, id->driver_data);
444 static DEFINE_PCI_DEVICE_TABLE(apci3501_pci_table) = {
445 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x3001) },
448 MODULE_DEVICE_TABLE(pci, apci3501_pci_table);
450 static struct pci_driver apci3501_pci_driver = {
451 .name = "addi_apci_3501",
452 .id_table = apci3501_pci_table,
453 .probe = apci3501_pci_probe,
454 .remove = comedi_pci_auto_unconfig,
456 module_comedi_pci_driver(apci3501_driver, apci3501_pci_driver);
458 MODULE_DESCRIPTION("ADDI-DATA APCI-3501 Analog output board");
459 MODULE_AUTHOR("Comedi http://www.comedi.org");
460 MODULE_LICENSE("GPL");