1 /* ------------------------------------------------------------------------- */
2 /* i2c-velleman.c i2c-hw access for Velleman K8000 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.19 2000/01/24 02:06:33 mds 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/errno.h>
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-algo-bit.h>
31 #include <asm/io.h>
32 #include <asm/param.h> /* for HZ */
33 
34 /* ----- global defines -----------------------------------------------	*/
35 #define DEB(x)		/* should be reasonable open, close &c. 	*/
36 #define DEB2(x) 	/* low level debugging - very slow 		*/
37 #define DEBE(x)	x	/* error messages 				*/
38 
39 					/* Pin Port  Inverted	name	*/
40 #define I2C_SDA		0x02		/*  ctrl bit 1 	(inv)	*/
41 #define I2C_SCL		0x08		/*  ctrl bit 3 	(inv)	*/
42 
43 #define I2C_SDAIN	0x10		/* stat bit 4		*/
44 #define I2C_SCLIN	0x08		/* ctrl bit 3 (inv)(reads own output)*/
45 
46 #define I2C_DMASK	0xfd
47 #define I2C_CMASK	0xf7
48 
49 
50 /* --- Convenience defines for the parallel port:			*/
51 #define BASE	(unsigned int)(data)
52 #define DATA	BASE			/* Centronics data port		*/
53 #define STAT	(BASE+1)		/* Centronics status port	*/
54 #define CTRL	(BASE+2)		/* Centronics control port	*/
55 
56 #define DEFAULT_BASE 0x378
57 static int base=0;
58 
59 /* ----- local functions --------------------------------------------------- */
60 
bit_velle_setscl(void * data,int state)61 static void bit_velle_setscl(void *data, int state)
62 {
63 	if (state) {
64 		outb(inb(CTRL) & I2C_CMASK,   CTRL);
65 	} else {
66 		outb(inb(CTRL) | I2C_SCL, CTRL);
67 	}
68 
69 }
70 
bit_velle_setsda(void * data,int state)71 static void bit_velle_setsda(void *data, int state)
72 {
73 	if (state) {
74 		outb(inb(CTRL) & I2C_DMASK , CTRL);
75 	} else {
76 		outb(inb(CTRL) | I2C_SDA, CTRL);
77 	}
78 
79 }
80 
bit_velle_getscl(void * data)81 static int bit_velle_getscl(void *data)
82 {
83 	return ( 0 == ( (inb(CTRL)) & I2C_SCLIN ) );
84 }
85 
bit_velle_getsda(void * data)86 static int bit_velle_getsda(void *data)
87 {
88 	return ( 0 != ( (inb(STAT)) & I2C_SDAIN ) );
89 }
90 
bit_velle_init(void)91 static int bit_velle_init(void)
92 {
93 	if (check_region(base,(base == 0x3bc)? 3 : 8) < 0 ) {
94 		DEBE(printk("i2c-velleman.o: Port %#x already in use.\n",
95 		     base));
96 		return -ENODEV;
97 	} else {
98 		request_region(base, (base == 0x3bc)? 3 : 8,
99 			"i2c (Vellemann adapter)");
100 		bit_velle_setsda((void*)base,1);
101 		bit_velle_setscl((void*)base,1);
102 	}
103 	return 0;
104 }
105 
bit_velle_exit(void)106 static void __exit bit_velle_exit(void)
107 {
108 	release_region( base , (base == 0x3bc)? 3 : 8 );
109 }
110 
111 
bit_velle_reg(struct i2c_client * client)112 static int bit_velle_reg(struct i2c_client *client)
113 {
114 	return 0;
115 }
116 
bit_velle_unreg(struct i2c_client * client)117 static int bit_velle_unreg(struct i2c_client *client)
118 {
119 	return 0;
120 }
121 
bit_velle_inc_use(struct i2c_adapter * adap)122 static void bit_velle_inc_use(struct i2c_adapter *adap)
123 {
124 #ifdef MODULE
125 	MOD_INC_USE_COUNT;
126 #endif
127 }
128 
bit_velle_dec_use(struct i2c_adapter * adap)129 static void bit_velle_dec_use(struct i2c_adapter *adap)
130 {
131 #ifdef MODULE
132 	MOD_DEC_USE_COUNT;
133 #endif
134 }
135 
136 /* ------------------------------------------------------------------------
137  * Encapsulate the above functions in the correct operations structure.
138  * This is only done when more than one hardware adapter is supported.
139  */
140 
141 static struct i2c_algo_bit_data bit_velle_data = {
142 	.setsda		= bit_velle_setsda,
143 	.setscl		= bit_velle_setscl,
144 	.getsda		= bit_velle_getsda,
145 	.getscl		= bit_velle_getscl,
146 	.udelay		= 10,
147 	.mdelay		= 10,
148 	.timeout	= HZ
149 };
150 
151 static struct i2c_adapter bit_velle_ops = {
152 	.name		= "Velleman K8000",
153 	.id		= I2C_HW_B_VELLE,
154 	.algo_data	= &bit_velle_data,
155 	.inc_use	= bit_velle_inc_use,
156 	.dec_use	= bit_velle_dec_use,
157 	.client_register = bit_velle_reg,
158 	.client_unregister = bit_velle_unreg,
159 };
160 
i2c_bitvelle_init(void)161 int __init  i2c_bitvelle_init(void)
162 {
163 	printk(KERN_INFO "i2c-velleman.o: i2c Velleman K8000 adapter module version %s (%s)\n", I2C_VERSION, I2C_DATE);
164 	if (base==0) {
165 		/* probe some values */
166 		base=DEFAULT_BASE;
167 		bit_velle_data.data=(void*)DEFAULT_BASE;
168 		if (bit_velle_init()==0) {
169 			if(i2c_bit_add_bus(&bit_velle_ops) < 0)
170 				return -ENODEV;
171 		} else {
172 			return -ENODEV;
173 		}
174 	} else {
175 		bit_velle_data.data=(void*)base;
176 		if (bit_velle_init()==0) {
177 			if(i2c_bit_add_bus(&bit_velle_ops) < 0)
178 				return -ENODEV;
179 		} else {
180 			return -ENODEV;
181 		}
182 	}
183 	printk(KERN_DEBUG "i2c-velleman.o: found device at %#x.\n",base);
184 	return 0;
185 }
186 
187 EXPORT_NO_SYMBOLS;
188 
189 #ifdef MODULE
190 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
191 MODULE_DESCRIPTION("I2C-Bus adapter routines for Velleman K8000 adapter");
192 MODULE_LICENSE("GPL");
193 
194 MODULE_PARM(base, "i");
195 
init_module(void)196 int init_module(void)
197 {
198 	return i2c_bitvelle_init();
199 }
200 
cleanup_module(void)201 void cleanup_module(void)
202 {
203 	i2c_bit_del_bus(&bit_velle_ops);
204 	bit_velle_exit();
205 }
206 
207 #endif
208