Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/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 *bar1_mem;
 97         void __iomem *bar2_mem;
 98 
 99         /*
100          * This configuration register has the same function and fields
101          * for both cards however it lies in different BARs on different
102          * offsets -- this variable makes the access easier
103          */
104         void __iomem *gpioc_R;
105 
106         /* DAC value cache -- used for insn_read function */
107         int ao_readback[8];
108 };
109 
110 static int mf6x4_di_insn_bits(struct comedi_device *dev,
111                                struct comedi_subdevice *s,
112                                struct comedi_insn *insn, unsigned int *data)
113 {
114         struct mf6x4_private *devpriv = dev->private;
115 
116         data[1] = ioread16(devpriv->bar1_mem + MF6X4_DIN_R) & MF6X4_DIN_M;
117 
118         return insn->n;
119 }
120 
121 static int mf6x4_do_insn_bits(struct comedi_device *dev,
122                                struct comedi_subdevice *s,
123                                struct comedi_insn *insn, unsigned int *data)
124 {
125         struct mf6x4_private *devpriv = dev->private;
126 
127         if (comedi_dio_update_state(s, data))
128                 iowrite16(s->state & MF6X4_DOUT_M,
129                           devpriv->bar1_mem + MF6X4_DOUT_R);
130 
131         data[1] = s->state;
132 
133         return insn->n;
134 }
135 
136 static int mf6x4_ai_eoc(struct comedi_device *dev,
137                         struct comedi_subdevice *s,
138                         struct comedi_insn *insn,
139                         unsigned long context)
140 {
141         struct mf6x4_private *devpriv = dev->private;
142         unsigned int status;
143 
144         status = ioread32(devpriv->gpioc_R);
145         if (status & MF6X4_GPIOC_EOLC)
146                 return 0;
147         return -EBUSY;
148 }
149 
150 static int mf6x4_ai_insn_read(struct comedi_device *dev,
151                               struct comedi_subdevice *s,
152                               struct comedi_insn *insn, unsigned int *data)
153 {
154         struct mf6x4_private *devpriv = dev->private;
155         int chan = CR_CHAN(insn->chanspec);
156         int ret;
157         int i;
158         int d;
159 
160         /* Set the ADC channel number in the scan list */
161         iowrite16((1 << chan) & MF6X4_ADCTRL_M,
162                   devpriv->bar1_mem + MF6X4_ADCTRL_R);
163 
164         for (i = 0; i < insn->n; i++) {
165                 /* Trigger ADC conversion by reading ADSTART */
166                 ioread16(devpriv->bar1_mem + MF6X4_ADSTART_R);
167 
168                 ret = comedi_timeout(dev, s, insn, mf6x4_ai_eoc, 0);
169                 if (ret)
170                         return ret;
171 
172                 /* Read the actual value */
173                 d = ioread16(devpriv->bar1_mem + MF6X4_ADDATA_R);
174                 d &= s->maxdata;
175                 data[i] = d;
176         }
177 
178         iowrite16(0x0, devpriv->bar1_mem + MF6X4_ADCTRL_R);
179 
180         return insn->n;
181 }
182 
183 static int mf6x4_ao_insn_write(struct comedi_device *dev,
184                                struct comedi_subdevice *s,
185                                struct comedi_insn *insn, unsigned int *data)
186 {
187         struct mf6x4_private *devpriv = dev->private;
188         unsigned int chan = CR_CHAN(insn->chanspec);
189         uint32_t gpioc;
190         int i;
191 
192         /* Enable instantaneous update of converters outputs + Enable DACs */
193         gpioc = ioread32(devpriv->gpioc_R);
194         iowrite32((gpioc & ~MF6X4_GPIOC_LDAC) | MF6X4_GPIOC_DACEN,
195                   devpriv->gpioc_R);
196 
197         for (i = 0; i < insn->n; i++) {
198                 iowrite16(data[i] & MF6X4_DA_M,
199                           devpriv->bar1_mem + MF6X4_DAC_R(chan));
200 
201                 devpriv->ao_readback[chan] = data[i];
202         }
203 
204         return insn->n;
205 }
206 
207 static int mf6x4_ao_insn_read(struct comedi_device *dev,
208                               struct comedi_subdevice *s,
209                               struct comedi_insn *insn, unsigned int *data)
210 {
211         struct mf6x4_private *devpriv = dev->private;
212         unsigned int chan = CR_CHAN(insn->chanspec);
213         int i;
214 
215         for (i = 0; i < insn->n; i++)
216                 data[i] = devpriv->ao_readback[chan];
217 
218         return insn->n;
219 }
220 
221 static int mf6x4_auto_attach(struct comedi_device *dev, unsigned long context)
222 {
223         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
224         const struct mf6x4_board *board = NULL;
225         struct mf6x4_private *devpriv;
226         struct comedi_subdevice *s;
227         int ret;
228 
229         if (context < ARRAY_SIZE(mf6x4_boards))
230                 board = &mf6x4_boards[context];
231         else
232                 return -ENODEV;
233 
234         dev->board_ptr = board;
235         dev->board_name = board->name;
236 
237         ret = comedi_pci_enable(dev);
238         if (ret)
239                 return ret;
240 
241         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
242         if (!devpriv)
243                 return -ENOMEM;
244 
245         devpriv->bar0_mem = pci_ioremap_bar(pcidev, board->bar_nums[0]);
246         if (!devpriv->bar0_mem)
247                 return -ENODEV;
248 
249         devpriv->bar1_mem = pci_ioremap_bar(pcidev, board->bar_nums[1]);
250         if (!devpriv->bar1_mem)
251                 return -ENODEV;
252 
253         devpriv->bar2_mem = pci_ioremap_bar(pcidev, board->bar_nums[2]);
254         if (!devpriv->bar2_mem)
255                 return -ENODEV;
256 
257         if (board == &mf6x4_boards[BOARD_MF634])
258                 devpriv->gpioc_R = devpriv->bar2_mem + MF634_GPIOC_R;
259         else
260                 devpriv->gpioc_R = devpriv->bar0_mem + MF624_GPIOC_R;
261 
262 
263         ret = comedi_alloc_subdevices(dev, 4);
264         if (ret)
265                 return ret;
266 
267         /* ADC */
268         s = &dev->subdevices[0];
269         s->type = COMEDI_SUBD_AI;
270         s->subdev_flags = SDF_READABLE | SDF_GROUND;
271         s->n_chan = 8;
272         s->maxdata = 0x3fff; /* 14 bits ADC */
273         s->range_table = &range_bipolar10;
274         s->insn_read = mf6x4_ai_insn_read;
275 
276         /* DAC */
277         s = &dev->subdevices[1];
278         s->type = COMEDI_SUBD_AO;
279         s->subdev_flags = SDF_WRITABLE;
280         s->n_chan = 8;
281         s->maxdata = 0x3fff; /* 14 bits DAC */
282         s->range_table = &range_bipolar10;
283         s->insn_write = mf6x4_ao_insn_write;
284         s->insn_read = mf6x4_ao_insn_read;
285 
286         /* DIN */
287         s = &dev->subdevices[2];
288         s->type = COMEDI_SUBD_DI;
289         s->subdev_flags = SDF_READABLE;
290         s->n_chan = 8;
291         s->maxdata = 1;
292         s->range_table = &range_digital;
293         s->insn_bits = mf6x4_di_insn_bits;
294 
295         /* DOUT */
296         s = &dev->subdevices[3];
297         s->type = COMEDI_SUBD_DO;
298         s->subdev_flags = SDF_WRITABLE;
299         s->n_chan = 8;
300         s->maxdata = 1;
301         s->range_table = &range_digital;
302         s->insn_bits = mf6x4_do_insn_bits;
303 
304         return 0;
305 }
306 
307 static void mf6x4_detach(struct comedi_device *dev)
308 {
309         struct mf6x4_private *devpriv = dev->private;
310 
311         if (devpriv->bar0_mem)
312                 iounmap(devpriv->bar0_mem);
313         if (devpriv->bar1_mem)
314                 iounmap(devpriv->bar1_mem);
315         if (devpriv->bar2_mem)
316                 iounmap(devpriv->bar2_mem);
317 
318         comedi_pci_disable(dev);
319 }
320 
321 static struct comedi_driver mf6x4_driver = {
322         .driver_name    = "mf6x4",
323         .module         = THIS_MODULE,
324         .auto_attach    = mf6x4_auto_attach,
325         .detach         = mf6x4_detach,
326 };
327 
328 static int mf6x4_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
329 {
330         return comedi_pci_auto_config(dev, &mf6x4_driver, id->driver_data);
331 }
332 
333 static const struct pci_device_id mf6x4_pci_table[] = {
334         { PCI_VDEVICE(HUMUSOFT, 0x0634), BOARD_MF634 },
335         { PCI_VDEVICE(HUMUSOFT, 0x0624), BOARD_MF624 },
336         { 0 }
337 };
338 MODULE_DEVICE_TABLE(pci, mf6x4_pci_table);
339 
340 static struct pci_driver mf6x4_pci_driver = {
341         .name           = "mf6x4",
342         .id_table       = mf6x4_pci_table,
343         .probe          = mf6x4_pci_probe,
344         .remove         = comedi_pci_auto_unconfig,
345 };
346 
347 module_comedi_pci_driver(mf6x4_driver, mf6x4_pci_driver);
348 
349 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");
350 MODULE_DESCRIPTION("Comedi MF634 and MF624 DAQ cards driver");
351 MODULE_LICENSE("GPL");
352 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us