1*8ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2*8ffdff6aSGreg Kroah-Hartman /* 3*8ffdff6aSGreg Kroah-Hartman * ssv_dnp.c 4*8ffdff6aSGreg Kroah-Hartman * generic comedi driver for SSV Embedded Systems' DIL/Net-PCs 5*8ffdff6aSGreg Kroah-Hartman * Copyright (C) 2001 Robert Schwebel <robert@schwebel.de> 6*8ffdff6aSGreg Kroah-Hartman * 7*8ffdff6aSGreg Kroah-Hartman * COMEDI - Linux Control and Measurement Device Interface 8*8ffdff6aSGreg Kroah-Hartman * Copyright (C) 2000 David A. Schleef <ds@schleef.org> 9*8ffdff6aSGreg Kroah-Hartman */ 10*8ffdff6aSGreg Kroah-Hartman 11*8ffdff6aSGreg Kroah-Hartman /* 12*8ffdff6aSGreg Kroah-Hartman * Driver: ssv_dnp 13*8ffdff6aSGreg Kroah-Hartman * Description: SSV Embedded Systems DIL/Net-PC 14*8ffdff6aSGreg Kroah-Hartman * Author: Robert Schwebel <robert@schwebel.de> 15*8ffdff6aSGreg Kroah-Hartman * Devices: [SSV Embedded Systems] DIL/Net-PC 1486 (dnp-1486) 16*8ffdff6aSGreg Kroah-Hartman * Status: unknown 17*8ffdff6aSGreg Kroah-Hartman */ 18*8ffdff6aSGreg Kroah-Hartman 19*8ffdff6aSGreg Kroah-Hartman /* include files ----------------------------------------------------------- */ 20*8ffdff6aSGreg Kroah-Hartman 21*8ffdff6aSGreg Kroah-Hartman #include <linux/module.h> 22*8ffdff6aSGreg Kroah-Hartman #include "../comedidev.h" 23*8ffdff6aSGreg Kroah-Hartman 24*8ffdff6aSGreg Kroah-Hartman /* Some global definitions: the registers of the DNP ----------------------- */ 25*8ffdff6aSGreg Kroah-Hartman /* */ 26*8ffdff6aSGreg Kroah-Hartman /* For port A and B the mode register has bits corresponding to the output */ 27*8ffdff6aSGreg Kroah-Hartman /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits */ 28*8ffdff6aSGreg Kroah-Hartman /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits */ 29*8ffdff6aSGreg Kroah-Hartman /* 0..3 remain unchanged! For details about Port C Mode Register see */ 30*8ffdff6aSGreg Kroah-Hartman /* the remarks in dnp_insn_config() below. */ 31*8ffdff6aSGreg Kroah-Hartman 32*8ffdff6aSGreg Kroah-Hartman #define CSCIR 0x22 /* Chip Setup and Control Index Register */ 33*8ffdff6aSGreg Kroah-Hartman #define CSCDR 0x23 /* Chip Setup and Control Data Register */ 34*8ffdff6aSGreg Kroah-Hartman #define PAMR 0xa5 /* Port A Mode Register */ 35*8ffdff6aSGreg Kroah-Hartman #define PADR 0xa9 /* Port A Data Register */ 36*8ffdff6aSGreg Kroah-Hartman #define PBMR 0xa4 /* Port B Mode Register */ 37*8ffdff6aSGreg Kroah-Hartman #define PBDR 0xa8 /* Port B Data Register */ 38*8ffdff6aSGreg Kroah-Hartman #define PCMR 0xa3 /* Port C Mode Register */ 39*8ffdff6aSGreg Kroah-Hartman #define PCDR 0xa7 /* Port C Data Register */ 40*8ffdff6aSGreg Kroah-Hartman 41*8ffdff6aSGreg Kroah-Hartman static int dnp_dio_insn_bits(struct comedi_device *dev, 42*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 43*8ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 44*8ffdff6aSGreg Kroah-Hartman unsigned int *data) 45*8ffdff6aSGreg Kroah-Hartman { 46*8ffdff6aSGreg Kroah-Hartman unsigned int mask; 47*8ffdff6aSGreg Kroah-Hartman unsigned int val; 48*8ffdff6aSGreg Kroah-Hartman 49*8ffdff6aSGreg Kroah-Hartman /* 50*8ffdff6aSGreg Kroah-Hartman * Ports A and B are straight forward: each bit corresponds to an 51*8ffdff6aSGreg Kroah-Hartman * output pin with the same order. Port C is different: bits 0...3 52*8ffdff6aSGreg Kroah-Hartman * correspond to bits 4...7 of the output register (PCDR). 53*8ffdff6aSGreg Kroah-Hartman */ 54*8ffdff6aSGreg Kroah-Hartman 55*8ffdff6aSGreg Kroah-Hartman mask = comedi_dio_update_state(s, data); 56*8ffdff6aSGreg Kroah-Hartman if (mask) { 57*8ffdff6aSGreg Kroah-Hartman outb(PADR, CSCIR); 58*8ffdff6aSGreg Kroah-Hartman outb(s->state & 0xff, CSCDR); 59*8ffdff6aSGreg Kroah-Hartman 60*8ffdff6aSGreg Kroah-Hartman outb(PBDR, CSCIR); 61*8ffdff6aSGreg Kroah-Hartman outb((s->state >> 8) & 0xff, CSCDR); 62*8ffdff6aSGreg Kroah-Hartman 63*8ffdff6aSGreg Kroah-Hartman outb(PCDR, CSCIR); 64*8ffdff6aSGreg Kroah-Hartman val = inb(CSCDR) & 0x0f; 65*8ffdff6aSGreg Kroah-Hartman outb(((s->state >> 12) & 0xf0) | val, CSCDR); 66*8ffdff6aSGreg Kroah-Hartman } 67*8ffdff6aSGreg Kroah-Hartman 68*8ffdff6aSGreg Kroah-Hartman outb(PADR, CSCIR); 69*8ffdff6aSGreg Kroah-Hartman val = inb(CSCDR); 70*8ffdff6aSGreg Kroah-Hartman outb(PBDR, CSCIR); 71*8ffdff6aSGreg Kroah-Hartman val |= (inb(CSCDR) << 8); 72*8ffdff6aSGreg Kroah-Hartman outb(PCDR, CSCIR); 73*8ffdff6aSGreg Kroah-Hartman val |= ((inb(CSCDR) & 0xf0) << 12); 74*8ffdff6aSGreg Kroah-Hartman 75*8ffdff6aSGreg Kroah-Hartman data[1] = val; 76*8ffdff6aSGreg Kroah-Hartman 77*8ffdff6aSGreg Kroah-Hartman return insn->n; 78*8ffdff6aSGreg Kroah-Hartman } 79*8ffdff6aSGreg Kroah-Hartman 80*8ffdff6aSGreg Kroah-Hartman static int dnp_dio_insn_config(struct comedi_device *dev, 81*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s, 82*8ffdff6aSGreg Kroah-Hartman struct comedi_insn *insn, 83*8ffdff6aSGreg Kroah-Hartman unsigned int *data) 84*8ffdff6aSGreg Kroah-Hartman { 85*8ffdff6aSGreg Kroah-Hartman unsigned int chan = CR_CHAN(insn->chanspec); 86*8ffdff6aSGreg Kroah-Hartman unsigned int mask; 87*8ffdff6aSGreg Kroah-Hartman unsigned int val; 88*8ffdff6aSGreg Kroah-Hartman int ret; 89*8ffdff6aSGreg Kroah-Hartman 90*8ffdff6aSGreg Kroah-Hartman ret = comedi_dio_insn_config(dev, s, insn, data, 0); 91*8ffdff6aSGreg Kroah-Hartman if (ret) 92*8ffdff6aSGreg Kroah-Hartman return ret; 93*8ffdff6aSGreg Kroah-Hartman 94*8ffdff6aSGreg Kroah-Hartman if (chan < 8) { /* Port A */ 95*8ffdff6aSGreg Kroah-Hartman mask = 1 << chan; 96*8ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR); 97*8ffdff6aSGreg Kroah-Hartman } else if (chan < 16) { /* Port B */ 98*8ffdff6aSGreg Kroah-Hartman mask = 1 << (chan - 8); 99*8ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR); 100*8ffdff6aSGreg Kroah-Hartman } else { /* Port C */ 101*8ffdff6aSGreg Kroah-Hartman /* 102*8ffdff6aSGreg Kroah-Hartman * We have to pay attention with port C. 103*8ffdff6aSGreg Kroah-Hartman * This is the meaning of PCMR: 104*8ffdff6aSGreg Kroah-Hartman * Bit in PCMR: 7 6 5 4 3 2 1 0 105*8ffdff6aSGreg Kroah-Hartman * Corresponding port C pin: d 3 d 2 d 1 d 0 d= don't touch 106*8ffdff6aSGreg Kroah-Hartman * 107*8ffdff6aSGreg Kroah-Hartman * Multiplication by 2 brings bits into correct position 108*8ffdff6aSGreg Kroah-Hartman * for PCMR! 109*8ffdff6aSGreg Kroah-Hartman */ 110*8ffdff6aSGreg Kroah-Hartman mask = 1 << ((chan - 16) * 2); 111*8ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR); 112*8ffdff6aSGreg Kroah-Hartman } 113*8ffdff6aSGreg Kroah-Hartman 114*8ffdff6aSGreg Kroah-Hartman val = inb(CSCDR); 115*8ffdff6aSGreg Kroah-Hartman if (data[0] == COMEDI_OUTPUT) 116*8ffdff6aSGreg Kroah-Hartman val |= mask; 117*8ffdff6aSGreg Kroah-Hartman else 118*8ffdff6aSGreg Kroah-Hartman val &= ~mask; 119*8ffdff6aSGreg Kroah-Hartman outb(val, CSCDR); 120*8ffdff6aSGreg Kroah-Hartman 121*8ffdff6aSGreg Kroah-Hartman return insn->n; 122*8ffdff6aSGreg Kroah-Hartman } 123*8ffdff6aSGreg Kroah-Hartman 124*8ffdff6aSGreg Kroah-Hartman static int dnp_attach(struct comedi_device *dev, struct comedi_devconfig *it) 125*8ffdff6aSGreg Kroah-Hartman { 126*8ffdff6aSGreg Kroah-Hartman struct comedi_subdevice *s; 127*8ffdff6aSGreg Kroah-Hartman int ret; 128*8ffdff6aSGreg Kroah-Hartman 129*8ffdff6aSGreg Kroah-Hartman /* 130*8ffdff6aSGreg Kroah-Hartman * We use I/O ports 0x22, 0x23 and 0xa3-0xa9, which are always 131*8ffdff6aSGreg Kroah-Hartman * allocated for the primary 8259, so we don't need to allocate 132*8ffdff6aSGreg Kroah-Hartman * them ourselves. 133*8ffdff6aSGreg Kroah-Hartman */ 134*8ffdff6aSGreg Kroah-Hartman 135*8ffdff6aSGreg Kroah-Hartman ret = comedi_alloc_subdevices(dev, 1); 136*8ffdff6aSGreg Kroah-Hartman if (ret) 137*8ffdff6aSGreg Kroah-Hartman return ret; 138*8ffdff6aSGreg Kroah-Hartman 139*8ffdff6aSGreg Kroah-Hartman s = &dev->subdevices[0]; 140*8ffdff6aSGreg Kroah-Hartman /* digital i/o subdevice */ 141*8ffdff6aSGreg Kroah-Hartman s->type = COMEDI_SUBD_DIO; 142*8ffdff6aSGreg Kroah-Hartman s->subdev_flags = SDF_READABLE | SDF_WRITABLE; 143*8ffdff6aSGreg Kroah-Hartman s->n_chan = 20; 144*8ffdff6aSGreg Kroah-Hartman s->maxdata = 1; 145*8ffdff6aSGreg Kroah-Hartman s->range_table = &range_digital; 146*8ffdff6aSGreg Kroah-Hartman s->insn_bits = dnp_dio_insn_bits; 147*8ffdff6aSGreg Kroah-Hartman s->insn_config = dnp_dio_insn_config; 148*8ffdff6aSGreg Kroah-Hartman 149*8ffdff6aSGreg Kroah-Hartman /* configure all ports as input (default) */ 150*8ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR); 151*8ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR); 152*8ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR); 153*8ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR); 154*8ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR); 155*8ffdff6aSGreg Kroah-Hartman outb((inb(CSCDR) & 0xAA), CSCDR); 156*8ffdff6aSGreg Kroah-Hartman 157*8ffdff6aSGreg Kroah-Hartman return 0; 158*8ffdff6aSGreg Kroah-Hartman } 159*8ffdff6aSGreg Kroah-Hartman 160*8ffdff6aSGreg Kroah-Hartman static void dnp_detach(struct comedi_device *dev) 161*8ffdff6aSGreg Kroah-Hartman { 162*8ffdff6aSGreg Kroah-Hartman outb(PAMR, CSCIR); 163*8ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR); 164*8ffdff6aSGreg Kroah-Hartman outb(PBMR, CSCIR); 165*8ffdff6aSGreg Kroah-Hartman outb(0x00, CSCDR); 166*8ffdff6aSGreg Kroah-Hartman outb(PCMR, CSCIR); 167*8ffdff6aSGreg Kroah-Hartman outb((inb(CSCDR) & 0xAA), CSCDR); 168*8ffdff6aSGreg Kroah-Hartman } 169*8ffdff6aSGreg Kroah-Hartman 170*8ffdff6aSGreg Kroah-Hartman static struct comedi_driver dnp_driver = { 171*8ffdff6aSGreg Kroah-Hartman .driver_name = "dnp-1486", 172*8ffdff6aSGreg Kroah-Hartman .module = THIS_MODULE, 173*8ffdff6aSGreg Kroah-Hartman .attach = dnp_attach, 174*8ffdff6aSGreg Kroah-Hartman .detach = dnp_detach, 175*8ffdff6aSGreg Kroah-Hartman }; 176*8ffdff6aSGreg Kroah-Hartman module_comedi_driver(dnp_driver); 177*8ffdff6aSGreg Kroah-Hartman 178*8ffdff6aSGreg Kroah-Hartman MODULE_AUTHOR("Comedi https://www.comedi.org"); 179*8ffdff6aSGreg Kroah-Hartman MODULE_DESCRIPTION("Comedi low-level driver"); 180*8ffdff6aSGreg Kroah-Hartman MODULE_LICENSE("GPL"); 181