Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * i2c-pca-isa.c driver for PCA9564 on ISA boards |
| 3 | * Copyright (C) 2004 Arcom Control Systems |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008 Pengutronix |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/wait.h> |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 29 | #include <linux/isa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/i2c.h> |
| 31 | #include <linux/i2c-algo-pca.h> |
| 32 | |
| 33 | #include <asm/io.h> |
| 34 | #include <asm/irq.h> |
| 35 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 36 | #define DRIVER "i2c-pca-isa" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define IO_SIZE 4 |
| 38 | |
Rene Herman | ce56403 | 2008-10-14 17:30:03 +0200 | [diff] [blame] | 39 | static unsigned long base; |
| 40 | static int irq = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* Data sheet recommends 59kHz for 100kHz operation due to variation |
| 43 | * in the actual clock rate */ |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame^] | 44 | static int clock = 59000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static wait_queue_head_t pca_wait; |
| 47 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 48 | static void pca_isa_writebyte(void *pd, int reg, int val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
| 50 | #ifdef DEBUG_IO |
| 51 | static char *names[] = { "T/O", "DAT", "ADR", "CON" }; |
Frank Seidel | 154d22b | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 52 | printk(KERN_DEBUG "*** write %s at %#lx <= %#04x\n", names[reg], |
| 53 | base+reg, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #endif |
| 55 | outb(val, base+reg); |
| 56 | } |
| 57 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 58 | static int pca_isa_readbyte(void *pd, int reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | int res = inb(base+reg); |
| 61 | #ifdef DEBUG_IO |
| 62 | { |
Wolfram Sang | 3d43829 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 63 | static char *names[] = { "STA", "DAT", "ADR", "CON" }; |
Frank Seidel | 154d22b | 2009-03-28 21:34:42 +0100 | [diff] [blame] | 64 | printk(KERN_DEBUG "*** read %s => %#04x\n", names[reg], res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | return res; |
| 68 | } |
| 69 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 70 | static int pca_isa_waitforcompletion(void *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | int ret = 0; |
| 73 | |
| 74 | if (irq > -1) { |
| 75 | ret = wait_event_interruptible(pca_wait, |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 76 | pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } else { |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 78 | while ((pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | udelay(100); |
| 80 | } |
| 81 | return ret; |
| 82 | } |
| 83 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 84 | static void pca_isa_resetchip(void *pd) |
| 85 | { |
| 86 | /* apparently only an external reset will do it. not a lot can be done */ |
| 87 | printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n"); |
| 88 | } |
| 89 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 90 | static irqreturn_t pca_handler(int this_irq, void *dev_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | wake_up_interruptible(&pca_wait); |
| 92 | return IRQ_HANDLED; |
| 93 | } |
| 94 | |
| 95 | static struct i2c_algo_pca_data pca_isa_data = { |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 96 | /* .data intentionally left NULL, not needed with ISA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | .write_byte = pca_isa_writebyte, |
| 98 | .read_byte = pca_isa_readbyte, |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 99 | .wait_for_completion = pca_isa_waitforcompletion, |
| 100 | .reset_chip = pca_isa_resetchip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static struct i2c_adapter pca_isa_ops = { |
| 104 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .algo_data = &pca_isa_data, |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame^] | 106 | .name = "PCA9564/PCA9665 ISA Adapter", |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 107 | .timeout = 100, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Rene Herman | ce56403 | 2008-10-14 17:30:03 +0200 | [diff] [blame] | 110 | static int __devinit pca_isa_match(struct device *dev, unsigned int id) |
| 111 | { |
| 112 | int match = base != 0; |
| 113 | |
| 114 | if (match) { |
| 115 | if (irq <= -1) |
| 116 | dev_warn(dev, "Using polling mode (specify irq)\n"); |
| 117 | } else |
| 118 | dev_err(dev, "Please specify I/O base\n"); |
| 119 | |
| 120 | return match; |
| 121 | } |
| 122 | |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 123 | static int __devinit pca_isa_probe(struct device *dev, unsigned int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | init_waitqueue_head(&pca_wait); |
| 126 | |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 127 | dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Kumar Gala | 68e1ee6 | 2008-09-22 14:41:31 -0700 | [diff] [blame] | 129 | #ifdef CONFIG_PPC |
Christian Krafft | 104cb57 | 2008-02-24 20:03:42 +0100 | [diff] [blame] | 130 | if (check_legacy_ioport(base)) { |
| 131 | dev_err(dev, "I/O address %#08lx is not available\n", base); |
| 132 | goto out; |
| 133 | } |
| 134 | #endif |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!request_region(base, IO_SIZE, "i2c-pca-isa")) { |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 137 | dev_err(dev, "I/O address %#08lx is in use\n", base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | goto out; |
| 139 | } |
| 140 | |
| 141 | if (irq > -1) { |
| 142 | if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) { |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 143 | dev_err(dev, "Request irq%d failed\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | goto out_region; |
| 145 | } |
| 146 | } |
| 147 | |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 148 | pca_isa_data.i2c_clock = clock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (i2c_pca_add_bus(&pca_isa_ops) < 0) { |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 150 | dev_err(dev, "Failed to add i2c bus\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto out_irq; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | |
| 156 | out_irq: |
| 157 | if (irq > -1) |
| 158 | free_irq(irq, &pca_isa_ops); |
| 159 | out_region: |
| 160 | release_region(base, IO_SIZE); |
| 161 | out: |
| 162 | return -ENODEV; |
| 163 | } |
| 164 | |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 165 | static int __devexit pca_isa_remove(struct device *dev, unsigned int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 167 | i2c_del_adapter(&pca_isa_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Rene Herman | ce56403 | 2008-10-14 17:30:03 +0200 | [diff] [blame] | 169 | if (irq > -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | disable_irq(irq); |
| 171 | free_irq(irq, &pca_isa_ops); |
| 172 | } |
| 173 | release_region(base, IO_SIZE); |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static struct isa_driver pca_isa_driver = { |
Rene Herman | ce56403 | 2008-10-14 17:30:03 +0200 | [diff] [blame] | 179 | .match = pca_isa_match, |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 180 | .probe = pca_isa_probe, |
| 181 | .remove = __devexit_p(pca_isa_remove), |
| 182 | .driver = { |
| 183 | .owner = THIS_MODULE, |
Wolfram Sang | c01b083 | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 184 | .name = DRIVER, |
Jean Delvare | 5cedb05 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
| 188 | static int __init pca_isa_init(void) |
| 189 | { |
| 190 | return isa_register_driver(&pca_isa_driver, 1); |
| 191 | } |
| 192 | |
| 193 | static void __exit pca_isa_exit(void) |
| 194 | { |
| 195 | isa_unregister_driver(&pca_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>"); |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame^] | 199 | MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | MODULE_LICENSE("GPL"); |
| 201 | |
| 202 | module_param(base, ulong, 0); |
| 203 | MODULE_PARM_DESC(base, "I/O base address"); |
| 204 | |
| 205 | module_param(irq, int, 0); |
| 206 | MODULE_PARM_DESC(irq, "IRQ"); |
| 207 | module_param(clock, int, 0); |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame^] | 208 | MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t" |
| 209 | "For PCA9564: 330000,288000,217000,146000," |
| 210 | "88000,59000,44000,36000\n" |
| 211 | "\t\tFor PCA9665:\tStandard: 60300 - 100099\n" |
| 212 | "\t\t\t\tFast: 100100 - 400099\n" |
| 213 | "\t\t\t\tFast+: 400100 - 10000099\n" |
| 214 | "\t\t\t\tTurbo: Up to 1265800"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | module_init(pca_isa_init); |
| 217 | module_exit(pca_isa_exit); |