Merge tag 'metag-for-v3.13' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[cascardo/linux.git] / drivers / staging / comedi / drivers / adl_pci6208.c
1 /*
2  * adl_pci6208.c
3  * Comedi driver for ADLink 6208 series cards
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 /*
20  * Driver: adl_pci6208
21  * Description: ADLink PCI-6208/6216 Series Multi-channel Analog Output Cards
22  * Devices: (ADLink) PCI-6208 [adl_pci6208]
23  *          (ADLink) PCI-6216 [adl_pci6216]
24  * Author: nsyeow <nsyeow@pd.jaring.my>
25  * Updated: Fri, 30 Jan 2004 14:44:27 +0800
26  * Status: untested
27  *
28  * Configuration Options: not applicable, uses PCI auto config
29  */
30
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/pci.h>
34
35 #include "../comedidev.h"
36
37 /*
38  * PCI-6208/6216-GL register map
39  */
40 #define PCI6208_AO_CONTROL(x)           (0x00 + (2 * (x)))
41 #define PCI6208_AO_STATUS               0x00
42 #define PCI6208_AO_STATUS_DATA_SEND     (1 << 0)
43 #define PCI6208_DIO                     0x40
44 #define PCI6208_DIO_DO_MASK             (0x0f)
45 #define PCI6208_DIO_DO_SHIFT            (0)
46 #define PCI6208_DIO_DI_MASK             (0xf0)
47 #define PCI6208_DIO_DI_SHIFT            (4)
48
49 #define PCI6208_MAX_AO_CHANNELS         16
50
51 enum pci6208_boardid {
52         BOARD_PCI6208,
53         BOARD_PCI6216,
54 };
55
56 struct pci6208_board {
57         const char *name;
58         int ao_chans;
59 };
60
61 static const struct pci6208_board pci6208_boards[] = {
62         [BOARD_PCI6208] = {
63                 .name           = "adl_pci6208",
64                 .ao_chans       = 8,
65         },
66         [BOARD_PCI6216] = {
67                 .name           = "adl_pci6216",
68                 .ao_chans       = 16,
69         },
70 };
71
72 struct pci6208_private {
73         unsigned int ao_readback[PCI6208_MAX_AO_CHANNELS];
74 };
75
76 static int pci6208_ao_wait_for_data_send(struct comedi_device *dev,
77                                          unsigned int timeout)
78 {
79         unsigned int status;
80
81         while (timeout--) {
82                 status = inw(dev->iobase + PCI6208_AO_STATUS);
83                 if ((status & PCI6208_AO_STATUS_DATA_SEND) == 0)
84                         return 0;
85                 udelay(1);
86         }
87
88         return -ETIME;
89 }
90
91 static int pci6208_ao_insn_write(struct comedi_device *dev,
92                                  struct comedi_subdevice *s,
93                                  struct comedi_insn *insn,
94                                  unsigned int *data)
95 {
96         struct pci6208_private *devpriv = dev->private;
97         unsigned int chan = CR_CHAN(insn->chanspec);
98         unsigned int val = devpriv->ao_readback[chan];
99         int ret;
100         int i;
101
102         for (i = 0; i < insn->n; i++) {
103                 val = data[i];
104
105                 /* D/A transfer rate is 2.2us, wait up to 10us */
106                 ret = pci6208_ao_wait_for_data_send(dev, 10);
107                 if (ret)
108                         return ret;
109
110                 /* the hardware expects two's complement values */
111                 outw(comedi_offset_munge(s, val),
112                      dev->iobase + PCI6208_AO_CONTROL(chan));
113         }
114         devpriv->ao_readback[chan] = val;
115
116         return insn->n;
117 }
118
119 static int pci6208_ao_insn_read(struct comedi_device *dev,
120                                 struct comedi_subdevice *s,
121                                 struct comedi_insn *insn,
122                                 unsigned int *data)
123 {
124         struct pci6208_private *devpriv = dev->private;
125         unsigned int chan = CR_CHAN(insn->chanspec);
126         int i;
127
128         for (i = 0; i < insn->n; i++)
129                 data[i] = devpriv->ao_readback[chan];
130
131         return insn->n;
132 }
133
134 static int pci6208_di_insn_bits(struct comedi_device *dev,
135                                 struct comedi_subdevice *s,
136                                 struct comedi_insn *insn,
137                                 unsigned int *data)
138 {
139         unsigned int val;
140
141         val = inw(dev->iobase + PCI6208_DIO);
142         val = (val & PCI6208_DIO_DI_MASK) >> PCI6208_DIO_DI_SHIFT;
143
144         data[1] = val;
145
146         return insn->n;
147 }
148
149 static int pci6208_do_insn_bits(struct comedi_device *dev,
150                                 struct comedi_subdevice *s,
151                                 struct comedi_insn *insn,
152                                 unsigned int *data)
153 {
154         if (comedi_dio_update_state(s, data))
155                 outw(s->state, dev->iobase + PCI6208_DIO);
156
157         data[1] = s->state;
158
159         return insn->n;
160 }
161
162 static int pci6208_auto_attach(struct comedi_device *dev,
163                                unsigned long context)
164 {
165         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
166         const struct pci6208_board *boardinfo = NULL;
167         struct pci6208_private *devpriv;
168         struct comedi_subdevice *s;
169         unsigned int val;
170         int ret;
171
172         if (context < ARRAY_SIZE(pci6208_boards))
173                 boardinfo = &pci6208_boards[context];
174         if (!boardinfo)
175                 return -ENODEV;
176         dev->board_ptr = boardinfo;
177         dev->board_name = boardinfo->name;
178
179         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
180         if (!devpriv)
181                 return -ENOMEM;
182
183         ret = comedi_pci_enable(dev);
184         if (ret)
185                 return ret;
186         dev->iobase = pci_resource_start(pcidev, 2);
187
188         ret = comedi_alloc_subdevices(dev, 3);
189         if (ret)
190                 return ret;
191
192         s = &dev->subdevices[0];
193         /* analog output subdevice */
194         s->type         = COMEDI_SUBD_AO;
195         s->subdev_flags = SDF_WRITABLE;
196         s->n_chan       = boardinfo->ao_chans;
197         s->maxdata      = 0xffff;
198         s->range_table  = &range_bipolar10;
199         s->insn_write   = pci6208_ao_insn_write;
200         s->insn_read    = pci6208_ao_insn_read;
201
202         s = &dev->subdevices[1];
203         /* digital input subdevice */
204         s->type         = COMEDI_SUBD_DI;
205         s->subdev_flags = SDF_READABLE;
206         s->n_chan       = 4;
207         s->maxdata      = 1;
208         s->range_table  = &range_digital;
209         s->insn_bits    = pci6208_di_insn_bits;
210
211         s = &dev->subdevices[2];
212         /* digital output subdevice */
213         s->type         = COMEDI_SUBD_DO;
214         s->subdev_flags = SDF_WRITABLE;
215         s->n_chan       = 4;
216         s->maxdata      = 1;
217         s->range_table  = &range_digital;
218         s->insn_bits    = pci6208_do_insn_bits;
219
220         /*
221          * Get the read back signals from the digital outputs
222          * and save it as the initial state for the subdevice.
223          */
224         val = inw(dev->iobase + PCI6208_DIO);
225         val = (val & PCI6208_DIO_DO_MASK) >> PCI6208_DIO_DO_SHIFT;
226         s->state        = val;
227
228         return 0;
229 }
230
231 static struct comedi_driver adl_pci6208_driver = {
232         .driver_name    = "adl_pci6208",
233         .module         = THIS_MODULE,
234         .auto_attach    = pci6208_auto_attach,
235         .detach         = comedi_pci_disable,
236 };
237
238 static int adl_pci6208_pci_probe(struct pci_dev *dev,
239                                  const struct pci_device_id *id)
240 {
241         return comedi_pci_auto_config(dev, &adl_pci6208_driver,
242                                       id->driver_data);
243 }
244
245 static DEFINE_PCI_DEVICE_TABLE(adl_pci6208_pci_table) = {
246         { PCI_VDEVICE(ADLINK, 0x6208), BOARD_PCI6208 },
247         { PCI_VDEVICE(ADLINK, 0x6216), BOARD_PCI6216 },
248         { 0 }
249 };
250 MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
251
252 static struct pci_driver adl_pci6208_pci_driver = {
253         .name           = "adl_pci6208",
254         .id_table       = adl_pci6208_pci_table,
255         .probe          = adl_pci6208_pci_probe,
256         .remove         = comedi_pci_auto_unconfig,
257 };
258 module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
259
260 MODULE_AUTHOR("Comedi http://www.comedi.org");
261 MODULE_DESCRIPTION("Comedi driver for ADLink 6208 series cards");
262 MODULE_LICENSE("GPL");