blob: 24e8445f5a70d01993e79e4edcf3c9b25ccee469 [file] [log] [blame]
Baruch Siach1ab52cf2009-06-22 16:36:29 +03001/*
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +09002 * Synopsys DesignWare I2C adapter driver (master only).
Baruch Siach1ab52cf2009-06-22 16:36:29 +03003 *
4 * Based on the TI DAVINCI I2C adapter driver.
5 *
6 * Copyright (C) 2006 Texas Instruments.
7 * Copyright (C) 2007 MontaVista Software Inc.
8 * Copyright (C) 2009 Provigent Ltd.
9 *
10 * ----------------------------------------------------------------------------
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
Baruch Siach1ab52cf2009-06-22 16:36:29 +030021 * ----------------------------------------------------------------------------
22 *
23 */
Axel Line68bb912012-09-10 10:14:02 +020024#include <linux/export.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030025#include <linux/errno.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030026#include <linux/err.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010027#include <linux/i2c.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030028#include <linux/interrupt.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030029#include <linux/io.h>
Dirk Brandewie18dbdda2011-10-06 11:26:36 -070030#include <linux/pm_runtime.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010031#include <linux/delay.h>
Mika Westerberg9dd31622013-01-17 12:31:04 +020032#include <linux/module.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010033#include "i2c-designware-core.h"
Shinya Kuribayashice6eb572009-11-06 21:51:57 +090034
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070035/*
36 * Registers offset
37 */
38#define DW_IC_CON 0x0
39#define DW_IC_TAR 0x4
40#define DW_IC_DATA_CMD 0x10
41#define DW_IC_SS_SCL_HCNT 0x14
42#define DW_IC_SS_SCL_LCNT 0x18
43#define DW_IC_FS_SCL_HCNT 0x1c
44#define DW_IC_FS_SCL_LCNT 0x20
Weifeng Voonb6e67142016-08-12 17:02:51 +030045#define DW_IC_HS_SCL_HCNT 0x24
46#define DW_IC_HS_SCL_LCNT 0x28
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070047#define DW_IC_INTR_STAT 0x2c
48#define DW_IC_INTR_MASK 0x30
49#define DW_IC_RAW_INTR_STAT 0x34
50#define DW_IC_RX_TL 0x38
51#define DW_IC_TX_TL 0x3c
52#define DW_IC_CLR_INTR 0x40
53#define DW_IC_CLR_RX_UNDER 0x44
54#define DW_IC_CLR_RX_OVER 0x48
55#define DW_IC_CLR_TX_OVER 0x4c
56#define DW_IC_CLR_RD_REQ 0x50
57#define DW_IC_CLR_TX_ABRT 0x54
58#define DW_IC_CLR_RX_DONE 0x58
59#define DW_IC_CLR_ACTIVITY 0x5c
60#define DW_IC_CLR_STOP_DET 0x60
61#define DW_IC_CLR_START_DET 0x64
62#define DW_IC_CLR_GEN_CALL 0x68
63#define DW_IC_ENABLE 0x6c
64#define DW_IC_STATUS 0x70
65#define DW_IC_TXFLR 0x74
66#define DW_IC_RXFLR 0x78
Christian Ruppert9803f862013-06-26 10:55:06 +020067#define DW_IC_SDA_HOLD 0x7c
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070068#define DW_IC_TX_ABRT_SOURCE 0x80
Mika Westerberg3ca4ed82013-04-10 00:36:40 +000069#define DW_IC_ENABLE_STATUS 0x9c
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070070#define DW_IC_COMP_PARAM_1 0xf4
Christian Ruppert9803f862013-06-26 10:55:06 +020071#define DW_IC_COMP_VERSION 0xf8
72#define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A
Dirk Brandewief3fa9f32011-10-06 11:26:34 -070073#define DW_IC_COMP_TYPE 0xfc
74#define DW_IC_COMP_TYPE_VALUE 0x44570140
75
76#define DW_IC_INTR_RX_UNDER 0x001
77#define DW_IC_INTR_RX_OVER 0x002
78#define DW_IC_INTR_RX_FULL 0x004
79#define DW_IC_INTR_TX_OVER 0x008
80#define DW_IC_INTR_TX_EMPTY 0x010
81#define DW_IC_INTR_RD_REQ 0x020
82#define DW_IC_INTR_TX_ABRT 0x040
83#define DW_IC_INTR_RX_DONE 0x080
84#define DW_IC_INTR_ACTIVITY 0x100
85#define DW_IC_INTR_STOP_DET 0x200
86#define DW_IC_INTR_START_DET 0x400
87#define DW_IC_INTR_GEN_CALL 0x800
88
89#define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
90 DW_IC_INTR_TX_EMPTY | \
91 DW_IC_INTR_TX_ABRT | \
92 DW_IC_INTR_STOP_DET)
93
94#define DW_IC_STATUS_ACTIVITY 0x1
95
96#define DW_IC_ERR_TX_ABRT 0x1
97
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +080098#define DW_IC_TAR_10BITADDR_MASTER BIT(12)
99
Weifeng Voonb6e67142016-08-12 17:02:51 +0300100#define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3))
101#define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2)
102
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700103/*
104 * status codes
105 */
106#define STATUS_IDLE 0x0
107#define STATUS_WRITE_IN_PROGRESS 0x1
108#define STATUS_READ_IN_PROGRESS 0x2
109
110#define TIMEOUT 20 /* ms */
111
112/*
113 * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
114 *
115 * only expected abort codes are listed here
116 * refer to the datasheet for the full list
117 */
118#define ABRT_7B_ADDR_NOACK 0
119#define ABRT_10ADDR1_NOACK 1
120#define ABRT_10ADDR2_NOACK 2
121#define ABRT_TXDATA_NOACK 3
122#define ABRT_GCALL_NOACK 4
123#define ABRT_GCALL_READ 5
124#define ABRT_SBYTE_ACKDET 7
125#define ABRT_SBYTE_NORSTRT 9
126#define ABRT_10B_RD_NORSTRT 10
127#define ABRT_MASTER_DIS 11
128#define ARB_LOST 12
129
130#define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
131#define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
132#define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
133#define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
134#define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
135#define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
136#define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
137#define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
138#define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
139#define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
140#define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
141
142#define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
143 DW_IC_TX_ABRT_10ADDR1_NOACK | \
144 DW_IC_TX_ABRT_10ADDR2_NOACK | \
145 DW_IC_TX_ABRT_TXDATA_NOACK | \
146 DW_IC_TX_ABRT_GCALL_NOACK)
147
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300148static char *abort_sources[] = {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900149 [ABRT_7B_ADDR_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300150 "slave address not acknowledged (7bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900151 [ABRT_10ADDR1_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300152 "first address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900153 [ABRT_10ADDR2_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300154 "second address byte not acknowledged (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900155 [ABRT_TXDATA_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300156 "data not acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900157 [ABRT_GCALL_NOACK] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300158 "no acknowledgement for a general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900159 [ABRT_GCALL_READ] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300160 "read after general call",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900161 [ABRT_SBYTE_ACKDET] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300162 "start byte acknowledged",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900163 [ABRT_SBYTE_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300164 "trying to send start byte when restart is disabled",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900165 [ABRT_10B_RD_NORSTRT] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300166 "trying to read when restart is disabled (10bit mode)",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900167 [ABRT_MASTER_DIS] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300168 "trying to use disabled adapter",
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900169 [ARB_LOST] =
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300170 "lost arbitration",
171};
172
Jarkko Nikula8a437452015-08-31 17:31:31 +0300173static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700174{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200175 u32 value;
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700176
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200177 if (dev->accessor_flags & ACCESS_16BIT)
Jisheng Zhang67105c52014-12-11 14:26:41 +0800178 value = readw_relaxed(dev->base + offset) |
179 (readw_relaxed(dev->base + offset + 2) << 16);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200180 else
Jisheng Zhang67105c52014-12-11 14:26:41 +0800181 value = readl_relaxed(dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200182
183 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700184 return swab32(value);
185 else
186 return value;
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700187}
188
Jarkko Nikula8a437452015-08-31 17:31:31 +0300189static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700190{
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200191 if (dev->accessor_flags & ACCESS_SWAP)
Jean-Hugues Deschenes18c40892011-10-06 11:26:27 -0700192 b = swab32(b);
193
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200194 if (dev->accessor_flags & ACCESS_16BIT) {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800195 writew_relaxed((u16)b, dev->base + offset);
196 writew_relaxed((u16)(b >> 16), dev->base + offset + 2);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200197 } else {
Jisheng Zhang67105c52014-12-11 14:26:41 +0800198 writel_relaxed(b, dev->base + offset);
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200199 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700200}
201
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900202static u32
203i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
204{
205 /*
206 * DesignWare I2C core doesn't seem to have solid strategy to meet
207 * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
208 * will result in violation of the tHD;STA spec.
209 */
210 if (cond)
211 /*
212 * Conditional expression:
213 *
214 * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
215 *
216 * This is based on the DW manuals, and represents an ideal
217 * configuration. The resulting I2C bus speed will be
218 * faster than any of the others.
219 *
220 * If your hardware is free from tHD;STA issue, try this one.
221 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100222 return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900223 else
224 /*
225 * Conditional expression:
226 *
227 * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
228 *
229 * This is just experimental rule; the tHD;STA period turned
230 * out to be proportinal to (_HCNT + 3). With this setting,
231 * we could meet both tHIGH and tHD;STA timing specs.
232 *
233 * If unsure, you'd better to take this alternative.
234 *
235 * The reason why we need to take into account "tf" here,
236 * is the same as described in i2c_dw_scl_lcnt().
237 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100238 return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000
239 - 3 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900240}
241
242static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
243{
244 /*
245 * Conditional expression:
246 *
247 * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
248 *
249 * DW I2C core starts counting the SCL CNTs for the LOW period
250 * of the SCL clock (tLOW) as soon as it pulls the SCL line.
251 * In order to meet the tLOW timing spec, we need to take into
252 * account the fall time of SCL signal (tf). Default tf value
253 * should be 0.3 us, for safety.
254 */
Romain Baeriswyl64682762014-01-20 17:43:43 +0100255 return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset;
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900256}
257
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000258static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable)
259{
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300260 dw_writel(dev, enable, DW_IC_ENABLE);
261}
262
263static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable)
264{
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000265 int timeout = 100;
266
267 do {
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300268 __i2c_dw_enable(dev, enable);
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000269 if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable)
270 return;
271
272 /*
273 * Wait 10 times the signaling period of the highest I2C
274 * transfer supported by the driver (for 400KHz this is
275 * 25us) as described in the DesignWare I2C databook.
276 */
277 usleep_range(25, 250);
278 } while (timeout--);
279
280 dev_warn(dev->dev, "timeout in %sabling adapter\n",
281 enable ? "en" : "dis");
282}
283
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600284static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev)
285{
286 /*
287 * Clock is not necessary if we got LCNT/HCNT values directly from
288 * the platform code.
289 */
290 if (WARN_ON_ONCE(!dev->get_clk_rate_khz))
291 return 0;
292 return dev->get_clk_rate_khz(dev);
293}
294
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300295/**
296 * i2c_dw_init() - initialize the designware i2c master hardware
297 * @dev: device private data
298 *
299 * This functions configures and enables the I2C master.
300 * This function is called during I2C init function, and in case of timeout at
301 * run time.
302 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100303int i2c_dw_init(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300304{
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700305 u32 hcnt, lcnt;
Weifeng Voonb6e67142016-08-12 17:02:51 +0300306 u32 reg, comp_param1;
Romain Baeriswyl64682762014-01-20 17:43:43 +0100307 u32 sda_falling_time, scl_falling_time;
David Boxc0601d22015-01-15 01:12:16 -0800308 int ret;
309
310 if (dev->acquire_lock) {
311 ret = dev->acquire_lock(dev);
312 if (ret) {
313 dev_err(dev->dev, "couldn't acquire bus ownership\n");
314 return ret;
315 }
316 }
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700317
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700318 reg = dw_readl(dev, DW_IC_COMP_TYPE);
319 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
Stefan Roesea8a9f3f2012-04-18 15:01:41 +0200320 /* Configure register endianess access */
321 dev->accessor_flags |= ACCESS_SWAP;
322 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
323 /* Configure register access mode 16bit */
324 dev->accessor_flags |= ACCESS_16BIT;
325 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700326 dev_err(dev->dev, "Unknown Synopsys component type: "
327 "0x%08x\n", reg);
David Boxc0601d22015-01-15 01:12:16 -0800328 if (dev->release_lock)
329 dev->release_lock(dev);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700330 return -ENODEV;
331 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300332
Weifeng Voonb6e67142016-08-12 17:02:51 +0300333 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
334
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300335 /* Disable the adapter */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300336 __i2c_dw_enable_and_wait(dev, false);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300337
338 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900339
Romain Baeriswyl64682762014-01-20 17:43:43 +0100340 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
341 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
342
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200343 /* Set SCL timing parameters for standard-mode */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300344 if (dev->ss_hcnt && dev->ss_lcnt) {
345 hcnt = dev->ss_hcnt;
346 lcnt = dev->ss_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200347 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600348 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200349 4000, /* tHD;STA = tHIGH = 4.0 us */
350 sda_falling_time,
351 0, /* 0: DW default, 1: Ideal */
352 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600353 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200354 4700, /* tLOW = 4.7 us */
355 scl_falling_time,
356 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300357 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700358 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
359 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900360 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
361
Weifeng Voond608c3d2016-08-12 17:02:49 +0300362 /* Set SCL timing parameters for fast-mode or fast-mode plus */
363 if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) {
364 hcnt = dev->fp_hcnt;
365 lcnt = dev->fp_lcnt;
366 } else if (dev->fs_hcnt && dev->fs_lcnt) {
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300367 hcnt = dev->fs_hcnt;
368 lcnt = dev->fs_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200369 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600370 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200371 600, /* tHD;STA = tHIGH = 0.6 us */
372 sda_falling_time,
373 0, /* 0: DW default, 1: Ideal */
374 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600375 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200376 1300, /* tLOW = 1.3 us */
377 scl_falling_time,
378 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300379 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700380 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
381 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900382 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300383
Weifeng Voonb6e67142016-08-12 17:02:51 +0300384 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
385 DW_IC_CON_SPEED_HIGH) {
386 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
387 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
388 dev_err(dev->dev, "High Speed not supported!\n");
389 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
390 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
391 } else if (dev->hs_hcnt && dev->hs_lcnt) {
392 hcnt = dev->hs_hcnt;
393 lcnt = dev->hs_lcnt;
394 dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
395 dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
396 dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
397 hcnt, lcnt);
398 }
399 }
400
Christian Ruppert9803f862013-06-26 10:55:06 +0200401 /* Configure SDA Hold Time if required */
402 if (dev->sda_hold_time) {
403 reg = dw_readl(dev, DW_IC_COMP_VERSION);
404 if (reg >= DW_IC_SDA_HOLD_MIN_VERS)
405 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
406 else
407 dev_warn(dev->dev,
408 "Hardware too old to adjust SDA hold time.");
409 }
410
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900411 /* Configure Tx/Rx FIFO threshold levels */
Andrew Jacksond39f77b2014-11-07 12:10:44 +0000412 dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700413 dw_writel(dev, 0, DW_IC_RX_TL);
Shinya Kuribayashi4cb6d1d2009-11-06 21:48:12 +0900414
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300415 /* configure the i2c master */
Dirk Brandewiee18563f2011-10-06 11:26:32 -0700416 dw_writel(dev, dev->master_cfg , DW_IC_CON);
David Boxc0601d22015-01-15 01:12:16 -0800417
418 if (dev->release_lock)
419 dev->release_lock(dev);
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700420 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300421}
Axel Line68bb912012-09-10 10:14:02 +0200422EXPORT_SYMBOL_GPL(i2c_dw_init);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300423
424/*
425 * Waiting for bus not busy
426 */
427static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
428{
429 int timeout = TIMEOUT;
430
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700431 while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300432 if (timeout <= 0) {
433 dev_warn(dev->dev, "timeout waiting for bus ready\n");
434 return -ETIMEDOUT;
435 }
436 timeout--;
Mika Westerberg1451b912013-04-10 00:36:41 +0000437 usleep_range(1000, 1100);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300438 }
439
440 return 0;
441}
442
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900443static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
444{
445 struct i2c_msg *msgs = dev->msgs;
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800446 u32 ic_con, ic_tar = 0;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900447
448 /* Disable the adapter */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300449 __i2c_dw_enable_and_wait(dev, false);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900450
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900451 /* if the slave address is ten bit address, enable 10BITADDR */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700452 ic_con = dw_readl(dev, DW_IC_CON);
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800453 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900454 ic_con |= DW_IC_CON_10BITADDR_MASTER;
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800455 /*
456 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
457 * mode has to be enabled via bit 12 of IC_TAR register.
458 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
459 * detected from registers.
460 */
461 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
462 } else {
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900463 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800464 }
465
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700466 dw_writel(dev, ic_con, DW_IC_CON);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900467
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800468 /*
469 * Set the slave (target) address and enable 10-bit addressing mode
470 * if applicable.
471 */
472 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
473
Du, Wenkai47bb27e2014-04-10 23:03:19 +0000474 /* enforce disabled interrupts (due to HW issues) */
475 i2c_dw_disable_int(dev);
476
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900477 /* Enable the adapter */
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000478 __i2c_dw_enable(dev, true);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900479
Mika Westerberg2a2d95e2013-05-13 00:54:30 +0000480 /* Clear and enable interrupts */
Jarkko Nikulac3356312015-08-31 17:31:28 +0300481 dw_readl(dev, DW_IC_CLR_INTR);
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700482 dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900483}
484
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300485/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900486 * Initiate (and continue) low level master read/write transaction.
487 * This function is only called from i2c_dw_isr, and pumping i2c_msg
488 * messages into the tx buffer. Even if the size of i2c_msg data is
489 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300490 */
Jean Delvarebccd7802012-10-05 22:23:53 +0200491static void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900492i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300493{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300494 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900495 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900496 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900497 u32 addr = msgs[dev->msg_write_idx].addr;
498 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700499 u8 *buf = dev->tx_buf;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800500 bool need_restart = false;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300501
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900502 intr_mask = DW_IC_INTR_DEFAULT_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900503
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900504 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900505 /*
506 * if target address has changed, we need to
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300507 * reprogram the target address in the i2c
508 * adapter when we are done with this transfer
509 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900510 if (msgs[dev->msg_write_idx].addr != addr) {
511 dev_err(dev->dev,
512 "%s: invalid target address\n", __func__);
513 dev->msg_err = -EINVAL;
514 break;
515 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300516
517 if (msgs[dev->msg_write_idx].len == 0) {
518 dev_err(dev->dev,
519 "%s: invalid message length\n", __func__);
520 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900521 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300522 }
523
524 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
525 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900526 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300527 buf_len = msgs[dev->msg_write_idx].len;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800528
529 /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
530 * IC_RESTART_EN are set, we must manually
531 * set restart bit between messages.
532 */
533 if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
534 (dev->msg_write_idx > 0))
535 need_restart = true;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300536 }
537
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700538 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
539 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900540
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300541 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
Mika Westerberg17a76b42013-01-17 12:31:05 +0200542 u32 cmd = 0;
543
544 /*
545 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
546 * manually set the stop bit. However, it cannot be
547 * detected from the registers so we set it always
548 * when writing/reading the last byte.
549 */
550 if (dev->msg_write_idx == dev->msgs_num - 1 &&
551 buf_len == 1)
552 cmd |= BIT(9);
553
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800554 if (need_restart) {
555 cmd |= BIT(10);
556 need_restart = false;
557 }
558
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300559 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100560
561 /* avoid rx buffer overrun */
562 if (rx_limit - dev->rx_outstanding <= 0)
563 break;
564
Mika Westerberg17a76b42013-01-17 12:31:05 +0200565 dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300566 rx_limit--;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100567 dev->rx_outstanding++;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300568 } else
Mika Westerberg17a76b42013-01-17 12:31:05 +0200569 dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300570 tx_limit--; buf_len--;
571 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900572
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900573 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900574 dev->tx_buf_len = buf_len;
575
576 if (buf_len > 0) {
577 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900578 dev->status |= STATUS_WRITE_IN_PROGRESS;
579 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900580 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900581 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300582 }
583
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900584 /*
585 * If i2c_msg index search is completed, we don't need TX_EMPTY
586 * interrupt any more.
587 */
588 if (dev->msg_write_idx == dev->msgs_num)
589 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
590
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900591 if (dev->msg_err)
592 intr_mask = 0;
593
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100594 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300595}
596
597static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900598i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300599{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300600 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900601 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300602
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900603 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900604 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300605 u8 *buf;
606
607 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
608 continue;
609
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300610 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
611 len = msgs[dev->msg_read_idx].len;
612 buf = msgs[dev->msg_read_idx].buf;
613 } else {
614 len = dev->rx_buf_len;
615 buf = dev->rx_buf;
616 }
617
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700618 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900619
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100620 for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700621 *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100622 dev->rx_outstanding--;
623 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300624
625 if (len > 0) {
626 dev->status |= STATUS_READ_IN_PROGRESS;
627 dev->rx_buf_len = len;
628 dev->rx_buf = buf;
629 return;
630 } else
631 dev->status &= ~STATUS_READ_IN_PROGRESS;
632 }
633}
634
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900635static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
636{
637 unsigned long abort_source = dev->abort_source;
638 int i;
639
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900640 if (abort_source & DW_IC_TX_ABRT_NOACK) {
Akinobu Mita984b3f52010-03-05 13:41:37 -0800641 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashi6d1ea0f2009-11-16 20:40:14 +0900642 dev_dbg(dev->dev,
643 "%s: %s\n", __func__, abort_sources[i]);
644 return -EREMOTEIO;
645 }
646
Akinobu Mita984b3f52010-03-05 13:41:37 -0800647 for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900648 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
649
650 if (abort_source & DW_IC_TX_ARB_LOST)
651 return -EAGAIN;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900652 else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
653 return -EINVAL; /* wrong msgs[] data */
654 else
655 return -EIO;
656}
657
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300658/*
659 * Prepare controller for a transaction and call i2c_dw_xfer_msg
660 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300661static int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300662i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
663{
664 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
665 int ret;
666
667 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
668
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700669 pm_runtime_get_sync(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300670
Wolfram Sang16735d02013-11-14 14:32:02 -0800671 reinit_completion(&dev->cmd_complete);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300672 dev->msgs = msgs;
673 dev->msgs_num = num;
674 dev->cmd_err = 0;
675 dev->msg_write_idx = 0;
676 dev->msg_read_idx = 0;
677 dev->msg_err = 0;
678 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900679 dev->abort_source = 0;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100680 dev->rx_outstanding = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300681
David Boxc0601d22015-01-15 01:12:16 -0800682 if (dev->acquire_lock) {
683 ret = dev->acquire_lock(dev);
684 if (ret) {
685 dev_err(dev->dev, "couldn't acquire bus ownership\n");
686 goto done_nolock;
687 }
688 }
689
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300690 ret = i2c_dw_wait_bus_not_busy(dev);
691 if (ret < 0)
692 goto done;
693
694 /* start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900695 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300696
697 /* wait for tx to complete */
Weifeng Voond0bcd8d2016-06-17 09:46:35 +0800698 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300699 dev_err(dev->dev, "controller timed out\n");
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200700 /* i2c_dw_init implicitly disables the adapter */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300701 i2c_dw_init(dev);
702 ret = -ETIMEDOUT;
703 goto done;
Mika Westerberge42dba52013-05-22 13:03:11 +0300704 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300705
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200706 /*
Baruch Siache3c97652016-01-12 15:16:35 +0200707 * We must disable the adapter before returning and signaling the end
708 * of the current transfer. Otherwise the hardware might continue
709 * generating interrupts which in turn causes a race condition with
710 * the following transfer. Needs some more investigation if the
711 * additional interrupts are a hardware bug or this driver doesn't
712 * handle them correctly yet.
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200713 */
714 __i2c_dw_enable(dev, false);
715
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300716 if (dev->msg_err) {
717 ret = dev->msg_err;
718 goto done;
719 }
720
721 /* no error */
722 if (likely(!dev->cmd_err)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300723 ret = num;
724 goto done;
725 }
726
727 /* We have an error */
728 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900729 ret = i2c_dw_handle_tx_abort(dev);
730 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300731 }
732 ret = -EIO;
733
734done:
David Boxc0601d22015-01-15 01:12:16 -0800735 if (dev->release_lock)
736 dev->release_lock(dev);
737
738done_nolock:
Mika Westerberg43452332013-04-10 00:36:42 +0000739 pm_runtime_mark_last_busy(dev->dev);
740 pm_runtime_put_autosuspend(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300741
742 return ret;
743}
744
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300745static u32 i2c_dw_func(struct i2c_adapter *adap)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300746{
Dirk Brandewie2fa83262011-10-06 11:26:31 -0700747 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
748 return dev->functionality;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300749}
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300750
751static struct i2c_algorithm i2c_dw_algo = {
752 .master_xfer = i2c_dw_xfer,
753 .functionality = i2c_dw_func,
754};
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300755
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900756static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
757{
758 u32 stat;
759
760 /*
761 * The IC_INTR_STAT register just indicates "enabled" interrupts.
762 * Ths unmasked raw version of interrupt status bits are available
763 * in the IC_RAW_INTR_STAT register.
764 *
765 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100766 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900767 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100768 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900769 *
770 * The raw version might be useful for debugging purposes.
771 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700772 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900773
774 /*
775 * Do not use the IC_CLR_INTR register to clear interrupts, or
776 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100777 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900778 *
779 * Instead, use the separately-prepared IC_CLR_* registers.
780 */
781 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700782 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900783 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700784 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900785 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700786 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900787 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700788 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900789 if (stat & DW_IC_INTR_TX_ABRT) {
790 /*
791 * The IC_TX_ABRT_SOURCE register is cleared whenever
792 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
793 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700794 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
795 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900796 }
797 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700798 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900799 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700800 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900801 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700802 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900803 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700804 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900805 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700806 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900807
808 return stat;
809}
810
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300811/*
812 * Interrupt service routine. This gets called whenever an I2C interrupt
813 * occurs.
814 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300815static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300816{
817 struct dw_i2c_dev *dev = dev_id;
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700818 u32 stat, enabled;
819
820 enabled = dw_readl(dev, DW_IC_ENABLE);
821 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
Jarkko Nikulafb427462015-08-07 14:53:03 +0300822 dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat);
Dirk Brandewieaf06cf62011-10-06 11:26:33 -0700823 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
824 return IRQ_NONE;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300825
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900826 stat = i2c_dw_read_clear_intrbits(dev);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900827
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300828 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300829 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
830 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900831
832 /*
833 * Anytime TX_ABRT is set, the contents of the tx/rx
834 * buffers are flushed. Make sure to skip them.
835 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700836 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900837 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900838 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300839
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900840 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900841 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900842
843 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900844 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900845
846 /*
847 * No need to modify or disable the interrupt mask here.
848 * i2c_dw_xfer_msg() will take care of it according to
849 * the current transmit status.
850 */
851
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900852tx_aborted:
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900853 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300854 complete(&dev->cmd_complete);
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800855 else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) {
856 /* workaround to trigger pending interrupt */
857 stat = dw_readl(dev, DW_IC_INTR_MASK);
858 i2c_dw_disable_int(dev);
859 dw_writel(dev, stat, DW_IC_INTR_MASK);
860 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300861
862 return IRQ_HANDLED;
863}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700864
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700865void i2c_dw_disable(struct dw_i2c_dev *dev)
866{
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700867 /* Disable controller */
José Roberto de Souza2702ea72016-08-23 19:18:53 -0300868 __i2c_dw_enable_and_wait(dev, false);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700869
870 /* Disable all interupts */
871 dw_writel(dev, 0, DW_IC_INTR_MASK);
872 dw_readl(dev, DW_IC_CLR_INTR);
873}
Axel Line68bb912012-09-10 10:14:02 +0200874EXPORT_SYMBOL_GPL(i2c_dw_disable);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700875
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700876void i2c_dw_disable_int(struct dw_i2c_dev *dev)
877{
878 dw_writel(dev, 0, DW_IC_INTR_MASK);
879}
Axel Line68bb912012-09-10 10:14:02 +0200880EXPORT_SYMBOL_GPL(i2c_dw_disable_int);
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700881
882u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev)
883{
884 return dw_readl(dev, DW_IC_COMP_PARAM_1);
885}
Axel Line68bb912012-09-10 10:14:02 +0200886EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param);
Mika Westerberg9dd31622013-01-17 12:31:04 +0200887
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300888int i2c_dw_probe(struct dw_i2c_dev *dev)
889{
890 struct i2c_adapter *adap = &dev->adapter;
891 int r;
892
893 init_completion(&dev->cmd_complete);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300894
895 r = i2c_dw_init(dev);
896 if (r)
897 return r;
898
899 snprintf(adap->name, sizeof(adap->name),
900 "Synopsys DesignWare I2C adapter");
Baruch Siach8d22f302015-12-23 18:43:24 +0200901 adap->retries = 3;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300902 adap->algo = &i2c_dw_algo;
903 adap->dev.parent = dev->dev;
904 i2c_set_adapdata(adap, dev);
905
906 i2c_dw_disable_int(dev);
Andy Shevchenko08c6e8c2016-01-15 22:02:12 +0200907 r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr,
908 IRQF_SHARED | IRQF_COND_SUSPEND,
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300909 dev_name(dev->dev), dev);
910 if (r) {
911 dev_err(dev->dev, "failure requesting irq %i: %d\n",
912 dev->irq, r);
913 return r;
914 }
915
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200916 /*
917 * Increment PM usage count during adapter registration in order to
918 * avoid possible spurious runtime suspend when adapter device is
919 * registered to the device core and immediate resume in case bus has
920 * registered I2C slaves that do I2C transfers in their probe.
921 */
922 pm_runtime_get_noresume(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300923 r = i2c_add_numbered_adapter(adap);
924 if (r)
925 dev_err(dev->dev, "failure adding adapter: %d\n", r);
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200926 pm_runtime_put_noidle(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300927
928 return r;
929}
930EXPORT_SYMBOL_GPL(i2c_dw_probe);
931
Mika Westerberg9dd31622013-01-17 12:31:04 +0200932MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
933MODULE_LICENSE("GPL");