root/drivers/staging/comedi/drivers/cb_pcidda.c

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

DEFINITIONS

This source file includes following definitions.
  1. cb_pcidda_serial_in
  2. cb_pcidda_serial_out
  3. cb_pcidda_read_eeprom
  4. cb_pcidda_write_caldac
  5. cb_pcidda_calibrate
  6. cb_pcidda_ao_insn_write
  7. cb_pcidda_auto_attach
  8. cb_pcidda_pci_probe

   1 // SPDX-License-Identifier: GPL-2.0+
   2 /*
   3  * comedi/drivers/cb_pcidda.c
   4  * Driver for the ComputerBoards / MeasurementComputing PCI-DDA series.
   5  *
   6  * Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
   7  * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
   8  *
   9  * COMEDI - Linux Control and Measurement Device Interface
  10  * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
  11  */
  12 
  13 /*
  14  * Driver: cb_pcidda
  15  * Description: MeasurementComputing PCI-DDA series
  16  * Devices: [Measurement Computing] PCI-DDA08/12 (pci-dda08/12),
  17  *   PCI-DDA04/12 (pci-dda04/12), PCI-DDA02/12 (pci-dda02/12),
  18  *   PCI-DDA08/16 (pci-dda08/16), PCI-DDA04/16 (pci-dda04/16),
  19  *   PCI-DDA02/16 (pci-dda02/16)
  20  * Author: Ivan Martinez <ivanmr@altavista.com>
  21  *         Frank Mori Hess <fmhess@users.sourceforge.net>
  22  * Status: works
  23  *
  24  * Configuration options: not applicable, uses PCI auto config
  25  *
  26  * Only simple analog output writing is supported.
  27  */
  28 
  29 #include <linux/module.h>
  30 
  31 #include "../comedi_pci.h"
  32 
  33 #include "8255.h"
  34 
  35 #define EEPROM_SIZE     128     /*  number of entries in eeprom */
  36 /* maximum number of ao channels for supported boards */
  37 #define MAX_AO_CHANNELS 8
  38 
  39 /* Digital I/O registers */
  40 #define CB_DDA_DIO0_8255_BASE           0x00
  41 #define CB_DDA_DIO1_8255_BASE           0x04
  42 
  43 /* DAC registers */
  44 #define CB_DDA_DA_CTRL_REG              0x00       /* D/A Control Register  */
  45 #define CB_DDA_DA_CTRL_SU               BIT(0)   /*  Simultaneous update  */
  46 #define CB_DDA_DA_CTRL_EN               BIT(1)   /*  Enable specified DAC */
  47 #define CB_DDA_DA_CTRL_DAC(x)           ((x) << 2) /*  Specify DAC channel  */
  48 #define CB_DDA_DA_CTRL_RANGE2V5         (0 << 6)   /*  2.5V range           */
  49 #define CB_DDA_DA_CTRL_RANGE5V          (2 << 6)   /*  5V range             */
  50 #define CB_DDA_DA_CTRL_RANGE10V         (3 << 6)   /*  10V range            */
  51 #define CB_DDA_DA_CTRL_UNIP             BIT(8)   /*  Unipolar range       */
  52 
  53 #define DACALIBRATION1  4       /*  D/A CALIBRATION REGISTER 1 */
  54 /* write bits */
  55 /* serial data input for eeprom, caldacs, reference dac */
  56 #define SERIAL_IN_BIT   0x1
  57 #define CAL_CHANNEL_MASK        (0x7 << 1)
  58 #define CAL_CHANNEL_BITS(channel)       (((channel) << 1) & CAL_CHANNEL_MASK)
  59 /* read bits */
  60 #define CAL_COUNTER_MASK        0x1f
  61 /* calibration counter overflow status bit */
  62 #define CAL_COUNTER_OVERFLOW_BIT        0x20
  63 /* analog output is less than reference dac voltage */
  64 #define AO_BELOW_REF_BIT        0x40
  65 #define SERIAL_OUT_BIT  0x80    /*  serial data out, for reading from eeprom */
  66 
  67 #define DACALIBRATION2  6       /*  D/A CALIBRATION REGISTER 2 */
  68 #define SELECT_EEPROM_BIT       0x1     /*  send serial data in to eeprom */
  69 /* don't send serial data to MAX542 reference dac */
  70 #define DESELECT_REF_DAC_BIT    0x2
  71 /* don't send serial data to caldac n */
  72 #define DESELECT_CALDAC_BIT(n)  (0x4 << (n))
  73 /* manual says to set this bit with no explanation */
  74 #define DUMMY_BIT       0x40
  75 
  76 #define CB_DDA_DA_DATA_REG(x)           (0x08 + ((x) * 2))
  77 
  78 /* Offsets for the caldac channels */
  79 #define CB_DDA_CALDAC_FINE_GAIN         0
  80 #define CB_DDA_CALDAC_COURSE_GAIN       1
  81 #define CB_DDA_CALDAC_COURSE_OFFSET     2
  82 #define CB_DDA_CALDAC_FINE_OFFSET       3
  83 
  84 static const struct comedi_lrange cb_pcidda_ranges = {
  85         6, {
  86                 BIP_RANGE(10),
  87                 BIP_RANGE(5),
  88                 BIP_RANGE(2.5),
  89                 UNI_RANGE(10),
  90                 UNI_RANGE(5),
  91                 UNI_RANGE(2.5)
  92         }
  93 };
  94 
  95 enum cb_pcidda_boardid {
  96         BOARD_DDA02_12,
  97         BOARD_DDA04_12,
  98         BOARD_DDA08_12,
  99         BOARD_DDA02_16,
 100         BOARD_DDA04_16,
 101         BOARD_DDA08_16,
 102 };
 103 
 104 struct cb_pcidda_board {
 105         const char *name;
 106         int ao_chans;
 107         int ao_bits;
 108 };
 109 
 110 static const struct cb_pcidda_board cb_pcidda_boards[] = {
 111         [BOARD_DDA02_12] = {
 112                 .name           = "pci-dda02/12",
 113                 .ao_chans       = 2,
 114                 .ao_bits        = 12,
 115         },
 116         [BOARD_DDA04_12] = {
 117                 .name           = "pci-dda04/12",
 118                 .ao_chans       = 4,
 119                 .ao_bits        = 12,
 120         },
 121         [BOARD_DDA08_12] = {
 122                 .name           = "pci-dda08/12",
 123                 .ao_chans       = 8,
 124                 .ao_bits        = 12,
 125         },
 126         [BOARD_DDA02_16] = {
 127                 .name           = "pci-dda02/16",
 128                 .ao_chans       = 2,
 129                 .ao_bits        = 16,
 130         },
 131         [BOARD_DDA04_16] = {
 132                 .name           = "pci-dda04/16",
 133                 .ao_chans       = 4,
 134                 .ao_bits        = 16,
 135         },
 136         [BOARD_DDA08_16] = {
 137                 .name           = "pci-dda08/16",
 138                 .ao_chans       = 8,
 139                 .ao_bits        = 16,
 140         },
 141 };
 142 
 143 struct cb_pcidda_private {
 144         unsigned long daqio;
 145         /* bits last written to da calibration register 1 */
 146         unsigned int dac_cal1_bits;
 147         /* current range settings for output channels */
 148         unsigned int ao_range[MAX_AO_CHANNELS];
 149         u16 eeprom_data[EEPROM_SIZE];   /*  software copy of board's eeprom */
 150 };
 151 
 152 /* lowlevel read from eeprom */
 153 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
 154 {
 155         struct cb_pcidda_private *devpriv = dev->private;
 156         unsigned int value = 0;
 157         int i;
 158         const int value_width = 16;     /*  number of bits wide values are */
 159 
 160         for (i = 1; i <= value_width; i++) {
 161                 /*  read bits most significant bit first */
 162                 if (inw_p(devpriv->daqio + DACALIBRATION1) & SERIAL_OUT_BIT)
 163                         value |= 1 << (value_width - i);
 164         }
 165 
 166         return value;
 167 }
 168 
 169 /* lowlevel write to eeprom/dac */
 170 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
 171                                  unsigned int num_bits)
 172 {
 173         struct cb_pcidda_private *devpriv = dev->private;
 174         int i;
 175 
 176         for (i = 1; i <= num_bits; i++) {
 177                 /*  send bits most significant bit first */
 178                 if (value & (1 << (num_bits - i)))
 179                         devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
 180                 else
 181                         devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
 182                 outw_p(devpriv->dac_cal1_bits, devpriv->daqio + DACALIBRATION1);
 183         }
 184 }
 185 
 186 /* reads a 16 bit value from board's eeprom */
 187 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
 188                                           unsigned int address)
 189 {
 190         struct cb_pcidda_private *devpriv = dev->private;
 191         unsigned int i;
 192         unsigned int cal2_bits;
 193         unsigned int value;
 194         /* one caldac for every two dac channels */
 195         const int max_num_caldacs = 4;
 196         /* bits to send to tell eeprom we want to read */
 197         const int read_instruction = 0x6;
 198         const int instruction_length = 3;
 199         const int address_length = 8;
 200 
 201         /*  send serial output stream to eeprom */
 202         cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
 203         /*  deactivate caldacs (one caldac for every two channels) */
 204         for (i = 0; i < max_num_caldacs; i++)
 205                 cal2_bits |= DESELECT_CALDAC_BIT(i);
 206         outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
 207 
 208         /*  tell eeprom we want to read */
 209         cb_pcidda_serial_out(dev, read_instruction, instruction_length);
 210         /*  send address we want to read from */
 211         cb_pcidda_serial_out(dev, address, address_length);
 212 
 213         value = cb_pcidda_serial_in(dev);
 214 
 215         /*  deactivate eeprom */
 216         cal2_bits &= ~SELECT_EEPROM_BIT;
 217         outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
 218 
 219         return value;
 220 }
 221 
 222 /* writes to 8 bit calibration dacs */
 223 static void cb_pcidda_write_caldac(struct comedi_device *dev,
 224                                    unsigned int caldac, unsigned int channel,
 225                                    unsigned int value)
 226 {
 227         struct cb_pcidda_private *devpriv = dev->private;
 228         unsigned int cal2_bits;
 229         unsigned int i;
 230         /* caldacs use 3 bit channel specification */
 231         const int num_channel_bits = 3;
 232         const int num_caldac_bits = 8;  /*  8 bit calibration dacs */
 233         /* one caldac for every two dac channels */
 234         const int max_num_caldacs = 4;
 235 
 236         /* write 3 bit channel */
 237         cb_pcidda_serial_out(dev, channel, num_channel_bits);
 238         /*  write 8 bit caldac value */
 239         cb_pcidda_serial_out(dev, value, num_caldac_bits);
 240 
 241 /*
 242  * latch stream into appropriate caldac deselect reference dac
 243  */
 244         cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
 245         /*  deactivate caldacs (one caldac for every two channels) */
 246         for (i = 0; i < max_num_caldacs; i++)
 247                 cal2_bits |= DESELECT_CALDAC_BIT(i);
 248         /*  activate the caldac we want */
 249         cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
 250         outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
 251         /*  deactivate caldac */
 252         cal2_bits |= DESELECT_CALDAC_BIT(caldac);
 253         outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
 254 }
 255 
 256 /* set caldacs to eeprom values for given channel and range */
 257 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
 258                                 unsigned int range)
 259 {
 260         struct cb_pcidda_private *devpriv = dev->private;
 261         unsigned int caldac = channel / 2;      /* two caldacs per channel */
 262         unsigned int chan = 4 * (channel % 2);  /* caldac channel base */
 263         unsigned int index = 2 * range + 12 * channel;
 264         unsigned int offset;
 265         unsigned int gain;
 266 
 267         /* save range so we can tell when we need to readjust calibration */
 268         devpriv->ao_range[channel] = range;
 269 
 270         /* get values from eeprom data */
 271         offset = devpriv->eeprom_data[0x7 + index];
 272         gain = devpriv->eeprom_data[0x8 + index];
 273 
 274         /* set caldacs */
 275         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_OFFSET,
 276                                (offset >> 8) & 0xff);
 277         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_OFFSET,
 278                                offset & 0xff);
 279         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_GAIN,
 280                                (gain >> 8) & 0xff);
 281         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_GAIN,
 282                                gain & 0xff);
 283 }
 284 
 285 static int cb_pcidda_ao_insn_write(struct comedi_device *dev,
 286                                    struct comedi_subdevice *s,
 287                                    struct comedi_insn *insn,
 288                                    unsigned int *data)
 289 {
 290         struct cb_pcidda_private *devpriv = dev->private;
 291         unsigned int channel = CR_CHAN(insn->chanspec);
 292         unsigned int range = CR_RANGE(insn->chanspec);
 293         unsigned int ctrl;
 294         unsigned int i;
 295 
 296         if (range != devpriv->ao_range[channel])
 297                 cb_pcidda_calibrate(dev, channel, range);
 298 
 299         ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
 300 
 301         switch (range) {
 302         case 0:
 303         case 3:
 304                 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
 305                 break;
 306         case 1:
 307         case 4:
 308                 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
 309                 break;
 310         case 2:
 311         case 5:
 312                 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
 313                 break;
 314         }
 315 
 316         if (range > 2)
 317                 ctrl |= CB_DDA_DA_CTRL_UNIP;
 318 
 319         outw(ctrl, devpriv->daqio + CB_DDA_DA_CTRL_REG);
 320 
 321         for (i = 0; i < insn->n; i++)
 322                 outw(data[i], devpriv->daqio + CB_DDA_DA_DATA_REG(channel));
 323 
 324         return insn->n;
 325 }
 326 
 327 static int cb_pcidda_auto_attach(struct comedi_device *dev,
 328                                  unsigned long context)
 329 {
 330         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 331         const struct cb_pcidda_board *board = NULL;
 332         struct cb_pcidda_private *devpriv;
 333         struct comedi_subdevice *s;
 334         int i;
 335         int ret;
 336 
 337         if (context < ARRAY_SIZE(cb_pcidda_boards))
 338                 board = &cb_pcidda_boards[context];
 339         if (!board)
 340                 return -ENODEV;
 341         dev->board_ptr = board;
 342         dev->board_name = board->name;
 343 
 344         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
 345         if (!devpriv)
 346                 return -ENOMEM;
 347 
 348         ret = comedi_pci_enable(dev);
 349         if (ret)
 350                 return ret;
 351         dev->iobase = pci_resource_start(pcidev, 2);
 352         devpriv->daqio = pci_resource_start(pcidev, 3);
 353 
 354         ret = comedi_alloc_subdevices(dev, 3);
 355         if (ret)
 356                 return ret;
 357 
 358         s = &dev->subdevices[0];
 359         /* analog output subdevice */
 360         s->type = COMEDI_SUBD_AO;
 361         s->subdev_flags = SDF_WRITABLE;
 362         s->n_chan = board->ao_chans;
 363         s->maxdata = (1 << board->ao_bits) - 1;
 364         s->range_table = &cb_pcidda_ranges;
 365         s->insn_write = cb_pcidda_ao_insn_write;
 366 
 367         /* two 8255 digital io subdevices */
 368         for (i = 0; i < 2; i++) {
 369                 s = &dev->subdevices[1 + i];
 370                 ret = subdev_8255_init(dev, s, NULL, i * I8255_SIZE);
 371                 if (ret)
 372                         return ret;
 373         }
 374 
 375         /* Read the caldac eeprom data */
 376         for (i = 0; i < EEPROM_SIZE; i++)
 377                 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
 378 
 379         /*  set calibrations dacs */
 380         for (i = 0; i < board->ao_chans; i++)
 381                 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
 382 
 383         return 0;
 384 }
 385 
 386 static struct comedi_driver cb_pcidda_driver = {
 387         .driver_name    = "cb_pcidda",
 388         .module         = THIS_MODULE,
 389         .auto_attach    = cb_pcidda_auto_attach,
 390         .detach         = comedi_pci_detach,
 391 };
 392 
 393 static int cb_pcidda_pci_probe(struct pci_dev *dev,
 394                                const struct pci_device_id *id)
 395 {
 396         return comedi_pci_auto_config(dev, &cb_pcidda_driver,
 397                                       id->driver_data);
 398 }
 399 
 400 static const struct pci_device_id cb_pcidda_pci_table[] = {
 401         { PCI_VDEVICE(CB, 0x0020), BOARD_DDA02_12 },
 402         { PCI_VDEVICE(CB, 0x0021), BOARD_DDA04_12 },
 403         { PCI_VDEVICE(CB, 0x0022), BOARD_DDA08_12 },
 404         { PCI_VDEVICE(CB, 0x0023), BOARD_DDA02_16 },
 405         { PCI_VDEVICE(CB, 0x0024), BOARD_DDA04_16 },
 406         { PCI_VDEVICE(CB, 0x0025), BOARD_DDA08_16 },
 407         { 0 }
 408 };
 409 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
 410 
 411 static struct pci_driver cb_pcidda_pci_driver = {
 412         .name           = "cb_pcidda",
 413         .id_table       = cb_pcidda_pci_table,
 414         .probe          = cb_pcidda_pci_probe,
 415         .remove         = comedi_pci_auto_unconfig,
 416 };
 417 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
 418 
 419 MODULE_AUTHOR("Comedi http://www.comedi.org");
 420 MODULE_DESCRIPTION("Comedi low-level driver");
 421 MODULE_LICENSE("GPL");

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