4 * @brief ME-6000 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 "me6000_dio_reg.h"
48 #include "me6000_dio.h"
58 static int me6000_dio_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me6000_dio_subdevice_t *instance;
64 PDEBUG("executed.\n");
66 instance = (me6000_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("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
86 instance->ctrl_reg - instance->reg_base, 0x00);
87 spin_unlock(&instance->subdevice_lock);
91 return ME_ERRNO_SUCCESS;
94 static int me6000_dio_io_single_config(me_subdevice_t *subdevice,
100 int trig_type, int trig_edge, int flags)
102 me6000_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 = (me6000_dio_subdevice_t *) subdevice;
114 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
115 spin_lock(instance->ctrl_reg_lock);
116 mode = inb(instance->ctrl_reg);
118 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
119 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
121 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
123 ~((ME6000_DIO_CTRL_BIT_MODE_0 |
124 ME6000_DIO_CTRL_BIT_MODE_1) <<
125 (instance->dio_idx * 2));
126 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
128 ~((ME6000_DIO_CTRL_BIT_MODE_0 |
129 ME6000_DIO_CTRL_BIT_MODE_1) <<
130 (instance->dio_idx * 2));
132 ME6000_DIO_CTRL_BIT_MODE_0 << (instance->
136 ("Invalid port configuration specified.\n");
137 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
140 PERROR("Invalid channel number.\n");
141 err = ME_ERRNO_INVALID_CHANNEL;
146 PERROR("Invalid flags.\n");
147 err = ME_ERRNO_INVALID_FLAGS;
151 outb(mode, instance->ctrl_reg);
152 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
154 instance->ctrl_reg - instance->reg_base, mode);
156 spin_unlock(instance->ctrl_reg_lock);
157 spin_unlock(&instance->subdevice_lock);
164 static int me6000_dio_io_single_read(me_subdevice_t *subdevice,
167 int *value, int time_out, int flags)
169 me6000_dio_subdevice_t *instance;
170 int err = ME_ERRNO_SUCCESS;
173 PDEBUG("executed.\n");
175 instance = (me6000_dio_subdevice_t *) subdevice;
177 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
178 spin_lock(instance->ctrl_reg_lock);
180 case ME_IO_SINGLE_TYPE_DIO_BIT:
181 if ((channel >= 0) && (channel < 8)) {
184 ctrl_reg) & ((ME6000_DIO_CTRL_BIT_MODE_0 |
185 ME6000_DIO_CTRL_BIT_MODE_1) <<
186 (instance->dio_idx * 2));
188 (ME6000_DIO_CTRL_BIT_MODE_0 <<
189 (instance->dio_idx * 2))) || !mode) {
191 inb(instance->port_reg) & (0x1 << channel);
193 PERROR("Port not in output or input mode.\n");
194 err = ME_ERRNO_PREVIOUS_CONFIG;
197 PERROR("Invalid bit number specified.\n");
198 err = ME_ERRNO_INVALID_CHANNEL;
202 case ME_IO_SINGLE_NO_FLAGS:
203 case ME_IO_SINGLE_TYPE_DIO_BYTE:
207 ctrl_reg) & ((ME6000_DIO_CTRL_BIT_MODE_0 |
208 ME6000_DIO_CTRL_BIT_MODE_1) <<
209 (instance->dio_idx * 2));
211 (ME6000_DIO_CTRL_BIT_MODE_0 <<
212 (instance->dio_idx * 2))) || !mode) {
213 *value = inb(instance->port_reg) & 0x00FF;
215 PERROR("Port not in output or input mode.\n");
216 err = ME_ERRNO_PREVIOUS_CONFIG;
219 PERROR("Invalid byte number specified.\n");
220 err = ME_ERRNO_INVALID_CHANNEL;
225 PERROR("Invalid flags specified.\n");
226 err = ME_ERRNO_INVALID_FLAGS;
228 spin_unlock(instance->ctrl_reg_lock);
229 spin_unlock(&instance->subdevice_lock);
236 static int me6000_dio_io_single_write(me_subdevice_t *subdevice,
239 int value, int time_out, int flags)
241 me6000_dio_subdevice_t *instance;
242 int err = ME_ERRNO_SUCCESS;
246 PDEBUG("executed.\n");
248 instance = (me6000_dio_subdevice_t *) subdevice;
250 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
251 spin_lock(instance->ctrl_reg_lock);
253 case ME_IO_SINGLE_TYPE_DIO_BIT:
254 if ((channel >= 0) && (channel < 8)) {
257 ctrl_reg) & ((ME6000_DIO_CTRL_BIT_MODE_0 |
258 ME6000_DIO_CTRL_BIT_MODE_1) <<
259 (instance->dio_idx * 2));
262 (ME6000_DIO_CTRL_BIT_MODE_0 <<
263 (instance->dio_idx * 2))) {
264 byte = inb(instance->port_reg) & 0x00FF;
267 byte |= 0x1 << channel;
269 byte &= ~(0x1 << channel);
271 outb(byte, instance->port_reg);
273 PERROR("Port not in output or input mode.\n");
274 err = ME_ERRNO_PREVIOUS_CONFIG;
277 PERROR("Invalid bit number specified.\n");
278 err = ME_ERRNO_INVALID_CHANNEL;
282 case ME_IO_SINGLE_NO_FLAGS:
283 case ME_IO_SINGLE_TYPE_DIO_BYTE:
287 ctrl_reg) & ((ME6000_DIO_CTRL_BIT_MODE_0 |
288 ME6000_DIO_CTRL_BIT_MODE_1) <<
289 (instance->dio_idx * 2));
292 (ME6000_DIO_CTRL_BIT_MODE_0 <<
293 (instance->dio_idx * 2))) {
294 outb(value, instance->port_reg);
296 PERROR("Port not in output or input mode.\n");
297 err = ME_ERRNO_PREVIOUS_CONFIG;
300 PERROR("Invalid byte number specified.\n");
301 err = ME_ERRNO_INVALID_CHANNEL;
306 PERROR("Invalid flags specified.\n");
307 err = ME_ERRNO_INVALID_FLAGS;
309 spin_unlock(instance->ctrl_reg_lock);
310 spin_unlock(&instance->subdevice_lock);
317 static int me6000_dio_query_number_channels(me_subdevice_t *subdevice,
320 PDEBUG("executed.\n");
322 return ME_ERRNO_SUCCESS;
325 static int me6000_dio_query_subdevice_type(me_subdevice_t *subdevice,
326 int *type, int *subtype)
328 PDEBUG("executed.\n");
330 *subtype = ME_SUBTYPE_SINGLE;
331 return ME_ERRNO_SUCCESS;
334 static int me6000_dio_query_subdevice_caps(me_subdevice_t *subdevice,
337 PDEBUG("executed.\n");
338 *caps = ME_CAPS_DIO_DIR_BYTE;
339 return ME_ERRNO_SUCCESS;
342 me6000_dio_subdevice_t *me6000_dio_constructor(uint32_t reg_base,
343 unsigned int dio_idx,
344 spinlock_t *ctrl_reg_lock)
346 me6000_dio_subdevice_t *subdevice;
349 PDEBUG("executed.\n");
351 /* Allocate memory for subdevice instance */
352 subdevice = kmalloc(sizeof(me6000_dio_subdevice_t), GFP_KERNEL);
355 PERROR("Cannot get memory for subdevice instance.\n");
359 memset(subdevice, 0, sizeof(me6000_dio_subdevice_t));
361 /* Initialize subdevice base class */
362 err = me_subdevice_init(&subdevice->base);
364 PERROR("Cannot initialize subdevice base class instance.\n");
369 /* Set the subdevice ports */
370 subdevice->ctrl_reg = reg_base + ME6000_DIO_CTRL_REG;
373 subdevice->port_reg = reg_base + ME6000_DIO_PORT_0_REG;
376 subdevice->port_reg = reg_base + ME6000_DIO_PORT_1_REG;
379 err = ME_ERRNO_INVALID_SUBDEVICE;
383 PERROR("Cannot initialize subdevice base class instance.\n");
387 // Initialize spin locks.
388 spin_lock_init(&subdevice->subdevice_lock);
390 subdevice->ctrl_reg_lock = ctrl_reg_lock;
392 /* Save digital i/o index */
393 subdevice->dio_idx = 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 me6000_dio_io_reset_subdevice;
402 subdevice->base.me_subdevice_io_single_config =
403 me6000_dio_io_single_config;
404 subdevice->base.me_subdevice_io_single_read = me6000_dio_io_single_read;
405 subdevice->base.me_subdevice_io_single_write =
406 me6000_dio_io_single_write;
407 subdevice->base.me_subdevice_query_number_channels =
408 me6000_dio_query_number_channels;
409 subdevice->base.me_subdevice_query_subdevice_type =
410 me6000_dio_query_subdevice_type;
411 subdevice->base.me_subdevice_query_subdevice_caps =
412 me6000_dio_query_subdevice_caps;