28 #include <linux/export.h>
30 #include <linux/errno.h>
32 #include <linux/i2c.h>
44 #define DW_IC_DATA_CMD 0x10
45 #define DW_IC_SS_SCL_HCNT 0x14
46 #define DW_IC_SS_SCL_LCNT 0x18
47 #define DW_IC_FS_SCL_HCNT 0x1c
48 #define DW_IC_FS_SCL_LCNT 0x20
49 #define DW_IC_INTR_STAT 0x2c
50 #define DW_IC_INTR_MASK 0x30
51 #define DW_IC_RAW_INTR_STAT 0x34
52 #define DW_IC_RX_TL 0x38
53 #define DW_IC_TX_TL 0x3c
54 #define DW_IC_CLR_INTR 0x40
55 #define DW_IC_CLR_RX_UNDER 0x44
56 #define DW_IC_CLR_RX_OVER 0x48
57 #define DW_IC_CLR_TX_OVER 0x4c
58 #define DW_IC_CLR_RD_REQ 0x50
59 #define DW_IC_CLR_TX_ABRT 0x54
60 #define DW_IC_CLR_RX_DONE 0x58
61 #define DW_IC_CLR_ACTIVITY 0x5c
62 #define DW_IC_CLR_STOP_DET 0x60
63 #define DW_IC_CLR_START_DET 0x64
64 #define DW_IC_CLR_GEN_CALL 0x68
65 #define DW_IC_ENABLE 0x6c
66 #define DW_IC_STATUS 0x70
67 #define DW_IC_TXFLR 0x74
68 #define DW_IC_RXFLR 0x78
69 #define DW_IC_TX_ABRT_SOURCE 0x80
70 #define DW_IC_COMP_PARAM_1 0xf4
71 #define DW_IC_COMP_TYPE 0xfc
72 #define DW_IC_COMP_TYPE_VALUE 0x44570140
74 #define DW_IC_INTR_RX_UNDER 0x001
75 #define DW_IC_INTR_RX_OVER 0x002
76 #define DW_IC_INTR_RX_FULL 0x004
77 #define DW_IC_INTR_TX_OVER 0x008
78 #define DW_IC_INTR_TX_EMPTY 0x010
79 #define DW_IC_INTR_RD_REQ 0x020
80 #define DW_IC_INTR_TX_ABRT 0x040
81 #define DW_IC_INTR_RX_DONE 0x080
82 #define DW_IC_INTR_ACTIVITY 0x100
83 #define DW_IC_INTR_STOP_DET 0x200
84 #define DW_IC_INTR_START_DET 0x400
85 #define DW_IC_INTR_GEN_CALL 0x800
87 #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
88 DW_IC_INTR_TX_EMPTY | \
89 DW_IC_INTR_TX_ABRT | \
92 #define DW_IC_STATUS_ACTIVITY 0x1
94 #define DW_IC_ERR_TX_ABRT 0x1
99 #define STATUS_IDLE 0x0
100 #define STATUS_WRITE_IN_PROGRESS 0x1
101 #define STATUS_READ_IN_PROGRESS 0x2
111 #define ABRT_7B_ADDR_NOACK 0
112 #define ABRT_10ADDR1_NOACK 1
113 #define ABRT_10ADDR2_NOACK 2
114 #define ABRT_TXDATA_NOACK 3
115 #define ABRT_GCALL_NOACK 4
116 #define ABRT_GCALL_READ 5
117 #define ABRT_SBYTE_ACKDET 7
118 #define ABRT_SBYTE_NORSTRT 9
119 #define ABRT_10B_RD_NORSTRT 10
120 #define ABRT_MASTER_DIS 11
123 #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
124 #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
125 #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
126 #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
127 #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
128 #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
129 #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
130 #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
131 #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
132 #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
133 #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
135 #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
136 DW_IC_TX_ABRT_10ADDR1_NOACK | \
137 DW_IC_TX_ABRT_10ADDR2_NOACK | \
138 DW_IC_TX_ABRT_TXDATA_NOACK | \
139 DW_IC_TX_ABRT_GCALL_NOACK)
141 static char *abort_sources[] = {
143 "slave address not acknowledged (7bit mode)",
145 "first address byte not acknowledged (10bit mode)",
147 "second address byte not acknowledged (10bit mode)",
149 "data not acknowledged",
151 "no acknowledgement for a general call",
153 "read after general call",
155 "start byte acknowledged",
157 "trying to send start byte when restart is disabled",
159 "trying to read when restart is disabled (10bit mode)",
161 "trying to use disabled adapter",
215 return (ic_clk * tSYMBOL + 5000) / 10000 - 8 +
offset;
231 return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 +
offset;
234 static u32 i2c_dw_scl_lcnt(
u32 ic_clk,
u32 tLOW,
u32 tf,
int offset)
247 return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 +
offset;
274 dev_err(dev->
dev,
"Unknown Synopsys component type: "
285 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
290 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
296 dev_dbg(dev->
dev,
"Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
299 hcnt = i2c_dw_scl_hcnt(input_clock_khz,
304 lcnt = i2c_dw_scl_lcnt(input_clock_khz,
310 dev_dbg(dev->
dev,
"Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
331 dev_warn(dev->
dev,
"timeout waiting for bus ready\n");
341 static void i2c_dw_xfer_init(
struct dw_i2c_dev *dev)
378 int tx_limit, rx_limit;
393 "%s: invalid target address\n", __func__);
400 "%s: invalid message length\n", __func__);
414 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
420 tx_limit--; buf_len--;
470 for (; len > 0 && rx_valid > 0; len--, rx_valid--)
483 static int i2c_dw_handle_tx_abort(
struct dw_i2c_dev *dev)
491 "%
s: %
s\
n", __func__, abort_sources[i]);
496 dev_err(dev->dev, "%
s: %
s\n", __func__, abort_sources[i]);
512 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
515 dev_dbg(dev->
dev,
"%s: msgs: %d\n", __func__, num);
518 pm_runtime_get_sync(dev->
dev);
530 ret = i2c_dw_wait_bus_not_busy(dev);
535 i2c_dw_xfer_init(dev);
562 ret = i2c_dw_handle_tx_abort(dev);
568 pm_runtime_put(dev->
dev);
577 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
582 static u32 i2c_dw_read_clear_intrbits(
struct dw_i2c_dev *dev)
648 dev_dbg(dev->
dev,
"%s: %s enabled= 0x%x stat=0x%x\n", __func__,
649 dev->
adapter.name, enabled, stat);
650 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
653 stat = i2c_dw_read_clear_intrbits(dev);
655 if (stat & DW_IC_INTR_TX_ABRT) {
671 i2c_dw_xfer_msg(dev);
680 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->
msg_err)