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 | */ |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 24 | #include <linux/export.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 25 | #include <linux/errno.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 26 | #include <linux/err.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 27 | #include <linux/i2c.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 29 | #include <linux/io.h> |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 31 | #include <linux/delay.h> |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 32 | #include <linux/module.h> |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 33 | #include "i2c-designware-core.h" |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 34 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Registers offset |
| 37 | */ |
| 38 | #define DW_IC_CON 0x0 |
| 39 | #define DW_IC_TAR 0x4 |
| 40 | #define DW_IC_DATA_CMD 0x10 |
| 41 | #define DW_IC_SS_SCL_HCNT 0x14 |
| 42 | #define DW_IC_SS_SCL_LCNT 0x18 |
| 43 | #define DW_IC_FS_SCL_HCNT 0x1c |
| 44 | #define DW_IC_FS_SCL_LCNT 0x20 |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 45 | #define DW_IC_HS_SCL_HCNT 0x24 |
| 46 | #define DW_IC_HS_SCL_LCNT 0x28 |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 47 | #define DW_IC_INTR_STAT 0x2c |
| 48 | #define DW_IC_INTR_MASK 0x30 |
| 49 | #define DW_IC_RAW_INTR_STAT 0x34 |
| 50 | #define DW_IC_RX_TL 0x38 |
| 51 | #define DW_IC_TX_TL 0x3c |
| 52 | #define DW_IC_CLR_INTR 0x40 |
| 53 | #define DW_IC_CLR_RX_UNDER 0x44 |
| 54 | #define DW_IC_CLR_RX_OVER 0x48 |
| 55 | #define DW_IC_CLR_TX_OVER 0x4c |
| 56 | #define DW_IC_CLR_RD_REQ 0x50 |
| 57 | #define DW_IC_CLR_TX_ABRT 0x54 |
| 58 | #define DW_IC_CLR_RX_DONE 0x58 |
| 59 | #define DW_IC_CLR_ACTIVITY 0x5c |
| 60 | #define DW_IC_CLR_STOP_DET 0x60 |
| 61 | #define DW_IC_CLR_START_DET 0x64 |
| 62 | #define DW_IC_CLR_GEN_CALL 0x68 |
| 63 | #define DW_IC_ENABLE 0x6c |
| 64 | #define DW_IC_STATUS 0x70 |
| 65 | #define DW_IC_TXFLR 0x74 |
| 66 | #define DW_IC_RXFLR 0x78 |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 67 | #define DW_IC_SDA_HOLD 0x7c |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 68 | #define DW_IC_TX_ABRT_SOURCE 0x80 |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 69 | #define DW_IC_ENABLE_STATUS 0x9c |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 70 | #define DW_IC_COMP_PARAM_1 0xf4 |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 71 | #define DW_IC_COMP_VERSION 0xf8 |
| 72 | #define DW_IC_SDA_HOLD_MIN_VERS 0x3131312A |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 73 | #define DW_IC_COMP_TYPE 0xfc |
| 74 | #define DW_IC_COMP_TYPE_VALUE 0x44570140 |
| 75 | |
| 76 | #define DW_IC_INTR_RX_UNDER 0x001 |
| 77 | #define DW_IC_INTR_RX_OVER 0x002 |
| 78 | #define DW_IC_INTR_RX_FULL 0x004 |
| 79 | #define DW_IC_INTR_TX_OVER 0x008 |
| 80 | #define DW_IC_INTR_TX_EMPTY 0x010 |
| 81 | #define DW_IC_INTR_RD_REQ 0x020 |
| 82 | #define DW_IC_INTR_TX_ABRT 0x040 |
| 83 | #define DW_IC_INTR_RX_DONE 0x080 |
| 84 | #define DW_IC_INTR_ACTIVITY 0x100 |
| 85 | #define DW_IC_INTR_STOP_DET 0x200 |
| 86 | #define DW_IC_INTR_START_DET 0x400 |
| 87 | #define DW_IC_INTR_GEN_CALL 0x800 |
| 88 | |
| 89 | #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \ |
| 90 | DW_IC_INTR_TX_EMPTY | \ |
| 91 | DW_IC_INTR_TX_ABRT | \ |
| 92 | DW_IC_INTR_STOP_DET) |
| 93 | |
| 94 | #define DW_IC_STATUS_ACTIVITY 0x1 |
| 95 | |
| 96 | #define DW_IC_ERR_TX_ABRT 0x1 |
| 97 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 98 | #define DW_IC_TAR_10BITADDR_MASTER BIT(12) |
| 99 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 100 | #define DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH (BIT(2) | BIT(3)) |
| 101 | #define DW_IC_COMP_PARAM_1_SPEED_MODE_MASK GENMASK(3, 2) |
| 102 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 103 | /* |
| 104 | * status codes |
| 105 | */ |
| 106 | #define STATUS_IDLE 0x0 |
| 107 | #define STATUS_WRITE_IN_PROGRESS 0x1 |
| 108 | #define STATUS_READ_IN_PROGRESS 0x2 |
| 109 | |
| 110 | #define TIMEOUT 20 /* ms */ |
| 111 | |
| 112 | /* |
| 113 | * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register |
| 114 | * |
| 115 | * only expected abort codes are listed here |
| 116 | * refer to the datasheet for the full list |
| 117 | */ |
| 118 | #define ABRT_7B_ADDR_NOACK 0 |
| 119 | #define ABRT_10ADDR1_NOACK 1 |
| 120 | #define ABRT_10ADDR2_NOACK 2 |
| 121 | #define ABRT_TXDATA_NOACK 3 |
| 122 | #define ABRT_GCALL_NOACK 4 |
| 123 | #define ABRT_GCALL_READ 5 |
| 124 | #define ABRT_SBYTE_ACKDET 7 |
| 125 | #define ABRT_SBYTE_NORSTRT 9 |
| 126 | #define ABRT_10B_RD_NORSTRT 10 |
| 127 | #define ABRT_MASTER_DIS 11 |
| 128 | #define ARB_LOST 12 |
| 129 | |
| 130 | #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK) |
| 131 | #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK) |
| 132 | #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK) |
| 133 | #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK) |
| 134 | #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK) |
| 135 | #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ) |
| 136 | #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET) |
| 137 | #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT) |
| 138 | #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT) |
| 139 | #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS) |
| 140 | #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST) |
| 141 | |
| 142 | #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \ |
| 143 | DW_IC_TX_ABRT_10ADDR1_NOACK | \ |
| 144 | DW_IC_TX_ABRT_10ADDR2_NOACK | \ |
| 145 | DW_IC_TX_ABRT_TXDATA_NOACK | \ |
| 146 | DW_IC_TX_ABRT_GCALL_NOACK) |
| 147 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 148 | static char *abort_sources[] = { |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 149 | [ABRT_7B_ADDR_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 150 | "slave address not acknowledged (7bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 151 | [ABRT_10ADDR1_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 152 | "first address byte not acknowledged (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 153 | [ABRT_10ADDR2_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 154 | "second address byte not acknowledged (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 155 | [ABRT_TXDATA_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 156 | "data not acknowledged", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 157 | [ABRT_GCALL_NOACK] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 158 | "no acknowledgement for a general call", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 159 | [ABRT_GCALL_READ] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 160 | "read after general call", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 161 | [ABRT_SBYTE_ACKDET] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 162 | "start byte acknowledged", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 163 | [ABRT_SBYTE_NORSTRT] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 164 | "trying to send start byte when restart is disabled", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 165 | [ABRT_10B_RD_NORSTRT] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 166 | "trying to read when restart is disabled (10bit mode)", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 167 | [ABRT_MASTER_DIS] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 168 | "trying to use disabled adapter", |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 169 | [ARB_LOST] = |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 170 | "lost arbitration", |
| 171 | }; |
| 172 | |
Jarkko Nikula | 8a43745 | 2015-08-31 17:31:31 +0300 | [diff] [blame] | 173 | static u32 dw_readl(struct dw_i2c_dev *dev, int offset) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 174 | { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 175 | u32 value; |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 176 | |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 177 | if (dev->accessor_flags & ACCESS_16BIT) |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 178 | value = readw_relaxed(dev->base + offset) | |
| 179 | (readw_relaxed(dev->base + offset + 2) << 16); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 180 | else |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 181 | value = readl_relaxed(dev->base + offset); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 182 | |
| 183 | if (dev->accessor_flags & ACCESS_SWAP) |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 184 | return swab32(value); |
| 185 | else |
| 186 | return value; |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jarkko Nikula | 8a43745 | 2015-08-31 17:31:31 +0300 | [diff] [blame] | 189 | static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 190 | { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 191 | if (dev->accessor_flags & ACCESS_SWAP) |
Jean-Hugues Deschenes | 18c4089 | 2011-10-06 11:26:27 -0700 | [diff] [blame] | 192 | b = swab32(b); |
| 193 | |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 194 | if (dev->accessor_flags & ACCESS_16BIT) { |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 195 | writew_relaxed((u16)b, dev->base + offset); |
| 196 | writew_relaxed((u16)(b >> 16), dev->base + offset + 2); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 197 | } else { |
Jisheng Zhang | 67105c5 | 2014-12-11 14:26:41 +0800 | [diff] [blame] | 198 | writel_relaxed(b, dev->base + offset); |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 199 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 202 | static u32 |
| 203 | i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) |
| 204 | { |
| 205 | /* |
| 206 | * DesignWare I2C core doesn't seem to have solid strategy to meet |
| 207 | * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec |
| 208 | * will result in violation of the tHD;STA spec. |
| 209 | */ |
| 210 | if (cond) |
| 211 | /* |
| 212 | * Conditional expression: |
| 213 | * |
| 214 | * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH |
| 215 | * |
| 216 | * This is based on the DW manuals, and represents an ideal |
| 217 | * configuration. The resulting I2C bus speed will be |
| 218 | * faster than any of the others. |
| 219 | * |
| 220 | * If your hardware is free from tHD;STA issue, try this one. |
| 221 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 222 | return (ic_clk * tSYMBOL + 500000) / 1000000 - 8 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 223 | else |
| 224 | /* |
| 225 | * Conditional expression: |
| 226 | * |
| 227 | * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) |
| 228 | * |
| 229 | * This is just experimental rule; the tHD;STA period turned |
| 230 | * out to be proportinal to (_HCNT + 3). With this setting, |
| 231 | * we could meet both tHIGH and tHD;STA timing specs. |
| 232 | * |
| 233 | * If unsure, you'd better to take this alternative. |
| 234 | * |
| 235 | * The reason why we need to take into account "tf" here, |
| 236 | * is the same as described in i2c_dw_scl_lcnt(). |
| 237 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 238 | return (ic_clk * (tSYMBOL + tf) + 500000) / 1000000 |
| 239 | - 3 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) |
| 243 | { |
| 244 | /* |
| 245 | * Conditional expression: |
| 246 | * |
| 247 | * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) |
| 248 | * |
| 249 | * DW I2C core starts counting the SCL CNTs for the LOW period |
| 250 | * of the SCL clock (tLOW) as soon as it pulls the SCL line. |
| 251 | * In order to meet the tLOW timing spec, we need to take into |
| 252 | * account the fall time of SCL signal (tf). Default tf value |
| 253 | * should be 0.3 us, for safety. |
| 254 | */ |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 255 | return ((ic_clk * (tLOW + tf) + 500000) / 1000000) - 1 + offset; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 258 | static void __i2c_dw_enable(struct dw_i2c_dev *dev, bool enable) |
| 259 | { |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 260 | dw_writel(dev, enable, DW_IC_ENABLE); |
| 261 | } |
| 262 | |
| 263 | static void __i2c_dw_enable_and_wait(struct dw_i2c_dev *dev, bool enable) |
| 264 | { |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 265 | int timeout = 100; |
| 266 | |
| 267 | do { |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 268 | __i2c_dw_enable(dev, enable); |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 269 | if ((dw_readl(dev, DW_IC_ENABLE_STATUS) & 1) == enable) |
| 270 | return; |
| 271 | |
| 272 | /* |
| 273 | * Wait 10 times the signaling period of the highest I2C |
| 274 | * transfer supported by the driver (for 400KHz this is |
| 275 | * 25us) as described in the DesignWare I2C databook. |
| 276 | */ |
| 277 | usleep_range(25, 250); |
| 278 | } while (timeout--); |
| 279 | |
| 280 | dev_warn(dev->dev, "timeout in %sabling adapter\n", |
| 281 | enable ? "en" : "dis"); |
| 282 | } |
| 283 | |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 284 | static unsigned long i2c_dw_clk_rate(struct dw_i2c_dev *dev) |
| 285 | { |
| 286 | /* |
| 287 | * Clock is not necessary if we got LCNT/HCNT values directly from |
| 288 | * the platform code. |
| 289 | */ |
| 290 | if (WARN_ON_ONCE(!dev->get_clk_rate_khz)) |
| 291 | return 0; |
| 292 | return dev->get_clk_rate_khz(dev); |
| 293 | } |
| 294 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 295 | static int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) |
| 296 | { |
| 297 | int ret; |
| 298 | |
| 299 | if (!dev->acquire_lock) |
| 300 | return 0; |
| 301 | |
| 302 | ret = dev->acquire_lock(dev); |
| 303 | if (!ret) |
| 304 | return 0; |
| 305 | |
| 306 | dev_err(dev->dev, "couldn't acquire bus ownership\n"); |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static void i2c_dw_release_lock(struct dw_i2c_dev *dev) |
| 312 | { |
| 313 | if (dev->release_lock) |
| 314 | dev->release_lock(dev); |
| 315 | } |
| 316 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 317 | /** |
| 318 | * i2c_dw_init() - initialize the designware i2c master hardware |
| 319 | * @dev: device private data |
| 320 | * |
| 321 | * This functions configures and enables the I2C master. |
| 322 | * This function is called during I2C init function, and in case of timeout at |
| 323 | * run time. |
| 324 | */ |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 325 | int i2c_dw_init(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 326 | { |
Dirk Brandewie | e18563f | 2011-10-06 11:26:32 -0700 | [diff] [blame] | 327 | u32 hcnt, lcnt; |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 328 | u32 reg, comp_param1; |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 329 | u32 sda_falling_time, scl_falling_time; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 330 | int ret; |
| 331 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 332 | ret = i2c_dw_acquire_lock(dev); |
| 333 | if (ret) |
| 334 | return ret; |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 335 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 336 | reg = dw_readl(dev, DW_IC_COMP_TYPE); |
| 337 | if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) { |
Stefan Roese | a8a9f3f | 2012-04-18 15:01:41 +0200 | [diff] [blame] | 338 | /* Configure register endianess access */ |
| 339 | dev->accessor_flags |= ACCESS_SWAP; |
| 340 | } else if (reg == (DW_IC_COMP_TYPE_VALUE & 0x0000ffff)) { |
| 341 | /* Configure register access mode 16bit */ |
| 342 | dev->accessor_flags |= ACCESS_16BIT; |
| 343 | } else if (reg != DW_IC_COMP_TYPE_VALUE) { |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 344 | dev_err(dev->dev, "Unknown Synopsys component type: " |
| 345 | "0x%08x\n", reg); |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 346 | i2c_dw_release_lock(dev); |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 347 | return -ENODEV; |
| 348 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 349 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 350 | comp_param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 351 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 352 | /* Disable the adapter */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 353 | __i2c_dw_enable_and_wait(dev, false); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 354 | |
| 355 | /* set standard and fast speed deviders for high/low periods */ |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 356 | |
Romain Baeriswyl | 6468276 | 2014-01-20 17:43:43 +0100 | [diff] [blame] | 357 | sda_falling_time = dev->sda_falling_time ?: 300; /* ns */ |
| 358 | scl_falling_time = dev->scl_falling_time ?: 300; /* ns */ |
| 359 | |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 360 | /* Set SCL timing parameters for standard-mode */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 361 | if (dev->ss_hcnt && dev->ss_lcnt) { |
| 362 | hcnt = dev->ss_hcnt; |
| 363 | lcnt = dev->ss_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 364 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 365 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 366 | 4000, /* tHD;STA = tHIGH = 4.0 us */ |
| 367 | sda_falling_time, |
| 368 | 0, /* 0: DW default, 1: Ideal */ |
| 369 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 370 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 371 | 4700, /* tLOW = 4.7 us */ |
| 372 | scl_falling_time, |
| 373 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 374 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 375 | dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); |
| 376 | dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 377 | dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
| 378 | |
Weifeng Voon | d608c3d | 2016-08-12 17:02:49 +0300 | [diff] [blame] | 379 | /* Set SCL timing parameters for fast-mode or fast-mode plus */ |
| 380 | if ((dev->clk_freq == 1000000) && dev->fp_hcnt && dev->fp_lcnt) { |
| 381 | hcnt = dev->fp_hcnt; |
| 382 | lcnt = dev->fp_lcnt; |
| 383 | } else if (dev->fs_hcnt && dev->fs_lcnt) { |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 384 | hcnt = dev->fs_hcnt; |
| 385 | lcnt = dev->fs_lcnt; |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 386 | } else { |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 387 | hcnt = i2c_dw_scl_hcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 388 | 600, /* tHD;STA = tHIGH = 0.6 us */ |
| 389 | sda_falling_time, |
| 390 | 0, /* 0: DW default, 1: Ideal */ |
| 391 | 0); /* No offset */ |
Suravee Suthikulpanit | b33af11 | 2016-01-04 09:17:35 -0600 | [diff] [blame] | 392 | lcnt = i2c_dw_scl_lcnt(i2c_dw_clk_rate(dev), |
Jarkko Nikula | 42ffd39 | 2015-01-23 11:35:55 +0200 | [diff] [blame] | 393 | 1300, /* tLOW = 1.3 us */ |
| 394 | scl_falling_time, |
| 395 | 0); /* No offset */ |
Mika Westerberg | defc0b2 | 2013-08-19 15:07:53 +0300 | [diff] [blame] | 396 | } |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 397 | dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); |
| 398 | dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 399 | 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] | 400 | |
Weifeng Voon | b6e6714 | 2016-08-12 17:02:51 +0300 | [diff] [blame] | 401 | if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) == |
| 402 | DW_IC_CON_SPEED_HIGH) { |
| 403 | if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK) |
| 404 | != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) { |
| 405 | dev_err(dev->dev, "High Speed not supported!\n"); |
| 406 | dev->master_cfg &= ~DW_IC_CON_SPEED_MASK; |
| 407 | dev->master_cfg |= DW_IC_CON_SPEED_FAST; |
| 408 | } else if (dev->hs_hcnt && dev->hs_lcnt) { |
| 409 | hcnt = dev->hs_hcnt; |
| 410 | lcnt = dev->hs_lcnt; |
| 411 | dw_writel(dev, hcnt, DW_IC_HS_SCL_HCNT); |
| 412 | dw_writel(dev, lcnt, DW_IC_HS_SCL_LCNT); |
| 413 | dev_dbg(dev->dev, "HighSpeed-mode HCNT:LCNT = %d:%d\n", |
| 414 | hcnt, lcnt); |
| 415 | } |
| 416 | } |
| 417 | |
Christian Ruppert | 9803f86 | 2013-06-26 10:55:06 +0200 | [diff] [blame] | 418 | /* Configure SDA Hold Time if required */ |
| 419 | if (dev->sda_hold_time) { |
| 420 | reg = dw_readl(dev, DW_IC_COMP_VERSION); |
| 421 | if (reg >= DW_IC_SDA_HOLD_MIN_VERS) |
| 422 | dw_writel(dev, dev->sda_hold_time, DW_IC_SDA_HOLD); |
| 423 | else |
| 424 | dev_warn(dev->dev, |
| 425 | "Hardware too old to adjust SDA hold time."); |
| 426 | } |
| 427 | |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 428 | /* Configure Tx/Rx FIFO threshold levels */ |
Andrew Jackson | d39f77b | 2014-11-07 12:10:44 +0000 | [diff] [blame] | 429 | dw_writel(dev, dev->tx_fifo_depth / 2, DW_IC_TX_TL); |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 430 | dw_writel(dev, 0, DW_IC_RX_TL); |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 431 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 432 | /* configure the i2c master */ |
Dirk Brandewie | e18563f | 2011-10-06 11:26:32 -0700 | [diff] [blame] | 433 | dw_writel(dev, dev->master_cfg , DW_IC_CON); |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 434 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 435 | i2c_dw_release_lock(dev); |
| 436 | |
Dirk Brandewie | 4a423a8 | 2011-10-06 11:26:28 -0700 | [diff] [blame] | 437 | return 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 438 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 439 | EXPORT_SYMBOL_GPL(i2c_dw_init); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * Waiting for bus not busy |
| 443 | */ |
| 444 | static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) |
| 445 | { |
| 446 | int timeout = TIMEOUT; |
| 447 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 448 | while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 449 | if (timeout <= 0) { |
| 450 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 451 | return -ETIMEDOUT; |
| 452 | } |
| 453 | timeout--; |
Mika Westerberg | 1451b91 | 2013-04-10 00:36:41 +0000 | [diff] [blame] | 454 | usleep_range(1000, 1100); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 460 | static void i2c_dw_xfer_init(struct dw_i2c_dev *dev) |
| 461 | { |
| 462 | struct i2c_msg *msgs = dev->msgs; |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 463 | u32 ic_con, ic_tar = 0; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 464 | |
| 465 | /* Disable the adapter */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 466 | __i2c_dw_enable_and_wait(dev, false); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 467 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 468 | /* if the slave address is ten bit address, enable 10BITADDR */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 469 | ic_con = dw_readl(dev, DW_IC_CON); |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 470 | if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) { |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 471 | ic_con |= DW_IC_CON_10BITADDR_MASTER; |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 472 | /* |
| 473 | * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing |
| 474 | * mode has to be enabled via bit 12 of IC_TAR register. |
| 475 | * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be |
| 476 | * detected from registers. |
| 477 | */ |
| 478 | ic_tar = DW_IC_TAR_10BITADDR_MASTER; |
| 479 | } else { |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 480 | ic_con &= ~DW_IC_CON_10BITADDR_MASTER; |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 481 | } |
| 482 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 483 | dw_writel(dev, ic_con, DW_IC_CON); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 484 | |
Chew, Chiau Ee | bd63ace | 2013-09-27 02:57:35 +0800 | [diff] [blame] | 485 | /* |
| 486 | * Set the slave (target) address and enable 10-bit addressing mode |
| 487 | * if applicable. |
| 488 | */ |
| 489 | dw_writel(dev, msgs[dev->msg_write_idx].addr | ic_tar, DW_IC_TAR); |
| 490 | |
Du, Wenkai | 47bb27e | 2014-04-10 23:03:19 +0000 | [diff] [blame] | 491 | /* enforce disabled interrupts (due to HW issues) */ |
| 492 | i2c_dw_disable_int(dev); |
| 493 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 494 | /* Enable the adapter */ |
Mika Westerberg | 3ca4ed8 | 2013-04-10 00:36:40 +0000 | [diff] [blame] | 495 | __i2c_dw_enable(dev, true); |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 496 | |
Mika Westerberg | 2a2d95e | 2013-05-13 00:54:30 +0000 | [diff] [blame] | 497 | /* Clear and enable interrupts */ |
Jarkko Nikula | c335631 | 2015-08-31 17:31:28 +0300 | [diff] [blame] | 498 | dw_readl(dev, DW_IC_CLR_INTR); |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 499 | dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 500 | } |
| 501 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 502 | /* |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 503 | * Initiate (and continue) low level master read/write transaction. |
| 504 | * This function is only called from i2c_dw_isr, and pumping i2c_msg |
| 505 | * messages into the tx buffer. Even if the size of i2c_msg data is |
| 506 | * longer than the size of the tx buffer, it handles everything. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 507 | */ |
Jean Delvare | bccd780 | 2012-10-05 22:23:53 +0200 | [diff] [blame] | 508 | static void |
Shinya Kuribayashi | e77cf23 | 2009-11-06 21:46:04 +0900 | [diff] [blame] | 509 | i2c_dw_xfer_msg(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 510 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 511 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 512 | u32 intr_mask; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 513 | int tx_limit, rx_limit; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 514 | u32 addr = msgs[dev->msg_write_idx].addr; |
| 515 | u32 buf_len = dev->tx_buf_len; |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 516 | u8 *buf = dev->tx_buf; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 517 | bool need_restart = false; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 518 | |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 519 | intr_mask = DW_IC_INTR_DEFAULT_MASK; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 520 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 521 | for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 522 | /* |
| 523 | * if target address has changed, we need to |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 524 | * reprogram the target address in the i2c |
| 525 | * adapter when we are done with this transfer |
| 526 | */ |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 527 | if (msgs[dev->msg_write_idx].addr != addr) { |
| 528 | dev_err(dev->dev, |
| 529 | "%s: invalid target address\n", __func__); |
| 530 | dev->msg_err = -EINVAL; |
| 531 | break; |
| 532 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 533 | |
| 534 | if (msgs[dev->msg_write_idx].len == 0) { |
| 535 | dev_err(dev->dev, |
| 536 | "%s: invalid message length\n", __func__); |
| 537 | dev->msg_err = -EINVAL; |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 538 | break; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { |
| 542 | /* new i2c_msg */ |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 543 | buf = msgs[dev->msg_write_idx].buf; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 544 | buf_len = msgs[dev->msg_write_idx].len; |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 545 | |
| 546 | /* If both IC_EMPTYFIFO_HOLD_MASTER_EN and |
| 547 | * IC_RESTART_EN are set, we must manually |
| 548 | * set restart bit between messages. |
| 549 | */ |
| 550 | if ((dev->master_cfg & DW_IC_CON_RESTART_EN) && |
| 551 | (dev->msg_write_idx > 0)) |
| 552 | need_restart = true; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 553 | } |
| 554 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 555 | tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR); |
| 556 | rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 557 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 558 | while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) { |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 559 | u32 cmd = 0; |
| 560 | |
| 561 | /* |
| 562 | * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must |
| 563 | * manually set the stop bit. However, it cannot be |
| 564 | * detected from the registers so we set it always |
| 565 | * when writing/reading the last byte. |
| 566 | */ |
| 567 | if (dev->msg_write_idx == dev->msgs_num - 1 && |
| 568 | buf_len == 1) |
| 569 | cmd |= BIT(9); |
| 570 | |
Chew, Chiau Ee | 8256424 | 2013-06-21 15:05:28 +0800 | [diff] [blame] | 571 | if (need_restart) { |
| 572 | cmd |= BIT(10); |
| 573 | need_restart = false; |
| 574 | } |
| 575 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 576 | if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 577 | |
| 578 | /* avoid rx buffer overrun */ |
| 579 | if (rx_limit - dev->rx_outstanding <= 0) |
| 580 | break; |
| 581 | |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 582 | dw_writel(dev, cmd | 0x100, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 583 | rx_limit--; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 584 | dev->rx_outstanding++; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 585 | } else |
Mika Westerberg | 17a76b4 | 2013-01-17 12:31:05 +0200 | [diff] [blame] | 586 | dw_writel(dev, cmd | *buf++, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 587 | tx_limit--; buf_len--; |
| 588 | } |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 589 | |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 590 | dev->tx_buf = buf; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 591 | dev->tx_buf_len = buf_len; |
| 592 | |
| 593 | if (buf_len > 0) { |
| 594 | /* more bytes to be written */ |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 595 | dev->status |= STATUS_WRITE_IN_PROGRESS; |
| 596 | break; |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 597 | } else |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 598 | dev->status &= ~STATUS_WRITE_IN_PROGRESS; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 599 | } |
| 600 | |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 601 | /* |
| 602 | * If i2c_msg index search is completed, we don't need TX_EMPTY |
| 603 | * interrupt any more. |
| 604 | */ |
| 605 | if (dev->msg_write_idx == dev->msgs_num) |
| 606 | intr_mask &= ~DW_IC_INTR_TX_EMPTY; |
| 607 | |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 608 | if (dev->msg_err) |
| 609 | intr_mask = 0; |
| 610 | |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 611 | dw_writel(dev, intr_mask, DW_IC_INTR_MASK); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static void |
Shinya Kuribayashi | 78839bd | 2009-11-06 21:45:39 +0900 | [diff] [blame] | 615 | i2c_dw_read(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 616 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 617 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 618 | int rx_valid; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 619 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 620 | for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) { |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 621 | u32 len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 622 | u8 *buf; |
| 623 | |
| 624 | if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD)) |
| 625 | continue; |
| 626 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 627 | if (!(dev->status & STATUS_READ_IN_PROGRESS)) { |
| 628 | len = msgs[dev->msg_read_idx].len; |
| 629 | buf = msgs[dev->msg_read_idx].buf; |
| 630 | } else { |
| 631 | len = dev->rx_buf_len; |
| 632 | buf = dev->rx_buf; |
| 633 | } |
| 634 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 635 | rx_valid = dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 636 | |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 637 | for (; len > 0 && rx_valid > 0; len--, rx_valid--) { |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 638 | *buf++ = dw_readl(dev, DW_IC_DATA_CMD); |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 639 | dev->rx_outstanding--; |
| 640 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 641 | |
| 642 | if (len > 0) { |
| 643 | dev->status |= STATUS_READ_IN_PROGRESS; |
| 644 | dev->rx_buf_len = len; |
| 645 | dev->rx_buf = buf; |
| 646 | return; |
| 647 | } else |
| 648 | dev->status &= ~STATUS_READ_IN_PROGRESS; |
| 649 | } |
| 650 | } |
| 651 | |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 652 | static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) |
| 653 | { |
| 654 | unsigned long abort_source = dev->abort_source; |
| 655 | int i; |
| 656 | |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 657 | if (abort_source & DW_IC_TX_ABRT_NOACK) { |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 658 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 659 | dev_dbg(dev->dev, |
| 660 | "%s: %s\n", __func__, abort_sources[i]); |
| 661 | return -EREMOTEIO; |
| 662 | } |
| 663 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 664 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 665 | dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); |
| 666 | |
| 667 | if (abort_source & DW_IC_TX_ARB_LOST) |
| 668 | return -EAGAIN; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 669 | else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) |
| 670 | return -EINVAL; /* wrong msgs[] data */ |
| 671 | else |
| 672 | return -EIO; |
| 673 | } |
| 674 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 675 | /* |
| 676 | * Prepare controller for a transaction and call i2c_dw_xfer_msg |
| 677 | */ |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 678 | static int |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 679 | i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 680 | { |
| 681 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 682 | int ret; |
| 683 | |
| 684 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
| 685 | |
Dirk Brandewie | 18dbdda | 2011-10-06 11:26:36 -0700 | [diff] [blame] | 686 | pm_runtime_get_sync(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 687 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 688 | reinit_completion(&dev->cmd_complete); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 689 | dev->msgs = msgs; |
| 690 | dev->msgs_num = num; |
| 691 | dev->cmd_err = 0; |
| 692 | dev->msg_write_idx = 0; |
| 693 | dev->msg_read_idx = 0; |
| 694 | dev->msg_err = 0; |
| 695 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 696 | dev->abort_source = 0; |
Josef Ahmad | e6f34ce | 2013-04-19 17:28:10 +0100 | [diff] [blame] | 697 | dev->rx_outstanding = 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 698 | |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 699 | ret = i2c_dw_acquire_lock(dev); |
| 700 | if (ret) |
| 701 | goto done_nolock; |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 702 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 703 | ret = i2c_dw_wait_bus_not_busy(dev); |
| 704 | if (ret < 0) |
| 705 | goto done; |
| 706 | |
| 707 | /* start the transfers */ |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 708 | i2c_dw_xfer_init(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 709 | |
| 710 | /* wait for tx to complete */ |
Weifeng Voon | d0bcd8d | 2016-06-17 09:46:35 +0800 | [diff] [blame] | 711 | if (!wait_for_completion_timeout(&dev->cmd_complete, adap->timeout)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 712 | dev_err(dev->dev, "controller timed out\n"); |
Christian Ruppert | 38d7fad | 2013-06-07 10:51:23 +0200 | [diff] [blame] | 713 | /* i2c_dw_init implicitly disables the adapter */ |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 714 | i2c_dw_init(dev); |
| 715 | ret = -ETIMEDOUT; |
| 716 | goto done; |
Mika Westerberg | e42dba5 | 2013-05-22 13:03:11 +0300 | [diff] [blame] | 717 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 718 | |
Christian Ruppert | 38d7fad | 2013-06-07 10:51:23 +0200 | [diff] [blame] | 719 | /* |
Baruch Siach | e3c9765 | 2016-01-12 15:16:35 +0200 | [diff] [blame] | 720 | * We must disable the adapter before returning and signaling the end |
| 721 | * of the current transfer. Otherwise the hardware might continue |
| 722 | * generating interrupts which in turn causes a race condition with |
| 723 | * the following transfer. Needs some more investigation if the |
| 724 | * additional interrupts are a hardware bug or this driver doesn't |
| 725 | * handle them correctly yet. |
Christian Ruppert | 38d7fad | 2013-06-07 10:51:23 +0200 | [diff] [blame] | 726 | */ |
| 727 | __i2c_dw_enable(dev, false); |
| 728 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 729 | if (dev->msg_err) { |
| 730 | ret = dev->msg_err; |
| 731 | goto done; |
| 732 | } |
| 733 | |
| 734 | /* no error */ |
| 735 | if (likely(!dev->cmd_err)) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 736 | ret = num; |
| 737 | goto done; |
| 738 | } |
| 739 | |
| 740 | /* We have an error */ |
| 741 | if (dev->cmd_err == DW_IC_ERR_TX_ABRT) { |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 742 | ret = i2c_dw_handle_tx_abort(dev); |
| 743 | goto done; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 744 | } |
| 745 | ret = -EIO; |
| 746 | |
| 747 | done: |
Lucas De Marchi | 8c5660b | 2016-08-23 19:18:54 -0300 | [diff] [blame^] | 748 | i2c_dw_release_lock(dev); |
David Box | c0601d2 | 2015-01-15 01:12:16 -0800 | [diff] [blame] | 749 | |
| 750 | done_nolock: |
Mika Westerberg | 4345233 | 2013-04-10 00:36:42 +0000 | [diff] [blame] | 751 | pm_runtime_mark_last_busy(dev->dev); |
| 752 | pm_runtime_put_autosuspend(dev->dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 753 | |
| 754 | return ret; |
| 755 | } |
| 756 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 757 | static u32 i2c_dw_func(struct i2c_adapter *adap) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 758 | { |
Dirk Brandewie | 2fa8326 | 2011-10-06 11:26:31 -0700 | [diff] [blame] | 759 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 760 | return dev->functionality; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 761 | } |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 762 | |
| 763 | static struct i2c_algorithm i2c_dw_algo = { |
| 764 | .master_xfer = i2c_dw_xfer, |
| 765 | .functionality = i2c_dw_func, |
| 766 | }; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 767 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 768 | static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) |
| 769 | { |
| 770 | u32 stat; |
| 771 | |
| 772 | /* |
| 773 | * The IC_INTR_STAT register just indicates "enabled" interrupts. |
| 774 | * Ths unmasked raw version of interrupt status bits are available |
| 775 | * in the IC_RAW_INTR_STAT register. |
| 776 | * |
| 777 | * That is, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 778 | * stat = dw_readl(IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 779 | * equals to, |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 780 | * stat = dw_readl(IC_RAW_INTR_STAT) & dw_readl(IC_INTR_MASK); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 781 | * |
| 782 | * The raw version might be useful for debugging purposes. |
| 783 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 784 | stat = dw_readl(dev, DW_IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 785 | |
| 786 | /* |
| 787 | * Do not use the IC_CLR_INTR register to clear interrupts, or |
| 788 | * you'll miss some interrupts, triggered during the period from |
Dirk Brandewie | 2373f6b | 2011-10-29 10:57:23 +0100 | [diff] [blame] | 789 | * dw_readl(IC_INTR_STAT) to dw_readl(IC_CLR_INTR). |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 790 | * |
| 791 | * Instead, use the separately-prepared IC_CLR_* registers. |
| 792 | */ |
| 793 | if (stat & DW_IC_INTR_RX_UNDER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 794 | dw_readl(dev, DW_IC_CLR_RX_UNDER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 795 | if (stat & DW_IC_INTR_RX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 796 | dw_readl(dev, DW_IC_CLR_RX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 797 | if (stat & DW_IC_INTR_TX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 798 | dw_readl(dev, DW_IC_CLR_TX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 799 | if (stat & DW_IC_INTR_RD_REQ) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 800 | dw_readl(dev, DW_IC_CLR_RD_REQ); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 801 | if (stat & DW_IC_INTR_TX_ABRT) { |
| 802 | /* |
| 803 | * The IC_TX_ABRT_SOURCE register is cleared whenever |
| 804 | * the IC_CLR_TX_ABRT is read. Preserve it beforehand. |
| 805 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 806 | dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE); |
| 807 | dw_readl(dev, DW_IC_CLR_TX_ABRT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 808 | } |
| 809 | if (stat & DW_IC_INTR_RX_DONE) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 810 | dw_readl(dev, DW_IC_CLR_RX_DONE); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 811 | if (stat & DW_IC_INTR_ACTIVITY) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 812 | dw_readl(dev, DW_IC_CLR_ACTIVITY); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 813 | if (stat & DW_IC_INTR_STOP_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 814 | dw_readl(dev, DW_IC_CLR_STOP_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 815 | if (stat & DW_IC_INTR_START_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 816 | dw_readl(dev, DW_IC_CLR_START_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 817 | if (stat & DW_IC_INTR_GEN_CALL) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 818 | dw_readl(dev, DW_IC_CLR_GEN_CALL); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 819 | |
| 820 | return stat; |
| 821 | } |
| 822 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 823 | /* |
| 824 | * Interrupt service routine. This gets called whenever an I2C interrupt |
| 825 | * occurs. |
| 826 | */ |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 827 | static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 828 | { |
| 829 | struct dw_i2c_dev *dev = dev_id; |
Dirk Brandewie | af06cf6 | 2011-10-06 11:26:33 -0700 | [diff] [blame] | 830 | u32 stat, enabled; |
| 831 | |
| 832 | enabled = dw_readl(dev, DW_IC_ENABLE); |
| 833 | stat = dw_readl(dev, DW_IC_RAW_INTR_STAT); |
Jarkko Nikula | fb42746 | 2015-08-07 14:53:03 +0300 | [diff] [blame] | 834 | dev_dbg(dev->dev, "%s: enabled=%#x stat=%#x\n", __func__, enabled, stat); |
Dirk Brandewie | af06cf6 | 2011-10-06 11:26:33 -0700 | [diff] [blame] | 835 | if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY)) |
| 836 | return IRQ_NONE; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 837 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 838 | stat = i2c_dw_read_clear_intrbits(dev); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 839 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 840 | if (stat & DW_IC_INTR_TX_ABRT) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 841 | dev->cmd_err |= DW_IC_ERR_TX_ABRT; |
| 842 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * Anytime TX_ABRT is set, the contents of the tx/rx |
| 846 | * buffers are flushed. Make sure to skip them. |
| 847 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame] | 848 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 849 | goto tx_aborted; |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 850 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 851 | |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 852 | if (stat & DW_IC_INTR_RX_FULL) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 853 | i2c_dw_read(dev); |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 854 | |
| 855 | if (stat & DW_IC_INTR_TX_EMPTY) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 856 | i2c_dw_xfer_msg(dev); |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * No need to modify or disable the interrupt mask here. |
| 860 | * i2c_dw_xfer_msg() will take care of it according to |
| 861 | * the current transmit status. |
| 862 | */ |
| 863 | |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 864 | tx_aborted: |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 865 | 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] | 866 | complete(&dev->cmd_complete); |
Xiangliang Yu | 2d244c8 | 2015-12-11 20:02:53 +0800 | [diff] [blame] | 867 | else if (unlikely(dev->accessor_flags & ACCESS_INTR_MASK)) { |
| 868 | /* workaround to trigger pending interrupt */ |
| 869 | stat = dw_readl(dev, DW_IC_INTR_MASK); |
| 870 | i2c_dw_disable_int(dev); |
| 871 | dw_writel(dev, stat, DW_IC_INTR_MASK); |
| 872 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 873 | |
| 874 | return IRQ_HANDLED; |
| 875 | } |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 876 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 877 | void i2c_dw_disable(struct dw_i2c_dev *dev) |
| 878 | { |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 879 | /* Disable controller */ |
José Roberto de Souza | 2702ea7 | 2016-08-23 19:18:53 -0300 | [diff] [blame] | 880 | __i2c_dw_enable_and_wait(dev, false); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 881 | |
| 882 | /* Disable all interupts */ |
| 883 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 884 | dw_readl(dev, DW_IC_CLR_INTR); |
| 885 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 886 | EXPORT_SYMBOL_GPL(i2c_dw_disable); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 887 | |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 888 | void i2c_dw_disable_int(struct dw_i2c_dev *dev) |
| 889 | { |
| 890 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
| 891 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 892 | EXPORT_SYMBOL_GPL(i2c_dw_disable_int); |
Dirk Brandewie | f3fa9f3 | 2011-10-06 11:26:34 -0700 | [diff] [blame] | 893 | |
| 894 | u32 i2c_dw_read_comp_param(struct dw_i2c_dev *dev) |
| 895 | { |
| 896 | return dw_readl(dev, DW_IC_COMP_PARAM_1); |
| 897 | } |
Axel Lin | e68bb91 | 2012-09-10 10:14:02 +0200 | [diff] [blame] | 898 | EXPORT_SYMBOL_GPL(i2c_dw_read_comp_param); |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 899 | |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 900 | int i2c_dw_probe(struct dw_i2c_dev *dev) |
| 901 | { |
| 902 | struct i2c_adapter *adap = &dev->adapter; |
| 903 | int r; |
| 904 | |
| 905 | init_completion(&dev->cmd_complete); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 906 | |
| 907 | r = i2c_dw_init(dev); |
| 908 | if (r) |
| 909 | return r; |
| 910 | |
| 911 | snprintf(adap->name, sizeof(adap->name), |
| 912 | "Synopsys DesignWare I2C adapter"); |
Baruch Siach | 8d22f30 | 2015-12-23 18:43:24 +0200 | [diff] [blame] | 913 | adap->retries = 3; |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 914 | adap->algo = &i2c_dw_algo; |
| 915 | adap->dev.parent = dev->dev; |
| 916 | i2c_set_adapdata(adap, dev); |
| 917 | |
| 918 | i2c_dw_disable_int(dev); |
Andy Shevchenko | 08c6e8c | 2016-01-15 22:02:12 +0200 | [diff] [blame] | 919 | r = devm_request_irq(dev->dev, dev->irq, i2c_dw_isr, |
| 920 | IRQF_SHARED | IRQF_COND_SUSPEND, |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 921 | dev_name(dev->dev), dev); |
| 922 | if (r) { |
| 923 | dev_err(dev->dev, "failure requesting irq %i: %d\n", |
| 924 | dev->irq, r); |
| 925 | return r; |
| 926 | } |
| 927 | |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 928 | /* |
| 929 | * Increment PM usage count during adapter registration in order to |
| 930 | * avoid possible spurious runtime suspend when adapter device is |
| 931 | * registered to the device core and immediate resume in case bus has |
| 932 | * registered I2C slaves that do I2C transfers in their probe. |
| 933 | */ |
| 934 | pm_runtime_get_noresume(dev->dev); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 935 | r = i2c_add_numbered_adapter(adap); |
| 936 | if (r) |
| 937 | dev_err(dev->dev, "failure adding adapter: %d\n", r); |
Jarkko Nikula | cd998de | 2016-02-11 16:36:03 +0200 | [diff] [blame] | 938 | pm_runtime_put_noidle(dev->dev); |
Jarkko Nikula | d80d134 | 2015-10-12 16:55:35 +0300 | [diff] [blame] | 939 | |
| 940 | return r; |
| 941 | } |
| 942 | EXPORT_SYMBOL_GPL(i2c_dw_probe); |
| 943 | |
Mika Westerberg | 9dd3162 | 2013-01-17 12:31:04 +0200 | [diff] [blame] | 944 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core"); |
| 945 | MODULE_LICENSE("GPL"); |