Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us