blob: a4cd79c9f7a7e49c86633ba30c0c508cbb07af03 [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
417 if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA) ||
418 !i2c_dev->hw->has_apb_dma) {
419 err = -ENODEV;
420 goto err_out;
421 }
422
423 chan = dma_request_slave_channel_reason(i2c_dev->dev, "rx");
424 if (IS_ERR(chan)) {
425 err = PTR_ERR(chan);
426 goto err_out;
427 }
428
429 i2c_dev->rx_dma_chan = chan;
430
431 chan = dma_request_slave_channel_reason(i2c_dev->dev, "tx");
432 if (IS_ERR(chan)) {
433 err = PTR_ERR(chan);
434 goto err_out;
435 }
436
437 i2c_dev->tx_dma_chan = chan;
438
439 dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
440 &dma_phys, GFP_KERNEL | __GFP_NOWARN);
441 if (!dma_buf) {
442 dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n");
443 err = -ENOMEM;
444 goto err_out;
445 }
446
447 i2c_dev->dma_buf = dma_buf;
448 i2c_dev->dma_phys = dma_phys;
449 return 0;
450
451err_out:
452 tegra_i2c_release_dma(i2c_dev);
453 if (err != -EPROBE_DEFER) {
454 dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
455 dev_err(i2c_dev->dev, "fallbacking to PIO\n");
456 return 0;
457 }
458
459 return err;
460}
461
Colin Crossdb811ca2011-02-20 17:14:21 -0800462static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
463{
464 unsigned long timeout = jiffies + HZ;
Thierry Redingc5907c62018-06-19 12:49:42 +0200465 unsigned int offset;
466 u32 mask, val;
Jon Hunterf5076682016-08-26 14:08:59 +0100467
Thierry Redingc5907c62018-06-19 12:49:42 +0200468 if (i2c_dev->hw->has_mst_fifo) {
469 mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
470 I2C_MST_FIFO_CONTROL_RX_FLUSH;
471 offset = I2C_MST_FIFO_CONTROL;
472 } else {
473 mask = I2C_FIFO_CONTROL_TX_FLUSH |
474 I2C_FIFO_CONTROL_RX_FLUSH;
475 offset = I2C_FIFO_CONTROL;
476 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800477
Thierry Redingc5907c62018-06-19 12:49:42 +0200478 val = i2c_readl(i2c_dev, offset);
479 val |= mask;
480 i2c_writel(i2c_dev, val, offset);
481
482 while (i2c_readl(i2c_dev, offset) & mask) {
Colin Crossdb811ca2011-02-20 17:14:21 -0800483 if (time_after(jiffies, timeout)) {
484 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
485 return -ETIMEDOUT;
486 }
487 msleep(1);
488 }
489 return 0;
490}
491
492static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
493{
494 u32 val;
495 int rx_fifo_avail;
496 u8 *buf = i2c_dev->msg_buf;
497 size_t buf_remaining = i2c_dev->msg_buf_remaining;
498 int words_to_transfer;
499
Thierry Redingc5907c62018-06-19 12:49:42 +0200500 if (i2c_dev->hw->has_mst_fifo) {
501 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
502 rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
503 I2C_MST_FIFO_STATUS_RX_SHIFT;
504 } else {
505 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
506 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
507 I2C_FIFO_STATUS_RX_SHIFT;
508 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800509
510 /* Rounds down to not include partial word at the end of buf */
511 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
512 if (words_to_transfer > rx_fifo_avail)
513 words_to_transfer = rx_fifo_avail;
514
515 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
516
517 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
518 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
519 rx_fifo_avail -= words_to_transfer;
520
521 /*
522 * If there is a partial word at the end of buf, handle it manually to
523 * prevent overwriting past the end of buf
524 */
525 if (rx_fifo_avail > 0 && buf_remaining > 0) {
526 BUG_ON(buf_remaining > 3);
527 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
Dmitry 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
534 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
535 i2c_dev->msg_buf_remaining = buf_remaining;
536 i2c_dev->msg_buf = buf;
Thierry Redingc5907c62018-06-19 12:49:42 +0200537
Colin Crossdb811ca2011-02-20 17:14:21 -0800538 return 0;
539}
540
541static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
542{
543 u32 val;
544 int tx_fifo_avail;
545 u8 *buf = i2c_dev->msg_buf;
546 size_t buf_remaining = i2c_dev->msg_buf_remaining;
547 int words_to_transfer;
548
Thierry Redingc5907c62018-06-19 12:49:42 +0200549 if (i2c_dev->hw->has_mst_fifo) {
550 val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
551 tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
552 I2C_MST_FIFO_STATUS_TX_SHIFT;
553 } else {
554 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
555 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
556 I2C_FIFO_STATUS_TX_SHIFT;
557 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800558
559 /* Rounds down to not include partial word at the end of buf */
560 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
Colin Crossdb811ca2011-02-20 17:14:21 -0800561
Doug Anderson96219c32011-08-30 11:46:10 -0600562 /* It's very common to have < 4 bytes, so optimize that case. */
563 if (words_to_transfer) {
564 if (words_to_transfer > tx_fifo_avail)
565 words_to_transfer = tx_fifo_avail;
Colin Crossdb811ca2011-02-20 17:14:21 -0800566
Doug Anderson96219c32011-08-30 11:46:10 -0600567 /*
568 * Update state before writing to FIFO. If this casues us
569 * to finish writing all bytes (AKA buf_remaining goes to 0) we
570 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
571 * not maskable). We need to make sure that the isr sees
572 * buf_remaining as 0 and doesn't call us back re-entrantly.
573 */
574 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
575 tx_fifo_avail -= words_to_transfer;
576 i2c_dev->msg_buf_remaining = buf_remaining;
577 i2c_dev->msg_buf = buf +
578 words_to_transfer * BYTES_PER_FIFO_WORD;
579 barrier();
580
581 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
582
583 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
584 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800585
586 /*
587 * If there is a partial word at the end of buf, handle it manually to
588 * prevent reading past the end of buf, which could cross a page
589 * boundary and fault.
590 */
591 if (tx_fifo_avail > 0 && buf_remaining > 0) {
592 BUG_ON(buf_remaining > 3);
593 memcpy(&val, buf, buf_remaining);
Dmitry Osipenko8c340f62015-01-26 19:55:02 +0300594 val = le32_to_cpu(val);
Doug Anderson96219c32011-08-30 11:46:10 -0600595
596 /* Again update before writing to FIFO to make sure isr sees. */
597 i2c_dev->msg_buf_remaining = 0;
598 i2c_dev->msg_buf = NULL;
599 barrier();
600
Colin Crossdb811ca2011-02-20 17:14:21 -0800601 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -0800602 }
603
Colin Crossdb811ca2011-02-20 17:14:21 -0800604 return 0;
605}
606
607/*
608 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
609 * block. This block is identical to the rest of the I2C blocks, except that
610 * it only supports master mode, it has registers moved around, and it needs
611 * some extra init to get it into I2C mode. The register moves are handled
612 * by i2c_readl and i2c_writel
613 */
614static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
615{
Jon Hunterf5076682016-08-26 14:08:59 +0100616 u32 val;
617
Colin Crossdb811ca2011-02-20 17:14:21 -0800618 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
619 val |= DVC_CTRL_REG3_SW_PROG;
620 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
621 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
622
623 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
624 val |= DVC_CTRL_REG1_INTR_EN;
625 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
626}
627
Jon Hunter1f50ad22016-08-26 14:09:04 +0100628static int tegra_i2c_runtime_resume(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530629{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100630 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530631 int ret;
Jon Hunterf5076682016-08-26 14:08:59 +0100632
Jon Hunter718917b2016-08-26 14:09:05 +0100633 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
634 if (ret)
635 return ret;
636
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530637 if (!i2c_dev->hw->has_single_clk_source) {
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300638 ret = clk_enable(i2c_dev->fast_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530639 if (ret < 0) {
640 dev_err(i2c_dev->dev,
641 "Enabling fast clk failed, err %d\n", ret);
642 return ret;
643 }
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530644 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100645
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300646 ret = clk_enable(i2c_dev->div_clk);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530647 if (ret < 0) {
648 dev_err(i2c_dev->dev,
649 "Enabling div clk failed, err %d\n", ret);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300650 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100651 return ret;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530652 }
Jon Hunter1f50ad22016-08-26 14:09:04 +0100653
654 return 0;
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530655}
656
Jon Hunter1f50ad22016-08-26 14:09:04 +0100657static int tegra_i2c_runtime_suspend(struct device *dev)
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530658{
Jon Hunter1f50ad22016-08-26 14:09:04 +0100659 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
660
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300661 clk_disable(i2c_dev->div_clk);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530662 if (!i2c_dev->hw->has_single_clk_source)
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +0300663 clk_disable(i2c_dev->fast_clk);
Jon Hunter1f50ad22016-08-26 14:09:04 +0100664
Jon Hunter718917b2016-08-26 14:09:05 +0100665 return pinctrl_pm_select_idle_state(i2c_dev->dev);
Laxman Dewanganfd301cc2012-08-19 00:47:47 +0530666}
667
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530668static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
669{
670 unsigned long reg_offset;
671 void __iomem *addr;
672 u32 val;
673 int err;
674
675 if (i2c_dev->hw->has_config_load_reg) {
676 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
677 addr = i2c_dev->base + reg_offset;
678 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
Shardar Shariff Md2bc445e2016-08-31 18:58:43 +0530679 if (in_interrupt())
680 err = readl_poll_timeout_atomic(addr, val, val == 0,
681 1000, I2C_CONFIG_LOAD_TIMEOUT);
682 else
683 err = readl_poll_timeout(addr, val, val == 0,
684 1000, I2C_CONFIG_LOAD_TIMEOUT);
685
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530686 if (err) {
687 dev_warn(i2c_dev->dev,
688 "timeout waiting for config load\n");
689 return err;
690 }
691 }
692
693 return 0;
694}
695
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800696static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit)
Colin Crossdb811ca2011-02-20 17:14:21 -0800697{
698 u32 val;
Jon Hunter1f50ad22016-08-26 14:09:04 +0100699 int err;
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800700 u32 clk_divisor, clk_multiplier;
701 u32 tsu_thd = 0;
702 u8 tlow, thigh;
Colin Crossdb811ca2011-02-20 17:14:21 -0800703
Jon Hunter1f50ad22016-08-26 14:09:04 +0100704 err = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000705 if (err < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +0100706 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
Laxman Dewangan132c8032013-03-15 05:34:08 +0000707 return err;
708 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800709
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700710 reset_control_assert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800711 udelay(2);
Stephen Warrendda9d6a2013-11-06 16:42:05 -0700712 reset_control_deassert(i2c_dev->rst);
Colin Crossdb811ca2011-02-20 17:14:21 -0800713
714 if (i2c_dev->is_dvc)
715 tegra_dvc_init(i2c_dev);
716
Jay Cheng40abcf72011-04-25 15:32:27 -0600717 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
718 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530719
720 if (i2c_dev->hw->has_multi_master_mode)
721 val |= I2C_CNFG_MULTI_MASTER_MODE;
722
Colin Crossdb811ca2011-02-20 17:14:21 -0800723 i2c_writel(i2c_dev, val, I2C_CNFG);
724 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530725
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530726 /* Make sure clock divisor programmed correctly */
727 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
Laxman Dewangand57f5de2015-06-30 16:24:27 +0530728 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
Laxman Dewangan2a2897b2013-01-05 17:34:46 +0530729 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
730 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
Colin Crossdb811ca2011-02-20 17:14:21 -0800731
Sowjanya Komatineni0940d242019-02-12 11:06:48 -0800732 if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
733 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE) {
734 tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
735 thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
736 tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
737 } else {
738 tlow = i2c_dev->hw->tlow_std_mode;
739 thigh = i2c_dev->hw->thigh_std_mode;
740 tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
741 }
742
743 if (i2c_dev->hw->has_interface_timing_reg) {
744 val = (thigh << I2C_THIGH_SHIFT) | tlow;
745 i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
746 }
747
748 /*
749 * configure setup and hold times only when tsu_thd is non-zero.
750 * otherwise, preserve the chip default values
751 */
752 if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
753 i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);
754
755 if (!clk_reinit) {
756 clk_multiplier = (tlow + thigh + 2);
757 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
758 err = clk_set_rate(i2c_dev->div_clk,
759 i2c_dev->bus_clk_rate * clk_multiplier);
760 if (err) {
761 dev_err(i2c_dev->dev,
762 "failed changing clock rate: %d\n", err);
763 goto err;
764 }
765 }
766
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600767 if (!i2c_dev->is_dvc) {
768 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
Jon Hunterf5076682016-08-26 14:08:59 +0100769
Stephen Warren5afa9d32011-06-06 11:25:19 -0600770 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
771 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
772 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
773 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
Kenneth Waters65a1a0a2011-04-25 12:29:54 -0600774 }
775
Jon Hunter1f50ad22016-08-26 14:09:04 +0100776 err = tegra_i2c_flush_fifos(i2c_dev);
Shardar Shariff Md2148c012016-08-31 18:58:41 +0530777 if (err)
778 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800779
Shardar Shariff Md497fbe22016-03-14 18:52:18 +0530780 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
781 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
782
Shardar Shariff Md89120d62016-08-31 18:58:42 +0530783 err = tegra_i2c_wait_for_config_load(i2c_dev);
784 if (err)
785 goto err;
Laxman Dewangan6f4664b2015-06-30 16:24:26 +0530786
Todd Poynorcb63c622011-04-25 15:32:25 -0600787 if (i2c_dev->irq_disabled) {
Jon Hunterfbf80902016-09-06 10:50:45 +0100788 i2c_dev->irq_disabled = false;
Todd Poynorcb63c622011-04-25 15:32:25 -0600789 enable_irq(i2c_dev->irq);
790 }
791
Shardar Shariff Md21e9efd2016-04-25 19:08:36 +0530792err:
Jon Hunter1f50ad22016-08-26 14:09:04 +0100793 pm_runtime_put(i2c_dev->dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800794 return err;
795}
796
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530797static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
798{
799 u32 cnfg;
800
Jon Hunter54836e22018-07-03 09:55:43 +0100801 /*
802 * NACK interrupt is generated before the I2C controller generates
803 * the STOP condition on the bus. So wait for 2 clock periods
804 * before disabling the controller so that the STOP condition has
805 * been delivered properly.
806 */
807 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
808
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530809 cnfg = i2c_readl(i2c_dev, I2C_CNFG);
810 if (cnfg & I2C_CNFG_PACKET_MODE_EN)
811 i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);
812
813 return tegra_i2c_wait_for_config_load(i2c_dev);
814}
815
Colin Crossdb811ca2011-02-20 17:14:21 -0800816static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
817{
818 u32 status;
819 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
820 struct tegra_i2c_dev *i2c_dev = dev_id;
821
822 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
823
jun qian3782cc32018-09-11 07:54:46 -0700824 spin_lock(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -0800825 if (status == 0) {
Todd Poynorcb63c622011-04-25 15:32:25 -0600826 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
827 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
828 i2c_readl(i2c_dev, I2C_STATUS),
829 i2c_readl(i2c_dev, I2C_CNFG));
830 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
831
832 if (!i2c_dev->irq_disabled) {
833 disable_irq_nosync(i2c_dev->irq);
Jon Hunterfbf80902016-09-06 10:50:45 +0100834 i2c_dev->irq_disabled = true;
Todd Poynorcb63c622011-04-25 15:32:25 -0600835 }
Todd Poynorcb63c622011-04-25 15:32:25 -0600836 goto err;
Colin Crossdb811ca2011-02-20 17:14:21 -0800837 }
838
839 if (unlikely(status & status_err)) {
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530840 tegra_i2c_disable_packet_mode(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -0800841 if (status & I2C_INT_NO_ACK)
842 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
843 if (status & I2C_INT_ARBITRATION_LOST)
844 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
Colin Crossdb811ca2011-02-20 17:14:21 -0800845 goto err;
846 }
847
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800848 /*
849 * I2C transfer is terminated during the bus clear so skip
850 * processing the other interrupts.
851 */
852 if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
853 goto err;
854
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800855 if (!i2c_dev->is_curr_dma_xfer) {
856 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
857 if (i2c_dev->msg_buf_remaining)
858 tegra_i2c_empty_rx_fifo(i2c_dev);
859 else
860 BUG();
861 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800862
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800863 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
864 if (i2c_dev->msg_buf_remaining)
865 tegra_i2c_fill_tx_fifo(i2c_dev);
866 else
867 tegra_i2c_mask_irq(i2c_dev,
868 I2C_INT_TX_FIFO_DATA_REQ);
869 }
Colin Crossdb811ca2011-02-20 17:14:21 -0800870 }
871
Laxman Dewanganc889e912012-05-07 12:16:19 +0530872 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
873 if (i2c_dev->is_dvc)
874 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
875
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800876 /*
877 * During message read XFER_COMPLETE interrupt is triggered prior to
878 * DMA completion and during message write XFER_COMPLETE interrupt is
879 * triggered after DMA completion.
880 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
881 * so forcing msg_buf_remaining to 0 in DMA mode.
882 */
Doug Anderson96219c32011-08-30 11:46:10 -0600883 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800884 if (i2c_dev->is_curr_dma_xfer)
885 i2c_dev->msg_buf_remaining = 0;
Doug Anderson96219c32011-08-30 11:46:10 -0600886 BUG_ON(i2c_dev->msg_buf_remaining);
Colin Crossdb811ca2011-02-20 17:14:21 -0800887 complete(&i2c_dev->msg_complete);
Doug Anderson96219c32011-08-30 11:46:10 -0600888 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530889 goto done;
Colin Crossdb811ca2011-02-20 17:14:21 -0800890err:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300891 /* An error occurred, mask all interrupts */
Colin Crossdb811ca2011-02-20 17:14:21 -0800892 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
893 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
894 I2C_INT_RX_FIFO_DATA_REQ);
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800895 if (i2c_dev->hw->supports_bus_clear)
896 tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
Colin Crossdb811ca2011-02-20 17:14:21 -0800897 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
Todd Poynorcb63c622011-04-25 15:32:25 -0600898 if (i2c_dev->is_dvc)
899 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
Laxman Dewanganc889e912012-05-07 12:16:19 +0530900
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800901 if (i2c_dev->is_curr_dma_xfer) {
902 if (i2c_dev->msg_read)
903 dmaengine_terminate_async(i2c_dev->rx_dma_chan);
904 else
905 dmaengine_terminate_async(i2c_dev->tx_dma_chan);
906
907 complete(&i2c_dev->dma_complete);
908 }
909
Laxman Dewanganc889e912012-05-07 12:16:19 +0530910 complete(&i2c_dev->msg_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +0530911done:
jun qian3782cc32018-09-11 07:54:46 -0700912 spin_unlock(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -0800913 return IRQ_HANDLED;
914}
915
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -0800916static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
917 size_t len)
918{
919 u32 val, reg;
920 u8 dma_burst;
921 struct dma_slave_config slv_config = {0};
922 struct dma_chan *chan;
923 int ret;
924 unsigned long reg_offset;
925
926 if (i2c_dev->hw->has_mst_fifo)
927 reg = I2C_MST_FIFO_CONTROL;
928 else
929 reg = I2C_FIFO_CONTROL;
930
931 if (i2c_dev->is_curr_dma_xfer) {
932 if (len & 0xF)
933 dma_burst = 1;
934 else if (len & 0x10)
935 dma_burst = 4;
936 else
937 dma_burst = 8;
938
939 if (i2c_dev->msg_read) {
940 chan = i2c_dev->rx_dma_chan;
941 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
942 slv_config.src_addr = i2c_dev->base_phys + reg_offset;
943 slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
944 slv_config.src_maxburst = dma_burst;
945
946 if (i2c_dev->hw->has_mst_fifo)
947 val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
948 else
949 val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
950 } else {
951 chan = i2c_dev->tx_dma_chan;
952 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
953 slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
954 slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
955 slv_config.dst_maxburst = dma_burst;
956
957 if (i2c_dev->hw->has_mst_fifo)
958 val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
959 else
960 val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
961 }
962
963 slv_config.device_fc = true;
964 ret = dmaengine_slave_config(chan, &slv_config);
965 if (ret < 0) {
966 dev_err(i2c_dev->dev, "DMA slave config failed: %d\n",
967 ret);
968 dev_err(i2c_dev->dev, "fallbacking to PIO\n");
969 tegra_i2c_release_dma(i2c_dev);
970 i2c_dev->is_curr_dma_xfer = false;
971 } else {
972 goto out;
973 }
974 }
975
976 if (i2c_dev->hw->has_mst_fifo)
977 val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
978 I2C_MST_FIFO_CONTROL_RX_TRIG(1);
979 else
980 val = I2C_FIFO_CONTROL_TX_TRIG(8) |
981 I2C_FIFO_CONTROL_RX_TRIG(1);
982out:
983 i2c_writel(i2c_dev, val, reg);
984}
985
Sowjanya Komatinenice956242019-02-12 11:06:43 -0800986static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
987{
988 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
989 int err;
990 unsigned long time_left;
991 u32 reg;
992
993 reinit_completion(&i2c_dev->msg_complete);
994 reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) |
995 I2C_BC_STOP_COND | I2C_BC_TERMINATE;
996 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
997 if (i2c_dev->hw->has_config_load_reg) {
998 err = tegra_i2c_wait_for_config_load(i2c_dev);
999 if (err)
1000 return err;
1001 }
1002
1003 reg |= I2C_BC_ENABLE;
1004 i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
1005 tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
1006
1007 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001008 msecs_to_jiffies(50));
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001009 if (time_left == 0) {
1010 dev_err(i2c_dev->dev, "timed out for bus clear\n");
1011 return -ETIMEDOUT;
1012 }
1013
1014 reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
1015 if (!(reg & I2C_BC_STATUS)) {
1016 dev_err(i2c_dev->dev,
1017 "un-recovered arbitration lost\n");
1018 return -EIO;
1019 }
1020
1021 return -EAGAIN;
1022}
1023
Colin Crossdb811ca2011-02-20 17:14:21 -08001024static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301025 struct i2c_msg *msg, enum msg_end_type end_state)
Colin Crossdb811ca2011-02-20 17:14:21 -08001026{
1027 u32 packet_header;
1028 u32 int_mask;
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001029 unsigned long time_left;
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301030 unsigned long flags;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001031 size_t xfer_size;
1032 u32 *buffer = NULL;
1033 int err = 0;
1034 bool dma;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001035 u16 xfer_time = 100;
Colin Crossdb811ca2011-02-20 17:14:21 -08001036
1037 tegra_i2c_flush_fifos(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001038
Colin Crossdb811ca2011-02-20 17:14:21 -08001039 i2c_dev->msg_buf = msg->buf;
1040 i2c_dev->msg_buf_remaining = msg->len;
1041 i2c_dev->msg_err = I2C_ERR_NONE;
1042 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
Wolfram Sang16735d02013-11-14 14:32:02 -08001043 reinit_completion(&i2c_dev->msg_complete);
Colin Crossdb811ca2011-02-20 17:14:21 -08001044
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001045 if (i2c_dev->msg_read)
1046 xfer_size = msg->len;
1047 else
1048 xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;
1049
1050 xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1051 i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_MAX_LEN) &&
1052 i2c_dev->dma_buf;
1053 tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
1054 dma = i2c_dev->is_curr_dma_xfer;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001055 /*
1056 * Transfer time in mSec = Total bits / transfer rate
1057 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
1058 */
1059 xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
1060 i2c_dev->bus_clk_rate);
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301061 spin_lock_irqsave(&i2c_dev->xfer_lock, flags);
1062
1063 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
1064 tegra_i2c_unmask_irq(i2c_dev, int_mask);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001065 if (dma) {
1066 if (i2c_dev->msg_read) {
1067 dma_sync_single_for_device(i2c_dev->dev,
1068 i2c_dev->dma_phys,
1069 xfer_size,
1070 DMA_FROM_DEVICE);
1071 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1072 if (err < 0) {
1073 dev_err(i2c_dev->dev,
1074 "starting RX DMA failed, err %d\n",
1075 err);
1076 goto unlock;
1077 }
1078
1079 } else {
1080 dma_sync_single_for_cpu(i2c_dev->dev,
1081 i2c_dev->dma_phys,
1082 xfer_size,
1083 DMA_TO_DEVICE);
1084 buffer = i2c_dev->dma_buf;
1085 }
1086 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301087
Colin Crossdb811ca2011-02-20 17:14:21 -08001088 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
1089 PACKET_HEADER0_PROTOCOL_I2C |
1090 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
1091 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001092 if (dma && !i2c_dev->msg_read)
1093 *buffer++ = packet_header;
1094 else
1095 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001096
1097 packet_header = msg->len - 1;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001098 if (dma && !i2c_dev->msg_read)
1099 *buffer++ = packet_header;
1100 else
1101 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001102
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301103 packet_header = I2C_HEADER_IE_ENABLE;
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301104 if (end_state == MSG_END_CONTINUE)
1105 packet_header |= I2C_HEADER_CONTINUE_XFER;
1106 else if (end_state == MSG_END_REPEAT_START)
Erik Gilling2078cf32011-04-25 15:32:26 -06001107 packet_header |= I2C_HEADER_REPEAT_START;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301108 if (msg->flags & I2C_M_TEN) {
1109 packet_header |= msg->addr;
Colin Crossdb811ca2011-02-20 17:14:21 -08001110 packet_header |= I2C_HEADER_10BIT_ADDR;
Laxman Dewangan353f56b2012-04-24 12:49:35 +05301111 } else {
1112 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
1113 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001114 if (msg->flags & I2C_M_IGNORE_NAK)
1115 packet_header |= I2C_HEADER_CONT_ON_NAK;
Colin Crossdb811ca2011-02-20 17:14:21 -08001116 if (msg->flags & I2C_M_RD)
1117 packet_header |= I2C_HEADER_READ;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001118 if (dma && !i2c_dev->msg_read)
1119 *buffer++ = packet_header;
1120 else
1121 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
Colin Crossdb811ca2011-02-20 17:14:21 -08001122
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001123 if (!i2c_dev->msg_read) {
1124 if (dma) {
1125 memcpy(buffer, msg->buf, msg->len);
1126 dma_sync_single_for_device(i2c_dev->dev,
1127 i2c_dev->dma_phys,
1128 xfer_size,
1129 DMA_TO_DEVICE);
1130 err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
1131 if (err < 0) {
1132 dev_err(i2c_dev->dev,
1133 "starting TX DMA failed, err %d\n",
1134 err);
1135 goto unlock;
1136 }
1137 } else {
1138 tegra_i2c_fill_tx_fifo(i2c_dev);
1139 }
1140 }
Colin Crossdb811ca2011-02-20 17:14:21 -08001141
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301142 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
1143 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001144 if (!dma) {
1145 if (msg->flags & I2C_M_RD)
1146 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
1147 else if (i2c_dev->msg_buf_remaining)
1148 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
1149 }
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301150
Colin Crossdb811ca2011-02-20 17:14:21 -08001151 tegra_i2c_unmask_irq(i2c_dev, int_mask);
1152 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
1153 i2c_readl(i2c_dev, I2C_INT_MASK));
1154
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001155unlock:
1156 spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
1157
1158 if (dma) {
1159 if (err)
1160 return err;
1161
1162 time_left = wait_for_completion_timeout(
1163 &i2c_dev->dma_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001164 msecs_to_jiffies(xfer_time));
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001165 if (time_left == 0) {
1166 dev_err(i2c_dev->dev, "DMA transfer timeout\n");
1167 dmaengine_terminate_sync(i2c_dev->msg_read ?
1168 i2c_dev->rx_dma_chan :
1169 i2c_dev->tx_dma_chan);
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001170 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001171 return -ETIMEDOUT;
1172 }
1173
1174 if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
1175 dma_sync_single_for_cpu(i2c_dev->dev,
1176 i2c_dev->dma_phys,
1177 xfer_size,
1178 DMA_FROM_DEVICE);
1179 memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf,
1180 msg->len);
1181 }
1182
1183 if (i2c_dev->msg_err != I2C_ERR_NONE)
1184 dmaengine_synchronize(i2c_dev->msg_read ?
1185 i2c_dev->rx_dma_chan :
1186 i2c_dev->tx_dma_chan);
1187 }
1188
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001189 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001190 msecs_to_jiffies(xfer_time));
Colin Crossdb811ca2011-02-20 17:14:21 -08001191 tegra_i2c_mask_irq(i2c_dev, int_mask);
1192
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001193 if (time_left == 0) {
Colin Crossdb811ca2011-02-20 17:14:21 -08001194 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
1195
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001196 tegra_i2c_init(i2c_dev, true);
Colin Crossdb811ca2011-02-20 17:14:21 -08001197 return -ETIMEDOUT;
1198 }
1199
Nicholas Mc Guire6973a392015-03-01 09:17:41 -05001200 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
1201 time_left, completion_done(&i2c_dev->msg_complete),
1202 i2c_dev->msg_err);
Colin Crossdb811ca2011-02-20 17:14:21 -08001203
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001204 i2c_dev->is_curr_dma_xfer = false;
Colin Crossdb811ca2011-02-20 17:14:21 -08001205 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
1206 return 0;
1207
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001208 tegra_i2c_init(i2c_dev, true);
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001209 /* start recovery upon arbitration loss in single master mode */
1210 if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
1211 if (!i2c_dev->is_multimaster_mode)
1212 return i2c_recover_bus(&i2c_dev->adapter);
1213 return -EAGAIN;
1214 }
1215
Colin Crossdb811ca2011-02-20 17:14:21 -08001216 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
1217 if (msg->flags & I2C_M_IGNORE_NAK)
1218 return 0;
1219 return -EREMOTEIO;
1220 }
1221
1222 return -EIO;
1223}
1224
1225static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1226 int num)
1227{
1228 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1229 int i;
1230 int ret = 0;
1231
Jon Hunter1f50ad22016-08-26 14:09:04 +01001232 ret = pm_runtime_get_sync(i2c_dev->dev);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001233 if (ret < 0) {
Jon Hunter1f50ad22016-08-26 14:09:04 +01001234 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
Laxman Dewangan132c8032013-03-15 05:34:08 +00001235 return ret;
1236 }
1237
Colin Crossdb811ca2011-02-20 17:14:21 -08001238 for (i = 0; i < num; i++) {
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301239 enum msg_end_type end_type = MSG_END_STOP;
Jon Hunterf5076682016-08-26 14:08:59 +01001240
Laxman Dewanganc8f5af22012-06-13 15:42:38 +05301241 if (i < (num - 1)) {
1242 if (msgs[i + 1].flags & I2C_M_NOSTART)
1243 end_type = MSG_END_CONTINUE;
1244 else
1245 end_type = MSG_END_REPEAT_START;
1246 }
1247 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
Colin Crossdb811ca2011-02-20 17:14:21 -08001248 if (ret)
1249 break;
1250 }
Jon Hunter1f50ad22016-08-26 14:09:04 +01001251
1252 pm_runtime_put(i2c_dev->dev);
1253
Colin Crossdb811ca2011-02-20 17:14:21 -08001254 return ret ?: i;
1255}
1256
1257static u32 tegra_i2c_func(struct i2c_adapter *adap)
1258{
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301259 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
Wolfram Sang4bb28e32015-06-16 19:47:21 +02001260 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
1261 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301262
1263 if (i2c_dev->hw->has_continue_xfer_support)
1264 ret |= I2C_FUNC_NOSTART;
1265 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001266}
1267
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301268static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
1269{
1270 struct device_node *np = i2c_dev->dev->of_node;
1271 int ret;
1272
1273 ret = of_property_read_u32(np, "clock-frequency",
1274 &i2c_dev->bus_clk_rate);
1275 if (ret)
1276 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
1277
1278 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
1279 "multi-master");
1280}
1281
Colin Crossdb811ca2011-02-20 17:14:21 -08001282static const struct i2c_algorithm tegra_i2c_algo = {
1283 .master_xfer = tegra_i2c_xfer,
1284 .functionality = tegra_i2c_func,
1285};
1286
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001287/* payload size is only 12 bit */
Bhumika Goyalae3923a2017-08-21 17:42:04 +05301288static const struct i2c_adapter_quirks tegra_i2c_quirks = {
Wolfram Sangc96c0f22018-07-23 22:26:12 +02001289 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001290 .max_read_len = SZ_4K,
1291 .max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
Wolfram Sang3aaa34b2015-06-16 19:57:29 +02001292};
1293
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001294static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
1295 .flags = I2C_AQ_NO_ZERO_LEN,
Sowjanya Komatinenib03ff2a2019-02-12 11:06:45 -08001296 .max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001297};
1298
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001299static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
1300 .recover_bus = tegra_i2c_issue_bus_clear,
1301};
1302
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301303static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
1304 .has_continue_xfer_support = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301305 .has_per_pkt_xfer_complete_irq = false,
1306 .has_single_clk_source = false,
1307 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001308 .clk_divisor_std_mode = 0,
1309 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301310 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301311 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301312 .has_multi_master_mode = false,
1313 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001314 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001315 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001316 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001317 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001318 .tlow_std_mode = 0x4,
1319 .thigh_std_mode = 0x2,
1320 .tlow_fast_fastplus_mode = 0x4,
1321 .thigh_fast_fastplus_mode = 0x2,
1322 .setup_hold_time_std_mode = 0x0,
1323 .setup_hold_time_fast_fast_plus_mode = 0x0,
1324 .setup_hold_time_hs_mode = 0x0,
1325 .has_interface_timing_reg = false,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301326};
1327
1328static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
1329 .has_continue_xfer_support = true,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301330 .has_per_pkt_xfer_complete_irq = false,
1331 .has_single_clk_source = false,
1332 .clk_divisor_hs_mode = 3,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001333 .clk_divisor_std_mode = 0,
1334 .clk_divisor_fast_mode = 0,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301335 .clk_divisor_fast_plus_mode = 0,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301336 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301337 .has_multi_master_mode = false,
1338 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001339 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001340 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001341 .supports_bus_clear = false,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001342 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001343 .tlow_std_mode = 0x4,
1344 .thigh_std_mode = 0x2,
1345 .tlow_fast_fastplus_mode = 0x4,
1346 .thigh_fast_fastplus_mode = 0x2,
1347 .setup_hold_time_std_mode = 0x0,
1348 .setup_hold_time_fast_fast_plus_mode = 0x0,
1349 .setup_hold_time_hs_mode = 0x0,
1350 .has_interface_timing_reg = false,
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301351};
1352
1353static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
1354 .has_continue_xfer_support = true,
1355 .has_per_pkt_xfer_complete_irq = true,
1356 .has_single_clk_source = true,
1357 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001358 .clk_divisor_std_mode = 0x19,
1359 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301360 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301361 .has_config_load_reg = false,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301362 .has_multi_master_mode = false,
1363 .has_slcg_override_reg = false,
Thierry Redingc5907c62018-06-19 12:49:42 +02001364 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001365 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001366 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001367 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001368 .tlow_std_mode = 0x4,
1369 .thigh_std_mode = 0x2,
1370 .tlow_fast_fastplus_mode = 0x4,
1371 .thigh_fast_fastplus_mode = 0x2,
1372 .setup_hold_time_std_mode = 0x0,
1373 .setup_hold_time_fast_fast_plus_mode = 0x0,
1374 .setup_hold_time_hs_mode = 0x0,
1375 .has_interface_timing_reg = false,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301376};
1377
1378static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
1379 .has_continue_xfer_support = true,
1380 .has_per_pkt_xfer_complete_irq = true,
1381 .has_single_clk_source = true,
1382 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001383 .clk_divisor_std_mode = 0x19,
1384 .clk_divisor_fast_mode = 0x19,
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301385 .clk_divisor_fast_plus_mode = 0x10,
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301386 .has_config_load_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301387 .has_multi_master_mode = false,
1388 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001389 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001390 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001391 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001392 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001393 .tlow_std_mode = 0x4,
1394 .thigh_std_mode = 0x2,
1395 .tlow_fast_fastplus_mode = 0x4,
1396 .thigh_fast_fastplus_mode = 0x2,
1397 .setup_hold_time_std_mode = 0x0,
1398 .setup_hold_time_fast_fast_plus_mode = 0x0,
1399 .setup_hold_time_hs_mode = 0x0,
1400 .has_interface_timing_reg = true,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301401};
1402
1403static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
1404 .has_continue_xfer_support = true,
1405 .has_per_pkt_xfer_complete_irq = true,
1406 .has_single_clk_source = true,
1407 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001408 .clk_divisor_std_mode = 0x19,
1409 .clk_divisor_fast_mode = 0x19,
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301410 .clk_divisor_fast_plus_mode = 0x10,
1411 .has_config_load_reg = true,
1412 .has_multi_master_mode = true,
1413 .has_slcg_override_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001414 .has_mst_fifo = false,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001415 .quirks = &tegra_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001416 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001417 .has_apb_dma = true,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001418 .tlow_std_mode = 0x4,
1419 .thigh_std_mode = 0x2,
1420 .tlow_fast_fastplus_mode = 0x4,
1421 .thigh_fast_fastplus_mode = 0x2,
1422 .setup_hold_time_std_mode = 0,
1423 .setup_hold_time_fast_fast_plus_mode = 0,
1424 .setup_hold_time_hs_mode = 0,
1425 .has_interface_timing_reg = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001426};
1427
1428static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
1429 .has_continue_xfer_support = true,
1430 .has_per_pkt_xfer_complete_irq = true,
1431 .has_single_clk_source = true,
1432 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001433 .clk_divisor_std_mode = 0x16,
1434 .clk_divisor_fast_mode = 0x19,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001435 .clk_divisor_fast_plus_mode = 0x10,
1436 .has_config_load_reg = true,
1437 .has_multi_master_mode = true,
1438 .has_slcg_override_reg = true,
1439 .has_mst_fifo = true,
1440 .quirks = &tegra_i2c_quirks,
1441 .supports_bus_clear = true,
1442 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001443 .tlow_std_mode = 0x4,
1444 .thigh_std_mode = 0x3,
1445 .tlow_fast_fastplus_mode = 0x4,
1446 .thigh_fast_fastplus_mode = 0x2,
1447 .setup_hold_time_std_mode = 0,
1448 .setup_hold_time_fast_fast_plus_mode = 0,
1449 .setup_hold_time_hs_mode = 0,
1450 .has_interface_timing_reg = true,
Thierry Redingc5907c62018-06-19 12:49:42 +02001451};
1452
1453static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
1454 .has_continue_xfer_support = true,
1455 .has_per_pkt_xfer_complete_irq = true,
1456 .has_single_clk_source = true,
1457 .clk_divisor_hs_mode = 1,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001458 .clk_divisor_std_mode = 0x4f,
1459 .clk_divisor_fast_mode = 0x3c,
1460 .clk_divisor_fast_plus_mode = 0x16,
Thierry Redingc5907c62018-06-19 12:49:42 +02001461 .has_config_load_reg = true,
1462 .has_multi_master_mode = true,
1463 .has_slcg_override_reg = true,
1464 .has_mst_fifo = true,
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001465 .quirks = &tegra194_i2c_quirks,
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001466 .supports_bus_clear = true,
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001467 .has_apb_dma = false,
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001468 .tlow_std_mode = 0x8,
1469 .thigh_std_mode = 0x7,
1470 .tlow_fast_fastplus_mode = 0x2,
1471 .thigh_fast_fastplus_mode = 0x2,
1472 .setup_hold_time_std_mode = 0x08080808,
1473 .setup_hold_time_fast_fast_plus_mode = 0x02020202,
1474 .setup_hold_time_hs_mode = 0x090909,
1475 .has_interface_timing_reg = true,
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301476};
1477
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301478/* Match table for of_platform binding */
Bill Pemberton0b255e92012-11-27 15:59:38 -05001479static const struct of_device_id tegra_i2c_of_match[] = {
Thierry Redingc5907c62018-06-19 12:49:42 +02001480 { .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001481 { .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301482 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
Laxman Dewangan6f4664b2015-06-30 16:24:26 +05301483 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301484 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301485 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
1486 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
1487 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
1488 {},
1489};
1490MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
Laxman Dewangan6ad068e2012-08-19 00:47:46 +05301491
Bill Pemberton0b255e92012-11-27 15:59:38 -05001492static int tegra_i2c_probe(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001493{
1494 struct tegra_i2c_dev *i2c_dev;
Colin Crossdb811ca2011-02-20 17:14:21 -08001495 struct resource *res;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301496 struct clk *div_clk;
1497 struct clk *fast_clk;
Olof Johanssonf533c612011-10-12 17:33:00 -07001498 void __iomem *base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001499 phys_addr_t base_phys;
Colin Crossdb811ca2011-02-20 17:14:21 -08001500 int irq;
1501 int ret = 0;
1502
1503 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001504 base_phys = res->start;
Thierry Reding84dbf802013-01-21 11:09:03 +01001505 base = devm_ioremap_resource(&pdev->dev, res);
1506 if (IS_ERR(base))
1507 return PTR_ERR(base);
Colin Crossdb811ca2011-02-20 17:14:21 -08001508
1509 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1510 if (!res) {
1511 dev_err(&pdev->dev, "no irq resource\n");
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301512 return -EINVAL;
Colin Crossdb811ca2011-02-20 17:14:21 -08001513 }
1514 irq = res->start;
1515
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301516 div_clk = devm_clk_get(&pdev->dev, "div-clk");
1517 if (IS_ERR(div_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001518 dev_err(&pdev->dev, "missing controller clock\n");
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301519 return PTR_ERR(div_clk);
Colin Crossdb811ca2011-02-20 17:14:21 -08001520 }
1521
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301522 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
Jingoo Han46797a22014-05-13 10:51:58 +09001523 if (!i2c_dev)
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301524 return -ENOMEM;
Colin Crossdb811ca2011-02-20 17:14:21 -08001525
1526 i2c_dev->base = base;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001527 i2c_dev->base_phys = base_phys;
Laxman Dewangan14e92bd2012-08-08 13:21:32 +05301528 i2c_dev->div_clk = div_clk;
Colin Crossdb811ca2011-02-20 17:14:21 -08001529 i2c_dev->adapter.algo = &tegra_i2c_algo;
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001530 i2c_dev->adapter.retries = 1;
Sowjanya Komatineni80d40622019-02-12 11:06:47 -08001531 i2c_dev->adapter.timeout = 6 * HZ;
Colin Crossdb811ca2011-02-20 17:14:21 -08001532 i2c_dev->irq = irq;
1533 i2c_dev->cont_id = pdev->id;
1534 i2c_dev->dev = &pdev->dev;
John Bonesio5c470f32011-06-22 09:16:56 -07001535
Philipp Zabel94d3b652017-07-19 17:25:34 +02001536 i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001537 if (IS_ERR(i2c_dev->rst)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001538 dev_err(&pdev->dev, "missing controller reset\n");
Stephen Warrendda9d6a2013-11-06 16:42:05 -07001539 return PTR_ERR(i2c_dev->rst);
1540 }
1541
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301542 tegra_i2c_parse_dt(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001543
Jon Huntera9e32cd2016-08-26 14:09:01 +01001544 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
1545 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
1546 "nvidia,tegra20-i2c-dvc");
Sowjanya Komatinenib67d4532019-01-08 13:59:10 -08001547 i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001548 i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len +
1549 I2C_PACKET_HEADER_SIZE;
Colin Crossdb811ca2011-02-20 17:14:21 -08001550 init_completion(&i2c_dev->msg_complete);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001551 init_completion(&i2c_dev->dma_complete);
Shardar Shariff Md77821b462016-08-31 18:58:44 +05301552 spin_lock_init(&i2c_dev->xfer_lock);
Colin Crossdb811ca2011-02-20 17:14:21 -08001553
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301554 if (!i2c_dev->hw->has_single_clk_source) {
1555 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
1556 if (IS_ERR(fast_clk)) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001557 dev_err(&pdev->dev, "missing fast clock\n");
Laxman Dewangan2a2897b2013-01-05 17:34:46 +05301558 return PTR_ERR(fast_clk);
1559 }
1560 i2c_dev->fast_clk = fast_clk;
1561 }
1562
Colin Crossdb811ca2011-02-20 17:14:21 -08001563 platform_set_drvdata(pdev, i2c_dev);
1564
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001565 if (!i2c_dev->hw->has_single_clk_source) {
1566 ret = clk_prepare(i2c_dev->fast_clk);
1567 if (ret < 0) {
1568 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1569 return ret;
1570 }
1571 }
1572
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001573 if (i2c_dev->bus_clk_rate > I2C_FAST_MODE &&
1574 i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE)
Laxman Dewangand57f5de2015-06-30 16:24:27 +05301575 i2c_dev->clk_divisor_non_hs_mode =
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001576 i2c_dev->hw->clk_divisor_fast_plus_mode;
1577 else if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
1578 i2c_dev->bus_clk_rate <= I2C_FAST_MODE)
1579 i2c_dev->clk_divisor_non_hs_mode =
1580 i2c_dev->hw->clk_divisor_fast_mode;
1581 else
1582 i2c_dev->clk_divisor_non_hs_mode =
1583 i2c_dev->hw->clk_divisor_std_mode;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001584
1585 ret = clk_prepare(i2c_dev->div_clk);
1586 if (ret < 0) {
1587 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
1588 goto unprepare_fast_clk;
1589 }
1590
Jon Hunter1f50ad22016-08-26 14:09:04 +01001591 pm_runtime_enable(&pdev->dev);
1592 if (!pm_runtime_enabled(&pdev->dev)) {
1593 ret = tegra_i2c_runtime_resume(&pdev->dev);
1594 if (ret < 0) {
1595 dev_err(&pdev->dev, "runtime resume failed\n");
1596 goto unprepare_div_clk;
1597 }
1598 }
1599
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301600 if (i2c_dev->is_multimaster_mode) {
1601 ret = clk_enable(i2c_dev->div_clk);
1602 if (ret < 0) {
1603 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
1604 ret);
Jon Hunter1f50ad22016-08-26 14:09:04 +01001605 goto disable_rpm;
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301606 }
1607 }
1608
Sowjanya Komatinenice956242019-02-12 11:06:43 -08001609 if (i2c_dev->hw->supports_bus_clear)
1610 i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;
1611
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001612 ret = tegra_i2c_init_dma(i2c_dev);
1613 if (ret < 0)
1614 goto disable_div_clk;
1615
Sowjanya Komatineni0940d242019-02-12 11:06:48 -08001616 ret = tegra_i2c_init(i2c_dev, false);
Colin Crossdb811ca2011-02-20 17:14:21 -08001617 if (ret) {
Jon Huntere8e999cb2016-08-26 14:09:00 +01001618 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001619 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001620 }
1621
Laxman Dewangan9cbb6b22012-06-13 15:42:39 +05301622 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
Laxman Dewangan91b370a2012-11-01 22:08:14 +05301623 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001624 if (ret) {
1625 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001626 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001627 }
1628
Colin Crossdb811ca2011-02-20 17:14:21 -08001629 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
1630 i2c_dev->adapter.owner = THIS_MODULE;
Wolfram Sang60251892014-07-10 13:46:35 +02001631 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
Jon Hunter0da9ab82016-08-26 14:09:02 +01001632 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
Colin Crossdb811ca2011-02-20 17:14:21 -08001633 sizeof(i2c_dev->adapter.name));
Colin Crossdb811ca2011-02-20 17:14:21 -08001634 i2c_dev->adapter.dev.parent = &pdev->dev;
1635 i2c_dev->adapter.nr = pdev->id;
John Bonesio5c470f32011-06-22 09:16:56 -07001636 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
Colin Crossdb811ca2011-02-20 17:14:21 -08001637
1638 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
Wolfram Sangea734402016-08-09 13:36:17 +02001639 if (ret)
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001640 goto release_dma;
Colin Crossdb811ca2011-02-20 17:14:21 -08001641
Colin Crossdb811ca2011-02-20 17:14:21 -08001642 return 0;
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001643
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001644release_dma:
1645 tegra_i2c_release_dma(i2c_dev);
1646
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301647disable_div_clk:
1648 if (i2c_dev->is_multimaster_mode)
1649 clk_disable(i2c_dev->div_clk);
1650
Jon Hunter1f50ad22016-08-26 14:09:04 +01001651disable_rpm:
1652 pm_runtime_disable(&pdev->dev);
1653 if (!pm_runtime_status_suspended(&pdev->dev))
1654 tegra_i2c_runtime_suspend(&pdev->dev);
1655
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001656unprepare_div_clk:
1657 clk_unprepare(i2c_dev->div_clk);
1658
1659unprepare_fast_clk:
1660 if (!i2c_dev->hw->has_single_clk_source)
1661 clk_unprepare(i2c_dev->fast_clk);
1662
1663 return ret;
Colin Crossdb811ca2011-02-20 17:14:21 -08001664}
1665
Bill Pemberton0b255e92012-11-27 15:59:38 -05001666static int tegra_i2c_remove(struct platform_device *pdev)
Colin Crossdb811ca2011-02-20 17:14:21 -08001667{
1668 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
Jon Hunterf5076682016-08-26 14:08:59 +01001669
Colin Crossdb811ca2011-02-20 17:14:21 -08001670 i2c_del_adapter(&i2c_dev->adapter);
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001671
Shardar Shariff Md497fbe22016-03-14 18:52:18 +05301672 if (i2c_dev->is_multimaster_mode)
1673 clk_disable(i2c_dev->div_clk);
1674
Jon Hunter1f50ad22016-08-26 14:09:04 +01001675 pm_runtime_disable(&pdev->dev);
1676 if (!pm_runtime_status_suspended(&pdev->dev))
1677 tegra_i2c_runtime_suspend(&pdev->dev);
1678
Mikko Perttunenc9a9ef42014-09-05 12:28:18 +03001679 clk_unprepare(i2c_dev->div_clk);
1680 if (!i2c_dev->hw->has_single_clk_source)
1681 clk_unprepare(i2c_dev->fast_clk);
1682
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001683 tegra_i2c_release_dma(i2c_dev);
Colin Crossdb811ca2011-02-20 17:14:21 -08001684 return 0;
1685}
1686
Laxman Dewangan371e67c2012-08-18 17:49:58 +05301687#ifdef CONFIG_PM_SLEEP
Jon Hunter1f50ad22016-08-26 14:09:04 +01001688static const struct dev_pm_ops tegra_i2c_pm = {
1689 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1690 NULL)
Jon Hunter1f50ad22016-08-26 14:09:04 +01001691};
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001692#define TEGRA_I2C_PM (&tegra_i2c_pm)
1693#else
1694#define TEGRA_I2C_PM NULL
Colin Crossdb811ca2011-02-20 17:14:21 -08001695#endif
1696
1697static struct platform_driver tegra_i2c_driver = {
1698 .probe = tegra_i2c_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -05001699 .remove = tegra_i2c_remove,
Colin Crossdb811ca2011-02-20 17:14:21 -08001700 .driver = {
1701 .name = "tegra-i2c",
Stephen Warren49a64ac2013-03-21 08:08:46 +00001702 .of_match_table = tegra_i2c_of_match,
Rafael J. Wysocki6a7b3c32012-07-11 21:27:30 +02001703 .pm = TEGRA_I2C_PM,
Colin Crossdb811ca2011-02-20 17:14:21 -08001704 },
1705};
1706
Sowjanya Komatineni86c92b92019-02-12 11:06:46 -08001707module_platform_driver(tegra_i2c_driver);
Colin Crossdb811ca2011-02-20 17:14:21 -08001708
1709MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1710MODULE_AUTHOR("Colin Cross");
1711MODULE_LICENSE("GPL v2");