added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / staging / meilhaus / me8200_do.c
blobd2bebd16ff41bffff9b60c9ee6b6f632c72da036
1 /**
2 * @file me8200_do.c
4 * @brief ME-8200 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)
8 */
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.
28 #ifndef __KERNEL__
29 # define __KERNEL__
30 #endif
33 * Includes
35 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <asm/io.h>
41 #include <linux/types.h>
42 #include <linux/version.h>
44 #include "medefines.h"
45 #include "meinternal.h"
46 #include "meerror.h"
48 #include "meids.h"
49 #include "medebug.h"
50 #include "me8200_reg.h"
51 #include "me8200_do_reg.h"
52 #include "me8200_do.h"
55 * Defines
59 * Functions
62 static int me8200_do_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)
68 me8200_do_subdevice_t *instance;
69 int err = ME_ERRNO_SUCCESS;
70 uint8_t tmp;
71 unsigned long status;
73 if (flags & ~ME_IO_IRQ_START_DIO_BYTE) {
74 PERROR("Invalid flag specified.\n");
75 return ME_ERRNO_INVALID_FLAGS;
78 if (channel != 0) {
79 PERROR("Invalid channel specified.\n");
80 return ME_ERRNO_INVALID_CHANNEL;
83 if (irq_source != ME_IRQ_SOURCE_DIO_OVER_TEMP) {
84 PERROR("Invalid interrupt source specified.\n");
85 return ME_ERRNO_INVALID_IRQ_SOURCE;
88 PDEBUG("executed.\n");
90 instance = (me8200_do_subdevice_t *) subdevice;
92 ME_SUBDEVICE_ENTER;
94 spin_lock_irqsave(&instance->subdevice_lock, status);
95 spin_lock(instance->irq_mode_lock);
96 tmp = inb(instance->irq_ctrl_reg);
97 tmp |=
98 ME8200_IRQ_MODE_BIT_ENABLE_POWER << (ME8200_IRQ_MODE_POWER_SHIFT *
99 instance->do_idx);
100 outb(tmp, instance->irq_ctrl_reg);
101 PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
102 instance->irq_ctrl_reg - instance->reg_base, tmp);
103 spin_unlock(instance->irq_mode_lock);
104 instance->rised = 0;
105 spin_unlock_irqrestore(&instance->subdevice_lock, status);
107 ME_SUBDEVICE_EXIT;
109 return err;
112 static int me8200_do_io_irq_wait(me_subdevice_t * subdevice,
113 struct file *filep,
114 int channel,
115 int *irq_count,
116 int *value, int time_out, int flags)
118 me8200_do_subdevice_t *instance;
119 int err = ME_ERRNO_SUCCESS;
120 long t = 0;
121 unsigned long cpu_flags;
123 PDEBUG("executed.\n");
125 instance = (me8200_do_subdevice_t *) subdevice;
127 if (flags) {
128 PERROR("Invalid flag specified.\n");
129 return ME_ERRNO_INVALID_FLAGS;
132 if (time_out < 0) {
133 PERROR("Invalid time_out specified.\n");
134 return ME_ERRNO_INVALID_TIMEOUT;
137 if (time_out) {
138 t = (time_out * HZ) / 1000;
140 if (t == 0)
141 t = 1;
144 ME_SUBDEVICE_ENTER;
146 if (instance->rised <= 0) {
147 instance->rised = 0;
149 if (time_out) {
150 t = wait_event_interruptible_timeout(instance->
151 wait_queue,
152 (instance->rised !=
153 0), t);
155 if (t == 0) {
156 PERROR
157 ("Wait on external interrupt timed out.\n");
158 err = ME_ERRNO_TIMEOUT;
160 } else {
161 wait_event_interruptible(instance->wait_queue,
162 (instance->rised != 0));
165 if (instance->rised < 0) {
166 PERROR("Wait on interrupt aborted by user.\n");
167 err = ME_ERRNO_CANCELLED;
171 if (signal_pending(current)) {
172 PERROR("Wait on external interrupt aborted by signal.\n");
173 err = ME_ERRNO_SIGNAL;
176 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
177 instance->rised = 0;
178 *irq_count = instance->count;
179 *value = 0;
180 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
182 ME_SUBDEVICE_EXIT;
184 return err;
187 static int me8200_do_io_irq_stop(me_subdevice_t * subdevice,
188 struct file *filep, int channel, int flags)
190 me8200_do_subdevice_t *instance;
191 uint8_t tmp;
192 unsigned long cpu_flags;
194 PDEBUG("executed.\n");
196 instance = (me8200_do_subdevice_t *) subdevice;
198 if (flags) {
199 PERROR("Invalid flag specified.\n");
200 return ME_ERRNO_INVALID_FLAGS;
203 ME_SUBDEVICE_ENTER;
205 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
206 spin_lock(instance->irq_mode_lock);
207 tmp = inb(instance->irq_ctrl_reg);
208 tmp &=
209 ~(ME8200_IRQ_MODE_BIT_ENABLE_POWER <<
210 (ME8200_IRQ_MODE_POWER_SHIFT * instance->do_idx));
211 outb(tmp, instance->irq_ctrl_reg);
212 PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
213 instance->irq_ctrl_reg - instance->reg_base, tmp);
214 spin_unlock(instance->irq_mode_lock);
215 instance->rised = -1;
216 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
217 wake_up_interruptible_all(&instance->wait_queue);
219 ME_SUBDEVICE_EXIT;
221 return ME_ERRNO_SUCCESS;
224 static int me8200_do_io_reset_subdevice(struct me_subdevice *subdevice,
225 struct file *filep, int flags)
227 me8200_do_subdevice_t *instance;
228 unsigned long cpu_flags;
229 uint8_t tmp;
231 PDEBUG("executed.\n");
233 instance = (me8200_do_subdevice_t *) subdevice;
235 if (flags) {
236 PERROR("Invalid flag specified.\n");
237 return ME_ERRNO_INVALID_FLAGS;
240 ME_SUBDEVICE_ENTER;
242 spin_lock_irqsave(&instance->subdevice_lock, cpu_flags);
243 outb(0x00, instance->port_reg);
244 PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
245 instance->port_reg - instance->reg_base, 0x00);
246 spin_lock(instance->irq_mode_lock);
247 tmp = inb(instance->irq_ctrl_reg);
248 tmp &=
249 ~(ME8200_IRQ_MODE_BIT_ENABLE_POWER <<
250 (ME8200_IRQ_MODE_POWER_SHIFT * instance->do_idx));
251 outb(tmp, instance->irq_ctrl_reg);
252 PDEBUG_REG("irq_ctrl_reg outb(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
253 instance->irq_ctrl_reg - instance->reg_base, tmp);
254 spin_unlock(instance->irq_mode_lock);
255 instance->rised = -1;
256 instance->count = 0;
257 spin_unlock_irqrestore(&instance->subdevice_lock, cpu_flags);
258 wake_up_interruptible_all(&instance->wait_queue);
260 ME_SUBDEVICE_EXIT;
262 return ME_ERRNO_SUCCESS;
265 static int me8200_do_io_single_config(me_subdevice_t * subdevice,
266 struct file *filep,
267 int channel,
268 int single_config,
269 int ref,
270 int trig_chan,
271 int trig_type, int trig_edge, int flags)
273 me8200_do_subdevice_t *instance;
274 int err = ME_ERRNO_SUCCESS;
275 unsigned long status;
277 PDEBUG("executed.\n");
279 instance = (me8200_do_subdevice_t *) subdevice;
281 ME_SUBDEVICE_ENTER;
283 spin_lock_irqsave(&instance->subdevice_lock, status);
284 switch (flags) {
285 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
286 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
287 if (channel == 0) {
288 if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
289 } else {
290 PERROR("Invalid byte direction specified.\n");
291 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
293 } else {
294 PERROR("Invalid byte specified.\n");
295 err = ME_ERRNO_INVALID_CHANNEL;
297 break;
299 default:
300 PERROR("Invalid flags specified.\n");
301 err = ME_ERRNO_INVALID_FLAGS;
303 spin_unlock_irqrestore(&instance->subdevice_lock, status);
305 ME_SUBDEVICE_EXIT;
307 return err;
310 static int me8200_do_io_single_read(me_subdevice_t * subdevice,
311 struct file *filep,
312 int channel,
313 int *value, int time_out, int flags)
315 me8200_do_subdevice_t *instance;
316 int err = ME_ERRNO_SUCCESS;
317 unsigned long status;
319 PDEBUG("executed.\n");
321 instance = (me8200_do_subdevice_t *) subdevice;
323 ME_SUBDEVICE_ENTER;
325 spin_lock_irqsave(&instance->subdevice_lock, status);
326 switch (flags) {
327 case ME_IO_SINGLE_TYPE_DIO_BIT:
328 if ((channel >= 0) && (channel < 8)) {
329 *value = inb(instance->port_reg) & (0x1 << channel);
330 } else {
331 PERROR("Invalid bit number specified.\n");
332 err = ME_ERRNO_INVALID_CHANNEL;
334 break;
336 case ME_IO_SINGLE_NO_FLAGS:
337 case ME_IO_SINGLE_TYPE_DIO_BYTE:
338 if (channel == 0) {
339 *value = inb(instance->port_reg);
340 } else {
341 PERROR("Invalid byte number specified.\n");
342 err = ME_ERRNO_INVALID_CHANNEL;
344 break;
346 default:
347 PERROR("Invalid flags specified.\n");
348 err = ME_ERRNO_INVALID_FLAGS;
350 spin_unlock_irqrestore(&instance->subdevice_lock, status);
352 ME_SUBDEVICE_EXIT;
354 return err;
357 static int me8200_do_io_single_write(me_subdevice_t * subdevice,
358 struct file *filep,
359 int channel,
360 int value, int time_out, int flags)
362 me8200_do_subdevice_t *instance;
363 int err = ME_ERRNO_SUCCESS;
364 uint8_t state;
365 unsigned long status;
367 PDEBUG("executed.\n");
369 instance = (me8200_do_subdevice_t *) subdevice;
371 ME_SUBDEVICE_ENTER;
373 spin_lock_irqsave(&instance->subdevice_lock, status);
374 switch (flags) {
375 case ME_IO_SINGLE_TYPE_DIO_BIT:
376 if ((channel >= 0) && (channel < 8)) {
377 state = inb(instance->port_reg);
378 state =
379 value ? (state | (0x1 << channel)) : (state &
380 ~(0x1 <<
381 channel));
382 outb(state, instance->port_reg);
383 PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n",
384 instance->reg_base,
385 instance->port_reg - instance->reg_base,
386 state);
387 } else {
388 PERROR("Invalid bit number specified.\n");
389 err = ME_ERRNO_INVALID_CHANNEL;
391 break;
393 case ME_IO_SINGLE_NO_FLAGS:
394 case ME_IO_SINGLE_TYPE_DIO_BYTE:
395 if (channel == 0) {
396 outb(value, instance->port_reg);
397 PDEBUG_REG("port_reg outb(0x%lX+0x%lX)=0x%x\n",
398 instance->reg_base,
399 instance->port_reg - instance->reg_base,
400 value);
401 } else {
402 PERROR("Invalid byte number specified.\n");
403 err = ME_ERRNO_INVALID_CHANNEL;
405 break;
407 default:
408 PERROR("Invalid flags specified.\n");
409 err = ME_ERRNO_INVALID_FLAGS;
411 spin_unlock_irqrestore(&instance->subdevice_lock, status);
413 ME_SUBDEVICE_EXIT;
415 return err;
418 static int me8200_do_query_number_channels(me_subdevice_t * subdevice,
419 int *number)
421 PDEBUG("executed.\n");
422 *number = 8;
423 return ME_ERRNO_SUCCESS;
426 static int me8200_do_query_subdevice_type(me_subdevice_t * subdevice,
427 int *type, int *subtype)
429 PDEBUG("executed.\n");
430 *type = ME_TYPE_DO;
431 *subtype = ME_SUBTYPE_SINGLE;
432 return ME_ERRNO_SUCCESS;
435 static int me8200_do_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
437 PDEBUG("executed.\n");
438 *caps = ME_CAPS_DIO_OVER_TEMP_IRQ;
439 return ME_ERRNO_SUCCESS;
442 static void me8200_do_destructor(struct me_subdevice *subdevice)
444 me8200_do_subdevice_t *instance;
446 PDEBUG("executed.\n");
448 instance = (me8200_do_subdevice_t *) subdevice;
450 free_irq(instance->irq, (void *)instance);
451 me_subdevice_deinit(&instance->base);
452 kfree(instance);
455 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
456 static irqreturn_t me8200_do_isr(int irq, void *dev_id)
457 #else
458 static irqreturn_t me8200_do_isr(int irq, void *dev_id, struct pt_regs *regs)
459 #endif
461 me8200_do_subdevice_t *instance;
462 uint16_t ctrl;
463 uint8_t irq_status;
465 instance = (me8200_do_subdevice_t *) dev_id;
467 if (irq != instance->irq) {
468 PERROR("Incorrect interrupt num: %d.\n", irq);
469 return IRQ_NONE;
472 irq_status = inb(instance->irq_status_reg);
473 if (!
474 (irq_status &
475 (ME8200_DO_IRQ_STATUS_BIT_ACTIVE << instance->do_idx))) {
476 PINFO
477 ("%ld Shared interrupt. %s(): idx=%d irq_status_reg=0x%04X\n",
478 jiffies, __func__, instance->do_idx, irq_status);
479 return IRQ_NONE;
482 PDEBUG("executed.\n");
484 spin_lock(&instance->subdevice_lock);
485 instance->rised = 1;
486 instance->count++;
488 spin_lock(instance->irq_mode_lock);
489 ctrl = inw(instance->irq_ctrl_reg);
490 ctrl |= ME8200_IRQ_MODE_BIT_CLEAR_POWER << instance->do_idx;
491 outw(ctrl, instance->irq_ctrl_reg);
492 PDEBUG_REG("irq_ctrl_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
493 instance->irq_ctrl_reg - instance->reg_base, ctrl);
494 ctrl &= ~(ME8200_IRQ_MODE_BIT_CLEAR_POWER << instance->do_idx);
495 outw(ctrl, instance->irq_ctrl_reg);
496 PDEBUG_REG("irq_ctrl_reg outw(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
497 instance->irq_ctrl_reg - instance->reg_base, ctrl);
498 spin_unlock(instance->irq_mode_lock);
499 spin_unlock(&instance->subdevice_lock);
500 wake_up_interruptible_all(&instance->wait_queue);
502 return IRQ_HANDLED;
505 me8200_do_subdevice_t *me8200_do_constructor(uint32_t reg_base,
506 unsigned int do_idx,
507 int irq,
508 spinlock_t * irq_mode_lock)
510 me8200_do_subdevice_t *subdevice;
511 int err;
513 PDEBUG("executed.\n");
515 /* Allocate memory for subdevice instance */
516 subdevice = kmalloc(sizeof(me8200_do_subdevice_t), GFP_KERNEL);
518 if (!subdevice) {
519 PERROR("Cannot get memory for subdevice instance.\n");
520 return NULL;
523 memset(subdevice, 0, sizeof(me8200_do_subdevice_t));
525 /* Initialize subdevice base class */
526 err = me_subdevice_init(&subdevice->base);
528 if (err) {
529 PERROR("Cannot initialize subdevice base class instance.\n");
530 kfree(subdevice);
531 return NULL;
533 // Initialize spin locks.
534 spin_lock_init(&subdevice->subdevice_lock);
536 subdevice->irq_mode_lock = irq_mode_lock;
538 /* Save the index of the digital output */
539 subdevice->do_idx = do_idx;
540 subdevice->irq = irq;
542 /* Initialize the registers */
543 if (do_idx == 0) {
544 subdevice->port_reg = reg_base + ME8200_DO_PORT_0_REG;
545 } else if (do_idx == 1) {
546 subdevice->port_reg = reg_base + ME8200_DO_PORT_1_REG;
547 } else {
548 PERROR("Wrong subdevice idx=%d.\n", do_idx);
549 kfree(subdevice);
550 return NULL;
552 subdevice->irq_ctrl_reg = reg_base + ME8200_IRQ_MODE_REG;
553 subdevice->irq_status_reg = reg_base + ME8200_DO_IRQ_STATUS_REG;
554 #ifdef MEDEBUG_DEBUG_REG
555 subdevice->reg_base = reg_base;
556 #endif
558 /* Initialize the wait queue */
559 init_waitqueue_head(&subdevice->wait_queue);
561 /* Request the interrupt line */
562 err = request_irq(irq, me8200_do_isr,
563 #ifdef IRQF_DISABLED
564 IRQF_DISABLED | IRQF_SHARED,
565 #else
566 SA_INTERRUPT | SA_SHIRQ,
567 #endif
568 ME8200_NAME, (void *)subdevice);
570 if (err) {
571 PERROR("Cannot get interrupt line.\n");
572 kfree(subdevice);
573 return NULL;
575 PINFO("Registered irq=%d.\n", irq);
577 /* Overload base class methods. */
578 subdevice->base.me_subdevice_io_irq_start = me8200_do_io_irq_start;
579 subdevice->base.me_subdevice_io_irq_wait = me8200_do_io_irq_wait;
580 subdevice->base.me_subdevice_io_irq_stop = me8200_do_io_irq_stop;
581 subdevice->base.me_subdevice_io_reset_subdevice =
582 me8200_do_io_reset_subdevice;
583 subdevice->base.me_subdevice_io_single_config =
584 me8200_do_io_single_config;
585 subdevice->base.me_subdevice_io_single_read = me8200_do_io_single_read;
586 subdevice->base.me_subdevice_io_single_write =
587 me8200_do_io_single_write;
588 subdevice->base.me_subdevice_query_number_channels =
589 me8200_do_query_number_channels;
590 subdevice->base.me_subdevice_query_subdevice_type =
591 me8200_do_query_subdevice_type;
592 subdevice->base.me_subdevice_query_subdevice_caps =
593 me8200_do_query_subdevice_caps;
594 subdevice->base.me_subdevice_destructor = me8200_do_destructor;
596 subdevice->rised = 0;
597 subdevice->count = 0;
599 return subdevice;