blob: 418c233075d3d87fbcd979fe6559b666536f43f8 [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 */
Luis Oliveirae393f672017-06-14 11:43:21 +010024#include <linux/delay.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030025#include <linux/err.h>
Luis Oliveira90312352017-06-14 11:43:23 +010026#include <linux/errno.h>
27#include <linux/export.h>
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010028#include <linux/i2c.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030029#include <linux/interrupt.h>
Baruch Siach1ab52cf2009-06-22 16:36:29 +030030#include <linux/io.h>
Mika Westerberg9dd31622013-01-17 12:31:04 +020031#include <linux/module.h>
Luis Oliveirae393f672017-06-14 11:43:21 +010032#include <linux/pm_runtime.h>
Shinya Kuribayashice6eb572009-11-06 21:51:57 +090033
Luis Oliveirae393f672017-06-14 11:43:21 +010034#include "i2c-designware-core.h"
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -070035
Luis Oliveira89a1e1b2017-06-14 11:43:22 +010036static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev)
37{
38 /* Configure Tx/Rx FIFO threshold levels */
39 dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL);
40 dw_writel(dev, 0, DW_IC_RX_TL);
41
42 /* Configure the I2C master */
43 dw_writel(dev, dev->master_cfg, DW_IC_CON);
44}
45
Baruch Siach1ab52cf2009-06-22 16:36:29 +030046/**
Luis Oliveirae393f672017-06-14 11:43:21 +010047 * i2c_dw_init() - Initialize the designware I2C master hardware
Baruch Siach1ab52cf2009-06-22 16:36:29 +030048 * @dev: device private data
49 *
50 * This functions configures and enables the I2C master.
51 * This function is called during I2C init function, and in case of timeout at
52 * run time.
53 */
Jarkko Nikula21bf4402017-06-28 17:23:28 +030054static int i2c_dw_init_master(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +030055{
Dirk Brandewiee18563f2011-10-06 11:26:32 -070056 u32 hcnt, lcnt;
Weifeng Voonb6e67142016-08-12 17:02:51 +030057 u32 reg, comp_param1;
Romain Baeriswyl64682762014-01-20 17:43:43 +010058 u32 sda_falling_time, scl_falling_time;
David Boxc0601d22015-01-15 01:12:16 -080059 int ret;
60
Lucas De Marchi8c5660b2016-08-23 19:18:54 -030061 ret = i2c_dw_acquire_lock(dev);
62 if (ret)
63 return ret;
Dirk Brandewie4a423a82011-10-06 11:26:28 -070064
Dirk Brandewie4a423a82011-10-06 11:26:28 -070065 reg = dw_readl(dev, DW_IC_COMP_TYPE);
66 if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
Stefan Roesea8a9f3f2012-04-18 15:01:41 +020067 /* Configure register endianess access */
Hans de Goede86524e52017-02-10 11:27:53 +010068 dev->flags |= ACCESS_SWAP;
Stefan Roesea8a9f3f2012-04-18 15:01:41 +020069 } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) {
70 /* Configure register access mode 16bit */
Hans de Goede86524e52017-02-10 11:27:53 +010071 dev->flags |= ACCESS_16BIT;
Stefan Roesea8a9f3f2012-04-18 15:01:41 +020072 } else if (reg != DW_IC_COMP_TYPE_VALUE) {
Luis Oliveirae393f672017-06-14 11:43:21 +010073 dev_err(dev->dev,
74 "Unknown Synopsys component type: 0x%08x\n", reg);
Lucas De Marchi8c5660b2016-08-23 19:18:54 -030075 i2c_dw_release_lock(dev);
Dirk Brandewie4a423a82011-10-06 11:26:28 -070076 return -ENODEV;
77 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +030078
Weifeng Voonb6e67142016-08-12 17:02:51 +030079 comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1);
80
Baruch Siach1ab52cf2009-06-22 16:36:29 +030081 /* Disable the adapter */
José Roberto de Souza2702ea72016-08-23 19:18:53 -030082 __i2c_dw_enable_and_wait(dev, false);
Baruch Siach1ab52cf2009-06-22 16:36:29 +030083
Luis Oliveirae393f672017-06-14 11:43:21 +010084 /* Set standard and fast speed deviders for high/low periods */
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +090085
Romain Baeriswyl64682762014-01-20 17:43:43 +010086 sda_falling_time = dev->sda_falling_time ?: 300; /* ns */
87 scl_falling_time = dev->scl_falling_time ?: 300; /* ns */
88
Jarkko Nikula42ffd392015-01-23 11:35:55 +020089 /* Set SCL timing parameters for standard-mode */
Mika Westerbergdefc0b22013-08-19 15:07:53 +030090 if (dev->ss_hcnt && dev->ss_lcnt) {
91 hcnt = dev->ss_hcnt;
92 lcnt = dev->ss_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +020093 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -060094 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +020095 4000, /* tHD;STA = tHIGH = 4.0 us */
96 sda_falling_time,
97 0, /* 0: DW default, 1: Ideal */
98 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -060099 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200100 4700, /* tLOW = 4.7 us */
101 scl_falling_time,
102 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300103 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700104 dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
105 dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900106 dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
107
Weifeng Voond608c3d2016-08-12 17:02:49 +0300108 /* Set SCL timing parameters for fast-mode or fast-mode plus */
109 if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) {
110 hcnt = dev->fp_hcnt;
111 lcnt = dev->fp_lcnt;
112 } else if (dev->fs_hcnt && dev->fs_lcnt) {
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300113 hcnt = dev->fs_hcnt;
114 lcnt = dev->fs_lcnt;
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200115 } else {
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600116 hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200117 600, /* tHD;STA = tHIGH = 0.6 us */
118 sda_falling_time,
119 0, /* 0: DW default, 1: Ideal */
120 0); /* No offset */
Suravee Suthikulpanitb33af112016-01-04 09:17:35 -0600121 lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev),
Jarkko Nikula42ffd392015-01-23 11:35:55 +0200122 1300, /* tLOW = 1.3 us */
123 scl_falling_time,
124 0); /* No offset */
Mika Westerbergdefc0b22013-08-19 15:07:53 +0300125 }
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700126 dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
127 dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
Shinya Kuribayashid60c7e82009-11-06 21:47:01 +0900128 dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300129
Weifeng Voonb6e67142016-08-12 17:02:51 +0300130 if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
131 DW_IC_CON_SPEED_HIGH) {
132 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
133 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
134 dev_err(dev->dev, "High Speed not supported!\n");
135 dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
136 dev->master_cfg |= DW_IC_CON_SPEED_FAST;
137 } else if (dev->hs_hcnt && dev->hs_lcnt) {
138 hcnt = dev->hs_hcnt;
139 lcnt = dev->hs_lcnt;
140 dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT);
141 dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT);
142 dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n",
143 hcnt, lcnt);
144 }
145 }
146
Christian Ruppert9803f862013-06-26 10:55:06 +0200147 /* Configure SDA Hold Time if required */
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800148 reg = dw_readl(dev, DW_IC_COMP_VERSION);
149 if (reg >= DW_IC_SDA_HOLD_MIN_VERS) {
Jarkko Nikula171e23e2016-09-29 16:04:59 +0300150 if (!dev->sda_hold_time) {
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800151 /* Keep previous hold time setting if no one set it */
152 dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD);
153 }
Jarkko Nikula171e23e2016-09-29 16:04:59 +0300154 /*
155 * Workaround for avoiding TX arbitration lost in case I2C
156 * slave pulls SDA down "too quickly" after falling egde of
157 * SCL by enabling non-zero SDA RX hold. Specification says it
158 * extends incoming SDA low to high transition while SCL is
159 * high but it apprears to help also above issue.
160 */
161 if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK))
162 dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT;
163 dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD);
Zhuo-hao Lee664d58b2016-08-27 15:39:30 +0800164 } else {
165 dev_warn(dev->dev,
166 "Hardware too old to adjust SDA hold time.\n");
Christian Ruppert9803f862013-06-26 10:55:06 +0200167 }
168
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100169 i2c_dw_configure_fifo_master(dev);
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300170 i2c_dw_release_lock(dev);
171
Dirk Brandewie4a423a82011-10-06 11:26:28 -0700172 return 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300173}
174
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900175static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
176{
177 struct i2c_msg *msgs = dev->msgs;
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200178 u32 ic_con, ic_tar = 0;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900179
Jarkko Nikula89119f02016-11-25 17:22:27 +0200180 /* Disable the adapter */
181 __i2c_dw_enable_and_wait(dev, false);
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800182
Luis Oliveirae393f672017-06-14 11:43:21 +0100183 /* If the slave address is ten bit address, enable 10BITADDR */
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200184 ic_con = dw_readl(dev, DW_IC_CON);
185 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
186 ic_con |= DW_IC_CON_10BITADDR_MASTER;
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000187 /*
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900188 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200189 * mode has to be enabled via bit 12 of IC_TAR register.
190 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
191 * detected from registers.
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900192 */
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200193 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900194 } else {
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200195 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300196 }
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900197
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200198 dw_writel(dev, ic_con, DW_IC_CON);
199
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800200 /*
201 * Set the slave (target) address and enable 10-bit addressing mode
202 * if applicable.
203 */
204 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
205
Luis Oliveirae393f672017-06-14 11:43:21 +0100206 /* Enforce disabled interrupts (due to HW issues) */
Du, Wenkai47bb27e2014-04-10 23:03:19 +0000207 i2c_dw_disable_int(dev);
208
Jarkko Nikula89119f02016-11-25 17:22:27 +0200209 /* Enable the adapter */
210 __i2c_dw_enable(dev, true);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900211
Mika Westerberg2a2d95e2013-05-13 00:54:30 +0000212 /* Clear and enable interrupts */
Jarkko Nikulac3356312015-08-31 17:31:28 +0300213 dw_readl(dev, DW_IC_CLR_INTR);
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100214 dw_writel(dev, DW_IC_INTR_MASTER_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900215}
216
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300217/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900218 * Initiate (and continue) low level master read/write transaction.
219 * This function is only called from i2c_dw_isr, and pumping i2c_msg
220 * messages into the tx buffer. Even if the size of i2c_msg data is
221 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300222 */
Jean Delvarebccd7802012-10-05 22:23:53 +0200223static void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900224i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300225{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300226 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900227 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900228 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900229 u32 addr = msgs[dev->msg_write_idx].addr;
230 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700231 u8 *buf = dev->tx_buf;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800232 bool need_restart = false;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300233
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100234 intr_mask = DW_IC_INTR_MASTER_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900235
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900236 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Tin Huynhc3ae1062016-11-10 09:56:33 +0700237 u32 flags = msgs[dev->msg_write_idx].flags;
238
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900239 /*
Luis Oliveirae393f672017-06-14 11:43:21 +0100240 * If target address has changed, we need to
241 * reprogram the target address in the I2C
242 * adapter when we are done with this transfer.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300243 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900244 if (msgs[dev->msg_write_idx].addr != addr) {
245 dev_err(dev->dev,
246 "%s: invalid target address\n", __func__);
247 dev->msg_err = -EINVAL;
248 break;
249 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300250
251 if (msgs[dev->msg_write_idx].len == 0) {
252 dev_err(dev->dev,
253 "%s: invalid message length\n", __func__);
254 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900255 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300256 }
257
258 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
259 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900260 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300261 buf_len = msgs[dev->msg_write_idx].len;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800262
263 /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
264 * IC_RESTART_EN are set, we must manually
265 * set restart bit between messages.
266 */
267 if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
268 (dev->msg_write_idx > 0))
269 need_restart = true;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300270 }
271
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700272 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
273 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900274
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300275 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
Mika Westerberg17a76b42013-01-17 12:31:05 +0200276 u32 cmd = 0;
277
278 /*
279 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
280 * manually set the stop bit. However, it cannot be
281 * detected from the registers so we set it always
282 * when writing/reading the last byte.
283 */
Tin Huynhc3ae1062016-11-10 09:56:33 +0700284
285 /*
Wolfram Sang91ed5342017-05-23 11:08:04 +0200286 * i2c-core always sets the buffer length of
Tin Huynhc3ae1062016-11-10 09:56:33 +0700287 * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will
288 * be adjusted when receiving the first byte.
289 * Thus we can't stop the transaction here.
290 */
Mika Westerberg17a76b42013-01-17 12:31:05 +0200291 if (dev->msg_write_idx == dev->msgs_num - 1 &&
Tin Huynhc3ae1062016-11-10 09:56:33 +0700292 buf_len == 1 && !(flags & I2C_M_RECV_LEN))
Mika Westerberg17a76b42013-01-17 12:31:05 +0200293 cmd |= BIT(9);
294
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800295 if (need_restart) {
296 cmd |= BIT(10);
297 need_restart = false;
298 }
299
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300300 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100301
Luis Oliveirae393f672017-06-14 11:43:21 +0100302 /* Avoid rx buffer overrun */
Russell King4d6d5f12016-11-18 19:40:10 +0000303 if (dev->rx_outstanding >= dev->rx_fifo_depth)
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100304 break;
305
Mika Westerberg17a76b42013-01-17 12:31:05 +0200306 dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300307 rx_limit--;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100308 dev->rx_outstanding++;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300309 } else
Mika Westerberg17a76b42013-01-17 12:31:05 +0200310 dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300311 tx_limit--; buf_len--;
312 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900313
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900314 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900315 dev->tx_buf_len = buf_len;
316
Tin Huynhc3ae1062016-11-10 09:56:33 +0700317 /*
318 * Because we don't know the buffer length in the
319 * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop
320 * the transaction here.
321 */
322 if (buf_len > 0 || flags & I2C_M_RECV_LEN) {
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900323 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900324 dev->status |= STATUS_WRITE_IN_PROGRESS;
325 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900326 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900327 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300328 }
329
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900330 /*
331 * If i2c_msg index search is completed, we don't need TX_EMPTY
332 * interrupt any more.
333 */
334 if (dev->msg_write_idx == dev->msgs_num)
335 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
336
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900337 if (dev->msg_err)
338 intr_mask = 0;
339
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100340 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300341}
342
Tin Huynhc3ae1062016-11-10 09:56:33 +0700343static u8
344i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len)
345{
346 struct i2c_msg *msgs = dev->msgs;
347 u32 flags = msgs[dev->msg_read_idx].flags;
348
349 /*
350 * Adjust the buffer length and mask the flag
351 * after receiving the first byte.
352 */
353 len += (flags & I2C_CLIENT_PEC) ? 2 : 1;
354 dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding);
355 msgs[dev->msg_read_idx].len = len;
356 msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN;
357
358 return len;
359}
360
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300361static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900362i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300363{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300364 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900365 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300366
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900367 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900368 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300369 u8 *buf;
370
371 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
372 continue;
373
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300374 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
375 len = msgs[dev->msg_read_idx].len;
376 buf = msgs[dev->msg_read_idx].buf;
377 } else {
378 len = dev->rx_buf_len;
379 buf = dev->rx_buf;
380 }
381
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700382 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900383
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100384 for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
Tin Huynhc3ae1062016-11-10 09:56:33 +0700385 u32 flags = msgs[dev->msg_read_idx].flags;
386
387 *buf = dw_readl(dev, DW_IC_DATA_CMD);
388 /* Ensure length byte is a valid value */
389 if (flags & I2C_M_RECV_LEN &&
390 *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) {
391 len = i2c_dw_recv_len(dev, *buf);
392 }
393 buf++;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100394 dev->rx_outstanding--;
395 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300396
397 if (len > 0) {
398 dev->status |= STATUS_READ_IN_PROGRESS;
399 dev->rx_buf_len = len;
400 dev->rx_buf = buf;
401 return;
402 } else
403 dev->status &= ~STATUS_READ_IN_PROGRESS;
404 }
405}
406
407/*
Luis Oliveirae393f672017-06-14 11:43:21 +0100408 * Prepare controller for a transaction and call i2c_dw_xfer_msg.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300409 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300410static int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300411i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
412{
413 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
414 int ret;
415
416 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
417
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700418 pm_runtime_get_sync(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300419
Wolfram Sang16735d02013-11-14 14:32:02 -0800420 reinit_completion(&dev->cmd_complete);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300421 dev->msgs = msgs;
422 dev->msgs_num = num;
423 dev->cmd_err = 0;
424 dev->msg_write_idx = 0;
425 dev->msg_read_idx = 0;
426 dev->msg_err = 0;
427 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900428 dev->abort_source = 0;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100429 dev->rx_outstanding = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300430
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300431 ret = i2c_dw_acquire_lock(dev);
432 if (ret)
433 goto done_nolock;
David Boxc0601d22015-01-15 01:12:16 -0800434
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300435 ret = i2c_dw_wait_bus_not_busy(dev);
436 if (ret < 0)
437 goto done;
438
Luis Oliveirae393f672017-06-14 11:43:21 +0100439 /* Start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900440 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300441
Luis Oliveirae393f672017-06-14 11:43:21 +0100442 /* Wait for tx to complete */
Weifeng Voond0bcd8d2016-06-17 09:46:35 +0800443 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300444 dev_err(dev->dev, "controller timed out\n");
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200445 /* i2c_dw_init implicitly disables the adapter */
Jarkko Nikula21bf4402017-06-28 17:23:28 +0300446 i2c_dw_init_master(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300447 ret = -ETIMEDOUT;
448 goto done;
Mika Westerberge42dba52013-05-22 13:03:11 +0300449 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300450
Jarkko Nikula89119f02016-11-25 17:22:27 +0200451 /*
452 * We must disable the adapter before returning and signaling the end
453 * of the current transfer. Otherwise the hardware might continue
454 * generating interrupts which in turn causes a race condition with
455 * the following transfer. Needs some more investigation if the
456 * additional interrupts are a hardware bug or this driver doesn't
457 * handle them correctly yet.
458 */
459 __i2c_dw_enable(dev, false);
460
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300461 if (dev->msg_err) {
462 ret = dev->msg_err;
463 goto done;
464 }
465
Luis Oliveirae393f672017-06-14 11:43:21 +0100466 /* No error */
Russell King2bf413d2016-11-18 19:40:04 +0000467 if (likely(!dev->cmd_err && !dev->status)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300468 ret = num;
469 goto done;
470 }
471
472 /* We have an error */
473 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900474 ret = i2c_dw_handle_tx_abort(dev);
475 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300476 }
Russell King2bf413d2016-11-18 19:40:04 +0000477
478 if (dev->status)
479 dev_err(dev->dev,
480 "transfer terminated early - interrupt latency too high?\n");
481
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300482 ret = -EIO;
483
484done:
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300485 i2c_dw_release_lock(dev);
David Boxc0601d22015-01-15 01:12:16 -0800486
487done_nolock:
Mika Westerberg43452332013-04-10 00:36:42 +0000488 pm_runtime_mark_last_busy(dev->dev);
489 pm_runtime_put_autosuspend(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300490
491 return ret;
492}
493
Bhumika Goyal92d9d0d2017-01-27 23:36:17 +0530494static const struct i2c_algorithm i2c_dw_algo = {
Luis Oliveirae393f672017-06-14 11:43:21 +0100495 .master_xfer = i2c_dw_xfer,
496 .functionality = i2c_dw_func,
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300497};
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300498
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900499static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
500{
501 u32 stat;
502
503 /*
504 * The IC_INTR_STAT register just indicates "enabled" interrupts.
505 * Ths unmasked raw version of interrupt status bits are available
506 * in the IC_RAW_INTR_STAT register.
507 *
508 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100509 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900510 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100511 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900512 *
513 * The raw version might be useful for debugging purposes.
514 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700515 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900516
517 /*
518 * Do not use the IC_CLR_INTR register to clear interrupts, or
519 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100520 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900521 *
522 * Instead, use the separately-prepared IC_CLR_* registers.
523 */
524 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700525 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900526 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700527 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900528 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700529 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900530 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700531 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900532 if (stat & DW_IC_INTR_TX_ABRT) {
533 /*
534 * The IC_TX_ABRT_SOURCE register is cleared whenever
535 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
536 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700537 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
538 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900539 }
540 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700541 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900542 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700543 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900544 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700545 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900546 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700547 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900548 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700549 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900550
551 return stat;
552}
553
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300554/*
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100555 * Interrupt service routine. This gets called whenever an I2C master interrupt
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300556 * occurs.
557 */
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100558static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300559{
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100560 u32 stat;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300561
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900562 stat = i2c_dw_read_clear_intrbits(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300563 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300564 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
565 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900566
567 /*
568 * Anytime TX_ABRT is set, the contents of the tx/rx
Luis Oliveirae393f672017-06-14 11:43:21 +0100569 * buffers are flushed. Make sure to skip them.
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900570 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700571 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900572 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900573 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300574
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900575 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900576 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900577
578 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900579 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900580
581 /*
582 * No need to modify or disable the interrupt mask here.
583 * i2c_dw_xfer_msg() will take care of it according to
584 * the current transmit status.
585 */
586
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900587tx_aborted:
Jarkko Nikula89119f02016-11-25 17:22:27 +0200588 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300589 complete(&dev->cmd_complete);
Hans de Goede86524e52017-02-10 11:27:53 +0100590 else if (unlikely(dev->flags & ACCESS_INTR_MASK)) {
Luis Oliveirae393f672017-06-14 11:43:21 +0100591 /* Workaround to trigger pending interrupt */
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800592 stat = dw_readl(dev, DW_IC_INTR_MASK);
593 i2c_dw_disable_int(dev);
594 dw_writel(dev, stat, DW_IC_INTR_MASK);
595 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300596
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100597 return 0;
598}
599
600static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
601{
602 struct dw_i2c_dev *dev = dev_id;
603 u32 stat, enabled;
604
605 enabled = dw_readl(dev, DW_IC_ENABLE);
606 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
607 dev_dbg(dev->dev, "enabled=%#x stat=%#x\n", enabled, stat);
608 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
609 return IRQ_NONE;
610
611 i2c_dw_irq_handler_master(dev);
612
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300613 return IRQ_HANDLED;
614}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700615
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300616int i2c_dw_probe(struct dw_i2c_dev *dev)
617{
618 struct i2c_adapter *adap = &dev->adapter;
Hans de Goede41c80b82017-03-13 23:25:09 +0100619 unsigned long irq_flags;
Luis Oliveirae393f672017-06-14 11:43:21 +0100620 int ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300621
622 init_completion(&dev->cmd_complete);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300623
Jarkko Nikula21bf4402017-06-28 17:23:28 +0300624 dev->init = i2c_dw_init_master;
Luis Oliveira90312352017-06-14 11:43:23 +0100625 dev->disable = i2c_dw_disable;
626 dev->disable_int = i2c_dw_disable_int;
627
628 ret = dev->init(dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100629 if (ret)
630 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300631
632 snprintf(adap->name, sizeof(adap->name),
633 "Synopsys DesignWare I2C adapter");
Baruch Siach8d22f302015-12-23 18:43:24 +0200634 adap->retries = 3;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300635 adap->algo = &i2c_dw_algo;
636 adap->dev.parent = dev->dev;
637 i2c_set_adapdata(adap, dev);
638
Hans de Goede41c80b82017-03-13 23:25:09 +0100639 if (dev->pm_disabled) {
640 dev_pm_syscore_device(dev->dev, true);
641 irq_flags = IRQF_NO_SUSPEND;
642 } else {
643 irq_flags = IRQF_SHARED | IRQF_COND_SUSPEND;
644 }
645
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300646 i2c_dw_disable_int(dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100647 ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags,
648 dev_name(dev->dev), dev);
649 if (ret) {
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300650 dev_err(dev->dev, "failure requesting irq %i: %d\n",
Luis Oliveirae393f672017-06-14 11:43:21 +0100651 dev->irq, ret);
652 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300653 }
654
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200655 /*
656 * Increment PM usage count during adapter registration in order to
657 * avoid possible spurious runtime suspend when adapter device is
658 * registered to the device core and immediate resume in case bus has
659 * registered I2C slaves that do I2C transfers in their probe.
660 */
661 pm_runtime_get_noresume(dev->dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100662 ret = i2c_add_numbered_adapter(adap);
663 if (ret)
664 dev_err(dev->dev, "failure adding adapter: %d\n", ret);
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200665 pm_runtime_put_noidle(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300666
Luis Oliveirae393f672017-06-14 11:43:21 +0100667 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300668}
669EXPORT_SYMBOL_GPL(i2c_dw_probe);
670
Luis Oliveira90312352017-06-14 11:43:23 +0100671MODULE_DESCRIPTION("Synopsys DesignWare I2C bus master adapter");
Mika Westerberg9dd31622013-01-17 12:31:04 +0200672MODULE_LICENSE("GPL");