blob: 26673be867bcbd7fc1c4b63f2b870b143eefa09b [file] [log] [blame]
Thierry Redingd6f04532018-12-17 15:16:54 +01001// SPDX-License-Identifier: GPL-2.0
Colin Crossdb811ca2011-02-20 17:14:21 -08002/*
3 * drivers/i2c/busses/i2c-tegra.c
4 *
5 * Copyright (C) 2010 Google, Inc.
6 * Author: Colin Cross <ccross@android.com>
Colin Crossdb811ca2011-02-20 17:14:21 -08007 */
8
Colin Crossdb811ca2011-02-20 17:14:21 -08009#include <linux/clk.h>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080010#include <linux/delay.h>
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -080011#include <linux/dmaengine.h>
12#include <linux/dma-mapping.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080013#include <linux/err.h>
14#include <linux/i2c.h>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080015#include <linux/init.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080016#include <linux/interrupt.h>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080017#include <linux/io.h>
Shardar Shariff Md685143a12016-08-31 18:58:40 +053018#include <linux/iopoll.h>
Dmitry Osipenkob5d56052020-01-14 04:34:37 +030019#include <linux/irq.h>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080020#include <linux/kernel.h>
Dmitry Osipenkoede22992020-01-14 04:34:38 +030021#include <linux/ktime.h>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080022#include <linux/module.h>
23#include <linux/of_device.h>
24#include <linux/pinctrl/consumer.h>
25#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/reset.h>
Colin Crossdb811ca2011-02-20 17:14:21 -080028
Colin Crossdb811ca2011-02-20 17:14:21 -080029#define BYTES_PER_FIFO_WORD 4
30
31#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060032#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010033#define I2C_CNFG_PACKET_MODE_EN BIT(10)
34#define I2C_CNFG_NEW_MASTER_FSM BIT(11)
35#define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
Todd Poynorcb63c622011-04-25 15:32:25 -060036#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080037#define I2C_SL_CNFG 0x020
Jon Hunter2929be22016-08-26 14:08:58 +010038#define I2C_SL_CNFG_NACK BIT(1)
39#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080040#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060041#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080042#define I2C_TX_FIFO 0x050
43#define I2C_RX_FIFO 0x054
44#define I2C_PACKET_TRANSFER_STATUS 0x058
45#define I2C_FIFO_CONTROL 0x05c
Jon Hunter2929be22016-08-26 14:08:58 +010046#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
47#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -080048#define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
49#define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
Colin Crossdb811ca2011-02-20 17:14:21 -080050#define I2C_FIFO_STATUS 0x060
51#define I2C_FIFO_STATUS_TX_MASK 0xF0
52#define I2C_FIFO_STATUS_TX_SHIFT 4
53#define I2C_FIFO_STATUS_RX_MASK 0x0F
54#define I2C_FIFO_STATUS_RX_SHIFT 0
55#define I2C_INT_MASK 0x064
56#define I2C_INT_STATUS 0x068
Sowjanya Komatinenice956242019-02-12 11:06:43 -080057#define I2C_INT_BUS_CLR_DONE BIT(11)
Jon Hunter2929be22016-08-26 14:08:58 +010058#define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
Jon Hunter2929be22016-08-26 14:08:58 +010059#define I2C_INT_NO_ACK BIT(3)
60#define I2C_INT_ARBITRATION_LOST BIT(2)
61#define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
62#define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
Colin Crossdb811ca2011-02-20 17:14:21 -080063#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053064#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
Colin Crossdb811ca2011-02-20 17:14:21 -080065
66#define DVC_CTRL_REG1 0x000
Jon Hunter2929be22016-08-26 14:08:58 +010067#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080068#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010069#define DVC_CTRL_REG3_SW_PROG BIT(26)
70#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080071#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010072#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080073
74#define I2C_ERR_NONE 0x00
Bitan Biswasc84663c2019-06-11 03:51:08 -070075#define I2C_ERR_NO_ACK BIT(0)
76#define I2C_ERR_ARBITRATION_LOST BIT(1)
77#define I2C_ERR_UNKNOWN_INTERRUPT BIT(2)
Bitan Biswas57ca9682019-06-18 04:09:42 -070078#define I2C_ERR_RX_BUFFER_OVERFLOW BIT(3)
Colin Crossdb811ca2011-02-20 17:14:21 -080079
80#define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
81#define PACKET_HEADER0_PACKET_ID_SHIFT 16
82#define PACKET_HEADER0_CONT_ID_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010083#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080084
Jon Hunter2929be22016-08-26 14:08:58 +010085#define I2C_HEADER_CONT_ON_NAK BIT(21)
Jon Hunter2929be22016-08-26 14:08:58 +010086#define I2C_HEADER_READ BIT(19)
87#define I2C_HEADER_10BIT_ADDR BIT(18)
88#define I2C_HEADER_IE_ENABLE BIT(17)
89#define I2C_HEADER_REPEAT_START BIT(16)
90#define I2C_HEADER_CONTINUE_XFER BIT(15)
Colin Crossdb811ca2011-02-20 17:14:21 -080091#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +053092
Sowjanya Komatinenice956242019-02-12 11:06:43 -080093#define I2C_BUS_CLEAR_CNFG 0x084
94#define I2C_BC_SCLK_THRESHOLD 9
95#define I2C_BC_SCLK_THRESHOLD_SHIFT 16
96#define I2C_BC_STOP_COND BIT(2)
97#define I2C_BC_TERMINATE BIT(1)
98#define I2C_BC_ENABLE BIT(0)
99#define I2C_BUS_CLEAR_STATUS 0x088
100#define I2C_BC_STATUS BIT(0)
101
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530102#define I2C_CONFIG_LOAD 0x08C
Jon Hunter2929be22016-08-26 14:08:58 +0100103#define I2C_MSTR_CONFIG_LOAD BIT(0)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530104
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530105#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100106#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530107
Shardar Shariff Md685143a12016-08-31 18:58:40 +0530108#define I2C_CONFIG_LOAD_TIMEOUT 1000000
109
Thierry Redingc5907c62018-06-19 12:49:42 +0200110#define I2C_MST_FIFO_CONTROL 0x0b4
111#define I2C_MST_FIFO_CONTROL_RX_FLUSH BIT(0)
112#define I2C_MST_FIFO_CONTROL_TX_FLUSH BIT(1)
113#define I2C_MST_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 4)
114#define I2C_MST_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 16)
115
116#define I2C_MST_FIFO_STATUS 0x0b8
117#define I2C_MST_FIFO_STATUS_RX_MASK 0xff
118#define I2C_MST_FIFO_STATUS_RX_SHIFT 0
119#define I2C_MST_FIFO_STATUS_TX_MASK 0xff0000
120#define I2C_MST_FIFO_STATUS_TX_SHIFT 16
121
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800122#define I2C_INTERFACE_TIMING_0 0x94
123#define I2C_THIGH_SHIFT 8
124#define I2C_INTERFACE_TIMING_1 0x98
125
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -0800126/* Packet header size in bytes */
127#define I2C_PACKET_HEADER_SIZE 12
128
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530129/*
Dmitry Osipenkoae6028a2020-01-14 04:34:39 +0300130 * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
131 * avoid DMA overhead, otherwise external APB DMA controller will be used.
132 * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
133 * I2C_PACKET_HEADER_SIZE.
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800134 */
Dmitry Osipenkoae6028a2020-01-14 04:34:39 +0300135#define I2C_PIO_MODE_PREFERRED_LEN 32
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800136
137/*
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530138 * msg_end_type: The bus control which need to be send at end of transfer.
139 * @MSG_END_STOP: Send stop pulse at end of transfer.
140 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
141 * @MSG_END_CONTINUE: The following on message is coming and so do not send
142 * stop or repeat start.
143 */
144enum msg_end_type {
145 MSG_END_STOP,
146 MSG_END_REPEAT_START,
147 MSG_END_CONTINUE,
148};
Colin Crossdb811ca2011-02-20 17:14:21 -0800149
150/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530151 * struct tegra_i2c_hw_feature : Different HW support on Tegra
152 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530153 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
154 * complete interrupt per packet basis.
Thierry Redingc990bba2018-12-17 15:16:52 +0100155 * @has_single_clk_source: The I2C controller has single clock source. Tegra30
156 * and earlier SoCs have two clock sources i.e. div-clk and
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530157 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530158 * @has_config_load_reg: Has the config load register to load the new
159 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530160 * @clk_divisor_hs_mode: Clock divisor in HS mode.
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800161 * @clk_divisor_std_mode: Clock divisor in standard mode. It is
162 * applicable if there is no fast clock source i.e. single clock
163 * source.
164 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530165 * applicable if there is no fast clock source i.e. single clock
166 * source.
Thierry Reding0604ee42018-12-17 15:16:53 +0100167 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
168 * applicable if there is no fast clock source (i.e. single
169 * clock source).
170 * @has_multi_master_mode: The I2C controller supports running in single-master
171 * or multi-master mode.
172 * @has_slcg_override_reg: The I2C controller supports a register that
173 * overrides the second level clock gating.
174 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
175 * provides additional features and allows for longer messages to
176 * be transferred in one go.
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -0800177 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
178 * allowing 0 length transfers.
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800179 * @supports_bus_clear: Bus Clear support to recover from bus hang during
180 * SDA stuck low from device for some unknown reasons.
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800181 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800182 * @tlow_std_mode: Low period of the clock in standard mode.
183 * @thigh_std_mode: High period of the clock in standard mode.
184 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
185 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
186 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
187 * in standard mode.
188 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
189 * conditions in fast/fast-plus modes.
190 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
191 * in HS mode.
192 * @has_interface_timing_reg: Has interface timing register to program the tuned
193 * timing settings.
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530194 */
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530195struct tegra_i2c_hw_feature {
196 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530197 bool has_per_pkt_xfer_complete_irq;
198 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530199 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530200 int clk_divisor_hs_mode;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800201 int clk_divisor_std_mode;
202 int clk_divisor_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530203 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530204 bool has_multi_master_mode;
205 bool has_slcg_override_reg;
Thierry Redingc5907c62018-06-19 12:49:42 +0200206 bool has_mst_fifo;
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -0800207 const struct i2c_adapter_quirks *quirks;
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800208 bool supports_bus_clear;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800209 bool has_apb_dma;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800210 u8 tlow_std_mode;
211 u8 thigh_std_mode;
212 u8 tlow_fast_fastplus_mode;
213 u8 thigh_fast_fastplus_mode;
214 u32 setup_hold_time_std_mode;
215 u32 setup_hold_time_fast_fast_plus_mode;
216 u32 setup_hold_time_hs_mode;
217 bool has_interface_timing_reg;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530218};
219
220/**
Thierry Redingc990bba2018-12-17 15:16:52 +0100221 * struct tegra_i2c_dev - per device I2C context
Colin Crossdb811ca2011-02-20 17:14:21 -0800222 * @dev: device reference for power management
Thierry Redingc990bba2018-12-17 15:16:52 +0100223 * @hw: Tegra I2C HW feature
224 * @adapter: core I2C layer adapter information
225 * @div_clk: clock reference for div clock of I2C controller
226 * @fast_clk: clock reference for fast clock of I2C controller
Thierry Reding0604ee42018-12-17 15:16:53 +0100227 * @rst: reset control for the I2C controller
Colin Crossdb811ca2011-02-20 17:14:21 -0800228 * @base: ioremapped registers cookie
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800229 * @base_phys: physical base address of the I2C controller
Thierry Redingc990bba2018-12-17 15:16:52 +0100230 * @cont_id: I2C controller ID, used for packet header
231 * @irq: IRQ number of transfer complete interrupt
232 * @is_dvc: identifies the DVC I2C controller, has a different register layout
Colin Crossdb811ca2011-02-20 17:14:21 -0800233 * @msg_complete: transfer completion notifier
234 * @msg_err: error code for completed message
235 * @msg_buf: pointer to current message data
236 * @msg_buf_remaining: size of unsent data in the message buffer
237 * @msg_read: identifies read transfers
Thierry Redingc990bba2018-12-17 15:16:52 +0100238 * @bus_clk_rate: current I2C bus clock rate
Thierry Reding0604ee42018-12-17 15:16:53 +0100239 * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes
240 * @is_multimaster_mode: track if I2C controller is in multi-master mode
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800241 * @tx_dma_chan: DMA transmit channel
242 * @rx_dma_chan: DMA receive channel
243 * @dma_phys: handle to DMA resources
244 * @dma_buf: pointer to allocated DMA buffer
245 * @dma_buf_size: DMA buffer size
246 * @is_curr_dma_xfer: indicates active DMA transfer
247 * @dma_complete: DMA completion notifier
Dmitry Osipenkoede22992020-01-14 04:34:38 +0300248 * @is_curr_atomic_xfer: indicates active atomic transfer
Colin Crossdb811ca2011-02-20 17:14:21 -0800249 */
250struct tegra_i2c_dev {
251 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530252 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800253 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530254 struct clk *div_clk;
255 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700256 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800257 void __iomem *base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800258 phys_addr_t base_phys;
Colin Crossdb811ca2011-02-20 17:14:21 -0800259 int cont_id;
260 int irq;
261 int is_dvc;
262 struct completion msg_complete;
263 int msg_err;
264 u8 *msg_buf;
265 size_t msg_buf_remaining;
266 int msg_read;
Stephen Warren49a64ac2013-03-21 08:08:46 +0000267 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530268 u16 clk_divisor_non_hs_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530269 bool is_multimaster_mode;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800270 struct dma_chan *tx_dma_chan;
271 struct dma_chan *rx_dma_chan;
272 dma_addr_t dma_phys;
273 u32 *dma_buf;
274 unsigned int dma_buf_size;
275 bool is_curr_dma_xfer;
276 struct completion dma_complete;
Dmitry Osipenkoede22992020-01-14 04:34:38 +0300277 bool is_curr_atomic_xfer;
Colin Crossdb811ca2011-02-20 17:14:21 -0800278};
279
Jon Hunterc7ae44e82016-08-26 14:08:57 +0100280static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
281 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800282{
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300283 writel_relaxed(val, i2c_dev->base + reg);
Colin Crossdb811ca2011-02-20 17:14:21 -0800284}
285
286static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
287{
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300288 return readl_relaxed(i2c_dev->base + reg);
Colin Crossdb811ca2011-02-20 17:14:21 -0800289}
290
291/*
292 * i2c_writel and i2c_readl will offset the register if necessary to talk
293 * to the I2C block inside the DVC block
294 */
295static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
Bitan Biswas26955a72019-06-11 03:51:10 -0700296 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800297{
298 if (i2c_dev->is_dvc)
299 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
300 return reg;
301}
302
303static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
Bitan Biswas26955a72019-06-11 03:51:10 -0700304 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800305{
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300306 writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Laxman Dewanganec7aaca2012-06-13 15:42:36 +0530307
308 /* Read back register to make sure that register writes completed */
309 if (reg != I2C_TX_FIFO)
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300310 readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800311}
312
313static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
314{
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300315 return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
Colin Crossdb811ca2011-02-20 17:14:21 -0800316}
317
318static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
Bitan Biswas26955a72019-06-11 03:51:10 -0700319 unsigned long reg, int len)
Colin Crossdb811ca2011-02-20 17:14:21 -0800320{
321 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
322}
323
324static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
Bitan Biswas26955a72019-06-11 03:51:10 -0700325 unsigned long reg, int len)
Colin Crossdb811ca2011-02-20 17:14:21 -0800326{
327 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
328}
329
330static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
331{
Jon Hunterf5076682016-08-26 14:08:59 +0100332 u32 int_mask;
333
334 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800335 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
336}
337
338static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
339{
Jon Hunterf5076682016-08-26 14:08:59 +0100340 u32 int_mask;
341
342 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800343 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
344}
345
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800346static void tegra_i2c_dma_complete(void *args)
347{
348 struct tegra_i2c_dev *i2c_dev = args;
349
350 complete(&i2c_dev->dma_complete);
351}
352
353static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
354{
355 struct dma_async_tx_descriptor *dma_desc;
356 enum dma_transfer_direction dir;
357 struct dma_chan *chan;
358
359 dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
360 reinit_completion(&i2c_dev->dma_complete);
361 dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
362 chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
363 dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
364 len, dir, DMA_PREP_INTERRUPT |
365 DMA_CTRL_ACK);
366 if (!dma_desc) {
367 dev_err(i2c_dev->dev, "failed to get DMA descriptor\n");
368 return -EINVAL;
369 }
370
371 dma_desc->callback = tegra_i2c_dma_complete;
372 dma_desc->callback_param = i2c_dev;
373 dmaengine_submit(dma_desc);
374 dma_async_issue_pending(chan);
375 return 0;
376}
377
378static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
379{
380 if (i2c_dev->dma_buf) {
381 dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
382 i2c_dev->dma_buf, i2c_dev->dma_phys);
383 i2c_dev->dma_buf = NULL;
384 }
385
386 if (i2c_dev->tx_dma_chan) {
387 dma_release_channel(i2c_dev->tx_dma_chan);
388 i2c_dev->tx_dma_chan = NULL;
389 }
390
391 if (i2c_dev->rx_dma_chan) {
392 dma_release_channel(i2c_dev->rx_dma_chan);
393 i2c_dev->rx_dma_chan = NULL;
394 }
395}
396
397static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
398{
399 struct dma_chan *chan;
400 u32 *dma_buf;
401 dma_addr_t dma_phys;
402 int err;
403
Jonathan Hunter89328b12019-02-21 15:00:38 +0000404 if (!i2c_dev->hw->has_apb_dma)
405 return 0;
406
407 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
408 dev_dbg(i2c_dev->dev, "Support for APB DMA not enabled!\n");
409 return 0;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800410 }
411
Peter Ujfalusi79e4be22019-11-13 11:22:35 +0200412 chan = dma_request_chan(i2c_dev->dev, "rx");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800413 if (IS_ERR(chan)) {
414 err = PTR_ERR(chan);
415 goto err_out;
416 }
417
418 i2c_dev->rx_dma_chan = chan;
419
Peter Ujfalusi79e4be22019-11-13 11:22:35 +0200420 chan = dma_request_chan(i2c_dev->dev, "tx");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800421 if (IS_ERR(chan)) {
422 err = PTR_ERR(chan);
423 goto err_out;
424 }
425
426 i2c_dev->tx_dma_chan = chan;
427
428 dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
429 &dma_phys, GFP_KERNEL | __GFP_NOWARN);
430 if (!dma_buf) {
431 dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n");
432 err = -ENOMEM;
433 goto err_out;
434 }
435
436 i2c_dev->dma_buf = dma_buf;
437 i2c_dev->dma_phys = dma_phys;
438 return 0;
439
440err_out:
441 tegra_i2c_release_dma(i2c_dev);
442 if (err != -EPROBE_DEFER) {
443 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
Colin Ian Kingbb0e9b12019-02-15 15:31:26 +0000444 dev_err(i2c_dev->dev, "falling back to PIO\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800445 return 0;
446 }
447
448 return err;
449}
450
Colin Crossdb811ca2011-02-20 17:14:21 -0800451static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
452{
453 unsigned long timeout = jiffies + HZ;
Thierry Redingc5907c62018-06-19 12:49:42 +0200454 unsigned int offset;
455 u32 mask, val;
Jon Hunterf5076682016-08-26 14:08:59 +0100456
Thierry Redingc5907c62018-06-19 12:49:42 +0200457 if (i2c_dev->hw->has_mst_fifo) {
458 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
459 I2C_MST_FIFO_CONTROL_RX_FLUSH;
460 offset = I2C_MST_FIFO_CONTROL;
461 } else {
462 mask = I2C_FIFO_CONTROL_TX_FLUSH |
463 I2C_FIFO_CONTROL_RX_FLUSH;
464 offset = I2C_FIFO_CONTROL;
465 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800466
Thierry Redingc5907c62018-06-19 12:49:42 +0200467 val = i2c_readl(i2c_dev, offset);
468 val |= mask;
469 i2c_writel(i2c_dev, val, offset);
470
471 while (i2c_readl(i2c_dev, offset) & mask) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800472 if (time_after(jiffies, timeout)) {
473 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
474 return -ETIMEDOUT;
475 }
Bitan Biswasfbbe4942019-06-11 03:51:12 -0700476 usleep_range(1000, 2000);
Colin Crossdb811ca2011-02-20 17:14:21 -0800477 }
478 return 0;
479}
480
481static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
482{
483 u32 val;
484 int rx_fifo_avail;
485 u8 *buf = i2c_dev->msg_buf;
486 size_t buf_remaining = i2c_dev->msg_buf_remaining;
487 int words_to_transfer;
488
Bitan Biswas57ca9682019-06-18 04:09:42 -0700489 /*
490 * Catch overflow due to message fully sent
491 * before the check for RX FIFO availability.
492 */
493 if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
494 return -EINVAL;
495
Thierry Redingc5907c62018-06-19 12:49:42 +0200496 if (i2c_dev->hw->has_mst_fifo) {
497 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
498 rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
499 I2C_MST_FIFO_STATUS_RX_SHIFT;
500 } else {
501 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
502 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
503 I2C_FIFO_STATUS_RX_SHIFT;
504 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800505
506 /* Rounds down to not include partial word at the end of buf */
507 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
508 if (words_to_transfer > rx_fifo_avail)
509 words_to_transfer = rx_fifo_avail;
510
511 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
512
513 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
514 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
515 rx_fifo_avail -= words_to_transfer;
516
517 /*
518 * If there is a partial word at the end of buf, handle it manually to
519 * prevent overwriting past the end of buf
520 */
521 if (rx_fifo_avail > 0 && buf_remaining > 0) {
Bitan Biswas57ca9682019-06-18 04:09:42 -0700522 /*
523 * buf_remaining > 3 check not needed as rx_fifo_avail == 0
524 * when (words_to_transfer was > rx_fifo_avail) earlier
525 * in this function.
526 */
Colin Crossdb811ca2011-02-20 17:14:21 -0800527 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300528 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800529 memcpy(buf, &val, buf_remaining);
530 buf_remaining = 0;
531 rx_fifo_avail--;
532 }
533
Bitan Biswas57ca9682019-06-18 04:09:42 -0700534 /* RX FIFO must be drained, otherwise it's an Overflow case. */
535 if (WARN_ON_ONCE(rx_fifo_avail))
536 return -EINVAL;
537
Colin Crossdb811ca2011-02-20 17:14:21 -0800538 i2c_dev->msg_buf_remaining = buf_remaining;
539 i2c_dev->msg_buf = buf;
Thierry Redingc5907c62018-06-19 12:49:42 +0200540
Colin Crossdb811ca2011-02-20 17:14:21 -0800541 return 0;
542}
543
544static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
545{
546 u32 val;
547 int tx_fifo_avail;
548 u8 *buf = i2c_dev->msg_buf;
549 size_t buf_remaining = i2c_dev->msg_buf_remaining;
550 int words_to_transfer;
551
Thierry Redingc5907c62018-06-19 12:49:42 +0200552 if (i2c_dev->hw->has_mst_fifo) {
553 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
554 tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
555 I2C_MST_FIFO_STATUS_TX_SHIFT;
556 } else {
557 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
558 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
559 I2C_FIFO_STATUS_TX_SHIFT;
560 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800561
562 /* Rounds down to not include partial word at the end of buf */
563 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800564
Doug Anderson96219c32011-08-30 11:46:10 -0600565 /* It's very common to have < 4 bytes, so optimize that case. */
566 if (words_to_transfer) {
567 if (words_to_transfer > tx_fifo_avail)
568 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800569
Doug Anderson96219c32011-08-30 11:46:10 -0600570 /*
571 * Update state before writing to FIFO. If this casues us
572 * to finish writing all bytes (AKA buf_remaining goes to 0) we
573 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
574 * not maskable). We need to make sure that the isr sees
575 * buf_remaining as 0 and doesn't call us back re-entrantly.
576 */
577 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
578 tx_fifo_avail -= words_to_transfer;
579 i2c_dev->msg_buf_remaining = buf_remaining;
580 i2c_dev->msg_buf = buf +
581 words_to_transfer * BYTES_PER_FIFO_WORD;
582 barrier();
583
584 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
585
586 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
587 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800588
589 /*
590 * If there is a partial word at the end of buf, handle it manually to
591 * prevent reading past the end of buf, which could cross a page
592 * boundary and fault.
593 */
594 if (tx_fifo_avail > 0 && buf_remaining > 0) {
Bitan Biswas57ca9682019-06-18 04:09:42 -0700595 /*
596 * buf_remaining > 3 check not needed as tx_fifo_avail == 0
597 * when (words_to_transfer was > tx_fifo_avail) earlier
598 * in this function for non-zero words_to_transfer.
599 */
Colin Crossdb811ca2011-02-20 17:14:21 -0800600 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300601 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600602
603 /* Again update before writing to FIFO to make sure isr sees. */
604 i2c_dev->msg_buf_remaining = 0;
605 i2c_dev->msg_buf = NULL;
606 barrier();
607
Colin Crossdb811ca2011-02-20 17:14:21 -0800608 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800609 }
610
Colin Crossdb811ca2011-02-20 17:14:21 -0800611 return 0;
612}
613
614/*
615 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
616 * block. This block is identical to the rest of the I2C blocks, except that
617 * it only supports master mode, it has registers moved around, and it needs
618 * some extra init to get it into I2C mode. The register moves are handled
619 * by i2c_readl and i2c_writel
620 */
621static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
622{
Jon Hunterf5076682016-08-26 14:08:59 +0100623 u32 val;
624
Colin Crossdb811ca2011-02-20 17:14:21 -0800625 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
626 val |= DVC_CTRL_REG3_SW_PROG;
627 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
628 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
629
630 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
631 val |= DVC_CTRL_REG1_INTR_EN;
632 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
633}
634
Dmitry Osipenkoe5738bc2019-07-08 02:12:34 +0300635static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530636{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100637 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530638 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100639
Jon Hunter718917b2016-08-26 14:09:05 +0100640 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
641 if (ret)
642 return ret;
643
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530644 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300645 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530646 if (ret < 0) {
647 dev_err(i2c_dev->dev,
648 "Enabling fast clk failed, err %d\n", ret);
649 return ret;
650 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530651 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100652
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300653 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530654 if (ret < 0) {
655 dev_err(i2c_dev->dev,
656 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300657 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100658 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530659 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100660
661 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530662}
663
Dmitry Osipenkoe5738bc2019-07-08 02:12:34 +0300664static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530665{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100666 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
667
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300668 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530669 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300670 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100671
Jon Hunter718917b2016-08-26 14:09:05 +0100672 return pinctrl_pm_select_idle_state(i2c_dev->dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530673}
674
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530675static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
676{
677 unsigned long reg_offset;
678 void __iomem *addr;
679 u32 val;
680 int err;
681
682 if (i2c_dev->hw->has_config_load_reg) {
683 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
684 addr = i2c_dev->base + reg_offset;
685 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
Dmitry Osipenkoede22992020-01-14 04:34:38 +0300686
687 if (i2c_dev->is_curr_atomic_xfer)
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300688 err = readl_relaxed_poll_timeout_atomic(
689 addr, val, val == 0, 1000,
690 I2C_CONFIG_LOAD_TIMEOUT);
Shardar Shariff Md2bc445e2016-08-31 18:58:43 +0530691 else
Dmitry Osipenko4211ffc2020-01-14 04:34:40 +0300692 err = readl_relaxed_poll_timeout(
693 addr, val, val == 0, 1000,
694 I2C_CONFIG_LOAD_TIMEOUT);
Shardar Shariff Md2bc445e2016-08-31 18:58:43 +0530695
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530696 if (err) {
697 dev_warn(i2c_dev->dev,
698 "timeout waiting for config load\n");
699 return err;
700 }
701 }
702
703 return 0;
704}
705
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800706static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit)
Colin Crossdb811ca2011-02-20 17:14:21 -0800707{
708 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100709 int err;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800710 u32 clk_divisor, clk_multiplier;
Bitan Biswas233d0ab2019-06-11 03:51:09 -0700711 u32 tsu_thd;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800712 u8 tlow, thigh;
Colin Crossdb811ca2011-02-20 17:14:21 -0800713
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700714 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800715 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700716 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800717
718 if (i2c_dev->is_dvc)
719 tegra_dvc_init(i2c_dev);
720
Jay Cheng40abcf72011-04-25 15:32:27 -0600721 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
722 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530723
724 if (i2c_dev->hw->has_multi_master_mode)
725 val |= I2C_CNFG_MULTI_MASTER_MODE;
726
Colin Crossdb811ca2011-02-20 17:14:21 -0800727 i2c_writel(i2c_dev, val, I2C_CNFG);
728 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530729
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530730 /* Make sure clock divisor programmed correctly */
731 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530732 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530733 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
734 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800735
Andy Shevchenko90224e62020-03-24 14:32:16 +0200736 if (i2c_dev->bus_clk_rate > I2C_MAX_STANDARD_MODE_FREQ &&
737 i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_PLUS_FREQ) {
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800738 tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
739 thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
740 tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
741 } else {
742 tlow = i2c_dev->hw->tlow_std_mode;
743 thigh = i2c_dev->hw->thigh_std_mode;
744 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
745 }
746
747 if (i2c_dev->hw->has_interface_timing_reg) {
748 val = (thigh << I2C_THIGH_SHIFT) | tlow;
749 i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
750 }
751
752 /*
753 * configure setup and hold times only when tsu_thd is non-zero.
754 * otherwise, preserve the chip default values
755 */
756 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
757 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
758
759 if (!clk_reinit) {
760 clk_multiplier = (tlow + thigh + 2);
761 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
762 err = clk_set_rate(i2c_dev->div_clk,
763 i2c_dev->bus_clk_rate * clk_multiplier);
764 if (err) {
765 dev_err(i2c_dev->dev,
766 "failed changing clock rate: %d\n", err);
Jon Hunter8ebf15e2019-09-10 10:29:17 +0100767 return err;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800768 }
769 }
770
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600771 if (!i2c_dev->is_dvc) {
772 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100773
Stephen Warren5afa9d32011-06-06 11:25:19 -0600774 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
775 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
776 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
777 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600778 }
779
Jon Hunter1f50ad22016-08-26 14:09:04 +0100780 err = tegra_i2c_flush_fifos(i2c_dev);
Shardar Shariff Md2148c012016-08-31 18:58:41 +0530781 if (err)
Jon Hunter8ebf15e2019-09-10 10:29:17 +0100782 return err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800783
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530784 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
785 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
786
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530787 err = tegra_i2c_wait_for_config_load(i2c_dev);
788 if (err)
Jon Hunter8ebf15e2019-09-10 10:29:17 +0100789 return err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530790
Jon Hunter8ebf15e2019-09-10 10:29:17 +0100791 return 0;
Colin Crossdb811ca2011-02-20 17:14:21 -0800792}
793
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530794static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
795{
796 u32 cnfg;
797
Jon Hunter54836e22018-07-03 09:55:43 +0100798 /*
799 * NACK interrupt is generated before the I2C controller generates
800 * the STOP condition on the bus. So wait for 2 clock periods
801 * before disabling the controller so that the STOP condition has
802 * been delivered properly.
803 */
804 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
805
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530806 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
807 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
808 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
809
810 return tegra_i2c_wait_for_config_load(i2c_dev);
811}
812
Colin Crossdb811ca2011-02-20 17:14:21 -0800813static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
814{
815 u32 status;
816 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
817 struct tegra_i2c_dev *i2c_dev = dev_id;
818
819 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
820
821 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600822 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
823 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
824 i2c_readl(i2c_dev, I2C_STATUS),
825 i2c_readl(i2c_dev, I2C_CNFG));
826 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
Todd Poynorcb63c622011-04-25 15:32:25 -0600827 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800828 }
829
830 if (unlikely(status & status_err)) {
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530831 tegra_i2c_disable_packet_mode(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800832 if (status & I2C_INT_NO_ACK)
833 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
834 if (status & I2C_INT_ARBITRATION_LOST)
835 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800836 goto err;
837 }
838
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800839 /*
840 * I2C transfer is terminated during the bus clear so skip
841 * processing the other interrupts.
842 */
843 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
844 goto err;
845
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800846 if (!i2c_dev->is_curr_dma_xfer) {
847 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
Bitan Biswas57ca9682019-06-18 04:09:42 -0700848 if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
849 /*
850 * Overflow error condition: message fully sent,
851 * with no XFER_COMPLETE interrupt but hardware
852 * asks to transfer more.
853 */
854 i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
855 goto err;
856 }
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800857 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800858
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800859 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
860 if (i2c_dev->msg_buf_remaining)
861 tegra_i2c_fill_tx_fifo(i2c_dev);
862 else
863 tegra_i2c_mask_irq(i2c_dev,
864 I2C_INT_TX_FIFO_DATA_REQ);
865 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800866 }
867
Laxman Dewanganc889e912012-05-07 12:16:19 +0530868 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
869 if (i2c_dev->is_dvc)
870 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
871
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800872 /*
873 * During message read XFER_COMPLETE interrupt is triggered prior to
874 * DMA completion and during message write XFER_COMPLETE interrupt is
875 * triggered after DMA completion.
876 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
877 * so forcing msg_buf_remaining to 0 in DMA mode.
878 */
Doug Anderson96219c32011-08-30 11:46:10 -0600879 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800880 if (i2c_dev->is_curr_dma_xfer)
881 i2c_dev->msg_buf_remaining = 0;
Bitan Biswas57ca9682019-06-18 04:09:42 -0700882 /*
883 * Underflow error condition: XFER_COMPLETE before message
884 * fully sent.
885 */
886 if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
887 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
888 goto err;
889 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800890 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600891 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530892 goto done;
Colin Crossdb811ca2011-02-20 17:14:21 -0800893err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300894 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800895 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
896 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
897 I2C_INT_RX_FIFO_DATA_REQ);
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800898 if (i2c_dev->hw->supports_bus_clear)
899 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
Colin Crossdb811ca2011-02-20 17:14:21 -0800900 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600901 if (i2c_dev->is_dvc)
902 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530903
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800904 if (i2c_dev->is_curr_dma_xfer) {
905 if (i2c_dev->msg_read)
906 dmaengine_terminate_async(i2c_dev->rx_dma_chan);
907 else
908 dmaengine_terminate_async(i2c_dev->tx_dma_chan);
909
910 complete(&i2c_dev->dma_complete);
911 }
912
Laxman Dewanganc889e912012-05-07 12:16:19 +0530913 complete(&i2c_dev->msg_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530914done:
Colin Crossdb811ca2011-02-20 17:14:21 -0800915 return IRQ_HANDLED;
916}
917
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800918static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
919 size_t len)
920{
921 u32 val, reg;
922 u8 dma_burst;
923 struct dma_slave_config slv_config = {0};
924 struct dma_chan *chan;
925 int ret;
926 unsigned long reg_offset;
927
928 if (i2c_dev->hw->has_mst_fifo)
929 reg = I2C_MST_FIFO_CONTROL;
930 else
931 reg = I2C_FIFO_CONTROL;
932
933 if (i2c_dev->is_curr_dma_xfer) {
934 if (len & 0xF)
935 dma_burst = 1;
936 else if (len & 0x10)
937 dma_burst = 4;
938 else
939 dma_burst = 8;
940
941 if (i2c_dev->msg_read) {
942 chan = i2c_dev->rx_dma_chan;
943 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
944 slv_config.src_addr = i2c_dev->base_phys + reg_offset;
945 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
946 slv_config.src_maxburst = dma_burst;
947
948 if (i2c_dev->hw->has_mst_fifo)
949 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
950 else
951 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
952 } else {
953 chan = i2c_dev->tx_dma_chan;
954 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
955 slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
956 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
957 slv_config.dst_maxburst = dma_burst;
958
959 if (i2c_dev->hw->has_mst_fifo)
960 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
961 else
962 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
963 }
964
965 slv_config.device_fc = true;
966 ret = dmaengine_slave_config(chan, &slv_config);
967 if (ret < 0) {
968 dev_err(i2c_dev->dev, "DMA slave config failed: %d\n",
969 ret);
Colin Ian Kingbb0e9b12019-02-15 15:31:26 +0000970 dev_err(i2c_dev->dev, "falling back to PIO\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800971 tegra_i2c_release_dma(i2c_dev);
972 i2c_dev->is_curr_dma_xfer = false;
973 } else {
974 goto out;
975 }
976 }
977
978 if (i2c_dev->hw->has_mst_fifo)
979 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
980 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
981 else
982 val = I2C_FIFO_CONTROL_TX_TRIG(8) |
983 I2C_FIFO_CONTROL_RX_TRIG(1);
984out:
985 i2c_writel(i2c_dev, val, reg);
986}
987
Dmitry Osipenkob5d56052020-01-14 04:34:37 +0300988static unsigned long
Dmitry Osipenkoede22992020-01-14 04:34:38 +0300989tegra_i2c_poll_completion_timeout(struct tegra_i2c_dev *i2c_dev,
990 struct completion *complete,
991 unsigned int timeout_ms)
992{
993 ktime_t ktime = ktime_get();
994 ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);
995
996 do {
997 u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
998
Wolfram Sang8839e462020-04-27 10:52:22 +0200999 if (status) {
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001000 tegra_i2c_isr(i2c_dev->irq, i2c_dev);
1001
Wolfram Sang8839e462020-04-27 10:52:22 +02001002 if (completion_done(complete)) {
1003 s64 delta = ktime_ms_delta(ktimeout, ktime);
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001004
Wolfram Sang8839e462020-04-27 10:52:22 +02001005 return msecs_to_jiffies(delta) ?: 1;
1006 }
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001007 }
1008
1009 ktime = ktime_get();
1010
1011 } while (ktime_before(ktime, ktimeout));
1012
1013 return 0;
1014}
1015
1016static unsigned long
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001017tegra_i2c_wait_completion_timeout(struct tegra_i2c_dev *i2c_dev,
1018 struct completion *complete,
1019 unsigned int timeout_ms)
1020{
1021 unsigned long ret;
1022
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001023 if (i2c_dev->is_curr_atomic_xfer) {
1024 ret = tegra_i2c_poll_completion_timeout(i2c_dev, complete,
1025 timeout_ms);
1026 } else {
1027 enable_irq(i2c_dev->irq);
1028 ret = wait_for_completion_timeout(complete,
1029 msecs_to_jiffies(timeout_ms));
1030 disable_irq(i2c_dev->irq);
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001031
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001032 /*
Wolfram Sang8839e462020-04-27 10:52:22 +02001033 * There is a chance that completion may happen after IRQ
1034 * synchronization, which is done by disable_irq().
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001035 */
Wolfram Sang8839e462020-04-27 10:52:22 +02001036 if (ret == 0 && completion_done(complete)) {
1037 dev_warn(i2c_dev->dev,
1038 "completion done after timeout\n");
1039 ret = 1;
1040 }
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001041 }
1042
1043 return ret;
1044}
1045
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001046static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
1047{
1048 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1049 int err;
1050 unsigned long time_left;
1051 u32 reg;
1052
1053 reinit_completion(&i2c_dev->msg_complete);
1054 reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) |
1055 I2C_BC_STOP_COND | I2C_BC_TERMINATE;
1056 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1057 if (i2c_dev->hw->has_config_load_reg) {
1058 err = tegra_i2c_wait_for_config_load(i2c_dev);
1059 if (err)
1060 return err;
1061 }
1062
1063 reg |= I2C_BC_ENABLE;
1064 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1065 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1066
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001067 time_left = tegra_i2c_wait_completion_timeout(
1068 i2c_dev, &i2c_dev->msg_complete, 50);
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001069 if (time_left == 0) {
1070 dev_err(i2c_dev->dev, "timed out for bus clear\n");
1071 return -ETIMEDOUT;
1072 }
1073
1074 reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1075 if (!(reg & I2C_BC_STATUS)) {
1076 dev_err(i2c_dev->dev,
1077 "un-recovered arbitration lost\n");
1078 return -EIO;
1079 }
1080
1081 return -EAGAIN;
1082}
1083
Colin Crossdb811ca2011-02-20 17:14:21 -08001084static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Bitan Biswas26955a72019-06-11 03:51:10 -07001085 struct i2c_msg *msg,
1086 enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -08001087{
1088 u32 packet_header;
1089 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001090 unsigned long time_left;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001091 size_t xfer_size;
1092 u32 *buffer = NULL;
1093 int err = 0;
1094 bool dma;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001095 u16 xfer_time = 100;
Colin Crossdb811ca2011-02-20 17:14:21 -08001096
1097 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001098
Colin Crossdb811ca2011-02-20 17:14:21 -08001099 i2c_dev->msg_buf = msg->buf;
1100 i2c_dev->msg_buf_remaining = msg->len;
1101 i2c_dev->msg_err = I2C_ERR_NONE;
1102 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -08001103 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -08001104
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001105 if (i2c_dev->msg_read)
1106 xfer_size = msg->len;
1107 else
1108 xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1109
1110 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
Dmitry Osipenkoae6028a2020-01-14 04:34:39 +03001111 i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_PREFERRED_LEN) &&
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001112 i2c_dev->dma_buf &&
1113 !i2c_dev->is_curr_atomic_xfer;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001114 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1115 dma = i2c_dev->is_curr_dma_xfer;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001116 /*
1117 * Transfer time in mSec = Total bits / transfer rate
1118 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1119 */
1120 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1121 i2c_dev->bus_clk_rate);
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301122
1123 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1124 tegra_i2c_unmask_irq(i2c_dev, int_mask);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001125 if (dma) {
1126 if (i2c_dev->msg_read) {
1127 dma_sync_single_for_device(i2c_dev->dev,
1128 i2c_dev->dma_phys,
1129 xfer_size,
1130 DMA_FROM_DEVICE);
1131 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1132 if (err < 0) {
1133 dev_err(i2c_dev->dev,
1134 "starting RX DMA failed, err %d\n",
1135 err);
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001136 return err;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001137 }
1138
1139 } else {
1140 dma_sync_single_for_cpu(i2c_dev->dev,
1141 i2c_dev->dma_phys,
1142 xfer_size,
1143 DMA_TO_DEVICE);
1144 buffer = i2c_dev->dma_buf;
1145 }
1146 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301147
Colin Crossdb811ca2011-02-20 17:14:21 -08001148 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
1149 PACKET_HEADER0_PROTOCOL_I2C |
1150 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
1151 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001152 if (dma && !i2c_dev->msg_read)
1153 *buffer++ = packet_header;
1154 else
1155 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001156
1157 packet_header = msg->len - 1;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001158 if (dma && !i2c_dev->msg_read)
1159 *buffer++ = packet_header;
1160 else
1161 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001162
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301163 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301164 if (end_state == MSG_END_CONTINUE)
1165 packet_header |= I2C_HEADER_CONTINUE_XFER;
1166 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -06001167 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301168 if (msg->flags & I2C_M_TEN) {
1169 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -08001170 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301171 } else {
1172 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1173 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001174 if (msg->flags & I2C_M_IGNORE_NAK)
1175 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -08001176 if (msg->flags & I2C_M_RD)
1177 packet_header |= I2C_HEADER_READ;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001178 if (dma && !i2c_dev->msg_read)
1179 *buffer++ = packet_header;
1180 else
1181 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001182
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001183 if (!i2c_dev->msg_read) {
1184 if (dma) {
1185 memcpy(buffer, msg->buf, msg->len);
1186 dma_sync_single_for_device(i2c_dev->dev,
1187 i2c_dev->dma_phys,
1188 xfer_size,
1189 DMA_TO_DEVICE);
1190 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1191 if (err < 0) {
1192 dev_err(i2c_dev->dev,
1193 "starting TX DMA failed, err %d\n",
1194 err);
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001195 return err;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001196 }
1197 } else {
1198 tegra_i2c_fill_tx_fifo(i2c_dev);
1199 }
1200 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001201
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301202 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1203 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001204 if (!dma) {
1205 if (msg->flags & I2C_M_RD)
1206 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1207 else if (i2c_dev->msg_buf_remaining)
1208 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1209 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301210
Colin Crossdb811ca2011-02-20 17:14:21 -08001211 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1212 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
1213 i2c_readl(i2c_dev, I2C_INT_MASK));
1214
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001215 if (dma) {
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001216 time_left = tegra_i2c_wait_completion_timeout(
1217 i2c_dev, &i2c_dev->dma_complete, xfer_time);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001218
Dmitry Osipenko28d98662020-01-14 04:34:41 +03001219 dmaengine_terminate_sync(i2c_dev->msg_read ?
1220 i2c_dev->rx_dma_chan :
1221 i2c_dev->tx_dma_chan);
1222
Dmitry Osipenkob3ec9462020-01-14 04:34:42 +03001223 if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001224 dev_err(i2c_dev->dev, "DMA transfer timeout\n");
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001225 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001226 return -ETIMEDOUT;
1227 }
1228
1229 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1230 dma_sync_single_for_cpu(i2c_dev->dev,
1231 i2c_dev->dma_phys,
1232 xfer_size,
1233 DMA_FROM_DEVICE);
1234 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf,
1235 msg->len);
1236 }
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001237 }
1238
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001239 time_left = tegra_i2c_wait_completion_timeout(
1240 i2c_dev, &i2c_dev->msg_complete, xfer_time);
1241
Colin Crossdb811ca2011-02-20 17:14:21 -08001242 tegra_i2c_mask_irq(i2c_dev, int_mask);
1243
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001244 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -08001245 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001246 tegra_i2c_init(i2c_dev, true);
Colin Crossdb811ca2011-02-20 17:14:21 -08001247 return -ETIMEDOUT;
1248 }
1249
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001250 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1251 time_left, completion_done(&i2c_dev->msg_complete),
1252 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -08001253
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001254 i2c_dev->is_curr_dma_xfer = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001255 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
1256 return 0;
1257
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001258 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001259 /* start recovery upon arbitration loss in single master mode */
1260 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1261 if (!i2c_dev->is_multimaster_mode)
1262 return i2c_recover_bus(&i2c_dev->adapter);
1263 return -EAGAIN;
1264 }
1265
Colin Crossdb811ca2011-02-20 17:14:21 -08001266 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1267 if (msg->flags & I2C_M_IGNORE_NAK)
1268 return 0;
1269 return -EREMOTEIO;
1270 }
1271
1272 return -EIO;
1273}
1274
1275static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
Bitan Biswas26955a72019-06-11 03:51:10 -07001276 int num)
Colin Crossdb811ca2011-02-20 17:14:21 -08001277{
1278 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1279 int i;
Bitan Biswas233d0ab2019-06-11 03:51:09 -07001280 int ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001281
Jon Hunter1f50ad22016-08-26 14:09:04 +01001282 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001283 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +01001284 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001285 return ret;
1286 }
1287
Colin Crossdb811ca2011-02-20 17:14:21 -08001288 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301289 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +01001290
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301291 if (i < (num - 1)) {
1292 if (msgs[i + 1].flags & I2C_M_NOSTART)
1293 end_type = MSG_END_CONTINUE;
1294 else
1295 end_type = MSG_END_REPEAT_START;
1296 }
1297 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -08001298 if (ret)
1299 break;
1300 }
Jon Hunter1f50ad22016-08-26 14:09:04 +01001301
1302 pm_runtime_put(i2c_dev->dev);
1303
Colin Crossdb811ca2011-02-20 17:14:21 -08001304 return ret ?: i;
1305}
1306
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001307static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
1308 struct i2c_msg msgs[], int num)
1309{
1310 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1311 int ret;
1312
1313 i2c_dev->is_curr_atomic_xfer = true;
1314 ret = tegra_i2c_xfer(adap, msgs, num);
1315 i2c_dev->is_curr_atomic_xfer = false;
1316
1317 return ret;
1318}
1319
Colin Crossdb811ca2011-02-20 17:14:21 -08001320static u32 tegra_i2c_func(struct i2c_adapter *adap)
1321{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301322 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +02001323 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1324 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301325
1326 if (i2c_dev->hw->has_continue_xfer_support)
1327 ret |= I2C_FUNC_NOSTART;
1328 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001329}
1330
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301331static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1332{
1333 struct device_node *np = i2c_dev->dev->of_node;
1334 int ret;
Bitan Biswas26955a72019-06-11 03:51:10 -07001335 bool multi_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301336
1337 ret = of_property_read_u32(np, "clock-frequency",
Bitan Biswas26955a72019-06-11 03:51:10 -07001338 &i2c_dev->bus_clk_rate);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301339 if (ret)
Andy Shevchenko90224e62020-03-24 14:32:16 +02001340 i2c_dev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301341
Bitan Biswas26955a72019-06-11 03:51:10 -07001342 multi_mode = of_property_read_bool(np, "multi-master");
1343 i2c_dev->is_multimaster_mode = multi_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301344}
1345
Colin Crossdb811ca2011-02-20 17:14:21 -08001346static const struct i2c_algorithm tegra_i2c_algo = {
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001347 .master_xfer = tegra_i2c_xfer,
1348 .master_xfer_atomic = tegra_i2c_xfer_atomic,
1349 .functionality = tegra_i2c_func,
Colin Crossdb811ca2011-02-20 17:14:21 -08001350};
1351
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001352/* payload size is only 12 bit */
Bhumika Goyalae3923a2017-08-21 17:42:04 +05301353static const struct i2c_adapter_quirks tegra_i2c_quirks = {
Wolfram Sangc96c0f22018-07-23 22:26:12 +02001354 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001355 .max_read_len = SZ_4K,
1356 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001357};
1358
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001359static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1360 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001361 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001362};
1363
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001364static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1365 .recover_bus = tegra_i2c_issue_bus_clear,
1366};
1367
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301368static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1369 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301370 .has_per_pkt_xfer_complete_irq = false,
1371 .has_single_clk_source = false,
1372 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001373 .clk_divisor_std_mode = 0,
1374 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301375 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301376 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301377 .has_multi_master_mode = false,
1378 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001379 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001380 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001381 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001382 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001383 .tlow_std_mode = 0x4,
1384 .thigh_std_mode = 0x2,
1385 .tlow_fast_fastplus_mode = 0x4,
1386 .thigh_fast_fastplus_mode = 0x2,
1387 .setup_hold_time_std_mode = 0x0,
1388 .setup_hold_time_fast_fast_plus_mode = 0x0,
1389 .setup_hold_time_hs_mode = 0x0,
1390 .has_interface_timing_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301391};
1392
1393static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1394 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301395 .has_per_pkt_xfer_complete_irq = false,
1396 .has_single_clk_source = false,
1397 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001398 .clk_divisor_std_mode = 0,
1399 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301400 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301401 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301402 .has_multi_master_mode = false,
1403 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001404 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001405 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001406 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001407 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001408 .tlow_std_mode = 0x4,
1409 .thigh_std_mode = 0x2,
1410 .tlow_fast_fastplus_mode = 0x4,
1411 .thigh_fast_fastplus_mode = 0x2,
1412 .setup_hold_time_std_mode = 0x0,
1413 .setup_hold_time_fast_fast_plus_mode = 0x0,
1414 .setup_hold_time_hs_mode = 0x0,
1415 .has_interface_timing_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301416};
1417
1418static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1419 .has_continue_xfer_support = true,
1420 .has_per_pkt_xfer_complete_irq = true,
1421 .has_single_clk_source = true,
1422 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001423 .clk_divisor_std_mode = 0x19,
1424 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301425 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301426 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301427 .has_multi_master_mode = false,
1428 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001429 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001430 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001431 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001432 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001433 .tlow_std_mode = 0x4,
1434 .thigh_std_mode = 0x2,
1435 .tlow_fast_fastplus_mode = 0x4,
1436 .thigh_fast_fastplus_mode = 0x2,
1437 .setup_hold_time_std_mode = 0x0,
1438 .setup_hold_time_fast_fast_plus_mode = 0x0,
1439 .setup_hold_time_hs_mode = 0x0,
1440 .has_interface_timing_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301441};
1442
1443static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1444 .has_continue_xfer_support = true,
1445 .has_per_pkt_xfer_complete_irq = true,
1446 .has_single_clk_source = true,
1447 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001448 .clk_divisor_std_mode = 0x19,
1449 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301450 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301451 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301452 .has_multi_master_mode = false,
1453 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001454 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001455 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001456 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001457 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001458 .tlow_std_mode = 0x4,
1459 .thigh_std_mode = 0x2,
1460 .tlow_fast_fastplus_mode = 0x4,
1461 .thigh_fast_fastplus_mode = 0x2,
1462 .setup_hold_time_std_mode = 0x0,
1463 .setup_hold_time_fast_fast_plus_mode = 0x0,
1464 .setup_hold_time_hs_mode = 0x0,
1465 .has_interface_timing_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301466};
1467
1468static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1469 .has_continue_xfer_support = true,
1470 .has_per_pkt_xfer_complete_irq = true,
1471 .has_single_clk_source = true,
1472 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001473 .clk_divisor_std_mode = 0x19,
1474 .clk_divisor_fast_mode = 0x19,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301475 .clk_divisor_fast_plus_mode = 0x10,
1476 .has_config_load_reg = true,
Sowjanya Komatineni6b9932b2019-02-19 09:28:52 -08001477 .has_multi_master_mode = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301478 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001479 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001480 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001481 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001482 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001483 .tlow_std_mode = 0x4,
1484 .thigh_std_mode = 0x2,
1485 .tlow_fast_fastplus_mode = 0x4,
1486 .thigh_fast_fastplus_mode = 0x2,
1487 .setup_hold_time_std_mode = 0,
1488 .setup_hold_time_fast_fast_plus_mode = 0,
1489 .setup_hold_time_hs_mode = 0,
1490 .has_interface_timing_reg = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001491};
1492
1493static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1494 .has_continue_xfer_support = true,
1495 .has_per_pkt_xfer_complete_irq = true,
1496 .has_single_clk_source = true,
1497 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001498 .clk_divisor_std_mode = 0x16,
1499 .clk_divisor_fast_mode = 0x19,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001500 .clk_divisor_fast_plus_mode = 0x10,
1501 .has_config_load_reg = true,
Sowjanya Komatineni6b9932b2019-02-19 09:28:52 -08001502 .has_multi_master_mode = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001503 .has_slcg_override_reg = true,
Sowjanya Komatineni9ffc1252019-02-19 09:28:51 -08001504 .has_mst_fifo = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001505 .quirks = &tegra_i2c_quirks,
1506 .supports_bus_clear = true,
1507 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001508 .tlow_std_mode = 0x4,
1509 .thigh_std_mode = 0x3,
1510 .tlow_fast_fastplus_mode = 0x4,
1511 .thigh_fast_fastplus_mode = 0x2,
1512 .setup_hold_time_std_mode = 0,
1513 .setup_hold_time_fast_fast_plus_mode = 0,
1514 .setup_hold_time_hs_mode = 0,
1515 .has_interface_timing_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001516};
1517
1518static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1519 .has_continue_xfer_support = true,
1520 .has_per_pkt_xfer_complete_irq = true,
1521 .has_single_clk_source = true,
1522 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001523 .clk_divisor_std_mode = 0x4f,
1524 .clk_divisor_fast_mode = 0x3c,
1525 .clk_divisor_fast_plus_mode = 0x16,
Thierry Redingc5907c62018-06-19 12:49:42 +02001526 .has_config_load_reg = true,
1527 .has_multi_master_mode = true,
1528 .has_slcg_override_reg = true,
1529 .has_mst_fifo = true,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001530 .quirks = &tegra194_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001531 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001532 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001533 .tlow_std_mode = 0x8,
1534 .thigh_std_mode = 0x7,
1535 .tlow_fast_fastplus_mode = 0x2,
1536 .thigh_fast_fastplus_mode = 0x2,
1537 .setup_hold_time_std_mode = 0x08080808,
1538 .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1539 .setup_hold_time_hs_mode = 0x090909,
1540 .has_interface_timing_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301541};
1542
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301543/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001544static const struct of_device_id tegra_i2c_of_match[] = {
Thierry Redingc5907c62018-06-19 12:49:42 +02001545 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001546 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301547 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301548 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301549 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301550 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1551 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1552 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1553 {},
1554};
1555MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301556
Bill Pemberton0b255e92012-11-27 15:59:38 -05001557static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001558{
1559 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -08001560 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301561 struct clk *div_clk;
1562 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -07001563 void __iomem *base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001564 phys_addr_t base_phys;
Colin Crossdb811ca2011-02-20 17:14:21 -08001565 int irq;
Bitan Biswas233d0ab2019-06-11 03:51:09 -07001566 int ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001567
1568 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001569 base_phys = res->start;
Thierry Reding84dbf802013-01-21 11:09:03 +01001570 base = devm_ioremap_resource(&pdev->dev, res);
1571 if (IS_ERR(base))
1572 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -08001573
1574 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1575 if (!res) {
1576 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301577 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -08001578 }
1579 irq = res->start;
1580
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301581 div_clk = devm_clk_get(&pdev->dev, "div-clk");
1582 if (IS_ERR(div_clk)) {
Thierry Redingd680a502019-05-27 12:29:39 +02001583 if (PTR_ERR(div_clk) != -EPROBE_DEFER)
1584 dev_err(&pdev->dev, "missing controller clock\n");
1585
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301586 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -08001587 }
1588
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301589 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001590 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301591 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -08001592
1593 i2c_dev->base = base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001594 i2c_dev->base_phys = base_phys;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301595 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -08001596 i2c_dev->adapter.algo = &tegra_i2c_algo;
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001597 i2c_dev->adapter.retries = 1;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001598 i2c_dev->adapter.timeout = 6 * HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -08001599 i2c_dev->irq = irq;
1600 i2c_dev->cont_id = pdev->id;
1601 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -07001602
Philipp Zabel94d3b652017-07-19 17:25:34 +02001603 i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001604 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001605 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001606 return PTR_ERR(i2c_dev->rst);
1607 }
1608
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301609 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001610
Jon Huntera9e32cd2016-08-26 14:09:01 +01001611 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
1612 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
1613 "nvidia,tegra20-i2c-dvc");
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001614 i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001615 i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len +
1616 I2C_PACKET_HEADER_SIZE;
Colin Crossdb811ca2011-02-20 17:14:21 -08001617 init_completion(&i2c_dev->msg_complete);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001618 init_completion(&i2c_dev->dma_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -08001619
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301620 if (!i2c_dev->hw->has_single_clk_source) {
1621 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
1622 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001623 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301624 return PTR_ERR(fast_clk);
1625 }
1626 i2c_dev->fast_clk = fast_clk;
1627 }
1628
Colin Crossdb811ca2011-02-20 17:14:21 -08001629 platform_set_drvdata(pdev, i2c_dev);
1630
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001631 if (!i2c_dev->hw->has_single_clk_source) {
1632 ret = clk_prepare(i2c_dev->fast_clk);
1633 if (ret < 0) {
1634 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1635 return ret;
1636 }
1637 }
1638
Andy Shevchenko90224e62020-03-24 14:32:16 +02001639 if (i2c_dev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ &&
1640 i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_PLUS_FREQ)
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301641 i2c_dev->clk_divisor_non_hs_mode =
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001642 i2c_dev->hw->clk_divisor_fast_plus_mode;
Andy Shevchenko90224e62020-03-24 14:32:16 +02001643 else if (i2c_dev->bus_clk_rate > I2C_MAX_STANDARD_MODE_FREQ &&
1644 i2c_dev->bus_clk_rate <= I2C_MAX_FAST_MODE_FREQ)
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001645 i2c_dev->clk_divisor_non_hs_mode =
1646 i2c_dev->hw->clk_divisor_fast_mode;
1647 else
1648 i2c_dev->clk_divisor_non_hs_mode =
1649 i2c_dev->hw->clk_divisor_std_mode;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001650
1651 ret = clk_prepare(i2c_dev->div_clk);
1652 if (ret < 0) {
1653 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1654 goto unprepare_fast_clk;
1655 }
1656
Dmitry Osipenkoede22992020-01-14 04:34:38 +03001657 pm_runtime_irq_safe(&pdev->dev);
Jon Hunter1f50ad22016-08-26 14:09:04 +01001658 pm_runtime_enable(&pdev->dev);
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001659 if (!pm_runtime_enabled(&pdev->dev)) {
Jon Hunter1f50ad22016-08-26 14:09:04 +01001660 ret = tegra_i2c_runtime_resume(&pdev->dev);
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001661 if (ret < 0) {
1662 dev_err(&pdev->dev, "runtime resume failed\n");
1663 goto unprepare_div_clk;
1664 }
1665 } else {
Jon Hunter8ebf15e2019-09-10 10:29:17 +01001666 ret = pm_runtime_get_sync(i2c_dev->dev);
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001667 if (ret < 0) {
1668 dev_err(&pdev->dev, "runtime resume failed\n");
1669 goto disable_rpm;
1670 }
Jon Hunter1f50ad22016-08-26 14:09:04 +01001671 }
1672
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301673 if (i2c_dev->is_multimaster_mode) {
1674 ret = clk_enable(i2c_dev->div_clk);
1675 if (ret < 0) {
1676 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
1677 ret);
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001678 goto put_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301679 }
1680 }
1681
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001682 if (i2c_dev->hw->supports_bus_clear)
1683 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1684
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001685 ret = tegra_i2c_init_dma(i2c_dev);
1686 if (ret < 0)
1687 goto disable_div_clk;
1688
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001689 ret = tegra_i2c_init(i2c_dev, false);
Colin Crossdb811ca2011-02-20 17:14:21 -08001690 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001691 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001692 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001693 }
1694
Dmitry Osipenkob5d56052020-01-14 04:34:37 +03001695 irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);
1696
Thierry Reding26ca88a2020-05-06 20:47:25 +02001697 ret = devm_request_irq(&pdev->dev, i2c_dev->irq, tegra_i2c_isr,
1698 IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001699 if (ret) {
1700 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001701 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001702 }
1703
Colin Crossdb811ca2011-02-20 17:14:21 -08001704 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1705 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +02001706 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +01001707 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -08001708 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -08001709 i2c_dev->adapter.dev.parent = &pdev->dev;
1710 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -07001711 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -08001712
1713 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +02001714 if (ret)
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001715 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001716
Jon Hunter8ebf15e2019-09-10 10:29:17 +01001717 pm_runtime_put(&pdev->dev);
1718
Colin Crossdb811ca2011-02-20 17:14:21 -08001719 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001720
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001721release_dma:
1722 tegra_i2c_release_dma(i2c_dev);
1723
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301724disable_div_clk:
1725 if (i2c_dev->is_multimaster_mode)
1726 clk_disable(i2c_dev->div_clk);
1727
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001728put_rpm:
1729 if (pm_runtime_enabled(&pdev->dev))
1730 pm_runtime_put_sync(&pdev->dev);
1731 else
Jon Hunter1f50ad22016-08-26 14:09:04 +01001732 tegra_i2c_runtime_suspend(&pdev->dev);
1733
Dmitry Osipenko24a49672020-01-14 04:34:36 +03001734disable_rpm:
1735 if (pm_runtime_enabled(&pdev->dev))
1736 pm_runtime_disable(&pdev->dev);
1737
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001738unprepare_div_clk:
1739 clk_unprepare(i2c_dev->div_clk);
1740
1741unprepare_fast_clk:
1742 if (!i2c_dev->hw->has_single_clk_source)
1743 clk_unprepare(i2c_dev->fast_clk);
1744
1745 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001746}
1747
Bill Pemberton0b255e92012-11-27 15:59:38 -05001748static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001749{
1750 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +01001751
Colin Crossdb811ca2011-02-20 17:14:21 -08001752 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001753
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301754 if (i2c_dev->is_multimaster_mode)
1755 clk_disable(i2c_dev->div_clk);
1756
Jon Hunter1f50ad22016-08-26 14:09:04 +01001757 pm_runtime_disable(&pdev->dev);
1758 if (!pm_runtime_status_suspended(&pdev->dev))
1759 tegra_i2c_runtime_suspend(&pdev->dev);
1760
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001761 clk_unprepare(i2c_dev->div_clk);
1762 if (!i2c_dev->hw->has_single_clk_source)
1763 clk_unprepare(i2c_dev->fast_clk);
1764
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001765 tegra_i2c_release_dma(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001766 return 0;
1767}
1768
Dmitry Osipenkoe5738bc2019-07-08 02:12:34 +03001769static int __maybe_unused tegra_i2c_suspend(struct device *dev)
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001770{
1771 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Thierry Reding44c99902019-12-13 14:44:17 +01001772 int err = 0;
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001773
1774 i2c_mark_adapter_suspended(&i2c_dev->adapter);
1775
Thierry Reding44c99902019-12-13 14:44:17 +01001776 if (!pm_runtime_status_suspended(dev))
1777 err = tegra_i2c_runtime_suspend(dev);
1778
1779 return err;
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001780}
1781
Dmitry Osipenkoe5738bc2019-07-08 02:12:34 +03001782static int __maybe_unused tegra_i2c_resume(struct device *dev)
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001783{
1784 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1785 int err;
1786
Thierry Reding44c99902019-12-13 14:44:17 +01001787 /*
1788 * We need to ensure that clocks are enabled so that registers can be
1789 * restored in tegra_i2c_init().
1790 */
Jon Hunter8ebf15e2019-09-10 10:29:17 +01001791 err = tegra_i2c_runtime_resume(dev);
1792 if (err)
1793 return err;
1794
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001795 err = tegra_i2c_init(i2c_dev, false);
1796 if (err)
1797 return err;
1798
Thierry Reding44c99902019-12-13 14:44:17 +01001799 /*
1800 * In case we are runtime suspended, disable clocks again so that we
1801 * don't unbalance the clock reference counts during the next runtime
1802 * resume transition.
1803 */
1804 if (pm_runtime_status_suspended(dev)) {
1805 err = tegra_i2c_runtime_suspend(dev);
1806 if (err)
1807 return err;
1808 }
Jon Hunter8ebf15e2019-09-10 10:29:17 +01001809
Bitan Biswasacc8abc2019-06-06 22:37:47 -07001810 i2c_mark_adapter_resumed(&i2c_dev->adapter);
1811
1812 return 0;
1813}
1814
Jon Hunter1f50ad22016-08-26 14:09:04 +01001815static const struct dev_pm_ops tegra_i2c_pm = {
Jon Hunter8ebf15e2019-09-10 10:29:17 +01001816 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
Jon Hunter1f50ad22016-08-26 14:09:04 +01001817 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1818 NULL)
Jon Hunter1f50ad22016-08-26 14:09:04 +01001819};
Bitan Biswas26955a72019-06-11 03:51:10 -07001820
Colin Crossdb811ca2011-02-20 17:14:21 -08001821static struct platform_driver tegra_i2c_driver = {
1822 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001823 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001824 .driver = {
1825 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001826 .of_match_table = tegra_i2c_of_match,
Dmitry Osipenkoe5738bc2019-07-08 02:12:34 +03001827 .pm = &tegra_i2c_pm,
Colin Crossdb811ca2011-02-20 17:14:21 -08001828 },
1829};
1830
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001831module_platform_driver(tegra_i2c_driver);
Colin Crossdb811ca2011-02-20 17:14:21 -08001832
1833MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1834MODULE_AUTHOR("Colin Cross");
1835MODULE_LICENSE("GPL v2");