blob: 28d8463b74ad16ba809494b3597b27506d8afb4b [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
Wolfram Sangc01b0832008-04-22 22:16:46 +02004 * Copyright (C) 2008 Pengutronix
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/wait.h>
Jean Delvare5cedb052007-05-01 23:26:30 +020029#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/i2c.h>
31#include <linux/i2c-algo-pca.h>
32
33#include <asm/io.h>
34#include <asm/irq.h>
35
Wolfram Sangc01b0832008-04-22 22:16:46 +020036#define DRIVER "i2c-pca-isa"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define IO_SIZE 4
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static unsigned long base = 0x330;
40static int irq = 10;
41
42/* Data sheet recommends 59kHz for 100kHz operation due to variation
43 * in the actual clock rate */
44static int clock = I2C_PCA_CON_59kHz;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static wait_queue_head_t pca_wait;
47
Wolfram Sangc01b0832008-04-22 22:16:46 +020048static void pca_isa_writebyte(void *pd, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50#ifdef DEBUG_IO
51 static char *names[] = { "T/O", "DAT", "ADR", "CON" };
52 printk("*** write %s at %#lx <= %#04x\n", names[reg], base+reg, val);
53#endif
54 outb(val, base+reg);
55}
56
Wolfram Sangc01b0832008-04-22 22:16:46 +020057static int pca_isa_readbyte(void *pd, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int res = inb(base+reg);
60#ifdef DEBUG_IO
61 {
Wolfram Sang3d438292008-04-22 22:16:46 +020062 static char *names[] = { "STA", "DAT", "ADR", "CON" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 printk("*** read %s => %#04x\n", names[reg], res);
64 }
65#endif
66 return res;
67}
68
Wolfram Sangc01b0832008-04-22 22:16:46 +020069static int pca_isa_waitforcompletion(void *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 int ret = 0;
72
73 if (irq > -1) {
74 ret = wait_event_interruptible(pca_wait,
Wolfram Sangc01b0832008-04-22 22:16:46 +020075 pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 } else {
Wolfram Sangc01b0832008-04-22 22:16:46 +020077 while ((pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 udelay(100);
79 }
80 return ret;
81}
82
Wolfram Sangc01b0832008-04-22 22:16:46 +020083static void pca_isa_resetchip(void *pd)
84{
85 /* apparently only an external reset will do it. not a lot can be done */
86 printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
87}
88
David Howells7d12e782006-10-05 14:55:46 +010089static irqreturn_t pca_handler(int this_irq, void *dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 wake_up_interruptible(&pca_wait);
91 return IRQ_HANDLED;
92}
93
94static struct i2c_algo_pca_data pca_isa_data = {
Wolfram Sangc01b0832008-04-22 22:16:46 +020095 /* .data intentionally left NULL, not needed with ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .write_byte = pca_isa_writebyte,
97 .read_byte = pca_isa_readbyte,
Wolfram Sangc01b0832008-04-22 22:16:46 +020098 .wait_for_completion = pca_isa_waitforcompletion,
99 .reset_chip = pca_isa_resetchip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102static struct i2c_adapter pca_isa_ops = {
103 .owner = THIS_MODULE,
104 .id = I2C_HW_A_ISA,
105 .algo_data = &pca_isa_data,
106 .name = "PCA9564 ISA Adapter",
Wolfram Sangc01b0832008-04-22 22:16:46 +0200107 .timeout = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Jean Delvare5cedb052007-05-01 23:26:30 +0200110static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 init_waitqueue_head(&pca_wait);
113
Jean Delvare5cedb052007-05-01 23:26:30 +0200114 dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Kumar Gala68e1ee62008-09-22 14:41:31 -0700116#ifdef CONFIG_PPC
Christian Krafft104cb572008-02-24 20:03:42 +0100117 if (check_legacy_ioport(base)) {
118 dev_err(dev, "I/O address %#08lx is not available\n", base);
119 goto out;
120 }
121#endif
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200124 dev_err(dev, "I/O address %#08lx is in use\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto out;
126 }
127
128 if (irq > -1) {
129 if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200130 dev_err(dev, "Request irq%d failed\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out_region;
132 }
133 }
134
Wolfram Sangc01b0832008-04-22 22:16:46 +0200135 pca_isa_data.i2c_clock = clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200137 dev_err(dev, "Failed to add i2c bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto out_irq;
139 }
140
141 return 0;
142
143 out_irq:
144 if (irq > -1)
145 free_irq(irq, &pca_isa_ops);
146 out_region:
147 release_region(base, IO_SIZE);
148 out:
149 return -ENODEV;
150}
151
Jean Delvare5cedb052007-05-01 23:26:30 +0200152static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Jean Delvare32697112006-12-10 21:21:33 +0100154 i2c_del_adapter(&pca_isa_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (irq > 0) {
157 disable_irq(irq);
158 free_irq(irq, &pca_isa_ops);
159 }
160 release_region(base, IO_SIZE);
Jean Delvare5cedb052007-05-01 23:26:30 +0200161
162 return 0;
163}
164
165static struct isa_driver pca_isa_driver = {
166 .probe = pca_isa_probe,
167 .remove = __devexit_p(pca_isa_remove),
168 .driver = {
169 .owner = THIS_MODULE,
Wolfram Sangc01b0832008-04-22 22:16:46 +0200170 .name = DRIVER,
Jean Delvare5cedb052007-05-01 23:26:30 +0200171 }
172};
173
174static int __init pca_isa_init(void)
175{
176 return isa_register_driver(&pca_isa_driver, 1);
177}
178
179static void __exit pca_isa_exit(void)
180{
181 isa_unregister_driver(&pca_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
185MODULE_DESCRIPTION("ISA base PCA9564 driver");
186MODULE_LICENSE("GPL");
187
188module_param(base, ulong, 0);
189MODULE_PARM_DESC(base, "I/O base address");
190
191module_param(irq, int, 0);
192MODULE_PARM_DESC(irq, "IRQ");
193module_param(clock, int, 0);
194MODULE_PARM_DESC(clock, "Clock rate as described in table 1 of PCA9564 datasheet");
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196module_init(pca_isa_init);
197module_exit(pca_isa_exit);