1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/i2c/busses/i2c-tegra.c 4 * 5 * Copyright (C) 2010 Google, Inc. 6 * Author: Colin Cross <ccross@android.com> 7 */ 8 9 #include <linux/acpi.h> 10 #include <linux/bitfield.h> 11 #include <linux/clk.h> 12 #include <linux/delay.h> 13 #include <linux/dmaengine.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/err.h> 16 #include <linux/i2c.h> 17 #include <linux/init.h> 18 #include <linux/interrupt.h> 19 #include <linux/io.h> 20 #include <linux/iopoll.h> 21 #include <linux/irq.h> 22 #include <linux/kernel.h> 23 #include <linux/ktime.h> 24 #include <linux/module.h> 25 #include <linux/of.h> 26 #include <linux/pinctrl/consumer.h> 27 #include <linux/platform_device.h> 28 #include <linux/pm_runtime.h> 29 #include <linux/reset.h> 30 31 #define BYTES_PER_FIFO_WORD 4 32 33 #define I2C_CNFG 0x000 34 #define I2C_CNFG_DEBOUNCE_CNT GENMASK(14, 12) 35 #define I2C_CNFG_PACKET_MODE_EN BIT(10) 36 #define I2C_CNFG_NEW_MASTER_FSM BIT(11) 37 #define I2C_CNFG_MULTI_MASTER_MODE BIT(17) 38 #define I2C_STATUS 0x01c 39 #define I2C_SL_CNFG 0x020 40 #define I2C_SL_CNFG_NACK BIT(1) 41 #define I2C_SL_CNFG_NEWSL BIT(2) 42 #define I2C_SL_ADDR1 0x02c 43 #define I2C_SL_ADDR2 0x030 44 #define I2C_TLOW_SEXT 0x034 45 #define I2C_TX_FIFO 0x050 46 #define I2C_RX_FIFO 0x054 47 #define I2C_PACKET_TRANSFER_STATUS 0x058 48 #define I2C_FIFO_CONTROL 0x05c 49 #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1) 50 #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0) 51 #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5) 52 #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2) 53 #define I2C_FIFO_STATUS 0x060 54 #define I2C_FIFO_STATUS_TX GENMASK(7, 4) 55 #define I2C_FIFO_STATUS_RX GENMASK(3, 0) 56 #define I2C_INT_MASK 0x064 57 #define I2C_INT_STATUS 0x068 58 #define I2C_INT_BUS_CLR_DONE BIT(11) 59 #define I2C_INT_PACKET_XFER_COMPLETE BIT(7) 60 #define I2C_INT_NO_ACK BIT(3) 61 #define I2C_INT_ARBITRATION_LOST BIT(2) 62 #define I2C_INT_TX_FIFO_DATA_REQ BIT(1) 63 #define I2C_INT_RX_FIFO_DATA_REQ BIT(0) 64 #define I2C_CLK_DIVISOR 0x06c 65 #define I2C_CLK_DIVISOR_STD_FAST_MODE GENMASK(31, 16) 66 #define I2C_CLK_DIVISOR_HSMODE GENMASK(15, 0) 67 68 #define DVC_CTRL_REG1 0x000 69 #define DVC_CTRL_REG1_INTR_EN BIT(10) 70 #define DVC_CTRL_REG3 0x008 71 #define DVC_CTRL_REG3_SW_PROG BIT(26) 72 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30) 73 #define DVC_STATUS 0x00c 74 #define DVC_STATUS_I2C_DONE_INTR BIT(30) 75 76 #define I2C_ERR_NONE 0x00 77 #define I2C_ERR_NO_ACK BIT(0) 78 #define I2C_ERR_ARBITRATION_LOST BIT(1) 79 #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2) 80 #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3) 81 82 #define PACKET_HEADER0_HEADER_SIZE GENMASK(29, 28) 83 #define PACKET_HEADER0_PACKET_ID GENMASK(23, 16) 84 #define PACKET_HEADER0_CONT_ID GENMASK(15, 12) 85 #define PACKET_HEADER0_PROTOCOL GENMASK(7, 4) 86 #define PACKET_HEADER0_PROTOCOL_I2C 1 87 88 #define I2C_HEADER_CONT_ON_NAK BIT(21) 89 #define I2C_HEADER_READ BIT(19) 90 #define I2C_HEADER_10BIT_ADDR BIT(18) 91 #define I2C_HEADER_IE_ENABLE BIT(17) 92 #define I2C_HEADER_REPEAT_START BIT(16) 93 #define I2C_HEADER_CONTINUE_XFER BIT(15) 94 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1 95 96 #define I2C_BUS_CLEAR_CNFG 0x084 97 #define I2C_BC_SCLK_THRESHOLD GENMASK(23, 16) 98 #define I2C_BC_STOP_COND BIT(2) 99 #define I2C_BC_TERMINATE BIT(1) 100 #define I2C_BC_ENABLE BIT(0) 101 #define I2C_BUS_CLEAR_STATUS 0x088 102 #define I2C_BC_STATUS BIT(0) 103 104 #define I2C_CONFIG_LOAD 0x08c 105 #define I2C_MSTR_CONFIG_LOAD BIT(0) 106 107 #define I2C_CLKEN_OVERRIDE 0x090 108 #define I2C_MST_CORE_CLKEN_OVR BIT(0) 109 110 #define I2C_INTERFACE_TIMING_0 0x094 111 #define I2C_INTERFACE_TIMING_THIGH GENMASK(13, 8) 112 #define I2C_INTERFACE_TIMING_TLOW GENMASK(5, 0) 113 #define I2C_INTERFACE_TIMING_1 0x098 114 #define I2C_INTERFACE_TIMING_TBUF GENMASK(29, 24) 115 #define I2C_INTERFACE_TIMING_TSU_STO GENMASK(21, 16) 116 #define I2C_INTERFACE_TIMING_THD_STA GENMASK(13, 8) 117 #define I2C_INTERFACE_TIMING_TSU_STA GENMASK(5, 0) 118 119 #define I2C_HS_INTERFACE_TIMING_0 0x09c 120 #define I2C_HS_INTERFACE_TIMING_THIGH GENMASK(13, 8) 121 #define I2C_HS_INTERFACE_TIMING_TLOW GENMASK(5, 0) 122 #define I2C_HS_INTERFACE_TIMING_1 0x0a0 123 #define I2C_HS_INTERFACE_TIMING_TSU_STO GENMASK(21, 16) 124 #define I2C_HS_INTERFACE_TIMING_THD_STA GENMASK(13, 8) 125 #define I2C_HS_INTERFACE_TIMING_TSU_STA GENMASK(5, 0) 126 127 #define I2C_MST_FIFO_CONTROL 0x0b4 128 #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0) 129 #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1) 130 #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4) 131 #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16) 132 133 #define I2C_MST_FIFO_STATUS 0x0b8 134 #define I2C_MST_FIFO_STATUS_TX GENMASK(23, 16) 135 #define I2C_MST_FIFO_STATUS_RX GENMASK(7, 0) 136 137 /* configuration load timeout in microseconds */ 138 #define I2C_CONFIG_LOAD_TIMEOUT 1000000 139 140 /* packet header size in bytes */ 141 #define I2C_PACKET_HEADER_SIZE 12 142 143 /* 144 * I2C Controller will use PIO mode for transfers up to 32 bytes in order to 145 * avoid DMA overhead, otherwise external APB DMA controller will be used. 146 * Note that the actual MAX PIO length is 20 bytes because 32 bytes include 147 * I2C_PACKET_HEADER_SIZE. 148 */ 149 #define I2C_PIO_MODE_PREFERRED_LEN 32 150 151 /* 152 * msg_end_type: The bus control which needs to be sent at end of transfer. 153 * @MSG_END_STOP: Send stop pulse. 154 * @MSG_END_REPEAT_START: Send repeat-start. 155 * @MSG_END_CONTINUE: Don't send stop or repeat-start. 156 */ 157 enum msg_end_type { 158 MSG_END_STOP, 159 MSG_END_REPEAT_START, 160 MSG_END_CONTINUE, 161 }; 162 163 /** 164 * struct tegra_i2c_hw_feature : per hardware generation features 165 * @has_continue_xfer_support: continue-transfer supported 166 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer 167 * completion interrupt on per packet basis. 168 * @has_config_load_reg: Has the config load register to load the new 169 * configuration. 170 * @clk_divisor_hs_mode: Clock divisor in HS mode. 171 * @clk_divisor_std_mode: Clock divisor in standard mode. It is 172 * applicable if there is no fast clock source i.e. single clock 173 * source. 174 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is 175 * applicable if there is no fast clock source i.e. single clock 176 * source. 177 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is 178 * applicable if there is no fast clock source (i.e. single 179 * clock source). 180 * @has_multi_master_mode: The I2C controller supports running in single-master 181 * or multi-master mode. 182 * @has_slcg_override_reg: The I2C controller supports a register that 183 * overrides the second level clock gating. 184 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that 185 * provides additional features and allows for longer messages to 186 * be transferred in one go. 187 * @quirks: I2C adapter quirks for limiting write/read transfer size and not 188 * allowing 0 length transfers. 189 * @supports_bus_clear: Bus Clear support to recover from bus hang during 190 * SDA stuck low from device for some unknown reasons. 191 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip. 192 * @tlow_std_mode: Low period of the clock in standard mode. 193 * @thigh_std_mode: High period of the clock in standard mode. 194 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes. 195 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes. 196 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions 197 * in standard mode. 198 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop 199 * conditions in fast/fast-plus modes. 200 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions 201 * in HS mode. 202 * @has_interface_timing_reg: Has interface timing register to program the tuned 203 * timing settings. 204 */ 205 struct tegra_i2c_hw_feature { 206 bool has_continue_xfer_support; 207 bool has_per_pkt_xfer_complete_irq; 208 bool has_config_load_reg; 209 u32 clk_divisor_hs_mode; 210 u32 clk_divisor_std_mode; 211 u32 clk_divisor_fast_mode; 212 u32 clk_divisor_fast_plus_mode; 213 bool has_multi_master_mode; 214 bool has_slcg_override_reg; 215 bool has_mst_fifo; 216 const struct i2c_adapter_quirks *quirks; 217 bool supports_bus_clear; 218 bool has_apb_dma; 219 u32 tlow_std_mode; 220 u32 thigh_std_mode; 221 u32 tlow_fast_fastplus_mode; 222 u32 thigh_fast_fastplus_mode; 223 u32 setup_hold_time_std_mode; 224 u32 setup_hold_time_fast_fast_plus_mode; 225 u32 setup_hold_time_hs_mode; 226 bool has_interface_timing_reg; 227 }; 228 229 /** 230 * struct tegra_i2c_dev - per device I2C context 231 * @dev: device reference for power management 232 * @hw: Tegra I2C HW feature 233 * @adapter: core I2C layer adapter information 234 * @div_clk: clock reference for div clock of I2C controller 235 * @clocks: array of I2C controller clocks 236 * @nclocks: number of clocks in the array 237 * @rst: reset control for the I2C controller 238 * @base: ioremapped registers cookie 239 * @base_phys: physical base address of the I2C controller 240 * @cont_id: I2C controller ID, used for packet header 241 * @irq: IRQ number of transfer complete interrupt 242 * @is_dvc: identifies the DVC I2C controller, has a different register layout 243 * @is_vi: identifies the VI I2C controller, has a different register layout 244 * @msg_complete: transfer completion notifier 245 * @msg_buf_remaining: size of unsent data in the message buffer 246 * @msg_len: length of message in current transfer 247 * @msg_err: error code for completed message 248 * @msg_buf: pointer to current message data 249 * @msg_read: indicates that the transfer is a read access 250 * @timings: i2c timings information like bus frequency 251 * @multimaster_mode: indicates that I2C controller is in multi-master mode 252 * @dma_chan: DMA channel 253 * @dma_phys: handle to DMA resources 254 * @dma_buf: pointer to allocated DMA buffer 255 * @dma_buf_size: DMA buffer size 256 * @dma_mode: indicates active DMA transfer 257 * @dma_complete: DMA completion notifier 258 * @atomic_mode: indicates active atomic transfer 259 */ 260 struct tegra_i2c_dev { 261 struct device *dev; 262 struct i2c_adapter adapter; 263 264 const struct tegra_i2c_hw_feature *hw; 265 struct reset_control *rst; 266 unsigned int cont_id; 267 unsigned int irq; 268 269 phys_addr_t base_phys; 270 void __iomem *base; 271 272 struct clk_bulk_data clocks[2]; 273 unsigned int nclocks; 274 275 struct clk *div_clk; 276 struct i2c_timings timings; 277 278 struct completion msg_complete; 279 size_t msg_buf_remaining; 280 unsigned int msg_len; 281 int msg_err; 282 u8 *msg_buf; 283 284 struct completion dma_complete; 285 struct dma_chan *dma_chan; 286 unsigned int dma_buf_size; 287 struct device *dma_dev; 288 dma_addr_t dma_phys; 289 void *dma_buf; 290 291 bool multimaster_mode; 292 bool atomic_mode; 293 bool dma_mode; 294 bool msg_read; 295 bool is_dvc; 296 bool is_vi; 297 }; 298 299 #define IS_DVC(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) && (dev)->is_dvc) 300 #define IS_VI(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) && (dev)->is_vi) 301 302 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, 303 unsigned int reg) 304 { 305 writel_relaxed(val, i2c_dev->base + reg); 306 } 307 308 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg) 309 { 310 return readl_relaxed(i2c_dev->base + reg); 311 } 312 313 /* 314 * If necessary, i2c_writel() and i2c_readl() will offset the register 315 * in order to talk to the I2C block inside the DVC block. 316 */ 317 static u32 tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, unsigned int reg) 318 { 319 if (IS_DVC(i2c_dev)) 320 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40; 321 else if (IS_VI(i2c_dev)) 322 reg = 0xc00 + (reg << 2); 323 324 return reg; 325 } 326 327 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg) 328 { 329 writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); 330 331 /* read back register to make sure that register writes completed */ 332 if (reg != I2C_TX_FIFO) 333 readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); 334 else if (IS_VI(i2c_dev)) 335 readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, I2C_INT_STATUS)); 336 } 337 338 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg) 339 { 340 return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); 341 } 342 343 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data, 344 unsigned int reg, unsigned int len) 345 { 346 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); 347 } 348 349 static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data, 350 unsigned int reg, unsigned int len) 351 { 352 u32 *data32 = data; 353 354 /* 355 * VI I2C controller has known hardware bug where writes get stuck 356 * when immediate multiple writes happen to TX_FIFO register. 357 * Recommended software work around is to read I2C register after 358 * each write to TX_FIFO register to flush out the data. 359 */ 360 while (len--) 361 i2c_writel(i2c_dev, *data32++, reg); 362 } 363 364 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data, 365 unsigned int reg, unsigned int len) 366 { 367 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); 368 } 369 370 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) 371 { 372 u32 int_mask; 373 374 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask; 375 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); 376 } 377 378 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) 379 { 380 u32 int_mask; 381 382 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask; 383 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); 384 } 385 386 static void tegra_i2c_dma_complete(void *args) 387 { 388 struct tegra_i2c_dev *i2c_dev = args; 389 390 complete(&i2c_dev->dma_complete); 391 } 392 393 static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len) 394 { 395 struct dma_async_tx_descriptor *dma_desc; 396 enum dma_transfer_direction dir; 397 398 dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len); 399 400 reinit_completion(&i2c_dev->dma_complete); 401 402 dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 403 404 dma_desc = dmaengine_prep_slave_single(i2c_dev->dma_chan, i2c_dev->dma_phys, 405 len, dir, DMA_PREP_INTERRUPT | 406 DMA_CTRL_ACK); 407 if (!dma_desc) { 408 dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n", 409 i2c_dev->msg_read ? "RX" : "TX"); 410 return -EINVAL; 411 } 412 413 dma_desc->callback = tegra_i2c_dma_complete; 414 dma_desc->callback_param = i2c_dev; 415 416 dmaengine_submit(dma_desc); 417 dma_async_issue_pending(i2c_dev->dma_chan); 418 419 return 0; 420 } 421 422 static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev) 423 { 424 if (i2c_dev->dma_buf) { 425 dma_free_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size, 426 i2c_dev->dma_buf, i2c_dev->dma_phys); 427 i2c_dev->dma_buf = NULL; 428 } 429 430 if (i2c_dev->dma_chan) { 431 dma_release_channel(i2c_dev->dma_chan); 432 i2c_dev->dma_chan = NULL; 433 } 434 } 435 436 static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) 437 { 438 dma_addr_t dma_phys; 439 u32 *dma_buf; 440 int err; 441 442 if (IS_VI(i2c_dev)) 443 return 0; 444 445 if (i2c_dev->hw->has_apb_dma) { 446 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) { 447 dev_dbg(i2c_dev->dev, "APB DMA support not enabled\n"); 448 return 0; 449 } 450 } else if (!IS_ENABLED(CONFIG_TEGRA186_GPC_DMA)) { 451 dev_dbg(i2c_dev->dev, "GPC DMA support not enabled\n"); 452 return 0; 453 } 454 455 /* 456 * The same channel will be used for both RX and TX. 457 * Keeping the name as "tx" for backward compatibility 458 * with existing devicetrees. 459 */ 460 i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx"); 461 if (IS_ERR(i2c_dev->dma_chan)) { 462 err = PTR_ERR(i2c_dev->dma_chan); 463 i2c_dev->dma_chan = NULL; 464 goto err_out; 465 } 466 467 i2c_dev->dma_dev = i2c_dev->dma_chan->device->dev; 468 i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len + 469 I2C_PACKET_HEADER_SIZE; 470 471 dma_buf = dma_alloc_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size, 472 &dma_phys, GFP_KERNEL | __GFP_NOWARN); 473 if (!dma_buf) { 474 dev_err(i2c_dev->dev, "failed to allocate DMA buffer\n"); 475 err = -ENOMEM; 476 goto err_out; 477 } 478 479 i2c_dev->dma_buf = dma_buf; 480 i2c_dev->dma_phys = dma_phys; 481 482 return 0; 483 484 err_out: 485 tegra_i2c_release_dma(i2c_dev); 486 if (err != -EPROBE_DEFER) { 487 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err); 488 dev_err(i2c_dev->dev, "falling back to PIO\n"); 489 return 0; 490 } 491 492 return err; 493 } 494 495 /* 496 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller) 497 * block. This block is identical to the rest of the I2C blocks, except that 498 * it only supports master mode, it has registers moved around, and it needs 499 * some extra init to get it into I2C mode. The register moves are handled 500 * by i2c_readl() and i2c_writel(). 501 */ 502 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev) 503 { 504 u32 val; 505 506 val = dvc_readl(i2c_dev, DVC_CTRL_REG3); 507 val |= DVC_CTRL_REG3_SW_PROG; 508 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN; 509 dvc_writel(i2c_dev, val, DVC_CTRL_REG3); 510 511 val = dvc_readl(i2c_dev, DVC_CTRL_REG1); 512 val |= DVC_CTRL_REG1_INTR_EN; 513 dvc_writel(i2c_dev, val, DVC_CTRL_REG1); 514 } 515 516 static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev) 517 { 518 u32 value; 519 520 value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) | 521 FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4); 522 i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_0); 523 524 value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) | 525 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) | 526 FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) | 527 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4); 528 i2c_writel(i2c_dev, value, I2C_INTERFACE_TIMING_1); 529 530 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) | 531 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8); 532 i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_0); 533 534 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) | 535 FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) | 536 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11); 537 i2c_writel(i2c_dev, value, I2C_HS_INTERFACE_TIMING_1); 538 539 value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND; 540 i2c_writel(i2c_dev, value, I2C_BUS_CLEAR_CNFG); 541 542 i2c_writel(i2c_dev, 0x0, I2C_TLOW_SEXT); 543 } 544 545 static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev, 546 u32 reg, u32 mask, u32 delay_us, 547 u32 timeout_us) 548 { 549 void __iomem *addr = i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg); 550 u32 val; 551 552 if (!i2c_dev->atomic_mode) 553 return readl_relaxed_poll_timeout(addr, val, !(val & mask), 554 delay_us, timeout_us); 555 556 return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask), 557 delay_us, timeout_us); 558 } 559 560 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev) 561 { 562 u32 mask, val, offset; 563 int err; 564 565 if (i2c_dev->hw->has_mst_fifo) { 566 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH | 567 I2C_MST_FIFO_CONTROL_RX_FLUSH; 568 offset = I2C_MST_FIFO_CONTROL; 569 } else { 570 mask = I2C_FIFO_CONTROL_TX_FLUSH | 571 I2C_FIFO_CONTROL_RX_FLUSH; 572 offset = I2C_FIFO_CONTROL; 573 } 574 575 val = i2c_readl(i2c_dev, offset); 576 val |= mask; 577 i2c_writel(i2c_dev, val, offset); 578 579 err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000); 580 if (err) { 581 dev_err(i2c_dev->dev, "failed to flush FIFO\n"); 582 return err; 583 } 584 585 return 0; 586 } 587 588 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev) 589 { 590 int err; 591 592 if (!i2c_dev->hw->has_config_load_reg) 593 return 0; 594 595 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD); 596 597 err = tegra_i2c_poll_register(i2c_dev, I2C_CONFIG_LOAD, 0xffffffff, 598 1000, I2C_CONFIG_LOAD_TIMEOUT); 599 if (err) { 600 dev_err(i2c_dev->dev, "failed to load config\n"); 601 return err; 602 } 603 604 return 0; 605 } 606 607 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev) 608 { 609 u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode; 610 struct i2c_timings *t = &i2c_dev->timings; 611 int err; 612 613 /* 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. 620 */ 621 err = device_reset(i2c_dev->dev); 622 WARN_ON_ONCE(err); 623 624 if (IS_DVC(i2c_dev)) 625 tegra_dvc_init(i2c_dev); 626 627 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN | 628 FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2); 629 630 if (i2c_dev->hw->has_multi_master_mode) 631 val |= I2C_CNFG_MULTI_MASTER_MODE; 632 633 i2c_writel(i2c_dev, val, I2C_CNFG); 634 i2c_writel(i2c_dev, 0, I2C_INT_MASK); 635 636 if (IS_VI(i2c_dev)) 637 tegra_i2c_vi_init(i2c_dev); 638 639 switch (t->bus_freq_hz) { 640 case I2C_MAX_STANDARD_MODE_FREQ + 1 ... I2C_MAX_FAST_MODE_PLUS_FREQ: 641 default: 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; 645 646 if (t->bus_freq_hz > I2C_MAX_FAST_MODE_FREQ) 647 non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode; 648 else 649 non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode; 650 break; 651 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; 657 break; 658 } 659 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); 665 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); 670 } 671 672 /* 673 * Configure setup and hold times only when tsu_thd is non-zero. 674 * Otherwise, preserve the chip default values. 675 */ 676 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd) 677 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1); 678 679 clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1); 680 681 err = clk_set_rate(i2c_dev->div_clk, 682 t->bus_freq_hz * clk_multiplier); 683 if (err) { 684 dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err); 685 return err; 686 } 687 688 if (!IS_DVC(i2c_dev) && !IS_VI(i2c_dev)) { 689 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG); 690 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); 695 } 696 697 err = tegra_i2c_flush_fifos(i2c_dev); 698 if (err) 699 return err; 700 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); 703 704 err = tegra_i2c_wait_for_config_load(i2c_dev); 705 if (err) 706 return err; 707 708 return 0; 709 } 710 711 static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev) 712 { 713 u32 cnfg; 714 715 /* 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. 720 */ 721 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->timings.bus_freq_hz)); 722 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); 726 727 return tegra_i2c_wait_for_config_load(i2c_dev); 728 } 729 730 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev) 731 { 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; 735 u32 val; 736 737 /* 738 * Catch overflow due to message fully sent before the check for 739 * RX FIFO availability. 740 */ 741 if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining))) 742 return -EINVAL; 743 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); 747 } else { 748 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); 749 rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val); 750 } 751 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; 756 757 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer); 758 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; 762 763 /* 764 * If there is a partial word at the end of buffer, handle it 765 * manually to prevent overwriting past the end of buffer. 766 */ 767 if (rx_fifo_avail > 0 && buf_remaining > 0) { 768 /* 769 * buf_remaining > 3 check not needed as rx_fifo_avail == 0 770 * when (words_to_transfer was > rx_fifo_avail) earlier 771 * in this function. 772 */ 773 val = i2c_readl(i2c_dev, I2C_RX_FIFO); 774 val = cpu_to_le32(val); 775 memcpy(buf, &val, buf_remaining); 776 buf_remaining = 0; 777 rx_fifo_avail--; 778 } 779 780 /* RX FIFO must be drained, otherwise it's an Overflow case. */ 781 if (WARN_ON_ONCE(rx_fifo_avail)) 782 return -EINVAL; 783 784 i2c_dev->msg_buf_remaining = buf_remaining; 785 i2c_dev->msg_buf = buf; 786 787 return 0; 788 } 789 790 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev) 791 { 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; 795 u32 val; 796 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); 800 } else { 801 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); 802 tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val); 803 } 804 805 /* round down to exclude partial word at the end of buffer */ 806 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; 807 808 /* 809 * This hunk pushes 4 bytes at a time into the TX FIFO. 810 * 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. 813 */ 814 if (words_to_transfer) { 815 if (words_to_transfer > tx_fifo_avail) 816 words_to_transfer = tx_fifo_avail; 817 818 /* 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. 824 */ 825 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; 826 tx_fifo_avail -= words_to_transfer; 827 828 i2c_dev->msg_buf_remaining = buf_remaining; 829 i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD; 830 831 if (IS_VI(i2c_dev)) 832 i2c_writesl_vi(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer); 833 else 834 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer); 835 836 buf += words_to_transfer * BYTES_PER_FIFO_WORD; 837 } 838 839 /* 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. 843 */ 844 if (tx_fifo_avail > 0 && buf_remaining > 0) { 845 /* 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. 849 */ 850 memcpy(&val, buf, buf_remaining); 851 val = le32_to_cpu(val); 852 853 i2c_dev->msg_buf_remaining = 0; 854 i2c_dev->msg_buf = NULL; 855 856 i2c_writel(i2c_dev, val, I2C_TX_FIFO); 857 } 858 859 return 0; 860 } 861 862 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id) 863 { 864 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; 865 struct tegra_i2c_dev *i2c_dev = dev_id; 866 u32 status; 867 868 status = i2c_readl(i2c_dev, I2C_INT_STATUS); 869 870 if (status == 0) { 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; 876 goto err; 877 } 878 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; 885 goto err; 886 } 887 888 /* 889 * I2C transfer is terminated during the bus clear, so skip 890 * processing the other interrupts. 891 */ 892 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE)) 893 goto err; 894 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)) { 898 /* 899 * Overflow error condition: message fully sent, 900 * with no XFER_COMPLETE interrupt but hardware 901 * asks to transfer more. 902 */ 903 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW; 904 goto err; 905 } 906 } 907 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); 911 else 912 tegra_i2c_mask_irq(i2c_dev, 913 I2C_INT_TX_FIFO_DATA_REQ); 914 } 915 } 916 917 i2c_writel(i2c_dev, status, I2C_INT_STATUS); 918 if (IS_DVC(i2c_dev)) 919 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); 920 921 /* 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. 925 * 926 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer, 927 * so forcing msg_buf_remaining to 0 in DMA mode. 928 */ 929 if (status & I2C_INT_PACKET_XFER_COMPLETE) { 930 if (i2c_dev->dma_mode) 931 i2c_dev->msg_buf_remaining = 0; 932 /* 933 * Underflow error condition: XFER_COMPLETE before message 934 * fully sent. 935 */ 936 if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) { 937 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; 938 goto err; 939 } 940 complete(&i2c_dev->msg_complete); 941 } 942 goto done; 943 err: 944 /* mask all interrupts on error */ 945 tegra_i2c_mask_irq(i2c_dev, 946 I2C_INT_NO_ACK | 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); 951 952 if (i2c_dev->hw->supports_bus_clear) 953 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 954 955 i2c_writel(i2c_dev, status, I2C_INT_STATUS); 956 957 if (IS_DVC(i2c_dev)) 958 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); 959 960 if (i2c_dev->dma_mode) { 961 dmaengine_terminate_async(i2c_dev->dma_chan); 962 complete(&i2c_dev->dma_complete); 963 } 964 965 complete(&i2c_dev->msg_complete); 966 done: 967 return IRQ_HANDLED; 968 } 969 970 static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev, 971 size_t len) 972 { 973 struct dma_slave_config slv_config = {0}; 974 u32 val, reg, dma_burst, reg_offset; 975 int err; 976 977 if (i2c_dev->hw->has_mst_fifo) 978 reg = I2C_MST_FIFO_CONTROL; 979 else 980 reg = I2C_FIFO_CONTROL; 981 982 if (i2c_dev->dma_mode) { 983 if (len & 0xF) 984 dma_burst = 1; 985 else if (len & 0x10) 986 dma_burst = 4; 987 else 988 dma_burst = 8; 989 990 if (i2c_dev->msg_read) { 991 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO); 992 993 slv_config.src_addr = i2c_dev->base_phys + reg_offset; 994 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 995 slv_config.src_maxburst = dma_burst; 996 997 if (i2c_dev->hw->has_mst_fifo) 998 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst); 999 else 1000 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst); 1001 } else { 1002 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO); 1003 1004 slv_config.dst_addr = i2c_dev->base_phys + reg_offset; 1005 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1006 slv_config.dst_maxburst = dma_burst; 1007 1008 if (i2c_dev->hw->has_mst_fifo) 1009 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst); 1010 else 1011 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst); 1012 } 1013 1014 slv_config.device_fc = true; 1015 err = dmaengine_slave_config(i2c_dev->dma_chan, &slv_config); 1016 if (err) { 1017 dev_err(i2c_dev->dev, "DMA config failed: %d\n", err); 1018 dev_err(i2c_dev->dev, "falling back to PIO\n"); 1019 1020 tegra_i2c_release_dma(i2c_dev); 1021 i2c_dev->dma_mode = false; 1022 } else { 1023 goto out; 1024 } 1025 } 1026 1027 if (i2c_dev->hw->has_mst_fifo) 1028 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) | 1029 I2C_MST_FIFO_CONTROL_RX_TRIG(1); 1030 else 1031 val = I2C_FIFO_CONTROL_TX_TRIG(8) | 1032 I2C_FIFO_CONTROL_RX_TRIG(1); 1033 out: 1034 i2c_writel(i2c_dev, val, reg); 1035 } 1036 1037 static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev, 1038 struct completion *complete, 1039 unsigned int timeout_ms) 1040 { 1041 ktime_t ktime = ktime_get(); 1042 ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms); 1043 1044 do { 1045 u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS); 1046 1047 if (status) 1048 tegra_i2c_isr(i2c_dev->irq, i2c_dev); 1049 1050 if (completion_done(complete)) { 1051 s64 delta = ktime_ms_delta(ktimeout, ktime); 1052 1053 return msecs_to_jiffies(delta) ?: 1; 1054 } 1055 1056 ktime = ktime_get(); 1057 1058 } while (ktime_before(ktime, ktimeout)); 1059 1060 return 0; 1061 } 1062 1063 static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev, 1064 struct completion *complete, 1065 unsigned int timeout_ms) 1066 { 1067 unsigned long ret; 1068 1069 if (i2c_dev->atomic_mode) { 1070 ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms); 1071 } else { 1072 enable_irq(i2c_dev->irq); 1073 ret = wait_for_completion_timeout(complete, 1074 msecs_to_jiffies(timeout_ms)); 1075 disable_irq(i2c_dev->irq); 1076 1077 /* 1078 * Under some rare circumstances (like running KASAN + 1079 * NFS root) CPU, which handles interrupt, may stuck in 1080 * uninterruptible state for a significant time. In this 1081 * case we will get timeout if I2C transfer is running on 1082 * a sibling CPU, despite of IRQ being raised. 1083 * 1084 * In order to handle this rare condition, the IRQ status 1085 * needs to be checked after timeout. 1086 */ 1087 if (ret == 0) 1088 ret = tegra_i2c_poll_completion(i2c_dev, complete, 0); 1089 } 1090 1091 return ret; 1092 } 1093 1094 static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap) 1095 { 1096 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1097 u32 val, time_left; 1098 int err; 1099 1100 reinit_completion(&i2c_dev->msg_complete); 1101 1102 val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND | 1103 I2C_BC_TERMINATE; 1104 i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG); 1105 1106 err = tegra_i2c_wait_for_config_load(i2c_dev); 1107 if (err) 1108 return err; 1109 1110 val |= I2C_BC_ENABLE; 1111 i2c_writel(i2c_dev, val, I2C_BUS_CLEAR_CNFG); 1112 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 1113 1114 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50); 1115 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 1116 1117 if (time_left == 0) { 1118 dev_err(i2c_dev->dev, "failed to clear bus\n"); 1119 return -ETIMEDOUT; 1120 } 1121 1122 val = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS); 1123 if (!(val & I2C_BC_STATUS)) { 1124 dev_err(i2c_dev->dev, "un-recovered arbitration lost\n"); 1125 return -EIO; 1126 } 1127 1128 return -EAGAIN; 1129 } 1130 1131 static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev, 1132 struct i2c_msg *msg, 1133 enum msg_end_type end_state) 1134 { 1135 u32 *dma_buf = i2c_dev->dma_buf; 1136 u32 packet_header; 1137 1138 packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) | 1139 FIELD_PREP(PACKET_HEADER0_PROTOCOL, 1140 PACKET_HEADER0_PROTOCOL_I2C) | 1141 FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) | 1142 FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1); 1143 1144 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1145 *dma_buf++ = packet_header; 1146 else 1147 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); 1148 1149 packet_header = i2c_dev->msg_len - 1; 1150 1151 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1152 *dma_buf++ = packet_header; 1153 else 1154 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); 1155 1156 packet_header = I2C_HEADER_IE_ENABLE; 1157 1158 if (end_state == MSG_END_CONTINUE) 1159 packet_header |= I2C_HEADER_CONTINUE_XFER; 1160 else if (end_state == MSG_END_REPEAT_START) 1161 packet_header |= I2C_HEADER_REPEAT_START; 1162 1163 if (msg->flags & I2C_M_TEN) { 1164 packet_header |= msg->addr; 1165 packet_header |= I2C_HEADER_10BIT_ADDR; 1166 } else { 1167 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT; 1168 } 1169 1170 if (msg->flags & I2C_M_IGNORE_NAK) 1171 packet_header |= I2C_HEADER_CONT_ON_NAK; 1172 1173 if (msg->flags & I2C_M_RD) 1174 packet_header |= I2C_HEADER_READ; 1175 1176 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1177 *dma_buf++ = packet_header; 1178 else 1179 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); 1180 } 1181 1182 static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev, 1183 struct i2c_msg *msg) 1184 { 1185 if (i2c_dev->msg_err == I2C_ERR_NONE) 1186 return 0; 1187 1188 tegra_i2c_init(i2c_dev); 1189 1190 /* start recovery upon arbitration loss in single master mode */ 1191 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) { 1192 if (!i2c_dev->multimaster_mode) 1193 return i2c_recover_bus(&i2c_dev->adapter); 1194 1195 return -EAGAIN; 1196 } 1197 1198 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) { 1199 if (msg->flags & I2C_M_IGNORE_NAK) 1200 return 0; 1201 1202 return -EREMOTEIO; 1203 } 1204 1205 return -EIO; 1206 } 1207 1208 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev, 1209 struct i2c_msg *msg, 1210 enum msg_end_type end_state) 1211 { 1212 unsigned long time_left, xfer_time = 100; 1213 size_t xfer_size; 1214 u32 int_mask; 1215 int err; 1216 1217 err = tegra_i2c_flush_fifos(i2c_dev); 1218 if (err) 1219 return err; 1220 1221 i2c_dev->msg_buf = msg->buf; 1222 i2c_dev->msg_len = msg->len; 1223 1224 i2c_dev->msg_err = I2C_ERR_NONE; 1225 i2c_dev->msg_read = !!(msg->flags & I2C_M_RD); 1226 reinit_completion(&i2c_dev->msg_complete); 1227 1228 /* 1229 * For SMBUS block read command, read only 1 byte in the first transfer. 1230 * Adjust that 1 byte for the next transfer in the msg buffer and msg 1231 * length. 1232 */ 1233 if (msg->flags & I2C_M_RECV_LEN) { 1234 if (end_state == MSG_END_CONTINUE) { 1235 i2c_dev->msg_len = 1; 1236 } else { 1237 i2c_dev->msg_buf += 1; 1238 i2c_dev->msg_len -= 1; 1239 } 1240 } 1241 1242 i2c_dev->msg_buf_remaining = i2c_dev->msg_len; 1243 1244 if (i2c_dev->msg_read) 1245 xfer_size = i2c_dev->msg_len; 1246 else 1247 xfer_size = i2c_dev->msg_len + I2C_PACKET_HEADER_SIZE; 1248 1249 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD); 1250 1251 i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN && 1252 i2c_dev->dma_buf && !i2c_dev->atomic_mode; 1253 1254 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size); 1255 1256 /* 1257 * Transfer time in mSec = Total bits / transfer rate 1258 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits 1259 */ 1260 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC, 1261 i2c_dev->timings.bus_freq_hz); 1262 1263 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; 1264 tegra_i2c_unmask_irq(i2c_dev, int_mask); 1265 1266 if (i2c_dev->dma_mode) { 1267 if (i2c_dev->msg_read) { 1268 dma_sync_single_for_device(i2c_dev->dma_dev, 1269 i2c_dev->dma_phys, 1270 xfer_size, DMA_FROM_DEVICE); 1271 1272 err = tegra_i2c_dma_submit(i2c_dev, xfer_size); 1273 if (err) 1274 return err; 1275 } else { 1276 dma_sync_single_for_cpu(i2c_dev->dma_dev, 1277 i2c_dev->dma_phys, 1278 xfer_size, DMA_TO_DEVICE); 1279 } 1280 } 1281 1282 tegra_i2c_push_packet_header(i2c_dev, msg, end_state); 1283 1284 if (!i2c_dev->msg_read) { 1285 if (i2c_dev->dma_mode) { 1286 memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE, 1287 msg->buf, i2c_dev->msg_len); 1288 1289 dma_sync_single_for_device(i2c_dev->dma_dev, 1290 i2c_dev->dma_phys, 1291 xfer_size, DMA_TO_DEVICE); 1292 1293 err = tegra_i2c_dma_submit(i2c_dev, xfer_size); 1294 if (err) 1295 return err; 1296 } else { 1297 tegra_i2c_fill_tx_fifo(i2c_dev); 1298 } 1299 } 1300 1301 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq) 1302 int_mask |= I2C_INT_PACKET_XFER_COMPLETE; 1303 1304 if (!i2c_dev->dma_mode) { 1305 if (msg->flags & I2C_M_RD) 1306 int_mask |= I2C_INT_RX_FIFO_DATA_REQ; 1307 else if (i2c_dev->msg_buf_remaining) 1308 int_mask |= I2C_INT_TX_FIFO_DATA_REQ; 1309 } 1310 1311 tegra_i2c_unmask_irq(i2c_dev, int_mask); 1312 dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n", 1313 i2c_readl(i2c_dev, I2C_INT_MASK)); 1314 1315 if (i2c_dev->dma_mode) { 1316 time_left = tegra_i2c_wait_completion(i2c_dev, 1317 &i2c_dev->dma_complete, 1318 xfer_time); 1319 1320 /* 1321 * Synchronize DMA first, since dmaengine_terminate_sync() 1322 * performs synchronization after the transfer's termination 1323 * and we want to get a completion if transfer succeeded. 1324 */ 1325 dmaengine_synchronize(i2c_dev->dma_chan); 1326 dmaengine_terminate_sync(i2c_dev->dma_chan); 1327 1328 if (!time_left && !completion_done(&i2c_dev->dma_complete)) { 1329 tegra_i2c_init(i2c_dev); 1330 return -ETIMEDOUT; 1331 } 1332 1333 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) { 1334 dma_sync_single_for_cpu(i2c_dev->dma_dev, 1335 i2c_dev->dma_phys, 1336 xfer_size, DMA_FROM_DEVICE); 1337 1338 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, i2c_dev->msg_len); 1339 } 1340 } 1341 1342 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 1343 xfer_time); 1344 1345 tegra_i2c_mask_irq(i2c_dev, int_mask); 1346 1347 if (time_left == 0) { 1348 tegra_i2c_init(i2c_dev); 1349 return -ETIMEDOUT; 1350 } 1351 1352 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n", 1353 time_left, completion_done(&i2c_dev->msg_complete), 1354 i2c_dev->msg_err); 1355 1356 i2c_dev->dma_mode = false; 1357 1358 err = tegra_i2c_error_recover(i2c_dev, msg); 1359 if (err) 1360 return err; 1361 1362 return 0; 1363 } 1364 1365 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], 1366 int num) 1367 { 1368 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1369 int i, ret; 1370 1371 ret = pm_runtime_get_sync(i2c_dev->dev); 1372 if (ret < 0) { 1373 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret); 1374 pm_runtime_put_noidle(i2c_dev->dev); 1375 return ret; 1376 } 1377 1378 for (i = 0; i < num; i++) { 1379 enum msg_end_type end_type = MSG_END_STOP; 1380 1381 if (i < (num - 1)) { 1382 /* check whether follow up message is coming */ 1383 if (msgs[i + 1].flags & I2C_M_NOSTART) 1384 end_type = MSG_END_CONTINUE; 1385 else 1386 end_type = MSG_END_REPEAT_START; 1387 } 1388 /* If M_RECV_LEN use ContinueXfer to read the first byte */ 1389 if (msgs[i].flags & I2C_M_RECV_LEN) { 1390 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], MSG_END_CONTINUE); 1391 if (ret) 1392 break; 1393 1394 /* Validate message length before proceeding */ 1395 if (msgs[i].buf[0] == 0 || msgs[i].buf[0] > I2C_SMBUS_BLOCK_MAX) 1396 break; 1397 1398 /* Set the msg length from first byte */ 1399 msgs[i].len += msgs[i].buf[0]; 1400 dev_dbg(i2c_dev->dev, "reading %d bytes\n", msgs[i].len); 1401 } 1402 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type); 1403 if (ret) 1404 break; 1405 } 1406 1407 pm_runtime_put(i2c_dev->dev); 1408 1409 return ret ?: i; 1410 } 1411 1412 static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap, 1413 struct i2c_msg msgs[], int num) 1414 { 1415 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1416 int ret; 1417 1418 i2c_dev->atomic_mode = true; 1419 ret = tegra_i2c_xfer(adap, msgs, num); 1420 i2c_dev->atomic_mode = false; 1421 1422 return ret; 1423 } 1424 1425 static u32 tegra_i2c_func(struct i2c_adapter *adap) 1426 { 1427 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1428 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | 1429 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; 1430 1431 if (i2c_dev->hw->has_continue_xfer_support) 1432 ret |= I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_READ_BLOCK_DATA; 1433 1434 return ret; 1435 } 1436 1437 static const struct i2c_algorithm tegra_i2c_algo = { 1438 .xfer = tegra_i2c_xfer, 1439 .xfer_atomic = tegra_i2c_xfer_atomic, 1440 .functionality = tegra_i2c_func, 1441 }; 1442 1443 /* payload size is only 12 bit */ 1444 static const struct i2c_adapter_quirks tegra_i2c_quirks = { 1445 .flags = I2C_AQ_NO_ZERO_LEN, 1446 .max_read_len = SZ_4K, 1447 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE, 1448 }; 1449 1450 static const struct i2c_adapter_quirks tegra194_i2c_quirks = { 1451 .flags = I2C_AQ_NO_ZERO_LEN, 1452 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE, 1453 }; 1454 1455 static struct i2c_bus_recovery_info tegra_i2c_recovery_info = { 1456 .recover_bus = tegra_i2c_issue_bus_clear, 1457 }; 1458 1459 static const struct tegra_i2c_hw_feature tegra20_i2c_hw = { 1460 .has_continue_xfer_support = false, 1461 .has_per_pkt_xfer_complete_irq = false, 1462 .clk_divisor_hs_mode = 3, 1463 .clk_divisor_std_mode = 0, 1464 .clk_divisor_fast_mode = 0, 1465 .clk_divisor_fast_plus_mode = 0, 1466 .has_config_load_reg = false, 1467 .has_multi_master_mode = false, 1468 .has_slcg_override_reg = false, 1469 .has_mst_fifo = false, 1470 .quirks = &tegra_i2c_quirks, 1471 .supports_bus_clear = false, 1472 .has_apb_dma = true, 1473 .tlow_std_mode = 0x4, 1474 .thigh_std_mode = 0x2, 1475 .tlow_fast_fastplus_mode = 0x4, 1476 .thigh_fast_fastplus_mode = 0x2, 1477 .setup_hold_time_std_mode = 0x0, 1478 .setup_hold_time_fast_fast_plus_mode = 0x0, 1479 .setup_hold_time_hs_mode = 0x0, 1480 .has_interface_timing_reg = false, 1481 }; 1482 1483 static const struct tegra_i2c_hw_feature tegra30_i2c_hw = { 1484 .has_continue_xfer_support = true, 1485 .has_per_pkt_xfer_complete_irq = false, 1486 .clk_divisor_hs_mode = 3, 1487 .clk_divisor_std_mode = 0, 1488 .clk_divisor_fast_mode = 0, 1489 .clk_divisor_fast_plus_mode = 0, 1490 .has_config_load_reg = false, 1491 .has_multi_master_mode = false, 1492 .has_slcg_override_reg = false, 1493 .has_mst_fifo = false, 1494 .quirks = &tegra_i2c_quirks, 1495 .supports_bus_clear = false, 1496 .has_apb_dma = true, 1497 .tlow_std_mode = 0x4, 1498 .thigh_std_mode = 0x2, 1499 .tlow_fast_fastplus_mode = 0x4, 1500 .thigh_fast_fastplus_mode = 0x2, 1501 .setup_hold_time_std_mode = 0x0, 1502 .setup_hold_time_fast_fast_plus_mode = 0x0, 1503 .setup_hold_time_hs_mode = 0x0, 1504 .has_interface_timing_reg = false, 1505 }; 1506 1507 static const struct tegra_i2c_hw_feature tegra114_i2c_hw = { 1508 .has_continue_xfer_support = true, 1509 .has_per_pkt_xfer_complete_irq = true, 1510 .clk_divisor_hs_mode = 1, 1511 .clk_divisor_std_mode = 0x19, 1512 .clk_divisor_fast_mode = 0x19, 1513 .clk_divisor_fast_plus_mode = 0x10, 1514 .has_config_load_reg = false, 1515 .has_multi_master_mode = false, 1516 .has_slcg_override_reg = false, 1517 .has_mst_fifo = false, 1518 .quirks = &tegra_i2c_quirks, 1519 .supports_bus_clear = true, 1520 .has_apb_dma = true, 1521 .tlow_std_mode = 0x4, 1522 .thigh_std_mode = 0x2, 1523 .tlow_fast_fastplus_mode = 0x4, 1524 .thigh_fast_fastplus_mode = 0x2, 1525 .setup_hold_time_std_mode = 0x0, 1526 .setup_hold_time_fast_fast_plus_mode = 0x0, 1527 .setup_hold_time_hs_mode = 0x0, 1528 .has_interface_timing_reg = false, 1529 }; 1530 1531 static const struct tegra_i2c_hw_feature tegra124_i2c_hw = { 1532 .has_continue_xfer_support = true, 1533 .has_per_pkt_xfer_complete_irq = true, 1534 .clk_divisor_hs_mode = 1, 1535 .clk_divisor_std_mode = 0x19, 1536 .clk_divisor_fast_mode = 0x19, 1537 .clk_divisor_fast_plus_mode = 0x10, 1538 .has_config_load_reg = true, 1539 .has_multi_master_mode = false, 1540 .has_slcg_override_reg = true, 1541 .has_mst_fifo = false, 1542 .quirks = &tegra_i2c_quirks, 1543 .supports_bus_clear = true, 1544 .has_apb_dma = true, 1545 .tlow_std_mode = 0x4, 1546 .thigh_std_mode = 0x2, 1547 .tlow_fast_fastplus_mode = 0x4, 1548 .thigh_fast_fastplus_mode = 0x2, 1549 .setup_hold_time_std_mode = 0x0, 1550 .setup_hold_time_fast_fast_plus_mode = 0x0, 1551 .setup_hold_time_hs_mode = 0x0, 1552 .has_interface_timing_reg = true, 1553 }; 1554 1555 static const struct tegra_i2c_hw_feature tegra210_i2c_hw = { 1556 .has_continue_xfer_support = true, 1557 .has_per_pkt_xfer_complete_irq = true, 1558 .clk_divisor_hs_mode = 1, 1559 .clk_divisor_std_mode = 0x19, 1560 .clk_divisor_fast_mode = 0x19, 1561 .clk_divisor_fast_plus_mode = 0x10, 1562 .has_config_load_reg = true, 1563 .has_multi_master_mode = false, 1564 .has_slcg_override_reg = true, 1565 .has_mst_fifo = false, 1566 .quirks = &tegra_i2c_quirks, 1567 .supports_bus_clear = true, 1568 .has_apb_dma = true, 1569 .tlow_std_mode = 0x4, 1570 .thigh_std_mode = 0x2, 1571 .tlow_fast_fastplus_mode = 0x4, 1572 .thigh_fast_fastplus_mode = 0x2, 1573 .setup_hold_time_std_mode = 0, 1574 .setup_hold_time_fast_fast_plus_mode = 0, 1575 .setup_hold_time_hs_mode = 0, 1576 .has_interface_timing_reg = true, 1577 }; 1578 1579 static const struct tegra_i2c_hw_feature tegra186_i2c_hw = { 1580 .has_continue_xfer_support = true, 1581 .has_per_pkt_xfer_complete_irq = true, 1582 .clk_divisor_hs_mode = 1, 1583 .clk_divisor_std_mode = 0x16, 1584 .clk_divisor_fast_mode = 0x19, 1585 .clk_divisor_fast_plus_mode = 0x10, 1586 .has_config_load_reg = true, 1587 .has_multi_master_mode = false, 1588 .has_slcg_override_reg = true, 1589 .has_mst_fifo = false, 1590 .quirks = &tegra_i2c_quirks, 1591 .supports_bus_clear = true, 1592 .has_apb_dma = false, 1593 .tlow_std_mode = 0x4, 1594 .thigh_std_mode = 0x3, 1595 .tlow_fast_fastplus_mode = 0x4, 1596 .thigh_fast_fastplus_mode = 0x2, 1597 .setup_hold_time_std_mode = 0, 1598 .setup_hold_time_fast_fast_plus_mode = 0, 1599 .setup_hold_time_hs_mode = 0, 1600 .has_interface_timing_reg = true, 1601 }; 1602 1603 static const struct tegra_i2c_hw_feature tegra194_i2c_hw = { 1604 .has_continue_xfer_support = true, 1605 .has_per_pkt_xfer_complete_irq = true, 1606 .clk_divisor_hs_mode = 1, 1607 .clk_divisor_std_mode = 0x4f, 1608 .clk_divisor_fast_mode = 0x3c, 1609 .clk_divisor_fast_plus_mode = 0x16, 1610 .has_config_load_reg = true, 1611 .has_multi_master_mode = true, 1612 .has_slcg_override_reg = true, 1613 .has_mst_fifo = true, 1614 .quirks = &tegra194_i2c_quirks, 1615 .supports_bus_clear = true, 1616 .has_apb_dma = false, 1617 .tlow_std_mode = 0x8, 1618 .thigh_std_mode = 0x7, 1619 .tlow_fast_fastplus_mode = 0x2, 1620 .thigh_fast_fastplus_mode = 0x2, 1621 .setup_hold_time_std_mode = 0x08080808, 1622 .setup_hold_time_fast_fast_plus_mode = 0x02020202, 1623 .setup_hold_time_hs_mode = 0x090909, 1624 .has_interface_timing_reg = true, 1625 }; 1626 1627 static const struct of_device_id tegra_i2c_of_match[] = { 1628 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, }, 1629 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, }, 1630 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 1631 { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_i2c_hw, }, 1632 #endif 1633 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, }, 1634 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, }, 1635 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, }, 1636 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, }, 1637 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, }, 1638 #if IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) 1639 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, }, 1640 #endif 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 (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) && 1656 of_device_is_compatible(np, "nvidia,tegra20-i2c-dvc")) 1657 i2c_dev->is_dvc = true; 1658 1659 if (IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) && 1660 of_device_is_compatible(np, "nvidia,tegra210-i2c-vi")) 1661 i2c_dev->is_vi = true; 1662 } 1663 1664 static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev) 1665 { 1666 int err; 1667 1668 if (ACPI_HANDLE(i2c_dev->dev)) 1669 return 0; 1670 1671 i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk"; 1672 1673 if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw) 1674 i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk"; 1675 1676 if (IS_VI(i2c_dev)) 1677 i2c_dev->clocks[i2c_dev->nclocks++].id = "slow"; 1678 1679 err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks, 1680 i2c_dev->clocks); 1681 if (err) 1682 return err; 1683 1684 err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks); 1685 if (err) 1686 return err; 1687 1688 i2c_dev->div_clk = i2c_dev->clocks[0].clk; 1689 1690 if (!i2c_dev->multimaster_mode) 1691 return 0; 1692 1693 err = clk_enable(i2c_dev->div_clk); 1694 if (err) { 1695 dev_err(i2c_dev->dev, "failed to enable div-clk: %d\n", err); 1696 goto unprepare_clocks; 1697 } 1698 1699 return 0; 1700 1701 unprepare_clocks: 1702 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks); 1703 1704 return err; 1705 } 1706 1707 static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev) 1708 { 1709 if (i2c_dev->multimaster_mode) 1710 clk_disable(i2c_dev->div_clk); 1711 1712 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks); 1713 } 1714 1715 static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev) 1716 { 1717 int ret; 1718 1719 ret = pm_runtime_get_sync(i2c_dev->dev); 1720 if (ret < 0) 1721 dev_err(i2c_dev->dev, "runtime resume failed: %d\n", ret); 1722 else 1723 ret = tegra_i2c_init(i2c_dev); 1724 1725 pm_runtime_put_sync(i2c_dev->dev); 1726 1727 return ret; 1728 } 1729 1730 static int tegra_i2c_probe(struct platform_device *pdev) 1731 { 1732 struct tegra_i2c_dev *i2c_dev; 1733 struct resource *res; 1734 int err; 1735 1736 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 1737 if (!i2c_dev) 1738 return -ENOMEM; 1739 1740 platform_set_drvdata(pdev, i2c_dev); 1741 1742 init_completion(&i2c_dev->msg_complete); 1743 init_completion(&i2c_dev->dma_complete); 1744 1745 i2c_dev->hw = device_get_match_data(&pdev->dev); 1746 i2c_dev->cont_id = pdev->id; 1747 i2c_dev->dev = &pdev->dev; 1748 1749 i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1750 if (IS_ERR(i2c_dev->base)) 1751 return PTR_ERR(i2c_dev->base); 1752 1753 i2c_dev->base_phys = res->start; 1754 1755 err = platform_get_irq(pdev, 0); 1756 if (err < 0) 1757 return err; 1758 1759 i2c_dev->irq = err; 1760 1761 /* interrupt will be enabled during of transfer time */ 1762 irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN); 1763 1764 err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq, 1765 NULL, tegra_i2c_isr, 1766 IRQF_NO_SUSPEND | IRQF_ONESHOT, 1767 dev_name(i2c_dev->dev), i2c_dev); 1768 if (err) 1769 return err; 1770 1771 tegra_i2c_parse_dt(i2c_dev); 1772 1773 err = tegra_i2c_init_clocks(i2c_dev); 1774 if (err) 1775 return err; 1776 1777 err = tegra_i2c_init_dma(i2c_dev); 1778 if (err) 1779 goto release_clocks; 1780 1781 /* 1782 * VI I2C is in VE power domain which is not always ON and not 1783 * IRQ-safe. Thus, IRQ-safe device shouldn't be attached to a 1784 * non IRQ-safe domain because this prevents powering off the power 1785 * domain. 1786 * 1787 * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't 1788 * be used for atomic transfers. ACPI device is not IRQ safe also. 1789 */ 1790 if (!IS_VI(i2c_dev) && !has_acpi_companion(i2c_dev->dev)) 1791 pm_runtime_irq_safe(i2c_dev->dev); 1792 1793 pm_runtime_enable(i2c_dev->dev); 1794 1795 err = tegra_i2c_init_hardware(i2c_dev); 1796 if (err) 1797 goto release_rpm; 1798 1799 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev); 1800 i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node; 1801 i2c_dev->adapter.dev.parent = i2c_dev->dev; 1802 i2c_dev->adapter.retries = 1; 1803 i2c_dev->adapter.timeout = 6 * HZ; 1804 i2c_dev->adapter.quirks = i2c_dev->hw->quirks; 1805 i2c_dev->adapter.owner = THIS_MODULE; 1806 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED; 1807 i2c_dev->adapter.algo = &tegra_i2c_algo; 1808 i2c_dev->adapter.nr = pdev->id; 1809 ACPI_COMPANION_SET(&i2c_dev->adapter.dev, ACPI_COMPANION(&pdev->dev)); 1810 1811 if (i2c_dev->hw->supports_bus_clear) 1812 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info; 1813 1814 strscpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev), 1815 sizeof(i2c_dev->adapter.name)); 1816 1817 err = i2c_add_numbered_adapter(&i2c_dev->adapter); 1818 if (err) 1819 goto release_rpm; 1820 1821 return 0; 1822 1823 release_rpm: 1824 pm_runtime_disable(i2c_dev->dev); 1825 1826 tegra_i2c_release_dma(i2c_dev); 1827 release_clocks: 1828 tegra_i2c_release_clocks(i2c_dev); 1829 1830 return err; 1831 } 1832 1833 static void tegra_i2c_remove(struct platform_device *pdev) 1834 { 1835 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 1836 1837 i2c_del_adapter(&i2c_dev->adapter); 1838 pm_runtime_force_suspend(i2c_dev->dev); 1839 1840 tegra_i2c_release_dma(i2c_dev); 1841 tegra_i2c_release_clocks(i2c_dev); 1842 } 1843 1844 static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev) 1845 { 1846 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 1847 int err; 1848 1849 err = pinctrl_pm_select_default_state(dev); 1850 if (err) 1851 return err; 1852 1853 err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks); 1854 if (err) 1855 return err; 1856 1857 /* 1858 * VI I2C device is attached to VE power domain which goes through 1859 * power ON/OFF during runtime PM resume/suspend, meaning that 1860 * controller needs to be re-initialized after power ON. 1861 */ 1862 if (IS_VI(i2c_dev)) { 1863 err = tegra_i2c_init(i2c_dev); 1864 if (err) 1865 goto disable_clocks; 1866 } 1867 1868 return 0; 1869 1870 disable_clocks: 1871 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks); 1872 1873 return err; 1874 } 1875 1876 static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev) 1877 { 1878 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 1879 1880 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks); 1881 1882 return pinctrl_pm_select_idle_state(dev); 1883 } 1884 1885 static int __maybe_unused tegra_i2c_suspend(struct device *dev) 1886 { 1887 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 1888 int err; 1889 1890 i2c_mark_adapter_suspended(&i2c_dev->adapter); 1891 1892 if (!pm_runtime_status_suspended(dev)) { 1893 err = tegra_i2c_runtime_suspend(dev); 1894 if (err) 1895 return err; 1896 } 1897 1898 return 0; 1899 } 1900 1901 static int __maybe_unused tegra_i2c_resume(struct device *dev) 1902 { 1903 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 1904 int err; 1905 1906 /* 1907 * We need to ensure that clocks are enabled so that registers can be 1908 * restored in tegra_i2c_init(). 1909 */ 1910 err = tegra_i2c_runtime_resume(dev); 1911 if (err) 1912 return err; 1913 1914 err = tegra_i2c_init(i2c_dev); 1915 if (err) 1916 return err; 1917 1918 /* 1919 * In case we are runtime suspended, disable clocks again so that we 1920 * don't unbalance the clock reference counts during the next runtime 1921 * resume transition. 1922 */ 1923 if (pm_runtime_status_suspended(dev)) { 1924 err = tegra_i2c_runtime_suspend(dev); 1925 if (err) 1926 return err; 1927 } 1928 1929 i2c_mark_adapter_resumed(&i2c_dev->adapter); 1930 1931 return 0; 1932 } 1933 1934 static const struct dev_pm_ops tegra_i2c_pm = { 1935 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume) 1936 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume, 1937 NULL) 1938 }; 1939 1940 static const struct acpi_device_id tegra_i2c_acpi_match[] = { 1941 {.id = "NVDA0101", .driver_data = (kernel_ulong_t)&tegra210_i2c_hw}, 1942 {.id = "NVDA0201", .driver_data = (kernel_ulong_t)&tegra186_i2c_hw}, 1943 {.id = "NVDA0301", .driver_data = (kernel_ulong_t)&tegra194_i2c_hw}, 1944 { } 1945 }; 1946 MODULE_DEVICE_TABLE(acpi, tegra_i2c_acpi_match); 1947 1948 static struct platform_driver tegra_i2c_driver = { 1949 .probe = tegra_i2c_probe, 1950 .remove = tegra_i2c_remove, 1951 .driver = { 1952 .name = "tegra-i2c", 1953 .of_match_table = tegra_i2c_of_match, 1954 .acpi_match_table = tegra_i2c_acpi_match, 1955 .pm = &tegra_i2c_pm, 1956 }, 1957 }; 1958 module_platform_driver(tegra_i2c_driver); 1959 1960 MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver"); 1961 MODULE_AUTHOR("Colin Cross"); 1962 MODULE_LICENSE("GPL v2"); 1963