Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-drm-fsl-dcu.git] / 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/isa.h>
39 #include <linux/i2c.h>
40 #include <linux/i2c-algo-pcf.h>
41
42 #include <asm/io.h>
43 #include <asm/irq.h>
44
45 #include "../algos/i2c-algo-pcf.h"
46
47 #define DEFAULT_BASE 0x330
48
49 static int base;
50 static u8 __iomem *base_iomem;
51
52 static int irq;
53 static int clock  = 0x1c;
54 static int own    = 0x55;
55 static int mmapped;
56
57 /* vdovikin: removed static struct i2c_pcf_isa gpi; code -
58   this module in real supports only one device, due to missing arguments
59   in some functions, called from the algo-pcf module. Sometimes it's
60   need to be rewriten - but for now just remove this for simpler reading */
61
62 static wait_queue_head_t pcf_wait;
63 static int pcf_pending;
64 static spinlock_t lock;
65
66 static struct i2c_adapter pcf_isa_ops;
67
68 /* ----- local functions ---------------------------------------------- */
69
70 static void pcf_isa_setbyte(void *data, int ctl, int val)
71 {
72         u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
73
74         /* enable irq if any specified for serial operation */
75         if (ctl && irq && (val & I2C_PCF_ESO)) {
76                 val |= I2C_PCF_ENI;
77         }
78
79         pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);
80         iowrite8(val, address);
81 #ifdef __alpha__
82         /* API UP2000 needs some hardware fudging to make the write stick */
83         iowrite8(val, address);
84 #endif
85 }
86
87 static int pcf_isa_getbyte(void *data, int ctl)
88 {
89         u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
90         int val = ioread8(address);
91
92         pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);
93         return (val);
94 }
95
96 static int pcf_isa_getown(void *data)
97 {
98         return (own);
99 }
100
101
102 static int pcf_isa_getclock(void *data)
103 {
104         return (clock);
105 }
106
107 static void pcf_isa_waitforpin(void) {
108         DEFINE_WAIT(wait);
109         int timeout = 2;
110         unsigned long flags;
111
112         if (irq > 0) {
113                 spin_lock_irqsave(&lock, flags);
114                 if (pcf_pending == 0) {
115                         spin_unlock_irqrestore(&lock, flags);
116                         prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);
117                         if (schedule_timeout(timeout*HZ)) {
118                                 spin_lock_irqsave(&lock, flags);
119                                 if (pcf_pending == 1) {
120                                         pcf_pending = 0;
121                                 }
122                                 spin_unlock_irqrestore(&lock, flags);
123                         }
124                         finish_wait(&pcf_wait, &wait);
125                 } else {
126                         pcf_pending = 0;
127                         spin_unlock_irqrestore(&lock, flags);
128                 }
129         } else {
130                 udelay(100);
131         }
132 }
133
134
135 static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id) {
136         spin_lock(&lock);
137         pcf_pending = 1;
138         spin_unlock(&lock);
139         wake_up_interruptible(&pcf_wait);
140         return IRQ_HANDLED;
141 }
142
143
144 static int pcf_isa_init(void)
145 {
146         spin_lock_init(&lock);
147         if (!mmapped) {
148                 if (!request_region(base, 2, pcf_isa_ops.name)) {
149                         printk(KERN_ERR "%s: requested I/O region (%#x:2) is "
150                                "in use\n", pcf_isa_ops.name, base);
151                         return -ENODEV;
152                 }
153                 base_iomem = ioport_map(base, 2);
154                 if (!base_iomem) {
155                         printk(KERN_ERR "%s: remap of I/O region %#x failed\n",
156                                pcf_isa_ops.name, base);
157                         release_region(base, 2);
158                         return -ENODEV;
159                 }
160         } else {
161                 if (!request_mem_region(base, 2, pcf_isa_ops.name)) {
162                         printk(KERN_ERR "%s: requested memory region (%#x:2) "
163                                "is in use\n", pcf_isa_ops.name, base);
164                         return -ENODEV;
165                 }
166                 base_iomem = ioremap(base, 2);
167                 if (base_iomem == NULL) {
168                         printk(KERN_ERR "%s: remap of memory region %#x "
169                                "failed\n", pcf_isa_ops.name, base);
170                         release_mem_region(base, 2);
171                         return -ENODEV;
172                 }
173         }
174         pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,
175                  base_iomem);
176
177         if (irq > 0) {
178                 if (request_irq(irq, pcf_isa_handler, 0, pcf_isa_ops.name,
179                                 NULL) < 0) {
180                         printk(KERN_ERR "%s: Request irq%d failed\n",
181                                pcf_isa_ops.name, irq);
182                         irq = 0;
183                 } else
184                         enable_irq(irq);
185         }
186         return 0;
187 }
188
189 /* ------------------------------------------------------------------------
190  * Encapsulate the above functions in the correct operations structure.
191  * This is only done when more than one hardware adapter is supported.
192  */
193 static struct i2c_algo_pcf_data pcf_isa_data = {
194         .setpcf     = pcf_isa_setbyte,
195         .getpcf     = pcf_isa_getbyte,
196         .getown     = pcf_isa_getown,
197         .getclock   = pcf_isa_getclock,
198         .waitforpin = pcf_isa_waitforpin,
199         .udelay     = 10,
200         .timeout    = 100,
201 };
202
203 static struct i2c_adapter pcf_isa_ops = {
204         .owner          = THIS_MODULE,
205         .class          = I2C_CLASS_HWMON,
206         .id             = I2C_HW_P_ELEK,
207         .algo_data      = &pcf_isa_data,
208         .name           = "i2c-elektor",
209 };
210
211 static int __devinit elektor_match(struct device *dev, unsigned int id)
212 {
213 #ifdef __alpha__
214         /* check to see we have memory mapped PCF8584 connected to the
215         Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
216         if (base == 0) {
217                 struct pci_dev *cy693_dev;
218
219                 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
220                                            PCI_DEVICE_ID_CONTAQ_82C693, NULL);
221                 if (cy693_dev) {
222                         unsigned char config;
223                         /* yeap, we've found cypress, let's check config */
224                         if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
225
226                                 dev_dbg(dev, "found cy82c693, config "
227                                         "register 0x47 = 0x%02x\n", config);
228
229                                 /* UP2000 board has this register set to 0xe1,
230                                    but the most significant bit as seems can be
231                                    reset during the proper initialisation
232                                    sequence if guys from API decides to do that
233                                    (so, we can even enable Tsunami Pchip
234                                    window for the upper 1 Gb) */
235
236                                 /* so just check for ROMCS at 0xe0000,
237                                    ROMCS enabled for writes
238                                    and external XD Bus buffer in use. */
239                                 if ((config & 0x7f) == 0x61) {
240                                         /* seems to be UP2000 like board */
241                                         base = 0xe0000;
242                                         mmapped = 1;
243                                         /* UP2000 drives ISA with
244                                            8.25 MHz (PCI/4) clock
245                                            (this can be read from cypress) */
246                                         clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
247                                         dev_info(dev, "found API UP2000 like "
248                                                  "board, will probe PCF8584 "
249                                                  "later\n");
250                                 }
251                         }
252                         pci_dev_put(cy693_dev);
253                 }
254         }
255 #endif
256
257         /* sanity checks for mmapped I/O */
258         if (mmapped && base < 0xc8000) {
259                 dev_err(dev, "incorrect base address (%#x) specified "
260                        "for mmapped I/O\n", base);
261                 return 0;
262         }
263
264         if (base == 0) {
265                 base = DEFAULT_BASE;
266         }
267         return 1;
268 }
269
270 static int __devinit elektor_probe(struct device *dev, unsigned int id)
271 {
272         init_waitqueue_head(&pcf_wait);
273         if (pcf_isa_init())
274                 return -ENODEV;
275         pcf_isa_ops.dev.parent = dev;
276         if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
277                 goto fail;
278
279         dev_info(dev, "found device at %#x\n", base);
280
281         return 0;
282
283  fail:
284         if (irq > 0) {
285                 disable_irq(irq);
286                 free_irq(irq, NULL);
287         }
288
289         if (!mmapped) {
290                 ioport_unmap(base_iomem);
291                 release_region(base, 2);
292         } else {
293                 iounmap(base_iomem);
294                 release_mem_region(base, 2);
295         }
296         return -ENODEV;
297 }
298
299 static int __devexit elektor_remove(struct device *dev, unsigned int id)
300 {
301         i2c_del_adapter(&pcf_isa_ops);
302
303         if (irq > 0) {
304                 disable_irq(irq);
305                 free_irq(irq, NULL);
306         }
307
308         if (!mmapped) {
309                 ioport_unmap(base_iomem);
310                 release_region(base, 2);
311         } else {
312                 iounmap(base_iomem);
313                 release_mem_region(base, 2);
314         }
315
316         return 0;
317 }
318
319 static struct isa_driver i2c_elektor_driver = {
320         .match          = elektor_match,
321         .probe          = elektor_probe,
322         .remove         = __devexit_p(elektor_remove),
323         .driver = {
324                 .owner  = THIS_MODULE,
325                 .name   = "i2c-elektor",
326         },
327 };
328
329 static int __init i2c_pcfisa_init(void)
330 {
331         return isa_register_driver(&i2c_elektor_driver, 1);
332 }
333
334 static void __exit i2c_pcfisa_exit(void)
335 {
336         isa_unregister_driver(&i2c_elektor_driver);
337 }
338
339 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
340 MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
341 MODULE_LICENSE("GPL");
342
343 module_param(base, int, 0);
344 module_param(irq, int, 0);
345 module_param(clock, int, 0);
346 module_param(own, int, 0);
347 module_param(mmapped, int, 0);
348
349 module_init(i2c_pcfisa_init);
350 module_exit(i2c_pcfisa_exit);