This source file includes following definitions.
- cb_pcidda_serial_in
- cb_pcidda_serial_out
- cb_pcidda_read_eeprom
- cb_pcidda_write_caldac
- cb_pcidda_calibrate
- cb_pcidda_ao_insn_write
- cb_pcidda_auto_attach
- cb_pcidda_pci_probe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <linux/module.h>
30
31 #include "../comedi_pci.h"
32
33 #include "8255.h"
34
35 #define EEPROM_SIZE 128
36
37 #define MAX_AO_CHANNELS 8
38
39
40 #define CB_DDA_DIO0_8255_BASE 0x00
41 #define CB_DDA_DIO1_8255_BASE 0x04
42
43
44 #define CB_DDA_DA_CTRL_REG 0x00
45 #define CB_DDA_DA_CTRL_SU BIT(0)
46 #define CB_DDA_DA_CTRL_EN BIT(1)
47 #define CB_DDA_DA_CTRL_DAC(x) ((x) << 2)
48 #define CB_DDA_DA_CTRL_RANGE2V5 (0 << 6)
49 #define CB_DDA_DA_CTRL_RANGE5V (2 << 6)
50 #define CB_DDA_DA_CTRL_RANGE10V (3 << 6)
51 #define CB_DDA_DA_CTRL_UNIP BIT(8)
52
53 #define DACALIBRATION1 4
54
55
56 #define SERIAL_IN_BIT 0x1
57 #define CAL_CHANNEL_MASK (0x7 << 1)
58 #define CAL_CHANNEL_BITS(channel) (((channel) << 1) & CAL_CHANNEL_MASK)
59
60 #define CAL_COUNTER_MASK 0x1f
61
62 #define CAL_COUNTER_OVERFLOW_BIT 0x20
63
64 #define AO_BELOW_REF_BIT 0x40
65 #define SERIAL_OUT_BIT 0x80
66
67 #define DACALIBRATION2 6
68 #define SELECT_EEPROM_BIT 0x1
69
70 #define DESELECT_REF_DAC_BIT 0x2
71
72 #define DESELECT_CALDAC_BIT(n) (0x4 << (n))
73
74 #define DUMMY_BIT 0x40
75
76 #define CB_DDA_DA_DATA_REG(x) (0x08 + ((x) * 2))
77
78
79 #define CB_DDA_CALDAC_FINE_GAIN 0
80 #define CB_DDA_CALDAC_COURSE_GAIN 1
81 #define CB_DDA_CALDAC_COURSE_OFFSET 2
82 #define CB_DDA_CALDAC_FINE_OFFSET 3
83
84 static const struct comedi_lrange cb_pcidda_ranges = {
85 6, {
86 BIP_RANGE(10),
87 BIP_RANGE(5),
88 BIP_RANGE(2.5),
89 UNI_RANGE(10),
90 UNI_RANGE(5),
91 UNI_RANGE(2.5)
92 }
93 };
94
95 enum cb_pcidda_boardid {
96 BOARD_DDA02_12,
97 BOARD_DDA04_12,
98 BOARD_DDA08_12,
99 BOARD_DDA02_16,
100 BOARD_DDA04_16,
101 BOARD_DDA08_16,
102 };
103
104 struct cb_pcidda_board {
105 const char *name;
106 int ao_chans;
107 int ao_bits;
108 };
109
110 static const struct cb_pcidda_board cb_pcidda_boards[] = {
111 [BOARD_DDA02_12] = {
112 .name = "pci-dda02/12",
113 .ao_chans = 2,
114 .ao_bits = 12,
115 },
116 [BOARD_DDA04_12] = {
117 .name = "pci-dda04/12",
118 .ao_chans = 4,
119 .ao_bits = 12,
120 },
121 [BOARD_DDA08_12] = {
122 .name = "pci-dda08/12",
123 .ao_chans = 8,
124 .ao_bits = 12,
125 },
126 [BOARD_DDA02_16] = {
127 .name = "pci-dda02/16",
128 .ao_chans = 2,
129 .ao_bits = 16,
130 },
131 [BOARD_DDA04_16] = {
132 .name = "pci-dda04/16",
133 .ao_chans = 4,
134 .ao_bits = 16,
135 },
136 [BOARD_DDA08_16] = {
137 .name = "pci-dda08/16",
138 .ao_chans = 8,
139 .ao_bits = 16,
140 },
141 };
142
143 struct cb_pcidda_private {
144 unsigned long daqio;
145
146 unsigned int dac_cal1_bits;
147
148 unsigned int ao_range[MAX_AO_CHANNELS];
149 u16 eeprom_data[EEPROM_SIZE];
150 };
151
152
153 static unsigned int cb_pcidda_serial_in(struct comedi_device *dev)
154 {
155 struct cb_pcidda_private *devpriv = dev->private;
156 unsigned int value = 0;
157 int i;
158 const int value_width = 16;
159
160 for (i = 1; i <= value_width; i++) {
161
162 if (inw_p(devpriv->daqio + DACALIBRATION1) & SERIAL_OUT_BIT)
163 value |= 1 << (value_width - i);
164 }
165
166 return value;
167 }
168
169
170 static void cb_pcidda_serial_out(struct comedi_device *dev, unsigned int value,
171 unsigned int num_bits)
172 {
173 struct cb_pcidda_private *devpriv = dev->private;
174 int i;
175
176 for (i = 1; i <= num_bits; i++) {
177
178 if (value & (1 << (num_bits - i)))
179 devpriv->dac_cal1_bits |= SERIAL_IN_BIT;
180 else
181 devpriv->dac_cal1_bits &= ~SERIAL_IN_BIT;
182 outw_p(devpriv->dac_cal1_bits, devpriv->daqio + DACALIBRATION1);
183 }
184 }
185
186
187 static unsigned int cb_pcidda_read_eeprom(struct comedi_device *dev,
188 unsigned int address)
189 {
190 struct cb_pcidda_private *devpriv = dev->private;
191 unsigned int i;
192 unsigned int cal2_bits;
193 unsigned int value;
194
195 const int max_num_caldacs = 4;
196
197 const int read_instruction = 0x6;
198 const int instruction_length = 3;
199 const int address_length = 8;
200
201
202 cal2_bits = SELECT_EEPROM_BIT | DESELECT_REF_DAC_BIT | DUMMY_BIT;
203
204 for (i = 0; i < max_num_caldacs; i++)
205 cal2_bits |= DESELECT_CALDAC_BIT(i);
206 outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
207
208
209 cb_pcidda_serial_out(dev, read_instruction, instruction_length);
210
211 cb_pcidda_serial_out(dev, address, address_length);
212
213 value = cb_pcidda_serial_in(dev);
214
215
216 cal2_bits &= ~SELECT_EEPROM_BIT;
217 outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
218
219 return value;
220 }
221
222
223 static void cb_pcidda_write_caldac(struct comedi_device *dev,
224 unsigned int caldac, unsigned int channel,
225 unsigned int value)
226 {
227 struct cb_pcidda_private *devpriv = dev->private;
228 unsigned int cal2_bits;
229 unsigned int i;
230
231 const int num_channel_bits = 3;
232 const int num_caldac_bits = 8;
233
234 const int max_num_caldacs = 4;
235
236
237 cb_pcidda_serial_out(dev, channel, num_channel_bits);
238
239 cb_pcidda_serial_out(dev, value, num_caldac_bits);
240
241
242
243
244 cal2_bits = DESELECT_REF_DAC_BIT | DUMMY_BIT;
245
246 for (i = 0; i < max_num_caldacs; i++)
247 cal2_bits |= DESELECT_CALDAC_BIT(i);
248
249 cal2_bits &= ~DESELECT_CALDAC_BIT(caldac);
250 outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
251
252 cal2_bits |= DESELECT_CALDAC_BIT(caldac);
253 outw_p(cal2_bits, devpriv->daqio + DACALIBRATION2);
254 }
255
256
257 static void cb_pcidda_calibrate(struct comedi_device *dev, unsigned int channel,
258 unsigned int range)
259 {
260 struct cb_pcidda_private *devpriv = dev->private;
261 unsigned int caldac = channel / 2;
262 unsigned int chan = 4 * (channel % 2);
263 unsigned int index = 2 * range + 12 * channel;
264 unsigned int offset;
265 unsigned int gain;
266
267
268 devpriv->ao_range[channel] = range;
269
270
271 offset = devpriv->eeprom_data[0x7 + index];
272 gain = devpriv->eeprom_data[0x8 + index];
273
274
275 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_OFFSET,
276 (offset >> 8) & 0xff);
277 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_OFFSET,
278 offset & 0xff);
279 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_COURSE_GAIN,
280 (gain >> 8) & 0xff);
281 cb_pcidda_write_caldac(dev, caldac, chan + CB_DDA_CALDAC_FINE_GAIN,
282 gain & 0xff);
283 }
284
285 static int cb_pcidda_ao_insn_write(struct comedi_device *dev,
286 struct comedi_subdevice *s,
287 struct comedi_insn *insn,
288 unsigned int *data)
289 {
290 struct cb_pcidda_private *devpriv = dev->private;
291 unsigned int channel = CR_CHAN(insn->chanspec);
292 unsigned int range = CR_RANGE(insn->chanspec);
293 unsigned int ctrl;
294 unsigned int i;
295
296 if (range != devpriv->ao_range[channel])
297 cb_pcidda_calibrate(dev, channel, range);
298
299 ctrl = CB_DDA_DA_CTRL_EN | CB_DDA_DA_CTRL_DAC(channel);
300
301 switch (range) {
302 case 0:
303 case 3:
304 ctrl |= CB_DDA_DA_CTRL_RANGE10V;
305 break;
306 case 1:
307 case 4:
308 ctrl |= CB_DDA_DA_CTRL_RANGE5V;
309 break;
310 case 2:
311 case 5:
312 ctrl |= CB_DDA_DA_CTRL_RANGE2V5;
313 break;
314 }
315
316 if (range > 2)
317 ctrl |= CB_DDA_DA_CTRL_UNIP;
318
319 outw(ctrl, devpriv->daqio + CB_DDA_DA_CTRL_REG);
320
321 for (i = 0; i < insn->n; i++)
322 outw(data[i], devpriv->daqio + CB_DDA_DA_DATA_REG(channel));
323
324 return insn->n;
325 }
326
327 static int cb_pcidda_auto_attach(struct comedi_device *dev,
328 unsigned long context)
329 {
330 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
331 const struct cb_pcidda_board *board = NULL;
332 struct cb_pcidda_private *devpriv;
333 struct comedi_subdevice *s;
334 int i;
335 int ret;
336
337 if (context < ARRAY_SIZE(cb_pcidda_boards))
338 board = &cb_pcidda_boards[context];
339 if (!board)
340 return -ENODEV;
341 dev->board_ptr = board;
342 dev->board_name = board->name;
343
344 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
345 if (!devpriv)
346 return -ENOMEM;
347
348 ret = comedi_pci_enable(dev);
349 if (ret)
350 return ret;
351 dev->iobase = pci_resource_start(pcidev, 2);
352 devpriv->daqio = pci_resource_start(pcidev, 3);
353
354 ret = comedi_alloc_subdevices(dev, 3);
355 if (ret)
356 return ret;
357
358 s = &dev->subdevices[0];
359
360 s->type = COMEDI_SUBD_AO;
361 s->subdev_flags = SDF_WRITABLE;
362 s->n_chan = board->ao_chans;
363 s->maxdata = (1 << board->ao_bits) - 1;
364 s->range_table = &cb_pcidda_ranges;
365 s->insn_write = cb_pcidda_ao_insn_write;
366
367
368 for (i = 0; i < 2; i++) {
369 s = &dev->subdevices[1 + i];
370 ret = subdev_8255_init(dev, s, NULL, i * I8255_SIZE);
371 if (ret)
372 return ret;
373 }
374
375
376 for (i = 0; i < EEPROM_SIZE; i++)
377 devpriv->eeprom_data[i] = cb_pcidda_read_eeprom(dev, i);
378
379
380 for (i = 0; i < board->ao_chans; i++)
381 cb_pcidda_calibrate(dev, i, devpriv->ao_range[i]);
382
383 return 0;
384 }
385
386 static struct comedi_driver cb_pcidda_driver = {
387 .driver_name = "cb_pcidda",
388 .module = THIS_MODULE,
389 .auto_attach = cb_pcidda_auto_attach,
390 .detach = comedi_pci_detach,
391 };
392
393 static int cb_pcidda_pci_probe(struct pci_dev *dev,
394 const struct pci_device_id *id)
395 {
396 return comedi_pci_auto_config(dev, &cb_pcidda_driver,
397 id->driver_data);
398 }
399
400 static const struct pci_device_id cb_pcidda_pci_table[] = {
401 { PCI_VDEVICE(CB, 0x0020), BOARD_DDA02_12 },
402 { PCI_VDEVICE(CB, 0x0021), BOARD_DDA04_12 },
403 { PCI_VDEVICE(CB, 0x0022), BOARD_DDA08_12 },
404 { PCI_VDEVICE(CB, 0x0023), BOARD_DDA02_16 },
405 { PCI_VDEVICE(CB, 0x0024), BOARD_DDA04_16 },
406 { PCI_VDEVICE(CB, 0x0025), BOARD_DDA08_16 },
407 { 0 }
408 };
409 MODULE_DEVICE_TABLE(pci, cb_pcidda_pci_table);
410
411 static struct pci_driver cb_pcidda_pci_driver = {
412 .name = "cb_pcidda",
413 .id_table = cb_pcidda_pci_table,
414 .probe = cb_pcidda_pci_probe,
415 .remove = comedi_pci_auto_unconfig,
416 };
417 module_comedi_pci_driver(cb_pcidda_driver, cb_pcidda_pci_driver);
418
419 MODULE_AUTHOR("Comedi http://www.comedi.org");
420 MODULE_DESCRIPTION("Comedi low-level driver");
421 MODULE_LICENSE("GPL");