Staging: Add the Meilhaus ME-IDS driver package
[linux-2.6/mini2440.git] / drivers / staging / meilhaus / me4600_di.c
blob7e3c9f4d2df2f0dbd1d375259744d5bab24d76a5
1 /**
2 * @file me4600_di.c
4 * @brief ME-4000 digital input 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 <asm/io.h>
40 #include <linux/types.h>
42 #include "medefines.h"
43 #include "meinternal.h"
44 #include "meerror.h"
46 #include "medebug.h"
47 #include "me4600_dio_reg.h"
48 #include "me4600_di.h"
51 * Defines
55 * Functions
58 static int me4600_di_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me4600_di_subdevice_t *instance;
62 uint32_t mode;
64 PDEBUG("executed.\n");
66 instance = (me4600_di_subdevice_t *) subdevice;
68 if (flags) {
69 PERROR("Invalid flag specified.\n");
70 return ME_ERRNO_INVALID_FLAGS;
73 ME_SUBDEVICE_ENTER;
75 spin_lock(&instance->subdevice_lock);
76 spin_lock(instance->ctrl_reg_lock);
77 mode = inl(instance->ctrl_reg);
78 mode &= ~(ME4600_DIO_CTRL_BIT_MODE_2 | ME4600_DIO_CTRL_BIT_MODE_3); //0xFFF3
79 outl(mode, instance->ctrl_reg);
80 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
81 instance->ctrl_reg - instance->reg_base, mode);
82 spin_unlock(instance->ctrl_reg_lock);
84 outl(0, instance->port_reg);
85 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
86 instance->port_reg - instance->reg_base, 0);
87 spin_unlock(&instance->subdevice_lock);
89 ME_SUBDEVICE_EXIT;
91 return ME_ERRNO_SUCCESS;
94 static int me4600_di_io_single_config(me_subdevice_t * subdevice,
95 struct file *filep,
96 int channel,
97 int single_config,
98 int ref,
99 int trig_chan,
100 int trig_type, int trig_edge, int flags)
102 int err = ME_ERRNO_SUCCESS;
103 me4600_di_subdevice_t *instance;
105 PDEBUG("executed.\n");
107 instance = (me4600_di_subdevice_t *) subdevice;
109 ME_SUBDEVICE_ENTER;
111 switch (flags) {
112 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
113 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
114 if (channel == 0) {
115 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
116 } else {
117 PERROR("Invalid port direction specified.\n");
118 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
120 } else {
121 PERROR("Invalid channel number.\n");
122 err = ME_ERRNO_INVALID_CHANNEL;
124 break;
126 default:
127 PERROR("Invalid flags specified.\n");
128 err = ME_ERRNO_INVALID_FLAGS;
131 ME_SUBDEVICE_EXIT;
133 return err;
136 static int me4600_di_io_single_read(me_subdevice_t * subdevice,
137 struct file *filep,
138 int channel,
139 int *value, int time_out, int flags)
141 me4600_di_subdevice_t *instance;
142 int err = ME_ERRNO_SUCCESS;
144 PDEBUG("executed.\n");
146 instance = (me4600_di_subdevice_t *) subdevice;
148 ME_SUBDEVICE_ENTER;
150 switch (flags) {
151 case ME_IO_SINGLE_TYPE_DIO_BIT:
152 if ((channel >= 0) && (channel < 8)) {
153 *value = inl(instance->port_reg) & (0x1 << channel);
154 } else {
155 PERROR("Invalid bit number specified.\n");
156 err = ME_ERRNO_INVALID_CHANNEL;
158 break;
160 case ME_IO_SINGLE_NO_FLAGS:
161 case ME_IO_SINGLE_TYPE_DIO_BYTE:
162 if (channel == 0) {
163 *value = inl(instance->port_reg) & 0xFF;
164 } else {
165 PERROR("Invalid byte number specified.\n");
166 err = ME_ERRNO_INVALID_CHANNEL;
168 break;
170 default:
171 PERROR("Invalid flags specified.\n");
172 err = ME_ERRNO_INVALID_FLAGS;
175 ME_SUBDEVICE_EXIT;
177 return err;
180 static int me4600_di_query_number_channels(me_subdevice_t * subdevice,
181 int *number)
183 PDEBUG("executed.\n");
184 *number = 8;
185 return ME_ERRNO_SUCCESS;
188 static int me4600_di_query_subdevice_type(me_subdevice_t * subdevice,
189 int *type, int *subtype)
191 PDEBUG("executed.\n");
192 *type = ME_TYPE_DI;
193 *subtype = ME_SUBTYPE_SINGLE;
194 return ME_ERRNO_SUCCESS;
197 static int me4600_di_query_subdevice_caps(me_subdevice_t * subdevice, int *caps)
199 PDEBUG("executed.\n");
200 *caps = 0;
201 return ME_ERRNO_SUCCESS;
204 me4600_di_subdevice_t *me4600_di_constructor(uint32_t reg_base,
205 spinlock_t * ctrl_reg_lock)
207 me4600_di_subdevice_t *subdevice;
208 int err;
210 PDEBUG("executed.\n");
212 /* Allocate memory for subdevice instance */
213 subdevice = kmalloc(sizeof(me4600_di_subdevice_t), GFP_KERNEL);
215 if (!subdevice) {
216 PERROR("Cannot get memory for subdevice instance.\n");
217 return NULL;
220 memset(subdevice, 0, sizeof(me4600_di_subdevice_t));
222 /* Initialize subdevice base class */
223 err = me_subdevice_init(&subdevice->base);
225 if (err) {
226 PERROR("Cannot initialize subdevice base class instance.\n");
227 kfree(subdevice);
228 return NULL;
230 // Initialize spin locks.
231 spin_lock_init(&subdevice->subdevice_lock);
233 subdevice->ctrl_reg_lock = ctrl_reg_lock;
235 /* Save the subdevice index */
236 subdevice->port_reg = reg_base + ME4600_DIO_PORT_1_REG;
237 subdevice->ctrl_reg = reg_base + ME4600_DIO_CTRL_REG;
238 #ifdef MEDEBUG_DEBUG_REG
239 subdevice->reg_base = reg_base;
240 #endif
242 /* Overload base class methods. */
243 subdevice->base.me_subdevice_io_reset_subdevice =
244 me4600_di_io_reset_subdevice;
245 subdevice->base.me_subdevice_io_single_config =
246 me4600_di_io_single_config;
247 subdevice->base.me_subdevice_io_single_read = me4600_di_io_single_read;
248 subdevice->base.me_subdevice_query_number_channels =
249 me4600_di_query_number_channels;
250 subdevice->base.me_subdevice_query_subdevice_type =
251 me4600_di_query_subdevice_type;
252 subdevice->base.me_subdevice_query_subdevice_caps =
253 me4600_di_query_subdevice_caps;
255 return subdevice;