]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/ssv_dnp.c
Merge commit 'c039c332f23e794deb6d6f37b9f07ff3b27fb2cf' into md
[karo-tx-linux.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 /* This data structure holds information about the supported boards -------- */
54
55 struct dnp_board {
56         const char *name;
57         int ai_chans;
58         int ai_bits;
59         int have_dio;
60 };
61
62 /* Useful for shorthand access to the particular board structure ----------- */
63 #define thisboard ((const struct dnp_board *)dev->board_ptr)
64
65 /* This structure is for data unique to the DNP driver --------------------- */
66 struct dnp_private_data {
67
68 };
69
70 /* Shorthand macro for faster access to the private data ------------------- */
71 #define devpriv ((dnp_private *)dev->private)
72
73 /* ------------------------------------------------------------------------- */
74 /* The insn_bits interface allows packed reading/writing of DIO channels.    */
75 /* The comedi core can convert between insn_bits and insn_read/write, so you */
76 /* are able to use these instructions as well.                               */
77 /* ------------------------------------------------------------------------- */
78
79 static int dnp_dio_insn_bits(struct comedi_device *dev,
80                              struct comedi_subdevice *s,
81                              struct comedi_insn *insn, unsigned int *data)
82 {
83
84         if (insn->n != 2)
85                 return -EINVAL; /* insn uses data[0] and data[1]     */
86
87         /* The insn data is a mask in data[0] and the new data in data[1],   */
88         /* each channel cooresponding to a bit.                              */
89
90         /* Ports A and B are straight forward: each bit corresponds to an    */
91         /* output pin with the same order. Port C is different: bits 0...3   */
92         /* correspond to bits 4...7 of the output register (PCDR).           */
93
94         if (data[0]) {
95
96                 outb(PADR, CSCIR);
97                 outb((inb(CSCDR)
98                       & ~(u8) (data[0] & 0x0000FF))
99                      | (u8) (data[1] & 0x0000FF), CSCDR);
100
101                 outb(PBDR, CSCIR);
102                 outb((inb(CSCDR)
103                       & ~(u8) ((data[0] & 0x00FF00) >> 8))
104                      | (u8) ((data[1] & 0x00FF00) >> 8), CSCDR);
105
106                 outb(PCDR, CSCIR);
107                 outb((inb(CSCDR)
108                       & ~(u8) ((data[0] & 0x0F0000) >> 12))
109                      | (u8) ((data[1] & 0x0F0000) >> 12), CSCDR);
110         }
111
112         /* on return, data[1] contains the value of the digital input lines. */
113         outb(PADR, CSCIR);
114         data[0] = inb(CSCDR);
115         outb(PBDR, CSCIR);
116         data[0] += inb(CSCDR) << 8;
117         outb(PCDR, CSCIR);
118         data[0] += ((inb(CSCDR) & 0xF0) << 12);
119
120         return 2;
121
122 }
123
124 /* ------------------------------------------------------------------------- */
125 /* Configure the direction of the bidirectional digital i/o pins. chanspec   */
126 /* contains the channel to be changed and data[0] contains either            */
127 /* COMEDI_INPUT or COMEDI_OUTPUT.                                            */
128 /* ------------------------------------------------------------------------- */
129
130 static int dnp_dio_insn_config(struct comedi_device *dev,
131                                struct comedi_subdevice *s,
132                                struct comedi_insn *insn, unsigned int *data)
133 {
134
135         u8 register_buffer;
136
137         /* reduces chanspec to lower 16 bits */
138         int chan = CR_CHAN(insn->chanspec);
139
140         switch (data[0]) {
141         case INSN_CONFIG_DIO_OUTPUT:
142         case INSN_CONFIG_DIO_INPUT:
143                 break;
144         case INSN_CONFIG_DIO_QUERY:
145                 data[1] =
146                     (inb(CSCDR) & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
147                 return insn->n;
148                 break;
149         default:
150                 return -EINVAL;
151                 break;
152         }
153         /* Test: which port does the channel belong to?                       */
154
155         /* We have to pay attention with port C: this is the meaning of PCMR: */
156         /* Bit in PCMR:              7 6 5 4 3 2 1 0                          */
157         /* Corresponding port C pin: d 3 d 2 d 1 d 0   d= don't touch         */
158
159         if ((chan >= 0) && (chan <= 7)) {
160                 /* this is port A */
161                 outb(PAMR, CSCIR);
162         } else if ((chan >= 8) && (chan <= 15)) {
163                 /* this is port B */
164                 chan -= 8;
165                 outb(PBMR, CSCIR);
166         } else if ((chan >= 16) && (chan <= 19)) {
167                 /* this is port C; multiplication with 2 brings bits into     */
168                 /* correct position for PCMR!                                 */
169                 chan -= 16;
170                 chan *= 2;
171                 outb(PCMR, CSCIR);
172         } else {
173                 return -EINVAL;
174         }
175
176         /* read 'old' direction of the port and set bits (out=1, in=0)        */
177         register_buffer = inb(CSCDR);
178         if (data[0] == COMEDI_OUTPUT)
179                 register_buffer |= (1 << chan);
180         else
181                 register_buffer &= ~(1 << chan);
182
183         outb(register_buffer, CSCDR);
184
185         return 1;
186
187 }
188
189 static int dnp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
190 {
191         struct comedi_subdevice *s;
192
193         printk(KERN_INFO "comedi%d: dnp: ", dev->minor);
194
195         /* Autoprobing: this should find out which board we have. Currently  */
196         /* only the 1486 board is supported and autoprobing is not           */
197         /* implemented :-)                                                   */
198         /* dev->board_ptr = dnp_probe(dev); */
199
200         /* Initialize the name of the board.                                 */
201         /* We can use the "thisboard" macro now.                             */
202         dev->board_name = thisboard->name;
203
204         /* Allocate the private structure area. alloc_private() is a         */
205         /* convenient macro defined in comedidev.h.                          */
206         if (alloc_private(dev, sizeof(struct dnp_private_data)) < 0)
207                 return -ENOMEM;
208
209         /* Allocate the subdevice structures. alloc_subdevice() is a         */
210         /* convenient macro defined in comedidev.h.                          */
211
212         if (alloc_subdevices(dev, 1) < 0)
213                 return -ENOMEM;
214
215         s = dev->subdevices + 0;
216         /* digital i/o subdevice                                             */
217         s->type = COMEDI_SUBD_DIO;
218         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
219         s->n_chan = 20;
220         s->maxdata = 1;
221         s->range_table = &range_digital;
222         s->insn_bits = dnp_dio_insn_bits;
223         s->insn_config = dnp_dio_insn_config;
224
225         printk("attached\n");
226
227         /* We use the I/O ports 0x22,0x23 and 0xa3-0xa9, which are always
228          * allocated for the primary 8259, so we don't need to allocate them
229          * ourselves. */
230
231         /* configure all ports as input (default)                            */
232         outb(PAMR, CSCIR);
233         outb(0x00, CSCDR);
234         outb(PBMR, CSCIR);
235         outb(0x00, CSCDR);
236         outb(PCMR, CSCIR);
237         outb((inb(CSCDR) & 0xAA), CSCDR);
238
239         return 1;
240 }
241
242 static void dnp_detach(struct comedi_device *dev)
243 {
244         outb(PAMR, CSCIR);
245         outb(0x00, CSCDR);
246         outb(PBMR, CSCIR);
247         outb(0x00, CSCDR);
248         outb(PCMR, CSCIR);
249         outb((inb(CSCDR) & 0xAA), CSCDR);
250 }
251
252 static const struct dnp_board dnp_boards[] = {
253         {
254                 .name           = "dnp-1486",
255                 .ai_chans       = 16,
256                 .ai_bits        = 12,
257                 .have_dio       = 1,
258         },
259 };
260
261 static struct comedi_driver dnp_driver = {
262         .driver_name    = "ssv_dnp",
263         .module         = THIS_MODULE,
264         .attach         = dnp_attach,
265         .detach         = dnp_detach,
266         .board_name     = &dnp_boards[0].name,
267         .offset         = sizeof(struct dnp_board),
268         .num_names      = ARRAY_SIZE(dnp_boards),
269 };
270 module_comedi_driver(dnp_driver);
271
272 MODULE_AUTHOR("Comedi http://www.comedi.org");
273 MODULE_DESCRIPTION("Comedi low-level driver");
274 MODULE_LICENSE("GPL");