blob: 29933f87d8fa8fdc31ffdd38c4ff905fddd7774d [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>
Wolfram Sang2378bc02009-03-28 21:34:45 +010026#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/wait.h>
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>
H Hartley Sweeten21782182010-05-21 18:41:01 +020033#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/irq.h>
36
Wolfram Sangc01b0832008-04-22 22:16:46 +020037#define DRIVER "i2c-pca-isa"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define IO_SIZE 4
39
Rene Hermance564032008-10-14 17:30:03 +020040static unsigned long base;
41static int irq = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Data sheet recommends 59kHz for 100kHz operation due to variation
44 * in the actual clock rate */
Marco Aurelio da Costaeff9ec92009-03-28 21:34:44 +010045static int clock = 59000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wolfram Sang2378bc02009-03-28 21:34:45 +010047static struct i2c_adapter pca_isa_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static wait_queue_head_t pca_wait;
49
Wolfram Sangc01b0832008-04-22 22:16:46 +020050static void pca_isa_writebyte(void *pd, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52#ifdef DEBUG_IO
53 static char *names[] = { "T/O", "DAT", "ADR", "CON" };
Frank Seidel154d22b2009-03-28 21:34:42 +010054 printk(KERN_DEBUG "*** write %s at %#lx <= %#04x\n", names[reg],
55 base+reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#endif
57 outb(val, base+reg);
58}
59
Wolfram Sangc01b0832008-04-22 22:16:46 +020060static int pca_isa_readbyte(void *pd, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 int res = inb(base+reg);
63#ifdef DEBUG_IO
64 {
Wolfram Sang3d438292008-04-22 22:16:46 +020065 static char *names[] = { "STA", "DAT", "ADR", "CON" };
Frank Seidel154d22b2009-03-28 21:34:42 +010066 printk(KERN_DEBUG "*** read %s => %#04x\n", names[reg], res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68#endif
69 return res;
70}
71
Wolfram Sangc01b0832008-04-22 22:16:46 +020072static int pca_isa_waitforcompletion(void *pd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Wolfram Sang2378bc02009-03-28 21:34:45 +010074 unsigned long timeout;
Yegor Yefremov6abb9302010-09-30 14:14:22 +020075 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (irq > -1) {
Wolfram Sang22f8b262010-01-16 20:43:13 +010078 ret = wait_event_timeout(pca_wait,
Wolfram Sang2378bc02009-03-28 21:34:45 +010079 pca_isa_readbyte(pd, I2C_PCA_CON)
80 & I2C_PCA_CON_SI, pca_isa_ops.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 } else {
Wolfram Sang2378bc02009-03-28 21:34:45 +010082 /* Do polling */
83 timeout = jiffies + pca_isa_ops.timeout;
Yegor Yefremov6abb9302010-09-30 14:14:22 +020084 do {
85 ret = time_before(jiffies, timeout);
86 if (pca_isa_readbyte(pd, I2C_PCA_CON)
87 & I2C_PCA_CON_SI)
88 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 udelay(100);
Yegor Yefremov6abb9302010-09-30 14:14:22 +020090 } while (ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Yegor Yefremov6abb9302010-09-30 14:14:22 +020092
Wolfram Sang2378bc02009-03-28 21:34:45 +010093 return ret > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Wolfram Sangc01b0832008-04-22 22:16:46 +020096static void pca_isa_resetchip(void *pd)
97{
98 /* apparently only an external reset will do it. not a lot can be done */
99 printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
100}
101
David Howells7d12e782006-10-05 14:55:46 +0100102static irqreturn_t pca_handler(int this_irq, void *dev_id) {
Wolfram Sang22f8b262010-01-16 20:43:13 +0100103 wake_up(&pca_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return IRQ_HANDLED;
105}
106
107static struct i2c_algo_pca_data pca_isa_data = {
Wolfram Sangc01b0832008-04-22 22:16:46 +0200108 /* .data intentionally left NULL, not needed with ISA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .write_byte = pca_isa_writebyte,
110 .read_byte = pca_isa_readbyte,
Wolfram Sangc01b0832008-04-22 22:16:46 +0200111 .wait_for_completion = pca_isa_waitforcompletion,
112 .reset_chip = pca_isa_resetchip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115static struct i2c_adapter pca_isa_ops = {
116 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .algo_data = &pca_isa_data,
Marco Aurelio da Costaeff9ec92009-03-28 21:34:44 +0100118 .name = "PCA9564/PCA9665 ISA Adapter",
Wolfram Sang8e99ada2009-03-28 21:34:45 +0100119 .timeout = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Rene Hermance564032008-10-14 17:30:03 +0200122static int __devinit pca_isa_match(struct device *dev, unsigned int id)
123{
124 int match = base != 0;
125
126 if (match) {
127 if (irq <= -1)
128 dev_warn(dev, "Using polling mode (specify irq)\n");
129 } else
130 dev_err(dev, "Please specify I/O base\n");
131
132 return match;
133}
134
Jean Delvare5cedb052007-05-01 23:26:30 +0200135static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 init_waitqueue_head(&pca_wait);
138
Jean Delvare5cedb052007-05-01 23:26:30 +0200139 dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Kumar Gala68e1ee62008-09-22 14:41:31 -0700141#ifdef CONFIG_PPC
Christian Krafft104cb572008-02-24 20:03:42 +0100142 if (check_legacy_ioport(base)) {
143 dev_err(dev, "I/O address %#08lx is not available\n", base);
144 goto out;
145 }
146#endif
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200149 dev_err(dev, "I/O address %#08lx is in use\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto out;
151 }
152
153 if (irq > -1) {
154 if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200155 dev_err(dev, "Request irq%d failed\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto out_region;
157 }
158 }
159
Wolfram Sangc01b0832008-04-22 22:16:46 +0200160 pca_isa_data.i2c_clock = clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
Jean Delvare5cedb052007-05-01 23:26:30 +0200162 dev_err(dev, "Failed to add i2c bus\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto out_irq;
164 }
165
166 return 0;
167
168 out_irq:
169 if (irq > -1)
170 free_irq(irq, &pca_isa_ops);
171 out_region:
172 release_region(base, IO_SIZE);
173 out:
174 return -ENODEV;
175}
176
Jean Delvare5cedb052007-05-01 23:26:30 +0200177static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Jean Delvare32697112006-12-10 21:21:33 +0100179 i2c_del_adapter(&pca_isa_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Rene Hermance564032008-10-14 17:30:03 +0200181 if (irq > -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 disable_irq(irq);
183 free_irq(irq, &pca_isa_ops);
184 }
185 release_region(base, IO_SIZE);
Jean Delvare5cedb052007-05-01 23:26:30 +0200186
187 return 0;
188}
189
190static struct isa_driver pca_isa_driver = {
Rene Hermance564032008-10-14 17:30:03 +0200191 .match = pca_isa_match,
Jean Delvare5cedb052007-05-01 23:26:30 +0200192 .probe = pca_isa_probe,
193 .remove = __devexit_p(pca_isa_remove),
194 .driver = {
195 .owner = THIS_MODULE,
Wolfram Sangc01b0832008-04-22 22:16:46 +0200196 .name = DRIVER,
Jean Delvare5cedb052007-05-01 23:26:30 +0200197 }
198};
199
200static int __init pca_isa_init(void)
201{
202 return isa_register_driver(&pca_isa_driver, 1);
203}
204
205static void __exit pca_isa_exit(void)
206{
207 isa_unregister_driver(&pca_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
Marco Aurelio da Costaeff9ec92009-03-28 21:34:44 +0100211MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212MODULE_LICENSE("GPL");
213
214module_param(base, ulong, 0);
215MODULE_PARM_DESC(base, "I/O base address");
216
217module_param(irq, int, 0);
218MODULE_PARM_DESC(irq, "IRQ");
219module_param(clock, int, 0);
Marco Aurelio da Costaeff9ec92009-03-28 21:34:44 +0100220MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t"
221 "For PCA9564: 330000,288000,217000,146000,"
222 "88000,59000,44000,36000\n"
223 "\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
224 "\t\t\t\tFast: 100100 - 400099\n"
225 "\t\t\t\tFast+: 400100 - 10000099\n"
226 "\t\t\t\tTurbo: Up to 1265800");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228module_init(pca_isa_init);
229module_exit(pca_isa_exit);