GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / staging / comedi / drivers / adl_pci6208.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * adl_pci6208.c
4  * Comedi driver for ADLink 6208 series cards
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  */
9
10 /*
11  * Driver: adl_pci6208
12  * Description: ADLink PCI-6208/6216 Series Multi-channel Analog Output Cards
13  * Devices: [ADLink] PCI-6208 (adl_pci6208), PCI-6216
14  * Author: nsyeow <nsyeow@pd.jaring.my>
15  * Updated: Wed, 11 Feb 2015 11:37:18 +0000
16  * Status: untested
17  *
18  * Configuration Options: not applicable, uses PCI auto config
19  *
20  * All supported devices share the same PCI device ID and are treated as a
21  * PCI-6216 with 16 analog output channels.  On a PCI-6208, the upper 8
22  * channels exist in registers, but don't go to DAC chips.
23  */
24
25 #include <linux/module.h>
26 #include <linux/delay.h>
27
28 #include "../comedi_pci.h"
29
30 /*
31  * PCI-6208/6216-GL register map
32  */
33 #define PCI6208_AO_CONTROL(x)           (0x00 + (2 * (x)))
34 #define PCI6208_AO_STATUS               0x00
35 #define PCI6208_AO_STATUS_DATA_SEND     BIT(0)
36 #define PCI6208_DIO                     0x40
37 #define PCI6208_DIO_DO_MASK             (0x0f)
38 #define PCI6208_DIO_DO_SHIFT            (0)
39 #define PCI6208_DIO_DI_MASK             (0xf0)
40 #define PCI6208_DIO_DI_SHIFT            (4)
41
42 static int pci6208_ao_eoc(struct comedi_device *dev,
43                           struct comedi_subdevice *s,
44                           struct comedi_insn *insn,
45                           unsigned long context)
46 {
47         unsigned int status;
48
49         status = inw(dev->iobase + PCI6208_AO_STATUS);
50         if ((status & PCI6208_AO_STATUS_DATA_SEND) == 0)
51                 return 0;
52         return -EBUSY;
53 }
54
55 static int pci6208_ao_insn_write(struct comedi_device *dev,
56                                  struct comedi_subdevice *s,
57                                  struct comedi_insn *insn,
58                                  unsigned int *data)
59 {
60         unsigned int chan = CR_CHAN(insn->chanspec);
61         int ret;
62         int i;
63
64         for (i = 0; i < insn->n; i++) {
65                 unsigned int val = data[i];
66
67                 /* D/A transfer rate is 2.2us */
68                 ret = comedi_timeout(dev, s, insn, pci6208_ao_eoc, 0);
69                 if (ret)
70                         return ret;
71
72                 /* the hardware expects two's complement values */
73                 outw(comedi_offset_munge(s, val),
74                      dev->iobase + PCI6208_AO_CONTROL(chan));
75
76                 s->readback[chan] = val;
77         }
78
79         return insn->n;
80 }
81
82 static int pci6208_di_insn_bits(struct comedi_device *dev,
83                                 struct comedi_subdevice *s,
84                                 struct comedi_insn *insn,
85                                 unsigned int *data)
86 {
87         unsigned int val;
88
89         val = inw(dev->iobase + PCI6208_DIO);
90         val = (val & PCI6208_DIO_DI_MASK) >> PCI6208_DIO_DI_SHIFT;
91
92         data[1] = val;
93
94         return insn->n;
95 }
96
97 static int pci6208_do_insn_bits(struct comedi_device *dev,
98                                 struct comedi_subdevice *s,
99                                 struct comedi_insn *insn,
100                                 unsigned int *data)
101 {
102         if (comedi_dio_update_state(s, data))
103                 outw(s->state, dev->iobase + PCI6208_DIO);
104
105         data[1] = s->state;
106
107         return insn->n;
108 }
109
110 static int pci6208_auto_attach(struct comedi_device *dev,
111                                unsigned long context_unused)
112 {
113         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
114         struct comedi_subdevice *s;
115         unsigned int val;
116         int ret;
117
118         ret = comedi_pci_enable(dev);
119         if (ret)
120                 return ret;
121         dev->iobase = pci_resource_start(pcidev, 2);
122
123         ret = comedi_alloc_subdevices(dev, 3);
124         if (ret)
125                 return ret;
126
127         s = &dev->subdevices[0];
128         /* analog output subdevice */
129         s->type         = COMEDI_SUBD_AO;
130         s->subdev_flags = SDF_WRITABLE;
131         s->n_chan       = 16;   /* Only 8 usable on PCI-6208 */
132         s->maxdata      = 0xffff;
133         s->range_table  = &range_bipolar10;
134         s->insn_write   = pci6208_ao_insn_write;
135
136         ret = comedi_alloc_subdev_readback(s);
137         if (ret)
138                 return ret;
139
140         s = &dev->subdevices[1];
141         /* digital input subdevice */
142         s->type         = COMEDI_SUBD_DI;
143         s->subdev_flags = SDF_READABLE;
144         s->n_chan       = 4;
145         s->maxdata      = 1;
146         s->range_table  = &range_digital;
147         s->insn_bits    = pci6208_di_insn_bits;
148
149         s = &dev->subdevices[2];
150         /* digital output subdevice */
151         s->type         = COMEDI_SUBD_DO;
152         s->subdev_flags = SDF_WRITABLE;
153         s->n_chan       = 4;
154         s->maxdata      = 1;
155         s->range_table  = &range_digital;
156         s->insn_bits    = pci6208_do_insn_bits;
157
158         /*
159          * Get the read back signals from the digital outputs
160          * and save it as the initial state for the subdevice.
161          */
162         val = inw(dev->iobase + PCI6208_DIO);
163         val = (val & PCI6208_DIO_DO_MASK) >> PCI6208_DIO_DO_SHIFT;
164         s->state        = val;
165
166         return 0;
167 }
168
169 static struct comedi_driver adl_pci6208_driver = {
170         .driver_name    = "adl_pci6208",
171         .module         = THIS_MODULE,
172         .auto_attach    = pci6208_auto_attach,
173         .detach         = comedi_pci_detach,
174 };
175
176 static int adl_pci6208_pci_probe(struct pci_dev *dev,
177                                  const struct pci_device_id *id)
178 {
179         return comedi_pci_auto_config(dev, &adl_pci6208_driver,
180                                       id->driver_data);
181 }
182
183 static const struct pci_device_id adl_pci6208_pci_table[] = {
184         { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, 0x6208) },
185         { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
186                          0x9999, 0x6208) },
187         { 0 }
188 };
189 MODULE_DEVICE_TABLE(pci, adl_pci6208_pci_table);
190
191 static struct pci_driver adl_pci6208_pci_driver = {
192         .name           = "adl_pci6208",
193         .id_table       = adl_pci6208_pci_table,
194         .probe          = adl_pci6208_pci_probe,
195         .remove         = comedi_pci_auto_unconfig,
196 };
197 module_comedi_pci_driver(adl_pci6208_driver, adl_pci6208_pci_driver);
198
199 MODULE_AUTHOR("Comedi http://www.comedi.org");
200 MODULE_DESCRIPTION("Comedi driver for ADLink 6208 series cards");
201 MODULE_LICENSE("GPL");