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