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