Thierry Reding | d6f0453 | 2018-12-17 15:16:54 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 2 | /* |
| 3 | * drivers/i2c/busses/i2c-tegra.c |
| 4 | * |
| 5 | * Copyright (C) 2010 Google, Inc. |
| 6 | * Author: Colin Cross <ccross@android.com> |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 9 | #include <linux/clk.h> |
Sowjanya Komatineni | ca86554 | 2019-02-12 11:06:42 -0800 | [diff] [blame] | 10 | #include <linux/delay.h> |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 11 | #include <linux/dmaengine.h> |
| 12 | #include <linux/dma-mapping.h> |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 13 | #include <linux/err.h> |
| 14 | #include <linux/i2c.h> |
Sowjanya Komatineni | ca86554 | 2019-02-12 11:06:42 -0800 | [diff] [blame] | 15 | #include <linux/init.h> |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Sowjanya Komatineni | ca86554 | 2019-02-12 11:06:42 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
Shardar Shariff Md | 685143a1 | 2016-08-31 18:58:40 +0530 | [diff] [blame] | 18 | #include <linux/iopoll.h> |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 19 | #include <linux/irq.h> |
Sowjanya Komatineni | ca86554 | 2019-02-12 11:06:42 -0800 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 21 | #include <linux/ktime.h> |
Sowjanya Komatineni | ca86554 | 2019-02-12 11:06:42 -0800 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/pinctrl/consumer.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | #include <linux/reset.h> |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 28 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 29 | #define BYTES_PER_FIFO_WORD 4 |
| 30 | |
| 31 | #define I2C_CNFG 0x000 |
Jay Cheng | 40abcf7 | 2011-04-25 15:32:27 -0600 | [diff] [blame] | 32 | #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 33 | #define I2C_CNFG_PACKET_MODE_EN BIT(10) |
| 34 | #define I2C_CNFG_NEW_MASTER_FSM BIT(11) |
| 35 | #define I2C_CNFG_MULTI_MASTER_MODE BIT(17) |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 36 | #define I2C_STATUS 0x01C |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 37 | #define I2C_SL_CNFG 0x020 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 38 | #define I2C_SL_CNFG_NACK BIT(1) |
| 39 | #define I2C_SL_CNFG_NEWSL BIT(2) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 40 | #define I2C_SL_ADDR1 0x02c |
Stephen Warren | 5afa9d3 | 2011-06-06 11:25:19 -0600 | [diff] [blame] | 41 | #define I2C_SL_ADDR2 0x030 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 42 | #define I2C_TX_FIFO 0x050 |
| 43 | #define I2C_RX_FIFO 0x054 |
| 44 | #define I2C_PACKET_TRANSFER_STATUS 0x058 |
| 45 | #define I2C_FIFO_CONTROL 0x05c |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 46 | #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1) |
| 47 | #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0) |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 48 | #define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5) |
| 49 | #define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 50 | #define I2C_FIFO_STATUS 0x060 |
| 51 | #define I2C_FIFO_STATUS_TX_MASK 0xF0 |
| 52 | #define I2C_FIFO_STATUS_TX_SHIFT 4 |
| 53 | #define I2C_FIFO_STATUS_RX_MASK 0x0F |
| 54 | #define I2C_FIFO_STATUS_RX_SHIFT 0 |
| 55 | #define I2C_INT_MASK 0x064 |
| 56 | #define I2C_INT_STATUS 0x068 |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 57 | #define I2C_INT_BUS_CLR_DONE BIT(11) |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 58 | #define I2C_INT_PACKET_XFER_COMPLETE BIT(7) |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 59 | #define I2C_INT_NO_ACK BIT(3) |
| 60 | #define I2C_INT_ARBITRATION_LOST BIT(2) |
| 61 | #define I2C_INT_TX_FIFO_DATA_REQ BIT(1) |
| 62 | #define I2C_INT_RX_FIFO_DATA_REQ BIT(0) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 63 | #define I2C_CLK_DIVISOR 0x06c |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 64 | #define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16 |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 65 | |
| 66 | #define DVC_CTRL_REG1 0x000 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 67 | #define DVC_CTRL_REG1_INTR_EN BIT(10) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 68 | #define DVC_CTRL_REG3 0x008 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 69 | #define DVC_CTRL_REG3_SW_PROG BIT(26) |
| 70 | #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 71 | #define DVC_STATUS 0x00c |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 72 | #define DVC_STATUS_I2C_DONE_INTR BIT(30) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 73 | |
| 74 | #define I2C_ERR_NONE 0x00 |
Bitan Biswas | c84663c | 2019-06-11 03:51:08 -0700 | [diff] [blame] | 75 | #define I2C_ERR_NO_ACK BIT(0) |
| 76 | #define I2C_ERR_ARBITRATION_LOST BIT(1) |
| 77 | #define I2C_ERR_UNKNOWN_INTERRUPT BIT(2) |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 78 | #define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 79 | |
| 80 | #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28 |
| 81 | #define PACKET_HEADER0_PACKET_ID_SHIFT 16 |
| 82 | #define PACKET_HEADER0_CONT_ID_SHIFT 12 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 83 | #define PACKET_HEADER0_PROTOCOL_I2C BIT(4) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 84 | |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 85 | #define I2C_HEADER_CONT_ON_NAK BIT(21) |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 86 | #define I2C_HEADER_READ BIT(19) |
| 87 | #define I2C_HEADER_10BIT_ADDR BIT(18) |
| 88 | #define I2C_HEADER_IE_ENABLE BIT(17) |
| 89 | #define I2C_HEADER_REPEAT_START BIT(16) |
| 90 | #define I2C_HEADER_CONTINUE_XFER BIT(15) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 91 | #define I2C_HEADER_SLAVE_ADDR_SHIFT 1 |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 92 | |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 93 | #define I2C_BUS_CLEAR_CNFG 0x084 |
| 94 | #define I2C_BC_SCLK_THRESHOLD 9 |
| 95 | #define I2C_BC_SCLK_THRESHOLD_SHIFT 16 |
| 96 | #define I2C_BC_STOP_COND BIT(2) |
| 97 | #define I2C_BC_TERMINATE BIT(1) |
| 98 | #define I2C_BC_ENABLE BIT(0) |
| 99 | #define I2C_BUS_CLEAR_STATUS 0x088 |
| 100 | #define I2C_BC_STATUS BIT(0) |
| 101 | |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 102 | #define I2C_CONFIG_LOAD 0x08C |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 103 | #define I2C_MSTR_CONFIG_LOAD BIT(0) |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 104 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 105 | #define I2C_CLKEN_OVERRIDE 0x090 |
Jon Hunter | 2929be2 | 2016-08-26 14:08:58 +0100 | [diff] [blame] | 106 | #define I2C_MST_CORE_CLKEN_OVR BIT(0) |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 107 | |
Shardar Shariff Md | 685143a1 | 2016-08-31 18:58:40 +0530 | [diff] [blame] | 108 | #define I2C_CONFIG_LOAD_TIMEOUT 1000000 |
| 109 | |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 110 | #define I2C_MST_FIFO_CONTROL 0x0b4 |
| 111 | #define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0) |
| 112 | #define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1) |
| 113 | #define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4) |
| 114 | #define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16) |
| 115 | |
| 116 | #define I2C_MST_FIFO_STATUS 0x0b8 |
| 117 | #define I2C_MST_FIFO_STATUS_RX_MASK 0xff |
| 118 | #define I2C_MST_FIFO_STATUS_RX_SHIFT 0 |
| 119 | #define I2C_MST_FIFO_STATUS_TX_MASK 0xff0000 |
| 120 | #define I2C_MST_FIFO_STATUS_TX_SHIFT 16 |
| 121 | |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 122 | #define I2C_INTERFACE_TIMING_0 0x94 |
| 123 | #define I2C_THIGH_SHIFT 8 |
| 124 | #define I2C_INTERFACE_TIMING_1 0x98 |
| 125 | |
Sowjanya Komatineni | b03ff2a | 2019-02-12 11:06:45 -0800 | [diff] [blame] | 126 | /* Packet header size in bytes */ |
| 127 | #define I2C_PACKET_HEADER_SIZE 12 |
| 128 | |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 129 | /* |
Dmitry Osipenko | ae6028a | 2020-01-14 04:34:39 +0300 | [diff] [blame] | 130 | * I2C Controller will use PIO mode for transfers up to 32 bytes in order to |
| 131 | * avoid DMA overhead, otherwise external APB DMA controller will be used. |
| 132 | * Note that the actual MAX PIO length is 20 bytes because 32 bytes include |
| 133 | * I2C_PACKET_HEADER_SIZE. |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 134 | */ |
Dmitry Osipenko | ae6028a | 2020-01-14 04:34:39 +0300 | [diff] [blame] | 135 | #define I2C_PIO_MODE_PREFERRED_LEN 32 |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 136 | |
| 137 | /* |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 138 | * msg_end_type: The bus control which need to be send at end of transfer. |
| 139 | * @MSG_END_STOP: Send stop pulse at end of transfer. |
| 140 | * @MSG_END_REPEAT_START: Send repeat start at end of transfer. |
| 141 | * @MSG_END_CONTINUE: The following on message is coming and so do not send |
| 142 | * stop or repeat start. |
| 143 | */ |
| 144 | enum msg_end_type { |
| 145 | MSG_END_STOP, |
| 146 | MSG_END_REPEAT_START, |
| 147 | MSG_END_CONTINUE, |
| 148 | }; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 149 | |
| 150 | /** |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 151 | * struct tegra_i2c_hw_feature : Different HW support on Tegra |
| 152 | * @has_continue_xfer_support: Continue transfer supports. |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 153 | * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer |
| 154 | * complete interrupt per packet basis. |
Thierry Reding | c990bba | 2018-12-17 15:16:52 +0100 | [diff] [blame] | 155 | * @has_single_clk_source: The I2C controller has single clock source. Tegra30 |
| 156 | * and earlier SoCs have two clock sources i.e. div-clk and |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 157 | * fast-clk. |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 158 | * @has_config_load_reg: Has the config load register to load the new |
| 159 | * configuration. |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 160 | * @clk_divisor_hs_mode: Clock divisor in HS mode. |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 161 | * @clk_divisor_std_mode: Clock divisor in standard mode. It is |
| 162 | * applicable if there is no fast clock source i.e. single clock |
| 163 | * source. |
| 164 | * @clk_divisor_fast_mode: Clock divisor in fast mode. It is |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 165 | * applicable if there is no fast clock source i.e. single clock |
| 166 | * source. |
Thierry Reding | 0604ee4 | 2018-12-17 15:16:53 +0100 | [diff] [blame] | 167 | * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is |
| 168 | * applicable if there is no fast clock source (i.e. single |
| 169 | * clock source). |
| 170 | * @has_multi_master_mode: The I2C controller supports running in single-master |
| 171 | * or multi-master mode. |
| 172 | * @has_slcg_override_reg: The I2C controller supports a register that |
| 173 | * overrides the second level clock gating. |
| 174 | * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that |
| 175 | * provides additional features and allows for longer messages to |
| 176 | * be transferred in one go. |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 177 | * @quirks: i2c adapter quirks for limiting write/read transfer size and not |
| 178 | * allowing 0 length transfers. |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 179 | * @supports_bus_clear: Bus Clear support to recover from bus hang during |
| 180 | * SDA stuck low from device for some unknown reasons. |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 181 | * @has_apb_dma: Support of APBDMA on corresponding Tegra chip. |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 182 | * @tlow_std_mode: Low period of the clock in standard mode. |
| 183 | * @thigh_std_mode: High period of the clock in standard mode. |
| 184 | * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes. |
| 185 | * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes. |
| 186 | * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions |
| 187 | * in standard mode. |
| 188 | * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop |
| 189 | * conditions in fast/fast-plus modes. |
| 190 | * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions |
| 191 | * in HS mode. |
| 192 | * @has_interface_timing_reg: Has interface timing register to program the tuned |
| 193 | * timing settings. |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 194 | */ |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 195 | struct tegra_i2c_hw_feature { |
| 196 | bool has_continue_xfer_support; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 197 | bool has_per_pkt_xfer_complete_irq; |
| 198 | bool has_single_clk_source; |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 199 | bool has_config_load_reg; |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 200 | int clk_divisor_hs_mode; |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 201 | int clk_divisor_std_mode; |
| 202 | int clk_divisor_fast_mode; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 203 | u16 clk_divisor_fast_plus_mode; |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 204 | bool has_multi_master_mode; |
| 205 | bool has_slcg_override_reg; |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 206 | bool has_mst_fifo; |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 207 | const struct i2c_adapter_quirks *quirks; |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 208 | bool supports_bus_clear; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 209 | bool has_apb_dma; |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 210 | u8 tlow_std_mode; |
| 211 | u8 thigh_std_mode; |
| 212 | u8 tlow_fast_fastplus_mode; |
| 213 | u8 thigh_fast_fastplus_mode; |
| 214 | u32 setup_hold_time_std_mode; |
| 215 | u32 setup_hold_time_fast_fast_plus_mode; |
| 216 | u32 setup_hold_time_hs_mode; |
| 217 | bool has_interface_timing_reg; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | /** |
Thierry Reding | c990bba | 2018-12-17 15:16:52 +0100 | [diff] [blame] | 221 | * struct tegra_i2c_dev - per device I2C context |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 222 | * @dev: device reference for power management |
Thierry Reding | c990bba | 2018-12-17 15:16:52 +0100 | [diff] [blame] | 223 | * @hw: Tegra I2C HW feature |
| 224 | * @adapter: core I2C layer adapter information |
| 225 | * @div_clk: clock reference for div clock of I2C controller |
| 226 | * @fast_clk: clock reference for fast clock of I2C controller |
Thierry Reding | 0604ee4 | 2018-12-17 15:16:53 +0100 | [diff] [blame] | 227 | * @rst: reset control for the I2C controller |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 228 | * @base: ioremapped registers cookie |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 229 | * @base_phys: physical base address of the I2C controller |
Thierry Reding | c990bba | 2018-12-17 15:16:52 +0100 | [diff] [blame] | 230 | * @cont_id: I2C controller ID, used for packet header |
| 231 | * @irq: IRQ number of transfer complete interrupt |
| 232 | * @is_dvc: identifies the DVC I2C controller, has a different register layout |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 233 | * @msg_complete: transfer completion notifier |
| 234 | * @msg_err: error code for completed message |
| 235 | * @msg_buf: pointer to current message data |
| 236 | * @msg_buf_remaining: size of unsent data in the message buffer |
| 237 | * @msg_read: identifies read transfers |
Thierry Reding | c990bba | 2018-12-17 15:16:52 +0100 | [diff] [blame] | 238 | * @bus_clk_rate: current I2C bus clock rate |
Thierry Reding | 0604ee4 | 2018-12-17 15:16:53 +0100 | [diff] [blame] | 239 | * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes |
| 240 | * @is_multimaster_mode: track if I2C controller is in multi-master mode |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 241 | * @tx_dma_chan: DMA transmit channel |
| 242 | * @rx_dma_chan: DMA receive channel |
| 243 | * @dma_phys: handle to DMA resources |
| 244 | * @dma_buf: pointer to allocated DMA buffer |
| 245 | * @dma_buf_size: DMA buffer size |
| 246 | * @is_curr_dma_xfer: indicates active DMA transfer |
| 247 | * @dma_complete: DMA completion notifier |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 248 | * @is_curr_atomic_xfer: indicates active atomic transfer |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 249 | */ |
| 250 | struct tegra_i2c_dev { |
| 251 | struct device *dev; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 252 | const struct tegra_i2c_hw_feature *hw; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 253 | struct i2c_adapter adapter; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 254 | struct clk *div_clk; |
| 255 | struct clk *fast_clk; |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 256 | struct reset_control *rst; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 257 | void __iomem *base; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 258 | phys_addr_t base_phys; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 259 | int cont_id; |
| 260 | int irq; |
| 261 | int is_dvc; |
| 262 | struct completion msg_complete; |
| 263 | int msg_err; |
| 264 | u8 *msg_buf; |
| 265 | size_t msg_buf_remaining; |
| 266 | int msg_read; |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 267 | u32 bus_clk_rate; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 268 | u16 clk_divisor_non_hs_mode; |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 269 | bool is_multimaster_mode; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 270 | struct dma_chan *tx_dma_chan; |
| 271 | struct dma_chan *rx_dma_chan; |
| 272 | dma_addr_t dma_phys; |
| 273 | u32 *dma_buf; |
| 274 | unsigned int dma_buf_size; |
| 275 | bool is_curr_dma_xfer; |
| 276 | struct completion dma_complete; |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 277 | bool is_curr_atomic_xfer; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
Jon Hunter | c7ae44e8 | 2016-08-26 14:08:57 +0100 | [diff] [blame] | 280 | static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val, |
| 281 | unsigned long reg) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 282 | { |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 283 | writel_relaxed(val, i2c_dev->base + reg); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg) |
| 287 | { |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 288 | return readl_relaxed(i2c_dev->base + reg); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * i2c_writel and i2c_readl will offset the register if necessary to talk |
| 293 | * to the I2C block inside the DVC block |
| 294 | */ |
| 295 | static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev, |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 296 | unsigned long reg) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 297 | { |
| 298 | if (i2c_dev->is_dvc) |
| 299 | reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40; |
| 300 | return reg; |
| 301 | } |
| 302 | |
| 303 | static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val, |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 304 | unsigned long reg) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 305 | { |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 306 | writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
Laxman Dewangan | ec7aaca | 2012-06-13 15:42:36 +0530 | [diff] [blame] | 307 | |
| 308 | /* Read back register to make sure that register writes completed */ |
| 309 | if (reg != I2C_TX_FIFO) |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 310 | readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg) |
| 314 | { |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 315 | return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg)); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data, |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 319 | unsigned long reg, int len) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 320 | { |
| 321 | writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); |
| 322 | } |
| 323 | |
| 324 | static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data, |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 325 | unsigned long reg, int len) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 326 | { |
| 327 | readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len); |
| 328 | } |
| 329 | |
| 330 | static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) |
| 331 | { |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 332 | u32 int_mask; |
| 333 | |
| 334 | int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 335 | i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); |
| 336 | } |
| 337 | |
| 338 | static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask) |
| 339 | { |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 340 | u32 int_mask; |
| 341 | |
| 342 | int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 343 | i2c_writel(i2c_dev, int_mask, I2C_INT_MASK); |
| 344 | } |
| 345 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 346 | static void tegra_i2c_dma_complete(void *args) |
| 347 | { |
| 348 | struct tegra_i2c_dev *i2c_dev = args; |
| 349 | |
| 350 | complete(&i2c_dev->dma_complete); |
| 351 | } |
| 352 | |
| 353 | static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len) |
| 354 | { |
| 355 | struct dma_async_tx_descriptor *dma_desc; |
| 356 | enum dma_transfer_direction dir; |
| 357 | struct dma_chan *chan; |
| 358 | |
| 359 | dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len); |
| 360 | reinit_completion(&i2c_dev->dma_complete); |
| 361 | dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; |
| 362 | chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan; |
| 363 | dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys, |
| 364 | len, dir, DMA_PREP_INTERRUPT | |
| 365 | DMA_CTRL_ACK); |
| 366 | if (!dma_desc) { |
| 367 | dev_err(i2c_dev->dev, "failed to get DMA descriptor\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
| 371 | dma_desc->callback = tegra_i2c_dma_complete; |
| 372 | dma_desc->callback_param = i2c_dev; |
| 373 | dmaengine_submit(dma_desc); |
| 374 | dma_async_issue_pending(chan); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev) |
| 379 | { |
| 380 | if (i2c_dev->dma_buf) { |
| 381 | dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size, |
| 382 | i2c_dev->dma_buf, i2c_dev->dma_phys); |
| 383 | i2c_dev->dma_buf = NULL; |
| 384 | } |
| 385 | |
| 386 | if (i2c_dev->tx_dma_chan) { |
| 387 | dma_release_channel(i2c_dev->tx_dma_chan); |
| 388 | i2c_dev->tx_dma_chan = NULL; |
| 389 | } |
| 390 | |
| 391 | if (i2c_dev->rx_dma_chan) { |
| 392 | dma_release_channel(i2c_dev->rx_dma_chan); |
| 393 | i2c_dev->rx_dma_chan = NULL; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev) |
| 398 | { |
| 399 | struct dma_chan *chan; |
| 400 | u32 *dma_buf; |
| 401 | dma_addr_t dma_phys; |
| 402 | int err; |
| 403 | |
Jonathan Hunter | 89328b1 | 2019-02-21 15:00:38 +0000 | [diff] [blame] | 404 | if (!i2c_dev->hw->has_apb_dma) |
| 405 | return 0; |
| 406 | |
| 407 | if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) { |
| 408 | dev_dbg(i2c_dev->dev, "Support for APB DMA not enabled!\n"); |
| 409 | return 0; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Peter Ujfalusi | 79e4be2 | 2019-11-13 11:22:35 +0200 | [diff] [blame] | 412 | chan = dma_request_chan(i2c_dev->dev, "rx"); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 413 | if (IS_ERR(chan)) { |
| 414 | err = PTR_ERR(chan); |
| 415 | goto err_out; |
| 416 | } |
| 417 | |
| 418 | i2c_dev->rx_dma_chan = chan; |
| 419 | |
Peter Ujfalusi | 79e4be2 | 2019-11-13 11:22:35 +0200 | [diff] [blame] | 420 | chan = dma_request_chan(i2c_dev->dev, "tx"); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 421 | if (IS_ERR(chan)) { |
| 422 | err = PTR_ERR(chan); |
| 423 | goto err_out; |
| 424 | } |
| 425 | |
| 426 | i2c_dev->tx_dma_chan = chan; |
| 427 | |
| 428 | dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size, |
| 429 | &dma_phys, GFP_KERNEL | __GFP_NOWARN); |
| 430 | if (!dma_buf) { |
| 431 | dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n"); |
| 432 | err = -ENOMEM; |
| 433 | goto err_out; |
| 434 | } |
| 435 | |
| 436 | i2c_dev->dma_buf = dma_buf; |
| 437 | i2c_dev->dma_phys = dma_phys; |
| 438 | return 0; |
| 439 | |
| 440 | err_out: |
| 441 | tegra_i2c_release_dma(i2c_dev); |
| 442 | if (err != -EPROBE_DEFER) { |
| 443 | dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err); |
Colin Ian King | bb0e9b1 | 2019-02-15 15:31:26 +0000 | [diff] [blame] | 444 | dev_err(i2c_dev->dev, "falling back to PIO\n"); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | return err; |
| 449 | } |
| 450 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 451 | static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev) |
| 452 | { |
| 453 | unsigned long timeout = jiffies + HZ; |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 454 | unsigned int offset; |
| 455 | u32 mask, val; |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 456 | |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 457 | if (i2c_dev->hw->has_mst_fifo) { |
| 458 | mask = I2C_MST_FIFO_CONTROL_TX_FLUSH | |
| 459 | I2C_MST_FIFO_CONTROL_RX_FLUSH; |
| 460 | offset = I2C_MST_FIFO_CONTROL; |
| 461 | } else { |
| 462 | mask = I2C_FIFO_CONTROL_TX_FLUSH | |
| 463 | I2C_FIFO_CONTROL_RX_FLUSH; |
| 464 | offset = I2C_FIFO_CONTROL; |
| 465 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 466 | |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 467 | val = i2c_readl(i2c_dev, offset); |
| 468 | val |= mask; |
| 469 | i2c_writel(i2c_dev, val, offset); |
| 470 | |
| 471 | while (i2c_readl(i2c_dev, offset) & mask) { |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 472 | if (time_after(jiffies, timeout)) { |
| 473 | dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n"); |
| 474 | return -ETIMEDOUT; |
| 475 | } |
Bitan Biswas | fbbe494 | 2019-06-11 03:51:12 -0700 | [diff] [blame] | 476 | usleep_range(1000, 2000); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 477 | } |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev) |
| 482 | { |
| 483 | u32 val; |
| 484 | int rx_fifo_avail; |
| 485 | u8 *buf = i2c_dev->msg_buf; |
| 486 | size_t buf_remaining = i2c_dev->msg_buf_remaining; |
| 487 | int words_to_transfer; |
| 488 | |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 489 | /* |
| 490 | * Catch overflow due to message fully sent |
| 491 | * before the check for RX FIFO availability. |
| 492 | */ |
| 493 | if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining))) |
| 494 | return -EINVAL; |
| 495 | |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 496 | if (i2c_dev->hw->has_mst_fifo) { |
| 497 | val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS); |
| 498 | rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >> |
| 499 | I2C_MST_FIFO_STATUS_RX_SHIFT; |
| 500 | } else { |
| 501 | val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); |
| 502 | rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >> |
| 503 | I2C_FIFO_STATUS_RX_SHIFT; |
| 504 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 505 | |
| 506 | /* Rounds down to not include partial word at the end of buf */ |
| 507 | words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; |
| 508 | if (words_to_transfer > rx_fifo_avail) |
| 509 | words_to_transfer = rx_fifo_avail; |
| 510 | |
| 511 | i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer); |
| 512 | |
| 513 | buf += words_to_transfer * BYTES_PER_FIFO_WORD; |
| 514 | buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; |
| 515 | rx_fifo_avail -= words_to_transfer; |
| 516 | |
| 517 | /* |
| 518 | * If there is a partial word at the end of buf, handle it manually to |
| 519 | * prevent overwriting past the end of buf |
| 520 | */ |
| 521 | if (rx_fifo_avail > 0 && buf_remaining > 0) { |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 522 | /* |
| 523 | * buf_remaining > 3 check not needed as rx_fifo_avail == 0 |
| 524 | * when (words_to_transfer was > rx_fifo_avail) earlier |
| 525 | * in this function. |
| 526 | */ |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 527 | val = i2c_readl(i2c_dev, I2C_RX_FIFO); |
Dmitry Osipenko | 8c340f6 | 2015-01-26 19:55:02 +0300 | [diff] [blame] | 528 | val = cpu_to_le32(val); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 529 | memcpy(buf, &val, buf_remaining); |
| 530 | buf_remaining = 0; |
| 531 | rx_fifo_avail--; |
| 532 | } |
| 533 | |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 534 | /* RX FIFO must be drained, otherwise it's an Overflow case. */ |
| 535 | if (WARN_ON_ONCE(rx_fifo_avail)) |
| 536 | return -EINVAL; |
| 537 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 538 | i2c_dev->msg_buf_remaining = buf_remaining; |
| 539 | i2c_dev->msg_buf = buf; |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 540 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev) |
| 545 | { |
| 546 | u32 val; |
| 547 | int tx_fifo_avail; |
| 548 | u8 *buf = i2c_dev->msg_buf; |
| 549 | size_t buf_remaining = i2c_dev->msg_buf_remaining; |
| 550 | int words_to_transfer; |
| 551 | |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 552 | if (i2c_dev->hw->has_mst_fifo) { |
| 553 | val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS); |
| 554 | tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >> |
| 555 | I2C_MST_FIFO_STATUS_TX_SHIFT; |
| 556 | } else { |
| 557 | val = i2c_readl(i2c_dev, I2C_FIFO_STATUS); |
| 558 | tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >> |
| 559 | I2C_FIFO_STATUS_TX_SHIFT; |
| 560 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 561 | |
| 562 | /* Rounds down to not include partial word at the end of buf */ |
| 563 | words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 564 | |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 565 | /* It's very common to have < 4 bytes, so optimize that case. */ |
| 566 | if (words_to_transfer) { |
| 567 | if (words_to_transfer > tx_fifo_avail) |
| 568 | words_to_transfer = tx_fifo_avail; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 569 | |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 570 | /* |
| 571 | * Update state before writing to FIFO. If this casues us |
| 572 | * to finish writing all bytes (AKA buf_remaining goes to 0) we |
| 573 | * have a potential for an interrupt (PACKET_XFER_COMPLETE is |
| 574 | * not maskable). We need to make sure that the isr sees |
| 575 | * buf_remaining as 0 and doesn't call us back re-entrantly. |
| 576 | */ |
| 577 | buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD; |
| 578 | tx_fifo_avail -= words_to_transfer; |
| 579 | i2c_dev->msg_buf_remaining = buf_remaining; |
| 580 | i2c_dev->msg_buf = buf + |
| 581 | words_to_transfer * BYTES_PER_FIFO_WORD; |
| 582 | barrier(); |
| 583 | |
| 584 | i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer); |
| 585 | |
| 586 | buf += words_to_transfer * BYTES_PER_FIFO_WORD; |
| 587 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 588 | |
| 589 | /* |
| 590 | * If there is a partial word at the end of buf, handle it manually to |
| 591 | * prevent reading past the end of buf, which could cross a page |
| 592 | * boundary and fault. |
| 593 | */ |
| 594 | if (tx_fifo_avail > 0 && buf_remaining > 0) { |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 595 | /* |
| 596 | * buf_remaining > 3 check not needed as tx_fifo_avail == 0 |
| 597 | * when (words_to_transfer was > tx_fifo_avail) earlier |
| 598 | * in this function for non-zero words_to_transfer. |
| 599 | */ |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 600 | memcpy(&val, buf, buf_remaining); |
Dmitry Osipenko | 8c340f6 | 2015-01-26 19:55:02 +0300 | [diff] [blame] | 601 | val = le32_to_cpu(val); |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 602 | |
| 603 | /* Again update before writing to FIFO to make sure isr sees. */ |
| 604 | i2c_dev->msg_buf_remaining = 0; |
| 605 | i2c_dev->msg_buf = NULL; |
| 606 | barrier(); |
| 607 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 608 | i2c_writel(i2c_dev, val, I2C_TX_FIFO); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller) |
| 616 | * block. This block is identical to the rest of the I2C blocks, except that |
| 617 | * it only supports master mode, it has registers moved around, and it needs |
| 618 | * some extra init to get it into I2C mode. The register moves are handled |
| 619 | * by i2c_readl and i2c_writel |
| 620 | */ |
| 621 | static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev) |
| 622 | { |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 623 | u32 val; |
| 624 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 625 | val = dvc_readl(i2c_dev, DVC_CTRL_REG3); |
| 626 | val |= DVC_CTRL_REG3_SW_PROG; |
| 627 | val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN; |
| 628 | dvc_writel(i2c_dev, val, DVC_CTRL_REG3); |
| 629 | |
| 630 | val = dvc_readl(i2c_dev, DVC_CTRL_REG1); |
| 631 | val |= DVC_CTRL_REG1_INTR_EN; |
| 632 | dvc_writel(i2c_dev, val, DVC_CTRL_REG1); |
| 633 | } |
| 634 | |
Dmitry Osipenko | e5738bc | 2019-07-08 02:12:34 +0300 | [diff] [blame] | 635 | static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev) |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 636 | { |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 637 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 638 | int ret; |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 639 | |
Jon Hunter | 718917b | 2016-08-26 14:09:05 +0100 | [diff] [blame] | 640 | ret = pinctrl_pm_select_default_state(i2c_dev->dev); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 644 | if (!i2c_dev->hw->has_single_clk_source) { |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 645 | ret = clk_enable(i2c_dev->fast_clk); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 646 | if (ret < 0) { |
| 647 | dev_err(i2c_dev->dev, |
| 648 | "Enabling fast clk failed, err %d\n", ret); |
| 649 | return ret; |
| 650 | } |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 651 | } |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 652 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 653 | ret = clk_enable(i2c_dev->div_clk); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 654 | if (ret < 0) { |
| 655 | dev_err(i2c_dev->dev, |
| 656 | "Enabling div clk failed, err %d\n", ret); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 657 | clk_disable(i2c_dev->fast_clk); |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 658 | return ret; |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 659 | } |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 660 | |
| 661 | return 0; |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 662 | } |
| 663 | |
Dmitry Osipenko | e5738bc | 2019-07-08 02:12:34 +0300 | [diff] [blame] | 664 | static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev) |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 665 | { |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 666 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
| 667 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 668 | clk_disable(i2c_dev->div_clk); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 669 | if (!i2c_dev->hw->has_single_clk_source) |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 670 | clk_disable(i2c_dev->fast_clk); |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 671 | |
Jon Hunter | 718917b | 2016-08-26 14:09:05 +0100 | [diff] [blame] | 672 | return pinctrl_pm_select_idle_state(i2c_dev->dev); |
Laxman Dewangan | fd301cc | 2012-08-19 00:47:47 +0530 | [diff] [blame] | 673 | } |
| 674 | |
Shardar Shariff Md | 89120d6 | 2016-08-31 18:58:42 +0530 | [diff] [blame] | 675 | static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev) |
| 676 | { |
| 677 | unsigned long reg_offset; |
| 678 | void __iomem *addr; |
| 679 | u32 val; |
| 680 | int err; |
| 681 | |
| 682 | if (i2c_dev->hw->has_config_load_reg) { |
| 683 | reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD); |
| 684 | addr = i2c_dev->base + reg_offset; |
| 685 | i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD); |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 686 | |
| 687 | if (i2c_dev->is_curr_atomic_xfer) |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 688 | err = readl_relaxed_poll_timeout_atomic( |
| 689 | addr, val, val == 0, 1000, |
| 690 | I2C_CONFIG_LOAD_TIMEOUT); |
Shardar Shariff Md | 2bc445e | 2016-08-31 18:58:43 +0530 | [diff] [blame] | 691 | else |
Dmitry Osipenko | 4211ffc | 2020-01-14 04:34:40 +0300 | [diff] [blame] | 692 | err = readl_relaxed_poll_timeout( |
| 693 | addr, val, val == 0, 1000, |
| 694 | I2C_CONFIG_LOAD_TIMEOUT); |
Shardar Shariff Md | 2bc445e | 2016-08-31 18:58:43 +0530 | [diff] [blame] | 695 | |
Shardar Shariff Md | 89120d6 | 2016-08-31 18:58:42 +0530 | [diff] [blame] | 696 | if (err) { |
| 697 | dev_warn(i2c_dev->dev, |
| 698 | "timeout waiting for config load\n"); |
| 699 | return err; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 706 | static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 707 | { |
| 708 | u32 val; |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 709 | int err; |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 710 | u32 clk_divisor, clk_multiplier; |
Bitan Biswas | 233d0ab | 2019-06-11 03:51:09 -0700 | [diff] [blame] | 711 | u32 tsu_thd; |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 712 | u8 tlow, thigh; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 713 | |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 714 | reset_control_assert(i2c_dev->rst); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 715 | udelay(2); |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 716 | reset_control_deassert(i2c_dev->rst); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 717 | |
| 718 | if (i2c_dev->is_dvc) |
| 719 | tegra_dvc_init(i2c_dev); |
| 720 | |
Jay Cheng | 40abcf7 | 2011-04-25 15:32:27 -0600 | [diff] [blame] | 721 | val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN | |
| 722 | (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT); |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 723 | |
| 724 | if (i2c_dev->hw->has_multi_master_mode) |
| 725 | val |= I2C_CNFG_MULTI_MASTER_MODE; |
| 726 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 727 | i2c_writel(i2c_dev, val, I2C_CNFG); |
| 728 | i2c_writel(i2c_dev, 0, I2C_INT_MASK); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 729 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 730 | /* Make sure clock divisor programmed correctly */ |
| 731 | clk_divisor = i2c_dev->hw->clk_divisor_hs_mode; |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 732 | clk_divisor |= i2c_dev->clk_divisor_non_hs_mode << |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 733 | I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT; |
| 734 | i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 735 | |
Andy Shevchenko | 90224e6 | 2020-03-24 14:32:16 +0200 | [diff] [blame] | 736 | if (i2c_dev->bus_clk_rate > I2C_MAX_STANDARD_MODE_FREQ && |
| 737 | i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_PLUS_FREQ) { |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 738 | tlow = i2c_dev->hw->tlow_fast_fastplus_mode; |
| 739 | thigh = i2c_dev->hw->thigh_fast_fastplus_mode; |
| 740 | tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode; |
| 741 | } else { |
| 742 | tlow = i2c_dev->hw->tlow_std_mode; |
| 743 | thigh = i2c_dev->hw->thigh_std_mode; |
| 744 | tsu_thd = i2c_dev->hw->setup_hold_time_std_mode; |
| 745 | } |
| 746 | |
| 747 | if (i2c_dev->hw->has_interface_timing_reg) { |
| 748 | val = (thigh << I2C_THIGH_SHIFT) | tlow; |
| 749 | i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0); |
| 750 | } |
| 751 | |
| 752 | /* |
| 753 | * configure setup and hold times only when tsu_thd is non-zero. |
| 754 | * otherwise, preserve the chip default values |
| 755 | */ |
| 756 | if (i2c_dev->hw->has_interface_timing_reg && tsu_thd) |
| 757 | i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1); |
| 758 | |
| 759 | if (!clk_reinit) { |
| 760 | clk_multiplier = (tlow + thigh + 2); |
| 761 | clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1); |
| 762 | err = clk_set_rate(i2c_dev->div_clk, |
| 763 | i2c_dev->bus_clk_rate * clk_multiplier); |
| 764 | if (err) { |
| 765 | dev_err(i2c_dev->dev, |
| 766 | "failed changing clock rate: %d\n", err); |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 767 | return err; |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Kenneth Waters | 65a1a0a | 2011-04-25 12:29:54 -0600 | [diff] [blame] | 771 | if (!i2c_dev->is_dvc) { |
| 772 | u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG); |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 773 | |
Stephen Warren | 5afa9d3 | 2011-06-06 11:25:19 -0600 | [diff] [blame] | 774 | sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL; |
| 775 | i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG); |
| 776 | i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1); |
| 777 | i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2); |
Kenneth Waters | 65a1a0a | 2011-04-25 12:29:54 -0600 | [diff] [blame] | 778 | } |
| 779 | |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 780 | err = tegra_i2c_flush_fifos(i2c_dev); |
Shardar Shariff Md | 2148c01 | 2016-08-31 18:58:41 +0530 | [diff] [blame] | 781 | if (err) |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 782 | return err; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 783 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 784 | if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg) |
| 785 | i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE); |
| 786 | |
Shardar Shariff Md | 89120d6 | 2016-08-31 18:58:42 +0530 | [diff] [blame] | 787 | err = tegra_i2c_wait_for_config_load(i2c_dev); |
| 788 | if (err) |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 789 | return err; |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 790 | |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 791 | return 0; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 794 | static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev) |
| 795 | { |
| 796 | u32 cnfg; |
| 797 | |
Jon Hunter | 54836e2 | 2018-07-03 09:55:43 +0100 | [diff] [blame] | 798 | /* |
| 799 | * NACK interrupt is generated before the I2C controller generates |
| 800 | * the STOP condition on the bus. So wait for 2 clock periods |
| 801 | * before disabling the controller so that the STOP condition has |
| 802 | * been delivered properly. |
| 803 | */ |
| 804 | udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate)); |
| 805 | |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 806 | cnfg = i2c_readl(i2c_dev, I2C_CNFG); |
| 807 | if (cnfg & I2C_CNFG_PACKET_MODE_EN) |
| 808 | i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG); |
| 809 | |
| 810 | return tegra_i2c_wait_for_config_load(i2c_dev); |
| 811 | } |
| 812 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 813 | static irqreturn_t tegra_i2c_isr(int irq, void *dev_id) |
| 814 | { |
| 815 | u32 status; |
| 816 | const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; |
| 817 | struct tegra_i2c_dev *i2c_dev = dev_id; |
| 818 | |
| 819 | status = i2c_readl(i2c_dev, I2C_INT_STATUS); |
| 820 | |
| 821 | if (status == 0) { |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 822 | dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n", |
| 823 | i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS), |
| 824 | i2c_readl(i2c_dev, I2C_STATUS), |
| 825 | i2c_readl(i2c_dev, I2C_CNFG)); |
| 826 | i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 827 | goto err; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (unlikely(status & status_err)) { |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 831 | tegra_i2c_disable_packet_mode(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 832 | if (status & I2C_INT_NO_ACK) |
| 833 | i2c_dev->msg_err |= I2C_ERR_NO_ACK; |
| 834 | if (status & I2C_INT_ARBITRATION_LOST) |
| 835 | i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 836 | goto err; |
| 837 | } |
| 838 | |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 839 | /* |
| 840 | * I2C transfer is terminated during the bus clear so skip |
| 841 | * processing the other interrupts. |
| 842 | */ |
| 843 | if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE)) |
| 844 | goto err; |
| 845 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 846 | if (!i2c_dev->is_curr_dma_xfer) { |
| 847 | if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) { |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 848 | if (tegra_i2c_empty_rx_fifo(i2c_dev)) { |
| 849 | /* |
| 850 | * Overflow error condition: message fully sent, |
| 851 | * with no XFER_COMPLETE interrupt but hardware |
| 852 | * asks to transfer more. |
| 853 | */ |
| 854 | i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW; |
| 855 | goto err; |
| 856 | } |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 857 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 858 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 859 | if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) { |
| 860 | if (i2c_dev->msg_buf_remaining) |
| 861 | tegra_i2c_fill_tx_fifo(i2c_dev); |
| 862 | else |
| 863 | tegra_i2c_mask_irq(i2c_dev, |
| 864 | I2C_INT_TX_FIFO_DATA_REQ); |
| 865 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Laxman Dewangan | c889e91 | 2012-05-07 12:16:19 +0530 | [diff] [blame] | 868 | i2c_writel(i2c_dev, status, I2C_INT_STATUS); |
| 869 | if (i2c_dev->is_dvc) |
| 870 | dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); |
| 871 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 872 | /* |
| 873 | * During message read XFER_COMPLETE interrupt is triggered prior to |
| 874 | * DMA completion and during message write XFER_COMPLETE interrupt is |
| 875 | * triggered after DMA completion. |
| 876 | * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer. |
| 877 | * so forcing msg_buf_remaining to 0 in DMA mode. |
| 878 | */ |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 879 | if (status & I2C_INT_PACKET_XFER_COMPLETE) { |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 880 | if (i2c_dev->is_curr_dma_xfer) |
| 881 | i2c_dev->msg_buf_remaining = 0; |
Bitan Biswas | 57ca968 | 2019-06-18 04:09:42 -0700 | [diff] [blame] | 882 | /* |
| 883 | * Underflow error condition: XFER_COMPLETE before message |
| 884 | * fully sent. |
| 885 | */ |
| 886 | if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) { |
| 887 | i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT; |
| 888 | goto err; |
| 889 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 890 | complete(&i2c_dev->msg_complete); |
Doug Anderson | 96219c3 | 2011-08-30 11:46:10 -0600 | [diff] [blame] | 891 | } |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 892 | goto done; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 893 | err: |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 894 | /* An error occurred, mask all interrupts */ |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 895 | tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST | |
| 896 | I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ | |
| 897 | I2C_INT_RX_FIFO_DATA_REQ); |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 898 | if (i2c_dev->hw->supports_bus_clear) |
| 899 | tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 900 | i2c_writel(i2c_dev, status, I2C_INT_STATUS); |
Todd Poynor | cb63c62 | 2011-04-25 15:32:25 -0600 | [diff] [blame] | 901 | if (i2c_dev->is_dvc) |
| 902 | dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS); |
Laxman Dewangan | c889e91 | 2012-05-07 12:16:19 +0530 | [diff] [blame] | 903 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 904 | if (i2c_dev->is_curr_dma_xfer) { |
| 905 | if (i2c_dev->msg_read) |
| 906 | dmaengine_terminate_async(i2c_dev->rx_dma_chan); |
| 907 | else |
| 908 | dmaengine_terminate_async(i2c_dev->tx_dma_chan); |
| 909 | |
| 910 | complete(&i2c_dev->dma_complete); |
| 911 | } |
| 912 | |
Laxman Dewangan | c889e91 | 2012-05-07 12:16:19 +0530 | [diff] [blame] | 913 | complete(&i2c_dev->msg_complete); |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 914 | done: |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 915 | return IRQ_HANDLED; |
| 916 | } |
| 917 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 918 | static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev, |
| 919 | size_t len) |
| 920 | { |
| 921 | u32 val, reg; |
| 922 | u8 dma_burst; |
| 923 | struct dma_slave_config slv_config = {0}; |
| 924 | struct dma_chan *chan; |
| 925 | int ret; |
| 926 | unsigned long reg_offset; |
| 927 | |
| 928 | if (i2c_dev->hw->has_mst_fifo) |
| 929 | reg = I2C_MST_FIFO_CONTROL; |
| 930 | else |
| 931 | reg = I2C_FIFO_CONTROL; |
| 932 | |
| 933 | if (i2c_dev->is_curr_dma_xfer) { |
| 934 | if (len & 0xF) |
| 935 | dma_burst = 1; |
| 936 | else if (len & 0x10) |
| 937 | dma_burst = 4; |
| 938 | else |
| 939 | dma_burst = 8; |
| 940 | |
| 941 | if (i2c_dev->msg_read) { |
| 942 | chan = i2c_dev->rx_dma_chan; |
| 943 | reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO); |
| 944 | slv_config.src_addr = i2c_dev->base_phys + reg_offset; |
| 945 | slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 946 | slv_config.src_maxburst = dma_burst; |
| 947 | |
| 948 | if (i2c_dev->hw->has_mst_fifo) |
| 949 | val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst); |
| 950 | else |
| 951 | val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst); |
| 952 | } else { |
| 953 | chan = i2c_dev->tx_dma_chan; |
| 954 | reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO); |
| 955 | slv_config.dst_addr = i2c_dev->base_phys + reg_offset; |
| 956 | slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 957 | slv_config.dst_maxburst = dma_burst; |
| 958 | |
| 959 | if (i2c_dev->hw->has_mst_fifo) |
| 960 | val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst); |
| 961 | else |
| 962 | val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst); |
| 963 | } |
| 964 | |
| 965 | slv_config.device_fc = true; |
| 966 | ret = dmaengine_slave_config(chan, &slv_config); |
| 967 | if (ret < 0) { |
| 968 | dev_err(i2c_dev->dev, "DMA slave config failed: %d\n", |
| 969 | ret); |
Colin Ian King | bb0e9b1 | 2019-02-15 15:31:26 +0000 | [diff] [blame] | 970 | dev_err(i2c_dev->dev, "falling back to PIO\n"); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 971 | tegra_i2c_release_dma(i2c_dev); |
| 972 | i2c_dev->is_curr_dma_xfer = false; |
| 973 | } else { |
| 974 | goto out; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | if (i2c_dev->hw->has_mst_fifo) |
| 979 | val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) | |
| 980 | I2C_MST_FIFO_CONTROL_RX_TRIG(1); |
| 981 | else |
| 982 | val = I2C_FIFO_CONTROL_TX_TRIG(8) | |
| 983 | I2C_FIFO_CONTROL_RX_TRIG(1); |
| 984 | out: |
| 985 | i2c_writel(i2c_dev, val, reg); |
| 986 | } |
| 987 | |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 988 | static unsigned long |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 989 | tegra_i2c_poll_completion_timeout(struct tegra_i2c_dev *i2c_dev, |
| 990 | struct completion *complete, |
| 991 | unsigned int timeout_ms) |
| 992 | { |
| 993 | ktime_t ktime = ktime_get(); |
| 994 | ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms); |
| 995 | |
| 996 | do { |
| 997 | u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS); |
| 998 | |
Wolfram Sang | 8839e46 | 2020-04-27 10:52:22 +0200 | [diff] [blame] | 999 | if (status) { |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1000 | tegra_i2c_isr(i2c_dev->irq, i2c_dev); |
| 1001 | |
Wolfram Sang | 8839e46 | 2020-04-27 10:52:22 +0200 | [diff] [blame] | 1002 | if (completion_done(complete)) { |
| 1003 | s64 delta = ktime_ms_delta(ktimeout, ktime); |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1004 | |
Wolfram Sang | 8839e46 | 2020-04-27 10:52:22 +0200 | [diff] [blame] | 1005 | return msecs_to_jiffies(delta) ?: 1; |
| 1006 | } |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | ktime = ktime_get(); |
| 1010 | |
| 1011 | } while (ktime_before(ktime, ktimeout)); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static unsigned long |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1017 | tegra_i2c_wait_completion_timeout(struct tegra_i2c_dev *i2c_dev, |
| 1018 | struct completion *complete, |
| 1019 | unsigned int timeout_ms) |
| 1020 | { |
| 1021 | unsigned long ret; |
| 1022 | |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1023 | if (i2c_dev->is_curr_atomic_xfer) { |
| 1024 | ret = tegra_i2c_poll_completion_timeout(i2c_dev, complete, |
| 1025 | timeout_ms); |
| 1026 | } else { |
| 1027 | enable_irq(i2c_dev->irq); |
| 1028 | ret = wait_for_completion_timeout(complete, |
| 1029 | msecs_to_jiffies(timeout_ms)); |
| 1030 | disable_irq(i2c_dev->irq); |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1031 | |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1032 | /* |
Wolfram Sang | 8839e46 | 2020-04-27 10:52:22 +0200 | [diff] [blame] | 1033 | * There is a chance that completion may happen after IRQ |
| 1034 | * synchronization, which is done by disable_irq(). |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1035 | */ |
Wolfram Sang | 8839e46 | 2020-04-27 10:52:22 +0200 | [diff] [blame] | 1036 | if (ret == 0 && completion_done(complete)) { |
| 1037 | dev_warn(i2c_dev->dev, |
| 1038 | "completion done after timeout\n"); |
| 1039 | ret = 1; |
| 1040 | } |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | return ret; |
| 1044 | } |
| 1045 | |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1046 | static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap) |
| 1047 | { |
| 1048 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
| 1049 | int err; |
| 1050 | unsigned long time_left; |
| 1051 | u32 reg; |
| 1052 | |
| 1053 | reinit_completion(&i2c_dev->msg_complete); |
| 1054 | reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) | |
| 1055 | I2C_BC_STOP_COND | I2C_BC_TERMINATE; |
| 1056 | i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG); |
| 1057 | if (i2c_dev->hw->has_config_load_reg) { |
| 1058 | err = tegra_i2c_wait_for_config_load(i2c_dev); |
| 1059 | if (err) |
| 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | reg |= I2C_BC_ENABLE; |
| 1064 | i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG); |
| 1065 | tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE); |
| 1066 | |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1067 | time_left = tegra_i2c_wait_completion_timeout( |
| 1068 | i2c_dev, &i2c_dev->msg_complete, 50); |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1069 | if (time_left == 0) { |
| 1070 | dev_err(i2c_dev->dev, "timed out for bus clear\n"); |
| 1071 | return -ETIMEDOUT; |
| 1072 | } |
| 1073 | |
| 1074 | reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS); |
| 1075 | if (!(reg & I2C_BC_STATUS)) { |
| 1076 | dev_err(i2c_dev->dev, |
| 1077 | "un-recovered arbitration lost\n"); |
| 1078 | return -EIO; |
| 1079 | } |
| 1080 | |
| 1081 | return -EAGAIN; |
| 1082 | } |
| 1083 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1084 | static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev, |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1085 | struct i2c_msg *msg, |
| 1086 | enum msg_end_type end_state) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1087 | { |
| 1088 | u32 packet_header; |
| 1089 | u32 int_mask; |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 1090 | unsigned long time_left; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1091 | size_t xfer_size; |
| 1092 | u32 *buffer = NULL; |
| 1093 | int err = 0; |
| 1094 | bool dma; |
Sowjanya Komatineni | 80d4062 | 2019-02-12 11:06:47 -0800 | [diff] [blame] | 1095 | u16 xfer_time = 100; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1096 | |
| 1097 | tegra_i2c_flush_fifos(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1098 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1099 | i2c_dev->msg_buf = msg->buf; |
| 1100 | i2c_dev->msg_buf_remaining = msg->len; |
| 1101 | i2c_dev->msg_err = I2C_ERR_NONE; |
| 1102 | i2c_dev->msg_read = (msg->flags & I2C_M_RD); |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 1103 | reinit_completion(&i2c_dev->msg_complete); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1104 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1105 | if (i2c_dev->msg_read) |
| 1106 | xfer_size = msg->len; |
| 1107 | else |
| 1108 | xfer_size = msg->len + I2C_PACKET_HEADER_SIZE; |
| 1109 | |
| 1110 | xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD); |
Dmitry Osipenko | ae6028a | 2020-01-14 04:34:39 +0300 | [diff] [blame] | 1111 | i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_PREFERRED_LEN) && |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1112 | i2c_dev->dma_buf && |
| 1113 | !i2c_dev->is_curr_atomic_xfer; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1114 | tegra_i2c_config_fifo_trig(i2c_dev, xfer_size); |
| 1115 | dma = i2c_dev->is_curr_dma_xfer; |
Sowjanya Komatineni | 80d4062 | 2019-02-12 11:06:47 -0800 | [diff] [blame] | 1116 | /* |
| 1117 | * Transfer time in mSec = Total bits / transfer rate |
| 1118 | * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits |
| 1119 | */ |
| 1120 | xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC, |
| 1121 | i2c_dev->bus_clk_rate); |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 1122 | |
| 1123 | int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST; |
| 1124 | tegra_i2c_unmask_irq(i2c_dev, int_mask); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1125 | if (dma) { |
| 1126 | if (i2c_dev->msg_read) { |
| 1127 | dma_sync_single_for_device(i2c_dev->dev, |
| 1128 | i2c_dev->dma_phys, |
| 1129 | xfer_size, |
| 1130 | DMA_FROM_DEVICE); |
| 1131 | err = tegra_i2c_dma_submit(i2c_dev, xfer_size); |
| 1132 | if (err < 0) { |
| 1133 | dev_err(i2c_dev->dev, |
| 1134 | "starting RX DMA failed, err %d\n", |
| 1135 | err); |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1136 | return err; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | } else { |
| 1140 | dma_sync_single_for_cpu(i2c_dev->dev, |
| 1141 | i2c_dev->dma_phys, |
| 1142 | xfer_size, |
| 1143 | DMA_TO_DEVICE); |
| 1144 | buffer = i2c_dev->dma_buf; |
| 1145 | } |
| 1146 | } |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 1147 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1148 | packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) | |
| 1149 | PACKET_HEADER0_PROTOCOL_I2C | |
| 1150 | (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) | |
| 1151 | (1 << PACKET_HEADER0_PACKET_ID_SHIFT); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1152 | if (dma && !i2c_dev->msg_read) |
| 1153 | *buffer++ = packet_header; |
| 1154 | else |
| 1155 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1156 | |
| 1157 | packet_header = msg->len - 1; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1158 | if (dma && !i2c_dev->msg_read) |
| 1159 | *buffer++ = packet_header; |
| 1160 | else |
| 1161 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1162 | |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 1163 | packet_header = I2C_HEADER_IE_ENABLE; |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 1164 | if (end_state == MSG_END_CONTINUE) |
| 1165 | packet_header |= I2C_HEADER_CONTINUE_XFER; |
| 1166 | else if (end_state == MSG_END_REPEAT_START) |
Erik Gilling | 2078cf3 | 2011-04-25 15:32:26 -0600 | [diff] [blame] | 1167 | packet_header |= I2C_HEADER_REPEAT_START; |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 1168 | if (msg->flags & I2C_M_TEN) { |
| 1169 | packet_header |= msg->addr; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1170 | packet_header |= I2C_HEADER_10BIT_ADDR; |
Laxman Dewangan | 353f56b | 2012-04-24 12:49:35 +0530 | [diff] [blame] | 1171 | } else { |
| 1172 | packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT; |
| 1173 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1174 | if (msg->flags & I2C_M_IGNORE_NAK) |
| 1175 | packet_header |= I2C_HEADER_CONT_ON_NAK; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1176 | if (msg->flags & I2C_M_RD) |
| 1177 | packet_header |= I2C_HEADER_READ; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1178 | if (dma && !i2c_dev->msg_read) |
| 1179 | *buffer++ = packet_header; |
| 1180 | else |
| 1181 | i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1182 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1183 | if (!i2c_dev->msg_read) { |
| 1184 | if (dma) { |
| 1185 | memcpy(buffer, msg->buf, msg->len); |
| 1186 | dma_sync_single_for_device(i2c_dev->dev, |
| 1187 | i2c_dev->dma_phys, |
| 1188 | xfer_size, |
| 1189 | DMA_TO_DEVICE); |
| 1190 | err = tegra_i2c_dma_submit(i2c_dev, xfer_size); |
| 1191 | if (err < 0) { |
| 1192 | dev_err(i2c_dev->dev, |
| 1193 | "starting TX DMA failed, err %d\n", |
| 1194 | err); |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1195 | return err; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } else { |
| 1198 | tegra_i2c_fill_tx_fifo(i2c_dev); |
| 1199 | } |
| 1200 | } |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1201 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1202 | if (i2c_dev->hw->has_per_pkt_xfer_complete_irq) |
| 1203 | int_mask |= I2C_INT_PACKET_XFER_COMPLETE; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1204 | if (!dma) { |
| 1205 | if (msg->flags & I2C_M_RD) |
| 1206 | int_mask |= I2C_INT_RX_FIFO_DATA_REQ; |
| 1207 | else if (i2c_dev->msg_buf_remaining) |
| 1208 | int_mask |= I2C_INT_TX_FIFO_DATA_REQ; |
| 1209 | } |
Shardar Shariff Md | 77821b46 | 2016-08-31 18:58:44 +0530 | [diff] [blame] | 1210 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1211 | tegra_i2c_unmask_irq(i2c_dev, int_mask); |
| 1212 | dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n", |
| 1213 | i2c_readl(i2c_dev, I2C_INT_MASK)); |
| 1214 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1215 | if (dma) { |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1216 | time_left = tegra_i2c_wait_completion_timeout( |
| 1217 | i2c_dev, &i2c_dev->dma_complete, xfer_time); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1218 | |
Dmitry Osipenko | 28d9866 | 2020-01-14 04:34:41 +0300 | [diff] [blame] | 1219 | dmaengine_terminate_sync(i2c_dev->msg_read ? |
| 1220 | i2c_dev->rx_dma_chan : |
| 1221 | i2c_dev->tx_dma_chan); |
| 1222 | |
Dmitry Osipenko | b3ec946 | 2020-01-14 04:34:42 +0300 | [diff] [blame] | 1223 | if (!time_left && !completion_done(&i2c_dev->dma_complete)) { |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1224 | dev_err(i2c_dev->dev, "DMA transfer timeout\n"); |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1225 | tegra_i2c_init(i2c_dev, true); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1226 | return -ETIMEDOUT; |
| 1227 | } |
| 1228 | |
| 1229 | if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) { |
| 1230 | dma_sync_single_for_cpu(i2c_dev->dev, |
| 1231 | i2c_dev->dma_phys, |
| 1232 | xfer_size, |
| 1233 | DMA_FROM_DEVICE); |
| 1234 | memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf, |
| 1235 | msg->len); |
| 1236 | } |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1239 | time_left = tegra_i2c_wait_completion_timeout( |
| 1240 | i2c_dev, &i2c_dev->msg_complete, xfer_time); |
| 1241 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1242 | tegra_i2c_mask_irq(i2c_dev, int_mask); |
| 1243 | |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 1244 | if (time_left == 0) { |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1245 | dev_err(i2c_dev->dev, "i2c transfer timed out\n"); |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1246 | tegra_i2c_init(i2c_dev, true); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1247 | return -ETIMEDOUT; |
| 1248 | } |
| 1249 | |
Nicholas Mc Guire | 6973a39 | 2015-03-01 09:17:41 -0500 | [diff] [blame] | 1250 | dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n", |
| 1251 | time_left, completion_done(&i2c_dev->msg_complete), |
| 1252 | i2c_dev->msg_err); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1253 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1254 | i2c_dev->is_curr_dma_xfer = false; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1255 | if (likely(i2c_dev->msg_err == I2C_ERR_NONE)) |
| 1256 | return 0; |
| 1257 | |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1258 | tegra_i2c_init(i2c_dev, true); |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1259 | /* start recovery upon arbitration loss in single master mode */ |
| 1260 | if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) { |
| 1261 | if (!i2c_dev->is_multimaster_mode) |
| 1262 | return i2c_recover_bus(&i2c_dev->adapter); |
| 1263 | return -EAGAIN; |
| 1264 | } |
| 1265 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1266 | if (i2c_dev->msg_err == I2C_ERR_NO_ACK) { |
| 1267 | if (msg->flags & I2C_M_IGNORE_NAK) |
| 1268 | return 0; |
| 1269 | return -EREMOTEIO; |
| 1270 | } |
| 1271 | |
| 1272 | return -EIO; |
| 1273 | } |
| 1274 | |
| 1275 | static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1276 | int num) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1277 | { |
| 1278 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
| 1279 | int i; |
Bitan Biswas | 233d0ab | 2019-06-11 03:51:09 -0700 | [diff] [blame] | 1280 | int ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1281 | |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1282 | ret = pm_runtime_get_sync(i2c_dev->dev); |
Laxman Dewangan | 132c803 | 2013-03-15 05:34:08 +0000 | [diff] [blame] | 1283 | if (ret < 0) { |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1284 | dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret); |
Laxman Dewangan | 132c803 | 2013-03-15 05:34:08 +0000 | [diff] [blame] | 1285 | return ret; |
| 1286 | } |
| 1287 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1288 | for (i = 0; i < num; i++) { |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 1289 | enum msg_end_type end_type = MSG_END_STOP; |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 1290 | |
Laxman Dewangan | c8f5af2 | 2012-06-13 15:42:38 +0530 | [diff] [blame] | 1291 | if (i < (num - 1)) { |
| 1292 | if (msgs[i + 1].flags & I2C_M_NOSTART) |
| 1293 | end_type = MSG_END_CONTINUE; |
| 1294 | else |
| 1295 | end_type = MSG_END_REPEAT_START; |
| 1296 | } |
| 1297 | ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1298 | if (ret) |
| 1299 | break; |
| 1300 | } |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1301 | |
| 1302 | pm_runtime_put(i2c_dev->dev); |
| 1303 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1304 | return ret ?: i; |
| 1305 | } |
| 1306 | |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1307 | static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap, |
| 1308 | struct i2c_msg msgs[], int num) |
| 1309 | { |
| 1310 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
| 1311 | int ret; |
| 1312 | |
| 1313 | i2c_dev->is_curr_atomic_xfer = true; |
| 1314 | ret = tegra_i2c_xfer(adap, msgs, num); |
| 1315 | i2c_dev->is_curr_atomic_xfer = false; |
| 1316 | |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1320 | static u32 tegra_i2c_func(struct i2c_adapter *adap) |
| 1321 | { |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1322 | struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap); |
Wolfram Sang | 4bb28e3 | 2015-06-16 19:47:21 +0200 | [diff] [blame] | 1323 | u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) | |
| 1324 | I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING; |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1325 | |
| 1326 | if (i2c_dev->hw->has_continue_xfer_support) |
| 1327 | ret |= I2C_FUNC_NOSTART; |
| 1328 | return ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1331 | static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev) |
| 1332 | { |
| 1333 | struct device_node *np = i2c_dev->dev->of_node; |
| 1334 | int ret; |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1335 | bool multi_mode; |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1336 | |
| 1337 | ret = of_property_read_u32(np, "clock-frequency", |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1338 | &i2c_dev->bus_clk_rate); |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1339 | if (ret) |
Andy Shevchenko | 90224e6 | 2020-03-24 14:32:16 +0200 | [diff] [blame] | 1340 | i2c_dev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */ |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1341 | |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1342 | multi_mode = of_property_read_bool(np, "multi-master"); |
| 1343 | i2c_dev->is_multimaster_mode = multi_mode; |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1344 | } |
| 1345 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1346 | static const struct i2c_algorithm tegra_i2c_algo = { |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1347 | .master_xfer = tegra_i2c_xfer, |
| 1348 | .master_xfer_atomic = tegra_i2c_xfer_atomic, |
| 1349 | .functionality = tegra_i2c_func, |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1350 | }; |
| 1351 | |
Wolfram Sang | 3aaa34b | 2015-06-16 19:57:29 +0200 | [diff] [blame] | 1352 | /* payload size is only 12 bit */ |
Bhumika Goyal | ae3923a | 2017-08-21 17:42:04 +0530 | [diff] [blame] | 1353 | static const struct i2c_adapter_quirks tegra_i2c_quirks = { |
Wolfram Sang | c96c0f2 | 2018-07-23 22:26:12 +0200 | [diff] [blame] | 1354 | .flags = I2C_AQ_NO_ZERO_LEN, |
Sowjanya Komatineni | b03ff2a | 2019-02-12 11:06:45 -0800 | [diff] [blame] | 1355 | .max_read_len = SZ_4K, |
| 1356 | .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE, |
Wolfram Sang | 3aaa34b | 2015-06-16 19:57:29 +0200 | [diff] [blame] | 1357 | }; |
| 1358 | |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1359 | static const struct i2c_adapter_quirks tegra194_i2c_quirks = { |
| 1360 | .flags = I2C_AQ_NO_ZERO_LEN, |
Sowjanya Komatineni | b03ff2a | 2019-02-12 11:06:45 -0800 | [diff] [blame] | 1361 | .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1362 | }; |
| 1363 | |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1364 | static struct i2c_bus_recovery_info tegra_i2c_recovery_info = { |
| 1365 | .recover_bus = tegra_i2c_issue_bus_clear, |
| 1366 | }; |
| 1367 | |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1368 | static const struct tegra_i2c_hw_feature tegra20_i2c_hw = { |
| 1369 | .has_continue_xfer_support = false, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1370 | .has_per_pkt_xfer_complete_irq = false, |
| 1371 | .has_single_clk_source = false, |
| 1372 | .clk_divisor_hs_mode = 3, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1373 | .clk_divisor_std_mode = 0, |
| 1374 | .clk_divisor_fast_mode = 0, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 1375 | .clk_divisor_fast_plus_mode = 0, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1376 | .has_config_load_reg = false, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1377 | .has_multi_master_mode = false, |
| 1378 | .has_slcg_override_reg = false, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1379 | .has_mst_fifo = false, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1380 | .quirks = &tegra_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1381 | .supports_bus_clear = false, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1382 | .has_apb_dma = true, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1383 | .tlow_std_mode = 0x4, |
| 1384 | .thigh_std_mode = 0x2, |
| 1385 | .tlow_fast_fastplus_mode = 0x4, |
| 1386 | .thigh_fast_fastplus_mode = 0x2, |
| 1387 | .setup_hold_time_std_mode = 0x0, |
| 1388 | .setup_hold_time_fast_fast_plus_mode = 0x0, |
| 1389 | .setup_hold_time_hs_mode = 0x0, |
| 1390 | .has_interface_timing_reg = false, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1391 | }; |
| 1392 | |
| 1393 | static const struct tegra_i2c_hw_feature tegra30_i2c_hw = { |
| 1394 | .has_continue_xfer_support = true, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1395 | .has_per_pkt_xfer_complete_irq = false, |
| 1396 | .has_single_clk_source = false, |
| 1397 | .clk_divisor_hs_mode = 3, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1398 | .clk_divisor_std_mode = 0, |
| 1399 | .clk_divisor_fast_mode = 0, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 1400 | .clk_divisor_fast_plus_mode = 0, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1401 | .has_config_load_reg = false, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1402 | .has_multi_master_mode = false, |
| 1403 | .has_slcg_override_reg = false, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1404 | .has_mst_fifo = false, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1405 | .quirks = &tegra_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1406 | .supports_bus_clear = false, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1407 | .has_apb_dma = true, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1408 | .tlow_std_mode = 0x4, |
| 1409 | .thigh_std_mode = 0x2, |
| 1410 | .tlow_fast_fastplus_mode = 0x4, |
| 1411 | .thigh_fast_fastplus_mode = 0x2, |
| 1412 | .setup_hold_time_std_mode = 0x0, |
| 1413 | .setup_hold_time_fast_fast_plus_mode = 0x0, |
| 1414 | .setup_hold_time_hs_mode = 0x0, |
| 1415 | .has_interface_timing_reg = false, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1416 | }; |
| 1417 | |
| 1418 | static const struct tegra_i2c_hw_feature tegra114_i2c_hw = { |
| 1419 | .has_continue_xfer_support = true, |
| 1420 | .has_per_pkt_xfer_complete_irq = true, |
| 1421 | .has_single_clk_source = true, |
| 1422 | .clk_divisor_hs_mode = 1, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1423 | .clk_divisor_std_mode = 0x19, |
| 1424 | .clk_divisor_fast_mode = 0x19, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 1425 | .clk_divisor_fast_plus_mode = 0x10, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1426 | .has_config_load_reg = false, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1427 | .has_multi_master_mode = false, |
| 1428 | .has_slcg_override_reg = false, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1429 | .has_mst_fifo = false, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1430 | .quirks = &tegra_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1431 | .supports_bus_clear = true, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1432 | .has_apb_dma = true, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1433 | .tlow_std_mode = 0x4, |
| 1434 | .thigh_std_mode = 0x2, |
| 1435 | .tlow_fast_fastplus_mode = 0x4, |
| 1436 | .thigh_fast_fastplus_mode = 0x2, |
| 1437 | .setup_hold_time_std_mode = 0x0, |
| 1438 | .setup_hold_time_fast_fast_plus_mode = 0x0, |
| 1439 | .setup_hold_time_hs_mode = 0x0, |
| 1440 | .has_interface_timing_reg = false, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1441 | }; |
| 1442 | |
| 1443 | static const struct tegra_i2c_hw_feature tegra124_i2c_hw = { |
| 1444 | .has_continue_xfer_support = true, |
| 1445 | .has_per_pkt_xfer_complete_irq = true, |
| 1446 | .has_single_clk_source = true, |
| 1447 | .clk_divisor_hs_mode = 1, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1448 | .clk_divisor_std_mode = 0x19, |
| 1449 | .clk_divisor_fast_mode = 0x19, |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 1450 | .clk_divisor_fast_plus_mode = 0x10, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1451 | .has_config_load_reg = true, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1452 | .has_multi_master_mode = false, |
| 1453 | .has_slcg_override_reg = true, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1454 | .has_mst_fifo = false, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1455 | .quirks = &tegra_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1456 | .supports_bus_clear = true, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1457 | .has_apb_dma = true, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1458 | .tlow_std_mode = 0x4, |
| 1459 | .thigh_std_mode = 0x2, |
| 1460 | .tlow_fast_fastplus_mode = 0x4, |
| 1461 | .thigh_fast_fastplus_mode = 0x2, |
| 1462 | .setup_hold_time_std_mode = 0x0, |
| 1463 | .setup_hold_time_fast_fast_plus_mode = 0x0, |
| 1464 | .setup_hold_time_hs_mode = 0x0, |
| 1465 | .has_interface_timing_reg = true, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1466 | }; |
| 1467 | |
| 1468 | static const struct tegra_i2c_hw_feature tegra210_i2c_hw = { |
| 1469 | .has_continue_xfer_support = true, |
| 1470 | .has_per_pkt_xfer_complete_irq = true, |
| 1471 | .has_single_clk_source = true, |
| 1472 | .clk_divisor_hs_mode = 1, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1473 | .clk_divisor_std_mode = 0x19, |
| 1474 | .clk_divisor_fast_mode = 0x19, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1475 | .clk_divisor_fast_plus_mode = 0x10, |
| 1476 | .has_config_load_reg = true, |
Sowjanya Komatineni | 6b9932b | 2019-02-19 09:28:52 -0800 | [diff] [blame] | 1477 | .has_multi_master_mode = false, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1478 | .has_slcg_override_reg = true, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1479 | .has_mst_fifo = false, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1480 | .quirks = &tegra_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1481 | .supports_bus_clear = true, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1482 | .has_apb_dma = true, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1483 | .tlow_std_mode = 0x4, |
| 1484 | .thigh_std_mode = 0x2, |
| 1485 | .tlow_fast_fastplus_mode = 0x4, |
| 1486 | .thigh_fast_fastplus_mode = 0x2, |
| 1487 | .setup_hold_time_std_mode = 0, |
| 1488 | .setup_hold_time_fast_fast_plus_mode = 0, |
| 1489 | .setup_hold_time_hs_mode = 0, |
| 1490 | .has_interface_timing_reg = true, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1491 | }; |
| 1492 | |
| 1493 | static const struct tegra_i2c_hw_feature tegra186_i2c_hw = { |
| 1494 | .has_continue_xfer_support = true, |
| 1495 | .has_per_pkt_xfer_complete_irq = true, |
| 1496 | .has_single_clk_source = true, |
| 1497 | .clk_divisor_hs_mode = 1, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1498 | .clk_divisor_std_mode = 0x16, |
| 1499 | .clk_divisor_fast_mode = 0x19, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1500 | .clk_divisor_fast_plus_mode = 0x10, |
| 1501 | .has_config_load_reg = true, |
Sowjanya Komatineni | 6b9932b | 2019-02-19 09:28:52 -0800 | [diff] [blame] | 1502 | .has_multi_master_mode = false, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1503 | .has_slcg_override_reg = true, |
Sowjanya Komatineni | 9ffc125 | 2019-02-19 09:28:51 -0800 | [diff] [blame] | 1504 | .has_mst_fifo = false, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1505 | .quirks = &tegra_i2c_quirks, |
| 1506 | .supports_bus_clear = true, |
| 1507 | .has_apb_dma = false, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1508 | .tlow_std_mode = 0x4, |
| 1509 | .thigh_std_mode = 0x3, |
| 1510 | .tlow_fast_fastplus_mode = 0x4, |
| 1511 | .thigh_fast_fastplus_mode = 0x2, |
| 1512 | .setup_hold_time_std_mode = 0, |
| 1513 | .setup_hold_time_fast_fast_plus_mode = 0, |
| 1514 | .setup_hold_time_hs_mode = 0, |
| 1515 | .has_interface_timing_reg = true, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1516 | }; |
| 1517 | |
| 1518 | static const struct tegra_i2c_hw_feature tegra194_i2c_hw = { |
| 1519 | .has_continue_xfer_support = true, |
| 1520 | .has_per_pkt_xfer_complete_irq = true, |
| 1521 | .has_single_clk_source = true, |
| 1522 | .clk_divisor_hs_mode = 1, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1523 | .clk_divisor_std_mode = 0x4f, |
| 1524 | .clk_divisor_fast_mode = 0x3c, |
| 1525 | .clk_divisor_fast_plus_mode = 0x16, |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1526 | .has_config_load_reg = true, |
| 1527 | .has_multi_master_mode = true, |
| 1528 | .has_slcg_override_reg = true, |
| 1529 | .has_mst_fifo = true, |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1530 | .quirks = &tegra194_i2c_quirks, |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1531 | .supports_bus_clear = true, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1532 | .has_apb_dma = false, |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1533 | .tlow_std_mode = 0x8, |
| 1534 | .thigh_std_mode = 0x7, |
| 1535 | .tlow_fast_fastplus_mode = 0x2, |
| 1536 | .thigh_fast_fastplus_mode = 0x2, |
| 1537 | .setup_hold_time_std_mode = 0x08080808, |
| 1538 | .setup_hold_time_fast_fast_plus_mode = 0x02020202, |
| 1539 | .setup_hold_time_hs_mode = 0x090909, |
| 1540 | .has_interface_timing_reg = true, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1541 | }; |
| 1542 | |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1543 | /* Match table for of_platform binding */ |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1544 | static const struct of_device_id tegra_i2c_of_match[] = { |
Thierry Reding | c5907c6 | 2018-06-19 12:49:42 +0200 | [diff] [blame] | 1545 | { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, }, |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1546 | { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, }, |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1547 | { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, }, |
Laxman Dewangan | 6f4664b | 2015-06-30 16:24:26 +0530 | [diff] [blame] | 1548 | { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, }, |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1549 | { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, }, |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1550 | { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, }, |
| 1551 | { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, }, |
| 1552 | { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, }, |
| 1553 | {}, |
| 1554 | }; |
| 1555 | MODULE_DEVICE_TABLE(of, tegra_i2c_of_match); |
Laxman Dewangan | 6ad068e | 2012-08-19 00:47:46 +0530 | [diff] [blame] | 1556 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1557 | static int tegra_i2c_probe(struct platform_device *pdev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1558 | { |
| 1559 | struct tegra_i2c_dev *i2c_dev; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1560 | struct resource *res; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 1561 | struct clk *div_clk; |
| 1562 | struct clk *fast_clk; |
Olof Johansson | f533c61 | 2011-10-12 17:33:00 -0700 | [diff] [blame] | 1563 | void __iomem *base; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1564 | phys_addr_t base_phys; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1565 | int irq; |
Bitan Biswas | 233d0ab | 2019-06-11 03:51:09 -0700 | [diff] [blame] | 1566 | int ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1567 | |
| 1568 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1569 | base_phys = res->start; |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 1570 | base = devm_ioremap_resource(&pdev->dev, res); |
| 1571 | if (IS_ERR(base)) |
| 1572 | return PTR_ERR(base); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1573 | |
| 1574 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1575 | if (!res) { |
| 1576 | dev_err(&pdev->dev, "no irq resource\n"); |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 1577 | return -EINVAL; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1578 | } |
| 1579 | irq = res->start; |
| 1580 | |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 1581 | div_clk = devm_clk_get(&pdev->dev, "div-clk"); |
| 1582 | if (IS_ERR(div_clk)) { |
Thierry Reding | d680a50 | 2019-05-27 12:29:39 +0200 | [diff] [blame] | 1583 | if (PTR_ERR(div_clk) != -EPROBE_DEFER) |
| 1584 | dev_err(&pdev->dev, "missing controller clock\n"); |
| 1585 | |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 1586 | return PTR_ERR(div_clk); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1587 | } |
| 1588 | |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 1589 | i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); |
Jingoo Han | 46797a2 | 2014-05-13 10:51:58 +0900 | [diff] [blame] | 1590 | if (!i2c_dev) |
Laxman Dewangan | 9cbb6b2 | 2012-06-13 15:42:39 +0530 | [diff] [blame] | 1591 | return -ENOMEM; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1592 | |
| 1593 | i2c_dev->base = base; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1594 | i2c_dev->base_phys = base_phys; |
Laxman Dewangan | 14e92bd | 2012-08-08 13:21:32 +0530 | [diff] [blame] | 1595 | i2c_dev->div_clk = div_clk; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1596 | i2c_dev->adapter.algo = &tegra_i2c_algo; |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1597 | i2c_dev->adapter.retries = 1; |
Sowjanya Komatineni | 80d4062 | 2019-02-12 11:06:47 -0800 | [diff] [blame] | 1598 | i2c_dev->adapter.timeout = 6 * HZ; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1599 | i2c_dev->irq = irq; |
| 1600 | i2c_dev->cont_id = pdev->id; |
| 1601 | i2c_dev->dev = &pdev->dev; |
John Bonesio | 5c470f3 | 2011-06-22 09:16:56 -0700 | [diff] [blame] | 1602 | |
Philipp Zabel | 94d3b65 | 2017-07-19 17:25:34 +0200 | [diff] [blame] | 1603 | i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c"); |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 1604 | if (IS_ERR(i2c_dev->rst)) { |
Jon Hunter | e8e999cb | 2016-08-26 14:09:00 +0100 | [diff] [blame] | 1605 | dev_err(&pdev->dev, "missing controller reset\n"); |
Stephen Warren | dda9d6a | 2013-11-06 16:42:05 -0700 | [diff] [blame] | 1606 | return PTR_ERR(i2c_dev->rst); |
| 1607 | } |
| 1608 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1609 | tegra_i2c_parse_dt(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1610 | |
Jon Hunter | a9e32cd | 2016-08-26 14:09:01 +0100 | [diff] [blame] | 1611 | i2c_dev->hw = of_device_get_match_data(&pdev->dev); |
| 1612 | i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node, |
| 1613 | "nvidia,tegra20-i2c-dvc"); |
Sowjanya Komatineni | b67d453 | 2019-01-08 13:59:10 -0800 | [diff] [blame] | 1614 | i2c_dev->adapter.quirks = i2c_dev->hw->quirks; |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1615 | i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len + |
| 1616 | I2C_PACKET_HEADER_SIZE; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1617 | init_completion(&i2c_dev->msg_complete); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1618 | init_completion(&i2c_dev->dma_complete); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1619 | |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1620 | if (!i2c_dev->hw->has_single_clk_source) { |
| 1621 | fast_clk = devm_clk_get(&pdev->dev, "fast-clk"); |
| 1622 | if (IS_ERR(fast_clk)) { |
Jon Hunter | e8e999cb | 2016-08-26 14:09:00 +0100 | [diff] [blame] | 1623 | dev_err(&pdev->dev, "missing fast clock\n"); |
Laxman Dewangan | 2a2897b | 2013-01-05 17:34:46 +0530 | [diff] [blame] | 1624 | return PTR_ERR(fast_clk); |
| 1625 | } |
| 1626 | i2c_dev->fast_clk = fast_clk; |
| 1627 | } |
| 1628 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1629 | platform_set_drvdata(pdev, i2c_dev); |
| 1630 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1631 | if (!i2c_dev->hw->has_single_clk_source) { |
| 1632 | ret = clk_prepare(i2c_dev->fast_clk); |
| 1633 | if (ret < 0) { |
| 1634 | dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret); |
| 1635 | return ret; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Andy Shevchenko | 90224e6 | 2020-03-24 14:32:16 +0200 | [diff] [blame] | 1639 | if (i2c_dev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ && |
| 1640 | i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_PLUS_FREQ) |
Laxman Dewangan | d57f5de | 2015-06-30 16:24:27 +0530 | [diff] [blame] | 1641 | i2c_dev->clk_divisor_non_hs_mode = |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1642 | i2c_dev->hw->clk_divisor_fast_plus_mode; |
Andy Shevchenko | 90224e6 | 2020-03-24 14:32:16 +0200 | [diff] [blame] | 1643 | else if (i2c_dev->bus_clk_rate > I2C_MAX_STANDARD_MODE_FREQ && |
| 1644 | i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_FREQ) |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1645 | i2c_dev->clk_divisor_non_hs_mode = |
| 1646 | i2c_dev->hw->clk_divisor_fast_mode; |
| 1647 | else |
| 1648 | i2c_dev->clk_divisor_non_hs_mode = |
| 1649 | i2c_dev->hw->clk_divisor_std_mode; |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1650 | |
| 1651 | ret = clk_prepare(i2c_dev->div_clk); |
| 1652 | if (ret < 0) { |
| 1653 | dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret); |
| 1654 | goto unprepare_fast_clk; |
| 1655 | } |
| 1656 | |
Dmitry Osipenko | ede2299 | 2020-01-14 04:34:38 +0300 | [diff] [blame] | 1657 | pm_runtime_irq_safe(&pdev->dev); |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1658 | pm_runtime_enable(&pdev->dev); |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1659 | if (!pm_runtime_enabled(&pdev->dev)) { |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1660 | ret = tegra_i2c_runtime_resume(&pdev->dev); |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1661 | if (ret < 0) { |
| 1662 | dev_err(&pdev->dev, "runtime resume failed\n"); |
| 1663 | goto unprepare_div_clk; |
| 1664 | } |
| 1665 | } else { |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 1666 | ret = pm_runtime_get_sync(i2c_dev->dev); |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1667 | if (ret < 0) { |
| 1668 | dev_err(&pdev->dev, "runtime resume failed\n"); |
| 1669 | goto disable_rpm; |
| 1670 | } |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1673 | if (i2c_dev->is_multimaster_mode) { |
| 1674 | ret = clk_enable(i2c_dev->div_clk); |
| 1675 | if (ret < 0) { |
| 1676 | dev_err(i2c_dev->dev, "div_clk enable failed %d\n", |
| 1677 | ret); |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1678 | goto put_rpm; |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Sowjanya Komatineni | ce95624 | 2019-02-12 11:06:43 -0800 | [diff] [blame] | 1682 | if (i2c_dev->hw->supports_bus_clear) |
| 1683 | i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info; |
| 1684 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1685 | ret = tegra_i2c_init_dma(i2c_dev); |
| 1686 | if (ret < 0) |
| 1687 | goto disable_div_clk; |
| 1688 | |
Sowjanya Komatineni | 0940d24 | 2019-02-12 11:06:48 -0800 | [diff] [blame] | 1689 | ret = tegra_i2c_init(i2c_dev, false); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1690 | if (ret) { |
Jon Hunter | e8e999cb | 2016-08-26 14:09:00 +0100 | [diff] [blame] | 1691 | dev_err(&pdev->dev, "Failed to initialize i2c controller\n"); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1692 | goto release_dma; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Dmitry Osipenko | b5d5605 | 2020-01-14 04:34:37 +0300 | [diff] [blame] | 1695 | irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN); |
| 1696 | |
Thierry Reding | 26ca88a | 2020-05-06 20:47:25 +0200 | [diff] [blame^] | 1697 | ret = devm_request_irq(&pdev->dev, i2c_dev->irq, tegra_i2c_isr, |
| 1698 | IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1699 | if (ret) { |
| 1700 | dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1701 | goto release_dma; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1704 | i2c_set_adapdata(&i2c_dev->adapter, i2c_dev); |
| 1705 | i2c_dev->adapter.owner = THIS_MODULE; |
Wolfram Sang | 6025189 | 2014-07-10 13:46:35 +0200 | [diff] [blame] | 1706 | i2c_dev->adapter.class = I2C_CLASS_DEPRECATED; |
Jon Hunter | 0da9ab8 | 2016-08-26 14:09:02 +0100 | [diff] [blame] | 1707 | strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev), |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1708 | sizeof(i2c_dev->adapter.name)); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1709 | i2c_dev->adapter.dev.parent = &pdev->dev; |
| 1710 | i2c_dev->adapter.nr = pdev->id; |
John Bonesio | 5c470f3 | 2011-06-22 09:16:56 -0700 | [diff] [blame] | 1711 | i2c_dev->adapter.dev.of_node = pdev->dev.of_node; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1712 | |
| 1713 | ret = i2c_add_numbered_adapter(&i2c_dev->adapter); |
Wolfram Sang | ea73440 | 2016-08-09 13:36:17 +0200 | [diff] [blame] | 1714 | if (ret) |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1715 | goto release_dma; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1716 | |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 1717 | pm_runtime_put(&pdev->dev); |
| 1718 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1719 | return 0; |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1720 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1721 | release_dma: |
| 1722 | tegra_i2c_release_dma(i2c_dev); |
| 1723 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1724 | disable_div_clk: |
| 1725 | if (i2c_dev->is_multimaster_mode) |
| 1726 | clk_disable(i2c_dev->div_clk); |
| 1727 | |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1728 | put_rpm: |
| 1729 | if (pm_runtime_enabled(&pdev->dev)) |
| 1730 | pm_runtime_put_sync(&pdev->dev); |
| 1731 | else |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1732 | tegra_i2c_runtime_suspend(&pdev->dev); |
| 1733 | |
Dmitry Osipenko | 24a4967 | 2020-01-14 04:34:36 +0300 | [diff] [blame] | 1734 | disable_rpm: |
| 1735 | if (pm_runtime_enabled(&pdev->dev)) |
| 1736 | pm_runtime_disable(&pdev->dev); |
| 1737 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1738 | unprepare_div_clk: |
| 1739 | clk_unprepare(i2c_dev->div_clk); |
| 1740 | |
| 1741 | unprepare_fast_clk: |
| 1742 | if (!i2c_dev->hw->has_single_clk_source) |
| 1743 | clk_unprepare(i2c_dev->fast_clk); |
| 1744 | |
| 1745 | return ret; |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1748 | static int tegra_i2c_remove(struct platform_device *pdev) |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1749 | { |
| 1750 | struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev); |
Jon Hunter | f507668 | 2016-08-26 14:08:59 +0100 | [diff] [blame] | 1751 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1752 | i2c_del_adapter(&i2c_dev->adapter); |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1753 | |
Shardar Shariff Md | 497fbe2 | 2016-03-14 18:52:18 +0530 | [diff] [blame] | 1754 | if (i2c_dev->is_multimaster_mode) |
| 1755 | clk_disable(i2c_dev->div_clk); |
| 1756 | |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1757 | pm_runtime_disable(&pdev->dev); |
| 1758 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 1759 | tegra_i2c_runtime_suspend(&pdev->dev); |
| 1760 | |
Mikko Perttunen | c9a9ef4 | 2014-09-05 12:28:18 +0300 | [diff] [blame] | 1761 | clk_unprepare(i2c_dev->div_clk); |
| 1762 | if (!i2c_dev->hw->has_single_clk_source) |
| 1763 | clk_unprepare(i2c_dev->fast_clk); |
| 1764 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1765 | tegra_i2c_release_dma(i2c_dev); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1766 | return 0; |
| 1767 | } |
| 1768 | |
Dmitry Osipenko | e5738bc | 2019-07-08 02:12:34 +0300 | [diff] [blame] | 1769 | static int __maybe_unused tegra_i2c_suspend(struct device *dev) |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1770 | { |
| 1771 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
Thierry Reding | 44c9990 | 2019-12-13 14:44:17 +0100 | [diff] [blame] | 1772 | int err = 0; |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1773 | |
| 1774 | i2c_mark_adapter_suspended(&i2c_dev->adapter); |
| 1775 | |
Thierry Reding | 44c9990 | 2019-12-13 14:44:17 +0100 | [diff] [blame] | 1776 | if (!pm_runtime_status_suspended(dev)) |
| 1777 | err = tegra_i2c_runtime_suspend(dev); |
| 1778 | |
| 1779 | return err; |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Dmitry Osipenko | e5738bc | 2019-07-08 02:12:34 +0300 | [diff] [blame] | 1782 | static int __maybe_unused tegra_i2c_resume(struct device *dev) |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1783 | { |
| 1784 | struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev); |
| 1785 | int err; |
| 1786 | |
Thierry Reding | 44c9990 | 2019-12-13 14:44:17 +0100 | [diff] [blame] | 1787 | /* |
| 1788 | * We need to ensure that clocks are enabled so that registers can be |
| 1789 | * restored in tegra_i2c_init(). |
| 1790 | */ |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 1791 | err = tegra_i2c_runtime_resume(dev); |
| 1792 | if (err) |
| 1793 | return err; |
| 1794 | |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1795 | err = tegra_i2c_init(i2c_dev, false); |
| 1796 | if (err) |
| 1797 | return err; |
| 1798 | |
Thierry Reding | 44c9990 | 2019-12-13 14:44:17 +0100 | [diff] [blame] | 1799 | /* |
| 1800 | * In case we are runtime suspended, disable clocks again so that we |
| 1801 | * don't unbalance the clock reference counts during the next runtime |
| 1802 | * resume transition. |
| 1803 | */ |
| 1804 | if (pm_runtime_status_suspended(dev)) { |
| 1805 | err = tegra_i2c_runtime_suspend(dev); |
| 1806 | if (err) |
| 1807 | return err; |
| 1808 | } |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 1809 | |
Bitan Biswas | acc8abc | 2019-06-06 22:37:47 -0700 | [diff] [blame] | 1810 | i2c_mark_adapter_resumed(&i2c_dev->adapter); |
| 1811 | |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1815 | static const struct dev_pm_ops tegra_i2c_pm = { |
Jon Hunter | 8ebf15e | 2019-09-10 10:29:17 +0100 | [diff] [blame] | 1816 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume) |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1817 | SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume, |
| 1818 | NULL) |
Jon Hunter | 1f50ad2 | 2016-08-26 14:09:04 +0100 | [diff] [blame] | 1819 | }; |
Bitan Biswas | 26955a7 | 2019-06-11 03:51:10 -0700 | [diff] [blame] | 1820 | |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1821 | static struct platform_driver tegra_i2c_driver = { |
| 1822 | .probe = tegra_i2c_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 1823 | .remove = tegra_i2c_remove, |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1824 | .driver = { |
| 1825 | .name = "tegra-i2c", |
Stephen Warren | 49a64ac | 2013-03-21 08:08:46 +0000 | [diff] [blame] | 1826 | .of_match_table = tegra_i2c_of_match, |
Dmitry Osipenko | e5738bc | 2019-07-08 02:12:34 +0300 | [diff] [blame] | 1827 | .pm = &tegra_i2c_pm, |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1828 | }, |
| 1829 | }; |
| 1830 | |
Sowjanya Komatineni | 86c92b9 | 2019-02-12 11:06:46 -0800 | [diff] [blame] | 1831 | module_platform_driver(tegra_i2c_driver); |
Colin Cross | db811ca | 2011-02-20 17:14:21 -0800 | [diff] [blame] | 1832 | |
| 1833 | MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver"); |
| 1834 | MODULE_AUTHOR("Colin Cross"); |
| 1835 | MODULE_LICENSE("GPL v2"); |