Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / drivers / cb_pcidda.c
1 /*
2 comedi/drivers/cb_pcidda.c
3 This intends to be a driver for the ComputerBoards / MeasurementComputing
4 PCI-DDA series.
5
6 Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
7 Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
8
9 COMEDI - Linux Control and Measurement Device Interface
10 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 */
27 /*
28 Driver: cb_pcidda
29 Description: MeasurementComputing PCI-DDA series
30 Author: Ivan Martinez <ivanmr@altavista.com>, Frank Mori Hess <fmhess@users.sourceforge.net>
31 Status: Supports 08/16, 04/16, 02/16, 08/12, 04/12, and 02/12
32 Devices: [Measurement Computing] PCI-DDA08/12 (cb_pcidda), PCI-DDA04/12,
33 PCI-DDA02/12, PCI-DDA08/16, PCI-DDA04/16, PCI-DDA02/16
34
35 Configuration options:
36 [0] - PCI bus of device (optional)
37 [1] - PCI slot of device (optional)
38 If bus/slot is not specified, the first available PCI
39 device will be used.
40
41 Only simple analog output writing is supported.
42
43 So far it has only been tested with:
44 - PCI-DDA08/12
45 Please report success/failure with other different cards to
46 <comedi@comedi.org>.
47 */
48
49 #include "../comedidev.h"
50
51 #include "comedi_pci.h"
52 #include "8255.h"
53
54 #define PCI_VENDOR_ID_CB 0x1307 /* PCI vendor number of ComputerBoards */
55 #define EEPROM_SIZE 128 /* number of entries in eeprom */
56 #define MAX_AO_CHANNELS 8 /* maximum number of ao channels for supported boards */
57
58 /* PCI-DDA base addresses */
59 #define DIGITALIO_BADRINDEX 2
60 /* DIGITAL I/O is pci_dev->resource[2] */
61 #define DIGITALIO_SIZE 8
62 /* DIGITAL I/O uses 8 I/O port addresses */
63 #define DAC_BADRINDEX 3
64 /* DAC is pci_dev->resource[3] */
65
66 /* Digital I/O registers */
67 #define PORT1A 0 /* PORT 1A DATA */
68
69 #define PORT1B 1 /* PORT 1B DATA */
70
71 #define PORT1C 2 /* PORT 1C DATA */
72
73 #define CONTROL1 3 /* CONTROL REGISTER 1 */
74
75 #define PORT2A 4 /* PORT 2A DATA */
76
77 #define PORT2B 5 /* PORT 2B DATA */
78
79 #define PORT2C 6 /* PORT 2C DATA */
80
81 #define CONTROL2 7 /* CONTROL REGISTER 2 */
82
83 /* DAC registers */
84 #define DACONTROL 0 /* D/A CONTROL REGISTER */
85 #define SU 0000001 /* Simultaneous update enabled */
86 #define NOSU 0000000 /* Simultaneous update disabled */
87 #define ENABLEDAC 0000002 /* Enable specified DAC */
88 #define DISABLEDAC 0000000 /* Disable specified DAC */
89 #define RANGE2V5 0000000 /* 2.5V */
90 #define RANGE5V 0000200 /* 5V */
91 #define RANGE10V 0000300 /* 10V */
92 #define UNIP 0000400 /* Unipolar outputs */
93 #define BIP 0000000 /* Bipolar outputs */
94
95 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
96 /* write bits */
97 #define SERIAL_IN_BIT 0x1 /* serial data input for eeprom, caldacs, reference dac */
98 #define CAL_CHANNEL_MASK (0x7 << 1)
99 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
100 /* read bits */
101 #define CAL_COUNTER_MASK 0x1f
102 #define CAL_COUNTER_OVERFLOW_BIT 0x20 /* calibration counter overflow status bit */
103 #define AO_BELOW_REF_BIT 0x40 /* analog output is less than reference dac voltage */
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 #define DESELECT_REF_DAC_BIT 0x2 /* don't send serial data to MAX542 reference dac */
109 #define DESELECT_CALDAC_BIT(n) (0x4 << (n)) /* don't send serial data to caldac n */
110 #define DUMMY_BIT 0x40 /* manual says to set this bit with no explanation */
111
112 #define DADATA 8 /* FIRST D/A DATA REGISTER (0) */
113
114 static const struct comedi_lrange cb_pcidda_ranges = {
115 6,
116 {
117 BIP_RANGE(10),
118 BIP_RANGE(5),
119 BIP_RANGE(2.5),
120 UNI_RANGE(10),
121 UNI_RANGE(5),
122 UNI_RANGE(2.5),
123 }
124 };
125
126 /*
127 * Board descriptions for two imaginary boards. Describing the
128 * boards in this way is optional, and completely driver-dependent.
129 * Some drivers use arrays such as this, other do not.
130 */
131 struct cb_pcidda_board {
132 const char *name;
133 char status; /* Driver status: */
134
135 /*
136 * 0 - tested
137 * 1 - manual read, not tested
138 * 2 - manual not read
139 */
140
141 unsigned short device_id;
142 int ao_chans;
143 int ao_bits;
144 const struct comedi_lrange *ranges;
145 };
146
147 static const struct cb_pcidda_board cb_pcidda_boards[] = {
148 {
149 .name = "pci-dda02/12",
150 .status = 1,
151 .device_id = 0x20,
152 .ao_chans = 2,
153 .ao_bits = 12,
154 .ranges = &cb_pcidda_ranges,
155 },
156 {
157 .name = "pci-dda04/12",
158 .status = 1,
159 .device_id = 0x21,
160 .ao_chans = 4,
161 .ao_bits = 12,
162 .ranges = &cb_pcidda_ranges,
163 },
164 {
165 .name = "pci-dda08/12",
166 .status = 0,
167 .device_id = 0x22,
168 .ao_chans = 8,
169 .ao_bits = 12,
170 .ranges = &cb_pcidda_ranges,
171 },
172 {
173 .name = "pci-dda02/16",
174 .status = 2,
175 .device_id = 0x23,
176 .ao_chans = 2,
177 .ao_bits = 16,
178 .ranges = &cb_pcidda_ranges,
179 },
180 {
181 .name = "pci-dda04/16",
182 .status = 2,
183 .device_id = 0x24,
184 .ao_chans = 4,
185 .ao_bits = 16,
186 .ranges = &cb_pcidda_ranges,
187 },
188 {
189 .name = "pci-dda08/16",
190 .status = 0,
191 .device_id = 0x25,
192 .ao_chans = 8,
193 .ao_bits = 16,
194 .ranges = &cb_pcidda_ranges,
195 },
196 };
197
198 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
199 {
200 PCI_VENDOR_ID_CB, 0x0020, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
201 PCI_VENDOR_ID_CB, 0x0021, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
202 PCI_VENDOR_ID_CB, 0x0022, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
203 PCI_VENDOR_ID_CB, 0x0023, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
204 PCI_VENDOR_ID_CB, 0x0024, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
205 PCI_VENDOR_ID_CB, 0x0025, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
206 0}
207 };
208
209 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
210
211 /*
212 * Useful for shorthand access to the particular board structure
213 */
214 #define thisboard ((const struct cb_pcidda_board *)dev->board_ptr)
215
216 /* this structure is for data unique to this hardware driver. If
217 several hardware drivers keep similar information in this structure,
218 feel free to suggest moving the variable to the struct comedi_device struct. */
219 struct cb_pcidda_private {
220 int data;
221
222 /* would be useful for a PCI device */
223 struct pci_dev *pci_dev;
224
225 unsigned long digitalio;
226 unsigned long dac;
227
228 /* unsigned long control_status; */
229 /* unsigned long adc_fifo; */
230
231 unsigned int dac_cal1_bits; /* bits last written to da calibration register 1 */
232 unsigned int ao_range[MAX_AO_CHANNELS]; /* current range settings for output channels */
233 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
234 };
235
236 /*
237 * most drivers define the following macro to make it easy to
238 * access the private structure.
239 */
240 #define devpriv ((struct cb_pcidda_private *)dev->private)
241
242 static int cb_pcidda_attach(struct comedi_device *dev,
243 struct comedi_devconfig *it);
244 static int cb_pcidda_detach(struct comedi_device *dev);
245 /* static int cb_pcidda_ai_rinsn(struct comedi_device *dev,struct comedi_subdevice *s,struct comedi_insn *insn,unsigned int *data); */
246 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
247 struct comedi_subdevice *s,
248 struct comedi_insn *insn, unsigned int *data);
249
250 /* static int cb_pcidda_ai_cmd(struct comedi_device *dev, struct *comedi_subdevice *s);*/
251 /* static int cb_pcidda_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, struct comedi_cmd *cmd); */
252 /* static int cb_pcidda_ns_to_timer(unsigned int *ns,int *round); */
253
254 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev);
255 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
256 unsigned int num_bits);
257 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
258 unsigned int address);
259 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
260 unsigned int range);
261
262 /*
263 * The struct comedi_driver structure tells the Comedi core module
264 * which functions to call to configure/deconfigure (attach/detach)
265 * the board, and also about the kernel module that contains
266 * the device code.
267 */
268 static struct comedi_driver driver_cb_pcidda = {
269 .driver_name = "cb_pcidda",
270 .module = THIS_MODULE,
271 .attach = cb_pcidda_attach,
272 .detach = cb_pcidda_detach,
273 };
274
275 /*
276 * Attach is called by the Comedi core to configure the driver
277 * for a particular board.
278 */
279 static int cb_pcidda_attach(struct comedi_device *dev,
280 struct comedi_devconfig *it)
281 {
282 struct comedi_subdevice *s;
283 struct pci_dev *pcidev = NULL;
284 int index;
285
286 printk("comedi%d: cb_pcidda: ", dev->minor);
287
288 /*
289 * Allocate the private structure area.
290 */
291 if (alloc_private(dev, sizeof(struct cb_pcidda_private)) < 0)
292 return -ENOMEM;
293
294 /*
295 * Probe the device to determine what device in the series it is.
296 */
297 printk("\n");
298
299 for_each_pci_dev(pcidev) {
300 if (pcidev->vendor == PCI_VENDOR_ID_CB) {
301 if (it->options[0] || it->options[1]) {
302 if (pcidev->bus->number != it->options[0] ||
303 PCI_SLOT(pcidev->devfn) != it->options[1]) {
304 continue;
305 }
306 }
307 for (index = 0; index < ARRAY_SIZE(cb_pcidda_boards); index++) {
308 if (cb_pcidda_boards[index].device_id ==
309 pcidev->device) {
310 goto found;
311 }
312 }
313 }
314 }
315 if (!pcidev) {
316 printk
317 ("Not a ComputerBoards/MeasurementComputing card on requested position\n");
318 return -EIO;
319 }
320 found:
321 devpriv->pci_dev = pcidev;
322 dev->board_ptr = cb_pcidda_boards + index;
323 /* "thisboard" macro can be used from here. */
324 printk("Found %s at requested position\n", thisboard->name);
325
326 /*
327 * Enable PCI device and request regions.
328 */
329 if (comedi_pci_enable(pcidev, thisboard->name)) {
330 printk
331 ("cb_pcidda: failed to enable PCI device and request regions\n");
332 return -EIO;
333 }
334
335 /*
336 * Allocate the I/O ports.
337 */
338 devpriv->digitalio =
339 pci_resource_start(devpriv->pci_dev, DIGITALIO_BADRINDEX);
340 devpriv->dac = pci_resource_start(devpriv->pci_dev, DAC_BADRINDEX);
341
342 /*
343 * Warn about the status of the driver.
344 */
345 if (thisboard->status == 2)
346 printk
347 ("WARNING: DRIVER FOR THIS BOARD NOT CHECKED WITH MANUAL. "
348 "WORKS ASSUMING FULL COMPATIBILITY WITH PCI-DDA08/12. "
349 "PLEASE REPORT USAGE TO <ivanmr@altavista.com>.\n");
350
351 /*
352 * Initialize dev->board_name.
353 */
354 dev->board_name = thisboard->name;
355
356 /*
357 * Allocate the subdevice structures.
358 */
359 if (alloc_subdevices(dev, 3) < 0)
360 return -ENOMEM;
361
362 s = dev->subdevices + 0;
363 /* analog output subdevice */
364 s->type = COMEDI_SUBD_AO;
365 s->subdev_flags = SDF_WRITABLE;
366 s->n_chan = thisboard->ao_chans;
367 s->maxdata = (1 << thisboard->ao_bits) - 1;
368 s->range_table = thisboard->ranges;
369 s->insn_write = cb_pcidda_ao_winsn;
370
371 /* s->subdev_flags |= SDF_CMD_READ; */
372 /* s->do_cmd = cb_pcidda_ai_cmd; */
373 /* s->do_cmdtest = cb_pcidda_ai_cmdtest; */
374
375 /* two 8255 digital io subdevices */
376 s = dev->subdevices + 1;
377 subdev_8255_init(dev, s, NULL, devpriv->digitalio);
378 s = dev->subdevices + 2;
379 subdev_8255_init(dev, s, NULL, devpriv->digitalio + PORT2A);
380
381 printk(" eeprom:");
382 for (index = 0; index < EEPROM_SIZE; index++) {
383 devpriv->eeprom_data[index] = cb_pcidda_read_eeprom(dev, index);
384 printk(" %i:0x%x ", index, devpriv->eeprom_data[index]);
385 }
386 printk("\n");
387
388 /* set calibrations dacs */
389 for (index = 0; index < thisboard->ao_chans; index++)
390 cb_pcidda_calibrate(dev, index, devpriv->ao_range[index]);
391
392 return 1;
393 }
394
395 /*
396 * _detach is called to deconfigure a device. It should deallocate
397 * resources.
398 * This function is also called when _attach() fails, so it should be
399 * careful not to release resources that were not necessarily
400 * allocated by _attach(). dev->private and dev->subdevices are
401 * deallocated automatically by the core.
402 */
403 static int cb_pcidda_detach(struct comedi_device *dev)
404 {
405 /*
406 * Deallocate the I/O ports.
407 */
408 if (devpriv) {
409 if (devpriv->pci_dev) {
410 if (devpriv->dac)
411 comedi_pci_disable(devpriv->pci_dev);
412 pci_dev_put(devpriv->pci_dev);
413 }
414 }
415 /* cleanup 8255 */
416 if (dev->subdevices) {
417 subdev_8255_cleanup(dev, dev->subdevices + 1);
418 subdev_8255_cleanup(dev, dev->subdevices + 2);
419 }
420
421 printk("comedi%d: cb_pcidda: remove\n", dev->minor);
422
423 return 0;
424 }
425
426 /*
427 * I will program this later... ;-)
428 */
429 #if 0
430 static int cb_pcidda_ai_cmd(struct comedi_device *dev,
431 struct comedi_subdevice *s)
432 {
433 printk("cb_pcidda_ai_cmd\n");
434 printk("subdev: %d\n", cmd->subdev);
435 printk("flags: %d\n", cmd->flags);
436 printk("start_src: %d\n", cmd->start_src);
437 printk("start_arg: %d\n", cmd->start_arg);
438 printk("scan_begin_src: %d\n", cmd->scan_begin_src);
439 printk("convert_src: %d\n", cmd->convert_src);
440 printk("convert_arg: %d\n", cmd->convert_arg);
441 printk("scan_end_src: %d\n", cmd->scan_end_src);
442 printk("scan_end_arg: %d\n", cmd->scan_end_arg);
443 printk("stop_src: %d\n", cmd->stop_src);
444 printk("stop_arg: %d\n", cmd->stop_arg);
445 printk("chanlist_len: %d\n", cmd->chanlist_len);
446 }
447 #endif
448
449 #if 0
450 static int cb_pcidda_ai_cmdtest(struct comedi_device *dev,
451 struct comedi_subdevice *s,
452 struct comedi_cmd *cmd)
453 {
454 int err = 0;
455 int tmp;
456
457 /* cmdtest tests a particular command to see if it is valid.
458 * Using the cmdtest ioctl, a user can create a valid cmd
459 * and then have it executes by the cmd ioctl.
460 *
461 * cmdtest returns 1,2,3,4 or 0, depending on which tests
462 * the command passes. */
463
464 /* step 1: make sure trigger sources are trivially valid */
465
466 tmp = cmd->start_src;
467 cmd->start_src &= TRIG_NOW;
468 if (!cmd->start_src || tmp != cmd->start_src)
469 err++;
470
471 tmp = cmd->scan_begin_src;
472 cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT;
473 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
474 err++;
475
476 tmp = cmd->convert_src;
477 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
478 if (!cmd->convert_src || tmp != cmd->convert_src)
479 err++;
480
481 tmp = cmd->scan_end_src;
482 cmd->scan_end_src &= TRIG_COUNT;
483 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
484 err++;
485
486 tmp = cmd->stop_src;
487 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
488 if (!cmd->stop_src || tmp != cmd->stop_src)
489 err++;
490
491 if (err)
492 return 1;
493
494 /* step 2: make sure trigger sources are unique and mutually compatible */
495
496 /* note that mutual compatibility is not an issue here */
497 if (cmd->scan_begin_src != TRIG_TIMER
498 && cmd->scan_begin_src != TRIG_EXT)
499 err++;
500 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
501 err++;
502 if (cmd->stop_src != TRIG_TIMER && cmd->stop_src != TRIG_EXT)
503 err++;
504
505 if (err)
506 return 2;
507
508 /* step 3: make sure arguments are trivially compatible */
509
510 if (cmd->start_arg != 0) {
511 cmd->start_arg = 0;
512 err++;
513 }
514 #define MAX_SPEED 10000 /* in nanoseconds */
515 #define MIN_SPEED 1000000000 /* in nanoseconds */
516
517 if (cmd->scan_begin_src == TRIG_TIMER) {
518 if (cmd->scan_begin_arg < MAX_SPEED) {
519 cmd->scan_begin_arg = MAX_SPEED;
520 err++;
521 }
522 if (cmd->scan_begin_arg > MIN_SPEED) {
523 cmd->scan_begin_arg = MIN_SPEED;
524 err++;
525 }
526 } else {
527 /* external trigger */
528 /* should be level/edge, hi/lo specification here */
529 /* should specify multiple external triggers */
530 if (cmd->scan_begin_arg > 9) {
531 cmd->scan_begin_arg = 9;
532 err++;
533 }
534 }
535 if (cmd->convert_src == TRIG_TIMER) {
536 if (cmd->convert_arg < MAX_SPEED) {
537 cmd->convert_arg = MAX_SPEED;
538 err++;
539 }
540 if (cmd->convert_arg > MIN_SPEED) {
541 cmd->convert_arg = MIN_SPEED;
542 err++;
543 }
544 } else {
545 /* external trigger */
546 /* see above */
547 if (cmd->convert_arg > 9) {
548 cmd->convert_arg = 9;
549 err++;
550 }
551 }
552
553 if (cmd->scan_end_arg != cmd->chanlist_len) {
554 cmd->scan_end_arg = cmd->chanlist_len;
555 err++;
556 }
557 if (cmd->stop_src == TRIG_COUNT) {
558 if (cmd->stop_arg > 0x00ffffff) {
559 cmd->stop_arg = 0x00ffffff;
560 err++;
561 }
562 } else {
563 /* TRIG_NONE */
564 if (cmd->stop_arg != 0) {
565 cmd->stop_arg = 0;
566 err++;
567 }
568 }
569
570 if (err)
571 return 3;
572
573 /* step 4: fix up any arguments */
574
575 if (cmd->scan_begin_src == TRIG_TIMER) {
576 tmp = cmd->scan_begin_arg;
577 cb_pcidda_ns_to_timer(&cmd->scan_begin_arg,
578 cmd->flags & TRIG_ROUND_MASK);
579 if (tmp != cmd->scan_begin_arg)
580 err++;
581 }
582 if (cmd->convert_src == TRIG_TIMER) {
583 tmp = cmd->convert_arg;
584 cb_pcidda_ns_to_timer(&cmd->convert_arg,
585 cmd->flags & TRIG_ROUND_MASK);
586 if (tmp != cmd->convert_arg)
587 err++;
588 if (cmd->scan_begin_src == TRIG_TIMER &&
589 cmd->scan_begin_arg <
590 cmd->convert_arg * cmd->scan_end_arg) {
591 cmd->scan_begin_arg =
592 cmd->convert_arg * cmd->scan_end_arg;
593 err++;
594 }
595 }
596
597 if (err)
598 return 4;
599
600 return 0;
601 }
602 #endif
603
604 /* This function doesn't require a particular form, this is just
605 * what happens to be used in some of the drivers. It should
606 * convert ns nanoseconds to a counter value suitable for programming
607 * the device. Also, it should adjust ns so that it cooresponds to
608 * the actual time that the device will use. */
609 #if 0
610 static int cb_pcidda_ns_to_timer(unsigned int *ns, int round)
611 {
612 /* trivial timer */
613 return *ns;
614 }
615 #endif
616
617 static int cb_pcidda_ao_winsn(struct comedi_device *dev,
618 struct comedi_subdevice *s,
619 struct comedi_insn *insn, unsigned int *data)
620 {
621 unsigned int command;
622 unsigned int channel, range;
623
624 channel = CR_CHAN(insn->chanspec);
625 range = CR_RANGE(insn->chanspec);
626
627 /* adjust calibration dacs if range has changed */
628 if (range != devpriv->ao_range[channel])
629 cb_pcidda_calibrate(dev, channel, range);
630
631 /* output channel configuration */
632 command = NOSU | ENABLEDAC;
633
634 /* output channel range */
635 switch (range) {
636 case 0:
637 command |= BIP | RANGE10V;
638 break;
639 case 1:
640 command |= BIP | RANGE5V;
641 break;
642 case 2:
643 command |= BIP | RANGE2V5;
644 break;
645 case 3:
646 command |= UNIP | RANGE10V;
647 break;
648 case 4:
649 command |= UNIP | RANGE5V;
650 break;
651 case 5:
652 command |= UNIP | RANGE2V5;
653 break;
654 };
655
656 /* output channel specification */
657 command |= channel << 2;
658 outw(command, devpriv->dac + DACONTROL);
659
660 /* write data */
661 outw(data[0], devpriv->dac + DADATA + channel * 2);
662
663 /* return the number of samples read/written */
664 return 1;
665 }
666
667 /* lowlevel read from eeprom */
668 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
669 {
670 unsigned int value = 0;
671 int i;
672 const int value_width = 16; /* number of bits wide values are */
673
674 for (i = 1; i <= value_width; i++) {
675 /* read bits most significant bit first */
676 if (inw_p(devpriv->dac + DACALIBRATION1) & SERIAL_OUT_BIT)
677 value |= 1 << (value_width - i);
678 }
679
680 return value;
681 }
682
683 /* lowlevel write to eeprom/dac */
684 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
685 unsigned int num_bits)
686 {
687 int i;
688
689 for (i = 1; i <= num_bits; i++) {
690 /* send bits most significant bit first */
691 if (value & (1 << (num_bits - i)))
692 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
693 else
694 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
695 outw_p(devpriv->dac_cal1_bits, devpriv->dac + DACALIBRATION1);
696 }
697 }
698
699 /* reads a 16 bit value from board's eeprom */
700 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
701 unsigned int address)
702 {
703 unsigned int i;
704 unsigned int cal2_bits;
705 unsigned int value;
706 const int max_num_caldacs = 4; /* one caldac for every two dac channels */
707 const int read_instruction = 0x6; /* bits to send to tell eeprom we want to read */
708 const int instruction_length = 3;
709 const int address_length = 8;
710
711 /* send serial output stream to eeprom */
712 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
713 /* deactivate caldacs (one caldac for every two channels) */
714 for (i = 0; i < max_num_caldacs; i++)
715 cal2_bits |= DESELECT_CALDAC_BIT(i);
716 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
717
718 /* tell eeprom we want to read */
719 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
720 /* send address we want to read from */
721 cb_pcidda_serial_out(dev, address, address_length);
722
723 value = cb_pcidda_serial_in(dev);
724
725 /* deactivate eeprom */
726 cal2_bits &= ~SELECT_EEPROM_BIT;
727 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
728
729 return value;
730 }
731
732 /* writes to 8 bit calibration dacs */
733 static void cb_pcidda_write_caldac(struct comedi_device *dev,
734 unsigned int caldac, unsigned int channel,
735 unsigned int value)
736 {
737 unsigned int cal2_bits;
738 unsigned int i;
739 const int num_channel_bits = 3; /* caldacs use 3 bit channel specification */
740 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
741 const int max_num_caldacs = 4; /* one caldac for every two dac channels */
742
743 /* write 3 bit channel */
744 cb_pcidda_serial_out(dev, channel, num_channel_bits);
745 /* write 8 bit caldac value */
746 cb_pcidda_serial_out(dev, value, num_caldac_bits);
747
748 /*
749 * latch stream into appropriate caldac deselect reference dac
750 */
751 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
752 /* deactivate caldacs (one caldac for every two channels) */
753 for (i = 0; i < max_num_caldacs; i++)
754 cal2_bits |= DESELECT_CALDAC_BIT(i);
755 /* activate the caldac we want */
756 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
757 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
758 /* deactivate caldac */
759 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
760 outw_p(cal2_bits, devpriv->dac + DACALIBRATION2);
761 }
762
763 /* returns caldac that calibrates given analog out channel */
764 static unsigned int caldac_number(unsigned int channel)
765 {
766 return channel / 2;
767 }
768
769 /* returns caldac channel that provides fine gain for given ao channel */
770 static unsigned int fine_gain_channel(unsigned int ao_channel)
771 {
772 return 4 * (ao_channel % 2);
773 }
774
775 /* returns caldac channel that provides coarse gain for given ao channel */
776 static unsigned int coarse_gain_channel(unsigned int ao_channel)
777 {
778 return 1 + 4 * (ao_channel % 2);
779 }
780
781 /* returns caldac channel that provides coarse offset for given ao channel */
782 static unsigned int coarse_offset_channel(unsigned int ao_channel)
783 {
784 return 2 + 4 * (ao_channel % 2);
785 }
786
787 /* returns caldac channel that provides fine offset for given ao channel */
788 static unsigned int fine_offset_channel(unsigned int ao_channel)
789 {
790 return 3 + 4 * (ao_channel % 2);
791 }
792
793 /* returns eeprom address that provides offset for given ao channel and range */
794 static unsigned int offset_eeprom_address(unsigned int ao_channel,
795 unsigned int range)
796 {
797 return 0x7 + 2 * range + 12 * ao_channel;
798 }
799
800 /* returns eeprom address that provides gain calibration for given ao channel and range */
801 static unsigned int gain_eeprom_address(unsigned int ao_channel,
802 unsigned int range)
803 {
804 return 0x8 + 2 * range + 12 * ao_channel;
805 }
806
807 /* returns upper byte of eeprom entry, which gives the coarse adjustment values */
808 static unsigned int eeprom_coarse_byte(unsigned int word)
809 {
810 return (word >> 8) & 0xff;
811 }
812
813 /* returns lower byte of eeprom entry, which gives the fine adjustment values */
814 static unsigned int eeprom_fine_byte(unsigned int word)
815 {
816 return word & 0xff;
817 }
818
819 /* set caldacs to eeprom values for given channel and range */
820 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
821 unsigned int range)
822 {
823 unsigned int coarse_offset, fine_offset, coarse_gain, fine_gain;
824
825 /* remember range so we can tell when we need to readjust calibration */
826 devpriv->ao_range[channel] = range;
827
828 /* get values from eeprom data */
829 coarse_offset =
830 eeprom_coarse_byte(devpriv->eeprom_data
831 [offset_eeprom_address(channel, range)]);
832 fine_offset =
833 eeprom_fine_byte(devpriv->eeprom_data
834 [offset_eeprom_address(channel, range)]);
835 coarse_gain =
836 eeprom_coarse_byte(devpriv->eeprom_data
837 [gain_eeprom_address(channel, range)]);
838 fine_gain =
839 eeprom_fine_byte(devpriv->eeprom_data
840 [gain_eeprom_address(channel, range)]);
841
842 /* set caldacs */
843 cb_pcidda_write_caldac(dev, caldac_number(channel),
844 coarse_offset_channel(channel), coarse_offset);
845 cb_pcidda_write_caldac(dev, caldac_number(channel),
846 fine_offset_channel(channel), fine_offset);
847 cb_pcidda_write_caldac(dev, caldac_number(channel),
848 coarse_gain_channel(channel), coarse_gain);
849 cb_pcidda_write_caldac(dev, caldac_number(channel),
850 fine_gain_channel(channel), fine_gain);
851 }
852
853 /*
854 * A convenient macro that defines init_module() and cleanup_module(),
855 * as necessary.
856 */
857 static int __devinit driver_cb_pcidda_pci_probe(struct pci_dev *dev,
858 const struct pci_device_id *ent)
859 {
860 return comedi_pci_auto_config(dev, driver_cb_pcidda.driver_name);
861 }
862
863 static void __devexit driver_cb_pcidda_pci_remove(struct pci_dev *dev)
864 {
865 comedi_pci_auto_unconfig(dev);
866 }
867
868 static struct pci_driver driver_cb_pcidda_pci_driver = {
869 .id_table = cb_pcidda_pci_table,
870 .probe = &driver_cb_pcidda_pci_probe,
871 .remove = __devexit_p(&driver_cb_pcidda_pci_remove)
872 };
873
874 static int __init driver_cb_pcidda_init_module(void)
875 {
876 int retval;
877
878 retval = comedi_driver_register(&driver_cb_pcidda);
879 if (retval < 0)
880 return retval;
881
882 driver_cb_pcidda_pci_driver.name = (char *)driver_cb_pcidda.driver_name;
883 return pci_register_driver(&driver_cb_pcidda_pci_driver);
884 }
885
886 static void __exit driver_cb_pcidda_cleanup_module(void)
887 {
888 pci_unregister_driver(&driver_cb_pcidda_pci_driver);
889 comedi_driver_unregister(&driver_cb_pcidda);
890 }
891
892 module_init(driver_cb_pcidda_init_module);
893 module_exit(driver_cb_pcidda_cleanup_module);
894
895 MODULE_AUTHOR("Comedi http://www.comedi.org");
896 MODULE_DESCRIPTION("Comedi low-level driver");
897 MODULE_LICENSE("GPL");