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
);
551 static void fill_tx_fifo(struct nmk_i2c_dev
*priv
, int no_bytes
)
555 for (count
= (no_bytes
- 2);
557 (priv
->cli
.count
!= 0);
559 /* write to the Tx FIFO */
560 nmk_i2c_writeb(priv
, *priv
->cli
.buffer
, I2C_TFR
);
563 priv
->cli
.xfer_bytes
++;
569 * write_i2c() - Write data to I2C client.
570 * @priv: private data of I2C Driver
571 * @flags: message flags
573 * This function writes data to I2C client
575 static int write_i2c(struct nmk_i2c_dev
*priv
, u16 flags
)
581 mcr
= load_i2c_mcr_reg(priv
, flags
);
583 writel(mcr
, priv
->virtbase
+ I2C_MCR
);
585 /* load the current CR value */
586 writel(readl(priv
->virtbase
+ I2C_CR
) | DEFAULT_I2C_REG_CR
,
587 priv
->virtbase
+ I2C_CR
);
589 /* enable the controller */
590 i2c_set_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
592 init_waitqueue_head(&priv
->xfer_wq
);
593 priv
->xfer_done
= false;
595 /* enable interrupts by settings the masks */
596 irq_mask
= (I2C_IT_TXFOVR
| I2C_IT_MAL
| I2C_IT_BERR
);
598 /* Fill the TX FIFO with transmit data */
599 fill_tx_fifo(priv
, MAX_I2C_FIFO_THRESHOLD
);
601 if (priv
->cli
.count
!= 0)
602 irq_mask
|= I2C_IT_TXFNE
;
605 * check if we want to transfer a single or multiple bytes, if so
606 * set the MTDWS bit (Master Transaction Done Without Stop)
607 * to start repeated start operation
609 if (priv
->stop
|| !priv
->vendor
->has_mtdws
)
610 irq_mask
|= I2C_IT_MTD
;
612 irq_mask
|= I2C_IT_MTDWS
;
614 irq_mask
&= I2C_CLEAR_ALL_INTS
;
616 writel(readl(priv
->virtbase
+ I2C_IMSCR
) | irq_mask
,
617 priv
->virtbase
+ I2C_IMSCR
);
619 xfer_done
= nmk_i2c_wait_xfer_done(priv
);
622 /* Controller timed out */
623 dev_err(&priv
->adev
->dev
, "write to slave 0x%x timed out\n",
624 priv
->cli
.slave_adr
);
632 * nmk_i2c_xfer_one() - transmit a single I2C message
633 * @priv: device with a message encoded into it
634 * @flags: message flags
636 static int nmk_i2c_xfer_one(struct nmk_i2c_dev
*priv
, u16 flags
)
640 if (flags
& I2C_M_RD
) {
642 priv
->cli
.operation
= I2C_READ
;
643 status
= read_i2c(priv
, flags
);
645 /* write operation */
646 priv
->cli
.operation
= I2C_WRITE
;
647 status
= write_i2c(priv
, flags
);
650 if (status
|| priv
->result
) {
654 i2c_sr
= readl(priv
->virtbase
+ I2C_SR
);
655 if (FIELD_GET(I2C_SR_STATUS
, i2c_sr
) == I2C_ABORT
) {
656 cause
= FIELD_GET(I2C_SR_CAUSE
, i2c_sr
);
657 dev_err(&priv
->adev
->dev
, "%s\n",
658 cause
>= ARRAY_SIZE(abort_causes
) ?
660 abort_causes
[cause
]);
665 status
= status
? status
: priv
->result
;
672 * nmk_i2c_xfer() - I2C transfer function used by kernel framework
673 * @i2c_adap: Adapter pointer to the controller
674 * @msgs: Pointer to data to be written.
675 * @num_msgs: Number of messages to be executed
677 * This is the function called by the generic kernel i2c_transfer()
678 * or i2c_smbus...() API calls. Note that this code is protected by the
679 * semaphore set in the kernel i2c_transfer() function.
682 * READ TRANSFER : We impose a restriction of the first message to be the
683 * index message for any read transaction.
684 * - a no index is coded as '0',
685 * - 2byte big endian index is coded as '3'
686 * !!! msg[0].buf holds the actual index.
687 * This is compatible with generic messages of smbus emulator
688 * that send a one byte index.
689 * eg. a I2C transation to read 2 bytes from index 0
691 * msg[0].addr = client->addr;
692 * msg[0].flags = 0x0;
696 * msg[1].addr = client->addr;
697 * msg[1].flags = I2C_M_RD;
699 * msg[1].buf = rd_buff
700 * i2c_transfer(adap, msg, 2);
702 * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
703 * If you want to emulate an SMBUS write transaction put the
704 * index as first byte(or first and second) in the payload.
705 * eg. a I2C transation to write 2 bytes from index 1
709 * msg[0].flags = 0x0;
711 * msg[0].buf = wr_buff;
712 * i2c_transfer(adap, msg, 1);
714 * To read or write a block of data (multiple bytes) using SMBUS emulation
715 * please use the i2c_smbus_read_i2c_block_data()
716 * or i2c_smbus_write_i2c_block_data() API
718 static int nmk_i2c_xfer(struct i2c_adapter
*i2c_adap
,
719 struct i2c_msg msgs
[], int num_msgs
)
723 struct nmk_i2c_dev
*priv
= i2c_get_adapdata(i2c_adap
);
726 pm_runtime_get_sync(&priv
->adev
->dev
);
728 /* Attempt three times to send the message queue */
729 for (j
= 0; j
< 3; j
++) {
730 /* setup the i2c controller */
731 setup_i2c_controller(priv
);
733 for (i
= 0; i
< num_msgs
; i
++) {
734 priv
->cli
.slave_adr
= msgs
[i
].addr
;
735 priv
->cli
.buffer
= msgs
[i
].buf
;
736 priv
->cli
.count
= msgs
[i
].len
;
737 priv
->stop
= (i
< (num_msgs
- 1)) ? 0 : 1;
740 status
= nmk_i2c_xfer_one(priv
, msgs
[i
].flags
);
748 pm_runtime_put_sync(&priv
->adev
->dev
);
750 /* return the no. messages processed */
758 * disable_interrupts() - disable the interrupts
759 * @priv: private data of controller
760 * @irq: interrupt number
762 static int disable_interrupts(struct nmk_i2c_dev
*priv
, u32 irq
)
764 irq
&= I2C_CLEAR_ALL_INTS
;
765 writel(readl(priv
->virtbase
+ I2C_IMSCR
) & ~irq
,
766 priv
->virtbase
+ I2C_IMSCR
);
771 * i2c_irq_handler() - interrupt routine
772 * @irq: interrupt number
773 * @arg: data passed to the handler
775 * This is the interrupt handler for the i2c driver. Currently
776 * it handles the major interrupts like Rx & Tx FIFO management
777 * interrupts, master transaction interrupts, arbitration and
778 * bus error interrupts. The rest of the interrupts are treated as
781 static irqreturn_t
i2c_irq_handler(int irq
, void *arg
)
783 struct nmk_i2c_dev
*priv
= arg
;
784 struct device
*dev
= &priv
->adev
->dev
;
789 /* load Tx FIFO and Rx FIFO threshold values */
790 tft
= readl(priv
->virtbase
+ I2C_TFTR
);
791 rft
= readl(priv
->virtbase
+ I2C_RFTR
);
793 /* read interrupt status register */
794 misr
= readl(priv
->virtbase
+ I2C_MISR
);
799 /* Transmit FIFO nearly empty interrupt */
802 if (priv
->cli
.operation
== I2C_READ
) {
804 * in read operation why do we care for writing?
805 * so disable the Transmit FIFO interrupt
807 disable_interrupts(priv
, I2C_IT_TXFNE
);
809 fill_tx_fifo(priv
, (MAX_I2C_FIFO_THRESHOLD
- tft
));
811 * if done, close the transfer by disabling the
812 * corresponding TXFNE interrupt
814 if (priv
->cli
.count
== 0)
815 disable_interrupts(priv
, I2C_IT_TXFNE
);
821 * Rx FIFO nearly full interrupt.
822 * This is set when the numer of entries in Rx FIFO is
823 * greater or equal than the threshold value programmed
827 for (count
= rft
; count
> 0; count
--) {
828 /* Read the Rx FIFO */
829 *priv
->cli
.buffer
= nmk_i2c_readb(priv
, I2C_RFR
);
832 priv
->cli
.count
-= rft
;
833 priv
->cli
.xfer_bytes
+= rft
;
838 for (count
= MAX_I2C_FIFO_THRESHOLD
; count
> 0; count
--) {
839 *priv
->cli
.buffer
= nmk_i2c_readb(priv
, I2C_RFR
);
842 priv
->cli
.count
-= MAX_I2C_FIFO_THRESHOLD
;
843 priv
->cli
.xfer_bytes
+= MAX_I2C_FIFO_THRESHOLD
;
846 /* Master Transaction Done with/without stop */
849 if (priv
->cli
.operation
== I2C_READ
) {
850 while (!(readl(priv
->virtbase
+ I2C_RISR
)
852 if (priv
->cli
.count
== 0)
855 nmk_i2c_readb(priv
, I2C_RFR
);
858 priv
->cli
.xfer_bytes
++;
862 disable_all_interrupts(priv
);
863 clear_all_interrupts(priv
);
865 if (priv
->cli
.count
) {
867 dev_err(dev
, "%lu bytes still remain to be xfered\n",
871 priv
->xfer_done
= true;
872 wake_up(&priv
->xfer_wq
);
877 /* Master Arbitration lost interrupt */
882 i2c_set_bit(priv
->virtbase
+ I2C_ICR
, I2C_IT_MAL
);
883 priv
->xfer_done
= true;
884 wake_up(&priv
->xfer_wq
);
890 * Bus Error interrupt.
891 * This happens when an unexpected start/stop condition occurs
892 * during the transaction.
898 sr
= readl(priv
->virtbase
+ I2C_SR
);
900 if (FIELD_GET(I2C_SR_STATUS
, sr
) == I2C_ABORT
)
903 i2c_set_bit(priv
->virtbase
+ I2C_ICR
, I2C_IT_BERR
);
904 priv
->xfer_done
= true;
905 wake_up(&priv
->xfer_wq
);
911 * Tx FIFO overrun interrupt.
912 * This is set when a write operation in Tx FIFO is performed and
913 * the Tx FIFO is full.
919 dev_err(dev
, "Tx Fifo Over run\n");
920 priv
->xfer_done
= true;
921 wake_up(&priv
->xfer_wq
);
926 /* unhandled interrupts by this driver - TODO*/
934 dev_err(dev
, "unhandled Interrupt\n");
937 dev_err(dev
, "spurious Interrupt..\n");
944 static int nmk_i2c_suspend_late(struct device
*dev
)
948 ret
= pm_runtime_force_suspend(dev
);
952 pinctrl_pm_select_sleep_state(dev
);
956 static int nmk_i2c_resume_early(struct device
*dev
)
958 return pm_runtime_force_resume(dev
);
961 static int nmk_i2c_runtime_suspend(struct device
*dev
)
963 struct amba_device
*adev
= to_amba_device(dev
);
964 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
966 clk_disable_unprepare(priv
->clk
);
967 pinctrl_pm_select_idle_state(dev
);
971 static int nmk_i2c_runtime_resume(struct device
*dev
)
973 struct amba_device
*adev
= to_amba_device(dev
);
974 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
977 ret
= clk_prepare_enable(priv
->clk
);
979 dev_err(dev
, "can't prepare_enable clock\n");
983 pinctrl_pm_select_default_state(dev
);
987 clk_disable_unprepare(priv
->clk
);
988 pinctrl_pm_select_idle_state(dev
);
994 static const struct dev_pm_ops nmk_i2c_pm
= {
995 LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late
, nmk_i2c_resume_early
)
996 RUNTIME_PM_OPS(nmk_i2c_runtime_suspend
, nmk_i2c_runtime_resume
, NULL
)
999 static unsigned int nmk_i2c_functionality(struct i2c_adapter
*adap
)
1001 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_10BIT_ADDR
;
1004 static const struct i2c_algorithm nmk_i2c_algo
= {
1005 .master_xfer
= nmk_i2c_xfer
,
1006 .functionality
= nmk_i2c_functionality
1009 static void nmk_i2c_of_probe(struct device_node
*np
,
1010 struct nmk_i2c_dev
*priv
)
1014 /* Default to 100 kHz if no frequency is given in the node */
1015 if (of_property_read_u32(np
, "clock-frequency", &priv
->clk_freq
))
1016 priv
->clk_freq
= I2C_MAX_STANDARD_MODE_FREQ
;
1018 /* This driver only supports 'standard' and 'fast' modes of operation. */
1019 if (priv
->clk_freq
<= I2C_MAX_STANDARD_MODE_FREQ
)
1020 priv
->sm
= I2C_FREQ_MODE_STANDARD
;
1022 priv
->sm
= I2C_FREQ_MODE_FAST
;
1023 priv
->tft
= 1; /* Tx FIFO threshold */
1024 priv
->rft
= 8; /* Rx FIFO threshold */
1026 /* Slave response timeout */
1027 if (!of_property_read_u32(np
, "i2c-transfer-timeout-us", &timeout_usecs
))
1028 priv
->timeout_usecs
= timeout_usecs
;
1030 priv
->timeout_usecs
= 200 * USEC_PER_MSEC
;
1033 static const unsigned int nmk_i2c_eyeq5_masks
[] = {
1041 static int nmk_i2c_eyeq5_probe(struct nmk_i2c_dev
*priv
)
1043 struct device
*dev
= &priv
->adev
->dev
;
1044 struct device_node
*np
= dev
->of_node
;
1045 unsigned int mask
, speed_mode
;
1049 priv
->has_32b_bus
= true;
1051 olb
= syscon_regmap_lookup_by_phandle_args(np
, "mobileye,olb", 1, &id
);
1053 return PTR_ERR(olb
);
1054 if (id
>= ARRAY_SIZE(nmk_i2c_eyeq5_masks
))
1057 if (priv
->clk_freq
<= 400000)
1058 speed_mode
= I2C_EYEQ5_SPEED_FAST
;
1059 else if (priv
->clk_freq
<= 1000000)
1060 speed_mode
= I2C_EYEQ5_SPEED_FAST_PLUS
;
1062 speed_mode
= I2C_EYEQ5_SPEED_HIGH_SPEED
;
1064 mask
= nmk_i2c_eyeq5_masks
[id
];
1065 regmap_update_bits(olb
, NMK_I2C_EYEQ5_OLB_IOCR2
,
1066 mask
, speed_mode
<< __fls(mask
));
1071 static int nmk_i2c_probe(struct amba_device
*adev
, const struct amba_id
*id
)
1074 struct nmk_i2c_dev
*priv
;
1075 struct device_node
*np
= adev
->dev
.of_node
;
1076 struct device
*dev
= &adev
->dev
;
1077 struct i2c_adapter
*adap
;
1078 struct i2c_vendor_data
*vendor
= id
->data
;
1079 u32 max_fifo_threshold
= (vendor
->fifodepth
/ 2) - 1;
1081 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1085 priv
->vendor
= vendor
;
1087 priv
->has_32b_bus
= false;
1088 nmk_i2c_of_probe(np
, priv
);
1090 if (of_device_is_compatible(np
, "mobileye,eyeq5-i2c")) {
1091 ret
= nmk_i2c_eyeq5_probe(priv
);
1093 return dev_err_probe(dev
, ret
, "failed OLB lookup\n");
1096 if (priv
->tft
> max_fifo_threshold
) {
1097 dev_warn(dev
, "requested TX FIFO threshold %u, adjusted down to %u\n",
1098 priv
->tft
, max_fifo_threshold
);
1099 priv
->tft
= max_fifo_threshold
;
1102 if (priv
->rft
> max_fifo_threshold
) {
1103 dev_warn(dev
, "requested RX FIFO threshold %u, adjusted down to %u\n",
1104 priv
->rft
, max_fifo_threshold
);
1105 priv
->rft
= max_fifo_threshold
;
1108 amba_set_drvdata(adev
, priv
);
1110 priv
->virtbase
= devm_ioremap(dev
, adev
->res
.start
,
1111 resource_size(&adev
->res
));
1112 if (!priv
->virtbase
)
1115 priv
->irq
= adev
->irq
[0];
1116 ret
= devm_request_irq(dev
, priv
->irq
, i2c_irq_handler
, 0,
1119 return dev_err_probe(dev
, ret
,
1120 "cannot claim the irq %d\n", priv
->irq
);
1122 priv
->clk
= devm_clk_get_enabled(dev
, NULL
);
1123 if (IS_ERR(priv
->clk
))
1124 return dev_err_probe(dev
, PTR_ERR(priv
->clk
),
1125 "could enable i2c clock\n");
1130 adap
->dev
.of_node
= np
;
1131 adap
->dev
.parent
= dev
;
1132 adap
->owner
= THIS_MODULE
;
1133 adap
->class = I2C_CLASS_DEPRECATED
;
1134 adap
->algo
= &nmk_i2c_algo
;
1135 adap
->timeout
= usecs_to_jiffies(priv
->timeout_usecs
);
1136 snprintf(adap
->name
, sizeof(adap
->name
),
1137 "Nomadik I2C at %pR", &adev
->res
);
1139 i2c_set_adapdata(adap
, priv
);
1142 "initialize %s on virtual base %p\n",
1143 adap
->name
, priv
->virtbase
);
1145 ret
= i2c_add_adapter(adap
);
1149 pm_runtime_put(dev
);
1154 static void nmk_i2c_remove(struct amba_device
*adev
)
1156 struct nmk_i2c_dev
*priv
= amba_get_drvdata(adev
);
1158 i2c_del_adapter(&priv
->adap
);
1159 flush_i2c_fifo(priv
);
1160 disable_all_interrupts(priv
);
1161 clear_all_interrupts(priv
);
1162 /* disable the controller */
1163 i2c_clr_bit(priv
->virtbase
+ I2C_CR
, I2C_CR_PE
);
1166 static struct i2c_vendor_data vendor_stn8815
= {
1168 .fifodepth
= 16, /* Guessed from TFTR/RFTR = 7 */
1171 static struct i2c_vendor_data vendor_db8500
= {
1173 .fifodepth
= 32, /* Guessed from TFTR/RFTR = 15 */
1176 static const struct amba_id nmk_i2c_ids
[] = {
1180 .data
= &vendor_stn8815
,
1185 .data
= &vendor_db8500
,
1190 MODULE_DEVICE_TABLE(amba
, nmk_i2c_ids
);
1192 static struct amba_driver nmk_i2c_driver
= {
1194 .name
= DRIVER_NAME
,
1195 .pm
= pm_ptr(&nmk_i2c_pm
),
1197 .id_table
= nmk_i2c_ids
,
1198 .probe
= nmk_i2c_probe
,
1199 .remove
= nmk_i2c_remove
,
1202 static int __init
nmk_i2c_init(void)
1204 return amba_driver_register(&nmk_i2c_driver
);
1207 static void __exit
nmk_i2c_exit(void)
1209 amba_driver_unregister(&nmk_i2c_driver
);
1212 subsys_initcall(nmk_i2c_init
);
1213 module_exit(nmk_i2c_exit
);
1215 MODULE_AUTHOR("Sachin Verma");
1216 MODULE_AUTHOR("Srinidhi KASAGAR");
1217 MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
1218 MODULE_LICENSE("GPL");