linux/drivers/staging/comedi/drivers/pcmad.c
<<
>>
Prefs
   1/*
   2 * pcmad.c
   3 * Hardware driver for Winsystems PCM-A/D12 and PCM-A/D16
   4 *
   5 * COMEDI - Linux Control and Measurement Device Interface
   6 * Copyright (C) 2000,2001 David A. Schleef <ds@schleef.org>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 */
  18
  19/*
  20 * Driver: pcmad
  21 * Description: Winsystems PCM-A/D12, PCM-A/D16
  22 * Devices: (Winsystems) PCM-A/D12 [pcmad12]
  23 *          (Winsystems) PCM-A/D16 [pcmad16]
  24 * Author: ds
  25 * Status: untested
  26 *
  27 * This driver was written on a bet that I couldn't write a driver
  28 * in less than 2 hours.  I won the bet, but never got paid.  =(
  29 *
  30 * Configuration options:
  31 *   [0] - I/O port base
  32 *   [1] - IRQ (unused)
  33 *   [2] - Analog input reference (must match jumpers)
  34 *         0 = single-ended (16 channels)
  35 *         1 = differential (8 channels)
  36 *   [3] - Analog input encoding (must match jumpers)
  37 *         0 = straight binary (0-5V input range)
  38 *         1 = two's complement (+-10V input range)
  39 */
  40
  41#include "../comedidev.h"
  42
  43#define PCMAD_STATUS            0
  44#define PCMAD_LSB               1
  45#define PCMAD_MSB               2
  46#define PCMAD_CONVERT           1
  47
  48struct pcmad_board_struct {
  49        const char *name;
  50        unsigned int ai_maxdata;
  51};
  52
  53static const struct pcmad_board_struct pcmad_boards[] = {
  54        {
  55                .name           = "pcmad12",
  56                .ai_maxdata     = 0x0fff,
  57        }, {
  58                .name           = "pcmad16",
  59                .ai_maxdata     = 0xffff,
  60        },
  61};
  62
  63#define TIMEOUT 100
  64
  65static int pcmad_ai_wait_for_eoc(struct comedi_device *dev,
  66                                 int timeout)
  67{
  68        int i;
  69
  70        for (i = 0; i < timeout; i++) {
  71                if ((inb(dev->iobase + PCMAD_STATUS) & 0x3) == 0x3)
  72                        return 0;
  73        }
  74        return -ETIME;
  75}
  76
  77static bool pcmad_range_is_bipolar(struct comedi_subdevice *s,
  78                                   unsigned int range)
  79{
  80        return s->range_table->range[range].min < 0;
  81}
  82
  83static int pcmad_ai_insn_read(struct comedi_device *dev,
  84                              struct comedi_subdevice *s,
  85                              struct comedi_insn *insn,
  86                              unsigned int *data)
  87{
  88        unsigned int chan = CR_CHAN(insn->chanspec);
  89        unsigned int range = CR_RANGE(insn->chanspec);
  90        unsigned int val;
  91        int ret;
  92        int i;
  93
  94        for (i = 0; i < insn->n; i++) {
  95                outb(chan, dev->iobase + PCMAD_CONVERT);
  96
  97                ret = pcmad_ai_wait_for_eoc(dev, TIMEOUT);
  98                if (ret)
  99                        return ret;
 100
 101                val = inb(dev->iobase + PCMAD_LSB) |
 102                      (inb(dev->iobase + PCMAD_MSB) << 8);
 103
 104                /* data is shifted on the pcmad12, fix it */
 105                if (s->maxdata == 0x0fff)
 106                        val >>= 4;
 107
 108                if (pcmad_range_is_bipolar(s, range)) {
 109                        /* munge the two's complement value */
 110                        val ^= ((s->maxdata + 1) >> 1);
 111                }
 112
 113                data[i] = val;
 114        }
 115
 116        return insn->n;
 117}
 118
 119static int pcmad_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 120{
 121        const struct pcmad_board_struct *board = comedi_board(dev);
 122        struct comedi_subdevice *s;
 123        int ret;
 124
 125        ret = comedi_request_region(dev, it->options[0], 0x04);
 126        if (ret)
 127                return ret;
 128
 129        ret = comedi_alloc_subdevices(dev, 1);
 130        if (ret)
 131                return ret;
 132
 133        s = &dev->subdevices[0];
 134        s->type         = COMEDI_SUBD_AI;
 135        if (it->options[1]) {
 136                /* 8 differential channels */
 137                s->subdev_flags = SDF_READABLE | AREF_DIFF;
 138                s->n_chan       = 8;
 139        } else {
 140                /* 16 single-ended channels */
 141                s->subdev_flags = SDF_READABLE | AREF_GROUND;
 142                s->n_chan       = 16;
 143        }
 144        s->len_chanlist = 1;
 145        s->maxdata      = board->ai_maxdata;
 146        s->range_table  = it->options[2] ? &range_bipolar10 : &range_unipolar5;
 147        s->insn_read    = pcmad_ai_insn_read;
 148
 149        return 0;
 150}
 151
 152static struct comedi_driver pcmad_driver = {
 153        .driver_name    = "pcmad",
 154        .module         = THIS_MODULE,
 155        .attach         = pcmad_attach,
 156        .detach         = comedi_legacy_detach,
 157        .board_name     = &pcmad_boards[0].name,
 158        .num_names      = ARRAY_SIZE(pcmad_boards),
 159        .offset         = sizeof(pcmad_boards[0]),
 160};
 161module_comedi_driver(pcmad_driver);
 162
 163MODULE_AUTHOR("Comedi http://www.comedi.org");
 164MODULE_DESCRIPTION("Comedi low-level driver");
 165MODULE_LICENSE("GPL");
 166