Linux 2.6.31-rc6
[linux-2.6] / drivers / staging / meilhaus / me4600_ext_irq.c
1 /**
2  * @file me4600_ext_irq.c
3  *
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)
8  */
9
10 /*
11  * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 /*
33  * Includes
34  */
35 #include <linux/module.h>
36
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/io.h>
41 #include <linux/types.h>
42
43 #include "medefines.h"
44 #include "meinternal.h"
45 #include "meerror.h"
46
47 #include "medebug.h"
48 #include "meids.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"
53
54 /*
55  * Defines
56  */
57
58 /*
59  * Functions
60  */
61
62 static int me4600_ext_irq_io_irq_start(me_subdevice_t *subdevice,
63                                        struct file *filep,
64                                        int channel,
65                                        int irq_source,
66                                        int irq_edge, int irq_arg, int flags)
67 {
68         me4600_ext_irq_subdevice_t *instance;
69         int err = ME_ERRNO_SUCCESS;
70         unsigned long cpu_flags;
71         uint32_t tmp;
72
73         PDEBUG("executed.\n");
74
75         instance = (me4600_ext_irq_subdevice_t *) subdevice;
76
77         if (flags & ~ME_IO_IRQ_START_DIO_BIT) {
78                 PERROR("Invalid flag specified.\n");
79                 return ME_ERRNO_INVALID_FLAGS;
80         }
81
82         if ((irq_edge != ME_IRQ_EDGE_RISING)
83             && (irq_edge != ME_IRQ_EDGE_FALLING)
84             && (irq_edge != ME_IRQ_EDGE_ANY)
85             ) {
86                 PERROR("Invalid irq edge specified.\n");
87                 return ME_ERRNO_INVALID_IRQ_EDGE;
88         }
89
90         if (irq_source != ME_IRQ_SOURCE_DIO_LINE) {
91                 PERROR("Invalid irq source specified.\n");
92                 return ME_ERRNO_INVALID_IRQ_SOURCE;
93         }
94
95         if (channel) {
96                 PERROR("Invalid channel specified.\n");
97                 return ME_ERRNO_INVALID_CHANNEL;
98         }
99
100         ME_SUBDEVICE_ENTER;
101
102         spin_lock(&instance->subdevice_lock);
103         tmp = 0x0;              //inl(instance->ext_irq_config_reg);
104
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;
116         }
117
118         outl(tmp, instance->ext_irq_config_reg);
119         PDEBUG_REG("ext_irq_config_reg outl(0x%lX+0x%lX)=0x%x\n",
120                    instance->reg_base,
121                    instance->ext_irq_config_reg - instance->reg_base, tmp);
122
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);
129         instance->rised = 0;
130         spin_unlock(&instance->subdevice_lock);
131
132         ME_SUBDEVICE_EXIT;
133
134         return err;
135 }
136
137 static int me4600_ext_irq_io_irq_wait(me_subdevice_t *subdevice,
138                                       struct file *filep,
139                                       int channel,
140                                       int *irq_count,
141                                       int *value, int time_out, int flags)
142 {
143         me4600_ext_irq_subdevice_t *instance;
144         int err = ME_ERRNO_SUCCESS;
145         long t = 0;
146         unsigned long cpu_flags;
147
148         PDEBUG("executed.\n");
149
150         instance = (me4600_ext_irq_subdevice_t *) subdevice;
151
152         if (flags) {
153                 PERROR("Invalid flag specified.\n");
154                 return ME_ERRNO_INVALID_FLAGS;
155         }
156
157         if (channel) {
158                 PERROR("Invalid channel specified.\n");
159                 return ME_ERRNO_INVALID_CHANNEL;
160         }
161
162         if (time_out < 0) {
163                 PERROR("Invalid time_out specified.\n");
164                 return ME_ERRNO_INVALID_TIMEOUT;
165         }
166
167         if (time_out) {
168                 t = (time_out * HZ) / 1000;
169
170                 if (t == 0)
171                         t = 1;
172         }
173
174         ME_SUBDEVICE_ENTER;
175
176         if (instance->rised <= 0) {
177                 instance->rised = 0;
178                 if (time_out) {
179                         t = wait_event_interruptible_timeout(instance->
180                                                              wait_queue,
181                                                              (instance->rised !=
182                                                               0), t);
183
184                         if (t == 0) {
185                                 PERROR
186                                     ("Wait on external interrupt timed out.\n");
187                                 err = ME_ERRNO_TIMEOUT;
188                         }
189                 } else {
190                         wait_event_interruptible(instance->wait_queue,
191                                                  (instance->rised != 0));
192                 }
193
194                 if (instance->rised < 0) {
195                         PERROR("Wait on interrupt aborted by user.\n");
196                         err = ME_ERRNO_CANCELLED;
197                 }
198         }
199
200         if (signal_pending(current)) {
201                 PERROR("Wait on external interrupt aborted by signal.\n");
202                 err = ME_ERRNO_SIGNAL;
203         }
204
205         spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
206         instance->rised = 0;
207         *irq_count = instance->count;
208         *value = instance->value;
209         spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
210
211         ME_SUBDEVICE_EXIT;
212
213         return err;
214 }
215
216 static int me4600_ext_irq_io_irq_stop(me_subdevice_t *subdevice,
217                                       struct file *filep,
218                                       int channel, int flags)
219 {
220         me4600_ext_irq_subdevice_t *instance;
221         int err = ME_ERRNO_SUCCESS;
222         unsigned long cpu_flags;
223         uint32_t tmp;
224
225         PDEBUG("executed.\n");
226
227         instance = (me4600_ext_irq_subdevice_t *) subdevice;
228
229         if (flags) {
230                 PERROR("Invalid flag specified.\n");
231                 return ME_ERRNO_INVALID_FLAGS;
232         }
233
234         if (channel) {
235                 PERROR("Invalid channel specified.\n");
236                 return ME_ERRNO_INVALID_CHANNEL;
237         }
238
239         ME_SUBDEVICE_ENTER;
240
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);
252
253         ME_SUBDEVICE_EXIT;
254
255         return err;
256 }
257
258 static int me4600_ext_irq_io_reset_subdevice(me_subdevice_t *subdevice,
259                                              struct file *filep, int flags)
260 {
261         me4600_ext_irq_subdevice_t *instance;
262         unsigned long cpu_flags;
263         uint32_t tmp;
264
265         PDEBUG("executed.\n");
266
267         instance = (me4600_ext_irq_subdevice_t *) subdevice;
268
269         if (flags) {
270                 PERROR("Invalid flag specified.\n");
271                 return ME_ERRNO_INVALID_FLAGS;
272         }
273
274         ME_SUBDEVICE_ENTER;
275
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;
285         instance->count = 0;
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",
288                    instance->reg_base,
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);
293
294         ME_SUBDEVICE_EXIT;
295
296         return ME_ERRNO_SUCCESS;
297 }
298
299 static void me4600_ext_irq_destructor(struct me_subdevice *subdevice)
300 {
301         me4600_ext_irq_subdevice_t *instance;
302
303         PDEBUG("executed.\n");
304         instance = (me4600_ext_irq_subdevice_t *) subdevice;
305         me_subdevice_deinit(&instance->base);
306         free_irq(instance->irq, instance);
307         kfree(instance);
308 }
309
310 static int me4600_ext_irq_query_number_channels(me_subdevice_t *subdevice,
311                                                 int *number)
312 {
313         PDEBUG("executed.\n");
314         *number = 1;
315         return ME_ERRNO_SUCCESS;
316 }
317
318 static int me4600_ext_irq_query_subdevice_type(me_subdevice_t *subdevice,
319                                                int *type, int *subtype)
320 {
321         PDEBUG("executed.\n");
322         *type = ME_TYPE_EXT_IRQ;
323         *subtype = ME_SUBTYPE_SINGLE;
324         return ME_ERRNO_SUCCESS;
325 }
326
327 static int me4600_ext_irq_query_subdevice_caps(me_subdevice_t *subdevice,
328                                                int *caps)
329 {
330         PDEBUG("executed.\n");
331         *caps =
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;
335 }
336
337 static irqreturn_t me4600_ext_irq_isr(int irq, void *dev_id)
338 {
339         me4600_ext_irq_subdevice_t *instance;
340         uint32_t ctrl;
341         uint32_t irq_status;
342
343         instance = (me4600_ext_irq_subdevice_t *) dev_id;
344
345         if (irq != instance->irq) {
346                 PERROR("Incorrect interrupt num: %d.\n", irq);
347                 return IRQ_NONE;
348         }
349
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);
354                 return IRQ_NONE;
355         }
356
357         PDEBUG("executed.\n");
358
359         spin_lock(&instance->subdevice_lock);
360         instance->rised = 1;
361         instance->value = inl(instance->ext_irq_value_reg);
362         instance->count++;
363
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);
375
376         spin_unlock(&instance->subdevice_lock);
377         wake_up_interruptible_all(&instance->wait_queue);
378
379         return IRQ_HANDLED;
380 }
381
382 me4600_ext_irq_subdevice_t *me4600_ext_irq_constructor(uint32_t reg_base,
383                                                        int irq,
384                                                        spinlock_t *
385                                                        ctrl_reg_lock)
386 {
387         me4600_ext_irq_subdevice_t *subdevice;
388         int err;
389
390         PDEBUG("executed.\n");
391
392         /* Allocate memory for subdevice instance */
393         subdevice = kmalloc(sizeof(me4600_ext_irq_subdevice_t), GFP_KERNEL);
394
395         if (!subdevice) {
396                 PERROR("Cannot get memory for subdevice instance.\n");
397                 return NULL;
398         }
399
400         memset(subdevice, 0, sizeof(me4600_ext_irq_subdevice_t));
401
402         /* Initialize subdevice base class */
403         err = me_subdevice_init(&subdevice->base);
404
405         if (err) {
406                 PERROR("Cannot initialize subdevice base class instance.\n");
407                 kfree(subdevice);
408                 return NULL;
409         }
410         // Initialize spin locks.
411         spin_lock_init(&subdevice->subdevice_lock);
412
413         subdevice->ctrl_reg_lock = ctrl_reg_lock;
414
415         /* Initialize wait queue */
416         init_waitqueue_head(&subdevice->wait_queue);
417
418         /* Register interrupt */
419         subdevice->irq = irq;
420
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");
425                 kfree(subdevice);
426                 return NULL;
427         }
428         PINFO("Registered irq=%d.\n", subdevice->irq);
429
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;
437 #endif
438
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;
452
453         subdevice->rised = 0;
454         subdevice->count = 0;
455
456         return subdevice;
457 }