2 comedi/drivers/c6xdigio.c
4 Hardware driver for Mechatronic Systems Inc. C6x_DIGIO DSP daughter card.
5 (http://robot0.ge.uiuc.edu/~spong/mecha/)
7 COMEDI - Linux Control and Measurement Device Interface
8 Copyright (C) 1999 Dan Block
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 Description: Mechatronic Systems Inc. C6x_DIGIO DSP daughter card
30 Devices: [Mechatronic Systems Inc.] C6x_DIGIO DSP daughter card (c6xdigio)
31 Updated: Sun Nov 20 20:18:34 EST 2005
33 This driver will not work with a 2.4 kernel.
34 http://robot0.ge.uiuc.edu/~spong/mecha/
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/sched.h>
42 #include <linux/errno.h>
43 #include <linux/ioport.h>
44 #include <linux/delay.h>
45 #include <linux/interrupt.h>
46 #include <linux/timex.h>
47 #include <linux/timer.h>
49 #include <linux/pnp.h>
51 #include "../comedidev.h"
53 static u8 ReadByteFromHwPort(unsigned long addr)
55 u8 result = inb(addr);
59 static void WriteByteToHwPort(unsigned long addr, u8 val)
64 #define C6XDIGIO_SIZE 3
69 #define C6XDIGIO_PARALLEL_DATA 0
70 #define C6XDIGIO_PARALLEL_STATUS 1
71 #define C6XDIGIO_PARALLEL_CONTROL 2
80 unsigned cmd; /* assuming here that int is 32bit */
81 struct pwmbitstype bits;
95 struct encbitstype bits;
98 #define C6XDIGIO_TIME_OUT 20
100 static int c6xdigio_attach(struct comedi_device *dev,
101 struct comedi_devconfig *it);
102 static int c6xdigio_detach(struct comedi_device *dev);
103 struct comedi_driver driver_c6xdigio = {
104 .driver_name = "c6xdigio",
105 .module = THIS_MODULE,
106 .attach = c6xdigio_attach,
107 .detach = c6xdigio_detach,
110 static void C6X_pwmInit(unsigned long baseAddr)
114 /* printk("Inside C6X_pwmInit\n"); */
116 WriteByteToHwPort(baseAddr, 0x70);
117 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0)
118 && (timeout < C6XDIGIO_TIME_OUT)) {
122 WriteByteToHwPort(baseAddr, 0x74);
124 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x80)
125 && (timeout < C6XDIGIO_TIME_OUT)) {
129 WriteByteToHwPort(baseAddr, 0x70);
131 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x0)
132 && (timeout < C6XDIGIO_TIME_OUT)) {
136 WriteByteToHwPort(baseAddr, 0x0);
138 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x80)
139 && (timeout < C6XDIGIO_TIME_OUT)) {
145 static void C6X_pwmOutput(unsigned long baseAddr, unsigned channel, int value)
148 union pwmcmdtype pwm;
152 /* printk("Inside C6X_pwmOutput\n"); */
162 } else { /* if channel == 1 */
166 WriteByteToHwPort(baseAddr, ppcmd + pwm.bits.sb0);
167 tmp = ReadByteFromHwPort(baseAddr + 1);
168 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
169 tmp = ReadByteFromHwPort(baseAddr + 1);
173 WriteByteToHwPort(baseAddr, ppcmd + pwm.bits.sb1 + 0x4);
175 tmp = ReadByteFromHwPort(baseAddr + 1);
176 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
177 tmp = ReadByteFromHwPort(baseAddr + 1);
181 WriteByteToHwPort(baseAddr, ppcmd + pwm.bits.sb2);
182 tmp = ReadByteFromHwPort(baseAddr + 1);
183 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
184 tmp = ReadByteFromHwPort(baseAddr + 1);
188 WriteByteToHwPort(baseAddr, ppcmd + pwm.bits.sb3 + 0x4);
190 tmp = ReadByteFromHwPort(baseAddr + 1);
191 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
192 tmp = ReadByteFromHwPort(baseAddr + 1);
196 WriteByteToHwPort(baseAddr, ppcmd + pwm.bits.sb4);
197 tmp = ReadByteFromHwPort(baseAddr + 1);
198 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
199 tmp = ReadByteFromHwPort(baseAddr + 1);
203 WriteByteToHwPort(baseAddr, 0x0);
205 tmp = ReadByteFromHwPort(baseAddr + 1);
206 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
207 tmp = ReadByteFromHwPort(baseAddr + 1);
213 static int C6X_encInput(unsigned long baseAddr, unsigned channel)
216 union encvaluetype enc;
220 /* printk("Inside C6X_encInput\n"); */
228 WriteByteToHwPort(baseAddr, ppcmd);
229 tmp = ReadByteFromHwPort(baseAddr + 1);
230 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
231 tmp = ReadByteFromHwPort(baseAddr + 1);
235 enc.bits.sb0 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
236 WriteByteToHwPort(baseAddr, ppcmd + 0x4);
238 tmp = ReadByteFromHwPort(baseAddr + 1);
239 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
240 tmp = ReadByteFromHwPort(baseAddr + 1);
243 enc.bits.sb1 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
244 WriteByteToHwPort(baseAddr, ppcmd);
246 tmp = ReadByteFromHwPort(baseAddr + 1);
247 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
248 tmp = ReadByteFromHwPort(baseAddr + 1);
251 enc.bits.sb2 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
252 WriteByteToHwPort(baseAddr, ppcmd + 0x4);
254 tmp = ReadByteFromHwPort(baseAddr + 1);
255 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
256 tmp = ReadByteFromHwPort(baseAddr + 1);
259 enc.bits.sb3 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
260 WriteByteToHwPort(baseAddr, ppcmd);
262 tmp = ReadByteFromHwPort(baseAddr + 1);
263 while (((tmp & 0x80) == 0) && (timeout < C6XDIGIO_TIME_OUT)) {
264 tmp = ReadByteFromHwPort(baseAddr + 1);
267 enc.bits.sb4 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
268 WriteByteToHwPort(baseAddr, ppcmd + 0x4);
270 tmp = ReadByteFromHwPort(baseAddr + 1);
271 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
272 tmp = ReadByteFromHwPort(baseAddr + 1);
275 enc.bits.sb5 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
276 WriteByteToHwPort(baseAddr, ppcmd);
278 tmp = ReadByteFromHwPort(baseAddr + 1);
279 while (((tmp & 0x80) == 0x0) && (timeout < C6XDIGIO_TIME_OUT)) {
280 tmp = ReadByteFromHwPort(baseAddr + 1);
283 enc.bits.sb6 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
284 WriteByteToHwPort(baseAddr, ppcmd + 0x4);
286 tmp = ReadByteFromHwPort(baseAddr + 1);
287 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
288 tmp = ReadByteFromHwPort(baseAddr + 1);
291 enc.bits.sb7 = ((ReadByteFromHwPort(baseAddr + 1) >> 3) & 0x7);
292 WriteByteToHwPort(baseAddr, ppcmd);
294 tmp = ReadByteFromHwPort(baseAddr + 1);
295 while (((tmp & 0x80) == 0x0) && (timeout < C6XDIGIO_TIME_OUT)) {
296 tmp = ReadByteFromHwPort(baseAddr + 1);
300 WriteByteToHwPort(baseAddr, 0x0);
302 tmp = ReadByteFromHwPort(baseAddr + 1);
303 while (((tmp & 0x80) == 0x80) && (timeout < C6XDIGIO_TIME_OUT)) {
304 tmp = ReadByteFromHwPort(baseAddr + 1);
308 return enc.value ^ 0x800000;
311 static void C6X_encResetAll(unsigned long baseAddr)
313 unsigned timeout = 0;
315 /* printk("Inside C6X_encResetAll\n"); */
317 WriteByteToHwPort(baseAddr, 0x68);
318 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0)
319 && (timeout < C6XDIGIO_TIME_OUT)) {
322 WriteByteToHwPort(baseAddr, 0x6C);
324 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x80)
325 && (timeout < C6XDIGIO_TIME_OUT)) {
328 WriteByteToHwPort(baseAddr, 0x68);
330 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x0)
331 && (timeout < C6XDIGIO_TIME_OUT)) {
334 WriteByteToHwPort(baseAddr, 0x0);
336 while (((ReadByteFromHwPort(baseAddr + 1) & 0x80) == 0x80)
337 && (timeout < C6XDIGIO_TIME_OUT)) {
342 static int c6xdigio_pwmo_insn_read(struct comedi_device *dev,
343 struct comedi_subdevice *s,
344 struct comedi_insn *insn, unsigned int *data)
346 printk("c6xdigio_pwmo_insn_read %x\n", insn->n);
350 static int c6xdigio_pwmo_insn_write(struct comedi_device *dev,
351 struct comedi_subdevice *s,
352 struct comedi_insn *insn,
356 int chan = CR_CHAN(insn->chanspec);
358 /* printk("c6xdigio_pwmo_insn_write %x\n", insn->n); */
359 for (i = 0; i < insn->n; i++) {
360 C6X_pwmOutput(dev->iobase, chan, data[i]);
361 /* devpriv->ao_readback[chan] = data[i]; */
366 /* static int c6xdigio_ei_init_insn_read(struct comedi_device *dev, */
367 /* struct comedi_subdevice *s, */
368 /* struct comedi_insn *insn, */
369 /* unsigned int *data) */
371 /* printk("c6xdigio_ei_init_insn_read %x\n", insn->n); */
372 /* return insn->n; */
375 /* static int c6xdigio_ei_init_insn_write(struct comedi_device *dev, */
376 /* struct comedi_subdevice *s, */
377 /* struct comedi_insn *insn, */
378 /* unsigned int *data) */
381 /* int chan = CR_CHAN(insn->chanspec); */
382 /* *//* C6X_encResetAll( dev->iobase ); */
383 /* *//* return insn->n; */
386 static int c6xdigio_ei_insn_read(struct comedi_device *dev,
387 struct comedi_subdevice *s,
388 struct comedi_insn *insn, unsigned int *data)
390 /* printk("c6xdigio_ei__insn_read %x\n", insn->n); */
392 int chan = CR_CHAN(insn->chanspec);
394 for (n = 0; n < insn->n; n++) {
395 data[n] = (C6X_encInput(dev->iobase, chan) & 0xffffff);
401 static void board_init(struct comedi_device *dev)
404 /* printk("Inside board_init\n"); */
406 C6X_pwmInit(dev->iobase);
407 C6X_encResetAll(dev->iobase);
411 /* static void board_halt(struct comedi_device *dev) { */
412 /* C6X_pwmInit(dev->iobase); */
416 options[0] - I/O port
418 options[2] - number of encoder chips installed
421 static const struct pnp_device_id c6xdigio_pnp_tbl[] = {
422 /* Standard LPT Printer Port */
423 {.id = "PNP0400",.driver_data = 0},
424 /* ECP Printer Port */
425 {.id = "PNP0401",.driver_data = 0},
429 static struct pnp_driver c6xdigio_pnp_driver = {
431 .id_table = c6xdigio_pnp_tbl,
434 static int c6xdigio_attach(struct comedi_device *dev,
435 struct comedi_devconfig *it)
438 unsigned long iobase;
440 struct comedi_subdevice *s;
442 iobase = it->options[0];
443 printk("comedi%d: c6xdigio: 0x%04lx\n", dev->minor, iobase);
444 if (!request_region(iobase, C6XDIGIO_SIZE, "c6xdigio")) {
445 printk("comedi%d: I/O port conflict\n", dev->minor);
448 dev->iobase = iobase;
449 dev->board_name = "c6xdigio";
451 result = alloc_subdevices(dev, 2); /* 3 with encoder_init write */
455 /* Make sure that PnP ports gets activated */
456 pnp_register_driver(&c6xdigio_pnp_driver);
458 irq = it->options[1];
460 printk("comedi%d: irq = %u ignored\n", dev->minor, irq);
461 } else if (irq == 0) {
462 printk("comedi%d: no irq\n", dev->minor);
465 s = dev->subdevices + 0;
466 /* pwm output subdevice */
467 s->type = COMEDI_SUBD_AO; /* Not sure what to put here */
468 s->subdev_flags = SDF_WRITEABLE;
470 /* s->trig[0] = c6xdigio_pwmo; */
471 s->insn_read = c6xdigio_pwmo_insn_read;
472 s->insn_write = c6xdigio_pwmo_insn_write;
474 s->range_table = &range_bipolar10; /* A suitable lie */
476 s = dev->subdevices + 1;
477 /* encoder (counter) subdevice */
478 s->type = COMEDI_SUBD_COUNTER;
479 s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
481 /* s->trig[0] = c6xdigio_ei; */
482 s->insn_read = c6xdigio_ei_insn_read;
483 s->maxdata = 0xffffff;
484 s->range_table = &range_unknown;
486 /* s = dev->subdevices + 2; */
487 /* pwm output subdevice */
488 /* s->type = COMEDI_SUBD_COUNTER; // Not sure what to put here */
489 /* s->subdev_flags = SDF_WRITEABLE; */
491 /* s->trig[0] = c6xdigio_ei_init; */
492 /* s->insn_read = c6xdigio_ei_init_insn_read; */
493 /* s->insn_write = c6xdigio_ei_init_insn_write; */
494 /* s->maxdata = 0xFFFF; // Really just a don't care */
495 /* s->range_table = &range_unknown; // Not sure what to put here */
497 /* I will call this init anyway but more than likely the DSP board will not be connect */
498 /* when device driver is loaded. */
504 static int c6xdigio_detach(struct comedi_device *dev)
506 /* board_halt(dev); may not need this */
508 printk("comedi%d: c6xdigio: remove\n", dev->minor);
511 release_region(dev->iobase, C6XDIGIO_SIZE);
514 free_irq(dev->irq, dev);
515 } /* Not using IRQ so I am not sure if I need this */
516 pnp_unregister_driver(&c6xdigio_pnp_driver);
521 COMEDI_INITCLEANUP(driver_c6xdigio);