]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/cb_pcidda.c
staging: comedi: cb_pcidda: cleanup the copyright and comedi comments
[~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 /*
129  * Board descriptions for two imaginary boards.  Describing the
130  * boards in this way is optional, and completely driver-dependent.
131  * Some drivers use arrays such as this, other do not.
132  */
133 struct cb_pcidda_board {
134         const char *name;
135         char status;            /*  Driver status: */
136
137         /*
138          * 0 - tested
139          * 1 - manual read, not tested
140          * 2 - manual not read
141          */
142
143         unsigned short device_id;
144         int ao_chans;
145         int ao_bits;
146 };
147
148 static const struct cb_pcidda_board cb_pcidda_boards[] = {
149         {
150          .name = "pci-dda02/12",
151          .status = 1,
152          .device_id = PCI_DEVICE_ID_DDA02_12,
153          .ao_chans = 2,
154          .ao_bits = 12,
155          },
156         {
157          .name = "pci-dda04/12",
158          .status = 1,
159          .device_id = PCI_DEVICE_ID_DDA04_12,
160          .ao_chans = 4,
161          .ao_bits = 12,
162          },
163         {
164          .name = "pci-dda08/12",
165          .status = 0,
166          .device_id = PCI_DEVICE_ID_DDA08_12,
167          .ao_chans = 8,
168          .ao_bits = 12,
169          },
170         {
171          .name = "pci-dda02/16",
172          .status = 2,
173          .device_id = PCI_DEVICE_ID_DDA02_16,
174          .ao_chans = 2,
175          .ao_bits = 16,
176          },
177         {
178          .name = "pci-dda04/16",
179          .status = 2,
180          .device_id = PCI_DEVICE_ID_DDA04_16,
181          .ao_chans = 4,
182          .ao_bits = 16,
183          },
184         {
185          .name = "pci-dda08/16",
186          .status = 0,
187          .device_id = PCI_DEVICE_ID_DDA08_16,
188          .ao_chans = 8,
189          .ao_bits = 16,
190          },
191 };
192
193 struct cb_pcidda_private {
194         /* bits last written to da calibration register 1 */
195         unsigned int dac_cal1_bits;
196         /* current range settings for output channels */
197         unsigned int ao_range[MAX_AO_CHANNELS];
198         u16 eeprom_data[EEPROM_SIZE];   /*  software copy of board's eeprom */
199 };
200
201 /* lowlevel read from eeprom */
202 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
203 {
204         unsigned int value = 0;
205         int i;
206         const int value_width = 16;     /*  number of bits wide values are */
207
208         for (i = 1; i <= value_width; i++) {
209                 /*  read bits most significant bit first */
210                 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
211                         value |= 1 << (value_width - i);
212         }
213
214         return value;
215 }
216
217 /* lowlevel write to eeprom/dac */
218 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
219                                  unsigned int num_bits)
220 {
221         struct cb_pcidda_private *devpriv = dev->private;
222         int i;
223
224         for (i = 1; i <= num_bits; i++) {
225                 /*  send bits most significant bit first */
226                 if (value & (1 << (num_bits - i)))
227                         devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
228                 else
229                         devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
230                 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
231         }
232 }
233
234 /* reads a 16 bit value from board's eeprom */
235 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
236                                           unsigned int address)
237 {
238         unsigned int i;
239         unsigned int cal2_bits;
240         unsigned int value;
241         /* one caldac for every two dac channels */
242         const int max_num_caldacs = 4;
243         /* bits to send to tell eeprom we want to read */
244         const int read_instruction = 0x6;
245         const int instruction_length = 3;
246         const int address_length = 8;
247
248         /*  send serial output stream to eeprom */
249         cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
250         /*  deactivate caldacs (one caldac for every two channels) */
251         for (i = 0; i < max_num_caldacs; i++)
252                 cal2_bits |= DESELECT_CALDAC_BIT(i);
253         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
254
255         /*  tell eeprom we want to read */
256         cb_pcidda_serial_out(dev, read_instruction, instruction_length);
257         /*  send address we want to read from */
258         cb_pcidda_serial_out(dev, address, address_length);
259
260         value = cb_pcidda_serial_in(dev);
261
262         /*  deactivate eeprom */
263         cal2_bits &= ~SELECT_EEPROM_BIT;
264         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
265
266         return value;
267 }
268
269 /* writes to 8 bit calibration dacs */
270 static void cb_pcidda_write_caldac(struct comedi_device *dev,
271                                    unsigned int caldac, unsigned int channel,
272                                    unsigned int value)
273 {
274         unsigned int cal2_bits;
275         unsigned int i;
276         /* caldacs use 3 bit channel specification */
277         const int num_channel_bits = 3;
278         const int num_caldac_bits = 8;  /*  8 bit calibration dacs */
279         /* one caldac for every two dac channels */
280         const int max_num_caldacs = 4;
281
282         /* write 3 bit channel */
283         cb_pcidda_serial_out(dev, channel, num_channel_bits);
284         /*  write 8 bit caldac value */
285         cb_pcidda_serial_out(dev, value, num_caldac_bits);
286
287 /*
288 * latch stream into appropriate caldac deselect reference dac
289 */
290         cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
291         /*  deactivate caldacs (one caldac for every two channels) */
292         for (i = 0; i < max_num_caldacs; i++)
293                 cal2_bits |= DESELECT_CALDAC_BIT(i);
294         /*  activate the caldac we want */
295         cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
296         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
297         /*  deactivate caldac */
298         cal2_bits |= DESELECT_CALDAC_BIT(caldac);
299         outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
300 }
301
302 /* returns caldac that calibrates given analog out channel */
303 static unsigned int caldac_number(unsigned int channel)
304 {
305         return channel / 2;
306 }
307
308 /* returns caldac channel that provides fine gain for given ao channel */
309 static unsigned int fine_gain_channel(unsigned int ao_channel)
310 {
311         return 4 * (ao_channel % 2);
312 }
313
314 /* returns caldac channel that provides coarse gain for given ao channel */
315 static unsigned int coarse_gain_channel(unsigned int ao_channel)
316 {
317         return 1 + 4 * (ao_channel % 2);
318 }
319
320 /* returns caldac channel that provides coarse offset for given ao channel */
321 static unsigned int coarse_offset_channel(unsigned int ao_channel)
322 {
323         return 2 + 4 * (ao_channel % 2);
324 }
325
326 /* returns caldac channel that provides fine offset for given ao channel */
327 static unsigned int fine_offset_channel(unsigned int ao_channel)
328 {
329         return 3 + 4 * (ao_channel % 2);
330 }
331
332 /* returns eeprom address that provides offset for given ao channel and range */
333 static unsigned int offset_eeprom_address(unsigned int ao_channel,
334                                           unsigned int range)
335 {
336         return 0x7 + 2 * range + 12 * ao_channel;
337 }
338
339 /*
340  * returns eeprom address that provides gain calibration for given ao
341  * channel and range
342  */
343 static unsigned int gain_eeprom_address(unsigned int ao_channel,
344                                         unsigned int range)
345 {
346         return 0x8 + 2 * range + 12 * ao_channel;
347 }
348
349 /*
350  * returns upper byte of eeprom entry, which gives the coarse adjustment
351  * values
352  */
353 static unsigned int eeprom_coarse_byte(unsigned int word)
354 {
355         return (word >> 8) & 0xff;
356 }
357
358 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
359 static unsigned int eeprom_fine_byte(unsigned int word)
360 {
361         return word & 0xff;
362 }
363
364 /* set caldacs to eeprom values for given channel and range */
365 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
366                                 unsigned int range)
367 {
368         struct cb_pcidda_private *devpriv = dev->private;
369         unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
370
371         /* remember range so we can tell when we need to readjust calibration */
372         devpriv->ao_range[channel] = range;
373
374         /*  get values from eeprom data */
375         coarse_offset =
376             eeprom_coarse_byte(devpriv->eeprom_data
377                                [offset_eeprom_address(channel, range)]);
378         fine_offset =
379             eeprom_fine_byte(devpriv->eeprom_data
380                              [offset_eeprom_address(channel, range)]);
381         coarse_gain =
382             eeprom_coarse_byte(devpriv->eeprom_data
383                                [gain_eeprom_address(channel, range)]);
384         fine_gain =
385             eeprom_fine_byte(devpriv->eeprom_data
386                              [gain_eeprom_address(channel, range)]);
387
388         /*  set caldacs */
389         cb_pcidda_write_caldac(dev, caldac_number(channel),
390                                coarse_offset_channel(channel), coarse_offset);
391         cb_pcidda_write_caldac(dev, caldac_number(channel),
392                                fine_offset_channel(channel), fine_offset);
393         cb_pcidda_write_caldac(dev, caldac_number(channel),
394                                coarse_gain_channel(channel), coarse_gain);
395         cb_pcidda_write_caldac(dev, caldac_number(channel),
396                                fine_gain_channel(channel), fine_gain);
397 }
398
399 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
400                               struct comedi_subdevice *s,
401                               struct comedi_insn *insn, unsigned int *data)
402 {
403         struct cb_pcidda_private *devpriv = dev->private;
404         unsigned int command;
405         unsigned int channel, range;
406
407         channel = CR_CHAN(insn->chanspec);
408         range = CR_RANGE(insn->chanspec);
409
410         /*  adjust calibration dacs if range has changed */
411         if (range != devpriv->ao_range[channel])
412                 cb_pcidda_calibrate(dev, channel, range);
413
414         /* output channel configuration */
415         command = NOSU | ENABLEDAC;
416
417         /* output channel range */
418         switch (range) {
419         case 0:
420                 command |= BIP | RANGE10V;
421                 break;
422         case 1:
423                 command |= BIP | RANGE5V;
424                 break;
425         case 2:
426                 command |= BIP | RANGE2V5;
427                 break;
428         case 3:
429                 command |= UNIP | RANGE10V;
430                 break;
431         case 4:
432                 command |= UNIP | RANGE5V;
433                 break;
434         case 5:
435                 command |= UNIP | RANGE2V5;
436                 break;
437         }
438
439         /* output channel specification */
440         command |= channel << 2;
441         outw(command, dev->iobase + DACONTROL);
442
443         /* write data */
444         outw(data[0], dev->iobase + DADATA + channel * 2);
445
446         /* return the number of samples read/written */
447         return 1;
448 }
449
450 static const void *cb_pcidda_find_boardinfo(struct comedi_device *dev,
451                                             struct pci_dev *pcidev)
452 {
453         const struct cb_pcidda_board *thisboard;
454         int i;
455
456         for (i = 0; i < ARRAY_SIZE(cb_pcidda_boards); i++) {
457                 thisboard = &cb_pcidda_boards[i];
458                 if (thisboard->device_id != pcidev->device)
459                         return thisboard;
460         }
461         return NULL;
462 }
463
464 static int cb_pcidda_attach_pci(struct comedi_device *dev,
465                                 struct pci_dev *pcidev)
466 {
467         const struct cb_pcidda_board *thisboard;
468         struct cb_pcidda_private *devpriv;
469         struct comedi_subdevice *s;
470         unsigned long iobase_8255;
471         int i;
472         int ret;
473
474         thisboard = cb_pcidda_find_boardinfo(dev, pcidev);
475         if (!thisboard)
476                 return -ENODEV;
477         dev->board_ptr = thisboard;
478         dev->board_name = thisboard->name;
479
480         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
481         if (!devpriv)
482                 return -ENOMEM;
483         dev->private = devpriv;
484
485         ret = comedi_pci_enable(pcidev, dev->board_name);
486         if (ret)
487                 return ret;
488         dev->iobase = pci_resource_start(pcidev, 3);
489         iobase_8255 = pci_resource_start(pcidev, 2);
490
491         if (thisboard->status == 2)
492                 printk
493                     ("WARNING: DRIVER FOR THIS BOARD NOT CHECKED WITH MANUAL. "
494                      "WORKS ASSUMING FULL COMPATIBILITY WITH PCI-DDA08/12. "
495                      "PLEASE REPORT USAGE TO <ivanmr@altavista.com>.\n");
496
497         ret = comedi_alloc_subdevices(dev, 3);
498         if (ret)
499                 return ret;
500
501         s = &dev->subdevices[0];
502         /* analog output subdevice */
503         s->type = COMEDI_SUBD_AO;
504         s->subdev_flags = SDF_WRITABLE;
505         s->n_chan = thisboard->ao_chans;
506         s->maxdata = (1 << thisboard->ao_bits) - 1;
507         s->range_table = &cb_pcidda_ranges;
508         s->insn_write = cb_pcidda_ao_winsn;
509
510         /* two 8255 digital io subdevices */
511         for (i = 0; i < 2; i++) {
512                 s = &dev->subdevices[1 + i];
513                 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
514                 if (ret)
515                         return ret;
516         }
517
518         /* Read the caldac eeprom data */
519         for (i = 0; i < EEPROM_SIZE; i++)
520                 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
521
522         /*  set calibrations dacs */
523         for (i = 0; i < thisboard->ao_chans; i++)
524                 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
525
526         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
527
528         return 0;
529 }
530
531 static void cb_pcidda_detach(struct comedi_device *dev)
532 {
533         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
534
535         if (dev->subdevices) {
536                 subdev_8255_cleanup(dev, &dev->subdevices[1]);
537                 subdev_8255_cleanup(dev, &dev->subdevices[2]);
538         }
539         if (pcidev) {
540                 if (dev->iobase)
541                         comedi_pci_disable(pcidev);
542         }
543 }
544
545 static struct comedi_driver cb_pcidda_driver = {
546         .driver_name    = "cb_pcidda",
547         .module         = THIS_MODULE,
548         .attach_pci     = cb_pcidda_attach_pci,
549         .detach         = cb_pcidda_detach,
550 };
551
552 static int __devinit cb_pcidda_pci_probe(struct pci_dev *dev,
553                                          const struct pci_device_id *ent)
554 {
555         return comedi_pci_auto_config(dev, &cb_pcidda_driver);
556 }
557
558 static void __devexit cb_pcidda_pci_remove(struct pci_dev *dev)
559 {
560         comedi_pci_auto_unconfig(dev);
561 }
562
563 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
564         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_12) },
565         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_12) },
566         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_12) },
567         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA02_16) },
568         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA04_16) },
569         { PCI_DEVICE(PCI_VENDOR_ID_CB, PCI_DEVICE_ID_DDA08_16) },
570         { 0 }
571 };
572 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
573
574 static struct pci_driver cb_pcidda_pci_driver = {
575         .name           = "cb_pcidda",
576         .id_table       = cb_pcidda_pci_table,
577         .probe          = cb_pcidda_pci_probe,
578         .remove         = __devexit_p(cb_pcidda_pci_remove),
579 };
580 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
581
582 MODULE_AUTHOR("Comedi http://www.comedi.org");
583 MODULE_DESCRIPTION("Comedi low-level driver");
584 MODULE_LICENSE("GPL");