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 and EyeQ6H platforms are also supported; they use
10 * the same Ux500/DB8500 IP block with two quirks:
11 * - The memory bus only supports 32-bit accesses.
12 * - (only EyeQ5) 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/of_device.h>
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/regmap.h>
33 #include <linux/slab.h>
35 #define DRIVER_NAME "nmk-i2c"
37 /* I2C Controller register offsets */
38 #define I2C_CR (0x000)
39 #define I2C_SCR (0x004)
40 #define I2C_HSMCR (0x008)
41 #define I2C_MCR (0x00C)
42 #define I2C_TFR (0x010)
43 #define I2C_SR (0x014)
44 #define I2C_RFR (0x018)
45 #define I2C_TFTR (0x01C)
46 #define I2C_RFTR (0x020)
47 #define I2C_DMAR (0x024)
48 #define I2C_BRCR (0x028)
49 #define I2C_IMSCR (0x02C)
50 #define I2C_RISR (0x030)
51 #define I2C_MISR (0x034)
52 #define I2C_ICR (0x038)
54 /* Control registers */
55 #define I2C_CR_PE BIT(0) /* Peripheral Enable */
56 #define I2C_CR_OM GENMASK(2, 1) /* Operating mode */
57 #define I2C_CR_SAM BIT(3) /* Slave addressing mode */
58 #define I2C_CR_SM GENMASK(5, 4) /* Speed mode */
59 #define I2C_CR_SGCM BIT(6) /* Slave general call mode */
60 #define I2C_CR_FTX BIT(7) /* Flush Transmit */
61 #define I2C_CR_FRX BIT(8) /* Flush Receive */
62 #define I2C_CR_DMA_TX_EN BIT(9) /* DMA Tx enable */
63 #define I2C_CR_DMA_RX_EN BIT(10) /* DMA Rx Enable */
64 #define I2C_CR_DMA_SLE BIT(11) /* DMA sync. logic enable */
65 #define I2C_CR_LM BIT(12) /* Loopback mode */
66 #define I2C_CR_FON GENMASK(14, 13) /* Filtering on */
67 #define I2C_CR_FS GENMASK(16, 15) /* Force stop enable */
69 /* Slave control register (SCR) */
70 #define I2C_SCR_SLSU GENMASK(31, 16) /* Slave data setup time */
72 /* Master controller (MCR) register */
73 #define I2C_MCR_OP BIT(0) /* Operation */
74 #define I2C_MCR_A7 GENMASK(7, 1) /* 7-bit address */
75 #define I2C_MCR_EA10 GENMASK(10, 8) /* 10-bit Extended address */
76 #define I2C_MCR_SB BIT(11) /* Extended address */
77 #define I2C_MCR_AM GENMASK(13, 12) /* Address type */
78 #define I2C_MCR_STOP BIT(14) /* Stop condition */
79 #define I2C_MCR_LENGTH GENMASK(25, 15) /* Transaction length */
81 /* Status register (SR) */
82 #define I2C_SR_OP GENMASK(1, 0) /* Operation */
83 #define I2C_SR_STATUS GENMASK(3, 2) /* controller status */
84 #define I2C_SR_CAUSE GENMASK(6, 4) /* Abort cause */
85 #define I2C_SR_TYPE GENMASK(8, 7) /* Receive type */
86 #define I2C_SR_LENGTH GENMASK(19, 9) /* Transfer length */
88 /* Baud-rate counter register (BRCR) */
89 #define I2C_BRCR_BRCNT1 GENMASK(31, 16) /* Baud-rate counter 1 */
90 #define I2C_BRCR_BRCNT2 GENMASK(15, 0) /* Baud-rate counter 2 */
92 /* Interrupt mask set/clear (IMSCR) bits */
93 #define I2C_IT_TXFE BIT(0)
94 #define I2C_IT_TXFNE BIT(1)
95 #define I2C_IT_TXFF BIT(2)
96 #define I2C_IT_TXFOVR BIT(3)
97 #define I2C_IT_RXFE BIT(4)
98 #define I2C_IT_RXFNF BIT(5)
99 #define I2C_IT_RXFF BIT(6)
100 #define I2C_IT_RFSR BIT(16)
101 #define I2C_IT_RFSE BIT(17)
102 #define I2C_IT_WTSR BIT(18)
103 #define I2C_IT_MTD BIT(19)
104 #define I2C_IT_STD BIT(20)
105 #define I2C_IT_MAL BIT(24)
106 #define I2C_IT_BERR BIT(25)
107 #define I2C_IT_MTDWS BIT(28)
109 /* some bits in ICR are reserved */
110 #define I2C_CLEAR_ALL_INTS 0x131f007f
112 /* maximum threshold value */
113 #define MAX_I2C_FIFO_THRESHOLD 15
116 I2C_FREQ_MODE_STANDARD
, /* up to 100 Kb/s */
117 I2C_FREQ_MODE_FAST
, /* up to 400 Kb/s */
118 I2C_FREQ_MODE_HIGH_SPEED
, /* up to 3.4 Mb/s */
119 I2C_FREQ_MODE_FAST_PLUS
, /* up to 1 Mb/s */
122 /* Mobileye EyeQ5 offset into a shared register region (called OLB) */
123 #define NMK_I2C_EYEQ5_OLB_IOCR2 0x0B8
125 enum i2c_eyeq5_speed
{
126 I2C_EYEQ5_SPEED_FAST
,
127 I2C_EYEQ5_SPEED_FAST_PLUS
,
128 I2C_EYEQ5_SPEED_HIGH_SPEED
,
132 * struct i2c_vendor_data - per-vendor variations
133 * @has_mtdws: variant has the MTDWS bit
134 * @fifodepth: variant FIFO depth
136 struct i2c_vendor_data
{
150 I2C_NO_OPERATION
= 0xff,
155 enum i2c_operating_mode
{
158 I2C_OM_MASTER_OR_SLAVE
,
162 * struct i2c_nmk_client - client specific data
163 * @slave_adr: 7-bit slave address
164 * @count: no. bytes to be transferred
165 * @buffer: client data buffer
166 * @xfer_bytes: bytes transferred till now
167 * @operation: current I2C operation
169 struct i2c_nmk_client
{
170 unsigned short slave_adr
;
172 unsigned char *buffer
;
173 unsigned long xfer_bytes
;
174 enum i2c_operation operation
;
178 * struct nmk_i2c_dev - private data structure of the controller.
179 * @vendor: vendor data for this variant.
180 * @adev: parent amba device.
181 * @adap: corresponding I2C adapter.
182 * @irq: interrupt line for the controller.
183 * @virtbase: virtual io memory area.
184 * @clk: hardware i2c block clock.
185 * @cli: holder of client specific data.
186 * @clk_freq: clock frequency for the operation mode
187 * @tft: Tx FIFO Threshold in bytes
188 * @rft: Rx FIFO Threshold in bytes
189 * @timeout_usecs: Slave response timeout
191 * @stop: stop condition.
192 * @xfer_wq: xfer done wait queue.
193 * @xfer_done: xfer done boolean.
194 * @result: controller propogated result.
195 * @has_32b_bus: controller is on a bus that only supports 32-bit accesses.
198 struct i2c_vendor_data
*vendor
;
199 struct amba_device
*adev
;
200 struct i2c_adapter adap
;
202 void __iomem
*virtbase
;
204 struct i2c_nmk_client cli
;
209 enum i2c_freq_mode sm
;
211 struct wait_queue_head xfer_wq
;
217 /* controller's abort causes */
218 static const char *abort_causes
[] = {
219 "no ack received after address transmission",
220 "no ack received during data phase",
221 "ack received after xmission of master code",
222 "master lost arbitration",
225 "overflow, maxsize is 2047 bytes",
228 static inline void i2c_set_bit(void __iomem
*reg
, u32 mask
)
230 writel(readl(reg
) | mask
, reg
);
233 static inline void i2c_clr_bit(void __iomem
*reg
, u32 mask
)
235 writel(readl(reg
) & ~mask
, reg
);
238 static inline u8
nmk_i2c_readb(const struct nmk_i2c_dev
*priv
,
241 if (priv
->has_32b_bus
)
242 return readl(priv
->virtbase
+ reg
);
244 return readb(priv
->virtbase
+ reg
);
247 static inline void nmk_i2c_writeb(const struct nmk_i2c_dev
*priv
, u32 val
,
250 if (priv
->has_32b_bus
)
251 writel(val
, priv
->virtbase
+ reg
);
253 writeb(val
, priv
->virtbase
+ reg
);
257 * flush_i2c_fifo() - This function flushes the I2C FIFO
258 * @priv: private data of I2C Driver
260 * This function flushes the I2C Tx and Rx FIFOs. It returns
261 * 0 on successful flushing of FIFO
263 static int flush_i2c_fifo(struct nmk_i2c_dev
*priv
)
265 #define LOOP_ATTEMPTS 10
270 * flush the transmit and receive FIFO. The flushing
271 * operation takes several cycles before to be completed.
272 * On the completion, the I2C internal logic clears these
273 * bits, until then no one must access Tx, Rx FIFO and
274 * should poll on these bits waiting for the completion.
276 writel((I2C_CR_FTX
| I2C_CR_FRX
), priv
->virtbase
+ I2C_CR
);
278 for (i
= 0; i
< LOOP_ATTEMPTS
; i
++) {
279 timeout
= ktime_add_us(ktime_get(), priv
->timeout_usecs
);
281 while (ktime_after(timeout
, ktime_get())) {
282 if ((readl(priv
->virtbase
+ I2C_CR
) &
283 (I2C_CR_FTX
| I2C_CR_FRX
)) == 0)
288 dev_err(&priv
->adev
->dev
,
289 "flushing operation timed out giving up after %d attempts",
296 * disable_all_interrupts() - Disable all interrupts of this I2c Bus
297 * @priv: private data of I2C Driver
299 static void disable_all_interrupts(struct nmk_i2c_dev
*priv
)
301 writel(0, priv
->virtbase
+ I2C_IMSCR
);
305 * clear_all_interrupts() - Clear all interrupts of I2C Controller
306 * @priv: private data of I2C Driver
308 static void clear_all_interrupts(struct nmk_i2c_dev
*priv
)
310 writel(I2C_CLEAR_ALL_INTS
, priv
->virtbase
+ I2C_ICR
);
314 * init_hw() - initialize the I2C hardware
315 * @priv: private data of I2C Driver
317 static int init_hw(struct nmk_i2c_dev
*priv
)
321 stat
= flush_i2c_fifo(priv
);
325 /* disable the controller */
326 i2c_clr_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
328 disable_all_interrupts(priv
);
330 clear_all_interrupts(priv
);
332 priv
->cli
.operation
= I2C_NO_OPERATION
;
338 /* enable peripheral, master mode operation */
339 #define DEFAULT_I2C_REG_CR (FIELD_PREP(I2C_CR_OM, I2C_OM_MASTER) | I2C_CR_PE)
341 /* grab top three bits from extended I2C addresses */
342 #define ADR_3MSB_BITS GENMASK(9, 7)
345 * load_i2c_mcr_reg() - load the MCR register
346 * @priv: private data of controller
347 * @flags: message flags
349 static u32
load_i2c_mcr_reg(struct nmk_i2c_dev
*priv
, u16 flags
)
352 unsigned short slave_adr_3msb_bits
;
354 mcr
|= FIELD_PREP(I2C_MCR_A7
, priv
->cli
.slave_adr
);
356 if (unlikely(flags
& I2C_M_TEN
)) {
357 /* 10-bit address transaction */
358 mcr
|= FIELD_PREP(I2C_MCR_AM
, 2);
360 * Get the top 3 bits.
361 * EA10 represents extended address in MCR. This includes
362 * the extension (MSB bits) of the 7 bit address loaded
365 slave_adr_3msb_bits
= FIELD_GET(ADR_3MSB_BITS
,
366 priv
->cli
.slave_adr
);
368 mcr
|= FIELD_PREP(I2C_MCR_EA10
, slave_adr_3msb_bits
);
370 /* 7-bit address transaction */
371 mcr
|= FIELD_PREP(I2C_MCR_AM
, 1);
374 /* start byte procedure not applied */
375 mcr
|= FIELD_PREP(I2C_MCR_SB
, 0);
377 /* check the operation, master read/write? */
378 if (priv
->cli
.operation
== I2C_WRITE
)
379 mcr
|= FIELD_PREP(I2C_MCR_OP
, I2C_WRITE
);
381 mcr
|= FIELD_PREP(I2C_MCR_OP
, I2C_READ
);
383 /* stop or repeated start? */
385 mcr
|= FIELD_PREP(I2C_MCR_STOP
, 1);
387 mcr
&= ~FIELD_PREP(I2C_MCR_STOP
, 1);
389 mcr
|= FIELD_PREP(I2C_MCR_LENGTH
, priv
->cli
.count
);
395 * setup_i2c_controller() - setup the controller
396 * @priv: private data of controller
398 static void setup_i2c_controller(struct nmk_i2c_dev
*priv
)
405 writel(0x0, priv
->virtbase
+ I2C_CR
);
406 writel(0x0, priv
->virtbase
+ I2C_HSMCR
);
407 writel(0x0, priv
->virtbase
+ I2C_TFTR
);
408 writel(0x0, priv
->virtbase
+ I2C_RFTR
);
409 writel(0x0, priv
->virtbase
+ I2C_DMAR
);
411 i2c_clk
= clk_get_rate(priv
->clk
);
416 * slsu defines the data setup time after SCL clock
417 * stretching in terms of i2c clk cycles + 1 (zero means
418 * "wait one cycle"), the needed setup time for the three
419 * modes are 250ns, 100ns, 10ns respectively.
421 * As the time for one cycle T in nanoseconds is
422 * T = (1/f) * 1000000000 =>
423 * slsu = cycles / (1000000000 / f) + 1
425 ns
= DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk
);
427 case I2C_FREQ_MODE_FAST
:
428 case I2C_FREQ_MODE_FAST_PLUS
:
429 slsu
= DIV_ROUND_UP(100, ns
); /* Fast */
431 case I2C_FREQ_MODE_HIGH_SPEED
:
432 slsu
= DIV_ROUND_UP(10, ns
); /* High */
434 case I2C_FREQ_MODE_STANDARD
:
436 slsu
= DIV_ROUND_UP(250, ns
); /* Standard */
441 dev_dbg(&priv
->adev
->dev
, "calculated SLSU = %04x\n", slsu
);
442 writel(FIELD_PREP(I2C_SCR_SLSU
, slsu
), priv
->virtbase
+ I2C_SCR
);
445 * The spec says, in case of std. mode the divider is
446 * 2 whereas it is 3 for fast and fastplus mode of
449 div
= (priv
->clk_freq
> I2C_MAX_STANDARD_MODE_FREQ
) ? 3 : 2;
452 * generate the mask for baud rate counters. The controller
453 * has two baud rate counters. One is used for High speed
454 * operation, and the other is for std, fast mode, fast mode
457 * BRCR is a clock divider amount. Pick highest value that
458 * leads to rate strictly below target. Eg when asking for
459 * 400kHz you want a bus rate <=400kHz (and not >=400kHz).
461 brcr
= DIV_ROUND_UP(i2c_clk
, priv
->clk_freq
* div
);
463 if (priv
->sm
== I2C_FREQ_MODE_HIGH_SPEED
)
464 brcr
= FIELD_PREP(I2C_BRCR_BRCNT1
, brcr
);
466 brcr
= FIELD_PREP(I2C_BRCR_BRCNT2
, brcr
);
468 /* set the baud rate counter register */
469 writel(brcr
, priv
->virtbase
+ I2C_BRCR
);
471 /* set the speed mode */
472 writel(FIELD_PREP(I2C_CR_SM
, priv
->sm
), priv
->virtbase
+ I2C_CR
);
474 /* set the Tx and Rx FIFO threshold */
475 writel(priv
->tft
, priv
->virtbase
+ I2C_TFTR
);
476 writel(priv
->rft
, priv
->virtbase
+ I2C_RFTR
);
479 static bool nmk_i2c_wait_xfer_done(struct nmk_i2c_dev
*priv
)
481 if (priv
->timeout_usecs
< jiffies_to_usecs(1)) {
482 unsigned long timeout_usecs
= priv
->timeout_usecs
;
483 ktime_t timeout
= ktime_set(0, timeout_usecs
* NSEC_PER_USEC
);
485 wait_event_hrtimeout(priv
->xfer_wq
, priv
->xfer_done
, timeout
);
487 unsigned long timeout
= usecs_to_jiffies(priv
->timeout_usecs
);
489 wait_event_timeout(priv
->xfer_wq
, priv
->xfer_done
, timeout
);
492 return priv
->xfer_done
;
496 * read_i2c() - Read from I2C client device
497 * @priv: private data of I2C Driver
498 * @flags: message flags
500 * This function reads from i2c client device when controller is in
501 * master mode. There is a completion timeout. If there is no transfer
502 * before timeout error is returned.
504 static int read_i2c(struct nmk_i2c_dev
*priv
, u16 flags
)
510 mcr
= load_i2c_mcr_reg(priv
, flags
);
511 writel(mcr
, priv
->virtbase
+ I2C_MCR
);
513 /* load the current CR value */
514 writel(readl(priv
->virtbase
+ I2C_CR
) | DEFAULT_I2C_REG_CR
,
515 priv
->virtbase
+ I2C_CR
);
517 /* enable the controller */
518 i2c_set_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
520 init_waitqueue_head(&priv
->xfer_wq
);
521 priv
->xfer_done
= false;
523 /* enable interrupts by setting the mask */
524 irq_mask
= (I2C_IT_RXFNF
| I2C_IT_RXFF
|
525 I2C_IT_MAL
| I2C_IT_BERR
);
527 if (priv
->stop
|| !priv
->vendor
->has_mtdws
)
528 irq_mask
|= I2C_IT_MTD
;
530 irq_mask
|= I2C_IT_MTDWS
;
532 irq_mask
&= I2C_CLEAR_ALL_INTS
;
534 writel(readl(priv
->virtbase
+ I2C_IMSCR
) | irq_mask
,
535 priv
->virtbase
+ I2C_IMSCR
);
537 xfer_done
= nmk_i2c_wait_xfer_done(priv
);
545 static void fill_tx_fifo(struct nmk_i2c_dev
*priv
, int no_bytes
)
549 for (count
= (no_bytes
- 2);
551 (priv
->cli
.count
!= 0);
553 /* write to the Tx FIFO */
554 nmk_i2c_writeb(priv
, *priv
->cli
.buffer
, I2C_TFR
);
557 priv
->cli
.xfer_bytes
++;
563 * write_i2c() - Write data to I2C client.
564 * @priv: private data of I2C Driver
565 * @flags: message flags
567 * This function writes data to I2C client
569 static int write_i2c(struct nmk_i2c_dev
*priv
, u16 flags
)
575 mcr
= load_i2c_mcr_reg(priv
, flags
);
577 writel(mcr
, priv
->virtbase
+ I2C_MCR
);
579 /* load the current CR value */
580 writel(readl(priv
->virtbase
+ I2C_CR
) | DEFAULT_I2C_REG_CR
,
581 priv
->virtbase
+ I2C_CR
);
583 /* enable the controller */
584 i2c_set_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
586 init_waitqueue_head(&priv
->xfer_wq
);
587 priv
->xfer_done
= false;
589 /* enable interrupts by settings the masks */
590 irq_mask
= (I2C_IT_TXFOVR
| I2C_IT_MAL
| I2C_IT_BERR
);
592 /* Fill the TX FIFO with transmit data */
593 fill_tx_fifo(priv
, MAX_I2C_FIFO_THRESHOLD
);
595 if (priv
->cli
.count
!= 0)
596 irq_mask
|= I2C_IT_TXFNE
;
599 * check if we want to transfer a single or multiple bytes, if so
600 * set the MTDWS bit (Master Transaction Done Without Stop)
601 * to start repeated start operation
603 if (priv
->stop
|| !priv
->vendor
->has_mtdws
)
604 irq_mask
|= I2C_IT_MTD
;
606 irq_mask
|= I2C_IT_MTDWS
;
608 irq_mask
&= I2C_CLEAR_ALL_INTS
;
610 writel(readl(priv
->virtbase
+ I2C_IMSCR
) | irq_mask
,
611 priv
->virtbase
+ I2C_IMSCR
);
613 xfer_done
= nmk_i2c_wait_xfer_done(priv
);
616 /* Controller timed out */
617 dev_err(&priv
->adev
->dev
, "write to slave 0x%x timed out\n",
618 priv
->cli
.slave_adr
);
626 * nmk_i2c_xfer_one() - transmit a single I2C message
627 * @priv: device with a message encoded into it
628 * @flags: message flags
630 static int nmk_i2c_xfer_one(struct nmk_i2c_dev
*priv
, u16 flags
)
634 if (flags
& I2C_M_RD
) {
636 priv
->cli
.operation
= I2C_READ
;
637 status
= read_i2c(priv
, flags
);
639 /* write operation */
640 priv
->cli
.operation
= I2C_WRITE
;
641 status
= write_i2c(priv
, flags
);
644 if (status
|| priv
->result
) {
648 i2c_sr
= readl(priv
->virtbase
+ I2C_SR
);
649 if (FIELD_GET(I2C_SR_STATUS
, i2c_sr
) == I2C_ABORT
) {
650 cause
= FIELD_GET(I2C_SR_CAUSE
, i2c_sr
);
651 dev_err(&priv
->adev
->dev
, "%s\n",
652 cause
>= ARRAY_SIZE(abort_causes
) ?
654 abort_causes
[cause
]);
659 status
= status
? status
: priv
->result
;
666 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
667 * @i2c_adap: Adapter pointer to the controller
668 * @msgs: Pointer to data to be written.
669 * @num_msgs: Number of messages to be executed
671 * This is the function called by the generic kernel i2c_transfer()
672 * or i2c_smbus...() API calls. Note that this code is protected by the
673 * semaphore set in the kernel i2c_transfer() function.
676 * READ TRANSFER : We impose a restriction of the first message to be the
677 * index message for any read transaction.
678 * - a no index is coded as '0',
679 * - 2byte big endian index is coded as '3'
680 * !!! msg[0].buf holds the actual index.
681 * This is compatible with generic messages of smbus emulator
682 * that send a one byte index.
683 * eg. a I2C transation to read 2 bytes from index 0
685 * msg[0].addr = client->addr;
686 * msg[0].flags = 0x0;
690 * msg[1].addr = client->addr;
691 * msg[1].flags = I2C_M_RD;
693 * msg[1].buf = rd_buff
694 * i2c_transfer(adap, msg, 2);
696 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
697 * If you want to emulate an SMBUS write transaction put the
698 * index as first byte(or first and second) in the payload.
699 * eg. a I2C transation to write 2 bytes from index 1
703 * msg[0].flags = 0x0;
705 * msg[0].buf = wr_buff;
706 * i2c_transfer(adap, msg, 1);
708 * To read or write a block of data (multiple bytes) using SMBUS emulation
709 * please use the i2c_smbus_read_i2c_block_data()
710 * or i2c_smbus_write_i2c_block_data() API
712 static int nmk_i2c_xfer(struct i2c_adapter
*i2c_adap
,
713 struct i2c_msg msgs
[], int num_msgs
)
717 struct nmk_i2c_dev
*priv
= i2c_get_adapdata(i2c_adap
);
720 pm_runtime_get_sync(&priv
->adev
->dev
);
722 /* Attempt three times to send the message queue */
723 for (j
= 0; j
< 3; j
++) {
724 /* setup the i2c controller */
725 setup_i2c_controller(priv
);
727 for (i
= 0; i
< num_msgs
; i
++) {
728 priv
->cli
.slave_adr
= msgs
[i
].addr
;
729 priv
->cli
.buffer
= msgs
[i
].buf
;
730 priv
->cli
.count
= msgs
[i
].len
;
731 priv
->stop
= (i
< (num_msgs
- 1)) ? 0 : 1;
734 status
= nmk_i2c_xfer_one(priv
, msgs
[i
].flags
);
742 pm_runtime_put_sync(&priv
->adev
->dev
);
744 /* return the no. messages processed */
752 * disable_interrupts() - disable the interrupts
753 * @priv: private data of controller
754 * @irq: interrupt number
756 static int disable_interrupts(struct nmk_i2c_dev
*priv
, u32 irq
)
758 irq
&= I2C_CLEAR_ALL_INTS
;
759 writel(readl(priv
->virtbase
+ I2C_IMSCR
) & ~irq
,
760 priv
->virtbase
+ I2C_IMSCR
);
765 * i2c_irq_handler() - interrupt routine
766 * @irq: interrupt number
767 * @arg: data passed to the handler
769 * This is the interrupt handler for the i2c driver. Currently
770 * it handles the major interrupts like Rx & Tx FIFO management
771 * interrupts, master transaction interrupts, arbitration and
772 * bus error interrupts. The rest of the interrupts are treated as
775 static irqreturn_t
i2c_irq_handler(int irq
, void *arg
)
777 struct nmk_i2c_dev
*priv
= arg
;
778 struct device
*dev
= &priv
->adev
->dev
;
783 /* load Tx FIFO and Rx FIFO threshold values */
784 tft
= readl(priv
->virtbase
+ I2C_TFTR
);
785 rft
= readl(priv
->virtbase
+ I2C_RFTR
);
787 /* read interrupt status register */
788 misr
= readl(priv
->virtbase
+ I2C_MISR
);
793 /* Transmit FIFO nearly empty interrupt */
796 if (priv
->cli
.operation
== I2C_READ
) {
798 * in read operation why do we care for writing?
799 * so disable the Transmit FIFO interrupt
801 disable_interrupts(priv
, I2C_IT_TXFNE
);
803 fill_tx_fifo(priv
, (MAX_I2C_FIFO_THRESHOLD
- tft
));
805 * if done, close the transfer by disabling the
806 * corresponding TXFNE interrupt
808 if (priv
->cli
.count
== 0)
809 disable_interrupts(priv
, I2C_IT_TXFNE
);
815 * Rx FIFO nearly full interrupt.
816 * This is set when the numer of entries in Rx FIFO is
817 * greater or equal than the threshold value programmed
821 for (count
= rft
; count
> 0; count
--) {
822 /* Read the Rx FIFO */
823 *priv
->cli
.buffer
= nmk_i2c_readb(priv
, I2C_RFR
);
826 priv
->cli
.count
-= rft
;
827 priv
->cli
.xfer_bytes
+= rft
;
832 for (count
= MAX_I2C_FIFO_THRESHOLD
; count
> 0; count
--) {
833 *priv
->cli
.buffer
= nmk_i2c_readb(priv
, I2C_RFR
);
836 priv
->cli
.count
-= MAX_I2C_FIFO_THRESHOLD
;
837 priv
->cli
.xfer_bytes
+= MAX_I2C_FIFO_THRESHOLD
;
840 /* Master Transaction Done with/without stop */
843 if (priv
->cli
.operation
== I2C_READ
) {
844 while (!(readl(priv
->virtbase
+ I2C_RISR
)
846 if (priv
->cli
.count
== 0)
849 nmk_i2c_readb(priv
, I2C_RFR
);
852 priv
->cli
.xfer_bytes
++;
856 disable_all_interrupts(priv
);
857 clear_all_interrupts(priv
);
859 if (priv
->cli
.count
) {
861 dev_err(dev
, "%lu bytes still remain to be xfered\n",
865 priv
->xfer_done
= true;
866 wake_up(&priv
->xfer_wq
);
871 /* Master Arbitration lost interrupt */
876 i2c_set_bit(priv
->virtbase
+ I2C_ICR
, I2C_IT_MAL
);
877 priv
->xfer_done
= true;
878 wake_up(&priv
->xfer_wq
);
884 * Bus Error interrupt.
885 * This happens when an unexpected start/stop condition occurs
886 * during the transaction.
892 sr
= readl(priv
->virtbase
+ I2C_SR
);
894 if (FIELD_GET(I2C_SR_STATUS
, sr
) == I2C_ABORT
)
897 i2c_set_bit(priv
->virtbase
+ I2C_ICR
, I2C_IT_BERR
);
898 priv
->xfer_done
= true;
899 wake_up(&priv
->xfer_wq
);
905 * Tx FIFO overrun interrupt.
906 * This is set when a write operation in Tx FIFO is performed and
907 * the Tx FIFO is full.
913 dev_err(dev
, "Tx Fifo Over run\n");
914 priv
->xfer_done
= true;
915 wake_up(&priv
->xfer_wq
);
920 /* unhandled interrupts by this driver - TODO*/
928 dev_err(dev
, "unhandled Interrupt\n");
931 dev_err(dev
, "spurious Interrupt..\n");
938 static int nmk_i2c_suspend_late(struct device
*dev
)
942 ret
= pm_runtime_force_suspend(dev
);
946 pinctrl_pm_select_sleep_state(dev
);
950 static int nmk_i2c_resume_early(struct device
*dev
)
952 return pm_runtime_force_resume(dev
);
955 static int nmk_i2c_runtime_suspend(struct device
*dev
)
957 struct amba_device
*adev
= to_amba_device(dev
);
958 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
960 clk_disable_unprepare(priv
->clk
);
961 pinctrl_pm_select_idle_state(dev
);
965 static int nmk_i2c_runtime_resume(struct device
*dev
)
967 struct amba_device
*adev
= to_amba_device(dev
);
968 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
971 ret
= clk_prepare_enable(priv
->clk
);
973 dev_err(dev
, "can't prepare_enable clock\n");
977 pinctrl_pm_select_default_state(dev
);
981 clk_disable_unprepare(priv
->clk
);
982 pinctrl_pm_select_idle_state(dev
);
988 static const struct dev_pm_ops nmk_i2c_pm
= {
989 LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late
, nmk_i2c_resume_early
)
990 RUNTIME_PM_OPS(nmk_i2c_runtime_suspend
, nmk_i2c_runtime_resume
, NULL
)
993 static unsigned int nmk_i2c_functionality(struct i2c_adapter
*adap
)
995 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
;
998 static const struct i2c_algorithm nmk_i2c_algo
= {
999 .master_xfer
= nmk_i2c_xfer
,
1000 .functionality
= nmk_i2c_functionality
1003 static void nmk_i2c_of_probe(struct device_node
*np
,
1004 struct nmk_i2c_dev
*priv
)
1008 /* Default to 100 kHz if no frequency is given in the node */
1009 if (of_property_read_u32(np
, "clock-frequency", &priv
->clk_freq
))
1010 priv
->clk_freq
= I2C_MAX_STANDARD_MODE_FREQ
;
1012 if (priv
->clk_freq
<= I2C_MAX_STANDARD_MODE_FREQ
)
1013 priv
->sm
= I2C_FREQ_MODE_STANDARD
;
1014 else if (priv
->clk_freq
<= I2C_MAX_FAST_MODE_FREQ
)
1015 priv
->sm
= I2C_FREQ_MODE_FAST
;
1016 else if (priv
->clk_freq
<= I2C_MAX_FAST_MODE_PLUS_FREQ
)
1017 priv
->sm
= I2C_FREQ_MODE_FAST_PLUS
;
1019 priv
->sm
= I2C_FREQ_MODE_HIGH_SPEED
;
1020 priv
->tft
= 1; /* Tx FIFO threshold */
1021 priv
->rft
= 8; /* Rx FIFO threshold */
1023 /* Slave response timeout */
1024 if (!of_property_read_u32(np
, "i2c-transfer-timeout-us", &timeout_usecs
))
1025 priv
->timeout_usecs
= timeout_usecs
;
1027 priv
->timeout_usecs
= 200 * USEC_PER_MSEC
;
1030 static const unsigned int nmk_i2c_eyeq5_masks
[] = {
1038 static int nmk_i2c_eyeq5_probe(struct nmk_i2c_dev
*priv
)
1040 struct device
*dev
= &priv
->adev
->dev
;
1041 struct device_node
*np
= dev
->of_node
;
1042 unsigned int mask
, speed_mode
;
1046 olb
= syscon_regmap_lookup_by_phandle_args(np
, "mobileye,olb", 1, &id
);
1048 return PTR_ERR(olb
);
1049 if (id
>= ARRAY_SIZE(nmk_i2c_eyeq5_masks
))
1052 if (priv
->clk_freq
<= 400000)
1053 speed_mode
= I2C_EYEQ5_SPEED_FAST
;
1054 else if (priv
->clk_freq
<= 1000000)
1055 speed_mode
= I2C_EYEQ5_SPEED_FAST_PLUS
;
1057 speed_mode
= I2C_EYEQ5_SPEED_HIGH_SPEED
;
1059 mask
= nmk_i2c_eyeq5_masks
[id
];
1060 regmap_update_bits(olb
, NMK_I2C_EYEQ5_OLB_IOCR2
,
1061 mask
, speed_mode
<< __fls(mask
));
1066 #define NMK_I2C_EYEQ_FLAG_32B_BUS BIT(0)
1067 #define NMK_I2C_EYEQ_FLAG_IS_EYEQ5 BIT(1)
1069 static const struct of_device_id nmk_i2c_eyeq_match_table
[] = {
1071 .compatible
= "mobileye,eyeq5-i2c",
1072 .data
= (void *)(NMK_I2C_EYEQ_FLAG_32B_BUS
| NMK_I2C_EYEQ_FLAG_IS_EYEQ5
),
1075 .compatible
= "mobileye,eyeq6h-i2c",
1076 .data
= (void *)NMK_I2C_EYEQ_FLAG_32B_BUS
,
1080 static int nmk_i2c_probe(struct amba_device
*adev
, const struct amba_id
*id
)
1082 struct i2c_vendor_data
*vendor
= id
->data
;
1083 u32 max_fifo_threshold
= (vendor
->fifodepth
/ 2) - 1;
1084 struct device_node
*np
= adev
->dev
.of_node
;
1085 const struct of_device_id
*match
;
1086 struct device
*dev
= &adev
->dev
;
1087 unsigned long match_flags
= 0;
1088 struct nmk_i2c_dev
*priv
;
1089 struct i2c_adapter
*adap
;
1093 * We do not want to attach a .of_match_table to our amba driver.
1094 * Do not convert to device_get_match_data().
1096 match
= of_match_device(nmk_i2c_eyeq_match_table
, dev
);
1098 match_flags
= (unsigned long)match
->data
;
1100 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1104 priv
->vendor
= vendor
;
1106 priv
->has_32b_bus
= match_flags
& NMK_I2C_EYEQ_FLAG_32B_BUS
;
1107 nmk_i2c_of_probe(np
, priv
);
1109 if (match_flags
& NMK_I2C_EYEQ_FLAG_IS_EYEQ5
) {
1110 ret
= nmk_i2c_eyeq5_probe(priv
);
1112 return dev_err_probe(dev
, ret
, "failed OLB lookup\n");
1115 if (priv
->tft
> max_fifo_threshold
) {
1116 dev_warn(dev
, "requested TX FIFO threshold %u, adjusted down to %u\n",
1117 priv
->tft
, max_fifo_threshold
);
1118 priv
->tft
= max_fifo_threshold
;
1121 if (priv
->rft
> max_fifo_threshold
) {
1122 dev_warn(dev
, "requested RX FIFO threshold %u, adjusted down to %u\n",
1123 priv
->rft
, max_fifo_threshold
);
1124 priv
->rft
= max_fifo_threshold
;
1127 amba_set_drvdata(adev
, priv
);
1129 priv
->virtbase
= devm_ioremap(dev
, adev
->res
.start
,
1130 resource_size(&adev
->res
));
1131 if (!priv
->virtbase
)
1134 priv
->irq
= adev
->irq
[0];
1135 ret
= devm_request_irq(dev
, priv
->irq
, i2c_irq_handler
, 0,
1138 return dev_err_probe(dev
, ret
,
1139 "cannot claim the irq %d\n", priv
->irq
);
1141 priv
->clk
= devm_clk_get_enabled(dev
, NULL
);
1142 if (IS_ERR(priv
->clk
))
1143 return dev_err_probe(dev
, PTR_ERR(priv
->clk
),
1144 "could enable i2c clock\n");
1149 adap
->dev
.of_node
= np
;
1150 adap
->dev
.parent
= dev
;
1151 adap
->owner
= THIS_MODULE
;
1152 adap
->class = I2C_CLASS_DEPRECATED
;
1153 adap
->algo
= &nmk_i2c_algo
;
1154 adap
->timeout
= usecs_to_jiffies(priv
->timeout_usecs
);
1155 snprintf(adap
->name
, sizeof(adap
->name
),
1156 "Nomadik I2C at %pR", &adev
->res
);
1158 i2c_set_adapdata(adap
, priv
);
1161 "initialize %s on virtual base %p\n",
1162 adap
->name
, priv
->virtbase
);
1164 ret
= i2c_add_adapter(adap
);
1168 pm_runtime_put(dev
);
1173 static void nmk_i2c_remove(struct amba_device
*adev
)
1175 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
1177 i2c_del_adapter(&priv
->adap
);
1178 flush_i2c_fifo(priv
);
1179 disable_all_interrupts(priv
);
1180 clear_all_interrupts(priv
);
1181 /* disable the controller */
1182 i2c_clr_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
1185 static struct i2c_vendor_data vendor_stn8815
= {
1187 .fifodepth
= 16, /* Guessed from TFTR/RFTR = 7 */
1190 static struct i2c_vendor_data vendor_db8500
= {
1192 .fifodepth
= 32, /* Guessed from TFTR/RFTR = 15 */
1195 static const struct amba_id nmk_i2c_ids
[] = {
1199 .data
= &vendor_stn8815
,
1204 .data
= &vendor_db8500
,
1209 MODULE_DEVICE_TABLE(amba
, nmk_i2c_ids
);
1211 static struct amba_driver nmk_i2c_driver
= {
1213 .name
= DRIVER_NAME
,
1214 .pm
= pm_ptr(&nmk_i2c_pm
),
1216 .id_table
= nmk_i2c_ids
,
1217 .probe
= nmk_i2c_probe
,
1218 .remove
= nmk_i2c_remove
,
1221 static int __init
nmk_i2c_init(void)
1223 return amba_driver_register(&nmk_i2c_driver
);
1226 static void __exit
nmk_i2c_exit(void)
1228 amba_driver_unregister(&nmk_i2c_driver
);
1231 subsys_initcall(nmk_i2c_init
);
1232 module_exit(nmk_i2c_exit
);
1234 MODULE_AUTHOR("Sachin Verma");
1235 MODULE_AUTHOR("Srinidhi KASAGAR");
1236 MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1237 MODULE_LICENSE("GPL");