Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / staging / comedi / drivers / cb_pcidda.c
blob2d3e920e598711a0cb0b2d6a08dc85b8363f97aa
1 /*
2 * comedi/drivers/cb_pcidda.c
3 * Driver for the ComputerBoards / MeasurementComputing PCI-DDA series.
5 * Copyright (C) 2001 Ivan Martinez <ivanmr@altavista.com>
6 * Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * COMEDI - Linux Control and Measurement Device Interface
9 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
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.
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.
23 * Driver: cb_pcidda
24 * Description: MeasurementComputing PCI-DDA series
25 * Devices: (Measurement Computing) PCI-DDA08/12 [pci-dda08/12]
26 * (Measurement Computing) PCI-DDA04/12 [pci-dda04/12]
27 * (Measurement Computing) PCI-DDA02/12 [pci-dda02/12]
28 * (Measurement Computing) PCI-DDA08/16 [pci-dda08/16]
29 * (Measurement Computing) PCI-DDA04/16 [pci-dda04/16]
30 * (Measurement Computing) PCI-DDA02/16 [pci-dda02/16]
31 * Author: Ivan Martinez <ivanmr@altavista.com>
32 * Frank Mori Hess <fmhess@users.sourceforge.net>
33 * Status: works
35 * Configuration options: not applicable, uses PCI auto config
37 * Only simple analog output writing is supported.
40 #include <linux/pci.h>
42 #include "../comedidev.h"
44 #include "comedi_fc.h"
45 #include "8255.h"
47 #define EEPROM_SIZE 128 /* number of entries in eeprom */
48 /* maximum number of ao channels for supported boards */
49 #define MAX_AO_CHANNELS 8
51 /* Digital I/O registers */
52 #define CB_DDA_DIO0_8255_BASE 0x00
53 #define CB_DDA_DIO1_8255_BASE 0x04
55 /* DAC registers */
56 #define CB_DDA_DA_CTRL_REG 0x00 /* D/A Control Register */
57 #define CB_DDA_DA_CTRL_SU (1 << 0) /* Simultaneous update */
58 #define CB_DDA_DA_CTRL_EN (1 << 1) /* Enable specified DAC */
59 #define CB_DDA_DA_CTRL_DAC(x) ((x) << 2) /* Specify DAC channel */
60 #define CB_DDA_DA_CTRL_RANGE2V5 (0 << 6) /* 2.5V range */
61 #define CB_DDA_DA_CTRL_RANGE5V (2 << 6) /* 5V range */
62 #define CB_DDA_DA_CTRL_RANGE10V (3 << 6) /* 10V range */
63 #define CB_DDA_DA_CTRL_UNIP (1 << 8) /* Unipolar range */
65 #define DACALIBRATION1 4 /* D/A CALIBRATION REGISTER 1 */
66 /* write bits */
67 /* serial data input for eeprom, caldacs, reference dac */
68 #define SERIAL_IN_BIT 0x1
69 #define CAL_CHANNEL_MASK (0x7 << 1)
70 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
71 /* read bits */
72 #define CAL_COUNTER_MASK 0x1f
73 /* calibration counter overflow status bit */
74 #define CAL_COUNTER_OVERFLOW_BIT 0x20
75 /* analog output is less than reference dac voltage */
76 #define AO_BELOW_REF_BIT 0x40
77 #define SERIAL_OUT_BIT 0x80 /* serial data out, for reading from eeprom */
79 #define DACALIBRATION2 6 /* D/A CALIBRATION REGISTER 2 */
80 #define SELECT_EEPROM_BIT 0x1 /* send serial data in to eeprom */
81 /* don't send serial data to MAX542 reference dac */
82 #define DESELECT_REF_DAC_BIT 0x2
83 /* don't send serial data to caldac n */
84 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
85 /* manual says to set this bit with no explanation */
86 #define DUMMY_BIT 0x40
88 #define CB_DDA_DA_DATA_REG(x) (0x08 + ((x) * 2))
90 /* Offsets for the caldac channels */
91 #define CB_DDA_CALDAC_FINE_GAIN 0
92 #define CB_DDA_CALDAC_COURSE_GAIN 1
93 #define CB_DDA_CALDAC_COURSE_OFFSET 2
94 #define CB_DDA_CALDAC_FINE_OFFSET 3
96 static const struct comedi_lrange cb_pcidda_ranges = {
97 6, {
98 BIP_RANGE(10),
99 BIP_RANGE(5),
100 BIP_RANGE(2.5),
101 UNI_RANGE(10),
102 UNI_RANGE(5),
103 UNI_RANGE(2.5)
107 enum cb_pcidda_boardid {
108 BOARD_DDA02_12,
109 BOARD_DDA04_12,
110 BOARD_DDA08_12,
111 BOARD_DDA02_16,
112 BOARD_DDA04_16,
113 BOARD_DDA08_16,
116 struct cb_pcidda_board {
117 const char *name;
118 int ao_chans;
119 int ao_bits;
122 static const struct cb_pcidda_board cb_pcidda_boards[] = {
123 [BOARD_DDA02_12] = {
124 .name = "pci-dda02/12",
125 .ao_chans = 2,
126 .ao_bits = 12,
128 [BOARD_DDA04_12] = {
129 .name = "pci-dda04/12",
130 .ao_chans = 4,
131 .ao_bits = 12,
133 [BOARD_DDA08_12] = {
134 .name = "pci-dda08/12",
135 .ao_chans = 8,
136 .ao_bits = 12,
138 [BOARD_DDA02_16] = {
139 .name = "pci-dda02/16",
140 .ao_chans = 2,
141 .ao_bits = 16,
143 [BOARD_DDA04_16] = {
144 .name = "pci-dda04/16",
145 .ao_chans = 4,
146 .ao_bits = 16,
148 [BOARD_DDA08_16] = {
149 .name = "pci-dda08/16",
150 .ao_chans = 8,
151 .ao_bits = 16,
155 struct cb_pcidda_private {
156 /* bits last written to da calibration register 1 */
157 unsigned int dac_cal1_bits;
158 /* current range settings for output channels */
159 unsigned int ao_range[MAX_AO_CHANNELS];
160 u16 eeprom_data[EEPROM_SIZE]; /* software copy of board's eeprom */
163 /* lowlevel read from eeprom */
164 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
166 unsigned int value = 0;
167 int i;
168 const int value_width = 16; /* number of bits wide values are */
170 for (i = 1; i <= value_width; i++) {
171 /* read bits most significant bit first */
172 if (inw_p(dev->iobase + DACALIBRATION1) & SERIAL_OUT_BIT)
173 value |= 1 << (value_width - i);
176 return value;
179 /* lowlevel write to eeprom/dac */
180 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
181 unsigned int num_bits)
183 struct cb_pcidda_private *devpriv = dev->private;
184 int i;
186 for (i = 1; i <= num_bits; i++) {
187 /* send bits most significant bit first */
188 if (value & (1 << (num_bits - i)))
189 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
190 else
191 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
192 outw_p(devpriv->dac_cal1_bits, dev->iobase + DACALIBRATION1);
196 /* reads a 16 bit value from board's eeprom */
197 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
198 unsigned int address)
200 unsigned int i;
201 unsigned int cal2_bits;
202 unsigned int value;
203 /* one caldac for every two dac channels */
204 const int max_num_caldacs = 4;
205 /* bits to send to tell eeprom we want to read */
206 const int read_instruction = 0x6;
207 const int instruction_length = 3;
208 const int address_length = 8;
210 /* send serial output stream to eeprom */
211 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
212 /* deactivate caldacs (one caldac for every two channels) */
213 for (i = 0; i < max_num_caldacs; i++)
214 cal2_bits |= DESELECT_CALDAC_BIT(i);
215 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
217 /* tell eeprom we want to read */
218 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
219 /* send address we want to read from */
220 cb_pcidda_serial_out(dev, address, address_length);
222 value = cb_pcidda_serial_in(dev);
224 /* deactivate eeprom */
225 cal2_bits &= ~SELECT_EEPROM_BIT;
226 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
228 return value;
231 /* writes to 8 bit calibration dacs */
232 static void cb_pcidda_write_caldac(struct comedi_device *dev,
233 unsigned int caldac, unsigned int channel,
234 unsigned int value)
236 unsigned int cal2_bits;
237 unsigned int i;
238 /* caldacs use 3 bit channel specification */
239 const int num_channel_bits = 3;
240 const int num_caldac_bits = 8; /* 8 bit calibration dacs */
241 /* one caldac for every two dac channels */
242 const int max_num_caldacs = 4;
244 /* write 3 bit channel */
245 cb_pcidda_serial_out(dev, channel, num_channel_bits);
246 /* write 8 bit caldac value */
247 cb_pcidda_serial_out(dev, value, num_caldac_bits);
250 * latch stream into appropriate caldac deselect reference dac
252 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
253 /* deactivate caldacs (one caldac for every two channels) */
254 for (i = 0; i < max_num_caldacs; i++)
255 cal2_bits |= DESELECT_CALDAC_BIT(i);
256 /* activate the caldac we want */
257 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
258 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
259 /* deactivate caldac */
260 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
261 outw_p(cal2_bits, dev->iobase + DACALIBRATION2);
264 /* set caldacs to eeprom values for given channel and range */
265 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
266 unsigned int range)
268 struct cb_pcidda_private *devpriv = dev->private;
269 unsigned int caldac = channel / 2; /* two caldacs per channel */
270 unsigned int chan = 4 * (channel % 2); /* caldac channel base */
271 unsigned int index = 2 * range + 12 * channel;
272 unsigned int offset;
273 unsigned int gain;
275 /* save range so we can tell when we need to readjust calibration */
276 devpriv->ao_range[channel] = range;
278 /* get values from eeprom data */
279 offset = devpriv->eeprom_data[0x7 + index];
280 gain = devpriv->eeprom_data[0x8 + index];
282 /* set caldacs */
283 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_OFFSET,
284 (offset >> 8) & 0xff);
285 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_OFFSET,
286 offset & 0xff);
287 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_GAIN,
288 (gain >> 8) & 0xff);
289 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_GAIN,
290 gain & 0xff);
293 static int cb_pcidda_ao_insn_write(struct comedi_device *dev,
294 struct comedi_subdevice *s,
295 struct comedi_insn *insn,
296 unsigned int *data)
298 struct cb_pcidda_private *devpriv = dev->private;
299 unsigned int channel = CR_CHAN(insn->chanspec);
300 unsigned int range = CR_RANGE(insn->chanspec);
301 unsigned int ctrl;
303 if (range != devpriv->ao_range[channel])
304 cb_pcidda_calibrate(dev, channel, range);
306 ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
308 switch (range) {
309 case 0:
310 case 3:
311 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
312 break;
313 case 1:
314 case 4:
315 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
316 break;
317 case 2:
318 case 5:
319 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
320 break;
323 if (range > 2)
324 ctrl |= CB_DDA_DA_CTRL_UNIP;
326 outw(ctrl, dev->iobase + CB_DDA_DA_CTRL_REG);
328 outw(data[0], dev->iobase + CB_DDA_DA_DATA_REG(channel));
330 return insn->n;
333 static int cb_pcidda_auto_attach(struct comedi_device *dev,
334 unsigned long context)
336 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
337 const struct cb_pcidda_board *thisboard = NULL;
338 struct cb_pcidda_private *devpriv;
339 struct comedi_subdevice *s;
340 unsigned long iobase_8255;
341 int i;
342 int ret;
344 if (context < ARRAY_SIZE(cb_pcidda_boards))
345 thisboard = &cb_pcidda_boards[context];
346 if (!thisboard)
347 return -ENODEV;
348 dev->board_ptr = thisboard;
349 dev->board_name = thisboard->name;
351 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
352 if (!devpriv)
353 return -ENOMEM;
354 dev->private = devpriv;
356 ret = comedi_pci_enable(dev);
357 if (ret)
358 return ret;
359 dev->iobase = pci_resource_start(pcidev, 3);
360 iobase_8255 = pci_resource_start(pcidev, 2);
362 ret = comedi_alloc_subdevices(dev, 3);
363 if (ret)
364 return ret;
366 s = &dev->subdevices[0];
367 /* analog output subdevice */
368 s->type = COMEDI_SUBD_AO;
369 s->subdev_flags = SDF_WRITABLE;
370 s->n_chan = thisboard->ao_chans;
371 s->maxdata = (1 << thisboard->ao_bits) - 1;
372 s->range_table = &cb_pcidda_ranges;
373 s->insn_write = cb_pcidda_ao_insn_write;
375 /* two 8255 digital io subdevices */
376 for (i = 0; i < 2; i++) {
377 s = &dev->subdevices[1 + i];
378 ret = subdev_8255_init(dev, s, NULL, iobase_8255 + (i * 4));
379 if (ret)
380 return ret;
383 /* Read the caldac eeprom data */
384 for (i = 0; i < EEPROM_SIZE; i++)
385 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
387 /* set calibrations dacs */
388 for (i = 0; i < thisboard->ao_chans; i++)
389 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
391 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
393 return 0;
396 static struct comedi_driver cb_pcidda_driver = {
397 .driver_name = "cb_pcidda",
398 .module = THIS_MODULE,
399 .auto_attach = cb_pcidda_auto_attach,
400 .detach = comedi_pci_disable,
403 static int cb_pcidda_pci_probe(struct pci_dev *dev,
404 const struct pci_device_id *id)
406 return comedi_pci_auto_config(dev, &cb_pcidda_driver,
407 id->driver_data);
410 static DEFINE_PCI_DEVICE_TABLE(cb_pcidda_pci_table) = {
411 { PCI_VDEVICE(CB, 0x0020), BOARD_DDA02_12 },
412 { PCI_VDEVICE(CB, 0x0021), BOARD_DDA04_12 },
413 { PCI_VDEVICE(CB, 0x0022), BOARD_DDA08_12 },
414 { PCI_VDEVICE(CB, 0x0023), BOARD_DDA02_16 },
415 { PCI_VDEVICE(CB, 0x0024), BOARD_DDA04_16 },
416 { PCI_VDEVICE(CB, 0x0025), BOARD_DDA08_16 },
417 { 0 }
419 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
421 static struct pci_driver cb_pcidda_pci_driver = {
422 .name = "cb_pcidda",
423 .id_table = cb_pcidda_pci_table,
424 .probe = cb_pcidda_pci_probe,
425 .remove = comedi_pci_auto_unconfig,
427 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
429 MODULE_AUTHOR("Comedi http://www.comedi.org");
430 MODULE_DESCRIPTION("Comedi low-level driver");
431 MODULE_LICENSE("GPL");