Automatic merge with /usr/src/ntfs-2.6.git.
[linux-2.6] / drivers / i2c / busses / i2c-elektor.c
1 /* ------------------------------------------------------------------------- */
2 /* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes             */
3 /* ------------------------------------------------------------------------- */
4 /*   Copyright (C) 1995-97 Simon G. Vogl
5                    1998-99 Hans Berglund
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
20 /* ------------------------------------------------------------------------- */
21
22 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
23    Frodo Looijaard <frodol@dds.nl> */
24
25 /* Partialy rewriten by Oleg I. Vdovikin for mmapped support of 
26    for Alpha Processor Inc. UP-2000(+) boards */
27
28 #include <linux/kernel.h>
29 #include <linux/ioport.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/wait.h>
37
38 #include <linux/i2c.h>
39 #include <linux/i2c-algo-pcf.h>
40
41 #include <asm/io.h>
42 #include <asm/irq.h>
43
44 #include "../algos/i2c-algo-pcf.h"
45
46 #define DEFAULT_BASE 0x330
47
48 static int base;
49 static int irq;
50 static int clock  = 0x1c;
51 static int own    = 0x55;
52 static int mmapped;
53
54 /* vdovikin: removed static struct i2c_pcf_isa gpi; code - 
55   this module in real supports only one device, due to missing arguments
56   in some functions, called from the algo-pcf module. Sometimes it's
57   need to be rewriten - but for now just remove this for simpler reading */
58
59 static wait_queue_head_t pcf_wait;
60 static int pcf_pending;
61 static spinlock_t lock;
62
63 /* ----- local functions ---------------------------------------------- */
64
65 static void pcf_isa_setbyte(void *data, int ctl, int val)
66 {
67         int address = ctl ? (base + 1) : base;
68
69         /* enable irq if any specified for serial operation */
70         if (ctl && irq && (val & I2C_PCF_ESO)) {
71                 val |= I2C_PCF_ENI;
72         }
73
74         pr_debug("i2c-elektor: Write 0x%X 0x%02X\n", address, val & 255);
75
76         switch (mmapped) {
77         case 0: /* regular I/O */
78                 outb(val, address);
79                 break;
80         case 2: /* double mapped I/O needed for UP2000 board,
81                    I don't know why this... */
82                 writeb(val, (void *)address);
83                 /* fall */
84         case 1: /* memory mapped I/O */
85                 writeb(val, (void *)address);
86                 break;
87         }
88 }
89
90 static int pcf_isa_getbyte(void *data, int ctl)
91 {
92         int address = ctl ? (base + 1) : base;
93         int val = mmapped ? readb((void *)address) : inb(address);
94
95         pr_debug("i2c-elektor: Read 0x%X 0x%02X\n", address, val);
96
97         return (val);
98 }
99
100 static int pcf_isa_getown(void *data)
101 {
102         return (own);
103 }
104
105
106 static int pcf_isa_getclock(void *data)
107 {
108         return (clock);
109 }
110
111 static void pcf_isa_waitforpin(void) {
112         DEFINE_WAIT(wait);
113         int timeout = 2;
114         unsigned long flags;
115
116         if (irq > 0) {
117                 spin_lock_irqsave(&lock, flags);
118                 if (pcf_pending == 0) {
119                         spin_unlock_irqrestore(&lock, flags);
120                         prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);
121                         if (schedule_timeout(timeout*HZ)) {
122                                 spin_lock_irqsave(&lock, flags);
123                                 if (pcf_pending == 1) {
124                                         pcf_pending = 0;
125                                 }
126                                 spin_unlock_irqrestore(&lock, flags);
127                         }
128                         finish_wait(&pcf_wait, &wait);
129                 } else {
130                         pcf_pending = 0;
131                         spin_unlock_irqrestore(&lock, flags);
132                 }
133         } else {
134                 udelay(100);
135         }
136 }
137
138
139 static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id, struct pt_regs *regs) {
140         spin_lock(&lock);
141         pcf_pending = 1;
142         spin_unlock(&lock);
143         wake_up_interruptible(&pcf_wait);
144         return IRQ_HANDLED;
145 }
146
147
148 static int pcf_isa_init(void)
149 {
150         spin_lock_init(&lock);
151         if (!mmapped) {
152                 if (!request_region(base, 2, "i2c (isa bus adapter)")) {
153                         printk(KERN_ERR
154                                "i2c-elektor: requested I/O region (0x%X:2) "
155                                "is in use.\n", base);
156                         return -ENODEV;
157                 }
158         }
159         if (irq > 0) {
160                 if (request_irq(irq, pcf_isa_handler, 0, "PCF8584", NULL) < 0) {
161                         printk(KERN_ERR "i2c-elektor: Request irq%d failed\n", irq);
162                         irq = 0;
163                 } else
164                         enable_irq(irq);
165         }
166         return 0;
167 }
168
169 /* ------------------------------------------------------------------------
170  * Encapsulate the above functions in the correct operations structure.
171  * This is only done when more than one hardware adapter is supported.
172  */
173 static struct i2c_algo_pcf_data pcf_isa_data = {
174         .setpcf     = pcf_isa_setbyte,
175         .getpcf     = pcf_isa_getbyte,
176         .getown     = pcf_isa_getown,
177         .getclock   = pcf_isa_getclock,
178         .waitforpin = pcf_isa_waitforpin,
179         .udelay     = 10,
180         .mdelay     = 10,
181         .timeout    = 100,
182 };
183
184 static struct i2c_adapter pcf_isa_ops = {
185         .owner          = THIS_MODULE,
186         .class          = I2C_CLASS_HWMON,
187         .id             = I2C_HW_P_ELEK,
188         .algo_data      = &pcf_isa_data,
189         .name           = "PCF8584 ISA adapter",
190 };
191
192 static int __init i2c_pcfisa_init(void) 
193 {
194 #ifdef __alpha__
195         /* check to see we have memory mapped PCF8584 connected to the 
196         Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
197         if (base == 0) {
198                 struct pci_dev *cy693_dev;
199                 
200                 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ, 
201                                            PCI_DEVICE_ID_CONTAQ_82C693, NULL);
202                 if (cy693_dev) {
203                         char config;
204                         /* yeap, we've found cypress, let's check config */
205                         if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
206                                 
207                                 pr_debug("i2c-elektor: found cy82c693, config register 0x47 = 0x%02x.\n", config);
208
209                                 /* UP2000 board has this register set to 0xe1,
210                                    but the most significant bit as seems can be 
211                                    reset during the proper initialisation
212                                    sequence if guys from API decides to do that
213                                    (so, we can even enable Tsunami Pchip
214                                    window for the upper 1 Gb) */
215
216                                 /* so just check for ROMCS at 0xe0000,
217                                    ROMCS enabled for writes
218                                    and external XD Bus buffer in use. */
219                                 if ((config & 0x7f) == 0x61) {
220                                         /* seems to be UP2000 like board */
221                                         base = 0xe0000;
222                                         /* I don't know why we need to
223                                            write twice */
224                                         mmapped = 2;
225                                         /* UP2000 drives ISA with
226                                            8.25 MHz (PCI/4) clock
227                                            (this can be read from cypress) */
228                                         clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
229                                         printk(KERN_INFO "i2c-elektor: found API UP2000 like board, will probe PCF8584 later.\n");
230                                 }
231                         }
232                         pci_dev_put(cy693_dev);
233                 }
234         }
235 #endif
236
237         /* sanity checks for mmapped I/O */
238         if (mmapped && base < 0xc8000) {
239                 printk(KERN_ERR "i2c-elektor: incorrect base address (0x%0X) specified for mmapped I/O.\n", base);
240                 return -ENODEV;
241         }
242
243         printk(KERN_INFO "i2c-elektor: i2c pcf8584-isa adapter driver\n");
244
245         if (base == 0) {
246                 base = DEFAULT_BASE;
247         }
248
249         init_waitqueue_head(&pcf_wait);
250         if (pcf_isa_init())
251                 return -ENODEV;
252         if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
253                 goto fail;
254         
255         printk(KERN_ERR "i2c-elektor: found device at %#x.\n", base);
256
257         return 0;
258
259  fail:
260         if (irq > 0) {
261                 disable_irq(irq);
262                 free_irq(irq, NULL);
263         }
264
265         if (!mmapped)
266                 release_region(base , 2);
267         return -ENODEV;
268 }
269
270 static void i2c_pcfisa_exit(void)
271 {
272         i2c_pcf_del_bus(&pcf_isa_ops);
273
274         if (irq > 0) {
275                 disable_irq(irq);
276                 free_irq(irq, NULL);
277         }
278
279         if (!mmapped)
280                 release_region(base , 2);
281 }
282
283 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
284 MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
285 MODULE_LICENSE("GPL");
286
287 module_param(base, int, 0);
288 module_param(irq, int, 0);
289 module_param(clock, int, 0);
290 module_param(own, int, 0);
291 module_param(mmapped, int, 0);
292
293 module_init(i2c_pcfisa_init);
294 module_exit(i2c_pcfisa_exit);