i2c-tegra.c 34.6 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>
C
Colin Cross 已提交
11 12
#include <linux/err.h>
#include <linux/i2c.h>
13
#include <linux/init.h>
C
Colin Cross 已提交
14
#include <linux/interrupt.h>
15 16 17
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
18
#include <linux/module.h>
19
#include <linux/of_device.h>
J
Jon Hunter 已提交
20
#include <linux/pinctrl/consumer.h>
21
#include <linux/platform_device.h>
22
#include <linux/pm_runtime.h>
23
#include <linux/reset.h>
C
Colin Cross 已提交
24 25 26 27 28

#define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
#define BYTES_PER_FIFO_WORD 4

#define I2C_CNFG				0x000
J
Jay Cheng 已提交
29
#define I2C_CNFG_DEBOUNCE_CNT_SHIFT		12
J
Jon Hunter 已提交
30 31 32
#define I2C_CNFG_PACKET_MODE_EN			BIT(10)
#define I2C_CNFG_NEW_MASTER_FSM			BIT(11)
#define I2C_CNFG_MULTI_MASTER_MODE		BIT(17)
33
#define I2C_STATUS				0x01C
C
Colin Cross 已提交
34
#define I2C_SL_CNFG				0x020
J
Jon Hunter 已提交
35 36
#define I2C_SL_CNFG_NACK			BIT(1)
#define I2C_SL_CNFG_NEWSL			BIT(2)
C
Colin Cross 已提交
37
#define I2C_SL_ADDR1				0x02c
38
#define I2C_SL_ADDR2				0x030
C
Colin Cross 已提交
39 40 41 42
#define I2C_TX_FIFO				0x050
#define I2C_RX_FIFO				0x054
#define I2C_PACKET_TRANSFER_STATUS		0x058
#define I2C_FIFO_CONTROL			0x05c
J
Jon Hunter 已提交
43 44
#define I2C_FIFO_CONTROL_TX_FLUSH		BIT(1)
#define I2C_FIFO_CONTROL_RX_FLUSH		BIT(0)
C
Colin Cross 已提交
45 46 47 48 49 50 51 52 53
#define I2C_FIFO_CONTROL_TX_TRIG_SHIFT		5
#define I2C_FIFO_CONTROL_RX_TRIG_SHIFT		2
#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
54
#define I2C_INT_BUS_CLR_DONE			BIT(11)
J
Jon Hunter 已提交
55 56 57 58 59 60 61 62
#define I2C_INT_PACKET_XFER_COMPLETE		BIT(7)
#define I2C_INT_ALL_PACKETS_XFER_COMPLETE	BIT(6)
#define I2C_INT_TX_FIFO_OVERFLOW		BIT(5)
#define I2C_INT_RX_FIFO_UNDERFLOW		BIT(4)
#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 65
#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT	16
#define I2C_CLK_MULTIPLIER_STD_FAST_MODE	8
C
Colin Cross 已提交
66 67

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

#define I2C_ERR_NONE				0x00
#define I2C_ERR_NO_ACK				0x01
#define I2C_ERR_ARBITRATION_LOST		0x02
79
#define I2C_ERR_UNKNOWN_INTERRUPT		0x04
C
Colin Cross 已提交
80 81 82 83

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

J
Jon Hunter 已提交
86 87 88 89 90 91 92 93
#define I2C_HEADER_HIGHSPEED_MODE		BIT(22)
#define I2C_HEADER_CONT_ON_NAK			BIT(21)
#define I2C_HEADER_SEND_START_BYTE		BIT(20)
#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 已提交
94 95
#define I2C_HEADER_MASTER_ADDR_SHIFT		12
#define I2C_HEADER_SLAVE_ADDR_SHIFT		1
96

97 98 99 100 101 102 103 104 105
#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)

106
#define I2C_CONFIG_LOAD				0x08C
J
Jon Hunter 已提交
107 108 109
#define I2C_MSTR_CONFIG_LOAD			BIT(0)
#define I2C_SLV_CONFIG_LOAD			BIT(1)
#define I2C_TIMEOUT_CONFIG_LOAD			BIT(2)
110

111
#define I2C_CLKEN_OVERRIDE			0x090
J
Jon Hunter 已提交
112
#define I2C_MST_CORE_CLKEN_OVR			BIT(0)
113

114 115
#define I2C_CONFIG_LOAD_TIMEOUT			1000000

116 117 118 119 120 121 122 123 124 125 126 127
#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

128 129 130
/* Packet header size in bytes */
#define I2C_PACKET_HEADER_SIZE			12

131 132 133 134 135 136 137 138 139 140 141 142
/*
 * 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 已提交
143

144 145 146
/**
 * struct tegra_i2c_hw_feature : Different HW support on Tegra
 * @has_continue_xfer_support: Continue transfer supports.
147 148
 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
 *		complete interrupt per packet basis.
149 150
 * @has_single_clk_source: The I2C controller has single clock source. Tegra30
 *		and earlier SoCs have two clock sources i.e. div-clk and
151
 *		fast-clk.
152 153
 * @has_config_load_reg: Has the config load register to load the new
 *		configuration.
154 155 156 157
 * @clk_divisor_hs_mode: Clock divisor in HS mode.
 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
 *		applicable if there is no fast clock source i.e. single clock
 *		source.
158 159 160 161 162 163 164 165 166 167
 * @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.
168 169
 * @quirks: i2c adapter quirks for limiting write/read transfer size and not
 *		allowing 0 length transfers.
170 171
 * @supports_bus_clear: Bus Clear support to recover from bus hang during
 *		SDA stuck low from device for some unknown reasons.
172 173 174
 */
struct tegra_i2c_hw_feature {
	bool has_continue_xfer_support;
175 176
	bool has_per_pkt_xfer_complete_irq;
	bool has_single_clk_source;
177
	bool has_config_load_reg;
178 179
	int clk_divisor_hs_mode;
	int clk_divisor_std_fast_mode;
180
	u16 clk_divisor_fast_plus_mode;
181 182
	bool has_multi_master_mode;
	bool has_slcg_override_reg;
183
	bool has_mst_fifo;
184
	const struct i2c_adapter_quirks *quirks;
185
	bool supports_bus_clear;
186 187
};

C
Colin Cross 已提交
188
/**
189
 * struct tegra_i2c_dev - per device I2C context
C
Colin Cross 已提交
190
 * @dev: device reference for power management
191 192 193 194
 * @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
195
 * @rst: reset control for the I2C controller
C
Colin Cross 已提交
196
 * @base: ioremapped registers cookie
197 198
 * @cont_id: I2C controller ID, used for packet header
 * @irq: IRQ number of transfer complete interrupt
199
 * @irq_disabled: used to track whether or not the interrupt is enabled
200
 * @is_dvc: identifies the DVC I2C controller, has a different register layout
C
Colin Cross 已提交
201 202 203 204 205
 * @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
206
 * @bus_clk_rate: current I2C bus clock rate
207 208 209
 * @clk_divisor_non_hs_mode: clock divider for non-high-speed modes
 * @is_multimaster_mode: track if I2C controller is in multi-master mode
 * @xfer_lock: lock to serialize transfer submission and processing
C
Colin Cross 已提交
210 211 212
 */
struct tegra_i2c_dev {
	struct device *dev;
213
	const struct tegra_i2c_hw_feature *hw;
C
Colin Cross 已提交
214
	struct i2c_adapter adapter;
215 216
	struct clk *div_clk;
	struct clk *fast_clk;
S
Stephen Warren 已提交
217
	struct reset_control *rst;
C
Colin Cross 已提交
218 219 220
	void __iomem *base;
	int cont_id;
	int irq;
221
	bool irq_disabled;
C
Colin Cross 已提交
222 223 224 225 226 227
	int is_dvc;
	struct completion msg_complete;
	int msg_err;
	u8 *msg_buf;
	size_t msg_buf_remaining;
	int msg_read;
228
	u32 bus_clk_rate;
229
	u16 clk_divisor_non_hs_mode;
230
	bool is_multimaster_mode;
231
	spinlock_t xfer_lock;
C
Colin Cross 已提交
232 233
};

234 235
static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
		       unsigned long reg)
C
Colin Cross 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
{
	writel(val, i2c_dev->base + reg);
}

static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
{
	return readl(i2c_dev->base + reg);
}

/*
 * 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,
	unsigned long reg)
{
	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,
	unsigned long reg)
{
	writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
261 262 263 264

	/* Read back register to make sure that register writes completed */
	if (reg != I2C_TX_FIFO)
		readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
C
Colin Cross 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
{
	return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
}

static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
	unsigned long reg, int len)
{
	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,
	unsigned long reg, int len)
{
	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)
{
286 287 288
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
C
Colin Cross 已提交
289 290 291 292 293
	i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
}

static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
{
294 295 296
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
C
Colin Cross 已提交
297 298 299 300 301 302
	i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
}

static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
{
	unsigned long timeout = jiffies + HZ;
303 304 305 306 307 308 309 310 311 312 313 314
	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;
	}
315

316 317 318
	val = i2c_readl(i2c_dev, offset);
	val |= mask;
	i2c_writel(i2c_dev, val, offset);
C
Colin Cross 已提交
319

320
	while (i2c_readl(i2c_dev, offset) & mask) {
C
Colin Cross 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		if (time_after(jiffies, timeout)) {
			dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
			return -ETIMEDOUT;
		}
		msleep(1);
	}
	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;

338 339 340 341 342 343 344 345 346
	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 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

	/* 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) {
		BUG_ON(buf_remaining > 3);
		val = i2c_readl(i2c_dev, I2C_RX_FIFO);
366
		val = cpu_to_le32(val);
C
Colin Cross 已提交
367 368 369 370 371 372 373 374
		memcpy(buf, &val, buf_remaining);
		buf_remaining = 0;
		rx_fifo_avail--;
	}

	BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
	i2c_dev->msg_buf_remaining = buf_remaining;
	i2c_dev->msg_buf = buf;
375

C
Colin Cross 已提交
376 377 378 379 380 381 382 383 384 385 386
	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;

387 388 389 390 391 392 393 394 395
	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 已提交
396 397 398 399

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

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	/* 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 已提交
423 424 425 426 427 428 429 430 431

	/*
	 * 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) {
		BUG_ON(buf_remaining > 3);
		memcpy(&val, buf, buf_remaining);
432
		val = le32_to_cpu(val);
433 434 435 436 437 438

		/* 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 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		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)
{
454 455
	u32 val;

C
Colin Cross 已提交
456 457 458 459 460 461 462 463 464 465
	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);
}

466
static int tegra_i2c_runtime_resume(struct device *dev)
467
{
468
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
469
	int ret;
470

J
Jon Hunter 已提交
471 472 473 474
	ret = pinctrl_pm_select_default_state(i2c_dev->dev);
	if (ret)
		return ret;

475
	if (!i2c_dev->hw->has_single_clk_source) {
476
		ret = clk_enable(i2c_dev->fast_clk);
477 478 479 480 481
		if (ret < 0) {
			dev_err(i2c_dev->dev,
				"Enabling fast clk failed, err %d\n", ret);
			return ret;
		}
482
	}
483

484
	ret = clk_enable(i2c_dev->div_clk);
485 486 487
	if (ret < 0) {
		dev_err(i2c_dev->dev,
			"Enabling div clk failed, err %d\n", ret);
488
		clk_disable(i2c_dev->fast_clk);
489
		return ret;
490
	}
491 492

	return 0;
493 494
}

495
static int tegra_i2c_runtime_suspend(struct device *dev)
496
{
497 498
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);

499
	clk_disable(i2c_dev->div_clk);
500
	if (!i2c_dev->hw->has_single_clk_source)
501
		clk_disable(i2c_dev->fast_clk);
502

J
Jon Hunter 已提交
503
	return pinctrl_pm_select_idle_state(i2c_dev->dev);
504 505
}

506 507 508 509 510 511 512 513 514 515 516
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);
517 518 519 520 521 522 523
		if (in_interrupt())
			err = readl_poll_timeout_atomic(addr, val, val == 0,
					1000, I2C_CONFIG_LOAD_TIMEOUT);
		else
			err = readl_poll_timeout(addr, val, val == 0,
					1000, I2C_CONFIG_LOAD_TIMEOUT);

524 525 526 527 528 529 530 531 532 533
		if (err) {
			dev_warn(i2c_dev->dev,
				 "timeout waiting for config load\n");
			return err;
		}
	}

	return 0;
}

C
Colin Cross 已提交
534 535 536
static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
{
	u32 val;
537
	int err;
538
	u32 clk_divisor;
C
Colin Cross 已提交
539

540
	err = pm_runtime_get_sync(i2c_dev->dev);
541
	if (err < 0) {
542
		dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
543 544
		return err;
	}
C
Colin Cross 已提交
545

S
Stephen Warren 已提交
546
	reset_control_assert(i2c_dev->rst);
C
Colin Cross 已提交
547
	udelay(2);
S
Stephen Warren 已提交
548
	reset_control_deassert(i2c_dev->rst);
C
Colin Cross 已提交
549 550 551 552

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

J
Jay Cheng 已提交
553 554
	val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
		(0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
555 556 557 558

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

C
Colin Cross 已提交
559 560
	i2c_writel(i2c_dev, val, I2C_CNFG);
	i2c_writel(i2c_dev, 0, I2C_INT_MASK);
561 562 563

	/* Make sure clock divisor programmed correctly */
	clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
564
	clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
565 566
					I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
	i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
C
Colin Cross 已提交
567

568 569
	if (!i2c_dev->is_dvc) {
		u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
570

571 572 573 574
		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);
575 576
	}

577 578 579 580 581 582 583 584 585
	if (i2c_dev->hw->has_mst_fifo) {
		val = I2C_MST_FIFO_CONTROL_TX_TRIG(8) |
		      I2C_MST_FIFO_CONTROL_RX_TRIG(1);
		i2c_writel(i2c_dev, val, I2C_MST_FIFO_CONTROL);
	} else {
		val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
			0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
		i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
	}
C
Colin Cross 已提交
586

587
	err = tegra_i2c_flush_fifos(i2c_dev);
588 589
	if (err)
		goto err;
C
Colin Cross 已提交
590

591 592 593
	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);

594 595 596
	err = tegra_i2c_wait_for_config_load(i2c_dev);
	if (err)
		goto err;
597

598
	if (i2c_dev->irq_disabled) {
599
		i2c_dev->irq_disabled = false;
600 601 602
		enable_irq(i2c_dev->irq);
	}

603
err:
604
	pm_runtime_put(i2c_dev->dev);
C
Colin Cross 已提交
605 606 607
	return err;
}

608 609 610 611
static int tegra_i2c_disable_packet_mode(struct tegra_i2c_dev *i2c_dev)
{
	u32 cnfg;

J
Jon Hunter 已提交
612 613 614 615 616 617 618 619
	/*
	 * 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));

620 621 622 623 624 625 626
	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 已提交
627 628 629 630 631 632 633 634
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);

635
	spin_lock(&i2c_dev->xfer_lock);
C
Colin Cross 已提交
636
	if (status == 0) {
637 638 639 640 641 642 643 644
		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;

		if (!i2c_dev->irq_disabled) {
			disable_irq_nosync(i2c_dev->irq);
645
			i2c_dev->irq_disabled = true;
646 647
		}
		goto err;
C
Colin Cross 已提交
648 649 650
	}

	if (unlikely(status & status_err)) {
651
		tegra_i2c_disable_packet_mode(i2c_dev);
C
Colin Cross 已提交
652 653 654 655 656 657 658
		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;
	}

659 660 661 662 663 664 665
	/*
	 * 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;

C
Colin Cross 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
	if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
		if (i2c_dev->msg_buf_remaining)
			tegra_i2c_empty_rx_fifo(i2c_dev);
		else
			BUG();
	}

	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);
	}

680 681 682 683
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);

684 685
	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
		BUG_ON(i2c_dev->msg_buf_remaining);
C
Colin Cross 已提交
686
		complete(&i2c_dev->msg_complete);
687
	}
688
	goto done;
C
Colin Cross 已提交
689
err:
L
Lucas De Marchi 已提交
690
	/* An error occurred, mask all interrupts */
C
Colin Cross 已提交
691 692 693
	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);
694 695
	if (i2c_dev->hw->supports_bus_clear)
		tegra_i2c_mask_irq(i2c_dev, I2C_INT_BUS_CLR_DONE);
C
Colin Cross 已提交
696
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
697 698
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
699 700

	complete(&i2c_dev->msg_complete);
701
done:
702
	spin_unlock(&i2c_dev->xfer_lock);
C
Colin Cross 已提交
703 704 705
	return IRQ_HANDLED;
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
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);

	time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
						TEGRA_I2C_TIMEOUT);
	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 已提交
744
static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
745
	struct i2c_msg *msg, enum msg_end_type end_state)
C
Colin Cross 已提交
746 747 748
{
	u32 packet_header;
	u32 int_mask;
749
	unsigned long time_left;
750
	unsigned long flags;
C
Colin Cross 已提交
751 752 753 754 755 756 757

	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);
758
	reinit_completion(&i2c_dev->msg_complete);
C
Colin Cross 已提交
759

760 761 762 763 764
	spin_lock_irqsave(&i2c_dev->xfer_lock, flags);

	int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
	tegra_i2c_unmask_irq(i2c_dev, int_mask);

C
Colin Cross 已提交
765 766 767 768 769 770 771 772 773
	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);
	i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);

	packet_header = msg->len - 1;
	i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);

774
	packet_header = I2C_HEADER_IE_ENABLE;
775 776 777
	if (end_state == MSG_END_CONTINUE)
		packet_header |= I2C_HEADER_CONTINUE_XFER;
	else if (end_state == MSG_END_REPEAT_START)
778
		packet_header |= I2C_HEADER_REPEAT_START;
779 780
	if (msg->flags & I2C_M_TEN) {
		packet_header |= msg->addr;
C
Colin Cross 已提交
781
		packet_header |= I2C_HEADER_10BIT_ADDR;
782 783 784
	} else {
		packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
	}
C
Colin Cross 已提交
785 786 787 788 789 790 791 792 793
	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;
	i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);

	if (!(msg->flags & I2C_M_RD))
		tegra_i2c_fill_tx_fifo(i2c_dev);

794 795
	if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
		int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
C
Colin Cross 已提交
796 797 798 799
	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;
800

C
Colin Cross 已提交
801
	tegra_i2c_unmask_irq(i2c_dev, int_mask);
802
	spin_unlock_irqrestore(&i2c_dev->xfer_lock, flags);
C
Colin Cross 已提交
803 804 805
	dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
		i2c_readl(i2c_dev, I2C_INT_MASK));

806 807
	time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
						TEGRA_I2C_TIMEOUT);
C
Colin Cross 已提交
808 809
	tegra_i2c_mask_irq(i2c_dev, int_mask);

810
	if (time_left == 0) {
C
Colin Cross 已提交
811 812 813 814 815 816
		dev_err(i2c_dev->dev, "i2c transfer timed out\n");

		tegra_i2c_init(i2c_dev);
		return -ETIMEDOUT;
	}

817 818 819
	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 已提交
820 821 822 823 824

	if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
		return 0;

	tegra_i2c_init(i2c_dev);
825 826 827 828 829 830 831
	/* 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 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	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[],
	int num)
{
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
	int i;
	int ret = 0;

848
	ret = pm_runtime_get_sync(i2c_dev->dev);
849
	if (ret < 0) {
850
		dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
851 852 853
		return ret;
	}

C
Colin Cross 已提交
854
	for (i = 0; i < num; i++) {
855
		enum msg_end_type end_type = MSG_END_STOP;
856

857 858 859 860 861 862 863
		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 已提交
864 865 866
		if (ret)
			break;
	}
867 868 869

	pm_runtime_put(i2c_dev->dev);

C
Colin Cross 已提交
870 871 872 873 874
	return ret ?: i;
}

static u32 tegra_i2c_func(struct i2c_adapter *adap)
{
875
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
876 877
	u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
		  I2C_FUNC_10BIT_ADDR |	I2C_FUNC_PROTOCOL_MANGLING;
878 879 880 881

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

884 885 886 887 888 889 890 891 892 893 894 895 896 897
static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
{
	struct device_node *np = i2c_dev->dev->of_node;
	int ret;

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

	i2c_dev->is_multimaster_mode = of_property_read_bool(np,
			"multi-master");
}

C
Colin Cross 已提交
898 899 900 901 902
static const struct i2c_algorithm tegra_i2c_algo = {
	.master_xfer	= tegra_i2c_xfer,
	.functionality	= tegra_i2c_func,
};

903
/* payload size is only 12 bit */
904
static const struct i2c_adapter_quirks tegra_i2c_quirks = {
905
	.flags = I2C_AQ_NO_ZERO_LEN,
906 907
	.max_read_len = SZ_4K,
	.max_write_len = SZ_4K - I2C_PACKET_HEADER_SIZE,
908 909
};

910 911
static const struct i2c_adapter_quirks tegra194_i2c_quirks = {
	.flags = I2C_AQ_NO_ZERO_LEN,
912
	.max_write_len = SZ_64K - I2C_PACKET_HEADER_SIZE,
913 914
};

915 916 917 918
static struct i2c_bus_recovery_info tegra_i2c_recovery_info = {
	.recover_bus = tegra_i2c_issue_bus_clear,
};

919 920
static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
	.has_continue_xfer_support = false,
921 922 923 924
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
	.clk_divisor_std_fast_mode = 0,
925
	.clk_divisor_fast_plus_mode = 0,
926
	.has_config_load_reg = false,
927 928
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
929
	.has_mst_fifo = false,
930
	.quirks = &tegra_i2c_quirks,
931
	.supports_bus_clear = false,
932 933 934 935
};

static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
	.has_continue_xfer_support = true,
936 937 938 939
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
	.clk_divisor_std_fast_mode = 0,
940
	.clk_divisor_fast_plus_mode = 0,
941
	.has_config_load_reg = false,
942 943
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
944
	.has_mst_fifo = false,
945
	.quirks = &tegra_i2c_quirks,
946
	.supports_bus_clear = false,
947 948 949 950 951 952 953 954
};

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,
	.clk_divisor_std_fast_mode = 0x19,
955
	.clk_divisor_fast_plus_mode = 0x10,
956
	.has_config_load_reg = false,
957 958
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
959
	.has_mst_fifo = false,
960
	.quirks = &tegra_i2c_quirks,
961
	.supports_bus_clear = true,
962 963 964 965 966 967 968 969
};

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,
	.clk_divisor_std_fast_mode = 0x19,
970
	.clk_divisor_fast_plus_mode = 0x10,
971
	.has_config_load_reg = true,
972 973
	.has_multi_master_mode = false,
	.has_slcg_override_reg = true,
974
	.has_mst_fifo = false,
975
	.quirks = &tegra_i2c_quirks,
976
	.supports_bus_clear = true,
977 978 979 980 981 982 983 984 985 986 987 988
};

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,
	.clk_divisor_std_fast_mode = 0x19,
	.clk_divisor_fast_plus_mode = 0x10,
	.has_config_load_reg = true,
	.has_multi_master_mode = true,
	.has_slcg_override_reg = true,
989
	.has_mst_fifo = false,
990
	.quirks = &tegra_i2c_quirks,
991
	.supports_bus_clear = true,
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
};

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,
	.clk_divisor_std_fast_mode = 0x19,
	.clk_divisor_fast_plus_mode = 0x10,
	.has_config_load_reg = true,
	.has_multi_master_mode = true,
	.has_slcg_override_reg = true,
	.has_mst_fifo = true,
1005
	.quirks = &tegra194_i2c_quirks,
1006
	.supports_bus_clear = true,
1007 1008 1009
};

/* Match table for of_platform binding */
1010
static const struct of_device_id tegra_i2c_of_match[] = {
1011
	{ .compatible = "nvidia,tegra194-i2c", .data = &tegra194_i2c_hw, },
1012
	{ .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
1013
	{ .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
1014
	{ .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
1015 1016 1017 1018 1019 1020 1021
	{ .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);

1022
static int tegra_i2c_probe(struct platform_device *pdev)
C
Colin Cross 已提交
1023 1024 1025
{
	struct tegra_i2c_dev *i2c_dev;
	struct resource *res;
1026 1027
	struct clk *div_clk;
	struct clk *fast_clk;
O
Olof Johansson 已提交
1028
	void __iomem *base;
C
Colin Cross 已提交
1029 1030
	int irq;
	int ret = 0;
1031
	int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
C
Colin Cross 已提交
1032 1033

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1034 1035 1036
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);
C
Colin Cross 已提交
1037 1038 1039 1040

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

1045 1046
	div_clk = devm_clk_get(&pdev->dev, "div-clk");
	if (IS_ERR(div_clk)) {
1047
		dev_err(&pdev->dev, "missing controller clock\n");
1048
		return PTR_ERR(div_clk);
C
Colin Cross 已提交
1049 1050
	}

1051
	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
1052
	if (!i2c_dev)
1053
		return -ENOMEM;
C
Colin Cross 已提交
1054 1055

	i2c_dev->base = base;
1056
	i2c_dev->div_clk = div_clk;
C
Colin Cross 已提交
1057
	i2c_dev->adapter.algo = &tegra_i2c_algo;
1058
	i2c_dev->adapter.retries = 1;
C
Colin Cross 已提交
1059 1060 1061
	i2c_dev->irq = irq;
	i2c_dev->cont_id = pdev->id;
	i2c_dev->dev = &pdev->dev;
1062

1063
	i2c_dev->rst = devm_reset_control_get_exclusive(&pdev->dev, "i2c");
S
Stephen Warren 已提交
1064
	if (IS_ERR(i2c_dev->rst)) {
1065
		dev_err(&pdev->dev, "missing controller reset\n");
S
Stephen Warren 已提交
1066 1067 1068
		return PTR_ERR(i2c_dev->rst);
	}

1069
	tegra_i2c_parse_dt(i2c_dev);
C
Colin Cross 已提交
1070

1071 1072 1073
	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");
1074
	i2c_dev->adapter.quirks = i2c_dev->hw->quirks;
C
Colin Cross 已提交
1075
	init_completion(&i2c_dev->msg_complete);
1076
	spin_lock_init(&i2c_dev->xfer_lock);
C
Colin Cross 已提交
1077

1078 1079 1080
	if (!i2c_dev->hw->has_single_clk_source) {
		fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
		if (IS_ERR(fast_clk)) {
1081
			dev_err(&pdev->dev, "missing fast clock\n");
1082 1083 1084 1085 1086
			return PTR_ERR(fast_clk);
		}
		i2c_dev->fast_clk = fast_clk;
	}

C
Colin Cross 已提交
1087 1088
	platform_set_drvdata(pdev, i2c_dev);

1089 1090 1091 1092 1093 1094 1095 1096
	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;
		}
	}

1097 1098 1099 1100 1101 1102 1103 1104
	i2c_dev->clk_divisor_non_hs_mode =
			i2c_dev->hw->clk_divisor_std_fast_mode;
	if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
		(i2c_dev->bus_clk_rate == 1000000))
		i2c_dev->clk_divisor_non_hs_mode =
			i2c_dev->hw->clk_divisor_fast_plus_mode;

	clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	ret = clk_set_rate(i2c_dev->div_clk,
			   i2c_dev->bus_clk_rate * clk_multiplier);
	if (ret) {
		dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
		goto unprepare_fast_clk;
	}

	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;
	}

1118 1119 1120 1121 1122 1123 1124 1125 1126
	pm_runtime_enable(&pdev->dev);
	if (!pm_runtime_enabled(&pdev->dev)) {
		ret = tegra_i2c_runtime_resume(&pdev->dev);
		if (ret < 0) {
			dev_err(&pdev->dev, "runtime resume failed\n");
			goto unprepare_div_clk;
		}
	}

1127 1128 1129 1130 1131
	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);
1132
			goto disable_rpm;
1133 1134 1135
		}
	}

1136 1137 1138
	if (i2c_dev->hw->supports_bus_clear)
		i2c_dev->adapter.bus_recovery_info = &tegra_i2c_recovery_info;

C
Colin Cross 已提交
1139 1140
	ret = tegra_i2c_init(i2c_dev);
	if (ret) {
1141
		dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
1142
		goto disable_div_clk;
C
Colin Cross 已提交
1143 1144
	}

1145
	ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
1146
			tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
C
Colin Cross 已提交
1147 1148
	if (ret) {
		dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
1149
		goto disable_div_clk;
C
Colin Cross 已提交
1150 1151 1152 1153
	}

	i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
	i2c_dev->adapter.owner = THIS_MODULE;
1154
	i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
1155
	strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
C
Colin Cross 已提交
1156 1157 1158
		sizeof(i2c_dev->adapter.name));
	i2c_dev->adapter.dev.parent = &pdev->dev;
	i2c_dev->adapter.nr = pdev->id;
1159
	i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
C
Colin Cross 已提交
1160 1161

	ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
1162
	if (ret)
1163
		goto disable_div_clk;
C
Colin Cross 已提交
1164 1165

	return 0;
1166

1167 1168 1169 1170
disable_div_clk:
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

1171 1172 1173 1174 1175
disable_rpm:
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		tegra_i2c_runtime_suspend(&pdev->dev);

1176 1177 1178 1179 1180 1181 1182 1183
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 已提交
1184 1185
}

1186
static int tegra_i2c_remove(struct platform_device *pdev)
C
Colin Cross 已提交
1187 1188
{
	struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1189

C
Colin Cross 已提交
1190
	i2c_del_adapter(&i2c_dev->adapter);
1191

1192 1193 1194
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

1195 1196 1197 1198
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		tegra_i2c_runtime_suspend(&pdev->dev);

1199 1200 1201 1202
	clk_unprepare(i2c_dev->div_clk);
	if (!i2c_dev->hw->has_single_clk_source)
		clk_unprepare(i2c_dev->fast_clk);

C
Colin Cross 已提交
1203 1204 1205
	return 0;
}

1206
#ifdef CONFIG_PM_SLEEP
1207 1208 1209 1210
static const struct dev_pm_ops tegra_i2c_pm = {
	SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
			   NULL)
};
1211 1212 1213
#define TEGRA_I2C_PM	(&tegra_i2c_pm)
#else
#define TEGRA_I2C_PM	NULL
C
Colin Cross 已提交
1214 1215 1216 1217
#endif

static struct platform_driver tegra_i2c_driver = {
	.probe   = tegra_i2c_probe,
1218
	.remove  = tegra_i2c_remove,
C
Colin Cross 已提交
1219 1220
	.driver  = {
		.name  = "tegra-i2c",
1221
		.of_match_table = tegra_i2c_of_match,
1222
		.pm    = TEGRA_I2C_PM,
C
Colin Cross 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	},
};

static int __init tegra_i2c_init_driver(void)
{
	return platform_driver_register(&tegra_i2c_driver);
}

static void __exit tegra_i2c_exit_driver(void)
{
	platform_driver_unregister(&tegra_i2c_driver);
}

subsys_initcall(tegra_i2c_init_driver);
module_exit(tegra_i2c_exit_driver);

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