4 * @brief ME-630 relay 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_relay_reg.h"
48 #include "me0600_relay.h"
58 static int me0600_relay_io_reset_subdevice(struct me_subdevice
*subdevice
,
59 struct file
*filep
, int flags
)
61 me0600_relay_subdevice_t
*instance
;
63 PDEBUG("executed.\n");
65 instance
= (me0600_relay_subdevice_t
*) subdevice
;
68 PERROR("Invalid flag specified.\n");
69 return ME_ERRNO_INVALID_FLAGS
;
74 spin_lock(&instance
->subdevice_lock
);
75 outb(0x0, instance
->port_0_reg
);
76 PDEBUG_REG("port_0_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
77 instance
->port_0_reg
- instance
->reg_base
, 0);
78 outb(0x0, instance
->port_1_reg
);
79 PDEBUG_REG("port_1_reg outl(0x%lX+0x%lX)=0x%x\n", instance
->reg_base
,
80 instance
->port_1_reg
- instance
->reg_base
, 0);
81 spin_unlock(&instance
->subdevice_lock
);
85 return ME_ERRNO_SUCCESS
;
88 static int me0600_relay_io_single_config(me_subdevice_t
* subdevice
,
95 int trig_edge
, int flags
)
97 me0600_relay_subdevice_t
*instance
;
98 int err
= ME_ERRNO_SUCCESS
;
100 PDEBUG("executed.\n");
102 instance
= (me0600_relay_subdevice_t
*) subdevice
;
106 spin_lock(&instance
->subdevice_lock
);
109 case ME_IO_SINGLE_CONFIG_NO_FLAGS
:
110 case ME_IO_SINGLE_CONFIG_DIO_WORD
:
112 if (single_config
!= ME_SINGLE_CONFIG_DIO_OUTPUT
) {
113 PERROR("Invalid word direction specified.\n");
114 err
= ME_ERRNO_INVALID_SINGLE_CONFIG
;
117 PERROR("Invalid channel specified.\n");
118 err
= ME_ERRNO_INVALID_CHANNEL
;
124 PERROR("Invalid flags specified.\n");
126 err
= ME_ERRNO_INVALID_FLAGS
;
131 spin_unlock(&instance
->subdevice_lock
);
138 static int me0600_relay_io_single_read(me_subdevice_t
* subdevice
,
141 int *value
, int time_out
, int flags
)
143 me0600_relay_subdevice_t
*instance
;
144 int err
= ME_ERRNO_SUCCESS
;
146 PDEBUG("executed.\n");
148 instance
= (me0600_relay_subdevice_t
*) subdevice
;
152 spin_lock(&instance
->subdevice_lock
);
156 case ME_IO_SINGLE_TYPE_DIO_BIT
:
157 if ((channel
>= 0) && (channel
< 8)) {
158 *value
= inb(instance
->port_0_reg
) & (0x1 << channel
);
159 } else if ((channel
>= 8) && (channel
< 16)) {
161 inb(instance
->port_1_reg
) & (0x1 << (channel
- 8));
163 PERROR("Invalid bit number specified.\n");
164 err
= ME_ERRNO_INVALID_CHANNEL
;
169 case ME_IO_SINGLE_TYPE_DIO_BYTE
:
171 *value
= inb(instance
->port_0_reg
);
172 } else if (channel
== 1) {
173 *value
= inb(instance
->port_1_reg
);
175 PERROR("Invalid byte number specified.\n");
176 err
= ME_ERRNO_INVALID_CHANNEL
;
181 case ME_IO_SINGLE_NO_FLAGS
:
182 case ME_IO_SINGLE_TYPE_DIO_WORD
:
184 *value
= (uint32_t) inb(instance
->port_1_reg
) << 8;
185 *value
|= inb(instance
->port_0_reg
);
187 PERROR("Invalid word number specified.\n");
188 err
= ME_ERRNO_INVALID_CHANNEL
;
194 PERROR("Invalid flags specified.\n");
196 err
= ME_ERRNO_INVALID_FLAGS
;
199 spin_unlock(&instance
->subdevice_lock
);
206 static int me0600_relay_io_single_write(me_subdevice_t
* subdevice
,
209 int value
, int time_out
, int flags
)
211 me0600_relay_subdevice_t
*instance
;
212 int err
= ME_ERRNO_SUCCESS
;
215 PDEBUG("executed.\n");
217 instance
= (me0600_relay_subdevice_t
*) subdevice
;
221 spin_lock(&instance
->subdevice_lock
);
224 case ME_IO_SINGLE_TYPE_DIO_BIT
:
225 if ((channel
>= 0) && (channel
< 8)) {
226 state
= inb(instance
->port_0_reg
);
228 value
? (state
| (0x1 << channel
)) : (state
&
231 outb(state
, instance
->port_0_reg
);
232 } else if ((channel
>= 8) && (channel
< 16)) {
233 state
= inb(instance
->port_1_reg
);
235 value
? (state
| (0x1 << (channel
- 8))) : (state
&
240 outb(state
, instance
->port_1_reg
);
242 PERROR("Invalid bit number specified.\n");
243 err
= ME_ERRNO_INVALID_CHANNEL
;
247 case ME_IO_SINGLE_TYPE_DIO_BYTE
:
249 outb(value
, instance
->port_0_reg
);
250 } else if (channel
== 1) {
251 outb(value
, instance
->port_1_reg
);
253 PERROR("Invalid byte number specified.\n");
254 err
= ME_ERRNO_INVALID_CHANNEL
;
258 case ME_IO_SINGLE_NO_FLAGS
:
259 case ME_IO_SINGLE_TYPE_DIO_WORD
:
261 outb(value
, instance
->port_0_reg
);
262 outb(value
>> 8, instance
->port_1_reg
);
264 PERROR("Invalid word number specified.\n");
265 err
= ME_ERRNO_INVALID_CHANNEL
;
270 PERROR("Invalid flags specified.\n");
271 err
= ME_ERRNO_INVALID_FLAGS
;
275 spin_unlock(&instance
->subdevice_lock
);
282 static int me0600_relay_query_number_channels(me_subdevice_t
* subdevice
,
285 PDEBUG("executed.\n");
287 return ME_ERRNO_SUCCESS
;
290 static int me0600_relay_query_subdevice_type(me_subdevice_t
* subdevice
,
291 int *type
, int *subtype
)
293 PDEBUG("executed.\n");
295 *subtype
= ME_SUBTYPE_SINGLE
;
296 return ME_ERRNO_SUCCESS
;
299 static int me0600_relay_query_subdevice_caps(me_subdevice_t
* subdevice
,
302 PDEBUG("executed.\n");
304 return ME_ERRNO_SUCCESS
;
307 me0600_relay_subdevice_t
*me0600_relay_constructor(uint32_t reg_base
)
309 me0600_relay_subdevice_t
*subdevice
;
312 PDEBUG("executed.\n");
314 /* Allocate memory for subdevice instance */
315 subdevice
= kmalloc(sizeof(me0600_relay_subdevice_t
), GFP_KERNEL
);
318 PERROR("Cannot get memory for subdevice instance.\n");
322 memset(subdevice
, 0, sizeof(me0600_relay_subdevice_t
));
324 /* Initialize subdevice base class */
325 err
= me_subdevice_init(&subdevice
->base
);
328 PERROR("Cannot initialize subdevice base class instance.\n");
332 // Initialize spin locks.
333 spin_lock_init(&subdevice
->subdevice_lock
);
335 /* Save the subdevice index */
336 subdevice
->port_0_reg
= reg_base
+ ME0600_RELAIS_0_REG
;
337 subdevice
->port_1_reg
= reg_base
+ ME0600_RELAIS_1_REG
;
338 #ifdef MEDEBUG_DEBUG_REG
339 subdevice
->reg_base
= reg_base
;
342 /* Overload base class methods. */
343 subdevice
->base
.me_subdevice_io_reset_subdevice
=
344 me0600_relay_io_reset_subdevice
;
345 subdevice
->base
.me_subdevice_io_single_config
=
346 me0600_relay_io_single_config
;
347 subdevice
->base
.me_subdevice_io_single_read
=
348 me0600_relay_io_single_read
;
349 subdevice
->base
.me_subdevice_io_single_write
=
350 me0600_relay_io_single_write
;
351 subdevice
->base
.me_subdevice_query_number_channels
=
352 me0600_relay_query_number_channels
;
353 subdevice
->base
.me_subdevice_query_subdevice_type
=
354 me0600_relay_query_subdevice_type
;
355 subdevice
->base
.me_subdevice_query_subdevice_caps
=
356 me0600_relay_query_subdevice_caps
;