4 * @brief ME-4000 digital 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_do.h"
58 static int me4600_do_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me4600_do_subdevice_t *instance;
64 PDEBUG("executed.\n");
66 instance = (me4600_do_subdevice_t *) subdevice;
69 PERROR("Invalid flag specified.\n");
70 return ME_ERRNO_INVALID_FLAGS;
75 /* Set port to output mode */
76 spin_lock(&instance->subdevice_lock);
77 spin_lock(instance->ctrl_reg_lock);
78 mode = inl(instance->ctrl_reg);
79 mode &= ~ME4600_DIO_CTRL_BIT_MODE_1; //0xFFFD
80 mode |= ME4600_DIO_CTRL_BIT_MODE_0; //0x1
81 outl(mode, instance->ctrl_reg);
82 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
83 instance->ctrl_reg - instance->reg_base, mode);
84 spin_unlock(instance->ctrl_reg_lock);
86 outl(0, instance->port_reg);
87 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
88 instance->port_reg - instance->reg_base, 0);
89 spin_unlock(&instance->subdevice_lock);
93 return ME_ERRNO_SUCCESS;
96 static int me4600_do_io_single_config(me_subdevice_t *subdevice,
102 int trig_type, int trig_edge, int flags)
104 me4600_do_subdevice_t *instance;
105 int err = ME_ERRNO_SUCCESS;
108 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
109 | ME_IO_SINGLE_CONFIG_DIO_WORD |
110 ME_IO_SINGLE_CONFIG_DIO_DWORD);
112 PDEBUG("executed.\n");
114 instance = (me4600_do_subdevice_t *) subdevice;
118 spin_lock(&instance->subdevice_lock);
119 spin_lock(instance->ctrl_reg_lock);
120 mode = inl(instance->ctrl_reg);
123 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
124 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
126 if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
127 mode &= ~(ME4600_DIO_CTRL_BIT_MODE_0 |
128 ME4600_DIO_CTRL_BIT_MODE_1);
129 mode |= (ME4600_DIO_CTRL_BIT_MODE_0);
130 } else if (single_config == ME_SINGLE_CONFIG_DIO_MUX32M) {
131 mode &= ~(ME4600_DIO_CTRL_BIT_MODE_0 |
132 ME4600_DIO_CTRL_BIT_MODE_1 |
133 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
134 ME4600_DIO_CTRL_BIT_FUNCTION_1 |
135 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
137 if (ref == ME_REF_DIO_FIFO_LOW) {
138 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
139 ME4600_DIO_CTRL_BIT_MODE_1 |
140 ME4600_DIO_CTRL_BIT_FUNCTION_1);
141 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
142 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
143 ME4600_DIO_CTRL_BIT_MODE_1 |
144 ME4600_DIO_CTRL_BIT_FUNCTION_1
146 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
149 ("Invalid port reference specified.\n");
150 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
152 } else if (single_config ==
153 ME_SINGLE_CONFIG_DIO_DEMUX32) {
155 ~(ME4600_DIO_CTRL_BIT_MODE_0 |
156 ME4600_DIO_CTRL_BIT_MODE_1 |
157 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
158 ME4600_DIO_CTRL_BIT_FUNCTION_1 |
159 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
161 if (ref == ME_REF_DIO_FIFO_LOW) {
162 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
163 ME4600_DIO_CTRL_BIT_MODE_1 |
164 ME4600_DIO_CTRL_BIT_FUNCTION_0);
165 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
166 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
167 ME4600_DIO_CTRL_BIT_MODE_1 |
168 ME4600_DIO_CTRL_BIT_FUNCTION_0
170 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
173 ("Invalid port reference specified.\n");
174 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
176 } else if (single_config ==
177 ME_SINGLE_CONFIG_DIO_BIT_PATTERN) {
179 ~(ME4600_DIO_CTRL_BIT_MODE_0 |
180 ME4600_DIO_CTRL_BIT_MODE_1 |
181 ME4600_DIO_CTRL_BIT_FUNCTION_0 |
182 ME4600_DIO_CTRL_BIT_FUNCTION_1 |
183 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
185 if (ref == ME_REF_DIO_FIFO_LOW) {
186 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
187 ME4600_DIO_CTRL_BIT_MODE_1);
188 } else if (ref == ME_REF_DIO_FIFO_HIGH) {
189 mode |= (ME4600_DIO_CTRL_BIT_MODE_0 |
190 ME4600_DIO_CTRL_BIT_MODE_1 |
191 ME4600_DIO_CTRL_BIT_FIFO_HIGH_0);
194 ("Invalid port reference specified.\n");
195 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
198 PERROR("Invalid port direction specified.\n");
199 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
202 PERROR("Invalid channel number.\n");
203 err = ME_ERRNO_INVALID_CHANNEL;
209 PERROR("Invalid flags specified.\n");
210 err = ME_ERRNO_INVALID_FLAGS;
214 outl(mode, instance->ctrl_reg);
215 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
217 instance->ctrl_reg - instance->reg_base, mode);
219 spin_unlock(instance->ctrl_reg_lock);
220 spin_unlock(&instance->subdevice_lock);
227 static int me4600_do_io_single_read(me_subdevice_t *subdevice,
230 int *value, int time_out, int flags)
232 me4600_do_subdevice_t *instance;
233 int err = ME_ERRNO_SUCCESS;
236 PDEBUG("executed.\n");
238 instance = (me4600_do_subdevice_t *) subdevice;
242 spin_lock(&instance->subdevice_lock);
243 spin_lock(instance->ctrl_reg_lock);
246 ctrl_reg) & (ME4600_DIO_CTRL_BIT_MODE_0 |
247 ME4600_DIO_CTRL_BIT_MODE_1);
249 if (mode == ME4600_DIO_CTRL_BIT_MODE_0) {
251 case ME_IO_SINGLE_TYPE_DIO_BIT:
252 if ((channel >= 0) && (channel < 8)) {
254 inl(instance->port_reg) & (0x1 << channel);
256 PERROR("Invalid bit number specified.\n");
257 err = ME_ERRNO_INVALID_CHANNEL;
261 case ME_IO_SINGLE_NO_FLAGS:
262 case ME_IO_SINGLE_TYPE_DIO_BYTE:
264 *value = inl(instance->port_reg) & 0xFF;
266 PERROR("Invalid byte number specified.\n");
267 err = ME_ERRNO_INVALID_CHANNEL;
272 PERROR("Invalid flags specified.\n");
273 err = ME_ERRNO_INVALID_FLAGS;
276 PERROR("Port not in output mode.\n");
277 err = ME_ERRNO_PREVIOUS_CONFIG;
279 spin_unlock(instance->ctrl_reg_lock);
280 spin_unlock(&instance->subdevice_lock);
287 static int me4600_do_io_single_write(me_subdevice_t *subdevice,
290 int value, int time_out, int flags)
292 me4600_do_subdevice_t *instance;
293 int err = ME_ERRNO_SUCCESS;
297 PDEBUG("executed.\n");
299 instance = (me4600_do_subdevice_t *) subdevice;
303 spin_lock(&instance->subdevice_lock);
304 spin_lock(instance->ctrl_reg_lock);
307 ctrl_reg) & (ME4600_DIO_CTRL_BIT_MODE_0 |
308 ME4600_DIO_CTRL_BIT_MODE_1);
310 if (mode == ME4600_DIO_CTRL_BIT_MODE_0) {
313 case ME_IO_SINGLE_TYPE_DIO_BIT:
314 if ((channel >= 0) && (channel < 8)) {
315 byte = inl(instance->port_reg) & 0xFF;
318 byte |= 0x1 << channel;
320 byte &= ~(0x1 << channel);
322 outl(byte, instance->port_reg);
324 PERROR("Invalid bit number specified.\n");
325 err = ME_ERRNO_INVALID_CHANNEL;
329 case ME_IO_SINGLE_NO_FLAGS:
330 case ME_IO_SINGLE_TYPE_DIO_BYTE:
332 outl(value, instance->port_reg);
334 PERROR("Invalid byte number specified.\n");
335 err = ME_ERRNO_INVALID_CHANNEL;
340 PERROR("Invalid flags specified.\n");
341 err = ME_ERRNO_INVALID_FLAGS;
344 PERROR("Port not in output mode.\n");
345 err = ME_ERRNO_PREVIOUS_CONFIG;
347 spin_unlock(instance->ctrl_reg_lock);
348 spin_unlock(&instance->subdevice_lock);
355 static int me4600_do_query_number_channels(me_subdevice_t *subdevice,
358 PDEBUG("executed.\n");
360 return ME_ERRNO_SUCCESS;
363 static int me4600_do_query_subdevice_type(me_subdevice_t *subdevice,
364 int *type, int *subtype)
366 PDEBUG("executed.\n");
368 *subtype = ME_SUBTYPE_SINGLE;
369 return ME_ERRNO_SUCCESS;
372 static int me4600_do_query_subdevice_caps(me_subdevice_t *subdevice, int *caps)
374 PDEBUG("executed.\n");
376 return ME_ERRNO_SUCCESS;
379 me4600_do_subdevice_t *me4600_do_constructor(uint32_t reg_base,
380 spinlock_t *ctrl_reg_lock)
382 me4600_do_subdevice_t *subdevice;
385 PDEBUG("executed.\n");
387 /* Allocate memory for subdevice instance */
388 subdevice = kmalloc(sizeof(me4600_do_subdevice_t), GFP_KERNEL);
391 PERROR("Cannot get memory for subdevice instance.\n");
395 memset(subdevice, 0, sizeof(me4600_do_subdevice_t));
397 /* Initialize subdevice base class */
398 err = me_subdevice_init(&subdevice->base);
401 PERROR("Cannot initialize subdevice base class instance.\n");
405 // Initialize spin locks.
406 spin_lock_init(&subdevice->subdevice_lock);
408 subdevice->ctrl_reg_lock = ctrl_reg_lock;
410 /* Save the subdevice index */
411 subdevice->ctrl_reg = reg_base + ME4600_DIO_CTRL_REG;
412 subdevice->port_reg = reg_base + ME4600_DIO_PORT_0_REG;
413 #ifdef MEDEBUG_DEBUG_REG
414 subdevice->reg_base = reg_base;
417 /* Overload base class methods. */
418 subdevice->base.me_subdevice_io_reset_subdevice =
419 me4600_do_io_reset_subdevice;
420 subdevice->base.me_subdevice_io_single_config =
421 me4600_do_io_single_config;
422 subdevice->base.me_subdevice_io_single_read = me4600_do_io_single_read;
423 subdevice->base.me_subdevice_io_single_write =
424 me4600_do_io_single_write;
425 subdevice->base.me_subdevice_query_number_channels =
426 me4600_do_query_number_channels;
427 subdevice->base.me_subdevice_query_subdevice_type =
428 me4600_do_query_subdevice_type;
429 subdevice->base.me_subdevice_query_subdevice_caps =
430 me4600_do_query_subdevice_caps;