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
26 * Driver: addi_apci_3501
27 * Description: ADDI-DATA APCI-3501 Analog output board
28 * Devices: [ADDI-DATA] APCI-3501 (addi_apci_3501)
29 * Author: H Hartley Sweeten <hsweeten@visionengravers.com>
30 * Updated: Mon, 20 Jun 2016 10:57:01 -0700
33 * Configuration Options: not applicable, uses comedi PCI auto config
35 * This board has the following features:
36 * - 4 or 8 analog output channels
37 * - 2 optically isolated digital inputs
38 * - 2 optically isolated digital outputs
39 * - 1 12-bit watchdog/timer
41 * There are 2 versions of the APCI-3501:
42 * - APCI-3501-4 4 analog output channels
43 * - APCI-3501-8 8 analog output channels
45 * These boards use the same PCI Vendor/Device IDs. The number of output
46 * channels used by this driver is determined by reading the EEPROM on
49 * The watchdog/timer subdevice is not currently supported.
52 #include <linux/module.h>
54 #include "../comedi_pci.h"
55 #include "amcc_s5933.h"
58 * PCI bar 1 register I/O map
60 #define APCI3501_AO_CTRL_STATUS_REG 0x00
61 #define APCI3501_AO_CTRL_BIPOLAR BIT(0)
62 #define APCI3501_AO_STATUS_READY BIT(8)
63 #define APCI3501_AO_DATA_REG 0x04
64 #define APCI3501_AO_DATA_CHAN(x) ((x) << 0)
65 #define APCI3501_AO_DATA_VAL(x) ((x) << 8)
66 #define APCI3501_AO_DATA_BIPOLAR BIT(31)
67 #define APCI3501_AO_TRIG_SCS_REG 0x08
68 #define APCI3501_TIMER_BASE 0x20
69 #define APCI3501_DO_REG 0x40
70 #define APCI3501_DI_REG 0x50
75 #define NVRAM_USER_DATA_START 0x100
77 #define NVCMD_BEGIN_READ (0x7 << 5)
78 #define NVCMD_LOAD_LOW (0x4 << 5)
79 #define NVCMD_LOAD_HIGH (0x5 << 5)
82 * Function types stored in the eeprom
84 #define EEPROM_DIGITALINPUT 0
85 #define EEPROM_DIGITALOUTPUT 1
86 #define EEPROM_ANALOGINPUT 2
87 #define EEPROM_ANALOGOUTPUT 3
88 #define EEPROM_TIMER 4
89 #define EEPROM_WATCHDOG 5
90 #define EEPROM_TIMER_WATCHDOG_COUNTER 10
92 struct apci3501_private {
94 unsigned char timer_mode;
97 static const struct comedi_lrange apci3501_ao_range = {
104 static int apci3501_wait_for_dac(struct comedi_device *dev)
109 status = inl(dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
110 } while (!(status & APCI3501_AO_STATUS_READY));
115 static int apci3501_ao_insn_write(struct comedi_device *dev,
116 struct comedi_subdevice *s,
117 struct comedi_insn *insn,
120 unsigned int chan = CR_CHAN(insn->chanspec);
121 unsigned int range = CR_RANGE(insn->chanspec);
122 unsigned int cfg = APCI3501_AO_DATA_CHAN(chan);
127 * All analog output channels have the same output range.
128 * 14-bit bipolar: 0-10V
129 * 13-bit unipolar: +/-10V
130 * Changing the range of one channel changes all of them!
133 outl(0, dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
135 cfg |= APCI3501_AO_DATA_BIPOLAR;
136 outl(APCI3501_AO_CTRL_BIPOLAR,
137 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
140 for (i = 0; i < insn->n; i++) {
141 unsigned int val = data[i];
144 if (data[i] > 0x1fff) {
145 dev_err(dev->class_dev,
146 "Unipolar resolution is only 13-bits\n");
151 ret = apci3501_wait_for_dac(dev);
155 outl(cfg | APCI3501_AO_DATA_VAL(val),
156 dev->iobase + APCI3501_AO_DATA_REG);
158 s->readback[chan] = val;
164 static int apci3501_di_insn_bits(struct comedi_device *dev,
165 struct comedi_subdevice *s,
166 struct comedi_insn *insn,
169 data[1] = inl(dev->iobase + APCI3501_DI_REG) & 0x3;
174 static int apci3501_do_insn_bits(struct comedi_device *dev,
175 struct comedi_subdevice *s,
176 struct comedi_insn *insn,
179 s->state = inl(dev->iobase + APCI3501_DO_REG);
181 if (comedi_dio_update_state(s, data))
182 outl(s->state, dev->iobase + APCI3501_DO_REG);
189 static void apci3501_eeprom_wait(unsigned long iobase)
194 val = inb(iobase + AMCC_OP_REG_MCSR_NVCMD);
195 } while (val & 0x80);
198 static unsigned short apci3501_eeprom_readw(unsigned long iobase,
201 unsigned short val = 0;
205 /* Add the offset to the start of the user data */
206 addr += NVRAM_USER_DATA_START;
208 for (i = 0; i < 2; i++) {
209 /* Load the low 8 bit address */
210 outb(NVCMD_LOAD_LOW, iobase + AMCC_OP_REG_MCSR_NVCMD);
211 apci3501_eeprom_wait(iobase);
212 outb((addr + i) & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
213 apci3501_eeprom_wait(iobase);
215 /* Load the high 8 bit address */
216 outb(NVCMD_LOAD_HIGH, iobase + AMCC_OP_REG_MCSR_NVCMD);
217 apci3501_eeprom_wait(iobase);
218 outb(((addr + i) >> 8) & 0xff,
219 iobase + AMCC_OP_REG_MCSR_NVDATA);
220 apci3501_eeprom_wait(iobase);
222 /* Read the eeprom data byte */
223 outb(NVCMD_BEGIN_READ, iobase + AMCC_OP_REG_MCSR_NVCMD);
224 apci3501_eeprom_wait(iobase);
225 tmp = inb(iobase + AMCC_OP_REG_MCSR_NVDATA);
226 apci3501_eeprom_wait(iobase);
237 static int apci3501_eeprom_get_ao_n_chan(struct comedi_device *dev)
239 struct apci3501_private *devpriv = dev->private;
240 unsigned char nfuncs;
243 nfuncs = apci3501_eeprom_readw(devpriv->amcc, 10) & 0xff;
245 /* Read functionality details */
246 for (i = 0; i < nfuncs; i++) {
247 unsigned short offset = i * 4;
252 func = apci3501_eeprom_readw(devpriv->amcc, 12 + offset) & 0x3f;
253 addr = apci3501_eeprom_readw(devpriv->amcc, 14 + offset);
255 if (func == EEPROM_ANALOGOUTPUT) {
256 val = apci3501_eeprom_readw(devpriv->amcc, addr + 10);
257 return (val >> 4) & 0x3ff;
263 static int apci3501_eeprom_insn_read(struct comedi_device *dev,
264 struct comedi_subdevice *s,
265 struct comedi_insn *insn,
268 struct apci3501_private *devpriv = dev->private;
269 unsigned short addr = CR_CHAN(insn->chanspec);
271 data[0] = apci3501_eeprom_readw(devpriv->amcc, 2 * addr);
276 static int apci3501_reset(struct comedi_device *dev)
282 /* Reset all digital outputs to "0" */
283 outl(0x0, dev->iobase + APCI3501_DO_REG);
285 /* Default all analog outputs to 0V (bipolar) */
286 outl(APCI3501_AO_CTRL_BIPOLAR,
287 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
288 val = APCI3501_AO_DATA_BIPOLAR | APCI3501_AO_DATA_VAL(0);
290 /* Set all analog output channels */
291 for (chan = 0; chan < 8; chan++) {
292 ret = apci3501_wait_for_dac(dev);
294 dev_warn(dev->class_dev,
295 "%s: DAC not-ready for channel %i\n",
298 outl(val | APCI3501_AO_DATA_CHAN(chan),
299 dev->iobase + APCI3501_AO_DATA_REG);
306 static int apci3501_auto_attach(struct comedi_device *dev,
307 unsigned long context_unused)
309 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
310 struct apci3501_private *devpriv;
311 struct comedi_subdevice *s;
315 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
319 ret = comedi_pci_enable(dev);
323 devpriv->amcc = pci_resource_start(pcidev, 0);
324 dev->iobase = pci_resource_start(pcidev, 1);
326 ao_n_chan = apci3501_eeprom_get_ao_n_chan(dev);
328 ret = comedi_alloc_subdevices(dev, 5);
332 /* Initialize the analog output subdevice */
333 s = &dev->subdevices[0];
335 s->type = COMEDI_SUBD_AO;
336 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
337 s->n_chan = ao_n_chan;
339 s->range_table = &apci3501_ao_range;
340 s->insn_write = apci3501_ao_insn_write;
342 ret = comedi_alloc_subdev_readback(s);
346 s->type = COMEDI_SUBD_UNUSED;
349 /* Initialize the digital input subdevice */
350 s = &dev->subdevices[1];
351 s->type = COMEDI_SUBD_DI;
352 s->subdev_flags = SDF_READABLE;
355 s->range_table = &range_digital;
356 s->insn_bits = apci3501_di_insn_bits;
358 /* Initialize the digital output subdevice */
359 s = &dev->subdevices[2];
360 s->type = COMEDI_SUBD_DO;
361 s->subdev_flags = SDF_WRITABLE;
364 s->range_table = &range_digital;
365 s->insn_bits = apci3501_do_insn_bits;
367 /* Timer/Watchdog subdevice */
368 s = &dev->subdevices[3];
369 s->type = COMEDI_SUBD_UNUSED;
371 /* Initialize the eeprom subdevice */
372 s = &dev->subdevices[4];
373 s->type = COMEDI_SUBD_MEMORY;
374 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
377 s->insn_read = apci3501_eeprom_insn_read;
383 static void apci3501_detach(struct comedi_device *dev)
387 comedi_pci_detach(dev);
390 static struct comedi_driver apci3501_driver = {
391 .driver_name = "addi_apci_3501",
392 .module = THIS_MODULE,
393 .auto_attach = apci3501_auto_attach,
394 .detach = apci3501_detach,
397 static int apci3501_pci_probe(struct pci_dev *dev,
398 const struct pci_device_id *id)
400 return comedi_pci_auto_config(dev, &apci3501_driver, id->driver_data);
403 static const struct pci_device_id apci3501_pci_table[] = {
404 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x3001) },
407 MODULE_DEVICE_TABLE(pci, apci3501_pci_table);
409 static struct pci_driver apci3501_pci_driver = {
410 .name = "addi_apci_3501",
411 .id_table = apci3501_pci_table,
412 .probe = apci3501_pci_probe,
413 .remove = comedi_pci_auto_unconfig,
415 module_comedi_pci_driver(apci3501_driver, apci3501_pci_driver);
417 MODULE_DESCRIPTION("ADDI-DATA APCI-3501 Analog output board");
418 MODULE_AUTHOR("Comedi http://www.comedi.org");
419 MODULE_LICENSE("GPL");