2 * drivers/i2c/busses/i2c-tegra.c
4 * Copyright (C) 2010 Google, Inc.
5 * Author: Colin Cross <ccross@android.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/err.h>
23 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/of_device.h>
29 #include <linux/module.h>
30 #include <linux/reset.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/iopoll.h>
35 #include <asm/unaligned.h>
37 #define TEGRA_I2C_TIMEOUT (msecs_to_jiffies(1000))
38 #define BYTES_PER_FIFO_WORD 4
40 #define I2C_CNFG 0x000
41 #define I2C_CNFG_DEBOUNCE_CNT_SHIFT 12
42 #define I2C_CNFG_PACKET_MODE_EN BIT(10)
43 #define I2C_CNFG_NEW_MASTER_FSM BIT(11)
44 #define I2C_CNFG_MULTI_MASTER_MODE BIT(17)
45 #define I2C_STATUS 0x01C
46 #define I2C_SL_CNFG 0x020
47 #define I2C_SL_CNFG_NACK BIT(1)
48 #define I2C_SL_CNFG_NEWSL BIT(2)
49 #define I2C_SL_ADDR1 0x02c
50 #define I2C_SL_ADDR2 0x030
51 #define I2C_TX_FIFO 0x050
52 #define I2C_RX_FIFO 0x054
53 #define I2C_PACKET_TRANSFER_STATUS 0x058
54 #define I2C_FIFO_CONTROL 0x05c
55 #define I2C_FIFO_CONTROL_TX_FLUSH BIT(1)
56 #define I2C_FIFO_CONTROL_RX_FLUSH BIT(0)
57 #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
58 #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
59 #define I2C_FIFO_STATUS 0x060
60 #define I2C_FIFO_STATUS_TX_MASK 0xF0
61 #define I2C_FIFO_STATUS_TX_SHIFT 4
62 #define I2C_FIFO_STATUS_RX_MASK 0x0F
63 #define I2C_FIFO_STATUS_RX_SHIFT 0
64 #define I2C_INT_MASK 0x064
65 #define I2C_INT_STATUS 0x068
66 #define I2C_INT_PACKET_XFER_COMPLETE BIT(7)
67 #define I2C_INT_ALL_PACKETS_XFER_COMPLETE BIT(6)
68 #define I2C_INT_TX_FIFO_OVERFLOW BIT(5)
69 #define I2C_INT_RX_FIFO_UNDERFLOW BIT(4)
70 #define I2C_INT_NO_ACK BIT(3)
71 #define I2C_INT_ARBITRATION_LOST BIT(2)
72 #define I2C_INT_TX_FIFO_DATA_REQ BIT(1)
73 #define I2C_INT_RX_FIFO_DATA_REQ BIT(0)
74 #define I2C_CLK_DIVISOR 0x06c
75 #define I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT 16
76 #define I2C_CLK_MULTIPLIER_STD_FAST_MODE 8
78 #define DVC_CTRL_REG1 0x000
79 #define DVC_CTRL_REG1_INTR_EN BIT(10)
80 #define DVC_CTRL_REG2 0x004
81 #define DVC_CTRL_REG3 0x008
82 #define DVC_CTRL_REG3_SW_PROG BIT(26)
83 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN BIT(30)
84 #define DVC_STATUS 0x00c
85 #define DVC_STATUS_I2C_DONE_INTR BIT(30)
87 #define I2C_ERR_NONE 0x00
88 #define I2C_ERR_NO_ACK 0x01
89 #define I2C_ERR_ARBITRATION_LOST 0x02
90 #define I2C_ERR_UNKNOWN_INTERRUPT 0x04
92 #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
93 #define PACKET_HEADER0_PACKET_ID_SHIFT 16
94 #define PACKET_HEADER0_CONT_ID_SHIFT 12
95 #define PACKET_HEADER0_PROTOCOL_I2C BIT(4)
97 #define I2C_HEADER_HIGHSPEED_MODE BIT(22)
98 #define I2C_HEADER_CONT_ON_NAK BIT(21)
99 #define I2C_HEADER_SEND_START_BYTE BIT(20)
100 #define I2C_HEADER_READ BIT(19)
101 #define I2C_HEADER_10BIT_ADDR BIT(18)
102 #define I2C_HEADER_IE_ENABLE BIT(17)
103 #define I2C_HEADER_REPEAT_START BIT(16)
104 #define I2C_HEADER_CONTINUE_XFER BIT(15)
105 #define I2C_HEADER_MASTER_ADDR_SHIFT 12
106 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
108 #define I2C_CONFIG_LOAD 0x08C
109 #define I2C_MSTR_CONFIG_LOAD BIT(0)
110 #define I2C_SLV_CONFIG_LOAD BIT(1)
111 #define I2C_TIMEOUT_CONFIG_LOAD BIT(2)
113 #define I2C_CLKEN_OVERRIDE 0x090
114 #define I2C_MST_CORE_CLKEN_OVR BIT(0)
116 #define I2C_CONFIG_LOAD_TIMEOUT 1000000
119 * msg_end_type: The bus control which need to be send at end of transfer.
120 * @MSG_END_STOP: Send stop pulse at end of transfer.
121 * @MSG_END_REPEAT_START: Send repeat start at end of transfer.
122 * @MSG_END_CONTINUE: The following on message is coming and so do not send
123 * stop or repeat start.
127 MSG_END_REPEAT_START,
132 * struct tegra_i2c_hw_feature : Different HW support on Tegra
133 * @has_continue_xfer_support: Continue transfer supports.
134 * @has_per_pkt_xfer_complete_irq: Has enable/disable capability for transfer
135 * complete interrupt per packet basis.
136 * @has_single_clk_source: The i2c controller has single clock source. Tegra30
137 * and earlier Socs has two clock sources i.e. div-clk and
139 * @has_config_load_reg: Has the config load register to load the new
141 * @clk_divisor_hs_mode: Clock divisor in HS mode.
142 * @clk_divisor_std_fast_mode: Clock divisor in standard/fast mode. It is
143 * applicable if there is no fast clock source i.e. single clock
147 struct tegra_i2c_hw_feature {
148 bool has_continue_xfer_support;
149 bool has_per_pkt_xfer_complete_irq;
150 bool has_single_clk_source;
151 bool has_config_load_reg;
152 int clk_divisor_hs_mode;
153 int clk_divisor_std_fast_mode;
154 u16 clk_divisor_fast_plus_mode;
155 bool has_multi_master_mode;
156 bool has_slcg_override_reg;
160 * struct tegra_i2c_dev - per device i2c context
161 * @dev: device reference for power management
162 * @hw: Tegra i2c hw feature.
163 * @adapter: core i2c layer adapter information
164 * @div_clk: clock reference for div clock of i2c controller.
165 * @fast_clk: clock reference for fast clock of i2c controller.
166 * @base: ioremapped registers cookie
167 * @cont_id: i2c controller id, used for for packet header
168 * @irq: irq number of transfer complete interrupt
169 * @is_dvc: identifies the DVC i2c controller, has a different register layout
170 * @msg_complete: transfer completion notifier
171 * @msg_err: error code for completed message
172 * @msg_buf: pointer to current message data
173 * @msg_buf_remaining: size of unsent data in the message buffer
174 * @msg_read: identifies read transfers
175 * @bus_clk_rate: current i2c bus clock rate
176 * @is_suspended: prevents i2c controller accesses after suspend is called
178 struct tegra_i2c_dev {
180 const struct tegra_i2c_hw_feature *hw;
181 struct i2c_adapter adapter;
183 struct clk *fast_clk;
184 struct reset_control *rst;
190 struct completion msg_complete;
193 size_t msg_buf_remaining;
196 u16 clk_divisor_non_hs_mode;
198 bool is_multimaster_mode;
201 static void dvc_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
204 writel(val, i2c_dev->base + reg);
207 static u32 dvc_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
209 return readl(i2c_dev->base + reg);
213 * i2c_writel and i2c_readl will offset the register if necessary to talk
214 * to the I2C block inside the DVC block
216 static unsigned long tegra_i2c_reg_addr(struct tegra_i2c_dev *i2c_dev,
220 reg += (reg >= I2C_TX_FIFO) ? 0x10 : 0x40;
224 static void i2c_writel(struct tegra_i2c_dev *i2c_dev, u32 val,
227 writel(val, i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
229 /* Read back register to make sure that register writes completed */
230 if (reg != I2C_TX_FIFO)
231 readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
234 static u32 i2c_readl(struct tegra_i2c_dev *i2c_dev, unsigned long reg)
236 return readl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg));
239 static void i2c_writesl(struct tegra_i2c_dev *i2c_dev, void *data,
240 unsigned long reg, int len)
242 writesl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
245 static void i2c_readsl(struct tegra_i2c_dev *i2c_dev, void *data,
246 unsigned long reg, int len)
248 readsl(i2c_dev->base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
251 static void tegra_i2c_mask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
255 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) & ~mask;
256 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
259 static void tegra_i2c_unmask_irq(struct tegra_i2c_dev *i2c_dev, u32 mask)
263 int_mask = i2c_readl(i2c_dev, I2C_INT_MASK) | mask;
264 i2c_writel(i2c_dev, int_mask, I2C_INT_MASK);
267 static int tegra_i2c_flush_fifos(struct tegra_i2c_dev *i2c_dev)
269 unsigned long timeout = jiffies + HZ;
270 u32 val = i2c_readl(i2c_dev, I2C_FIFO_CONTROL);
272 val |= I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH;
273 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
275 while (i2c_readl(i2c_dev, I2C_FIFO_CONTROL) &
276 (I2C_FIFO_CONTROL_TX_FLUSH | I2C_FIFO_CONTROL_RX_FLUSH)) {
277 if (time_after(jiffies, timeout)) {
278 dev_warn(i2c_dev->dev, "timeout waiting for fifo flush\n");
286 static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev *i2c_dev)
290 u8 *buf = i2c_dev->msg_buf;
291 size_t buf_remaining = i2c_dev->msg_buf_remaining;
292 int words_to_transfer;
294 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
295 rx_fifo_avail = (val & I2C_FIFO_STATUS_RX_MASK) >>
296 I2C_FIFO_STATUS_RX_SHIFT;
298 /* Rounds down to not include partial word at the end of buf */
299 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
300 if (words_to_transfer > rx_fifo_avail)
301 words_to_transfer = rx_fifo_avail;
303 i2c_readsl(i2c_dev, buf, I2C_RX_FIFO, words_to_transfer);
305 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
306 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
307 rx_fifo_avail -= words_to_transfer;
310 * If there is a partial word at the end of buf, handle it manually to
311 * prevent overwriting past the end of buf
313 if (rx_fifo_avail > 0 && buf_remaining > 0) {
314 BUG_ON(buf_remaining > 3);
315 val = i2c_readl(i2c_dev, I2C_RX_FIFO);
316 val = cpu_to_le32(val);
317 memcpy(buf, &val, buf_remaining);
322 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
323 i2c_dev->msg_buf_remaining = buf_remaining;
324 i2c_dev->msg_buf = buf;
328 static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev *i2c_dev)
332 u8 *buf = i2c_dev->msg_buf;
333 size_t buf_remaining = i2c_dev->msg_buf_remaining;
334 int words_to_transfer;
336 val = i2c_readl(i2c_dev, I2C_FIFO_STATUS);
337 tx_fifo_avail = (val & I2C_FIFO_STATUS_TX_MASK) >>
338 I2C_FIFO_STATUS_TX_SHIFT;
340 /* Rounds down to not include partial word at the end of buf */
341 words_to_transfer = buf_remaining / BYTES_PER_FIFO_WORD;
343 /* It's very common to have < 4 bytes, so optimize that case. */
344 if (words_to_transfer) {
345 if (words_to_transfer > tx_fifo_avail)
346 words_to_transfer = tx_fifo_avail;
349 * Update state before writing to FIFO. If this casues us
350 * to finish writing all bytes (AKA buf_remaining goes to 0) we
351 * have a potential for an interrupt (PACKET_XFER_COMPLETE is
352 * not maskable). We need to make sure that the isr sees
353 * buf_remaining as 0 and doesn't call us back re-entrantly.
355 buf_remaining -= words_to_transfer * BYTES_PER_FIFO_WORD;
356 tx_fifo_avail -= words_to_transfer;
357 i2c_dev->msg_buf_remaining = buf_remaining;
358 i2c_dev->msg_buf = buf +
359 words_to_transfer * BYTES_PER_FIFO_WORD;
362 i2c_writesl(i2c_dev, buf, I2C_TX_FIFO, words_to_transfer);
364 buf += words_to_transfer * BYTES_PER_FIFO_WORD;
368 * If there is a partial word at the end of buf, handle it manually to
369 * prevent reading past the end of buf, which could cross a page
370 * boundary and fault.
372 if (tx_fifo_avail > 0 && buf_remaining > 0) {
373 BUG_ON(buf_remaining > 3);
374 memcpy(&val, buf, buf_remaining);
375 val = le32_to_cpu(val);
377 /* Again update before writing to FIFO to make sure isr sees. */
378 i2c_dev->msg_buf_remaining = 0;
379 i2c_dev->msg_buf = NULL;
382 i2c_writel(i2c_dev, val, I2C_TX_FIFO);
389 * One of the Tegra I2C blocks is inside the DVC (Digital Voltage Controller)
390 * block. This block is identical to the rest of the I2C blocks, except that
391 * it only supports master mode, it has registers moved around, and it needs
392 * some extra init to get it into I2C mode. The register moves are handled
393 * by i2c_readl and i2c_writel
395 static void tegra_dvc_init(struct tegra_i2c_dev *i2c_dev)
399 val = dvc_readl(i2c_dev, DVC_CTRL_REG3);
400 val |= DVC_CTRL_REG3_SW_PROG;
401 val |= DVC_CTRL_REG3_I2C_DONE_INTR_EN;
402 dvc_writel(i2c_dev, val, DVC_CTRL_REG3);
404 val = dvc_readl(i2c_dev, DVC_CTRL_REG1);
405 val |= DVC_CTRL_REG1_INTR_EN;
406 dvc_writel(i2c_dev, val, DVC_CTRL_REG1);
409 static int tegra_i2c_runtime_resume(struct device *dev)
411 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
414 ret = pinctrl_pm_select_default_state(i2c_dev->dev);
418 if (!i2c_dev->hw->has_single_clk_source) {
419 ret = clk_enable(i2c_dev->fast_clk);
421 dev_err(i2c_dev->dev,
422 "Enabling fast clk failed, err %d\n", ret);
427 ret = clk_enable(i2c_dev->div_clk);
429 dev_err(i2c_dev->dev,
430 "Enabling div clk failed, err %d\n", ret);
431 clk_disable(i2c_dev->fast_clk);
438 static int tegra_i2c_runtime_suspend(struct device *dev)
440 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);
446 return pinctrl_pm_select_idle_state(i2c_dev->dev);
449 static int tegra_i2c_wait_for_config_load(struct tegra_i2c_dev *i2c_dev)
451 unsigned long reg_offset;
456 if (i2c_dev->hw->has_config_load_reg) {
457 reg_offset = tegra_i2c_reg_addr(i2c_dev, I2C_CONFIG_LOAD);
458 addr = i2c_dev->base + reg_offset;
459 i2c_writel(i2c_dev, I2C_MSTR_CONFIG_LOAD, I2C_CONFIG_LOAD);
461 err = readl_poll_timeout_atomic(addr, val, val == 0,
462 1000, I2C_CONFIG_LOAD_TIMEOUT);
464 err = readl_poll_timeout(addr, val, val == 0,
465 1000, I2C_CONFIG_LOAD_TIMEOUT);
468 dev_warn(i2c_dev->dev,
469 "timeout waiting for config load\n");
477 static int tegra_i2c_init(struct tegra_i2c_dev *i2c_dev)
483 err = pm_runtime_get_sync(i2c_dev->dev);
485 dev_err(i2c_dev->dev, "runtime resume failed %d\n", err);
489 reset_control_assert(i2c_dev->rst);
491 reset_control_deassert(i2c_dev->rst);
494 tegra_dvc_init(i2c_dev);
496 val = I2C_CNFG_NEW_MASTER_FSM | I2C_CNFG_PACKET_MODE_EN |
497 (0x2 << I2C_CNFG_DEBOUNCE_CNT_SHIFT);
499 if (i2c_dev->hw->has_multi_master_mode)
500 val |= I2C_CNFG_MULTI_MASTER_MODE;
502 i2c_writel(i2c_dev, val, I2C_CNFG);
503 i2c_writel(i2c_dev, 0, I2C_INT_MASK);
505 /* Make sure clock divisor programmed correctly */
506 clk_divisor = i2c_dev->hw->clk_divisor_hs_mode;
507 clk_divisor |= i2c_dev->clk_divisor_non_hs_mode <<
508 I2C_CLK_DIVISOR_STD_FAST_MODE_SHIFT;
509 i2c_writel(i2c_dev, clk_divisor, I2C_CLK_DIVISOR);
511 if (!i2c_dev->is_dvc) {
512 u32 sl_cfg = i2c_readl(i2c_dev, I2C_SL_CNFG);
514 sl_cfg |= I2C_SL_CNFG_NACK | I2C_SL_CNFG_NEWSL;
515 i2c_writel(i2c_dev, sl_cfg, I2C_SL_CNFG);
516 i2c_writel(i2c_dev, 0xfc, I2C_SL_ADDR1);
517 i2c_writel(i2c_dev, 0x00, I2C_SL_ADDR2);
520 val = 7 << I2C_FIFO_CONTROL_TX_TRIG_SHIFT |
521 0 << I2C_FIFO_CONTROL_RX_TRIG_SHIFT;
522 i2c_writel(i2c_dev, val, I2C_FIFO_CONTROL);
524 err = tegra_i2c_flush_fifos(i2c_dev);
528 if (i2c_dev->is_multimaster_mode && i2c_dev->hw->has_slcg_override_reg)
529 i2c_writel(i2c_dev, I2C_MST_CORE_CLKEN_OVR, I2C_CLKEN_OVERRIDE);
531 err = tegra_i2c_wait_for_config_load(i2c_dev);
535 if (i2c_dev->irq_disabled) {
536 i2c_dev->irq_disabled = 0;
537 enable_irq(i2c_dev->irq);
541 pm_runtime_put(i2c_dev->dev);
545 static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
548 const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
549 struct tegra_i2c_dev *i2c_dev = dev_id;
551 status = i2c_readl(i2c_dev, I2C_INT_STATUS);
554 dev_warn(i2c_dev->dev, "irq status 0 %08x %08x %08x\n",
555 i2c_readl(i2c_dev, I2C_PACKET_TRANSFER_STATUS),
556 i2c_readl(i2c_dev, I2C_STATUS),
557 i2c_readl(i2c_dev, I2C_CNFG));
558 i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
560 if (!i2c_dev->irq_disabled) {
561 disable_irq_nosync(i2c_dev->irq);
562 i2c_dev->irq_disabled = 1;
567 if (unlikely(status & status_err)) {
568 if (status & I2C_INT_NO_ACK)
569 i2c_dev->msg_err |= I2C_ERR_NO_ACK;
570 if (status & I2C_INT_ARBITRATION_LOST)
571 i2c_dev->msg_err |= I2C_ERR_ARBITRATION_LOST;
575 if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
576 if (i2c_dev->msg_buf_remaining)
577 tegra_i2c_empty_rx_fifo(i2c_dev);
582 if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
583 if (i2c_dev->msg_buf_remaining)
584 tegra_i2c_fill_tx_fifo(i2c_dev);
586 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
589 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
591 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
593 if (status & I2C_INT_PACKET_XFER_COMPLETE) {
594 BUG_ON(i2c_dev->msg_buf_remaining);
595 complete(&i2c_dev->msg_complete);
599 /* An error occurred, mask all interrupts */
600 tegra_i2c_mask_irq(i2c_dev, I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST |
601 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
602 I2C_INT_RX_FIFO_DATA_REQ);
603 i2c_writel(i2c_dev, status, I2C_INT_STATUS);
605 dvc_writel(i2c_dev, DVC_STATUS_I2C_DONE_INTR, DVC_STATUS);
607 complete(&i2c_dev->msg_complete);
611 static int tegra_i2c_xfer_msg(struct tegra_i2c_dev *i2c_dev,
612 struct i2c_msg *msg, enum msg_end_type end_state)
616 unsigned long time_left;
618 tegra_i2c_flush_fifos(i2c_dev);
623 i2c_dev->msg_buf = msg->buf;
624 i2c_dev->msg_buf_remaining = msg->len;
625 i2c_dev->msg_err = I2C_ERR_NONE;
626 i2c_dev->msg_read = (msg->flags & I2C_M_RD);
627 reinit_completion(&i2c_dev->msg_complete);
629 packet_header = (0 << PACKET_HEADER0_HEADER_SIZE_SHIFT) |
630 PACKET_HEADER0_PROTOCOL_I2C |
631 (i2c_dev->cont_id << PACKET_HEADER0_CONT_ID_SHIFT) |
632 (1 << PACKET_HEADER0_PACKET_ID_SHIFT);
633 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
635 packet_header = msg->len - 1;
636 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
638 packet_header = I2C_HEADER_IE_ENABLE;
639 if (end_state == MSG_END_CONTINUE)
640 packet_header |= I2C_HEADER_CONTINUE_XFER;
641 else if (end_state == MSG_END_REPEAT_START)
642 packet_header |= I2C_HEADER_REPEAT_START;
643 if (msg->flags & I2C_M_TEN) {
644 packet_header |= msg->addr;
645 packet_header |= I2C_HEADER_10BIT_ADDR;
647 packet_header |= msg->addr << I2C_HEADER_SLAVE_ADDR_SHIFT;
649 if (msg->flags & I2C_M_IGNORE_NAK)
650 packet_header |= I2C_HEADER_CONT_ON_NAK;
651 if (msg->flags & I2C_M_RD)
652 packet_header |= I2C_HEADER_READ;
653 i2c_writel(i2c_dev, packet_header, I2C_TX_FIFO);
655 if (!(msg->flags & I2C_M_RD))
656 tegra_i2c_fill_tx_fifo(i2c_dev);
658 int_mask = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
659 if (i2c_dev->hw->has_per_pkt_xfer_complete_irq)
660 int_mask |= I2C_INT_PACKET_XFER_COMPLETE;
661 if (msg->flags & I2C_M_RD)
662 int_mask |= I2C_INT_RX_FIFO_DATA_REQ;
663 else if (i2c_dev->msg_buf_remaining)
664 int_mask |= I2C_INT_TX_FIFO_DATA_REQ;
665 tegra_i2c_unmask_irq(i2c_dev, int_mask);
666 dev_dbg(i2c_dev->dev, "unmasked irq: %02x\n",
667 i2c_readl(i2c_dev, I2C_INT_MASK));
669 time_left = wait_for_completion_timeout(&i2c_dev->msg_complete,
671 tegra_i2c_mask_irq(i2c_dev, int_mask);
673 if (time_left == 0) {
674 dev_err(i2c_dev->dev, "i2c transfer timed out\n");
676 tegra_i2c_init(i2c_dev);
680 dev_dbg(i2c_dev->dev, "transfer complete: %lu %d %d\n",
681 time_left, completion_done(&i2c_dev->msg_complete),
684 if (likely(i2c_dev->msg_err == I2C_ERR_NONE))
688 * NACK interrupt is generated before the I2C controller generates
689 * the STOP condition on the bus. So wait for 2 clock periods
690 * before resetting the controller so that the STOP condition has
691 * been delivered properly.
693 if (i2c_dev->msg_err == I2C_ERR_NO_ACK)
694 udelay(DIV_ROUND_UP(2 * 1000000, i2c_dev->bus_clk_rate));
696 tegra_i2c_init(i2c_dev);
697 if (i2c_dev->msg_err == I2C_ERR_NO_ACK) {
698 if (msg->flags & I2C_M_IGNORE_NAK)
706 static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
709 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
713 if (i2c_dev->is_suspended)
716 ret = pm_runtime_get_sync(i2c_dev->dev);
718 dev_err(i2c_dev->dev, "runtime resume failed %d\n", ret);
722 for (i = 0; i < num; i++) {
723 enum msg_end_type end_type = MSG_END_STOP;
726 if (msgs[i + 1].flags & I2C_M_NOSTART)
727 end_type = MSG_END_CONTINUE;
729 end_type = MSG_END_REPEAT_START;
731 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
736 pm_runtime_put(i2c_dev->dev);
741 static u32 tegra_i2c_func(struct i2c_adapter *adap)
743 struct tegra_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
744 u32 ret = I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
745 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
747 if (i2c_dev->hw->has_continue_xfer_support)
748 ret |= I2C_FUNC_NOSTART;
752 static void tegra_i2c_parse_dt(struct tegra_i2c_dev *i2c_dev)
754 struct device_node *np = i2c_dev->dev->of_node;
757 ret = of_property_read_u32(np, "clock-frequency",
758 &i2c_dev->bus_clk_rate);
760 i2c_dev->bus_clk_rate = 100000; /* default clock rate */
762 i2c_dev->is_multimaster_mode = of_property_read_bool(np,
766 static const struct i2c_algorithm tegra_i2c_algo = {
767 .master_xfer = tegra_i2c_xfer,
768 .functionality = tegra_i2c_func,
771 /* payload size is only 12 bit */
772 static struct i2c_adapter_quirks tegra_i2c_quirks = {
773 .max_read_len = 4096,
774 .max_write_len = 4096,
777 static const struct tegra_i2c_hw_feature tegra20_i2c_hw = {
778 .has_continue_xfer_support = false,
779 .has_per_pkt_xfer_complete_irq = false,
780 .has_single_clk_source = false,
781 .clk_divisor_hs_mode = 3,
782 .clk_divisor_std_fast_mode = 0,
783 .clk_divisor_fast_plus_mode = 0,
784 .has_config_load_reg = false,
785 .has_multi_master_mode = false,
786 .has_slcg_override_reg = false,
789 static const struct tegra_i2c_hw_feature tegra30_i2c_hw = {
790 .has_continue_xfer_support = true,
791 .has_per_pkt_xfer_complete_irq = false,
792 .has_single_clk_source = false,
793 .clk_divisor_hs_mode = 3,
794 .clk_divisor_std_fast_mode = 0,
795 .clk_divisor_fast_plus_mode = 0,
796 .has_config_load_reg = false,
797 .has_multi_master_mode = false,
798 .has_slcg_override_reg = false,
801 static const struct tegra_i2c_hw_feature tegra114_i2c_hw = {
802 .has_continue_xfer_support = true,
803 .has_per_pkt_xfer_complete_irq = true,
804 .has_single_clk_source = true,
805 .clk_divisor_hs_mode = 1,
806 .clk_divisor_std_fast_mode = 0x19,
807 .clk_divisor_fast_plus_mode = 0x10,
808 .has_config_load_reg = false,
809 .has_multi_master_mode = false,
810 .has_slcg_override_reg = false,
813 static const struct tegra_i2c_hw_feature tegra124_i2c_hw = {
814 .has_continue_xfer_support = true,
815 .has_per_pkt_xfer_complete_irq = true,
816 .has_single_clk_source = true,
817 .clk_divisor_hs_mode = 1,
818 .clk_divisor_std_fast_mode = 0x19,
819 .clk_divisor_fast_plus_mode = 0x10,
820 .has_config_load_reg = true,
821 .has_multi_master_mode = false,
822 .has_slcg_override_reg = true,
825 static const struct tegra_i2c_hw_feature tegra210_i2c_hw = {
826 .has_continue_xfer_support = true,
827 .has_per_pkt_xfer_complete_irq = true,
828 .has_single_clk_source = true,
829 .clk_divisor_hs_mode = 1,
830 .clk_divisor_std_fast_mode = 0x19,
831 .clk_divisor_fast_plus_mode = 0x10,
832 .has_config_load_reg = true,
833 .has_multi_master_mode = true,
834 .has_slcg_override_reg = true,
837 /* Match table for of_platform binding */
838 static const struct of_device_id tegra_i2c_of_match[] = {
839 { .compatible = "nvidia,tegra210-i2c", .data = &tegra210_i2c_hw, },
840 { .compatible = "nvidia,tegra124-i2c", .data = &tegra124_i2c_hw, },
841 { .compatible = "nvidia,tegra114-i2c", .data = &tegra114_i2c_hw, },
842 { .compatible = "nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
843 { .compatible = "nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
844 { .compatible = "nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
847 MODULE_DEVICE_TABLE(of, tegra_i2c_of_match);
849 static int tegra_i2c_probe(struct platform_device *pdev)
851 struct tegra_i2c_dev *i2c_dev;
852 struct resource *res;
854 struct clk *fast_clk;
858 int clk_multiplier = I2C_CLK_MULTIPLIER_STD_FAST_MODE;
860 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
861 base = devm_ioremap_resource(&pdev->dev, res);
863 return PTR_ERR(base);
865 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
867 dev_err(&pdev->dev, "no irq resource\n");
872 div_clk = devm_clk_get(&pdev->dev, "div-clk");
873 if (IS_ERR(div_clk)) {
874 dev_err(&pdev->dev, "missing controller clock\n");
875 return PTR_ERR(div_clk);
878 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
882 i2c_dev->base = base;
883 i2c_dev->div_clk = div_clk;
884 i2c_dev->adapter.algo = &tegra_i2c_algo;
885 i2c_dev->adapter.quirks = &tegra_i2c_quirks;
887 i2c_dev->cont_id = pdev->id;
888 i2c_dev->dev = &pdev->dev;
890 i2c_dev->rst = devm_reset_control_get(&pdev->dev, "i2c");
891 if (IS_ERR(i2c_dev->rst)) {
892 dev_err(&pdev->dev, "missing controller reset\n");
893 return PTR_ERR(i2c_dev->rst);
896 tegra_i2c_parse_dt(i2c_dev);
898 i2c_dev->hw = of_device_get_match_data(&pdev->dev);
899 i2c_dev->is_dvc = of_device_is_compatible(pdev->dev.of_node,
900 "nvidia,tegra20-i2c-dvc");
901 init_completion(&i2c_dev->msg_complete);
903 if (!i2c_dev->hw->has_single_clk_source) {
904 fast_clk = devm_clk_get(&pdev->dev, "fast-clk");
905 if (IS_ERR(fast_clk)) {
906 dev_err(&pdev->dev, "missing fast clock\n");
907 return PTR_ERR(fast_clk);
909 i2c_dev->fast_clk = fast_clk;
912 platform_set_drvdata(pdev, i2c_dev);
914 if (!i2c_dev->hw->has_single_clk_source) {
915 ret = clk_prepare(i2c_dev->fast_clk);
917 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
922 i2c_dev->clk_divisor_non_hs_mode =
923 i2c_dev->hw->clk_divisor_std_fast_mode;
924 if (i2c_dev->hw->clk_divisor_fast_plus_mode &&
925 (i2c_dev->bus_clk_rate == 1000000))
926 i2c_dev->clk_divisor_non_hs_mode =
927 i2c_dev->hw->clk_divisor_fast_plus_mode;
929 clk_multiplier *= (i2c_dev->clk_divisor_non_hs_mode + 1);
930 ret = clk_set_rate(i2c_dev->div_clk,
931 i2c_dev->bus_clk_rate * clk_multiplier);
933 dev_err(i2c_dev->dev, "Clock rate change failed %d\n", ret);
934 goto unprepare_fast_clk;
937 ret = clk_prepare(i2c_dev->div_clk);
939 dev_err(i2c_dev->dev, "Clock prepare failed %d\n", ret);
940 goto unprepare_fast_clk;
943 pm_runtime_enable(&pdev->dev);
944 if (!pm_runtime_enabled(&pdev->dev)) {
945 ret = tegra_i2c_runtime_resume(&pdev->dev);
947 dev_err(&pdev->dev, "runtime resume failed\n");
948 goto unprepare_div_clk;
952 if (i2c_dev->is_multimaster_mode) {
953 ret = clk_enable(i2c_dev->div_clk);
955 dev_err(i2c_dev->dev, "div_clk enable failed %d\n",
961 ret = tegra_i2c_init(i2c_dev);
963 dev_err(&pdev->dev, "Failed to initialize i2c controller\n");
964 goto disable_div_clk;
967 ret = devm_request_irq(&pdev->dev, i2c_dev->irq,
968 tegra_i2c_isr, 0, dev_name(&pdev->dev), i2c_dev);
970 dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq);
971 goto disable_div_clk;
974 i2c_set_adapdata(&i2c_dev->adapter, i2c_dev);
975 i2c_dev->adapter.owner = THIS_MODULE;
976 i2c_dev->adapter.class = I2C_CLASS_DEPRECATED;
977 strlcpy(i2c_dev->adapter.name, dev_name(&pdev->dev),
978 sizeof(i2c_dev->adapter.name));
979 i2c_dev->adapter.dev.parent = &pdev->dev;
980 i2c_dev->adapter.nr = pdev->id;
981 i2c_dev->adapter.dev.of_node = pdev->dev.of_node;
983 ret = i2c_add_numbered_adapter(&i2c_dev->adapter);
985 goto disable_div_clk;
990 if (i2c_dev->is_multimaster_mode)
991 clk_disable(i2c_dev->div_clk);
994 pm_runtime_disable(&pdev->dev);
995 if (!pm_runtime_status_suspended(&pdev->dev))
996 tegra_i2c_runtime_suspend(&pdev->dev);
999 clk_unprepare(i2c_dev->div_clk);
1002 if (!i2c_dev->hw->has_single_clk_source)
1003 clk_unprepare(i2c_dev->fast_clk);
1008 static int tegra_i2c_remove(struct platform_device *pdev)
1010 struct tegra_i2c_dev *i2c_dev = platform_get_drvdata(pdev);
1012 i2c_del_adapter(&i2c_dev->adapter);
1014 if (i2c_dev->is_multimaster_mode)
1015 clk_disable(i2c_dev->div_clk);
1017 pm_runtime_disable(&pdev->dev);
1018 if (!pm_runtime_status_suspended(&pdev->dev))
1019 tegra_i2c_runtime_suspend(&pdev->dev);
1021 clk_unprepare(i2c_dev->div_clk);
1022 if (!i2c_dev->hw->has_single_clk_source)
1023 clk_unprepare(i2c_dev->fast_clk);
1028 #ifdef CONFIG_PM_SLEEP
1029 static int tegra_i2c_suspend(struct device *dev)
1031 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1033 i2c_lock_adapter(&i2c_dev->adapter);
1034 i2c_dev->is_suspended = true;
1035 i2c_unlock_adapter(&i2c_dev->adapter);
1040 static int tegra_i2c_resume(struct device *dev)
1042 struct tegra_i2c_dev *i2c_dev = dev_get_drvdata(dev);
1045 i2c_lock_adapter(&i2c_dev->adapter);
1047 ret = tegra_i2c_init(i2c_dev);
1049 i2c_dev->is_suspended = false;
1051 i2c_unlock_adapter(&i2c_dev->adapter);
1056 static const struct dev_pm_ops tegra_i2c_pm = {
1057 SET_RUNTIME_PM_OPS(tegra_i2c_runtime_suspend, tegra_i2c_runtime_resume,
1059 SET_SYSTEM_SLEEP_PM_OPS(tegra_i2c_suspend, tegra_i2c_resume)
1061 #define TEGRA_I2C_PM (&tegra_i2c_pm)
1063 #define TEGRA_I2C_PM NULL
1066 static struct platform_driver tegra_i2c_driver = {
1067 .probe = tegra_i2c_probe,
1068 .remove = tegra_i2c_remove,
1070 .name = "tegra-i2c",
1071 .of_match_table = tegra_i2c_of_match,
1076 static int __init tegra_i2c_init_driver(void)
1078 return platform_driver_register(&tegra_i2c_driver);
1081 static void __exit tegra_i2c_exit_driver(void)
1083 platform_driver_unregister(&tegra_i2c_driver);
1086 subsys_initcall(tegra_i2c_init_driver);
1087 module_exit(tegra_i2c_exit_driver);
1089 MODULE_DESCRIPTION("nVidia Tegra2 I2C Bus Controller driver");
1090 MODULE_AUTHOR("Colin Cross");
1091 MODULE_LICENSE("GPL v2");