4 * @brief ME-1600 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.
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
40 #include <linux/types.h>
41 #include <linux/sched.h>
43 #include <linux/workqueue.h>
45 #include "medefines.h"
46 #include "meinternal.h"
50 #include "me1600_ao_reg.h"
51 #include "me1600_ao.h"
56 static void me1600_ao_destructor(struct me_subdevice *subdevice);
58 static void me1600_ao_work_control_task(struct work_struct *work);
60 static int me1600_ao_io_reset_subdevice(me_subdevice_t *subdevice,
61 struct file *filep, int flags);
62 static int me1600_ao_io_single_config(me_subdevice_t *subdevice,
63 struct file *filep, int channel,
64 int single_config, int ref, int trig_chan,
65 int trig_type, int trig_edge, int flags);
66 static int me1600_ao_io_single_read(me_subdevice_t *subdevice,
67 struct file *filep, int channel, int *value,
68 int time_out, int flags);
69 static int me1600_ao_io_single_write(me_subdevice_t *subdevice,
70 struct file *filep, int channel, int value,
71 int time_out, int flags);
72 static int me1600_ao_query_number_channels(me_subdevice_t *subdevice,
74 static int me1600_ao_query_subdevice_type(me_subdevice_t *subdevice, int *type,
76 static int me1600_ao_query_subdevice_caps(me_subdevice_t *subdevice,
78 static int me1600_ao_query_range_by_min_max(me_subdevice_t *subdevice,
79 int unit, int *min, int *max,
80 int *maxdata, int *range);
81 static int me1600_ao_query_number_ranges(me_subdevice_t *subdevice, int unit,
83 static int me1600_ao_query_range_info(me_subdevice_t *subdevice, int range,
84 int *unit, int *min, int *max,
90 me1600_ao_subdevice_t *me1600_ao_constructor(uint32_t reg_base,
93 spinlock_t *config_regs_lock,
94 spinlock_t *ao_shadows_lock,
95 me1600_ao_shadow_t *ao_regs_shadows,
96 struct workqueue_struct *me1600_wq)
98 me1600_ao_subdevice_t *subdevice;
101 PDEBUG("executed. idx=%d\n", ao_idx);
103 // Allocate memory for subdevice instance.
104 subdevice = kmalloc(sizeof(me1600_ao_subdevice_t), GFP_KERNEL);
108 ("Cannot get memory for analog output subdevice instance.\n");
112 memset(subdevice, 0, sizeof(me1600_ao_subdevice_t));
114 // Initialize subdevice base class.
115 err = me_subdevice_init(&subdevice->base);
118 PERROR("Cannot initialize subdevice base class instance.\n");
122 // Initialize spin locks.
123 spin_lock_init(&subdevice->subdevice_lock);
124 subdevice->config_regs_lock = config_regs_lock;
125 subdevice->ao_shadows_lock = ao_shadows_lock;
127 // Save the subdevice index.
128 subdevice->ao_idx = ao_idx;
130 // Initialize range lists.
131 subdevice->u_ranges_count = 2;
133 subdevice->u_ranges[0].min = 0; //0V
134 subdevice->u_ranges[0].max = 9997558; //10V
136 subdevice->u_ranges[1].min = -10E6; //-10V
137 subdevice->u_ranges[1].max = 9995117; //10V
139 if (curr) { // This is version with current outputs.
140 subdevice->i_ranges_count = 2;
142 subdevice->i_ranges[0].min = 0; //0mA
143 subdevice->i_ranges[0].max = 19995117; //20mA
145 subdevice->i_ranges[1].min = 4E3; //4mA
146 subdevice->i_ranges[1].max = 19995118; //20mA
147 } else { // This is version without current outputs.
148 subdevice->i_ranges_count = 0;
150 subdevice->i_ranges[0].min = 0; //0mA
151 subdevice->i_ranges[0].max = 0; //0mA
153 subdevice->i_ranges[1].min = 0; //0mA
154 subdevice->i_ranges[1].max = 0; //0mA
157 // Initialize registers.
158 subdevice->uni_bi_reg = reg_base + ME1600_UNI_BI_REG;
159 subdevice->i_range_reg = reg_base + ME1600_020_420_REG;
160 subdevice->sim_output_reg = reg_base + ME1600_SIM_OUTPUT_REG;
161 subdevice->current_on_reg = reg_base + ME1600_CURRENT_ON_REG;
162 #ifdef MEDEBUG_DEBUG_REG
163 subdevice->reg_base = reg_base;
166 // Initialize shadow structure.
167 subdevice->ao_regs_shadows = ao_regs_shadows;
169 // Override base class methods.
170 subdevice->base.me_subdevice_destructor = me1600_ao_destructor;
171 subdevice->base.me_subdevice_io_reset_subdevice =
172 me1600_ao_io_reset_subdevice;
173 subdevice->base.me_subdevice_io_single_config =
174 me1600_ao_io_single_config;
175 subdevice->base.me_subdevice_io_single_read = me1600_ao_io_single_read;
176 subdevice->base.me_subdevice_io_single_write =
177 me1600_ao_io_single_write;
178 subdevice->base.me_subdevice_query_number_channels =
179 me1600_ao_query_number_channels;
180 subdevice->base.me_subdevice_query_subdevice_type =
181 me1600_ao_query_subdevice_type;
182 subdevice->base.me_subdevice_query_subdevice_caps =
183 me1600_ao_query_subdevice_caps;
184 subdevice->base.me_subdevice_query_range_by_min_max =
185 me1600_ao_query_range_by_min_max;
186 subdevice->base.me_subdevice_query_number_ranges =
187 me1600_ao_query_number_ranges;
188 subdevice->base.me_subdevice_query_range_info =
189 me1600_ao_query_range_info;
191 // Initialize wait queue.
192 init_waitqueue_head(&subdevice->wait_queue);
194 // Prepare work queue.
195 subdevice->me1600_workqueue = me1600_wq;
197 /* workqueue API changed in kernel 2.6.20 */
198 INIT_DELAYED_WORK(&subdevice->ao_control_task,
199 me1600_ao_work_control_task);
203 static void me1600_ao_destructor(struct me_subdevice *subdevice)
205 me1600_ao_subdevice_t *instance;
207 instance = (me1600_ao_subdevice_t *) subdevice;
209 PDEBUG("executed. idx=%d\n", instance->ao_idx);
211 instance->ao_control_task_flag = 0;
213 // Reset subdevice to asure clean exit.
214 me1600_ao_io_reset_subdevice(subdevice, NULL,
215 ME_IO_RESET_SUBDEVICE_NO_FLAGS);
217 // Remove any tasks from work queue. This is paranoic because it was done allready in reset().
218 if (!cancel_delayed_work(&instance->ao_control_task)) { //Wait 2 ticks to be sure that control task is removed from queue.
219 set_current_state(TASK_INTERRUPTIBLE);
224 static int me1600_ao_io_reset_subdevice(me_subdevice_t *subdevice,
225 struct file *filep, int flags)
227 me1600_ao_subdevice_t *instance;
230 instance = (me1600_ao_subdevice_t *) subdevice;
232 PDEBUG("executed. idx=%d\n", instance->ao_idx);
235 PERROR("Invalid flag specified.\n");
236 return ME_ERRNO_INVALID_FLAGS;
241 //Cancel control task
242 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
243 instance->ao_control_task_flag = 0;
244 cancel_delayed_work(&instance->ao_control_task);
245 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
247 // Reset all settings.
248 spin_lock(&instance->subdevice_lock);
249 spin_lock(instance->ao_shadows_lock);
250 (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
251 (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
252 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Not waiting for triggering.
253 (instance->ao_regs_shadows)->synchronous &= ~(0x1 << instance->ao_idx); //Individual triggering.
255 // Set output to default (safe) state.
256 spin_lock(instance->config_regs_lock);
257 tmp = inw(instance->uni_bi_reg); // unipolar
258 tmp |= (0x1 << instance->ao_idx);
259 outw(tmp, instance->uni_bi_reg);
260 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
261 instance->uni_bi_reg - instance->reg_base, tmp);
263 tmp = inw(instance->current_on_reg); // Volts only!
264 tmp &= ~(0x1 << instance->ao_idx);
266 outw(tmp, instance->current_on_reg);
267 PDEBUG_REG("current_on_reg outl(0x%lX+0x%lX)=0x%x\n",
269 instance->current_on_reg - instance->reg_base, tmp);
271 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
272 tmp &= ~(0x1 << instance->ao_idx);
273 outw(tmp, instance->i_range_reg);
274 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
275 instance->i_range_reg - instance->reg_base, tmp);
277 outw(0, (instance->ao_regs_shadows)->registry[instance->ao_idx]);
278 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
279 (instance->ao_regs_shadows)->registry[instance->ao_idx] -
280 instance->reg_base, 0);
283 outw(0x0000, instance->sim_output_reg);
284 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
286 instance->sim_output_reg - instance->reg_base, 0x0000);
287 outw(0xFFFF, instance->sim_output_reg);
288 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
290 instance->sim_output_reg - instance->reg_base, 0xFFFF);
291 spin_unlock(instance->config_regs_lock);
292 spin_unlock(instance->ao_shadows_lock);
294 // Set status to 'none'
295 instance->status = ao_status_none;
296 spin_unlock(&instance->subdevice_lock);
298 //Signal reset if user is on wait.
299 wake_up_interruptible_all(&instance->wait_queue);
303 return ME_ERRNO_SUCCESS;
306 static int me1600_ao_io_single_config(me_subdevice_t *subdevice,
312 int trig_type, int trig_edge, int flags)
314 me1600_ao_subdevice_t *instance;
317 instance = (me1600_ao_subdevice_t *) subdevice;
319 PDEBUG("executed. idx=%d\n", instance->ao_idx);
321 // Checking parameters.
324 ("Invalid flag specified. Must be ME_IO_SINGLE_CONFIG_NO_FLAGS.\n");
325 return ME_ERRNO_INVALID_FLAGS;
328 if (trig_edge != ME_TRIG_EDGE_NONE) {
330 ("Invalid trigger edge. Software trigger has not edge. Must be ME_TRIG_EDGE_NONE\n");
331 return ME_ERRNO_INVALID_TRIG_EDGE;
334 if (trig_type != ME_TRIG_TYPE_SW) {
335 PERROR("Invalid trigger edge. Must be ME_TRIG_TYPE_SW.\n");
336 return ME_ERRNO_INVALID_TRIG_TYPE;
339 if ((trig_chan != ME_TRIG_CHAN_DEFAULT)
340 && (trig_chan != ME_TRIG_CHAN_SYNCHRONOUS)) {
341 PERROR("Invalid trigger channel specified.\n");
342 return ME_ERRNO_INVALID_TRIG_CHAN;
345 if (ref != ME_REF_AO_GROUND) {
347 ("Invalid reference. Analog outputs have to have got REF_AO_GROUND.\n");
348 return ME_ERRNO_INVALID_REF;
351 if (((single_config + 1) >
352 (instance->u_ranges_count + instance->i_ranges_count))
353 || (single_config < 0)) {
354 PERROR("Invalid range specified.\n");
355 return ME_ERRNO_INVALID_SINGLE_CONFIG;
359 PERROR("Invalid channel specified.\n");
360 return ME_ERRNO_INVALID_CHANNEL;
362 // Checking parameters - done. All is fine. Do config.
366 //Cancel control task
367 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
368 instance->ao_control_task_flag = 0;
369 cancel_delayed_work(&instance->ao_control_task);
371 spin_lock(&instance->subdevice_lock);
372 spin_lock(instance->ao_shadows_lock);
373 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
374 (instance->ao_regs_shadows)->shadow[instance->ao_idx] = 0;
375 (instance->ao_regs_shadows)->mirror[instance->ao_idx] = 0;
377 spin_lock(instance->config_regs_lock);
378 switch (single_config) {
380 tmp = inw(instance->current_on_reg); // Volts
381 tmp &= ~(0x1 << instance->ao_idx);
382 outw(tmp, instance->current_on_reg);
383 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
385 instance->current_on_reg - instance->reg_base, tmp);
389 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
390 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
392 (instance->ao_regs_shadows)->registry[instance->
394 instance->reg_base, 0);
396 tmp = inw(instance->uni_bi_reg); // unipolar
397 tmp |= (0x1 << instance->ao_idx);
398 outw(tmp, instance->uni_bi_reg);
399 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
401 instance->uni_bi_reg - instance->reg_base, tmp);
403 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
404 tmp &= ~(0x1 << instance->ao_idx);
405 outw(tmp, instance->i_range_reg);
406 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
408 instance->i_range_reg - instance->reg_base, tmp);
412 tmp = inw(instance->current_on_reg); // Volts
413 tmp &= ~(0x1 << instance->ao_idx);
414 outw(tmp, instance->current_on_reg);
415 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
417 instance->current_on_reg - instance->reg_base, tmp);
421 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
422 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
424 (instance->ao_regs_shadows)->registry[instance->
426 instance->reg_base, 0x0800);
428 tmp = inw(instance->uni_bi_reg); // bipolar
429 tmp &= ~(0x1 << instance->ao_idx);
430 outw(tmp, instance->uni_bi_reg);
431 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
433 instance->uni_bi_reg - instance->reg_base, tmp);
435 tmp = inw(instance->i_range_reg); // 0..20mA <= If exists.
436 tmp &= ~(0x1 << instance->ao_idx);
437 outw(tmp, instance->i_range_reg);
438 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
440 instance->i_range_reg - instance->reg_base, tmp);
444 tmp = inw(instance->current_on_reg); // mAmpers
445 tmp |= (0x1 << instance->ao_idx);
446 outw(tmp, instance->current_on_reg);
447 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
449 instance->current_on_reg - instance->reg_base, tmp);
451 tmp = inw(instance->i_range_reg); // 0..20mA
452 tmp &= ~(0x1 << instance->ao_idx);
453 outw(tmp, instance->i_range_reg);
454 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
456 instance->i_range_reg - instance->reg_base, tmp);
460 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
461 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
463 (instance->ao_regs_shadows)->registry[instance->
465 instance->reg_base, 0);
467 tmp = inw(instance->uni_bi_reg); // unipolar
468 tmp |= (0x1 << instance->ao_idx);
469 outw(tmp, instance->uni_bi_reg);
470 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
472 instance->uni_bi_reg - instance->reg_base, tmp);
476 tmp = inw(instance->current_on_reg); // mAmpers
477 tmp |= (0x1 << instance->ao_idx);
478 outw(tmp, instance->current_on_reg);
479 PDEBUG_REG("current_on_reg outw(0x%lX+0x%lX)=0x%x\n",
481 instance->current_on_reg - instance->reg_base, tmp);
483 tmp = inw(instance->i_range_reg); // 4..20mA
484 tmp |= (0x1 << instance->ao_idx);
485 outw(tmp, instance->i_range_reg);
486 PDEBUG_REG("i_range_reg outl(0x%lX+0x%lX)=0x%x\n",
488 instance->i_range_reg - instance->reg_base, tmp);
492 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
493 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
495 (instance->ao_regs_shadows)->registry[instance->
497 instance->reg_base, 0);
499 tmp = inw(instance->uni_bi_reg); // unipolar
500 tmp |= (0x1 << instance->ao_idx);
501 outw(tmp, instance->uni_bi_reg);
502 PDEBUG_REG("uni_bi_reg outw(0x%lX+0x%lX)=0x%x\n",
504 instance->uni_bi_reg - instance->reg_base, tmp);
509 outw(0x0000, instance->sim_output_reg);
510 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
512 instance->sim_output_reg - instance->reg_base, 0x0000);
513 outw(0xFFFF, instance->sim_output_reg);
514 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
516 instance->sim_output_reg - instance->reg_base, 0xFFFF);
518 if (trig_chan == ME_TRIG_CHAN_DEFAULT) { // Individual triggering.
519 (instance->ao_regs_shadows)->synchronous &=
520 ~(0x1 << instance->ao_idx);
521 PDEBUG("Individual triggering.\n");
522 } else if (trig_chan == ME_TRIG_CHAN_SYNCHRONOUS) { // Synchronous triggering.
523 (instance->ao_regs_shadows)->synchronous |=
524 (0x1 << instance->ao_idx);
525 PDEBUG("Synchronous triggering.\n");
527 spin_unlock(instance->config_regs_lock);
528 spin_unlock(instance->ao_shadows_lock);
530 instance->status = ao_status_single_configured;
531 spin_unlock(&instance->subdevice_lock);
535 return ME_ERRNO_SUCCESS;
538 static int me1600_ao_io_single_read(me_subdevice_t *subdevice,
541 int *value, int time_out, int flags)
543 me1600_ao_subdevice_t *instance;
544 unsigned long delay = 0;
546 int err = ME_ERRNO_SUCCESS;
548 instance = (me1600_ao_subdevice_t *) subdevice;
550 PDEBUG("executed. idx=%d\n", instance->ao_idx);
552 if (flags & ~ME_IO_SINGLE_NONBLOCKING) {
553 PERROR("Invalid flag specified. %d\n", flags);
554 return ME_ERRNO_INVALID_FLAGS;
558 PERROR("Invalid timeout specified.\n");
559 return ME_ERRNO_INVALID_TIMEOUT;
563 PERROR("Invalid channel specified.\n");
564 return ME_ERRNO_INVALID_CHANNEL;
567 if ((!flags) && ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) { //Blocking mode. Wait for software trigger.
569 delay = (time_out * HZ) / 1000;
576 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
577 wait_event_interruptible_timeout(instance->wait_queue,
582 (delay) ? delay : LONG_MAX);
584 if (instance == ao_status_none) { // Reset was called.
585 PDEBUG("Single canceled.\n");
586 err = ME_ERRNO_CANCELLED;
589 if (signal_pending(current)) {
590 PERROR("Wait on start of state machine interrupted.\n");
591 err = ME_ERRNO_SIGNAL;
594 if ((delay) && ((jiffies - j) >= delay)) {
595 PDEBUG("Timeout reached.\n");
596 err = ME_ERRNO_TIMEOUT;
600 *value = (instance->ao_regs_shadows)->mirror[instance->ao_idx];
605 static int me1600_ao_io_single_write(me_subdevice_t *subdevice,
608 int value, int time_out, int flags)
610 me1600_ao_subdevice_t *instance;
611 int err = ME_ERRNO_SUCCESS;
612 unsigned long delay = 0;
616 instance = (me1600_ao_subdevice_t *) subdevice;
618 PDEBUG("executed. idx=%d\n", instance->ao_idx);
621 ~(ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS |
622 ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) {
623 PERROR("Invalid flag specified.\n");
624 return ME_ERRNO_INVALID_FLAGS;
628 PERROR("Invalid timeout specified.\n");
629 return ME_ERRNO_INVALID_TIMEOUT;
632 if (value & ~ME1600_AO_MAX_DATA) {
633 PERROR("Invalid value provided.\n");
634 return ME_ERRNO_VALUE_OUT_OF_RANGE;
638 PERROR("Invalid channel specified.\n");
639 return ME_ERRNO_INVALID_CHANNEL;
644 //Cancel control task
645 PDEBUG("Cancel control task. idx=%d\n", instance->ao_idx);
646 instance->ao_control_task_flag = 0;
647 cancel_delayed_work(&instance->ao_control_task);
648 (instance->ao_regs_shadows)->trigger &= ~(0x1 << instance->ao_idx); //Cancell waiting for trigger.
651 delay = (time_out * HZ) / 1000;
657 spin_lock(instance->ao_shadows_lock);
658 (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
661 if (flags & ME_IO_SINGLE_TYPE_TRIG_SYNCHRONOUS) { // Trigger all outputs from synchronous list.
662 for (i = 0; i < (instance->ao_regs_shadows)->count; i++) {
663 if (((instance->ao_regs_shadows)->synchronous & (0x1 << i)) || (i == instance->ao_idx)) { // Set all from synchronous list to correct state.
665 ("Synchronous triggering: output %d. idx=%d\n",
666 i, instance->ao_idx);
667 (instance->ao_regs_shadows)->mirror[i] =
668 (instance->ao_regs_shadows)->shadow[i];
670 outw((instance->ao_regs_shadows)->shadow[i],
671 (instance->ao_regs_shadows)->registry[i]);
673 ("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
675 (instance->ao_regs_shadows)->registry[i] -
677 (instance->ao_regs_shadows)->shadow[i]);
679 (instance->ao_regs_shadows)->trigger &=
685 outw(0x0000, instance->sim_output_reg);
686 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
688 instance->sim_output_reg - instance->reg_base, 0);
689 outw(0xFFFF, instance->sim_output_reg);
690 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
692 instance->sim_output_reg - instance->reg_base,
694 instance->status = ao_status_single_end;
695 } else { // Individual mode.
696 if ((instance->ao_regs_shadows)->synchronous & (0x1 << instance->ao_idx)) { // Put on synchronous start list. Set output as waiting for trigger.
697 PDEBUG("Add to synchronous list. idx=%d\n",
699 (instance->ao_regs_shadows)->trigger |=
700 (0x1 << instance->ao_idx);
701 instance->status = ao_status_single_run;
702 PDEBUG("Synchronous list: 0x%x.\n",
703 (instance->ao_regs_shadows)->synchronous);
704 } else { // Fired this one.
705 PDEBUG("Triggering. idx=%d\n", instance->ao_idx);
706 (instance->ao_regs_shadows)->mirror[instance->ao_idx] =
707 (instance->ao_regs_shadows)->shadow[instance->
710 outw((instance->ao_regs_shadows)->
711 shadow[instance->ao_idx],
712 (instance->ao_regs_shadows)->registry[instance->
714 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
716 (instance->ao_regs_shadows)->
717 registry[instance->ao_idx] -
719 (instance->ao_regs_shadows)->
720 shadow[instance->ao_idx]);
722 // Set output as triggered.
723 (instance->ao_regs_shadows)->trigger &=
724 ~(0x1 << instance->ao_idx);
727 outw(0x0000, instance->sim_output_reg);
728 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
730 instance->sim_output_reg -
731 instance->reg_base, 0);
732 outw(0xFFFF, instance->sim_output_reg);
733 PDEBUG_REG("sim_output_reg outl(0x%lX+0x%lX)=0x%x\n",
735 instance->sim_output_reg -
736 instance->reg_base, 0xFFFF);
737 instance->status = ao_status_single_end;
740 spin_unlock(instance->ao_shadows_lock);
743 instance->timeout.delay = delay;
744 instance->timeout.start_time = jiffies;
745 instance->ao_control_task_flag = 1;
746 queue_delayed_work(instance->me1600_workqueue,
747 &instance->ao_control_task, 1);
749 if ((!(flags & ME_IO_SINGLE_TYPE_WRITE_NONBLOCKING)) &&
750 ((instance->ao_regs_shadows)->trigger & instance->ao_idx)) {
751 /* Blocking mode. Wait for software trigger. */
753 delay = (time_out * HZ) / 1000;
760 //Only runing process will interrupt this call. Events are signaled when status change. This procedure has own timeout.
761 wait_event_interruptible_timeout(instance->wait_queue,
766 (delay) ? delay : LONG_MAX);
768 if (instance == ao_status_none) {
769 PDEBUG("Single canceled.\n");
770 err = ME_ERRNO_CANCELLED;
772 if (signal_pending(current)) {
773 PERROR("Wait on start of state machine interrupted.\n");
774 err = ME_ERRNO_SIGNAL;
777 if ((delay) && ((jiffies - j) >= delay)) {
778 PDEBUG("Timeout reached.\n");
779 err = ME_ERRNO_TIMEOUT;
788 static int me1600_ao_query_number_channels(me_subdevice_t *subdevice,
791 me1600_ao_subdevice_t *instance;
792 instance = (me1600_ao_subdevice_t *) subdevice;
794 PDEBUG("executed. idx=%d\n", instance->ao_idx);
796 *number = 1; //Every subdevice has only 1 channel.
797 return ME_ERRNO_SUCCESS;
800 static int me1600_ao_query_subdevice_type(me_subdevice_t *subdevice, int *type,
803 me1600_ao_subdevice_t *instance;
804 instance = (me1600_ao_subdevice_t *) subdevice;
806 PDEBUG("executed. idx=%d\n", instance->ao_idx);
809 *subtype = ME_SUBTYPE_SINGLE;
810 return ME_ERRNO_SUCCESS;
813 static int me1600_ao_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
815 PDEBUG("executed.\n");
816 *caps = ME_CAPS_AO_TRIG_SYNCHRONOUS;
817 return ME_ERRNO_SUCCESS;
820 static int me1600_ao_query_range_by_min_max(me_subdevice_t *subdevice,
823 int *max, int *maxdata, int *range)
825 me1600_ao_subdevice_t *instance;
830 instance = (me1600_ao_subdevice_t *) subdevice;
832 PDEBUG("executed. idx=%d\n", instance->ao_idx);
834 if ((*max - *min) < 0) {
835 PERROR("Invalid minimum and maximum values specified.\n");
836 return ME_ERRNO_INVALID_MIN_MAX;
838 // Maximum ranges are slightly less then 10V or 20mA. For convenient we accepted this value as valid one.
839 if (unit == ME_UNIT_VOLT) {
840 for (i = 0; i < instance->u_ranges_count; i++) {
841 if ((instance->u_ranges[i].min <= *min)
842 && ((instance->u_ranges[i].max + 5000) >= *max)) {
843 if ((instance->u_ranges[i].max -
844 instance->u_ranges[i].min) - (*max -
849 (instance->u_ranges[i].max -
850 instance->u_ranges[i].min) -
857 PERROR("No matching range found.\n");
858 return ME_ERRNO_NO_RANGE;
860 *min = instance->u_ranges[r].min;
861 *max = instance->u_ranges[r].max;
864 } else if (unit == ME_UNIT_AMPERE) {
865 for (i = 0; i < instance->i_ranges_count; i++) {
866 if ((instance->i_ranges[i].min <= *min)
867 && (instance->i_ranges[i].max + 5000 >= *max)) {
868 if ((instance->i_ranges[i].max -
869 instance->i_ranges[i].min) - (*max -
874 (instance->i_ranges[i].max -
875 instance->i_ranges[i].min) -
882 PERROR("No matching range found.\n");
883 return ME_ERRNO_NO_RANGE;
885 *min = instance->i_ranges[r].min;
886 *max = instance->i_ranges[r].max;
887 *range = r + instance->u_ranges_count;
890 PERROR("Invalid physical unit specified.\n");
891 return ME_ERRNO_INVALID_UNIT;
893 *maxdata = ME1600_AO_MAX_DATA;
895 return ME_ERRNO_SUCCESS;
898 static int me1600_ao_query_number_ranges(me_subdevice_t *subdevice,
899 int unit, int *count)
901 me1600_ao_subdevice_t *instance;
903 PDEBUG("executed.\n");
905 instance = (me1600_ao_subdevice_t *) subdevice;
908 *count = instance->u_ranges_count;
911 *count = instance->i_ranges_count;
914 *count = instance->u_ranges_count + instance->i_ranges_count;
920 return ME_ERRNO_SUCCESS;
923 static int me1600_ao_query_range_info(me_subdevice_t *subdevice,
926 int *min, int *max, int *maxdata)
928 me1600_ao_subdevice_t *instance;
930 PDEBUG("executed.\n");
932 instance = (me1600_ao_subdevice_t *) subdevice;
935 (instance->u_ranges_count + instance->i_ranges_count))
937 PERROR("Invalid range number specified.\n");
938 return ME_ERRNO_INVALID_RANGE;
941 if (range < instance->u_ranges_count) {
942 *unit = ME_UNIT_VOLT;
943 *min = instance->u_ranges[range].min;
944 *max = instance->u_ranges[range].max;
945 } else if (range < instance->u_ranges_count + instance->i_ranges_count) {
946 *unit = ME_UNIT_AMPERE;
947 *min = instance->i_ranges[range - instance->u_ranges_count].min;
948 *max = instance->i_ranges[range - instance->u_ranges_count].max;
950 *maxdata = ME1600_AO_MAX_DATA;
952 return ME_ERRNO_SUCCESS;
955 static void me1600_ao_work_control_task(struct work_struct *work)
957 me1600_ao_subdevice_t *instance;
962 container_of((void *)work, me1600_ao_subdevice_t, ao_control_task);
964 PINFO("<%s: %ld> executed. idx=%d\n", __func__, jiffies,
967 if (!((instance->ao_regs_shadows)->trigger & instance->ao_idx)) { // Output was triggerd.
971 if (instance->status == ao_status_single_run) {
972 instance->status = ao_status_single_end;
975 } else if ((instance->timeout.delay) && ((jiffies - instance->timeout.start_time) >= instance->timeout.delay)) { // Timeout
976 PDEBUG("Timeout reached.\n");
977 spin_lock(instance->ao_shadows_lock);
978 // Restore old settings.
979 PDEBUG("Write old value back to register.\n");
980 (instance->ao_regs_shadows)->shadow[instance->ao_idx] =
981 (instance->ao_regs_shadows)->mirror[instance->ao_idx];
983 outw((instance->ao_regs_shadows)->mirror[instance->ao_idx],
984 (instance->ao_regs_shadows)->registry[instance->ao_idx]);
985 PDEBUG_REG("channel_reg outw(0x%lX+0x%lX)=0x%x\n",
987 (instance->ao_regs_shadows)->registry[instance->
990 (instance->ao_regs_shadows)->mirror[instance->
993 //Remove from synchronous strt list.
994 (instance->ao_regs_shadows)->trigger &=
995 ~(0x1 << instance->ao_idx);
996 if (instance->status == ao_status_none) {
997 instance->status = ao_status_single_end;
999 spin_unlock(instance->ao_shadows_lock);
1006 if (signaling) { //Signal it.
1007 wake_up_interruptible_all(&instance->wait_queue);
1010 if (instance->ao_control_task_flag && reschedule) { // Reschedule task
1011 queue_delayed_work(instance->me1600_workqueue,
1012 &instance->ao_control_task, 1);
1014 PINFO("<%s> Ending control task.\n", __func__);