blob: 496ee875eb4f2ffa67933abc23da2d2814205633 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * i2c-pca-isa.c driver for PCA9564 on ISA boards
3 * Copyright (C) 2004 Arcom Control Systems
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/ioport.h>
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/wait.h>
29
Jean Delvare5cedb052007-05-01 23:26:30 +020030#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/i2c.h>
32#include <linux/i2c-algo-pca.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36
37#include "../algos/i2c-algo-pca.h"
38
39#define IO_SIZE 4
40
41#undef DEBUG_IO
42//#define DEBUG_IO
43
44static unsigned long base = 0x330;
45static int irq = 10;
46
47/* Data sheet recommends 59kHz for 100kHz operation due to variation
48 * in the actual clock rate */
49static int clock = I2C_PCA_CON_59kHz;
50
51static int own = 0x55;
52
53static wait_queue_head_t pca_wait;
54
55static int pca_isa_getown(struct i2c_algo_pca_data *adap)
56{
57 return (own);
58}
59
60static int pca_isa_getclock(struct i2c_algo_pca_data *adap)
61{
62 return (clock);
63}
64
65static void
66pca_isa_writebyte(struct i2c_algo_pca_data *adap, int reg, int val)
67{
68#ifdef DEBUG_IO
69 static char *names[] = { "T/O", "DAT", "ADR", "CON" };
70 printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
71#endif
72 outb(val, base+reg);
73}
74
75static int
76pca_isa_readbyte(struct i2c_algo_pca_data *adap, int reg)
77{
78 int res = inb(base+reg);
79#ifdef DEBUG_IO
80 {
81 static char *names[] = { "STA", "DAT", "ADR", "CON" };
82 printk("*** read %s => %#04x\n", names[reg], res);
83 }
84#endif
85 return res;
86}
87
88static int pca_isa_waitforinterrupt(struct i2c_algo_pca_data *adap)
89{
90 int ret = 0;
91
92 if (irq > -1) {
93 ret = wait_event_interruptible(pca_wait,
94 pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI);
95 } else {
96 while ((pca_isa_readbyte(adap, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
97 udelay(100);
98 }
99 return ret;
100}
101
David Howells7d12e782006-10-05 14:55:46 +0100102static irqreturn_t pca_handler(int this_irq, void *dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 wake_up_interruptible(&pca_wait);
104 return IRQ_HANDLED;
105}
106
107static struct i2c_algo_pca_data pca_isa_data = {
108 .get_own = pca_isa_getown,
109 .get_clock = pca_isa_getclock,
110 .write_byte = pca_isa_writebyte,
111 .read_byte = pca_isa_readbyte,
112 .wait_for_interrupt = pca_isa_waitforinterrupt,
113};
114
115static struct i2c_adapter pca_isa_ops = {
116 .owner = THIS_MODULE,
117 .id = I2C_HW_A_ISA,
118 .algo_data = &pca_isa_data,
119 .name = "PCA9564 ISA Adapter",
120};
121
Jean Delvare5cedb052007-05-01 23:26:30 +0200122static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 init_waitqueue_head(&pca_wait);
125
Jean Delvare5cedb052007-05-01 23:26:30 +0200126 dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Christian Krafft104cb572008-02-24 20:03:42 +0100128#ifdef CONFIG_PPC_MERGE
129 if (check_legacy_ioport(base)) {
130 dev_err(dev, "I/O address %#08lx is not available\n", base);
131 goto out;
132 }
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200136 dev_err(dev, "I/O address %#08lx is in use\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto out;
138 }
139
140 if (irq > -1) {
141 if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200142 dev_err(dev, "Request irq%d failed\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto out_region;
144 }
145 }
146
147 if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200148 dev_err(dev, "Failed to add i2c bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out_irq;
150 }
151
152 return 0;
153
154 out_irq:
155 if (irq > -1)
156 free_irq(irq, &pca_isa_ops);
157 out_region:
158 release_region(base, IO_SIZE);
159 out:
160 return -ENODEV;
161}
162
Jean Delvare5cedb052007-05-01 23:26:30 +0200163static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Jean Delvare32697112006-12-10 21:21:33 +0100165 i2c_del_adapter(&pca_isa_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (irq > 0) {
168 disable_irq(irq);
169 free_irq(irq, &pca_isa_ops);
170 }
171 release_region(base, IO_SIZE);
Jean Delvare5cedb052007-05-01 23:26:30 +0200172
173 return 0;
174}
175
176static struct isa_driver pca_isa_driver = {
177 .probe = pca_isa_probe,
178 .remove = __devexit_p(pca_isa_remove),
179 .driver = {
180 .owner = THIS_MODULE,
181 .name = "i2c-pca-isa",
182 }
183};
184
185static int __init pca_isa_init(void)
186{
187 return isa_register_driver(&pca_isa_driver, 1);
188}
189
190static void __exit pca_isa_exit(void)
191{
192 isa_unregister_driver(&pca_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
196MODULE_DESCRIPTION("ISA base PCA9564 driver");
197MODULE_LICENSE("GPL");
198
199module_param(base, ulong, 0);
200MODULE_PARM_DESC(base, "I/O base address");
201
202module_param(irq, int, 0);
203MODULE_PARM_DESC(irq, "IRQ");
204module_param(clock, int, 0);
205MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
206
207module_param(own, int, 0); /* the driver can't do slave mode, so there's no real point in this */
208
209module_init(pca_isa_init);
210module_exit(pca_isa_exit);