]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/cb_pcidda.c
Merge tag 'ext4_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso...
[karo-tx-linux.git] / drivers / staging / comedi / drivers / cb_pcidda.c
1 /*
2  * comedi/drivers/cb_pcidda.c
3  * Driver for the ComputerBoards / MeasurementComputing PCI-DDA series.
4  *
5  * Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
6  * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
7  *
8  * COMEDI - Linux Control and Measurement Device Interface
9  * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 /*
27  * Driver: cb_pcidda
28  * Description: MeasurementComputing PCI-DDA series
29  * Devices: (Measurement Computing) PCI-DDA08/12 [pci-dda08/12]
30  *          (Measurement Computing) PCI-DDA04/12 [pci-dda04/12]
31  *          (Measurement Computing) PCI-DDA02/12 [pci-dda02/12]
32  *          (Measurement Computing) PCI-DDA08/16 [pci-dda08/16]
33  *          (Measurement Computing) PCI-DDA04/16 [pci-dda04/16]
34  *          (Measurement Computing) PCI-DDA02/16 [pci-dda02/16]
35  * Author: Ivan Martinez <ivanmr@altavista.com>
36  *         Frank Mori Hess <fmhess@users.sourceforge.net>
37  * Status: works
38  *
39  * Configuration options: not applicable, uses PCI auto config
40  *
41  * Only simple analog output writing is supported.
42  */
43
44 #include <linux/pci.h>
45
46 #include "../comedidev.h"
47
48 #include "comedi_fc.h"
49 #include "8255.h"
50
51 /*
52  * ComputerBoards PCI Device ID's supported by this driver
53  */
54 #define PCI_DEVICE_ID_DDA02_12          0x0020
55 #define PCI_DEVICE_ID_DDA04_12          0x0021
56 #define PCI_DEVICE_ID_DDA08_12          0x0022
57 #define PCI_DEVICE_ID_DDA02_16          0x0023
58 #define PCI_DEVICE_ID_DDA04_16          0x0024
59 #define PCI_DEVICE_ID_DDA08_16          0x0025
60
61 #define EEPROM_SIZE     128     /*  number of entries in eeprom */
62 /* maximum number of ao channels for supported boards */
63 #define MAX_AO_CHANNELS 8
64
65 /* Digital I/O registers */
66 #define CB_DDA_DIO0_8255_BASE           0x00
67 #define CB_DDA_DIO1_8255_BASE           0x04
68
69 /* DAC registers */
70 #define CB_DDA_DA_CTRL_REG              0x00       /* D/A Control Register  */
71 #define CB_DDA_DA_CTRL_SU               (1 << 0)   /*  Simultaneous update  */
72 #define CB_DDA_DA_CTRL_EN               (1 << 1)   /*  Enable specified DAC */
73 #define CB_DDA_DA_CTRL_DAC(x)           ((x) << 2) /*  Specify DAC channel  */
74 #define CB_DDA_DA_CTRL_RANGE2V5         (0 << 6)   /*  2.5V range           */
75 #define CB_DDA_DA_CTRL_RANGE5V          (2 << 6)   /*  5V range             */
76 #define CB_DDA_DA_CTRL_RANGE10V         (3 << 6)   /*  10V range            */
77 #define CB_DDA_DA_CTRL_UNIP             (1 << 8)   /*  Unipolar range       */
78
79 #define DACALIBRATION1  4       /*  D/A CALIBRATION REGISTER 1 */
80 /* write bits */
81 /* serial data input for eeprom, caldacs, reference dac */
82 #define SERIAL_IN_BIT   0x1
83 #define CAL_CHANNEL_MASK        (0x7 << 1)
84 #define CAL_CHANNEL_BITS(channel)       (((channel) << 1) & CAL_CHANNEL_MASK)
85 /* read bits */
86 #define CAL_COUNTER_MASK        0x1f
87 /* calibration counter overflow status bit */
88 #define CAL_COUNTER_OVERFLOW_BIT        0x20
89 /* analog output is less than reference dac voltage */
90 #define AO_BELOW_REF_BIT        0x40
91 #define SERIAL_OUT_BIT  0x80    /*  serial data out, for reading from eeprom */
92
93 #define DACALIBRATION2  6       /*  D/A CALIBRATION REGISTER 2 */
94 #define SELECT_EEPROM_BIT       0x1     /*  send serial data in to eeprom */
95 /* don't send serial data to MAX542 reference dac */
96 #define DESELECT_REF_DAC_BIT    0x2
97 /* don't send serial data to caldac n */
98 #define DESELECT_CALDAC_BIT(n)  (0x4 << (n))
99 /* manual says to set this bit with no explanation */
100 #define DUMMY_BIT       0x40
101
102 #define CB_DDA_DA_DATA_REG(x)           (0x08 + ((x) * 2))
103
104 /* Offsets for the caldac channels */
105 #define CB_DDA_CALDAC_FINE_GAIN         0
106 #define CB_DDA_CALDAC_COURSE_GAIN       1
107 #define CB_DDA_CALDAC_COURSE_OFFSET     2
108 #define CB_DDA_CALDAC_FINE_OFFSET       3
109
110 static const struct comedi_lrange cb_pcidda_ranges = {
111         6, {
112                 BIP_RANGE(10),
113                 BIP_RANGE(5),
114                 BIP_RANGE(2.5),
115                 UNI_RANGE(10),
116                 UNI_RANGE(5),
117                 UNI_RANGE(2.5)
118         }
119 };
120
121 struct cb_pcidda_board {
122         const char *name;
123         unsigned short device_id;
124         int ao_chans;
125         int ao_bits;
126 };
127
128 static const struct cb_pcidda_board cb_pcidda_boards[] = {
129         {
130                 .name           = "pci-dda02/12",
131                 .device_id      = PCI_DEVICE_ID_DDA02_12,
132                 .ao_chans       = 2,
133                 .ao_bits        = 12,
134         }, {
135                 .name           = "pci-dda04/12",
136                 .device_id      = PCI_DEVICE_ID_DDA04_12,
137                 .ao_chans       = 4,
138                 .ao_bits        = 12,
139         }, {
140                 .name           = "pci-dda08/12",
141                 .device_id      = PCI_DEVICE_ID_DDA08_12,
142                 .ao_chans       = 8,
143                 .ao_bits        = 12,
144         }, {
145                 .name           = "pci-dda02/16",
146                 .device_id      = PCI_DEVICE_ID_DDA02_16,
147                 .ao_chans       = 2,
148                 .ao_bits        = 16,
149         }, {
150                 .name           = "pci-dda04/16",
151                 .device_id      = PCI_DEVICE_ID_DDA04_16,
152                 .ao_chans       = 4,
153                 .ao_bits        = 16,
154         }, {
155                 .name           = "pci-dda08/16",
156                 .device_id      = PCI_DEVICE_ID_DDA08_16,
157                 .ao_chans       = 8,
158                 .ao_bits        = 16,
159         },
160 };
161
162 struct cb_pcidda_private {
163         /* bits last written to da calibration register 1 */
164         unsigned int dac_cal1_bits;
165         /* current range settings for output channels */
166         unsigned int ao_range[MAX_AO_CHANNELS];
167         u16 eeprom_data[EEPROM_SIZE];   /*  software copy of board's eeprom */
168 };
169
170 /* lowlevel read from eeprom */
171 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
172 {
173         unsigned int value = 0;
174         int i;
175         const int value_width = 16;     /*  number of bits wide values are */
176
177         for (i = 1; i <= value_width; i++) {
178                 /*  read bits most significant bit first */
179                 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
180                         value |= 1 << (value_width - i);
181         }
182
183         return value;
184 }
185
186 /* lowlevel write to eeprom/dac */
187 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
188                                  unsigned int num_bits)
189 {
190         struct cb_pcidda_private *devpriv = dev->private;
191         int i;
192
193         for (i = 1; i <= num_bits; i++) {
194                 /*  send bits most significant bit first */
195                 if (value & (1 << (num_bits - i)))
196                         devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
197                 else
198                         devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
199                 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
200         }
201 }
202
203 /* reads a 16 bit value from board's eeprom */
204 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
205                                           unsigned int address)
206 {
207         unsigned int i;
208         unsigned int cal2_bits;
209         unsigned int value;
210         /* one caldac for every two dac channels */
211         const int max_num_caldacs = 4;
212         /* bits to send to tell eeprom we want to read */
213         const int read_instruction = 0x6;
214         const int instruction_length = 3;
215         const int address_length = 8;
216
217         /*  send serial output stream to eeprom */
218         cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
219         /*  deactivate caldacs (one caldac for every two channels) */
220         for (i = 0; i < max_num_caldacs; i++)
221                 cal2_bits |= DESELECT_CALDAC_BIT(i);
222         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
223
224         /*  tell eeprom we want to read */
225         cb_pcidda_serial_out(dev, read_instruction, instruction_length);
226         /*  send address we want to read from */
227         cb_pcidda_serial_out(dev, address, address_length);
228
229         value = cb_pcidda_serial_in(dev);
230
231         /*  deactivate eeprom */
232         cal2_bits &= ~SELECT_EEPROM_BIT;
233         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
234
235         return value;
236 }
237
238 /* writes to 8 bit calibration dacs */
239 static void cb_pcidda_write_caldac(struct comedi_device *dev,
240                                    unsigned int caldac, unsigned int channel,
241                                    unsigned int value)
242 {
243         unsigned int cal2_bits;
244         unsigned int i;
245         /* caldacs use 3 bit channel specification */
246         const int num_channel_bits = 3;
247         const int num_caldac_bits = 8;  /*  8 bit calibration dacs */
248         /* one caldac for every two dac channels */
249         const int max_num_caldacs = 4;
250
251         /* write 3 bit channel */
252         cb_pcidda_serial_out(dev, channel, num_channel_bits);
253         /*  write 8 bit caldac value */
254         cb_pcidda_serial_out(dev, value, num_caldac_bits);
255
256 /*
257 * latch stream into appropriate caldac deselect reference dac
258 */
259         cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
260         /*  deactivate caldacs (one caldac for every two channels) */
261         for (i = 0; i < max_num_caldacs; i++)
262                 cal2_bits |= DESELECT_CALDAC_BIT(i);
263         /*  activate the caldac we want */
264         cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
265         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
266         /*  deactivate caldac */
267         cal2_bits |= DESELECT_CALDAC_BIT(caldac);
268         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
269 }
270
271 /* set caldacs to eeprom values for given channel and range */
272 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
273                                 unsigned int range)
274 {
275         struct cb_pcidda_private *devpriv = dev->private;
276         unsigned int caldac = channel / 2;      /* two caldacs per channel */
277         unsigned int chan = 4 * (channel % 2);  /* caldac channel base */
278         unsigned int index = 2 * range + 12 * channel;
279         unsigned int offset;
280         unsigned int gain;
281
282         /* save range so we can tell when we need to readjust calibration */
283         devpriv->ao_range[channel] = range;
284
285         /* get values from eeprom data */
286         offset = devpriv->eeprom_data[0x7 + index];
287         gain = devpriv->eeprom_data[0x8 + index];
288
289         /* set caldacs */
290         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_OFFSET,
291                                (offset >> 8) & 0xff);
292         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_OFFSET,
293                                offset & 0xff);
294         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_GAIN,
295                                (gain >> 8) & 0xff);
296         cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_GAIN,
297                                gain & 0xff);
298 }
299
300 static int cb_pcidda_ao_insn_write(struct comedi_device *dev,
301                                    struct comedi_subdevice *s,
302                                    struct comedi_insn *insn,
303                                    unsigned int *data)
304 {
305         struct cb_pcidda_private *devpriv = dev->private;
306         unsigned int channel = CR_CHAN(insn->chanspec);
307         unsigned int range = CR_RANGE(insn->chanspec);
308         unsigned int ctrl;
309
310         if (range != devpriv->ao_range[channel])
311                 cb_pcidda_calibrate(dev, channel, range);
312
313         ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
314
315         switch (range) {
316         case 0:
317         case 3:
318                 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
319                 break;
320         case 1:
321         case 4:
322                 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
323                 break;
324         case 2:
325         case 5:
326                 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
327                 break;
328         }
329
330         if (range > 2)
331                 ctrl |= CB_DDA_DA_CTRL_UNIP;
332
333         outw(ctrl, dev->iobase + CB_DDA_DA_CTRL_REG);
334
335         outw(data[0], dev->iobase + CB_DDA_DA_DATA_REG(channel));
336
337         return insn->n;
338 }
339
340 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
341                                             struct pci_dev *pcidev)
342 {
343         const struct cb_pcidda_board *thisboard;
344         int i;
345
346         for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
347                 thisboard = &cb_pcidda_boards[i];
348                 if (thisboard->device_id != pcidev->device)
349                         return thisboard;
350         }
351         return NULL;
352 }
353
354 static int cb_pcidda_auto_attach(struct comedi_device *dev,
355                                            unsigned long context_unused)
356 {
357         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
358         const struct cb_pcidda_board *thisboard;
359         struct cb_pcidda_private *devpriv;
360         struct comedi_subdevice *s;
361         unsigned long iobase_8255;
362         int i;
363         int ret;
364
365         thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
366         if (!thisboard)
367                 return -ENODEV;
368         dev->board_ptr = thisboard;
369         dev->board_name = thisboard->name;
370
371         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
372         if (!devpriv)
373                 return -ENOMEM;
374         dev->private = devpriv;
375
376         ret = comedi_pci_enable(pcidev, dev->board_name);
377         if (ret)
378                 return ret;
379         dev->iobase = pci_resource_start(pcidev, 3);
380         iobase_8255 = pci_resource_start(pcidev, 2);
381
382         ret = comedi_alloc_subdevices(dev, 3);
383         if (ret)
384                 return ret;
385
386         s = &dev->subdevices[0];
387         /* analog output subdevice */
388         s->type = COMEDI_SUBD_AO;
389         s->subdev_flags = SDF_WRITABLE;
390         s->n_chan = thisboard->ao_chans;
391         s->maxdata = (1 << thisboard->ao_bits) - 1;
392         s->range_table = &cb_pcidda_ranges;
393         s->insn_write = cb_pcidda_ao_insn_write;
394
395         /* two 8255 digital io subdevices */
396         for (i = 0; i < 2; i++) {
397                 s = &dev->subdevices[1 + i];
398                 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
399                 if (ret)
400                         return ret;
401         }
402
403         /* Read the caldac eeprom data */
404         for (i = 0; i < EEPROM_SIZE; i++)
405                 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
406
407         /*  set calibrations dacs */
408         for (i = 0; i < thisboard->ao_chans; i++)
409                 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
410
411         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
412
413         return 0;
414 }
415
416 static void cb_pcidda_detach(struct comedi_device *dev)
417 {
418         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
419
420         if (dev->subdevices) {
421                 subdev_8255_cleanup(dev, &dev->subdevices[1]);
422                 subdev_8255_cleanup(dev, &dev->subdevices[2]);
423         }
424         if (pcidev) {
425                 if (dev->iobase)
426                         comedi_pci_disable(pcidev);
427         }
428 }
429
430 static struct comedi_driver cb_pcidda_driver = {
431         .driver_name    = "cb_pcidda",
432         .module         = THIS_MODULE,
433         .auto_attach    = cb_pcidda_auto_attach,
434         .detach         = cb_pcidda_detach,
435 };
436
437 static int cb_pcidda_pci_probe(struct pci_dev *dev,
438                                          const struct pci_device_id *ent)
439 {
440         return comedi_pci_auto_config(dev, &cb_pcidda_driver);
441 }
442
443 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
444         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
445         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
446         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
447         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
448         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
449         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
450         { 0 }
451 };
452 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
453
454 static struct pci_driver cb_pcidda_pci_driver = {
455         .name           = "cb_pcidda",
456         .id_table       = cb_pcidda_pci_table,
457         .probe          = cb_pcidda_pci_probe,
458         .remove         = comedi_pci_auto_unconfig,
459 };
460 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
461
462 MODULE_AUTHOR("Comedi http://www.comedi.org");
463 MODULE_DESCRIPTION("Comedi low-level driver");
464 MODULE_LICENSE("GPL");