blob: c3eb54622bc37c46a5c37d84e0436757b4a37d30 [file] [log] [blame]
Chris R. Baugher2323b272009-02-19 10:03:23 -08001/*
2 comedi/drivers/ni_atmio16d.c
3 Hardware driver for National Instruments AT-MIO16D board
4 Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
Chris R. Baugher2323b272009-02-19 10:03:23 -080015 */
16/*
17Driver: ni_atmio16d
18Description: National Instruments AT-MIO-16D
19Author: Chris R. Baugher <baugher@enteract.com>
20Status: unknown
21Devices: [National Instruments] AT-MIO-16 (atmio16), AT-MIO-16D (atmio16d)
22*/
23/*
24 * I must give credit here to Michal Dobes <dobes@tesnet.cz> who
25 * wrote the driver for Advantec's pcl812 boards. I used the interrupt
26 * handling code from his driver as an example for this one.
27 *
28 * Chris Baugher
29 * 5/1/2000
30 *
31 */
32
H Hartley Sweetence157f82013-06-24 17:04:43 -070033#include <linux/module.h>
Greg Kroah-Hartman25436dc2009-04-27 15:14:34 -070034#include <linux/interrupt.h>
Chris R. Baugher2323b272009-02-19 10:03:23 -080035#include "../comedidev.h"
36
Chris R. Baugher2323b272009-02-19 10:03:23 -080037#include "8255.h"
38
39/* Configuration and Status Registers */
40#define COM_REG_1 0x00 /* wo 16 */
41#define STAT_REG 0x00 /* ro 16 */
42#define COM_REG_2 0x02 /* wo 16 */
43/* Event Strobe Registers */
44#define START_CONVERT_REG 0x08 /* wo 16 */
45#define START_DAQ_REG 0x0A /* wo 16 */
46#define AD_CLEAR_REG 0x0C /* wo 16 */
47#define EXT_STROBE_REG 0x0E /* wo 16 */
48/* Analog Output Registers */
49#define DAC0_REG 0x10 /* wo 16 */
50#define DAC1_REG 0x12 /* wo 16 */
51#define INT2CLR_REG 0x14 /* wo 16 */
52/* Analog Input Registers */
53#define MUX_CNTR_REG 0x04 /* wo 16 */
54#define MUX_GAIN_REG 0x06 /* wo 16 */
55#define AD_FIFO_REG 0x16 /* ro 16 */
56#define DMA_TC_INT_CLR_REG 0x16 /* wo 16 */
57/* AM9513A Counter/Timer Registers */
58#define AM9513A_DATA_REG 0x18 /* rw 16 */
59#define AM9513A_COM_REG 0x1A /* wo 16 */
60#define AM9513A_STAT_REG 0x1A /* ro 16 */
61/* MIO-16 Digital I/O Registers */
62#define MIO_16_DIG_IN_REG 0x1C /* ro 16 */
63#define MIO_16_DIG_OUT_REG 0x1C /* wo 16 */
64/* RTSI Switch Registers */
65#define RTSI_SW_SHIFT_REG 0x1E /* wo 8 */
66#define RTSI_SW_STROBE_REG 0x1F /* wo 8 */
67/* DIO-24 Registers */
68#define DIO_24_PORTA_REG 0x00 /* rw 8 */
69#define DIO_24_PORTB_REG 0x01 /* rw 8 */
70#define DIO_24_PORTC_REG 0x02 /* rw 8 */
71#define DIO_24_CNFG_REG 0x03 /* wo 8 */
72
73/* Command Register bits */
74#define COMREG1_2SCADC 0x0001
75#define COMREG1_1632CNT 0x0002
76#define COMREG1_SCANEN 0x0008
77#define COMREG1_DAQEN 0x0010
78#define COMREG1_DMAEN 0x0020
79#define COMREG1_CONVINTEN 0x0080
80#define COMREG2_SCN2 0x0010
81#define COMREG2_INTEN 0x0080
82#define COMREG2_DOUTEN0 0x0100
83#define COMREG2_DOUTEN1 0x0200
84/* Status Register bits */
85#define STAT_AD_OVERRUN 0x0100
86#define STAT_AD_OVERFLOW 0x0200
87#define STAT_AD_DAQPROG 0x0800
88#define STAT_AD_CONVAVAIL 0x2000
89#define STAT_AD_DAQSTOPINT 0x4000
90/* AM9513A Counter/Timer defines */
91#define CLOCK_1_MHZ 0x8B25
92#define CLOCK_100_KHZ 0x8C25
93#define CLOCK_10_KHZ 0x8D25
94#define CLOCK_1_KHZ 0x8E25
95#define CLOCK_100_HZ 0x8F25
Chris R. Baugher2323b272009-02-19 10:03:23 -080096
Bill Pemberton9f30c242009-03-16 22:16:18 -040097struct atmio16_board_t {
Chris R. Baugher2323b272009-02-19 10:03:23 -080098 const char *name;
99 int has_8255;
Bill Pemberton9f30c242009-03-16 22:16:18 -0400100};
101
Chris R. Baugher2323b272009-02-19 10:03:23 -0800102/* range structs */
H Hartley Sweetenaf71a892013-12-09 17:31:06 -0700103static const struct comedi_lrange range_atmio16d_ai_10_bipolar = {
104 4, {
105 BIP_RANGE(10),
106 BIP_RANGE(1),
107 BIP_RANGE(0.1),
108 BIP_RANGE(0.02)
109 }
Chris R. Baugher2323b272009-02-19 10:03:23 -0800110};
111
H Hartley Sweetenaf71a892013-12-09 17:31:06 -0700112static const struct comedi_lrange range_atmio16d_ai_5_bipolar = {
113 4, {
114 BIP_RANGE(5),
115 BIP_RANGE(0.5),
116 BIP_RANGE(0.05),
117 BIP_RANGE(0.01)
118 }
Chris R. Baugher2323b272009-02-19 10:03:23 -0800119};
120
H Hartley Sweetenaf71a892013-12-09 17:31:06 -0700121static const struct comedi_lrange range_atmio16d_ai_unipolar = {
122 4, {
123 UNI_RANGE(10),
124 UNI_RANGE(1),
125 UNI_RANGE(0.1),
126 UNI_RANGE(0.02)
127 }
Chris R. Baugher2323b272009-02-19 10:03:23 -0800128};
129
130/* private data struct */
Bill Pemberton8c8a2882009-03-19 17:59:28 -0400131struct atmio16d_private {
Chris R. Baugher2323b272009-02-19 10:03:23 -0800132 enum { adc_diff, adc_singleended } adc_mux;
133 enum { adc_bipolar10, adc_bipolar5, adc_unipolar10 } adc_range;
134 enum { adc_2comp, adc_straight } adc_coding;
135 enum { dac_bipolar, dac_unipolar } dac0_range, dac1_range;
136 enum { dac_internal, dac_external } dac0_reference, dac1_reference;
137 enum { dac_2comp, dac_straight } dac0_coding, dac1_coding;
Bill Pemberton9ced1de2009-03-16 22:05:31 -0400138 const struct comedi_lrange *ao_range_type_list[2];
Bruce Jones2c146812009-09-24 14:59:52 -0700139 unsigned int com_reg_1_state; /* current state of command register 1 */
140 unsigned int com_reg_2_state; /* current state of command register 2 */
Bill Pemberton8c8a2882009-03-19 17:59:28 -0400141};
Chris R. Baugher2323b272009-02-19 10:03:23 -0800142
Bill Pembertonda91b262009-04-09 16:07:03 -0400143static void reset_counters(struct comedi_device *dev)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800144{
145 /* Counter 2 */
146 outw(0xFFC2, dev->iobase + AM9513A_COM_REG);
147 outw(0xFF02, dev->iobase + AM9513A_COM_REG);
148 outw(0x4, dev->iobase + AM9513A_DATA_REG);
149 outw(0xFF0A, dev->iobase + AM9513A_COM_REG);
150 outw(0x3, dev->iobase + AM9513A_DATA_REG);
151 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
152 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
153 /* Counter 3 */
154 outw(0xFFC4, dev->iobase + AM9513A_COM_REG);
155 outw(0xFF03, dev->iobase + AM9513A_COM_REG);
156 outw(0x4, dev->iobase + AM9513A_DATA_REG);
157 outw(0xFF0B, dev->iobase + AM9513A_COM_REG);
158 outw(0x3, dev->iobase + AM9513A_DATA_REG);
159 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
160 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
161 /* Counter 4 */
162 outw(0xFFC8, dev->iobase + AM9513A_COM_REG);
163 outw(0xFF04, dev->iobase + AM9513A_COM_REG);
164 outw(0x4, dev->iobase + AM9513A_DATA_REG);
165 outw(0xFF0C, dev->iobase + AM9513A_COM_REG);
166 outw(0x3, dev->iobase + AM9513A_DATA_REG);
167 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
168 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
169 /* Counter 5 */
170 outw(0xFFD0, dev->iobase + AM9513A_COM_REG);
171 outw(0xFF05, dev->iobase + AM9513A_COM_REG);
172 outw(0x4, dev->iobase + AM9513A_DATA_REG);
173 outw(0xFF0D, dev->iobase + AM9513A_COM_REG);
174 outw(0x3, dev->iobase + AM9513A_DATA_REG);
175 outw(0xFF50, dev->iobase + AM9513A_COM_REG);
176 outw(0xFF50, dev->iobase + AM9513A_COM_REG);
177
178 outw(0, dev->iobase + AD_CLEAR_REG);
179}
180
Bill Pembertonda91b262009-04-09 16:07:03 -0400181static void reset_atmio16d(struct comedi_device *dev)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800182{
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700183 struct atmio16d_private *devpriv = dev->private;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800184 int i;
185
186 /* now we need to initialize the board */
187 outw(0, dev->iobase + COM_REG_1);
188 outw(0, dev->iobase + COM_REG_2);
189 outw(0, dev->iobase + MUX_GAIN_REG);
190 /* init AM9513A timer */
191 outw(0xFFFF, dev->iobase + AM9513A_COM_REG);
192 outw(0xFFEF, dev->iobase + AM9513A_COM_REG);
193 outw(0xFF17, dev->iobase + AM9513A_COM_REG);
194 outw(0xF000, dev->iobase + AM9513A_DATA_REG);
195 for (i = 1; i <= 5; ++i) {
196 outw(0xFF00 + i, dev->iobase + AM9513A_COM_REG);
197 outw(0x0004, dev->iobase + AM9513A_DATA_REG);
198 outw(0xFF08 + i, dev->iobase + AM9513A_COM_REG);
199 outw(0x3, dev->iobase + AM9513A_DATA_REG);
200 }
201 outw(0xFF5F, dev->iobase + AM9513A_COM_REG);
202 /* timer init done */
203 outw(0, dev->iobase + AD_CLEAR_REG);
204 outw(0, dev->iobase + INT2CLR_REG);
205 /* select straight binary mode for Analog Input */
206 devpriv->com_reg_1_state |= 1;
207 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
208 devpriv->adc_coding = adc_straight;
209 /* zero the analog outputs */
210 outw(2048, dev->iobase + DAC0_REG);
211 outw(2048, dev->iobase + DAC1_REG);
212}
213
Jiri Slaby70265d22009-03-26 09:34:06 +0100214static irqreturn_t atmio16d_interrupt(int irq, void *d)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800215{
Bill Pemberton71b5f4f2009-03-16 22:05:08 -0400216 struct comedi_device *dev = d;
H Hartley Sweetenbea70d02013-12-05 13:43:46 -0700217 struct comedi_subdevice *s = dev->read_subdev;
H Hartley Sweeten67fdc302014-10-22 15:36:59 -0700218 unsigned short val;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800219
H Hartley Sweeten67fdc302014-10-22 15:36:59 -0700220 val = inw(dev->iobase + AD_FIFO_REG);
221 comedi_buf_write_samples(s, &val, 1);
H Hartley Sweetenc827d2b2014-09-18 11:35:28 -0700222 comedi_handle_events(dev, s);
H Hartley Sweeten67fdc302014-10-22 15:36:59 -0700223
Chris R. Baugher2323b272009-02-19 10:03:23 -0800224 return IRQ_HANDLED;
225}
226
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530227static int atmio16d_ai_cmdtest(struct comedi_device *dev,
228 struct comedi_subdevice *s,
229 struct comedi_cmd *cmd)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800230{
H Hartley Sweeten27020ff2012-09-26 14:11:10 -0700231 int err = 0;
H Hartley Sweeten985eb502012-06-13 18:09:51 -0700232
H Hartley Sweeten27020ff2012-09-26 14:11:10 -0700233 /* Step 1 : check if triggers are trivially valid */
Chris R. Baugher2323b272009-02-19 10:03:23 -0800234
Ian Abbott2684a5e2015-03-27 19:14:22 +0000235 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
236 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
H Hartley Sweeten27020ff2012-09-26 14:11:10 -0700237 TRIG_FOLLOW | TRIG_TIMER);
Ian Abbott2684a5e2015-03-27 19:14:22 +0000238 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_TIMER);
239 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
240 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800241
242 if (err)
243 return 1;
244
H Hartley Sweeten27020ff2012-09-26 14:11:10 -0700245 /* Step 2a : make sure trigger sources are unique */
246
Ian Abbott2684a5e2015-03-27 19:14:22 +0000247 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
248 err |= comedi_check_trigger_is_unique(cmd->stop_src);
H Hartley Sweeten27020ff2012-09-26 14:11:10 -0700249
250 /* Step 2b : and mutually compatible */
Chris R. Baugher2323b272009-02-19 10:03:23 -0800251
252 if (err)
253 return 2;
254
H Hartley Sweetenec3ffe62012-11-13 17:56:41 -0700255 /* Step 3: check if arguments are trivially valid */
Chris R. Baugher2323b272009-02-19 10:03:23 -0800256
Ian Abbott2684a5e2015-03-27 19:14:22 +0000257 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
H Hartley Sweetenec3ffe62012-11-13 17:56:41 -0700258
Chris R. Baugher2323b272009-02-19 10:03:23 -0800259 if (cmd->scan_begin_src == TRIG_FOLLOW) {
260 /* internal trigger */
Ian Abbott2684a5e2015-03-27 19:14:22 +0000261 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800262 } else {
263#if 0
264 /* external trigger */
265 /* should be level/edge, hi/lo specification here */
Ian Abbott2684a5e2015-03-27 19:14:22 +0000266 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800267#endif
268 }
269
Ian Abbott2684a5e2015-03-27 19:14:22 +0000270 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 10000);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800271#if 0
Ian Abbott2684a5e2015-03-27 19:14:22 +0000272 err |= comedi_check_trigger_arg_max(&cmd->convert_arg, SLOWEST_TIMER);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800273#endif
H Hartley Sweetenec3ffe62012-11-13 17:56:41 -0700274
Ian Abbott2684a5e2015-03-27 19:14:22 +0000275 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
276 cmd->chanlist_len);
H Hartley Sweetenec3ffe62012-11-13 17:56:41 -0700277
H Hartley Sweetenfd6887b2014-09-09 16:15:50 -0700278 if (cmd->stop_src == TRIG_COUNT)
Ian Abbott2684a5e2015-03-27 19:14:22 +0000279 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
H Hartley Sweetenfd6887b2014-09-09 16:15:50 -0700280 else /* TRIG_NONE */
Ian Abbott2684a5e2015-03-27 19:14:22 +0000281 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800282
283 if (err)
284 return 3;
285
286 return 0;
287}
288
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530289static int atmio16d_ai_cmd(struct comedi_device *dev,
290 struct comedi_subdevice *s)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800291{
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700292 struct atmio16d_private *devpriv = dev->private;
Bill Pembertonea6d0d42009-03-16 22:05:47 -0400293 struct comedi_cmd *cmd = &s->async->cmd;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800294 unsigned int timer, base_clock;
295 unsigned int sample_count, tmp, chan, gain;
296 int i;
H Hartley Sweeten985eb502012-06-13 18:09:51 -0700297
Chris R. Baugher2323b272009-02-19 10:03:23 -0800298 /* This is slowly becoming a working command interface. *
299 * It is still uber-experimental */
300
301 reset_counters(dev);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800302
303 /* check if scanning multiple channels */
304 if (cmd->chanlist_len < 2) {
305 devpriv->com_reg_1_state &= ~COMREG1_SCANEN;
306 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
307 } else {
308 devpriv->com_reg_1_state |= COMREG1_SCANEN;
309 devpriv->com_reg_2_state |= COMREG2_SCN2;
310 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
311 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
312 }
313
314 /* Setup the Mux-Gain Counter */
315 for (i = 0; i < cmd->chanlist_len; ++i) {
316 chan = CR_CHAN(cmd->chanlist[i]);
317 gain = CR_RANGE(cmd->chanlist[i]);
318 outw(i, dev->iobase + MUX_CNTR_REG);
319 tmp = chan | (gain << 6);
320 if (i == cmd->scan_end_arg - 1)
321 tmp |= 0x0010; /* set LASTONE bit */
322 outw(tmp, dev->iobase + MUX_GAIN_REG);
323 }
324
325 /* Now program the sample interval timer */
326 /* Figure out which clock to use then get an
327 * appropriate timer value */
328 if (cmd->convert_arg < 65536000) {
329 base_clock = CLOCK_1_MHZ;
330 timer = cmd->convert_arg / 1000;
331 } else if (cmd->convert_arg < 655360000) {
332 base_clock = CLOCK_100_KHZ;
333 timer = cmd->convert_arg / 10000;
Andrey Utkind2fd4d32014-07-11 15:38:30 +0100334 } else /* cmd->convert_arg < 6553600000 */ {
Chris R. Baugher2323b272009-02-19 10:03:23 -0800335 base_clock = CLOCK_10_KHZ;
336 timer = cmd->convert_arg / 100000;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800337 }
338 outw(0xFF03, dev->iobase + AM9513A_COM_REG);
339 outw(base_clock, dev->iobase + AM9513A_DATA_REG);
340 outw(0xFF0B, dev->iobase + AM9513A_COM_REG);
341 outw(0x2, dev->iobase + AM9513A_DATA_REG);
342 outw(0xFF44, dev->iobase + AM9513A_COM_REG);
343 outw(0xFFF3, dev->iobase + AM9513A_COM_REG);
344 outw(timer, dev->iobase + AM9513A_DATA_REG);
345 outw(0xFF24, dev->iobase + AM9513A_COM_REG);
346
347 /* Now figure out how many samples to get */
348 /* and program the sample counter */
349 sample_count = cmd->stop_arg * cmd->scan_end_arg;
350 outw(0xFF04, dev->iobase + AM9513A_COM_REG);
351 outw(0x1025, dev->iobase + AM9513A_DATA_REG);
352 outw(0xFF0C, dev->iobase + AM9513A_COM_REG);
353 if (sample_count < 65536) {
354 /* use only Counter 4 */
355 outw(sample_count, dev->iobase + AM9513A_DATA_REG);
356 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
357 outw(0xFFF4, dev->iobase + AM9513A_COM_REG);
358 outw(0xFF28, dev->iobase + AM9513A_COM_REG);
359 devpriv->com_reg_1_state &= ~COMREG1_1632CNT;
360 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
361 } else {
362 /* Counter 4 and 5 are needed */
Bill Pembertonc3744132009-04-22 21:11:47 -0400363
364 tmp = sample_count & 0xFFFF;
365 if (tmp)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800366 outw(tmp - 1, dev->iobase + AM9513A_DATA_REG);
Bill Pembertonc3744132009-04-22 21:11:47 -0400367 else
Chris R. Baugher2323b272009-02-19 10:03:23 -0800368 outw(0xFFFF, dev->iobase + AM9513A_DATA_REG);
Bill Pembertonc3744132009-04-22 21:11:47 -0400369
Chris R. Baugher2323b272009-02-19 10:03:23 -0800370 outw(0xFF48, dev->iobase + AM9513A_COM_REG);
371 outw(0, dev->iobase + AM9513A_DATA_REG);
372 outw(0xFF28, dev->iobase + AM9513A_COM_REG);
373 outw(0xFF05, dev->iobase + AM9513A_COM_REG);
374 outw(0x25, dev->iobase + AM9513A_DATA_REG);
375 outw(0xFF0D, dev->iobase + AM9513A_COM_REG);
376 tmp = sample_count & 0xFFFF;
377 if ((tmp == 0) || (tmp == 1)) {
378 outw((sample_count >> 16) & 0xFFFF,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530379 dev->iobase + AM9513A_DATA_REG);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800380 } else {
381 outw(((sample_count >> 16) & 0xFFFF) + 1,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530382 dev->iobase + AM9513A_DATA_REG);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800383 }
384 outw(0xFF70, dev->iobase + AM9513A_COM_REG);
385 devpriv->com_reg_1_state |= COMREG1_1632CNT;
386 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
387 }
388
389 /* Program the scan interval timer ONLY IF SCANNING IS ENABLED */
390 /* Figure out which clock to use then get an
391 * appropriate timer value */
392 if (cmd->chanlist_len > 1) {
393 if (cmd->scan_begin_arg < 65536000) {
394 base_clock = CLOCK_1_MHZ;
395 timer = cmd->scan_begin_arg / 1000;
396 } else if (cmd->scan_begin_arg < 655360000) {
397 base_clock = CLOCK_100_KHZ;
398 timer = cmd->scan_begin_arg / 10000;
Andrey Utkind2fd4d32014-07-11 15:38:30 +0100399 } else /* cmd->scan_begin_arg < 6553600000 */ {
Chris R. Baugher2323b272009-02-19 10:03:23 -0800400 base_clock = CLOCK_10_KHZ;
401 timer = cmd->scan_begin_arg / 100000;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800402 }
403 outw(0xFF02, dev->iobase + AM9513A_COM_REG);
404 outw(base_clock, dev->iobase + AM9513A_DATA_REG);
405 outw(0xFF0A, dev->iobase + AM9513A_COM_REG);
406 outw(0x2, dev->iobase + AM9513A_DATA_REG);
407 outw(0xFF42, dev->iobase + AM9513A_COM_REG);
408 outw(0xFFF2, dev->iobase + AM9513A_COM_REG);
409 outw(timer, dev->iobase + AM9513A_DATA_REG);
410 outw(0xFF22, dev->iobase + AM9513A_COM_REG);
411 }
412
413 /* Clear the A/D FIFO and reset the MUX counter */
414 outw(0, dev->iobase + AD_CLEAR_REG);
415 outw(0, dev->iobase + MUX_CNTR_REG);
416 outw(0, dev->iobase + INT2CLR_REG);
417 /* enable this acquisition operation */
418 devpriv->com_reg_1_state |= COMREG1_DAQEN;
419 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
420 /* enable interrupts for conversion completion */
421 devpriv->com_reg_1_state |= COMREG1_CONVINTEN;
422 devpriv->com_reg_2_state |= COMREG2_INTEN;
423 outw(devpriv->com_reg_1_state, dev->iobase + COM_REG_1);
424 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
425 /* apply a trigger. this starts the counters! */
426 outw(0, dev->iobase + START_DAQ_REG);
427
428 return 0;
429}
430
431/* This will cancel a running acquisition operation */
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530432static int atmio16d_ai_cancel(struct comedi_device *dev,
433 struct comedi_subdevice *s)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800434{
435 reset_atmio16d(dev);
436
437 return 0;
438}
439
H Hartley Sweeten03ff5892014-02-10 11:49:26 -0700440static int atmio16d_ai_eoc(struct comedi_device *dev,
441 struct comedi_subdevice *s,
442 struct comedi_insn *insn,
443 unsigned long context)
444{
445 unsigned int status;
446
447 status = inw(dev->iobase + STAT_REG);
448 if (status & STAT_AD_CONVAVAIL)
449 return 0;
450 if (status & STAT_AD_OVERFLOW) {
451 outw(0, dev->iobase + AD_CLEAR_REG);
452 return -EOVERFLOW;
453 }
454 return -EBUSY;
455}
456
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530457static int atmio16d_ai_insn_read(struct comedi_device *dev,
458 struct comedi_subdevice *s,
459 struct comedi_insn *insn, unsigned int *data)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800460{
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700461 struct atmio16d_private *devpriv = dev->private;
H Hartley Sweeten03ff5892014-02-10 11:49:26 -0700462 int i;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800463 int chan;
464 int gain;
H Hartley Sweeten03ff5892014-02-10 11:49:26 -0700465 int ret;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800466
Chris R. Baugher2323b272009-02-19 10:03:23 -0800467 chan = CR_CHAN(insn->chanspec);
468 gain = CR_RANGE(insn->chanspec);
469
470 /* reset the Analog input circuitry */
Bill Pemberton2696fb52009-03-27 11:29:34 -0400471 /* outw( 0, dev->iobase+AD_CLEAR_REG ); */
Chris R. Baugher2323b272009-02-19 10:03:23 -0800472 /* reset the Analog Input MUX Counter to 0 */
Bill Pemberton2696fb52009-03-27 11:29:34 -0400473 /* outw( 0, dev->iobase+MUX_CNTR_REG ); */
Chris R. Baugher2323b272009-02-19 10:03:23 -0800474
475 /* set the Input MUX gain */
476 outw(chan | (gain << 6), dev->iobase + MUX_GAIN_REG);
477
478 for (i = 0; i < insn->n; i++) {
479 /* start the conversion */
480 outw(0, dev->iobase + START_CONVERT_REG);
H Hartley Sweeten03ff5892014-02-10 11:49:26 -0700481
Chris R. Baugher2323b272009-02-19 10:03:23 -0800482 /* wait for it to finish */
H Hartley Sweeten03ff5892014-02-10 11:49:26 -0700483 ret = comedi_timeout(dev, s, insn, atmio16d_ai_eoc, 0);
484 if (ret)
485 return ret;
486
487 /* read the data now */
488 data[i] = inw(dev->iobase + AD_FIFO_REG);
489 /* change to two's complement if need be */
490 if (devpriv->adc_coding == adc_2comp)
491 data[i] ^= 0x800;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800492 }
493
494 return i;
495}
496
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530497static int atmio16d_ao_insn_write(struct comedi_device *dev,
498 struct comedi_subdevice *s,
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700499 struct comedi_insn *insn,
500 unsigned int *data)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800501{
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700502 struct atmio16d_private *devpriv = dev->private;
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700503 unsigned int chan = CR_CHAN(insn->chanspec);
504 unsigned int reg = (chan) ? DAC1_REG : DAC0_REG;
505 bool munge = false;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800506 int i;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800507
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700508 if (chan == 0 && devpriv->dac0_coding == dac_2comp)
509 munge = true;
510 if (chan == 1 && devpriv->dac1_coding == dac_2comp)
511 munge = true;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800512
513 for (i = 0; i < insn->n; i++) {
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700514 unsigned int val = data[i];
515
H Hartley Sweetene8928752014-08-25 16:04:27 -0700516 s->readback[chan] = val;
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700517
518 if (munge)
519 val ^= 0x800;
520
521 outw(val, dev->iobase + reg);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800522 }
H Hartley Sweeten898fb5c2014-08-25 16:04:26 -0700523
524 return insn->n;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800525}
526
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530527static int atmio16d_dio_insn_bits(struct comedi_device *dev,
528 struct comedi_subdevice *s,
H Hartley Sweeten97f42892013-08-30 11:05:58 -0700529 struct comedi_insn *insn,
530 unsigned int *data)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800531{
H Hartley Sweeten97f42892013-08-30 11:05:58 -0700532 if (comedi_dio_update_state(s, data))
Chris R. Baugher2323b272009-02-19 10:03:23 -0800533 outw(s->state, dev->iobase + MIO_16_DIG_OUT_REG);
H Hartley Sweeten97f42892013-08-30 11:05:58 -0700534
Chris R. Baugher2323b272009-02-19 10:03:23 -0800535 data[1] = inw(dev->iobase + MIO_16_DIG_IN_REG);
536
H Hartley Sweetena2714e32012-06-18 13:16:35 -0700537 return insn->n;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800538}
539
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530540static int atmio16d_dio_insn_config(struct comedi_device *dev,
541 struct comedi_subdevice *s,
542 struct comedi_insn *insn,
543 unsigned int *data)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800544{
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700545 struct atmio16d_private *devpriv = dev->private;
H Hartley Sweetenf9f34d52013-08-26 15:29:33 -0700546 unsigned int chan = CR_CHAN(insn->chanspec);
547 unsigned int mask;
548 int ret;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800549
H Hartley Sweetenf9f34d52013-08-26 15:29:33 -0700550 if (chan < 4)
551 mask = 0x0f;
552 else
553 mask = 0xf0;
554
555 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
556 if (ret)
557 return ret;
558
Chris R. Baugher2323b272009-02-19 10:03:23 -0800559 devpriv->com_reg_2_state &= ~(COMREG2_DOUTEN0 | COMREG2_DOUTEN1);
560 if (s->io_bits & 0x0f)
561 devpriv->com_reg_2_state |= COMREG2_DOUTEN0;
562 if (s->io_bits & 0xf0)
563 devpriv->com_reg_2_state |= COMREG2_DOUTEN1;
564 outw(devpriv->com_reg_2_state, dev->iobase + COM_REG_2);
565
H Hartley Sweetenf9f34d52013-08-26 15:29:33 -0700566 return insn->n;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800567}
568
569/*
570 options[0] - I/O port
571 options[1] - MIO irq
Bruce Jonesce5ade42009-09-24 15:02:36 -0700572 0 == no irq
573 N == irq N {3,4,5,6,7,9,10,11,12,14,15}
Chris R. Baugher2323b272009-02-19 10:03:23 -0800574 options[2] - DIO irq
Bruce Jonesce5ade42009-09-24 15:02:36 -0700575 0 == no irq
576 N == irq N {3,4,5,6,7,9}
Chris R. Baugher2323b272009-02-19 10:03:23 -0800577 options[3] - DMA1 channel
Bruce Jonesce5ade42009-09-24 15:02:36 -0700578 0 == no DMA
579 N == DMA N {5,6,7}
Chris R. Baugher2323b272009-02-19 10:03:23 -0800580 options[4] - DMA2 channel
Bruce Jonesce5ade42009-09-24 15:02:36 -0700581 0 == no DMA
582 N == DMA N {5,6,7}
Chris R. Baugher2323b272009-02-19 10:03:23 -0800583
584 options[5] - a/d mux
Bruce Jonesce5ade42009-09-24 15:02:36 -0700585 0=differential, 1=single
Chris R. Baugher2323b272009-02-19 10:03:23 -0800586 options[6] - a/d range
Bruce Jonesce5ade42009-09-24 15:02:36 -0700587 0=bipolar10, 1=bipolar5, 2=unipolar10
Chris R. Baugher2323b272009-02-19 10:03:23 -0800588
589 options[7] - dac0 range
Bruce Jonesce5ade42009-09-24 15:02:36 -0700590 0=bipolar, 1=unipolar
Chris R. Baugher2323b272009-02-19 10:03:23 -0800591 options[8] - dac0 reference
Bruce Jonesce5ade42009-09-24 15:02:36 -0700592 0=internal, 1=external
Chris R. Baugher2323b272009-02-19 10:03:23 -0800593 options[9] - dac0 coding
Bruce Jonesce5ade42009-09-24 15:02:36 -0700594 0=2's comp, 1=straight binary
Chris R. Baugher2323b272009-02-19 10:03:23 -0800595
596 options[10] - dac1 range
597 options[11] - dac1 reference
598 options[12] - dac1 coding
599 */
600
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530601static int atmio16d_attach(struct comedi_device *dev,
602 struct comedi_devconfig *it)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800603{
Ian Abbott967b3eb2014-09-09 11:26:40 +0100604 const struct atmio16_board_t *board = dev->board_ptr;
H Hartley Sweeten9a1a6cf2012-10-15 10:15:52 -0700605 struct atmio16d_private *devpriv;
H Hartley Sweeten2028bdc2013-04-09 16:25:48 -0700606 struct comedi_subdevice *s;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800607 int ret;
608
H Hartley Sweeten862755e2014-07-18 17:01:22 -0700609 ret = comedi_request_region(dev, it->options[0], 0x20);
H Hartley Sweeten2028bdc2013-04-09 16:25:48 -0700610 if (ret)
611 return ret;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800612
H Hartley Sweeten2f0b9d02012-06-11 17:45:15 -0700613 ret = comedi_alloc_subdevices(dev, 4);
H Hartley Sweeten8b6c5692012-06-12 11:59:33 -0700614 if (ret)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800615 return ret;
Bill Pembertonc3744132009-04-22 21:11:47 -0400616
H Hartley Sweeten0bdab502013-06-24 16:55:44 -0700617 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
H Hartley Sweetenc34fa262012-10-23 13:22:37 -0700618 if (!devpriv)
619 return -ENOMEM;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800620
621 /* reset the atmio16d hardware */
622 reset_atmio16d(dev);
623
H Hartley Sweetenec9b2f42013-11-26 16:42:30 -0700624 if (it->options[1]) {
625 ret = request_irq(it->options[1], atmio16d_interrupt, 0,
626 dev->board_name, dev);
627 if (ret == 0)
628 dev->irq = it->options[1];
Chris R. Baugher2323b272009-02-19 10:03:23 -0800629 }
630
631 /* set device options */
632 devpriv->adc_mux = it->options[5];
633 devpriv->adc_range = it->options[6];
634
635 devpriv->dac0_range = it->options[7];
636 devpriv->dac0_reference = it->options[8];
637 devpriv->dac0_coding = it->options[9];
638 devpriv->dac1_range = it->options[10];
639 devpriv->dac1_reference = it->options[11];
640 devpriv->dac1_coding = it->options[12];
641
642 /* setup sub-devices */
H Hartley Sweeten3f507ce2012-09-05 18:48:58 -0700643 s = &dev->subdevices[0];
Chris R. Baugher2323b272009-02-19 10:03:23 -0800644 /* ai subdevice */
645 s->type = COMEDI_SUBD_AI;
H Hartley Sweetenec9b2f42013-11-26 16:42:30 -0700646 s->subdev_flags = SDF_READABLE | SDF_GROUND;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800647 s->n_chan = (devpriv->adc_mux ? 16 : 8);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800648 s->insn_read = atmio16d_ai_insn_read;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800649 s->maxdata = 0xfff; /* 4095 decimal */
650 switch (devpriv->adc_range) {
651 case adc_bipolar10:
652 s->range_table = &range_atmio16d_ai_10_bipolar;
653 break;
654 case adc_bipolar5:
655 s->range_table = &range_atmio16d_ai_5_bipolar;
656 break;
657 case adc_unipolar10:
658 s->range_table = &range_atmio16d_ai_unipolar;
659 break;
660 }
H Hartley Sweetenec9b2f42013-11-26 16:42:30 -0700661 if (dev->irq) {
662 dev->read_subdev = s;
663 s->subdev_flags |= SDF_CMD_READ;
664 s->len_chanlist = 16;
665 s->do_cmdtest = atmio16d_ai_cmdtest;
666 s->do_cmd = atmio16d_ai_cmd;
667 s->cancel = atmio16d_ai_cancel;
668 }
Chris R. Baugher2323b272009-02-19 10:03:23 -0800669
670 /* ao subdevice */
H Hartley Sweeten3f507ce2012-09-05 18:48:58 -0700671 s = &dev->subdevices[1];
Chris R. Baugher2323b272009-02-19 10:03:23 -0800672 s->type = COMEDI_SUBD_AO;
673 s->subdev_flags = SDF_WRITABLE;
674 s->n_chan = 2;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800675 s->maxdata = 0xfff; /* 4095 decimal */
676 s->range_table_list = devpriv->ao_range_type_list;
677 switch (devpriv->dac0_range) {
678 case dac_bipolar:
679 devpriv->ao_range_type_list[0] = &range_bipolar10;
680 break;
681 case dac_unipolar:
682 devpriv->ao_range_type_list[0] = &range_unipolar10;
683 break;
684 }
685 switch (devpriv->dac1_range) {
686 case dac_bipolar:
687 devpriv->ao_range_type_list[1] = &range_bipolar10;
688 break;
689 case dac_unipolar:
690 devpriv->ao_range_type_list[1] = &range_unipolar10;
691 break;
692 }
H Hartley Sweetene8928752014-08-25 16:04:27 -0700693 s->insn_write = atmio16d_ao_insn_write;
H Hartley Sweetene8928752014-08-25 16:04:27 -0700694
695 ret = comedi_alloc_subdev_readback(s);
696 if (ret)
697 return ret;
Chris R. Baugher2323b272009-02-19 10:03:23 -0800698
699 /* Digital I/O */
H Hartley Sweeten3f507ce2012-09-05 18:48:58 -0700700 s = &dev->subdevices[2];
Chris R. Baugher2323b272009-02-19 10:03:23 -0800701 s->type = COMEDI_SUBD_DIO;
702 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
703 s->n_chan = 8;
704 s->insn_bits = atmio16d_dio_insn_bits;
705 s->insn_config = atmio16d_dio_insn_config;
706 s->maxdata = 1;
707 s->range_table = &range_digital;
708
709 /* 8255 subdevice */
H Hartley Sweeten3f507ce2012-09-05 18:48:58 -0700710 s = &dev->subdevices[3];
H Hartley Sweetene6439a42014-02-03 10:43:25 -0700711 if (board->has_8255) {
H Hartley Sweeten4085e932014-08-12 11:41:26 -0700712 ret = subdev_8255_init(dev, s, NULL, 0x00);
H Hartley Sweetene6439a42014-02-03 10:43:25 -0700713 if (ret)
714 return ret;
715 } else {
Chris R. Baugher2323b272009-02-19 10:03:23 -0800716 s->type = COMEDI_SUBD_UNUSED;
H Hartley Sweetene6439a42014-02-03 10:43:25 -0700717 }
Chris R. Baugher2323b272009-02-19 10:03:23 -0800718
719/* don't yet know how to deal with counter/timers */
720#if 0
H Hartley Sweeten3f507ce2012-09-05 18:48:58 -0700721 s = &dev->subdevices[4];
Chris R. Baugher2323b272009-02-19 10:03:23 -0800722 /* do */
723 s->type = COMEDI_SUBD_TIMER;
724 s->n_chan = 0;
725 s->maxdata = 0
726#endif
Chris R. Baugher2323b272009-02-19 10:03:23 -0800727
728 return 0;
729}
730
H Hartley Sweeten484ecc92012-05-17 17:11:14 -0700731static void atmio16d_detach(struct comedi_device *dev)
Chris R. Baugher2323b272009-02-19 10:03:23 -0800732{
Chris R. Baugher2323b272009-02-19 10:03:23 -0800733 reset_atmio16d(dev);
H Hartley Sweetena32c6d02013-04-18 14:34:19 -0700734 comedi_legacy_detach(dev);
Chris R. Baugher2323b272009-02-19 10:03:23 -0800735}
Arun Thomas90f703d2010-06-06 22:23:29 +0200736
H Hartley Sweetenfbf0e452012-05-15 17:28:53 -0700737static const struct atmio16_board_t atmio16_boards[] = {
738 {
739 .name = "atmio16",
740 .has_8255 = 0,
741 }, {
742 .name = "atmio16d",
743 .has_8255 = 1,
744 },
745};
746
747static struct comedi_driver atmio16d_driver = {
748 .driver_name = "atmio16",
749 .module = THIS_MODULE,
750 .attach = atmio16d_attach,
751 .detach = atmio16d_detach,
752 .board_name = &atmio16_boards[0].name,
753 .num_names = ARRAY_SIZE(atmio16_boards),
754 .offset = sizeof(struct atmio16_board_t),
755};
756module_comedi_driver(atmio16d_driver);
757
Arun Thomas90f703d2010-06-06 22:23:29 +0200758MODULE_AUTHOR("Comedi http://www.comedi.org");
759MODULE_DESCRIPTION("Comedi low-level driver");
760MODULE_LICENSE("GPL");