GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / comedi / drivers / ni_670x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Comedi driver for NI 670x devices
4  *
5  * COMEDI - Linux Control and Measurement Device Interface
6  * Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7  */
8
9 /*
10  * Driver: ni_670x
11  * Description: National Instruments 670x
12  * Author: Bart Joris <bjoris@advalvas.be>
13  * Updated: Wed, 11 Dec 2002 18:25:35 -0800
14  * Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
15  * Status: unknown
16  *
17  * Commands are not supported.
18  *
19  * Manuals:
20  *   322110a.pdf        PCI/PXI-6704 User Manual
21  *   322110b.pdf        PCI/PXI-6703/6704 User Manual
22  */
23
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/slab.h>
27 #include <linux/comedi/comedi_pci.h>
28
29 #define AO_VALUE_OFFSET                 0x00
30 #define AO_CHAN_OFFSET                  0x0c
31 #define AO_STATUS_OFFSET                0x10
32 #define AO_CONTROL_OFFSET               0x10
33 #define DIO_PORT0_DIR_OFFSET    0x20
34 #define DIO_PORT0_DATA_OFFSET   0x24
35 #define DIO_PORT1_DIR_OFFSET    0x28
36 #define DIO_PORT1_DATA_OFFSET   0x2c
37 #define MISC_STATUS_OFFSET              0x14
38 #define MISC_CONTROL_OFFSET             0x14
39
40 enum ni_670x_boardid {
41         BOARD_PCI6703,
42         BOARD_PXI6704,
43         BOARD_PCI6704,
44 };
45
46 struct ni_670x_board {
47         const char *name;
48         unsigned short ao_chans;
49 };
50
51 static const struct ni_670x_board ni_670x_boards[] = {
52         [BOARD_PCI6703] = {
53                 .name           = "PCI-6703",
54                 .ao_chans       = 16,
55         },
56         [BOARD_PXI6704] = {
57                 .name           = "PXI-6704",
58                 .ao_chans       = 32,
59         },
60         [BOARD_PCI6704] = {
61                 .name           = "PCI-6704",
62                 .ao_chans       = 32,
63         },
64 };
65
66 struct ni_670x_private {
67         int boardtype;
68         int dio;
69 };
70
71 static int ni_670x_ao_insn_write(struct comedi_device *dev,
72                                  struct comedi_subdevice *s,
73                                  struct comedi_insn *insn,
74                                  unsigned int *data)
75 {
76         unsigned int chan = CR_CHAN(insn->chanspec);
77         unsigned int val = s->readback[chan];
78         int i;
79
80         /*
81          * Channel number mapping:
82          *
83          * NI 6703/ NI 6704 | NI 6704 Only
84          * -------------------------------
85          * vch(0)  :  0     | ich(16) :  1
86          * vch(1)  :  2     | ich(17) :  3
87          * ...              | ...
88          * vch(15) : 30     | ich(31) : 31
89          */
90         for (i = 0; i < insn->n; i++) {
91                 val = data[i];
92                 /* First write in channel register which channel to use */
93                 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
94                        dev->mmio + AO_CHAN_OFFSET);
95                 /* write channel value */
96                 writel(val, dev->mmio + AO_VALUE_OFFSET);
97         }
98         s->readback[chan] = val;
99
100         return insn->n;
101 }
102
103 static int ni_670x_dio_insn_bits(struct comedi_device *dev,
104                                  struct comedi_subdevice *s,
105                                  struct comedi_insn *insn,
106                                  unsigned int *data)
107 {
108         if (comedi_dio_update_state(s, data))
109                 writel(s->state, dev->mmio + DIO_PORT0_DATA_OFFSET);
110
111         data[1] = readl(dev->mmio + DIO_PORT0_DATA_OFFSET);
112
113         return insn->n;
114 }
115
116 static int ni_670x_dio_insn_config(struct comedi_device *dev,
117                                    struct comedi_subdevice *s,
118                                    struct comedi_insn *insn,
119                                    unsigned int *data)
120 {
121         int ret;
122
123         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
124         if (ret)
125                 return ret;
126
127         writel(s->io_bits, dev->mmio + DIO_PORT0_DIR_OFFSET);
128
129         return insn->n;
130 }
131
132 /* ripped from mite.h and mite_setup2() to avoid mite dependency */
133 #define MITE_IODWBSR    0xc0     /* IO Device Window Base Size Register */
134 #define WENAB           BIT(7) /* window enable */
135
136 static int ni_670x_mite_init(struct pci_dev *pcidev)
137 {
138         void __iomem *mite_base;
139         u32 main_phys_addr;
140
141         /* ioremap the MITE registers (BAR 0) temporarily */
142         mite_base = pci_ioremap_bar(pcidev, 0);
143         if (!mite_base)
144                 return -ENOMEM;
145
146         /* set data window to main registers (BAR 1) */
147         main_phys_addr = pci_resource_start(pcidev, 1);
148         writel(main_phys_addr | WENAB, mite_base + MITE_IODWBSR);
149
150         /* finished with MITE registers */
151         iounmap(mite_base);
152         return 0;
153 }
154
155 static int ni_670x_auto_attach(struct comedi_device *dev,
156                                unsigned long context)
157 {
158         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
159         const struct ni_670x_board *board = NULL;
160         struct ni_670x_private *devpriv;
161         struct comedi_subdevice *s;
162         int ret;
163         int i;
164
165         if (context < ARRAY_SIZE(ni_670x_boards))
166                 board = &ni_670x_boards[context];
167         if (!board)
168                 return -ENODEV;
169         dev->board_ptr = board;
170         dev->board_name = board->name;
171
172         ret = comedi_pci_enable(dev);
173         if (ret)
174                 return ret;
175
176         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
177         if (!devpriv)
178                 return -ENOMEM;
179
180         ret = ni_670x_mite_init(pcidev);
181         if (ret)
182                 return ret;
183
184         dev->mmio = pci_ioremap_bar(pcidev, 1);
185         if (!dev->mmio)
186                 return -ENOMEM;
187
188         ret = comedi_alloc_subdevices(dev, 2);
189         if (ret)
190                 return ret;
191
192         s = &dev->subdevices[0];
193         /* analog output subdevice */
194         s->type = COMEDI_SUBD_AO;
195         s->subdev_flags = SDF_WRITABLE;
196         s->n_chan = board->ao_chans;
197         s->maxdata = 0xffff;
198         if (s->n_chan == 32) {
199                 const struct comedi_lrange **range_table_list;
200
201                 range_table_list = kmalloc_array(32,
202                                                  sizeof(struct comedi_lrange *),
203                                                  GFP_KERNEL);
204                 if (!range_table_list)
205                         return -ENOMEM;
206                 s->range_table_list = range_table_list;
207                 for (i = 0; i < 16; i++) {
208                         range_table_list[i] = &range_bipolar10;
209                         range_table_list[16 + i] = &range_0_20mA;
210                 }
211         } else {
212                 s->range_table = &range_bipolar10;
213         }
214         s->insn_write = ni_670x_ao_insn_write;
215
216         ret = comedi_alloc_subdev_readback(s);
217         if (ret)
218                 return ret;
219
220         s = &dev->subdevices[1];
221         /* digital i/o subdevice */
222         s->type = COMEDI_SUBD_DIO;
223         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
224         s->n_chan = 8;
225         s->maxdata = 1;
226         s->range_table = &range_digital;
227         s->insn_bits = ni_670x_dio_insn_bits;
228         s->insn_config = ni_670x_dio_insn_config;
229
230         /* Config of misc registers */
231         writel(0x10, dev->mmio + MISC_CONTROL_OFFSET);
232         /* Config of ao registers */
233         writel(0x00, dev->mmio + AO_CONTROL_OFFSET);
234
235         return 0;
236 }
237
238 static void ni_670x_detach(struct comedi_device *dev)
239 {
240         struct comedi_subdevice *s;
241
242         comedi_pci_detach(dev);
243         if (dev->n_subdevices) {
244                 s = &dev->subdevices[0];
245                 if (s)
246                         kfree(s->range_table_list);
247         }
248 }
249
250 static struct comedi_driver ni_670x_driver = {
251         .driver_name    = "ni_670x",
252         .module         = THIS_MODULE,
253         .auto_attach    = ni_670x_auto_attach,
254         .detach         = ni_670x_detach,
255 };
256
257 static int ni_670x_pci_probe(struct pci_dev *dev,
258                              const struct pci_device_id *id)
259 {
260         return comedi_pci_auto_config(dev, &ni_670x_driver, id->driver_data);
261 }
262
263 static const struct pci_device_id ni_670x_pci_table[] = {
264         { PCI_VDEVICE(NI, 0x1290), BOARD_PCI6704 },
265         { PCI_VDEVICE(NI, 0x1920), BOARD_PXI6704 },
266         { PCI_VDEVICE(NI, 0x2c90), BOARD_PCI6703 },
267         { 0 }
268 };
269 MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
270
271 static struct pci_driver ni_670x_pci_driver = {
272         .name           = "ni_670x",
273         .id_table       = ni_670x_pci_table,
274         .probe          = ni_670x_pci_probe,
275         .remove         = comedi_pci_auto_unconfig,
276 };
277 module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
278
279 MODULE_AUTHOR("Comedi https://www.comedi.org");
280 MODULE_DESCRIPTION("Comedi low-level driver");
281 MODULE_LICENSE("GPL");