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