1 // SPDX-License-Identifier: GPL-2.0-only
5 * I2C adapter for the PXA I2C bus access.
7 * Copyright (C) 2002 Intrinsyc Software Inc.
8 * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
11 * Apr 2002: Initial version [CS]
12 * Jun 2002: Properly separated algo/adap [FB]
13 * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
14 * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
15 * Sep 2004: Major rework to ensure efficient bus handling [RMK]
16 * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
17 * Feb 2005: Rework slave mode handling [RMK]
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
32 #include <linux/platform_data/i2c-pxa.h>
33 #include <linux/slab.h>
35 /* I2C register field definitions */
36 #define IBMR_SDAS (1 << 0)
37 #define IBMR_SCLS (1 << 1)
39 #define ICR_START (1 << 0) /* start bit */
40 #define ICR_STOP (1 << 1) /* stop bit */
41 #define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
42 #define ICR_TB (1 << 3) /* transfer byte bit */
43 #define ICR_MA (1 << 4) /* master abort */
44 #define ICR_SCLE (1 << 5) /* master clock enable */
45 #define ICR_IUE (1 << 6) /* unit enable */
46 #define ICR_GCD (1 << 7) /* general call disable */
47 #define ICR_ITEIE (1 << 8) /* enable tx interrupts */
48 #define ICR_IRFIE (1 << 9) /* enable rx interrupts */
49 #define ICR_BEIE (1 << 10) /* enable bus error ints */
50 #define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
51 #define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
52 #define ICR_SADIE (1 << 13) /* slave address detected int enable */
53 #define ICR_UR (1 << 14) /* unit reset */
54 #define ICR_FM (1 << 15) /* fast mode */
55 #define ICR_HS (1 << 16) /* High Speed mode */
56 #define ICR_A3700_FM (1 << 16) /* fast mode for armada-3700 */
57 #define ICR_A3700_HS (1 << 17) /* high speed mode for armada-3700 */
58 #define ICR_GPIOEN (1 << 19) /* enable GPIO mode for SCL in HS */
60 #define ISR_RWM (1 << 0) /* read/write mode */
61 #define ISR_ACKNAK (1 << 1) /* ack/nak status */
62 #define ISR_UB (1 << 2) /* unit busy */
63 #define ISR_IBB (1 << 3) /* bus busy */
64 #define ISR_SSD (1 << 4) /* slave stop detected */
65 #define ISR_ALD (1 << 5) /* arbitration loss detected */
66 #define ISR_ITE (1 << 6) /* tx buffer empty */
67 #define ISR_IRF (1 << 7) /* rx buffer full */
68 #define ISR_GCAD (1 << 8) /* general call address detected */
69 #define ISR_SAD (1 << 9) /* slave address detected */
70 #define ISR_BED (1 << 10) /* bus error no ACK/NAK */
72 #define ILCR_SLV_SHIFT 0
73 #define ILCR_SLV_MASK (0x1FF << ILCR_SLV_SHIFT)
74 #define ILCR_FLV_SHIFT 9
75 #define ILCR_FLV_MASK (0x1FF << ILCR_FLV_SHIFT)
76 #define ILCR_HLVL_SHIFT 18
77 #define ILCR_HLVL_MASK (0x1FF << ILCR_HLVL_SHIFT)
78 #define ILCR_HLVH_SHIFT 27
79 #define ILCR_HLVH_MASK (0x1F << ILCR_HLVH_SHIFT)
81 #define IWCR_CNT_SHIFT 0
82 #define IWCR_CNT_MASK (0x1F << IWCR_CNT_SHIFT)
83 #define IWCR_HS_CNT1_SHIFT 5
84 #define IWCR_HS_CNT1_MASK (0x1F << IWCR_HS_CNT1_SHIFT)
85 #define IWCR_HS_CNT2_SHIFT 10
86 #define IWCR_HS_CNT2_MASK (0x1F << IWCR_HS_CNT2_SHIFT)
88 /* need a longer timeout if we're dealing with the fact we may well be
89 * looking at a multi-master environment
91 #define DEF_TIMEOUT 32
93 #define BUS_ERROR (-EREMOTEIO)
94 #define XFER_NAKED (-ECONNREFUSED)
95 #define I2C_RETRY (-2000) /* an error has occurred retry transmit */
97 /* ICR initialize bit values
99 * 15 FM 0 (100 kHz operation)
100 * 14 UR 0 (No unit reset)
101 * 13 SADIE 0 (Disables the unit from interrupting on slave addresses
102 * matching its slave address)
103 * 12 ALDIE 0 (Disables the unit from interrupt when it loses arbitration
105 * 11 SSDIE 0 (Disables interrupts from a slave stop detected, in slave mode)
106 * 10 BEIE 1 (Enable interrupts from detected bus errors, no ACK sent)
107 * 9 IRFIE 1 (Enable interrupts from full buffer received)
108 * 8 ITEIE 1 (Enables the I2C unit to interrupt when transmit buffer empty)
109 * 7 GCD 1 (Disables i2c unit response to general call messages as a slave)
110 * 6 IUE 0 (Disable unit until we change settings)
111 * 5 SCLE 1 (Enables the i2c clock output for master mode (drives SCL)
112 * 4 MA 0 (Only send stop with the ICR stop bit)
113 * 3 TB 0 (We are not transmitting a byte initially)
114 * 2 ACKNAK 0 (Send an ACK after the unit receives a byte)
115 * 1 STOP 0 (Do not send a STOP)
116 * 0 START 0 (Do not send a START)
118 #define I2C_ICR_INIT (ICR_BEIE | ICR_IRFIE | ICR_ITEIE | ICR_GCD | ICR_SCLE)
120 /* I2C status register init values
122 * 10 BED 1 (Clear bus error detected)
123 * 9 SAD 1 (Clear slave address detected)
124 * 7 IRF 1 (Clear IDBR Receive Full)
125 * 6 ITE 1 (Clear IDBR Transmit Empty)
126 * 5 ALD 1 (Clear Arbitration Loss Detected)
127 * 4 SSD 1 (Clear Slave Stop Detected)
129 #define I2C_ISR_INIT 0x7FF /* status register init */
131 struct pxa_reg_layout {
151 /* I2C register layout definitions */
152 static struct pxa_reg_layout pxa_reg_layout[] = {
176 /* no isar register */
202 static const struct of_device_id i2c_pxa_dt_ids[] = {
203 { .compatible = "mrvl,pxa-i2c", .data = (void *)REGS_PXA2XX },
204 { .compatible = "mrvl,pwri2c", .data = (void *)REGS_PXA3XX },
205 { .compatible = "mrvl,mmp-twsi", .data = (void *)REGS_PXA910 },
206 { .compatible = "marvell,armada-3700-i2c", .data = (void *)REGS_A3700 },
209 MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
211 static const struct platform_device_id i2c_pxa_id_table[] = {
212 { "pxa2xx-i2c", REGS_PXA2XX },
213 { "pxa3xx-pwri2c", REGS_PXA3XX },
214 { "ce4100-i2c", REGS_CE4100 },
215 { "pxa910-i2c", REGS_PXA910 },
216 { "armada-3700-i2c", REGS_A3700 },
219 MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
223 wait_queue_head_t wait;
225 unsigned int msg_num;
226 unsigned int msg_idx;
227 unsigned int msg_ptr;
228 unsigned int slave_addr;
229 unsigned int req_slave_addr;
231 struct i2c_adapter adap;
233 #ifdef CONFIG_I2C_PXA_SLAVE
234 struct i2c_client *slave;
237 unsigned int irqlogidx;
241 void __iomem *reg_base;
242 void __iomem *reg_ibmr;
243 void __iomem *reg_idbr;
244 void __iomem *reg_icr;
245 void __iomem *reg_isr;
246 void __iomem *reg_isar;
247 void __iomem *reg_ilcr;
248 void __iomem *reg_iwcr;
250 unsigned long iobase;
251 unsigned long iosize;
254 unsigned int use_pio :1;
255 unsigned int fast_mode :1;
256 unsigned int high_mode:1;
257 unsigned char master_code;
264 #define _IBMR(i2c) ((i2c)->reg_ibmr)
265 #define _IDBR(i2c) ((i2c)->reg_idbr)
266 #define _ICR(i2c) ((i2c)->reg_icr)
267 #define _ISR(i2c) ((i2c)->reg_isr)
268 #define _ISAR(i2c) ((i2c)->reg_isar)
269 #define _ILCR(i2c) ((i2c)->reg_ilcr)
270 #define _IWCR(i2c) ((i2c)->reg_iwcr)
273 * I2C Slave mode address
275 #define I2C_PXA_SLAVE_ADDR 0x1
284 #define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
287 decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
289 printk("%s %08x:", prefix, val);
291 const char *str = val & bits->mask ? bits->set : bits->unset;
299 static const struct bits isr_bits[] = {
300 PXA_BIT(ISR_RWM, "RX", "TX"),
301 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
302 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
303 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
304 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
305 PXA_BIT(ISR_ALD, "ALD", NULL),
306 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
307 PXA_BIT(ISR_IRF, "RxFull", NULL),
308 PXA_BIT(ISR_GCAD, "GenCall", NULL),
309 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
310 PXA_BIT(ISR_BED, "BusErr", NULL),
313 static void decode_ISR(unsigned int val)
315 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
318 static const struct bits icr_bits[] = {
319 PXA_BIT(ICR_START, "START", NULL),
320 PXA_BIT(ICR_STOP, "STOP", NULL),
321 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
322 PXA_BIT(ICR_TB, "TB", NULL),
323 PXA_BIT(ICR_MA, "MA", NULL),
324 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
325 PXA_BIT(ICR_IUE, "IUE", "iue"),
326 PXA_BIT(ICR_GCD, "GCD", NULL),
327 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
328 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
329 PXA_BIT(ICR_BEIE, "BEIE", NULL),
330 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
331 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
332 PXA_BIT(ICR_SADIE, "SADIE", NULL),
333 PXA_BIT(ICR_UR, "UR", "ur"),
336 #ifdef CONFIG_I2C_PXA_SLAVE
337 static void decode_ICR(unsigned int val)
339 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
343 static unsigned int i2c_debug = DEBUG;
345 static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
347 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
348 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
351 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
353 static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
356 struct device *dev = &i2c->adap.dev;
358 dev_err(dev, "slave_0x%x error: %s\n",
359 i2c->req_slave_addr >> 1, why);
360 dev_err(dev, "msg_num: %d msg_idx: %d msg_ptr: %d\n",
361 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
362 dev_err(dev, "IBMR: %08x IDBR: %08x ICR: %08x ISR: %08x\n",
363 readl(_IBMR(i2c)), readl(_IDBR(i2c)), readl(_ICR(i2c)),
365 dev_err(dev, "log:");
366 for (i = 0; i < i2c->irqlogidx; i++)
367 pr_cont(" [%03x:%05x]", i2c->isrlog[i], i2c->icrlog[i]);
371 #else /* ifdef DEBUG */
375 #define show_state(i2c) do { } while (0)
376 #define decode_ISR(val) do { } while (0)
377 #define decode_ICR(val) do { } while (0)
378 #define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
380 #endif /* ifdef DEBUG / else */
382 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
384 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
386 return !(readl(_ICR(i2c)) & ICR_SCLE);
389 static void i2c_pxa_abort(struct pxa_i2c *i2c)
393 if (i2c_pxa_is_slavemode(i2c)) {
394 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
398 while ((i > 0) && (readl(_IBMR(i2c)) & IBMR_SDAS) == 0) {
399 unsigned long icr = readl(_ICR(i2c));
402 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
404 writel(icr, _ICR(i2c));
412 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
416 static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
418 int timeout = DEF_TIMEOUT;
422 isr = readl(_ISR(i2c));
423 if (!(isr & (ISR_IBB | ISR_UB)))
441 static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
443 unsigned long timeout = jiffies + HZ*4;
445 while (time_before(jiffies, timeout)) {
447 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
448 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
450 if (readl(_ISR(i2c)) & ISR_SAD) {
452 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
456 /* wait for unit and bus being not busy, and we also do a
457 * quick check of the i2c lines themselves to ensure they've
460 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 &&
461 readl(_IBMR(i2c)) == (IBMR_SCLS | IBMR_SDAS)) {
463 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
471 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
476 static int i2c_pxa_set_master(struct pxa_i2c *i2c)
479 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
481 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
482 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
483 if (!i2c_pxa_wait_master(i2c)) {
484 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
489 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
493 #ifdef CONFIG_I2C_PXA_SLAVE
494 static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
496 unsigned long timeout = jiffies + HZ*1;
502 while (time_before(jiffies, timeout)) {
504 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
505 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
507 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
508 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
509 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
511 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
519 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
524 * clear the hold on the bus, and take of anything else
525 * that has been configured
527 static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
532 udelay(100); /* simple delay */
534 /* we need to wait for the stop condition to end */
536 /* if we where in stop, then clear... */
537 if (readl(_ICR(i2c)) & ICR_STOP) {
539 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
542 if (!i2c_pxa_wait_slave(i2c)) {
543 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
549 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
550 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
553 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
554 decode_ICR(readl(_ICR(i2c)));
558 #define i2c_pxa_set_slave(i2c, err) do { } while (0)
561 static void i2c_pxa_reset(struct pxa_i2c *i2c)
563 pr_debug("Resetting I2C Controller Unit\n");
565 /* abort any transfer currently under way */
568 /* reset according to 9.8 */
569 writel(ICR_UR, _ICR(i2c));
570 writel(I2C_ISR_INIT, _ISR(i2c));
571 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
573 if (i2c->reg_isar && IS_ENABLED(CONFIG_I2C_PXA_SLAVE))
574 writel(i2c->slave_addr, _ISAR(i2c));
576 /* set control register values */
577 writel(I2C_ICR_INIT | (i2c->fast_mode ? i2c->fm_mask : 0), _ICR(i2c));
578 writel(readl(_ICR(i2c)) | (i2c->high_mode ? i2c->hs_mask : 0), _ICR(i2c));
580 #ifdef CONFIG_I2C_PXA_SLAVE
581 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
582 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
585 i2c_pxa_set_slave(i2c, 0);
588 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
593 #ifdef CONFIG_I2C_PXA_SLAVE
598 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
601 /* what should we do here? */
605 if (i2c->slave != NULL)
606 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_PROCESSED,
609 writel(byte, _IDBR(i2c));
610 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
614 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
616 u8 byte = readl(_IDBR(i2c));
618 if (i2c->slave != NULL)
619 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_RECEIVED, &byte);
621 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
624 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
629 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
630 (isr & ISR_RWM) ? 'r' : 't');
632 if (i2c->slave != NULL) {
636 i2c_slave_event(i2c->slave, I2C_SLAVE_READ_REQUESTED,
638 writel(byte, _IDBR(i2c));
640 i2c_slave_event(i2c->slave, I2C_SLAVE_WRITE_REQUESTED,
646 * slave could interrupt in the middle of us generating a
647 * start condition... if this happens, we'd better back off
648 * and stop holding the poor thing up
650 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
651 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
656 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
662 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
667 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
670 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
673 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
675 if (i2c->slave != NULL)
676 i2c_slave_event(i2c->slave, I2C_SLAVE_STOP, NULL);
679 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
682 * If we have a master-mode message waiting,
683 * kick it off now that the slave has completed.
686 i2c_pxa_master_complete(i2c, I2C_RETRY);
689 static int i2c_pxa_slave_reg(struct i2c_client *slave)
691 struct pxa_i2c *i2c = slave->adapter->algo_data;
697 return -EAFNOSUPPORT;
700 i2c->slave_addr = slave->addr;
702 writel(i2c->slave_addr, _ISAR(i2c));
707 static int i2c_pxa_slave_unreg(struct i2c_client *slave)
709 struct pxa_i2c *i2c = slave->adapter->algo_data;
711 WARN_ON(!i2c->slave);
713 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
714 writel(i2c->slave_addr, _ISAR(i2c));
721 static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
724 /* what should we do here? */
726 writel(0, _IDBR(i2c));
727 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
731 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
733 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
736 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
741 * slave could interrupt in the middle of us generating a
742 * start condition... if this happens, we'd better back off
743 * and stop holding the poor thing up
745 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
746 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
751 if ((readl(_IBMR(i2c)) & IBMR_SCLS) == IBMR_SCLS)
757 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
762 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
765 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
768 i2c_pxa_master_complete(i2c, I2C_RETRY);
773 * PXA I2C Master mode
776 static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
781 * Step 1: target slave address into IDBR
783 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
784 writel(i2c->req_slave_addr, _IDBR(i2c));
787 * Step 2: initiate the write.
789 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
790 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
793 static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
798 * Clear the STOP and ACK flags
800 icr = readl(_ICR(i2c));
801 icr &= ~(ICR_STOP | ICR_ACKNAK);
802 writel(icr, _ICR(i2c));
806 * PXA I2C send master code
807 * 1. Load master code to IDBR and send it.
808 * Note for HS mode, set ICR [GPIOEN].
809 * 2. Wait until win arbitration.
811 static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
816 spin_lock_irq(&i2c->lock);
817 i2c->highmode_enter = true;
818 writel(i2c->master_code, _IDBR(i2c));
820 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
821 icr |= ICR_GPIOEN | ICR_START | ICR_TB | ICR_ITEIE;
822 writel(icr, _ICR(i2c));
824 spin_unlock_irq(&i2c->lock);
825 timeout = wait_event_timeout(i2c->wait,
826 i2c->highmode_enter == false, HZ * 1);
828 i2c->highmode_enter = false;
830 return (timeout == 0) ? I2C_RETRY : 0;
834 * i2c_pxa_master_complete - complete the message and wake up.
836 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
848 static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
850 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
854 * If ISR_ALD is set, we lost arbitration.
858 * Do we need to do anything here? The PXA docs
859 * are vague about what happens.
861 i2c_pxa_scream_blue_murder(i2c, "ALD set");
864 * We ignore this error. We seem to see spurious ALDs
865 * for seemingly no reason. If we handle them as I think
866 * they should, we end up causing an I2C error, which
867 * is painful for some systems.
872 if ((isr & ISR_BED) &&
873 (!((i2c->msg->flags & I2C_M_IGNORE_NAK) &&
874 (isr & ISR_ACKNAK)))) {
878 * I2C bus error - either the device NAK'd us, or
879 * something more serious happened. If we were NAK'd
880 * on the initial address phase, we can retry.
882 if (isr & ISR_ACKNAK) {
883 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
888 i2c_pxa_master_complete(i2c, ret);
889 } else if (isr & ISR_RWM) {
891 * Read mode. We have just sent the address byte, and
892 * now we must initiate the transfer.
894 if (i2c->msg_ptr == i2c->msg->len - 1 &&
895 i2c->msg_idx == i2c->msg_num - 1)
896 icr |= ICR_STOP | ICR_ACKNAK;
898 icr |= ICR_ALDIE | ICR_TB;
899 } else if (i2c->msg_ptr < i2c->msg->len) {
901 * Write mode. Write the next data byte.
903 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
905 icr |= ICR_ALDIE | ICR_TB;
908 * If this is the last byte of the last message or last byte
909 * of any message with I2C_M_STOP (e.g. SCCB), send a STOP.
911 if ((i2c->msg_ptr == i2c->msg->len) &&
912 ((i2c->msg->flags & I2C_M_STOP) ||
913 (i2c->msg_idx == i2c->msg_num - 1)))
916 } else if (i2c->msg_idx < i2c->msg_num - 1) {
918 * Next segment of the message.
925 * If we aren't doing a repeated start and address,
926 * go back and try to send the next byte. Note that
927 * we do not support switching the R/W direction here.
929 if (i2c->msg->flags & I2C_M_NOSTART)
933 * Write the next address.
935 i2c->req_slave_addr = i2c_8bit_addr_from_msg(i2c->msg);
936 writel(i2c->req_slave_addr, _IDBR(i2c));
939 * And trigger a repeated start, and send the byte.
942 icr |= ICR_START | ICR_TB;
944 if (i2c->msg->len == 0) {
946 * Device probes have a message length of zero
947 * and need the bus to be reset before it can
952 i2c_pxa_master_complete(i2c, 0);
955 i2c->icrlog[i2c->irqlogidx-1] = icr;
957 writel(icr, _ICR(i2c));
961 static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
963 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
968 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
970 if (i2c->msg_ptr < i2c->msg->len) {
972 * If this is the last byte of the last
973 * message, send a STOP.
975 if (i2c->msg_ptr == i2c->msg->len - 1)
976 icr |= ICR_STOP | ICR_ACKNAK;
978 icr |= ICR_ALDIE | ICR_TB;
980 i2c_pxa_master_complete(i2c, 0);
983 i2c->icrlog[i2c->irqlogidx-1] = icr;
985 writel(icr, _ICR(i2c));
988 #define VALID_INT_SOURCE (ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
990 static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
992 struct pxa_i2c *i2c = dev_id;
993 u32 isr = readl(_ISR(i2c));
995 if (!(isr & VALID_INT_SOURCE))
998 if (i2c_debug > 2 && 0) {
999 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
1000 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
1004 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
1005 i2c->isrlog[i2c->irqlogidx++] = isr;
1010 * Always clear all pending IRQs.
1012 writel(isr & VALID_INT_SOURCE, _ISR(i2c));
1015 i2c_pxa_slave_start(i2c, isr);
1017 i2c_pxa_slave_stop(i2c);
1019 if (i2c_pxa_is_slavemode(i2c)) {
1021 i2c_pxa_slave_txempty(i2c, isr);
1023 i2c_pxa_slave_rxfull(i2c, isr);
1024 } else if (i2c->msg && (!i2c->highmode_enter)) {
1026 i2c_pxa_irq_txempty(i2c, isr);
1028 i2c_pxa_irq_rxfull(i2c, isr);
1029 } else if ((isr & ISR_ITE) && i2c->highmode_enter) {
1030 i2c->highmode_enter = false;
1031 wake_up(&i2c->wait);
1033 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
1040 * We are protected by the adapter bus mutex.
1042 static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
1048 * Wait for the bus to become free.
1050 ret = i2c_pxa_wait_bus_not_busy(i2c);
1052 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
1059 ret = i2c_pxa_set_master(i2c);
1061 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
1065 if (i2c->high_mode) {
1066 ret = i2c_pxa_send_mastercode(i2c);
1068 dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
1073 spin_lock_irq(&i2c->lock);
1081 i2c_pxa_start_message(i2c);
1083 spin_unlock_irq(&i2c->lock);
1086 * The rest of the processing occurs in the interrupt handler.
1088 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
1089 i2c_pxa_stop_message(i2c);
1092 * We place the return code in i2c->msg_idx.
1096 if (!timeout && i2c->msg_num) {
1097 i2c_pxa_scream_blue_murder(i2c, "timeout");
1105 static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
1107 struct pxa_i2c *i2c = adap->algo_data;
1110 for (i = adap->retries; i >= 0; i--) {
1111 ret = i2c_pxa_do_xfer(i2c, msgs, num);
1112 if (ret != I2C_RETRY)
1116 dev_dbg(&adap->dev, "Retrying transmission\n");
1119 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1122 i2c_pxa_set_slave(i2c, ret);
1126 static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
1128 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
1129 I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_NOSTART;
1132 static const struct i2c_algorithm i2c_pxa_algorithm = {
1133 .master_xfer = i2c_pxa_xfer,
1134 .functionality = i2c_pxa_functionality,
1135 #ifdef CONFIG_I2C_PXA_SLAVE
1136 .reg_slave = i2c_pxa_slave_reg,
1137 .unreg_slave = i2c_pxa_slave_unreg,
1141 /* Non-interrupt mode support */
1142 static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
1144 /* make timeout the same as for interrupt based functions */
1145 long timeout = 2 * DEF_TIMEOUT;
1148 * Wait for the bus to become free.
1150 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
1157 dev_err(&i2c->adap.dev,
1158 "i2c_pxa: timeout waiting for bus free\n");
1165 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
1170 static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
1171 struct i2c_msg *msg, int num)
1173 unsigned long timeout = 500000; /* 5 seconds */
1176 ret = i2c_pxa_pio_set_master(i2c);
1186 i2c_pxa_start_message(i2c);
1188 while (i2c->msg_num > 0 && --timeout) {
1189 i2c_pxa_handler(0, i2c);
1193 i2c_pxa_stop_message(i2c);
1196 * We place the return code in i2c->msg_idx.
1202 i2c_pxa_scream_blue_murder(i2c, "timeout");
1209 static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
1210 struct i2c_msg msgs[], int num)
1212 struct pxa_i2c *i2c = adap->algo_data;
1215 /* If the I2C controller is disabled we need to reset it
1216 (probably due to a suspend/resume destroying state). We do
1217 this here as we can then avoid worrying about resuming the
1218 controller before its users. */
1219 if (!(readl(_ICR(i2c)) & ICR_IUE))
1222 for (i = adap->retries; i >= 0; i--) {
1223 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
1224 if (ret != I2C_RETRY)
1228 dev_dbg(&adap->dev, "Retrying transmission\n");
1231 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1234 i2c_pxa_set_slave(i2c, ret);
1238 static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
1239 .master_xfer = i2c_pxa_pio_xfer,
1240 .functionality = i2c_pxa_functionality,
1241 #ifdef CONFIG_I2C_PXA_SLAVE
1242 .reg_slave = i2c_pxa_slave_reg,
1243 .unreg_slave = i2c_pxa_slave_unreg,
1247 static int i2c_pxa_probe_dt(struct platform_device *pdev, struct pxa_i2c *i2c,
1248 enum pxa_i2c_types *i2c_types)
1250 struct device_node *np = pdev->dev.of_node;
1251 const struct of_device_id *of_id =
1252 of_match_device(i2c_pxa_dt_ids, &pdev->dev);
1257 /* For device tree we always use the dynamic or alias-assigned ID */
1260 if (of_get_property(np, "mrvl,i2c-polling", NULL))
1262 if (of_get_property(np, "mrvl,i2c-fast-mode", NULL))
1265 *i2c_types = (enum pxa_i2c_types)(of_id->data);
1270 static int i2c_pxa_probe_pdata(struct platform_device *pdev,
1271 struct pxa_i2c *i2c,
1272 enum pxa_i2c_types *i2c_types)
1274 struct i2c_pxa_platform_data *plat = dev_get_platdata(&pdev->dev);
1275 const struct platform_device_id *id = platform_get_device_id(pdev);
1277 *i2c_types = id->driver_data;
1279 i2c->use_pio = plat->use_pio;
1280 i2c->fast_mode = plat->fast_mode;
1281 i2c->high_mode = plat->high_mode;
1282 i2c->master_code = plat->master_code;
1283 if (!i2c->master_code)
1284 i2c->master_code = 0xe;
1285 i2c->rate = plat->rate;
1290 static int i2c_pxa_probe(struct platform_device *dev)
1292 struct i2c_pxa_platform_data *plat = dev_get_platdata(&dev->dev);
1293 enum pxa_i2c_types i2c_type;
1294 struct pxa_i2c *i2c;
1295 struct resource *res = NULL;
1298 i2c = devm_kzalloc(&dev->dev, sizeof(struct pxa_i2c), GFP_KERNEL);
1302 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1303 i2c->reg_base = devm_ioremap_resource(&dev->dev, res);
1304 if (IS_ERR(i2c->reg_base))
1305 return PTR_ERR(i2c->reg_base);
1307 irq = platform_get_irq(dev, 0);
1311 /* Default adapter num to device id; i2c_pxa_probe_dt can override. */
1312 i2c->adap.nr = dev->id;
1314 ret = i2c_pxa_probe_dt(dev, i2c, &i2c_type);
1316 ret = i2c_pxa_probe_pdata(dev, i2c, &i2c_type);
1320 i2c->adap.owner = THIS_MODULE;
1321 i2c->adap.retries = 5;
1323 spin_lock_init(&i2c->lock);
1324 init_waitqueue_head(&i2c->wait);
1326 strlcpy(i2c->adap.name, "pxa_i2c-i2c", sizeof(i2c->adap.name));
1328 i2c->clk = devm_clk_get(&dev->dev, NULL);
1329 if (IS_ERR(i2c->clk)) {
1330 dev_err(&dev->dev, "failed to get the clk: %ld\n", PTR_ERR(i2c->clk));
1331 return PTR_ERR(i2c->clk);
1334 i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
1335 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
1336 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
1337 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
1338 i2c->fm_mask = pxa_reg_layout[i2c_type].fm;
1339 i2c->hs_mask = pxa_reg_layout[i2c_type].hs;
1341 if (i2c_type != REGS_CE4100)
1342 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
1344 if (i2c_type == REGS_PXA910) {
1345 i2c->reg_ilcr = i2c->reg_base + pxa_reg_layout[i2c_type].ilcr;
1346 i2c->reg_iwcr = i2c->reg_base + pxa_reg_layout[i2c_type].iwcr;
1349 i2c->iobase = res->start;
1350 i2c->iosize = resource_size(res);
1354 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
1355 i2c->highmode_enter = false;
1358 i2c->adap.class = plat->class;
1361 if (i2c->high_mode) {
1363 clk_set_rate(i2c->clk, i2c->rate);
1364 pr_info("i2c: <%s> set rate to %ld\n",
1365 i2c->adap.name, clk_get_rate(i2c->clk));
1367 pr_warn("i2c: <%s> clock rate not set\n",
1371 clk_prepare_enable(i2c->clk);
1374 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1376 i2c->adap.algo = &i2c_pxa_algorithm;
1377 ret = devm_request_irq(&dev->dev, irq, i2c_pxa_handler,
1378 IRQF_SHARED | IRQF_NO_SUSPEND,
1379 dev_name(&dev->dev), i2c);
1381 dev_err(&dev->dev, "failed to request irq: %d\n", ret);
1388 i2c->adap.algo_data = i2c;
1389 i2c->adap.dev.parent = &dev->dev;
1391 i2c->adap.dev.of_node = dev->dev.of_node;
1394 ret = i2c_add_numbered_adapter(&i2c->adap);
1398 platform_set_drvdata(dev, i2c);
1400 #ifdef CONFIG_I2C_PXA_SLAVE
1401 dev_info(&i2c->adap.dev, " PXA I2C adapter, slave address %d\n",
1404 dev_info(&i2c->adap.dev, " PXA I2C adapter\n");
1409 clk_disable_unprepare(i2c->clk);
1413 static int i2c_pxa_remove(struct platform_device *dev)
1415 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1417 i2c_del_adapter(&i2c->adap);
1419 clk_disable_unprepare(i2c->clk);
1425 static int i2c_pxa_suspend_noirq(struct device *dev)
1427 struct pxa_i2c *i2c = dev_get_drvdata(dev);
1429 clk_disable(i2c->clk);
1434 static int i2c_pxa_resume_noirq(struct device *dev)
1436 struct pxa_i2c *i2c = dev_get_drvdata(dev);
1438 clk_enable(i2c->clk);
1444 static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
1445 .suspend_noirq = i2c_pxa_suspend_noirq,
1446 .resume_noirq = i2c_pxa_resume_noirq,
1449 #define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
1451 #define I2C_PXA_DEV_PM_OPS NULL
1454 static struct platform_driver i2c_pxa_driver = {
1455 .probe = i2c_pxa_probe,
1456 .remove = i2c_pxa_remove,
1458 .name = "pxa2xx-i2c",
1459 .pm = I2C_PXA_DEV_PM_OPS,
1460 .of_match_table = i2c_pxa_dt_ids,
1462 .id_table = i2c_pxa_id_table,
1465 static int __init i2c_adap_pxa_init(void)
1467 return platform_driver_register(&i2c_pxa_driver);
1470 static void __exit i2c_adap_pxa_exit(void)
1472 platform_driver_unregister(&i2c_pxa_driver);
1475 MODULE_LICENSE("GPL");
1476 MODULE_ALIAS("platform:pxa2xx-i2c");
1478 subsys_initcall(i2c_adap_pxa_init);
1479 module_exit(i2c_adap_pxa_exit);