1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * fl512.c
4  * Anders Gnistrup <ex18@kalman.iau.dtu.dk>
5  *
6  * COMEDI - Linux Control and Measurement Device Interface
7  * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8  */
9 
10 /*
11  * Driver: fl512
12  * Description: unknown
13  * Author: Anders Gnistrup <ex18@kalman.iau.dtu.dk>
14  * Devices: [unknown] FL512 (fl512)
15  * Status: unknown
16  *
17  * Digital I/O is not supported.
18  *
19  * Configuration options:
20  *   [0] - I/O port base address
21  */
22 
23 #include <linux/module.h>
24 #include <linux/comedi/comedidev.h>
25 #include <linux/delay.h>
26 
27 /*
28  * Register I/O map
29  */
30 #define FL512_AI_LSB_REG		0x02
31 #define FL512_AI_MSB_REG		0x03
32 #define FL512_AI_MUX_REG		0x02
33 #define FL512_AI_START_CONV_REG		0x03
34 #define FL512_AO_DATA_REG(x)		(0x04 + ((x) * 2))
35 #define FL512_AO_TRIG_REG(x)		(0x04 + ((x) * 2))
36 
37 static const struct comedi_lrange range_fl512 = {
38 	4, {
39 		BIP_RANGE(0.5),
40 		BIP_RANGE(1),
41 		BIP_RANGE(5),
42 		BIP_RANGE(10),
43 		UNI_RANGE(1),
44 		UNI_RANGE(5),
45 		UNI_RANGE(10)
46 	}
47 };
48 
fl512_ai_insn_read(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)49 static int fl512_ai_insn_read(struct comedi_device *dev,
50 			      struct comedi_subdevice *s,
51 			      struct comedi_insn *insn,
52 			      unsigned int *data)
53 {
54 	unsigned int chan = CR_CHAN(insn->chanspec);
55 	unsigned int val;
56 	int i;
57 
58 	outb(chan, dev->iobase + FL512_AI_MUX_REG);
59 
60 	for (i = 0; i < insn->n; i++) {
61 		outb(0, dev->iobase + FL512_AI_START_CONV_REG);
62 
63 		/* XXX should test "done" flag instead of delay */
64 		usleep_range(30, 100);
65 
66 		val = inb(dev->iobase + FL512_AI_LSB_REG);
67 		val |= (inb(dev->iobase + FL512_AI_MSB_REG) << 8);
68 		val &= s->maxdata;
69 
70 		data[i] = val;
71 	}
72 
73 	return insn->n;
74 }
75 
fl512_ao_insn_write(struct comedi_device * dev,struct comedi_subdevice * s,struct comedi_insn * insn,unsigned int * data)76 static int fl512_ao_insn_write(struct comedi_device *dev,
77 			       struct comedi_subdevice *s,
78 			       struct comedi_insn *insn,
79 			       unsigned int *data)
80 {
81 	unsigned int chan = CR_CHAN(insn->chanspec);
82 	unsigned int val = s->readback[chan];
83 	int i;
84 
85 	for (i = 0; i < insn->n; i++) {
86 		val = data[i];
87 
88 		/* write LSB, MSB then trigger conversion */
89 		outb(val & 0x0ff, dev->iobase + FL512_AO_DATA_REG(chan));
90 		outb((val >> 8) & 0xf, dev->iobase + FL512_AO_DATA_REG(chan));
91 		inb(dev->iobase + FL512_AO_TRIG_REG(chan));
92 	}
93 	s->readback[chan] = val;
94 
95 	return insn->n;
96 }
97 
fl512_attach(struct comedi_device * dev,struct comedi_devconfig * it)98 static int fl512_attach(struct comedi_device *dev, struct comedi_devconfig *it)
99 {
100 	struct comedi_subdevice *s;
101 	int ret;
102 
103 	ret = comedi_request_region(dev, it->options[0], 0x10);
104 	if (ret)
105 		return ret;
106 
107 	ret = comedi_alloc_subdevices(dev, 2);
108 	if (ret)
109 		return ret;
110 
111 	/* Analog Input subdevice */
112 	s = &dev->subdevices[0];
113 	s->type		= COMEDI_SUBD_AI;
114 	s->subdev_flags	= SDF_READABLE | SDF_GROUND;
115 	s->n_chan	= 16;
116 	s->maxdata	= 0x0fff;
117 	s->range_table	= &range_fl512;
118 	s->insn_read	= fl512_ai_insn_read;
119 
120 	/* Analog Output subdevice */
121 	s = &dev->subdevices[1];
122 	s->type		= COMEDI_SUBD_AO;
123 	s->subdev_flags	= SDF_WRITABLE;
124 	s->n_chan	= 2;
125 	s->maxdata	= 0x0fff;
126 	s->range_table	= &range_fl512;
127 	s->insn_write	= fl512_ao_insn_write;
128 
129 	return comedi_alloc_subdev_readback(s);
130 }
131 
132 static struct comedi_driver fl512_driver = {
133 	.driver_name	= "fl512",
134 	.module		= THIS_MODULE,
135 	.attach		= fl512_attach,
136 	.detach		= comedi_legacy_detach,
137 };
138 module_comedi_driver(fl512_driver);
139 
140 MODULE_AUTHOR("Comedi https://www.comedi.org");
141 MODULE_DESCRIPTION("Comedi low-level driver");
142 MODULE_LICENSE("GPL");
143