1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 ST-Ericsson SA
4 * Copyright (C) 2009 STMicroelectronics
6 * I2C master mode controller driver, used in Nomadik 8815
9 * The Mobileye EyeQ5 platform is also supported; it uses
10 * the same Ux500/DB8500 IP block with two quirks:
11 * - The memory bus only supports 32-bit accesses.
12 * - A register must be configured for the I2C speed mode;
13 * it is located in a shared register region called OLB.
15 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
16 * Author: Sachin Verma <sachin.verma@st.com>
18 #include <linux/amba/bus.h>
19 #include <linux/bitfield.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/module.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/regmap.h>
32 #include <linux/slab.h>
34 #define DRIVER_NAME "nmk-i2c"
36 /* I2C Controller register offsets */
37 #define I2C_CR (0x000)
38 #define I2C_SCR (0x004)
39 #define I2C_HSMCR (0x008)
40 #define I2C_MCR (0x00C)
41 #define I2C_TFR (0x010)
42 #define I2C_SR (0x014)
43 #define I2C_RFR (0x018)
44 #define I2C_TFTR (0x01C)
45 #define I2C_RFTR (0x020)
46 #define I2C_DMAR (0x024)
47 #define I2C_BRCR (0x028)
48 #define I2C_IMSCR (0x02C)
49 #define I2C_RISR (0x030)
50 #define I2C_MISR (0x034)
51 #define I2C_ICR (0x038)
53 /* Control registers */
54 #define I2C_CR_PE BIT(0) /* Peripheral Enable */
55 #define I2C_CR_OM GENMASK(2, 1) /* Operating mode */
56 #define I2C_CR_SAM BIT(3) /* Slave addressing mode */
57 #define I2C_CR_SM GENMASK(5, 4) /* Speed mode */
58 #define I2C_CR_SGCM BIT(6) /* Slave general call mode */
59 #define I2C_CR_FTX BIT(7) /* Flush Transmit */
60 #define I2C_CR_FRX BIT(8) /* Flush Receive */
61 #define I2C_CR_DMA_TX_EN BIT(9) /* DMA Tx enable */
62 #define I2C_CR_DMA_RX_EN BIT(10) /* DMA Rx Enable */
63 #define I2C_CR_DMA_SLE BIT(11) /* DMA sync. logic enable */
64 #define I2C_CR_LM BIT(12) /* Loopback mode */
65 #define I2C_CR_FON GENMASK(14, 13) /* Filtering on */
66 #define I2C_CR_FS GENMASK(16, 15) /* Force stop enable */
68 /* Slave control register (SCR) */
69 #define I2C_SCR_SLSU GENMASK(31, 16) /* Slave data setup time */
71 /* Master controller (MCR) register */
72 #define I2C_MCR_OP BIT(0) /* Operation */
73 #define I2C_MCR_A7 GENMASK(7, 1) /* 7-bit address */
74 #define I2C_MCR_EA10 GENMASK(10, 8) /* 10-bit Extended address */
75 #define I2C_MCR_SB BIT(11) /* Extended address */
76 #define I2C_MCR_AM GENMASK(13, 12) /* Address type */
77 #define I2C_MCR_STOP BIT(14) /* Stop condition */
78 #define I2C_MCR_LENGTH GENMASK(25, 15) /* Transaction length */
80 /* Status register (SR) */
81 #define I2C_SR_OP GENMASK(1, 0) /* Operation */
82 #define I2C_SR_STATUS GENMASK(3, 2) /* controller status */
83 #define I2C_SR_CAUSE GENMASK(6, 4) /* Abort cause */
84 #define I2C_SR_TYPE GENMASK(8, 7) /* Receive type */
85 #define I2C_SR_LENGTH GENMASK(19, 9) /* Transfer length */
87 /* Baud-rate counter register (BRCR) */
88 #define I2C_BRCR_BRCNT1 GENMASK(31, 16) /* Baud-rate counter 1 */
89 #define I2C_BRCR_BRCNT2 GENMASK(15, 0) /* Baud-rate counter 2 */
91 /* Interrupt mask set/clear (IMSCR) bits */
92 #define I2C_IT_TXFE BIT(0)
93 #define I2C_IT_TXFNE BIT(1)
94 #define I2C_IT_TXFF BIT(2)
95 #define I2C_IT_TXFOVR BIT(3)
96 #define I2C_IT_RXFE BIT(4)
97 #define I2C_IT_RXFNF BIT(5)
98 #define I2C_IT_RXFF BIT(6)
99 #define I2C_IT_RFSR BIT(16)
100 #define I2C_IT_RFSE BIT(17)
101 #define I2C_IT_WTSR BIT(18)
102 #define I2C_IT_MTD BIT(19)
103 #define I2C_IT_STD BIT(20)
104 #define I2C_IT_MAL BIT(24)
105 #define I2C_IT_BERR BIT(25)
106 #define I2C_IT_MTDWS BIT(28)
108 /* some bits in ICR are reserved */
109 #define I2C_CLEAR_ALL_INTS 0x131f007f
111 /* maximum threshold value */
112 #define MAX_I2C_FIFO_THRESHOLD 15
115 I2C_FREQ_MODE_STANDARD, /* up to 100 Kb/s */
116 I2C_FREQ_MODE_FAST, /* up to 400 Kb/s */
117 I2C_FREQ_MODE_HIGH_SPEED, /* up to 3.4 Mb/s */
118 I2C_FREQ_MODE_FAST_PLUS, /* up to 1 Mb/s */
121 /* Mobileye EyeQ5 offset into a shared register region (called OLB) */
122 #define NMK_I2C_EYEQ5_OLB_IOCR2 0x0B8
124 enum i2c_eyeq5_speed {
125 I2C_EYEQ5_SPEED_FAST,
126 I2C_EYEQ5_SPEED_FAST_PLUS,
127 I2C_EYEQ5_SPEED_HIGH_SPEED,
131 * struct i2c_vendor_data - per-vendor variations
132 * @has_mtdws: variant has the MTDWS bit
133 * @fifodepth: variant FIFO depth
135 struct i2c_vendor_data {
149 I2C_NO_OPERATION = 0xff,
154 enum i2c_operating_mode {
157 I2C_OM_MASTER_OR_SLAVE,
161 * struct i2c_nmk_client - client specific data
162 * @slave_adr: 7-bit slave address
163 * @count: no. bytes to be transferred
164 * @buffer: client data buffer
165 * @xfer_bytes: bytes transferred till now
166 * @operation: current I2C operation
168 struct i2c_nmk_client {
169 unsigned short slave_adr;
171 unsigned char *buffer;
172 unsigned long xfer_bytes;
173 enum i2c_operation operation;
177 * struct nmk_i2c_dev - private data structure of the controller.
178 * @vendor: vendor data for this variant.
179 * @adev: parent amba device.
180 * @adap: corresponding I2C adapter.
181 * @irq: interrupt line for the controller.
182 * @virtbase: virtual io memory area.
183 * @clk: hardware i2c block clock.
184 * @cli: holder of client specific data.
185 * @clk_freq: clock frequency for the operation mode
186 * @tft: Tx FIFO Threshold in bytes
187 * @rft: Rx FIFO Threshold in bytes
188 * @timeout_usecs: Slave response timeout
190 * @stop: stop condition.
191 * @xfer_wq: xfer done wait queue.
192 * @xfer_done: xfer done boolean.
193 * @result: controller propogated result.
194 * @has_32b_bus: controller is on a bus that only supports 32-bit accesses.
197 struct i2c_vendor_data *vendor;
198 struct amba_device *adev;
199 struct i2c_adapter adap;
201 void __iomem *virtbase;
203 struct i2c_nmk_client cli;
208 enum i2c_freq_mode sm;
210 struct wait_queue_head xfer_wq;
216 /* controller's abort causes */
217 static const char *abort_causes[] = {
218 "no ack received after address transmission",
219 "no ack received during data phase",
220 "ack received after xmission of master code",
221 "master lost arbitration",
224 "overflow, maxsize is 2047 bytes",
227 static inline void i2c_set_bit(void __iomem *reg, u32 mask)
229 writel(readl(reg) | mask, reg);
232 static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
234 writel(readl(reg) & ~mask, reg);
237 static inline u8 nmk_i2c_readb(const struct nmk_i2c_dev *priv,
240 if (priv->has_32b_bus)
241 return readl(priv->virtbase + reg);
243 return readb(priv->virtbase + reg);
246 static inline void nmk_i2c_writeb(const struct nmk_i2c_dev *priv, u32 val,
249 if (priv->has_32b_bus)
250 writel(val, priv->virtbase + reg);
252 writeb(val, priv->virtbase + reg);
256 * flush_i2c_fifo() - This function flushes the I2C FIFO
257 * @priv: private data of I2C Driver
259 * This function flushes the I2C Tx and Rx FIFOs. It returns
260 * 0 on successful flushing of FIFO
262 static int flush_i2c_fifo(struct nmk_i2c_dev *priv)
264 #define LOOP_ATTEMPTS 10
269 * flush the transmit and receive FIFO. The flushing
270 * operation takes several cycles before to be completed.
271 * On the completion, the I2C internal logic clears these
272 * bits, until then no one must access Tx, Rx FIFO and
273 * should poll on these bits waiting for the completion.
275 writel((I2C_CR_FTX | I2C_CR_FRX), priv->virtbase + I2C_CR);
277 for (i = 0; i < LOOP_ATTEMPTS; i++) {
278 timeout = ktime_add_us(ktime_get(), priv->timeout_usecs);
280 while (ktime_after(timeout, ktime_get())) {
281 if ((readl(priv->virtbase + I2C_CR) &
282 (I2C_CR_FTX | I2C_CR_FRX)) == 0)
287 dev_err(&priv->adev->dev,
288 "flushing operation timed out giving up after %d attempts",
295 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
296 * @priv: private data of I2C Driver
298 static void disable_all_interrupts(struct nmk_i2c_dev *priv)
300 writel(0, priv->virtbase + I2C_IMSCR);
304 * clear_all_interrupts() - Clear all interrupts of I2C Controller
305 * @priv: private data of I2C Driver
307 static void clear_all_interrupts(struct nmk_i2c_dev *priv)
309 writel(I2C_CLEAR_ALL_INTS, priv->virtbase + I2C_ICR);
313 * init_hw() - initialize the I2C hardware
314 * @priv: private data of I2C Driver
316 static int init_hw(struct nmk_i2c_dev *priv)
320 stat = flush_i2c_fifo(priv);
324 /* disable the controller */
325 i2c_clr_bit(priv->virtbase + I2C_CR, I2C_CR_PE);
327 disable_all_interrupts(priv);
329 clear_all_interrupts(priv);
331 priv->cli.operation = I2C_NO_OPERATION;
337 /* enable peripheral, master mode operation */
338 #define DEFAULT_I2C_REG_CR (FIELD_PREP(I2C_CR_OM, I2C_OM_MASTER) | I2C_CR_PE)
340 /* grab top three bits from extended I2C addresses */
341 #define ADR_3MSB_BITS GENMASK(9, 7)
344 * load_i2c_mcr_reg() - load the MCR register
345 * @priv: private data of controller
346 * @flags: message flags
348 static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *priv, u16 flags)
351 unsigned short slave_adr_3msb_bits;
353 mcr |= FIELD_PREP(I2C_MCR_A7, priv->cli.slave_adr);
355 if (unlikely(flags & I2C_M_TEN)) {
356 /* 10-bit address transaction */
357 mcr |= FIELD_PREP(I2C_MCR_AM, 2);
359 * Get the top 3 bits.
360 * EA10 represents extended address in MCR. This includes
361 * the extension (MSB bits) of the 7 bit address loaded
364 slave_adr_3msb_bits = FIELD_GET(ADR_3MSB_BITS,
365 priv->cli.slave_adr);
367 mcr |= FIELD_PREP(I2C_MCR_EA10, slave_adr_3msb_bits);
369 /* 7-bit address transaction */
370 mcr |= FIELD_PREP(I2C_MCR_AM, 1);
373 /* start byte procedure not applied */
374 mcr |= FIELD_PREP(I2C_MCR_SB, 0);
376 /* check the operation, master read/write? */
377 if (priv->cli.operation == I2C_WRITE)
378 mcr |= FIELD_PREP(I2C_MCR_OP, I2C_WRITE);
380 mcr |= FIELD_PREP(I2C_MCR_OP, I2C_READ);
382 /* stop or repeated start? */
384 mcr |= FIELD_PREP(I2C_MCR_STOP, 1);
386 mcr &= ~FIELD_PREP(I2C_MCR_STOP, 1);
388 mcr |= FIELD_PREP(I2C_MCR_LENGTH, priv->cli.count);
394 * setup_i2c_controller() - setup the controller
395 * @priv: private data of controller
397 static void setup_i2c_controller(struct nmk_i2c_dev *priv)
404 writel(0x0, priv->virtbase + I2C_CR);
405 writel(0x0, priv->virtbase + I2C_HSMCR);
406 writel(0x0, priv->virtbase + I2C_TFTR);
407 writel(0x0, priv->virtbase + I2C_RFTR);
408 writel(0x0, priv->virtbase + I2C_DMAR);
410 i2c_clk = clk_get_rate(priv->clk);
415 * slsu defines the data setup time after SCL clock
416 * stretching in terms of i2c clk cycles + 1 (zero means
417 * "wait one cycle"), the needed setup time for the three
418 * modes are 250ns, 100ns, 10ns respectively.
420 * As the time for one cycle T in nanoseconds is
421 * T = (1/f) * 1000000000 =>
422 * slsu = cycles / (1000000000 / f) + 1
424 ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk);
426 case I2C_FREQ_MODE_FAST:
427 case I2C_FREQ_MODE_FAST_PLUS:
428 slsu = DIV_ROUND_UP(100, ns); /* Fast */
430 case I2C_FREQ_MODE_HIGH_SPEED:
431 slsu = DIV_ROUND_UP(10, ns); /* High */
433 case I2C_FREQ_MODE_STANDARD:
435 slsu = DIV_ROUND_UP(250, ns); /* Standard */
440 dev_dbg(&priv->adev->dev, "calculated SLSU = %04x\n", slsu);
441 writel(FIELD_PREP(I2C_SCR_SLSU, slsu), priv->virtbase + I2C_SCR);
444 * The spec says, in case of std. mode the divider is
445 * 2 whereas it is 3 for fast and fastplus mode of
446 * operation. TODO - high speed support.
448 div = (priv->clk_freq > I2C_MAX_STANDARD_MODE_FREQ) ? 3 : 2;
451 * generate the mask for baud rate counters. The controller
452 * has two baud rate counters. One is used for High speed
453 * operation, and the other is for std, fast mode, fast mode
454 * plus operation. Currently we do not supprt high speed mode
457 brcr1 = FIELD_PREP(I2C_BRCR_BRCNT1, 0);
458 brcr2 = FIELD_PREP(I2C_BRCR_BRCNT2, i2c_clk / (priv->clk_freq * div));
460 /* set the baud rate counter register */
461 writel((brcr1 | brcr2), priv->virtbase + I2C_BRCR);
464 * set the speed mode. Currently we support
465 * only standard and fast mode of operation
466 * TODO - support for fast mode plus (up to 1Mb/s)
467 * and high speed (up to 3.4 Mb/s)
469 if (priv->sm > I2C_FREQ_MODE_FAST) {
470 dev_err(&priv->adev->dev,
471 "do not support this mode defaulting to std. mode\n");
472 brcr2 = FIELD_PREP(I2C_BRCR_BRCNT2,
473 i2c_clk / (I2C_MAX_STANDARD_MODE_FREQ * 2));
474 writel((brcr1 | brcr2), priv->virtbase + I2C_BRCR);
475 writel(FIELD_PREP(I2C_CR_SM, I2C_FREQ_MODE_STANDARD),
476 priv->virtbase + I2C_CR);
478 writel(FIELD_PREP(I2C_CR_SM, priv->sm), priv->virtbase + I2C_CR);
480 /* set the Tx and Rx FIFO threshold */
481 writel(priv->tft, priv->virtbase + I2C_TFTR);
482 writel(priv->rft, priv->virtbase + I2C_RFTR);
485 static bool nmk_i2c_wait_xfer_done(struct nmk_i2c_dev *priv)
487 if (priv->timeout_usecs < jiffies_to_usecs(1)) {
488 unsigned long timeout_usecs = priv->timeout_usecs;
489 ktime_t timeout = ktime_set(0, timeout_usecs * NSEC_PER_USEC);
491 wait_event_hrtimeout(priv->xfer_wq, priv->xfer_done, timeout);
493 unsigned long timeout = usecs_to_jiffies(priv->timeout_usecs);
495 wait_event_timeout(priv->xfer_wq, priv->xfer_done, timeout);
498 return priv->xfer_done;
502 * read_i2c() - Read from I2C client device
503 * @priv: private data of I2C Driver
504 * @flags: message flags
506 * This function reads from i2c client device when controller is in
507 * master mode. There is a completion timeout. If there is no transfer
508 * before timeout error is returned.
510 static int read_i2c(struct nmk_i2c_dev *priv, u16 flags)
516 mcr = load_i2c_mcr_reg(priv, flags);
517 writel(mcr, priv->virtbase + I2C_MCR);
519 /* load the current CR value */
520 writel(readl(priv->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
521 priv->virtbase + I2C_CR);
523 /* enable the controller */
524 i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE);
526 init_waitqueue_head(&priv->xfer_wq);
527 priv->xfer_done = false;
529 /* enable interrupts by setting the mask */
530 irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
531 I2C_IT_MAL | I2C_IT_BERR);
533 if (priv->stop || !priv->vendor->has_mtdws)
534 irq_mask |= I2C_IT_MTD;
536 irq_mask |= I2C_IT_MTDWS;
538 irq_mask &= I2C_CLEAR_ALL_INTS;
540 writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask,
541 priv->virtbase + I2C_IMSCR);
543 xfer_done = nmk_i2c_wait_xfer_done(priv);
546 /* Controller timed out */
547 dev_err(&priv->adev->dev, "read from slave 0x%x timed out\n",
548 priv->cli.slave_adr);
554 static void fill_tx_fifo(struct nmk_i2c_dev *priv, int no_bytes)
558 for (count = (no_bytes - 2);
560 (priv->cli.count != 0);
562 /* write to the Tx FIFO */
563 nmk_i2c_writeb(priv, *priv->cli.buffer, I2C_TFR);
566 priv->cli.xfer_bytes++;
572 * write_i2c() - Write data to I2C client.
573 * @priv: private data of I2C Driver
574 * @flags: message flags
576 * This function writes data to I2C client
578 static int write_i2c(struct nmk_i2c_dev *priv, u16 flags)
584 mcr = load_i2c_mcr_reg(priv, flags);
586 writel(mcr, priv->virtbase + I2C_MCR);
588 /* load the current CR value */
589 writel(readl(priv->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
590 priv->virtbase + I2C_CR);
592 /* enable the controller */
593 i2c_set_bit(priv->virtbase + I2C_CR, I2C_CR_PE);
595 init_waitqueue_head(&priv->xfer_wq);
596 priv->xfer_done = false;
598 /* enable interrupts by settings the masks */
599 irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
601 /* Fill the TX FIFO with transmit data */
602 fill_tx_fifo(priv, MAX_I2C_FIFO_THRESHOLD);
604 if (priv->cli.count != 0)
605 irq_mask |= I2C_IT_TXFNE;
608 * check if we want to transfer a single or multiple bytes, if so
609 * set the MTDWS bit (Master Transaction Done Without Stop)
610 * to start repeated start operation
612 if (priv->stop || !priv->vendor->has_mtdws)
613 irq_mask |= I2C_IT_MTD;
615 irq_mask |= I2C_IT_MTDWS;
617 irq_mask &= I2C_CLEAR_ALL_INTS;
619 writel(readl(priv->virtbase + I2C_IMSCR) | irq_mask,
620 priv->virtbase + I2C_IMSCR);
622 xfer_done = nmk_i2c_wait_xfer_done(priv);
625 /* Controller timed out */
626 dev_err(&priv->adev->dev, "write to slave 0x%x timed out\n",
627 priv->cli.slave_adr);
635 * nmk_i2c_xfer_one() - transmit a single I2C message
636 * @priv: device with a message encoded into it
637 * @flags: message flags
639 static int nmk_i2c_xfer_one(struct nmk_i2c_dev *priv, u16 flags)
643 if (flags & I2C_M_RD) {
645 priv->cli.operation = I2C_READ;
646 status = read_i2c(priv, flags);
648 /* write operation */
649 priv->cli.operation = I2C_WRITE;
650 status = write_i2c(priv, flags);
653 if (status || priv->result) {
657 i2c_sr = readl(priv->virtbase + I2C_SR);
658 if (FIELD_GET(I2C_SR_STATUS, i2c_sr) == I2C_ABORT) {
659 cause = FIELD_GET(I2C_SR_CAUSE, i2c_sr);
660 dev_err(&priv->adev->dev, "%s\n",
661 cause >= ARRAY_SIZE(abort_causes) ?
663 abort_causes[cause]);
668 status = status ? status : priv->result;
675 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
676 * @i2c_adap: Adapter pointer to the controller
677 * @msgs: Pointer to data to be written.
678 * @num_msgs: Number of messages to be executed
680 * This is the function called by the generic kernel i2c_transfer()
681 * or i2c_smbus...() API calls. Note that this code is protected by the
682 * semaphore set in the kernel i2c_transfer() function.
685 * READ TRANSFER : We impose a restriction of the first message to be the
686 * index message for any read transaction.
687 * - a no index is coded as '0',
688 * - 2byte big endian index is coded as '3'
689 * !!! msg[0].buf holds the actual index.
690 * This is compatible with generic messages of smbus emulator
691 * that send a one byte index.
692 * eg. a I2C transation to read 2 bytes from index 0
694 * msg[0].addr = client->addr;
695 * msg[0].flags = 0x0;
699 * msg[1].addr = client->addr;
700 * msg[1].flags = I2C_M_RD;
702 * msg[1].buf = rd_buff
703 * i2c_transfer(adap, msg, 2);
705 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
706 * If you want to emulate an SMBUS write transaction put the
707 * index as first byte(or first and second) in the payload.
708 * eg. a I2C transation to write 2 bytes from index 1
712 * msg[0].flags = 0x0;
714 * msg[0].buf = wr_buff;
715 * i2c_transfer(adap, msg, 1);
717 * To read or write a block of data (multiple bytes) using SMBUS emulation
718 * please use the i2c_smbus_read_i2c_block_data()
719 * or i2c_smbus_write_i2c_block_data() API
721 static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
722 struct i2c_msg msgs[], int num_msgs)
726 struct nmk_i2c_dev *priv = i2c_get_adapdata(i2c_adap);
729 pm_runtime_get_sync(&priv->adev->dev);
731 /* Attempt three times to send the message queue */
732 for (j = 0; j < 3; j++) {
733 /* setup the i2c controller */
734 setup_i2c_controller(priv);
736 for (i = 0; i < num_msgs; i++) {
737 priv->cli.slave_adr = msgs[i].addr;
738 priv->cli.buffer = msgs[i].buf;
739 priv->cli.count = msgs[i].len;
740 priv->stop = (i < (num_msgs - 1)) ? 0 : 1;
743 status = nmk_i2c_xfer_one(priv, msgs[i].flags);
751 pm_runtime_put_sync(&priv->adev->dev);
753 /* return the no. messages processed */
761 * disable_interrupts() - disable the interrupts
762 * @priv: private data of controller
763 * @irq: interrupt number
765 static int disable_interrupts(struct nmk_i2c_dev *priv, u32 irq)
767 irq &= I2C_CLEAR_ALL_INTS;
768 writel(readl(priv->virtbase + I2C_IMSCR) & ~irq,
769 priv->virtbase + I2C_IMSCR);
774 * i2c_irq_handler() - interrupt routine
775 * @irq: interrupt number
776 * @arg: data passed to the handler
778 * This is the interrupt handler for the i2c driver. Currently
779 * it handles the major interrupts like Rx & Tx FIFO management
780 * interrupts, master transaction interrupts, arbitration and
781 * bus error interrupts. The rest of the interrupts are treated as
784 static irqreturn_t i2c_irq_handler(int irq, void *arg)
786 struct nmk_i2c_dev *priv = arg;
787 struct device *dev = &priv->adev->dev;
792 /* load Tx FIFO and Rx FIFO threshold values */
793 tft = readl(priv->virtbase + I2C_TFTR);
794 rft = readl(priv->virtbase + I2C_RFTR);
796 /* read interrupt status register */
797 misr = readl(priv->virtbase + I2C_MISR);
802 /* Transmit FIFO nearly empty interrupt */
805 if (priv->cli.operation == I2C_READ) {
807 * in read operation why do we care for writing?
808 * so disable the Transmit FIFO interrupt
810 disable_interrupts(priv, I2C_IT_TXFNE);
812 fill_tx_fifo(priv, (MAX_I2C_FIFO_THRESHOLD - tft));
814 * if done, close the transfer by disabling the
815 * corresponding TXFNE interrupt
817 if (priv->cli.count == 0)
818 disable_interrupts(priv, I2C_IT_TXFNE);
824 * Rx FIFO nearly full interrupt.
825 * This is set when the numer of entries in Rx FIFO is
826 * greater or equal than the threshold value programmed
830 for (count = rft; count > 0; count--) {
831 /* Read the Rx FIFO */
832 *priv->cli.buffer = nmk_i2c_readb(priv, I2C_RFR);
835 priv->cli.count -= rft;
836 priv->cli.xfer_bytes += rft;
841 for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
842 *priv->cli.buffer = nmk_i2c_readb(priv, I2C_RFR);
845 priv->cli.count -= MAX_I2C_FIFO_THRESHOLD;
846 priv->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
849 /* Master Transaction Done with/without stop */
852 if (priv->cli.operation == I2C_READ) {
853 while (!(readl(priv->virtbase + I2C_RISR)
855 if (priv->cli.count == 0)
858 nmk_i2c_readb(priv, I2C_RFR);
861 priv->cli.xfer_bytes++;
865 disable_all_interrupts(priv);
866 clear_all_interrupts(priv);
868 if (priv->cli.count) {
870 dev_err(dev, "%lu bytes still remain to be xfered\n",
874 priv->xfer_done = true;
875 wake_up(&priv->xfer_wq);
880 /* Master Arbitration lost interrupt */
885 i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_MAL);
886 priv->xfer_done = true;
887 wake_up(&priv->xfer_wq);
893 * Bus Error interrupt.
894 * This happens when an unexpected start/stop condition occurs
895 * during the transaction.
901 sr = readl(priv->virtbase + I2C_SR);
903 if (FIELD_GET(I2C_SR_STATUS, sr) == I2C_ABORT)
906 i2c_set_bit(priv->virtbase + I2C_ICR, I2C_IT_BERR);
907 priv->xfer_done = true;
908 wake_up(&priv->xfer_wq);
914 * Tx FIFO overrun interrupt.
915 * This is set when a write operation in Tx FIFO is performed and
916 * the Tx FIFO is full.
922 dev_err(dev, "Tx Fifo Over run\n");
923 priv->xfer_done = true;
924 wake_up(&priv->xfer_wq);
929 /* unhandled interrupts by this driver - TODO*/
937 dev_err(dev, "unhandled Interrupt\n");
940 dev_err(dev, "spurious Interrupt..\n");
947 static int nmk_i2c_suspend_late(struct device *dev)
951 ret = pm_runtime_force_suspend(dev);
955 pinctrl_pm_select_sleep_state(dev);
959 static int nmk_i2c_resume_early(struct device *dev)
961 return pm_runtime_force_resume(dev);
964 static int nmk_i2c_runtime_suspend(struct device *dev)
966 struct amba_device *adev = to_amba_device(dev);
967 struct nmk_i2c_dev *priv = amba_get_drvdata(adev);
969 clk_disable_unprepare(priv->clk);
970 pinctrl_pm_select_idle_state(dev);
974 static int nmk_i2c_runtime_resume(struct device *dev)
976 struct amba_device *adev = to_amba_device(dev);
977 struct nmk_i2c_dev *priv = amba_get_drvdata(adev);
980 ret = clk_prepare_enable(priv->clk);
982 dev_err(dev, "can't prepare_enable clock\n");
986 pinctrl_pm_select_default_state(dev);
990 clk_disable_unprepare(priv->clk);
991 pinctrl_pm_select_idle_state(dev);
997 static const struct dev_pm_ops nmk_i2c_pm = {
998 LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late, nmk_i2c_resume_early)
999 RUNTIME_PM_OPS(nmk_i2c_runtime_suspend, nmk_i2c_runtime_resume, NULL)
1002 static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
1004 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
1007 static const struct i2c_algorithm nmk_i2c_algo = {
1008 .master_xfer = nmk_i2c_xfer,
1009 .functionality = nmk_i2c_functionality
1012 static void nmk_i2c_of_probe(struct device_node *np,
1013 struct nmk_i2c_dev *priv)
1017 /* Default to 100 kHz if no frequency is given in the node */
1018 if (of_property_read_u32(np, "clock-frequency", &priv->clk_freq))
1019 priv->clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
1021 /* This driver only supports 'standard' and 'fast' modes of operation. */
1022 if (priv->clk_freq <= I2C_MAX_STANDARD_MODE_FREQ)
1023 priv->sm = I2C_FREQ_MODE_STANDARD;
1025 priv->sm = I2C_FREQ_MODE_FAST;
1026 priv->tft = 1; /* Tx FIFO threshold */
1027 priv->rft = 8; /* Rx FIFO threshold */
1029 /* Slave response timeout */
1030 if (!of_property_read_u32(np, "i2c-transfer-timeout-us", &timeout_usecs))
1031 priv->timeout_usecs = timeout_usecs;
1033 priv->timeout_usecs = 200 * USEC_PER_MSEC;
1036 static const unsigned int nmk_i2c_eyeq5_masks[] = {
1044 static int nmk_i2c_eyeq5_probe(struct nmk_i2c_dev *priv)
1046 struct device *dev = &priv->adev->dev;
1047 struct device_node *np = dev->of_node;
1048 unsigned int mask, speed_mode;
1052 priv->has_32b_bus = true;
1054 olb = syscon_regmap_lookup_by_phandle_args(np, "mobileye,olb", 1, &id);
1056 return PTR_ERR(olb);
1057 if (id >= ARRAY_SIZE(nmk_i2c_eyeq5_masks))
1060 if (priv->clk_freq <= 400000)
1061 speed_mode = I2C_EYEQ5_SPEED_FAST;
1062 else if (priv->clk_freq <= 1000000)
1063 speed_mode = I2C_EYEQ5_SPEED_FAST_PLUS;
1065 speed_mode = I2C_EYEQ5_SPEED_HIGH_SPEED;
1067 mask = nmk_i2c_eyeq5_masks[id];
1068 regmap_update_bits(olb, NMK_I2C_EYEQ5_OLB_IOCR2,
1069 mask, speed_mode << __fls(mask));
1074 static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
1077 struct nmk_i2c_dev *priv;
1078 struct device_node *np = adev->dev.of_node;
1079 struct device *dev = &adev->dev;
1080 struct i2c_adapter *adap;
1081 struct i2c_vendor_data *vendor = id->data;
1082 u32 max_fifo_threshold = (vendor->fifodepth / 2) - 1;
1084 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1088 priv->vendor = vendor;
1090 priv->has_32b_bus = false;
1091 nmk_i2c_of_probe(np, priv);
1093 if (of_device_is_compatible(np, "mobileye,eyeq5-i2c")) {
1094 ret = nmk_i2c_eyeq5_probe(priv);
1096 return dev_err_probe(dev, ret, "failed OLB lookup\n");
1099 if (priv->tft > max_fifo_threshold) {
1100 dev_warn(dev, "requested TX FIFO threshold %u, adjusted down to %u\n",
1101 priv->tft, max_fifo_threshold);
1102 priv->tft = max_fifo_threshold;
1105 if (priv->rft > max_fifo_threshold) {
1106 dev_warn(dev, "requested RX FIFO threshold %u, adjusted down to %u\n",
1107 priv->rft, max_fifo_threshold);
1108 priv->rft = max_fifo_threshold;
1111 amba_set_drvdata(adev, priv);
1113 priv->virtbase = devm_ioremap(dev, adev->res.start,
1114 resource_size(&adev->res));
1115 if (!priv->virtbase)
1118 priv->irq = adev->irq[0];
1119 ret = devm_request_irq(dev, priv->irq, i2c_irq_handler, 0,
1122 return dev_err_probe(dev, ret,
1123 "cannot claim the irq %d\n", priv->irq);
1125 priv->clk = devm_clk_get_enabled(dev, NULL);
1126 if (IS_ERR(priv->clk))
1127 return dev_err_probe(dev, PTR_ERR(priv->clk),
1128 "could enable i2c clock\n");
1133 adap->dev.of_node = np;
1134 adap->dev.parent = dev;
1135 adap->owner = THIS_MODULE;
1136 adap->class = I2C_CLASS_DEPRECATED;
1137 adap->algo = &nmk_i2c_algo;
1138 adap->timeout = usecs_to_jiffies(priv->timeout_usecs);
1139 snprintf(adap->name, sizeof(adap->name),
1140 "Nomadik I2C at %pR", &adev->res);
1142 i2c_set_adapdata(adap, priv);
1145 "initialize %s on virtual base %p\n",
1146 adap->name, priv->virtbase);
1148 ret = i2c_add_adapter(adap);
1152 pm_runtime_put(dev);
1157 static void nmk_i2c_remove(struct amba_device *adev)
1159 struct nmk_i2c_dev *priv = amba_get_drvdata(adev);
1161 i2c_del_adapter(&priv->adap);
1162 flush_i2c_fifo(priv);
1163 disable_all_interrupts(priv);
1164 clear_all_interrupts(priv);
1165 /* disable the controller */
1166 i2c_clr_bit(priv->virtbase + I2C_CR, I2C_CR_PE);
1169 static struct i2c_vendor_data vendor_stn8815 = {
1171 .fifodepth = 16, /* Guessed from TFTR/RFTR = 7 */
1174 static struct i2c_vendor_data vendor_db8500 = {
1176 .fifodepth = 32, /* Guessed from TFTR/RFTR = 15 */
1179 static const struct amba_id nmk_i2c_ids[] = {
1183 .data = &vendor_stn8815,
1188 .data = &vendor_db8500,
1193 MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
1195 static struct amba_driver nmk_i2c_driver = {
1197 .owner = THIS_MODULE,
1198 .name = DRIVER_NAME,
1199 .pm = pm_ptr(&nmk_i2c_pm),
1201 .id_table = nmk_i2c_ids,
1202 .probe = nmk_i2c_probe,
1203 .remove = nmk_i2c_remove,
1206 static int __init nmk_i2c_init(void)
1208 return amba_driver_register(&nmk_i2c_driver);
1211 static void __exit nmk_i2c_exit(void)
1213 amba_driver_unregister(&nmk_i2c_driver);
1216 subsys_initcall(nmk_i2c_init);
1217 module_exit(nmk_i2c_exit);
1219 MODULE_AUTHOR("Sachin Verma");
1220 MODULE_AUTHOR("Srinidhi KASAGAR");
1221 MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1222 MODULE_LICENSE("GPL");