18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman * ssv_dnp.c
48ffdff6aSGreg Kroah-Hartman * generic comedi driver for SSV Embedded Systems' DIL/Net-PCs
58ffdff6aSGreg Kroah-Hartman * Copyright (C) 2001 Robert Schwebel <robert@schwebel.de>
68ffdff6aSGreg Kroah-Hartman *
78ffdff6aSGreg Kroah-Hartman * COMEDI - Linux Control and Measurement Device Interface
88ffdff6aSGreg Kroah-Hartman * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
98ffdff6aSGreg Kroah-Hartman */
108ffdff6aSGreg Kroah-Hartman
118ffdff6aSGreg Kroah-Hartman /*
128ffdff6aSGreg Kroah-Hartman * Driver: ssv_dnp
138ffdff6aSGreg Kroah-Hartman * Description: SSV Embedded Systems DIL/Net-PC
148ffdff6aSGreg Kroah-Hartman * Author: Robert Schwebel <robert@schwebel.de>
158ffdff6aSGreg Kroah-Hartman * Devices: [SSV Embedded Systems] DIL/Net-PC 1486 (dnp-1486)
168ffdff6aSGreg Kroah-Hartman * Status: unknown
178ffdff6aSGreg Kroah-Hartman */
188ffdff6aSGreg Kroah-Hartman
198ffdff6aSGreg Kroah-Hartman /* include files ----------------------------------------------------------- */
208ffdff6aSGreg Kroah-Hartman
218ffdff6aSGreg Kroah-Hartman #include <linux/module.h>
22*df0e68c1SIan Abbott #include <linux/comedi/comedidev.h>
238ffdff6aSGreg Kroah-Hartman
248ffdff6aSGreg Kroah-Hartman /* Some global definitions: the registers of the DNP ----------------------- */
258ffdff6aSGreg Kroah-Hartman /* */
268ffdff6aSGreg Kroah-Hartman /* For port A and B the mode register has bits corresponding to the output */
278ffdff6aSGreg Kroah-Hartman /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits */
288ffdff6aSGreg Kroah-Hartman /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits */
298ffdff6aSGreg Kroah-Hartman /* 0..3 remain unchanged! For details about Port C Mode Register see */
308ffdff6aSGreg Kroah-Hartman /* the remarks in dnp_insn_config() below. */
318ffdff6aSGreg Kroah-Hartman
328ffdff6aSGreg Kroah-Hartman #define CSCIR 0x22 /* Chip Setup and Control Index Register */
338ffdff6aSGreg Kroah-Hartman #define CSCDR 0x23 /* Chip Setup and Control Data Register */
348ffdff6aSGreg Kroah-Hartman #define PAMR 0xa5 /* Port A Mode Register */
358ffdff6aSGreg Kroah-Hartman #define PADR 0xa9 /* Port A Data Register */
368ffdff6aSGreg Kroah-Hartman #define PBMR 0xa4 /* Port B Mode Register */
378ffdff6aSGreg Kroah-Hartman #define PBDR 0xa8 /* Port B Data Register */
388ffdff6aSGreg Kroah-Hartman #define PCMR 0xa3 /* Port C Mode Register */
398ffdff6aSGreg Kroah-Hartman #define PCDR 0xa7 /* Port C Data Register */
408ffdff6aSGreg Kroah-Hartman
dnp_dio_insn_bits(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)418ffdff6aSGreg Kroah-Hartman static int dnp_dio_insn_bits(struct comedi_device *dev,
428ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
438ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
448ffdff6aSGreg Kroah-Hartman unsigned int *data)
458ffdff6aSGreg Kroah-Hartman {
468ffdff6aSGreg Kroah-Hartman unsigned int mask;
478ffdff6aSGreg Kroah-Hartman unsigned int val;
488ffdff6aSGreg Kroah-Hartman
498ffdff6aSGreg Kroah-Hartman /*
508ffdff6aSGreg Kroah-Hartman * Ports A and B are straight forward: each bit corresponds to an
518ffdff6aSGreg Kroah-Hartman * output pin with the same order. Port C is different: bits 0...3
528ffdff6aSGreg Kroah-Hartman * correspond to bits 4...7 of the output register (PCDR).
538ffdff6aSGreg Kroah-Hartman */
548ffdff6aSGreg Kroah-Hartman
558ffdff6aSGreg Kroah-Hartman mask = comedi_dio_update_state(s, data);
568ffdff6aSGreg Kroah-Hartman if (mask) {
578ffdff6aSGreg Kroah-Hartman outb(PADR, CSCIR);
588ffdff6aSGreg Kroah-Hartman outb(s->state & 0xff, CSCDR);
598ffdff6aSGreg Kroah-Hartman
608ffdff6aSGreg Kroah-Hartman outb(PBDR, CSCIR);
618ffdff6aSGreg Kroah-Hartman outb((s->state >> 8) & 0xff, CSCDR);
628ffdff6aSGreg Kroah-Hartman
638ffdff6aSGreg Kroah-Hartman outb(PCDR, CSCIR);
648ffdff6aSGreg Kroah-Hartman val = inb(CSCDR) & 0x0f;
658ffdff6aSGreg Kroah-Hartman outb(((s->state >> 12) & 0xf0) | val, CSCDR);
668ffdff6aSGreg Kroah-Hartman }
678ffdff6aSGreg Kroah-Hartman
688ffdff6aSGreg Kroah-Hartman outb(PADR, CSCIR);
698ffdff6aSGreg Kroah-Hartman val = inb(CSCDR);
708ffdff6aSGreg Kroah-Hartman outb(PBDR, CSCIR);
718ffdff6aSGreg Kroah-Hartman val |= (inb(CSCDR) << 8);
728ffdff6aSGreg Kroah-Hartman outb(PCDR, CSCIR);
738ffdff6aSGreg Kroah-Hartman val |= ((inb(CSCDR) & 0xf0) << 12);
748ffdff6aSGreg Kroah-Hartman
758ffdff6aSGreg Kroah-Hartman data[1] = val;
768ffdff6aSGreg Kroah-Hartman
778ffdff6aSGreg Kroah-Hartman return insn->n;
788ffdff6aSGreg Kroah-Hartman }
798ffdff6aSGreg Kroah-Hartman
dnp_dio_insn_config(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)808ffdff6aSGreg Kroah-Hartman static int dnp_dio_insn_config(struct comedi_device *dev,
818ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s,
828ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn,
838ffdff6aSGreg Kroah-Hartman unsigned int *data)
848ffdff6aSGreg Kroah-Hartman {
858ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec);
868ffdff6aSGreg Kroah-Hartman unsigned int mask;
878ffdff6aSGreg Kroah-Hartman unsigned int val;
888ffdff6aSGreg Kroah-Hartman int ret;
898ffdff6aSGreg Kroah-Hartman
908ffdff6aSGreg Kroah-Hartman ret = comedi_dio_insn_config(dev, s, insn, data, 0);
918ffdff6aSGreg Kroah-Hartman if (ret)
928ffdff6aSGreg Kroah-Hartman return ret;
938ffdff6aSGreg Kroah-Hartman
948ffdff6aSGreg Kroah-Hartman if (chan < 8) { /* Port A */
958ffdff6aSGreg Kroah-Hartman mask = 1 << chan;
968ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR);
978ffdff6aSGreg Kroah-Hartman } else if (chan < 16) { /* Port B */
988ffdff6aSGreg Kroah-Hartman mask = 1 << (chan - 8);
998ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR);
1008ffdff6aSGreg Kroah-Hartman } else { /* Port C */
1018ffdff6aSGreg Kroah-Hartman /*
1028ffdff6aSGreg Kroah-Hartman * We have to pay attention with port C.
1038ffdff6aSGreg Kroah-Hartman * This is the meaning of PCMR:
1048ffdff6aSGreg Kroah-Hartman * Bit in PCMR: 7 6 5 4 3 2 1 0
1058ffdff6aSGreg Kroah-Hartman * Corresponding port C pin: d 3 d 2 d 1 d 0 d= don't touch
1068ffdff6aSGreg Kroah-Hartman *
1078ffdff6aSGreg Kroah-Hartman * Multiplication by 2 brings bits into correct position
1088ffdff6aSGreg Kroah-Hartman * for PCMR!
1098ffdff6aSGreg Kroah-Hartman */
1108ffdff6aSGreg Kroah-Hartman mask = 1 << ((chan - 16) * 2);
1118ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR);
1128ffdff6aSGreg Kroah-Hartman }
1138ffdff6aSGreg Kroah-Hartman
1148ffdff6aSGreg Kroah-Hartman val = inb(CSCDR);
1158ffdff6aSGreg Kroah-Hartman if (data[0] == COMEDI_OUTPUT)
1168ffdff6aSGreg Kroah-Hartman val |= mask;
1178ffdff6aSGreg Kroah-Hartman else
1188ffdff6aSGreg Kroah-Hartman val &= ~mask;
1198ffdff6aSGreg Kroah-Hartman outb(val, CSCDR);
1208ffdff6aSGreg Kroah-Hartman
1218ffdff6aSGreg Kroah-Hartman return insn->n;
1228ffdff6aSGreg Kroah-Hartman }
1238ffdff6aSGreg Kroah-Hartman
dnp_attach(struct comedi_device * dev,struct comedi_devconfig * it)1248ffdff6aSGreg Kroah-Hartman static int dnp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1258ffdff6aSGreg Kroah-Hartman {
1268ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s;
1278ffdff6aSGreg Kroah-Hartman int ret;
1288ffdff6aSGreg Kroah-Hartman
1298ffdff6aSGreg Kroah-Hartman /*
1308ffdff6aSGreg Kroah-Hartman * We use I/O ports 0x22, 0x23 and 0xa3-0xa9, which are always
1318ffdff6aSGreg Kroah-Hartman * allocated for the primary 8259, so we don't need to allocate
1328ffdff6aSGreg Kroah-Hartman * them ourselves.
1338ffdff6aSGreg Kroah-Hartman */
1348ffdff6aSGreg Kroah-Hartman
1358ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, 1);
1368ffdff6aSGreg Kroah-Hartman if (ret)
1378ffdff6aSGreg Kroah-Hartman return ret;
1388ffdff6aSGreg Kroah-Hartman
1398ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[0];
1408ffdff6aSGreg Kroah-Hartman /* digital i/o subdevice */
1418ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_DIO;
1428ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1438ffdff6aSGreg Kroah-Hartman s->n_chan = 20;
1448ffdff6aSGreg Kroah-Hartman s->maxdata = 1;
1458ffdff6aSGreg Kroah-Hartman s->range_table = &range_digital;
1468ffdff6aSGreg Kroah-Hartman s->insn_bits = dnp_dio_insn_bits;
1478ffdff6aSGreg Kroah-Hartman s->insn_config = dnp_dio_insn_config;
1488ffdff6aSGreg Kroah-Hartman
1498ffdff6aSGreg Kroah-Hartman /* configure all ports as input (default) */
1508ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR);
1518ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR);
1528ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR);
1538ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR);
1548ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR);
1558ffdff6aSGreg Kroah-Hartman outb((inb(CSCDR) & 0xAA), CSCDR);
1568ffdff6aSGreg Kroah-Hartman
1578ffdff6aSGreg Kroah-Hartman return 0;
1588ffdff6aSGreg Kroah-Hartman }
1598ffdff6aSGreg Kroah-Hartman
dnp_detach(struct comedi_device * dev)1608ffdff6aSGreg Kroah-Hartman static void dnp_detach(struct comedi_device *dev)
1618ffdff6aSGreg Kroah-Hartman {
1628ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR);
1638ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR);
1648ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR);
1658ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR);
1668ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR);
1678ffdff6aSGreg Kroah-Hartman outb((inb(CSCDR) & 0xAA), CSCDR);
1688ffdff6aSGreg Kroah-Hartman }
1698ffdff6aSGreg Kroah-Hartman
1708ffdff6aSGreg Kroah-Hartman static struct comedi_driver dnp_driver = {
1718ffdff6aSGreg Kroah-Hartman .driver_name = "dnp-1486",
1728ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE,
1738ffdff6aSGreg Kroah-Hartman .attach = dnp_attach,
1748ffdff6aSGreg Kroah-Hartman .detach = dnp_detach,
1758ffdff6aSGreg Kroah-Hartman };
1768ffdff6aSGreg Kroah-Hartman module_comedi_driver(dnp_driver);
1778ffdff6aSGreg Kroah-Hartman
1788ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org");
1798ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi low-level driver");
1808ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL");
181