1 /* ------------------------------------------------------------------------- */
2 /* i2c-philips-par.c i2c-hw access for philips style parallel port adapters */
3 /* ------------------------------------------------------------------------- */
4 /* Copyright (C) 1995-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 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
22 Frodo Looijaard <frodol@dds.nl> */
24 /* $Id: i2c-philips-par.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $ */
26 #include <linux/kernel.h>
27 #include <linux/ioport.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/stddef.h>
31 #include <linux/parport.h>
33 #include <linux/i2c.h>
34 #include <linux/i2c-algo-bit.h>
44 struct pardevice *pdev;
45 struct i2c_adapter adapter;
46 struct i2c_algo_bit_data bit_lp_data;
50 static struct i2c_par *adapter_list;
53 /* ----- global defines ----------------------------------------------- */
54 #define DEB(x) /* should be reasonable open, close &c. */
55 #define DEB2(x) /* low level debugging - very slow */
56 #define DEBE(x) x /* error messages */
58 /* ----- printer port defines ------------------------------------------*/
59 /* Pin Port Inverted name */
60 #define I2C_ON 0x20 /* 12 status N paper */
61 /* ... only for phil. not used */
62 #define I2C_SDA 0x80 /* 9 data N data7 */
63 #define I2C_SCL 0x08 /* 17 ctrl N dsel */
65 #define I2C_SDAIN 0x80 /* 11 stat Y busy */
66 #define I2C_SCLIN 0x08 /* 15 stat Y enable */
68 #define I2C_DMASK 0x7f
69 #define I2C_CMASK 0xf7
71 /* ----- local functions ---------------------------------------------- */
73 static void bit_lp_setscl(void *data, int state)
75 /*be cautious about state of the control register -
76 touch only the one bit needed*/
78 parport_write_control((struct parport *) data,
79 parport_read_control((struct parport *) data)|I2C_SCL);
81 parport_write_control((struct parport *) data,
82 parport_read_control((struct parport *) data)&I2C_CMASK);
86 static void bit_lp_setsda(void *data, int state)
89 parport_write_data((struct parport *) data, I2C_DMASK);
91 parport_write_data((struct parport *) data, I2C_SDA);
95 static int bit_lp_getscl(void *data)
97 return parport_read_status((struct parport *) data) & I2C_SCLIN;
100 static int bit_lp_getsda(void *data)
102 return parport_read_status((struct parport *) data) & I2C_SDAIN;
105 static void bit_lp_setscl2(void *data, int state)
108 parport_write_data((struct parport *) data,
109 parport_read_data((struct parport *) data)|0x1);
111 parport_write_data((struct parport *) data,
112 parport_read_data((struct parport *) data)&0xfe);
116 static void bit_lp_setsda2(void *data, int state)
119 parport_write_data((struct parport *) data,
120 parport_read_data((struct parport *) data)|0x2);
122 parport_write_data((struct parport *) data,
123 parport_read_data((struct parport *) data)&0xfd);
127 static int bit_lp_getsda2(void *data)
129 return (parport_read_status((struct parport *) data) &
130 PARPORT_STATUS_BUSY) ? 0 : 1;
133 static int bit_lp_reg(struct i2c_client *client)
138 static int bit_lp_unreg(struct i2c_client *client)
143 static void bit_lp_inc_use(struct i2c_adapter *adap)
148 static void bit_lp_dec_use(struct i2c_adapter *adap)
153 /* ------------------------------------------------------------------------
154 * Encapsulate the above functions in the correct operations structure.
155 * This is only done when more than one hardware adapter is supported.
158 static struct i2c_algo_bit_data bit_lp_data = {
164 80, 80, 100, /* waits, timeout */
167 static struct i2c_algo_bit_data bit_lp_data2 = {
173 80, 80, 100, /* waits, timeout */
176 static struct i2c_adapter bit_lp_ops = {
177 "Philips Parallel port adapter",
188 static void i2c_parport_attach (struct parport *port)
190 struct i2c_par *adapter = kmalloc(sizeof(struct i2c_par),
193 printk("i2c-philips-par: Unable to malloc.\n");
197 printk("i2c-philips-par.o: attaching to %s\n", port->name);
199 adapter->pdev = parport_register_device(port, "i2c-philips-par",
203 if (!adapter->pdev) {
204 printk("i2c-philips-par: Unable to register with parport.\n");
208 adapter->adapter = bit_lp_ops;
209 adapter->adapter.algo_data = &adapter->bit_lp_data;
210 adapter->bit_lp_data = type ? bit_lp_data2 : bit_lp_data;
211 adapter->bit_lp_data.data = port;
213 /* reset hardware to sane state */
214 parport_claim_or_block(adapter->pdev);
215 bit_lp_setsda(port, 1);
216 bit_lp_setscl(port, 1);
217 parport_release(adapter->pdev);
219 if (i2c_bit_add_bus(&adapter->adapter) < 0)
221 printk("i2c-philips-par: Unable to register with I2C.\n");
222 parport_unregister_device(adapter->pdev);
224 return; /* No good */
227 adapter->next = adapter_list;
228 adapter_list = adapter;
231 static void i2c_parport_detach (struct parport *port)
233 struct i2c_par *adapter, *prev = NULL;
235 for (adapter = adapter_list; adapter; adapter = adapter->next)
237 if (adapter->pdev->port == port)
239 parport_unregister_device(adapter->pdev);
240 i2c_bit_del_bus(&adapter->adapter);
242 prev->next = adapter->next;
244 adapter_list = adapter->next;
253 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
254 static struct parport_driver i2c_driver = {
262 int __init i2c_bitlp_init(void)
264 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,4)
265 struct parport *port;
267 printk("i2c-philips-par.o: i2c Philips parallel port adapter module\n");
269 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
270 parport_register_driver(&i2c_driver);
272 for (port = parport_enumerate(); port; port=port->next)
273 i2c_parport_attach(port);
279 void __exit i2c_bitlp_exit(void)
281 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,4)
282 parport_unregister_driver(&i2c_driver);
284 struct parport *port;
285 for (port = parport_enumerate(); port; port=port->next)
286 i2c_parport_detach(port);
292 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
293 MODULE_DESCRIPTION("I2C-Bus adapter routines for Philips parallel port adapter");
294 MODULE_LICENSE("GPL");
296 MODULE_PARM(type, "i");
299 int init_module(void)
301 return i2c_bitlp_init();
304 void cleanup_module(void)