blob: 515822429b1987369215e4b081f0bcd2327e806a [file] [log] [blame]
Bryan Wud24ecfc2007-05-01 23:26:32 +02001/*
Mike Frysingerbd584992008-04-22 22:16:48 +02002 * Blackfin On-Chip Two Wire Interface Driver
Bryan Wud24ecfc2007-05-01 23:26:32 +02003 *
Mike Frysingerbd584992008-04-22 22:16:48 +02004 * Copyright 2005-2007 Analog Devices Inc.
Bryan Wud24ecfc2007-05-01 23:26:32 +02005 *
Mike Frysingerbd584992008-04-22 22:16:48 +02006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wud24ecfc2007-05-01 23:26:32 +02007 *
Mike Frysingerbd584992008-04-22 22:16:48 +02008 * Licensed under the GPL-2 or later.
Bryan Wud24ecfc2007-05-01 23:26:32 +02009 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mike Frysinger6df263c2009-06-14 01:55:37 -040016#include <linux/io.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020017#include <linux/mm.h>
18#include <linux/timer.h>
19#include <linux/spinlock.h>
20#include <linux/completion.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
Michael Hennerich540ac552011-01-11 00:25:08 -050023#include <linux/delay.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020024
25#include <asm/blackfin.h>
Bryan Wu74d362e2008-04-22 22:16:48 +020026#include <asm/portmux.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020027#include <asm/irq.h>
28
Bryan Wud24ecfc2007-05-01 23:26:32 +020029/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020030#define TWI_I2C_MODE_STANDARD 1
31#define TWI_I2C_MODE_STANDARDSUB 2
32#define TWI_I2C_MODE_COMBINED 3
33#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020034
35struct bfin_twi_iface {
Bryan Wud24ecfc2007-05-01 23:26:32 +020036 int irq;
37 spinlock_t lock;
38 char read_write;
39 u8 command;
40 u8 *transPtr;
41 int readNum;
42 int writeNum;
43 int cur_mode;
44 int manual_stop;
45 int result;
Bryan Wud24ecfc2007-05-01 23:26:32 +020046 struct i2c_adapter adap;
47 struct completion complete;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020048 struct i2c_msg *pmsg;
49 int msg_num;
50 int cur_msg;
Michael Hennerich958585f2008-07-27 14:41:54 +080051 u16 saved_clkdiv;
52 u16 saved_control;
Bryan Wuaa3d0202008-04-22 22:16:48 +020053 void __iomem *regs_base;
Bryan Wud24ecfc2007-05-01 23:26:32 +020054};
55
Bryan Wuaa3d0202008-04-22 22:16:48 +020056
57#define DEFINE_TWI_REG(reg, off) \
58static inline u16 read_##reg(struct bfin_twi_iface *iface) \
59 { return bfin_read16(iface->regs_base + (off)); } \
60static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \
61 { bfin_write16(iface->regs_base + (off), v); }
62
63DEFINE_TWI_REG(CLKDIV, 0x00)
64DEFINE_TWI_REG(CONTROL, 0x04)
65DEFINE_TWI_REG(SLAVE_CTL, 0x08)
66DEFINE_TWI_REG(SLAVE_STAT, 0x0C)
67DEFINE_TWI_REG(SLAVE_ADDR, 0x10)
68DEFINE_TWI_REG(MASTER_CTL, 0x14)
69DEFINE_TWI_REG(MASTER_STAT, 0x18)
70DEFINE_TWI_REG(MASTER_ADDR, 0x1C)
71DEFINE_TWI_REG(INT_STAT, 0x20)
72DEFINE_TWI_REG(INT_MASK, 0x24)
73DEFINE_TWI_REG(FIFO_CTL, 0x28)
74DEFINE_TWI_REG(FIFO_STAT, 0x2C)
75DEFINE_TWI_REG(XMT_DATA8, 0x80)
76DEFINE_TWI_REG(XMT_DATA16, 0x84)
77DEFINE_TWI_REG(RCV_DATA8, 0x88)
78DEFINE_TWI_REG(RCV_DATA16, 0x8C)
Bryan Wud24ecfc2007-05-01 23:26:32 +020079
Bryan Wu74d362e2008-04-22 22:16:48 +020080static const u16 pin_req[2][3] = {
81 {P_TWI0_SCL, P_TWI0_SDA, 0},
82 {P_TWI1_SCL, P_TWI1_SDA, 0},
83};
84
Sonic Zhang5481d072010-03-22 03:23:18 -040085static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
86 unsigned short twi_int_status)
Bryan Wud24ecfc2007-05-01 23:26:32 +020087{
Bryan Wuaa3d0202008-04-22 22:16:48 +020088 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020089
90 if (twi_int_status & XMTSERV) {
91 /* Transmit next data */
92 if (iface->writeNum > 0) {
Sonic Zhang5481d072010-03-22 03:23:18 -040093 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +020094 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020095 iface->writeNum--;
96 }
97 /* start receive immediately after complete sending in
98 * combine mode.
99 */
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200100 else if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200101 write_MASTER_CTL(iface,
102 read_MASTER_CTL(iface) | MDIR | RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200103 else if (iface->manual_stop)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200104 write_MASTER_CTL(iface,
105 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200106 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400107 iface->cur_msg + 1 < iface->msg_num) {
108 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
109 write_MASTER_CTL(iface,
110 read_MASTER_CTL(iface) | RSTART | MDIR);
111 else
112 write_MASTER_CTL(iface,
113 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
114 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200115 }
116 if (twi_int_status & RCVSERV) {
117 if (iface->readNum > 0) {
118 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200119 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200120 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
121 /* Change combine mode into sub mode after
122 * read first data.
123 */
124 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
125 /* Get read number from first byte in block
126 * combine mode.
127 */
128 if (iface->readNum == 1 && iface->manual_stop)
129 iface->readNum = *iface->transPtr + 1;
130 }
131 iface->transPtr++;
132 iface->readNum--;
133 } else if (iface->manual_stop) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200134 write_MASTER_CTL(iface,
135 read_MASTER_CTL(iface) | STOP);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200136 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Frank Shew94327d02009-05-19 07:23:49 -0400137 iface->cur_msg + 1 < iface->msg_num) {
138 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
139 write_MASTER_CTL(iface,
140 read_MASTER_CTL(iface) | RSTART | MDIR);
141 else
142 write_MASTER_CTL(iface,
143 (read_MASTER_CTL(iface) | RSTART) & ~MDIR);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200144 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200145 }
146 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200147 write_INT_MASK(iface, 0);
148 write_MASTER_STAT(iface, 0x3e);
149 write_MASTER_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200150 iface->result = -EIO;
Michael Hennerich5cfafc12010-03-22 03:23:17 -0400151
152 if (mast_stat & LOSTARB)
153 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
154 if (mast_stat & ANAK)
155 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
156 if (mast_stat & DNAK)
157 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
158 if (mast_stat & BUFRDERR)
159 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
160 if (mast_stat & BUFWRERR)
161 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
162
Michael Hennerich540ac552011-01-11 00:25:08 -0500163 /* Faulty slave devices, may drive SDA low after a transfer
164 * finishes. To release the bus this code generates up to 9
165 * extra clocks until SDA is released.
166 */
167
168 if (read_MASTER_STAT(iface) & SDASEN) {
169 int cnt = 9;
170 do {
171 write_MASTER_CTL(iface, SCLOVR);
172 udelay(6);
173 write_MASTER_CTL(iface, 0);
174 udelay(6);
175 } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);
176
177 write_MASTER_CTL(iface, SDAOVR | SCLOVR);
178 udelay(6);
179 write_MASTER_CTL(iface, SDAOVR);
180 udelay(6);
181 write_MASTER_CTL(iface, 0);
182 }
183
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400184 /* If it is a quick transfer, only address without data,
185 * not an err, return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200186 */
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400187 if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
188 iface->transPtr == NULL &&
189 (twi_int_status & MCOMP) && (mast_stat & DNAK))
190 iface->result = 1;
191
Bryan Wud24ecfc2007-05-01 23:26:32 +0200192 complete(&iface->complete);
193 return;
194 }
195 if (twi_int_status & MCOMP) {
Sonic Zhang4a651632011-06-23 17:07:54 -0400196 if ((read_MASTER_CTL(iface) & MEN) == 0 &&
197 (iface->cur_mode == TWI_I2C_MODE_REPEAT ||
198 iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
199 iface->result = -1;
200 write_INT_MASK(iface, 0);
201 write_MASTER_CTL(iface, 0);
202 } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
Bryan Wud24ecfc2007-05-01 23:26:32 +0200203 if (iface->readNum == 0) {
204 /* set the read number to 1 and ask for manual
205 * stop in block combine mode
206 */
207 iface->readNum = 1;
208 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200209 write_MASTER_CTL(iface,
210 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200211 } else {
212 /* set the readd number in other
213 * combine mode.
214 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200215 write_MASTER_CTL(iface,
216 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200217 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200218 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200219 }
220 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200221 write_MASTER_CTL(iface,
222 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200223 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
224 iface->cur_msg+1 < iface->msg_num) {
225 iface->cur_msg++;
226 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
227 iface->writeNum = iface->readNum =
228 iface->pmsg[iface->cur_msg].len;
229 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200230 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200231 iface->pmsg[iface->cur_msg].addr);
232 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
233 iface->read_write = I2C_SMBUS_READ;
234 else {
235 iface->read_write = I2C_SMBUS_WRITE;
236 /* Transmit first data */
237 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200238 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200239 *(iface->transPtr++));
240 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200241 }
242 }
243
244 if (iface->pmsg[iface->cur_msg].len <= 255)
Sonic Zhang57a8f322009-05-19 07:21:58 -0400245 write_MASTER_CTL(iface,
246 (read_MASTER_CTL(iface) &
247 (~(0xff << 6))) |
248 (iface->pmsg[iface->cur_msg].len << 6));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200249 else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400250 write_MASTER_CTL(iface,
251 (read_MASTER_CTL(iface) |
252 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200253 iface->manual_stop = 1;
254 }
255 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200256 write_MASTER_CTL(iface,
257 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200258 } else {
259 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200260 write_INT_MASK(iface, 0);
261 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200262 }
263 }
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400264 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200265}
266
267/* Interrupt handler */
268static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
269{
270 struct bfin_twi_iface *iface = dev_id;
271 unsigned long flags;
Sonic Zhang5481d072010-03-22 03:23:18 -0400272 unsigned short twi_int_status;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200273
274 spin_lock_irqsave(&iface->lock, flags);
Sonic Zhang5481d072010-03-22 03:23:18 -0400275 while (1) {
276 twi_int_status = read_INT_STAT(iface);
277 if (!twi_int_status)
278 break;
279 /* Clear interrupt status */
280 write_INT_STAT(iface, twi_int_status);
281 bfin_twi_handle_interrupt(iface, twi_int_status);
282 SSYNC();
283 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200284 spin_unlock_irqrestore(&iface->lock, flags);
285 return IRQ_HANDLED;
286}
287
Bryan Wud24ecfc2007-05-01 23:26:32 +0200288/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400289 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200290 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400291static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200292 struct i2c_msg *msgs, int num)
293{
294 struct bfin_twi_iface *iface = adap->algo_data;
295 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200296 int rc = 0;
297
Bryan Wuaa3d0202008-04-22 22:16:48 +0200298 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200299 return -ENXIO;
300
Bryan Wuaa3d0202008-04-22 22:16:48 +0200301 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200302 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200303
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200304 iface->pmsg = msgs;
305 iface->msg_num = num;
306 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200307
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200308 pmsg = &msgs[0];
309 if (pmsg->flags & I2C_M_TEN) {
310 dev_err(&adap->dev, "10 bits addr not supported!\n");
311 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200312 }
313
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200314 iface->cur_mode = TWI_I2C_MODE_REPEAT;
315 iface->manual_stop = 0;
316 iface->transPtr = pmsg->buf;
317 iface->writeNum = iface->readNum = pmsg->len;
318 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200319 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200320 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200321 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200322
323 /* FIFO Initiation. Data in FIFO should be
324 * discarded before start a new operation.
325 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200326 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200327 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200328 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200329 SSYNC();
330
331 if (pmsg->flags & I2C_M_RD)
332 iface->read_write = I2C_SMBUS_READ;
333 else {
334 iface->read_write = I2C_SMBUS_WRITE;
335 /* Transmit first data */
336 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200337 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200338 iface->writeNum--;
339 SSYNC();
340 }
341 }
342
343 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200344 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200345
346 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200347 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200348 SSYNC();
349
350 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200351 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200352 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200353 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200354 iface->manual_stop = 1;
355 }
356
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200357 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200358 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200359 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
360 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
361 SSYNC();
362
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400363 while (!iface->result) {
364 if (!wait_for_completion_timeout(&iface->complete,
365 adap->timeout)) {
366 iface->result = -1;
367 dev_err(&adap->dev, "master transfer timeout\n");
368 }
369 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200370
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400371 if (iface->result == 1)
372 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200373 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400374 rc = iface->result;
375
376 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200377}
378
379/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400380 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200381 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400382static int bfin_twi_master_xfer(struct i2c_adapter *adap,
383 struct i2c_msg *msgs, int num)
384{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400385 return bfin_twi_do_master_xfer(adap, msgs, num);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400386}
387
388/*
389 * One I2C SMBus transfer
390 */
391int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200392 unsigned short flags, char read_write,
393 u8 command, int size, union i2c_smbus_data *data)
394{
395 struct bfin_twi_iface *iface = adap->algo_data;
396 int rc = 0;
397
Bryan Wuaa3d0202008-04-22 22:16:48 +0200398 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200399 return -ENXIO;
400
Bryan Wuaa3d0202008-04-22 22:16:48 +0200401 while (read_MASTER_STAT(iface) & BUSBUSY)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200402 yield();
Bryan Wud24ecfc2007-05-01 23:26:32 +0200403
404 iface->writeNum = 0;
405 iface->readNum = 0;
406
407 /* Prepare datas & select mode */
408 switch (size) {
409 case I2C_SMBUS_QUICK:
410 iface->transPtr = NULL;
411 iface->cur_mode = TWI_I2C_MODE_STANDARD;
412 break;
413 case I2C_SMBUS_BYTE:
414 if (data == NULL)
415 iface->transPtr = NULL;
416 else {
417 if (read_write == I2C_SMBUS_READ)
418 iface->readNum = 1;
419 else
420 iface->writeNum = 1;
421 iface->transPtr = &data->byte;
422 }
423 iface->cur_mode = TWI_I2C_MODE_STANDARD;
424 break;
425 case I2C_SMBUS_BYTE_DATA:
426 if (read_write == I2C_SMBUS_READ) {
427 iface->readNum = 1;
428 iface->cur_mode = TWI_I2C_MODE_COMBINED;
429 } else {
430 iface->writeNum = 1;
431 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
432 }
433 iface->transPtr = &data->byte;
434 break;
435 case I2C_SMBUS_WORD_DATA:
436 if (read_write == I2C_SMBUS_READ) {
437 iface->readNum = 2;
438 iface->cur_mode = TWI_I2C_MODE_COMBINED;
439 } else {
440 iface->writeNum = 2;
441 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
442 }
443 iface->transPtr = (u8 *)&data->word;
444 break;
445 case I2C_SMBUS_PROC_CALL:
446 iface->writeNum = 2;
447 iface->readNum = 2;
448 iface->cur_mode = TWI_I2C_MODE_COMBINED;
449 iface->transPtr = (u8 *)&data->word;
450 break;
451 case I2C_SMBUS_BLOCK_DATA:
452 if (read_write == I2C_SMBUS_READ) {
453 iface->readNum = 0;
454 iface->cur_mode = TWI_I2C_MODE_COMBINED;
455 } else {
456 iface->writeNum = data->block[0] + 1;
457 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
458 }
459 iface->transPtr = data->block;
460 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000461 case I2C_SMBUS_I2C_BLOCK_DATA:
462 if (read_write == I2C_SMBUS_READ) {
463 iface->readNum = data->block[0];
464 iface->cur_mode = TWI_I2C_MODE_COMBINED;
465 } else {
466 iface->writeNum = data->block[0];
467 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
468 }
469 iface->transPtr = (u8 *)&data->block[1];
470 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200471 default:
472 return -1;
473 }
474
475 iface->result = 0;
476 iface->manual_stop = 0;
477 iface->read_write = read_write;
478 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200479 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200480
481 /* FIFO Initiation. Data in FIFO should be discarded before
482 * start a new operation.
483 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200484 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200485 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200486 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200487
488 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200489 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200490
491 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200492 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200493 SSYNC();
494
Bryan Wud24ecfc2007-05-01 23:26:32 +0200495 switch (iface->cur_mode) {
496 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200497 write_XMT_DATA8(iface, iface->command);
498 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200499 ((iface->read_write == I2C_SMBUS_READ) ?
500 RCVSERV : XMTSERV));
501 SSYNC();
502
503 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200504 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200505 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200506 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200507 iface->manual_stop = 1;
508 }
509 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200510 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200511 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
512 break;
513 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200514 write_XMT_DATA8(iface, iface->command);
515 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200516 SSYNC();
517
518 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200519 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200520 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200521 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200522 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200523 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200524 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
525 break;
526 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200527 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200528 if (size != I2C_SMBUS_QUICK) {
529 /* Don't access xmit data register when this is a
530 * read operation.
531 */
532 if (iface->read_write != I2C_SMBUS_READ) {
533 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200534 write_XMT_DATA8(iface,
535 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200536 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200537 write_MASTER_CTL(iface,
538 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200539 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200540 write_MASTER_CTL(iface,
541 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200542 iface->manual_stop = 1;
543 }
544 iface->writeNum--;
545 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200546 write_XMT_DATA8(iface, iface->command);
547 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200548 }
549 } else {
550 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200551 write_MASTER_CTL(iface,
552 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200553 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200554 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200555 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400556 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200557 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200558 }
559 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200560 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200561 ((iface->read_write == I2C_SMBUS_READ) ?
562 RCVSERV : XMTSERV));
563 SSYNC();
564
565 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200566 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200567 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
568 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
569 break;
570 }
571 SSYNC();
572
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400573 while (!iface->result) {
574 if (!wait_for_completion_timeout(&iface->complete,
575 adap->timeout)) {
576 iface->result = -1;
577 dev_err(&adap->dev, "smbus transfer timeout\n");
578 }
579 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200580
581 rc = (iface->result >= 0) ? 0 : -1;
582
Bryan Wud24ecfc2007-05-01 23:26:32 +0200583 return rc;
584}
585
586/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400587 * Generic I2C SMBus transfer entrypoint
588 */
589int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
590 unsigned short flags, char read_write,
591 u8 command, int size, union i2c_smbus_data *data)
592{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400593 return bfin_twi_do_smbus_xfer(adap, addr, flags,
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400594 read_write, command, size, data);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400595}
596
597/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200598 * Return what the adapter supports
599 */
600static u32 bfin_twi_functionality(struct i2c_adapter *adap)
601{
602 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
603 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
604 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000605 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200606}
607
Bryan Wud24ecfc2007-05-01 23:26:32 +0200608static struct i2c_algorithm bfin_twi_algorithm = {
609 .master_xfer = bfin_twi_master_xfer,
610 .smbus_xfer = bfin_twi_smbus_xfer,
611 .functionality = bfin_twi_functionality,
612};
613
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200614static int i2c_bfin_twi_suspend(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200615{
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200616 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Michael Hennerich958585f2008-07-27 14:41:54 +0800617
618 iface->saved_clkdiv = read_CLKDIV(iface);
619 iface->saved_control = read_CONTROL(iface);
620
621 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200622
623 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800624 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200625
626 return 0;
627}
628
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200629static int i2c_bfin_twi_resume(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200630{
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200631 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200632
Michael Hennerich958585f2008-07-27 14:41:54 +0800633 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200634 0, to_platform_device(dev)->name, iface);
Michael Hennerich958585f2008-07-27 14:41:54 +0800635 if (rc) {
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200636 dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
Michael Hennerich958585f2008-07-27 14:41:54 +0800637 return -ENODEV;
638 }
639
640 /* Resume TWI interface clock as specified */
641 write_CLKDIV(iface, iface->saved_clkdiv);
642
643 /* Resume TWI */
644 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200645
646 return 0;
647}
648
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200649static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
650 i2c_bfin_twi_suspend, i2c_bfin_twi_resume);
651
Bryan Wuaa3d0202008-04-22 22:16:48 +0200652static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200653{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200654 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200655 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200656 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200657 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400658 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200659
Bryan Wuaa3d0202008-04-22 22:16:48 +0200660 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
661 if (!iface) {
662 dev_err(&pdev->dev, "Cannot allocate memory\n");
663 rc = -ENOMEM;
664 goto out_error_nomem;
665 }
666
Bryan Wud24ecfc2007-05-01 23:26:32 +0200667 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200668
669 /* Find and map our resources */
670 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
671 if (res == NULL) {
672 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
673 rc = -ENOENT;
674 goto out_error_get_res;
675 }
676
Linus Walleijc6ffdde2009-06-14 00:20:36 +0200677 iface->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200678 if (iface->regs_base == NULL) {
679 dev_err(&pdev->dev, "Cannot map IO\n");
680 rc = -ENXIO;
681 goto out_error_ioremap;
682 }
683
684 iface->irq = platform_get_irq(pdev, 0);
685 if (iface->irq < 0) {
686 dev_err(&pdev->dev, "No IRQ specified\n");
687 rc = -ENOENT;
688 goto out_error_no_irq;
689 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200690
Bryan Wud24ecfc2007-05-01 23:26:32 +0200691 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200692 p_adap->nr = pdev->id;
693 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200694 p_adap->algo = &bfin_twi_algorithm;
695 p_adap->algo_data = iface;
Jean Delvaree1995f62009-01-07 14:29:16 +0100696 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200697 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400698 p_adap->timeout = 5 * HZ;
699 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200700
Bryan Wu74d362e2008-04-22 22:16:48 +0200701 rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi");
702 if (rc) {
703 dev_err(&pdev->dev, "Can't setup pin mux!\n");
704 goto out_error_pin_mux;
705 }
706
Bryan Wud24ecfc2007-05-01 23:26:32 +0200707 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Yong Zhang43110512011-09-21 17:28:33 +0800708 0, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200709 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200710 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
711 rc = -ENODEV;
712 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200713 }
714
715 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500716 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200717
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400718 /*
719 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500720 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400721 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500722 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400723
Bryan Wud24ecfc2007-05-01 23:26:32 +0200724 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400725 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200726
727 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200728 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200729 SSYNC();
730
Kalle Pokki991dee52008-01-27 18:14:52 +0100731 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200732 if (rc < 0) {
733 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
734 goto out_error_add_adapter;
735 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200736
Bryan Wuaa3d0202008-04-22 22:16:48 +0200737 platform_set_drvdata(pdev, iface);
738
Bryan Wufa6ad222008-04-22 22:16:48 +0200739 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
740 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200741
742 return 0;
743
744out_error_add_adapter:
745 free_irq(iface->irq, iface);
746out_error_req_irq:
747out_error_no_irq:
Bryan Wu74d362e2008-04-22 22:16:48 +0200748 peripheral_free_list(pin_req[pdev->id]);
749out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200750 iounmap(iface->regs_base);
751out_error_ioremap:
752out_error_get_res:
753 kfree(iface);
754out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200755 return rc;
756}
757
758static int i2c_bfin_twi_remove(struct platform_device *pdev)
759{
760 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
761
762 platform_set_drvdata(pdev, NULL);
763
764 i2c_del_adapter(&(iface->adap));
765 free_irq(iface->irq, iface);
Bryan Wu74d362e2008-04-22 22:16:48 +0200766 peripheral_free_list(pin_req[pdev->id]);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200767 iounmap(iface->regs_base);
768 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200769
770 return 0;
771}
772
773static struct platform_driver i2c_bfin_twi_driver = {
774 .probe = i2c_bfin_twi_probe,
775 .remove = i2c_bfin_twi_remove,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200776 .driver = {
777 .name = "i2c-bfin-twi",
778 .owner = THIS_MODULE,
Rafael J. Wysocki85777ad22012-07-11 21:23:31 +0200779 .pm = &i2c_bfin_twi_pm,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200780 },
781};
782
783static int __init i2c_bfin_twi_init(void)
784{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200785 return platform_driver_register(&i2c_bfin_twi_driver);
786}
787
788static void __exit i2c_bfin_twi_exit(void)
789{
790 platform_driver_unregister(&i2c_bfin_twi_driver);
791}
792
Michael Hennerich74f56c42011-01-11 00:25:09 -0500793subsys_initcall(i2c_bfin_twi_init);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200794module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200795
796MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
797MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
798MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200799MODULE_ALIAS("platform:i2c-bfin-twi");