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