File: /usr/src/linux/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.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/string.h>  /* for 2.0 kernels to get NULL   */
28     #include <asm/errno.h>     /* for 2.0 kernels to get ENODEV */
29     #include <asm/io.h>
30     
31     #include <linux/i2c.h>
32     #include <linux/i2c-algo-bit.h>
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     
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     
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     
81     static int bit_velle_getscl(void *data)
82     {
83     	return ( 0 == ( (inb(CTRL)) & I2C_SCLIN ) );
84     }
85     
86     static int bit_velle_getsda(void *data)
87     {
88     	return ( 0 != ( (inb(STAT)) & I2C_SDAIN ) );
89     }
90     
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     
106     static void bit_velle_exit(void)
107     {	
108     	release_region( base , (base == 0x3bc)? 3 : 8 );
109     }
110     
111     
112     static int bit_velle_reg(struct i2c_client *client)
113     {
114     	return 0;
115     }
116     
117     static int bit_velle_unreg(struct i2c_client *client)
118     {
119     	return 0;
120     }
121     
122     static void bit_velle_inc_use(struct i2c_adapter *adap)
123     {
124     #ifdef MODULE
125     	MOD_INC_USE_COUNT;
126     #endif
127     }
128     
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     	NULL,
143     	bit_velle_setsda,
144     	bit_velle_setscl,
145     	bit_velle_getsda,
146     	bit_velle_getscl,
147     	10, 10, 100,		/*	waits, timeout */
148     };
149     
150     static struct i2c_adapter bit_velle_ops = {
151     	"Velleman K8000",
152     	I2C_HW_B_VELLE,
153     	NULL,
154     	&bit_velle_data,
155     	bit_velle_inc_use,
156     	bit_velle_dec_use,
157     	bit_velle_reg,
158     	bit_velle_unreg,
159     };
160     
161     int __init  i2c_bitvelle_init(void)
162     {
163     	printk("i2c-velleman.o: i2c Velleman K8000 adapter module\n");
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("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     
193     MODULE_PARM(base, "i");
194     
195     int init_module(void) 
196     {
197     	return i2c_bitvelle_init();
198     }
199     
200     void cleanup_module(void) 
201     {
202     	i2c_bit_del_bus(&bit_velle_ops);
203     	bit_velle_exit();
204     }
205     
206     #endif
207