Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | i2c Support for Atmel's AT91 Two-Wire Interface (TWI) |
| 3 | |
| 4 | Copyright (C) 2004 Rick Bronson |
| 5 | Converted to 2.6 by Andrew Victor <andrew@sanpeople.com> |
| 6 | |
| 7 | Borrowed heavily from original work by: |
| 8 | Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com> |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Alexey Dobriyan | c91e23c | 2007-05-10 03:15:58 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 20 | #include <linux/types.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/clk.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | |
| 27 | #include <asm/io.h> |
| 28 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 29 | #include <mach/at91_twi.h> |
| 30 | #include <mach/board.h> |
| 31 | #include <mach/cpu.h> |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 32 | |
| 33 | #define TWI_CLOCK 100000 /* Hz. max 400 Kbits/sec */ |
| 34 | |
| 35 | |
| 36 | static struct clk *twi_clk; |
| 37 | static void __iomem *twi_base; |
| 38 | |
| 39 | #define at91_twi_read(reg) __raw_readl(twi_base + (reg)) |
| 40 | #define at91_twi_write(reg, val) __raw_writel((val), twi_base + (reg)) |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * Initialize the TWI hardware registers. |
| 45 | */ |
| 46 | static void __devinit at91_twi_hwinit(void) |
| 47 | { |
| 48 | unsigned long cdiv, ckdiv; |
| 49 | |
| 50 | at91_twi_write(AT91_TWI_IDR, 0xffffffff); /* Disable all interrupts */ |
| 51 | at91_twi_write(AT91_TWI_CR, AT91_TWI_SWRST); /* Reset peripheral */ |
| 52 | at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN); /* Set Master mode */ |
| 53 | |
| 54 | /* Calcuate clock dividers */ |
| 55 | cdiv = (clk_get_rate(twi_clk) / (2 * TWI_CLOCK)) - 3; |
| 56 | cdiv = cdiv + 1; /* round up */ |
| 57 | ckdiv = 0; |
| 58 | while (cdiv > 255) { |
| 59 | ckdiv++; |
| 60 | cdiv = cdiv >> 1; |
| 61 | } |
| 62 | |
| 63 | if (cpu_is_at91rm9200()) { /* AT91RM9200 Errata #22 */ |
| 64 | if (ckdiv > 5) { |
| 65 | printk(KERN_ERR "AT91 I2C: Invalid TWI_CLOCK value!\n"); |
| 66 | ckdiv = 5; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | at91_twi_write(AT91_TWI_CWGR, (ckdiv << 16) | (cdiv << 8) | cdiv); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Poll the i2c status register until the specified bit is set. |
| 75 | * Returns 0 if timed out (100 msec). |
| 76 | */ |
| 77 | static short at91_poll_status(unsigned long bit) |
| 78 | { |
| 79 | int loop_cntr = 10000; |
| 80 | |
| 81 | do { |
| 82 | udelay(10); |
| 83 | } while (!(at91_twi_read(AT91_TWI_SR) & bit) && (--loop_cntr > 0)); |
| 84 | |
| 85 | return (loop_cntr > 0); |
| 86 | } |
| 87 | |
| 88 | static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length) |
| 89 | { |
| 90 | /* Send Start */ |
| 91 | at91_twi_write(AT91_TWI_CR, AT91_TWI_START); |
| 92 | |
| 93 | /* Read data */ |
| 94 | while (length--) { |
| 95 | if (!length) /* need to send Stop before reading last byte */ |
| 96 | at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP); |
| 97 | if (!at91_poll_status(AT91_TWI_RXRDY)) { |
| 98 | dev_dbg(&adap->dev, "RXRDY timeout\n"); |
| 99 | return -ETIMEDOUT; |
| 100 | } |
| 101 | *buf++ = (at91_twi_read(AT91_TWI_RHR) & 0xff); |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int xfer_write(struct i2c_adapter *adap, unsigned char *buf, int length) |
| 108 | { |
| 109 | /* Load first byte into transmitter */ |
| 110 | at91_twi_write(AT91_TWI_THR, *buf++); |
| 111 | |
| 112 | /* Send Start */ |
| 113 | at91_twi_write(AT91_TWI_CR, AT91_TWI_START); |
| 114 | |
| 115 | do { |
| 116 | if (!at91_poll_status(AT91_TWI_TXRDY)) { |
| 117 | dev_dbg(&adap->dev, "TXRDY timeout\n"); |
| 118 | return -ETIMEDOUT; |
| 119 | } |
| 120 | |
| 121 | length--; /* byte was transmitted */ |
| 122 | |
| 123 | if (length > 0) /* more data to send? */ |
| 124 | at91_twi_write(AT91_TWI_THR, *buf++); |
| 125 | } while (length); |
| 126 | |
| 127 | /* Send Stop */ |
| 128 | at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Generic i2c master transfer entrypoint. |
| 135 | * |
| 136 | * Note: We do not use Atmel's feature of storing the "internal device address". |
Joe Perches | 96acafe | 2008-01-14 21:53:30 +0100 | [diff] [blame] | 137 | * Instead the "internal device address" has to be written using a separate |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 138 | * i2c message. |
| 139 | * http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html |
| 140 | */ |
| 141 | static int at91_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num) |
| 142 | { |
| 143 | int i, ret; |
| 144 | |
| 145 | dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num); |
| 146 | |
| 147 | for (i = 0; i < num; i++) { |
| 148 | dev_dbg(&adap->dev, " #%d: %sing %d byte%s %s 0x%02x\n", i, |
| 149 | pmsg->flags & I2C_M_RD ? "read" : "writ", |
| 150 | pmsg->len, pmsg->len > 1 ? "s" : "", |
| 151 | pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr); |
| 152 | |
| 153 | at91_twi_write(AT91_TWI_MMR, (pmsg->addr << 16) |
| 154 | | ((pmsg->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0)); |
| 155 | |
| 156 | if (pmsg->len && pmsg->buf) { /* sanity check */ |
| 157 | if (pmsg->flags & I2C_M_RD) |
| 158 | ret = xfer_read(adap, pmsg->buf, pmsg->len); |
| 159 | else |
| 160 | ret = xfer_write(adap, pmsg->buf, pmsg->len); |
| 161 | |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | /* Wait until transfer is finished */ |
| 166 | if (!at91_poll_status(AT91_TWI_TXCOMP)) { |
| 167 | dev_dbg(&adap->dev, "TXCOMP timeout\n"); |
| 168 | return -ETIMEDOUT; |
| 169 | } |
| 170 | } |
| 171 | dev_dbg(&adap->dev, "transfer complete\n"); |
| 172 | pmsg++; /* next message */ |
| 173 | } |
| 174 | return i; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Return list of supported functionality. |
| 179 | */ |
| 180 | static u32 at91_func(struct i2c_adapter *adapter) |
| 181 | { |
| 182 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 183 | } |
| 184 | |
| 185 | static struct i2c_algorithm at91_algorithm = { |
| 186 | .master_xfer = at91_xfer, |
| 187 | .functionality = at91_func, |
| 188 | }; |
| 189 | |
| 190 | /* |
| 191 | * Main initialization routine. |
| 192 | */ |
| 193 | static int __devinit at91_i2c_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | struct i2c_adapter *adapter; |
| 196 | struct resource *res; |
| 197 | int rc; |
| 198 | |
| 199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 200 | if (!res) |
| 201 | return -ENXIO; |
| 202 | |
| 203 | if (!request_mem_region(res->start, res->end - res->start + 1, "at91_i2c")) |
| 204 | return -EBUSY; |
| 205 | |
| 206 | twi_base = ioremap(res->start, res->end - res->start + 1); |
| 207 | if (!twi_base) { |
| 208 | rc = -ENOMEM; |
| 209 | goto fail0; |
| 210 | } |
| 211 | |
| 212 | twi_clk = clk_get(NULL, "twi_clk"); |
| 213 | if (IS_ERR(twi_clk)) { |
| 214 | dev_err(&pdev->dev, "no clock defined\n"); |
| 215 | rc = -ENODEV; |
| 216 | goto fail1; |
| 217 | } |
| 218 | |
| 219 | adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL); |
| 220 | if (adapter == NULL) { |
| 221 | dev_err(&pdev->dev, "can't allocate inteface!\n"); |
| 222 | rc = -ENOMEM; |
| 223 | goto fail2; |
| 224 | } |
Jean Delvare | 66c7acf | 2009-01-07 14:29:18 +0100 | [diff] [blame] | 225 | snprintf(adapter->name, sizeof(adapter->name), "AT91"); |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 226 | adapter->algo = &at91_algorithm; |
| 227 | adapter->class = I2C_CLASS_HWMON; |
| 228 | adapter->dev.parent = &pdev->dev; |
David Brownell | 6e99806 | 2007-05-10 03:15:52 -0700 | [diff] [blame] | 229 | /* adapter->id == 0 ... only one TWI controller for now */ |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 230 | |
| 231 | platform_set_drvdata(pdev, adapter); |
| 232 | |
| 233 | clk_enable(twi_clk); /* enable peripheral clock */ |
| 234 | at91_twi_hwinit(); /* initialize TWI controller */ |
| 235 | |
David Brownell | 6e99806 | 2007-05-10 03:15:52 -0700 | [diff] [blame] | 236 | rc = i2c_add_numbered_adapter(adapter); |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 237 | if (rc) { |
| 238 | dev_err(&pdev->dev, "Adapter %s registration failed\n", |
| 239 | adapter->name); |
| 240 | goto fail3; |
| 241 | } |
| 242 | |
| 243 | dev_info(&pdev->dev, "AT91 i2c bus driver.\n"); |
| 244 | return 0; |
| 245 | |
| 246 | fail3: |
| 247 | platform_set_drvdata(pdev, NULL); |
| 248 | kfree(adapter); |
| 249 | clk_disable(twi_clk); |
| 250 | fail2: |
| 251 | clk_put(twi_clk); |
| 252 | fail1: |
| 253 | iounmap(twi_base); |
| 254 | fail0: |
| 255 | release_mem_region(res->start, res->end - res->start + 1); |
| 256 | |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | static int __devexit at91_i2c_remove(struct platform_device *pdev) |
| 261 | { |
| 262 | struct i2c_adapter *adapter = platform_get_drvdata(pdev); |
| 263 | struct resource *res; |
| 264 | int rc; |
| 265 | |
| 266 | rc = i2c_del_adapter(adapter); |
| 267 | platform_set_drvdata(pdev, NULL); |
| 268 | |
| 269 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 270 | iounmap(twi_base); |
| 271 | release_mem_region(res->start, res->end - res->start + 1); |
| 272 | |
| 273 | clk_disable(twi_clk); /* disable peripheral clock */ |
| 274 | clk_put(twi_clk); |
| 275 | |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | #ifdef CONFIG_PM |
| 280 | |
| 281 | /* NOTE: could save a few mA by keeping clock off outside of at91_xfer... */ |
| 282 | |
| 283 | static int at91_i2c_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 284 | { |
| 285 | clk_disable(twi_clk); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int at91_i2c_resume(struct platform_device *pdev) |
| 290 | { |
| 291 | return clk_enable(twi_clk); |
| 292 | } |
| 293 | |
| 294 | #else |
| 295 | #define at91_i2c_suspend NULL |
| 296 | #define at91_i2c_resume NULL |
| 297 | #endif |
| 298 | |
David Brownell | 6e99806 | 2007-05-10 03:15:52 -0700 | [diff] [blame] | 299 | /* work with "modprobe at91_i2c" from hotplugging or coldplugging */ |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 300 | MODULE_ALIAS("platform:at91_i2c"); |
David Brownell | 6e99806 | 2007-05-10 03:15:52 -0700 | [diff] [blame] | 301 | |
Andrew Victor | 813e30e | 2006-12-10 21:21:30 +0100 | [diff] [blame] | 302 | static struct platform_driver at91_i2c_driver = { |
| 303 | .probe = at91_i2c_probe, |
| 304 | .remove = __devexit_p(at91_i2c_remove), |
| 305 | .suspend = at91_i2c_suspend, |
| 306 | .resume = at91_i2c_resume, |
| 307 | .driver = { |
| 308 | .name = "at91_i2c", |
| 309 | .owner = THIS_MODULE, |
| 310 | }, |
| 311 | }; |
| 312 | |
| 313 | static int __init at91_i2c_init(void) |
| 314 | { |
| 315 | return platform_driver_register(&at91_i2c_driver); |
| 316 | } |
| 317 | |
| 318 | static void __exit at91_i2c_exit(void) |
| 319 | { |
| 320 | platform_driver_unregister(&at91_i2c_driver); |
| 321 | } |
| 322 | |
| 323 | module_init(at91_i2c_init); |
| 324 | module_exit(at91_i2c_exit); |
| 325 | |
| 326 | MODULE_AUTHOR("Rick Bronson"); |
| 327 | MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91"); |
| 328 | MODULE_LICENSE("GPL"); |