4 * Platform driver for the PCA9564 I2C controller.
6 * Copyright (C) 2008 Pengutronix
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/i2c-algo-pca.h>
23 #include <linux/i2c-pca-platform.h>
24 #include <linux/gpio.h>
29 #define res_len(r) ((r)->end - (r)->start + 1)
31 struct i2c_pca_pf_data {
32 void __iomem *reg_base;
33 int irq; /* if 0, use polling */
35 wait_queue_head_t wait;
36 struct i2c_adapter adap;
37 struct i2c_algo_pca_data algo_data;
38 unsigned long io_base;
39 unsigned long io_size;
42 /* Read/Write functions for different register alignments */
44 static int i2c_pca_pf_readbyte8(void *pd, int reg)
46 struct i2c_pca_pf_data *i2c = pd;
47 return ioread8(i2c->reg_base + reg);
50 static int i2c_pca_pf_readbyte16(void *pd, int reg)
52 struct i2c_pca_pf_data *i2c = pd;
53 return ioread8(i2c->reg_base + reg * 2);
56 static int i2c_pca_pf_readbyte32(void *pd, int reg)
58 struct i2c_pca_pf_data *i2c = pd;
59 return ioread8(i2c->reg_base + reg * 4);
62 static void i2c_pca_pf_writebyte8(void *pd, int reg, int val)
64 struct i2c_pca_pf_data *i2c = pd;
65 iowrite8(val, i2c->reg_base + reg);
68 static void i2c_pca_pf_writebyte16(void *pd, int reg, int val)
70 struct i2c_pca_pf_data *i2c = pd;
71 iowrite8(val, i2c->reg_base + reg * 2);
74 static void i2c_pca_pf_writebyte32(void *pd, int reg, int val)
76 struct i2c_pca_pf_data *i2c = pd;
77 iowrite8(val, i2c->reg_base + reg * 4);
81 static int i2c_pca_pf_waitforcompletion(void *pd)
83 struct i2c_pca_pf_data *i2c = pd;
87 ret = wait_event_interruptible(i2c->wait,
88 i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
93 * XXX: Could get stuck in extreme cases!
94 * Maybe add timeout, but using irqs is preferred anyhow.
96 while ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON)
97 & I2C_PCA_CON_SI) == 0)
104 static void i2c_pca_pf_dummyreset(void *pd)
106 struct i2c_pca_pf_data *i2c = pd;
107 printk(KERN_WARNING "%s: No reset-pin found. Chip may get stuck!\n",
111 static void i2c_pca_pf_resetchip(void *pd)
113 struct i2c_pca_pf_data *i2c = pd;
115 gpio_set_value(i2c->gpio, 0);
117 gpio_set_value(i2c->gpio, 1);
120 static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id)
122 struct i2c_pca_pf_data *i2c = dev_id;
124 if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0)
127 wake_up_interruptible(&i2c->wait);
133 static int __devinit i2c_pca_pf_probe(struct platform_device *pdev)
135 struct i2c_pca_pf_data *i2c;
136 struct resource *res;
137 struct i2c_pca9564_pf_platform_data *platform_data =
138 pdev->dev.platform_data;
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 irq = platform_get_irq(pdev, 0);
144 /* If irq is 0, we do polling. */
151 if (!request_mem_region(res->start, res_len(res), res->name)) {
156 i2c = kzalloc(sizeof(struct i2c_pca_pf_data), GFP_KERNEL);
162 init_waitqueue_head(&i2c->wait);
164 i2c->reg_base = ioremap(res->start, res_len(res));
165 if (!i2c->reg_base) {
169 i2c->io_base = res->start;
170 i2c->io_size = res_len(res);
173 i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0;
174 i2c->adap.owner = THIS_MODULE;
175 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "PCA9564 at 0x%08lx",
176 (unsigned long) res->start);
177 i2c->adap.algo_data = &i2c->algo_data;
178 i2c->adap.dev.parent = &pdev->dev;
179 i2c->adap.timeout = platform_data->timeout;
181 i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed;
182 i2c->algo_data.data = i2c;
184 switch (res->flags & IORESOURCE_MEM_TYPE_MASK) {
185 case IORESOURCE_MEM_32BIT:
186 i2c->algo_data.write_byte = i2c_pca_pf_writebyte32;
187 i2c->algo_data.read_byte = i2c_pca_pf_readbyte32;
189 case IORESOURCE_MEM_16BIT:
190 i2c->algo_data.write_byte = i2c_pca_pf_writebyte16;
191 i2c->algo_data.read_byte = i2c_pca_pf_readbyte16;
193 case IORESOURCE_MEM_8BIT:
195 i2c->algo_data.write_byte = i2c_pca_pf_writebyte8;
196 i2c->algo_data.read_byte = i2c_pca_pf_readbyte8;
200 i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion;
202 i2c->gpio = platform_data->gpio;
203 i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset;
205 /* Use gpio_is_valid() when in mainline */
206 if (i2c->gpio > -1) {
207 ret = gpio_request(i2c->gpio, i2c->adap.name);
209 gpio_direction_output(i2c->gpio, 1);
210 i2c->algo_data.reset_chip = i2c_pca_pf_resetchip;
212 printk(KERN_WARNING "%s: Registering gpio failed!\n",
219 ret = request_irq(irq, i2c_pca_pf_handler,
220 IRQF_TRIGGER_FALLING, i2c->adap.name, i2c);
225 if (i2c_pca_add_numbered_bus(&i2c->adap) < 0) {
230 platform_set_drvdata(pdev, i2c);
232 printk(KERN_INFO "%s registered.\n", i2c->adap.name);
241 gpio_free(i2c->gpio);
243 iounmap(i2c->reg_base);
247 release_mem_region(res->start, res_len(res));
249 printk(KERN_ERR "Registering PCA9564 FAILED! (%d)\n", ret);
253 static int __devexit i2c_pca_pf_remove(struct platform_device *pdev)
255 struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev);
256 platform_set_drvdata(pdev, NULL);
258 i2c_del_adapter(&i2c->adap);
261 free_irq(i2c->irq, i2c);
264 gpio_free(i2c->gpio);
266 iounmap(i2c->reg_base);
267 release_mem_region(i2c->io_base, i2c->io_size);
273 static struct platform_driver i2c_pca_pf_driver = {
274 .probe = i2c_pca_pf_probe,
275 .remove = __devexit_p(i2c_pca_pf_remove),
277 .name = "i2c-pca-platform",
278 .owner = THIS_MODULE,
282 static int __init i2c_pca_pf_init(void)
284 return platform_driver_register(&i2c_pca_pf_driver);
287 static void __exit i2c_pca_pf_exit(void)
289 platform_driver_unregister(&i2c_pca_pf_driver);
292 MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
293 MODULE_DESCRIPTION("I2C-PCA9564 platform driver");
294 MODULE_LICENSE("GPL");
296 module_init(i2c_pca_pf_init);
297 module_exit(i2c_pca_pf_exit);