linux/drivers/staging/comedi/drivers/adv_pci1724.c
<<
>>
Prefs
   1/*
   2 * adv_pci1724.c
   3 * Comedi driver for the Advantech PCI-1724U card.
   4 *
   5 * Author:  Frank Mori Hess <fmh6jj@gmail.com>
   6 * Copyright (C) 2013 GnuBIO Inc
   7 *
   8 * COMEDI - Linux Control and Measurement Device Interface
   9 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 */
  21
  22/*
  23 * Driver: adv_pci1724
  24 * Description: Advantech PCI-1724U
  25 * Devices: (Advantech) PCI-1724U [adv_pci1724]
  26 * Author: Frank Mori Hess <fmh6jj@gmail.com>
  27 * Updated: 2013-02-09
  28 * Status: works
  29 *
  30 * Configuration Options: not applicable, uses comedi PCI auto config
  31 *
  32 * Subdevice 0 is the analog output.
  33 * Subdevice 1 is the offset calibration for the analog output.
  34 * Subdevice 2 is the gain calibration for the analog output.
  35 *
  36 * The calibration offset and gains have quite a large effect on the
  37 * analog output, so it is possible to adjust the analog output to
  38 * have an output range significantly different from the board's
  39 * nominal output ranges. For a calibrated +/-10V range, the analog
  40 * output's offset will be set somewhere near mid-range (0x2000) and
  41 * its gain will be near maximum (0x3fff).
  42 *
  43 * There is really no difference between the board's documented 0-20mA
  44 * versus 4-20mA output ranges. To pick one or the other is simply a
  45 * matter of adjusting the offset and gain calibration until the board
  46 * outputs in the desired range.
  47 */
  48
  49#include <linux/module.h>
  50#include <linux/pci.h>
  51
  52#include "../comedidev.h"
  53
  54/*
  55 * PCI bar 2 Register I/O map (dev->iobase)
  56 */
  57#define PCI1724_DAC_CTRL_REG            0x00
  58#define PCI1724_DAC_CTRL_GX(x)          (1 << (20 + ((x) / 8)))
  59#define PCI1724_DAC_CTRL_CX(x)          (((x) % 8) << 16)
  60#define PCI1724_DAC_CTRL_MODE_GAIN      (1 << 14)
  61#define PCI1724_DAC_CTRL_MODE_OFFSET    (2 << 14)
  62#define PCI1724_DAC_CTRL_MODE_NORMAL    (3 << 14)
  63#define PCI1724_DAC_CTRL_MODE_MASK      (3 << 14)
  64#define PCI1724_DAC_CTRL_DATA(x)        (((x) & 0x3fff) << 0)
  65#define PCI1724_SYNC_CTRL_REG           0x04
  66#define PCI1724_SYNC_CTRL_DACSTAT       (1 << 1)
  67#define PCI1724_SYNC_CTRL_SYN           (1 << 0)
  68#define PCI1724_EEPROM_CTRL_REG         0x08
  69#define PCI1724_SYNC_TRIG_REG           0x0c  /* any value works */
  70#define PCI1724_BOARD_ID_REG            0x10
  71#define PCI1724_BOARD_ID_MASK           (0xf << 0)
  72
  73static const struct comedi_lrange adv_pci1724_ao_ranges = {
  74        4, {
  75                BIP_RANGE(10),
  76                RANGE_mA(0, 20),
  77                RANGE_mA(4, 20),
  78                RANGE_unitless(0, 1)
  79        }
  80};
  81
  82static int adv_pci1724_dac_idle(struct comedi_device *dev,
  83                                struct comedi_subdevice *s,
  84                                struct comedi_insn *insn,
  85                                unsigned long context)
  86{
  87        unsigned int status;
  88
  89        status = inl(dev->iobase + PCI1724_SYNC_CTRL_REG);
  90        if ((status & PCI1724_SYNC_CTRL_DACSTAT) == 0)
  91                return 0;
  92        return -EBUSY;
  93}
  94
  95static int adv_pci1724_insn_write(struct comedi_device *dev,
  96                                  struct comedi_subdevice *s,
  97                                  struct comedi_insn *insn,
  98                                  unsigned int *data)
  99{
 100        unsigned long mode = (unsigned long)s->private;
 101        unsigned int chan = CR_CHAN(insn->chanspec);
 102        unsigned int ctrl;
 103        int ret;
 104        int i;
 105
 106        ctrl = PCI1724_DAC_CTRL_GX(chan) | PCI1724_DAC_CTRL_CX(chan) | mode;
 107
 108        /* turn off synchronous mode */
 109        outl(0, dev->iobase + PCI1724_SYNC_CTRL_REG);
 110
 111        for (i = 0; i < insn->n; ++i) {
 112                unsigned int val = data[i];
 113
 114                ret = comedi_timeout(dev, s, insn, adv_pci1724_dac_idle, 0);
 115                if (ret)
 116                        return ret;
 117
 118                outl(ctrl | PCI1724_DAC_CTRL_DATA(val),
 119                     dev->iobase + PCI1724_DAC_CTRL_REG);
 120
 121                s->readback[chan] = val;
 122        }
 123
 124        return insn->n;
 125}
 126
 127static int adv_pci1724_auto_attach(struct comedi_device *dev,
 128                                   unsigned long context_unused)
 129{
 130        struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 131        struct comedi_subdevice *s;
 132        unsigned int board_id;
 133        int ret;
 134
 135        ret = comedi_pci_enable(dev);
 136        if (ret)
 137                return ret;
 138
 139        dev->iobase = pci_resource_start(pcidev, 2);
 140        board_id = inl(dev->iobase + PCI1724_BOARD_ID_REG);
 141        dev_info(dev->class_dev, "board id: %d\n",
 142                 board_id & PCI1724_BOARD_ID_MASK);
 143
 144        ret = comedi_alloc_subdevices(dev, 3);
 145        if (ret)
 146                return ret;
 147
 148        /* Analog Output subdevice */
 149        s = &dev->subdevices[0];
 150        s->type         = COMEDI_SUBD_AO;
 151        s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
 152        s->n_chan       = 32;
 153        s->maxdata      = 0x3fff;
 154        s->range_table  = &adv_pci1724_ao_ranges;
 155        s->insn_write   = adv_pci1724_insn_write;
 156        s->private      = (void *)PCI1724_DAC_CTRL_MODE_NORMAL;
 157
 158        ret = comedi_alloc_subdev_readback(s);
 159        if (ret)
 160                return ret;
 161
 162        /* Offset Calibration subdevice */
 163        s = &dev->subdevices[1];
 164        s->type         = COMEDI_SUBD_CALIB;
 165        s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
 166        s->n_chan       = 32;
 167        s->maxdata      = 0x3fff;
 168        s->insn_write   = adv_pci1724_insn_write;
 169        s->private      = (void *)PCI1724_DAC_CTRL_MODE_OFFSET;
 170
 171        ret = comedi_alloc_subdev_readback(s);
 172        if (ret)
 173                return ret;
 174
 175        /* Gain Calibration subdevice */
 176        s = &dev->subdevices[2];
 177        s->type         = COMEDI_SUBD_CALIB;
 178        s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
 179        s->n_chan       = 32;
 180        s->maxdata      = 0x3fff;
 181        s->insn_write   = adv_pci1724_insn_write;
 182        s->private      = (void *)PCI1724_DAC_CTRL_MODE_GAIN;
 183
 184        ret = comedi_alloc_subdev_readback(s);
 185        if (ret)
 186                return ret;
 187
 188        return 0;
 189}
 190
 191static struct comedi_driver adv_pci1724_driver = {
 192        .driver_name    = "adv_pci1724",
 193        .module         = THIS_MODULE,
 194        .auto_attach    = adv_pci1724_auto_attach,
 195        .detach         = comedi_pci_detach,
 196};
 197
 198static int adv_pci1724_pci_probe(struct pci_dev *dev,
 199                                 const struct pci_device_id *id)
 200{
 201        return comedi_pci_auto_config(dev, &adv_pci1724_driver,
 202                                      id->driver_data);
 203}
 204
 205static const struct pci_device_id adv_pci1724_pci_table[] = {
 206        { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1724) },
 207        { 0 }
 208};
 209MODULE_DEVICE_TABLE(pci, adv_pci1724_pci_table);
 210
 211static struct pci_driver adv_pci1724_pci_driver = {
 212        .name           = "adv_pci1724",
 213        .id_table       = adv_pci1724_pci_table,
 214        .probe          = adv_pci1724_pci_probe,
 215        .remove         = comedi_pci_auto_unconfig,
 216};
 217module_comedi_pci_driver(adv_pci1724_driver, adv_pci1724_pci_driver);
 218
 219MODULE_AUTHOR("Frank Mori Hess <fmh6jj@gmail.com>");
 220MODULE_DESCRIPTION("Advantech PCI-1724U Comedi driver");
 221MODULE_LICENSE("GPL");
 222
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.