1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/i2c/busses/i2c-tegra.c
5 * Copyright (C) 2010 Google, Inc.
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/dmaengine.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
19 #include <linux/iopoll.h>
20 #include <linux/irq.h>
21 #include <linux/kernel.h>
22 #include <linux/ktime.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/reset.h>
30 #define BYTES_PER_FIFO_WORD 4
32 #define I2C_CNFG 0x000
33 #define I2C_CNFG_DEBOUNCE_CNT GENMASK(14, 12)
34 #define I2C_CNFG_PACKET_MODE_EN BIT(10)
35 #define I2C_CNFG_NEW_MASTER_FSM BIT(11)
36 #define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
37 #define I2C_STATUS 0x01c
38 #define I2C_SL_CNFG 0x020
39 #define I2C_SL_CNFG_NACK BIT(1)
40 #define I2C_SL_CNFG_NEWSL BIT(2)
41 #define I2C_SL_ADDR1 0x02c
42 #define I2C_SL_ADDR2 0x030
43 #define I2C_TLOW_SEXT 0x034
44 #define I2C_TX_FIFO 0x050
45 #define I2C_RX_FIFO 0x054
46 #define I2C_PACKET_TRANSFER_STATUS 0x058
47 #define I2C_FIFO_CONTROL 0x05c
48 #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
49 #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
50 #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
51 #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
52 #define I2C_FIFO_STATUS 0x060
53 #define I2C_FIFO_STATUS_TX GENMASK(7, 4)
54 #define I2C_FIFO_STATUS_RX GENMASK(3, 0)
55 #define I2C_INT_MASK 0x064
56 #define I2C_INT_STATUS 0x068
57 #define I2C_INT_BUS_CLR_DONE BIT(11)
58 #define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
59 #define I2C_INT_NO_ACK BIT(3)
60 #define I2C_INT_ARBITRATION_LOST BIT(2)
61 #define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
62 #define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
63 #define I2C_CLK_DIVISOR 0x06c
64 #define I2C_CLK_DIVISOR_STD_FAST_MODE GENMASK(31, 16)
65 #define I2C_CLK_DIVISOR_HSMODE GENMASK(15, 0)
67 #define DVC_CTRL_REG1 0x000
68 #define DVC_CTRL_REG1_INTR_EN BIT(10)
69 #define DVC_CTRL_REG3 0x008
70 #define DVC_CTRL_REG3_SW_PROG BIT(26)
71 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
72 #define DVC_STATUS 0x00c
73 #define DVC_STATUS_I2C_DONE_INTR BIT(30)
75 #define I2C_ERR_NONE 0x00
76 #define I2C_ERR_NO_ACK BIT(0)
77 #define I2C_ERR_ARBITRATION_LOST BIT(1)
78 #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
79 #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
81 #define PACKET_HEADER0_HEADER_SIZE GENMASK(29, 28)
82 #define PACKET_HEADER0_PACKET_ID GENMASK(23, 16)
83 #define PACKET_HEADER0_CONT_ID GENMASK(15, 12)
84 #define PACKET_HEADER0_PROTOCOL GENMASK(7, 4)
85 #define PACKET_HEADER0_PROTOCOL_I2C 1
87 #define I2C_HEADER_CONT_ON_NAK BIT(21)
88 #define I2C_HEADER_READ BIT(19)
89 #define I2C_HEADER_10BIT_ADDR BIT(18)
90 #define I2C_HEADER_IE_ENABLE BIT(17)
91 #define I2C_HEADER_REPEAT_START BIT(16)
92 #define I2C_HEADER_CONTINUE_XFER BIT(15)
93 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
95 #define I2C_BUS_CLEAR_CNFG 0x084
96 #define I2C_BC_SCLK_THRESHOLD GENMASK(23, 16)
97 #define I2C_BC_STOP_COND BIT(2)
98 #define I2C_BC_TERMINATE BIT(1)
99 #define I2C_BC_ENABLE BIT(0)
100 #define I2C_BUS_CLEAR_STATUS 0x088
101 #define I2C_BC_STATUS BIT(0)
103 #define I2C_CONFIG_LOAD 0x08c
104 #define I2C_MSTR_CONFIG_LOAD BIT(0)
106 #define I2C_CLKEN_OVERRIDE 0x090
107 #define I2C_MST_CORE_CLKEN_OVR BIT(0)
109 #define I2C_INTERFACE_TIMING_0 0x094
110 #define I2C_INTERFACE_TIMING_THIGH GENMASK(13, 8)
111 #define I2C_INTERFACE_TIMING_TLOW GENMASK(5, 0)
112 #define I2C_INTERFACE_TIMING_1 0x098
113 #define I2C_INTERFACE_TIMING_TBUF GENMASK(29, 24)
114 #define I2C_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
115 #define I2C_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
116 #define I2C_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
118 #define I2C_HS_INTERFACE_TIMING_0 0x09c
119 #define I2C_HS_INTERFACE_TIMING_THIGH GENMASK(13, 8)
120 #define I2C_HS_INTERFACE_TIMING_TLOW GENMASK(5, 0)
121 #define I2C_HS_INTERFACE_TIMING_1 0x0a0
122 #define I2C_HS_INTERFACE_TIMING_TSU_STO GENMASK(21, 16)
123 #define I2C_HS_INTERFACE_TIMING_THD_STA GENMASK(13, 8)
124 #define I2C_HS_INTERFACE_TIMING_TSU_STA GENMASK(5, 0)
126 #define I2C_MST_FIFO_CONTROL 0x0b4
127 #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
128 #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
129 #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
130 #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
132 #define I2C_MST_FIFO_STATUS 0x0b8
133 #define I2C_MST_FIFO_STATUS_TX GENMASK(23, 16)
134 #define I2C_MST_FIFO_STATUS_RX GENMASK(7, 0)
136 /* configuration load timeout in microseconds */
137 #define I2C_CONFIG_LOAD_TIMEOUT 1000000
139 /* packet header size in bytes */
140 #define I2C_PACKET_HEADER_SIZE 12
143 * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
144 * avoid DMA overhead, otherwise external APB DMA controller will be used.
145 * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
146 * I2C_PACKET_HEADER_SIZE.
148 #define I2C_PIO_MODE_PREFERRED_LEN 32
151 * msg_end_type: The bus control which needs to be sent at end of transfer.
152 * @MSG_END_STOP: Send stop pulse.
153 * @MSG_END_REPEAT_START: Send repeat-start.
154 * @MSG_END_CONTINUE: Don't send stop or repeat-start.
158 MSG_END_REPEAT_START,
163 * struct tegra_i2c_hw_feature : per hardware generation features
164 * @has_continue_xfer_support: continue-transfer supported
165 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
166 * completion interrupt on per packet basis.
167 * @has_config_load_reg: Has the config load register to load the new
169 * @clk_divisor_hs_mode: Clock divisor in HS mode.
170 * @clk_divisor_std_mode: Clock divisor in standard mode. It is
171 * applicable if there is no fast clock source i.e. single clock
173 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
174 * applicable if there is no fast clock source i.e. single clock
176 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
177 * applicable if there is no fast clock source (i.e. single
179 * @has_multi_master_mode: The I2C controller supports running in single-master
180 * or multi-master mode.
181 * @has_slcg_override_reg: The I2C controller supports a register that
182 * overrides the second level clock gating.
183 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
184 * provides additional features and allows for longer messages to
185 * be transferred in one go.
186 * @quirks: I2C adapter quirks for limiting write/read transfer size and not
187 * allowing 0 length transfers.
188 * @supports_bus_clear: Bus Clear support to recover from bus hang during
189 * SDA stuck low from device for some unknown reasons.
190 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
191 * @tlow_std_mode: Low period of the clock in standard mode.
192 * @thigh_std_mode: High period of the clock in standard mode.
193 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
194 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
195 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
197 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
198 * conditions in fast/fast-plus modes.
199 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
201 * @has_interface_timing_reg: Has interface timing register to program the tuned
204 struct tegra_i2c_hw_feature {
205 bool has_continue_xfer_support;
206 bool has_per_pkt_xfer_complete_irq;
207 bool has_config_load_reg;
208 u32 clk_divisor_hs_mode;
209 u32 clk_divisor_std_mode;
210 u32 clk_divisor_fast_mode;
211 u32 clk_divisor_fast_plus_mode;
212 bool has_multi_master_mode;
213 bool has_slcg_override_reg;
215 const struct i2c_adapter_quirks *quirks;
216 bool supports_bus_clear;
220 u32 tlow_fast_fastplus_mode;
221 u32 thigh_fast_fastplus_mode;
222 u32 setup_hold_time_std_mode;
223 u32 setup_hold_time_fast_fast_plus_mode;
224 u32 setup_hold_time_hs_mode;
225 bool has_interface_timing_reg;
229 * struct tegra_i2c_dev - per device I2C context
230 * @dev: device reference for power management
231 * @hw: Tegra I2C HW feature
232 * @adapter: core I2C layer adapter information
233 * @div_clk: clock reference for div clock of I2C controller
234 * @clocks: array of I2C controller clocks
235 * @nclocks: number of clocks in the array
236 * @rst: reset control for the I2C controller
237 * @base: ioremapped registers cookie
238 * @base_phys: physical base address of the I2C controller
239 * @cont_id: I2C controller ID, used for packet header
240 * @irq: IRQ number of transfer complete interrupt
241 * @is_dvc: identifies the DVC I2C controller, has a different register layout
242 * @is_vi: identifies the VI I2C controller, has a different register layout
243 * @msg_complete: transfer completion notifier
244 * @msg_err: error code for completed message
245 * @msg_buf: pointer to current message data
246 * @msg_buf_remaining: size of unsent data in the message buffer
247 * @msg_read: indicates that the transfer is a read access
248 * @bus_clk_rate: current I2C bus clock rate
249 * @multimaster_mode: indicates that I2C controller is in multi-master mode
250 * @tx_dma_chan: DMA transmit channel
251 * @rx_dma_chan: DMA receive channel
252 * @dma_phys: handle to DMA resources
253 * @dma_buf: pointer to allocated DMA buffer
254 * @dma_buf_size: DMA buffer size
255 * @dma_mode: indicates active DMA transfer
256 * @dma_complete: DMA completion notifier
257 * @atomic_mode: indicates active atomic transfer
259 struct tegra_i2c_dev {
261 struct i2c_adapter adapter;
263 const struct tegra_i2c_hw_feature *hw;
264 struct reset_control *rst;
265 unsigned int cont_id;
268 phys_addr_t base_phys;
271 struct clk_bulk_data clocks[2];
272 unsigned int nclocks;
277 struct completion msg_complete;
278 size_t msg_buf_remaining;
282 struct completion dma_complete;
283 struct dma_chan *tx_dma_chan;
284 struct dma_chan *rx_dma_chan;
285 unsigned int dma_buf_size;
289 bool multimaster_mode;
297 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
300 writel_relaxed(val, i2c_dev->base + reg);
303 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
305 return readl_relaxed(i2c_dev->base + reg);
309 * If necessary, i2c_writel() and i2c_readl() will offset the register
310 * in order to talk to the I2C block inside the DVC block.
312 static u32 tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
315 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
316 else if (i2c_dev->is_vi)
317 reg = 0xc00 + (reg << 2);
322 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg)
324 writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
326 /* read back register to make sure that register writes completed */
327 if (reg != I2C_TX_FIFO)
328 readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
329 else if (i2c_dev->is_vi)
330 readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, I2C_INT_STATUS));
333 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
335 return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
338 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
339 unsigned int reg, unsigned int len)
341 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
344 static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data,
345 unsigned int reg, unsigned int len)
350 * VI I2C controller has known hardware bug where writes get stuck
351 * when immediate multiple writes happen to TX_FIFO register.
352 * Recommended software work around is to read I2C register after
353 * each write to TX_FIFO register to flush out the data.
356 i2c_writel(i2c_dev, *data32++, reg);
359 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
360 unsigned int reg, unsigned int len)
362 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
365 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
369 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
370 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
373 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
377 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
378 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
381 static void tegra_i2c_dma_complete(void *args)
383 struct tegra_i2c_dev *i2c_dev = args;
385 complete(&i2c_dev->dma_complete);
388 static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
390 struct dma_async_tx_descriptor *dma_desc;
391 enum dma_transfer_direction dir;
392 struct dma_chan *chan;
394 dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
396 reinit_completion(&i2c_dev->dma_complete);
398 dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
399 chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
401 dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
402 len, dir, DMA_PREP_INTERRUPT |
405 dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n",
406 i2c_dev->msg_read ? "RX" : "TX");
410 dma_desc->callback = tegra_i2c_dma_complete;
411 dma_desc->callback_param = i2c_dev;
413 dmaengine_submit(dma_desc);
414 dma_async_issue_pending(chan);
419 static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
421 if (i2c_dev->dma_buf) {
422 dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
423 i2c_dev->dma_buf, i2c_dev->dma_phys);
424 i2c_dev->dma_buf = NULL;
427 if (i2c_dev->tx_dma_chan) {
428 dma_release_channel(i2c_dev->tx_dma_chan);
429 i2c_dev->tx_dma_chan = NULL;
432 if (i2c_dev->rx_dma_chan) {
433 dma_release_channel(i2c_dev->rx_dma_chan);
434 i2c_dev->rx_dma_chan = NULL;
438 static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
440 struct dma_chan *chan;
445 if (!i2c_dev->hw->has_apb_dma || i2c_dev->is_vi)
448 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
449 dev_dbg(i2c_dev->dev, "DMA support not enabled\n");
453 chan = dma_request_chan(i2c_dev->dev, "rx");
459 i2c_dev->rx_dma_chan = chan;
461 chan = dma_request_chan(i2c_dev->dev, "tx");
467 i2c_dev->tx_dma_chan = chan;
469 i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
470 I2C_PACKET_HEADER_SIZE;
472 dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
473 &dma_phys, GFP_KERNEL | __GFP_NOWARN);
475 dev_err(i2c_dev->dev, "failed to allocate DMA buffer\n");
480 i2c_dev->dma_buf = dma_buf;
481 i2c_dev->dma_phys = dma_phys;
486 tegra_i2c_release_dma(i2c_dev);
487 if (err != -EPROBE_DEFER) {
488 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
489 dev_err(i2c_dev->dev, "falling back to PIO\n");
497 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
498 * block. This block is identical to the rest of the I2C blocks, except that
499 * it only supports master mode, it has registers moved around, and it needs
500 * some extra init to get it into I2C mode. The register moves are handled
501 * by i2c_readl() and i2c_writel().
503 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
507 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
508 val |= DVC_CTRL_REG3_SW_PROG;
509 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
510 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
512 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
513 val |= DVC_CTRL_REG1_INTR_EN;
514 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
517 static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev)
521 value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) |
522 FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4);
523 i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_0);
525 value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) |
526 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) |
527 FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) |
528 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4);
529 i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_1);
531 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) |
532 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8);
533 i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_0);
535 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) |
536 FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) |
537 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11);
538 i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_1);
540 value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND;
541 i2c_writel(i2c_dev, value, I2C_BUS_CLEAR_CNFG);
543 i2c_writel(i2c_dev, 0x0, I2C_TLOW_SEXT);
546 static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev,
547 u32 reg, u32 mask, u32 delay_us,
550 void __iomem *addr = i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg);
553 if (!i2c_dev->atomic_mode)
554 return readl_relaxed_poll_timeout(addr, val, !(val & mask),
555 delay_us, timeout_us);
557 return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask),
558 delay_us, timeout_us);
561 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
563 u32 mask, val, offset;
566 if (i2c_dev->hw->has_mst_fifo) {
567 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
568 I2C_MST_FIFO_CONTROL_RX_FLUSH;
569 offset = I2C_MST_FIFO_CONTROL;
571 mask = I2C_FIFO_CONTROL_TX_FLUSH |
572 I2C_FIFO_CONTROL_RX_FLUSH;
573 offset = I2C_FIFO_CONTROL;
576 val = i2c_readl(i2c_dev, offset);
578 i2c_writel(i2c_dev, val, offset);
580 err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000);
582 dev_err(i2c_dev->dev, "failed to flush FIFO\n");
589 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
593 if (!i2c_dev->hw->has_config_load_reg)
596 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
598 err = tegra_i2c_poll_register(i2c_dev, I2C_CONFIG_LOAD, 0xffffffff,
599 1000, I2C_CONFIG_LOAD_TIMEOUT);
601 dev_err(i2c_dev->dev, "failed to load config\n");
608 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
610 u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode;
614 * The reset shouldn't ever fail in practice. The failure will be a
615 * sign of a severe problem that needs to be resolved. Still we don't
616 * want to fail the initialization completely because this may break
617 * kernel boot up since voltage regulators use I2C. Hence, we will
618 * emit a noisy warning on error, which won't stay unnoticed and
619 * won't hose machine entirely.
621 err = reset_control_reset(i2c_dev->rst);
625 tegra_dvc_init(i2c_dev);
627 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
628 FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2);
630 if (i2c_dev->hw->has_multi_master_mode)
631 val |= I2C_CNFG_MULTI_MASTER_MODE;
633 i2c_writel(i2c_dev, val, I2C_CNFG);
634 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
637 tegra_i2c_vi_init(i2c_dev);
639 switch (i2c_dev->bus_clk_rate) {
640 case I2C_MAX_STANDARD_MODE_FREQ + 1 ... I2C_MAX_FAST_MODE_PLUS_FREQ:
642 tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
643 thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
644 tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
646 if (i2c_dev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ)
647 non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode;
649 non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode;
652 case 0 ... I2C_MAX_STANDARD_MODE_FREQ:
653 tlow = i2c_dev->hw->tlow_std_mode;
654 thigh = i2c_dev->hw->thigh_std_mode;
655 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
656 non_hs_mode = i2c_dev->hw->clk_divisor_std_mode;
660 /* make sure clock divisor programmed correctly */
661 clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE,
662 i2c_dev->hw->clk_divisor_hs_mode) |
663 FIELD_PREP(I2C_CLK_DIVISOR_STD_FAST_MODE, non_hs_mode);
664 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
666 if (i2c_dev->hw->has_interface_timing_reg) {
667 val = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, thigh) |
668 FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, tlow);
669 i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
673 * Configure setup and hold times only when tsu_thd is non-zero.
674 * Otherwise, preserve the chip default values.
676 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
677 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
679 clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
681 err = clk_set_rate(i2c_dev->div_clk,
682 i2c_dev->bus_clk_rate * clk_multiplier);
684 dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err);
688 if (!i2c_dev->is_dvc && !i2c_dev->is_vi) {
689 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
691 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
692 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
693 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
694 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
697 err = tegra_i2c_flush_fifos(i2c_dev);
701 if (i2c_dev->multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
702 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
704 err = tegra_i2c_wait_for_config_load(i2c_dev);
711 static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
716 * NACK interrupt is generated before the I2C controller generates
717 * the STOP condition on the bus. So, wait for 2 clock periods
718 * before disabling the controller so that the STOP condition has
719 * been delivered properly.
721 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
723 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
724 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
725 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
727 return tegra_i2c_wait_for_config_load(i2c_dev);
730 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
732 size_t buf_remaining = i2c_dev->msg_buf_remaining;
733 unsigned int words_to_transfer, rx_fifo_avail;
734 u8 *buf = i2c_dev->msg_buf;
738 * Catch overflow due to message fully sent before the check for
739 * RX FIFO availability.
741 if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
744 if (i2c_dev->hw->has_mst_fifo) {
745 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
746 rx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_RX, val);
748 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
749 rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val);
752 /* round down to exclude partial word at the end of buffer */
753 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
754 if (words_to_transfer > rx_fifo_avail)
755 words_to_transfer = rx_fifo_avail;
757 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
759 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
760 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
761 rx_fifo_avail -= words_to_transfer;
764 * If there is a partial word at the end of buffer, handle it
765 * manually to prevent overwriting past the end of buffer.
767 if (rx_fifo_avail > 0 && buf_remaining > 0) {
769 * buf_remaining > 3 check not needed as rx_fifo_avail == 0
770 * when (words_to_transfer was > rx_fifo_avail) earlier
773 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
774 val = cpu_to_le32(val);
775 memcpy(buf, &val, buf_remaining);
780 /* RX FIFO must be drained, otherwise it's an Overflow case. */
781 if (WARN_ON_ONCE(rx_fifo_avail))
784 i2c_dev->msg_buf_remaining = buf_remaining;
785 i2c_dev->msg_buf = buf;
790 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
792 size_t buf_remaining = i2c_dev->msg_buf_remaining;
793 unsigned int words_to_transfer, tx_fifo_avail;
794 u8 *buf = i2c_dev->msg_buf;
797 if (i2c_dev->hw->has_mst_fifo) {
798 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
799 tx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_TX, val);
801 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
802 tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val);
805 /* round down to exclude partial word at the end of buffer */
806 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
809 * This hunk pushes 4 bytes at a time into the TX FIFO.
811 * It's very common to have < 4 bytes, hence there is no word
812 * to push if we have less than 4 bytes to transfer.
814 if (words_to_transfer) {
815 if (words_to_transfer > tx_fifo_avail)
816 words_to_transfer = tx_fifo_avail;
819 * Update state before writing to FIFO. Note that this may
820 * cause us to finish writing all bytes (AKA buf_remaining
821 * goes to 0), hence we have a potential for an interrupt
822 * (PACKET_XFER_COMPLETE is not maskable), but GIC interrupt
823 * is disabled at this point.
825 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
826 tx_fifo_avail -= words_to_transfer;
828 i2c_dev->msg_buf_remaining = buf_remaining;
829 i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD;
832 i2c_writesl_vi(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
834 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
836 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
840 * If there is a partial word at the end of buffer, handle it manually
841 * to prevent reading past the end of buffer, which could cross a page
842 * boundary and fault.
844 if (tx_fifo_avail > 0 && buf_remaining > 0) {
846 * buf_remaining > 3 check not needed as tx_fifo_avail == 0
847 * when (words_to_transfer was > tx_fifo_avail) earlier
848 * in this function for non-zero words_to_transfer.
850 memcpy(&val, buf, buf_remaining);
851 val = le32_to_cpu(val);
853 i2c_dev->msg_buf_remaining = 0;
854 i2c_dev->msg_buf = NULL;
856 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
862 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
864 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
865 struct tegra_i2c_dev *i2c_dev = dev_id;
868 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
871 dev_warn(i2c_dev->dev, "IRQ status 0 %08x %08x %08x\n",
872 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
873 i2c_readl(i2c_dev, I2C_STATUS),
874 i2c_readl(i2c_dev, I2C_CNFG));
875 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
879 if (status & status_err) {
880 tegra_i2c_disable_packet_mode(i2c_dev);
881 if (status & I2C_INT_NO_ACK)
882 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
883 if (status & I2C_INT_ARBITRATION_LOST)
884 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
889 * I2C transfer is terminated during the bus clear, so skip
890 * processing the other interrupts.
892 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
895 if (!i2c_dev->dma_mode) {
896 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
897 if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
899 * Overflow error condition: message fully sent,
900 * with no XFER_COMPLETE interrupt but hardware
901 * asks to transfer more.
903 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
908 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
909 if (i2c_dev->msg_buf_remaining)
910 tegra_i2c_fill_tx_fifo(i2c_dev);
912 tegra_i2c_mask_irq(i2c_dev,
913 I2C_INT_TX_FIFO_DATA_REQ);
917 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
919 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
922 * During message read XFER_COMPLETE interrupt is triggered prior to
923 * DMA completion and during message write XFER_COMPLETE interrupt is
924 * triggered after DMA completion.
926 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer,
927 * so forcing msg_buf_remaining to 0 in DMA mode.
929 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
930 if (i2c_dev->dma_mode)
931 i2c_dev->msg_buf_remaining = 0;
933 * Underflow error condition: XFER_COMPLETE before message
936 if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
937 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
940 complete(&i2c_dev->msg_complete);
944 /* mask all interrupts on error */
945 tegra_i2c_mask_irq(i2c_dev,
947 I2C_INT_ARBITRATION_LOST |
948 I2C_INT_PACKET_XFER_COMPLETE |
949 I2C_INT_TX_FIFO_DATA_REQ |
950 I2C_INT_RX_FIFO_DATA_REQ);
952 if (i2c_dev->hw->supports_bus_clear)
953 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
955 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
958 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
960 if (i2c_dev->dma_mode) {
961 if (i2c_dev->msg_read)
962 dmaengine_terminate_async(i2c_dev->rx_dma_chan);
964 dmaengine_terminate_async(i2c_dev->tx_dma_chan);
966 complete(&i2c_dev->dma_complete);
969 complete(&i2c_dev->msg_complete);
974 static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
977 struct dma_slave_config slv_config = {0};
978 u32 val, reg, dma_burst, reg_offset;
979 struct dma_chan *chan;
982 if (i2c_dev->hw->has_mst_fifo)
983 reg = I2C_MST_FIFO_CONTROL;
985 reg = I2C_FIFO_CONTROL;
987 if (i2c_dev->dma_mode) {
995 if (i2c_dev->msg_read) {
996 chan = i2c_dev->rx_dma_chan;
997 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
999 slv_config.src_addr = i2c_dev->base_phys + reg_offset;
1000 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1001 slv_config.src_maxburst = dma_burst;
1003 if (i2c_dev->hw->has_mst_fifo)
1004 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
1006 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
1008 chan = i2c_dev->tx_dma_chan;
1009 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
1011 slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
1012 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1013 slv_config.dst_maxburst = dma_burst;
1015 if (i2c_dev->hw->has_mst_fifo)
1016 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
1018 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
1021 slv_config.device_fc = true;
1022 err = dmaengine_slave_config(chan, &slv_config);
1024 dev_err(i2c_dev->dev, "DMA config failed: %d\n", err);
1025 dev_err(i2c_dev->dev, "falling back to PIO\n");
1027 tegra_i2c_release_dma(i2c_dev);
1028 i2c_dev->dma_mode = false;
1034 if (i2c_dev->hw->has_mst_fifo)
1035 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
1036 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
1038 val = I2C_FIFO_CONTROL_TX_TRIG(8) |
1039 I2C_FIFO_CONTROL_RX_TRIG(1);
1041 i2c_writel(i2c_dev, val, reg);
1044 static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev,
1045 struct completion *complete,
1046 unsigned int timeout_ms)
1048 ktime_t ktime = ktime_get();
1049 ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);
1052 u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
1055 tegra_i2c_isr(i2c_dev->irq, i2c_dev);
1057 if (completion_done(complete)) {
1058 s64 delta = ktime_ms_delta(ktimeout, ktime);
1060 return msecs_to_jiffies(delta) ?: 1;
1063 ktime = ktime_get();
1065 } while (ktime_before(ktime, ktimeout));
1070 static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev,
1071 struct completion *complete,
1072 unsigned int timeout_ms)
1076 if (i2c_dev->atomic_mode) {
1077 ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms);
1079 enable_irq(i2c_dev->irq);
1080 ret = wait_for_completion_timeout(complete,
1081 msecs_to_jiffies(timeout_ms));
1082 disable_irq(i2c_dev->irq);
1085 * Under some rare circumstances (like running KASAN +
1086 * NFS root) CPU, which handles interrupt, may stuck in
1087 * uninterruptible state for a significant time. In this
1088 * case we will get timeout if I2C transfer is running on
1089 * a sibling CPU, despite of IRQ being raised.
1091 * In order to handle this rare condition, the IRQ status
1092 * needs to be checked after timeout.
1095 ret = tegra_i2c_poll_completion(i2c_dev, complete, 0);
1101 static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
1103 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1107 reinit_completion(&i2c_dev->msg_complete);
1109 val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND |
1111 i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
1113 err = tegra_i2c_wait_for_config_load(i2c_dev);
1117 val |= I2C_BC_ENABLE;
1118 i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
1119 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1121 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50);
1122 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1124 if (time_left == 0) {
1125 dev_err(i2c_dev->dev, "failed to clear bus\n");
1129 val = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1130 if (!(val & I2C_BC_STATUS)) {
1131 dev_err(i2c_dev->dev, "un-recovered arbitration lost\n");
1138 static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev,
1139 struct i2c_msg *msg,
1140 enum msg_end_type end_state)
1142 u32 *dma_buf = i2c_dev->dma_buf;
1145 packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) |
1146 FIELD_PREP(PACKET_HEADER0_PROTOCOL,
1147 PACKET_HEADER0_PROTOCOL_I2C) |
1148 FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) |
1149 FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1);
1151 if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1152 *dma_buf++ = packet_header;
1154 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1156 packet_header = msg->len - 1;
1158 if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1159 *dma_buf++ = packet_header;
1161 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1163 packet_header = I2C_HEADER_IE_ENABLE;
1165 if (end_state == MSG_END_CONTINUE)
1166 packet_header |= I2C_HEADER_CONTINUE_XFER;
1167 else if (end_state == MSG_END_REPEAT_START)
1168 packet_header |= I2C_HEADER_REPEAT_START;
1170 if (msg->flags & I2C_M_TEN) {
1171 packet_header |= msg->addr;
1172 packet_header |= I2C_HEADER_10BIT_ADDR;
1174 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1177 if (msg->flags & I2C_M_IGNORE_NAK)
1178 packet_header |= I2C_HEADER_CONT_ON_NAK;
1180 if (msg->flags & I2C_M_RD)
1181 packet_header |= I2C_HEADER_READ;
1183 if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1184 *dma_buf++ = packet_header;
1186 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1189 static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev,
1190 struct i2c_msg *msg)
1192 if (i2c_dev->msg_err == I2C_ERR_NONE)
1195 tegra_i2c_init(i2c_dev);
1197 /* start recovery upon arbitration loss in single master mode */
1198 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1199 if (!i2c_dev->multimaster_mode)
1200 return i2c_recover_bus(&i2c_dev->adapter);
1205 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1206 if (msg->flags & I2C_M_IGNORE_NAK)
1215 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
1216 struct i2c_msg *msg,
1217 enum msg_end_type end_state)
1219 unsigned long time_left, xfer_time = 100;
1224 err = tegra_i2c_flush_fifos(i2c_dev);
1228 i2c_dev->msg_buf = msg->buf;
1229 i2c_dev->msg_buf_remaining = msg->len;
1230 i2c_dev->msg_err = I2C_ERR_NONE;
1231 i2c_dev->msg_read = !!(msg->flags & I2C_M_RD);
1232 reinit_completion(&i2c_dev->msg_complete);
1234 if (i2c_dev->msg_read)
1235 xfer_size = msg->len;
1237 xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1239 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1241 i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN &&
1242 i2c_dev->dma_buf && !i2c_dev->atomic_mode;
1244 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1247 * Transfer time in mSec = Total bits / transfer rate
1248 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1250 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1251 i2c_dev->bus_clk_rate);
1253 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1254 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1256 if (i2c_dev->dma_mode) {
1257 if (i2c_dev->msg_read) {
1258 dma_sync_single_for_device(i2c_dev->dev,
1260 xfer_size, DMA_FROM_DEVICE);
1262 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1266 dma_sync_single_for_cpu(i2c_dev->dev,
1268 xfer_size, DMA_TO_DEVICE);
1272 tegra_i2c_push_packet_header(i2c_dev, msg, end_state);
1274 if (!i2c_dev->msg_read) {
1275 if (i2c_dev->dma_mode) {
1276 memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE,
1277 msg->buf, msg->len);
1279 dma_sync_single_for_device(i2c_dev->dev,
1281 xfer_size, DMA_TO_DEVICE);
1283 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1287 tegra_i2c_fill_tx_fifo(i2c_dev);
1291 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1292 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
1294 if (!i2c_dev->dma_mode) {
1295 if (msg->flags & I2C_M_RD)
1296 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1297 else if (i2c_dev->msg_buf_remaining)
1298 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1301 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1302 dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n",
1303 i2c_readl(i2c_dev, I2C_INT_MASK));
1305 if (i2c_dev->dma_mode) {
1306 time_left = tegra_i2c_wait_completion(i2c_dev,
1307 &i2c_dev->dma_complete,
1311 * Synchronize DMA first, since dmaengine_terminate_sync()
1312 * performs synchronization after the transfer's termination
1313 * and we want to get a completion if transfer succeeded.
1315 dmaengine_synchronize(i2c_dev->msg_read ?
1316 i2c_dev->rx_dma_chan :
1317 i2c_dev->tx_dma_chan);
1319 dmaengine_terminate_sync(i2c_dev->msg_read ?
1320 i2c_dev->rx_dma_chan :
1321 i2c_dev->tx_dma_chan);
1323 if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
1324 dev_err(i2c_dev->dev, "DMA transfer timed out\n");
1325 tegra_i2c_init(i2c_dev);
1329 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1330 dma_sync_single_for_cpu(i2c_dev->dev,
1332 xfer_size, DMA_FROM_DEVICE);
1334 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, msg->len);
1338 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete,
1341 tegra_i2c_mask_irq(i2c_dev, int_mask);
1343 if (time_left == 0) {
1344 dev_err(i2c_dev->dev, "I2C transfer timed out\n");
1345 tegra_i2c_init(i2c_dev);
1349 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1350 time_left, completion_done(&i2c_dev->msg_complete),
1353 i2c_dev->dma_mode = false;
1355 err = tegra_i2c_error_recover(i2c_dev, msg);
1362 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1365 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1368 ret = pm_runtime_get_sync(i2c_dev->dev);
1370 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
1371 pm_runtime_put_noidle(i2c_dev->dev);
1375 for (i = 0; i < num; i++) {
1376 enum msg_end_type end_type = MSG_END_STOP;
1378 if (i < (num - 1)) {
1379 /* check whether follow up message is coming */
1380 if (msgs[i + 1].flags & I2C_M_NOSTART)
1381 end_type = MSG_END_CONTINUE;
1383 end_type = MSG_END_REPEAT_START;
1385 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
1390 pm_runtime_put(i2c_dev->dev);
1395 static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
1396 struct i2c_msg msgs[], int num)
1398 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1401 i2c_dev->atomic_mode = true;
1402 ret = tegra_i2c_xfer(adap, msgs, num);
1403 i2c_dev->atomic_mode = false;
1408 static u32 tegra_i2c_func(struct i2c_adapter *adap)
1410 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1411 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1412 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
1414 if (i2c_dev->hw->has_continue_xfer_support)
1415 ret |= I2C_FUNC_NOSTART;
1420 static const struct i2c_algorithm tegra_i2c_algo = {
1421 .master_xfer = tegra_i2c_xfer,
1422 .master_xfer_atomic = tegra_i2c_xfer_atomic,
1423 .functionality = tegra_i2c_func,
1426 /* payload size is only 12 bit */
1427 static const struct i2c_adapter_quirks tegra_i2c_quirks = {
1428 .flags = I2C_AQ_NO_ZERO_LEN,
1429 .max_read_len = SZ_4K,
1430 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
1433 static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1434 .flags = I2C_AQ_NO_ZERO_LEN,
1435 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
1438 static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1439 .recover_bus = tegra_i2c_issue_bus_clear,
1442 static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1443 .has_continue_xfer_support = false,
1444 .has_per_pkt_xfer_complete_irq = false,
1445 .clk_divisor_hs_mode = 3,
1446 .clk_divisor_std_mode = 0,
1447 .clk_divisor_fast_mode = 0,
1448 .clk_divisor_fast_plus_mode = 0,
1449 .has_config_load_reg = false,
1450 .has_multi_master_mode = false,
1451 .has_slcg_override_reg = false,
1452 .has_mst_fifo = false,
1453 .quirks = &tegra_i2c_quirks,
1454 .supports_bus_clear = false,
1455 .has_apb_dma = true,
1456 .tlow_std_mode = 0x4,
1457 .thigh_std_mode = 0x2,
1458 .tlow_fast_fastplus_mode = 0x4,
1459 .thigh_fast_fastplus_mode = 0x2,
1460 .setup_hold_time_std_mode = 0x0,
1461 .setup_hold_time_fast_fast_plus_mode = 0x0,
1462 .setup_hold_time_hs_mode = 0x0,
1463 .has_interface_timing_reg = false,
1466 static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1467 .has_continue_xfer_support = true,
1468 .has_per_pkt_xfer_complete_irq = false,
1469 .clk_divisor_hs_mode = 3,
1470 .clk_divisor_std_mode = 0,
1471 .clk_divisor_fast_mode = 0,
1472 .clk_divisor_fast_plus_mode = 0,
1473 .has_config_load_reg = false,
1474 .has_multi_master_mode = false,
1475 .has_slcg_override_reg = false,
1476 .has_mst_fifo = false,
1477 .quirks = &tegra_i2c_quirks,
1478 .supports_bus_clear = false,
1479 .has_apb_dma = true,
1480 .tlow_std_mode = 0x4,
1481 .thigh_std_mode = 0x2,
1482 .tlow_fast_fastplus_mode = 0x4,
1483 .thigh_fast_fastplus_mode = 0x2,
1484 .setup_hold_time_std_mode = 0x0,
1485 .setup_hold_time_fast_fast_plus_mode = 0x0,
1486 .setup_hold_time_hs_mode = 0x0,
1487 .has_interface_timing_reg = false,
1490 static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1491 .has_continue_xfer_support = true,
1492 .has_per_pkt_xfer_complete_irq = true,
1493 .clk_divisor_hs_mode = 1,
1494 .clk_divisor_std_mode = 0x19,
1495 .clk_divisor_fast_mode = 0x19,
1496 .clk_divisor_fast_plus_mode = 0x10,
1497 .has_config_load_reg = false,
1498 .has_multi_master_mode = false,
1499 .has_slcg_override_reg = false,
1500 .has_mst_fifo = false,
1501 .quirks = &tegra_i2c_quirks,
1502 .supports_bus_clear = true,
1503 .has_apb_dma = true,
1504 .tlow_std_mode = 0x4,
1505 .thigh_std_mode = 0x2,
1506 .tlow_fast_fastplus_mode = 0x4,
1507 .thigh_fast_fastplus_mode = 0x2,
1508 .setup_hold_time_std_mode = 0x0,
1509 .setup_hold_time_fast_fast_plus_mode = 0x0,
1510 .setup_hold_time_hs_mode = 0x0,
1511 .has_interface_timing_reg = false,
1514 static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1515 .has_continue_xfer_support = true,
1516 .has_per_pkt_xfer_complete_irq = true,
1517 .clk_divisor_hs_mode = 1,
1518 .clk_divisor_std_mode = 0x19,
1519 .clk_divisor_fast_mode = 0x19,
1520 .clk_divisor_fast_plus_mode = 0x10,
1521 .has_config_load_reg = true,
1522 .has_multi_master_mode = false,
1523 .has_slcg_override_reg = true,
1524 .has_mst_fifo = false,
1525 .quirks = &tegra_i2c_quirks,
1526 .supports_bus_clear = true,
1527 .has_apb_dma = true,
1528 .tlow_std_mode = 0x4,
1529 .thigh_std_mode = 0x2,
1530 .tlow_fast_fastplus_mode = 0x4,
1531 .thigh_fast_fastplus_mode = 0x2,
1532 .setup_hold_time_std_mode = 0x0,
1533 .setup_hold_time_fast_fast_plus_mode = 0x0,
1534 .setup_hold_time_hs_mode = 0x0,
1535 .has_interface_timing_reg = true,
1538 static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1539 .has_continue_xfer_support = true,
1540 .has_per_pkt_xfer_complete_irq = true,
1541 .clk_divisor_hs_mode = 1,
1542 .clk_divisor_std_mode = 0x19,
1543 .clk_divisor_fast_mode = 0x19,
1544 .clk_divisor_fast_plus_mode = 0x10,
1545 .has_config_load_reg = true,
1546 .has_multi_master_mode = false,
1547 .has_slcg_override_reg = true,
1548 .has_mst_fifo = false,
1549 .quirks = &tegra_i2c_quirks,
1550 .supports_bus_clear = true,
1551 .has_apb_dma = true,
1552 .tlow_std_mode = 0x4,
1553 .thigh_std_mode = 0x2,
1554 .tlow_fast_fastplus_mode = 0x4,
1555 .thigh_fast_fastplus_mode = 0x2,
1556 .setup_hold_time_std_mode = 0,
1557 .setup_hold_time_fast_fast_plus_mode = 0,
1558 .setup_hold_time_hs_mode = 0,
1559 .has_interface_timing_reg = true,
1562 static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1563 .has_continue_xfer_support = true,
1564 .has_per_pkt_xfer_complete_irq = true,
1565 .clk_divisor_hs_mode = 1,
1566 .clk_divisor_std_mode = 0x16,
1567 .clk_divisor_fast_mode = 0x19,
1568 .clk_divisor_fast_plus_mode = 0x10,
1569 .has_config_load_reg = true,
1570 .has_multi_master_mode = false,
1571 .has_slcg_override_reg = true,
1572 .has_mst_fifo = false,
1573 .quirks = &tegra_i2c_quirks,
1574 .supports_bus_clear = true,
1575 .has_apb_dma = false,
1576 .tlow_std_mode = 0x4,
1577 .thigh_std_mode = 0x3,
1578 .tlow_fast_fastplus_mode = 0x4,
1579 .thigh_fast_fastplus_mode = 0x2,
1580 .setup_hold_time_std_mode = 0,
1581 .setup_hold_time_fast_fast_plus_mode = 0,
1582 .setup_hold_time_hs_mode = 0,
1583 .has_interface_timing_reg = true,
1586 static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1587 .has_continue_xfer_support = true,
1588 .has_per_pkt_xfer_complete_irq = true,
1589 .clk_divisor_hs_mode = 1,
1590 .clk_divisor_std_mode = 0x4f,
1591 .clk_divisor_fast_mode = 0x3c,
1592 .clk_divisor_fast_plus_mode = 0x16,
1593 .has_config_load_reg = true,
1594 .has_multi_master_mode = true,
1595 .has_slcg_override_reg = true,
1596 .has_mst_fifo = true,
1597 .quirks = &tegra194_i2c_quirks,
1598 .supports_bus_clear = true,
1599 .has_apb_dma = false,
1600 .tlow_std_mode = 0x8,
1601 .thigh_std_mode = 0x7,
1602 .tlow_fast_fastplus_mode = 0x2,
1603 .thigh_fast_fastplus_mode = 0x2,
1604 .setup_hold_time_std_mode = 0x08080808,
1605 .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1606 .setup_hold_time_hs_mode = 0x090909,
1607 .has_interface_timing_reg = true,
1610 static const struct of_device_id tegra_i2c_of_match[] = {
1611 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
1612 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
1613 { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_i2c_hw, },
1614 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
1615 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
1616 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
1617 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1618 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1619 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1622 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
1624 static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1626 struct device_node *np = i2c_dev->dev->of_node;
1630 err = of_property_read_u32(np, "clock-frequency",
1631 &i2c_dev->bus_clk_rate);
1633 i2c_dev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ;
1635 multi_mode = of_property_read_bool(np, "multi-master");
1636 i2c_dev->multimaster_mode = multi_mode;
1638 if (of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc"))
1639 i2c_dev->is_dvc = true;
1641 if (of_device_is_compatible(np, "nvidia,tegra210-i2c-vi"))
1642 i2c_dev->is_vi = true;
1645 static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev)
1649 i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk";
1651 if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw)
1652 i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk";
1655 i2c_dev->clocks[i2c_dev->nclocks++].id = "slow";
1657 err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks,
1662 err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks);
1666 i2c_dev->div_clk = i2c_dev->clocks[0].clk;
1668 if (!i2c_dev->multimaster_mode)
1671 err = clk_enable(i2c_dev->div_clk);
1673 dev_err(i2c_dev->dev, "failed to enable div-clk: %d\n", err);
1674 goto unprepare_clocks;
1680 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
1685 static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev)
1687 if (i2c_dev->multimaster_mode)
1688 clk_disable(i2c_dev->div_clk);
1690 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
1693 static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev)
1697 ret = pm_runtime_get_sync(i2c_dev->dev);
1699 dev_err(i2c_dev->dev, "runtime resume failed: %d\n", ret);
1701 ret = tegra_i2c_init(i2c_dev);
1703 pm_runtime_put(i2c_dev->dev);
1708 static int tegra_i2c_probe(struct platform_device *pdev)
1710 struct tegra_i2c_dev *i2c_dev;
1711 struct resource *res;
1714 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1718 platform_set_drvdata(pdev, i2c_dev);
1720 init_completion(&i2c_dev->msg_complete);
1721 init_completion(&i2c_dev->dma_complete);
1723 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
1724 i2c_dev->cont_id = pdev->id;
1725 i2c_dev->dev = &pdev->dev;
1727 i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1728 if (IS_ERR(i2c_dev->base))
1729 return PTR_ERR(i2c_dev->base);
1731 i2c_dev->base_phys = res->start;
1733 err = platform_get_irq(pdev, 0);
1739 /* interrupt will be enabled during of transfer time */
1740 irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
1742 err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq,
1743 NULL, tegra_i2c_isr,
1744 IRQF_NO_SUSPEND | IRQF_ONESHOT,
1745 dev_name(i2c_dev->dev), i2c_dev);
1749 i2c_dev->rst = devm_reset_control_get_exclusive(i2c_dev->dev, "i2c");
1750 if (IS_ERR(i2c_dev->rst)) {
1751 dev_err_probe(i2c_dev->dev, PTR_ERR(i2c_dev->rst),
1752 "failed to get reset control\n");
1753 return PTR_ERR(i2c_dev->rst);
1756 tegra_i2c_parse_dt(i2c_dev);
1758 err = tegra_i2c_init_clocks(i2c_dev);
1762 err = tegra_i2c_init_dma(i2c_dev);
1764 goto release_clocks;
1767 * VI I2C is in VE power domain which is not always ON and not
1768 * IRQ-safe. Thus, IRQ-safe device shouldn't be attached to a
1769 * non IRQ-safe domain because this prevents powering off the power
1772 * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't
1773 * be used for atomic transfers.
1775 if (!i2c_dev->is_vi)
1776 pm_runtime_irq_safe(i2c_dev->dev);
1778 pm_runtime_enable(i2c_dev->dev);
1780 err = tegra_i2c_init_hardware(i2c_dev);
1784 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1785 i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node;
1786 i2c_dev->adapter.dev.parent = i2c_dev->dev;
1787 i2c_dev->adapter.retries = 1;
1788 i2c_dev->adapter.timeout = 6 * HZ;
1789 i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
1790 i2c_dev->adapter.owner = THIS_MODULE;
1791 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
1792 i2c_dev->adapter.algo = &tegra_i2c_algo;
1793 i2c_dev->adapter.nr = pdev->id;
1795 if (i2c_dev->hw->supports_bus_clear)
1796 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1798 strlcpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev),
1799 sizeof(i2c_dev->adapter.name));
1801 err = i2c_add_numbered_adapter(&i2c_dev->adapter);
1808 pm_runtime_disable(i2c_dev->dev);
1810 tegra_i2c_release_dma(i2c_dev);
1812 tegra_i2c_release_clocks(i2c_dev);
1817 static int tegra_i2c_remove(struct platform_device *pdev)
1819 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1821 i2c_del_adapter(&i2c_dev->adapter);
1822 pm_runtime_disable(i2c_dev->dev);
1824 tegra_i2c_release_dma(i2c_dev);
1825 tegra_i2c_release_clocks(i2c_dev);
1830 static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
1832 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1835 err = pinctrl_pm_select_default_state(dev);
1839 err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks);
1844 * VI I2C device is attached to VE power domain which goes through
1845 * power ON/OFF during runtime PM resume/suspend, meaning that
1846 * controller needs to be re-initialized after power ON.
1848 if (i2c_dev->is_vi) {
1849 err = tegra_i2c_init(i2c_dev);
1851 goto disable_clocks;
1857 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
1862 static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
1864 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1866 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
1868 return pinctrl_pm_select_idle_state(dev);
1871 static int __maybe_unused tegra_i2c_suspend(struct device *dev)
1873 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1876 i2c_mark_adapter_suspended(&i2c_dev->adapter);
1878 if (!pm_runtime_status_suspended(dev)) {
1879 err = tegra_i2c_runtime_suspend(dev);
1887 static int __maybe_unused tegra_i2c_resume(struct device *dev)
1889 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1893 * We need to ensure that clocks are enabled so that registers can be
1894 * restored in tegra_i2c_init().
1896 err = tegra_i2c_runtime_resume(dev);
1900 err = tegra_i2c_init(i2c_dev);
1905 * In case we are runtime suspended, disable clocks again so that we
1906 * don't unbalance the clock reference counts during the next runtime
1907 * resume transition.
1909 if (pm_runtime_status_suspended(dev)) {
1910 err = tegra_i2c_runtime_suspend(dev);
1915 i2c_mark_adapter_resumed(&i2c_dev->adapter);
1920 static const struct dev_pm_ops tegra_i2c_pm = {
1921 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1922 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1926 static struct platform_driver tegra_i2c_driver = {
1927 .probe = tegra_i2c_probe,
1928 .remove = tegra_i2c_remove,
1930 .name = "tegra-i2c",
1931 .of_match_table = tegra_i2c_of_match,
1932 .pm = &tegra_i2c_pm,
1935 module_platform_driver(tegra_i2c_driver);
1937 MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver");
1938 MODULE_AUTHOR("Colin Cross");
1939 MODULE_LICENSE("GPL v2");