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 #ifdef IRQF_DISABLED
2571 IRQF_DISABLED
| IRQF_SHARED
,
2573 SA_INTERRUPT
| SA_SHIRQ
,
2575 ME4600_NAME
, subdevice
)) {
2576 PERROR("Cannot get interrupt line.\n");
2577 PDEBUG("free circ_buf = %p size=%d",
2578 subdevice
->circ_buf
.buf
,
2579 PAGE_SHIFT
<< ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2580 free_pages((unsigned long)subdevice
->circ_buf
.buf
,
2581 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2582 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
2586 PINFO("Registered irq=%d.\n", subdevice
->irq
);
2591 // Initialize registers.
2592 subdevice
->irq_status_reg
= reg_base
+ ME4600_IRQ_STATUS_REG
;
2593 subdevice
->preload_reg
= reg_base
+ ME4600_AO_SYNC_REG
;
2595 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_00_CTRL_REG
;
2596 subdevice
->status_reg
= reg_base
+ ME4600_AO_00_STATUS_REG
;
2597 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_00_FIFO_REG
;
2598 subdevice
->single_reg
= reg_base
+ ME4600_AO_00_SINGLE_REG
;
2599 subdevice
->timer_reg
= reg_base
+ ME4600_AO_00_TIMER_REG
;
2600 subdevice
->reg_base
= reg_base
;
2601 subdevice
->bitpattern
= 0;
2602 } else if (ao_idx
== 1) {
2603 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_01_CTRL_REG
;
2604 subdevice
->status_reg
= reg_base
+ ME4600_AO_01_STATUS_REG
;
2605 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_01_FIFO_REG
;
2606 subdevice
->single_reg
= reg_base
+ ME4600_AO_01_SINGLE_REG
;
2607 subdevice
->timer_reg
= reg_base
+ ME4600_AO_01_TIMER_REG
;
2608 subdevice
->reg_base
= reg_base
;
2609 subdevice
->bitpattern
= 0;
2610 } else if (ao_idx
== 2) {
2611 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_02_CTRL_REG
;
2612 subdevice
->status_reg
= reg_base
+ ME4600_AO_02_STATUS_REG
;
2613 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_02_FIFO_REG
;
2614 subdevice
->single_reg
= reg_base
+ ME4600_AO_02_SINGLE_REG
;
2615 subdevice
->timer_reg
= reg_base
+ ME4600_AO_02_TIMER_REG
;
2616 subdevice
->reg_base
= reg_base
;
2617 subdevice
->bitpattern
= 0;
2618 } else if (ao_idx
== 3) {
2619 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_03_CTRL_REG
;
2620 subdevice
->status_reg
= reg_base
+ ME4600_AO_03_STATUS_REG
;
2621 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_03_FIFO_REG
;
2622 subdevice
->single_reg
= reg_base
+ ME4600_AO_03_SINGLE_REG
;
2623 subdevice
->timer_reg
= reg_base
+ ME4600_AO_03_TIMER_REG
;
2624 subdevice
->reg_base
= reg_base
;
2625 subdevice
->bitpattern
= 1;
2627 PERROR_CRITICAL("WRONG SUBDEVICE idx=%d!", ao_idx
);
2628 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
2629 if (subdevice
->fifo
) {
2630 free_pages((unsigned long)subdevice
->circ_buf
.buf
,
2631 ME4600_AO_CIRC_BUF_SIZE_ORDER
);
2633 subdevice
->circ_buf
.buf
= NULL
;
2638 // Override base class methods.
2639 subdevice
->base
.me_subdevice_destructor
= me4600_ao_destructor
;
2640 subdevice
->base
.me_subdevice_io_reset_subdevice
=
2641 me4600_ao_io_reset_subdevice
;
2642 subdevice
->base
.me_subdevice_io_single_config
=
2643 me4600_ao_io_single_config
;
2644 subdevice
->base
.me_subdevice_io_single_read
= me4600_ao_io_single_read
;
2645 subdevice
->base
.me_subdevice_io_single_write
=
2646 me4600_ao_io_single_write
;
2647 subdevice
->base
.me_subdevice_io_stream_config
=
2648 me4600_ao_io_stream_config
;
2649 subdevice
->base
.me_subdevice_io_stream_new_values
=
2650 me4600_ao_io_stream_new_values
;
2651 subdevice
->base
.me_subdevice_io_stream_write
=
2652 me4600_ao_io_stream_write
;
2653 subdevice
->base
.me_subdevice_io_stream_start
=
2654 me4600_ao_io_stream_start
;
2655 subdevice
->base
.me_subdevice_io_stream_status
=
2656 me4600_ao_io_stream_status
;
2657 subdevice
->base
.me_subdevice_io_stream_stop
= me4600_ao_io_stream_stop
;
2658 subdevice
->base
.me_subdevice_query_number_channels
=
2659 me4600_ao_query_number_channels
;
2660 subdevice
->base
.me_subdevice_query_subdevice_type
=
2661 me4600_ao_query_subdevice_type
;
2662 subdevice
->base
.me_subdevice_query_subdevice_caps
=
2663 me4600_ao_query_subdevice_caps
;
2664 subdevice
->base
.me_subdevice_query_subdevice_caps_args
=
2665 me4600_ao_query_subdevice_caps_args
;
2666 subdevice
->base
.me_subdevice_query_range_by_min_max
=
2667 me4600_ao_query_range_by_min_max
;
2668 subdevice
->base
.me_subdevice_query_number_ranges
=
2669 me4600_ao_query_number_ranges
;
2670 subdevice
->base
.me_subdevice_query_range_info
=
2671 me4600_ao_query_range_info
;
2672 subdevice
->base
.me_subdevice_query_timer
= me4600_ao_query_timer
;
2674 // Prepare work queue
2675 subdevice
->me4600_workqueue
= me4600_wq
;
2677 /* workqueue API changed in kernel 2.6.20 */
2678 INIT_DELAYED_WORK(&subdevice
->ao_control_task
,
2679 me4600_ao_work_control_task
);
2681 if (subdevice
->fifo
) { // Set speed for single operations.
2682 outl(ME4600_AO_MIN_CHAN_TICKS
- 1, subdevice
->timer_reg
);
2683 subdevice
->hardware_stop_delay
= HZ
/ 10; //100ms
2689 /** @brief Stop presentation. Preserve FIFOs.
2691 * @param instance The subdevice instance (pointer).
2693 inline int ao_stop_immediately(me4600_ao_subdevice_t
*instance
)
2695 unsigned long cpu_flags
;
2701 (instance
->hardware_stop_delay
>
2702 (HZ
/ 10)) ? instance
->hardware_stop_delay
: HZ
/ 10;
2703 for (i
= 0; i
<= timeout
; i
++) {
2704 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
2705 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
2706 ctrl
= inl(instance
->ctrl_reg
);
2708 ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
2709 | ME4600_AO_CTRL_BIT_RESET_IRQ
;
2711 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
2712 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
2713 outl(ctrl
, instance
->ctrl_reg
);
2714 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
2716 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
2717 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
2719 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) { // Exit.
2723 set_current_state(TASK_INTERRUPTIBLE
);
2724 schedule_timeout(1);
2728 PERROR_CRITICAL("FSM IS BUSY!\n");
2729 return ME_ERRNO_INTERNAL
;
2731 return ME_ERRNO_SUCCESS
;
2734 /** @brief Copy data from circular buffer to fifo (fast) in wraparound.
2735 * @note This is time critical function. Checking is done at begining and end only.
2736 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2738 * @param instance The subdevice instance (pointer).
2739 * @param count Maximum number of copied data.
2740 * @param start_pos Position of the firs value in buffer.
2742 * @return On success: Number of copied data.
2743 * @return On error/success: 0. No datas were copied => no data in buffer.
2744 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2746 inline int ao_write_data_wraparound(me4600_ao_subdevice_t
*instance
, int count
,
2748 { /// @note This is time critical function!
2752 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2753 int local_count
= count
;
2756 if (count
<= 0) { //Wrong count!
2760 while (i
< local_count
) {
2761 //Get value from buffer
2762 value
= *(instance
->circ_buf
.buf
+ pos
);
2764 if (instance
->ao_idx
& 0x1) {
2768 outl(value
, instance
->fifo_reg
);
2769 //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);
2772 pos
&= instance
->circ_buf
.mask
;
2773 if (pos
== instance
->circ_buf
.head
) {
2774 pos
= instance
->circ_buf
.tail
;
2779 status
= inl(instance
->status_reg
);
2780 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2781 PERROR("FIFO was full before all datas were copied! idx=%d\n",
2783 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2784 } else { //Add last value
2785 value
= *(instance
->circ_buf
.buf
+ pos
);
2786 if (instance
->ao_idx
& 0x1) {
2790 outl(value
, instance
->fifo_reg
);
2791 //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);
2794 PINFO("WRAPAROUND LOADED %d values. idx=%d\n", local_count
,
2799 /** @brief Copy data from software buffer to fifo (fast).
2800 * @note This is time critical function. Checking is done at begining and end only.
2801 * @note The is not reasonable way to check how many walues was in FIFO at begining. The count must be managed externaly.
2803 * @param instance The subdevice instance (pointer).
2804 * @param count Maximum number of copied data.
2805 * @param start_pos Position of the firs value in buffer.
2807 * @return On success: Number of copied data.
2808 * @return On error/success: 0. No datas were copied => no data in buffer.
2809 * @return On error: -ME_ERRNO_FIFO_BUFFER_OVERFLOW.
2811 inline int ao_write_data(me4600_ao_subdevice_t
*instance
, int count
,
2813 { /// @note This is time critical function!
2817 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2818 int local_count
= count
;
2822 if (count
<= 0) { //Wrong count!
2826 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
2827 if (max_count
<= 0) { //No data to copy!
2831 if (max_count
< count
) {
2832 local_count
= max_count
;
2835 while (i
< local_count
) {
2836 //Get value from buffer
2837 value
= *(instance
->circ_buf
.buf
+ pos
);
2839 if (instance
->ao_idx
& 0x1) {
2843 outl(value
, instance
->fifo_reg
);
2844 //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);
2847 pos
&= instance
->circ_buf
.mask
;
2851 status
= inl(instance
->status_reg
);
2852 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full before all datas were copied!
2853 PERROR("FIFO was full before all datas were copied! idx=%d\n",
2855 return -ME_ERRNO_FIFO_BUFFER_OVERFLOW
;
2856 } else { //Add last value
2857 value
= *(instance
->circ_buf
.buf
+ pos
);
2858 if (instance
->ao_idx
& 0x1) {
2862 outl(value
, instance
->fifo_reg
);
2863 //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);
2866 PINFO("FAST LOADED %d values. idx=%d\n", local_count
, instance
->ao_idx
);
2870 /** @brief Copy data from software buffer to fifo (slow).
2871 * @note This is slow function that copy all data from buffer to FIFO with full control.
2873 * @param instance The subdevice instance (pointer).
2874 * @param count Maximum number of copied data.
2875 * @param start_pos Position of the firs value in buffer.
2877 * @return On success: Number of copied values.
2878 * @return On error/success: 0. FIFO was full at begining.
2879 * @return On error: -ME_ERRNO_RING_BUFFER_UNDEFFLOW.
2881 inline int ao_write_data_pooling(me4600_ao_subdevice_t
*instance
, int count
,
2883 { /// @note This is slow function!
2887 (instance
->circ_buf
.tail
+ start_pos
) & instance
->circ_buf
.mask
;
2888 int local_count
= count
;
2892 if (count
<= 0) { //Wrong count!
2893 PERROR("SLOW LOADED: Wrong count! idx=%d\n", instance
->ao_idx
);
2897 max_count
= me_circ_buf_values(&instance
->circ_buf
) - start_pos
;
2898 if (max_count
<= 0) { //No data to copy!
2899 PERROR("SLOW LOADED: No data to copy! idx=%d\n",
2904 if (max_count
< count
) {
2905 local_count
= max_count
;
2908 for (i
= 0; i
< local_count
; i
++) {
2909 status
= inl(instance
->status_reg
);
2910 if (!(status
& ME4600_AO_STATUS_BIT_FF
)) { //FIFO is full!
2913 //Get value from buffer
2914 value
= *(instance
->circ_buf
.buf
+ pos
);
2916 if (instance
->ao_idx
& 0x1) {
2920 outl(value
, instance
->fifo_reg
);
2921 //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);
2924 pos
&= instance
->circ_buf
.mask
;
2927 PINFO("SLOW LOADED %d values. idx=%d\n", local_count
, instance
->ao_idx
);
2931 /** @brief Copy data from user space to circular buffer.
2932 * @param instance The subdevice instance (pointer).
2933 * @param count Number of datas in user space.
2934 * @param user_values Buffer's pointer.
2936 * @return On success: Number of copied values.
2937 * @return On error: -ME_ERRNO_INTERNAL.
2939 inline int ao_get_data_from_user(me4600_ao_subdevice_t
*instance
, int count
,
2947 empty_space
= me_circ_buf_space(&instance
->circ_buf
);
2948 //We have only this space free.
2949 copied
= (count
< empty_space
) ? count
: empty_space
;
2950 for (i
= 0; i
< copied
; i
++) { //Copy from user to buffer
2951 if ((err
= get_user(value
, (int *)(user_values
+ i
)))) {
2953 ("BUFFER LOADED: get_user(0x%p) return an error: %d. idx=%d\n",
2954 user_values
+ i
, err
, instance
->ao_idx
);
2955 return -ME_ERRNO_INTERNAL
;
2957 /// @note The analog output in me4600 series has size of 16 bits.
2958 *(instance
->circ_buf
.buf
+ instance
->circ_buf
.head
) =
2960 instance
->circ_buf
.head
++;
2961 instance
->circ_buf
.head
&= instance
->circ_buf
.mask
;
2964 PINFO("BUFFER LOADED %d values. idx=%d\n", copied
, instance
->ao_idx
);
2968 /** @brief Checking actual hardware and logical state.
2969 * @param instance The subdevice instance (pointer).
2971 static void me4600_ao_work_control_task(struct work_struct
*work
)
2973 me4600_ao_subdevice_t
*instance
;
2974 unsigned long cpu_flags
= 0;
2982 container_of((void *)work
, me4600_ao_subdevice_t
, ao_control_task
);
2983 PINFO("<%s: %ld> executed. idx=%d\n", __func__
, jiffies
,
2986 status
= inl(instance
->status_reg
);
2987 PDEBUG_REG("status_reg inl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
2988 instance
->status_reg
- instance
->reg_base
, status
);
2990 switch (instance
->status
) { // Checking actual mode.
2992 // Not configured for work.
2993 case ao_status_none
:
2996 //This are stable modes. No need to do anything. (?)
2997 case ao_status_single_configured
:
2998 case ao_status_stream_configured
:
2999 case ao_status_stream_fifo_error
:
3000 case ao_status_stream_buffer_error
:
3001 case ao_status_stream_error
:
3002 PERROR("Shouldn't be running!.\n");
3005 case ao_status_stream_end
:
3006 if (!instance
->fifo
) {
3008 ("Streaming on single device! This feature is not implemented in this version!\n");
3009 instance
->status
= ao_status_stream_error
;
3014 case ao_status_single_end
:
3015 if (status
& ME4600_AO_STATUS_BIT_FSM
) { // State machine is working but the status is set to end. Force stop.
3021 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3022 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3023 ctrl
= inl(instance
->ctrl_reg
);
3025 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
| ME4600_AO_CTRL_BIT_STOP
3026 | ME4600_AO_CTRL_BIT_RESET_IRQ
;
3028 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3029 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3031 ~(ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3032 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
3033 outl(ctrl
, instance
->ctrl_reg
);
3034 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3036 instance
->ctrl_reg
- instance
->reg_base
, ctrl
);
3037 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3041 case ao_status_single_run_wait
:
3042 case ao_status_single_run
:
3043 case ao_status_single_end_wait
:
3045 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working.
3046 if (((instance
->fifo
)
3047 && (!(status
& ME4600_AO_STATUS_BIT_EF
)))
3048 || (!(instance
->fifo
))) { // Single is in end state.
3049 PDEBUG("Single call has been complited.\n");
3051 // Set correct value for single_read();
3052 instance
->single_value
=
3053 instance
->single_value_in_fifo
;
3055 // Set status as 'ao_status_single_end'
3056 instance
->status
= ao_status_single_end
;
3060 // Wait for stop ISM.
3067 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3068 PDEBUG("Timeout reached.\n");
3069 // Stop all actions. No conditions! Block interrupts and trigger. Leave FIFO untouched!
3070 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3071 ctrl
= inl(instance
->ctrl_reg
);
3073 ME4600_AO_CTRL_BIT_STOP
|
3074 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
3075 ME4600_AO_CTRL_BIT_RESET_IRQ
;
3077 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3078 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3079 /// Fix for timeout error.
3081 ~(ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3082 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
);
3083 if (instance
->fifo
) { //Disabling FIFO
3084 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
3086 outl(ctrl
, instance
->ctrl_reg
);
3087 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3089 instance
->ctrl_reg
- instance
->reg_base
,
3091 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3094 spin_lock(instance
->preload_reg_lock
);
3095 //Remove from synchronous start. Block triggering from this output.
3096 synch
= inl(instance
->preload_reg
);
3098 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) <<
3100 if (!(instance
->fifo
)) { // No FIFO - set to single safe mode
3102 ME4600_AO_SYNC_HOLD
<< instance
->ao_idx
;
3104 outl(synch
, instance
->preload_reg
);
3105 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3107 instance
->preload_reg
- instance
->reg_base
,
3109 spin_unlock(instance
->preload_reg_lock
);
3111 if (!(instance
->fifo
)) { // No FIFO
3112 // Restore old settings.
3113 PDEBUG("Write old value back to register.\n");
3114 outl(instance
->single_value
,
3115 instance
->single_reg
);
3117 ("single_reg outl(0x%lX+0x%lX)=0x%x\n",
3119 instance
->single_reg
- instance
->reg_base
,
3120 instance
->single_value
);
3122 // Set correct value for single_read();
3123 instance
->single_value_in_fifo
= instance
->single_value
;
3125 instance
->status
= ao_status_single_end
;
3135 case ao_status_stream_run_wait
:
3136 if (!instance
->fifo
) {
3138 ("Streaming on single device! This feature is not implemented in this version!\n");
3139 instance
->status
= ao_status_stream_error
;
3145 if (status
& ME4600_AO_STATUS_BIT_FSM
) { // State machine is working. Waiting for start finish.
3146 instance
->status
= ao_status_stream_run
;
3148 // Signal end of this step
3150 } else { // State machine is not working.
3151 if (!(status
& ME4600_AO_STATUS_BIT_EF
)) { // FIFO is empty. Procedure has started and finish already!
3152 instance
->status
= ao_status_stream_end
;
3163 if ((instance
->timeout
.delay
) && ((jiffies
- instance
->timeout
.start_time
) >= instance
->timeout
.delay
)) { // Timeout
3164 PDEBUG("Timeout reached.\n");
3165 // Stop all actions. No conditions! Block interrupts. Leave FIFO untouched!
3166 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3167 ctrl
= inl(instance
->ctrl_reg
);
3169 ME4600_AO_CTRL_BIT_STOP
|
3170 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
|
3171 ME4600_AO_CTRL_BIT_RESET_IRQ
;
3173 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
3174 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
3175 outl(ctrl
, instance
->ctrl_reg
);
3176 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
3178 instance
->ctrl_reg
- instance
->reg_base
,
3180 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3182 spin_lock(instance
->preload_reg_lock
);
3183 //Remove from synchronous start. Block triggering from this output.
3184 synch
= inl(instance
->preload_reg
);
3186 ~((ME4600_AO_SYNC_HOLD
| ME4600_AO_SYNC_EXT_TRIG
) <<
3188 outl(synch
, instance
->preload_reg
);
3189 PDEBUG_REG("preload_reg outl(0x%lX+0x%lX)=0x%x\n",
3191 instance
->preload_reg
- instance
->reg_base
,
3193 spin_unlock(instance
->preload_reg_lock
);
3195 instance
->status
= ao_status_stream_end
;
3204 case ao_status_stream_run
:
3205 if (!instance
->fifo
) {
3207 ("Streaming on single device! This feature is not implemented in this version!\n");
3208 instance
->status
= ao_status_stream_error
;
3214 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working. This is an error.
3216 if (!(status
& ME4600_AO_STATUS_BIT_EF
)) { // FIFO is empty.
3217 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3218 if (instance
->stop_data_count
&& (instance
->stop_data_count
<= instance
->data_count
)) { //Finishing work. Requed data shown.
3220 ("ISM stoped. No data in FIFO. Buffer is not empty.\n");
3222 ao_status_stream_end
;
3225 ("Output stream has been broken. ISM stoped. No data in FIFO. Buffer is not empty.\n");
3227 ao_status_stream_buffer_error
;
3229 } else { // Software buffer is empty.
3231 ("ISM stoped. No data in FIFO. Buffer is empty.\n");
3232 instance
->status
= ao_status_stream_end
;
3234 } else { // There are still datas in FIFO.
3235 if (me_circ_buf_values(&instance
->circ_buf
)) { // Software buffer is not empty.
3237 ("Output stream has been broken. ISM stoped but some data in FIFO and buffer.\n");
3238 } else { // Software buffer is empty.
3240 ("Output stream has been broken. ISM stoped but some data in FIFO. Buffer is empty.\n");
3242 instance
->status
= ao_status_stream_fifo_error
;
3246 // Signal the failure.
3254 case ao_status_stream_end_wait
:
3255 if (!instance
->fifo
) {
3257 ("Streaming on single device! This feature is not implemented in this version!\n");
3258 instance
->status
= ao_status_stream_error
;
3264 if (!(status
& ME4600_AO_STATUS_BIT_FSM
)) { // State machine is not working. Waiting for stop finish.
3265 instance
->status
= ao_status_stream_end
;
3268 // State machine is working.
3273 PERROR_CRITICAL("Status is in wrong state (%d)!\n",
3275 instance
->status
= ao_status_stream_error
;
3282 if (signaling
) { //Signal it.
3283 wake_up_interruptible_all(&instance
->wait_queue
);
3286 if (instance
->ao_control_task_flag
&& reschedule
) { // Reschedule task
3287 queue_delayed_work(instance
->me4600_workqueue
,
3288 &instance
->ao_control_task
, 1);
3290 PINFO("<%s> Ending control task.\n", __func__
);
3295 /// @note SPECIAL BUILD FOR BOSCH
3296 /// @author Guenter Gebhardt
3297 static int me4600_ao_io_reset_subdevice(me_subdevice_t
*subdevice
,
3298 struct file
*filep
, int flags
)
3300 me4600_ao_subdevice_t
*instance
;
3301 int err
= ME_ERRNO_SUCCESS
;
3303 unsigned long status
;
3305 PDEBUG("executed.\n");
3307 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3309 ME_SUBDEVICE_ENTER
spin_lock_irqsave(&instance
->subdevice_lock
, status
);
3310 spin_lock(instance
->preload_reg_lock
);
3311 tmp
= inl(instance
->preload_reg
);
3312 tmp
&= ~(0x10001 << instance
->ao_idx
);
3313 outl(tmp
, instance
->preload_reg
);
3314 *instance
->preload_flags
&= ~(0x1 << instance
->ao_idx
);
3315 spin_unlock(instance
->preload_reg_lock
);
3317 tmp
= inl(instance
->ctrl_reg
);
3318 tmp
|= ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3319 outl(tmp
, instance
->ctrl_reg
);
3321 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
3323 outl(ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
,
3324 instance
->ctrl_reg
);
3326 outl(0x8000, instance
->single_reg
);
3328 instance
->single_value
= 0x8000;
3329 instance
->circ_buf
.head
= 0;
3330 instance
->circ_buf
.tail
= 0;
3332 spin_unlock_irqrestore(&instance
->subdevice_lock
, status
);
3339 static int me4600_ao_io_single_config(me_subdevice_t
*subdevice
,
3345 int trig_type
, int trig_edge
, int flags
)
3347 me4600_ao_subdevice_t
*instance
;
3348 int err
= ME_ERRNO_SUCCESS
;
3350 unsigned long cpu_flags
;
3352 PDEBUG("executed.\n");
3354 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3357 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3359 if (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) {
3360 PERROR("Subdevice is busy.\n");
3361 err
= ME_ERRNO_SUBDEVICE_BUSY
;
3366 if (single_config
== 0) {
3367 if (ref
== ME_REF_AO_GROUND
) {
3368 if (trig_chan
== ME_TRIG_CHAN_DEFAULT
) {
3369 if (trig_type
== ME_TRIG_TYPE_SW
) {
3370 tmp
= inl(instance
->ctrl_reg
);
3372 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3373 outl(tmp
, instance
->ctrl_reg
);
3375 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3376 outl(tmp
, instance
->ctrl_reg
);
3378 spin_lock(instance
->
3381 inl(instance
->preload_reg
);
3383 ~(0x10001 << instance
->
3386 instance
->preload_reg
);
3387 *instance
->preload_flags
&=
3388 ~(0x1 << instance
->ao_idx
);
3389 spin_unlock(instance
->
3391 } else if (trig_type
==
3392 ME_TRIG_TYPE_EXT_DIGITAL
) {
3394 ME_TRIG_EDGE_RISING
) {
3399 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3404 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3406 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3410 } else if (trig_edge
==
3411 ME_TRIG_EDGE_FALLING
)
3417 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3422 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3424 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
3426 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3430 } else if (trig_edge
==
3436 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3441 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3443 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
3445 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
3447 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3453 ("Invalid trigger edge.\n");
3455 ME_ERRNO_INVALID_TRIG_EDGE
;
3459 spin_lock(instance
->
3463 inl(instance
->preload_reg
);
3465 ~(0x10001 << instance
->
3467 tmp
|= 0x1 << instance
->ao_idx
;
3469 instance
->preload_reg
);
3470 *instance
->preload_flags
&=
3471 ~(0x1 << instance
->ao_idx
);
3472 spin_unlock(instance
->
3476 ("Invalid trigger type.\n");
3478 ME_ERRNO_INVALID_TRIG_TYPE
;
3481 } else if (trig_chan
==
3482 ME_TRIG_CHAN_SYNCHRONOUS
) {
3483 if (trig_type
== ME_TRIG_TYPE_SW
) {
3484 tmp
= inl(instance
->ctrl_reg
);
3486 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3487 outl(tmp
, instance
->ctrl_reg
);
3489 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3490 outl(tmp
, instance
->ctrl_reg
);
3492 spin_lock(instance
->
3495 inl(instance
->preload_reg
);
3497 ~(0x10001 << instance
->
3499 tmp
|= 0x1 << instance
->ao_idx
;
3501 instance
->preload_reg
);
3502 *instance
->preload_flags
|=
3503 0x1 << instance
->ao_idx
;
3504 spin_unlock(instance
->
3506 } else if (trig_type
==
3507 ME_TRIG_TYPE_EXT_DIGITAL
) {
3509 ME_TRIG_EDGE_RISING
) {
3514 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3519 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3523 } else if (trig_edge
==
3524 ME_TRIG_EDGE_FALLING
)
3530 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3535 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3537 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3541 } else if (trig_edge
==
3547 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3552 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
3554 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
3556 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3562 ("Invalid trigger edge.\n");
3564 ME_ERRNO_INVALID_TRIG_EDGE
;
3568 spin_lock(instance
->
3572 inl(instance
->preload_reg
);
3574 0x10001 << instance
->ao_idx
;
3576 instance
->preload_reg
);
3577 *instance
->preload_flags
&=
3578 ~(0x1 << instance
->ao_idx
);
3579 spin_unlock(instance
->
3583 ("Invalid trigger type.\n");
3585 ME_ERRNO_INVALID_TRIG_TYPE
;
3590 ("Invalid trigger channel specified.\n");
3591 err
= ME_ERRNO_INVALID_REF
;
3595 PERROR("Invalid analog reference specified.\n");
3596 err
= ME_ERRNO_INVALID_REF
;
3600 PERROR("Invalid single config specified.\n");
3601 err
= ME_ERRNO_INVALID_SINGLE_CONFIG
;
3605 PERROR("Invalid channel number specified.\n");
3606 err
= ME_ERRNO_INVALID_CHANNEL
;
3612 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3619 static int me4600_ao_io_single_read(me_subdevice_t
*subdevice
,
3622 int *value
, int time_out
, int flags
)
3624 me4600_ao_subdevice_t
*instance
;
3625 int err
= ME_ERRNO_SUCCESS
;
3627 unsigned long cpu_flags
;
3629 PDEBUG("executed.\n");
3631 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3634 PERROR("Invalid channel number specified.\n");
3635 return ME_ERRNO_INVALID_CHANNEL
;
3639 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3640 tmp
= inl(instance
->ctrl_reg
);
3643 PERROR("Not in single mode.\n");
3644 err
= ME_ERRNO_PREVIOUS_CONFIG
;
3646 *value
= instance
->single_value
;
3649 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3656 static int me4600_ao_io_single_write(me_subdevice_t
*subdevice
,
3659 int value
, int time_out
, int flags
)
3661 me4600_ao_subdevice_t
*instance
;
3662 int err
= ME_ERRNO_SUCCESS
;
3663 unsigned long mask
= 0;
3665 unsigned long cpu_flags
;
3667 wait_queue_head_t queue
;
3669 unsigned long delay
= 0;
3671 PDEBUG("executed.\n");
3673 init_waitqueue_head(&queue
);
3675 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3678 PERROR("Invalid channel number specified.\n");
3679 return ME_ERRNO_INVALID_CHANNEL
;
3683 PERROR("Invalid timeout specified.\n");
3684 return ME_ERRNO_INVALID_TIMEOUT
;
3688 delay
= (time_out
* HZ
) / 1000;
3695 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3697 tmp
= inl(instance
->ctrl_reg
);
3700 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3701 PERROR("Not in single mode.\n");
3702 err
= ME_ERRNO_PREVIOUS_CONFIG
;
3706 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) {
3707 outl(value
, instance
->single_reg
);
3708 instance
->single_value
= value
;
3709 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3711 if (!(flags
& ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
3714 while (inl(instance
->status_reg
) &
3715 ME4600_AO_STATUS_BIT_FSM
) {
3716 interruptible_sleep_on_timeout(&queue
, 1);
3718 if (signal_pending(current
)) {
3720 ("Wait on external trigger interrupted by signal.\n");
3721 err
= ME_ERRNO_SIGNAL
;
3725 if (delay
&& ((jiffies
- j
) > delay
)) {
3726 PERROR("Timeout reached.\n");
3727 err
= ME_ERRNO_TIMEOUT
;
3732 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
))
3733 == (0x10001 << instance
->ao_idx
)) {
3734 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) {
3735 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3736 outl(tmp
, instance
->ctrl_reg
);
3737 outl(value
, instance
->single_reg
);
3738 instance
->single_value
= value
;
3739 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3742 if (!(flags
& ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING
)) {
3745 while (inl(instance
->status_reg
) &
3746 ME4600_AO_STATUS_BIT_FSM
) {
3747 interruptible_sleep_on_timeout(&queue
,
3750 if (signal_pending(current
)) {
3752 ("Wait on external trigger interrupted by signal.\n");
3753 err
= ME_ERRNO_SIGNAL
;
3757 if (delay
&& ((jiffies
- j
) > delay
)) {
3758 PERROR("Timeout reached.\n");
3759 err
= ME_ERRNO_TIMEOUT
;
3765 outl(value
, instance
->single_reg
);
3766 instance
->single_value
= value
;
3767 spin_unlock_irqrestore(&instance
->subdevice_lock
,
3770 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
))
3771 == (0x1 << instance
->ao_idx
)) {
3772 outl(value
, instance
->single_reg
);
3773 instance
->single_value
= value
;
3775 PDEBUG("Synchronous SW, flags = 0x%X.\n", flags
);
3777 if (flags
& ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS
) {
3778 PDEBUG("Trigger synchronous SW.\n");
3779 spin_lock(instance
->preload_reg_lock
);
3780 tmp
= inl(instance
->preload_reg
);
3782 for (i
= 0; i
< ME4600_AO_MAX_SUBDEVICES
; i
++) {
3783 if ((*instance
->preload_flags
& (0x1 << i
))) {
3784 if ((tmp
& (0x10001 << i
)) ==
3793 outl(tmp
, instance
->preload_reg
);
3794 spin_unlock(instance
->preload_reg_lock
);
3797 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3799 outl(value
, instance
->single_reg
);
3800 instance
->single_value
= value
;
3801 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
3811 static int me4600_ao_io_stream_config(me_subdevice_t
*subdevice
,
3813 meIOStreamConfig_t
*config_list
,
3815 meIOStreamTrigger_t
*trigger
,
3816 int fifo_irq_threshold
, int flags
)
3818 me4600_ao_subdevice_t
*instance
;
3819 int err
= ME_ERRNO_SUCCESS
;
3822 unsigned long cpu_flags
;
3823 uint64_t conv_ticks
;
3824 unsigned int conv_start_ticks_low
= trigger
->iConvStartTicksLow
;
3825 unsigned int conv_start_ticks_high
= trigger
->iConvStartTicksHigh
;
3827 PDEBUG("executed.\n");
3829 instance
= (me4600_ao_subdevice_t
*) subdevice
;
3832 (uint64_t) conv_start_ticks_low
+
3833 ((uint64_t) conv_start_ticks_high
<< 32);
3835 if (!instance
->fifo
) {
3836 PERROR("Not a streaming ao.\n");
3837 return ME_ERRNO_NOT_SUPPORTED
;
3841 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
3843 if ((inl(instance
->status_reg
)) & ME4600_AO_STATUS_BIT_FSM
) {
3844 PERROR("Subdevice is busy.\n");
3845 err
= ME_ERRNO_SUBDEVICE_BUSY
;
3849 ctrl
= inl(instance
->ctrl_reg
);
3850 ctrl
|= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3851 outl(ctrl
, instance
->ctrl_reg
);
3852 ctrl
= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
3853 outl(ctrl
, instance
->ctrl_reg
);
3856 PERROR("Invalid stream configuration list count specified.\n");
3857 err
= ME_ERRNO_INVALID_CONFIG_LIST_COUNT
;
3861 if (config_list
[0].iChannel
!= 0) {
3862 PERROR("Invalid channel number specified.\n");
3863 err
= ME_ERRNO_INVALID_CHANNEL
;
3867 if (config_list
[0].iStreamConfig
!= 0) {
3868 PERROR("Invalid stream config specified.\n");
3869 err
= ME_ERRNO_INVALID_STREAM_CONFIG
;
3873 if (config_list
[0].iRef
!= ME_REF_AO_GROUND
) {
3874 PERROR("Invalid analog reference.\n");
3875 err
= ME_ERRNO_INVALID_REF
;
3879 if ((trigger
->iAcqStartTicksLow
!= 0)
3880 || (trigger
->iAcqStartTicksHigh
!= 0)) {
3882 ("Invalid acquisition start trigger argument specified.\n");
3883 err
= ME_ERRNO_INVALID_ACQ_START_ARG
;
3887 switch (trigger
->iAcqStartTrigType
) {
3889 case ME_TRIG_TYPE_SW
:
3892 case ME_TRIG_TYPE_EXT_DIGITAL
:
3893 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
3895 switch (trigger
->iAcqStartTrigEdge
) {
3897 case ME_TRIG_EDGE_RISING
:
3900 case ME_TRIG_EDGE_FALLING
:
3901 ctrl
|= ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
;
3905 case ME_TRIG_EDGE_ANY
:
3907 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE
|
3908 ME4600_AO_CTRL_BIT_EX_TRIG_EDGE_BOTH
;
3914 ("Invalid acquisition start trigger edge specified.\n");
3916 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_EDGE
;
3926 PERROR("Invalid acquisition start trigger type specified.\n");
3928 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_TYPE
;
3935 switch (trigger
->iScanStartTrigType
) {
3937 case ME_TRIG_TYPE_FOLLOW
:
3941 PERROR("Invalid scan start trigger type specified.\n");
3943 err
= ME_ERRNO_INVALID_SCAN_START_TRIG_TYPE
;
3950 switch (trigger
->iConvStartTrigType
) {
3952 case ME_TRIG_TYPE_TIMER
:
3953 if ((conv_ticks
< ME4600_AO_MIN_CHAN_TICKS
)
3954 || (conv_ticks
> ME4600_AO_MAX_CHAN_TICKS
)) {
3956 ("Invalid conv start trigger argument specified.\n");
3957 err
= ME_ERRNO_INVALID_CONV_START_ARG
;
3964 PERROR("Invalid conv start trigger type specified.\n");
3966 err
= ME_ERRNO_INVALID_CONV_START_TRIG_TYPE
;
3973 /* Preset to hardware wraparound mode */
3974 instance
->flags
&= ~(ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
);
3976 switch (trigger
->iScanStopTrigType
) {
3978 case ME_TRIG_TYPE_NONE
:
3979 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
3980 /* Set flags to indicate usage of software mode. */
3981 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_INF
;
3982 instance
->wrap_count
= 0;
3983 instance
->wrap_remaining
= 0;
3988 case ME_TRIG_TYPE_COUNT
:
3989 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
3990 if (trigger
->iScanStopCount
<= 0) {
3991 PERROR("Invalid scan stop count specified.\n");
3992 err
= ME_ERRNO_INVALID_SCAN_STOP_ARG
;
3996 /* Set flags to indicate usage of software mode. */
3997 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
;
3998 instance
->wrap_count
= trigger
->iScanStopCount
;
3999 instance
->wrap_remaining
= trigger
->iScanStopCount
;
4001 PERROR("Invalid scan stop trigger type specified.\n");
4002 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4009 PERROR("Invalid scan stop trigger type specified.\n");
4011 err
= ME_ERRNO_INVALID_SCAN_STOP_TRIG_TYPE
;
4018 switch (trigger
->iAcqStopTrigType
) {
4020 case ME_TRIG_TYPE_NONE
:
4023 case ME_TRIG_TYPE_COUNT
:
4024 if (trigger
->iScanStopTrigType
!= ME_TRIG_TYPE_NONE
) {
4025 PERROR("Invalid acq stop trigger type specified.\n");
4026 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4030 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
4031 if (trigger
->iAcqStopCount
<= 0) {
4032 PERROR("Invalid acq stop count specified.\n");
4033 err
= ME_ERRNO_INVALID_ACQ_STOP_ARG
;
4037 /* Set flags to indicate usage of software mode. */
4038 instance
->flags
|= ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
;
4039 instance
->wrap_count
= trigger
->iAcqStopCount
;
4040 instance
->wrap_remaining
= trigger
->iAcqStopCount
;
4042 PERROR("Invalid acp stop trigger type specified.\n");
4043 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4050 PERROR("Invalid acq stop trigger type specified.\n");
4051 err
= ME_ERRNO_INVALID_ACQ_STOP_TRIG_TYPE
;
4056 switch (trigger
->iAcqStartTrigChan
) {
4058 case ME_TRIG_CHAN_DEFAULT
:
4059 spin_lock(instance
->preload_reg_lock
);
4060 tmp
= inl(instance
->preload_reg
);
4061 tmp
&= ~(0x10001 << instance
->ao_idx
);
4062 outl(tmp
, instance
->preload_reg
);
4063 spin_unlock(instance
->preload_reg_lock
);
4067 case ME_TRIG_CHAN_SYNCHRONOUS
:
4068 if (trigger
->iAcqStartTrigType
== ME_TRIG_TYPE_SW
) {
4069 spin_lock(instance
->preload_reg_lock
);
4070 tmp
= inl(instance
->preload_reg
);
4071 tmp
&= ~(0x10001 << instance
->ao_idx
);
4072 outl(tmp
, instance
->preload_reg
);
4073 tmp
|= 0x1 << instance
->ao_idx
;
4074 outl(tmp
, instance
->preload_reg
);
4075 spin_unlock(instance
->preload_reg_lock
);
4077 ctrl
&= ~(ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
);
4078 spin_lock(instance
->preload_reg_lock
);
4079 tmp
= inl(instance
->preload_reg
);
4080 tmp
&= ~(0x10001 << instance
->ao_idx
);
4081 outl(tmp
, instance
->preload_reg
);
4082 tmp
|= 0x10000 << instance
->ao_idx
;
4083 outl(tmp
, instance
->preload_reg
);
4084 spin_unlock(instance
->preload_reg_lock
);
4090 PERROR("Invalid acq start trigger channel specified.\n");
4091 err
= ME_ERRNO_INVALID_ACQ_START_TRIG_CHAN
;
4097 outl(conv_ticks
- 2, instance
->timer_reg
);
4099 if (flags
& ME_IO_STREAM_CONFIG_BIT_PATTERN
) {
4100 if (instance
->ao_idx
== 3) {
4101 ctrl
|= ME4600_AO_CTRL_BIT_ENABLE_DO
;
4103 err
= ME_ERRNO_INVALID_FLAGS
;
4107 if (instance
->ao_idx
== 3) {
4108 ctrl
&= ~ME4600_AO_CTRL_BIT_ENABLE_DO
;
4112 /* Set hardware mode. */
4113 if (flags
& ME_IO_STREAM_CONFIG_WRAPAROUND
) {
4114 ctrl
|= ME4600_AO_CTRL_BIT_MODE_0
;
4116 ctrl
|= ME4600_AO_CTRL_BIT_MODE_1
;
4119 PDEBUG("Preload word = 0x%X.\n", inl(instance
->preload_reg
));
4121 PDEBUG("Ctrl word = 0x%lX.\n", ctrl
);
4122 outl(ctrl
, instance
->ctrl_reg
); // Write the control word
4126 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4133 static int me4600_ao_io_stream_new_values(me_subdevice_t
*subdevice
,
4135 int time_out
, int *count
, int flags
)
4137 me4600_ao_subdevice_t
*instance
;
4138 int err
= ME_ERRNO_SUCCESS
;
4142 PDEBUG("executed.\n");
4144 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4146 if (!instance
->fifo
) {
4147 PERROR("Not a streaming ao.\n");
4148 return ME_ERRNO_NOT_SUPPORTED
;
4152 PERROR("Invalid time_out specified.\n");
4153 return ME_ERRNO_INVALID_TIMEOUT
;
4157 t
= (time_out
* HZ
) / 1000;
4169 wait_event_interruptible_timeout(instance
->wait_queue
,
4171 (&instance
->circ_buf
))
4172 || !(inl(instance
->status_reg
)
4174 ME4600_AO_STATUS_BIT_FSM
)),
4177 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
4178 PERROR("AO subdevice is not running.\n");
4179 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
4180 } else if (signal_pending(current
)) {
4181 PERROR("Wait on values interrupted from signal.\n");
4182 err
= ME_ERRNO_SIGNAL
;
4183 } else if ((jiffies
- j
) >= t
) {
4184 PERROR("Wait on values timed out.\n");
4185 err
= ME_ERRNO_TIMEOUT
;
4187 *count
= me_circ_buf_space(&instance
->circ_buf
);
4190 wait_event_interruptible(instance
->wait_queue
,
4192 (&instance
->circ_buf
))
4193 || !(inl(instance
->status_reg
) &
4194 ME4600_AO_STATUS_BIT_FSM
)));
4196 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
4197 PERROR("AO subdevice is not running.\n");
4198 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
4199 } else if (signal_pending(current
)) {
4200 PERROR("Wait on values interrupted from signal.\n");
4201 err
= ME_ERRNO_SIGNAL
;
4203 *count
= me_circ_buf_space(&instance
->circ_buf
);
4212 static void stop_immediately(me4600_ao_subdevice_t
*instance
)
4214 unsigned long cpu_flags
;
4217 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4218 tmp
= inl(instance
->ctrl_reg
);
4219 tmp
|= ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4220 outl(tmp
, instance
->ctrl_reg
);
4222 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
4224 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4227 static int me4600_ao_io_stream_start(me_subdevice_t
*subdevice
,
4229 int start_mode
, int time_out
, int flags
)
4231 me4600_ao_subdevice_t
*instance
;
4232 int err
= ME_ERRNO_SUCCESS
;
4233 unsigned long cpu_flags
= 0;
4236 unsigned long delay
= 0;
4237 wait_queue_head_t queue
;
4239 PDEBUG("executed.\n");
4241 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4243 init_waitqueue_head(&queue
);
4246 PERROR("Invalid timeout specified.\n");
4247 return ME_ERRNO_INVALID_TIMEOUT
;
4251 delay
= (time_out
* HZ
) / 1000;
4257 if (!instance
->fifo
) {
4258 PERROR("Not a streaming ao.\n");
4259 return ME_ERRNO_NOT_SUPPORTED
;
4263 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4265 tmp
= inl(instance
->ctrl_reg
);
4267 switch (tmp
& (ME4600_AO_CTRL_MASK_MODE
)) {
4269 case 0: // Single mode
4270 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4271 PERROR("Subdevice is configured in single mode.\n");
4272 err
= ME_ERRNO_PREVIOUS_CONFIG
;
4275 case 1: // Wraparound mode
4276 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) { // Normal wraparound with external trigger
4278 if ((inl(instance
->status_reg
) &
4279 ME4600_AO_STATUS_BIT_FSM
)) {
4280 spin_unlock_irqrestore(&instance
->
4283 PERROR("Conversion is already running.\n");
4284 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4289 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4290 ME4600_AO_CTRL_BIT_STOP
|
4291 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4293 outl(tmp
, instance
->ctrl_reg
);
4294 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4297 if (start_mode
== ME_START_MODE_BLOCKING
) {
4298 init_waitqueue_head(&queue
);
4304 (inl(instance
->status_reg
) &
4305 ME4600_AO_STATUS_BIT_FSM
)) {
4306 interruptible_sleep_on_timeout
4309 if (signal_pending(current
)) {
4311 ("Wait on start of state machine interrupted.\n");
4314 err
= ME_ERRNO_SIGNAL
;
4318 if (((jiffies
- ref
) >= delay
)) {
4320 ("Timeout reached.\n");
4323 err
= ME_ERRNO_TIMEOUT
;
4329 (inl(instance
->status_reg
) &
4330 ME4600_AO_STATUS_BIT_FSM
)) {
4331 interruptible_sleep_on_timeout
4334 if (signal_pending(current
)) {
4336 ("Wait on start of state machine interrupted.\n");
4339 err
= ME_ERRNO_SIGNAL
;
4344 } else if (start_mode
== ME_START_MODE_NONBLOCKING
) {
4346 PERROR("Invalid start mode specified.\n");
4347 err
= ME_ERRNO_INVALID_START_MODE
;
4350 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x10000 << instance
->ao_idx
)) { // Synchronous with external trigger
4352 if ((inl(instance
->status_reg
) &
4353 ME4600_AO_STATUS_BIT_FSM
)) {
4354 spin_unlock_irqrestore(&instance
->
4357 PERROR("Conversion is already running.\n");
4358 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4362 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4363 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
;
4365 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4366 ME4600_AO_CTRL_BIT_STOP
|
4367 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4368 outl(tmp
, instance
->ctrl_reg
);
4369 spin_unlock_irqrestore(&instance
->
4373 if (start_mode
== ME_START_MODE_BLOCKING
) {
4374 init_waitqueue_head(&queue
);
4383 ME4600_AO_STATUS_BIT_FSM
))
4385 interruptible_sleep_on_timeout
4391 ("Wait on start of state machine interrupted.\n");
4399 if (((jiffies
- ref
) >=
4402 ("Timeout reached.\n");
4415 ME4600_AO_STATUS_BIT_FSM
))
4417 interruptible_sleep_on_timeout
4423 ("Wait on start of state machine interrupted.\n");
4432 } else if (start_mode
==
4433 ME_START_MODE_NONBLOCKING
) {
4436 ("Invalid start mode specified.\n");
4437 err
= ME_ERRNO_INVALID_START_MODE
;
4442 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4443 ME4600_AO_CTRL_BIT_STOP
|
4444 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4445 outl(tmp
, instance
->ctrl_reg
);
4446 spin_unlock_irqrestore(&instance
->
4450 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x1 << instance
->ao_idx
)) { // Synchronous wraparound with sw trigger
4452 if ((inl(instance
->status_reg
) &
4453 ME4600_AO_STATUS_BIT_FSM
)) {
4454 spin_unlock_irqrestore(&instance
->
4457 PERROR("Conversion is already running.\n");
4458 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4463 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4464 ME4600_AO_CTRL_BIT_STOP
|
4465 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4467 outl(tmp
, instance
->ctrl_reg
);
4469 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4470 outl(0x8000, instance
->single_reg
);
4471 instance
->single_value
= 0x8000;
4474 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4476 } else { // Software start
4478 if ((inl(instance
->status_reg
) &
4479 ME4600_AO_STATUS_BIT_FSM
)) {
4480 spin_unlock_irqrestore(&instance
->
4483 PERROR("Conversion is already running.\n");
4484 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4489 ~(ME4600_AO_CTRL_BIT_ENABLE_IRQ
|
4490 ME4600_AO_CTRL_BIT_STOP
|
4491 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4493 outl(tmp
, instance
->ctrl_reg
);
4495 outl(0x8000, instance
->single_reg
);
4496 instance
->single_value
= 0x8000;
4498 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4504 case 2: // Continuous mode
4505 if (tmp
& ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
) { // Externally triggered
4507 if ((inl(instance
->status_reg
) &
4508 ME4600_AO_STATUS_BIT_FSM
)) {
4509 spin_unlock_irqrestore(&instance
->
4512 PERROR("Conversion is already running.\n");
4513 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4518 ~(ME4600_AO_CTRL_BIT_STOP
|
4519 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4520 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4521 outl(tmp
, instance
->ctrl_reg
);
4522 instance
->wrap_remaining
= instance
->wrap_count
;
4523 instance
->circ_buf
.tail
= 0;
4524 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4527 if (start_mode
== ME_START_MODE_BLOCKING
) {
4528 init_waitqueue_head(&queue
);
4534 (inl(instance
->status_reg
) &
4535 ME4600_AO_STATUS_BIT_FSM
)) {
4536 interruptible_sleep_on_timeout
4539 if (signal_pending(current
)) {
4541 ("Wait on start of state machine interrupted.\n");
4544 err
= ME_ERRNO_SIGNAL
;
4548 if (((jiffies
- ref
) >= delay
)) {
4550 ("Timeout reached.\n");
4553 err
= ME_ERRNO_TIMEOUT
;
4559 (inl(instance
->status_reg
) &
4560 ME4600_AO_STATUS_BIT_FSM
)) {
4561 interruptible_sleep_on_timeout
4564 if (signal_pending(current
)) {
4566 ("Wait on start of state machine interrupted.\n");
4569 err
= ME_ERRNO_SIGNAL
;
4574 } else if (start_mode
== ME_START_MODE_NONBLOCKING
) {
4577 PERROR("Invalid start mode specified.\n");
4578 stop_immediately(instance
);
4579 err
= ME_ERRNO_INVALID_START_MODE
;
4582 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x10000 << instance
->ao_idx
)) { // Synchronous with external trigger
4584 if ((inl(instance
->status_reg
) &
4585 ME4600_AO_STATUS_BIT_FSM
)) {
4586 spin_unlock_irqrestore(&instance
->
4589 PERROR("Conversion is already running.\n");
4590 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4594 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4596 ME4600_AO_CTRL_BIT_ENABLE_EX_TRIG
|
4597 ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4599 ~(ME4600_AO_CTRL_BIT_STOP
|
4600 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4601 outl(tmp
, instance
->ctrl_reg
);
4602 instance
->wrap_remaining
= instance
->wrap_count
;
4603 instance
->circ_buf
.tail
= 0;
4605 spin_unlock_irqrestore(&instance
->
4609 if (start_mode
== ME_START_MODE_BLOCKING
) {
4610 init_waitqueue_head(&queue
);
4619 ME4600_AO_STATUS_BIT_FSM
))
4621 interruptible_sleep_on_timeout
4627 ("Wait on start of state machine interrupted.\n");
4635 if (((jiffies
- ref
) >=
4638 ("Timeout reached.\n");
4651 ME4600_AO_STATUS_BIT_FSM
))
4653 interruptible_sleep_on_timeout
4659 ("Wait on start of state machine interrupted.\n");
4668 } else if (start_mode
==
4669 ME_START_MODE_NONBLOCKING
) {
4672 ("Invalid start mode specified.\n");
4673 stop_immediately(instance
);
4674 err
= ME_ERRNO_INVALID_START_MODE
;
4678 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4680 ~(ME4600_AO_CTRL_BIT_STOP
|
4681 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4682 outl(tmp
, instance
->ctrl_reg
);
4683 instance
->wrap_remaining
= instance
->wrap_count
;
4684 instance
->circ_buf
.tail
= 0;
4685 spin_unlock_irqrestore(&instance
->
4689 } else if ((inl(instance
->preload_reg
) & (0x10001 << instance
->ao_idx
)) == (0x1 << instance
->ao_idx
)) { // Synchronous wraparound with sw trigger
4691 if ((inl(instance
->status_reg
) &
4692 ME4600_AO_STATUS_BIT_FSM
)) {
4693 spin_unlock_irqrestore(&instance
->
4696 PERROR("Conversion is already running.\n");
4697 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4702 ~(ME4600_AO_CTRL_BIT_STOP
|
4703 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4704 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4705 instance
->wrap_remaining
= instance
->wrap_count
;
4706 instance
->circ_buf
.tail
= 0;
4707 PDEBUG("CTRL Reg = 0x%X.\n", inl(instance
->ctrl_reg
));
4708 outl(tmp
, instance
->ctrl_reg
);
4710 if (flags
& ME_IO_STREAM_START_TYPE_TRIG_SYNCHRONOUS
) {
4711 outl(0x8000, instance
->single_reg
);
4712 instance
->single_value
= 0x8000;
4715 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4717 } else { // Software start
4719 if ((inl(instance
->status_reg
) &
4720 ME4600_AO_STATUS_BIT_FSM
)) {
4721 spin_unlock_irqrestore(&instance
->
4724 PERROR("Conversion is already running.\n");
4725 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4730 ~(ME4600_AO_CTRL_BIT_STOP
|
4731 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
);
4733 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
4734 outl(tmp
, instance
->ctrl_reg
);
4735 outl(0x8000, instance
->single_reg
);
4736 instance
->single_value
= 0x8000;
4737 instance
->wrap_remaining
= instance
->wrap_count
;
4738 instance
->circ_buf
.tail
= 0;
4739 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4746 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4747 PERROR("Invalid mode configured.\n");
4748 err
= ME_ERRNO_INTERNAL
;
4759 static int me4600_ao_io_stream_status(me_subdevice_t
*subdevice
,
4762 int *status
, int *values
, int flags
)
4764 me4600_ao_subdevice_t
*instance
;
4765 int err
= ME_ERRNO_SUCCESS
;
4766 wait_queue_head_t queue
;
4768 PDEBUG("executed.\n");
4770 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4772 init_waitqueue_head(&queue
);
4774 if (!instance
->fifo
) {
4775 PERROR("Not a streaming ao.\n");
4776 return ME_ERRNO_NOT_SUPPORTED
;
4781 if (wait
== ME_WAIT_NONE
) {
4783 (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ?
4784 ME_STATUS_BUSY
: ME_STATUS_IDLE
;
4785 *values
= me_circ_buf_space(&instance
->circ_buf
);
4786 } else if (wait
== ME_WAIT_IDLE
) {
4787 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) {
4788 interruptible_sleep_on_timeout(&queue
, 1);
4790 if (instance
->flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
4791 PERROR("Output stream was interrupted.\n");
4792 *status
= ME_STATUS_ERROR
;
4793 err
= ME_ERRNO_SUCCESS
;
4797 if (signal_pending(current
)) {
4799 ("Wait on state machine interrupted by signal.\n");
4800 *status
= ME_STATUS_INVALID
;
4801 err
= ME_ERRNO_SIGNAL
;
4806 *status
= ME_STATUS_IDLE
;
4808 *values
= me_circ_buf_space(&instance
->circ_buf
);
4810 PERROR("Invalid wait argument specified.\n");
4811 *status
= ME_STATUS_INVALID
;
4812 err
= ME_ERRNO_INVALID_WAIT
;
4823 static int me4600_ao_io_stream_stop(me_subdevice_t
*subdevice
,
4825 int stop_mode
, int flags
)
4827 int err
= ME_ERRNO_SUCCESS
;
4828 me4600_ao_subdevice_t
*instance
;
4829 unsigned long cpu_flags
;
4832 PDEBUG("executed.\n");
4834 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4836 if (!instance
->fifo
) {
4837 PERROR("Not a streaming ao.\n");
4838 return ME_ERRNO_NOT_SUPPORTED
;
4843 if (stop_mode
== ME_STOP_MODE_IMMEDIATE
) {
4844 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4845 tmp
= inl(instance
->ctrl_reg
);
4847 ME4600_AO_CTRL_BIT_STOP
| ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4848 outl(tmp
, instance
->ctrl_reg
);
4850 while (inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
) ;
4852 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4853 } else if (stop_mode
== ME_STOP_MODE_LAST_VALUE
) {
4854 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4855 tmp
= inl(instance
->ctrl_reg
);
4856 tmp
|= ME4600_AO_CTRL_BIT_STOP
;
4857 outl(tmp
, instance
->ctrl_reg
);
4858 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
4860 PERROR("Invalid stop mode specified.\n");
4861 err
= ME_ERRNO_INVALID_STOP_MODE
;
4872 static int me4600_ao_io_stream_write(me_subdevice_t
*subdevice
,
4875 int *values
, int *count
, int flags
)
4877 int err
= ME_ERRNO_SUCCESS
;
4878 me4600_ao_subdevice_t
*instance
;
4886 unsigned long cpu_flags
= 0;
4888 PDEBUG("executed.\n");
4890 instance
= (me4600_ao_subdevice_t
*) subdevice
;
4892 if (!instance
->fifo
) {
4893 PERROR("Not a streaming ao.\n");
4894 return ME_ERRNO_NOT_SUPPORTED
;
4900 PERROR("Invalid count of values specified.\n");
4901 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
4905 spin_lock_irqsave(&instance
->subdevice_lock
, cpu_flags
);
4907 tmp
= inl(instance
->ctrl_reg
);
4909 switch (tmp
& 0x3) {
4911 case 1: // Wraparound mode
4912 if (instance
->bosch_fw
) { // Bosch firmware
4913 spin_unlock_irqrestore(&instance
->subdevice_lock
,
4918 ("Invalid count of values specified. 7 expected.\n");
4919 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
4923 for (i
= 0; i
< 7; i
++) {
4924 if (get_user(value
, values
)) {
4926 ("Can't copy value from user space.\n");
4927 err
= ME_ERRNO_INTERNAL
;
4932 /* Maximum voltage */
4935 inl(instance
->reg_base
+
4937 outl(value
, instance
->reg_base
+ 0xD4);
4938 } else if (i
== 1) {
4939 /* Minimum voltage */
4942 inl(instance
->reg_base
+
4944 outl(value
, instance
->reg_base
+ 0xD4);
4945 } else if (i
== 2) {
4949 inl(instance
->reg_base
+
4951 outl(value
, instance
->reg_base
+ 0xD8);
4952 } else if (i
== 3) {
4956 inl(instance
->reg_base
+
4958 outl(value
, instance
->reg_base
+ 0xD8);
4959 } else if (i
== 4) {
4961 outl(value
, instance
->reg_base
+ 0xDC);
4962 } else if (i
== 5) {
4965 value
= inl(instance
->ctrl_reg
);
4967 outl(value
, instance
->ctrl_reg
);
4969 value
= inl(instance
->ctrl_reg
);
4971 outl(value
, instance
->ctrl_reg
);
4973 } else if (i
== 6) {
4974 /* Timer for positive ramp */
4975 outl(value
, instance
->reg_base
+ 0xE0);
4980 } else { // Normal firmware
4981 PDEBUG("Write for wraparound mode.\n");
4983 if (inl(instance
->status_reg
) &
4984 ME4600_AO_STATUS_BIT_FSM
) {
4985 spin_unlock_irqrestore(&instance
->
4989 ("There is already a conversion running.\n");
4990 err
= ME_ERRNO_SUBDEVICE_BUSY
;
4994 tmp
|= ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
4995 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
4996 outl(tmp
, instance
->ctrl_reg
);
4997 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
4999 if ((*count
> ME4600_AO_FIFO_COUNT
) ||
5001 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5002 ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
)) {
5004 ~(ME4600_AO_CTRL_BIT_MODE_0
|
5005 ME4600_AO_CTRL_BIT_MODE_1
);
5006 tmp
|= ME4600_AO_CTRL_BIT_MODE_1
;
5009 outl(tmp
, instance
->ctrl_reg
);
5010 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5013 if ((*count
<= ME4600_AO_FIFO_COUNT
) &&
5015 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5016 ME4600_AO_FLAGS_SW_WRAP_MODE_INF
)) {
5017 for (i
= 0; i
< *count
; i
++) {
5018 if (get_user(value
, values
+ i
)) {
5020 ("Cannot copy value from user space.\n");
5021 err
= ME_ERRNO_INTERNAL
;
5025 if (instance
->ao_idx
& 0x1)
5028 outl(value
, instance
->fifo_reg
);
5030 } else if ((*count
<= ME4600_AO_CIRC_BUF_COUNT
) &&
5032 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
)
5033 == ME4600_AO_FLAGS_SW_WRAP_MODE_INF
)) {
5034 for (i
= 0; i
< *count
; i
++) {
5035 if (get_user(value
, values
+ i
)) {
5037 ("Cannot copy value from user space.\n");
5038 err
= ME_ERRNO_INTERNAL
;
5042 instance
->circ_buf
.buf
[i
] = value
; /* Used to hold the values. */
5045 instance
->circ_buf
.tail
= 0; /* Used as the current read position. */
5046 instance
->circ_buf
.head
= *count
; /* Used as the buffer size. */
5048 /* Preload the FIFO. */
5050 for (i
= 0; i
< ME4600_AO_FIFO_COUNT
;
5051 i
++, instance
->circ_buf
.tail
++) {
5052 if (instance
->circ_buf
.tail
>=
5053 instance
->circ_buf
.head
)
5054 instance
->circ_buf
.tail
= 0;
5056 if (instance
->ao_idx
& 0x1)
5057 outl(instance
->circ_buf
.
5058 buf
[instance
->circ_buf
.
5060 instance
->fifo_reg
);
5062 outl(instance
->circ_buf
.
5063 buf
[instance
->circ_buf
.
5065 instance
->fifo_reg
);
5067 } else if ((*count
<= ME4600_AO_CIRC_BUF_COUNT
) &&
5069 flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
)
5070 == ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
)) {
5071 unsigned int preload_count
;
5073 for (i
= 0; i
< *count
; i
++) {
5074 if (get_user(value
, values
+ i
)) {
5076 ("Cannot copy value from user space.\n");
5077 err
= ME_ERRNO_INTERNAL
;
5081 instance
->circ_buf
.buf
[i
] = value
; /* Used to hold the values. */
5084 instance
->circ_buf
.tail
= 0; /* Used as the current read position. */
5085 instance
->circ_buf
.head
= *count
; /* Used as the buffer size. */
5087 /* Try to preload the whole FIFO. */
5088 preload_count
= ME4600_AO_FIFO_COUNT
;
5090 if (preload_count
> instance
->wrap_count
)
5091 preload_count
= instance
->wrap_count
;
5093 /* Preload the FIFO. */
5094 for (i
= 0; i
< preload_count
;
5095 i
++, instance
->circ_buf
.tail
++) {
5096 if (instance
->circ_buf
.tail
>=
5097 instance
->circ_buf
.head
)
5098 instance
->circ_buf
.tail
= 0;
5100 if (instance
->ao_idx
& 0x1)
5101 outl(instance
->circ_buf
.
5102 buf
[instance
->circ_buf
.
5104 instance
->fifo_reg
);
5106 outl(instance
->circ_buf
.
5107 buf
[instance
->circ_buf
.
5109 instance
->fifo_reg
);
5112 instance
->wrap_remaining
=
5113 instance
->wrap_count
- preload_count
;
5115 PERROR("To many values written.\n");
5116 err
= ME_ERRNO_INVALID_VALUE_COUNT
;
5123 case 2: // Continuous mode
5124 /* Check if in SW wrapround mode */
5125 if (instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) {
5126 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5128 PERROR("Subdevice is configured SW wrapround mode.\n");
5129 err
= ME_ERRNO_PREVIOUS_CONFIG
;
5133 switch (write_mode
) {
5135 case ME_WRITE_MODE_BLOCKING
:
5136 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5139 PDEBUG("Write for blocking continuous mode.\n");
5142 wait_event_interruptible(instance
->wait_queue
,
5144 me_circ_buf_space_to_end
5149 flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
5151 ("Broken pipe in blocking write.\n");
5152 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
5154 } else if (signal_pending(current
)) {
5156 ("Wait for free buffer interrupted from signal.\n");
5157 err
= ME_ERRNO_SIGNAL
;
5161 PDEBUG("Space to end = %d.\n", c
);
5163 /* Only able to write size of free buffer or size of count */
5167 k
= sizeof(int) * c
;
5168 k
-= copy_from_user(instance
->circ_buf
.buf
+
5169 instance
->circ_buf
.head
,
5171 c
= k
/ sizeof(int);
5173 PDEBUG("Copy %d values from user space.\n", c
);
5177 ("Cannot copy values from user space.\n");
5178 err
= ME_ERRNO_INTERNAL
;
5182 instance
->circ_buf
.head
=
5183 (instance
->circ_buf
.head
+
5184 c
) & (instance
->circ_buf
.mask
);
5190 /* Values are now available so enable interrupts */
5191 spin_lock_irqsave(&instance
->subdevice_lock
,
5194 if (me_circ_buf_space(&instance
->circ_buf
)) {
5195 tmp
= inl(instance
->ctrl_reg
);
5196 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5197 outl(tmp
, instance
->ctrl_reg
);
5200 spin_unlock_irqrestore(&instance
->
5209 case ME_WRITE_MODE_NONBLOCKING
:
5210 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5213 PDEBUG("Write for non blocking continuous mode.\n");
5217 flags
& ME4600_AO_FLAGS_BROKEN_PIPE
) {
5219 ("ME4600:Broken pipe in nonblocking write.\n");
5220 err
= ME_ERRNO_SUBDEVICE_NOT_RUNNING
;
5224 c
= me_circ_buf_space_to_end(&instance
->
5229 ("Returning from nonblocking write.\n");
5233 PDEBUG("Space to end = %d.\n", c
);
5235 /* Only able to write size of free buffer or size of count */
5239 k
= sizeof(int) * c
;
5240 k
-= copy_from_user(instance
->circ_buf
.buf
+
5241 instance
->circ_buf
.head
,
5243 c
= k
/ sizeof(int);
5245 PDEBUG("Copy %d values from user space.\n", c
);
5249 ("Cannot copy values from user space.\n");
5250 err
= ME_ERRNO_INTERNAL
;
5254 instance
->circ_buf
.head
=
5255 (instance
->circ_buf
.head
+
5256 c
) & (instance
->circ_buf
.mask
);
5262 /* Values are now available so enable interrupts */
5263 spin_lock_irqsave(&instance
->subdevice_lock
,
5266 if (me_circ_buf_space(&instance
->circ_buf
)) {
5267 tmp
= inl(instance
->ctrl_reg
);
5268 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5269 outl(tmp
, instance
->ctrl_reg
);
5272 spin_unlock_irqrestore(&instance
->
5281 case ME_WRITE_MODE_PRELOAD
:
5282 PDEBUG("Write for preload continuous mode.\n");
5284 if ((inl(instance
->status_reg
) &
5285 ME4600_AO_STATUS_BIT_FSM
)) {
5286 spin_unlock_irqrestore(&instance
->
5290 ("Can't Preload DAC FIFO while conversion is running.\n");
5291 err
= ME_ERRNO_SUBDEVICE_BUSY
;
5295 tmp
= inl(instance
->ctrl_reg
);
5298 ME4600_AO_CTRL_BIT_STOP
|
5299 ME4600_AO_CTRL_BIT_IMMEDIATE_STOP
;
5300 outl(tmp
, instance
->ctrl_reg
);
5302 ~(ME4600_AO_CTRL_BIT_ENABLE_FIFO
|
5303 ME4600_AO_CTRL_BIT_ENABLE_IRQ
);
5304 outl(tmp
, instance
->ctrl_reg
);
5305 tmp
|= ME4600_AO_CTRL_BIT_ENABLE_FIFO
;
5306 outl(tmp
, instance
->ctrl_reg
);
5308 instance
->circ_buf
.head
= 0;
5309 instance
->circ_buf
.tail
= 0;
5310 instance
->flags
&= ~ME4600_AO_FLAGS_BROKEN_PIPE
;
5312 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5315 c
= ME4600_AO_FIFO_COUNT
;
5320 for (i
= 0; i
< c
; i
++) {
5321 if (get_user(value
, values
)) {
5323 ("Can't copy value from user space.\n");
5324 err
= ME_ERRNO_INTERNAL
;
5328 if (instance
->ao_idx
& 0x1)
5331 outl(value
, instance
->fifo_reg
);
5340 PDEBUG("Wrote %d values to fifo.\n", c
);
5343 c
= me_circ_buf_space_to_end(&instance
->
5355 k
= sizeof(int) * c
;
5357 k
-= copy_from_user(instance
->circ_buf
.buf
+
5358 instance
->circ_buf
.head
,
5361 c
= k
/ sizeof(int);
5363 PDEBUG("Wrote %d values to circular buffer.\n",
5368 ("Can't copy values from user space.\n");
5369 err
= ME_ERRNO_INTERNAL
;
5373 instance
->circ_buf
.head
=
5374 (instance
->circ_buf
.head
+
5375 c
) & (instance
->circ_buf
.mask
);
5387 spin_unlock_irqrestore(&instance
->subdevice_lock
,
5390 PERROR("Invalid write mode specified.\n");
5392 err
= ME_ERRNO_INVALID_WRITE_MODE
;
5399 default: // Single mode of invalid
5400 spin_unlock_irqrestore(&instance
->subdevice_lock
, cpu_flags
);
5401 PERROR("Subdevice is configured in single mode.\n");
5402 err
= ME_ERRNO_PREVIOUS_CONFIG
;
5413 static irqreturn_t
me4600_ao_isr(int irq
, void *dev_id
)
5417 me4600_ao_subdevice_t
*instance
= dev_id
;
5422 if (irq
!= instance
->irq
) {
5423 PDEBUG("Incorrect interrupt num: %d.\n", irq
);
5427 if (!((0x1 << (instance
->ao_idx
+ 3)) & inl(instance
->irq_status_reg
))) {
5431 PDEBUG("executed.\n");
5433 tmp
= inl(instance
->status_reg
);
5435 if (!(tmp
& ME4600_AO_STATUS_BIT_EF
) &&
5436 (tmp
& ME4600_AO_STATUS_BIT_HF
) &&
5437 (tmp
& ME4600_AO_STATUS_BIT_HF
)) {
5438 c
= ME4600_AO_FIFO_COUNT
;
5439 PDEBUG("Fifo empty.\n");
5440 } else if ((tmp
& ME4600_AO_STATUS_BIT_EF
) &&
5441 (tmp
& ME4600_AO_STATUS_BIT_HF
) &&
5442 (tmp
& ME4600_AO_STATUS_BIT_HF
)) {
5443 c
= ME4600_AO_FIFO_COUNT
/ 2;
5444 PDEBUG("Fifo under half full.\n");
5447 PDEBUG("Fifo full.\n");
5450 PDEBUG("Try to write 0x%04X values.\n", c
);
5452 if ((instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5453 ME4600_AO_FLAGS_SW_WRAP_MODE_INF
) {
5457 if (c1
> (instance
->circ_buf
.head
- instance
->circ_buf
.tail
)) /* Only up to the end of the buffer */
5458 c1
= (instance
->circ_buf
.head
-
5459 instance
->circ_buf
.tail
);
5461 /* Write the values to the FIFO */
5462 for (i
= 0; i
< c1
; i
++, instance
->circ_buf
.tail
++, c
--) {
5463 if (instance
->ao_idx
& 0x1)
5464 outl(instance
->circ_buf
.
5465 buf
[instance
->circ_buf
.tail
] << 16,
5466 instance
->fifo_reg
);
5468 outl(instance
->circ_buf
.
5469 buf
[instance
->circ_buf
.tail
],
5470 instance
->fifo_reg
);
5473 if (instance
->circ_buf
.tail
>= instance
->circ_buf
.head
) /* Start from beginning */
5474 instance
->circ_buf
.tail
= 0;
5477 spin_lock(&instance
->subdevice_lock
);
5479 tmp
= inl(instance
->ctrl_reg
);
5480 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5481 outl(tmp
, instance
->ctrl_reg
);
5482 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5483 outl(tmp
, instance
->ctrl_reg
);
5485 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5486 PERROR("Broken pipe.\n");
5487 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5488 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5489 outl(tmp
, instance
->ctrl_reg
);
5492 spin_unlock(&instance
->subdevice_lock
);
5493 } else if ((instance
->flags
& ME4600_AO_FLAGS_SW_WRAP_MODE_MASK
) ==
5494 ME4600_AO_FLAGS_SW_WRAP_MODE_FIN
) {
5495 while (c
&& instance
->wrap_remaining
) {
5498 if (c1
> (instance
->circ_buf
.head
- instance
->circ_buf
.tail
)) /* Only up to the end of the buffer */
5499 c1
= (instance
->circ_buf
.head
-
5500 instance
->circ_buf
.tail
);
5502 if (c1
> instance
->wrap_remaining
) /* Only up to count of user defined number of values */
5503 c1
= instance
->wrap_remaining
;
5505 /* Write the values to the FIFO */
5507 i
++, instance
->circ_buf
.tail
++, c
--,
5508 instance
->wrap_remaining
--) {
5509 if (instance
->ao_idx
& 0x1)
5510 outl(instance
->circ_buf
.
5511 buf
[instance
->circ_buf
.tail
] << 16,
5512 instance
->fifo_reg
);
5514 outl(instance
->circ_buf
.
5515 buf
[instance
->circ_buf
.tail
],
5516 instance
->fifo_reg
);
5519 if (instance
->circ_buf
.tail
>= instance
->circ_buf
.head
) /* Start from beginning */
5520 instance
->circ_buf
.tail
= 0;
5523 spin_lock(&instance
->subdevice_lock
);
5525 tmp
= inl(instance
->ctrl_reg
);
5527 if (!instance
->wrap_remaining
) {
5528 PDEBUG("Finite SW wraparound done.\n");
5529 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5532 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5534 outl(tmp
, instance
->ctrl_reg
);
5535 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5536 outl(tmp
, instance
->ctrl_reg
);
5538 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5539 PERROR("Broken pipe.\n");
5540 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5541 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5542 outl(tmp
, instance
->ctrl_reg
);
5545 spin_unlock(&instance
->subdevice_lock
);
5547 } else { /* Regular continuous mode */
5550 c1
= me_circ_buf_values_to_end(&instance
->circ_buf
);
5551 PDEBUG("Values to end = %d.\n", c1
);
5557 PDEBUG("Work done or buffer empty.\n");
5561 if (instance
->ao_idx
& 0x1) {
5562 for (i
= 0; i
< c1
; i
++) {
5564 *(instance
->circ_buf
.buf
+
5565 instance
->circ_buf
.tail
+
5567 outl(value
, instance
->fifo_reg
);
5570 outsl(instance
->fifo_reg
,
5571 instance
->circ_buf
.buf
+
5572 instance
->circ_buf
.tail
, c1
);
5574 instance
->circ_buf
.tail
=
5575 (instance
->circ_buf
.tail
+
5576 c1
) & (instance
->circ_buf
.mask
);
5578 PDEBUG("%d values wrote to port 0x%04X.\n", c1
,
5579 instance
->fifo_reg
);
5584 spin_lock(&instance
->subdevice_lock
);
5586 tmp
= inl(instance
->ctrl_reg
);
5588 if (!me_circ_buf_values(&instance
->circ_buf
)) {
5590 ("Disable Interrupt because no values left in buffer.\n");
5591 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5594 tmp
|= ME4600_AO_CTRL_BIT_RESET_IRQ
;
5596 outl(tmp
, instance
->ctrl_reg
);
5597 tmp
&= ~ME4600_AO_CTRL_BIT_RESET_IRQ
;
5598 outl(tmp
, instance
->ctrl_reg
);
5600 if (!(inl(instance
->status_reg
) & ME4600_AO_STATUS_BIT_FSM
)) {
5601 PDEBUG("Broken pipe in me4600_ao_isr.\n");
5602 instance
->flags
|= ME4600_AO_FLAGS_BROKEN_PIPE
;
5603 tmp
&= ~ME4600_AO_CTRL_BIT_ENABLE_IRQ
;
5604 outl(tmp
, instance
->ctrl_reg
);
5607 spin_unlock(&instance
->subdevice_lock
);
5609 wake_up_interruptible(&instance
->wait_queue
);
5615 static void me4600_ao_destructor(struct me_subdevice
*subdevice
)
5617 me4600_ao_subdevice_t
*instance
;
5619 PDEBUG("executed.\n");
5621 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5623 free_irq(instance
->irq
, instance
);
5624 kfree(instance
->circ_buf
.buf
);
5625 me_subdevice_deinit(&instance
->base
);
5629 me4600_ao_subdevice_t
*me4600_ao_constructor(uint32_t reg_base
,
5630 spinlock_t
*preload_reg_lock
,
5631 uint32_t *preload_flags
,
5632 int ao_idx
, int fifo
, int irq
)
5634 me4600_ao_subdevice_t
*subdevice
;
5637 PDEBUG("executed.\n");
5639 /* Allocate memory for subdevice instance */
5640 subdevice
= kmalloc(sizeof(me4600_ao_subdevice_t
), GFP_KERNEL
);
5643 PERROR("Cannot get memory for subdevice instance.\n");
5647 memset(subdevice
, 0, sizeof(me4600_ao_subdevice_t
));
5649 /* Initialize subdevice base class */
5650 err
= me_subdevice_init(&subdevice
->base
);
5653 PERROR("Cannot initialize subdevice base class instance.\n");
5657 // Initialize spin locks.
5658 spin_lock_init(&subdevice
->subdevice_lock
);
5660 subdevice
->preload_reg_lock
= preload_reg_lock
;
5661 subdevice
->preload_flags
= preload_flags
;
5663 /* Allocate and initialize circular buffer */
5664 subdevice
->circ_buf
.mask
= ME4600_AO_CIRC_BUF_COUNT
- 1;
5665 subdevice
->circ_buf
.buf
= kmalloc(ME4600_AO_CIRC_BUF_SIZE
, GFP_KERNEL
);
5667 if (!subdevice
->circ_buf
.buf
) {
5668 PERROR("Cannot initialize subdevice base class instance.\n");
5669 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
5674 memset(subdevice
->circ_buf
.buf
, 0, ME4600_AO_CIRC_BUF_SIZE
);
5676 subdevice
->circ_buf
.head
= 0;
5677 subdevice
->circ_buf
.tail
= 0;
5679 /* Initialize wait queue */
5680 init_waitqueue_head(&subdevice
->wait_queue
);
5682 /* Initialize single value to 0V */
5683 subdevice
->single_value
= 0x8000;
5685 /* Store analog output index */
5686 subdevice
->ao_idx
= ao_idx
;
5688 /* Store if analog output has fifo */
5689 subdevice
->fifo
= fifo
;
5691 /* Initialize registers */
5694 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_00_CTRL_REG
;
5695 subdevice
->status_reg
= reg_base
+ ME4600_AO_00_STATUS_REG
;
5696 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_00_FIFO_REG
;
5697 subdevice
->single_reg
= reg_base
+ ME4600_AO_00_SINGLE_REG
;
5698 subdevice
->timer_reg
= reg_base
+ ME4600_AO_00_TIMER_REG
;
5699 subdevice
->reg_base
= reg_base
;
5701 if (inl(subdevice
->reg_base
+ ME4600_AO_BOSCH_REG
) == 0x20000) {
5702 PINFO("Bosch firmware in use for channel 0.\n");
5703 subdevice
->bosch_fw
= 1;
5705 subdevice
->bosch_fw
= 0;
5707 } else if (ao_idx
== 1) {
5708 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_01_CTRL_REG
;
5709 subdevice
->status_reg
= reg_base
+ ME4600_AO_01_STATUS_REG
;
5710 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_01_FIFO_REG
;
5711 subdevice
->single_reg
= reg_base
+ ME4600_AO_01_SINGLE_REG
;
5712 subdevice
->timer_reg
= reg_base
+ ME4600_AO_01_TIMER_REG
;
5713 subdevice
->reg_base
= reg_base
;
5714 subdevice
->bosch_fw
= 0;
5715 } else if (ao_idx
== 2) {
5716 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_02_CTRL_REG
;
5717 subdevice
->status_reg
= reg_base
+ ME4600_AO_02_STATUS_REG
;
5718 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_02_FIFO_REG
;
5719 subdevice
->single_reg
= reg_base
+ ME4600_AO_02_SINGLE_REG
;
5720 subdevice
->timer_reg
= reg_base
+ ME4600_AO_02_TIMER_REG
;
5721 subdevice
->reg_base
= reg_base
;
5722 subdevice
->bosch_fw
= 0;
5724 subdevice
->ctrl_reg
= reg_base
+ ME4600_AO_03_CTRL_REG
;
5725 subdevice
->status_reg
= reg_base
+ ME4600_AO_03_STATUS_REG
;
5726 subdevice
->fifo_reg
= reg_base
+ ME4600_AO_03_FIFO_REG
;
5727 subdevice
->single_reg
= reg_base
+ ME4600_AO_03_SINGLE_REG
;
5728 subdevice
->timer_reg
= reg_base
+ ME4600_AO_03_TIMER_REG
;
5729 subdevice
->reg_base
= reg_base
;
5730 subdevice
->bosch_fw
= 0;
5733 subdevice
->irq_status_reg
= reg_base
+ ME4600_IRQ_STATUS_REG
;
5734 subdevice
->preload_reg
= reg_base
+ ME4600_AO_LOADSETREG_XX
;
5736 /* Register interrupt service routine */
5737 subdevice
->irq
= irq
;
5740 (subdevice
->irq
, me4600_ao_isr
, SA_INTERRUPT
| SA_SHIRQ
,
5741 ME4600_NAME
, subdevice
)) {
5742 PERROR("Cannot get interrupt line.\n");
5743 me_subdevice_deinit((me_subdevice_t
*) subdevice
);
5744 kfree(subdevice
->circ_buf
.buf
);
5749 /* Override base class methods. */
5750 subdevice
->base
.me_subdevice_destructor
= me4600_ao_destructor
;
5751 subdevice
->base
.me_subdevice_io_reset_subdevice
=
5752 me4600_ao_io_reset_subdevice
;
5753 subdevice
->base
.me_subdevice_io_single_config
=
5754 me4600_ao_io_single_config
;
5755 subdevice
->base
.me_subdevice_io_single_read
= me4600_ao_io_single_read
;
5756 subdevice
->base
.me_subdevice_io_single_write
=
5757 me4600_ao_io_single_write
;
5758 subdevice
->base
.me_subdevice_io_stream_config
=
5759 me4600_ao_io_stream_config
;
5760 subdevice
->base
.me_subdevice_io_stream_new_values
=
5761 me4600_ao_io_stream_new_values
;
5762 subdevice
->base
.me_subdevice_io_stream_write
=
5763 me4600_ao_io_stream_write
;
5764 subdevice
->base
.me_subdevice_io_stream_start
=
5765 me4600_ao_io_stream_start
;
5766 subdevice
->base
.me_subdevice_io_stream_status
=
5767 me4600_ao_io_stream_status
;
5768 subdevice
->base
.me_subdevice_io_stream_stop
= me4600_ao_io_stream_stop
;
5769 subdevice
->base
.me_subdevice_query_number_channels
=
5770 me4600_ao_query_number_channels
;
5771 subdevice
->base
.me_subdevice_query_subdevice_type
=
5772 me4600_ao_query_subdevice_type
;
5773 subdevice
->base
.me_subdevice_query_subdevice_caps
=
5774 me4600_ao_query_subdevice_caps
;
5775 subdevice
->base
.me_subdevice_query_subdevice_caps_args
=
5776 me4600_ao_query_subdevice_caps_args
;
5777 subdevice
->base
.me_subdevice_query_range_by_min_max
=
5778 me4600_ao_query_range_by_min_max
;
5779 subdevice
->base
.me_subdevice_query_number_ranges
=
5780 me4600_ao_query_number_ranges
;
5781 subdevice
->base
.me_subdevice_query_range_info
=
5782 me4600_ao_query_range_info
;
5783 subdevice
->base
.me_subdevice_query_timer
= me4600_ao_query_timer
;
5793 static int me4600_ao_query_range_by_min_max(me_subdevice_t
*subdevice
,
5796 int *max
, int *maxdata
, int *range
)
5798 me4600_ao_subdevice_t
*instance
;
5800 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5802 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5804 if ((*max
- *min
) < 0) {
5805 PERROR("Invalid minimum and maximum values specified.\n");
5806 return ME_ERRNO_INVALID_MIN_MAX
;
5809 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
5810 if ((*max
<= (ME4600_AO_MAX_RANGE
+ 1000))
5811 && (*min
>= ME4600_AO_MIN_RANGE
)) {
5812 *min
= ME4600_AO_MIN_RANGE
;
5813 *max
= ME4600_AO_MAX_RANGE
;
5814 *maxdata
= ME4600_AO_MAX_DATA
;
5817 PERROR("No matching range available.\n");
5818 return ME_ERRNO_NO_RANGE
;
5821 PERROR("Invalid physical unit specified.\n");
5822 return ME_ERRNO_INVALID_UNIT
;
5825 return ME_ERRNO_SUCCESS
;
5828 static int me4600_ao_query_number_ranges(me_subdevice_t
*subdevice
,
5829 int unit
, int *count
)
5831 me4600_ao_subdevice_t
*instance
;
5833 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5835 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5837 if ((unit
== ME_UNIT_VOLT
) || (unit
== ME_UNIT_ANY
)) {
5843 return ME_ERRNO_SUCCESS
;
5846 static int me4600_ao_query_range_info(me_subdevice_t
*subdevice
,
5849 int *min
, int *max
, int *maxdata
)
5851 me4600_ao_subdevice_t
*instance
;
5853 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5855 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5858 *unit
= ME_UNIT_VOLT
;
5859 *min
= ME4600_AO_MIN_RANGE
;
5860 *max
= ME4600_AO_MAX_RANGE
;
5861 *maxdata
= ME4600_AO_MAX_DATA
;
5863 PERROR("Invalid range number specified.\n");
5864 return ME_ERRNO_INVALID_RANGE
;
5867 return ME_ERRNO_SUCCESS
;
5870 static int me4600_ao_query_timer(me_subdevice_t
*subdevice
,
5872 int *base_frequency
,
5873 long long *min_ticks
, long long *max_ticks
)
5875 me4600_ao_subdevice_t
*instance
;
5877 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5879 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5881 if ((timer
!= ME_TIMER_ACQ_START
) && (timer
!= ME_TIMER_CONV_START
)) {
5882 PERROR("Invalid timer specified.\n");
5883 return ME_ERRNO_INVALID_TIMER
;
5886 if (instance
->fifo
) { //Streaming device.
5887 *base_frequency
= ME4600_AO_BASE_FREQUENCY
;
5888 if (timer
== ME_TIMER_ACQ_START
) {
5889 *min_ticks
= ME4600_AO_MIN_ACQ_TICKS
;
5890 *max_ticks
= ME4600_AO_MAX_ACQ_TICKS
;
5891 } else if (timer
== ME_TIMER_CONV_START
) {
5892 *min_ticks
= ME4600_AO_MIN_CHAN_TICKS
;
5893 *max_ticks
= ME4600_AO_MAX_CHAN_TICKS
;
5895 } else { //Not streaming device!
5896 *base_frequency
= 0;
5901 return ME_ERRNO_SUCCESS
;
5904 static int me4600_ao_query_number_channels(me_subdevice_t
*subdevice
,
5907 me4600_ao_subdevice_t
*instance
;
5908 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5910 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5914 return ME_ERRNO_SUCCESS
;
5917 static int me4600_ao_query_subdevice_type(me_subdevice_t
*subdevice
,
5918 int *type
, int *subtype
)
5920 me4600_ao_subdevice_t
*instance
;
5922 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5924 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5927 *subtype
= (instance
->fifo
) ? ME_SUBTYPE_STREAMING
: ME_SUBTYPE_SINGLE
;
5929 return ME_ERRNO_SUCCESS
;
5932 static int me4600_ao_query_subdevice_caps(me_subdevice_t
*subdevice
, int *caps
)
5934 me4600_ao_subdevice_t
*instance
;
5935 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5937 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5940 ME_CAPS_AO_TRIG_SYNCHRONOUS
| ((instance
->fifo
) ? ME_CAPS_AO_FIFO
:
5943 return ME_ERRNO_SUCCESS
;
5946 static int me4600_ao_query_subdevice_caps_args(struct me_subdevice
*subdevice
,
5947 int cap
, int *args
, int count
)
5949 me4600_ao_subdevice_t
*instance
;
5950 int err
= ME_ERRNO_SUCCESS
;
5952 instance
= (me4600_ao_subdevice_t
*) subdevice
;
5954 PDEBUG("executed. idx=%d\n", instance
->ao_idx
);
5957 PERROR("Invalid capability argument count.\n");
5958 return ME_ERRNO_INVALID_CAP_ARG_COUNT
;
5962 case ME_CAP_AI_FIFO_SIZE
:
5963 args
[0] = (instance
->fifo
) ? ME4600_AO_FIFO_COUNT
: 0;
5966 case ME_CAP_AI_BUFFER_SIZE
:
5968 (instance
->circ_buf
.buf
) ? ME4600_AO_CIRC_BUF_COUNT
: 0;
5972 PERROR("Invalid capability.\n");
5973 err
= ME_ERRNO_INVALID_CAP
;