4 * @brief ME-8200 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 "me8200_dio_reg.h"
48 #include "me8200_dio.h"
58 static int me8200_dio_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me8200_dio_subdevice_t *instance;
64 PDEBUG("executed.\n");
67 PERROR("Invalid flag specified.\n");
68 return ME_ERRNO_INVALID_FLAGS;
71 instance = (me8200_dio_subdevice_t *) subdevice;
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);
83 outb(0x00, instance->port_reg);
84 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
85 instance->port_reg - instance->reg_base, 0x00);
86 spin_unlock(&instance->subdevice_lock);
90 return ME_ERRNO_SUCCESS;
93 static int me8200_dio_io_single_config(me_subdevice_t *subdevice,
99 int trig_type, int trig_edge, int flags)
101 me8200_dio_subdevice_t *instance;
102 int err = ME_ERRNO_SUCCESS;
105 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
106 | ME_IO_SINGLE_CONFIG_DIO_WORD |
107 ME_IO_SINGLE_CONFIG_DIO_DWORD);
109 PDEBUG("executed.\n");
111 instance = (me8200_dio_subdevice_t *) subdevice;
115 spin_lock(&instance->subdevice_lock);
116 spin_lock(instance->ctrl_reg_lock);
117 mode = inb(instance->ctrl_reg);
119 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
120 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
122 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
124 ~((ME8200_DIO_CTRL_BIT_MODE_0 |
125 ME8200_DIO_CTRL_BIT_MODE_1) <<
126 (instance->dio_idx * 2));
127 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
129 ~((ME8200_DIO_CTRL_BIT_MODE_0 |
130 ME8200_DIO_CTRL_BIT_MODE_1) <<
131 (instance->dio_idx * 2));
133 ME8200_DIO_CTRL_BIT_MODE_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;
148 PERROR("Invalid flags.\n");
150 err = ME_ERRNO_INVALID_FLAGS;
154 outb(mode, instance->ctrl_reg);
155 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
157 instance->ctrl_reg - instance->reg_base, mode);
159 spin_unlock(instance->ctrl_reg_lock);
160 spin_unlock(&instance->subdevice_lock);
167 static int me8200_dio_io_single_read(me_subdevice_t *subdevice,
170 int *value, int time_out, int flags)
172 me8200_dio_subdevice_t *instance;
173 int err = ME_ERRNO_SUCCESS;
176 PDEBUG("executed.\n");
178 instance = (me8200_dio_subdevice_t *) subdevice;
182 spin_lock(&instance->subdevice_lock);
183 spin_lock(instance->ctrl_reg_lock);
185 case ME_IO_SINGLE_TYPE_DIO_BIT:
186 if ((channel >= 0) && (channel < 8)) {
189 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
190 ME8200_DIO_CTRL_BIT_MODE_1) <<
191 (instance->dio_idx * 2));
194 (ME8200_DIO_CTRL_BIT_MODE_0 <<
195 (instance->dio_idx * 2))) || !mode) {
198 port_reg) & (0x0001 << channel);
200 PERROR("Port not in output or input mode.\n");
201 err = ME_ERRNO_PREVIOUS_CONFIG;
204 PERROR("Invalid bit number specified.\n");
205 err = ME_ERRNO_INVALID_CHANNEL;
209 case ME_IO_SINGLE_NO_FLAGS:
210 case ME_IO_SINGLE_TYPE_DIO_BYTE:
214 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
215 ME8200_DIO_CTRL_BIT_MODE_1) <<
216 (instance->dio_idx * 2));
219 (ME8200_DIO_CTRL_BIT_MODE_0 <<
220 (instance->dio_idx * 2))) || !mode) {
221 *value = inb(instance->port_reg) & 0x00FF;
223 PERROR("Port not in output or input mode.\n");
224 err = ME_ERRNO_PREVIOUS_CONFIG;
227 PERROR("Invalid byte number specified.\n");
228 err = ME_ERRNO_INVALID_CHANNEL;
233 PERROR("Invalid flags specified.\n");
234 err = ME_ERRNO_INVALID_FLAGS;
236 spin_unlock(instance->ctrl_reg_lock);
237 spin_unlock(&instance->subdevice_lock);
244 static int me8200_dio_io_single_write(me_subdevice_t *subdevice,
247 int value, int time_out, int flags)
249 me8200_dio_subdevice_t *instance;
250 int err = ME_ERRNO_SUCCESS;
254 PDEBUG("executed.\n");
256 instance = (me8200_dio_subdevice_t *) subdevice;
260 spin_lock(&instance->subdevice_lock);
261 spin_lock(instance->ctrl_reg_lock);
263 case ME_IO_SINGLE_TYPE_DIO_BIT:
264 if ((channel >= 0) && (channel < 8)) {
267 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
268 ME8200_DIO_CTRL_BIT_MODE_1) <<
269 (instance->dio_idx * 2));
272 (ME8200_DIO_CTRL_BIT_MODE_0 <<
273 (instance->dio_idx * 2))) {
274 byte = inb(instance->port_reg);
277 byte |= 0x1 << channel;
279 byte &= ~(0x1 << channel);
281 outb(byte, instance->port_reg);
282 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
285 instance->reg_base, byte);
287 PERROR("Port not in output or input mode.\n");
288 err = ME_ERRNO_PREVIOUS_CONFIG;
291 PERROR("Invalid bit number specified.\n");
292 err = ME_ERRNO_INVALID_CHANNEL;
296 case ME_IO_SINGLE_NO_FLAGS:
297 case ME_IO_SINGLE_TYPE_DIO_BYTE:
301 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
302 ME8200_DIO_CTRL_BIT_MODE_1) <<
303 (instance->dio_idx * 2));
306 (ME8200_DIO_CTRL_BIT_MODE_0 <<
307 (instance->dio_idx * 2))) {
308 outb(value, instance->port_reg);
309 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
312 instance->reg_base, value);
314 PERROR("Port not in output or input mode.\n");
315 err = ME_ERRNO_PREVIOUS_CONFIG;
318 PERROR("Invalid byte number specified.\n");
319 err = ME_ERRNO_INVALID_CHANNEL;
324 PERROR("Invalid flags specified.\n");
325 err = ME_ERRNO_INVALID_FLAGS;
327 spin_unlock(instance->ctrl_reg_lock);
328 spin_unlock(&instance->subdevice_lock);
335 static int me8200_dio_query_number_channels(me_subdevice_t *subdevice,
338 PDEBUG("executed.\n");
340 return ME_ERRNO_SUCCESS;
343 static int me8200_dio_query_subdevice_type(me_subdevice_t *subdevice,
344 int *type, int *subtype)
346 PDEBUG("executed.\n");
348 *subtype = ME_SUBTYPE_SINGLE;
349 return ME_ERRNO_SUCCESS;
352 static int me8200_dio_query_subdevice_caps(me_subdevice_t *subdevice,
355 PDEBUG("executed.\n");
356 *caps = ME_CAPS_DIO_DIR_BYTE;
357 return ME_ERRNO_SUCCESS;
360 me8200_dio_subdevice_t *me8200_dio_constructor(uint32_t reg_base,
361 unsigned int dio_idx,
362 spinlock_t *ctrl_reg_lock)
364 me8200_dio_subdevice_t *subdevice;
367 PDEBUG("executed.\n");
369 /* Allocate memory for subdevice instance */
370 subdevice = kmalloc(sizeof(me8200_dio_subdevice_t), GFP_KERNEL);
373 PERROR("Cannot get memory for subdevice instance.\n");
377 memset(subdevice, 0, sizeof(me8200_dio_subdevice_t));
379 /* Initialize subdevice base class */
380 err = me_subdevice_init(&subdevice->base);
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 /* Save the subdevice index */
396 subdevice->ctrl_reg = reg_base + ME8200_DIO_CTRL_REG;
397 subdevice->port_reg = reg_base + ME8200_DIO_PORT_REG + dio_idx;
398 #ifdef MEDEBUG_DEBUG_REG
399 subdevice->reg_base = reg_base;
402 /* Overload base class methods. */
403 subdevice->base.me_subdevice_io_reset_subdevice =
404 me8200_dio_io_reset_subdevice;
405 subdevice->base.me_subdevice_io_single_config =
406 me8200_dio_io_single_config;
407 subdevice->base.me_subdevice_io_single_read = me8200_dio_io_single_read;
408 subdevice->base.me_subdevice_io_single_write =
409 me8200_dio_io_single_write;
410 subdevice->base.me_subdevice_query_number_channels =
411 me8200_dio_query_number_channels;
412 subdevice->base.me_subdevice_query_subdevice_type =
413 me8200_dio_query_subdevice_type;
414 subdevice->base.me_subdevice_query_subdevice_caps =
415 me8200_dio_query_subdevice_caps;