Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 giacomo 1
/* ------------------------------------------------------------------------- */
2
/* i2c-philips-par.c i2c-hw access for philips style parallel port adapters  */
3
/* ------------------------------------------------------------------------- */
4
/*   Copyright (C) 1995-2000 Simon G. Vogl
5
 
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.
10
 
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.
15
 
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
/* ------------------------------------------------------------------------- */
20
 
21
/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
22
   Frodo Looijaard <frodol@dds.nl> */
23
 
24
/* $Id: i2c-philips-par.c,v 1.1 2004-01-28 15:12:06 giacomo Exp $ */
25
 
26
#include <linux/kernel.h>
27
#include <linux/module.h>
28
#include <linux/init.h>
29
#include <linux/parport.h>
30
#include <linux/i2c.h>
31
#include <linux/i2c-algo-bit.h>
32
 
33
static int type;
34
 
35
struct i2c_par
36
{
37
        struct pardevice *pdev;
38
        struct i2c_adapter adapter;
39
        struct i2c_algo_bit_data bit_lp_data;
40
        struct i2c_par *next;
41
};
42
 
43
static struct i2c_par *adapter_list;
44
 
45
 
46
/* ----- printer port defines ------------------------------------------*/
47
                                        /* Pin Port  Inverted   name    */
48
#define I2C_ON          0x20            /* 12 status N  paper           */
49
                                        /* ... only for phil. not used  */
50
#define I2C_SDA         0x80            /*  9 data   N  data7           */
51
#define I2C_SCL         0x08            /* 17 ctrl   N  dsel            */
52
 
53
#define I2C_SDAIN       0x80            /* 11 stat   Y  busy            */
54
#define I2C_SCLIN       0x08            /* 15 stat   Y  enable          */
55
 
56
#define I2C_DMASK       0x7f
57
#define I2C_CMASK       0xf7
58
 
59
/* ----- local functions ---------------------------------------------- */
60
 
61
static void bit_lp_setscl(void *data, int state)
62
{
63
        /*be cautious about state of the control register -
64
                touch only the one bit needed*/
65
        if (state) {
66
                parport_write_control((struct parport *) data,
67
                      parport_read_control((struct parport *) data)|I2C_SCL);
68
        } else {
69
                parport_write_control((struct parport *) data,
70
                      parport_read_control((struct parport *) data)&I2C_CMASK);
71
        }
72
}
73
 
74
static void bit_lp_setsda(void *data, int state)
75
{
76
        if (state) {
77
                parport_write_data((struct parport *) data, I2C_DMASK);
78
        } else {
79
                parport_write_data((struct parport *) data, I2C_SDA);
80
        }
81
}
82
 
83
static int bit_lp_getscl(void *data)
84
{
85
        return parport_read_status((struct parport *) data) & I2C_SCLIN;
86
}
87
 
88
static int bit_lp_getsda(void *data)
89
{
90
        return parport_read_status((struct parport *) data) & I2C_SDAIN;
91
}
92
 
93
static void bit_lp_setscl2(void *data, int state)
94
{
95
        if (state) {
96
                parport_write_data((struct parport *) data,
97
                      parport_read_data((struct parport *) data)|0x1);
98
        } else {
99
                parport_write_data((struct parport *) data,
100
                      parport_read_data((struct parport *) data)&0xfe);
101
        }
102
}
103
 
104
static void bit_lp_setsda2(void *data, int state)
105
{
106
        if (state) {
107
                parport_write_data((struct parport *) data,
108
                      parport_read_data((struct parport *) data)|0x2);
109
        } else {
110
                parport_write_data((struct parport *) data,
111
                      parport_read_data((struct parport *) data)&0xfd);
112
        }
113
}
114
 
115
static int bit_lp_getsda2(void *data)
116
{
117
        return (parport_read_status((struct parport *) data) &
118
                                     PARPORT_STATUS_BUSY) ? 0 : 1;
119
}
120
 
121
/* ------------------------------------------------------------------------
122
 * Encapsulate the above functions in the correct operations structure.
123
 * This is only done when more than one hardware adapter is supported.
124
 */
125
 
126
static struct i2c_algo_bit_data bit_lp_data = {
127
        .setsda         = bit_lp_setsda,
128
        .setscl         = bit_lp_setscl,
129
        .getsda         = bit_lp_getsda,
130
        .getscl         = bit_lp_getscl,
131
        .udelay         = 80,
132
        .mdelay         = 80,
133
        .timeout        = HZ
134
};
135
 
136
static struct i2c_algo_bit_data bit_lp_data2 = {
137
        .setsda         = bit_lp_setsda2,
138
        .setscl         = bit_lp_setscl2,
139
        .getsda         = bit_lp_getsda2,
140
        .udelay         = 80,
141
        .mdelay         = 80,
142
        .timeout        = HZ
143
};
144
 
145
static struct i2c_adapter bit_lp_ops = {
146
        .owner          = THIS_MODULE,
147
        .id             = I2C_HW_B_LP,
148
        .name           = "Philips Parallel port adapter",
149
};
150
 
151
static void i2c_parport_attach (struct parport *port)
152
{
153
        struct i2c_par *adapter = kmalloc(sizeof(struct i2c_par),
154
                                          GFP_KERNEL);
155
        if (!adapter) {
156
                printk(KERN_ERR "i2c-philips-par: Unable to malloc.\n");
157
                return;
158
        }
159
        memset (adapter, 0x00, sizeof(struct i2c_par));
160
 
161
        /* printk(KERN_DEBUG "i2c-philips-par.o: attaching to %s\n", port->name); */
162
 
163
        adapter->pdev = parport_register_device(port, "i2c-philips-par",
164
                                                NULL, NULL, NULL,
165
                                                PARPORT_FLAG_EXCL,
166
                                                NULL);
167
        if (!adapter->pdev) {
168
                printk(KERN_ERR "i2c-philips-par: Unable to register with parport.\n");
169
                kfree(adapter);
170
                return;
171
        }
172
 
173
        adapter->adapter = bit_lp_ops;
174
        adapter->adapter.algo_data = &adapter->bit_lp_data;
175
        adapter->bit_lp_data = type ? bit_lp_data2 : bit_lp_data;
176
        adapter->bit_lp_data.data = port;
177
 
178
        if (parport_claim_or_block(adapter->pdev) < 0 ) {
179
                printk(KERN_ERR "i2c-philips-par: Could not claim parallel port.\n");
180
                kfree(adapter);
181
                return;
182
        }
183
        /* reset hardware to sane state */
184
        bit_lp_setsda(port, 1);
185
        bit_lp_setscl(port, 1);
186
        parport_release(adapter->pdev);
187
 
188
        if (i2c_bit_add_bus(&adapter->adapter) < 0) {
189
                printk(KERN_ERR "i2c-philips-par: Unable to register with I2C.\n");
190
                parport_unregister_device(adapter->pdev);
191
                kfree(adapter);
192
                return;         /* No good */
193
        }
194
 
195
        adapter->next = adapter_list;
196
        adapter_list = adapter;
197
}
198
 
199
static void i2c_parport_detach (struct parport *port)
200
{
201
        struct i2c_par *adapter, *prev = NULL;
202
 
203
        for (adapter = adapter_list; adapter; adapter = adapter->next) {
204
                if (adapter->pdev->port == port) {
205
                        parport_unregister_device(adapter->pdev);
206
                        i2c_bit_del_bus(&adapter->adapter);
207
                        if (prev)
208
                                prev->next = adapter->next;
209
                        else
210
                                adapter_list = adapter->next;
211
                        kfree(adapter);
212
                        return;
213
                }
214
                prev = adapter;
215
        }
216
}
217
 
218
static struct parport_driver i2c_driver = {
219
        .name =         "i2c-philips-par",
220
        .attach =       i2c_parport_attach,
221
        .detach =       i2c_parport_detach,
222
};
223
 
224
int __init i2c_bitlp_init(void)
225
{
226
        printk(KERN_INFO "i2c Philips parallel port adapter driver\n");
227
 
228
        return parport_register_driver(&i2c_driver);
229
}
230
 
231
void __exit i2c_bitlp_exit(void)
232
{
233
        parport_unregister_driver(&i2c_driver);
234
}
235
 
236
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
237
MODULE_DESCRIPTION("I2C-Bus adapter routines for Philips parallel port adapter");
238
MODULE_LICENSE("GPL");
239
 
240
MODULE_PARM(type, "i");
241
 
242
module_init(i2c_bitlp_init);
243
module_exit(i2c_bitlp_exit);