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