Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
420 | giacomo | 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.1 2004-01-28 15:12:07 giacomo 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 | .algo_data = &bit_velle_data, |
||
118 | .name = "Velleman K8000", |
||
119 | }; |
||
120 | |||
121 | static int __init i2c_bitvelle_init(void) |
||
122 | { |
||
123 | printk(KERN_INFO "i2c-velleman: i2c Velleman K8000 driver\n"); |
||
124 | if (base==0) { |
||
125 | /* probe some values */ |
||
126 | base=DEFAULT_BASE; |
||
127 | bit_velle_data.data=(void*)DEFAULT_BASE; |
||
128 | if (bit_velle_init()==0) { |
||
129 | if(i2c_bit_add_bus(&bit_velle_ops) < 0) |
||
130 | return -ENODEV; |
||
131 | } else { |
||
132 | return -ENODEV; |
||
133 | } |
||
134 | } else { |
||
135 | bit_velle_data.data=(void*)base; |
||
136 | if (bit_velle_init()==0) { |
||
137 | if(i2c_bit_add_bus(&bit_velle_ops) < 0) |
||
138 | return -ENODEV; |
||
139 | } else { |
||
140 | return -ENODEV; |
||
141 | } |
||
142 | } |
||
143 | printk(KERN_DEBUG "i2c-velleman: found device at %#x.\n",base); |
||
144 | return 0; |
||
145 | } |
||
146 | |||
147 | static void __exit i2c_bitvelle_exit(void) |
||
148 | { |
||
149 | i2c_bit_del_bus(&bit_velle_ops); |
||
150 | release_region(base, (base == 0x3bc) ? 3 : 8); |
||
151 | } |
||
152 | |||
153 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
||
154 | MODULE_DESCRIPTION("I2C-Bus adapter routines for Velleman K8000 adapter"); |
||
155 | MODULE_LICENSE("GPL"); |
||
156 | |||
157 | MODULE_PARM(base, "i"); |
||
158 | |||
159 | module_init(i2c_bitvelle_init); |
||
160 | module_exit(i2c_bitvelle_exit); |