4 * @brief ME-630 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 "me0600_dio_reg.h"
48 #include "me0600_dio.h"
58 static int me0600_dio_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me0600_dio_subdevice_t *instance;
64 PDEBUG("executed.\n");
66 instance = (me0600_dio_subdevice_t *) subdevice;
69 PERROR("Invalid flag specified.\n");
70 return ME_ERRNO_INVALID_FLAGS;
75 spin_lock(&instance->subdevice_lock);
76 spin_lock(instance->ctrl_reg_lock);
77 mode = inb(instance->ctrl_reg);
78 mode &= ~(0x3 << (instance->dio_idx * 2));
79 outb(mode, instance->ctrl_reg);
80 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
81 instance->ctrl_reg - instance->reg_base, mode);
82 spin_unlock(instance->ctrl_reg_lock);
84 outb(0x00, instance->port_reg);
85 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
86 instance->port_reg - instance->reg_base, 0x00);
87 spin_unlock(&instance->subdevice_lock);
91 return ME_ERRNO_SUCCESS;
94 static int me0600_dio_io_single_config(me_subdevice_t * subdevice,
100 int trig_type, int trig_edge, int flags)
102 me0600_dio_subdevice_t *instance;
103 int err = ME_ERRNO_SUCCESS;
106 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
107 | ME_IO_SINGLE_CONFIG_DIO_WORD |
108 ME_IO_SINGLE_CONFIG_DIO_DWORD);
110 PDEBUG("executed.\n");
112 instance = (me0600_dio_subdevice_t *) subdevice;
116 spin_lock(&instance->subdevice_lock);
117 spin_lock(instance->ctrl_reg_lock);
118 mode = inb(instance->ctrl_reg);
120 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
121 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
123 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
125 ~((ME0600_DIO_CONFIG_BIT_OUT_0) <<
126 (instance->dio_idx * 2));
127 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
129 ~((ME0600_DIO_CONFIG_BIT_OUT_0) <<
130 (instance->dio_idx * 2));
132 ME0600_DIO_CONFIG_BIT_OUT_0 << (instance->
137 ("Invalid port configuration specified.\n");
138 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
141 PERROR("Invalid channel number.\n");
142 err = ME_ERRNO_INVALID_CHANNEL;
147 PERROR("Invalid flags.\n");
148 err = ME_ERRNO_INVALID_FLAGS;
152 outb(mode, instance->ctrl_reg);
153 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
155 instance->ctrl_reg - instance->reg_base, mode);
157 spin_unlock(instance->ctrl_reg_lock);
158 spin_unlock(&instance->subdevice_lock);
165 static int me0600_dio_io_single_read(me_subdevice_t * subdevice,
168 int *value, int time_out, int flags)
170 me0600_dio_subdevice_t *instance;
171 int err = ME_ERRNO_SUCCESS;
174 PDEBUG("executed.\n");
176 instance = (me0600_dio_subdevice_t *) subdevice;
180 spin_lock(&instance->subdevice_lock);
181 spin_lock(instance->ctrl_reg_lock);
183 case ME_IO_SINGLE_TYPE_DIO_BIT:
184 if ((channel >= 0) && (channel < 8)) {
187 ctrl_reg) & ((ME0600_DIO_CONFIG_BIT_OUT_0) <<
188 (instance->dio_idx * 2));
191 (ME0600_DIO_CONFIG_BIT_OUT_0 <<
192 (instance->dio_idx * 2))) || !mode) {
195 port_reg) & (0x0001 << channel);
197 PERROR("Port not in output or input mode.\n");
198 err = ME_ERRNO_PREVIOUS_CONFIG;
201 PERROR("Invalid bit number specified.\n");
202 err = ME_ERRNO_INVALID_CHANNEL;
207 case ME_IO_SINGLE_NO_FLAGS:
208 case ME_IO_SINGLE_TYPE_DIO_BYTE:
212 ctrl_reg) & ((ME0600_DIO_CONFIG_BIT_OUT_0) <<
213 (instance->dio_idx * 2));
216 (ME0600_DIO_CONFIG_BIT_OUT_0 <<
217 (instance->dio_idx * 2))) || !mode) {
218 *value = inb(instance->port_reg) & 0x00FF;
220 PERROR("Port not in output or input mode.\n");
221 err = ME_ERRNO_PREVIOUS_CONFIG;
224 PERROR("Invalid byte number specified.\n");
225 err = ME_ERRNO_INVALID_CHANNEL;
231 PERROR("Invalid flags specified.\n");
233 err = ME_ERRNO_INVALID_FLAGS;
237 spin_unlock(instance->ctrl_reg_lock);
238 spin_unlock(&instance->subdevice_lock);
245 static int me0600_dio_io_single_write(me_subdevice_t * subdevice,
248 int value, int time_out, int flags)
250 me0600_dio_subdevice_t *instance;
251 int err = ME_ERRNO_SUCCESS;
255 PDEBUG("executed.\n");
257 instance = (me0600_dio_subdevice_t *) subdevice;
261 spin_lock(&instance->subdevice_lock);
262 spin_lock(instance->ctrl_reg_lock);
265 case ME_IO_SINGLE_TYPE_DIO_BIT:
266 if ((channel >= 0) && (channel < 8)) {
269 ctrl_reg) & ((ME0600_DIO_CONFIG_BIT_OUT_0) <<
270 (instance->dio_idx * 2));
273 (ME0600_DIO_CONFIG_BIT_OUT_0 <<
274 (instance->dio_idx * 2))) {
275 byte = inb(instance->port_reg);
278 byte |= 0x1 << channel;
280 byte &= ~(0x1 << channel);
282 outb(byte, instance->port_reg);
284 PERROR("Port not in output or input mode.\n");
285 err = ME_ERRNO_PREVIOUS_CONFIG;
288 PERROR("Invalid bit number specified.\n");
289 err = ME_ERRNO_INVALID_CHANNEL;
294 case ME_IO_SINGLE_NO_FLAGS:
295 case ME_IO_SINGLE_TYPE_DIO_BYTE:
299 ctrl_reg) & ((ME0600_DIO_CONFIG_BIT_OUT_0) <<
300 (instance->dio_idx * 2));
303 (ME0600_DIO_CONFIG_BIT_OUT_0 <<
304 (instance->dio_idx * 2))) {
305 outb(value, instance->port_reg);
307 PERROR("Port not in output or input mode.\n");
308 err = ME_ERRNO_PREVIOUS_CONFIG;
311 PERROR("Invalid byte number specified.\n");
312 err = ME_ERRNO_INVALID_CHANNEL;
318 PERROR("Invalid flags specified.\n");
320 err = ME_ERRNO_INVALID_FLAGS;
324 spin_unlock(instance->ctrl_reg_lock);
325 spin_unlock(&instance->subdevice_lock);
332 static int me0600_dio_query_number_channels(me_subdevice_t * subdevice,
335 PDEBUG("executed.\n");
337 return ME_ERRNO_SUCCESS;
340 static int me0600_dio_query_subdevice_type(me_subdevice_t * subdevice,
341 int *type, int *subtype)
343 PDEBUG("executed.\n");
345 *subtype = ME_SUBTYPE_SINGLE;
346 return ME_ERRNO_SUCCESS;
349 static int me0600_dio_query_subdevice_caps(me_subdevice_t * subdevice,
352 PDEBUG("executed.\n");
353 *caps = ME_CAPS_DIO_DIR_BYTE;
354 return ME_ERRNO_SUCCESS;
357 me0600_dio_subdevice_t *me0600_dio_constructor(uint32_t reg_base,
358 unsigned int dio_idx,
359 spinlock_t * ctrl_reg_lock)
361 me0600_dio_subdevice_t *subdevice;
364 PDEBUG("executed.\n");
366 /* Allocate memory for subdevice instance */
367 subdevice = kmalloc(sizeof(me0600_dio_subdevice_t), GFP_KERNEL);
370 PERROR("Cannot get memory for subdevice instance.\n");
374 memset(subdevice, 0, sizeof(me0600_dio_subdevice_t));
376 /* Initialize subdevice base class */
377 err = me_subdevice_init(&subdevice->base);
380 PERROR("Cannot initialize subdevice base class instance.\n");
384 // Initialize spin locks.
385 spin_lock_init(&subdevice->subdevice_lock);
387 subdevice->ctrl_reg_lock = ctrl_reg_lock;
389 /* Save digital i/o index */
390 subdevice->dio_idx = dio_idx;
392 /* Save the subdevice index */
393 subdevice->ctrl_reg = reg_base + ME0600_DIO_CONFIG_REG;
394 subdevice->port_reg = reg_base + ME0600_DIO_PORT_REG + dio_idx;
395 #ifdef MEDEBUG_DEBUG_REG
396 subdevice->reg_base = reg_base;
399 /* Overload base class methods. */
400 subdevice->base.me_subdevice_io_reset_subdevice =
401 me0600_dio_io_reset_subdevice;
402 subdevice->base.me_subdevice_io_single_config =
403 me0600_dio_io_single_config;
404 subdevice->base.me_subdevice_io_single_read = me0600_dio_io_single_read;
405 subdevice->base.me_subdevice_io_single_write =
406 me0600_dio_io_single_write;
407 subdevice->base.me_subdevice_query_number_channels =
408 me0600_dio_query_number_channels;
409 subdevice->base.me_subdevice_query_subdevice_type =
410 me0600_dio_query_subdevice_type;
411 subdevice->base.me_subdevice_query_subdevice_caps =
412 me0600_dio_query_subdevice_caps;