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_DEBOUNCE_CNT GENMASK(14, 12) 34 #define I2C_CNFG_PACKET_MODE_EN BIT(10) 35 #define I2C_CNFG_NEW_MASTER_FSM BIT(11) 36 #define I2C_CNFG_MULTI_MASTER_MODE BIT(17) 37 38 #define I2C_SL_CNFG_NACK BIT(1) 39 #define I2C_SL_CNFG_NEWSL BIT(2) 40 41 #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1) 42 #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0) 43 #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5) 44 #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2) 45 46 #define I2C_FIFO_STATUS_TX GENMASK(7, 4) 47 #define I2C_FIFO_STATUS_RX GENMASK(3, 0) 48 49 #define I2C_INT_BUS_CLR_DONE BIT(11) 50 #define I2C_INT_PACKET_XFER_COMPLETE BIT(7) 51 #define I2C_INT_NO_ACK BIT(3) 52 #define I2C_INT_ARBITRATION_LOST BIT(2) 53 #define I2C_INT_TX_FIFO_DATA_REQ BIT(1) 54 #define I2C_INT_RX_FIFO_DATA_REQ BIT(0) 55 56 #define I2C_CLK_DIVISOR_STD_FAST_MODE GENMASK(31, 16) 57 #define I2C_CLK_DIVISOR_HSMODE GENMASK(15, 0) 58 59 #define DVC_CTRL_REG1 0x000 60 #define DVC_CTRL_REG1_INTR_EN BIT(10) 61 #define DVC_CTRL_REG3 0x008 62 #define DVC_CTRL_REG3_SW_PROG BIT(26) 63 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30) 64 #define DVC_STATUS 0x00c 65 #define DVC_STATUS_I2C_DONE_INTR BIT(30) 66 67 #define I2C_ERR_NONE 0x00 68 #define I2C_ERR_NO_ACK BIT(0) 69 #define I2C_ERR_ARBITRATION_LOST BIT(1) 70 #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2) 71 #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3) 72 73 #define PACKET_HEADER0_HEADER_SIZE GENMASK(29, 28) 74 #define PACKET_HEADER0_PACKET_ID GENMASK(23, 16) 75 #define PACKET_HEADER0_CONT_ID GENMASK(15, 12) 76 #define PACKET_HEADER0_PROTOCOL GENMASK(7, 4) 77 #define PACKET_HEADER0_PROTOCOL_I2C 1 78 79 #define I2C_HEADER_HS_MODE BIT(22) 80 #define I2C_HEADER_CONT_ON_NAK BIT(21) 81 #define I2C_HEADER_READ BIT(19) 82 #define I2C_HEADER_10BIT_ADDR BIT(18) 83 #define I2C_HEADER_IE_ENABLE BIT(17) 84 #define I2C_HEADER_REPEAT_START BIT(16) 85 #define I2C_HEADER_CONTINUE_XFER BIT(15) 86 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1 87 88 #define I2C_BC_SCLK_THRESHOLD GENMASK(23, 16) 89 #define I2C_BC_STOP_COND BIT(2) 90 #define I2C_BC_TERMINATE BIT(1) 91 #define I2C_BC_ENABLE BIT(0) 92 93 #define I2C_BC_STATUS BIT(0) 94 95 #define I2C_MSTR_CONFIG_LOAD BIT(0) 96 97 #define I2C_MST_CORE_CLKEN_OVR BIT(0) 98 99 #define I2C_INTERFACE_TIMING_THIGH GENMASK(13, 8) 100 #define I2C_INTERFACE_TIMING_TLOW GENMASK(5, 0) 101 #define I2C_INTERFACE_TIMING_TBUF GENMASK(29, 24) 102 #define I2C_INTERFACE_TIMING_TSU_STO GENMASK(21, 16) 103 #define I2C_INTERFACE_TIMING_THD_STA GENMASK(13, 8) 104 #define I2C_INTERFACE_TIMING_TSU_STA GENMASK(5, 0) 105 106 #define I2C_HS_INTERFACE_TIMING_THIGH GENMASK(13, 8) 107 #define I2C_HS_INTERFACE_TIMING_TLOW GENMASK(5, 0) 108 #define I2C_HS_INTERFACE_TIMING_TSU_STO GENMASK(21, 16) 109 #define I2C_HS_INTERFACE_TIMING_THD_STA GENMASK(13, 8) 110 #define I2C_HS_INTERFACE_TIMING_TSU_STA GENMASK(5, 0) 111 112 #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0) 113 #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1) 114 #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4) 115 #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16) 116 117 #define I2C_MST_FIFO_STATUS_TX GENMASK(23, 16) 118 #define I2C_MST_FIFO_STATUS_RX GENMASK(7, 0) 119 120 #define I2C_SW_MUTEX_REQUEST GENMASK(3, 0) 121 #define I2C_SW_MUTEX_GRANT GENMASK(7, 4) 122 #define I2C_SW_MUTEX_ID_CCPLEX 9 123 124 /* SW mutex acquire timeout value in microseconds. */ 125 #define I2C_SW_MUTEX_TIMEOUT_US (25 * USEC_PER_MSEC) 126 127 /* configuration load timeout in microseconds */ 128 #define I2C_CONFIG_LOAD_TIMEOUT 1000000 129 130 /* packet header size in bytes */ 131 #define I2C_PACKET_HEADER_SIZE 12 132 133 /* 134 * I2C Controller will use PIO mode for transfers up to 32 bytes in order to 135 * avoid DMA overhead, otherwise external APB DMA controller will be used. 136 * Note that the actual MAX PIO length is 20 bytes because 32 bytes include 137 * I2C_PACKET_HEADER_SIZE. 138 */ 139 #define I2C_PIO_MODE_PREFERRED_LEN 32 140 141 struct tegra_i2c_regs { 142 unsigned int cnfg; 143 unsigned int status; 144 unsigned int sl_cnfg; 145 unsigned int sl_addr1; 146 unsigned int sl_addr2; 147 unsigned int tlow_sext; 148 unsigned int tx_fifo; 149 unsigned int rx_fifo; 150 unsigned int packet_transfer_status; 151 unsigned int fifo_control; 152 unsigned int fifo_status; 153 unsigned int int_mask; 154 unsigned int int_status; 155 unsigned int clk_divisor; 156 unsigned int bus_clear_cnfg; 157 unsigned int bus_clear_status; 158 unsigned int config_load; 159 unsigned int clken_override; 160 unsigned int interface_timing_0; 161 unsigned int interface_timing_1; 162 unsigned int hs_interface_timing_0; 163 unsigned int hs_interface_timing_1; 164 unsigned int master_reset_cntrl; 165 unsigned int mst_fifo_control; 166 unsigned int mst_fifo_status; 167 unsigned int fairness_arb; 168 unsigned int sw_mutex; 169 }; 170 171 static const struct tegra_i2c_regs tegra20_i2c_regs = { 172 .cnfg = 0x000, 173 .status = 0x01c, 174 .sl_cnfg = 0x020, 175 .sl_addr1 = 0x02c, 176 .sl_addr2 = 0x030, 177 .tx_fifo = 0x050, 178 .rx_fifo = 0x054, 179 .packet_transfer_status = 0x058, 180 .fifo_control = 0x05c, 181 .fifo_status = 0x060, 182 .int_mask = 0x064, 183 .int_status = 0x068, 184 .clk_divisor = 0x06c, 185 .bus_clear_cnfg = 0x084, 186 .bus_clear_status = 0x088, 187 .config_load = 0x08c, 188 .clken_override = 0x090, 189 .interface_timing_0 = 0x094, 190 .interface_timing_1 = 0x098, 191 .hs_interface_timing_0 = 0x09c, 192 .hs_interface_timing_1 = 0x0a0, 193 .master_reset_cntrl = 0x0a8, 194 .mst_fifo_control = 0x0b4, 195 .mst_fifo_status = 0x0b8, 196 }; 197 198 #if IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) 199 static const struct tegra_i2c_regs tegra20_dvc_i2c_regs = { 200 .cnfg = 0x040, 201 .status = 0x05c, 202 .tx_fifo = 0x060, 203 .rx_fifo = 0x064, 204 .packet_transfer_status = 0x068, 205 .fifo_control = 0x06c, 206 .fifo_status = 0x070, 207 .int_mask = 0x074, 208 .int_status = 0x078, 209 .clk_divisor = 0x07c, 210 .bus_clear_cnfg = 0x094, 211 .bus_clear_status = 0x098, 212 .config_load = 0x09c, 213 .clken_override = 0x0a0, 214 .interface_timing_0 = 0x0a4, 215 .interface_timing_1 = 0x0a8, 216 .hs_interface_timing_0 = 0x0ac, 217 .hs_interface_timing_1 = 0x0b0, 218 .master_reset_cntrl = 0x0b8, 219 .mst_fifo_control = 0x0c4, 220 .mst_fifo_status = 0x0c8, 221 }; 222 #endif 223 224 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 225 static const struct tegra_i2c_regs tegra210_vi_i2c_regs = { 226 .cnfg = 0x0c00, 227 .status = 0x0c70, 228 .tlow_sext = 0x0cd0, 229 .tx_fifo = 0x0d40, 230 .rx_fifo = 0x0d50, 231 .packet_transfer_status = 0x0d60, 232 .fifo_control = 0x0d70, 233 .fifo_status = 0x0d80, 234 .int_mask = 0x0d90, 235 .int_status = 0x0da0, 236 .clk_divisor = 0x0db0, 237 .bus_clear_cnfg = 0x0e10, 238 .bus_clear_status = 0x0e20, 239 .config_load = 0x0e30, 240 .clken_override = 0x0e40, 241 .interface_timing_0 = 0x0e50, 242 .interface_timing_1 = 0x0e60, 243 .hs_interface_timing_0 = 0x0e70, 244 .hs_interface_timing_1 = 0x0e80, 245 .master_reset_cntrl = 0x0ea0, 246 .mst_fifo_control = 0x0ed0, 247 .mst_fifo_status = 0x0ee0, 248 }; 249 #endif 250 251 static const struct tegra_i2c_regs tegra264_i2c_regs = { 252 .cnfg = 0x000, 253 .status = 0x01c, 254 .sl_cnfg = 0x020, 255 .sl_addr1 = 0x02c, 256 .sl_addr2 = 0x030, 257 .tx_fifo = 0x050, 258 .rx_fifo = 0x054, 259 .packet_transfer_status = 0x058, 260 .fifo_control = 0x05c, 261 .fifo_status = 0x060, 262 .int_mask = 0x064, 263 .int_status = 0x068, 264 .clk_divisor = 0x06c, 265 .bus_clear_cnfg = 0x084, 266 .bus_clear_status = 0x088, 267 .config_load = 0x08c, 268 .clken_override = 0x090, 269 .interface_timing_0 = 0x094, 270 .interface_timing_1 = 0x098, 271 .hs_interface_timing_0 = 0x09c, 272 .hs_interface_timing_1 = 0x0a0, 273 .master_reset_cntrl = 0x0a8, 274 .mst_fifo_control = 0x0b4, 275 .mst_fifo_status = 0x0b8, 276 .fairness_arb = 0x0e8, 277 .sw_mutex = 0x0ec, 278 }; 279 280 static const struct tegra_i2c_regs tegra410_i2c_regs = { 281 .cnfg = 0x000, 282 .status = 0x01c, 283 .sl_cnfg = 0x020, 284 .sl_addr1 = 0x02c, 285 .sl_addr2 = 0x030, 286 .tx_fifo = 0x054, 287 .rx_fifo = 0x058, 288 .packet_transfer_status = 0x05c, 289 .fifo_control = 0x060, 290 .fifo_status = 0x064, 291 .int_mask = 0x068, 292 .int_status = 0x06c, 293 .clk_divisor = 0x070, 294 .bus_clear_cnfg = 0x088, 295 .bus_clear_status = 0x08c, 296 .config_load = 0x090, 297 .clken_override = 0x094, 298 .interface_timing_0 = 0x098, 299 .interface_timing_1 = 0x09c, 300 .hs_interface_timing_0 = 0x0a0, 301 .hs_interface_timing_1 = 0x0a4, 302 .master_reset_cntrl = 0x0ac, 303 .mst_fifo_control = 0x0b8, 304 .mst_fifo_status = 0x0bc, 305 .fairness_arb = 0x0ec, 306 .sw_mutex = 0x0f0, 307 }; 308 309 /* 310 * msg_end_type: The bus control which needs to be sent at end of transfer. 311 * @MSG_END_STOP: Send stop pulse. 312 * @MSG_END_REPEAT_START: Send repeat-start. 313 * @MSG_END_CONTINUE: Don't send stop or repeat-start. 314 */ 315 enum msg_end_type { 316 MSG_END_STOP, 317 MSG_END_REPEAT_START, 318 MSG_END_CONTINUE, 319 }; 320 321 /* 322 * tegra_i2c_variant: Identifies the variant of I2C controller. 323 * @TEGRA_I2C_VARIANT_DEFAULT: Identifies the default I2C controller. 324 * @TEGRA_I2C_VARIANT_DVC: Identifies the DVC I2C controller, has a different register layout. 325 * @TEGRA_I2C_VARIANT_VI: Identifies the VI I2C controller, has a different register layout. 326 */ 327 enum tegra_i2c_variant { 328 TEGRA_I2C_VARIANT_DEFAULT, 329 TEGRA_I2C_VARIANT_DVC, 330 TEGRA_I2C_VARIANT_VI, 331 }; 332 333 /** 334 * struct tegra_i2c_hw_feature : per hardware generation features 335 * @has_continue_xfer_support: continue-transfer supported 336 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer 337 * completion interrupt on per packet basis. 338 * @has_config_load_reg: Has the config load register to load the new 339 * configuration. 340 * @clk_divisor_hs_mode: Clock divisor in HS mode. 341 * @clk_divisor_std_mode: Clock divisor in standard mode. It is 342 * applicable if there is no fast clock source i.e. single clock 343 * source. 344 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is 345 * applicable if there is no fast clock source i.e. single clock 346 * source. 347 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is 348 * applicable if there is no fast clock source (i.e. single 349 * clock source). 350 * @has_multi_master_mode: The I2C controller supports running in single-master 351 * or multi-master mode. 352 * @has_slcg_override_reg: The I2C controller supports a register that 353 * overrides the second level clock gating. 354 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that 355 * provides additional features and allows for longer messages to 356 * be transferred in one go. 357 * @has_mst_reset: The I2C controller contains MASTER_RESET_CTRL register which 358 * provides an alternative to controller reset when configured as 359 * I2C master 360 * @quirks: I2C adapter quirks for limiting write/read transfer size and not 361 * allowing 0 length transfers. 362 * @supports_bus_clear: Bus Clear support to recover from bus hang during 363 * SDA stuck low from device for some unknown reasons. 364 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip. 365 * @tlow_std_mode: Low period of the clock in standard mode. 366 * @thigh_std_mode: High period of the clock in standard mode. 367 * @tlow_fast_mode: Low period of the clock in fast mode. 368 * @thigh_fast_mode: High period of the clock in fast mode. 369 * @tlow_fastplus_mode: Low period of the clock in fast-plus mode. 370 * @thigh_fastplus_mode: High period of the clock in fast-plus mode. 371 * @tlow_hs_mode: Low period of the clock in HS mode. 372 * @thigh_hs_mode: High period of the clock in HS mode. 373 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions 374 * in standard mode. 375 * @setup_hold_time_fast_mode: Setup and hold time for start and stop 376 * conditions in fast mode. 377 * @setup_hold_time_fastplus_mode: Setup and hold time for start and stop 378 * conditions in fast-plus mode. 379 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions 380 * in HS mode. 381 * @has_interface_timing_reg: Has interface timing register to program the tuned 382 * timing settings. 383 * @enable_hs_mode_support: Enable support for high speed (HS) mode transfers. 384 * @has_mutex: Has mutex register for mutual exclusion with other firmwares or VMs. 385 * @has_fairarb_reg: Has fairness arbitration register for SMBUS/MCTP support. 386 * @variant: This represents the I2C controller variant. 387 * @regs: Register offsets for the specific SoC variant. 388 */ 389 struct tegra_i2c_hw_feature { 390 bool has_continue_xfer_support; 391 bool has_per_pkt_xfer_complete_irq; 392 bool has_config_load_reg; 393 u32 clk_divisor_hs_mode; 394 u32 clk_divisor_std_mode; 395 u32 clk_divisor_fast_mode; 396 u32 clk_divisor_fast_plus_mode; 397 bool has_multi_master_mode; 398 bool has_slcg_override_reg; 399 bool has_mst_fifo; 400 bool has_mst_reset; 401 const struct i2c_adapter_quirks *quirks; 402 bool supports_bus_clear; 403 bool has_apb_dma; 404 u32 tlow_std_mode; 405 u32 thigh_std_mode; 406 u32 tlow_fast_mode; 407 u32 thigh_fast_mode; 408 u32 tlow_fastplus_mode; 409 u32 thigh_fastplus_mode; 410 u32 tlow_hs_mode; 411 u32 thigh_hs_mode; 412 u32 setup_hold_time_std_mode; 413 u32 setup_hold_time_fast_mode; 414 u32 setup_hold_time_fastplus_mode; 415 u32 setup_hold_time_hs_mode; 416 bool has_interface_timing_reg; 417 bool enable_hs_mode_support; 418 bool has_mutex; 419 bool has_fairarb_reg; 420 enum tegra_i2c_variant variant; 421 const struct tegra_i2c_regs *regs; 422 }; 423 424 /** 425 * struct tegra_i2c_dev - per device I2C context 426 * @dev: device reference for power management 427 * @hw: Tegra I2C HW feature 428 * @adapter: core I2C layer adapter information 429 * @div_clk: clock reference for div clock of I2C controller 430 * @clocks: array of I2C controller clocks 431 * @nclocks: number of clocks in the array 432 * @base: ioremapped registers cookie 433 * @base_phys: physical base address of the I2C controller 434 * @cont_id: I2C controller ID, used for packet header 435 * @irq: IRQ number of transfer complete interrupt 436 * @msg_complete: transfer completion notifier 437 * @msg_buf_remaining: size of unsent data in the message buffer 438 * @msg_len: length of message in current transfer 439 * @msg_err: error code for completed message 440 * @msg_buf: pointer to current message data 441 * @msg_read: indicates that the transfer is a read access 442 * @timings: i2c timings information like bus frequency 443 * @multimaster_mode: indicates that I2C controller is in multi-master mode 444 * @is_mctp: indicates that the I2C controller is used as an MCTP controller 445 * @dma_chan: DMA channel 446 * @dma_phys: handle to DMA resources 447 * @dma_buf: pointer to allocated DMA buffer 448 * @dma_buf_size: DMA buffer size 449 * @dma_dev: DMA device used for transfers 450 * @dma_mode: indicates active DMA transfer 451 * @dma_complete: DMA completion notifier 452 * @atomic_mode: indicates active atomic transfer 453 */ 454 struct tegra_i2c_dev { 455 struct device *dev; 456 struct i2c_adapter adapter; 457 458 const struct tegra_i2c_hw_feature *hw; 459 unsigned int cont_id; 460 unsigned int irq; 461 462 phys_addr_t base_phys; 463 void __iomem *base; 464 465 struct clk_bulk_data clocks[2]; 466 unsigned int nclocks; 467 468 struct clk *div_clk; 469 struct i2c_timings timings; 470 471 struct completion msg_complete; 472 size_t msg_buf_remaining; 473 unsigned int msg_len; 474 int msg_err; 475 u8 *msg_buf; 476 477 struct completion dma_complete; 478 struct dma_chan *dma_chan; 479 unsigned int dma_buf_size; 480 struct device *dma_dev; 481 dma_addr_t dma_phys; 482 void *dma_buf; 483 484 bool multimaster_mode; 485 bool is_mctp; 486 bool atomic_mode; 487 bool dma_mode; 488 bool msg_read; 489 }; 490 491 #define IS_DVC(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) && \ 492 (dev)->hw->variant == TEGRA_I2C_VARIANT_DVC) 493 #define IS_VI(dev) (IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) && \ 494 (dev)->hw->variant == TEGRA_I2C_VARIANT_VI) 495 496 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, 497 unsigned int reg) 498 { 499 writel_relaxed(val, i2c_dev->base + reg); 500 } 501 502 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg) 503 { 504 return readl_relaxed(i2c_dev->base + reg); 505 } 506 507 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, unsigned int reg) 508 { 509 writel_relaxed(val, i2c_dev->base + reg); 510 511 /* read back register to make sure that register writes completed */ 512 if (reg != i2c_dev->hw->regs->tx_fifo) 513 readl_relaxed(i2c_dev->base + reg); 514 else if (IS_VI(i2c_dev)) 515 readl_relaxed(i2c_dev->base + i2c_dev->hw->regs->int_status); 516 } 517 518 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned int reg) 519 { 520 return readl_relaxed(i2c_dev->base + reg); 521 } 522 523 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data, 524 unsigned int reg, unsigned int len) 525 { 526 writesl(i2c_dev->base + reg, data, len); 527 } 528 529 static void i2c_writesl_vi(struct tegra_i2c_dev *i2c_dev, void *data, 530 unsigned int reg, unsigned int len) 531 { 532 u32 *data32 = data; 533 534 /* 535 * VI I2C controller has known hardware bug where writes get stuck 536 * when immediate multiple writes happen to TX_FIFO register. 537 * Recommended software work around is to read I2C register after 538 * each write to TX_FIFO register to flush out the data. 539 */ 540 while (len--) 541 i2c_writel(i2c_dev, *data32++, reg); 542 } 543 544 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data, 545 unsigned int reg, unsigned int len) 546 { 547 readsl(i2c_dev->base + reg, data, len); 548 } 549 550 static bool tegra_i2c_mutex_acquired(struct tegra_i2c_dev *i2c_dev) 551 { 552 unsigned int reg = i2c_dev->hw->regs->sw_mutex; 553 u32 val, id; 554 555 val = readl(i2c_dev->base + reg); 556 id = FIELD_GET(I2C_SW_MUTEX_GRANT, val); 557 558 return id == I2C_SW_MUTEX_ID_CCPLEX; 559 } 560 561 static bool tegra_i2c_mutex_trylock(struct tegra_i2c_dev *i2c_dev) 562 { 563 unsigned int reg = i2c_dev->hw->regs->sw_mutex; 564 u32 val, id; 565 566 val = readl(i2c_dev->base + reg); 567 id = FIELD_GET(I2C_SW_MUTEX_GRANT, val); 568 if (id != 0 && id != I2C_SW_MUTEX_ID_CCPLEX) 569 return false; 570 571 val = FIELD_PREP(I2C_SW_MUTEX_REQUEST, I2C_SW_MUTEX_ID_CCPLEX); 572 writel(val, i2c_dev->base + reg); 573 574 return tegra_i2c_mutex_acquired(i2c_dev); 575 } 576 577 static int tegra_i2c_mutex_lock(struct tegra_i2c_dev *i2c_dev) 578 { 579 bool locked; 580 int ret; 581 582 if (!i2c_dev->hw->has_mutex) 583 return 0; 584 585 if (i2c_dev->atomic_mode) 586 ret = read_poll_timeout_atomic(tegra_i2c_mutex_trylock, locked, locked, 587 USEC_PER_MSEC, I2C_SW_MUTEX_TIMEOUT_US, 588 false, i2c_dev); 589 else 590 ret = read_poll_timeout(tegra_i2c_mutex_trylock, locked, locked, USEC_PER_MSEC, 591 I2C_SW_MUTEX_TIMEOUT_US, false, i2c_dev); 592 593 if (ret) 594 dev_warn(i2c_dev->dev, "failed to acquire mutex\n"); 595 596 return ret; 597 } 598 599 static void tegra_i2c_mutex_unlock(struct tegra_i2c_dev *i2c_dev) 600 { 601 unsigned int reg = i2c_dev->hw->regs->sw_mutex; 602 u32 val, id; 603 604 if (!i2c_dev->hw->has_mutex) 605 return; 606 607 val = readl(i2c_dev->base + reg); 608 609 id = FIELD_GET(I2C_SW_MUTEX_GRANT, val); 610 if (WARN(id && id != I2C_SW_MUTEX_ID_CCPLEX, 611 "unable to unlock mutex, mutex is owned by: %u\n", id)) 612 return; 613 614 writel(0, i2c_dev->base + reg); 615 } 616 617 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) 618 { 619 u32 int_mask; 620 621 int_mask = i2c_readl(i2c_dev, i2c_dev->hw->regs->int_mask) & ~mask; 622 i2c_writel(i2c_dev, int_mask, i2c_dev->hw->regs->int_mask); 623 } 624 625 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) 626 { 627 u32 int_mask; 628 629 int_mask = i2c_readl(i2c_dev, i2c_dev->hw->regs->int_mask) | mask; 630 i2c_writel(i2c_dev, int_mask, i2c_dev->hw->regs->int_mask); 631 } 632 633 static void tegra_i2c_dma_complete(void *args) 634 { 635 struct tegra_i2c_dev *i2c_dev = args; 636 637 complete(&i2c_dev->dma_complete); 638 } 639 640 static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len) 641 { 642 struct dma_async_tx_descriptor *dma_desc; 643 enum dma_transfer_direction dir; 644 645 dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len); 646 647 reinit_completion(&i2c_dev->dma_complete); 648 649 dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 650 651 dma_desc = dmaengine_prep_slave_single(i2c_dev->dma_chan, i2c_dev->dma_phys, 652 len, dir, DMA_PREP_INTERRUPT | 653 DMA_CTRL_ACK); 654 if (!dma_desc) { 655 dev_err(i2c_dev->dev, "failed to get %s DMA descriptor\n", 656 i2c_dev->msg_read ? "RX" : "TX"); 657 return -EINVAL; 658 } 659 660 dma_desc->callback = tegra_i2c_dma_complete; 661 dma_desc->callback_param = i2c_dev; 662 663 dmaengine_submit(dma_desc); 664 dma_async_issue_pending(i2c_dev->dma_chan); 665 666 return 0; 667 } 668 669 static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev) 670 { 671 if (i2c_dev->dma_buf) { 672 dma_free_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size, 673 i2c_dev->dma_buf, i2c_dev->dma_phys); 674 i2c_dev->dma_buf = NULL; 675 } 676 677 if (i2c_dev->dma_chan) { 678 dma_release_channel(i2c_dev->dma_chan); 679 i2c_dev->dma_chan = NULL; 680 } 681 } 682 683 static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) 684 { 685 dma_addr_t dma_phys; 686 u32 *dma_buf; 687 int err; 688 689 if (IS_VI(i2c_dev)) 690 return 0; 691 692 if (!of_property_present(i2c_dev->dev->of_node, "dmas")) { 693 dev_dbg(i2c_dev->dev, "DMA not available, falling back to PIO\n"); 694 return 0; 695 } 696 697 if (i2c_dev->hw->has_apb_dma) { 698 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) { 699 dev_dbg(i2c_dev->dev, "APB DMA support not enabled\n"); 700 return 0; 701 } 702 } else if (!IS_ENABLED(CONFIG_TEGRA186_GPC_DMA)) { 703 dev_dbg(i2c_dev->dev, "GPC DMA support not enabled\n"); 704 return 0; 705 } 706 707 /* 708 * The same channel will be used for both RX and TX. 709 * Keeping the name as "tx" for backward compatibility 710 * with existing devicetrees. 711 */ 712 i2c_dev->dma_chan = dma_request_chan(i2c_dev->dev, "tx"); 713 if (IS_ERR(i2c_dev->dma_chan)) { 714 err = PTR_ERR(i2c_dev->dma_chan); 715 i2c_dev->dma_chan = NULL; 716 goto err_out; 717 } 718 719 i2c_dev->dma_dev = dmaengine_get_dma_device(i2c_dev->dma_chan); 720 i2c_dev->dma_buf_size = i2c_dev->hw->quirks->max_write_len + 721 I2C_PACKET_HEADER_SIZE; 722 723 dma_buf = dma_alloc_coherent(i2c_dev->dma_dev, i2c_dev->dma_buf_size, 724 &dma_phys, GFP_KERNEL | __GFP_NOWARN); 725 if (!dma_buf) { 726 err = dev_err_probe(i2c_dev->dev, -ENOMEM, 727 "failed to allocate DMA buffer\n"); 728 goto err_out; 729 } 730 731 i2c_dev->dma_buf = dma_buf; 732 i2c_dev->dma_phys = dma_phys; 733 734 return 0; 735 736 err_out: 737 tegra_i2c_release_dma(i2c_dev); 738 if (err != -EPROBE_DEFER) { 739 dev_err(i2c_dev->dev, "cannot use DMA, falling back to PIO\n"); 740 return 0; 741 } 742 743 return err; 744 } 745 746 /* 747 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller) 748 * block. This block is identical to the rest of the I2C blocks, except that 749 * it only supports master mode, it has registers moved around, and it needs 750 * some extra init to get it into I2C mode. The register moves are handled 751 * by i2c_readl() and i2c_writel(). 752 */ 753 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev) 754 { 755 u32 val; 756 757 val = dvc_readl(i2c_dev, DVC_CTRL_REG3); 758 val |= DVC_CTRL_REG3_SW_PROG; 759 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN; 760 dvc_writel(i2c_dev, val, DVC_CTRL_REG3); 761 762 val = dvc_readl(i2c_dev, DVC_CTRL_REG1); 763 val |= DVC_CTRL_REG1_INTR_EN; 764 dvc_writel(i2c_dev, val, DVC_CTRL_REG1); 765 } 766 767 static void tegra_i2c_vi_init(struct tegra_i2c_dev *i2c_dev) 768 { 769 u32 value; 770 771 value = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, 2) | 772 FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, 4); 773 i2c_writel(i2c_dev, value, i2c_dev->hw->regs->interface_timing_0); 774 775 value = FIELD_PREP(I2C_INTERFACE_TIMING_TBUF, 4) | 776 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STO, 7) | 777 FIELD_PREP(I2C_INTERFACE_TIMING_THD_STA, 4) | 778 FIELD_PREP(I2C_INTERFACE_TIMING_TSU_STA, 4); 779 i2c_writel(i2c_dev, value, i2c_dev->hw->regs->interface_timing_1); 780 781 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, 3) | 782 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, 8); 783 i2c_writel(i2c_dev, value, i2c_dev->hw->regs->hs_interface_timing_0); 784 785 value = FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STO, 11) | 786 FIELD_PREP(I2C_HS_INTERFACE_TIMING_THD_STA, 11) | 787 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TSU_STA, 11); 788 i2c_writel(i2c_dev, value, i2c_dev->hw->regs->hs_interface_timing_1); 789 790 value = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND; 791 i2c_writel(i2c_dev, value, i2c_dev->hw->regs->bus_clear_cnfg); 792 793 i2c_writel(i2c_dev, 0x0, i2c_dev->hw->regs->tlow_sext); 794 } 795 796 static int tegra_i2c_poll_register(struct tegra_i2c_dev *i2c_dev, 797 u32 reg, u32 mask, u32 delay_us, 798 u32 timeout_us) 799 { 800 void __iomem *addr = i2c_dev->base + reg; 801 u32 val; 802 803 if (!i2c_dev->atomic_mode) 804 return readl_relaxed_poll_timeout(addr, val, !(val & mask), 805 delay_us, timeout_us); 806 807 return readl_relaxed_poll_timeout_atomic(addr, val, !(val & mask), 808 delay_us, timeout_us); 809 } 810 811 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev) 812 { 813 u32 mask, val, offset; 814 int err; 815 816 if (i2c_dev->hw->has_mst_fifo) { 817 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH | 818 I2C_MST_FIFO_CONTROL_RX_FLUSH; 819 offset = i2c_dev->hw->regs->mst_fifo_control; 820 } else { 821 mask = I2C_FIFO_CONTROL_TX_FLUSH | 822 I2C_FIFO_CONTROL_RX_FLUSH; 823 offset = i2c_dev->hw->regs->fifo_control; 824 } 825 826 val = i2c_readl(i2c_dev, offset); 827 val |= mask; 828 i2c_writel(i2c_dev, val, offset); 829 830 err = tegra_i2c_poll_register(i2c_dev, offset, mask, 1000, 1000000); 831 if (err) { 832 dev_err(i2c_dev->dev, "failed to flush FIFO\n"); 833 return err; 834 } 835 836 return 0; 837 } 838 839 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev) 840 { 841 int err; 842 843 if (!i2c_dev->hw->has_config_load_reg) 844 return 0; 845 846 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, i2c_dev->hw->regs->config_load); 847 848 err = tegra_i2c_poll_register(i2c_dev, i2c_dev->hw->regs->config_load, 0xffffffff, 849 1000, I2C_CONFIG_LOAD_TIMEOUT); 850 if (err) { 851 dev_err(i2c_dev->dev, "failed to load config\n"); 852 return err; 853 } 854 855 return 0; 856 } 857 858 static int tegra_i2c_master_reset(struct tegra_i2c_dev *i2c_dev) 859 { 860 if (!i2c_dev->hw->has_mst_reset) 861 return -EOPNOTSUPP; 862 863 /* 864 * Writing 1 to I2C_MASTER_RESET_CNTRL will reset all internal state of 865 * Master logic including FIFOs. Clear this bit to 0 for normal operation. 866 * SW needs to wait for 2us after assertion and de-assertion of this soft 867 * reset. 868 */ 869 i2c_writel(i2c_dev, 0x1, i2c_dev->hw->regs->master_reset_cntrl); 870 fsleep(2); 871 872 i2c_writel(i2c_dev, 0x0, i2c_dev->hw->regs->master_reset_cntrl); 873 fsleep(2); 874 875 return 0; 876 } 877 878 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev) 879 { 880 u32 val, clk_divisor, clk_multiplier, tsu_thd, tlow, thigh, non_hs_mode; 881 u32 max_bus_freq_hz; 882 struct i2c_timings *t = &i2c_dev->timings; 883 int err; 884 885 /* 886 * Reset the controller before initializing it. 887 * In case if device_reset() returns -ENOENT, i.e. when the reset is 888 * not available, the internal software reset will be used if it is 889 * supported by the controller. 890 */ 891 err = device_reset(i2c_dev->dev); 892 if (err == -ENOENT) 893 err = tegra_i2c_master_reset(i2c_dev); 894 895 /* 896 * The reset shouldn't ever fail in practice. The failure will be a 897 * sign of a severe problem that needs to be resolved. Still we don't 898 * want to fail the initialization completely because this may break 899 * kernel boot up since voltage regulators use I2C. Hence, we will 900 * emit a noisy warning on error, which won't stay unnoticed and 901 * won't hose machine entirely. 902 */ 903 WARN_ON_ONCE(err); 904 905 if (IS_DVC(i2c_dev)) 906 tegra_dvc_init(i2c_dev); 907 908 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN | 909 FIELD_PREP(I2C_CNFG_DEBOUNCE_CNT, 2); 910 911 if (i2c_dev->hw->has_multi_master_mode) 912 val |= I2C_CNFG_MULTI_MASTER_MODE; 913 914 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->cnfg); 915 i2c_writel(i2c_dev, 0, i2c_dev->hw->regs->int_mask); 916 917 if (IS_VI(i2c_dev)) 918 tegra_i2c_vi_init(i2c_dev); 919 920 /* Disable fairness arbitration if not an MCTP controller */ 921 if (i2c_dev->hw->has_fairarb_reg && !i2c_dev->is_mctp) 922 i2c_writel(i2c_dev, 0, i2c_dev->hw->regs->fairness_arb); 923 924 if (i2c_dev->hw->enable_hs_mode_support) 925 max_bus_freq_hz = I2C_MAX_HIGH_SPEED_MODE_FREQ; 926 else 927 max_bus_freq_hz = I2C_MAX_FAST_MODE_PLUS_FREQ; 928 929 if (WARN_ON(t->bus_freq_hz > max_bus_freq_hz)) 930 t->bus_freq_hz = max_bus_freq_hz; 931 932 if (t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ) { 933 tlow = i2c_dev->hw->tlow_std_mode; 934 thigh = i2c_dev->hw->thigh_std_mode; 935 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode; 936 non_hs_mode = i2c_dev->hw->clk_divisor_std_mode; 937 } else if (t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ) { 938 tlow = i2c_dev->hw->tlow_fast_mode; 939 thigh = i2c_dev->hw->thigh_fast_mode; 940 tsu_thd = i2c_dev->hw->setup_hold_time_fast_mode; 941 non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode; 942 } else if (t->bus_freq_hz <= I2C_MAX_FAST_MODE_PLUS_FREQ) { 943 tlow = i2c_dev->hw->tlow_fastplus_mode; 944 thigh = i2c_dev->hw->thigh_fastplus_mode; 945 tsu_thd = i2c_dev->hw->setup_hold_time_fastplus_mode; 946 non_hs_mode = i2c_dev->hw->clk_divisor_fast_plus_mode; 947 } else { 948 /* 949 * When using HS mode, i.e. when the bus frequency is greater than fast plus mode, 950 * the non-hs timing registers will be used for sending the master code byte for 951 * transition to HS mode. Configure the non-hs timing registers for Fast Mode to 952 * send the master code byte at 400kHz. 953 */ 954 tlow = i2c_dev->hw->tlow_fast_mode; 955 thigh = i2c_dev->hw->thigh_fast_mode; 956 tsu_thd = i2c_dev->hw->setup_hold_time_fast_mode; 957 non_hs_mode = i2c_dev->hw->clk_divisor_fast_mode; 958 } 959 960 /* make sure clock divisor programmed correctly */ 961 clk_divisor = FIELD_PREP(I2C_CLK_DIVISOR_HSMODE, 962 i2c_dev->hw->clk_divisor_hs_mode) | 963 FIELD_PREP(I2C_CLK_DIVISOR_STD_FAST_MODE, non_hs_mode); 964 i2c_writel(i2c_dev, clk_divisor, i2c_dev->hw->regs->clk_divisor); 965 966 if (i2c_dev->hw->has_interface_timing_reg) { 967 val = FIELD_PREP(I2C_INTERFACE_TIMING_THIGH, thigh) | 968 FIELD_PREP(I2C_INTERFACE_TIMING_TLOW, tlow); 969 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->interface_timing_0); 970 } 971 972 /* 973 * Configure setup and hold times only when tsu_thd is non-zero. 974 * Otherwise, preserve the chip default values. 975 */ 976 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd) 977 i2c_writel(i2c_dev, tsu_thd, i2c_dev->hw->regs->interface_timing_1); 978 979 /* Write HS mode registers. These will get used only for HS mode*/ 980 if (i2c_dev->hw->enable_hs_mode_support) { 981 tlow = i2c_dev->hw->tlow_hs_mode; 982 thigh = i2c_dev->hw->thigh_hs_mode; 983 tsu_thd = i2c_dev->hw->setup_hold_time_hs_mode; 984 985 val = FIELD_PREP(I2C_HS_INTERFACE_TIMING_THIGH, thigh) | 986 FIELD_PREP(I2C_HS_INTERFACE_TIMING_TLOW, tlow); 987 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->hs_interface_timing_0); 988 i2c_writel(i2c_dev, tsu_thd, i2c_dev->hw->regs->hs_interface_timing_1); 989 } 990 991 clk_multiplier = (tlow + thigh + 2) * (non_hs_mode + 1); 992 993 err = clk_set_rate(i2c_dev->div_clk, 994 t->bus_freq_hz * clk_multiplier); 995 if (err) { 996 dev_err(i2c_dev->dev, "failed to set div-clk rate: %d\n", err); 997 return err; 998 } 999 1000 if (!IS_DVC(i2c_dev) && !IS_VI(i2c_dev)) { 1001 u32 sl_cfg = i2c_readl(i2c_dev, i2c_dev->hw->regs->sl_cnfg); 1002 1003 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL; 1004 i2c_writel(i2c_dev, sl_cfg, i2c_dev->hw->regs->sl_cnfg); 1005 i2c_writel(i2c_dev, 0xfc, i2c_dev->hw->regs->sl_addr1); 1006 i2c_writel(i2c_dev, 0x00, i2c_dev->hw->regs->sl_addr2); 1007 } 1008 1009 err = tegra_i2c_flush_fifos(i2c_dev); 1010 if (err) 1011 return err; 1012 1013 if (i2c_dev->multimaster_mode && i2c_dev->hw->has_slcg_override_reg) 1014 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, i2c_dev->hw->regs->clken_override); 1015 1016 err = tegra_i2c_wait_for_config_load(i2c_dev); 1017 if (err) 1018 return err; 1019 1020 return 0; 1021 } 1022 1023 static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev) 1024 { 1025 u32 cnfg; 1026 1027 /* 1028 * NACK interrupt is generated before the I2C controller generates 1029 * the STOP condition on the bus. So, wait for 2 clock periods 1030 * before disabling the controller so that the STOP condition has 1031 * been delivered properly. 1032 */ 1033 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->timings.bus_freq_hz)); 1034 1035 cnfg = i2c_readl(i2c_dev, i2c_dev->hw->regs->cnfg); 1036 if (cnfg & I2C_CNFG_PACKET_MODE_EN) 1037 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, i2c_dev->hw->regs->cnfg); 1038 1039 return tegra_i2c_wait_for_config_load(i2c_dev); 1040 } 1041 1042 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev) 1043 { 1044 size_t buf_remaining = i2c_dev->msg_buf_remaining; 1045 unsigned int words_to_transfer, rx_fifo_avail; 1046 u8 *buf = i2c_dev->msg_buf; 1047 u32 val; 1048 1049 /* 1050 * Catch overflow due to message fully sent before the check for 1051 * RX FIFO availability. 1052 */ 1053 if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining))) 1054 return -EINVAL; 1055 1056 if (i2c_dev->hw->has_mst_fifo) { 1057 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->mst_fifo_status); 1058 rx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_RX, val); 1059 } else { 1060 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->fifo_status); 1061 rx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_RX, val); 1062 } 1063 1064 /* round down to exclude partial word at the end of buffer */ 1065 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; 1066 if (words_to_transfer > rx_fifo_avail) 1067 words_to_transfer = rx_fifo_avail; 1068 1069 i2c_readsl(i2c_dev, buf, i2c_dev->hw->regs->rx_fifo, words_to_transfer); 1070 1071 buf += words_to_transfer * BYTES_PER_FIFO_WORD; 1072 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; 1073 rx_fifo_avail -= words_to_transfer; 1074 1075 /* 1076 * If there is a partial word at the end of buffer, handle it 1077 * manually to prevent overwriting past the end of buffer. 1078 */ 1079 if (rx_fifo_avail > 0 && buf_remaining > 0) { 1080 /* 1081 * buf_remaining > 3 check not needed as rx_fifo_avail == 0 1082 * when (words_to_transfer was > rx_fifo_avail) earlier 1083 * in this function. 1084 */ 1085 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->rx_fifo); 1086 val = cpu_to_le32(val); 1087 memcpy(buf, &val, buf_remaining); 1088 buf_remaining = 0; 1089 rx_fifo_avail--; 1090 } 1091 1092 /* RX FIFO must be drained, otherwise it's an Overflow case. */ 1093 if (WARN_ON_ONCE(rx_fifo_avail)) 1094 return -EINVAL; 1095 1096 i2c_dev->msg_buf_remaining = buf_remaining; 1097 i2c_dev->msg_buf = buf; 1098 1099 return 0; 1100 } 1101 1102 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev) 1103 { 1104 size_t buf_remaining = i2c_dev->msg_buf_remaining; 1105 unsigned int words_to_transfer, tx_fifo_avail; 1106 u8 *buf = i2c_dev->msg_buf; 1107 u32 val; 1108 1109 if (i2c_dev->hw->has_mst_fifo) { 1110 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->mst_fifo_status); 1111 tx_fifo_avail = FIELD_GET(I2C_MST_FIFO_STATUS_TX, val); 1112 } else { 1113 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->fifo_status); 1114 tx_fifo_avail = FIELD_GET(I2C_FIFO_STATUS_TX, val); 1115 } 1116 1117 /* round down to exclude partial word at the end of buffer */ 1118 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; 1119 1120 /* 1121 * This hunk pushes 4 bytes at a time into the TX FIFO. 1122 * 1123 * It's very common to have < 4 bytes, hence there is no word 1124 * to push if we have less than 4 bytes to transfer. 1125 */ 1126 if (words_to_transfer) { 1127 if (words_to_transfer > tx_fifo_avail) 1128 words_to_transfer = tx_fifo_avail; 1129 1130 /* 1131 * Update state before writing to FIFO. Note that this may 1132 * cause us to finish writing all bytes (AKA buf_remaining 1133 * goes to 0), hence we have a potential for an interrupt 1134 * (PACKET_XFER_COMPLETE is not maskable), but GIC interrupt 1135 * is disabled at this point. 1136 */ 1137 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; 1138 tx_fifo_avail -= words_to_transfer; 1139 1140 i2c_dev->msg_buf_remaining = buf_remaining; 1141 i2c_dev->msg_buf = buf + words_to_transfer * BYTES_PER_FIFO_WORD; 1142 1143 if (IS_VI(i2c_dev)) 1144 i2c_writesl_vi(i2c_dev, buf, i2c_dev->hw->regs->tx_fifo, words_to_transfer); 1145 else 1146 i2c_writesl(i2c_dev, buf, i2c_dev->hw->regs->tx_fifo, words_to_transfer); 1147 1148 buf += words_to_transfer * BYTES_PER_FIFO_WORD; 1149 } 1150 1151 /* 1152 * If there is a partial word at the end of buffer, handle it manually 1153 * to prevent reading past the end of buffer, which could cross a page 1154 * boundary and fault. 1155 */ 1156 if (tx_fifo_avail > 0 && buf_remaining > 0) { 1157 /* 1158 * buf_remaining > 3 check not needed as tx_fifo_avail == 0 1159 * when (words_to_transfer was > tx_fifo_avail) earlier 1160 * in this function for non-zero words_to_transfer. 1161 */ 1162 memcpy(&val, buf, buf_remaining); 1163 val = le32_to_cpu(val); 1164 1165 i2c_dev->msg_buf_remaining = 0; 1166 i2c_dev->msg_buf = NULL; 1167 1168 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->tx_fifo); 1169 } 1170 1171 return 0; 1172 } 1173 1174 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id) 1175 { 1176 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; 1177 struct tegra_i2c_dev *i2c_dev = dev_id; 1178 u32 status; 1179 1180 status = i2c_readl(i2c_dev, i2c_dev->hw->regs->int_status); 1181 1182 if (status == 0) { 1183 dev_warn(i2c_dev->dev, "IRQ status 0 %08x %08x %08x\n", 1184 i2c_readl(i2c_dev, i2c_dev->hw->regs->packet_transfer_status), 1185 i2c_readl(i2c_dev, i2c_dev->hw->regs->status), 1186 i2c_readl(i2c_dev, i2c_dev->hw->regs->cnfg)); 1187 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; 1188 goto err; 1189 } 1190 1191 if (status & status_err) { 1192 tegra_i2c_disable_packet_mode(i2c_dev); 1193 if (status & I2C_INT_NO_ACK) 1194 i2c_dev->msg_err |= I2C_ERR_NO_ACK; 1195 if (status & I2C_INT_ARBITRATION_LOST) 1196 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST; 1197 goto err; 1198 } 1199 1200 /* 1201 * I2C transfer is terminated during the bus clear, so skip 1202 * processing the other interrupts. 1203 */ 1204 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE)) 1205 goto err; 1206 1207 if (!i2c_dev->dma_mode) { 1208 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) { 1209 if (tegra_i2c_empty_rx_fifo(i2c_dev)) { 1210 /* 1211 * Overflow error condition: message fully sent, 1212 * with no XFER_COMPLETE interrupt but hardware 1213 * asks to transfer more. 1214 */ 1215 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW; 1216 goto err; 1217 } 1218 } 1219 1220 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) { 1221 if (i2c_dev->msg_buf_remaining) 1222 tegra_i2c_fill_tx_fifo(i2c_dev); 1223 else 1224 tegra_i2c_mask_irq(i2c_dev, 1225 I2C_INT_TX_FIFO_DATA_REQ); 1226 } 1227 } 1228 1229 i2c_writel(i2c_dev, status, i2c_dev->hw->regs->int_status); 1230 if (IS_DVC(i2c_dev)) 1231 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); 1232 1233 /* 1234 * During message read XFER_COMPLETE interrupt is triggered prior to 1235 * DMA completion and during message write XFER_COMPLETE interrupt is 1236 * triggered after DMA completion. 1237 * 1238 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer, 1239 * so forcing msg_buf_remaining to 0 in DMA mode. 1240 */ 1241 if (status & I2C_INT_PACKET_XFER_COMPLETE) { 1242 if (i2c_dev->dma_mode) 1243 i2c_dev->msg_buf_remaining = 0; 1244 /* 1245 * Underflow error condition: XFER_COMPLETE before message 1246 * fully sent. 1247 */ 1248 if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) { 1249 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; 1250 goto err; 1251 } 1252 complete(&i2c_dev->msg_complete); 1253 } 1254 goto done; 1255 err: 1256 /* mask all interrupts on error */ 1257 tegra_i2c_mask_irq(i2c_dev, 1258 I2C_INT_NO_ACK | 1259 I2C_INT_ARBITRATION_LOST | 1260 I2C_INT_PACKET_XFER_COMPLETE | 1261 I2C_INT_TX_FIFO_DATA_REQ | 1262 I2C_INT_RX_FIFO_DATA_REQ); 1263 1264 if (i2c_dev->hw->supports_bus_clear) 1265 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 1266 1267 i2c_writel(i2c_dev, status, i2c_dev->hw->regs->int_status); 1268 1269 if (IS_DVC(i2c_dev)) 1270 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); 1271 1272 if (i2c_dev->dma_mode) { 1273 dmaengine_terminate_async(i2c_dev->dma_chan); 1274 complete(&i2c_dev->dma_complete); 1275 } 1276 1277 complete(&i2c_dev->msg_complete); 1278 done: 1279 return IRQ_HANDLED; 1280 } 1281 1282 static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev, 1283 size_t len) 1284 { 1285 struct dma_slave_config slv_config = {0}; 1286 u32 val, reg, dma_burst, reg_offset; 1287 int err; 1288 1289 if (i2c_dev->hw->has_mst_fifo) 1290 reg = i2c_dev->hw->regs->mst_fifo_control; 1291 else 1292 reg = i2c_dev->hw->regs->fifo_control; 1293 1294 if (i2c_dev->dma_mode) { 1295 if (len & 0xF) 1296 dma_burst = 1; 1297 else if (len & 0x10) 1298 dma_burst = 4; 1299 else 1300 dma_burst = 8; 1301 1302 if (i2c_dev->msg_read) { 1303 reg_offset = i2c_dev->hw->regs->rx_fifo; 1304 1305 slv_config.src_addr = i2c_dev->base_phys + reg_offset; 1306 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1307 slv_config.src_maxburst = dma_burst; 1308 1309 if (i2c_dev->hw->has_mst_fifo) 1310 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst); 1311 else 1312 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst); 1313 } else { 1314 reg_offset = i2c_dev->hw->regs->tx_fifo; 1315 1316 slv_config.dst_addr = i2c_dev->base_phys + reg_offset; 1317 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1318 slv_config.dst_maxburst = dma_burst; 1319 1320 if (i2c_dev->hw->has_mst_fifo) 1321 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst); 1322 else 1323 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst); 1324 } 1325 1326 slv_config.device_fc = true; 1327 err = dmaengine_slave_config(i2c_dev->dma_chan, &slv_config); 1328 if (err) { 1329 dev_err(i2c_dev->dev, "DMA config failed: %d\n", err); 1330 dev_err(i2c_dev->dev, "falling back to PIO\n"); 1331 1332 tegra_i2c_release_dma(i2c_dev); 1333 i2c_dev->dma_mode = false; 1334 } else { 1335 goto out; 1336 } 1337 } 1338 1339 if (i2c_dev->hw->has_mst_fifo) 1340 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) | 1341 I2C_MST_FIFO_CONTROL_RX_TRIG(1); 1342 else 1343 val = I2C_FIFO_CONTROL_TX_TRIG(8) | 1344 I2C_FIFO_CONTROL_RX_TRIG(1); 1345 out: 1346 i2c_writel(i2c_dev, val, reg); 1347 } 1348 1349 static unsigned long tegra_i2c_poll_completion(struct tegra_i2c_dev *i2c_dev, 1350 struct completion *complete, 1351 unsigned int timeout_ms) 1352 { 1353 ktime_t ktime = ktime_get(); 1354 ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms); 1355 1356 do { 1357 u32 status = i2c_readl(i2c_dev, i2c_dev->hw->regs->int_status); 1358 1359 if (status) 1360 tegra_i2c_isr(i2c_dev->irq, i2c_dev); 1361 1362 if (completion_done(complete)) { 1363 s64 delta = ktime_ms_delta(ktimeout, ktime); 1364 1365 return msecs_to_jiffies(delta) ?: 1; 1366 } 1367 1368 ktime = ktime_get(); 1369 1370 } while (ktime_before(ktime, ktimeout)); 1371 1372 return 0; 1373 } 1374 1375 static unsigned long tegra_i2c_wait_completion(struct tegra_i2c_dev *i2c_dev, 1376 struct completion *complete, 1377 unsigned int timeout_ms) 1378 { 1379 unsigned long ret; 1380 1381 if (i2c_dev->atomic_mode) { 1382 ret = tegra_i2c_poll_completion(i2c_dev, complete, timeout_ms); 1383 } else { 1384 enable_irq(i2c_dev->irq); 1385 ret = wait_for_completion_timeout(complete, 1386 msecs_to_jiffies(timeout_ms)); 1387 disable_irq(i2c_dev->irq); 1388 1389 /* 1390 * Under some rare circumstances (like running KASAN + 1391 * NFS root) CPU, which handles interrupt, may stuck in 1392 * uninterruptible state for a significant time. In this 1393 * case we will get timeout if I2C transfer is running on 1394 * a sibling CPU, despite of IRQ being raised. 1395 * 1396 * In order to handle this rare condition, the IRQ status 1397 * needs to be checked after timeout. 1398 */ 1399 if (ret == 0) 1400 ret = tegra_i2c_poll_completion(i2c_dev, complete, 0); 1401 } 1402 1403 return ret; 1404 } 1405 1406 static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap) 1407 { 1408 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1409 u32 val, time_left; 1410 int err; 1411 1412 reinit_completion(&i2c_dev->msg_complete); 1413 1414 val = FIELD_PREP(I2C_BC_SCLK_THRESHOLD, 9) | I2C_BC_STOP_COND | 1415 I2C_BC_TERMINATE; 1416 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->bus_clear_cnfg); 1417 1418 err = tegra_i2c_wait_for_config_load(i2c_dev); 1419 if (err) 1420 return err; 1421 1422 val |= I2C_BC_ENABLE; 1423 i2c_writel(i2c_dev, val, i2c_dev->hw->regs->bus_clear_cnfg); 1424 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 1425 1426 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 50); 1427 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); 1428 1429 if (time_left == 0) { 1430 dev_err(i2c_dev->dev, "failed to clear bus\n"); 1431 return -ETIMEDOUT; 1432 } 1433 1434 val = i2c_readl(i2c_dev, i2c_dev->hw->regs->bus_clear_status); 1435 if (!(val & I2C_BC_STATUS)) { 1436 dev_err(i2c_dev->dev, "un-recovered arbitration lost\n"); 1437 return -EIO; 1438 } 1439 1440 return -EAGAIN; 1441 } 1442 1443 static void tegra_i2c_push_packet_header(struct tegra_i2c_dev *i2c_dev, 1444 struct i2c_msg *msg, 1445 enum msg_end_type end_state) 1446 { 1447 u32 *dma_buf = i2c_dev->dma_buf; 1448 u32 packet_header; 1449 1450 packet_header = FIELD_PREP(PACKET_HEADER0_HEADER_SIZE, 0) | 1451 FIELD_PREP(PACKET_HEADER0_PROTOCOL, 1452 PACKET_HEADER0_PROTOCOL_I2C) | 1453 FIELD_PREP(PACKET_HEADER0_CONT_ID, i2c_dev->cont_id) | 1454 FIELD_PREP(PACKET_HEADER0_PACKET_ID, 1); 1455 1456 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1457 *dma_buf++ = packet_header; 1458 else 1459 i2c_writel(i2c_dev, packet_header, i2c_dev->hw->regs->tx_fifo); 1460 1461 packet_header = i2c_dev->msg_len - 1; 1462 1463 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1464 *dma_buf++ = packet_header; 1465 else 1466 i2c_writel(i2c_dev, packet_header, i2c_dev->hw->regs->tx_fifo); 1467 1468 packet_header = I2C_HEADER_IE_ENABLE; 1469 1470 if (end_state == MSG_END_CONTINUE) 1471 packet_header |= I2C_HEADER_CONTINUE_XFER; 1472 else if (end_state == MSG_END_REPEAT_START) 1473 packet_header |= I2C_HEADER_REPEAT_START; 1474 1475 if (msg->flags & I2C_M_TEN) { 1476 packet_header |= msg->addr; 1477 packet_header |= I2C_HEADER_10BIT_ADDR; 1478 } else { 1479 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT; 1480 } 1481 1482 if (msg->flags & I2C_M_IGNORE_NAK) 1483 packet_header |= I2C_HEADER_CONT_ON_NAK; 1484 1485 if (msg->flags & I2C_M_RD) 1486 packet_header |= I2C_HEADER_READ; 1487 1488 if (i2c_dev->timings.bus_freq_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) 1489 packet_header |= I2C_HEADER_HS_MODE; 1490 1491 if (i2c_dev->dma_mode && !i2c_dev->msg_read) 1492 *dma_buf++ = packet_header; 1493 else 1494 i2c_writel(i2c_dev, packet_header, i2c_dev->hw->regs->tx_fifo); 1495 } 1496 1497 static int tegra_i2c_error_recover(struct tegra_i2c_dev *i2c_dev, 1498 struct i2c_msg *msg) 1499 { 1500 if (i2c_dev->msg_err == I2C_ERR_NONE) 1501 return 0; 1502 1503 tegra_i2c_init(i2c_dev); 1504 1505 /* start recovery upon arbitration loss in single master mode */ 1506 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) { 1507 if (!i2c_dev->multimaster_mode) 1508 return i2c_recover_bus(&i2c_dev->adapter); 1509 1510 return -EAGAIN; 1511 } 1512 1513 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) { 1514 if (msg->flags & I2C_M_IGNORE_NAK) 1515 return 0; 1516 1517 return -EREMOTEIO; 1518 } 1519 1520 return -EIO; 1521 } 1522 1523 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev, 1524 struct i2c_msg *msg, 1525 enum msg_end_type end_state) 1526 { 1527 unsigned long time_left, xfer_time = 100; 1528 size_t xfer_size; 1529 u32 int_mask; 1530 int err; 1531 1532 err = tegra_i2c_flush_fifos(i2c_dev); 1533 if (err) 1534 return err; 1535 1536 i2c_dev->msg_buf = msg->buf; 1537 i2c_dev->msg_len = msg->len; 1538 1539 i2c_dev->msg_err = I2C_ERR_NONE; 1540 i2c_dev->msg_read = !!(msg->flags & I2C_M_RD); 1541 reinit_completion(&i2c_dev->msg_complete); 1542 1543 /* 1544 * For SMBUS block read command, read only 1 byte in the first transfer. 1545 * Adjust that 1 byte for the next transfer in the msg buffer and msg 1546 * length. 1547 */ 1548 if (msg->flags & I2C_M_RECV_LEN) { 1549 if (end_state == MSG_END_CONTINUE) { 1550 i2c_dev->msg_len = 1; 1551 } else { 1552 i2c_dev->msg_buf += 1; 1553 i2c_dev->msg_len -= 1; 1554 } 1555 } 1556 1557 i2c_dev->msg_buf_remaining = i2c_dev->msg_len; 1558 1559 if (i2c_dev->msg_read) 1560 xfer_size = i2c_dev->msg_len; 1561 else 1562 xfer_size = i2c_dev->msg_len + I2C_PACKET_HEADER_SIZE; 1563 1564 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD); 1565 1566 i2c_dev->dma_mode = xfer_size > I2C_PIO_MODE_PREFERRED_LEN && 1567 i2c_dev->dma_buf && !i2c_dev->atomic_mode; 1568 1569 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size); 1570 1571 /* 1572 * Transfer time in mSec = Total bits / transfer rate 1573 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits 1574 */ 1575 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC, 1576 i2c_dev->timings.bus_freq_hz); 1577 1578 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; 1579 tegra_i2c_unmask_irq(i2c_dev, int_mask); 1580 1581 if (i2c_dev->dma_mode) { 1582 if (i2c_dev->msg_read) { 1583 err = tegra_i2c_dma_submit(i2c_dev, xfer_size); 1584 if (err) 1585 return err; 1586 } 1587 } 1588 1589 tegra_i2c_push_packet_header(i2c_dev, msg, end_state); 1590 1591 if (!i2c_dev->msg_read) { 1592 if (i2c_dev->dma_mode) { 1593 memcpy(i2c_dev->dma_buf + I2C_PACKET_HEADER_SIZE, 1594 msg->buf, i2c_dev->msg_len); 1595 err = tegra_i2c_dma_submit(i2c_dev, xfer_size); 1596 if (err) 1597 return err; 1598 } else { 1599 tegra_i2c_fill_tx_fifo(i2c_dev); 1600 } 1601 } 1602 1603 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq) 1604 int_mask |= I2C_INT_PACKET_XFER_COMPLETE; 1605 1606 if (!i2c_dev->dma_mode) { 1607 if (msg->flags & I2C_M_RD) 1608 int_mask |= I2C_INT_RX_FIFO_DATA_REQ; 1609 else if (i2c_dev->msg_buf_remaining) 1610 int_mask |= I2C_INT_TX_FIFO_DATA_REQ; 1611 } 1612 1613 tegra_i2c_unmask_irq(i2c_dev, int_mask); 1614 dev_dbg(i2c_dev->dev, "unmasked IRQ: %02x\n", 1615 i2c_readl(i2c_dev, i2c_dev->hw->regs->int_mask)); 1616 1617 if (i2c_dev->dma_mode) { 1618 time_left = tegra_i2c_wait_completion(i2c_dev, 1619 &i2c_dev->dma_complete, 1620 xfer_time); 1621 1622 /* 1623 * Synchronize DMA first, since dmaengine_terminate_sync() 1624 * performs synchronization after the transfer's termination 1625 * and we want to get a completion if transfer succeeded. 1626 */ 1627 dmaengine_synchronize(i2c_dev->dma_chan); 1628 dmaengine_terminate_sync(i2c_dev->dma_chan); 1629 1630 if (!time_left && !completion_done(&i2c_dev->dma_complete)) { 1631 tegra_i2c_init(i2c_dev); 1632 return -ETIMEDOUT; 1633 } 1634 1635 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) 1636 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, i2c_dev->msg_len); 1637 } 1638 1639 time_left = tegra_i2c_wait_completion(i2c_dev, &i2c_dev->msg_complete, 1640 xfer_time); 1641 1642 tegra_i2c_mask_irq(i2c_dev, int_mask); 1643 1644 if (time_left == 0) { 1645 tegra_i2c_init(i2c_dev); 1646 return -ETIMEDOUT; 1647 } 1648 1649 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n", 1650 time_left, completion_done(&i2c_dev->msg_complete), 1651 i2c_dev->msg_err); 1652 1653 i2c_dev->dma_mode = false; 1654 1655 err = tegra_i2c_error_recover(i2c_dev, msg); 1656 if (err) 1657 return err; 1658 1659 return 0; 1660 } 1661 1662 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], 1663 int num) 1664 { 1665 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1666 int i, ret; 1667 1668 ret = pm_runtime_get_sync(i2c_dev->dev); 1669 if (ret < 0) { 1670 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret); 1671 pm_runtime_put_noidle(i2c_dev->dev); 1672 return ret; 1673 } 1674 1675 ret = tegra_i2c_mutex_lock(i2c_dev); 1676 if (ret) { 1677 pm_runtime_put(i2c_dev->dev); 1678 return ret; 1679 } 1680 1681 for (i = 0; i < num; i++) { 1682 enum msg_end_type end_type = MSG_END_STOP; 1683 1684 if (i < (num - 1)) { 1685 /* check whether follow up message is coming */ 1686 if (msgs[i + 1].flags & I2C_M_NOSTART) 1687 end_type = MSG_END_CONTINUE; 1688 else 1689 end_type = MSG_END_REPEAT_START; 1690 } 1691 /* If M_RECV_LEN use ContinueXfer to read the first byte */ 1692 if (msgs[i].flags & I2C_M_RECV_LEN) { 1693 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], MSG_END_CONTINUE); 1694 if (ret) 1695 break; 1696 1697 /* Validate message length before proceeding */ 1698 if (msgs[i].buf[0] == 0 || msgs[i].buf[0] > I2C_SMBUS_BLOCK_MAX) 1699 break; 1700 1701 /* Set the msg length from first byte */ 1702 msgs[i].len += msgs[i].buf[0]; 1703 dev_dbg(i2c_dev->dev, "reading %d bytes\n", msgs[i].len); 1704 } 1705 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type); 1706 if (ret) 1707 break; 1708 } 1709 1710 tegra_i2c_mutex_unlock(i2c_dev); 1711 pm_runtime_put(i2c_dev->dev); 1712 1713 return ret ?: i; 1714 } 1715 1716 static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap, 1717 struct i2c_msg msgs[], int num) 1718 { 1719 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1720 int ret; 1721 1722 i2c_dev->atomic_mode = true; 1723 ret = tegra_i2c_xfer(adap, msgs, num); 1724 i2c_dev->atomic_mode = false; 1725 1726 return ret; 1727 } 1728 1729 static u32 tegra_i2c_func(struct i2c_adapter *adap) 1730 { 1731 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); 1732 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | 1733 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; 1734 1735 if (i2c_dev->hw->has_continue_xfer_support) 1736 ret |= I2C_FUNC_NOSTART | I2C_FUNC_SMBUS_READ_BLOCK_DATA; 1737 1738 return ret; 1739 } 1740 1741 static const struct i2c_algorithm tegra_i2c_algo = { 1742 .xfer = tegra_i2c_xfer, 1743 .xfer_atomic = tegra_i2c_xfer_atomic, 1744 .functionality = tegra_i2c_func, 1745 }; 1746 1747 /* payload size is only 12 bit */ 1748 static const struct i2c_adapter_quirks tegra_i2c_quirks = { 1749 .flags = I2C_AQ_NO_ZERO_LEN, 1750 .max_read_len = SZ_4K, 1751 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE, 1752 }; 1753 1754 static const struct i2c_adapter_quirks tegra194_i2c_quirks = { 1755 .flags = I2C_AQ_NO_ZERO_LEN, 1756 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE, 1757 }; 1758 1759 static struct i2c_bus_recovery_info tegra_i2c_recovery_info = { 1760 .recover_bus = tegra_i2c_issue_bus_clear, 1761 }; 1762 1763 static const struct tegra_i2c_hw_feature tegra20_i2c_hw = { 1764 .has_continue_xfer_support = false, 1765 .has_per_pkt_xfer_complete_irq = false, 1766 .clk_divisor_hs_mode = 3, 1767 .clk_divisor_std_mode = 0, 1768 .clk_divisor_fast_mode = 0, 1769 .clk_divisor_fast_plus_mode = 0, 1770 .has_config_load_reg = false, 1771 .has_multi_master_mode = false, 1772 .has_slcg_override_reg = false, 1773 .has_mst_fifo = false, 1774 .has_mst_reset = false, 1775 .quirks = &tegra_i2c_quirks, 1776 .supports_bus_clear = false, 1777 .has_apb_dma = true, 1778 .tlow_std_mode = 0x4, 1779 .thigh_std_mode = 0x2, 1780 .tlow_fast_mode = 0x4, 1781 .thigh_fast_mode = 0x2, 1782 .tlow_fastplus_mode = 0x4, 1783 .thigh_fastplus_mode = 0x2, 1784 .setup_hold_time_std_mode = 0x0, 1785 .setup_hold_time_fast_mode = 0x0, 1786 .setup_hold_time_fastplus_mode = 0x0, 1787 .setup_hold_time_hs_mode = 0x0, 1788 .has_interface_timing_reg = false, 1789 .enable_hs_mode_support = false, 1790 .has_mutex = false, 1791 .has_fairarb_reg = false, 1792 .variant = TEGRA_I2C_VARIANT_DEFAULT, 1793 .regs = &tegra20_i2c_regs, 1794 }; 1795 1796 #if IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) 1797 static const struct tegra_i2c_hw_feature tegra20_dvc_i2c_hw = { 1798 .has_continue_xfer_support = false, 1799 .has_per_pkt_xfer_complete_irq = false, 1800 .clk_divisor_hs_mode = 3, 1801 .clk_divisor_std_mode = 0, 1802 .clk_divisor_fast_mode = 0, 1803 .clk_divisor_fast_plus_mode = 0, 1804 .has_config_load_reg = false, 1805 .has_multi_master_mode = false, 1806 .has_slcg_override_reg = false, 1807 .has_mst_fifo = false, 1808 .has_mst_reset = false, 1809 .quirks = &tegra_i2c_quirks, 1810 .supports_bus_clear = false, 1811 .has_apb_dma = true, 1812 .tlow_std_mode = 0x4, 1813 .thigh_std_mode = 0x2, 1814 .tlow_fast_mode = 0x4, 1815 .thigh_fast_mode = 0x2, 1816 .tlow_fastplus_mode = 0x4, 1817 .thigh_fastplus_mode = 0x2, 1818 .setup_hold_time_std_mode = 0x0, 1819 .setup_hold_time_fast_mode = 0x0, 1820 .setup_hold_time_fastplus_mode = 0x0, 1821 .setup_hold_time_hs_mode = 0x0, 1822 .has_interface_timing_reg = false, 1823 .enable_hs_mode_support = false, 1824 .has_mutex = false, 1825 .has_fairarb_reg = false, 1826 .variant = TEGRA_I2C_VARIANT_DVC, 1827 .regs = &tegra20_dvc_i2c_regs, 1828 }; 1829 #endif 1830 1831 static const struct tegra_i2c_hw_feature tegra30_i2c_hw = { 1832 .has_continue_xfer_support = true, 1833 .has_per_pkt_xfer_complete_irq = false, 1834 .clk_divisor_hs_mode = 3, 1835 .clk_divisor_std_mode = 0, 1836 .clk_divisor_fast_mode = 0, 1837 .clk_divisor_fast_plus_mode = 0, 1838 .has_config_load_reg = false, 1839 .has_multi_master_mode = false, 1840 .has_slcg_override_reg = false, 1841 .has_mst_fifo = false, 1842 .has_mst_reset = false, 1843 .quirks = &tegra_i2c_quirks, 1844 .supports_bus_clear = false, 1845 .has_apb_dma = true, 1846 .tlow_std_mode = 0x4, 1847 .thigh_std_mode = 0x2, 1848 .tlow_fast_mode = 0x4, 1849 .thigh_fast_mode = 0x2, 1850 .tlow_fastplus_mode = 0x4, 1851 .thigh_fastplus_mode = 0x2, 1852 .setup_hold_time_std_mode = 0x0, 1853 .setup_hold_time_fast_mode = 0x0, 1854 .setup_hold_time_fastplus_mode = 0x0, 1855 .setup_hold_time_hs_mode = 0x0, 1856 .has_interface_timing_reg = false, 1857 .enable_hs_mode_support = false, 1858 .has_mutex = false, 1859 .has_fairarb_reg = false, 1860 .variant = TEGRA_I2C_VARIANT_DEFAULT, 1861 .regs = &tegra20_i2c_regs, 1862 }; 1863 1864 static const struct tegra_i2c_hw_feature tegra114_i2c_hw = { 1865 .has_continue_xfer_support = true, 1866 .has_per_pkt_xfer_complete_irq = true, 1867 .clk_divisor_hs_mode = 1, 1868 .clk_divisor_std_mode = 0x19, 1869 .clk_divisor_fast_mode = 0x19, 1870 .clk_divisor_fast_plus_mode = 0x10, 1871 .has_config_load_reg = false, 1872 .has_multi_master_mode = false, 1873 .has_slcg_override_reg = false, 1874 .has_mst_fifo = false, 1875 .has_mst_reset = false, 1876 .quirks = &tegra_i2c_quirks, 1877 .supports_bus_clear = true, 1878 .has_apb_dma = true, 1879 .tlow_std_mode = 0x4, 1880 .thigh_std_mode = 0x2, 1881 .tlow_fast_mode = 0x4, 1882 .thigh_fast_mode = 0x2, 1883 .tlow_fastplus_mode = 0x4, 1884 .thigh_fastplus_mode = 0x2, 1885 .setup_hold_time_std_mode = 0x0, 1886 .setup_hold_time_fast_mode = 0x0, 1887 .setup_hold_time_fastplus_mode = 0x0, 1888 .setup_hold_time_hs_mode = 0x0, 1889 .has_interface_timing_reg = false, 1890 .enable_hs_mode_support = false, 1891 .has_mutex = false, 1892 .has_fairarb_reg = false, 1893 .variant = TEGRA_I2C_VARIANT_DEFAULT, 1894 .regs = &tegra20_i2c_regs, 1895 }; 1896 1897 static const struct tegra_i2c_hw_feature tegra124_i2c_hw = { 1898 .has_continue_xfer_support = true, 1899 .has_per_pkt_xfer_complete_irq = true, 1900 .clk_divisor_hs_mode = 1, 1901 .clk_divisor_std_mode = 0x19, 1902 .clk_divisor_fast_mode = 0x19, 1903 .clk_divisor_fast_plus_mode = 0x10, 1904 .has_config_load_reg = true, 1905 .has_multi_master_mode = false, 1906 .has_slcg_override_reg = true, 1907 .has_mst_fifo = false, 1908 .has_mst_reset = false, 1909 .quirks = &tegra_i2c_quirks, 1910 .supports_bus_clear = true, 1911 .has_apb_dma = true, 1912 .tlow_std_mode = 0x4, 1913 .thigh_std_mode = 0x2, 1914 .tlow_fast_mode = 0x4, 1915 .thigh_fast_mode = 0x2, 1916 .tlow_fastplus_mode = 0x4, 1917 .thigh_fastplus_mode = 0x2, 1918 .setup_hold_time_std_mode = 0x0, 1919 .setup_hold_time_fast_mode = 0x0, 1920 .setup_hold_time_fastplus_mode = 0x0, 1921 .setup_hold_time_hs_mode = 0x0, 1922 .has_interface_timing_reg = true, 1923 .enable_hs_mode_support = false, 1924 .has_mutex = false, 1925 .has_fairarb_reg = false, 1926 .variant = TEGRA_I2C_VARIANT_DEFAULT, 1927 .regs = &tegra20_i2c_regs, 1928 }; 1929 1930 static const struct tegra_i2c_hw_feature tegra210_i2c_hw = { 1931 .has_continue_xfer_support = true, 1932 .has_per_pkt_xfer_complete_irq = true, 1933 .clk_divisor_hs_mode = 1, 1934 .clk_divisor_std_mode = 0x19, 1935 .clk_divisor_fast_mode = 0x19, 1936 .clk_divisor_fast_plus_mode = 0x10, 1937 .has_config_load_reg = true, 1938 .has_multi_master_mode = false, 1939 .has_slcg_override_reg = true, 1940 .has_mst_fifo = false, 1941 .has_mst_reset = false, 1942 .quirks = &tegra_i2c_quirks, 1943 .supports_bus_clear = true, 1944 .has_apb_dma = true, 1945 .tlow_std_mode = 0x4, 1946 .thigh_std_mode = 0x2, 1947 .tlow_fast_mode = 0x4, 1948 .thigh_fast_mode = 0x2, 1949 .tlow_fastplus_mode = 0x4, 1950 .thigh_fastplus_mode = 0x2, 1951 .setup_hold_time_std_mode = 0, 1952 .setup_hold_time_fast_mode = 0, 1953 .setup_hold_time_fastplus_mode = 0, 1954 .setup_hold_time_hs_mode = 0, 1955 .has_interface_timing_reg = true, 1956 .enable_hs_mode_support = false, 1957 .has_mutex = false, 1958 .has_fairarb_reg = false, 1959 .variant = TEGRA_I2C_VARIANT_DEFAULT, 1960 .regs = &tegra20_i2c_regs, 1961 }; 1962 1963 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 1964 static const struct tegra_i2c_hw_feature tegra210_vi_i2c_hw = { 1965 .has_continue_xfer_support = true, 1966 .has_per_pkt_xfer_complete_irq = true, 1967 .clk_divisor_hs_mode = 1, 1968 .clk_divisor_std_mode = 0x19, 1969 .clk_divisor_fast_mode = 0x19, 1970 .clk_divisor_fast_plus_mode = 0x10, 1971 .has_config_load_reg = true, 1972 .has_multi_master_mode = false, 1973 .has_slcg_override_reg = true, 1974 .has_mst_fifo = false, 1975 .has_mst_reset = false, 1976 .quirks = &tegra_i2c_quirks, 1977 .supports_bus_clear = true, 1978 .has_apb_dma = true, 1979 .tlow_std_mode = 0x4, 1980 .thigh_std_mode = 0x2, 1981 .tlow_fast_mode = 0x4, 1982 .thigh_fast_mode = 0x2, 1983 .tlow_fastplus_mode = 0x4, 1984 .thigh_fastplus_mode = 0x2, 1985 .setup_hold_time_std_mode = 0, 1986 .setup_hold_time_fast_mode = 0, 1987 .setup_hold_time_fastplus_mode = 0, 1988 .setup_hold_time_hs_mode = 0, 1989 .has_interface_timing_reg = true, 1990 .enable_hs_mode_support = false, 1991 .has_mutex = false, 1992 .has_fairarb_reg = false, 1993 .variant = TEGRA_I2C_VARIANT_VI, 1994 .regs = &tegra210_vi_i2c_regs, 1995 }; 1996 #endif 1997 1998 static const struct tegra_i2c_hw_feature tegra186_i2c_hw = { 1999 .has_continue_xfer_support = true, 2000 .has_per_pkt_xfer_complete_irq = true, 2001 .clk_divisor_hs_mode = 1, 2002 .clk_divisor_std_mode = 0x16, 2003 .clk_divisor_fast_mode = 0x19, 2004 .clk_divisor_fast_plus_mode = 0x10, 2005 .has_config_load_reg = true, 2006 .has_multi_master_mode = false, 2007 .has_slcg_override_reg = true, 2008 .has_mst_fifo = false, 2009 .has_mst_reset = false, 2010 .quirks = &tegra_i2c_quirks, 2011 .supports_bus_clear = true, 2012 .has_apb_dma = false, 2013 .tlow_std_mode = 0x4, 2014 .thigh_std_mode = 0x3, 2015 .tlow_fast_mode = 0x4, 2016 .thigh_fast_mode = 0x2, 2017 .tlow_fastplus_mode = 0x4, 2018 .thigh_fastplus_mode = 0x2, 2019 .setup_hold_time_std_mode = 0, 2020 .setup_hold_time_fast_mode = 0, 2021 .setup_hold_time_fastplus_mode = 0, 2022 .setup_hold_time_hs_mode = 0, 2023 .has_interface_timing_reg = true, 2024 .enable_hs_mode_support = false, 2025 .has_mutex = false, 2026 .has_fairarb_reg = false, 2027 .variant = TEGRA_I2C_VARIANT_DEFAULT, 2028 .regs = &tegra20_i2c_regs, 2029 }; 2030 2031 static const struct tegra_i2c_hw_feature tegra194_i2c_hw = { 2032 .has_continue_xfer_support = true, 2033 .has_per_pkt_xfer_complete_irq = true, 2034 .clk_divisor_hs_mode = 1, 2035 .clk_divisor_std_mode = 0x4f, 2036 .clk_divisor_fast_mode = 0x3c, 2037 .clk_divisor_fast_plus_mode = 0x16, 2038 .has_config_load_reg = true, 2039 .has_multi_master_mode = true, 2040 .has_slcg_override_reg = true, 2041 .has_mst_fifo = true, 2042 .has_mst_reset = true, 2043 .quirks = &tegra194_i2c_quirks, 2044 .supports_bus_clear = true, 2045 .has_apb_dma = false, 2046 .tlow_std_mode = 0x8, 2047 .thigh_std_mode = 0x7, 2048 .tlow_fast_mode = 0x2, 2049 .thigh_fast_mode = 0x2, 2050 .tlow_fastplus_mode = 0x2, 2051 .thigh_fastplus_mode = 0x2, 2052 .tlow_hs_mode = 0x8, 2053 .thigh_hs_mode = 0x3, 2054 .setup_hold_time_std_mode = 0x08080808, 2055 .setup_hold_time_fast_mode = 0x02020202, 2056 .setup_hold_time_fastplus_mode = 0x02020202, 2057 .setup_hold_time_hs_mode = 0x090909, 2058 .has_interface_timing_reg = true, 2059 .enable_hs_mode_support = true, 2060 .has_mutex = false, 2061 .has_fairarb_reg = false, 2062 .variant = TEGRA_I2C_VARIANT_DEFAULT, 2063 .regs = &tegra20_i2c_regs, 2064 }; 2065 2066 static const struct tegra_i2c_hw_feature tegra256_i2c_hw = { 2067 .has_continue_xfer_support = true, 2068 .has_per_pkt_xfer_complete_irq = true, 2069 .clk_divisor_hs_mode = 9, 2070 .clk_divisor_std_mode = 0x7a, 2071 .clk_divisor_fast_mode = 0x40, 2072 .clk_divisor_fast_plus_mode = 0x14, 2073 .has_config_load_reg = true, 2074 .has_multi_master_mode = true, 2075 .has_slcg_override_reg = true, 2076 .has_mst_fifo = true, 2077 .has_mst_reset = true, 2078 .quirks = &tegra194_i2c_quirks, 2079 .supports_bus_clear = true, 2080 .has_apb_dma = false, 2081 .tlow_std_mode = 0x8, 2082 .thigh_std_mode = 0x7, 2083 .tlow_fast_mode = 0x4, 2084 .thigh_fast_mode = 0x2, 2085 .tlow_fastplus_mode = 0x4, 2086 .thigh_fastplus_mode = 0x4, 2087 .tlow_hs_mode = 0x3, 2088 .thigh_hs_mode = 0x2, 2089 .setup_hold_time_std_mode = 0x08080808, 2090 .setup_hold_time_fast_mode = 0x04010101, 2091 .setup_hold_time_fastplus_mode = 0x04020202, 2092 .setup_hold_time_hs_mode = 0x030303, 2093 .has_interface_timing_reg = true, 2094 .enable_hs_mode_support = true, 2095 .has_mutex = true, 2096 .has_fairarb_reg = true, 2097 .variant = TEGRA_I2C_VARIANT_DEFAULT, 2098 .regs = &tegra264_i2c_regs, 2099 }; 2100 2101 static const struct tegra_i2c_hw_feature tegra264_i2c_hw = { 2102 .has_continue_xfer_support = true, 2103 .has_per_pkt_xfer_complete_irq = true, 2104 .clk_divisor_hs_mode = 1, 2105 .clk_divisor_std_mode = 0x1d, 2106 .clk_divisor_fast_mode = 0x15, 2107 .clk_divisor_fast_plus_mode = 0x8, 2108 .has_config_load_reg = true, 2109 .has_multi_master_mode = true, 2110 .has_slcg_override_reg = true, 2111 .has_mst_fifo = true, 2112 .has_mst_reset = true, 2113 .quirks = &tegra194_i2c_quirks, 2114 .supports_bus_clear = true, 2115 .has_apb_dma = false, 2116 .tlow_std_mode = 0x8, 2117 .thigh_std_mode = 0x7, 2118 .tlow_fast_mode = 0x2, 2119 .thigh_fast_mode = 0x2, 2120 .tlow_fastplus_mode = 0x2, 2121 .thigh_fastplus_mode = 0x2, 2122 .tlow_hs_mode = 0x4, 2123 .thigh_hs_mode = 0x2, 2124 .setup_hold_time_std_mode = 0x08080808, 2125 .setup_hold_time_fast_mode = 0x02020202, 2126 .setup_hold_time_fastplus_mode = 0x02020202, 2127 .setup_hold_time_hs_mode = 0x090909, 2128 .has_interface_timing_reg = true, 2129 .enable_hs_mode_support = true, 2130 .has_mutex = true, 2131 .has_fairarb_reg = true, 2132 .variant = TEGRA_I2C_VARIANT_DEFAULT, 2133 .regs = &tegra264_i2c_regs, 2134 }; 2135 2136 static const struct tegra_i2c_hw_feature tegra410_i2c_hw = { 2137 .has_continue_xfer_support = true, 2138 .has_per_pkt_xfer_complete_irq = true, 2139 .clk_divisor_hs_mode = 2, 2140 .clk_divisor_std_mode = 0x3f, 2141 .clk_divisor_fast_mode = 0x2f, 2142 .clk_divisor_fast_plus_mode = 0x11, 2143 .has_config_load_reg = true, 2144 .has_multi_master_mode = true, 2145 .has_slcg_override_reg = true, 2146 .has_mst_fifo = true, 2147 .has_mst_reset = true, 2148 .quirks = &tegra194_i2c_quirks, 2149 .supports_bus_clear = true, 2150 .has_apb_dma = false, 2151 .tlow_std_mode = 0x8, 2152 .thigh_std_mode = 0x7, 2153 .tlow_fast_mode = 0x2, 2154 .thigh_fast_mode = 0x2, 2155 .tlow_fastplus_mode = 0x2, 2156 .thigh_fastplus_mode = 0x2, 2157 .tlow_hs_mode = 0x5, 2158 .thigh_hs_mode = 0x2, 2159 .setup_hold_time_std_mode = 0x08080808, 2160 .setup_hold_time_fast_mode = 0x02020202, 2161 .setup_hold_time_fastplus_mode = 0x02020202, 2162 .setup_hold_time_hs_mode = 0x0b0b0b, 2163 .has_interface_timing_reg = true, 2164 .enable_hs_mode_support = true, 2165 .has_mutex = true, 2166 .has_fairarb_reg = true, 2167 .variant = TEGRA_I2C_VARIANT_DEFAULT, 2168 .regs = &tegra410_i2c_regs, 2169 }; 2170 2171 static const struct of_device_id tegra_i2c_of_match[] = { 2172 { .compatible = "nvidia,tegra264-i2c", .data = &tegra264_i2c_hw, }, 2173 { .compatible = "nvidia,tegra256-i2c", .data = &tegra256_i2c_hw, }, 2174 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, }, 2175 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, }, 2176 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 2177 { .compatible = "nvidia,tegra210-i2c-vi", .data = &tegra210_vi_i2c_hw, }, 2178 #endif 2179 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, }, 2180 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, }, 2181 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, }, 2182 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, }, 2183 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, }, 2184 #if IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC) 2185 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_dvc_i2c_hw, }, 2186 #endif 2187 {}, 2188 }; 2189 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match); 2190 2191 static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev) 2192 { 2193 bool multi_mode; 2194 2195 i2c_parse_fw_timings(i2c_dev->dev, &i2c_dev->timings, true); 2196 2197 multi_mode = device_property_read_bool(i2c_dev->dev, "multi-master"); 2198 i2c_dev->multimaster_mode = multi_mode; 2199 i2c_dev->is_mctp = device_property_present(i2c_dev->dev, "mctp-controller"); 2200 } 2201 2202 static int tegra_i2c_init_clocks(struct tegra_i2c_dev *i2c_dev) 2203 { 2204 int err; 2205 2206 if (ACPI_HANDLE(i2c_dev->dev)) 2207 return 0; 2208 2209 i2c_dev->clocks[i2c_dev->nclocks++].id = "div-clk"; 2210 2211 if (i2c_dev->hw == &tegra20_i2c_hw || i2c_dev->hw == &tegra30_i2c_hw) 2212 i2c_dev->clocks[i2c_dev->nclocks++].id = "fast-clk"; 2213 2214 if (IS_VI(i2c_dev)) 2215 i2c_dev->clocks[i2c_dev->nclocks++].id = "slow"; 2216 2217 err = devm_clk_bulk_get(i2c_dev->dev, i2c_dev->nclocks, 2218 i2c_dev->clocks); 2219 if (err) 2220 return err; 2221 2222 err = clk_bulk_prepare(i2c_dev->nclocks, i2c_dev->clocks); 2223 if (err) 2224 return err; 2225 2226 i2c_dev->div_clk = i2c_dev->clocks[0].clk; 2227 2228 if (!i2c_dev->multimaster_mode) 2229 return 0; 2230 2231 err = clk_enable(i2c_dev->div_clk); 2232 if (err) { 2233 dev_err_probe(i2c_dev->dev, err, "failed to enable div-clk\n"); 2234 goto unprepare_clocks; 2235 } 2236 2237 return 0; 2238 2239 unprepare_clocks: 2240 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks); 2241 2242 return err; 2243 } 2244 2245 static void tegra_i2c_release_clocks(struct tegra_i2c_dev *i2c_dev) 2246 { 2247 if (i2c_dev->multimaster_mode) 2248 clk_disable(i2c_dev->div_clk); 2249 2250 clk_bulk_unprepare(i2c_dev->nclocks, i2c_dev->clocks); 2251 } 2252 2253 static int tegra_i2c_init_hardware(struct tegra_i2c_dev *i2c_dev) 2254 { 2255 int ret; 2256 2257 ret = pm_runtime_get_sync(i2c_dev->dev); 2258 if (ret < 0) 2259 dev_err_probe(i2c_dev->dev, ret, "runtime resume failed\n"); 2260 else 2261 ret = tegra_i2c_init(i2c_dev); 2262 2263 pm_runtime_put_sync(i2c_dev->dev); 2264 2265 return ret; 2266 } 2267 2268 static int tegra_i2c_probe(struct platform_device *pdev) 2269 { 2270 struct tegra_i2c_dev *i2c_dev; 2271 struct resource *res; 2272 int err; 2273 2274 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 2275 if (!i2c_dev) 2276 return -ENOMEM; 2277 2278 platform_set_drvdata(pdev, i2c_dev); 2279 2280 init_completion(&i2c_dev->msg_complete); 2281 init_completion(&i2c_dev->dma_complete); 2282 2283 i2c_dev->hw = device_get_match_data(&pdev->dev); 2284 i2c_dev->cont_id = pdev->id; 2285 i2c_dev->dev = &pdev->dev; 2286 2287 i2c_dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 2288 if (IS_ERR(i2c_dev->base)) 2289 return PTR_ERR(i2c_dev->base); 2290 2291 i2c_dev->base_phys = res->start; 2292 2293 err = platform_get_irq(pdev, 0); 2294 if (err < 0) 2295 return err; 2296 2297 i2c_dev->irq = err; 2298 2299 /* interrupt will be enabled during of transfer time */ 2300 irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN); 2301 2302 err = devm_request_threaded_irq(i2c_dev->dev, i2c_dev->irq, 2303 NULL, tegra_i2c_isr, 2304 IRQF_NO_SUSPEND | IRQF_ONESHOT, 2305 dev_name(i2c_dev->dev), i2c_dev); 2306 if (err) 2307 return err; 2308 2309 tegra_i2c_parse_dt(i2c_dev); 2310 2311 err = tegra_i2c_init_clocks(i2c_dev); 2312 if (err) 2313 return err; 2314 2315 err = tegra_i2c_init_dma(i2c_dev); 2316 if (err) 2317 goto release_clocks; 2318 2319 /* 2320 * VI I2C is in VE power domain which is not always ON and not 2321 * IRQ-safe. Thus, IRQ-safe device shouldn't be attached to a 2322 * non IRQ-safe domain because this prevents powering off the power 2323 * domain. 2324 * 2325 * VI I2C device shouldn't be marked as IRQ-safe because VI I2C won't 2326 * be used for atomic transfers. ACPI device is not IRQ safe also. 2327 * 2328 * Devices with pinctrl states cannot be marked IRQ-safe as the pinctrl 2329 * state transitions during runtime PM require mutexes. 2330 */ 2331 if (!IS_VI(i2c_dev) && !has_acpi_companion(i2c_dev->dev) && !i2c_dev->dev->pins) 2332 pm_runtime_irq_safe(i2c_dev->dev); 2333 2334 pm_runtime_enable(i2c_dev->dev); 2335 2336 err = tegra_i2c_init_hardware(i2c_dev); 2337 if (err) 2338 goto release_rpm; 2339 2340 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev); 2341 i2c_dev->adapter.dev.of_node = i2c_dev->dev->of_node; 2342 i2c_dev->adapter.dev.parent = i2c_dev->dev; 2343 i2c_dev->adapter.retries = 1; 2344 i2c_dev->adapter.timeout = 6 * HZ; 2345 i2c_dev->adapter.quirks = i2c_dev->hw->quirks; 2346 i2c_dev->adapter.owner = THIS_MODULE; 2347 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED; 2348 i2c_dev->adapter.algo = &tegra_i2c_algo; 2349 i2c_dev->adapter.nr = pdev->id; 2350 ACPI_COMPANION_SET(&i2c_dev->adapter.dev, ACPI_COMPANION(&pdev->dev)); 2351 2352 if (i2c_dev->hw->supports_bus_clear) 2353 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info; 2354 2355 strscpy(i2c_dev->adapter.name, dev_name(i2c_dev->dev), 2356 sizeof(i2c_dev->adapter.name)); 2357 2358 err = i2c_add_numbered_adapter(&i2c_dev->adapter); 2359 if (err) 2360 goto release_rpm; 2361 2362 return 0; 2363 2364 release_rpm: 2365 pm_runtime_disable(i2c_dev->dev); 2366 2367 tegra_i2c_release_dma(i2c_dev); 2368 release_clocks: 2369 tegra_i2c_release_clocks(i2c_dev); 2370 2371 return err; 2372 } 2373 2374 static void tegra_i2c_remove(struct platform_device *pdev) 2375 { 2376 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 2377 2378 i2c_del_adapter(&i2c_dev->adapter); 2379 pm_runtime_force_suspend(i2c_dev->dev); 2380 2381 tegra_i2c_release_dma(i2c_dev); 2382 tegra_i2c_release_clocks(i2c_dev); 2383 } 2384 2385 static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev) 2386 { 2387 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 2388 int err; 2389 2390 err = pinctrl_pm_select_default_state(dev); 2391 if (err) 2392 return err; 2393 2394 err = clk_bulk_enable(i2c_dev->nclocks, i2c_dev->clocks); 2395 if (err) 2396 return err; 2397 2398 /* 2399 * VI I2C device is attached to VE power domain which goes through 2400 * power ON/OFF during runtime PM resume/suspend, meaning that 2401 * controller needs to be re-initialized after power ON. 2402 */ 2403 if (IS_VI(i2c_dev)) { 2404 err = tegra_i2c_init(i2c_dev); 2405 if (err) 2406 goto disable_clocks; 2407 } 2408 2409 return 0; 2410 2411 disable_clocks: 2412 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks); 2413 2414 return err; 2415 } 2416 2417 static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev) 2418 { 2419 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 2420 2421 clk_bulk_disable(i2c_dev->nclocks, i2c_dev->clocks); 2422 2423 return pinctrl_pm_select_idle_state(dev); 2424 } 2425 2426 static int __maybe_unused tegra_i2c_suspend(struct device *dev) 2427 { 2428 /* 2429 * Bring the controller up and hold a usage count so it stays 2430 * available until the noirq phase. 2431 */ 2432 return pm_runtime_resume_and_get(dev); 2433 } 2434 2435 static int __maybe_unused tegra_i2c_suspend_noirq(struct device *dev) 2436 { 2437 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 2438 2439 i2c_mark_adapter_suspended(&i2c_dev->adapter); 2440 2441 /* 2442 * Runtime PM is already disabled at this point, so invoke the 2443 * runtime_suspend callback directly to put the controller down. 2444 */ 2445 return tegra_i2c_runtime_suspend(dev); 2446 } 2447 2448 static int __maybe_unused tegra_i2c_resume_noirq(struct device *dev) 2449 { 2450 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); 2451 int err; 2452 2453 /* 2454 * Runtime PM is still disabled at this point, so invoke the 2455 * runtime_resume callback directly to bring the controller back up 2456 * before re-initializing the hardware. The adapter is then marked 2457 * resumed so that consumers can issue transfers from their own 2458 * resume_noirq() handlers and onwards. 2459 */ 2460 err = tegra_i2c_runtime_resume(dev); 2461 if (err) 2462 return err; 2463 2464 err = tegra_i2c_init(i2c_dev); 2465 if (err) 2466 return err; 2467 2468 i2c_mark_adapter_resumed(&i2c_dev->adapter); 2469 2470 return 0; 2471 } 2472 2473 static int __maybe_unused tegra_i2c_resume(struct device *dev) 2474 { 2475 pm_runtime_put(dev); 2476 2477 return 0; 2478 } 2479 2480 static const struct dev_pm_ops tegra_i2c_pm = { 2481 SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume) 2482 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend_noirq, 2483 tegra_i2c_resume_noirq) 2484 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume, 2485 NULL) 2486 }; 2487 2488 static const struct acpi_device_id tegra_i2c_acpi_match[] = { 2489 {.id = "NVDA0101", .driver_data = (kernel_ulong_t)&tegra210_i2c_hw}, 2490 {.id = "NVDA0201", .driver_data = (kernel_ulong_t)&tegra186_i2c_hw}, 2491 {.id = "NVDA0301", .driver_data = (kernel_ulong_t)&tegra194_i2c_hw}, 2492 {.id = "NVDA2017", .driver_data = (kernel_ulong_t)&tegra410_i2c_hw}, 2493 { } 2494 }; 2495 MODULE_DEVICE_TABLE(acpi, tegra_i2c_acpi_match); 2496 2497 static struct platform_driver tegra_i2c_driver = { 2498 .probe = tegra_i2c_probe, 2499 .remove = tegra_i2c_remove, 2500 .driver = { 2501 .name = "tegra-i2c", 2502 .of_match_table = tegra_i2c_of_match, 2503 .acpi_match_table = tegra_i2c_acpi_match, 2504 .pm = &tegra_i2c_pm, 2505 }, 2506 }; 2507 module_platform_driver(tegra_i2c_driver); 2508 2509 MODULE_DESCRIPTION("NVIDIA Tegra I2C Bus Controller driver"); 2510 MODULE_AUTHOR("Colin Cross"); 2511 MODULE_LICENSE("GPL v2"); 2512