Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/i2c/busses/i2c-tegra.c
0004  *
0005  * Copyright (C) 2010 Google, Inc.
0006  * Author: Colin Cross <ccross@android.com>
0007  */
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/bitfield.h>
0011 #include <linux/clk.h>
0012 #include <linux/delay.h>
0013 #include <linux/dmaengine.h>
0014 #include <linux/dma-mapping.h>
0015 #include <linux/err.h>
0016 #include <linux/i2c.h>
0017 #include <linux/init.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/io.h>
0020 #include <linux/iopoll.h>
0021 #include <linux/irq.h>
0022 #include <linux/kernel.h>
0023 #include <linux/ktime.h>
0024 #include <linux/module.h>
0025 #include <linux/of_device.h>
0026 #include <linux/pinctrl/consumer.h>
0027 #include <linux/platform_device.h>
0028 #include <linux/pm_runtime.h>
0029 #include <linux/reset.h>
0030 
0031 #define BYTES_PER_FIFO_WORD 4
0032 
0033 #define I2C_CNFG                0x000
0034 #define I2C_CNFG_DEBOUNCE_CNT           GENMASK(14, 12)
0035 #define I2C_CNFG_PACKET_MODE_EN         BIT(10)
0036 #define I2C_CNFG_NEW_MASTER_FSM         BIT(11)
0037 #define I2C_CNFG_MULTI_MASTER_MODE      BIT(17)
0038 #define I2C_STATUS              0x01c
0039 #define I2C_SL_CNFG             0x020
0040 #define I2C_SL_CNFG_NACK            BIT(1)
0041 #define I2C_SL_CNFG_NEWSL           BIT(2)
0042 #define I2C_SL_ADDR1                0x02c
0043 #define I2C_SL_ADDR2                0x030
0044 #define I2C_TLOW_SEXT               0x034
0045 #define I2C_TX_FIFO             0x050
0046 #define I2C_RX_FIFO             0x054
0047 #define I2C_PACKET_TRANSFER_STATUS      0x058
0048 #define I2C_FIFO_CONTROL            0x05c
0049 #define I2C_FIFO_CONTROL_TX_FLUSH       BIT(1)
0050 #define I2C_FIFO_CONTROL_RX_FLUSH       BIT(0)
0051 #define I2C_FIFO_CONTROL_TX_TRIG(x)     (((x) - 1) << 5)
0052 #define I2C_FIFO_CONTROL_RX_TRIG(x)     (((x) - 1) << 2)
0053 #define I2C_FIFO_STATUS             0x060
0054 #define I2C_FIFO_STATUS_TX          GENMASK(7, 4)
0055 #define I2C_FIFO_STATUS_RX          GENMASK(3, 0)
0056 #define I2C_INT_MASK                0x064
0057 #define I2C_INT_STATUS              0x068
0058 #define I2C_INT_BUS_CLR_DONE            BIT(11)
0059 #define I2C_INT_PACKET_XFER_COMPLETE        BIT(7)
0060 #define I2C_INT_NO_ACK              BIT(3)
0061 #define I2C_INT_ARBITRATION_LOST        BIT(2)
0062 #define I2C_INT_TX_FIFO_DATA_REQ        BIT(1)
0063 #define I2C_INT_RX_FIFO_DATA_REQ        BIT(0)
0064 #define I2C_CLK_DIVISOR             0x06c
0065 #define I2C_CLK_DIVISOR_STD_FAST_MODE       GENMASK(31, 16)
0066 #define I2C_CLK_DIVISOR_HSMODE          GENMASK(15, 0)
0067 
0068 #define DVC_CTRL_REG1               0x000
0069 #define DVC_CTRL_REG1_INTR_EN           BIT(10)
0070 #define DVC_CTRL_REG3               0x008
0071 #define DVC_CTRL_REG3_SW_PROG           BIT(26)
0072 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN      BIT(30)
0073 #define DVC_STATUS              0x00c
0074 #define DVC_STATUS_I2C_DONE_INTR        BIT(30)
0075 
0076 #define I2C_ERR_NONE                0x00
0077 #define I2C_ERR_NO_ACK              BIT(0)
0078 #define I2C_ERR_ARBITRATION_LOST        BIT(1)
0079 #define I2C_ERR_UNKNOWN_INTERRUPT       BIT(2)
0080 #define I2C_ERR_RX_BUFFER_OVERFLOW      BIT(3)
0081 
0082 #define PACKET_HEADER0_HEADER_SIZE      GENMASK(29, 28)
0083 #define PACKET_HEADER0_PACKET_ID        GENMASK(23, 16)
0084 #define PACKET_HEADER0_CONT_ID          GENMASK(15, 12)
0085 #define PACKET_HEADER0_PROTOCOL         GENMASK(7, 4)
0086 #define PACKET_HEADER0_PROTOCOL_I2C     1
0087 
0088 #define I2C_HEADER_CONT_ON_NAK          BIT(21)
0089 #define I2C_HEADER_READ             BIT(19)
0090 #define I2C_HEADER_10BIT_ADDR           BIT(18)
0091 #define I2C_HEADER_IE_ENABLE            BIT(17)
0092 #define I2C_HEADER_REPEAT_START         BIT(16)
0093 #define I2C_HEADER_CONTINUE_XFER        BIT(15)
0094 #define I2C_HEADER_SLAVE_ADDR_SHIFT     1
0095 
0096 #define I2C_BUS_CLEAR_CNFG          0x084
0097 #define I2C_BC_SCLK_THRESHOLD           GENMASK(23, 16)
0098 #define I2C_BC_STOP_COND            BIT(2)
0099 #define I2C_BC_TERMINATE            BIT(1)
0100 #define I2C_BC_ENABLE               BIT(0)
0101 #define I2C_BUS_CLEAR_STATUS            0x088
0102 #define I2C_BC_STATUS               BIT(0)
0103 
0104 #define I2C_CONFIG_LOAD             0x08c
0105 #define I2C_MSTR_CONFIG_LOAD            BIT(0)
0106 
0107 #define I2C_CLKEN_OVERRIDE          0x090
0108 #define I2C_MST_CORE_CLKEN_OVR          BIT(0)
0109 
0110 #define I2C_INTERFACE_TIMING_0          0x094
0111 #define  I2C_INTERFACE_TIMING_THIGH     GENMASK(13, 8)
0112 #define  I2C_INTERFACE_TIMING_TLOW      GENMASK(5, 0)
0113 #define I2C_INTERFACE_TIMING_1          0x098
0114 #define  I2C_INTERFACE_TIMING_TBUF      GENMASK(29, 24)
0115 #define  I2C_INTERFACE_TIMING_TSU_STO       GENMASK(21, 16)
0116 #define  I2C_INTERFACE_TIMING_THD_STA       GENMASK(13, 8)
0117 #define  I2C_INTERFACE_TIMING_TSU_STA       GENMASK(5, 0)
0118 
0119 #define I2C_HS_INTERFACE_TIMING_0       0x09c
0120 #define  I2C_HS_INTERFACE_TIMING_THIGH      GENMASK(13, 8)
0121 #define  I2C_HS_INTERFACE_TIMING_TLOW       GENMASK(5, 0)
0122 #define I2C_HS_INTERFACE_TIMING_1       0x0a0
0123 #define  I2C_HS_INTERFACE_TIMING_TSU_STO    GENMASK(21, 16)
0124 #define  I2C_HS_INTERFACE_TIMING_THD_STA    GENMASK(13, 8)
0125 #define  I2C_HS_INTERFACE_TIMING_TSU_STA    GENMASK(5, 0)
0126 
0127 #define I2C_MST_FIFO_CONTROL            0x0b4
0128 #define I2C_MST_FIFO_CONTROL_RX_FLUSH       BIT(0)
0129 #define I2C_MST_FIFO_CONTROL_TX_FLUSH       BIT(1)
0130 #define I2C_MST_FIFO_CONTROL_RX_TRIG(x)     (((x) - 1) <<  4)
0131 #define I2C_MST_FIFO_CONTROL_TX_TRIG(x)     (((x) - 1) << 16)
0132 
0133 #define I2C_MST_FIFO_STATUS         0x0b8
0134 #define I2C_MST_FIFO_STATUS_TX          GENMASK(23, 16)
0135 #define I2C_MST_FIFO_STATUS_RX          GENMASK(7, 0)
0136 
0137 /* configuration load timeout in microseconds */
0138 #define I2C_CONFIG_LOAD_TIMEOUT         1000000
0139 
0140 /* packet header size in bytes */
0141 #define I2C_PACKET_HEADER_SIZE          12
0142 
0143 /*
0144  * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
0145  * avoid DMA overhead, otherwise external APB DMA controller will be used.
0146  * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
0147  * I2C_PACKET_HEADER_SIZE.
0148  */
0149 #define I2C_PIO_MODE_PREFERRED_LEN      32
0150 
0151 /*
0152  * msg_end_type: The bus control which needs to be sent at end of transfer.
0153  * @MSG_END_STOP: Send stop pulse.
0154  * @MSG_END_REPEAT_START: Send repeat-start.
0155  * @MSG_END_CONTINUE: Don't send stop or repeat-start.
0156  */
0157 enum msg_end_type {
0158     MSG_END_STOP,
0159     MSG_END_REPEAT_START,
0160     MSG_END_CONTINUE,
0161 };
0162 
0163 /**
0164  * struct tegra_i2c_hw_feature : per hardware generation features
0165  * @has_continue_xfer_support: continue-transfer supported
0166  * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
0167  *      completion interrupt on per packet basis.
0168  * @has_config_load_reg: Has the config load register to load the new
0169  *      configuration.
0170  * @clk_divisor_hs_mode: Clock divisor in HS mode.
0171  * @clk_divisor_std_mode: Clock divisor in standard mode. It is
0172  *      applicable if there is no fast clock source i.e. single clock
0173  *      source.
0174  * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
0175  *      applicable if there is no fast clock source i.e. single clock
0176  *      source.
0177  * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
0178  *      applicable if there is no fast clock source (i.e. single
0179  *      clock source).
0180  * @has_multi_master_mode: The I2C controller supports running in single-master
0181  *      or multi-master mode.
0182  * @has_slcg_override_reg: The I2C controller supports a register that
0183  *      overrides the second level clock gating.
0184  * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
0185  *      provides additional features and allows for longer messages to
0186  *      be transferred in one go.
0187  * @quirks: I2C adapter quirks for limiting write/read transfer size and not
0188  *      allowing 0 length transfers.
0189  * @supports_bus_clear: Bus Clear support to recover from bus hang during
0190  *      SDA stuck low from device for some unknown reasons.
0191  * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
0192  * @tlow_std_mode: Low period of the clock in standard mode.
0193  * @thigh_std_mode: High period of the clock in standard mode.
0194  * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
0195  * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
0196  * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
0197  *      in standard mode.
0198  * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
0199  *      conditions in fast/fast-plus modes.
0200  * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
0201  *      in HS mode.
0202  * @has_interface_timing_reg: Has interface timing register to program the tuned
0203  *      timing settings.
0204  */
0205 struct tegra_i2c_hw_feature {
0206     bool has_continue_xfer_support;
0207     bool has_per_pkt_xfer_complete_irq;
0208     bool has_config_load_reg;
0209     u32 clk_divisor_hs_mode;
0210     u32 clk_divisor_std_mode;
0211     u32 clk_divisor_fast_mode;
0212     u32 clk_divisor_fast_plus_mode;
0213     bool has_multi_master_mode;
0214     bool has_slcg_override_reg;
0215     bool has_mst_fifo;
0216     const struct i2c_adapter_quirks *quirks;
0217     bool supports_bus_clear;
0218     bool has_apb_dma;
0219     u32 tlow_std_mode;
0220     u32 thigh_std_mode;
0221     u32 tlow_fast_fastplus_mode;
0222     u32 thigh_fast_fastplus_mode;
0223     u32 setup_hold_time_std_mode;
0224     u32 setup_hold_time_fast_fast_plus_mode;
0225     u32 setup_hold_time_hs_mode;
0226     bool has_interface_timing_reg;
0227 };
0228 
0229 /**
0230  * struct tegra_i2c_dev - per device I2C context
0231  * @dev: device reference for power management
0232  * @hw: Tegra I2C HW feature
0233  * @adapter: core I2C layer adapter information
0234  * @div_clk: clock reference for div clock of I2C controller
0235  * @clocks: array of I2C controller clocks
0236  * @nclocks: number of clocks in the array
0237  * @rst: reset control for the I2C controller
0238  * @base: ioremapped registers cookie
0239  * @base_phys: physical base address of the I2C controller
0240  * @cont_id: I2C controller ID, used for packet header
0241  * @irq: IRQ number of transfer complete interrupt
0242  * @is_dvc: identifies the DVC I2C controller, has a different register layout
0243  * @is_vi: identifies the VI I2C controller, has a different register layout
0244  * @msg_complete: transfer completion notifier
0245  * @msg_err: error code for completed message
0246  * @msg_buf: pointer to current message data
0247  * @msg_buf_remaining: size of unsent data in the message buffer
0248  * @msg_read: indicates that the transfer is a read access
0249  * @timings: i2c timings information like bus frequency
0250  * @multimaster_mode: indicates that I2C controller is in multi-master mode
0251  * @tx_dma_chan: DMA transmit channel
0252  * @rx_dma_chan: DMA receive channel
0253  * @dma_phys: handle to DMA resources
0254  * @dma_buf: pointer to allocated DMA buffer
0255  * @dma_buf_size: DMA buffer size
0256  * @dma_mode: indicates active DMA transfer
0257  * @dma_complete: DMA completion notifier
0258  * @atomic_mode: indicates active atomic transfer
0259  */
0260 struct tegra_i2c_dev {
0261     struct device *dev;
0262     struct i2c_adapter adapter;
0263 
0264     const struct tegra_i2c_hw_feature *hw;
0265     struct reset_control *rst;
0266     unsigned int cont_id;
0267     unsigned int irq;
0268 
0269     phys_addr_t base_phys;
0270     void __iomem *base;
0271 
0272     struct clk_bulk_data clocks[2];
0273     unsigned int nclocks;
0274 
0275     struct clk *div_clk;
0276     struct i2c_timings timings;
0277 
0278     struct completion msg_complete;
0279     size_t msg_buf_remaining;
0280     int msg_err;
0281     u8 *msg_buf;
0282 
0283     struct completion dma_complete;
0284     struct dma_chan *tx_dma_chan;
0285     struct dma_chan *rx_dma_chan;
0286     unsigned int dma_buf_size;
0287     dma_addr_t dma_phys;
0288     void *dma_buf;
0289 
0290     bool multimaster_mode;
0291     bool atomic_mode;
0292     bool dma_mode;
0293     bool msg_read;
0294     bool is_dvc;
0295     bool is_vi;
0296 };
0297 
0298 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
0299                unsigned int reg)
0300 {
0301     writel_relaxed(val, i2c_dev->base + reg);
0302 }
0303 
0304 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
0305 {
0306     return readl_relaxed(i2c_dev->base + reg);
0307 }
0308 
0309 /*
0310  * If necessary, i2c_writel() and i2c_readl() will offset the register
0311  * in order to talk to the I2C block inside the DVC block.
0312  */
0313 static u32 tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
0314 {
0315     if (i2c_dev->is_dvc)
0316         reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
0317     else if (i2c_dev->is_vi)
0318         reg = 0xc00 + (reg << 2);
0319 
0320     return reg;
0321 }
0322 
0323 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg)
0324 {
0325     writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
0326 
0327     /* read back register to make sure that register writes completed */
0328     if (reg != I2C_TX_FIFO)
0329         readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
0330     else if (i2c_dev->is_vi)
0331         readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, I2C_INT_STATUS));
0332 }
0333 
0334 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg)
0335 {
0336     return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
0337 }
0338 
0339 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
0340             unsigned int reg, unsigned int len)
0341 {
0342     writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
0343 }
0344 
0345 static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data,
0346                unsigned int reg, unsigned int len)
0347 {
0348     u32 *data32 = data;
0349 
0350     /*
0351      * VI I2C controller has known hardware bug where writes get stuck
0352      * when immediate multiple writes happen to TX_FIFO register.
0353      * Recommended software work around is to read I2C register after
0354      * each write to TX_FIFO register to flush out the data.
0355      */
0356     while (len--)
0357         i2c_writel(i2c_dev, *data32++, reg);
0358 }
0359 
0360 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
0361                unsigned int reg, unsigned int len)
0362 {
0363     readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
0364 }
0365 
0366 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
0367 {
0368     u32 int_mask;
0369 
0370     int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
0371     i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
0372 }
0373 
0374 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
0375 {
0376     u32 int_mask;
0377 
0378     int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
0379     i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
0380 }
0381 
0382 static void tegra_i2c_dma_complete(void *args)
0383 {
0384     struct tegra_i2c_dev *i2c_dev = args;
0385 
0386     complete(&i2c_dev->dma_complete);
0387 }
0388 
0389 static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
0390 {
0391     struct dma_async_tx_descriptor *dma_desc;
0392     enum dma_transfer_direction dir;
0393     struct dma_chan *chan;
0394 
0395     dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
0396 
0397     reinit_completion(&i2c_dev->dma_complete);
0398 
0399     dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
0400     chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
0401 
0402     dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
0403                            len, dir, DMA_PREP_INTERRUPT |
0404                            DMA_CTRL_ACK);
0405     if (!dma_desc) {
0406         dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n",
0407             i2c_dev->msg_read ? "RX" : "TX");
0408         return -EINVAL;
0409     }
0410 
0411     dma_desc->callback = tegra_i2c_dma_complete;
0412     dma_desc->callback_param = i2c_dev;
0413 
0414     dmaengine_submit(dma_desc);
0415     dma_async_issue_pending(chan);
0416 
0417     return 0;
0418 }
0419 
0420 static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
0421 {
0422     if (i2c_dev->dma_buf) {
0423         dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
0424                   i2c_dev->dma_buf, i2c_dev->dma_phys);
0425         i2c_dev->dma_buf = NULL;
0426     }
0427 
0428     if (i2c_dev->tx_dma_chan) {
0429         dma_release_channel(i2c_dev->tx_dma_chan);
0430         i2c_dev->tx_dma_chan = NULL;
0431     }
0432 
0433     if (i2c_dev->rx_dma_chan) {
0434         dma_release_channel(i2c_dev->rx_dma_chan);
0435         i2c_dev->rx_dma_chan = NULL;
0436     }
0437 }
0438 
0439 static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
0440 {
0441     struct dma_chan *chan;
0442     dma_addr_t dma_phys;
0443     u32 *dma_buf;
0444     int err;
0445 
0446     if (!i2c_dev->hw->has_apb_dma || i2c_dev->is_vi)
0447         return 0;
0448 
0449     if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
0450         dev_dbg(i2c_dev->dev, "DMA support not enabled\n");
0451         return 0;
0452     }
0453 
0454     chan = dma_request_chan(i2c_dev->dev, "rx");
0455     if (IS_ERR(chan)) {
0456         err = PTR_ERR(chan);
0457         goto err_out;
0458     }
0459 
0460     i2c_dev->rx_dma_chan = chan;
0461 
0462     chan = dma_request_chan(i2c_dev->dev, "tx");
0463     if (IS_ERR(chan)) {
0464         err = PTR_ERR(chan);
0465         goto err_out;
0466     }
0467 
0468     i2c_dev->tx_dma_chan = chan;
0469 
0470     i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len +
0471                 I2C_PACKET_HEADER_SIZE;
0472 
0473     dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
0474                      &dma_phys, GFP_KERNEL | __GFP_NOWARN);
0475     if (!dma_buf) {
0476         dev_err(i2c_dev->dev, "failed to allocate DMA buffer\n");
0477         err = -ENOMEM;
0478         goto err_out;
0479     }
0480 
0481     i2c_dev->dma_buf = dma_buf;
0482     i2c_dev->dma_phys = dma_phys;
0483 
0484     return 0;
0485 
0486 err_out:
0487     tegra_i2c_release_dma(i2c_dev);
0488     if (err != -EPROBE_DEFER) {
0489         dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
0490         dev_err(i2c_dev->dev, "falling back to PIO\n");
0491         return 0;
0492     }
0493 
0494     return err;
0495 }
0496 
0497 /*
0498  * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
0499  * block.  This block is identical to the rest of the I2C blocks, except that
0500  * it only supports master mode, it has registers moved around, and it needs
0501  * some extra init to get it into I2C mode.  The register moves are handled
0502  * by i2c_readl() and i2c_writel().
0503  */
0504 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
0505 {
0506     u32 val;
0507 
0508     val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
0509     val |= DVC_CTRL_REG3_SW_PROG;
0510     val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
0511     dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
0512 
0513     val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
0514     val |= DVC_CTRL_REG1_INTR_EN;
0515     dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
0516 }
0517 
0518 static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev)
0519 {
0520     u32 value;
0521 
0522     value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) |
0523         FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4);
0524     i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_0);
0525 
0526     value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) |
0527         FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) |
0528         FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) |
0529         FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4);
0530     i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_1);
0531 
0532     value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) |
0533         FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8);
0534     i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_0);
0535 
0536     value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) |
0537         FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) |
0538         FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11);
0539     i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_1);
0540 
0541     value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND;
0542     i2c_writel(i2c_dev, value, I2C_BUS_CLEAR_CNFG);
0543 
0544     i2c_writel(i2c_dev, 0x0, I2C_TLOW_SEXT);
0545 }
0546 
0547 static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev,
0548                    u32 reg, u32 mask, u32 delay_us,
0549                    u32 timeout_us)
0550 {
0551     void __iomem *addr = i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg);
0552     u32 val;
0553 
0554     if (!i2c_dev->atomic_mode)
0555         return readl_relaxed_poll_timeout(addr, val, !(val & mask),
0556                           delay_us, timeout_us);
0557 
0558     return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask),
0559                          delay_us, timeout_us);
0560 }
0561 
0562 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
0563 {
0564     u32 mask, val, offset;
0565     int err;
0566 
0567     if (i2c_dev->hw->has_mst_fifo) {
0568         mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
0569                I2C_MST_FIFO_CONTROL_RX_FLUSH;
0570         offset = I2C_MST_FIFO_CONTROL;
0571     } else {
0572         mask = I2C_FIFO_CONTROL_TX_FLUSH |
0573                I2C_FIFO_CONTROL_RX_FLUSH;
0574         offset = I2C_FIFO_CONTROL;
0575     }
0576 
0577     val = i2c_readl(i2c_dev, offset);
0578     val |= mask;
0579     i2c_writel(i2c_dev, val, offset);
0580 
0581     err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000);
0582     if (err) {
0583         dev_err(i2c_dev->dev, "failed to flush FIFO\n");
0584         return err;
0585     }
0586 
0587     return 0;
0588 }
0589 
0590 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
0591 {
0592     int err;
0593 
0594     if (!i2c_dev->hw->has_config_load_reg)
0595         return 0;
0596 
0597     i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
0598 
0599     err = tegra_i2c_poll_register(i2c_dev, I2C_CONFIG_LOAD, 0xffffffff,
0600                       1000, I2C_CONFIG_LOAD_TIMEOUT);
0601     if (err) {
0602         dev_err(i2c_dev->dev, "failed to load config\n");
0603         return err;
0604     }
0605 
0606     return 0;
0607 }
0608 
0609 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
0610 {
0611     u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode;
0612     acpi_handle handle = ACPI_HANDLE(i2c_dev->dev);
0613     struct i2c_timings *t = &i2c_dev->timings;
0614     int err;
0615 
0616     /*
0617      * The reset shouldn't ever fail in practice. The failure will be a
0618      * sign of a severe problem that needs to be resolved. Still we don't
0619      * want to fail the initialization completely because this may break
0620      * kernel boot up since voltage regulators use I2C. Hence, we will
0621      * emit a noisy warning on error, which won't stay unnoticed and
0622      * won't hose machine entirely.
0623      */
0624     if (handle)
0625         err = acpi_evaluate_object(handle, "_RST", NULL, NULL);
0626     else
0627         err = reset_control_reset(i2c_dev->rst);
0628 
0629     WARN_ON_ONCE(err);
0630 
0631     if (i2c_dev->is_dvc)
0632         tegra_dvc_init(i2c_dev);
0633 
0634     val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
0635           FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2);
0636 
0637     if (i2c_dev->hw->has_multi_master_mode)
0638         val |= I2C_CNFG_MULTI_MASTER_MODE;
0639 
0640     i2c_writel(i2c_dev, val, I2C_CNFG);
0641     i2c_writel(i2c_dev, 0, I2C_INT_MASK);
0642 
0643     if (i2c_dev->is_vi)
0644         tegra_i2c_vi_init(i2c_dev);
0645 
0646     switch (t->bus_freq_hz) {
0647     case I2C_MAX_STANDARD_MODE_FREQ + 1 ... I2C_MAX_FAST_MODE_PLUS_FREQ:
0648     default:
0649         tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
0650         thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
0651         tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
0652 
0653         if (t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ)
0654             non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode;
0655         else
0656             non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode;
0657         break;
0658 
0659     case 0 ... I2C_MAX_STANDARD_MODE_FREQ:
0660         tlow = i2c_dev->hw->tlow_std_mode;
0661         thigh = i2c_dev->hw->thigh_std_mode;
0662         tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
0663         non_hs_mode = i2c_dev->hw->clk_divisor_std_mode;
0664         break;
0665     }
0666 
0667     /* make sure clock divisor programmed correctly */
0668     clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE,
0669                  i2c_dev->hw->clk_divisor_hs_mode) |
0670               FIELD_PREP(I2C_CLK_DIVISOR_STD_FAST_MODE, non_hs_mode);
0671     i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
0672 
0673     if (i2c_dev->hw->has_interface_timing_reg) {
0674         val = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, thigh) |
0675               FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, tlow);
0676         i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
0677     }
0678 
0679     /*
0680      * Configure setup and hold times only when tsu_thd is non-zero.
0681      * Otherwise, preserve the chip default values.
0682      */
0683     if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
0684         i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
0685 
0686     clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1);
0687 
0688     err = clk_set_rate(i2c_dev->div_clk,
0689                t->bus_freq_hz * clk_multiplier);
0690     if (err) {
0691         dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err);
0692         return err;
0693     }
0694 
0695     if (!i2c_dev->is_dvc && !i2c_dev->is_vi) {
0696         u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
0697 
0698         sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
0699         i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
0700         i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
0701         i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
0702     }
0703 
0704     err = tegra_i2c_flush_fifos(i2c_dev);
0705     if (err)
0706         return err;
0707 
0708     if (i2c_dev->multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
0709         i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
0710 
0711     err = tegra_i2c_wait_for_config_load(i2c_dev);
0712     if (err)
0713         return err;
0714 
0715     return 0;
0716 }
0717 
0718 static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
0719 {
0720     u32 cnfg;
0721 
0722     /*
0723      * NACK interrupt is generated before the I2C controller generates
0724      * the STOP condition on the bus.  So, wait for 2 clock periods
0725      * before disabling the controller so that the STOP condition has
0726      * been delivered properly.
0727      */
0728     udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->timings.bus_freq_hz));
0729 
0730     cnfg = i2c_readl(i2c_dev, I2C_CNFG);
0731     if (cnfg & I2C_CNFG_PACKET_MODE_EN)
0732         i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
0733 
0734     return tegra_i2c_wait_for_config_load(i2c_dev);
0735 }
0736 
0737 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
0738 {
0739     size_t buf_remaining = i2c_dev->msg_buf_remaining;
0740     unsigned int words_to_transfer, rx_fifo_avail;
0741     u8 *buf = i2c_dev->msg_buf;
0742     u32 val;
0743 
0744     /*
0745      * Catch overflow due to message fully sent before the check for
0746      * RX FIFO availability.
0747      */
0748     if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
0749         return -EINVAL;
0750 
0751     if (i2c_dev->hw->has_mst_fifo) {
0752         val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
0753         rx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_RX, val);
0754     } else {
0755         val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
0756         rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val);
0757     }
0758 
0759     /* round down to exclude partial word at the end of buffer */
0760     words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
0761     if (words_to_transfer > rx_fifo_avail)
0762         words_to_transfer = rx_fifo_avail;
0763 
0764     i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
0765 
0766     buf += words_to_transfer * BYTES_PER_FIFO_WORD;
0767     buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
0768     rx_fifo_avail -= words_to_transfer;
0769 
0770     /*
0771      * If there is a partial word at the end of buffer, handle it
0772      * manually to prevent overwriting past the end of buffer.
0773      */
0774     if (rx_fifo_avail > 0 && buf_remaining > 0) {
0775         /*
0776          * buf_remaining > 3 check not needed as rx_fifo_avail == 0
0777          * when (words_to_transfer was > rx_fifo_avail) earlier
0778          * in this function.
0779          */
0780         val = i2c_readl(i2c_dev, I2C_RX_FIFO);
0781         val = cpu_to_le32(val);
0782         memcpy(buf, &val, buf_remaining);
0783         buf_remaining = 0;
0784         rx_fifo_avail--;
0785     }
0786 
0787     /* RX FIFO must be drained, otherwise it's an Overflow case. */
0788     if (WARN_ON_ONCE(rx_fifo_avail))
0789         return -EINVAL;
0790 
0791     i2c_dev->msg_buf_remaining = buf_remaining;
0792     i2c_dev->msg_buf = buf;
0793 
0794     return 0;
0795 }
0796 
0797 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
0798 {
0799     size_t buf_remaining = i2c_dev->msg_buf_remaining;
0800     unsigned int words_to_transfer, tx_fifo_avail;
0801     u8 *buf = i2c_dev->msg_buf;
0802     u32 val;
0803 
0804     if (i2c_dev->hw->has_mst_fifo) {
0805         val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
0806         tx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_TX, val);
0807     } else {
0808         val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
0809         tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val);
0810     }
0811 
0812     /* round down to exclude partial word at the end of buffer */
0813     words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
0814 
0815     /*
0816      * This hunk pushes 4 bytes at a time into the TX FIFO.
0817      *
0818      * It's very common to have < 4 bytes, hence there is no word
0819      * to push if we have less than 4 bytes to transfer.
0820      */
0821     if (words_to_transfer) {
0822         if (words_to_transfer > tx_fifo_avail)
0823             words_to_transfer = tx_fifo_avail;
0824 
0825         /*
0826          * Update state before writing to FIFO.  Note that this may
0827          * cause us to finish writing all bytes (AKA buf_remaining
0828          * goes to 0), hence we have a potential for an interrupt
0829          * (PACKET_XFER_COMPLETE is not maskable), but GIC interrupt
0830          * is disabled at this point.
0831          */
0832         buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
0833         tx_fifo_avail -= words_to_transfer;
0834 
0835         i2c_dev->msg_buf_remaining = buf_remaining;
0836         i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD;
0837 
0838         if (i2c_dev->is_vi)
0839             i2c_writesl_vi(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
0840         else
0841             i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
0842 
0843         buf += words_to_transfer * BYTES_PER_FIFO_WORD;
0844     }
0845 
0846     /*
0847      * If there is a partial word at the end of buffer, handle it manually
0848      * to prevent reading past the end of buffer, which could cross a page
0849      * boundary and fault.
0850      */
0851     if (tx_fifo_avail > 0 && buf_remaining > 0) {
0852         /*
0853          * buf_remaining > 3 check not needed as tx_fifo_avail == 0
0854          * when (words_to_transfer was > tx_fifo_avail) earlier
0855          * in this function for non-zero words_to_transfer.
0856          */
0857         memcpy(&val, buf, buf_remaining);
0858         val = le32_to_cpu(val);
0859 
0860         i2c_dev->msg_buf_remaining = 0;
0861         i2c_dev->msg_buf = NULL;
0862 
0863         i2c_writel(i2c_dev, val, I2C_TX_FIFO);
0864     }
0865 
0866     return 0;
0867 }
0868 
0869 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
0870 {
0871     const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
0872     struct tegra_i2c_dev *i2c_dev = dev_id;
0873     u32 status;
0874 
0875     status = i2c_readl(i2c_dev, I2C_INT_STATUS);
0876 
0877     if (status == 0) {
0878         dev_warn(i2c_dev->dev, "IRQ status 0 %08x %08x %08x\n",
0879              i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
0880              i2c_readl(i2c_dev, I2C_STATUS),
0881              i2c_readl(i2c_dev, I2C_CNFG));
0882         i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
0883         goto err;
0884     }
0885 
0886     if (status & status_err) {
0887         tegra_i2c_disable_packet_mode(i2c_dev);
0888         if (status & I2C_INT_NO_ACK)
0889             i2c_dev->msg_err |= I2C_ERR_NO_ACK;
0890         if (status & I2C_INT_ARBITRATION_LOST)
0891             i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
0892         goto err;
0893     }
0894 
0895     /*
0896      * I2C transfer is terminated during the bus clear, so skip
0897      * processing the other interrupts.
0898      */
0899     if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
0900         goto err;
0901 
0902     if (!i2c_dev->dma_mode) {
0903         if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
0904             if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
0905                 /*
0906                  * Overflow error condition: message fully sent,
0907                  * with no XFER_COMPLETE interrupt but hardware
0908                  * asks to transfer more.
0909                  */
0910                 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
0911                 goto err;
0912             }
0913         }
0914 
0915         if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
0916             if (i2c_dev->msg_buf_remaining)
0917                 tegra_i2c_fill_tx_fifo(i2c_dev);
0918             else
0919                 tegra_i2c_mask_irq(i2c_dev,
0920                            I2C_INT_TX_FIFO_DATA_REQ);
0921         }
0922     }
0923 
0924     i2c_writel(i2c_dev, status, I2C_INT_STATUS);
0925     if (i2c_dev->is_dvc)
0926         dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
0927 
0928     /*
0929      * During message read XFER_COMPLETE interrupt is triggered prior to
0930      * DMA completion and during message write XFER_COMPLETE interrupt is
0931      * triggered after DMA completion.
0932      *
0933      * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer,
0934      * so forcing msg_buf_remaining to 0 in DMA mode.
0935      */
0936     if (status & I2C_INT_PACKET_XFER_COMPLETE) {
0937         if (i2c_dev->dma_mode)
0938             i2c_dev->msg_buf_remaining = 0;
0939         /*
0940          * Underflow error condition: XFER_COMPLETE before message
0941          * fully sent.
0942          */
0943         if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
0944             i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
0945             goto err;
0946         }
0947         complete(&i2c_dev->msg_complete);
0948     }
0949     goto done;
0950 err:
0951     /* mask all interrupts on error */
0952     tegra_i2c_mask_irq(i2c_dev,
0953                I2C_INT_NO_ACK |
0954                I2C_INT_ARBITRATION_LOST |
0955                I2C_INT_PACKET_XFER_COMPLETE |
0956                I2C_INT_TX_FIFO_DATA_REQ |
0957                I2C_INT_RX_FIFO_DATA_REQ);
0958 
0959     if (i2c_dev->hw->supports_bus_clear)
0960         tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
0961 
0962     i2c_writel(i2c_dev, status, I2C_INT_STATUS);
0963 
0964     if (i2c_dev->is_dvc)
0965         dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
0966 
0967     if (i2c_dev->dma_mode) {
0968         if (i2c_dev->msg_read)
0969             dmaengine_terminate_async(i2c_dev->rx_dma_chan);
0970         else
0971             dmaengine_terminate_async(i2c_dev->tx_dma_chan);
0972 
0973         complete(&i2c_dev->dma_complete);
0974     }
0975 
0976     complete(&i2c_dev->msg_complete);
0977 done:
0978     return IRQ_HANDLED;
0979 }
0980 
0981 static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
0982                        size_t len)
0983 {
0984     struct dma_slave_config slv_config = {0};
0985     u32 val, reg, dma_burst, reg_offset;
0986     struct dma_chan *chan;
0987     int err;
0988 
0989     if (i2c_dev->hw->has_mst_fifo)
0990         reg = I2C_MST_FIFO_CONTROL;
0991     else
0992         reg = I2C_FIFO_CONTROL;
0993 
0994     if (i2c_dev->dma_mode) {
0995         if (len & 0xF)
0996             dma_burst = 1;
0997         else if (len & 0x10)
0998             dma_burst = 4;
0999         else
1000             dma_burst = 8;
1001 
1002         if (i2c_dev->msg_read) {
1003             chan = i2c_dev->rx_dma_chan;
1004             reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
1005 
1006             slv_config.src_addr = i2c_dev->base_phys + reg_offset;
1007             slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1008             slv_config.src_maxburst = dma_burst;
1009 
1010             if (i2c_dev->hw->has_mst_fifo)
1011                 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
1012             else
1013                 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
1014         } else {
1015             chan = i2c_dev->tx_dma_chan;
1016             reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
1017 
1018             slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
1019             slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1020             slv_config.dst_maxburst = dma_burst;
1021 
1022             if (i2c_dev->hw->has_mst_fifo)
1023                 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
1024             else
1025                 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
1026         }
1027 
1028         slv_config.device_fc = true;
1029         err = dmaengine_slave_config(chan, &slv_config);
1030         if (err) {
1031             dev_err(i2c_dev->dev, "DMA config failed: %d\n", err);
1032             dev_err(i2c_dev->dev, "falling back to PIO\n");
1033 
1034             tegra_i2c_release_dma(i2c_dev);
1035             i2c_dev->dma_mode = false;
1036         } else {
1037             goto out;
1038         }
1039     }
1040 
1041     if (i2c_dev->hw->has_mst_fifo)
1042         val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
1043               I2C_MST_FIFO_CONTROL_RX_TRIG(1);
1044     else
1045         val = I2C_FIFO_CONTROL_TX_TRIG(8) |
1046               I2C_FIFO_CONTROL_RX_TRIG(1);
1047 out:
1048     i2c_writel(i2c_dev, val, reg);
1049 }
1050 
1051 static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev,
1052                            struct completion *complete,
1053                            unsigned int timeout_ms)
1054 {
1055     ktime_t ktime = ktime_get();
1056     ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);
1057 
1058     do {
1059         u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
1060 
1061         if (status)
1062             tegra_i2c_isr(i2c_dev->irq, i2c_dev);
1063 
1064         if (completion_done(complete)) {
1065             s64 delta = ktime_ms_delta(ktimeout, ktime);
1066 
1067             return msecs_to_jiffies(delta) ?: 1;
1068         }
1069 
1070         ktime = ktime_get();
1071 
1072     } while (ktime_before(ktime, ktimeout));
1073 
1074     return 0;
1075 }
1076 
1077 static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev,
1078                            struct completion *complete,
1079                            unsigned int timeout_ms)
1080 {
1081     unsigned long ret;
1082 
1083     if (i2c_dev->atomic_mode) {
1084         ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms);
1085     } else {
1086         enable_irq(i2c_dev->irq);
1087         ret = wait_for_completion_timeout(complete,
1088                           msecs_to_jiffies(timeout_ms));
1089         disable_irq(i2c_dev->irq);
1090 
1091         /*
1092          * Under some rare circumstances (like running KASAN +
1093          * NFS root) CPU, which handles interrupt, may stuck in
1094          * uninterruptible state for a significant time.  In this
1095          * case we will get timeout if I2C transfer is running on
1096          * a sibling CPU, despite of IRQ being raised.
1097          *
1098          * In order to handle this rare condition, the IRQ status
1099          * needs to be checked after timeout.
1100          */
1101         if (ret == 0)
1102             ret = tegra_i2c_poll_completion(i2c_dev, complete, 0);
1103     }
1104 
1105     return ret;
1106 }
1107 
1108 static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
1109 {
1110     struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1111     u32 val, time_left;
1112     int err;
1113 
1114     reinit_completion(&i2c_dev->msg_complete);
1115 
1116     val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND |
1117           I2C_BC_TERMINATE;
1118     i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
1119 
1120     err = tegra_i2c_wait_for_config_load(i2c_dev);
1121     if (err)
1122         return err;
1123 
1124     val |= I2C_BC_ENABLE;
1125     i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG);
1126     tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1127 
1128     time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50);
1129     tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1130 
1131     if (time_left == 0) {
1132         dev_err(i2c_dev->dev, "failed to clear bus\n");
1133         return -ETIMEDOUT;
1134     }
1135 
1136     val = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1137     if (!(val & I2C_BC_STATUS)) {
1138         dev_err(i2c_dev->dev, "un-recovered arbitration lost\n");
1139         return -EIO;
1140     }
1141 
1142     return -EAGAIN;
1143 }
1144 
1145 static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev,
1146                      struct i2c_msg *msg,
1147                      enum msg_end_type end_state)
1148 {
1149     u32 *dma_buf = i2c_dev->dma_buf;
1150     u32 packet_header;
1151 
1152     packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) |
1153             FIELD_PREP(PACKET_HEADER0_PROTOCOL,
1154                    PACKET_HEADER0_PROTOCOL_I2C) |
1155             FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) |
1156             FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1);
1157 
1158     if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1159         *dma_buf++ = packet_header;
1160     else
1161         i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1162 
1163     packet_header = msg->len - 1;
1164 
1165     if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1166         *dma_buf++ = packet_header;
1167     else
1168         i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1169 
1170     packet_header = I2C_HEADER_IE_ENABLE;
1171 
1172     if (end_state == MSG_END_CONTINUE)
1173         packet_header |= I2C_HEADER_CONTINUE_XFER;
1174     else if (end_state == MSG_END_REPEAT_START)
1175         packet_header |= I2C_HEADER_REPEAT_START;
1176 
1177     if (msg->flags & I2C_M_TEN) {
1178         packet_header |= msg->addr;
1179         packet_header |= I2C_HEADER_10BIT_ADDR;
1180     } else {
1181         packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1182     }
1183 
1184     if (msg->flags & I2C_M_IGNORE_NAK)
1185         packet_header |= I2C_HEADER_CONT_ON_NAK;
1186 
1187     if (msg->flags & I2C_M_RD)
1188         packet_header |= I2C_HEADER_READ;
1189 
1190     if (i2c_dev->dma_mode && !i2c_dev->msg_read)
1191         *dma_buf++ = packet_header;
1192     else
1193         i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
1194 }
1195 
1196 static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev,
1197                    struct i2c_msg *msg)
1198 {
1199     if (i2c_dev->msg_err == I2C_ERR_NONE)
1200         return 0;
1201 
1202     tegra_i2c_init(i2c_dev);
1203 
1204     /* start recovery upon arbitration loss in single master mode */
1205     if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1206         if (!i2c_dev->multimaster_mode)
1207             return i2c_recover_bus(&i2c_dev->adapter);
1208 
1209         return -EAGAIN;
1210     }
1211 
1212     if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1213         if (msg->flags & I2C_M_IGNORE_NAK)
1214             return 0;
1215 
1216         return -EREMOTEIO;
1217     }
1218 
1219     return -EIO;
1220 }
1221 
1222 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
1223                   struct i2c_msg *msg,
1224                   enum msg_end_type end_state)
1225 {
1226     unsigned long time_left, xfer_time = 100;
1227     size_t xfer_size;
1228     u32 int_mask;
1229     int err;
1230 
1231     err = tegra_i2c_flush_fifos(i2c_dev);
1232     if (err)
1233         return err;
1234 
1235     i2c_dev->msg_buf = msg->buf;
1236 
1237     /* The condition true implies smbus block read and len is already read */
1238     if (msg->flags & I2C_M_RECV_LEN && end_state != MSG_END_CONTINUE)
1239         i2c_dev->msg_buf = msg->buf + 1;
1240 
1241     i2c_dev->msg_buf_remaining = msg->len;
1242     i2c_dev->msg_err = I2C_ERR_NONE;
1243     i2c_dev->msg_read = !!(msg->flags & I2C_M_RD);
1244     reinit_completion(&i2c_dev->msg_complete);
1245 
1246     if (i2c_dev->msg_read)
1247         xfer_size = msg->len;
1248     else
1249         xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1250 
1251     xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1252 
1253     i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN &&
1254                 i2c_dev->dma_buf && !i2c_dev->atomic_mode;
1255 
1256     tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1257 
1258     /*
1259      * Transfer time in mSec = Total bits / transfer rate
1260      * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1261      */
1262     xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1263                        i2c_dev->timings.bus_freq_hz);
1264 
1265     int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1266     tegra_i2c_unmask_irq(i2c_dev, int_mask);
1267 
1268     if (i2c_dev->dma_mode) {
1269         if (i2c_dev->msg_read) {
1270             dma_sync_single_for_device(i2c_dev->dev,
1271                            i2c_dev->dma_phys,
1272                            xfer_size, DMA_FROM_DEVICE);
1273 
1274             err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1275             if (err)
1276                 return err;
1277         } else {
1278             dma_sync_single_for_cpu(i2c_dev->dev,
1279                         i2c_dev->dma_phys,
1280                         xfer_size, DMA_TO_DEVICE);
1281         }
1282     }
1283 
1284     tegra_i2c_push_packet_header(i2c_dev, msg, end_state);
1285 
1286     if (!i2c_dev->msg_read) {
1287         if (i2c_dev->dma_mode) {
1288             memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE,
1289                    msg->buf, msg->len);
1290 
1291             dma_sync_single_for_device(i2c_dev->dev,
1292                            i2c_dev->dma_phys,
1293                            xfer_size, DMA_TO_DEVICE);
1294 
1295             err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1296             if (err)
1297                 return err;
1298         } else {
1299             tegra_i2c_fill_tx_fifo(i2c_dev);
1300         }
1301     }
1302 
1303     if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1304         int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
1305 
1306     if (!i2c_dev->dma_mode) {
1307         if (msg->flags & I2C_M_RD)
1308             int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1309         else if (i2c_dev->msg_buf_remaining)
1310             int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1311     }
1312 
1313     tegra_i2c_unmask_irq(i2c_dev, int_mask);
1314     dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n",
1315         i2c_readl(i2c_dev, I2C_INT_MASK));
1316 
1317     if (i2c_dev->dma_mode) {
1318         time_left = tegra_i2c_wait_completion(i2c_dev,
1319                               &i2c_dev->dma_complete,
1320                               xfer_time);
1321 
1322         /*
1323          * Synchronize DMA first, since dmaengine_terminate_sync()
1324          * performs synchronization after the transfer's termination
1325          * and we want to get a completion if transfer succeeded.
1326          */
1327         dmaengine_synchronize(i2c_dev->msg_read ?
1328                       i2c_dev->rx_dma_chan :
1329                       i2c_dev->tx_dma_chan);
1330 
1331         dmaengine_terminate_sync(i2c_dev->msg_read ?
1332                      i2c_dev->rx_dma_chan :
1333                      i2c_dev->tx_dma_chan);
1334 
1335         if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
1336             dev_err(i2c_dev->dev, "DMA transfer timed out\n");
1337             tegra_i2c_init(i2c_dev);
1338             return -ETIMEDOUT;
1339         }
1340 
1341         if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1342             dma_sync_single_for_cpu(i2c_dev->dev,
1343                         i2c_dev->dma_phys,
1344                         xfer_size, DMA_FROM_DEVICE);
1345 
1346             memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, msg->len);
1347         }
1348     }
1349 
1350     time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete,
1351                           xfer_time);
1352 
1353     tegra_i2c_mask_irq(i2c_dev, int_mask);
1354 
1355     if (time_left == 0) {
1356         dev_err(i2c_dev->dev, "I2C transfer timed out\n");
1357         tegra_i2c_init(i2c_dev);
1358         return -ETIMEDOUT;
1359     }
1360 
1361     dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1362         time_left, completion_done(&i2c_dev->msg_complete),
1363         i2c_dev->msg_err);
1364 
1365     i2c_dev->dma_mode = false;
1366 
1367     err = tegra_i2c_error_recover(i2c_dev, msg);
1368     if (err)
1369         return err;
1370 
1371     return 0;
1372 }
1373 
1374 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1375               int num)
1376 {
1377     struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1378     int i, ret;
1379 
1380     ret = pm_runtime_get_sync(i2c_dev->dev);
1381     if (ret < 0) {
1382         dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
1383         pm_runtime_put_noidle(i2c_dev->dev);
1384         return ret;
1385     }
1386 
1387     for (i = 0; i < num; i++) {
1388         enum msg_end_type end_type = MSG_END_STOP;
1389 
1390         if (i < (num - 1)) {
1391             /* check whether follow up message is coming */
1392             if (msgs[i + 1].flags & I2C_M_NOSTART)
1393                 end_type = MSG_END_CONTINUE;
1394             else
1395                 end_type = MSG_END_REPEAT_START;
1396         }
1397         /* If M_RECV_LEN use ContinueXfer to read the first byte */
1398         if (msgs[i].flags & I2C_M_RECV_LEN) {
1399             ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], MSG_END_CONTINUE);
1400             if (ret)
1401                 break;
1402             /* Set the read byte as msg len */
1403             msgs[i].len = msgs[i].buf[0];
1404             dev_dbg(i2c_dev->dev, "reading %d bytes\n", msgs[i].len);
1405         }
1406         ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
1407         if (ret)
1408             break;
1409     }
1410 
1411     pm_runtime_put(i2c_dev->dev);
1412 
1413     return ret ?: i;
1414 }
1415 
1416 static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
1417                  struct i2c_msg msgs[], int num)
1418 {
1419     struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1420     int ret;
1421 
1422     i2c_dev->atomic_mode = true;
1423     ret = tegra_i2c_xfer(adap, msgs, num);
1424     i2c_dev->atomic_mode = false;
1425 
1426     return ret;
1427 }
1428 
1429 static u32 tegra_i2c_func(struct i2c_adapter *adap)
1430 {
1431     struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1432     u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1433           I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
1434 
1435     if (i2c_dev->hw->has_continue_xfer_support)
1436         ret |= I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
1437 
1438     return ret;
1439 }
1440 
1441 static const struct i2c_algorithm tegra_i2c_algo = {
1442     .master_xfer        = tegra_i2c_xfer,
1443     .master_xfer_atomic = tegra_i2c_xfer_atomic,
1444     .functionality      = tegra_i2c_func,
1445 };
1446 
1447 /* payload size is only 12 bit */
1448 static const struct i2c_adapter_quirks tegra_i2c_quirks = {
1449     .flags = I2C_AQ_NO_ZERO_LEN,
1450     .max_read_len = SZ_4K,
1451     .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
1452 };
1453 
1454 static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1455     .flags = I2C_AQ_NO_ZERO_LEN,
1456     .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
1457 };
1458 
1459 static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1460     .recover_bus = tegra_i2c_issue_bus_clear,
1461 };
1462 
1463 static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1464     .has_continue_xfer_support = false,
1465     .has_per_pkt_xfer_complete_irq = false,
1466     .clk_divisor_hs_mode = 3,
1467     .clk_divisor_std_mode = 0,
1468     .clk_divisor_fast_mode = 0,
1469     .clk_divisor_fast_plus_mode = 0,
1470     .has_config_load_reg = false,
1471     .has_multi_master_mode = false,
1472     .has_slcg_override_reg = false,
1473     .has_mst_fifo = false,
1474     .quirks = &tegra_i2c_quirks,
1475     .supports_bus_clear = false,
1476     .has_apb_dma = true,
1477     .tlow_std_mode = 0x4,
1478     .thigh_std_mode = 0x2,
1479     .tlow_fast_fastplus_mode = 0x4,
1480     .thigh_fast_fastplus_mode = 0x2,
1481     .setup_hold_time_std_mode = 0x0,
1482     .setup_hold_time_fast_fast_plus_mode = 0x0,
1483     .setup_hold_time_hs_mode = 0x0,
1484     .has_interface_timing_reg = false,
1485 };
1486 
1487 static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1488     .has_continue_xfer_support = true,
1489     .has_per_pkt_xfer_complete_irq = false,
1490     .clk_divisor_hs_mode = 3,
1491     .clk_divisor_std_mode = 0,
1492     .clk_divisor_fast_mode = 0,
1493     .clk_divisor_fast_plus_mode = 0,
1494     .has_config_load_reg = false,
1495     .has_multi_master_mode = false,
1496     .has_slcg_override_reg = false,
1497     .has_mst_fifo = false,
1498     .quirks = &tegra_i2c_quirks,
1499     .supports_bus_clear = false,
1500     .has_apb_dma = true,
1501     .tlow_std_mode = 0x4,
1502     .thigh_std_mode = 0x2,
1503     .tlow_fast_fastplus_mode = 0x4,
1504     .thigh_fast_fastplus_mode = 0x2,
1505     .setup_hold_time_std_mode = 0x0,
1506     .setup_hold_time_fast_fast_plus_mode = 0x0,
1507     .setup_hold_time_hs_mode = 0x0,
1508     .has_interface_timing_reg = false,
1509 };
1510 
1511 static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1512     .has_continue_xfer_support = true,
1513     .has_per_pkt_xfer_complete_irq = true,
1514     .clk_divisor_hs_mode = 1,
1515     .clk_divisor_std_mode = 0x19,
1516     .clk_divisor_fast_mode = 0x19,
1517     .clk_divisor_fast_plus_mode = 0x10,
1518     .has_config_load_reg = false,
1519     .has_multi_master_mode = false,
1520     .has_slcg_override_reg = false,
1521     .has_mst_fifo = false,
1522     .quirks = &tegra_i2c_quirks,
1523     .supports_bus_clear = true,
1524     .has_apb_dma = true,
1525     .tlow_std_mode = 0x4,
1526     .thigh_std_mode = 0x2,
1527     .tlow_fast_fastplus_mode = 0x4,
1528     .thigh_fast_fastplus_mode = 0x2,
1529     .setup_hold_time_std_mode = 0x0,
1530     .setup_hold_time_fast_fast_plus_mode = 0x0,
1531     .setup_hold_time_hs_mode = 0x0,
1532     .has_interface_timing_reg = false,
1533 };
1534 
1535 static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1536     .has_continue_xfer_support = true,
1537     .has_per_pkt_xfer_complete_irq = true,
1538     .clk_divisor_hs_mode = 1,
1539     .clk_divisor_std_mode = 0x19,
1540     .clk_divisor_fast_mode = 0x19,
1541     .clk_divisor_fast_plus_mode = 0x10,
1542     .has_config_load_reg = true,
1543     .has_multi_master_mode = false,
1544     .has_slcg_override_reg = true,
1545     .has_mst_fifo = false,
1546     .quirks = &tegra_i2c_quirks,
1547     .supports_bus_clear = true,
1548     .has_apb_dma = true,
1549     .tlow_std_mode = 0x4,
1550     .thigh_std_mode = 0x2,
1551     .tlow_fast_fastplus_mode = 0x4,
1552     .thigh_fast_fastplus_mode = 0x2,
1553     .setup_hold_time_std_mode = 0x0,
1554     .setup_hold_time_fast_fast_plus_mode = 0x0,
1555     .setup_hold_time_hs_mode = 0x0,
1556     .has_interface_timing_reg = true,
1557 };
1558 
1559 static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1560     .has_continue_xfer_support = true,
1561     .has_per_pkt_xfer_complete_irq = true,
1562     .clk_divisor_hs_mode = 1,
1563     .clk_divisor_std_mode = 0x19,
1564     .clk_divisor_fast_mode = 0x19,
1565     .clk_divisor_fast_plus_mode = 0x10,
1566     .has_config_load_reg = true,
1567     .has_multi_master_mode = false,
1568     .has_slcg_override_reg = true,
1569     .has_mst_fifo = false,
1570     .quirks = &tegra_i2c_quirks,
1571     .supports_bus_clear = true,
1572     .has_apb_dma = true,
1573     .tlow_std_mode = 0x4,
1574     .thigh_std_mode = 0x2,
1575     .tlow_fast_fastplus_mode = 0x4,
1576     .thigh_fast_fastplus_mode = 0x2,
1577     .setup_hold_time_std_mode = 0,
1578     .setup_hold_time_fast_fast_plus_mode = 0,
1579     .setup_hold_time_hs_mode = 0,
1580     .has_interface_timing_reg = true,
1581 };
1582 
1583 static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1584     .has_continue_xfer_support = true,
1585     .has_per_pkt_xfer_complete_irq = true,
1586     .clk_divisor_hs_mode = 1,
1587     .clk_divisor_std_mode = 0x16,
1588     .clk_divisor_fast_mode = 0x19,
1589     .clk_divisor_fast_plus_mode = 0x10,
1590     .has_config_load_reg = true,
1591     .has_multi_master_mode = false,
1592     .has_slcg_override_reg = true,
1593     .has_mst_fifo = false,
1594     .quirks = &tegra_i2c_quirks,
1595     .supports_bus_clear = true,
1596     .has_apb_dma = false,
1597     .tlow_std_mode = 0x4,
1598     .thigh_std_mode = 0x3,
1599     .tlow_fast_fastplus_mode = 0x4,
1600     .thigh_fast_fastplus_mode = 0x2,
1601     .setup_hold_time_std_mode = 0,
1602     .setup_hold_time_fast_fast_plus_mode = 0,
1603     .setup_hold_time_hs_mode = 0,
1604     .has_interface_timing_reg = true,
1605 };
1606 
1607 static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1608     .has_continue_xfer_support = true,
1609     .has_per_pkt_xfer_complete_irq = true,
1610     .clk_divisor_hs_mode = 1,
1611     .clk_divisor_std_mode = 0x4f,
1612     .clk_divisor_fast_mode = 0x3c,
1613     .clk_divisor_fast_plus_mode = 0x16,
1614     .has_config_load_reg = true,
1615     .has_multi_master_mode = true,
1616     .has_slcg_override_reg = true,
1617     .has_mst_fifo = true,
1618     .quirks = &tegra194_i2c_quirks,
1619     .supports_bus_clear = true,
1620     .has_apb_dma = false,
1621     .tlow_std_mode = 0x8,
1622     .thigh_std_mode = 0x7,
1623     .tlow_fast_fastplus_mode = 0x2,
1624     .thigh_fast_fastplus_mode = 0x2,
1625     .setup_hold_time_std_mode = 0x08080808,
1626     .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1627     .setup_hold_time_hs_mode = 0x090909,
1628     .has_interface_timing_reg = true,
1629 };
1630 
1631 static const struct of_device_id tegra_i2c_of_match[] = {
1632     { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
1633     { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
1634     { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_i2c_hw, },
1635     { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
1636     { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
1637     { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
1638     { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1639     { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1640     { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1641     {},
1642 };
1643 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
1644 
1645 static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1646 {
1647     struct device_node *np = i2c_dev->dev->of_node;
1648     bool multi_mode;
1649 
1650     i2c_parse_fw_timings(i2c_dev->dev, &i2c_dev->timings, true);
1651 
1652     multi_mode = device_property_read_bool(i2c_dev->dev, "multi-master");
1653     i2c_dev->multimaster_mode = multi_mode;
1654 
1655     if (of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc"))
1656         i2c_dev->is_dvc = true;
1657 
1658     if (of_device_is_compatible(np, "nvidia,tegra210-i2c-vi"))
1659         i2c_dev->is_vi = true;
1660 }
1661 
1662 static int tegra_i2c_init_reset(struct tegra_i2c_dev *i2c_dev)
1663 {
1664     if (ACPI_HANDLE(i2c_dev->dev))
1665         return 0;
1666 
1667     i2c_dev->rst = devm_reset_control_get_exclusive(i2c_dev->dev, "i2c");
1668     if (IS_ERR(i2c_dev->rst))
1669         return dev_err_probe(i2c_dev->dev, PTR_ERR(i2c_dev->rst),
1670                       "failed to get reset control\n");
1671 
1672     return 0;
1673 }
1674 
1675 static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev)
1676 {
1677     int err;
1678 
1679     if (ACPI_HANDLE(i2c_dev->dev))
1680         return 0;
1681 
1682     i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk";
1683 
1684     if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw)
1685         i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk";
1686 
1687     if (i2c_dev->is_vi)
1688         i2c_dev->clocks[i2c_dev->nclocks++].id = "slow";
1689 
1690     err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks,
1691                 i2c_dev->clocks);
1692     if (err)
1693         return err;
1694 
1695     err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks);
1696     if (err)
1697         return err;
1698 
1699     i2c_dev->div_clk = i2c_dev->clocks[0].clk;
1700 
1701     if (!i2c_dev->multimaster_mode)
1702         return 0;
1703 
1704     err = clk_enable(i2c_dev->div_clk);
1705     if (err) {
1706         dev_err(i2c_dev->dev, "failed to enable div-clk: %d\n", err);
1707         goto unprepare_clocks;
1708     }
1709 
1710     return 0;
1711 
1712 unprepare_clocks:
1713     clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
1714 
1715     return err;
1716 }
1717 
1718 static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev)
1719 {
1720     if (i2c_dev->multimaster_mode)
1721         clk_disable(i2c_dev->div_clk);
1722 
1723     clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks);
1724 }
1725 
1726 static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev)
1727 {
1728     int ret;
1729 
1730     ret = pm_runtime_get_sync(i2c_dev->dev);
1731     if (ret < 0)
1732         dev_err(i2c_dev->dev, "runtime resume failed: %d\n", ret);
1733     else
1734         ret = tegra_i2c_init(i2c_dev);
1735 
1736     pm_runtime_put_sync(i2c_dev->dev);
1737 
1738     return ret;
1739 }
1740 
1741 static int tegra_i2c_probe(struct platform_device *pdev)
1742 {
1743     struct tegra_i2c_dev *i2c_dev;
1744     struct resource *res;
1745     int err;
1746 
1747     i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1748     if (!i2c_dev)
1749         return -ENOMEM;
1750 
1751     platform_set_drvdata(pdev, i2c_dev);
1752 
1753     init_completion(&i2c_dev->msg_complete);
1754     init_completion(&i2c_dev->dma_complete);
1755 
1756     i2c_dev->hw = device_get_match_data(&pdev->dev);
1757     i2c_dev->cont_id = pdev->id;
1758     i2c_dev->dev = &pdev->dev;
1759 
1760     i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1761     if (IS_ERR(i2c_dev->base))
1762         return PTR_ERR(i2c_dev->base);
1763 
1764     i2c_dev->base_phys = res->start;
1765 
1766     err = platform_get_irq(pdev, 0);
1767     if (err < 0)
1768         return err;
1769 
1770     i2c_dev->irq = err;
1771 
1772     /* interrupt will be enabled during of transfer time */
1773     irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
1774 
1775     err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq,
1776                     NULL, tegra_i2c_isr,
1777                     IRQF_NO_SUSPEND | IRQF_ONESHOT,
1778                     dev_name(i2c_dev->dev), i2c_dev);
1779     if (err)
1780         return err;
1781 
1782     tegra_i2c_parse_dt(i2c_dev);
1783 
1784     err = tegra_i2c_init_reset(i2c_dev);
1785     if (err)
1786         return err;
1787 
1788     err = tegra_i2c_init_clocks(i2c_dev);
1789     if (err)
1790         return err;
1791 
1792     err = tegra_i2c_init_dma(i2c_dev);
1793     if (err)
1794         goto release_clocks;
1795 
1796     /*
1797      * VI I2C is in VE power domain which is not always ON and not
1798      * IRQ-safe.  Thus, IRQ-safe device shouldn't be attached to a
1799      * non IRQ-safe domain because this prevents powering off the power
1800      * domain.
1801      *
1802      * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't
1803      * be used for atomic transfers.
1804      */
1805     if (!i2c_dev->is_vi)
1806         pm_runtime_irq_safe(i2c_dev->dev);
1807 
1808     pm_runtime_enable(i2c_dev->dev);
1809 
1810     err = tegra_i2c_init_hardware(i2c_dev);
1811     if (err)
1812         goto release_rpm;
1813 
1814     i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1815     i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node;
1816     i2c_dev->adapter.dev.parent = i2c_dev->dev;
1817     i2c_dev->adapter.retries = 1;
1818     i2c_dev->adapter.timeout = 6 * HZ;
1819     i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
1820     i2c_dev->adapter.owner = THIS_MODULE;
1821     i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
1822     i2c_dev->adapter.algo = &tegra_i2c_algo;
1823     i2c_dev->adapter.nr = pdev->id;
1824 
1825     if (i2c_dev->hw->supports_bus_clear)
1826         i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1827 
1828     strscpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev),
1829         sizeof(i2c_dev->adapter.name));
1830 
1831     err = i2c_add_numbered_adapter(&i2c_dev->adapter);
1832     if (err)
1833         goto release_rpm;
1834 
1835     return 0;
1836 
1837 release_rpm:
1838     pm_runtime_disable(i2c_dev->dev);
1839 
1840     tegra_i2c_release_dma(i2c_dev);
1841 release_clocks:
1842     tegra_i2c_release_clocks(i2c_dev);
1843 
1844     return err;
1845 }
1846 
1847 static int tegra_i2c_remove(struct platform_device *pdev)
1848 {
1849     struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1850 
1851     i2c_del_adapter(&i2c_dev->adapter);
1852     pm_runtime_force_suspend(i2c_dev->dev);
1853 
1854     tegra_i2c_release_dma(i2c_dev);
1855     tegra_i2c_release_clocks(i2c_dev);
1856 
1857     return 0;
1858 }
1859 
1860 static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
1861 {
1862     struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1863     int err;
1864 
1865     err = pinctrl_pm_select_default_state(dev);
1866     if (err)
1867         return err;
1868 
1869     err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks);
1870     if (err)
1871         return err;
1872 
1873     /*
1874      * VI I2C device is attached to VE power domain which goes through
1875      * power ON/OFF during runtime PM resume/suspend, meaning that
1876      * controller needs to be re-initialized after power ON.
1877      */
1878     if (i2c_dev->is_vi) {
1879         err = tegra_i2c_init(i2c_dev);
1880         if (err)
1881             goto disable_clocks;
1882     }
1883 
1884     return 0;
1885 
1886 disable_clocks:
1887     clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
1888 
1889     return err;
1890 }
1891 
1892 static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
1893 {
1894     struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1895 
1896     clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks);
1897 
1898     return pinctrl_pm_select_idle_state(dev);
1899 }
1900 
1901 static int __maybe_unused tegra_i2c_suspend(struct device *dev)
1902 {
1903     struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1904     int err;
1905 
1906     i2c_mark_adapter_suspended(&i2c_dev->adapter);
1907 
1908     if (!pm_runtime_status_suspended(dev)) {
1909         err = tegra_i2c_runtime_suspend(dev);
1910         if (err)
1911             return err;
1912     }
1913 
1914     return 0;
1915 }
1916 
1917 static int __maybe_unused tegra_i2c_resume(struct device *dev)
1918 {
1919     struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1920     int err;
1921 
1922     /*
1923      * We need to ensure that clocks are enabled so that registers can be
1924      * restored in tegra_i2c_init().
1925      */
1926     err = tegra_i2c_runtime_resume(dev);
1927     if (err)
1928         return err;
1929 
1930     err = tegra_i2c_init(i2c_dev);
1931     if (err)
1932         return err;
1933 
1934     /*
1935      * In case we are runtime suspended, disable clocks again so that we
1936      * don't unbalance the clock reference counts during the next runtime
1937      * resume transition.
1938      */
1939     if (pm_runtime_status_suspended(dev)) {
1940         err = tegra_i2c_runtime_suspend(dev);
1941         if (err)
1942             return err;
1943     }
1944 
1945     i2c_mark_adapter_resumed(&i2c_dev->adapter);
1946 
1947     return 0;
1948 }
1949 
1950 static const struct dev_pm_ops tegra_i2c_pm = {
1951     SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1952     SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1953                NULL)
1954 };
1955 
1956 static const struct acpi_device_id tegra_i2c_acpi_match[] = {
1957     {.id = "NVDA0101", .driver_data = (kernel_ulong_t)&tegra210_i2c_hw},
1958     {.id = "NVDA0201", .driver_data = (kernel_ulong_t)&tegra186_i2c_hw},
1959     {.id = "NVDA0301", .driver_data = (kernel_ulong_t)&tegra194_i2c_hw},
1960     { }
1961 };
1962 MODULE_DEVICE_TABLE(acpi, tegra_i2c_acpi_match);
1963 
1964 static struct platform_driver tegra_i2c_driver = {
1965     .probe = tegra_i2c_probe,
1966     .remove = tegra_i2c_remove,
1967     .driver = {
1968         .name = "tegra-i2c",
1969         .of_match_table = tegra_i2c_of_match,
1970         .acpi_match_table = tegra_i2c_acpi_match,
1971         .pm = &tegra_i2c_pm,
1972     },
1973 };
1974 module_platform_driver(tegra_i2c_driver);
1975 
1976 MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver");
1977 MODULE_AUTHOR("Colin Cross");
1978 MODULE_LICENSE("GPL v2");