Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 giacomo 1
/* ------------------------------------------------------------------------- */
2
/* i2c-elv.c i2c-hw access for philips style parallel port adapters          */
3
/* ------------------------------------------------------------------------- */
4
/*   Copyright (C) 1995-2000 Simon G. Vogl
5
 
6
    This program is free software; you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation; either version 2 of the License, or
9
    (at your option) any later version.
10
 
11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with this program; if not, write to the Free Software
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
19
/* ------------------------------------------------------------------------- */
20
 
21
/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
22
   Frodo Looijaard <frodol@dds.nl> */
23
 
24
#include <linux/kernel.h>
25
#include <linux/module.h>
26
#include <linux/delay.h>
27
#include <linux/slab.h>
28
#include <linux/init.h>
29
#include <linux/ioport.h>
30
#include <linux/errno.h>
31
#include <linux/i2c.h>
32
#include <linux/i2c-algo-bit.h>
33
#include <asm/io.h>
34
 
35
#define DEFAULT_BASE 0x378
36
static int base=0;
37
static unsigned char port_data = 0;
38
 
39
/* ----- global defines ----------------------------------------------- */
40
#define DEB(x)          /* should be reasonable open, close &c.         */
41
#define DEB2(x)         /* low level debugging - very slow              */
42
#define DEBE(x) x       /* error messages                               */
43
#define DEBINIT(x) x    /* detection status messages                    */
44
 
45
/* --- Convenience defines for the parallel port:                       */
46
#define BASE    (unsigned int)(data)
47
#define DATA    BASE                    /* Centronics data port         */
48
#define STAT    (BASE+1)                /* Centronics status port       */
49
#define CTRL    (BASE+2)                /* Centronics control port      */
50
 
51
 
52
/* ----- local functions ---------------------------------------------- */
53
 
54
 
55
static void bit_elv_setscl(void *data, int state)
56
{
57
        if (state) {
58
                port_data &= 0xfe;
59
        } else {
60
                port_data |=1;
61
        }
62
        outb(port_data, DATA);
63
}
64
 
65
static void bit_elv_setsda(void *data, int state)
66
{
67
        if (state) {
68
                port_data &=0xfd;
69
        } else {
70
                port_data |=2;
71
        }
72
        outb(port_data, DATA);
73
}
74
 
75
static int bit_elv_getscl(void *data)
76
{
77
        return ( 0 == ( (inb_p(STAT)) & 0x08 ) );
78
}
79
 
80
static int bit_elv_getsda(void *data)
81
{
82
        return ( 0 == ( (inb_p(STAT)) & 0x40 ) );
83
}
84
 
85
static int bit_elv_init(void)
86
{
87
        if (!request_region(base, (base == 0x3bc) ? 3 : 8,
88
                                "i2c (ELV adapter)"))
89
                return -ENODEV;
90
 
91
        if (inb(base+1) & 0x80) {       /* BUSY should be high  */
92
                DEBINIT(printk(KERN_DEBUG "i2c-elv.o: Busy was low.\n"));
93
                goto fail;
94
        }
95
 
96
        outb(0x0c,base+2);      /* SLCT auf low         */
97
        udelay(400);
98
        if (!(inb(base+1) && 0x10)) {
99
                outb(0x04,base+2);
100
                DEBINIT(printk(KERN_DEBUG "i2c-elv.o: Select was high.\n"));
101
                goto fail;
102
        }
103
 
104
        port_data = 0;
105
        bit_elv_setsda((void*)base,1);
106
        bit_elv_setscl((void*)base,1);
107
        return 0;
108
 
109
fail:
110
        release_region(base , (base == 0x3bc) ? 3 : 8);
111
        return -ENODEV;
112
}
113
 
114
/* ------------------------------------------------------------------------
115
 * Encapsulate the above functions in the correct operations structure.
116
 * This is only done when more than one hardware adapter is supported.
117
 */
118
static struct i2c_algo_bit_data bit_elv_data = {
119
        .setsda         = bit_elv_setsda,
120
        .setscl         = bit_elv_setscl,
121
        .getsda         = bit_elv_getsda,
122
        .getscl         = bit_elv_getscl,
123
        .udelay         = 80,
124
        .mdelay         = 80,
125
        .timeout        = HZ
126
};
127
 
128
static struct i2c_adapter bit_elv_ops = {
129
        .owner          = THIS_MODULE,
130
        .algo_data      = &bit_elv_data,
131
        .name           = "ELV Parallel port adaptor",
132
};
133
 
134
static int __init i2c_bitelv_init(void)
135
{
136
        printk(KERN_INFO "i2c ELV parallel port adapter driver\n");
137
        if (base==0) {
138
                /* probe some values */
139
                base=DEFAULT_BASE;
140
                bit_elv_data.data=(void*)DEFAULT_BASE;
141
                if (bit_elv_init()==0) {
142
                        if(i2c_bit_add_bus(&bit_elv_ops) < 0)
143
                                return -ENODEV;
144
                } else {
145
                        return -ENODEV;
146
                }
147
        } else {
148
                i2c_set_adapdata(&bit_elv_ops, (void *)base);
149
                if (bit_elv_init()==0) {
150
                        if(i2c_bit_add_bus(&bit_elv_ops) < 0)
151
                                return -ENODEV;
152
                } else {
153
                        return -ENODEV;
154
                }
155
        }
156
        printk(KERN_DEBUG "i2c-elv.o: found device at %#x.\n",base);
157
        return 0;
158
}
159
 
160
static void __exit i2c_bitelv_exit(void)
161
{
162
        i2c_bit_del_bus(&bit_elv_ops);
163
        release_region(base , (base == 0x3bc) ? 3 : 8);
164
}
165
 
166
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
167
MODULE_DESCRIPTION("I2C-Bus adapter routines for ELV parallel port adapter");
168
MODULE_LICENSE("GPL");
169
 
170
MODULE_PARM(base, "i");
171
 
172
module_init(i2c_bitelv_init);
173
module_exit(i2c_bitelv_exit);