4 * @brief ME-630 Optoisolated 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 "me0600_optoi_reg.h"
48 #include "me0600_optoi.h"
58 static int me0600_optoi_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
63 PERROR("Invalid flag specified.\n");
64 return ME_ERRNO_INVALID_FLAGS;
67 PDEBUG("executed.\n");
68 return ME_ERRNO_SUCCESS;
71 static int me0600_optoi_io_single_config(me_subdevice_t * subdevice,
78 int trig_edge, int flags)
80 me0600_optoi_subdevice_t *instance;
81 int err = ME_ERRNO_SUCCESS;
83 PDEBUG("executed.\n");
85 instance = (me0600_optoi_subdevice_t *) subdevice;
89 spin_lock(&instance->subdevice_lock);
92 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
93 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
95 if (single_config != ME_SINGLE_CONFIG_DIO_INPUT) {
96 PERROR("Invalid port direction specified.\n");
97 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
100 PERROR("Invalid channel specified.\n");
101 err = ME_ERRNO_INVALID_CHANNEL;
107 PERROR("Invalid flags specified.\n");
109 err = ME_ERRNO_INVALID_FLAGS;
114 spin_unlock(&instance->subdevice_lock);
121 static int me0600_optoi_io_single_read(me_subdevice_t * subdevice,
124 int *value, int time_out, int flags)
126 me0600_optoi_subdevice_t *instance;
127 int err = ME_ERRNO_SUCCESS;
129 PDEBUG("executed.\n");
131 instance = (me0600_optoi_subdevice_t *) subdevice;
135 spin_lock(&instance->subdevice_lock);
138 case ME_IO_SINGLE_TYPE_DIO_BIT:
139 if ((channel >= 0) && (channel < 8)) {
140 *value = inb(instance->port_reg) & (0x1 << channel);
142 PERROR("Invalid bit number specified.\n");
143 err = ME_ERRNO_INVALID_CHANNEL;
148 case ME_IO_SINGLE_NO_FLAGS:
149 case ME_IO_SINGLE_TYPE_DIO_BYTE:
151 *value = inb(instance->port_reg);
153 PERROR("Invalid byte number specified.\n");
154 err = ME_ERRNO_INVALID_CHANNEL;
160 PERROR("Invalid flags specified.\n");
162 err = ME_ERRNO_INVALID_FLAGS;
165 spin_unlock(&instance->subdevice_lock);
172 static int me0600_optoi_query_number_channels(me_subdevice_t * subdevice,
175 PDEBUG("executed.\n");
177 return ME_ERRNO_SUCCESS;
180 static int me0600_optoi_query_subdevice_type(me_subdevice_t * subdevice,
181 int *type, int *subtype)
183 PDEBUG("executed.\n");
185 *subtype = ME_SUBTYPE_SINGLE;
186 return ME_ERRNO_SUCCESS;
189 static int me0600_optoi_query_subdevice_caps(me_subdevice_t * subdevice,
192 PDEBUG("executed.\n");
194 return ME_ERRNO_SUCCESS;
197 me0600_optoi_subdevice_t *me0600_optoi_constructor(uint32_t reg_base)
199 me0600_optoi_subdevice_t *subdevice;
202 PDEBUG("executed.\n");
204 /* Allocate memory for subdevice instance */
205 subdevice = kmalloc(sizeof(me0600_optoi_subdevice_t), GFP_KERNEL);
208 PERROR("Cannot get memory for subdevice instance.\n");
212 memset(subdevice, 0, sizeof(me0600_optoi_subdevice_t));
214 /* Initialize subdevice base class */
215 err = me_subdevice_init(&subdevice->base);
218 PERROR("Cannot initialize subdevice base class instance.\n");
222 // Initialize spin locks.
223 spin_lock_init(&subdevice->subdevice_lock);
225 /* Save the subdevice index */
226 subdevice->port_reg = reg_base + ME0600_OPTO_INPUT_REG;
228 /* Overload base class methods. */
229 subdevice->base.me_subdevice_io_reset_subdevice =
230 me0600_optoi_io_reset_subdevice;
231 subdevice->base.me_subdevice_io_single_config =
232 me0600_optoi_io_single_config;
233 subdevice->base.me_subdevice_io_single_read =
234 me0600_optoi_io_single_read;
235 subdevice->base.me_subdevice_query_number_channels =
236 me0600_optoi_query_number_channels;
237 subdevice->base.me_subdevice_query_subdevice_type =
238 me0600_optoi_query_subdevice_type;
239 subdevice->base.me_subdevice_query_subdevice_caps =
240 me0600_optoi_query_subdevice_caps;