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
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
30 #include "../comedidev.h"
31 #include "comedi_fc.h"
32 #include "amcc_s5933.h"
35 * PCI bar 1 register I/O map
37 #define APCI3501_AO_CTRL_STATUS_REG 0x00
38 #define APCI3501_AO_CTRL_BIPOLAR (1 << 0)
39 #define APCI3501_AO_STATUS_READY (1 << 8)
40 #define APCI3501_AO_DATA_REG 0x04
41 #define APCI3501_AO_DATA_CHAN(x) ((x) << 0)
42 #define APCI3501_AO_DATA_VAL(x) ((x) << 8)
43 #define APCI3501_AO_DATA_BIPOLAR (1 << 31)
44 #define APCI3501_AO_TRIG_SCS_REG 0x08
45 #define APCI3501_TIMER_SYNC_REG 0x20
46 #define APCI3501_TIMER_RELOAD_REG 0x24
47 #define APCI3501_TIMER_TIMEBASE_REG 0x28
48 #define APCI3501_TIMER_CTRL_REG 0x2c
49 #define APCI3501_TIMER_STATUS_REG 0x30
50 #define APCI3501_TIMER_IRQ_REG 0x34
51 #define APCI3501_TIMER_WARN_RELOAD_REG 0x38
52 #define APCI3501_TIMER_WARN_TIMEBASE_REG 0x3c
53 #define APCI3501_DO_REG 0x40
54 #define APCI3501_DI_REG 0x50
59 #define NVRAM_USER_DATA_START 0x100
61 #define NVCMD_BEGIN_READ (0x7 << 5)
62 #define NVCMD_LOAD_LOW (0x4 << 5)
63 #define NVCMD_LOAD_HIGH (0x5 << 5)
66 * Function types stored in the eeprom
68 #define EEPROM_DIGITALINPUT 0
69 #define EEPROM_DIGITALOUTPUT 1
70 #define EEPROM_ANALOGINPUT 2
71 #define EEPROM_ANALOGOUTPUT 3
72 #define EEPROM_TIMER 4
73 #define EEPROM_WATCHDOG 5
74 #define EEPROM_TIMER_WATCHDOG_COUNTER 10
76 struct apci3501_private {
78 struct task_struct *tsk_Current;
79 unsigned char b_TimerSelectMode;
82 static struct comedi_lrange apci3501_ao_range = {
89 static int apci3501_wait_for_dac(struct comedi_device *dev)
94 status = inl(dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
95 } while (!(status & APCI3501_AO_STATUS_READY));
100 static int apci3501_ao_insn_write(struct comedi_device *dev,
101 struct comedi_subdevice *s,
102 struct comedi_insn *insn,
105 unsigned int chan = CR_CHAN(insn->chanspec);
106 unsigned int range = CR_RANGE(insn->chanspec);
107 unsigned int val = 0;
112 * All analog output channels have the same output range.
113 * 14-bit bipolar: 0-10V
114 * 13-bit unipolar: +/-10V
115 * Changing the range of one channel changes all of them!
118 outl(0, dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
120 val |= APCI3501_AO_DATA_BIPOLAR;
121 outl(APCI3501_AO_CTRL_BIPOLAR,
122 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
125 val |= APCI3501_AO_DATA_CHAN(chan);
127 for (i = 0; i < insn->n; i++) {
129 if (data[i] > 0x1fff) {
130 dev_err(dev->class_dev,
131 "Unipolar resolution is only 13-bits\n");
136 ret = apci3501_wait_for_dac(dev);
140 outl(val | APCI3501_AO_DATA_VAL(data[i]),
141 dev->iobase + APCI3501_AO_DATA_REG);
147 #include "addi-data/hwdrv_apci3501.c"
149 static int apci3501_di_insn_bits(struct comedi_device *dev,
150 struct comedi_subdevice *s,
151 struct comedi_insn *insn,
154 data[1] = inl(dev->iobase + APCI3501_DI_REG) & 0x3;
159 static int apci3501_do_insn_bits(struct comedi_device *dev,
160 struct comedi_subdevice *s,
161 struct comedi_insn *insn,
164 s->state = inl(dev->iobase + APCI3501_DO_REG);
166 if (comedi_dio_update_state(s, data))
167 outl(s->state, dev->iobase + APCI3501_DO_REG);
174 static void apci3501_eeprom_wait(unsigned long iobase)
179 val = inb(iobase + AMCC_OP_REG_MCSR_NVCMD);
180 } while (val & 0x80);
183 static unsigned short apci3501_eeprom_readw(unsigned long iobase,
186 unsigned short val = 0;
190 /* Add the offset to the start of the user data */
191 addr += NVRAM_USER_DATA_START;
193 for (i = 0; i < 2; i++) {
194 /* Load the low 8 bit address */
195 outb(NVCMD_LOAD_LOW, iobase + AMCC_OP_REG_MCSR_NVCMD);
196 apci3501_eeprom_wait(iobase);
197 outb((addr + i) & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
198 apci3501_eeprom_wait(iobase);
200 /* Load the high 8 bit address */
201 outb(NVCMD_LOAD_HIGH, iobase + AMCC_OP_REG_MCSR_NVCMD);
202 apci3501_eeprom_wait(iobase);
203 outb(((addr + i) >> 8) & 0xff,
204 iobase + AMCC_OP_REG_MCSR_NVDATA);
205 apci3501_eeprom_wait(iobase);
207 /* Read the eeprom data byte */
208 outb(NVCMD_BEGIN_READ, iobase + AMCC_OP_REG_MCSR_NVCMD);
209 apci3501_eeprom_wait(iobase);
210 tmp = inb(iobase + AMCC_OP_REG_MCSR_NVDATA);
211 apci3501_eeprom_wait(iobase);
222 static int apci3501_eeprom_get_ao_n_chan(struct comedi_device *dev)
224 struct apci3501_private *devpriv = dev->private;
225 unsigned long iobase = devpriv->i_IobaseAmcc;
226 unsigned char nfuncs;
229 nfuncs = apci3501_eeprom_readw(iobase, 10) & 0xff;
231 /* Read functionality details */
232 for (i = 0; i < nfuncs; i++) {
233 unsigned short offset = i * 4;
238 func = apci3501_eeprom_readw(iobase, 12 + offset) & 0x3f;
239 addr = apci3501_eeprom_readw(iobase, 14 + offset);
241 if (func == EEPROM_ANALOGOUTPUT) {
242 val = apci3501_eeprom_readw(iobase, addr + 10);
243 return (val >> 4) & 0x3ff;
249 static int apci3501_eeprom_insn_read(struct comedi_device *dev,
250 struct comedi_subdevice *s,
251 struct comedi_insn *insn,
254 struct apci3501_private *devpriv = dev->private;
255 unsigned short addr = CR_CHAN(insn->chanspec);
257 data[0] = apci3501_eeprom_readw(devpriv->i_IobaseAmcc, 2 * addr);
262 static irqreturn_t apci3501_interrupt(int irq, void *d)
264 struct comedi_device *dev = d;
265 struct apci3501_private *devpriv = dev->private;
266 unsigned int ui_Timer_AOWatchdog;
267 unsigned long ul_Command1;
270 /* Disable Interrupt */
271 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
272 ul_Command1 = (ul_Command1 & 0xFFFFF9FDul);
273 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
275 ui_Timer_AOWatchdog = inl(dev->iobase + APCI3501_TIMER_IRQ_REG) & 0x1;
276 if ((!ui_Timer_AOWatchdog)) {
277 comedi_error(dev, "IRQ from unknown source");
281 /* Enable Interrupt Send a signal to from kernel to user space */
282 send_sig(SIGIO, devpriv->tsk_Current, 0);
283 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
284 ul_Command1 = ((ul_Command1 & 0xFFFFF9FDul) | 1 << 1);
285 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
286 i_temp = inl(dev->iobase + APCI3501_TIMER_STATUS_REG) & 0x1;
291 static int apci3501_reset(struct comedi_device *dev)
297 /* Reset all digital outputs to "0" */
298 outl(0x0, dev->iobase + APCI3501_DO_REG);
300 /* Default all analog outputs to 0V (bipolar) */
301 outl(APCI3501_AO_CTRL_BIPOLAR,
302 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
303 val = APCI3501_AO_DATA_BIPOLAR | APCI3501_AO_DATA_VAL(0);
305 /* Set all analog output channels */
306 for (chan = 0; chan < 8; chan++) {
307 ret = apci3501_wait_for_dac(dev);
309 dev_warn(dev->class_dev,
310 "%s: DAC not-ready for channel %i\n",
313 outl(val | APCI3501_AO_DATA_CHAN(chan),
314 dev->iobase + APCI3501_AO_DATA_REG);
321 static int apci3501_auto_attach(struct comedi_device *dev,
322 unsigned long context_unused)
324 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
325 struct apci3501_private *devpriv;
326 struct comedi_subdevice *s;
330 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
334 ret = comedi_pci_enable(dev);
338 dev->iobase = pci_resource_start(pcidev, 1);
339 devpriv->i_IobaseAmcc = pci_resource_start(pcidev, 0);
341 ao_n_chan = apci3501_eeprom_get_ao_n_chan(dev);
343 if (pcidev->irq > 0) {
344 ret = request_irq(pcidev->irq, apci3501_interrupt, IRQF_SHARED,
345 dev->board_name, dev);
347 dev->irq = pcidev->irq;
350 ret = comedi_alloc_subdevices(dev, 5);
354 /* Initialize the analog output subdevice */
355 s = &dev->subdevices[0];
357 s->type = COMEDI_SUBD_AO;
358 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
359 s->n_chan = ao_n_chan;
361 s->range_table = &apci3501_ao_range;
362 s->insn_write = apci3501_ao_insn_write;
364 s->type = COMEDI_SUBD_UNUSED;
367 /* Initialize the digital input subdevice */
368 s = &dev->subdevices[1];
369 s->type = COMEDI_SUBD_DI;
370 s->subdev_flags = SDF_READABLE;
373 s->range_table = &range_digital;
374 s->insn_bits = apci3501_di_insn_bits;
376 /* Initialize the digital output subdevice */
377 s = &dev->subdevices[2];
378 s->type = COMEDI_SUBD_DO;
379 s->subdev_flags = SDF_WRITEABLE;
382 s->range_table = &range_digital;
383 s->insn_bits = apci3501_do_insn_bits;
385 /* Initialize the timer/watchdog subdevice */
386 s = &dev->subdevices[3];
387 s->type = COMEDI_SUBD_TIMER;
388 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
392 s->range_table = &range_digital;
393 s->insn_write = i_APCI3501_StartStopWriteTimerCounterWatchdog;
394 s->insn_read = i_APCI3501_ReadTimerCounterWatchdog;
395 s->insn_config = i_APCI3501_ConfigTimerCounterWatchdog;
397 /* Initialize the eeprom subdevice */
398 s = &dev->subdevices[4];
399 s->type = COMEDI_SUBD_MEMORY;
400 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
403 s->insn_read = apci3501_eeprom_insn_read;
409 static void apci3501_detach(struct comedi_device *dev)
414 free_irq(dev->irq, dev);
415 comedi_pci_disable(dev);
418 static struct comedi_driver apci3501_driver = {
419 .driver_name = "addi_apci_3501",
420 .module = THIS_MODULE,
421 .auto_attach = apci3501_auto_attach,
422 .detach = apci3501_detach,
425 static int apci3501_pci_probe(struct pci_dev *dev,
426 const struct pci_device_id *id)
428 return comedi_pci_auto_config(dev, &apci3501_driver, id->driver_data);
431 static DEFINE_PCI_DEVICE_TABLE(apci3501_pci_table) = {
432 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x3001) },
435 MODULE_DEVICE_TABLE(pci, apci3501_pci_table);
437 static struct pci_driver apci3501_pci_driver = {
438 .name = "addi_apci_3501",
439 .id_table = apci3501_pci_table,
440 .probe = apci3501_pci_probe,
441 .remove = comedi_pci_auto_unconfig,
443 module_comedi_pci_driver(apci3501_driver, apci3501_pci_driver);
445 MODULE_DESCRIPTION("ADDI-DATA APCI-3501 Analog output board");
446 MODULE_AUTHOR("Comedi http://www.comedi.org");
447 MODULE_LICENSE("GPL");