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 <asm/hardware.h>
40 #include <asm/arch/i2c.h>
41 #include <asm/arch/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
;
71 #define _IBMR(i2c) ((i2c)->reg_base + 0)
72 #define _IDBR(i2c) ((i2c)->reg_base + 8)
73 #define _ICR(i2c) ((i2c)->reg_base + 0x10)
74 #define _ISR(i2c) ((i2c)->reg_base + 0x18)
75 #define _ISAR(i2c) ((i2c)->reg_base + 0x20)
78 * I2C Slave mode address
80 #define I2C_PXA_SLAVE_ADDR 0x1
89 #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
92 decode_bits(const char *prefix
, const struct bits
*bits
, int num
, u32 val
)
94 printk("%s %08x: ", prefix
, val
);
96 const char *str
= val
& bits
->mask
? bits
->set
: bits
->unset
;
103 static const struct bits isr_bits
[] = {
104 PXA_BIT(ISR_RWM
, "RX", "TX"),
105 PXA_BIT(ISR_ACKNAK
, "NAK", "ACK"),
106 PXA_BIT(ISR_UB
, "Bsy", "Rdy"),
107 PXA_BIT(ISR_IBB
, "BusBsy", "BusRdy"),
108 PXA_BIT(ISR_SSD
, "SlaveStop", NULL
),
109 PXA_BIT(ISR_ALD
, "ALD", NULL
),
110 PXA_BIT(ISR_ITE
, "TxEmpty", NULL
),
111 PXA_BIT(ISR_IRF
, "RxFull", NULL
),
112 PXA_BIT(ISR_GCAD
, "GenCall", NULL
),
113 PXA_BIT(ISR_SAD
, "SlaveAddr", NULL
),
114 PXA_BIT(ISR_BED
, "BusErr", NULL
),
117 static void decode_ISR(unsigned int val
)
119 decode_bits(KERN_DEBUG
"ISR", isr_bits
, ARRAY_SIZE(isr_bits
), val
);
123 static const struct bits icr_bits
[] = {
124 PXA_BIT(ICR_START
, "START", NULL
),
125 PXA_BIT(ICR_STOP
, "STOP", NULL
),
126 PXA_BIT(ICR_ACKNAK
, "ACKNAK", NULL
),
127 PXA_BIT(ICR_TB
, "TB", NULL
),
128 PXA_BIT(ICR_MA
, "MA", NULL
),
129 PXA_BIT(ICR_SCLE
, "SCLE", "scle"),
130 PXA_BIT(ICR_IUE
, "IUE", "iue"),
131 PXA_BIT(ICR_GCD
, "GCD", NULL
),
132 PXA_BIT(ICR_ITEIE
, "ITEIE", NULL
),
133 PXA_BIT(ICR_IRFIE
, "IRFIE", NULL
),
134 PXA_BIT(ICR_BEIE
, "BEIE", NULL
),
135 PXA_BIT(ICR_SSDIE
, "SSDIE", NULL
),
136 PXA_BIT(ICR_ALDIE
, "ALDIE", NULL
),
137 PXA_BIT(ICR_SADIE
, "SADIE", NULL
),
138 PXA_BIT(ICR_UR
, "UR", "ur"),
141 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
143 #ifdef CONFIG_I2C_PXA_SLAVE
144 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
145 static void decode_ICR(unsigned int val
)
147 decode_bits(KERN_DEBUG
"ICR", icr_bits
, ARRAY_SIZE(icr_bits
), val
);
150 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
153 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
155 static unsigned int i2c_debug
= DEBUG
;
157 static void i2c_pxa_show_state(struct pxa_i2c
*i2c
, int lno
, const char *fname
)
159 dev_dbg(&i2c
->adap
.dev
, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname
, lno
,
160 readl(_ISR(i2c
)), readl(_ICR(i2c
)), readl(_IBMR(i2c
)));
163 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)
167 #define show_state(i2c) do { } while (0)
168 #define decode_ISR(val) do { } while (0)
169 #define decode_ICR(val) do { } while (0)
172 #define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
174 static void i2c_pxa_master_complete(struct pxa_i2c
*i2c
, int ret
);
175 static irqreturn_t
i2c_pxa_handler(int this_irq
, void *dev_id
);
177 static void i2c_pxa_scream_blue_murder(struct pxa_i2c
*i2c
, const char *why
)
180 printk("i2c: error: %s\n", why
);
181 printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
182 i2c
->msg_num
, i2c
->msg_idx
, i2c
->msg_ptr
);
183 printk("i2c: ICR: %08x ISR: %08x\n"
184 "i2c: log: ", readl(_ICR(i2c
)), readl(_ISR(i2c
)));
185 for (i
= 0; i
< i2c
->irqlogidx
; i
++)
186 printk("[%08x:%08x] ", i2c
->isrlog
[i
], i2c
->icrlog
[i
]);
190 static inline int i2c_pxa_is_slavemode(struct pxa_i2c
*i2c
)
192 return !(readl(_ICR(i2c
)) & ICR_SCLE
);
195 static void i2c_pxa_abort(struct pxa_i2c
*i2c
)
197 unsigned long timeout
= jiffies
+ HZ
/4;
199 if (i2c_pxa_is_slavemode(i2c
)) {
200 dev_dbg(&i2c
->adap
.dev
, "%s: called in slave mode\n", __func__
);
204 while (time_before(jiffies
, timeout
) && (readl(_IBMR(i2c
)) & 0x1) == 0) {
205 unsigned long icr
= readl(_ICR(i2c
));
208 icr
|= ICR_ACKNAK
| ICR_STOP
| ICR_TB
;
210 writel(icr
, _ICR(i2c
));
217 writel(readl(_ICR(i2c
)) & ~(ICR_MA
| ICR_START
| ICR_STOP
),
221 static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c
*i2c
)
223 int timeout
= DEF_TIMEOUT
;
225 while (timeout
-- && readl(_ISR(i2c
)) & (ISR_IBB
| ISR_UB
)) {
226 if ((readl(_ISR(i2c
)) & ISR_SAD
) != 0)
236 return timeout
<= 0 ? I2C_RETRY
: 0;
239 static int i2c_pxa_wait_master(struct pxa_i2c
*i2c
)
241 unsigned long timeout
= jiffies
+ HZ
*4;
243 while (time_before(jiffies
, timeout
)) {
245 dev_dbg(&i2c
->adap
.dev
, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
246 __func__
, (long)jiffies
, readl(_ISR(i2c
)), readl(_ICR(i2c
)), readl(_IBMR(i2c
)));
248 if (readl(_ISR(i2c
)) & ISR_SAD
) {
250 dev_dbg(&i2c
->adap
.dev
, "%s: Slave detected\n", __func__
);
254 /* wait for unit and bus being not busy, and we also do a
255 * quick check of the i2c lines themselves to ensure they've
258 if ((readl(_ISR(i2c
)) & (ISR_UB
| ISR_IBB
)) == 0 && readl(_IBMR(i2c
)) == 3) {
260 dev_dbg(&i2c
->adap
.dev
, "%s: done\n", __func__
);
268 dev_dbg(&i2c
->adap
.dev
, "%s: did not free\n", __func__
);
273 static int i2c_pxa_set_master(struct pxa_i2c
*i2c
)
276 dev_dbg(&i2c
->adap
.dev
, "setting to bus master\n");
278 if ((readl(_ISR(i2c
)) & (ISR_UB
| ISR_IBB
)) != 0) {
279 dev_dbg(&i2c
->adap
.dev
, "%s: unit is busy\n", __func__
);
280 if (!i2c_pxa_wait_master(i2c
)) {
281 dev_dbg(&i2c
->adap
.dev
, "%s: error: unit busy\n", __func__
);
286 writel(readl(_ICR(i2c
)) | ICR_SCLE
, _ICR(i2c
));
290 #ifdef CONFIG_I2C_PXA_SLAVE
291 static int i2c_pxa_wait_slave(struct pxa_i2c
*i2c
)
293 unsigned long timeout
= jiffies
+ HZ
*1;
299 while (time_before(jiffies
, timeout
)) {
301 dev_dbg(&i2c
->adap
.dev
, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
302 __func__
, (long)jiffies
, readl(_ISR(i2c
)), readl(_ICR(i2c
)), readl(_IBMR(i2c
)));
304 if ((readl(_ISR(i2c
)) & (ISR_UB
|ISR_IBB
)) == 0 ||
305 (readl(_ISR(i2c
)) & ISR_SAD
) != 0 ||
306 (readl(_ICR(i2c
)) & ICR_SCLE
) == 0) {
308 dev_dbg(&i2c
->adap
.dev
, "%s: done\n", __func__
);
316 dev_dbg(&i2c
->adap
.dev
, "%s: did not free\n", __func__
);
321 * clear the hold on the bus, and take of anything else
322 * that has been configured
324 static void i2c_pxa_set_slave(struct pxa_i2c
*i2c
, int errcode
)
329 udelay(100); /* simple delay */
331 /* we need to wait for the stop condition to end */
333 /* if we where in stop, then clear... */
334 if (readl(_ICR(i2c
)) & ICR_STOP
) {
336 writel(readl(_ICR(i2c
)) & ~ICR_STOP
, _ICR(i2c
));
339 if (!i2c_pxa_wait_slave(i2c
)) {
340 dev_err(&i2c
->adap
.dev
, "%s: wait timedout\n",
346 writel(readl(_ICR(i2c
)) & ~(ICR_STOP
|ICR_ACKNAK
|ICR_MA
), _ICR(i2c
));
347 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
350 dev_dbg(&i2c
->adap
.dev
, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c
)), readl(_ISR(i2c
)));
351 decode_ICR(readl(_ICR(i2c
)));
355 #define i2c_pxa_set_slave(i2c, err) do { } while (0)
358 static void i2c_pxa_reset(struct pxa_i2c
*i2c
)
360 pr_debug("Resetting I2C Controller Unit\n");
362 /* abort any transfer currently under way */
365 /* reset according to 9.8 */
366 writel(ICR_UR
, _ICR(i2c
));
367 writel(I2C_ISR_INIT
, _ISR(i2c
));
368 writel(readl(_ICR(i2c
)) & ~ICR_UR
, _ICR(i2c
));
370 writel(i2c
->slave_addr
, _ISAR(i2c
));
372 /* set control register values */
373 writel(I2C_ICR_INIT
, _ICR(i2c
));
375 #ifdef CONFIG_I2C_PXA_SLAVE
376 dev_info(&i2c
->adap
.dev
, "Enabling slave mode\n");
377 writel(readl(_ICR(i2c
)) | ICR_SADIE
| ICR_ALDIE
| ICR_SSDIE
, _ICR(i2c
));
380 i2c_pxa_set_slave(i2c
, 0);
383 writel(readl(_ICR(i2c
)) | ICR_IUE
, _ICR(i2c
));
388 #ifdef CONFIG_I2C_PXA_SLAVE
393 static void i2c_pxa_slave_txempty(struct pxa_i2c
*i2c
, u32 isr
)
396 /* what should we do here? */
400 if (i2c
->slave
!= NULL
)
401 ret
= i2c
->slave
->read(i2c
->slave
->data
);
403 writel(ret
, _IDBR(i2c
));
404 writel(readl(_ICR(i2c
)) | ICR_TB
, _ICR(i2c
)); /* allow next byte */
408 static void i2c_pxa_slave_rxfull(struct pxa_i2c
*i2c
, u32 isr
)
410 unsigned int byte
= readl(_IDBR(i2c
));
412 if (i2c
->slave
!= NULL
)
413 i2c
->slave
->write(i2c
->slave
->data
, byte
);
415 writel(readl(_ICR(i2c
)) | ICR_TB
, _ICR(i2c
));
418 static void i2c_pxa_slave_start(struct pxa_i2c
*i2c
, u32 isr
)
423 dev_dbg(&i2c
->adap
.dev
, "SAD, mode is slave-%cx\n",
424 (isr
& ISR_RWM
) ? 'r' : 't');
426 if (i2c
->slave
!= NULL
)
427 i2c
->slave
->event(i2c
->slave
->data
,
428 (isr
& ISR_RWM
) ? I2C_SLAVE_EVENT_START_READ
: I2C_SLAVE_EVENT_START_WRITE
);
431 * slave could interrupt in the middle of us generating a
432 * start condition... if this happens, we'd better back off
433 * and stop holding the poor thing up
435 writel(readl(_ICR(i2c
)) & ~(ICR_START
|ICR_STOP
), _ICR(i2c
));
436 writel(readl(_ICR(i2c
)) | ICR_TB
, _ICR(i2c
));
441 if ((readl(_IBMR(i2c
)) & 2) == 2)
447 dev_err(&i2c
->adap
.dev
, "timeout waiting for SCL high\n");
452 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
455 static void i2c_pxa_slave_stop(struct pxa_i2c
*i2c
)
458 dev_dbg(&i2c
->adap
.dev
, "ISR: SSD (Slave Stop)\n");
460 if (i2c
->slave
!= NULL
)
461 i2c
->slave
->event(i2c
->slave
->data
, I2C_SLAVE_EVENT_STOP
);
464 dev_dbg(&i2c
->adap
.dev
, "ISR: SSD (Slave Stop) acked\n");
467 * If we have a master-mode message waiting,
468 * kick it off now that the slave has completed.
471 i2c_pxa_master_complete(i2c
, I2C_RETRY
);
474 static void i2c_pxa_slave_txempty(struct pxa_i2c
*i2c
, u32 isr
)
477 /* what should we do here? */
479 writel(0, _IDBR(i2c
));
480 writel(readl(_ICR(i2c
)) | ICR_TB
, _ICR(i2c
));
484 static void i2c_pxa_slave_rxfull(struct pxa_i2c
*i2c
, u32 isr
)
486 writel(readl(_ICR(i2c
)) | ICR_TB
| ICR_ACKNAK
, _ICR(i2c
));
489 static void i2c_pxa_slave_start(struct pxa_i2c
*i2c
, u32 isr
)
494 * slave could interrupt in the middle of us generating a
495 * start condition... if this happens, we'd better back off
496 * and stop holding the poor thing up
498 writel(readl(_ICR(i2c
)) & ~(ICR_START
|ICR_STOP
), _ICR(i2c
));
499 writel(readl(_ICR(i2c
)) | ICR_TB
| ICR_ACKNAK
, _ICR(i2c
));
504 if ((readl(_IBMR(i2c
)) & 2) == 2)
510 dev_err(&i2c
->adap
.dev
, "timeout waiting for SCL high\n");
515 writel(readl(_ICR(i2c
)) & ~ICR_SCLE
, _ICR(i2c
));
518 static void i2c_pxa_slave_stop(struct pxa_i2c
*i2c
)
521 i2c_pxa_master_complete(i2c
, I2C_RETRY
);
526 * PXA I2C Master mode
529 static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg
*msg
)
531 unsigned int addr
= (msg
->addr
& 0x7f) << 1;
533 if (msg
->flags
& I2C_M_RD
)
539 static inline void i2c_pxa_start_message(struct pxa_i2c
*i2c
)
544 * Step 1: target slave address into IDBR
546 writel(i2c_pxa_addr_byte(i2c
->msg
), _IDBR(i2c
));
549 * Step 2: initiate the write.
551 icr
= readl(_ICR(i2c
)) & ~(ICR_STOP
| ICR_ALDIE
);
552 writel(icr
| ICR_START
| ICR_TB
, _ICR(i2c
));
555 static inline void i2c_pxa_stop_message(struct pxa_i2c
*i2c
)
560 * Clear the STOP and ACK flags
562 icr
= readl(_ICR(i2c
));
563 icr
&= ~(ICR_STOP
| ICR_ACKNAK
);
564 writel(icr
, _ICR(i2c
));
567 static int i2c_pxa_pio_set_master(struct pxa_i2c
*i2c
)
569 /* make timeout the same as for interrupt based functions */
570 long timeout
= 2 * DEF_TIMEOUT
;
573 * Wait for the bus to become free.
575 while (timeout
-- && readl(_ISR(i2c
)) & (ISR_IBB
| ISR_UB
)) {
582 dev_err(&i2c
->adap
.dev
,
583 "i2c_pxa: timeout waiting for bus free\n");
590 writel(readl(_ICR(i2c
)) | ICR_SCLE
, _ICR(i2c
));
595 static int i2c_pxa_do_pio_xfer(struct pxa_i2c
*i2c
,
596 struct i2c_msg
*msg
, int num
)
598 unsigned long timeout
= 500000; /* 5 seconds */
601 ret
= i2c_pxa_pio_set_master(i2c
);
611 i2c_pxa_start_message(i2c
);
613 while (timeout
-- && i2c
->msg_num
> 0) {
614 i2c_pxa_handler(0, i2c
);
618 i2c_pxa_stop_message(i2c
);
621 * We place the return code in i2c->msg_idx.
627 i2c_pxa_scream_blue_murder(i2c
, "timeout");
633 * We are protected by the adapter bus mutex.
635 static int i2c_pxa_do_xfer(struct pxa_i2c
*i2c
, struct i2c_msg
*msg
, int num
)
641 * Wait for the bus to become free.
643 ret
= i2c_pxa_wait_bus_not_busy(i2c
);
645 dev_err(&i2c
->adap
.dev
, "i2c_pxa: timeout waiting for bus free\n");
652 ret
= i2c_pxa_set_master(i2c
);
654 dev_err(&i2c
->adap
.dev
, "i2c_pxa_set_master: error %d\n", ret
);
658 spin_lock_irq(&i2c
->lock
);
666 i2c_pxa_start_message(i2c
);
668 spin_unlock_irq(&i2c
->lock
);
671 * The rest of the processing occurs in the interrupt handler.
673 timeout
= wait_event_timeout(i2c
->wait
, i2c
->msg_num
== 0, HZ
* 5);
674 i2c_pxa_stop_message(i2c
);
677 * We place the return code in i2c->msg_idx.
682 i2c_pxa_scream_blue_murder(i2c
, "timeout");
688 static int i2c_pxa_pio_xfer(struct i2c_adapter
*adap
,
689 struct i2c_msg msgs
[], int num
)
691 struct pxa_i2c
*i2c
= adap
->algo_data
;
694 /* If the I2C controller is disabled we need to reset it
695 (probably due to a suspend/resume destroying state). We do
696 this here as we can then avoid worrying about resuming the
697 controller before its users. */
698 if (!(readl(_ICR(i2c
)) & ICR_IUE
))
701 for (i
= adap
->retries
; i
>= 0; i
--) {
702 ret
= i2c_pxa_do_pio_xfer(i2c
, msgs
, num
);
703 if (ret
!= I2C_RETRY
)
707 dev_dbg(&adap
->dev
, "Retrying transmission\n");
710 i2c_pxa_scream_blue_murder(i2c
, "exhausted retries");
713 i2c_pxa_set_slave(i2c
, ret
);
718 * i2c_pxa_master_complete - complete the message and wake up.
720 static void i2c_pxa_master_complete(struct pxa_i2c
*i2c
, int ret
)
732 static void i2c_pxa_irq_txempty(struct pxa_i2c
*i2c
, u32 isr
)
734 u32 icr
= readl(_ICR(i2c
)) & ~(ICR_START
|ICR_STOP
|ICR_ACKNAK
|ICR_TB
);
738 * If ISR_ALD is set, we lost arbitration.
742 * Do we need to do anything here? The PXA docs
743 * are vague about what happens.
745 i2c_pxa_scream_blue_murder(i2c
, "ALD set");
748 * We ignore this error. We seem to see spurious ALDs
749 * for seemingly no reason. If we handle them as I think
750 * they should, we end up causing an I2C error, which
751 * is painful for some systems.
760 * I2C bus error - either the device NAK'd us, or
761 * something more serious happened. If we were NAK'd
762 * on the initial address phase, we can retry.
764 if (isr
& ISR_ACKNAK
) {
765 if (i2c
->msg_ptr
== 0 && i2c
->msg_idx
== 0)
770 i2c_pxa_master_complete(i2c
, ret
);
771 } else if (isr
& ISR_RWM
) {
773 * Read mode. We have just sent the address byte, and
774 * now we must initiate the transfer.
776 if (i2c
->msg_ptr
== i2c
->msg
->len
- 1 &&
777 i2c
->msg_idx
== i2c
->msg_num
- 1)
778 icr
|= ICR_STOP
| ICR_ACKNAK
;
780 icr
|= ICR_ALDIE
| ICR_TB
;
781 } else if (i2c
->msg_ptr
< i2c
->msg
->len
) {
783 * Write mode. Write the next data byte.
785 writel(i2c
->msg
->buf
[i2c
->msg_ptr
++], _IDBR(i2c
));
787 icr
|= ICR_ALDIE
| ICR_TB
;
790 * If this is the last byte of the last message, send
793 if (i2c
->msg_ptr
== i2c
->msg
->len
&&
794 i2c
->msg_idx
== i2c
->msg_num
- 1)
796 } else if (i2c
->msg_idx
< i2c
->msg_num
- 1) {
798 * Next segment of the message.
805 * If we aren't doing a repeated start and address,
806 * go back and try to send the next byte. Note that
807 * we do not support switching the R/W direction here.
809 if (i2c
->msg
->flags
& I2C_M_NOSTART
)
813 * Write the next address.
815 writel(i2c_pxa_addr_byte(i2c
->msg
), _IDBR(i2c
));
818 * And trigger a repeated start, and send the byte.
821 icr
|= ICR_START
| ICR_TB
;
823 if (i2c
->msg
->len
== 0) {
825 * Device probes have a message length of zero
826 * and need the bus to be reset before it can
831 i2c_pxa_master_complete(i2c
, 0);
834 i2c
->icrlog
[i2c
->irqlogidx
-1] = icr
;
836 writel(icr
, _ICR(i2c
));
840 static void i2c_pxa_irq_rxfull(struct pxa_i2c
*i2c
, u32 isr
)
842 u32 icr
= readl(_ICR(i2c
)) & ~(ICR_START
|ICR_STOP
|ICR_ACKNAK
|ICR_TB
);
847 i2c
->msg
->buf
[i2c
->msg_ptr
++] = readl(_IDBR(i2c
));
849 if (i2c
->msg_ptr
< i2c
->msg
->len
) {
851 * If this is the last byte of the last
852 * message, send a STOP.
854 if (i2c
->msg_ptr
== i2c
->msg
->len
- 1)
855 icr
|= ICR_STOP
| ICR_ACKNAK
;
857 icr
|= ICR_ALDIE
| ICR_TB
;
859 i2c_pxa_master_complete(i2c
, 0);
862 i2c
->icrlog
[i2c
->irqlogidx
-1] = icr
;
864 writel(icr
, _ICR(i2c
));
867 static irqreturn_t
i2c_pxa_handler(int this_irq
, void *dev_id
)
869 struct pxa_i2c
*i2c
= dev_id
;
870 u32 isr
= readl(_ISR(i2c
));
872 if (i2c_debug
> 2 && 0) {
873 dev_dbg(&i2c
->adap
.dev
, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
874 __func__
, isr
, readl(_ICR(i2c
)), readl(_IBMR(i2c
)));
878 if (i2c
->irqlogidx
< ARRAY_SIZE(i2c
->isrlog
))
879 i2c
->isrlog
[i2c
->irqlogidx
++] = isr
;
884 * Always clear all pending IRQs.
886 writel(isr
& (ISR_SSD
|ISR_ALD
|ISR_ITE
|ISR_IRF
|ISR_SAD
|ISR_BED
), _ISR(i2c
));
889 i2c_pxa_slave_start(i2c
, isr
);
891 i2c_pxa_slave_stop(i2c
);
893 if (i2c_pxa_is_slavemode(i2c
)) {
895 i2c_pxa_slave_txempty(i2c
, isr
);
897 i2c_pxa_slave_rxfull(i2c
, isr
);
898 } else if (i2c
->msg
) {
900 i2c_pxa_irq_txempty(i2c
, isr
);
902 i2c_pxa_irq_rxfull(i2c
, isr
);
904 i2c_pxa_scream_blue_murder(i2c
, "spurious irq");
911 static int i2c_pxa_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
913 struct pxa_i2c
*i2c
= adap
->algo_data
;
916 /* If the I2C controller is disabled we need to reset it (probably due
917 to a suspend/resume destroying state). We do this here as we can then
918 avoid worrying about resuming the controller before its users. */
919 if (!(readl(_ICR(i2c
)) & ICR_IUE
))
922 for (i
= adap
->retries
; i
>= 0; i
--) {
923 ret
= i2c_pxa_do_xfer(i2c
, msgs
, num
);
924 if (ret
!= I2C_RETRY
)
928 dev_dbg(&adap
->dev
, "Retrying transmission\n");
931 i2c_pxa_scream_blue_murder(i2c
, "exhausted retries");
934 i2c_pxa_set_slave(i2c
, ret
);
938 static u32
i2c_pxa_functionality(struct i2c_adapter
*adap
)
940 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
943 static const struct i2c_algorithm i2c_pxa_algorithm
= {
944 .master_xfer
= i2c_pxa_xfer
,
945 .functionality
= i2c_pxa_functionality
,
948 static const struct i2c_algorithm i2c_pxa_pio_algorithm
= {
949 .master_xfer
= i2c_pxa_pio_xfer
,
950 .functionality
= i2c_pxa_functionality
,
953 static void i2c_pxa_enable(struct platform_device
*dev
)
955 if (cpu_is_pxa27x()) {
958 pxa_gpio_mode(GPIO117_I2CSCL_MD
);
959 pxa_gpio_mode(GPIO118_I2CSDA_MD
);
970 static void i2c_pxa_disable(struct platform_device
*dev
)
972 if (cpu_is_pxa27x() && dev
->id
== 1) {
974 PCFR
&= ~PCFR_PI2CEN
;
979 #define res_len(r) ((r)->end - (r)->start + 1)
980 static int i2c_pxa_probe(struct platform_device
*dev
)
983 struct resource
*res
;
984 struct i2c_pxa_platform_data
*plat
= dev
->dev
.platform_data
;
988 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
989 irq
= platform_get_irq(dev
, 0);
990 if (res
== NULL
|| irq
< 0)
993 if (!request_mem_region(res
->start
, res_len(res
), res
->name
))
996 i2c
= kzalloc(sizeof(struct pxa_i2c
), GFP_KERNEL
);
1002 i2c
->adap
.owner
= THIS_MODULE
;
1003 i2c
->adap
.retries
= 5;
1005 spin_lock_init(&i2c
->lock
);
1006 init_waitqueue_head(&i2c
->wait
);
1008 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1009 sprintf(i2c
->adap
.name
, "pxa_i2c-i2c.%u", dev
->id
);
1012 * If "dev->id" is negative we consider it as zero.
1013 * The reason to do so is to avoid sysfs names that only make
1014 * sense when there are multiple adapters.
1016 i2c
->adap
.nr
= dev
->id
!= -1 ? dev
->id
: 0;
1017 snprintf(i2c
->adap
.name
, sizeof(i2c
->adap
.name
), "pxa_i2c-i2c.%u",
1019 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1021 i2c
->clk
= clk_get(&dev
->dev
, "I2CCLK");
1022 if (IS_ERR(i2c
->clk
)) {
1023 ret
= PTR_ERR(i2c
->clk
);
1027 i2c
->reg_base
= ioremap(res
->start
, res_len(res
));
1028 if (!i2c
->reg_base
) {
1033 i2c
->iobase
= res
->start
;
1034 i2c
->iosize
= res_len(res
);
1038 i2c
->slave_addr
= I2C_PXA_SLAVE_ADDR
;
1040 #ifdef CONFIG_I2C_PXA_SLAVE
1042 i2c
->slave_addr
= plat
->slave_addr
;
1043 i2c
->slave
= plat
->slave
;
1047 clk_enable(i2c
->clk
);
1048 i2c_pxa_enable(dev
);
1051 i2c
->adap
.class = plat
->class;
1052 i2c
->use_pio
= plat
->use_pio
;
1056 i2c
->adap
.algo
= &i2c_pxa_pio_algorithm
;
1058 i2c
->adap
.algo
= &i2c_pxa_algorithm
;
1059 ret
= request_irq(irq
, i2c_pxa_handler
, IRQF_DISABLED
,
1060 i2c
->adap
.name
, i2c
);
1067 i2c
->adap
.algo_data
= i2c
;
1068 i2c
->adap
.dev
.parent
= &dev
->dev
;
1070 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1072 * If "dev->id" is negative we consider it as zero.
1073 * The reason to do so is to avoid sysfs names that only make
1074 * sense when there are multiple adapters.
1076 i2c
->adap
.nr
= dev
->id
!= -1 ? dev
->id
: 0;
1079 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1080 ret
= i2c_add_numbered_adapter(&i2c
->adap
);
1082 printk(KERN_INFO
"I2C: Failed to add bus\n");
1086 platform_set_drvdata(dev
, i2c
);
1088 #ifdef CONFIG_I2C_PXA_SLAVE
1089 printk(KERN_INFO
"I2C: %s: PXA I2C adapter, slave address %d\n",
1090 i2c
->adap
.dev
.bus_id
, i2c
->slave_addr
);
1092 printk(KERN_INFO
"I2C: %s: PXA I2C adapter\n",
1093 i2c
->adap
.dev
.bus_id
);
1101 clk_disable(i2c
->clk
);
1102 i2c_pxa_disable(dev
);
1103 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1105 iounmap(i2c
->reg_base
);
1106 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1112 release_mem_region(res
->start
, res_len(res
));
1116 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1117 static int i2c_pxa_remove(struct platform_device
*dev
)
1119 static int __exit
i2c_pxa_remove(struct platform_device
*dev
)
1120 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1122 struct pxa_i2c
*i2c
= platform_get_drvdata(dev
);
1124 platform_set_drvdata(dev
, NULL
);
1126 i2c_del_adapter(&i2c
->adap
);
1128 free_irq(i2c
->irq
, i2c
);
1130 clk_disable(i2c
->clk
);
1132 i2c_pxa_disable(dev
);
1134 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1136 iounmap(i2c
->reg_base
);
1137 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1138 release_mem_region(i2c
->iobase
, i2c
->iosize
);
1144 static struct platform_driver i2c_pxa_driver
= {
1145 .probe
= i2c_pxa_probe
,
1146 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1147 .remove
= i2c_pxa_remove
,
1149 .remove
= __exit_p(i2c_pxa_remove
),
1150 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1152 .name
= "pxa2xx-i2c",
1153 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1155 .owner
= THIS_MODULE
,
1156 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1160 static int __init
i2c_adap_pxa_init(void)
1162 return platform_driver_register(&i2c_pxa_driver
);
1165 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1166 static void i2c_adap_pxa_exit(void)
1168 static void __exit
i2c_adap_pxa_exit(void)
1169 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1171 <<<<<<< HEAD
:drivers
/i2c
/busses
/i2c
-pxa
.c
1172 return platform_driver_unregister(&i2c_pxa_driver
);
1174 platform_driver_unregister(&i2c_pxa_driver
);
1175 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/busses
/i2c
-pxa
.c
1178 MODULE_LICENSE("GPL");
1180 module_init(i2c_adap_pxa_init
);
1181 module_exit(i2c_adap_pxa_exit
);