i2c-pca-isa.c 5.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  i2c-pca-isa.c driver for PCA9564 on ISA boards
 *    Copyright (C) 2004 Arcom Control Systems
4
 *    Copyright (C) 2008 Pengutronix
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/kernel.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
29
#include <linux/isa.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35
#include <linux/i2c.h>
#include <linux/i2c-algo-pca.h>

#include <asm/io.h>
#include <asm/irq.h>

36
#define DRIVER "i2c-pca-isa"
L
Linus Torvalds 已提交
37 38
#define IO_SIZE 4

39 40
static unsigned long base;
static int irq = -1;
L
Linus Torvalds 已提交
41 42 43

/* Data sheet recommends 59kHz for 100kHz operation due to variation
 * in the actual clock rate */
44
static int clock  = 59000;
L
Linus Torvalds 已提交
45 46 47

static wait_queue_head_t pca_wait;

48
static void pca_isa_writebyte(void *pd, int reg, int val)
L
Linus Torvalds 已提交
49 50 51
{
#ifdef DEBUG_IO
	static char *names[] = { "T/O", "DAT", "ADR", "CON" };
52 53
	printk(KERN_DEBUG "*** write %s at %#lx <= %#04x\n", names[reg],
	       base+reg, val);
L
Linus Torvalds 已提交
54 55 56 57
#endif
	outb(val, base+reg);
}

58
static int pca_isa_readbyte(void *pd, int reg)
L
Linus Torvalds 已提交
59 60 61 62
{
	int res = inb(base+reg);
#ifdef DEBUG_IO
	{
63
		static char *names[] = { "STA", "DAT", "ADR", "CON" };
64
		printk(KERN_DEBUG "*** read  %s => %#04x\n", names[reg], res);
L
Linus Torvalds 已提交
65 66 67 68 69
	}
#endif
	return res;
}

70
static int pca_isa_waitforcompletion(void *pd)
L
Linus Torvalds 已提交
71 72 73 74 75
{
	int ret = 0;

	if (irq > -1) {
		ret = wait_event_interruptible(pca_wait,
76
					       pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI);
L
Linus Torvalds 已提交
77
	} else {
78
		while ((pca_isa_readbyte(pd, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
L
Linus Torvalds 已提交
79 80 81 82 83
			udelay(100);
	}
	return ret;
}

84 85 86 87 88 89
static void pca_isa_resetchip(void *pd)
{
	/* apparently only an external reset will do it. not a lot can be done */
	printk(KERN_WARNING DRIVER ": Haven't figured out how to do a reset yet\n");
}

90
static irqreturn_t pca_handler(int this_irq, void *dev_id) {
L
Linus Torvalds 已提交
91 92 93 94 95
	wake_up_interruptible(&pca_wait);
	return IRQ_HANDLED;
}

static struct i2c_algo_pca_data pca_isa_data = {
96
	/* .data intentionally left NULL, not needed with ISA */
L
Linus Torvalds 已提交
97 98
	.write_byte		= pca_isa_writebyte,
	.read_byte		= pca_isa_readbyte,
99 100
	.wait_for_completion	= pca_isa_waitforcompletion,
	.reset_chip		= pca_isa_resetchip,
L
Linus Torvalds 已提交
101 102 103 104 105
};

static struct i2c_adapter pca_isa_ops = {
	.owner          = THIS_MODULE,
	.algo_data	= &pca_isa_data,
106
	.name		= "PCA9564/PCA9665 ISA Adapter",
107
	.timeout	= 100,
L
Linus Torvalds 已提交
108 109
};

110 111 112 113 114 115 116 117 118 119 120 121 122
static int __devinit pca_isa_match(struct device *dev, unsigned int id)
{
	int match = base != 0;

	if (match) {
		if (irq <= -1)
			dev_warn(dev, "Using polling mode (specify irq)\n");
	} else
		dev_err(dev, "Please specify I/O base\n");

	return match;
}

123
static int __devinit pca_isa_probe(struct device *dev, unsigned int id)
L
Linus Torvalds 已提交
124 125 126
{
	init_waitqueue_head(&pca_wait);

127
	dev_info(dev, "i/o base %#08lx. irq %d\n", base, irq);
L
Linus Torvalds 已提交
128

129
#ifdef CONFIG_PPC
130 131 132 133 134 135
	if (check_legacy_ioport(base)) {
		dev_err(dev, "I/O address %#08lx is not available\n", base);
		goto out;
	}
#endif

L
Linus Torvalds 已提交
136
	if (!request_region(base, IO_SIZE, "i2c-pca-isa")) {
137
		dev_err(dev, "I/O address %#08lx is in use\n", base);
L
Linus Torvalds 已提交
138 139 140 141 142
		goto out;
	}

	if (irq > -1) {
		if (request_irq(irq, pca_handler, 0, "i2c-pca-isa", &pca_isa_ops) < 0) {
143
			dev_err(dev, "Request irq%d failed\n", irq);
L
Linus Torvalds 已提交
144 145 146 147
			goto out_region;
		}
	}

148
	pca_isa_data.i2c_clock = clock;
L
Linus Torvalds 已提交
149
	if (i2c_pca_add_bus(&pca_isa_ops) < 0) {
150
		dev_err(dev, "Failed to add i2c bus\n");
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164
		goto out_irq;
	}

	return 0;

 out_irq:
	if (irq > -1)
		free_irq(irq, &pca_isa_ops);
 out_region:
	release_region(base, IO_SIZE);
 out:
	return -ENODEV;
}

165
static int __devexit pca_isa_remove(struct device *dev, unsigned int id)
L
Linus Torvalds 已提交
166
{
167
	i2c_del_adapter(&pca_isa_ops);
L
Linus Torvalds 已提交
168

169
	if (irq > -1) {
L
Linus Torvalds 已提交
170 171 172 173
		disable_irq(irq);
		free_irq(irq, &pca_isa_ops);
	}
	release_region(base, IO_SIZE);
174 175 176 177 178

	return 0;
}

static struct isa_driver pca_isa_driver = {
179
	.match		= pca_isa_match,
180 181 182 183
	.probe		= pca_isa_probe,
	.remove		= __devexit_p(pca_isa_remove),
	.driver = {
		.owner	= THIS_MODULE,
184
		.name	= DRIVER,
185 186 187 188 189 190 191 192 193 194 195
	}
};

static int __init pca_isa_init(void)
{
	return isa_register_driver(&pca_isa_driver, 1);
}

static void __exit pca_isa_exit(void)
{
	isa_unregister_driver(&pca_isa_driver);
L
Linus Torvalds 已提交
196 197 198
}

MODULE_AUTHOR("Ian Campbell <icampbell@arcom.com>");
199
MODULE_DESCRIPTION("ISA base PCA9564/PCA9665 driver");
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207
MODULE_LICENSE("GPL");

module_param(base, ulong, 0);
MODULE_PARM_DESC(base, "I/O base address");

module_param(irq, int, 0);
MODULE_PARM_DESC(irq, "IRQ");
module_param(clock, int, 0);
208 209 210 211 212 213 214
MODULE_PARM_DESC(clock, "Clock rate in hertz.\n\t\t"
		"For PCA9564: 330000,288000,217000,146000,"
		"88000,59000,44000,36000\n"
		"\t\tFor PCA9665:\tStandard: 60300 - 100099\n"
		"\t\t\t\tFast: 100100 - 400099\n"
		"\t\t\t\tFast+: 400100 - 10000099\n"
		"\t\t\t\tTurbo: Up to 1265800");
L
Linus Torvalds 已提交
215 216 217

module_init(pca_isa_init);
module_exit(pca_isa_exit);