Linux 2.6.33-rc8
[linux-2.6/lguest.git] / drivers / i2c / busses / i2c-parport-light.c
blob322c5691e38eb72072c8135f8036dd35e991d958
1 /* ------------------------------------------------------------------------ *
2 * i2c-parport-light.c I2C bus over parallel port *
3 * ------------------------------------------------------------------------ *
4 Copyright (C) 2003-2007 Jean Delvare <khali@linux-fr.org>
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>
10 Kyösti Mälkki <kmalkki@cc.hut.fi>
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.
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.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * ------------------------------------------------------------------------ */
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"
37 #define DEFAULT_BASE 0x378
38 #define DRVNAME "i2c-parport-light"
40 static struct platform_device *pdev;
42 static u16 base;
43 module_param(base, ushort, 0);
44 MODULE_PARM_DESC(base, "Base I/O address");
46 /* ----- Low-level parallel port access ----------------------------------- */
48 static inline void port_write(unsigned char p, unsigned char d)
50 outb(d, base+p);
53 static inline unsigned char port_read(unsigned char p)
55 return inb(base+p);
58 /* ----- Unified line operation functions --------------------------------- */
60 static inline void line_set(int state, const struct lineop *op)
62 u8 oldval = port_read(op->port);
64 /* Touch only the bit(s) needed */
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);
71 static inline int line_get(const struct lineop *op)
73 u8 oldval = port_read(op->port);
75 return ((op->inverted && (oldval & op->val) != op->val)
76 || (!op->inverted && (oldval & op->val) == op->val));
79 /* ----- I2C algorithm call-back functions and structures ----------------- */
81 static void parport_setscl(void *data, int state)
83 line_set(state, &adapter_parm[type].setscl);
86 static void parport_setsda(void *data, int state)
88 line_set(state, &adapter_parm[type].setsda);
91 static int parport_getscl(void *data)
93 return line_get(&adapter_parm[type].getscl);
96 static int parport_getsda(void *data)
98 return line_get(&adapter_parm[type].getsda);
101 /* Encapsulate the functions above in the correct structure
102 Note that getscl will be set to NULL by the attaching code for adapters
103 that cannot read SCL back */
104 static 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,
113 /* ----- Driver registration ---------------------------------------------- */
115 static 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)",
122 static int __devinit i2c_parport_probe(struct platform_device *pdev)
124 int err;
126 /* Reset hardware to a sane state (SCL and SDA high) */
127 parport_setsda(NULL, 1);
128 parport_setscl(NULL, 1);
129 /* Other init if needed (power on...) */
130 if (adapter_parm[type].init.val)
131 line_set(1, &adapter_parm[type].init);
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;
140 static int __devexit i2c_parport_remove(struct platform_device *pdev)
142 i2c_del_adapter(&parport_adapter);
144 /* Un-init if needed (power off...) */
145 if (adapter_parm[type].init.val)
146 line_set(0, &adapter_parm[type].init);
148 return 0;
151 static struct platform_driver i2c_parport_driver = {
152 .driver = {
153 .owner = THIS_MODULE,
154 .name = DRVNAME,
156 .probe = i2c_parport_probe,
157 .remove = __devexit_p(i2c_parport_remove),
160 static int __init i2c_parport_device_add(u16 address)
162 int err;
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;
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;
178 return 0;
180 exit_device_put:
181 platform_device_put(pdev);
182 exit:
183 return err;
186 static int __init i2c_parport_init(void)
188 int err;
190 if (type < 0) {
191 printk(KERN_ERR DRVNAME ": adapter type unspecified\n");
192 return -ENODEV;
195 if (type >= ARRAY_SIZE(adapter_parm)) {
196 printk(KERN_ERR DRVNAME ": invalid type (%d)\n", type);
197 return -ENODEV;
200 if (base == 0) {
201 pr_info(DRVNAME ": using default base 0x%x\n", DEFAULT_BASE);
202 base = DEFAULT_BASE;
205 if (!request_region(base, 3, DRVNAME))
206 return -EBUSY;
208 if (!adapter_parm[type].getscl.val)
209 parport_algo_data.getscl = NULL;
211 /* Sets global pdev as a side effect */
212 err = i2c_parport_device_add(base);
213 if (err)
214 goto exit_release;
216 err = platform_driver_register(&i2c_parport_driver);
217 if (err)
218 goto exit_device;
220 return 0;
222 exit_device:
223 platform_device_unregister(pdev);
224 exit_release:
225 release_region(base, 3);
226 return err;
229 static void __exit i2c_parport_exit(void)
231 platform_driver_unregister(&i2c_parport_driver);
232 platform_device_unregister(pdev);
233 release_region(base, 3);
236 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
237 MODULE_DESCRIPTION("I2C bus over parallel port (light)");
238 MODULE_LICENSE("GPL");
240 module_init(i2c_parport_init);
241 module_exit(i2c_parport_exit);