]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/cb_pcidda.c
f1aa9c9431574f81e535ca97a1636cb2b7700a3a
[~andy/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  * 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 "../comedidev.h"
45
46 #include "comedi_fc.h"
47 #include "8255.h"
48
49 /*
50  * ComputerBoards PCI Device ID's supported by this driver
51  */
52 #define PCI_DEVICE_ID_DDA02_12          0x0020
53 #define PCI_DEVICE_ID_DDA04_12          0x0021
54 #define PCI_DEVICE_ID_DDA08_12          0x0022
55 #define PCI_DEVICE_ID_DDA02_16          0x0023
56 #define PCI_DEVICE_ID_DDA04_16          0x0024
57 #define PCI_DEVICE_ID_DDA08_16          0x0025
58
59 #define EEPROM_SIZE     128     /*  number of entries in eeprom */
60 /* maximum number of ao channels for supported boards */
61 #define MAX_AO_CHANNELS 8
62
63 /* Digital I/O registers */
64 #define PORT1A 0                /*  PORT 1A DATA */
65
66 #define PORT1B 1                /*  PORT 1B DATA */
67
68 #define PORT1C 2                /*  PORT 1C DATA */
69
70 #define CONTROL1 3              /*  CONTROL REGISTER 1 */
71
72 #define PORT2A 4                /*  PORT 2A DATA */
73
74 #define PORT2B 5                /*  PORT 2B DATA */
75
76 #define PORT2C 6                /*  PORT 2C DATA */
77
78 #define CONTROL2 7              /*  CONTROL REGISTER 2 */
79
80 /* DAC registers */
81 #define DACONTROL       0       /*  D/A CONTROL REGISTER */
82 #define SU      0000001         /*  Simultaneous update enabled */
83 #define NOSU    0000000         /*  Simultaneous update disabled */
84 #define ENABLEDAC       0000002 /*  Enable specified DAC */
85 #define DISABLEDAC      0000000 /*  Disable specified DAC */
86 #define RANGE2V5        0000000 /*  2.5V */
87 #define RANGE5V 0000200         /*  5V */
88 #define RANGE10V        0000300 /*  10V */
89 #define UNIP    0000400         /*  Unipolar outputs */
90 #define BIP     0000000         /*  Bipolar outputs */
91
92 #define DACALIBRATION1  4       /*  D/A CALIBRATION REGISTER 1 */
93 /* write bits */
94 /* serial data input for eeprom, caldacs, reference dac */
95 #define SERIAL_IN_BIT   0x1
96 #define CAL_CHANNEL_MASK        (0x7 << 1)
97 #define CAL_CHANNEL_BITS(channel)       (((channel) << 1) & CAL_CHANNEL_MASK)
98 /* read bits */
99 #define CAL_COUNTER_MASK        0x1f
100 /* calibration counter overflow status bit */
101 #define CAL_COUNTER_OVERFLOW_BIT        0x20
102 /* analog output is less than reference dac voltage */
103 #define AO_BELOW_REF_BIT        0x40
104 #define SERIAL_OUT_BIT  0x80    /*  serial data out, for reading from eeprom */
105
106 #define DACALIBRATION2  6       /*  D/A CALIBRATION REGISTER 2 */
107 #define SELECT_EEPROM_BIT       0x1     /*  send serial data in to eeprom */
108 /* don't send serial data to MAX542 reference dac */
109 #define DESELECT_REF_DAC_BIT    0x2
110 /* don't send serial data to caldac n */
111 #define DESELECT_CALDAC_BIT(n)  (0x4 << (n))
112 /* manual says to set this bit with no explanation */
113 #define DUMMY_BIT       0x40
114
115 #define DADATA  8               /*  FIRST D/A DATA REGISTER (0) */
116
117 static const struct comedi_lrange cb_pcidda_ranges = {
118         6, {
119                 BIP_RANGE(10),
120                 BIP_RANGE(5),
121                 BIP_RANGE(2.5),
122                 UNI_RANGE(10),
123                 UNI_RANGE(5),
124                 UNI_RANGE(2.5)
125         }
126 };
127
128 struct cb_pcidda_board {
129         const char *name;
130         unsigned short device_id;
131         int ao_chans;
132         int ao_bits;
133 };
134
135 static const struct cb_pcidda_board cb_pcidda_boards[] = {
136         {
137                 .name           = "pci-dda02/12",
138                 .device_id      = PCI_DEVICE_ID_DDA02_12,
139                 .ao_chans       = 2,
140                 .ao_bits        = 12,
141         }, {
142                 .name           = "pci-dda04/12",
143                 .device_id      = PCI_DEVICE_ID_DDA04_12,
144                 .ao_chans       = 4,
145                 .ao_bits        = 12,
146         }, {
147                 .name           = "pci-dda08/12",
148                 .device_id      = PCI_DEVICE_ID_DDA08_12,
149                 .ao_chans       = 8,
150                 .ao_bits        = 12,
151         }, {
152                 .name           = "pci-dda02/16",
153                 .device_id      = PCI_DEVICE_ID_DDA02_16,
154                 .ao_chans       = 2,
155                 .ao_bits        = 16,
156         }, {
157                 .name           = "pci-dda04/16",
158                 .device_id      = PCI_DEVICE_ID_DDA04_16,
159                 .ao_chans       = 4,
160                 .ao_bits        = 16,
161         }, {
162                 .name           = "pci-dda08/16",
163                 .device_id      = PCI_DEVICE_ID_DDA08_16,
164                 .ao_chans       = 8,
165                 .ao_bits        = 16,
166         },
167 };
168
169 struct cb_pcidda_private {
170         /* bits last written to da calibration register 1 */
171         unsigned int dac_cal1_bits;
172         /* current range settings for output channels */
173         unsigned int ao_range[MAX_AO_CHANNELS];
174         u16 eeprom_data[EEPROM_SIZE];   /*  software copy of board's eeprom */
175 };
176
177 /* lowlevel read from eeprom */
178 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
179 {
180         unsigned int value = 0;
181         int i;
182         const int value_width = 16;     /*  number of bits wide values are */
183
184         for (i = 1; i <= value_width; i++) {
185                 /*  read bits most significant bit first */
186                 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
187                         value |= 1 << (value_width - i);
188         }
189
190         return value;
191 }
192
193 /* lowlevel write to eeprom/dac */
194 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
195                                  unsigned int num_bits)
196 {
197         struct cb_pcidda_private *devpriv = dev->private;
198         int i;
199
200         for (i = 1; i <= num_bits; i++) {
201                 /*  send bits most significant bit first */
202                 if (value & (1 << (num_bits - i)))
203                         devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
204                 else
205                         devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
206                 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
207         }
208 }
209
210 /* reads a 16 bit value from board's eeprom */
211 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
212                                           unsigned int address)
213 {
214         unsigned int i;
215         unsigned int cal2_bits;
216         unsigned int value;
217         /* one caldac for every two dac channels */
218         const int max_num_caldacs = 4;
219         /* bits to send to tell eeprom we want to read */
220         const int read_instruction = 0x6;
221         const int instruction_length = 3;
222         const int address_length = 8;
223
224         /*  send serial output stream to eeprom */
225         cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
226         /*  deactivate caldacs (one caldac for every two channels) */
227         for (i = 0; i < max_num_caldacs; i++)
228                 cal2_bits |= DESELECT_CALDAC_BIT(i);
229         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
230
231         /*  tell eeprom we want to read */
232         cb_pcidda_serial_out(dev, read_instruction, instruction_length);
233         /*  send address we want to read from */
234         cb_pcidda_serial_out(dev, address, address_length);
235
236         value = cb_pcidda_serial_in(dev);
237
238         /*  deactivate eeprom */
239         cal2_bits &= ~SELECT_EEPROM_BIT;
240         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
241
242         return value;
243 }
244
245 /* writes to 8 bit calibration dacs */
246 static void cb_pcidda_write_caldac(struct comedi_device *dev,
247                                    unsigned int caldac, unsigned int channel,
248                                    unsigned int value)
249 {
250         unsigned int cal2_bits;
251         unsigned int i;
252         /* caldacs use 3 bit channel specification */
253         const int num_channel_bits = 3;
254         const int num_caldac_bits = 8;  /*  8 bit calibration dacs */
255         /* one caldac for every two dac channels */
256         const int max_num_caldacs = 4;
257
258         /* write 3 bit channel */
259         cb_pcidda_serial_out(dev, channel, num_channel_bits);
260         /*  write 8 bit caldac value */
261         cb_pcidda_serial_out(dev, value, num_caldac_bits);
262
263 /*
264 * latch stream into appropriate caldac deselect reference dac
265 */
266         cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
267         /*  deactivate caldacs (one caldac for every two channels) */
268         for (i = 0; i < max_num_caldacs; i++)
269                 cal2_bits |= DESELECT_CALDAC_BIT(i);
270         /*  activate the caldac we want */
271         cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
272         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
273         /*  deactivate caldac */
274         cal2_bits |= DESELECT_CALDAC_BIT(caldac);
275         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
276 }
277
278 /* returns caldac that calibrates given analog out channel */
279 static unsigned int caldac_number(unsigned int channel)
280 {
281         return channel / 2;
282 }
283
284 /* returns caldac channel that provides fine gain for given ao channel */
285 static unsigned int fine_gain_channel(unsigned int ao_channel)
286 {
287         return 4 * (ao_channel % 2);
288 }
289
290 /* returns caldac channel that provides coarse gain for given ao channel */
291 static unsigned int coarse_gain_channel(unsigned int ao_channel)
292 {
293         return 1 + 4 * (ao_channel % 2);
294 }
295
296 /* returns caldac channel that provides coarse offset for given ao channel */
297 static unsigned int coarse_offset_channel(unsigned int ao_channel)
298 {
299         return 2 + 4 * (ao_channel % 2);
300 }
301
302 /* returns caldac channel that provides fine offset for given ao channel */
303 static unsigned int fine_offset_channel(unsigned int ao_channel)
304 {
305         return 3 + 4 * (ao_channel % 2);
306 }
307
308 /* returns eeprom address that provides offset for given ao channel and range */
309 static unsigned int offset_eeprom_address(unsigned int ao_channel,
310                                           unsigned int range)
311 {
312         return 0x7 + 2 * range + 12 * ao_channel;
313 }
314
315 /*
316  * returns eeprom address that provides gain calibration for given ao
317  * channel and range
318  */
319 static unsigned int gain_eeprom_address(unsigned int ao_channel,
320                                         unsigned int range)
321 {
322         return 0x8 + 2 * range + 12 * ao_channel;
323 }
324
325 /*
326  * returns upper byte of eeprom entry, which gives the coarse adjustment
327  * values
328  */
329 static unsigned int eeprom_coarse_byte(unsigned int word)
330 {
331         return (word >> 8) & 0xff;
332 }
333
334 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
335 static unsigned int eeprom_fine_byte(unsigned int word)
336 {
337         return word & 0xff;
338 }
339
340 /* set caldacs to eeprom values for given channel and range */
341 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
342                                 unsigned int range)
343 {
344         struct cb_pcidda_private *devpriv = dev->private;
345         unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
346
347         /* remember range so we can tell when we need to readjust calibration */
348         devpriv->ao_range[channel] = range;
349
350         /*  get values from eeprom data */
351         coarse_offset =
352             eeprom_coarse_byte(devpriv->eeprom_data
353                                [offset_eeprom_address(channel, range)]);
354         fine_offset =
355             eeprom_fine_byte(devpriv->eeprom_data
356                              [offset_eeprom_address(channel, range)]);
357         coarse_gain =
358             eeprom_coarse_byte(devpriv->eeprom_data
359                                [gain_eeprom_address(channel, range)]);
360         fine_gain =
361             eeprom_fine_byte(devpriv->eeprom_data
362                              [gain_eeprom_address(channel, range)]);
363
364         /*  set caldacs */
365         cb_pcidda_write_caldac(dev, caldac_number(channel),
366                                coarse_offset_channel(channel), coarse_offset);
367         cb_pcidda_write_caldac(dev, caldac_number(channel),
368                                fine_offset_channel(channel), fine_offset);
369         cb_pcidda_write_caldac(dev, caldac_number(channel),
370                                coarse_gain_channel(channel), coarse_gain);
371         cb_pcidda_write_caldac(dev, caldac_number(channel),
372                                fine_gain_channel(channel), fine_gain);
373 }
374
375 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
376                               struct comedi_subdevice *s,
377                               struct comedi_insn *insn, unsigned int *data)
378 {
379         struct cb_pcidda_private *devpriv = dev->private;
380         unsigned int command;
381         unsigned int channel, range;
382
383         channel = CR_CHAN(insn->chanspec);
384         range = CR_RANGE(insn->chanspec);
385
386         /*  adjust calibration dacs if range has changed */
387         if (range != devpriv->ao_range[channel])
388                 cb_pcidda_calibrate(dev, channel, range);
389
390         /* output channel configuration */
391         command = NOSU | ENABLEDAC;
392
393         /* output channel range */
394         switch (range) {
395         case 0:
396                 command |= BIP | RANGE10V;
397                 break;
398         case 1:
399                 command |= BIP | RANGE5V;
400                 break;
401         case 2:
402                 command |= BIP | RANGE2V5;
403                 break;
404         case 3:
405                 command |= UNIP | RANGE10V;
406                 break;
407         case 4:
408                 command |= UNIP | RANGE5V;
409                 break;
410         case 5:
411                 command |= UNIP | RANGE2V5;
412                 break;
413         }
414
415         /* output channel specification */
416         command |= channel << 2;
417         outw(command, dev->iobase + DACONTROL);
418
419         /* write data */
420         outw(data[0], dev->iobase + DADATA + channel * 2);
421
422         /* return the number of samples read/written */
423         return 1;
424 }
425
426 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
427                                             struct pci_dev *pcidev)
428 {
429         const struct cb_pcidda_board *thisboard;
430         int i;
431
432         for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
433                 thisboard = &cb_pcidda_boards[i];
434                 if (thisboard->device_id != pcidev->device)
435                         return thisboard;
436         }
437         return NULL;
438 }
439
440 static int cb_pcidda_attach_pci(struct comedi_device *dev,
441                                 struct pci_dev *pcidev)
442 {
443         const struct cb_pcidda_board *thisboard;
444         struct cb_pcidda_private *devpriv;
445         struct comedi_subdevice *s;
446         unsigned long iobase_8255;
447         int i;
448         int ret;
449
450         thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
451         if (!thisboard)
452                 return -ENODEV;
453         dev->board_ptr = thisboard;
454         dev->board_name = thisboard->name;
455
456         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
457         if (!devpriv)
458                 return -ENOMEM;
459         dev->private = devpriv;
460
461         ret = comedi_pci_enable(pcidev, dev->board_name);
462         if (ret)
463                 return ret;
464         dev->iobase = pci_resource_start(pcidev, 3);
465         iobase_8255 = pci_resource_start(pcidev, 2);
466
467         ret = comedi_alloc_subdevices(dev, 3);
468         if (ret)
469                 return ret;
470
471         s = &dev->subdevices[0];
472         /* analog output subdevice */
473         s->type = COMEDI_SUBD_AO;
474         s->subdev_flags = SDF_WRITABLE;
475         s->n_chan = thisboard->ao_chans;
476         s->maxdata = (1 << thisboard->ao_bits) - 1;
477         s->range_table = &cb_pcidda_ranges;
478         s->insn_write = cb_pcidda_ao_winsn;
479
480         /* two 8255 digital io subdevices */
481         for (i = 0; i < 2; i++) {
482                 s = &dev->subdevices[1 + i];
483                 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
484                 if (ret)
485                         return ret;
486         }
487
488         /* Read the caldac eeprom data */
489         for (i = 0; i < EEPROM_SIZE; i++)
490                 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
491
492         /*  set calibrations dacs */
493         for (i = 0; i < thisboard->ao_chans; i++)
494                 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
495
496         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
497
498         return 0;
499 }
500
501 static void cb_pcidda_detach(struct comedi_device *dev)
502 {
503         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
504
505         if (dev->subdevices) {
506                 subdev_8255_cleanup(dev, &dev->subdevices[1]);
507                 subdev_8255_cleanup(dev, &dev->subdevices[2]);
508         }
509         if (pcidev) {
510                 if (dev->iobase)
511                         comedi_pci_disable(pcidev);
512         }
513 }
514
515 static struct comedi_driver cb_pcidda_driver = {
516         .driver_name    = "cb_pcidda",
517         .module         = THIS_MODULE,
518         .attach_pci     = cb_pcidda_attach_pci,
519         .detach         = cb_pcidda_detach,
520 };
521
522 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
523                                          const struct pci_device_id *ent)
524 {
525         return comedi_pci_auto_config(dev, &cb_pcidda_driver);
526 }
527
528 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
529 {
530         comedi_pci_auto_unconfig(dev);
531 }
532
533 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
534         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
535         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
536         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
537         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
538         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
539         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
540         { 0 }
541 };
542 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
543
544 static struct pci_driver cb_pcidda_pci_driver = {
545         .name           = "cb_pcidda",
546         .id_table       = cb_pcidda_pci_table,
547         .probe          = cb_pcidda_pci_probe,
548         .remove         = __devexit_p(cb_pcidda_pci_remove),
549 };
550 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
551
552 MODULE_AUTHOR("Comedi http://www.comedi.org");
553 MODULE_DESCRIPTION("Comedi low-level driver");
554 MODULE_LICENSE("GPL");