Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 1 | /* |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 2 | * Synopsys DesignWare I2C adapter driver (master only). |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 3 | * |
| 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 Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 21 | * ---------------------------------------------------------------------------- |
| 22 | * |
| 23 | */ |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 24 | #include <linux/delay.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 25 | #include <linux/err.h> |
Luis Oliveira | 9031235 | 2017-06-14 11:43:23 +0100 | [diff] [blame] | 26 | #include <linux/errno.h> |
| 27 | #include <linux/export.h> |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 28 | #include <linux/gpio/consumer.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 29 | #include <linux/i2c.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 30 | #include <linux/interrupt.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 31 | #include <linux/io.h> |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 32 | #include <linux/module.h> |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 34 | #include <linux/reset.h> |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 35 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 36 | #include "i2c-designware-core.h" |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 37 | |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 38 | static void i2c_dw_configure_fifo_master(struct dw_i2c_dev *dev) |
| 39 | { |
| 40 | /* Configure Tx/Rx FIFO threshold levels */ |
| 41 | dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL); |
| 42 | dw_writel(dev, 0, DW_IC_RX_TL); |
| 43 | |
| 44 | /* Configure the I2C master */ |
| 45 | dw_writel(dev, dev->master_cfg, DW_IC_CON); |
| 46 | } |
| 47 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 48 | /** |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 49 | * i2c_dw_init() - Initialize the designware I2C master hardware |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 50 | * @dev: device private data |
| 51 | * |
| 52 | * This functions configures and enables the I2C master. |
| 53 | * This function is called during I2C init function, and in case of timeout at |
| 54 | * run time. |
| 55 | */ |
Jarkko Nikula | 21bf440 | 2017-06-28 17:23:28 +0300 | [diff] [blame] | 56 | static int i2c_dw_init_master(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 57 | { |
Dirk Brandewie | e18563f | 2011-10-06 11:26:32 -0700 | [diff] [blame] | 58 | u32 hcnt, lcnt; |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 59 | u32 reg, comp_param1; |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 60 | u32 sda_falling_time, scl_falling_time; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 61 | int ret; |
| 62 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 63 | ret = i2c_dw_acquire_lock(dev); |
| 64 | if (ret) |
| 65 | return ret; |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 66 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 67 | reg = dw_readl(dev, DW_IC_COMP_TYPE); |
| 68 | if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 69 | /* Configure register endianess access */ |
Hans de Goede | 86524e5 | 2017-02-10 11:27:53 +0100 | [diff] [blame] | 70 | dev->flags |= ACCESS_SWAP; |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 71 | } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) { |
| 72 | /* Configure register access mode 16bit */ |
Hans de Goede | 86524e5 | 2017-02-10 11:27:53 +0100 | [diff] [blame] | 73 | dev->flags |= ACCESS_16BIT; |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 74 | } else if (reg != DW_IC_COMP_TYPE_VALUE) { |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 75 | dev_err(dev->dev, |
| 76 | "Unknown Synopsys component type: 0x%08x\n", reg); |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 77 | i2c_dw_release_lock(dev); |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 78 | return -ENODEV; |
| 79 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 80 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 81 | comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 82 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 83 | /* Disable the adapter */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 84 | __i2c_dw_enable_and_wait(dev, false); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 85 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 86 | /* Set standard and fast speed deviders for high/low periods */ |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 87 | |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 88 | sda_falling_time = dev->sda_falling_time ?: 300; /* ns */ |
| 89 | scl_falling_time = dev->scl_falling_time ?: 300; /* ns */ |
| 90 | |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 91 | /* Set SCL timing parameters for standard-mode */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 92 | if (dev->ss_hcnt && dev->ss_lcnt) { |
| 93 | hcnt = dev->ss_hcnt; |
| 94 | lcnt = dev->ss_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 95 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 96 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 97 | 4000, /* tHD;STA = tHIGH = 4.0 us */ |
| 98 | sda_falling_time, |
| 99 | 0, /* 0: DW default, 1: Ideal */ |
| 100 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 101 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 102 | 4700, /* tLOW = 4.7 us */ |
| 103 | scl_falling_time, |
| 104 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 105 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 106 | dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); |
| 107 | dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 108 | dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
| 109 | |
Weifeng Voon | d608c3d | 2016-08-12 17:02:49 +0300 | [diff] [blame] | 110 | /* Set SCL timing parameters for fast-mode or fast-mode plus */ |
| 111 | if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) { |
| 112 | hcnt = dev->fp_hcnt; |
| 113 | lcnt = dev->fp_lcnt; |
| 114 | } else if (dev->fs_hcnt && dev->fs_lcnt) { |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 115 | hcnt = dev->fs_hcnt; |
| 116 | lcnt = dev->fs_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 117 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 118 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 119 | 600, /* tHD;STA = tHIGH = 0.6 us */ |
| 120 | sda_falling_time, |
| 121 | 0, /* 0: DW default, 1: Ideal */ |
| 122 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 123 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 124 | 1300, /* tLOW = 1.3 us */ |
| 125 | scl_falling_time, |
| 126 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 127 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 128 | dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); |
| 129 | dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 130 | dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 131 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 132 | if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) == |
| 133 | DW_IC_CON_SPEED_HIGH) { |
| 134 | if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK) |
| 135 | != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) { |
| 136 | dev_err(dev->dev, "High Speed not supported!\n"); |
| 137 | dev->master_cfg &= ~DW_IC_CON_SPEED_MASK; |
| 138 | dev->master_cfg |= DW_IC_CON_SPEED_FAST; |
| 139 | } else if (dev->hs_hcnt && dev->hs_lcnt) { |
| 140 | hcnt = dev->hs_hcnt; |
| 141 | lcnt = dev->hs_lcnt; |
| 142 | dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT); |
| 143 | dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT); |
| 144 | dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n", |
| 145 | hcnt, lcnt); |
| 146 | } |
| 147 | } |
| 148 | |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 149 | /* Configure SDA Hold Time if required */ |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 150 | reg = dw_readl(dev, DW_IC_COMP_VERSION); |
| 151 | if (reg >= DW_IC_SDA_HOLD_MIN_VERS) { |
Jarkko Nikula | 171e23e | 2016-09-29 16:04:59 +0300 | [diff] [blame] | 152 | if (!dev->sda_hold_time) { |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 153 | /* Keep previous hold time setting if no one set it */ |
| 154 | dev->sda_hold_time = dw_readl(dev, DW_IC_SDA_HOLD); |
| 155 | } |
Jarkko Nikula | 171e23e | 2016-09-29 16:04:59 +0300 | [diff] [blame] | 156 | /* |
| 157 | * Workaround for avoiding TX arbitration lost in case I2C |
| 158 | * slave pulls SDA down "too quickly" after falling egde of |
| 159 | * SCL by enabling non-zero SDA RX hold. Specification says it |
| 160 | * extends incoming SDA low to high transition while SCL is |
| 161 | * high but it apprears to help also above issue. |
| 162 | */ |
| 163 | if (!(dev->sda_hold_time & DW_IC_SDA_HOLD_RX_MASK)) |
| 164 | dev->sda_hold_time |= 1 << DW_IC_SDA_HOLD_RX_SHIFT; |
| 165 | dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); |
Alexander Monakov | 7a20e70 | 2018-03-08 16:23:53 +0300 | [diff] [blame] | 166 | } else if (dev->sda_hold_time) { |
Zhuo-hao Lee | 664d58b | 2016-08-27 15:39:30 +0800 | [diff] [blame] | 167 | dev_warn(dev->dev, |
| 168 | "Hardware too old to adjust SDA hold time.\n"); |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 169 | } |
| 170 | |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 171 | i2c_dw_configure_fifo_master(dev); |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 172 | i2c_dw_release_lock(dev); |
| 173 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 174 | return 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 175 | } |
| 176 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 177 | static void i2c_dw_xfer_init(struct dw_i2c_dev *dev) |
| 178 | { |
| 179 | struct i2c_msg *msgs = dev->msgs; |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 180 | u32 ic_con, ic_tar = 0; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 181 | |
Jarkko Nikula | 89119f0 | 2016-11-25 17:22:27 +0200 | [diff] [blame] | 182 | /* Disable the adapter */ |
| 183 | __i2c_dw_enable_and_wait(dev, false); |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 184 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 185 | /* If the slave address is ten bit address, enable 10BITADDR */ |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 186 | ic_con = dw_readl(dev, DW_IC_CON); |
| 187 | if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) { |
| 188 | ic_con |= DW_IC_CON_10BITADDR_MASTER; |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 189 | /* |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 190 | * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 191 | * mode has to be enabled via bit 12 of IC_TAR register. |
| 192 | * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be |
| 193 | * detected from registers. |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 194 | */ |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 195 | ic_tar = DW_IC_TAR_10BITADDR_MASTER; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 196 | } else { |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 197 | ic_con &= ~DW_IC_CON_10BITADDR_MASTER; |
Lucas De Marchi | 63d0f0a6 | 2016-08-23 19:18:55 -0300 | [diff] [blame] | 198 | } |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 199 | |
Jarkko Nikula | 12688dc | 2017-02-13 11:18:19 +0200 | [diff] [blame] | 200 | dw_writel(dev, ic_con, DW_IC_CON); |
| 201 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 202 | /* |
| 203 | * Set the slave (target) address and enable 10-bit addressing mode |
| 204 | * if applicable. |
| 205 | */ |
| 206 | dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR); |
| 207 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 208 | /* Enforce disabled interrupts (due to HW issues) */ |
Du, Wenkai | 47bb27e | 2014-04-10 23:03:19 +0000 | [diff] [blame] | 209 | i2c_dw_disable_int(dev); |
| 210 | |
Jarkko Nikula | 89119f0 | 2016-11-25 17:22:27 +0200 | [diff] [blame] | 211 | /* Enable the adapter */ |
Ben Gardner | fba4adb | 2018-02-14 09:29:52 -0600 | [diff] [blame] | 212 | __i2c_dw_enable_and_wait(dev, true); |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 213 | |
Mika Westerberg | 2a2d95e | 2013-05-13 00:54:30 +0000 | [diff] [blame] | 214 | /* Clear and enable interrupts */ |
Jarkko Nikula | c335631 | 2015-08-31 17:31:28 +0300 | [diff] [blame] | 215 | dw_readl(dev, DW_IC_CLR_INTR); |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 216 | dw_writel(dev, DW_IC_INTR_MASTER_MASK, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 219 | /* |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 220 | * Initiate (and continue) low level master read/write transaction. |
| 221 | * This function is only called from i2c_dw_isr, and pumping i2c_msg |
| 222 | * messages into the tx buffer. Even if the size of i2c_msg data is |
| 223 | * longer than the size of the tx buffer, it handles everything. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 224 | */ |
Jean Delvare | bccd780 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 225 | static void |
Shinya Kuribayashi | e77cf23 | 2009-11-06 21:46:04 +0900 | [diff] [blame] | 226 | i2c_dw_xfer_msg(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 227 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 228 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 229 | u32 intr_mask; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 230 | int tx_limit, rx_limit; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 231 | u32 addr = msgs[dev->msg_write_idx].addr; |
| 232 | u32 buf_len = dev->tx_buf_len; |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 233 | u8 *buf = dev->tx_buf; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 234 | bool need_restart = false; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 235 | |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 236 | intr_mask = DW_IC_INTR_MASTER_MASK; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 237 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 238 | for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 239 | u32 flags = msgs[dev->msg_write_idx].flags; |
| 240 | |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 241 | /* |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 242 | * If target address has changed, we need to |
| 243 | * reprogram the target address in the I2C |
| 244 | * adapter when we are done with this transfer. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 245 | */ |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 246 | if (msgs[dev->msg_write_idx].addr != addr) { |
| 247 | dev_err(dev->dev, |
| 248 | "%s: invalid target address\n", __func__); |
| 249 | dev->msg_err = -EINVAL; |
| 250 | break; |
| 251 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 252 | |
| 253 | if (msgs[dev->msg_write_idx].len == 0) { |
| 254 | dev_err(dev->dev, |
| 255 | "%s: invalid message length\n", __func__); |
| 256 | dev->msg_err = -EINVAL; |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 257 | break; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { |
| 261 | /* new i2c_msg */ |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 262 | buf = msgs[dev->msg_write_idx].buf; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 263 | buf_len = msgs[dev->msg_write_idx].len; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 264 | |
| 265 | /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and |
| 266 | * IC_RESTART_EN are set, we must manually |
| 267 | * set restart bit between messages. |
| 268 | */ |
| 269 | if ((dev->master_cfg & DW_IC_CON_RESTART_EN) && |
| 270 | (dev->msg_write_idx > 0)) |
| 271 | need_restart = true; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 272 | } |
| 273 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 274 | tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR); |
| 275 | rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 276 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 277 | while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) { |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 278 | u32 cmd = 0; |
| 279 | |
| 280 | /* |
| 281 | * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must |
| 282 | * manually set the stop bit. However, it cannot be |
| 283 | * detected from the registers so we set it always |
| 284 | * when writing/reading the last byte. |
| 285 | */ |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 286 | |
| 287 | /* |
Wolfram Sang | 91ed534 | 2017-05-23 11:08:04 +0200 | [diff] [blame] | 288 | * i2c-core always sets the buffer length of |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 289 | * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will |
| 290 | * be adjusted when receiving the first byte. |
| 291 | * Thus we can't stop the transaction here. |
| 292 | */ |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 293 | if (dev->msg_write_idx == dev->msgs_num - 1 && |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 294 | buf_len == 1 && !(flags & I2C_M_RECV_LEN)) |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 295 | cmd |= BIT(9); |
| 296 | |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 297 | if (need_restart) { |
| 298 | cmd |= BIT(10); |
| 299 | need_restart = false; |
| 300 | } |
| 301 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 302 | if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 303 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 304 | /* Avoid rx buffer overrun */ |
Russell King | 4d6d5f1 | 2016-11-18 19:40:10 +0000 | [diff] [blame] | 305 | if (dev->rx_outstanding >= dev->rx_fifo_depth) |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 306 | break; |
| 307 | |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 308 | dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 309 | rx_limit--; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 310 | dev->rx_outstanding++; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 311 | } else |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 312 | dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 313 | tx_limit--; buf_len--; |
| 314 | } |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 315 | |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 316 | dev->tx_buf = buf; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 317 | dev->tx_buf_len = buf_len; |
| 318 | |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 319 | /* |
| 320 | * Because we don't know the buffer length in the |
| 321 | * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop |
| 322 | * the transaction here. |
| 323 | */ |
| 324 | if (buf_len > 0 || flags & I2C_M_RECV_LEN) { |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 325 | /* more bytes to be written */ |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 326 | dev->status |= STATUS_WRITE_IN_PROGRESS; |
| 327 | break; |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 328 | } else |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 329 | dev->status &= ~STATUS_WRITE_IN_PROGRESS; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 330 | } |
| 331 | |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 332 | /* |
| 333 | * If i2c_msg index search is completed, we don't need TX_EMPTY |
| 334 | * interrupt any more. |
| 335 | */ |
| 336 | if (dev->msg_write_idx == dev->msgs_num) |
| 337 | intr_mask &= ~DW_IC_INTR_TX_EMPTY; |
| 338 | |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 339 | if (dev->msg_err) |
| 340 | intr_mask = 0; |
| 341 | |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 342 | dw_writel(dev, intr_mask, DW_IC_INTR_MASK); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 343 | } |
| 344 | |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 345 | static u8 |
| 346 | i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len) |
| 347 | { |
| 348 | struct i2c_msg *msgs = dev->msgs; |
| 349 | u32 flags = msgs[dev->msg_read_idx].flags; |
| 350 | |
| 351 | /* |
| 352 | * Adjust the buffer length and mask the flag |
| 353 | * after receiving the first byte. |
| 354 | */ |
| 355 | len += (flags & I2C_CLIENT_PEC) ? 2 : 1; |
| 356 | dev->tx_buf_len = len - min_t(u8, len, dev->rx_outstanding); |
| 357 | msgs[dev->msg_read_idx].len = len; |
| 358 | msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN; |
| 359 | |
| 360 | return len; |
| 361 | } |
| 362 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 363 | static void |
Shinya Kuribayashi | 78839bd | 2009-11-06 21:45:39 +0900 | [diff] [blame] | 364 | i2c_dw_read(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 365 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 366 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 367 | int rx_valid; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 368 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 369 | for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) { |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 370 | u32 len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 371 | u8 *buf; |
| 372 | |
| 373 | if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD)) |
| 374 | continue; |
| 375 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 376 | if (!(dev->status & STATUS_READ_IN_PROGRESS)) { |
| 377 | len = msgs[dev->msg_read_idx].len; |
| 378 | buf = msgs[dev->msg_read_idx].buf; |
| 379 | } else { |
| 380 | len = dev->rx_buf_len; |
| 381 | buf = dev->rx_buf; |
| 382 | } |
| 383 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 384 | rx_valid = dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 385 | |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 386 | for (; len > 0 && rx_valid > 0; len--, rx_valid--) { |
Tin Huynh | c3ae106 | 2016-11-10 09:56:33 +0700 | [diff] [blame] | 387 | u32 flags = msgs[dev->msg_read_idx].flags; |
| 388 | |
| 389 | *buf = dw_readl(dev, DW_IC_DATA_CMD); |
| 390 | /* Ensure length byte is a valid value */ |
| 391 | if (flags & I2C_M_RECV_LEN && |
| 392 | *buf <= I2C_SMBUS_BLOCK_MAX && *buf > 0) { |
| 393 | len = i2c_dw_recv_len(dev, *buf); |
| 394 | } |
| 395 | buf++; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 396 | dev->rx_outstanding--; |
| 397 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 398 | |
| 399 | if (len > 0) { |
| 400 | dev->status |= STATUS_READ_IN_PROGRESS; |
| 401 | dev->rx_buf_len = len; |
| 402 | dev->rx_buf = buf; |
| 403 | return; |
| 404 | } else |
| 405 | dev->status &= ~STATUS_READ_IN_PROGRESS; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /* |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 410 | * Prepare controller for a transaction and call i2c_dw_xfer_msg. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 411 | */ |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 412 | static int |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 413 | i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 414 | { |
| 415 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 416 | int ret; |
| 417 | |
| 418 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
| 419 | |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 420 | pm_runtime_get_sync(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 421 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 422 | reinit_completion(&dev->cmd_complete); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 423 | dev->msgs = msgs; |
| 424 | dev->msgs_num = num; |
| 425 | dev->cmd_err = 0; |
| 426 | dev->msg_write_idx = 0; |
| 427 | dev->msg_read_idx = 0; |
| 428 | dev->msg_err = 0; |
| 429 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 430 | dev->abort_source = 0; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 431 | dev->rx_outstanding = 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 432 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 433 | ret = i2c_dw_acquire_lock(dev); |
| 434 | if (ret) |
| 435 | goto done_nolock; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 436 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 437 | ret = i2c_dw_wait_bus_not_busy(dev); |
| 438 | if (ret < 0) |
| 439 | goto done; |
| 440 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 441 | /* Start the transfers */ |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 442 | i2c_dw_xfer_init(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 443 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 444 | /* Wait for tx to complete */ |
Weifeng Voon | d0bcd8d | 2016-06-17 09:46:35 +0800 | [diff] [blame] | 445 | if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 446 | dev_err(dev->dev, "controller timed out\n"); |
Christian Ruppert | 38d7fad | 2013-06-07 10:51:23 +0200 | [diff] [blame] | 447 | /* i2c_dw_init implicitly disables the adapter */ |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 448 | i2c_recover_bus(&dev->adapter); |
Jarkko Nikula | 21bf440 | 2017-06-28 17:23:28 +0300 | [diff] [blame] | 449 | i2c_dw_init_master(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 450 | ret = -ETIMEDOUT; |
| 451 | goto done; |
Mika Westerberg | e42dba5 | 2013-05-22 13:03:11 +0300 | [diff] [blame] | 452 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 453 | |
Jarkko Nikula | 89119f0 | 2016-11-25 17:22:27 +0200 | [diff] [blame] | 454 | /* |
| 455 | * We must disable the adapter before returning and signaling the end |
| 456 | * of the current transfer. Otherwise the hardware might continue |
| 457 | * generating interrupts which in turn causes a race condition with |
| 458 | * the following transfer. Needs some more investigation if the |
| 459 | * additional interrupts are a hardware bug or this driver doesn't |
| 460 | * handle them correctly yet. |
| 461 | */ |
| 462 | __i2c_dw_enable(dev, false); |
| 463 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 464 | if (dev->msg_err) { |
| 465 | ret = dev->msg_err; |
| 466 | goto done; |
| 467 | } |
| 468 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 469 | /* No error */ |
Russell King | 2bf413d | 2016-11-18 19:40:04 +0000 | [diff] [blame] | 470 | if (likely(!dev->cmd_err && !dev->status)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 471 | ret = num; |
| 472 | goto done; |
| 473 | } |
| 474 | |
| 475 | /* We have an error */ |
| 476 | if (dev->cmd_err == DW_IC_ERR_TX_ABRT) { |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 477 | ret = i2c_dw_handle_tx_abort(dev); |
| 478 | goto done; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 479 | } |
Russell King | 2bf413d | 2016-11-18 19:40:04 +0000 | [diff] [blame] | 480 | |
| 481 | if (dev->status) |
| 482 | dev_err(dev->dev, |
| 483 | "transfer terminated early - interrupt latency too high?\n"); |
| 484 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 485 | ret = -EIO; |
| 486 | |
| 487 | done: |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame] | 488 | i2c_dw_release_lock(dev); |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 489 | |
| 490 | done_nolock: |
Mika Westerberg | 4345233 | 2013-04-10 00:36:42 +0000 | [diff] [blame] | 491 | pm_runtime_mark_last_busy(dev->dev); |
| 492 | pm_runtime_put_autosuspend(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
Bhumika Goyal | 92d9d0d | 2017-01-27 23:36:17 +0530 | [diff] [blame] | 497 | static const struct i2c_algorithm i2c_dw_algo = { |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 498 | .master_xfer = i2c_dw_xfer, |
| 499 | .functionality = i2c_dw_func, |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 500 | }; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 501 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 502 | static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) |
| 503 | { |
| 504 | u32 stat; |
| 505 | |
| 506 | /* |
| 507 | * The IC_INTR_STAT register just indicates "enabled" interrupts. |
| 508 | * Ths unmasked raw version of interrupt status bits are available |
| 509 | * in the IC_RAW_INTR_STAT register. |
| 510 | * |
| 511 | * That is, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 512 | * stat = dw_readl(IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 513 | * equals to, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 514 | * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 515 | * |
| 516 | * The raw version might be useful for debugging purposes. |
| 517 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 518 | stat = dw_readl(dev, DW_IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 519 | |
| 520 | /* |
| 521 | * Do not use the IC_CLR_INTR register to clear interrupts, or |
| 522 | * you'll miss some interrupts, triggered during the period from |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 523 | * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR). |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 524 | * |
| 525 | * Instead, use the separately-prepared IC_CLR_* registers. |
| 526 | */ |
| 527 | if (stat & DW_IC_INTR_RX_UNDER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 528 | dw_readl(dev, DW_IC_CLR_RX_UNDER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 529 | if (stat & DW_IC_INTR_RX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 530 | dw_readl(dev, DW_IC_CLR_RX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 531 | if (stat & DW_IC_INTR_TX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 532 | dw_readl(dev, DW_IC_CLR_TX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 533 | if (stat & DW_IC_INTR_RD_REQ) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 534 | dw_readl(dev, DW_IC_CLR_RD_REQ); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 535 | if (stat & DW_IC_INTR_TX_ABRT) { |
| 536 | /* |
| 537 | * The IC_TX_ABRT_SOURCE register is cleared whenever |
| 538 | * the IC_CLR_TX_ABRT is read. Preserve it beforehand. |
| 539 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 540 | dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE); |
| 541 | dw_readl(dev, DW_IC_CLR_TX_ABRT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 542 | } |
| 543 | if (stat & DW_IC_INTR_RX_DONE) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 544 | dw_readl(dev, DW_IC_CLR_RX_DONE); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 545 | if (stat & DW_IC_INTR_ACTIVITY) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 546 | dw_readl(dev, DW_IC_CLR_ACTIVITY); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 547 | if (stat & DW_IC_INTR_STOP_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 548 | dw_readl(dev, DW_IC_CLR_STOP_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 549 | if (stat & DW_IC_INTR_START_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 550 | dw_readl(dev, DW_IC_CLR_START_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 551 | if (stat & DW_IC_INTR_GEN_CALL) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 552 | dw_readl(dev, DW_IC_CLR_GEN_CALL); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 553 | |
| 554 | return stat; |
| 555 | } |
| 556 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 557 | /* |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 558 | * Interrupt service routine. This gets called whenever an I2C master interrupt |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 559 | * occurs. |
| 560 | */ |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 561 | static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 562 | { |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 563 | u32 stat; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 564 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 565 | stat = i2c_dw_read_clear_intrbits(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 566 | if (stat & DW_IC_INTR_TX_ABRT) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 567 | dev->cmd_err |= DW_IC_ERR_TX_ABRT; |
| 568 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * Anytime TX_ABRT is set, the contents of the tx/rx |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 572 | * buffers are flushed. Make sure to skip them. |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 573 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 574 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 575 | goto tx_aborted; |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 576 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 577 | |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 578 | if (stat & DW_IC_INTR_RX_FULL) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 579 | i2c_dw_read(dev); |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 580 | |
| 581 | if (stat & DW_IC_INTR_TX_EMPTY) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 582 | i2c_dw_xfer_msg(dev); |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * No need to modify or disable the interrupt mask here. |
| 586 | * i2c_dw_xfer_msg() will take care of it according to |
| 587 | * the current transmit status. |
| 588 | */ |
| 589 | |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 590 | tx_aborted: |
Jarkko Nikula | 89119f0 | 2016-11-25 17:22:27 +0200 | [diff] [blame] | 591 | if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 592 | complete(&dev->cmd_complete); |
Hans de Goede | 86524e5 | 2017-02-10 11:27:53 +0100 | [diff] [blame] | 593 | else if (unlikely(dev->flags & ACCESS_INTR_MASK)) { |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 594 | /* Workaround to trigger pending interrupt */ |
Xiangliang Yu | 2d244c8 | 2015-12-11 20:02:53 +0800 | [diff] [blame] | 595 | stat = dw_readl(dev, DW_IC_INTR_MASK); |
| 596 | i2c_dw_disable_int(dev); |
| 597 | dw_writel(dev, stat, DW_IC_INTR_MASK); |
| 598 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 599 | |
Luis Oliveira | 89a1e1b | 2017-06-14 11:43:22 +0100 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) |
| 604 | { |
| 605 | struct dw_i2c_dev *dev = dev_id; |
| 606 | u32 stat, enabled; |
| 607 | |
| 608 | enabled = dw_readl(dev, DW_IC_ENABLE); |
| 609 | stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); |
| 610 | dev_dbg(dev->dev, "enabled=%#x stat=%#x\n", enabled, stat); |
| 611 | if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY)) |
| 612 | return IRQ_NONE; |
| 613 | |
| 614 | i2c_dw_irq_handler_master(dev); |
| 615 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 616 | return IRQ_HANDLED; |
| 617 | } |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 618 | |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 619 | static void i2c_dw_prepare_recovery(struct i2c_adapter *adap) |
| 620 | { |
| 621 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 622 | |
| 623 | i2c_dw_disable(dev); |
| 624 | reset_control_assert(dev->rst); |
| 625 | i2c_dw_prepare_clk(dev, false); |
| 626 | } |
| 627 | |
| 628 | static void i2c_dw_unprepare_recovery(struct i2c_adapter *adap) |
| 629 | { |
| 630 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 631 | |
| 632 | i2c_dw_prepare_clk(dev, true); |
| 633 | reset_control_deassert(dev->rst); |
| 634 | i2c_dw_init_master(dev); |
| 635 | } |
| 636 | |
| 637 | static int i2c_dw_init_recovery_info(struct dw_i2c_dev *dev) |
| 638 | { |
| 639 | struct i2c_bus_recovery_info *rinfo = &dev->rinfo; |
| 640 | struct i2c_adapter *adap = &dev->adapter; |
| 641 | struct gpio_desc *gpio; |
| 642 | int r; |
| 643 | |
| 644 | gpio = devm_gpiod_get(dev->dev, "scl", GPIOD_OUT_HIGH); |
| 645 | if (IS_ERR(gpio)) { |
| 646 | r = PTR_ERR(gpio); |
Andy Shevchenko | d1fa745 | 2018-02-17 22:58:43 +0200 | [diff] [blame] | 647 | if (r == -ENOENT || r == -ENOSYS) |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 648 | return 0; |
| 649 | return r; |
| 650 | } |
| 651 | rinfo->scl_gpiod = gpio; |
| 652 | |
| 653 | gpio = devm_gpiod_get_optional(dev->dev, "sda", GPIOD_IN); |
| 654 | if (IS_ERR(gpio)) |
| 655 | return PTR_ERR(gpio); |
| 656 | rinfo->sda_gpiod = gpio; |
| 657 | |
| 658 | rinfo->recover_bus = i2c_generic_scl_recovery; |
| 659 | rinfo->prepare_recovery = i2c_dw_prepare_recovery; |
| 660 | rinfo->unprepare_recovery = i2c_dw_unprepare_recovery; |
| 661 | adap->bus_recovery_info = rinfo; |
| 662 | |
| 663 | dev_info(dev->dev, "running with gpio recovery mode! scl%s", |
| 664 | rinfo->sda_gpiod ? ",sda" : ""); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 669 | int i2c_dw_probe(struct dw_i2c_dev *dev) |
| 670 | { |
| 671 | struct i2c_adapter *adap = &dev->adapter; |
Hans de Goede | 41c80b8 | 2017-03-13 23:25:09 +0100 | [diff] [blame] | 672 | unsigned long irq_flags; |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 673 | int ret; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 674 | |
| 675 | init_completion(&dev->cmd_complete); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 676 | |
Jarkko Nikula | 21bf440 | 2017-06-28 17:23:28 +0300 | [diff] [blame] | 677 | dev->init = i2c_dw_init_master; |
Luis Oliveira | 9031235 | 2017-06-14 11:43:23 +0100 | [diff] [blame] | 678 | dev->disable = i2c_dw_disable; |
| 679 | dev->disable_int = i2c_dw_disable_int; |
| 680 | |
| 681 | ret = dev->init(dev); |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 682 | if (ret) |
| 683 | return ret; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 684 | |
| 685 | snprintf(adap->name, sizeof(adap->name), |
| 686 | "Synopsys DesignWare I2C adapter"); |
Baruch Siach | 8d22f30 | 2015-12-23 18:43:24 +0200 | [diff] [blame] | 687 | adap->retries = 3; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 688 | adap->algo = &i2c_dw_algo; |
| 689 | adap->dev.parent = dev->dev; |
| 690 | i2c_set_adapdata(adap, dev); |
| 691 | |
Hans de Goede | 41c80b8 | 2017-03-13 23:25:09 +0100 | [diff] [blame] | 692 | if (dev->pm_disabled) { |
| 693 | dev_pm_syscore_device(dev->dev, true); |
| 694 | irq_flags = IRQF_NO_SUSPEND; |
| 695 | } else { |
| 696 | irq_flags = IRQF_SHARED | IRQF_COND_SUSPEND; |
| 697 | } |
| 698 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 699 | i2c_dw_disable_int(dev); |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 700 | ret = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, irq_flags, |
| 701 | dev_name(dev->dev), dev); |
| 702 | if (ret) { |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 703 | dev_err(dev->dev, "failure requesting irq %i: %d\n", |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 704 | dev->irq, ret); |
| 705 | return ret; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 706 | } |
| 707 | |
Tim Sander | ca382f5 | 2017-11-02 10:40:27 +0800 | [diff] [blame] | 708 | ret = i2c_dw_init_recovery_info(dev); |
| 709 | if (ret) |
| 710 | return ret; |
| 711 | |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 712 | /* |
| 713 | * Increment PM usage count during adapter registration in order to |
| 714 | * avoid possible spurious runtime suspend when adapter device is |
| 715 | * registered to the device core and immediate resume in case bus has |
| 716 | * registered I2C slaves that do I2C transfers in their probe. |
| 717 | */ |
| 718 | pm_runtime_get_noresume(dev->dev); |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 719 | ret = i2c_add_numbered_adapter(adap); |
| 720 | if (ret) |
| 721 | dev_err(dev->dev, "failure adding adapter: %d\n", ret); |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 722 | pm_runtime_put_noidle(dev->dev); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 723 | |
Luis Oliveira | e393f67 | 2017-06-14 11:43:21 +0100 | [diff] [blame] | 724 | return ret; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 725 | } |
| 726 | EXPORT_SYMBOL_GPL(i2c_dw_probe); |
| 727 | |
Luis Oliveira | 9031235 | 2017-06-14 11:43:23 +0100 | [diff] [blame] | 728 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus master adapter"); |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 729 | MODULE_LICENSE("GPL"); |