root/drivers/staging/comedi/drivers/dyna_pci10xx.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dyna_pci10xx_ai_eoc
  2. dyna_pci10xx_insn_read_ai
  3. dyna_pci10xx_insn_write_ao
  4. dyna_pci10xx_di_insn_bits
  5. dyna_pci10xx_do_insn_bits
  6. dyna_pci10xx_auto_attach
  7. dyna_pci10xx_detach
  8. dyna_pci10xx_pci_probe

   1 // SPDX-License-Identifier: GPL-2.0+
   2 /*
   3  * comedi/drivers/dyna_pci10xx.c
   4  * Copyright (C) 2011 Prashant Shah, pshah.mumbai@gmail.com
   5  */
   6 
   7 /*
   8  * Driver: dyna_pci10xx
   9  * Description: Dynalog India PCI DAQ Cards, http://www.dynalogindia.com/
  10  * Devices: [Dynalog] PCI-1050 (dyna_pci1050)
  11  * Author: Prashant Shah <pshah.mumbai@gmail.com>
  12  * Status: Stable
  13  *
  14  * Developed at Automation Labs, Chemical Dept., IIT Bombay, India.
  15  * Prof. Kannan Moudgalya <kannan@iitb.ac.in>
  16  * http://www.iitb.ac.in
  17  *
  18  * Notes :
  19  * - Dynalog India Pvt. Ltd. does not have a registered PCI Vendor ID and
  20  *   they are using the PLX Technlogies Vendor ID since that is the PCI Chip
  21  *   used in the card.
  22  * - Dynalog India Pvt. Ltd. has provided the internal register specification
  23  *   for their cards in their manuals.
  24  */
  25 
  26 #include <linux/module.h>
  27 #include <linux/delay.h>
  28 #include <linux/mutex.h>
  29 
  30 #include "../comedi_pci.h"
  31 
  32 #define READ_TIMEOUT 50
  33 
  34 static const struct comedi_lrange range_pci1050_ai = {
  35         3, {
  36                 BIP_RANGE(10),
  37                 BIP_RANGE(5),
  38                 UNI_RANGE(10)
  39         }
  40 };
  41 
  42 static const char range_codes_pci1050_ai[] = { 0x00, 0x10, 0x30 };
  43 
  44 struct dyna_pci10xx_private {
  45         struct mutex mutex;
  46         unsigned long BADR3;
  47 };
  48 
  49 static int dyna_pci10xx_ai_eoc(struct comedi_device *dev,
  50                                struct comedi_subdevice *s,
  51                                struct comedi_insn *insn,
  52                                unsigned long context)
  53 {
  54         unsigned int status;
  55 
  56         status = inw_p(dev->iobase);
  57         if (status & BIT(15))
  58                 return 0;
  59         return -EBUSY;
  60 }
  61 
  62 static int dyna_pci10xx_insn_read_ai(struct comedi_device *dev,
  63                                      struct comedi_subdevice *s,
  64                                      struct comedi_insn *insn,
  65                                      unsigned int *data)
  66 {
  67         struct dyna_pci10xx_private *devpriv = dev->private;
  68         int n;
  69         u16 d = 0;
  70         int ret = 0;
  71         unsigned int chan, range;
  72 
  73         /* get the channel number and range */
  74         chan = CR_CHAN(insn->chanspec);
  75         range = range_codes_pci1050_ai[CR_RANGE((insn->chanspec))];
  76 
  77         mutex_lock(&devpriv->mutex);
  78         /* convert n samples */
  79         for (n = 0; n < insn->n; n++) {
  80                 /* trigger conversion */
  81                 smp_mb();
  82                 outw_p(0x0000 + range + chan, dev->iobase + 2);
  83                 usleep_range(10, 20);
  84 
  85                 ret = comedi_timeout(dev, s, insn, dyna_pci10xx_ai_eoc, 0);
  86                 if (ret)
  87                         break;
  88 
  89                 /* read data */
  90                 d = inw_p(dev->iobase);
  91                 /* mask the first 4 bits - EOC bits */
  92                 d &= 0x0FFF;
  93                 data[n] = d;
  94         }
  95         mutex_unlock(&devpriv->mutex);
  96 
  97         /* return the number of samples read/written */
  98         return ret ? ret : n;
  99 }
 100 
 101 /* analog output callback */
 102 static int dyna_pci10xx_insn_write_ao(struct comedi_device *dev,
 103                                       struct comedi_subdevice *s,
 104                                       struct comedi_insn *insn,
 105                                       unsigned int *data)
 106 {
 107         struct dyna_pci10xx_private *devpriv = dev->private;
 108         int n;
 109 
 110         mutex_lock(&devpriv->mutex);
 111         for (n = 0; n < insn->n; n++) {
 112                 smp_mb();
 113                 /* trigger conversion and write data */
 114                 outw_p(data[n], dev->iobase);
 115                 usleep_range(10, 20);
 116         }
 117         mutex_unlock(&devpriv->mutex);
 118         return n;
 119 }
 120 
 121 /* digital input bit interface */
 122 static int dyna_pci10xx_di_insn_bits(struct comedi_device *dev,
 123                                      struct comedi_subdevice *s,
 124                                      struct comedi_insn *insn,
 125                                      unsigned int *data)
 126 {
 127         struct dyna_pci10xx_private *devpriv = dev->private;
 128         u16 d = 0;
 129 
 130         mutex_lock(&devpriv->mutex);
 131         smp_mb();
 132         d = inw_p(devpriv->BADR3);
 133         usleep_range(10, 100);
 134 
 135         /* on return the data[0] contains output and data[1] contains input */
 136         data[1] = d;
 137         data[0] = s->state;
 138         mutex_unlock(&devpriv->mutex);
 139         return insn->n;
 140 }
 141 
 142 static int dyna_pci10xx_do_insn_bits(struct comedi_device *dev,
 143                                      struct comedi_subdevice *s,
 144                                      struct comedi_insn *insn,
 145                                      unsigned int *data)
 146 {
 147         struct dyna_pci10xx_private *devpriv = dev->private;
 148 
 149         mutex_lock(&devpriv->mutex);
 150         if (comedi_dio_update_state(s, data)) {
 151                 smp_mb();
 152                 outw_p(s->state, devpriv->BADR3);
 153                 usleep_range(10, 100);
 154         }
 155 
 156         data[1] = s->state;
 157         mutex_unlock(&devpriv->mutex);
 158 
 159         return insn->n;
 160 }
 161 
 162 static int dyna_pci10xx_auto_attach(struct comedi_device *dev,
 163                                     unsigned long context_unused)
 164 {
 165         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 166         struct dyna_pci10xx_private *devpriv;
 167         struct comedi_subdevice *s;
 168         int ret;
 169 
 170         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
 171         if (!devpriv)
 172                 return -ENOMEM;
 173 
 174         ret = comedi_pci_enable(dev);
 175         if (ret)
 176                 return ret;
 177         dev->iobase = pci_resource_start(pcidev, 2);
 178         devpriv->BADR3 = pci_resource_start(pcidev, 3);
 179 
 180         mutex_init(&devpriv->mutex);
 181 
 182         ret = comedi_alloc_subdevices(dev, 4);
 183         if (ret)
 184                 return ret;
 185 
 186         /* analog input */
 187         s = &dev->subdevices[0];
 188         s->type = COMEDI_SUBD_AI;
 189         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
 190         s->n_chan = 16;
 191         s->maxdata = 0x0FFF;
 192         s->range_table = &range_pci1050_ai;
 193         s->insn_read = dyna_pci10xx_insn_read_ai;
 194 
 195         /* analog output */
 196         s = &dev->subdevices[1];
 197         s->type = COMEDI_SUBD_AO;
 198         s->subdev_flags = SDF_WRITABLE;
 199         s->n_chan = 1;
 200         s->maxdata = 0x0FFF;
 201         s->range_table = &range_unipolar10;
 202         s->insn_write = dyna_pci10xx_insn_write_ao;
 203 
 204         /* digital input */
 205         s = &dev->subdevices[2];
 206         s->type = COMEDI_SUBD_DI;
 207         s->subdev_flags = SDF_READABLE;
 208         s->n_chan = 16;
 209         s->maxdata = 1;
 210         s->range_table = &range_digital;
 211         s->insn_bits = dyna_pci10xx_di_insn_bits;
 212 
 213         /* digital output */
 214         s = &dev->subdevices[3];
 215         s->type = COMEDI_SUBD_DO;
 216         s->subdev_flags = SDF_WRITABLE;
 217         s->n_chan = 16;
 218         s->maxdata = 1;
 219         s->range_table = &range_digital;
 220         s->state = 0;
 221         s->insn_bits = dyna_pci10xx_do_insn_bits;
 222 
 223         return 0;
 224 }
 225 
 226 static void dyna_pci10xx_detach(struct comedi_device *dev)
 227 {
 228         struct dyna_pci10xx_private *devpriv = dev->private;
 229 
 230         comedi_pci_detach(dev);
 231         if (devpriv)
 232                 mutex_destroy(&devpriv->mutex);
 233 }
 234 
 235 static struct comedi_driver dyna_pci10xx_driver = {
 236         .driver_name    = "dyna_pci10xx",
 237         .module         = THIS_MODULE,
 238         .auto_attach    = dyna_pci10xx_auto_attach,
 239         .detach         = dyna_pci10xx_detach,
 240 };
 241 
 242 static int dyna_pci10xx_pci_probe(struct pci_dev *dev,
 243                                   const struct pci_device_id *id)
 244 {
 245         return comedi_pci_auto_config(dev, &dyna_pci10xx_driver,
 246                                       id->driver_data);
 247 }
 248 
 249 static const struct pci_device_id dyna_pci10xx_pci_table[] = {
 250         { PCI_DEVICE(PCI_VENDOR_ID_PLX, 0x1050) },
 251         { 0 }
 252 };
 253 MODULE_DEVICE_TABLE(pci, dyna_pci10xx_pci_table);
 254 
 255 static struct pci_driver dyna_pci10xx_pci_driver = {
 256         .name           = "dyna_pci10xx",
 257         .id_table       = dyna_pci10xx_pci_table,
 258         .probe          = dyna_pci10xx_pci_probe,
 259         .remove         = comedi_pci_auto_unconfig,
 260 };
 261 module_comedi_pci_driver(dyna_pci10xx_driver, dyna_pci10xx_pci_driver);
 262 
 263 MODULE_LICENSE("GPL");
 264 MODULE_AUTHOR("Prashant Shah <pshah.mumbai@gmail.com>");
 265 MODULE_DESCRIPTION("Comedi based drivers for Dynalog PCI DAQ cards");

/* [<][>][^][v][top][bottom][index][help] */