staging: comedi: drivers: let core handle freeing s->private
[firefly-linux-kernel-4.4.55.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 /*
18 Driver: adv_pci1723
19 Description: Advantech PCI-1723
20 Author: yonggang <rsmgnu@gmail.com>, Ian Abbott <abbotti@mev.co.uk>
21 Devices: [Advantech] PCI-1723 (adv_pci1723)
22 Updated: Mon, 14 Apr 2008 15:12:56 +0100
23 Status: works
24
25 Configuration Options:
26   [0] - PCI bus of device (optional)
27   [1] - PCI slot of device (optional)
28
29   If bus/slot is not specified, the first supported
30   PCI device found will be used.
31
32 Subdevice 0 is 8-channel AO, 16-bit, range +/- 10 V.
33
34 Subdevice 1 is 16-channel DIO.  The channels are configurable as input or
35 output in 2 groups (0 to 7, 8 to 15).  Configuring any channel implicitly
36 configures all channels in the same group.
37
38 TODO:
39
40 1. Add the two milliamp ranges to the AO subdevice (0 to 20 mA, 4 to 20 mA).
41 2. Read the initial ranges and values of the AO subdevice at start-up instead
42    of reinitializing them.
43 3. Implement calibration.
44 */
45
46 #include <linux/pci.h>
47
48 #include "../comedidev.h"
49
50 /* all the registers for the pci1723 board */
51 #define PCI1723_DA(N)   ((N)<<1)        /* W: D/A register N (0 to 7) */
52
53 #define PCI1723_SYN_SET  0x12           /* synchronized set register */
54 #define PCI1723_ALL_CHNNELE_SYN_STROBE  0x12
55                                         /* synchronized status register */
56
57 #define PCI1723_RANGE_CALIBRATION_MODE 0x14
58                                         /* range and calibration mode */
59 #define PCI1723_RANGE_CALIBRATION_STATUS 0x14
60                                         /* range and calibration status */
61
62 #define PCI1723_CONTROL_CMD_CALIBRATION_FUN 0x16
63                                                 /*
64                                                  * SADC control command for
65                                                  * calibration function
66                                                  */
67 #define PCI1723_STATUS_CMD_CALIBRATION_FUN 0x16
68                                                 /*
69                                                  * SADC control status for
70                                                  * calibration function
71                                                  */
72
73 #define PCI1723_CALIBRATION_PARA_STROBE 0x18
74                                         /* 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
80                                         /* Write digital output command */
81 #define PCI1723_READ_DIGITAL_INPUT_DATA 0x1C    /* Read digital input data */
82
83 #define PCI1723_WRITE_CAL_CMD 0x1E              /* Write calibration command */
84 #define PCI1723_READ_CAL_STATUS 0x1E            /* Read calibration status */
85
86 #define PCI1723_SYN_STROBE 0x20                 /* Synchronized strobe */
87
88 #define PCI1723_RESET_ALL_CHN_STROBE 0x22
89                                         /* Reset all D/A channels strobe */
90
91 #define PCI1723_RESET_CAL_CONTROL_STROBE 0x24
92                                                 /*
93                                                  * Reset the calibration
94                                                  * controller strobe
95                                                  */
96
97 #define PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE 0x26
98                                                 /*
99                                                  * Change D/A channels output
100                                                  * type strobe
101                                                  */
102
103 #define PCI1723_SELECT_CALIBRATION 0x28 /* Select the calibration Ref_V */
104
105 struct pci1723_private {
106         unsigned char da_range[8];      /* D/A output range for each channel */
107         short ao_data[8];       /* data output buffer */
108 };
109
110 /*
111  * The pci1723 card reset;
112  */
113 static int pci1723_reset(struct comedi_device *dev)
114 {
115         struct pci1723_private *devpriv = dev->private;
116         int i;
117
118         outw(0x01, dev->iobase + PCI1723_SYN_SET);
119                                                /* set synchronous output mode */
120
121         for (i = 0; i < 8; i++) {
122                 /* set all outputs to 0V */
123                 devpriv->ao_data[i] = 0x8000;
124                 outw(devpriv->ao_data[i], dev->iobase + PCI1723_DA(i));
125                 /* set all ranges to +/- 10V */
126                 devpriv->da_range[i] = 0;
127                 outw(((devpriv->da_range[i] << 4) | i),
128                      PCI1723_RANGE_CALIBRATION_MODE);
129         }
130
131         outw(0, dev->iobase + PCI1723_CHANGE_CHA_OUTPUT_TYPE_STROBE);
132                                                             /* update ranges */
133         outw(0, dev->iobase + PCI1723_SYN_STROBE);          /* update outputs */
134
135         /* set asynchronous output mode */
136         outw(0, dev->iobase + PCI1723_SYN_SET);
137
138         return 0;
139 }
140
141 static int pci1723_insn_read_ao(struct comedi_device *dev,
142                                 struct comedi_subdevice *s,
143                                 struct comedi_insn *insn, unsigned int *data)
144 {
145         struct pci1723_private *devpriv = dev->private;
146         int n, chan;
147
148         chan = CR_CHAN(insn->chanspec);
149         for (n = 0; n < insn->n; n++)
150                 data[n] = devpriv->ao_data[chan];
151
152         return n;
153 }
154
155 /*
156   analog data output;
157 */
158 static int pci1723_ao_write_winsn(struct comedi_device *dev,
159                                   struct comedi_subdevice *s,
160                                   struct comedi_insn *insn, unsigned int *data)
161 {
162         struct pci1723_private *devpriv = dev->private;
163         int n, chan;
164         chan = CR_CHAN(insn->chanspec);
165
166         for (n = 0; n < insn->n; n++) {
167
168                 devpriv->ao_data[chan] = data[n];
169                 outw(data[n], dev->iobase + PCI1723_DA(chan));
170         }
171
172         return n;
173 }
174
175 /*
176   digital i/o config/query
177 */
178 static int pci1723_dio_insn_config(struct comedi_device *dev,
179                                    struct comedi_subdevice *s,
180                                    struct comedi_insn *insn, unsigned int *data)
181 {
182         unsigned int mask;
183         unsigned int bits;
184         unsigned short dio_mode;
185
186         mask = 1 << CR_CHAN(insn->chanspec);
187         if (mask & 0x00FF)
188                 bits = 0x00FF;
189         else
190                 bits = 0xFF00;
191
192         switch (data[0]) {
193         case INSN_CONFIG_DIO_INPUT:
194                 s->io_bits &= ~bits;
195                 break;
196         case INSN_CONFIG_DIO_OUTPUT:
197                 s->io_bits |= bits;
198                 break;
199         case INSN_CONFIG_DIO_QUERY:
200                 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
201                 return insn->n;
202         default:
203                 return -EINVAL;
204         }
205
206         /* update hardware DIO mode */
207         dio_mode = 0x0000;      /* low byte output, high byte output */
208         if ((s->io_bits & 0x00FF) == 0)
209                 dio_mode |= 0x0001;     /* low byte input */
210         if ((s->io_bits & 0xFF00) == 0)
211                 dio_mode |= 0x0002;     /* high byte input */
212         outw(dio_mode, dev->iobase + PCI1723_DIGITAL_IO_PORT_SET);
213         return 1;
214 }
215
216 /*
217   digital i/o bits read/write
218 */
219 static int pci1723_dio_insn_bits(struct comedi_device *dev,
220                                  struct comedi_subdevice *s,
221                                  struct comedi_insn *insn, unsigned int *data)
222 {
223         if (data[0]) {
224                 s->state &= ~data[0];
225                 s->state |= (data[0] & data[1]);
226                 outw(s->state, dev->iobase + PCI1723_WRITE_DIGITAL_OUTPUT_CMD);
227         }
228         data[1] = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
229         return insn->n;
230 }
231
232 static int pci1723_auto_attach(struct comedi_device *dev,
233                                          unsigned long context_unused)
234 {
235         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
236         struct pci1723_private *devpriv;
237         struct comedi_subdevice *s;
238         int ret;
239
240         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
241         if (!devpriv)
242                 return -ENOMEM;
243         dev->private = devpriv;
244
245         ret = comedi_pci_enable(dev);
246         if (ret)
247                 return ret;
248         dev->iobase = pci_resource_start(pcidev, 2);
249
250         ret = comedi_alloc_subdevices(dev, 2);
251         if (ret)
252                 return ret;
253
254         s = &dev->subdevices[0];
255         dev->write_subdev = s;
256         s->type         = COMEDI_SUBD_AO;
257         s->subdev_flags = SDF_WRITEABLE | SDF_GROUND | SDF_COMMON;
258         s->n_chan       = 8;
259         s->maxdata      = 0xffff;
260         s->len_chanlist = 8;
261         s->range_table  = &range_bipolar10;
262         s->insn_write   = pci1723_ao_write_winsn;
263         s->insn_read    = pci1723_insn_read_ao;
264
265         s = &dev->subdevices[1];
266         s->type         = COMEDI_SUBD_DIO;
267         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
268         s->n_chan       = 16;
269         s->maxdata      = 1;
270         s->len_chanlist = 16;
271         s->range_table  = &range_digital;
272         s->insn_config  = pci1723_dio_insn_config;
273         s->insn_bits    = pci1723_dio_insn_bits;
274
275         /* read DIO config */
276         switch (inw(dev->iobase + PCI1723_DIGITAL_IO_PORT_MODE) & 0x03) {
277         case 0x00:      /* low byte output, high byte output */
278                 s->io_bits = 0xFFFF;
279                 break;
280         case 0x01:      /* low byte input, high byte output */
281                 s->io_bits = 0xFF00;
282                 break;
283         case 0x02:      /* low byte output, high byte input */
284                 s->io_bits = 0x00FF;
285                 break;
286         case 0x03:      /* low byte input, high byte input */
287                 s->io_bits = 0x0000;
288                 break;
289         }
290         /* read DIO port state */
291         s->state = inw(dev->iobase + PCI1723_READ_DIGITAL_INPUT_DATA);
292
293         pci1723_reset(dev);
294
295         dev_info(dev->class_dev, "%s attached\n", dev->board_name);
296
297         return 0;
298 }
299
300 static void pci1723_detach(struct comedi_device *dev)
301 {
302         if (dev->iobase)
303                 pci1723_reset(dev);
304         comedi_pci_disable(dev);
305 }
306
307 static struct comedi_driver adv_pci1723_driver = {
308         .driver_name    = "adv_pci1723",
309         .module         = THIS_MODULE,
310         .auto_attach    = pci1723_auto_attach,
311         .detach         = pci1723_detach,
312 };
313
314 static int adv_pci1723_pci_probe(struct pci_dev *dev,
315                                  const struct pci_device_id *id)
316 {
317         return comedi_pci_auto_config(dev, &adv_pci1723_driver,
318                                       id->driver_data);
319 }
320
321 static DEFINE_PCI_DEVICE_TABLE(adv_pci1723_pci_table) = {
322         { PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1723) },
323         { 0 }
324 };
325 MODULE_DEVICE_TABLE(pci, adv_pci1723_pci_table);
326
327 static struct pci_driver adv_pci1723_pci_driver = {
328         .name           = "adv_pci1723",
329         .id_table       = adv_pci1723_pci_table,
330         .probe          = adv_pci1723_pci_probe,
331         .remove         = comedi_pci_auto_unconfig,
332 };
333 module_comedi_pci_driver(adv_pci1723_driver, adv_pci1723_pci_driver);
334
335 MODULE_AUTHOR("Comedi http://www.comedi.org");
336 MODULE_DESCRIPTION("Comedi low-level driver");
337 MODULE_LICENSE("GPL");