staging: comedi: rtd520: sample types are unsigned
[linux-2.6-block.git] / drivers / staging / comedi / drivers / s626.c
CommitLineData
11e865c1 1/*
7f32c7c4
IA
2 * comedi/drivers/s626.c
3 * Sensoray s626 Comedi driver
4 *
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 *
8 * Based on Sensoray Model 626 Linux driver Version 0.2
9 * Copyright (C) 2002-2004 Sensoray Co., Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 */
11e865c1
GP
21
22/*
7f32c7c4
IA
23 * Driver: s626
24 * Description: Sensoray 626 driver
25 * Devices: [Sensoray] 626 (s626)
26 * Authors: Gianluca Palli <gpalli@deis.unibo.it>,
27 * Updated: Fri, 15 Feb 2008 10:28:42 +0000
28 * Status: experimental
29
30 * Configuration options: not applicable, uses PCI auto config
31
32 * INSN_CONFIG instructions:
33 * analog input:
34 * none
35 *
36 * analog output:
37 * none
38 *
39 * digital channel:
40 * s626 has 3 dio subdevices (2,3 and 4) each with 16 i/o channels
41 * supported configuration options:
42 * INSN_CONFIG_DIO_QUERY
43 * COMEDI_INPUT
44 * COMEDI_OUTPUT
45 *
46 * encoder:
47 * Every channel must be configured before reading.
48 *
49 * Example code
50 *
51 * insn.insn=INSN_CONFIG; //configuration instruction
52 * insn.n=1; //number of operation (must be 1)
53 * insn.data=&initialvalue; //initial value loaded into encoder
54 * //during configuration
55 * insn.subdev=5; //encoder subdevice
56 * insn.chanspec=CR_PACK(encoder_channel,0,AREF_OTHER); //encoder_channel
57 * //to configure
58 *
59 * comedi_do_insn(cf,&insn); //executing configuration
60 */
11e865c1 61
ce157f80
HS
62#include <linux/module.h>
63#include <linux/delay.h>
33782dd5 64#include <linux/pci.h>
25436dc9 65#include <linux/interrupt.h>
11e865c1
GP
66#include <linux/kernel.h>
67#include <linux/types.h>
68
69#include "../comedidev.h"
70
11e865c1
GP
71#include "comedi_fc.h"
72#include "s626.h"
73
dbb263f5 74struct s626_buffer_dma {
8e06d662
IA
75 dma_addr_t physical_base;
76 void *logical_base;
77};
78
eb5e029e 79struct s626_private {
7d856da2 80 void __iomem *mmio;
8ee52611 81 uint8_t ai_cmd_running; /* ai_cmd is running */
e6132fc9 82 uint8_t ai_continuous; /* continuous acquisition */
8ee52611
IA
83 int ai_sample_count; /* number of samples to acquire */
84 unsigned int ai_sample_timer; /* time between samples in
85 * units of the timer */
86 int ai_convert_count; /* conversion counter */
87 unsigned int ai_convert_timer; /* time between conversion in
88 * units of the timer */
07a36d66 89 uint16_t counter_int_enabs; /* counter interrupt enable mask
8ee52611 90 * for MISC2 register */
07a36d66 91 uint8_t adc_items; /* number of items in ADC poll list */
dbb263f5 92 struct s626_buffer_dma rps_buf; /* DMA buffer used to hold ADC (RPS1)
8ee52611 93 * program */
dbb263f5 94 struct s626_buffer_dma ana_buf; /* DMA buffer used to receive ADC data
8ee52611 95 * and hold DAC data */
07a36d66 96 uint32_t *dac_wbuf; /* pointer to logical adrs of DMA buffer
8ee52611 97 * used to hold DAC data */
07a36d66
IA
98 uint16_t dacpol; /* image of DAC polarity register */
99 uint8_t trim_setpoint[12]; /* images of TrimDAC setpoints */
100 uint32_t i2c_adrs; /* I2C device address for onboard EEPROM
8ee52611 101 * (board rev dependent) */
790c5541 102 unsigned int ao_readback[S626_DAC_CHANNELS];
eb5e029e 103};
11e865c1 104
8ee52611 105/* COUNTER OBJECT ------------------------------------------------ */
3a305a66 106struct s626_enc_info {
8ee52611
IA
107 /* Pointers to functions that differ for A and B counters: */
108 /* Return clock enable. */
3a305a66
IA
109 uint16_t(*get_enable)(struct comedi_device *dev,
110 const struct s626_enc_info *k);
8ee52611 111 /* Return interrupt source. */
b075ac8e 112 uint16_t(*get_int_src)(struct comedi_device *dev,
3a305a66 113 const struct s626_enc_info *k);
8ee52611 114 /* Return preload trigger source. */
b075ac8e 115 uint16_t(*get_load_trig)(struct comedi_device *dev,
3a305a66 116 const struct s626_enc_info *k);
8ee52611 117 /* Return standardized operating mode. */
3a305a66
IA
118 uint16_t(*get_mode)(struct comedi_device *dev,
119 const struct s626_enc_info *k);
8ee52611 120 /* Generate soft index strobe. */
3a305a66
IA
121 void (*pulse_index)(struct comedi_device *dev,
122 const struct s626_enc_info *k);
8ee52611 123 /* Program clock enable. */
3a305a66
IA
124 void (*set_enable)(struct comedi_device *dev,
125 const struct s626_enc_info *k, uint16_t enab);
8ee52611 126 /* Program interrupt source. */
3a305a66
IA
127 void (*set_int_src)(struct comedi_device *dev,
128 const struct s626_enc_info *k, uint16_t int_source);
8ee52611 129 /* Program preload trigger source. */
3a305a66
IA
130 void (*set_load_trig)(struct comedi_device *dev,
131 const struct s626_enc_info *k, uint16_t trig);
8ee52611 132 /* Program standardized operating mode. */
3a305a66
IA
133 void (*set_mode)(struct comedi_device *dev,
134 const struct s626_enc_info *k, uint16_t setup,
135 uint16_t disable_int_src);
8ee52611 136 /* Reset event capture flags. */
b075ac8e 137 void (*reset_cap_flags)(struct comedi_device *dev,
3a305a66 138 const struct s626_enc_info *k);
8ee52611 139
b075ac8e
IA
140 uint16_t my_cra; /* address of CRA register */
141 uint16_t my_crb; /* address of CRB register */
142 uint16_t my_latch_lsw; /* address of Latch least-significant-word
8ee52611 143 * register */
b075ac8e 144 uint16_t my_event_bits[4]; /* bit translations for IntSrc -->RDMISC2 */
eb5e029e 145};
11e865c1 146
8ee52611 147/* Counter overflow/index event flag masks for RDMISC2. */
676921c9
IA
148#define S626_INDXMASK(C) (1 << (((C) > 2) ? ((C) * 2 - 1) : ((C) * 2 + 4)))
149#define S626_OVERMASK(C) (1 << (((C) > 2) ? ((C) * 2 + 5) : ((C) * 2 + 10)))
150#define S626_EVBITS(C) { 0, S626_OVERMASK(C), S626_INDXMASK(C), \
151 S626_OVERMASK(C) | S626_INDXMASK(C) }
11e865c1 152
8ee52611
IA
153/*
154 * Translation table to map IntSrc into equivalent RDMISC2 event flag bits.
31de1948 155 * static const uint16_t s626_event_bits[][4] =
676921c9
IA
156 * { S626_EVBITS(0), S626_EVBITS(1), S626_EVBITS(2), S626_EVBITS(3),
157 * S626_EVBITS(4), S626_EVBITS(5) };
8ee52611 158 */
11e865c1 159
ddd9813e
HS
160/*
161 * Enable/disable a function or test status bit(s) that are accessed
162 * through Main Control Registers 1 or 2.
163 */
164static void s626_mc_enable(struct comedi_device *dev,
165 unsigned int cmd, unsigned int reg)
166{
167 struct s626_private *devpriv = dev->private;
168 unsigned int val = (cmd << 16) | cmd;
169
bb49cddc 170 mmiowb();
7d856da2 171 writel(val, devpriv->mmio + reg);
ddd9813e 172}
11e865c1 173
c5cf4606
HS
174static void s626_mc_disable(struct comedi_device *dev,
175 unsigned int cmd, unsigned int reg)
176{
177 struct s626_private *devpriv = dev->private;
178
7d856da2 179 writel(cmd << 16 , devpriv->mmio + reg);
bb49cddc 180 mmiowb();
c5cf4606 181}
11e865c1 182
95bb7982
HS
183static bool s626_mc_test(struct comedi_device *dev,
184 unsigned int cmd, unsigned int reg)
185{
186 struct s626_private *devpriv = dev->private;
187 unsigned int val;
188
7d856da2 189 val = readl(devpriv->mmio + reg);
95bb7982
HS
190
191 return (val & cmd) ? true : false;
192}
11e865c1 193
676921c9 194#define S626_BUGFIX_STREG(REGADRS) ((REGADRS) - 4)
11e865c1 195
8ee52611 196/* Write a time slot control record to TSL2. */
d8515652 197#define S626_VECTPORT(VECTNUM) (S626_P_TSL2 + ((VECTNUM) << 2))
11e865c1 198
90d54ff2
HS
199static const struct comedi_lrange s626_range_table = {
200 2, {
201 BIP_RANGE(5),
202 BIP_RANGE(10),
203 }
11e865c1
GP
204};
205
8ee52611
IA
206/*
207 * Execute a DEBI transfer. This must be called from within a critical section.
208 */
31de1948 209static void s626_debi_transfer(struct comedi_device *dev)
6b387b70 210{
7f2f7e05
HS
211 struct s626_private *devpriv = dev->private;
212
ddd9813e 213 /* Initiate upload of shadow RAM to DEBI control register */
d8515652 214 s626_mc_enable(dev, S626_MC2_UPLD_DEBI, S626_P_MC2);
6b387b70 215
95bb7982
HS
216 /*
217 * Wait for completion of upload from shadow RAM to
218 * DEBI control register.
219 */
d8515652 220 while (!s626_mc_test(dev, S626_MC2_UPLD_DEBI, S626_P_MC2))
6b387b70
HS
221 ;
222
be008602 223 /* Wait until DEBI transfer is done */
d8515652 224 while (readl(devpriv->mmio + S626_P_PSR) & S626_PSR_DEBI_S)
6b387b70
HS
225 ;
226}
227
8ee52611
IA
228/*
229 * Read a value from a gate array register.
230 */
31de1948 231static uint16_t s626_debi_read(struct comedi_device *dev, uint16_t addr)
6b387b70 232{
7f2f7e05 233 struct s626_private *devpriv = dev->private;
6b387b70 234
25f8fd5e 235 /* Set up DEBI control register value in shadow RAM */
d8515652 236 writel(S626_DEBI_CMD_RDWORD | addr, devpriv->mmio + S626_P_DEBICMD);
6b387b70
HS
237
238 /* Execute the DEBI transfer. */
31de1948 239 s626_debi_transfer(dev);
6b387b70 240
d8515652 241 return readl(devpriv->mmio + S626_P_DEBIAD);
6b387b70
HS
242}
243
8ee52611
IA
244/*
245 * Write a value to a gate array register.
246 */
31de1948
IA
247static void s626_debi_write(struct comedi_device *dev, uint16_t addr,
248 uint16_t wdata)
6b387b70 249{
7f2f7e05 250 struct s626_private *devpriv = dev->private;
6b387b70 251
25f8fd5e 252 /* Set up DEBI control register value in shadow RAM */
d8515652
IA
253 writel(S626_DEBI_CMD_WRWORD | addr, devpriv->mmio + S626_P_DEBICMD);
254 writel(wdata, devpriv->mmio + S626_P_DEBIAD);
6b387b70
HS
255
256 /* Execute the DEBI transfer. */
31de1948 257 s626_debi_transfer(dev);
6b387b70
HS
258}
259
8ee52611
IA
260/*
261 * Replace the specified bits in a gate array register. Imports: mask
6b387b70
HS
262 * specifies bits that are to be preserved, wdata is new value to be
263 * or'd with the masked original.
264 */
31de1948
IA
265static void s626_debi_replace(struct comedi_device *dev, unsigned int addr,
266 unsigned int mask, unsigned int wdata)
6b387b70 267{
7f2f7e05 268 struct s626_private *devpriv = dev->private;
be008602 269 unsigned int val;
6b387b70 270
12f4e2f2 271 addr &= 0xffff;
d8515652 272 writel(S626_DEBI_CMD_RDWORD | addr, devpriv->mmio + S626_P_DEBICMD);
31de1948 273 s626_debi_transfer(dev);
6b387b70 274
d8515652
IA
275 writel(S626_DEBI_CMD_WRWORD | addr, devpriv->mmio + S626_P_DEBICMD);
276 val = readl(devpriv->mmio + S626_P_DEBIAD);
be008602
HS
277 val &= mask;
278 val |= wdata;
d8515652 279 writel(val & 0xffff, devpriv->mmio + S626_P_DEBIAD);
31de1948 280 s626_debi_transfer(dev);
6b387b70
HS
281}
282
982e3d11
HS
283/* ************** EEPROM ACCESS FUNCTIONS ************** */
284
31de1948 285static uint32_t s626_i2c_handshake(struct comedi_device *dev, uint32_t val)
982e3d11 286{
7f2f7e05 287 struct s626_private *devpriv = dev->private;
be008602 288 unsigned int ctrl;
7f2f7e05 289
25f8fd5e 290 /* Write I2C command to I2C Transfer Control shadow register */
d8515652 291 writel(val, devpriv->mmio + S626_P_I2CCTRL);
982e3d11 292
ddd9813e
HS
293 /*
294 * Upload I2C shadow registers into working registers and
295 * wait for upload confirmation.
296 */
d8515652
IA
297 s626_mc_enable(dev, S626_MC2_UPLD_IIC, S626_P_MC2);
298 while (!s626_mc_test(dev, S626_MC2_UPLD_IIC, S626_P_MC2))
982e3d11
HS
299 ;
300
be008602
HS
301 /* Wait until I2C bus transfer is finished or an error occurs */
302 do {
d8515652
IA
303 ctrl = readl(devpriv->mmio + S626_P_I2CCTRL);
304 } while ((ctrl & (S626_I2C_BUSY | S626_I2C_ERR)) == S626_I2C_BUSY);
982e3d11 305
be008602 306 /* Return non-zero if I2C error occurred */
d8515652 307 return ctrl & S626_I2C_ERR;
982e3d11
HS
308}
309
8ee52611 310/* Read uint8_t from EEPROM. */
31de1948 311static uint8_t s626_i2c_read(struct comedi_device *dev, uint8_t addr)
982e3d11 312{
7f2f7e05 313 struct s626_private *devpriv = dev->private;
982e3d11 314
8ee52611
IA
315 /*
316 * Send EEPROM target address:
317 * Byte2 = I2C command: write to I2C EEPROM device.
318 * Byte1 = EEPROM internal target address.
319 * Byte0 = Not sent.
320 */
d8515652
IA
321 if (s626_i2c_handshake(dev, S626_I2C_B2(S626_I2C_ATTRSTART,
322 devpriv->i2c_adrs) |
323 S626_I2C_B1(S626_I2C_ATTRSTOP, addr) |
324 S626_I2C_B0(S626_I2C_ATTRNOP, 0)))
8ee52611 325 /* Abort function and declare error if handshake failed. */
982e3d11 326 return 0;
982e3d11 327
8ee52611
IA
328 /*
329 * Execute EEPROM read:
330 * Byte2 = I2C command: read from I2C EEPROM device.
331 * Byte1 receives uint8_t from EEPROM.
332 * Byte0 = Not sent.
333 */
d8515652 334 if (s626_i2c_handshake(dev, S626_I2C_B2(S626_I2C_ATTRSTART,
31de1948 335 (devpriv->i2c_adrs | 1)) |
d8515652
IA
336 S626_I2C_B1(S626_I2C_ATTRSTOP, 0) |
337 S626_I2C_B0(S626_I2C_ATTRNOP, 0)))
8ee52611 338 /* Abort function and declare error if handshake failed. */
982e3d11 339 return 0;
be008602 340
d8515652 341 return (readl(devpriv->mmio + S626_P_I2CCTRL) >> 16) & 0xff;
982e3d11
HS
342}
343
95414729
HS
344/* *********** DAC FUNCTIONS *********** */
345
8ee52611 346/* TrimDac LogicalChan-to-PhysicalChan mapping table. */
31de1948 347static const uint8_t s626_trimchan[] = { 10, 9, 8, 3, 2, 7, 6, 1, 0, 5, 4 };
95414729 348
8ee52611 349/* TrimDac LogicalChan-to-EepromAdrs mapping table. */
31de1948 350static const uint8_t s626_trimadrs[] = {
8ee52611
IA
351 0x40, 0x41, 0x42, 0x50, 0x51, 0x52, 0x53, 0x60, 0x61, 0x62, 0x63
352};
95414729 353
8ee52611
IA
354/*
355 * Private helper function: Transmit serial data to DAC via Audio
95414729 356 * channel 2. Assumes: (1) TSL2 slot records initialized, and (2)
07a36d66 357 * dacpol contains valid target image.
95414729 358 */
31de1948 359static void s626_send_dac(struct comedi_device *dev, uint32_t val)
95414729 360{
7f2f7e05 361 struct s626_private *devpriv = dev->private;
95414729
HS
362
363 /* START THE SERIAL CLOCK RUNNING ------------- */
364
8ee52611
IA
365 /*
366 * Assert DAC polarity control and enable gating of DAC serial clock
95414729
HS
367 * and audio bit stream signals. At this point in time we must be
368 * assured of being in time slot 0. If we are not in slot 0, the
369 * serial clock and audio stream signals will be disabled; this is
31de1948
IA
370 * because the following s626_debi_write statement (which enables
371 * signals to be passed through the gate array) would execute before
372 * the trailing edge of WS1/WS3 (which turns off the signals), thus
95414729
HS
373 * causing the signals to be inactive during the DAC write.
374 */
d8515652 375 s626_debi_write(dev, S626_LP_DACPOL, devpriv->dacpol);
95414729
HS
376
377 /* TRANSFER OUTPUT DWORD VALUE INTO A2'S OUTPUT FIFO ---------------- */
378
379 /* Copy DAC setpoint value to DAC's output DMA buffer. */
07a36d66
IA
380 /* writel(val, devpriv->mmio + (uint32_t)devpriv->dac_wbuf); */
381 *devpriv->dac_wbuf = val;
95414729 382
ddd9813e
HS
383 /*
384 * Enable the output DMA transfer. This will cause the DMAC to copy
385 * the DAC's data value to A2's output FIFO. The DMA transfer will
95414729
HS
386 * then immediately terminate because the protection address is
387 * reached upon transfer of the first DWORD value.
388 */
d8515652 389 s626_mc_enable(dev, S626_MC1_A2OUT, S626_P_MC1);
95414729 390
8ee52611 391 /* While the DMA transfer is executing ... */
95414729 392
25f8fd5e
HS
393 /*
394 * Reset Audio2 output FIFO's underflow flag (along with any
395 * other FIFO underflow/overflow flags). When set, this flag
396 * will indicate that we have emerged from slot 0.
95414729 397 */
d8515652 398 writel(S626_ISR_AFOU, devpriv->mmio + S626_P_ISR);
95414729 399
8ee52611
IA
400 /*
401 * Wait for the DMA transfer to finish so that there will be data
95414729
HS
402 * available in the FIFO when time slot 1 tries to transfer a DWORD
403 * from the FIFO to the output buffer register. We test for DMA
404 * Done by polling the DMAC enable flag; this flag is automatically
405 * cleared when the transfer has finished.
406 */
d8515652 407 while (readl(devpriv->mmio + S626_P_MC1) & S626_MC1_A2OUT)
95414729
HS
408 ;
409
410 /* START THE OUTPUT STREAM TO THE TARGET DAC -------------------- */
411
8ee52611
IA
412 /*
413 * FIFO data is now available, so we enable execution of time slots
95414729
HS
414 * 1 and higher by clearing the EOS flag in slot 0. Note that SD3
415 * will be shifted in and stored in FB_BUFFER2 for end-of-slot-list
416 * detection.
417 */
d8515652
IA
418 writel(S626_XSD2 | S626_RSD3 | S626_SIB_A2,
419 devpriv->mmio + S626_VECTPORT(0));
95414729 420
8ee52611
IA
421 /*
422 * Wait for slot 1 to execute to ensure that the Packet will be
95414729
HS
423 * transmitted. This is detected by polling the Audio2 output FIFO
424 * underflow flag, which will be set when slot 1 execution has
425 * finished transferring the DAC's data DWORD from the output FIFO
426 * to the output buffer register.
427 */
d8515652 428 while (!(readl(devpriv->mmio + S626_P_SSR) & S626_SSR_AF2_OUT))
95414729
HS
429 ;
430
8ee52611
IA
431 /*
432 * Set up to trap execution at slot 0 when the TSL sequencer cycles
95414729
HS
433 * back to slot 0 after executing the EOS in slot 5. Also,
434 * simultaneously shift out and in the 0x00 that is ALWAYS the value
435 * stored in the last byte to be shifted out of the FIFO's DWORD
436 * buffer register.
437 */
d8515652 438 writel(S626_XSD2 | S626_XFIFO_2 | S626_RSD2 | S626_SIB_A2 | S626_EOS,
676921c9 439 devpriv->mmio + S626_VECTPORT(0));
95414729
HS
440
441 /* WAIT FOR THE TRANSACTION TO FINISH ----------------------- */
442
8ee52611
IA
443 /*
444 * Wait for the TSL to finish executing all time slots before
95414729
HS
445 * exiting this function. We must do this so that the next DAC
446 * write doesn't start, thereby enabling clock/chip select signals:
447 *
448 * 1. Before the TSL sequence cycles back to slot 0, which disables
449 * the clock/cs signal gating and traps slot // list execution.
450 * we have not yet finished slot 5 then the clock/cs signals are
451 * still gated and we have not finished transmitting the stream.
452 *
453 * 2. While slots 2-5 are executing due to a late slot 0 trap. In
454 * this case, the slot sequence is currently repeating, but with
455 * clock/cs signals disabled. We must wait for slot 0 to trap
456 * execution before setting up the next DAC setpoint DMA transfer
457 * and enabling the clock/cs signals. To detect the end of slot 5,
458 * we test for the FB_BUFFER2 MSB contents to be equal to 0xFF. If
459 * the TSL has not yet finished executing slot 5 ...
460 */
d8515652 461 if (readl(devpriv->mmio + S626_P_FB_BUFFER2) & 0xff000000) {
8ee52611
IA
462 /*
463 * The trap was set on time and we are still executing somewhere
95414729
HS
464 * in slots 2-5, so we now wait for slot 0 to execute and trap
465 * TSL execution. This is detected when FB_BUFFER2 MSB changes
466 * from 0xFF to 0x00, which slot 0 causes to happen by shifting
467 * out/in on SD2 the 0x00 that is always referenced by slot 5.
468 */
d8515652 469 while (readl(devpriv->mmio + S626_P_FB_BUFFER2) & 0xff000000)
95414729
HS
470 ;
471 }
8ee52611
IA
472 /*
473 * Either (1) we were too late setting the slot 0 trap; the TSL
95414729
HS
474 * sequencer restarted slot 0 before we could set the EOS trap flag,
475 * or (2) we were not late and execution is now trapped at slot 0.
476 * In either case, we must now change slot 0 so that it will store
477 * value 0xFF (instead of 0x00) to FB_BUFFER2 next time it executes.
478 * In order to do this, we reprogram slot 0 so that it will shift in
479 * SD3, which is driven only by a pull-up resistor.
480 */
d8515652
IA
481 writel(S626_RSD3 | S626_SIB_A2 | S626_EOS,
482 devpriv->mmio + S626_VECTPORT(0));
95414729 483
8ee52611
IA
484 /*
485 * Wait for slot 0 to execute, at which time the TSL is setup for
95414729
HS
486 * the next DAC write. This is detected when FB_BUFFER2 MSB changes
487 * from 0x00 to 0xFF.
488 */
d8515652 489 while (!(readl(devpriv->mmio + S626_P_FB_BUFFER2) & 0xff000000))
95414729
HS
490 ;
491}
492
8ee52611
IA
493/*
494 * Private helper function: Write setpoint to an application DAC channel.
495 */
31de1948
IA
496static void s626_set_dac(struct comedi_device *dev, uint16_t chan,
497 short dacdata)
95414729 498{
7f2f7e05 499 struct s626_private *devpriv = dev->private;
8ee52611 500 uint16_t signmask;
f1f7efce 501 uint32_t ws_image;
8ee52611 502 uint32_t val;
95414729 503
8ee52611
IA
504 /*
505 * Adjust DAC data polarity and set up Polarity Control Register image.
506 */
95414729
HS
507 signmask = 1 << chan;
508 if (dacdata < 0) {
509 dacdata = -dacdata;
07a36d66 510 devpriv->dacpol |= signmask;
8ee52611 511 } else {
07a36d66 512 devpriv->dacpol &= ~signmask;
8ee52611 513 }
95414729 514
8ee52611
IA
515 /* Limit DAC setpoint value to valid range. */
516 if ((uint16_t)dacdata > 0x1FFF)
95414729
HS
517 dacdata = 0x1FFF;
518
8ee52611
IA
519 /*
520 * Set up TSL2 records (aka "vectors") for DAC update. Vectors V2
95414729
HS
521 * and V3 transmit the setpoint to the target DAC. V4 and V5 send
522 * data to a non-existent TrimDac channel just to keep the clock
523 * running after sending data to the target DAC. This is necessary
524 * to eliminate the clock glitch that would otherwise occur at the
525 * end of the target DAC's serial data stream. When the sequence
526 * restarts at V0 (after executing V5), the gate array automatically
527 * disables gating for the DAC clock and all DAC chip selects.
528 */
529
25f8fd5e 530 /* Choose DAC chip select to be asserted */
d8515652 531 ws_image = (chan & 2) ? S626_WS1 : S626_WS2;
25f8fd5e 532 /* Slot 2: Transmit high data byte to target DAC */
d8515652
IA
533 writel(S626_XSD2 | S626_XFIFO_1 | ws_image,
534 devpriv->mmio + S626_VECTPORT(2));
25f8fd5e 535 /* Slot 3: Transmit low data byte to target DAC */
d8515652
IA
536 writel(S626_XSD2 | S626_XFIFO_0 | ws_image,
537 devpriv->mmio + S626_VECTPORT(3));
95414729 538 /* Slot 4: Transmit to non-existent TrimDac channel to keep clock */
d8515652
IA
539 writel(S626_XSD2 | S626_XFIFO_3 | S626_WS3,
540 devpriv->mmio + S626_VECTPORT(4));
25f8fd5e 541 /* Slot 5: running after writing target DAC's low data byte */
d8515652
IA
542 writel(S626_XSD2 | S626_XFIFO_2 | S626_WS3 | S626_EOS,
543 devpriv->mmio + S626_VECTPORT(5));
95414729 544
8ee52611
IA
545 /*
546 * Construct and transmit target DAC's serial packet:
547 * (A10D DDDD), (DDDD DDDD), (0x0F), (0x00) where A is chan<0>,
95414729
HS
548 * and D<12:0> is the DAC setpoint. Append a WORD value (that writes
549 * to a non-existent TrimDac channel) that serves to keep the clock
550 * running after the packet has been sent to the target DAC.
551 */
8ee52611
IA
552 val = 0x0F000000; /* Continue clock after target DAC data
553 * (write to non-existent trimdac). */
554 val |= 0x00004000; /* Address the two main dual-DAC devices
555 * (TSL's chip select enables target device). */
556 val |= ((uint32_t)(chan & 1) << 15); /* Address the DAC channel
557 * within the device. */
558 val |= (uint32_t)dacdata; /* Include DAC setpoint data. */
31de1948 559 s626_send_dac(dev, val);
95414729
HS
560}
561
31de1948
IA
562static void s626_write_trim_dac(struct comedi_device *dev, uint8_t logical_chan,
563 uint8_t dac_data)
95414729 564{
7f2f7e05 565 struct s626_private *devpriv = dev->private;
95414729
HS
566 uint32_t chan;
567
8ee52611
IA
568 /*
569 * Save the new setpoint in case the application needs to read it back
570 * later.
571 */
f1f7efce 572 devpriv->trim_setpoint[logical_chan] = (uint8_t)dac_data;
95414729 573
8ee52611 574 /* Map logical channel number to physical channel number. */
31de1948 575 chan = s626_trimchan[logical_chan];
95414729 576
8ee52611
IA
577 /*
578 * Set up TSL2 records for TrimDac write operation. All slots shift
95414729
HS
579 * 0xFF in from pulled-up SD3 so that the end of the slot sequence
580 * can be detected.
581 */
582
25f8fd5e 583 /* Slot 2: Send high uint8_t to target TrimDac */
d8515652
IA
584 writel(S626_XSD2 | S626_XFIFO_1 | S626_WS3,
585 devpriv->mmio + S626_VECTPORT(2));
25f8fd5e 586 /* Slot 3: Send low uint8_t to target TrimDac */
d8515652
IA
587 writel(S626_XSD2 | S626_XFIFO_0 | S626_WS3,
588 devpriv->mmio + S626_VECTPORT(3));
25f8fd5e 589 /* Slot 4: Send NOP high uint8_t to DAC0 to keep clock running */
d8515652
IA
590 writel(S626_XSD2 | S626_XFIFO_3 | S626_WS1,
591 devpriv->mmio + S626_VECTPORT(4));
25f8fd5e 592 /* Slot 5: Send NOP low uint8_t to DAC0 */
d8515652
IA
593 writel(S626_XSD2 | S626_XFIFO_2 | S626_WS1 | S626_EOS,
594 devpriv->mmio + S626_VECTPORT(5));
95414729 595
8ee52611
IA
596 /*
597 * Construct and transmit target DAC's serial packet:
598 * (0000 AAAA), (DDDD DDDD), (0x00), (0x00) where A<3:0> is the
95414729
HS
599 * DAC channel's address, and D<7:0> is the DAC setpoint. Append a
600 * WORD value (that writes a channel 0 NOP command to a non-existent
601 * main DAC channel) that serves to keep the clock running after the
602 * packet has been sent to the target DAC.
603 */
604
8ee52611
IA
605 /*
606 * Address the DAC channel within the trimdac device.
607 * Include DAC setpoint data.
608 */
31de1948 609 s626_send_dac(dev, (chan << 8) | dac_data);
95414729
HS
610}
611
31de1948 612static void s626_load_trim_dacs(struct comedi_device *dev)
95414729 613{
8ee52611 614 uint8_t i;
95414729 615
8ee52611 616 /* Copy TrimDac setpoint values from EEPROM to TrimDacs. */
31de1948
IA
617 for (i = 0; i < ARRAY_SIZE(s626_trimchan); i++)
618 s626_write_trim_dac(dev, i,
619 s626_i2c_read(dev, s626_trimadrs[i]));
95414729
HS
620}
621
e3eb08d0 622/* ****** COUNTER FUNCTIONS ******* */
8ee52611
IA
623
624/*
625 * All counter functions address a specific counter by means of the
e3eb08d0
HS
626 * "Counter" argument, which is a logical counter number. The Counter
627 * argument may have any of the following legal values: 0=0A, 1=1A,
628 * 2=2A, 3=0B, 4=1B, 5=2B.
629 */
630
8ee52611
IA
631/*
632 * Read a counter's output latch.
633 */
31de1948
IA
634static uint32_t s626_read_latch(struct comedi_device *dev,
635 const struct s626_enc_info *k)
e3eb08d0 636{
8ee52611 637 uint32_t value;
e3eb08d0 638
8ee52611 639 /* Latch counts and fetch LSW of latched counts value. */
31de1948 640 value = s626_debi_read(dev, k->my_latch_lsw);
e3eb08d0 641
8ee52611 642 /* Fetch MSW of latched counts and combine with LSW. */
31de1948 643 value |= ((uint32_t)s626_debi_read(dev, k->my_latch_lsw + 2) << 16);
e3eb08d0 644
8ee52611 645 /* Return latched counts. */
e3eb08d0
HS
646 return value;
647}
648
8ee52611
IA
649/*
650 * Return/set a counter pair's latch trigger source. 0: On read
e3eb08d0
HS
651 * access, 1: A index latches A, 2: B index latches B, 3: A overflow
652 * latches B.
653 */
31de1948
IA
654static void s626_set_latch_source(struct comedi_device *dev,
655 const struct s626_enc_info *k, uint16_t value)
e3eb08d0 656{
d8515652
IA
657 s626_debi_replace(dev, k->my_crb,
658 ~(S626_CRBMSK_INTCTRL | S626_CRBMSK_LATCHSRC),
659 value << S626_CRBBIT_LATCHSRC);
e3eb08d0
HS
660}
661
8ee52611
IA
662/*
663 * Write value into counter preload register.
664 */
31de1948
IA
665static void s626_preload(struct comedi_device *dev,
666 const struct s626_enc_info *k, uint32_t value)
e3eb08d0 667{
31de1948
IA
668 s626_debi_write(dev, k->my_latch_lsw, value);
669 s626_debi_write(dev, k->my_latch_lsw + 2, value >> 16);
e3eb08d0
HS
670}
671
010be96f
IA
672/* ****** PRIVATE COUNTER FUNCTIONS ****** */
673
674/*
675 * Reset a counter's index and overflow event capture flags.
676 */
31de1948
IA
677static void s626_reset_cap_flags_a(struct comedi_device *dev,
678 const struct s626_enc_info *k)
010be96f 679{
d8515652
IA
680 s626_debi_replace(dev, k->my_crb, ~S626_CRBMSK_INTCTRL,
681 S626_CRBMSK_INTRESETCMD | S626_CRBMSK_INTRESET_A);
010be96f
IA
682}
683
31de1948
IA
684static void s626_reset_cap_flags_b(struct comedi_device *dev,
685 const struct s626_enc_info *k)
010be96f 686{
d8515652
IA
687 s626_debi_replace(dev, k->my_crb, ~S626_CRBMSK_INTCTRL,
688 S626_CRBMSK_INTRESETCMD | S626_CRBMSK_INTRESET_B);
010be96f
IA
689}
690
691/*
692 * Return counter setup in a format (COUNTER_SETUP) that is consistent
693 * for both A and B counters.
694 */
31de1948
IA
695static uint16_t s626_get_mode_a(struct comedi_device *dev,
696 const struct s626_enc_info *k)
010be96f
IA
697{
698 uint16_t cra;
699 uint16_t crb;
700 uint16_t setup;
701
702 /* Fetch CRA and CRB register images. */
31de1948
IA
703 cra = s626_debi_read(dev, k->my_cra);
704 crb = s626_debi_read(dev, k->my_crb);
010be96f
IA
705
706 /*
707 * Populate the standardized counter setup bit fields.
708 * Note: IndexSrc is restricted to ENC_X or IndxPol.
709 */
d8515652
IA
710 setup = (cra & S626_STDMSK_LOADSRC) | /* LoadSrc = LoadSrcA. */
711 ((crb << (S626_STDBIT_LATCHSRC - S626_CRBBIT_LATCHSRC)) &
712 S626_STDMSK_LATCHSRC) | /* LatchSrc = LatchSrcA. */
713 ((cra << (S626_STDBIT_INTSRC - S626_CRABIT_INTSRC_A)) &
714 S626_STDMSK_INTSRC) | /* IntSrc = IntSrcA. */
715 ((cra << (S626_STDBIT_INDXSRC - (S626_CRABIT_INDXSRC_A + 1))) &
716 S626_STDMSK_INDXSRC) | /* IndxSrc = IndxSrcA<1>. */
717 ((cra >> (S626_CRABIT_INDXPOL_A - S626_STDBIT_INDXPOL)) &
718 S626_STDMSK_INDXPOL) | /* IndxPol = IndxPolA. */
719 ((crb >> (S626_CRBBIT_CLKENAB_A - S626_STDBIT_CLKENAB)) &
720 S626_STDMSK_CLKENAB); /* ClkEnab = ClkEnabA. */
010be96f
IA
721
722 /* Adjust mode-dependent parameters. */
d8515652 723 if (cra & (2 << S626_CRABIT_CLKSRC_A)) {
010be96f
IA
724 /* Timer mode (ClkSrcA<1> == 1): */
725 /* Indicate Timer mode. */
d8515652 726 setup |= S626_CLKSRC_TIMER << S626_STDBIT_CLKSRC;
010be96f 727 /* Set ClkPol to indicate count direction (ClkSrcA<0>). */
d8515652
IA
728 setup |= (cra << (S626_STDBIT_CLKPOL - S626_CRABIT_CLKSRC_A)) &
729 S626_STDMSK_CLKPOL;
010be96f 730 /* ClkMult must be 1x in Timer mode. */
d8515652 731 setup |= S626_MULT_X1 << S626_STDBIT_CLKMULT;
010be96f
IA
732 } else {
733 /* Counter mode (ClkSrcA<1> == 0): */
734 /* Indicate Counter mode. */
d8515652 735 setup |= S626_CLKSRC_COUNTER << S626_STDBIT_CLKSRC;
010be96f 736 /* Pass through ClkPol. */
d8515652
IA
737 setup |= (cra >> (S626_CRABIT_CLKPOL_A - S626_STDBIT_CLKPOL)) &
738 S626_STDMSK_CLKPOL;
010be96f 739 /* Force ClkMult to 1x if not legal, else pass through. */
d8515652
IA
740 if ((cra & S626_CRAMSK_CLKMULT_A) ==
741 (S626_MULT_X0 << S626_CRABIT_CLKMULT_A))
742 setup |= S626_MULT_X1 << S626_STDBIT_CLKMULT;
010be96f 743 else
d8515652
IA
744 setup |= (cra >> (S626_CRABIT_CLKMULT_A -
745 S626_STDBIT_CLKMULT)) &
746 S626_STDMSK_CLKMULT;
010be96f
IA
747 }
748
749 /* Return adjusted counter setup. */
750 return setup;
751}
752
31de1948
IA
753static uint16_t s626_get_mode_b(struct comedi_device *dev,
754 const struct s626_enc_info *k)
010be96f
IA
755{
756 uint16_t cra;
757 uint16_t crb;
758 uint16_t setup;
759
760 /* Fetch CRA and CRB register images. */
31de1948
IA
761 cra = s626_debi_read(dev, k->my_cra);
762 crb = s626_debi_read(dev, k->my_crb);
010be96f
IA
763
764 /*
765 * Populate the standardized counter setup bit fields.
766 * Note: IndexSrc is restricted to ENC_X or IndxPol.
767 */
d8515652
IA
768 setup = ((crb << (S626_STDBIT_INTSRC - S626_CRBBIT_INTSRC_B)) &
769 S626_STDMSK_INTSRC) | /* IntSrc = IntSrcB. */
770 ((crb << (S626_STDBIT_LATCHSRC - S626_CRBBIT_LATCHSRC)) &
771 S626_STDMSK_LATCHSRC) | /* LatchSrc = LatchSrcB. */
772 ((crb << (S626_STDBIT_LOADSRC - S626_CRBBIT_LOADSRC_B)) &
773 S626_STDMSK_LOADSRC) | /* LoadSrc = LoadSrcB. */
774 ((crb << (S626_STDBIT_INDXPOL - S626_CRBBIT_INDXPOL_B)) &
775 S626_STDMSK_INDXPOL) | /* IndxPol = IndxPolB. */
776 ((crb >> (S626_CRBBIT_CLKENAB_B - S626_STDBIT_CLKENAB)) &
777 S626_STDMSK_CLKENAB) | /* ClkEnab = ClkEnabB. */
778 ((cra >> ((S626_CRABIT_INDXSRC_B + 1) - S626_STDBIT_INDXSRC)) &
779 S626_STDMSK_INDXSRC); /* IndxSrc = IndxSrcB<1>. */
010be96f
IA
780
781 /* Adjust mode-dependent parameters. */
d8515652
IA
782 if ((crb & S626_CRBMSK_CLKMULT_B) ==
783 (S626_MULT_X0 << S626_CRBBIT_CLKMULT_B)) {
784 /* Extender mode (ClkMultB == S626_MULT_X0): */
010be96f 785 /* Indicate Extender mode. */
d8515652 786 setup |= S626_CLKSRC_EXTENDER << S626_STDBIT_CLKSRC;
010be96f 787 /* Indicate multiplier is 1x. */
d8515652 788 setup |= S626_MULT_X1 << S626_STDBIT_CLKMULT;
010be96f 789 /* Set ClkPol equal to Timer count direction (ClkSrcB<0>). */
d8515652
IA
790 setup |= (cra >> (S626_CRABIT_CLKSRC_B - S626_STDBIT_CLKPOL)) &
791 S626_STDMSK_CLKPOL;
792 } else if (cra & (2 << S626_CRABIT_CLKSRC_B)) {
010be96f
IA
793 /* Timer mode (ClkSrcB<1> == 1): */
794 /* Indicate Timer mode. */
d8515652 795 setup |= S626_CLKSRC_TIMER << S626_STDBIT_CLKSRC;
010be96f 796 /* Indicate multiplier is 1x. */
d8515652 797 setup |= S626_MULT_X1 << S626_STDBIT_CLKMULT;
010be96f 798 /* Set ClkPol equal to Timer count direction (ClkSrcB<0>). */
d8515652
IA
799 setup |= (cra >> (S626_CRABIT_CLKSRC_B - S626_STDBIT_CLKPOL)) &
800 S626_STDMSK_CLKPOL;
010be96f
IA
801 } else {
802 /* If Counter mode (ClkSrcB<1> == 0): */
803 /* Indicate Timer mode. */
d8515652 804 setup |= S626_CLKSRC_COUNTER << S626_STDBIT_CLKSRC;
010be96f 805 /* Clock multiplier is passed through. */
d8515652
IA
806 setup |= (crb >> (S626_CRBBIT_CLKMULT_B -
807 S626_STDBIT_CLKMULT)) & S626_STDMSK_CLKMULT;
010be96f 808 /* Clock polarity is passed through. */
d8515652
IA
809 setup |= (crb << (S626_STDBIT_CLKPOL - S626_CRBBIT_CLKPOL_B)) &
810 S626_STDMSK_CLKPOL;
010be96f
IA
811 }
812
813 /* Return adjusted counter setup. */
814 return setup;
815}
816
17afeac2
IA
817/*
818 * Set the operating mode for the specified counter. The setup
819 * parameter is treated as a COUNTER_SETUP data type. The following
820 * parameters are programmable (all other parms are ignored): ClkMult,
821 * ClkPol, ClkEnab, IndexSrc, IndexPol, LoadSrc.
822 */
31de1948
IA
823static void s626_set_mode_a(struct comedi_device *dev,
824 const struct s626_enc_info *k, uint16_t setup,
825 uint16_t disable_int_src)
17afeac2
IA
826{
827 struct s626_private *devpriv = dev->private;
828 uint16_t cra;
829 uint16_t crb;
830
831 /* Initialize CRA and CRB images. */
832 /* Preload trigger is passed through. */
d8515652 833 cra = setup & S626_CRAMSK_LOADSRC_A;
17afeac2 834 /* IndexSrc is restricted to ENC_X or IndxPol. */
d8515652
IA
835 cra |= (setup & S626_STDMSK_INDXSRC) >>
836 (S626_STDBIT_INDXSRC - (S626_CRABIT_INDXSRC_A + 1));
17afeac2
IA
837
838 /* Reset any pending CounterA event captures. */
d8515652 839 crb = S626_CRBMSK_INTRESETCMD | S626_CRBMSK_INTRESET_A;
17afeac2 840 /* Clock enable is passed through. */
d8515652
IA
841 crb |= (setup & S626_STDMSK_CLKENAB) <<
842 (S626_CRBBIT_CLKENAB_A - S626_STDBIT_CLKENAB);
17afeac2
IA
843
844 /* Force IntSrc to Disabled if disable_int_src is asserted. */
845 if (!disable_int_src)
d8515652
IA
846 cra |= (setup & S626_STDMSK_INTSRC) >>
847 (S626_STDBIT_INTSRC - S626_CRABIT_INTSRC_A);
17afeac2
IA
848
849 /* Populate all mode-dependent attributes of CRA & CRB images. */
d8515652
IA
850 switch ((setup & S626_STDMSK_CLKSRC) >> S626_STDBIT_CLKSRC) {
851 case S626_CLKSRC_EXTENDER: /* Extender Mode: */
852 /* Force to Timer mode (Extender valid only for B counters). */
853 /* Fall through to case S626_CLKSRC_TIMER: */
854 case S626_CLKSRC_TIMER: /* Timer Mode: */
17afeac2 855 /* ClkSrcA<1> selects system clock */
d8515652 856 cra |= 2 << S626_CRABIT_CLKSRC_A;
17afeac2 857 /* Count direction (ClkSrcA<0>) obtained from ClkPol. */
d8515652
IA
858 cra |= (setup & S626_STDMSK_CLKPOL) >>
859 (S626_STDBIT_CLKPOL - S626_CRABIT_CLKSRC_A);
17afeac2 860 /* ClkPolA behaves as always-on clock enable. */
d8515652 861 cra |= 1 << S626_CRABIT_CLKPOL_A;
17afeac2 862 /* ClkMult must be 1x. */
d8515652 863 cra |= S626_MULT_X1 << S626_CRABIT_CLKMULT_A;
17afeac2
IA
864 break;
865 default: /* Counter Mode: */
866 /* Select ENC_C and ENC_D as clock/direction inputs. */
d8515652 867 cra |= S626_CLKSRC_COUNTER;
17afeac2 868 /* Clock polarity is passed through. */
d8515652
IA
869 cra |= (setup & S626_STDMSK_CLKPOL) <<
870 (S626_CRABIT_CLKPOL_A - S626_STDBIT_CLKPOL);
17afeac2 871 /* Force multiplier to x1 if not legal, else pass through. */
d8515652
IA
872 if ((setup & S626_STDMSK_CLKMULT) ==
873 (S626_MULT_X0 << S626_STDBIT_CLKMULT))
874 cra |= S626_MULT_X1 << S626_CRABIT_CLKMULT_A;
17afeac2 875 else
d8515652
IA
876 cra |= (setup & S626_STDMSK_CLKMULT) <<
877 (S626_CRABIT_CLKMULT_A - S626_STDBIT_CLKMULT);
17afeac2
IA
878 break;
879 }
880
881 /*
882 * Force positive index polarity if IndxSrc is software-driven only,
883 * otherwise pass it through.
884 */
d8515652
IA
885 if (~setup & S626_STDMSK_INDXSRC)
886 cra |= (setup & S626_STDMSK_INDXPOL) <<
887 (S626_CRABIT_INDXPOL_A - S626_STDBIT_INDXPOL);
17afeac2
IA
888
889 /*
890 * If IntSrc has been forced to Disabled, update the MISC2 interrupt
891 * enable mask to indicate the counter interrupt is disabled.
892 */
893 if (disable_int_src)
894 devpriv->counter_int_enabs &= ~k->my_event_bits[3];
895
896 /*
897 * While retaining CounterB and LatchSrc configurations, program the
898 * new counter operating mode.
899 */
d8515652
IA
900 s626_debi_replace(dev, k->my_cra,
901 S626_CRAMSK_INDXSRC_B | S626_CRAMSK_CLKSRC_B, cra);
902 s626_debi_replace(dev, k->my_crb,
903 ~(S626_CRBMSK_INTCTRL | S626_CRBMSK_CLKENAB_A), crb);
17afeac2
IA
904}
905
31de1948
IA
906static void s626_set_mode_b(struct comedi_device *dev,
907 const struct s626_enc_info *k, uint16_t setup,
908 uint16_t disable_int_src)
17afeac2
IA
909{
910 struct s626_private *devpriv = dev->private;
911 uint16_t cra;
912 uint16_t crb;
913
914 /* Initialize CRA and CRB images. */
915 /* IndexSrc field is restricted to ENC_X or IndxPol. */
d8515652
IA
916 cra = (setup & S626_STDMSK_INDXSRC) <<
917 (S626_CRABIT_INDXSRC_B + 1 - S626_STDBIT_INDXSRC);
17afeac2
IA
918
919 /* Reset event captures and disable interrupts. */
d8515652 920 crb = S626_CRBMSK_INTRESETCMD | S626_CRBMSK_INTRESET_B;
17afeac2 921 /* Clock enable is passed through. */
d8515652
IA
922 crb |= (setup & S626_STDMSK_CLKENAB) <<
923 (S626_CRBBIT_CLKENAB_B - S626_STDBIT_CLKENAB);
17afeac2 924 /* Preload trigger source is passed through. */
d8515652
IA
925 crb |= (setup & S626_STDMSK_LOADSRC) >>
926 (S626_STDBIT_LOADSRC - S626_CRBBIT_LOADSRC_B);
17afeac2
IA
927
928 /* Force IntSrc to Disabled if disable_int_src is asserted. */
929 if (!disable_int_src)
d8515652
IA
930 crb |= (setup & S626_STDMSK_INTSRC) >>
931 (S626_STDBIT_INTSRC - S626_CRBBIT_INTSRC_B);
17afeac2
IA
932
933 /* Populate all mode-dependent attributes of CRA & CRB images. */
d8515652
IA
934 switch ((setup & S626_STDMSK_CLKSRC) >> S626_STDBIT_CLKSRC) {
935 case S626_CLKSRC_TIMER: /* Timer Mode: */
17afeac2 936 /* ClkSrcB<1> selects system clock */
d8515652 937 cra |= 2 << S626_CRABIT_CLKSRC_B;
17afeac2 938 /* with direction (ClkSrcB<0>) obtained from ClkPol. */
d8515652
IA
939 cra |= (setup & S626_STDMSK_CLKPOL) <<
940 (S626_CRABIT_CLKSRC_B - S626_STDBIT_CLKPOL);
17afeac2 941 /* ClkPolB behaves as always-on clock enable. */
d8515652 942 crb |= 1 << S626_CRBBIT_CLKPOL_B;
17afeac2 943 /* ClkMultB must be 1x. */
d8515652 944 crb |= S626_MULT_X1 << S626_CRBBIT_CLKMULT_B;
17afeac2 945 break;
d8515652 946 case S626_CLKSRC_EXTENDER: /* Extender Mode: */
17afeac2 947 /* ClkSrcB source is OverflowA (same as "timer") */
d8515652 948 cra |= 2 << S626_CRABIT_CLKSRC_B;
17afeac2 949 /* with direction obtained from ClkPol. */
d8515652
IA
950 cra |= (setup & S626_STDMSK_CLKPOL) <<
951 (S626_CRABIT_CLKSRC_B - S626_STDBIT_CLKPOL);
17afeac2 952 /* ClkPolB controls IndexB -- always set to active. */
d8515652 953 crb |= 1 << S626_CRBBIT_CLKPOL_B;
17afeac2 954 /* ClkMultB selects OverflowA as the clock source. */
d8515652 955 crb |= S626_MULT_X0 << S626_CRBBIT_CLKMULT_B;
17afeac2
IA
956 break;
957 default: /* Counter Mode: */
958 /* Select ENC_C and ENC_D as clock/direction inputs. */
d8515652 959 cra |= S626_CLKSRC_COUNTER << S626_CRABIT_CLKSRC_B;
17afeac2 960 /* ClkPol is passed through. */
d8515652
IA
961 crb |= (setup & S626_STDMSK_CLKPOL) >>
962 (S626_STDBIT_CLKPOL - S626_CRBBIT_CLKPOL_B);
17afeac2 963 /* Force ClkMult to x1 if not legal, otherwise pass through. */
d8515652
IA
964 if ((setup & S626_STDMSK_CLKMULT) ==
965 (S626_MULT_X0 << S626_STDBIT_CLKMULT))
966 crb |= S626_MULT_X1 << S626_CRBBIT_CLKMULT_B;
17afeac2 967 else
d8515652
IA
968 crb |= (setup & S626_STDMSK_CLKMULT) <<
969 (S626_CRBBIT_CLKMULT_B - S626_STDBIT_CLKMULT);
17afeac2
IA
970 break;
971 }
972
973 /*
974 * Force positive index polarity if IndxSrc is software-driven only,
975 * otherwise pass it through.
976 */
d8515652
IA
977 if (~setup & S626_STDMSK_INDXSRC)
978 crb |= (setup & S626_STDMSK_INDXPOL) >>
979 (S626_STDBIT_INDXPOL - S626_CRBBIT_INDXPOL_B);
17afeac2
IA
980
981 /*
982 * If IntSrc has been forced to Disabled, update the MISC2 interrupt
983 * enable mask to indicate the counter interrupt is disabled.
984 */
985 if (disable_int_src)
986 devpriv->counter_int_enabs &= ~k->my_event_bits[3];
987
988 /*
989 * While retaining CounterA and LatchSrc configurations, program the
990 * new counter operating mode.
991 */
d8515652
IA
992 s626_debi_replace(dev, k->my_cra,
993 ~(S626_CRAMSK_INDXSRC_B | S626_CRAMSK_CLKSRC_B), cra);
994 s626_debi_replace(dev, k->my_crb,
995 S626_CRBMSK_CLKENAB_A | S626_CRBMSK_LATCHSRC, crb);
17afeac2
IA
996}
997
998/*
999 * Return/set a counter's enable. enab: 0=always enabled, 1=enabled by index.
1000 */
31de1948
IA
1001static void s626_set_enable_a(struct comedi_device *dev,
1002 const struct s626_enc_info *k, uint16_t enab)
17afeac2 1003{
d8515652
IA
1004 s626_debi_replace(dev, k->my_crb,
1005 ~(S626_CRBMSK_INTCTRL | S626_CRBMSK_CLKENAB_A),
1006 enab << S626_CRBBIT_CLKENAB_A);
17afeac2
IA
1007}
1008
31de1948
IA
1009static void s626_set_enable_b(struct comedi_device *dev,
1010 const struct s626_enc_info *k, uint16_t enab)
17afeac2 1011{
d8515652
IA
1012 s626_debi_replace(dev, k->my_crb,
1013 ~(S626_CRBMSK_INTCTRL | S626_CRBMSK_CLKENAB_B),
1014 enab << S626_CRBBIT_CLKENAB_B);
17afeac2
IA
1015}
1016
31de1948
IA
1017static uint16_t s626_get_enable_a(struct comedi_device *dev,
1018 const struct s626_enc_info *k)
17afeac2 1019{
d8515652 1020 return (s626_debi_read(dev, k->my_crb) >> S626_CRBBIT_CLKENAB_A) & 1;
17afeac2
IA
1021}
1022
31de1948
IA
1023static uint16_t s626_get_enable_b(struct comedi_device *dev,
1024 const struct s626_enc_info *k)
17afeac2 1025{
d8515652 1026 return (s626_debi_read(dev, k->my_crb) >> S626_CRBBIT_CLKENAB_B) & 1;
17afeac2
IA
1027}
1028
1029#ifdef unused
31de1948
IA
1030static uint16_t s626_get_latch_source(struct comedi_device *dev,
1031 const struct s626_enc_info *k)
17afeac2 1032{
d8515652 1033 return (s626_debi_read(dev, k->my_crb) >> S626_CRBBIT_LATCHSRC) & 3;
17afeac2
IA
1034}
1035#endif
1036
1037/*
1038 * Return/set the event that will trigger transfer of the preload
1039 * register into the counter. 0=ThisCntr_Index, 1=ThisCntr_Overflow,
1040 * 2=OverflowA (B counters only), 3=disabled.
1041 */
31de1948
IA
1042static void s626_set_load_trig_a(struct comedi_device *dev,
1043 const struct s626_enc_info *k, uint16_t trig)
17afeac2 1044{
d8515652
IA
1045 s626_debi_replace(dev, k->my_cra, ~S626_CRAMSK_LOADSRC_A,
1046 trig << S626_CRABIT_LOADSRC_A);
17afeac2
IA
1047}
1048
31de1948
IA
1049static void s626_set_load_trig_b(struct comedi_device *dev,
1050 const struct s626_enc_info *k, uint16_t trig)
17afeac2 1051{
d8515652
IA
1052 s626_debi_replace(dev, k->my_crb,
1053 ~(S626_CRBMSK_LOADSRC_B | S626_CRBMSK_INTCTRL),
1054 trig << S626_CRBBIT_LOADSRC_B);
17afeac2
IA
1055}
1056
31de1948
IA
1057static uint16_t s626_get_load_trig_a(struct comedi_device *dev,
1058 const struct s626_enc_info *k)
17afeac2 1059{
d8515652 1060 return (s626_debi_read(dev, k->my_cra) >> S626_CRABIT_LOADSRC_A) & 3;
17afeac2
IA
1061}
1062
31de1948
IA
1063static uint16_t s626_get_load_trig_b(struct comedi_device *dev,
1064 const struct s626_enc_info *k)
17afeac2 1065{
d8515652 1066 return (s626_debi_read(dev, k->my_crb) >> S626_CRBBIT_LOADSRC_B) & 3;
17afeac2
IA
1067}
1068
bc284a2a
IA
1069/*
1070 * Return/set counter interrupt source and clear any captured
1071 * index/overflow events. int_source: 0=Disabled, 1=OverflowOnly,
1072 * 2=IndexOnly, 3=IndexAndOverflow.
1073 */
31de1948
IA
1074static void s626_set_int_src_a(struct comedi_device *dev,
1075 const struct s626_enc_info *k,
1076 uint16_t int_source)
bc284a2a
IA
1077{
1078 struct s626_private *devpriv = dev->private;
1079
1080 /* Reset any pending counter overflow or index captures. */
d8515652
IA
1081 s626_debi_replace(dev, k->my_crb, ~S626_CRBMSK_INTCTRL,
1082 S626_CRBMSK_INTRESETCMD | S626_CRBMSK_INTRESET_A);
bc284a2a
IA
1083
1084 /* Program counter interrupt source. */
d8515652
IA
1085 s626_debi_replace(dev, k->my_cra, ~S626_CRAMSK_INTSRC_A,
1086 int_source << S626_CRABIT_INTSRC_A);
bc284a2a
IA
1087
1088 /* Update MISC2 interrupt enable mask. */
1089 devpriv->counter_int_enabs =
1090 (devpriv->counter_int_enabs & ~k->my_event_bits[3]) |
1091 k->my_event_bits[int_source];
1092}
1093
31de1948
IA
1094static void s626_set_int_src_b(struct comedi_device *dev,
1095 const struct s626_enc_info *k,
1096 uint16_t int_source)
bc284a2a
IA
1097{
1098 struct s626_private *devpriv = dev->private;
1099 uint16_t crb;
1100
1101 /* Cache writeable CRB register image. */
d8515652 1102 crb = s626_debi_read(dev, k->my_crb) & ~S626_CRBMSK_INTCTRL;
bc284a2a
IA
1103
1104 /* Reset any pending counter overflow or index captures. */
d8515652
IA
1105 s626_debi_write(dev, k->my_crb, (crb | S626_CRBMSK_INTRESETCMD |
1106 S626_CRBMSK_INTRESET_B));
bc284a2a
IA
1107
1108 /* Program counter interrupt source. */
31de1948 1109 s626_debi_write(dev, k->my_crb,
d8515652
IA
1110 ((crb & ~S626_CRBMSK_INTSRC_B) |
1111 (int_source << S626_CRBBIT_INTSRC_B)));
bc284a2a
IA
1112
1113 /* Update MISC2 interrupt enable mask. */
1114 devpriv->counter_int_enabs =
1115 (devpriv->counter_int_enabs & ~k->my_event_bits[3]) |
1116 k->my_event_bits[int_source];
1117}
1118
31de1948
IA
1119static uint16_t s626_get_int_src_a(struct comedi_device *dev,
1120 const struct s626_enc_info *k)
bc284a2a 1121{
d8515652 1122 return (s626_debi_read(dev, k->my_cra) >> S626_CRABIT_INTSRC_A) & 3;
bc284a2a
IA
1123}
1124
31de1948
IA
1125static uint16_t s626_get_int_src_b(struct comedi_device *dev,
1126 const struct s626_enc_info *k)
bc284a2a 1127{
d8515652 1128 return (s626_debi_read(dev, k->my_crb) >> S626_CRBBIT_INTSRC_B) & 3;
bc284a2a
IA
1129}
1130
1131#ifdef unused
1132/*
1133 * Return/set the clock multiplier.
1134 */
31de1948
IA
1135static void s626_set_clk_mult(struct comedi_device *dev,
1136 const struct s626_enc_info *k, uint16_t value)
bc284a2a 1137{
d8515652
IA
1138 k->set_mode(dev, k, ((k->get_mode(dev, k) & ~S626_STDMSK_CLKMULT) |
1139 (value << S626_STDBIT_CLKMULT)), false);
bc284a2a
IA
1140}
1141
31de1948
IA
1142static uint16_t s626_get_clk_mult(struct comedi_device *dev,
1143 const struct s626_enc_info *k)
bc284a2a 1144{
d8515652 1145 return (k->get_mode(dev, k) >> S626_STDBIT_CLKMULT) & 3;
bc284a2a
IA
1146}
1147
1148/*
1149 * Return/set the clock polarity.
1150 */
31de1948
IA
1151static void s626_set_clk_pol(struct comedi_device *dev,
1152 const struct s626_enc_info *k, uint16_t value)
bc284a2a 1153{
d8515652
IA
1154 k->set_mode(dev, k, ((k->get_mode(dev, k) & ~S626_STDMSK_CLKPOL) |
1155 (value << S626_STDBIT_CLKPOL)), false);
bc284a2a
IA
1156}
1157
31de1948
IA
1158static uint16_t s626_get_clk_pol(struct comedi_device *dev,
1159 const struct s626_enc_info *k)
bc284a2a 1160{
d8515652 1161 return (k->get_mode(dev, k) >> S626_STDBIT_CLKPOL) & 1;
bc284a2a
IA
1162}
1163
1164/*
1165 * Return/set the clock source.
1166 */
31de1948
IA
1167static void s626_set_clk_src(struct comedi_device *dev,
1168 const struct s626_enc_info *k, uint16_t value)
bc284a2a 1169{
d8515652
IA
1170 k->set_mode(dev, k, ((k->get_mode(dev, k) & ~S626_STDMSK_CLKSRC) |
1171 (value << S626_STDBIT_CLKSRC)), false);
bc284a2a
IA
1172}
1173
31de1948
IA
1174static uint16_t s626_get_clk_src(struct comedi_device *dev,
1175 const struct s626_enc_info *k)
bc284a2a 1176{
d8515652 1177 return (k->get_mode(dev, k) >> S626_STDBIT_CLKSRC) & 3;
bc284a2a
IA
1178}
1179
1180/*
1181 * Return/set the index polarity.
1182 */
31de1948
IA
1183static void s626_set_index_pol(struct comedi_device *dev,
1184 const struct s626_enc_info *k, uint16_t value)
bc284a2a 1185{
d8515652
IA
1186 k->set_mode(dev, k, ((k->get_mode(dev, k) & ~S626_STDMSK_INDXPOL) |
1187 ((value != 0) << S626_STDBIT_INDXPOL)), false);
bc284a2a
IA
1188}
1189
31de1948
IA
1190static uint16_t s626_get_index_pol(struct comedi_device *dev,
1191 const struct s626_enc_info *k)
bc284a2a 1192{
d8515652 1193 return (k->get_mode(dev, k) >> S626_STDBIT_INDXPOL) & 1;
bc284a2a
IA
1194}
1195
1196/*
1197 * Return/set the index source.
1198 */
31de1948
IA
1199static void s626_set_index_src(struct comedi_device *dev,
1200 const struct s626_enc_info *k, uint16_t value)
bc284a2a 1201{
d8515652
IA
1202 k->set_mode(dev, k, ((k->get_mode(dev, k) & ~S626_STDMSK_INDXSRC) |
1203 ((value != 0) << S626_STDBIT_INDXSRC)), false);
bc284a2a
IA
1204}
1205
31de1948
IA
1206static uint16_t s626_get_index_src(struct comedi_device *dev,
1207 const struct s626_enc_info *k)
bc284a2a 1208{
d8515652 1209 return (k->get_mode(dev, k) >> S626_STDBIT_INDXSRC) & 1;
bc284a2a
IA
1210}
1211#endif
1212
1213/*
1214 * Generate an index pulse.
1215 */
31de1948
IA
1216static void s626_pulse_index_a(struct comedi_device *dev,
1217 const struct s626_enc_info *k)
bc284a2a
IA
1218{
1219 uint16_t cra;
1220
31de1948 1221 cra = s626_debi_read(dev, k->my_cra);
bc284a2a 1222 /* Pulse index. */
d8515652 1223 s626_debi_write(dev, k->my_cra, (cra ^ S626_CRAMSK_INDXPOL_A));
31de1948 1224 s626_debi_write(dev, k->my_cra, cra);
bc284a2a
IA
1225}
1226
31de1948
IA
1227static void s626_pulse_index_b(struct comedi_device *dev,
1228 const struct s626_enc_info *k)
bc284a2a
IA
1229{
1230 uint16_t crb;
1231
d8515652 1232 crb = s626_debi_read(dev, k->my_crb) & ~S626_CRBMSK_INTCTRL;
bc284a2a 1233 /* Pulse index. */
d8515652 1234 s626_debi_write(dev, k->my_crb, (crb ^ S626_CRBMSK_INDXPOL_B));
31de1948 1235 s626_debi_write(dev, k->my_crb, crb);
bc284a2a
IA
1236}
1237
3f1f219c
IA
1238static const struct s626_enc_info s626_enc_chan_info[] = {
1239 {
31de1948
IA
1240 .get_enable = s626_get_enable_a,
1241 .get_int_src = s626_get_int_src_a,
1242 .get_load_trig = s626_get_load_trig_a,
1243 .get_mode = s626_get_mode_a,
1244 .pulse_index = s626_pulse_index_a,
1245 .set_enable = s626_set_enable_a,
1246 .set_int_src = s626_set_int_src_a,
1247 .set_load_trig = s626_set_load_trig_a,
1248 .set_mode = s626_set_mode_a,
1249 .reset_cap_flags = s626_reset_cap_flags_a,
d8515652
IA
1250 .my_cra = S626_LP_CR0A,
1251 .my_crb = S626_LP_CR0B,
1252 .my_latch_lsw = S626_LP_CNTR0ALSW,
676921c9 1253 .my_event_bits = S626_EVBITS(0),
3f1f219c 1254 }, {
31de1948
IA
1255 .get_enable = s626_get_enable_a,
1256 .get_int_src = s626_get_int_src_a,
1257 .get_load_trig = s626_get_load_trig_a,
1258 .get_mode = s626_get_mode_a,
1259 .pulse_index = s626_pulse_index_a,
1260 .set_enable = s626_set_enable_a,
1261 .set_int_src = s626_set_int_src_a,
1262 .set_load_trig = s626_set_load_trig_a,
1263 .set_mode = s626_set_mode_a,
1264 .reset_cap_flags = s626_reset_cap_flags_a,
d8515652
IA
1265 .my_cra = S626_LP_CR1A,
1266 .my_crb = S626_LP_CR1B,
1267 .my_latch_lsw = S626_LP_CNTR1ALSW,
676921c9 1268 .my_event_bits = S626_EVBITS(1),
3f1f219c 1269 }, {
31de1948
IA
1270 .get_enable = s626_get_enable_a,
1271 .get_int_src = s626_get_int_src_a,
1272 .get_load_trig = s626_get_load_trig_a,
1273 .get_mode = s626_get_mode_a,
1274 .pulse_index = s626_pulse_index_a,
1275 .set_enable = s626_set_enable_a,
1276 .set_int_src = s626_set_int_src_a,
1277 .set_load_trig = s626_set_load_trig_a,
1278 .set_mode = s626_set_mode_a,
1279 .reset_cap_flags = s626_reset_cap_flags_a,
d8515652
IA
1280 .my_cra = S626_LP_CR2A,
1281 .my_crb = S626_LP_CR2B,
1282 .my_latch_lsw = S626_LP_CNTR2ALSW,
676921c9 1283 .my_event_bits = S626_EVBITS(2),
3f1f219c 1284 }, {
31de1948
IA
1285 .get_enable = s626_get_enable_b,
1286 .get_int_src = s626_get_int_src_b,
1287 .get_load_trig = s626_get_load_trig_b,
1288 .get_mode = s626_get_mode_b,
1289 .pulse_index = s626_pulse_index_b,
1290 .set_enable = s626_set_enable_b,
1291 .set_int_src = s626_set_int_src_b,
1292 .set_load_trig = s626_set_load_trig_b,
1293 .set_mode = s626_set_mode_b,
1294 .reset_cap_flags = s626_reset_cap_flags_b,
d8515652
IA
1295 .my_cra = S626_LP_CR0A,
1296 .my_crb = S626_LP_CR0B,
1297 .my_latch_lsw = S626_LP_CNTR0BLSW,
676921c9 1298 .my_event_bits = S626_EVBITS(3),
3f1f219c 1299 }, {
31de1948
IA
1300 .get_enable = s626_get_enable_b,
1301 .get_int_src = s626_get_int_src_b,
1302 .get_load_trig = s626_get_load_trig_b,
1303 .get_mode = s626_get_mode_b,
1304 .pulse_index = s626_pulse_index_b,
1305 .set_enable = s626_set_enable_b,
1306 .set_int_src = s626_set_int_src_b,
1307 .set_load_trig = s626_set_load_trig_b,
1308 .set_mode = s626_set_mode_b,
1309 .reset_cap_flags = s626_reset_cap_flags_b,
d8515652
IA
1310 .my_cra = S626_LP_CR1A,
1311 .my_crb = S626_LP_CR1B,
1312 .my_latch_lsw = S626_LP_CNTR1BLSW,
676921c9 1313 .my_event_bits = S626_EVBITS(4),
3f1f219c 1314 }, {
31de1948
IA
1315 .get_enable = s626_get_enable_b,
1316 .get_int_src = s626_get_int_src_b,
1317 .get_load_trig = s626_get_load_trig_b,
1318 .get_mode = s626_get_mode_b,
1319 .pulse_index = s626_pulse_index_b,
1320 .set_enable = s626_set_enable_b,
1321 .set_int_src = s626_set_int_src_b,
1322 .set_load_trig = s626_set_load_trig_b,
1323 .set_mode = s626_set_mode_b,
1324 .reset_cap_flags = s626_reset_cap_flags_b,
d8515652
IA
1325 .my_cra = S626_LP_CR2A,
1326 .my_crb = S626_LP_CR2B,
1327 .my_latch_lsw = S626_LP_CNTR2BLSW,
676921c9 1328 .my_event_bits = S626_EVBITS(5),
3f1f219c
IA
1329 },
1330};
1331
020c44f3 1332static unsigned int s626_ai_reg_to_uint(int data)
11e865c1 1333{
020c44f3 1334 unsigned int tempdata;
11e865c1 1335
020c44f3
HS
1336 tempdata = (data >> 18);
1337 if (tempdata & 0x2000)
1338 tempdata &= 0x1fff;
1339 else
1340 tempdata += (1 << 13);
11e865c1 1341
020c44f3
HS
1342 return tempdata;
1343}
8231eb56 1344
6baffbc2
HS
1345static int s626_dio_set_irq(struct comedi_device *dev, unsigned int chan)
1346{
100b4edc
HS
1347 unsigned int group = chan / 16;
1348 unsigned int mask = 1 << (chan - (16 * group));
6baffbc2
HS
1349 unsigned int status;
1350
6baffbc2 1351 /* set channel to capture positive edge */
d8515652
IA
1352 status = s626_debi_read(dev, S626_LP_RDEDGSEL(group));
1353 s626_debi_write(dev, S626_LP_WREDGSEL(group), mask | status);
6baffbc2
HS
1354
1355 /* enable interrupt on selected channel */
d8515652
IA
1356 status = s626_debi_read(dev, S626_LP_RDINTSEL(group));
1357 s626_debi_write(dev, S626_LP_WRINTSEL(group), mask | status);
6baffbc2
HS
1358
1359 /* enable edge capture write command */
d8515652 1360 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_EDCAP);
6baffbc2
HS
1361
1362 /* enable edge capture on selected channel */
d8515652
IA
1363 status = s626_debi_read(dev, S626_LP_RDCAPSEL(group));
1364 s626_debi_write(dev, S626_LP_WRCAPSEL(group), mask | status);
6baffbc2
HS
1365
1366 return 0;
1367}
1368
1369static int s626_dio_reset_irq(struct comedi_device *dev, unsigned int group,
1370 unsigned int mask)
1371{
6baffbc2 1372 /* disable edge capture write command */
d8515652 1373 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_NOEDCAP);
6baffbc2
HS
1374
1375 /* enable edge capture on selected channel */
d8515652 1376 s626_debi_write(dev, S626_LP_WRCAPSEL(group), mask);
6baffbc2
HS
1377
1378 return 0;
1379}
1380
1381static int s626_dio_clear_irq(struct comedi_device *dev)
1382{
1383 unsigned int group;
1384
1385 /* disable edge capture write command */
d8515652 1386 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_NOEDCAP);
6baffbc2 1387
100b4edc
HS
1388 /* clear all dio pending events and interrupt */
1389 for (group = 0; group < S626_DIO_BANKS; group++)
d8515652 1390 s626_debi_write(dev, S626_LP_WRCAPSEL(group), 0xffff);
6baffbc2
HS
1391
1392 return 0;
1393}
1394
31de1948
IA
1395static void s626_handle_dio_interrupt(struct comedi_device *dev,
1396 uint16_t irqbit, uint8_t group)
65a17c29
HS
1397{
1398 struct s626_private *devpriv = dev->private;
1399 struct comedi_subdevice *s = dev->read_subdev;
1400 struct comedi_cmd *cmd = &s->async->cmd;
1401
1402 s626_dio_reset_irq(dev, group, irqbit);
1403
1404 if (devpriv->ai_cmd_running) {
1405 /* check if interrupt is an ai acquisition start trigger */
1406 if ((irqbit >> (cmd->start_arg - (16 * group))) == 1 &&
1407 cmd->start_src == TRIG_EXT) {
1408 /* Start executing the RPS program */
d8515652 1409 s626_mc_enable(dev, S626_MC1_ERPS1, S626_P_MC1);
65a17c29
HS
1410
1411 if (cmd->scan_begin_src == TRIG_EXT)
1412 s626_dio_set_irq(dev, cmd->scan_begin_arg);
1413 }
1414 if ((irqbit >> (cmd->scan_begin_arg - (16 * group))) == 1 &&
1415 cmd->scan_begin_src == TRIG_EXT) {
ddd9813e 1416 /* Trigger ADC scan loop start */
d8515652 1417 s626_mc_enable(dev, S626_MC2_ADC_RPS, S626_P_MC2);
65a17c29
HS
1418
1419 if (cmd->convert_src == TRIG_EXT) {
1420 devpriv->ai_convert_count = cmd->chanlist_len;
1421
1422 s626_dio_set_irq(dev, cmd->convert_arg);
1423 }
1424
1425 if (cmd->convert_src == TRIG_TIMER) {
3a305a66
IA
1426 const struct s626_enc_info *k =
1427 &s626_enc_chan_info[5];
65a17c29
HS
1428
1429 devpriv->ai_convert_count = cmd->chanlist_len;
d8515652 1430 k->set_enable(dev, k, S626_CLKENAB_ALWAYS);
65a17c29
HS
1431 }
1432 }
1433 if ((irqbit >> (cmd->convert_arg - (16 * group))) == 1 &&
1434 cmd->convert_src == TRIG_EXT) {
ddd9813e 1435 /* Trigger ADC scan loop start */
d8515652 1436 s626_mc_enable(dev, S626_MC2_ADC_RPS, S626_P_MC2);
65a17c29
HS
1437
1438 devpriv->ai_convert_count--;
1439 if (devpriv->ai_convert_count > 0)
1440 s626_dio_set_irq(dev, cmd->convert_arg);
1441 }
1442 }
1443}
1444
31de1948 1445static void s626_check_dio_interrupts(struct comedi_device *dev)
65a17c29
HS
1446{
1447 uint16_t irqbit;
1448 uint8_t group;
1449
1450 for (group = 0; group < S626_DIO_BANKS; group++) {
1451 irqbit = 0;
1452 /* read interrupt type */
d8515652 1453 irqbit = s626_debi_read(dev, S626_LP_RDCAPFLG(group));
65a17c29
HS
1454
1455 /* check if interrupt is generated from dio channels */
1456 if (irqbit) {
31de1948 1457 s626_handle_dio_interrupt(dev, irqbit, group);
65a17c29
HS
1458 return;
1459 }
1460 }
1461}
1462
31de1948 1463static void s626_check_counter_interrupts(struct comedi_device *dev)
0b9675d5
HS
1464{
1465 struct s626_private *devpriv = dev->private;
1466 struct comedi_subdevice *s = dev->read_subdev;
1467 struct comedi_async *async = s->async;
1468 struct comedi_cmd *cmd = &async->cmd;
3a305a66 1469 const struct s626_enc_info *k;
0b9675d5
HS
1470 uint16_t irqbit;
1471
1472 /* read interrupt type */
d8515652 1473 irqbit = s626_debi_read(dev, S626_LP_RDMISC2);
0b9675d5
HS
1474
1475 /* check interrupt on counters */
d8515652 1476 if (irqbit & S626_IRQ_COINT1A) {
3a305a66 1477 k = &s626_enc_chan_info[0];
0b9675d5
HS
1478
1479 /* clear interrupt capture flag */
b075ac8e 1480 k->reset_cap_flags(dev, k);
0b9675d5 1481 }
d8515652 1482 if (irqbit & S626_IRQ_COINT2A) {
3a305a66 1483 k = &s626_enc_chan_info[1];
0b9675d5
HS
1484
1485 /* clear interrupt capture flag */
b075ac8e 1486 k->reset_cap_flags(dev, k);
0b9675d5 1487 }
d8515652 1488 if (irqbit & S626_IRQ_COINT3A) {
3a305a66 1489 k = &s626_enc_chan_info[2];
0b9675d5
HS
1490
1491 /* clear interrupt capture flag */
b075ac8e 1492 k->reset_cap_flags(dev, k);
0b9675d5 1493 }
d8515652 1494 if (irqbit & S626_IRQ_COINT1B) {
3a305a66 1495 k = &s626_enc_chan_info[3];
0b9675d5
HS
1496
1497 /* clear interrupt capture flag */
b075ac8e 1498 k->reset_cap_flags(dev, k);
0b9675d5 1499 }
d8515652 1500 if (irqbit & S626_IRQ_COINT2B) {
3a305a66 1501 k = &s626_enc_chan_info[4];
0b9675d5
HS
1502
1503 /* clear interrupt capture flag */
b075ac8e 1504 k->reset_cap_flags(dev, k);
0b9675d5
HS
1505
1506 if (devpriv->ai_convert_count > 0) {
1507 devpriv->ai_convert_count--;
1508 if (devpriv->ai_convert_count == 0)
d8515652 1509 k->set_enable(dev, k, S626_CLKENAB_INDEX);
0b9675d5
HS
1510
1511 if (cmd->convert_src == TRIG_TIMER) {
ddd9813e 1512 /* Trigger ADC scan loop start */
d8515652
IA
1513 s626_mc_enable(dev, S626_MC2_ADC_RPS,
1514 S626_P_MC2);
0b9675d5
HS
1515 }
1516 }
1517 }
d8515652 1518 if (irqbit & S626_IRQ_COINT3B) {
3a305a66 1519 k = &s626_enc_chan_info[5];
0b9675d5
HS
1520
1521 /* clear interrupt capture flag */
b075ac8e 1522 k->reset_cap_flags(dev, k);
0b9675d5
HS
1523
1524 if (cmd->scan_begin_src == TRIG_TIMER) {
ddd9813e 1525 /* Trigger ADC scan loop start */
d8515652 1526 s626_mc_enable(dev, S626_MC2_ADC_RPS, S626_P_MC2);
0b9675d5
HS
1527 }
1528
1529 if (cmd->convert_src == TRIG_TIMER) {
3a305a66 1530 k = &s626_enc_chan_info[4];
0b9675d5 1531 devpriv->ai_convert_count = cmd->chanlist_len;
d8515652 1532 k->set_enable(dev, k, S626_CLKENAB_ALWAYS);
0b9675d5
HS
1533 }
1534 }
1535}
1536
31de1948 1537static bool s626_handle_eos_interrupt(struct comedi_device *dev)
4c2d13e0
HS
1538{
1539 struct s626_private *devpriv = dev->private;
1540 struct comedi_subdevice *s = dev->read_subdev;
1541 struct comedi_async *async = s->async;
1542 struct comedi_cmd *cmd = &async->cmd;
1543 /*
1544 * Init ptr to DMA buffer that holds new ADC data. We skip the
1545 * first uint16_t in the buffer because it contains junk data
1546 * from the final ADC of the previous poll list scan.
1547 */
07a36d66 1548 int32_t *readaddr = (int32_t *)devpriv->ana_buf.logical_base + 1;
4c2d13e0
HS
1549 bool finished = false;
1550 int i;
1551
1552 /* get the data and hand it over to comedi */
1553 for (i = 0; i < cmd->chanlist_len; i++) {
1554 short tempdata;
1555
1556 /*
1557 * Convert ADC data to 16-bit integer values and copy
1558 * to application buffer.
1559 */
1560 tempdata = s626_ai_reg_to_uint((int)*readaddr);
1561 readaddr++;
1562
1563 /* put data into read buffer */
1564 /* comedi_buf_put(async, tempdata); */
1565 cfc_write_to_buffer(s, tempdata);
1566 }
1567
1568 /* end of scan occurs */
1569 async->events |= COMEDI_CB_EOS;
1570
e6132fc9 1571 if (!devpriv->ai_continuous)
4c2d13e0
HS
1572 devpriv->ai_sample_count--;
1573 if (devpriv->ai_sample_count <= 0) {
1574 devpriv->ai_cmd_running = 0;
1575
c5cf4606 1576 /* Stop RPS program */
d8515652 1577 s626_mc_disable(dev, S626_MC1_ERPS1, S626_P_MC1);
4c2d13e0
HS
1578
1579 /* send end of acquisition */
1580 async->events |= COMEDI_CB_EOA;
1581
1582 /* disable master interrupt */
1583 finished = true;
1584 }
1585
1586 if (devpriv->ai_cmd_running && cmd->scan_begin_src == TRIG_EXT)
1587 s626_dio_set_irq(dev, cmd->scan_begin_arg);
1588
1589 /* tell comedi that data is there */
1590 comedi_event(dev, s);
1591
1592 return finished;
1593}
1594
020c44f3
HS
1595static irqreturn_t s626_irq_handler(int irq, void *d)
1596{
1597 struct comedi_device *dev = d;
7f2f7e05 1598 struct s626_private *devpriv = dev->private;
020c44f3 1599 unsigned long flags;
020c44f3 1600 uint32_t irqtype, irqstatus;
11e865c1 1601
a7401cdd 1602 if (!dev->attached)
020c44f3 1603 return IRQ_NONE;
8ee52611 1604 /* lock to avoid race with comedi_poll */
020c44f3 1605 spin_lock_irqsave(&dev->spinlock, flags);
11e865c1 1606
020c44f3 1607 /* save interrupt enable register state */
d8515652 1608 irqstatus = readl(devpriv->mmio + S626_P_IER);
11e865c1 1609
020c44f3 1610 /* read interrupt type */
d8515652 1611 irqtype = readl(devpriv->mmio + S626_P_ISR);
11e865c1 1612
020c44f3 1613 /* disable master interrupt */
d8515652 1614 writel(0, devpriv->mmio + S626_P_IER);
11e865c1 1615
020c44f3 1616 /* clear interrupt */
d8515652 1617 writel(irqtype, devpriv->mmio + S626_P_ISR);
11e865c1 1618
020c44f3 1619 switch (irqtype) {
d8515652 1620 case S626_IRQ_RPS1: /* end_of_scan occurs */
31de1948 1621 if (s626_handle_eos_interrupt(dev))
020c44f3 1622 irqstatus = 0;
020c44f3 1623 break;
d8515652 1624 case S626_IRQ_GPIO3: /* check dio and counter interrupt */
020c44f3 1625 /* s626_dio_clear_irq(dev); */
31de1948
IA
1626 s626_check_dio_interrupts(dev);
1627 s626_check_counter_interrupts(dev);
0b9675d5 1628 break;
020c44f3 1629 }
11e865c1 1630
020c44f3 1631 /* enable interrupt */
d8515652 1632 writel(irqstatus, devpriv->mmio + S626_P_IER);
b6c77757 1633
020c44f3
HS
1634 spin_unlock_irqrestore(&dev->spinlock, flags);
1635 return IRQ_HANDLED;
1636}
b6c77757 1637
020c44f3 1638/*
8ee52611 1639 * This function builds the RPS program for hardware driven acquisition.
020c44f3 1640 */
31de1948 1641static void s626_reset_adc(struct comedi_device *dev, uint8_t *ppl)
020c44f3 1642{
7f2f7e05 1643 struct s626_private *devpriv = dev->private;
f1f7efce
IA
1644 uint32_t *rps;
1645 uint32_t jmp_adrs;
020c44f3
HS
1646 uint16_t i;
1647 uint16_t n;
f1f7efce 1648 uint32_t local_ppl;
8ee52611 1649 struct comedi_cmd *cmd = &dev->subdevices->async->cmd;
11e865c1 1650
c5cf4606 1651 /* Stop RPS program in case it is currently running */
d8515652 1652 s626_mc_disable(dev, S626_MC1_ERPS1, S626_P_MC1);
11e865c1 1653
8ee52611 1654 /* Set starting logical address to write RPS commands. */
f1f7efce 1655 rps = (uint32_t *)devpriv->rps_buf.logical_base;
11e865c1 1656
25f8fd5e 1657 /* Initialize RPS instruction pointer */
07a36d66 1658 writel((uint32_t)devpriv->rps_buf.physical_base,
d8515652 1659 devpriv->mmio + S626_P_RPSADDR1);
11e865c1 1660
07a36d66 1661 /* Construct RPS program in rps_buf DMA buffer */
020c44f3 1662 if (cmd != NULL && cmd->scan_begin_src != TRIG_FOLLOW) {
8ee52611 1663 /* Wait for Start trigger. */
d8515652
IA
1664 *rps++ = S626_RPS_PAUSE | S626_RPS_SIGADC;
1665 *rps++ = S626_RPS_CLRSIGNAL | S626_RPS_SIGADC;
020c44f3 1666 }
11e865c1 1667
8ee52611
IA
1668 /*
1669 * SAA7146 BUG WORKAROUND Do a dummy DEBI Write. This is necessary
020c44f3
HS
1670 * because the first RPS DEBI Write following a non-RPS DEBI write
1671 * seems to always fail. If we don't do this dummy write, the ADC
1672 * gain might not be set to the value required for the first slot in
1673 * the poll list; the ADC gain would instead remain unchanged from
1674 * the previously programmed value.
1675 */
020c44f3 1676 /* Write DEBI Write command and address to shadow RAM. */
d8515652
IA
1677 *rps++ = S626_RPS_LDREG | (S626_P_DEBICMD >> 2);
1678 *rps++ = S626_DEBI_CMD_WRWORD | S626_LP_GSEL;
1679 *rps++ = S626_RPS_LDREG | (S626_P_DEBIAD >> 2);
8ee52611 1680 /* Write DEBI immediate data to shadow RAM: */
d8515652
IA
1681 *rps++ = S626_GSEL_BIPOLAR5V; /* arbitrary immediate data value. */
1682 *rps++ = S626_RPS_CLRSIGNAL | S626_RPS_DEBI;
8ee52611 1683 /* Reset "shadow RAM uploaded" flag. */
d8515652
IA
1684 /* Invoke shadow RAM upload. */
1685 *rps++ = S626_RPS_UPLOAD | S626_RPS_DEBI;
1686 /* Wait for shadow upload to finish. */
1687 *rps++ = S626_RPS_PAUSE | S626_RPS_DEBI;
11e865c1 1688
8ee52611
IA
1689 /*
1690 * Digitize all slots in the poll list. This is implemented as a
020c44f3 1691 * for loop to limit the slot count to 16 in case the application
d8515652 1692 * forgot to set the S626_EOPL flag in the final slot.
020c44f3 1693 */
07a36d66
IA
1694 for (devpriv->adc_items = 0; devpriv->adc_items < 16;
1695 devpriv->adc_items++) {
8ee52611
IA
1696 /*
1697 * Convert application's poll list item to private board class
020c44f3
HS
1698 * format. Each app poll list item is an uint8_t with form
1699 * (EOPL,x,x,RANGE,CHAN<3:0>), where RANGE code indicates 0 =
1700 * +-10V, 1 = +-5V, and EOPL = End of Poll List marker.
b6c77757 1701 */
d8515652
IA
1702 local_ppl = (*ppl << 8) | (*ppl & 0x10 ? S626_GSEL_BIPOLAR5V :
1703 S626_GSEL_BIPOLAR10V);
8ee52611
IA
1704
1705 /* Switch ADC analog gain. */
1706 /* Write DEBI command and address to shadow RAM. */
d8515652
IA
1707 *rps++ = S626_RPS_LDREG | (S626_P_DEBICMD >> 2);
1708 *rps++ = S626_DEBI_CMD_WRWORD | S626_LP_GSEL;
8ee52611 1709 /* Write DEBI immediate data to shadow RAM. */
d8515652 1710 *rps++ = S626_RPS_LDREG | (S626_P_DEBIAD >> 2);
f1f7efce 1711 *rps++ = local_ppl;
8ee52611 1712 /* Reset "shadow RAM uploaded" flag. */
d8515652 1713 *rps++ = S626_RPS_CLRSIGNAL | S626_RPS_DEBI;
8ee52611 1714 /* Invoke shadow RAM upload. */
d8515652 1715 *rps++ = S626_RPS_UPLOAD | S626_RPS_DEBI;
8ee52611 1716 /* Wait for shadow upload to finish. */
d8515652 1717 *rps++ = S626_RPS_PAUSE | S626_RPS_DEBI;
8ee52611 1718 /* Select ADC analog input channel. */
d8515652 1719 *rps++ = S626_RPS_LDREG | (S626_P_DEBICMD >> 2);
8ee52611 1720 /* Write DEBI command and address to shadow RAM. */
d8515652
IA
1721 *rps++ = S626_DEBI_CMD_WRWORD | S626_LP_ISEL;
1722 *rps++ = S626_RPS_LDREG | (S626_P_DEBIAD >> 2);
8ee52611 1723 /* Write DEBI immediate data to shadow RAM. */
f1f7efce 1724 *rps++ = local_ppl;
8ee52611 1725 /* Reset "shadow RAM uploaded" flag. */
d8515652 1726 *rps++ = S626_RPS_CLRSIGNAL | S626_RPS_DEBI;
8ee52611 1727 /* Invoke shadow RAM upload. */
d8515652 1728 *rps++ = S626_RPS_UPLOAD | S626_RPS_DEBI;
8ee52611 1729 /* Wait for shadow upload to finish. */
d8515652 1730 *rps++ = S626_RPS_PAUSE | S626_RPS_DEBI;
11e865c1 1731
8ee52611
IA
1732 /*
1733 * Delay at least 10 microseconds for analog input settling.
d8515652
IA
1734 * Instead of padding with NOPs, we use S626_RPS_JUMP
1735 * instructions here; this allows us to produce a longer delay
1736 * than is possible with NOPs because each S626_RPS_JUMP
1737 * flushes the RPS' instruction prefetch pipeline.
020c44f3 1738 */
f1f7efce 1739 jmp_adrs =
07a36d66 1740 (uint32_t)devpriv->rps_buf.physical_base +
f1f7efce 1741 (uint32_t)((unsigned long)rps -
07a36d66
IA
1742 (unsigned long)devpriv->
1743 rps_buf.logical_base);
d8515652 1744 for (i = 0; i < (10 * S626_RPSCLK_PER_US / 2); i++) {
f1f7efce 1745 jmp_adrs += 8; /* Repeat to implement time delay: */
d8515652
IA
1746 /* Jump to next RPS instruction. */
1747 *rps++ = S626_RPS_JUMP;
f1f7efce 1748 *rps++ = jmp_adrs;
020c44f3 1749 }
11e865c1 1750
020c44f3 1751 if (cmd != NULL && cmd->convert_src != TRIG_NOW) {
8ee52611 1752 /* Wait for Start trigger. */
d8515652
IA
1753 *rps++ = S626_RPS_PAUSE | S626_RPS_SIGADC;
1754 *rps++ = S626_RPS_CLRSIGNAL | S626_RPS_SIGADC;
020c44f3 1755 }
8ee52611
IA
1756 /* Start ADC by pulsing GPIO1. */
1757 /* Begin ADC Start pulse. */
d8515652
IA
1758 *rps++ = S626_RPS_LDREG | (S626_P_GPIO >> 2);
1759 *rps++ = S626_GPIO_BASE | S626_GPIO1_LO;
1760 *rps++ = S626_RPS_NOP;
8ee52611
IA
1761 /* VERSION 2.03 CHANGE: STRETCH OUT ADC START PULSE. */
1762 /* End ADC Start pulse. */
d8515652
IA
1763 *rps++ = S626_RPS_LDREG | (S626_P_GPIO >> 2);
1764 *rps++ = S626_GPIO_BASE | S626_GPIO1_HI;
8ee52611
IA
1765 /*
1766 * Wait for ADC to complete (GPIO2 is asserted high when ADC not
020c44f3
HS
1767 * busy) and for data from previous conversion to shift into FB
1768 * BUFFER 1 register.
1769 */
d8515652
IA
1770 /* Wait for ADC done. */
1771 *rps++ = S626_RPS_PAUSE | S626_RPS_GPIO2;
11e865c1 1772
8ee52611 1773 /* Transfer ADC data from FB BUFFER 1 register to DMA buffer. */
d8515652
IA
1774 *rps++ = S626_RPS_STREG |
1775 (S626_BUGFIX_STREG(S626_P_FB_BUFFER1) >> 2);
f1f7efce
IA
1776 *rps++ = (uint32_t)devpriv->ana_buf.physical_base +
1777 (devpriv->adc_items << 2);
11e865c1 1778
8ee52611
IA
1779 /*
1780 * If this slot's EndOfPollList flag is set, all channels have
1781 * now been processed.
1782 */
d8515652 1783 if (*ppl++ & S626_EOPL) {
07a36d66 1784 devpriv->adc_items++; /* Adjust poll list item count. */
8ee52611 1785 break; /* Exit poll list processing loop. */
020c44f3
HS
1786 }
1787 }
11e865c1 1788
8ee52611
IA
1789 /*
1790 * VERSION 2.01 CHANGE: DELAY CHANGED FROM 250NS to 2US. Allow the
020c44f3
HS
1791 * ADC to stabilize for 2 microseconds before starting the final
1792 * (dummy) conversion. This delay is necessary to allow sufficient
1793 * time between last conversion finished and the start of the dummy
1794 * conversion. Without this delay, the last conversion's data value
1795 * is sometimes set to the previous conversion's data value.
1796 */
d8515652
IA
1797 for (n = 0; n < (2 * S626_RPSCLK_PER_US); n++)
1798 *rps++ = S626_RPS_NOP;
11e865c1 1799
8ee52611
IA
1800 /*
1801 * Start a dummy conversion to cause the data from the last
020c44f3
HS
1802 * conversion of interest to be shifted in.
1803 */
d8515652
IA
1804 /* Begin ADC Start pulse. */
1805 *rps++ = S626_RPS_LDREG | (S626_P_GPIO >> 2);
1806 *rps++ = S626_GPIO_BASE | S626_GPIO1_LO;
1807 *rps++ = S626_RPS_NOP;
020c44f3 1808 /* VERSION 2.03 CHANGE: STRETCH OUT ADC START PULSE. */
d8515652
IA
1809 *rps++ = S626_RPS_LDREG | (S626_P_GPIO >> 2); /* End ADC Start pulse. */
1810 *rps++ = S626_GPIO_BASE | S626_GPIO1_HI;
11e865c1 1811
8ee52611
IA
1812 /*
1813 * Wait for the data from the last conversion of interest to arrive
020c44f3
HS
1814 * in FB BUFFER 1 register.
1815 */
d8515652 1816 *rps++ = S626_RPS_PAUSE | S626_RPS_GPIO2; /* Wait for ADC done. */
11e865c1 1817
8ee52611 1818 /* Transfer final ADC data from FB BUFFER 1 register to DMA buffer. */
d8515652 1819 *rps++ = S626_RPS_STREG | (S626_BUGFIX_STREG(S626_P_FB_BUFFER1) >> 2);
f1f7efce
IA
1820 *rps++ = (uint32_t)devpriv->ana_buf.physical_base +
1821 (devpriv->adc_items << 2);
11e865c1 1822
8ee52611
IA
1823 /* Indicate ADC scan loop is finished. */
1824 /* Signal ReadADC() that scan is done. */
d8515652 1825 /* *rps++= S626_RPS_CLRSIGNAL | S626_RPS_SIGADC; */
11e865c1 1826
020c44f3 1827 /* invoke interrupt */
8ee52611 1828 if (devpriv->ai_cmd_running == 1)
d8515652 1829 *rps++ = S626_RPS_IRQ;
11e865c1 1830
8ee52611 1831 /* Restart RPS program at its beginning. */
d8515652 1832 *rps++ = S626_RPS_JUMP; /* Branch to start of RPS program. */
f1f7efce 1833 *rps++ = (uint32_t)devpriv->rps_buf.physical_base;
8ee52611
IA
1834
1835 /* End of RPS program build */
020c44f3 1836}
11e865c1 1837
e4632a71
HS
1838#ifdef unused_code
1839static int s626_ai_rinsn(struct comedi_device *dev,
1840 struct comedi_subdevice *s,
1841 struct comedi_insn *insn,
1842 unsigned int *data)
1843{
1844 struct s626_private *devpriv = dev->private;
8ee52611
IA
1845 uint8_t i;
1846 int32_t *readaddr;
11e865c1 1847
ddd9813e 1848 /* Trigger ADC scan loop start */
d8515652 1849 s626_mc_enable(dev, S626_MC2_ADC_RPS, S626_P_MC2);
11e865c1 1850
e4632a71 1851 /* Wait until ADC scan loop is finished (RPS Signal 0 reset) */
d8515652 1852 while (s626_mc_test(dev, S626_MC2_ADC_RPS, S626_P_MC2))
e4632a71 1853 ;
11e865c1 1854
e4632a71
HS
1855 /*
1856 * Init ptr to DMA buffer that holds new ADC data. We skip the
1857 * first uint16_t in the buffer because it contains junk data from
1858 * the final ADC of the previous poll list scan.
1859 */
07a36d66 1860 readaddr = (uint32_t *)devpriv->ana_buf.logical_base + 1;
11e865c1 1861
e4632a71
HS
1862 /*
1863 * Convert ADC data to 16-bit integer values and
1864 * copy to application buffer.
1865 */
07a36d66 1866 for (i = 0; i < devpriv->adc_items; i++) {
e4632a71
HS
1867 *data = s626_ai_reg_to_uint(*readaddr++);
1868 data++;
1869 }
11e865c1 1870
e4632a71
HS
1871 return i;
1872}
1873#endif
11e865c1 1874
020c44f3
HS
1875static int s626_ai_insn_read(struct comedi_device *dev,
1876 struct comedi_subdevice *s,
1877 struct comedi_insn *insn, unsigned int *data)
1878{
7f2f7e05 1879 struct s626_private *devpriv = dev->private;
020c44f3
HS
1880 uint16_t chan = CR_CHAN(insn->chanspec);
1881 uint16_t range = CR_RANGE(insn->chanspec);
f1f7efce
IA
1882 uint16_t adc_spec = 0;
1883 uint32_t gpio_image;
be008602 1884 int tmp;
020c44f3 1885 int n;
11e865c1 1886
8ee52611
IA
1887 /*
1888 * Convert application's ADC specification into form
020c44f3
HS
1889 * appropriate for register programming.
1890 */
1891 if (range == 0)
d8515652 1892 adc_spec = (chan << 8) | (S626_GSEL_BIPOLAR5V);
020c44f3 1893 else
d8515652 1894 adc_spec = (chan << 8) | (S626_GSEL_BIPOLAR10V);
11e865c1 1895
8ee52611 1896 /* Switch ADC analog gain. */
d8515652 1897 s626_debi_write(dev, S626_LP_GSEL, adc_spec); /* Set gain. */
11e865c1 1898
8ee52611 1899 /* Select ADC analog input channel. */
d8515652 1900 s626_debi_write(dev, S626_LP_ISEL, adc_spec); /* Select channel. */
11e865c1 1901
020c44f3 1902 for (n = 0; n < insn->n; n++) {
8ee52611 1903 /* Delay 10 microseconds for analog input settling. */
020c44f3 1904 udelay(10);
11e865c1 1905
be008602 1906 /* Start ADC by pulsing GPIO1 low */
d8515652 1907 gpio_image = readl(devpriv->mmio + S626_P_GPIO);
25f8fd5e 1908 /* Assert ADC Start command */
d8515652
IA
1909 writel(gpio_image & ~S626_GPIO1_HI,
1910 devpriv->mmio + S626_P_GPIO);
25f8fd5e 1911 /* and stretch it out */
d8515652
IA
1912 writel(gpio_image & ~S626_GPIO1_HI,
1913 devpriv->mmio + S626_P_GPIO);
1914 writel(gpio_image & ~S626_GPIO1_HI,
1915 devpriv->mmio + S626_P_GPIO);
25f8fd5e 1916 /* Negate ADC Start command */
d8515652 1917 writel(gpio_image | S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
11e865c1 1918
8ee52611
IA
1919 /*
1920 * Wait for ADC to complete (GPIO2 is asserted high when
1921 * ADC not busy) and for data from previous conversion to
1922 * shift into FB BUFFER 1 register.
1923 */
11e865c1 1924
be008602 1925 /* Wait for ADC done */
d8515652 1926 while (!(readl(devpriv->mmio + S626_P_PSR) & S626_PSR_GPIO2))
020c44f3 1927 ;
11e865c1 1928
be008602
HS
1929 /* Fetch ADC data */
1930 if (n != 0) {
d8515652 1931 tmp = readl(devpriv->mmio + S626_P_FB_BUFFER1);
be008602
HS
1932 data[n - 1] = s626_ai_reg_to_uint(tmp);
1933 }
11e865c1 1934
8ee52611
IA
1935 /*
1936 * Allow the ADC to stabilize for 4 microseconds before
020c44f3
HS
1937 * starting the next (final) conversion. This delay is
1938 * necessary to allow sufficient time between last
1939 * conversion finished and the start of the next
1940 * conversion. Without this delay, the last conversion's
1941 * data value is sometimes set to the previous
1942 * conversion's data value.
1943 */
1944 udelay(4);
1945 }
11e865c1 1946
8ee52611
IA
1947 /*
1948 * Start a dummy conversion to cause the data from the
1949 * previous conversion to be shifted in.
1950 */
d8515652 1951 gpio_image = readl(devpriv->mmio + S626_P_GPIO);
020c44f3 1952 /* Assert ADC Start command */
d8515652 1953 writel(gpio_image & ~S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
25f8fd5e 1954 /* and stretch it out */
d8515652
IA
1955 writel(gpio_image & ~S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
1956 writel(gpio_image & ~S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
25f8fd5e 1957 /* Negate ADC Start command */
d8515652 1958 writel(gpio_image | S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
11e865c1 1959
8ee52611 1960 /* Wait for the data to arrive in FB BUFFER 1 register. */
11e865c1 1961
be008602 1962 /* Wait for ADC done */
d8515652 1963 while (!(readl(devpriv->mmio + S626_P_PSR) & S626_PSR_GPIO2))
020c44f3 1964 ;
11e865c1 1965
8ee52611 1966 /* Fetch ADC data from audio interface's input shift register. */
11e865c1 1967
be008602
HS
1968 /* Fetch ADC data */
1969 if (n != 0) {
d8515652 1970 tmp = readl(devpriv->mmio + S626_P_FB_BUFFER1);
be008602
HS
1971 data[n - 1] = s626_ai_reg_to_uint(tmp);
1972 }
11e865c1 1973
020c44f3
HS
1974 return n;
1975}
11e865c1 1976
020c44f3
HS
1977static int s626_ai_load_polllist(uint8_t *ppl, struct comedi_cmd *cmd)
1978{
020c44f3 1979 int n;
11e865c1 1980
020c44f3 1981 for (n = 0; n < cmd->chanlist_len; n++) {
8ee52611 1982 if (CR_RANGE(cmd->chanlist[n]) == 0)
d8515652 1983 ppl[n] = CR_CHAN(cmd->chanlist[n]) | S626_RANGE_5V;
020c44f3 1984 else
d8515652 1985 ppl[n] = CR_CHAN(cmd->chanlist[n]) | S626_RANGE_10V;
020c44f3
HS
1986 }
1987 if (n != 0)
d8515652 1988 ppl[n - 1] |= S626_EOPL;
11e865c1 1989
020c44f3
HS
1990 return n;
1991}
11e865c1 1992
020c44f3
HS
1993static int s626_ai_inttrig(struct comedi_device *dev,
1994 struct comedi_subdevice *s, unsigned int trignum)
1995{
1996 if (trignum != 0)
1997 return -EINVAL;
11e865c1 1998
ddd9813e 1999 /* Start executing the RPS program */
d8515652 2000 s626_mc_enable(dev, S626_MC1_ERPS1, S626_P_MC1);
11e865c1 2001
020c44f3 2002 s->async->inttrig = NULL;
11e865c1 2003
020c44f3
HS
2004 return 1;
2005}
11e865c1 2006
8ee52611
IA
2007/*
2008 * This function doesn't require a particular form, this is just what
6baffbc2
HS
2009 * happens to be used in some of the drivers. It should convert ns
2010 * nanoseconds to a counter value suitable for programming the device.
2011 * Also, it should adjust ns so that it cooresponds to the actual time
8ee52611
IA
2012 * that the device will use.
2013 */
6baffbc2
HS
2014static int s626_ns_to_timer(int *nanosec, int round_mode)
2015{
2016 int divider, base;
2017
2018 base = 500; /* 2MHz internal clock */
2019
2020 switch (round_mode) {
2021 case TRIG_ROUND_NEAREST:
2022 default:
2023 divider = (*nanosec + base / 2) / base;
2024 break;
2025 case TRIG_ROUND_DOWN:
2026 divider = (*nanosec) / base;
2027 break;
2028 case TRIG_ROUND_UP:
2029 divider = (*nanosec + base - 1) / base;
2030 break;
2031 }
2032
2033 *nanosec = base * divider;
2034 return divider - 1;
2035}
2036
3a305a66
IA
2037static void s626_timer_load(struct comedi_device *dev,
2038 const struct s626_enc_info *k, int tick)
e3eb08d0 2039{
f1f7efce 2040 uint16_t setup =
d8515652
IA
2041 /* Preload upon index. */
2042 (S626_LOADSRC_INDX << S626_BF_LOADSRC) |
2043 /* Disable hardware index. */
2044 (S626_INDXSRC_SOFT << S626_BF_INDXSRC) |
2045 /* Operating mode is Timer. */
2046 (S626_CLKSRC_TIMER << S626_BF_CLKSRC) |
2047 /* Active high clock. */
2048 (S626_CLKPOL_POS << S626_BF_CLKPOL) |
2049 /* Count direction is Down. */
2050 (S626_CNTDIR_DOWN << S626_BF_CLKPOL) |
2051 /* Clock multiplier is 1x. */
2052 (S626_CLKMULT_1X << S626_BF_CLKMULT) |
2053 (S626_CLKENAB_INDEX << S626_BF_CLKENAB);
2054 uint16_t value_latchsrc = S626_LATCHSRC_A_INDXA;
2055 /* uint16_t enab = S626_CLKENAB_ALWAYS; */
e3eb08d0 2056
c3e3a56d 2057 k->set_mode(dev, k, setup, false);
e3eb08d0 2058
8ee52611 2059 /* Set the preload register */
31de1948 2060 s626_preload(dev, k, tick);
e3eb08d0 2061
8ee52611
IA
2062 /*
2063 * Software index pulse forces the preload register to load
2064 * into the counter
2065 */
b075ac8e
IA
2066 k->set_load_trig(dev, k, 0);
2067 k->pulse_index(dev, k);
e3eb08d0
HS
2068
2069 /* set reload on counter overflow */
b075ac8e 2070 k->set_load_trig(dev, k, 1);
e3eb08d0
HS
2071
2072 /* set interrupt on overflow */
d8515652 2073 k->set_int_src(dev, k, S626_INTSRC_OVER);
e3eb08d0 2074
31de1948 2075 s626_set_latch_source(dev, k, value_latchsrc);
b075ac8e 2076 /* k->set_enable(dev, k, (uint16_t)(enab != 0)); */
e3eb08d0
HS
2077}
2078
8ee52611 2079/* TO COMPLETE */
020c44f3
HS
2080static int s626_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2081{
7f2f7e05 2082 struct s626_private *devpriv = dev->private;
020c44f3
HS
2083 uint8_t ppl[16];
2084 struct comedi_cmd *cmd = &s->async->cmd;
3a305a66 2085 const struct s626_enc_info *k;
020c44f3 2086 int tick;
11e865c1 2087
020c44f3 2088 if (devpriv->ai_cmd_running) {
730b8e15
IA
2089 dev_err(dev->class_dev,
2090 "s626_ai_cmd: Another ai_cmd is running\n");
020c44f3
HS
2091 return -EBUSY;
2092 }
2093 /* disable interrupt */
d8515652 2094 writel(0, devpriv->mmio + S626_P_IER);
11e865c1 2095
020c44f3 2096 /* clear interrupt request */
d8515652 2097 writel(S626_IRQ_RPS1 | S626_IRQ_GPIO3, devpriv->mmio + S626_P_ISR);
11e865c1 2098
020c44f3
HS
2099 /* clear any pending interrupt */
2100 s626_dio_clear_irq(dev);
8ee52611 2101 /* s626_enc_clear_irq(dev); */
11e865c1 2102
020c44f3
HS
2103 /* reset ai_cmd_running flag */
2104 devpriv->ai_cmd_running = 0;
11e865c1 2105
8ee52611 2106 /* test if cmd is valid */
bdf5aa39 2107 if (cmd == NULL)
020c44f3 2108 return -EINVAL;
11e865c1 2109
020c44f3
HS
2110 if (dev->irq == 0) {
2111 comedi_error(dev,
2112 "s626_ai_cmd: cannot run command without an irq");
2113 return -EIO;
2114 }
11e865c1 2115
020c44f3
HS
2116 s626_ai_load_polllist(ppl, cmd);
2117 devpriv->ai_cmd_running = 1;
2118 devpriv->ai_convert_count = 0;
11e865c1 2119
020c44f3
HS
2120 switch (cmd->scan_begin_src) {
2121 case TRIG_FOLLOW:
2122 break;
2123 case TRIG_TIMER:
8ee52611
IA
2124 /*
2125 * set a counter to generate adc trigger at scan_begin_arg
2126 * interval
2127 */
3a305a66 2128 k = &s626_enc_chan_info[5];
020c44f3
HS
2129 tick = s626_ns_to_timer((int *)&cmd->scan_begin_arg,
2130 cmd->flags & TRIG_ROUND_MASK);
11e865c1 2131
020c44f3
HS
2132 /* load timer value and enable interrupt */
2133 s626_timer_load(dev, k, tick);
d8515652 2134 k->set_enable(dev, k, S626_CLKENAB_ALWAYS);
020c44f3
HS
2135 break;
2136 case TRIG_EXT:
8ee52611 2137 /* set the digital line and interrupt for scan trigger */
020c44f3
HS
2138 if (cmd->start_src != TRIG_EXT)
2139 s626_dio_set_irq(dev, cmd->scan_begin_arg);
020c44f3
HS
2140 break;
2141 }
11e865c1 2142
020c44f3
HS
2143 switch (cmd->convert_src) {
2144 case TRIG_NOW:
2145 break;
2146 case TRIG_TIMER:
8ee52611
IA
2147 /*
2148 * set a counter to generate adc trigger at convert_arg
2149 * interval
2150 */
3a305a66 2151 k = &s626_enc_chan_info[4];
020c44f3
HS
2152 tick = s626_ns_to_timer((int *)&cmd->convert_arg,
2153 cmd->flags & TRIG_ROUND_MASK);
11e865c1 2154
020c44f3
HS
2155 /* load timer value and enable interrupt */
2156 s626_timer_load(dev, k, tick);
d8515652 2157 k->set_enable(dev, k, S626_CLKENAB_INDEX);
020c44f3
HS
2158 break;
2159 case TRIG_EXT:
8ee52611
IA
2160 /* set the digital line and interrupt for convert trigger */
2161 if (cmd->scan_begin_src != TRIG_EXT &&
2162 cmd->start_src == TRIG_EXT)
020c44f3 2163 s626_dio_set_irq(dev, cmd->convert_arg);
020c44f3
HS
2164 break;
2165 }
11e865c1 2166
020c44f3
HS
2167 switch (cmd->stop_src) {
2168 case TRIG_COUNT:
8ee52611 2169 /* data arrives as one packet */
020c44f3 2170 devpriv->ai_sample_count = cmd->stop_arg;
e6132fc9 2171 devpriv->ai_continuous = 0;
020c44f3
HS
2172 break;
2173 case TRIG_NONE:
8ee52611 2174 /* continuous acquisition */
e6132fc9 2175 devpriv->ai_continuous = 1;
e4317ce8 2176 devpriv->ai_sample_count = 1;
020c44f3 2177 break;
11e865c1 2178 }
11e865c1 2179
31de1948 2180 s626_reset_adc(dev, ppl);
11e865c1 2181
020c44f3
HS
2182 switch (cmd->start_src) {
2183 case TRIG_NOW:
ddd9813e 2184 /* Trigger ADC scan loop start */
d8515652 2185 /* s626_mc_enable(dev, S626_MC2_ADC_RPS, S626_P_MC2); */
11e865c1 2186
ddd9813e 2187 /* Start executing the RPS program */
d8515652 2188 s626_mc_enable(dev, S626_MC1_ERPS1, S626_P_MC1);
020c44f3
HS
2189 s->async->inttrig = NULL;
2190 break;
2191 case TRIG_EXT:
2192 /* configure DIO channel for acquisition trigger */
2193 s626_dio_set_irq(dev, cmd->start_arg);
020c44f3
HS
2194 s->async->inttrig = NULL;
2195 break;
2196 case TRIG_INT:
2197 s->async->inttrig = s626_ai_inttrig;
2198 break;
11e865c1 2199 }
b6c77757 2200
020c44f3 2201 /* enable interrupt */
d8515652 2202 writel(S626_IRQ_GPIO3 | S626_IRQ_RPS1, devpriv->mmio + S626_P_IER);
b6c77757 2203
020c44f3
HS
2204 return 0;
2205}
b6c77757 2206
020c44f3
HS
2207static int s626_ai_cmdtest(struct comedi_device *dev,
2208 struct comedi_subdevice *s, struct comedi_cmd *cmd)
2209{
2210 int err = 0;
2211 int tmp;
b6c77757 2212
27020ffe 2213 /* Step 1 : check if triggers are trivially valid */
b6c77757 2214
27020ffe 2215 err |= cfc_check_trigger_src(&cmd->start_src,
8ee52611 2216 TRIG_NOW | TRIG_INT | TRIG_EXT);
27020ffe 2217 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
8ee52611 2218 TRIG_TIMER | TRIG_EXT | TRIG_FOLLOW);
27020ffe 2219 err |= cfc_check_trigger_src(&cmd->convert_src,
8ee52611 2220 TRIG_TIMER | TRIG_EXT | TRIG_NOW);
27020ffe
HS
2221 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2222 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
11e865c1 2223
020c44f3
HS
2224 if (err)
2225 return 1;
11e865c1 2226
27020ffe 2227 /* Step 2a : make sure trigger sources are unique */
11e865c1 2228
27020ffe
HS
2229 err |= cfc_check_trigger_is_unique(cmd->start_src);
2230 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
2231 err |= cfc_check_trigger_is_unique(cmd->convert_src);
2232 err |= cfc_check_trigger_is_unique(cmd->stop_src);
2233
2234 /* Step 2b : and mutually compatible */
020c44f3
HS
2235
2236 if (err)
2237 return 2;
2238
2239 /* step 3: make sure arguments are trivially compatible */
2240
53a254b9
HS
2241 if (cmd->start_src != TRIG_EXT)
2242 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
2243 if (cmd->start_src == TRIG_EXT)
2244 err |= cfc_check_trigger_arg_max(&cmd->start_arg, 39);
53a254b9
HS
2245 if (cmd->scan_begin_src == TRIG_EXT)
2246 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 39);
53a254b9
HS
2247 if (cmd->convert_src == TRIG_EXT)
2248 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, 39);
11e865c1 2249
676921c9
IA
2250#define S626_MAX_SPEED 200000 /* in nanoseconds */
2251#define S626_MIN_SPEED 2000000000 /* in nanoseconds */
11e865c1 2252
020c44f3 2253 if (cmd->scan_begin_src == TRIG_TIMER) {
53a254b9 2254 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
676921c9 2255 S626_MAX_SPEED);
53a254b9 2256 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
676921c9 2257 S626_MIN_SPEED);
020c44f3
HS
2258 } else {
2259 /* external trigger */
2260 /* should be level/edge, hi/lo specification here */
2261 /* should specify multiple external triggers */
8ee52611 2262 /* err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9); */
020c44f3
HS
2263 }
2264 if (cmd->convert_src == TRIG_TIMER) {
676921c9
IA
2265 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
2266 S626_MAX_SPEED);
2267 err |= cfc_check_trigger_arg_max(&cmd->convert_arg,
2268 S626_MIN_SPEED);
020c44f3
HS
2269 } else {
2270 /* external trigger */
2271 /* see above */
8ee52611 2272 /* err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9); */
020c44f3 2273 }
11e865c1 2274
53a254b9
HS
2275 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
2276
2277 if (cmd->stop_src == TRIG_COUNT)
2278 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
2279 else /* TRIG_NONE */
2280 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
11e865c1 2281
020c44f3
HS
2282 if (err)
2283 return 3;
2284
2285 /* step 4: fix up any arguments */
2286
2287 if (cmd->scan_begin_src == TRIG_TIMER) {
2288 tmp = cmd->scan_begin_arg;
2289 s626_ns_to_timer((int *)&cmd->scan_begin_arg,
2290 cmd->flags & TRIG_ROUND_MASK);
2291 if (tmp != cmd->scan_begin_arg)
2292 err++;
2293 }
2294 if (cmd->convert_src == TRIG_TIMER) {
2295 tmp = cmd->convert_arg;
2296 s626_ns_to_timer((int *)&cmd->convert_arg,
2297 cmd->flags & TRIG_ROUND_MASK);
2298 if (tmp != cmd->convert_arg)
2299 err++;
2300 if (cmd->scan_begin_src == TRIG_TIMER &&
8ee52611
IA
2301 cmd->scan_begin_arg < cmd->convert_arg *
2302 cmd->scan_end_arg) {
2303 cmd->scan_begin_arg = cmd->convert_arg *
2304 cmd->scan_end_arg;
020c44f3
HS
2305 err++;
2306 }
11e865c1 2307 }
11e865c1 2308
020c44f3
HS
2309 if (err)
2310 return 4;
2311
2312 return 0;
11e865c1
GP
2313}
2314
020c44f3 2315static int s626_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
11e865c1 2316{
7f2f7e05
HS
2317 struct s626_private *devpriv = dev->private;
2318
c5cf4606 2319 /* Stop RPS program in case it is currently running */
d8515652 2320 s626_mc_disable(dev, S626_MC1_ERPS1, S626_P_MC1);
11e865c1 2321
020c44f3 2322 /* disable master interrupt */
d8515652 2323 writel(0, devpriv->mmio + S626_P_IER);
11e865c1 2324
020c44f3 2325 devpriv->ai_cmd_running = 0;
11e865c1 2326
020c44f3
HS
2327 return 0;
2328}
11e865c1 2329
020c44f3
HS
2330static int s626_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
2331 struct comedi_insn *insn, unsigned int *data)
11e865c1 2332{
7f2f7e05 2333 struct s626_private *devpriv = dev->private;
020c44f3 2334 int i;
11e865c1 2335 uint16_t chan = CR_CHAN(insn->chanspec);
020c44f3 2336 int16_t dacdata;
11e865c1 2337
020c44f3
HS
2338 for (i = 0; i < insn->n; i++) {
2339 dacdata = (int16_t) data[i];
2340 devpriv->ao_readback[CR_CHAN(insn->chanspec)] = data[i];
2341 dacdata -= (0x1fff);
11e865c1 2342
31de1948 2343 s626_set_dac(dev, chan, dacdata);
020c44f3 2344 }
11e865c1 2345
020c44f3
HS
2346 return i;
2347}
11e865c1 2348
020c44f3
HS
2349static int s626_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
2350 struct comedi_insn *insn, unsigned int *data)
2351{
7f2f7e05 2352 struct s626_private *devpriv = dev->private;
020c44f3 2353 int i;
11e865c1 2354
020c44f3
HS
2355 for (i = 0; i < insn->n; i++)
2356 data[i] = devpriv->ao_readback[CR_CHAN(insn->chanspec)];
11e865c1 2357
020c44f3
HS
2358 return i;
2359}
11e865c1 2360
8ee52611
IA
2361/* *************** DIGITAL I/O FUNCTIONS *************** */
2362
2363/*
020c44f3
HS
2364 * All DIO functions address a group of DIO channels by means of
2365 * "group" argument. group may be 0, 1 or 2, which correspond to DIO
2366 * ports A, B and C, respectively.
2367 */
11e865c1 2368
020c44f3
HS
2369static void s626_dio_init(struct comedi_device *dev)
2370{
2371 uint16_t group;
11e865c1 2372
8ee52611 2373 /* Prepare to treat writes to WRCapSel as capture disables. */
d8515652 2374 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_NOEDCAP);
11e865c1 2375
8ee52611 2376 /* For each group of sixteen channels ... */
020c44f3 2377 for (group = 0; group < S626_DIO_BANKS; group++) {
100b4edc 2378 /* Disable all interrupts */
d8515652 2379 s626_debi_write(dev, S626_LP_WRINTSEL(group), 0);
100b4edc 2380 /* Disable all event captures */
d8515652 2381 s626_debi_write(dev, S626_LP_WRCAPSEL(group), 0xffff);
100b4edc 2382 /* Init all DIOs to default edge polarity */
d8515652 2383 s626_debi_write(dev, S626_LP_WREDGSEL(group), 0);
100b4edc 2384 /* Program all outputs to inactive state */
d8515652 2385 s626_debi_write(dev, S626_LP_WRDOUT(group), 0);
11e865c1 2386 }
020c44f3 2387}
11e865c1 2388
020c44f3
HS
2389static int s626_dio_insn_bits(struct comedi_device *dev,
2390 struct comedi_subdevice *s,
1515e522
HS
2391 struct comedi_insn *insn,
2392 unsigned int *data)
020c44f3 2393{
100b4edc 2394 unsigned long group = (unsigned long)s->private;
11e865c1 2395
6ea79c1d 2396 if (comedi_dio_update_state(s, data))
d8515652 2397 s626_debi_write(dev, S626_LP_WRDOUT(group), s->state);
6ea79c1d 2398
d8515652 2399 data[1] = s626_debi_read(dev, S626_LP_RDDIN(group));
11e865c1 2400
020c44f3 2401 return insn->n;
11e865c1
GP
2402}
2403
020c44f3
HS
2404static int s626_dio_insn_config(struct comedi_device *dev,
2405 struct comedi_subdevice *s,
e920fad2
HS
2406 struct comedi_insn *insn,
2407 unsigned int *data)
11e865c1 2408{
100b4edc 2409 unsigned long group = (unsigned long)s->private;
ddf62f2c
HS
2410 int ret;
2411
2412 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
2413 if (ret)
2414 return ret;
11e865c1 2415
d8515652 2416 s626_debi_write(dev, S626_LP_WRDOUT(group), s->io_bits);
11e865c1 2417
e920fad2 2418 return insn->n;
11e865c1
GP
2419}
2420
8ee52611
IA
2421/*
2422 * Now this function initializes the value of the counter (data[0])
2423 * and set the subdevice. To complete with trigger and interrupt
2424 * configuration.
2425 *
2426 * FIXME: data[0] is supposed to be an INSN_CONFIG_xxx constant indicating
affdc230 2427 * what is being configured, but this function appears to be using data[0]
8ee52611
IA
2428 * as a variable.
2429 */
020c44f3
HS
2430static int s626_enc_insn_config(struct comedi_device *dev,
2431 struct comedi_subdevice *s,
2432 struct comedi_insn *insn, unsigned int *data)
2433{
f1f7efce 2434 uint16_t setup =
d8515652
IA
2435 /* Preload upon index. */
2436 (S626_LOADSRC_INDX << S626_BF_LOADSRC) |
2437 /* Disable hardware index. */
2438 (S626_INDXSRC_SOFT << S626_BF_INDXSRC) |
2439 /* Operating mode is Counter. */
2440 (S626_CLKSRC_COUNTER << S626_BF_CLKSRC) |
2441 /* Active high clock. */
2442 (S626_CLKPOL_POS << S626_BF_CLKPOL) |
2443 /* Clock multiplier is 1x. */
2444 (S626_CLKMULT_1X << S626_BF_CLKMULT) |
2445 (S626_CLKENAB_INDEX << S626_BF_CLKENAB);
c3e3a56d 2446 /* uint16_t disable_int_src = true; */
8ee52611 2447 /* uint32_t Preloadvalue; //Counter initial value */
d8515652
IA
2448 uint16_t value_latchsrc = S626_LATCHSRC_AB_READ;
2449 uint16_t enab = S626_CLKENAB_ALWAYS;
3a305a66
IA
2450 const struct s626_enc_info *k =
2451 &s626_enc_chan_info[CR_CHAN(insn->chanspec)];
11e865c1 2452
8ee52611 2453 /* (data==NULL) ? (Preloadvalue=0) : (Preloadvalue=data[0]); */
11e865c1 2454
c3e3a56d 2455 k->set_mode(dev, k, setup, true);
31de1948 2456 s626_preload(dev, k, data[0]);
b075ac8e 2457 k->pulse_index(dev, k);
31de1948 2458 s626_set_latch_source(dev, k, value_latchsrc);
b075ac8e 2459 k->set_enable(dev, k, (enab != 0));
11e865c1 2460
020c44f3
HS
2461 return insn->n;
2462}
11e865c1 2463
020c44f3
HS
2464static int s626_enc_insn_read(struct comedi_device *dev,
2465 struct comedi_subdevice *s,
2466 struct comedi_insn *insn, unsigned int *data)
2467{
020c44f3 2468 int n;
3a305a66
IA
2469 const struct s626_enc_info *k =
2470 &s626_enc_chan_info[CR_CHAN(insn->chanspec)];
11e865c1 2471
020c44f3 2472 for (n = 0; n < insn->n; n++)
31de1948 2473 data[n] = s626_read_latch(dev, k);
11e865c1 2474
020c44f3
HS
2475 return n;
2476}
11e865c1 2477
020c44f3
HS
2478static int s626_enc_insn_write(struct comedi_device *dev,
2479 struct comedi_subdevice *s,
2480 struct comedi_insn *insn, unsigned int *data)
2481{
3a305a66
IA
2482 const struct s626_enc_info *k =
2483 &s626_enc_chan_info[CR_CHAN(insn->chanspec)];
11e865c1 2484
8ee52611 2485 /* Set the preload register */
31de1948 2486 s626_preload(dev, k, data[0]);
11e865c1 2487
8ee52611
IA
2488 /*
2489 * Software index pulse forces the preload register to load
2490 * into the counter
2491 */
b075ac8e
IA
2492 k->set_load_trig(dev, k, 0);
2493 k->pulse_index(dev, k);
2494 k->set_load_trig(dev, k, 2);
11e865c1 2495
020c44f3 2496 return 1;
11e865c1
GP
2497}
2498
31de1948 2499static void s626_write_misc2(struct comedi_device *dev, uint16_t new_image)
11e865c1 2500{
d8515652
IA
2501 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_WENABLE);
2502 s626_debi_write(dev, S626_LP_WRMISC2, new_image);
2503 s626_debi_write(dev, S626_LP_MISC1, S626_MISC1_WDISABLE);
020c44f3 2504}
11e865c1 2505
31de1948
IA
2506static void s626_close_dma_b(struct comedi_device *dev,
2507 struct s626_buffer_dma *pdma, size_t bsize)
020c44f3 2508{
f574af6d 2509 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
020c44f3
HS
2510 void *vbptr;
2511 dma_addr_t vpptr;
11e865c1 2512
020c44f3
HS
2513 if (pdma == NULL)
2514 return;
11e865c1 2515
8ee52611 2516 /* find the matching allocation from the board struct */
3a387506
IA
2517 vbptr = pdma->logical_base;
2518 vpptr = pdma->physical_base;
020c44f3 2519 if (vbptr) {
f574af6d 2520 pci_free_consistent(pcidev, bsize, vbptr, vpptr);
3a387506
IA
2521 pdma->logical_base = NULL;
2522 pdma->physical_base = 0;
020c44f3 2523 }
11e865c1
GP
2524}
2525
31de1948 2526static void s626_counters_init(struct comedi_device *dev)
11e865c1 2527{
020c44f3 2528 int chan;
3a305a66 2529 const struct s626_enc_info *k;
f1f7efce 2530 uint16_t setup =
d8515652
IA
2531 /* Preload upon index. */
2532 (S626_LOADSRC_INDX << S626_BF_LOADSRC) |
2533 /* Disable hardware index. */
2534 (S626_INDXSRC_SOFT << S626_BF_INDXSRC) |
2535 /* Operating mode is counter. */
2536 (S626_CLKSRC_COUNTER << S626_BF_CLKSRC) |
2537 /* Active high clock. */
2538 (S626_CLKPOL_POS << S626_BF_CLKPOL) |
2539 /* Count direction is up. */
2540 (S626_CNTDIR_UP << S626_BF_CLKPOL) |
2541 /* Clock multiplier is 1x. */
2542 (S626_CLKMULT_1X << S626_BF_CLKMULT) |
2543 /* Enabled by index */
2544 (S626_CLKENAB_INDEX << S626_BF_CLKENAB);
8ee52611
IA
2545
2546 /*
2547 * Disable all counter interrupts and clear any captured counter events.
2548 */
020c44f3 2549 for (chan = 0; chan < S626_ENCODER_CHANNELS; chan++) {
3a305a66 2550 k = &s626_enc_chan_info[chan];
c3e3a56d 2551 k->set_mode(dev, k, setup, true);
b075ac8e
IA
2552 k->set_int_src(dev, k, 0);
2553 k->reset_cap_flags(dev, k);
d8515652 2554 k->set_enable(dev, k, S626_CLKENAB_ALWAYS);
020c44f3 2555 }
020c44f3 2556}
11e865c1 2557
b7047895
HS
2558static int s626_allocate_dma_buffers(struct comedi_device *dev)
2559{
2560 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
7f2f7e05 2561 struct s626_private *devpriv = dev->private;
b7047895
HS
2562 void *addr;
2563 dma_addr_t appdma;
2564
d8515652 2565 addr = pci_alloc_consistent(pcidev, S626_DMABUF_SIZE, &appdma);
b7047895
HS
2566 if (!addr)
2567 return -ENOMEM;
07a36d66
IA
2568 devpriv->ana_buf.logical_base = addr;
2569 devpriv->ana_buf.physical_base = appdma;
b7047895 2570
d8515652 2571 addr = pci_alloc_consistent(pcidev, S626_DMABUF_SIZE, &appdma);
b7047895
HS
2572 if (!addr)
2573 return -ENOMEM;
07a36d66
IA
2574 devpriv->rps_buf.logical_base = addr;
2575 devpriv->rps_buf.physical_base = appdma;
b7047895 2576
b7047895
HS
2577 return 0;
2578}
2579
80ec9510 2580static void s626_initialize(struct comedi_device *dev)
020c44f3 2581{
7f2f7e05 2582 struct s626_private *devpriv = dev->private;
f1f7efce 2583 dma_addr_t phys_buf;
68ad0ae0 2584 uint16_t chan;
020c44f3 2585 int i;
11e865c1 2586
54a2a02e 2587 /* Enable DEBI and audio pins, enable I2C interface */
d8515652
IA
2588 s626_mc_enable(dev, S626_MC1_DEBI | S626_MC1_AUDIO | S626_MC1_I2C,
2589 S626_P_MC1);
54a2a02e
HS
2590
2591 /*
8ee52611 2592 * Configure DEBI operating mode
54a2a02e 2593 *
8ee52611
IA
2594 * Local bus is 16 bits wide
2595 * Declare DEBI transfer timeout interval
2596 * Set up byte lane steering
2597 * Intel-compatible local bus (DEBI never times out)
54a2a02e 2598 */
d8515652
IA
2599 writel(S626_DEBI_CFG_SLAVE16 |
2600 (S626_DEBI_TOUT << S626_DEBI_CFG_TOUT_BIT) | S626_DEBI_SWAP |
2601 S626_DEBI_CFG_INTEL, devpriv->mmio + S626_P_DEBICFG);
54a2a02e
HS
2602
2603 /* Disable MMU paging */
d8515652 2604 writel(S626_DEBI_PAGE_DISABLE, devpriv->mmio + S626_P_DEBIPAGE);
54a2a02e
HS
2605
2606 /* Init GPIO so that ADC Start* is negated */
d8515652 2607 writel(S626_GPIO_BASE | S626_GPIO1_HI, devpriv->mmio + S626_P_GPIO);
68ad0ae0 2608
17553c88 2609 /* I2C device address for onboard eeprom (revb) */
07a36d66 2610 devpriv->i2c_adrs = 0xA0;
11e865c1 2611
54a2a02e
HS
2612 /*
2613 * Issue an I2C ABORT command to halt any I2C
2614 * operation in progress and reset BUSY flag.
2615 */
d8515652
IA
2616 writel(S626_I2C_CLKSEL | S626_I2C_ABORT,
2617 devpriv->mmio + S626_P_I2CSTAT);
2618 s626_mc_enable(dev, S626_MC2_UPLD_IIC, S626_P_MC2);
2619 while (!(readl(devpriv->mmio + S626_P_MC2) & S626_MC2_UPLD_IIC))
68ad0ae0 2620 ;
68ad0ae0 2621
54a2a02e
HS
2622 /*
2623 * Per SAA7146 data sheet, write to STATUS
2624 * reg twice to reset all I2C error flags.
2625 */
68ad0ae0 2626 for (i = 0; i < 2; i++) {
d8515652
IA
2627 writel(S626_I2C_CLKSEL, devpriv->mmio + S626_P_I2CSTAT);
2628 s626_mc_enable(dev, S626_MC2_UPLD_IIC, S626_P_MC2);
2629 while (!s626_mc_test(dev, S626_MC2_UPLD_IIC, S626_P_MC2))
020c44f3 2630 ;
68ad0ae0 2631 }
11e865c1 2632
54a2a02e
HS
2633 /*
2634 * Init audio interface functional attributes: set DAC/ADC
68ad0ae0
HS
2635 * serial clock rates, invert DAC serial clock so that
2636 * DAC data setup times are satisfied, enable DAC serial
2637 * clock out.
2638 */
d8515652 2639 writel(S626_ACON2_INIT, devpriv->mmio + S626_P_ACON2);
11e865c1 2640
54a2a02e
HS
2641 /*
2642 * Set up TSL1 slot list, which is used to control the
d8515652
IA
2643 * accumulation of ADC data: S626_RSD1 = shift data in on SD1.
2644 * S626_SIB_A1 = store data uint8_t at next available location
54a2a02e
HS
2645 * in FB BUFFER1 register.
2646 */
d8515652
IA
2647 writel(S626_RSD1 | S626_SIB_A1, devpriv->mmio + S626_P_TSL1);
2648 writel(S626_RSD1 | S626_SIB_A1 | S626_EOS,
2649 devpriv->mmio + S626_P_TSL1 + 4);
11e865c1 2650
54a2a02e 2651 /* Enable TSL1 slot list so that it executes all the time */
d8515652 2652 writel(S626_ACON1_ADCSTART, devpriv->mmio + S626_P_ACON1);
11e865c1 2653
54a2a02e
HS
2654 /*
2655 * Initialize RPS registers used for ADC
2656 */
11e865c1 2657
54a2a02e 2658 /* Physical start of RPS program */
07a36d66 2659 writel((uint32_t)devpriv->rps_buf.physical_base,
d8515652 2660 devpriv->mmio + S626_P_RPSADDR1);
54a2a02e 2661 /* RPS program performs no explicit mem writes */
d8515652 2662 writel(0, devpriv->mmio + S626_P_RPSPAGE1);
54a2a02e 2663 /* Disable RPS timeouts */
d8515652 2664 writel(0, devpriv->mmio + S626_P_RPS1_TOUT);
11e865c1 2665
59747847
HS
2666#if 0
2667 /*
2668 * SAA7146 BUG WORKAROUND
2669 *
2670 * Initialize SAA7146 ADC interface to a known state by
2671 * invoking ADCs until FB BUFFER 1 register shows that it
2672 * is correctly receiving ADC data. This is necessary
2673 * because the SAA7146 ADC interface does not start up in
2674 * a defined state after a PCI reset.
68ad0ae0 2675 */
59747847 2676 {
f1f7efce
IA
2677 uint8_t poll_list;
2678 uint16_t adc_data;
2679 uint16_t start_val;
8ee52611
IA
2680 uint16_t index;
2681 unsigned int data[16];
59747847 2682
8ee52611 2683 /* Create a simple polling list for analog input channel 0 */
d8515652 2684 poll_list = S626_EOPL;
31de1948 2685 s626_reset_adc(dev, &poll_list);
59747847 2686
8ee52611 2687 /* Get initial ADC value */
59747847 2688 s626_ai_rinsn(dev, dev->subdevices, NULL, data);
f1f7efce 2689 start_val = data[0];
59747847 2690
8ee52611
IA
2691 /*
2692 * VERSION 2.01 CHANGE: TIMEOUT ADDED TO PREVENT HANGED
2693 * EXECUTION.
2694 *
2695 * Invoke ADCs until the new ADC value differs from the initial
2696 * value or a timeout occurs. The timeout protects against the
2697 * possibility that the driver is restarting and the ADC data is
2698 * a fixed value resulting from the applied ADC analog input
2699 * being unusually quiet or at the rail.
2700 */
2701 for (index = 0; index < 500; index++) {
2702 s626_ai_rinsn(dev, dev->subdevices, NULL, data);
f1f7efce
IA
2703 adc_data = data[0];
2704 if (adc_data != start_val)
8ee52611
IA
2705 break;
2706 }
59747847
HS
2707 }
2708#endif /* SAA7146 BUG WORKAROUND */
11e865c1 2709
54a2a02e
HS
2710 /*
2711 * Initialize the DAC interface
2712 */
11e865c1 2713
54a2a02e
HS
2714 /*
2715 * Init Audio2's output DMAC attributes:
2716 * burst length = 1 DWORD
2717 * threshold = 1 DWORD.
68ad0ae0 2718 */
d8515652 2719 writel(0, devpriv->mmio + S626_P_PCI_BT_A);
68ad0ae0 2720
54a2a02e
HS
2721 /*
2722 * Init Audio2's output DMA physical addresses. The protection
68ad0ae0
HS
2723 * address is set to 1 DWORD past the base address so that a
2724 * single DWORD will be transferred each time a DMA transfer is
54a2a02e
HS
2725 * enabled.
2726 */
f1f7efce 2727 phys_buf = devpriv->ana_buf.physical_base +
d8515652
IA
2728 (S626_DAC_WDMABUF_OS * sizeof(uint32_t));
2729 writel((uint32_t)phys_buf, devpriv->mmio + S626_P_BASEA2_OUT);
f1f7efce 2730 writel((uint32_t)(phys_buf + sizeof(uint32_t)),
d8515652 2731 devpriv->mmio + S626_P_PROTA2_OUT);
68ad0ae0 2732
54a2a02e
HS
2733 /*
2734 * Cache Audio2's output DMA buffer logical address. This is
2735 * where DAC data is buffered for A2 output DMA transfers.
2736 */
07a36d66 2737 devpriv->dac_wbuf = (uint32_t *)devpriv->ana_buf.logical_base +
d8515652 2738 S626_DAC_WDMABUF_OS;
68ad0ae0 2739
54a2a02e
HS
2740 /*
2741 * Audio2's output channels does not use paging. The
2742 * protection violation handling bit is set so that the
2743 * DMAC will automatically halt and its PCI address pointer
2744 * will be reset when the protection address is reached.
2745 */
d8515652 2746 writel(8, devpriv->mmio + S626_P_PAGEA2_OUT);
68ad0ae0 2747
54a2a02e
HS
2748 /*
2749 * Initialize time slot list 2 (TSL2), which is used to control
68ad0ae0
HS
2750 * the clock generation for and serialization of data to be sent
2751 * to the DAC devices. Slot 0 is a NOP that is used to trap TSL
2752 * execution; this permits other slots to be safely modified
2753 * without first turning off the TSL sequencer (which is
2754 * apparently impossible to do). Also, SD3 (which is driven by a
2755 * pull-up resistor) is shifted in and stored to the MSB of
2756 * FB_BUFFER2 to be used as evidence that the slot sequence has
2757 * not yet finished executing.
2758 */
11e865c1 2759
54a2a02e 2760 /* Slot 0: Trap TSL execution, shift 0xFF into FB_BUFFER2 */
d8515652
IA
2761 writel(S626_XSD2 | S626_RSD3 | S626_SIB_A2 | S626_EOS,
2762 devpriv->mmio + S626_VECTPORT(0));
11e865c1 2763
54a2a02e
HS
2764 /*
2765 * Initialize slot 1, which is constant. Slot 1 causes a
68ad0ae0
HS
2766 * DWORD to be transferred from audio channel 2's output FIFO
2767 * to the FIFO's output buffer so that it can be serialized
2768 * and sent to the DAC during subsequent slots. All remaining
2769 * slots are dynamically populated as required by the target
2770 * DAC device.
2771 */
54a2a02e
HS
2772
2773 /* Slot 1: Fetch DWORD from Audio2's output FIFO */
d8515652 2774 writel(S626_LF_A2, devpriv->mmio + S626_VECTPORT(1));
11e865c1 2775
54a2a02e 2776 /* Start DAC's audio interface (TSL2) running */
d8515652 2777 writel(S626_ACON1_DACSTART, devpriv->mmio + S626_P_ACON1);
11e865c1 2778
54a2a02e
HS
2779 /*
2780 * Init Trim DACs to calibrated values. Do it twice because the
68ad0ae0
HS
2781 * SAA7146 audio channel does not always reset properly and
2782 * sometimes causes the first few TrimDAC writes to malfunction.
2783 */
31de1948
IA
2784 s626_load_trim_dacs(dev);
2785 s626_load_trim_dacs(dev);
11e865c1 2786
54a2a02e
HS
2787 /*
2788 * Manually init all gate array hardware in case this is a soft
68ad0ae0
HS
2789 * reset (we have no way of determining whether this is a warm
2790 * or cold start). This is necessary because the gate array will
2791 * reset only in response to a PCI hard reset; there is no soft
54a2a02e
HS
2792 * reset function.
2793 */
11e865c1 2794
54a2a02e
HS
2795 /*
2796 * Init all DAC outputs to 0V and init all DAC setpoint and
68ad0ae0
HS
2797 * polarity images.
2798 */
2799 for (chan = 0; chan < S626_DAC_CHANNELS; chan++)
31de1948 2800 s626_set_dac(dev, chan, 0);
11e865c1 2801
54a2a02e 2802 /* Init counters */
31de1948 2803 s626_counters_init(dev);
11e865c1 2804
54a2a02e
HS
2805 /*
2806 * Without modifying the state of the Battery Backup enab, disable
68ad0ae0
HS
2807 * the watchdog timer, set DIO channels 0-5 to operate in the
2808 * standard DIO (vs. counter overflow) mode, disable the battery
2809 * charger, and reset the watchdog interval selector to zero.
2810 */
d8515652
IA
2811 s626_write_misc2(dev, (s626_debi_read(dev, S626_LP_RDMISC2) &
2812 S626_MISC2_BATT_ENABLE));
11e865c1 2813
54a2a02e 2814 /* Initialize the digital I/O subsystem */
68ad0ae0 2815 s626_dio_init(dev);
80ec9510
HS
2816}
2817
a690b7e5 2818static int s626_auto_attach(struct comedi_device *dev,
750af5e5 2819 unsigned long context_unused)
80ec9510 2820{
750af5e5 2821 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
7f2f7e05 2822 struct s626_private *devpriv;
80ec9510
HS
2823 struct comedi_subdevice *s;
2824 int ret;
2825
0bdab509 2826 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
c34fa261
HS
2827 if (!devpriv)
2828 return -ENOMEM;
80ec9510 2829
818f569f 2830 ret = comedi_pci_enable(dev);
80ec9510
HS
2831 if (ret)
2832 return ret;
80ec9510 2833
5970b102 2834 devpriv->mmio = pci_ioremap_bar(pcidev, 0);
7d856da2 2835 if (!devpriv->mmio)
80ec9510
HS
2836 return -ENOMEM;
2837
2838 /* disable master interrupt */
d8515652 2839 writel(0, devpriv->mmio + S626_P_IER);
80ec9510
HS
2840
2841 /* soft reset */
d8515652 2842 writel(S626_MC1_SOFT_RESET, devpriv->mmio + S626_P_MC1);
80ec9510
HS
2843
2844 /* DMA FIXME DMA// */
2845
2846 ret = s626_allocate_dma_buffers(dev);
2847 if (ret)
2848 return ret;
2849
2850 if (pcidev->irq) {
2851 ret = request_irq(pcidev->irq, s626_irq_handler, IRQF_SHARED,
2852 dev->board_name, dev);
2853
2854 if (ret == 0)
2855 dev->irq = pcidev->irq;
2856 }
2857
2858 ret = comedi_alloc_subdevices(dev, 6);
2859 if (ret)
2860 return ret;
2861
f0717f5d 2862 s = &dev->subdevices[0];
80ec9510 2863 /* analog input subdevice */
ca2f1091
HS
2864 s->type = COMEDI_SUBD_AI;
2865 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_CMD_READ;
2866 s->n_chan = S626_ADC_CHANNELS;
2867 s->maxdata = 0x3fff;
2868 s->range_table = &s626_range_table;
2869 s->len_chanlist = S626_ADC_CHANNELS;
ca2f1091 2870 s->insn_read = s626_ai_insn_read;
2281befd
HS
2871 if (dev->irq) {
2872 dev->read_subdev = s;
2873 s->do_cmd = s626_ai_cmd;
2874 s->do_cmdtest = s626_ai_cmdtest;
2875 s->cancel = s626_ai_cancel;
2876 }
80ec9510 2877
f0717f5d 2878 s = &dev->subdevices[1];
80ec9510 2879 /* analog output subdevice */
ca2f1091
HS
2880 s->type = COMEDI_SUBD_AO;
2881 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2882 s->n_chan = S626_DAC_CHANNELS;
2883 s->maxdata = 0x3fff;
2884 s->range_table = &range_bipolar10;
2885 s->insn_write = s626_ao_winsn;
2886 s->insn_read = s626_ao_rinsn;
80ec9510 2887
f0717f5d 2888 s = &dev->subdevices[2];
80ec9510 2889 /* digital I/O subdevice */
ca2f1091
HS
2890 s->type = COMEDI_SUBD_DIO;
2891 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2892 s->n_chan = 16;
2893 s->maxdata = 1;
2894 s->io_bits = 0xffff;
2895 s->private = (void *)0; /* DIO group 0 */
2896 s->range_table = &range_digital;
2897 s->insn_config = s626_dio_insn_config;
2898 s->insn_bits = s626_dio_insn_bits;
80ec9510 2899
f0717f5d 2900 s = &dev->subdevices[3];
80ec9510 2901 /* digital I/O subdevice */
ca2f1091
HS
2902 s->type = COMEDI_SUBD_DIO;
2903 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2904 s->n_chan = 16;
2905 s->maxdata = 1;
2906 s->io_bits = 0xffff;
2907 s->private = (void *)1; /* DIO group 1 */
2908 s->range_table = &range_digital;
2909 s->insn_config = s626_dio_insn_config;
2910 s->insn_bits = s626_dio_insn_bits;
80ec9510 2911
f0717f5d 2912 s = &dev->subdevices[4];
80ec9510 2913 /* digital I/O subdevice */
ca2f1091
HS
2914 s->type = COMEDI_SUBD_DIO;
2915 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2916 s->n_chan = 16;
2917 s->maxdata = 1;
2918 s->io_bits = 0xffff;
2919 s->private = (void *)2; /* DIO group 2 */
2920 s->range_table = &range_digital;
8ee52611 2921 s->insn_config = s626_dio_insn_config;
ca2f1091 2922 s->insn_bits = s626_dio_insn_bits;
80ec9510 2923
f0717f5d 2924 s = &dev->subdevices[5];
80ec9510 2925 /* encoder (counter) subdevice */
ca2f1091
HS
2926 s->type = COMEDI_SUBD_COUNTER;
2927 s->subdev_flags = SDF_WRITABLE | SDF_READABLE | SDF_LSAMPL;
2928 s->n_chan = S626_ENCODER_CHANNELS;
2929 s->maxdata = 0xffffff;
ca2f1091
HS
2930 s->range_table = &range_unknown;
2931 s->insn_config = s626_enc_insn_config;
2932 s->insn_read = s626_enc_insn_read;
2933 s->insn_write = s626_enc_insn_write;
80ec9510 2934
80ec9510 2935 s626_initialize(dev);
11e865c1 2936
f996ab29
HS
2937 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
2938
2939 return 0;
11e865c1
GP
2940}
2941
020c44f3 2942static void s626_detach(struct comedi_device *dev)
11e865c1 2943{
7f2f7e05 2944 struct s626_private *devpriv = dev->private;
f574af6d 2945
020c44f3
HS
2946 if (devpriv) {
2947 /* stop ai_command */
2948 devpriv->ai_cmd_running = 0;
11e865c1 2949
7d856da2 2950 if (devpriv->mmio) {
020c44f3 2951 /* interrupt mask */
25f8fd5e 2952 /* Disable master interrupt */
d8515652 2953 writel(0, devpriv->mmio + S626_P_IER);
25f8fd5e 2954 /* Clear board's IRQ status flag */
d8515652
IA
2955 writel(S626_IRQ_GPIO3 | S626_IRQ_RPS1,
2956 devpriv->mmio + S626_P_ISR);
11e865c1 2957
8ee52611 2958 /* Disable the watchdog timer and battery charger. */
31de1948 2959 s626_write_misc2(dev, 0);
11e865c1 2960
25f8fd5e 2961 /* Close all interfaces on 7146 device */
d8515652
IA
2962 writel(S626_MC1_SHUTDOWN, devpriv->mmio + S626_P_MC1);
2963 writel(S626_ACON1_BASE, devpriv->mmio + S626_P_ACON1);
11e865c1 2964
d8515652
IA
2965 s626_close_dma_b(dev, &devpriv->rps_buf,
2966 S626_DMABUF_SIZE);
2967 s626_close_dma_b(dev, &devpriv->ana_buf,
2968 S626_DMABUF_SIZE);
020c44f3 2969 }
b6c77757 2970
020c44f3
HS
2971 if (dev->irq)
2972 free_irq(dev->irq, dev);
7d856da2
HS
2973 if (devpriv->mmio)
2974 iounmap(devpriv->mmio);
f574af6d 2975 }
7f072f54 2976 comedi_pci_disable(dev);
11e865c1 2977}
7122b76d 2978
75e6301b 2979static struct comedi_driver s626_driver = {
7122b76d
HS
2980 .driver_name = "s626",
2981 .module = THIS_MODULE,
750af5e5 2982 .auto_attach = s626_auto_attach,
7122b76d
HS
2983 .detach = s626_detach,
2984};
2985
a690b7e5 2986static int s626_pci_probe(struct pci_dev *dev,
b8f4ac23 2987 const struct pci_device_id *id)
7122b76d 2988{
b8f4ac23 2989 return comedi_pci_auto_config(dev, &s626_driver, id->driver_data);
7122b76d
HS
2990}
2991
7122b76d
HS
2992/*
2993 * For devices with vendor:device id == 0x1131:0x7146 you must specify
2994 * also subvendor:subdevice ids, because otherwise it will conflict with
2995 * Philips SAA7146 media/dvb based cards.
2996 */
2997static DEFINE_PCI_DEVICE_TABLE(s626_pci_table) = {
498c5070
IA
2998 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PHILIPS, PCI_DEVICE_ID_PHILIPS_SAA7146,
2999 0x6000, 0x0272) },
7122b76d
HS
3000 { 0 }
3001};
3002MODULE_DEVICE_TABLE(pci, s626_pci_table);
3003
75e6301b
HS
3004static struct pci_driver s626_pci_driver = {
3005 .name = "s626",
7122b76d 3006 .id_table = s626_pci_table,
75e6301b 3007 .probe = s626_pci_probe,
9901a4d7 3008 .remove = comedi_pci_auto_unconfig,
7122b76d 3009};
75e6301b 3010module_comedi_pci_driver(s626_driver, s626_pci_driver);
7122b76d
HS
3011
3012MODULE_AUTHOR("Gianluca Palli <gpalli@deis.unibo.it>");
3013MODULE_DESCRIPTION("Sensoray 626 Comedi driver module");
3014MODULE_LICENSE("GPL");