1 /*******************************************************************************
2 comedi/drivers/pci1723.c
4 COMEDI - Linux Control and Measurement Device Interface
5 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *******************************************************************************/
24 Description: Advantech PCI-1723
25 Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
26 Devices: [Advantech] PCI-1723 (adv_pci1723)
27 Updated: Mon, 14 Apr 2008 15:12:56 +0100
30 Configuration Options:
31 [0] - PCI bus of device (optional)
32 [1] - PCI slot of device (optional)
34 If bus/slot is not specified, the first supported
35 PCI device found will be used.
37 Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
39 Subdevice 1 is 16-channel DIO. The channels are configurable as input or
40 output in 2 groups (0 to 7, 8 to 15). Configuring any channel implicitly
41 configures all channels in the same group.
45 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA, 4 to 20 mA).
46 2. Read the initial ranges and values of the AO subdevice at start-up instead
47 of reinitializing them.
48 3. Implement calibration.
51 #include "../comedidev.h"
53 #define PCI_VENDOR_ID_ADVANTECH 0x13fe /* Advantech PCI vendor ID */
55 /* hardware types of the cards */
56 #define TYPE_PCI1723 0
58 #define IORANGE_1723 0x2A
60 /* all the registers for the pci1723 board */
61 #define PCI1723_DA(N) ((N)<<1) /* W: D/A register N (0 to 7) */
63 #define PCI1723_SYN_SET 0x12 /* synchronized set register */
64 #define PCI1723_ALL_CHNNELE_SYN_STROBE 0x12
65 /* synchronized status register */
67 #define PCI1723_RANGE_CALIBRATION_MODE 0x14
68 /* range and calibration mode */
69 #define PCI1723_RANGE_CALIBRATION_STATUS 0x14
70 /* range and calibration status */
72 #define PCI1723_CONTROL_CMD_CALIBRATION_FUN 0x16
74 * SADC control command for
75 * calibration function
77 #define PCI1723_STATUS_CMD_CALIBRATION_FUN 0x16
79 * SADC control status for
80 * calibration function
83 #define PCI1723_CALIBRATION_PARA_STROBE 0x18
84 /* Calibration parameter strobe */
86 #define PCI1723_DIGITAL_IO_PORT_SET 0x1A /* Digital I/O port setting */
87 #define PCI1723_DIGITAL_IO_PORT_MODE 0x1A /* Digital I/O port mode */
89 #define PCI1723_WRITE_DIGITAL_OUTPUT_CMD 0x1C
90 /* Write digital output command */
91 #define PCI1723_READ_DIGITAL_INPUT_DATA 0x1C /* Read digital input data */
93 #define PCI1723_WRITE_CAL_CMD 0x1E /* Write calibration command */
94 #define PCI1723_READ_CAL_STATUS 0x1E /* Read calibration status */
96 #define PCI1723_SYN_STROBE 0x20 /* Synchronized strobe */
98 #define PCI1723_RESET_ALL_CHN_STROBE 0x22
99 /* Reset all D/A channels strobe */
101 #define PCI1723_RESET_CAL_CONTROL_STROBE 0x24
103 * Reset the calibration
107 #define PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE 0x26
109 * Change D/A channels output
113 #define PCI1723_SELECT_CALIBRATION 0x28 /* Select the calibration Ref_V */
115 /* static unsigned short pci_list_builded=0; =1 list of card is know */
117 static const struct comedi_lrange range_pci1723 = { 1, {
123 * Board descriptions for pci1723 boards.
125 struct pci1723_board {
127 int vendor_id; /* PCI vendor a device ID of card */
131 int n_aochan; /* num of D/A chans */
132 int n_diochan; /* num of DIO chans */
133 int ao_maxdata; /* resolution of D/A */
134 const struct comedi_lrange *rangelist_ao; /* rangelist for D/A */
137 static const struct pci1723_board boardtypes[] = {
140 .vendor_id = PCI_VENDOR_ID_ADVANTECH,
142 .iorange = IORANGE_1723,
143 .cardtype = TYPE_PCI1723,
146 .ao_maxdata = 0xffff,
147 .rangelist_ao = &range_pci1723,
151 /* This structure is for data unique to this hardware driver. */
152 struct pci1723_private {
153 int valid; /* card is usable; */
155 unsigned char da_range[8]; /* D/A output range for each channel */
157 short ao_data[8]; /* data output buffer */
160 /* The following macro to make it easy to access the private structure. */
161 #define devpriv ((struct pci1723_private *)dev->private)
163 #define this_board boardtypes
166 * The pci1723 card reset;
168 static int pci1723_reset(struct comedi_device *dev)
171 DPRINTK("adv_pci1723 EDBG: BGN: pci1723_reset(...)\n");
173 outw(0x01, dev->iobase + PCI1723_SYN_SET);
174 /* set synchronous output mode */
176 for (i = 0; i < 8; i++) {
177 /* set all outputs to 0V */
178 devpriv->ao_data[i] = 0x8000;
179 outw(devpriv->ao_data[i], dev->iobase + PCI1723_DA(i));
180 /* set all ranges to +/- 10V */
181 devpriv->da_range[i] = 0;
182 outw(((devpriv->da_range[i] << 4) | i),
183 PCI1723_RANGE_CALIBRATION_MODE);
186 outw(0, dev->iobase + PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE);
188 outw(0, dev->iobase + PCI1723_SYN_STROBE); /* update outputs */
190 /* set asynchronous output mode */
191 outw(0, dev->iobase + PCI1723_SYN_SET);
193 DPRINTK("adv_pci1723 EDBG: END: pci1723_reset(...)\n");
197 static int pci1723_insn_read_ao(struct comedi_device *dev,
198 struct comedi_subdevice *s,
199 struct comedi_insn *insn, unsigned int *data)
203 chan = CR_CHAN(insn->chanspec);
204 DPRINTK(" adv_PCI1723 DEBUG: pci1723_insn_read_ao() -----\n");
205 for (n = 0; n < insn->n; n++)
206 data[n] = devpriv->ao_data[chan];
214 static int pci1723_ao_write_winsn(struct comedi_device *dev,
215 struct comedi_subdevice *s,
216 struct comedi_insn *insn, unsigned int *data)
219 chan = CR_CHAN(insn->chanspec);
221 DPRINTK("PCI1723: the pci1723_ao_write_winsn() ------\n");
223 for (n = 0; n < insn->n; n++) {
225 devpriv->ao_data[chan] = data[n];
226 outw(data[n], dev->iobase + PCI1723_DA(chan));
233 digital i/o config/query
235 static int pci1723_dio_insn_config(struct comedi_device *dev,
236 struct comedi_subdevice *s,
237 struct comedi_insn *insn, unsigned int *data)
241 unsigned short dio_mode;
243 mask = 1 << CR_CHAN(insn->chanspec);
250 case INSN_CONFIG_DIO_INPUT:
253 case INSN_CONFIG_DIO_OUTPUT:
256 case INSN_CONFIG_DIO_QUERY:
257 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
263 /* update hardware DIO mode */
264 dio_mode = 0x0000; /* low byte output, high byte output */
265 if ((s->io_bits & 0x00FF) == 0)
266 dio_mode |= 0x0001; /* low byte input */
267 if ((s->io_bits & 0xFF00) == 0)
268 dio_mode |= 0x0002; /* high byte input */
269 outw(dio_mode, dev->iobase + PCI1723_DIGITAL_IO_PORT_SET);
274 digital i/o bits read/write
276 static int pci1723_dio_insn_bits(struct comedi_device *dev,
277 struct comedi_subdevice *s,
278 struct comedi_insn *insn, unsigned int *data)
281 s->state &= ~data[0];
282 s->state |= (data[0] & data[1]);
283 outw(s->state, dev->iobase + PCI1723_WRITE_DIGITAL_OUTPUT_CMD);
285 data[1] = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
289 static struct pci_dev *pci1723_find_pci_dev(struct comedi_device *dev,
290 struct comedi_devconfig *it)
292 struct pci_dev *pcidev = NULL;
293 int bus = it->options[0];
294 int slot = it->options[1];
296 for_each_pci_dev(pcidev) {
298 if (bus != pcidev->bus->number ||
299 slot != PCI_SLOT(pcidev->devfn))
302 if (pcidev->vendor != PCI_VENDOR_ID_ADVANTECH)
304 if (pci_is_enabled(pcidev))
308 dev_err(dev->class_dev,
309 "No supported board found! (req. bus %d, slot %d)\n",
314 static int pci1723_attach(struct comedi_device *dev,
315 struct comedi_devconfig *it)
317 struct pci_dev *pcidev;
318 struct comedi_subdevice *s;
319 int ret, subdev, n_subdevices;
321 printk(KERN_ERR "comedi%d: adv_pci1723: board=%s",
322 dev->minor, this_board->name);
324 ret = alloc_private(dev, sizeof(struct pci1723_private));
326 printk(" - Allocation failed!\n");
330 pcidev = pci1723_find_pci_dev(dev, it);
333 comedi_set_hw_dev(dev, &pcidev->dev);
335 ret = comedi_pci_enable(pcidev, "adv_pci1723");
339 dev->iobase = pci_resource_start(pcidev, 2);
341 dev->board_name = this_board->name;
345 if (this_board->n_aochan)
347 if (this_board->n_diochan)
350 ret = comedi_alloc_subdevices(dev, n_subdevices);
356 if (this_board->n_aochan) {
357 s = dev->subdevices + subdev;
358 dev->write_subdev = s;
359 s->type = COMEDI_SUBD_AO;
360 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
361 s->n_chan = this_board->n_aochan;
362 s->maxdata = this_board->ao_maxdata;
363 s->len_chanlist = this_board->n_aochan;
364 s->range_table = this_board->rangelist_ao;
366 s->insn_write = pci1723_ao_write_winsn;
367 s->insn_read = pci1723_insn_read_ao;
369 /* read DIO config */
370 switch (inw(dev->iobase + PCI1723_DIGITAL_IO_PORT_MODE)
372 case 0x00: /* low byte output, high byte output */
375 case 0x01: /* low byte input, high byte output */
378 case 0x02: /* low byte output, high byte input */
381 case 0x03: /* low byte input, high byte input */
385 /* read DIO port state */
386 s->state = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
391 if (this_board->n_diochan) {
392 s = dev->subdevices + subdev;
393 s->type = COMEDI_SUBD_DIO;
395 SDF_READABLE | SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
396 s->n_chan = this_board->n_diochan;
398 s->len_chanlist = this_board->n_diochan;
399 s->range_table = &range_digital;
400 s->insn_config = pci1723_dio_insn_config;
401 s->insn_bits = pci1723_dio_insn_bits;
412 static void pci1723_detach(struct comedi_device *dev)
414 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
422 comedi_pci_disable(pcidev);
427 static struct comedi_driver adv_pci1723_driver = {
428 .driver_name = "adv_pci1723",
429 .module = THIS_MODULE,
430 .attach = pci1723_attach,
431 .detach = pci1723_detach,
434 static int __devinit adv_pci1723_pci_probe(struct pci_dev *dev,
435 const struct pci_device_id *ent)
437 return comedi_pci_auto_config(dev, &adv_pci1723_driver);
440 static void __devexit adv_pci1723_pci_remove(struct pci_dev *dev)
442 comedi_pci_auto_unconfig(dev);
445 static DEFINE_PCI_DEVICE_TABLE(adv_pci1723_pci_table) = {
446 { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
449 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
451 static struct pci_driver adv_pci1723_pci_driver = {
452 .name = "adv_pci1723",
453 .id_table = adv_pci1723_pci_table,
454 .probe = adv_pci1723_pci_probe,
455 .remove = __devexit_p(adv_pci1723_pci_remove),
457 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
459 MODULE_AUTHOR("Comedi http://www.comedi.org");
460 MODULE_DESCRIPTION("Comedi low-level driver");
461 MODULE_LICENSE("GPL");