3 * Hardware driver for Mechatronic Systems Inc. C6x_DIGIO DSP daughter card.
4 * http://web.archive.org/web/%2A/http://robot0.ge.uiuc.edu/~spong/mecha/
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1999 Dan Block
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 * Description: Mechatronic Systems Inc. C6x_DIGIO DSP daughter card
25 * Devices: [Mechatronic Systems Inc.] C6x_DIGIO DSP daughter card (c6xdigio)
26 * Updated: Sun Nov 20 20:18:34 EST 2005
28 * Configuration Options:
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/sched.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/timex.h>
39 #include <linux/timer.h>
41 #include <linux/pnp.h>
43 #include "../comedidev.h"
48 #define C6XDIGIO_DATA_REG 0x00
49 #define C6XDIGIO_DATA_CHAN(x) (((x) + 1) << 4)
50 #define C6XDIGIO_DATA_PWM (1 << 5)
51 #define C6XDIGIO_DATA_ENCODER (1 << 6)
52 #define C6XDIGIO_STATUS_REG 0x01
53 #define C6XDIGIO_CTRL_REG 0x02
55 #define C6XDIGIO_TIME_OUT 20
57 static int c6xdigio_chk_status(struct comedi_device *dev, unsigned long context)
63 status = inb(dev->iobase + C6XDIGIO_STATUS_REG);
64 if ((status & 0x80) != context)
67 } while (timeout < C6XDIGIO_TIME_OUT);
72 static int c6xdigio_write_data(struct comedi_device *dev,
73 unsigned int val, unsigned int status)
75 outb_p(val, dev->iobase + C6XDIGIO_DATA_REG);
76 return c6xdigio_chk_status(dev, status);
79 static int c6xdigio_get_encoder_bits(struct comedi_device *dev,
86 val = inb(dev->iobase + C6XDIGIO_STATUS_REG);
92 return c6xdigio_write_data(dev, cmd, status);
95 static void c6xdigio_pwm_write(struct comedi_device *dev,
96 unsigned int chan, unsigned int val)
98 unsigned int cmd = C6XDIGIO_DATA_PWM | C6XDIGIO_DATA_CHAN(chan);
106 bits = (val >> 0) & 0x03;
107 c6xdigio_write_data(dev, cmd | bits | (0 << 2), 0x00);
108 bits = (val >> 2) & 0x03;
109 c6xdigio_write_data(dev, cmd | bits | (1 << 2), 0x80);
110 bits = (val >> 4) & 0x03;
111 c6xdigio_write_data(dev, cmd | bits | (0 << 2), 0x00);
112 bits = (val >> 6) & 0x03;
113 c6xdigio_write_data(dev, cmd | bits | (1 << 2), 0x80);
114 bits = (val >> 8) & 0x03;
115 c6xdigio_write_data(dev, cmd | bits | (0 << 2), 0x00);
117 c6xdigio_write_data(dev, 0x00, 0x80);
120 static int c6xdigio_encoder_read(struct comedi_device *dev,
123 unsigned int cmd = C6XDIGIO_DATA_ENCODER | C6XDIGIO_DATA_CHAN(chan);
124 unsigned int val = 0;
127 c6xdigio_write_data(dev, cmd, 0x00);
129 c6xdigio_get_encoder_bits(dev, &bits, cmd | (1 << 2), 0x80);
132 c6xdigio_get_encoder_bits(dev, &bits, cmd | (0 << 2), 0x00);
135 c6xdigio_get_encoder_bits(dev, &bits, cmd | (1 << 2), 0x80);
138 c6xdigio_get_encoder_bits(dev, &bits, cmd | (0 << 2), 0x00);
141 c6xdigio_get_encoder_bits(dev, &bits, cmd | (1 << 2), 0x80);
144 c6xdigio_get_encoder_bits(dev, &bits, cmd | (0 << 2), 0x00);
147 c6xdigio_get_encoder_bits(dev, &bits, cmd | (1 << 2), 0x80);
150 c6xdigio_get_encoder_bits(dev, &bits, cmd | (0 << 2), 0x00);
153 c6xdigio_write_data(dev, 0x00, 0x80);
158 static int c6xdigio_pwm_insn_write(struct comedi_device *dev,
159 struct comedi_subdevice *s,
160 struct comedi_insn *insn,
163 unsigned int chan = CR_CHAN(insn->chanspec);
164 unsigned int val = (s->state >> (16 * chan)) & 0xffff;
167 for (i = 0; i < insn->n; i++) {
169 c6xdigio_pwm_write(dev, chan, val);
173 * There are only 2 PWM channels and they have a maxdata of 500.
174 * Instead of allocating private data to save the values in for
175 * readback this driver just packs the values for the two channels
178 s->state &= (0xffff << (16 * chan));
179 s->state |= (val << (16 * chan));
184 static int c6xdigio_pwm_insn_read(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_insn *insn,
189 unsigned int chan = CR_CHAN(insn->chanspec);
193 val = (s->state >> (16 * chan)) & 0xffff;
195 for (i = 0; i < insn->n; i++)
201 static int c6xdigio_encoder_insn_read(struct comedi_device *dev,
202 struct comedi_subdevice *s,
203 struct comedi_insn *insn,
206 unsigned int chan = CR_CHAN(insn->chanspec);
210 for (i = 0; i < insn->n; i++) {
211 val = c6xdigio_encoder_read(dev, chan);
213 /* munge two's complement value to offset binary */
214 data[i] = comedi_offset_munge(s, val);
220 static void c6xdigio_init(struct comedi_device *dev)
222 /* Initialize the PWM */
223 c6xdigio_write_data(dev, 0x70, 0x00);
224 c6xdigio_write_data(dev, 0x74, 0x80);
225 c6xdigio_write_data(dev, 0x70, 0x00);
226 c6xdigio_write_data(dev, 0x00, 0x80);
228 /* Reset the encoders */
229 c6xdigio_write_data(dev, 0x68, 0x00);
230 c6xdigio_write_data(dev, 0x6c, 0x80);
231 c6xdigio_write_data(dev, 0x68, 0x00);
232 c6xdigio_write_data(dev, 0x00, 0x80);
235 static const struct pnp_device_id c6xdigio_pnp_tbl[] = {
236 /* Standard LPT Printer Port */
237 {.id = "PNP0400", .driver_data = 0},
238 /* ECP Printer Port */
239 {.id = "PNP0401", .driver_data = 0},
243 static struct pnp_driver c6xdigio_pnp_driver = {
245 .id_table = c6xdigio_pnp_tbl,
248 static int c6xdigio_attach(struct comedi_device *dev,
249 struct comedi_devconfig *it)
251 struct comedi_subdevice *s;
254 ret = comedi_request_region(dev, it->options[0], 0x03);
258 ret = comedi_alloc_subdevices(dev, 2);
262 /* Make sure that PnP ports get activated */
263 pnp_register_driver(&c6xdigio_pnp_driver);
265 s = &dev->subdevices[0];
266 /* pwm output subdevice */
267 s->type = COMEDI_SUBD_PWM;
268 s->subdev_flags = SDF_WRITABLE;
271 s->range_table = &range_unknown;
272 s->insn_write = c6xdigio_pwm_insn_write;
273 s->insn_read = c6xdigio_pwm_insn_read;
275 s = &dev->subdevices[1];
276 /* encoder (counter) subdevice */
277 s->type = COMEDI_SUBD_COUNTER;
278 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
280 s->maxdata = 0xffffff;
281 s->range_table = &range_unknown;
282 s->insn_read = c6xdigio_encoder_insn_read;
284 /* I will call this init anyway but more than likely the DSP board */
285 /* will not be connected when device driver is loaded. */
291 static void c6xdigio_detach(struct comedi_device *dev)
293 comedi_legacy_detach(dev);
294 pnp_unregister_driver(&c6xdigio_pnp_driver);
297 static struct comedi_driver c6xdigio_driver = {
298 .driver_name = "c6xdigio",
299 .module = THIS_MODULE,
300 .attach = c6xdigio_attach,
301 .detach = c6xdigio_detach,
303 module_comedi_driver(c6xdigio_driver);
305 MODULE_AUTHOR("Comedi http://www.comedi.org");
306 MODULE_DESCRIPTION("Comedi driver for the C6x_DIGIO DSP daughter card");
307 MODULE_LICENSE("GPL");