4 * @brief ME-4000 digital input/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>
42 #include "medefines.h"
43 #include "meinternal.h"
47 #include "me4600_dio_reg.h"
48 #include "me4600_dio.h"
58 static int me4600_dio_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me4600_dio_subdevice_t *instance;
64 PDEBUG("executed.\n");
66 instance = (me4600_dio_subdevice_t *) subdevice;
69 PERROR("Invalid flag specified.\n");
70 return ME_ERRNO_INVALID_FLAGS;
75 /* Set port to input mode */
76 spin_lock(&instance->subdevice_lock);
77 spin_lock(instance->ctrl_reg_lock);
78 mode = inl(instance->ctrl_reg);
80 ~((ME4600_DIO_CTRL_BIT_MODE_0 | ME4600_DIO_CTRL_BIT_MODE_1) <<
81 (instance->dio_idx * 2));
82 outl(mode, instance->ctrl_reg);
83 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
84 instance->ctrl_reg - instance->reg_base, mode);
85 spin_unlock(instance->ctrl_reg_lock);
87 outl(0, instance->port_reg);
88 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
89 instance->port_reg - instance->reg_base, 0);
90 spin_unlock(&instance->subdevice_lock);
94 return ME_ERRNO_SUCCESS;
97 static int me4600_dio_io_single_config(me_subdevice_t * subdevice,
103 int trig_type, int trig_edge, int flags)
105 me4600_dio_subdevice_t *instance;
106 int err = ME_ERRNO_SUCCESS;
109 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
110 | ME_IO_SINGLE_CONFIG_DIO_WORD |
111 ME_IO_SINGLE_CONFIG_DIO_DWORD);
114 PDEBUG("executed.\n");
116 instance = (me4600_dio_subdevice_t *) subdevice;
118 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
119 spin_lock(instance->ctrl_reg_lock);
120 mode = inl(instance->ctrl_reg);
122 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
123 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
125 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
127 ~((ME4600_DIO_CTRL_BIT_MODE_0 |
128 ME4600_DIO_CTRL_BIT_MODE_1) <<
129 (instance->dio_idx * 2));
130 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
132 ~((ME4600_DIO_CTRL_BIT_MODE_0 |
133 ME4600_DIO_CTRL_BIT_MODE_1) <<
134 (instance->dio_idx * 2));
136 ME4600_DIO_CTRL_BIT_MODE_0 << (instance->
138 } else if (single_config == ME_SINGLE_CONFIG_DIO_MUX32M) {
140 (ME4600_DIO_CTRL_BIT_MODE_0 |
141 ME4600_DIO_CTRL_BIT_MODE_1) << (instance->
145 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
146 ME4600_DIO_CTRL_BIT_FUNCTION_1;
148 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
152 if (ref == ME_REF_DIO_FIFO_LOW) {
154 (ME4600_DIO_CTRL_BIT_MODE_0 |
155 ME4600_DIO_CTRL_BIT_MODE_1) <<
156 (instance->dio_idx * 2);
157 mode |= ME4600_DIO_CTRL_BIT_FUNCTION_1;
158 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
160 (ME4600_DIO_CTRL_BIT_MODE_0 |
161 ME4600_DIO_CTRL_BIT_MODE_1) <<
162 (instance->dio_idx * 2);
163 mode |= ME4600_DIO_CTRL_BIT_FUNCTION_1;
165 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
169 ("Invalid port reference specified.\n");
170 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
172 } else if (single_config ==
173 ME_SINGLE_CONFIG_DIO_DEMUX32) {
175 (ME4600_DIO_CTRL_BIT_MODE_0 |
176 ME4600_DIO_CTRL_BIT_MODE_1) << (instance->
180 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
181 ME4600_DIO_CTRL_BIT_FUNCTION_1;
183 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
187 if (ref == ME_REF_DIO_FIFO_LOW) {
189 (ME4600_DIO_CTRL_BIT_MODE_0 |
190 ME4600_DIO_CTRL_BIT_MODE_1) <<
191 (instance->dio_idx * 2);
192 mode |= ME4600_DIO_CTRL_BIT_FUNCTION_0;
193 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
195 (ME4600_DIO_CTRL_BIT_MODE_0 |
196 ME4600_DIO_CTRL_BIT_MODE_1) <<
197 (instance->dio_idx * 2);
198 mode |= ME4600_DIO_CTRL_BIT_FUNCTION_0;
200 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
204 ("Invalid port reference specified.\n");
205 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
207 } else if (single_config ==
208 ME_SINGLE_CONFIG_DIO_BIT_PATTERN) {
210 (ME4600_DIO_CTRL_BIT_MODE_0 |
211 ME4600_DIO_CTRL_BIT_MODE_1) << (instance->
215 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
216 ME4600_DIO_CTRL_BIT_FUNCTION_1;
218 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
222 if (ref == ME_REF_DIO_FIFO_LOW) {
224 (ME4600_DIO_CTRL_BIT_MODE_0 |
225 ME4600_DIO_CTRL_BIT_MODE_1) <<
226 (instance->dio_idx * 2);
227 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
229 (ME4600_DIO_CTRL_BIT_MODE_0 |
230 ME4600_DIO_CTRL_BIT_MODE_1) <<
231 (instance->dio_idx * 2);
233 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0 <<
237 ("Invalid port reference specified.\n");
238 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
242 ("Invalid port configuration specified.\n");
243 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
246 PERROR("Invalid channel number.\n");
247 err = ME_ERRNO_INVALID_CHANNEL;
253 PERROR("Invalid flags.\n");
254 err = ME_ERRNO_INVALID_FLAGS;
258 outl(mode, instance->ctrl_reg);
259 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
261 instance->ctrl_reg - instance->reg_base, mode);
263 spin_unlock(instance->ctrl_reg_lock);
264 spin_unlock(&instance->subdevice_lock);
271 static int me4600_dio_io_single_read(me_subdevice_t * subdevice,
274 int *value, int time_out, int flags)
276 me4600_dio_subdevice_t *instance;
277 int err = ME_ERRNO_SUCCESS;
280 PDEBUG("executed.\n");
282 instance = (me4600_dio_subdevice_t *) subdevice;
286 spin_lock(&instance->subdevice_lock);
287 spin_lock(instance->ctrl_reg_lock);
289 case ME_IO_SINGLE_TYPE_DIO_BIT:
290 if ((channel >= 0) && (channel < 8)) {
293 ctrl_reg) & ((ME4600_DIO_CTRL_BIT_MODE_0 |
294 ME4600_DIO_CTRL_BIT_MODE_1) <<
295 (instance->dio_idx * 2));
297 (ME4600_DIO_CTRL_BIT_MODE_0 <<
298 (instance->dio_idx * 2))) || !mode) {
300 inl(instance->port_reg) & (0x1 << channel);
302 PERROR("Port not in output or input mode.\n");
303 err = ME_ERRNO_PREVIOUS_CONFIG;
306 PERROR("Invalid bit number specified.\n");
307 err = ME_ERRNO_INVALID_CHANNEL;
311 case ME_IO_SINGLE_NO_FLAGS:
312 case ME_IO_SINGLE_TYPE_DIO_BYTE:
316 ctrl_reg) & ((ME4600_DIO_CTRL_BIT_MODE_0 |
317 ME4600_DIO_CTRL_BIT_MODE_1) <<
318 (instance->dio_idx * 2));
320 (ME4600_DIO_CTRL_BIT_MODE_0 <<
321 (instance->dio_idx * 2))) || !mode) {
322 *value = inl(instance->port_reg) & 0xFF;
324 PERROR("Port not in output or input mode.\n");
325 err = ME_ERRNO_PREVIOUS_CONFIG;
328 PERROR("Invalid byte number specified.\n");
329 err = ME_ERRNO_INVALID_CHANNEL;
334 PERROR("Invalid flags specified.\n");
335 err = ME_ERRNO_INVALID_FLAGS;
337 spin_unlock(instance->ctrl_reg_lock);
338 spin_unlock(&instance->subdevice_lock);
345 static int me4600_dio_io_single_write(me_subdevice_t * subdevice,
348 int value, int time_out, int flags)
350 me4600_dio_subdevice_t *instance;
351 int err = ME_ERRNO_SUCCESS;
355 PDEBUG("executed.\n");
357 instance = (me4600_dio_subdevice_t *) subdevice;
361 spin_lock(&instance->subdevice_lock);
362 spin_lock(instance->ctrl_reg_lock);
364 case ME_IO_SINGLE_TYPE_DIO_BIT:
365 if ((channel >= 0) && (channel < 8)) {
368 ctrl_reg) & ((ME4600_DIO_CTRL_BIT_MODE_0 |
369 ME4600_DIO_CTRL_BIT_MODE_1) <<
370 (instance->dio_idx * 2));
373 (ME4600_DIO_CTRL_BIT_MODE_0 <<
374 (instance->dio_idx * 2))) {
375 byte = inl(instance->port_reg) & 0xFF;
378 byte |= 0x1 << channel;
380 byte &= ~(0x1 << channel);
382 outl(byte, instance->port_reg);
384 PERROR("Port not in output or input mode.\n");
385 err = ME_ERRNO_PREVIOUS_CONFIG;
388 PERROR("Invalid bit number specified.\n");
389 err = ME_ERRNO_INVALID_CHANNEL;
393 case ME_IO_SINGLE_NO_FLAGS:
394 case ME_IO_SINGLE_TYPE_DIO_BYTE:
398 ctrl_reg) & ((ME4600_DIO_CTRL_BIT_MODE_0 |
399 ME4600_DIO_CTRL_BIT_MODE_1) <<
400 (instance->dio_idx * 2));
403 (ME4600_DIO_CTRL_BIT_MODE_0 <<
404 (instance->dio_idx * 2))) {
405 outl(value, instance->port_reg);
407 PERROR("Port not in output or input mode.\n");
408 err = ME_ERRNO_PREVIOUS_CONFIG;
411 PERROR("Invalid byte number specified.\n");
412 err = ME_ERRNO_INVALID_CHANNEL;
417 PERROR("Invalid flags specified.\n");
418 err = ME_ERRNO_INVALID_FLAGS;
420 spin_unlock(instance->ctrl_reg_lock);
421 spin_unlock(&instance->subdevice_lock);
428 static int me4600_dio_query_number_channels(me_subdevice_t * subdevice,
431 PDEBUG("executed.\n");
433 return ME_ERRNO_SUCCESS;
436 static int me4600_dio_query_subdevice_type(me_subdevice_t * subdevice,
437 int *type, int *subtype)
439 PDEBUG("executed.\n");
441 *subtype = ME_SUBTYPE_SINGLE;
442 return ME_ERRNO_SUCCESS;
445 static int me4600_dio_query_subdevice_caps(me_subdevice_t * subdevice,
448 PDEBUG("executed.\n");
449 *caps = ME_CAPS_DIO_DIR_BYTE;
450 return ME_ERRNO_SUCCESS;
453 me4600_dio_subdevice_t *me4600_dio_constructor(uint32_t reg_base,
454 unsigned int dio_idx,
455 spinlock_t * ctrl_reg_lock)
457 me4600_dio_subdevice_t *subdevice;
460 PDEBUG("executed.\n");
462 /* Allocate memory for subdevice instance */
463 subdevice = kmalloc(sizeof(me4600_dio_subdevice_t), GFP_KERNEL);
466 PERROR("Cannot get memory for subdevice instance.\n");
470 memset(subdevice, 0, sizeof(me4600_dio_subdevice_t));
472 /* Initialize subdevice base class */
473 err = me_subdevice_init(&subdevice->base);
476 PERROR("Cannot initialize subdevice base class instance.\n");
480 // Initialize spin locks.
481 spin_lock_init(&subdevice->subdevice_lock);
482 subdevice->ctrl_reg_lock = ctrl_reg_lock;
484 /* Save digital i/o index */
485 subdevice->dio_idx = dio_idx;
487 /* Save the subdevice index */
488 subdevice->ctrl_reg = reg_base + ME4600_DIO_CTRL_REG;
489 subdevice->port_reg = reg_base + ME4600_DIO_PORT_REG + (dio_idx * 4);
490 #ifdef MEDEBUG_DEBUG_REG
491 subdevice->reg_base = reg_base;
494 /* Overload base class methods. */
495 subdevice->base.me_subdevice_io_reset_subdevice =
496 me4600_dio_io_reset_subdevice;
497 subdevice->base.me_subdevice_io_single_config =
498 me4600_dio_io_single_config;
499 subdevice->base.me_subdevice_io_single_read = me4600_dio_io_single_read;
500 subdevice->base.me_subdevice_io_single_write =
501 me4600_dio_io_single_write;
502 subdevice->base.me_subdevice_query_number_channels =
503 me4600_dio_query_number_channels;
504 subdevice->base.me_subdevice_query_subdevice_type =
505 me4600_dio_query_subdevice_type;
506 subdevice->base.me_subdevice_query_subdevice_caps =
507 me4600_dio_query_subdevice_caps;