Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4

Linux/drivers/staging/comedi/drivers/cb_das16_cs.c

  1 /*
  2  * cb_das16_cs.c
  3  * Driver for Computer Boards PC-CARD DAS16/16.
  4  *
  5  * COMEDI - Linux Control and Measurement Device Interface
  6  * Copyright (C) 2000, 2001, 2002 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  * PCMCIA support code for this driver is adapted from the dummy_cs.c
 19  * driver of the Linux PCMCIA Card Services package.
 20  *
 21  * The initial developer of the original code is David A. Hinds
 22  * <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
 23  * are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
 24  */
 25 
 26 /*
 27  * Driver: cb_das16_cs
 28  * Description: Computer Boards PC-CARD DAS16/16
 29  * Devices: [ComputerBoards] PC-CARD DAS16/16 (cb_das16_cs),
 30  *   PC-CARD DAS16/16-AO
 31  * Author: ds
 32  * Updated: Mon, 04 Nov 2002 20:04:21 -0800
 33  * Status: experimental
 34  */
 35 
 36 #include <linux/module.h>
 37 #include <linux/interrupt.h>
 38 #include <linux/delay.h>
 39 
 40 #include "../comedi_pcmcia.h"
 41 
 42 #include "comedi_8254.h"
 43 
 44 /*
 45  * Register I/O map
 46  */
 47 #define DAS16CS_AI_DATA_REG             0x00
 48 #define DAS16CS_AI_MUX_REG              0x02
 49 #define DAS16CS_AI_MUX_HI_CHAN(x)       (((x) & 0xf) << 4)
 50 #define DAS16CS_AI_MUX_LO_CHAN(x)       (((x) & 0xf) << 0)
 51 #define DAS16CS_AI_MUX_SINGLE_CHAN(x)   (DAS16CS_AI_MUX_HI_CHAN(x) |    \
 52                                          DAS16CS_AI_MUX_LO_CHAN(x))
 53 #define DAS16CS_MISC1_REG               0x04
 54 #define DAS16CS_MISC1_INTE              BIT(15) /* 1=enable; 0=disable */
 55 #define DAS16CS_MISC1_INT_SRC(x)        (((x) & 0x7) << 12) /* interrupt src */
 56 #define DAS16CS_MISC1_INT_SRC_NONE      DAS16CS_MISC1_INT_SRC(0)
 57 #define DAS16CS_MISC1_INT_SRC_PACER     DAS16CS_MISC1_INT_SRC(1)
 58 #define DAS16CS_MISC1_INT_SRC_EXT       DAS16CS_MISC1_INT_SRC(2)
 59 #define DAS16CS_MISC1_INT_SRC_FNE       DAS16CS_MISC1_INT_SRC(3)
 60 #define DAS16CS_MISC1_INT_SRC_FHF       DAS16CS_MISC1_INT_SRC(4)
 61 #define DAS16CS_MISC1_INT_SRC_EOS       DAS16CS_MISC1_INT_SRC(5)
 62 #define DAS16CS_MISC1_INT_SRC_MASK      DAS16CS_MISC1_INT_SRC(7)
 63 #define DAS16CS_MISC1_OVR               BIT(10) /* ro - 1=FIFO overflow */
 64 #define DAS16CS_MISC1_AI_CONV(x)        (((x) & 0x3) << 8) /* AI convert src */
 65 #define DAS16CS_MISC1_AI_CONV_SW        DAS16CS_MISC1_AI_CONV(0)
 66 #define DAS16CS_MISC1_AI_CONV_EXT_NEG   DAS16CS_MISC1_AI_CONV(1)
 67 #define DAS16CS_MISC1_AI_CONV_EXT_POS   DAS16CS_MISC1_AI_CONV(2)
 68 #define DAS16CS_MISC1_AI_CONV_PACER     DAS16CS_MISC1_AI_CONV(3)
 69 #define DAS16CS_MISC1_AI_CONV_MASK      DAS16CS_MISC1_AI_CONV(3)
 70 #define DAS16CS_MISC1_EOC               BIT(7)  /* ro - 0=busy; 1=ready */
 71 #define DAS16CS_MISC1_SEDIFF            BIT(5)  /* 0=diff; 1=se */
 72 #define DAS16CS_MISC1_INTB              BIT(4)  /* ro - 0=latched; 1=cleared */
 73 #define DAS16CS_MISC1_MA_MASK           (0xf << 0) /* ro - current ai mux */
 74 #define DAS16CS_MISC1_DAC1CS            BIT(3)  /* wo - DAC1 chip select */
 75 #define DAS16CS_MISC1_DACCLK            BIT(2)  /* wo - Serial DAC clock */
 76 #define DAS16CS_MISC1_DACSD             BIT(1)  /* wo - Serial DAC data */
 77 #define DAS16CS_MISC1_DAC0CS            BIT(0)  /* wo - DAC0 chip select */
 78 #define DAS16CS_MISC1_DAC_MASK          (0x0f << 0)
 79 #define DAS16CS_MISC2_REG               0x06
 80 #define DAS16CS_MISC2_BME               BIT(14) /* 1=burst enable; 0=disable */
 81 #define DAS16CS_MISC2_AI_GAIN(x)        (((x) & 0xf) << 8) /* AI gain */
 82 #define DAS16CS_MISC2_AI_GAIN_1         DAS16CS_MISC2_AI_GAIN(4) /* +/-10V */
 83 #define DAS16CS_MISC2_AI_GAIN_2         DAS16CS_MISC2_AI_GAIN(0) /* +/-5V */
 84 #define DAS16CS_MISC2_AI_GAIN_4         DAS16CS_MISC2_AI_GAIN(1) /* +/-2.5V */
 85 #define DAS16CS_MISC2_AI_GAIN_8         DAS16CS_MISC2_AI_GAIN(2) /* +-1.25V */
 86 #define DAS16CS_MISC2_AI_GAIN_MASK      DAS16CS_MISC2_AI_GAIN(0xf)
 87 #define DAS16CS_MISC2_UDIR              BIT(7)  /* 1=dio7:4 output; 0=input */
 88 #define DAS16CS_MISC2_LDIR              BIT(6)  /* 1=dio3:0 output; 0=input */
 89 #define DAS16CS_MISC2_TRGPOL            BIT(5)  /* 1=active lo; 0=hi */
 90 #define DAS16CS_MISC2_TRGSEL            BIT(4)  /* 1=edge; 0=level */
 91 #define DAS16CS_MISC2_FFNE              BIT(3)  /* ro - 1=FIFO not empty */
 92 #define DAS16CS_MISC2_TRGCLR            BIT(3)  /* wo - 1=clr (monstable) */
 93 #define DAS16CS_MISC2_CLK2              BIT(2)  /* 1=10 MHz; 0=1 MHz */
 94 #define DAS16CS_MISC2_CTR1              BIT(1)  /* 1=int. 100 kHz; 0=ext. clk */
 95 #define DAS16CS_MISC2_TRG0              BIT(0)  /* 1=enable; 0=disable */
 96 #define DAS16CS_TIMER_BASE              0x08
 97 #define DAS16CS_DIO_REG                 0x10
 98 
 99 struct das16cs_board {
100         const char *name;
101         int device_id;
102         unsigned int has_ao:1;
103         unsigned int has_4dio:1;
104 };
105 
106 static const struct das16cs_board das16cs_boards[] = {
107         {
108                 .name           = "PC-CARD DAS16/16-AO",
109                 .device_id      = 0x0039,
110                 .has_ao         = 1,
111                 .has_4dio       = 1,
112         }, {
113                 .name           = "PCM-DAS16s/16",
114                 .device_id      = 0x4009,
115         }, {
116                 .name           = "PC-CARD DAS16/16",
117                 .device_id      = 0x0000,       /* unknown */
118         },
119 };
120 
121 struct das16cs_private {
122         unsigned short misc1;
123         unsigned short misc2;
124 };
125 
126 static const struct comedi_lrange das16cs_ai_range = {
127         4, {
128                 BIP_RANGE(10),
129                 BIP_RANGE(5),
130                 BIP_RANGE(2.5),
131                 BIP_RANGE(1.25),
132         }
133 };
134 
135 static int das16cs_ai_eoc(struct comedi_device *dev,
136                           struct comedi_subdevice *s,
137                           struct comedi_insn *insn,
138                           unsigned long context)
139 {
140         unsigned int status;
141 
142         status = inw(dev->iobase + DAS16CS_MISC1_REG);
143         if (status & DAS16CS_MISC1_EOC)
144                 return 0;
145         return -EBUSY;
146 }
147 
148 static int das16cs_ai_insn_read(struct comedi_device *dev,
149                                 struct comedi_subdevice *s,
150                                 struct comedi_insn *insn,
151                                 unsigned int *data)
152 {
153         struct das16cs_private *devpriv = dev->private;
154         int chan = CR_CHAN(insn->chanspec);
155         int range = CR_RANGE(insn->chanspec);
156         int aref = CR_AREF(insn->chanspec);
157         int ret;
158         int i;
159 
160         outw(DAS16CS_AI_MUX_SINGLE_CHAN(chan),
161              dev->iobase + DAS16CS_AI_MUX_REG);
162 
163         /* disable interrupts, software convert */
164         devpriv->misc1 &= ~(DAS16CS_MISC1_INTE | DAS16CS_MISC1_INT_SRC_MASK |
165                               DAS16CS_MISC1_AI_CONV_MASK);
166         if (aref == AREF_DIFF)
167                 devpriv->misc1 &= ~DAS16CS_MISC1_SEDIFF;
168         else
169                 devpriv->misc1 |= DAS16CS_MISC1_SEDIFF;
170         outw(devpriv->misc1, dev->iobase + DAS16CS_MISC1_REG);
171 
172         devpriv->misc2 &= ~(DAS16CS_MISC2_BME | DAS16CS_MISC2_AI_GAIN_MASK);
173         switch (range) {
174         case 0:
175                 devpriv->misc2 |= DAS16CS_MISC2_AI_GAIN_1;
176                 break;
177         case 1:
178                 devpriv->misc2 |= DAS16CS_MISC2_AI_GAIN_2;
179                 break;
180         case 2:
181                 devpriv->misc2 |= DAS16CS_MISC2_AI_GAIN_4;
182                 break;
183         case 3:
184                 devpriv->misc2 |= DAS16CS_MISC2_AI_GAIN_8;
185                 break;
186         }
187         outw(devpriv->misc2, dev->iobase + DAS16CS_MISC2_REG);
188 
189         for (i = 0; i < insn->n; i++) {
190                 outw(0, dev->iobase + DAS16CS_AI_DATA_REG);
191 
192                 ret = comedi_timeout(dev, s, insn, das16cs_ai_eoc, 0);
193                 if (ret)
194                         return ret;
195 
196                 data[i] = inw(dev->iobase + DAS16CS_AI_DATA_REG);
197         }
198 
199         return i;
200 }
201 
202 static int das16cs_ao_insn_write(struct comedi_device *dev,
203                                  struct comedi_subdevice *s,
204                                  struct comedi_insn *insn,
205                                  unsigned int *data)
206 {
207         struct das16cs_private *devpriv = dev->private;
208         unsigned int chan = CR_CHAN(insn->chanspec);
209         unsigned int val = s->readback[chan];
210         unsigned short misc1;
211         int bit;
212         int i;
213 
214         for (i = 0; i < insn->n; i++) {
215                 val = data[i];
216 
217                 outw(devpriv->misc1, dev->iobase + DAS16CS_MISC1_REG);
218                 udelay(1);
219 
220                 /* raise the DACxCS line for the non-selected channel */
221                 misc1 = devpriv->misc1 & ~DAS16CS_MISC1_DAC_MASK;
222                 if (chan)
223                         misc1 |= DAS16CS_MISC1_DAC0CS;
224                 else
225                         misc1 |= DAS16CS_MISC1_DAC1CS;
226 
227                 outw(misc1, dev->iobase + DAS16CS_MISC1_REG);
228                 udelay(1);
229 
230                 for (bit = 15; bit >= 0; bit--) {
231                         if ((val >> bit) & 0x1)
232                                 misc1 |= DAS16CS_MISC1_DACSD;
233                         else
234                                 misc1 &= ~DAS16CS_MISC1_DACSD;
235                         outw(misc1, dev->iobase + DAS16CS_MISC1_REG);
236                         udelay(1);
237                         outw(misc1 | DAS16CS_MISC1_DACCLK,
238                              dev->iobase + DAS16CS_MISC1_REG);
239                         udelay(1);
240                 }
241                 /*
242                  * Make both DAC0CS and DAC1CS high to load
243                  * the new data and update analog the output
244                  */
245                 outw(misc1 | DAS16CS_MISC1_DAC0CS | DAS16CS_MISC1_DAC1CS,
246                      dev->iobase + DAS16CS_MISC1_REG);
247         }
248         s->readback[chan] = val;
249 
250         return insn->n;
251 }
252 
253 static int das16cs_dio_insn_bits(struct comedi_device *dev,
254                                  struct comedi_subdevice *s,
255                                  struct comedi_insn *insn,
256                                  unsigned int *data)
257 {
258         if (comedi_dio_update_state(s, data))
259                 outw(s->state, dev->iobase + DAS16CS_DIO_REG);
260 
261         data[1] = inw(dev->iobase + DAS16CS_DIO_REG);
262 
263         return insn->n;
264 }
265 
266 static int das16cs_dio_insn_config(struct comedi_device *dev,
267                                    struct comedi_subdevice *s,
268                                    struct comedi_insn *insn,
269                                    unsigned int *data)
270 {
271         struct das16cs_private *devpriv = dev->private;
272         unsigned int chan = CR_CHAN(insn->chanspec);
273         unsigned int mask;
274         int ret;
275 
276         if (chan < 4)
277                 mask = 0x0f;
278         else
279                 mask = 0xf0;
280 
281         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
282         if (ret)
283                 return ret;
284 
285         if (s->io_bits & 0xf0)
286                 devpriv->misc2 |= DAS16CS_MISC2_UDIR;
287         else
288                 devpriv->misc2 &= ~DAS16CS_MISC2_UDIR;
289         if (s->io_bits & 0x0f)
290                 devpriv->misc2 |= DAS16CS_MISC2_LDIR;
291         else
292                 devpriv->misc2 &= ~DAS16CS_MISC2_LDIR;
293         outw(devpriv->misc2, dev->iobase + DAS16CS_MISC2_REG);
294 
295         return insn->n;
296 }
297 
298 static int das16cs_counter_insn_config(struct comedi_device *dev,
299                                        struct comedi_subdevice *s,
300                                        struct comedi_insn *insn,
301                                        unsigned int *data)
302 {
303         struct das16cs_private *devpriv = dev->private;
304 
305         switch (data[0]) {
306         case INSN_CONFIG_SET_CLOCK_SRC:
307                 switch (data[1]) {
308                 case 0: /* internal 100 kHz */
309                         devpriv->misc2 |= DAS16CS_MISC2_CTR1;
310                         break;
311                 case 1: /* external */
312                         devpriv->misc2 &= ~DAS16CS_MISC2_CTR1;
313                         break;
314                 default:
315                         return -EINVAL;
316                 }
317                 outw(devpriv->misc2, dev->iobase + DAS16CS_MISC2_REG);
318                 break;
319         case INSN_CONFIG_GET_CLOCK_SRC:
320                 if (devpriv->misc2 & DAS16CS_MISC2_CTR1) {
321                         data[1] = 0;
322                         data[2] = I8254_OSC_BASE_100KHZ;
323                 } else {
324                         data[1] = 1;
325                         data[2] = 0;    /* unknown */
326                 }
327                 break;
328         default:
329                 return -EINVAL;
330         }
331 
332         return insn->n;
333 }
334 
335 static const void *das16cs_find_boardinfo(struct comedi_device *dev,
336                                           struct pcmcia_device *link)
337 {
338         const struct das16cs_board *board;
339         int i;
340 
341         for (i = 0; i < ARRAY_SIZE(das16cs_boards); i++) {
342                 board = &das16cs_boards[i];
343                 if (board->device_id == link->card_id)
344                         return board;
345         }
346 
347         return NULL;
348 }
349 
350 static int das16cs_auto_attach(struct comedi_device *dev,
351                                unsigned long context)
352 {
353         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
354         const struct das16cs_board *board;
355         struct das16cs_private *devpriv;
356         struct comedi_subdevice *s;
357         int ret;
358 
359         board = das16cs_find_boardinfo(dev, link);
360         if (!board)
361                 return -ENODEV;
362         dev->board_ptr = board;
363         dev->board_name = board->name;
364 
365         link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
366         ret = comedi_pcmcia_enable(dev, NULL);
367         if (ret)
368                 return ret;
369         dev->iobase = link->resource[0]->start;
370 
371         link->priv = dev;
372 
373         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
374         if (!devpriv)
375                 return -ENOMEM;
376 
377         dev->pacer = comedi_8254_init(dev->iobase + DAS16CS_TIMER_BASE,
378                                       I8254_OSC_BASE_10MHZ, I8254_IO16, 0);
379         if (!dev->pacer)
380                 return -ENOMEM;
381 
382         ret = comedi_alloc_subdevices(dev, 4);
383         if (ret)
384                 return ret;
385 
386         /* Analog Input subdevice */
387         s = &dev->subdevices[0];
388         s->type         = COMEDI_SUBD_AI;
389         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
390         s->n_chan       = 16;
391         s->maxdata      = 0xffff;
392         s->range_table  = &das16cs_ai_range;
393         s->insn_read    = das16cs_ai_insn_read;
394 
395         /* Analog Output subdevice */
396         s = &dev->subdevices[1];
397         if (board->has_ao) {
398                 s->type         = COMEDI_SUBD_AO;
399                 s->subdev_flags = SDF_WRITABLE;
400                 s->n_chan       = 2;
401                 s->maxdata      = 0xffff;
402                 s->range_table  = &range_bipolar10;
403                 s->insn_write   = &das16cs_ao_insn_write;
404 
405                 ret = comedi_alloc_subdev_readback(s);
406                 if (ret)
407                         return ret;
408         } else {
409                 s->type         = COMEDI_SUBD_UNUSED;
410         }
411 
412         /* Digital I/O subdevice */
413         s = &dev->subdevices[2];
414         s->type         = COMEDI_SUBD_DIO;
415         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
416         s->n_chan       = board->has_4dio ? 4 : 8;
417         s->maxdata      = 1;
418         s->range_table  = &range_digital;
419         s->insn_bits    = das16cs_dio_insn_bits;
420         s->insn_config  = das16cs_dio_insn_config;
421 
422         /* Counter subdevice (8254) */
423         s = &dev->subdevices[3];
424         comedi_8254_subdevice_init(s, dev->pacer);
425 
426         dev->pacer->insn_config = das16cs_counter_insn_config;
427 
428         /* counters 1 and 2 are used internally for the pacer */
429         comedi_8254_set_busy(dev->pacer, 1, true);
430         comedi_8254_set_busy(dev->pacer, 2, true);
431 
432         return 0;
433 }
434 
435 static struct comedi_driver driver_das16cs = {
436         .driver_name    = "cb_das16_cs",
437         .module         = THIS_MODULE,
438         .auto_attach    = das16cs_auto_attach,
439         .detach         = comedi_pcmcia_disable,
440 };
441 
442 static int das16cs_pcmcia_attach(struct pcmcia_device *link)
443 {
444         return comedi_pcmcia_auto_config(link, &driver_das16cs);
445 }
446 
447 static const struct pcmcia_device_id das16cs_id_table[] = {
448         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0039),
449         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4009),
450         PCMCIA_DEVICE_NULL
451 };
452 MODULE_DEVICE_TABLE(pcmcia, das16cs_id_table);
453 
454 static struct pcmcia_driver das16cs_driver = {
455         .name           = "cb_das16_cs",
456         .owner          = THIS_MODULE,
457         .id_table       = das16cs_id_table,
458         .probe          = das16cs_pcmcia_attach,
459         .remove         = comedi_pcmcia_auto_unconfig,
460 };
461 module_comedi_pcmcia_driver(driver_das16cs, das16cs_driver);
462 
463 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>");
464 MODULE_DESCRIPTION("Comedi driver for Computer Boards PC-CARD DAS16/16");
465 MODULE_LICENSE("GPL");
466 

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