Merge branch 'simplify_PRT' into release
[linux-2.6] / drivers / staging / meilhaus / me1000_dio.c
1 /**
2  * @file me1000_dio.c
3  *
4  * @brief ME-1000 DIO subdevice instance.
5  * @note Copyright (C) 2006 Meilhaus Electronic GmbH (support@meilhaus.de)
6  * @author Guenter Gebhardt
7  * @author Krzysztof Gantzke    (k.gantzke@meilhaus.de)
8  */
9
10 /*
11  * Copyright (C) 2006 Meilhaus Electronic GmbH (support@meilhaus.de)
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifndef __KERNEL__
29 #  define __KERNEL__
30 #endif
31
32 /*
33  * Includes
34  */
35 #include <linux/module.h>
36
37 #include <linux/slab.h>
38 #include <linux/spinlock.h>
39 #include <asm/io.h>
40 #include <linux/types.h>
41
42 #include "medefines.h"
43 #include "meinternal.h"
44 #include "meerror.h"
45 #include "medebug.h"
46
47 #include "me1000_dio_reg.h"
48 #include "me1000_dio.h"
49
50 /*
51  * Defines
52  */
53 #define ME1000_DIO_MAGIC_NUMBER 0x1000  /**< The magic number of the class structure. */
54
55 /*
56  * Functions
57  */
58
59 static int me1000_dio_io_reset_subdevice(struct me_subdevice *subdevice,
60                                          struct file *filep, int flags)
61 {
62         me1000_dio_subdevice_t *instance;
63         uint32_t tmp;
64
65         PDEBUG("executed.\n");
66
67         instance = (me1000_dio_subdevice_t *) subdevice;
68
69         if (flags) {
70                 PERROR("Invalid flag specified.\n");
71                 return ME_ERRNO_INVALID_FLAGS;
72         }
73
74         ME_SUBDEVICE_ENTER;
75
76         spin_lock(&instance->subdevice_lock);
77         spin_lock(instance->ctrl_reg_lock);
78         tmp = inl(instance->ctrl_reg);
79         tmp &= ~(0x1 << instance->dio_idx);
80         outl(tmp, instance->ctrl_reg);
81         PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
82                    instance->ctrl_reg - instance->reg_base, tmp);
83         spin_unlock(instance->ctrl_reg_lock);
84
85         outl(0x00000000, instance->port_reg);
86         PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n", instance->reg_base,
87                    instance->ctrl_reg - instance->reg_base, 0);
88         spin_unlock(&instance->subdevice_lock);
89
90         ME_SUBDEVICE_EXIT;
91
92         return ME_ERRNO_SUCCESS;
93 }
94
95 static int me1000_dio_io_single_config(struct me_subdevice *subdevice,
96                                        struct file *filep,
97                                        int channel,
98                                        int single_config,
99                                        int ref,
100                                        int trig_chan,
101                                        int trig_type, int trig_edge, int flags)
102 {
103         me1000_dio_subdevice_t *instance;
104         int err = ME_ERRNO_SUCCESS;
105         int ctrl;
106         int size =
107             flags & (ME_IO_SINGLE_CONFIG_DIO_BIT | ME_IO_SINGLE_CONFIG_DIO_BYTE
108                      | ME_IO_SINGLE_CONFIG_DIO_WORD |
109                      ME_IO_SINGLE_CONFIG_DIO_DWORD);
110
111         PDEBUG("executed.\n");
112
113         instance = (me1000_dio_subdevice_t *) subdevice;
114
115         ME_SUBDEVICE_ENTER;
116
117         spin_lock(&instance->subdevice_lock);
118         spin_lock(instance->ctrl_reg_lock);
119         ctrl = inl(instance->ctrl_reg);
120
121         switch (size) {
122         case ME_IO_SINGLE_CONFIG_NO_FLAGS:
123         case ME_IO_SINGLE_CONFIG_DIO_DWORD:
124                 if (channel == 0) {
125                         if (single_config == ME_SINGLE_CONFIG_DIO_INPUT) {
126                                 ctrl &= ~(0x1 << instance->dio_idx);
127                         } else if (single_config == ME_SINGLE_CONFIG_DIO_OUTPUT) {
128                                 ctrl |= 0x1 << instance->dio_idx;
129                         } else {
130                                 PERROR("Invalid port direction.\n");
131                                 err = ME_ERRNO_INVALID_SINGLE_CONFIG;
132                         }
133                 } else {
134                         PERROR("Invalid channel number.\n");
135                         err = ME_ERRNO_INVALID_CHANNEL;
136                 }
137                 break;
138
139         default:
140                 PERROR("Invalid flags.\n");
141                 err = ME_ERRNO_INVALID_FLAGS;
142         }
143
144         if (!err) {
145                 outl(ctrl, instance->ctrl_reg);
146                 PDEBUG_REG("ctrl_reg outl(0x%lX+0x%lX)=0x%x\n",
147                            instance->reg_base,
148                            instance->ctrl_reg - instance->reg_base, ctrl);
149         }
150         spin_unlock(instance->ctrl_reg_lock);
151         spin_unlock(&instance->subdevice_lock);
152
153         ME_SUBDEVICE_EXIT;
154
155         return err;
156 }
157
158 static int me1000_dio_io_single_read(struct me_subdevice *subdevice,
159                                      struct file *filep,
160                                      int channel,
161                                      int *value, int time_out, int flags)
162 {
163         me1000_dio_subdevice_t *instance;
164         int err = ME_ERRNO_SUCCESS;
165
166         PDEBUG("executed.\n");
167
168         instance = (me1000_dio_subdevice_t *) subdevice;
169
170         ME_SUBDEVICE_ENTER;
171
172         spin_lock(&instance->subdevice_lock);
173         switch (flags) {
174         case ME_IO_SINGLE_TYPE_DIO_BIT:
175                 if ((channel >= 0) && (channel < 32)) {
176                         *value = inl(instance->port_reg) & (0x1 << channel);
177                 } else {
178                         PERROR("Invalid bit number.\n");
179                         err = ME_ERRNO_INVALID_CHANNEL;
180                 }
181                 break;
182
183         case ME_IO_SINGLE_TYPE_DIO_BYTE:
184                 if ((channel >= 0) && (channel < 4)) {
185                         *value =
186                             (inl(instance->port_reg) >> (channel * 8)) & 0xFF;
187                 } else {
188                         PERROR("Invalid byte number.\n");
189                         err = ME_ERRNO_INVALID_CHANNEL;
190                 }
191                 break;
192
193         case ME_IO_SINGLE_TYPE_DIO_WORD:
194                 if ((channel >= 0) && (channel < 2)) {
195                         *value =
196                             (inl(instance->port_reg) >> (channel * 16)) &
197                             0xFFFF;
198                 } else {
199                         PERROR("Invalid word number.\n");
200                         err = ME_ERRNO_INVALID_CHANNEL;
201                 }
202                 break;
203
204         case ME_IO_SINGLE_NO_FLAGS:
205         case ME_IO_SINGLE_TYPE_DIO_DWORD:
206                 if (channel == 0) {
207                         *value = inl(instance->port_reg);
208                 } else {
209                         PERROR("Invalid dword number.\n");
210                         err = ME_ERRNO_INVALID_CHANNEL;
211                 }
212                 break;
213
214         default:
215                 PERROR("Invalid flags specified.\n");
216                 err = ME_ERRNO_INVALID_FLAGS;
217         }
218         spin_unlock(&instance->subdevice_lock);
219
220         ME_SUBDEVICE_EXIT;
221
222         return err;
223 }
224
225 static int me1000_dio_io_single_write(struct me_subdevice *subdevice,
226                                       struct file *filep,
227                                       int channel,
228                                       int value, int time_out, int flags)
229 {
230         me1000_dio_subdevice_t *instance;
231         int err = ME_ERRNO_SUCCESS;
232         uint32_t config;
233         uint32_t state;
234
235         PDEBUG("executed.\n");
236
237         instance = (me1000_dio_subdevice_t *) subdevice;
238
239         ME_SUBDEVICE_ENTER spin_lock(&instance->subdevice_lock);
240         spin_lock(instance->ctrl_reg_lock);
241         config = inl(instance->ctrl_reg) & (0x1 << instance->dio_idx);
242         switch (flags) {
243         case ME_IO_SINGLE_TYPE_DIO_BIT:
244                 if ((channel >= 0) && (channel < 32)) {
245                         if (config) {
246                                 state = inl(instance->port_reg);
247                                 state =
248                                     value ? (state | (0x1 << channel)) : (state
249                                                                           &
250                                                                           ~(0x1
251                                                                             <<
252                                                                             channel));
253                                 outl(state, instance->port_reg);
254                                 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
255                                            instance->reg_base,
256                                            instance->port_reg -
257                                            instance->reg_base, state);
258                         } else {
259                                 PERROR("Port is not in output mode.\n");
260                                 err = ME_ERRNO_PREVIOUS_CONFIG;
261                         }
262                 } else {
263                         PERROR("Invalid bit number.\n");
264                         err = ME_ERRNO_INVALID_CHANNEL;
265                 }
266                 break;
267
268         case ME_IO_SINGLE_TYPE_DIO_BYTE:
269                 if ((channel >= 0) && (channel < 4)) {
270                         if (config) {
271                                 state = inl(instance->port_reg);
272                                 state &= ~(0xFF << (channel * 8));
273                                 state |= (value & 0xFF) << (channel * 8);
274                                 outl(state, instance->port_reg);
275                                 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
276                                            instance->reg_base,
277                                            instance->port_reg -
278                                            instance->reg_base, state);
279                         } else {
280                                 PERROR("Port is not in output mode.\n");
281                                 err = ME_ERRNO_PREVIOUS_CONFIG;
282                         }
283                 } else {
284                         PERROR("Invalid byte number.\n");
285                         err = ME_ERRNO_INVALID_CHANNEL;
286                 }
287                 break;
288
289         case ME_IO_SINGLE_TYPE_DIO_WORD:
290                 if ((channel >= 0) && (channel < 2)) {
291                         if (config) {
292                                 state = inl(instance->port_reg);
293                                 state &= ~(0xFFFF << (channel * 16));
294                                 state |= (value & 0xFFFF) << (channel * 16);
295                                 outl(state, instance->port_reg);
296                                 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
297                                            instance->reg_base,
298                                            instance->port_reg -
299                                            instance->reg_base, state);
300                         } else {
301                                 PERROR("Port is not in output mode.\n");
302                                 err = ME_ERRNO_PREVIOUS_CONFIG;
303                         }
304                 } else {
305                         PERROR("Invalid word number.\n");
306                         err = ME_ERRNO_INVALID_CHANNEL;
307                 }
308                 break;
309
310         case ME_IO_SINGLE_NO_FLAGS:
311         case ME_IO_SINGLE_TYPE_DIO_DWORD:
312                 if (channel == 0) {
313                         if (config) {
314                                 outl(value, instance->port_reg);
315                                 PDEBUG_REG("port_reg outl(0x%lX+0x%lX)=0x%x\n",
316                                            instance->reg_base,
317                                            instance->port_reg -
318                                            instance->reg_base, value);
319                         } else {
320                                 PERROR("Port is not in output mode.\n");
321                                 err = ME_ERRNO_PREVIOUS_CONFIG;
322                         }
323                 } else {
324                         PERROR("Invalid dword number.\n");
325                         err = ME_ERRNO_INVALID_CHANNEL;
326                 }
327                 break;
328
329         default:
330                 PERROR("Invalid flags specified.\n");
331                 err = ME_ERRNO_INVALID_FLAGS;
332         }
333         spin_unlock(instance->ctrl_reg_lock);
334         spin_unlock(&instance->subdevice_lock);
335
336         ME_SUBDEVICE_EXIT;
337
338         return err;
339 }
340
341 static int me1000_dio_query_number_channels(struct me_subdevice *subdevice,
342                                             int *number)
343 {
344         PDEBUG("executed.\n");
345         *number = ME1000_DIO_NUMBER_CHANNELS;
346         return ME_ERRNO_SUCCESS;
347 }
348
349 static int me1000_dio_query_subdevice_type(struct me_subdevice *subdevice,
350                                            int *type, int *subtype)
351 {
352         PDEBUG("executed.\n");
353         *type = ME_TYPE_DIO;
354         *subtype = ME_SUBTYPE_SINGLE;
355         return ME_ERRNO_SUCCESS;
356 }
357
358 static int me1000_dio_query_subdevice_caps(struct me_subdevice *subdevice,
359                                            int *caps)
360 {
361         me1000_dio_subdevice_t *instance;
362
363         PDEBUG("executed.\n");
364
365         instance = (me1000_dio_subdevice_t *) subdevice;
366
367         *caps = ME_CAPS_DIO_DIR_DWORD;
368
369         return ME_ERRNO_SUCCESS;
370 }
371
372 me1000_dio_subdevice_t *me1000_dio_constructor(uint32_t reg_base,
373                                                unsigned int dio_idx,
374                                                spinlock_t * ctrl_reg_lock)
375 {
376         me1000_dio_subdevice_t *subdevice;
377         int err;
378
379         PDEBUG("executed.\n");
380
381         /* Allocate memory for subdevice instance */
382         subdevice = kmalloc(sizeof(me1000_dio_subdevice_t), GFP_KERNEL);
383
384         if (!subdevice) {
385                 PERROR("Cannot get memory for ME-1000 DIO instance.\n");
386                 return NULL;
387         }
388
389         memset(subdevice, 0, sizeof(me1000_dio_subdevice_t));
390
391         /* Check if counter index is out of range */
392
393         if (dio_idx >= ME1000_DIO_NUMBER_PORTS) {
394                 PERROR("DIO index is out of range.\n");
395                 kfree(subdevice);
396                 return NULL;
397         }
398
399         /* Initialize subdevice base class */
400         err = me_subdevice_init(&subdevice->base);
401
402         if (err) {
403                 PERROR("Cannot initialize subdevice base class instance.\n");
404                 kfree(subdevice);
405                 return NULL;
406         }
407         // Initialize spin locks.
408         spin_lock_init(&subdevice->subdevice_lock);
409         subdevice->ctrl_reg_lock = ctrl_reg_lock;
410
411         /* Save the DIO index */
412         subdevice->dio_idx = dio_idx;
413
414         /* Initialize registers. */
415 #ifdef MEDEBUG_DEBUG_REG
416         subdevice->reg_base = reg_base;
417 #endif
418         subdevice->ctrl_reg = reg_base + ME1000_PORT_MODE;
419         subdevice->port_reg =
420             reg_base + ME1000_PORT + (dio_idx * ME1000_PORT_STEP);
421
422         /* Override base class methods. */
423         subdevice->base.me_subdevice_io_reset_subdevice =
424             me1000_dio_io_reset_subdevice;
425         subdevice->base.me_subdevice_io_single_config =
426             me1000_dio_io_single_config;
427         subdevice->base.me_subdevice_io_single_read = me1000_dio_io_single_read;
428         subdevice->base.me_subdevice_io_single_write =
429             me1000_dio_io_single_write;
430         subdevice->base.me_subdevice_query_number_channels =
431             me1000_dio_query_number_channels;
432         subdevice->base.me_subdevice_query_subdevice_type =
433             me1000_dio_query_subdevice_type;
434         subdevice->base.me_subdevice_query_subdevice_caps =
435             me1000_dio_query_subdevice_caps;
436
437         return subdevice;
438 }