blob: eefc4db1ee3e2467d33e96f4d4149dd724153a54 [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 */
Dirk Brandewie2373f6b2011-10-29 10:57:23 +010054int i2c_dw_init(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}
Axel Line68bb912012-09-10 10:14:02 +0200174EXPORT_SYMBOL_GPL(i2c_dw_init);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300175
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900176static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
177{
178 struct i2c_msg *msgs = dev->msgs;
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200179 u32 ic_con, ic_tar = 0;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900180
Jarkko Nikula89119f02016-11-25 17:22:27 +0200181 /* Disable the adapter */
182 __i2c_dw_enable_and_wait(dev, false);
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800183
Luis Oliveirae393f672017-06-14 11:43:21 +0100184 /* If the slave address is ten bit address, enable 10BITADDR */
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200185 ic_con = dw_readl(dev, DW_IC_CON);
186 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
187 ic_con |= DW_IC_CON_10BITADDR_MASTER;
Mika Westerberg3ca4ed82013-04-10 00:36:40 +0000188 /*
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900189 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200190 * mode has to be enabled via bit 12 of IC_TAR register.
191 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
192 * detected from registers.
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900193 */
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200194 ic_tar = DW_IC_TAR_10BITADDR_MASTER;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900195 } else {
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200196 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Lucas De Marchi63d0f0a62016-08-23 19:18:55 -0300197 }
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900198
Jarkko Nikula12688dc2017-02-13 11:18:19 +0200199 dw_writel(dev, ic_con, DW_IC_CON);
200
Chew, Chiau Eebd63ace2013-09-27 02:57:35 +0800201 /*
202 * Set the slave (target) address and enable 10-bit addressing mode
203 * if applicable.
204 */
205 dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR);
206
Luis Oliveirae393f672017-06-14 11:43:21 +0100207 /* Enforce disabled interrupts (due to HW issues) */
Du, Wenkai47bb27e2014-04-10 23:03:19 +0000208 i2c_dw_disable_int(dev);
209
Jarkko Nikula89119f02016-11-25 17:22:27 +0200210 /* Enable the adapter */
211 __i2c_dw_enable(dev, true);
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900212
Mika Westerberg2a2d95e2013-05-13 00:54:30 +0000213 /* Clear and enable interrupts */
Jarkko Nikulac3356312015-08-31 17:31:28 +0300214 dw_readl(dev, DW_IC_CLR_INTR);
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100215 dw_writel(dev, DW_IC_INTR_MASTER_MASK, DW_IC_INTR_MASK);
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900216}
217
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300218/*
Shinya Kuribayashi201d6a72009-11-06 21:50:40 +0900219 * Initiate (and continue) low level master read/write transaction.
220 * This function is only called from i2c_dw_isr, and pumping i2c_msg
221 * messages into the tx buffer. Even if the size of i2c_msg data is
222 * longer than the size of the tx buffer, it handles everything.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300223 */
Jean Delvarebccd7802012-10-05 22:23:53 +0200224static void
Shinya Kuribayashie77cf232009-11-06 21:46:04 +0900225i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300226{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300227 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900228 u32 intr_mask;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900229 int tx_limit, rx_limit;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900230 u32 addr = msgs[dev->msg_write_idx].addr;
231 u32 buf_len = dev->tx_buf_len;
Justin P. Mattock69932482011-07-26 23:06:29 -0700232 u8 *buf = dev->tx_buf;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800233 bool need_restart = false;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300234
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100235 intr_mask = DW_IC_INTR_MASTER_MASK;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900236
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900237 for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
Tin Huynhc3ae1062016-11-10 09:56:33 +0700238 u32 flags = msgs[dev->msg_write_idx].flags;
239
Shinya Kuribayashia0e06ea2009-11-06 21:52:22 +0900240 /*
Luis Oliveirae393f672017-06-14 11:43:21 +0100241 * If target address has changed, we need to
242 * reprogram the target address in the I2C
243 * adapter when we are done with this transfer.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300244 */
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900245 if (msgs[dev->msg_write_idx].addr != addr) {
246 dev_err(dev->dev,
247 "%s: invalid target address\n", __func__);
248 dev->msg_err = -EINVAL;
249 break;
250 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300251
252 if (msgs[dev->msg_write_idx].len == 0) {
253 dev_err(dev->dev,
254 "%s: invalid message length\n", __func__);
255 dev->msg_err = -EINVAL;
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900256 break;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300257 }
258
259 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
260 /* new i2c_msg */
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900261 buf = msgs[dev->msg_write_idx].buf;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300262 buf_len = msgs[dev->msg_write_idx].len;
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800263
264 /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and
265 * IC_RESTART_EN are set, we must manually
266 * set restart bit between messages.
267 */
268 if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
269 (dev->msg_write_idx > 0))
270 need_restart = true;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300271 }
272
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700273 tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
274 rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900275
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300276 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
Mika Westerberg17a76b42013-01-17 12:31:05 +0200277 u32 cmd = 0;
278
279 /*
280 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
281 * manually set the stop bit. However, it cannot be
282 * detected from the registers so we set it always
283 * when writing/reading the last byte.
284 */
Tin Huynhc3ae1062016-11-10 09:56:33 +0700285
286 /*
Wolfram Sang91ed5342017-05-23 11:08:04 +0200287 * i2c-core always sets the buffer length of
Tin Huynhc3ae1062016-11-10 09:56:33 +0700288 * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will
289 * be adjusted when receiving the first byte.
290 * Thus we can't stop the transaction here.
291 */
Mika Westerberg17a76b42013-01-17 12:31:05 +0200292 if (dev->msg_write_idx == dev->msgs_num - 1 &&
Tin Huynhc3ae1062016-11-10 09:56:33 +0700293 buf_len == 1 && !(flags & I2C_M_RECV_LEN))
Mika Westerberg17a76b42013-01-17 12:31:05 +0200294 cmd |= BIT(9);
295
Chew, Chiau Ee82564242013-06-21 15:05:28 +0800296 if (need_restart) {
297 cmd |= BIT(10);
298 need_restart = false;
299 }
300
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300301 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100302
Luis Oliveirae393f672017-06-14 11:43:21 +0100303 /* Avoid rx buffer overrun */
Russell King4d6d5f12016-11-18 19:40:10 +0000304 if (dev->rx_outstanding >= dev->rx_fifo_depth)
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100305 break;
306
Mika Westerberg17a76b42013-01-17 12:31:05 +0200307 dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300308 rx_limit--;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100309 dev->rx_outstanding++;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300310 } else
Mika Westerberg17a76b42013-01-17 12:31:05 +0200311 dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300312 tx_limit--; buf_len--;
313 }
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900314
Shinya Kuribayashi26ea15b2009-11-06 21:49:14 +0900315 dev->tx_buf = buf;
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900316 dev->tx_buf_len = buf_len;
317
Tin Huynhc3ae1062016-11-10 09:56:33 +0700318 /*
319 * Because we don't know the buffer length in the
320 * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop
321 * the transaction here.
322 */
323 if (buf_len > 0 || flags & I2C_M_RECV_LEN) {
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900324 /* more bytes to be written */
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900325 dev->status |= STATUS_WRITE_IN_PROGRESS;
326 break;
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900327 } else
Shinya Kuribayashic70c5cd2009-11-06 21:47:30 +0900328 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300329 }
330
Shinya Kuribayashi69151e52009-11-06 21:51:00 +0900331 /*
332 * If i2c_msg index search is completed, we don't need TX_EMPTY
333 * interrupt any more.
334 */
335 if (dev->msg_write_idx == dev->msgs_num)
336 intr_mask &= ~DW_IC_INTR_TX_EMPTY;
337
Shinya Kuribayashi8f588e42009-11-06 21:51:18 +0900338 if (dev->msg_err)
339 intr_mask = 0;
340
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100341 dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300342}
343
Tin Huynhc3ae1062016-11-10 09:56:33 +0700344static u8
345i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len)
346{
347 struct i2c_msg *msgs = dev->msgs;
348 u32 flags = msgs[dev->msg_read_idx].flags;
349
350 /*
351 * Adjust the buffer length and mask the flag
352 * after receiving the first byte.
353 */
354 len += (flags & I2C_CLIENT_PEC) ? 2 : 1;
355 dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding);
356 msgs[dev->msg_read_idx].len = len;
357 msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN;
358
359 return len;
360}
361
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300362static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900363i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300364{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300365 struct i2c_msg *msgs = dev->msgs;
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900366 int rx_valid;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300367
Shinya Kuribayashi6d2ea482009-11-06 21:46:29 +0900368 for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900369 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300370 u8 *buf;
371
372 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
373 continue;
374
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300375 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
376 len = msgs[dev->msg_read_idx].len;
377 buf = msgs[dev->msg_read_idx].buf;
378 } else {
379 len = dev->rx_buf_len;
380 buf = dev->rx_buf;
381 }
382
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700383 rx_valid = dw_readl(dev, DW_IC_RXFLR);
Shinya Kuribayashiae722222009-11-06 21:49:39 +0900384
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100385 for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
Tin Huynhc3ae1062016-11-10 09:56:33 +0700386 u32 flags = msgs[dev->msg_read_idx].flags;
387
388 *buf = dw_readl(dev, DW_IC_DATA_CMD);
389 /* Ensure length byte is a valid value */
390 if (flags & I2C_M_RECV_LEN &&
391 *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) {
392 len = i2c_dw_recv_len(dev, *buf);
393 }
394 buf++;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100395 dev->rx_outstanding--;
396 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300397
398 if (len > 0) {
399 dev->status |= STATUS_READ_IN_PROGRESS;
400 dev->rx_buf_len = len;
401 dev->rx_buf = buf;
402 return;
403 } else
404 dev->status &= ~STATUS_READ_IN_PROGRESS;
405 }
406}
407
408/*
Luis Oliveirae393f672017-06-14 11:43:21 +0100409 * Prepare controller for a transaction and call i2c_dw_xfer_msg.
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300410 */
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300411static int
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300412i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
413{
414 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
415 int ret;
416
417 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
418
Dirk Brandewie18dbdda2011-10-06 11:26:36 -0700419 pm_runtime_get_sync(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300420
Wolfram Sang16735d02013-11-14 14:32:02 -0800421 reinit_completion(&dev->cmd_complete);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300422 dev->msgs = msgs;
423 dev->msgs_num = num;
424 dev->cmd_err = 0;
425 dev->msg_write_idx = 0;
426 dev->msg_read_idx = 0;
427 dev->msg_err = 0;
428 dev->status = STATUS_IDLE;
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900429 dev->abort_source = 0;
Josef Ahmade6f34ce2013-04-19 17:28:10 +0100430 dev->rx_outstanding = 0;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300431
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300432 ret = i2c_dw_acquire_lock(dev);
433 if (ret)
434 goto done_nolock;
David Boxc0601d22015-01-15 01:12:16 -0800435
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300436 ret = i2c_dw_wait_bus_not_busy(dev);
437 if (ret < 0)
438 goto done;
439
Luis Oliveirae393f672017-06-14 11:43:21 +0100440 /* Start the transfers */
Shinya Kuribayashi81e798b2009-11-06 21:48:55 +0900441 i2c_dw_xfer_init(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300442
Luis Oliveirae393f672017-06-14 11:43:21 +0100443 /* Wait for tx to complete */
Weifeng Voond0bcd8d2016-06-17 09:46:35 +0800444 if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300445 dev_err(dev->dev, "controller timed out\n");
Christian Ruppert38d7fad2013-06-07 10:51:23 +0200446 /* i2c_dw_init implicitly disables the adapter */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300447 i2c_dw_init(dev);
448 ret = -ETIMEDOUT;
449 goto done;
Mika Westerberge42dba52013-05-22 13:03:11 +0300450 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300451
Jarkko Nikula89119f02016-11-25 17:22:27 +0200452 /*
453 * We must disable the adapter before returning and signaling the end
454 * of the current transfer. Otherwise the hardware might continue
455 * generating interrupts which in turn causes a race condition with
456 * the following transfer. Needs some more investigation if the
457 * additional interrupts are a hardware bug or this driver doesn't
458 * handle them correctly yet.
459 */
460 __i2c_dw_enable(dev, false);
461
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300462 if (dev->msg_err) {
463 ret = dev->msg_err;
464 goto done;
465 }
466
Luis Oliveirae393f672017-06-14 11:43:21 +0100467 /* No error */
Russell King2bf413d2016-11-18 19:40:04 +0000468 if (likely(!dev->cmd_err && !dev->status)) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300469 ret = num;
470 goto done;
471 }
472
473 /* We have an error */
474 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
Shinya Kuribayashice6eb572009-11-06 21:51:57 +0900475 ret = i2c_dw_handle_tx_abort(dev);
476 goto done;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300477 }
Russell King2bf413d2016-11-18 19:40:04 +0000478
479 if (dev->status)
480 dev_err(dev->dev,
481 "transfer terminated early - interrupt latency too high?\n");
482
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300483 ret = -EIO;
484
485done:
Lucas De Marchi8c5660b2016-08-23 19:18:54 -0300486 i2c_dw_release_lock(dev);
David Boxc0601d22015-01-15 01:12:16 -0800487
488done_nolock:
Mika Westerberg43452332013-04-10 00:36:42 +0000489 pm_runtime_mark_last_busy(dev->dev);
490 pm_runtime_put_autosuspend(dev->dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300491
492 return ret;
493}
494
Bhumika Goyal92d9d0d2017-01-27 23:36:17 +0530495static const struct i2c_algorithm i2c_dw_algo = {
Luis Oliveirae393f672017-06-14 11:43:21 +0100496 .master_xfer = i2c_dw_xfer,
497 .functionality = i2c_dw_func,
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300498};
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300499
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900500static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
501{
502 u32 stat;
503
504 /*
505 * The IC_INTR_STAT register just indicates "enabled" interrupts.
506 * Ths unmasked raw version of interrupt status bits are available
507 * in the IC_RAW_INTR_STAT register.
508 *
509 * That is,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100510 * stat = dw_readl(IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900511 * equals to,
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100512 * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900513 *
514 * The raw version might be useful for debugging purposes.
515 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700516 stat = dw_readl(dev, DW_IC_INTR_STAT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900517
518 /*
519 * Do not use the IC_CLR_INTR register to clear interrupts, or
520 * you'll miss some interrupts, triggered during the period from
Dirk Brandewie2373f6b2011-10-29 10:57:23 +0100521 * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR).
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900522 *
523 * Instead, use the separately-prepared IC_CLR_* registers.
524 */
525 if (stat & DW_IC_INTR_RX_UNDER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700526 dw_readl(dev, DW_IC_CLR_RX_UNDER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900527 if (stat & DW_IC_INTR_RX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700528 dw_readl(dev, DW_IC_CLR_RX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900529 if (stat & DW_IC_INTR_TX_OVER)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700530 dw_readl(dev, DW_IC_CLR_TX_OVER);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900531 if (stat & DW_IC_INTR_RD_REQ)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700532 dw_readl(dev, DW_IC_CLR_RD_REQ);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900533 if (stat & DW_IC_INTR_TX_ABRT) {
534 /*
535 * The IC_TX_ABRT_SOURCE register is cleared whenever
536 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
537 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700538 dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
539 dw_readl(dev, DW_IC_CLR_TX_ABRT);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900540 }
541 if (stat & DW_IC_INTR_RX_DONE)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700542 dw_readl(dev, DW_IC_CLR_RX_DONE);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900543 if (stat & DW_IC_INTR_ACTIVITY)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700544 dw_readl(dev, DW_IC_CLR_ACTIVITY);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900545 if (stat & DW_IC_INTR_STOP_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700546 dw_readl(dev, DW_IC_CLR_STOP_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900547 if (stat & DW_IC_INTR_START_DET)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700548 dw_readl(dev, DW_IC_CLR_START_DET);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900549 if (stat & DW_IC_INTR_GEN_CALL)
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700550 dw_readl(dev, DW_IC_CLR_GEN_CALL);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900551
552 return stat;
553}
554
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300555/*
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100556 * Interrupt service routine. This gets called whenever an I2C master interrupt
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300557 * occurs.
558 */
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100559static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300560{
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100561 u32 stat;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300562
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900563 stat = i2c_dw_read_clear_intrbits(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300564 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300565 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
566 dev->status = STATUS_IDLE;
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900567
568 /*
569 * Anytime TX_ABRT is set, the contents of the tx/rx
Luis Oliveirae393f672017-06-14 11:43:21 +0100570 * buffers are flushed. Make sure to skip them.
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900571 */
Jean-Hugues Deschenes7f279602011-10-06 11:26:25 -0700572 dw_writel(dev, 0, DW_IC_INTR_MASK);
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900573 goto tx_aborted;
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900574 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300575
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900576 if (stat & DW_IC_INTR_RX_FULL)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900577 i2c_dw_read(dev);
Shinya Kuribayashi21a89d42009-11-06 21:48:33 +0900578
579 if (stat & DW_IC_INTR_TX_EMPTY)
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900580 i2c_dw_xfer_msg(dev);
Shinya Kuribayashi07745392009-11-06 21:47:51 +0900581
582 /*
583 * No need to modify or disable the interrupt mask here.
584 * i2c_dw_xfer_msg() will take care of it according to
585 * the current transmit status.
586 */
587
Shinya Kuribayashi597fe312009-11-06 21:51:36 +0900588tx_aborted:
Jarkko Nikula89119f02016-11-25 17:22:27 +0200589 if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300590 complete(&dev->cmd_complete);
Hans de Goede86524e52017-02-10 11:27:53 +0100591 else if (unlikely(dev->flags & ACCESS_INTR_MASK)) {
Luis Oliveirae393f672017-06-14 11:43:21 +0100592 /* Workaround to trigger pending interrupt */
Xiangliang Yu2d244c82015-12-11 20:02:53 +0800593 stat = dw_readl(dev, DW_IC_INTR_MASK);
594 i2c_dw_disable_int(dev);
595 dw_writel(dev, stat, DW_IC_INTR_MASK);
596 }
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300597
Luis Oliveira89a1e1b2017-06-14 11:43:22 +0100598 return 0;
599}
600
601static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
602{
603 struct dw_i2c_dev *dev = dev_id;
604 u32 stat, enabled;
605
606 enabled = dw_readl(dev, DW_IC_ENABLE);
607 stat = dw_readl(dev, DW_IC_RAW_INTR_STAT);
608 dev_dbg(dev->dev, "enabled=%#x stat=%#x\n", enabled, stat);
609 if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
610 return IRQ_NONE;
611
612 i2c_dw_irq_handler_master(dev);
613
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300614 return IRQ_HANDLED;
615}
Dirk Brandewief3fa9f32011-10-06 11:26:34 -0700616
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300617int i2c_dw_probe(struct dw_i2c_dev *dev)
618{
619 struct i2c_adapter *adap = &dev->adapter;
Hans de Goede41c80b82017-03-13 23:25:09 +0100620 unsigned long irq_flags;
Luis Oliveirae393f672017-06-14 11:43:21 +0100621 int ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300622
623 init_completion(&dev->cmd_complete);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300624
Luis Oliveira90312352017-06-14 11:43:23 +0100625 dev->init = i2c_dw_init;
626 dev->disable = i2c_dw_disable;
627 dev->disable_int = i2c_dw_disable_int;
628
629 ret = dev->init(dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100630 if (ret)
631 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300632
633 snprintf(adap->name, sizeof(adap->name),
634 "Synopsys DesignWare I2C adapter");
Baruch Siach8d22f302015-12-23 18:43:24 +0200635 adap->retries = 3;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300636 adap->algo = &i2c_dw_algo;
637 adap->dev.parent = dev->dev;
638 i2c_set_adapdata(adap, dev);
639
Hans de Goede41c80b82017-03-13 23:25:09 +0100640 if (dev->pm_disabled) {
641 dev_pm_syscore_device(dev->dev, true);
642 irq_flags = IRQF_NO_SUSPEND;
643 } else {
644 irq_flags = IRQF_SHARED | IRQF_COND_SUSPEND;
645 }
646
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300647 i2c_dw_disable_int(dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100648 ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags,
649 dev_name(dev->dev), dev);
650 if (ret) {
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300651 dev_err(dev->dev, "failure requesting irq %i: %d\n",
Luis Oliveirae393f672017-06-14 11:43:21 +0100652 dev->irq, ret);
653 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300654 }
655
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200656 /*
657 * Increment PM usage count during adapter registration in order to
658 * avoid possible spurious runtime suspend when adapter device is
659 * registered to the device core and immediate resume in case bus has
660 * registered I2C slaves that do I2C transfers in their probe.
661 */
662 pm_runtime_get_noresume(dev->dev);
Luis Oliveirae393f672017-06-14 11:43:21 +0100663 ret = i2c_add_numbered_adapter(adap);
664 if (ret)
665 dev_err(dev->dev, "failure adding adapter: %d\n", ret);
Jarkko Nikulacd998de2016-02-11 16:36:03 +0200666 pm_runtime_put_noidle(dev->dev);
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300667
Luis Oliveirae393f672017-06-14 11:43:21 +0100668 return ret;
Jarkko Nikulad80d1342015-10-12 16:55:35 +0300669}
670EXPORT_SYMBOL_GPL(i2c_dw_probe);
671
Luis Oliveira90312352017-06-14 11:43:23 +0100672MODULE_DESCRIPTION("Synopsys DesignWare I2C bus master adapter");
Mika Westerberg9dd31622013-01-17 12:31:04 +0200673MODULE_LICENSE("GPL");