1 /* ------------------------------------------------------------------------- */
2 /* i2c-velleman.c i2c-hw access for Velleman K8000 adapters */
3 /* ------------------------------------------------------------------------- */
4 /* Copyright (C) 1995-96, 2000 Simon G. Vogl
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.
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.
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 /* ------------------------------------------------------------------------- */
21 /* $Id: i2c-velleman.c,v 1.19 2000/01/24 02:06:33 mds Exp $ */
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>
32 #include <asm/param.h> /* for HZ */
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 */
39 /* Pin Port Inverted name */
40 #define I2C_SDA 0x02 /* ctrl bit 1 (inv) */
41 #define I2C_SCL 0x08 /* ctrl bit 3 (inv) */
43 #define I2C_SDAIN 0x10 /* stat bit 4 */
44 #define I2C_SCLIN 0x08 /* ctrl bit 3 (inv)(reads own output)*/
46 #define I2C_DMASK 0xfd
47 #define I2C_CMASK 0xf7
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 */
56 #define DEFAULT_BASE 0x378
59 /* ----- local functions --------------------------------------------------- */
61 static void bit_velle_setscl(void *data, int state)
64 outb(inb(CTRL) & I2C_CMASK, CTRL);
66 outb(inb(CTRL) | I2C_SCL, CTRL);
71 static void bit_velle_setsda(void *data, int state)
74 outb(inb(CTRL) & I2C_DMASK , CTRL);
76 outb(inb(CTRL) | I2C_SDA, CTRL);
81 static int bit_velle_getscl(void *data)
83 return ( 0 == ( (inb(CTRL)) & I2C_SCLIN ) );
86 static int bit_velle_getsda(void *data)
88 return ( 0 != ( (inb(STAT)) & I2C_SDAIN ) );
91 static int bit_velle_init(void)
93 if (check_region(base,(base == 0x3bc)? 3 : 8) < 0 ) {
94 DEBE(printk("i2c-velleman.o: Port %#x already in use.\n",
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);
106 static void __exit bit_velle_exit(void)
108 release_region( base , (base == 0x3bc)? 3 : 8 );
112 static int bit_velle_reg(struct i2c_client *client)
117 static int bit_velle_unreg(struct i2c_client *client)
122 static void bit_velle_inc_use(struct i2c_adapter *adap)
129 static void bit_velle_dec_use(struct i2c_adapter *adap)
136 /* ------------------------------------------------------------------------
137 * Encapsulate the above functions in the correct operations structure.
138 * This is only done when more than one hardware adapter is supported.
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,
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,
161 int __init i2c_bitvelle_init(void)
163 printk(KERN_INFO "i2c-velleman.o: i2c Velleman K8000 adapter module version %s (%s)\n", I2C_VERSION, I2C_DATE);
165 /* probe some values */
167 bit_velle_data.data=(void*)DEFAULT_BASE;
168 if (bit_velle_init()==0) {
169 if(i2c_bit_add_bus(&bit_velle_ops) < 0)
175 bit_velle_data.data=(void*)base;
176 if (bit_velle_init()==0) {
177 if(i2c_bit_add_bus(&bit_velle_ops) < 0)
183 printk(KERN_DEBUG "i2c-velleman.o: found device at %#x.\n",base);
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");
194 MODULE_PARM(base, "i");
196 int init_module(void)
198 return i2c_bitvelle_init();
201 void cleanup_module(void)
203 i2c_bit_del_bus(&bit_velle_ops);