i2c-tegra.c 29.4 KB
Newer Older
C
Colin Cross 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * drivers/i2c/busses/i2c-tegra.c
 *
 * Copyright (C) 2010 Google, Inc.
 * Author: Colin Cross <ccross@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
28
#include <linux/of_device.h>
29
#include <linux/module.h>
S
Stephen Warren 已提交
30
#include <linux/reset.h>
J
Jon Hunter 已提交
31
#include <linux/pinctrl/consumer.h>
32
#include <linux/pm_runtime.h>
33
#include <linux/iopoll.h>
C
Colin Cross 已提交
34 35 36 37 38 39 40

#include <asm/unaligned.h>

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

#define I2C_CNFG				0x000
J
Jay Cheng 已提交
41
#define I2C_CNFG_DEBOUNCE_CNT_SHIFT		12
J
Jon Hunter 已提交
42 43 44
#define I2C_CNFG_PACKET_MODE_EN			BIT(10)
#define I2C_CNFG_NEW_MASTER_FSM			BIT(11)
#define I2C_CNFG_MULTI_MASTER_MODE		BIT(17)
45
#define I2C_STATUS				0x01C
C
Colin Cross 已提交
46
#define I2C_SL_CNFG				0x020
J
Jon Hunter 已提交
47 48
#define I2C_SL_CNFG_NACK			BIT(1)
#define I2C_SL_CNFG_NEWSL			BIT(2)
C
Colin Cross 已提交
49
#define I2C_SL_ADDR1				0x02c
50
#define I2C_SL_ADDR2				0x030
C
Colin Cross 已提交
51 52 53 54
#define I2C_TX_FIFO				0x050
#define I2C_RX_FIFO				0x054
#define I2C_PACKET_TRANSFER_STATUS		0x058
#define I2C_FIFO_CONTROL			0x05c
J
Jon Hunter 已提交
55 56
#define I2C_FIFO_CONTROL_TX_FLUSH		BIT(1)
#define I2C_FIFO_CONTROL_RX_FLUSH		BIT(0)
C
Colin Cross 已提交
57 58 59 60 61 62 63 64 65
#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
J
Jon Hunter 已提交
66 67 68 69 70 71 72 73
#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 已提交
74
#define I2C_CLK_DIVISOR				0x06c
75 76
#define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT	16
#define I2C_CLK_MULTIPLIER_STD_FAST_MODE	8
C
Colin Cross 已提交
77 78

#define DVC_CTRL_REG1				0x000
J
Jon Hunter 已提交
79
#define DVC_CTRL_REG1_INTR_EN			BIT(10)
C
Colin Cross 已提交
80 81
#define DVC_CTRL_REG2				0x004
#define DVC_CTRL_REG3				0x008
J
Jon Hunter 已提交
82 83
#define DVC_CTRL_REG3_SW_PROG			BIT(26)
#define DVC_CTRL_REG3_I2C_DONE_INTR_EN		BIT(30)
C
Colin Cross 已提交
84
#define DVC_STATUS				0x00c
J
Jon Hunter 已提交
85
#define DVC_STATUS_I2C_DONE_INTR		BIT(30)
C
Colin Cross 已提交
86 87 88 89

#define I2C_ERR_NONE				0x00
#define I2C_ERR_NO_ACK				0x01
#define I2C_ERR_ARBITRATION_LOST		0x02
90
#define I2C_ERR_UNKNOWN_INTERRUPT		0x04
C
Colin Cross 已提交
91 92 93 94

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

J
Jon Hunter 已提交
97 98 99 100 101 102 103 104
#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 已提交
105 106
#define I2C_HEADER_MASTER_ADDR_SHIFT		12
#define I2C_HEADER_SLAVE_ADDR_SHIFT		1
107 108

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

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

116 117
#define I2C_CONFIG_LOAD_TIMEOUT			1000000

118 119 120 121 122 123 124 125 126 127 128 129
/*
 * 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 已提交
130

131 132 133
/**
 * struct tegra_i2c_hw_feature : Different HW support on Tegra
 * @has_continue_xfer_support: Continue transfer supports.
134 135 136 137 138
 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
 *		complete interrupt per packet basis.
 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
 *		and earlier Socs has two clock sources i.e. div-clk and
 *		fast-clk.
139 140
 * @has_config_load_reg: Has the config load register to load the new
 *		configuration.
141 142 143 144
 * @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.
145 146 147 148
 */

struct tegra_i2c_hw_feature {
	bool has_continue_xfer_support;
149 150
	bool has_per_pkt_xfer_complete_irq;
	bool has_single_clk_source;
151
	bool has_config_load_reg;
152 153
	int clk_divisor_hs_mode;
	int clk_divisor_std_fast_mode;
154
	u16 clk_divisor_fast_plus_mode;
155 156
	bool has_multi_master_mode;
	bool has_slcg_override_reg;
157 158
};

C
Colin Cross 已提交
159 160 161
/**
 * struct tegra_i2c_dev	- per device i2c context
 * @dev: device reference for power management
162
 * @hw: Tegra i2c hw feature.
C
Colin Cross 已提交
163
 * @adapter: core i2c layer adapter information
164 165
 * @div_clk: clock reference for div clock of i2c controller.
 * @fast_clk: clock reference for fast clock of i2c controller.
C
Colin Cross 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179
 * @base: ioremapped registers cookie
 * @cont_id: i2c controller id, used for for packet header
 * @irq: irq number of transfer complete interrupt
 * @is_dvc: identifies the DVC i2c controller, has a different register layout
 * @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
 * @bus_clk_rate: current i2c bus clock rate
 * @is_suspended: prevents i2c controller accesses after suspend is called
 */
struct tegra_i2c_dev {
	struct device *dev;
180
	const struct tegra_i2c_hw_feature *hw;
C
Colin Cross 已提交
181
	struct i2c_adapter adapter;
182 183
	struct clk *div_clk;
	struct clk *fast_clk;
S
Stephen Warren 已提交
184
	struct reset_control *rst;
C
Colin Cross 已提交
185 186 187
	void __iomem *base;
	int cont_id;
	int irq;
188
	bool irq_disabled;
C
Colin Cross 已提交
189 190 191 192 193 194
	int is_dvc;
	struct completion msg_complete;
	int msg_err;
	u8 *msg_buf;
	size_t msg_buf_remaining;
	int msg_read;
195
	u32 bus_clk_rate;
196
	u16 clk_divisor_non_hs_mode;
C
Colin Cross 已提交
197
	bool is_suspended;
198
	bool is_multimaster_mode;
C
Colin Cross 已提交
199 200
};

201 202
static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
		       unsigned long reg)
C
Colin Cross 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
{
	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));
228 229 230 231

	/* 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 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
}

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)
{
253 254 255
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
C
Colin Cross 已提交
256 257 258 259 260
	i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
}

static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
{
261 262 263
	u32 int_mask;

	int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
C
Colin Cross 已提交
264 265 266 267 268 269 270
	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;
	u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
271

C
Colin Cross 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
	i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);

	while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
		(I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
		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;

	val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
	rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
		I2C_FIFO_STATUS_RX_SHIFT;

	/* 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);
316
		val = cpu_to_le32(val);
C
Colin Cross 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		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;
	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;

	val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
	tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
		I2C_FIFO_STATUS_TX_SHIFT;

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

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	/* 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 已提交
366 367 368 369 370 371 372 373 374

	/*
	 * 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);
375
		val = le32_to_cpu(val);
376 377 378 379 380 381

		/* 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 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		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)
{
397 398
	u32 val;

C
Colin Cross 已提交
399 400 401 402 403 404 405 406 407 408
	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);
}

409
static int tegra_i2c_runtime_resume(struct device *dev)
410
{
411
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
412
	int ret;
413

J
Jon Hunter 已提交
414 415 416 417
	ret = pinctrl_pm_select_default_state(i2c_dev->dev);
	if (ret)
		return ret;

418
	if (!i2c_dev->hw->has_single_clk_source) {
419
		ret = clk_enable(i2c_dev->fast_clk);
420 421 422 423 424
		if (ret < 0) {
			dev_err(i2c_dev->dev,
				"Enabling fast clk failed, err %d\n", ret);
			return ret;
		}
425
	}
426

427
	ret = clk_enable(i2c_dev->div_clk);
428 429 430
	if (ret < 0) {
		dev_err(i2c_dev->dev,
			"Enabling div clk failed, err %d\n", ret);
431
		clk_disable(i2c_dev->fast_clk);
432
		return ret;
433
	}
434 435

	return 0;
436 437
}

438
static int tegra_i2c_runtime_suspend(struct device *dev)
439
{
440 441
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);

442
	clk_disable(i2c_dev->div_clk);
443
	if (!i2c_dev->hw->has_single_clk_source)
444
		clk_disable(i2c_dev->fast_clk);
445

J
Jon Hunter 已提交
446
	return pinctrl_pm_select_idle_state(i2c_dev->dev);
447 448
}

C
Colin Cross 已提交
449 450 451
static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
{
	u32 val;
452
	int err;
453
	u32 clk_divisor;
C
Colin Cross 已提交
454

455
	err = pm_runtime_get_sync(i2c_dev->dev);
456
	if (err < 0) {
457
		dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
458 459
		return err;
	}
C
Colin Cross 已提交
460

S
Stephen Warren 已提交
461
	reset_control_assert(i2c_dev->rst);
C
Colin Cross 已提交
462
	udelay(2);
S
Stephen Warren 已提交
463
	reset_control_deassert(i2c_dev->rst);
C
Colin Cross 已提交
464 465 466 467

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

J
Jay Cheng 已提交
468 469
	val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
		(0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
470 471 472 473

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

C
Colin Cross 已提交
474 475
	i2c_writel(i2c_dev, val, I2C_CNFG);
	i2c_writel(i2c_dev, 0, I2C_INT_MASK);
476 477 478

	/* Make sure clock divisor programmed correctly */
	clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
479
	clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
480 481
					I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
	i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
C
Colin Cross 已提交
482

483 484
	if (!i2c_dev->is_dvc) {
		u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
485

486 487 488 489
		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);
490 491
	}

C
Colin Cross 已提交
492 493 494 495
	val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
		0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
	i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);

496
	err = tegra_i2c_flush_fifos(i2c_dev);
C
Colin Cross 已提交
497

498 499 500
	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);

501
	if (i2c_dev->hw->has_config_load_reg) {
502 503 504 505 506
		unsigned long reg_offset;
		void __iomem *addr;

		reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
		addr = i2c_dev->base + reg_offset;
507
		i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
508 509 510 511 512 513
		err = readl_poll_timeout(addr, val, val == 0, 1000,
					 I2C_CONFIG_LOAD_TIMEOUT);
		if (err) {
			dev_warn(i2c_dev->dev,
				 "timeout waiting for config load\n");
			goto err;
514 515 516
		}
	}

517 518 519 520 521
	if (i2c_dev->irq_disabled) {
		i2c_dev->irq_disabled = 0;
		enable_irq(i2c_dev->irq);
	}

522
err:
523
	pm_runtime_put(i2c_dev->dev);
C
Colin Cross 已提交
524 525 526 527 528 529 530 531 532 533 534 535
	return err;
}

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) {
536 537 538 539 540 541 542 543 544 545 546
		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);
			i2c_dev->irq_disabled = 1;
		}
		goto err;
C
Colin Cross 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
	}

	if (unlikely(status & status_err)) {
		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;
	}

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

571 572 573 574
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);

575 576
	if (status & I2C_INT_PACKET_XFER_COMPLETE) {
		BUG_ON(i2c_dev->msg_buf_remaining);
C
Colin Cross 已提交
577
		complete(&i2c_dev->msg_complete);
578
	}
C
Colin Cross 已提交
579 580
	return IRQ_HANDLED;
err:
L
Lucas De Marchi 已提交
581
	/* An error occurred, mask all interrupts */
C
Colin Cross 已提交
582 583 584 585
	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);
	i2c_writel(i2c_dev, status, I2C_INT_STATUS);
586 587
	if (i2c_dev->is_dvc)
		dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
588 589

	complete(&i2c_dev->msg_complete);
C
Colin Cross 已提交
590 591 592 593
	return IRQ_HANDLED;
}

static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
594
	struct i2c_msg *msg, enum msg_end_type end_state)
C
Colin Cross 已提交
595 596 597
{
	u32 packet_header;
	u32 int_mask;
598
	unsigned long time_left;
C
Colin Cross 已提交
599 600 601 602 603 604 605 606 607 608

	tegra_i2c_flush_fifos(i2c_dev);

	if (msg->len == 0)
		return -EINVAL;

	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);
609
	reinit_completion(&i2c_dev->msg_complete);
C
Colin Cross 已提交
610 611 612 613 614 615 616 617 618 619

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

620
	packet_header = I2C_HEADER_IE_ENABLE;
621 622 623
	if (end_state == MSG_END_CONTINUE)
		packet_header |= I2C_HEADER_CONTINUE_XFER;
	else if (end_state == MSG_END_REPEAT_START)
624
		packet_header |= I2C_HEADER_REPEAT_START;
625 626
	if (msg->flags & I2C_M_TEN) {
		packet_header |= msg->addr;
C
Colin Cross 已提交
627
		packet_header |= I2C_HEADER_10BIT_ADDR;
628 629 630
	} else {
		packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
	}
C
Colin Cross 已提交
631 632 633 634 635 636 637 638 639 640
	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);

	int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
641 642
	if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
		int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
C
Colin Cross 已提交
643 644 645 646 647 648 649 650
	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;
	tegra_i2c_unmask_irq(i2c_dev, int_mask);
	dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
		i2c_readl(i2c_dev, I2C_INT_MASK));

651 652
	time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
						TEGRA_I2C_TIMEOUT);
C
Colin Cross 已提交
653 654
	tegra_i2c_mask_irq(i2c_dev, int_mask);

655
	if (time_left == 0) {
C
Colin Cross 已提交
656 657 658 659 660 661
		dev_err(i2c_dev->dev, "i2c transfer timed out\n");

		tegra_i2c_init(i2c_dev);
		return -ETIMEDOUT;
	}

662 663 664
	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 已提交
665 666 667 668

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

669
	/*
670 671 672 673
	 * NACK interrupt is generated before the I2C controller generates
	 * the STOP condition on the bus. So wait for 2 clock periods
	 * before resetting the controller so that the STOP condition has
	 * been delivered properly.
674 675 676 677
	 */
	if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
		udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));

C
Colin Cross 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	tegra_i2c_init(i2c_dev);
	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;

	if (i2c_dev->is_suspended)
		return -EBUSY;

698
	ret = pm_runtime_get_sync(i2c_dev->dev);
699
	if (ret < 0) {
700
		dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
701 702 703
		return ret;
	}

C
Colin Cross 已提交
704
	for (i = 0; i < num; i++) {
705
		enum msg_end_type end_type = MSG_END_STOP;
706

707 708 709 710 711 712 713
		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 已提交
714 715 716
		if (ret)
			break;
	}
717 718 719

	pm_runtime_put(i2c_dev->dev);

C
Colin Cross 已提交
720 721 722 723 724
	return ret ?: i;
}

static u32 tegra_i2c_func(struct i2c_adapter *adap)
{
725
	struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
726 727
	u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
		  I2C_FUNC_10BIT_ADDR |	I2C_FUNC_PROTOCOL_MANGLING;
728 729 730 731

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

734 735 736 737 738 739 740 741 742 743 744 745 746 747
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 已提交
748 749 750 751 752
static const struct i2c_algorithm tegra_i2c_algo = {
	.master_xfer	= tegra_i2c_xfer,
	.functionality	= tegra_i2c_func,
};

753 754 755 756 757 758
/* payload size is only 12 bit */
static struct i2c_adapter_quirks tegra_i2c_quirks = {
	.max_read_len = 4096,
	.max_write_len = 4096,
};

759 760
static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
	.has_continue_xfer_support = false,
761 762 763 764
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
	.clk_divisor_std_fast_mode = 0,
765
	.clk_divisor_fast_plus_mode = 0,
766
	.has_config_load_reg = false,
767 768
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
769 770 771 772
};

static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
	.has_continue_xfer_support = true,
773 774 775 776
	.has_per_pkt_xfer_complete_irq = false,
	.has_single_clk_source = false,
	.clk_divisor_hs_mode = 3,
	.clk_divisor_std_fast_mode = 0,
777
	.clk_divisor_fast_plus_mode = 0,
778
	.has_config_load_reg = false,
779 780
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
781 782 783 784 785 786 787 788
};

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,
789
	.clk_divisor_fast_plus_mode = 0x10,
790
	.has_config_load_reg = false,
791 792
	.has_multi_master_mode = false,
	.has_slcg_override_reg = false,
793 794 795 796 797 798 799 800
};

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,
801
	.clk_divisor_fast_plus_mode = 0x10,
802
	.has_config_load_reg = true,
803 804 805 806 807 808 809 810 811 812 813 814 815 816
	.has_multi_master_mode = false,
	.has_slcg_override_reg = true,
};

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,
817 818 819
};

/* Match table for of_platform binding */
820
static const struct of_device_id tegra_i2c_of_match[] = {
821
	{ .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
822
	{ .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
823
	{ .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
824 825 826 827 828 829 830
	{ .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);

831
static int tegra_i2c_probe(struct platform_device *pdev)
C
Colin Cross 已提交
832 833 834
{
	struct tegra_i2c_dev *i2c_dev;
	struct resource *res;
835 836
	struct clk *div_clk;
	struct clk *fast_clk;
O
Olof Johansson 已提交
837
	void __iomem *base;
C
Colin Cross 已提交
838 839
	int irq;
	int ret = 0;
840
	int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
C
Colin Cross 已提交
841 842

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
843 844 845
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base))
		return PTR_ERR(base);
C
Colin Cross 已提交
846 847 848 849

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

854 855
	div_clk = devm_clk_get(&pdev->dev, "div-clk");
	if (IS_ERR(div_clk)) {
856
		dev_err(&pdev->dev, "missing controller clock\n");
857
		return PTR_ERR(div_clk);
C
Colin Cross 已提交
858 859
	}

860
	i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
861
	if (!i2c_dev)
862
		return -ENOMEM;
C
Colin Cross 已提交
863 864

	i2c_dev->base = base;
865
	i2c_dev->div_clk = div_clk;
C
Colin Cross 已提交
866
	i2c_dev->adapter.algo = &tegra_i2c_algo;
867
	i2c_dev->adapter.quirks = &tegra_i2c_quirks;
C
Colin Cross 已提交
868 869 870
	i2c_dev->irq = irq;
	i2c_dev->cont_id = pdev->id;
	i2c_dev->dev = &pdev->dev;
871

S
Stephen Warren 已提交
872 873
	i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
	if (IS_ERR(i2c_dev->rst)) {
874
		dev_err(&pdev->dev, "missing controller reset\n");
S
Stephen Warren 已提交
875 876 877
		return PTR_ERR(i2c_dev->rst);
	}

878
	tegra_i2c_parse_dt(i2c_dev);
C
Colin Cross 已提交
879

880 881 882
	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");
C
Colin Cross 已提交
883 884
	init_completion(&i2c_dev->msg_complete);

885 886 887
	if (!i2c_dev->hw->has_single_clk_source) {
		fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
		if (IS_ERR(fast_clk)) {
888
			dev_err(&pdev->dev, "missing fast clock\n");
889 890 891 892 893
			return PTR_ERR(fast_clk);
		}
		i2c_dev->fast_clk = fast_clk;
	}

C
Colin Cross 已提交
894 895
	platform_set_drvdata(pdev, i2c_dev);

896 897 898 899 900 901 902 903
	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;
		}
	}

904 905 906 907 908 909 910 911
	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);
912 913 914 915 916 917 918 919 920 921 922 923 924
	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;
	}

925 926 927 928 929 930 931 932 933
	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;
		}
	}

934 935 936 937 938
	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);
939
			goto disable_rpm;
940 941 942
		}
	}

C
Colin Cross 已提交
943 944
	ret = tegra_i2c_init(i2c_dev);
	if (ret) {
945
		dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
946
		goto disable_div_clk;
C
Colin Cross 已提交
947 948
	}

949
	ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
950
			tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
C
Colin Cross 已提交
951 952
	if (ret) {
		dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
953
		goto disable_div_clk;
C
Colin Cross 已提交
954 955 956 957
	}

	i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
	i2c_dev->adapter.owner = THIS_MODULE;
958
	i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
959
	strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
C
Colin Cross 已提交
960 961 962
		sizeof(i2c_dev->adapter.name));
	i2c_dev->adapter.dev.parent = &pdev->dev;
	i2c_dev->adapter.nr = pdev->id;
963
	i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
C
Colin Cross 已提交
964 965

	ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
966
	if (ret)
967
		goto disable_div_clk;
C
Colin Cross 已提交
968 969

	return 0;
970

971 972 973 974
disable_div_clk:
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

975 976 977 978 979
disable_rpm:
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		tegra_i2c_runtime_suspend(&pdev->dev);

980 981 982 983 984 985 986 987
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 已提交
988 989
}

990
static int tegra_i2c_remove(struct platform_device *pdev)
C
Colin Cross 已提交
991 992
{
	struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
993

C
Colin Cross 已提交
994
	i2c_del_adapter(&i2c_dev->adapter);
995

996 997 998
	if (i2c_dev->is_multimaster_mode)
		clk_disable(i2c_dev->div_clk);

999 1000 1001 1002
	pm_runtime_disable(&pdev->dev);
	if (!pm_runtime_status_suspended(&pdev->dev))
		tegra_i2c_runtime_suspend(&pdev->dev);

1003 1004 1005 1006
	clk_unprepare(i2c_dev->div_clk);
	if (!i2c_dev->hw->has_single_clk_source)
		clk_unprepare(i2c_dev->fast_clk);

C
Colin Cross 已提交
1007 1008 1009
	return 0;
}

1010
#ifdef CONFIG_PM_SLEEP
1011
static int tegra_i2c_suspend(struct device *dev)
C
Colin Cross 已提交
1012
{
1013
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
C
Colin Cross 已提交
1014 1015 1016 1017 1018 1019 1020 1021

	i2c_lock_adapter(&i2c_dev->adapter);
	i2c_dev->is_suspended = true;
	i2c_unlock_adapter(&i2c_dev->adapter);

	return 0;
}

1022
static int tegra_i2c_resume(struct device *dev)
C
Colin Cross 已提交
1023
{
1024
	struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
C
Colin Cross 已提交
1025 1026 1027 1028 1029
	int ret;

	i2c_lock_adapter(&i2c_dev->adapter);

	ret = tegra_i2c_init(i2c_dev);
J
Jon Hunter 已提交
1030 1031
	if (!ret)
		i2c_dev->is_suspended = false;
C
Colin Cross 已提交
1032 1033 1034

	i2c_unlock_adapter(&i2c_dev->adapter);

J
Jon Hunter 已提交
1035
	return ret;
C
Colin Cross 已提交
1036
}
1037

1038 1039 1040 1041 1042
static const struct dev_pm_ops tegra_i2c_pm = {
	SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
			   NULL)
	SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
};
1043 1044 1045
#define TEGRA_I2C_PM	(&tegra_i2c_pm)
#else
#define TEGRA_I2C_PM	NULL
C
Colin Cross 已提交
1046 1047 1048 1049
#endif

static struct platform_driver tegra_i2c_driver = {
	.probe   = tegra_i2c_probe,
1050
	.remove  = tegra_i2c_remove,
C
Colin Cross 已提交
1051 1052
	.driver  = {
		.name  = "tegra-i2c",
1053
		.of_match_table = tegra_i2c_of_match,
1054
		.pm    = TEGRA_I2C_PM,
C
Colin Cross 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	},
};

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