Merge tag 'hsi-for-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-hsi
[cascardo/linux.git] / drivers / staging / comedi / drivers / mf6x4.c
1 /*
2  *  comedi/drivers/mf6x4.c
3  *  Driver for Humusoft MF634 and MF624 Data acquisition cards
4  *
5  *  COMEDI - Linux Control and Measurement Device Interface
6  *  Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  */
18 /*
19  * Driver: mf6x4
20  * Description: Humusoft MF634 and MF624 Data acquisition card driver
21  * Devices: Humusoft MF634, Humusoft MF624
22  * Author: Rostislav Lisovy <lisovy@gmail.com>
23  * Status: works
24  * Updated:
25  * Configuration Options: none
26  */
27
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include "../comedidev.h"
32
33 /* Registers present in BAR0 memory region */
34 #define MF624_GPIOC_R                                   0x54
35
36 #define MF6X4_GPIOC_EOLC /* End Of Last Conversion */   (1 << 17)
37 #define MF6X4_GPIOC_LDAC /* Load DACs */                (1 << 23)
38 #define MF6X4_GPIOC_DACEN                               (1 << 26)
39
40 /* BAR1 registers */
41 #define MF6X4_DIN_R                                     0x10
42 #define MF6X4_DIN_M                                     0xff
43 #define MF6X4_DOUT_R                                    0x10
44 #define MF6X4_DOUT_M                                    0xff
45
46 #define MF6X4_ADSTART_R                                 0x20
47 #define MF6X4_ADDATA_R                                  0x00
48 #define MF6X4_ADCTRL_R                                  0x00
49 #define MF6X4_ADCTRL_M                                  0xff
50
51 #define MF6X4_DA0_R                                     0x20
52 #define MF6X4_DA1_R                                     0x22
53 #define MF6X4_DA2_R                                     0x24
54 #define MF6X4_DA3_R                                     0x26
55 #define MF6X4_DA4_R                                     0x28
56 #define MF6X4_DA5_R                                     0x2a
57 #define MF6X4_DA6_R                                     0x2c
58 #define MF6X4_DA7_R                                     0x2e
59 /* Map DAC cahnnel id to real HW-dependent offset value */
60 #define MF6X4_DAC_R(x)                                  (0x20 + ((x) * 2))
61 #define MF6X4_DA_M                                      0x3fff
62
63 /* BAR2 registers */
64 #define MF634_GPIOC_R                                   0x68
65
66 enum mf6x4_boardid {
67         BOARD_MF634,
68         BOARD_MF624,
69 };
70
71 struct mf6x4_board {
72         const char *name;
73         unsigned int bar_nums[3]; /* We need to keep track of the
74                                      order of BARs used by the cards */
75 };
76
77 static const struct mf6x4_board mf6x4_boards[] = {
78         [BOARD_MF634] = {
79                 .name           = "mf634",
80                 .bar_nums       = {0, 2, 3},
81         },
82         [BOARD_MF624] = {
83                 .name           = "mf624",
84                 .bar_nums       = {0, 2, 4},
85         },
86 };
87
88 struct mf6x4_private {
89         /*
90          * Documentation for both MF634 and MF624 describes registers
91          * present in BAR0, 1 and 2 regions.
92          * The real (i.e. in HW) BAR numbers are different for MF624
93          * and MF634 yet we will call them 0, 1, 2
94          */
95         void __iomem *bar0_mem;
96         void __iomem *bar2_mem;
97
98         /*
99          * This configuration register has the same function and fields
100          * for both cards however it lies in different BARs on different
101          * offsets -- this variable makes the access easier
102          */
103         void __iomem *gpioc_R;
104
105         /* DAC value cache -- used for insn_read function */
106         int ao_readback[8];
107 };
108
109 static int mf6x4_di_insn_bits(struct comedi_device *dev,
110                               struct comedi_subdevice *s,
111                               struct comedi_insn *insn,
112                               unsigned int *data)
113 {
114         data[1] = ioread16(dev->mmio + MF6X4_DIN_R) & MF6X4_DIN_M;
115
116         return insn->n;
117 }
118
119 static int mf6x4_do_insn_bits(struct comedi_device *dev,
120                               struct comedi_subdevice *s,
121                               struct comedi_insn *insn,
122                               unsigned int *data)
123 {
124         if (comedi_dio_update_state(s, data))
125                 iowrite16(s->state & MF6X4_DOUT_M, dev->mmio + MF6X4_DOUT_R);
126
127         data[1] = s->state;
128
129         return insn->n;
130 }
131
132 static int mf6x4_ai_eoc(struct comedi_device *dev,
133                         struct comedi_subdevice *s,
134                         struct comedi_insn *insn,
135                         unsigned long context)
136 {
137         struct mf6x4_private *devpriv = dev->private;
138         unsigned int status;
139
140         status = ioread32(devpriv->gpioc_R);
141         if (status & MF6X4_GPIOC_EOLC)
142                 return 0;
143         return -EBUSY;
144 }
145
146 static int mf6x4_ai_insn_read(struct comedi_device *dev,
147                               struct comedi_subdevice *s,
148                               struct comedi_insn *insn,
149                               unsigned int *data)
150 {
151         int chan = CR_CHAN(insn->chanspec);
152         int ret;
153         int i;
154         int d;
155
156         /* Set the ADC channel number in the scan list */
157         iowrite16((1 << chan) & MF6X4_ADCTRL_M, dev->mmio + MF6X4_ADCTRL_R);
158
159         for (i = 0; i < insn->n; i++) {
160                 /* Trigger ADC conversion by reading ADSTART */
161                 ioread16(dev->mmio + MF6X4_ADSTART_R);
162
163                 ret = comedi_timeout(dev, s, insn, mf6x4_ai_eoc, 0);
164                 if (ret)
165                         return ret;
166
167                 /* Read the actual value */
168                 d = ioread16(dev->mmio + MF6X4_ADDATA_R);
169                 d &= s->maxdata;
170                 data[i] = d;
171         }
172
173         iowrite16(0x0, dev->mmio + MF6X4_ADCTRL_R);
174
175         return insn->n;
176 }
177
178 static int mf6x4_ao_insn_write(struct comedi_device *dev,
179                                struct comedi_subdevice *s,
180                                struct comedi_insn *insn,
181                                unsigned int *data)
182 {
183         struct mf6x4_private *devpriv = dev->private;
184         unsigned int chan = CR_CHAN(insn->chanspec);
185         uint32_t gpioc;
186         int i;
187
188         /* Enable instantaneous update of converters outputs + Enable DACs */
189         gpioc = ioread32(devpriv->gpioc_R);
190         iowrite32((gpioc & ~MF6X4_GPIOC_LDAC) | MF6X4_GPIOC_DACEN,
191                   devpriv->gpioc_R);
192
193         for (i = 0; i < insn->n; i++) {
194                 iowrite16(data[i] & MF6X4_DA_M, dev->mmio + MF6X4_DAC_R(chan));
195
196                 devpriv->ao_readback[chan] = data[i];
197         }
198
199         return insn->n;
200 }
201
202 static int mf6x4_ao_insn_read(struct comedi_device *dev,
203                               struct comedi_subdevice *s,
204                               struct comedi_insn *insn, unsigned int *data)
205 {
206         struct mf6x4_private *devpriv = dev->private;
207         unsigned int chan = CR_CHAN(insn->chanspec);
208         int i;
209
210         for (i = 0; i < insn->n; i++)
211                 data[i] = devpriv->ao_readback[chan];
212
213         return insn->n;
214 }
215
216 static int mf6x4_auto_attach(struct comedi_device *dev, unsigned long context)
217 {
218         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
219         const struct mf6x4_board *board = NULL;
220         struct mf6x4_private *devpriv;
221         struct comedi_subdevice *s;
222         int ret;
223
224         if (context < ARRAY_SIZE(mf6x4_boards))
225                 board = &mf6x4_boards[context];
226         else
227                 return -ENODEV;
228
229         dev->board_ptr = board;
230         dev->board_name = board->name;
231
232         ret = comedi_pci_enable(dev);
233         if (ret)
234                 return ret;
235
236         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
237         if (!devpriv)
238                 return -ENOMEM;
239
240         devpriv->bar0_mem = pci_ioremap_bar(pcidev, board->bar_nums[0]);
241         if (!devpriv->bar0_mem)
242                 return -ENODEV;
243
244         dev->mmio = pci_ioremap_bar(pcidev, board->bar_nums[1]);
245         if (!dev->mmio)
246                 return -ENODEV;
247
248         devpriv->bar2_mem = pci_ioremap_bar(pcidev, board->bar_nums[2]);
249         if (!devpriv->bar2_mem)
250                 return -ENODEV;
251
252         if (board == &mf6x4_boards[BOARD_MF634])
253                 devpriv->gpioc_R = devpriv->bar2_mem + MF634_GPIOC_R;
254         else
255                 devpriv->gpioc_R = devpriv->bar0_mem + MF624_GPIOC_R;
256
257
258         ret = comedi_alloc_subdevices(dev, 4);
259         if (ret)
260                 return ret;
261
262         /* ADC */
263         s = &dev->subdevices[0];
264         s->type = COMEDI_SUBD_AI;
265         s->subdev_flags = SDF_READABLE | SDF_GROUND;
266         s->n_chan = 8;
267         s->maxdata = 0x3fff; /* 14 bits ADC */
268         s->range_table = &range_bipolar10;
269         s->insn_read = mf6x4_ai_insn_read;
270
271         /* DAC */
272         s = &dev->subdevices[1];
273         s->type = COMEDI_SUBD_AO;
274         s->subdev_flags = SDF_WRITABLE;
275         s->n_chan = 8;
276         s->maxdata = 0x3fff; /* 14 bits DAC */
277         s->range_table = &range_bipolar10;
278         s->insn_write = mf6x4_ao_insn_write;
279         s->insn_read = mf6x4_ao_insn_read;
280
281         /* DIN */
282         s = &dev->subdevices[2];
283         s->type = COMEDI_SUBD_DI;
284         s->subdev_flags = SDF_READABLE;
285         s->n_chan = 8;
286         s->maxdata = 1;
287         s->range_table = &range_digital;
288         s->insn_bits = mf6x4_di_insn_bits;
289
290         /* DOUT */
291         s = &dev->subdevices[3];
292         s->type = COMEDI_SUBD_DO;
293         s->subdev_flags = SDF_WRITABLE;
294         s->n_chan = 8;
295         s->maxdata = 1;
296         s->range_table = &range_digital;
297         s->insn_bits = mf6x4_do_insn_bits;
298
299         return 0;
300 }
301
302 static void mf6x4_detach(struct comedi_device *dev)
303 {
304         struct mf6x4_private *devpriv = dev->private;
305
306         if (devpriv->bar0_mem)
307                 iounmap(devpriv->bar0_mem);
308         if (dev->mmio)
309                 iounmap(dev->mmio);
310         if (devpriv->bar2_mem)
311                 iounmap(devpriv->bar2_mem);
312
313         comedi_pci_disable(dev);
314 }
315
316 static struct comedi_driver mf6x4_driver = {
317         .driver_name    = "mf6x4",
318         .module         = THIS_MODULE,
319         .auto_attach    = mf6x4_auto_attach,
320         .detach         = mf6x4_detach,
321 };
322
323 static int mf6x4_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
324 {
325         return comedi_pci_auto_config(dev, &mf6x4_driver, id->driver_data);
326 }
327
328 static const struct pci_device_id mf6x4_pci_table[] = {
329         { PCI_VDEVICE(HUMUSOFT, 0x0634), BOARD_MF634 },
330         { PCI_VDEVICE(HUMUSOFT, 0x0624), BOARD_MF624 },
331         { 0 }
332 };
333 MODULE_DEVICE_TABLE(pci, mf6x4_pci_table);
334
335 static struct pci_driver mf6x4_pci_driver = {
336         .name           = "mf6x4",
337         .id_table       = mf6x4_pci_table,
338         .probe          = mf6x4_pci_probe,
339         .remove         = comedi_pci_auto_unconfig,
340 };
341
342 module_comedi_pci_driver(mf6x4_driver, mf6x4_pci_driver);
343
344 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");
345 MODULE_DESCRIPTION("Comedi MF634 and MF624 DAQ cards driver");
346 MODULE_LICENSE("GPL");