blob: bb7b25766ea9c9263e8626f94561015887d7806b [file] [log] [blame]
Baruch Siach1ab52cf2009-06-22 16:36:29 +03001/*
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 Kuribayashie28000a2009-11-06 21:44:37 +090052#define DW_IC_RAW_INTR_STAT 0x34
Baruch Siach1ab52cf2009-06-22 16:36:29 +030053#define DW_IC_CLR_INTR 0x40
Shinya Kuribayashie28000a2009-11-06 21:44:37 +090054#define DW_IC_CLR_RX_UNDER 0x44
55#define DW_IC_CLR_RX_OVER 0x48
56#define DW_IC_CLR_TX_OVER 0x4c
57#define DW_IC_CLR_RD_REQ 0x50
58#define DW_IC_CLR_TX_ABRT 0x54
59#define DW_IC_CLR_RX_DONE 0x58
60#define DW_IC_CLR_ACTIVITY 0x5c
61#define DW_IC_CLR_STOP_DET 0x60
62#define DW_IC_CLR_START_DET 0x64
63#define DW_IC_CLR_GEN_CALL 0x68
Baruch Siach1ab52cf2009-06-22 16:36:29 +030064#define DW_IC_ENABLE 0x6c
65#define DW_IC_STATUS 0x70
66#define DW_IC_TXFLR 0x74
67#define DW_IC_RXFLR 0x78
68#define DW_IC_COMP_PARAM_1 0xf4
69#define DW_IC_TX_ABRT_SOURCE 0x80
70
71#define DW_IC_CON_MASTER 0x1
72#define DW_IC_CON_SPEED_STD 0x2
73#define DW_IC_CON_SPEED_FAST 0x4
74#define DW_IC_CON_10BITADDR_MASTER 0x10
75#define DW_IC_CON_RESTART_EN 0x20
76#define DW_IC_CON_SLAVE_DISABLE 0x40
77
Shinya Kuribayashie28000a2009-11-06 21:44:37 +090078#define DW_IC_INTR_RX_UNDER 0x001
79#define DW_IC_INTR_RX_OVER 0x002
80#define DW_IC_INTR_RX_FULL 0x004
81#define DW_IC_INTR_TX_OVER 0x008
82#define DW_IC_INTR_TX_EMPTY 0x010
83#define DW_IC_INTR_RD_REQ 0x020
84#define DW_IC_INTR_TX_ABRT 0x040
85#define DW_IC_INTR_RX_DONE 0x080
86#define DW_IC_INTR_ACTIVITY 0x100
Baruch Siach1ab52cf2009-06-22 16:36:29 +030087#define DW_IC_INTR_STOP_DET 0x200
Shinya Kuribayashie28000a2009-11-06 21:44:37 +090088#define DW_IC_INTR_START_DET 0x400
89#define DW_IC_INTR_GEN_CALL 0x800
Baruch Siach1ab52cf2009-06-22 16:36:29 +030090
91#define DW_IC_STATUS_ACTIVITY 0x1
92
93#define DW_IC_ERR_TX_ABRT 0x1
94
95/*
96 * status codes
97 */
98#define STATUS_IDLE 0x0
99#define STATUS_WRITE_IN_PROGRESS 0x1
100#define STATUS_READ_IN_PROGRESS 0x2
101
102#define TIMEOUT 20 /* ms */
103
104/*
105 * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
106 *
107 * only expected abort codes are listed here
108 * refer to the datasheet for the full list
109 */
110#define ABRT_7B_ADDR_NOACK 0
111#define ABRT_10ADDR1_NOACK 1
112#define ABRT_10ADDR2_NOACK 2
113#define ABRT_TXDATA_NOACK 3
114#define ABRT_GCALL_NOACK 4
115#define ABRT_GCALL_READ 5
116#define ABRT_SBYTE_ACKDET 7
117#define ABRT_SBYTE_NORSTRT 9
118#define ABRT_10B_RD_NORSTRT 10
119#define ARB_MASTER_DIS 11
120#define ARB_LOST 12
121
122static char *abort_sources[] = {
123 [ABRT_7B_ADDR_NOACK] =
124 "slave address not acknowledged (7bit mode)",
125 [ABRT_10ADDR1_NOACK] =
126 "first address byte not acknowledged (10bit mode)",
127 [ABRT_10ADDR2_NOACK] =
128 "second address byte not acknowledged (10bit mode)",
129 [ABRT_TXDATA_NOACK] =
130 "data not acknowledged",
131 [ABRT_GCALL_NOACK] =
132 "no acknowledgement for a general call",
133 [ABRT_GCALL_READ] =
134 "read after general call",
135 [ABRT_SBYTE_ACKDET] =
136 "start byte acknowledged",
137 [ABRT_SBYTE_NORSTRT] =
138 "trying to send start byte when restart is disabled",
139 [ABRT_10B_RD_NORSTRT] =
140 "trying to read when restart is disabled (10bit mode)",
141 [ARB_MASTER_DIS] =
142 "trying to use disabled adapter",
143 [ARB_LOST] =
144 "lost arbitration",
145};
146
147/**
148 * struct dw_i2c_dev - private i2c-designware data
149 * @dev: driver model device node
150 * @base: IO registers pointer
151 * @cmd_complete: tx completion indicator
152 * @pump_msg: continue in progress transfers
153 * @lock: protect this struct and IO registers
154 * @clk: input reference clock
155 * @cmd_err: run time hadware error code
156 * @msgs: points to an array of messages currently being transfered
157 * @msgs_num: the number of elements in msgs
158 * @msg_write_idx: the element index of the current tx message in the msgs
159 * array
160 * @tx_buf_len: the length of the current tx buffer
161 * @tx_buf: the current tx buffer
162 * @msg_read_idx: the element index of the current rx message in the msgs
163 * array
164 * @rx_buf_len: the length of the current rx buffer
165 * @rx_buf: the current rx buffer
166 * @msg_err: error status of the current transfer
167 * @status: i2c master status, one of STATUS_*
168 * @abort_source: copy of the TX_ABRT_SOURCE register
169 * @irq: interrupt number for the i2c master
170 * @adapter: i2c subsystem adapter node
171 * @tx_fifo_depth: depth of the hardware tx fifo
172 * @rx_fifo_depth: depth of the hardware rx fifo
173 */
174struct dw_i2c_dev {
175 struct device *dev;
176 void __iomem *base;
177 struct completion cmd_complete;
178 struct tasklet_struct pump_msg;
179 struct mutex lock;
180 struct clk *clk;
181 int cmd_err;
182 struct i2c_msg *msgs;
183 int msgs_num;
184 int msg_write_idx;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900185 u32 tx_buf_len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300186 u8 *tx_buf;
187 int msg_read_idx;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900188 u32 rx_buf_len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300189 u8 *rx_buf;
190 int msg_err;
191 unsigned int status;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900192 u32 abort_source;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300193 int irq;
194 struct i2c_adapter adapter;
195 unsigned int tx_fifo_depth;
196 unsigned int rx_fifo_depth;
197};
198
199/**
200 * i2c_dw_init() - initialize the designware i2c master hardware
201 * @dev: device private data
202 *
203 * This functions configures and enables the I2C master.
204 * This function is called during I2C init function, and in case of timeout at
205 * run time.
206 */
207static void i2c_dw_init(struct dw_i2c_dev *dev)
208{
209 u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900210 u32 ic_con;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300211
212 /* Disable the adapter */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900213 writel(0, dev->base + DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300214
215 /* set standard and fast speed deviders for high/low periods */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900216 writel((input_clock_khz * 40 / 10000)+1, /* std speed high, 4us */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300217 dev->base + DW_IC_SS_SCL_HCNT);
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900218 writel((input_clock_khz * 47 / 10000)+1, /* std speed low, 4.7us */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300219 dev->base + DW_IC_SS_SCL_LCNT);
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900220 writel((input_clock_khz * 6 / 10000)+1, /* fast speed high, 0.6us */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300221 dev->base + DW_IC_FS_SCL_HCNT);
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900222 writel((input_clock_khz * 13 / 10000)+1, /* fast speed low, 1.3us */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300223 dev->base + DW_IC_FS_SCL_LCNT);
224
225 /* configure the i2c master */
226 ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
227 DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900228 writel(ic_con, dev->base + DW_IC_CON);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300229}
230
231/*
232 * Waiting for bus not busy
233 */
234static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
235{
236 int timeout = TIMEOUT;
237
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900238 while (readl(dev->base + DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300239 if (timeout <= 0) {
240 dev_warn(dev->dev, "timeout waiting for bus ready\n");
241 return -ETIMEDOUT;
242 }
243 timeout--;
244 mdelay(1);
245 }
246
247 return 0;
248}
249
250/*
251 * Initiate low level master read/write transaction.
252 * This function is called from i2c_dw_xfer when starting a transfer.
253 * This function is also called from dw_i2c_pump_msg to continue a transfer
254 * that is longer than the size of the TX FIFO.
255 */
256static void
257i2c_dw_xfer_msg(struct i2c_adapter *adap)
258{
259 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
260 struct i2c_msg *msgs = dev->msgs;
261 int num = dev->msgs_num;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900262 u32 ic_con, intr_mask;
263 int tx_limit = dev->tx_fifo_depth - readl(dev->base + DW_IC_TXFLR);
264 int rx_limit = dev->rx_fifo_depth - readl(dev->base + DW_IC_RXFLR);
265 u32 addr = msgs[dev->msg_write_idx].addr;
266 u32 buf_len = dev->tx_buf_len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300267
268 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
269 /* Disable the adapter */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900270 writel(0, dev->base + DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300271
272 /* set the slave (target) address */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900273 writel(msgs[dev->msg_write_idx].addr, dev->base + DW_IC_TAR);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300274
275 /* if the slave address is ten bit address, enable 10BITADDR */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900276 ic_con = readl(dev->base + DW_IC_CON);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300277 if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
278 ic_con |= DW_IC_CON_10BITADDR_MASTER;
279 else
280 ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900281 writel(ic_con, dev->base + DW_IC_CON);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300282
283 /* Enable the adapter */
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900284 writel(1, dev->base + DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300285 }
286
287 for (; dev->msg_write_idx < num; dev->msg_write_idx++) {
288 /* if target address has changed, we need to
289 * reprogram the target address in the i2c
290 * adapter when we are done with this transfer
291 */
292 if (msgs[dev->msg_write_idx].addr != addr)
293 return;
294
295 if (msgs[dev->msg_write_idx].len == 0) {
296 dev_err(dev->dev,
297 "%s: invalid message length\n", __func__);
298 dev->msg_err = -EINVAL;
299 return;
300 }
301
302 if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
303 /* new i2c_msg */
304 dev->tx_buf = msgs[dev->msg_write_idx].buf;
305 buf_len = msgs[dev->msg_write_idx].len;
306 }
307
308 while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
309 if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900310 writel(0x100, dev->base + DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300311 rx_limit--;
312 } else
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900313 writel(*(dev->tx_buf++),
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300314 dev->base + DW_IC_DATA_CMD);
315 tx_limit--; buf_len--;
316 }
317 }
318
319 intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
320 if (buf_len > 0) { /* more bytes to be written */
321 intr_mask |= DW_IC_INTR_TX_EMPTY;
322 dev->status |= STATUS_WRITE_IN_PROGRESS;
323 } else
324 dev->status &= ~STATUS_WRITE_IN_PROGRESS;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900325 writel(intr_mask, dev->base + DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300326
327 dev->tx_buf_len = buf_len;
328}
329
330static void
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900331i2c_dw_read(struct dw_i2c_dev *dev)
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300332{
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300333 struct i2c_msg *msgs = dev->msgs;
334 int num = dev->msgs_num;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900335 u32 addr = msgs[dev->msg_read_idx].addr;
336 int rx_valid = readl(dev->base + DW_IC_RXFLR);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300337
338 for (; dev->msg_read_idx < num; dev->msg_read_idx++) {
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900339 u32 len;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300340 u8 *buf;
341
342 if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
343 continue;
344
345 /* different i2c client, reprogram the i2c adapter */
346 if (msgs[dev->msg_read_idx].addr != addr)
347 return;
348
349 if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
350 len = msgs[dev->msg_read_idx].len;
351 buf = msgs[dev->msg_read_idx].buf;
352 } else {
353 len = dev->rx_buf_len;
354 buf = dev->rx_buf;
355 }
356
357 for (; len > 0 && rx_valid > 0; len--, rx_valid--)
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900358 *buf++ = readl(dev->base + DW_IC_DATA_CMD);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300359
360 if (len > 0) {
361 dev->status |= STATUS_READ_IN_PROGRESS;
362 dev->rx_buf_len = len;
363 dev->rx_buf = buf;
364 return;
365 } else
366 dev->status &= ~STATUS_READ_IN_PROGRESS;
367 }
368}
369
370/*
371 * Prepare controller for a transaction and call i2c_dw_xfer_msg
372 */
373static int
374i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
375{
376 struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
377 int ret;
378
379 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
380
381 mutex_lock(&dev->lock);
382
383 INIT_COMPLETION(dev->cmd_complete);
384 dev->msgs = msgs;
385 dev->msgs_num = num;
386 dev->cmd_err = 0;
387 dev->msg_write_idx = 0;
388 dev->msg_read_idx = 0;
389 dev->msg_err = 0;
390 dev->status = STATUS_IDLE;
391
392 ret = i2c_dw_wait_bus_not_busy(dev);
393 if (ret < 0)
394 goto done;
395
396 /* start the transfers */
397 i2c_dw_xfer_msg(adap);
398
399 /* wait for tx to complete */
400 ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
401 if (ret == 0) {
402 dev_err(dev->dev, "controller timed out\n");
403 i2c_dw_init(dev);
404 ret = -ETIMEDOUT;
405 goto done;
406 } else if (ret < 0)
407 goto done;
408
409 if (dev->msg_err) {
410 ret = dev->msg_err;
411 goto done;
412 }
413
414 /* no error */
415 if (likely(!dev->cmd_err)) {
416 /* read rx fifo, and disable the adapter */
417 do {
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900418 i2c_dw_read(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300419 } while (dev->status & STATUS_READ_IN_PROGRESS);
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900420 writel(0, dev->base + DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300421 ret = num;
422 goto done;
423 }
424
425 /* We have an error */
426 if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
427 unsigned long abort_source = dev->abort_source;
428 int i;
429
430 for_each_bit(i, &abort_source, ARRAY_SIZE(abort_sources)) {
431 dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
432 }
433 }
434 ret = -EIO;
435
436done:
437 mutex_unlock(&dev->lock);
438
439 return ret;
440}
441
442static u32 i2c_dw_func(struct i2c_adapter *adap)
443{
444 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR;
445}
446
447static void dw_i2c_pump_msg(unsigned long data)
448{
449 struct dw_i2c_dev *dev = (struct dw_i2c_dev *) data;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900450 u32 intr_mask;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300451
Shinya Kuribayashi78839bd2009-11-06 21:45:39 +0900452 i2c_dw_read(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300453 i2c_dw_xfer_msg(&dev->adapter);
454
455 intr_mask = DW_IC_INTR_STOP_DET | DW_IC_INTR_TX_ABRT;
456 if (dev->status & STATUS_WRITE_IN_PROGRESS)
457 intr_mask |= DW_IC_INTR_TX_EMPTY;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900458 writel(intr_mask, dev->base + DW_IC_INTR_MASK);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300459}
460
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900461static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
462{
463 u32 stat;
464
465 /*
466 * The IC_INTR_STAT register just indicates "enabled" interrupts.
467 * Ths unmasked raw version of interrupt status bits are available
468 * in the IC_RAW_INTR_STAT register.
469 *
470 * That is,
471 * stat = readl(IC_INTR_STAT);
472 * equals to,
473 * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
474 *
475 * The raw version might be useful for debugging purposes.
476 */
477 stat = readl(dev->base + DW_IC_INTR_STAT);
478
479 /*
480 * Do not use the IC_CLR_INTR register to clear interrupts, or
481 * you'll miss some interrupts, triggered during the period from
482 * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
483 *
484 * Instead, use the separately-prepared IC_CLR_* registers.
485 */
486 if (stat & DW_IC_INTR_RX_UNDER)
487 readl(dev->base + DW_IC_CLR_RX_UNDER);
488 if (stat & DW_IC_INTR_RX_OVER)
489 readl(dev->base + DW_IC_CLR_RX_OVER);
490 if (stat & DW_IC_INTR_TX_OVER)
491 readl(dev->base + DW_IC_CLR_TX_OVER);
492 if (stat & DW_IC_INTR_RD_REQ)
493 readl(dev->base + DW_IC_CLR_RD_REQ);
494 if (stat & DW_IC_INTR_TX_ABRT) {
495 /*
496 * The IC_TX_ABRT_SOURCE register is cleared whenever
497 * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
498 */
499 dev->abort_source = readl(dev->base + DW_IC_TX_ABRT_SOURCE);
500 readl(dev->base + DW_IC_CLR_TX_ABRT);
501 }
502 if (stat & DW_IC_INTR_RX_DONE)
503 readl(dev->base + DW_IC_CLR_RX_DONE);
504 if (stat & DW_IC_INTR_ACTIVITY)
505 readl(dev->base + DW_IC_CLR_ACTIVITY);
506 if (stat & DW_IC_INTR_STOP_DET)
507 readl(dev->base + DW_IC_CLR_STOP_DET);
508 if (stat & DW_IC_INTR_START_DET)
509 readl(dev->base + DW_IC_CLR_START_DET);
510 if (stat & DW_IC_INTR_GEN_CALL)
511 readl(dev->base + DW_IC_CLR_GEN_CALL);
512
513 return stat;
514}
515
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300516/*
517 * Interrupt service routine. This gets called whenever an I2C interrupt
518 * occurs.
519 */
520static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
521{
522 struct dw_i2c_dev *dev = dev_id;
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900523 u32 stat;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300524
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900525 stat = i2c_dw_read_clear_intrbits(dev);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300526 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
Shinya Kuribayashie28000a2009-11-06 21:44:37 +0900527
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300528 if (stat & DW_IC_INTR_TX_ABRT) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300529 dev->cmd_err |= DW_IC_ERR_TX_ABRT;
530 dev->status = STATUS_IDLE;
531 } else if (stat & DW_IC_INTR_TX_EMPTY)
532 tasklet_schedule(&dev->pump_msg);
533
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900534 writel(0, dev->base + DW_IC_INTR_MASK); /* disable interrupts */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300535 if (stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET))
536 complete(&dev->cmd_complete);
537
538 return IRQ_HANDLED;
539}
540
541static struct i2c_algorithm i2c_dw_algo = {
542 .master_xfer = i2c_dw_xfer,
543 .functionality = i2c_dw_func,
544};
545
546static int __devinit dw_i2c_probe(struct platform_device *pdev)
547{
548 struct dw_i2c_dev *dev;
549 struct i2c_adapter *adap;
Shinya Kuribayashi91b52ca2009-11-06 21:45:07 +0900550 struct resource *mem, *ioarea;
551 int irq, r;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300552
553 /* NOTE: driver uses the static register mapping */
554 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
555 if (!mem) {
556 dev_err(&pdev->dev, "no mem resource?\n");
557 return -EINVAL;
558 }
559
Shinya Kuribayashi91b52ca2009-11-06 21:45:07 +0900560 irq = platform_get_irq(pdev, 0);
561 if (irq < 0) {
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300562 dev_err(&pdev->dev, "no irq resource?\n");
Shinya Kuribayashi91b52ca2009-11-06 21:45:07 +0900563 return irq; /* -ENXIO */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300564 }
565
566 ioarea = request_mem_region(mem->start, resource_size(mem),
567 pdev->name);
568 if (!ioarea) {
569 dev_err(&pdev->dev, "I2C region already claimed\n");
570 return -EBUSY;
571 }
572
573 dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
574 if (!dev) {
575 r = -ENOMEM;
576 goto err_release_region;
577 }
578
579 init_completion(&dev->cmd_complete);
580 tasklet_init(&dev->pump_msg, dw_i2c_pump_msg, (unsigned long) dev);
581 mutex_init(&dev->lock);
582 dev->dev = get_device(&pdev->dev);
Shinya Kuribayashi91b52ca2009-11-06 21:45:07 +0900583 dev->irq = irq;
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300584 platform_set_drvdata(pdev, dev);
585
586 dev->clk = clk_get(&pdev->dev, NULL);
587 if (IS_ERR(dev->clk)) {
588 r = -ENODEV;
589 goto err_free_mem;
590 }
591 clk_enable(dev->clk);
592
593 dev->base = ioremap(mem->start, resource_size(mem));
594 if (dev->base == NULL) {
595 dev_err(&pdev->dev, "failure mapping io resources\n");
596 r = -EBUSY;
597 goto err_unuse_clocks;
598 }
599 {
600 u32 param1 = readl(dev->base + DW_IC_COMP_PARAM_1);
601
602 dev->tx_fifo_depth = ((param1 >> 16) & 0xff) + 1;
603 dev->rx_fifo_depth = ((param1 >> 8) & 0xff) + 1;
604 }
605 i2c_dw_init(dev);
606
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900607 writel(0, dev->base + DW_IC_INTR_MASK); /* disable IRQ */
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300608 r = request_irq(dev->irq, i2c_dw_isr, 0, pdev->name, dev);
609 if (r) {
610 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
611 goto err_iounmap;
612 }
613
614 adap = &dev->adapter;
615 i2c_set_adapdata(adap, dev);
616 adap->owner = THIS_MODULE;
617 adap->class = I2C_CLASS_HWMON;
618 strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
619 sizeof(adap->name));
620 adap->algo = &i2c_dw_algo;
621 adap->dev.parent = &pdev->dev;
622
623 adap->nr = pdev->id;
624 r = i2c_add_numbered_adapter(adap);
625 if (r) {
626 dev_err(&pdev->dev, "failure adding adapter\n");
627 goto err_free_irq;
628 }
629
630 return 0;
631
632err_free_irq:
633 free_irq(dev->irq, dev);
634err_iounmap:
635 iounmap(dev->base);
636err_unuse_clocks:
637 clk_disable(dev->clk);
638 clk_put(dev->clk);
639 dev->clk = NULL;
640err_free_mem:
641 platform_set_drvdata(pdev, NULL);
642 put_device(&pdev->dev);
643 kfree(dev);
644err_release_region:
645 release_mem_region(mem->start, resource_size(mem));
646
647 return r;
648}
649
650static int __devexit dw_i2c_remove(struct platform_device *pdev)
651{
652 struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
653 struct resource *mem;
654
655 platform_set_drvdata(pdev, NULL);
656 i2c_del_adapter(&dev->adapter);
657 put_device(&pdev->dev);
658
659 clk_disable(dev->clk);
660 clk_put(dev->clk);
661 dev->clk = NULL;
662
Shinya Kuribayashied5e1dd2009-11-06 21:43:52 +0900663 writel(0, dev->base + DW_IC_ENABLE);
Baruch Siach1ab52cf2009-06-22 16:36:29 +0300664 free_irq(dev->irq, dev);
665 kfree(dev);
666
667 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
668 release_mem_region(mem->start, resource_size(mem));
669 return 0;
670}
671
672/* work with hotplug and coldplug */
673MODULE_ALIAS("platform:i2c_designware");
674
675static struct platform_driver dw_i2c_driver = {
676 .remove = __devexit_p(dw_i2c_remove),
677 .driver = {
678 .name = "i2c_designware",
679 .owner = THIS_MODULE,
680 },
681};
682
683static int __init dw_i2c_init_driver(void)
684{
685 return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
686}
687module_init(dw_i2c_init_driver);
688
689static void __exit dw_i2c_exit_driver(void)
690{
691 platform_driver_unregister(&dw_i2c_driver);
692}
693module_exit(dw_i2c_exit_driver);
694
695MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
696MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
697MODULE_LICENSE("GPL");