Linux-libre 2.6.32.42-gnu1
[librecmc/linux-libre.git] / drivers / staging / comedi / drivers / adv_pci1723.c
1 /*******************************************************************************
2    comedi/drivers/pci1723.c
3
4    COMEDI - Linux Control and Measurement Device Interface
5    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 *******************************************************************************/
22 /*
23 Driver: adv_pci1723
24 Description: Advantech PCI-1723
25 Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
26 Devices: [Advantech] PCI-1723 (adv_pci1723)
27 Updated: Mon, 14 Apr 2008 15:12:56 +0100
28 Status: works
29
30 Configuration Options:
31   [0] - PCI bus of device (optional)
32   [1] - PCI slot of device (optional)
33
34   If bus/slot is not specified, the first supported
35   PCI device found will be used.
36
37 Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
38
39 Subdevice 1 is 16-channel DIO.  The channels are configurable as input or
40 output in 2 groups (0 to 7, 8 to 15).  Configuring any channel implicitly
41 configures all channels in the same group.
42
43 TODO:
44
45 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA, 4 to 20 mA).
46 2. Read the initial ranges and values of the AO subdevice at start-up instead
47    of reinitializing them.
48 3. Implement calibration.
49 */
50
51 #include "../comedidev.h"
52
53 #include "comedi_pci.h"
54
55 #define ADVANTECH_VENDOR        0x13fe  /* Advantech PCI vendor ID */
56
57 /* hardware types of the cards */
58 #define TYPE_PCI1723 0
59
60 #define IORANGE_1723  0x2A
61
62 /* all the registers for the pci1723 board */
63 #define PCI1723_DA(N)   ((N)<<1)        /* W:   D/A register N (0 to 7) */
64
65 #define PCI1723_SYN_SET  0x12   /*synchronized set register       */
66 #define PCI1723_ALL_CHNNELE_SYN_STROBE  0x12    /*synchronized status register    */
67
68 #define PCI1723_RANGE_CALIBRATION_MODE 0x14     /* range and calibration mode   */
69 #define PCI1723_RANGE_CALIBRATION_STATUS 0x14   /* range and calibration status */
70
71 #define PCI1723_CONTROL_CMD_CALIBRATION_FUN 0x16        /* SADC control command for calibration function */
72 #define PCI1723_STATUS_CMD_CALIBRATION_FUN 0x16 /* SADC control status for calibration function */
73
74 #define PCI1723_CALIBRATION_PARA_STROBE 0x18    /* Calibration parameter strobe */
75
76 #define PCI1723_DIGITAL_IO_PORT_SET 0x1A        /* Digital I/O port setting */
77 #define PCI1723_DIGITAL_IO_PORT_MODE 0x1A       /* Digital I/O port mode */
78
79 #define PCI1723_WRITE_DIGITAL_OUTPUT_CMD 0x1C   /* Write digital output command */
80 #define PCI1723_READ_DIGITAL_INPUT_DATA 0x1C    /* Read digital input data */
81
82 #define PCI1723_WRITE_CAL_CMD 0x1E      /* Write calibration command */
83 #define PCI1723_READ_CAL_STATUS 0x1E    /* Read calibration status */
84
85 #define PCI1723_SYN_STROBE 0x20 /* Synchronized strobe */
86
87 #define PCI1723_RESET_ALL_CHN_STROBE 0x22       /* Reset all D/A channels strobe */
88
89 #define PCI1723_RESET_CAL_CONTROL_STROBE 0x24   /* Reset the calibration controller strobe */
90
91 #define PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE 0x26      /* Change D/A channels output type strobe */
92
93 #define PCI1723_SELECT_CALIBRATION 0x28 /* Select the calibration Ref_V */
94
95 /* static unsigned short pci_list_builded=0;      =1 list of card is know */
96
97 static const struct comedi_lrange range_pci1723 = { 1, {
98                                                         BIP_RANGE(10)
99                                                         }
100 };
101
102 /*
103  * Board descriptions for pci1723 boards.
104  */
105 struct pci1723_board {
106         const char *name;
107         int vendor_id;          /*  PCI vendor a device ID of card */
108         int device_id;
109         int iorange;
110         char cardtype;
111         int n_aochan;           /*  num of D/A chans */
112         int n_diochan;          /*  num of DIO chans */
113         int ao_maxdata;         /*  resolution of D/A */
114         const struct comedi_lrange *rangelist_ao;       /*  rangelist for D/A */
115 };
116
117 static const struct pci1723_board boardtypes[] = {
118         {
119          .name = "pci1723",
120          .vendor_id = ADVANTECH_VENDOR,
121          .device_id = 0x1723,
122          .iorange = IORANGE_1723,
123          .cardtype = TYPE_PCI1723,
124          .n_aochan = 8,
125          .n_diochan = 16,
126          .ao_maxdata = 0xffff,
127          .rangelist_ao = &range_pci1723,
128          },
129 };
130
131 /* This is used by modprobe to translate PCI IDs to drivers.  Should
132  * only be used for PCI and ISA-PnP devices */
133 static DEFINE_PCI_DEVICE_TABLE(pci1723_pci_table) = {
134         {
135         PCI_VENDOR_ID_ADVANTECH, 0x1723, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
136         0}
137 };
138
139 MODULE_DEVICE_TABLE(pci, pci1723_pci_table);
140
141 /*
142  * The struct comedi_driver structure tells the Comedi core module
143  * which functions to call to configure/deconfigure (attach/detach)
144  * the board, and also about the kernel module that contains
145  * the device code.
146  */
147 static int pci1723_attach(struct comedi_device *dev,
148                           struct comedi_devconfig *it);
149 static int pci1723_detach(struct comedi_device *dev);
150
151 #define n_boardtypes (sizeof(boardtypes)/sizeof(struct pci1723_board))
152
153 static struct comedi_driver driver_pci1723 = {
154         .driver_name = "adv_pci1723",
155         .module = THIS_MODULE,
156         .attach = pci1723_attach,
157         .detach = pci1723_detach,
158 };
159
160 /* this structure is for data unique to this hardware driver. */
161 struct pci1723_private {
162         int valid;              /* card is usable; */
163
164         struct pci_dev *pcidev;
165         unsigned char da_range[8];      /*  D/A output range for each channel */
166
167         short ao_data[8];       /*  data output buffer */
168 };
169
170 /*the following macro to make it easy to
171 * access the private structure.
172 */
173 #define devpriv ((struct pci1723_private *)dev->private)
174
175 #define this_board boardtypes
176
177 /*
178  *   the pci1723 card reset;
179  */
180 static int pci1723_reset(struct comedi_device *dev)
181 {
182         int i;
183         DPRINTK("adv_pci1723 EDBG: BGN: pci1723_reset(...)\n");
184
185         outw(0x01, dev->iobase + PCI1723_SYN_SET);      /*  set synchronous output mode */
186
187         for (i = 0; i < 8; i++) {
188                 /*  set all outputs to 0V */
189                 devpriv->ao_data[i] = 0x8000;
190                 outw(devpriv->ao_data[i], dev->iobase + PCI1723_DA(i));
191                 /*  set all ranges to +/- 10V */
192                 devpriv->da_range[i] = 0;
193                 outw(((devpriv->da_range[i] << 4) | i),
194                      PCI1723_RANGE_CALIBRATION_MODE);
195         }
196
197         outw(0, dev->iobase + PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE);   /*  update ranges */
198         outw(0, dev->iobase + PCI1723_SYN_STROBE);      /*  update outputs */
199
200         /*  set asynchronous output mode */
201         outw(0, dev->iobase + PCI1723_SYN_SET);
202
203         DPRINTK("adv_pci1723 EDBG: END: pci1723_reset(...)\n");
204         return 0;
205 }
206
207 static int pci1723_insn_read_ao(struct comedi_device *dev,
208                                 struct comedi_subdevice *s,
209                                 struct comedi_insn *insn, unsigned int *data)
210 {
211         int n, chan;
212
213         chan = CR_CHAN(insn->chanspec);
214         DPRINTK(" adv_PCI1723 DEBUG: pci1723_insn_read_ao() ----- \n");
215         for (n = 0; n < insn->n; n++)
216                 data[n] = devpriv->ao_data[chan];
217
218         return n;
219 }
220
221 /*
222   analog data output;
223 */
224 static int pci1723_ao_write_winsn(struct comedi_device *dev,
225                                   struct comedi_subdevice *s,
226                                   struct comedi_insn *insn, unsigned int *data)
227 {
228         int n, chan;
229         chan = CR_CHAN(insn->chanspec);
230
231         DPRINTK("PCI1723: the pci1723_ao_write_winsn() ------\n");
232
233         for (n = 0; n < insn->n; n++) {
234
235                 devpriv->ao_data[chan] = data[n];
236                 outw(data[n], dev->iobase + PCI1723_DA(chan));
237         }
238
239         return n;
240 }
241
242 /*
243   digital i/o config/query
244 */
245 static int pci1723_dio_insn_config(struct comedi_device *dev,
246                                    struct comedi_subdevice *s,
247                                    struct comedi_insn *insn, unsigned int *data)
248 {
249         unsigned int mask;
250         unsigned int bits;
251         unsigned short dio_mode;
252
253         mask = 1 << CR_CHAN(insn->chanspec);
254         if (mask & 0x00FF) {
255                 bits = 0x00FF;
256         } else {
257                 bits = 0xFF00;
258         }
259         switch (data[0]) {
260         case INSN_CONFIG_DIO_INPUT:
261                 s->io_bits &= ~bits;
262                 break;
263         case INSN_CONFIG_DIO_OUTPUT:
264                 s->io_bits |= bits;
265                 break;
266         case INSN_CONFIG_DIO_QUERY:
267                 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
268                 return insn->n;
269         default:
270                 return -EINVAL;
271         }
272
273         /*  update hardware DIO mode */
274         dio_mode = 0x0000;      /*  low byte output, high byte output */
275         if ((s->io_bits & 0x00FF) == 0)
276                 dio_mode |= 0x0001;     /*  low byte input */
277         if ((s->io_bits & 0xFF00) == 0)
278                 dio_mode |= 0x0002;     /*  high byte input */
279         outw(dio_mode, dev->iobase + PCI1723_DIGITAL_IO_PORT_SET);
280         return 1;
281 }
282
283 /*
284   digital i/o bits read/write
285 */
286 static int pci1723_dio_insn_bits(struct comedi_device *dev,
287                                  struct comedi_subdevice *s,
288                                  struct comedi_insn *insn, unsigned int *data)
289 {
290         if (data[0]) {
291                 s->state &= ~data[0];
292                 s->state |= (data[0] & data[1]);
293                 outw(s->state, dev->iobase + PCI1723_WRITE_DIGITAL_OUTPUT_CMD);
294         }
295         data[1] = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
296         return 2;
297 }
298
299 /*
300  * Attach is called by the Comedi core to configure the driver
301  * for a pci1723 board.
302  */
303 static int pci1723_attach(struct comedi_device *dev,
304                           struct comedi_devconfig *it)
305 {
306         struct comedi_subdevice *s;
307         int ret, subdev, n_subdevices;
308         struct pci_dev *pcidev;
309         unsigned int iobase;
310         unsigned char pci_bus, pci_slot, pci_func;
311         int opt_bus, opt_slot;
312         const char *errstr;
313
314         printk("comedi%d: adv_pci1723: board=%s", dev->minor, this_board->name);
315
316         opt_bus = it->options[0];
317         opt_slot = it->options[1];
318
319         ret = alloc_private(dev, sizeof(struct pci1723_private));
320         if (ret < 0) {
321                 printk(" - Allocation failed!\n");
322                 return -ENOMEM;
323         }
324
325         /* Look for matching PCI device */
326         errstr = "not found!";
327         pcidev = NULL;
328         while (NULL != (pcidev =
329                         pci_get_device(PCI_VENDOR_ID_ADVANTECH,
330                                        this_board->device_id, pcidev))) {
331                 /* Found matching vendor/device. */
332                 if (opt_bus || opt_slot) {
333                         /* Check bus/slot. */
334                         if (opt_bus != pcidev->bus->number
335                             || opt_slot != PCI_SLOT(pcidev->devfn))
336                                 continue;       /* no match */
337                 }
338                 /*
339                  * Look for device that isn't in use.
340                  * Enable PCI device and request regions.
341                  */
342                 if (comedi_pci_enable(pcidev, "adv_pci1723")) {
343                         errstr =
344                             "failed to enable PCI device and request regions!";
345                         continue;
346                 }
347                 break;
348         }
349
350         if (!pcidev) {
351                 if (opt_bus || opt_slot) {
352                         printk(" - Card at b:s %d:%d %s\n",
353                                opt_bus, opt_slot, errstr);
354                 } else {
355                         printk(" - Card %s\n", errstr);
356                 }
357                 return -EIO;
358         }
359
360         pci_bus = pcidev->bus->number;
361         pci_slot = PCI_SLOT(pcidev->devfn);
362         pci_func = PCI_FUNC(pcidev->devfn);
363         iobase = pci_resource_start(pcidev, 2);
364
365         printk(", b:s:f=%d:%d:%d, io=0x%4x", pci_bus, pci_slot, pci_func,
366                iobase);
367
368         dev->iobase = iobase;
369
370         dev->board_name = this_board->name;
371         devpriv->pcidev = pcidev;
372
373         n_subdevices = 0;
374
375         if (this_board->n_aochan)
376                 n_subdevices++;
377         if (this_board->n_diochan)
378                 n_subdevices++;
379
380         ret = alloc_subdevices(dev, n_subdevices);
381         if (ret < 0) {
382                 printk(" - Allocation failed!\n");
383                 return ret;
384         }
385
386         pci1723_reset(dev);
387         subdev = 0;
388         if (this_board->n_aochan) {
389                 s = dev->subdevices + subdev;
390                 dev->write_subdev = s;
391                 s->type = COMEDI_SUBD_AO;
392                 s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
393                 s->n_chan = this_board->n_aochan;
394                 s->maxdata = this_board->ao_maxdata;
395                 s->len_chanlist = this_board->n_aochan;
396                 s->range_table = this_board->rangelist_ao;
397
398                 s->insn_write = pci1723_ao_write_winsn;
399                 s->insn_read = pci1723_insn_read_ao;
400
401                 /*  read DIO config */
402                 switch (inw(dev->iobase + PCI1723_DIGITAL_IO_PORT_MODE) & 0x03) {
403                 case 0x00:      /*  low byte output, high byte output */
404                         s->io_bits = 0xFFFF;
405                         break;
406                 case 0x01:      /*  low byte input, high byte output */
407                         s->io_bits = 0xFF00;
408                         break;
409                 case 0x02:      /*  low byte output, high byte input */
410                         s->io_bits = 0x00FF;
411                         break;
412                 case 0x03:      /*  low byte input, high byte input */
413                         s->io_bits = 0x0000;
414                         break;
415                 }
416                 /*  read DIO port state */
417                 s->state = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
418
419                 subdev++;
420         }
421
422         if (this_board->n_diochan) {
423                 s = dev->subdevices + subdev;
424                 s->type = COMEDI_SUBD_DIO;
425                 s->subdev_flags =
426                     SDF_READABLE | SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
427                 s->n_chan = this_board->n_diochan;
428                 s->maxdata = 1;
429                 s->len_chanlist = this_board->n_diochan;
430                 s->range_table = &range_digital;
431                 s->insn_config = pci1723_dio_insn_config;
432                 s->insn_bits = pci1723_dio_insn_bits;
433                 subdev++;
434         }
435
436         devpriv->valid = 1;
437
438         pci1723_reset(dev);
439
440         return 0;
441 }
442
443 /*
444  * _detach is called to deconfigure a device.  It should deallocate
445  * resources.
446  * This function is also called when _attach() fails, so it should be
447  * careful not to release resources that were not necessarily
448  * allocated by _attach().  dev->private and dev->subdevices are
449  * deallocated automatically by the core.
450  */
451 static int pci1723_detach(struct comedi_device *dev)
452 {
453         printk("comedi%d: pci1723: remove\n", dev->minor);
454
455         if (dev->private) {
456                 if (devpriv->valid)
457                         pci1723_reset(dev);
458
459                 if (devpriv->pcidev) {
460                         if (dev->iobase) {
461                                 comedi_pci_disable(devpriv->pcidev);
462                         }
463                         pci_dev_put(devpriv->pcidev);
464                 }
465         }
466
467         return 0;
468 }
469
470 /*
471  * A convenient macro that defines init_module() and cleanup_module(),
472  * as necessary.
473  */
474 COMEDI_PCI_INITCLEANUP(driver_pci1723, pci1723_pci_table);