1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * i2c-pca-isa.c driver for PCA9564 on ISA boards
4 * Copyright (C) 2004 Arcom Control Systems
5 * Copyright (C) 2008 Pengutronix
8 #include <linux/kernel.h>
9 #include <linux/ioport.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/delay.h>
13 #include <linux/jiffies.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/wait.h>
17 #include <linux/isa.h>
18 #include <linux/i2c.h>
19 #include <linux/i2c-algo-pca.h>
24 #define DRIVER "i2c-pca-isa"
27 static unsigned long base
;
30 /* Data sheet recommends 59kHz for 100kHz operation due to variation
31 * in the actual clock rate */
32 static int clock
= 59000;
34 static struct i2c_adapter pca_isa_ops
;
35 static wait_queue_head_t pca_wait
;
37 static void pca_isa_writebyte(void *pd
, int reg
, int val
)
40 static char *names
[] = { "T/O", "DAT", "ADR", "CON" };
41 printk(KERN_DEBUG
"*** write %s at %#lx <= %#04x\n", names
[reg
],
47 static int pca_isa_readbyte(void *pd
, int reg
)
49 int res
= inb(base
+reg
);
52 static char *names
[] = { "STA", "DAT", "ADR", "CON" };
53 printk(KERN_DEBUG
"*** read %s => %#04x\n", names
[reg
], res
);
59 static int pca_isa_waitforcompletion(void *pd
)
61 unsigned long timeout
;
65 ret
= wait_event_timeout(pca_wait
,
66 pca_isa_readbyte(pd
, I2C_PCA_CON
)
67 & I2C_PCA_CON_SI
, pca_isa_ops
.timeout
);
70 timeout
= jiffies
+ pca_isa_ops
.timeout
;
72 ret
= time_before(jiffies
, timeout
);
73 if (pca_isa_readbyte(pd
, I2C_PCA_CON
)
83 static void pca_isa_resetchip(void *pd
)
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");
89 static irqreturn_t
pca_handler(int this_irq
, void *dev_id
) {
94 static struct i2c_algo_pca_data pca_isa_data
= {
95 /* .data intentionally left NULL, not needed with ISA */
96 .write_byte
= pca_isa_writebyte
,
97 .read_byte
= pca_isa_readbyte
,
98 .wait_for_completion
= pca_isa_waitforcompletion
,
99 .reset_chip
= pca_isa_resetchip
,
102 static struct i2c_adapter pca_isa_ops
= {
103 .owner
= THIS_MODULE
,
104 .algo_data
= &pca_isa_data
,
105 .name
= "PCA9564/PCA9665 ISA Adapter",
109 static int pca_isa_match(struct device
*dev
, unsigned int id
)
111 int match
= base
!= 0;
115 dev_warn(dev
, "Using polling mode (specify irq)\n");
117 dev_err(dev
, "Please specify I/O base\n");
122 static int pca_isa_probe(struct device
*dev
, unsigned int id
)
124 init_waitqueue_head(&pca_wait
);
126 dev_info(dev
, "i/o base %#08lx. irq %d\n", base
, irq
);
129 if (check_legacy_ioport(base
)) {
130 dev_err(dev
, "I/O address %#08lx is not available\n", base
);
135 if (!request_region(base
, IO_SIZE
, "i2c-pca-isa")) {
136 dev_err(dev
, "I/O address %#08lx is in use\n", base
);
141 if (request_irq(irq
, pca_handler
, 0, "i2c-pca-isa", &pca_isa_ops
) < 0) {
142 dev_err(dev
, "Request irq%d failed\n", irq
);
147 pca_isa_data
.i2c_clock
= clock
;
148 if (i2c_pca_add_bus(&pca_isa_ops
) < 0) {
149 dev_err(dev
, "Failed to add i2c bus\n");
157 free_irq(irq
, &pca_isa_ops
);
159 release_region(base
, IO_SIZE
);
164 static int pca_isa_remove(struct device
*dev
, unsigned int id
)
166 i2c_del_adapter(&pca_isa_ops
);
170 free_irq(irq
, &pca_isa_ops
);
172 release_region(base
, IO_SIZE
);
177 static struct isa_driver pca_isa_driver
= {
178 .match
= pca_isa_match
,
179 .probe
= pca_isa_probe
,
180 .remove
= pca_isa_remove
,
182 .owner
= THIS_MODULE
,
187 MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
188 MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
189 MODULE_LICENSE("GPL");
191 module_param_hw(base
, ulong
, ioport
, 0);
192 MODULE_PARM_DESC(base
, "I/O base address");
193 module_param_hw(irq
, int, irq
, 0);
194 MODULE_PARM_DESC(irq
, "IRQ");
195 module_param(clock
, int, 0);
196 MODULE_PARM_DESC(clock
, "Clock rate in hertz.\n\t\t"
197 "For PCA9564: 330000,288000,217000,146000,"
198 "88000,59000,44000,36000\n"
199 "\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
200 "\t\t\t\tFast: 100100 - 400099\n"
201 "\t\t\t\tFast+: 400100 - 10000099\n"
202 "\t\t\t\tTurbo: Up to 1265800");
203 module_isa_driver(pca_isa_driver
, 1);