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