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. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | * ---------------------------------------------------------------------------- |
| 26 | * |
| 27 | */ |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/i2c.h> |
| 32 | #include <linux/clk.h> |
| 33 | #include <linux/errno.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/err.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/platform_device.h> |
| 38 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Registers offset |
| 43 | */ |
| 44 | #define DW_IC_CON 0x0 |
| 45 | #define DW_IC_TAR 0x4 |
| 46 | #define DW_IC_DATA_CMD 0x10 |
| 47 | #define DW_IC_SS_SCL_HCNT 0x14 |
| 48 | #define DW_IC_SS_SCL_LCNT 0x18 |
| 49 | #define DW_IC_FS_SCL_HCNT 0x1c |
| 50 | #define DW_IC_FS_SCL_LCNT 0x20 |
| 51 | #define DW_IC_INTR_STAT 0x2c |
| 52 | #define DW_IC_INTR_MASK 0x30 |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 53 | #define DW_IC_RAW_INTR_STAT 0x34 |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 54 | #define DW_IC_RX_TL 0x38 |
| 55 | #define DW_IC_TX_TL 0x3c |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 56 | #define DW_IC_CLR_INTR 0x40 |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 57 | #define DW_IC_CLR_RX_UNDER 0x44 |
| 58 | #define DW_IC_CLR_RX_OVER 0x48 |
| 59 | #define DW_IC_CLR_TX_OVER 0x4c |
| 60 | #define DW_IC_CLR_RD_REQ 0x50 |
| 61 | #define DW_IC_CLR_TX_ABRT 0x54 |
| 62 | #define DW_IC_CLR_RX_DONE 0x58 |
| 63 | #define DW_IC_CLR_ACTIVITY 0x5c |
| 64 | #define DW_IC_CLR_STOP_DET 0x60 |
| 65 | #define DW_IC_CLR_START_DET 0x64 |
| 66 | #define DW_IC_CLR_GEN_CALL 0x68 |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 67 | #define DW_IC_ENABLE 0x6c |
| 68 | #define DW_IC_STATUS 0x70 |
| 69 | #define DW_IC_TXFLR 0x74 |
| 70 | #define DW_IC_RXFLR 0x78 |
| 71 | #define DW_IC_COMP_PARAM_1 0xf4 |
| 72 | #define DW_IC_TX_ABRT_SOURCE 0x80 |
| 73 | |
| 74 | #define DW_IC_CON_MASTER 0x1 |
| 75 | #define DW_IC_CON_SPEED_STD 0x2 |
| 76 | #define DW_IC_CON_SPEED_FAST 0x4 |
| 77 | #define DW_IC_CON_10BITADDR_MASTER 0x10 |
| 78 | #define DW_IC_CON_RESTART_EN 0x20 |
| 79 | #define DW_IC_CON_SLAVE_DISABLE 0x40 |
| 80 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 81 | #define DW_IC_INTR_RX_UNDER 0x001 |
| 82 | #define DW_IC_INTR_RX_OVER 0x002 |
| 83 | #define DW_IC_INTR_RX_FULL 0x004 |
| 84 | #define DW_IC_INTR_TX_OVER 0x008 |
| 85 | #define DW_IC_INTR_TX_EMPTY 0x010 |
| 86 | #define DW_IC_INTR_RD_REQ 0x020 |
| 87 | #define DW_IC_INTR_TX_ABRT 0x040 |
| 88 | #define DW_IC_INTR_RX_DONE 0x080 |
| 89 | #define DW_IC_INTR_ACTIVITY 0x100 |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 90 | #define DW_IC_INTR_STOP_DET 0x200 |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 91 | #define DW_IC_INTR_START_DET 0x400 |
| 92 | #define DW_IC_INTR_GEN_CALL 0x800 |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 93 | |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 94 | #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \ |
| 95 | DW_IC_INTR_TX_EMPTY | \ |
| 96 | DW_IC_INTR_TX_ABRT | \ |
| 97 | DW_IC_INTR_STOP_DET) |
| 98 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 99 | #define DW_IC_STATUS_ACTIVITY 0x1 |
| 100 | |
| 101 | #define DW_IC_ERR_TX_ABRT 0x1 |
| 102 | |
| 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 |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 127 | #define ABRT_MASTER_DIS 11 |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 128 | #define ARB_LOST 12 |
| 129 | |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 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 | |
| 173 | /** |
| 174 | * struct dw_i2c_dev - private i2c-designware data |
| 175 | * @dev: driver model device node |
| 176 | * @base: IO registers pointer |
| 177 | * @cmd_complete: tx completion indicator |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 178 | * @lock: protect this struct and IO registers |
| 179 | * @clk: input reference clock |
| 180 | * @cmd_err: run time hadware error code |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 181 | * @msgs: points to an array of messages currently being transferred |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 182 | * @msgs_num: the number of elements in msgs |
| 183 | * @msg_write_idx: the element index of the current tx message in the msgs |
| 184 | * array |
| 185 | * @tx_buf_len: the length of the current tx buffer |
| 186 | * @tx_buf: the current tx buffer |
| 187 | * @msg_read_idx: the element index of the current rx message in the msgs |
| 188 | * array |
| 189 | * @rx_buf_len: the length of the current rx buffer |
| 190 | * @rx_buf: the current rx buffer |
| 191 | * @msg_err: error status of the current transfer |
| 192 | * @status: i2c master status, one of STATUS_* |
| 193 | * @abort_source: copy of the TX_ABRT_SOURCE register |
| 194 | * @irq: interrupt number for the i2c master |
| 195 | * @adapter: i2c subsystem adapter node |
| 196 | * @tx_fifo_depth: depth of the hardware tx fifo |
| 197 | * @rx_fifo_depth: depth of the hardware rx fifo |
| 198 | */ |
| 199 | struct dw_i2c_dev { |
| 200 | struct device *dev; |
| 201 | void __iomem *base; |
| 202 | struct completion cmd_complete; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 203 | struct mutex lock; |
| 204 | struct clk *clk; |
| 205 | int cmd_err; |
| 206 | struct i2c_msg *msgs; |
| 207 | int msgs_num; |
| 208 | int msg_write_idx; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 209 | u32 tx_buf_len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 210 | u8 *tx_buf; |
| 211 | int msg_read_idx; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 212 | u32 rx_buf_len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 213 | u8 *rx_buf; |
| 214 | int msg_err; |
| 215 | unsigned int status; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 216 | u32 abort_source; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 217 | int irq; |
| 218 | struct i2c_adapter adapter; |
| 219 | unsigned int tx_fifo_depth; |
| 220 | unsigned int rx_fifo_depth; |
| 221 | }; |
| 222 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 223 | static u32 dw_readl(struct dw_i2c_dev *dev, int offset) |
| 224 | { |
| 225 | return readl(dev->base + offset); |
| 226 | } |
| 227 | |
| 228 | static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset) |
| 229 | { |
| 230 | writel(b, dev->base + offset); |
| 231 | } |
| 232 | |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 233 | static u32 |
| 234 | i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset) |
| 235 | { |
| 236 | /* |
| 237 | * DesignWare I2C core doesn't seem to have solid strategy to meet |
| 238 | * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec |
| 239 | * will result in violation of the tHD;STA spec. |
| 240 | */ |
| 241 | if (cond) |
| 242 | /* |
| 243 | * Conditional expression: |
| 244 | * |
| 245 | * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH |
| 246 | * |
| 247 | * This is based on the DW manuals, and represents an ideal |
| 248 | * configuration. The resulting I2C bus speed will be |
| 249 | * faster than any of the others. |
| 250 | * |
| 251 | * If your hardware is free from tHD;STA issue, try this one. |
| 252 | */ |
| 253 | return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset; |
| 254 | else |
| 255 | /* |
| 256 | * Conditional expression: |
| 257 | * |
| 258 | * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf) |
| 259 | * |
| 260 | * This is just experimental rule; the tHD;STA period turned |
| 261 | * out to be proportinal to (_HCNT + 3). With this setting, |
| 262 | * we could meet both tHIGH and tHD;STA timing specs. |
| 263 | * |
| 264 | * If unsure, you'd better to take this alternative. |
| 265 | * |
| 266 | * The reason why we need to take into account "tf" here, |
| 267 | * is the same as described in i2c_dw_scl_lcnt(). |
| 268 | */ |
| 269 | return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset; |
| 270 | } |
| 271 | |
| 272 | static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset) |
| 273 | { |
| 274 | /* |
| 275 | * Conditional expression: |
| 276 | * |
| 277 | * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf) |
| 278 | * |
| 279 | * DW I2C core starts counting the SCL CNTs for the LOW period |
| 280 | * of the SCL clock (tLOW) as soon as it pulls the SCL line. |
| 281 | * In order to meet the tLOW timing spec, we need to take into |
| 282 | * account the fall time of SCL signal (tf). Default tf value |
| 283 | * should be 0.3 us, for safety. |
| 284 | */ |
| 285 | return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset; |
| 286 | } |
| 287 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 288 | /** |
| 289 | * i2c_dw_init() - initialize the designware i2c master hardware |
| 290 | * @dev: device private data |
| 291 | * |
| 292 | * This functions configures and enables the I2C master. |
| 293 | * This function is called during I2C init function, and in case of timeout at |
| 294 | * run time. |
| 295 | */ |
| 296 | static void i2c_dw_init(struct dw_i2c_dev *dev) |
| 297 | { |
| 298 | u32 input_clock_khz = clk_get_rate(dev->clk) / 1000; |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 299 | u32 ic_con, hcnt, lcnt; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 300 | |
| 301 | /* Disable the adapter */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 302 | dw_writel(dev, 0, DW_IC_ENABLE); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 303 | |
| 304 | /* set standard and fast speed deviders for high/low periods */ |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 305 | |
| 306 | /* Standard-mode */ |
| 307 | hcnt = i2c_dw_scl_hcnt(input_clock_khz, |
| 308 | 40, /* tHD;STA = tHIGH = 4.0 us */ |
| 309 | 3, /* tf = 0.3 us */ |
| 310 | 0, /* 0: DW default, 1: Ideal */ |
| 311 | 0); /* No offset */ |
| 312 | lcnt = i2c_dw_scl_lcnt(input_clock_khz, |
| 313 | 47, /* tLOW = 4.7 us */ |
| 314 | 3, /* tf = 0.3 us */ |
| 315 | 0); /* No offset */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 316 | dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT); |
| 317 | dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 318 | dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt); |
| 319 | |
| 320 | /* Fast-mode */ |
| 321 | hcnt = i2c_dw_scl_hcnt(input_clock_khz, |
| 322 | 6, /* tHD;STA = tHIGH = 0.6 us */ |
| 323 | 3, /* tf = 0.3 us */ |
| 324 | 0, /* 0: DW default, 1: Ideal */ |
| 325 | 0); /* No offset */ |
| 326 | lcnt = i2c_dw_scl_lcnt(input_clock_khz, |
| 327 | 13, /* tLOW = 1.3 us */ |
| 328 | 3, /* tf = 0.3 us */ |
| 329 | 0); /* No offset */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 330 | dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT); |
| 331 | dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT); |
Shinya Kuribayashi | d60c7e8 | 2009-11-06 21:47:01 +0900 | [diff] [blame] | 332 | 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] | 333 | |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 334 | /* Configure Tx/Rx FIFO threshold levels */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 335 | dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL); |
| 336 | dw_writel(dev, 0, DW_IC_RX_TL); |
Shinya Kuribayashi | 4cb6d1d | 2009-11-06 21:48:12 +0900 | [diff] [blame] | 337 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 338 | /* configure the i2c master */ |
| 339 | ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | |
| 340 | DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST; |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 341 | dw_writel(dev, ic_con, DW_IC_CON); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Waiting for bus not busy |
| 346 | */ |
| 347 | static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev) |
| 348 | { |
| 349 | int timeout = TIMEOUT; |
| 350 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 351 | while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 352 | if (timeout <= 0) { |
| 353 | dev_warn(dev->dev, "timeout waiting for bus ready\n"); |
| 354 | return -ETIMEDOUT; |
| 355 | } |
| 356 | timeout--; |
| 357 | mdelay(1); |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 363 | static void i2c_dw_xfer_init(struct dw_i2c_dev *dev) |
| 364 | { |
| 365 | struct i2c_msg *msgs = dev->msgs; |
| 366 | u32 ic_con; |
| 367 | |
| 368 | /* Disable the adapter */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 369 | dw_writel(dev, 0, DW_IC_ENABLE); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 370 | |
| 371 | /* set the slave (target) address */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 372 | dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 373 | |
| 374 | /* if the slave address is ten bit address, enable 10BITADDR */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 375 | ic_con = dw_readl(dev, DW_IC_CON); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 376 | if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) |
| 377 | ic_con |= DW_IC_CON_10BITADDR_MASTER; |
| 378 | else |
| 379 | ic_con &= ~DW_IC_CON_10BITADDR_MASTER; |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 380 | dw_writel(dev, ic_con, DW_IC_CON); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 381 | |
| 382 | /* Enable the adapter */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 383 | dw_writel(dev, 1, DW_IC_ENABLE); |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 384 | |
| 385 | /* Enable interrupts */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 386 | dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 387 | } |
| 388 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 389 | /* |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 390 | * Initiate (and continue) low level master read/write transaction. |
| 391 | * This function is only called from i2c_dw_isr, and pumping i2c_msg |
| 392 | * messages into the tx buffer. Even if the size of i2c_msg data is |
| 393 | * longer than the size of the tx buffer, it handles everything. |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 394 | */ |
| 395 | static void |
Shinya Kuribayashi | e77cf23 | 2009-11-06 21:46:04 +0900 | [diff] [blame] | 396 | i2c_dw_xfer_msg(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 397 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 398 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 399 | u32 intr_mask; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 400 | int tx_limit, rx_limit; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 401 | u32 addr = msgs[dev->msg_write_idx].addr; |
| 402 | u32 buf_len = dev->tx_buf_len; |
Justin P. Mattock | 6993248 | 2011-07-26 23:06:29 -0700 | [diff] [blame] | 403 | u8 *buf = dev->tx_buf; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 404 | |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 405 | intr_mask = DW_IC_INTR_DEFAULT_MASK; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 406 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 407 | for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) { |
Shinya Kuribayashi | a0e06ea | 2009-11-06 21:52:22 +0900 | [diff] [blame] | 408 | /* |
| 409 | * if target address has changed, we need to |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 410 | * reprogram the target address in the i2c |
| 411 | * adapter when we are done with this transfer |
| 412 | */ |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 413 | if (msgs[dev->msg_write_idx].addr != addr) { |
| 414 | dev_err(dev->dev, |
| 415 | "%s: invalid target address\n", __func__); |
| 416 | dev->msg_err = -EINVAL; |
| 417 | break; |
| 418 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 419 | |
| 420 | if (msgs[dev->msg_write_idx].len == 0) { |
| 421 | dev_err(dev->dev, |
| 422 | "%s: invalid message length\n", __func__); |
| 423 | dev->msg_err = -EINVAL; |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 424 | break; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) { |
| 428 | /* new i2c_msg */ |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 429 | buf = msgs[dev->msg_write_idx].buf; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 430 | buf_len = msgs[dev->msg_write_idx].len; |
| 431 | } |
| 432 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 433 | tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR); |
| 434 | rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 435 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 436 | while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) { |
| 437 | if (msgs[dev->msg_write_idx].flags & I2C_M_RD) { |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 438 | dw_writel(dev, 0x100, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 439 | rx_limit--; |
| 440 | } else |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 441 | dw_writel(dev, *buf++, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 442 | tx_limit--; buf_len--; |
| 443 | } |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 444 | |
Shinya Kuribayashi | 26ea15b | 2009-11-06 21:49:14 +0900 | [diff] [blame] | 445 | dev->tx_buf = buf; |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 446 | dev->tx_buf_len = buf_len; |
| 447 | |
| 448 | if (buf_len > 0) { |
| 449 | /* more bytes to be written */ |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 450 | dev->status |= STATUS_WRITE_IN_PROGRESS; |
| 451 | break; |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 452 | } else |
Shinya Kuribayashi | c70c5cd | 2009-11-06 21:47:30 +0900 | [diff] [blame] | 453 | dev->status &= ~STATUS_WRITE_IN_PROGRESS; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 454 | } |
| 455 | |
Shinya Kuribayashi | 69151e5 | 2009-11-06 21:51:00 +0900 | [diff] [blame] | 456 | /* |
| 457 | * If i2c_msg index search is completed, we don't need TX_EMPTY |
| 458 | * interrupt any more. |
| 459 | */ |
| 460 | if (dev->msg_write_idx == dev->msgs_num) |
| 461 | intr_mask &= ~DW_IC_INTR_TX_EMPTY; |
| 462 | |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 463 | if (dev->msg_err) |
| 464 | intr_mask = 0; |
| 465 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 466 | dw_writel(dev, intr_mask, DW_IC_INTR_MASK); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void |
Shinya Kuribayashi | 78839bd | 2009-11-06 21:45:39 +0900 | [diff] [blame] | 470 | i2c_dw_read(struct dw_i2c_dev *dev) |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 471 | { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 472 | struct i2c_msg *msgs = dev->msgs; |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 473 | int rx_valid; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 474 | |
Shinya Kuribayashi | 6d2ea48 | 2009-11-06 21:46:29 +0900 | [diff] [blame] | 475 | for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) { |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 476 | u32 len; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 477 | u8 *buf; |
| 478 | |
| 479 | if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD)) |
| 480 | continue; |
| 481 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 482 | if (!(dev->status & STATUS_READ_IN_PROGRESS)) { |
| 483 | len = msgs[dev->msg_read_idx].len; |
| 484 | buf = msgs[dev->msg_read_idx].buf; |
| 485 | } else { |
| 486 | len = dev->rx_buf_len; |
| 487 | buf = dev->rx_buf; |
| 488 | } |
| 489 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 490 | rx_valid = dw_readl(dev, DW_IC_RXFLR); |
Shinya Kuribayashi | ae72222 | 2009-11-06 21:49:39 +0900 | [diff] [blame] | 491 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 492 | for (; len > 0 && rx_valid > 0; len--, rx_valid--) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 493 | *buf++ = dw_readl(dev, DW_IC_DATA_CMD); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 494 | |
| 495 | if (len > 0) { |
| 496 | dev->status |= STATUS_READ_IN_PROGRESS; |
| 497 | dev->rx_buf_len = len; |
| 498 | dev->rx_buf = buf; |
| 499 | return; |
| 500 | } else |
| 501 | dev->status &= ~STATUS_READ_IN_PROGRESS; |
| 502 | } |
| 503 | } |
| 504 | |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 505 | static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev) |
| 506 | { |
| 507 | unsigned long abort_source = dev->abort_source; |
| 508 | int i; |
| 509 | |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 510 | if (abort_source & DW_IC_TX_ABRT_NOACK) { |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 511 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | 6d1ea0f | 2009-11-16 20:40:14 +0900 | [diff] [blame] | 512 | dev_dbg(dev->dev, |
| 513 | "%s: %s\n", __func__, abort_sources[i]); |
| 514 | return -EREMOTEIO; |
| 515 | } |
| 516 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 517 | for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 518 | dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]); |
| 519 | |
| 520 | if (abort_source & DW_IC_TX_ARB_LOST) |
| 521 | return -EAGAIN; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 522 | else if (abort_source & DW_IC_TX_ABRT_GCALL_READ) |
| 523 | return -EINVAL; /* wrong msgs[] data */ |
| 524 | else |
| 525 | return -EIO; |
| 526 | } |
| 527 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 528 | /* |
| 529 | * Prepare controller for a transaction and call i2c_dw_xfer_msg |
| 530 | */ |
| 531 | static int |
| 532 | i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) |
| 533 | { |
| 534 | struct dw_i2c_dev *dev = i2c_get_adapdata(adap); |
| 535 | int ret; |
| 536 | |
| 537 | dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num); |
| 538 | |
| 539 | mutex_lock(&dev->lock); |
| 540 | |
| 541 | INIT_COMPLETION(dev->cmd_complete); |
| 542 | dev->msgs = msgs; |
| 543 | dev->msgs_num = num; |
| 544 | dev->cmd_err = 0; |
| 545 | dev->msg_write_idx = 0; |
| 546 | dev->msg_read_idx = 0; |
| 547 | dev->msg_err = 0; |
| 548 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 549 | dev->abort_source = 0; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 550 | |
| 551 | ret = i2c_dw_wait_bus_not_busy(dev); |
| 552 | if (ret < 0) |
| 553 | goto done; |
| 554 | |
| 555 | /* start the transfers */ |
Shinya Kuribayashi | 81e798b | 2009-11-06 21:48:55 +0900 | [diff] [blame] | 556 | i2c_dw_xfer_init(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 557 | |
| 558 | /* wait for tx to complete */ |
| 559 | ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ); |
| 560 | if (ret == 0) { |
| 561 | dev_err(dev->dev, "controller timed out\n"); |
| 562 | i2c_dw_init(dev); |
| 563 | ret = -ETIMEDOUT; |
| 564 | goto done; |
| 565 | } else if (ret < 0) |
| 566 | goto done; |
| 567 | |
| 568 | if (dev->msg_err) { |
| 569 | ret = dev->msg_err; |
| 570 | goto done; |
| 571 | } |
| 572 | |
| 573 | /* no error */ |
| 574 | if (likely(!dev->cmd_err)) { |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 575 | /* Disable the adapter */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 576 | dw_writel(dev, 0, DW_IC_ENABLE); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 577 | ret = num; |
| 578 | goto done; |
| 579 | } |
| 580 | |
| 581 | /* We have an error */ |
| 582 | if (dev->cmd_err == DW_IC_ERR_TX_ABRT) { |
Shinya Kuribayashi | ce6eb57 | 2009-11-06 21:51:57 +0900 | [diff] [blame] | 583 | ret = i2c_dw_handle_tx_abort(dev); |
| 584 | goto done; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 585 | } |
| 586 | ret = -EIO; |
| 587 | |
| 588 | done: |
| 589 | mutex_unlock(&dev->lock); |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | static u32 i2c_dw_func(struct i2c_adapter *adap) |
| 595 | { |
Shinya Kuribayashi | 52d7e43 | 2009-11-06 21:50:02 +0900 | [diff] [blame] | 596 | return I2C_FUNC_I2C | |
| 597 | I2C_FUNC_10BIT_ADDR | |
| 598 | I2C_FUNC_SMBUS_BYTE | |
| 599 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 600 | I2C_FUNC_SMBUS_WORD_DATA | |
| 601 | I2C_FUNC_SMBUS_I2C_BLOCK; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 602 | } |
| 603 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 604 | static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev) |
| 605 | { |
| 606 | u32 stat; |
| 607 | |
| 608 | /* |
| 609 | * The IC_INTR_STAT register just indicates "enabled" interrupts. |
| 610 | * Ths unmasked raw version of interrupt status bits are available |
| 611 | * in the IC_RAW_INTR_STAT register. |
| 612 | * |
| 613 | * That is, |
| 614 | * stat = readl(IC_INTR_STAT); |
| 615 | * equals to, |
| 616 | * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK); |
| 617 | * |
| 618 | * The raw version might be useful for debugging purposes. |
| 619 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 620 | stat = dw_readl(dev, DW_IC_INTR_STAT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Do not use the IC_CLR_INTR register to clear interrupts, or |
| 624 | * you'll miss some interrupts, triggered during the period from |
| 625 | * readl(IC_INTR_STAT) to readl(IC_CLR_INTR). |
| 626 | * |
| 627 | * Instead, use the separately-prepared IC_CLR_* registers. |
| 628 | */ |
| 629 | if (stat & DW_IC_INTR_RX_UNDER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 630 | dw_readl(dev, DW_IC_CLR_RX_UNDER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 631 | if (stat & DW_IC_INTR_RX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 632 | dw_readl(dev, DW_IC_CLR_RX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 633 | if (stat & DW_IC_INTR_TX_OVER) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 634 | dw_readl(dev, DW_IC_CLR_TX_OVER); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 635 | if (stat & DW_IC_INTR_RD_REQ) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 636 | dw_readl(dev, DW_IC_CLR_RD_REQ); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 637 | if (stat & DW_IC_INTR_TX_ABRT) { |
| 638 | /* |
| 639 | * The IC_TX_ABRT_SOURCE register is cleared whenever |
| 640 | * the IC_CLR_TX_ABRT is read. Preserve it beforehand. |
| 641 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 642 | dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE); |
| 643 | dw_readl(dev, DW_IC_CLR_TX_ABRT); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 644 | } |
| 645 | if (stat & DW_IC_INTR_RX_DONE) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 646 | dw_readl(dev, DW_IC_CLR_RX_DONE); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 647 | if (stat & DW_IC_INTR_ACTIVITY) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 648 | dw_readl(dev, DW_IC_CLR_ACTIVITY); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 649 | if (stat & DW_IC_INTR_STOP_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 650 | dw_readl(dev, DW_IC_CLR_STOP_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 651 | if (stat & DW_IC_INTR_START_DET) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 652 | dw_readl(dev, DW_IC_CLR_START_DET); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 653 | if (stat & DW_IC_INTR_GEN_CALL) |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 654 | dw_readl(dev, DW_IC_CLR_GEN_CALL); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 655 | |
| 656 | return stat; |
| 657 | } |
| 658 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 659 | /* |
| 660 | * Interrupt service routine. This gets called whenever an I2C interrupt |
| 661 | * occurs. |
| 662 | */ |
| 663 | static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id) |
| 664 | { |
| 665 | struct dw_i2c_dev *dev = dev_id; |
Shinya Kuribayashi | ed5e1dd | 2009-11-06 21:43:52 +0900 | [diff] [blame] | 666 | u32 stat; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 667 | |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 668 | stat = i2c_dw_read_clear_intrbits(dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 669 | dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat); |
Shinya Kuribayashi | e28000a | 2009-11-06 21:44:37 +0900 | [diff] [blame] | 670 | |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 671 | if (stat & DW_IC_INTR_TX_ABRT) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 672 | dev->cmd_err |= DW_IC_ERR_TX_ABRT; |
| 673 | dev->status = STATUS_IDLE; |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 674 | |
| 675 | /* |
| 676 | * Anytime TX_ABRT is set, the contents of the tx/rx |
| 677 | * buffers are flushed. Make sure to skip them. |
| 678 | */ |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 679 | dw_writel(dev, 0, DW_IC_INTR_MASK); |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 680 | goto tx_aborted; |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 681 | } |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 682 | |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 683 | if (stat & DW_IC_INTR_RX_FULL) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 684 | i2c_dw_read(dev); |
Shinya Kuribayashi | 21a89d4 | 2009-11-06 21:48:33 +0900 | [diff] [blame] | 685 | |
| 686 | if (stat & DW_IC_INTR_TX_EMPTY) |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 687 | i2c_dw_xfer_msg(dev); |
Shinya Kuribayashi | 0774539 | 2009-11-06 21:47:51 +0900 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * No need to modify or disable the interrupt mask here. |
| 691 | * i2c_dw_xfer_msg() will take care of it according to |
| 692 | * the current transmit status. |
| 693 | */ |
| 694 | |
Shinya Kuribayashi | 597fe31 | 2009-11-06 21:51:36 +0900 | [diff] [blame] | 695 | tx_aborted: |
Shinya Kuribayashi | 8f588e4 | 2009-11-06 21:51:18 +0900 | [diff] [blame] | 696 | 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] | 697 | complete(&dev->cmd_complete); |
| 698 | |
| 699 | return IRQ_HANDLED; |
| 700 | } |
| 701 | |
| 702 | static struct i2c_algorithm i2c_dw_algo = { |
| 703 | .master_xfer = i2c_dw_xfer, |
| 704 | .functionality = i2c_dw_func, |
| 705 | }; |
| 706 | |
| 707 | static int __devinit dw_i2c_probe(struct platform_device *pdev) |
| 708 | { |
| 709 | struct dw_i2c_dev *dev; |
| 710 | struct i2c_adapter *adap; |
Shinya Kuribayashi | 91b52ca | 2009-11-06 21:45:07 +0900 | [diff] [blame] | 711 | struct resource *mem, *ioarea; |
| 712 | int irq, r; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 713 | |
| 714 | /* NOTE: driver uses the static register mapping */ |
| 715 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 716 | if (!mem) { |
| 717 | dev_err(&pdev->dev, "no mem resource?\n"); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | |
Shinya Kuribayashi | 91b52ca | 2009-11-06 21:45:07 +0900 | [diff] [blame] | 721 | irq = platform_get_irq(pdev, 0); |
| 722 | if (irq < 0) { |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 723 | dev_err(&pdev->dev, "no irq resource?\n"); |
Shinya Kuribayashi | 91b52ca | 2009-11-06 21:45:07 +0900 | [diff] [blame] | 724 | return irq; /* -ENXIO */ |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | ioarea = request_mem_region(mem->start, resource_size(mem), |
| 728 | pdev->name); |
| 729 | if (!ioarea) { |
| 730 | dev_err(&pdev->dev, "I2C region already claimed\n"); |
| 731 | return -EBUSY; |
| 732 | } |
| 733 | |
| 734 | dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL); |
| 735 | if (!dev) { |
| 736 | r = -ENOMEM; |
| 737 | goto err_release_region; |
| 738 | } |
| 739 | |
| 740 | init_completion(&dev->cmd_complete); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 741 | mutex_init(&dev->lock); |
| 742 | dev->dev = get_device(&pdev->dev); |
Shinya Kuribayashi | 91b52ca | 2009-11-06 21:45:07 +0900 | [diff] [blame] | 743 | dev->irq = irq; |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 744 | platform_set_drvdata(pdev, dev); |
| 745 | |
| 746 | dev->clk = clk_get(&pdev->dev, NULL); |
| 747 | if (IS_ERR(dev->clk)) { |
| 748 | r = -ENODEV; |
| 749 | goto err_free_mem; |
| 750 | } |
| 751 | clk_enable(dev->clk); |
| 752 | |
| 753 | dev->base = ioremap(mem->start, resource_size(mem)); |
| 754 | if (dev->base == NULL) { |
| 755 | dev_err(&pdev->dev, "failure mapping io resources\n"); |
| 756 | r = -EBUSY; |
| 757 | goto err_unuse_clocks; |
| 758 | } |
| 759 | { |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 760 | u32 param1 = dw_readl(dev, DW_IC_COMP_PARAM_1); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 761 | |
| 762 | dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1; |
| 763 | dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1; |
| 764 | } |
| 765 | i2c_dw_init(dev); |
| 766 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 767 | dw_writel(dev, 0, DW_IC_INTR_MASK); /* disable IRQ */ |
Shinya Kuribayashi | 201d6a7 | 2009-11-06 21:50:40 +0900 | [diff] [blame] | 768 | r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 769 | if (r) { |
| 770 | dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq); |
| 771 | goto err_iounmap; |
| 772 | } |
| 773 | |
| 774 | adap = &dev->adapter; |
| 775 | i2c_set_adapdata(adap, dev); |
| 776 | adap->owner = THIS_MODULE; |
| 777 | adap->class = I2C_CLASS_HWMON; |
| 778 | strlcpy(adap->name, "Synopsys DesignWare I2C adapter", |
| 779 | sizeof(adap->name)); |
| 780 | adap->algo = &i2c_dw_algo; |
| 781 | adap->dev.parent = &pdev->dev; |
| 782 | |
| 783 | adap->nr = pdev->id; |
| 784 | r = i2c_add_numbered_adapter(adap); |
| 785 | if (r) { |
| 786 | dev_err(&pdev->dev, "failure adding adapter\n"); |
| 787 | goto err_free_irq; |
| 788 | } |
| 789 | |
| 790 | return 0; |
| 791 | |
| 792 | err_free_irq: |
| 793 | free_irq(dev->irq, dev); |
| 794 | err_iounmap: |
| 795 | iounmap(dev->base); |
| 796 | err_unuse_clocks: |
| 797 | clk_disable(dev->clk); |
| 798 | clk_put(dev->clk); |
| 799 | dev->clk = NULL; |
| 800 | err_free_mem: |
| 801 | platform_set_drvdata(pdev, NULL); |
| 802 | put_device(&pdev->dev); |
| 803 | kfree(dev); |
| 804 | err_release_region: |
| 805 | release_mem_region(mem->start, resource_size(mem)); |
| 806 | |
| 807 | return r; |
| 808 | } |
| 809 | |
| 810 | static int __devexit dw_i2c_remove(struct platform_device *pdev) |
| 811 | { |
| 812 | struct dw_i2c_dev *dev = platform_get_drvdata(pdev); |
| 813 | struct resource *mem; |
| 814 | |
| 815 | platform_set_drvdata(pdev, NULL); |
| 816 | i2c_del_adapter(&dev->adapter); |
| 817 | put_device(&pdev->dev); |
| 818 | |
| 819 | clk_disable(dev->clk); |
| 820 | clk_put(dev->clk); |
| 821 | dev->clk = NULL; |
| 822 | |
Jean-Hugues Deschenes | 7f27960 | 2011-10-06 11:26:25 -0700 | [diff] [blame^] | 823 | dw_writel(dev, 0, DW_IC_ENABLE); |
Baruch Siach | 1ab52cf | 2009-06-22 16:36:29 +0300 | [diff] [blame] | 824 | free_irq(dev->irq, dev); |
| 825 | kfree(dev); |
| 826 | |
| 827 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 828 | release_mem_region(mem->start, resource_size(mem)); |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | /* work with hotplug and coldplug */ |
| 833 | MODULE_ALIAS("platform:i2c_designware"); |
| 834 | |
| 835 | static struct platform_driver dw_i2c_driver = { |
| 836 | .remove = __devexit_p(dw_i2c_remove), |
| 837 | .driver = { |
| 838 | .name = "i2c_designware", |
| 839 | .owner = THIS_MODULE, |
| 840 | }, |
| 841 | }; |
| 842 | |
| 843 | static int __init dw_i2c_init_driver(void) |
| 844 | { |
| 845 | return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe); |
| 846 | } |
| 847 | module_init(dw_i2c_init_driver); |
| 848 | |
| 849 | static void __exit dw_i2c_exit_driver(void) |
| 850 | { |
| 851 | platform_driver_unregister(&dw_i2c_driver); |
| 852 | } |
| 853 | module_exit(dw_i2c_exit_driver); |
| 854 | |
| 855 | MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>"); |
| 856 | MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter"); |
| 857 | MODULE_LICENSE("GPL"); |