linux/drivers/staging/comedi/drivers/comedi_8255.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * comedi_8255.c
   4 * Generic 8255 digital I/O support
   5 *
   6 * Split from the Comedi "8255" driver module.
   7 *
   8 * COMEDI - Linux Control and Measurement Device Interface
   9 * Copyright (C) 1998 David A. Schleef <ds@schleef.org>
  10 */
  11
  12/*
  13 * Module: comedi_8255
  14 * Description: Generic 8255 support
  15 * Author: ds
  16 * Updated: Fri, 22 May 2015 12:14:17 +0000
  17 * Status: works
  18 *
  19 * This module is not used directly by end-users.  Rather, it is used by
  20 * other drivers to provide support for an 8255 "Programmable Peripheral
  21 * Interface" (PPI) chip.
  22 *
  23 * The classic in digital I/O.  The 8255 appears in Comedi as a single
  24 * digital I/O subdevice with 24 channels.  The channel 0 corresponds to
  25 * the 8255's port A, bit 0; channel 23 corresponds to port C, bit 7.
  26 * Direction configuration is done in blocks, with channels 0-7, 8-15,
  27 * 16-19, and 20-23 making up the 4 blocks.  The only 8255 mode
  28 * supported is mode 0.
  29 */
  30
  31#include <linux/module.h>
  32#include "../comedidev.h"
  33
  34#include "8255.h"
  35
  36struct subdev_8255_private {
  37        unsigned long regbase;
  38        int (*io)(struct comedi_device *dev, int dir, int port, int data,
  39                  unsigned long regbase);
  40};
  41
  42static int subdev_8255_io(struct comedi_device *dev,
  43                          int dir, int port, int data, unsigned long regbase)
  44{
  45        if (dir) {
  46                outb(data, dev->iobase + regbase + port);
  47                return 0;
  48        }
  49        return inb(dev->iobase + regbase + port);
  50}
  51
  52static int subdev_8255_mmio(struct comedi_device *dev,
  53                            int dir, int port, int data, unsigned long regbase)
  54{
  55        if (dir) {
  56                writeb(data, dev->mmio + regbase + port);
  57                return 0;
  58        }
  59        return readb(dev->mmio + regbase + port);
  60}
  61
  62static int subdev_8255_insn(struct comedi_device *dev,
  63                            struct comedi_subdevice *s,
  64                            struct comedi_insn *insn,
  65                            unsigned int *data)
  66{
  67        struct subdev_8255_private *spriv = s->private;
  68        unsigned long regbase = spriv->regbase;
  69        unsigned int mask;
  70        unsigned int v;
  71
  72        mask = comedi_dio_update_state(s, data);
  73        if (mask) {
  74                if (mask & 0xff)
  75                        spriv->io(dev, 1, I8255_DATA_A_REG,
  76                                  s->state & 0xff, regbase);
  77                if (mask & 0xff00)
  78                        spriv->io(dev, 1, I8255_DATA_B_REG,
  79                                  (s->state >> 8) & 0xff, regbase);
  80                if (mask & 0xff0000)
  81                        spriv->io(dev, 1, I8255_DATA_C_REG,
  82                                  (s->state >> 16) & 0xff, regbase);
  83        }
  84
  85        v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, regbase);
  86        v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, regbase) << 8);
  87        v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, regbase) << 16);
  88
  89        data[1] = v;
  90
  91        return insn->n;
  92}
  93
  94static void subdev_8255_do_config(struct comedi_device *dev,
  95                                  struct comedi_subdevice *s)
  96{
  97        struct subdev_8255_private *spriv = s->private;
  98        unsigned long regbase = spriv->regbase;
  99        int config;
 100
 101        config = I8255_CTRL_CW;
 102        /* 1 in io_bits indicates output, 1 in config indicates input */
 103        if (!(s->io_bits & 0x0000ff))
 104                config |= I8255_CTRL_A_IO;
 105        if (!(s->io_bits & 0x00ff00))
 106                config |= I8255_CTRL_B_IO;
 107        if (!(s->io_bits & 0x0f0000))
 108                config |= I8255_CTRL_C_LO_IO;
 109        if (!(s->io_bits & 0xf00000))
 110                config |= I8255_CTRL_C_HI_IO;
 111
 112        spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
 113}
 114
 115static int subdev_8255_insn_config(struct comedi_device *dev,
 116                                   struct comedi_subdevice *s,
 117                                   struct comedi_insn *insn,
 118                                   unsigned int *data)
 119{
 120        unsigned int chan = CR_CHAN(insn->chanspec);
 121        unsigned int mask;
 122        int ret;
 123
 124        if (chan < 8)
 125                mask = 0x0000ff;
 126        else if (chan < 16)
 127                mask = 0x00ff00;
 128        else if (chan < 20)
 129                mask = 0x0f0000;
 130        else
 131                mask = 0xf00000;
 132
 133        ret = comedi_dio_insn_config(dev, s, insn, data, mask);
 134        if (ret)
 135                return ret;
 136
 137        subdev_8255_do_config(dev, s);
 138
 139        return insn->n;
 140}
 141
 142static int __subdev_8255_init(struct comedi_device *dev,
 143                              struct comedi_subdevice *s,
 144                              int (*io)(struct comedi_device *dev,
 145                                        int dir, int port, int data,
 146                                        unsigned long regbase),
 147                              unsigned long regbase,
 148                              bool is_mmio)
 149{
 150        struct subdev_8255_private *spriv;
 151
 152        spriv = comedi_alloc_spriv(s, sizeof(*spriv));
 153        if (!spriv)
 154                return -ENOMEM;
 155
 156        if (io)
 157                spriv->io = io;
 158        else if (is_mmio)
 159                spriv->io = subdev_8255_mmio;
 160        else
 161                spriv->io = subdev_8255_io;
 162        spriv->regbase  = regbase;
 163
 164        s->type         = COMEDI_SUBD_DIO;
 165        s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
 166        s->n_chan       = 24;
 167        s->range_table  = &range_digital;
 168        s->maxdata      = 1;
 169        s->insn_bits    = subdev_8255_insn;
 170        s->insn_config  = subdev_8255_insn_config;
 171
 172        subdev_8255_do_config(dev, s);
 173
 174        return 0;
 175}
 176
 177/**
 178 * subdev_8255_init - initialize DIO subdevice for driving I/O mapped 8255
 179 * @dev: comedi device owning subdevice
 180 * @s: comedi subdevice to initialize
 181 * @io: (optional) register I/O call-back function
 182 * @regbase: offset of 8255 registers from dev->iobase, or call-back context
 183 *
 184 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
 185 *
 186 * If the optional I/O call-back function is provided, its prototype is of
 187 * the following form:
 188 *
 189 *   int my_8255_callback(struct comedi_device *dev, int dir, int port,
 190 *                        int data, unsigned long regbase);
 191 *
 192 * where 'dev', and 'regbase' match the values passed to this function,
 193 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
 194 * is the direction (0 for read, 1 for write) and 'data' is the value to be
 195 * written.  It should return 0 if writing or the value read if reading.
 196 *
 197 * If the optional I/O call-back function is not provided, an internal
 198 * call-back function is used which uses consecutive I/O port addresses
 199 * starting at dev->iobase + regbase.
 200 *
 201 * Return: -ENOMEM if failed to allocate memory, zero on success.
 202 */
 203int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
 204                     int (*io)(struct comedi_device *dev, int dir, int port,
 205                               int data, unsigned long regbase),
 206                     unsigned long regbase)
 207{
 208        return __subdev_8255_init(dev, s, io, regbase, false);
 209}
 210EXPORT_SYMBOL_GPL(subdev_8255_init);
 211
 212/**
 213 * subdev_8255_mm_init - initialize DIO subdevice for driving mmio-mapped 8255
 214 * @dev: comedi device owning subdevice
 215 * @s: comedi subdevice to initialize
 216 * @io: (optional) register I/O call-back function
 217 * @regbase: offset of 8255 registers from dev->mmio, or call-back context
 218 *
 219 * Initializes a comedi subdevice as a DIO subdevice driving an 8255 chip.
 220 *
 221 * If the optional I/O call-back function is provided, its prototype is of
 222 * the following form:
 223 *
 224 *   int my_8255_callback(struct comedi_device *dev, int dir, int port,
 225 *                        int data, unsigned long regbase);
 226 *
 227 * where 'dev', and 'regbase' match the values passed to this function,
 228 * 'port' is the 8255 port number 0 to 3 (including the control port), 'dir'
 229 * is the direction (0 for read, 1 for write) and 'data' is the value to be
 230 * written.  It should return 0 if writing or the value read if reading.
 231 *
 232 * If the optional I/O call-back function is not provided, an internal
 233 * call-back function is used which uses consecutive MMIO virtual addresses
 234 * starting at dev->mmio + regbase.
 235 *
 236 * Return: -ENOMEM if failed to allocate memory, zero on success.
 237 */
 238int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
 239                        int (*io)(struct comedi_device *dev, int dir, int port,
 240                                  int data, unsigned long regbase),
 241                        unsigned long regbase)
 242{
 243        return __subdev_8255_init(dev, s, io, regbase, true);
 244}
 245EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
 246
 247/**
 248 * subdev_8255_regbase - get offset of 8255 registers or call-back context
 249 * @s: comedi subdevice
 250 *
 251 * Returns the 'regbase' parameter that was previously passed to to
 252 * subdev_8255_init() or subdev_8255_mm_init() to set up the subdevice.
 253 * Only valid if the subdevice was set up successfully.
 254 */
 255unsigned long subdev_8255_regbase(struct comedi_subdevice *s)
 256{
 257        struct subdev_8255_private *spriv = s->private;
 258
 259        return spriv->regbase;
 260}
 261EXPORT_SYMBOL_GPL(subdev_8255_regbase);
 262
 263static int __init comedi_8255_module_init(void)
 264{
 265        return 0;
 266}
 267module_init(comedi_8255_module_init);
 268
 269static void __exit comedi_8255_module_exit(void)
 270{
 271}
 272module_exit(comedi_8255_module_exit);
 273
 274MODULE_AUTHOR("Comedi http://www.comedi.org");
 275MODULE_DESCRIPTION("Comedi: Generic 8255 digital I/O support");
 276MODULE_LICENSE("GPL");
 277