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