OSDN Git Service

Merge branch 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / staging / comedi / drivers / ssv_dnp.c
1 /*
2     comedi/drivers/ssv_dnp.c
3     generic comedi driver for SSV Embedded Systems' DIL/Net-PCs
4     Copyright (C) 2001 Robert Schwebel <robert@schwebel.de>
5
6     COMEDI - Linux Control and Measurement Device Interface
7     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 */
19 /*
20 Driver: ssv_dnp
21 Description: SSV Embedded Systems DIL/Net-PC
22 Author: Robert Schwebel <robert@schwebel.de>
23 Devices: [SSV Embedded Systems] DIL/Net-PC 1486 (dnp-1486)
24 Status: unknown
25 */
26
27 /* include files ----------------------------------------------------------- */
28
29 #include <linux/module.h>
30 #include "../comedidev.h"
31
32 /* Some global definitions: the registers of the DNP ----------------------- */
33 /*                                                                           */
34 /* For port A and B the mode register has bits corresponding to the output   */
35 /* pins, where Bit-N = 0 -> input, Bit-N = 1 -> output. Note that bits       */
36 /* 4 to 7 correspond to pin 0..3 for port C data register. Ensure that bits  */
37 /* 0..3 remain unchanged! For details about Port C Mode Register see         */
38 /* the remarks in dnp_insn_config() below.                                   */
39
40 #define CSCIR 0x22              /* Chip Setup and Control Index Register     */
41 #define CSCDR 0x23              /* Chip Setup and Control Data Register      */
42 #define PAMR  0xa5              /* Port A Mode Register                      */
43 #define PADR  0xa9              /* Port A Data Register                      */
44 #define PBMR  0xa4              /* Port B Mode Register                      */
45 #define PBDR  0xa8              /* Port B Data Register                      */
46 #define PCMR  0xa3              /* Port C Mode Register                      */
47 #define PCDR  0xa7              /* Port C Data Register                      */
48
49 /* ------------------------------------------------------------------------- */
50 /* The insn_bits interface allows packed reading/writing of DIO channels.    */
51 /* The comedi core can convert between insn_bits and insn_read/write, so you */
52 /* are able to use these instructions as well.                               */
53 /* ------------------------------------------------------------------------- */
54
55 static int dnp_dio_insn_bits(struct comedi_device *dev,
56                              struct comedi_subdevice *s,
57                              struct comedi_insn *insn, unsigned int *data)
58 {
59         /* The insn data is a mask in data[0] and the new data in data[1],   */
60         /* each channel cooresponding to a bit.                              */
61
62         /* Ports A and B are straight forward: each bit corresponds to an    */
63         /* output pin with the same order. Port C is different: bits 0...3   */
64         /* correspond to bits 4...7 of the output register (PCDR).           */
65
66         if (data[0]) {
67
68                 outb(PADR, CSCIR);
69                 outb((inb(CSCDR)
70                       & ~(u8) (data[0] & 0x0000FF))
71                      | (u8) (data[1] & 0x0000FF), CSCDR);
72
73                 outb(PBDR, CSCIR);
74                 outb((inb(CSCDR)
75                       & ~(u8) ((data[0] & 0x00FF00) >> 8))
76                      | (u8) ((data[1] & 0x00FF00) >> 8), CSCDR);
77
78                 outb(PCDR, CSCIR);
79                 outb((inb(CSCDR)
80                       & ~(u8) ((data[0] & 0x0F0000) >> 12))
81                      | (u8) ((data[1] & 0x0F0000) >> 12), CSCDR);
82         }
83
84         /* on return, data[1] contains the value of the digital input lines. */
85         outb(PADR, CSCIR);
86         data[0] = inb(CSCDR);
87         outb(PBDR, CSCIR);
88         data[0] += inb(CSCDR) << 8;
89         outb(PCDR, CSCIR);
90         data[0] += ((inb(CSCDR) & 0xF0) << 12);
91
92         return insn->n;
93
94 }
95
96 static int dnp_dio_insn_config(struct comedi_device *dev,
97                                struct comedi_subdevice *s,
98                                struct comedi_insn *insn,
99                                unsigned int *data)
100 {
101         unsigned int chan = CR_CHAN(insn->chanspec);
102         unsigned int mask;
103         unsigned int val;
104         int ret;
105
106         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
107         if (ret)
108                 return ret;
109
110         if (chan < 8) {                 /* Port A */
111                 mask = 1 << chan;
112                 outb(PAMR, CSCIR);
113         } else if (chan < 16) {         /* Port B */
114                 mask = 1 << (chan - 8);
115                 outb(PBMR, CSCIR);
116         } else {                        /* Port C */
117                 /*
118                  * We have to pay attention with port C.
119                  * This is the meaning of PCMR:
120                  *   Bit in PCMR:              7 6 5 4 3 2 1 0
121                  *   Corresponding port C pin: d 3 d 2 d 1 d 0   d= don't touch
122                  *
123                  * Multiplication by 2 brings bits into correct position
124                  * for PCMR!
125                  */
126                 mask = 1 << ((chan - 16) * 2);
127                 outb(PCMR, CSCIR);
128         }
129
130         val = inb(CSCDR);
131         if (data[0] == COMEDI_OUTPUT)
132                 val |= mask;
133         else
134                 val &= ~mask;
135         outb(val, CSCDR);
136
137         return insn->n;
138
139 }
140
141 static int dnp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
142 {
143         struct comedi_subdevice *s;
144         int ret;
145
146         ret = comedi_alloc_subdevices(dev, 1);
147         if (ret)
148                 return ret;
149
150         s = &dev->subdevices[0];
151         /* digital i/o subdevice                                             */
152         s->type = COMEDI_SUBD_DIO;
153         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
154         s->n_chan = 20;
155         s->maxdata = 1;
156         s->range_table = &range_digital;
157         s->insn_bits = dnp_dio_insn_bits;
158         s->insn_config = dnp_dio_insn_config;
159
160         /* We use the I/O ports 0x22,0x23 and 0xa3-0xa9, which are always
161          * allocated for the primary 8259, so we don't need to allocate them
162          * ourselves. */
163
164         /* configure all ports as input (default)                            */
165         outb(PAMR, CSCIR);
166         outb(0x00, CSCDR);
167         outb(PBMR, CSCIR);
168         outb(0x00, CSCDR);
169         outb(PCMR, CSCIR);
170         outb((inb(CSCDR) & 0xAA), CSCDR);
171
172         dev_info(dev->class_dev, "%s: attached\n", dev->board_name);
173         return 1;
174 }
175
176 static void dnp_detach(struct comedi_device *dev)
177 {
178         outb(PAMR, CSCIR);
179         outb(0x00, CSCDR);
180         outb(PBMR, CSCIR);
181         outb(0x00, CSCDR);
182         outb(PCMR, CSCIR);
183         outb((inb(CSCDR) & 0xAA), CSCDR);
184 }
185
186 static struct comedi_driver dnp_driver = {
187         .driver_name    = "dnp-1486",
188         .module         = THIS_MODULE,
189         .attach         = dnp_attach,
190         .detach         = dnp_detach,
191 };
192 module_comedi_driver(dnp_driver);
193
194 MODULE_AUTHOR("Comedi http://www.comedi.org");
195 MODULE_DESCRIPTION("Comedi low-level driver");
196 MODULE_LICENSE("GPL");