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