i2c-tegra.c 50.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
C
Colin Cross 已提交
2 3 4 5 6 7 8 9
/*
 * drivers/i2c/busses/i2c-tegra.c
 *
 * Copyright (C) 2010 Google, Inc.
 * Author: Colin Cross <ccross@android.com>
 */

#include <linux/clk.h>
10
#include <linux/delay.h>
11 12
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
C
Colin Cross 已提交
13 14
#include <linux/err.h>
#include <linux/i2c.h>
15
#include <linux/init.h>
C
Colin Cross 已提交
16
#include <linux/interrupt.h>
17 18
#include <linux/io.h>
#include <linux/iopoll.h>
19
#include <linux/irq.h>
20
#include <linux/kernel.h>
21
#include <linux/ktime.h>
22
#include <linux/module.h>
23
#include <linux/of_device.h>
J
Jon Hunter 已提交
24
#include <linux/pinctrl/consumer.h>
25
#include <linux/platform_device.h>
26
#include <linux/pm_runtime.h>
27
#include <linux/reset.h>
C
Colin Cross 已提交
28 29 30 31

#define BYTES_PER_FIFO_WORD 4

#define I2C_CNFG				0x000
J
Jay Cheng 已提交
32
#define I2C_CNFG_DEBOUNCE_CNT_SHIFT		12
J
Jon Hunter 已提交
33 34 35
#define I2C_CNFG_PACKET_MODE_EN			BIT(10)
#define I2C_CNFG_NEW_MASTER_FSM			BIT(11)
#define I2C_CNFG_MULTI_MASTER_MODE		BIT(17)
36
#define I2C_STATUS				0x01C
C
Colin Cross 已提交
37
#define I2C_SL_CNFG				0x020
J
Jon Hunter 已提交
38 39
#define I2C_SL_CNFG_NACK			BIT(1)
#define I2C_SL_CNFG_NEWSL			BIT(2)
C
Colin Cross 已提交
40
#define I2C_SL_ADDR1				0x02c
41
#define I2C_SL_ADDR2				0x030
C
Colin Cross 已提交
42 43 44 45
#define I2C_TX_FIFO				0x050
#define I2C_RX_FIFO				0x054
#define I2C_PACKET_TRANSFER_STATUS		0x058
#define I2C_FIFO_CONTROL			0x05c
J
Jon Hunter 已提交
46 47
#define I2C_FIFO_CONTROL_TX_FLUSH		BIT(1)
#define I2C_FIFO_CONTROL_RX_FLUSH		BIT(0)
48 49
#define I2C_FIFO_CONTROL_TX_TRIG(x)		(((x) - 1) << 5)
#define I2C_FIFO_CONTROL_RX_TRIG(x)		(((x) - 1) << 2)
C
Colin Cross 已提交
50 51 52 53 54 55 56
#define I2C_FIFO_STATUS				0x060
#define I2C_FIFO_STATUS_TX_MASK			0xF0
#define I2C_FIFO_STATUS_TX_SHIFT		4
#define I2C_FIFO_STATUS_RX_MASK			0x0F
#define I2C_FIFO_STATUS_RX_SHIFT		0
#define I2C_INT_MASK				0x064
#define I2C_INT_STATUS				0x068
57
#define I2C_INT_BUS_CLR_DONE			BIT(11)
J
Jon Hunter 已提交
58 59 60 61 62
#define I2C_INT_PACKET_XFER_COMPLETE		BIT(7)
#define I2C_INT_NO_ACK				BIT(3)
#define I2C_INT_ARBITRATION_LOST		BIT(2)
#define I2C_INT_TX_FIFO_DATA_REQ		BIT(1)
#define I2C_INT_RX_FIFO_DATA_REQ		BIT(0)
C
Colin Cross 已提交
63
#define I2C_CLK_DIVISOR				0x06c
64
#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT	16
C
Colin Cross 已提交
65 66

#define DVC_CTRL_REG1				0x000
J
Jon Hunter 已提交
67
#define DVC_CTRL_REG1_INTR_EN			BIT(10)
C
Colin Cross 已提交
68
#define DVC_CTRL_REG3				0x008
J
Jon Hunter 已提交
69 70
#define DVC_CTRL_REG3_SW_PROG			BIT(26)
#define DVC_CTRL_REG3_I2C_DONE_INTR_EN		BIT(30)
C
Colin Cross 已提交
71
#define DVC_STATUS				0x00c
J
Jon Hunter 已提交
72
#define DVC_STATUS_I2C_DONE_INTR		BIT(30)
C
Colin Cross 已提交
73 74

#define I2C_ERR_NONE				0x00
B
Bitan Biswas 已提交
75 76 77
#define I2C_ERR_NO_ACK				BIT(0)
#define I2C_ERR_ARBITRATION_LOST		BIT(1)
#define I2C_ERR_UNKNOWN_INTERRUPT		BIT(2)
B
Bitan Biswas 已提交
78
#define I2C_ERR_RX_BUFFER_OVERFLOW		BIT(3)
C
Colin Cross 已提交
79 80 81 82

#define PACKET_HEADER0_HEADER_SIZE_SHIFT	28
#define PACKET_HEADER0_PACKET_ID_SHIFT		16
#define PACKET_HEADER0_CONT_ID_SHIFT		12
J
Jon Hunter 已提交
83
#define PACKET_HEADER0_PROTOCOL_I2C		BIT(4)
C
Colin Cross 已提交
84

J
Jon Hunter 已提交
85 86 87 88 89 90
#define I2C_HEADER_CONT_ON_NAK			BIT(21)
#define I2C_HEADER_READ				BIT(19)
#define I2C_HEADER_10BIT_ADDR			BIT(18)
#define I2C_HEADER_IE_ENABLE			BIT(17)
#define I2C_HEADER_REPEAT_START			BIT(16)
#define I2C_HEADER_CONTINUE_XFER		BIT(15)
C
Colin Cross 已提交
91
#define I2C_HEADER_SLAVE_ADDR_SHIFT		1
92

93 94 95 96 97 98 99 100 101
#define I2C_BUS_CLEAR_CNFG			0x084
#define I2C_BC_SCLK_THRESHOLD			9
#define I2C_BC_SCLK_THRESHOLD_SHIFT		16
#define I2C_BC_STOP_COND			BIT(2)
#define I2C_BC_TERMINATE			BIT(1)
#define I2C_BC_ENABLE				BIT(0)
#define I2C_BUS_CLEAR_STATUS			0x088
#define I2C_BC_STATUS				BIT(0)

102
#define I2C_CONFIG_LOAD				0x08C
J
Jon Hunter 已提交
103
#define I2C_MSTR_CONFIG_LOAD			BIT(0)
104

105
#define I2C_CLKEN_OVERRIDE			0x090
J
Jon Hunter 已提交
106
#define I2C_MST_CORE_CLKEN_OVR			BIT(0)
107

108 109
#define I2C_CONFIG_LOAD_TIMEOUT			1000000

110 111 112 113 114 115 116 117 118 119 120 121
#define I2C_MST_FIFO_CONTROL			0x0b4
#define I2C_MST_FIFO_CONTROL_RX_FLUSH		BIT(0)
#define I2C_MST_FIFO_CONTROL_TX_FLUSH		BIT(1)
#define I2C_MST_FIFO_CONTROL_RX_TRIG(x)		(((x) - 1) <<  4)
#define I2C_MST_FIFO_CONTROL_TX_TRIG(x)		(((x) - 1) << 16)

#define I2C_MST_FIFO_STATUS			0x0b8
#define I2C_MST_FIFO_STATUS_RX_MASK		0xff
#define I2C_MST_FIFO_STATUS_RX_SHIFT		0
#define I2C_MST_FIFO_STATUS_TX_MASK		0xff0000
#define I2C_MST_FIFO_STATUS_TX_SHIFT		16

122 123 124 125 126 127 128 129
#define I2C_INTERFACE_TIMING_0			0x94
#define I2C_THIGH_SHIFT				8
#define I2C_INTERFACE_TIMING_1			0x98

#define I2C_STANDARD_MODE			100000
#define I2C_FAST_MODE				400000
#define I2C_FAST_PLUS_MODE			1000000

130 131 132
/* Packet header size in bytes */
#define I2C_PACKET_HEADER_SIZE			12

133
/*
134 135 136 137
 * I2C Controller will use PIO mode for transfers up to 32 bytes in order to
 * avoid DMA overhead, otherwise external APB DMA controller will be used.
 * Note that the actual MAX PIO length is 20 bytes because 32 bytes include
 * I2C_PACKET_HEADER_SIZE.
138
 */
139
#define I2C_PIO_MODE_PREFERRED_LEN		32
140

141 142 143 144 145 146 147 148 149 150 151 152
/*
 * msg_end_type: The bus control which need to be send at end of transfer.
 * @MSG_END_STOP: Send stop pulse at end of transfer.
 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
 * @MSG_END_CONTINUE: The following on message is coming and so do not send
 *		stop or repeat start.
 */
enum msg_end_type {
	MSG_END_STOP,
	MSG_END_REPEAT_START,
	MSG_END_CONTINUE,
};
C
Colin Cross 已提交
153

154 155 156
/**
 * struct tegra_i2c_hw_feature : Different HW support on Tegra
 * @has_continue_xfer_support: Continue transfer supports.
157 158
 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
 *		complete interrupt per packet basis.
159 160
 * @has_single_clk_source: The I2C controller has single clock source. Tegra30
 *		and earlier SoCs have two clock sources i.e. div-clk and
161
 *		fast-clk.
162 163
 * @has_config_load_reg: Has the config load register to load the new
 *		configuration.
164
 * @clk_divisor_hs_mode: Clock divisor in HS mode.
165 166 167 168
 * @clk_divisor_std_mode: Clock divisor in standard mode. It is
 *		applicable if there is no fast clock source i.e. single clock
 *		source.
 * @clk_divisor_fast_mode: Clock divisor in fast mode. It is
169 170
 *		applicable if there is no fast clock source i.e. single clock
 *		source.
171 172 173 174 175 176 177 178 179 180
 * @clk_divisor_fast_plus_mode: Clock divisor in fast mode plus. It is
 *		applicable if there is no fast clock source (i.e. single
 *		clock source).
 * @has_multi_master_mode: The I2C controller supports running in single-master
 *		or multi-master mode.
 * @has_slcg_override_reg: The I2C controller supports a register that
 *		overrides the second level clock gating.
 * @has_mst_fifo: The I2C controller contains the new MST FIFO interface that
 *		provides additional features and allows for longer messages to
 *		be transferred in one go.
181 182
 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
 *		allowing 0 length transfers.
183 184
 * @supports_bus_clear: Bus Clear support to recover from bus hang during
 *		SDA stuck low from device for some unknown reasons.
185
 * @has_apb_dma: Support of APBDMA on corresponding Tegra chip.
186 187 188 189 190 191 192 193 194 195 196 197
 * @tlow_std_mode: Low period of the clock in standard mode.
 * @thigh_std_mode: High period of the clock in standard mode.
 * @tlow_fast_fastplus_mode: Low period of the clock in fast/fast-plus modes.
 * @thigh_fast_fastplus_mode: High period of the clock in fast/fast-plus modes.
 * @setup_hold_time_std_mode: Setup and hold time for start and stop conditions
 *		in standard mode.
 * @setup_hold_time_fast_fast_plus_mode: Setup and hold time for start and stop
 *		conditions in fast/fast-plus modes.
 * @setup_hold_time_hs_mode: Setup and hold time for start and stop conditions
 *		in HS mode.
 * @has_interface_timing_reg: Has interface timing register to program the tuned
 *		timing settings.
198 199 200
 */
struct tegra_i2c_hw_feature {
	bool has_continue_xfer_support;
201 202
	bool has_per_pkt_xfer_complete_irq;
	bool has_single_clk_source;
203
	bool has_config_load_reg;
204
	int clk_divisor_hs_mode;
205 206
	int clk_divisor_std_mode;
	int clk_divisor_fast_mode;
207
	u16 clk_divisor_fast_plus_mode;
208 209
	bool has_multi_master_mode;
	bool has_slcg_override_reg;
210
	bool has_mst_fifo;
211
	const struct i2c_adapter_quirks *quirks;
212
	bool supports_bus_clear;
213
	bool has_apb_dma;
214 215 216 217 218 219 220 221
	u8 tlow_std_mode;
	u8 thigh_std_mode;
	u8 tlow_fast_fastplus_mode;
	u8 thigh_fast_fastplus_mode;
	u32 setup_hold_time_std_mode;
	u32 setup_hold_time_fast_fast_plus_mode;
	u32 setup_hold_time_hs_mode;
	bool has_interface_timing_reg;
222 223
};

C
Colin Cross 已提交
224
/**
225
 * struct tegra_i2c_dev - per device I2C context
C
Colin Cross 已提交
226
 * @dev: device reference for power management
227 228 229 230
 * @hw: Tegra I2C HW feature
 * @adapter: core I2C layer adapter information
 * @div_clk: clock reference for div clock of I2C controller
 * @fast_clk: clock reference for fast clock of I2C controller
231
 * @rst: reset control for the I2C controller
C
Colin Cross 已提交
232
 * @base: ioremapped registers cookie
233
 * @base_phys: physical base address of the I2C controller
234 235 236
 * @cont_id: I2C controller ID, used for packet header
 * @irq: IRQ number of transfer complete interrupt
 * @is_dvc: identifies the DVC I2C controller, has a different register layout
C
Colin Cross 已提交
237 238 239 240 241
 * @msg_complete: transfer completion notifier
 * @msg_err: error code for completed message
 * @msg_buf: pointer to current message data
 * @msg_buf_remaining: size of unsent data in the message buffer
 * @msg_read: identifies read transfers
242
 * @bus_clk_rate: current I2C bus clock rate
243 244
 * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes
 * @is_multimaster_mode: track if I2C controller is in multi-master mode
245 246 247 248 249 250 251
 * @tx_dma_chan: DMA transmit channel
 * @rx_dma_chan: DMA receive channel
 * @dma_phys: handle to DMA resources
 * @dma_buf: pointer to allocated DMA buffer
 * @dma_buf_size: DMA buffer size
 * @is_curr_dma_xfer: indicates active DMA transfer
 * @dma_complete: DMA completion notifier
252
 * @is_curr_atomic_xfer: indicates active atomic transfer
C
Colin Cross 已提交
253 254 255
 */
struct tegra_i2c_dev {
	struct device *dev;
256
	const struct tegra_i2c_hw_feature *hw;
C
Colin Cross 已提交
257
	struct i2c_adapter adapter;
258 259
	struct clk *div_clk;
	struct clk *fast_clk;
S
Stephen Warren 已提交
260
	struct reset_control *rst;
C
Colin Cross 已提交
261
	void __iomem *base;
262
	phys_addr_t base_phys;
C
Colin Cross 已提交
263 264 265 266 267 268 269 270
	int cont_id;
	int irq;
	int is_dvc;
	struct completion msg_complete;
	int msg_err;
	u8 *msg_buf;
	size_t msg_buf_remaining;
	int msg_read;
271
	u32 bus_clk_rate;
272
	u16 clk_divisor_non_hs_mode;
273
	bool is_multimaster_mode;
274 275 276 277 278 279 280
	struct dma_chan *tx_dma_chan;
	struct dma_chan *rx_dma_chan;
	dma_addr_t dma_phys;
	u32 *dma_buf;
	unsigned int dma_buf_size;
	bool is_curr_dma_xfer;
	struct completion dma_complete;
281
	bool is_curr_atomic_xfer;
C
Colin Cross 已提交
282 283
};

284 285
static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
		       unsigned long reg)
C
Colin Cross 已提交
286
{
287
	writel_relaxed(val, i2c_dev->base + reg);
C
Colin Cross 已提交
288 289 290 291
}

static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
{
292
	return readl_relaxed(i2c_dev->base + reg);
C
Colin Cross 已提交
293 294 295 296 297 298 299
}

/*
 * i2c_writel and i2c_readl will offset the register if necessary to talk
 * to the I2C block inside the DVC block
 */
static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
300
					unsigned long reg)
C
Colin Cross 已提交
301 302 303 304 305 306 307
{
	if (i2c_dev->is_dvc)
		reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
	return reg;
}

static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
308
		       unsigned long reg)
C
Colin Cross 已提交
309
{
310
	writel_relaxed(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
311 312 313

	/* Read back register to make sure that register writes completed */
	if (reg != I2C_TX_FIFO)
314
		readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
C
Colin Cross 已提交
315 316 317 318
}

static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
{
319
	return readl_relaxed(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
C
Colin Cross 已提交
320 321 322
}

static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
323
			unsigned long reg, int len)
C
Colin Cross 已提交
324 325 326 327 328
{
	writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
}

static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
329
		       unsigned long reg, int len)
C
Colin Cross 已提交
330 331 332 333 334 335
{
	readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
}

static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
{
336 337 338
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
C
Colin Cross 已提交
339 340 341 342 343
	i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
}

static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
{
344 345 346
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
C
Colin Cross 已提交
347 348 349
	i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
}

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
static void tegra_i2c_dma_complete(void *args)
{
	struct tegra_i2c_dev *i2c_dev = args;

	complete(&i2c_dev->dma_complete);
}

static int tegra_i2c_dma_submit(struct tegra_i2c_dev *i2c_dev, size_t len)
{
	struct dma_async_tx_descriptor *dma_desc;
	enum dma_transfer_direction dir;
	struct dma_chan *chan;

	dev_dbg(i2c_dev->dev, "starting DMA for length: %zu\n", len);
	reinit_completion(&i2c_dev->dma_complete);
	dir = i2c_dev->msg_read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
	chan = i2c_dev->msg_read ? i2c_dev->rx_dma_chan : i2c_dev->tx_dma_chan;
	dma_desc = dmaengine_prep_slave_single(chan, i2c_dev->dma_phys,
					       len, dir, DMA_PREP_INTERRUPT |
					       DMA_CTRL_ACK);
	if (!dma_desc) {
		dev_err(i2c_dev->dev, "failed to get DMA descriptor\n");
		return -EINVAL;
	}

	dma_desc->callback = tegra_i2c_dma_complete;
	dma_desc->callback_param = i2c_dev;
	dmaengine_submit(dma_desc);
	dma_async_issue_pending(chan);
	return 0;
}

static void tegra_i2c_release_dma(struct tegra_i2c_dev *i2c_dev)
{
	if (i2c_dev->dma_buf) {
		dma_free_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
				  i2c_dev->dma_buf, i2c_dev->dma_phys);
		i2c_dev->dma_buf = NULL;
	}

	if (i2c_dev->tx_dma_chan) {
		dma_release_channel(i2c_dev->tx_dma_chan);
		i2c_dev->tx_dma_chan = NULL;
	}

	if (i2c_dev->rx_dma_chan) {
		dma_release_channel(i2c_dev->rx_dma_chan);
		i2c_dev->rx_dma_chan = NULL;
	}
}

static int tegra_i2c_init_dma(struct tegra_i2c_dev *i2c_dev)
{
	struct dma_chan *chan;
	u32 *dma_buf;
	dma_addr_t dma_phys;
	int err;

408 409 410 411 412 413
	if (!i2c_dev->hw->has_apb_dma)
		return 0;

	if (!IS_ENABLED(CONFIG_TEGRA20_APB_DMA)) {
		dev_dbg(i2c_dev->dev, "Support for APB DMA not enabled!\n");
		return 0;
414 415
	}

416
	chan = dma_request_chan(i2c_dev->dev, "rx");
417 418 419 420 421 422 423
	if (IS_ERR(chan)) {
		err = PTR_ERR(chan);
		goto err_out;
	}

	i2c_dev->rx_dma_chan = chan;

424
	chan = dma_request_chan(i2c_dev->dev, "tx");
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	if (IS_ERR(chan)) {
		err = PTR_ERR(chan);
		goto err_out;
	}

	i2c_dev->tx_dma_chan = chan;

	dma_buf = dma_alloc_coherent(i2c_dev->dev, i2c_dev->dma_buf_size,
				     &dma_phys, GFP_KERNEL | __GFP_NOWARN);
	if (!dma_buf) {
		dev_err(i2c_dev->dev, "failed to allocate the DMA buffer\n");
		err = -ENOMEM;
		goto err_out;
	}

	i2c_dev->dma_buf = dma_buf;
	i2c_dev->dma_phys = dma_phys;
	return 0;

err_out:
	tegra_i2c_release_dma(i2c_dev);
	if (err != -EPROBE_DEFER) {
		dev_err(i2c_dev->dev, "cannot use DMA: %d\n", err);
448
		dev_err(i2c_dev->dev, "falling back to PIO\n");
449 450 451 452 453 454
		return 0;
	}

	return err;
}

C
Colin Cross 已提交
455 456 457
static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
{
	unsigned long timeout = jiffies + HZ;
458 459 460 461 462 463 464 465 466 467 468 469
	unsigned int offset;
	u32 mask, val;

	if (i2c_dev->hw->has_mst_fifo) {
		mask = I2C_MST_FIFO_CONTROL_TX_FLUSH |
		       I2C_MST_FIFO_CONTROL_RX_FLUSH;
		offset = I2C_MST_FIFO_CONTROL;
	} else {
		mask = I2C_FIFO_CONTROL_TX_FLUSH |
		       I2C_FIFO_CONTROL_RX_FLUSH;
		offset = I2C_FIFO_CONTROL;
	}
470

471 472 473
	val = i2c_readl(i2c_dev, offset);
	val |= mask;
	i2c_writel(i2c_dev, val, offset);
C
Colin Cross 已提交
474

475
	while (i2c_readl(i2c_dev, offset) & mask) {
C
Colin Cross 已提交
476 477 478 479
		if (time_after(jiffies, timeout)) {
			dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
			return -ETIMEDOUT;
		}
B
Bitan Biswas 已提交
480
		usleep_range(1000, 2000);
C
Colin Cross 已提交
481 482 483 484 485 486 487 488 489 490 491 492
	}
	return 0;
}

static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
{
	u32 val;
	int rx_fifo_avail;
	u8 *buf = i2c_dev->msg_buf;
	size_t buf_remaining = i2c_dev->msg_buf_remaining;
	int words_to_transfer;

B
Bitan Biswas 已提交
493 494 495 496 497 498 499
	/*
	 * Catch overflow due to message fully sent
	 * before the check for RX FIFO availability.
	 */
	if (WARN_ON_ONCE(!(i2c_dev->msg_buf_remaining)))
		return -EINVAL;

500 501 502 503 504 505 506 507 508
	if (i2c_dev->hw->has_mst_fifo) {
		val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
		rx_fifo_avail = (val & I2C_MST_FIFO_STATUS_RX_MASK) >>
			I2C_MST_FIFO_STATUS_RX_SHIFT;
	} else {
		val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
		rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
			I2C_FIFO_STATUS_RX_SHIFT;
	}
C
Colin Cross 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

	/* Rounds down to not include partial word at the end of buf */
	words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
	if (words_to_transfer > rx_fifo_avail)
		words_to_transfer = rx_fifo_avail;

	i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);

	buf += words_to_transfer * BYTES_PER_FIFO_WORD;
	buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
	rx_fifo_avail -= words_to_transfer;

	/*
	 * If there is a partial word at the end of buf, handle it manually to
	 * prevent overwriting past the end of buf
	 */
	if (rx_fifo_avail > 0 && buf_remaining > 0) {
B
Bitan Biswas 已提交
526 527 528 529 530
		/*
		 * buf_remaining > 3 check not needed as rx_fifo_avail == 0
		 * when (words_to_transfer was > rx_fifo_avail) earlier
		 * in this function.
		 */
C
Colin Cross 已提交
531
		val = i2c_readl(i2c_dev, I2C_RX_FIFO);
532
		val = cpu_to_le32(val);
C
Colin Cross 已提交
533 534 535 536 537
		memcpy(buf, &val, buf_remaining);
		buf_remaining = 0;
		rx_fifo_avail--;
	}

B
Bitan Biswas 已提交
538 539 540 541
	/* RX FIFO must be drained, otherwise it's an Overflow case. */
	if (WARN_ON_ONCE(rx_fifo_avail))
		return -EINVAL;

C
Colin Cross 已提交
542 543
	i2c_dev->msg_buf_remaining = buf_remaining;
	i2c_dev->msg_buf = buf;
544

C
Colin Cross 已提交
545 546 547 548 549 550 551 552 553 554 555
	return 0;
}

static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
{
	u32 val;
	int tx_fifo_avail;
	u8 *buf = i2c_dev->msg_buf;
	size_t buf_remaining = i2c_dev->msg_buf_remaining;
	int words_to_transfer;

556 557 558 559 560 561 562 563 564
	if (i2c_dev->hw->has_mst_fifo) {
		val = i2c_readl(i2c_dev, I2C_MST_FIFO_STATUS);
		tx_fifo_avail = (val & I2C_MST_FIFO_STATUS_TX_MASK) >>
			I2C_MST_FIFO_STATUS_TX_SHIFT;
	} else {
		val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
		tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
			I2C_FIFO_STATUS_TX_SHIFT;
	}
C
Colin Cross 已提交
565 566 567 568

	/* Rounds down to not include partial word at the end of buf */
	words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	/* It's very common to have < 4 bytes, so optimize that case. */
	if (words_to_transfer) {
		if (words_to_transfer > tx_fifo_avail)
			words_to_transfer = tx_fifo_avail;

		/*
		 * Update state before writing to FIFO.  If this casues us
		 * to finish writing all bytes (AKA buf_remaining goes to 0) we
		 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
		 * not maskable).  We need to make sure that the isr sees
		 * buf_remaining as 0 and doesn't call us back re-entrantly.
		 */
		buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
		tx_fifo_avail -= words_to_transfer;
		i2c_dev->msg_buf_remaining = buf_remaining;
		i2c_dev->msg_buf = buf +
			words_to_transfer * BYTES_PER_FIFO_WORD;
		barrier();

		i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);

		buf += words_to_transfer * BYTES_PER_FIFO_WORD;
	}
C
Colin Cross 已提交
592 593 594 595 596 597 598

	/*
	 * If there is a partial word at the end of buf, handle it manually to
	 * prevent reading past the end of buf, which could cross a page
	 * boundary and fault.
	 */
	if (tx_fifo_avail > 0 && buf_remaining > 0) {
B
Bitan Biswas 已提交
599 600 601 602 603
		/*
		 * buf_remaining > 3 check not needed as tx_fifo_avail == 0
		 * when (words_to_transfer was > tx_fifo_avail) earlier
		 * in this function for non-zero words_to_transfer.
		 */
C
Colin Cross 已提交
604
		memcpy(&val, buf, buf_remaining);
605
		val = le32_to_cpu(val);
606 607 608 609 610 611

		/* Again update before writing to FIFO to make sure isr sees. */
		i2c_dev->msg_buf_remaining = 0;
		i2c_dev->msg_buf = NULL;
		barrier();

C
Colin Cross 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
		i2c_writel(i2c_dev, val, I2C_TX_FIFO);
	}

	return 0;
}

/*
 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
 * block.  This block is identical to the rest of the I2C blocks, except that
 * it only supports master mode, it has registers moved around, and it needs
 * some extra init to get it into I2C mode.  The register moves are handled
 * by i2c_readl and i2c_writel
 */
static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
{
627 628
	u32 val;

C
Colin Cross 已提交
629 630 631 632 633 634 635 636 637 638
	val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
	val |= DVC_CTRL_REG3_SW_PROG;
	val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
	dvc_writel(i2c_dev, val, DVC_CTRL_REG3);

	val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
	val |= DVC_CTRL_REG1_INTR_EN;
	dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
}

639
static int __maybe_unused tegra_i2c_runtime_resume(struct device *dev)
640
{
641
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
642
	int ret;
643

J
Jon Hunter 已提交
644 645 646 647
	ret = pinctrl_pm_select_default_state(i2c_dev->dev);
	if (ret)
		return ret;

648
	if (!i2c_dev->hw->has_single_clk_source) {
649
		ret = clk_enable(i2c_dev->fast_clk);
650 651 652 653 654
		if (ret < 0) {
			dev_err(i2c_dev->dev,
				"Enabling fast clk failed, err %d\n", ret);
			return ret;
		}
655
	}
656

657
	ret = clk_enable(i2c_dev->div_clk);
658 659 660
	if (ret < 0) {
		dev_err(i2c_dev->dev,
			"Enabling div clk failed, err %d\n", ret);
661
		clk_disable(i2c_dev->fast_clk);
662
		return ret;
663
	}
664 665

	return 0;
666 667
}

668
static int __maybe_unused tegra_i2c_runtime_suspend(struct device *dev)
669
{
670 671
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);

672
	clk_disable(i2c_dev->div_clk);
673
	if (!i2c_dev->hw->has_single_clk_source)
674
		clk_disable(i2c_dev->fast_clk);
675

J
Jon Hunter 已提交
676
	return pinctrl_pm_select_idle_state(i2c_dev->dev);
677 678
}

679 680 681 682 683 684 685 686 687 688 689
static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
{
	unsigned long reg_offset;
	void __iomem *addr;
	u32 val;
	int err;

	if (i2c_dev->hw->has_config_load_reg) {
		reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
		addr = i2c_dev->base + reg_offset;
		i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
690 691

		if (i2c_dev->is_curr_atomic_xfer)
692 693 694
			err = readl_relaxed_poll_timeout_atomic(
						addr, val, val == 0, 1000,
						I2C_CONFIG_LOAD_TIMEOUT);
695
		else
696 697 698
			err = readl_relaxed_poll_timeout(
						addr, val, val == 0, 1000,
						I2C_CONFIG_LOAD_TIMEOUT);
699

700 701 702 703 704 705 706 707 708 709
		if (err) {
			dev_warn(i2c_dev->dev,
				 "timeout waiting for config load\n");
			return err;
		}
	}

	return 0;
}

710
static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev, bool clk_reinit)
C
Colin Cross 已提交
711 712
{
	u32 val;
713
	int err;
714
	u32 clk_divisor, clk_multiplier;
715
	u32 tsu_thd;
716
	u8 tlow, thigh;
C
Colin Cross 已提交
717

S
Stephen Warren 已提交
718
	reset_control_assert(i2c_dev->rst);
C
Colin Cross 已提交
719
	udelay(2);
S
Stephen Warren 已提交
720
	reset_control_deassert(i2c_dev->rst);
C
Colin Cross 已提交
721 722 723 724

	if (i2c_dev->is_dvc)
		tegra_dvc_init(i2c_dev);

J
Jay Cheng 已提交
725 726
	val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
		(0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
727 728 729 730

	if (i2c_dev->hw->has_multi_master_mode)
		val |= I2C_CNFG_MULTI_MASTER_MODE;

C
Colin Cross 已提交
731 732
	i2c_writel(i2c_dev, val, I2C_CNFG);
	i2c_writel(i2c_dev, 0, I2C_INT_MASK);
733 734 735

	/* Make sure clock divisor programmed correctly */
	clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
736
	clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
737 738
					I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
	i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
C
Colin Cross 已提交
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
	    i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE) {
		tlow = i2c_dev->hw->tlow_fast_fastplus_mode;
		thigh = i2c_dev->hw->thigh_fast_fastplus_mode;
		tsu_thd = i2c_dev->hw->setup_hold_time_fast_fast_plus_mode;
	} else {
		tlow = i2c_dev->hw->tlow_std_mode;
		thigh = i2c_dev->hw->thigh_std_mode;
		tsu_thd = i2c_dev->hw->setup_hold_time_std_mode;
	}

	if (i2c_dev->hw->has_interface_timing_reg) {
		val = (thigh << I2C_THIGH_SHIFT) | tlow;
		i2c_writel(i2c_dev, val, I2C_INTERFACE_TIMING_0);
	}

	/*
	 * configure setup and hold times only when tsu_thd is non-zero.
	 * otherwise, preserve the chip default values
	 */
	if (i2c_dev->hw->has_interface_timing_reg && tsu_thd)
		i2c_writel(i2c_dev, tsu_thd, I2C_INTERFACE_TIMING_1);

	if (!clk_reinit) {
		clk_multiplier = (tlow + thigh + 2);
		clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
		err = clk_set_rate(i2c_dev->div_clk,
				   i2c_dev->bus_clk_rate * clk_multiplier);
		if (err) {
			dev_err(i2c_dev->dev,
				"failed changing clock rate: %d\n", err);
771
			return err;
772 773 774
		}
	}

775 776
	if (!i2c_dev->is_dvc) {
		u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
777

778 779 780 781
		sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
		i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
		i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
		i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
782 783
	}

784
	err = tegra_i2c_flush_fifos(i2c_dev);
785
	if (err)
786
		return err;
C
Colin Cross 已提交
787

788 789 790
	if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
		i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);

791 792
	err = tegra_i2c_wait_for_config_load(i2c_dev);
	if (err)
793
		return err;
794

795
	return 0;
C
Colin Cross 已提交
796 797
}

798 799 800 801
static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
{
	u32 cnfg;

J
Jon Hunter 已提交
802 803 804 805 806 807 808 809
	/*
	 * NACK interrupt is generated before the I2C controller generates
	 * the STOP condition on the bus. So wait for 2 clock periods
	 * before disabling the controller so that the STOP condition has
	 * been delivered properly.
	 */
	udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));

810 811 812 813 814 815 816
	cnfg = i2c_readl(i2c_dev, I2C_CNFG);
	if (cnfg & I2C_CNFG_PACKET_MODE_EN)
		i2c_writel(i2c_dev, cnfg & ~I2C_CNFG_PACKET_MODE_EN, I2C_CNFG);

	return tegra_i2c_wait_for_config_load(i2c_dev);
}

C
Colin Cross 已提交
817 818 819 820 821 822 823 824 825
static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
{
	u32 status;
	const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
	struct tegra_i2c_dev *i2c_dev = dev_id;

	status = i2c_readl(i2c_dev, I2C_INT_STATUS);

	if (status == 0) {
826 827 828 829 830 831
		dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
			 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
			 i2c_readl(i2c_dev, I2C_STATUS),
			 i2c_readl(i2c_dev, I2C_CNFG));
		i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
		goto err;
C
Colin Cross 已提交
832 833 834
	}

	if (unlikely(status & status_err)) {
835
		tegra_i2c_disable_packet_mode(i2c_dev);
C
Colin Cross 已提交
836 837 838 839 840 841 842
		if (status & I2C_INT_NO_ACK)
			i2c_dev->msg_err |= I2C_ERR_NO_ACK;
		if (status & I2C_INT_ARBITRATION_LOST)
			i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
		goto err;
	}

843 844 845 846 847 848 849
	/*
	 * I2C transfer is terminated during the bus clear so skip
	 * processing the other interrupts.
	 */
	if (i2c_dev->hw->supports_bus_clear && (status & I2C_INT_BUS_CLR_DONE))
		goto err;

850 851
	if (!i2c_dev->is_curr_dma_xfer) {
		if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
B
Bitan Biswas 已提交
852 853 854 855 856 857 858 859 860
			if (tegra_i2c_empty_rx_fifo(i2c_dev)) {
				/*
				 * Overflow error condition: message fully sent,
				 * with no XFER_COMPLETE interrupt but hardware
				 * asks to transfer more.
				 */
				i2c_dev->msg_err |= I2C_ERR_RX_BUFFER_OVERFLOW;
				goto err;
			}
861
		}
C
Colin Cross 已提交
862

863 864 865 866 867 868 869
		if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
			if (i2c_dev->msg_buf_remaining)
				tegra_i2c_fill_tx_fifo(i2c_dev);
			else
				tegra_i2c_mask_irq(i2c_dev,
						   I2C_INT_TX_FIFO_DATA_REQ);
		}
C
Colin Cross 已提交
870 871
	}

872 873 874 875
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);

876 877 878 879 880 881 882
	/*
	 * During message read XFER_COMPLETE interrupt is triggered prior to
	 * DMA completion and during message write XFER_COMPLETE interrupt is
	 * triggered after DMA completion.
	 * PACKETS_XFER_COMPLETE indicates completion of all bytes of transfer.
	 * so forcing msg_buf_remaining to 0 in DMA mode.
	 */
883
	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
884 885
		if (i2c_dev->is_curr_dma_xfer)
			i2c_dev->msg_buf_remaining = 0;
B
Bitan Biswas 已提交
886 887 888 889 890 891 892 893
		/*
		 * Underflow error condition: XFER_COMPLETE before message
		 * fully sent.
		 */
		if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
			i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
			goto err;
		}
C
Colin Cross 已提交
894
		complete(&i2c_dev->msg_complete);
895
	}
896
	goto done;
C
Colin Cross 已提交
897
err:
L
Lucas De Marchi 已提交
898
	/* An error occurred, mask all interrupts */
C
Colin Cross 已提交
899 900 901
	tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
		I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
		I2C_INT_RX_FIFO_DATA_REQ);
902 903
	if (i2c_dev->hw->supports_bus_clear)
		tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
C
Colin Cross 已提交
904
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
905 906
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
907

908 909 910 911 912 913 914 915 916
	if (i2c_dev->is_curr_dma_xfer) {
		if (i2c_dev->msg_read)
			dmaengine_terminate_async(i2c_dev->rx_dma_chan);
		else
			dmaengine_terminate_async(i2c_dev->tx_dma_chan);

		complete(&i2c_dev->dma_complete);
	}

917
	complete(&i2c_dev->msg_complete);
918
done:
C
Colin Cross 已提交
919 920 921
	return IRQ_HANDLED;
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
static void tegra_i2c_config_fifo_trig(struct tegra_i2c_dev *i2c_dev,
				       size_t len)
{
	u32 val, reg;
	u8 dma_burst;
	struct dma_slave_config slv_config = {0};
	struct dma_chan *chan;
	int ret;
	unsigned long reg_offset;

	if (i2c_dev->hw->has_mst_fifo)
		reg = I2C_MST_FIFO_CONTROL;
	else
		reg = I2C_FIFO_CONTROL;

	if (i2c_dev->is_curr_dma_xfer) {
		if (len & 0xF)
			dma_burst = 1;
		else if (len & 0x10)
			dma_burst = 4;
		else
			dma_burst = 8;

		if (i2c_dev->msg_read) {
			chan = i2c_dev->rx_dma_chan;
			reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_RX_FIFO);
			slv_config.src_addr = i2c_dev->base_phys + reg_offset;
			slv_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
			slv_config.src_maxburst = dma_burst;

			if (i2c_dev->hw->has_mst_fifo)
				val = I2C_MST_FIFO_CONTROL_RX_TRIG(dma_burst);
			else
				val = I2C_FIFO_CONTROL_RX_TRIG(dma_burst);
		} else {
			chan = i2c_dev->tx_dma_chan;
			reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_TX_FIFO);
			slv_config.dst_addr = i2c_dev->base_phys + reg_offset;
			slv_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
			slv_config.dst_maxburst = dma_burst;

			if (i2c_dev->hw->has_mst_fifo)
				val = I2C_MST_FIFO_CONTROL_TX_TRIG(dma_burst);
			else
				val = I2C_FIFO_CONTROL_TX_TRIG(dma_burst);
		}

		slv_config.device_fc = true;
		ret = dmaengine_slave_config(chan, &slv_config);
		if (ret < 0) {
			dev_err(i2c_dev->dev, "DMA slave config failed: %d\n",
				ret);
974
			dev_err(i2c_dev->dev, "falling back to PIO\n");
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
			tegra_i2c_release_dma(i2c_dev);
			i2c_dev->is_curr_dma_xfer = false;
		} else {
			goto out;
		}
	}

	if (i2c_dev->hw->has_mst_fifo)
		val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
		      I2C_MST_FIFO_CONTROL_RX_TRIG(1);
	else
		val = I2C_FIFO_CONTROL_TX_TRIG(8) |
		      I2C_FIFO_CONTROL_RX_TRIG(1);
out:
	i2c_writel(i2c_dev, val, reg);
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static unsigned long
tegra_i2c_poll_completion_timeout(struct tegra_i2c_dev *i2c_dev,
				  struct completion *complete,
				  unsigned int timeout_ms)
{
	ktime_t ktime = ktime_get();
	ktime_t ktimeout = ktime_add_ms(ktime, timeout_ms);

	do {
		u32 status = i2c_readl(i2c_dev, I2C_INT_STATUS);

		if (status) {
			tegra_i2c_isr(i2c_dev->irq, i2c_dev);

			if (completion_done(complete)) {
				s64 delta = ktime_ms_delta(ktimeout, ktime);

				return msecs_to_jiffies(delta) ?: 1;
			}
		}

		ktime = ktime_get();

	} while (ktime_before(ktime, ktimeout));

	return 0;
}

1020 1021 1022 1023 1024 1025 1026
static unsigned long
tegra_i2c_wait_completion_timeout(struct tegra_i2c_dev *i2c_dev,
				  struct completion *complete,
				  unsigned int timeout_ms)
{
	unsigned long ret;

1027 1028 1029 1030 1031 1032 1033 1034
	if (i2c_dev->is_curr_atomic_xfer) {
		ret = tegra_i2c_poll_completion_timeout(i2c_dev, complete,
							timeout_ms);
	} else {
		enable_irq(i2c_dev->irq);
		ret = wait_for_completion_timeout(complete,
						  msecs_to_jiffies(timeout_ms));
		disable_irq(i2c_dev->irq);
1035

1036 1037 1038 1039 1040 1041 1042 1043 1044
		/*
		 * There is a chance that completion may happen after IRQ
		 * synchronization, which is done by disable_irq().
		 */
		if (ret == 0 && completion_done(complete)) {
			dev_warn(i2c_dev->dev,
				 "completion done after timeout\n");
			ret = 1;
		}
1045 1046 1047 1048 1049
	}

	return ret;
}

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
static int tegra_i2c_issue_bus_clear(struct i2c_adapter *adap)
{
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
	int err;
	unsigned long time_left;
	u32 reg;

	reinit_completion(&i2c_dev->msg_complete);
	reg = (I2C_BC_SCLK_THRESHOLD << I2C_BC_SCLK_THRESHOLD_SHIFT) |
	      I2C_BC_STOP_COND | I2C_BC_TERMINATE;
	i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
	if (i2c_dev->hw->has_config_load_reg) {
		err = tegra_i2c_wait_for_config_load(i2c_dev);
		if (err)
			return err;
	}

	reg |= I2C_BC_ENABLE;
	i2c_writel(i2c_dev, reg, I2C_BUS_CLEAR_CNFG);
	tegra_i2c_unmask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);

1071 1072
	time_left = tegra_i2c_wait_completion_timeout(
			i2c_dev, &i2c_dev->msg_complete, 50);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	if (time_left == 0) {
		dev_err(i2c_dev->dev, "timed out for bus clear\n");
		return -ETIMEDOUT;
	}

	reg = i2c_readl(i2c_dev, I2C_BUS_CLEAR_STATUS);
	if (!(reg & I2C_BC_STATUS)) {
		dev_err(i2c_dev->dev,
			"un-recovered arbitration lost\n");
		return -EIO;
	}

	return -EAGAIN;
}

C
Colin Cross 已提交
1088
static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
1089 1090
			      struct i2c_msg *msg,
			      enum msg_end_type end_state)
C
Colin Cross 已提交
1091 1092 1093
{
	u32 packet_header;
	u32 int_mask;
1094
	unsigned long time_left;
1095 1096 1097 1098
	size_t xfer_size;
	u32 *buffer = NULL;
	int err = 0;
	bool dma;
1099
	u16 xfer_time = 100;
C
Colin Cross 已提交
1100 1101 1102 1103 1104 1105 1106

	tegra_i2c_flush_fifos(i2c_dev);

	i2c_dev->msg_buf = msg->buf;
	i2c_dev->msg_buf_remaining = msg->len;
	i2c_dev->msg_err = I2C_ERR_NONE;
	i2c_dev->msg_read = (msg->flags & I2C_M_RD);
1107
	reinit_completion(&i2c_dev->msg_complete);
C
Colin Cross 已提交
1108

1109 1110 1111 1112 1113 1114
	if (i2c_dev->msg_read)
		xfer_size = msg->len;
	else
		xfer_size = msg->len + I2C_PACKET_HEADER_SIZE;

	xfer_size = ALIGN(xfer_size, BYTES_PER_FIFO_WORD);
1115
	i2c_dev->is_curr_dma_xfer = (xfer_size > I2C_PIO_MODE_PREFERRED_LEN) &&
1116 1117
				    i2c_dev->dma_buf &&
				    !i2c_dev->is_curr_atomic_xfer;
1118 1119
	tegra_i2c_config_fifo_trig(i2c_dev, xfer_size);
	dma = i2c_dev->is_curr_dma_xfer;
1120 1121 1122 1123 1124 1125
	/*
	 * Transfer time in mSec = Total bits / transfer rate
	 * Total bits = 9 bits per byte (including ACK bit) + Start & stop bits
	 */
	xfer_time += DIV_ROUND_CLOSEST(((xfer_size * 9) + 2) * MSEC_PER_SEC,
					i2c_dev->bus_clk_rate);
1126 1127 1128

	int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
	tegra_i2c_unmask_irq(i2c_dev, int_mask);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	if (dma) {
		if (i2c_dev->msg_read) {
			dma_sync_single_for_device(i2c_dev->dev,
						   i2c_dev->dma_phys,
						   xfer_size,
						   DMA_FROM_DEVICE);
			err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
			if (err < 0) {
				dev_err(i2c_dev->dev,
					"starting RX DMA failed, err %d\n",
					err);
1140
				return err;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
			}

		} else {
			dma_sync_single_for_cpu(i2c_dev->dev,
						i2c_dev->dma_phys,
						xfer_size,
						DMA_TO_DEVICE);
			buffer = i2c_dev->dma_buf;
		}
	}
1151

C
Colin Cross 已提交
1152 1153 1154 1155
	packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
			PACKET_HEADER0_PROTOCOL_I2C |
			(i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
			(1 << PACKET_HEADER0_PACKET_ID_SHIFT);
1156 1157 1158 1159
	if (dma && !i2c_dev->msg_read)
		*buffer++ = packet_header;
	else
		i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
C
Colin Cross 已提交
1160 1161

	packet_header = msg->len - 1;
1162 1163 1164 1165
	if (dma && !i2c_dev->msg_read)
		*buffer++ = packet_header;
	else
		i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
C
Colin Cross 已提交
1166

1167
	packet_header = I2C_HEADER_IE_ENABLE;
1168 1169 1170
	if (end_state == MSG_END_CONTINUE)
		packet_header |= I2C_HEADER_CONTINUE_XFER;
	else if (end_state == MSG_END_REPEAT_START)
1171
		packet_header |= I2C_HEADER_REPEAT_START;
1172 1173
	if (msg->flags & I2C_M_TEN) {
		packet_header |= msg->addr;
C
Colin Cross 已提交
1174
		packet_header |= I2C_HEADER_10BIT_ADDR;
1175 1176 1177
	} else {
		packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
	}
C
Colin Cross 已提交
1178 1179 1180 1181
	if (msg->flags & I2C_M_IGNORE_NAK)
		packet_header |= I2C_HEADER_CONT_ON_NAK;
	if (msg->flags & I2C_M_RD)
		packet_header |= I2C_HEADER_READ;
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	if (dma && !i2c_dev->msg_read)
		*buffer++ = packet_header;
	else
		i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);

	if (!i2c_dev->msg_read) {
		if (dma) {
			memcpy(buffer, msg->buf, msg->len);
			dma_sync_single_for_device(i2c_dev->dev,
						   i2c_dev->dma_phys,
						   xfer_size,
						   DMA_TO_DEVICE);
			err = tegra_i2c_dma_submit(i2c_dev, xfer_size);
			if (err < 0) {
				dev_err(i2c_dev->dev,
					"starting TX DMA failed, err %d\n",
					err);
1199
				return err;
1200 1201 1202 1203 1204
			}
		} else {
			tegra_i2c_fill_tx_fifo(i2c_dev);
		}
	}
C
Colin Cross 已提交
1205

1206 1207
	if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
		int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
1208 1209 1210 1211 1212 1213
	if (!dma) {
		if (msg->flags & I2C_M_RD)
			int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
		else if (i2c_dev->msg_buf_remaining)
			int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
	}
1214

C
Colin Cross 已提交
1215 1216 1217 1218
	tegra_i2c_unmask_irq(i2c_dev, int_mask);
	dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
		i2c_readl(i2c_dev, I2C_INT_MASK));

1219
	if (dma) {
1220 1221
		time_left = tegra_i2c_wait_completion_timeout(
				i2c_dev, &i2c_dev->dma_complete, xfer_time);
1222

1223 1224 1225 1226
		dmaengine_terminate_sync(i2c_dev->msg_read ?
					 i2c_dev->rx_dma_chan :
					 i2c_dev->tx_dma_chan);

1227
		if (!time_left && !completion_done(&i2c_dev->dma_complete)) {
1228
			dev_err(i2c_dev->dev, "DMA transfer timeout\n");
1229
			tegra_i2c_init(i2c_dev, true);
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			return -ETIMEDOUT;
		}

		if (i2c_dev->msg_read && i2c_dev->msg_err == I2C_ERR_NONE) {
			dma_sync_single_for_cpu(i2c_dev->dev,
						i2c_dev->dma_phys,
						xfer_size,
						DMA_FROM_DEVICE);
			memcpy(i2c_dev->msg_buf, i2c_dev->dma_buf,
			       msg->len);
		}
	}

1243 1244 1245
	time_left = tegra_i2c_wait_completion_timeout(
			i2c_dev, &i2c_dev->msg_complete, xfer_time);

C
Colin Cross 已提交
1246 1247
	tegra_i2c_mask_irq(i2c_dev, int_mask);

1248
	if (time_left == 0) {
C
Colin Cross 已提交
1249
		dev_err(i2c_dev->dev, "i2c transfer timed out\n");
1250
		tegra_i2c_init(i2c_dev, true);
C
Colin Cross 已提交
1251 1252 1253
		return -ETIMEDOUT;
	}

1254 1255 1256
	dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
		time_left, completion_done(&i2c_dev->msg_complete),
		i2c_dev->msg_err);
C
Colin Cross 已提交
1257

1258
	i2c_dev->is_curr_dma_xfer = false;
C
Colin Cross 已提交
1259 1260 1261
	if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
		return 0;

1262
	tegra_i2c_init(i2c_dev, true);
1263 1264 1265 1266 1267 1268 1269
	/* start recovery upon arbitration loss in single master mode */
	if (i2c_dev->msg_err == I2C_ERR_ARBITRATION_LOST) {
		if (!i2c_dev->is_multimaster_mode)
			return i2c_recover_bus(&i2c_dev->adapter);
		return -EAGAIN;
	}

C
Colin Cross 已提交
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
		if (msg->flags & I2C_M_IGNORE_NAK)
			return 0;
		return -EREMOTEIO;
	}

	return -EIO;
}

static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
1280
			  int num)
C
Colin Cross 已提交
1281 1282 1283
{
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
	int i;
1284
	int ret;
C
Colin Cross 已提交
1285

1286
	ret = pm_runtime_get_sync(i2c_dev->dev);
1287
	if (ret < 0) {
1288
		dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
1289 1290 1291
		return ret;
	}

C
Colin Cross 已提交
1292
	for (i = 0; i < num; i++) {
1293
		enum msg_end_type end_type = MSG_END_STOP;
1294

1295 1296 1297 1298 1299 1300 1301
		if (i < (num - 1)) {
			if (msgs[i + 1].flags & I2C_M_NOSTART)
				end_type = MSG_END_CONTINUE;
			else
				end_type = MSG_END_REPEAT_START;
		}
		ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
C
Colin Cross 已提交
1302 1303 1304
		if (ret)
			break;
	}
1305 1306 1307

	pm_runtime_put(i2c_dev->dev);

C
Colin Cross 已提交
1308 1309 1310
	return ret ?: i;
}

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
				 struct i2c_msg msgs[], int num)
{
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
	int ret;

	i2c_dev->is_curr_atomic_xfer = true;
	ret = tegra_i2c_xfer(adap, msgs, num);
	i2c_dev->is_curr_atomic_xfer = false;

	return ret;
}

C
Colin Cross 已提交
1324 1325
static u32 tegra_i2c_func(struct i2c_adapter *adap)
{
1326
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
1327 1328
	u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
		  I2C_FUNC_10BIT_ADDR |	I2C_FUNC_PROTOCOL_MANGLING;
1329 1330 1331 1332

	if (i2c_dev->hw->has_continue_xfer_support)
		ret |= I2C_FUNC_NOSTART;
	return ret;
C
Colin Cross 已提交
1333 1334
}

1335 1336 1337 1338
static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
{
	struct device_node *np = i2c_dev->dev->of_node;
	int ret;
1339
	bool multi_mode;
1340 1341

	ret = of_property_read_u32(np, "clock-frequency",
1342
				   &i2c_dev->bus_clk_rate);
1343 1344 1345
	if (ret)
		i2c_dev->bus_clk_rate = 100000; /* default clock rate */

1346 1347
	multi_mode = of_property_read_bool(np, "multi-master");
	i2c_dev->is_multimaster_mode = multi_mode;
1348 1349
}

C
Colin Cross 已提交
1350
static const struct i2c_algorithm tegra_i2c_algo = {
1351 1352 1353
	.master_xfer		= tegra_i2c_xfer,
	.master_xfer_atomic	= tegra_i2c_xfer_atomic,
	.functionality		= tegra_i2c_func,
C
Colin Cross 已提交
1354 1355
};

1356
/* payload size is only 12 bit */
1357
static const struct i2c_adapter_quirks tegra_i2c_quirks = {
1358
	.flags = I2C_AQ_NO_ZERO_LEN,
1359 1360
	.max_read_len = SZ_4K,
	.max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
1361 1362
};

1363 1364
static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
	.flags = I2C_AQ_NO_ZERO_LEN,
1365
	.max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
1366 1367
};

1368 1369 1370 1371
static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
	.recover_bus = tegra_i2c_issue_bus_clear,
};

1372 1373
static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
	.has_continue_xfer_support = false,
1374 1375 1376
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
1377 1378
	.clk_divisor_std_mode = 0,
	.clk_divisor_fast_mode = 0,
1379
	.clk_divisor_fast_plus_mode = 0,
1380
	.has_config_load_reg = false,
1381 1382
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
1383
	.has_mst_fifo = false,
1384
	.quirks = &tegra_i2c_quirks,
1385
	.supports_bus_clear = false,
1386
	.has_apb_dma = true,
1387 1388 1389 1390 1391 1392 1393 1394
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x2,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0x0,
	.setup_hold_time_fast_fast_plus_mode = 0x0,
	.setup_hold_time_hs_mode = 0x0,
	.has_interface_timing_reg = false,
1395 1396 1397 1398
};

static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
	.has_continue_xfer_support = true,
1399 1400 1401
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
1402 1403
	.clk_divisor_std_mode = 0,
	.clk_divisor_fast_mode = 0,
1404
	.clk_divisor_fast_plus_mode = 0,
1405
	.has_config_load_reg = false,
1406 1407
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
1408
	.has_mst_fifo = false,
1409
	.quirks = &tegra_i2c_quirks,
1410
	.supports_bus_clear = false,
1411
	.has_apb_dma = true,
1412 1413 1414 1415 1416 1417 1418 1419
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x2,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0x0,
	.setup_hold_time_fast_fast_plus_mode = 0x0,
	.setup_hold_time_hs_mode = 0x0,
	.has_interface_timing_reg = false,
1420 1421 1422 1423 1424 1425 1426
};

static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
	.has_continue_xfer_support = true,
	.has_per_pkt_xfer_complete_irq = true,
	.has_single_clk_source = true,
	.clk_divisor_hs_mode = 1,
1427 1428
	.clk_divisor_std_mode = 0x19,
	.clk_divisor_fast_mode = 0x19,
1429
	.clk_divisor_fast_plus_mode = 0x10,
1430
	.has_config_load_reg = false,
1431 1432
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
1433
	.has_mst_fifo = false,
1434
	.quirks = &tegra_i2c_quirks,
1435
	.supports_bus_clear = true,
1436
	.has_apb_dma = true,
1437 1438 1439 1440 1441 1442 1443 1444
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x2,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0x0,
	.setup_hold_time_fast_fast_plus_mode = 0x0,
	.setup_hold_time_hs_mode = 0x0,
	.has_interface_timing_reg = false,
1445 1446 1447 1448 1449 1450 1451
};

static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
	.has_continue_xfer_support = true,
	.has_per_pkt_xfer_complete_irq = true,
	.has_single_clk_source = true,
	.clk_divisor_hs_mode = 1,
1452 1453
	.clk_divisor_std_mode = 0x19,
	.clk_divisor_fast_mode = 0x19,
1454
	.clk_divisor_fast_plus_mode = 0x10,
1455
	.has_config_load_reg = true,
1456 1457
	.has_multi_master_mode = false,
	.has_slcg_override_reg = true,
1458
	.has_mst_fifo = false,
1459
	.quirks = &tegra_i2c_quirks,
1460
	.supports_bus_clear = true,
1461
	.has_apb_dma = true,
1462 1463 1464 1465 1466 1467 1468 1469
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x2,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0x0,
	.setup_hold_time_fast_fast_plus_mode = 0x0,
	.setup_hold_time_hs_mode = 0x0,
	.has_interface_timing_reg = true,
1470 1471 1472 1473 1474 1475 1476
};

static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
	.has_continue_xfer_support = true,
	.has_per_pkt_xfer_complete_irq = true,
	.has_single_clk_source = true,
	.clk_divisor_hs_mode = 1,
1477 1478
	.clk_divisor_std_mode = 0x19,
	.clk_divisor_fast_mode = 0x19,
1479 1480
	.clk_divisor_fast_plus_mode = 0x10,
	.has_config_load_reg = true,
1481
	.has_multi_master_mode = false,
1482
	.has_slcg_override_reg = true,
1483
	.has_mst_fifo = false,
1484
	.quirks = &tegra_i2c_quirks,
1485
	.supports_bus_clear = true,
1486
	.has_apb_dma = true,
1487 1488 1489 1490 1491 1492 1493 1494
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x2,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0,
	.setup_hold_time_fast_fast_plus_mode = 0,
	.setup_hold_time_hs_mode = 0,
	.has_interface_timing_reg = true,
1495 1496 1497 1498 1499 1500 1501
};

static const struct tegra_i2c_hw_feature tegra186_i2c_hw = {
	.has_continue_xfer_support = true,
	.has_per_pkt_xfer_complete_irq = true,
	.has_single_clk_source = true,
	.clk_divisor_hs_mode = 1,
1502 1503
	.clk_divisor_std_mode = 0x16,
	.clk_divisor_fast_mode = 0x19,
1504 1505
	.clk_divisor_fast_plus_mode = 0x10,
	.has_config_load_reg = true,
1506
	.has_multi_master_mode = false,
1507
	.has_slcg_override_reg = true,
1508
	.has_mst_fifo = false,
1509 1510 1511
	.quirks = &tegra_i2c_quirks,
	.supports_bus_clear = true,
	.has_apb_dma = false,
1512 1513 1514 1515 1516 1517 1518 1519
	.tlow_std_mode = 0x4,
	.thigh_std_mode = 0x3,
	.tlow_fast_fastplus_mode = 0x4,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0,
	.setup_hold_time_fast_fast_plus_mode = 0,
	.setup_hold_time_hs_mode = 0,
	.has_interface_timing_reg = true,
1520 1521 1522 1523 1524 1525 1526
};

static const struct tegra_i2c_hw_feature tegra194_i2c_hw = {
	.has_continue_xfer_support = true,
	.has_per_pkt_xfer_complete_irq = true,
	.has_single_clk_source = true,
	.clk_divisor_hs_mode = 1,
1527 1528 1529
	.clk_divisor_std_mode = 0x4f,
	.clk_divisor_fast_mode = 0x3c,
	.clk_divisor_fast_plus_mode = 0x16,
1530 1531 1532 1533
	.has_config_load_reg = true,
	.has_multi_master_mode = true,
	.has_slcg_override_reg = true,
	.has_mst_fifo = true,
1534
	.quirks = &tegra194_i2c_quirks,
1535
	.supports_bus_clear = true,
1536
	.has_apb_dma = false,
1537 1538 1539 1540 1541 1542 1543 1544
	.tlow_std_mode = 0x8,
	.thigh_std_mode = 0x7,
	.tlow_fast_fastplus_mode = 0x2,
	.thigh_fast_fastplus_mode = 0x2,
	.setup_hold_time_std_mode = 0x08080808,
	.setup_hold_time_fast_fast_plus_mode = 0x02020202,
	.setup_hold_time_hs_mode = 0x090909,
	.has_interface_timing_reg = true,
1545 1546 1547
};

/* Match table for of_platform binding */
1548
static const struct of_device_id tegra_i2c_of_match[] = {
1549
	{ .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
1550
	{ .compatible = "nvidia,tegra186-i2c", .data = &tegra186_i2c_hw, },
1551
	{ .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
1552
	{ .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
1553
	{ .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
1554 1555 1556 1557 1558 1559 1560
	{ .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
	{ .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
	{ .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
	{},
};
MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);

1561
static int tegra_i2c_probe(struct platform_device *pdev)
C
Colin Cross 已提交
1562 1563 1564
{
	struct tegra_i2c_dev *i2c_dev;
	struct resource *res;
1565 1566
	struct clk *div_clk;
	struct clk *fast_clk;
O
Olof Johansson 已提交
1567
	void __iomem *base;
1568
	phys_addr_t base_phys;
C
Colin Cross 已提交
1569
	int irq;
1570
	int ret;
C
Colin Cross 已提交
1571 1572

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1573
	base_phys = res->start;
1574 1575 1576
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);
C
Colin Cross 已提交
1577 1578 1579 1580

	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res) {
		dev_err(&pdev->dev, "no irq resource\n");
1581
		return -EINVAL;
C
Colin Cross 已提交
1582 1583 1584
	}
	irq = res->start;

1585 1586
	div_clk = devm_clk_get(&pdev->dev, "div-clk");
	if (IS_ERR(div_clk)) {
1587 1588 1589
		if (PTR_ERR(div_clk) != -EPROBE_DEFER)
			dev_err(&pdev->dev, "missing controller clock\n");

1590
		return PTR_ERR(div_clk);
C
Colin Cross 已提交
1591 1592
	}

1593
	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1594
	if (!i2c_dev)
1595
		return -ENOMEM;
C
Colin Cross 已提交
1596 1597

	i2c_dev->base = base;
1598
	i2c_dev->base_phys = base_phys;
1599
	i2c_dev->div_clk = div_clk;
C
Colin Cross 已提交
1600
	i2c_dev->adapter.algo = &tegra_i2c_algo;
1601
	i2c_dev->adapter.retries = 1;
1602
	i2c_dev->adapter.timeout = 6 * HZ;
C
Colin Cross 已提交
1603 1604 1605
	i2c_dev->irq = irq;
	i2c_dev->cont_id = pdev->id;
	i2c_dev->dev = &pdev->dev;
1606

1607
	i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
S
Stephen Warren 已提交
1608
	if (IS_ERR(i2c_dev->rst)) {
1609
		dev_err(&pdev->dev, "missing controller reset\n");
S
Stephen Warren 已提交
1610 1611 1612
		return PTR_ERR(i2c_dev->rst);
	}

1613
	tegra_i2c_parse_dt(i2c_dev);
C
Colin Cross 已提交
1614

1615 1616 1617
	i2c_dev->hw = of_device_get_match_data(&pdev->dev);
	i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
						  "nvidia,tegra20-i2c-dvc");
1618
	i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
1619 1620
	i2c_dev->dma_buf_size = i2c_dev->adapter.quirks->max_write_len +
				I2C_PACKET_HEADER_SIZE;
C
Colin Cross 已提交
1621
	init_completion(&i2c_dev->msg_complete);
1622
	init_completion(&i2c_dev->dma_complete);
C
Colin Cross 已提交
1623

1624 1625 1626
	if (!i2c_dev->hw->has_single_clk_source) {
		fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
		if (IS_ERR(fast_clk)) {
1627
			dev_err(&pdev->dev, "missing fast clock\n");
1628 1629 1630 1631 1632
			return PTR_ERR(fast_clk);
		}
		i2c_dev->fast_clk = fast_clk;
	}

C
Colin Cross 已提交
1633 1634
	platform_set_drvdata(pdev, i2c_dev);

1635 1636 1637 1638 1639 1640 1641 1642
	if (!i2c_dev->hw->has_single_clk_source) {
		ret = clk_prepare(i2c_dev->fast_clk);
		if (ret < 0) {
			dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
			return ret;
		}
	}

1643 1644
	if (i2c_dev->bus_clk_rate > I2C_FAST_MODE &&
	    i2c_dev->bus_clk_rate <= I2C_FAST_PLUS_MODE)
1645
		i2c_dev->clk_divisor_non_hs_mode =
1646 1647 1648 1649 1650 1651 1652 1653
				i2c_dev->hw->clk_divisor_fast_plus_mode;
	else if (i2c_dev->bus_clk_rate > I2C_STANDARD_MODE &&
		 i2c_dev->bus_clk_rate <= I2C_FAST_MODE)
		i2c_dev->clk_divisor_non_hs_mode =
				i2c_dev->hw->clk_divisor_fast_mode;
	else
		i2c_dev->clk_divisor_non_hs_mode =
				i2c_dev->hw->clk_divisor_std_mode;
1654 1655 1656 1657 1658 1659 1660

	ret = clk_prepare(i2c_dev->div_clk);
	if (ret < 0) {
		dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
		goto unprepare_fast_clk;
	}

1661
	pm_runtime_irq_safe(&pdev->dev);
1662
	pm_runtime_enable(&pdev->dev);
1663
	if (!pm_runtime_enabled(&pdev->dev)) {
1664
		ret = tegra_i2c_runtime_resume(&pdev->dev);
1665 1666 1667 1668 1669
		if (ret < 0) {
			dev_err(&pdev->dev, "runtime resume failed\n");
			goto unprepare_div_clk;
		}
	} else {
1670
		ret = pm_runtime_get_sync(i2c_dev->dev);
1671 1672 1673 1674
		if (ret < 0) {
			dev_err(&pdev->dev, "runtime resume failed\n");
			goto disable_rpm;
		}
1675 1676
	}

1677 1678 1679 1680 1681
	if (i2c_dev->is_multimaster_mode) {
		ret = clk_enable(i2c_dev->div_clk);
		if (ret < 0) {
			dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
				ret);
1682
			goto put_rpm;
1683 1684 1685
		}
	}

1686 1687 1688
	if (i2c_dev->hw->supports_bus_clear)
		i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;

1689 1690 1691 1692
	ret = tegra_i2c_init_dma(i2c_dev);
	if (ret < 0)
		goto disable_div_clk;

1693
	ret = tegra_i2c_init(i2c_dev, false);
C
Colin Cross 已提交
1694
	if (ret) {
1695
		dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
1696
		goto release_dma;
C
Colin Cross 已提交
1697 1698
	}

1699 1700
	irq_set_status_flags(i2c_dev->irq, IRQ_NOAUTOEN);

1701
	ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
1702
			       tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
C
Colin Cross 已提交
1703 1704
	if (ret) {
		dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
1705
		goto release_dma;
C
Colin Cross 已提交
1706 1707 1708 1709
	}

	i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
	i2c_dev->adapter.owner = THIS_MODULE;
1710
	i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
1711
	strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
C
Colin Cross 已提交
1712 1713 1714
		sizeof(i2c_dev->adapter.name));
	i2c_dev->adapter.dev.parent = &pdev->dev;
	i2c_dev->adapter.nr = pdev->id;
1715
	i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
C
Colin Cross 已提交
1716 1717

	ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
1718
	if (ret)
1719
		goto release_dma;
C
Colin Cross 已提交
1720

1721 1722
	pm_runtime_put(&pdev->dev);

C
Colin Cross 已提交
1723
	return 0;
1724

1725 1726 1727
release_dma:
	tegra_i2c_release_dma(i2c_dev);

1728 1729 1730 1731
disable_div_clk:
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

1732 1733 1734 1735
put_rpm:
	if (pm_runtime_enabled(&pdev->dev))
		pm_runtime_put_sync(&pdev->dev);
	else
1736 1737
		tegra_i2c_runtime_suspend(&pdev->dev);

1738 1739 1740 1741
disable_rpm:
	if (pm_runtime_enabled(&pdev->dev))
		pm_runtime_disable(&pdev->dev);

1742 1743 1744 1745 1746 1747 1748 1749
unprepare_div_clk:
	clk_unprepare(i2c_dev->div_clk);

unprepare_fast_clk:
	if (!i2c_dev->hw->has_single_clk_source)
		clk_unprepare(i2c_dev->fast_clk);

	return ret;
C
Colin Cross 已提交
1750 1751
}

1752
static int tegra_i2c_remove(struct platform_device *pdev)
C
Colin Cross 已提交
1753 1754
{
	struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1755

C
Colin Cross 已提交
1756
	i2c_del_adapter(&i2c_dev->adapter);
1757

1758 1759 1760
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

1761 1762 1763 1764
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		tegra_i2c_runtime_suspend(&pdev->dev);

1765 1766 1767 1768
	clk_unprepare(i2c_dev->div_clk);
	if (!i2c_dev->hw->has_single_clk_source)
		clk_unprepare(i2c_dev->fast_clk);

1769
	tegra_i2c_release_dma(i2c_dev);
C
Colin Cross 已提交
1770 1771 1772
	return 0;
}

1773
static int __maybe_unused tegra_i2c_suspend(struct device *dev)
1774 1775
{
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1776
	int err;
1777 1778 1779

	i2c_mark_adapter_suspended(&i2c_dev->adapter);

1780 1781 1782 1783
	err = pm_runtime_force_suspend(dev);
	if (err < 0)
		return err;

1784 1785 1786
	return 0;
}

1787
static int __maybe_unused tegra_i2c_resume(struct device *dev)
1788 1789 1790 1791
{
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
	int err;

1792 1793 1794 1795
	err = tegra_i2c_runtime_resume(dev);
	if (err)
		return err;

1796 1797 1798 1799
	err = tegra_i2c_init(i2c_dev, false);
	if (err)
		return err;

1800 1801 1802 1803
	err = tegra_i2c_runtime_suspend(dev);
	if (err)
		return err;

1804 1805 1806 1807
	err = pm_runtime_force_resume(dev);
	if (err < 0)
		return err;

1808 1809 1810 1811 1812
	i2c_mark_adapter_resumed(&i2c_dev->adapter);

	return 0;
}

1813
static const struct dev_pm_ops tegra_i2c_pm = {
1814
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1815 1816 1817
	SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
			   NULL)
};
1818

C
Colin Cross 已提交
1819 1820
static struct platform_driver tegra_i2c_driver = {
	.probe   = tegra_i2c_probe,
1821
	.remove  = tegra_i2c_remove,
C
Colin Cross 已提交
1822 1823
	.driver  = {
		.name  = "tegra-i2c",
1824
		.of_match_table = tegra_i2c_of_match,
1825
		.pm    = &tegra_i2c_pm,
C
Colin Cross 已提交
1826 1827 1828
	},
};

1829
module_platform_driver(tegra_i2c_driver);
C
Colin Cross 已提交
1830 1831 1832 1833

MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
MODULE_AUTHOR("Colin Cross");
MODULE_LICENSE("GPL v2");