Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / i2c / busses / i2c-at91.c
blob1679deef9c890131a951ee5cb8d50217ecb9bcf5
1 /*
2 i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
4 Copyright (C) 2004 Rick Bronson
5 Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
7 Borrowed heavily from original work by:
8 Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
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.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
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 #include <linux/io.h>
28 #include <mach/at91_twi.h>
29 #include <mach/board.h>
30 #include <mach/cpu.h>
32 #define TWI_CLOCK 100000 /* Hz. max 400 Kbits/sec */
35 static struct clk *twi_clk;
36 static void __iomem *twi_base;
38 #define at91_twi_read(reg) __raw_readl(twi_base + (reg))
39 #define at91_twi_write(reg, val) __raw_writel((val), twi_base + (reg))
43 * Initialize the TWI hardware registers.
45 static void __devinit at91_twi_hwinit(void)
47 unsigned long cdiv, ckdiv;
49 at91_twi_write(AT91_TWI_IDR, 0xffffffff); /* Disable all interrupts */
50 at91_twi_write(AT91_TWI_CR, AT91_TWI_SWRST); /* Reset peripheral */
51 at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN); /* Set Master mode */
53 /* Calcuate clock dividers */
54 cdiv = (clk_get_rate(twi_clk) / (2 * TWI_CLOCK)) - 3;
55 cdiv = cdiv + 1; /* round up */
56 ckdiv = 0;
57 while (cdiv > 255) {
58 ckdiv++;
59 cdiv = cdiv >> 1;
62 if (cpu_is_at91rm9200()) { /* AT91RM9200 Errata #22 */
63 if (ckdiv > 5) {
64 printk(KERN_ERR "AT91 I2C: Invalid TWI_CLOCK value!\n");
65 ckdiv = 5;
69 at91_twi_write(AT91_TWI_CWGR, (ckdiv << 16) | (cdiv << 8) | cdiv);
73 * Poll the i2c status register until the specified bit is set.
74 * Returns 0 if timed out (100 msec).
76 static short at91_poll_status(unsigned long bit)
78 int loop_cntr = 10000;
80 do {
81 udelay(10);
82 } while (!(at91_twi_read(AT91_TWI_SR) & bit) && (--loop_cntr > 0));
84 return (loop_cntr > 0);
87 static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length)
89 /* Send Start */
90 at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
92 /* Read data */
93 while (length--) {
94 if (!length) /* need to send Stop before reading last byte */
95 at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
96 if (!at91_poll_status(AT91_TWI_RXRDY)) {
97 dev_dbg(&adap->dev, "RXRDY timeout\n");
98 return -ETIMEDOUT;
100 *buf++ = (at91_twi_read(AT91_TWI_RHR) & 0xff);
103 return 0;
106 static int xfer_write(struct i2c_adapter *adap, unsigned char *buf, int length)
108 /* Load first byte into transmitter */
109 at91_twi_write(AT91_TWI_THR, *buf++);
111 /* Send Start */
112 at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
114 do {
115 if (!at91_poll_status(AT91_TWI_TXRDY)) {
116 dev_dbg(&adap->dev, "TXRDY timeout\n");
117 return -ETIMEDOUT;
120 length--; /* byte was transmitted */
122 if (length > 0) /* more data to send? */
123 at91_twi_write(AT91_TWI_THR, *buf++);
124 } while (length);
126 /* Send Stop */
127 at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
129 return 0;
133 * Generic i2c master transfer entrypoint.
135 * Note: We do not use Atmel's feature of storing the "internal device address".
136 * Instead the "internal device address" has to be written using a separate
137 * i2c message.
138 * http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html
140 static int at91_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num)
142 int i, ret;
144 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
146 for (i = 0; i < num; i++) {
147 dev_dbg(&adap->dev, " #%d: %sing %d byte%s %s 0x%02x\n", i,
148 pmsg->flags & I2C_M_RD ? "read" : "writ",
149 pmsg->len, pmsg->len > 1 ? "s" : "",
150 pmsg->flags & I2C_M_RD ? "from" : "to", pmsg->addr);
152 at91_twi_write(AT91_TWI_MMR, (pmsg->addr << 16)
153 | ((pmsg->flags & I2C_M_RD) ? AT91_TWI_MREAD : 0));
155 if (pmsg->len && pmsg->buf) { /* sanity check */
156 if (pmsg->flags & I2C_M_RD)
157 ret = xfer_read(adap, pmsg->buf, pmsg->len);
158 else
159 ret = xfer_write(adap, pmsg->buf, pmsg->len);
161 if (ret)
162 return ret;
164 /* Wait until transfer is finished */
165 if (!at91_poll_status(AT91_TWI_TXCOMP)) {
166 dev_dbg(&adap->dev, "TXCOMP timeout\n");
167 return -ETIMEDOUT;
170 dev_dbg(&adap->dev, "transfer complete\n");
171 pmsg++; /* next message */
173 return i;
177 * Return list of supported functionality.
179 static u32 at91_func(struct i2c_adapter *adapter)
181 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
184 static struct i2c_algorithm at91_algorithm = {
185 .master_xfer = at91_xfer,
186 .functionality = at91_func,
190 * Main initialization routine.
192 static int __devinit at91_i2c_probe(struct platform_device *pdev)
194 struct i2c_adapter *adapter;
195 struct resource *res;
196 int rc;
198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 if (!res)
200 return -ENXIO;
202 if (!request_mem_region(res->start, resource_size(res), "at91_i2c"))
203 return -EBUSY;
205 twi_base = ioremap(res->start, resource_size(res));
206 if (!twi_base) {
207 rc = -ENOMEM;
208 goto fail0;
211 twi_clk = clk_get(NULL, "twi_clk");
212 if (IS_ERR(twi_clk)) {
213 dev_err(&pdev->dev, "no clock defined\n");
214 rc = -ENODEV;
215 goto fail1;
218 adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
219 if (adapter == NULL) {
220 dev_err(&pdev->dev, "can't allocate inteface!\n");
221 rc = -ENOMEM;
222 goto fail2;
224 snprintf(adapter->name, sizeof(adapter->name), "AT91");
225 adapter->algo = &at91_algorithm;
226 adapter->class = I2C_CLASS_HWMON;
227 adapter->dev.parent = &pdev->dev;
228 /* adapter->id == 0 ... only one TWI controller for now */
230 platform_set_drvdata(pdev, adapter);
232 clk_enable(twi_clk); /* enable peripheral clock */
233 at91_twi_hwinit(); /* initialize TWI controller */
235 rc = i2c_add_numbered_adapter(adapter);
236 if (rc) {
237 dev_err(&pdev->dev, "Adapter %s registration failed\n",
238 adapter->name);
239 goto fail3;
242 dev_info(&pdev->dev, "AT91 i2c bus driver.\n");
243 return 0;
245 fail3:
246 platform_set_drvdata(pdev, NULL);
247 kfree(adapter);
248 clk_disable(twi_clk);
249 fail2:
250 clk_put(twi_clk);
251 fail1:
252 iounmap(twi_base);
253 fail0:
254 release_mem_region(res->start, resource_size(res));
256 return rc;
259 static int __devexit at91_i2c_remove(struct platform_device *pdev)
261 struct i2c_adapter *adapter = platform_get_drvdata(pdev);
262 struct resource *res;
263 int rc;
265 rc = i2c_del_adapter(adapter);
266 platform_set_drvdata(pdev, NULL);
268 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
269 iounmap(twi_base);
270 release_mem_region(res->start, resource_size(res));
272 clk_disable(twi_clk); /* disable peripheral clock */
273 clk_put(twi_clk);
275 return rc;
278 #ifdef CONFIG_PM
280 /* NOTE: could save a few mA by keeping clock off outside of at91_xfer... */
282 static int at91_i2c_suspend(struct platform_device *pdev, pm_message_t mesg)
284 clk_disable(twi_clk);
285 return 0;
288 static int at91_i2c_resume(struct platform_device *pdev)
290 return clk_enable(twi_clk);
293 #else
294 #define at91_i2c_suspend NULL
295 #define at91_i2c_resume NULL
296 #endif
298 static struct platform_driver at91_i2c_driver = {
299 .probe = at91_i2c_probe,
300 .remove = __devexit_p(at91_i2c_remove),
301 .suspend = at91_i2c_suspend,
302 .resume = at91_i2c_resume,
303 .driver = {
304 .name = "at91_i2c",
305 .owner = THIS_MODULE,
309 module_platform_driver(at91_i2c_driver);
311 MODULE_AUTHOR("Rick Bronson");
312 MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
313 MODULE_LICENSE("GPL");
314 MODULE_ALIAS("platform:at91_i2c");