Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * i2c_pca_platform.c |
| 3 | * |
| 4 | * Platform driver for the PCA9564 I2C controller. |
| 5 | * |
| 6 | * Copyright (C) 2008 Pengutronix |
| 7 | * |
| 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. |
| 11 | |
| 12 | */ |
| 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> |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/i2c-algo-pca.h> |
| 24 | #include <linux/i2c-pca-platform.h> |
| 25 | #include <linux/gpio.h> |
| 26 | |
| 27 | #include <asm/irq.h> |
| 28 | #include <asm/io.h> |
| 29 | |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 30 | struct i2c_pca_pf_data { |
| 31 | void __iomem *reg_base; |
| 32 | int irq; /* if 0, use polling */ |
| 33 | int gpio; |
| 34 | wait_queue_head_t wait; |
| 35 | struct i2c_adapter adap; |
| 36 | struct i2c_algo_pca_data algo_data; |
| 37 | unsigned long io_base; |
| 38 | unsigned long io_size; |
| 39 | }; |
| 40 | |
| 41 | /* Read/Write functions for different register alignments */ |
| 42 | |
| 43 | static int i2c_pca_pf_readbyte8(void *pd, int reg) |
| 44 | { |
| 45 | struct i2c_pca_pf_data *i2c = pd; |
| 46 | return ioread8(i2c->reg_base + reg); |
| 47 | } |
| 48 | |
| 49 | static int i2c_pca_pf_readbyte16(void *pd, int reg) |
| 50 | { |
| 51 | struct i2c_pca_pf_data *i2c = pd; |
| 52 | return ioread8(i2c->reg_base + reg * 2); |
| 53 | } |
| 54 | |
| 55 | static int i2c_pca_pf_readbyte32(void *pd, int reg) |
| 56 | { |
| 57 | struct i2c_pca_pf_data *i2c = pd; |
| 58 | return ioread8(i2c->reg_base + reg * 4); |
| 59 | } |
| 60 | |
| 61 | static void i2c_pca_pf_writebyte8(void *pd, int reg, int val) |
| 62 | { |
| 63 | struct i2c_pca_pf_data *i2c = pd; |
| 64 | iowrite8(val, i2c->reg_base + reg); |
| 65 | } |
| 66 | |
| 67 | static void i2c_pca_pf_writebyte16(void *pd, int reg, int val) |
| 68 | { |
| 69 | struct i2c_pca_pf_data *i2c = pd; |
| 70 | iowrite8(val, i2c->reg_base + reg * 2); |
| 71 | } |
| 72 | |
| 73 | static void i2c_pca_pf_writebyte32(void *pd, int reg, int val) |
| 74 | { |
| 75 | struct i2c_pca_pf_data *i2c = pd; |
| 76 | iowrite8(val, i2c->reg_base + reg * 4); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | static int i2c_pca_pf_waitforcompletion(void *pd) |
| 81 | { |
| 82 | struct i2c_pca_pf_data *i2c = pd; |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 83 | long ret = ~0; |
| 84 | unsigned long timeout; |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 85 | |
| 86 | if (i2c->irq) { |
Wolfram Sang | 22f8b26 | 2010-01-16 20:43:13 +0100 | [diff] [blame] | 87 | ret = wait_event_timeout(i2c->wait, |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 88 | i2c->algo_data.read_byte(i2c, I2C_PCA_CON) |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 89 | & I2C_PCA_CON_SI, i2c->adap.timeout); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 90 | } else { |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 91 | /* Do polling */ |
| 92 | timeout = jiffies + i2c->adap.timeout; |
| 93 | while (((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 94 | & I2C_PCA_CON_SI) == 0) |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 95 | && (ret = time_before(jiffies, timeout))) |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 96 | udelay(100); |
| 97 | } |
| 98 | |
Wolfram Sang | 2378bc0 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 99 | return ret > 0; |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void i2c_pca_pf_dummyreset(void *pd) |
| 103 | { |
| 104 | struct i2c_pca_pf_data *i2c = pd; |
| 105 | printk(KERN_WARNING "%s: No reset-pin found. Chip may get stuck!\n", |
| 106 | i2c->adap.name); |
| 107 | } |
| 108 | |
| 109 | static void i2c_pca_pf_resetchip(void *pd) |
| 110 | { |
| 111 | struct i2c_pca_pf_data *i2c = pd; |
| 112 | |
| 113 | gpio_set_value(i2c->gpio, 0); |
| 114 | ndelay(100); |
| 115 | gpio_set_value(i2c->gpio, 1); |
| 116 | } |
| 117 | |
| 118 | static irqreturn_t i2c_pca_pf_handler(int this_irq, void *dev_id) |
| 119 | { |
| 120 | struct i2c_pca_pf_data *i2c = dev_id; |
| 121 | |
| 122 | if ((i2c->algo_data.read_byte(i2c, I2C_PCA_CON) & I2C_PCA_CON_SI) == 0) |
| 123 | return IRQ_NONE; |
| 124 | |
Wolfram Sang | 22f8b26 | 2010-01-16 20:43:13 +0100 | [diff] [blame] | 125 | wake_up(&i2c->wait); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 126 | |
| 127 | return IRQ_HANDLED; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static int __devinit i2c_pca_pf_probe(struct platform_device *pdev) |
| 132 | { |
| 133 | struct i2c_pca_pf_data *i2c; |
| 134 | struct resource *res; |
| 135 | struct i2c_pca9564_pf_platform_data *platform_data = |
| 136 | pdev->dev.platform_data; |
| 137 | int ret = 0; |
| 138 | int irq; |
| 139 | |
| 140 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 141 | irq = platform_get_irq(pdev, 0); |
| 142 | /* If irq is 0, we do polling. */ |
| 143 | |
| 144 | if (res == NULL) { |
| 145 | ret = -ENODEV; |
| 146 | goto e_print; |
| 147 | } |
| 148 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 149 | if (!request_mem_region(res->start, resource_size(res), res->name)) { |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 150 | ret = -ENOMEM; |
| 151 | goto e_print; |
| 152 | } |
| 153 | |
| 154 | i2c = kzalloc(sizeof(struct i2c_pca_pf_data), GFP_KERNEL); |
| 155 | if (!i2c) { |
| 156 | ret = -ENOMEM; |
| 157 | goto e_alloc; |
| 158 | } |
| 159 | |
| 160 | init_waitqueue_head(&i2c->wait); |
| 161 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 162 | i2c->reg_base = ioremap(res->start, resource_size(res)); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 163 | if (!i2c->reg_base) { |
Wolfram Sang | 7650da0 | 2008-07-14 22:38:26 +0200 | [diff] [blame] | 164 | ret = -ENOMEM; |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 165 | goto e_remap; |
| 166 | } |
| 167 | i2c->io_base = res->start; |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 168 | i2c->io_size = resource_size(res); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 169 | i2c->irq = irq; |
| 170 | |
| 171 | i2c->adap.nr = pdev->id >= 0 ? pdev->id : 0; |
| 172 | i2c->adap.owner = THIS_MODULE; |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame] | 173 | snprintf(i2c->adap.name, sizeof(i2c->adap.name), |
| 174 | "PCA9564/PCA9665 at 0x%08lx", |
| 175 | (unsigned long) res->start); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 176 | i2c->adap.algo_data = &i2c->algo_data; |
| 177 | i2c->adap.dev.parent = &pdev->dev; |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 178 | |
Wolfram Sang | 6b110d1 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 179 | if (platform_data) { |
| 180 | i2c->adap.timeout = platform_data->timeout; |
| 181 | i2c->algo_data.i2c_clock = platform_data->i2c_clock_speed; |
| 182 | i2c->gpio = platform_data->gpio; |
| 183 | } else { |
| 184 | i2c->adap.timeout = HZ; |
| 185 | i2c->algo_data.i2c_clock = 59000; |
| 186 | i2c->gpio = -1; |
| 187 | } |
| 188 | |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 189 | i2c->algo_data.data = i2c; |
Wolfram Sang | 6b110d1 | 2009-03-28 21:34:45 +0100 | [diff] [blame] | 190 | i2c->algo_data.wait_for_completion = i2c_pca_pf_waitforcompletion; |
| 191 | i2c->algo_data.reset_chip = i2c_pca_pf_dummyreset; |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 192 | |
| 193 | switch (res->flags & IORESOURCE_MEM_TYPE_MASK) { |
| 194 | case IORESOURCE_MEM_32BIT: |
| 195 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte32; |
| 196 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte32; |
| 197 | break; |
| 198 | case IORESOURCE_MEM_16BIT: |
| 199 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte16; |
| 200 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte16; |
| 201 | break; |
| 202 | case IORESOURCE_MEM_8BIT: |
| 203 | default: |
| 204 | i2c->algo_data.write_byte = i2c_pca_pf_writebyte8; |
| 205 | i2c->algo_data.read_byte = i2c_pca_pf_readbyte8; |
| 206 | break; |
| 207 | } |
| 208 | |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 209 | /* Use gpio_is_valid() when in mainline */ |
| 210 | if (i2c->gpio > -1) { |
| 211 | ret = gpio_request(i2c->gpio, i2c->adap.name); |
| 212 | if (ret == 0) { |
| 213 | gpio_direction_output(i2c->gpio, 1); |
| 214 | i2c->algo_data.reset_chip = i2c_pca_pf_resetchip; |
| 215 | } else { |
| 216 | printk(KERN_WARNING "%s: Registering gpio failed!\n", |
| 217 | i2c->adap.name); |
| 218 | i2c->gpio = ret; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (irq) { |
| 223 | ret = request_irq(irq, i2c_pca_pf_handler, |
| 224 | IRQF_TRIGGER_FALLING, i2c->adap.name, i2c); |
| 225 | if (ret) |
| 226 | goto e_reqirq; |
| 227 | } |
| 228 | |
| 229 | if (i2c_pca_add_numbered_bus(&i2c->adap) < 0) { |
| 230 | ret = -ENODEV; |
| 231 | goto e_adapt; |
| 232 | } |
| 233 | |
| 234 | platform_set_drvdata(pdev, i2c); |
| 235 | |
| 236 | printk(KERN_INFO "%s registered.\n", i2c->adap.name); |
| 237 | |
| 238 | return 0; |
| 239 | |
| 240 | e_adapt: |
| 241 | if (irq) |
| 242 | free_irq(irq, i2c); |
| 243 | e_reqirq: |
| 244 | if (i2c->gpio > -1) |
| 245 | gpio_free(i2c->gpio); |
| 246 | |
| 247 | iounmap(i2c->reg_base); |
| 248 | e_remap: |
| 249 | kfree(i2c); |
| 250 | e_alloc: |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 251 | release_mem_region(res->start, resource_size(res)); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 252 | e_print: |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame] | 253 | printk(KERN_ERR "Registering PCA9564/PCA9665 FAILED! (%d)\n", ret); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static int __devexit i2c_pca_pf_remove(struct platform_device *pdev) |
| 258 | { |
| 259 | struct i2c_pca_pf_data *i2c = platform_get_drvdata(pdev); |
| 260 | platform_set_drvdata(pdev, NULL); |
| 261 | |
| 262 | i2c_del_adapter(&i2c->adap); |
| 263 | |
| 264 | if (i2c->irq) |
| 265 | free_irq(i2c->irq, i2c); |
| 266 | |
| 267 | if (i2c->gpio > -1) |
| 268 | gpio_free(i2c->gpio); |
| 269 | |
| 270 | iounmap(i2c->reg_base); |
| 271 | release_mem_region(i2c->io_base, i2c->io_size); |
| 272 | kfree(i2c); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static struct platform_driver i2c_pca_pf_driver = { |
| 278 | .probe = i2c_pca_pf_probe, |
| 279 | .remove = __devexit_p(i2c_pca_pf_remove), |
| 280 | .driver = { |
| 281 | .name = "i2c-pca-platform", |
| 282 | .owner = THIS_MODULE, |
| 283 | }, |
| 284 | }; |
| 285 | |
| 286 | static int __init i2c_pca_pf_init(void) |
| 287 | { |
| 288 | return platform_driver_register(&i2c_pca_pf_driver); |
| 289 | } |
| 290 | |
| 291 | static void __exit i2c_pca_pf_exit(void) |
| 292 | { |
| 293 | platform_driver_unregister(&i2c_pca_pf_driver); |
| 294 | } |
| 295 | |
| 296 | MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>"); |
Marco Aurelio da Costa | eff9ec9 | 2009-03-28 21:34:44 +0100 | [diff] [blame] | 297 | MODULE_DESCRIPTION("I2C-PCA9564/PCA9665 platform driver"); |
Wolfram Sang | 244fbbb | 2008-04-22 22:16:46 +0200 | [diff] [blame] | 298 | MODULE_LICENSE("GPL"); |
| 299 | |
| 300 | module_init(i2c_pca_pf_init); |
| 301 | module_exit(i2c_pca_pf_exit); |
| 302 | |