Merge tag 'edac_fixes_for_3.10' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / cb_pcimdda.c
1 /*
2     comedi/drivers/cb_pcimdda.c
3     Computer Boards PCIM-DDA06-16 Comedi driver
4     Author: Calin Culianu <calin@ajvar.org>
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: cb_pcimdda
26 Description: Measurement Computing PCIM-DDA06-16
27 Devices: [Measurement Computing] PCIM-DDA06-16 (cb_pcimdda)
28 Author: Calin Culianu <calin@ajvar.org>
29 Updated: Mon, 14 Apr 2008 15:15:51 +0100
30 Status: works
31
32 All features of the PCIM-DDA06-16 board are supported.  This board
33 has 6 16-bit AO channels, and the usual 8255 DIO setup.  (24 channels,
34 configurable in banks of 8 and 4, etc.).  This board does not support commands.
35
36 The board has a peculiar way of specifying AO gain/range settings -- You have
37 1 jumper bank on the card, which either makes all 6 AO channels either
38 5 Volt unipolar, 5V bipolar, 10 Volt unipolar or 10V bipolar.
39
40 Since there is absolutely _no_ way to tell in software how this jumper is set
41 (well, at least according  to the rather thin spec. from Measurement Computing
42  that comes with the board), the driver assumes the jumper is at its factory
43 default setting of +/-5V.
44
45 Also of note is the fact that this board features another jumper, whose
46 state is also completely invisible to software.  It toggles two possible AO
47 output modes on the board:
48
49   - Update Mode: Writing to an AO channel instantaneously updates the actual
50     signal output by the DAC on the board (this is the factory default).
51   - Simultaneous XFER Mode: Writing to an AO channel has no effect until
52     you read from any one of the AO channels.  This is useful for loading
53     all 6 AO values, and then reading from any one of the AO channels on the
54     device to instantly update all 6 AO values in unison.  Useful for some
55     control apps, I would assume?  If your jumper is in this setting, then you
56     need to issue your comedi_data_write()s to load all the values you want,
57     then issue one comedi_data_read() on any channel on the AO subdevice
58     to initiate the simultaneous XFER.
59
60 Configuration Options: not applicable, uses PCI auto config
61 */
62
63 /*
64     This is a driver for the Computer Boards PCIM-DDA06-16 Analog Output
65     card.  This board has a unique register layout and as such probably
66     deserves its own driver file.
67
68     It is theoretically possible to integrate this board into the cb_pcidda
69     file, but since that isn't my code, I didn't want to significantly
70     modify that file to support this board (I thought it impolite to do so).
71
72     At any rate, if you feel ambitious, please feel free to take
73     the code out of this file and combine it with a more unified driver
74     file.
75
76     I would like to thank Timothy Curry <Timothy.Curry@rdec.redstone.army.mil>
77     for lending me a board so that I could write this driver.
78
79     -Calin Culianu <calin@ajvar.org>
80  */
81
82 #include <linux/pci.h>
83
84 #include "../comedidev.h"
85
86 #include "8255.h"
87
88 /* device ids of the cards we support -- currently only 1 card supported */
89 #define PCI_ID_PCIM_DDA06_16            0x0053
90
91 /*
92  * Register map, 8-bit access only
93  */
94 #define PCIMDDA_DA_CHAN(x)              (0x00 + (x) * 2)
95 #define PCIMDDA_8255_BASE_REG           0x0c
96
97 #define MAX_AO_READBACK_CHANNELS        6
98
99 struct cb_pcimdda_private {
100         unsigned int ao_readback[MAX_AO_READBACK_CHANNELS];
101 };
102
103 static int cb_pcimdda_ao_winsn(struct comedi_device *dev,
104                                struct comedi_subdevice *s,
105                                struct comedi_insn *insn,
106                                unsigned int *data)
107 {
108         struct cb_pcimdda_private *devpriv = dev->private;
109         unsigned int chan = CR_CHAN(insn->chanspec);
110         unsigned long offset = dev->iobase + PCIMDDA_DA_CHAN(chan);
111         unsigned int val = 0;
112         int i;
113
114         for (i = 0; i < insn->n; i++) {
115                 val = data[i];
116
117                 /*
118                  * Write the LSB then MSB.
119                  *
120                  * If the simultaneous xfer mode is selected by the
121                  * jumper on the card, a read instruction is needed
122                  * in order to initiate the simultaneous transfer.
123                  * Otherwise, the DAC will be updated when the MSB
124                  * is written.
125                  */
126                 outb(val & 0x00ff, offset);
127                 outb((val >> 8) & 0x00ff, offset + 1);
128         }
129
130         /* Cache the last value for readback */
131         devpriv->ao_readback[chan] = val;
132
133         return insn->n;
134 }
135
136 static int cb_pcimdda_ao_rinsn(struct comedi_device *dev,
137                                struct comedi_subdevice *s,
138                                struct comedi_insn *insn,
139                                unsigned int *data)
140 {
141         struct cb_pcimdda_private *devpriv = dev->private;
142         int chan = CR_CHAN(insn->chanspec);
143         unsigned long offset = dev->iobase + PCIMDDA_DA_CHAN(chan);
144         int i;
145
146         for (i = 0; i < insn->n; i++) {
147                 /* Initiate the simultaneous transfer */
148                 inw(offset);
149
150                 data[i] = devpriv->ao_readback[chan];
151         }
152
153         return insn->n;
154 }
155
156 static int cb_pcimdda_auto_attach(struct comedi_device *dev,
157                                             unsigned long context_unused)
158 {
159         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
160         struct cb_pcimdda_private *devpriv;
161         struct comedi_subdevice *s;
162         int ret;
163
164         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
165         if (!devpriv)
166                 return -ENOMEM;
167         dev->private = devpriv;
168
169         ret = comedi_pci_enable(dev);
170         if (ret)
171                 return ret;
172         dev->iobase = pci_resource_start(pcidev, 3);
173
174         ret = comedi_alloc_subdevices(dev, 2);
175         if (ret)
176                 return ret;
177
178         s = &dev->subdevices[0];
179         /* analog output subdevice */
180         s->type         = COMEDI_SUBD_AO;
181         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
182         s->n_chan       = 6;
183         s->maxdata      = 0xffff;
184         s->range_table  = &range_bipolar5;
185         s->insn_write   = cb_pcimdda_ao_winsn;
186         s->insn_read    = cb_pcimdda_ao_rinsn;
187
188         s = &dev->subdevices[1];
189         /* digital i/o subdevice */
190         ret = subdev_8255_init(dev, s, NULL,
191                         dev->iobase + PCIMDDA_8255_BASE_REG);
192         if (ret)
193                 return ret;
194
195         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
196
197         return 1;
198 }
199
200 static void cb_pcimdda_detach(struct comedi_device *dev)
201 {
202         comedi_spriv_free(dev, 1);
203         comedi_pci_disable(dev);
204 }
205
206 static struct comedi_driver cb_pcimdda_driver = {
207         .driver_name    = "cb_pcimdda",
208         .module         = THIS_MODULE,
209         .auto_attach    = cb_pcimdda_auto_attach,
210         .detach         = cb_pcimdda_detach,
211 };
212
213 static int cb_pcimdda_pci_probe(struct pci_dev *dev,
214                                 const struct pci_device_id *id)
215 {
216         return comedi_pci_auto_config(dev, &cb_pcimdda_driver,
217                                       id->driver_data);
218 }
219
220 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdda_pci_table) = {
221         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_ID_PCIM_DDA06_16) },
222         { 0 }
223 };
224 MODULE_DEVICE_TABLE(pci, cb_pcimdda_pci_table);
225
226 static struct pci_driver cb_pcimdda_driver_pci_driver = {
227         .name           = "cb_pcimdda",
228         .id_table       = cb_pcimdda_pci_table,
229         .probe          = cb_pcimdda_pci_probe,
230         .remove         = comedi_pci_auto_unconfig,
231 };
232 module_comedi_pci_driver(cb_pcimdda_driver, cb_pcimdda_driver_pci_driver);
233
234 MODULE_AUTHOR("Calin A. Culianu <calin@rtlab.org>");
235 MODULE_DESCRIPTION("Comedi low-level driver for the Computerboards PCIM-DDA "
236                    "series.  Currently only supports PCIM-DDA06-16 (which "
237                    "also happens to be the only board in this series. :) ) ");
238 MODULE_LICENSE("GPL");