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