2 * @file me4600_ext_irq.c
4 * @brief ME-4000 external interrupt 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>
39 #include <linux/interrupt.h>
41 #include <linux/types.h>
43 #include "medefines.h"
44 #include "meinternal.h"
49 #include "me4600_reg.h"
50 #include "me4600_ai_reg.h"
51 #include "me4600_ext_irq_reg.h"
52 #include "me4600_ext_irq.h"
62 static int me4600_ext_irq_io_irq_start(me_subdevice_t *subdevice,
66 int irq_edge, int irq_arg, int flags)
68 me4600_ext_irq_subdevice_t *instance;
69 int err = ME_ERRNO_SUCCESS;
70 unsigned long cpu_flags;
73 PDEBUG("executed.\n");
75 instance = (me4600_ext_irq_subdevice_t *) subdevice;
77 if (flags & ~ME_IO_IRQ_START_DIO_BIT) {
78 PERROR("Invalid flag specified.\n");
79 return ME_ERRNO_INVALID_FLAGS;
82 if ((irq_edge != ME_IRQ_EDGE_RISING)
83 && (irq_edge != ME_IRQ_EDGE_FALLING)
84 && (irq_edge != ME_IRQ_EDGE_ANY)
86 PERROR("Invalid irq edge specified.\n");
87 return ME_ERRNO_INVALID_IRQ_EDGE;
90 if (irq_source != ME_IRQ_SOURCE_DIO_LINE) {
91 PERROR("Invalid irq source specified.\n");
92 return ME_ERRNO_INVALID_IRQ_SOURCE;
96 PERROR("Invalid channel specified.\n");
97 return ME_ERRNO_INVALID_CHANNEL;
102 spin_lock(&instance->subdevice_lock);
103 tmp = 0x0; //inl(instance->ext_irq_config_reg);
105 if (irq_edge == ME_IRQ_EDGE_RISING) {
106 //tmp &= ~ME4600_EXT_IRQ_CONFIG_MASK;
107 //tmp |= ME4600_EXT_IRQ_CONFIG_MASK_RISING;
108 } else if (irq_edge == ME_IRQ_EDGE_FALLING) {
109 //tmp &= ~ME4600_EXT_IRQ_CONFIG_MASK;
110 //tmp |= ME4600_EXT_IRQ_CONFIG_MASK_FALLING;
111 tmp = ME4600_EXT_IRQ_CONFIG_MASK_FALLING;
112 } else if (irq_edge == ME_IRQ_EDGE_ANY) {
113 //tmp &= ~ME4600_EXT_IRQ_CONFIG_MASK;
114 //tmp |= ME4600_EXT_IRQ_CONFIG_MASK_ANY;
115 tmp = ME4600_EXT_IRQ_CONFIG_MASK_ANY;
118 outl(tmp, instance->ext_irq_config_reg);
119 PDEBUG_REG("ext_irq_config_reg outl(0x%lX+0x%lX)=0x%x\n",
121 instance->ext_irq_config_reg - instance->reg_base, tmp);
123 spin_lock_irqsave(instance->ctrl_reg_lock, cpu_flags);
124 tmp = inl(instance->ctrl_reg);
125 tmp &= ~(ME4600_AI_CTRL_BIT_EX_IRQ | ME4600_AI_CTRL_BIT_EX_IRQ_RESET);
126 tmp |= ME4600_AI_CTRL_BIT_EX_IRQ;
127 outl(tmp, instance->ctrl_reg);
128 spin_unlock_irqrestore(instance->ctrl_reg_lock, cpu_flags);
130 spin_unlock(&instance->subdevice_lock);
137 static int me4600_ext_irq_io_irq_wait(me_subdevice_t *subdevice,
141 int *value, int time_out, int flags)
143 me4600_ext_irq_subdevice_t *instance;
144 int err = ME_ERRNO_SUCCESS;
146 unsigned long cpu_flags;
148 PDEBUG("executed.\n");
150 instance = (me4600_ext_irq_subdevice_t *) subdevice;
153 PERROR("Invalid flag specified.\n");
154 return ME_ERRNO_INVALID_FLAGS;
158 PERROR("Invalid channel specified.\n");
159 return ME_ERRNO_INVALID_CHANNEL;
163 PERROR("Invalid time_out specified.\n");
164 return ME_ERRNO_INVALID_TIMEOUT;
168 t = (time_out * HZ) / 1000;
176 if (instance->rised <= 0) {
179 t = wait_event_interruptible_timeout(instance->
186 ("Wait on external interrupt timed out.\n");
187 err = ME_ERRNO_TIMEOUT;
190 wait_event_interruptible(instance->wait_queue,
191 (instance->rised != 0));
194 if (instance->rised < 0) {
195 PERROR("Wait on interrupt aborted by user.\n");
196 err = ME_ERRNO_CANCELLED;
200 if (signal_pending(current)) {
201 PERROR("Wait on external interrupt aborted by signal.\n");
202 err = ME_ERRNO_SIGNAL;
205 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
207 *irq_count = instance->count;
208 *value = instance->value;
209 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
216 static int me4600_ext_irq_io_irq_stop(me_subdevice_t *subdevice,
218 int channel, int flags)
220 me4600_ext_irq_subdevice_t *instance;
221 int err = ME_ERRNO_SUCCESS;
222 unsigned long cpu_flags;
225 PDEBUG("executed.\n");
227 instance = (me4600_ext_irq_subdevice_t *) subdevice;
230 PERROR("Invalid flag specified.\n");
231 return ME_ERRNO_INVALID_FLAGS;
235 PERROR("Invalid channel specified.\n");
236 return ME_ERRNO_INVALID_CHANNEL;
241 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
242 spin_lock(instance->ctrl_reg_lock);
243 tmp = inl(instance->ctrl_reg);
244 tmp &= ~(ME4600_AI_CTRL_BIT_EX_IRQ | ME4600_AI_CTRL_BIT_EX_IRQ_RESET);
245 outl(tmp, instance->ctrl_reg);
246 PDEBUG_REG("ctrl_regv outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
247 instance->ctrl_reg - instance->reg_base, tmp);
248 spin_unlock(instance->ctrl_reg_lock);
249 instance->rised = -1;
250 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
251 wake_up_interruptible_all(&instance->wait_queue);
258 static int me4600_ext_irq_io_reset_subdevice(me_subdevice_t *subdevice,
259 struct file *filep, int flags)
261 me4600_ext_irq_subdevice_t *instance;
262 unsigned long cpu_flags;
265 PDEBUG("executed.\n");
267 instance = (me4600_ext_irq_subdevice_t *) subdevice;
270 PERROR("Invalid flag specified.\n");
271 return ME_ERRNO_INVALID_FLAGS;
276 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
277 spin_lock(instance->ctrl_reg_lock);
278 tmp = inl(instance->ctrl_reg);
279 tmp &= ~(ME4600_AI_CTRL_BIT_EX_IRQ | ME4600_AI_CTRL_BIT_EX_IRQ_RESET);
280 outl(tmp, instance->ctrl_reg);
281 PDEBUG_REG("ctrl_regv outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
282 instance->ctrl_reg - instance->reg_base, tmp);
283 spin_unlock(instance->ctrl_reg_lock);
284 instance->rised = -1;
286 outl(ME4600_EXT_IRQ_CONFIG_MASK_ANY, instance->ext_irq_config_reg);
287 PDEBUG_REG("ext_irq_config_reg outl(0x%lX+0x%lX)=0x%x\n",
289 instance->ext_irq_config_reg - instance->reg_base,
290 ME4600_EXT_IRQ_CONFIG_MASK_ANY);
291 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
292 wake_up_interruptible_all(&instance->wait_queue);
296 return ME_ERRNO_SUCCESS;
299 static void me4600_ext_irq_destructor(struct me_subdevice *subdevice)
301 me4600_ext_irq_subdevice_t *instance;
303 PDEBUG("executed.\n");
304 instance = (me4600_ext_irq_subdevice_t *) subdevice;
305 me_subdevice_deinit(&instance->base);
306 free_irq(instance->irq, instance);
310 static int me4600_ext_irq_query_number_channels(me_subdevice_t *subdevice,
313 PDEBUG("executed.\n");
315 return ME_ERRNO_SUCCESS;
318 static int me4600_ext_irq_query_subdevice_type(me_subdevice_t *subdevice,
319 int *type, int *subtype)
321 PDEBUG("executed.\n");
322 *type = ME_TYPE_EXT_IRQ;
323 *subtype = ME_SUBTYPE_SINGLE;
324 return ME_ERRNO_SUCCESS;
327 static int me4600_ext_irq_query_subdevice_caps(me_subdevice_t *subdevice,
330 PDEBUG("executed.\n");
332 ME_CAPS_EXT_IRQ_EDGE_RISING | ME_CAPS_EXT_IRQ_EDGE_FALLING |
333 ME_CAPS_EXT_IRQ_EDGE_ANY;
334 return ME_ERRNO_SUCCESS;
337 static irqreturn_t me4600_ext_irq_isr(int irq, void *dev_id)
339 me4600_ext_irq_subdevice_t *instance;
343 instance = (me4600_ext_irq_subdevice_t *) dev_id;
345 if (irq != instance->irq) {
346 PERROR("Incorrect interrupt num: %d.\n", irq);
350 irq_status = inl(instance->irq_status_reg);
351 if (!(irq_status & ME4600_IRQ_STATUS_BIT_EX)) {
352 PINFO("%ld Shared interrupt. %s(): irq_status_reg=0x%04X\n",
353 jiffies, __func__, irq_status);
357 PDEBUG("executed.\n");
359 spin_lock(&instance->subdevice_lock);
361 instance->value = inl(instance->ext_irq_value_reg);
364 spin_lock(instance->ctrl_reg_lock);
365 ctrl = inl(instance->ctrl_reg);
366 ctrl |= ME4600_AI_CTRL_BIT_EX_IRQ_RESET;
367 outl(ctrl, instance->ctrl_reg);
368 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
369 instance->ctrl_reg - instance->reg_base, ctrl);
370 ctrl &= ~ME4600_AI_CTRL_BIT_EX_IRQ_RESET;
371 outl(ctrl, instance->ctrl_reg);
372 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
373 instance->ctrl_reg - instance->reg_base, ctrl);
374 spin_unlock(instance->ctrl_reg_lock);
376 spin_unlock(&instance->subdevice_lock);
377 wake_up_interruptible_all(&instance->wait_queue);
382 me4600_ext_irq_subdevice_t *me4600_ext_irq_constructor(uint32_t reg_base,
387 me4600_ext_irq_subdevice_t *subdevice;
390 PDEBUG("executed.\n");
392 /* Allocate memory for subdevice instance */
393 subdevice = kmalloc(sizeof(me4600_ext_irq_subdevice_t), GFP_KERNEL);
396 PERROR("Cannot get memory for subdevice instance.\n");
400 memset(subdevice, 0, sizeof(me4600_ext_irq_subdevice_t));
402 /* Initialize subdevice base class */
403 err = me_subdevice_init(&subdevice->base);
406 PERROR("Cannot initialize subdevice base class instance.\n");
410 // Initialize spin locks.
411 spin_lock_init(&subdevice->subdevice_lock);
413 subdevice->ctrl_reg_lock = ctrl_reg_lock;
415 /* Initialize wait queue */
416 init_waitqueue_head(&subdevice->wait_queue);
418 /* Register interrupt */
419 subdevice->irq = irq;
421 if (request_irq(subdevice->irq, me4600_ext_irq_isr,
422 IRQF_DISABLED | IRQF_SHARED,
423 ME4600_NAME, subdevice)) {
424 PERROR("Cannot register interrupt.\n");
428 PINFO("Registered irq=%d.\n", subdevice->irq);
430 /* Initialize registers */
431 subdevice->irq_status_reg = reg_base + ME4600_IRQ_STATUS_REG;
432 subdevice->ctrl_reg = reg_base + ME4600_AI_CTRL_REG;
433 subdevice->ext_irq_config_reg = reg_base + ME4600_EXT_IRQ_CONFIG_REG;
434 subdevice->ext_irq_value_reg = reg_base + ME4600_EXT_IRQ_VALUE_REG;
435 #ifdef MEDEBUG_DEBUG_REG
436 subdevice->reg_base = reg_base;
439 /* Override base class methods. */
440 subdevice->base.me_subdevice_destructor = me4600_ext_irq_destructor;
441 subdevice->base.me_subdevice_io_reset_subdevice =
442 me4600_ext_irq_io_reset_subdevice;
443 subdevice->base.me_subdevice_io_irq_start = me4600_ext_irq_io_irq_start;
444 subdevice->base.me_subdevice_io_irq_wait = me4600_ext_irq_io_irq_wait;
445 subdevice->base.me_subdevice_io_irq_stop = me4600_ext_irq_io_irq_stop;
446 subdevice->base.me_subdevice_query_number_channels =
447 me4600_ext_irq_query_number_channels;
448 subdevice->base.me_subdevice_query_subdevice_type =
449 me4600_ext_irq_query_subdevice_type;
450 subdevice->base.me_subdevice_query_subdevice_caps =
451 me4600_ext_irq_query_subdevice_caps;
453 subdevice->rised = 0;
454 subdevice->count = 0;