[ARM] 5203/1: i2c-pxa: fix scheduling while atomic in i2c_pxa_abort()
[linux-2.6/mini2440.git] / drivers / i2c / busses / i2c-pxa.c
blob57fbffd1ccc0def139b687621815fcc214fdfe08
1 /*
2 * i2c_adap_pxa.c
4 * I2C adapter for the PXA I2C bus access.
6 * Copyright (C) 2002 Intrinsyc Software Inc.
7 * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * History:
14 * Apr 2002: Initial version [CS]
15 * Jun 2002: Properly seperated algo/adap [FB]
16 * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
17 * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
18 * Sep 2004: Major rework to ensure efficient bus handling [RMK]
19 * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
20 * Feb 2005: Rework slave mode handling [RMK]
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-id.h>
26 #include <linux/init.h>
27 #include <linux/time.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2c-pxa.h>
33 #include <linux/platform_device.h>
34 #include <linux/err.h>
35 #include <linux/clk.h>
37 #include <mach/hardware.h>
38 #include <asm/irq.h>
39 #include <asm/io.h>
40 #include <mach/i2c.h>
41 #include <mach/pxa-regs.h>
43 struct pxa_i2c {
44 spinlock_t lock;
45 wait_queue_head_t wait;
46 struct i2c_msg *msg;
47 unsigned int msg_num;
48 unsigned int msg_idx;
49 unsigned int msg_ptr;
50 unsigned int slave_addr;
52 struct i2c_adapter adap;
53 struct clk *clk;
54 #ifdef CONFIG_I2C_PXA_SLAVE
55 struct i2c_slave_client *slave;
56 #endif
58 unsigned int irqlogidx;
59 u32 isrlog[32];
60 u32 icrlog[32];
62 void __iomem *reg_base;
63 unsigned int reg_shift;
65 unsigned long iobase;
66 unsigned long iosize;
68 int irq;
69 int use_pio;
72 #define _IBMR(i2c) ((i2c)->reg_base + (0x0 << (i2c)->reg_shift))
73 #define _IDBR(i2c) ((i2c)->reg_base + (0x4 << (i2c)->reg_shift))
74 #define _ICR(i2c) ((i2c)->reg_base + (0x8 << (i2c)->reg_shift))
75 #define _ISR(i2c) ((i2c)->reg_base + (0xc << (i2c)->reg_shift))
76 #define _ISAR(i2c) ((i2c)->reg_base + (0x10 << (i2c)->reg_shift))
79 * I2C Slave mode address
81 #define I2C_PXA_SLAVE_ADDR 0x1
83 #ifdef DEBUG
85 struct bits {
86 u32 mask;
87 const char *set;
88 const char *unset;
90 #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
92 static inline void
93 decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
95 printk("%s %08x: ", prefix, val);
96 while (num--) {
97 const char *str = val & bits->mask ? bits->set : bits->unset;
98 if (str)
99 printk("%s ", str);
100 bits++;
104 static const struct bits isr_bits[] = {
105 PXA_BIT(ISR_RWM, "RX", "TX"),
106 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
107 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
108 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
109 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
110 PXA_BIT(ISR_ALD, "ALD", NULL),
111 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
112 PXA_BIT(ISR_IRF, "RxFull", NULL),
113 PXA_BIT(ISR_GCAD, "GenCall", NULL),
114 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
115 PXA_BIT(ISR_BED, "BusErr", NULL),
118 static void decode_ISR(unsigned int val)
120 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
121 printk("\n");
124 static const struct bits icr_bits[] = {
125 PXA_BIT(ICR_START, "START", NULL),
126 PXA_BIT(ICR_STOP, "STOP", NULL),
127 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
128 PXA_BIT(ICR_TB, "TB", NULL),
129 PXA_BIT(ICR_MA, "MA", NULL),
130 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
131 PXA_BIT(ICR_IUE, "IUE", "iue"),
132 PXA_BIT(ICR_GCD, "GCD", NULL),
133 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
134 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
135 PXA_BIT(ICR_BEIE, "BEIE", NULL),
136 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
137 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
138 PXA_BIT(ICR_SADIE, "SADIE", NULL),
139 PXA_BIT(ICR_UR, "UR", "ur"),
142 #ifdef CONFIG_I2C_PXA_SLAVE
143 static void decode_ICR(unsigned int val)
145 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
146 printk("\n");
148 #endif
150 static unsigned int i2c_debug = DEBUG;
152 static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
154 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
155 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
158 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
159 #else
160 #define i2c_debug 0
162 #define show_state(i2c) do { } while (0)
163 #define decode_ISR(val) do { } while (0)
164 #define decode_ICR(val) do { } while (0)
165 #endif
167 #define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
169 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
170 static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
172 static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
174 unsigned int i;
175 printk("i2c: error: %s\n", why);
176 printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
177 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
178 printk("i2c: ICR: %08x ISR: %08x\n"
179 "i2c: log: ", readl(_ICR(i2c)), readl(_ISR(i2c)));
180 for (i = 0; i < i2c->irqlogidx; i++)
181 printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
182 printk("\n");
185 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
187 return !(readl(_ICR(i2c)) & ICR_SCLE);
190 static void i2c_pxa_abort(struct pxa_i2c *i2c)
192 int i = 250;
194 if (i2c_pxa_is_slavemode(i2c)) {
195 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
196 return;
199 while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
200 unsigned long icr = readl(_ICR(i2c));
202 icr &= ~ICR_START;
203 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
205 writel(icr, _ICR(i2c));
207 show_state(i2c);
209 mdelay(1);
210 i --;
213 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
214 _ICR(i2c));
217 static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
219 int timeout = DEF_TIMEOUT;
221 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
222 if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
223 timeout += 4;
225 msleep(2);
226 show_state(i2c);
229 if (timeout <= 0)
230 show_state(i2c);
232 return timeout <= 0 ? I2C_RETRY : 0;
235 static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
237 unsigned long timeout = jiffies + HZ*4;
239 while (time_before(jiffies, timeout)) {
240 if (i2c_debug > 1)
241 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
242 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
244 if (readl(_ISR(i2c)) & ISR_SAD) {
245 if (i2c_debug > 0)
246 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
247 goto out;
250 /* wait for unit and bus being not busy, and we also do a
251 * quick check of the i2c lines themselves to ensure they've
252 * gone high...
254 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
255 if (i2c_debug > 0)
256 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
257 return 1;
260 msleep(1);
263 if (i2c_debug > 0)
264 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
265 out:
266 return 0;
269 static int i2c_pxa_set_master(struct pxa_i2c *i2c)
271 if (i2c_debug)
272 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
274 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
275 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
276 if (!i2c_pxa_wait_master(i2c)) {
277 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
278 return I2C_RETRY;
282 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
283 return 0;
286 #ifdef CONFIG_I2C_PXA_SLAVE
287 static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
289 unsigned long timeout = jiffies + HZ*1;
291 /* wait for stop */
293 show_state(i2c);
295 while (time_before(jiffies, timeout)) {
296 if (i2c_debug > 1)
297 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
298 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
300 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
301 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
302 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
303 if (i2c_debug > 1)
304 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
305 return 1;
308 msleep(1);
311 if (i2c_debug > 0)
312 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
313 return 0;
317 * clear the hold on the bus, and take of anything else
318 * that has been configured
320 static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
322 show_state(i2c);
324 if (errcode < 0) {
325 udelay(100); /* simple delay */
326 } else {
327 /* we need to wait for the stop condition to end */
329 /* if we where in stop, then clear... */
330 if (readl(_ICR(i2c)) & ICR_STOP) {
331 udelay(100);
332 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
335 if (!i2c_pxa_wait_slave(i2c)) {
336 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
337 __func__);
338 return;
342 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
343 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
345 if (i2c_debug) {
346 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
347 decode_ICR(readl(_ICR(i2c)));
350 #else
351 #define i2c_pxa_set_slave(i2c, err) do { } while (0)
352 #endif
354 static void i2c_pxa_reset(struct pxa_i2c *i2c)
356 pr_debug("Resetting I2C Controller Unit\n");
358 /* abort any transfer currently under way */
359 i2c_pxa_abort(i2c);
361 /* reset according to 9.8 */
362 writel(ICR_UR, _ICR(i2c));
363 writel(I2C_ISR_INIT, _ISR(i2c));
364 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
366 writel(i2c->slave_addr, _ISAR(i2c));
368 /* set control register values */
369 writel(I2C_ICR_INIT, _ICR(i2c));
371 #ifdef CONFIG_I2C_PXA_SLAVE
372 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
373 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
374 #endif
376 i2c_pxa_set_slave(i2c, 0);
378 /* enable unit */
379 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
380 udelay(100);
384 #ifdef CONFIG_I2C_PXA_SLAVE
386 * PXA I2C Slave mode
389 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
391 if (isr & ISR_BED) {
392 /* what should we do here? */
393 } else {
394 int ret = 0;
396 if (i2c->slave != NULL)
397 ret = i2c->slave->read(i2c->slave->data);
399 writel(ret, _IDBR(i2c));
400 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
404 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
406 unsigned int byte = readl(_IDBR(i2c));
408 if (i2c->slave != NULL)
409 i2c->slave->write(i2c->slave->data, byte);
411 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
414 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
416 int timeout;
418 if (i2c_debug > 0)
419 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
420 (isr & ISR_RWM) ? 'r' : 't');
422 if (i2c->slave != NULL)
423 i2c->slave->event(i2c->slave->data,
424 (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
427 * slave could interrupt in the middle of us generating a
428 * start condition... if this happens, we'd better back off
429 * and stop holding the poor thing up
431 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
432 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
434 timeout = 0x10000;
436 while (1) {
437 if ((readl(_IBMR(i2c)) & 2) == 2)
438 break;
440 timeout--;
442 if (timeout <= 0) {
443 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
444 break;
448 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
451 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
453 if (i2c_debug > 2)
454 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
456 if (i2c->slave != NULL)
457 i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
459 if (i2c_debug > 2)
460 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
463 * If we have a master-mode message waiting,
464 * kick it off now that the slave has completed.
466 if (i2c->msg)
467 i2c_pxa_master_complete(i2c, I2C_RETRY);
469 #else
470 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
472 if (isr & ISR_BED) {
473 /* what should we do here? */
474 } else {
475 writel(0, _IDBR(i2c));
476 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
480 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
482 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
485 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
487 int timeout;
490 * slave could interrupt in the middle of us generating a
491 * start condition... if this happens, we'd better back off
492 * and stop holding the poor thing up
494 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
495 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
497 timeout = 0x10000;
499 while (1) {
500 if ((readl(_IBMR(i2c)) & 2) == 2)
501 break;
503 timeout--;
505 if (timeout <= 0) {
506 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
507 break;
511 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
514 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
516 if (i2c->msg)
517 i2c_pxa_master_complete(i2c, I2C_RETRY);
519 #endif
522 * PXA I2C Master mode
525 static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
527 unsigned int addr = (msg->addr & 0x7f) << 1;
529 if (msg->flags & I2C_M_RD)
530 addr |= 1;
532 return addr;
535 static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
537 u32 icr;
540 * Step 1: target slave address into IDBR
542 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
545 * Step 2: initiate the write.
547 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
548 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
551 static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
553 u32 icr;
556 * Clear the STOP and ACK flags
558 icr = readl(_ICR(i2c));
559 icr &= ~(ICR_STOP | ICR_ACKNAK);
560 writel(icr, _ICR(i2c));
563 static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
565 /* make timeout the same as for interrupt based functions */
566 long timeout = 2 * DEF_TIMEOUT;
569 * Wait for the bus to become free.
571 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
572 udelay(1000);
573 show_state(i2c);
576 if (timeout <= 0) {
577 show_state(i2c);
578 dev_err(&i2c->adap.dev,
579 "i2c_pxa: timeout waiting for bus free\n");
580 return I2C_RETRY;
584 * Set master mode.
586 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
588 return 0;
591 static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
592 struct i2c_msg *msg, int num)
594 unsigned long timeout = 500000; /* 5 seconds */
595 int ret = 0;
597 ret = i2c_pxa_pio_set_master(i2c);
598 if (ret)
599 goto out;
601 i2c->msg = msg;
602 i2c->msg_num = num;
603 i2c->msg_idx = 0;
604 i2c->msg_ptr = 0;
605 i2c->irqlogidx = 0;
607 i2c_pxa_start_message(i2c);
609 while (timeout-- && i2c->msg_num > 0) {
610 i2c_pxa_handler(0, i2c);
611 udelay(10);
614 i2c_pxa_stop_message(i2c);
617 * We place the return code in i2c->msg_idx.
619 ret = i2c->msg_idx;
621 out:
622 if (timeout == 0)
623 i2c_pxa_scream_blue_murder(i2c, "timeout");
625 return ret;
629 * We are protected by the adapter bus mutex.
631 static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
633 long timeout;
634 int ret;
637 * Wait for the bus to become free.
639 ret = i2c_pxa_wait_bus_not_busy(i2c);
640 if (ret) {
641 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
642 goto out;
646 * Set master mode.
648 ret = i2c_pxa_set_master(i2c);
649 if (ret) {
650 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
651 goto out;
654 spin_lock_irq(&i2c->lock);
656 i2c->msg = msg;
657 i2c->msg_num = num;
658 i2c->msg_idx = 0;
659 i2c->msg_ptr = 0;
660 i2c->irqlogidx = 0;
662 i2c_pxa_start_message(i2c);
664 spin_unlock_irq(&i2c->lock);
667 * The rest of the processing occurs in the interrupt handler.
669 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
670 i2c_pxa_stop_message(i2c);
673 * We place the return code in i2c->msg_idx.
675 ret = i2c->msg_idx;
677 if (timeout == 0)
678 i2c_pxa_scream_blue_murder(i2c, "timeout");
680 out:
681 return ret;
684 static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
685 struct i2c_msg msgs[], int num)
687 struct pxa_i2c *i2c = adap->algo_data;
688 int ret, i;
690 /* If the I2C controller is disabled we need to reset it
691 (probably due to a suspend/resume destroying state). We do
692 this here as we can then avoid worrying about resuming the
693 controller before its users. */
694 if (!(readl(_ICR(i2c)) & ICR_IUE))
695 i2c_pxa_reset(i2c);
697 for (i = adap->retries; i >= 0; i--) {
698 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
699 if (ret != I2C_RETRY)
700 goto out;
702 if (i2c_debug)
703 dev_dbg(&adap->dev, "Retrying transmission\n");
704 udelay(100);
706 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
707 ret = -EREMOTEIO;
708 out:
709 i2c_pxa_set_slave(i2c, ret);
710 return ret;
714 * i2c_pxa_master_complete - complete the message and wake up.
716 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
718 i2c->msg_ptr = 0;
719 i2c->msg = NULL;
720 i2c->msg_idx ++;
721 i2c->msg_num = 0;
722 if (ret)
723 i2c->msg_idx = ret;
724 if (!i2c->use_pio)
725 wake_up(&i2c->wait);
728 static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
730 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
732 again:
734 * If ISR_ALD is set, we lost arbitration.
736 if (isr & ISR_ALD) {
738 * Do we need to do anything here? The PXA docs
739 * are vague about what happens.
741 i2c_pxa_scream_blue_murder(i2c, "ALD set");
744 * We ignore this error. We seem to see spurious ALDs
745 * for seemingly no reason. If we handle them as I think
746 * they should, we end up causing an I2C error, which
747 * is painful for some systems.
749 return; /* ignore */
752 if (isr & ISR_BED) {
753 int ret = BUS_ERROR;
756 * I2C bus error - either the device NAK'd us, or
757 * something more serious happened. If we were NAK'd
758 * on the initial address phase, we can retry.
760 if (isr & ISR_ACKNAK) {
761 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
762 ret = I2C_RETRY;
763 else
764 ret = XFER_NAKED;
766 i2c_pxa_master_complete(i2c, ret);
767 } else if (isr & ISR_RWM) {
769 * Read mode. We have just sent the address byte, and
770 * now we must initiate the transfer.
772 if (i2c->msg_ptr == i2c->msg->len - 1 &&
773 i2c->msg_idx == i2c->msg_num - 1)
774 icr |= ICR_STOP | ICR_ACKNAK;
776 icr |= ICR_ALDIE | ICR_TB;
777 } else if (i2c->msg_ptr < i2c->msg->len) {
779 * Write mode. Write the next data byte.
781 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
783 icr |= ICR_ALDIE | ICR_TB;
786 * If this is the last byte of the last message, send
787 * a STOP.
789 if (i2c->msg_ptr == i2c->msg->len &&
790 i2c->msg_idx == i2c->msg_num - 1)
791 icr |= ICR_STOP;
792 } else if (i2c->msg_idx < i2c->msg_num - 1) {
794 * Next segment of the message.
796 i2c->msg_ptr = 0;
797 i2c->msg_idx ++;
798 i2c->msg++;
801 * If we aren't doing a repeated start and address,
802 * go back and try to send the next byte. Note that
803 * we do not support switching the R/W direction here.
805 if (i2c->msg->flags & I2C_M_NOSTART)
806 goto again;
809 * Write the next address.
811 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
814 * And trigger a repeated start, and send the byte.
816 icr &= ~ICR_ALDIE;
817 icr |= ICR_START | ICR_TB;
818 } else {
819 if (i2c->msg->len == 0) {
821 * Device probes have a message length of zero
822 * and need the bus to be reset before it can
823 * be used again.
825 i2c_pxa_reset(i2c);
827 i2c_pxa_master_complete(i2c, 0);
830 i2c->icrlog[i2c->irqlogidx-1] = icr;
832 writel(icr, _ICR(i2c));
833 show_state(i2c);
836 static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
838 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
841 * Read the byte.
843 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
845 if (i2c->msg_ptr < i2c->msg->len) {
847 * If this is the last byte of the last
848 * message, send a STOP.
850 if (i2c->msg_ptr == i2c->msg->len - 1)
851 icr |= ICR_STOP | ICR_ACKNAK;
853 icr |= ICR_ALDIE | ICR_TB;
854 } else {
855 i2c_pxa_master_complete(i2c, 0);
858 i2c->icrlog[i2c->irqlogidx-1] = icr;
860 writel(icr, _ICR(i2c));
863 static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
865 struct pxa_i2c *i2c = dev_id;
866 u32 isr = readl(_ISR(i2c));
868 if (i2c_debug > 2 && 0) {
869 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
870 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
871 decode_ISR(isr);
874 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
875 i2c->isrlog[i2c->irqlogidx++] = isr;
877 show_state(i2c);
880 * Always clear all pending IRQs.
882 writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
884 if (isr & ISR_SAD)
885 i2c_pxa_slave_start(i2c, isr);
886 if (isr & ISR_SSD)
887 i2c_pxa_slave_stop(i2c);
889 if (i2c_pxa_is_slavemode(i2c)) {
890 if (isr & ISR_ITE)
891 i2c_pxa_slave_txempty(i2c, isr);
892 if (isr & ISR_IRF)
893 i2c_pxa_slave_rxfull(i2c, isr);
894 } else if (i2c->msg) {
895 if (isr & ISR_ITE)
896 i2c_pxa_irq_txempty(i2c, isr);
897 if (isr & ISR_IRF)
898 i2c_pxa_irq_rxfull(i2c, isr);
899 } else {
900 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
903 return IRQ_HANDLED;
907 static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
909 struct pxa_i2c *i2c = adap->algo_data;
910 int ret, i;
912 /* If the I2C controller is disabled we need to reset it (probably due
913 to a suspend/resume destroying state). We do this here as we can then
914 avoid worrying about resuming the controller before its users. */
915 if (!(readl(_ICR(i2c)) & ICR_IUE))
916 i2c_pxa_reset(i2c);
918 for (i = adap->retries; i >= 0; i--) {
919 ret = i2c_pxa_do_xfer(i2c, msgs, num);
920 if (ret != I2C_RETRY)
921 goto out;
923 if (i2c_debug)
924 dev_dbg(&adap->dev, "Retrying transmission\n");
925 udelay(100);
927 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
928 ret = -EREMOTEIO;
929 out:
930 i2c_pxa_set_slave(i2c, ret);
931 return ret;
934 static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
936 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
939 static const struct i2c_algorithm i2c_pxa_algorithm = {
940 .master_xfer = i2c_pxa_xfer,
941 .functionality = i2c_pxa_functionality,
944 static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
945 .master_xfer = i2c_pxa_pio_xfer,
946 .functionality = i2c_pxa_functionality,
949 #define res_len(r) ((r)->end - (r)->start + 1)
950 static int i2c_pxa_probe(struct platform_device *dev)
952 struct pxa_i2c *i2c;
953 struct resource *res;
954 struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
955 int ret;
956 int irq;
958 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
959 irq = platform_get_irq(dev, 0);
960 if (res == NULL || irq < 0)
961 return -ENODEV;
963 if (!request_mem_region(res->start, res_len(res), res->name))
964 return -ENOMEM;
966 i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
967 if (!i2c) {
968 ret = -ENOMEM;
969 goto emalloc;
972 i2c->adap.owner = THIS_MODULE;
973 i2c->adap.retries = 5;
975 spin_lock_init(&i2c->lock);
976 init_waitqueue_head(&i2c->wait);
979 * If "dev->id" is negative we consider it as zero.
980 * The reason to do so is to avoid sysfs names that only make
981 * sense when there are multiple adapters.
983 i2c->adap.nr = dev->id != -1 ? dev->id : 0;
984 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
985 i2c->adap.nr);
987 i2c->clk = clk_get(&dev->dev, "I2CCLK");
988 if (IS_ERR(i2c->clk)) {
989 ret = PTR_ERR(i2c->clk);
990 goto eclk;
993 i2c->reg_base = ioremap(res->start, res_len(res));
994 if (!i2c->reg_base) {
995 ret = -EIO;
996 goto eremap;
998 i2c->reg_shift = (cpu_is_pxa3xx() && (dev->id == 1)) ? 0 : 1;
1000 i2c->iobase = res->start;
1001 i2c->iosize = res_len(res);
1003 i2c->irq = irq;
1005 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
1007 #ifdef CONFIG_I2C_PXA_SLAVE
1008 if (plat) {
1009 i2c->slave_addr = plat->slave_addr;
1010 i2c->slave = plat->slave;
1012 #endif
1014 clk_enable(i2c->clk);
1016 if (plat) {
1017 i2c->adap.class = plat->class;
1018 i2c->use_pio = plat->use_pio;
1021 if (i2c->use_pio) {
1022 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1023 } else {
1024 i2c->adap.algo = &i2c_pxa_algorithm;
1025 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
1026 i2c->adap.name, i2c);
1027 if (ret)
1028 goto ereqirq;
1031 i2c_pxa_reset(i2c);
1033 i2c->adap.algo_data = i2c;
1034 i2c->adap.dev.parent = &dev->dev;
1036 ret = i2c_add_numbered_adapter(&i2c->adap);
1037 if (ret < 0) {
1038 printk(KERN_INFO "I2C: Failed to add bus\n");
1039 goto eadapt;
1042 platform_set_drvdata(dev, i2c);
1044 #ifdef CONFIG_I2C_PXA_SLAVE
1045 printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
1046 i2c->adap.dev.bus_id, i2c->slave_addr);
1047 #else
1048 printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
1049 i2c->adap.dev.bus_id);
1050 #endif
1051 return 0;
1053 eadapt:
1054 if (!i2c->use_pio)
1055 free_irq(irq, i2c);
1056 ereqirq:
1057 clk_disable(i2c->clk);
1058 iounmap(i2c->reg_base);
1059 eremap:
1060 clk_put(i2c->clk);
1061 eclk:
1062 kfree(i2c);
1063 emalloc:
1064 release_mem_region(res->start, res_len(res));
1065 return ret;
1068 static int __exit i2c_pxa_remove(struct platform_device *dev)
1070 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1072 platform_set_drvdata(dev, NULL);
1074 i2c_del_adapter(&i2c->adap);
1075 if (!i2c->use_pio)
1076 free_irq(i2c->irq, i2c);
1078 clk_disable(i2c->clk);
1079 clk_put(i2c->clk);
1081 iounmap(i2c->reg_base);
1082 release_mem_region(i2c->iobase, i2c->iosize);
1083 kfree(i2c);
1085 return 0;
1088 static struct platform_driver i2c_pxa_driver = {
1089 .probe = i2c_pxa_probe,
1090 .remove = __exit_p(i2c_pxa_remove),
1091 .driver = {
1092 .name = "pxa2xx-i2c",
1093 .owner = THIS_MODULE,
1097 static int __init i2c_adap_pxa_init(void)
1099 return platform_driver_register(&i2c_pxa_driver);
1102 static void __exit i2c_adap_pxa_exit(void)
1104 platform_driver_unregister(&i2c_pxa_driver);
1107 MODULE_LICENSE("GPL");
1108 MODULE_ALIAS("platform:pxa2xx-i2c");
1110 subsys_initcall(i2c_adap_pxa_init);
1111 module_exit(i2c_adap_pxa_exit);