]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* ------------------------------------------------------------------------ * |
c6e8bb2c | 2 | * i2c-parport-light.c I2C bus over parallel port * |
1da177e4 | 3 | * ------------------------------------------------------------------------ * |
7c81c60f | 4 | Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de> |
07da0372 | 5 | |
1da177e4 LT |
6 | Based on older i2c-velleman.c driver |
7 | Copyright (C) 1995-2000 Simon G. Vogl | |
8 | With some changes from: | |
9 | Frodo Looijaard <frodol@dds.nl> | |
96de0e25 | 10 | Kyösti Mälkki <kmalkki@cc.hut.fi> |
07da0372 | 11 | |
1da177e4 LT |
12 | This program is free software; you can redistribute it and/or modify |
13 | it under the terms of the GNU General Public License as published by | |
14 | the Free Software Foundation; either version 2 of the License, or | |
15 | (at your option) any later version. | |
16 | ||
17 | This program is distributed in the hope that it will be useful, | |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | GNU General Public License for more details. | |
1da177e4 LT |
21 | * ------------------------------------------------------------------------ */ |
22 | ||
1da177e4 LT |
23 | #include <linux/kernel.h> |
24 | #include <linux/module.h> | |
25 | #include <linux/init.h> | |
6d376fcc | 26 | #include <linux/delay.h> |
c6e8bb2c | 27 | #include <linux/platform_device.h> |
1da177e4 LT |
28 | #include <linux/ioport.h> |
29 | #include <linux/i2c.h> | |
30 | #include <linux/i2c-algo-bit.h> | |
927ab2f8 | 31 | #include <linux/i2c-smbus.h> |
21782180 | 32 | #include <linux/io.h> |
1da177e4 LT |
33 | #include "i2c-parport.h" |
34 | ||
35 | #define DEFAULT_BASE 0x378 | |
c6e8bb2c JD |
36 | #define DRVNAME "i2c-parport-light" |
37 | ||
38 | static struct platform_device *pdev; | |
1da177e4 LT |
39 | |
40 | static u16 base; | |
c78babcc | 41 | module_param_hw(base, ushort, ioport, 0); |
1da177e4 LT |
42 | MODULE_PARM_DESC(base, "Base I/O address"); |
43 | ||
927ab2f8 | 44 | static int irq; |
c78babcc | 45 | module_param_hw(irq, int, irq, 0); |
927ab2f8 JD |
46 | MODULE_PARM_DESC(irq, "IRQ (optional)"); |
47 | ||
1da177e4 LT |
48 | /* ----- Low-level parallel port access ----------------------------------- */ |
49 | ||
50 | static inline void port_write(unsigned char p, unsigned char d) | |
51 | { | |
52 | outb(d, base+p); | |
53 | } | |
54 | ||
55 | static inline unsigned char port_read(unsigned char p) | |
56 | { | |
57 | return inb(base+p); | |
58 | } | |
59 | ||
60 | /* ----- Unified line operation functions --------------------------------- */ | |
61 | ||
62 | static inline void line_set(int state, const struct lineop *op) | |
63 | { | |
64 | u8 oldval = port_read(op->port); | |
65 | ||
66 | /* Touch only the bit(s) needed */ | |
67 | if ((op->inverted && !state) || (!op->inverted && state)) | |
68 | port_write(op->port, oldval | op->val); | |
69 | else | |
70 | port_write(op->port, oldval & ~op->val); | |
71 | } | |
72 | ||
73 | static inline int line_get(const struct lineop *op) | |
74 | { | |
75 | u8 oldval = port_read(op->port); | |
76 | ||
77 | return ((op->inverted && (oldval & op->val) != op->val) | |
78 | || (!op->inverted && (oldval & op->val) == op->val)); | |
79 | } | |
80 | ||
81 | /* ----- I2C algorithm call-back functions and structures ----------------- */ | |
82 | ||
83 | static void parport_setscl(void *data, int state) | |
84 | { | |
85 | line_set(state, &adapter_parm[type].setscl); | |
86 | } | |
87 | ||
88 | static void parport_setsda(void *data, int state) | |
89 | { | |
90 | line_set(state, &adapter_parm[type].setsda); | |
91 | } | |
92 | ||
93 | static int parport_getscl(void *data) | |
94 | { | |
95 | return line_get(&adapter_parm[type].getscl); | |
96 | } | |
97 | ||
98 | static int parport_getsda(void *data) | |
99 | { | |
100 | return line_get(&adapter_parm[type].getsda); | |
101 | } | |
102 | ||
103 | /* Encapsulate the functions above in the correct structure | |
104 | Note that getscl will be set to NULL by the attaching code for adapters | |
105 | that cannot read SCL back */ | |
106 | static struct i2c_algo_bit_data parport_algo_data = { | |
107 | .setsda = parport_setsda, | |
108 | .setscl = parport_setscl, | |
109 | .getsda = parport_getsda, | |
110 | .getscl = parport_getscl, | |
111 | .udelay = 50, | |
1da177e4 | 112 | .timeout = HZ, |
07da0372 | 113 | }; |
1da177e4 | 114 | |
c6e8bb2c | 115 | /* ----- Driver registration ---------------------------------------------- */ |
1da177e4 LT |
116 | |
117 | static struct i2c_adapter parport_adapter = { | |
118 | .owner = THIS_MODULE, | |
119 | .class = I2C_CLASS_HWMON, | |
1da177e4 LT |
120 | .algo_data = &parport_algo_data, |
121 | .name = "Parallel port adapter (light)", | |
122 | }; | |
123 | ||
927ab2f8 JD |
124 | /* SMBus alert support */ |
125 | static struct i2c_smbus_alert_setup alert_data = { | |
927ab2f8 JD |
126 | }; |
127 | static struct i2c_client *ara; | |
128 | static struct lineop parport_ctrl_irq = { | |
129 | .val = (1 << 4), | |
07da0372 | 130 | .port = PORT_CTRL, |
927ab2f8 JD |
131 | }; |
132 | ||
0b255e92 | 133 | static int i2c_parport_probe(struct platform_device *pdev) |
c6e8bb2c JD |
134 | { |
135 | int err; | |
c6e8bb2c JD |
136 | |
137 | /* Reset hardware to a sane state (SCL and SDA high) */ | |
138 | parport_setsda(NULL, 1); | |
139 | parport_setscl(NULL, 1); | |
140 | /* Other init if needed (power on...) */ | |
6d376fcc | 141 | if (adapter_parm[type].init.val) { |
c6e8bb2c | 142 | line_set(1, &adapter_parm[type].init); |
6d376fcc JD |
143 | /* Give powered devices some time to settle */ |
144 | msleep(100); | |
145 | } | |
c6e8bb2c JD |
146 | |
147 | parport_adapter.dev.parent = &pdev->dev; | |
148 | err = i2c_bit_add_bus(&parport_adapter); | |
927ab2f8 | 149 | if (err) { |
c6e8bb2c | 150 | dev_err(&pdev->dev, "Unable to register with I2C\n"); |
927ab2f8 JD |
151 | return err; |
152 | } | |
153 | ||
154 | /* Setup SMBus alert if supported */ | |
155 | if (adapter_parm[type].smbus_alert && irq) { | |
156 | alert_data.irq = irq; | |
157 | ara = i2c_setup_smbus_alert(&parport_adapter, &alert_data); | |
158 | if (ara) | |
159 | line_set(1, &parport_ctrl_irq); | |
160 | else | |
161 | dev_warn(&pdev->dev, "Failed to register ARA client\n"); | |
162 | } | |
163 | ||
164 | return 0; | |
c6e8bb2c JD |
165 | } |
166 | ||
0b255e92 | 167 | static int i2c_parport_remove(struct platform_device *pdev) |
c6e8bb2c | 168 | { |
927ab2f8 JD |
169 | if (ara) { |
170 | line_set(0, &parport_ctrl_irq); | |
171 | i2c_unregister_device(ara); | |
172 | ara = NULL; | |
173 | } | |
c6e8bb2c JD |
174 | i2c_del_adapter(&parport_adapter); |
175 | ||
176 | /* Un-init if needed (power off...) */ | |
177 | if (adapter_parm[type].init.val) | |
178 | line_set(0, &adapter_parm[type].init); | |
179 | ||
c6e8bb2c JD |
180 | return 0; |
181 | } | |
182 | ||
183 | static struct platform_driver i2c_parport_driver = { | |
184 | .driver = { | |
c6e8bb2c JD |
185 | .name = DRVNAME, |
186 | }, | |
187 | .probe = i2c_parport_probe, | |
0b255e92 | 188 | .remove = i2c_parport_remove, |
c6e8bb2c JD |
189 | }; |
190 | ||
191 | static int __init i2c_parport_device_add(u16 address) | |
192 | { | |
c6e8bb2c JD |
193 | int err; |
194 | ||
195 | pdev = platform_device_alloc(DRVNAME, -1); | |
196 | if (!pdev) { | |
197 | err = -ENOMEM; | |
198 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); | |
199 | goto exit; | |
200 | } | |
201 | ||
c6e8bb2c JD |
202 | err = platform_device_add(pdev); |
203 | if (err) { | |
204 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", | |
205 | err); | |
206 | goto exit_device_put; | |
207 | } | |
208 | ||
209 | return 0; | |
210 | ||
211 | exit_device_put: | |
212 | platform_device_put(pdev); | |
213 | exit: | |
214 | return err; | |
215 | } | |
1da177e4 LT |
216 | |
217 | static int __init i2c_parport_init(void) | |
218 | { | |
c6e8bb2c JD |
219 | int err; |
220 | ||
e97b81dd | 221 | if (type < 0) { |
c6e8bb2c | 222 | printk(KERN_ERR DRVNAME ": adapter type unspecified\n"); |
e97b81dd MH |
223 | return -ENODEV; |
224 | } | |
225 | ||
226 | if (type >= ARRAY_SIZE(adapter_parm)) { | |
c6e8bb2c | 227 | printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type); |
e97b81dd | 228 | return -ENODEV; |
1da177e4 | 229 | } |
7e3d7db5 | 230 | |
1da177e4 | 231 | if (base == 0) { |
c6e8bb2c | 232 | pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE); |
1da177e4 LT |
233 | base = DEFAULT_BASE; |
234 | } | |
235 | ||
9def2556 JD |
236 | if (!request_region(base, 3, DRVNAME)) |
237 | return -EBUSY; | |
238 | ||
927ab2f8 JD |
239 | if (irq != 0) |
240 | pr_info(DRVNAME ": using irq %d\n", irq); | |
241 | ||
07da0372 | 242 | if (!adapter_parm[type].getscl.val) |
1da177e4 LT |
243 | parport_algo_data.getscl = NULL; |
244 | ||
c6e8bb2c JD |
245 | /* Sets global pdev as a side effect */ |
246 | err = i2c_parport_device_add(base); | |
247 | if (err) | |
9def2556 | 248 | goto exit_release; |
1da177e4 | 249 | |
c6e8bb2c JD |
250 | err = platform_driver_register(&i2c_parport_driver); |
251 | if (err) | |
252 | goto exit_device; | |
7e3d7db5 | 253 | |
1da177e4 | 254 | return 0; |
c6e8bb2c JD |
255 | |
256 | exit_device: | |
257 | platform_device_unregister(pdev); | |
9def2556 JD |
258 | exit_release: |
259 | release_region(base, 3); | |
c6e8bb2c | 260 | return err; |
1da177e4 LT |
261 | } |
262 | ||
263 | static void __exit i2c_parport_exit(void) | |
264 | { | |
c6e8bb2c JD |
265 | platform_driver_unregister(&i2c_parport_driver); |
266 | platform_device_unregister(pdev); | |
9def2556 | 267 | release_region(base, 3); |
1da177e4 LT |
268 | } |
269 | ||
7c81c60f | 270 | MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>"); |
1da177e4 LT |
271 | MODULE_DESCRIPTION("I2C bus over parallel port (light)"); |
272 | MODULE_LICENSE("GPL"); | |
273 | ||
274 | module_init(i2c_parport_init); | |
275 | module_exit(i2c_parport_exit); |