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