linux/drivers/comedi/drivers/pcm3724.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * pcm3724.c
   4 * Comedi driver for Advantech PCM-3724 Digital I/O board
   5 *
   6 * Drew Csillag <drew_csillag@yahoo.com>
   7 */
   8
   9/*
  10 * Driver: pcm3724
  11 * Description: Advantech PCM-3724
  12 * Devices: [Advantech] PCM-3724 (pcm3724)
  13 * Author: Drew Csillag <drew_csillag@yahoo.com>
  14 * Status: tested
  15 *
  16 * This is driver for digital I/O boards PCM-3724 with 48 DIO.
  17 * It needs 8255.o for operations and only immediate mode is supported.
  18 * See the source for configuration details.
  19 *
  20 * Copy/pasted/hacked from pcm724.c
  21 *
  22 * Configuration Options:
  23 *   [0] - I/O port base address
  24 */
  25
  26#include <linux/module.h>
  27#include "../comedidev.h"
  28
  29#include "8255.h"
  30
  31/*
  32 * Register I/O Map
  33 *
  34 * This board has two standard 8255 devices that provide six 8-bit DIO ports
  35 * (48 channels total). Six 74HCT245 chips (one for each port) buffer the
  36 * I/O lines to increase driving capability. Because the 74HCT245 is a
  37 * bidirectional, tri-state line buffer, two additional I/O ports are used
  38 * to control the direction of data and the enable of each port.
  39 */
  40#define PCM3724_8255_0_BASE             0x00
  41#define PCM3724_8255_1_BASE             0x04
  42#define PCM3724_DIO_DIR_REG             0x08
  43#define PCM3724_DIO_DIR_C0_OUT          BIT(0)
  44#define PCM3724_DIO_DIR_B0_OUT          BIT(1)
  45#define PCM3724_DIO_DIR_A0_OUT          BIT(2)
  46#define PCM3724_DIO_DIR_C1_OUT          BIT(3)
  47#define PCM3724_DIO_DIR_B1_OUT          BIT(4)
  48#define PCM3724_DIO_DIR_A1_OUT          BIT(5)
  49#define PCM3724_GATE_CTRL_REG           0x09
  50#define PCM3724_GATE_CTRL_C0_ENA        BIT(0)
  51#define PCM3724_GATE_CTRL_B0_ENA        BIT(1)
  52#define PCM3724_GATE_CTRL_A0_ENA        BIT(2)
  53#define PCM3724_GATE_CTRL_C1_ENA        BIT(3)
  54#define PCM3724_GATE_CTRL_B1_ENA        BIT(4)
  55#define PCM3724_GATE_CTRL_A1_ENA        BIT(5)
  56
  57/* used to track configured dios */
  58struct priv_pcm3724 {
  59        int dio_1;
  60        int dio_2;
  61};
  62
  63static int compute_buffer(int config, int devno, struct comedi_subdevice *s)
  64{
  65        /* 1 in io_bits indicates output */
  66        if (s->io_bits & 0x0000ff) {
  67                if (devno == 0)
  68                        config |= PCM3724_DIO_DIR_A0_OUT;
  69                else
  70                        config |= PCM3724_DIO_DIR_A1_OUT;
  71        }
  72        if (s->io_bits & 0x00ff00) {
  73                if (devno == 0)
  74                        config |= PCM3724_DIO_DIR_B0_OUT;
  75                else
  76                        config |= PCM3724_DIO_DIR_B1_OUT;
  77        }
  78        if (s->io_bits & 0xff0000) {
  79                if (devno == 0)
  80                        config |= PCM3724_DIO_DIR_C0_OUT;
  81                else
  82                        config |= PCM3724_DIO_DIR_C1_OUT;
  83        }
  84        return config;
  85}
  86
  87static void do_3724_config(struct comedi_device *dev,
  88                           struct comedi_subdevice *s, int chanspec)
  89{
  90        struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
  91        struct comedi_subdevice *s_dio2 = &dev->subdevices[1];
  92        int config;
  93        int buffer_config;
  94        unsigned long port_8255_cfg;
  95
  96        config = I8255_CTRL_CW;
  97        buffer_config = 0;
  98
  99        /* 1 in io_bits indicates output, 1 in config indicates input */
 100        if (!(s->io_bits & 0x0000ff))
 101                config |= I8255_CTRL_A_IO;
 102
 103        if (!(s->io_bits & 0x00ff00))
 104                config |= I8255_CTRL_B_IO;
 105
 106        if (!(s->io_bits & 0xff0000))
 107                config |= I8255_CTRL_C_HI_IO | I8255_CTRL_C_LO_IO;
 108
 109        buffer_config = compute_buffer(0, 0, s_dio1);
 110        buffer_config = compute_buffer(buffer_config, 1, s_dio2);
 111
 112        if (s == s_dio1)
 113                port_8255_cfg = dev->iobase + I8255_CTRL_REG;
 114        else
 115                port_8255_cfg = dev->iobase + I8255_SIZE + I8255_CTRL_REG;
 116
 117        outb(buffer_config, dev->iobase + PCM3724_DIO_DIR_REG);
 118
 119        outb(config, port_8255_cfg);
 120}
 121
 122static void enable_chan(struct comedi_device *dev, struct comedi_subdevice *s,
 123                        int chanspec)
 124{
 125        struct priv_pcm3724 *priv = dev->private;
 126        struct comedi_subdevice *s_dio1 = &dev->subdevices[0];
 127        unsigned int mask;
 128        int gatecfg;
 129
 130        gatecfg = 0;
 131
 132        mask = 1 << CR_CHAN(chanspec);
 133        if (s == s_dio1)
 134                priv->dio_1 |= mask;
 135        else
 136                priv->dio_2 |= mask;
 137
 138        if (priv->dio_1 & 0xff0000)
 139                gatecfg |= PCM3724_GATE_CTRL_C0_ENA;
 140
 141        if (priv->dio_1 & 0xff00)
 142                gatecfg |= PCM3724_GATE_CTRL_B0_ENA;
 143
 144        if (priv->dio_1 & 0xff)
 145                gatecfg |= PCM3724_GATE_CTRL_A0_ENA;
 146
 147        if (priv->dio_2 & 0xff0000)
 148                gatecfg |= PCM3724_GATE_CTRL_C1_ENA;
 149
 150        if (priv->dio_2 & 0xff00)
 151                gatecfg |= PCM3724_GATE_CTRL_B1_ENA;
 152
 153        if (priv->dio_2 & 0xff)
 154                gatecfg |= PCM3724_GATE_CTRL_A1_ENA;
 155
 156        outb(gatecfg, dev->iobase + PCM3724_GATE_CTRL_REG);
 157}
 158
 159/* overriding the 8255 insn config */
 160static int subdev_3724_insn_config(struct comedi_device *dev,
 161                                   struct comedi_subdevice *s,
 162                                   struct comedi_insn *insn,
 163                                   unsigned int *data)
 164{
 165        unsigned int chan = CR_CHAN(insn->chanspec);
 166        unsigned int mask;
 167        int ret;
 168
 169        if (chan < 8)
 170                mask = 0x0000ff;
 171        else if (chan < 16)
 172                mask = 0x00ff00;
 173        else if (chan < 20)
 174                mask = 0x0f0000;
 175        else
 176                mask = 0xf00000;
 177
 178        ret = comedi_dio_insn_config(dev, s, insn, data, mask);
 179        if (ret)
 180                return ret;
 181
 182        do_3724_config(dev, s, insn->chanspec);
 183        enable_chan(dev, s, insn->chanspec);
 184
 185        return insn->n;
 186}
 187
 188static int pcm3724_attach(struct comedi_device *dev,
 189                          struct comedi_devconfig *it)
 190{
 191        struct priv_pcm3724 *priv;
 192        struct comedi_subdevice *s;
 193        int ret, i;
 194
 195        priv = comedi_alloc_devpriv(dev, sizeof(*priv));
 196        if (!priv)
 197                return -ENOMEM;
 198
 199        ret = comedi_request_region(dev, it->options[0], 0x10);
 200        if (ret)
 201                return ret;
 202
 203        ret = comedi_alloc_subdevices(dev, 2);
 204        if (ret)
 205                return ret;
 206
 207        for (i = 0; i < dev->n_subdevices; i++) {
 208                s = &dev->subdevices[i];
 209                ret = subdev_8255_init(dev, s, NULL, i * I8255_SIZE);
 210                if (ret)
 211                        return ret;
 212                s->insn_config = subdev_3724_insn_config;
 213        }
 214        return 0;
 215}
 216
 217static struct comedi_driver pcm3724_driver = {
 218        .driver_name    = "pcm3724",
 219        .module         = THIS_MODULE,
 220        .attach         = pcm3724_attach,
 221        .detach         = comedi_legacy_detach,
 222};
 223module_comedi_driver(pcm3724_driver);
 224
 225MODULE_AUTHOR("Comedi https://www.comedi.org");
 226MODULE_DESCRIPTION("Comedi driver for Advantech PCM-3724 Digital I/O board");
 227MODULE_LICENSE("GPL");
 228