pre-2.3.4..
[davej-history.git] / drivers / char / i2c-parport.c
blobcafe38f378d06c055802d91e4ac6df9ab7c66f04
1 /*
2 * I2C driver for parallel port
4 * Author: Phil Blundell <philb@gnu.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * This driver implements a simple I2C protocol by bit-twiddling some
12 * signals on the parallel port. Since the outputs on the parallel port
13 * aren't open collector, three lines rather than two are used:
15 * D0 clock out
16 * D1 data out
17 * BUSY data in
20 #include <linux/parport.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <asm/spinlock.h>
27 #define I2C_DELAY 10
29 static int debug = 0;
31 struct parport_i2c_bus
33 struct i2c_bus i2c;
34 struct parport_i2c_bus *next;
37 static struct parport_i2c_bus *bus_list;
39 #ifdef __SMP__
40 static spinlock_t bus_list_lock = SPIN_LOCK_UNLOCKED;
41 #endif
43 /* software I2C functions */
45 static void i2c_setlines(struct i2c_bus *bus, int clk, int data)
47 struct parport *p = bus->data;
48 parport_write_data(p, (clk?1:0) | (data?2:0));
49 udelay(I2C_DELAY);
52 static int i2c_getdataline(struct i2c_bus *bus)
54 struct parport *p = bus->data;
55 return (parport_read_status(p) & PARPORT_STATUS_BUSY) ? 0 : 1;
58 static struct i2c_bus parport_i2c_bus_template =
60 "...",
61 I2C_BUSID_PARPORT,
62 NULL,
64 SPIN_LOCK_UNLOCKED,
66 NULL,
67 NULL,
69 i2c_setlines,
70 i2c_getdataline,
71 NULL,
72 NULL,
75 static void i2c_parport_attach(struct parport *port)
77 struct parport_i2c_bus *b = kmalloc(sizeof(struct parport_i2c_bus),
78 GFP_KERNEL);
79 b->i2c = parport_i2c_bus_template;
80 b->i2c.data = port;
81 strncpy(b->i2c.name, port->name, 32);
82 spin_lock(&bus_list_lock);
83 b->next = bus_list;
84 bus_list = b;
85 spin_unlock(&bus_list_lock);
86 i2c_register_bus(&b->i2c);
87 if (debug)
88 printk(KERN_DEBUG "i2c: attached to %s\n", port->name);
91 static void i2c_parport_detach(struct parport *port)
93 struct parport_i2c_bus *b, *old_b = NULL;
94 spin_lock(&bus_list_lock);
95 b = bus_list;
96 while (b)
98 if (b->i2c.data == port)
100 if (old_b)
101 old_b->next = b->next;
102 else
103 bus_list = b->next;
104 i2c_unregister_bus(&b->i2c);
105 kfree(b);
106 break;
108 old_b = b;
109 b = b->next;
111 spin_unlock(&bus_list_lock);
112 if (debug)
113 printk(KERN_DEBUG "i2c: detached from %s\n", port->name);
116 static struct parport_driver parport_i2c_driver =
118 "i2c",
119 i2c_parport_attach,
120 i2c_parport_detach
123 #ifdef MODULE
124 int init_module(void)
125 #else
126 int __init i2c_parport_init(void)
127 #endif
129 printk("I2C: driver for parallel port v0.1 philb@gnu.org\n");
130 parport_register_driver(&parport_i2c_driver);
131 return 0;
134 #ifdef MODULE
135 MODULE_PARM(debug, "i");
137 void cleanup_module(void)
139 struct parport_i2c_bus *b = bus_list;
140 while (b)
142 struct parport_i2c_bus *next = b->next;
143 i2c_unregister_bus(&b->i2c);
144 kfree(b);
145 b = next;
147 parport_unregister_driver(&parport_i2c_driver);
149 #endif