1 --- a/drivers/i2c/busses/Kconfig
2 +++ b/drivers/i2c/busses/Kconfig
3 @@ -283,7 +283,7 @@ comment "I2C system bus drivers (mostly
6 tristate "Atmel AT91 I2C Two-Wire interface (TWI)"
7 - depends on ARCH_AT91 && EXPERIMENTAL && BROKEN
8 + depends on ARCH_AT91 && EXPERIMENTAL
10 This supports the use of the I2C interface on Atmel AT91
12 --- a/drivers/i2c/busses/i2c-at91.c
13 +++ b/drivers/i2c/busses/i2c-at91.c
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 + D. Gilbert [20100318 AT91SAM9G20]
20 + - Check for NACK, a NACK will abort current tranfser,
21 + returned as errno=EREMOTEIO unless I2C_M_IGNORE_NAK is set
22 + - Only supports 7 bit I2C device (slave) address
23 + - clockrate adjustable (module_param).
27 +/* Uncomment following line to see dev_dbg() output in logs */
28 +/* #define DEBUG 1 */
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/err.h>
34 #define TWI_CLOCK 100000 /* Hz. max 400 Kbits/sec */
37 +static unsigned int clockrate = TWI_CLOCK;
38 +static unsigned int prev_clockrate = TWI_CLOCK;
39 static struct clk *twi_clk;
40 static void __iomem *twi_base;
42 #define at91_twi_read(reg) __raw_readl(twi_base + (reg))
43 #define at91_twi_write(reg, val) __raw_writel((val), twi_base + (reg))
47 - * Initialize the TWI hardware registers.
48 + * Set TWI clock dividers based on clockrate (clock rate for SCL)
50 -static void __devinit at91_twi_hwinit(void)
51 +static void at91_twi_clock_dividers(void)
53 unsigned long cdiv, ckdiv;
55 - at91_twi_write(AT91_TWI_IDR, 0xffffffff); /* Disable all interrupts */
56 - at91_twi_write(AT91_TWI_CR, AT91_TWI_SWRST); /* Reset peripheral */
57 - at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN); /* Set Master mode */
58 + if (clockrate < 1000)
60 + else if (clockrate > 400000)
63 - /* Calcuate clock dividers */
64 - cdiv = (clk_get_rate(twi_clk) / (2 * TWI_CLOCK)) - 3;
65 + /* Calculate clock dividers */
66 + cdiv = (clk_get_rate(twi_clk) / (2 * clockrate)) - 3;
67 cdiv = cdiv + 1; /* round up */
70 @@ -61,41 +73,92 @@ static void __devinit at91_twi_hwinit(vo
72 if (cpu_is_at91rm9200()) { /* AT91RM9200 Errata #22 */
74 - printk(KERN_ERR "AT91 I2C: Invalid TWI_CLOCK value!\n");
75 + printk(KERN_ERR "i2c-at91: Invalid AT91RM9200 clock rate\n");
79 + /* AT91SAM9G20 has 3 bits for ckdiv so it cannot exceed 7 */
80 + if (cpu_is_at91sam9g20()) {
82 + printk(KERN_ERR "i2c-at91: Invalid AT91SAM9G20 clock "
83 + "rate, ckdiv=%lu\n", ckdiv);
88 at91_twi_write(AT91_TWI_CWGR, (ckdiv << 16) | (cdiv << 8) | cdiv);
89 + prev_clockrate = clockrate;
93 - * Poll the i2c status register until the specified bit is set.
94 - * Returns 0 if timed out (100 msec).
95 + * Initialize the TWI hardware registers.
97 -static short at91_poll_status(unsigned long bit)
98 +static void __devinit at91_twi_hwinit(void)
100 - int loop_cntr = 10000;
101 + at91_twi_write(AT91_TWI_IDR, 0xffffffff); /* Disable all interrupts */
102 + at91_twi_write(AT91_TWI_CR, AT91_TWI_SWRST); /* Reset peripheral */
103 + /* Set Master mode; Atmel suggests disabling slave mode */
104 + at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN | AT91_TWI_SVDIS);
106 + at91_twi_clock_dividers();
110 + * Poll the i2c status register until the specified bit is set or a NACK
111 + * occurs. Returns 0 if timed out (50 msec). If nack_seen_p is non-NULL
112 + * then write 0 to it first, then if the NACK bit is set in the status
113 + * register then write 1 to it and immediately return with a value of 1.
115 +static short at91_poll_status(unsigned long bit, int * nack_seen_p)
117 + int loop_cntr = 5000;
118 + unsigned long stat;
122 + if (clockrate <= 20000)
126 - } while (!(at91_twi_read(AT91_TWI_SR) & bit) && (--loop_cntr > 0));
127 + if (clockrate <= 20000)
129 + else if (clockrate <= 100000)
133 + stat = at91_twi_read(AT91_TWI_SR);
134 + if ((stat & AT91_TWI_NACK) && nack_seen_p) {
138 + } while (!(stat & bit) && (--loop_cntr > 0));
140 return (loop_cntr > 0);
143 static int xfer_read(struct i2c_adapter *adap, unsigned char *buf, int length)
149 - at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
151 + at91_twi_write(AT91_TWI_CR, AT91_TWI_START | AT91_TWI_STOP);
154 + at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
158 - if (!length) /* need to send Stop before reading last byte */
159 + /* send Stop before reading last byte (if not already done) */
160 + if ((0 == length) && (0 == sent_stop))
161 at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
162 - if (!at91_poll_status(AT91_TWI_RXRDY)) {
163 + if (!at91_poll_status(AT91_TWI_RXRDY, &nack_seen)) {
164 dev_dbg(&adap->dev, "RXRDY timeout\n");
166 + } else if (nack_seen) {
167 + dev_dbg(&adap->dev, "read NACKed\n");
168 + /* NACK supplies Stop */
171 *buf++ = (at91_twi_read(AT91_TWI_RHR) & 0xff);
173 @@ -105,16 +168,24 @@ static int xfer_read(struct i2c_adapter
175 static int xfer_write(struct i2c_adapter *adap, unsigned char *buf, int length)
179 /* Load first byte into transmitter */
180 at91_twi_write(AT91_TWI_THR, *buf++);
183 + /* Send Start [AT91SAM9G20 does not need this on write] */
184 at91_twi_write(AT91_TWI_CR, AT91_TWI_START);
187 - if (!at91_poll_status(AT91_TWI_TXRDY)) {
188 + if (!at91_poll_status(AT91_TWI_TXRDY, &nack_seen)) {
189 dev_dbg(&adap->dev, "TXRDY timeout\n");
190 + /* Set Master mode again */
191 + at91_twi_write(AT91_TWI_CR, AT91_TWI_MSEN);
193 + } else if (nack_seen) {
194 + dev_dbg(&adap->dev, "write NACKed\n");
195 + /* NACK supplies Stop */
199 length--; /* byte was transmitted */
200 @@ -123,7 +194,7 @@ static int xfer_write(struct i2c_adapter
201 at91_twi_write(AT91_TWI_THR, *buf++);
205 + /* Send Stop [AT91SAM9G20 does not need this on write] */
206 at91_twi_write(AT91_TWI_CR, AT91_TWI_STOP);
209 @@ -136,11 +207,19 @@ static int xfer_write(struct i2c_adapter
210 * Instead the "internal device address" has to be written using a separate
212 * http://lists.arm.linux.org.uk/pipermail/linux-arm-kernel/2004-September/024411.html
213 + * [dpg] By 2010 silicon bugs should be fixed, will need IADR for 10 bit device address
215 static int at91_xfer(struct i2c_adapter *adap, struct i2c_msg *pmsg, int num)
220 + if (prev_clockrate != clockrate) {
221 + dev_dbg(&adap->dev, "at91_xfer: prev_clockrate=%u "
222 + "clockrate=%u, change\n", prev_clockrate, clockrate);
223 + at91_twi_clock_dividers();
224 + msleep(1); /* let things settle */
226 dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
228 for (i = 0; i < num; i++) {
229 @@ -158,13 +237,23 @@ static int at91_xfer(struct i2c_adapter
231 ret = xfer_write(adap, pmsg->buf, pmsg->len);
237 + if ((I2C_M_IGNORE_NAK & pmsg->flags) &&
238 + (-EREMOTEIO == ret)) {
239 + dev_dbg(&adap->dev, "transfer "
240 + "NACKed, skip to next\n");
246 /* Wait until transfer is finished */
247 - if (!at91_poll_status(AT91_TWI_TXCOMP)) {
248 + if (!at91_poll_status(AT91_TWI_TXCOMP, &nack_seen)) {
249 dev_dbg(&adap->dev, "TXCOMP timeout\n");
251 + } else if (nack_seen) {
252 + dev_dbg(&adap->dev, "TXCOMP NACKed\n");
256 dev_dbg(&adap->dev, "transfer complete\n");
257 @@ -239,7 +328,8 @@ static int __devinit at91_i2c_probe(stru
261 - dev_info(&pdev->dev, "AT91 i2c bus driver.\n");
262 + dev_info(&pdev->dev, "AT91 TWI (I2C) bus driver [SCL %d Hz]\n",
267 @@ -295,6 +385,11 @@ static int at91_i2c_resume(struct platfo
268 #define at91_i2c_resume NULL
271 +/* I2C clock speed, in Hz 0-400kHz*/
272 +module_param(clockrate, uint, S_IRUGO | S_IWUSR);
273 +MODULE_PARM_DESC(clockrate,
274 + "SCL clock rate, 1000 to 400000 Hz (def: 100 kHz)");
276 /* work with "modprobe at91_i2c" from hotplugging or coldplugging */
277 MODULE_ALIAS("platform:at91_i2c");
279 @@ -323,5 +418,5 @@ module_init(at91_i2c_init);
280 module_exit(at91_i2c_exit);
282 MODULE_AUTHOR("Rick Bronson");
283 -MODULE_DESCRIPTION("I2C (TWI) driver for Atmel AT91");
284 +MODULE_DESCRIPTION("I2C (TWI) master driver for Atmel AT91");
285 MODULE_LICENSE("GPL");