commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / i2c / i2c-velleman.c
1 /* ------------------------------------------------------------------------- */
2 /* i2c-velleman.c i2c-hw access for Velleman K9000 adapters                  */
3 /* ------------------------------------------------------------------------- */
4 /*   Copyright (C) 1995-96, 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 /* $Id: i2c-velleman.c,v 1.29 2003/01/21 08:08:16 kmalkki Exp $ */
22
23 #include <linux/kernel.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-algo-bit.h>
30 #include <asm/io.h>
31
32 /* ----- global defines ----------------------------------------------- */
33 #define DEB(x)          /* should be reasonable open, close &c.         */
34 #define DEB2(x)         /* low level debugging - very slow              */
35 #define DEBE(x) x       /* error messages                               */
36
37                                         /* Pin Port  Inverted   name    */
38 #define I2C_SDA         0x02            /*  ctrl bit 1  (inv)   */
39 #define I2C_SCL         0x08            /*  ctrl bit 3  (inv)   */
40
41 #define I2C_SDAIN       0x10            /* stat bit 4           */
42 #define I2C_SCLIN       0x08            /* ctrl bit 3 (inv)(reads own output)*/
43
44 #define I2C_DMASK       0xfd
45 #define I2C_CMASK       0xf7
46
47
48 /* --- Convenience defines for the parallel port:                       */
49 #define BASE    (unsigned int)(data)
50 #define DATA    BASE                    /* Centronics data port         */
51 #define STAT    (BASE+1)                /* Centronics status port       */
52 #define CTRL    (BASE+2)                /* Centronics control port      */
53
54 #define DEFAULT_BASE 0x378
55 static int base=0;
56
57 /* ----- local functions --------------------------------------------------- */
58
59 static void bit_velle_setscl(void *data, int state)
60 {
61         if (state) {
62                 outb(inb(CTRL) & I2C_CMASK,   CTRL);
63         } else {
64                 outb(inb(CTRL) | I2C_SCL, CTRL);
65         }
66         
67 }
68
69 static void bit_velle_setsda(void *data, int state)
70 {
71         if (state) {
72                 outb(inb(CTRL) & I2C_DMASK , CTRL);
73         } else {
74                 outb(inb(CTRL) | I2C_SDA, CTRL);
75         }
76         
77
78
79 static int bit_velle_getscl(void *data)
80 {
81         return ( 0 == ( (inb(CTRL)) & I2C_SCLIN ) );
82 }
83
84 static int bit_velle_getsda(void *data)
85 {
86         return ( 0 != ( (inb(STAT)) & I2C_SDAIN ) );
87 }
88
89 static int bit_velle_init(void)
90 {
91         if (!request_region(base, (base == 0x3bc) ? 3 : 8, 
92                         "i2c (Vellemann adapter)"))
93                 return -ENODEV;
94
95         bit_velle_setsda((void*)base,1);
96         bit_velle_setscl((void*)base,1);
97         return 0;
98 }
99
100 /* ------------------------------------------------------------------------
101  * Encapsulate the above functions in the correct operations structure.
102  * This is only done when more than one hardware adapter is supported.
103  */
104
105 static struct i2c_algo_bit_data bit_velle_data = {
106         .setsda         = bit_velle_setsda,
107         .setscl         = bit_velle_setscl,
108         .getsda         = bit_velle_getsda,
109         .getscl         = bit_velle_getscl,
110         .udelay         = 10,
111         .mdelay         = 10,
112         .timeout        = HZ
113 };
114
115 static struct i2c_adapter bit_velle_ops = {
116         .owner          = THIS_MODULE,
117         .id             = I2C_HW_B_VELLE,
118         .algo_data      = &bit_velle_data,
119         .name           = "Velleman K8000",
120 };
121
122 static int __init i2c_bitvelle_init(void)
123 {
124         printk(KERN_INFO "i2c-velleman.o: i2c Velleman K8000 adapter module version %s (%s)\n", I2C_VERSION, I2C_DATE);
125         if (base==0) {
126                 /* probe some values */
127                 base=DEFAULT_BASE;
128                 bit_velle_data.data=(void*)DEFAULT_BASE;
129                 if (bit_velle_init()==0) {
130                         if(i2c_bit_add_bus(&bit_velle_ops) < 0)
131                                 return -ENODEV;
132                 } else {
133                         return -ENODEV;
134                 }
135         } else {
136                 bit_velle_data.data=(void*)base;
137                 if (bit_velle_init()==0) {
138                         if(i2c_bit_add_bus(&bit_velle_ops) < 0)
139                                 return -ENODEV;
140                 } else {
141                         return -ENODEV;
142                 }
143         }
144         printk(KERN_DEBUG "i2c-velleman.o: found device at %#x.\n",base);
145         return 0;
146 }
147
148 static void __exit i2c_bitvelle_exit(void)
149 {       
150         i2c_bit_del_bus(&bit_velle_ops);
151         release_region(base, (base == 0x3bc) ? 3 : 8);
152 }
153
154 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
155 MODULE_DESCRIPTION("I2C-Bus adapter routines for Velleman K8000 adapter");
156 MODULE_LICENSE("GPL");
157
158 MODULE_PARM(base, "i");
159
160 module_init(i2c_bitvelle_init);
161 module_exit(i2c_bitvelle_exit);