added poc driver
[comedi.git] / comedi / drivers / poc.c
1 /*
2     comedi/drivers/poc.c
3     Mini-drivers for POC (Piece of Crap) boards
4     Copyright (C) 2000 Frank Mori Hess <fmhess@uiuc.edu>
5     Copyright (C) 2001 David A. Schleef <ds@schleef.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 /*
22  * This driver is a kind of miscellaneous driver for very
23  * simple ISA boards.  
24  */
25 /*
26     dac02 - Keithley DAC-02 analog output board driver
27
28 The card's ranges are set by the wiring you use.  The driver only
29 uses the range specified to decide whether or not to take the
30 complement of the data before sending it to the card (since the
31 bipolar outputs of the card are inverted.)
32
33 */
34
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/sched.h>
38 #include <linux/mm.h>
39 #include <linux/malloc.h>
40 #include <linux/errno.h>
41 #include <linux/ioport.h>
42 #include <linux/delay.h>
43 #include <linux/interrupt.h>
44 #include <linux/timex.h>
45 #include <linux/timer.h>
46 #include <asm/io.h>
47 #include <linux/comedidev.h>
48
49 /* DAC-02 registers */
50 #define DAC02_LSB(a)    (2 * a)
51 #define DAC02_MSB(a)    (2 * a + 1)
52
53 static int poc_attach(comedi_device *dev,comedi_devconfig *it);
54 static int poc_detach(comedi_device *dev);
55 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
56 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data);
57 static int poc_recognize(const char *name);
58
59 struct boarddef_struct{
60         char *name;
61         int iosize;
62         int (*setup)(comedi_device *);
63         int type;
64         int n_chan;
65         int n_bits;
66         int (*winsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
67         int (*rinsn)(comedi_device *,comedi_subdevice *,comedi_insn *,lsampl_t *);
68 };
69 static struct boarddef_struct boards[]={
70         {
71         name:           "dac02",
72         size:           8,
73         setup:          dac02_setup,
74         type:           COMEDI_SUBD_AO,
75         n_chan:         2,
76         n_bits:         12,
77         winsn:          dac02_ao_winsn,
78         rinsn:          readback_insn,
79         }
80 };
81 #define n_boards ((sizeof(boards)/sizeof(boards[0]))
82
83 comedi_driver driver_poc=
84 {
85         driver_name:    "poc",
86         module:         THIS_MODULE,
87         attach:         poc_attach,
88         detach:         poc_detach,
89         recognize:      poc_recognize,
90 };
91
92 // analog output ranges
93 static comedi_lrange range_dac02 = {
94         4,
95         {
96                 RANGE( -5, 5 ),
97                 RANGE( -10, 10 ),
98                 RANGE( 0, 5 ),
99                 RANGE( 0, 10 ),
100         }
101 };
102
103 static int poc_recognize(const char *name)
104 {
105         int i;
106
107         for(i=0;i<n_boards;i++){
108                 if(!strcmp(boards[i].name,name))
109                         return i;
110         }
111
112         return -1;
113 }
114
115 static int dac02_attach(comedi_device *dev, comedi_devconfig *it)
116 {
117         comedi_subdevice *s;
118         int iosize, iobase;
119
120         printk("comedi%d: poc: using %s iobase 0x%x\n", dev->minor,
121                 boards[dev->board].name, iobase);
122
123         dev->board_name = boards[dev->board].name;
124
125         if(iobase == 0)
126         {
127                 printk("io base address required\n");
128                 return -EINVAL;
129         }
130
131         iobase = it->options[0];
132         iosize = boards[dev->board].name;
133         /* check if io addresses are available */
134         if(check_region(iobase, iosize) < 0)
135         {
136                 printk("I/O port conflict: failed to allocate ports 0x%x to 0x%x\n",
137                         iobase, iobase + iosize - 1);
138                 return -EIO;
139         }
140         request_region(iobase, iosize, "dac02");
141         dev->iobase = iobase;
142         dev->iosize = iosize;
143
144         dev->n_subdevices = 1;
145         if(alloc_subdevices(dev) < 0)
146                 return -ENOMEM;
147         if(alloc_private(dev,sizeof(lsampl_t)*boards[dev->board].n_chan) < 0)
148                 return -ENOMEM;
149
150         /* analog output subdevice */
151         s=dev->subdevices + 0;
152         s->type = boards[dev->board].type;
153         s->n_chan = boards[dev->board].n_chan;
154         s->maxdata = (1<<boards[dev->board].n_bits)-1;
155         s->range_table = &range_dac02; // XXX
156         s->insn_write = boards[dev->board].winsn;
157         s->insn_write = boards[dev->board].rinsn;
158         if(s->type==COMEDI_SUBD_AO || s->type==COMEDI_SUBD_DO){
159                 s->subdev_flags = SDF_WRITEABLE;
160         }
161
162         return 0;
163 }
164
165 static int poc_detach(comedi_device *dev)
166 {
167         /* only free stuff if it has been allocated by _attach */
168         if(dev->iobase)
169                 release_region(dev->iobase, dev->iosize);
170
171         printk("comedi%d: dac02: remove\n", dev->minor);
172
173         return 0;
174 }
175
176 static int readback_insn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
177 {
178         int temp;
179         int chan;
180         int output;
181
182         chan = CR_CHAN(insn->chanspec);
183         data[0]=((lsampl_t *)dev->private)[chan];
184
185         return 1;
186 }
187
188 static int dac02_ao_winsn(comedi_device *dev,comedi_subdevice *s,comedi_insn *insn,lsampl_t *data)
189 {
190         int temp;
191         int chan;
192         int output;
193
194         chan = CR_CHAN(insn->chanspec);
195         ((lsampl_t *)dev->private)[chan] = data[0];
196         output = data[0];
197 #if wrong
198         // convert to complementary binary if range is bipolar
199         if((CR_RANGE(insn->chanspec) & 0x2) == 0)
200                 output = ~output;
201 #endif
202         temp = (output << 4) & 0xf0;
203         outb(temp, dev->iobase + DAC02_LSB(chan));
204         temp = (output >> 4) & 0xff;
205         outb(temp, dev->iobase + DAC02_MSB(chan));
206
207         return 1;
208 }
209
210 COMEDI_INITCLEANUP(driver_poc);
211