4 * @brief ME-1000 DIO subdevice instance.
5 * @note Copyright (C) 2006 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
11 * Copyright (C) 2006 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 "me1000_dio_reg.h"
48 #include "me1000_dio.h"
53 #define ME1000_DIO_MAGIC_NUMBER 0x1000 /**< The magic number of the class structure. */
59 static int me1000_dio_io_reset_subdevice(struct me_subdevice *subdevice,
60 struct file *filep, int flags)
62 me1000_dio_subdevice_t *instance;
65 PDEBUG("executed.\n");
67 instance = (me1000_dio_subdevice_t *) subdevice;
70 PERROR("Invalid flag specified.\n");
71 return ME_ERRNO_INVALID_FLAGS;
76 spin_lock(&instance->subdevice_lock);
77 spin_lock(instance->ctrl_reg_lock);
78 tmp = inl(instance->ctrl_reg);
79 tmp &= ~(0x1 << instance->dio_idx);
80 outl(tmp, instance->ctrl_reg);
81 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
82 instance->ctrl_reg - instance->reg_base, tmp);
83 spin_unlock(instance->ctrl_reg_lock);
85 outl(0x00000000, instance->port_reg);
86 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
87 instance->ctrl_reg - instance->reg_base, 0);
88 spin_unlock(&instance->subdevice_lock);
92 return ME_ERRNO_SUCCESS;
95 static int me1000_dio_io_single_config(struct me_subdevice *subdevice,
101 int trig_type, int trig_edge, int flags)
103 me1000_dio_subdevice_t *instance;
104 int err = ME_ERRNO_SUCCESS;
107 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
108 | ME_IO_SINGLE_CONFIG_DIO_WORD |
109 ME_IO_SINGLE_CONFIG_DIO_DWORD);
111 PDEBUG("executed.\n");
113 instance = (me1000_dio_subdevice_t *) subdevice;
117 spin_lock(&instance->subdevice_lock);
118 spin_lock(instance->ctrl_reg_lock);
119 ctrl = inl(instance->ctrl_reg);
122 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
123 case ME_IO_SINGLE_CONFIG_DIO_DWORD:
125 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
126 ctrl &= ~(0x1 << instance->dio_idx);
127 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
128 ctrl |= 0x1 << instance->dio_idx;
130 PERROR("Invalid port direction.\n");
131 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
134 PERROR("Invalid channel number.\n");
135 err = ME_ERRNO_INVALID_CHANNEL;
140 PERROR("Invalid flags.\n");
141 err = ME_ERRNO_INVALID_FLAGS;
145 outl(ctrl, instance->ctrl_reg);
146 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
148 instance->ctrl_reg - instance->reg_base, ctrl);
150 spin_unlock(instance->ctrl_reg_lock);
151 spin_unlock(&instance->subdevice_lock);
158 static int me1000_dio_io_single_read(struct me_subdevice *subdevice,
161 int *value, int time_out, int flags)
163 me1000_dio_subdevice_t *instance;
164 int err = ME_ERRNO_SUCCESS;
166 PDEBUG("executed.\n");
168 instance = (me1000_dio_subdevice_t *) subdevice;
172 spin_lock(&instance->subdevice_lock);
174 case ME_IO_SINGLE_TYPE_DIO_BIT:
175 if ((channel >= 0) && (channel < 32)) {
176 *value = inl(instance->port_reg) & (0x1 << channel);
178 PERROR("Invalid bit number.\n");
179 err = ME_ERRNO_INVALID_CHANNEL;
183 case ME_IO_SINGLE_TYPE_DIO_BYTE:
184 if ((channel >= 0) && (channel < 4)) {
186 (inl(instance->port_reg) >> (channel * 8)) & 0xFF;
188 PERROR("Invalid byte number.\n");
189 err = ME_ERRNO_INVALID_CHANNEL;
193 case ME_IO_SINGLE_TYPE_DIO_WORD:
194 if ((channel >= 0) && (channel < 2)) {
196 (inl(instance->port_reg) >> (channel * 16)) &
199 PERROR("Invalid word number.\n");
200 err = ME_ERRNO_INVALID_CHANNEL;
204 case ME_IO_SINGLE_NO_FLAGS:
205 case ME_IO_SINGLE_TYPE_DIO_DWORD:
207 *value = inl(instance->port_reg);
209 PERROR("Invalid dword number.\n");
210 err = ME_ERRNO_INVALID_CHANNEL;
215 PERROR("Invalid flags specified.\n");
216 err = ME_ERRNO_INVALID_FLAGS;
218 spin_unlock(&instance->subdevice_lock);
225 static int me1000_dio_io_single_write(struct me_subdevice *subdevice,
228 int value, int time_out, int flags)
230 me1000_dio_subdevice_t *instance;
231 int err = ME_ERRNO_SUCCESS;
235 PDEBUG("executed.\n");
237 instance = (me1000_dio_subdevice_t *) subdevice;
239 ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
240 spin_lock(instance->ctrl_reg_lock);
241 config = inl(instance->ctrl_reg) & (0x1 << instance->dio_idx);
243 case ME_IO_SINGLE_TYPE_DIO_BIT:
244 if ((channel >= 0) && (channel < 32)) {
246 state = inl(instance->port_reg);
248 value ? (state | (0x1 << channel)) : (state
253 outl(state, instance->port_reg);
254 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
257 instance->reg_base, state);
259 PERROR("Port is not in output mode.\n");
260 err = ME_ERRNO_PREVIOUS_CONFIG;
263 PERROR("Invalid bit number.\n");
264 err = ME_ERRNO_INVALID_CHANNEL;
268 case ME_IO_SINGLE_TYPE_DIO_BYTE:
269 if ((channel >= 0) && (channel < 4)) {
271 state = inl(instance->port_reg);
272 state &= ~(0xFF << (channel * 8));
273 state |= (value & 0xFF) << (channel * 8);
274 outl(state, instance->port_reg);
275 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
278 instance->reg_base, state);
280 PERROR("Port is not in output mode.\n");
281 err = ME_ERRNO_PREVIOUS_CONFIG;
284 PERROR("Invalid byte number.\n");
285 err = ME_ERRNO_INVALID_CHANNEL;
289 case ME_IO_SINGLE_TYPE_DIO_WORD:
290 if ((channel >= 0) && (channel < 2)) {
292 state = inl(instance->port_reg);
293 state &= ~(0xFFFF << (channel * 16));
294 state |= (value & 0xFFFF) << (channel * 16);
295 outl(state, instance->port_reg);
296 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
299 instance->reg_base, state);
301 PERROR("Port is not in output mode.\n");
302 err = ME_ERRNO_PREVIOUS_CONFIG;
305 PERROR("Invalid word number.\n");
306 err = ME_ERRNO_INVALID_CHANNEL;
310 case ME_IO_SINGLE_NO_FLAGS:
311 case ME_IO_SINGLE_TYPE_DIO_DWORD:
314 outl(value, instance->port_reg);
315 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
318 instance->reg_base, value);
320 PERROR("Port is not in output mode.\n");
321 err = ME_ERRNO_PREVIOUS_CONFIG;
324 PERROR("Invalid dword number.\n");
325 err = ME_ERRNO_INVALID_CHANNEL;
330 PERROR("Invalid flags specified.\n");
331 err = ME_ERRNO_INVALID_FLAGS;
333 spin_unlock(instance->ctrl_reg_lock);
334 spin_unlock(&instance->subdevice_lock);
341 static int me1000_dio_query_number_channels(struct me_subdevice *subdevice,
344 PDEBUG("executed.\n");
345 *number = ME1000_DIO_NUMBER_CHANNELS;
346 return ME_ERRNO_SUCCESS;
349 static int me1000_dio_query_subdevice_type(struct me_subdevice *subdevice,
350 int *type, int *subtype)
352 PDEBUG("executed.\n");
354 *subtype = ME_SUBTYPE_SINGLE;
355 return ME_ERRNO_SUCCESS;
358 static int me1000_dio_query_subdevice_caps(struct me_subdevice *subdevice,
361 me1000_dio_subdevice_t *instance;
363 PDEBUG("executed.\n");
365 instance = (me1000_dio_subdevice_t *) subdevice;
367 *caps = ME_CAPS_DIO_DIR_DWORD;
369 return ME_ERRNO_SUCCESS;
372 me1000_dio_subdevice_t *me1000_dio_constructor(uint32_t reg_base,
373 unsigned int dio_idx,
374 spinlock_t * ctrl_reg_lock)
376 me1000_dio_subdevice_t *subdevice;
379 PDEBUG("executed.\n");
381 /* Allocate memory for subdevice instance */
382 subdevice = kmalloc(sizeof(me1000_dio_subdevice_t), GFP_KERNEL);
385 PERROR("Cannot get memory for ME-1000 DIO instance.\n");
389 memset(subdevice, 0, sizeof(me1000_dio_subdevice_t));
391 /* Check if counter index is out of range */
393 if (dio_idx >= ME1000_DIO_NUMBER_PORTS) {
394 PERROR("DIO index is out of range.\n");
399 /* Initialize subdevice base class */
400 err = me_subdevice_init(&subdevice->base);
403 PERROR("Cannot initialize subdevice base class instance.\n");
407 // Initialize spin locks.
408 spin_lock_init(&subdevice->subdevice_lock);
409 subdevice->ctrl_reg_lock = ctrl_reg_lock;
411 /* Save the DIO index */
412 subdevice->dio_idx = dio_idx;
414 /* Initialize registers. */
415 #ifdef MEDEBUG_DEBUG_REG
416 subdevice->reg_base = reg_base;
418 subdevice->ctrl_reg = reg_base + ME1000_PORT_MODE;
419 subdevice->port_reg =
420 reg_base + ME1000_PORT + (dio_idx * ME1000_PORT_STEP);
422 /* Override base class methods. */
423 subdevice->base.me_subdevice_io_reset_subdevice =
424 me1000_dio_io_reset_subdevice;
425 subdevice->base.me_subdevice_io_single_config =
426 me1000_dio_io_single_config;
427 subdevice->base.me_subdevice_io_single_read = me1000_dio_io_single_read;
428 subdevice->base.me_subdevice_io_single_write =
429 me1000_dio_io_single_write;
430 subdevice->base.me_subdevice_query_number_channels =
431 me1000_dio_query_number_channels;
432 subdevice->base.me_subdevice_query_subdevice_type =
433 me1000_dio_query_subdevice_type;
434 subdevice->base.me_subdevice_query_subdevice_caps =
435 me1000_dio_query_subdevice_caps;