Linux-libre 3.10.72-gnu
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / ssv_dnp.c
1 /*
2     comedi/drivers/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     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 */
24 /*
25 Driver: ssv_dnp
26 Description: SSV Embedded Systems DIL/Net-PC
27 Author: Robert Schwebel <robert@schwebel.de>
28 Devices: [SSV Embedded Systems] DIL/Net-PC 1486 (dnp-1486)
29 Status: unknown
30 */
31
32 /* include files ----------------------------------------------------------- */
33
34 #include "../comedidev.h"
35
36 /* Some global definitions: the registers of the DNP ----------------------- */
37 /*                                                                           */
38 /* For port A and B the mode register has bits corresponding to the output   */
39 /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits       */
40 /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits  */
41 /* 0..3 remain unchanged! For details about Port C Mode Register see         */
42 /* the remarks in dnp_insn_config() below.                                   */
43
44 #define CSCIR 0x22              /* Chip Setup and Control Index Register     */
45 #define CSCDR 0x23              /* Chip Setup and Control Data Register      */
46 #define PAMR  0xa5              /* Port A Mode Register                      */
47 #define PADR  0xa9              /* Port A Data Register                      */
48 #define PBMR  0xa4              /* Port B Mode Register                      */
49 #define PBDR  0xa8              /* Port B Data Register                      */
50 #define PCMR  0xa3              /* Port C Mode Register                      */
51 #define PCDR  0xa7              /* Port C Data Register                      */
52
53 /* ------------------------------------------------------------------------- */
54 /* The insn_bits interface allows packed reading/writing of DIO channels.    */
55 /* The comedi core can convert between insn_bits and insn_read/write, so you */
56 /* are able to use these instructions as well.                               */
57 /* ------------------------------------------------------------------------- */
58
59 static int dnp_dio_insn_bits(struct comedi_device *dev,
60                              struct comedi_subdevice *s,
61                              struct comedi_insn *insn, unsigned int *data)
62 {
63         /* The insn data is a mask in data[0] and the new data in data[1],   */
64         /* each channel cooresponding to a bit.                              */
65
66         /* Ports A and B are straight forward: each bit corresponds to an    */
67         /* output pin with the same order. Port C is different: bits 0...3   */
68         /* correspond to bits 4...7 of the output register (PCDR).           */
69
70         if (data[0]) {
71
72                 outb(PADR, CSCIR);
73                 outb((inb(CSCDR)
74                       & ~(u8) (data[0] & 0x0000FF))
75                      | (u8) (data[1] & 0x0000FF), CSCDR);
76
77                 outb(PBDR, CSCIR);
78                 outb((inb(CSCDR)
79                       & ~(u8) ((data[0] & 0x00FF00) >> 8))
80                      | (u8) ((data[1] & 0x00FF00) >> 8), CSCDR);
81
82                 outb(PCDR, CSCIR);
83                 outb((inb(CSCDR)
84                       & ~(u8) ((data[0] & 0x0F0000) >> 12))
85                      | (u8) ((data[1] & 0x0F0000) >> 12), CSCDR);
86         }
87
88         /* on return, data[1] contains the value of the digital input lines. */
89         outb(PADR, CSCIR);
90         data[1] = inb(CSCDR);
91         outb(PBDR, CSCIR);
92         data[1] += inb(CSCDR) << 8;
93         outb(PCDR, CSCIR);
94         data[1] += ((inb(CSCDR) & 0xF0) << 12);
95
96         return insn->n;
97
98 }
99
100 /* ------------------------------------------------------------------------- */
101 /* Configure the direction of the bidirectional digital i/o pins. chanspec   */
102 /* contains the channel to be changed and data[0] contains either            */
103 /* COMEDI_INPUT or COMEDI_OUTPUT.                                            */
104 /* ------------------------------------------------------------------------- */
105
106 static int dnp_dio_insn_config(struct comedi_device *dev,
107                                struct comedi_subdevice *s,
108                                struct comedi_insn *insn, unsigned int *data)
109 {
110
111         u8 register_buffer;
112
113         /* reduces chanspec to lower 16 bits */
114         int chan = CR_CHAN(insn->chanspec);
115
116         switch (data[0]) {
117         case INSN_CONFIG_DIO_OUTPUT:
118         case INSN_CONFIG_DIO_INPUT:
119                 break;
120         case INSN_CONFIG_DIO_QUERY:
121                 data[1] =
122                     (inb(CSCDR) & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
123                 return insn->n;
124                 break;
125         default:
126                 return -EINVAL;
127                 break;
128         }
129         /* Test: which port does the channel belong to?                       */
130
131         /* We have to pay attention with port C: this is the meaning of PCMR: */
132         /* Bit in PCMR:              7 6 5 4 3 2 1 0                          */
133         /* Corresponding port C pin: d 3 d 2 d 1 d 0   d= don't touch         */
134
135         if ((chan >= 0) && (chan <= 7)) {
136                 /* this is port A */
137                 outb(PAMR, CSCIR);
138         } else if ((chan >= 8) && (chan <= 15)) {
139                 /* this is port B */
140                 chan -= 8;
141                 outb(PBMR, CSCIR);
142         } else if ((chan >= 16) && (chan <= 19)) {
143                 /* this is port C; multiplication with 2 brings bits into     */
144                 /* correct position for PCMR!                                 */
145                 chan -= 16;
146                 chan *= 2;
147                 outb(PCMR, CSCIR);
148         } else {
149                 return -EINVAL;
150         }
151
152         /* read 'old' direction of the port and set bits (out=1, in=0)        */
153         register_buffer = inb(CSCDR);
154         if (data[0] == COMEDI_OUTPUT)
155                 register_buffer |= (1 << chan);
156         else
157                 register_buffer &= ~(1 << chan);
158
159         outb(register_buffer, CSCDR);
160
161         return 1;
162
163 }
164
165 static int dnp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
166 {
167         struct comedi_subdevice *s;
168         int ret;
169
170         ret = comedi_alloc_subdevices(dev, 1);
171         if (ret)
172                 return ret;
173
174         s = &dev->subdevices[0];
175         /* digital i/o subdevice                                             */
176         s->type = COMEDI_SUBD_DIO;
177         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
178         s->n_chan = 20;
179         s->maxdata = 1;
180         s->range_table = &range_digital;
181         s->insn_bits = dnp_dio_insn_bits;
182         s->insn_config = dnp_dio_insn_config;
183
184         /* We use the I/O ports 0x22,0x23 and 0xa3-0xa9, which are always
185          * allocated for the primary 8259, so we don't need to allocate them
186          * ourselves. */
187
188         /* configure all ports as input (default)                            */
189         outb(PAMR, CSCIR);
190         outb(0x00, CSCDR);
191         outb(PBMR, CSCIR);
192         outb(0x00, CSCDR);
193         outb(PCMR, CSCIR);
194         outb((inb(CSCDR) & 0xAA), CSCDR);
195
196         dev_info(dev->class_dev, "%s: attached\n", dev->board_name);
197         return 1;
198 }
199
200 static void dnp_detach(struct comedi_device *dev)
201 {
202         outb(PAMR, CSCIR);
203         outb(0x00, CSCDR);
204         outb(PBMR, CSCIR);
205         outb(0x00, CSCDR);
206         outb(PCMR, CSCIR);
207         outb((inb(CSCDR) & 0xAA), CSCDR);
208 }
209
210 static struct comedi_driver dnp_driver = {
211         .driver_name    = "dnp-1486",
212         .module         = THIS_MODULE,
213         .attach         = dnp_attach,
214         .detach         = dnp_detach,
215 };
216 module_comedi_driver(dnp_driver);
217
218 MODULE_AUTHOR("Comedi http://www.comedi.org");
219 MODULE_DESCRIPTION("Comedi low-level driver");
220 MODULE_LICENSE("GPL");