OMAP2/3: Add omap_type() for determining GP/EMU/HS
[linux-ginger.git] / drivers / staging / meilhaus / me8200_dio.c
blobc7f43f011f3cbec2ce399354810000dbb6f17ac5
1 /**
2 * @file me8200_dio.c
4 * @brief ME-8200 digital input/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/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 "me8200_dio_reg.h"
48 #include "me8200_dio.h"
51 * Defines
55 * Functions
58 static int me8200_dio_io_reset_subdevice(struct me_subdevice *subdevice,
59 struct file *filep, int flags)
61 me8200_dio_subdevice_t *instance;
62 uint8_t mode;
64 PDEBUG("executed.\n");
66 if (flags) {
67 PERROR("Invalid flag specified.\n");
68 return ME_ERRNO_INVALID_FLAGS;
71 instance = (me8200_dio_subdevice_t *) subdevice;
73 ME_SUBDEVICE_ENTER;
75 spin_lock(&instance->subdevice_lock);
76 spin_lock(instance->ctrl_reg_lock);
77 mode = inb(instance->ctrl_reg);
78 mode &= ~(0x3 << (instance->dio_idx * 2));
79 outb(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);
83 outb(0x00, instance->port_reg);
84 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
85 instance->port_reg - instance->reg_base, 0x00);
86 spin_unlock(&instance->subdevice_lock);
88 ME_SUBDEVICE_EXIT;
90 return ME_ERRNO_SUCCESS;
93 static int me8200_dio_io_single_config(me_subdevice_t *subdevice,
94 struct file *filep,
95 int channel,
96 int single_config,
97 int ref,
98 int trig_chan,
99 int trig_type, int trig_edge, int flags)
101 me8200_dio_subdevice_t *instance;
102 int err = ME_ERRNO_SUCCESS;
103 uint32_t mode;
104 uint32_t size =
105 flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
106 | ME_IO_SINGLE_CONFIG_DIO_WORD |
107 ME_IO_SINGLE_CONFIG_DIO_DWORD);
109 PDEBUG("executed.\n");
111 instance = (me8200_dio_subdevice_t *) subdevice;
113 ME_SUBDEVICE_ENTER;
115 spin_lock(&instance->subdevice_lock);
116 spin_lock(instance->ctrl_reg_lock);
117 mode = inb(instance->ctrl_reg);
118 switch (size) {
119 case ME_IO_SINGLE_CONFIG_NO_FLAGS:
120 case ME_IO_SINGLE_CONFIG_DIO_BYTE:
121 if (channel == 0) {
122 if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
123 mode &=
124 ~((ME8200_DIO_CTRL_BIT_MODE_0 |
125 ME8200_DIO_CTRL_BIT_MODE_1) <<
126 (instance->dio_idx * 2));
127 } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
128 mode &=
129 ~((ME8200_DIO_CTRL_BIT_MODE_0 |
130 ME8200_DIO_CTRL_BIT_MODE_1) <<
131 (instance->dio_idx * 2));
132 mode |=
133 ME8200_DIO_CTRL_BIT_MODE_0 << (instance->
134 dio_idx * 2);
135 } else {
136 PERROR
137 ("Invalid port configuration specified.\n");
138 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
140 } else {
141 PERROR("Invalid channel number.\n");
142 err = ME_ERRNO_INVALID_CHANNEL;
145 break;
147 default:
148 PERROR("Invalid flags.\n");
150 err = ME_ERRNO_INVALID_FLAGS;
153 if (!err) {
154 outb(mode, instance->ctrl_reg);
155 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
156 instance->reg_base,
157 instance->ctrl_reg - instance->reg_base, mode);
159 spin_unlock(instance->ctrl_reg_lock);
160 spin_unlock(&instance->subdevice_lock);
162 ME_SUBDEVICE_EXIT;
164 return err;
167 static int me8200_dio_io_single_read(me_subdevice_t *subdevice,
168 struct file *filep,
169 int channel,
170 int *value, int time_out, int flags)
172 me8200_dio_subdevice_t *instance;
173 int err = ME_ERRNO_SUCCESS;
174 uint8_t mode;
176 PDEBUG("executed.\n");
178 instance = (me8200_dio_subdevice_t *) subdevice;
180 ME_SUBDEVICE_ENTER;
182 spin_lock(&instance->subdevice_lock);
183 spin_lock(instance->ctrl_reg_lock);
184 switch (flags) {
185 case ME_IO_SINGLE_TYPE_DIO_BIT:
186 if ((channel >= 0) && (channel < 8)) {
187 mode =
188 inb(instance->
189 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
190 ME8200_DIO_CTRL_BIT_MODE_1) <<
191 (instance->dio_idx * 2));
193 if ((mode ==
194 (ME8200_DIO_CTRL_BIT_MODE_0 <<
195 (instance->dio_idx * 2))) || !mode) {
196 *value =
197 inb(instance->
198 port_reg) & (0x0001 << channel);
199 } else {
200 PERROR("Port not in output or input mode.\n");
201 err = ME_ERRNO_PREVIOUS_CONFIG;
203 } else {
204 PERROR("Invalid bit number specified.\n");
205 err = ME_ERRNO_INVALID_CHANNEL;
207 break;
209 case ME_IO_SINGLE_NO_FLAGS:
210 case ME_IO_SINGLE_TYPE_DIO_BYTE:
211 if (channel == 0) {
212 mode =
213 inb(instance->
214 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
215 ME8200_DIO_CTRL_BIT_MODE_1) <<
216 (instance->dio_idx * 2));
218 if ((mode ==
219 (ME8200_DIO_CTRL_BIT_MODE_0 <<
220 (instance->dio_idx * 2))) || !mode) {
221 *value = inb(instance->port_reg) & 0x00FF;
222 } else {
223 PERROR("Port not in output or input mode.\n");
224 err = ME_ERRNO_PREVIOUS_CONFIG;
226 } else {
227 PERROR("Invalid byte number specified.\n");
228 err = ME_ERRNO_INVALID_CHANNEL;
230 break;
232 default:
233 PERROR("Invalid flags specified.\n");
234 err = ME_ERRNO_INVALID_FLAGS;
236 spin_unlock(instance->ctrl_reg_lock);
237 spin_unlock(&instance->subdevice_lock);
239 ME_SUBDEVICE_EXIT;
241 return err;
244 static int me8200_dio_io_single_write(me_subdevice_t *subdevice,
245 struct file *filep,
246 int channel,
247 int value, int time_out, int flags)
249 me8200_dio_subdevice_t *instance;
250 int err = ME_ERRNO_SUCCESS;
251 uint8_t mode;
252 uint8_t byte;
254 PDEBUG("executed.\n");
256 instance = (me8200_dio_subdevice_t *) subdevice;
258 ME_SUBDEVICE_ENTER;
260 spin_lock(&instance->subdevice_lock);
261 spin_lock(instance->ctrl_reg_lock);
262 switch (flags) {
263 case ME_IO_SINGLE_TYPE_DIO_BIT:
264 if ((channel >= 0) && (channel < 8)) {
265 mode =
266 inb(instance->
267 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
268 ME8200_DIO_CTRL_BIT_MODE_1) <<
269 (instance->dio_idx * 2));
271 if (mode ==
272 (ME8200_DIO_CTRL_BIT_MODE_0 <<
273 (instance->dio_idx * 2))) {
274 byte = inb(instance->port_reg);
276 if (value)
277 byte |= 0x1 << channel;
278 else
279 byte &= ~(0x1 << channel);
281 outb(byte, instance->port_reg);
282 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
283 instance->reg_base,
284 instance->port_reg -
285 instance->reg_base, byte);
286 } else {
287 PERROR("Port not in output or input mode.\n");
288 err = ME_ERRNO_PREVIOUS_CONFIG;
290 } else {
291 PERROR("Invalid bit number specified.\n");
292 err = ME_ERRNO_INVALID_CHANNEL;
294 break;
296 case ME_IO_SINGLE_NO_FLAGS:
297 case ME_IO_SINGLE_TYPE_DIO_BYTE:
298 if (channel == 0) {
299 mode =
300 inb(instance->
301 ctrl_reg) & ((ME8200_DIO_CTRL_BIT_MODE_0 |
302 ME8200_DIO_CTRL_BIT_MODE_1) <<
303 (instance->dio_idx * 2));
305 if (mode ==
306 (ME8200_DIO_CTRL_BIT_MODE_0 <<
307 (instance->dio_idx * 2))) {
308 outb(value, instance->port_reg);
309 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
310 instance->reg_base,
311 instance->port_reg -
312 instance->reg_base, value);
313 } else {
314 PERROR("Port not in output or input mode.\n");
315 err = ME_ERRNO_PREVIOUS_CONFIG;
317 } else {
318 PERROR("Invalid byte number specified.\n");
319 err = ME_ERRNO_INVALID_CHANNEL;
321 break;
323 default:
324 PERROR("Invalid flags specified.\n");
325 err = ME_ERRNO_INVALID_FLAGS;
327 spin_unlock(instance->ctrl_reg_lock);
328 spin_unlock(&instance->subdevice_lock);
330 ME_SUBDEVICE_EXIT;
332 return err;
335 static int me8200_dio_query_number_channels(me_subdevice_t *subdevice,
336 int *number)
338 PDEBUG("executed.\n");
339 *number = 8;
340 return ME_ERRNO_SUCCESS;
343 static int me8200_dio_query_subdevice_type(me_subdevice_t *subdevice,
344 int *type, int *subtype)
346 PDEBUG("executed.\n");
347 *type = ME_TYPE_DIO;
348 *subtype = ME_SUBTYPE_SINGLE;
349 return ME_ERRNO_SUCCESS;
352 static int me8200_dio_query_subdevice_caps(me_subdevice_t *subdevice,
353 int *caps)
355 PDEBUG("executed.\n");
356 *caps = ME_CAPS_DIO_DIR_BYTE;
357 return ME_ERRNO_SUCCESS;
360 me8200_dio_subdevice_t *me8200_dio_constructor(uint32_t reg_base,
361 unsigned int dio_idx,
362 spinlock_t *ctrl_reg_lock)
364 me8200_dio_subdevice_t *subdevice;
365 int err;
367 PDEBUG("executed.\n");
369 /* Allocate memory for subdevice instance */
370 subdevice = kmalloc(sizeof(me8200_dio_subdevice_t), GFP_KERNEL);
372 if (!subdevice) {
373 PERROR("Cannot get memory for subdevice instance.\n");
374 return NULL;
377 memset(subdevice, 0, sizeof(me8200_dio_subdevice_t));
379 /* Initialize subdevice base class */
380 err = me_subdevice_init(&subdevice->base);
382 if (err) {
383 PERROR("Cannot initialize subdevice base class instance.\n");
384 kfree(subdevice);
385 return NULL;
387 // Initialize spin locks.
388 spin_lock_init(&subdevice->subdevice_lock);
390 subdevice->ctrl_reg_lock = ctrl_reg_lock;
392 /* Save digital i/o index */
393 subdevice->dio_idx = dio_idx;
395 /* Save the subdevice index */
396 subdevice->ctrl_reg = reg_base + ME8200_DIO_CTRL_REG;
397 subdevice->port_reg = reg_base + ME8200_DIO_PORT_REG + dio_idx;
398 #ifdef MEDEBUG_DEBUG_REG
399 subdevice->reg_base = reg_base;
400 #endif
402 /* Overload base class methods. */
403 subdevice->base.me_subdevice_io_reset_subdevice =
404 me8200_dio_io_reset_subdevice;
405 subdevice->base.me_subdevice_io_single_config =
406 me8200_dio_io_single_config;
407 subdevice->base.me_subdevice_io_single_read = me8200_dio_io_single_read;
408 subdevice->base.me_subdevice_io_single_write =
409 me8200_dio_io_single_write;
410 subdevice->base.me_subdevice_query_number_channels =
411 me8200_dio_query_number_channels;
412 subdevice->base.me_subdevice_query_subdevice_type =
413 me8200_dio_query_subdevice_type;
414 subdevice->base.me_subdevice_query_subdevice_caps =
415 me8200_dio_query_subdevice_caps;
417 return subdevice;