1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/platform_device.h>
31#include <linux/ioport.h>
32#include <linux/i2c.h>
33#include <linux/i2c-algo-bit.h>
34#include <asm/io.h>
35#include "i2c-parport.h"
36
37#define DEFAULT_BASE 0x378
38#define DRVNAME "i2c-parport-light"
39
40static struct platform_device *pdev;
41
42static u16 base;
43module_param(base, ushort, 0);
44MODULE_PARM_DESC(base, "Base I/O address");
45
46
47
48static inline void port_write(unsigned char p, unsigned char d)
49{
50 outb(d, base+p);
51}
52
53static inline unsigned char port_read(unsigned char p)
54{
55 return inb(base+p);
56}
57
58
59
60static inline void line_set(int state, const struct lineop *op)
61{
62 u8 oldval = port_read(op->port);
63
64
65 if ((op->inverted && !state) || (!op->inverted && state))
66 port_write(op->port, oldval | op->val);
67 else
68 port_write(op->port, oldval & ~op->val);
69}
70
71static inline int line_get(const struct lineop *op)
72{
73 u8 oldval = port_read(op->port);
74
75 return ((op->inverted && (oldval & op->val) != op->val)
76 || (!op->inverted && (oldval & op->val) == op->val));
77}
78
79
80
81static void parport_setscl(void *data, int state)
82{
83 line_set(state, &adapter_parm[type].setscl);
84}
85
86static void parport_setsda(void *data, int state)
87{
88 line_set(state, &adapter_parm[type].setsda);
89}
90
91static int parport_getscl(void *data)
92{
93 return line_get(&adapter_parm[type].getscl);
94}
95
96static int parport_getsda(void *data)
97{
98 return line_get(&adapter_parm[type].getsda);
99}
100
101
102
103
104static struct i2c_algo_bit_data parport_algo_data = {
105 .setsda = parport_setsda,
106 .setscl = parport_setscl,
107 .getsda = parport_getsda,
108 .getscl = parport_getscl,
109 .udelay = 50,
110 .timeout = HZ,
111};
112
113
114
115static struct i2c_adapter parport_adapter = {
116 .owner = THIS_MODULE,
117 .class = I2C_CLASS_HWMON,
118 .algo_data = &parport_algo_data,
119 .name = "Parallel port adapter (light)",
120};
121
122static int __devinit i2c_parport_probe(struct platform_device *pdev)
123{
124 int err;
125
126
127 parport_setsda(NULL, 1);
128 parport_setscl(NULL, 1);
129
130 if (adapter_parm[type].init.val)
131 line_set(1, &adapter_parm[type].init);
132
133 parport_adapter.dev.parent = &pdev->dev;
134 err = i2c_bit_add_bus(&parport_adapter);
135 if (err)
136 dev_err(&pdev->dev, "Unable to register with I2C\n");
137 return err;
138}
139
140static int __devexit i2c_parport_remove(struct platform_device *pdev)
141{
142 i2c_del_adapter(&parport_adapter);
143
144
145 if (adapter_parm[type].init.val)
146 line_set(0, &adapter_parm[type].init);
147
148 return 0;
149}
150
151static struct platform_driver i2c_parport_driver = {
152 .driver = {
153 .owner = THIS_MODULE,
154 .name = DRVNAME,
155 },
156 .probe = i2c_parport_probe,
157 .remove = __devexit_p(i2c_parport_remove),
158};
159
160static int __init i2c_parport_device_add(u16 address)
161{
162 int err;
163
164 pdev = platform_device_alloc(DRVNAME, -1);
165 if (!pdev) {
166 err = -ENOMEM;
167 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
168 goto exit;
169 }
170
171 err = platform_device_add(pdev);
172 if (err) {
173 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
174 err);
175 goto exit_device_put;
176 }
177
178 return 0;
179
180exit_device_put:
181 platform_device_put(pdev);
182exit:
183 return err;
184}
185
186static int __init i2c_parport_init(void)
187{
188 int err;
189
190 if (type < 0) {
191 printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
192 return -ENODEV;
193 }
194
195 if (type >= ARRAY_SIZE(adapter_parm)) {
196 printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
197 return -ENODEV;
198 }
199
200 if (base == 0) {
201 pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
202 base = DEFAULT_BASE;
203 }
204
205 if (!request_region(base, 3, DRVNAME))
206 return -EBUSY;
207
208 if (!adapter_parm[type].getscl.val)
209 parport_algo_data.getscl = NULL;
210
211
212 err = i2c_parport_device_add(base);
213 if (err)
214 goto exit_release;
215
216 err = platform_driver_register(&i2c_parport_driver);
217 if (err)
218 goto exit_device;
219
220 return 0;
221
222exit_device:
223 platform_device_unregister(pdev);
224exit_release:
225 release_region(base, 3);
226 return err;
227}
228
229static void __exit i2c_parport_exit(void)
230{
231 platform_driver_unregister(&i2c_parport_driver);
232 platform_device_unregister(pdev);
233 release_region(base, 3);
234}
235
236MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
237MODULE_DESCRIPTION("I2C bus over parallel port (light)");
238MODULE_LICENSE("GPL");
239
240module_init(i2c_parport_init);
241module_exit(i2c_parport_exit);
242