1/*
2 *  comedi/drivers/das08.c
3 *  comedi driver for common DAS08 support (used by ISA/PCI/PCMCIA drivers)
4 *
5 *  COMEDI - Linux Control and Measurement Device Interface
6 *  Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 *  Copyright (C) 2001,2002,2003 Frank Mori Hess <fmhess@users.sourceforge.net>
8 *  Copyright (C) 2004 Salvador E. Tropea <set@users.sf.net> <set@ieee.org>
9 *
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.
14 *
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.
19 */
20
21/*
22 * Driver: das08
23 * Description: DAS-08 compatible boards
24 * Devices: various, see das08_isa, das08_cs, and das08_pci drivers
25 * Author: Warren Jasper, ds, Frank Hess
26 * Updated: Fri, 31 Aug 2012 19:19:06 +0100
27 * Status: works
28 *
29 * This driver is used by the das08_isa, das08_cs, and das08_pci
30 * drivers to provide the common support for the DAS-08 hardware.
31 *
32 * The driver doesn't support asynchronous commands, since the
33 * cheap das08 hardware doesn't really support them.
34 */
35
36#include <linux/module.h>
37
38#include "../comedidev.h"
39
40#include "8255.h"
41#include "8253.h"
42#include "das08.h"
43
44/*
45    cio-das08.pdf
46
47  "isa-das08"
48
49  0	a/d bits 0-3		start 8 bit
50  1	a/d bits 4-11		start 12 bit
51  2	eoc, ip1-3, irq, mux	op1-4, inte, mux
52  3	unused			unused
53  4567	8254
54  89ab	8255
55
56  requires hard-wiring for async ai
57
58*/
59
60#define DAS08_LSB		0
61#define DAS08_MSB		1
62#define DAS08_TRIG_12BIT	1
63#define DAS08_STATUS		2
64#define   DAS08_EOC			(1<<7)
65#define   DAS08_IRQ			(1<<3)
66#define   DAS08_IP(x)			(((x)>>4)&0x7)
67#define DAS08_CONTROL		2
68#define   DAS08_MUX_MASK	0x7
69#define   DAS08_MUX(x)		((x) & DAS08_MUX_MASK)
70#define   DAS08_INTE			(1<<3)
71#define   DAS08_DO_MASK		0xf0
72#define   DAS08_OP(x)		(((x) << 4) & DAS08_DO_MASK)
73
74/*
75    cio-das08jr.pdf
76
77  "das08/jr-ao"
78
79  0	a/d bits 0-3		unused
80  1	a/d bits 4-11		start 12 bit
81  2	eoc, mux		mux
82  3	di			do
83  4	unused			ao0_lsb
84  5	unused			ao0_msb
85  6	unused			ao1_lsb
86  7	unused			ao1_msb
87
88*/
89
90#define DAS08JR_DIO		3
91#define DAS08JR_AO_LSB(x)	((x) ? 6 : 4)
92#define DAS08JR_AO_MSB(x)	((x) ? 7 : 5)
93
94/*
95    cio-das08_aox.pdf
96
97  "das08-aoh"
98  "das08-aol"
99  "das08-aom"
100
101  0	a/d bits 0-3		start 8 bit
102  1	a/d bits 4-11		start 12 bit
103  2	eoc, ip1-3, irq, mux	op1-4, inte, mux
104  3	mux, gain status	gain control
105  4567	8254
106  8	unused			ao0_lsb
107  9	unused			ao0_msb
108  a	unused			ao1_lsb
109  b	unused			ao1_msb
110  89ab
111  cdef	8255
112*/
113
114#define DAS08AO_GAIN_CONTROL	3
115#define DAS08AO_GAIN_STATUS	3
116
117#define DAS08AO_AO_LSB(x)	((x) ? 0xa : 8)
118#define DAS08AO_AO_MSB(x)	((x) ? 0xb : 9)
119#define DAS08AO_AO_UPDATE	8
120
121/* gainlist same as _pgx_ below */
122
123static const struct comedi_lrange range_das08_pgl = {
124	9, {
125		BIP_RANGE(10),
126		BIP_RANGE(5),
127		BIP_RANGE(2.5),
128		BIP_RANGE(1.25),
129		BIP_RANGE(0.625),
130		UNI_RANGE(10),
131		UNI_RANGE(5),
132		UNI_RANGE(2.5),
133		UNI_RANGE(1.25)
134	}
135};
136
137static const struct comedi_lrange range_das08_pgh = {
138	12, {
139		BIP_RANGE(10),
140		BIP_RANGE(5),
141		BIP_RANGE(1),
142		BIP_RANGE(0.5),
143		BIP_RANGE(0.1),
144		BIP_RANGE(0.05),
145		BIP_RANGE(0.01),
146		BIP_RANGE(0.005),
147		UNI_RANGE(10),
148		UNI_RANGE(1),
149		UNI_RANGE(0.1),
150		UNI_RANGE(0.01)
151	}
152};
153
154static const struct comedi_lrange range_das08_pgm = {
155	9, {
156		BIP_RANGE(10),
157		BIP_RANGE(5),
158		BIP_RANGE(0.5),
159		BIP_RANGE(0.05),
160		BIP_RANGE(0.01),
161		UNI_RANGE(10),
162		UNI_RANGE(1),
163		UNI_RANGE(0.1),
164		UNI_RANGE(0.01)
165	}
166};				/*
167				   cio-das08jr.pdf
168
169				   "das08/jr-ao"
170
171				   0 a/d bits 0-3            unused
172				   1 a/d bits 4-11           start 12 bit
173				   2 eoc, mux                mux
174				   3 di                      do
175				   4 unused                  ao0_lsb
176				   5 unused                  ao0_msb
177				   6 unused                  ao1_lsb
178				   7 unused                  ao1_msb
179
180				 */
181
182static const struct comedi_lrange *const das08_ai_lranges[] = {
183	&range_unknown,
184	&range_bipolar5,
185	&range_das08_pgh,
186	&range_das08_pgl,
187	&range_das08_pgm,
188};
189
190static const int das08_pgh_gainlist[] = {
191	8, 0, 10, 2, 12, 4, 14, 6, 1, 3, 5, 7
192};
193static const int das08_pgl_gainlist[] = { 8, 0, 2, 4, 6, 1, 3, 5, 7 };
194static const int das08_pgm_gainlist[] = { 8, 0, 10, 12, 14, 9, 11, 13, 15 };
195
196static const int *const das08_gainlists[] = {
197	NULL,
198	NULL,
199	das08_pgh_gainlist,
200	das08_pgl_gainlist,
201	das08_pgm_gainlist,
202};
203
204static int das08_ai_eoc(struct comedi_device *dev,
205			struct comedi_subdevice *s,
206			struct comedi_insn *insn,
207			unsigned long context)
208{
209	unsigned int status;
210
211	status = inb(dev->iobase + DAS08_STATUS);
212	if ((status & DAS08_EOC) == 0)
213		return 0;
214	return -EBUSY;
215}
216
217static int das08_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
218			  struct comedi_insn *insn, unsigned int *data)
219{
220	const struct das08_board_struct *thisboard = dev->board_ptr;
221	struct das08_private_struct *devpriv = dev->private;
222	int n;
223	int chan;
224	int range;
225	int lsb, msb;
226	int ret;
227
228	chan = CR_CHAN(insn->chanspec);
229	range = CR_RANGE(insn->chanspec);
230
231	/* clear crap */
232	inb(dev->iobase + DAS08_LSB);
233	inb(dev->iobase + DAS08_MSB);
234
235	/* set multiplexer */
236	/*  lock to prevent race with digital output */
237	spin_lock(&dev->spinlock);
238	devpriv->do_mux_bits &= ~DAS08_MUX_MASK;
239	devpriv->do_mux_bits |= DAS08_MUX(chan);
240	outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
241	spin_unlock(&dev->spinlock);
242
243	if (s->range_table->length > 1) {
244		/* set gain/range */
245		range = CR_RANGE(insn->chanspec);
246		outb(devpriv->pg_gainlist[range],
247		     dev->iobase + DAS08AO_GAIN_CONTROL);
248	}
249
250	for (n = 0; n < insn->n; n++) {
251		/* clear over-range bits for 16-bit boards */
252		if (thisboard->ai_nbits == 16)
253			if (inb(dev->iobase + DAS08_MSB) & 0x80)
254				dev_info(dev->class_dev, "over-range\n");
255
256		/* trigger conversion */
257		outb_p(0, dev->iobase + DAS08_TRIG_12BIT);
258
259		ret = comedi_timeout(dev, s, insn, das08_ai_eoc, 0);
260		if (ret)
261			return ret;
262
263		msb = inb(dev->iobase + DAS08_MSB);
264		lsb = inb(dev->iobase + DAS08_LSB);
265		if (thisboard->ai_encoding == das08_encode12) {
266			data[n] = (lsb >> 4) | (msb << 4);
267		} else if (thisboard->ai_encoding == das08_pcm_encode12) {
268			data[n] = (msb << 8) + lsb;
269		} else if (thisboard->ai_encoding == das08_encode16) {
270			/* FPOS 16-bit boards are sign-magnitude */
271			if (msb & 0x80)
272				data[n] = (1 << 15) | lsb | ((msb & 0x7f) << 8);
273			else
274				data[n] = (1 << 15) - (lsb | (msb & 0x7f) << 8);
275		} else {
276			dev_err(dev->class_dev, "bug! unknown ai encoding\n");
277			return -1;
278		}
279	}
280
281	return n;
282}
283
284static int das08_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
285			  struct comedi_insn *insn, unsigned int *data)
286{
287	data[0] = 0;
288	data[1] = DAS08_IP(inb(dev->iobase + DAS08_STATUS));
289
290	return insn->n;
291}
292
293static int das08_do_wbits(struct comedi_device *dev,
294			  struct comedi_subdevice *s,
295			  struct comedi_insn *insn,
296			  unsigned int *data)
297{
298	struct das08_private_struct *devpriv = dev->private;
299
300	if (comedi_dio_update_state(s, data)) {
301		/* prevent race with setting of analog input mux */
302		spin_lock(&dev->spinlock);
303		devpriv->do_mux_bits &= ~DAS08_DO_MASK;
304		devpriv->do_mux_bits |= DAS08_OP(s->state);
305		outb(devpriv->do_mux_bits, dev->iobase + DAS08_CONTROL);
306		spin_unlock(&dev->spinlock);
307	}
308
309	data[1] = s->state;
310
311	return insn->n;
312}
313
314static int das08jr_di_rbits(struct comedi_device *dev,
315			    struct comedi_subdevice *s,
316			    struct comedi_insn *insn, unsigned int *data)
317{
318	data[0] = 0;
319	data[1] = inb(dev->iobase + DAS08JR_DIO);
320
321	return insn->n;
322}
323
324static int das08jr_do_wbits(struct comedi_device *dev,
325			    struct comedi_subdevice *s,
326			    struct comedi_insn *insn,
327			    unsigned int *data)
328{
329	if (comedi_dio_update_state(s, data))
330		outb(s->state, dev->iobase + DAS08JR_DIO);
331
332	data[1] = s->state;
333
334	return insn->n;
335}
336
337static void das08_ao_set_data(struct comedi_device *dev,
338			      unsigned int chan, unsigned int data)
339{
340	const struct das08_board_struct *thisboard = dev->board_ptr;
341	unsigned char lsb;
342	unsigned char msb;
343
344	lsb = data & 0xff;
345	msb = (data >> 8) & 0xff;
346	if (thisboard->is_jr) {
347		outb(lsb, dev->iobase + DAS08JR_AO_LSB(chan));
348		outb(msb, dev->iobase + DAS08JR_AO_MSB(chan));
349		/* load DACs */
350		inb(dev->iobase + DAS08JR_DIO);
351	} else {
352		outb(lsb, dev->iobase + DAS08AO_AO_LSB(chan));
353		outb(msb, dev->iobase + DAS08AO_AO_MSB(chan));
354		/* load DACs */
355		inb(dev->iobase + DAS08AO_AO_UPDATE);
356	}
357}
358
359static int das08_ao_insn_write(struct comedi_device *dev,
360			       struct comedi_subdevice *s,
361			       struct comedi_insn *insn,
362			       unsigned int *data)
363{
364	unsigned int chan = CR_CHAN(insn->chanspec);
365	unsigned int val = s->readback[chan];
366	int i;
367
368	for (i = 0; i < insn->n; i++) {
369		val = data[i];
370		das08_ao_set_data(dev, chan, val);
371	}
372	s->readback[chan] = val;
373
374	return insn->n;
375}
376
377static void i8254_initialize(struct comedi_device *dev)
378{
379	const struct das08_board_struct *thisboard = dev->board_ptr;
380	unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
381	unsigned int mode = I8254_MODE0 | I8254_BINARY;
382	int i;
383
384	for (i = 0; i < 3; ++i)
385		i8254_set_mode(i8254_iobase, 0, i, mode);
386}
387
388static int das08_counter_read(struct comedi_device *dev,
389			      struct comedi_subdevice *s,
390			      struct comedi_insn *insn, unsigned int *data)
391{
392	const struct das08_board_struct *thisboard = dev->board_ptr;
393	unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
394	int chan = insn->chanspec;
395
396	data[0] = i8254_read(i8254_iobase, 0, chan);
397	return 1;
398}
399
400static int das08_counter_write(struct comedi_device *dev,
401			       struct comedi_subdevice *s,
402			       struct comedi_insn *insn, unsigned int *data)
403{
404	const struct das08_board_struct *thisboard = dev->board_ptr;
405	unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
406	int chan = insn->chanspec;
407
408	i8254_write(i8254_iobase, 0, chan, data[0]);
409	return 1;
410}
411
412static int das08_counter_config(struct comedi_device *dev,
413				struct comedi_subdevice *s,
414				struct comedi_insn *insn, unsigned int *data)
415{
416	const struct das08_board_struct *thisboard = dev->board_ptr;
417	unsigned long i8254_iobase = dev->iobase + thisboard->i8254_offset;
418	int chan = insn->chanspec;
419
420	switch (data[0]) {
421	case INSN_CONFIG_SET_COUNTER_MODE:
422		i8254_set_mode(i8254_iobase, 0, chan, data[1]);
423		break;
424	case INSN_CONFIG_8254_READ_STATUS:
425		data[1] = i8254_status(i8254_iobase, 0, chan);
426		break;
427	default:
428		return -EINVAL;
429	}
430	return 2;
431}
432
433int das08_common_attach(struct comedi_device *dev, unsigned long iobase)
434{
435	const struct das08_board_struct *thisboard = dev->board_ptr;
436	struct das08_private_struct *devpriv = dev->private;
437	struct comedi_subdevice *s;
438	int ret;
439	int i;
440
441	dev->iobase = iobase;
442
443	dev->board_name = thisboard->name;
444
445	ret = comedi_alloc_subdevices(dev, 6);
446	if (ret)
447		return ret;
448
449	s = &dev->subdevices[0];
450	/* ai */
451	if (thisboard->ai_nbits) {
452		s->type = COMEDI_SUBD_AI;
453		/* XXX some boards actually have differential
454		 * inputs instead of single ended.
455		 * The driver does nothing with arefs though,
456		 * so it's no big deal.
457		 */
458		s->subdev_flags = SDF_READABLE | SDF_GROUND;
459		s->n_chan = 8;
460		s->maxdata = (1 << thisboard->ai_nbits) - 1;
461		s->range_table = das08_ai_lranges[thisboard->ai_pg];
462		s->insn_read = das08_ai_rinsn;
463		devpriv->pg_gainlist = das08_gainlists[thisboard->ai_pg];
464	} else {
465		s->type = COMEDI_SUBD_UNUSED;
466	}
467
468	s = &dev->subdevices[1];
469	/* ao */
470	if (thisboard->ao_nbits) {
471		s->type = COMEDI_SUBD_AO;
472		s->subdev_flags = SDF_WRITABLE;
473		s->n_chan = 2;
474		s->maxdata = (1 << thisboard->ao_nbits) - 1;
475		s->range_table = &range_bipolar5;
476		s->insn_write = das08_ao_insn_write;
477		s->insn_read = comedi_readback_insn_read;
478
479		ret = comedi_alloc_subdev_readback(s);
480		if (ret)
481			return ret;
482
483		/* intialize all channels to 0V */
484		for (i = 0; i < s->n_chan; i++) {
485			s->readback[i] = s->maxdata / 2;
486			das08_ao_set_data(dev, i, s->readback[i]);
487		}
488	} else {
489		s->type = COMEDI_SUBD_UNUSED;
490	}
491
492	s = &dev->subdevices[2];
493	/* di */
494	if (thisboard->di_nchan) {
495		s->type = COMEDI_SUBD_DI;
496		s->subdev_flags = SDF_READABLE;
497		s->n_chan = thisboard->di_nchan;
498		s->maxdata = 1;
499		s->range_table = &range_digital;
500		s->insn_bits =
501			thisboard->is_jr ? das08jr_di_rbits : das08_di_rbits;
502	} else {
503		s->type = COMEDI_SUBD_UNUSED;
504	}
505
506	s = &dev->subdevices[3];
507	/* do */
508	if (thisboard->do_nchan) {
509		s->type = COMEDI_SUBD_DO;
510		s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
511		s->n_chan = thisboard->do_nchan;
512		s->maxdata = 1;
513		s->range_table = &range_digital;
514		s->insn_bits =
515			thisboard->is_jr ? das08jr_do_wbits : das08_do_wbits;
516	} else {
517		s->type = COMEDI_SUBD_UNUSED;
518	}
519
520	s = &dev->subdevices[4];
521	/* 8255 */
522	if (thisboard->i8255_offset != 0) {
523		ret = subdev_8255_init(dev, s, NULL, thisboard->i8255_offset);
524		if (ret)
525			return ret;
526	} else {
527		s->type = COMEDI_SUBD_UNUSED;
528	}
529
530	s = &dev->subdevices[5];
531	/* 8254 */
532	if (thisboard->i8254_offset != 0) {
533		s->type = COMEDI_SUBD_COUNTER;
534		s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
535		s->n_chan = 3;
536		s->maxdata = 0xFFFF;
537		s->insn_read = das08_counter_read;
538		s->insn_write = das08_counter_write;
539		s->insn_config = das08_counter_config;
540		i8254_initialize(dev);
541	} else {
542		s->type = COMEDI_SUBD_UNUSED;
543	}
544
545	return 0;
546}
547EXPORT_SYMBOL_GPL(das08_common_attach);
548
549static int __init das08_init(void)
550{
551	return 0;
552}
553module_init(das08_init);
554
555static void __exit das08_exit(void)
556{
557}
558module_exit(das08_exit);
559
560MODULE_AUTHOR("Comedi http://www.comedi.org");
561MODULE_DESCRIPTION("Comedi low-level driver");
562MODULE_LICENSE("GPL");
563