staging: comedi: remove FSF address from boilerplate text
[deliverable/linux.git] / drivers / staging / comedi / drivers / addi_apci_3501.c
1 /*
2 * addi_apci_3501.c
3 * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module.
4 * Project manager: Eric Stolz
5 *
6 * ADDI-DATA GmbH
7 * Dieselstrasse 3
8 * D-77833 Ottersweier
9 * Tel: +19(0)7223/9493-0
10 * Fax: +49(0)7223/9493-92
11 * http://www.addi-data.com
12 * info@addi-data.com
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 */
24
25 #include <linux/pci.h>
26 #include <linux/interrupt.h>
27 #include <linux/sched.h>
28
29 #include "../comedidev.h"
30 #include "comedi_fc.h"
31 #include "amcc_s5933.h"
32
33 /*
34 * PCI bar 1 register I/O map
35 */
36 #define APCI3501_AO_CTRL_STATUS_REG 0x00
37 #define APCI3501_AO_CTRL_BIPOLAR (1 << 0)
38 #define APCI3501_AO_STATUS_READY (1 << 8)
39 #define APCI3501_AO_DATA_REG 0x04
40 #define APCI3501_AO_DATA_CHAN(x) ((x) << 0)
41 #define APCI3501_AO_DATA_VAL(x) ((x) << 8)
42 #define APCI3501_AO_DATA_BIPOLAR (1 << 31)
43 #define APCI3501_AO_TRIG_SCS_REG 0x08
44 #define APCI3501_TIMER_SYNC_REG 0x20
45 #define APCI3501_TIMER_RELOAD_REG 0x24
46 #define APCI3501_TIMER_TIMEBASE_REG 0x28
47 #define APCI3501_TIMER_CTRL_REG 0x2c
48 #define APCI3501_TIMER_STATUS_REG 0x30
49 #define APCI3501_TIMER_IRQ_REG 0x34
50 #define APCI3501_TIMER_WARN_RELOAD_REG 0x38
51 #define APCI3501_TIMER_WARN_TIMEBASE_REG 0x3c
52 #define APCI3501_DO_REG 0x40
53 #define APCI3501_DI_REG 0x50
54
55 /*
56 * AMCC S5933 NVRAM
57 */
58 #define NVRAM_USER_DATA_START 0x100
59
60 #define NVCMD_BEGIN_READ (0x7 << 5)
61 #define NVCMD_LOAD_LOW (0x4 << 5)
62 #define NVCMD_LOAD_HIGH (0x5 << 5)
63
64 /*
65 * Function types stored in the eeprom
66 */
67 #define EEPROM_DIGITALINPUT 0
68 #define EEPROM_DIGITALOUTPUT 1
69 #define EEPROM_ANALOGINPUT 2
70 #define EEPROM_ANALOGOUTPUT 3
71 #define EEPROM_TIMER 4
72 #define EEPROM_WATCHDOG 5
73 #define EEPROM_TIMER_WATCHDOG_COUNTER 10
74
75 struct apci3501_private {
76 int i_IobaseAmcc;
77 struct task_struct *tsk_Current;
78 unsigned char b_TimerSelectMode;
79 };
80
81 static struct comedi_lrange apci3501_ao_range = {
82 2, {
83 BIP_RANGE(10),
84 UNI_RANGE(10)
85 }
86 };
87
88 static int apci3501_wait_for_dac(struct comedi_device *dev)
89 {
90 unsigned int status;
91
92 do {
93 status = inl(dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
94 } while (!(status & APCI3501_AO_STATUS_READY));
95
96 return 0;
97 }
98
99 static int apci3501_ao_insn_write(struct comedi_device *dev,
100 struct comedi_subdevice *s,
101 struct comedi_insn *insn,
102 unsigned int *data)
103 {
104 unsigned int chan = CR_CHAN(insn->chanspec);
105 unsigned int range = CR_RANGE(insn->chanspec);
106 unsigned int val = 0;
107 int i;
108 int ret;
109
110 /*
111 * All analog output channels have the same output range.
112 * 14-bit bipolar: 0-10V
113 * 13-bit unipolar: +/-10V
114 * Changing the range of one channel changes all of them!
115 */
116 if (range) {
117 outl(0, dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
118 } else {
119 val |= APCI3501_AO_DATA_BIPOLAR;
120 outl(APCI3501_AO_CTRL_BIPOLAR,
121 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
122 }
123
124 val |= APCI3501_AO_DATA_CHAN(chan);
125
126 for (i = 0; i < insn->n; i++) {
127 if (range == 1) {
128 if (data[i] > 0x1fff) {
129 dev_err(dev->class_dev,
130 "Unipolar resolution is only 13-bits\n");
131 return -EINVAL;
132 }
133 }
134
135 ret = apci3501_wait_for_dac(dev);
136 if (ret)
137 return ret;
138
139 outl(val | APCI3501_AO_DATA_VAL(data[i]),
140 dev->iobase + APCI3501_AO_DATA_REG);
141 }
142
143 return insn->n;
144 }
145
146 #include "addi-data/hwdrv_apci3501.c"
147
148 static int apci3501_di_insn_bits(struct comedi_device *dev,
149 struct comedi_subdevice *s,
150 struct comedi_insn *insn,
151 unsigned int *data)
152 {
153 data[1] = inl(dev->iobase + APCI3501_DI_REG) & 0x3;
154
155 return insn->n;
156 }
157
158 static int apci3501_do_insn_bits(struct comedi_device *dev,
159 struct comedi_subdevice *s,
160 struct comedi_insn *insn,
161 unsigned int *data)
162 {
163 unsigned int mask = data[0];
164 unsigned int bits = data[1];
165
166 s->state = inl(dev->iobase + APCI3501_DO_REG);
167 if (mask) {
168 s->state &= ~mask;
169 s->state |= (bits & mask);
170
171 outl(s->state, dev->iobase + APCI3501_DO_REG);
172 }
173
174 data[1] = s->state;
175
176 return insn->n;
177 }
178
179 static void apci3501_eeprom_wait(unsigned long iobase)
180 {
181 unsigned char val;
182
183 do {
184 val = inb(iobase + AMCC_OP_REG_MCSR_NVCMD);
185 } while (val & 0x80);
186 }
187
188 static unsigned short apci3501_eeprom_readw(unsigned long iobase,
189 unsigned short addr)
190 {
191 unsigned short val = 0;
192 unsigned char tmp;
193 unsigned char i;
194
195 /* Add the offset to the start of the user data */
196 addr += NVRAM_USER_DATA_START;
197
198 for (i = 0; i < 2; i++) {
199 /* Load the low 8 bit address */
200 outb(NVCMD_LOAD_LOW, iobase + AMCC_OP_REG_MCSR_NVCMD);
201 apci3501_eeprom_wait(iobase);
202 outb((addr + i) & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
203 apci3501_eeprom_wait(iobase);
204
205 /* Load the high 8 bit address */
206 outb(NVCMD_LOAD_HIGH, iobase + AMCC_OP_REG_MCSR_NVCMD);
207 apci3501_eeprom_wait(iobase);
208 outb(((addr + i) >> 8) & 0xff,
209 iobase + AMCC_OP_REG_MCSR_NVDATA);
210 apci3501_eeprom_wait(iobase);
211
212 /* Read the eeprom data byte */
213 outb(NVCMD_BEGIN_READ, iobase + AMCC_OP_REG_MCSR_NVCMD);
214 apci3501_eeprom_wait(iobase);
215 tmp = inb(iobase + AMCC_OP_REG_MCSR_NVDATA);
216 apci3501_eeprom_wait(iobase);
217
218 if (i == 0)
219 val |= tmp;
220 else
221 val |= (tmp << 8);
222 }
223
224 return val;
225 }
226
227 static int apci3501_eeprom_get_ao_n_chan(struct comedi_device *dev)
228 {
229 struct apci3501_private *devpriv = dev->private;
230 unsigned long iobase = devpriv->i_IobaseAmcc;
231 unsigned char nfuncs;
232 int i;
233
234 nfuncs = apci3501_eeprom_readw(iobase, 10) & 0xff;
235
236 /* Read functionality details */
237 for (i = 0; i < nfuncs; i++) {
238 unsigned short offset = i * 4;
239 unsigned short addr;
240 unsigned char func;
241 unsigned short val;
242
243 func = apci3501_eeprom_readw(iobase, 12 + offset) & 0x3f;
244 addr = apci3501_eeprom_readw(iobase, 14 + offset);
245
246 if (func == EEPROM_ANALOGOUTPUT) {
247 val = apci3501_eeprom_readw(iobase, addr + 10);
248 return (val >> 4) & 0x3ff;
249 }
250 }
251 return 0;
252 }
253
254 static int apci3501_eeprom_insn_read(struct comedi_device *dev,
255 struct comedi_subdevice *s,
256 struct comedi_insn *insn,
257 unsigned int *data)
258 {
259 struct apci3501_private *devpriv = dev->private;
260 unsigned short addr = CR_CHAN(insn->chanspec);
261
262 data[0] = apci3501_eeprom_readw(devpriv->i_IobaseAmcc, 2 * addr);
263
264 return insn->n;
265 }
266
267 static irqreturn_t apci3501_interrupt(int irq, void *d)
268 {
269 struct comedi_device *dev = d;
270 struct apci3501_private *devpriv = dev->private;
271 unsigned int ui_Timer_AOWatchdog;
272 unsigned long ul_Command1;
273 int i_temp;
274
275 /* Disable Interrupt */
276 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
277 ul_Command1 = (ul_Command1 & 0xFFFFF9FDul);
278 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
279
280 ui_Timer_AOWatchdog = inl(dev->iobase + APCI3501_TIMER_IRQ_REG) & 0x1;
281 if ((!ui_Timer_AOWatchdog)) {
282 comedi_error(dev, "IRQ from unknown source");
283 return IRQ_NONE;
284 }
285
286 /* Enable Interrupt Send a signal to from kernel to user space */
287 send_sig(SIGIO, devpriv->tsk_Current, 0);
288 ul_Command1 = inl(dev->iobase + APCI3501_TIMER_CTRL_REG);
289 ul_Command1 = ((ul_Command1 & 0xFFFFF9FDul) | 1 << 1);
290 outl(ul_Command1, dev->iobase + APCI3501_TIMER_CTRL_REG);
291 i_temp = inl(dev->iobase + APCI3501_TIMER_STATUS_REG) & 0x1;
292
293 return IRQ_HANDLED;
294 }
295
296 static int apci3501_reset(struct comedi_device *dev)
297 {
298 unsigned int val;
299 int chan;
300 int ret;
301
302 /* Reset all digital outputs to "0" */
303 outl(0x0, dev->iobase + APCI3501_DO_REG);
304
305 /* Default all analog outputs to 0V (bipolar) */
306 outl(APCI3501_AO_CTRL_BIPOLAR,
307 dev->iobase + APCI3501_AO_CTRL_STATUS_REG);
308 val = APCI3501_AO_DATA_BIPOLAR | APCI3501_AO_DATA_VAL(0);
309
310 /* Set all analog output channels */
311 for (chan = 0; chan < 8; chan++) {
312 ret = apci3501_wait_for_dac(dev);
313 if (ret) {
314 dev_warn(dev->class_dev,
315 "%s: DAC not-ready for channel %i\n",
316 __func__, chan);
317 } else {
318 outl(val | APCI3501_AO_DATA_CHAN(chan),
319 dev->iobase + APCI3501_AO_DATA_REG);
320 }
321 }
322
323 return 0;
324 }
325
326 static int apci3501_auto_attach(struct comedi_device *dev,
327 unsigned long context_unused)
328 {
329 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
330 struct apci3501_private *devpriv;
331 struct comedi_subdevice *s;
332 int ao_n_chan;
333 int ret;
334
335 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
336 if (!devpriv)
337 return -ENOMEM;
338 dev->private = devpriv;
339
340 ret = comedi_pci_enable(dev);
341 if (ret)
342 return ret;
343
344 dev->iobase = pci_resource_start(pcidev, 1);
345 devpriv->i_IobaseAmcc = pci_resource_start(pcidev, 0);
346
347 ao_n_chan = apci3501_eeprom_get_ao_n_chan(dev);
348
349 if (pcidev->irq > 0) {
350 ret = request_irq(pcidev->irq, apci3501_interrupt, IRQF_SHARED,
351 dev->board_name, dev);
352 if (ret == 0)
353 dev->irq = pcidev->irq;
354 }
355
356 ret = comedi_alloc_subdevices(dev, 5);
357 if (ret)
358 return ret;
359
360 /* Initialize the analog output subdevice */
361 s = &dev->subdevices[0];
362 if (ao_n_chan) {
363 s->type = COMEDI_SUBD_AO;
364 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
365 s->n_chan = ao_n_chan;
366 s->maxdata = 0x3fff;
367 s->range_table = &apci3501_ao_range;
368 s->insn_write = apci3501_ao_insn_write;
369 } else {
370 s->type = COMEDI_SUBD_UNUSED;
371 }
372
373 /* Initialize the digital input subdevice */
374 s = &dev->subdevices[1];
375 s->type = COMEDI_SUBD_DI;
376 s->subdev_flags = SDF_READABLE;
377 s->n_chan = 2;
378 s->maxdata = 1;
379 s->range_table = &range_digital;
380 s->insn_bits = apci3501_di_insn_bits;
381
382 /* Initialize the digital output subdevice */
383 s = &dev->subdevices[2];
384 s->type = COMEDI_SUBD_DO;
385 s->subdev_flags = SDF_WRITEABLE;
386 s->n_chan = 2;
387 s->maxdata = 1;
388 s->range_table = &range_digital;
389 s->insn_bits = apci3501_do_insn_bits;
390
391 /* Initialize the timer/watchdog subdevice */
392 s = &dev->subdevices[3];
393 s->type = COMEDI_SUBD_TIMER;
394 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
395 s->n_chan = 1;
396 s->maxdata = 0;
397 s->len_chanlist = 1;
398 s->range_table = &range_digital;
399 s->insn_write = i_APCI3501_StartStopWriteTimerCounterWatchdog;
400 s->insn_read = i_APCI3501_ReadTimerCounterWatchdog;
401 s->insn_config = i_APCI3501_ConfigTimerCounterWatchdog;
402
403 /* Initialize the eeprom subdevice */
404 s = &dev->subdevices[4];
405 s->type = COMEDI_SUBD_MEMORY;
406 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
407 s->n_chan = 256;
408 s->maxdata = 0xffff;
409 s->insn_read = apci3501_eeprom_insn_read;
410
411 apci3501_reset(dev);
412 return 0;
413 }
414
415 static void apci3501_detach(struct comedi_device *dev)
416 {
417 if (dev->iobase)
418 apci3501_reset(dev);
419 if (dev->irq)
420 free_irq(dev->irq, dev);
421 comedi_pci_disable(dev);
422 }
423
424 static struct comedi_driver apci3501_driver = {
425 .driver_name = "addi_apci_3501",
426 .module = THIS_MODULE,
427 .auto_attach = apci3501_auto_attach,
428 .detach = apci3501_detach,
429 };
430
431 static int apci3501_pci_probe(struct pci_dev *dev,
432 const struct pci_device_id *id)
433 {
434 return comedi_pci_auto_config(dev, &apci3501_driver, id->driver_data);
435 }
436
437 static DEFINE_PCI_DEVICE_TABLE(apci3501_pci_table) = {
438 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, 0x3001) },
439 { 0 }
440 };
441 MODULE_DEVICE_TABLE(pci, apci3501_pci_table);
442
443 static struct pci_driver apci3501_pci_driver = {
444 .name = "addi_apci_3501",
445 .id_table = apci3501_pci_table,
446 .probe = apci3501_pci_probe,
447 .remove = comedi_pci_auto_unconfig,
448 };
449 module_comedi_pci_driver(apci3501_driver, apci3501_pci_driver);
450
451 MODULE_DESCRIPTION("ADDI-DATA APCI-3501 Analog output board");
452 MODULE_AUTHOR("Comedi http://www.comedi.org");
453 MODULE_LICENSE("GPL");
This page took 0.051964 seconds and 5 git commands to generate.