18 #include <linux/kernel.h>
23 #include <linux/i2c.h>
27 #include <linux/slab.h>
31 #include <linux/module.h>
33 #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 (1<<10)
43 #define I2C_CNFG_NEW_MASTER_FSM (1<<11)
44 #define I2C_STATUS 0x01C
45 #define I2C_SL_CNFG 0x020
46 #define I2C_SL_CNFG_NACK (1<<1)
47 #define I2C_SL_CNFG_NEWSL (1<<2)
48 #define I2C_SL_ADDR1 0x02c
49 #define I2C_SL_ADDR2 0x030
50 #define I2C_TX_FIFO 0x050
51 #define I2C_RX_FIFO 0x054
52 #define I2C_PACKET_TRANSFER_STATUS 0x058
53 #define I2C_FIFO_CONTROL 0x05c
54 #define I2C_FIFO_CONTROL_TX_FLUSH (1<<1)
55 #define I2C_FIFO_CONTROL_RX_FLUSH (1<<0)
56 #define I2C_FIFO_CONTROL_TX_TRIG_SHIFT 5
57 #define I2C_FIFO_CONTROL_RX_TRIG_SHIFT 2
58 #define I2C_FIFO_STATUS 0x060
59 #define I2C_FIFO_STATUS_TX_MASK 0xF0
60 #define I2C_FIFO_STATUS_TX_SHIFT 4
61 #define I2C_FIFO_STATUS_RX_MASK 0x0F
62 #define I2C_FIFO_STATUS_RX_SHIFT 0
63 #define I2C_INT_MASK 0x064
64 #define I2C_INT_STATUS 0x068
65 #define I2C_INT_PACKET_XFER_COMPLETE (1<<7)
66 #define I2C_INT_ALL_PACKETS_XFER_COMPLETE (1<<6)
67 #define I2C_INT_TX_FIFO_OVERFLOW (1<<5)
68 #define I2C_INT_RX_FIFO_UNDERFLOW (1<<4)
69 #define I2C_INT_NO_ACK (1<<3)
70 #define I2C_INT_ARBITRATION_LOST (1<<2)
71 #define I2C_INT_TX_FIFO_DATA_REQ (1<<1)
72 #define I2C_INT_RX_FIFO_DATA_REQ (1<<0)
73 #define I2C_CLK_DIVISOR 0x06c
75 #define DVC_CTRL_REG1 0x000
76 #define DVC_CTRL_REG1_INTR_EN (1<<10)
77 #define DVC_CTRL_REG2 0x004
78 #define DVC_CTRL_REG3 0x008
79 #define DVC_CTRL_REG3_SW_PROG (1<<26)
80 #define DVC_CTRL_REG3_I2C_DONE_INTR_EN (1<<30)
81 #define DVC_STATUS 0x00c
82 #define DVC_STATUS_I2C_DONE_INTR (1<<30)
84 #define I2C_ERR_NONE 0x00
85 #define I2C_ERR_NO_ACK 0x01
86 #define I2C_ERR_ARBITRATION_LOST 0x02
87 #define I2C_ERR_UNKNOWN_INTERRUPT 0x04
89 #define PACKET_HEADER0_HEADER_SIZE_SHIFT 28
90 #define PACKET_HEADER0_PACKET_ID_SHIFT 16
91 #define PACKET_HEADER0_CONT_ID_SHIFT 12
92 #define PACKET_HEADER0_PROTOCOL_I2C (1<<4)
94 #define I2C_HEADER_HIGHSPEED_MODE (1<<22)
95 #define I2C_HEADER_CONT_ON_NAK (1<<21)
96 #define I2C_HEADER_SEND_START_BYTE (1<<20)
97 #define I2C_HEADER_READ (1<<19)
98 #define I2C_HEADER_10BIT_ADDR (1<<18)
99 #define I2C_HEADER_IE_ENABLE (1<<17)
100 #define I2C_HEADER_REPEAT_START (1<<16)
101 #define I2C_HEADER_CONTINUE_XFER (1<<15)
102 #define I2C_HEADER_MASTER_ADDR_SHIFT 12
103 #define I2C_HEADER_SLAVE_ADDR_SHIFT 1
190 writel(val, i2c_dev->
base + tegra_i2c_reg_addr(i2c_dev, reg));
194 readl(i2c_dev->
base + tegra_i2c_reg_addr(i2c_dev, reg));
199 return readl(i2c_dev->
base + tegra_i2c_reg_addr(i2c_dev, reg));
203 unsigned long reg,
int len)
205 writesl(i2c_dev->
base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
208 static void i2c_readsl(
struct tegra_i2c_dev *i2c_dev,
void *data,
209 unsigned long reg,
int len)
211 readsl(i2c_dev->
base + tegra_i2c_reg_addr(i2c_dev, reg), data, len);
228 static int tegra_i2c_flush_fifos(
struct tegra_i2c_dev *i2c_dev)
238 dev_warn(i2c_dev->
dev,
"timeout waiting for fifo flush\n");
246 static int tegra_i2c_empty_rx_fifo(
struct tegra_i2c_dev *i2c_dev)
252 int words_to_transfer;
260 if (words_to_transfer > rx_fifo_avail)
261 words_to_transfer = rx_fifo_avail;
263 i2c_readsl(i2c_dev, buf,
I2C_RX_FIFO, words_to_transfer);
267 rx_fifo_avail -= words_to_transfer;
273 if (rx_fifo_avail > 0 && buf_remaining > 0) {
274 BUG_ON(buf_remaining > 3);
276 memcpy(buf, &val, buf_remaining);
281 BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
287 static int tegra_i2c_fill_tx_fifo(
struct tegra_i2c_dev *i2c_dev)
293 int words_to_transfer;
303 if (words_to_transfer) {
304 if (words_to_transfer > tx_fifo_avail)
305 words_to_transfer = tx_fifo_avail;
315 tx_fifo_avail -= words_to_transfer;
321 i2c_writesl(i2c_dev, buf,
I2C_TX_FIFO, words_to_transfer);
331 if (tx_fifo_avail > 0 && buf_remaining > 0) {
332 BUG_ON(buf_remaining > 3);
333 memcpy(&val, buf, buf_remaining);
366 static inline int tegra_i2c_clock_enable(
struct tegra_i2c_dev *i2c_dev)
369 ret = clk_prepare_enable(i2c_dev->
fast_clk);
372 "Enabling fast clk failed, err %d\n", ret);
375 ret = clk_prepare_enable(i2c_dev->
div_clk);
378 "Enabling div clk failed, err %d\n", ret);
379 clk_disable_unprepare(i2c_dev->
fast_clk);
384 static inline void tegra_i2c_clock_disable(
struct tegra_i2c_dev *i2c_dev)
386 clk_disable_unprepare(i2c_dev->
div_clk);
387 clk_disable_unprepare(i2c_dev->
fast_clk);
395 tegra_i2c_clock_enable(i2c_dev);
402 tegra_dvc_init(i2c_dev);
423 if (tegra_i2c_flush_fifos(i2c_dev))
426 tegra_i2c_clock_disable(i2c_dev);
445 dev_warn(i2c_dev->
dev,
"irq status 0 %08x %08x %08x\n",
458 if (
unlikely(status & status_err)) {
468 tegra_i2c_empty_rx_fifo(i2c_dev);
475 tegra_i2c_fill_tx_fifo(i2c_dev);
477 tegra_i2c_mask_irq(i2c_dev, I2C_INT_TX_FIFO_DATA_REQ);
492 I2C_INT_PACKET_XFER_COMPLETE | I2C_INT_TX_FIFO_DATA_REQ |
493 I2C_INT_RX_FIFO_DATA_REQ);
509 tegra_i2c_flush_fifos(i2c_dev);
526 packet_header = msg->
len - 1;
535 packet_header |= msg->
addr;
547 tegra_i2c_fill_tx_fifo(i2c_dev);
554 tegra_i2c_unmask_irq(i2c_dev, int_mask);
559 tegra_i2c_mask_irq(i2c_dev, int_mask);
562 dev_err(i2c_dev->
dev,
"i2c transfer timed out\n");
564 tegra_i2c_init(i2c_dev);
568 dev_dbg(i2c_dev->
dev,
"transfer complete: %d %d %d\n",
582 tegra_i2c_init(i2c_dev);
602 tegra_i2c_clock_enable(i2c_dev);
603 for (i = 0; i < num; i++) {
611 ret = tegra_i2c_xfer_msg(i2c_dev, &msgs[i], end_type);
615 tegra_i2c_clock_disable(i2c_dev);
625 if (i2c_dev->
hw->has_continue_xfer_support)
631 .master_xfer = tegra_i2c_xfer,
632 .functionality = tegra_i2c_func,
636 .has_continue_xfer_support =
false,
640 .has_continue_xfer_support =
true,
643 #if defined(CONFIG_OF)
646 { .compatible =
"nvidia,tegra30-i2c", .data = &tegra30_i2c_hw, },
647 { .compatible =
"nvidia,tegra20-i2c", .data = &tegra20_i2c_hw, },
648 { .compatible =
"nvidia,tegra20-i2c-dvc", .data = &tegra20_i2c_hw, },
660 struct clk *fast_clk;
661 const unsigned int *prop;
674 dev_err(&pdev->
dev,
"Cannot request/ioremap I2C registers\n");
686 if (IS_ERR(div_clk)) {
687 dev_err(&pdev->
dev,
"missing controller clock");
688 return PTR_ERR(div_clk);
692 if (IS_ERR(fast_clk)) {
694 return PTR_ERR(fast_clk);
699 dev_err(&pdev->
dev,
"Could not allocate struct tegra_i2c_dev");
703 i2c_dev->
base = base;
706 i2c_dev->
adapter.algo = &tegra_i2c_algo;
709 i2c_dev->
dev = &pdev->
dev;
715 }
else if (i2c_dev->
dev->of_node) {
717 "clock-frequency",
NULL);
722 i2c_dev->
hw = &tegra20_i2c_hw;
724 if (pdev->
dev.of_node) {
728 i2c_dev->
hw = match->
data;
730 "nvidia,tegra20-i2c-dvc");
731 }
else if (pdev->
id == 3) {
736 platform_set_drvdata(pdev, i2c_dev);
738 ret = tegra_i2c_init(i2c_dev);
740 dev_err(&pdev->
dev,
"Failed to initialize i2c controller");
744 ret = devm_request_irq(&pdev->
dev, i2c_dev->
irq,
745 tegra_i2c_isr, 0, dev_name(&pdev->
dev), i2c_dev);
747 dev_err(&pdev->
dev,
"Failed to request irq %i\n", i2c_dev->
irq);
751 i2c_set_adapdata(&i2c_dev->
adapter, i2c_dev);
755 sizeof(i2c_dev->
adapter.name));
756 i2c_dev->
adapter.algo = &tegra_i2c_algo;
759 i2c_dev->
adapter.dev.of_node = pdev->
dev.of_node;
763 dev_err(&pdev->
dev,
"Failed to add I2C adapter\n");
779 #ifdef CONFIG_PM_SLEEP
780 static int tegra_i2c_suspend(
struct device *
dev)
791 static int tegra_i2c_resume(
struct device *
dev)
798 ret = tegra_i2c_init(i2c_dev);
813 #define TEGRA_I2C_PM (&tegra_i2c_pm)
815 #define TEGRA_I2C_PM NULL
819 .probe = tegra_i2c_probe,
829 static int __init tegra_i2c_init_driver(
void)
834 static void __exit tegra_i2c_exit_driver(
void)