arm64: Implement coherent DMA API based on swiotlb
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / cb_das16_cs.c
1 /*
2     comedi/drivers/das16cs.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     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22     PCMCIA support code for this driver is adapted from the dummy_cs.c
23     driver of the Linux PCMCIA Card Services package.
24
25     The initial developer of the original code is David A. Hinds
26     <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
27     are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
28
29 */
30 /*
31 Driver: cb_das16_cs
32 Description: Computer Boards PC-CARD DAS16/16
33 Devices: [ComputerBoards] PC-CARD DAS16/16 (cb_das16_cs), PC-CARD DAS16/16-AO
34 Author: ds
35 Updated: Mon, 04 Nov 2002 20:04:21 -0800
36 Status: experimental
37
38
39 */
40
41 #include <linux/interrupt.h>
42 #include <linux/slab.h>
43 #include <linux/delay.h>
44
45 #include "../comedidev.h"
46
47 #include <pcmcia/cistpl.h>
48 #include <pcmcia/ds.h>
49
50 #include "comedi_fc.h"
51 #include "8253.h"
52
53 #define DAS16CS_SIZE                    18
54
55 #define DAS16CS_ADC_DATA                0
56 #define DAS16CS_DIO_MUX                 2
57 #define DAS16CS_MISC1                   4
58 #define DAS16CS_MISC2                   6
59 #define DAS16CS_CTR0                    8
60 #define DAS16CS_CTR1                    10
61 #define DAS16CS_CTR2                    12
62 #define DAS16CS_CTR_CONTROL             14
63 #define DAS16CS_DIO                     16
64
65 struct das16cs_board {
66         const char *name;
67         int device_id;
68         int n_ao_chans;
69 };
70
71 static const struct das16cs_board das16cs_boards[] = {
72         {
73                 .name           = "PC-CARD DAS16/16-AO",
74                 .device_id      = 0x0039,
75                 .n_ao_chans     = 2,
76         }, {
77                 .name           = "PCM-DAS16s/16",
78                 .device_id      = 0x4009,
79                 .n_ao_chans     = 0,
80         }, {
81                 .name           = "PC-CARD DAS16/16",
82                 .device_id      = 0x0000,       /* unknown */
83                 .n_ao_chans     = 0,
84         },
85 };
86
87 struct das16cs_private {
88         unsigned int ao_readback[2];
89         unsigned short status1;
90         unsigned short status2;
91 };
92
93 static const struct comedi_lrange das16cs_ai_range = {
94         4, {
95                 BIP_RANGE(10),
96                 BIP_RANGE(5),
97                 BIP_RANGE(2.5),
98                 BIP_RANGE(1.25),
99         }
100 };
101
102 static irqreturn_t das16cs_interrupt(int irq, void *d)
103 {
104         /* struct comedi_device *dev = d; */
105         return IRQ_HANDLED;
106 }
107
108 static int das16cs_ai_rinsn(struct comedi_device *dev,
109                             struct comedi_subdevice *s,
110                             struct comedi_insn *insn, unsigned int *data)
111 {
112         struct das16cs_private *devpriv = dev->private;
113         int chan = CR_CHAN(insn->chanspec);
114         int range = CR_RANGE(insn->chanspec);
115         int aref = CR_AREF(insn->chanspec);
116         int i;
117         int to;
118
119         outw(chan, dev->iobase + DAS16CS_DIO_MUX);
120
121         devpriv->status1 &= ~0xf320;
122         devpriv->status1 |= (aref == AREF_DIFF) ? 0 : 0x0020;
123         outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
124
125         devpriv->status2 &= ~0xff00;
126         switch (range) {
127         case 0:
128                 devpriv->status2 |= 0x800;
129                 break;
130         case 1:
131                 devpriv->status2 |= 0x000;
132                 break;
133         case 2:
134                 devpriv->status2 |= 0x100;
135                 break;
136         case 3:
137                 devpriv->status2 |= 0x200;
138                 break;
139         }
140         outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
141
142         for (i = 0; i < insn->n; i++) {
143                 outw(0, dev->iobase + DAS16CS_ADC_DATA);
144
145 #define TIMEOUT 1000
146                 for (to = 0; to < TIMEOUT; to++) {
147                         if (inw(dev->iobase + DAS16CS_MISC1) & 0x0080)
148                                 break;
149                 }
150                 if (to == TIMEOUT) {
151                         dev_dbg(dev->class_dev, "cb_das16_cs: ai timeout\n");
152                         return -ETIME;
153                 }
154                 data[i] = inw(dev->iobase + DAS16CS_ADC_DATA);
155         }
156
157         return i;
158 }
159
160 static int das16cs_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
161 {
162         return -EINVAL;
163 }
164
165 static int das16cs_ai_cmdtest(struct comedi_device *dev,
166                               struct comedi_subdevice *s,
167                               struct comedi_cmd *cmd)
168 {
169         int err = 0;
170         int tmp;
171
172         /* Step 1 : check if triggers are trivially valid */
173
174         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
175         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
176                                         TRIG_TIMER | TRIG_EXT);
177         err |= cfc_check_trigger_src(&cmd->convert_src,
178                                         TRIG_TIMER | TRIG_EXT);
179         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
180         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
181
182         if (err)
183                 return 1;
184
185         /* Step 2a : make sure trigger sources are unique */
186
187         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
188         err |= cfc_check_trigger_is_unique(cmd->convert_src);
189         err |= cfc_check_trigger_is_unique(cmd->stop_src);
190
191         /* Step 2b : and mutually compatible */
192
193         if (err)
194                 return 2;
195
196         /* Step 3: check if arguments are trivially valid */
197
198         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
199
200 #define MAX_SPEED       10000   /* in nanoseconds */
201 #define MIN_SPEED       1000000000      /* in nanoseconds */
202
203         if (cmd->scan_begin_src == TRIG_TIMER) {
204                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
205                                                  MAX_SPEED);
206                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
207                                                  MIN_SPEED);
208         } else {
209                 /* external trigger */
210                 /* should be level/edge, hi/lo specification here */
211                 /* should specify multiple external triggers */
212                 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
213         }
214         if (cmd->convert_src == TRIG_TIMER) {
215                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
216                                                  MAX_SPEED);
217                 err |= cfc_check_trigger_arg_max(&cmd->convert_arg,
218                                                  MIN_SPEED);
219         } else {
220                 /* external trigger */
221                 /* see above */
222                 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, 9);
223         }
224
225         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
226
227         if (cmd->stop_src == TRIG_COUNT)
228                 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
229         else    /* TRIG_NONE */
230                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
231
232         if (err)
233                 return 3;
234
235         /* step 4: fix up any arguments */
236
237         if (cmd->scan_begin_src == TRIG_TIMER) {
238                 unsigned int div1 = 0, div2 = 0;
239
240                 tmp = cmd->scan_begin_arg;
241                 i8253_cascade_ns_to_timer(100, &div1, &div2,
242                                           &cmd->scan_begin_arg,
243                                           cmd->flags & TRIG_ROUND_MASK);
244                 if (tmp != cmd->scan_begin_arg)
245                         err++;
246         }
247         if (cmd->convert_src == TRIG_TIMER) {
248                 unsigned int div1 = 0, div2 = 0;
249
250                 tmp = cmd->convert_arg;
251                 i8253_cascade_ns_to_timer(100, &div1, &div2,
252                                           &cmd->scan_begin_arg,
253                                           cmd->flags & TRIG_ROUND_MASK);
254                 if (tmp != cmd->convert_arg)
255                         err++;
256                 if (cmd->scan_begin_src == TRIG_TIMER &&
257                     cmd->scan_begin_arg <
258                     cmd->convert_arg * cmd->scan_end_arg) {
259                         cmd->scan_begin_arg =
260                             cmd->convert_arg * cmd->scan_end_arg;
261                         err++;
262                 }
263         }
264
265         if (err)
266                 return 4;
267
268         return 0;
269 }
270
271 static int das16cs_ao_winsn(struct comedi_device *dev,
272                             struct comedi_subdevice *s,
273                             struct comedi_insn *insn, unsigned int *data)
274 {
275         struct das16cs_private *devpriv = dev->private;
276         int i;
277         int chan = CR_CHAN(insn->chanspec);
278         unsigned short status1;
279         unsigned short d;
280         int bit;
281
282         for (i = 0; i < insn->n; i++) {
283                 devpriv->ao_readback[chan] = data[i];
284                 d = data[i];
285
286                 outw(devpriv->status1, dev->iobase + DAS16CS_MISC1);
287                 udelay(1);
288
289                 status1 = devpriv->status1 & ~0xf;
290                 if (chan)
291                         status1 |= 0x0001;
292                 else
293                         status1 |= 0x0008;
294
295                 outw(status1, dev->iobase + DAS16CS_MISC1);
296                 udelay(1);
297
298                 for (bit = 15; bit >= 0; bit--) {
299                         int b = (d >> bit) & 0x1;
300                         b <<= 1;
301                         outw(status1 | b | 0x0000, dev->iobase + DAS16CS_MISC1);
302                         udelay(1);
303                         outw(status1 | b | 0x0004, dev->iobase + DAS16CS_MISC1);
304                         udelay(1);
305                 }
306                 /*
307                  * Make both DAC0CS and DAC1CS high to load
308                  * the new data and update analog the output
309                  */
310                 outw(status1 | 0x9, dev->iobase + DAS16CS_MISC1);
311         }
312
313         return i;
314 }
315
316 static int das16cs_ao_rinsn(struct comedi_device *dev,
317                             struct comedi_subdevice *s,
318                             struct comedi_insn *insn, unsigned int *data)
319 {
320         struct das16cs_private *devpriv = dev->private;
321         int i;
322         int chan = CR_CHAN(insn->chanspec);
323
324         for (i = 0; i < insn->n; i++)
325                 data[i] = devpriv->ao_readback[chan];
326
327         return i;
328 }
329
330 static int das16cs_dio_insn_bits(struct comedi_device *dev,
331                                  struct comedi_subdevice *s,
332                                  struct comedi_insn *insn, unsigned int *data)
333 {
334         if (data[0]) {
335                 s->state &= ~data[0];
336                 s->state |= data[0] & data[1];
337
338                 outw(s->state, dev->iobase + DAS16CS_DIO);
339         }
340
341         data[1] = inw(dev->iobase + DAS16CS_DIO);
342
343         return insn->n;
344 }
345
346 static int das16cs_dio_insn_config(struct comedi_device *dev,
347                                    struct comedi_subdevice *s,
348                                    struct comedi_insn *insn, unsigned int *data)
349 {
350         struct das16cs_private *devpriv = dev->private;
351         int chan = CR_CHAN(insn->chanspec);
352         int bits;
353
354         if (chan < 4)
355                 bits = 0x0f;
356         else
357                 bits = 0xf0;
358
359         switch (data[0]) {
360         case INSN_CONFIG_DIO_OUTPUT:
361                 s->io_bits |= bits;
362                 break;
363         case INSN_CONFIG_DIO_INPUT:
364                 s->io_bits &= bits;
365                 break;
366         case INSN_CONFIG_DIO_QUERY:
367                 data[1] =
368                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
369                 return insn->n;
370                 break;
371         default:
372                 return -EINVAL;
373                 break;
374         }
375
376         devpriv->status2 &= ~0x00c0;
377         devpriv->status2 |= (s->io_bits & 0xf0) ? 0x0080 : 0;
378         devpriv->status2 |= (s->io_bits & 0x0f) ? 0x0040 : 0;
379
380         outw(devpriv->status2, dev->iobase + DAS16CS_MISC2);
381
382         return insn->n;
383 }
384
385 static const void *das16cs_find_boardinfo(struct comedi_device *dev,
386                                           struct pcmcia_device *link)
387 {
388         const struct das16cs_board *board;
389         int i;
390
391         for (i = 0; i < ARRAY_SIZE(das16cs_boards); i++) {
392                 board = &das16cs_boards[i];
393                 if (board->device_id == link->card_id)
394                         return board;
395         }
396
397         return NULL;
398 }
399
400 static int das16cs_auto_attach(struct comedi_device *dev,
401                                unsigned long context)
402 {
403         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
404         const struct das16cs_board *board;
405         struct das16cs_private *devpriv;
406         struct comedi_subdevice *s;
407         int ret;
408
409         board = das16cs_find_boardinfo(dev, link);
410         if (!board)
411                 return -ENODEV;
412         dev->board_ptr = board;
413         dev->board_name = board->name;
414
415         link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
416         ret = comedi_pcmcia_enable(dev, NULL);
417         if (ret)
418                 return ret;
419         dev->iobase = link->resource[0]->start;
420
421         link->priv = dev;
422         ret = pcmcia_request_irq(link, das16cs_interrupt);
423         if (ret)
424                 return ret;
425         dev->irq = link->irq;
426
427         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
428         if (!devpriv)
429                 return -ENOMEM;
430         dev->private = devpriv;
431
432         ret = comedi_alloc_subdevices(dev, 3);
433         if (ret)
434                 return ret;
435
436         s = &dev->subdevices[0];
437         dev->read_subdev = s;
438         /* analog input subdevice */
439         s->type         = COMEDI_SUBD_AI;
440         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
441         s->n_chan       = 16;
442         s->maxdata      = 0xffff;
443         s->range_table  = &das16cs_ai_range;
444         s->len_chanlist = 16;
445         s->insn_read    = das16cs_ai_rinsn;
446         s->do_cmd       = das16cs_ai_cmd;
447         s->do_cmdtest   = das16cs_ai_cmdtest;
448
449         s = &dev->subdevices[1];
450         /* analog output subdevice */
451         if (board->n_ao_chans) {
452                 s->type         = COMEDI_SUBD_AO;
453                 s->subdev_flags = SDF_WRITABLE;
454                 s->n_chan       = board->n_ao_chans;
455                 s->maxdata      = 0xffff;
456                 s->range_table  = &range_bipolar10;
457                 s->insn_write   = &das16cs_ao_winsn;
458                 s->insn_read    = &das16cs_ao_rinsn;
459         } else {
460                 s->type         = COMEDI_SUBD_UNUSED;
461         }
462
463         s = &dev->subdevices[2];
464         /* digital i/o subdevice */
465         s->type         = COMEDI_SUBD_DIO;
466         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
467         s->n_chan       = 8;
468         s->maxdata      = 1;
469         s->range_table  = &range_digital;
470         s->insn_bits    = das16cs_dio_insn_bits;
471         s->insn_config  = das16cs_dio_insn_config;
472
473         dev_info(dev->class_dev, "%s: %s, I/O base=0x%04lx, irq=%u\n",
474                 dev->driver->driver_name, dev->board_name,
475                 dev->iobase, dev->irq);
476
477         return 0;
478 }
479
480 static struct comedi_driver driver_das16cs = {
481         .driver_name    = "cb_das16_cs",
482         .module         = THIS_MODULE,
483         .auto_attach    = das16cs_auto_attach,
484         .detach         = comedi_pcmcia_disable,
485 };
486
487 static int das16cs_pcmcia_attach(struct pcmcia_device *link)
488 {
489         return comedi_pcmcia_auto_config(link, &driver_das16cs);
490 }
491
492 static const struct pcmcia_device_id das16cs_id_table[] = {
493         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x0039),
494         PCMCIA_DEVICE_MANF_CARD(0x01c5, 0x4009),
495         PCMCIA_DEVICE_NULL
496 };
497 MODULE_DEVICE_TABLE(pcmcia, das16cs_id_table);
498
499 static struct pcmcia_driver das16cs_driver = {
500         .name           = "cb_das16_cs",
501         .owner          = THIS_MODULE,
502         .id_table       = das16cs_id_table,
503         .probe          = das16cs_pcmcia_attach,
504         .remove         = comedi_pcmcia_auto_unconfig,
505 };
506 module_comedi_pcmcia_driver(driver_das16cs, das16cs_driver);
507
508 MODULE_AUTHOR("David A. Schleef <ds@schleef.org>");
509 MODULE_DESCRIPTION("Comedi driver for Computer Boards PC-CARD DAS16/16");
510 MODULE_LICENSE("GPL");