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)
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_di.h"
58 static int me4600_di_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me4600_di_subdevice_t *instance;
64 PDEBUG("executed.\n");
66 instance = (me4600_di_subdevice_t *) subdevice;
69 PERROR("Invalid flag specified.\n");
70 return ME_ERRNO_INVALID_FLAGS;
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);
91 return ME_ERRNO_SUCCESS;
94 static int me4600_di_io_single_config(me_subdevice_t *subdevice,
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;
112 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
113 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
115 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
117 PERROR("Invalid port direction specified.\n");
118 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
121 PERROR("Invalid channel number.\n");
122 err = ME_ERRNO_INVALID_CHANNEL;
127 PERROR("Invalid flags specified.\n");
128 err = ME_ERRNO_INVALID_FLAGS;
136 static int me4600_di_io_single_read(me_subdevice_t *subdevice,
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;
151 case ME_IO_SINGLE_TYPE_DIO_BIT:
152 if ((channel >= 0) && (channel < 8)) {
153 *value = inl(instance->port_reg) & (0x1 << channel);
155 PERROR("Invalid bit number specified.\n");
156 err = ME_ERRNO_INVALID_CHANNEL;
160 case ME_IO_SINGLE_NO_FLAGS:
161 case ME_IO_SINGLE_TYPE_DIO_BYTE:
163 *value = inl(instance->port_reg) & 0xFF;
165 PERROR("Invalid byte number specified.\n");
166 err = ME_ERRNO_INVALID_CHANNEL;
171 PERROR("Invalid flags specified.\n");
172 err = ME_ERRNO_INVALID_FLAGS;
180 static int me4600_di_query_number_channels(me_subdevice_t *subdevice,
183 PDEBUG("executed.\n");
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");
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");
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;
210 PDEBUG("executed.\n");
212 /* Allocate memory for subdevice instance */
213 subdevice = kmalloc(sizeof(me4600_di_subdevice_t), GFP_KERNEL);
216 PERROR("Cannot get memory for subdevice instance.\n");
220 memset(subdevice, 0, sizeof(me4600_di_subdevice_t));
222 /* Initialize subdevice base class */
223 err = me_subdevice_init(&subdevice->base);
226 PERROR("Cannot initialize subdevice base class instance.\n");
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;
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;