Import 2.3.18pre1
[davej-history.git] / drivers / char / i2c-parport.c
bloba8f83ce8e06b99378e8731357ab2b0c56d9423ea
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 <linux/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 static spinlock_t bus_list_lock = SPIN_LOCK_UNLOCKED;
41 /* software I2C functions */
43 static void i2c_setlines(struct i2c_bus *bus, int clk, int data)
45 struct parport *p = bus->data;
46 parport_write_data(p, (clk?1:0) | (data?2:0));
47 udelay(I2C_DELAY);
50 static int i2c_getdataline(struct i2c_bus *bus)
52 struct parport *p = bus->data;
53 return (parport_read_status(p) & PARPORT_STATUS_BUSY) ? 0 : 1;
56 static struct i2c_bus parport_i2c_bus_template =
58 "...",
59 I2C_BUSID_PARPORT,
60 NULL,
62 SPIN_LOCK_UNLOCKED,
64 NULL,
65 NULL,
67 i2c_setlines,
68 i2c_getdataline,
69 NULL,
70 NULL,
73 static void i2c_parport_attach(struct parport *port)
75 struct parport_i2c_bus *b = kmalloc(sizeof(struct parport_i2c_bus),
76 GFP_KERNEL);
77 b->i2c = parport_i2c_bus_template;
78 b->i2c.data = port;
79 strncpy(b->i2c.name, port->name, 32);
80 spin_lock(&bus_list_lock);
81 b->next = bus_list;
82 bus_list = b;
83 spin_unlock(&bus_list_lock);
84 i2c_register_bus(&b->i2c);
85 if (debug)
86 printk(KERN_DEBUG "i2c: attached to %s\n", port->name);
89 static void i2c_parport_detach(struct parport *port)
91 struct parport_i2c_bus *b, *old_b = NULL;
92 spin_lock(&bus_list_lock);
93 b = bus_list;
94 while (b)
96 if (b->i2c.data == port)
98 if (old_b)
99 old_b->next = b->next;
100 else
101 bus_list = b->next;
102 i2c_unregister_bus(&b->i2c);
103 kfree(b);
104 break;
106 old_b = b;
107 b = b->next;
109 spin_unlock(&bus_list_lock);
110 if (debug)
111 printk(KERN_DEBUG "i2c: detached from %s\n", port->name);
114 static struct parport_driver parport_i2c_driver =
116 "i2c",
117 i2c_parport_attach,
118 i2c_parport_detach
121 #ifdef MODULE
122 int init_module(void)
123 #else
124 int __init i2c_parport_init(void)
125 #endif
127 printk("I2C: driver for parallel port v0.1 philb@gnu.org\n");
128 parport_register_driver(&parport_i2c_driver);
129 return 0;
132 #ifdef MODULE
133 MODULE_PARM(debug, "i");
135 void cleanup_module(void)
137 struct parport_i2c_bus *b = bus_list;
138 while (b)
140 struct parport_i2c_bus *next = b->next;
141 i2c_unregister_bus(&b->i2c);
142 kfree(b);
143 b = next;
145 parport_unregister_driver(&parport_i2c_driver);
147 #endif