blob: c75f0e943b32362d6bd9aa373acde40719c969a6 [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>
Sonic Zhangc9d87ed2012-06-13 16:22:45 +080028#include <asm/bfin_twi.h>
Bryan Wud24ecfc2007-05-01 23:26:32 +020029
Bryan Wud24ecfc2007-05-01 23:26:32 +020030/* SMBus mode*/
Sonic Zhang4dd39bb2008-04-22 22:16:47 +020031#define TWI_I2C_MODE_STANDARD 1
32#define TWI_I2C_MODE_STANDARDSUB 2
33#define TWI_I2C_MODE_COMBINED 3
34#define TWI_I2C_MODE_REPEAT 4
Bryan Wud24ecfc2007-05-01 23:26:32 +020035
Sonic Zhang5481d072010-03-22 03:23:18 -040036static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface,
37 unsigned short twi_int_status)
Bryan Wud24ecfc2007-05-01 23:26:32 +020038{
Bryan Wuaa3d0202008-04-22 22:16:48 +020039 unsigned short mast_stat = read_MASTER_STAT(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020040
41 if (twi_int_status & XMTSERV) {
Sonic Zhang8419c8d2013-05-28 18:41:09 +080042 if (iface->writeNum <= 0) {
43 /* start receive immediately after complete sending in
44 * combine mode.
45 */
46 if (iface->cur_mode == TWI_I2C_MODE_COMBINED)
47 write_MASTER_CTL(iface,
48 read_MASTER_CTL(iface) | MDIR);
49 else if (iface->manual_stop)
50 write_MASTER_CTL(iface,
51 read_MASTER_CTL(iface) | STOP);
52 else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
53 iface->cur_msg + 1 < iface->msg_num) {
54 if (iface->pmsg[iface->cur_msg + 1].flags &
55 I2C_M_RD)
56 write_MASTER_CTL(iface,
57 read_MASTER_CTL(iface) |
58 MDIR);
59 else
60 write_MASTER_CTL(iface,
61 read_MASTER_CTL(iface) &
62 ~MDIR);
63 }
64 }
Bryan Wud24ecfc2007-05-01 23:26:32 +020065 /* Transmit next data */
Sonic Zhang8419c8d2013-05-28 18:41:09 +080066 while (iface->writeNum > 0 &&
67 (read_FIFO_STAT(iface) & XMTSTAT) != XMT_FULL) {
Sonic Zhang5481d072010-03-22 03:23:18 -040068 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +020069 write_XMT_DATA8(iface, *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +020070 iface->writeNum--;
71 }
Bryan Wud24ecfc2007-05-01 23:26:32 +020072 }
73 if (twi_int_status & RCVSERV) {
Sonic Zhang8419c8d2013-05-28 18:41:09 +080074 while (iface->readNum > 0 &&
75 (read_FIFO_STAT(iface) & RCVSTAT)) {
Bryan Wud24ecfc2007-05-01 23:26:32 +020076 /* Receive next data */
Bryan Wuaa3d0202008-04-22 22:16:48 +020077 *(iface->transPtr) = read_RCV_DATA8(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +020078 if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
79 /* Change combine mode into sub mode after
80 * read first data.
81 */
82 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
83 /* Get read number from first byte in block
84 * combine mode.
85 */
86 if (iface->readNum == 1 && iface->manual_stop)
87 iface->readNum = *iface->transPtr + 1;
88 }
89 iface->transPtr++;
90 iface->readNum--;
Sonic Zhanga20a64d2012-06-13 16:22:41 +080091 }
92
93 if (iface->readNum == 0) {
94 if (iface->manual_stop) {
95 /* Temporary workaround to avoid possible bus stall -
96 * Flush FIFO before issuing the STOP condition
97 */
98 read_RCV_DATA16(iface);
Frank Shew94327d02009-05-19 07:23:49 -040099 write_MASTER_CTL(iface,
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800100 read_MASTER_CTL(iface) | STOP);
101 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
102 iface->cur_msg + 1 < iface->msg_num) {
103 if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD)
104 write_MASTER_CTL(iface,
Sonic Zhang28a377c2012-06-13 16:22:44 +0800105 read_MASTER_CTL(iface) | MDIR);
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800106 else
107 write_MASTER_CTL(iface,
Sonic Zhang28a377c2012-06-13 16:22:44 +0800108 read_MASTER_CTL(iface) & ~MDIR);
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800109 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200110 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200111 }
112 if (twi_int_status & MERR) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200113 write_INT_MASK(iface, 0);
114 write_MASTER_STAT(iface, 0x3e);
115 write_MASTER_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200116 iface->result = -EIO;
Michael Hennerich5cfafc12010-03-22 03:23:17 -0400117
118 if (mast_stat & LOSTARB)
119 dev_dbg(&iface->adap.dev, "Lost Arbitration\n");
120 if (mast_stat & ANAK)
121 dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n");
122 if (mast_stat & DNAK)
123 dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n");
124 if (mast_stat & BUFRDERR)
125 dev_dbg(&iface->adap.dev, "Buffer Read Error\n");
126 if (mast_stat & BUFWRERR)
127 dev_dbg(&iface->adap.dev, "Buffer Write Error\n");
128
Michael Hennerich540ac552011-01-11 00:25:08 -0500129 /* Faulty slave devices, may drive SDA low after a transfer
130 * finishes. To release the bus this code generates up to 9
131 * extra clocks until SDA is released.
132 */
133
134 if (read_MASTER_STAT(iface) & SDASEN) {
135 int cnt = 9;
136 do {
137 write_MASTER_CTL(iface, SCLOVR);
138 udelay(6);
139 write_MASTER_CTL(iface, 0);
140 udelay(6);
141 } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--);
142
143 write_MASTER_CTL(iface, SDAOVR | SCLOVR);
144 udelay(6);
145 write_MASTER_CTL(iface, SDAOVR);
146 udelay(6);
147 write_MASTER_CTL(iface, 0);
148 }
149
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400150 /* If it is a quick transfer, only address without data,
151 * not an err, return 1.
Bryan Wud24ecfc2007-05-01 23:26:32 +0200152 */
Sonic Zhangf0ac1312010-03-22 03:23:20 -0400153 if (iface->cur_mode == TWI_I2C_MODE_STANDARD &&
154 iface->transPtr == NULL &&
155 (twi_int_status & MCOMP) && (mast_stat & DNAK))
156 iface->result = 1;
157
Bryan Wud24ecfc2007-05-01 23:26:32 +0200158 complete(&iface->complete);
159 return;
160 }
161 if (twi_int_status & MCOMP) {
Sonic Zhang2ee74eb2012-06-13 16:22:43 +0800162 if (twi_int_status & (XMTSERV | RCVSERV) &&
163 (read_MASTER_CTL(iface) & MEN) == 0 &&
Sonic Zhang4a651632011-06-23 17:07:54 -0400164 (iface->cur_mode == TWI_I2C_MODE_REPEAT ||
165 iface->cur_mode == TWI_I2C_MODE_COMBINED)) {
166 iface->result = -1;
167 write_INT_MASK(iface, 0);
168 write_MASTER_CTL(iface, 0);
169 } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) {
Bryan Wud24ecfc2007-05-01 23:26:32 +0200170 if (iface->readNum == 0) {
171 /* set the read number to 1 and ask for manual
172 * stop in block combine mode
173 */
174 iface->readNum = 1;
175 iface->manual_stop = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200176 write_MASTER_CTL(iface,
177 read_MASTER_CTL(iface) | (0xff << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200178 } else {
179 /* set the readd number in other
180 * combine mode.
181 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200182 write_MASTER_CTL(iface,
183 (read_MASTER_CTL(iface) &
Bryan Wud24ecfc2007-05-01 23:26:32 +0200184 (~(0xff << 6))) |
Bryan Wuaa3d0202008-04-22 22:16:48 +0200185 (iface->readNum << 6));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200186 }
187 /* remove restart bit and enable master receive */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200188 write_MASTER_CTL(iface,
189 read_MASTER_CTL(iface) & ~RSTART);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200190 } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT &&
Sonic Zhang28a377c2012-06-13 16:22:44 +0800191 iface->cur_msg + 1 < iface->msg_num) {
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200192 iface->cur_msg++;
193 iface->transPtr = iface->pmsg[iface->cur_msg].buf;
194 iface->writeNum = iface->readNum =
195 iface->pmsg[iface->cur_msg].len;
196 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200197 write_MASTER_ADDR(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200198 iface->pmsg[iface->cur_msg].addr);
199 if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD)
200 iface->read_write = I2C_SMBUS_READ;
201 else {
202 iface->read_write = I2C_SMBUS_WRITE;
203 /* Transmit first data */
204 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200205 write_XMT_DATA8(iface,
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200206 *(iface->transPtr++));
207 iface->writeNum--;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200208 }
209 }
210
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800211 if (iface->pmsg[iface->cur_msg].len <= 255) {
212 write_MASTER_CTL(iface,
Sonic Zhang57a8f322009-05-19 07:21:58 -0400213 (read_MASTER_CTL(iface) &
214 (~(0xff << 6))) |
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800215 (iface->pmsg[iface->cur_msg].len << 6));
216 iface->manual_stop = 0;
217 } else {
Sonic Zhang57a8f322009-05-19 07:21:58 -0400218 write_MASTER_CTL(iface,
219 (read_MASTER_CTL(iface) |
220 (0xff << 6)));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200221 iface->manual_stop = 1;
222 }
Sonic Zhang28a377c2012-06-13 16:22:44 +0800223 /* remove restart bit before last message */
224 if (iface->cur_msg + 1 == iface->msg_num)
225 write_MASTER_CTL(iface,
226 read_MASTER_CTL(iface) & ~RSTART);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200227 } else {
228 iface->result = 1;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200229 write_INT_MASK(iface, 0);
230 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200231 }
Sonic Zhanga20a64d2012-06-13 16:22:41 +0800232 complete(&iface->complete);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200233 }
234}
235
236/* Interrupt handler */
237static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id)
238{
239 struct bfin_twi_iface *iface = dev_id;
240 unsigned long flags;
Sonic Zhang5481d072010-03-22 03:23:18 -0400241 unsigned short twi_int_status;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200242
243 spin_lock_irqsave(&iface->lock, flags);
Sonic Zhang5481d072010-03-22 03:23:18 -0400244 while (1) {
245 twi_int_status = read_INT_STAT(iface);
246 if (!twi_int_status)
247 break;
248 /* Clear interrupt status */
249 write_INT_STAT(iface, twi_int_status);
250 bfin_twi_handle_interrupt(iface, twi_int_status);
251 SSYNC();
252 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200253 spin_unlock_irqrestore(&iface->lock, flags);
254 return IRQ_HANDLED;
255}
256
Bryan Wud24ecfc2007-05-01 23:26:32 +0200257/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400258 * One i2c master transfer
Bryan Wud24ecfc2007-05-01 23:26:32 +0200259 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400260static int bfin_twi_do_master_xfer(struct i2c_adapter *adap,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200261 struct i2c_msg *msgs, int num)
262{
263 struct bfin_twi_iface *iface = adap->algo_data;
264 struct i2c_msg *pmsg;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200265 int rc = 0;
266
Bryan Wuaa3d0202008-04-22 22:16:48 +0200267 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200268 return -ENXIO;
269
Sonic Zhanga25733d2012-06-13 16:22:42 +0800270 if (read_MASTER_STAT(iface) & BUSBUSY)
271 return -EAGAIN;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200272
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200273 iface->pmsg = msgs;
274 iface->msg_num = num;
275 iface->cur_msg = 0;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200276
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200277 pmsg = &msgs[0];
278 if (pmsg->flags & I2C_M_TEN) {
279 dev_err(&adap->dev, "10 bits addr not supported!\n");
280 return -EINVAL;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200281 }
282
Sonic Zhang28a377c2012-06-13 16:22:44 +0800283 if (iface->msg_num > 1)
284 iface->cur_mode = TWI_I2C_MODE_REPEAT;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200285 iface->manual_stop = 0;
286 iface->transPtr = pmsg->buf;
287 iface->writeNum = iface->readNum = pmsg->len;
288 iface->result = 0;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200289 init_completion(&(iface->complete));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200290 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200291 write_MASTER_ADDR(iface, pmsg->addr);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200292
293 /* FIFO Initiation. Data in FIFO should be
294 * discarded before start a new operation.
295 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200296 write_FIFO_CTL(iface, 0x3);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200297 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200298 write_FIFO_CTL(iface, 0);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200299 SSYNC();
300
301 if (pmsg->flags & I2C_M_RD)
302 iface->read_write = I2C_SMBUS_READ;
303 else {
304 iface->read_write = I2C_SMBUS_WRITE;
305 /* Transmit first data */
306 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200307 write_XMT_DATA8(iface, *(iface->transPtr++));
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200308 iface->writeNum--;
309 SSYNC();
310 }
311 }
312
313 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200314 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200315
316 /* Interrupt mask . Enable XMT, RCV interrupt */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200317 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200318 SSYNC();
319
320 if (pmsg->len <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200321 write_MASTER_CTL(iface, pmsg->len << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200322 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200323 write_MASTER_CTL(iface, 0xff << 6);
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200324 iface->manual_stop = 1;
325 }
326
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200327 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200328 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Sonic Zhang28a377c2012-06-13 16:22:44 +0800329 (iface->msg_num > 1 ? RSTART : 0) |
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200330 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
331 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
332 SSYNC();
333
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400334 while (!iface->result) {
335 if (!wait_for_completion_timeout(&iface->complete,
336 adap->timeout)) {
337 iface->result = -1;
338 dev_err(&adap->dev, "master transfer timeout\n");
339 }
340 }
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200341
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400342 if (iface->result == 1)
343 rc = iface->cur_msg + 1;
Sonic Zhang4dd39bb2008-04-22 22:16:47 +0200344 else
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400345 rc = iface->result;
346
347 return rc;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200348}
349
350/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400351 * Generic i2c master transfer entrypoint
Bryan Wud24ecfc2007-05-01 23:26:32 +0200352 */
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400353static int bfin_twi_master_xfer(struct i2c_adapter *adap,
354 struct i2c_msg *msgs, int num)
355{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400356 return bfin_twi_do_master_xfer(adap, msgs, num);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400357}
358
359/*
360 * One I2C SMBus transfer
361 */
362int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200363 unsigned short flags, char read_write,
364 u8 command, int size, union i2c_smbus_data *data)
365{
366 struct bfin_twi_iface *iface = adap->algo_data;
367 int rc = 0;
368
Bryan Wuaa3d0202008-04-22 22:16:48 +0200369 if (!(read_CONTROL(iface) & TWI_ENA))
Bryan Wud24ecfc2007-05-01 23:26:32 +0200370 return -ENXIO;
371
Sonic Zhanga25733d2012-06-13 16:22:42 +0800372 if (read_MASTER_STAT(iface) & BUSBUSY)
373 return -EAGAIN;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200374
375 iface->writeNum = 0;
376 iface->readNum = 0;
377
378 /* Prepare datas & select mode */
379 switch (size) {
380 case I2C_SMBUS_QUICK:
381 iface->transPtr = NULL;
382 iface->cur_mode = TWI_I2C_MODE_STANDARD;
383 break;
384 case I2C_SMBUS_BYTE:
385 if (data == NULL)
386 iface->transPtr = NULL;
387 else {
388 if (read_write == I2C_SMBUS_READ)
389 iface->readNum = 1;
390 else
391 iface->writeNum = 1;
392 iface->transPtr = &data->byte;
393 }
394 iface->cur_mode = TWI_I2C_MODE_STANDARD;
395 break;
396 case I2C_SMBUS_BYTE_DATA:
397 if (read_write == I2C_SMBUS_READ) {
398 iface->readNum = 1;
399 iface->cur_mode = TWI_I2C_MODE_COMBINED;
400 } else {
401 iface->writeNum = 1;
402 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
403 }
404 iface->transPtr = &data->byte;
405 break;
406 case I2C_SMBUS_WORD_DATA:
407 if (read_write == I2C_SMBUS_READ) {
408 iface->readNum = 2;
409 iface->cur_mode = TWI_I2C_MODE_COMBINED;
410 } else {
411 iface->writeNum = 2;
412 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
413 }
414 iface->transPtr = (u8 *)&data->word;
415 break;
416 case I2C_SMBUS_PROC_CALL:
417 iface->writeNum = 2;
418 iface->readNum = 2;
419 iface->cur_mode = TWI_I2C_MODE_COMBINED;
420 iface->transPtr = (u8 *)&data->word;
421 break;
422 case I2C_SMBUS_BLOCK_DATA:
423 if (read_write == I2C_SMBUS_READ) {
424 iface->readNum = 0;
425 iface->cur_mode = TWI_I2C_MODE_COMBINED;
426 } else {
427 iface->writeNum = data->block[0] + 1;
428 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
429 }
430 iface->transPtr = data->block;
431 break;
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000432 case I2C_SMBUS_I2C_BLOCK_DATA:
433 if (read_write == I2C_SMBUS_READ) {
434 iface->readNum = data->block[0];
435 iface->cur_mode = TWI_I2C_MODE_COMBINED;
436 } else {
437 iface->writeNum = data->block[0];
438 iface->cur_mode = TWI_I2C_MODE_STANDARDSUB;
439 }
440 iface->transPtr = (u8 *)&data->block[1];
441 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200442 default:
443 return -1;
444 }
445
446 iface->result = 0;
447 iface->manual_stop = 0;
448 iface->read_write = read_write;
449 iface->command = command;
Hans Schillstromafc13b72008-04-22 22:16:48 +0200450 init_completion(&(iface->complete));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200451
452 /* FIFO Initiation. Data in FIFO should be discarded before
453 * start a new operation.
454 */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200455 write_FIFO_CTL(iface, 0x3);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200456 SSYNC();
Bryan Wuaa3d0202008-04-22 22:16:48 +0200457 write_FIFO_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200458
459 /* clear int stat */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200460 write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200461
462 /* Set Transmit device address */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200463 write_MASTER_ADDR(iface, addr);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200464 SSYNC();
465
Bryan Wud24ecfc2007-05-01 23:26:32 +0200466 switch (iface->cur_mode) {
467 case TWI_I2C_MODE_STANDARDSUB:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200468 write_XMT_DATA8(iface, iface->command);
469 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200470 ((iface->read_write == I2C_SMBUS_READ) ?
471 RCVSERV : XMTSERV));
472 SSYNC();
473
474 if (iface->writeNum + 1 <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200475 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200476 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200477 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200478 iface->manual_stop = 1;
479 }
480 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200481 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200482 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
483 break;
484 case TWI_I2C_MODE_COMBINED:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200485 write_XMT_DATA8(iface, iface->command);
486 write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200487 SSYNC();
488
489 if (iface->writeNum > 0)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200490 write_MASTER_CTL(iface, (iface->writeNum + 1) << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200491 else
Bryan Wuaa3d0202008-04-22 22:16:48 +0200492 write_MASTER_CTL(iface, 0x1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200493 /* Master enable */
Sonic Zhang28a377c2012-06-13 16:22:44 +0800494 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | RSTART |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200495 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0));
496 break;
497 default:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200498 write_MASTER_CTL(iface, 0);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200499 if (size != I2C_SMBUS_QUICK) {
500 /* Don't access xmit data register when this is a
501 * read operation.
502 */
503 if (iface->read_write != I2C_SMBUS_READ) {
504 if (iface->writeNum > 0) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200505 write_XMT_DATA8(iface,
506 *(iface->transPtr++));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200507 if (iface->writeNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200508 write_MASTER_CTL(iface,
509 iface->writeNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200510 else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200511 write_MASTER_CTL(iface,
512 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200513 iface->manual_stop = 1;
514 }
515 iface->writeNum--;
516 } else {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200517 write_XMT_DATA8(iface, iface->command);
518 write_MASTER_CTL(iface, 1 << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200519 }
520 } else {
521 if (iface->readNum > 0 && iface->readNum <= 255)
Bryan Wuaa3d0202008-04-22 22:16:48 +0200522 write_MASTER_CTL(iface,
523 iface->readNum << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200524 else if (iface->readNum > 255) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200525 write_MASTER_CTL(iface, 0xff << 6);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200526 iface->manual_stop = 1;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400527 } else
Bryan Wud24ecfc2007-05-01 23:26:32 +0200528 break;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200529 }
530 }
Bryan Wuaa3d0202008-04-22 22:16:48 +0200531 write_INT_MASK(iface, MCOMP | MERR |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200532 ((iface->read_write == I2C_SMBUS_READ) ?
533 RCVSERV : XMTSERV));
534 SSYNC();
535
536 /* Master enable */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200537 write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN |
Bryan Wud24ecfc2007-05-01 23:26:32 +0200538 ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) |
539 ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0));
540 break;
541 }
542 SSYNC();
543
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400544 while (!iface->result) {
545 if (!wait_for_completion_timeout(&iface->complete,
546 adap->timeout)) {
547 iface->result = -1;
548 dev_err(&adap->dev, "smbus transfer timeout\n");
549 }
550 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200551
552 rc = (iface->result >= 0) ? 0 : -1;
553
Bryan Wud24ecfc2007-05-01 23:26:32 +0200554 return rc;
555}
556
557/*
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400558 * Generic I2C SMBus transfer entrypoint
559 */
560int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr,
561 unsigned short flags, char read_write,
562 u8 command, int size, union i2c_smbus_data *data)
563{
Sonic Zhangbe2f80f2010-03-22 03:23:19 -0400564 return bfin_twi_do_smbus_xfer(adap, addr, flags,
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400565 read_write, command, size, data);
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400566}
567
568/*
Bryan Wud24ecfc2007-05-01 23:26:32 +0200569 * Return what the adapter supports
570 */
571static u32 bfin_twi_functionality(struct i2c_adapter *adap)
572{
573 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
574 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
575 I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL |
Michael Henneriche0cd2dd2009-05-27 09:24:10 +0000576 I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200577}
578
Bryan Wud24ecfc2007-05-01 23:26:32 +0200579static struct i2c_algorithm bfin_twi_algorithm = {
580 .master_xfer = bfin_twi_master_xfer,
581 .smbus_xfer = bfin_twi_smbus_xfer,
582 .functionality = bfin_twi_functionality,
583};
584
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900585#ifdef CONFIG_PM_SLEEP
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200586static int i2c_bfin_twi_suspend(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200587{
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200588 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Michael Hennerich958585f2008-07-27 14:41:54 +0800589
590 iface->saved_clkdiv = read_CLKDIV(iface);
591 iface->saved_control = read_CONTROL(iface);
592
593 free_irq(iface->irq, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200594
595 /* Disable TWI */
Michael Hennerich958585f2008-07-27 14:41:54 +0800596 write_CONTROL(iface, iface->saved_control & ~TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200597
598 return 0;
599}
600
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200601static int i2c_bfin_twi_resume(struct device *dev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200602{
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200603 struct bfin_twi_iface *iface = dev_get_drvdata(dev);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200604
Michael Hennerich958585f2008-07-27 14:41:54 +0800605 int rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200606 0, to_platform_device(dev)->name, iface);
Michael Hennerich958585f2008-07-27 14:41:54 +0800607 if (rc) {
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200608 dev_err(dev, "Can't get IRQ %d !\n", iface->irq);
Michael Hennerich958585f2008-07-27 14:41:54 +0800609 return -ENODEV;
610 }
611
612 /* Resume TWI interface clock as specified */
613 write_CLKDIV(iface, iface->saved_clkdiv);
614
615 /* Resume TWI */
616 write_CONTROL(iface, iface->saved_control);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200617
618 return 0;
619}
620
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200621static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm,
622 i2c_bfin_twi_suspend, i2c_bfin_twi_resume);
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900623#define I2C_BFIN_TWI_PM_OPS (&i2c_bfin_twi_pm)
624#else
625#define I2C_BFIN_TWI_PM_OPS NULL
626#endif
Rafael J. Wysocki85777ad2012-07-11 21:23:31 +0200627
Bryan Wuaa3d0202008-04-22 22:16:48 +0200628static int i2c_bfin_twi_probe(struct platform_device *pdev)
Bryan Wud24ecfc2007-05-01 23:26:32 +0200629{
Bryan Wuaa3d0202008-04-22 22:16:48 +0200630 struct bfin_twi_iface *iface;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200631 struct i2c_adapter *p_adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200632 struct resource *res;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200633 int rc;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400634 unsigned int clkhilow;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200635
Bryan Wuaa3d0202008-04-22 22:16:48 +0200636 iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL);
637 if (!iface) {
638 dev_err(&pdev->dev, "Cannot allocate memory\n");
639 rc = -ENOMEM;
640 goto out_error_nomem;
641 }
642
Bryan Wud24ecfc2007-05-01 23:26:32 +0200643 spin_lock_init(&(iface->lock));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200644
645 /* Find and map our resources */
646 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
647 if (res == NULL) {
648 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
649 rc = -ENOENT;
650 goto out_error_get_res;
651 }
652
Linus Walleijc6ffdde2009-06-14 00:20:36 +0200653 iface->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200654 if (iface->regs_base == NULL) {
655 dev_err(&pdev->dev, "Cannot map IO\n");
656 rc = -ENXIO;
657 goto out_error_ioremap;
658 }
659
660 iface->irq = platform_get_irq(pdev, 0);
661 if (iface->irq < 0) {
662 dev_err(&pdev->dev, "No IRQ specified\n");
663 rc = -ENOENT;
664 goto out_error_no_irq;
665 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200666
Bryan Wud24ecfc2007-05-01 23:26:32 +0200667 p_adap = &iface->adap;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200668 p_adap->nr = pdev->id;
669 strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name));
Bryan Wud24ecfc2007-05-01 23:26:32 +0200670 p_adap->algo = &bfin_twi_algorithm;
671 p_adap->algo_data = iface;
Wolfram Sang24ed93a2014-02-10 11:04:00 +0100672 p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
Bryan Wuaa3d0202008-04-22 22:16:48 +0200673 p_adap->dev.parent = &pdev->dev;
Sonic Zhangdd7319a2010-03-22 03:23:16 -0400674 p_adap->timeout = 5 * HZ;
675 p_adap->retries = 3;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200676
Jingoo Han6d4028c2013-07-30 16:59:33 +0900677 rc = peripheral_request_list(
Jingoo Han3c41aa72013-09-09 14:32:25 +0900678 dev_get_platdata(&pdev->dev),
Jingoo Han6d4028c2013-07-30 16:59:33 +0900679 "i2c-bfin-twi");
Bryan Wu74d362e2008-04-22 22:16:48 +0200680 if (rc) {
681 dev_err(&pdev->dev, "Can't setup pin mux!\n");
682 goto out_error_pin_mux;
683 }
684
Bryan Wud24ecfc2007-05-01 23:26:32 +0200685 rc = request_irq(iface->irq, bfin_twi_interrupt_entry,
Yong Zhang43110512011-09-21 17:28:33 +0800686 0, pdev->name, iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200687 if (rc) {
Bryan Wuaa3d0202008-04-22 22:16:48 +0200688 dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq);
689 rc = -ENODEV;
690 goto out_error_req_irq;
Bryan Wud24ecfc2007-05-01 23:26:32 +0200691 }
692
693 /* Set TWI internal clock as 10MHz */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500694 write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200695
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400696 /*
697 * We will not end up with a CLKDIV=0 because no one will specify
Sonic Zhangac07fb42009-12-21 09:28:30 -0500698 * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250)
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400699 */
Sonic Zhangac07fb42009-12-21 09:28:30 -0500700 clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2;
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400701
Bryan Wud24ecfc2007-05-01 23:26:32 +0200702 /* Set Twi interface clock as specified */
Michael Hennerich9528d1c2009-05-18 08:14:41 -0400703 write_CLKDIV(iface, (clkhilow << 8) | clkhilow);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200704
705 /* Enable TWI */
Bryan Wuaa3d0202008-04-22 22:16:48 +0200706 write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200707 SSYNC();
708
Kalle Pokki991dee52008-01-27 18:14:52 +0100709 rc = i2c_add_numbered_adapter(p_adap);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200710 if (rc < 0) {
711 dev_err(&pdev->dev, "Can't add i2c adapter!\n");
712 goto out_error_add_adapter;
713 }
Bryan Wud24ecfc2007-05-01 23:26:32 +0200714
Bryan Wuaa3d0202008-04-22 22:16:48 +0200715 platform_set_drvdata(pdev, iface);
716
Bryan Wufa6ad222008-04-22 22:16:48 +0200717 dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, "
718 "regs_base@%p\n", iface->regs_base);
Bryan Wuaa3d0202008-04-22 22:16:48 +0200719
720 return 0;
721
722out_error_add_adapter:
723 free_irq(iface->irq, iface);
724out_error_req_irq:
725out_error_no_irq:
Jingoo Han3c41aa72013-09-09 14:32:25 +0900726 peripheral_free_list(dev_get_platdata(&pdev->dev));
Bryan Wu74d362e2008-04-22 22:16:48 +0200727out_error_pin_mux:
Bryan Wuaa3d0202008-04-22 22:16:48 +0200728 iounmap(iface->regs_base);
729out_error_ioremap:
730out_error_get_res:
731 kfree(iface);
732out_error_nomem:
Bryan Wud24ecfc2007-05-01 23:26:32 +0200733 return rc;
734}
735
736static int i2c_bfin_twi_remove(struct platform_device *pdev)
737{
738 struct bfin_twi_iface *iface = platform_get_drvdata(pdev);
739
Bryan Wud24ecfc2007-05-01 23:26:32 +0200740 i2c_del_adapter(&(iface->adap));
741 free_irq(iface->irq, iface);
Jingoo Han3c41aa72013-09-09 14:32:25 +0900742 peripheral_free_list(dev_get_platdata(&pdev->dev));
Bryan Wuaa3d0202008-04-22 22:16:48 +0200743 iounmap(iface->regs_base);
744 kfree(iface);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200745
746 return 0;
747}
748
749static struct platform_driver i2c_bfin_twi_driver = {
750 .probe = i2c_bfin_twi_probe,
751 .remove = i2c_bfin_twi_remove,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200752 .driver = {
753 .name = "i2c-bfin-twi",
754 .owner = THIS_MODULE,
Jingoo Han2fb9ac02013-07-15 11:30:57 +0900755 .pm = I2C_BFIN_TWI_PM_OPS,
Bryan Wud24ecfc2007-05-01 23:26:32 +0200756 },
757};
758
759static int __init i2c_bfin_twi_init(void)
760{
Bryan Wud24ecfc2007-05-01 23:26:32 +0200761 return platform_driver_register(&i2c_bfin_twi_driver);
762}
763
764static void __exit i2c_bfin_twi_exit(void)
765{
766 platform_driver_unregister(&i2c_bfin_twi_driver);
767}
768
Michael Hennerich74f56c42011-01-11 00:25:09 -0500769subsys_initcall(i2c_bfin_twi_init);
Bryan Wud24ecfc2007-05-01 23:26:32 +0200770module_exit(i2c_bfin_twi_exit);
Bryan Wufa6ad222008-04-22 22:16:48 +0200771
772MODULE_AUTHOR("Bryan Wu, Sonic Zhang");
773MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver");
774MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +0200775MODULE_ALIAS("platform:i2c-bfin-twi");