4 * @brief ME-4000 analog output subdevice instance.
5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
13 * This file is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 ///Common part. (For normal and Bosch builds.)
37 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
43 #include <linux/types.h>
44 #include <linux/version.h>
45 #include <linux/interrupt.h>
46 #include <linux/delay.h>
48 #include "medefines.h"
49 #include "meinternal.h"
54 #include "me4600_reg.h"
55 #include "me4600_ao_reg.h"
56 #include "me4600_ao.h"
61 static int me4600_ao_query_range_by_min_max(me_subdevice_t
*subdevice
,
64 int *max
, int *maxdata
, int *range
);
66 static int me4600_ao_query_number_ranges(me_subdevice_t
*subdevice
,
67 int unit
, int *count
);
69 static int me4600_ao_query_range_info(me_subdevice_t
*subdevice
,
72 int *min
, int *max
, int *maxdata
);
74 static int me4600_ao_query_timer(me_subdevice_t
*subdevice
,
77 long long *min_ticks
, long long *max_ticks
);
79 static int me4600_ao_query_number_channels(me_subdevice_t
*subdevice
,
82 static int me4600_ao_query_subdevice_type(me_subdevice_t
*subdevice
,
83 int *type
, int *subtype
);
85 static int me4600_ao_query_subdevice_caps(me_subdevice_t
*subdevice
,
88 static int me4600_ao_query_subdevice_caps_args(struct me_subdevice
*subdevice
,
89 int cap
, int *args
, int count
);
92 /// @note NORMAL BUILD
93 /// @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
97 # include <linux/workqueue.h>
102 /** Remove subdevice.
104 static void me4600_ao_destructor(struct me_subdevice
*subdevice
);
106 /** Reset subdevice. Stop all actions. Reset registry. Disable FIFO. Set output to 0V and status to 'none'.
108 static int me4600_ao_io_reset_subdevice(me_subdevice_t
*subdevice
,
109 struct file
*filep
, int flags
);
111 /** Set output as single
113 static int me4600_ao_io_single_config(me_subdevice_t
*subdevice
,
119 int trig_type
, int trig_edge
, int flags
);
121 /** Pass to user actual value of output.
123 static int me4600_ao_io_single_read(me_subdevice_t
*subdevice
,
126 int *value
, int time_out
, int flags
);
128 /** Write to output requed value.
130 static int me4600_ao_io_single_write(me_subdevice_t
*subdevice
,
133 int value
, int time_out
, int flags
);
135 /** Set output as streamed device.
137 static int me4600_ao_io_stream_config(me_subdevice_t
*subdevice
,
139 meIOStreamConfig_t
*config_list
,
141 meIOStreamTrigger_t
*trigger
,
142 int fifo_irq_threshold
, int flags
);
144 /** Wait for / Check empty space in buffer.
146 static int me4600_ao_io_stream_new_values(me_subdevice_t
*subdevice
,
148 int time_out
, int *count
, int flags
);
152 static int me4600_ao_io_stream_start(me_subdevice_t
*subdevice
,
154 int start_mode
, int time_out
, int flags
);
156 /** Check actual state. / Wait for end.
158 static int me4600_ao_io_stream_status(me_subdevice_t
*subdevice
,
161 int *status
, int *values
, int flags
);
165 static int me4600_ao_io_stream_stop(me_subdevice_t
*subdevice
,
167 int stop_mode
, int flags
);
169 /** Write datas to buffor.
171 static int me4600_ao_io_stream_write(me_subdevice_t
*subdevice
,
174 int *values
, int *count
, int flags
);
176 /** Interrupt handler. Copy from buffer to FIFO.
178 static irqreturn_t
me4600_ao_isr(int irq
, void *dev_id
);
179 /** Copy data from circular buffer to fifo (fast) in wraparound mode.
181 inline int ao_write_data_wraparound(me4600_ao_subdevice_t
*instance
, int count
,
184 /** Copy data from circular buffer to fifo (fast).
186 inline int ao_write_data(me4600_ao_subdevice_t
*instance
, int count
,
189 /** Copy data from circular buffer to fifo (slow).
191 inline int ao_write_data_pooling(me4600_ao_subdevice_t
*instance
, int count
,
194 /** Copy data from user space to circular buffer.
196 inline int ao_get_data_from_user(me4600_ao_subdevice_t
*instance
, int count
,
199 /** Stop presentation. Preserve FIFOs.
201 inline int ao_stop_immediately(me4600_ao_subdevice_t
*instance
);
203 /** Task for asynchronical state verifying.
205 static void me4600_ao_work_control_task(struct work_struct
*work
);
209 static int me4600_ao_io_reset_subdevice(me_subdevice_t
*subdevice
,
210 struct file
*filep
, int flags
)
212 me4600_ao_subdevice_t
*instance
;
213 int err
= ME_ERRNO_SUCCESS
;
216 instance
= (me4600_ao_subdevice_t
*) subdevice
;
218 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
221 PERROR("Invalid flag specified.\n");
222 return ME_ERRNO_INVALID_FLAGS
;
227 instance
->status
= ao_status_none
;
228 instance
->ao_control_task_flag
= 0;
229 cancel_delayed_work(&instance
->ao_control_task
);
230 instance
->timeout
.delay
= 0;
231 instance
->timeout
.start_time
= jiffies
;
233 //Stop state machine.
234 err
= ao_stop_immediately(instance
);
236 //Remove from synchronous start.
237 spin_lock(instance
->preload_reg_lock
);
238 tmp
= inl(instance
->preload_reg
);
240 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->
242 outl(tmp
, instance
->preload_reg
);
243 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
244 instance
->preload_reg
- instance
->reg_base
, tmp
);
245 *instance
->preload_flags
&=
246 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->
248 spin_unlock(instance
->preload_reg_lock
);
250 //Set single mode, dissable FIFO, dissable external trigger, set output to analog, block interrupt.
251 outl(ME4600_AO_MODE_SINGLE
| ME4600_AO_CTRL_BIT_STOP
|
252 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_RESET_IRQ
,
254 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
255 instance
->ctrl_reg
- instance
->reg_base
,
256 ME4600_AO_MODE_SINGLE
| ME4600_AO_CTRL_BIT_STOP
|
257 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
258 ME4600_AO_CTRL_BIT_RESET_IRQ
);
261 outl(0x8000, instance
->single_reg
);
262 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
263 instance
->single_reg
- instance
->reg_base
, 0x8000);
265 instance
->circ_buf
.head
= 0;
266 instance
->circ_buf
.tail
= 0;
267 instance
->preloaded_count
= 0;
268 instance
->data_count
= 0;
269 instance
->single_value
= 0x8000;
270 instance
->single_value_in_fifo
= 0x8000;
272 //Set status to signal that device is unconfigured.
273 instance
->status
= ao_status_none
;
275 //Signal reset if user is on wait.
276 wake_up_interruptible_all(&instance
->wait_queue
);
283 static int me4600_ao_io_single_config(me_subdevice_t
*subdevice
,
289 int trig_type
, int trig_edge
, int flags
)
291 me4600_ao_subdevice_t
*instance
;
292 int err
= ME_ERRNO_SUCCESS
;
295 unsigned long cpu_flags
;
297 instance
= (me4600_ao_subdevice_t
*) subdevice
;
299 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
301 // Checking parameters
304 ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
305 return ME_ERRNO_INVALID_FLAGS
;
309 case ME_TRIG_TYPE_SW
:
310 if (trig_edge
!= ME_TRIG_EDGE_NONE
) {
312 ("Invalid trigger edge. Software trigger has not edge.\n");
313 return ME_ERRNO_INVALID_TRIG_EDGE
;
317 case ME_TRIG_TYPE_EXT_DIGITAL
:
319 case ME_TRIG_EDGE_ANY
:
320 case ME_TRIG_EDGE_RISING
:
321 case ME_TRIG_EDGE_FALLING
:
325 PERROR("Invalid trigger edge.\n");
326 return ME_ERRNO_INVALID_TRIG_EDGE
;
332 ("Invalid trigger type. Trigger must be software or digital.\n");
333 return ME_ERRNO_INVALID_TRIG_TYPE
;
336 if ((trig_chan
!= ME_TRIG_CHAN_DEFAULT
)
337 && (trig_chan
!= ME_TRIG_CHAN_SYNCHRONOUS
)) {
338 PERROR("Invalid trigger channel specified.\n");
339 return ME_ERRNO_INVALID_TRIG_CHAN
;
342 if (ref
!= ME_REF_AO_GROUND
) {
344 ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
345 return ME_ERRNO_INVALID_REF
;
348 if (single_config
!= 0) {
350 ("Invalid single config specified. Only one range for anlog outputs is available.\n");
351 return ME_ERRNO_INVALID_SINGLE_CONFIG
;
356 ("Invalid channel number specified. Analog output have only one channel.\n");
357 return ME_ERRNO_INVALID_CHANNEL
;
362 //Subdevice running in stream mode!
363 if ((instance
->status
>= ao_status_stream_run_wait
)
364 && (instance
->status
< ao_status_stream_end
)) {
365 PERROR("Subdevice is busy.\n");
368 return ME_ERRNO_SUBDEVICE_BUSY
;
370 /// @note For single all calls (config and write) are erasing previous state!
372 instance
->status
= ao_status_none
;
374 // Correct single mirrors
375 instance
->single_value_in_fifo
= instance
->single_value
;
378 err
= ao_stop_immediately(instance
);
380 PERROR_CRITICAL("FSM IS BUSY!\n");
383 return ME_ERRNO_SUBDEVICE_BUSY
;
385 // Set control register.
386 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
387 // Set stop bit. Stop streaming mode.
388 ctrl
= inl(instance
->ctrl_reg
);
390 ctrl
= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_STOP
;
392 if (trig_type
== ME_TRIG_TYPE_EXT_DIGITAL
) {
393 PINFO("External digital trigger.\n");
395 if (trig_edge
== ME_TRIG_EDGE_ANY
) {
396 // ctrl |= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE | ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
398 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
399 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
400 } else if (trig_edge
== ME_TRIG_EDGE_FALLING
) {
401 // ctrl |= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE;
402 instance
->ctrl_trg
= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
403 } else if (trig_edge
== ME_TRIG_EDGE_RISING
) {
404 instance
->ctrl_trg
= 0x0;
406 } else if (trig_type
== ME_TRIG_TYPE_SW
) {
407 PDEBUG("Software trigger\n");
408 instance
->ctrl_trg
= 0x0;
411 outl(ctrl
, instance
->ctrl_reg
);
412 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
413 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
414 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
416 // Set preload/synchronization register.
417 spin_lock(instance
->preload_reg_lock
);
418 if (trig_type
== ME_TRIG_TYPE_SW
) {
419 *instance
->preload_flags
&=
420 ~(ME4600_AO_SYNC_EXT_TRIG
<< instance
->ao_idx
);
421 } else //if (trig_type == ME_TRIG_TYPE_EXT_DIGITAL)
423 *instance
->preload_flags
|=
424 ME4600_AO_SYNC_EXT_TRIG
<< instance
->ao_idx
;
427 if (trig_chan
== ME_TRIG_CHAN_DEFAULT
) {
428 *instance
->preload_flags
&=
429 ~(ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
);
430 } else //if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS)
432 *instance
->preload_flags
|=
433 ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
436 //Reset hardware register
437 sync
= inl(instance
->preload_reg
);
438 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
439 instance
->preload_reg
- instance
->reg_base
, sync
);
440 sync
&= ~(ME4600_AO_SYNC_EXT_TRIG
<< instance
->ao_idx
);
441 sync
|= ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
443 //Output configured in default (safe) mode.
444 outl(sync
, instance
->preload_reg
);
445 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
446 instance
->preload_reg
- instance
->reg_base
, sync
);
447 spin_unlock(instance
->preload_reg_lock
);
449 instance
->status
= ao_status_single_configured
;
456 static int me4600_ao_io_single_read(me_subdevice_t
*subdevice
,
459 int *value
, int time_out
, int flags
)
461 me4600_ao_subdevice_t
*instance
;
462 int err
= ME_ERRNO_SUCCESS
;
465 unsigned long delay
= 0;
467 instance
= (me4600_ao_subdevice_t
*) subdevice
;
469 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
471 if (flags
& ~ME_IO_SINGLE_NONBLOCKING
) {
472 PERROR("Invalid flag specified. %d\n", flags
);
473 return ME_ERRNO_INVALID_FLAGS
;
477 PERROR("Invalid timeout specified.\n");
478 return ME_ERRNO_INVALID_TIMEOUT
;
482 PERROR("Invalid channel number specified.\n");
483 return ME_ERRNO_INVALID_CHANNEL
;
486 if ((instance
->status
>= ao_status_stream_configured
)
487 && (instance
->status
<= ao_status_stream_end
)) {
488 PERROR("Subdevice not configured to work in single mode!\n");
489 return ME_ERRNO_PREVIOUS_CONFIG
;
493 if ((!flags
) && (instance
->status
== ao_status_single_run_wait
)) { //Blocking mode. Wait for trigger.
495 delay
= (time_out
* HZ
) / 1000;
502 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
503 wait_event_interruptible_timeout(instance
->wait_queue
,
505 ao_status_single_run_wait
),
509 if (instance
->status
== ao_status_none
) {
510 PDEBUG("Single canceled.\n");
511 err
= ME_ERRNO_CANCELLED
;
514 if (signal_pending(current
)) {
515 PERROR("Wait on start of state machine interrupted.\n");
516 instance
->status
= ao_status_none
;
517 ao_stop_immediately(instance
);
518 err
= ME_ERRNO_SIGNAL
;
521 if ((delay
) && ((jiffies
- j
) >= delay
)) {
523 PDEBUG("Timeout reached.\n");
524 err
= ME_ERRNO_TIMEOUT
;
528 (!err
) ? instance
->single_value_in_fifo
: instance
->
530 } else { //Non-blocking mode
532 *value
= instance
->single_value
;
540 static int me4600_ao_io_single_write(me_subdevice_t
*subdevice
,
543 int value
, int time_out
, int flags
)
545 me4600_ao_subdevice_t
*instance
;
546 int err
= ME_ERRNO_SUCCESS
;
547 unsigned long cpu_flags
;
549 unsigned long delay
= 0x0;
551 //Registry handling variables.
558 instance
= (me4600_ao_subdevice_t
*) subdevice
;
560 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
563 ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
|
564 ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
565 PERROR("Invalid flag specified.\n");
566 return ME_ERRNO_INVALID_FLAGS
;
570 PERROR("Invalid timeout specified.\n");
571 return ME_ERRNO_INVALID_TIMEOUT
;
574 if (value
& ~ME4600_AO_MAX_DATA
) {
575 PERROR("Invalid value provided.\n");
576 return ME_ERRNO_VALUE_OUT_OF_RANGE
;
580 PERROR("Invalid channel number specified.\n");
581 return ME_ERRNO_INVALID_CHANNEL
;
584 if ((instance
->status
== ao_status_none
)
585 || (instance
->status
> ao_status_single_end
)) {
586 PERROR("Subdevice not configured to work in single mode!\n");
587 return ME_ERRNO_PREVIOUS_CONFIG
;
592 /// @note For single all calls (config and write) are erasing previous state!
594 //Cancel control task
595 PDEBUG("Cancel control task. idx=%d\n", instance
->ao_idx
);
596 instance
->ao_control_task_flag
= 0;
597 cancel_delayed_work(&instance
->ao_control_task
);
599 // Correct single mirrors
600 instance
->single_value_in_fifo
= instance
->single_value
;
603 err
= ao_stop_immediately(instance
);
605 PERROR_CRITICAL("FSM IS BUSY!\n");
608 return ME_ERRNO_SUBDEVICE_BUSY
;
612 delay
= (time_out
* HZ
) / 1000;
618 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
620 instance
->single_value_in_fifo
= value
;
622 ctrl
= inl(instance
->ctrl_reg
);
624 if (!instance
->fifo
) { //No FIFO
625 //Set the single mode.
626 ctrl
&= ~ME4600_AO_CTRL_MODE_MASK
;
629 PDEBUG("Write value\n");
630 outl(value
, instance
->single_reg
);
631 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
633 instance
->single_reg
- instance
->reg_base
, value
);
636 outl(ME4600_AO_MIN_CHAN_TICKS
- 1, instance
->timer_reg
);
637 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%x\n",
639 instance
->timer_reg
- instance
->reg_base
,
640 (int)ME4600_AO_MIN_CHAN_TICKS
);
641 instance
->hardware_stop_delay
= HZ
/ 10; //100ms
643 status
= inl(instance
->status_reg
);
645 //Set the continous mode.
646 ctrl
&= ~ME4600_AO_CTRL_MODE_MASK
;
647 ctrl
|= ME4600_AO_MODE_CONTINUOUS
;
650 if (!(ctrl
& ME4600_AO_CTRL_BIT_ENABLE_FIFO
)) { //FIFO wasn't enabeled. Do it.
651 PINFO("Enableing FIFO.\n");
652 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
654 ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
655 ME4600_AO_CTRL_BIT_RESET_IRQ
;
656 } else { //Check if FIFO is empty
657 if (status
& ME4600_AO_STATUS_BIT_EF
) { //FIFO not empty
658 PINFO("Reseting FIFO.\n");
660 ~(ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
661 ME4600_AO_CTRL_BIT_ENABLE_IRQ
);
662 ctrl
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
663 outl(ctrl
, instance
->ctrl_reg
);
664 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
667 instance
->reg_base
, ctrl
);
670 ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
671 ME4600_AO_CTRL_BIT_RESET_IRQ
;
672 } else { //FIFO empty, only interrupt needs to be disabled!
673 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
674 ctrl
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
678 outl(ctrl
, instance
->ctrl_reg
);
679 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
681 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
683 //Write output - 1 value to FIFO
684 if (instance
->ao_idx
& 0x1) {
685 outl(value
<<= 16, instance
->fifo_reg
);
686 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
688 instance
->fifo_reg
- instance
->reg_base
,
691 outl(value
, instance
->fifo_reg
);
692 PDEBUG_REG("fifo_reg outl(0x%lX+0x%lX)=0x%x\n",
694 instance
->fifo_reg
- instance
->reg_base
,
699 mode
= *instance
->preload_flags
>> instance
->ao_idx
;
700 mode
&= (ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
);
702 PINFO("Triggering mode: 0x%x\n", mode
);
704 spin_lock(instance
->preload_reg_lock
);
705 sync_mask
= inl(instance
->preload_reg
);
706 PDEBUG_REG("preload_reg inl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
707 instance
->preload_reg
- instance
->reg_base
, sync_mask
);
709 case 0: //Individual software
710 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
712 if (!instance
->fifo
) { // No FIFO - In this case resetting 'ME4600_AO_SYNC_HOLD' will trigger output.
713 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != ME4600_AO_SYNC_HOLD
) { //Now we can set correct mode. This is exception. It is set to synchronous and triggered later.
715 ~(ME4600_AO_SYNC_EXT_TRIG
<< instance
->
718 ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
720 outl(sync_mask
, instance
->preload_reg
);
722 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
724 instance
->preload_reg
- instance
->reg_base
,
728 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != 0x0) { //Now we can set correct mode.
730 ~((ME4600_AO_SYNC_EXT_TRIG
|
731 ME4600_AO_SYNC_HOLD
) << instance
->
734 outl(sync_mask
, instance
->preload_reg
);
736 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
738 instance
->preload_reg
- instance
->reg_base
,
742 instance
->single_value
= value
;
745 case ME4600_AO_SYNC_EXT_TRIG
: //Individual hardware
746 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
748 if (!instance
->fifo
) { // No FIFO - In this case resetting 'ME4600_AO_SYNC_HOLD' will trigger output.
749 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != ME4600_AO_SYNC_HOLD
) { //Now we can set correct mode
751 ~(ME4600_AO_SYNC_EXT_TRIG
<< instance
->
754 ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
756 outl(sync_mask
, instance
->preload_reg
);
758 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
760 instance
->preload_reg
- instance
->reg_base
,
764 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != 0x0) { //Now we can set correct mode.
766 ~((ME4600_AO_SYNC_EXT_TRIG
|
767 ME4600_AO_SYNC_HOLD
) << instance
->
770 outl(sync_mask
, instance
->preload_reg
);
772 ("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
774 instance
->preload_reg
- instance
->reg_base
,
780 case ME4600_AO_SYNC_HOLD
: //Synchronous software
781 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
783 // if((sync_mask & ((ME4600_AO_SYNC_HOLD | ME4600_AO_SYNC_EXT_TRIG) << instance->ao_idx)) != ME4600_AO_SYNC_HOLD)
784 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != (ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
)) { //Now we can set correct mode
786 ME4600_AO_SYNC_EXT_TRIG
<< instance
->ao_idx
;
787 // sync_mask &= ~(ME4600_AO_SYNC_EXT_TRIG << instance->ao_idx);
788 sync_mask
|= ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
790 outl(sync_mask
, instance
->preload_reg
);
791 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
793 instance
->preload_reg
- instance
->reg_base
,
798 case (ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
): //Synchronous hardware
799 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
800 if ((sync_mask
& ((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->ao_idx
)) != (ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
)) { //Now we can set correct mode
802 (ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) <<
805 outl(sync_mask
, instance
->preload_reg
);
806 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
808 instance
->preload_reg
- instance
->reg_base
,
813 // spin_unlock(instance->preload_reg_lock); // Moved down.
815 //Activate ISM (remove 'stop' bits)
817 ~(ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
818 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
819 ctrl
|= instance
->ctrl_trg
;
820 ctrl
&= ~(ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
821 outl(ctrl
, instance
->ctrl_reg
);
822 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
823 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
824 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
826 /// @note When flag 'ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS' is set than output is triggered. ALWAYS!
828 if (!instance
->fifo
) { //No FIFO
829 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) { //Fired all software synchronous outputs.
830 tmp
= ~(*instance
->preload_flags
| 0xFFFF0000);
832 ("Fired all software synchronous outputs. mask:0x%08x\n",
834 tmp
|= sync_mask
& 0xFFFF0000;
835 // Add this channel to list
836 tmp
&= ~(ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
);
839 PINFO("Software trigger.\n");
840 outl(tmp
, instance
->preload_reg
);
841 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
843 instance
->preload_reg
- instance
->reg_base
,
846 //Restore save settings
847 outl(sync_mask
, instance
->preload_reg
);
848 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
850 instance
->preload_reg
- instance
->reg_base
,
852 } else if (!mode
) { // Add this channel to list
854 ~(ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
),
855 instance
->preload_reg
);
856 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
858 instance
->preload_reg
- instance
->reg_base
,
859 sync_mask
& ~(ME4600_AO_SYNC_HOLD
<<
863 PINFO("Software trigger.\n");
865 //Restore save settings
866 outl(sync_mask
, instance
->preload_reg
);
867 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
869 instance
->preload_reg
- instance
->reg_base
,
873 } else { // mix-mode - begin
874 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) { //Trigger outputs
875 //Add channel to start list
877 (ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
),
878 instance
->preload_reg
);
879 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
881 instance
->preload_reg
- instance
->reg_base
,
882 sync_mask
| (ME4600_AO_SYNC_HOLD
<<
887 ("Fired all software synchronous outputs by software trigger.\n");
888 outl(0x8000, instance
->single_reg
);
889 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
891 instance
->single_reg
- instance
->reg_base
,
894 //Restore save settings
895 outl(sync_mask
, instance
->preload_reg
);
896 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
898 instance
->preload_reg
- instance
->reg_base
,
900 } else if (!mode
) { //Trigger outputs
901 /* //Remove channel from start list //<== Unnecessary. Removed.
902 outl(sync_mask & ~(ME4600_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
903 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, tmp);
906 PINFO("Software trigger.\n");
907 outl(0x8000, instance
->single_reg
);
908 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
910 instance
->single_reg
- instance
->reg_base
,
913 /* //Restore save settings //<== Unnecessary. Removed.
914 outl(sync_mask, instance->preload_reg);
915 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, sync_mask);
919 spin_unlock(instance
->preload_reg_lock
);
922 instance
->status
= ao_status_single_run_wait
;
924 instance
->timeout
.delay
= delay
;
925 instance
->timeout
.start_time
= j
;
926 instance
->ao_control_task_flag
= 1;
927 queue_delayed_work(instance
->me4600_workqueue
,
928 &instance
->ao_control_task
, 1);
930 if (!(flags
& ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
932 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
933 wait_event_interruptible_timeout(instance
->wait_queue
,
935 ao_status_single_run_wait
),
939 if (((!delay
) || ((jiffies
- j
) <= delay
))
940 && (instance
->status
!= ao_status_single_end
)) {
941 PDEBUG("Single canceled.\n");
942 err
= ME_ERRNO_CANCELLED
;
945 if (signal_pending(current
)) {
946 PERROR("Wait on start of state machine interrupted.\n");
947 instance
->ao_control_task_flag
= 0;
948 cancel_delayed_work(&instance
->ao_control_task
);
949 ao_stop_immediately(instance
);
950 instance
->status
= ao_status_none
;
951 err
= ME_ERRNO_SIGNAL
;
954 if ((delay
) && ((jiffies
- j
) >= delay
)) {
955 if (instance
->status
== ao_status_single_end
) {
956 PDEBUG("Timeout reached.\n");
958 if ((jiffies
- j
) > delay
) {
960 ("Timeout reached. Not handled by control task!\n");
963 ("Timeout reached. Signal come but status is strange: %d\n",
967 ao_stop_immediately(instance
);
970 instance
->ao_control_task_flag
= 0;
971 cancel_delayed_work(&instance
->ao_control_task
);
972 instance
->status
= ao_status_single_end
;
973 err
= ME_ERRNO_TIMEOUT
;
982 static int me4600_ao_io_stream_config(me_subdevice_t
*subdevice
,
984 meIOStreamConfig_t
*config_list
,
986 meIOStreamTrigger_t
*trigger
,
987 int fifo_irq_threshold
, int flags
)
989 me4600_ao_subdevice_t
*instance
;
990 int err
= ME_ERRNO_SUCCESS
;
992 unsigned long cpu_flags
;
994 unsigned int conv_start_ticks_low
= trigger
->iConvStartTicksLow
;
995 unsigned int conv_start_ticks_high
= trigger
->iConvStartTicksHigh
;
997 instance
= (me4600_ao_subdevice_t
*) subdevice
;
999 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
1001 if (!instance
->fifo
) {
1002 PERROR("Not a streaming ao.\n");
1003 return ME_ERRNO_NOT_SUPPORTED
;
1007 (uint64_t) conv_start_ticks_low
+
1008 ((uint64_t) conv_start_ticks_high
<< 32);
1011 ~(ME_IO_STREAM_CONFIG_HARDWARE_ONLY
| ME_IO_STREAM_CONFIG_WRAPAROUND
1012 | ME_IO_STREAM_CONFIG_BIT_PATTERN
)) {
1013 PERROR("Invalid flags.\n");
1014 return ME_ERRNO_INVALID_FLAGS
;
1017 if (flags
& ME_IO_STREAM_CONFIG_HARDWARE_ONLY
) {
1018 if (!(flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
)) {
1020 ("Hardware ME_IO_STREAM_CONFIG_HARDWARE_ONLY has to be with ME_IO_STREAM_CONFIG_WRAPAROUND.\n");
1021 return ME_ERRNO_INVALID_FLAGS
;
1024 if ((trigger
->iAcqStopTrigType
!= ME_TRIG_TYPE_NONE
)
1025 || (trigger
->iScanStopTrigType
!= ME_TRIG_TYPE_NONE
)) {
1027 ("Hardware wraparound mode must be in infinite mode.\n");
1028 return ME_ERRNO_INVALID_FLAGS
;
1033 PERROR("Only 1 entry in config list acceptable.\n");
1034 return ME_ERRNO_INVALID_CONFIG_LIST_COUNT
;
1037 if (config_list
[0].iChannel
!= 0) {
1038 PERROR("Invalid channel number specified.\n");
1039 return ME_ERRNO_INVALID_CHANNEL
;
1042 if (config_list
[0].iStreamConfig
!= 0) {
1043 PERROR("Only one range available.\n");
1044 return ME_ERRNO_INVALID_STREAM_CONFIG
;
1047 if (config_list
[0].iRef
!= ME_REF_AO_GROUND
) {
1048 PERROR("Output is referenced to ground.\n");
1049 return ME_ERRNO_INVALID_REF
;
1052 if ((trigger
->iAcqStartTicksLow
!= 0)
1053 || (trigger
->iAcqStartTicksHigh
!= 0)) {
1055 ("Invalid acquisition start trigger argument specified.\n");
1056 return ME_ERRNO_INVALID_ACQ_START_ARG
;
1059 if (config_list
[0].iFlags
) {
1060 PERROR("Invalid config list flag.\n");
1061 return ME_ERRNO_INVALID_FLAGS
;
1064 switch (trigger
->iAcqStartTrigType
) {
1065 case ME_TRIG_TYPE_SW
:
1066 if (trigger
->iAcqStartTrigEdge
!= ME_TRIG_EDGE_NONE
) {
1068 ("Invalid acquisition start trigger edge specified.\n");
1069 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE
;
1073 case ME_TRIG_TYPE_EXT_DIGITAL
:
1074 switch (trigger
->iAcqStartTrigEdge
) {
1075 case ME_TRIG_EDGE_ANY
:
1076 case ME_TRIG_EDGE_RISING
:
1077 case ME_TRIG_EDGE_FALLING
:
1082 ("Invalid acquisition start trigger edge specified.\n");
1083 return ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE
;
1088 PERROR("Invalid acquisition start trigger type specified.\n");
1089 return ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE
;
1092 if (trigger
->iScanStartTrigType
!= ME_TRIG_TYPE_FOLLOW
) {
1093 PERROR("Invalid scan start trigger type specified.\n");
1094 return ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE
;
1097 if (trigger
->iConvStartTrigType
!= ME_TRIG_TYPE_TIMER
) {
1098 PERROR("Invalid conv start trigger type specified.\n");
1099 return ME_ERRNO_INVALID_CONV_START_TRIG_TYPE
;
1102 if ((conv_ticks
< ME4600_AO_MIN_CHAN_TICKS
)
1103 || (conv_ticks
> ME4600_AO_MAX_CHAN_TICKS
)) {
1104 PERROR("Invalid conv start trigger argument specified.\n");
1105 return ME_ERRNO_INVALID_CONV_START_ARG
;
1108 if (trigger
->iAcqStartTicksLow
|| trigger
->iAcqStartTicksHigh
) {
1109 PERROR("Invalid acq start trigger argument specified.\n");
1110 return ME_ERRNO_INVALID_ACQ_START_ARG
;
1113 if (trigger
->iScanStartTicksLow
|| trigger
->iScanStartTicksHigh
) {
1114 PERROR("Invalid scan start trigger argument specified.\n");
1115 return ME_ERRNO_INVALID_SCAN_START_ARG
;
1118 switch (trigger
->iScanStopTrigType
) {
1119 case ME_TRIG_TYPE_NONE
:
1120 if (trigger
->iScanStopCount
!= 0) {
1121 PERROR("Invalid scan stop count specified.\n");
1122 return ME_ERRNO_INVALID_SCAN_STOP_ARG
;
1126 case ME_TRIG_TYPE_COUNT
:
1127 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
1128 if (trigger
->iScanStopCount
<= 0) {
1129 PERROR("Invalid scan stop count specified.\n");
1130 return ME_ERRNO_INVALID_SCAN_STOP_ARG
;
1133 PERROR("The continous mode has not 'scan' contects.\n");
1134 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
1139 PERROR("Invalid scan stop trigger type specified.\n");
1140 return ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE
;
1143 switch (trigger
->iAcqStopTrigType
) {
1144 case ME_TRIG_TYPE_NONE
:
1145 if (trigger
->iAcqStopCount
!= 0) {
1146 PERROR("Invalid acq stop count specified.\n");
1147 return ME_ERRNO_INVALID_ACQ_STOP_ARG
;
1151 case ME_TRIG_TYPE_COUNT
:
1152 if (trigger
->iScanStopTrigType
!= ME_TRIG_TYPE_NONE
) {
1153 PERROR("Invalid acq stop trigger type specified.\n");
1154 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
1157 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
1158 if (trigger
->iAcqStopCount
<= 0) {
1160 ("The continous mode has not 'scan' contects.\n");
1161 return ME_ERRNO_INVALID_ACQ_STOP_ARG
;
1167 PERROR("Invalid acq stop trigger type specified.\n");
1168 return ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
1171 switch (trigger
->iAcqStartTrigChan
) {
1172 case ME_TRIG_CHAN_DEFAULT
:
1173 case ME_TRIG_CHAN_SYNCHRONOUS
:
1177 PERROR("Invalid acq start trigger channel specified.\n");
1178 return ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN
;
1183 if ((flags
& ME_IO_STREAM_CONFIG_BIT_PATTERN
) && !instance
->bitpattern
) {
1184 PERROR("This subdevice not support output redirection.\n");
1186 return ME_ERRNO_INVALID_FLAGS
;
1190 //Cancel control task
1191 PDEBUG("Cancel control task. idx=%d\n", instance
->ao_idx
);
1192 instance
->ao_control_task_flag
= 0;
1193 cancel_delayed_work(&instance
->ao_control_task
);
1195 //Check if state machine is stopped.
1196 err
= ao_stop_immediately(instance
);
1198 PERROR_CRITICAL("FSM IS BUSY!\n");
1201 return ME_ERRNO_SUBDEVICE_BUSY
;
1204 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
1205 //Reset control register. Block all actions. Disable IRQ. Disable FIFO.
1207 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_STOP
|
1208 ME4600_AO_CTRL_BIT_RESET_IRQ
;
1209 outl(ctrl
, instance
->ctrl_reg
);
1210 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
1211 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
1213 //This is paranoic, but to be sure.
1214 instance
->preloaded_count
= 0;
1215 instance
->data_count
= 0;
1216 instance
->circ_buf
.head
= 0;
1217 instance
->circ_buf
.tail
= 0;
1220 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) { //Wraparound
1221 if (flags
& ME_IO_STREAM_CONFIG_HARDWARE_ONLY
) { //Hardware wraparound
1222 PINFO("Hardware wraparound.\n");
1223 ctrl
|= ME4600_AO_MODE_WRAPAROUND
;
1224 instance
->mode
= ME4600_AO_HW_WRAP_MODE
;
1225 } else { //Software wraparound
1226 PINFO("Software wraparound.\n");
1227 ctrl
|= ME4600_AO_MODE_CONTINUOUS
;
1228 instance
->mode
= ME4600_AO_SW_WRAP_MODE
;
1230 } else { //Continous
1231 PINFO("Continous.\n");
1232 ctrl
|= ME4600_AO_MODE_CONTINUOUS
;
1233 instance
->mode
= ME4600_AO_CONTINOUS
;
1236 //Set the trigger edge.
1237 if (trigger
->iAcqStartTrigType
== ME_TRIG_TYPE_EXT_DIGITAL
) { //Set the trigger type and edge for external trigger.
1238 PINFO("External digital trigger.\n");
1239 instance
->start_mode
= ME4600_AO_EXT_TRIG
;
1241 ctrl |= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG;
1243 switch (trigger
->iAcqStartTrigEdge
) {
1244 case ME_TRIG_EDGE_RISING
:
1245 PINFO("Set the trigger edge: rising.\n");
1246 instance
->ctrl_trg
= 0x0;
1249 case ME_TRIG_EDGE_FALLING
:
1250 PINFO("Set the trigger edge: falling.\n");
1251 // ctrl |= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE;
1252 instance
->ctrl_trg
= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
1255 case ME_TRIG_EDGE_ANY
:
1256 PINFO("Set the trigger edge: both edges.\n");
1257 // ctrl |= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE | ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH;
1258 instance
->ctrl_trg
=
1259 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
1260 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
1264 PINFO("Internal software trigger.\n");
1265 instance
->start_mode
= 0;
1268 //Set the stop mode and value.
1269 if (trigger
->iAcqStopTrigType
== ME_TRIG_TYPE_COUNT
) { //Amount of data
1270 instance
->stop_mode
= ME4600_AO_ACQ_STOP_MODE
;
1271 instance
->stop_count
= trigger
->iAcqStopCount
;
1272 } else if (trigger
->iScanStopTrigType
== ME_TRIG_TYPE_COUNT
) { //Amount of 'scans'
1273 instance
->stop_mode
= ME4600_AO_SCAN_STOP_MODE
;
1274 instance
->stop_count
= trigger
->iScanStopCount
;
1276 instance
->stop_mode
= ME4600_AO_INF_STOP_MODE
;
1277 instance
->stop_count
= 0;
1280 PINFO("Stop count: %d.\n", instance
->stop_count
);
1282 if (trigger
->iAcqStartTrigChan
== ME_TRIG_CHAN_SYNCHRONOUS
) { //Synchronous start
1283 instance
->start_mode
|= ME4600_AO_SYNC_HOLD
;
1284 if (trigger
->iAcqStartTrigType
== ME_TRIG_TYPE_EXT_DIGITAL
) { //Externaly triggered
1285 PINFO("Synchronous start. Externaly trigger active.\n");
1286 instance
->start_mode
|= ME4600_AO_SYNC_EXT_TRIG
;
1291 ("Synchronous start. Externaly trigger dissabled.\n");
1297 outl(conv_ticks
- 2, instance
->timer_reg
);
1298 PDEBUG_REG("timer_reg outl(0x%lX+0x%lX)=0x%llx\n", instance
->reg_base
,
1299 instance
->timer_reg
- instance
->reg_base
, conv_ticks
- 2);
1300 instance
->hardware_stop_delay
= (int)(conv_ticks
* HZ
) / ME4600_AO_BASE_FREQUENCY
; //<== MUST be with cast!
1302 //Conect outputs to analog or digital port.
1303 if (flags
& ME_IO_STREAM_CONFIG_BIT_PATTERN
) {
1304 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_DO
;
1306 // Write the control word
1307 outl(ctrl
, instance
->ctrl_reg
);
1308 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
1309 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
1312 instance
->status
= ao_status_stream_configured
;
1313 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
1320 static int me4600_ao_io_stream_new_values(me_subdevice_t
*subdevice
,
1322 int time_out
, int *count
, int flags
)
1324 me4600_ao_subdevice_t
*instance
;
1325 int err
= ME_ERRNO_SUCCESS
;
1329 instance
= (me4600_ao_subdevice_t
*) subdevice
;
1331 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
1333 if (!instance
->fifo
) {
1334 PERROR("Not a streaming ao.\n");
1335 return ME_ERRNO_NOT_SUPPORTED
;
1339 PERROR("Invalid flag specified.\n");
1340 return ME_ERRNO_INVALID_FLAGS
;
1343 if (!instance
->circ_buf
.buf
) {
1344 PERROR("Circular buffer not exists.\n");
1345 return ME_ERRNO_INTERNAL
;
1349 PERROR("Invalid time_out specified.\n");
1350 return ME_ERRNO_INVALID_TIMEOUT
;
1355 if (me_circ_buf_space(&instance
->circ_buf
)) { //The buffer is NOT full.
1356 *count
= me_circ_buf_space(&instance
->circ_buf
);
1357 } else { //The buffer is full.
1359 t
= (time_out
* HZ
) / 1000;
1363 } else { //Max time.
1371 //Only runing process will interrupt this call. Interrupts are when FIFO HF is signaled.
1372 wait_event_interruptible_timeout(instance
->wait_queue
,
1374 (&instance
->circ_buf
))
1375 || !(inl(instance
->status_reg
)
1377 ME4600_AO_STATUS_BIT_FSM
)),
1380 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
1381 PERROR("AO subdevice is not running.\n");
1382 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
1383 } else if (signal_pending(current
)) {
1384 PERROR("Wait on values interrupted from signal.\n");
1385 instance
->status
= ao_status_none
;
1386 ao_stop_immediately(instance
);
1387 err
= ME_ERRNO_SIGNAL
;
1388 } else if ((jiffies
- j
) >= t
) {
1389 PERROR("Wait on values timed out.\n");
1390 err
= ME_ERRNO_TIMEOUT
;
1391 } else { //Uff... all is good. Inform user about empty space.
1392 *count
= me_circ_buf_space(&instance
->circ_buf
);
1401 static int me4600_ao_io_stream_start(me_subdevice_t
*subdevice
,
1403 int start_mode
, int time_out
, int flags
)
1405 me4600_ao_subdevice_t
*instance
;
1406 int err
= ME_ERRNO_SUCCESS
;
1407 unsigned long cpu_flags
= 0;
1412 int circ_buffer_count
;
1415 unsigned long delay
= 0;
1417 instance
= (me4600_ao_subdevice_t
*) subdevice
;
1419 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
1421 if (!instance
->fifo
) {
1422 PERROR("Not a streaming ao.\n");
1423 return ME_ERRNO_NOT_SUPPORTED
;
1426 if (flags
& ~ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
1427 PERROR("Invalid flags.\n");
1428 return ME_ERRNO_INVALID_FLAGS
;
1432 PERROR("Invalid timeout specified.\n");
1433 return ME_ERRNO_INVALID_TIMEOUT
;
1436 if ((start_mode
!= ME_START_MODE_BLOCKING
)
1437 && (start_mode
!= ME_START_MODE_NONBLOCKING
)) {
1438 PERROR("Invalid start mode specified.\n");
1439 return ME_ERRNO_INVALID_START_MODE
;
1443 delay
= (time_out
* HZ
) / 1000;
1448 switch (instance
->status
) { //Checking actual mode.
1449 case ao_status_stream_configured
:
1450 case ao_status_stream_end
:
1454 //The device is in wrong mode.
1455 case ao_status_none
:
1456 case ao_status_single_configured
:
1457 case ao_status_single_run_wait
:
1458 case ao_status_single_run
:
1459 case ao_status_single_end_wait
:
1461 ("Subdevice must be preinitialize correctly for streaming.\n");
1462 return ME_ERRNO_PREVIOUS_CONFIG
;
1464 case ao_status_stream_fifo_error
:
1465 case ao_status_stream_buffer_error
:
1466 case ao_status_stream_error
:
1467 PDEBUG("Before restart broke stream 'STOP' must be caled.\n");
1468 return ME_STATUS_ERROR
;
1470 case ao_status_stream_run_wait
:
1471 case ao_status_stream_run
:
1472 case ao_status_stream_end_wait
:
1473 PDEBUG("Stream is already working.\n");
1474 return ME_ERRNO_SUBDEVICE_BUSY
;
1477 instance
->status
= ao_status_stream_error
;
1478 PERROR_CRITICAL("Status is in wrong state!\n");
1479 return ME_ERRNO_INTERNAL
;
1485 if (instance
->mode
== ME4600_AO_CONTINOUS
) { //Continous
1486 instance
->circ_buf
.tail
+= instance
->preloaded_count
;
1487 instance
->circ_buf
.tail
&= instance
->circ_buf
.mask
;
1489 circ_buffer_count
= me_circ_buf_values(&instance
->circ_buf
);
1491 if (!circ_buffer_count
&& !instance
->preloaded_count
) { //No values in buffer
1493 PERROR("No values in buffer!\n");
1494 return ME_ERRNO_LACK_OF_RESOURCES
;
1497 //Cancel control task
1498 PDEBUG("Cancel control task. idx=%d\n", instance
->ao_idx
);
1499 instance
->ao_control_task_flag
= 0;
1500 cancel_delayed_work(&instance
->ao_control_task
);
1503 err
= ao_stop_immediately(instance
);
1505 PERROR_CRITICAL("FSM IS BUSY!\n");
1508 return ME_ERRNO_SUBDEVICE_BUSY
;
1510 //Set values for single_read()
1511 instance
->single_value
= ME4600_AO_MAX_DATA
+ 1;
1512 instance
->single_value_in_fifo
= ME4600_AO_MAX_DATA
+ 1;
1514 //Setting stop points
1515 if (instance
->stop_mode
== ME4600_AO_SCAN_STOP_MODE
) {
1516 instance
->stop_data_count
=
1517 instance
->stop_count
* circ_buffer_count
;
1519 instance
->stop_data_count
= instance
->stop_count
;
1522 if ((instance
->stop_data_count
!= 0)
1523 && (instance
->stop_data_count
< circ_buffer_count
)) {
1524 PERROR("More data in buffer than previously set limit!\n");
1527 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
1528 ctrl
= inl(instance
->ctrl_reg
);
1530 if (!(ctrl
& ME4600_AO_CTRL_BIT_ENABLE_FIFO
)) { //FIFO wasn't enabeled. Do it. <= This should be done by user call with ME_WRITE_MODE_PRELOAD
1531 PINFO("Enableing FIFO.\n");
1533 ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
1534 ME4600_AO_CTRL_BIT_RESET_IRQ
;
1536 instance
->preloaded_count
= 0;
1537 instance
->data_count
= 0;
1538 } else { //Block IRQ
1539 ctrl
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
1541 outl(ctrl
, instance
->ctrl_reg
);
1542 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
1543 instance
->ctrl_reg
- instance
->reg_base
,
1544 ctrl
| ME4600_AO_CTRL_BIT_RESET_IRQ
);
1546 //Fill FIFO <= Generaly this should be done by user pre-load call but this is second place to do it.
1547 status
= inl(instance
->status_reg
);
1548 if (!(status
& ME4600_AO_STATUS_BIT_EF
)) { //FIFO empty
1549 if (instance
->stop_data_count
== 0) {
1550 count
= ME4600_AO_FIFO_COUNT
;
1553 (ME4600_AO_FIFO_COUNT
<
1555 stop_data_count
) ? ME4600_AO_FIFO_COUNT
:
1556 instance
->stop_data_count
;
1561 ao_write_data(instance
, count
, instance
->preloaded_count
);
1563 if (count
< 0) { //This should never happend!
1564 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1565 spin_unlock_irqrestore(&instance
->subdevice_lock
,
1568 return ME_ERRNO_INTERNAL
;
1571 //Set pre-load features.
1572 spin_lock(instance
->preload_reg_lock
);
1573 synch
= inl(instance
->preload_reg
);
1575 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) << instance
->
1578 (instance
->start_mode
& ~ME4600_AO_EXT_TRIG
) << instance
->ao_idx
;
1579 outl(synch
, instance
->preload_reg
);
1580 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
1581 instance
->preload_reg
- instance
->reg_base
, synch
);
1582 spin_unlock(instance
->preload_reg_lock
);
1584 //Default count is '0'
1585 if (instance
->mode
== ME4600_AO_CONTINOUS
) { //Continous
1586 instance
->preloaded_count
= 0;
1587 instance
->circ_buf
.tail
+= count
;
1588 instance
->circ_buf
.tail
&= instance
->circ_buf
.mask
;
1589 } else { //Wraparound
1590 instance
->preloaded_count
+= count
;
1591 instance
->data_count
+= count
;
1593 //Special case: Infinite wraparound with less than FIFO datas always should runs in hardware mode.
1594 if ((instance
->stop_mode
== ME4600_AO_INF_STOP_MODE
)
1595 && (circ_buffer_count
<= ME4600_AO_FIFO_COUNT
)) { //Change to hardware wraparound
1597 ("Changeing mode from software wraparound to hardware wraparound.\n");
1600 ao_write_data(instance
, circ_buffer_count
,
1601 instance
->preloaded_count
);
1602 ctrl
&= ~ME4600_AO_CTRL_MODE_MASK
;
1603 ctrl
|= ME4600_AO_MODE_WRAPAROUND
;
1606 if (instance
->preloaded_count
== me_circ_buf_values(&instance
->circ_buf
)) { //Reset position indicator.
1607 instance
->preloaded_count
= 0;
1608 } else if (instance
->preloaded_count
> me_circ_buf_values(&instance
->circ_buf
)) { //This should never happend!
1610 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1611 spin_unlock_irqrestore(&instance
->subdevice_lock
,
1614 return ME_ERRNO_INTERNAL
;
1618 //Set status to 'wait for start'
1619 instance
->status
= ao_status_stream_run_wait
;
1621 status
= inl(instance
->status_reg
);
1622 //Start state machine and interrupts
1623 ctrl
&= ~(ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
1624 if (instance
->start_mode
== ME4600_AO_EXT_TRIG
) { // External trigger.
1625 PINFO("External trigger.\n");
1626 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
1628 if (!(status
& ME4600_AO_STATUS_BIT_HF
)) { //More than half!
1629 if ((ctrl
& ME4600_AO_CTRL_MODE_MASK
) == ME4600_AO_MODE_CONTINUOUS
) { //Enable IRQ only when hardware_continous is set and FIFO is more than half
1630 ctrl
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
1631 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
1634 outl(ctrl
, instance
->ctrl_reg
);
1635 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
1636 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
1637 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
1640 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) { //Trigger outputs
1641 spin_lock(instance
->preload_reg_lock
);
1642 synch
= inl(instance
->preload_reg
);
1643 //Add channel to start list
1644 outl(synch
| (ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
),
1645 instance
->preload_reg
);
1646 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1648 instance
->preload_reg
- instance
->reg_base
,
1649 synch
| (ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
));
1653 ("Fired all software synchronous outputs by software trigger.\n");
1654 outl(0x8000, instance
->single_reg
);
1655 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1657 instance
->single_reg
- instance
->reg_base
, 0x8000);
1659 //Restore save settings
1660 outl(synch
, instance
->preload_reg
);
1661 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
1663 instance
->preload_reg
- instance
->reg_base
, synch
);
1664 spin_unlock(instance
->preload_reg_lock
);
1665 } else if (!instance
->start_mode
) { //Trigger outputs
1667 //Remove channel from start list. // <== Unnecessary. Removed.
1668 spin_lock(instance->preload_reg_lock);
1669 synch = inl(instance->preload_reg);
1670 outl(synch & ~(ME4600_AO_SYNC_HOLD << instance->ao_idx), instance->preload_reg);
1671 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch & ~(ME4600_AO_SYNC_HOLD << instance->ao_idx));
1674 PINFO("Software trigger.\n");
1675 outl(0x8000, instance
->single_reg
);
1676 PDEBUG_REG("single_reg outl(0x%lX+0x%lX)=0x%x\n",
1678 instance
->single_reg
- instance
->reg_base
, 0x8000);
1681 //Restore save settings. // <== Unnecessary. Removed.
1682 outl(synch, instance->preload_reg);
1683 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base, instance->preload_reg - instance->reg_base, synch);
1684 spin_unlock(instance->preload_reg_lock);
1687 // Set control task's timeout
1689 instance
->timeout
.delay
= delay
;
1690 instance
->timeout
.start_time
= ref
;
1692 if (status
& ME4600_AO_STATUS_BIT_HF
) { //Less than half but not empty!
1693 PINFO("Less than half.\n");
1694 if (instance
->stop_data_count
!= 0) {
1695 count
= ME4600_AO_FIFO_COUNT
/ 2;
1698 ((ME4600_AO_FIFO_COUNT
/ 2) <
1699 instance
->stop_data_count
) ? ME4600_AO_FIFO_COUNT
/
1700 2 : instance
->stop_data_count
;
1705 ao_write_data(instance
, count
, instance
->preloaded_count
);
1707 if (count
< 0) { //This should never happend!
1708 PERROR_CRITICAL("COPY FINISH WITH ERROR!\n");
1710 return ME_ERRNO_INTERNAL
;
1713 if (instance
->mode
== ME4600_AO_CONTINOUS
) { //Continous
1714 instance
->circ_buf
.tail
+= count
;
1715 instance
->circ_buf
.tail
&= instance
->circ_buf
.mask
;
1716 } else { //Wraparound
1717 instance
->data_count
+= count
;
1718 instance
->preloaded_count
+= count
;
1720 if (instance
->preloaded_count
== me_circ_buf_values(&instance
->circ_buf
)) { //Reset position indicator.
1721 instance
->preloaded_count
= 0;
1722 } else if (instance
->preloaded_count
> me_circ_buf_values(&instance
->circ_buf
)) { //This should never happend!
1724 ("PRELOADED MORE VALUES THAN ARE IN BUFFER!\n");
1726 return ME_ERRNO_INTERNAL
;
1730 status
= inl(instance
->status_reg
);
1731 if (!(status
& ME4600_AO_STATUS_BIT_HF
)) { //More than half!
1732 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
1733 ctrl
= inl(instance
->ctrl_reg
);
1734 ctrl
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
1735 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
1736 outl(ctrl
, instance
->ctrl_reg
);
1737 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1739 instance
->ctrl_reg
- instance
->reg_base
,
1741 spin_unlock_irqrestore(&instance
->subdevice_lock
,
1745 //Special case: Limited wraparound with less than HALF FIFO datas need work around to generate first interrupt.
1746 if ((instance
->stop_mode
!= ME4600_AO_INF_STOP_MODE
)
1747 && (instance
->mode
== ME4600_AO_SW_WRAP_MODE
)
1748 && (circ_buffer_count
<= (ME4600_AO_FIFO_COUNT
/ 2))) { //Put more data to FIFO
1749 PINFO("Limited wraparound with less than HALF FIFO datas.\n");
1750 if (instance
->preloaded_count
) { //This should never happend!
1752 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1754 return ME_ERRNO_INTERNAL
;
1757 while (instance
->stop_data_count
> instance
->data_count
) { //Maximum data not set jet.
1759 if (circ_buffer_count
!= ao_write_data(instance
, circ_buffer_count
, 0)) { //This should never happend!
1761 ("ERROR WHEN LOADING VALUES FOR WRAPAROUND!\n");
1763 return ME_ERRNO_INTERNAL
;
1765 instance
->data_count
+= circ_buffer_count
;
1767 if (!((status
= inl(instance
->status_reg
)) & ME4600_AO_STATUS_BIT_HF
)) { //FIFO is more than half. Enable IRQ and end copy.
1768 spin_lock_irqsave(&instance
->subdevice_lock
,
1770 ctrl
= inl(instance
->ctrl_reg
);
1771 ctrl
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
1772 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
1773 outl(ctrl
, instance
->ctrl_reg
);
1774 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1776 instance
->ctrl_reg
-
1777 instance
->reg_base
, ctrl
);
1778 spin_unlock_irqrestore(&instance
->
1785 // Schedule control task.
1786 instance
->ao_control_task_flag
= 1;
1787 queue_delayed_work(instance
->me4600_workqueue
,
1788 &instance
->ao_control_task
, 1);
1790 if (start_mode
== ME_START_MODE_BLOCKING
) { //Wait for start.
1791 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1792 wait_event_interruptible_timeout(instance
->wait_queue
,
1793 (instance
->status
!=
1794 ao_status_stream_run_wait
),
1798 if ((instance
->status
!= ao_status_stream_run
)
1799 && (instance
->status
!= ao_status_stream_end
)) {
1800 PDEBUG("Starting stream canceled. %d\n",
1802 err
= ME_ERRNO_CANCELLED
;
1805 if (signal_pending(current
)) {
1806 PERROR("Wait on start of state machine interrupted.\n");
1807 instance
->status
= ao_status_none
;
1808 ao_stop_immediately(instance
);
1809 err
= ME_ERRNO_SIGNAL
;
1810 } else if ((delay
) && ((jiffies
- ref
) >= delay
)) {
1811 if (instance
->status
!= ao_status_stream_run
) {
1812 if (instance
->status
== ao_status_stream_end
) {
1813 PDEBUG("Timeout reached.\n");
1815 if ((jiffies
- ref
) > delay
) {
1817 ("Timeout reached. Not handled by control task!\n");
1820 ("Timeout reached. Signal come but status is strange: %d\n",
1823 ao_stop_immediately(instance
);
1826 instance
->ao_control_task_flag
= 0;
1827 cancel_delayed_work(&instance
->ao_control_task
);
1828 instance
->status
= ao_status_stream_end
;
1829 err
= ME_ERRNO_TIMEOUT
;
1838 static int me4600_ao_io_stream_status(me_subdevice_t
*subdevice
,
1841 int *status
, int *values
, int flags
)
1843 me4600_ao_subdevice_t
*instance
;
1844 int err
= ME_ERRNO_SUCCESS
;
1846 instance
= (me4600_ao_subdevice_t
*) subdevice
;
1848 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
1850 if (!instance
->fifo
) {
1851 PERROR("Not a streaming ao.\n");
1852 return ME_ERRNO_NOT_SUPPORTED
;
1856 PERROR("Invalid flag specified.\n");
1857 return ME_ERRNO_INVALID_FLAGS
;
1860 if ((wait
!= ME_WAIT_NONE
) && (wait
!= ME_WAIT_IDLE
)) {
1861 PERROR("Invalid wait argument specified.\n");
1862 *status
= ME_STATUS_INVALID
;
1863 return ME_ERRNO_INVALID_WAIT
;
1868 switch (instance
->status
) {
1869 case ao_status_single_configured
:
1870 case ao_status_single_end
:
1871 case ao_status_stream_configured
:
1872 case ao_status_stream_end
:
1873 case ao_status_stream_fifo_error
:
1874 case ao_status_stream_buffer_error
:
1875 case ao_status_stream_error
:
1876 *status
= ME_STATUS_IDLE
;
1879 case ao_status_single_run_wait
:
1880 case ao_status_single_run
:
1881 case ao_status_single_end_wait
:
1882 case ao_status_stream_run_wait
:
1883 case ao_status_stream_run
:
1884 case ao_status_stream_end_wait
:
1885 *status
= ME_STATUS_BUSY
;
1888 case ao_status_none
:
1891 (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ?
1892 ME_STATUS_BUSY
: ME_STATUS_IDLE
;
1896 if ((wait
== ME_WAIT_IDLE
) && (*status
== ME_STATUS_BUSY
)) {
1897 //Only runing process will interrupt this call. Events are signaled when status change. Extra timeout add for safe reason.
1898 wait_event_interruptible_timeout(instance
->wait_queue
,
1899 ((instance
->status
!=
1900 ao_status_single_run_wait
)
1901 && (instance
->status
!=
1902 ao_status_single_run
)
1903 && (instance
->status
!=
1904 ao_status_single_end_wait
)
1905 && (instance
->status
!=
1906 ao_status_stream_run_wait
)
1907 && (instance
->status
!=
1908 ao_status_stream_run
)
1909 && (instance
->status
!=
1910 ao_status_stream_end_wait
)),
1913 if (instance
->status
!= ao_status_stream_end
) {
1914 PDEBUG("Wait for IDLE canceled. %d\n",
1916 err
= ME_ERRNO_CANCELLED
;
1919 if (signal_pending(current
)) {
1920 PERROR("Wait for IDLE interrupted.\n");
1921 instance
->status
= ao_status_none
;
1922 ao_stop_immediately(instance
);
1923 err
= ME_ERRNO_SIGNAL
;
1926 *status
= ME_STATUS_IDLE
;
1929 *values
= me_circ_buf_space(&instance
->circ_buf
);
1936 static int me4600_ao_io_stream_stop(me_subdevice_t
*subdevice
,
1938 int stop_mode
, int flags
)
1939 { // Stop work and empty buffer and FIFO
1940 int err
= ME_ERRNO_SUCCESS
;
1941 me4600_ao_subdevice_t
*instance
;
1942 unsigned long cpu_flags
;
1943 volatile uint32_t ctrl
;
1945 instance
= (me4600_ao_subdevice_t
*) subdevice
;
1947 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
1949 if (flags
& ~ME_IO_STREAM_STOP_PRESERVE_BUFFERS
) {
1950 PERROR("Invalid flag specified.\n");
1951 return ME_ERRNO_INVALID_FLAGS
;
1954 if ((stop_mode
!= ME_STOP_MODE_IMMEDIATE
)
1955 && (stop_mode
!= ME_STOP_MODE_LAST_VALUE
)) {
1956 PERROR("Invalid stop mode specified.\n");
1957 return ME_ERRNO_INVALID_STOP_MODE
;
1960 if (!instance
->fifo
) {
1961 PERROR("Not a streaming ao.\n");
1962 return ME_ERRNO_NOT_SUPPORTED
;
1965 if (instance
->status
< ao_status_stream_configured
) {
1966 //There is nothing to stop!
1967 PERROR("Subdevice not in streaming mode. %d\n",
1969 return ME_ERRNO_PREVIOUS_CONFIG
;
1974 //Mark as stopping. => Software stop.
1975 instance
->status
= ao_status_stream_end_wait
;
1977 if (stop_mode
== ME_STOP_MODE_IMMEDIATE
) { //Stopped now!
1978 err
= ao_stop_immediately(instance
);
1979 } else if (stop_mode
== ME_STOP_MODE_LAST_VALUE
) {
1980 ctrl
= inl(instance
->ctrl_reg
) & ME4600_AO_CTRL_MODE_MASK
;
1981 if (ctrl
== ME4600_AO_MODE_WRAPAROUND
) { //Hardware wraparound => Hardware stop.
1982 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
1983 ctrl
= inl(instance
->ctrl_reg
);
1985 ME4600_AO_CTRL_BIT_STOP
|
1986 ME4600_AO_CTRL_BIT_RESET_IRQ
;
1987 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
1988 outl(ctrl
, instance
->ctrl_reg
);
1989 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
1991 instance
->ctrl_reg
- instance
->reg_base
,
1993 spin_unlock_irqrestore(&instance
->subdevice_lock
,
1996 //Only runing process will interrupt this call. Events are signaled when status change.
1997 wait_event_interruptible_timeout(instance
->wait_queue
,
1998 (instance
->status
!=
1999 ao_status_stream_end_wait
),
2002 if (instance
->status
!= ao_status_stream_end
) {
2003 PDEBUG("Stopping stream canceled.\n");
2004 err
= ME_ERRNO_CANCELLED
;
2007 if (signal_pending(current
)) {
2008 PERROR("Stopping stream interrupted.\n");
2009 instance
->status
= ao_status_none
;
2010 ao_stop_immediately(instance
);
2011 err
= ME_ERRNO_SIGNAL
;
2015 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2016 ctrl
= inl(instance
->ctrl_reg
);
2018 ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
2019 ME4600_AO_CTRL_BIT_RESET_IRQ
;
2020 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2021 if (!flags
) { //Reset FIFO
2022 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
2024 outl(ctrl
, instance
->ctrl_reg
);
2025 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
2026 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2027 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
2029 if (!flags
) { //Reset software buffer
2030 instance
->circ_buf
.head
= 0;
2031 instance
->circ_buf
.tail
= 0;
2032 instance
->preloaded_count
= 0;
2033 instance
->data_count
= 0;
2041 static int me4600_ao_io_stream_write(me_subdevice_t
*subdevice
,
2044 int *values
, int *count
, int flags
)
2046 int err
= ME_ERRNO_SUCCESS
;
2047 me4600_ao_subdevice_t
*instance
;
2048 unsigned long cpu_flags
= 0;
2051 int copied_from_user
= 0;
2052 int left_to_copy_from_user
= *count
;
2056 instance
= (me4600_ao_subdevice_t
*) subdevice
;
2058 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
2060 //Checking arguments
2061 if (!instance
->fifo
) {
2062 PERROR("Not a streaming ao.\n");
2063 return ME_ERRNO_NOT_SUPPORTED
;
2067 PERROR("Invalid flag specified.\n");
2068 return ME_ERRNO_INVALID_FLAGS
;
2072 PERROR("Invalid count of values specified.\n");
2073 return ME_ERRNO_INVALID_VALUE_COUNT
;
2076 if (values
== NULL
) {
2077 PERROR("Invalid address of values specified.\n");
2078 return ME_ERRNO_INVALID_POINTER
;
2081 if ((instance
->status
== ao_status_none
) || (instance
->status
== ao_status_single_configured
)) { //The device is in single mode.
2083 ("Subdevice must be preinitialize correctly for streaming.\n");
2084 return ME_ERRNO_PREVIOUS_CONFIG
;
2086 /// @note If no 'pre-load' is used. stream_start() will move data to FIFO.
2087 switch (write_mode
) {
2088 case ME_WRITE_MODE_PRELOAD
:
2090 //Device must be stopped.
2091 if ((instance
->status
!= ao_status_stream_configured
)
2092 && (instance
->status
!= ao_status_stream_end
)) {
2094 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2095 return ME_ERRNO_PREVIOUS_CONFIG
;
2098 case ME_WRITE_MODE_NONBLOCKING
:
2099 case ME_WRITE_MODE_BLOCKING
:
2100 /// @note In blocking mode: When device is not runing and there is not enought space call will blocked up!
2101 /// @note Some other thread must empty buffer by starting engine.
2105 PERROR("Invalid write mode specified.\n");
2106 return ME_ERRNO_INVALID_WRITE_MODE
;
2109 if (instance
->mode
& ME4600_AO_WRAP_MODE
) { //Wraparound mode. Device must be stopped.
2110 if ((instance
->status
!= ao_status_stream_configured
)
2111 && (instance
->status
!= ao_status_stream_end
)) {
2113 ("Subdevice mustn't be runing when 'pre-load' mode is used.\n");
2114 return ME_ERRNO_INVALID_WRITE_MODE
;
2118 if ((instance
->mode
== ME4600_AO_HW_WRAP_MODE
) && (write_mode
!= ME_WRITE_MODE_PRELOAD
)) { // hardware wrap_around mode.
2119 //This is transparent for user.
2120 PDEBUG("Changing write_mode to ME_WRITE_MODE_PRELOAD.\n");
2121 write_mode
= ME_WRITE_MODE_PRELOAD
;
2126 if (write_mode
== ME_WRITE_MODE_PRELOAD
) { //Init enviroment - preload
2127 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2128 reg_copy
= inl(instance
->ctrl_reg
);
2130 if (!(reg_copy
& ME4600_AO_CTRL_BIT_ENABLE_FIFO
)) { //FIFO not active. Enable it.
2131 reg_copy
|= ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
2132 outl(reg_copy
, instance
->ctrl_reg
);
2133 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2135 instance
->ctrl_reg
- instance
->reg_base
,
2137 instance
->preloaded_count
= 0;
2139 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
2143 //Copy to buffer. This step is common for all modes.
2145 ao_get_data_from_user(instance
, left_to_copy_from_user
,
2147 left_to_copy_from_user
));
2148 left_to_copy_from_user
-= copied_from_user
;
2150 reg_copy
= inl(instance
->status_reg
);
2151 if ((instance
->status
== ao_status_stream_run
) && !(reg_copy
& ME4600_AO_STATUS_BIT_FSM
)) { //BROKEN PIPE! The state machine is stoped but logical status show that should be working.
2152 PERROR("Broken pipe in write.\n");
2153 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
2157 if ((instance
->status
== ao_status_stream_run
) && (instance
->mode
== ME4600_AO_CONTINOUS
) && (reg_copy
& ME4600_AO_STATUS_BIT_HF
)) { //Continous mode runing and data are below half!
2159 // Block interrupts.
2160 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2161 reg_copy
= inl(instance
->ctrl_reg
);
2162 //reg_copy &= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ;
2163 reg_copy
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
2164 outl(reg_copy
, instance
->ctrl_reg
);
2165 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2167 instance
->ctrl_reg
- instance
->reg_base
,
2169 spin_unlock_irqrestore(&instance
->subdevice_lock
,
2174 ao_write_data(instance
, ME4600_AO_FIFO_COUNT
/ 2,
2176 if (copied_values
> 0) {
2177 instance
->circ_buf
.tail
+= copied_values
;
2178 instance
->circ_buf
.tail
&=
2179 instance
->circ_buf
.mask
;
2182 // Activate interrupts.
2183 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2184 reg_copy
= inl(instance
->ctrl_reg
);
2185 //reg_copy |= ME4600_AO_CTRL_BIT_ENABLE_IRQ;
2186 reg_copy
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
2187 outl(reg_copy
, instance
->ctrl_reg
);
2188 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2190 instance
->ctrl_reg
- instance
->reg_base
,
2192 spin_unlock_irqrestore(&instance
->subdevice_lock
,
2195 if (copied_values
== 0) { //This was checked and never should happend!
2196 PERROR_CRITICAL("COPING FINISH WITH 0!\n");
2199 if (copied_values
< 0) { //This was checked and never should happend!
2201 ("COPING FINISH WITH AN ERROR!\n");
2202 instance
->status
= ao_status_stream_fifo_error
;
2203 err
= ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2208 if (!left_to_copy_from_user
) { //All datas were copied.
2210 } else { //Not all datas were copied.
2211 if (instance
->mode
& ME4600_AO_WRAP_MODE
) { //Error too much datas! Wraparound is limited in size!
2213 ("Too much data for wraparound mode! Exceeded size of %ld.\n",
2214 ME4600_AO_CIRC_BUF_COUNT
- 1);
2215 err
= ME_ERRNO_RING_BUFFER_OVERFLOW
;
2219 if (write_mode
!= ME_WRITE_MODE_BLOCKING
) { //Non blocking calls
2223 wait_event_interruptible(instance
->wait_queue
,
2224 me_circ_buf_space(&instance
->
2227 if (signal_pending(current
)) {
2228 PERROR("Writing interrupted by signal.\n");
2229 instance
->status
= ao_status_none
;
2230 ao_stop_immediately(instance
);
2231 err
= ME_ERRNO_SIGNAL
;
2235 if (instance
->status
== ao_status_none
) { //Reset
2236 PERROR("Writing interrupted by reset.\n");
2237 err
= ME_ERRNO_CANCELLED
;
2243 if (write_mode
== ME_WRITE_MODE_PRELOAD
) { //Copy data to FIFO - preload
2245 ao_write_data_pooling(instance
, ME4600_AO_FIFO_COUNT
,
2246 instance
->preloaded_count
);
2247 instance
->preloaded_count
+= copied_values
;
2248 instance
->data_count
+= copied_values
;
2250 if ((instance
->mode
== ME4600_AO_HW_WRAP_MODE
)
2251 && (me_circ_buf_values(&instance
->circ_buf
) >
2252 ME4600_AO_FIFO_COUNT
)) {
2254 ("Too much data for hardware wraparound mode! Exceeded size of %d.\n",
2255 ME4600_AO_FIFO_COUNT
);
2256 err
= ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2260 *count
= *count
- left_to_copy_from_user
;
2265 static irqreturn_t
me4600_ao_isr(int irq
, void *dev_id
)
2267 me4600_ao_subdevice_t
*instance
= dev_id
;
2268 uint32_t irq_status
;
2273 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
2275 if (irq
!= instance
->irq
) {
2276 PERROR("Incorrect interrupt num: %d.\n", irq
);
2280 irq_status
= inl(instance
->irq_status_reg
);
2281 if (!(irq_status
& (ME4600_IRQ_STATUS_BIT_AO_HF
<< instance
->ao_idx
))) {
2282 PINFO("%ld Shared interrupt. %s(): ID=%d: status_reg=0x%04X\n",
2283 jiffies
, __func__
, instance
->ao_idx
, irq_status
);
2287 if (!instance
->circ_buf
.buf
) {
2288 instance
->status
= ao_status_stream_error
;
2289 PERROR_CRITICAL("CIRCULAR BUFFER NOT EXISTS!\n");
2290 //Block interrupts. Stop machine.
2291 ctrl
= inl(instance
->ctrl_reg
);
2292 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2294 ME4600_AO_CTRL_BIT_RESET_IRQ
|
2295 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_STOP
;
2296 outl(ctrl
, instance
->ctrl_reg
);
2297 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2299 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2302 wake_up_interruptible_all(&instance
->wait_queue
);
2306 status
= inl(instance
->status_reg
);
2307 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { //Too late. Not working! END? BROKEN PIPE?
2308 PDEBUG("Interrupt come but ISM is not working!\n");
2309 //Block interrupts. Stop machine.
2310 ctrl
= inl(instance
->ctrl_reg
);
2311 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2313 ME4600_AO_CTRL_BIT_RESET_IRQ
| ME4600_AO_CTRL_BIT_STOP
|
2314 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
2315 outl(ctrl
, instance
->ctrl_reg
);
2316 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2318 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2322 //General procedure. Process more datas.
2324 #ifdef MEDEBUG_DEBUG
2325 if (!me_circ_buf_values(&instance
->circ_buf
)) { //Buffer is empty!
2326 PDEBUG("Circular buffer empty!\n");
2331 if (status
& ME4600_AO_STATUS_BIT_HF
) { //OK less than half
2334 ctrl
= inl(instance
->ctrl_reg
);
2335 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2336 ctrl
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
2337 outl(ctrl
, instance
->ctrl_reg
);
2338 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2340 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2343 //Calculate how many should be copied.
2345 (instance
->stop_data_count
) ? instance
->
2347 instance
->data_count
: ME4600_AO_FIFO_COUNT
/ 2;
2348 if (ME4600_AO_FIFO_COUNT
/ 2 < count
) {
2349 count
= ME4600_AO_FIFO_COUNT
/ 2;
2352 if (instance
->mode
== ME4600_AO_CONTINOUS
) { //Continous
2353 count
= ao_write_data(instance
, count
, 0);
2355 instance
->circ_buf
.tail
+= count
;
2356 instance
->circ_buf
.tail
&=
2357 instance
->circ_buf
.mask
;
2358 instance
->data_count
+= count
;
2360 if ((instance
->status
== ao_status_stream_end_wait
) && !me_circ_buf_values(&instance
->circ_buf
)) { //Stoping. Whole buffer was copied.
2364 } else if ((instance
->mode
== ME4600_AO_SW_WRAP_MODE
) && ((ctrl
& ME4600_AO_CTRL_MODE_MASK
) == ME4600_AO_MODE_CONTINUOUS
)) { //Wraparound (software)
2365 if (instance
->status
== ao_status_stream_end_wait
) { //We stoping => Copy to the end of the buffer.
2367 ao_write_data(instance
, count
, 0);
2368 } else { //Copy in wraparound mode.
2370 ao_write_data_wraparound(instance
,
2377 instance
->data_count
+= count
;
2378 instance
->preloaded_count
+= count
;
2379 instance
->preloaded_count
%=
2380 me_circ_buf_values(&instance
->
2383 if ((instance
->status
== ao_status_stream_end_wait
) && !instance
->preloaded_count
) { //Stoping. Whole buffer was copied.
2389 if ((count
<= 0) || (instance
->stop_data_count
&& (instance
->stop_data_count
<= instance
->data_count
))) { //End of work.
2392 } //Repeat if still is under half fifo
2394 inl(instance
->status_reg
)) & ME4600_AO_STATUS_BIT_HF
);
2396 //Unblock interrupts
2397 ctrl
= inl(instance
->ctrl_reg
);
2398 if (count
>= 0) { //Copy was successful.
2399 if (instance
->stop_data_count
&& (instance
->stop_data_count
<= instance
->data_count
)) { //Finishing work. No more interrupts.
2400 PDEBUG("Finishing work. Interrupt disabled.\n");
2401 instance
->status
= ao_status_stream_end_wait
;
2402 } else if (count
> 0) { //Normal work. Enable interrupt.
2403 PDEBUG("Normal work. Enable interrupt.\n");
2404 ctrl
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
2405 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2406 } else { //Normal work but there are no more data in buffer. Interrupt active but blocked. stream_write() will unblock it.
2408 ("No data in software buffer. Interrupt blocked.\n");
2409 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
2411 } else { //Error during copy.
2412 instance
->status
= ao_status_stream_fifo_error
;
2415 outl(ctrl
, instance
->ctrl_reg
);
2416 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2418 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2419 } else { //?? more than half
2421 ("Interrupt come but FIFO more than half full! Reset interrupt.\n");
2422 //Reset pending interrupt
2423 ctrl
= inl(instance
->ctrl_reg
);
2424 ctrl
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
2425 outl(ctrl
, instance
->ctrl_reg
);
2426 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2428 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2429 ctrl
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
2430 outl(ctrl
, instance
->ctrl_reg
);
2431 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2433 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2436 PINFO("ISR: Buffer count: %d.(T:%d H:%d)\n",
2437 me_circ_buf_values(&instance
->circ_buf
), instance
->circ_buf
.tail
,
2438 instance
->circ_buf
.head
);
2439 PINFO("ISR: Stop count: %d.\n", instance
->stop_count
);
2440 PINFO("ISR: Stop data count: %d.\n", instance
->stop_data_count
);
2441 PINFO("ISR: Data count: %d.\n", instance
->data_count
);
2444 wake_up_interruptible_all(&instance
->wait_queue
);
2449 static void me4600_ao_destructor(struct me_subdevice
*subdevice
)
2451 me4600_ao_subdevice_t
*instance
;
2453 instance
= (me4600_ao_subdevice_t
*) subdevice
;
2455 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
2457 instance
->ao_control_task_flag
= 0;
2459 // Reset subdevice to asure clean exit.
2460 me4600_ao_io_reset_subdevice(subdevice
, NULL
,
2461 ME_IO_RESET_SUBDEVICE_NO_FLAGS
);
2463 // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
2464 if (!cancel_delayed_work(&instance
->ao_control_task
)) { //Wait 2 ticks to be sure that control task is removed from queue.
2465 set_current_state(TASK_INTERRUPTIBLE
);
2466 schedule_timeout(2);
2469 if (instance
->fifo
) {
2470 if (instance
->irq
) {
2471 free_irq(instance
->irq
, instance
);
2475 if (instance
->circ_buf
.buf
) {
2476 free_pages((unsigned long)instance
->circ_buf
.buf
,
2477 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2479 instance
->circ_buf
.buf
= NULL
;
2482 me_subdevice_deinit(&instance
->base
);
2486 me4600_ao_subdevice_t
*me4600_ao_constructor(uint32_t reg_base
,
2487 spinlock_t
*preload_reg_lock
,
2488 uint32_t *preload_flags
,
2492 struct workqueue_struct
*me4600_wq
)
2494 me4600_ao_subdevice_t
*subdevice
;
2497 PDEBUG("executed. idx=%d\n", ao_idx
);
2499 // Allocate memory for subdevice instance.
2500 subdevice
= kmalloc(sizeof(me4600_ao_subdevice_t
), GFP_KERNEL
);
2503 PERROR("Cannot get memory for subdevice instance.\n");
2507 memset(subdevice
, 0, sizeof(me4600_ao_subdevice_t
));
2509 // Initialize subdevice base class.
2510 err
= me_subdevice_init(&subdevice
->base
);
2513 PERROR("Cannot initialize subdevice base class instance.\n");
2517 // Initialize spin locks.
2518 spin_lock_init(&subdevice
->subdevice_lock
);
2520 subdevice
->preload_reg_lock
= preload_reg_lock
;
2521 subdevice
->preload_flags
= preload_flags
;
2523 // Store analog output index.
2524 subdevice
->ao_idx
= ao_idx
;
2526 // Store if analog output has fifo.
2527 subdevice
->fifo
= (ao_idx
< fifo
) ? 1 : 0;
2529 if (subdevice
->fifo
) { // Allocate and initialize circular buffer.
2530 subdevice
->circ_buf
.mask
= ME4600_AO_CIRC_BUF_COUNT
- 1;
2532 subdevice
->circ_buf
.buf
=
2533 (void *)__get_free_pages(GFP_KERNEL
,
2534 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2535 PDEBUG("circ_buf = %p size=%ld\n", subdevice
->circ_buf
.buf
,
2536 ME4600_AO_CIRC_BUF_SIZE
);
2538 if (!subdevice
->circ_buf
.buf
) {
2540 ("Cannot initialize subdevice base class instance.\n");
2545 memset(subdevice
->circ_buf
.buf
, 0, ME4600_AO_CIRC_BUF_SIZE
);
2546 } else { // No FIFO.
2547 subdevice
->circ_buf
.mask
= 0;
2548 subdevice
->circ_buf
.buf
= NULL
;
2551 subdevice
->circ_buf
.head
= 0;
2552 subdevice
->circ_buf
.tail
= 0;
2554 subdevice
->status
= ao_status_none
;
2555 subdevice
->ao_control_task_flag
= 0;
2556 subdevice
->timeout
.delay
= 0;
2557 subdevice
->timeout
.start_time
= jiffies
;
2559 // Initialize wait queue.
2560 init_waitqueue_head(&subdevice
->wait_queue
);
2562 // Initialize single value to 0V.
2563 subdevice
->single_value
= 0x8000;
2564 subdevice
->single_value_in_fifo
= 0x8000;
2566 // Register interrupt service routine.
2567 if (subdevice
->fifo
) {
2568 subdevice
->irq
= irq
;
2569 if (request_irq(subdevice
->irq
, me4600_ao_isr
,
2570 IRQF_DISABLED
| IRQF_SHARED
,
2571 ME4600_NAME
, subdevice
)) {
2572 PERROR("Cannot get interrupt line.\n");
2573 PDEBUG("free circ_buf = %p size=%d",
2574 subdevice
->circ_buf
.buf
,
2575 PAGE_SHIFT
<< ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2576 free_pages((unsigned long)subdevice
->circ_buf
.buf
,
2577 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2578 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
2582 PINFO("Registered irq=%d.\n", subdevice
->irq
);
2587 // Initialize registers.
2588 subdevice
->irq_status_reg
= reg_base
+ ME4600_IRQ_STATUS_REG
;
2589 subdevice
->preload_reg
= reg_base
+ ME4600_AO_SYNC_REG
;
2591 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_00_CTRL_REG
;
2592 subdevice
->status_reg
= reg_base
+ ME4600_AO_00_STATUS_REG
;
2593 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_00_FIFO_REG
;
2594 subdevice
->single_reg
= reg_base
+ ME4600_AO_00_SINGLE_REG
;
2595 subdevice
->timer_reg
= reg_base
+ ME4600_AO_00_TIMER_REG
;
2596 subdevice
->reg_base
= reg_base
;
2597 subdevice
->bitpattern
= 0;
2598 } else if (ao_idx
== 1) {
2599 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_01_CTRL_REG
;
2600 subdevice
->status_reg
= reg_base
+ ME4600_AO_01_STATUS_REG
;
2601 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_01_FIFO_REG
;
2602 subdevice
->single_reg
= reg_base
+ ME4600_AO_01_SINGLE_REG
;
2603 subdevice
->timer_reg
= reg_base
+ ME4600_AO_01_TIMER_REG
;
2604 subdevice
->reg_base
= reg_base
;
2605 subdevice
->bitpattern
= 0;
2606 } else if (ao_idx
== 2) {
2607 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_02_CTRL_REG
;
2608 subdevice
->status_reg
= reg_base
+ ME4600_AO_02_STATUS_REG
;
2609 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_02_FIFO_REG
;
2610 subdevice
->single_reg
= reg_base
+ ME4600_AO_02_SINGLE_REG
;
2611 subdevice
->timer_reg
= reg_base
+ ME4600_AO_02_TIMER_REG
;
2612 subdevice
->reg_base
= reg_base
;
2613 subdevice
->bitpattern
= 0;
2614 } else if (ao_idx
== 3) {
2615 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_03_CTRL_REG
;
2616 subdevice
->status_reg
= reg_base
+ ME4600_AO_03_STATUS_REG
;
2617 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_03_FIFO_REG
;
2618 subdevice
->single_reg
= reg_base
+ ME4600_AO_03_SINGLE_REG
;
2619 subdevice
->timer_reg
= reg_base
+ ME4600_AO_03_TIMER_REG
;
2620 subdevice
->reg_base
= reg_base
;
2621 subdevice
->bitpattern
= 1;
2623 PERROR_CRITICAL("WRONG SUBDEVICE idx=%d!", ao_idx
);
2624 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
2625 if (subdevice
->fifo
) {
2626 free_pages((unsigned long)subdevice
->circ_buf
.buf
,
2627 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2629 subdevice
->circ_buf
.buf
= NULL
;
2634 // Override base class methods.
2635 subdevice
->base
.me_subdevice_destructor
= me4600_ao_destructor
;
2636 subdevice
->base
.me_subdevice_io_reset_subdevice
=
2637 me4600_ao_io_reset_subdevice
;
2638 subdevice
->base
.me_subdevice_io_single_config
=
2639 me4600_ao_io_single_config
;
2640 subdevice
->base
.me_subdevice_io_single_read
= me4600_ao_io_single_read
;
2641 subdevice
->base
.me_subdevice_io_single_write
=
2642 me4600_ao_io_single_write
;
2643 subdevice
->base
.me_subdevice_io_stream_config
=
2644 me4600_ao_io_stream_config
;
2645 subdevice
->base
.me_subdevice_io_stream_new_values
=
2646 me4600_ao_io_stream_new_values
;
2647 subdevice
->base
.me_subdevice_io_stream_write
=
2648 me4600_ao_io_stream_write
;
2649 subdevice
->base
.me_subdevice_io_stream_start
=
2650 me4600_ao_io_stream_start
;
2651 subdevice
->base
.me_subdevice_io_stream_status
=
2652 me4600_ao_io_stream_status
;
2653 subdevice
->base
.me_subdevice_io_stream_stop
= me4600_ao_io_stream_stop
;
2654 subdevice
->base
.me_subdevice_query_number_channels
=
2655 me4600_ao_query_number_channels
;
2656 subdevice
->base
.me_subdevice_query_subdevice_type
=
2657 me4600_ao_query_subdevice_type
;
2658 subdevice
->base
.me_subdevice_query_subdevice_caps
=
2659 me4600_ao_query_subdevice_caps
;
2660 subdevice
->base
.me_subdevice_query_subdevice_caps_args
=
2661 me4600_ao_query_subdevice_caps_args
;
2662 subdevice
->base
.me_subdevice_query_range_by_min_max
=
2663 me4600_ao_query_range_by_min_max
;
2664 subdevice
->base
.me_subdevice_query_number_ranges
=
2665 me4600_ao_query_number_ranges
;
2666 subdevice
->base
.me_subdevice_query_range_info
=
2667 me4600_ao_query_range_info
;
2668 subdevice
->base
.me_subdevice_query_timer
= me4600_ao_query_timer
;
2670 // Prepare work queue
2671 subdevice
->me4600_workqueue
= me4600_wq
;
2673 /* workqueue API changed in kernel 2.6.20 */
2674 INIT_DELAYED_WORK(&subdevice
->ao_control_task
,
2675 me4600_ao_work_control_task
);
2677 if (subdevice
->fifo
) { // Set speed for single operations.
2678 outl(ME4600_AO_MIN_CHAN_TICKS
- 1, subdevice
->timer_reg
);
2679 subdevice
->hardware_stop_delay
= HZ
/ 10; //100ms
2685 /** @brief Stop presentation. Preserve FIFOs.
2687 * @param instance The subdevice instance (pointer).
2689 inline int ao_stop_immediately(me4600_ao_subdevice_t
*instance
)
2691 unsigned long cpu_flags
;
2697 (instance
->hardware_stop_delay
>
2698 (HZ
/ 10)) ? instance
->hardware_stop_delay
: HZ
/ 10;
2699 for (i
= 0; i
<= timeout
; i
++) {
2700 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2701 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2702 ctrl
= inl(instance
->ctrl_reg
);
2704 ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
2705 | ME4600_AO_CTRL_BIT_RESET_IRQ
;
2707 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
2708 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
2709 outl(ctrl
, instance
->ctrl_reg
);
2710 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2712 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2713 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
2715 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) { // Exit.
2719 set_current_state(TASK_INTERRUPTIBLE
);
2720 schedule_timeout(1);
2724 PERROR_CRITICAL("FSM IS BUSY!\n");
2725 return ME_ERRNO_INTERNAL
;
2727 return ME_ERRNO_SUCCESS
;
2730 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2731 * @note This is time critical function. Checking is done at begining and end only.
2732 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2734 * @param instance The subdevice instance (pointer).
2735 * @param count Maximum number of copied data.
2736 * @param start_pos Position of the firs value in buffer.
2738 * @return On success: Number of copied data.
2739 * @return On error/success: 0. No datas were copied => no data in buffer.
2740 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2742 inline int ao_write_data_wraparound(me4600_ao_subdevice_t
*instance
, int count
,
2744 { /// @note This is time critical function!
2748 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2749 int local_count
= count
;
2752 if (count
<= 0) { //Wrong count!
2756 while (i
< local_count
) {
2757 //Get value from buffer
2758 value
= *(instance
->circ_buf
.buf
+ pos
);
2760 if (instance
->ao_idx
& 0x1) {
2764 outl(value
, instance
->fifo_reg
);
2765 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2768 pos
&= instance
->circ_buf
.mask
;
2769 if (pos
== instance
->circ_buf
.head
) {
2770 pos
= instance
->circ_buf
.tail
;
2775 status
= inl(instance
->status_reg
);
2776 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2777 PERROR("FIFO was full before all datas were copied! idx=%d\n",
2779 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2780 } else { //Add last value
2781 value
= *(instance
->circ_buf
.buf
+ pos
);
2782 if (instance
->ao_idx
& 0x1) {
2786 outl(value
, instance
->fifo_reg
);
2787 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2790 PINFO("WRAPAROUND LOADED %d values. idx=%d\n", local_count
,
2795 /** @brief Copy data from software buffer to fifo (fast).
2796 * @note This is time critical function. Checking is done at begining and end only.
2797 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2799 * @param instance The subdevice instance (pointer).
2800 * @param count Maximum number of copied data.
2801 * @param start_pos Position of the firs value in buffer.
2803 * @return On success: Number of copied data.
2804 * @return On error/success: 0. No datas were copied => no data in buffer.
2805 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2807 inline int ao_write_data(me4600_ao_subdevice_t
*instance
, int count
,
2809 { /// @note This is time critical function!
2813 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2814 int local_count
= count
;
2818 if (count
<= 0) { //Wrong count!
2822 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
2823 if (max_count
<= 0) { //No data to copy!
2827 if (max_count
< count
) {
2828 local_count
= max_count
;
2831 while (i
< local_count
) {
2832 //Get value from buffer
2833 value
= *(instance
->circ_buf
.buf
+ pos
);
2835 if (instance
->ao_idx
& 0x1) {
2839 outl(value
, instance
->fifo_reg
);
2840 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2843 pos
&= instance
->circ_buf
.mask
;
2847 status
= inl(instance
->status_reg
);
2848 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2849 PERROR("FIFO was full before all datas were copied! idx=%d\n",
2851 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2852 } else { //Add last value
2853 value
= *(instance
->circ_buf
.buf
+ pos
);
2854 if (instance
->ao_idx
& 0x1) {
2858 outl(value
, instance
->fifo_reg
);
2859 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2862 PINFO("FAST LOADED %d values. idx=%d\n", local_count
, instance
->ao_idx
);
2866 /** @brief Copy data from software buffer to fifo (slow).
2867 * @note This is slow function that copy all data from buffer to FIFO with full control.
2869 * @param instance The subdevice instance (pointer).
2870 * @param count Maximum number of copied data.
2871 * @param start_pos Position of the firs value in buffer.
2873 * @return On success: Number of copied values.
2874 * @return On error/success: 0. FIFO was full at begining.
2875 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
2877 inline int ao_write_data_pooling(me4600_ao_subdevice_t
*instance
, int count
,
2879 { /// @note This is slow function!
2883 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2884 int local_count
= count
;
2888 if (count
<= 0) { //Wrong count!
2889 PERROR("SLOW LOADED: Wrong count! idx=%d\n", instance
->ao_idx
);
2893 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
2894 if (max_count
<= 0) { //No data to copy!
2895 PERROR("SLOW LOADED: No data to copy! idx=%d\n",
2900 if (max_count
< count
) {
2901 local_count
= max_count
;
2904 for (i
= 0; i
< local_count
; i
++) {
2905 status
= inl(instance
->status_reg
);
2906 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full!
2909 //Get value from buffer
2910 value
= *(instance
->circ_buf
.buf
+ pos
);
2912 if (instance
->ao_idx
& 0x1) {
2916 outl(value
, instance
->fifo_reg
);
2917 //PDEBUG_REG("idx=%d fifo_reg outl(0x%lX+0x%lX)=0x%x\n", instance->ao_idx, instance->reg_base, instance->fifo_reg - instance->reg_base, value);
2920 pos
&= instance
->circ_buf
.mask
;
2923 PINFO("SLOW LOADED %d values. idx=%d\n", local_count
, instance
->ao_idx
);
2927 /** @brief Copy data from user space to circular buffer.
2928 * @param instance The subdevice instance (pointer).
2929 * @param count Number of datas in user space.
2930 * @param user_values Buffer's pointer.
2932 * @return On success: Number of copied values.
2933 * @return On error: -ME_ERRNO_INTERNAL.
2935 inline int ao_get_data_from_user(me4600_ao_subdevice_t
*instance
, int count
,
2943 empty_space
= me_circ_buf_space(&instance
->circ_buf
);
2944 //We have only this space free.
2945 copied
= (count
< empty_space
) ? count
: empty_space
;
2946 for (i
= 0; i
< copied
; i
++) { //Copy from user to buffer
2947 if ((err
= get_user(value
, (int *)(user_values
+ i
)))) {
2949 ("BUFFER LOADED: get_user(0x%p) return an error: %d. idx=%d\n",
2950 user_values
+ i
, err
, instance
->ao_idx
);
2951 return -ME_ERRNO_INTERNAL
;
2953 /// @note The analog output in me4600 series has size of 16 bits.
2954 *(instance
->circ_buf
.buf
+ instance
->circ_buf
.head
) =
2956 instance
->circ_buf
.head
++;
2957 instance
->circ_buf
.head
&= instance
->circ_buf
.mask
;
2960 PINFO("BUFFER LOADED %d values. idx=%d\n", copied
, instance
->ao_idx
);
2964 /** @brief Checking actual hardware and logical state.
2965 * @param instance The subdevice instance (pointer).
2967 static void me4600_ao_work_control_task(struct work_struct
*work
)
2969 me4600_ao_subdevice_t
*instance
;
2970 unsigned long cpu_flags
= 0;
2978 container_of((void *)work
, me4600_ao_subdevice_t
, ao_control_task
);
2979 PINFO("<%s: %ld> executed. idx=%d\n", __func__
, jiffies
,
2982 status
= inl(instance
->status_reg
);
2983 PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
2984 instance
->status_reg
- instance
->reg_base
, status
);
2986 switch (instance
->status
) { // Checking actual mode.
2988 // Not configured for work.
2989 case ao_status_none
:
2992 //This are stable modes. No need to do anything. (?)
2993 case ao_status_single_configured
:
2994 case ao_status_stream_configured
:
2995 case ao_status_stream_fifo_error
:
2996 case ao_status_stream_buffer_error
:
2997 case ao_status_stream_error
:
2998 PERROR("Shouldn't be running!.\n");
3001 case ao_status_stream_end
:
3002 if (!instance
->fifo
) {
3004 ("Streaming on single device! This feature is not implemented in this version!\n");
3005 instance
->status
= ao_status_stream_error
;
3010 case ao_status_single_end
:
3011 if (status
& ME4600_AO_STATUS_BIT_FSM
) { // State machine is working but the status is set to end. Force stop.
3017 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3018 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3019 ctrl
= inl(instance
->ctrl_reg
);
3021 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_STOP
3022 | ME4600_AO_CTRL_BIT_RESET_IRQ
;
3024 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3025 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3027 ~(ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3028 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
3029 outl(ctrl
, instance
->ctrl_reg
);
3030 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3032 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
3033 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3037 case ao_status_single_run_wait
:
3038 case ao_status_single_run
:
3039 case ao_status_single_end_wait
:
3041 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working.
3042 if (((instance
->fifo
)
3043 && (!(status
& ME4600_AO_STATUS_BIT_EF
)))
3044 || (!(instance
->fifo
))) { // Single is in end state.
3045 PDEBUG("Single call has been complited.\n");
3047 // Set correct value for single_read();
3048 instance
->single_value
=
3049 instance
->single_value_in_fifo
;
3051 // Set status as 'ao_status_single_end'
3052 instance
->status
= ao_status_single_end
;
3056 // Wait for stop ISM.
3063 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3064 PDEBUG("Timeout reached.\n");
3065 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3066 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3067 ctrl
= inl(instance
->ctrl_reg
);
3069 ME4600_AO_CTRL_BIT_STOP
|
3070 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
3071 ME4600_AO_CTRL_BIT_RESET_IRQ
;
3073 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3074 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3075 /// Fix for timeout error.
3077 ~(ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3078 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
3079 if (instance
->fifo
) { //Disabling FIFO
3080 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
3082 outl(ctrl
, instance
->ctrl_reg
);
3083 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3085 instance
->ctrl_reg
- instance
->reg_base
,
3087 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3090 spin_lock(instance
->preload_reg_lock
);
3091 //Remove from synchronous start. Block triggering from this output.
3092 synch
= inl(instance
->preload_reg
);
3094 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) <<
3096 if (!(instance
->fifo
)) { // No FIFO - set to single safe mode
3098 ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
3100 outl(synch
, instance
->preload_reg
);
3101 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3103 instance
->preload_reg
- instance
->reg_base
,
3105 spin_unlock(instance
->preload_reg_lock
);
3107 if (!(instance
->fifo
)) { // No FIFO
3108 // Restore old settings.
3109 PDEBUG("Write old value back to register.\n");
3110 outl(instance
->single_value
,
3111 instance
->single_reg
);
3113 ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3115 instance
->single_reg
- instance
->reg_base
,
3116 instance
->single_value
);
3118 // Set correct value for single_read();
3119 instance
->single_value_in_fifo
= instance
->single_value
;
3121 instance
->status
= ao_status_single_end
;
3131 case ao_status_stream_run_wait
:
3132 if (!instance
->fifo
) {
3134 ("Streaming on single device! This feature is not implemented in this version!\n");
3135 instance
->status
= ao_status_stream_error
;
3141 if (status
& ME4600_AO_STATUS_BIT_FSM
) { // State machine is working. Waiting for start finish.
3142 instance
->status
= ao_status_stream_run
;
3144 // Signal end of this step
3146 } else { // State machine is not working.
3147 if (!(status
& ME4600_AO_STATUS_BIT_EF
)) { // FIFO is empty. Procedure has started and finish already!
3148 instance
->status
= ao_status_stream_end
;
3159 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3160 PDEBUG("Timeout reached.\n");
3161 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3162 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3163 ctrl
= inl(instance
->ctrl_reg
);
3165 ME4600_AO_CTRL_BIT_STOP
|
3166 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
3167 ME4600_AO_CTRL_BIT_RESET_IRQ
;
3169 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3170 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3171 outl(ctrl
, instance
->ctrl_reg
);
3172 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3174 instance
->ctrl_reg
- instance
->reg_base
,
3176 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3178 spin_lock(instance
->preload_reg_lock
);
3179 //Remove from synchronous start. Block triggering from this output.
3180 synch
= inl(instance
->preload_reg
);
3182 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) <<
3184 outl(synch
, instance
->preload_reg
);
3185 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3187 instance
->preload_reg
- instance
->reg_base
,
3189 spin_unlock(instance
->preload_reg_lock
);
3191 instance
->status
= ao_status_stream_end
;
3200 case ao_status_stream_run
:
3201 if (!instance
->fifo
) {
3203 ("Streaming on single device! This feature is not implemented in this version!\n");
3204 instance
->status
= ao_status_stream_error
;
3210 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working. This is an error.
3212 if (!(status
& ME4600_AO_STATUS_BIT_EF
)) { // FIFO is empty.
3213 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3214 if (instance
->stop_data_count
&& (instance
->stop_data_count
<= instance
->data_count
)) { //Finishing work. Requed data shown.
3216 ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3218 ao_status_stream_end
;
3221 ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3223 ao_status_stream_buffer_error
;
3225 } else { // Software buffer is empty.
3227 ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3228 instance
->status
= ao_status_stream_end
;
3230 } else { // There are still datas in FIFO.
3231 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3233 ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3234 } else { // Software buffer is empty.
3236 ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3238 instance
->status
= ao_status_stream_fifo_error
;
3242 // Signal the failure.
3250 case ao_status_stream_end_wait
:
3251 if (!instance
->fifo
) {
3253 ("Streaming on single device! This feature is not implemented in this version!\n");
3254 instance
->status
= ao_status_stream_error
;
3260 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working. Waiting for stop finish.
3261 instance
->status
= ao_status_stream_end
;
3264 // State machine is working.
3269 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3271 instance
->status
= ao_status_stream_error
;
3278 if (signaling
) { //Signal it.
3279 wake_up_interruptible_all(&instance
->wait_queue
);
3282 if (instance
->ao_control_task_flag
&& reschedule
) { // Reschedule task
3283 queue_delayed_work(instance
->me4600_workqueue
,
3284 &instance
->ao_control_task
, 1);
3286 PINFO("<%s> Ending control task.\n", __func__
);
3291 /// @note SPECIAL BUILD FOR BOSCH
3292 /// @author Guenter Gebhardt
3293 static int me4600_ao_io_reset_subdevice(me_subdevice_t
*subdevice
,
3294 struct file
*filep
, int flags
)
3296 me4600_ao_subdevice_t
*instance
;
3297 int err
= ME_ERRNO_SUCCESS
;
3299 unsigned long status
;
3301 PDEBUG("executed.\n");
3303 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3305 ME_SUBDEVICE_ENTER
spin_lock_irqsave(&instance
->subdevice_lock
, status
);
3306 spin_lock(instance
->preload_reg_lock
);
3307 tmp
= inl(instance
->preload_reg
);
3308 tmp
&= ~(0x10001 << instance
->ao_idx
);
3309 outl(tmp
, instance
->preload_reg
);
3310 *instance
->preload_flags
&= ~(0x1 << instance
->ao_idx
);
3311 spin_unlock(instance
->preload_reg_lock
);
3313 tmp
= inl(instance
->ctrl_reg
);
3314 tmp
|= ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3315 outl(tmp
, instance
->ctrl_reg
);
3317 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
3319 outl(ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
,
3320 instance
->ctrl_reg
);
3322 outl(0x8000, instance
->single_reg
);
3324 instance
->single_value
= 0x8000;
3325 instance
->circ_buf
.head
= 0;
3326 instance
->circ_buf
.tail
= 0;
3328 spin_unlock_irqrestore(&instance
->subdevice_lock
, status
);
3335 static int me4600_ao_io_single_config(me_subdevice_t
*subdevice
,
3341 int trig_type
, int trig_edge
, int flags
)
3343 me4600_ao_subdevice_t
*instance
;
3344 int err
= ME_ERRNO_SUCCESS
;
3346 unsigned long cpu_flags
;
3348 PDEBUG("executed.\n");
3350 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3353 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3355 if (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) {
3356 PERROR("Subdevice is busy.\n");
3357 err
= ME_ERRNO_SUBDEVICE_BUSY
;
3362 if (single_config
== 0) {
3363 if (ref
== ME_REF_AO_GROUND
) {
3364 if (trig_chan
== ME_TRIG_CHAN_DEFAULT
) {
3365 if (trig_type
== ME_TRIG_TYPE_SW
) {
3366 tmp
= inl(instance
->ctrl_reg
);
3368 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3369 outl(tmp
, instance
->ctrl_reg
);
3371 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3372 outl(tmp
, instance
->ctrl_reg
);
3374 spin_lock(instance
->
3377 inl(instance
->preload_reg
);
3379 ~(0x10001 << instance
->
3382 instance
->preload_reg
);
3383 *instance
->preload_flags
&=
3384 ~(0x1 << instance
->ao_idx
);
3385 spin_unlock(instance
->
3387 } else if (trig_type
==
3388 ME_TRIG_TYPE_EXT_DIGITAL
) {
3390 ME_TRIG_EDGE_RISING
) {
3395 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3400 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3402 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3406 } else if (trig_edge
==
3407 ME_TRIG_EDGE_FALLING
)
3413 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3418 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3420 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
3422 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3426 } else if (trig_edge
==
3432 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3437 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3439 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
3441 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
3443 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3449 ("Invalid trigger edge.\n");
3451 ME_ERRNO_INVALID_TRIG_EDGE
;
3455 spin_lock(instance
->
3459 inl(instance
->preload_reg
);
3461 ~(0x10001 << instance
->
3463 tmp
|= 0x1 << instance
->ao_idx
;
3465 instance
->preload_reg
);
3466 *instance
->preload_flags
&=
3467 ~(0x1 << instance
->ao_idx
);
3468 spin_unlock(instance
->
3472 ("Invalid trigger type.\n");
3474 ME_ERRNO_INVALID_TRIG_TYPE
;
3477 } else if (trig_chan
==
3478 ME_TRIG_CHAN_SYNCHRONOUS
) {
3479 if (trig_type
== ME_TRIG_TYPE_SW
) {
3480 tmp
= inl(instance
->ctrl_reg
);
3482 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3483 outl(tmp
, instance
->ctrl_reg
);
3485 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3486 outl(tmp
, instance
->ctrl_reg
);
3488 spin_lock(instance
->
3491 inl(instance
->preload_reg
);
3493 ~(0x10001 << instance
->
3495 tmp
|= 0x1 << instance
->ao_idx
;
3497 instance
->preload_reg
);
3498 *instance
->preload_flags
|=
3499 0x1 << instance
->ao_idx
;
3500 spin_unlock(instance
->
3502 } else if (trig_type
==
3503 ME_TRIG_TYPE_EXT_DIGITAL
) {
3505 ME_TRIG_EDGE_RISING
) {
3510 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3515 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3519 } else if (trig_edge
==
3520 ME_TRIG_EDGE_FALLING
)
3526 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3531 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3533 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3537 } else if (trig_edge
==
3543 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3548 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3550 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
3552 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3558 ("Invalid trigger edge.\n");
3560 ME_ERRNO_INVALID_TRIG_EDGE
;
3564 spin_lock(instance
->
3568 inl(instance
->preload_reg
);
3570 0x10001 << instance
->ao_idx
;
3572 instance
->preload_reg
);
3573 *instance
->preload_flags
&=
3574 ~(0x1 << instance
->ao_idx
);
3575 spin_unlock(instance
->
3579 ("Invalid trigger type.\n");
3581 ME_ERRNO_INVALID_TRIG_TYPE
;
3586 ("Invalid trigger channel specified.\n");
3587 err
= ME_ERRNO_INVALID_REF
;
3591 PERROR("Invalid analog reference specified.\n");
3592 err
= ME_ERRNO_INVALID_REF
;
3596 PERROR("Invalid single config specified.\n");
3597 err
= ME_ERRNO_INVALID_SINGLE_CONFIG
;
3601 PERROR("Invalid channel number specified.\n");
3602 err
= ME_ERRNO_INVALID_CHANNEL
;
3608 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3615 static int me4600_ao_io_single_read(me_subdevice_t
*subdevice
,
3618 int *value
, int time_out
, int flags
)
3620 me4600_ao_subdevice_t
*instance
;
3621 int err
= ME_ERRNO_SUCCESS
;
3623 unsigned long cpu_flags
;
3625 PDEBUG("executed.\n");
3627 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3630 PERROR("Invalid channel number specified.\n");
3631 return ME_ERRNO_INVALID_CHANNEL
;
3635 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3636 tmp
= inl(instance
->ctrl_reg
);
3639 PERROR("Not in single mode.\n");
3640 err
= ME_ERRNO_PREVIOUS_CONFIG
;
3642 *value
= instance
->single_value
;
3645 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3652 static int me4600_ao_io_single_write(me_subdevice_t
*subdevice
,
3655 int value
, int time_out
, int flags
)
3657 me4600_ao_subdevice_t
*instance
;
3658 int err
= ME_ERRNO_SUCCESS
;
3659 unsigned long mask
= 0;
3661 unsigned long cpu_flags
;
3663 wait_queue_head_t queue
;
3665 unsigned long delay
= 0;
3667 PDEBUG("executed.\n");
3669 init_waitqueue_head(&queue
);
3671 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3674 PERROR("Invalid channel number specified.\n");
3675 return ME_ERRNO_INVALID_CHANNEL
;
3679 PERROR("Invalid timeout specified.\n");
3680 return ME_ERRNO_INVALID_TIMEOUT
;
3684 delay
= (time_out
* HZ
) / 1000;
3691 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3693 tmp
= inl(instance
->ctrl_reg
);
3696 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3697 PERROR("Not in single mode.\n");
3698 err
= ME_ERRNO_PREVIOUS_CONFIG
;
3702 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) {
3703 outl(value
, instance
->single_reg
);
3704 instance
->single_value
= value
;
3705 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3707 if (!(flags
& ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
3710 while (inl(instance
->status_reg
) &
3711 ME4600_AO_STATUS_BIT_FSM
) {
3712 interruptible_sleep_on_timeout(&queue
, 1);
3714 if (signal_pending(current
)) {
3716 ("Wait on external trigger interrupted by signal.\n");
3717 err
= ME_ERRNO_SIGNAL
;
3721 if (delay
&& ((jiffies
- j
) > delay
)) {
3722 PERROR("Timeout reached.\n");
3723 err
= ME_ERRNO_TIMEOUT
;
3728 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
))
3729 == (0x10001 << instance
->ao_idx
)) {
3730 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) {
3731 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3732 outl(tmp
, instance
->ctrl_reg
);
3733 outl(value
, instance
->single_reg
);
3734 instance
->single_value
= value
;
3735 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3738 if (!(flags
& ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
3741 while (inl(instance
->status_reg
) &
3742 ME4600_AO_STATUS_BIT_FSM
) {
3743 interruptible_sleep_on_timeout(&queue
,
3746 if (signal_pending(current
)) {
3748 ("Wait on external trigger interrupted by signal.\n");
3749 err
= ME_ERRNO_SIGNAL
;
3753 if (delay
&& ((jiffies
- j
) > delay
)) {
3754 PERROR("Timeout reached.\n");
3755 err
= ME_ERRNO_TIMEOUT
;
3761 outl(value
, instance
->single_reg
);
3762 instance
->single_value
= value
;
3763 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3766 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
))
3767 == (0x1 << instance
->ao_idx
)) {
3768 outl(value
, instance
->single_reg
);
3769 instance
->single_value
= value
;
3771 PDEBUG("Synchronous SW, flags = 0x%X.\n", flags
);
3773 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) {
3774 PDEBUG("Trigger synchronous SW.\n");
3775 spin_lock(instance
->preload_reg_lock
);
3776 tmp
= inl(instance
->preload_reg
);
3778 for (i
= 0; i
< ME4600_AO_MAX_SUBDEVICES
; i
++) {
3779 if ((*instance
->preload_flags
& (0x1 << i
))) {
3780 if ((tmp
& (0x10001 << i
)) ==
3789 outl(tmp
, instance
->preload_reg
);
3790 spin_unlock(instance
->preload_reg_lock
);
3793 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3795 outl(value
, instance
->single_reg
);
3796 instance
->single_value
= value
;
3797 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3807 static int me4600_ao_io_stream_config(me_subdevice_t
*subdevice
,
3809 meIOStreamConfig_t
*config_list
,
3811 meIOStreamTrigger_t
*trigger
,
3812 int fifo_irq_threshold
, int flags
)
3814 me4600_ao_subdevice_t
*instance
;
3815 int err
= ME_ERRNO_SUCCESS
;
3818 unsigned long cpu_flags
;
3819 uint64_t conv_ticks
;
3820 unsigned int conv_start_ticks_low
= trigger
->iConvStartTicksLow
;
3821 unsigned int conv_start_ticks_high
= trigger
->iConvStartTicksHigh
;
3823 PDEBUG("executed.\n");
3825 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3828 (uint64_t) conv_start_ticks_low
+
3829 ((uint64_t) conv_start_ticks_high
<< 32);
3831 if (!instance
->fifo
) {
3832 PERROR("Not a streaming ao.\n");
3833 return ME_ERRNO_NOT_SUPPORTED
;
3837 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3839 if ((inl(instance
->status_reg
)) & ME4600_AO_STATUS_BIT_FSM
) {
3840 PERROR("Subdevice is busy.\n");
3841 err
= ME_ERRNO_SUBDEVICE_BUSY
;
3845 ctrl
= inl(instance
->ctrl_reg
);
3846 ctrl
|= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3847 outl(ctrl
, instance
->ctrl_reg
);
3848 ctrl
= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3849 outl(ctrl
, instance
->ctrl_reg
);
3852 PERROR("Invalid stream configuration list count specified.\n");
3853 err
= ME_ERRNO_INVALID_CONFIG_LIST_COUNT
;
3857 if (config_list
[0].iChannel
!= 0) {
3858 PERROR("Invalid channel number specified.\n");
3859 err
= ME_ERRNO_INVALID_CHANNEL
;
3863 if (config_list
[0].iStreamConfig
!= 0) {
3864 PERROR("Invalid stream config specified.\n");
3865 err
= ME_ERRNO_INVALID_STREAM_CONFIG
;
3869 if (config_list
[0].iRef
!= ME_REF_AO_GROUND
) {
3870 PERROR("Invalid analog reference.\n");
3871 err
= ME_ERRNO_INVALID_REF
;
3875 if ((trigger
->iAcqStartTicksLow
!= 0)
3876 || (trigger
->iAcqStartTicksHigh
!= 0)) {
3878 ("Invalid acquisition start trigger argument specified.\n");
3879 err
= ME_ERRNO_INVALID_ACQ_START_ARG
;
3883 switch (trigger
->iAcqStartTrigType
) {
3885 case ME_TRIG_TYPE_SW
:
3888 case ME_TRIG_TYPE_EXT_DIGITAL
:
3889 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3891 switch (trigger
->iAcqStartTrigEdge
) {
3893 case ME_TRIG_EDGE_RISING
:
3896 case ME_TRIG_EDGE_FALLING
:
3897 ctrl
|= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3901 case ME_TRIG_EDGE_ANY
:
3903 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3904 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3910 ("Invalid acquisition start trigger edge specified.\n");
3912 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE
;
3922 PERROR("Invalid acquisition start trigger type specified.\n");
3924 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE
;
3931 switch (trigger
->iScanStartTrigType
) {
3933 case ME_TRIG_TYPE_FOLLOW
:
3937 PERROR("Invalid scan start trigger type specified.\n");
3939 err
= ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE
;
3946 switch (trigger
->iConvStartTrigType
) {
3948 case ME_TRIG_TYPE_TIMER
:
3949 if ((conv_ticks
< ME4600_AO_MIN_CHAN_TICKS
)
3950 || (conv_ticks
> ME4600_AO_MAX_CHAN_TICKS
)) {
3952 ("Invalid conv start trigger argument specified.\n");
3953 err
= ME_ERRNO_INVALID_CONV_START_ARG
;
3960 PERROR("Invalid conv start trigger type specified.\n");
3962 err
= ME_ERRNO_INVALID_CONV_START_TRIG_TYPE
;
3969 /* Preset to hardware wraparound mode */
3970 instance
->flags
&= ~(ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
);
3972 switch (trigger
->iScanStopTrigType
) {
3974 case ME_TRIG_TYPE_NONE
:
3975 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
3976 /* Set flags to indicate usage of software mode. */
3977 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_INF
;
3978 instance
->wrap_count
= 0;
3979 instance
->wrap_remaining
= 0;
3984 case ME_TRIG_TYPE_COUNT
:
3985 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
3986 if (trigger
->iScanStopCount
<= 0) {
3987 PERROR("Invalid scan stop count specified.\n");
3988 err
= ME_ERRNO_INVALID_SCAN_STOP_ARG
;
3992 /* Set flags to indicate usage of software mode. */
3993 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
;
3994 instance
->wrap_count
= trigger
->iScanStopCount
;
3995 instance
->wrap_remaining
= trigger
->iScanStopCount
;
3997 PERROR("Invalid scan stop trigger type specified.\n");
3998 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4005 PERROR("Invalid scan stop trigger type specified.\n");
4007 err
= ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE
;
4014 switch (trigger
->iAcqStopTrigType
) {
4016 case ME_TRIG_TYPE_NONE
:
4019 case ME_TRIG_TYPE_COUNT
:
4020 if (trigger
->iScanStopTrigType
!= ME_TRIG_TYPE_NONE
) {
4021 PERROR("Invalid acq stop trigger type specified.\n");
4022 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4026 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
4027 if (trigger
->iAcqStopCount
<= 0) {
4028 PERROR("Invalid acq stop count specified.\n");
4029 err
= ME_ERRNO_INVALID_ACQ_STOP_ARG
;
4033 /* Set flags to indicate usage of software mode. */
4034 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
;
4035 instance
->wrap_count
= trigger
->iAcqStopCount
;
4036 instance
->wrap_remaining
= trigger
->iAcqStopCount
;
4038 PERROR("Invalid acp stop trigger type specified.\n");
4039 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4046 PERROR("Invalid acq stop trigger type specified.\n");
4047 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4052 switch (trigger
->iAcqStartTrigChan
) {
4054 case ME_TRIG_CHAN_DEFAULT
:
4055 spin_lock(instance
->preload_reg_lock
);
4056 tmp
= inl(instance
->preload_reg
);
4057 tmp
&= ~(0x10001 << instance
->ao_idx
);
4058 outl(tmp
, instance
->preload_reg
);
4059 spin_unlock(instance
->preload_reg_lock
);
4063 case ME_TRIG_CHAN_SYNCHRONOUS
:
4064 if (trigger
->iAcqStartTrigType
== ME_TRIG_TYPE_SW
) {
4065 spin_lock(instance
->preload_reg_lock
);
4066 tmp
= inl(instance
->preload_reg
);
4067 tmp
&= ~(0x10001 << instance
->ao_idx
);
4068 outl(tmp
, instance
->preload_reg
);
4069 tmp
|= 0x1 << instance
->ao_idx
;
4070 outl(tmp
, instance
->preload_reg
);
4071 spin_unlock(instance
->preload_reg_lock
);
4073 ctrl
&= ~(ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
4074 spin_lock(instance
->preload_reg_lock
);
4075 tmp
= inl(instance
->preload_reg
);
4076 tmp
&= ~(0x10001 << instance
->ao_idx
);
4077 outl(tmp
, instance
->preload_reg
);
4078 tmp
|= 0x10000 << instance
->ao_idx
;
4079 outl(tmp
, instance
->preload_reg
);
4080 spin_unlock(instance
->preload_reg_lock
);
4086 PERROR("Invalid acq start trigger channel specified.\n");
4087 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN
;
4093 outl(conv_ticks
- 2, instance
->timer_reg
);
4095 if (flags
& ME_IO_STREAM_CONFIG_BIT_PATTERN
) {
4096 if (instance
->ao_idx
== 3) {
4097 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_DO
;
4099 err
= ME_ERRNO_INVALID_FLAGS
;
4103 if (instance
->ao_idx
== 3) {
4104 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_DO
;
4108 /* Set hardware mode. */
4109 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
4110 ctrl
|= ME4600_AO_CTRL_BIT_MODE_0
;
4112 ctrl
|= ME4600_AO_CTRL_BIT_MODE_1
;
4115 PDEBUG("Preload word = 0x%X.\n", inl(instance
->preload_reg
));
4117 PDEBUG("Ctrl word = 0x%lX.\n", ctrl
);
4118 outl(ctrl
, instance
->ctrl_reg
); // Write the control word
4122 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4129 static int me4600_ao_io_stream_new_values(me_subdevice_t
*subdevice
,
4131 int time_out
, int *count
, int flags
)
4133 me4600_ao_subdevice_t
*instance
;
4134 int err
= ME_ERRNO_SUCCESS
;
4138 PDEBUG("executed.\n");
4140 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4142 if (!instance
->fifo
) {
4143 PERROR("Not a streaming ao.\n");
4144 return ME_ERRNO_NOT_SUPPORTED
;
4148 PERROR("Invalid time_out specified.\n");
4149 return ME_ERRNO_INVALID_TIMEOUT
;
4153 t
= (time_out
* HZ
) / 1000;
4165 wait_event_interruptible_timeout(instance
->wait_queue
,
4167 (&instance
->circ_buf
))
4168 || !(inl(instance
->status_reg
)
4170 ME4600_AO_STATUS_BIT_FSM
)),
4173 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
4174 PERROR("AO subdevice is not running.\n");
4175 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
4176 } else if (signal_pending(current
)) {
4177 PERROR("Wait on values interrupted from signal.\n");
4178 err
= ME_ERRNO_SIGNAL
;
4179 } else if ((jiffies
- j
) >= t
) {
4180 PERROR("Wait on values timed out.\n");
4181 err
= ME_ERRNO_TIMEOUT
;
4183 *count
= me_circ_buf_space(&instance
->circ_buf
);
4186 wait_event_interruptible(instance
->wait_queue
,
4188 (&instance
->circ_buf
))
4189 || !(inl(instance
->status_reg
) &
4190 ME4600_AO_STATUS_BIT_FSM
)));
4192 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
4193 PERROR("AO subdevice is not running.\n");
4194 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
4195 } else if (signal_pending(current
)) {
4196 PERROR("Wait on values interrupted from signal.\n");
4197 err
= ME_ERRNO_SIGNAL
;
4199 *count
= me_circ_buf_space(&instance
->circ_buf
);
4208 static void stop_immediately(me4600_ao_subdevice_t
*instance
)
4210 unsigned long cpu_flags
;
4213 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4214 tmp
= inl(instance
->ctrl_reg
);
4215 tmp
|= ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4216 outl(tmp
, instance
->ctrl_reg
);
4218 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
4220 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4223 static int me4600_ao_io_stream_start(me_subdevice_t
*subdevice
,
4225 int start_mode
, int time_out
, int flags
)
4227 me4600_ao_subdevice_t
*instance
;
4228 int err
= ME_ERRNO_SUCCESS
;
4229 unsigned long cpu_flags
= 0;
4232 unsigned long delay
= 0;
4233 wait_queue_head_t queue
;
4235 PDEBUG("executed.\n");
4237 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4239 init_waitqueue_head(&queue
);
4242 PERROR("Invalid timeout specified.\n");
4243 return ME_ERRNO_INVALID_TIMEOUT
;
4247 delay
= (time_out
* HZ
) / 1000;
4253 if (!instance
->fifo
) {
4254 PERROR("Not a streaming ao.\n");
4255 return ME_ERRNO_NOT_SUPPORTED
;
4259 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4261 tmp
= inl(instance
->ctrl_reg
);
4263 switch (tmp
& (ME4600_AO_CTRL_MASK_MODE
)) {
4265 case 0: // Single mode
4266 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4267 PERROR("Subdevice is configured in single mode.\n");
4268 err
= ME_ERRNO_PREVIOUS_CONFIG
;
4271 case 1: // Wraparound mode
4272 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) { // Normal wraparound with external trigger
4274 if ((inl(instance
->status_reg
) &
4275 ME4600_AO_STATUS_BIT_FSM
)) {
4276 spin_unlock_irqrestore(&instance
->
4279 PERROR("Conversion is already running.\n");
4280 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4285 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4286 ME4600_AO_CTRL_BIT_STOP
|
4287 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4289 outl(tmp
, instance
->ctrl_reg
);
4290 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4293 if (start_mode
== ME_START_MODE_BLOCKING
) {
4294 init_waitqueue_head(&queue
);
4300 (inl(instance
->status_reg
) &
4301 ME4600_AO_STATUS_BIT_FSM
)) {
4302 interruptible_sleep_on_timeout
4305 if (signal_pending(current
)) {
4307 ("Wait on start of state machine interrupted.\n");
4310 err
= ME_ERRNO_SIGNAL
;
4314 if (((jiffies
- ref
) >= delay
)) {
4316 ("Timeout reached.\n");
4319 err
= ME_ERRNO_TIMEOUT
;
4325 (inl(instance
->status_reg
) &
4326 ME4600_AO_STATUS_BIT_FSM
)) {
4327 interruptible_sleep_on_timeout
4330 if (signal_pending(current
)) {
4332 ("Wait on start of state machine interrupted.\n");
4335 err
= ME_ERRNO_SIGNAL
;
4340 } else if (start_mode
== ME_START_MODE_NONBLOCKING
) {
4342 PERROR("Invalid start mode specified.\n");
4343 err
= ME_ERRNO_INVALID_START_MODE
;
4346 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x10000 << instance
->ao_idx
)) { // Synchronous with external trigger
4348 if ((inl(instance
->status_reg
) &
4349 ME4600_AO_STATUS_BIT_FSM
)) {
4350 spin_unlock_irqrestore(&instance
->
4353 PERROR("Conversion is already running.\n");
4354 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4358 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4359 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
4361 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4362 ME4600_AO_CTRL_BIT_STOP
|
4363 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4364 outl(tmp
, instance
->ctrl_reg
);
4365 spin_unlock_irqrestore(&instance
->
4369 if (start_mode
== ME_START_MODE_BLOCKING
) {
4370 init_waitqueue_head(&queue
);
4379 ME4600_AO_STATUS_BIT_FSM
))
4381 interruptible_sleep_on_timeout
4387 ("Wait on start of state machine interrupted.\n");
4395 if (((jiffies
- ref
) >=
4398 ("Timeout reached.\n");
4411 ME4600_AO_STATUS_BIT_FSM
))
4413 interruptible_sleep_on_timeout
4419 ("Wait on start of state machine interrupted.\n");
4428 } else if (start_mode
==
4429 ME_START_MODE_NONBLOCKING
) {
4432 ("Invalid start mode specified.\n");
4433 err
= ME_ERRNO_INVALID_START_MODE
;
4438 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4439 ME4600_AO_CTRL_BIT_STOP
|
4440 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4441 outl(tmp
, instance
->ctrl_reg
);
4442 spin_unlock_irqrestore(&instance
->
4446 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x1 << instance
->ao_idx
)) { // Synchronous wraparound with sw trigger
4448 if ((inl(instance
->status_reg
) &
4449 ME4600_AO_STATUS_BIT_FSM
)) {
4450 spin_unlock_irqrestore(&instance
->
4453 PERROR("Conversion is already running.\n");
4454 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4459 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4460 ME4600_AO_CTRL_BIT_STOP
|
4461 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4463 outl(tmp
, instance
->ctrl_reg
);
4465 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4466 outl(0x8000, instance
->single_reg
);
4467 instance
->single_value
= 0x8000;
4470 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4472 } else { // Software start
4474 if ((inl(instance
->status_reg
) &
4475 ME4600_AO_STATUS_BIT_FSM
)) {
4476 spin_unlock_irqrestore(&instance
->
4479 PERROR("Conversion is already running.\n");
4480 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4485 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4486 ME4600_AO_CTRL_BIT_STOP
|
4487 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4489 outl(tmp
, instance
->ctrl_reg
);
4491 outl(0x8000, instance
->single_reg
);
4492 instance
->single_value
= 0x8000;
4494 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4500 case 2: // Continuous mode
4501 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) { // Externally triggered
4503 if ((inl(instance
->status_reg
) &
4504 ME4600_AO_STATUS_BIT_FSM
)) {
4505 spin_unlock_irqrestore(&instance
->
4508 PERROR("Conversion is already running.\n");
4509 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4514 ~(ME4600_AO_CTRL_BIT_STOP
|
4515 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4516 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4517 outl(tmp
, instance
->ctrl_reg
);
4518 instance
->wrap_remaining
= instance
->wrap_count
;
4519 instance
->circ_buf
.tail
= 0;
4520 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4523 if (start_mode
== ME_START_MODE_BLOCKING
) {
4524 init_waitqueue_head(&queue
);
4530 (inl(instance
->status_reg
) &
4531 ME4600_AO_STATUS_BIT_FSM
)) {
4532 interruptible_sleep_on_timeout
4535 if (signal_pending(current
)) {
4537 ("Wait on start of state machine interrupted.\n");
4540 err
= ME_ERRNO_SIGNAL
;
4544 if (((jiffies
- ref
) >= delay
)) {
4546 ("Timeout reached.\n");
4549 err
= ME_ERRNO_TIMEOUT
;
4555 (inl(instance
->status_reg
) &
4556 ME4600_AO_STATUS_BIT_FSM
)) {
4557 interruptible_sleep_on_timeout
4560 if (signal_pending(current
)) {
4562 ("Wait on start of state machine interrupted.\n");
4565 err
= ME_ERRNO_SIGNAL
;
4570 } else if (start_mode
== ME_START_MODE_NONBLOCKING
) {
4573 PERROR("Invalid start mode specified.\n");
4574 stop_immediately(instance
);
4575 err
= ME_ERRNO_INVALID_START_MODE
;
4578 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x10000 << instance
->ao_idx
)) { // Synchronous with external trigger
4580 if ((inl(instance
->status_reg
) &
4581 ME4600_AO_STATUS_BIT_FSM
)) {
4582 spin_unlock_irqrestore(&instance
->
4585 PERROR("Conversion is already running.\n");
4586 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4590 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4592 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
|
4593 ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4595 ~(ME4600_AO_CTRL_BIT_STOP
|
4596 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4597 outl(tmp
, instance
->ctrl_reg
);
4598 instance
->wrap_remaining
= instance
->wrap_count
;
4599 instance
->circ_buf
.tail
= 0;
4601 spin_unlock_irqrestore(&instance
->
4605 if (start_mode
== ME_START_MODE_BLOCKING
) {
4606 init_waitqueue_head(&queue
);
4615 ME4600_AO_STATUS_BIT_FSM
))
4617 interruptible_sleep_on_timeout
4623 ("Wait on start of state machine interrupted.\n");
4631 if (((jiffies
- ref
) >=
4634 ("Timeout reached.\n");
4647 ME4600_AO_STATUS_BIT_FSM
))
4649 interruptible_sleep_on_timeout
4655 ("Wait on start of state machine interrupted.\n");
4664 } else if (start_mode
==
4665 ME_START_MODE_NONBLOCKING
) {
4668 ("Invalid start mode specified.\n");
4669 stop_immediately(instance
);
4670 err
= ME_ERRNO_INVALID_START_MODE
;
4674 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4676 ~(ME4600_AO_CTRL_BIT_STOP
|
4677 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4678 outl(tmp
, instance
->ctrl_reg
);
4679 instance
->wrap_remaining
= instance
->wrap_count
;
4680 instance
->circ_buf
.tail
= 0;
4681 spin_unlock_irqrestore(&instance
->
4685 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x1 << instance
->ao_idx
)) { // Synchronous wraparound with sw trigger
4687 if ((inl(instance
->status_reg
) &
4688 ME4600_AO_STATUS_BIT_FSM
)) {
4689 spin_unlock_irqrestore(&instance
->
4692 PERROR("Conversion is already running.\n");
4693 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4698 ~(ME4600_AO_CTRL_BIT_STOP
|
4699 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4700 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4701 instance
->wrap_remaining
= instance
->wrap_count
;
4702 instance
->circ_buf
.tail
= 0;
4703 PDEBUG("CTRL Reg = 0x%X.\n", inl(instance
->ctrl_reg
));
4704 outl(tmp
, instance
->ctrl_reg
);
4706 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4707 outl(0x8000, instance
->single_reg
);
4708 instance
->single_value
= 0x8000;
4711 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4713 } else { // Software start
4715 if ((inl(instance
->status_reg
) &
4716 ME4600_AO_STATUS_BIT_FSM
)) {
4717 spin_unlock_irqrestore(&instance
->
4720 PERROR("Conversion is already running.\n");
4721 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4726 ~(ME4600_AO_CTRL_BIT_STOP
|
4727 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4729 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4730 outl(tmp
, instance
->ctrl_reg
);
4731 outl(0x8000, instance
->single_reg
);
4732 instance
->single_value
= 0x8000;
4733 instance
->wrap_remaining
= instance
->wrap_count
;
4734 instance
->circ_buf
.tail
= 0;
4735 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4742 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4743 PERROR("Invalid mode configured.\n");
4744 err
= ME_ERRNO_INTERNAL
;
4755 static int me4600_ao_io_stream_status(me_subdevice_t
*subdevice
,
4758 int *status
, int *values
, int flags
)
4760 me4600_ao_subdevice_t
*instance
;
4761 int err
= ME_ERRNO_SUCCESS
;
4762 wait_queue_head_t queue
;
4764 PDEBUG("executed.\n");
4766 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4768 init_waitqueue_head(&queue
);
4770 if (!instance
->fifo
) {
4771 PERROR("Not a streaming ao.\n");
4772 return ME_ERRNO_NOT_SUPPORTED
;
4777 if (wait
== ME_WAIT_NONE
) {
4779 (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ?
4780 ME_STATUS_BUSY
: ME_STATUS_IDLE
;
4781 *values
= me_circ_buf_space(&instance
->circ_buf
);
4782 } else if (wait
== ME_WAIT_IDLE
) {
4783 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) {
4784 interruptible_sleep_on_timeout(&queue
, 1);
4786 if (instance
->flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
4787 PERROR("Output stream was interrupted.\n");
4788 *status
= ME_STATUS_ERROR
;
4789 err
= ME_ERRNO_SUCCESS
;
4793 if (signal_pending(current
)) {
4795 ("Wait on state machine interrupted by signal.\n");
4796 *status
= ME_STATUS_INVALID
;
4797 err
= ME_ERRNO_SIGNAL
;
4802 *status
= ME_STATUS_IDLE
;
4804 *values
= me_circ_buf_space(&instance
->circ_buf
);
4806 PERROR("Invalid wait argument specified.\n");
4807 *status
= ME_STATUS_INVALID
;
4808 err
= ME_ERRNO_INVALID_WAIT
;
4819 static int me4600_ao_io_stream_stop(me_subdevice_t
*subdevice
,
4821 int stop_mode
, int flags
)
4823 int err
= ME_ERRNO_SUCCESS
;
4824 me4600_ao_subdevice_t
*instance
;
4825 unsigned long cpu_flags
;
4828 PDEBUG("executed.\n");
4830 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4832 if (!instance
->fifo
) {
4833 PERROR("Not a streaming ao.\n");
4834 return ME_ERRNO_NOT_SUPPORTED
;
4839 if (stop_mode
== ME_STOP_MODE_IMMEDIATE
) {
4840 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4841 tmp
= inl(instance
->ctrl_reg
);
4843 ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4844 outl(tmp
, instance
->ctrl_reg
);
4846 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
4848 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4849 } else if (stop_mode
== ME_STOP_MODE_LAST_VALUE
) {
4850 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4851 tmp
= inl(instance
->ctrl_reg
);
4852 tmp
|= ME4600_AO_CTRL_BIT_STOP
;
4853 outl(tmp
, instance
->ctrl_reg
);
4854 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4856 PERROR("Invalid stop mode specified.\n");
4857 err
= ME_ERRNO_INVALID_STOP_MODE
;
4868 static int me4600_ao_io_stream_write(me_subdevice_t
*subdevice
,
4871 int *values
, int *count
, int flags
)
4873 int err
= ME_ERRNO_SUCCESS
;
4874 me4600_ao_subdevice_t
*instance
;
4882 unsigned long cpu_flags
= 0;
4884 PDEBUG("executed.\n");
4886 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4888 if (!instance
->fifo
) {
4889 PERROR("Not a streaming ao.\n");
4890 return ME_ERRNO_NOT_SUPPORTED
;
4896 PERROR("Invalid count of values specified.\n");
4897 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
4901 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4903 tmp
= inl(instance
->ctrl_reg
);
4905 switch (tmp
& 0x3) {
4907 case 1: // Wraparound mode
4908 if (instance
->bosch_fw
) { // Bosch firmware
4909 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4914 ("Invalid count of values specified. 7 expected.\n");
4915 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
4919 for (i
= 0; i
< 7; i
++) {
4920 if (get_user(value
, values
)) {
4922 ("Can't copy value from user space.\n");
4923 err
= ME_ERRNO_INTERNAL
;
4928 /* Maximum voltage */
4931 inl(instance
->reg_base
+
4933 outl(value
, instance
->reg_base
+ 0xD4);
4934 } else if (i
== 1) {
4935 /* Minimum voltage */
4938 inl(instance
->reg_base
+
4940 outl(value
, instance
->reg_base
+ 0xD4);
4941 } else if (i
== 2) {
4945 inl(instance
->reg_base
+
4947 outl(value
, instance
->reg_base
+ 0xD8);
4948 } else if (i
== 3) {
4952 inl(instance
->reg_base
+
4954 outl(value
, instance
->reg_base
+ 0xD8);
4955 } else if (i
== 4) {
4957 outl(value
, instance
->reg_base
+ 0xDC);
4958 } else if (i
== 5) {
4961 value
= inl(instance
->ctrl_reg
);
4963 outl(value
, instance
->ctrl_reg
);
4965 value
= inl(instance
->ctrl_reg
);
4967 outl(value
, instance
->ctrl_reg
);
4969 } else if (i
== 6) {
4970 /* Timer for positive ramp */
4971 outl(value
, instance
->reg_base
+ 0xE0);
4976 } else { // Normal firmware
4977 PDEBUG("Write for wraparound mode.\n");
4979 if (inl(instance
->status_reg
) &
4980 ME4600_AO_STATUS_BIT_FSM
) {
4981 spin_unlock_irqrestore(&instance
->
4985 ("There is already a conversion running.\n");
4986 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4990 tmp
|= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4991 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
4992 outl(tmp
, instance
->ctrl_reg
);
4993 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
4995 if ((*count
> ME4600_AO_FIFO_COUNT
) ||
4997 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
4998 ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
)) {
5000 ~(ME4600_AO_CTRL_BIT_MODE_0
|
5001 ME4600_AO_CTRL_BIT_MODE_1
);
5002 tmp
|= ME4600_AO_CTRL_BIT_MODE_1
;
5005 outl(tmp
, instance
->ctrl_reg
);
5006 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5009 if ((*count
<= ME4600_AO_FIFO_COUNT
) &&
5011 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5012 ME4600_AO_FLAGS_SW_WRAP_MODE_INF
)) {
5013 for (i
= 0; i
< *count
; i
++) {
5014 if (get_user(value
, values
+ i
)) {
5016 ("Cannot copy value from user space.\n");
5017 err
= ME_ERRNO_INTERNAL
;
5021 if (instance
->ao_idx
& 0x1)
5024 outl(value
, instance
->fifo_reg
);
5026 } else if ((*count
<= ME4600_AO_CIRC_BUF_COUNT
) &&
5028 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
)
5029 == ME4600_AO_FLAGS_SW_WRAP_MODE_INF
)) {
5030 for (i
= 0; i
< *count
; i
++) {
5031 if (get_user(value
, values
+ i
)) {
5033 ("Cannot copy value from user space.\n");
5034 err
= ME_ERRNO_INTERNAL
;
5038 instance
->circ_buf
.buf
[i
] = value
; /* Used to hold the values. */
5041 instance
->circ_buf
.tail
= 0; /* Used as the current read position. */
5042 instance
->circ_buf
.head
= *count
; /* Used as the buffer size. */
5044 /* Preload the FIFO. */
5046 for (i
= 0; i
< ME4600_AO_FIFO_COUNT
;
5047 i
++, instance
->circ_buf
.tail
++) {
5048 if (instance
->circ_buf
.tail
>=
5049 instance
->circ_buf
.head
)
5050 instance
->circ_buf
.tail
= 0;
5052 if (instance
->ao_idx
& 0x1)
5053 outl(instance
->circ_buf
.
5054 buf
[instance
->circ_buf
.
5056 instance
->fifo_reg
);
5058 outl(instance
->circ_buf
.
5059 buf
[instance
->circ_buf
.
5061 instance
->fifo_reg
);
5063 } else if ((*count
<= ME4600_AO_CIRC_BUF_COUNT
) &&
5065 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
)
5066 == ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
)) {
5067 unsigned int preload_count
;
5069 for (i
= 0; i
< *count
; i
++) {
5070 if (get_user(value
, values
+ i
)) {
5072 ("Cannot copy value from user space.\n");
5073 err
= ME_ERRNO_INTERNAL
;
5077 instance
->circ_buf
.buf
[i
] = value
; /* Used to hold the values. */
5080 instance
->circ_buf
.tail
= 0; /* Used as the current read position. */
5081 instance
->circ_buf
.head
= *count
; /* Used as the buffer size. */
5083 /* Try to preload the whole FIFO. */
5084 preload_count
= ME4600_AO_FIFO_COUNT
;
5086 if (preload_count
> instance
->wrap_count
)
5087 preload_count
= instance
->wrap_count
;
5089 /* Preload the FIFO. */
5090 for (i
= 0; i
< preload_count
;
5091 i
++, instance
->circ_buf
.tail
++) {
5092 if (instance
->circ_buf
.tail
>=
5093 instance
->circ_buf
.head
)
5094 instance
->circ_buf
.tail
= 0;
5096 if (instance
->ao_idx
& 0x1)
5097 outl(instance
->circ_buf
.
5098 buf
[instance
->circ_buf
.
5100 instance
->fifo_reg
);
5102 outl(instance
->circ_buf
.
5103 buf
[instance
->circ_buf
.
5105 instance
->fifo_reg
);
5108 instance
->wrap_remaining
=
5109 instance
->wrap_count
- preload_count
;
5111 PERROR("To many values written.\n");
5112 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
5119 case 2: // Continuous mode
5120 /* Check if in SW wrapround mode */
5121 if (instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) {
5122 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5124 PERROR("Subdevice is configured SW wrapround mode.\n");
5125 err
= ME_ERRNO_PREVIOUS_CONFIG
;
5129 switch (write_mode
) {
5131 case ME_WRITE_MODE_BLOCKING
:
5132 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5135 PDEBUG("Write for blocking continuous mode.\n");
5138 wait_event_interruptible(instance
->wait_queue
,
5140 me_circ_buf_space_to_end
5145 flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
5147 ("Broken pipe in blocking write.\n");
5148 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
5150 } else if (signal_pending(current
)) {
5152 ("Wait for free buffer interrupted from signal.\n");
5153 err
= ME_ERRNO_SIGNAL
;
5157 PDEBUG("Space to end = %d.\n", c
);
5159 /* Only able to write size of free buffer or size of count */
5163 k
= sizeof(int) * c
;
5164 k
-= copy_from_user(instance
->circ_buf
.buf
+
5165 instance
->circ_buf
.head
,
5167 c
= k
/ sizeof(int);
5169 PDEBUG("Copy %d values from user space.\n", c
);
5173 ("Cannot copy values from user space.\n");
5174 err
= ME_ERRNO_INTERNAL
;
5178 instance
->circ_buf
.head
=
5179 (instance
->circ_buf
.head
+
5180 c
) & (instance
->circ_buf
.mask
);
5186 /* Values are now available so enable interrupts */
5187 spin_lock_irqsave(&instance
->subdevice_lock
,
5190 if (me_circ_buf_space(&instance
->circ_buf
)) {
5191 tmp
= inl(instance
->ctrl_reg
);
5192 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5193 outl(tmp
, instance
->ctrl_reg
);
5196 spin_unlock_irqrestore(&instance
->
5205 case ME_WRITE_MODE_NONBLOCKING
:
5206 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5209 PDEBUG("Write for non blocking continuous mode.\n");
5213 flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
5215 ("ME4600:Broken pipe in nonblocking write.\n");
5216 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
5220 c
= me_circ_buf_space_to_end(&instance
->
5225 ("Returning from nonblocking write.\n");
5229 PDEBUG("Space to end = %d.\n", c
);
5231 /* Only able to write size of free buffer or size of count */
5235 k
= sizeof(int) * c
;
5236 k
-= copy_from_user(instance
->circ_buf
.buf
+
5237 instance
->circ_buf
.head
,
5239 c
= k
/ sizeof(int);
5241 PDEBUG("Copy %d values from user space.\n", c
);
5245 ("Cannot copy values from user space.\n");
5246 err
= ME_ERRNO_INTERNAL
;
5250 instance
->circ_buf
.head
=
5251 (instance
->circ_buf
.head
+
5252 c
) & (instance
->circ_buf
.mask
);
5258 /* Values are now available so enable interrupts */
5259 spin_lock_irqsave(&instance
->subdevice_lock
,
5262 if (me_circ_buf_space(&instance
->circ_buf
)) {
5263 tmp
= inl(instance
->ctrl_reg
);
5264 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5265 outl(tmp
, instance
->ctrl_reg
);
5268 spin_unlock_irqrestore(&instance
->
5277 case ME_WRITE_MODE_PRELOAD
:
5278 PDEBUG("Write for preload continuous mode.\n");
5280 if ((inl(instance
->status_reg
) &
5281 ME4600_AO_STATUS_BIT_FSM
)) {
5282 spin_unlock_irqrestore(&instance
->
5286 ("Can't Preload DAC FIFO while conversion is running.\n");
5287 err
= ME_ERRNO_SUBDEVICE_BUSY
;
5291 tmp
= inl(instance
->ctrl_reg
);
5294 ME4600_AO_CTRL_BIT_STOP
|
5295 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
5296 outl(tmp
, instance
->ctrl_reg
);
5298 ~(ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
5299 ME4600_AO_CTRL_BIT_ENABLE_IRQ
);
5300 outl(tmp
, instance
->ctrl_reg
);
5301 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
5302 outl(tmp
, instance
->ctrl_reg
);
5304 instance
->circ_buf
.head
= 0;
5305 instance
->circ_buf
.tail
= 0;
5306 instance
->flags
&= ~ME4600_AO_FLAGS_BROKEN_PIPE
;
5308 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5311 c
= ME4600_AO_FIFO_COUNT
;
5316 for (i
= 0; i
< c
; i
++) {
5317 if (get_user(value
, values
)) {
5319 ("Can't copy value from user space.\n");
5320 err
= ME_ERRNO_INTERNAL
;
5324 if (instance
->ao_idx
& 0x1)
5327 outl(value
, instance
->fifo_reg
);
5336 PDEBUG("Wrote %d values to fifo.\n", c
);
5339 c
= me_circ_buf_space_to_end(&instance
->
5351 k
= sizeof(int) * c
;
5353 k
-= copy_from_user(instance
->circ_buf
.buf
+
5354 instance
->circ_buf
.head
,
5357 c
= k
/ sizeof(int);
5359 PDEBUG("Wrote %d values to circular buffer.\n",
5364 ("Can't copy values from user space.\n");
5365 err
= ME_ERRNO_INTERNAL
;
5369 instance
->circ_buf
.head
=
5370 (instance
->circ_buf
.head
+
5371 c
) & (instance
->circ_buf
.mask
);
5383 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5386 PERROR("Invalid write mode specified.\n");
5388 err
= ME_ERRNO_INVALID_WRITE_MODE
;
5395 default: // Single mode of invalid
5396 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
5397 PERROR("Subdevice is configured in single mode.\n");
5398 err
= ME_ERRNO_PREVIOUS_CONFIG
;
5409 static irqreturn_t
me4600_ao_isr(int irq
, void *dev_id
)
5413 me4600_ao_subdevice_t
*instance
= dev_id
;
5418 if (irq
!= instance
->irq
) {
5419 PDEBUG("Incorrect interrupt num: %d.\n", irq
);
5423 if (!((0x1 << (instance
->ao_idx
+ 3)) & inl(instance
->irq_status_reg
))) {
5427 PDEBUG("executed.\n");
5429 tmp
= inl(instance
->status_reg
);
5431 if (!(tmp
& ME4600_AO_STATUS_BIT_EF
) &&
5432 (tmp
& ME4600_AO_STATUS_BIT_HF
) &&
5433 (tmp
& ME4600_AO_STATUS_BIT_HF
)) {
5434 c
= ME4600_AO_FIFO_COUNT
;
5435 PDEBUG("Fifo empty.\n");
5436 } else if ((tmp
& ME4600_AO_STATUS_BIT_EF
) &&
5437 (tmp
& ME4600_AO_STATUS_BIT_HF
) &&
5438 (tmp
& ME4600_AO_STATUS_BIT_HF
)) {
5439 c
= ME4600_AO_FIFO_COUNT
/ 2;
5440 PDEBUG("Fifo under half full.\n");
5443 PDEBUG("Fifo full.\n");
5446 PDEBUG("Try to write 0x%04X values.\n", c
);
5448 if ((instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5449 ME4600_AO_FLAGS_SW_WRAP_MODE_INF
) {
5453 if (c1
> (instance
->circ_buf
.head
- instance
->circ_buf
.tail
)) /* Only up to the end of the buffer */
5454 c1
= (instance
->circ_buf
.head
-
5455 instance
->circ_buf
.tail
);
5457 /* Write the values to the FIFO */
5458 for (i
= 0; i
< c1
; i
++, instance
->circ_buf
.tail
++, c
--) {
5459 if (instance
->ao_idx
& 0x1)
5460 outl(instance
->circ_buf
.
5461 buf
[instance
->circ_buf
.tail
] << 16,
5462 instance
->fifo_reg
);
5464 outl(instance
->circ_buf
.
5465 buf
[instance
->circ_buf
.tail
],
5466 instance
->fifo_reg
);
5469 if (instance
->circ_buf
.tail
>= instance
->circ_buf
.head
) /* Start from beginning */
5470 instance
->circ_buf
.tail
= 0;
5473 spin_lock(&instance
->subdevice_lock
);
5475 tmp
= inl(instance
->ctrl_reg
);
5476 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5477 outl(tmp
, instance
->ctrl_reg
);
5478 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5479 outl(tmp
, instance
->ctrl_reg
);
5481 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5482 PERROR("Broken pipe.\n");
5483 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5484 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5485 outl(tmp
, instance
->ctrl_reg
);
5488 spin_unlock(&instance
->subdevice_lock
);
5489 } else if ((instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5490 ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
) {
5491 while (c
&& instance
->wrap_remaining
) {
5494 if (c1
> (instance
->circ_buf
.head
- instance
->circ_buf
.tail
)) /* Only up to the end of the buffer */
5495 c1
= (instance
->circ_buf
.head
-
5496 instance
->circ_buf
.tail
);
5498 if (c1
> instance
->wrap_remaining
) /* Only up to count of user defined number of values */
5499 c1
= instance
->wrap_remaining
;
5501 /* Write the values to the FIFO */
5503 i
++, instance
->circ_buf
.tail
++, c
--,
5504 instance
->wrap_remaining
--) {
5505 if (instance
->ao_idx
& 0x1)
5506 outl(instance
->circ_buf
.
5507 buf
[instance
->circ_buf
.tail
] << 16,
5508 instance
->fifo_reg
);
5510 outl(instance
->circ_buf
.
5511 buf
[instance
->circ_buf
.tail
],
5512 instance
->fifo_reg
);
5515 if (instance
->circ_buf
.tail
>= instance
->circ_buf
.head
) /* Start from beginning */
5516 instance
->circ_buf
.tail
= 0;
5519 spin_lock(&instance
->subdevice_lock
);
5521 tmp
= inl(instance
->ctrl_reg
);
5523 if (!instance
->wrap_remaining
) {
5524 PDEBUG("Finite SW wraparound done.\n");
5525 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5528 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5530 outl(tmp
, instance
->ctrl_reg
);
5531 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5532 outl(tmp
, instance
->ctrl_reg
);
5534 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5535 PERROR("Broken pipe.\n");
5536 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5537 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5538 outl(tmp
, instance
->ctrl_reg
);
5541 spin_unlock(&instance
->subdevice_lock
);
5543 } else { /* Regular continuous mode */
5546 c1
= me_circ_buf_values_to_end(&instance
->circ_buf
);
5547 PDEBUG("Values to end = %d.\n", c1
);
5553 PDEBUG("Work done or buffer empty.\n");
5557 if (instance
->ao_idx
& 0x1) {
5558 for (i
= 0; i
< c1
; i
++) {
5560 *(instance
->circ_buf
.buf
+
5561 instance
->circ_buf
.tail
+
5563 outl(value
, instance
->fifo_reg
);
5566 outsl(instance
->fifo_reg
,
5567 instance
->circ_buf
.buf
+
5568 instance
->circ_buf
.tail
, c1
);
5570 instance
->circ_buf
.tail
=
5571 (instance
->circ_buf
.tail
+
5572 c1
) & (instance
->circ_buf
.mask
);
5574 PDEBUG("%d values wrote to port 0x%04X.\n", c1
,
5575 instance
->fifo_reg
);
5580 spin_lock(&instance
->subdevice_lock
);
5582 tmp
= inl(instance
->ctrl_reg
);
5584 if (!me_circ_buf_values(&instance
->circ_buf
)) {
5586 ("Disable Interrupt because no values left in buffer.\n");
5587 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5590 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5592 outl(tmp
, instance
->ctrl_reg
);
5593 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5594 outl(tmp
, instance
->ctrl_reg
);
5596 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5597 PDEBUG("Broken pipe in me4600_ao_isr.\n");
5598 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5599 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5600 outl(tmp
, instance
->ctrl_reg
);
5603 spin_unlock(&instance
->subdevice_lock
);
5605 wake_up_interruptible(&instance
->wait_queue
);
5611 static void me4600_ao_destructor(struct me_subdevice
*subdevice
)
5613 me4600_ao_subdevice_t
*instance
;
5615 PDEBUG("executed.\n");
5617 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5619 free_irq(instance
->irq
, instance
);
5620 kfree(instance
->circ_buf
.buf
);
5621 me_subdevice_deinit(&instance
->base
);
5625 me4600_ao_subdevice_t
*me4600_ao_constructor(uint32_t reg_base
,
5626 spinlock_t
*preload_reg_lock
,
5627 uint32_t *preload_flags
,
5628 int ao_idx
, int fifo
, int irq
)
5630 me4600_ao_subdevice_t
*subdevice
;
5633 PDEBUG("executed.\n");
5635 /* Allocate memory for subdevice instance */
5636 subdevice
= kmalloc(sizeof(me4600_ao_subdevice_t
), GFP_KERNEL
);
5639 PERROR("Cannot get memory for subdevice instance.\n");
5643 memset(subdevice
, 0, sizeof(me4600_ao_subdevice_t
));
5645 /* Initialize subdevice base class */
5646 err
= me_subdevice_init(&subdevice
->base
);
5649 PERROR("Cannot initialize subdevice base class instance.\n");
5653 // Initialize spin locks.
5654 spin_lock_init(&subdevice
->subdevice_lock
);
5656 subdevice
->preload_reg_lock
= preload_reg_lock
;
5657 subdevice
->preload_flags
= preload_flags
;
5659 /* Allocate and initialize circular buffer */
5660 subdevice
->circ_buf
.mask
= ME4600_AO_CIRC_BUF_COUNT
- 1;
5661 subdevice
->circ_buf
.buf
= kmalloc(ME4600_AO_CIRC_BUF_SIZE
, GFP_KERNEL
);
5663 if (!subdevice
->circ_buf
.buf
) {
5664 PERROR("Cannot initialize subdevice base class instance.\n");
5665 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
5670 memset(subdevice
->circ_buf
.buf
, 0, ME4600_AO_CIRC_BUF_SIZE
);
5672 subdevice
->circ_buf
.head
= 0;
5673 subdevice
->circ_buf
.tail
= 0;
5675 /* Initialize wait queue */
5676 init_waitqueue_head(&subdevice
->wait_queue
);
5678 /* Initialize single value to 0V */
5679 subdevice
->single_value
= 0x8000;
5681 /* Store analog output index */
5682 subdevice
->ao_idx
= ao_idx
;
5684 /* Store if analog output has fifo */
5685 subdevice
->fifo
= fifo
;
5687 /* Initialize registers */
5690 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_00_CTRL_REG
;
5691 subdevice
->status_reg
= reg_base
+ ME4600_AO_00_STATUS_REG
;
5692 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_00_FIFO_REG
;
5693 subdevice
->single_reg
= reg_base
+ ME4600_AO_00_SINGLE_REG
;
5694 subdevice
->timer_reg
= reg_base
+ ME4600_AO_00_TIMER_REG
;
5695 subdevice
->reg_base
= reg_base
;
5697 if (inl(subdevice
->reg_base
+ ME4600_AO_BOSCH_REG
) == 0x20000) {
5698 PINFO("Bosch firmware in use for channel 0.\n");
5699 subdevice
->bosch_fw
= 1;
5701 subdevice
->bosch_fw
= 0;
5703 } else if (ao_idx
== 1) {
5704 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_01_CTRL_REG
;
5705 subdevice
->status_reg
= reg_base
+ ME4600_AO_01_STATUS_REG
;
5706 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_01_FIFO_REG
;
5707 subdevice
->single_reg
= reg_base
+ ME4600_AO_01_SINGLE_REG
;
5708 subdevice
->timer_reg
= reg_base
+ ME4600_AO_01_TIMER_REG
;
5709 subdevice
->reg_base
= reg_base
;
5710 subdevice
->bosch_fw
= 0;
5711 } else if (ao_idx
== 2) {
5712 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_02_CTRL_REG
;
5713 subdevice
->status_reg
= reg_base
+ ME4600_AO_02_STATUS_REG
;
5714 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_02_FIFO_REG
;
5715 subdevice
->single_reg
= reg_base
+ ME4600_AO_02_SINGLE_REG
;
5716 subdevice
->timer_reg
= reg_base
+ ME4600_AO_02_TIMER_REG
;
5717 subdevice
->reg_base
= reg_base
;
5718 subdevice
->bosch_fw
= 0;
5720 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_03_CTRL_REG
;
5721 subdevice
->status_reg
= reg_base
+ ME4600_AO_03_STATUS_REG
;
5722 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_03_FIFO_REG
;
5723 subdevice
->single_reg
= reg_base
+ ME4600_AO_03_SINGLE_REG
;
5724 subdevice
->timer_reg
= reg_base
+ ME4600_AO_03_TIMER_REG
;
5725 subdevice
->reg_base
= reg_base
;
5726 subdevice
->bosch_fw
= 0;
5729 subdevice
->irq_status_reg
= reg_base
+ ME4600_IRQ_STATUS_REG
;
5730 subdevice
->preload_reg
= reg_base
+ ME4600_AO_LOADSETREG_XX
;
5732 /* Register interrupt service routine */
5733 subdevice
->irq
= irq
;
5736 (subdevice
->irq
, me4600_ao_isr
, IRQF_DISABLED
| IRQF_SHARED
,
5737 ME4600_NAME
, subdevice
)) {
5738 PERROR("Cannot get interrupt line.\n");
5739 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
5740 kfree(subdevice
->circ_buf
.buf
);
5745 /* Override base class methods. */
5746 subdevice
->base
.me_subdevice_destructor
= me4600_ao_destructor
;
5747 subdevice
->base
.me_subdevice_io_reset_subdevice
=
5748 me4600_ao_io_reset_subdevice
;
5749 subdevice
->base
.me_subdevice_io_single_config
=
5750 me4600_ao_io_single_config
;
5751 subdevice
->base
.me_subdevice_io_single_read
= me4600_ao_io_single_read
;
5752 subdevice
->base
.me_subdevice_io_single_write
=
5753 me4600_ao_io_single_write
;
5754 subdevice
->base
.me_subdevice_io_stream_config
=
5755 me4600_ao_io_stream_config
;
5756 subdevice
->base
.me_subdevice_io_stream_new_values
=
5757 me4600_ao_io_stream_new_values
;
5758 subdevice
->base
.me_subdevice_io_stream_write
=
5759 me4600_ao_io_stream_write
;
5760 subdevice
->base
.me_subdevice_io_stream_start
=
5761 me4600_ao_io_stream_start
;
5762 subdevice
->base
.me_subdevice_io_stream_status
=
5763 me4600_ao_io_stream_status
;
5764 subdevice
->base
.me_subdevice_io_stream_stop
= me4600_ao_io_stream_stop
;
5765 subdevice
->base
.me_subdevice_query_number_channels
=
5766 me4600_ao_query_number_channels
;
5767 subdevice
->base
.me_subdevice_query_subdevice_type
=
5768 me4600_ao_query_subdevice_type
;
5769 subdevice
->base
.me_subdevice_query_subdevice_caps
=
5770 me4600_ao_query_subdevice_caps
;
5771 subdevice
->base
.me_subdevice_query_subdevice_caps_args
=
5772 me4600_ao_query_subdevice_caps_args
;
5773 subdevice
->base
.me_subdevice_query_range_by_min_max
=
5774 me4600_ao_query_range_by_min_max
;
5775 subdevice
->base
.me_subdevice_query_number_ranges
=
5776 me4600_ao_query_number_ranges
;
5777 subdevice
->base
.me_subdevice_query_range_info
=
5778 me4600_ao_query_range_info
;
5779 subdevice
->base
.me_subdevice_query_timer
= me4600_ao_query_timer
;
5789 static int me4600_ao_query_range_by_min_max(me_subdevice_t
*subdevice
,
5792 int *max
, int *maxdata
, int *range
)
5794 me4600_ao_subdevice_t
*instance
;
5796 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5798 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5800 if ((*max
- *min
) < 0) {
5801 PERROR("Invalid minimum and maximum values specified.\n");
5802 return ME_ERRNO_INVALID_MIN_MAX
;
5805 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
5806 if ((*max
<= (ME4600_AO_MAX_RANGE
+ 1000))
5807 && (*min
>= ME4600_AO_MIN_RANGE
)) {
5808 *min
= ME4600_AO_MIN_RANGE
;
5809 *max
= ME4600_AO_MAX_RANGE
;
5810 *maxdata
= ME4600_AO_MAX_DATA
;
5813 PERROR("No matching range available.\n");
5814 return ME_ERRNO_NO_RANGE
;
5817 PERROR("Invalid physical unit specified.\n");
5818 return ME_ERRNO_INVALID_UNIT
;
5821 return ME_ERRNO_SUCCESS
;
5824 static int me4600_ao_query_number_ranges(me_subdevice_t
*subdevice
,
5825 int unit
, int *count
)
5827 me4600_ao_subdevice_t
*instance
;
5829 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5831 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5833 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
5839 return ME_ERRNO_SUCCESS
;
5842 static int me4600_ao_query_range_info(me_subdevice_t
*subdevice
,
5845 int *min
, int *max
, int *maxdata
)
5847 me4600_ao_subdevice_t
*instance
;
5849 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5851 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5854 *unit
= ME_UNIT_VOLT
;
5855 *min
= ME4600_AO_MIN_RANGE
;
5856 *max
= ME4600_AO_MAX_RANGE
;
5857 *maxdata
= ME4600_AO_MAX_DATA
;
5859 PERROR("Invalid range number specified.\n");
5860 return ME_ERRNO_INVALID_RANGE
;
5863 return ME_ERRNO_SUCCESS
;
5866 static int me4600_ao_query_timer(me_subdevice_t
*subdevice
,
5868 int *base_frequency
,
5869 long long *min_ticks
, long long *max_ticks
)
5871 me4600_ao_subdevice_t
*instance
;
5873 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5875 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5877 if ((timer
!= ME_TIMER_ACQ_START
) && (timer
!= ME_TIMER_CONV_START
)) {
5878 PERROR("Invalid timer specified.\n");
5879 return ME_ERRNO_INVALID_TIMER
;
5882 if (instance
->fifo
) { //Streaming device.
5883 *base_frequency
= ME4600_AO_BASE_FREQUENCY
;
5884 if (timer
== ME_TIMER_ACQ_START
) {
5885 *min_ticks
= ME4600_AO_MIN_ACQ_TICKS
;
5886 *max_ticks
= ME4600_AO_MAX_ACQ_TICKS
;
5887 } else if (timer
== ME_TIMER_CONV_START
) {
5888 *min_ticks
= ME4600_AO_MIN_CHAN_TICKS
;
5889 *max_ticks
= ME4600_AO_MAX_CHAN_TICKS
;
5891 } else { //Not streaming device!
5892 *base_frequency
= 0;
5897 return ME_ERRNO_SUCCESS
;
5900 static int me4600_ao_query_number_channels(me_subdevice_t
*subdevice
,
5903 me4600_ao_subdevice_t
*instance
;
5904 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5906 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5910 return ME_ERRNO_SUCCESS
;
5913 static int me4600_ao_query_subdevice_type(me_subdevice_t
*subdevice
,
5914 int *type
, int *subtype
)
5916 me4600_ao_subdevice_t
*instance
;
5918 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5920 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5923 *subtype
= (instance
->fifo
) ? ME_SUBTYPE_STREAMING
: ME_SUBTYPE_SINGLE
;
5925 return ME_ERRNO_SUCCESS
;
5928 static int me4600_ao_query_subdevice_caps(me_subdevice_t
*subdevice
, int *caps
)
5930 me4600_ao_subdevice_t
*instance
;
5931 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5933 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5936 ME_CAPS_AO_TRIG_SYNCHRONOUS
| ((instance
->fifo
) ? ME_CAPS_AO_FIFO
:
5939 return ME_ERRNO_SUCCESS
;
5942 static int me4600_ao_query_subdevice_caps_args(struct me_subdevice
*subdevice
,
5943 int cap
, int *args
, int count
)
5945 me4600_ao_subdevice_t
*instance
;
5946 int err
= ME_ERRNO_SUCCESS
;
5948 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5950 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5953 PERROR("Invalid capability argument count.\n");
5954 return ME_ERRNO_INVALID_CAP_ARG_COUNT
;
5958 case ME_CAP_AI_FIFO_SIZE
:
5959 args
[0] = (instance
->fifo
) ? ME4600_AO_FIFO_COUNT
: 0;
5962 case ME_CAP_AI_BUFFER_SIZE
:
5964 (instance
->circ_buf
.buf
) ? ME4600_AO_CIRC_BUF_COUNT
: 0;
5968 PERROR("Invalid capability.\n");
5969 err
= ME_ERRNO_INVALID_CAP
;