blob: ebaa78d17d6ecd4d61ed405f9472e92da940b3ff [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>
Sowjanya Komatinenica865542019-02-12 11:06:42 -080019#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 Crossdb811ca2011-02-20 17:14:21 -080026
Colin Crossdb811ca2011-02-20 17:14:21 -080027#define BYTES_PER_FIFO_WORD 4
28
29#define I2C_CNFG 0x000
Jay Cheng40abcf72011-04-25 15:32:27 -060030#define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
Jon Hunter2929be22016-08-26 14:08:58 +010031#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 Poynorcb63c622011-04-25 15:32:25 -060034#define I2C_STATUS 0x01C
Colin Crossdb811ca2011-02-20 17:14:21 -080035#define I2C_SL_CNFG 0x020
Jon Hunter2929be22016-08-26 14:08:58 +010036#define I2C_SL_CNFG_NACK BIT(1)
37#define I2C_SL_CNFG_NEWSL BIT(2)
Colin Crossdb811ca2011-02-20 17:14:21 -080038#define I2C_SL_ADDR1 0x02c
Stephen Warren5afa9d32011-06-06 11:25:19 -060039#define I2C_SL_ADDR2 0x030
Colin Crossdb811ca2011-02-20 17:14:21 -080040#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 Hunter2929be22016-08-26 14:08:58 +010044#define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
45#define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -080046#define I2C_FIFO_CONTROL_TX_TRIG(x) (((x) - 1) << 5)
47#define I2C_FIFO_CONTROL_RX_TRIG(x) (((x) - 1) << 2)
Colin Crossdb811ca2011-02-20 17:14:21 -080048#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 Komatinenice956242019-02-12 11:06:43 -080055#define I2C_INT_BUS_CLR_DONE BIT(11)
Jon Hunter2929be22016-08-26 14:08:58 +010056#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 Crossdb811ca2011-02-20 17:14:21 -080064#define I2C_CLK_DIVISOR 0x06c
Laxman Dewangan2a2897b2013-01-05 17:34:46 +053065#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
66#define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
Colin Crossdb811ca2011-02-20 17:14:21 -080067
68#define DVC_CTRL_REG1 0x000
Jon Hunter2929be22016-08-26 14:08:58 +010069#define DVC_CTRL_REG1_INTR_EN BIT(10)
Colin Crossdb811ca2011-02-20 17:14:21 -080070#define DVC_CTRL_REG2 0x004
71#define DVC_CTRL_REG3 0x008
Jon Hunter2929be22016-08-26 14:08:58 +010072#define DVC_CTRL_REG3_SW_PROG BIT(26)
73#define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080074#define DVC_STATUS 0x00c
Jon Hunter2929be22016-08-26 14:08:58 +010075#define DVC_STATUS_I2C_DONE_INTR BIT(30)
Colin Crossdb811ca2011-02-20 17:14:21 -080076
77#define I2C_ERR_NONE 0x00
78#define I2C_ERR_NO_ACK 0x01
79#define I2C_ERR_ARBITRATION_LOST 0x02
Todd Poynorcb63c622011-04-25 15:32:25 -060080#define I2C_ERR_UNKNOWN_INTERRUPT 0x04
Colin Crossdb811ca2011-02-20 17:14:21 -080081
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 Hunter2929be22016-08-26 14:08:58 +010085#define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
Colin Crossdb811ca2011-02-20 17:14:21 -080086
Jon Hunter2929be22016-08-26 14:08:58 +010087#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 Crossdb811ca2011-02-20 17:14:21 -080095#define I2C_HEADER_MASTER_ADDR_SHIFT 12
96#define I2C_HEADER_SLAVE_ADDR_SHIFT 1
Laxman Dewangan6f4664b2015-06-30 16:24:26 +053097
Sowjanya Komatinenice956242019-02-12 11:06:43 -080098#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 Dewangan6f4664b2015-06-30 16:24:26 +0530107#define I2C_CONFIG_LOAD 0x08C
Jon Hunter2929be22016-08-26 14:08:58 +0100108#define I2C_MSTR_CONFIG_LOAD BIT(0)
109#define I2C_SLV_CONFIG_LOAD BIT(1)
110#define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530111
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530112#define I2C_CLKEN_OVERRIDE 0x090
Jon Hunter2929be22016-08-26 14:08:58 +0100113#define I2C_MST_CORE_CLKEN_OVR BIT(0)
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530114
Shardar Shariff Md685143a12016-08-31 18:58:40 +0530115#define I2C_CONFIG_LOAD_TIMEOUT 1000000
116
Thierry Redingc5907c62018-06-19 12:49:42 +0200117#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 Komatineni0940d242019-02-12 11:06:48 -0800129#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 Komatinenib03ff2a2019-02-12 11:06:45 -0800138/* Packet header size in bytes */
139#define I2C_PACKET_HEADER_SIZE 12
140
Laxman Dewanganc8f5af22012-06-13 15:42:38 +0530141/*
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800142 * 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 Dewanganc8f5af22012-06-13 15:42:38 +0530149 * 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 */
155enum msg_end_type {
156 MSG_END_STOP,
157 MSG_END_REPEAT_START,
158 MSG_END_CONTINUE,
159};
Colin Crossdb811ca2011-02-20 17:14:21 -0800160
161/**
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530162 * struct tegra_i2c_hw_feature : Different HW support on Tegra
163 * @has_continue_xfer_support: Continue transfer supports.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530164 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
165 * complete interrupt per packet basis.
Thierry Redingc990bba2018-12-17 15:16:52 +0100166 * @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 Dewangan2a2897b2013-01-05 17:34:46 +0530168 * fast-clk.
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530169 * @has_config_load_reg: Has the config load register to load the new
170 * configuration.
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530171 * @clk_divisor_hs_mode: Clock divisor in HS mode.
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800172 * @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 Dewangan2a2897b2013-01-05 17:34:46 +0530176 * applicable if there is no fast clock source i.e. single clock
177 * source.
Thierry Reding0604ee42018-12-17 15:16:53 +0100178 * @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 Komatinenib67d4532019-01-08 13:59:10 -0800188 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
189 * allowing 0 length transfers.
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800190 * @supports_bus_clear: Bus Clear support to recover from bus hang during
191 * SDA stuck low from device for some unknown reasons.
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800192 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800193 * @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 Dewangan6ad068e2012-08-19 00:47:46 +0530205 */
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530206struct tegra_i2c_hw_feature {
207 bool has_continue_xfer_support;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530208 bool has_per_pkt_xfer_complete_irq;
209 bool has_single_clk_source;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530210 bool has_config_load_reg;
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530211 int clk_divisor_hs_mode;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800212 int clk_divisor_std_mode;
213 int clk_divisor_fast_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530214 u16 clk_divisor_fast_plus_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530215 bool has_multi_master_mode;
216 bool has_slcg_override_reg;
Thierry Redingc5907c62018-06-19 12:49:42 +0200217 bool has_mst_fifo;
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -0800218 const struct i2c_adapter_quirks *quirks;
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800219 bool supports_bus_clear;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800220 bool has_apb_dma;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800221 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 Dewangan6ad068e2012-08-19 00:47:46 +0530229};
230
231/**
Thierry Redingc990bba2018-12-17 15:16:52 +0100232 * struct tegra_i2c_dev - per device I2C context
Colin Crossdb811ca2011-02-20 17:14:21 -0800233 * @dev: device reference for power management
Thierry Redingc990bba2018-12-17 15:16:52 +0100234 * @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 Reding0604ee42018-12-17 15:16:53 +0100238 * @rst: reset control for the I2C controller
Colin Crossdb811ca2011-02-20 17:14:21 -0800239 * @base: ioremapped registers cookie
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800240 * @base_phys: physical base address of the I2C controller
Thierry Redingc990bba2018-12-17 15:16:52 +0100241 * @cont_id: I2C controller ID, used for packet header
242 * @irq: IRQ number of transfer complete interrupt
Thierry Reding0604ee42018-12-17 15:16:53 +0100243 * @irq_disabled: used to track whether or not the interrupt is enabled
Thierry Redingc990bba2018-12-17 15:16:52 +0100244 * @is_dvc: identifies the DVC I2C controller, has a different register layout
Colin Crossdb811ca2011-02-20 17:14:21 -0800245 * @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 Redingc990bba2018-12-17 15:16:52 +0100250 * @bus_clk_rate: current I2C bus clock rate
Thierry Reding0604ee42018-12-17 15:16:53 +0100251 * @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 Komatineni86c92b92019-02-12 11:06:46 -0800254 * @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 Crossdb811ca2011-02-20 17:14:21 -0800261 */
262struct tegra_i2c_dev {
263 struct device *dev;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +0530264 const struct tegra_i2c_hw_feature *hw;
Colin Crossdb811ca2011-02-20 17:14:21 -0800265 struct i2c_adapter adapter;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +0530266 struct clk *div_clk;
267 struct clk *fast_clk;
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700268 struct reset_control *rst;
Colin Crossdb811ca2011-02-20 17:14:21 -0800269 void __iomem *base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800270 phys_addr_t base_phys;
Colin Crossdb811ca2011-02-20 17:14:21 -0800271 int cont_id;
272 int irq;
Todd Poynorcb63c622011-04-25 15:32:25 -0600273 bool irq_disabled;
Colin Crossdb811ca2011-02-20 17:14:21 -0800274 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 Warren49a64ac2013-03-21 08:08:46 +0000280 u32 bus_clk_rate;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530281 u16 clk_divisor_non_hs_mode;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530282 bool is_multimaster_mode;
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530283 spinlock_t xfer_lock;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800284 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 Crossdb811ca2011-02-20 17:14:21 -0800291};
292
Jon Hunterc7ae44e82016-08-26 14:08:57 +0100293static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
294 unsigned long reg)
Colin Crossdb811ca2011-02-20 17:14:21 -0800295{
296 writel(val, i2c_dev->base + reg);
297}
298
299static 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 */
308static 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
316static 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 Dewanganec7aaca2012-06-13 15:42:36 +0530320
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 Crossdb811ca2011-02-20 17:14:21 -0800324}
325
326static 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
331static 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
337static 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
343static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
344{
Jon Hunterf5076682016-08-26 14:08:59 +0100345 u32 int_mask;
346
347 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800348 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
349}
350
351static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
352{
Jon Hunterf5076682016-08-26 14:08:59 +0100353 u32 int_mask;
354
355 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
Colin Crossdb811ca2011-02-20 17:14:21 -0800356 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
357}
358
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800359static 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
366static 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
391static 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
410static 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
Jonathan Hunter89328b12019-02-21 15:00:38 +0000417 if (!i2c_dev->hw->has_apb_dma)
418 return 0;
419
420 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
421 dev_dbg(i2c_dev->dev, "Support for APB DMA not enabled!\n");
422 return 0;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800423 }
424
425 chan = dma_request_slave_channel_reason(i2c_dev->dev, "rx");
426 if (IS_ERR(chan)) {
427 err = PTR_ERR(chan);
428 goto err_out;
429 }
430
431 i2c_dev->rx_dma_chan = chan;
432
433 chan = dma_request_slave_channel_reason(i2c_dev->dev, "tx");
434 if (IS_ERR(chan)) {
435 err = PTR_ERR(chan);
436 goto err_out;
437 }
438
439 i2c_dev->tx_dma_chan = chan;
440
441 dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
442 &dma_phys, GFP_KERNEL | __GFP_NOWARN);
443 if (!dma_buf) {
444 dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n");
445 err = -ENOMEM;
446 goto err_out;
447 }
448
449 i2c_dev->dma_buf = dma_buf;
450 i2c_dev->dma_phys = dma_phys;
451 return 0;
452
453err_out:
454 tegra_i2c_release_dma(i2c_dev);
455 if (err != -EPROBE_DEFER) {
456 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
Colin Ian Kingbb0e9b12019-02-15 15:31:26 +0000457 dev_err(i2c_dev->dev, "falling back to PIO\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800458 return 0;
459 }
460
461 return err;
462}
463
Colin Crossdb811ca2011-02-20 17:14:21 -0800464static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
465{
466 unsigned long timeout = jiffies + HZ;
Thierry Redingc5907c62018-06-19 12:49:42 +0200467 unsigned int offset;
468 u32 mask, val;
Jon Hunterf5076682016-08-26 14:08:59 +0100469
Thierry Redingc5907c62018-06-19 12:49:42 +0200470 if (i2c_dev->hw->has_mst_fifo) {
471 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
472 I2C_MST_FIFO_CONTROL_RX_FLUSH;
473 offset = I2C_MST_FIFO_CONTROL;
474 } else {
475 mask = I2C_FIFO_CONTROL_TX_FLUSH |
476 I2C_FIFO_CONTROL_RX_FLUSH;
477 offset = I2C_FIFO_CONTROL;
478 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800479
Thierry Redingc5907c62018-06-19 12:49:42 +0200480 val = i2c_readl(i2c_dev, offset);
481 val |= mask;
482 i2c_writel(i2c_dev, val, offset);
483
484 while (i2c_readl(i2c_dev, offset) & mask) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800485 if (time_after(jiffies, timeout)) {
486 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
487 return -ETIMEDOUT;
488 }
489 msleep(1);
490 }
491 return 0;
492}
493
494static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
495{
496 u32 val;
497 int rx_fifo_avail;
498 u8 *buf = i2c_dev->msg_buf;
499 size_t buf_remaining = i2c_dev->msg_buf_remaining;
500 int words_to_transfer;
501
Thierry Redingc5907c62018-06-19 12:49:42 +0200502 if (i2c_dev->hw->has_mst_fifo) {
503 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
504 rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
505 I2C_MST_FIFO_STATUS_RX_SHIFT;
506 } else {
507 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
508 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
509 I2C_FIFO_STATUS_RX_SHIFT;
510 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800511
512 /* Rounds down to not include partial word at the end of buf */
513 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
514 if (words_to_transfer > rx_fifo_avail)
515 words_to_transfer = rx_fifo_avail;
516
517 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
518
519 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
520 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
521 rx_fifo_avail -= words_to_transfer;
522
523 /*
524 * If there is a partial word at the end of buf, handle it manually to
525 * prevent overwriting past the end of buf
526 */
527 if (rx_fifo_avail > 0 && buf_remaining > 0) {
528 BUG_ON(buf_remaining > 3);
529 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300530 val = cpu_to_le32(val);
Colin Crossdb811ca2011-02-20 17:14:21 -0800531 memcpy(buf, &val, buf_remaining);
532 buf_remaining = 0;
533 rx_fifo_avail--;
534 }
535
536 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
537 i2c_dev->msg_buf_remaining = buf_remaining;
538 i2c_dev->msg_buf = buf;
Thierry Redingc5907c62018-06-19 12:49:42 +0200539
Colin Crossdb811ca2011-02-20 17:14:21 -0800540 return 0;
541}
542
543static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
544{
545 u32 val;
546 int tx_fifo_avail;
547 u8 *buf = i2c_dev->msg_buf;
548 size_t buf_remaining = i2c_dev->msg_buf_remaining;
549 int words_to_transfer;
550
Thierry Redingc5907c62018-06-19 12:49:42 +0200551 if (i2c_dev->hw->has_mst_fifo) {
552 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
553 tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
554 I2C_MST_FIFO_STATUS_TX_SHIFT;
555 } else {
556 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
557 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
558 I2C_FIFO_STATUS_TX_SHIFT;
559 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800560
561 /* Rounds down to not include partial word at the end of buf */
562 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800563
Doug Anderson96219c32011-08-30 11:46:10 -0600564 /* It's very common to have < 4 bytes, so optimize that case. */
565 if (words_to_transfer) {
566 if (words_to_transfer > tx_fifo_avail)
567 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800568
Doug Anderson96219c32011-08-30 11:46:10 -0600569 /*
570 * Update state before writing to FIFO. If this casues us
571 * to finish writing all bytes (AKA buf_remaining goes to 0) we
572 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
573 * not maskable). We need to make sure that the isr sees
574 * buf_remaining as 0 and doesn't call us back re-entrantly.
575 */
576 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
577 tx_fifo_avail -= words_to_transfer;
578 i2c_dev->msg_buf_remaining = buf_remaining;
579 i2c_dev->msg_buf = buf +
580 words_to_transfer * BYTES_PER_FIFO_WORD;
581 barrier();
582
583 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
584
585 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
586 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800587
588 /*
589 * If there is a partial word at the end of buf, handle it manually to
590 * prevent reading past the end of buf, which could cross a page
591 * boundary and fault.
592 */
593 if (tx_fifo_avail > 0 && buf_remaining > 0) {
594 BUG_ON(buf_remaining > 3);
595 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300596 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600597
598 /* Again update before writing to FIFO to make sure isr sees. */
599 i2c_dev->msg_buf_remaining = 0;
600 i2c_dev->msg_buf = NULL;
601 barrier();
602
Colin Crossdb811ca2011-02-20 17:14:21 -0800603 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800604 }
605
Colin Crossdb811ca2011-02-20 17:14:21 -0800606 return 0;
607}
608
609/*
610 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
611 * block. This block is identical to the rest of the I2C blocks, except that
612 * it only supports master mode, it has registers moved around, and it needs
613 * some extra init to get it into I2C mode. The register moves are handled
614 * by i2c_readl and i2c_writel
615 */
616static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
617{
Jon Hunterf5076682016-08-26 14:08:59 +0100618 u32 val;
619
Colin Crossdb811ca2011-02-20 17:14:21 -0800620 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
621 val |= DVC_CTRL_REG3_SW_PROG;
622 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
623 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
624
625 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
626 val |= DVC_CTRL_REG1_INTR_EN;
627 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
628}
629
Jon Hunter1f50ad22016-08-26 14:09:04 +0100630static int tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530631{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100632 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530633 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100634
Jon Hunter718917b2016-08-26 14:09:05 +0100635 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
636 if (ret)
637 return ret;
638
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530639 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300640 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530641 if (ret < 0) {
642 dev_err(i2c_dev->dev,
643 "Enabling fast clk failed, err %d\n", ret);
644 return ret;
645 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530646 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100647
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300648 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530649 if (ret < 0) {
650 dev_err(i2c_dev->dev,
651 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300652 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100653 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530654 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100655
656 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530657}
658
Jon Hunter1f50ad22016-08-26 14:09:04 +0100659static int tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530660{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100661 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
662
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300663 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530664 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300665 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100666
Jon Hunter718917b2016-08-26 14:09:05 +0100667 return pinctrl_pm_select_idle_state(i2c_dev->dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530668}
669
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530670static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
671{
672 unsigned long reg_offset;
673 void __iomem *addr;
674 u32 val;
675 int err;
676
677 if (i2c_dev->hw->has_config_load_reg) {
678 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
679 addr = i2c_dev->base + reg_offset;
680 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
Shardar Shariff Md2bc445e2016-08-31 18:58:43 +0530681 if (in_interrupt())
682 err = readl_poll_timeout_atomic(addr, val, val == 0,
683 1000, I2C_CONFIG_LOAD_TIMEOUT);
684 else
685 err = readl_poll_timeout(addr, val, val == 0,
686 1000, I2C_CONFIG_LOAD_TIMEOUT);
687
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530688 if (err) {
689 dev_warn(i2c_dev->dev,
690 "timeout waiting for config load\n");
691 return err;
692 }
693 }
694
695 return 0;
696}
697
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800698static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit)
Colin Crossdb811ca2011-02-20 17:14:21 -0800699{
700 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100701 int err;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800702 u32 clk_divisor, clk_multiplier;
703 u32 tsu_thd = 0;
704 u8 tlow, thigh;
Colin Crossdb811ca2011-02-20 17:14:21 -0800705
Jon Hunter1f50ad22016-08-26 14:09:04 +0100706 err = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000707 if (err < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100708 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000709 return err;
710 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800711
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700712 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800713 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700714 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800715
716 if (i2c_dev->is_dvc)
717 tegra_dvc_init(i2c_dev);
718
Jay Cheng40abcf72011-04-25 15:32:27 -0600719 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
720 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530721
722 if (i2c_dev->hw->has_multi_master_mode)
723 val |= I2C_CNFG_MULTI_MASTER_MODE;
724
Colin Crossdb811ca2011-02-20 17:14:21 -0800725 i2c_writel(i2c_dev, val, I2C_CNFG);
726 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530727
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530728 /* Make sure clock divisor programmed correctly */
729 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530730 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530731 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
732 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800733
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800734 if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
735 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE) {
736 tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
737 thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
738 tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
739 } else {
740 tlow = i2c_dev->hw->tlow_std_mode;
741 thigh = i2c_dev->hw->thigh_std_mode;
742 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
743 }
744
745 if (i2c_dev->hw->has_interface_timing_reg) {
746 val = (thigh << I2C_THIGH_SHIFT) | tlow;
747 i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
748 }
749
750 /*
751 * configure setup and hold times only when tsu_thd is non-zero.
752 * otherwise, preserve the chip default values
753 */
754 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
755 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
756
757 if (!clk_reinit) {
758 clk_multiplier = (tlow + thigh + 2);
759 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
760 err = clk_set_rate(i2c_dev->div_clk,
761 i2c_dev->bus_clk_rate * clk_multiplier);
762 if (err) {
763 dev_err(i2c_dev->dev,
764 "failed changing clock rate: %d\n", err);
765 goto err;
766 }
767 }
768
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600769 if (!i2c_dev->is_dvc) {
770 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100771
Stephen Warren5afa9d32011-06-06 11:25:19 -0600772 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
773 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
774 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
775 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600776 }
777
Jon Hunter1f50ad22016-08-26 14:09:04 +0100778 err = tegra_i2c_flush_fifos(i2c_dev);
Shardar Shariff Md2148c012016-08-31 18:58:41 +0530779 if (err)
780 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800781
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530782 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
783 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
784
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530785 err = tegra_i2c_wait_for_config_load(i2c_dev);
786 if (err)
787 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530788
Todd Poynorcb63c622011-04-25 15:32:25 -0600789 if (i2c_dev->irq_disabled) {
Jon Hunterfbf80902016-09-06 10:50:45 +0100790 i2c_dev->irq_disabled = false;
Todd Poynorcb63c622011-04-25 15:32:25 -0600791 enable_irq(i2c_dev->irq);
792 }
793
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530794err:
Jon Hunter1f50ad22016-08-26 14:09:04 +0100795 pm_runtime_put(i2c_dev->dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800796 return err;
797}
798
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530799static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
800{
801 u32 cnfg;
802
Jon Hunter54836e22018-07-03 09:55:43 +0100803 /*
804 * NACK interrupt is generated before the I2C controller generates
805 * the STOP condition on the bus. So wait for 2 clock periods
806 * before disabling the controller so that the STOP condition has
807 * been delivered properly.
808 */
809 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
810
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530811 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
812 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
813 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
814
815 return tegra_i2c_wait_for_config_load(i2c_dev);
816}
817
Colin Crossdb811ca2011-02-20 17:14:21 -0800818static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
819{
820 u32 status;
821 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
822 struct tegra_i2c_dev *i2c_dev = dev_id;
823
824 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
825
jun qian3782cc32018-09-11 07:54:46 -0700826 spin_lock(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -0800827 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600828 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
829 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
830 i2c_readl(i2c_dev, I2C_STATUS),
831 i2c_readl(i2c_dev, I2C_CNFG));
832 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
833
834 if (!i2c_dev->irq_disabled) {
835 disable_irq_nosync(i2c_dev->irq);
Jon Hunterfbf80902016-09-06 10:50:45 +0100836 i2c_dev->irq_disabled = true;
Todd Poynorcb63c622011-04-25 15:32:25 -0600837 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600838 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800839 }
840
841 if (unlikely(status & status_err)) {
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530842 tegra_i2c_disable_packet_mode(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800843 if (status & I2C_INT_NO_ACK)
844 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
845 if (status & I2C_INT_ARBITRATION_LOST)
846 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800847 goto err;
848 }
849
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800850 /*
851 * I2C transfer is terminated during the bus clear so skip
852 * processing the other interrupts.
853 */
854 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
855 goto err;
856
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800857 if (!i2c_dev->is_curr_dma_xfer) {
858 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
859 if (i2c_dev->msg_buf_remaining)
860 tegra_i2c_empty_rx_fifo(i2c_dev);
861 else
862 BUG();
863 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800864
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800865 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
866 if (i2c_dev->msg_buf_remaining)
867 tegra_i2c_fill_tx_fifo(i2c_dev);
868 else
869 tegra_i2c_mask_irq(i2c_dev,
870 I2C_INT_TX_FIFO_DATA_REQ);
871 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800872 }
873
Laxman Dewanganc889e912012-05-07 12:16:19 +0530874 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
875 if (i2c_dev->is_dvc)
876 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
877
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800878 /*
879 * During message read XFER_COMPLETE interrupt is triggered prior to
880 * DMA completion and during message write XFER_COMPLETE interrupt is
881 * triggered after DMA completion.
882 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
883 * so forcing msg_buf_remaining to 0 in DMA mode.
884 */
Doug Anderson96219c32011-08-30 11:46:10 -0600885 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800886 if (i2c_dev->is_curr_dma_xfer)
887 i2c_dev->msg_buf_remaining = 0;
Doug Anderson96219c32011-08-30 11:46:10 -0600888 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800889 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600890 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530891 goto done;
Colin Crossdb811ca2011-02-20 17:14:21 -0800892err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300893 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800894 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
895 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
896 I2C_INT_RX_FIFO_DATA_REQ);
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800897 if (i2c_dev->hw->supports_bus_clear)
898 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
Colin Crossdb811ca2011-02-20 17:14:21 -0800899 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600900 if (i2c_dev->is_dvc)
901 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530902
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800903 if (i2c_dev->is_curr_dma_xfer) {
904 if (i2c_dev->msg_read)
905 dmaengine_terminate_async(i2c_dev->rx_dma_chan);
906 else
907 dmaengine_terminate_async(i2c_dev->tx_dma_chan);
908
909 complete(&i2c_dev->dma_complete);
910 }
911
Laxman Dewanganc889e912012-05-07 12:16:19 +0530912 complete(&i2c_dev->msg_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530913done:
jun qian3782cc32018-09-11 07:54:46 -0700914 spin_unlock(&i2c_dev->xfer_lock);
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
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800988static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
989{
990 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
991 int err;
992 unsigned long time_left;
993 u32 reg;
994
995 reinit_completion(&i2c_dev->msg_complete);
996 reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) |
997 I2C_BC_STOP_COND | I2C_BC_TERMINATE;
998 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
999 if (i2c_dev->hw->has_config_load_reg) {
1000 err = tegra_i2c_wait_for_config_load(i2c_dev);
1001 if (err)
1002 return err;
1003 }
1004
1005 reg |= I2C_BC_ENABLE;
1006 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1007 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1008
1009 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001010 msecs_to_jiffies(50));
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001011 if (time_left == 0) {
1012 dev_err(i2c_dev->dev, "timed out for bus clear\n");
1013 return -ETIMEDOUT;
1014 }
1015
1016 reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1017 if (!(reg & I2C_BC_STATUS)) {
1018 dev_err(i2c_dev->dev,
1019 "un-recovered arbitration lost\n");
1020 return -EIO;
1021 }
1022
1023 return -EAGAIN;
1024}
1025
Colin Crossdb811ca2011-02-20 17:14:21 -08001026static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301027 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -08001028{
1029 u32 packet_header;
1030 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001031 unsigned long time_left;
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301032 unsigned long flags;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001033 size_t xfer_size;
1034 u32 *buffer = NULL;
1035 int err = 0;
1036 bool dma;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001037 u16 xfer_time = 100;
Colin Crossdb811ca2011-02-20 17:14:21 -08001038
1039 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001040
Colin Crossdb811ca2011-02-20 17:14:21 -08001041 i2c_dev->msg_buf = msg->buf;
1042 i2c_dev->msg_buf_remaining = msg->len;
1043 i2c_dev->msg_err = I2C_ERR_NONE;
1044 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -08001045 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -08001046
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001047 if (i2c_dev->msg_read)
1048 xfer_size = msg->len;
1049 else
1050 xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1051
1052 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1053 i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_MAX_LEN) &&
1054 i2c_dev->dma_buf;
1055 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1056 dma = i2c_dev->is_curr_dma_xfer;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001057 /*
1058 * Transfer time in mSec = Total bits / transfer rate
1059 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1060 */
1061 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1062 i2c_dev->bus_clk_rate);
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301063 spin_lock_irqsave(&i2c_dev->xfer_lock, flags);
1064
1065 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1066 tegra_i2c_unmask_irq(i2c_dev, int_mask);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001067 if (dma) {
1068 if (i2c_dev->msg_read) {
1069 dma_sync_single_for_device(i2c_dev->dev,
1070 i2c_dev->dma_phys,
1071 xfer_size,
1072 DMA_FROM_DEVICE);
1073 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1074 if (err < 0) {
1075 dev_err(i2c_dev->dev,
1076 "starting RX DMA failed, err %d\n",
1077 err);
1078 goto unlock;
1079 }
1080
1081 } else {
1082 dma_sync_single_for_cpu(i2c_dev->dev,
1083 i2c_dev->dma_phys,
1084 xfer_size,
1085 DMA_TO_DEVICE);
1086 buffer = i2c_dev->dma_buf;
1087 }
1088 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301089
Colin Crossdb811ca2011-02-20 17:14:21 -08001090 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
1091 PACKET_HEADER0_PROTOCOL_I2C |
1092 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
1093 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001094 if (dma && !i2c_dev->msg_read)
1095 *buffer++ = packet_header;
1096 else
1097 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001098
1099 packet_header = msg->len - 1;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001100 if (dma && !i2c_dev->msg_read)
1101 *buffer++ = packet_header;
1102 else
1103 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001104
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301105 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301106 if (end_state == MSG_END_CONTINUE)
1107 packet_header |= I2C_HEADER_CONTINUE_XFER;
1108 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -06001109 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301110 if (msg->flags & I2C_M_TEN) {
1111 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -08001112 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301113 } else {
1114 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1115 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001116 if (msg->flags & I2C_M_IGNORE_NAK)
1117 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -08001118 if (msg->flags & I2C_M_RD)
1119 packet_header |= I2C_HEADER_READ;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001120 if (dma && !i2c_dev->msg_read)
1121 *buffer++ = packet_header;
1122 else
1123 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001124
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001125 if (!i2c_dev->msg_read) {
1126 if (dma) {
1127 memcpy(buffer, msg->buf, msg->len);
1128 dma_sync_single_for_device(i2c_dev->dev,
1129 i2c_dev->dma_phys,
1130 xfer_size,
1131 DMA_TO_DEVICE);
1132 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1133 if (err < 0) {
1134 dev_err(i2c_dev->dev,
1135 "starting TX DMA failed, err %d\n",
1136 err);
1137 goto unlock;
1138 }
1139 } else {
1140 tegra_i2c_fill_tx_fifo(i2c_dev);
1141 }
1142 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001143
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301144 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1145 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001146 if (!dma) {
1147 if (msg->flags & I2C_M_RD)
1148 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1149 else if (i2c_dev->msg_buf_remaining)
1150 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1151 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301152
Colin Crossdb811ca2011-02-20 17:14:21 -08001153 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1154 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
1155 i2c_readl(i2c_dev, I2C_INT_MASK));
1156
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001157unlock:
1158 spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
1159
1160 if (dma) {
1161 if (err)
1162 return err;
1163
1164 time_left = wait_for_completion_timeout(
1165 &i2c_dev->dma_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001166 msecs_to_jiffies(xfer_time));
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001167 if (time_left == 0) {
1168 dev_err(i2c_dev->dev, "DMA transfer timeout\n");
1169 dmaengine_terminate_sync(i2c_dev->msg_read ?
1170 i2c_dev->rx_dma_chan :
1171 i2c_dev->tx_dma_chan);
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001172 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001173 return -ETIMEDOUT;
1174 }
1175
1176 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1177 dma_sync_single_for_cpu(i2c_dev->dev,
1178 i2c_dev->dma_phys,
1179 xfer_size,
1180 DMA_FROM_DEVICE);
1181 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf,
1182 msg->len);
1183 }
1184
1185 if (i2c_dev->msg_err != I2C_ERR_NONE)
1186 dmaengine_synchronize(i2c_dev->msg_read ?
1187 i2c_dev->rx_dma_chan :
1188 i2c_dev->tx_dma_chan);
1189 }
1190
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001191 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001192 msecs_to_jiffies(xfer_time));
Colin Crossdb811ca2011-02-20 17:14:21 -08001193 tegra_i2c_mask_irq(i2c_dev, int_mask);
1194
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001195 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -08001196 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
1197
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001198 tegra_i2c_init(i2c_dev, true);
Colin Crossdb811ca2011-02-20 17:14:21 -08001199 return -ETIMEDOUT;
1200 }
1201
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001202 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1203 time_left, completion_done(&i2c_dev->msg_complete),
1204 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -08001205
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001206 i2c_dev->is_curr_dma_xfer = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001207 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
1208 return 0;
1209
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001210 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001211 /* start recovery upon arbitration loss in single master mode */
1212 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1213 if (!i2c_dev->is_multimaster_mode)
1214 return i2c_recover_bus(&i2c_dev->adapter);
1215 return -EAGAIN;
1216 }
1217
Colin Crossdb811ca2011-02-20 17:14:21 -08001218 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1219 if (msg->flags & I2C_M_IGNORE_NAK)
1220 return 0;
1221 return -EREMOTEIO;
1222 }
1223
1224 return -EIO;
1225}
1226
1227static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1228 int num)
1229{
1230 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1231 int i;
1232 int ret = 0;
1233
Jon Hunter1f50ad22016-08-26 14:09:04 +01001234 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001235 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +01001236 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001237 return ret;
1238 }
1239
Colin Crossdb811ca2011-02-20 17:14:21 -08001240 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301241 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +01001242
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301243 if (i < (num - 1)) {
1244 if (msgs[i + 1].flags & I2C_M_NOSTART)
1245 end_type = MSG_END_CONTINUE;
1246 else
1247 end_type = MSG_END_REPEAT_START;
1248 }
1249 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -08001250 if (ret)
1251 break;
1252 }
Jon Hunter1f50ad22016-08-26 14:09:04 +01001253
1254 pm_runtime_put(i2c_dev->dev);
1255
Colin Crossdb811ca2011-02-20 17:14:21 -08001256 return ret ?: i;
1257}
1258
1259static u32 tegra_i2c_func(struct i2c_adapter *adap)
1260{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301261 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +02001262 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1263 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301264
1265 if (i2c_dev->hw->has_continue_xfer_support)
1266 ret |= I2C_FUNC_NOSTART;
1267 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001268}
1269
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301270static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1271{
1272 struct device_node *np = i2c_dev->dev->of_node;
1273 int ret;
1274
1275 ret = of_property_read_u32(np, "clock-frequency",
1276 &i2c_dev->bus_clk_rate);
1277 if (ret)
1278 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
1279
1280 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
1281 "multi-master");
1282}
1283
Colin Crossdb811ca2011-02-20 17:14:21 -08001284static const struct i2c_algorithm tegra_i2c_algo = {
1285 .master_xfer = tegra_i2c_xfer,
1286 .functionality = tegra_i2c_func,
1287};
1288
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001289/* payload size is only 12 bit */
Bhumika Goyalae3923a2017-08-21 17:42:04 +05301290static const struct i2c_adapter_quirks tegra_i2c_quirks = {
Wolfram Sangc96c0f22018-07-23 22:26:12 +02001291 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001292 .max_read_len = SZ_4K,
1293 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001294};
1295
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001296static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1297 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001298 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001299};
1300
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001301static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1302 .recover_bus = tegra_i2c_issue_bus_clear,
1303};
1304
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301305static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1306 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301307 .has_per_pkt_xfer_complete_irq = false,
1308 .has_single_clk_source = false,
1309 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001310 .clk_divisor_std_mode = 0,
1311 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301312 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301313 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301314 .has_multi_master_mode = false,
1315 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001316 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001317 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001318 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001319 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001320 .tlow_std_mode = 0x4,
1321 .thigh_std_mode = 0x2,
1322 .tlow_fast_fastplus_mode = 0x4,
1323 .thigh_fast_fastplus_mode = 0x2,
1324 .setup_hold_time_std_mode = 0x0,
1325 .setup_hold_time_fast_fast_plus_mode = 0x0,
1326 .setup_hold_time_hs_mode = 0x0,
1327 .has_interface_timing_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301328};
1329
1330static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1331 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301332 .has_per_pkt_xfer_complete_irq = false,
1333 .has_single_clk_source = false,
1334 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001335 .clk_divisor_std_mode = 0,
1336 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301337 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301338 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301339 .has_multi_master_mode = false,
1340 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001341 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001342 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001343 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001344 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001345 .tlow_std_mode = 0x4,
1346 .thigh_std_mode = 0x2,
1347 .tlow_fast_fastplus_mode = 0x4,
1348 .thigh_fast_fastplus_mode = 0x2,
1349 .setup_hold_time_std_mode = 0x0,
1350 .setup_hold_time_fast_fast_plus_mode = 0x0,
1351 .setup_hold_time_hs_mode = 0x0,
1352 .has_interface_timing_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301353};
1354
1355static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1356 .has_continue_xfer_support = true,
1357 .has_per_pkt_xfer_complete_irq = true,
1358 .has_single_clk_source = true,
1359 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001360 .clk_divisor_std_mode = 0x19,
1361 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301362 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301363 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301364 .has_multi_master_mode = false,
1365 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001366 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001367 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001368 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001369 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001370 .tlow_std_mode = 0x4,
1371 .thigh_std_mode = 0x2,
1372 .tlow_fast_fastplus_mode = 0x4,
1373 .thigh_fast_fastplus_mode = 0x2,
1374 .setup_hold_time_std_mode = 0x0,
1375 .setup_hold_time_fast_fast_plus_mode = 0x0,
1376 .setup_hold_time_hs_mode = 0x0,
1377 .has_interface_timing_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301378};
1379
1380static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1381 .has_continue_xfer_support = true,
1382 .has_per_pkt_xfer_complete_irq = true,
1383 .has_single_clk_source = true,
1384 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001385 .clk_divisor_std_mode = 0x19,
1386 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301387 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301388 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301389 .has_multi_master_mode = false,
1390 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001391 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001392 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001393 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001394 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001395 .tlow_std_mode = 0x4,
1396 .thigh_std_mode = 0x2,
1397 .tlow_fast_fastplus_mode = 0x4,
1398 .thigh_fast_fastplus_mode = 0x2,
1399 .setup_hold_time_std_mode = 0x0,
1400 .setup_hold_time_fast_fast_plus_mode = 0x0,
1401 .setup_hold_time_hs_mode = 0x0,
1402 .has_interface_timing_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301403};
1404
1405static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1406 .has_continue_xfer_support = true,
1407 .has_per_pkt_xfer_complete_irq = true,
1408 .has_single_clk_source = true,
1409 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001410 .clk_divisor_std_mode = 0x19,
1411 .clk_divisor_fast_mode = 0x19,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301412 .clk_divisor_fast_plus_mode = 0x10,
1413 .has_config_load_reg = true,
Sowjanya Komatineni6b9932b2019-02-19 09:28:52 -08001414 .has_multi_master_mode = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301415 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001416 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001417 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001418 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001419 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001420 .tlow_std_mode = 0x4,
1421 .thigh_std_mode = 0x2,
1422 .tlow_fast_fastplus_mode = 0x4,
1423 .thigh_fast_fastplus_mode = 0x2,
1424 .setup_hold_time_std_mode = 0,
1425 .setup_hold_time_fast_fast_plus_mode = 0,
1426 .setup_hold_time_hs_mode = 0,
1427 .has_interface_timing_reg = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001428};
1429
1430static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1431 .has_continue_xfer_support = true,
1432 .has_per_pkt_xfer_complete_irq = true,
1433 .has_single_clk_source = true,
1434 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001435 .clk_divisor_std_mode = 0x16,
1436 .clk_divisor_fast_mode = 0x19,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001437 .clk_divisor_fast_plus_mode = 0x10,
1438 .has_config_load_reg = true,
Sowjanya Komatineni6b9932b2019-02-19 09:28:52 -08001439 .has_multi_master_mode = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001440 .has_slcg_override_reg = true,
Sowjanya Komatineni9ffc1252019-02-19 09:28:51 -08001441 .has_mst_fifo = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001442 .quirks = &tegra_i2c_quirks,
1443 .supports_bus_clear = true,
1444 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001445 .tlow_std_mode = 0x4,
1446 .thigh_std_mode = 0x3,
1447 .tlow_fast_fastplus_mode = 0x4,
1448 .thigh_fast_fastplus_mode = 0x2,
1449 .setup_hold_time_std_mode = 0,
1450 .setup_hold_time_fast_fast_plus_mode = 0,
1451 .setup_hold_time_hs_mode = 0,
1452 .has_interface_timing_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001453};
1454
1455static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1456 .has_continue_xfer_support = true,
1457 .has_per_pkt_xfer_complete_irq = true,
1458 .has_single_clk_source = true,
1459 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001460 .clk_divisor_std_mode = 0x4f,
1461 .clk_divisor_fast_mode = 0x3c,
1462 .clk_divisor_fast_plus_mode = 0x16,
Thierry Redingc5907c62018-06-19 12:49:42 +02001463 .has_config_load_reg = true,
1464 .has_multi_master_mode = true,
1465 .has_slcg_override_reg = true,
1466 .has_mst_fifo = true,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001467 .quirks = &tegra194_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001468 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001469 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001470 .tlow_std_mode = 0x8,
1471 .thigh_std_mode = 0x7,
1472 .tlow_fast_fastplus_mode = 0x2,
1473 .thigh_fast_fastplus_mode = 0x2,
1474 .setup_hold_time_std_mode = 0x08080808,
1475 .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1476 .setup_hold_time_hs_mode = 0x090909,
1477 .has_interface_timing_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301478};
1479
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301480/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001481static const struct of_device_id tegra_i2c_of_match[] = {
Thierry Redingc5907c62018-06-19 12:49:42 +02001482 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001483 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301484 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301485 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301486 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301487 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1488 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1489 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1490 {},
1491};
1492MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301493
Bill Pemberton0b255e92012-11-27 15:59:38 -05001494static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001495{
1496 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -08001497 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301498 struct clk *div_clk;
1499 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -07001500 void __iomem *base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001501 phys_addr_t base_phys;
Colin Crossdb811ca2011-02-20 17:14:21 -08001502 int irq;
1503 int ret = 0;
1504
1505 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001506 base_phys = res->start;
Thierry Reding84dbf802013-01-21 11:09:03 +01001507 base = devm_ioremap_resource(&pdev->dev, res);
1508 if (IS_ERR(base))
1509 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -08001510
1511 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1512 if (!res) {
1513 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301514 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -08001515 }
1516 irq = res->start;
1517
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301518 div_clk = devm_clk_get(&pdev->dev, "div-clk");
1519 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001520 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301521 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -08001522 }
1523
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301524 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001525 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301526 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -08001527
1528 i2c_dev->base = base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001529 i2c_dev->base_phys = base_phys;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301530 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -08001531 i2c_dev->adapter.algo = &tegra_i2c_algo;
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001532 i2c_dev->adapter.retries = 1;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001533 i2c_dev->adapter.timeout = 6 * HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -08001534 i2c_dev->irq = irq;
1535 i2c_dev->cont_id = pdev->id;
1536 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -07001537
Philipp Zabel94d3b652017-07-19 17:25:34 +02001538 i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001539 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001540 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001541 return PTR_ERR(i2c_dev->rst);
1542 }
1543
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301544 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001545
Jon Huntera9e32cd2016-08-26 14:09:01 +01001546 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
1547 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
1548 "nvidia,tegra20-i2c-dvc");
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001549 i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001550 i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len +
1551 I2C_PACKET_HEADER_SIZE;
Colin Crossdb811ca2011-02-20 17:14:21 -08001552 init_completion(&i2c_dev->msg_complete);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001553 init_completion(&i2c_dev->dma_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301554 spin_lock_init(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -08001555
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301556 if (!i2c_dev->hw->has_single_clk_source) {
1557 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
1558 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001559 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301560 return PTR_ERR(fast_clk);
1561 }
1562 i2c_dev->fast_clk = fast_clk;
1563 }
1564
Colin Crossdb811ca2011-02-20 17:14:21 -08001565 platform_set_drvdata(pdev, i2c_dev);
1566
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001567 if (!i2c_dev->hw->has_single_clk_source) {
1568 ret = clk_prepare(i2c_dev->fast_clk);
1569 if (ret < 0) {
1570 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1571 return ret;
1572 }
1573 }
1574
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001575 if (i2c_dev->bus_clk_rate > I2C_FAST_MODE &&
1576 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE)
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301577 i2c_dev->clk_divisor_non_hs_mode =
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001578 i2c_dev->hw->clk_divisor_fast_plus_mode;
1579 else if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
1580 i2c_dev->bus_clk_rate <= I2C_FAST_MODE)
1581 i2c_dev->clk_divisor_non_hs_mode =
1582 i2c_dev->hw->clk_divisor_fast_mode;
1583 else
1584 i2c_dev->clk_divisor_non_hs_mode =
1585 i2c_dev->hw->clk_divisor_std_mode;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001586
1587 ret = clk_prepare(i2c_dev->div_clk);
1588 if (ret < 0) {
1589 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1590 goto unprepare_fast_clk;
1591 }
1592
Jon Hunter1f50ad22016-08-26 14:09:04 +01001593 pm_runtime_enable(&pdev->dev);
1594 if (!pm_runtime_enabled(&pdev->dev)) {
1595 ret = tegra_i2c_runtime_resume(&pdev->dev);
1596 if (ret < 0) {
1597 dev_err(&pdev->dev, "runtime resume failed\n");
1598 goto unprepare_div_clk;
1599 }
1600 }
1601
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301602 if (i2c_dev->is_multimaster_mode) {
1603 ret = clk_enable(i2c_dev->div_clk);
1604 if (ret < 0) {
1605 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
1606 ret);
Jon Hunter1f50ad22016-08-26 14:09:04 +01001607 goto disable_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301608 }
1609 }
1610
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001611 if (i2c_dev->hw->supports_bus_clear)
1612 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1613
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001614 ret = tegra_i2c_init_dma(i2c_dev);
1615 if (ret < 0)
1616 goto disable_div_clk;
1617
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001618 ret = tegra_i2c_init(i2c_dev, false);
Colin Crossdb811ca2011-02-20 17:14:21 -08001619 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001620 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001621 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001622 }
1623
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301624 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +05301625 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001626 if (ret) {
1627 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001628 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001629 }
1630
Colin Crossdb811ca2011-02-20 17:14:21 -08001631 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1632 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +02001633 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +01001634 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -08001635 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -08001636 i2c_dev->adapter.dev.parent = &pdev->dev;
1637 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -07001638 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -08001639
1640 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +02001641 if (ret)
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001642 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001643
Colin Crossdb811ca2011-02-20 17:14:21 -08001644 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001645
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001646release_dma:
1647 tegra_i2c_release_dma(i2c_dev);
1648
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301649disable_div_clk:
1650 if (i2c_dev->is_multimaster_mode)
1651 clk_disable(i2c_dev->div_clk);
1652
Jon Hunter1f50ad22016-08-26 14:09:04 +01001653disable_rpm:
1654 pm_runtime_disable(&pdev->dev);
1655 if (!pm_runtime_status_suspended(&pdev->dev))
1656 tegra_i2c_runtime_suspend(&pdev->dev);
1657
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001658unprepare_div_clk:
1659 clk_unprepare(i2c_dev->div_clk);
1660
1661unprepare_fast_clk:
1662 if (!i2c_dev->hw->has_single_clk_source)
1663 clk_unprepare(i2c_dev->fast_clk);
1664
1665 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001666}
1667
Bill Pemberton0b255e92012-11-27 15:59:38 -05001668static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001669{
1670 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +01001671
Colin Crossdb811ca2011-02-20 17:14:21 -08001672 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001673
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301674 if (i2c_dev->is_multimaster_mode)
1675 clk_disable(i2c_dev->div_clk);
1676
Jon Hunter1f50ad22016-08-26 14:09:04 +01001677 pm_runtime_disable(&pdev->dev);
1678 if (!pm_runtime_status_suspended(&pdev->dev))
1679 tegra_i2c_runtime_suspend(&pdev->dev);
1680
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001681 clk_unprepare(i2c_dev->div_clk);
1682 if (!i2c_dev->hw->has_single_clk_source)
1683 clk_unprepare(i2c_dev->fast_clk);
1684
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001685 tegra_i2c_release_dma(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001686 return 0;
1687}
1688
Laxman Dewangan371e67c2012-08-18 17:49:58 +05301689#ifdef CONFIG_PM_SLEEP
Jon Hunter1f50ad22016-08-26 14:09:04 +01001690static const struct dev_pm_ops tegra_i2c_pm = {
1691 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1692 NULL)
Jon Hunter1f50ad22016-08-26 14:09:04 +01001693};
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001694#define TEGRA_I2C_PM (&tegra_i2c_pm)
1695#else
1696#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001697#endif
1698
1699static struct platform_driver tegra_i2c_driver = {
1700 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001701 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001702 .driver = {
1703 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001704 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001705 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001706 },
1707};
1708
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001709module_platform_driver(tegra_i2c_driver);
Colin Crossdb811ca2011-02-20 17:14:21 -08001710
1711MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1712MODULE_AUTHOR("Colin Cross");
1713MODULE_LICENSE("GPL v2");