1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/i2c/busses/i2c-tegra.c
5 * Copyright (C) 2010 Google, Inc.
6 * Author: Colin Cross <ccross@android.com>
10 #include <linux/delay.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/reset.h>
27 #define BYTES_PER_FIFO_WORD 4
29 #define I2C_CNFG 0x000
30 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
31 #define I2C_CNFG_PACKET_MODE_EN BIT(10)
32 #define I2C_CNFG_NEW_MASTER_FSM BIT(11)
33 #define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
34 #define I2C_STATUS 0x01C
35 #define I2C_SL_CNFG 0x020
36 #define I2C_SL_CNFG_NACK BIT(1)
37 #define I2C_SL_CNFG_NEWSL BIT(2)
38 #define I2C_SL_ADDR1 0x02c
39 #define I2C_SL_ADDR2 0x030
40 #define I2C_TX_FIFO 0x050
41 #define I2C_RX_FIFO 0x054
42 #define I2C_PACKET_TRANSFER_STATUS 0x058
43 #define I2C_FIFO_CONTROL 0x05c
44 #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
45 #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
46 #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
47 #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
48 #define I2C_FIFO_STATUS 0x060
49 #define I2C_FIFO_STATUS_TX_MASK 0xF0
50 #define I2C_FIFO_STATUS_TX_SHIFT 4
51 #define I2C_FIFO_STATUS_RX_MASK 0x0F
52 #define I2C_FIFO_STATUS_RX_SHIFT 0
53 #define I2C_INT_MASK 0x064
54 #define I2C_INT_STATUS 0x068
55 #define I2C_INT_BUS_CLR_DONE BIT(11)
56 #define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
57 #define I2C_INT_NO_ACK BIT(3)
58 #define I2C_INT_ARBITRATION_LOST BIT(2)
59 #define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
60 #define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
61 #define I2C_CLK_DIVISOR 0x06c
62 #define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
64 #define DVC_CTRL_REG1 0x000
65 #define DVC_CTRL_REG1_INTR_EN BIT(10)
66 #define DVC_CTRL_REG3 0x008
67 #define DVC_CTRL_REG3_SW_PROG BIT(26)
68 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
69 #define DVC_STATUS 0x00c
70 #define DVC_STATUS_I2C_DONE_INTR BIT(30)
72 #define I2C_ERR_NONE 0x00
73 #define I2C_ERR_NO_ACK BIT(0)
74 #define I2C_ERR_ARBITRATION_LOST BIT(1)
75 #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
76 #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
78 #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
79 #define PACKET_HEADER0_PACKET_ID_SHIFT 16
80 #define PACKET_HEADER0_CONT_ID_SHIFT 12
81 #define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
83 #define I2C_HEADER_CONT_ON_NAK BIT(21)
84 #define I2C_HEADER_READ BIT(19)
85 #define I2C_HEADER_10BIT_ADDR BIT(18)
86 #define I2C_HEADER_IE_ENABLE BIT(17)
87 #define I2C_HEADER_REPEAT_START BIT(16)
88 #define I2C_HEADER_CONTINUE_XFER BIT(15)
89 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
91 #define I2C_BUS_CLEAR_CNFG 0x084
92 #define I2C_BC_SCLK_THRESHOLD 9
93 #define I2C_BC_SCLK_THRESHOLD_SHIFT 16
94 #define I2C_BC_STOP_COND BIT(2)
95 #define I2C_BC_TERMINATE BIT(1)
96 #define I2C_BC_ENABLE BIT(0)
97 #define I2C_BUS_CLEAR_STATUS 0x088
98 #define I2C_BC_STATUS BIT(0)
100 #define I2C_CONFIG_LOAD 0x08C
101 #define I2C_MSTR_CONFIG_LOAD BIT(0)
103 #define I2C_CLKEN_OVERRIDE 0x090
104 #define I2C_MST_CORE_CLKEN_OVR BIT(0)
106 #define I2C_CONFIG_LOAD_TIMEOUT 1000000
108 #define I2C_MST_FIFO_CONTROL 0x0b4
109 #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
110 #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
111 #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
112 #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
114 #define I2C_MST_FIFO_STATUS 0x0b8
115 #define I2C_MST_FIFO_STATUS_RX_MASK 0xff
116 #define I2C_MST_FIFO_STATUS_RX_SHIFT 0
117 #define I2C_MST_FIFO_STATUS_TX_MASK 0xff0000
118 #define I2C_MST_FIFO_STATUS_TX_SHIFT 16
120 #define I2C_INTERFACE_TIMING_0 0x94
121 #define I2C_THIGH_SHIFT 8
122 #define I2C_INTERFACE_TIMING_1 0x98
124 #define I2C_STANDARD_MODE 100000
125 #define I2C_FAST_MODE 400000
126 #define I2C_FAST_PLUS_MODE 1000000
128 /* Packet header size in bytes */
129 #define I2C_PACKET_HEADER_SIZE 12
132 * Upto I2C_PIO_MODE_MAX_LEN bytes, controller will use PIO mode,
133 * above this, controller will use DMA to fill FIFO.
134 * MAX PIO len is 20 bytes excluding packet header.
136 #define I2C_PIO_MODE_MAX_LEN 32
139 * msg_end_type: The bus control which need to be send at end of transfer.
140 * @MSG_END_STOP: Send stop pulse at end of transfer.
141 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
142 * @MSG_END_CONTINUE: The following on message is coming and so do not send
143 * stop or repeat start.
147 MSG_END_REPEAT_START
,
152 * struct tegra_i2c_hw_feature : Different HW support on Tegra
153 * @has_continue_xfer_support: Continue transfer supports.
154 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
155 * complete interrupt per packet basis.
156 * @has_single_clk_source: The I2C controller has single clock source. Tegra30
157 * and earlier SoCs have two clock sources i.e. div-clk and
159 * @has_config_load_reg: Has the config load register to load the new
161 * @clk_divisor_hs_mode: Clock divisor in HS mode.
162 * @clk_divisor_std_mode: Clock divisor in standard mode. It is
163 * applicable if there is no fast clock source i.e. single clock
165 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
166 * applicable if there is no fast clock source i.e. single clock
168 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
169 * applicable if there is no fast clock source (i.e. single
171 * @has_multi_master_mode: The I2C controller supports running in single-master
172 * or multi-master mode.
173 * @has_slcg_override_reg: The I2C controller supports a register that
174 * overrides the second level clock gating.
175 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
176 * provides additional features and allows for longer messages to
177 * be transferred in one go.
178 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
179 * allowing 0 length transfers.
180 * @supports_bus_clear: Bus Clear support to recover from bus hang during
181 * SDA stuck low from device for some unknown reasons.
182 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
183 * @tlow_std_mode: Low period of the clock in standard mode.
184 * @thigh_std_mode: High period of the clock in standard mode.
185 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
186 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
187 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
189 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
190 * conditions in fast/fast-plus modes.
191 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
193 * @has_interface_timing_reg: Has interface timing register to program the tuned
196 struct tegra_i2c_hw_feature
{
197 bool has_continue_xfer_support
;
198 bool has_per_pkt_xfer_complete_irq
;
199 bool has_single_clk_source
;
200 bool has_config_load_reg
;
201 int clk_divisor_hs_mode
;
202 int clk_divisor_std_mode
;
203 int clk_divisor_fast_mode
;
204 u16 clk_divisor_fast_plus_mode
;
205 bool has_multi_master_mode
;
206 bool has_slcg_override_reg
;
208 const struct i2c_adapter_quirks
*quirks
;
209 bool supports_bus_clear
;
213 u8 tlow_fast_fastplus_mode
;
214 u8 thigh_fast_fastplus_mode
;
215 u32 setup_hold_time_std_mode
;
216 u32 setup_hold_time_fast_fast_plus_mode
;
217 u32 setup_hold_time_hs_mode
;
218 bool has_interface_timing_reg
;
222 * struct tegra_i2c_dev - per device I2C context
223 * @dev: device reference for power management
224 * @hw: Tegra I2C HW feature
225 * @adapter: core I2C layer adapter information
226 * @div_clk: clock reference for div clock of I2C controller
227 * @fast_clk: clock reference for fast clock of I2C controller
228 * @rst: reset control for the I2C controller
229 * @base: ioremapped registers cookie
230 * @base_phys: physical base address of the I2C controller
231 * @cont_id: I2C controller ID, used for packet header
232 * @irq: IRQ number of transfer complete interrupt
233 * @irq_disabled: used to track whether or not the interrupt is enabled
234 * @is_dvc: identifies the DVC I2C controller, has a different register layout
235 * @msg_complete: transfer completion notifier
236 * @msg_err: error code for completed message
237 * @msg_buf: pointer to current message data
238 * @msg_buf_remaining: size of unsent data in the message buffer
239 * @msg_read: identifies read transfers
240 * @bus_clk_rate: current I2C bus clock rate
241 * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes
242 * @is_multimaster_mode: track if I2C controller is in multi-master mode
243 * @xfer_lock: lock to serialize transfer submission and processing
244 * @tx_dma_chan: DMA transmit channel
245 * @rx_dma_chan: DMA receive channel
246 * @dma_phys: handle to DMA resources
247 * @dma_buf: pointer to allocated DMA buffer
248 * @dma_buf_size: DMA buffer size
249 * @is_curr_dma_xfer: indicates active DMA transfer
250 * @dma_complete: DMA completion notifier
252 struct tegra_i2c_dev
{
254 const struct tegra_i2c_hw_feature
*hw
;
255 struct i2c_adapter adapter
;
257 struct clk
*fast_clk
;
258 struct reset_control
*rst
;
260 phys_addr_t base_phys
;
265 struct completion msg_complete
;
268 size_t msg_buf_remaining
;
271 u16 clk_divisor_non_hs_mode
;
272 bool is_multimaster_mode
;
273 /* xfer_lock: lock to serialize transfer submission and processing */
274 spinlock_t xfer_lock
;
275 struct dma_chan
*tx_dma_chan
;
276 struct dma_chan
*rx_dma_chan
;
279 unsigned int dma_buf_size
;
280 bool is_curr_dma_xfer
;
281 struct completion dma_complete
;
284 static void dvc_writel(struct tegra_i2c_dev
*i2c_dev
, u32 val
,
287 writel(val
, i2c_dev
->base
+ reg
);
290 static u32
dvc_readl(struct tegra_i2c_dev
*i2c_dev
, unsigned long reg
)
292 return readl(i2c_dev
->base
+ reg
);
296 * i2c_writel and i2c_readl will offset the register if necessary to talk
297 * to the I2C block inside the DVC block
299 static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev
*i2c_dev
,
303 reg
+= (reg
>= I2C_TX_FIFO
) ? 0x10 : 0x40;
307 static void i2c_writel(struct tegra_i2c_dev
*i2c_dev
, u32 val
,
310 writel(val
, i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
));
312 /* Read back register to make sure that register writes completed */
313 if (reg
!= I2C_TX_FIFO
)
314 readl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
));
317 static u32
i2c_readl(struct tegra_i2c_dev
*i2c_dev
, unsigned long reg
)
319 return readl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
));
322 static void i2c_writesl(struct tegra_i2c_dev
*i2c_dev
, void *data
,
323 unsigned long reg
, int len
)
325 writesl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
), data
, len
);
328 static void i2c_readsl(struct tegra_i2c_dev
*i2c_dev
, void *data
,
329 unsigned long reg
, int len
)
331 readsl(i2c_dev
->base
+ tegra_i2c_reg_addr(i2c_dev
, reg
), data
, len
);
334 static void tegra_i2c_mask_irq(struct tegra_i2c_dev
*i2c_dev
, u32 mask
)
338 int_mask
= i2c_readl(i2c_dev
, I2C_INT_MASK
) & ~mask
;
339 i2c_writel(i2c_dev
, int_mask
, I2C_INT_MASK
);
342 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev
*i2c_dev
, u32 mask
)
346 int_mask
= i2c_readl(i2c_dev
, I2C_INT_MASK
) | mask
;
347 i2c_writel(i2c_dev
, int_mask
, I2C_INT_MASK
);
350 static void tegra_i2c_dma_complete(void *args
)
352 struct tegra_i2c_dev
*i2c_dev
= args
;
354 complete(&i2c_dev
->dma_complete
);
357 static int tegra_i2c_dma_submit(struct tegra_i2c_dev
*i2c_dev
, size_t len
)
359 struct dma_async_tx_descriptor
*dma_desc
;
360 enum dma_transfer_direction dir
;
361 struct dma_chan
*chan
;
363 dev_dbg(i2c_dev
->dev
, "starting DMA for length: %zu\n", len
);
364 reinit_completion(&i2c_dev
->dma_complete
);
365 dir
= i2c_dev
->msg_read
? DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
;
366 chan
= i2c_dev
->msg_read
? i2c_dev
->rx_dma_chan
: i2c_dev
->tx_dma_chan
;
367 dma_desc
= dmaengine_prep_slave_single(chan
, i2c_dev
->dma_phys
,
368 len
, dir
, DMA_PREP_INTERRUPT
|
371 dev_err(i2c_dev
->dev
, "failed to get DMA descriptor\n");
375 dma_desc
->callback
= tegra_i2c_dma_complete
;
376 dma_desc
->callback_param
= i2c_dev
;
377 dmaengine_submit(dma_desc
);
378 dma_async_issue_pending(chan
);
382 static void tegra_i2c_release_dma(struct tegra_i2c_dev
*i2c_dev
)
384 if (i2c_dev
->dma_buf
) {
385 dma_free_coherent(i2c_dev
->dev
, i2c_dev
->dma_buf_size
,
386 i2c_dev
->dma_buf
, i2c_dev
->dma_phys
);
387 i2c_dev
->dma_buf
= NULL
;
390 if (i2c_dev
->tx_dma_chan
) {
391 dma_release_channel(i2c_dev
->tx_dma_chan
);
392 i2c_dev
->tx_dma_chan
= NULL
;
395 if (i2c_dev
->rx_dma_chan
) {
396 dma_release_channel(i2c_dev
->rx_dma_chan
);
397 i2c_dev
->rx_dma_chan
= NULL
;
401 static int tegra_i2c_init_dma(struct tegra_i2c_dev
*i2c_dev
)
403 struct dma_chan
*chan
;
408 if (!i2c_dev
->hw
->has_apb_dma
)
411 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA
)) {
412 dev_dbg(i2c_dev
->dev
, "Support for APB DMA not enabled!\n");
416 chan
= dma_request_chan(i2c_dev
->dev
, "rx");
422 i2c_dev
->rx_dma_chan
= chan
;
424 chan
= dma_request_chan(i2c_dev
->dev
, "tx");
430 i2c_dev
->tx_dma_chan
= chan
;
432 dma_buf
= dma_alloc_coherent(i2c_dev
->dev
, i2c_dev
->dma_buf_size
,
433 &dma_phys
, GFP_KERNEL
| __GFP_NOWARN
);
435 dev_err(i2c_dev
->dev
, "failed to allocate the DMA buffer\n");
440 i2c_dev
->dma_buf
= dma_buf
;
441 i2c_dev
->dma_phys
= dma_phys
;
445 tegra_i2c_release_dma(i2c_dev
);
446 if (err
!= -EPROBE_DEFER
) {
447 dev_err(i2c_dev
->dev
, "cannot use DMA: %d\n", err
);
448 dev_err(i2c_dev
->dev
, "falling back to PIO\n");
455 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev
*i2c_dev
)
457 unsigned long timeout
= jiffies
+ HZ
;
461 if (i2c_dev
->hw
->has_mst_fifo
) {
462 mask
= I2C_MST_FIFO_CONTROL_TX_FLUSH
|
463 I2C_MST_FIFO_CONTROL_RX_FLUSH
;
464 offset
= I2C_MST_FIFO_CONTROL
;
466 mask
= I2C_FIFO_CONTROL_TX_FLUSH
|
467 I2C_FIFO_CONTROL_RX_FLUSH
;
468 offset
= I2C_FIFO_CONTROL
;
471 val
= i2c_readl(i2c_dev
, offset
);
473 i2c_writel(i2c_dev
, val
, offset
);
475 while (i2c_readl(i2c_dev
, offset
) & mask
) {
476 if (time_after(jiffies
, timeout
)) {
477 dev_warn(i2c_dev
->dev
, "timeout waiting for fifo flush\n");
480 usleep_range(1000, 2000);
485 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev
*i2c_dev
)
489 u8
*buf
= i2c_dev
->msg_buf
;
490 size_t buf_remaining
= i2c_dev
->msg_buf_remaining
;
491 int words_to_transfer
;
494 * Catch overflow due to message fully sent
495 * before the check for RX FIFO availability.
497 if (WARN_ON_ONCE(!(i2c_dev
->msg_buf_remaining
)))
500 if (i2c_dev
->hw
->has_mst_fifo
) {
501 val
= i2c_readl(i2c_dev
, I2C_MST_FIFO_STATUS
);
502 rx_fifo_avail
= (val
& I2C_MST_FIFO_STATUS_RX_MASK
) >>
503 I2C_MST_FIFO_STATUS_RX_SHIFT
;
505 val
= i2c_readl(i2c_dev
, I2C_FIFO_STATUS
);
506 rx_fifo_avail
= (val
& I2C_FIFO_STATUS_RX_MASK
) >>
507 I2C_FIFO_STATUS_RX_SHIFT
;
510 /* Rounds down to not include partial word at the end of buf */
511 words_to_transfer
= buf_remaining
/ BYTES_PER_FIFO_WORD
;
512 if (words_to_transfer
> rx_fifo_avail
)
513 words_to_transfer
= rx_fifo_avail
;
515 i2c_readsl(i2c_dev
, buf
, I2C_RX_FIFO
, words_to_transfer
);
517 buf
+= words_to_transfer
* BYTES_PER_FIFO_WORD
;
518 buf_remaining
-= words_to_transfer
* BYTES_PER_FIFO_WORD
;
519 rx_fifo_avail
-= words_to_transfer
;
522 * If there is a partial word at the end of buf, handle it manually to
523 * prevent overwriting past the end of buf
525 if (rx_fifo_avail
> 0 && buf_remaining
> 0) {
527 * buf_remaining > 3 check not needed as rx_fifo_avail == 0
528 * when (words_to_transfer was > rx_fifo_avail) earlier
531 val
= i2c_readl(i2c_dev
, I2C_RX_FIFO
);
532 val
= cpu_to_le32(val
);
533 memcpy(buf
, &val
, buf_remaining
);
538 /* RX FIFO must be drained, otherwise it's an Overflow case. */
539 if (WARN_ON_ONCE(rx_fifo_avail
))
542 i2c_dev
->msg_buf_remaining
= buf_remaining
;
543 i2c_dev
->msg_buf
= buf
;
548 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev
*i2c_dev
)
552 u8
*buf
= i2c_dev
->msg_buf
;
553 size_t buf_remaining
= i2c_dev
->msg_buf_remaining
;
554 int words_to_transfer
;
556 if (i2c_dev
->hw
->has_mst_fifo
) {
557 val
= i2c_readl(i2c_dev
, I2C_MST_FIFO_STATUS
);
558 tx_fifo_avail
= (val
& I2C_MST_FIFO_STATUS_TX_MASK
) >>
559 I2C_MST_FIFO_STATUS_TX_SHIFT
;
561 val
= i2c_readl(i2c_dev
, I2C_FIFO_STATUS
);
562 tx_fifo_avail
= (val
& I2C_FIFO_STATUS_TX_MASK
) >>
563 I2C_FIFO_STATUS_TX_SHIFT
;
566 /* Rounds down to not include partial word at the end of buf */
567 words_to_transfer
= buf_remaining
/ BYTES_PER_FIFO_WORD
;
569 /* It's very common to have < 4 bytes, so optimize that case. */
570 if (words_to_transfer
) {
571 if (words_to_transfer
> tx_fifo_avail
)
572 words_to_transfer
= tx_fifo_avail
;
575 * Update state before writing to FIFO. If this casues us
576 * to finish writing all bytes (AKA buf_remaining goes to 0) we
577 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
578 * not maskable). We need to make sure that the isr sees
579 * buf_remaining as 0 and doesn't call us back re-entrantly.
581 buf_remaining
-= words_to_transfer
* BYTES_PER_FIFO_WORD
;
582 tx_fifo_avail
-= words_to_transfer
;
583 i2c_dev
->msg_buf_remaining
= buf_remaining
;
584 i2c_dev
->msg_buf
= buf
+
585 words_to_transfer
* BYTES_PER_FIFO_WORD
;
588 i2c_writesl(i2c_dev
, buf
, I2C_TX_FIFO
, words_to_transfer
);
590 buf
+= words_to_transfer
* BYTES_PER_FIFO_WORD
;
594 * If there is a partial word at the end of buf, handle it manually to
595 * prevent reading past the end of buf, which could cross a page
596 * boundary and fault.
598 if (tx_fifo_avail
> 0 && buf_remaining
> 0) {
600 * buf_remaining > 3 check not needed as tx_fifo_avail == 0
601 * when (words_to_transfer was > tx_fifo_avail) earlier
602 * in this function for non-zero words_to_transfer.
604 memcpy(&val
, buf
, buf_remaining
);
605 val
= le32_to_cpu(val
);
607 /* Again update before writing to FIFO to make sure isr sees. */
608 i2c_dev
->msg_buf_remaining
= 0;
609 i2c_dev
->msg_buf
= NULL
;
612 i2c_writel(i2c_dev
, val
, I2C_TX_FIFO
);
619 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
620 * block. This block is identical to the rest of the I2C blocks, except that
621 * it only supports master mode, it has registers moved around, and it needs
622 * some extra init to get it into I2C mode. The register moves are handled
623 * by i2c_readl and i2c_writel
625 static void tegra_dvc_init(struct tegra_i2c_dev
*i2c_dev
)
629 val
= dvc_readl(i2c_dev
, DVC_CTRL_REG3
);
630 val
|= DVC_CTRL_REG3_SW_PROG
;
631 val
|= DVC_CTRL_REG3_I2C_DONE_INTR_EN
;
632 dvc_writel(i2c_dev
, val
, DVC_CTRL_REG3
);
634 val
= dvc_readl(i2c_dev
, DVC_CTRL_REG1
);
635 val
|= DVC_CTRL_REG1_INTR_EN
;
636 dvc_writel(i2c_dev
, val
, DVC_CTRL_REG1
);
639 static int __maybe_unused
tegra_i2c_runtime_resume(struct device
*dev
)
641 struct tegra_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
644 ret
= pinctrl_pm_select_default_state(i2c_dev
->dev
);
648 if (!i2c_dev
->hw
->has_single_clk_source
) {
649 ret
= clk_enable(i2c_dev
->fast_clk
);
651 dev_err(i2c_dev
->dev
,
652 "Enabling fast clk failed, err %d\n", ret
);
657 ret
= clk_enable(i2c_dev
->div_clk
);
659 dev_err(i2c_dev
->dev
,
660 "Enabling div clk failed, err %d\n", ret
);
661 clk_disable(i2c_dev
->fast_clk
);
668 static int __maybe_unused
tegra_i2c_runtime_suspend(struct device
*dev
)
670 struct tegra_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
672 clk_disable(i2c_dev
->div_clk
);
673 if (!i2c_dev
->hw
->has_single_clk_source
)
674 clk_disable(i2c_dev
->fast_clk
);
676 return pinctrl_pm_select_idle_state(i2c_dev
->dev
);
679 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev
*i2c_dev
)
681 unsigned long reg_offset
;
686 if (i2c_dev
->hw
->has_config_load_reg
) {
687 reg_offset
= tegra_i2c_reg_addr(i2c_dev
, I2C_CONFIG_LOAD
);
688 addr
= i2c_dev
->base
+ reg_offset
;
689 i2c_writel(i2c_dev
, I2C_MSTR_CONFIG_LOAD
, I2C_CONFIG_LOAD
);
691 err
= readl_poll_timeout_atomic(addr
, val
, val
== 0,
693 I2C_CONFIG_LOAD_TIMEOUT
);
695 err
= readl_poll_timeout(addr
, val
, val
== 0, 1000,
696 I2C_CONFIG_LOAD_TIMEOUT
);
699 dev_warn(i2c_dev
->dev
,
700 "timeout waiting for config load\n");
708 static int tegra_i2c_init(struct tegra_i2c_dev
*i2c_dev
, bool clk_reinit
)
712 u32 clk_divisor
, clk_multiplier
;
716 reset_control_assert(i2c_dev
->rst
);
718 reset_control_deassert(i2c_dev
->rst
);
721 tegra_dvc_init(i2c_dev
);
723 val
= I2C_CNFG_NEW_MASTER_FSM
| I2C_CNFG_PACKET_MODE_EN
|
724 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT
);
726 if (i2c_dev
->hw
->has_multi_master_mode
)
727 val
|= I2C_CNFG_MULTI_MASTER_MODE
;
729 i2c_writel(i2c_dev
, val
, I2C_CNFG
);
730 i2c_writel(i2c_dev
, 0, I2C_INT_MASK
);
732 /* Make sure clock divisor programmed correctly */
733 clk_divisor
= i2c_dev
->hw
->clk_divisor_hs_mode
;
734 clk_divisor
|= i2c_dev
->clk_divisor_non_hs_mode
<<
735 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT
;
736 i2c_writel(i2c_dev
, clk_divisor
, I2C_CLK_DIVISOR
);
738 if (i2c_dev
->bus_clk_rate
> I2C_STANDARD_MODE
&&
739 i2c_dev
->bus_clk_rate
<= I2C_FAST_PLUS_MODE
) {
740 tlow
= i2c_dev
->hw
->tlow_fast_fastplus_mode
;
741 thigh
= i2c_dev
->hw
->thigh_fast_fastplus_mode
;
742 tsu_thd
= i2c_dev
->hw
->setup_hold_time_fast_fast_plus_mode
;
744 tlow
= i2c_dev
->hw
->tlow_std_mode
;
745 thigh
= i2c_dev
->hw
->thigh_std_mode
;
746 tsu_thd
= i2c_dev
->hw
->setup_hold_time_std_mode
;
749 if (i2c_dev
->hw
->has_interface_timing_reg
) {
750 val
= (thigh
<< I2C_THIGH_SHIFT
) | tlow
;
751 i2c_writel(i2c_dev
, val
, I2C_INTERFACE_TIMING_0
);
755 * configure setup and hold times only when tsu_thd is non-zero.
756 * otherwise, preserve the chip default values
758 if (i2c_dev
->hw
->has_interface_timing_reg
&& tsu_thd
)
759 i2c_writel(i2c_dev
, tsu_thd
, I2C_INTERFACE_TIMING_1
);
762 clk_multiplier
= (tlow
+ thigh
+ 2);
763 clk_multiplier
*= (i2c_dev
->clk_divisor_non_hs_mode
+ 1);
764 err
= clk_set_rate(i2c_dev
->div_clk
,
765 i2c_dev
->bus_clk_rate
* clk_multiplier
);
767 dev_err(i2c_dev
->dev
,
768 "failed changing clock rate: %d\n", err
);
773 if (!i2c_dev
->is_dvc
) {
774 u32 sl_cfg
= i2c_readl(i2c_dev
, I2C_SL_CNFG
);
776 sl_cfg
|= I2C_SL_CNFG_NACK
| I2C_SL_CNFG_NEWSL
;
777 i2c_writel(i2c_dev
, sl_cfg
, I2C_SL_CNFG
);
778 i2c_writel(i2c_dev
, 0xfc, I2C_SL_ADDR1
);
779 i2c_writel(i2c_dev
, 0x00, I2C_SL_ADDR2
);
782 err
= tegra_i2c_flush_fifos(i2c_dev
);
786 if (i2c_dev
->is_multimaster_mode
&& i2c_dev
->hw
->has_slcg_override_reg
)
787 i2c_writel(i2c_dev
, I2C_MST_CORE_CLKEN_OVR
, I2C_CLKEN_OVERRIDE
);
789 err
= tegra_i2c_wait_for_config_load(i2c_dev
);
793 if (i2c_dev
->irq_disabled
) {
794 i2c_dev
->irq_disabled
= false;
795 enable_irq(i2c_dev
->irq
);
801 static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev
*i2c_dev
)
806 * NACK interrupt is generated before the I2C controller generates
807 * the STOP condition on the bus. So wait for 2 clock periods
808 * before disabling the controller so that the STOP condition has
809 * been delivered properly.
811 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev
->bus_clk_rate
));
813 cnfg
= i2c_readl(i2c_dev
, I2C_CNFG
);
814 if (cnfg
& I2C_CNFG_PACKET_MODE_EN
)
815 i2c_writel(i2c_dev
, cnfg
& ~I2C_CNFG_PACKET_MODE_EN
, I2C_CNFG
);
817 return tegra_i2c_wait_for_config_load(i2c_dev
);
820 static irqreturn_t
tegra_i2c_isr(int irq
, void *dev_id
)
823 const u32 status_err
= I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
;
824 struct tegra_i2c_dev
*i2c_dev
= dev_id
;
826 status
= i2c_readl(i2c_dev
, I2C_INT_STATUS
);
828 spin_lock(&i2c_dev
->xfer_lock
);
830 dev_warn(i2c_dev
->dev
, "irq status 0 %08x %08x %08x\n",
831 i2c_readl(i2c_dev
, I2C_PACKET_TRANSFER_STATUS
),
832 i2c_readl(i2c_dev
, I2C_STATUS
),
833 i2c_readl(i2c_dev
, I2C_CNFG
));
834 i2c_dev
->msg_err
|= I2C_ERR_UNKNOWN_INTERRUPT
;
836 if (!i2c_dev
->irq_disabled
) {
837 disable_irq_nosync(i2c_dev
->irq
);
838 i2c_dev
->irq_disabled
= true;
843 if (unlikely(status
& status_err
)) {
844 tegra_i2c_disable_packet_mode(i2c_dev
);
845 if (status
& I2C_INT_NO_ACK
)
846 i2c_dev
->msg_err
|= I2C_ERR_NO_ACK
;
847 if (status
& I2C_INT_ARBITRATION_LOST
)
848 i2c_dev
->msg_err
|= I2C_ERR_ARBITRATION_LOST
;
853 * I2C transfer is terminated during the bus clear so skip
854 * processing the other interrupts.
856 if (i2c_dev
->hw
->supports_bus_clear
&& (status
& I2C_INT_BUS_CLR_DONE
))
859 if (!i2c_dev
->is_curr_dma_xfer
) {
860 if (i2c_dev
->msg_read
&& (status
& I2C_INT_RX_FIFO_DATA_REQ
)) {
861 if (tegra_i2c_empty_rx_fifo(i2c_dev
)) {
863 * Overflow error condition: message fully sent,
864 * with no XFER_COMPLETE interrupt but hardware
865 * asks to transfer more.
867 i2c_dev
->msg_err
|= I2C_ERR_RX_BUFFER_OVERFLOW
;
872 if (!i2c_dev
->msg_read
&& (status
& I2C_INT_TX_FIFO_DATA_REQ
)) {
873 if (i2c_dev
->msg_buf_remaining
)
874 tegra_i2c_fill_tx_fifo(i2c_dev
);
876 tegra_i2c_mask_irq(i2c_dev
,
877 I2C_INT_TX_FIFO_DATA_REQ
);
881 i2c_writel(i2c_dev
, status
, I2C_INT_STATUS
);
883 dvc_writel(i2c_dev
, DVC_STATUS_I2C_DONE_INTR
, DVC_STATUS
);
886 * During message read XFER_COMPLETE interrupt is triggered prior to
887 * DMA completion and during message write XFER_COMPLETE interrupt is
888 * triggered after DMA completion.
889 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
890 * so forcing msg_buf_remaining to 0 in DMA mode.
892 if (status
& I2C_INT_PACKET_XFER_COMPLETE
) {
893 if (i2c_dev
->is_curr_dma_xfer
)
894 i2c_dev
->msg_buf_remaining
= 0;
896 * Underflow error condition: XFER_COMPLETE before message
899 if (WARN_ON_ONCE(i2c_dev
->msg_buf_remaining
)) {
900 i2c_dev
->msg_err
|= I2C_ERR_UNKNOWN_INTERRUPT
;
903 complete(&i2c_dev
->msg_complete
);
907 /* An error occurred, mask all interrupts */
908 tegra_i2c_mask_irq(i2c_dev
, I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
|
909 I2C_INT_PACKET_XFER_COMPLETE
| I2C_INT_TX_FIFO_DATA_REQ
|
910 I2C_INT_RX_FIFO_DATA_REQ
);
911 if (i2c_dev
->hw
->supports_bus_clear
)
912 tegra_i2c_mask_irq(i2c_dev
, I2C_INT_BUS_CLR_DONE
);
913 i2c_writel(i2c_dev
, status
, I2C_INT_STATUS
);
915 dvc_writel(i2c_dev
, DVC_STATUS_I2C_DONE_INTR
, DVC_STATUS
);
917 if (i2c_dev
->is_curr_dma_xfer
) {
918 if (i2c_dev
->msg_read
)
919 dmaengine_terminate_async(i2c_dev
->rx_dma_chan
);
921 dmaengine_terminate_async(i2c_dev
->tx_dma_chan
);
923 complete(&i2c_dev
->dma_complete
);
926 complete(&i2c_dev
->msg_complete
);
928 spin_unlock(&i2c_dev
->xfer_lock
);
932 static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev
*i2c_dev
,
937 struct dma_slave_config slv_config
= {0};
938 struct dma_chan
*chan
;
940 unsigned long reg_offset
;
942 if (i2c_dev
->hw
->has_mst_fifo
)
943 reg
= I2C_MST_FIFO_CONTROL
;
945 reg
= I2C_FIFO_CONTROL
;
947 if (i2c_dev
->is_curr_dma_xfer
) {
955 if (i2c_dev
->msg_read
) {
956 chan
= i2c_dev
->rx_dma_chan
;
957 reg_offset
= tegra_i2c_reg_addr(i2c_dev
, I2C_RX_FIFO
);
958 slv_config
.src_addr
= i2c_dev
->base_phys
+ reg_offset
;
959 slv_config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
960 slv_config
.src_maxburst
= dma_burst
;
962 if (i2c_dev
->hw
->has_mst_fifo
)
963 val
= I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst
);
965 val
= I2C_FIFO_CONTROL_RX_TRIG(dma_burst
);
967 chan
= i2c_dev
->tx_dma_chan
;
968 reg_offset
= tegra_i2c_reg_addr(i2c_dev
, I2C_TX_FIFO
);
969 slv_config
.dst_addr
= i2c_dev
->base_phys
+ reg_offset
;
970 slv_config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
971 slv_config
.dst_maxburst
= dma_burst
;
973 if (i2c_dev
->hw
->has_mst_fifo
)
974 val
= I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst
);
976 val
= I2C_FIFO_CONTROL_TX_TRIG(dma_burst
);
979 slv_config
.device_fc
= true;
980 ret
= dmaengine_slave_config(chan
, &slv_config
);
982 dev_err(i2c_dev
->dev
, "DMA slave config failed: %d\n",
984 dev_err(i2c_dev
->dev
, "falling back to PIO\n");
985 tegra_i2c_release_dma(i2c_dev
);
986 i2c_dev
->is_curr_dma_xfer
= false;
992 if (i2c_dev
->hw
->has_mst_fifo
)
993 val
= I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
994 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
996 val
= I2C_FIFO_CONTROL_TX_TRIG(8) |
997 I2C_FIFO_CONTROL_RX_TRIG(1);
999 i2c_writel(i2c_dev
, val
, reg
);
1002 static int tegra_i2c_issue_bus_clear(struct i2c_adapter
*adap
)
1004 struct tegra_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
1006 unsigned long time_left
;
1009 reinit_completion(&i2c_dev
->msg_complete
);
1010 reg
= (I2C_BC_SCLK_THRESHOLD
<< I2C_BC_SCLK_THRESHOLD_SHIFT
) |
1011 I2C_BC_STOP_COND
| I2C_BC_TERMINATE
;
1012 i2c_writel(i2c_dev
, reg
, I2C_BUS_CLEAR_CNFG
);
1013 if (i2c_dev
->hw
->has_config_load_reg
) {
1014 err
= tegra_i2c_wait_for_config_load(i2c_dev
);
1019 reg
|= I2C_BC_ENABLE
;
1020 i2c_writel(i2c_dev
, reg
, I2C_BUS_CLEAR_CNFG
);
1021 tegra_i2c_unmask_irq(i2c_dev
, I2C_INT_BUS_CLR_DONE
);
1023 time_left
= wait_for_completion_timeout(&i2c_dev
->msg_complete
,
1024 msecs_to_jiffies(50));
1025 if (time_left
== 0) {
1026 dev_err(i2c_dev
->dev
, "timed out for bus clear\n");
1030 reg
= i2c_readl(i2c_dev
, I2C_BUS_CLEAR_STATUS
);
1031 if (!(reg
& I2C_BC_STATUS
)) {
1032 dev_err(i2c_dev
->dev
,
1033 "un-recovered arbitration lost\n");
1040 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev
*i2c_dev
,
1041 struct i2c_msg
*msg
,
1042 enum msg_end_type end_state
)
1046 unsigned long time_left
;
1047 unsigned long flags
;
1052 u16 xfer_time
= 100;
1054 tegra_i2c_flush_fifos(i2c_dev
);
1056 i2c_dev
->msg_buf
= msg
->buf
;
1057 i2c_dev
->msg_buf_remaining
= msg
->len
;
1058 i2c_dev
->msg_err
= I2C_ERR_NONE
;
1059 i2c_dev
->msg_read
= (msg
->flags
& I2C_M_RD
);
1060 reinit_completion(&i2c_dev
->msg_complete
);
1062 if (i2c_dev
->msg_read
)
1063 xfer_size
= msg
->len
;
1065 xfer_size
= msg
->len
+ I2C_PACKET_HEADER_SIZE
;
1067 xfer_size
= ALIGN(xfer_size
, BYTES_PER_FIFO_WORD
);
1068 i2c_dev
->is_curr_dma_xfer
= (xfer_size
> I2C_PIO_MODE_MAX_LEN
) &&
1070 tegra_i2c_config_fifo_trig(i2c_dev
, xfer_size
);
1071 dma
= i2c_dev
->is_curr_dma_xfer
;
1073 * Transfer time in mSec = Total bits / transfer rate
1074 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1076 xfer_time
+= DIV_ROUND_CLOSEST(((xfer_size
* 9) + 2) * MSEC_PER_SEC
,
1077 i2c_dev
->bus_clk_rate
);
1078 spin_lock_irqsave(&i2c_dev
->xfer_lock
, flags
);
1080 int_mask
= I2C_INT_NO_ACK
| I2C_INT_ARBITRATION_LOST
;
1081 tegra_i2c_unmask_irq(i2c_dev
, int_mask
);
1083 if (i2c_dev
->msg_read
) {
1084 dma_sync_single_for_device(i2c_dev
->dev
,
1088 err
= tegra_i2c_dma_submit(i2c_dev
, xfer_size
);
1090 dev_err(i2c_dev
->dev
,
1091 "starting RX DMA failed, err %d\n",
1097 dma_sync_single_for_cpu(i2c_dev
->dev
,
1101 buffer
= i2c_dev
->dma_buf
;
1105 packet_header
= (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT
) |
1106 PACKET_HEADER0_PROTOCOL_I2C
|
1107 (i2c_dev
->cont_id
<< PACKET_HEADER0_CONT_ID_SHIFT
) |
1108 (1 << PACKET_HEADER0_PACKET_ID_SHIFT
);
1109 if (dma
&& !i2c_dev
->msg_read
)
1110 *buffer
++ = packet_header
;
1112 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
1114 packet_header
= msg
->len
- 1;
1115 if (dma
&& !i2c_dev
->msg_read
)
1116 *buffer
++ = packet_header
;
1118 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
1120 packet_header
= I2C_HEADER_IE_ENABLE
;
1121 if (end_state
== MSG_END_CONTINUE
)
1122 packet_header
|= I2C_HEADER_CONTINUE_XFER
;
1123 else if (end_state
== MSG_END_REPEAT_START
)
1124 packet_header
|= I2C_HEADER_REPEAT_START
;
1125 if (msg
->flags
& I2C_M_TEN
) {
1126 packet_header
|= msg
->addr
;
1127 packet_header
|= I2C_HEADER_10BIT_ADDR
;
1129 packet_header
|= msg
->addr
<< I2C_HEADER_SLAVE_ADDR_SHIFT
;
1131 if (msg
->flags
& I2C_M_IGNORE_NAK
)
1132 packet_header
|= I2C_HEADER_CONT_ON_NAK
;
1133 if (msg
->flags
& I2C_M_RD
)
1134 packet_header
|= I2C_HEADER_READ
;
1135 if (dma
&& !i2c_dev
->msg_read
)
1136 *buffer
++ = packet_header
;
1138 i2c_writel(i2c_dev
, packet_header
, I2C_TX_FIFO
);
1140 if (!i2c_dev
->msg_read
) {
1142 memcpy(buffer
, msg
->buf
, msg
->len
);
1143 dma_sync_single_for_device(i2c_dev
->dev
,
1147 err
= tegra_i2c_dma_submit(i2c_dev
, xfer_size
);
1149 dev_err(i2c_dev
->dev
,
1150 "starting TX DMA failed, err %d\n",
1155 tegra_i2c_fill_tx_fifo(i2c_dev
);
1159 if (i2c_dev
->hw
->has_per_pkt_xfer_complete_irq
)
1160 int_mask
|= I2C_INT_PACKET_XFER_COMPLETE
;
1162 if (msg
->flags
& I2C_M_RD
)
1163 int_mask
|= I2C_INT_RX_FIFO_DATA_REQ
;
1164 else if (i2c_dev
->msg_buf_remaining
)
1165 int_mask
|= I2C_INT_TX_FIFO_DATA_REQ
;
1168 tegra_i2c_unmask_irq(i2c_dev
, int_mask
);
1169 dev_dbg(i2c_dev
->dev
, "unmasked irq: %02x\n",
1170 i2c_readl(i2c_dev
, I2C_INT_MASK
));
1173 spin_unlock_irqrestore(&i2c_dev
->xfer_lock
, flags
);
1179 time_left
= wait_for_completion_timeout(&i2c_dev
->dma_complete
,
1180 msecs_to_jiffies(xfer_time
));
1181 if (time_left
== 0) {
1182 dev_err(i2c_dev
->dev
, "DMA transfer timeout\n");
1183 dmaengine_terminate_sync(i2c_dev
->msg_read
?
1184 i2c_dev
->rx_dma_chan
:
1185 i2c_dev
->tx_dma_chan
);
1186 tegra_i2c_init(i2c_dev
, true);
1190 if (i2c_dev
->msg_read
&& i2c_dev
->msg_err
== I2C_ERR_NONE
) {
1191 dma_sync_single_for_cpu(i2c_dev
->dev
,
1195 memcpy(i2c_dev
->msg_buf
, i2c_dev
->dma_buf
,
1199 if (i2c_dev
->msg_err
!= I2C_ERR_NONE
)
1200 dmaengine_synchronize(i2c_dev
->msg_read
?
1201 i2c_dev
->rx_dma_chan
:
1202 i2c_dev
->tx_dma_chan
);
1205 time_left
= wait_for_completion_timeout(&i2c_dev
->msg_complete
,
1206 msecs_to_jiffies(xfer_time
));
1207 tegra_i2c_mask_irq(i2c_dev
, int_mask
);
1209 if (time_left
== 0) {
1210 dev_err(i2c_dev
->dev
, "i2c transfer timed out\n");
1212 tegra_i2c_init(i2c_dev
, true);
1216 dev_dbg(i2c_dev
->dev
, "transfer complete: %lu %d %d\n",
1217 time_left
, completion_done(&i2c_dev
->msg_complete
),
1220 i2c_dev
->is_curr_dma_xfer
= false;
1221 if (likely(i2c_dev
->msg_err
== I2C_ERR_NONE
))
1224 tegra_i2c_init(i2c_dev
, true);
1225 /* start recovery upon arbitration loss in single master mode */
1226 if (i2c_dev
->msg_err
== I2C_ERR_ARBITRATION_LOST
) {
1227 if (!i2c_dev
->is_multimaster_mode
)
1228 return i2c_recover_bus(&i2c_dev
->adapter
);
1232 if (i2c_dev
->msg_err
== I2C_ERR_NO_ACK
) {
1233 if (msg
->flags
& I2C_M_IGNORE_NAK
)
1241 static int tegra_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[],
1244 struct tegra_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
1248 ret
= pm_runtime_get_sync(i2c_dev
->dev
);
1250 dev_err(i2c_dev
->dev
, "runtime resume failed %d\n", ret
);
1254 for (i
= 0; i
< num
; i
++) {
1255 enum msg_end_type end_type
= MSG_END_STOP
;
1257 if (i
< (num
- 1)) {
1258 if (msgs
[i
+ 1].flags
& I2C_M_NOSTART
)
1259 end_type
= MSG_END_CONTINUE
;
1261 end_type
= MSG_END_REPEAT_START
;
1263 ret
= tegra_i2c_xfer_msg(i2c_dev
, &msgs
[i
], end_type
);
1268 pm_runtime_put(i2c_dev
->dev
);
1273 static u32
tegra_i2c_func(struct i2c_adapter
*adap
)
1275 struct tegra_i2c_dev
*i2c_dev
= i2c_get_adapdata(adap
);
1276 u32 ret
= I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
) |
1277 I2C_FUNC_10BIT_ADDR
| I2C_FUNC_PROTOCOL_MANGLING
;
1279 if (i2c_dev
->hw
->has_continue_xfer_support
)
1280 ret
|= I2C_FUNC_NOSTART
;
1284 static void tegra_i2c_parse_dt(struct tegra_i2c_dev
*i2c_dev
)
1286 struct device_node
*np
= i2c_dev
->dev
->of_node
;
1290 ret
= of_property_read_u32(np
, "clock-frequency",
1291 &i2c_dev
->bus_clk_rate
);
1293 i2c_dev
->bus_clk_rate
= 100000; /* default clock rate */
1295 multi_mode
= of_property_read_bool(np
, "multi-master");
1296 i2c_dev
->is_multimaster_mode
= multi_mode
;
1299 static const struct i2c_algorithm tegra_i2c_algo
= {
1300 .master_xfer
= tegra_i2c_xfer
,
1301 .functionality
= tegra_i2c_func
,
1304 /* payload size is only 12 bit */
1305 static const struct i2c_adapter_quirks tegra_i2c_quirks
= {
1306 .flags
= I2C_AQ_NO_ZERO_LEN
,
1307 .max_read_len
= SZ_4K
,
1308 .max_write_len
= SZ_4K
- I2C_PACKET_HEADER_SIZE
,
1311 static const struct i2c_adapter_quirks tegra194_i2c_quirks
= {
1312 .flags
= I2C_AQ_NO_ZERO_LEN
,
1313 .max_write_len
= SZ_64K
- I2C_PACKET_HEADER_SIZE
,
1316 static struct i2c_bus_recovery_info tegra_i2c_recovery_info
= {
1317 .recover_bus
= tegra_i2c_issue_bus_clear
,
1320 static const struct tegra_i2c_hw_feature tegra20_i2c_hw
= {
1321 .has_continue_xfer_support
= false,
1322 .has_per_pkt_xfer_complete_irq
= false,
1323 .has_single_clk_source
= false,
1324 .clk_divisor_hs_mode
= 3,
1325 .clk_divisor_std_mode
= 0,
1326 .clk_divisor_fast_mode
= 0,
1327 .clk_divisor_fast_plus_mode
= 0,
1328 .has_config_load_reg
= false,
1329 .has_multi_master_mode
= false,
1330 .has_slcg_override_reg
= false,
1331 .has_mst_fifo
= false,
1332 .quirks
= &tegra_i2c_quirks
,
1333 .supports_bus_clear
= false,
1334 .has_apb_dma
= true,
1335 .tlow_std_mode
= 0x4,
1336 .thigh_std_mode
= 0x2,
1337 .tlow_fast_fastplus_mode
= 0x4,
1338 .thigh_fast_fastplus_mode
= 0x2,
1339 .setup_hold_time_std_mode
= 0x0,
1340 .setup_hold_time_fast_fast_plus_mode
= 0x0,
1341 .setup_hold_time_hs_mode
= 0x0,
1342 .has_interface_timing_reg
= false,
1345 static const struct tegra_i2c_hw_feature tegra30_i2c_hw
= {
1346 .has_continue_xfer_support
= true,
1347 .has_per_pkt_xfer_complete_irq
= false,
1348 .has_single_clk_source
= false,
1349 .clk_divisor_hs_mode
= 3,
1350 .clk_divisor_std_mode
= 0,
1351 .clk_divisor_fast_mode
= 0,
1352 .clk_divisor_fast_plus_mode
= 0,
1353 .has_config_load_reg
= false,
1354 .has_multi_master_mode
= false,
1355 .has_slcg_override_reg
= false,
1356 .has_mst_fifo
= false,
1357 .quirks
= &tegra_i2c_quirks
,
1358 .supports_bus_clear
= false,
1359 .has_apb_dma
= true,
1360 .tlow_std_mode
= 0x4,
1361 .thigh_std_mode
= 0x2,
1362 .tlow_fast_fastplus_mode
= 0x4,
1363 .thigh_fast_fastplus_mode
= 0x2,
1364 .setup_hold_time_std_mode
= 0x0,
1365 .setup_hold_time_fast_fast_plus_mode
= 0x0,
1366 .setup_hold_time_hs_mode
= 0x0,
1367 .has_interface_timing_reg
= false,
1370 static const struct tegra_i2c_hw_feature tegra114_i2c_hw
= {
1371 .has_continue_xfer_support
= true,
1372 .has_per_pkt_xfer_complete_irq
= true,
1373 .has_single_clk_source
= true,
1374 .clk_divisor_hs_mode
= 1,
1375 .clk_divisor_std_mode
= 0x19,
1376 .clk_divisor_fast_mode
= 0x19,
1377 .clk_divisor_fast_plus_mode
= 0x10,
1378 .has_config_load_reg
= false,
1379 .has_multi_master_mode
= false,
1380 .has_slcg_override_reg
= false,
1381 .has_mst_fifo
= false,
1382 .quirks
= &tegra_i2c_quirks
,
1383 .supports_bus_clear
= true,
1384 .has_apb_dma
= true,
1385 .tlow_std_mode
= 0x4,
1386 .thigh_std_mode
= 0x2,
1387 .tlow_fast_fastplus_mode
= 0x4,
1388 .thigh_fast_fastplus_mode
= 0x2,
1389 .setup_hold_time_std_mode
= 0x0,
1390 .setup_hold_time_fast_fast_plus_mode
= 0x0,
1391 .setup_hold_time_hs_mode
= 0x0,
1392 .has_interface_timing_reg
= false,
1395 static const struct tegra_i2c_hw_feature tegra124_i2c_hw
= {
1396 .has_continue_xfer_support
= true,
1397 .has_per_pkt_xfer_complete_irq
= true,
1398 .has_single_clk_source
= true,
1399 .clk_divisor_hs_mode
= 1,
1400 .clk_divisor_std_mode
= 0x19,
1401 .clk_divisor_fast_mode
= 0x19,
1402 .clk_divisor_fast_plus_mode
= 0x10,
1403 .has_config_load_reg
= true,
1404 .has_multi_master_mode
= false,
1405 .has_slcg_override_reg
= true,
1406 .has_mst_fifo
= false,
1407 .quirks
= &tegra_i2c_quirks
,
1408 .supports_bus_clear
= true,
1409 .has_apb_dma
= true,
1410 .tlow_std_mode
= 0x4,
1411 .thigh_std_mode
= 0x2,
1412 .tlow_fast_fastplus_mode
= 0x4,
1413 .thigh_fast_fastplus_mode
= 0x2,
1414 .setup_hold_time_std_mode
= 0x0,
1415 .setup_hold_time_fast_fast_plus_mode
= 0x0,
1416 .setup_hold_time_hs_mode
= 0x0,
1417 .has_interface_timing_reg
= true,
1420 static const struct tegra_i2c_hw_feature tegra210_i2c_hw
= {
1421 .has_continue_xfer_support
= true,
1422 .has_per_pkt_xfer_complete_irq
= true,
1423 .has_single_clk_source
= true,
1424 .clk_divisor_hs_mode
= 1,
1425 .clk_divisor_std_mode
= 0x19,
1426 .clk_divisor_fast_mode
= 0x19,
1427 .clk_divisor_fast_plus_mode
= 0x10,
1428 .has_config_load_reg
= true,
1429 .has_multi_master_mode
= false,
1430 .has_slcg_override_reg
= true,
1431 .has_mst_fifo
= false,
1432 .quirks
= &tegra_i2c_quirks
,
1433 .supports_bus_clear
= true,
1434 .has_apb_dma
= true,
1435 .tlow_std_mode
= 0x4,
1436 .thigh_std_mode
= 0x2,
1437 .tlow_fast_fastplus_mode
= 0x4,
1438 .thigh_fast_fastplus_mode
= 0x2,
1439 .setup_hold_time_std_mode
= 0,
1440 .setup_hold_time_fast_fast_plus_mode
= 0,
1441 .setup_hold_time_hs_mode
= 0,
1442 .has_interface_timing_reg
= true,
1445 static const struct tegra_i2c_hw_feature tegra186_i2c_hw
= {
1446 .has_continue_xfer_support
= true,
1447 .has_per_pkt_xfer_complete_irq
= true,
1448 .has_single_clk_source
= true,
1449 .clk_divisor_hs_mode
= 1,
1450 .clk_divisor_std_mode
= 0x16,
1451 .clk_divisor_fast_mode
= 0x19,
1452 .clk_divisor_fast_plus_mode
= 0x10,
1453 .has_config_load_reg
= true,
1454 .has_multi_master_mode
= false,
1455 .has_slcg_override_reg
= true,
1456 .has_mst_fifo
= false,
1457 .quirks
= &tegra_i2c_quirks
,
1458 .supports_bus_clear
= true,
1459 .has_apb_dma
= false,
1460 .tlow_std_mode
= 0x4,
1461 .thigh_std_mode
= 0x3,
1462 .tlow_fast_fastplus_mode
= 0x4,
1463 .thigh_fast_fastplus_mode
= 0x2,
1464 .setup_hold_time_std_mode
= 0,
1465 .setup_hold_time_fast_fast_plus_mode
= 0,
1466 .setup_hold_time_hs_mode
= 0,
1467 .has_interface_timing_reg
= true,
1470 static const struct tegra_i2c_hw_feature tegra194_i2c_hw
= {
1471 .has_continue_xfer_support
= true,
1472 .has_per_pkt_xfer_complete_irq
= true,
1473 .has_single_clk_source
= true,
1474 .clk_divisor_hs_mode
= 1,
1475 .clk_divisor_std_mode
= 0x4f,
1476 .clk_divisor_fast_mode
= 0x3c,
1477 .clk_divisor_fast_plus_mode
= 0x16,
1478 .has_config_load_reg
= true,
1479 .has_multi_master_mode
= true,
1480 .has_slcg_override_reg
= true,
1481 .has_mst_fifo
= true,
1482 .quirks
= &tegra194_i2c_quirks
,
1483 .supports_bus_clear
= true,
1484 .has_apb_dma
= false,
1485 .tlow_std_mode
= 0x8,
1486 .thigh_std_mode
= 0x7,
1487 .tlow_fast_fastplus_mode
= 0x2,
1488 .thigh_fast_fastplus_mode
= 0x2,
1489 .setup_hold_time_std_mode
= 0x08080808,
1490 .setup_hold_time_fast_fast_plus_mode
= 0x02020202,
1491 .setup_hold_time_hs_mode
= 0x090909,
1492 .has_interface_timing_reg
= true,
1495 /* Match table for of_platform binding */
1496 static const struct of_device_id tegra_i2c_of_match
[] = {
1497 { .compatible
= "nvidia,tegra194-i2c", .data
= &tegra194_i2c_hw
, },
1498 { .compatible
= "nvidia,tegra186-i2c", .data
= &tegra186_i2c_hw
, },
1499 { .compatible
= "nvidia,tegra210-i2c", .data
= &tegra210_i2c_hw
, },
1500 { .compatible
= "nvidia,tegra124-i2c", .data
= &tegra124_i2c_hw
, },
1501 { .compatible
= "nvidia,tegra114-i2c", .data
= &tegra114_i2c_hw
, },
1502 { .compatible
= "nvidia,tegra30-i2c", .data
= &tegra30_i2c_hw
, },
1503 { .compatible
= "nvidia,tegra20-i2c", .data
= &tegra20_i2c_hw
, },
1504 { .compatible
= "nvidia,tegra20-i2c-dvc", .data
= &tegra20_i2c_hw
, },
1507 MODULE_DEVICE_TABLE(of
, tegra_i2c_of_match
);
1509 static int tegra_i2c_probe(struct platform_device
*pdev
)
1511 struct tegra_i2c_dev
*i2c_dev
;
1512 struct resource
*res
;
1513 struct clk
*div_clk
;
1514 struct clk
*fast_clk
;
1516 phys_addr_t base_phys
;
1520 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1521 base_phys
= res
->start
;
1522 base
= devm_ioremap_resource(&pdev
->dev
, res
);
1524 return PTR_ERR(base
);
1526 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1528 dev_err(&pdev
->dev
, "no irq resource\n");
1533 div_clk
= devm_clk_get(&pdev
->dev
, "div-clk");
1534 if (IS_ERR(div_clk
)) {
1535 if (PTR_ERR(div_clk
) != -EPROBE_DEFER
)
1536 dev_err(&pdev
->dev
, "missing controller clock\n");
1538 return PTR_ERR(div_clk
);
1541 i2c_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c_dev
), GFP_KERNEL
);
1545 i2c_dev
->base
= base
;
1546 i2c_dev
->base_phys
= base_phys
;
1547 i2c_dev
->div_clk
= div_clk
;
1548 i2c_dev
->adapter
.algo
= &tegra_i2c_algo
;
1549 i2c_dev
->adapter
.retries
= 1;
1550 i2c_dev
->adapter
.timeout
= 6 * HZ
;
1552 i2c_dev
->cont_id
= pdev
->id
;
1553 i2c_dev
->dev
= &pdev
->dev
;
1555 i2c_dev
->rst
= devm_reset_control_get_exclusive(&pdev
->dev
, "i2c");
1556 if (IS_ERR(i2c_dev
->rst
)) {
1557 dev_err(&pdev
->dev
, "missing controller reset\n");
1558 return PTR_ERR(i2c_dev
->rst
);
1561 tegra_i2c_parse_dt(i2c_dev
);
1563 i2c_dev
->hw
= of_device_get_match_data(&pdev
->dev
);
1564 i2c_dev
->is_dvc
= of_device_is_compatible(pdev
->dev
.of_node
,
1565 "nvidia,tegra20-i2c-dvc");
1566 i2c_dev
->adapter
.quirks
= i2c_dev
->hw
->quirks
;
1567 i2c_dev
->dma_buf_size
= i2c_dev
->adapter
.quirks
->max_write_len
+
1568 I2C_PACKET_HEADER_SIZE
;
1569 init_completion(&i2c_dev
->msg_complete
);
1570 init_completion(&i2c_dev
->dma_complete
);
1571 spin_lock_init(&i2c_dev
->xfer_lock
);
1573 if (!i2c_dev
->hw
->has_single_clk_source
) {
1574 fast_clk
= devm_clk_get(&pdev
->dev
, "fast-clk");
1575 if (IS_ERR(fast_clk
)) {
1576 dev_err(&pdev
->dev
, "missing fast clock\n");
1577 return PTR_ERR(fast_clk
);
1579 i2c_dev
->fast_clk
= fast_clk
;
1582 platform_set_drvdata(pdev
, i2c_dev
);
1584 if (!i2c_dev
->hw
->has_single_clk_source
) {
1585 ret
= clk_prepare(i2c_dev
->fast_clk
);
1587 dev_err(i2c_dev
->dev
, "Clock prepare failed %d\n", ret
);
1592 if (i2c_dev
->bus_clk_rate
> I2C_FAST_MODE
&&
1593 i2c_dev
->bus_clk_rate
<= I2C_FAST_PLUS_MODE
)
1594 i2c_dev
->clk_divisor_non_hs_mode
=
1595 i2c_dev
->hw
->clk_divisor_fast_plus_mode
;
1596 else if (i2c_dev
->bus_clk_rate
> I2C_STANDARD_MODE
&&
1597 i2c_dev
->bus_clk_rate
<= I2C_FAST_MODE
)
1598 i2c_dev
->clk_divisor_non_hs_mode
=
1599 i2c_dev
->hw
->clk_divisor_fast_mode
;
1601 i2c_dev
->clk_divisor_non_hs_mode
=
1602 i2c_dev
->hw
->clk_divisor_std_mode
;
1604 ret
= clk_prepare(i2c_dev
->div_clk
);
1606 dev_err(i2c_dev
->dev
, "Clock prepare failed %d\n", ret
);
1607 goto unprepare_fast_clk
;
1610 pm_runtime_enable(&pdev
->dev
);
1611 if (!pm_runtime_enabled(&pdev
->dev
)) {
1612 ret
= tegra_i2c_runtime_resume(&pdev
->dev
);
1614 dev_err(&pdev
->dev
, "runtime resume failed\n");
1615 goto unprepare_div_clk
;
1618 ret
= pm_runtime_get_sync(i2c_dev
->dev
);
1620 dev_err(&pdev
->dev
, "runtime resume failed\n");
1625 if (i2c_dev
->is_multimaster_mode
) {
1626 ret
= clk_enable(i2c_dev
->div_clk
);
1628 dev_err(i2c_dev
->dev
, "div_clk enable failed %d\n",
1634 if (i2c_dev
->hw
->supports_bus_clear
)
1635 i2c_dev
->adapter
.bus_recovery_info
= &tegra_i2c_recovery_info
;
1637 ret
= tegra_i2c_init_dma(i2c_dev
);
1639 goto disable_div_clk
;
1641 ret
= tegra_i2c_init(i2c_dev
, false);
1643 dev_err(&pdev
->dev
, "Failed to initialize i2c controller\n");
1647 ret
= devm_request_irq(&pdev
->dev
, i2c_dev
->irq
,
1648 tegra_i2c_isr
, 0, dev_name(&pdev
->dev
), i2c_dev
);
1650 dev_err(&pdev
->dev
, "Failed to request irq %i\n", i2c_dev
->irq
);
1654 i2c_set_adapdata(&i2c_dev
->adapter
, i2c_dev
);
1655 i2c_dev
->adapter
.owner
= THIS_MODULE
;
1656 i2c_dev
->adapter
.class = I2C_CLASS_DEPRECATED
;
1657 strlcpy(i2c_dev
->adapter
.name
, dev_name(&pdev
->dev
),
1658 sizeof(i2c_dev
->adapter
.name
));
1659 i2c_dev
->adapter
.dev
.parent
= &pdev
->dev
;
1660 i2c_dev
->adapter
.nr
= pdev
->id
;
1661 i2c_dev
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
1663 ret
= i2c_add_numbered_adapter(&i2c_dev
->adapter
);
1667 pm_runtime_put(&pdev
->dev
);
1672 tegra_i2c_release_dma(i2c_dev
);
1675 if (i2c_dev
->is_multimaster_mode
)
1676 clk_disable(i2c_dev
->div_clk
);
1679 if (pm_runtime_enabled(&pdev
->dev
))
1680 pm_runtime_put_sync(&pdev
->dev
);
1682 tegra_i2c_runtime_suspend(&pdev
->dev
);
1685 if (pm_runtime_enabled(&pdev
->dev
))
1686 pm_runtime_disable(&pdev
->dev
);
1689 clk_unprepare(i2c_dev
->div_clk
);
1692 if (!i2c_dev
->hw
->has_single_clk_source
)
1693 clk_unprepare(i2c_dev
->fast_clk
);
1698 static int tegra_i2c_remove(struct platform_device
*pdev
)
1700 struct tegra_i2c_dev
*i2c_dev
= platform_get_drvdata(pdev
);
1702 i2c_del_adapter(&i2c_dev
->adapter
);
1704 if (i2c_dev
->is_multimaster_mode
)
1705 clk_disable(i2c_dev
->div_clk
);
1707 pm_runtime_disable(&pdev
->dev
);
1708 if (!pm_runtime_status_suspended(&pdev
->dev
))
1709 tegra_i2c_runtime_suspend(&pdev
->dev
);
1711 clk_unprepare(i2c_dev
->div_clk
);
1712 if (!i2c_dev
->hw
->has_single_clk_source
)
1713 clk_unprepare(i2c_dev
->fast_clk
);
1715 tegra_i2c_release_dma(i2c_dev
);
1719 static int __maybe_unused
tegra_i2c_suspend(struct device
*dev
)
1721 struct tegra_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
1724 i2c_mark_adapter_suspended(&i2c_dev
->adapter
);
1726 err
= pm_runtime_force_suspend(dev
);
1733 static int __maybe_unused
tegra_i2c_resume(struct device
*dev
)
1735 struct tegra_i2c_dev
*i2c_dev
= dev_get_drvdata(dev
);
1738 err
= tegra_i2c_runtime_resume(dev
);
1742 err
= tegra_i2c_init(i2c_dev
, false);
1746 err
= tegra_i2c_runtime_suspend(dev
);
1750 err
= pm_runtime_force_resume(dev
);
1754 i2c_mark_adapter_resumed(&i2c_dev
->adapter
);
1759 static const struct dev_pm_ops tegra_i2c_pm
= {
1760 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend
, tegra_i2c_resume
)
1761 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend
, tegra_i2c_runtime_resume
,
1765 static struct platform_driver tegra_i2c_driver
= {
1766 .probe
= tegra_i2c_probe
,
1767 .remove
= tegra_i2c_remove
,
1769 .name
= "tegra-i2c",
1770 .of_match_table
= tegra_i2c_of_match
,
1771 .pm
= &tegra_i2c_pm
,
1775 module_platform_driver(tegra_i2c_driver
);
1777 MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1778 MODULE_AUTHOR("Colin Cross");
1779 MODULE_LICENSE("GPL v2");