blob: 7f38c01fb3a06ce83753f4d411cde7f907f91086 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------- */
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
Jan Engelhardt96de0e22007-10-19 23:21:04 +020022/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 Frodo Looijaard <frodol@dds.nl> */
24
Stig Telferfe3d6a92005-10-08 00:23:27 +020025/* Partialy rewriten by Oleg I. Vdovikin for mmapped support of
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 for Alpha Processor Inc. UP-2000(+) boards */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
Jean Delvare4a5d3032007-05-01 23:26:30 +020038#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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
49static int base;
Stig Telfer3634ff62005-10-08 00:21:48 +020050static u8 __iomem *base_iomem;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int irq;
53static int clock = 0x1c;
54static int own = 0x55;
55static int mmapped;
56
Stig Telferfe3d6a92005-10-08 00:23:27 +020057/* vdovikin: removed static struct i2c_pcf_isa gpi; code -
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 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
62static wait_queue_head_t pcf_wait;
63static int pcf_pending;
64static spinlock_t lock;
65
Stig Telferfe3d6a92005-10-08 00:23:27 +020066static struct i2c_adapter pcf_isa_ops;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* ----- local functions ---------------------------------------------- */
69
70static void pcf_isa_setbyte(void *data, int ctl, int val)
71{
Stig Telfer3634ff62005-10-08 00:21:48 +020072 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 /* enable irq if any specified for serial operation */
75 if (ctl && irq && (val & I2C_PCF_ESO)) {
76 val |= I2C_PCF_ENI;
77 }
78
Stig Telferfe3d6a92005-10-08 00:23:27 +020079 pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);
Stig Telfer3634ff62005-10-08 00:21:48 +020080 iowrite8(val, address);
81#ifdef __alpha__
82 /* API UP2000 needs some hardware fudging to make the write stick */
83 iowrite8(val, address);
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static int pcf_isa_getbyte(void *data, int ctl)
88{
Stig Telfer3634ff62005-10-08 00:21:48 +020089 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
90 int val = ioread8(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Stig Telferfe3d6a92005-10-08 00:23:27 +020092 pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return (val);
94}
95
96static int pcf_isa_getown(void *data)
97{
98 return (own);
99}
100
101
102static int pcf_isa_getclock(void *data)
103{
104 return (clock);
105}
106
107static 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
David Howells7d12e782006-10-05 14:55:46 +0100135static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 spin_lock(&lock);
137 pcf_pending = 1;
138 spin_unlock(&lock);
139 wake_up_interruptible(&pcf_wait);
140 return IRQ_HANDLED;
141}
142
143
144static int pcf_isa_init(void)
145{
146 spin_lock_init(&lock);
147 if (!mmapped) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200148 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -ENODEV;
152 }
Stig Telfer3634ff62005-10-08 00:21:48 +0200153 base_iomem = ioport_map(base, 2);
154 if (!base_iomem) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200155 printk(KERN_ERR "%s: remap of I/O region %#x failed\n",
156 pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200157 release_region(base, 2);
158 return -ENODEV;
159 }
160 } else {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200161 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);
Stig Telfer3634ff62005-10-08 00:21:48 +0200164 return -ENODEV;
165 }
166 base_iomem = ioremap(base, 2);
167 if (base_iomem == NULL) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200168 printk(KERN_ERR "%s: remap of memory region %#x "
169 "failed\n", pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200170 release_mem_region(base, 2);
171 return -ENODEV;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Stig Telferfe3d6a92005-10-08 00:23:27 +0200174 pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,
Stig Telfer3634ff62005-10-08 00:21:48 +0200175 base_iomem);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (irq > 0) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200178 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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 */
193static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201static struct i2c_adapter pcf_isa_ops = {
202 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200203 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 .id = I2C_HW_P_ELEK,
205 .algo_data = &pcf_isa_data,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200206 .name = "i2c-elektor",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Jean Delvare4a5d3032007-05-01 23:26:30 +0200209static int __devinit elektor_match(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211#ifdef __alpha__
Stig Telferfe3d6a92005-10-08 00:23:27 +0200212 /* check to see we have memory mapped PCF8584 connected to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
214 if (base == 0) {
215 struct pci_dev *cy693_dev;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200216
217 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 PCI_DEVICE_ID_CONTAQ_82C693, NULL);
219 if (cy693_dev) {
Stig Telfer3634ff62005-10-08 00:21:48 +0200220 unsigned char config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* yeap, we've found cypress, let's check config */
222 if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200223
Jean Delvare4a5d3032007-05-01 23:26:30 +0200224 dev_dbg(dev, "found cy82c693, config "
225 "register 0x47 = 0x%02x\n", config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* UP2000 board has this register set to 0xe1,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200228 but the most significant bit as seems can be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 reset during the proper initialisation
Stig Telferfe3d6a92005-10-08 00:23:27 +0200230 sequence if guys from API decides to do that
231 (so, we can even enable Tsunami Pchip
232 window for the upper 1 Gb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /* so just check for ROMCS at 0xe0000,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200235 ROMCS enabled for writes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 and external XD Bus buffer in use. */
237 if ((config & 0x7f) == 0x61) {
238 /* seems to be UP2000 like board */
239 base = 0xe0000;
Stig Telfer3634ff62005-10-08 00:21:48 +0200240 mmapped = 1;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200241 /* UP2000 drives ISA with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 8.25 MHz (PCI/4) clock
243 (this can be read from cypress) */
244 clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
Jean Delvare4a5d3032007-05-01 23:26:30 +0200245 dev_info(dev, "found API UP2000 like "
246 "board, will probe PCF8584 "
247 "later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250 pci_dev_put(cy693_dev);
251 }
252 }
253#endif
254
255 /* sanity checks for mmapped I/O */
256 if (mmapped && base < 0xc8000) {
Jean Delvare4a5d3032007-05-01 23:26:30 +0200257 dev_err(dev, "incorrect base address (%#x) specified "
258 "for mmapped I/O\n", base);
259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (base == 0) {
263 base = DEFAULT_BASE;
264 }
Jean Delvare4a5d3032007-05-01 23:26:30 +0200265 return 1;
266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jean Delvare4a5d3032007-05-01 23:26:30 +0200268static int __devinit elektor_probe(struct device *dev, unsigned int id)
269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 init_waitqueue_head(&pcf_wait);
271 if (pcf_isa_init())
272 return -ENODEV;
Jean Delvare4a5d3032007-05-01 23:26:30 +0200273 pcf_isa_ops.dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
275 goto fail;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200276
Jean Delvare4a5d3032007-05-01 23:26:30 +0200277 dev_info(dev, "found device at %#x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return 0;
280
281 fail:
282 if (irq > 0) {
283 disable_irq(irq);
284 free_irq(irq, NULL);
285 }
286
Stig Telfer3634ff62005-10-08 00:21:48 +0200287 if (!mmapped) {
288 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200289 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200290 } else {
291 iounmap(base_iomem);
292 release_mem_region(base, 2);
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -ENODEV;
295}
296
Jean Delvare4a5d3032007-05-01 23:26:30 +0200297static int __devexit elektor_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Jean Delvare32697112006-12-10 21:21:33 +0100299 i2c_del_adapter(&pcf_isa_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (irq > 0) {
302 disable_irq(irq);
303 free_irq(irq, NULL);
304 }
305
Stig Telfer3634ff62005-10-08 00:21:48 +0200306 if (!mmapped) {
307 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200308 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200309 } else {
310 iounmap(base_iomem);
311 release_mem_region(base, 2);
312 }
Jean Delvare4a5d3032007-05-01 23:26:30 +0200313
314 return 0;
315}
316
317static struct isa_driver i2c_elektor_driver = {
318 .match = elektor_match,
319 .probe = elektor_probe,
320 .remove = __devexit_p(elektor_remove),
321 .driver = {
322 .owner = THIS_MODULE,
323 .name = "i2c-elektor",
324 },
325};
326
327static int __init i2c_pcfisa_init(void)
328{
329 return isa_register_driver(&i2c_elektor_driver, 1);
330}
331
332static void __exit i2c_pcfisa_exit(void)
333{
334 isa_unregister_driver(&i2c_elektor_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
338MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
339MODULE_LICENSE("GPL");
340
341module_param(base, int, 0);
342module_param(irq, int, 0);
343module_param(clock, int, 0);
344module_param(own, int, 0);
345module_param(mmapped, int, 0);
346
347module_init(i2c_pcfisa_init);
348module_exit(i2c_pcfisa_exit);