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.
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>
41 #include <mach/pxa-regs.h>
45 wait_queue_head_t wait
;
50 unsigned int slave_addr
;
52 struct i2c_adapter adap
;
54 #ifdef CONFIG_I2C_PXA_SLAVE
55 struct i2c_slave_client
*slave
;
58 unsigned int irqlogidx
;
62 void __iomem
*reg_base
;
63 unsigned int reg_shift
;
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
90 #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
93 decode_bits(const char *prefix
, const struct bits
*bits
, int num
, u32 val
)
95 printk("%s %08x: ", prefix
, val
);
97 const char *str
= val
& bits
->mask
? bits
->set
: bits
->unset
;
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
);
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
);
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__)
162 #define show_state(i2c) do { } while (0)
163 #define decode_ISR(val) do { } while (0)
164 #define decode_ICR(val) do { } while (0)
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
)
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
]);
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
)
194 if (i2c_pxa_is_slavemode(i2c
)) {
195 dev_dbg(&i2c
->adap
.dev
, "%s: called in slave mode\n", __func__
);
199 while ((i
> 0) && (readl(_IBMR(i2c
)) & 0x1) == 0) {
200 unsigned long icr
= readl(_ICR(i2c
));
203 icr
|= ICR_ACKNAK
| ICR_STOP
| ICR_TB
;
205 writel(icr
, _ICR(i2c
));
213 writel(readl(_ICR(i2c
)) & ~(ICR_MA
| ICR_START
| ICR_STOP
),
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)
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
)) {
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
) {
246 dev_dbg(&i2c
->adap
.dev
, "%s: Slave detected\n", __func__
);
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
254 if ((readl(_ISR(i2c
)) & (ISR_UB
| ISR_IBB
)) == 0 && readl(_IBMR(i2c
)) == 3) {
256 dev_dbg(&i2c
->adap
.dev
, "%s: done\n", __func__
);
264 dev_dbg(&i2c
->adap
.dev
, "%s: did not free\n", __func__
);
269 static int i2c_pxa_set_master(struct pxa_i2c
*i2c
)
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__
);
282 writel(readl(_ICR(i2c
)) | ICR_SCLE
, _ICR(i2c
));
286 #ifdef CONFIG_I2C_PXA_SLAVE
287 static int i2c_pxa_wait_slave(struct pxa_i2c
*i2c
)
289 unsigned long timeout
= jiffies
+ HZ
*1;
295 while (time_before(jiffies
, timeout
)) {
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) {
304 dev_dbg(&i2c
->adap
.dev
, "%s: done\n", __func__
);
312 dev_dbg(&i2c
->adap
.dev
, "%s: did not free\n", __func__
);
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
)
325 udelay(100); /* simple delay */
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
) {
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",
342 writel(readl(_ICR(i2c
)) & ~(ICR_STOP
|ICR_ACKNAK
|ICR_MA
), _ICR(i2c
));
343 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
346 dev_dbg(&i2c
->adap
.dev
, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c
)), readl(_ISR(i2c
)));
347 decode_ICR(readl(_ICR(i2c
)));
351 #define i2c_pxa_set_slave(i2c, err) do { } while (0)
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 */
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
));
376 i2c_pxa_set_slave(i2c
, 0);
379 writel(readl(_ICR(i2c
)) | ICR_IUE
, _ICR(i2c
));
384 #ifdef CONFIG_I2C_PXA_SLAVE
389 static void i2c_pxa_slave_txempty(struct pxa_i2c
*i2c
, u32 isr
)
392 /* what should we do here? */
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
)
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
));
437 if ((readl(_IBMR(i2c
)) & 2) == 2)
443 dev_err(&i2c
->adap
.dev
, "timeout waiting for SCL high\n");
448 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
451 static void i2c_pxa_slave_stop(struct pxa_i2c
*i2c
)
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
);
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.
467 i2c_pxa_master_complete(i2c
, I2C_RETRY
);
470 static void i2c_pxa_slave_txempty(struct pxa_i2c
*i2c
, u32 isr
)
473 /* what should we do here? */
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
)
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
));
500 if ((readl(_IBMR(i2c
)) & 2) == 2)
506 dev_err(&i2c
->adap
.dev
, "timeout waiting for SCL high\n");
511 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
514 static void i2c_pxa_slave_stop(struct pxa_i2c
*i2c
)
517 i2c_pxa_master_complete(i2c
, I2C_RETRY
);
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
)
535 static inline void i2c_pxa_start_message(struct pxa_i2c
*i2c
)
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
)
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
)) {
578 dev_err(&i2c
->adap
.dev
,
579 "i2c_pxa: timeout waiting for bus free\n");
586 writel(readl(_ICR(i2c
)) | ICR_SCLE
, _ICR(i2c
));
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 */
597 ret
= i2c_pxa_pio_set_master(i2c
);
607 i2c_pxa_start_message(i2c
);
609 while (timeout
-- && i2c
->msg_num
> 0) {
610 i2c_pxa_handler(0, i2c
);
614 i2c_pxa_stop_message(i2c
);
617 * We place the return code in i2c->msg_idx.
623 i2c_pxa_scream_blue_murder(i2c
, "timeout");
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
)
637 * Wait for the bus to become free.
639 ret
= i2c_pxa_wait_bus_not_busy(i2c
);
641 dev_err(&i2c
->adap
.dev
, "i2c_pxa: timeout waiting for bus free\n");
648 ret
= i2c_pxa_set_master(i2c
);
650 dev_err(&i2c
->adap
.dev
, "i2c_pxa_set_master: error %d\n", ret
);
654 spin_lock_irq(&i2c
->lock
);
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.
678 i2c_pxa_scream_blue_murder(i2c
, "timeout");
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
;
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
))
697 for (i
= adap
->retries
; i
>= 0; i
--) {
698 ret
= i2c_pxa_do_pio_xfer(i2c
, msgs
, num
);
699 if (ret
!= I2C_RETRY
)
703 dev_dbg(&adap
->dev
, "Retrying transmission\n");
706 i2c_pxa_scream_blue_murder(i2c
, "exhausted retries");
709 i2c_pxa_set_slave(i2c
, 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
)
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
);
734 * If ISR_ALD is set, we lost arbitration.
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.
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)
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
789 if (i2c
->msg_ptr
== i2c
->msg
->len
&&
790 i2c
->msg_idx
== i2c
->msg_num
- 1)
792 } else if (i2c
->msg_idx
< i2c
->msg_num
- 1) {
794 * Next segment of the message.
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
)
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.
817 icr
|= ICR_START
| ICR_TB
;
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
827 i2c_pxa_master_complete(i2c
, 0);
830 i2c
->icrlog
[i2c
->irqlogidx
-1] = icr
;
832 writel(icr
, _ICR(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
);
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
;
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
)));
874 if (i2c
->irqlogidx
< ARRAY_SIZE(i2c
->isrlog
))
875 i2c
->isrlog
[i2c
->irqlogidx
++] = isr
;
880 * Always clear all pending IRQs.
882 writel(isr
& (ISR_SSD
|ISR_ALD
|ISR_ITE
|ISR_IRF
|ISR_SAD
|ISR_BED
), _ISR(i2c
));
885 i2c_pxa_slave_start(i2c
, isr
);
887 i2c_pxa_slave_stop(i2c
);
889 if (i2c_pxa_is_slavemode(i2c
)) {
891 i2c_pxa_slave_txempty(i2c
, isr
);
893 i2c_pxa_slave_rxfull(i2c
, isr
);
894 } else if (i2c
->msg
) {
896 i2c_pxa_irq_txempty(i2c
, isr
);
898 i2c_pxa_irq_rxfull(i2c
, isr
);
900 i2c_pxa_scream_blue_murder(i2c
, "spurious irq");
907 static int i2c_pxa_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
909 struct pxa_i2c
*i2c
= adap
->algo_data
;
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
))
918 for (i
= adap
->retries
; i
>= 0; i
--) {
919 ret
= i2c_pxa_do_xfer(i2c
, msgs
, num
);
920 if (ret
!= I2C_RETRY
)
924 dev_dbg(&adap
->dev
, "Retrying transmission\n");
927 i2c_pxa_scream_blue_murder(i2c
, "exhausted retries");
930 i2c_pxa_set_slave(i2c
, 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
)
953 struct resource
*res
;
954 struct i2c_pxa_platform_data
*plat
= dev
->dev
.platform_data
;
958 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
959 irq
= platform_get_irq(dev
, 0);
960 if (res
== NULL
|| irq
< 0)
963 if (!request_mem_region(res
->start
, res_len(res
), res
->name
))
966 i2c
= kzalloc(sizeof(struct pxa_i2c
), GFP_KERNEL
);
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",
987 i2c
->clk
= clk_get(&dev
->dev
, "I2CCLK");
988 if (IS_ERR(i2c
->clk
)) {
989 ret
= PTR_ERR(i2c
->clk
);
993 i2c
->reg_base
= ioremap(res
->start
, res_len(res
));
994 if (!i2c
->reg_base
) {
998 i2c
->reg_shift
= (cpu_is_pxa3xx() && (dev
->id
== 1)) ? 0 : 1;
1000 i2c
->iobase
= res
->start
;
1001 i2c
->iosize
= res_len(res
);
1005 i2c
->slave_addr
= I2C_PXA_SLAVE_ADDR
;
1007 #ifdef CONFIG_I2C_PXA_SLAVE
1009 i2c
->slave_addr
= plat
->slave_addr
;
1010 i2c
->slave
= plat
->slave
;
1014 clk_enable(i2c
->clk
);
1017 i2c
->adap
.class = plat
->class;
1018 i2c
->use_pio
= plat
->use_pio
;
1022 i2c
->adap
.algo
= &i2c_pxa_pio_algorithm
;
1024 i2c
->adap
.algo
= &i2c_pxa_algorithm
;
1025 ret
= request_irq(irq
, i2c_pxa_handler
, IRQF_DISABLED
,
1026 i2c
->adap
.name
, i2c
);
1033 i2c
->adap
.algo_data
= i2c
;
1034 i2c
->adap
.dev
.parent
= &dev
->dev
;
1036 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
1038 printk(KERN_INFO
"I2C: Failed to add bus\n");
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
);
1048 printk(KERN_INFO
"I2C: %s: PXA I2C adapter\n",
1049 i2c
->adap
.dev
.bus_id
);
1057 clk_disable(i2c
->clk
);
1058 iounmap(i2c
->reg_base
);
1064 release_mem_region(res
->start
, res_len(res
));
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
);
1076 free_irq(i2c
->irq
, i2c
);
1078 clk_disable(i2c
->clk
);
1081 iounmap(i2c
->reg_base
);
1082 release_mem_region(i2c
->iobase
, i2c
->iosize
);
1088 static struct platform_driver i2c_pxa_driver
= {
1089 .probe
= i2c_pxa_probe
,
1090 .remove
= __exit_p(i2c_pxa_remove
),
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
);